drm/i915|intel-gtt: consolidate intel-gtt.h headers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sparc / kernel / leon_smp.c
blob16582d85368af84b891fded4406d7f0088bf3056
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/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>
24 #include <linux/gfp.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
29 #include <asm/ptrace.h>
30 #include <asm/atomic.h>
31 #include <asm/irq_regs.h>
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>
44 #ifdef CONFIG_SPARC_LEON
46 #include "irq.h"
48 extern ctxd_t *srmmu_ctx_table_phys;
49 static int smp_processors_ready;
50 extern volatile unsigned long cpu_callin_map[NR_CPUS];
51 extern unsigned char boot_cpu_id;
52 extern cpumask_t smp_commenced_mask;
53 void __init leon_configure_cache_smp(void);
55 static inline unsigned long do_swap(volatile unsigned long *ptr,
56 unsigned long val)
58 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
59 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
60 : "memory");
61 return val;
64 static void smp_setup_percpu_timer(void);
66 void __cpuinit leon_callin(void)
68 int cpuid = hard_smpleon_processor_id();
70 local_flush_cache_all();
71 local_flush_tlb_all();
72 leon_configure_cache_smp();
74 /* Get our local ticker going. */
75 smp_setup_percpu_timer();
77 calibrate_delay();
78 smp_store_cpu_info(cpuid);
80 local_flush_cache_all();
81 local_flush_tlb_all();
84 * Unblock the master CPU _only_ when the scheduler state
85 * of all secondary CPUs will be up-to-date, so after
86 * the SMP initialization the master will be just allowed
87 * to call the scheduler code.
88 * Allow master to continue.
90 do_swap(&cpu_callin_map[cpuid], 1);
92 local_flush_cache_all();
93 local_flush_tlb_all();
95 cpu_probe();
97 /* Fix idle thread fields. */
98 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
99 : "memory" /* paranoid */);
101 /* Attach to the address space of init_task. */
102 atomic_inc(&init_mm.mm_count);
103 current->active_mm = &init_mm;
105 while (!cpu_isset(cpuid, smp_commenced_mask))
106 mb();
108 local_irq_enable();
109 cpu_set(cpuid, cpu_online_map);
113 * Cycle through the processors asking the PROM to start each one.
116 extern struct linux_prom_registers smp_penguin_ctable;
118 void __init leon_configure_cache_smp(void)
120 unsigned long cfg = sparc_leon3_get_dcachecfg();
121 int me = smp_processor_id();
123 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
124 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
125 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
126 (unsigned int)cfg, (unsigned int)me);
127 sparc_leon3_disable_cache();
128 } else {
129 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
130 sparc_leon3_enable_snooping();
131 } else {
132 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
133 me);
134 sparc_leon3_disable_cache();
138 local_flush_cache_all();
139 local_flush_tlb_all();
142 void leon_smp_setbroadcast(unsigned int mask)
144 int broadcast =
145 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
146 LEON3_IRQMPSTATUS_BROADCAST) & 1);
147 if (!broadcast) {
148 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
149 leon_smp_nrcpus());
150 if (leon_smp_nrcpus() > 1) {
151 BUG();
152 } else {
153 prom_printf("continue anyway\n");
154 return;
157 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
160 unsigned int leon_smp_getbroadcast(void)
162 unsigned int mask;
163 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
164 return mask;
167 int leon_smp_nrcpus(void)
169 int nrcpu =
170 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
171 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
172 return nrcpu;
175 void __init leon_boot_cpus(void)
177 int nrcpu = leon_smp_nrcpus();
178 int me = smp_processor_id();
180 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
181 (unsigned int)nrcpu, (unsigned int)NR_CPUS,
182 (unsigned int)&(leon3_irqctrl_regs->mpstatus));
184 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
185 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
186 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, me);
188 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
190 leon_configure_cache_smp();
191 smp_setup_percpu_timer();
192 local_flush_cache_all();
196 int __cpuinit leon_boot_one_cpu(int i)
199 struct task_struct *p;
200 int timeout;
202 /* Cook up an idler for this guy. */
203 p = fork_idle(i);
205 current_set[i] = task_thread_info(p);
207 /* See trampoline.S:leon_smp_cpu_startup for details...
208 * Initialize the contexts table
209 * Since the call to prom_startcpu() trashes the structure,
210 * we need to re-initialize it for each cpu
212 smp_penguin_ctable.which_io = 0;
213 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
214 smp_penguin_ctable.reg_size = 0;
216 /* whirrr, whirrr, whirrrrrrrrr... */
217 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
218 (unsigned int)&leon3_irqctrl_regs->mpstatus);
219 local_flush_cache_all();
221 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
223 /* wheee... it's going... */
224 for (timeout = 0; timeout < 10000; timeout++) {
225 if (cpu_callin_map[i])
226 break;
227 udelay(200);
229 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
231 if (!(cpu_callin_map[i])) {
232 printk(KERN_ERR "Processor %d is stuck.\n", i);
233 return -ENODEV;
234 } else {
235 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
236 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
237 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, i);
240 local_flush_cache_all();
241 return 0;
244 void __init leon_smp_done(void)
247 int i, first;
248 int *prev;
250 /* setup cpu list for irq rotation */
251 first = 0;
252 prev = &first;
253 for (i = 0; i < NR_CPUS; i++) {
254 if (cpu_online(i)) {
255 *prev = i;
256 prev = &cpu_data(i).next;
259 *prev = first;
260 local_flush_cache_all();
262 /* Free unneeded trap tables */
263 if (!cpu_isset(1, cpu_present_map)) {
264 ClearPageReserved(virt_to_page(trapbase_cpu1));
265 init_page_count(virt_to_page(trapbase_cpu1));
266 free_page((unsigned long)trapbase_cpu1);
267 totalram_pages++;
268 num_physpages++;
270 if (!cpu_isset(2, cpu_present_map)) {
271 ClearPageReserved(virt_to_page(trapbase_cpu2));
272 init_page_count(virt_to_page(trapbase_cpu2));
273 free_page((unsigned long)trapbase_cpu2);
274 totalram_pages++;
275 num_physpages++;
277 if (!cpu_isset(3, cpu_present_map)) {
278 ClearPageReserved(virt_to_page(trapbase_cpu3));
279 init_page_count(virt_to_page(trapbase_cpu3));
280 free_page((unsigned long)trapbase_cpu3);
281 totalram_pages++;
282 num_physpages++;
284 /* Ok, they are spinning and ready to go. */
285 smp_processors_ready = 1;
289 void leon_irq_rotate(int cpu)
293 static struct smp_funcall {
294 smpfunc_t func;
295 unsigned long arg1;
296 unsigned long arg2;
297 unsigned long arg3;
298 unsigned long arg4;
299 unsigned long arg5;
300 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
301 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
302 } ccall_info;
304 static DEFINE_SPINLOCK(cross_call_lock);
306 /* Cross calls must be serialized, at least currently. */
307 static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
308 unsigned long arg2, unsigned long arg3,
309 unsigned long arg4)
311 if (smp_processors_ready) {
312 register int high = NR_CPUS - 1;
313 unsigned long flags;
315 spin_lock_irqsave(&cross_call_lock, flags);
318 /* If you make changes here, make sure gcc generates proper code... */
319 register smpfunc_t f asm("i0") = func;
320 register unsigned long a1 asm("i1") = arg1;
321 register unsigned long a2 asm("i2") = arg2;
322 register unsigned long a3 asm("i3") = arg3;
323 register unsigned long a4 asm("i4") = arg4;
324 register unsigned long a5 asm("i5") = 0;
326 __asm__ __volatile__("std %0, [%6]\n\t"
327 "std %2, [%6 + 8]\n\t"
328 "std %4, [%6 + 16]\n\t" : :
329 "r"(f), "r"(a1), "r"(a2), "r"(a3),
330 "r"(a4), "r"(a5),
331 "r"(&ccall_info.func));
334 /* Init receive/complete mapping, plus fire the IPI's off. */
336 register int i;
338 cpu_clear(smp_processor_id(), mask);
339 cpus_and(mask, cpu_online_map, mask);
340 for (i = 0; i <= high; i++) {
341 if (cpu_isset(i, mask)) {
342 ccall_info.processors_in[i] = 0;
343 ccall_info.processors_out[i] = 0;
344 set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
351 register int i;
353 i = 0;
354 do {
355 if (!cpu_isset(i, mask))
356 continue;
358 while (!ccall_info.processors_in[i])
359 barrier();
360 } while (++i <= high);
362 i = 0;
363 do {
364 if (!cpu_isset(i, mask))
365 continue;
367 while (!ccall_info.processors_out[i])
368 barrier();
369 } while (++i <= high);
372 spin_unlock_irqrestore(&cross_call_lock, flags);
376 /* Running cross calls. */
377 void leon_cross_call_irq(void)
379 int i = smp_processor_id();
381 ccall_info.processors_in[i] = 1;
382 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
383 ccall_info.arg4, ccall_info.arg5);
384 ccall_info.processors_out[i] = 1;
387 void leon_percpu_timer_interrupt(struct pt_regs *regs)
389 struct pt_regs *old_regs;
390 int cpu = smp_processor_id();
392 old_regs = set_irq_regs(regs);
394 leon_clear_profile_irq(cpu);
396 profile_tick(CPU_PROFILING);
398 if (!--prof_counter(cpu)) {
399 int user = user_mode(regs);
401 irq_enter();
402 update_process_times(user);
403 irq_exit();
405 prof_counter(cpu) = prof_multiplier(cpu);
407 set_irq_regs(old_regs);
410 static void __init smp_setup_percpu_timer(void)
412 int cpu = smp_processor_id();
414 prof_counter(cpu) = prof_multiplier(cpu) = 1;
417 void __init leon_blackbox_id(unsigned *addr)
419 int rd = *addr & 0x3e000000;
420 int rs1 = rd >> 11;
422 /* patch places where ___b_hard_smp_processor_id appears */
423 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
424 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
425 addr[2] = 0x01000000; /* nop */
428 void __init leon_blackbox_current(unsigned *addr)
430 int rd = *addr & 0x3e000000;
431 int rs1 = rd >> 11;
433 /* patch LOAD_CURRENT macro where ___b_load_current appears */
434 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
435 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
436 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
441 * CPU idle callback function
442 * See .../arch/sparc/kernel/process.c
444 void pmc_leon_idle(void)
446 __asm__ volatile ("mov %g0, %asr19");
449 void __init leon_init_smp(void)
451 /* Patch ipi15 trap table */
452 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
454 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
455 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
456 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
457 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
458 BTFIXUPCALL_NORM);
460 #ifndef PMC_NO_IDLE
461 /* Assign power management IDLE handler */
462 pm_idle = pmc_leon_idle;
463 printk(KERN_INFO "leon: power management initialized\n");
464 #endif
468 #endif /* CONFIG_SPARC_LEON */