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>
27 #include <asm/cacheflush.h>
28 #include <asm/tlbflush.h>
30 #include <asm/ptrace.h>
31 #include <asm/atomic.h>
32 #include <asm/irq_regs.h>
33 #include <asm/traps.h>
35 #include <asm/delay.h>
38 #include <asm/pgalloc.h>
39 #include <asm/pgtable.h>
40 #include <asm/oplib.h>
41 #include <asm/cpudata.h>
44 #include <asm/leon_amba.h>
48 #ifdef CONFIG_SPARC_LEON
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 __init
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 static void smp_setup_percpu_timer(void);
73 void __cpuinit
leon_callin(void)
75 int cpuid
= hard_smpleon_processor_id();
77 local_flush_cache_all();
78 local_flush_tlb_all();
79 leon_configure_cache_smp();
81 /* Get our local ticker going. */
82 smp_setup_percpu_timer();
85 smp_store_cpu_info(cpuid
);
87 local_flush_cache_all();
88 local_flush_tlb_all();
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.
97 do_swap(&cpu_callin_map
[cpuid
], 1);
99 local_flush_cache_all();
100 local_flush_tlb_all();
102 /* Fix idle thread fields. */
103 __asm__
__volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set
[cpuid
])
104 : "memory" /* paranoid */);
106 /* Attach to the address space of init_task. */
107 atomic_inc(&init_mm
.mm_count
);
108 current
->active_mm
= &init_mm
;
110 while (!cpumask_test_cpu(cpuid
, &smp_commenced_mask
))
114 set_cpu_online(cpuid
, true);
118 * Cycle through the processors asking the PROM to start each one.
121 extern struct linux_prom_registers smp_penguin_ctable
;
123 void __init
leon_configure_cache_smp(void)
125 unsigned long cfg
= sparc_leon3_get_dcachecfg();
126 int me
= smp_processor_id();
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();
134 if (cfg
& ASI_LEON3_SYSCTRL_CFG_SNOOPING
) {
135 sparc_leon3_enable_snooping();
137 printk(KERN_INFO
"Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
139 sparc_leon3_disable_cache();
143 local_flush_cache_all();
144 local_flush_tlb_all();
147 void leon_smp_setbroadcast(unsigned int mask
)
150 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs
->mpstatus
)) >>
151 LEON3_IRQMPSTATUS_BROADCAST
) & 1);
153 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
155 if (leon_smp_nrcpus() > 1) {
158 prom_printf("continue anyway\n");
162 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs
->mpbroadcast
), mask
);
165 unsigned int leon_smp_getbroadcast(void)
168 mask
= LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs
->mpbroadcast
));
172 int leon_smp_nrcpus(void)
175 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs
->mpstatus
)) >>
176 LEON3_IRQMPSTATUS_CPUNR
) & 0xf) + 1;
180 void __init
leon_boot_cpus(void)
182 int nrcpu
= leon_smp_nrcpus();
183 int me
= smp_processor_id();
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
));
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
);
196 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER
);
198 leon_configure_cache_smp();
199 smp_setup_percpu_timer();
200 local_flush_cache_all();
204 int __cpuinit
leon_boot_one_cpu(int i
)
207 struct task_struct
*p
;
210 /* Cook up an idler for this guy. */
213 current_set
[i
] = task_thread_info(p
);
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
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;
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();
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);
233 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs
->mpstatus
), 1 << i
);
235 /* wheee... it's going... */
236 for (timeout
= 0; timeout
< 10000; timeout
++) {
237 if (cpu_callin_map
[i
])
241 printk(KERN_INFO
"Started CPU %d\n", (unsigned int)i
);
243 if (!(cpu_callin_map
[i
])) {
244 printk(KERN_ERR
"Processor %d is stuck.\n", i
);
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
);
252 local_flush_cache_all();
256 void __init
leon_smp_done(void)
262 /* setup cpu list for irq rotation */
265 for (i
= 0; i
< NR_CPUS
; i
++) {
268 prev
= &cpu_data(i
).next
;
272 local_flush_cache_all();
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
);
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
);
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
);
296 /* Ok, they are spinning and ready to go. */
297 smp_processors_ready
= 1;
301 void leon_irq_rotate(int cpu
)
305 struct leon_ipi_work
{
311 static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work
, leon_ipi_work
);
313 /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
314 * is used for all three types of IPIs.
316 static void __init
leon_ipi_init(void)
319 struct leon_ipi_work
*work
;
321 struct device_node
*rootnp
;
322 struct tt_entry
*trap_table
;
325 /* Find IPI IRQ or stick with default value */
326 rootnp
= of_find_node_by_path("/ambapp0");
328 pp
= of_find_property(rootnp
, "ipi_num", &len
);
329 if (pp
&& (*(int *)pp
->value
))
330 leon_ipi_irq
= *(int *)pp
->value
;
332 printk(KERN_INFO
"leon: SMP IPIs at IRQ %d\n", leon_ipi_irq
);
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
);
341 for_each_possible_cpu(cpu
) {
342 work
= &per_cpu(leon_ipi_work
, cpu
);
343 work
->single
= work
->msk
= work
->resched
= 0;
347 static void leon_ipi_single(int cpu
)
349 struct leon_ipi_work
*work
= &per_cpu(leon_ipi_work
, cpu
);
354 /* Generate IRQ on the CPU */
355 set_cpu_int(cpu
, leon_ipi_irq
);
358 static void leon_ipi_mask_one(int cpu
)
360 struct leon_ipi_work
*work
= &per_cpu(leon_ipi_work
, cpu
);
365 /* Generate IRQ on the CPU */
366 set_cpu_int(cpu
, leon_ipi_irq
);
369 static void leon_ipi_resched(int cpu
)
371 struct leon_ipi_work
*work
= &per_cpu(leon_ipi_work
, cpu
);
376 /* Generate IRQ on the CPU (any IRQ will cause resched) */
377 set_cpu_int(cpu
, leon_ipi_irq
);
380 void leonsmp_ipi_interrupt(void)
382 struct leon_ipi_work
*work
= &__get_cpu_var(leon_ipi_work
);
386 smp_call_function_single_interrupt();
390 smp_call_function_interrupt();
394 smp_resched_interrupt();
398 static struct smp_funcall
{
405 unsigned long processors_in
[NR_CPUS
]; /* Set when ipi entered. */
406 unsigned long processors_out
[NR_CPUS
]; /* Set when ipi exited. */
409 static DEFINE_SPINLOCK(cross_call_lock
);
411 /* Cross calls must be serialized, at least currently. */
412 static void leon_cross_call(smpfunc_t func
, cpumask_t mask
, unsigned long arg1
,
413 unsigned long arg2
, unsigned long arg3
,
416 if (smp_processors_ready
) {
417 register int high
= NR_CPUS
- 1;
420 spin_lock_irqsave(&cross_call_lock
, flags
);
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;
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
),
436 "r"(&ccall_info
.func
));
439 /* Init receive/complete mapping, plus fire the IPI's off. */
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
);
460 if (!cpumask_test_cpu(i
, &mask
))
463 while (!ccall_info
.processors_in
[i
])
465 } while (++i
<= high
);
469 if (!cpumask_test_cpu(i
, &mask
))
472 while (!ccall_info
.processors_out
[i
])
474 } while (++i
<= high
);
477 spin_unlock_irqrestore(&cross_call_lock
, flags
);
481 /* Running cross calls. */
482 void leon_cross_call_irq(void)
484 int i
= smp_processor_id();
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;
492 irqreturn_t
leon_percpu_timer_interrupt(int irq
, void *unused
)
494 int cpu
= smp_processor_id();
496 leon_clear_profile_irq(cpu
);
498 profile_tick(CPU_PROFILING
);
500 if (!--prof_counter(cpu
)) {
501 int user
= user_mode(get_irq_regs());
503 update_process_times(user
);
505 prof_counter(cpu
) = prof_multiplier(cpu
);
511 static void __init
smp_setup_percpu_timer(void)
513 int cpu
= smp_processor_id();
515 prof_counter(cpu
) = prof_multiplier(cpu
) = 1;
518 void __init
leon_blackbox_id(unsigned *addr
)
520 int rd
= *addr
& 0x3e000000;
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 */
529 void __init
leon_blackbox_current(unsigned *addr
)
531 int rd
= *addr
& 0x3e000000;
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 */
541 void __init
leon_init_smp(void)
543 /* Patch ipi15 trap table */
544 t_nmi
[1] = t_nmi
[1] + (linux_trap_ipi15_leon
- linux_trap_ipi15_sun4m
);
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
,
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
);
556 #endif /* CONFIG_SPARC_LEON */