blob: 4fea3ac7bff08d1b13cd4e54f2d2be6b08198d1a [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>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036int smp_num_cpus = 1;
37volatile unsigned long cpu_callin_map[NR_CPUS] __initdata = {0,};
38unsigned char boot_cpu_id = 0;
39unsigned char boot_cpu_id4 = 0; /* boot_cpu_id << 2 */
40int smp_activated = 0;
41volatile int __cpu_number_map[NR_CPUS];
42volatile int __cpu_logical_map[NR_CPUS];
43
44cpumask_t cpu_online_map = CPU_MASK_NONE;
45cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
Bob Breuera54123e2006-03-23 22:36:19 -080046cpumask_t smp_commenced_mask = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* The only guaranteed locking primitive available on all Sparc
49 * processors is 'ldstub [%reg + immediate], %dest_reg' which atomically
50 * places the current byte at the effective address into dest_reg and
51 * places 0xff there afterwards. Pretty lame locking primitive
52 * compared to the Alpha and the Intel no? Most Sparcs have 'swap'
53 * instruction which is much better...
54 */
55
56/* Used to make bitops atomic */
57unsigned char bitops_spinlock = 0;
58
Bob Breuer92d452f2006-06-20 00:36:10 -070059void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 int cpu_node;
62
63 cpu_data(id).udelay_val = loops_per_jiffy;
64
65 cpu_find_by_mid(id, &cpu_node);
66 cpu_data(id).clock_tick = prom_getintdefault(cpu_node,
67 "clock-frequency", 0);
68 cpu_data(id).prom_node = cpu_node;
69 cpu_data(id).mid = cpu_get_hwmid(cpu_node);
Krzysztof Helt650fb832006-06-10 22:03:43 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (cpu_data(id).mid < 0)
72 panic("No MID found for CPU%d at node 0x%08d", id, cpu_node);
73}
74
75void __init smp_cpus_done(unsigned int max_cpus)
76{
Bob Breuera54123e2006-03-23 22:36:19 -080077 extern void smp4m_smp_done(void);
Raymond Burns8b3c8482006-07-17 21:57:09 -070078 extern void smp4d_smp_done(void);
Bob Breuera54123e2006-03-23 22:36:19 -080079 unsigned long bogosum = 0;
80 int cpu, num;
81
82 for (cpu = 0, num = 0; cpu < NR_CPUS; cpu++)
83 if (cpu_online(cpu)) {
84 num++;
85 bogosum += cpu_data(cpu).udelay_val;
86 }
87
88 printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
89 num, bogosum/(500000/HZ),
90 (bogosum/(5000/HZ))%100);
91
Raymond Burns8b3c8482006-07-17 21:57:09 -070092 switch(sparc_cpu_model) {
93 case sun4:
94 printk("SUN4\n");
95 BUG();
96 break;
97 case sun4c:
98 printk("SUN4C\n");
99 BUG();
100 break;
101 case sun4m:
102 smp4m_smp_done();
103 break;
104 case sun4d:
105 smp4d_smp_done();
106 break;
107 case sun4e:
108 printk("SUN4E\n");
109 BUG();
110 break;
111 case sun4u:
112 printk("SUN4U\n");
113 BUG();
114 break;
115 default:
116 printk("UNKNOWN!\n");
117 BUG();
118 break;
119 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122void cpu_panic(void)
123{
124 printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
125 panic("SMP bolixed\n");
126}
127
128struct linux_prom_registers smp_penguin_ctable __initdata = { 0 };
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130void smp_send_reschedule(int cpu)
131{
132 /* See sparc64 */
133}
134
135void smp_send_stop(void)
136{
137}
138
139void smp_flush_cache_all(void)
140{
141 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_cache_all));
142 local_flush_cache_all();
143}
144
145void smp_flush_tlb_all(void)
146{
147 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_all));
148 local_flush_tlb_all();
149}
150
151void smp_flush_cache_mm(struct mm_struct *mm)
152{
153 if(mm->context != NO_CONTEXT) {
154 cpumask_t cpu_mask = mm->cpu_vm_mask;
155 cpu_clear(smp_processor_id(), cpu_mask);
156 if (!cpus_empty(cpu_mask))
157 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
158 local_flush_cache_mm(mm);
159 }
160}
161
162void smp_flush_tlb_mm(struct mm_struct *mm)
163{
164 if(mm->context != NO_CONTEXT) {
165 cpumask_t cpu_mask = mm->cpu_vm_mask;
166 cpu_clear(smp_processor_id(), cpu_mask);
167 if (!cpus_empty(cpu_mask)) {
168 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm);
169 if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
170 mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id());
171 }
172 local_flush_tlb_mm(mm);
173 }
174}
175
176void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
177 unsigned long end)
178{
179 struct mm_struct *mm = vma->vm_mm;
180
181 if (mm->context != NO_CONTEXT) {
182 cpumask_t cpu_mask = mm->cpu_vm_mask;
183 cpu_clear(smp_processor_id(), cpu_mask);
184 if (!cpus_empty(cpu_mask))
185 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
186 local_flush_cache_range(vma, start, end);
187 }
188}
189
190void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
191 unsigned long end)
192{
193 struct mm_struct *mm = vma->vm_mm;
194
195 if (mm->context != NO_CONTEXT) {
196 cpumask_t cpu_mask = mm->cpu_vm_mask;
197 cpu_clear(smp_processor_id(), cpu_mask);
198 if (!cpus_empty(cpu_mask))
199 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
200 local_flush_tlb_range(vma, start, end);
201 }
202}
203
204void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
205{
206 struct mm_struct *mm = vma->vm_mm;
207
208 if(mm->context != NO_CONTEXT) {
209 cpumask_t cpu_mask = mm->cpu_vm_mask;
210 cpu_clear(smp_processor_id(), cpu_mask);
211 if (!cpus_empty(cpu_mask))
212 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
213 local_flush_cache_page(vma, page);
214 }
215}
216
217void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
218{
219 struct mm_struct *mm = vma->vm_mm;
220
221 if(mm->context != NO_CONTEXT) {
222 cpumask_t cpu_mask = mm->cpu_vm_mask;
223 cpu_clear(smp_processor_id(), cpu_mask);
224 if (!cpus_empty(cpu_mask))
225 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
226 local_flush_tlb_page(vma, page);
227 }
228}
229
230void smp_reschedule_irq(void)
231{
232 set_need_resched();
233}
234
235void smp_flush_page_to_ram(unsigned long page)
236{
237 /* Current theory is that those who call this are the one's
238 * who have just dirtied their cache with the pages contents
239 * in kernel space, therefore we only run this on local cpu.
240 *
241 * XXX This experiment failed, research further... -DaveM
242 */
243#if 1
244 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page);
245#endif
246 local_flush_page_to_ram(page);
247}
248
249void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
250{
251 cpumask_t cpu_mask = mm->cpu_vm_mask;
252 cpu_clear(smp_processor_id(), cpu_mask);
253 if (!cpus_empty(cpu_mask))
254 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
255 local_flush_sig_insns(mm, insn_addr);
256}
257
258extern unsigned int lvl14_resolution;
259
260/* /proc/profile writes can call this, don't __init it please. */
261static DEFINE_SPINLOCK(prof_setup_lock);
262
263int setup_profiling_timer(unsigned int multiplier)
264{
265 int i;
266 unsigned long flags;
267
268 /* Prevent level14 ticker IRQ flooding. */
269 if((!multiplier) || (lvl14_resolution / multiplier) < 500)
270 return -EINVAL;
271
272 spin_lock_irqsave(&prof_setup_lock, flags);
KAMEZAWA Hiroyukifff8efe2006-04-10 22:52:51 -0700273 for_each_possible_cpu(i) {
Andrew Morton394e3902006-03-23 03:01:05 -0800274 load_profile_irq(i, lvl14_resolution / multiplier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 prof_multiplier(i) = multiplier;
276 }
277 spin_unlock_irqrestore(&prof_setup_lock, flags);
278
279 return 0;
280}
281
Bob Breuera54123e2006-03-23 22:36:19 -0800282void __init smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Al Virob4cff842007-02-01 13:52:33 +0000284 extern void __init smp4m_boot_cpus(void);
285 extern void __init smp4d_boot_cpus(void);
Bob Breuer7202fb42006-06-20 00:30:31 -0700286 int i, cpuid, extra;
Bob Breuera54123e2006-03-23 22:36:19 -0800287
Bob Breuera54123e2006-03-23 22:36:19 -0800288 printk("Entering SMP Mode...\n");
289
Bob Breuera54123e2006-03-23 22:36:19 -0800290 extra = 0;
291 for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
Bob Breuer7202fb42006-06-20 00:30:31 -0700292 if (cpuid >= NR_CPUS)
Bob Breuera54123e2006-03-23 22:36:19 -0800293 extra++;
294 }
Bob Breuer7202fb42006-06-20 00:30:31 -0700295 /* i = number of cpus */
296 if (extra && max_cpus > i - extra)
Bob Breuera54123e2006-03-23 22:36:19 -0800297 printk("Warning: NR_CPUS is too low to start all cpus\n");
298
299 smp_store_cpu_info(boot_cpu_id);
300
Raymond Burns8b3c8482006-07-17 21:57:09 -0700301 switch(sparc_cpu_model) {
302 case sun4:
303 printk("SUN4\n");
304 BUG();
305 break;
306 case sun4c:
307 printk("SUN4C\n");
308 BUG();
309 break;
310 case sun4m:
311 smp4m_boot_cpus();
312 break;
313 case sun4d:
314 smp4d_boot_cpus();
315 break;
316 case sun4e:
317 printk("SUN4E\n");
318 BUG();
319 break;
320 case sun4u:
321 printk("SUN4U\n");
322 BUG();
323 break;
324 default:
325 printk("UNKNOWN!\n");
326 BUG();
327 break;
328 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Bob Breuer7202fb42006-06-20 00:30:31 -0700331/* Set this up early so that things like the scheduler can init
332 * properly. We use the same cpu mask for both the present and
333 * possible cpu map.
334 */
335void __init smp_setup_cpu_possible_map(void)
336{
337 int instance, mid;
338
339 instance = 0;
340 while (!cpu_find_by_instance(instance, NULL, &mid)) {
341 if (mid < NR_CPUS) {
342 cpu_set(mid, phys_cpu_present_map);
343 cpu_set(mid, cpu_present_map);
344 }
345 instance++;
346 }
347}
348
Bob Breuer92d452f2006-06-20 00:36:10 -0700349void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Bob Breuera54123e2006-03-23 22:36:19 -0800351 int cpuid = hard_smp_processor_id();
352
353 if (cpuid >= NR_CPUS) {
354 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
355 prom_halt();
356 }
357 if (cpuid != 0)
358 printk("boot cpu id != 0, this could work but is untested\n");
359
360 current_thread_info()->cpu = cpuid;
361 cpu_set(cpuid, cpu_online_map);
362 cpu_set(cpuid, phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Bob Breuer92d452f2006-06-20 00:36:10 -0700365int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Al Virob4cff842007-02-01 13:52:33 +0000367 extern int __cpuinit smp4m_boot_one_cpu(int);
368 extern int __cpuinit smp4d_boot_one_cpu(int);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700369 int ret=0;
Bob Breuera54123e2006-03-23 22:36:19 -0800370
Raymond Burns8b3c8482006-07-17 21:57:09 -0700371 switch(sparc_cpu_model) {
372 case sun4:
373 printk("SUN4\n");
374 BUG();
375 break;
376 case sun4c:
377 printk("SUN4C\n");
378 BUG();
379 break;
380 case sun4m:
381 ret = smp4m_boot_one_cpu(cpu);
382 break;
383 case sun4d:
384 ret = smp4d_boot_one_cpu(cpu);
385 break;
386 case sun4e:
387 printk("SUN4E\n");
388 BUG();
389 break;
390 case sun4u:
391 printk("SUN4U\n");
392 BUG();
393 break;
394 default:
395 printk("UNKNOWN!\n");
396 BUG();
397 break;
398 };
Bob Breuera54123e2006-03-23 22:36:19 -0800399
400 if (!ret) {
401 cpu_set(cpu, smp_commenced_mask);
402 while (!cpu_online(cpu))
403 mb();
404 }
405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408void smp_bogo(struct seq_file *m)
409{
410 int i;
411
Andrew Morton394e3902006-03-23 03:01:05 -0800412 for_each_online_cpu(i) {
413 seq_printf(m,
414 "Cpu%dBogo\t: %lu.%02lu\n",
415 i,
416 cpu_data(i).udelay_val/(500000/HZ),
417 (cpu_data(i).udelay_val/(5000/HZ))%100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419}
420
421void smp_info(struct seq_file *m)
422{
423 int i;
424
425 seq_printf(m, "State:\n");
Andrew Morton394e3902006-03-23 03:01:05 -0800426 for_each_online_cpu(i)
427 seq_printf(m, "CPU%d\t\t: online\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}