RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / sparc / kernel / leon_smp.c
blobe1656fc41ccbc275dece71ed001a2bead7005ac8
1 /* leon_smp.c: Sparc-Leon SMP support.
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 */
9 #include <asm/head.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/threads.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.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>
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>
34 #include <asm/delay.h>
35 #include <asm/irq.h>
36 #include <asm/page.h>
37 #include <asm/pgalloc.h>
38 #include <asm/pgtable.h>
39 #include <asm/oplib.h>
40 #include <asm/cpudata.h>
41 #include <asm/asi.h>
42 #include <asm/leon.h>
43 #include <asm/leon_amba.h>
45 #ifdef CONFIG_SPARC_LEON
47 #include "irq.h"
49 extern ctxd_t *srmmu_ctx_table_phys;
50 static int smp_processors_ready;
51 extern volatile unsigned long cpu_callin_map[NR_CPUS];
52 extern unsigned char boot_cpu_id;
53 extern cpumask_t smp_commenced_mask;
54 void __init leon_configure_cache_smp(void);
56 static inline unsigned long do_swap(volatile unsigned long *ptr,
57 unsigned long val)
59 __asm__ __volatile__("swapa [%1] %2, %0\n\t" : "=&r"(val)
60 : "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
61 : "memory");
62 return val;
65 static void smp_setup_percpu_timer(void);
67 void __cpuinit leon_callin(void)
69 int cpuid = hard_smpleon_processor_id();
71 local_flush_cache_all();
72 local_flush_tlb_all();
73 leon_configure_cache_smp();
75 /* Get our local ticker going. */
76 smp_setup_percpu_timer();
78 calibrate_delay();
79 smp_store_cpu_info(cpuid);
81 local_flush_cache_all();
82 local_flush_tlb_all();
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.
91 do_swap(&cpu_callin_map[cpuid], 1);
93 local_flush_cache_all();
94 local_flush_tlb_all();
96 cpu_probe();
98 /* Fix idle thread fields. */
99 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
100 : "memory" /* paranoid */);
102 /* Attach to the address space of init_task. */
103 atomic_inc(&init_mm.mm_count);
104 current->active_mm = &init_mm;
106 while (!cpu_isset(cpuid, smp_commenced_mask))
107 mb();
109 local_irq_enable();
110 cpu_set(cpuid, cpu_online_map);
114 * Cycle through the processors asking the PROM to start each one.
117 extern struct linux_prom_registers smp_penguin_ctable;
119 void __init leon_configure_cache_smp(void)
121 unsigned long cfg = sparc_leon3_get_dcachecfg();
122 int me = smp_processor_id();
124 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
125 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
126 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
127 (unsigned int)cfg, (unsigned int)me);
128 sparc_leon3_disable_cache();
129 } else {
130 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
131 sparc_leon3_enable_snooping();
132 } else {
133 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
134 me);
135 sparc_leon3_disable_cache();
139 local_flush_cache_all();
140 local_flush_tlb_all();
143 void leon_smp_setbroadcast(unsigned int mask)
145 int broadcast =
146 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
147 LEON3_IRQMPSTATUS_BROADCAST) & 1);
148 if (!broadcast) {
149 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
150 leon_smp_nrcpus());
151 if (leon_smp_nrcpus() > 1) {
152 BUG();
153 } else {
154 prom_printf("continue anyway\n");
155 return;
158 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
161 unsigned int leon_smp_getbroadcast(void)
163 unsigned int mask;
164 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
165 return mask;
168 int leon_smp_nrcpus(void)
170 int nrcpu =
171 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
172 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
173 return nrcpu;
176 void __init leon_boot_cpus(void)
178 int nrcpu = leon_smp_nrcpus();
179 int me = smp_processor_id();
181 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
182 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
183 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
185 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
186 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
187 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, me);
189 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
191 leon_configure_cache_smp();
192 smp_setup_percpu_timer();
193 local_flush_cache_all();
197 int __cpuinit leon_boot_one_cpu(int i)
200 struct task_struct *p;
201 int timeout;
203 /* Cook up an idler for this guy. */
204 p = fork_idle(i);
206 current_set[i] = task_thread_info(p);
208 /* See trampoline.S:leon_smp_cpu_startup for details...
209 * Initialize the contexts table
210 * Since the call to prom_startcpu() trashes the structure,
211 * we need to re-initialize it for each cpu
213 smp_penguin_ctable.which_io = 0;
214 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
215 smp_penguin_ctable.reg_size = 0;
217 /* whirrr, whirrr, whirrrrrrrrr... */
218 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
219 (unsigned int)&leon3_irqctrl_regs->mpstatus);
220 local_flush_cache_all();
222 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
224 /* wheee... it's going... */
225 for (timeout = 0; timeout < 10000; timeout++) {
226 if (cpu_callin_map[i])
227 break;
228 udelay(200);
230 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
232 if (!(cpu_callin_map[i])) {
233 printk(KERN_ERR "Processor %d is stuck.\n", i);
234 return -ENODEV;
235 } else {
236 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
237 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
238 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, i);
241 local_flush_cache_all();
242 return 0;
245 void __init leon_smp_done(void)
248 int i, first;
249 int *prev;
251 /* setup cpu list for irq rotation */
252 first = 0;
253 prev = &first;
254 for (i = 0; i < NR_CPUS; i++) {
255 if (cpu_online(i)) {
256 *prev = i;
257 prev = &cpu_data(i).next;
260 *prev = first;
261 local_flush_cache_all();
263 /* Free unneeded trap tables */
264 if (!cpu_isset(1, cpu_present_map)) {
265 ClearPageReserved(virt_to_page(trapbase_cpu1));
266 init_page_count(virt_to_page(trapbase_cpu1));
267 free_page((unsigned long)trapbase_cpu1);
268 totalram_pages++;
269 num_physpages++;
271 if (!cpu_isset(2, cpu_present_map)) {
272 ClearPageReserved(virt_to_page(trapbase_cpu2));
273 init_page_count(virt_to_page(trapbase_cpu2));
274 free_page((unsigned long)trapbase_cpu2);
275 totalram_pages++;
276 num_physpages++;
278 if (!cpu_isset(3, cpu_present_map)) {
279 ClearPageReserved(virt_to_page(trapbase_cpu3));
280 init_page_count(virt_to_page(trapbase_cpu3));
281 free_page((unsigned long)trapbase_cpu3);
282 totalram_pages++;
283 num_physpages++;
285 /* Ok, they are spinning and ready to go. */
286 smp_processors_ready = 1;
290 void leon_irq_rotate(int cpu)
294 static struct smp_funcall {
295 smpfunc_t func;
296 unsigned long arg1;
297 unsigned long arg2;
298 unsigned long arg3;
299 unsigned long arg4;
300 unsigned long arg5;
301 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
302 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
303 } ccall_info;
305 static DEFINE_SPINLOCK(cross_call_lock);
307 /* Cross calls must be serialized, at least currently. */
308 static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
309 unsigned long arg2, unsigned long arg3,
310 unsigned long arg4)
312 if (smp_processors_ready) {
313 register int high = NR_CPUS - 1;
314 unsigned long flags;
316 spin_lock_irqsave(&cross_call_lock, flags);
319 /* If you make changes here, make sure gcc generates proper code... */
320 register smpfunc_t f asm("i0") = func;
321 register unsigned long a1 asm("i1") = arg1;
322 register unsigned long a2 asm("i2") = arg2;
323 register unsigned long a3 asm("i3") = arg3;
324 register unsigned long a4 asm("i4") = arg4;
325 register unsigned long a5 asm("i5") = 0;
327 __asm__ __volatile__("std %0, [%6]\n\t"
328 "std %2, [%6 + 8]\n\t"
329 "std %4, [%6 + 16]\n\t" : :
330 "r"(f), "r"(a1), "r"(a2), "r"(a3),
331 "r"(a4), "r"(a5),
332 "r"(&ccall_info.func));
335 /* Init receive/complete mapping, plus fire the IPI's off. */
337 register int i;
339 cpu_clear(smp_processor_id(), mask);
340 cpus_and(mask, cpu_online_map, mask);
341 for (i = 0; i <= high; i++) {
342 if (cpu_isset(i, mask)) {
343 ccall_info.processors_in[i] = 0;
344 ccall_info.processors_out[i] = 0;
345 set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
352 register int i;
354 i = 0;
355 do {
356 if (!cpu_isset(i, mask))
357 continue;
359 while (!ccall_info.processors_in[i])
360 barrier();
361 } while (++i <= high);
363 i = 0;
364 do {
365 if (!cpu_isset(i, mask))
366 continue;
368 while (!ccall_info.processors_out[i])
369 barrier();
370 } while (++i <= high);
373 spin_unlock_irqrestore(&cross_call_lock, flags);
377 /* Running cross calls. */
378 void leon_cross_call_irq(void)
380 int i = smp_processor_id();
382 ccall_info.processors_in[i] = 1;
383 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
384 ccall_info.arg4, ccall_info.arg5);
385 ccall_info.processors_out[i] = 1;
388 void leon_percpu_timer_interrupt(struct pt_regs *regs)
390 struct pt_regs *old_regs;
391 int cpu = smp_processor_id();
393 old_regs = set_irq_regs(regs);
395 leon_clear_profile_irq(cpu);
397 profile_tick(CPU_PROFILING);
399 if (!--prof_counter(cpu)) {
400 int user = user_mode(regs);
402 irq_enter();
403 update_process_times(user);
404 irq_exit();
406 prof_counter(cpu) = prof_multiplier(cpu);
408 set_irq_regs(old_regs);
411 static void __init smp_setup_percpu_timer(void)
413 int cpu = smp_processor_id();
415 prof_counter(cpu) = prof_multiplier(cpu) = 1;
418 void __init leon_blackbox_id(unsigned *addr)
420 int rd = *addr & 0x3e000000;
421 int rs1 = rd >> 11;
423 /* patch places where ___b_hard_smp_processor_id appears */
424 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
425 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
426 addr[2] = 0x01000000; /* nop */
429 void __init leon_blackbox_current(unsigned *addr)
431 int rd = *addr & 0x3e000000;
432 int rs1 = rd >> 11;
434 /* patch LOAD_CURRENT macro where ___b_load_current appears */
435 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
436 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
437 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
442 * CPU idle callback function
443 * See .../arch/sparc/kernel/process.c
445 void pmc_leon_idle(void)
447 __asm__ volatile ("mov %g0, %asr19");
450 void __init leon_init_smp(void)
452 /* Patch ipi15 trap table */
453 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
455 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
456 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
457 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
458 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
459 BTFIXUPCALL_NORM);
461 #ifndef PMC_NO_IDLE
462 /* Assign power management IDLE handler */
463 pm_idle = pmc_leon_idle;
464 printk(KERN_INFO "leon: power management initialized\n");
465 #endif
469 #endif /* CONFIG_SPARC_LEON */