blob: bf7147a9308e1ea68565179418c3b21ef5497adc [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
74void __init smp4d_callin(void)
75{
76 int cpuid = hard_smp4d_processor_id();
77 extern spinlock_t sun4d_imsk_lock;
78 unsigned long flags;
79
80 /* Show we are alive */
81 cpu_leds[cpuid] = 0x6;
82 show_leds(cpuid);
83
84 /* Enable level15 interrupt, disable level14 interrupt for now */
85 cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
86
87 local_flush_cache_all();
88 local_flush_tlb_all();
89
90 /*
91 * Unblock the master CPU _only_ when the scheduler state
92 * of all secondary CPUs will be up-to-date, so after
93 * the SMP initialization the master will be just allowed
94 * to call the scheduler code.
95 */
96 /* Get our local ticker going. */
97 smp_setup_percpu_timer();
98
99 calibrate_delay();
100 smp_store_cpu_info(cpuid);
101 local_flush_cache_all();
102 local_flush_tlb_all();
103
104 /* Allow master to continue. */
105 swap((unsigned long *)&cpu_callin_map[cpuid], 1);
106 local_flush_cache_all();
107 local_flush_tlb_all();
108
109 cpu_probe();
110
111 while((unsigned long)current_set[cpuid] < PAGE_OFFSET)
112 barrier();
113
114 while(current_set[cpuid]->cpu != cpuid)
115 barrier();
116
117 /* Fix idle thread fields. */
118 __asm__ __volatile__("ld [%0], %%g6\n\t"
119 : : "r" (&current_set[cpuid])
120 : "memory" /* paranoid */);
121
122 cpu_leds[cpuid] = 0x9;
123 show_leds(cpuid);
124
125 /* Attach to the address space of init_task. */
126 atomic_inc(&init_mm.mm_count);
127 current->active_mm = &init_mm;
128
129 local_flush_cache_all();
130 local_flush_tlb_all();
131
132 local_irq_enable(); /* We don't allow PIL 14 yet */
133
Bob Breuera54123e2006-03-23 22:36:19 -0800134 while (!cpu_isset(cpuid, smp_commenced_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 barrier();
136
137 spin_lock_irqsave(&sun4d_imsk_lock, flags);
138 cc_set_imsk(cc_get_imsk() & ~0x4000); /* Allow PIL 14 as well */
139 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700140 cpu_set(cpuid, cpu_online_map);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144extern void init_IRQ(void);
145extern void cpu_panic(void);
146
147/*
148 * Cycle through the processors asking the PROM to start each one.
149 */
150
151extern struct linux_prom_registers smp_penguin_ctable;
152extern unsigned long trapbase_cpu1[];
153extern unsigned long trapbase_cpu2[];
154extern unsigned long trapbase_cpu3[];
155
156void __init smp4d_boot_cpus(void)
157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (boot_cpu_id)
159 current_set[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 smp_setup_percpu_timer();
161 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Al Virob4cff842007-02-01 13:52:33 +0000164int __cpuinit smp4d_boot_one_cpu(int i)
Raymond Burns8b3c8482006-07-17 21:57:09 -0700165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 extern unsigned long sun4d_cpu_startup;
167 unsigned long *entry = &sun4d_cpu_startup;
168 struct task_struct *p;
169 int timeout;
Raymond Burns8b3c8482006-07-17 21:57:09 -0700170 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Raymond Burns8b3c8482006-07-17 21:57:09 -0700172 cpu_find_by_instance(i, &cpu_node,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* Cook up an idler for this guy. */
174 p = fork_idle(i);
Al Virod562ef62006-01-12 01:05:46 -0800175 current_set[i] = task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /*
178 * Initialize the contexts table
179 * Since the call to prom_startcpu() trashes the structure,
180 * we need to re-initialize it for each cpu
181 */
182 smp_penguin_ctable.which_io = 0;
183 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
184 smp_penguin_ctable.reg_size = 0;
185
186 /* whirrr, whirrr, whirrrrrrrrr... */
Raymond Burns8b3c8482006-07-17 21:57:09 -0700187 SMP_PRINTK(("Starting CPU %d at %p \n", i, entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700189 prom_startcpu(cpu_node,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 &smp_penguin_ctable, 0, (char *)entry);
191
192 SMP_PRINTK(("prom_startcpu returned :)\n"));
193
194 /* wheee... it's going... */
195 for(timeout = 0; timeout < 10000; timeout++) {
196 if(cpu_callin_map[i])
197 break;
198 udelay(200);
199 }
200
Raymond Burns8b3c8482006-07-17 21:57:09 -0700201 if (!(cpu_callin_map[i])) {
202 printk("Processor %d is stuck.\n", i);
203 return -ENODEV;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700207 return 0;
208}
209
210void __init smp4d_smp_done(void)
211{
212 int i, first;
213 int *prev;
214
215 /* setup cpu list for irq rotation */
216 first = 0;
217 prev = &first;
218 for (i = 0; i < NR_CPUS; i++)
219 if (cpu_online(i)) {
220 *prev = i;
221 prev = &cpu_data(i).next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Raymond Burns8b3c8482006-07-17 21:57:09 -0700223 *prev = first;
224 local_flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* Free unneeded trap tables */
227 ClearPageReserved(virt_to_page(trapbase_cpu1));
Nick Piggin7835e982006-03-22 00:08:40 -0800228 init_page_count(virt_to_page(trapbase_cpu1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 free_page((unsigned long)trapbase_cpu1);
230 totalram_pages++;
231 num_physpages++;
232
233 ClearPageReserved(virt_to_page(trapbase_cpu2));
Nick Piggin7835e982006-03-22 00:08:40 -0800234 init_page_count(virt_to_page(trapbase_cpu2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 free_page((unsigned long)trapbase_cpu2);
236 totalram_pages++;
237 num_physpages++;
238
239 ClearPageReserved(virt_to_page(trapbase_cpu3));
Nick Piggin7835e982006-03-22 00:08:40 -0800240 init_page_count(virt_to_page(trapbase_cpu3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 free_page((unsigned long)trapbase_cpu3);
242 totalram_pages++;
243 num_physpages++;
244
245 /* Ok, they are spinning and ready to go. */
246 smp_processors_ready = 1;
247 sun4d_distribute_irqs();
248}
249
250static struct smp_funcall {
251 smpfunc_t func;
252 unsigned long arg1;
253 unsigned long arg2;
254 unsigned long arg3;
255 unsigned long arg4;
256 unsigned long arg5;
257 unsigned char processors_in[NR_CPUS]; /* Set when ipi entered. */
258 unsigned char processors_out[NR_CPUS]; /* Set when ipi exited. */
259} ccall_info __attribute__((aligned(8)));
260
261static DEFINE_SPINLOCK(cross_call_lock);
262
263/* Cross calls must be serialized, at least currently. */
264void smp4d_cross_call(smpfunc_t func, unsigned long arg1, unsigned long arg2,
265 unsigned long arg3, unsigned long arg4, unsigned long arg5)
266{
267 if(smp_processors_ready) {
268 register int high = smp_highest_cpu;
269 unsigned long flags;
270
271 spin_lock_irqsave(&cross_call_lock, flags);
272
273 {
274 /* If you make changes here, make sure gcc generates proper code... */
275 register smpfunc_t f asm("i0") = func;
276 register unsigned long a1 asm("i1") = arg1;
277 register unsigned long a2 asm("i2") = arg2;
278 register unsigned long a3 asm("i3") = arg3;
279 register unsigned long a4 asm("i4") = arg4;
280 register unsigned long a5 asm("i5") = arg5;
281
282 __asm__ __volatile__(
283 "std %0, [%6]\n\t"
284 "std %2, [%6 + 8]\n\t"
285 "std %4, [%6 + 16]\n\t" : :
286 "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
287 "r" (&ccall_info.func));
288 }
289
290 /* Init receive/complete mapping, plus fire the IPI's off. */
291 {
292 cpumask_t mask;
293 register int i;
294
295 mask = cpumask_of_cpu(hard_smp4d_processor_id());
Raymond Burns8b3c8482006-07-17 21:57:09 -0700296 cpus_andnot(mask, cpu_online_map, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 for(i = 0; i <= high; i++) {
298 if (cpu_isset(i, mask)) {
299 ccall_info.processors_in[i] = 0;
300 ccall_info.processors_out[i] = 0;
301 sun4d_send_ipi(i, IRQ_CROSS_CALL);
302 }
303 }
304 }
305
306 {
307 register int i;
308
309 i = 0;
310 do {
311 while(!ccall_info.processors_in[i])
312 barrier();
313 } while(++i <= high);
314
315 i = 0;
316 do {
317 while(!ccall_info.processors_out[i])
318 barrier();
319 } while(++i <= high);
320 }
321
322 spin_unlock_irqrestore(&cross_call_lock, flags);
323 }
324}
325
326/* Running cross calls. */
327void smp4d_cross_call_irq(void)
328{
329 int i = hard_smp4d_processor_id();
330
331 ccall_info.processors_in[i] = 1;
332 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
333 ccall_info.arg4, ccall_info.arg5);
334 ccall_info.processors_out[i] = 1;
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
338{
Al Viro0d844382006-10-08 14:30:44 +0100339 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int cpu = hard_smp4d_processor_id();
341 static int cpu_tick[NR_CPUS];
342 static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
343
Al Viro0d844382006-10-08 14:30:44 +0100344 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 bw_get_prof_limit(cpu);
346 bw_clear_intr_mask(0, 1); /* INTR_TABLE[0] & 1 is Profile IRQ */
347
348 cpu_tick[cpu]++;
349 if (!(cpu_tick[cpu] & 15)) {
350 if (cpu_tick[cpu] == 0x60)
351 cpu_tick[cpu] = 0;
352 cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
353 show_leds(cpu);
354 }
355
Al Viro0d844382006-10-08 14:30:44 +0100356 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if(!--prof_counter(cpu)) {
359 int user = user_mode(regs);
360
361 irq_enter();
362 update_process_times(user);
363 irq_exit();
364
365 prof_counter(cpu) = prof_multiplier(cpu);
366 }
Al Viro0d844382006-10-08 14:30:44 +0100367 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370extern unsigned int lvl14_resolution;
371
372static void __init smp_setup_percpu_timer(void)
373{
374 int cpu = hard_smp4d_processor_id();
375
376 prof_counter(cpu) = prof_multiplier(cpu) = 1;
377 load_profile_irq(cpu, lvl14_resolution);
378}
379
380void __init smp4d_blackbox_id(unsigned *addr)
381{
382 int rd = *addr & 0x3e000000;
383
384 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
385 addr[1] = 0x01000000; /* nop */
386 addr[2] = 0x01000000; /* nop */
387}
388
389void __init smp4d_blackbox_current(unsigned *addr)
390{
391 int rd = *addr & 0x3e000000;
392
393 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
394 addr[2] = 0x81282002 | rd | (rd >> 11); /* sll reg, 2, reg */
395 addr[4] = 0x01000000; /* nop */
396}
397
398void __init sun4d_init_smp(void)
399{
400 int i;
401 extern unsigned int t_nmi[], linux_trap_ipi15_sun4d[], linux_trap_ipi15_sun4m[];
402
403 /* Patch ipi15 trap table */
404 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_sun4d - linux_trap_ipi15_sun4m);
405
406 /* And set btfixup... */
407 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4d_blackbox_id);
408 BTFIXUPSET_BLACKBOX(load_current, smp4d_blackbox_current);
409 BTFIXUPSET_CALL(smp_cross_call, smp4d_cross_call, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4d_processor_id, BTFIXUPCALL_NORM);
411
412 for (i = 0; i < NR_CPUS; i++) {
413 ccall_info.processors_in[i] = 1;
414 ccall_info.processors_out[i] = 1;
415 }
416}