Loading...
1// SPDX-License-Identifier: GPL-2.0
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>
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>
18#include <linux/of.h>
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>
26#include <linux/gfp.h>
27#include <linux/cpu.h>
28#include <linux/clockchips.h>
29
30#include <asm/cacheflush.h>
31#include <asm/tlbflush.h>
32
33#include <asm/ptrace.h>
34#include <linux/atomic.h>
35#include <asm/irq_regs.h>
36#include <asm/traps.h>
37
38#include <asm/delay.h>
39#include <asm/irq.h>
40#include <asm/page.h>
41#include <asm/oplib.h>
42#include <asm/cpudata.h>
43#include <asm/asi.h>
44#include <asm/leon.h>
45#include <asm/leon_amba.h>
46#include <asm/timer.h>
47
48#include "kernel.h"
49
50#include "irq.h"
51
52extern ctxd_t *srmmu_ctx_table_phys;
53static int smp_processors_ready;
54extern volatile unsigned long cpu_callin_map[NR_CPUS];
55extern cpumask_t smp_commenced_mask;
56void leon_configure_cache_smp(void);
57static void leon_ipi_init(void);
58
59/* IRQ number of LEON IPIs */
60int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
61
62static inline unsigned long do_swap(volatile unsigned long *ptr,
63 unsigned long val)
64{
65 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
66 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
67 : "memory");
68 return val;
69}
70
71void leon_cpu_pre_starting(void *arg)
72{
73 leon_configure_cache_smp();
74}
75
76void leon_cpu_pre_online(void *arg)
77{
78 int cpuid = hard_smp_processor_id();
79
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).
84 */
85 do_swap(&cpu_callin_map[cpuid], 1);
86
87 local_ops->cache_all();
88 local_ops->tlb_all();
89
90 /* Fix idle thread fields. */
91 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set[cpuid])
92 : "memory" /* paranoid */);
93
94 /* Attach to the address space of init_task. */
95 mmgrab(&init_mm);
96 current->active_mm = &init_mm;
97
98 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
99 mb();
100}
101
102/*
103 * Cycle through the processors asking the PROM to start each one.
104 */
105
106extern struct linux_prom_registers smp_penguin_ctable;
107
108void leon_configure_cache_smp(void)
109{
110 unsigned long cfg = sparc_leon3_get_dcachecfg();
111 int me = smp_processor_id();
112
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();
118 } else {
119 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
120 sparc_leon3_enable_snooping();
121 } else {
122 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
123 me);
124 sparc_leon3_disable_cache();
125 }
126 }
127
128 local_ops->cache_all();
129 local_ops->tlb_all();
130}
131
132static void leon_smp_setbroadcast(unsigned int mask)
133{
134 int broadcast =
135 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
136 LEON3_IRQMPSTATUS_BROADCAST) & 1);
137 if (!broadcast) {
138 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
139 leon_smp_nrcpus());
140 if (leon_smp_nrcpus() > 1) {
141 BUG();
142 } else {
143 prom_printf("continue anyway\n");
144 return;
145 }
146 }
147 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
148}
149
150int leon_smp_nrcpus(void)
151{
152 int nrcpu =
153 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
154 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
155 return nrcpu;
156}
157
158void __init leon_boot_cpus(void)
159{
160 int nrcpu = leon_smp_nrcpus();
161 int me = smp_processor_id();
162
163 /* Setup IPI */
164 leon_ipi_init();
165
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));
169
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);
173
174 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
175
176 leon_configure_cache_smp();
177 local_ops->cache_all();
178
179}
180
181int leon_boot_one_cpu(int i, struct task_struct *idle)
182{
183 int timeout;
184
185 current_set[i] = task_thread_info(idle);
186
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
191 */
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;
195
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();
200
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);
203
204 /* Wake one CPU */
205 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
206
207 /* wheee... it's going... */
208 for (timeout = 0; timeout < 10000; timeout++) {
209 if (cpu_callin_map[i])
210 break;
211 udelay(200);
212 }
213 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
214
215 if (!(cpu_callin_map[i])) {
216 printk(KERN_ERR "Processor %d is stuck.\n", i);
217 return -ENODEV;
218 } else {
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);
222 }
223
224 local_ops->cache_all();
225 return 0;
226}
227
228void __init leon_smp_done(void)
229{
230
231 int i, first;
232 int *prev;
233
234 /* setup cpu list for irq rotation */
235 first = 0;
236 prev = &first;
237 for (i = 0; i < NR_CPUS; i++) {
238 if (cpu_online(i)) {
239 *prev = i;
240 prev = &cpu_data(i).next;
241 }
242 }
243 *prev = first;
244 local_ops->cache_all();
245
246 /* Free unneeded trap tables */
247 if (!cpu_present(1)) {
248 free_reserved_page(virt_to_page(&trapbase_cpu1));
249 }
250 if (!cpu_present(2)) {
251 free_reserved_page(virt_to_page(&trapbase_cpu2));
252 }
253 if (!cpu_present(3)) {
254 free_reserved_page(virt_to_page(&trapbase_cpu3));
255 }
256 /* Ok, they are spinning and ready to go. */
257 smp_processors_ready = 1;
258
259}
260
261struct leon_ipi_work {
262 int single;
263 int msk;
264 int resched;
265};
266
267static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
268
269/* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
270 * is used for all three types of IPIs.
271 */
272static void __init leon_ipi_init(void)
273{
274 int cpu, len;
275 struct leon_ipi_work *work;
276 struct property *pp;
277 struct device_node *rootnp;
278 struct tt_entry *trap_table;
279 unsigned long flags;
280
281 /* Find IPI IRQ or stick with default value */
282 rootnp = of_find_node_by_path("/ambapp0");
283 if (rootnp) {
284 pp = of_find_property(rootnp, "ipi_num", &len);
285 if (pp && (*(int *)pp->value))
286 leon_ipi_irq = *(int *)pp->value;
287 }
288 printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
289
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);
296
297 for_each_possible_cpu(cpu) {
298 work = &per_cpu(leon_ipi_work, cpu);
299 work->single = work->msk = work->resched = 0;
300 }
301}
302
303static void leon_send_ipi(int cpu, int level)
304{
305 unsigned long mask;
306 mask = leon_get_irqmask(level);
307 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
308}
309
310static void leon_ipi_single(int cpu)
311{
312 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
313
314 /* Mark work */
315 work->single = 1;
316
317 /* Generate IRQ on the CPU */
318 leon_send_ipi(cpu, leon_ipi_irq);
319}
320
321static void leon_ipi_mask_one(int cpu)
322{
323 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
324
325 /* Mark work */
326 work->msk = 1;
327
328 /* Generate IRQ on the CPU */
329 leon_send_ipi(cpu, leon_ipi_irq);
330}
331
332static void leon_ipi_resched(int cpu)
333{
334 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
335
336 /* Mark work */
337 work->resched = 1;
338
339 /* Generate IRQ on the CPU (any IRQ will cause resched) */
340 leon_send_ipi(cpu, leon_ipi_irq);
341}
342
343void leonsmp_ipi_interrupt(void)
344{
345 struct leon_ipi_work *work = this_cpu_ptr(&leon_ipi_work);
346
347 if (work->single) {
348 work->single = 0;
349 smp_call_function_single_interrupt();
350 }
351 if (work->msk) {
352 work->msk = 0;
353 smp_call_function_interrupt();
354 }
355 if (work->resched) {
356 work->resched = 0;
357 smp_resched_interrupt();
358 }
359}
360
361static struct smp_funcall {
362 void *func;
363 unsigned long arg1;
364 unsigned long arg2;
365 unsigned long arg3;
366 unsigned long arg4;
367 unsigned long arg5;
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)));
371
372static DEFINE_SPINLOCK(cross_call_lock);
373
374/* Cross calls must be serialized, at least currently. */
375static void leon_cross_call(void *func, cpumask_t mask, unsigned long arg1,
376 unsigned long arg2, unsigned long arg3,
377 unsigned long arg4)
378{
379 if (smp_processors_ready) {
380 register int high = NR_CPUS - 1;
381 unsigned long flags;
382
383 spin_lock_irqsave(&cross_call_lock, flags);
384
385 {
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;
393
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),
398 "r"(a4), "r"(a5),
399 "r"(&ccall_info.func));
400 }
401
402 /* Init receive/complete mapping, plus fire the IPI's off. */
403 {
404 register int i;
405
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);
413
414 }
415 }
416 }
417
418 {
419 register int i;
420
421 i = 0;
422 do {
423 if (!cpumask_test_cpu(i, &mask))
424 continue;
425
426 while (!ccall_info.processors_in[i])
427 barrier();
428 } while (++i <= high);
429
430 i = 0;
431 do {
432 if (!cpumask_test_cpu(i, &mask))
433 continue;
434
435 while (!ccall_info.processors_out[i])
436 barrier();
437 } while (++i <= high);
438 }
439
440 spin_unlock_irqrestore(&cross_call_lock, flags);
441 }
442}
443
444/* Running cross calls. */
445void leon_cross_call_irq(void)
446{
447 void (*func)(unsigned long, unsigned long, unsigned long, unsigned long,
448 unsigned long) = ccall_info.func;
449 int i = smp_processor_id();
450
451 ccall_info.processors_in[i] = 1;
452 func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3, ccall_info.arg4,
453 ccall_info.arg5);
454 ccall_info.processors_out[i] = 1;
455}
456
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
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
469 sparc32_ipi_ops = &leon_ipi_ops;
470}
1/* leon_smp.c: Sparc-Leon SMP support.
2 *
3 * based on sun4m_smp.c
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
7 */
8
9#include <asm/head.h>
10
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/of.h>
18#include <linux/init.h>
19#include <linux/spinlock.h>
20#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/profile.h>
23#include <linux/pm.h>
24#include <linux/delay.h>
25#include <linux/gfp.h>
26
27#include <asm/cacheflush.h>
28#include <asm/tlbflush.h>
29
30#include <asm/ptrace.h>
31#include <linux/atomic.h>
32#include <asm/irq_regs.h>
33#include <asm/traps.h>
34
35#include <asm/delay.h>
36#include <asm/irq.h>
37#include <asm/page.h>
38#include <asm/pgalloc.h>
39#include <asm/pgtable.h>
40#include <asm/oplib.h>
41#include <asm/cpudata.h>
42#include <asm/asi.h>
43#include <asm/leon.h>
44#include <asm/leon_amba.h>
45
46#include "kernel.h"
47
48#ifdef CONFIG_SPARC_LEON
49
50#include "irq.h"
51
52extern ctxd_t *srmmu_ctx_table_phys;
53static int smp_processors_ready;
54extern volatile unsigned long cpu_callin_map[NR_CPUS];
55extern cpumask_t smp_commenced_mask;
56void __init leon_configure_cache_smp(void);
57static void leon_ipi_init(void);
58
59/* IRQ number of LEON IPIs */
60int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
61
62static inline unsigned long do_swap(volatile unsigned long *ptr,
63 unsigned long val)
64{
65 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
66 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
67 : "memory");
68 return val;
69}
70
71static void smp_setup_percpu_timer(void);
72
73void __cpuinit leon_callin(void)
74{
75 int cpuid = hard_smpleon_processor_id();
76
77 local_flush_cache_all();
78 local_flush_tlb_all();
79 leon_configure_cache_smp();
80
81 /* Get our local ticker going. */
82 smp_setup_percpu_timer();
83
84 calibrate_delay();
85 smp_store_cpu_info(cpuid);
86
87 local_flush_cache_all();
88 local_flush_tlb_all();
89
90 /*
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.
95 * Allow master to continue.
96 */
97 do_swap(&cpu_callin_map[cpuid], 1);
98
99 local_flush_cache_all();
100 local_flush_tlb_all();
101
102 /* Fix idle thread fields. */
103 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set[cpuid])
104 : "memory" /* paranoid */);
105
106 /* Attach to the address space of init_task. */
107 atomic_inc(&init_mm.mm_count);
108 current->active_mm = &init_mm;
109
110 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
111 mb();
112
113 local_irq_enable();
114 set_cpu_online(cpuid, true);
115}
116
117/*
118 * Cycle through the processors asking the PROM to start each one.
119 */
120
121extern struct linux_prom_registers smp_penguin_ctable;
122
123void __init leon_configure_cache_smp(void)
124{
125 unsigned long cfg = sparc_leon3_get_dcachecfg();
126 int me = smp_processor_id();
127
128 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
129 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
130 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
131 (unsigned int)cfg, (unsigned int)me);
132 sparc_leon3_disable_cache();
133 } else {
134 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
135 sparc_leon3_enable_snooping();
136 } else {
137 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
138 me);
139 sparc_leon3_disable_cache();
140 }
141 }
142
143 local_flush_cache_all();
144 local_flush_tlb_all();
145}
146
147void leon_smp_setbroadcast(unsigned int mask)
148{
149 int broadcast =
150 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
151 LEON3_IRQMPSTATUS_BROADCAST) & 1);
152 if (!broadcast) {
153 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
154 leon_smp_nrcpus());
155 if (leon_smp_nrcpus() > 1) {
156 BUG();
157 } else {
158 prom_printf("continue anyway\n");
159 return;
160 }
161 }
162 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
163}
164
165unsigned int leon_smp_getbroadcast(void)
166{
167 unsigned int mask;
168 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
169 return mask;
170}
171
172int leon_smp_nrcpus(void)
173{
174 int nrcpu =
175 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
176 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
177 return nrcpu;
178}
179
180void __init leon_boot_cpus(void)
181{
182 int nrcpu = leon_smp_nrcpus();
183 int me = smp_processor_id();
184
185 /* Setup IPI */
186 leon_ipi_init();
187
188 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
189 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
190 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
191
192 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
193 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
194 leon_enable_irq_cpu(leon_ipi_irq, me);
195
196 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
197
198 leon_configure_cache_smp();
199 smp_setup_percpu_timer();
200 local_flush_cache_all();
201
202}
203
204int __cpuinit leon_boot_one_cpu(int i)
205{
206
207 struct task_struct *p;
208 int timeout;
209
210 /* Cook up an idler for this guy. */
211 p = fork_idle(i);
212
213 current_set[i] = task_thread_info(p);
214
215 /* See trampoline.S:leon_smp_cpu_startup for details...
216 * Initialize the contexts table
217 * Since the call to prom_startcpu() trashes the structure,
218 * we need to re-initialize it for each cpu
219 */
220 smp_penguin_ctable.which_io = 0;
221 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
222 smp_penguin_ctable.reg_size = 0;
223
224 /* whirrr, whirrr, whirrrrrrrrr... */
225 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
226 (unsigned int)&leon3_irqctrl_regs->mpstatus);
227 local_flush_cache_all();
228
229 /* Make sure all IRQs are of from the start for this new CPU */
230 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
231
232 /* Wake one CPU */
233 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
234
235 /* wheee... it's going... */
236 for (timeout = 0; timeout < 10000; timeout++) {
237 if (cpu_callin_map[i])
238 break;
239 udelay(200);
240 }
241 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
242
243 if (!(cpu_callin_map[i])) {
244 printk(KERN_ERR "Processor %d is stuck.\n", i);
245 return -ENODEV;
246 } else {
247 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
248 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
249 leon_enable_irq_cpu(leon_ipi_irq, i);
250 }
251
252 local_flush_cache_all();
253 return 0;
254}
255
256void __init leon_smp_done(void)
257{
258
259 int i, first;
260 int *prev;
261
262 /* setup cpu list for irq rotation */
263 first = 0;
264 prev = &first;
265 for (i = 0; i < NR_CPUS; i++) {
266 if (cpu_online(i)) {
267 *prev = i;
268 prev = &cpu_data(i).next;
269 }
270 }
271 *prev = first;
272 local_flush_cache_all();
273
274 /* Free unneeded trap tables */
275 if (!cpu_present(1)) {
276 ClearPageReserved(virt_to_page(&trapbase_cpu1));
277 init_page_count(virt_to_page(&trapbase_cpu1));
278 free_page((unsigned long)&trapbase_cpu1);
279 totalram_pages++;
280 num_physpages++;
281 }
282 if (!cpu_present(2)) {
283 ClearPageReserved(virt_to_page(&trapbase_cpu2));
284 init_page_count(virt_to_page(&trapbase_cpu2));
285 free_page((unsigned long)&trapbase_cpu2);
286 totalram_pages++;
287 num_physpages++;
288 }
289 if (!cpu_present(3)) {
290 ClearPageReserved(virt_to_page(&trapbase_cpu3));
291 init_page_count(virt_to_page(&trapbase_cpu3));
292 free_page((unsigned long)&trapbase_cpu3);
293 totalram_pages++;
294 num_physpages++;
295 }
296 /* Ok, they are spinning and ready to go. */
297 smp_processors_ready = 1;
298
299}
300
301void leon_irq_rotate(int cpu)
302{
303}
304
305struct leon_ipi_work {
306 int single;
307 int msk;
308 int resched;
309};
310
311static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
312
313/* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
314 * is used for all three types of IPIs.
315 */
316static void __init leon_ipi_init(void)
317{
318 int cpu, len;
319 struct leon_ipi_work *work;
320 struct property *pp;
321 struct device_node *rootnp;
322 struct tt_entry *trap_table;
323 unsigned long flags;
324
325 /* Find IPI IRQ or stick with default value */
326 rootnp = of_find_node_by_path("/ambapp0");
327 if (rootnp) {
328 pp = of_find_property(rootnp, "ipi_num", &len);
329 if (pp && (*(int *)pp->value))
330 leon_ipi_irq = *(int *)pp->value;
331 }
332 printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
333
334 /* Adjust so that we jump directly to smpleon_ipi */
335 local_irq_save(flags);
336 trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
337 trap_table->inst_three += smpleon_ipi - real_irq_entry;
338 local_flush_cache_all();
339 local_irq_restore(flags);
340
341 for_each_possible_cpu(cpu) {
342 work = &per_cpu(leon_ipi_work, cpu);
343 work->single = work->msk = work->resched = 0;
344 }
345}
346
347static void leon_ipi_single(int cpu)
348{
349 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
350
351 /* Mark work */
352 work->single = 1;
353
354 /* Generate IRQ on the CPU */
355 set_cpu_int(cpu, leon_ipi_irq);
356}
357
358static void leon_ipi_mask_one(int cpu)
359{
360 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
361
362 /* Mark work */
363 work->msk = 1;
364
365 /* Generate IRQ on the CPU */
366 set_cpu_int(cpu, leon_ipi_irq);
367}
368
369static void leon_ipi_resched(int cpu)
370{
371 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
372
373 /* Mark work */
374 work->resched = 1;
375
376 /* Generate IRQ on the CPU (any IRQ will cause resched) */
377 set_cpu_int(cpu, leon_ipi_irq);
378}
379
380void leonsmp_ipi_interrupt(void)
381{
382 struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
383
384 if (work->single) {
385 work->single = 0;
386 smp_call_function_single_interrupt();
387 }
388 if (work->msk) {
389 work->msk = 0;
390 smp_call_function_interrupt();
391 }
392 if (work->resched) {
393 work->resched = 0;
394 smp_resched_interrupt();
395 }
396}
397
398static struct smp_funcall {
399 smpfunc_t func;
400 unsigned long arg1;
401 unsigned long arg2;
402 unsigned long arg3;
403 unsigned long arg4;
404 unsigned long arg5;
405 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
406 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
407} ccall_info;
408
409static DEFINE_SPINLOCK(cross_call_lock);
410
411/* Cross calls must be serialized, at least currently. */
412static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
413 unsigned long arg2, unsigned long arg3,
414 unsigned long arg4)
415{
416 if (smp_processors_ready) {
417 register int high = NR_CPUS - 1;
418 unsigned long flags;
419
420 spin_lock_irqsave(&cross_call_lock, flags);
421
422 {
423 /* If you make changes here, make sure gcc generates proper code... */
424 register smpfunc_t f asm("i0") = func;
425 register unsigned long a1 asm("i1") = arg1;
426 register unsigned long a2 asm("i2") = arg2;
427 register unsigned long a3 asm("i3") = arg3;
428 register unsigned long a4 asm("i4") = arg4;
429 register unsigned long a5 asm("i5") = 0;
430
431 __asm__ __volatile__("std %0, [%6]\n\t"
432 "std %2, [%6 + 8]\n\t"
433 "std %4, [%6 + 16]\n\t" : :
434 "r"(f), "r"(a1), "r"(a2), "r"(a3),
435 "r"(a4), "r"(a5),
436 "r"(&ccall_info.func));
437 }
438
439 /* Init receive/complete mapping, plus fire the IPI's off. */
440 {
441 register int i;
442
443 cpumask_clear_cpu(smp_processor_id(), &mask);
444 cpumask_and(&mask, cpu_online_mask, &mask);
445 for (i = 0; i <= high; i++) {
446 if (cpumask_test_cpu(i, &mask)) {
447 ccall_info.processors_in[i] = 0;
448 ccall_info.processors_out[i] = 0;
449 set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
450
451 }
452 }
453 }
454
455 {
456 register int i;
457
458 i = 0;
459 do {
460 if (!cpumask_test_cpu(i, &mask))
461 continue;
462
463 while (!ccall_info.processors_in[i])
464 barrier();
465 } while (++i <= high);
466
467 i = 0;
468 do {
469 if (!cpumask_test_cpu(i, &mask))
470 continue;
471
472 while (!ccall_info.processors_out[i])
473 barrier();
474 } while (++i <= high);
475 }
476
477 spin_unlock_irqrestore(&cross_call_lock, flags);
478 }
479}
480
481/* Running cross calls. */
482void leon_cross_call_irq(void)
483{
484 int i = smp_processor_id();
485
486 ccall_info.processors_in[i] = 1;
487 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
488 ccall_info.arg4, ccall_info.arg5);
489 ccall_info.processors_out[i] = 1;
490}
491
492irqreturn_t leon_percpu_timer_interrupt(int irq, void *unused)
493{
494 int cpu = smp_processor_id();
495
496 leon_clear_profile_irq(cpu);
497
498 profile_tick(CPU_PROFILING);
499
500 if (!--prof_counter(cpu)) {
501 int user = user_mode(get_irq_regs());
502
503 update_process_times(user);
504
505 prof_counter(cpu) = prof_multiplier(cpu);
506 }
507
508 return IRQ_HANDLED;
509}
510
511static void __init smp_setup_percpu_timer(void)
512{
513 int cpu = smp_processor_id();
514
515 prof_counter(cpu) = prof_multiplier(cpu) = 1;
516}
517
518void __init leon_blackbox_id(unsigned *addr)
519{
520 int rd = *addr & 0x3e000000;
521 int rs1 = rd >> 11;
522
523 /* patch places where ___b_hard_smp_processor_id appears */
524 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
525 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
526 addr[2] = 0x01000000; /* nop */
527}
528
529void __init leon_blackbox_current(unsigned *addr)
530{
531 int rd = *addr & 0x3e000000;
532 int rs1 = rd >> 11;
533
534 /* patch LOAD_CURRENT macro where ___b_load_current appears */
535 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
536 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
537 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
538
539}
540
541void __init leon_init_smp(void)
542{
543 /* Patch ipi15 trap table */
544 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
545
546 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
547 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
548 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
549 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
550 BTFIXUPCALL_NORM);
551 BTFIXUPSET_CALL(smp_ipi_resched, leon_ipi_resched, BTFIXUPCALL_NORM);
552 BTFIXUPSET_CALL(smp_ipi_single, leon_ipi_single, BTFIXUPCALL_NORM);
553 BTFIXUPSET_CALL(smp_ipi_mask_one, leon_ipi_mask_one, BTFIXUPCALL_NORM);
554}
555
556#endif /* CONFIG_SPARC_LEON */