1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 *
5 * Derived from MIPS:
6 * Copyright (C) 2000, 2001 Kanoj Sarcar
7 * Copyright (C) 2000, 2001 Ralf Baechle
8 * Copyright (C) 2000, 2001 Silicon Graphics, Inc.
9 * Copyright (C) 2000, 2001, 2003 Broadcom Corporation
10 */
11 #include <linux/acpi.h>
12 #include <linux/cpu.h>
13 #include <linux/cpumask.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq_work.h>
17 #include <linux/profile.h>
18 #include <linux/seq_file.h>
19 #include <linux/smp.h>
20 #include <linux/threads.h>
21 #include <linux/export.h>
22 #include <linux/suspend.h>
23 #include <linux/syscore_ops.h>
24 #include <linux/time.h>
25 #include <linux/tracepoint.h>
26 #include <linux/sched/hotplug.h>
27 #include <linux/sched/task_stack.h>
28
29 #include <asm/cpu.h>
30 #include <asm/idle.h>
31 #include <asm/loongson.h>
32 #include <asm/mmu_context.h>
33 #include <asm/numa.h>
34 #include <asm/paravirt.h>
35 #include <asm/processor.h>
36 #include <asm/setup.h>
37 #include <asm/time.h>
38
39 int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
40 EXPORT_SYMBOL(__cpu_number_map);
41
42 int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
43 EXPORT_SYMBOL(__cpu_logical_map);
44
45 /* Representing the threads (siblings) of each logical CPU */
46 cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
47 EXPORT_SYMBOL(cpu_sibling_map);
48
49 /* Representing the last level cache shared map of each logical CPU */
50 cpumask_t cpu_llc_shared_map[NR_CPUS] __read_mostly;
51 EXPORT_SYMBOL(cpu_llc_shared_map);
52
53 /* Representing the core map of multi-core chips of each logical CPU */
54 cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
55 EXPORT_SYMBOL(cpu_core_map);
56
57 static DECLARE_COMPLETION(cpu_starting);
58 static DECLARE_COMPLETION(cpu_running);
59
60 /*
61 * A logcal cpu mask containing only one VPE per core to
62 * reduce the number of IPIs on large MT systems.
63 */
64 cpumask_t cpu_foreign_map[NR_CPUS] __read_mostly;
65 EXPORT_SYMBOL(cpu_foreign_map);
66
67 /* representing cpus for which sibling maps can be computed */
68 static cpumask_t cpu_sibling_setup_map;
69
70 /* representing cpus for which llc shared maps can be computed */
71 static cpumask_t cpu_llc_shared_setup_map;
72
73 /* representing cpus for which core maps can be computed */
74 static cpumask_t cpu_core_setup_map;
75
76 struct secondary_data cpuboot_data;
77 static DEFINE_PER_CPU(int, cpu_state);
78
79 static const char *ipi_types[NR_IPI] __tracepoint_string = {
80 [IPI_RESCHEDULE] = "Rescheduling interrupts",
81 [IPI_CALL_FUNCTION] = "Function call interrupts",
82 [IPI_IRQ_WORK] = "IRQ work interrupts",
83 [IPI_CLEAR_VECTOR] = "Clear vector interrupts",
84 };
85
show_ipi_list(struct seq_file * p,int prec)86 void show_ipi_list(struct seq_file *p, int prec)
87 {
88 unsigned int cpu, i;
89
90 for (i = 0; i < NR_IPI; i++) {
91 seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i, prec >= 4 ? " " : "");
92 for_each_online_cpu(cpu)
93 seq_put_decimal_ull_width(p, " ", per_cpu(irq_stat, cpu).ipi_irqs[i], 10);
94 seq_printf(p, " LoongArch %d %s\n", i + 1, ipi_types[i]);
95 }
96 }
97
set_cpu_core_map(int cpu)98 static inline void set_cpu_core_map(int cpu)
99 {
100 int i;
101
102 cpumask_set_cpu(cpu, &cpu_core_setup_map);
103
104 for_each_cpu(i, &cpu_core_setup_map) {
105 if (cpu_data[cpu].package == cpu_data[i].package) {
106 cpumask_set_cpu(i, &cpu_core_map[cpu]);
107 cpumask_set_cpu(cpu, &cpu_core_map[i]);
108 }
109 }
110 }
111
set_cpu_llc_shared_map(int cpu)112 static inline void set_cpu_llc_shared_map(int cpu)
113 {
114 int i;
115
116 cpumask_set_cpu(cpu, &cpu_llc_shared_setup_map);
117
118 for_each_cpu(i, &cpu_llc_shared_setup_map) {
119 if (cpu_to_node(cpu) == cpu_to_node(i)) {
120 cpumask_set_cpu(i, &cpu_llc_shared_map[cpu]);
121 cpumask_set_cpu(cpu, &cpu_llc_shared_map[i]);
122 }
123 }
124 }
125
clear_cpu_llc_shared_map(int cpu)126 static inline void clear_cpu_llc_shared_map(int cpu)
127 {
128 int i;
129
130 for_each_cpu(i, &cpu_llc_shared_setup_map) {
131 if (cpu_to_node(cpu) == cpu_to_node(i)) {
132 cpumask_clear_cpu(i, &cpu_llc_shared_map[cpu]);
133 cpumask_clear_cpu(cpu, &cpu_llc_shared_map[i]);
134 }
135 }
136
137 cpumask_clear_cpu(cpu, &cpu_llc_shared_setup_map);
138 }
139
set_cpu_sibling_map(int cpu)140 static inline void set_cpu_sibling_map(int cpu)
141 {
142 int i;
143
144 cpumask_set_cpu(cpu, &cpu_sibling_setup_map);
145
146 for_each_cpu(i, &cpu_sibling_setup_map) {
147 if (cpus_are_siblings(cpu, i)) {
148 cpumask_set_cpu(i, &cpu_sibling_map[cpu]);
149 cpumask_set_cpu(cpu, &cpu_sibling_map[i]);
150 }
151 }
152 }
153
clear_cpu_sibling_map(int cpu)154 static inline void clear_cpu_sibling_map(int cpu)
155 {
156 int i;
157
158 for_each_cpu(i, &cpu_sibling_setup_map) {
159 if (cpus_are_siblings(cpu, i)) {
160 cpumask_clear_cpu(i, &cpu_sibling_map[cpu]);
161 cpumask_clear_cpu(cpu, &cpu_sibling_map[i]);
162 }
163 }
164
165 cpumask_clear_cpu(cpu, &cpu_sibling_setup_map);
166 }
167
168 /*
169 * Calculate a new cpu_foreign_map mask whenever a
170 * new cpu appears or disappears.
171 */
calculate_cpu_foreign_map(void)172 void calculate_cpu_foreign_map(void)
173 {
174 int i, k, core_present;
175 cpumask_t temp_foreign_map;
176
177 /* Re-calculate the mask */
178 cpumask_clear(&temp_foreign_map);
179 for_each_online_cpu(i) {
180 core_present = 0;
181 for_each_cpu(k, &temp_foreign_map)
182 if (cpus_are_siblings(i, k))
183 core_present = 1;
184 if (!core_present)
185 cpumask_set_cpu(i, &temp_foreign_map);
186 }
187
188 for_each_online_cpu(i)
189 cpumask_andnot(&cpu_foreign_map[i],
190 &temp_foreign_map, &cpu_sibling_map[i]);
191 }
192
193 /* Send mailbox buffer via Mail_Send */
csr_mail_send(uint64_t data,int cpu,int mailbox)194 static void csr_mail_send(uint64_t data, int cpu, int mailbox)
195 {
196 uint64_t val;
197
198 /* Send high 32 bits */
199 val = IOCSR_MBUF_SEND_BLOCKING;
200 val |= (IOCSR_MBUF_SEND_BOX_HI(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
201 val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
202 val |= (data & IOCSR_MBUF_SEND_H32_MASK);
203 iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
204
205 /* Send low 32 bits */
206 val = IOCSR_MBUF_SEND_BLOCKING;
207 val |= (IOCSR_MBUF_SEND_BOX_LO(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
208 val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
209 val |= (data << IOCSR_MBUF_SEND_BUF_SHIFT);
210 iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
211 };
212
ipi_read_clear(int cpu)213 static u32 ipi_read_clear(int cpu)
214 {
215 u32 action;
216
217 /* Load the ipi register to figure out what we're supposed to do */
218 action = iocsr_read32(LOONGARCH_IOCSR_IPI_STATUS);
219 /* Clear the ipi register to clear the interrupt */
220 iocsr_write32(action, LOONGARCH_IOCSR_IPI_CLEAR);
221 wbflush();
222
223 return action;
224 }
225
ipi_write_action(int cpu,u32 action)226 static void ipi_write_action(int cpu, u32 action)
227 {
228 uint32_t val;
229
230 val = IOCSR_IPI_SEND_BLOCKING | action;
231 val |= (cpu << IOCSR_IPI_SEND_CPU_SHIFT);
232 iocsr_write32(val, LOONGARCH_IOCSR_IPI_SEND);
233 }
234
loongson_send_ipi_single(int cpu,unsigned int action)235 static void loongson_send_ipi_single(int cpu, unsigned int action)
236 {
237 ipi_write_action(cpu_logical_map(cpu), (u32)action);
238 }
239
loongson_send_ipi_mask(const struct cpumask * mask,unsigned int action)240 static void loongson_send_ipi_mask(const struct cpumask *mask, unsigned int action)
241 {
242 unsigned int i;
243
244 for_each_cpu(i, mask)
245 ipi_write_action(cpu_logical_map(i), (u32)action);
246 }
247
248 /*
249 * This function sends a 'reschedule' IPI to another CPU.
250 * it goes straight through and wastes no time serializing
251 * anything. Worst case is that we lose a reschedule ...
252 */
arch_smp_send_reschedule(int cpu)253 void arch_smp_send_reschedule(int cpu)
254 {
255 mp_ops.send_ipi_single(cpu, ACTION_RESCHEDULE);
256 }
257 EXPORT_SYMBOL_GPL(arch_smp_send_reschedule);
258
259 #ifdef CONFIG_IRQ_WORK
arch_irq_work_raise(void)260 void arch_irq_work_raise(void)
261 {
262 mp_ops.send_ipi_single(smp_processor_id(), ACTION_IRQ_WORK);
263 }
264 #endif
265
loongson_ipi_interrupt(int irq,void * dev)266 static irqreturn_t loongson_ipi_interrupt(int irq, void *dev)
267 {
268 unsigned int action;
269 unsigned int cpu = smp_processor_id();
270
271 action = ipi_read_clear(cpu_logical_map(cpu));
272
273 if (action & SMP_RESCHEDULE) {
274 scheduler_ipi();
275 per_cpu(irq_stat, cpu).ipi_irqs[IPI_RESCHEDULE]++;
276 }
277
278 if (action & SMP_CALL_FUNCTION) {
279 generic_smp_call_function_interrupt();
280 per_cpu(irq_stat, cpu).ipi_irqs[IPI_CALL_FUNCTION]++;
281 }
282
283 if (action & SMP_IRQ_WORK) {
284 irq_work_run();
285 per_cpu(irq_stat, cpu).ipi_irqs[IPI_IRQ_WORK]++;
286 }
287
288 if (action & SMP_CLEAR_VECTOR) {
289 complete_irq_moving();
290 per_cpu(irq_stat, cpu).ipi_irqs[IPI_CLEAR_VECTOR]++;
291 }
292
293 return IRQ_HANDLED;
294 }
295
loongson_init_ipi(void)296 static void loongson_init_ipi(void)
297 {
298 int r, ipi_irq;
299
300 ipi_irq = get_percpu_irq(INT_IPI);
301 if (ipi_irq < 0)
302 panic("IPI IRQ mapping failed\n");
303
304 irq_set_percpu_devid(ipi_irq);
305 r = request_percpu_irq(ipi_irq, loongson_ipi_interrupt, "IPI", &irq_stat);
306 if (r < 0)
307 panic("IPI IRQ request failed\n");
308 }
309
310 struct smp_ops mp_ops = {
311 .init_ipi = loongson_init_ipi,
312 .send_ipi_single = loongson_send_ipi_single,
313 .send_ipi_mask = loongson_send_ipi_mask,
314 };
315
fdt_smp_setup(void)316 static void __init fdt_smp_setup(void)
317 {
318 #ifdef CONFIG_OF
319 unsigned int cpu, cpuid;
320 struct device_node *node = NULL;
321
322 for_each_of_cpu_node(node) {
323 if (!of_device_is_available(node))
324 continue;
325
326 cpuid = of_get_cpu_hwid(node, 0);
327 if (cpuid >= nr_cpu_ids)
328 continue;
329
330 if (cpuid == loongson_sysconf.boot_cpu_id)
331 cpu = 0;
332 else
333 cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS);
334
335 num_processors++;
336 set_cpu_possible(cpu, true);
337 set_cpu_present(cpu, true);
338 __cpu_number_map[cpuid] = cpu;
339 __cpu_logical_map[cpu] = cpuid;
340
341 early_numa_add_cpu(cpuid, 0);
342 set_cpuid_to_node(cpuid, 0);
343 }
344
345 loongson_sysconf.nr_cpus = num_processors;
346 set_bit(0, loongson_sysconf.cores_io_master);
347 #endif
348 }
349
loongson_smp_setup(void)350 void __init loongson_smp_setup(void)
351 {
352 fdt_smp_setup();
353
354 if (loongson_sysconf.cores_per_package == 0)
355 loongson_sysconf.cores_per_package = num_processors;
356
357 cpu_data[0].core = cpu_logical_map(0) % loongson_sysconf.cores_per_package;
358 cpu_data[0].package = cpu_logical_map(0) / loongson_sysconf.cores_per_package;
359
360 pv_ipi_init();
361 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
362 pr_info("Detected %i available CPU(s)\n", loongson_sysconf.nr_cpus);
363 }
364
loongson_prepare_cpus(unsigned int max_cpus)365 void __init loongson_prepare_cpus(unsigned int max_cpus)
366 {
367 int i = 0;
368 int threads_per_core = 0;
369
370 parse_acpi_topology();
371 cpu_data[0].global_id = cpu_logical_map(0);
372
373 if (!pptt_enabled)
374 threads_per_core = 1;
375 else {
376 for_each_possible_cpu(i) {
377 if (cpu_to_node(i) != 0)
378 continue;
379 if (cpus_are_siblings(0, i))
380 threads_per_core++;
381 }
382 }
383
384 for (i = 0; i < loongson_sysconf.nr_cpus; i++) {
385 set_cpu_present(i, true);
386 csr_mail_send(0, __cpu_logical_map[i], 0);
387 }
388
389 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
390 cpu_smt_set_num_threads(threads_per_core, threads_per_core);
391 }
392
393 /*
394 * Setup the PC, SP, and TP of a secondary processor and start it running!
395 */
loongson_boot_secondary(int cpu,struct task_struct * idle)396 void loongson_boot_secondary(int cpu, struct task_struct *idle)
397 {
398 unsigned long entry;
399
400 pr_info("Booting CPU#%d...\n", cpu);
401
402 entry = __pa_symbol((unsigned long)&smpboot_entry);
403 cpuboot_data.stack = (unsigned long)__KSTK_TOS(idle);
404 cpuboot_data.thread_info = (unsigned long)task_thread_info(idle);
405
406 csr_mail_send(entry, cpu_logical_map(cpu), 0);
407
408 loongson_send_ipi_single(cpu, ACTION_BOOT_CPU);
409 }
410
411 /*
412 * SMP init and finish on secondary CPUs
413 */
loongson_init_secondary(void)414 void loongson_init_secondary(void)
415 {
416 unsigned int cpu = smp_processor_id();
417 unsigned int imask = ECFGF_IP0 | ECFGF_IP1 | ECFGF_IP2 |
418 ECFGF_IPI | ECFGF_PMC | ECFGF_TIMER | ECFGF_SIP0;
419
420 change_csr_ecfg(ECFG0_IM, imask);
421
422 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
423
424 #ifdef CONFIG_NUMA
425 numa_add_cpu(cpu);
426 #endif
427 per_cpu(cpu_state, cpu) = CPU_ONLINE;
428 cpu_data[cpu].package =
429 cpu_logical_map(cpu) / loongson_sysconf.cores_per_package;
430 cpu_data[cpu].core = pptt_enabled ? cpu_data[cpu].core :
431 cpu_logical_map(cpu) % loongson_sysconf.cores_per_package;
432 cpu_data[cpu].global_id = cpu_logical_map(cpu);
433 }
434
loongson_smp_finish(void)435 void loongson_smp_finish(void)
436 {
437 local_irq_enable();
438 iocsr_write64(0, LOONGARCH_IOCSR_MBUF0);
439 pr_info("CPU#%d finished\n", smp_processor_id());
440 }
441
442 #ifdef CONFIG_HOTPLUG_CPU
443
loongson_cpu_disable(void)444 int loongson_cpu_disable(void)
445 {
446 unsigned long flags;
447 unsigned int cpu = smp_processor_id();
448
449 if (io_master(cpu))
450 return -EBUSY;
451
452 #ifdef CONFIG_NUMA
453 numa_remove_cpu(cpu);
454 #endif
455 set_cpu_online(cpu, false);
456 clear_cpu_sibling_map(cpu);
457 clear_cpu_llc_shared_map(cpu);
458 calculate_cpu_foreign_map();
459 local_irq_save(flags);
460 irq_migrate_all_off_this_cpu();
461 clear_csr_ecfg(ECFG0_IM);
462 local_irq_restore(flags);
463 local_flush_tlb_all();
464
465 return 0;
466 }
467
loongson_cpu_die(unsigned int cpu)468 void loongson_cpu_die(unsigned int cpu)
469 {
470 while (per_cpu(cpu_state, cpu) != CPU_DEAD)
471 cpu_relax();
472
473 mb();
474 }
475
idle_play_dead(void)476 static void __noreturn idle_play_dead(void)
477 {
478 register uint64_t addr;
479 register void (*init_fn)(void);
480
481 idle_task_exit();
482 local_irq_enable();
483 set_csr_ecfg(ECFGF_IPI);
484 __this_cpu_write(cpu_state, CPU_DEAD);
485
486 __smp_mb();
487 do {
488 __asm__ __volatile__("idle 0\n\t");
489 addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
490 } while (addr == 0);
491
492 local_irq_disable();
493 init_fn = (void *)TO_CACHE(addr);
494 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
495
496 init_fn();
497 BUG();
498 }
499
500 #ifdef CONFIG_HIBERNATION
poll_play_dead(void)501 static void __noreturn poll_play_dead(void)
502 {
503 register uint64_t addr;
504 register void (*init_fn)(void);
505
506 idle_task_exit();
507 __this_cpu_write(cpu_state, CPU_DEAD);
508
509 __smp_mb();
510 do {
511 __asm__ __volatile__("nop\n\t");
512 addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
513 } while (addr == 0);
514
515 init_fn = (void *)TO_CACHE(addr);
516 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
517
518 init_fn();
519 BUG();
520 }
521 #endif
522
523 static void (*play_dead)(void) = idle_play_dead;
524
arch_cpu_idle_dead(void)525 void __noreturn arch_cpu_idle_dead(void)
526 {
527 play_dead();
528 BUG(); /* play_dead() doesn't return */
529 }
530
531 #ifdef CONFIG_HIBERNATION
hibernate_resume_nonboot_cpu_disable(void)532 int hibernate_resume_nonboot_cpu_disable(void)
533 {
534 int ret;
535
536 play_dead = poll_play_dead;
537 ret = suspend_disable_secondary_cpus();
538 play_dead = idle_play_dead;
539
540 return ret;
541 }
542 #endif
543
544 #endif
545
546 /*
547 * Power management
548 */
549 #ifdef CONFIG_PM
550
loongson_ipi_suspend(void * data)551 static int loongson_ipi_suspend(void *data)
552 {
553 return 0;
554 }
555
loongson_ipi_resume(void * data)556 static void loongson_ipi_resume(void *data)
557 {
558 iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
559 }
560
561 static const struct syscore_ops loongson_ipi_syscore_ops = {
562 .resume = loongson_ipi_resume,
563 .suspend = loongson_ipi_suspend,
564 };
565
566 static struct syscore loongson_ipi_syscore = {
567 .ops = &loongson_ipi_syscore_ops,
568 };
569
570 /*
571 * Enable boot cpu ipi before enabling nonboot cpus
572 * during syscore_resume.
573 */
ipi_pm_init(void)574 static int __init ipi_pm_init(void)
575 {
576 register_syscore(&loongson_ipi_syscore);
577 return 0;
578 }
579
580 core_initcall(ipi_pm_init);
581 #endif
582
583 /* Preload SMP state for boot cpu */
smp_prepare_boot_cpu(void)584 void __init smp_prepare_boot_cpu(void)
585 {
586 unsigned int cpu, node, rr_node;
587
588 set_cpu_possible(0, true);
589 set_cpu_online(0, true);
590 set_my_cpu_offset(per_cpu_offset(0));
591 numa_add_cpu(0);
592
593 rr_node = first_node(node_online_map);
594 for_each_possible_cpu(cpu) {
595 node = early_cpu_to_node(cpu);
596
597 /*
598 * The mapping between present cpus and nodes has been
599 * built during MADT and SRAT parsing.
600 *
601 * If possible cpus = present cpus here, early_cpu_to_node
602 * will return valid node.
603 *
604 * If possible cpus > present cpus here (e.g. some possible
605 * cpus will be added by cpu-hotplug later), for possible but
606 * not present cpus, early_cpu_to_node will return NUMA_NO_NODE,
607 * and we just map them to online nodes in round-robin way.
608 * Once hotplugged, new correct mapping will be built for them.
609 */
610 if (node != NUMA_NO_NODE)
611 set_cpu_numa_node(cpu, node);
612 else {
613 set_cpu_numa_node(cpu, rr_node);
614 rr_node = next_node_in(rr_node, node_online_map);
615 }
616 }
617
618 pv_spinlock_init();
619 }
620
621 /* called from main before smp_init() */
smp_prepare_cpus(unsigned int max_cpus)622 void __init smp_prepare_cpus(unsigned int max_cpus)
623 {
624 init_new_context(current, &init_mm);
625 current_thread_info()->cpu = 0;
626 loongson_prepare_cpus(max_cpus);
627 set_cpu_sibling_map(0);
628 set_cpu_llc_shared_map(0);
629 set_cpu_core_map(0);
630 calculate_cpu_foreign_map();
631 #ifndef CONFIG_HOTPLUG_CPU
632 init_cpu_present(cpu_possible_mask);
633 #endif
634 }
635
__cpu_up(unsigned int cpu,struct task_struct * tidle)636 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
637 {
638 loongson_boot_secondary(cpu, tidle);
639
640 /* Wait for CPU to start and be ready to sync counters */
641 if (!wait_for_completion_timeout(&cpu_starting,
642 msecs_to_jiffies(5000))) {
643 pr_crit("CPU%u: failed to start\n", cpu);
644 return -EIO;
645 }
646
647 /* Wait for CPU to finish startup & mark itself online before return */
648 wait_for_completion(&cpu_running);
649
650 return 0;
651 }
652
653 /*
654 * First C code run on the secondary CPUs after being started up by
655 * the master.
656 */
start_secondary(void)657 asmlinkage void start_secondary(void)
658 {
659 unsigned int cpu;
660
661 sync_counter();
662 cpu = raw_smp_processor_id();
663 set_my_cpu_offset(per_cpu_offset(cpu));
664
665 cpu_probe();
666 constant_clockevent_init();
667 loongson_init_secondary();
668
669 set_cpu_sibling_map(cpu);
670 set_cpu_llc_shared_map(cpu);
671 set_cpu_core_map(cpu);
672
673 notify_cpu_starting(cpu);
674
675 /* Notify boot CPU that we're starting */
676 complete(&cpu_starting);
677
678 /* The CPU is running, now mark it online */
679 set_cpu_online(cpu, true);
680
681 calculate_cpu_foreign_map();
682
683 /*
684 * Notify boot CPU that we're up & online and it can safely return
685 * from __cpu_up()
686 */
687 complete(&cpu_running);
688
689 /*
690 * irq will be enabled in loongson_smp_finish(), enabling it too
691 * early is dangerous.
692 */
693 WARN_ON_ONCE(!irqs_disabled());
694 loongson_smp_finish();
695
696 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
697 }
698
smp_cpus_done(unsigned int max_cpus)699 void __init smp_cpus_done(unsigned int max_cpus)
700 {
701 }
702
stop_this_cpu(void * dummy)703 static void stop_this_cpu(void *dummy)
704 {
705 set_cpu_online(smp_processor_id(), false);
706 calculate_cpu_foreign_map();
707 local_irq_disable();
708 while (true);
709 }
710
smp_send_stop(void)711 void smp_send_stop(void)
712 {
713 smp_call_function(stop_this_cpu, NULL, 0);
714 }
715
716 #ifdef CONFIG_PROFILING
setup_profiling_timer(unsigned int multiplier)717 int setup_profiling_timer(unsigned int multiplier)
718 {
719 return 0;
720 }
721 #endif
722
flush_tlb_all_ipi(void * info)723 static void flush_tlb_all_ipi(void *info)
724 {
725 local_flush_tlb_all();
726 }
727
flush_tlb_all(void)728 void flush_tlb_all(void)
729 {
730 on_each_cpu(flush_tlb_all_ipi, NULL, 1);
731 }
732
flush_tlb_mm_ipi(void * mm)733 static void flush_tlb_mm_ipi(void *mm)
734 {
735 local_flush_tlb_mm((struct mm_struct *)mm);
736 }
737
flush_tlb_mm(struct mm_struct * mm)738 void flush_tlb_mm(struct mm_struct *mm)
739 {
740 if (atomic_read(&mm->mm_users) == 0)
741 return; /* happens as a result of exit_mmap() */
742
743 preempt_disable();
744
745 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
746 on_each_cpu_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm, 1);
747 } else {
748 unsigned int cpu;
749
750 for_each_online_cpu(cpu) {
751 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
752 cpu_context(cpu, mm) = 0;
753 }
754 local_flush_tlb_mm(mm);
755 }
756
757 preempt_enable();
758 }
759
760 struct flush_tlb_data {
761 struct vm_area_struct *vma;
762 unsigned long addr1;
763 unsigned long addr2;
764 };
765
flush_tlb_range_ipi(void * info)766 static void flush_tlb_range_ipi(void *info)
767 {
768 struct flush_tlb_data *fd = info;
769
770 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
771 }
772
flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)773 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
774 {
775 struct mm_struct *mm = vma->vm_mm;
776
777 preempt_disable();
778 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
779 struct flush_tlb_data fd = {
780 .vma = vma,
781 .addr1 = start,
782 .addr2 = end,
783 };
784
785 on_each_cpu_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd, 1);
786 } else {
787 unsigned int cpu;
788
789 for_each_online_cpu(cpu) {
790 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
791 cpu_context(cpu, mm) = 0;
792 }
793 local_flush_tlb_range(vma, start, end);
794 }
795 preempt_enable();
796 }
797
flush_tlb_kernel_range_ipi(void * info)798 static void flush_tlb_kernel_range_ipi(void *info)
799 {
800 struct flush_tlb_data *fd = info;
801
802 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
803 }
804
flush_tlb_kernel_range(unsigned long start,unsigned long end)805 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
806 {
807 struct flush_tlb_data fd = {
808 .addr1 = start,
809 .addr2 = end,
810 };
811
812 on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
813 }
814
flush_tlb_page_ipi(void * info)815 static void flush_tlb_page_ipi(void *info)
816 {
817 struct flush_tlb_data *fd = info;
818
819 local_flush_tlb_page(fd->vma, fd->addr1);
820 }
821
flush_tlb_page(struct vm_area_struct * vma,unsigned long page)822 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
823 {
824 preempt_disable();
825 if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
826 struct flush_tlb_data fd = {
827 .vma = vma,
828 .addr1 = page,
829 };
830
831 on_each_cpu_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd, 1);
832 } else {
833 unsigned int cpu;
834
835 for_each_online_cpu(cpu) {
836 if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
837 cpu_context(cpu, vma->vm_mm) = 0;
838 }
839 local_flush_tlb_page(vma, page);
840 }
841 preempt_enable();
842 }
843 EXPORT_SYMBOL(flush_tlb_page);
844
flush_tlb_one_ipi(void * info)845 static void flush_tlb_one_ipi(void *info)
846 {
847 unsigned long vaddr = (unsigned long) info;
848
849 local_flush_tlb_one(vaddr);
850 }
851
flush_tlb_one(unsigned long vaddr)852 void flush_tlb_one(unsigned long vaddr)
853 {
854 on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
855 }
856 EXPORT_SYMBOL(flush_tlb_one);
857