blob: 0a022ee33b2a15c5c9722e2e9aa5504358ba05f4 [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>
28#include <linux/module.h>
29#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/cpu.h>
39#include <asm/processor.h>
Ralf Baechlebdc92d742013-05-21 16:59:19 +020040#include <asm/idle.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010041#include <asm/r4k-timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/mmu_context.h>
Ralf Baechle7bcf7712007-10-11 23:46:09 +010043#include <asm/time.h>
David Howellsb81947c2012-03-28 18:30:02 +010044#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ralf Baechle41c594a2006-04-05 09:45:45 +010046#ifdef CONFIG_MIPS_MT_SMTC
47#include <asm/mipsmtregs.h>
48#endif /* CONFIG_MIPS_MT_SMTC */
49
Ralf Baechle1b2bc752009-06-23 10:00:31 +010050volatile cpumask_t cpu_callin_map; /* Bitmask of started secondaries */
David Daney2dc2ae32010-07-23 18:41:45 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
David Daney2dc2ae32010-07-23 18:41:45 -070053EXPORT_SYMBOL(__cpu_number_map);
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
David Daney2dc2ae32010-07-23 18:41:45 -070056EXPORT_SYMBOL(__cpu_logical_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Ralf Baechle0ab7aef2007-03-02 20:42:04 +000058/* Number of TCs (or siblings in Intel speak) per CPU core */
59int smp_num_siblings = 1;
60EXPORT_SYMBOL(smp_num_siblings);
61
62/* representing the TCs (or siblings in Intel speak) of each logical CPU */
63cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
64EXPORT_SYMBOL(cpu_sibling_map);
65
66/* representing cpus for which sibling maps can be computed */
67static cpumask_t cpu_sibling_setup_map;
68
69static inline void set_cpu_sibling_map(int cpu)
70{
71 int i;
72
73 cpu_set(cpu, cpu_sibling_setup_map);
74
75 if (smp_num_siblings > 1) {
76 for_each_cpu_mask(i, cpu_sibling_setup_map) {
77 if (cpu_data[cpu].core == cpu_data[i].core) {
78 cpu_set(i, cpu_sibling_map[cpu]);
79 cpu_set(cpu, cpu_sibling_map[i]);
80 }
81 }
82 } else
83 cpu_set(cpu, cpu_sibling_map[cpu]);
84}
85
Ralf Baechle87353d82007-11-19 12:23:51 +000086struct plat_smp_ops *mp_ops;
Sanjay Lal82d45de2012-11-21 18:34:14 -080087EXPORT_SYMBOL(mp_ops);
Ralf Baechle87353d82007-11-19 12:23:51 +000088
Paul Gortmaker078a55f2013-06-18 13:38:59 +000089void register_smp_ops(struct plat_smp_ops *ops)
Ralf Baechle87353d82007-11-19 12:23:51 +000090{
Thiemo Seufer83738e32008-05-06 11:21:22 +010091 if (mp_ops)
92 printk(KERN_WARNING "Overriding previously set SMP ops\n");
Ralf Baechle87353d82007-11-19 12:23:51 +000093
94 mp_ops = ops;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * First C code run on the secondary CPUs after being started up by
99 * the master.
100 */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000101asmlinkage void start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800103 unsigned int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Ralf Baechle41c594a2006-04-05 09:45:45 +0100105#ifdef CONFIG_MIPS_MT_SMTC
106 /* Only do cpu_probe for first TC of CPU */
Steven J. Hill889a4c7b2012-04-09 10:58:39 -0500107 if ((read_c0_tcbind() & TCBIND_CURTC) != 0)
108 __cpu_name[smp_processor_id()] = __cpu_name[0];
109 else
Ralf Baechle41c594a2006-04-05 09:45:45 +0100110#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 cpu_probe();
112 cpu_report();
David Daney6650df32012-05-15 00:04:50 -0700113 per_cpu_trap_init(false);
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100114 mips_clockevent_init();
Ralf Baechle87353d82007-11-19 12:23:51 +0000115 mp_ops->init_secondary();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /*
118 * XXX parity protection should be folded in here when it's converted
119 * to an option instead of something based on .cputype
120 */
121
122 calibrate_delay();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800123 preempt_disable();
124 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 cpu_data[cpu].udelay_val = loops_per_jiffy;
126
Manfred Spraule545a612008-09-07 16:57:22 +0200127 notify_cpu_starting(cpu);
128
Yong Zhangb9a09a02012-07-19 09:13:53 +0200129 set_cpu_online(cpu, true);
130
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000131 set_cpu_sibling_map(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 cpu_set(cpu, cpu_callin_map);
134
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530135 synchronise_count_slave(cpu);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100136
Yong Zhangb789ad62012-07-19 09:13:53 +0200137 /*
138 * irq will be enabled in ->smp_finish(), enabling it too early
139 * is dangerous.
140 */
141 WARN_ON_ONCE(!irqs_disabled());
Yong Zhang5309bda2012-07-19 09:13:53 +0200142 mp_ops->smp_finish();
143
Thomas Gleixnercdbedc62013-03-21 22:49:52 +0100144 cpu_startup_entry(CPUHP_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Jens Axboe2f304c02008-06-17 10:45:23 +0200147/*
148 * Call into both interrupt handlers, as we share the IPI for them
149 */
Wu Zhangjin8f99a162009-11-20 20:34:33 +0800150void __irq_entry smp_call_function_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 irq_enter();
Jens Axboe2f304c02008-06-17 10:45:23 +0200153 generic_smp_call_function_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static void stop_this_cpu(void *dummy)
158{
159 /*
160 * Remove this CPU:
161 */
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030162 set_cpu_online(smp_processor_id(), false);
Ralf Baechle7920c4d2008-10-18 13:23:10 +0100163 for (;;) {
164 if (cpu_wait)
165 (*cpu_wait)(); /* Wait if available. */
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169void smp_send_stop(void)
170{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200171 smp_call_function(stop_this_cpu, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174void __init smp_cpus_done(unsigned int max_cpus)
175{
Ralf Baechle87353d82007-11-19 12:23:51 +0000176 mp_ops->cpus_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/* called from main before smp_init() */
180void __init smp_prepare_cpus(unsigned int max_cpus)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 init_new_context(current, &init_mm);
183 current_thread_info()->cpu = 0;
Ralf Baechle87353d82007-11-19 12:23:51 +0000184 mp_ops->prepare_cpus(max_cpus);
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000185 set_cpu_sibling_map(0);
Ralf Baechle320e6ab2006-05-22 14:24:04 +0100186#ifndef CONFIG_HOTPLUG_CPU
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030187 init_cpu_present(cpu_possible_mask);
Ralf Baechle320e6ab2006-05-22 14:24:04 +0100188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/* preload SMP state for boot cpu */
Greg Kroah-Hartman28eb0e42012-12-21 14:04:39 -0800192void smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Rusty Russell4037ac62009-09-24 09:34:47 -0600194 set_cpu_possible(0, true);
195 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 cpu_set(0, cpu_callin_map);
197}
198
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000199int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Thomas Gleixner360014a2012-04-20 13:05:51 +0000201 mp_ops->boot_secondary(cpu, tidle);
Ralf Baechleb727a602005-02-22 21:18:01 +0000202
203 /*
204 * Trust is futile. We should really have timeouts ...
205 */
206 while (!cpu_isset(cpu, cpu_callin_map))
207 udelay(100);
208
Jayachandran Ccf9bfe52012-08-14 18:56:13 +0530209 synchronise_count_master(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211}
212
213/* Not really SMP stuff ... */
214int setup_profiling_timer(unsigned int multiplier)
215{
216 return 0;
217}
218
219static void flush_tlb_all_ipi(void *info)
220{
221 local_flush_tlb_all();
222}
223
224void flush_tlb_all(void)
225{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200226 on_each_cpu(flush_tlb_all_ipi, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229static void flush_tlb_mm_ipi(void *mm)
230{
231 local_flush_tlb_mm((struct mm_struct *)mm);
232}
233
234/*
Ralf Baechle25969352006-06-22 22:42:32 +0100235 * Special Variant of smp_call_function for use by TLB functions:
236 *
237 * o No return value
238 * o collapses to normal function call on UP kernels
239 * o collapses to normal function call on systems with a single shared
240 * primary cache.
241 * o CONFIG_MIPS_MT_SMTC currently implies there is only one physical core.
242 */
243static inline void smp_on_other_tlbs(void (*func) (void *info), void *info)
244{
245#ifndef CONFIG_MIPS_MT_SMTC
Jens Axboe8691e5a2008-06-06 11:18:06 +0200246 smp_call_function(func, info, 1);
Ralf Baechle25969352006-06-22 22:42:32 +0100247#endif
248}
249
250static inline void smp_on_each_tlb(void (*func) (void *info), void *info)
251{
252 preempt_disable();
253
254 smp_on_other_tlbs(func, info);
255 func(info);
256
257 preempt_enable();
258}
259
260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * The following tlb flush calls are invoked when old translations are
262 * being torn down, or pte attributes are changing. For single threaded
263 * address spaces, a new context is obtained on the current cpu, and tlb
264 * context on other cpus are invalidated to force a new context allocation
265 * at switch_mm time, should the mm ever be used on other cpus. For
266 * multithreaded address spaces, intercpu interrupts have to be sent.
267 * Another case where intercpu interrupts are required is when the target
268 * mm might be active on another cpu (eg debuggers doing the flushes on
269 * behalf of debugees, kswapd stealing pages from another process etc).
270 * Kanoj 07/00.
271 */
272
273void flush_tlb_mm(struct mm_struct *mm)
274{
275 preempt_disable();
276
277 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Ralf Baechlec50cade2007-10-04 16:57:08 +0100278 smp_on_other_tlbs(flush_tlb_mm_ipi, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100280 unsigned int cpu;
281
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030282 for_each_online_cpu(cpu) {
283 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100284 cpu_context(cpu, mm) = 0;
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 local_flush_tlb_mm(mm);
288
289 preempt_enable();
290}
291
292struct flush_tlb_data {
293 struct vm_area_struct *vma;
294 unsigned long addr1;
295 unsigned long addr2;
296};
297
298static void flush_tlb_range_ipi(void *info)
299{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100300 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
303}
304
305void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
306{
307 struct mm_struct *mm = vma->vm_mm;
308
309 preempt_disable();
310 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100311 struct flush_tlb_data fd = {
312 .vma = vma,
313 .addr1 = start,
314 .addr2 = end,
315 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Ralf Baechlec50cade2007-10-04 16:57:08 +0100317 smp_on_other_tlbs(flush_tlb_range_ipi, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100319 unsigned int cpu;
320
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030321 for_each_online_cpu(cpu) {
322 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100323 cpu_context(cpu, mm) = 0;
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 local_flush_tlb_range(vma, start, end);
327 preempt_enable();
328}
329
330static void flush_tlb_kernel_range_ipi(void *info)
331{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100332 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
335}
336
337void flush_tlb_kernel_range(unsigned long start, unsigned long end)
338{
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100339 struct flush_tlb_data fd = {
340 .addr1 = start,
341 .addr2 = end,
342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200344 on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347static void flush_tlb_page_ipi(void *info)
348{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100349 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 local_flush_tlb_page(fd->vma, fd->addr1);
352}
353
354void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
355{
356 preempt_disable();
357 if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100358 struct flush_tlb_data fd = {
359 .vma = vma,
360 .addr1 = page,
361 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Ralf Baechlec50cade2007-10-04 16:57:08 +0100363 smp_on_other_tlbs(flush_tlb_page_ipi, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100365 unsigned int cpu;
366
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030367 for_each_online_cpu(cpu) {
368 if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100369 cpu_context(cpu, vma->vm_mm) = 0;
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 local_flush_tlb_page(vma, page);
373 preempt_enable();
374}
375
376static void flush_tlb_one_ipi(void *info)
377{
378 unsigned long vaddr = (unsigned long) info;
379
380 local_flush_tlb_one(vaddr);
381}
382
383void flush_tlb_one(unsigned long vaddr)
384{
Ralf Baechle25969352006-06-22 22:42:32 +0100385 smp_on_each_tlb(flush_tlb_one_ipi, (void *) vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388EXPORT_SYMBOL(flush_tlb_page);
389EXPORT_SYMBOL(flush_tlb_one);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200390
391#if defined(CONFIG_KEXEC)
392void (*dump_ipi_function_ptr)(void *) = NULL;
393void dump_send_ipi(void (*dump_ipi_callback)(void *))
394{
395 int i;
396 int cpu = smp_processor_id();
397
398 dump_ipi_function_ptr = dump_ipi_callback;
399 smp_mb();
400 for_each_online_cpu(i)
401 if (i != cpu)
402 mp_ops->send_ipi_single(i, SMP_DUMP);
403
404}
405EXPORT_SYMBOL(dump_send_ipi);
406#endif