1 /* leon_smp.c: Sparc-Leon SMP support.
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
6 * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
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>
21 #include <linux/swap.h>
22 #include <linux/profile.h>
24 #include <linux/delay.h>
25 #include <linux/gfp.h>
27 #include <asm/cacheflush.h>
28 #include <asm/tlbflush.h>
30 #include <asm/ptrace.h>
31 #include <asm/atomic.h>
32 #include <asm/irq_regs.h>
34 #include <asm/delay.h>
37 #include <asm/pgalloc.h>
38 #include <asm/pgtable.h>
39 #include <asm/oplib.h>
40 #include <asm/cpudata.h>
43 #include <asm/leon_amba.h>
45 #ifdef CONFIG_SPARC_LEON
49 extern ctxd_t *srmmu_ctx_table_phys;
50 static int smp_processors_ready;
51 extern volatile unsigned long cpu_callin_map[NR_CPUS];
52 extern unsigned char boot_cpu_id;
53 extern cpumask_t smp_commenced_mask;
54 void __init leon_configure_cache_smp(void);
56 static inline unsigned long do_swap(volatile unsigned long *ptr,
59 __asm__ __volatile__("swapa [%1] %2, %0\n\t" : "=&r"(val)
60 : "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
65 static void smp_setup_percpu_timer(void);
67 void __cpuinit leon_callin(void)
69 int cpuid = hard_smpleon_processor_id();
71 local_flush_cache_all();
72 local_flush_tlb_all();
73 leon_configure_cache_smp();
75 /* Get our local ticker going. */
76 smp_setup_percpu_timer();
79 smp_store_cpu_info(cpuid);
81 local_flush_cache_all();
82 local_flush_tlb_all();
85 * Unblock the master CPU _only_ when the scheduler state
86 * of all secondary CPUs will be up-to-date, so after
87 * the SMP initialization the master will be just allowed
88 * to call the scheduler code.
89 * Allow master to continue.
91 do_swap(&cpu_callin_map[cpuid], 1);
93 local_flush_cache_all();
94 local_flush_tlb_all();
98 /* Fix idle thread fields. */
99 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set[cpuid])
100 : "memory" /* paranoid */);
102 /* Attach to the address space of init_task. */
103 atomic_inc(&init_mm.mm_count);
104 current->active_mm = &init_mm;
106 while (!cpu_isset(cpuid, smp_commenced_mask))
110 cpu_set(cpuid, cpu_online_map);
114 * Cycle through the processors asking the PROM to start each one.
117 extern struct linux_prom_registers smp_penguin_ctable;
119 void __init leon_configure_cache_smp(void)
121 unsigned long cfg = sparc_leon3_get_dcachecfg();
122 int me = smp_processor_id();
124 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
125 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
126 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
127 (unsigned int)cfg, (unsigned int)me);
128 sparc_leon3_disable_cache();
130 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
131 sparc_leon3_enable_snooping();
133 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
135 sparc_leon3_disable_cache();
139 local_flush_cache_all();
140 local_flush_tlb_all();
143 void leon_smp_setbroadcast(unsigned int mask)
146 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
147 LEON3_IRQMPSTATUS_BROADCAST) & 1);
149 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
151 if (leon_smp_nrcpus() > 1) {
154 prom_printf("continue anyway\n");
158 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
161 unsigned int leon_smp_getbroadcast(void)
164 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
168 int leon_smp_nrcpus(void)
171 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
172 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
176 void __init leon_boot_cpus(void)
178 int nrcpu = leon_smp_nrcpus();
179 int me = smp_processor_id();
181 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
182 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
183 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
185 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
186 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
187 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, me);
189 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
191 leon_configure_cache_smp();
192 smp_setup_percpu_timer();
193 local_flush_cache_all();
197 int __cpuinit leon_boot_one_cpu(int i)
200 struct task_struct *p;
203 /* Cook up an idler for this guy. */
206 current_set[i] = task_thread_info(p);
208 /* See trampoline.S:leon_smp_cpu_startup for details...
209 * Initialize the contexts table
210 * Since the call to prom_startcpu() trashes the structure,
211 * we need to re-initialize it for each cpu
213 smp_penguin_ctable.which_io = 0;
214 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
215 smp_penguin_ctable.reg_size = 0;
217 /* whirrr, whirrr, whirrrrrrrrr... */
218 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
219 (unsigned int)&leon3_irqctrl_regs->mpstatus);
220 local_flush_cache_all();
222 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
224 /* wheee... it's going... */
225 for (timeout = 0; timeout < 10000; timeout++) {
226 if (cpu_callin_map[i])
230 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
232 if (!(cpu_callin_map[i])) {
233 printk(KERN_ERR "Processor %d is stuck.\n", i);
236 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
237 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
238 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, i);
241 local_flush_cache_all();
245 void __init leon_smp_done(void)
251 /* setup cpu list for irq rotation */
254 for (i = 0; i < NR_CPUS; i++) {
257 prev = &cpu_data(i).next;
261 local_flush_cache_all();
263 /* Free unneeded trap tables */
264 if (!cpu_isset(1, cpu_present_map)) {
265 ClearPageReserved(virt_to_page(trapbase_cpu1));
266 init_page_count(virt_to_page(trapbase_cpu1));
267 free_page((unsigned long)trapbase_cpu1);
271 if (!cpu_isset(2, cpu_present_map)) {
272 ClearPageReserved(virt_to_page(trapbase_cpu2));
273 init_page_count(virt_to_page(trapbase_cpu2));
274 free_page((unsigned long)trapbase_cpu2);
278 if (!cpu_isset(3, cpu_present_map)) {
279 ClearPageReserved(virt_to_page(trapbase_cpu3));
280 init_page_count(virt_to_page(trapbase_cpu3));
281 free_page((unsigned long)trapbase_cpu3);
285 /* Ok, they are spinning and ready to go. */
286 smp_processors_ready = 1;
290 void leon_irq_rotate(int cpu)
294 static struct smp_funcall {
301 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
302 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
305 static DEFINE_SPINLOCK(cross_call_lock);
307 /* Cross calls must be serialized, at least currently. */
308 static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
309 unsigned long arg2, unsigned long arg3,
312 if (smp_processors_ready) {
313 register int high = NR_CPUS - 1;
316 spin_lock_irqsave(&cross_call_lock, flags);
319 /* If you make changes here, make sure gcc generates proper code... */
320 register smpfunc_t f asm("i0") = func;
321 register unsigned long a1 asm("i1") = arg1;
322 register unsigned long a2 asm("i2") = arg2;
323 register unsigned long a3 asm("i3") = arg3;
324 register unsigned long a4 asm("i4") = arg4;
325 register unsigned long a5 asm("i5") = 0;
327 __asm__ __volatile__("std %0, [%6]\n\t"
328 "std %2, [%6 + 8]\n\t"
329 "std %4, [%6 + 16]\n\t" : :
330 "r"(f), "r"(a1), "r"(a2), "r"(a3),
332 "r"(&ccall_info.func));
335 /* Init receive/complete mapping, plus fire the IPI's off. */
339 cpu_clear(smp_processor_id(), mask);
340 cpus_and(mask, cpu_online_map, mask);
341 for (i = 0; i <= high; i++) {
342 if (cpu_isset(i, mask)) {
343 ccall_info.processors_in[i] = 0;
344 ccall_info.processors_out[i] = 0;
345 set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
356 if (!cpu_isset(i, mask))
359 while (!ccall_info.processors_in[i])
361 } while (++i <= high);
365 if (!cpu_isset(i, mask))
368 while (!ccall_info.processors_out[i])
370 } while (++i <= high);
373 spin_unlock_irqrestore(&cross_call_lock, flags);
377 /* Running cross calls. */
378 void leon_cross_call_irq(void)
380 int i = smp_processor_id();
382 ccall_info.processors_in[i] = 1;
383 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
384 ccall_info.arg4, ccall_info.arg5);
385 ccall_info.processors_out[i] = 1;
388 void leon_percpu_timer_interrupt(struct pt_regs *regs)
390 struct pt_regs *old_regs;
391 int cpu = smp_processor_id();
393 old_regs = set_irq_regs(regs);
395 leon_clear_profile_irq(cpu);
397 profile_tick(CPU_PROFILING);
399 if (!--prof_counter(cpu)) {
400 int user = user_mode(regs);
403 update_process_times(user);
406 prof_counter(cpu) = prof_multiplier(cpu);
408 set_irq_regs(old_regs);
411 static void __init smp_setup_percpu_timer(void)
413 int cpu = smp_processor_id();
415 prof_counter(cpu) = prof_multiplier(cpu) = 1;
418 void __init leon_blackbox_id(unsigned *addr)
420 int rd = *addr & 0x3e000000;
423 /* patch places where ___b_hard_smp_processor_id appears */
424 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
425 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
426 addr[2] = 0x01000000; /* nop */
429 void __init leon_blackbox_current(unsigned *addr)
431 int rd = *addr & 0x3e000000;
434 /* patch LOAD_CURRENT macro where ___b_load_current appears */
435 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
436 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
437 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
442 * CPU idle callback function
443 * See .../arch/sparc/kernel/process.c
445 void pmc_leon_idle(void)
447 __asm__ volatile ("mov %g0, %asr19");
450 void __init leon_init_smp(void)
452 /* Patch ipi15 trap table */
453 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
455 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
456 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
457 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
458 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
462 /* Assign power management IDLE handler */
463 pm_idle = pmc_leon_idle;
464 printk(KERN_INFO "leon: power management initialized\n");
469 #endif /* CONFIG_SPARC_LEON */