blob: 03daf900812453450fe835417cfacd175c989505 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2000, 2001 Kanoj Sarcar
17 * Copyright (C) 2000, 2001 Ralf Baechle
18 * Copyright (C) 2000, 2001 Silicon Graphics, Inc.
19 * Copyright (C) 2000, 2001, 2003 Broadcom Corporation
20 */
21#include <linux/cache.h>
22#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010025#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/spinlock.h>
27#include <linux/threads.h>
Paul Gortmakerd9d54172016-08-21 15:58:13 -040028#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/time.h>
30#include <linux/timex.h>
31#include <linux/sched.h>
32#include <linux/cpumask.h>
Rojhalat Ibrahim1e35aab2006-02-20 13:35:27 +000033#include <linux/cpu.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040034#include <linux/err.h>
Wu Zhangjin8f99a162009-11-20 20:34:33 +080035#include <linux/ftrace.h>
Qais Youseffbde2d72015-12-08 13:20:27 +000036#include <linux/irqdomain.h>
37#include <linux/of.h>
38#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Arun Sharma600634972011-07-26 16:09:06 -070040#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/cpu.h>
42#include <asm/processor.h>
Ralf Baechlebdc92d742013-05-21 16:59:19 +020043#include <asm/idle.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010044#include <asm/r4k-timer.h>
Qais Youseffbde2d72015-12-08 13:20:27 +000045#include <asm/mips-cpc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/mmu_context.h>
Ralf Baechle7bcf7712007-10-11 23:46:09 +010047#include <asm/time.h>
David Howellsb81947c2012-03-28 18:30:02 +010048#include <asm/setup.h>
Paul Burtone060f6e2015-09-25 08:59:38 -070049#include <asm/maar.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Ralf Baechlecafb45b2015-05-12 06:43:04 +020051cpumask_t cpu_callin_map; /* Bitmask of started secondaries */
David Daney2dc2ae32010-07-23 18:41:45 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
David Daney2dc2ae32010-07-23 18:41:45 -070054EXPORT_SYMBOL(__cpu_number_map);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
David Daney2dc2ae32010-07-23 18:41:45 -070057EXPORT_SYMBOL(__cpu_logical_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ralf Baechle0ab7aef2007-03-02 20:42:04 +000059/* Number of TCs (or siblings in Intel speak) per CPU core */
60int smp_num_siblings = 1;
61EXPORT_SYMBOL(smp_num_siblings);
62
63/* representing the TCs (or siblings in Intel speak) of each logical CPU */
64cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
65EXPORT_SYMBOL(cpu_sibling_map);
66
Huacai Chenbda45842014-06-26 11:41:26 +080067/* representing the core map of multi-core chips of each logical CPU */
68cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
69EXPORT_SYMBOL(cpu_core_map);
70
Matt Redfearna00eeed2016-11-04 09:28:56 +000071static DECLARE_COMPLETION(cpu_running);
72
Markos Chandrascccf34e2015-07-10 09:29:10 +010073/*
74 * A logcal cpu mask containing only one VPE per core to
75 * reduce the number of IPIs on large MT systems.
76 */
James Hogan640511a2016-07-13 14:12:52 +010077cpumask_t cpu_foreign_map[NR_CPUS] __read_mostly;
Markos Chandrascccf34e2015-07-10 09:29:10 +010078EXPORT_SYMBOL(cpu_foreign_map);
79
Ralf Baechle0ab7aef2007-03-02 20:42:04 +000080/* representing cpus for which sibling maps can be computed */
81static cpumask_t cpu_sibling_setup_map;
82
Huacai Chenbda45842014-06-26 11:41:26 +080083/* representing cpus for which core maps can be computed */
84static cpumask_t cpu_core_setup_map;
85
Paul Burton76306f42014-02-14 16:30:52 +000086cpumask_t cpu_coherent_mask;
87
Qais Youseffbde2d72015-12-08 13:20:27 +000088#ifdef CONFIG_GENERIC_IRQ_IPI
89static struct irq_desc *call_desc;
90static struct irq_desc *sched_desc;
91#endif
92
Ralf Baechle0ab7aef2007-03-02 20:42:04 +000093static inline void set_cpu_sibling_map(int cpu)
94{
95 int i;
96
Rusty Russell8dd92892015-03-05 10:49:17 +103097 cpumask_set_cpu(cpu, &cpu_sibling_setup_map);
Ralf Baechle0ab7aef2007-03-02 20:42:04 +000098
99 if (smp_num_siblings > 1) {
Rusty Russell8dd92892015-03-05 10:49:17 +1030100 for_each_cpu(i, &cpu_sibling_setup_map) {
Huacai Chenbda45842014-06-26 11:41:26 +0800101 if (cpu_data[cpu].package == cpu_data[i].package &&
102 cpu_data[cpu].core == cpu_data[i].core) {
Rusty Russell8dd92892015-03-05 10:49:17 +1030103 cpumask_set_cpu(i, &cpu_sibling_map[cpu]);
104 cpumask_set_cpu(cpu, &cpu_sibling_map[i]);
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000105 }
106 }
107 } else
Rusty Russell8dd92892015-03-05 10:49:17 +1030108 cpumask_set_cpu(cpu, &cpu_sibling_map[cpu]);
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000109}
110
Huacai Chenbda45842014-06-26 11:41:26 +0800111static inline void set_cpu_core_map(int cpu)
112{
113 int i;
114
Rusty Russell8dd92892015-03-05 10:49:17 +1030115 cpumask_set_cpu(cpu, &cpu_core_setup_map);
Huacai Chenbda45842014-06-26 11:41:26 +0800116
Rusty Russell8dd92892015-03-05 10:49:17 +1030117 for_each_cpu(i, &cpu_core_setup_map) {
Huacai Chenbda45842014-06-26 11:41:26 +0800118 if (cpu_data[cpu].package == cpu_data[i].package) {
Rusty Russell8dd92892015-03-05 10:49:17 +1030119 cpumask_set_cpu(i, &cpu_core_map[cpu]);
120 cpumask_set_cpu(cpu, &cpu_core_map[i]);
Huacai Chenbda45842014-06-26 11:41:26 +0800121 }
122 }
123}
124
Markos Chandrascccf34e2015-07-10 09:29:10 +0100125/*
126 * Calculate a new cpu_foreign_map mask whenever a
127 * new cpu appears or disappears.
128 */
James Hogan826e99b2016-07-13 14:12:45 +0100129void calculate_cpu_foreign_map(void)
Markos Chandrascccf34e2015-07-10 09:29:10 +0100130{
131 int i, k, core_present;
132 cpumask_t temp_foreign_map;
133
134 /* Re-calculate the mask */
James Hogand825c062016-03-04 10:10:51 +0000135 cpumask_clear(&temp_foreign_map);
Markos Chandrascccf34e2015-07-10 09:29:10 +0100136 for_each_online_cpu(i) {
137 core_present = 0;
138 for_each_cpu(k, &temp_foreign_map)
139 if (cpu_data[i].package == cpu_data[k].package &&
140 cpu_data[i].core == cpu_data[k].core)
141 core_present = 1;
142 if (!core_present)
143 cpumask_set_cpu(i, &temp_foreign_map);
144 }
145
James Hogan640511a2016-07-13 14:12:52 +0100146 for_each_online_cpu(i)
147 cpumask_andnot(&cpu_foreign_map[i],
148 &temp_foreign_map, &cpu_sibling_map[i]);
Markos Chandrascccf34e2015-07-10 09:29:10 +0100149}
150
Ralf Baechle87353d82007-11-19 12:23:51 +0000151struct plat_smp_ops *mp_ops;
Sanjay Lal82d45de2012-11-21 18:34:14 -0800152EXPORT_SYMBOL(mp_ops);
Ralf Baechle87353d82007-11-19 12:23:51 +0000153
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000154void register_smp_ops(struct plat_smp_ops *ops)
Ralf Baechle87353d82007-11-19 12:23:51 +0000155{
Thiemo Seufer83738e32008-05-06 11:21:22 +0100156 if (mp_ops)
157 printk(KERN_WARNING "Overriding previously set SMP ops\n");
Ralf Baechle87353d82007-11-19 12:23:51 +0000158
159 mp_ops = ops;
160}
161
Qais Youseffbde2d72015-12-08 13:20:27 +0000162#ifdef CONFIG_GENERIC_IRQ_IPI
163void mips_smp_send_ipi_single(int cpu, unsigned int action)
164{
165 mips_smp_send_ipi_mask(cpumask_of(cpu), action);
166}
167
168void mips_smp_send_ipi_mask(const struct cpumask *mask, unsigned int action)
169{
170 unsigned long flags;
171 unsigned int core;
172 int cpu;
173
174 local_irq_save(flags);
175
176 switch (action) {
177 case SMP_CALL_FUNCTION:
178 __ipi_send_mask(call_desc, mask);
179 break;
180
181 case SMP_RESCHEDULE_YOURSELF:
182 __ipi_send_mask(sched_desc, mask);
183 break;
184
185 default:
186 BUG();
187 }
188
189 if (mips_cpc_present()) {
190 for_each_cpu(cpu, mask) {
191 core = cpu_data[cpu].core;
192
193 if (core == current_cpu_data.core)
194 continue;
195
196 while (!cpumask_test_cpu(cpu, &cpu_coherent_mask)) {
Matt Redfearn4b640132016-09-07 10:45:19 +0100197 mips_cm_lock_other(core, 0);
Qais Youseffbde2d72015-12-08 13:20:27 +0000198 mips_cpc_lock_other(core);
199 write_cpc_co_cmd(CPC_Cx_CMD_PWRUP);
200 mips_cpc_unlock_other();
Matt Redfearn4b640132016-09-07 10:45:19 +0100201 mips_cm_unlock_other();
Qais Youseffbde2d72015-12-08 13:20:27 +0000202 }
203 }
204 }
205
206 local_irq_restore(flags);
207}
208
209
210static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
211{
212 scheduler_ipi();
213
214 return IRQ_HANDLED;
215}
216
217static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
218{
219 generic_smp_call_function_interrupt();
220
221 return IRQ_HANDLED;
222}
223
224static struct irqaction irq_resched = {
225 .handler = ipi_resched_interrupt,
226 .flags = IRQF_PERCPU,
227 .name = "IPI resched"
228};
229
230static struct irqaction irq_call = {
231 .handler = ipi_call_interrupt,
232 .flags = IRQF_PERCPU,
233 .name = "IPI call"
234};
235
Matt Redfearn7688c532016-09-20 09:47:26 +0100236static void smp_ipi_init_one(unsigned int virq,
Qais Youseffbde2d72015-12-08 13:20:27 +0000237 struct irqaction *action)
238{
239 int ret;
240
241 irq_set_handler(virq, handle_percpu_irq);
242 ret = setup_irq(virq, action);
243 BUG_ON(ret);
244}
245
Matt Redfearn7688c532016-09-20 09:47:26 +0100246static unsigned int call_virq, sched_virq;
247
248int mips_smp_ipi_allocate(const struct cpumask *mask)
Qais Youseffbde2d72015-12-08 13:20:27 +0000249{
Matt Redfearn7688c532016-09-20 09:47:26 +0100250 int virq;
Qais Youseffbde2d72015-12-08 13:20:27 +0000251 struct irq_domain *ipidomain;
252 struct device_node *node;
253
254 node = of_irq_find_parent(of_root);
255 ipidomain = irq_find_matching_host(node, DOMAIN_BUS_IPI);
256
257 /*
258 * Some platforms have half DT setup. So if we found irq node but
259 * didn't find an ipidomain, try to search for one that is not in the
260 * DT.
261 */
262 if (node && !ipidomain)
263 ipidomain = irq_find_matching_host(NULL, DOMAIN_BUS_IPI);
264
Paul Burton578bffc2016-04-04 10:04:52 +0100265 /*
266 * There are systems which only use IPI domains some of the time,
267 * depending upon configuration we don't know until runtime. An
268 * example is Malta where we may compile in support for GIC & the
269 * MT ASE, but run on a system which has multiple VPEs in a single
270 * core and doesn't include a GIC. Until all IPI implementations
271 * have been converted to use IPI domains the best we can do here
272 * is to return & hope some other code sets up the IPIs.
273 */
274 if (!ipidomain)
275 return 0;
Qais Youseffbde2d72015-12-08 13:20:27 +0000276
Matt Redfearn7688c532016-09-20 09:47:26 +0100277 virq = irq_reserve_ipi(ipidomain, mask);
278 BUG_ON(!virq);
279 if (!call_virq)
280 call_virq = virq;
Qais Youseffbde2d72015-12-08 13:20:27 +0000281
Matt Redfearn7688c532016-09-20 09:47:26 +0100282 virq = irq_reserve_ipi(ipidomain, mask);
283 BUG_ON(!virq);
284 if (!sched_virq)
285 sched_virq = virq;
Qais Youseffbde2d72015-12-08 13:20:27 +0000286
287 if (irq_domain_is_ipi_per_cpu(ipidomain)) {
288 int cpu;
289
Matt Redfearn7688c532016-09-20 09:47:26 +0100290 for_each_cpu(cpu, mask) {
Qais Youseffbde2d72015-12-08 13:20:27 +0000291 smp_ipi_init_one(call_virq + cpu, &irq_call);
292 smp_ipi_init_one(sched_virq + cpu, &irq_resched);
293 }
294 } else {
295 smp_ipi_init_one(call_virq, &irq_call);
296 smp_ipi_init_one(sched_virq, &irq_resched);
297 }
298
Matt Redfearn7688c532016-09-20 09:47:26 +0100299 return 0;
300}
301
302int mips_smp_ipi_free(const struct cpumask *mask)
303{
304 struct irq_domain *ipidomain;
305 struct device_node *node;
306
307 node = of_irq_find_parent(of_root);
308 ipidomain = irq_find_matching_host(node, DOMAIN_BUS_IPI);
309
310 /*
311 * Some platforms have half DT setup. So if we found irq node but
312 * didn't find an ipidomain, try to search for one that is not in the
313 * DT.
314 */
315 if (node && !ipidomain)
316 ipidomain = irq_find_matching_host(NULL, DOMAIN_BUS_IPI);
317
318 BUG_ON(!ipidomain);
319
320 if (irq_domain_is_ipi_per_cpu(ipidomain)) {
321 int cpu;
322
323 for_each_cpu(cpu, mask) {
324 remove_irq(call_virq + cpu, &irq_call);
325 remove_irq(sched_virq + cpu, &irq_resched);
326 }
327 }
328 irq_destroy_ipi(call_virq, mask);
329 irq_destroy_ipi(sched_virq, mask);
330 return 0;
331}
332
333
334static int __init mips_smp_ipi_init(void)
335{
336 mips_smp_ipi_allocate(cpu_possible_mask);
337
Qais Youseffbde2d72015-12-08 13:20:27 +0000338 call_desc = irq_to_desc(call_virq);
339 sched_desc = irq_to_desc(sched_virq);
340
341 return 0;
342}
343early_initcall(mips_smp_ipi_init);
344#endif
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * First C code run on the secondary CPUs after being started up by
348 * the master.
349 */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000350asmlinkage void start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800352 unsigned int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 cpu_probe();
David Daney6650df32012-05-15 00:04:50 -0700355 per_cpu_trap_init(false);
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100356 mips_clockevent_init();
Ralf Baechle87353d82007-11-19 12:23:51 +0000357 mp_ops->init_secondary();
Hemmo Nieminenc7754e72015-01-15 23:01:59 +0200358 cpu_report();
Paul Burtone060f6e2015-09-25 08:59:38 -0700359 maar_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /*
362 * XXX parity protection should be folded in here when it's converted
363 * to an option instead of something based on .cputype
364 */
365
366 calibrate_delay();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800367 preempt_disable();
368 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 cpu_data[cpu].udelay_val = loops_per_jiffy;
370
Rusty Russell8dd92892015-03-05 10:49:17 +1030371 cpumask_set_cpu(cpu, &cpu_coherent_mask);
Manfred Spraule545a612008-09-07 16:57:22 +0200372 notify_cpu_starting(cpu);
373
Matt Redfearna00eeed2016-11-04 09:28:56 +0000374 complete(&cpu_running);
Matt Redfearn8f46cca2016-09-22 17:15:47 +0100375 synchronise_count_slave(cpu);
376
Yong Zhangb9a09a02012-07-19 09:13:53 +0200377 set_cpu_online(cpu, true);
378
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000379 set_cpu_sibling_map(cpu);
Huacai Chenbda45842014-06-26 11:41:26 +0800380 set_cpu_core_map(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Markos Chandrascccf34e2015-07-10 09:29:10 +0100382 calculate_cpu_foreign_map();
383
Yong Zhangb789ad62012-07-19 09:13:53 +0200384 /*
385 * irq will be enabled in ->smp_finish(), enabling it too early
386 * is dangerous.
387 */
388 WARN_ON_ONCE(!irqs_disabled());
Yong Zhang5309bda2012-07-19 09:13:53 +0200389 mp_ops->smp_finish();
390
Thomas Gleixnerfc6d73d2016-02-26 18:43:40 +0000391 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static void stop_this_cpu(void *dummy)
395{
396 /*
James Hogan92696312016-07-13 14:12:46 +0100397 * Remove this CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Markos Chandrascccf34e2015-07-10 09:29:10 +0100399
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030400 set_cpu_online(smp_processor_id(), false);
Markos Chandrascccf34e2015-07-10 09:29:10 +0100401 calculate_cpu_foreign_map();
Andrew Brestickerea925a722015-03-25 10:25:43 -0700402 local_irq_disable();
403 while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406void smp_send_stop(void)
407{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200408 smp_call_function(stop_this_cpu, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411void __init smp_cpus_done(unsigned int max_cpus)
412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415/* called from main before smp_init() */
416void __init smp_prepare_cpus(unsigned int max_cpus)
417{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 init_new_context(current, &init_mm);
419 current_thread_info()->cpu = 0;
Ralf Baechle87353d82007-11-19 12:23:51 +0000420 mp_ops->prepare_cpus(max_cpus);
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000421 set_cpu_sibling_map(0);
Huacai Chenbda45842014-06-26 11:41:26 +0800422 set_cpu_core_map(0);
Markos Chandrascccf34e2015-07-10 09:29:10 +0100423 calculate_cpu_foreign_map();
Ralf Baechle320e6ab2006-05-22 14:24:04 +0100424#ifndef CONFIG_HOTPLUG_CPU
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030425 init_cpu_present(cpu_possible_mask);
Ralf Baechle320e6ab2006-05-22 14:24:04 +0100426#endif
Paul Burton76306f42014-02-14 16:30:52 +0000427 cpumask_copy(&cpu_coherent_mask, cpu_possible_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/* preload SMP state for boot cpu */
Greg Kroah-Hartman28eb0e42012-12-21 14:04:39 -0800431void smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Rusty Russell4037ac62009-09-24 09:34:47 -0600433 set_cpu_possible(0, true);
434 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000437int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Thomas Gleixner360014a2012-04-20 13:05:51 +0000439 mp_ops->boot_secondary(cpu, tidle);
Ralf Baechleb727a602005-02-22 21:18:01 +0000440
441 /*
Matt Redfearna00eeed2016-11-04 09:28:56 +0000442 * We must check for timeout here, as the CPU will not be marked
443 * online until the counters are synchronised.
Ralf Baechleb727a602005-02-22 21:18:01 +0000444 */
Matt Redfearna00eeed2016-11-04 09:28:56 +0000445 if (!wait_for_completion_timeout(&cpu_running,
446 msecs_to_jiffies(1000))) {
447 pr_crit("CPU%u: failed to start\n", cpu);
448 return -EIO;
Ralf Baechlecafb45b2015-05-12 06:43:04 +0200449 }
Ralf Baechleb727a602005-02-22 21:18:01 +0000450
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530451 synchronise_count_master(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453}
454
455/* Not really SMP stuff ... */
456int setup_profiling_timer(unsigned int multiplier)
457{
458 return 0;
459}
460
461static void flush_tlb_all_ipi(void *info)
462{
463 local_flush_tlb_all();
464}
465
466void flush_tlb_all(void)
467{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200468 on_each_cpu(flush_tlb_all_ipi, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471static void flush_tlb_mm_ipi(void *mm)
472{
473 local_flush_tlb_mm((struct mm_struct *)mm);
474}
475
476/*
Ralf Baechle25969352006-06-22 22:42:32 +0100477 * Special Variant of smp_call_function for use by TLB functions:
478 *
479 * o No return value
480 * o collapses to normal function call on UP kernels
481 * o collapses to normal function call on systems with a single shared
482 * primary cache.
Ralf Baechle25969352006-06-22 22:42:32 +0100483 */
484static inline void smp_on_other_tlbs(void (*func) (void *info), void *info)
485{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200486 smp_call_function(func, info, 1);
Ralf Baechle25969352006-06-22 22:42:32 +0100487}
488
489static inline void smp_on_each_tlb(void (*func) (void *info), void *info)
490{
491 preempt_disable();
492
493 smp_on_other_tlbs(func, info);
494 func(info);
495
496 preempt_enable();
497}
498
499/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * The following tlb flush calls are invoked when old translations are
501 * being torn down, or pte attributes are changing. For single threaded
502 * address spaces, a new context is obtained on the current cpu, and tlb
503 * context on other cpus are invalidated to force a new context allocation
504 * at switch_mm time, should the mm ever be used on other cpus. For
505 * multithreaded address spaces, intercpu interrupts have to be sent.
506 * Another case where intercpu interrupts are required is when the target
507 * mm might be active on another cpu (eg debuggers doing the flushes on
508 * behalf of debugees, kswapd stealing pages from another process etc).
509 * Kanoj 07/00.
510 */
511
512void flush_tlb_mm(struct mm_struct *mm)
513{
514 preempt_disable();
515
516 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Ralf Baechlec50cade2007-10-04 16:57:08 +0100517 smp_on_other_tlbs(flush_tlb_mm_ipi, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100519 unsigned int cpu;
520
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030521 for_each_online_cpu(cpu) {
522 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100523 cpu_context(cpu, mm) = 0;
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 local_flush_tlb_mm(mm);
527
528 preempt_enable();
529}
530
531struct flush_tlb_data {
532 struct vm_area_struct *vma;
533 unsigned long addr1;
534 unsigned long addr2;
535};
536
537static void flush_tlb_range_ipi(void *info)
538{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100539 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
542}
543
544void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
545{
546 struct mm_struct *mm = vma->vm_mm;
547
548 preempt_disable();
549 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100550 struct flush_tlb_data fd = {
551 .vma = vma,
552 .addr1 = start,
553 .addr2 = end,
554 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Ralf Baechlec50cade2007-10-04 16:57:08 +0100556 smp_on_other_tlbs(flush_tlb_range_ipi, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100558 unsigned int cpu;
James Hogana05c3922016-07-13 14:12:44 +0100559 int exec = vma->vm_flags & VM_EXEC;
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100560
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030561 for_each_online_cpu(cpu) {
James Hogana05c3922016-07-13 14:12:44 +0100562 /*
563 * flush_cache_range() will only fully flush icache if
564 * the VMA is executable, otherwise we must invalidate
565 * ASID without it appearing to has_valid_asid() as if
566 * mm has been completely unused by that CPU.
567 */
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030568 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
James Hogana05c3922016-07-13 14:12:44 +0100569 cpu_context(cpu, mm) = !exec;
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 local_flush_tlb_range(vma, start, end);
573 preempt_enable();
574}
575
576static void flush_tlb_kernel_range_ipi(void *info)
577{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100578 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
581}
582
583void flush_tlb_kernel_range(unsigned long start, unsigned long end)
584{
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100585 struct flush_tlb_data fd = {
586 .addr1 = start,
587 .addr2 = end,
588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200590 on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void flush_tlb_page_ipi(void *info)
594{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100595 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 local_flush_tlb_page(fd->vma, fd->addr1);
598}
599
600void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
601{
602 preempt_disable();
603 if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100604 struct flush_tlb_data fd = {
605 .vma = vma,
606 .addr1 = page,
607 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Ralf Baechlec50cade2007-10-04 16:57:08 +0100609 smp_on_other_tlbs(flush_tlb_page_ipi, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100611 unsigned int cpu;
612
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030613 for_each_online_cpu(cpu) {
James Hogana05c3922016-07-13 14:12:44 +0100614 /*
615 * flush_cache_page() only does partial flushes, so
616 * invalidate ASID without it appearing to
617 * has_valid_asid() as if mm has been completely unused
618 * by that CPU.
619 */
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030620 if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
James Hogana05c3922016-07-13 14:12:44 +0100621 cpu_context(cpu, vma->vm_mm) = 1;
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 local_flush_tlb_page(vma, page);
625 preempt_enable();
626}
627
628static void flush_tlb_one_ipi(void *info)
629{
630 unsigned long vaddr = (unsigned long) info;
631
632 local_flush_tlb_one(vaddr);
633}
634
635void flush_tlb_one(unsigned long vaddr)
636{
Ralf Baechle25969352006-06-22 22:42:32 +0100637 smp_on_each_tlb(flush_tlb_one_ipi, (void *) vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640EXPORT_SYMBOL(flush_tlb_page);
641EXPORT_SYMBOL(flush_tlb_one);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200642
643#if defined(CONFIG_KEXEC)
644void (*dump_ipi_function_ptr)(void *) = NULL;
645void dump_send_ipi(void (*dump_ipi_callback)(void *))
646{
647 int i;
648 int cpu = smp_processor_id();
649
650 dump_ipi_function_ptr = dump_ipi_callback;
651 smp_mb();
652 for_each_online_cpu(i)
653 if (i != cpu)
654 mp_ops->send_ipi_single(i, SMP_DUMP);
655
656}
657EXPORT_SYMBOL(dump_send_ipi);
658#endif
Paul Burtoncc7964a2014-02-14 09:24:58 +0000659
660#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
661
662static DEFINE_PER_CPU(atomic_t, tick_broadcast_count);
663static DEFINE_PER_CPU(struct call_single_data, tick_broadcast_csd);
664
665void tick_broadcast(const struct cpumask *mask)
666{
667 atomic_t *count;
668 struct call_single_data *csd;
669 int cpu;
670
671 for_each_cpu(cpu, mask) {
672 count = &per_cpu(tick_broadcast_count, cpu);
673 csd = &per_cpu(tick_broadcast_csd, cpu);
674
675 if (atomic_inc_return(count) == 1)
676 smp_call_function_single_async(cpu, csd);
677 }
678}
679
680static void tick_broadcast_callee(void *info)
681{
682 int cpu = smp_processor_id();
683 tick_receive_broadcast();
684 atomic_set(&per_cpu(tick_broadcast_count, cpu), 0);
685}
686
687static int __init tick_broadcast_init(void)
688{
689 struct call_single_data *csd;
690 int cpu;
691
692 for (cpu = 0; cpu < NR_CPUS; cpu++) {
693 csd = &per_cpu(tick_broadcast_csd, cpu);
694 csd->func = tick_broadcast_callee;
695 }
696
697 return 0;
698}
699early_initcall(tick_broadcast_init);
700
701#endif /* CONFIG_GENERIC_CLOCKEVENTS_BROADCAST */