blob: 49a08b487ca7a3fc5896c6a5c83f3d8d6e737918 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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/swap.h>
21#include <linux/profile.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -080022#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/ptrace.h>
25#include <asm/atomic.h>
Al Viro0d844382006-10-08 14:30:44 +010026#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/irq.h>
29#include <asm/page.h>
30#include <asm/pgalloc.h>
31#include <asm/pgtable.h>
32#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/sbi.h>
34#include <asm/tlbflush.h>
35#include <asm/cacheflush.h>
36#include <asm/cpudata.h>
37
Al Viro32231a62007-07-21 19:18:57 -070038#include "irq.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define IRQ_CROSS_CALL 15
40
41extern ctxd_t *srmmu_ctx_table_phys;
42
Krzysztof Helt5fec8112006-08-25 16:22:21 -070043static volatile int smp_processors_ready = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int smp_highest_cpu;
45extern volatile unsigned long cpu_callin_map[NR_CPUS];
Bob Breuera54123e2006-03-23 22:36:19 -080046extern cpuinfo_sparc cpu_data[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047extern unsigned char boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048extern volatile int smp_process_available;
Bob Breuera54123e2006-03-23 22:36:19 -080049
50extern cpumask_t smp_commenced_mask;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052extern int __smp4d_processor_id(void);
53
54/* #define SMP_DEBUG */
55
56#ifdef SMP_DEBUG
57#define SMP_PRINTK(x) printk x
58#else
59#define SMP_PRINTK(x)
60#endif
61
62static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
63{
64 __asm__ __volatile__("swap [%1], %0\n\t" :
65 "=&r" (val), "=&r" (ptr) :
66 "0" (val), "1" (ptr));
67 return val;
68}
69
70static void smp_setup_percpu_timer(void);
71extern void cpu_probe(void);
72extern void sun4d_distribute_irqs(void);
73
David S. Miller7b1af322008-09-02 01:17:41 -070074static unsigned char cpu_leds[32];
75
76static inline void show_leds(int cpuid)
77{
78 cpuid &= 0x1e;
79 __asm__ __volatile__ ("stba %0, [%1] %2" : :
80 "r" ((cpu_leds[cpuid] << 4) | cpu_leds[cpuid+1]),
81 "r" (ECSR_BASE(cpuid) | BB_LEDS),
82 "i" (ASI_M_CTL));
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085void __init smp4d_callin(void)
86{
87 int cpuid = hard_smp4d_processor_id();
88 extern spinlock_t sun4d_imsk_lock;
89 unsigned long flags;
90
91 /* Show we are alive */
92 cpu_leds[cpuid] = 0x6;
93 show_leds(cpuid);
94
95 /* Enable level15 interrupt, disable level14 interrupt for now */
96 cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
97
98 local_flush_cache_all();
99 local_flush_tlb_all();
100
101 /*
102 * Unblock the master CPU _only_ when the scheduler state
103 * of all secondary CPUs will be up-to-date, so after
104 * the SMP initialization the master will be just allowed
105 * to call the scheduler code.
106 */
107 /* Get our local ticker going. */
108 smp_setup_percpu_timer();
109
110 calibrate_delay();
111 smp_store_cpu_info(cpuid);
112 local_flush_cache_all();
113 local_flush_tlb_all();
114
115 /* Allow master to continue. */
116 swap((unsigned long *)&cpu_callin_map[cpuid], 1);
117 local_flush_cache_all();
118 local_flush_tlb_all();
119
120 cpu_probe();
121
122 while((unsigned long)current_set[cpuid] < PAGE_OFFSET)
123 barrier();
124
125 while(current_set[cpuid]->cpu != cpuid)
126 barrier();
127
128 /* Fix idle thread fields. */
129 __asm__ __volatile__("ld [%0], %%g6\n\t"
130 : : "r" (&current_set[cpuid])
131 : "memory" /* paranoid */);
132
133 cpu_leds[cpuid] = 0x9;
134 show_leds(cpuid);
135
136 /* Attach to the address space of init_task. */
137 atomic_inc(&init_mm.mm_count);
138 current->active_mm = &init_mm;
139
140 local_flush_cache_all();
141 local_flush_tlb_all();
142
143 local_irq_enable(); /* We don't allow PIL 14 yet */
144
Bob Breuera54123e2006-03-23 22:36:19 -0800145 while (!cpu_isset(cpuid, smp_commenced_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 barrier();
147
148 spin_lock_irqsave(&sun4d_imsk_lock, flags);
149 cc_set_imsk(cc_get_imsk() & ~0x4000); /* Allow PIL 14 as well */
150 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700151 cpu_set(cpuid, cpu_online_map);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155extern void init_IRQ(void);
156extern void cpu_panic(void);
157
158/*
159 * Cycle through the processors asking the PROM to start each one.
160 */
161
162extern struct linux_prom_registers smp_penguin_ctable;
163extern unsigned long trapbase_cpu1[];
164extern unsigned long trapbase_cpu2[];
165extern unsigned long trapbase_cpu3[];
166
167void __init smp4d_boot_cpus(void)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (boot_cpu_id)
170 current_set[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 smp_setup_percpu_timer();
172 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Al Virob4cff842007-02-01 13:52:33 +0000175int __cpuinit smp4d_boot_one_cpu(int i)
Raymond Burns8b3c8482006-07-17 21:57:09 -0700176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 extern unsigned long sun4d_cpu_startup;
178 unsigned long *entry = &sun4d_cpu_startup;
179 struct task_struct *p;
180 int timeout;
Raymond Burns8b3c8482006-07-17 21:57:09 -0700181 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Raymond Burns8b3c8482006-07-17 21:57:09 -0700183 cpu_find_by_instance(i, &cpu_node,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* Cook up an idler for this guy. */
185 p = fork_idle(i);
Al Virod562ef62006-01-12 01:05:46 -0800186 current_set[i] = task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /*
189 * Initialize the contexts table
190 * Since the call to prom_startcpu() trashes the structure,
191 * we need to re-initialize it for each cpu
192 */
193 smp_penguin_ctable.which_io = 0;
194 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
195 smp_penguin_ctable.reg_size = 0;
196
197 /* whirrr, whirrr, whirrrrrrrrr... */
Raymond Burns8b3c8482006-07-17 21:57:09 -0700198 SMP_PRINTK(("Starting CPU %d at %p \n", i, entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700200 prom_startcpu(cpu_node,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 &smp_penguin_ctable, 0, (char *)entry);
202
203 SMP_PRINTK(("prom_startcpu returned :)\n"));
204
205 /* wheee... it's going... */
206 for(timeout = 0; timeout < 10000; timeout++) {
207 if(cpu_callin_map[i])
208 break;
209 udelay(200);
210 }
211
Raymond Burns8b3c8482006-07-17 21:57:09 -0700212 if (!(cpu_callin_map[i])) {
213 printk("Processor %d is stuck.\n", i);
214 return -ENODEV;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700218 return 0;
219}
220
221void __init smp4d_smp_done(void)
222{
223 int i, first;
224 int *prev;
225
226 /* setup cpu list for irq rotation */
227 first = 0;
228 prev = &first;
229 for (i = 0; i < NR_CPUS; i++)
230 if (cpu_online(i)) {
231 *prev = i;
232 prev = &cpu_data(i).next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Raymond Burns8b3c8482006-07-17 21:57:09 -0700234 *prev = first;
235 local_flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* Free unneeded trap tables */
238 ClearPageReserved(virt_to_page(trapbase_cpu1));
Nick Piggin7835e982006-03-22 00:08:40 -0800239 init_page_count(virt_to_page(trapbase_cpu1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 free_page((unsigned long)trapbase_cpu1);
241 totalram_pages++;
242 num_physpages++;
243
244 ClearPageReserved(virt_to_page(trapbase_cpu2));
Nick Piggin7835e982006-03-22 00:08:40 -0800245 init_page_count(virt_to_page(trapbase_cpu2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 free_page((unsigned long)trapbase_cpu2);
247 totalram_pages++;
248 num_physpages++;
249
250 ClearPageReserved(virt_to_page(trapbase_cpu3));
Nick Piggin7835e982006-03-22 00:08:40 -0800251 init_page_count(virt_to_page(trapbase_cpu3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 free_page((unsigned long)trapbase_cpu3);
253 totalram_pages++;
254 num_physpages++;
255
256 /* Ok, they are spinning and ready to go. */
257 smp_processors_ready = 1;
258 sun4d_distribute_irqs();
259}
260
261static struct smp_funcall {
262 smpfunc_t func;
263 unsigned long arg1;
264 unsigned long arg2;
265 unsigned long arg3;
266 unsigned long arg4;
267 unsigned long arg5;
268 unsigned char processors_in[NR_CPUS]; /* Set when ipi entered. */
269 unsigned char processors_out[NR_CPUS]; /* Set when ipi exited. */
270} ccall_info __attribute__((aligned(8)));
271
272static DEFINE_SPINLOCK(cross_call_lock);
273
274/* Cross calls must be serialized, at least currently. */
David S. Miller66e4f8c2008-08-27 20:03:22 -0700275static void smp4d_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
276 unsigned long arg2, unsigned long arg3,
277 unsigned long arg4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 if(smp_processors_ready) {
280 register int high = smp_highest_cpu;
281 unsigned long flags;
282
283 spin_lock_irqsave(&cross_call_lock, flags);
284
285 {
286 /* If you make changes here, make sure gcc generates proper code... */
287 register smpfunc_t f asm("i0") = func;
288 register unsigned long a1 asm("i1") = arg1;
289 register unsigned long a2 asm("i2") = arg2;
290 register unsigned long a3 asm("i3") = arg3;
291 register unsigned long a4 asm("i4") = arg4;
David S. Miller66e4f8c2008-08-27 20:03:22 -0700292 register unsigned long a5 asm("i5") = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 __asm__ __volatile__(
295 "std %0, [%6]\n\t"
296 "std %2, [%6 + 8]\n\t"
297 "std %4, [%6 + 16]\n\t" : :
298 "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
299 "r" (&ccall_info.func));
300 }
301
302 /* Init receive/complete mapping, plus fire the IPI's off. */
303 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 register int i;
305
David S. Miller66e4f8c2008-08-27 20:03:22 -0700306 cpu_clear(smp_processor_id(), mask);
307 cpus_and(mask, cpu_online_map, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 for(i = 0; i <= high; i++) {
309 if (cpu_isset(i, mask)) {
310 ccall_info.processors_in[i] = 0;
311 ccall_info.processors_out[i] = 0;
312 sun4d_send_ipi(i, IRQ_CROSS_CALL);
313 }
314 }
315 }
316
317 {
318 register int i;
319
320 i = 0;
321 do {
David S. Miller66e4f8c2008-08-27 20:03:22 -0700322 if (!cpu_isset(i, mask))
323 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 while(!ccall_info.processors_in[i])
325 barrier();
326 } while(++i <= high);
327
328 i = 0;
329 do {
David S. Miller66e4f8c2008-08-27 20:03:22 -0700330 if (!cpu_isset(i, mask))
331 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 while(!ccall_info.processors_out[i])
333 barrier();
334 } while(++i <= high);
335 }
336
337 spin_unlock_irqrestore(&cross_call_lock, flags);
338 }
339}
340
341/* Running cross calls. */
342void smp4d_cross_call_irq(void)
343{
344 int i = hard_smp4d_processor_id();
345
346 ccall_info.processors_in[i] = 1;
347 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
348 ccall_info.arg4, ccall_info.arg5);
349 ccall_info.processors_out[i] = 1;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
353{
Al Viro0d844382006-10-08 14:30:44 +0100354 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int cpu = hard_smp4d_processor_id();
356 static int cpu_tick[NR_CPUS];
357 static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
358
Al Viro0d844382006-10-08 14:30:44 +0100359 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 bw_get_prof_limit(cpu);
361 bw_clear_intr_mask(0, 1); /* INTR_TABLE[0] & 1 is Profile IRQ */
362
363 cpu_tick[cpu]++;
364 if (!(cpu_tick[cpu] & 15)) {
365 if (cpu_tick[cpu] == 0x60)
366 cpu_tick[cpu] = 0;
367 cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
368 show_leds(cpu);
369 }
370
Al Viro0d844382006-10-08 14:30:44 +0100371 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if(!--prof_counter(cpu)) {
374 int user = user_mode(regs);
375
376 irq_enter();
377 update_process_times(user);
378 irq_exit();
379
380 prof_counter(cpu) = prof_multiplier(cpu);
381 }
Al Viro0d844382006-10-08 14:30:44 +0100382 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385extern unsigned int lvl14_resolution;
386
387static void __init smp_setup_percpu_timer(void)
388{
389 int cpu = hard_smp4d_processor_id();
390
391 prof_counter(cpu) = prof_multiplier(cpu) = 1;
392 load_profile_irq(cpu, lvl14_resolution);
393}
394
395void __init smp4d_blackbox_id(unsigned *addr)
396{
397 int rd = *addr & 0x3e000000;
398
399 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
400 addr[1] = 0x01000000; /* nop */
401 addr[2] = 0x01000000; /* nop */
402}
403
404void __init smp4d_blackbox_current(unsigned *addr)
405{
406 int rd = *addr & 0x3e000000;
407
408 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
409 addr[2] = 0x81282002 | rd | (rd >> 11); /* sll reg, 2, reg */
410 addr[4] = 0x01000000; /* nop */
411}
412
413void __init sun4d_init_smp(void)
414{
415 int i;
416 extern unsigned int t_nmi[], linux_trap_ipi15_sun4d[], linux_trap_ipi15_sun4m[];
417
418 /* Patch ipi15 trap table */
419 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_sun4d - linux_trap_ipi15_sun4m);
420
421 /* And set btfixup... */
422 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4d_blackbox_id);
423 BTFIXUPSET_BLACKBOX(load_current, smp4d_blackbox_current);
424 BTFIXUPSET_CALL(smp_cross_call, smp4d_cross_call, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4d_processor_id, BTFIXUPCALL_NORM);
426
427 for (i = 0; i < NR_CPUS; i++) {
428 ccall_info.processors_in[i] = 1;
429 ccall_info.processors_out[i] = 1;
430 }
431}