1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/spinlock.h>
23#include <linux/sched.h>
24#include <linux/interrupt.h>
25#include <linux/cache.h>
26#include <linux/profile.h>
27#include <linux/errno.h>
28#include <linux/mm.h>
29#include <linux/err.h>
30#include <linux/cpu.h>
31#include <linux/smp.h>
32#include <linux/seq_file.h>
33#include <linux/irq.h>
34#include <linux/percpu.h>
35#include <linux/clockchips.h>
36#include <linux/completion.h>
37#include <linux/of.h>
38#include <linux/irq_work.h>
39
40#include <asm/alternative.h>
41#include <asm/atomic.h>
42#include <asm/cacheflush.h>
43#include <asm/cpu.h>
44#include <asm/cputype.h>
45#include <asm/cpu_ops.h>
46#include <asm/mmu_context.h>
47#include <asm/pgtable.h>
48#include <asm/pgalloc.h>
49#include <asm/processor.h>
50#include <asm/smp_plat.h>
51#include <asm/sections.h>
52#include <asm/tlbflush.h>
53#include <asm/ptrace.h>
54
55#define CREATE_TRACE_POINTS
56#include <trace/events/ipi.h>
57
58
59
60
61
62
63struct secondary_data secondary_data;
64
65enum ipi_msg_type {
66 IPI_RESCHEDULE,
67 IPI_CALL_FUNC,
68 IPI_CALL_FUNC_SINGLE,
69 IPI_CPU_STOP,
70 IPI_TIMER,
71 IPI_IRQ_WORK,
72};
73
74
75
76
77
78static int boot_secondary(unsigned int cpu, struct task_struct *idle)
79{
80 if (cpu_ops[cpu]->cpu_boot)
81 return cpu_ops[cpu]->cpu_boot(cpu);
82
83 return -EOPNOTSUPP;
84}
85
86static DECLARE_COMPLETION(cpu_running);
87
88int __cpu_up(unsigned int cpu, struct task_struct *idle)
89{
90 int ret;
91
92
93
94
95
96 secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
97 __flush_dcache_area(&secondary_data, sizeof(secondary_data));
98
99
100
101
102 ret = boot_secondary(cpu, idle);
103 if (ret == 0) {
104
105
106
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
121 return ret;
122}
123
124static void smp_store_cpu_info(unsigned int cpuid)
125{
126 store_cpu_topology(cpuid);
127}
128
129
130
131
132
133asmlinkage void secondary_start_kernel(void)
134{
135 struct mm_struct *mm = &init_mm;
136 unsigned int cpu = smp_processor_id();
137
138
139
140
141
142 atomic_inc(&mm->mm_count);
143 current->active_mm = mm;
144 cpumask_set_cpu(cpu, mm_cpumask(mm));
145
146 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
147 printk("CPU%u: Booted secondary processor\n", cpu);
148
149
150
151
152
153 cpu_set_reserved_ttbr0();
154 flush_tlb_all();
155
156 preempt_disable();
157 trace_hardirqs_off();
158
159 if (cpu_ops[cpu]->cpu_postboot)
160 cpu_ops[cpu]->cpu_postboot();
161
162
163
164
165 cpuinfo_store_cpu();
166
167
168
169
170 notify_cpu_starting(cpu);
171
172 smp_store_cpu_info(cpu);
173
174
175
176
177
178
179 set_cpu_online(cpu, true);
180 complete(&cpu_running);
181
182 local_dbg_enable();
183 local_irq_enable();
184 local_async_enable();
185
186
187
188
189 cpu_startup_entry(CPUHP_ONLINE);
190}
191
192#ifdef CONFIG_HOTPLUG_CPU
193static int op_cpu_disable(unsigned int cpu)
194{
195
196
197
198
199 if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_die)
200 return -EOPNOTSUPP;
201
202
203
204
205
206 if (cpu_ops[cpu]->cpu_disable)
207 return cpu_ops[cpu]->cpu_disable(cpu);
208
209 return 0;
210}
211
212
213
214
215int __cpu_disable(void)
216{
217 unsigned int cpu = smp_processor_id();
218 int ret;
219
220 ret = op_cpu_disable(cpu);
221 if (ret)
222 return ret;
223
224
225
226
227
228 set_cpu_online(cpu, false);
229
230
231
232
233 migrate_irqs();
234
235
236
237
238 clear_tasks_mm_cpumask(cpu);
239
240 return 0;
241}
242
243static int op_cpu_kill(unsigned int cpu)
244{
245
246
247
248
249
250 if (!cpu_ops[cpu]->cpu_kill)
251 return 1;
252
253 return cpu_ops[cpu]->cpu_kill(cpu);
254}
255
256static DECLARE_COMPLETION(cpu_died);
257
258
259
260
261
262void __cpu_die(unsigned int cpu)
263{
264 if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
265 pr_crit("CPU%u: cpu didn't die\n", cpu);
266 return;
267 }
268 pr_notice("CPU%u: shutdown\n", cpu);
269
270
271
272
273
274
275
276 if (!op_cpu_kill(cpu))
277 pr_warn("CPU%d may not have shut down cleanly\n", cpu);
278}
279
280
281
282
283
284
285
286
287
288void cpu_die(void)
289{
290 unsigned int cpu = smp_processor_id();
291
292 idle_task_exit();
293
294 local_irq_disable();
295
296
297 complete(&cpu_died);
298
299
300
301
302
303
304 cpu_ops[cpu]->cpu_die(cpu);
305
306 BUG();
307}
308#endif
309
310void __init smp_cpus_done(unsigned int max_cpus)
311{
312 pr_info("SMP: Total of %d processors activated.\n", num_online_cpus());
313 apply_alternatives_all();
314}
315
316void __init smp_prepare_boot_cpu(void)
317{
318 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
319}
320
321
322
323
324
325
326void __init smp_init_cpus(void)
327{
328 struct device_node *dn = NULL;
329 unsigned int i, cpu = 1;
330 bool bootcpu_valid = false;
331
332 while ((dn = of_find_node_by_type(dn, "cpu"))) {
333 const u32 *cell;
334 u64 hwid;
335
336
337
338
339
340
341 cell = of_get_property(dn, "reg", NULL);
342 if (!cell) {
343 pr_err("%s: missing reg property\n", dn->full_name);
344 goto next;
345 }
346 hwid = of_read_number(cell, of_n_addr_cells(dn));
347
348
349
350
351 if (hwid & ~MPIDR_HWID_BITMASK) {
352 pr_err("%s: invalid reg property\n", dn->full_name);
353 goto next;
354 }
355
356
357
358
359
360
361
362
363 for (i = 1; (i < cpu) && (i < NR_CPUS); i++) {
364 if (cpu_logical_map(i) == hwid) {
365 pr_err("%s: duplicate cpu reg properties in the DT\n",
366 dn->full_name);
367 goto next;
368 }
369 }
370
371
372
373
374
375
376
377 if (hwid == cpu_logical_map(0)) {
378 if (bootcpu_valid) {
379 pr_err("%s: duplicate boot cpu reg property in DT\n",
380 dn->full_name);
381 goto next;
382 }
383
384 bootcpu_valid = true;
385
386
387
388
389
390
391
392 continue;
393 }
394
395 if (cpu >= NR_CPUS)
396 goto next;
397
398 if (cpu_read_ops(dn, cpu) != 0)
399 goto next;
400
401 if (cpu_ops[cpu]->cpu_init(dn, cpu))
402 goto next;
403
404 pr_debug("cpu logical map 0x%llx\n", hwid);
405 cpu_logical_map(cpu) = hwid;
406next:
407 cpu++;
408 }
409
410
411 if (cpu > NR_CPUS)
412 pr_warning("no. of cores (%d) greater than configured maximum of %d - clipping\n",
413 cpu, NR_CPUS);
414
415 if (!bootcpu_valid) {
416 pr_err("DT missing boot CPU MPIDR, not enabling secondaries\n");
417 return;
418 }
419
420
421
422
423
424 for (i = 0; i < NR_CPUS; i++)
425 if (cpu_logical_map(i) != INVALID_HWID)
426 set_cpu_possible(i, true);
427}
428
429void __init smp_prepare_cpus(unsigned int max_cpus)
430{
431 int err;
432 unsigned int cpu, ncores = num_possible_cpus();
433
434 init_cpu_topology();
435
436 smp_store_cpu_info(smp_processor_id());
437
438
439
440
441 if (max_cpus > ncores)
442 max_cpus = ncores;
443
444
445 if (max_cpus <= 1)
446 return;
447
448
449
450
451
452
453
454
455 max_cpus--;
456 for_each_possible_cpu(cpu) {
457 if (max_cpus == 0)
458 break;
459
460 if (cpu == smp_processor_id())
461 continue;
462
463 if (!cpu_ops[cpu])
464 continue;
465
466 err = cpu_ops[cpu]->cpu_prepare(cpu);
467 if (err)
468 continue;
469
470 set_cpu_present(cpu, true);
471 max_cpus--;
472 }
473}
474
475void (*__smp_cross_call)(const struct cpumask *, unsigned int);
476
477void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
478{
479 __smp_cross_call = fn;
480}
481
482static const char *ipi_types[NR_IPI] __tracepoint_string = {
483#define S(x,s) [x] = s
484 S(IPI_RESCHEDULE, "Rescheduling interrupts"),
485 S(IPI_CALL_FUNC, "Function call interrupts"),
486 S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
487 S(IPI_CPU_STOP, "CPU stop interrupts"),
488 S(IPI_TIMER, "Timer broadcast interrupts"),
489 S(IPI_IRQ_WORK, "IRQ work interrupts"),
490};
491
492static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
493{
494 trace_ipi_raise(target, ipi_types[ipinr]);
495 __smp_cross_call(target, ipinr);
496}
497
498void show_ipi_list(struct seq_file *p, int prec)
499{
500 unsigned int cpu, i;
501
502 for (i = 0; i < NR_IPI; i++) {
503 seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
504 prec >= 4 ? " " : "");
505 for_each_online_cpu(cpu)
506 seq_printf(p, "%10u ",
507 __get_irq_stat(cpu, ipi_irqs[i]));
508 seq_printf(p, " %s\n", ipi_types[i]);
509 }
510}
511
512u64 smp_irq_stat_cpu(unsigned int cpu)
513{
514 u64 sum = 0;
515 int i;
516
517 for (i = 0; i < NR_IPI; i++)
518 sum += __get_irq_stat(cpu, ipi_irqs[i]);
519
520 return sum;
521}
522
523void arch_send_call_function_ipi_mask(const struct cpumask *mask)
524{
525 smp_cross_call(mask, IPI_CALL_FUNC);
526}
527
528void arch_send_call_function_single_ipi(int cpu)
529{
530 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
531}
532
533#ifdef CONFIG_IRQ_WORK
534void arch_irq_work_raise(void)
535{
536 if (__smp_cross_call)
537 smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
538}
539#endif
540
541static DEFINE_RAW_SPINLOCK(stop_lock);
542
543
544
545
546static void ipi_cpu_stop(unsigned int cpu)
547{
548 if (system_state == SYSTEM_BOOTING ||
549 system_state == SYSTEM_RUNNING) {
550 raw_spin_lock(&stop_lock);
551 pr_crit("CPU%u: stopping\n", cpu);
552 dump_stack();
553 raw_spin_unlock(&stop_lock);
554 }
555
556 set_cpu_online(cpu, false);
557
558 local_irq_disable();
559
560 while (1)
561 cpu_relax();
562}
563
564
565
566
567void handle_IPI(int ipinr, struct pt_regs *regs)
568{
569 unsigned int cpu = smp_processor_id();
570 struct pt_regs *old_regs = set_irq_regs(regs);
571
572 if ((unsigned)ipinr < NR_IPI) {
573 trace_ipi_entry(ipi_types[ipinr]);
574 __inc_irq_stat(cpu, ipi_irqs[ipinr]);
575 }
576
577 switch (ipinr) {
578 case IPI_RESCHEDULE:
579 scheduler_ipi();
580 break;
581
582 case IPI_CALL_FUNC:
583 irq_enter();
584 generic_smp_call_function_interrupt();
585 irq_exit();
586 break;
587
588 case IPI_CALL_FUNC_SINGLE:
589 irq_enter();
590 generic_smp_call_function_single_interrupt();
591 irq_exit();
592 break;
593
594 case IPI_CPU_STOP:
595 irq_enter();
596 ipi_cpu_stop(cpu);
597 irq_exit();
598 break;
599
600#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
601 case IPI_TIMER:
602 irq_enter();
603 tick_receive_broadcast();
604 irq_exit();
605 break;
606#endif
607
608#ifdef CONFIG_IRQ_WORK
609 case IPI_IRQ_WORK:
610 irq_enter();
611 irq_work_run();
612 irq_exit();
613 break;
614#endif
615
616 default:
617 pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
618 break;
619 }
620
621 if ((unsigned)ipinr < NR_IPI)
622 trace_ipi_exit(ipi_types[ipinr]);
623 set_irq_regs(old_regs);
624}
625
626void smp_send_reschedule(int cpu)
627{
628 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
629}
630
631#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
632void tick_broadcast(const struct cpumask *mask)
633{
634 smp_cross_call(mask, IPI_TIMER);
635}
636#endif
637
638void smp_send_stop(void)
639{
640 unsigned long timeout;
641
642 if (num_online_cpus() > 1) {
643 cpumask_t mask;
644
645 cpumask_copy(&mask, cpu_online_mask);
646 cpu_clear(smp_processor_id(), mask);
647
648 smp_cross_call(&mask, IPI_CPU_STOP);
649 }
650
651
652 timeout = USEC_PER_SEC;
653 while (num_online_cpus() > 1 && timeout--)
654 udelay(1);
655
656 if (num_online_cpus() > 1)
657 pr_warning("SMP: failed to stop secondary CPUs\n");
658}
659
660
661
662
663int setup_profiling_timer(unsigned int multiplier)
664{
665 return -EINVAL;
666}
667