VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / sparc / kernel / sun4d_smp.c
1 /* sun4d_smp.c: Sparc SS1000/SC2000 SMP support.
2  *
3  * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
4  *
5  * Based on sun4m's smp.c, which is:
6  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
7  */
8
9 #include <asm/head.h>
10
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/threads.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/mm.h>
21 #include <linux/swap.h>
22
23 #include <asm/ptrace.h>
24 #include <asm/atomic.h>
25
26 #include <asm/delay.h>
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/hardirq.h>
33 #include <asm/sbus.h>
34 #include <asm/sbi.h>
35 #include <asm/tlbflush.h>
36 #include <asm/cacheflush.h>
37 #include <asm/cpudata.h>
38
39 #define IRQ_CROSS_CALL          15
40
41 extern ctxd_t *srmmu_ctx_table_phys;
42
43 extern void calibrate_delay(void);
44
45 extern volatile int smp_processors_ready;
46 extern int smp_num_cpus;
47 static int smp_highest_cpu;
48 extern int smp_threads_ready;
49 extern volatile unsigned long cpu_callin_map[NR_CPUS];
50 extern struct cpuinfo_sparc cpu_data[NR_CPUS];
51 extern unsigned char boot_cpu_id;
52 extern int smp_activated;
53 extern volatile int __cpu_number_map[NR_CPUS];
54 extern volatile int __cpu_logical_map[NR_CPUS];
55 extern volatile unsigned long ipi_count;
56 extern volatile int smp_process_available;
57 extern volatile int smp_commenced;
58 extern int __smp4d_processor_id(void);
59
60 /* #define SMP_DEBUG */
61
62 #ifdef SMP_DEBUG
63 #define SMP_PRINTK(x)   printk x
64 #else
65 #define SMP_PRINTK(x)
66 #endif
67
68 static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
69 {
70         __asm__ __volatile__("swap [%1], %0\n\t" :
71                              "=&r" (val), "=&r" (ptr) :
72                              "0" (val), "1" (ptr));
73         return val;
74 }
75
76 static void smp_setup_percpu_timer(void);
77 extern void cpu_probe(void);
78 extern void sun4d_distribute_irqs(void);
79
80 void __init smp4d_callin(void)
81 {
82         int cpuid = hard_smp4d_processor_id();
83         extern spinlock_t sun4d_imsk_lock;
84         unsigned long flags;
85         
86         /* Show we are alive */
87         cpu_leds[cpuid] = 0x6;
88         show_leds(cpuid);
89
90         /* Enable level15 interrupt, disable level14 interrupt for now */
91         cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
92
93         local_flush_cache_all();
94         local_flush_tlb_all();
95
96         /*
97          * Unblock the master CPU _only_ when the scheduler state
98          * of all secondary CPUs will be up-to-date, so after
99          * the SMP initialization the master will be just allowed
100          * to call the scheduler code.
101          */
102         /* Get our local ticker going. */
103         smp_setup_percpu_timer();
104
105         calibrate_delay();
106         smp_store_cpu_info(cpuid);
107         local_flush_cache_all();
108         local_flush_tlb_all();
109
110         /* Allow master to continue. */
111         swap((unsigned long *)&cpu_callin_map[cpuid], 1);
112         local_flush_cache_all();
113         local_flush_tlb_all();
114         
115         cpu_probe();
116
117         while((unsigned long)current_set[cpuid] < PAGE_OFFSET)
118                 barrier();
119                 
120         while(current_set[cpuid]->cpu != cpuid)
121                 barrier();
122                 
123         /* Fix idle thread fields. */
124         __asm__ __volatile__("ld [%0], %%g6\n\t"
125                              "sta %%g6, [%%g0] %1\n\t"
126                              : : "r" (&current_set[cpuid]), "i" (ASI_M_VIKING_TMP2)
127                              : "memory" /* paranoid */);
128
129         cpu_leds[cpuid] = 0x9;
130         show_leds(cpuid);
131         
132         /* Attach to the address space of init_task. */
133         atomic_inc(&init_mm.mm_count);
134         current->active_mm = &init_mm;
135
136         local_flush_cache_all();
137         local_flush_tlb_all();
138         
139         local_irq_enable();     /* We don't allow PIL 14 yet */
140         
141         while(!smp_commenced)
142                 barrier();
143
144         spin_lock_irqsave(&sun4d_imsk_lock, flags);
145         cc_set_imsk(cc_get_imsk() & ~0x4000); /* Allow PIL 14 as well */
146         spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
147 }
148
149 extern int cpu_idle(void *unused);
150 extern void init_IRQ(void);
151 extern void cpu_panic(void);
152
153 /*
154  *      Cycle through the processors asking the PROM to start each one.
155  */
156  
157 extern struct linux_prom_registers smp_penguin_ctable;
158 extern unsigned long trapbase_cpu1[];
159 extern unsigned long trapbase_cpu2[];
160 extern unsigned long trapbase_cpu3[];
161
162 void __init smp4d_boot_cpus(void)
163 {
164         int cpucount = 0;
165         int i, mid;
166
167         printk("Entering SMP Mode...\n");
168         
169         if (boot_cpu_id)
170                 current_set[0] = NULL;
171
172         local_irq_enable();
173         cpus_clear(cpu_present_map);
174
175         /* XXX This whole thing has to go.  See sparc64. */
176         for (i = 0; !cpu_find_by_instance(i, NULL, &mid); i++)
177                 cpu_set(mid, cpu_present_map);
178         SMP_PRINTK(("cpu_present_map %08lx\n", cpus_addr(cpu_present_map)[0]));
179         for(i=0; i < NR_CPUS; i++)
180                 __cpu_number_map[i] = -1;
181         for(i=0; i < NR_CPUS; i++)
182                 __cpu_logical_map[i] = -1;
183         __cpu_number_map[boot_cpu_id] = 0;
184         __cpu_logical_map[0] = boot_cpu_id;
185         current_thread_info()->cpu = boot_cpu_id;
186         smp_store_cpu_info(boot_cpu_id);
187         smp_setup_percpu_timer();
188         local_flush_cache_all();
189         if (cpu_find_by_instance(1, NULL, NULL))
190                 return;  /* Not an MP box. */
191         SMP_PRINTK(("Iterating over CPUs\n"));
192         for(i = 0; i < NR_CPUS; i++) {
193                 if(i == boot_cpu_id)
194                         continue;
195
196                 if (cpu_isset(i, cpu_present_map)) {
197                         extern unsigned long sun4d_cpu_startup;
198                         unsigned long *entry = &sun4d_cpu_startup;
199                         struct task_struct *p;
200                         int timeout;
201                         int no;
202
203                         /* Cook up an idler for this guy. */
204                         kernel_thread(start_secondary, NULL, CLONE_IDLETASK);
205
206                         cpucount++;
207
208                         p = prev_task(&init_task);
209
210                         init_idle(p, i);
211
212                         current_set[i] = p->thread_info;
213
214                         unhash_process(p);
215
216                         for (no = 0; !cpu_find_by_instance(no, NULL, &mid)
217                                      && mid != i; no++) ;
218
219                         /*
220                          * Initialize the contexts table
221                          * Since the call to prom_startcpu() trashes the structure,
222                          * we need to re-initialize it for each cpu
223                          */
224                         smp_penguin_ctable.which_io = 0;
225                         smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
226                         smp_penguin_ctable.reg_size = 0;
227
228                         /* whirrr, whirrr, whirrrrrrrrr... */
229                         SMP_PRINTK(("Starting CPU %d at %p task %d node %08x\n", i, entry, cpucount, cpu_data(no).prom_node));
230                         local_flush_cache_all();
231                         prom_startcpu(cpu_data(no).prom_node,
232                                       &smp_penguin_ctable, 0, (char *)entry);
233                                       
234                         SMP_PRINTK(("prom_startcpu returned :)\n"));
235
236                         /* wheee... it's going... */
237                         for(timeout = 0; timeout < 10000; timeout++) {
238                                 if(cpu_callin_map[i])
239                                         break;
240                                 udelay(200);
241                         }
242                         
243                         if(cpu_callin_map[i]) {
244                                 /* Another "Red Snapper". */
245                                 __cpu_number_map[i] = cpucount;
246                                 __cpu_logical_map[cpucount] = i;
247                         } else {
248                                 cpucount--;
249                                 printk("Processor %d is stuck.\n", i);
250                         }
251                 }
252                 if(!(cpu_callin_map[i])) {
253                         cpu_clear(i, cpu_present_map);
254                         __cpu_number_map[i] = -1;
255                 }
256         }
257         local_flush_cache_all();
258         if(cpucount == 0) {
259                 printk("Error: only one Processor found.\n");
260                 cpu_present_map = cpumask_of_cpu(hard_smp4d_processor_id());
261         } else {
262                 unsigned long bogosum = 0;
263                 
264                 for(i = 0; i < NR_CPUS; i++) {
265                         if (cpu_isset(i, cpu_present_map)) {
266                                 bogosum += cpu_data(i).udelay_val;
267                                 smp_highest_cpu = i;
268                         }
269                 }
270                 SMP_PRINTK(("Total of %d Processors activated (%lu.%02lu BogoMIPS).\n", cpucount + 1, bogosum/(500000/HZ), (bogosum/(5000/HZ))%100));
271                 printk("Total of %d Processors activated (%lu.%02lu BogoMIPS).\n",
272                        cpucount + 1,
273                        bogosum/(500000/HZ),
274                        (bogosum/(5000/HZ))%100);
275                 smp_activated = 1;
276                 smp_num_cpus = cpucount + 1;
277         }
278
279         /* Free unneeded trap tables */
280         ClearPageReserved(virt_to_page(trapbase_cpu1));
281         set_page_count(virt_to_page(trapbase_cpu1), 1);
282         free_page((unsigned long)trapbase_cpu1);
283         totalram_pages++;
284         num_physpages++;
285
286         ClearPageReserved(virt_to_page(trapbase_cpu2));
287         set_page_count(virt_to_page(trapbase_cpu2), 1);
288         free_page((unsigned long)trapbase_cpu2);
289         totalram_pages++;
290         num_physpages++;
291
292         ClearPageReserved(virt_to_page(trapbase_cpu3));
293         set_page_count(virt_to_page(trapbase_cpu3), 1);
294         free_page((unsigned long)trapbase_cpu3);
295         totalram_pages++;
296         num_physpages++;
297
298         /* Ok, they are spinning and ready to go. */
299         smp_processors_ready = 1;
300         sun4d_distribute_irqs();
301 }
302
303 static struct smp_funcall {
304         smpfunc_t func;
305         unsigned long arg1;
306         unsigned long arg2;
307         unsigned long arg3;
308         unsigned long arg4;
309         unsigned long arg5;
310         unsigned char processors_in[NR_CPUS];  /* Set when ipi entered. */
311         unsigned char processors_out[NR_CPUS]; /* Set when ipi exited. */
312 } ccall_info __attribute__((aligned(8)));
313
314 static spinlock_t cross_call_lock = SPIN_LOCK_UNLOCKED;
315
316 /* Cross calls must be serialized, at least currently. */
317 void smp4d_cross_call(smpfunc_t func, unsigned long arg1, unsigned long arg2,
318                     unsigned long arg3, unsigned long arg4, unsigned long arg5)
319 {
320         if(smp_processors_ready) {
321                 register int high = smp_highest_cpu;
322                 unsigned long flags;
323
324                 spin_lock_irqsave(&cross_call_lock, flags);
325
326                 {
327                         /* If you make changes here, make sure gcc generates proper code... */
328                         register smpfunc_t f asm("i0") = func;
329                         register unsigned long a1 asm("i1") = arg1;
330                         register unsigned long a2 asm("i2") = arg2;
331                         register unsigned long a3 asm("i3") = arg3;
332                         register unsigned long a4 asm("i4") = arg4;
333                         register unsigned long a5 asm("i5") = arg5;
334
335                         __asm__ __volatile__(
336                                 "std %0, [%6]\n\t"
337                                 "std %2, [%6 + 8]\n\t"
338                                 "std %4, [%6 + 16]\n\t" : :
339                                 "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
340                                 "r" (&ccall_info.func));
341                 }
342
343                 /* Init receive/complete mapping, plus fire the IPI's off. */
344                 {
345                         cpumask_t mask;
346                         register int i;
347
348                         mask = cpumask_of_cpu(hard_smp4d_processor_id());
349                         cpus_andnot(mask, cpu_present_map, mask);
350                         for(i = 0; i <= high; i++) {
351                                 if (cpu_isset(i, mask)) {
352                                         ccall_info.processors_in[i] = 0;
353                                         ccall_info.processors_out[i] = 0;
354                                         sun4d_send_ipi(i, IRQ_CROSS_CALL);
355                                 }
356                         }
357                 }
358
359                 {
360                         register int i;
361
362                         i = 0;
363                         do {
364                                 while(!ccall_info.processors_in[i])
365                                         barrier();
366                         } while(++i <= high);
367
368                         i = 0;
369                         do {
370                                 while(!ccall_info.processors_out[i])
371                                         barrier();
372                         } while(++i <= high);
373                 }
374
375                 spin_unlock_irqrestore(&cross_call_lock, flags);
376         }
377 }
378
379 /* Running cross calls. */
380 void smp4d_cross_call_irq(void)
381 {
382         int i = hard_smp4d_processor_id();
383
384         ccall_info.processors_in[i] = 1;
385         ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
386                         ccall_info.arg4, ccall_info.arg5);
387         ccall_info.processors_out[i] = 1;
388 }
389
390 static int smp4d_stop_cpu_sender;
391
392 static void smp4d_stop_cpu(void)
393 {
394         int me = hard_smp4d_processor_id();
395         
396         if (me != smp4d_stop_cpu_sender)
397                 while(1) barrier();
398 }
399
400 /* Cross calls, in order to work efficiently and atomically do all
401  * the message passing work themselves, only stopcpu and reschedule
402  * messages come through here.
403  */
404 void smp4d_message_pass(int target, int msg, unsigned long data, int wait)
405 {
406         int me = hard_smp4d_processor_id();
407
408         SMP_PRINTK(("smp4d_message_pass %d %d %08lx %d\n", target, msg, data, wait));
409         if (msg == MSG_STOP_CPU && target == MSG_ALL_BUT_SELF) {
410                 unsigned long flags;
411                 static spinlock_t stop_cpu_lock = SPIN_LOCK_UNLOCKED;
412                 spin_lock_irqsave(&stop_cpu_lock, flags);
413                 smp4d_stop_cpu_sender = me;
414                 smp4d_cross_call((smpfunc_t)smp4d_stop_cpu, 0, 0, 0, 0, 0);
415                 spin_unlock_irqrestore(&stop_cpu_lock, flags);
416         }
417         printk("Yeeee, trying to send SMP msg(%d) to %d on cpu %d\n", msg, target, me);
418         panic("Bogon SMP message pass.");
419 }
420
421 extern void sparc_do_profile(unsigned long pc, unsigned long o7);
422
423 void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
424 {
425         int cpu = hard_smp4d_processor_id();
426         static int cpu_tick[NR_CPUS];
427         static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
428
429         bw_get_prof_limit(cpu); 
430         bw_clear_intr_mask(0, 1);       /* INTR_TABLE[0] & 1 is Profile IRQ */
431
432         cpu_tick[cpu]++;
433         if (!(cpu_tick[cpu] & 15)) {
434                 if (cpu_tick[cpu] == 0x60)
435                         cpu_tick[cpu] = 0;
436                 cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
437                 show_leds(cpu);
438         }
439
440         if(!user_mode(regs))
441                 sparc_do_profile(regs->pc, regs->u_regs[UREG_RETPC]);
442
443         if(!--prof_counter(cpu)) {
444                 int user = user_mode(regs);
445
446                 irq_enter();
447                 update_process_times(user);
448                 irq_exit();
449
450                 prof_counter(cpu) = prof_multiplier(cpu);
451         }
452 }
453
454 extern unsigned int lvl14_resolution;
455
456 static void __init smp_setup_percpu_timer(void)
457 {
458         int cpu = hard_smp4d_processor_id();
459
460         prof_counter(cpu) = prof_multiplier(cpu) = 1;
461         load_profile_irq(cpu, lvl14_resolution);
462 }
463
464 void __init smp4d_blackbox_id(unsigned *addr)
465 {
466         int rd = *addr & 0x3e000000;
467         
468         addr[0] = 0xc0800800 | rd;              /* lda [%g0] ASI_M_VIKING_TMP1, reg */
469         addr[1] = 0x01000000;                   /* nop */
470         addr[2] = 0x01000000;                   /* nop */
471 }
472
473 void __init smp4d_blackbox_current(unsigned *addr)
474 {
475         /* We have a nice Linux current register :) */
476         int rd = addr[1] & 0x3e000000;
477         
478         addr[0] = 0x10800006;                   /* b .+24 */
479         addr[1] = 0xc0800820 | rd;              /* lda [%g0] ASI_M_VIKING_TMP2, reg */
480 }
481
482 void __init sun4d_init_smp(void)
483 {
484         int i;
485         extern unsigned int patchme_store_new_current[];
486         extern unsigned int t_nmi[], linux_trap_ipi15_sun4d[], linux_trap_ipi15_sun4m[];
487
488         /* Store current into Linux current register :) */
489         __asm__ __volatile__("sta %%g6, [%%g0] %0" : : "i"(ASI_M_VIKING_TMP2));
490         
491         /* Patch switch_to */
492         patchme_store_new_current[0] = (patchme_store_new_current[0] & 0x3e000000) | 0xc0a00820;
493         
494         /* Patch ipi15 trap table */
495         t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_sun4d - linux_trap_ipi15_sun4m);
496         
497         /* And set btfixup... */
498         BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4d_blackbox_id);
499         BTFIXUPSET_BLACKBOX(load_current, smp4d_blackbox_current);
500         BTFIXUPSET_CALL(smp_cross_call, smp4d_cross_call, BTFIXUPCALL_NORM);
501         BTFIXUPSET_CALL(smp_message_pass, smp4d_message_pass, BTFIXUPCALL_NORM);
502         BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4d_processor_id, BTFIXUPCALL_NORM);
503         
504         for (i = 0; i < NR_CPUS; i++) {
505                 ccall_info.processors_in[i] = 1;
506                 ccall_info.processors_out[i] = 1;
507         }
508 }