xref: /openbmc/linux/arch/sparc/kernel/leon_smp.c (revision a09d2831)
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/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 
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
28 
29 #include <asm/ptrace.h>
30 #include <asm/atomic.h>
31 #include <asm/irq_regs.h>
32 
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>
43 
44 #ifdef CONFIG_SPARC_LEON
45 
46 #include "irq.h"
47 
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);
54 
55 static inline unsigned long do_swap(volatile unsigned long *ptr,
56 				    unsigned long val)
57 {
58 	__asm__ __volatile__("swapa [%1] %2, %0\n\t" : "=&r"(val)
59 			     : "r"(ptr), "i"(ASI_LEON_DCACHE_MISS)
60 			     : "memory");
61 	return val;
62 }
63 
64 static void smp_setup_percpu_timer(void);
65 
66 void __cpuinit leon_callin(void)
67 {
68 	int cpuid = hard_smpleon_processor_id();
69 
70 	local_flush_cache_all();
71 	local_flush_tlb_all();
72 	leon_configure_cache_smp();
73 
74 	/* Get our local ticker going. */
75 	smp_setup_percpu_timer();
76 
77 	calibrate_delay();
78 	smp_store_cpu_info(cpuid);
79 
80 	local_flush_cache_all();
81 	local_flush_tlb_all();
82 
83 	/*
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.
89 	 */
90 	do_swap(&cpu_callin_map[cpuid], 1);
91 
92 	local_flush_cache_all();
93 	local_flush_tlb_all();
94 
95 	cpu_probe();
96 
97 	/* Fix idle thread fields. */
98 	__asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(&current_set[cpuid])
99 			     : "memory" /* paranoid */);
100 
101 	/* Attach to the address space of init_task. */
102 	atomic_inc(&init_mm.mm_count);
103 	current->active_mm = &init_mm;
104 
105 	while (!cpu_isset(cpuid, smp_commenced_mask))
106 		mb();
107 
108 	local_irq_enable();
109 	cpu_set(cpuid, cpu_online_map);
110 }
111 
112 /*
113  *	Cycle through the processors asking the PROM to start each one.
114  */
115 
116 extern struct linux_prom_registers smp_penguin_ctable;
117 
118 void __init leon_configure_cache_smp(void)
119 {
120 	unsigned long cfg = sparc_leon3_get_dcachecfg();
121 	int me = smp_processor_id();
122 
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();
135 		}
136 	}
137 
138 	local_flush_cache_all();
139 	local_flush_tlb_all();
140 }
141 
142 void leon_smp_setbroadcast(unsigned int mask)
143 {
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;
155 		}
156 	}
157 	LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask);
158 }
159 
160 unsigned int leon_smp_getbroadcast(void)
161 {
162 	unsigned int mask;
163 	mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast));
164 	return mask;
165 }
166 
167 int leon_smp_nrcpus(void)
168 {
169 	int nrcpu =
170 	    ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >>
171 	      LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1;
172 	return nrcpu;
173 }
174 
175 void __init leon_boot_cpus(void)
176 {
177 	int nrcpu = leon_smp_nrcpus();
178 	int me = smp_processor_id();
179 
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));
183 
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);
187 
188 	leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER);
189 
190 	leon_configure_cache_smp();
191 	smp_setup_percpu_timer();
192 	local_flush_cache_all();
193 
194 }
195 
196 int __cpuinit leon_boot_one_cpu(int i)
197 {
198 
199 	struct task_struct *p;
200 	int timeout;
201 
202 	/* Cook up an idler for this guy. */
203 	p = fork_idle(i);
204 
205 	current_set[i] = task_thread_info(p);
206 
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
211 	 */
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;
215 
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();
220 
221 	LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i);
222 
223 	/* wheee... it's going... */
224 	for (timeout = 0; timeout < 10000; timeout++) {
225 		if (cpu_callin_map[i])
226 			break;
227 		udelay(200);
228 	}
229 	printk(KERN_INFO "Started CPU %d \n", (unsigned int)i);
230 
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);
238 	}
239 
240 	local_flush_cache_all();
241 	return 0;
242 }
243 
244 void __init leon_smp_done(void)
245 {
246 
247 	int i, first;
248 	int *prev;
249 
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;
257 		}
258 	}
259 	*prev = first;
260 	local_flush_cache_all();
261 
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++;
269 	}
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++;
276 	}
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++;
283 	}
284 	/* Ok, they are spinning and ready to go. */
285 	smp_processors_ready = 1;
286 
287 }
288 
289 void leon_irq_rotate(int cpu)
290 {
291 }
292 
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;
303 
304 static DEFINE_SPINLOCK(cross_call_lock);
305 
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)
310 {
311 	if (smp_processors_ready) {
312 		register int high = NR_CPUS - 1;
313 		unsigned long flags;
314 
315 		spin_lock_irqsave(&cross_call_lock, flags);
316 
317 		{
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;
325 
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));
332 		}
333 
334 		/* Init receive/complete mapping, plus fire the IPI's off. */
335 		{
336 			register int i;
337 
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);
345 
346 				}
347 			}
348 		}
349 
350 		{
351 			register int i;
352 
353 			i = 0;
354 			do {
355 				if (!cpu_isset(i, mask))
356 					continue;
357 
358 				while (!ccall_info.processors_in[i])
359 					barrier();
360 			} while (++i <= high);
361 
362 			i = 0;
363 			do {
364 				if (!cpu_isset(i, mask))
365 					continue;
366 
367 				while (!ccall_info.processors_out[i])
368 					barrier();
369 			} while (++i <= high);
370 		}
371 
372 		spin_unlock_irqrestore(&cross_call_lock, flags);
373 	}
374 }
375 
376 /* Running cross calls. */
377 void leon_cross_call_irq(void)
378 {
379 	int i = smp_processor_id();
380 
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;
385 }
386 
387 void leon_percpu_timer_interrupt(struct pt_regs *regs)
388 {
389 	struct pt_regs *old_regs;
390 	int cpu = smp_processor_id();
391 
392 	old_regs = set_irq_regs(regs);
393 
394 	leon_clear_profile_irq(cpu);
395 
396 	profile_tick(CPU_PROFILING);
397 
398 	if (!--prof_counter(cpu)) {
399 		int user = user_mode(regs);
400 
401 		irq_enter();
402 		update_process_times(user);
403 		irq_exit();
404 
405 		prof_counter(cpu) = prof_multiplier(cpu);
406 	}
407 	set_irq_regs(old_regs);
408 }
409 
410 static void __init smp_setup_percpu_timer(void)
411 {
412 	int cpu = smp_processor_id();
413 
414 	prof_counter(cpu) = prof_multiplier(cpu) = 1;
415 }
416 
417 void __init leon_blackbox_id(unsigned *addr)
418 {
419 	int rd = *addr & 0x3e000000;
420 	int rs1 = rd >> 11;
421 
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 */
426 }
427 
428 void __init leon_blackbox_current(unsigned *addr)
429 {
430 	int rd = *addr & 0x3e000000;
431 	int rs1 = rd >> 11;
432 
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 */
437 
438 }
439 
440 /*
441  * CPU idle callback function
442  * See .../arch/sparc/kernel/process.c
443  */
444 void pmc_leon_idle(void)
445 {
446 	__asm__ volatile ("mov %g0, %asr19");
447 }
448 
449 void __init leon_init_smp(void)
450 {
451 	/* Patch ipi15 trap table */
452 	t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m);
453 
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);
459 
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
465 
466 }
467 
468 #endif /* CONFIG_SPARC_LEON */
469