xref: /openbmc/linux/arch/loongarch/kernel/smp.c (revision 7cc39531)
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/cpu.h>
12 #include <linux/cpumask.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/seq_file.h>
16 #include <linux/smp.h>
17 #include <linux/threads.h>
18 #include <linux/export.h>
19 #include <linux/time.h>
20 #include <linux/tracepoint.h>
21 #include <linux/sched/hotplug.h>
22 #include <linux/sched/task_stack.h>
23 
24 #include <asm/cpu.h>
25 #include <asm/idle.h>
26 #include <asm/loongson.h>
27 #include <asm/mmu_context.h>
28 #include <asm/numa.h>
29 #include <asm/processor.h>
30 #include <asm/setup.h>
31 #include <asm/time.h>
32 
33 int __cpu_number_map[NR_CPUS];   /* Map physical to logical */
34 EXPORT_SYMBOL(__cpu_number_map);
35 
36 int __cpu_logical_map[NR_CPUS];		/* Map logical to physical */
37 EXPORT_SYMBOL(__cpu_logical_map);
38 
39 /* Number of threads (siblings) per CPU core */
40 int smp_num_siblings = 1;
41 EXPORT_SYMBOL(smp_num_siblings);
42 
43 /* Representing the threads (siblings) of each logical CPU */
44 cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
45 EXPORT_SYMBOL(cpu_sibling_map);
46 
47 /* Representing the core map of multi-core chips of each logical CPU */
48 cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
49 EXPORT_SYMBOL(cpu_core_map);
50 
51 static DECLARE_COMPLETION(cpu_starting);
52 static DECLARE_COMPLETION(cpu_running);
53 
54 /*
55  * A logcal cpu mask containing only one VPE per core to
56  * reduce the number of IPIs on large MT systems.
57  */
58 cpumask_t cpu_foreign_map[NR_CPUS] __read_mostly;
59 EXPORT_SYMBOL(cpu_foreign_map);
60 
61 /* representing cpus for which sibling maps can be computed */
62 static cpumask_t cpu_sibling_setup_map;
63 
64 /* representing cpus for which core maps can be computed */
65 static cpumask_t cpu_core_setup_map;
66 
67 struct secondary_data cpuboot_data;
68 static DEFINE_PER_CPU(int, cpu_state);
69 
70 enum ipi_msg_type {
71 	IPI_RESCHEDULE,
72 	IPI_CALL_FUNCTION,
73 };
74 
75 static const char *ipi_types[NR_IPI] __tracepoint_string = {
76 	[IPI_RESCHEDULE] = "Rescheduling interrupts",
77 	[IPI_CALL_FUNCTION] = "Function call interrupts",
78 };
79 
80 void show_ipi_list(struct seq_file *p, int prec)
81 {
82 	unsigned int cpu, i;
83 
84 	for (i = 0; i < NR_IPI; i++) {
85 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i, prec >= 4 ? " " : "");
86 		for_each_online_cpu(cpu)
87 			seq_printf(p, "%10u ", per_cpu(irq_stat, cpu).ipi_irqs[i]);
88 		seq_printf(p, " LoongArch  %d  %s\n", i + 1, ipi_types[i]);
89 	}
90 }
91 
92 /* Send mailbox buffer via Mail_Send */
93 static void csr_mail_send(uint64_t data, int cpu, int mailbox)
94 {
95 	uint64_t val;
96 
97 	/* Send high 32 bits */
98 	val = IOCSR_MBUF_SEND_BLOCKING;
99 	val |= (IOCSR_MBUF_SEND_BOX_HI(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
100 	val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
101 	val |= (data & IOCSR_MBUF_SEND_H32_MASK);
102 	iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
103 
104 	/* Send low 32 bits */
105 	val = IOCSR_MBUF_SEND_BLOCKING;
106 	val |= (IOCSR_MBUF_SEND_BOX_LO(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
107 	val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
108 	val |= (data << IOCSR_MBUF_SEND_BUF_SHIFT);
109 	iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
110 };
111 
112 static u32 ipi_read_clear(int cpu)
113 {
114 	u32 action;
115 
116 	/* Load the ipi register to figure out what we're supposed to do */
117 	action = iocsr_read32(LOONGARCH_IOCSR_IPI_STATUS);
118 	/* Clear the ipi register to clear the interrupt */
119 	iocsr_write32(action, LOONGARCH_IOCSR_IPI_CLEAR);
120 	smp_mb();
121 
122 	return action;
123 }
124 
125 static void ipi_write_action(int cpu, u32 action)
126 {
127 	unsigned int irq = 0;
128 
129 	while ((irq = ffs(action))) {
130 		uint32_t val = IOCSR_IPI_SEND_BLOCKING;
131 
132 		val |= (irq - 1);
133 		val |= (cpu << IOCSR_IPI_SEND_CPU_SHIFT);
134 		iocsr_write32(val, LOONGARCH_IOCSR_IPI_SEND);
135 		action &= ~BIT(irq - 1);
136 	}
137 }
138 
139 void loongson3_send_ipi_single(int cpu, unsigned int action)
140 {
141 	ipi_write_action(cpu_logical_map(cpu), (u32)action);
142 }
143 
144 void loongson3_send_ipi_mask(const struct cpumask *mask, unsigned int action)
145 {
146 	unsigned int i;
147 
148 	for_each_cpu(i, mask)
149 		ipi_write_action(cpu_logical_map(i), (u32)action);
150 }
151 
152 irqreturn_t loongson3_ipi_interrupt(int irq, void *dev)
153 {
154 	unsigned int action;
155 	unsigned int cpu = smp_processor_id();
156 
157 	action = ipi_read_clear(cpu_logical_map(cpu));
158 
159 	if (action & SMP_RESCHEDULE) {
160 		scheduler_ipi();
161 		per_cpu(irq_stat, cpu).ipi_irqs[IPI_RESCHEDULE]++;
162 	}
163 
164 	if (action & SMP_CALL_FUNCTION) {
165 		generic_smp_call_function_interrupt();
166 		per_cpu(irq_stat, cpu).ipi_irqs[IPI_CALL_FUNCTION]++;
167 	}
168 
169 	return IRQ_HANDLED;
170 }
171 
172 void __init loongson3_smp_setup(void)
173 {
174 	cpu_data[0].core = cpu_logical_map(0) % loongson_sysconf.cores_per_package;
175 	cpu_data[0].package = cpu_logical_map(0) / loongson_sysconf.cores_per_package;
176 
177 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
178 	pr_info("Detected %i available CPU(s)\n", loongson_sysconf.nr_cpus);
179 }
180 
181 void __init loongson3_prepare_cpus(unsigned int max_cpus)
182 {
183 	int i = 0;
184 
185 	for (i = 0; i < loongson_sysconf.nr_cpus; i++) {
186 		set_cpu_present(i, true);
187 		csr_mail_send(0, __cpu_logical_map[i], 0);
188 	}
189 
190 	per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
191 }
192 
193 /*
194  * Setup the PC, SP, and TP of a secondary processor and start it running!
195  */
196 void loongson3_boot_secondary(int cpu, struct task_struct *idle)
197 {
198 	unsigned long entry;
199 
200 	pr_info("Booting CPU#%d...\n", cpu);
201 
202 	entry = __pa_symbol((unsigned long)&smpboot_entry);
203 	cpuboot_data.stack = (unsigned long)__KSTK_TOS(idle);
204 	cpuboot_data.thread_info = (unsigned long)task_thread_info(idle);
205 
206 	csr_mail_send(entry, cpu_logical_map(cpu), 0);
207 
208 	loongson3_send_ipi_single(cpu, SMP_BOOT_CPU);
209 }
210 
211 /*
212  * SMP init and finish on secondary CPUs
213  */
214 void loongson3_init_secondary(void)
215 {
216 	unsigned int cpu = smp_processor_id();
217 	unsigned int imask = ECFGF_IP0 | ECFGF_IP1 | ECFGF_IP2 |
218 			     ECFGF_IPI | ECFGF_PMC | ECFGF_TIMER;
219 
220 	change_csr_ecfg(ECFG0_IM, imask);
221 
222 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
223 
224 #ifdef CONFIG_NUMA
225 	numa_add_cpu(cpu);
226 #endif
227 	per_cpu(cpu_state, cpu) = CPU_ONLINE;
228 	cpu_data[cpu].core =
229 		     cpu_logical_map(cpu) % loongson_sysconf.cores_per_package;
230 	cpu_data[cpu].package =
231 		     cpu_logical_map(cpu) / loongson_sysconf.cores_per_package;
232 }
233 
234 void loongson3_smp_finish(void)
235 {
236 	local_irq_enable();
237 	iocsr_write64(0, LOONGARCH_IOCSR_MBUF0);
238 	pr_info("CPU#%d finished\n", smp_processor_id());
239 }
240 
241 #ifdef CONFIG_HOTPLUG_CPU
242 
243 static bool io_master(int cpu)
244 {
245 	return test_bit(cpu, &loongson_sysconf.cores_io_master);
246 }
247 
248 int loongson3_cpu_disable(void)
249 {
250 	unsigned long flags;
251 	unsigned int cpu = smp_processor_id();
252 
253 	if (io_master(cpu))
254 		return -EBUSY;
255 
256 #ifdef CONFIG_NUMA
257 	numa_remove_cpu(cpu);
258 #endif
259 	set_cpu_online(cpu, false);
260 	calculate_cpu_foreign_map();
261 	local_irq_save(flags);
262 	irq_migrate_all_off_this_cpu();
263 	clear_csr_ecfg(ECFG0_IM);
264 	local_irq_restore(flags);
265 	local_flush_tlb_all();
266 
267 	return 0;
268 }
269 
270 void loongson3_cpu_die(unsigned int cpu)
271 {
272 	while (per_cpu(cpu_state, cpu) != CPU_DEAD)
273 		cpu_relax();
274 
275 	mb();
276 }
277 
278 void play_dead(void)
279 {
280 	register uint64_t addr;
281 	register void (*init_fn)(void);
282 
283 	idle_task_exit();
284 	local_irq_enable();
285 	set_csr_ecfg(ECFGF_IPI);
286 	__this_cpu_write(cpu_state, CPU_DEAD);
287 
288 	__smp_mb();
289 	do {
290 		__asm__ __volatile__("idle 0\n\t");
291 		addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
292 	} while (addr == 0);
293 
294 	init_fn = (void *)TO_CACHE(addr);
295 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
296 
297 	init_fn();
298 	unreachable();
299 }
300 
301 #endif
302 
303 /*
304  * Power management
305  */
306 #ifdef CONFIG_PM
307 
308 static int loongson3_ipi_suspend(void)
309 {
310 	return 0;
311 }
312 
313 static void loongson3_ipi_resume(void)
314 {
315 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
316 }
317 
318 static struct syscore_ops loongson3_ipi_syscore_ops = {
319 	.resume         = loongson3_ipi_resume,
320 	.suspend        = loongson3_ipi_suspend,
321 };
322 
323 /*
324  * Enable boot cpu ipi before enabling nonboot cpus
325  * during syscore_resume.
326  */
327 static int __init ipi_pm_init(void)
328 {
329 	register_syscore_ops(&loongson3_ipi_syscore_ops);
330 	return 0;
331 }
332 
333 core_initcall(ipi_pm_init);
334 #endif
335 
336 static inline void set_cpu_sibling_map(int cpu)
337 {
338 	int i;
339 
340 	cpumask_set_cpu(cpu, &cpu_sibling_setup_map);
341 
342 	if (smp_num_siblings <= 1)
343 		cpumask_set_cpu(cpu, &cpu_sibling_map[cpu]);
344 	else {
345 		for_each_cpu(i, &cpu_sibling_setup_map) {
346 			if (cpus_are_siblings(cpu, i)) {
347 				cpumask_set_cpu(i, &cpu_sibling_map[cpu]);
348 				cpumask_set_cpu(cpu, &cpu_sibling_map[i]);
349 			}
350 		}
351 	}
352 }
353 
354 static inline void set_cpu_core_map(int cpu)
355 {
356 	int i;
357 
358 	cpumask_set_cpu(cpu, &cpu_core_setup_map);
359 
360 	for_each_cpu(i, &cpu_core_setup_map) {
361 		if (cpu_data[cpu].package == cpu_data[i].package) {
362 			cpumask_set_cpu(i, &cpu_core_map[cpu]);
363 			cpumask_set_cpu(cpu, &cpu_core_map[i]);
364 		}
365 	}
366 }
367 
368 /*
369  * Calculate a new cpu_foreign_map mask whenever a
370  * new cpu appears or disappears.
371  */
372 void calculate_cpu_foreign_map(void)
373 {
374 	int i, k, core_present;
375 	cpumask_t temp_foreign_map;
376 
377 	/* Re-calculate the mask */
378 	cpumask_clear(&temp_foreign_map);
379 	for_each_online_cpu(i) {
380 		core_present = 0;
381 		for_each_cpu(k, &temp_foreign_map)
382 			if (cpus_are_siblings(i, k))
383 				core_present = 1;
384 		if (!core_present)
385 			cpumask_set_cpu(i, &temp_foreign_map);
386 	}
387 
388 	for_each_online_cpu(i)
389 		cpumask_andnot(&cpu_foreign_map[i],
390 			       &temp_foreign_map, &cpu_sibling_map[i]);
391 }
392 
393 /* Preload SMP state for boot cpu */
394 void smp_prepare_boot_cpu(void)
395 {
396 	unsigned int cpu, node, rr_node;
397 
398 	set_cpu_possible(0, true);
399 	set_cpu_online(0, true);
400 	set_my_cpu_offset(per_cpu_offset(0));
401 
402 	rr_node = first_node(node_online_map);
403 	for_each_possible_cpu(cpu) {
404 		node = early_cpu_to_node(cpu);
405 
406 		/*
407 		 * The mapping between present cpus and nodes has been
408 		 * built during MADT and SRAT parsing.
409 		 *
410 		 * If possible cpus = present cpus here, early_cpu_to_node
411 		 * will return valid node.
412 		 *
413 		 * If possible cpus > present cpus here (e.g. some possible
414 		 * cpus will be added by cpu-hotplug later), for possible but
415 		 * not present cpus, early_cpu_to_node will return NUMA_NO_NODE,
416 		 * and we just map them to online nodes in round-robin way.
417 		 * Once hotplugged, new correct mapping will be built for them.
418 		 */
419 		if (node != NUMA_NO_NODE)
420 			set_cpu_numa_node(cpu, node);
421 		else {
422 			set_cpu_numa_node(cpu, rr_node);
423 			rr_node = next_node_in(rr_node, node_online_map);
424 		}
425 	}
426 }
427 
428 /* called from main before smp_init() */
429 void __init smp_prepare_cpus(unsigned int max_cpus)
430 {
431 	init_new_context(current, &init_mm);
432 	current_thread_info()->cpu = 0;
433 	loongson3_prepare_cpus(max_cpus);
434 	set_cpu_sibling_map(0);
435 	set_cpu_core_map(0);
436 	calculate_cpu_foreign_map();
437 #ifndef CONFIG_HOTPLUG_CPU
438 	init_cpu_present(cpu_possible_mask);
439 #endif
440 }
441 
442 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
443 {
444 	loongson3_boot_secondary(cpu, tidle);
445 
446 	/* Wait for CPU to start and be ready to sync counters */
447 	if (!wait_for_completion_timeout(&cpu_starting,
448 					 msecs_to_jiffies(5000))) {
449 		pr_crit("CPU%u: failed to start\n", cpu);
450 		return -EIO;
451 	}
452 
453 	/* Wait for CPU to finish startup & mark itself online before return */
454 	wait_for_completion(&cpu_running);
455 
456 	return 0;
457 }
458 
459 /*
460  * First C code run on the secondary CPUs after being started up by
461  * the master.
462  */
463 asmlinkage void start_secondary(void)
464 {
465 	unsigned int cpu;
466 
467 	sync_counter();
468 	cpu = smp_processor_id();
469 	set_my_cpu_offset(per_cpu_offset(cpu));
470 
471 	cpu_probe();
472 	constant_clockevent_init();
473 	loongson3_init_secondary();
474 
475 	set_cpu_sibling_map(cpu);
476 	set_cpu_core_map(cpu);
477 
478 	notify_cpu_starting(cpu);
479 
480 	/* Notify boot CPU that we're starting */
481 	complete(&cpu_starting);
482 
483 	/* The CPU is running, now mark it online */
484 	set_cpu_online(cpu, true);
485 
486 	calculate_cpu_foreign_map();
487 
488 	/*
489 	 * Notify boot CPU that we're up & online and it can safely return
490 	 * from __cpu_up()
491 	 */
492 	complete(&cpu_running);
493 
494 	/*
495 	 * irq will be enabled in loongson3_smp_finish(), enabling it too
496 	 * early is dangerous.
497 	 */
498 	WARN_ON_ONCE(!irqs_disabled());
499 	loongson3_smp_finish();
500 
501 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
502 }
503 
504 void __init smp_cpus_done(unsigned int max_cpus)
505 {
506 }
507 
508 static void stop_this_cpu(void *dummy)
509 {
510 	set_cpu_online(smp_processor_id(), false);
511 	calculate_cpu_foreign_map();
512 	local_irq_disable();
513 	while (true);
514 }
515 
516 void smp_send_stop(void)
517 {
518 	smp_call_function(stop_this_cpu, NULL, 0);
519 }
520 
521 int setup_profiling_timer(unsigned int multiplier)
522 {
523 	return 0;
524 }
525 
526 static void flush_tlb_all_ipi(void *info)
527 {
528 	local_flush_tlb_all();
529 }
530 
531 void flush_tlb_all(void)
532 {
533 	on_each_cpu(flush_tlb_all_ipi, NULL, 1);
534 }
535 
536 static void flush_tlb_mm_ipi(void *mm)
537 {
538 	local_flush_tlb_mm((struct mm_struct *)mm);
539 }
540 
541 void flush_tlb_mm(struct mm_struct *mm)
542 {
543 	if (atomic_read(&mm->mm_users) == 0)
544 		return;		/* happens as a result of exit_mmap() */
545 
546 	preempt_disable();
547 
548 	if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
549 		on_each_cpu_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm, 1);
550 	} else {
551 		unsigned int cpu;
552 
553 		for_each_online_cpu(cpu) {
554 			if (cpu != smp_processor_id() && cpu_context(cpu, mm))
555 				cpu_context(cpu, mm) = 0;
556 		}
557 		local_flush_tlb_mm(mm);
558 	}
559 
560 	preempt_enable();
561 }
562 
563 struct flush_tlb_data {
564 	struct vm_area_struct *vma;
565 	unsigned long addr1;
566 	unsigned long addr2;
567 };
568 
569 static void flush_tlb_range_ipi(void *info)
570 {
571 	struct flush_tlb_data *fd = info;
572 
573 	local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
574 }
575 
576 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
577 {
578 	struct mm_struct *mm = vma->vm_mm;
579 
580 	preempt_disable();
581 	if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
582 		struct flush_tlb_data fd = {
583 			.vma = vma,
584 			.addr1 = start,
585 			.addr2 = end,
586 		};
587 
588 		on_each_cpu_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd, 1);
589 	} else {
590 		unsigned int cpu;
591 
592 		for_each_online_cpu(cpu) {
593 			if (cpu != smp_processor_id() && cpu_context(cpu, mm))
594 				cpu_context(cpu, mm) = 0;
595 		}
596 		local_flush_tlb_range(vma, start, end);
597 	}
598 	preempt_enable();
599 }
600 
601 static void flush_tlb_kernel_range_ipi(void *info)
602 {
603 	struct flush_tlb_data *fd = info;
604 
605 	local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
606 }
607 
608 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
609 {
610 	struct flush_tlb_data fd = {
611 		.addr1 = start,
612 		.addr2 = end,
613 	};
614 
615 	on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
616 }
617 
618 static void flush_tlb_page_ipi(void *info)
619 {
620 	struct flush_tlb_data *fd = info;
621 
622 	local_flush_tlb_page(fd->vma, fd->addr1);
623 }
624 
625 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
626 {
627 	preempt_disable();
628 	if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
629 		struct flush_tlb_data fd = {
630 			.vma = vma,
631 			.addr1 = page,
632 		};
633 
634 		on_each_cpu_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd, 1);
635 	} else {
636 		unsigned int cpu;
637 
638 		for_each_online_cpu(cpu) {
639 			if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
640 				cpu_context(cpu, vma->vm_mm) = 0;
641 		}
642 		local_flush_tlb_page(vma, page);
643 	}
644 	preempt_enable();
645 }
646 EXPORT_SYMBOL(flush_tlb_page);
647 
648 static void flush_tlb_one_ipi(void *info)
649 {
650 	unsigned long vaddr = (unsigned long) info;
651 
652 	local_flush_tlb_one(vaddr);
653 }
654 
655 void flush_tlb_one(unsigned long vaddr)
656 {
657 	on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
658 }
659 EXPORT_SYMBOL(flush_tlb_one);
660