blob: 139c312a41f73c04edc06e58c7e6510f3304fa5f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* smp.c: Sparc SMP support.
2 *
3 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
4 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Copyright (C) 2004 Keith M Wesolowski (wesolows@foobazco.org)
6 */
7
8#include <asm/head.h>
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/threads.h>
13#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/interrupt.h>
15#include <linux/kernel_stat.h>
16#include <linux/init.h>
17#include <linux/spinlock.h>
18#include <linux/mm.h>
19#include <linux/fs.h>
20#include <linux/seq_file.h>
21#include <linux/cache.h>
22#include <linux/delay.h>
23
24#include <asm/ptrace.h>
25#include <asm/atomic.h>
26
27#include <asm/irq.h>
28#include <asm/page.h>
29#include <asm/pgalloc.h>
30#include <asm/pgtable.h>
31#include <asm/oplib.h>
32#include <asm/cacheflush.h>
33#include <asm/tlbflush.h>
34#include <asm/cpudata.h>
Konrad Eisele84017072009-08-31 22:08:13 +000035#include <asm/leon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Al Viro32231a62007-07-21 19:18:57 -070037#include "irq.h"
38
Al Viro409832f2008-11-22 17:33:54 +000039volatile unsigned long cpu_callin_map[NR_CPUS] __cpuinitdata = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Bob Breuera54123e2006-03-23 22:36:19 -080041cpumask_t smp_commenced_mask = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* The only guaranteed locking primitive available on all Sparc
44 * processors is 'ldstub [%reg + immediate], %dest_reg' which atomically
45 * places the current byte at the effective address into dest_reg and
46 * places 0xff there afterwards. Pretty lame locking primitive
47 * compared to the Alpha and the Intel no? Most Sparcs have 'swap'
48 * instruction which is much better...
49 */
50
Bob Breuer92d452f2006-06-20 00:36:10 -070051void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 int cpu_node;
54
55 cpu_data(id).udelay_val = loops_per_jiffy;
56
57 cpu_find_by_mid(id, &cpu_node);
58 cpu_data(id).clock_tick = prom_getintdefault(cpu_node,
59 "clock-frequency", 0);
60 cpu_data(id).prom_node = cpu_node;
61 cpu_data(id).mid = cpu_get_hwmid(cpu_node);
Krzysztof Helt650fb832006-06-10 22:03:43 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (cpu_data(id).mid < 0)
64 panic("No MID found for CPU%d at node 0x%08d", id, cpu_node);
65}
66
67void __init smp_cpus_done(unsigned int max_cpus)
68{
Bob Breuera54123e2006-03-23 22:36:19 -080069 extern void smp4m_smp_done(void);
Raymond Burns8b3c8482006-07-17 21:57:09 -070070 extern void smp4d_smp_done(void);
Bob Breuera54123e2006-03-23 22:36:19 -080071 unsigned long bogosum = 0;
Rusty Russellec7c14b2009-03-16 14:40:24 +103072 int cpu, num = 0;
Bob Breuera54123e2006-03-23 22:36:19 -080073
Rusty Russellec7c14b2009-03-16 14:40:24 +103074 for_each_online_cpu(cpu) {
75 num++;
76 bogosum += cpu_data(cpu).udelay_val;
77 }
Bob Breuera54123e2006-03-23 22:36:19 -080078
79 printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
80 num, bogosum/(500000/HZ),
81 (bogosum/(5000/HZ))%100);
82
Raymond Burns8b3c8482006-07-17 21:57:09 -070083 switch(sparc_cpu_model) {
84 case sun4:
85 printk("SUN4\n");
86 BUG();
87 break;
88 case sun4c:
89 printk("SUN4C\n");
90 BUG();
91 break;
92 case sun4m:
93 smp4m_smp_done();
94 break;
95 case sun4d:
96 smp4d_smp_done();
97 break;
Konrad Eisele84017072009-08-31 22:08:13 +000098 case sparc_leon:
99 leon_smp_done();
100 break;
Raymond Burns8b3c8482006-07-17 21:57:09 -0700101 case sun4e:
102 printk("SUN4E\n");
103 BUG();
104 break;
105 case sun4u:
106 printk("SUN4U\n");
107 BUG();
108 break;
109 default:
110 printk("UNKNOWN!\n");
111 BUG();
112 break;
113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116void cpu_panic(void)
117{
118 printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
119 panic("SMP bolixed\n");
120}
121
Al Viro409832f2008-11-22 17:33:54 +0000122struct linux_prom_registers smp_penguin_ctable __cpuinitdata = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124void smp_send_reschedule(int cpu)
125{
Daniel Hellstromd6d04812011-05-02 00:08:51 +0000126 /*
127 * CPU model dependent way of implementing IPI generation targeting
128 * a single CPU. The trap handler needs only to do trap entry/return
129 * to call schedule.
130 */
131 BTFIXUP_CALL(smp_ipi_resched)(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134void smp_send_stop(void)
135{
136}
137
Daniel Hellstromd6d04812011-05-02 00:08:51 +0000138void arch_send_call_function_single_ipi(int cpu)
139{
140 /* trigger one IPI single call on one CPU */
141 BTFIXUP_CALL(smp_ipi_single)(cpu);
142}
143
144void arch_send_call_function_ipi_mask(const struct cpumask *mask)
145{
146 int cpu;
147
148 /* trigger IPI mask call on each CPU */
149 for_each_cpu(cpu, mask)
150 BTFIXUP_CALL(smp_ipi_mask_one)(cpu);
151}
152
153void smp_resched_interrupt(void)
154{
155 local_cpu_data().irq_resched_count++;
156 /*
157 * do nothing, since it all was about calling re-schedule
158 * routine called by interrupt return code.
159 */
160}
161
162void smp_call_function_single_interrupt(void)
163{
164 irq_enter();
165 generic_smp_call_function_single_interrupt();
166 local_cpu_data().irq_call_count++;
167 irq_exit();
168}
169
170void smp_call_function_interrupt(void)
171{
172 irq_enter();
173 generic_smp_call_function_interrupt();
174 local_cpu_data().irq_call_count++;
175 irq_exit();
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178void smp_flush_cache_all(void)
179{
180 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_cache_all));
181 local_flush_cache_all();
182}
183
184void smp_flush_tlb_all(void)
185{
186 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_all));
187 local_flush_tlb_all();
188}
189
190void smp_flush_cache_mm(struct mm_struct *mm)
191{
192 if(mm->context != NO_CONTEXT) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700193 cpumask_t cpu_mask;
194 cpumask_copy(&cpu_mask, mm_cpumask(mm));
195 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
196 if (!cpumask_empty(&cpu_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
198 local_flush_cache_mm(mm);
199 }
200}
201
202void smp_flush_tlb_mm(struct mm_struct *mm)
203{
204 if(mm->context != NO_CONTEXT) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700205 cpumask_t cpu_mask;
206 cpumask_copy(&cpu_mask, mm_cpumask(mm));
207 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
208 if (!cpumask_empty(&cpu_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm);
210 if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
Rusty Russell81f1adf2009-03-16 14:40:39 +1030211 cpumask_copy(mm_cpumask(mm),
212 cpumask_of(smp_processor_id()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 local_flush_tlb_mm(mm);
215 }
216}
217
218void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
219 unsigned long end)
220{
221 struct mm_struct *mm = vma->vm_mm;
222
223 if (mm->context != NO_CONTEXT) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700224 cpumask_t cpu_mask;
225 cpumask_copy(&cpu_mask, mm_cpumask(mm));
226 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
227 if (!cpumask_empty(&cpu_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
229 local_flush_cache_range(vma, start, end);
230 }
231}
232
233void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
234 unsigned long end)
235{
236 struct mm_struct *mm = vma->vm_mm;
237
238 if (mm->context != NO_CONTEXT) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700239 cpumask_t cpu_mask;
240 cpumask_copy(&cpu_mask, mm_cpumask(mm));
241 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
242 if (!cpumask_empty(&cpu_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
244 local_flush_tlb_range(vma, start, end);
245 }
246}
247
248void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
249{
250 struct mm_struct *mm = vma->vm_mm;
251
252 if(mm->context != NO_CONTEXT) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700253 cpumask_t cpu_mask;
254 cpumask_copy(&cpu_mask, mm_cpumask(mm));
255 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
256 if (!cpumask_empty(&cpu_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
258 local_flush_cache_page(vma, page);
259 }
260}
261
262void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
263{
264 struct mm_struct *mm = vma->vm_mm;
265
266 if(mm->context != NO_CONTEXT) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700267 cpumask_t cpu_mask;
268 cpumask_copy(&cpu_mask, mm_cpumask(mm));
269 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
270 if (!cpumask_empty(&cpu_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
272 local_flush_tlb_page(vma, page);
273 }
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276void smp_flush_page_to_ram(unsigned long page)
277{
278 /* Current theory is that those who call this are the one's
279 * who have just dirtied their cache with the pages contents
280 * in kernel space, therefore we only run this on local cpu.
281 *
282 * XXX This experiment failed, research further... -DaveM
283 */
284#if 1
285 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page);
286#endif
287 local_flush_page_to_ram(page);
288}
289
290void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
291{
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700292 cpumask_t cpu_mask;
293 cpumask_copy(&cpu_mask, mm_cpumask(mm));
294 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
295 if (!cpumask_empty(&cpu_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
297 local_flush_sig_insns(mm, insn_addr);
298}
299
300extern unsigned int lvl14_resolution;
301
302/* /proc/profile writes can call this, don't __init it please. */
303static DEFINE_SPINLOCK(prof_setup_lock);
304
305int setup_profiling_timer(unsigned int multiplier)
306{
307 int i;
308 unsigned long flags;
309
310 /* Prevent level14 ticker IRQ flooding. */
311 if((!multiplier) || (lvl14_resolution / multiplier) < 500)
312 return -EINVAL;
313
314 spin_lock_irqsave(&prof_setup_lock, flags);
KAMEZAWA Hiroyukifff8efe2006-04-10 22:52:51 -0700315 for_each_possible_cpu(i) {
Andrew Morton394e3902006-03-23 03:01:05 -0800316 load_profile_irq(i, lvl14_resolution / multiplier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 prof_multiplier(i) = multiplier;
318 }
319 spin_unlock_irqrestore(&prof_setup_lock, flags);
320
321 return 0;
322}
323
Bob Breuera54123e2006-03-23 22:36:19 -0800324void __init smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Al Virob4cff842007-02-01 13:52:33 +0000326 extern void __init smp4m_boot_cpus(void);
327 extern void __init smp4d_boot_cpus(void);
Bob Breuer7202fb42006-06-20 00:30:31 -0700328 int i, cpuid, extra;
Bob Breuera54123e2006-03-23 22:36:19 -0800329
Bob Breuera54123e2006-03-23 22:36:19 -0800330 printk("Entering SMP Mode...\n");
331
Bob Breuera54123e2006-03-23 22:36:19 -0800332 extra = 0;
333 for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
Bob Breuer7202fb42006-06-20 00:30:31 -0700334 if (cpuid >= NR_CPUS)
Bob Breuera54123e2006-03-23 22:36:19 -0800335 extra++;
336 }
Bob Breuer7202fb42006-06-20 00:30:31 -0700337 /* i = number of cpus */
338 if (extra && max_cpus > i - extra)
Bob Breuera54123e2006-03-23 22:36:19 -0800339 printk("Warning: NR_CPUS is too low to start all cpus\n");
340
341 smp_store_cpu_info(boot_cpu_id);
342
Raymond Burns8b3c8482006-07-17 21:57:09 -0700343 switch(sparc_cpu_model) {
344 case sun4:
345 printk("SUN4\n");
346 BUG();
347 break;
348 case sun4c:
349 printk("SUN4C\n");
350 BUG();
351 break;
352 case sun4m:
353 smp4m_boot_cpus();
354 break;
355 case sun4d:
356 smp4d_boot_cpus();
357 break;
Konrad Eisele84017072009-08-31 22:08:13 +0000358 case sparc_leon:
359 leon_boot_cpus();
360 break;
Raymond Burns8b3c8482006-07-17 21:57:09 -0700361 case sun4e:
362 printk("SUN4E\n");
363 BUG();
364 break;
365 case sun4u:
366 printk("SUN4U\n");
367 BUG();
368 break;
369 default:
370 printk("UNKNOWN!\n");
371 BUG();
372 break;
373 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Bob Breuer7202fb42006-06-20 00:30:31 -0700376/* Set this up early so that things like the scheduler can init
377 * properly. We use the same cpu mask for both the present and
378 * possible cpu map.
379 */
380void __init smp_setup_cpu_possible_map(void)
381{
382 int instance, mid;
383
384 instance = 0;
385 while (!cpu_find_by_instance(instance, NULL, &mid)) {
386 if (mid < NR_CPUS) {
Rusty Russellfe739712009-03-16 14:40:22 +1030387 set_cpu_possible(mid, true);
388 set_cpu_present(mid, true);
Bob Breuer7202fb42006-06-20 00:30:31 -0700389 }
390 instance++;
391 }
392}
393
Bob Breuer92d452f2006-06-20 00:36:10 -0700394void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Bob Breuera54123e2006-03-23 22:36:19 -0800396 int cpuid = hard_smp_processor_id();
397
398 if (cpuid >= NR_CPUS) {
399 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
400 prom_halt();
401 }
402 if (cpuid != 0)
403 printk("boot cpu id != 0, this could work but is untested\n");
404
405 current_thread_info()->cpu = cpuid;
Rusty Russellfe739712009-03-16 14:40:22 +1030406 set_cpu_online(cpuid, true);
407 set_cpu_possible(cpuid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Bob Breuer92d452f2006-06-20 00:36:10 -0700410int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Al Virob4cff842007-02-01 13:52:33 +0000412 extern int __cpuinit smp4m_boot_one_cpu(int);
413 extern int __cpuinit smp4d_boot_one_cpu(int);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700414 int ret=0;
Bob Breuera54123e2006-03-23 22:36:19 -0800415
Raymond Burns8b3c8482006-07-17 21:57:09 -0700416 switch(sparc_cpu_model) {
417 case sun4:
418 printk("SUN4\n");
419 BUG();
420 break;
421 case sun4c:
422 printk("SUN4C\n");
423 BUG();
424 break;
425 case sun4m:
426 ret = smp4m_boot_one_cpu(cpu);
427 break;
428 case sun4d:
429 ret = smp4d_boot_one_cpu(cpu);
430 break;
Konrad Eisele84017072009-08-31 22:08:13 +0000431 case sparc_leon:
432 ret = leon_boot_one_cpu(cpu);
433 break;
Raymond Burns8b3c8482006-07-17 21:57:09 -0700434 case sun4e:
435 printk("SUN4E\n");
436 BUG();
437 break;
438 case sun4u:
439 printk("SUN4U\n");
440 BUG();
441 break;
442 default:
443 printk("UNKNOWN!\n");
444 BUG();
445 break;
446 };
Bob Breuera54123e2006-03-23 22:36:19 -0800447
448 if (!ret) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700449 cpumask_set_cpu(cpu, &smp_commenced_mask);
Bob Breuera54123e2006-03-23 22:36:19 -0800450 while (!cpu_online(cpu))
451 mb();
452 }
453 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456void smp_bogo(struct seq_file *m)
457{
458 int i;
459
Andrew Morton394e3902006-03-23 03:01:05 -0800460 for_each_online_cpu(i) {
461 seq_printf(m,
462 "Cpu%dBogo\t: %lu.%02lu\n",
463 i,
464 cpu_data(i).udelay_val/(500000/HZ),
465 (cpu_data(i).udelay_val/(5000/HZ))%100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467}
468
469void smp_info(struct seq_file *m)
470{
471 int i;
472
473 seq_printf(m, "State:\n");
Andrew Morton394e3902006-03-23 03:01:05 -0800474 for_each_online_cpu(i)
475 seq_printf(m, "CPU%d\t\t: online\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}