xref: /linux/arch/loongarch/kernel/smp.c (revision bb3c90fe347a5321e7d176ed5b21367aa28be9ee)
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 
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 
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 
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 
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 
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 
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  */
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 */
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 
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 
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 
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 
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  */
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
260 void arch_irq_work_raise(void)
261 {
262 	mp_ops.send_ipi_single(smp_processor_id(), ACTION_IRQ_WORK);
263 }
264 #endif
265 
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 
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 
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 
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 
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  */
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.task = (unsigned long)idle;
404 	cpuboot_data.stack = (unsigned long)task_pt_regs(idle);
405 	cpuboot_data.offset = per_cpu_offset(cpu);
406 
407 	csr_mail_send(entry, cpu_logical_map(cpu), 0);
408 
409 	loongson_send_ipi_single(cpu, ACTION_BOOT_CPU);
410 }
411 
412 /*
413  * SMP init and finish on secondary CPUs
414  */
415 void loongson_init_secondary(void)
416 {
417 	unsigned int cpu = smp_processor_id();
418 	unsigned int imask = ECFGF_IP0 | ECFGF_IP1 | ECFGF_IP2 |
419 			     ECFGF_IPI | ECFGF_PMC | ECFGF_TIMER | ECFGF_SIP0;
420 
421 	change_csr_ecfg(ECFG0_IM, imask);
422 
423 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
424 
425 #ifdef CONFIG_NUMA
426 	numa_add_cpu(cpu);
427 #endif
428 	per_cpu(cpu_state, cpu) = CPU_ONLINE;
429 	cpu_data[cpu].package =
430 		     cpu_logical_map(cpu) / loongson_sysconf.cores_per_package;
431 	cpu_data[cpu].core = pptt_enabled ? cpu_data[cpu].core :
432 		     cpu_logical_map(cpu) % loongson_sysconf.cores_per_package;
433 	cpu_data[cpu].global_id = cpu_logical_map(cpu);
434 }
435 
436 void loongson_smp_finish(void)
437 {
438 	local_irq_enable();
439 	iocsr_write64(0, LOONGARCH_IOCSR_MBUF0);
440 	pr_info("CPU#%d finished\n", smp_processor_id());
441 }
442 
443 #ifdef CONFIG_HOTPLUG_CPU
444 
445 int loongson_cpu_disable(void)
446 {
447 	unsigned long flags;
448 	unsigned int cpu = smp_processor_id();
449 
450 	if (io_master(cpu))
451 		return -EBUSY;
452 
453 #ifdef CONFIG_NUMA
454 	numa_remove_cpu(cpu);
455 #endif
456 	set_cpu_online(cpu, false);
457 	clear_cpu_sibling_map(cpu);
458 	clear_cpu_llc_shared_map(cpu);
459 	calculate_cpu_foreign_map();
460 	local_irq_save(flags);
461 	irq_migrate_all_off_this_cpu();
462 	clear_csr_ecfg(ECFG0_IM);
463 	local_irq_restore(flags);
464 	local_flush_tlb_all();
465 
466 	return 0;
467 }
468 
469 void loongson_cpu_die(unsigned int cpu)
470 {
471 	while (per_cpu(cpu_state, cpu) != CPU_DEAD)
472 		cpu_relax();
473 
474 	mb();
475 }
476 
477 static void __noreturn idle_play_dead(void)
478 {
479 	register uint64_t addr;
480 	register void (*init_fn)(void);
481 
482 	idle_task_exit();
483 	local_irq_enable();
484 	set_csr_ecfg(ECFGF_IPI);
485 	__this_cpu_write(cpu_state, CPU_DEAD);
486 
487 	__smp_mb();
488 	do {
489 		__asm__ __volatile__("idle 0\n\t");
490 		addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
491 	} while (addr == 0);
492 
493 	local_irq_disable();
494 	init_fn = (void *)TO_CACHE(addr);
495 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
496 
497 	init_fn();
498 	BUG();
499 }
500 
501 #ifdef CONFIG_HIBERNATION
502 static void __noreturn poll_play_dead(void)
503 {
504 	register uint64_t addr;
505 	register void (*init_fn)(void);
506 
507 	idle_task_exit();
508 	__this_cpu_write(cpu_state, CPU_DEAD);
509 
510 	__smp_mb();
511 	do {
512 		__asm__ __volatile__("nop\n\t");
513 		addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
514 	} while (addr == 0);
515 
516 	init_fn = (void *)TO_CACHE(addr);
517 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
518 
519 	init_fn();
520 	BUG();
521 }
522 #endif
523 
524 static void (*play_dead)(void) = idle_play_dead;
525 
526 void __noreturn arch_cpu_idle_dead(void)
527 {
528 	play_dead();
529 	BUG(); /* play_dead() doesn't return */
530 }
531 
532 #ifdef CONFIG_HIBERNATION
533 int hibernate_resume_nonboot_cpu_disable(void)
534 {
535 	int ret;
536 
537 	play_dead = poll_play_dead;
538 	ret = suspend_disable_secondary_cpus();
539 	play_dead = idle_play_dead;
540 
541 	return ret;
542 }
543 #endif
544 
545 #endif
546 
547 /*
548  * Power management
549  */
550 #ifdef CONFIG_PM
551 
552 static int loongson_ipi_suspend(void *data)
553 {
554 	return 0;
555 }
556 
557 static void loongson_ipi_resume(void *data)
558 {
559 	iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
560 }
561 
562 static const struct syscore_ops loongson_ipi_syscore_ops = {
563 	.resume         = loongson_ipi_resume,
564 	.suspend        = loongson_ipi_suspend,
565 };
566 
567 static struct syscore loongson_ipi_syscore = {
568 	.ops = &loongson_ipi_syscore_ops,
569 };
570 
571 /*
572  * Enable boot cpu ipi before enabling nonboot cpus
573  * during syscore_resume.
574  */
575 static int __init ipi_pm_init(void)
576 {
577 	register_syscore(&loongson_ipi_syscore);
578 	return 0;
579 }
580 
581 core_initcall(ipi_pm_init);
582 #endif
583 
584 /* Preload SMP state for boot cpu */
585 void __init smp_prepare_boot_cpu(void)
586 {
587 	unsigned int cpu, node, rr_node;
588 
589 	set_cpu_possible(0, true);
590 	set_cpu_online(0, true);
591 	set_my_cpu_offset(per_cpu_offset(0));
592 	numa_add_cpu(0);
593 
594 	rr_node = first_node(node_online_map);
595 	for_each_possible_cpu(cpu) {
596 		node = early_cpu_to_node(cpu);
597 
598 		/*
599 		 * The mapping between present cpus and nodes has been
600 		 * built during MADT and SRAT parsing.
601 		 *
602 		 * If possible cpus = present cpus here, early_cpu_to_node
603 		 * will return valid node.
604 		 *
605 		 * If possible cpus > present cpus here (e.g. some possible
606 		 * cpus will be added by cpu-hotplug later), for possible but
607 		 * not present cpus, early_cpu_to_node will return NUMA_NO_NODE,
608 		 * and we just map them to online nodes in round-robin way.
609 		 * Once hotplugged, new correct mapping will be built for them.
610 		 */
611 		if (node != NUMA_NO_NODE)
612 			set_cpu_numa_node(cpu, node);
613 		else {
614 			set_cpu_numa_node(cpu, rr_node);
615 			rr_node = next_node_in(rr_node, node_online_map);
616 		}
617 	}
618 
619 	pv_spinlock_init();
620 }
621 
622 /* called from main before smp_init() */
623 void __init smp_prepare_cpus(unsigned int max_cpus)
624 {
625 	init_new_context(current, &init_mm);
626 	current_thread_info()->cpu = 0;
627 	loongson_prepare_cpus(max_cpus);
628 	set_cpu_sibling_map(0);
629 	set_cpu_llc_shared_map(0);
630 	set_cpu_core_map(0);
631 	calculate_cpu_foreign_map();
632 #ifndef CONFIG_HOTPLUG_CPU
633 	init_cpu_present(cpu_possible_mask);
634 #endif
635 }
636 
637 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
638 {
639 	loongson_boot_secondary(cpu, tidle);
640 
641 	/* Wait for CPU to start and be ready to sync counters */
642 	if (!wait_for_completion_timeout(&cpu_starting,
643 					 msecs_to_jiffies(5000))) {
644 		pr_crit("CPU%u: failed to start\n", cpu);
645 		return -EIO;
646 	}
647 
648 	/* Wait for CPU to finish startup & mark itself online before return */
649 	wait_for_completion(&cpu_running);
650 
651 	return 0;
652 }
653 
654 /*
655  * First C code run on the secondary CPUs after being started up by
656  * the master.
657  */
658 asmlinkage void start_secondary(void)
659 {
660 	unsigned int cpu;
661 
662 	sync_counter();
663 	cpu = raw_smp_processor_id();
664 	set_my_cpu_offset(per_cpu_offset(cpu));
665 
666 	cpu_probe();
667 	set_current(current);
668 	constant_clockevent_init();
669 	loongson_init_secondary();
670 
671 	set_cpu_sibling_map(cpu);
672 	set_cpu_llc_shared_map(cpu);
673 	set_cpu_core_map(cpu);
674 
675 	notify_cpu_starting(cpu);
676 
677 	/* Notify boot CPU that we're starting */
678 	complete(&cpu_starting);
679 
680 	/* The CPU is running, now mark it online */
681 	set_cpu_online(cpu, true);
682 
683 	calculate_cpu_foreign_map();
684 
685 	/*
686 	 * Notify boot CPU that we're up & online and it can safely return
687 	 * from __cpu_up()
688 	 */
689 	complete(&cpu_running);
690 
691 	/*
692 	 * irq will be enabled in loongson_smp_finish(), enabling it too
693 	 * early is dangerous.
694 	 */
695 	WARN_ON_ONCE(!irqs_disabled());
696 	loongson_smp_finish();
697 
698 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
699 }
700 
701 void __init smp_cpus_done(unsigned int max_cpus)
702 {
703 }
704 
705 static void stop_this_cpu(void *dummy)
706 {
707 	set_cpu_online(smp_processor_id(), false);
708 	calculate_cpu_foreign_map();
709 	local_irq_disable();
710 	rcutree_report_cpu_dead();
711 	while (true);
712 }
713 
714 void smp_send_stop(void)
715 {
716 	smp_call_function(stop_this_cpu, NULL, 0);
717 }
718 
719 #ifdef CONFIG_PROFILING
720 int setup_profiling_timer(unsigned int multiplier)
721 {
722 	return 0;
723 }
724 #endif
725 
726 static void flush_tlb_all_ipi(void *info)
727 {
728 	local_flush_tlb_all();
729 }
730 
731 void flush_tlb_all(void)
732 {
733 	on_each_cpu(flush_tlb_all_ipi, NULL, 1);
734 }
735 
736 static void flush_tlb_mm_ipi(void *mm)
737 {
738 	local_flush_tlb_mm((struct mm_struct *)mm);
739 }
740 
741 void flush_tlb_mm(struct mm_struct *mm)
742 {
743 	if (atomic_read(&mm->mm_users) == 0)
744 		return;		/* happens as a result of exit_mmap() */
745 
746 	preempt_disable();
747 
748 	if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
749 		on_each_cpu_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm, 1);
750 	} else {
751 		unsigned int cpu;
752 
753 		for_each_online_cpu(cpu) {
754 			if (cpu != smp_processor_id() && cpu_context(cpu, mm))
755 				cpu_context(cpu, mm) = 0;
756 		}
757 		local_flush_tlb_mm(mm);
758 	}
759 
760 	preempt_enable();
761 }
762 
763 struct flush_tlb_data {
764 	struct vm_area_struct *vma;
765 	unsigned long addr1;
766 	unsigned long addr2;
767 };
768 
769 static void flush_tlb_range_ipi(void *info)
770 {
771 	struct flush_tlb_data *fd = info;
772 
773 	local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
774 }
775 
776 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
777 {
778 	struct mm_struct *mm = vma->vm_mm;
779 
780 	preempt_disable();
781 	if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
782 		struct flush_tlb_data fd = {
783 			.vma = vma,
784 			.addr1 = start,
785 			.addr2 = end,
786 		};
787 
788 		on_each_cpu_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd, 1);
789 	} else {
790 		unsigned int cpu;
791 
792 		for_each_online_cpu(cpu) {
793 			if (cpu != smp_processor_id() && cpu_context(cpu, mm))
794 				cpu_context(cpu, mm) = 0;
795 		}
796 		local_flush_tlb_range(vma, start, end);
797 	}
798 	preempt_enable();
799 }
800 
801 static void flush_tlb_kernel_range_ipi(void *info)
802 {
803 	struct flush_tlb_data *fd = info;
804 
805 	local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
806 }
807 
808 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
809 {
810 	struct flush_tlb_data fd = {
811 		.addr1 = start,
812 		.addr2 = end,
813 	};
814 
815 	on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
816 }
817 
818 static void flush_tlb_page_ipi(void *info)
819 {
820 	struct flush_tlb_data *fd = info;
821 
822 	local_flush_tlb_page(fd->vma, fd->addr1);
823 }
824 
825 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
826 {
827 	preempt_disable();
828 	if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
829 		struct flush_tlb_data fd = {
830 			.vma = vma,
831 			.addr1 = page,
832 		};
833 
834 		on_each_cpu_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd, 1);
835 	} else {
836 		unsigned int cpu;
837 
838 		for_each_online_cpu(cpu) {
839 			if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
840 				cpu_context(cpu, vma->vm_mm) = 0;
841 		}
842 		local_flush_tlb_page(vma, page);
843 	}
844 	preempt_enable();
845 }
846 EXPORT_SYMBOL(flush_tlb_page);
847 
848 static void flush_tlb_one_ipi(void *info)
849 {
850 	unsigned long vaddr = (unsigned long) info;
851 
852 	local_flush_tlb_one(vaddr);
853 }
854 
855 void flush_tlb_one(unsigned long vaddr)
856 {
857 	on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
858 }
859 EXPORT_SYMBOL(flush_tlb_one);
860