1 /* sun4d_smp.c: Sparc SS1000/SC2000 SMP support.
3 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Based on sun4m's smp.c, which is:
6 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/threads.h>
14 #include <linux/smp.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
20 #include <linux/swap.h>
21 #include <linux/profile.h>
22 #include <linux/delay.h>
24 #include <asm/ptrace.h>
25 #include <asm/atomic.h>
26 #include <asm/irq_regs.h>
30 #include <asm/pgalloc.h>
31 #include <asm/pgtable.h>
32 #include <asm/oplib.h>
34 #include <asm/tlbflush.h>
35 #include <asm/cacheflush.h>
36 #include <asm/cpudata.h>
39 #define IRQ_CROSS_CALL 15
41 extern ctxd_t *srmmu_ctx_table_phys;
43 static volatile int smp_processors_ready = 0;
44 static int smp_highest_cpu;
45 extern volatile unsigned long cpu_callin_map[NR_CPUS];
46 extern cpuinfo_sparc cpu_data[NR_CPUS];
47 extern unsigned char boot_cpu_id;
48 extern volatile int smp_process_available;
50 extern cpumask_t smp_commenced_mask;
52 extern int __smp4d_processor_id(void);
54 /* #define SMP_DEBUG */
57 #define SMP_PRINTK(x) printk x
62 static inline unsigned long swap(volatile unsigned long *ptr, unsigned long val)
64 __asm__ __volatile__("swap [%1], %0\n\t" :
65 "=&r" (val), "=&r" (ptr) :
66 "0" (val), "1" (ptr));
70 static void smp_setup_percpu_timer(void);
71 extern void cpu_probe(void);
72 extern void sun4d_distribute_irqs(void);
74 void __init smp4d_callin(void)
76 int cpuid = hard_smp4d_processor_id();
77 extern spinlock_t sun4d_imsk_lock;
80 /* Show we are alive */
81 cpu_leds[cpuid] = 0x6;
84 /* Enable level15 interrupt, disable level14 interrupt for now */
85 cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
87 local_flush_cache_all();
88 local_flush_tlb_all();
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.
96 /* Get our local ticker going. */
97 smp_setup_percpu_timer();
100 smp_store_cpu_info(cpuid);
101 local_flush_cache_all();
102 local_flush_tlb_all();
104 /* Allow master to continue. */
105 swap((unsigned long *)&cpu_callin_map[cpuid], 1);
106 local_flush_cache_all();
107 local_flush_tlb_all();
111 while((unsigned long)current_set[cpuid] < PAGE_OFFSET)
114 while(current_set[cpuid]->cpu != cpuid)
117 /* Fix idle thread fields. */
118 __asm__ __volatile__("ld [%0], %%g6\n\t"
119 : : "r" (¤t_set[cpuid])
120 : "memory" /* paranoid */);
122 cpu_leds[cpuid] = 0x9;
125 /* Attach to the address space of init_task. */
126 atomic_inc(&init_mm.mm_count);
127 current->active_mm = &init_mm;
129 local_flush_cache_all();
130 local_flush_tlb_all();
132 local_irq_enable(); /* We don't allow PIL 14 yet */
134 while (!cpu_isset(cpuid, smp_commenced_mask))
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);
140 cpu_set(cpuid, cpu_online_map);
144 extern void init_IRQ(void);
145 extern void cpu_panic(void);
148 * Cycle through the processors asking the PROM to start each one.
151 extern struct linux_prom_registers smp_penguin_ctable;
152 extern unsigned long trapbase_cpu1[];
153 extern unsigned long trapbase_cpu2[];
154 extern unsigned long trapbase_cpu3[];
156 void __init smp4d_boot_cpus(void)
159 current_set[0] = NULL;
160 smp_setup_percpu_timer();
161 local_flush_cache_all();
164 int __cpuinit smp4d_boot_one_cpu(int i)
166 extern unsigned long sun4d_cpu_startup;
167 unsigned long *entry = &sun4d_cpu_startup;
168 struct task_struct *p;
172 cpu_find_by_instance(i, &cpu_node,NULL);
173 /* Cook up an idler for this guy. */
175 current_set[i] = task_thread_info(p);
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
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;
186 /* whirrr, whirrr, whirrrrrrrrr... */
187 SMP_PRINTK(("Starting CPU %d at %p \n", i, entry));
188 local_flush_cache_all();
189 prom_startcpu(cpu_node,
190 &smp_penguin_ctable, 0, (char *)entry);
192 SMP_PRINTK(("prom_startcpu returned :)\n"));
194 /* wheee... it's going... */
195 for(timeout = 0; timeout < 10000; timeout++) {
196 if(cpu_callin_map[i])
201 if (!(cpu_callin_map[i])) {
202 printk("Processor %d is stuck.\n", i);
206 local_flush_cache_all();
210 void __init smp4d_smp_done(void)
215 /* setup cpu list for irq rotation */
218 for (i = 0; i < NR_CPUS; i++)
221 prev = &cpu_data(i).next;
224 local_flush_cache_all();
226 /* Free unneeded trap tables */
227 ClearPageReserved(virt_to_page(trapbase_cpu1));
228 init_page_count(virt_to_page(trapbase_cpu1));
229 free_page((unsigned long)trapbase_cpu1);
233 ClearPageReserved(virt_to_page(trapbase_cpu2));
234 init_page_count(virt_to_page(trapbase_cpu2));
235 free_page((unsigned long)trapbase_cpu2);
239 ClearPageReserved(virt_to_page(trapbase_cpu3));
240 init_page_count(virt_to_page(trapbase_cpu3));
241 free_page((unsigned long)trapbase_cpu3);
245 /* Ok, they are spinning and ready to go. */
246 smp_processors_ready = 1;
247 sun4d_distribute_irqs();
250 static struct smp_funcall {
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)));
261 static DEFINE_SPINLOCK(cross_call_lock);
263 /* Cross calls must be serialized, at least currently. */
264 static void smp4d_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
265 unsigned long arg2, unsigned long arg3,
268 if(smp_processors_ready) {
269 register int high = smp_highest_cpu;
272 spin_lock_irqsave(&cross_call_lock, flags);
275 /* If you make changes here, make sure gcc generates proper code... */
276 register smpfunc_t f asm("i0") = func;
277 register unsigned long a1 asm("i1") = arg1;
278 register unsigned long a2 asm("i2") = arg2;
279 register unsigned long a3 asm("i3") = arg3;
280 register unsigned long a4 asm("i4") = arg4;
281 register unsigned long a5 asm("i5") = 0;
283 __asm__ __volatile__(
285 "std %2, [%6 + 8]\n\t"
286 "std %4, [%6 + 16]\n\t" : :
287 "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
288 "r" (&ccall_info.func));
291 /* Init receive/complete mapping, plus fire the IPI's off. */
295 cpu_clear(smp_processor_id(), mask);
296 cpus_and(mask, cpu_online_map, mask);
297 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);
311 if (!cpu_isset(i, mask))
313 while(!ccall_info.processors_in[i])
315 } while(++i <= high);
319 if (!cpu_isset(i, mask))
321 while(!ccall_info.processors_out[i])
323 } while(++i <= high);
326 spin_unlock_irqrestore(&cross_call_lock, flags);
330 /* Running cross calls. */
331 void smp4d_cross_call_irq(void)
333 int i = hard_smp4d_processor_id();
335 ccall_info.processors_in[i] = 1;
336 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
337 ccall_info.arg4, ccall_info.arg5);
338 ccall_info.processors_out[i] = 1;
341 void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
343 struct pt_regs *old_regs;
344 int cpu = hard_smp4d_processor_id();
345 static int cpu_tick[NR_CPUS];
346 static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
348 old_regs = set_irq_regs(regs);
349 bw_get_prof_limit(cpu);
350 bw_clear_intr_mask(0, 1); /* INTR_TABLE[0] & 1 is Profile IRQ */
353 if (!(cpu_tick[cpu] & 15)) {
354 if (cpu_tick[cpu] == 0x60)
356 cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
360 profile_tick(CPU_PROFILING);
362 if(!--prof_counter(cpu)) {
363 int user = user_mode(regs);
366 update_process_times(user);
369 prof_counter(cpu) = prof_multiplier(cpu);
371 set_irq_regs(old_regs);
374 extern unsigned int lvl14_resolution;
376 static void __init smp_setup_percpu_timer(void)
378 int cpu = hard_smp4d_processor_id();
380 prof_counter(cpu) = prof_multiplier(cpu) = 1;
381 load_profile_irq(cpu, lvl14_resolution);
384 void __init smp4d_blackbox_id(unsigned *addr)
386 int rd = *addr & 0x3e000000;
388 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
389 addr[1] = 0x01000000; /* nop */
390 addr[2] = 0x01000000; /* nop */
393 void __init smp4d_blackbox_current(unsigned *addr)
395 int rd = *addr & 0x3e000000;
397 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
398 addr[2] = 0x81282002 | rd | (rd >> 11); /* sll reg, 2, reg */
399 addr[4] = 0x01000000; /* nop */
402 void __init sun4d_init_smp(void)
405 extern unsigned int t_nmi[], linux_trap_ipi15_sun4d[], linux_trap_ipi15_sun4m[];
407 /* Patch ipi15 trap table */
408 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_sun4d - linux_trap_ipi15_sun4m);
410 /* And set btfixup... */
411 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4d_blackbox_id);
412 BTFIXUPSET_BLACKBOX(load_current, smp4d_blackbox_current);
413 BTFIXUPSET_CALL(smp_cross_call, smp4d_cross_call, BTFIXUPCALL_NORM);
414 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4d_processor_id, BTFIXUPCALL_NORM);
416 for (i = 0; i < NR_CPUS; i++) {
417 ccall_info.processors_in[i] = 1;
418 ccall_info.processors_out[i] = 1;