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/interrupt.h>
16 #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>
26 #include <linux/cpu.h>
27 #include <linux/clockchips.h>
29 #include <asm/cacheflush.h>
30 #include <asm/tlbflush.h>
32 #include <asm/ptrace.h>
33 #include <linux/atomic.h>
34 #include <asm/irq_regs.h>
35 #include <asm/traps.h>
37 #include <asm/delay.h>
40 #include <asm/pgalloc.h>
41 #include <asm/pgtable.h>
42 #include <asm/oplib.h>
43 #include <asm/cpudata.h>
46 #include <asm/leon_amba.h>
47 #include <asm/timer.h>
53 extern ctxd_t *srmmu_ctx_table_phys;
54 static int smp_processors_ready;
55 extern volatile unsigned long cpu_callin_map[NR_CPUS];
56 extern cpumask_t smp_commenced_mask;
57 void __cpuinit leon_configure_cache_smp(void);
58 static void leon_ipi_init(void);
60 /* IRQ number of LEON IPIs */
61 int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
63 static inline unsigned long do_swap(volatile unsigned long *ptr,
66 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
67 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
72 void __cpuinit leon_callin(void)
74 int cpuid = hard_smp_processor_id();
76 local_ops->cache_all();
78 leon_configure_cache_smp();
80 notify_cpu_starting(cpuid);
82 /* Get our local ticker going. */
83 register_percpu_ce(cpuid);
86 smp_store_cpu_info(cpuid);
88 local_ops->cache_all();
92 * Unblock the master CPU _only_ when the scheduler state
93 * of all secondary CPUs will be up-to-date, so after
94 * the SMP initialization the master will be just allowed
95 * to call the scheduler code.
96 * Allow master to continue.
98 do_swap(&cpu_callin_map[cpuid], 1);
100 local_ops->cache_all();
101 local_ops->tlb_all();
103 /* Fix idle thread fields. */
104 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set[cpuid])
105 : "memory" /* paranoid */);
107 /* Attach to the address space of init_task. */
108 atomic_inc(&init_mm.mm_count);
109 current->active_mm = &init_mm;
111 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
115 set_cpu_online(cpuid, true);
119 * Cycle through the processors asking the PROM to start each one.
122 extern struct linux_prom_registers smp_penguin_ctable;
124 void __cpuinit leon_configure_cache_smp(void)
126 unsigned long cfg = sparc_leon3_get_dcachecfg();
127 int me = smp_processor_id();
129 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
130 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
131 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
132 (unsigned int)cfg, (unsigned int)me);
133 sparc_leon3_disable_cache();
135 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
136 sparc_leon3_enable_snooping();
138 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
140 sparc_leon3_disable_cache();
144 local_ops->cache_all();
145 local_ops->tlb_all();
148 void leon_smp_setbroadcast(unsigned int mask)
151 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
152 LEON3_IRQMPSTATUS_BROADCAST) & 1);
154 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
156 if (leon_smp_nrcpus() > 1) {
159 prom_printf("continue anyway\n");
163 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
166 unsigned int leon_smp_getbroadcast(void)
169 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
173 int leon_smp_nrcpus(void)
176 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
177 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
181 void __init leon_boot_cpus(void)
183 int nrcpu = leon_smp_nrcpus();
184 int me = smp_processor_id();
189 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
190 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
191 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
193 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
194 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
195 leon_enable_irq_cpu(leon_ipi_irq, me);
197 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
199 leon_configure_cache_smp();
200 local_ops->cache_all();
204 int __cpuinit leon_boot_one_cpu(int i, struct task_struct *idle)
208 current_set[i] = task_thread_info(idle);
210 /* See trampoline.S:leon_smp_cpu_startup for details...
211 * Initialize the contexts table
212 * Since the call to prom_startcpu() trashes the structure,
213 * we need to re-initialize it for each cpu
215 smp_penguin_ctable.which_io = 0;
216 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
217 smp_penguin_ctable.reg_size = 0;
219 /* whirrr, whirrr, whirrrrrrrrr... */
220 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
221 (unsigned int)&leon3_irqctrl_regs->mpstatus);
222 local_ops->cache_all();
224 /* Make sure all IRQs are of from the start for this new CPU */
225 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
228 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
230 /* wheee... it's going... */
231 for (timeout = 0; timeout < 10000; timeout++) {
232 if (cpu_callin_map[i])
236 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
238 if (!(cpu_callin_map[i])) {
239 printk(KERN_ERR "Processor %d is stuck.\n", i);
242 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
243 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
244 leon_enable_irq_cpu(leon_ipi_irq, i);
247 local_ops->cache_all();
251 void __init leon_smp_done(void)
257 /* setup cpu list for irq rotation */
260 for (i = 0; i < NR_CPUS; i++) {
263 prev = &cpu_data(i).next;
267 local_ops->cache_all();
269 /* Free unneeded trap tables */
270 if (!cpu_present(1)) {
271 ClearPageReserved(virt_to_page(&trapbase_cpu1));
272 init_page_count(virt_to_page(&trapbase_cpu1));
273 free_page((unsigned long)&trapbase_cpu1);
277 if (!cpu_present(2)) {
278 ClearPageReserved(virt_to_page(&trapbase_cpu2));
279 init_page_count(virt_to_page(&trapbase_cpu2));
280 free_page((unsigned long)&trapbase_cpu2);
284 if (!cpu_present(3)) {
285 ClearPageReserved(virt_to_page(&trapbase_cpu3));
286 init_page_count(virt_to_page(&trapbase_cpu3));
287 free_page((unsigned long)&trapbase_cpu3);
291 /* Ok, they are spinning and ready to go. */
292 smp_processors_ready = 1;
296 void leon_irq_rotate(int cpu)
300 struct leon_ipi_work {
306 static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
308 /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
309 * is used for all three types of IPIs.
311 static void __init leon_ipi_init(void)
314 struct leon_ipi_work *work;
316 struct device_node *rootnp;
317 struct tt_entry *trap_table;
320 /* Find IPI IRQ or stick with default value */
321 rootnp = of_find_node_by_path("/ambapp0");
323 pp = of_find_property(rootnp, "ipi_num", &len);
324 if (pp && (*(int *)pp->value))
325 leon_ipi_irq = *(int *)pp->value;
327 printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
329 /* Adjust so that we jump directly to smpleon_ipi */
330 local_irq_save(flags);
331 trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
332 trap_table->inst_three += smpleon_ipi - real_irq_entry;
333 local_ops->cache_all();
334 local_irq_restore(flags);
336 for_each_possible_cpu(cpu) {
337 work = &per_cpu(leon_ipi_work, cpu);
338 work->single = work->msk = work->resched = 0;
342 static void leon_send_ipi(int cpu, int level)
345 mask = leon_get_irqmask(level);
346 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
349 static void leon_ipi_single(int cpu)
351 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
356 /* Generate IRQ on the CPU */
357 leon_send_ipi(cpu, leon_ipi_irq);
360 static void leon_ipi_mask_one(int cpu)
362 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
367 /* Generate IRQ on the CPU */
368 leon_send_ipi(cpu, leon_ipi_irq);
371 static void leon_ipi_resched(int cpu)
373 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
378 /* Generate IRQ on the CPU (any IRQ will cause resched) */
379 leon_send_ipi(cpu, leon_ipi_irq);
382 void leonsmp_ipi_interrupt(void)
384 struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
388 smp_call_function_single_interrupt();
392 smp_call_function_interrupt();
396 smp_resched_interrupt();
400 static struct smp_funcall {
407 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
408 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
411 static DEFINE_SPINLOCK(cross_call_lock);
413 /* Cross calls must be serialized, at least currently. */
414 static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
415 unsigned long arg2, unsigned long arg3,
418 if (smp_processors_ready) {
419 register int high = NR_CPUS - 1;
422 spin_lock_irqsave(&cross_call_lock, flags);
425 /* If you make changes here, make sure gcc generates proper code... */
426 register smpfunc_t f asm("i0") = func;
427 register unsigned long a1 asm("i1") = arg1;
428 register unsigned long a2 asm("i2") = arg2;
429 register unsigned long a3 asm("i3") = arg3;
430 register unsigned long a4 asm("i4") = arg4;
431 register unsigned long a5 asm("i5") = 0;
433 __asm__ __volatile__("std %0, [%6]\n\t"
434 "std %2, [%6 + 8]\n\t"
435 "std %4, [%6 + 16]\n\t" : :
436 "r"(f), "r"(a1), "r"(a2), "r"(a3),
438 "r"(&ccall_info.func));
441 /* Init receive/complete mapping, plus fire the IPI's off. */
445 cpumask_clear_cpu(smp_processor_id(), &mask);
446 cpumask_and(&mask, cpu_online_mask, &mask);
447 for (i = 0; i <= high; i++) {
448 if (cpumask_test_cpu(i, &mask)) {
449 ccall_info.processors_in[i] = 0;
450 ccall_info.processors_out[i] = 0;
451 leon_send_ipi(i, LEON3_IRQ_CROSS_CALL);
462 if (!cpumask_test_cpu(i, &mask))
465 while (!ccall_info.processors_in[i])
467 } while (++i <= high);
471 if (!cpumask_test_cpu(i, &mask))
474 while (!ccall_info.processors_out[i])
476 } while (++i <= high);
479 spin_unlock_irqrestore(&cross_call_lock, flags);
483 /* Running cross calls. */
484 void leon_cross_call_irq(void)
486 int i = smp_processor_id();
488 ccall_info.processors_in[i] = 1;
489 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
490 ccall_info.arg4, ccall_info.arg5);
491 ccall_info.processors_out[i] = 1;
494 static const struct sparc32_ipi_ops leon_ipi_ops = {
495 .cross_call = leon_cross_call,
496 .resched = leon_ipi_resched,
497 .single = leon_ipi_single,
498 .mask_one = leon_ipi_mask_one,
501 void __init leon_init_smp(void)
503 /* Patch ipi15 trap table */
504 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
506 sparc32_ipi_ops = &leon_ipi_ops;