]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/sparc/kernel/leon_smp.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-bionic-kernel.git] / arch / sparc / kernel / leon_smp.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
8401707f
KE
2/* leon_smp.c: Sparc-Leon SMP support.
3 *
4 * based on sun4m_smp.c
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
8 */
9
10#include <asm/head.h>
11
12#include <linux/kernel.h>
68e21be2 13#include <linux/sched/mm.h>
8401707f
KE
14#include <linux/threads.h>
15#include <linux/smp.h>
8401707f
KE
16#include <linux/interrupt.h>
17#include <linux/kernel_stat.h>
1ca0c808 18#include <linux/of.h>
8401707f
KE
19#include <linux/init.h>
20#include <linux/spinlock.h>
21#include <linux/mm.h>
22#include <linux/swap.h>
23#include <linux/profile.h>
24#include <linux/pm.h>
25#include <linux/delay.h>
5a0e3ad6 26#include <linux/gfp.h>
62f08283
TK
27#include <linux/cpu.h>
28#include <linux/clockchips.h>
8401707f
KE
29
30#include <asm/cacheflush.h>
31#include <asm/tlbflush.h>
32
33#include <asm/ptrace.h>
60063497 34#include <linux/atomic.h>
8401707f 35#include <asm/irq_regs.h>
1ca0c808 36#include <asm/traps.h>
8401707f
KE
37
38#include <asm/delay.h>
39#include <asm/irq.h>
40#include <asm/page.h>
41#include <asm/pgalloc.h>
42#include <asm/pgtable.h>
43#include <asm/oplib.h>
44#include <asm/cpudata.h>
45#include <asm/asi.h>
46#include <asm/leon.h>
47#include <asm/leon_amba.h>
62f08283 48#include <asm/timer.h>
8401707f 49
a2a211cb
SR
50#include "kernel.h"
51
8401707f
KE
52#include "irq.h"
53
54extern ctxd_t *srmmu_ctx_table_phys;
55static int smp_processors_ready;
56extern volatile unsigned long cpu_callin_map[NR_CPUS];
8401707f 57extern cpumask_t smp_commenced_mask;
2066aadd 58void leon_configure_cache_smp(void);
1ca0c808
DH
59static void leon_ipi_init(void);
60
61/* IRQ number of LEON IPIs */
62int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
8401707f
KE
63
64static inline unsigned long do_swap(volatile unsigned long *ptr,
65 unsigned long val)
66{
502279a7
DH
67 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
68 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
8401707f
KE
69 : "memory");
70 return val;
71}
72
2066aadd 73void leon_cpu_pre_starting(void *arg)
8401707f 74{
8401707f 75 leon_configure_cache_smp();
f9fd3488 76}
8401707f 77
2066aadd 78void leon_cpu_pre_online(void *arg)
f9fd3488
SR
79{
80 int cpuid = hard_smp_processor_id();
8401707f 81
f9fd3488
SR
82 /* Allow master to continue. The master will then give us the
83 * go-ahead by setting the smp_commenced_mask and will wait without
84 * timeouts until our setup is completed fully (signified by
85 * our bit being set in the cpu_online_mask).
8401707f
KE
86 */
87 do_swap(&cpu_callin_map[cpuid], 1);
88
5d83d666
DM
89 local_ops->cache_all();
90 local_ops->tlb_all();
8401707f 91
8401707f
KE
92 /* Fix idle thread fields. */
93 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
94 : "memory" /* paranoid */);
95
96 /* Attach to the address space of init_task. */
f1f10076 97 mmgrab(&init_mm);
8401707f
KE
98 current->active_mm = &init_mm;
99
fb1fece5 100 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
8401707f 101 mb();
8401707f
KE
102}
103
104/*
105 * Cycle through the processors asking the PROM to start each one.
106 */
107
108extern struct linux_prom_registers smp_penguin_ctable;
109
2066aadd 110void leon_configure_cache_smp(void)
8401707f
KE
111{
112 unsigned long cfg = sparc_leon3_get_dcachecfg();
113 int me = smp_processor_id();
114
115 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
116 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
117 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
118 (unsigned int)cfg, (unsigned int)me);
119 sparc_leon3_disable_cache();
120 } else {
121 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
122 sparc_leon3_enable_snooping();
123 } else {
124 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
125 me);
126 sparc_leon3_disable_cache();
127 }
128 }
129
5d83d666
DM
130 local_ops->cache_all();
131 local_ops->tlb_all();
8401707f
KE
132}
133
1bbc9060 134static void leon_smp_setbroadcast(unsigned int mask)
8401707f
KE
135{
136 int broadcast =
137 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
138 LEON3_IRQMPSTATUS_BROADCAST) & 1);
139 if (!broadcast) {
140 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
141 leon_smp_nrcpus());
142 if (leon_smp_nrcpus() > 1) {
143 BUG();
144 } else {
145 prom_printf("continue anyway\n");
146 return;
147 }
148 }
149 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
150}
151
8401707f
KE
152int leon_smp_nrcpus(void)
153{
154 int nrcpu =
155 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
156 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
157 return nrcpu;
158}
159
160void __init leon_boot_cpus(void)
161{
162 int nrcpu = leon_smp_nrcpus();
163 int me = smp_processor_id();
164
1ca0c808
DH
165 /* Setup IPI */
166 leon_ipi_init();
167
0da2b300 168 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
8401707f
KE
169 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
170 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
171
172 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
173 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
1ca0c808 174 leon_enable_irq_cpu(leon_ipi_irq, me);
8401707f
KE
175
176 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
177
178 leon_configure_cache_smp();
5d83d666 179 local_ops->cache_all();
8401707f
KE
180
181}
182
2066aadd 183int leon_boot_one_cpu(int i, struct task_struct *idle)
8401707f 184{
8401707f
KE
185 int timeout;
186
f0a2bc7e 187 current_set[i] = task_thread_info(idle);
8401707f
KE
188
189 /* See trampoline.S:leon_smp_cpu_startup for details...
190 * Initialize the contexts table
191 * Since the call to prom_startcpu() trashes the structure,
192 * we need to re-initialize it for each cpu
193 */
194 smp_penguin_ctable.which_io = 0;
195 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
196 smp_penguin_ctable.reg_size = 0;
197
198 /* whirrr, whirrr, whirrrrrrrrr... */
199 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
200 (unsigned int)&leon3_irqctrl_regs->mpstatus);
5d83d666 201 local_ops->cache_all();
8401707f 202
970def65
DH
203 /* Make sure all IRQs are of from the start for this new CPU */
204 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
205
206 /* Wake one CPU */
8401707f
KE
207 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
208
209 /* wheee... it's going... */
210 for (timeout = 0; timeout < 10000; timeout++) {
211 if (cpu_callin_map[i])
212 break;
213 udelay(200);
214 }
0da2b300 215 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
8401707f
KE
216
217 if (!(cpu_callin_map[i])) {
218 printk(KERN_ERR "Processor %d is stuck.\n", i);
219 return -ENODEV;
220 } else {
221 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
222 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
1ca0c808 223 leon_enable_irq_cpu(leon_ipi_irq, i);
8401707f
KE
224 }
225
5d83d666 226 local_ops->cache_all();
8401707f
KE
227 return 0;
228}
229
230void __init leon_smp_done(void)
231{
232
233 int i, first;
234 int *prev;
235
236 /* setup cpu list for irq rotation */
237 first = 0;
238 prev = &first;
239 for (i = 0; i < NR_CPUS; i++) {
240 if (cpu_online(i)) {
241 *prev = i;
242 prev = &cpu_data(i).next;
243 }
244 }
245 *prev = first;
5d83d666 246 local_ops->cache_all();
8401707f
KE
247
248 /* Free unneeded trap tables */
fb1fece5 249 if (!cpu_present(1)) {
70affe45 250 free_reserved_page(virt_to_page(&trapbase_cpu1));
8401707f 251 }
fb1fece5 252 if (!cpu_present(2)) {
70affe45 253 free_reserved_page(virt_to_page(&trapbase_cpu2));
8401707f 254 }
fb1fece5 255 if (!cpu_present(3)) {
70affe45 256 free_reserved_page(virt_to_page(&trapbase_cpu3));
8401707f
KE
257 }
258 /* Ok, they are spinning and ready to go. */
259 smp_processors_ready = 1;
260
261}
262
1ca0c808
DH
263struct leon_ipi_work {
264 int single;
265 int msk;
266 int resched;
267};
268
269static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
270
271/* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
272 * is used for all three types of IPIs.
273 */
274static void __init leon_ipi_init(void)
275{
276 int cpu, len;
277 struct leon_ipi_work *work;
278 struct property *pp;
279 struct device_node *rootnp;
280 struct tt_entry *trap_table;
281 unsigned long flags;
282
283 /* Find IPI IRQ or stick with default value */
284 rootnp = of_find_node_by_path("/ambapp0");
285 if (rootnp) {
286 pp = of_find_property(rootnp, "ipi_num", &len);
287 if (pp && (*(int *)pp->value))
288 leon_ipi_irq = *(int *)pp->value;
289 }
290 printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
291
292 /* Adjust so that we jump directly to smpleon_ipi */
293 local_irq_save(flags);
294 trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
295 trap_table->inst_three += smpleon_ipi - real_irq_entry;
5d83d666 296 local_ops->cache_all();
1ca0c808
DH
297 local_irq_restore(flags);
298
299 for_each_possible_cpu(cpu) {
300 work = &per_cpu(leon_ipi_work, cpu);
301 work->single = work->msk = work->resched = 0;
302 }
303}
304
4ba22b16
SR
305static void leon_send_ipi(int cpu, int level)
306{
307 unsigned long mask;
308 mask = leon_get_irqmask(level);
309 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
310}
311
1ca0c808
DH
312static void leon_ipi_single(int cpu)
313{
314 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
315
316 /* Mark work */
317 work->single = 1;
318
319 /* Generate IRQ on the CPU */
4ba22b16 320 leon_send_ipi(cpu, leon_ipi_irq);
1ca0c808
DH
321}
322
323static void leon_ipi_mask_one(int cpu)
324{
325 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
326
327 /* Mark work */
328 work->msk = 1;
329
330 /* Generate IRQ on the CPU */
4ba22b16 331 leon_send_ipi(cpu, leon_ipi_irq);
1ca0c808
DH
332}
333
334static void leon_ipi_resched(int cpu)
335{
336 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
337
338 /* Mark work */
339 work->resched = 1;
340
341 /* Generate IRQ on the CPU (any IRQ will cause resched) */
4ba22b16 342 leon_send_ipi(cpu, leon_ipi_irq);
1ca0c808
DH
343}
344
345void leonsmp_ipi_interrupt(void)
346{
494fc421 347 struct leon_ipi_work *work = this_cpu_ptr(&leon_ipi_work);
1ca0c808
DH
348
349 if (work->single) {
350 work->single = 0;
351 smp_call_function_single_interrupt();
352 }
353 if (work->msk) {
354 work->msk = 0;
355 smp_call_function_interrupt();
356 }
357 if (work->resched) {
358 work->resched = 0;
359 smp_resched_interrupt();
360 }
361}
362
8401707f
KE
363static struct smp_funcall {
364 smpfunc_t func;
365 unsigned long arg1;
366 unsigned long arg2;
367 unsigned long arg3;
368 unsigned long arg4;
369 unsigned long arg5;
370 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
371 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
92f268ee 372} ccall_info __attribute__((aligned(8)));
8401707f
KE
373
374static DEFINE_SPINLOCK(cross_call_lock);
375
376/* Cross calls must be serialized, at least currently. */
377static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
378 unsigned long arg2, unsigned long arg3,
379 unsigned long arg4)
380{
381 if (smp_processors_ready) {
382 register int high = NR_CPUS - 1;
383 unsigned long flags;
384
385 spin_lock_irqsave(&cross_call_lock, flags);
386
387 {
388 /* If you make changes here, make sure gcc generates proper code... */
389 register smpfunc_t f asm("i0") = func;
390 register unsigned long a1 asm("i1") = arg1;
391 register unsigned long a2 asm("i2") = arg2;
392 register unsigned long a3 asm("i3") = arg3;
393 register unsigned long a4 asm("i4") = arg4;
394 register unsigned long a5 asm("i5") = 0;
395
396 __asm__ __volatile__("std %0, [%6]\n\t"
397 "std %2, [%6 + 8]\n\t"
398 "std %4, [%6 + 16]\n\t" : :
399 "r"(f), "r"(a1), "r"(a2), "r"(a3),
400 "r"(a4), "r"(a5),
401 "r"(&ccall_info.func));
402 }
403
404 /* Init receive/complete mapping, plus fire the IPI's off. */
405 {
406 register int i;
407
fb1fece5
KM
408 cpumask_clear_cpu(smp_processor_id(), &mask);
409 cpumask_and(&mask, cpu_online_mask, &mask);
8401707f 410 for (i = 0; i <= high; i++) {
fb1fece5 411 if (cpumask_test_cpu(i, &mask)) {
8401707f
KE
412 ccall_info.processors_in[i] = 0;
413 ccall_info.processors_out[i] = 0;
4ba22b16 414 leon_send_ipi(i, LEON3_IRQ_CROSS_CALL);
8401707f
KE
415
416 }
417 }
418 }
419
420 {
421 register int i;
422
423 i = 0;
424 do {
fb1fece5 425 if (!cpumask_test_cpu(i, &mask))
8401707f
KE
426 continue;
427
428 while (!ccall_info.processors_in[i])
429 barrier();
430 } while (++i <= high);
431
432 i = 0;
433 do {
fb1fece5 434 if (!cpumask_test_cpu(i, &mask))
8401707f
KE
435 continue;
436
437 while (!ccall_info.processors_out[i])
438 barrier();
439 } while (++i <= high);
440 }
441
442 spin_unlock_irqrestore(&cross_call_lock, flags);
443 }
444}
445
446/* Running cross calls. */
447void leon_cross_call_irq(void)
448{
449 int i = smp_processor_id();
450
451 ccall_info.processors_in[i] = 1;
452 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
453 ccall_info.arg4, ccall_info.arg5);
454 ccall_info.processors_out[i] = 1;
455}
456
4ba22b16
SR
457static 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,
462};
463
8401707f
KE
464void __init leon_init_smp(void)
465{
466 /* Patch ipi15 trap table */
467 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
468
4ba22b16 469 sparc32_ipi_ops = &leon_ipi_ops;
8401707f 470}