blob: e72e6844d134759218227bee63faac5f2d9bbea8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/atomic.h>
37#include <asm/cpu.h>
38#include <asm/processor.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010039#include <asm/r4k-timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/system.h>
41#include <asm/mmu_context.h>
Ralf Baechle7bcf7712007-10-11 23:46:09 +010042#include <asm/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ralf Baechle41c594a2006-04-05 09:45:45 +010044#ifdef CONFIG_MIPS_MT_SMTC
45#include <asm/mipsmtregs.h>
46#endif /* CONFIG_MIPS_MT_SMTC */
47
Ralf Baechle1b2bc752009-06-23 10:00:31 +010048volatile cpumask_t cpu_callin_map; /* Bitmask of started secondaries */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
50int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
51
Ralf Baechle0ab7aef2007-03-02 20:42:04 +000052/* Number of TCs (or siblings in Intel speak) per CPU core */
53int smp_num_siblings = 1;
54EXPORT_SYMBOL(smp_num_siblings);
55
56/* representing the TCs (or siblings in Intel speak) of each logical CPU */
57cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
58EXPORT_SYMBOL(cpu_sibling_map);
59
60/* representing cpus for which sibling maps can be computed */
61static cpumask_t cpu_sibling_setup_map;
62
63static inline void set_cpu_sibling_map(int cpu)
64{
65 int i;
66
67 cpu_set(cpu, cpu_sibling_setup_map);
68
69 if (smp_num_siblings > 1) {
70 for_each_cpu_mask(i, cpu_sibling_setup_map) {
71 if (cpu_data[cpu].core == cpu_data[i].core) {
72 cpu_set(i, cpu_sibling_map[cpu]);
73 cpu_set(cpu, cpu_sibling_map[i]);
74 }
75 }
76 } else
77 cpu_set(cpu, cpu_sibling_map[cpu]);
78}
79
Ralf Baechle87353d82007-11-19 12:23:51 +000080struct plat_smp_ops *mp_ops;
81
82__cpuinit void register_smp_ops(struct plat_smp_ops *ops)
83{
Thiemo Seufer83738e32008-05-06 11:21:22 +010084 if (mp_ops)
85 printk(KERN_WARNING "Overriding previously set SMP ops\n");
Ralf Baechle87353d82007-11-19 12:23:51 +000086
87 mp_ops = ops;
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * First C code run on the secondary CPUs after being started up by
92 * the master.
93 */
Ralf Baechle4ebd5232007-05-31 16:15:01 +010094asmlinkage __cpuinit void start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Nick Piggin5bfb5d62005-11-08 21:39:01 -080096 unsigned int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Ralf Baechle41c594a2006-04-05 09:45:45 +010098#ifdef CONFIG_MIPS_MT_SMTC
99 /* Only do cpu_probe for first TC of CPU */
100 if ((read_c0_tcbind() & TCBIND_CURTC) == 0)
101#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 cpu_probe();
103 cpu_report();
104 per_cpu_trap_init();
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100105 mips_clockevent_init();
Ralf Baechle87353d82007-11-19 12:23:51 +0000106 mp_ops->init_secondary();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /*
109 * XXX parity protection should be folded in here when it's converted
110 * to an option instead of something based on .cputype
111 */
112
113 calibrate_delay();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800114 preempt_disable();
115 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 cpu_data[cpu].udelay_val = loops_per_jiffy;
117
Manfred Spraule545a612008-09-07 16:57:22 +0200118 notify_cpu_starting(cpu);
119
Ralf Baechle87353d82007-11-19 12:23:51 +0000120 mp_ops->smp_finish();
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000121 set_cpu_sibling_map(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 cpu_set(cpu, cpu_callin_map);
124
Ralf Baechle39b8d522008-04-28 17:14:26 +0100125 synchronise_count_slave();
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 cpu_idle();
128}
129
Jens Axboe2f304c02008-06-17 10:45:23 +0200130/*
131 * Call into both interrupt handlers, as we share the IPI for them
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133void smp_call_function_interrupt(void)
134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 irq_enter();
Jens Axboe2f304c02008-06-17 10:45:23 +0200136 generic_smp_call_function_single_interrupt();
137 generic_smp_call_function_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141static void stop_this_cpu(void *dummy)
142{
143 /*
144 * Remove this CPU:
145 */
146 cpu_clear(smp_processor_id(), cpu_online_map);
Ralf Baechle7920c4d2008-10-18 13:23:10 +0100147 for (;;) {
148 if (cpu_wait)
149 (*cpu_wait)(); /* Wait if available. */
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153void smp_send_stop(void)
154{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200155 smp_call_function(stop_this_cpu, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158void __init smp_cpus_done(unsigned int max_cpus)
159{
Ralf Baechle87353d82007-11-19 12:23:51 +0000160 mp_ops->cpus_done();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100161 synchronise_count_master();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164/* called from main before smp_init() */
165void __init smp_prepare_cpus(unsigned int max_cpus)
166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 init_new_context(current, &init_mm);
168 current_thread_info()->cpu = 0;
Ralf Baechle87353d82007-11-19 12:23:51 +0000169 mp_ops->prepare_cpus(max_cpus);
Ralf Baechle0ab7aef2007-03-02 20:42:04 +0000170 set_cpu_sibling_map(0);
Ralf Baechle320e6ab2006-05-22 14:24:04 +0100171#ifndef CONFIG_HOTPLUG_CPU
Rusty Russell4037ac62009-09-24 09:34:47 -0600172 init_cpu_present(&cpu_possible_map);
Ralf Baechle320e6ab2006-05-22 14:24:04 +0100173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/* preload SMP state for boot cpu */
177void __devinit smp_prepare_boot_cpu(void)
178{
Rusty Russell4037ac62009-09-24 09:34:47 -0600179 set_cpu_possible(0, true);
180 set_cpu_online(0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 cpu_set(0, cpu_callin_map);
182}
183
184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Called once for each "cpu_possible(cpu)". Needs to spin up the cpu
186 * and keep control until "cpu_online(cpu)" is set. Note: cpu is
187 * physical, not logical.
188 */
Ralf Baechle1b2bc752009-06-23 10:00:31 +0100189static struct task_struct *cpu_idle_thread[NR_CPUS];
190
Gautham R Shenoyb282b6f2007-01-10 23:15:34 -0800191int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Ralf Baechleb727a602005-02-22 21:18:01 +0000193 struct task_struct *idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Ralf Baechleb727a602005-02-22 21:18:01 +0000195 /*
196 * Processor goes to start_secondary(), sets online flag
197 * The following code is purely to make sure
198 * Linux can schedule processes on this slave.
199 */
Ralf Baechle1b2bc752009-06-23 10:00:31 +0100200 if (!cpu_idle_thread[cpu]) {
201 idle = fork_idle(cpu);
202 cpu_idle_thread[cpu] = idle;
203
204 if (IS_ERR(idle))
205 panic(KERN_ERR "Fork failed for CPU %d", cpu);
206 } else {
207 idle = cpu_idle_thread[cpu];
208 init_idle(idle, cpu);
209 }
Ralf Baechleb727a602005-02-22 21:18:01 +0000210
Ralf Baechle87353d82007-11-19 12:23:51 +0000211 mp_ops->boot_secondary(cpu, idle);
Ralf Baechleb727a602005-02-22 21:18:01 +0000212
213 /*
214 * Trust is futile. We should really have timeouts ...
215 */
216 while (!cpu_isset(cpu, cpu_callin_map))
217 udelay(100);
218
219 cpu_set(cpu, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return 0;
222}
223
224/* Not really SMP stuff ... */
225int setup_profiling_timer(unsigned int multiplier)
226{
227 return 0;
228}
229
230static void flush_tlb_all_ipi(void *info)
231{
232 local_flush_tlb_all();
233}
234
235void flush_tlb_all(void)
236{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200237 on_each_cpu(flush_tlb_all_ipi, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static void flush_tlb_mm_ipi(void *mm)
241{
242 local_flush_tlb_mm((struct mm_struct *)mm);
243}
244
245/*
Ralf Baechle25969352006-06-22 22:42:32 +0100246 * Special Variant of smp_call_function for use by TLB functions:
247 *
248 * o No return value
249 * o collapses to normal function call on UP kernels
250 * o collapses to normal function call on systems with a single shared
251 * primary cache.
252 * o CONFIG_MIPS_MT_SMTC currently implies there is only one physical core.
253 */
254static inline void smp_on_other_tlbs(void (*func) (void *info), void *info)
255{
256#ifndef CONFIG_MIPS_MT_SMTC
Jens Axboe8691e5a2008-06-06 11:18:06 +0200257 smp_call_function(func, info, 1);
Ralf Baechle25969352006-06-22 22:42:32 +0100258#endif
259}
260
261static inline void smp_on_each_tlb(void (*func) (void *info), void *info)
262{
263 preempt_disable();
264
265 smp_on_other_tlbs(func, info);
266 func(info);
267
268 preempt_enable();
269}
270
271/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * The following tlb flush calls are invoked when old translations are
273 * being torn down, or pte attributes are changing. For single threaded
274 * address spaces, a new context is obtained on the current cpu, and tlb
275 * context on other cpus are invalidated to force a new context allocation
276 * at switch_mm time, should the mm ever be used on other cpus. For
277 * multithreaded address spaces, intercpu interrupts have to be sent.
278 * Another case where intercpu interrupts are required is when the target
279 * mm might be active on another cpu (eg debuggers doing the flushes on
280 * behalf of debugees, kswapd stealing pages from another process etc).
281 * Kanoj 07/00.
282 */
283
284void flush_tlb_mm(struct mm_struct *mm)
285{
286 preempt_disable();
287
288 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Ralf Baechlec50cade2007-10-04 16:57:08 +0100289 smp_on_other_tlbs(flush_tlb_mm_ipi, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100291 cpumask_t mask = cpu_online_map;
292 unsigned int cpu;
293
294 cpu_clear(smp_processor_id(), mask);
Ralf Baechleece8a9e2007-10-12 15:03:38 +0100295 for_each_cpu_mask(cpu, mask)
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100296 if (cpu_context(cpu, mm))
297 cpu_context(cpu, mm) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 local_flush_tlb_mm(mm);
300
301 preempt_enable();
302}
303
304struct flush_tlb_data {
305 struct vm_area_struct *vma;
306 unsigned long addr1;
307 unsigned long addr2;
308};
309
310static void flush_tlb_range_ipi(void *info)
311{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100312 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
315}
316
317void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
318{
319 struct mm_struct *mm = vma->vm_mm;
320
321 preempt_disable();
322 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100323 struct flush_tlb_data fd = {
324 .vma = vma,
325 .addr1 = start,
326 .addr2 = end,
327 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Ralf Baechlec50cade2007-10-04 16:57:08 +0100329 smp_on_other_tlbs(flush_tlb_range_ipi, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100331 cpumask_t mask = cpu_online_map;
332 unsigned int cpu;
333
334 cpu_clear(smp_processor_id(), mask);
Ralf Baechleece8a9e2007-10-12 15:03:38 +0100335 for_each_cpu_mask(cpu, mask)
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100336 if (cpu_context(cpu, mm))
337 cpu_context(cpu, mm) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 local_flush_tlb_range(vma, start, end);
340 preempt_enable();
341}
342
343static void flush_tlb_kernel_range_ipi(void *info)
344{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100345 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
348}
349
350void flush_tlb_kernel_range(unsigned long start, unsigned long end)
351{
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100352 struct flush_tlb_data fd = {
353 .addr1 = start,
354 .addr2 = end,
355 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200357 on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360static void flush_tlb_page_ipi(void *info)
361{
Ralf Baechlec50cade2007-10-04 16:57:08 +0100362 struct flush_tlb_data *fd = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 local_flush_tlb_page(fd->vma, fd->addr1);
365}
366
367void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
368{
369 preempt_disable();
370 if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
Ralf Baechle89a8a5a2007-10-04 18:18:52 +0100371 struct flush_tlb_data fd = {
372 .vma = vma,
373 .addr1 = page,
374 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Ralf Baechlec50cade2007-10-04 16:57:08 +0100376 smp_on_other_tlbs(flush_tlb_page_ipi, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 } else {
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100378 cpumask_t mask = cpu_online_map;
379 unsigned int cpu;
380
381 cpu_clear(smp_processor_id(), mask);
Ralf Baechleece8a9e2007-10-12 15:03:38 +0100382 for_each_cpu_mask(cpu, mask)
Ralf Baechleb5eb5512007-10-03 19:16:57 +0100383 if (cpu_context(cpu, vma->vm_mm))
384 cpu_context(cpu, vma->vm_mm) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 local_flush_tlb_page(vma, page);
387 preempt_enable();
388}
389
390static void flush_tlb_one_ipi(void *info)
391{
392 unsigned long vaddr = (unsigned long) info;
393
394 local_flush_tlb_one(vaddr);
395}
396
397void flush_tlb_one(unsigned long vaddr)
398{
Ralf Baechle25969352006-06-22 22:42:32 +0100399 smp_on_each_tlb(flush_tlb_one_ipi, (void *) vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402EXPORT_SYMBOL(flush_tlb_page);
403EXPORT_SYMBOL(flush_tlb_one);