sparc: Use generic idle thread allocation
[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>
1ca0c808 17#include <linux/of.h>
8401707f
KE
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>
5a0e3ad6 25#include <linux/gfp.h>
e9a5ea18 26#include <linux/cpu.h>
8401707f
KE
27
28#include <asm/cacheflush.h>
29#include <asm/tlbflush.h>
30
31#include <asm/ptrace.h>
60063497 32#include <linux/atomic.h>
8401707f 33#include <asm/irq_regs.h>
1ca0c808 34#include <asm/traps.h>
8401707f
KE
35
36#include <asm/delay.h>
37#include <asm/irq.h>
38#include <asm/page.h>
39#include <asm/pgalloc.h>
40#include <asm/pgtable.h>
41#include <asm/oplib.h>
42#include <asm/cpudata.h>
43#include <asm/asi.h>
44#include <asm/leon.h>
45#include <asm/leon_amba.h>
46
a2a211cb
SR
47#include "kernel.h"
48
8401707f
KE
49#ifdef CONFIG_SPARC_LEON
50
51#include "irq.h"
52
53extern ctxd_t *srmmu_ctx_table_phys;
54static int smp_processors_ready;
55extern volatile unsigned long cpu_callin_map[NR_CPUS];
8401707f
KE
56extern cpumask_t smp_commenced_mask;
57void __init leon_configure_cache_smp(void);
1ca0c808
DH
58static void leon_ipi_init(void);
59
60/* IRQ number of LEON IPIs */
61int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
8401707f
KE
62
63static inline unsigned long do_swap(volatile unsigned long *ptr,
64 unsigned long val)
65{
502279a7
DH
66 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
67 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
8401707f
KE
68 : "memory");
69 return val;
70}
71
72static void smp_setup_percpu_timer(void);
73
74void __cpuinit leon_callin(void)
75{
76 int cpuid = hard_smpleon_processor_id();
77
78 local_flush_cache_all();
79 local_flush_tlb_all();
80 leon_configure_cache_smp();
81
e9a5ea18
YZ
82 notify_cpu_starting(cpuid);
83
8401707f
KE
84 /* Get our local ticker going. */
85 smp_setup_percpu_timer();
86
87 calibrate_delay();
88 smp_store_cpu_info(cpuid);
89
90 local_flush_cache_all();
91 local_flush_tlb_all();
92
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
102 local_flush_cache_all();
103 local_flush_tlb_all();
104
8401707f
KE
105 /* 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
fb1fece5 113 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
8401707f
KE
114 mb();
115
116 local_irq_enable();
fb1fece5 117 set_cpu_online(cpuid, true);
8401707f
KE
118}
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
146 local_flush_cache_all();
147 local_flush_tlb_all();
148}
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
1ca0c808
DH
188 /* Setup IPI */
189 leon_ipi_init();
190
0da2b300 191 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
8401707f
KE
192 (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);
1ca0c808 197 leon_enable_irq_cpu(leon_ipi_irq, me);
8401707f
KE
198
199 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
200
201 leon_configure_cache_smp();
202 smp_setup_percpu_timer();
203 local_flush_cache_all();
204
205}
206
f0a2bc7e 207int __cpuinit leon_boot_one_cpu(int i, struct task_struct *idle)
8401707f 208{
8401707f
KE
209 int timeout;
210
f0a2bc7e 211 current_set[i] = task_thread_info(idle);
8401707f
KE
212
213 /* See trampoline.S:leon_smp_cpu_startup for details...
214 * Initialize the contexts table
215 * Since the call to prom_startcpu() trashes the structure,
216 * we need to re-initialize it for each cpu
217 */
218 smp_penguin_ctable.which_io = 0;
219 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
220 smp_penguin_ctable.reg_size = 0;
221
222 /* whirrr, whirrr, whirrrrrrrrr... */
223 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
224 (unsigned int)&leon3_irqctrl_regs->mpstatus);
225 local_flush_cache_all();
226
970def65
DH
227 /* Make sure all IRQs are of from the start for this new CPU */
228 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
229
230 /* Wake one CPU */
8401707f
KE
231 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
232
233 /* wheee... it's going... */
234 for (timeout = 0; timeout < 10000; timeout++) {
235 if (cpu_callin_map[i])
236 break;
237 udelay(200);
238 }
0da2b300 239 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
8401707f
KE
240
241 if (!(cpu_callin_map[i])) {
242 printk(KERN_ERR "Processor %d is stuck.\n", i);
243 return -ENODEV;
244 } else {
245 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
246 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
1ca0c808 247 leon_enable_irq_cpu(leon_ipi_irq, i);
8401707f
KE
248 }
249
250 local_flush_cache_all();
251 return 0;
252}
253
254void __init leon_smp_done(void)
255{
256
257 int i, first;
258 int *prev;
259
260 /* setup cpu list for irq rotation */
261 first = 0;
262 prev = &first;
263 for (i = 0; i < NR_CPUS; i++) {
264 if (cpu_online(i)) {
265 *prev = i;
266 prev = &cpu_data(i).next;
267 }
268 }
269 *prev = first;
270 local_flush_cache_all();
271
272 /* Free unneeded trap tables */
fb1fece5 273 if (!cpu_present(1)) {
a2a211cb
SR
274 ClearPageReserved(virt_to_page(&trapbase_cpu1));
275 init_page_count(virt_to_page(&trapbase_cpu1));
276 free_page((unsigned long)&trapbase_cpu1);
8401707f
KE
277 totalram_pages++;
278 num_physpages++;
279 }
fb1fece5 280 if (!cpu_present(2)) {
a2a211cb
SR
281 ClearPageReserved(virt_to_page(&trapbase_cpu2));
282 init_page_count(virt_to_page(&trapbase_cpu2));
283 free_page((unsigned long)&trapbase_cpu2);
8401707f
KE
284 totalram_pages++;
285 num_physpages++;
286 }
fb1fece5 287 if (!cpu_present(3)) {
a2a211cb
SR
288 ClearPageReserved(virt_to_page(&trapbase_cpu3));
289 init_page_count(virt_to_page(&trapbase_cpu3));
290 free_page((unsigned long)&trapbase_cpu3);
8401707f
KE
291 totalram_pages++;
292 num_physpages++;
293 }
294 /* Ok, they are spinning and ready to go. */
295 smp_processors_ready = 1;
296
297}
298
299void leon_irq_rotate(int cpu)
300{
301}
302
1ca0c808
DH
303struct leon_ipi_work {
304 int single;
305 int msk;
306 int resched;
307};
308
309static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
310
311/* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
312 * is used for all three types of IPIs.
313 */
314static void __init leon_ipi_init(void)
315{
316 int cpu, len;
317 struct leon_ipi_work *work;
318 struct property *pp;
319 struct device_node *rootnp;
320 struct tt_entry *trap_table;
321 unsigned long flags;
322
323 /* Find IPI IRQ or stick with default value */
324 rootnp = of_find_node_by_path("/ambapp0");
325 if (rootnp) {
326 pp = of_find_property(rootnp, "ipi_num", &len);
327 if (pp && (*(int *)pp->value))
328 leon_ipi_irq = *(int *)pp->value;
329 }
330 printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
331
332 /* Adjust so that we jump directly to smpleon_ipi */
333 local_irq_save(flags);
334 trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
335 trap_table->inst_three += smpleon_ipi - real_irq_entry;
336 local_flush_cache_all();
337 local_irq_restore(flags);
338
339 for_each_possible_cpu(cpu) {
340 work = &per_cpu(leon_ipi_work, cpu);
341 work->single = work->msk = work->resched = 0;
342 }
343}
344
345static void leon_ipi_single(int cpu)
346{
347 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
348
349 /* Mark work */
350 work->single = 1;
351
352 /* Generate IRQ on the CPU */
353 set_cpu_int(cpu, leon_ipi_irq);
354}
355
356static void leon_ipi_mask_one(int cpu)
357{
358 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
359
360 /* Mark work */
361 work->msk = 1;
362
363 /* Generate IRQ on the CPU */
364 set_cpu_int(cpu, leon_ipi_irq);
365}
366
367static void leon_ipi_resched(int cpu)
368{
369 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
370
371 /* Mark work */
372 work->resched = 1;
373
374 /* Generate IRQ on the CPU (any IRQ will cause resched) */
375 set_cpu_int(cpu, leon_ipi_irq);
376}
377
378void leonsmp_ipi_interrupt(void)
379{
380 struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
381
382 if (work->single) {
383 work->single = 0;
384 smp_call_function_single_interrupt();
385 }
386 if (work->msk) {
387 work->msk = 0;
388 smp_call_function_interrupt();
389 }
390 if (work->resched) {
391 work->resched = 0;
392 smp_resched_interrupt();
393 }
394}
395
8401707f
KE
396static struct smp_funcall {
397 smpfunc_t func;
398 unsigned long arg1;
399 unsigned long arg2;
400 unsigned long arg3;
401 unsigned long arg4;
402 unsigned long arg5;
403 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */
404 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */
405} ccall_info;
406
407static DEFINE_SPINLOCK(cross_call_lock);
408
409/* Cross calls must be serialized, at least currently. */
410static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
411 unsigned long arg2, unsigned long arg3,
412 unsigned long arg4)
413{
414 if (smp_processors_ready) {
415 register int high = NR_CPUS - 1;
416 unsigned long flags;
417
418 spin_lock_irqsave(&cross_call_lock, flags);
419
420 {
421 /* If you make changes here, make sure gcc generates proper code... */
422 register smpfunc_t f asm("i0") = func;
423 register unsigned long a1 asm("i1") = arg1;
424 register unsigned long a2 asm("i2") = arg2;
425 register unsigned long a3 asm("i3") = arg3;
426 register unsigned long a4 asm("i4") = arg4;
427 register unsigned long a5 asm("i5") = 0;
428
429 __asm__ __volatile__("std %0, [%6]\n\t"
430 "std %2, [%6 + 8]\n\t"
431 "std %4, [%6 + 16]\n\t" : :
432 "r"(f), "r"(a1), "r"(a2), "r"(a3),
433 "r"(a4), "r"(a5),
434 "r"(&ccall_info.func));
435 }
436
437 /* Init receive/complete mapping, plus fire the IPI's off. */
438 {
439 register int i;
440
fb1fece5
KM
441 cpumask_clear_cpu(smp_processor_id(), &mask);
442 cpumask_and(&mask, cpu_online_mask, &mask);
8401707f 443 for (i = 0; i <= high; i++) {
fb1fece5 444 if (cpumask_test_cpu(i, &mask)) {
8401707f
KE
445 ccall_info.processors_in[i] = 0;
446 ccall_info.processors_out[i] = 0;
447 set_cpu_int(i, LEON3_IRQ_CROSS_CALL);
448
449 }
450 }
451 }
452
453 {
454 register int i;
455
456 i = 0;
457 do {
fb1fece5 458 if (!cpumask_test_cpu(i, &mask))
8401707f
KE
459 continue;
460
461 while (!ccall_info.processors_in[i])
462 barrier();
463 } while (++i <= high);
464
465 i = 0;
466 do {
fb1fece5 467 if (!cpumask_test_cpu(i, &mask))
8401707f
KE
468 continue;
469
470 while (!ccall_info.processors_out[i])
471 barrier();
472 } while (++i <= high);
473 }
474
475 spin_unlock_irqrestore(&cross_call_lock, flags);
476 }
477}
478
479/* Running cross calls. */
480void leon_cross_call_irq(void)
481{
482 int i = smp_processor_id();
483
484 ccall_info.processors_in[i] = 1;
485 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
486 ccall_info.arg4, ccall_info.arg5);
487 ccall_info.processors_out[i] = 1;
488}
489
2cf95304 490irqreturn_t leon_percpu_timer_interrupt(int irq, void *unused)
8401707f 491{
8401707f
KE
492 int cpu = smp_processor_id();
493
8401707f
KE
494 leon_clear_profile_irq(cpu);
495
496 profile_tick(CPU_PROFILING);
497
498 if (!--prof_counter(cpu)) {
2cf95304 499 int user = user_mode(get_irq_regs());
8401707f 500
8401707f 501 update_process_times(user);
8401707f
KE
502
503 prof_counter(cpu) = prof_multiplier(cpu);
504 }
2cf95304
DH
505
506 return IRQ_HANDLED;
8401707f
KE
507}
508
509static void __init smp_setup_percpu_timer(void)
510{
511 int cpu = smp_processor_id();
512
513 prof_counter(cpu) = prof_multiplier(cpu) = 1;
514}
515
516void __init leon_blackbox_id(unsigned *addr)
517{
518 int rd = *addr & 0x3e000000;
519 int rs1 = rd >> 11;
520
521 /* patch places where ___b_hard_smp_processor_id appears */
522 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
523 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
524 addr[2] = 0x01000000; /* nop */
525}
526
527void __init leon_blackbox_current(unsigned *addr)
528{
529 int rd = *addr & 0x3e000000;
530 int rs1 = rd >> 11;
531
532 /* patch LOAD_CURRENT macro where ___b_load_current appears */
533 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */
534 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */
535 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */
536
537}
538
8401707f
KE
539void __init leon_init_smp(void)
540{
541 /* Patch ipi15 trap table */
542 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
543
544 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id);
545 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current);
546 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM);
547 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id,
548 BTFIXUPCALL_NORM);
1ca0c808
DH
549 BTFIXUPSET_CALL(smp_ipi_resched, leon_ipi_resched, BTFIXUPCALL_NORM);
550 BTFIXUPSET_CALL(smp_ipi_single, leon_ipi_single, BTFIXUPCALL_NORM);
551 BTFIXUPSET_CALL(smp_ipi_mask_one, leon_ipi_mask_one, BTFIXUPCALL_NORM);
8401707f
KE
552}
553
554#endif /* CONFIG_SPARC_LEON */
This page took 0.187014 seconds and 5 git commands to generate.