blob: f3e3630e31a3a38de68fbb60b577f9f908273ea8 [file] [log] [blame]
Konrad Eisele84017072009-08-31 22:08:13 +00001/* 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>
Konrad Eisele84017072009-08-31 22:08:13 +000015#include <linux/interrupt.h>
16#include <linux/kernel_stat.h>
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +000017#include <linux/of.h>
Konrad Eisele84017072009-08-31 22:08:13 +000018#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Tkhai Kirill62f08282012-04-04 21:49:26 +020026#include <linux/cpu.h>
27#include <linux/clockchips.h>
Konrad Eisele84017072009-08-31 22:08:13 +000028
29#include <asm/cacheflush.h>
30#include <asm/tlbflush.h>
31
32#include <asm/ptrace.h>
Arun Sharma600634972011-07-26 16:09:06 -070033#include <linux/atomic.h>
Konrad Eisele84017072009-08-31 22:08:13 +000034#include <asm/irq_regs.h>
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +000035#include <asm/traps.h>
Konrad Eisele84017072009-08-31 22:08:13 +000036
37#include <asm/delay.h>
38#include <asm/irq.h>
39#include <asm/page.h>
40#include <asm/pgalloc.h>
41#include <asm/pgtable.h>
42#include <asm/oplib.h>
43#include <asm/cpudata.h>
44#include <asm/asi.h>
45#include <asm/leon.h>
46#include <asm/leon_amba.h>
Tkhai Kirill62f08282012-04-04 21:49:26 +020047#include <asm/timer.h>
Konrad Eisele84017072009-08-31 22:08:13 +000048
Sam Ravnborga2a211c2011-02-25 22:59:20 -080049#include "kernel.h"
50
Konrad Eisele84017072009-08-31 22:08:13 +000051#ifdef CONFIG_SPARC_LEON
52
53#include "irq.h"
54
55extern ctxd_t *srmmu_ctx_table_phys;
56static int smp_processors_ready;
57extern volatile unsigned long cpu_callin_map[NR_CPUS];
Konrad Eisele84017072009-08-31 22:08:13 +000058extern cpumask_t smp_commenced_mask;
59void __init leon_configure_cache_smp(void);
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +000060static void leon_ipi_init(void);
61
62/* IRQ number of LEON IPIs */
63int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
Konrad Eisele84017072009-08-31 22:08:13 +000064
65static inline unsigned long do_swap(volatile unsigned long *ptr,
66 unsigned long val)
67{
Daniel Hellstrom502279a2010-10-25 21:23:46 +000068 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
69 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
Konrad Eisele84017072009-08-31 22:08:13 +000070 : "memory");
71 return val;
72}
73
Konrad Eisele84017072009-08-31 22:08:13 +000074void __cpuinit leon_callin(void)
75{
76 int cpuid = hard_smpleon_processor_id();
77
David S. Miller5d83d662012-05-13 20:49:31 -070078 local_ops->cache_all();
79 local_ops->tlb_all();
Konrad Eisele84017072009-08-31 22:08:13 +000080 leon_configure_cache_smp();
81
Yong Zhange9a5ea12012-04-19 20:28:32 +000082 notify_cpu_starting(cpuid);
83
Konrad Eisele84017072009-08-31 22:08:13 +000084 /* Get our local ticker going. */
Tkhai Kirill62f08282012-04-04 21:49:26 +020085 register_percpu_ce(cpuid);
Konrad Eisele84017072009-08-31 22:08:13 +000086
87 calibrate_delay();
88 smp_store_cpu_info(cpuid);
89
David S. Miller5d83d662012-05-13 20:49:31 -070090 local_ops->cache_all();
91 local_ops->tlb_all();
Konrad Eisele84017072009-08-31 22:08:13 +000092
93 /*
94 * Unblock the master CPU _only_ when the scheduler state
95 * of all secondary CPUs will be up-to-date, so after
96 * the SMP initialization the master will be just allowed
97 * to call the scheduler code.
98 * Allow master to continue.
99 */
100 do_swap(&cpu_callin_map[cpuid], 1);
101
David S. Miller5d83d662012-05-13 20:49:31 -0700102 local_ops->cache_all();
103 local_ops->tlb_all();
Konrad Eisele84017072009-08-31 22:08:13 +0000104
Konrad Eisele84017072009-08-31 22:08:13 +0000105 /* Fix idle thread fields. */
106 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
107 : "memory" /* paranoid */);
108
109 /* Attach to the address space of init_task. */
110 atomic_inc(&init_mm.mm_count);
111 current->active_mm = &init_mm;
112
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700113 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
Konrad Eisele84017072009-08-31 22:08:13 +0000114 mb();
115
116 local_irq_enable();
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700117 set_cpu_online(cpuid, true);
Konrad Eisele84017072009-08-31 22:08:13 +0000118}
119
120/*
121 * Cycle through the processors asking the PROM to start each one.
122 */
123
124extern struct linux_prom_registers smp_penguin_ctable;
125
126void __init leon_configure_cache_smp(void)
127{
128 unsigned long cfg = sparc_leon3_get_dcachecfg();
129 int me = smp_processor_id();
130
131 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
132 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
133 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
134 (unsigned int)cfg, (unsigned int)me);
135 sparc_leon3_disable_cache();
136 } else {
137 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
138 sparc_leon3_enable_snooping();
139 } else {
140 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
141 me);
142 sparc_leon3_disable_cache();
143 }
144 }
145
David S. Miller5d83d662012-05-13 20:49:31 -0700146 local_ops->cache_all();
147 local_ops->tlb_all();
Konrad Eisele84017072009-08-31 22:08:13 +0000148}
149
150void leon_smp_setbroadcast(unsigned int mask)
151{
152 int broadcast =
153 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
154 LEON3_IRQMPSTATUS_BROADCAST) & 1);
155 if (!broadcast) {
156 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
157 leon_smp_nrcpus());
158 if (leon_smp_nrcpus() > 1) {
159 BUG();
160 } else {
161 prom_printf("continue anyway\n");
162 return;
163 }
164 }
165 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
166}
167
168unsigned int leon_smp_getbroadcast(void)
169{
170 unsigned int mask;
171 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
172 return mask;
173}
174
175int leon_smp_nrcpus(void)
176{
177 int nrcpu =
178 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
179 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
180 return nrcpu;
181}
182
183void __init leon_boot_cpus(void)
184{
185 int nrcpu = leon_smp_nrcpus();
186 int me = smp_processor_id();
187
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +0000188 /* Setup IPI */
189 leon_ipi_init();
190
Frans Pop0da2b302010-02-12 12:08:51 -0800191 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
Konrad Eisele84017072009-08-31 22:08:13 +0000192 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
193 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
194
195 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
196 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +0000197 leon_enable_irq_cpu(leon_ipi_irq, me);
Konrad Eisele84017072009-08-31 22:08:13 +0000198
199 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
200
201 leon_configure_cache_smp();
David S. Miller5d83d662012-05-13 20:49:31 -0700202 local_ops->cache_all();
Konrad Eisele84017072009-08-31 22:08:13 +0000203
204}
205
206int __cpuinit leon_boot_one_cpu(int i)
207{
208
209 struct task_struct *p;
210 int timeout;
211
212 /* Cook up an idler for this guy. */
213 p = fork_idle(i);
214
215 current_set[i] = task_thread_info(p);
216
217 /* See trampoline.S:leon_smp_cpu_startup for details...
218 * Initialize the contexts table
219 * Since the call to prom_startcpu() trashes the structure,
220 * we need to re-initialize it for each cpu
221 */
222 smp_penguin_ctable.which_io = 0;
223 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
224 smp_penguin_ctable.reg_size = 0;
225
226 /* whirrr, whirrr, whirrrrrrrrr... */
227 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
228 (unsigned int)&leon3_irqctrl_regs->mpstatus);
David S. Miller5d83d662012-05-13 20:49:31 -0700229 local_ops->cache_all();
Konrad Eisele84017072009-08-31 22:08:13 +0000230
Daniel Hellstrom970def62011-04-21 04:20:25 +0000231 /* Make sure all IRQs are of from the start for this new CPU */
232 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
233
234 /* Wake one CPU */
Konrad Eisele84017072009-08-31 22:08:13 +0000235 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
236
237 /* wheee... it's going... */
238 for (timeout = 0; timeout < 10000; timeout++) {
239 if (cpu_callin_map[i])
240 break;
241 udelay(200);
242 }
Frans Pop0da2b302010-02-12 12:08:51 -0800243 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
Konrad Eisele84017072009-08-31 22:08:13 +0000244
245 if (!(cpu_callin_map[i])) {
246 printk(KERN_ERR "Processor %d is stuck.\n", i);
247 return -ENODEV;
248 } else {
249 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
250 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +0000251 leon_enable_irq_cpu(leon_ipi_irq, i);
Konrad Eisele84017072009-08-31 22:08:13 +0000252 }
253
David S. Miller5d83d662012-05-13 20:49:31 -0700254 local_ops->cache_all();
Konrad Eisele84017072009-08-31 22:08:13 +0000255 return 0;
256}
257
258void __init leon_smp_done(void)
259{
260
261 int i, first;
262 int *prev;
263
264 /* setup cpu list for irq rotation */
265 first = 0;
266 prev = &first;
267 for (i = 0; i < NR_CPUS; i++) {
268 if (cpu_online(i)) {
269 *prev = i;
270 prev = &cpu_data(i).next;
271 }
272 }
273 *prev = first;
David S. Miller5d83d662012-05-13 20:49:31 -0700274 local_ops->cache_all();
Konrad Eisele84017072009-08-31 22:08:13 +0000275
276 /* Free unneeded trap tables */
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700277 if (!cpu_present(1)) {
Sam Ravnborga2a211c2011-02-25 22:59:20 -0800278 ClearPageReserved(virt_to_page(&trapbase_cpu1));
279 init_page_count(virt_to_page(&trapbase_cpu1));
280 free_page((unsigned long)&trapbase_cpu1);
Konrad Eisele84017072009-08-31 22:08:13 +0000281 totalram_pages++;
282 num_physpages++;
283 }
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700284 if (!cpu_present(2)) {
Sam Ravnborga2a211c2011-02-25 22:59:20 -0800285 ClearPageReserved(virt_to_page(&trapbase_cpu2));
286 init_page_count(virt_to_page(&trapbase_cpu2));
287 free_page((unsigned long)&trapbase_cpu2);
Konrad Eisele84017072009-08-31 22:08:13 +0000288 totalram_pages++;
289 num_physpages++;
290 }
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700291 if (!cpu_present(3)) {
Sam Ravnborga2a211c2011-02-25 22:59:20 -0800292 ClearPageReserved(virt_to_page(&trapbase_cpu3));
293 init_page_count(virt_to_page(&trapbase_cpu3));
294 free_page((unsigned long)&trapbase_cpu3);
Konrad Eisele84017072009-08-31 22:08:13 +0000295 totalram_pages++;
296 num_physpages++;
297 }
298 /* Ok, they are spinning and ready to go. */
299 smp_processors_ready = 1;
300
301}
302
303void leon_irq_rotate(int cpu)
304{
305}
306
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +0000307struct leon_ipi_work {
308 int single;
309 int msk;
310 int resched;
311};
312
313static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
314
315/* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
316 * is used for all three types of IPIs.
317 */
318static void __init leon_ipi_init(void)
319{
320 int cpu, len;
321 struct leon_ipi_work *work;
322 struct property *pp;
323 struct device_node *rootnp;
324 struct tt_entry *trap_table;
325 unsigned long flags;
326
327 /* Find IPI IRQ or stick with default value */
328 rootnp = of_find_node_by_path("/ambapp0");
329 if (rootnp) {
330 pp = of_find_property(rootnp, "ipi_num", &len);
331 if (pp && (*(int *)pp->value))
332 leon_ipi_irq = *(int *)pp->value;
333 }
334 printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
335
336 /* Adjust so that we jump directly to smpleon_ipi */
337 local_irq_save(flags);
338 trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
339 trap_table->inst_three += smpleon_ipi - real_irq_entry;
David S. Miller5d83d662012-05-13 20:49:31 -0700340 local_ops->cache_all();
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +0000341 local_irq_restore(flags);
342
343 for_each_possible_cpu(cpu) {
344 work = &per_cpu(leon_ipi_work, cpu);
345 work->single = work->msk = work->resched = 0;
346 }
347}
348
349static void leon_ipi_single(int cpu)
350{
351 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
352
353 /* Mark work */
354 work->single = 1;
355
356 /* Generate IRQ on the CPU */
357 set_cpu_int(cpu, leon_ipi_irq);
358}
359
360static void leon_ipi_mask_one(int cpu)
361{
362 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
363
364 /* Mark work */
365 work->msk = 1;
366
367 /* Generate IRQ on the CPU */
368 set_cpu_int(cpu, leon_ipi_irq);
369}
370
371static void leon_ipi_resched(int cpu)
372{
373 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
374
375 /* Mark work */
376 work->resched = 1;
377
378 /* Generate IRQ on the CPU (any IRQ will cause resched) */
379 set_cpu_int(cpu, leon_ipi_irq);
380}
381
382void leonsmp_ipi_interrupt(void)
383{
384 struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
385
386 if (work->single) {
387 work->single = 0;
388 smp_call_function_single_interrupt();
389 }
390 if (work->msk) {
391 work->msk = 0;
392 smp_call_function_interrupt();
393 }
394 if (work->resched) {
395 work->resched = 0;
396 smp_resched_interrupt();
397 }
398}
399
Konrad Eisele84017072009-08-31 22:08:13 +0000400static struct smp_funcall {
401 smpfunc_t func;
402 unsigned long arg1;
403 unsigned long arg2;
404 unsigned long arg3;
405 unsigned long arg4;
406 unsigned long arg5;
407 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
408 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
409} ccall_info;
410
411static DEFINE_SPINLOCK(cross_call_lock);
412
413/* Cross calls must be serialized, at least currently. */
414static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
415 unsigned long arg2, unsigned long arg3,
416 unsigned long arg4)
417{
418 if (smp_processors_ready) {
419 register int high = NR_CPUS - 1;
420 unsigned long flags;
421
422 spin_lock_irqsave(&cross_call_lock, flags);
423
424 {
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;
432
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),
437 "r"(a4), "r"(a5),
438 "r"(&ccall_info.func));
439 }
440
441 /* Init receive/complete mapping, plus fire the IPI's off. */
442 {
443 register int i;
444
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700445 cpumask_clear_cpu(smp_processor_id(), &mask);
446 cpumask_and(&mask, cpu_online_mask, &mask);
Konrad Eisele84017072009-08-31 22:08:13 +0000447 for (i = 0; i <= high; i++) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700448 if (cpumask_test_cpu(i, &mask)) {
Konrad Eisele84017072009-08-31 22:08:13 +0000449 ccall_info.processors_in[i] = 0;
450 ccall_info.processors_out[i] = 0;
451 set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
452
453 }
454 }
455 }
456
457 {
458 register int i;
459
460 i = 0;
461 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700462 if (!cpumask_test_cpu(i, &mask))
Konrad Eisele84017072009-08-31 22:08:13 +0000463 continue;
464
465 while (!ccall_info.processors_in[i])
466 barrier();
467 } while (++i <= high);
468
469 i = 0;
470 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700471 if (!cpumask_test_cpu(i, &mask))
Konrad Eisele84017072009-08-31 22:08:13 +0000472 continue;
473
474 while (!ccall_info.processors_out[i])
475 barrier();
476 } while (++i <= high);
477 }
478
479 spin_unlock_irqrestore(&cross_call_lock, flags);
480 }
481}
482
483/* Running cross calls. */
484void leon_cross_call_irq(void)
485{
486 int i = smp_processor_id();
487
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;
492}
493
Konrad Eisele84017072009-08-31 22:08:13 +0000494void __init leon_blackbox_id(unsigned *addr)
495{
496 int rd = *addr & 0x3e000000;
497 int rs1 = rd >> 11;
498
499 /* patch places where ___b_hard_smp_processor_id appears */
500 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
501 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
502 addr[2] = 0x01000000; /* nop */
503}
504
505void __init leon_blackbox_current(unsigned *addr)
506{
507 int rd = *addr & 0x3e000000;
508 int rs1 = rd >> 11;
509
510 /* patch LOAD_CURRENT macro where ___b_load_current appears */
511 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
512 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
513 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
514
515}
516
Konrad Eisele84017072009-08-31 22:08:13 +0000517void __init leon_init_smp(void)
518{
519 /* Patch ipi15 trap table */
520 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
521
522 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
523 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
524 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
525 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
526 BTFIXUPCALL_NORM);
Daniel Hellstrom1ca0c802011-05-02 00:08:52 +0000527 BTFIXUPSET_CALL(smp_ipi_resched, leon_ipi_resched, BTFIXUPCALL_NORM);
528 BTFIXUPSET_CALL(smp_ipi_single, leon_ipi_single, BTFIXUPCALL_NORM);
529 BTFIXUPSET_CALL(smp_ipi_mask_one, leon_ipi_mask_one, BTFIXUPCALL_NORM);
Konrad Eisele84017072009-08-31 22:08:13 +0000530}
531
532#endif /* CONFIG_SPARC_LEON */