1 // SPDX-License-Identifier: GPL-2.0
2 /* leon_smp.c: Sparc-Leon SMP support.
5 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
7 * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
12 #include <linux/kernel.h>
13 #include <linux/sched/mm.h>
14 #include <linux/threads.h>
15 #include <linux/smp.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
22 #include <linux/swap.h>
23 #include <linux/profile.h>
25 #include <linux/delay.h>
26 #include <linux/gfp.h>
27 #include <linux/cpu.h>
28 #include <linux/clockchips.h>
30 #include <asm/cacheflush.h>
31 #include <asm/tlbflush.h>
33 #include <asm/ptrace.h>
34 #include <linux/atomic.h>
35 #include <asm/irq_regs.h>
36 #include <asm/traps.h>
38 #include <asm/delay.h>
41 #include <asm/oplib.h>
42 #include <asm/cpudata.h>
45 #include <asm/leon_amba.h>
46 #include <asm/timer.h>
52 extern ctxd_t
*srmmu_ctx_table_phys
;
53 static int smp_processors_ready
;
54 extern volatile unsigned long cpu_callin_map
[NR_CPUS
];
55 extern cpumask_t smp_commenced_mask
;
56 void leon_configure_cache_smp(void);
57 static void leon_ipi_init(void);
59 /* IRQ number of LEON IPIs */
60 int leon_ipi_irq
= LEON3_IRQ_IPI_DEFAULT
;
62 static inline unsigned long do_swap(volatile unsigned long *ptr
,
65 __asm__
__volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val
)
66 : "0"(val
), "r"(ptr
), "i"(ASI_LEON_DCACHE_MISS
)
71 void leon_cpu_pre_starting(void *arg
)
73 leon_configure_cache_smp();
76 void leon_cpu_pre_online(void *arg
)
78 int cpuid
= hard_smp_processor_id();
80 /* Allow master to continue. The master will then give us the
81 * go-ahead by setting the smp_commenced_mask and will wait without
82 * timeouts until our setup is completed fully (signified by
83 * our bit being set in the cpu_online_mask).
85 do_swap(&cpu_callin_map
[cpuid
], 1);
87 local_ops
->cache_all();
90 /* Fix idle thread fields. */
91 __asm__
__volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set
[cpuid
])
92 : "memory" /* paranoid */);
94 /* Attach to the address space of init_task. */
96 current
->active_mm
= &init_mm
;
98 while (!cpumask_test_cpu(cpuid
, &smp_commenced_mask
))
103 * Cycle through the processors asking the PROM to start each one.
106 extern struct linux_prom_registers smp_penguin_ctable
;
108 void leon_configure_cache_smp(void)
110 unsigned long cfg
= sparc_leon3_get_dcachecfg();
111 int me
= smp_processor_id();
113 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg
) > 4) {
114 printk(KERN_INFO
"Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
115 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg
),
116 (unsigned int)cfg
, (unsigned int)me
);
117 sparc_leon3_disable_cache();
119 if (cfg
& ASI_LEON3_SYSCTRL_CFG_SNOOPING
) {
120 sparc_leon3_enable_snooping();
122 printk(KERN_INFO
"Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
124 sparc_leon3_disable_cache();
128 local_ops
->cache_all();
129 local_ops
->tlb_all();
132 static void leon_smp_setbroadcast(unsigned int mask
)
135 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs
->mpstatus
)) >>
136 LEON3_IRQMPSTATUS_BROADCAST
) & 1);
138 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
140 if (leon_smp_nrcpus() > 1) {
143 prom_printf("continue anyway\n");
147 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs
->mpbroadcast
), mask
);
150 int leon_smp_nrcpus(void)
153 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs
->mpstatus
)) >>
154 LEON3_IRQMPSTATUS_CPUNR
) & 0xf) + 1;
158 void __init
leon_boot_cpus(void)
160 int nrcpu
= leon_smp_nrcpus();
161 int me
= smp_processor_id();
166 printk(KERN_INFO
"%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me
,
167 (unsigned int)nrcpu
, (unsigned int)NR_CPUS
,
168 (unsigned int)&(leon3_irqctrl_regs
->mpstatus
));
170 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL
, me
);
171 leon_enable_irq_cpu(LEON3_IRQ_TICKER
, me
);
172 leon_enable_irq_cpu(leon_ipi_irq
, me
);
174 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER
);
176 leon_configure_cache_smp();
177 local_ops
->cache_all();
181 int leon_boot_one_cpu(int i
, struct task_struct
*idle
)
185 current_set
[i
] = task_thread_info(idle
);
187 /* See trampoline.S:leon_smp_cpu_startup for details...
188 * Initialize the contexts table
189 * Since the call to prom_startcpu() trashes the structure,
190 * we need to re-initialize it for each cpu
192 smp_penguin_ctable
.which_io
= 0;
193 smp_penguin_ctable
.phys_addr
= (unsigned int)srmmu_ctx_table_phys
;
194 smp_penguin_ctable
.reg_size
= 0;
196 /* whirrr, whirrr, whirrrrrrrrr... */
197 printk(KERN_INFO
"Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i
,
198 (unsigned int)&leon3_irqctrl_regs
->mpstatus
);
199 local_ops
->cache_all();
201 /* Make sure all IRQs are of from the start for this new CPU */
202 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs
->mask
[i
], 0);
205 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs
->mpstatus
), 1 << i
);
207 /* wheee... it's going... */
208 for (timeout
= 0; timeout
< 10000; timeout
++) {
209 if (cpu_callin_map
[i
])
213 printk(KERN_INFO
"Started CPU %d\n", (unsigned int)i
);
215 if (!(cpu_callin_map
[i
])) {
216 printk(KERN_ERR
"Processor %d is stuck.\n", i
);
219 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL
, i
);
220 leon_enable_irq_cpu(LEON3_IRQ_TICKER
, i
);
221 leon_enable_irq_cpu(leon_ipi_irq
, i
);
224 local_ops
->cache_all();
228 void __init
leon_smp_done(void)
234 /* setup cpu list for irq rotation */
237 for (i
= 0; i
< NR_CPUS
; i
++) {
240 prev
= &cpu_data(i
).next
;
244 local_ops
->cache_all();
246 /* Free unneeded trap tables */
247 if (!cpu_present(1)) {
248 free_reserved_page(virt_to_page(&trapbase_cpu1
[0]));
250 if (!cpu_present(2)) {
251 free_reserved_page(virt_to_page(&trapbase_cpu2
[0]));
253 if (!cpu_present(3)) {
254 free_reserved_page(virt_to_page(&trapbase_cpu3
[0]));
256 /* Ok, they are spinning and ready to go. */
257 smp_processors_ready
= 1;
261 struct leon_ipi_work
{
267 static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work
, leon_ipi_work
);
269 /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
270 * is used for all three types of IPIs.
272 static void __init
leon_ipi_init(void)
275 struct leon_ipi_work
*work
;
277 struct device_node
*rootnp
;
278 struct tt_entry
*trap_table
;
281 /* Find IPI IRQ or stick with default value */
282 rootnp
= of_find_node_by_path("/ambapp0");
284 pp
= of_find_property(rootnp
, "ipi_num", &len
);
285 if (pp
&& (*(int *)pp
->value
))
286 leon_ipi_irq
= *(int *)pp
->value
;
288 printk(KERN_INFO
"leon: SMP IPIs at IRQ %d\n", leon_ipi_irq
);
290 /* Adjust so that we jump directly to smpleon_ipi */
291 local_irq_save(flags
);
292 trap_table
= &sparc_ttable
[SP_TRAP_IRQ1
+ (leon_ipi_irq
- 1)];
293 trap_table
->inst_three
+= smpleon_ipi
- real_irq_entry
;
294 local_ops
->cache_all();
295 local_irq_restore(flags
);
297 for_each_possible_cpu(cpu
) {
298 work
= &per_cpu(leon_ipi_work
, cpu
);
299 work
->single
= work
->msk
= work
->resched
= 0;
303 static void leon_send_ipi(int cpu
, int level
)
306 mask
= leon_get_irqmask(level
);
307 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs
->force
[cpu
], mask
);
310 static void leon_ipi_single(int cpu
)
312 struct leon_ipi_work
*work
= &per_cpu(leon_ipi_work
, cpu
);
317 /* Generate IRQ on the CPU */
318 leon_send_ipi(cpu
, leon_ipi_irq
);
321 static void leon_ipi_mask_one(int cpu
)
323 struct leon_ipi_work
*work
= &per_cpu(leon_ipi_work
, cpu
);
328 /* Generate IRQ on the CPU */
329 leon_send_ipi(cpu
, leon_ipi_irq
);
332 static void leon_ipi_resched(int cpu
)
334 struct leon_ipi_work
*work
= &per_cpu(leon_ipi_work
, cpu
);
339 /* Generate IRQ on the CPU (any IRQ will cause resched) */
340 leon_send_ipi(cpu
, leon_ipi_irq
);
343 void leonsmp_ipi_interrupt(void)
345 struct leon_ipi_work
*work
= this_cpu_ptr(&leon_ipi_work
);
349 smp_call_function_single_interrupt();
353 smp_call_function_interrupt();
357 smp_resched_interrupt();
361 static struct smp_funcall
{
368 unsigned long processors_in
[NR_CPUS
]; /* Set when ipi entered. */
369 unsigned long processors_out
[NR_CPUS
]; /* Set when ipi exited. */
370 } ccall_info
__attribute__((aligned(8)));
372 static DEFINE_SPINLOCK(cross_call_lock
);
374 /* Cross calls must be serialized, at least currently. */
375 static void leon_cross_call(void *func
, cpumask_t mask
, unsigned long arg1
,
376 unsigned long arg2
, unsigned long arg3
,
379 if (smp_processors_ready
) {
380 register int high
= NR_CPUS
- 1;
383 spin_lock_irqsave(&cross_call_lock
, flags
);
386 /* If you make changes here, make sure gcc generates proper code... */
387 register void *f
asm("i0") = func
;
388 register unsigned long a1
asm("i1") = arg1
;
389 register unsigned long a2
asm("i2") = arg2
;
390 register unsigned long a3
asm("i3") = arg3
;
391 register unsigned long a4
asm("i4") = arg4
;
392 register unsigned long a5
asm("i5") = 0;
394 __asm__
__volatile__("std %0, [%6]\n\t"
395 "std %2, [%6 + 8]\n\t"
396 "std %4, [%6 + 16]\n\t" : :
397 "r"(f
), "r"(a1
), "r"(a2
), "r"(a3
),
399 "r"(&ccall_info
.func
));
402 /* Init receive/complete mapping, plus fire the IPI's off. */
406 cpumask_clear_cpu(smp_processor_id(), &mask
);
407 cpumask_and(&mask
, cpu_online_mask
, &mask
);
408 for (i
= 0; i
<= high
; i
++) {
409 if (cpumask_test_cpu(i
, &mask
)) {
410 ccall_info
.processors_in
[i
] = 0;
411 ccall_info
.processors_out
[i
] = 0;
412 leon_send_ipi(i
, LEON3_IRQ_CROSS_CALL
);
423 if (!cpumask_test_cpu(i
, &mask
))
426 while (!ccall_info
.processors_in
[i
])
428 } while (++i
<= high
);
432 if (!cpumask_test_cpu(i
, &mask
))
435 while (!ccall_info
.processors_out
[i
])
437 } while (++i
<= high
);
440 spin_unlock_irqrestore(&cross_call_lock
, flags
);
444 /* Running cross calls. */
445 void leon_cross_call_irq(void)
447 void (*func
)(unsigned long, unsigned long, unsigned long, unsigned long,
448 unsigned long) = ccall_info
.func
;
449 int i
= smp_processor_id();
451 ccall_info
.processors_in
[i
] = 1;
452 func(ccall_info
.arg1
, ccall_info
.arg2
, ccall_info
.arg3
, ccall_info
.arg4
,
454 ccall_info
.processors_out
[i
] = 1;
457 static const struct sparc32_ipi_ops leon_ipi_ops
= {
458 .cross_call
= leon_cross_call
,
459 .resched
= leon_ipi_resched
,
460 .single
= leon_ipi_single
,
461 .mask_one
= leon_ipi_mask_one
,
464 void __init
leon_init_smp(void)
466 /* Patch ipi15 trap table */
467 t_nmi
[1] = t_nmi
[1] + (linux_trap_ipi15_leon
- linux_trap_ipi15_sun4m
);
469 sparc32_ipi_ops
= &leon_ipi_ops
;