xref: /linux/arch/sparc/kernel/leon_smp.c (revision c75c5ab575af7db707689cdbb5a5c458e9a034bb)
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>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/of.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>
26 #include <linux/cpu.h>
27 #include <linux/clockchips.h>
28 
29 #include <asm/cacheflush.h>
30 #include <asm/tlbflush.h>
31 
32 #include <asm/ptrace.h>
33 #include <linux/atomic.h>
34 #include <asm/irq_regs.h>
35 #include <asm/traps.h>
36 
37 #include <asm/delay.h>
38 #include <asm/irq.h>
39 #include <asm/page.h>
40 #include <asm/pgalloc.h>
41 #include <asm/pgtable.h>
42 #include <asm/oplib.h>
43 #include <asm/cpudata.h>
44 #include <asm/asi.h>
45 #include <asm/leon.h>
46 #include <asm/leon_amba.h>
47 #include <asm/timer.h>
48 
49 #include "kernel.h"
50 
51 #include "irq.h"
52 
53 extern ctxd_t *srmmu_ctx_table_phys;
54 static int smp_processors_ready;
55 extern volatile unsigned long cpu_callin_map[NR_CPUS];
56 extern cpumask_t smp_commenced_mask;
57 void __cpuinit leon_configure_cache_smp(void);
58 static void leon_ipi_init(void);
59 
60 /* IRQ number of LEON IPIs */
61 int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT;
62 
63 static inline unsigned long do_swap(volatile unsigned long *ptr,
64 				    unsigned long val)
65 {
66 	__asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val)
67 			     : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
68 			     : "memory");
69 	return val;
70 }
71 
72 void __cpuinit leon_cpu_pre_starting(void *arg)
73 {
74 	leon_configure_cache_smp();
75 }
76 
77 void __cpuinit leon_cpu_pre_online(void *arg)
78 {
79 	int cpuid = hard_smp_processor_id();
80 
81 	/* Allow master to continue. The master will then give us the
82 	 * go-ahead by setting the smp_commenced_mask and will wait without
83 	 * timeouts until our setup is completed fully (signified by
84 	 * our bit being set in the cpu_online_mask).
85 	 */
86 	do_swap(&cpu_callin_map[cpuid], 1);
87 
88 	local_ops->cache_all();
89 	local_ops->tlb_all();
90 
91 	/* Fix idle thread fields. */
92 	__asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
93 			     : "memory" /* paranoid */);
94 
95 	/* Attach to the address space of init_task. */
96 	atomic_inc(&init_mm.mm_count);
97 	current->active_mm = &init_mm;
98 
99 	while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
100 		mb();
101 }
102 
103 /*
104  *	Cycle through the processors asking the PROM to start each one.
105  */
106 
107 extern struct linux_prom_registers smp_penguin_ctable;
108 
109 void __cpuinit leon_configure_cache_smp(void)
110 {
111 	unsigned long cfg = sparc_leon3_get_dcachecfg();
112 	int me = smp_processor_id();
113 
114 	if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) {
115 		printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n",
116 		     (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg),
117 		     (unsigned int)cfg, (unsigned int)me);
118 		sparc_leon3_disable_cache();
119 	} else {
120 		if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) {
121 			sparc_leon3_enable_snooping();
122 		} else {
123 			printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n",
124 			     me);
125 			sparc_leon3_disable_cache();
126 		}
127 	}
128 
129 	local_ops->cache_all();
130 	local_ops->tlb_all();
131 }
132 
133 void leon_smp_setbroadcast(unsigned int mask)
134 {
135 	int broadcast =
136 	    ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
137 	      LEON3_IRQMPSTATUS_BROADCAST) & 1);
138 	if (!broadcast) {
139 		prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n",
140 		     leon_smp_nrcpus());
141 		if (leon_smp_nrcpus() > 1) {
142 			BUG();
143 		} else {
144 			prom_printf("continue anyway\n");
145 			return;
146 		}
147 	}
148 	LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
149 }
150 
151 unsigned int leon_smp_getbroadcast(void)
152 {
153 	unsigned int mask;
154 	mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
155 	return mask;
156 }
157 
158 int leon_smp_nrcpus(void)
159 {
160 	int nrcpu =
161 	    ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
162 	      LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
163 	return nrcpu;
164 }
165 
166 void __init leon_boot_cpus(void)
167 {
168 	int nrcpu = leon_smp_nrcpus();
169 	int me = smp_processor_id();
170 
171 	/* Setup IPI */
172 	leon_ipi_init();
173 
174 	printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me,
175 	       (unsigned int)nrcpu, (unsigned int)NR_CPUS,
176 	       (unsigned int)&(leon3_irqctrl_regs->mpstatus));
177 
178 	leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me);
179 	leon_enable_irq_cpu(LEON3_IRQ_TICKER, me);
180 	leon_enable_irq_cpu(leon_ipi_irq, me);
181 
182 	leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
183 
184 	leon_configure_cache_smp();
185 	local_ops->cache_all();
186 
187 }
188 
189 int __cpuinit leon_boot_one_cpu(int i, struct task_struct *idle)
190 {
191 	int timeout;
192 
193 	current_set[i] = task_thread_info(idle);
194 
195 	/* See trampoline.S:leon_smp_cpu_startup for details...
196 	 * Initialize the contexts table
197 	 * Since the call to prom_startcpu() trashes the structure,
198 	 * we need to re-initialize it for each cpu
199 	 */
200 	smp_penguin_ctable.which_io = 0;
201 	smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys;
202 	smp_penguin_ctable.reg_size = 0;
203 
204 	/* whirrr, whirrr, whirrrrrrrrr... */
205 	printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i,
206 	       (unsigned int)&leon3_irqctrl_regs->mpstatus);
207 	local_ops->cache_all();
208 
209 	/* Make sure all IRQs are of from the start for this new CPU */
210 	LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0);
211 
212 	/* Wake one CPU */
213 	LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
214 
215 	/* wheee... it's going... */
216 	for (timeout = 0; timeout < 10000; timeout++) {
217 		if (cpu_callin_map[i])
218 			break;
219 		udelay(200);
220 	}
221 	printk(KERN_INFO "Started CPU %d\n", (unsigned int)i);
222 
223 	if (!(cpu_callin_map[i])) {
224 		printk(KERN_ERR "Processor %d is stuck.\n", i);
225 		return -ENODEV;
226 	} else {
227 		leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i);
228 		leon_enable_irq_cpu(LEON3_IRQ_TICKER, i);
229 		leon_enable_irq_cpu(leon_ipi_irq, i);
230 	}
231 
232 	local_ops->cache_all();
233 	return 0;
234 }
235 
236 void __init leon_smp_done(void)
237 {
238 
239 	int i, first;
240 	int *prev;
241 
242 	/* setup cpu list for irq rotation */
243 	first = 0;
244 	prev = &first;
245 	for (i = 0; i < NR_CPUS; i++) {
246 		if (cpu_online(i)) {
247 			*prev = i;
248 			prev = &cpu_data(i).next;
249 		}
250 	}
251 	*prev = first;
252 	local_ops->cache_all();
253 
254 	/* Free unneeded trap tables */
255 	if (!cpu_present(1)) {
256 		ClearPageReserved(virt_to_page(&trapbase_cpu1));
257 		init_page_count(virt_to_page(&trapbase_cpu1));
258 		free_page((unsigned long)&trapbase_cpu1);
259 		totalram_pages++;
260 		num_physpages++;
261 	}
262 	if (!cpu_present(2)) {
263 		ClearPageReserved(virt_to_page(&trapbase_cpu2));
264 		init_page_count(virt_to_page(&trapbase_cpu2));
265 		free_page((unsigned long)&trapbase_cpu2);
266 		totalram_pages++;
267 		num_physpages++;
268 	}
269 	if (!cpu_present(3)) {
270 		ClearPageReserved(virt_to_page(&trapbase_cpu3));
271 		init_page_count(virt_to_page(&trapbase_cpu3));
272 		free_page((unsigned long)&trapbase_cpu3);
273 		totalram_pages++;
274 		num_physpages++;
275 	}
276 	/* Ok, they are spinning and ready to go. */
277 	smp_processors_ready = 1;
278 
279 }
280 
281 void leon_irq_rotate(int cpu)
282 {
283 }
284 
285 struct leon_ipi_work {
286 	int single;
287 	int msk;
288 	int resched;
289 };
290 
291 static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work);
292 
293 /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ
294  * is used for all three types of IPIs.
295  */
296 static void __init leon_ipi_init(void)
297 {
298 	int cpu, len;
299 	struct leon_ipi_work *work;
300 	struct property *pp;
301 	struct device_node *rootnp;
302 	struct tt_entry *trap_table;
303 	unsigned long flags;
304 
305 	/* Find IPI IRQ or stick with default value */
306 	rootnp = of_find_node_by_path("/ambapp0");
307 	if (rootnp) {
308 		pp = of_find_property(rootnp, "ipi_num", &len);
309 		if (pp && (*(int *)pp->value))
310 			leon_ipi_irq = *(int *)pp->value;
311 	}
312 	printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq);
313 
314 	/* Adjust so that we jump directly to smpleon_ipi */
315 	local_irq_save(flags);
316 	trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)];
317 	trap_table->inst_three += smpleon_ipi - real_irq_entry;
318 	local_ops->cache_all();
319 	local_irq_restore(flags);
320 
321 	for_each_possible_cpu(cpu) {
322 		work = &per_cpu(leon_ipi_work, cpu);
323 		work->single = work->msk = work->resched = 0;
324 	}
325 }
326 
327 static void leon_send_ipi(int cpu, int level)
328 {
329 	unsigned long mask;
330 	mask = leon_get_irqmask(level);
331 	LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
332 }
333 
334 static void leon_ipi_single(int cpu)
335 {
336 	struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
337 
338 	/* Mark work */
339 	work->single = 1;
340 
341 	/* Generate IRQ on the CPU */
342 	leon_send_ipi(cpu, leon_ipi_irq);
343 }
344 
345 static void leon_ipi_mask_one(int cpu)
346 {
347 	struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
348 
349 	/* Mark work */
350 	work->msk = 1;
351 
352 	/* Generate IRQ on the CPU */
353 	leon_send_ipi(cpu, leon_ipi_irq);
354 }
355 
356 static void leon_ipi_resched(int cpu)
357 {
358 	struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu);
359 
360 	/* Mark work */
361 	work->resched = 1;
362 
363 	/* Generate IRQ on the CPU (any IRQ will cause resched) */
364 	leon_send_ipi(cpu, leon_ipi_irq);
365 }
366 
367 void leonsmp_ipi_interrupt(void)
368 {
369 	struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work);
370 
371 	if (work->single) {
372 		work->single = 0;
373 		smp_call_function_single_interrupt();
374 	}
375 	if (work->msk) {
376 		work->msk = 0;
377 		smp_call_function_interrupt();
378 	}
379 	if (work->resched) {
380 		work->resched = 0;
381 		smp_resched_interrupt();
382 	}
383 }
384 
385 static struct smp_funcall {
386 	smpfunc_t func;
387 	unsigned long arg1;
388 	unsigned long arg2;
389 	unsigned long arg3;
390 	unsigned long arg4;
391 	unsigned long arg5;
392 	unsigned long processors_in[NR_CPUS];	/* Set when ipi entered. */
393 	unsigned long processors_out[NR_CPUS];	/* Set when ipi exited. */
394 } ccall_info;
395 
396 static DEFINE_SPINLOCK(cross_call_lock);
397 
398 /* Cross calls must be serialized, at least currently. */
399 static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
400 			    unsigned long arg2, unsigned long arg3,
401 			    unsigned long arg4)
402 {
403 	if (smp_processors_ready) {
404 		register int high = NR_CPUS - 1;
405 		unsigned long flags;
406 
407 		spin_lock_irqsave(&cross_call_lock, flags);
408 
409 		{
410 			/* If you make changes here, make sure gcc generates proper code... */
411 			register smpfunc_t f asm("i0") = func;
412 			register unsigned long a1 asm("i1") = arg1;
413 			register unsigned long a2 asm("i2") = arg2;
414 			register unsigned long a3 asm("i3") = arg3;
415 			register unsigned long a4 asm("i4") = arg4;
416 			register unsigned long a5 asm("i5") = 0;
417 
418 			__asm__ __volatile__("std %0, [%6]\n\t"
419 					     "std %2, [%6 + 8]\n\t"
420 					     "std %4, [%6 + 16]\n\t" : :
421 					     "r"(f), "r"(a1), "r"(a2), "r"(a3),
422 					     "r"(a4), "r"(a5),
423 					     "r"(&ccall_info.func));
424 		}
425 
426 		/* Init receive/complete mapping, plus fire the IPI's off. */
427 		{
428 			register int i;
429 
430 			cpumask_clear_cpu(smp_processor_id(), &mask);
431 			cpumask_and(&mask, cpu_online_mask, &mask);
432 			for (i = 0; i <= high; i++) {
433 				if (cpumask_test_cpu(i, &mask)) {
434 					ccall_info.processors_in[i] = 0;
435 					ccall_info.processors_out[i] = 0;
436 					leon_send_ipi(i, LEON3_IRQ_CROSS_CALL);
437 
438 				}
439 			}
440 		}
441 
442 		{
443 			register int i;
444 
445 			i = 0;
446 			do {
447 				if (!cpumask_test_cpu(i, &mask))
448 					continue;
449 
450 				while (!ccall_info.processors_in[i])
451 					barrier();
452 			} while (++i <= high);
453 
454 			i = 0;
455 			do {
456 				if (!cpumask_test_cpu(i, &mask))
457 					continue;
458 
459 				while (!ccall_info.processors_out[i])
460 					barrier();
461 			} while (++i <= high);
462 		}
463 
464 		spin_unlock_irqrestore(&cross_call_lock, flags);
465 	}
466 }
467 
468 /* Running cross calls. */
469 void leon_cross_call_irq(void)
470 {
471 	int i = smp_processor_id();
472 
473 	ccall_info.processors_in[i] = 1;
474 	ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
475 			ccall_info.arg4, ccall_info.arg5);
476 	ccall_info.processors_out[i] = 1;
477 }
478 
479 static const struct sparc32_ipi_ops leon_ipi_ops = {
480 	.cross_call = leon_cross_call,
481 	.resched    = leon_ipi_resched,
482 	.single     = leon_ipi_single,
483 	.mask_one   = leon_ipi_mask_one,
484 };
485 
486 void __init leon_init_smp(void)
487 {
488 	/* Patch ipi15 trap table */
489 	t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
490 
491 	sparc32_ipi_ops = &leon_ipi_ops;
492 }
493