blob: 22ad00adfd706d006d0984ebe4b75f14fdcf265e [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * linux/arch/arm/kernel/smp.c
3 *
4 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/module.h>
11#include <linux/delay.h>
12#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/sched.h>
15#include <linux/interrupt.h>
16#include <linux/cache.h>
17#include <linux/profile.h>
18#include <linux/errno.h>
19#include <linux/mm.h>
20#include <linux/err.h>
21#include <linux/cpu.h>
22#include <linux/smp.h>
23#include <linux/seq_file.h>
24#include <linux/irq.h>
25#include <linux/percpu.h>
26#include <linux/clockchips.h>
27#include <linux/completion.h>
28
29#include <linux/atomic.h>
30#include <asm/cacheflush.h>
31#include <asm/cpu.h>
32#include <asm/cputype.h>
33#include <asm/exception.h>
34#include <asm/idmap.h>
35#include <asm/topology.h>
36#include <asm/mmu_context.h>
37#include <asm/pgtable.h>
38#include <asm/pgalloc.h>
39#include <asm/processor.h>
40#include <asm/sections.h>
41#include <asm/tlbflush.h>
42#include <asm/ptrace.h>
43#include <asm/localtimer.h>
44#include <asm/smp_plat.h>
45
46/*
47 * as from 2.5, kernels no longer have an init_tasks structure
48 * so we need some other way of telling a new secondary core
49 * where to place its SVC stack
50 */
51struct secondary_data secondary_data;
52
53enum ipi_msg_type {
54 IPI_TIMER = 2,
55 IPI_RESCHEDULE,
56 IPI_CALL_FUNC,
57 IPI_CALL_FUNC_SINGLE,
58 IPI_CPU_STOP,
59 IPI_CPU_BACKTRACE,
60};
61
62static DECLARE_COMPLETION(cpu_running);
63
64int __cpuinit __cpu_up(unsigned int cpu)
65{
66 struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
67 struct task_struct *idle = ci->idle;
68 int ret;
69
70 /*
71 * Spawn a new process manually, if not already done.
72 * Grab a pointer to its task struct so we can mess with it
73 */
74 if (!idle) {
75 idle = fork_idle(cpu);
76 if (IS_ERR(idle)) {
77 printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
78 return PTR_ERR(idle);
79 }
80 ci->idle = idle;
81 } else {
82 /*
83 * Since this idle thread is being re-used, call
84 * init_idle() to reinitialize the thread structure.
85 */
86 init_idle(idle, cpu);
87 }
88
89 /*
90 * We need to tell the secondary core where to find
91 * its stack and the page tables.
92 */
93 secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
94 secondary_data.pgdir = virt_to_phys(idmap_pgd);
95 secondary_data.swapper_pg_dir = virt_to_phys(swapper_pg_dir);
96 __cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data));
97 outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1));
98
99 /*
100 * Now bring the CPU into our world.
101 */
102 ret = boot_secondary(cpu, idle);
103 if (ret == 0) {
104 /*
105 * CPU was successfully started, wait for it
106 * to come online or time out.
107 */
108 wait_for_completion_timeout(&cpu_running,
109 msecs_to_jiffies(1000));
110
111 if (!cpu_online(cpu)) {
112 pr_crit("CPU%u: failed to come online\n", cpu);
113 ret = -EIO;
114 }
115 } else {
116 pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
117 }
118
119 secondary_data.stack = NULL;
120 secondary_data.pgdir = 0;
121
122 return ret;
123}
124
125#ifdef CONFIG_HOTPLUG_CPU
126static void percpu_timer_stop(void);
127
128/*
129 * __cpu_disable runs on the processor to be shutdown.
130 */
131int __cpu_disable(void)
132{
133 unsigned int cpu = smp_processor_id();
134 struct task_struct *p;
135 int ret;
136
137 ret = platform_cpu_disable(cpu);
138 if (ret)
139 return ret;
140
141 /*
142 * Take this CPU offline. Once we clear this, we can't return,
143 * and we must not schedule until we're ready to give up the cpu.
144 */
145 set_cpu_online(cpu, false);
146
147 /*
148 * OK - migrate IRQs away from this CPU
149 */
150 migrate_irqs();
151
152 /*
153 * Stop the local timer for this CPU.
154 */
155 percpu_timer_stop();
156
157 /*
158 * Flush user cache and TLB mappings, and then remove this CPU
159 * from the vm mask set of all processes.
160 */
161 flush_cache_all();
162 local_flush_tlb_all();
163
164 read_lock(&tasklist_lock);
165 for_each_process(p) {
166 if (p->mm)
167 cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
168 }
169 read_unlock(&tasklist_lock);
170
171 return 0;
172}
173
174static DECLARE_COMPLETION(cpu_died);
175
176/*
177 * called on the thread which is asking for a CPU to be shutdown -
178 * waits until shutdown has completed, or it is timed out.
179 */
180void __cpu_die(unsigned int cpu)
181{
182 if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
183 pr_err("CPU%u: cpu didn't die\n", cpu);
184 return;
185 }
186 printk(KERN_NOTICE "CPU%u: shutdown\n", cpu);
187
188 if (!platform_cpu_kill(cpu))
189 printk("CPU%u: unable to kill\n", cpu);
190}
191
192/*
193 * Called from the idle thread for the CPU which has been shutdown.
194 *
195 * Note that we disable IRQs here, but do not re-enable them
196 * before returning to the caller. This is also the behaviour
197 * of the other hotplug-cpu capable cores, so presumably coming
198 * out of idle fixes this.
199 */
200void __ref cpu_die(void)
201{
202 unsigned int cpu = smp_processor_id();
203
204 idle_task_exit();
205
206 local_irq_disable();
207 mb();
208
209 /* Tell __cpu_die() that this CPU is now safe to dispose of */
210 complete(&cpu_died);
211
212 /*
213 * actual CPU shutdown procedure is at least platform (if not
214 * CPU) specific.
215 */
216 platform_cpu_die(cpu);
217
218 /*
219 * Do not return to the idle loop - jump back to the secondary
220 * cpu initialisation. There's some initialisation which needs
221 * to be repeated to undo the effects of taking the CPU offline.
222 */
223 __asm__("mov sp, %0\n"
224 " mov fp, #0\n"
225 " b secondary_start_kernel"
226 :
227 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
228}
229#endif /* CONFIG_HOTPLUG_CPU */
230
231/*
232 * Called by both boot and secondaries to move global data into
233 * per-processor storage.
234 */
235static void __cpuinit smp_store_cpu_info(unsigned int cpuid)
236{
237 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
238
239 cpu_info->loops_per_jiffy = loops_per_jiffy;
240
241 store_cpu_topology(cpuid);
242}
243
244static void percpu_timer_setup(void);
245
246/*
247 * This is the secondary CPU boot entry. We're using this CPUs
248 * idle thread stack, but a set of temporary page tables.
249 */
250asmlinkage void __cpuinit secondary_start_kernel(void)
251{
252 struct mm_struct *mm = &init_mm;
253 unsigned int cpu;
254
255 /*
256 * The identity mapping is uncached (strongly ordered), so
257 * switch away from it before attempting any exclusive accesses.
258 */
259 cpu_switch_mm(mm->pgd, mm);
260 enter_lazy_tlb(mm, current);
261 local_flush_tlb_all();
262
263 /*
264 * All kernel threads share the same mm context; grab a
265 * reference and switch to it.
266 */
267 cpu = smp_processor_id();
268 atomic_inc(&mm->mm_count);
269 current->active_mm = mm;
270 cpumask_set_cpu(cpu, mm_cpumask(mm));
271
272 printk("CPU%u: Booted secondary processor\n", cpu);
273
274 cpu_init();
275 preempt_disable();
276 trace_hardirqs_off();
277
278 /*
279 * Give the platform a chance to do its own initialisation.
280 */
281 platform_secondary_init(cpu);
282
283 notify_cpu_starting(cpu);
284
285 calibrate_delay();
286
287 smp_store_cpu_info(cpu);
288
289 /*
290 * OK, now it's safe to let the boot CPU continue. Wait for
291 * the CPU migration code to notice that the CPU is online
292 * before we continue - which happens after __cpu_up returns.
293 */
294 set_cpu_online(cpu, true);
295 complete(&cpu_running);
296
297 /*
298 * Setup the percpu timer for this CPU.
299 */
300 percpu_timer_setup();
301
302 local_irq_enable();
303 local_fiq_enable();
304
305 /*
306 * OK, it's off to the idle thread for us
307 */
308 cpu_idle();
309}
310
311void __init smp_cpus_done(unsigned int max_cpus)
312{
313 int cpu;
314 unsigned long bogosum = 0;
315
316 for_each_online_cpu(cpu)
317 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
318
319 printk(KERN_INFO "SMP: Total of %d processors activated "
320 "(%lu.%02lu BogoMIPS).\n",
321 num_online_cpus(),
322 bogosum / (500000/HZ),
323 (bogosum / (5000/HZ)) % 100);
324}
325
326void __init smp_prepare_boot_cpu(void)
327{
328 unsigned int cpu = smp_processor_id();
329
330 per_cpu(cpu_data, cpu).idle = current;
331}
332
333void __init smp_prepare_cpus(unsigned int max_cpus)
334{
335 unsigned int ncores = num_possible_cpus();
336
337 init_cpu_topology();
338
339 smp_store_cpu_info(smp_processor_id());
340
341 /*
342 * are we trying to boot more cores than exist?
343 */
344 if (max_cpus > ncores)
345 max_cpus = ncores;
346 if (ncores > 1 && max_cpus) {
347 /*
348 * Enable the local timer or broadcast device for the
349 * boot CPU, but only if we have more than one CPU.
350 */
351 percpu_timer_setup();
352
353 /*
354 * Initialise the present map, which describes the set of CPUs
355 * actually populated at the present time. A platform should
356 * re-initialize the map in platform_smp_prepare_cpus() if
357 * present != possible (e.g. physical hotplug).
358 */
359 init_cpu_present(cpu_possible_mask);
360
361 /*
362 * Initialise the SCU if there are more than one CPU
363 * and let them know where to start.
364 */
365 platform_smp_prepare_cpus(max_cpus);
366 }
367}
368
369static void (*smp_cross_call)(const struct cpumask *, unsigned int);
370
371void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
372{
373 smp_cross_call = fn;
374}
375
376void arch_send_call_function_ipi_mask(const struct cpumask *mask)
377{
378 smp_cross_call(mask, IPI_CALL_FUNC);
379}
380
381void arch_send_call_function_single_ipi(int cpu)
382{
383 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
384}
385
386static const char *ipi_types[NR_IPI] = {
387#define S(x,s) [x - IPI_TIMER] = s
388 S(IPI_TIMER, "Timer broadcast interrupts"),
389 S(IPI_RESCHEDULE, "Rescheduling interrupts"),
390 S(IPI_CALL_FUNC, "Function call interrupts"),
391 S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
392 S(IPI_CPU_STOP, "CPU stop interrupts"),
393 S(IPI_CPU_BACKTRACE, "CPU backtrace"),
394};
395
396void show_ipi_list(struct seq_file *p, int prec)
397{
398 unsigned int cpu, i;
399
400 for (i = 0; i < NR_IPI; i++) {
401 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
402
403 for_each_present_cpu(cpu)
404 seq_printf(p, "%10u ",
405 __get_irq_stat(cpu, ipi_irqs[i]));
406
407 seq_printf(p, " %s\n", ipi_types[i]);
408 }
409}
410
411u64 smp_irq_stat_cpu(unsigned int cpu)
412{
413 u64 sum = 0;
414 int i;
415
416 for (i = 0; i < NR_IPI; i++)
417 sum += __get_irq_stat(cpu, ipi_irqs[i]);
418
419 return sum;
420}
421
422/*
423 * Timer (local or broadcast) support
424 */
425static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
426
427static void ipi_timer(void)
428{
429 struct clock_event_device *evt = &__get_cpu_var(percpu_clockevent);
430 evt->event_handler(evt);
431}
432
433#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
434static void smp_timer_broadcast(const struct cpumask *mask)
435{
436 smp_cross_call(mask, IPI_TIMER);
437}
438#else
439#define smp_timer_broadcast NULL
440#endif
441
442static void broadcast_timer_set_mode(enum clock_event_mode mode,
443 struct clock_event_device *evt)
444{
445}
446
447static void __cpuinit broadcast_timer_setup(struct clock_event_device *evt)
448{
449 evt->name = "dummy_timer";
450 evt->features = CLOCK_EVT_FEAT_ONESHOT |
451 CLOCK_EVT_FEAT_PERIODIC |
452 CLOCK_EVT_FEAT_DUMMY;
453 evt->rating = 400;
454 evt->mult = 1;
455 evt->set_mode = broadcast_timer_set_mode;
456
457 clockevents_register_device(evt);
458}
459
460static struct local_timer_ops *lt_ops;
461
462#ifdef CONFIG_LOCAL_TIMERS
463int local_timer_register(struct local_timer_ops *ops)
464{
465 if (lt_ops)
466 return -EBUSY;
467
468 lt_ops = ops;
469 return 0;
470}
471#endif
472
473static void __cpuinit percpu_timer_setup(void)
474{
475 unsigned int cpu = smp_processor_id();
476 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
477
478 evt->cpumask = cpumask_of(cpu);
479 evt->broadcast = smp_timer_broadcast;
480
481 if (!lt_ops || lt_ops->setup(evt))
482 broadcast_timer_setup(evt);
483}
484
485#ifdef CONFIG_HOTPLUG_CPU
486/*
487 * The generic clock events code purposely does not stop the local timer
488 * on CPU_DEAD/CPU_DEAD_FROZEN hotplug events, so we have to do it
489 * manually here.
490 */
491static void percpu_timer_stop(void)
492{
493 unsigned int cpu = smp_processor_id();
494 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
495
496 if (lt_ops)
497 lt_ops->stop(evt);
498}
499#endif
500
501static DEFINE_RAW_SPINLOCK(stop_lock);
502
503/*
504 * ipi_cpu_stop - handle IPI from smp_send_stop()
505 */
506static void ipi_cpu_stop(unsigned int cpu)
507{
508 if (system_state == SYSTEM_BOOTING ||
509 system_state == SYSTEM_RUNNING) {
510 raw_spin_lock(&stop_lock);
511 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
512 dump_stack();
513 raw_spin_unlock(&stop_lock);
514 }
515
516 set_cpu_online(cpu, false);
517
518 local_fiq_disable();
519 local_irq_disable();
520
521 while (1)
522 cpu_relax();
523}
524
525static cpumask_t backtrace_mask;
526static DEFINE_RAW_SPINLOCK(backtrace_lock);
527
528/* "in progress" flag of arch_trigger_all_cpu_backtrace */
529static unsigned long backtrace_flag;
530
531void smp_send_all_cpu_backtrace(void)
532{
533 unsigned int this_cpu = smp_processor_id();
534 int i;
535
536 if (test_and_set_bit(0, &backtrace_flag))
537 /*
538 * If there is already a trigger_all_cpu_backtrace() in progress
539 * (backtrace_flag == 1), don't output double cpu dump infos.
540 */
541 return;
542
543 cpumask_copy(&backtrace_mask, cpu_online_mask);
544 cpu_clear(this_cpu, backtrace_mask);
545
546 pr_info("Backtrace for cpu %d (current):\n", this_cpu);
547 dump_stack();
548
549 pr_info("\nsending IPI to all other CPUs:\n");
550 smp_cross_call(&backtrace_mask, IPI_CPU_BACKTRACE);
551
552 /* Wait for up to 10 seconds for all other CPUs to do the backtrace */
553 for (i = 0; i < 10 * 1000; i++) {
554 if (cpumask_empty(&backtrace_mask))
555 break;
556 mdelay(1);
557 }
558
559 clear_bit(0, &backtrace_flag);
560 smp_mb__after_clear_bit();
561}
562
563/*
564 * ipi_cpu_backtrace - handle IPI from smp_send_all_cpu_backtrace()
565 */
566static void ipi_cpu_backtrace(unsigned int cpu, struct pt_regs *regs)
567{
568 if (cpu_isset(cpu, backtrace_mask)) {
569 raw_spin_lock(&backtrace_lock);
570 pr_warning("IPI backtrace for cpu %d\n", cpu);
571 show_regs(regs);
572 raw_spin_unlock(&backtrace_lock);
573 cpu_clear(cpu, backtrace_mask);
574 }
575}
576
577/*
578 * Main handler for inter-processor interrupts
579 */
580asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs)
581{
582 handle_IPI(ipinr, regs);
583}
584
585void handle_IPI(int ipinr, struct pt_regs *regs)
586{
587 unsigned int cpu = smp_processor_id();
588 struct pt_regs *old_regs = set_irq_regs(regs);
589
590 if (ipinr >= IPI_TIMER && ipinr < IPI_TIMER + NR_IPI)
591 __inc_irq_stat(cpu, ipi_irqs[ipinr - IPI_TIMER]);
592
593 switch (ipinr) {
594 case IPI_TIMER:
595 irq_enter();
596 ipi_timer();
597 irq_exit();
598 break;
599
600 case IPI_RESCHEDULE:
601 scheduler_ipi();
602 break;
603
604 case IPI_CALL_FUNC:
605 irq_enter();
606 generic_smp_call_function_interrupt();
607 irq_exit();
608 break;
609
610 case IPI_CALL_FUNC_SINGLE:
611 irq_enter();
612 generic_smp_call_function_single_interrupt();
613 irq_exit();
614 break;
615
616 case IPI_CPU_STOP:
617 irq_enter();
618 ipi_cpu_stop(cpu);
619 irq_exit();
620 break;
621
622 case IPI_CPU_BACKTRACE:
623 ipi_cpu_backtrace(cpu, regs);
624 break;
625
626 default:
627 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
628 cpu, ipinr);
629 break;
630 }
631 set_irq_regs(old_regs);
632}
633
634void smp_send_reschedule(int cpu)
635{
636 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
637}
638
639#ifdef CONFIG_HOTPLUG_CPU
640static void smp_kill_cpus(cpumask_t *mask)
641{
642 unsigned int cpu;
643 for_each_cpu(cpu, mask)
644 platform_cpu_kill(cpu);
645}
646#else
647static void smp_kill_cpus(cpumask_t *mask) { }
648#endif
649
650void smp_send_stop(void)
651{
652 unsigned long timeout;
653 struct cpumask mask;
654
655 cpumask_copy(&mask, cpu_online_mask);
656 cpumask_clear_cpu(smp_processor_id(), &mask);
657 if (!cpumask_empty(&mask))
658 smp_cross_call(&mask, IPI_CPU_STOP);
659
660 /* Wait up to one second for other CPUs to stop */
661 timeout = USEC_PER_SEC;
662 while (num_online_cpus() > 1 && timeout--)
663 udelay(1);
664
665 if (num_online_cpus() > 1)
666 pr_warning("SMP: failed to stop secondary CPUs\n");
667
668 smp_kill_cpus(&mask);
669}
670
671/*
672 * not supported here
673 */
674int setup_profiling_timer(unsigned int multiplier)
675{
676 return -EINVAL;
677}