sparc32: implement SMP IPIs using the generic functions
[deliverable/linux.git] / arch / sparc / kernel / leon_smp.c
CommitLineData
8401707f
KE
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>
8401707f
KE
15#include <linux/interrupt.h>
16#include <linux/kernel_stat.h>
17#include <linux/init.h>
18#include <linux/spinlock.h>
19#include <linux/mm.h>
20#include <linux/swap.h>
21#include <linux/profile.h>
22#include <linux/pm.h>
23#include <linux/delay.h>
5a0e3ad6 24#include <linux/gfp.h>
8401707f
KE
25
26#include <asm/cacheflush.h>
27#include <asm/tlbflush.h>
28
29#include <asm/ptrace.h>
30#include <asm/atomic.h>
31#include <asm/irq_regs.h>
32
33#include <asm/delay.h>
34#include <asm/irq.h>
35#include <asm/page.h>
36#include <asm/pgalloc.h>
37#include <asm/pgtable.h>
38#include <asm/oplib.h>
39#include <asm/cpudata.h>
40#include <asm/asi.h>
41#include <asm/leon.h>
42#include <asm/leon_amba.h>
43
a2a211cb
SR
44#include "kernel.h"
45
8401707f
KE
46#ifdef CONFIG_SPARC_LEON
47
48#include "irq.h"
49
50extern ctxd_t *srmmu_ctx_table_phys;
51static int smp_processors_ready;
52extern volatile unsigned long cpu_callin_map[NR_CPUS];
8401707f
KE
53extern cpumask_t smp_commenced_mask;
54void __init leon_configure_cache_smp(void);
55
56static inline unsigned long do_swap(volatile unsigned long *ptr,
57 unsigned long val)
58{
502279a7
DH
59 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
60 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
8401707f
KE
61 : "memory");
62 return val;
63}
64
65static void smp_setup_percpu_timer(void);
66
67void __cpuinit leon_callin(void)
68{
69 int cpuid = hard_smpleon_processor_id();
70
71 local_flush_cache_all();
72 local_flush_tlb_all();
73 leon_configure_cache_smp();
74
75 /* Get our local ticker going. */
76 smp_setup_percpu_timer();
77
78 calibrate_delay();
79 smp_store_cpu_info(cpuid);
80
81 local_flush_cache_all();
82 local_flush_tlb_all();
83
84 /*
85 * Unblock the master CPU _only_ when the scheduler state
86 * of all secondary CPUs will be up-to-date, so after
87 * the SMP initialization the master will be just allowed
88 * to call the scheduler code.
89 * Allow master to continue.
90 */
91 do_swap(&cpu_callin_map[cpuid], 1);
92
93 local_flush_cache_all();
94 local_flush_tlb_all();
95
8401707f
KE
96 /* Fix idle thread fields. */
97 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
98 : "memory" /* paranoid */);
99
100 /* Attach to the address space of init_task. */
101 atomic_inc(&init_mm.mm_count);
102 current->active_mm = &init_mm;
103
104 while (!cpu_isset(cpuid, smp_commenced_mask))
105 mb();
106
107 local_irq_enable();
108 cpu_set(cpuid, cpu_online_map);
109}
110
111/*
112 * Cycle through the processors asking the PROM to start each one.
113 */
114
115extern struct linux_prom_registers smp_penguin_ctable;
116
117void __init leon_configure_cache_smp(void)
118{
119 unsigned long cfg = sparc_leon3_get_dcachecfg();
120 int me = smp_processor_id();
121
122 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
123 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
124 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
125 (unsigned int)cfg, (unsigned int)me);
126 sparc_leon3_disable_cache();
127 } else {
128 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
129 sparc_leon3_enable_snooping();
130 } else {
131 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
132 me);
133 sparc_leon3_disable_cache();
134 }
135 }
136
137 local_flush_cache_all();
138 local_flush_tlb_all();
139}
140
141void leon_smp_setbroadcast(unsigned int mask)
142{
143 int broadcast =
144 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
145 LEON3_IRQMPSTATUS_BROADCAST) & 1);
146 if (!broadcast) {
147 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
148 leon_smp_nrcpus());
149 if (leon_smp_nrcpus() > 1) {
150 BUG();
151 } else {
152 prom_printf("continue anyway\n");
153 return;
154 }
155 }
156 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
157}
158
159unsigned int leon_smp_getbroadcast(void)
160{
161 unsigned int mask;
162 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
163 return mask;
164}
165
166int leon_smp_nrcpus(void)
167{
168 int nrcpu =
169 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
170 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
171 return nrcpu;
172}
173
174void __init leon_boot_cpus(void)
175{
176 int nrcpu = leon_smp_nrcpus();
177 int me = smp_processor_id();
178
0da2b300 179 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
8401707f
KE
180 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
181 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
182
183 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
184 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
185 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, me);
186
187 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
188
189 leon_configure_cache_smp();
190 smp_setup_percpu_timer();
191 local_flush_cache_all();
192
193}
194
195int __cpuinit leon_boot_one_cpu(int i)
196{
197
198 struct task_struct *p;
199 int timeout;
200
201 /* Cook up an idler for this guy. */
202 p = fork_idle(i);
203
204 current_set[i] = task_thread_info(p);
205
206 /* See trampoline.S:leon_smp_cpu_startup for details...
207 * Initialize the contexts table
208 * Since the call to prom_startcpu() trashes the structure,
209 * we need to re-initialize it for each cpu
210 */
211 smp_penguin_ctable.which_io = 0;
212 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
213 smp_penguin_ctable.reg_size = 0;
214
215 /* whirrr, whirrr, whirrrrrrrrr... */
216 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
217 (unsigned int)&leon3_irqctrl_regs->mpstatus);
218 local_flush_cache_all();
219
970def65
DH
220 /* Make sure all IRQs are of from the start for this new CPU */
221 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
222
223 /* Wake one CPU */
8401707f
KE
224 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
225
226 /* wheee... it's going... */
227 for (timeout = 0; timeout < 10000; timeout++) {
228 if (cpu_callin_map[i])
229 break;
230 udelay(200);
231 }
0da2b300 232 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
8401707f
KE
233
234 if (!(cpu_callin_map[i])) {
235 printk(KERN_ERR "Processor %d is stuck.\n", i);
236 return -ENODEV;
237 } else {
238 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
239 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
240 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, i);
241 }
242
243 local_flush_cache_all();
244 return 0;
245}
246
247void __init leon_smp_done(void)
248{
249
250 int i, first;
251 int *prev;
252
253 /* setup cpu list for irq rotation */
254 first = 0;
255 prev = &first;
256 for (i = 0; i < NR_CPUS; i++) {
257 if (cpu_online(i)) {
258 *prev = i;
259 prev = &cpu_data(i).next;
260 }
261 }
262 *prev = first;
263 local_flush_cache_all();
264
265 /* Free unneeded trap tables */
266 if (!cpu_isset(1, cpu_present_map)) {
a2a211cb
SR
267 ClearPageReserved(virt_to_page(&trapbase_cpu1));
268 init_page_count(virt_to_page(&trapbase_cpu1));
269 free_page((unsigned long)&trapbase_cpu1);
8401707f
KE
270 totalram_pages++;
271 num_physpages++;
272 }
273 if (!cpu_isset(2, cpu_present_map)) {
a2a211cb
SR
274 ClearPageReserved(virt_to_page(&trapbase_cpu2));
275 init_page_count(virt_to_page(&trapbase_cpu2));
276 free_page((unsigned long)&trapbase_cpu2);
8401707f
KE
277 totalram_pages++;
278 num_physpages++;
279 }
280 if (!cpu_isset(3, cpu_present_map)) {
a2a211cb
SR
281 ClearPageReserved(virt_to_page(&trapbase_cpu3));
282 init_page_count(virt_to_page(&trapbase_cpu3));
283 free_page((unsigned long)&trapbase_cpu3);
8401707f
KE
284 totalram_pages++;
285 num_physpages++;
286 }
287 /* Ok, they are spinning and ready to go. */
288 smp_processors_ready = 1;
289
290}
291
292void leon_irq_rotate(int cpu)
293{
294}
295
296static struct smp_funcall {
297 smpfunc_t func;
298 unsigned long arg1;
299 unsigned long arg2;
300 unsigned long arg3;
301 unsigned long arg4;
302 unsigned long arg5;
303 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
304 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
305} ccall_info;
306
307static DEFINE_SPINLOCK(cross_call_lock);
308
309/* Cross calls must be serialized, at least currently. */
310static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
311 unsigned long arg2, unsigned long arg3,
312 unsigned long arg4)
313{
314 if (smp_processors_ready) {
315 register int high = NR_CPUS - 1;
316 unsigned long flags;
317
318 spin_lock_irqsave(&cross_call_lock, flags);
319
320 {
321 /* If you make changes here, make sure gcc generates proper code... */
322 register smpfunc_t f asm("i0") = func;
323 register unsigned long a1 asm("i1") = arg1;
324 register unsigned long a2 asm("i2") = arg2;
325 register unsigned long a3 asm("i3") = arg3;
326 register unsigned long a4 asm("i4") = arg4;
327 register unsigned long a5 asm("i5") = 0;
328
329 __asm__ __volatile__("std %0, [%6]\n\t"
330 "std %2, [%6 + 8]\n\t"
331 "std %4, [%6 + 16]\n\t" : :
332 "r"(f), "r"(a1), "r"(a2), "r"(a3),
333 "r"(a4), "r"(a5),
334 "r"(&ccall_info.func));
335 }
336
337 /* Init receive/complete mapping, plus fire the IPI's off. */
338 {
339 register int i;
340
341 cpu_clear(smp_processor_id(), mask);
342 cpus_and(mask, cpu_online_map, mask);
343 for (i = 0; i <= high; i++) {
344 if (cpu_isset(i, mask)) {
345 ccall_info.processors_in[i] = 0;
346 ccall_info.processors_out[i] = 0;
347 set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
348
349 }
350 }
351 }
352
353 {
354 register int i;
355
356 i = 0;
357 do {
358 if (!cpu_isset(i, mask))
359 continue;
360
361 while (!ccall_info.processors_in[i])
362 barrier();
363 } while (++i <= high);
364
365 i = 0;
366 do {
367 if (!cpu_isset(i, mask))
368 continue;
369
370 while (!ccall_info.processors_out[i])
371 barrier();
372 } while (++i <= high);
373 }
374
375 spin_unlock_irqrestore(&cross_call_lock, flags);
376 }
377}
378
379/* Running cross calls. */
380void leon_cross_call_irq(void)
381{
382 int i = smp_processor_id();
383
384 ccall_info.processors_in[i] = 1;
385 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
386 ccall_info.arg4, ccall_info.arg5);
387 ccall_info.processors_out[i] = 1;
388}
389
2cf95304 390irqreturn_t leon_percpu_timer_interrupt(int irq, void *unused)
8401707f 391{
8401707f
KE
392 int cpu = smp_processor_id();
393
8401707f
KE
394 leon_clear_profile_irq(cpu);
395
396 profile_tick(CPU_PROFILING);
397
398 if (!--prof_counter(cpu)) {
2cf95304 399 int user = user_mode(get_irq_regs());
8401707f 400
8401707f 401 update_process_times(user);
8401707f
KE
402
403 prof_counter(cpu) = prof_multiplier(cpu);
404 }
2cf95304
DH
405
406 return IRQ_HANDLED;
8401707f
KE
407}
408
409static void __init smp_setup_percpu_timer(void)
410{
411 int cpu = smp_processor_id();
412
413 prof_counter(cpu) = prof_multiplier(cpu) = 1;
414}
415
416void __init leon_blackbox_id(unsigned *addr)
417{
418 int rd = *addr & 0x3e000000;
419 int rs1 = rd >> 11;
420
421 /* patch places where ___b_hard_smp_processor_id appears */
422 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
423 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
424 addr[2] = 0x01000000; /* nop */
425}
426
427void __init leon_blackbox_current(unsigned *addr)
428{
429 int rd = *addr & 0x3e000000;
430 int rs1 = rd >> 11;
431
432 /* patch LOAD_CURRENT macro where ___b_load_current appears */
433 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
434 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
435 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
436
437}
438
8401707f
KE
439void __init leon_init_smp(void)
440{
441 /* Patch ipi15 trap table */
442 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
443
444 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
445 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
446 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
447 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
448 BTFIXUPCALL_NORM);
8401707f
KE
449}
450
451#endif /* CONFIG_SPARC_LEON */
This page took 0.1224 seconds and 5 git commands to generate.