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