blob: 2710602281de1318c24ac4ca2c5be7247b259b3b [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) {
Rusty Russell81f1adf02009-03-16 14:40:39 +1030193 cpumask_t cpu_mask = *mm_cpumask(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 cpu_clear(smp_processor_id(), cpu_mask);
195 if (!cpus_empty(cpu_mask))
196 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
197 local_flush_cache_mm(mm);
198 }
199}
200
201void smp_flush_tlb_mm(struct mm_struct *mm)
202{
203 if(mm->context != NO_CONTEXT) {
Rusty Russell81f1adf02009-03-16 14:40:39 +1030204 cpumask_t cpu_mask = *mm_cpumask(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 cpu_clear(smp_processor_id(), cpu_mask);
206 if (!cpus_empty(cpu_mask)) {
207 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm);
208 if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
Rusty Russell81f1adf02009-03-16 14:40:39 +1030209 cpumask_copy(mm_cpumask(mm),
210 cpumask_of(smp_processor_id()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 local_flush_tlb_mm(mm);
213 }
214}
215
216void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
217 unsigned long end)
218{
219 struct mm_struct *mm = vma->vm_mm;
220
221 if (mm->context != NO_CONTEXT) {
Rusty Russell81f1adf02009-03-16 14:40:39 +1030222 cpumask_t cpu_mask = *mm_cpumask(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 cpu_clear(smp_processor_id(), cpu_mask);
224 if (!cpus_empty(cpu_mask))
225 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
226 local_flush_cache_range(vma, start, end);
227 }
228}
229
230void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
231 unsigned long end)
232{
233 struct mm_struct *mm = vma->vm_mm;
234
235 if (mm->context != NO_CONTEXT) {
Rusty Russell81f1adf02009-03-16 14:40:39 +1030236 cpumask_t cpu_mask = *mm_cpumask(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 cpu_clear(smp_processor_id(), cpu_mask);
238 if (!cpus_empty(cpu_mask))
239 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
240 local_flush_tlb_range(vma, start, end);
241 }
242}
243
244void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
245{
246 struct mm_struct *mm = vma->vm_mm;
247
248 if(mm->context != NO_CONTEXT) {
Rusty Russell81f1adf02009-03-16 14:40:39 +1030249 cpumask_t cpu_mask = *mm_cpumask(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cpu_clear(smp_processor_id(), cpu_mask);
251 if (!cpus_empty(cpu_mask))
252 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
253 local_flush_cache_page(vma, page);
254 }
255}
256
257void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
258{
259 struct mm_struct *mm = vma->vm_mm;
260
261 if(mm->context != NO_CONTEXT) {
Rusty Russell81f1adf02009-03-16 14:40:39 +1030262 cpumask_t cpu_mask = *mm_cpumask(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 cpu_clear(smp_processor_id(), cpu_mask);
264 if (!cpus_empty(cpu_mask))
265 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
266 local_flush_tlb_page(vma, page);
267 }
268}
269
270void smp_reschedule_irq(void)
271{
272 set_need_resched();
273}
274
275void smp_flush_page_to_ram(unsigned long page)
276{
277 /* Current theory is that those who call this are the one's
278 * who have just dirtied their cache with the pages contents
279 * in kernel space, therefore we only run this on local cpu.
280 *
281 * XXX This experiment failed, research further... -DaveM
282 */
283#if 1
284 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page);
285#endif
286 local_flush_page_to_ram(page);
287}
288
289void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
290{
Rusty Russell81f1adf02009-03-16 14:40:39 +1030291 cpumask_t cpu_mask = *mm_cpumask(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 cpu_clear(smp_processor_id(), cpu_mask);
293 if (!cpus_empty(cpu_mask))
294 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
295 local_flush_sig_insns(mm, insn_addr);
296}
297
298extern unsigned int lvl14_resolution;
299
300/* /proc/profile writes can call this, don't __init it please. */
301static DEFINE_SPINLOCK(prof_setup_lock);
302
303int setup_profiling_timer(unsigned int multiplier)
304{
305 int i;
306 unsigned long flags;
307
308 /* Prevent level14 ticker IRQ flooding. */
309 if((!multiplier) || (lvl14_resolution / multiplier) < 500)
310 return -EINVAL;
311
312 spin_lock_irqsave(&prof_setup_lock, flags);
KAMEZAWA Hiroyukifff8efe2006-04-10 22:52:51 -0700313 for_each_possible_cpu(i) {
Andrew Morton394e3902006-03-23 03:01:05 -0800314 load_profile_irq(i, lvl14_resolution / multiplier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 prof_multiplier(i) = multiplier;
316 }
317 spin_unlock_irqrestore(&prof_setup_lock, flags);
318
319 return 0;
320}
321
Bob Breuera54123e2006-03-23 22:36:19 -0800322void __init smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Al Virob4cff842007-02-01 13:52:33 +0000324 extern void __init smp4m_boot_cpus(void);
325 extern void __init smp4d_boot_cpus(void);
Bob Breuer7202fb42006-06-20 00:30:31 -0700326 int i, cpuid, extra;
Bob Breuera54123e2006-03-23 22:36:19 -0800327
Bob Breuera54123e2006-03-23 22:36:19 -0800328 printk("Entering SMP Mode...\n");
329
Bob Breuera54123e2006-03-23 22:36:19 -0800330 extra = 0;
331 for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
Bob Breuer7202fb42006-06-20 00:30:31 -0700332 if (cpuid >= NR_CPUS)
Bob Breuera54123e2006-03-23 22:36:19 -0800333 extra++;
334 }
Bob Breuer7202fb42006-06-20 00:30:31 -0700335 /* i = number of cpus */
336 if (extra && max_cpus > i - extra)
Bob Breuera54123e2006-03-23 22:36:19 -0800337 printk("Warning: NR_CPUS is too low to start all cpus\n");
338
339 smp_store_cpu_info(boot_cpu_id);
340
Raymond Burns8b3c8482006-07-17 21:57:09 -0700341 switch(sparc_cpu_model) {
342 case sun4:
343 printk("SUN4\n");
344 BUG();
345 break;
346 case sun4c:
347 printk("SUN4C\n");
348 BUG();
349 break;
350 case sun4m:
351 smp4m_boot_cpus();
352 break;
353 case sun4d:
354 smp4d_boot_cpus();
355 break;
Konrad Eisele84017072009-08-31 22:08:13 +0000356 case sparc_leon:
357 leon_boot_cpus();
358 break;
Raymond Burns8b3c8482006-07-17 21:57:09 -0700359 case sun4e:
360 printk("SUN4E\n");
361 BUG();
362 break;
363 case sun4u:
364 printk("SUN4U\n");
365 BUG();
366 break;
367 default:
368 printk("UNKNOWN!\n");
369 BUG();
370 break;
371 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Bob Breuer7202fb42006-06-20 00:30:31 -0700374/* Set this up early so that things like the scheduler can init
375 * properly. We use the same cpu mask for both the present and
376 * possible cpu map.
377 */
378void __init smp_setup_cpu_possible_map(void)
379{
380 int instance, mid;
381
382 instance = 0;
383 while (!cpu_find_by_instance(instance, NULL, &mid)) {
384 if (mid < NR_CPUS) {
Rusty Russellfe739712009-03-16 14:40:22 +1030385 set_cpu_possible(mid, true);
386 set_cpu_present(mid, true);
Bob Breuer7202fb42006-06-20 00:30:31 -0700387 }
388 instance++;
389 }
390}
391
Bob Breuer92d452f2006-06-20 00:36:10 -0700392void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Bob Breuera54123e2006-03-23 22:36:19 -0800394 int cpuid = hard_smp_processor_id();
395
396 if (cpuid >= NR_CPUS) {
397 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
398 prom_halt();
399 }
400 if (cpuid != 0)
401 printk("boot cpu id != 0, this could work but is untested\n");
402
403 current_thread_info()->cpu = cpuid;
Rusty Russellfe739712009-03-16 14:40:22 +1030404 set_cpu_online(cpuid, true);
405 set_cpu_possible(cpuid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Bob Breuer92d452f2006-06-20 00:36:10 -0700408int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Al Virob4cff842007-02-01 13:52:33 +0000410 extern int __cpuinit smp4m_boot_one_cpu(int);
411 extern int __cpuinit smp4d_boot_one_cpu(int);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700412 int ret=0;
Bob Breuera54123e2006-03-23 22:36:19 -0800413
Raymond Burns8b3c8482006-07-17 21:57:09 -0700414 switch(sparc_cpu_model) {
415 case sun4:
416 printk("SUN4\n");
417 BUG();
418 break;
419 case sun4c:
420 printk("SUN4C\n");
421 BUG();
422 break;
423 case sun4m:
424 ret = smp4m_boot_one_cpu(cpu);
425 break;
426 case sun4d:
427 ret = smp4d_boot_one_cpu(cpu);
428 break;
Konrad Eisele84017072009-08-31 22:08:13 +0000429 case sparc_leon:
430 ret = leon_boot_one_cpu(cpu);
431 break;
Raymond Burns8b3c8482006-07-17 21:57:09 -0700432 case sun4e:
433 printk("SUN4E\n");
434 BUG();
435 break;
436 case sun4u:
437 printk("SUN4U\n");
438 BUG();
439 break;
440 default:
441 printk("UNKNOWN!\n");
442 BUG();
443 break;
444 };
Bob Breuera54123e2006-03-23 22:36:19 -0800445
446 if (!ret) {
447 cpu_set(cpu, smp_commenced_mask);
448 while (!cpu_online(cpu))
449 mb();
450 }
451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454void smp_bogo(struct seq_file *m)
455{
456 int i;
457
Andrew Morton394e3902006-03-23 03:01:05 -0800458 for_each_online_cpu(i) {
459 seq_printf(m,
460 "Cpu%dBogo\t: %lu.%02lu\n",
461 i,
462 cpu_data(i).udelay_val/(500000/HZ),
463 (cpu_data(i).udelay_val/(5000/HZ))%100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465}
466
467void smp_info(struct seq_file *m)
468{
469 int i;
470
471 seq_printf(m, "State:\n");
Andrew Morton394e3902006-03-23 03:01:05 -0800472 for_each_online_cpu(i)
473 seq_printf(m, "CPU%d\t\t: online\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}