blob: e311ade1b490adad0b3188f694ca96d345e82ac6 [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>
14#include <linux/smp_lock.h>
15#include <linux/interrupt.h>
16#include <linux/kernel_stat.h>
17#include <linux/init.h>
18#include <linux/spinlock.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/seq_file.h>
22#include <linux/cache.h>
23#include <linux/delay.h>
24
25#include <asm/ptrace.h>
26#include <asm/atomic.h>
27
28#include <asm/irq.h>
29#include <asm/page.h>
30#include <asm/pgalloc.h>
31#include <asm/pgtable.h>
32#include <asm/oplib.h>
33#include <asm/cacheflush.h>
34#include <asm/tlbflush.h>
35#include <asm/cpudata.h>
36
37volatile int smp_processors_ready = 0;
38int smp_num_cpus = 1;
39volatile unsigned long cpu_callin_map[NR_CPUS] __initdata = {0,};
40unsigned char boot_cpu_id = 0;
41unsigned char boot_cpu_id4 = 0; /* boot_cpu_id << 2 */
42int smp_activated = 0;
43volatile int __cpu_number_map[NR_CPUS];
44volatile int __cpu_logical_map[NR_CPUS];
45
46cpumask_t cpu_online_map = CPU_MASK_NONE;
47cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
Bob Breuera54123e2006-03-23 22:36:19 -080048cpumask_t smp_commenced_mask = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* The only guaranteed locking primitive available on all Sparc
51 * processors is 'ldstub [%reg + immediate], %dest_reg' which atomically
52 * places the current byte at the effective address into dest_reg and
53 * places 0xff there afterwards. Pretty lame locking primitive
54 * compared to the Alpha and the Intel no? Most Sparcs have 'swap'
55 * instruction which is much better...
56 */
57
58/* Used to make bitops atomic */
59unsigned char bitops_spinlock = 0;
60
Bob Breuer92d452f2006-06-20 00:36:10 -070061void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 int cpu_node;
64
65 cpu_data(id).udelay_val = loops_per_jiffy;
66
67 cpu_find_by_mid(id, &cpu_node);
68 cpu_data(id).clock_tick = prom_getintdefault(cpu_node,
69 "clock-frequency", 0);
70 cpu_data(id).prom_node = cpu_node;
71 cpu_data(id).mid = cpu_get_hwmid(cpu_node);
Krzysztof Helt650fb832006-06-10 22:03:43 -070072
73 /* this is required to tune the scheduler correctly */
74 /* is it possible to have CPUs with different cache sizes? */
75 if (id == boot_cpu_id) {
76 int cache_line,cache_nlines;
77 cache_line = 0x20;
78 cache_line = prom_getintdefault(cpu_node, "ecache-line-size", cache_line);
79 cache_nlines = 0x8000;
80 cache_nlines = prom_getintdefault(cpu_node, "ecache-nlines", cache_nlines);
81 max_cache_size = cache_line * cache_nlines;
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (cpu_data(id).mid < 0)
84 panic("No MID found for CPU%d at node 0x%08d", id, cpu_node);
85}
86
87void __init smp_cpus_done(unsigned int max_cpus)
88{
Bob Breuera54123e2006-03-23 22:36:19 -080089 extern void smp4m_smp_done(void);
Raymond Burns8b3c8482006-07-17 21:57:09 -070090 extern void smp4d_smp_done(void);
Bob Breuera54123e2006-03-23 22:36:19 -080091 unsigned long bogosum = 0;
92 int cpu, num;
93
94 for (cpu = 0, num = 0; cpu < NR_CPUS; cpu++)
95 if (cpu_online(cpu)) {
96 num++;
97 bogosum += cpu_data(cpu).udelay_val;
98 }
99
100 printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
101 num, bogosum/(500000/HZ),
102 (bogosum/(5000/HZ))%100);
103
Raymond Burns8b3c8482006-07-17 21:57:09 -0700104 switch(sparc_cpu_model) {
105 case sun4:
106 printk("SUN4\n");
107 BUG();
108 break;
109 case sun4c:
110 printk("SUN4C\n");
111 BUG();
112 break;
113 case sun4m:
114 smp4m_smp_done();
115 break;
116 case sun4d:
117 smp4d_smp_done();
118 break;
119 case sun4e:
120 printk("SUN4E\n");
121 BUG();
122 break;
123 case sun4u:
124 printk("SUN4U\n");
125 BUG();
126 break;
127 default:
128 printk("UNKNOWN!\n");
129 BUG();
130 break;
131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134void cpu_panic(void)
135{
136 printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
137 panic("SMP bolixed\n");
138}
139
140struct linux_prom_registers smp_penguin_ctable __initdata = { 0 };
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142void smp_send_reschedule(int cpu)
143{
144 /* See sparc64 */
145}
146
147void smp_send_stop(void)
148{
149}
150
151void smp_flush_cache_all(void)
152{
153 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_cache_all));
154 local_flush_cache_all();
155}
156
157void smp_flush_tlb_all(void)
158{
159 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_all));
160 local_flush_tlb_all();
161}
162
163void smp_flush_cache_mm(struct mm_struct *mm)
164{
165 if(mm->context != NO_CONTEXT) {
166 cpumask_t cpu_mask = mm->cpu_vm_mask;
167 cpu_clear(smp_processor_id(), cpu_mask);
168 if (!cpus_empty(cpu_mask))
169 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
170 local_flush_cache_mm(mm);
171 }
172}
173
174void smp_flush_tlb_mm(struct mm_struct *mm)
175{
176 if(mm->context != NO_CONTEXT) {
177 cpumask_t cpu_mask = mm->cpu_vm_mask;
178 cpu_clear(smp_processor_id(), cpu_mask);
179 if (!cpus_empty(cpu_mask)) {
180 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm);
181 if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
182 mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id());
183 }
184 local_flush_tlb_mm(mm);
185 }
186}
187
188void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
189 unsigned long end)
190{
191 struct mm_struct *mm = vma->vm_mm;
192
193 if (mm->context != NO_CONTEXT) {
194 cpumask_t cpu_mask = mm->cpu_vm_mask;
195 cpu_clear(smp_processor_id(), cpu_mask);
196 if (!cpus_empty(cpu_mask))
197 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
198 local_flush_cache_range(vma, start, end);
199 }
200}
201
202void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
203 unsigned long end)
204{
205 struct mm_struct *mm = vma->vm_mm;
206
207 if (mm->context != NO_CONTEXT) {
208 cpumask_t cpu_mask = mm->cpu_vm_mask;
209 cpu_clear(smp_processor_id(), cpu_mask);
210 if (!cpus_empty(cpu_mask))
211 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
212 local_flush_tlb_range(vma, start, end);
213 }
214}
215
216void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
217{
218 struct mm_struct *mm = vma->vm_mm;
219
220 if(mm->context != NO_CONTEXT) {
221 cpumask_t cpu_mask = mm->cpu_vm_mask;
222 cpu_clear(smp_processor_id(), cpu_mask);
223 if (!cpus_empty(cpu_mask))
224 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
225 local_flush_cache_page(vma, page);
226 }
227}
228
229void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
230{
231 struct mm_struct *mm = vma->vm_mm;
232
233 if(mm->context != NO_CONTEXT) {
234 cpumask_t cpu_mask = mm->cpu_vm_mask;
235 cpu_clear(smp_processor_id(), cpu_mask);
236 if (!cpus_empty(cpu_mask))
237 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
238 local_flush_tlb_page(vma, page);
239 }
240}
241
242void smp_reschedule_irq(void)
243{
244 set_need_resched();
245}
246
247void smp_flush_page_to_ram(unsigned long page)
248{
249 /* Current theory is that those who call this are the one's
250 * who have just dirtied their cache with the pages contents
251 * in kernel space, therefore we only run this on local cpu.
252 *
253 * XXX This experiment failed, research further... -DaveM
254 */
255#if 1
256 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page);
257#endif
258 local_flush_page_to_ram(page);
259}
260
261void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
262{
263 cpumask_t cpu_mask = mm->cpu_vm_mask;
264 cpu_clear(smp_processor_id(), cpu_mask);
265 if (!cpus_empty(cpu_mask))
266 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
267 local_flush_sig_insns(mm, insn_addr);
268}
269
270extern unsigned int lvl14_resolution;
271
272/* /proc/profile writes can call this, don't __init it please. */
273static DEFINE_SPINLOCK(prof_setup_lock);
274
275int setup_profiling_timer(unsigned int multiplier)
276{
277 int i;
278 unsigned long flags;
279
280 /* Prevent level14 ticker IRQ flooding. */
281 if((!multiplier) || (lvl14_resolution / multiplier) < 500)
282 return -EINVAL;
283
284 spin_lock_irqsave(&prof_setup_lock, flags);
KAMEZAWA Hiroyukifff8efe2006-04-10 22:52:51 -0700285 for_each_possible_cpu(i) {
Andrew Morton394e3902006-03-23 03:01:05 -0800286 load_profile_irq(i, lvl14_resolution / multiplier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 prof_multiplier(i) = multiplier;
288 }
289 spin_unlock_irqrestore(&prof_setup_lock, flags);
290
291 return 0;
292}
293
Bob Breuera54123e2006-03-23 22:36:19 -0800294void __init smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Bob Breuera54123e2006-03-23 22:36:19 -0800296 extern void smp4m_boot_cpus(void);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700297 extern void smp4d_boot_cpus(void);
Bob Breuer7202fb42006-06-20 00:30:31 -0700298 int i, cpuid, extra;
Bob Breuera54123e2006-03-23 22:36:19 -0800299
Bob Breuera54123e2006-03-23 22:36:19 -0800300 printk("Entering SMP Mode...\n");
301
Bob Breuera54123e2006-03-23 22:36:19 -0800302 extra = 0;
303 for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
Bob Breuer7202fb42006-06-20 00:30:31 -0700304 if (cpuid >= NR_CPUS)
Bob Breuera54123e2006-03-23 22:36:19 -0800305 extra++;
306 }
Bob Breuer7202fb42006-06-20 00:30:31 -0700307 /* i = number of cpus */
308 if (extra && max_cpus > i - extra)
Bob Breuera54123e2006-03-23 22:36:19 -0800309 printk("Warning: NR_CPUS is too low to start all cpus\n");
310
311 smp_store_cpu_info(boot_cpu_id);
312
Raymond Burns8b3c8482006-07-17 21:57:09 -0700313 switch(sparc_cpu_model) {
314 case sun4:
315 printk("SUN4\n");
316 BUG();
317 break;
318 case sun4c:
319 printk("SUN4C\n");
320 BUG();
321 break;
322 case sun4m:
323 smp4m_boot_cpus();
324 break;
325 case sun4d:
326 smp4d_boot_cpus();
327 break;
328 case sun4e:
329 printk("SUN4E\n");
330 BUG();
331 break;
332 case sun4u:
333 printk("SUN4U\n");
334 BUG();
335 break;
336 default:
337 printk("UNKNOWN!\n");
338 BUG();
339 break;
340 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Bob Breuer7202fb42006-06-20 00:30:31 -0700343/* Set this up early so that things like the scheduler can init
344 * properly. We use the same cpu mask for both the present and
345 * possible cpu map.
346 */
347void __init smp_setup_cpu_possible_map(void)
348{
349 int instance, mid;
350
351 instance = 0;
352 while (!cpu_find_by_instance(instance, NULL, &mid)) {
353 if (mid < NR_CPUS) {
354 cpu_set(mid, phys_cpu_present_map);
355 cpu_set(mid, cpu_present_map);
356 }
357 instance++;
358 }
359}
360
Bob Breuer92d452f2006-06-20 00:36:10 -0700361void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Bob Breuera54123e2006-03-23 22:36:19 -0800363 int cpuid = hard_smp_processor_id();
364
365 if (cpuid >= NR_CPUS) {
366 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
367 prom_halt();
368 }
369 if (cpuid != 0)
370 printk("boot cpu id != 0, this could work but is untested\n");
371
372 current_thread_info()->cpu = cpuid;
373 cpu_set(cpuid, cpu_online_map);
374 cpu_set(cpuid, phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Bob Breuer92d452f2006-06-20 00:36:10 -0700377int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Bob Breuera54123e2006-03-23 22:36:19 -0800379 extern int smp4m_boot_one_cpu(int);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700380 extern int smp4d_boot_one_cpu(int);
381 int ret=0;
Bob Breuera54123e2006-03-23 22:36:19 -0800382
Raymond Burns8b3c8482006-07-17 21:57:09 -0700383 switch(sparc_cpu_model) {
384 case sun4:
385 printk("SUN4\n");
386 BUG();
387 break;
388 case sun4c:
389 printk("SUN4C\n");
390 BUG();
391 break;
392 case sun4m:
393 ret = smp4m_boot_one_cpu(cpu);
394 break;
395 case sun4d:
396 ret = smp4d_boot_one_cpu(cpu);
397 break;
398 case sun4e:
399 printk("SUN4E\n");
400 BUG();
401 break;
402 case sun4u:
403 printk("SUN4U\n");
404 BUG();
405 break;
406 default:
407 printk("UNKNOWN!\n");
408 BUG();
409 break;
410 };
Bob Breuera54123e2006-03-23 22:36:19 -0800411
412 if (!ret) {
413 cpu_set(cpu, smp_commenced_mask);
414 while (!cpu_online(cpu))
415 mb();
416 }
417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420void smp_bogo(struct seq_file *m)
421{
422 int i;
423
Andrew Morton394e3902006-03-23 03:01:05 -0800424 for_each_online_cpu(i) {
425 seq_printf(m,
426 "Cpu%dBogo\t: %lu.%02lu\n",
427 i,
428 cpu_data(i).udelay_val/(500000/HZ),
429 (cpu_data(i).udelay_val/(5000/HZ))%100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431}
432
433void smp_info(struct seq_file *m)
434{
435 int i;
436
437 seq_printf(m, "State:\n");
Andrew Morton394e3902006-03-23 03:01:05 -0800438 for_each_online_cpu(i)
439 seq_printf(m, "CPU%d\t\t: online\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}