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