blob: 576fe74d226b262191ff2cf2d8a7d0cca7cd00c9 [file] [log] [blame]
Sam Ravnborge54f8542011-01-28 22:08:21 +00001/* Sparc SS1000/SC2000 SMP support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
4 *
5 * Based on sun4m's smp.c, which is:
6 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
7 */
8
Tkhai Kirill62f08282012-04-04 21:49:26 +02009#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/profile.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -080012#include <linux/delay.h>
Robert Reif4245e592008-10-12 20:52:26 -070013#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Tkhai Kirill62f08282012-04-04 21:49:26 +020015#include <asm/cacheflush.h>
16#include <asm/switch_to.h>
17#include <asm/tlbflush.h>
18#include <asm/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/sbi.h>
Sam Ravnborge54f8542011-01-28 22:08:21 +000020#include <asm/mmu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Sam Ravnborge54f8542011-01-28 22:08:21 +000022#include "kernel.h"
Al Viro32231a62007-07-21 19:18:57 -070023#include "irq.h"
Sam Ravnborge54f8542011-01-28 22:08:21 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define IRQ_CROSS_CALL 15
26
Sam Ravnborge54f8542011-01-28 22:08:21 +000027static volatile int smp_processors_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int smp_highest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Millera638f252009-01-08 16:47:17 -080030static inline unsigned long sun4d_swap(volatile unsigned long *ptr, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 __asm__ __volatile__("swap [%1], %0\n\t" :
33 "=&r" (val), "=&r" (ptr) :
34 "0" (val), "1" (ptr));
35 return val;
36}
37
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +000038static void smp4d_ipi_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
David S. Miller7b1af322008-09-02 01:17:41 -070040static unsigned char cpu_leds[32];
41
42static inline void show_leds(int cpuid)
43{
44 cpuid &= 0x1e;
45 __asm__ __volatile__ ("stba %0, [%1] %2" : :
46 "r" ((cpu_leds[cpuid] << 4) | cpu_leds[cpuid+1]),
47 "r" (ECSR_BASE(cpuid) | BB_LEDS),
48 "i" (ASI_M_CTL));
49}
50
Al Viro409832f2008-11-22 17:33:54 +000051void __cpuinit smp4d_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 int cpuid = hard_smp4d_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unsigned long flags;
Sam Ravnborge54f8542011-01-28 22:08:21 +000055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* Show we are alive */
57 cpu_leds[cpuid] = 0x6;
58 show_leds(cpuid);
59
60 /* Enable level15 interrupt, disable level14 interrupt for now */
61 cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
62
63 local_flush_cache_all();
64 local_flush_tlb_all();
65
Manfred Spraule545a612008-09-07 16:57:22 +020066 notify_cpu_starting(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /*
68 * Unblock the master CPU _only_ when the scheduler state
69 * of all secondary CPUs will be up-to-date, so after
70 * the SMP initialization the master will be just allowed
71 * to call the scheduler code.
72 */
73 /* Get our local ticker going. */
Tkhai Kirill62f08282012-04-04 21:49:26 +020074 register_percpu_ce(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 calibrate_delay();
77 smp_store_cpu_info(cpuid);
78 local_flush_cache_all();
79 local_flush_tlb_all();
80
81 /* Allow master to continue. */
David S. Millera638f252009-01-08 16:47:17 -080082 sun4d_swap((unsigned long *)&cpu_callin_map[cpuid], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 local_flush_cache_all();
84 local_flush_tlb_all();
Sam Ravnborge54f8542011-01-28 22:08:21 +000085
Sam Ravnborge54f8542011-01-28 22:08:21 +000086 while ((unsigned long)current_set[cpuid] < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +000088
89 while (current_set[cpuid]->cpu != cpuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +000091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* Fix idle thread fields. */
93 __asm__ __volatile__("ld [%0], %%g6\n\t"
94 : : "r" (&current_set[cpuid])
95 : "memory" /* paranoid */);
96
97 cpu_leds[cpuid] = 0x9;
98 show_leds(cpuid);
Sam Ravnborge54f8542011-01-28 22:08:21 +000099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* Attach to the address space of init_task. */
101 atomic_inc(&init_mm.mm_count);
102 current->active_mm = &init_mm;
103
104 local_flush_cache_all();
105 local_flush_tlb_all();
Sam Ravnborge54f8542011-01-28 22:08:21 +0000106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 local_irq_enable(); /* We don't allow PIL 14 yet */
Sam Ravnborge54f8542011-01-28 22:08:21 +0000108
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700109 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 barrier();
111
112 spin_lock_irqsave(&sun4d_imsk_lock, flags);
113 cc_set_imsk(cc_get_imsk() & ~0x4000); /* Allow PIL 14 as well */
114 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
Rusty Russellfe739712009-03-16 14:40:22 +1030115 set_cpu_online(cpuid, true);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * Cycle through the processors asking the PROM to start each one.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122void __init smp4d_boot_cpus(void)
123{
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000124 smp4d_ipi_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (boot_cpu_id)
126 current_set[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Al Virob4cff842007-02-01 13:52:33 +0000130int __cpuinit smp4d_boot_one_cpu(int i)
Raymond Burns8b3c8482006-07-17 21:57:09 -0700131{
Sam Ravnborge54f8542011-01-28 22:08:21 +0000132 unsigned long *entry = &sun4d_cpu_startup;
133 struct task_struct *p;
134 int timeout;
135 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Sam Ravnborge54f8542011-01-28 22:08:21 +0000137 cpu_find_by_instance(i, &cpu_node, NULL);
138 /* Cook up an idler for this guy. */
139 p = fork_idle(i);
140 current_set[i] = task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Sam Ravnborge54f8542011-01-28 22:08:21 +0000142 /*
143 * Initialize the contexts table
144 * Since the call to prom_startcpu() trashes the structure,
145 * we need to re-initialize it for each cpu
146 */
147 smp_penguin_ctable.which_io = 0;
148 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
149 smp_penguin_ctable.reg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Sam Ravnborge54f8542011-01-28 22:08:21 +0000151 /* whirrr, whirrr, whirrrrrrrrr... */
152 printk(KERN_INFO "Starting CPU %d at %p\n", i, entry);
153 local_flush_cache_all();
154 prom_startcpu(cpu_node,
155 &smp_penguin_ctable, 0, (char *)entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Sam Ravnborge54f8542011-01-28 22:08:21 +0000157 printk(KERN_INFO "prom_startcpu returned :)\n");
158
159 /* wheee... it's going... */
160 for (timeout = 0; timeout < 10000; timeout++) {
161 if (cpu_callin_map[i])
162 break;
163 udelay(200);
164 }
165
Raymond Burns8b3c8482006-07-17 21:57:09 -0700166 if (!(cpu_callin_map[i])) {
Sam Ravnborge54f8542011-01-28 22:08:21 +0000167 printk(KERN_ERR "Processor %d is stuck.\n", i);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700168 return -ENODEV;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 local_flush_cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700172 return 0;
173}
174
175void __init smp4d_smp_done(void)
176{
177 int i, first;
178 int *prev;
179
180 /* setup cpu list for irq rotation */
181 first = 0;
182 prev = &first;
Rusty Russellec7c14b2009-03-16 14:40:24 +1030183 for_each_online_cpu(i) {
184 *prev = i;
185 prev = &cpu_data(i).next;
186 }
Raymond Burns8b3c8482006-07-17 21:57:09 -0700187 *prev = first;
188 local_flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* Ok, they are spinning and ready to go. */
191 smp_processors_ready = 1;
192 sun4d_distribute_irqs();
193}
194
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000195/* Memory structure giving interrupt handler information about IPI generated */
196struct sun4d_ipi_work {
197 int single;
198 int msk;
199 int resched;
200};
201
202static DEFINE_PER_CPU_SHARED_ALIGNED(struct sun4d_ipi_work, sun4d_ipi_work);
203
204/* Initialize IPIs on the SUN4D SMP machine */
205static void __init smp4d_ipi_init(void)
206{
207 int cpu;
208 struct sun4d_ipi_work *work;
209
210 printk(KERN_INFO "smp4d: setup IPI at IRQ %d\n", SUN4D_IPI_IRQ);
211
212 for_each_possible_cpu(cpu) {
213 work = &per_cpu(sun4d_ipi_work, cpu);
214 work->single = work->msk = work->resched = 0;
215 }
216}
217
218void sun4d_ipi_interrupt(void)
219{
220 struct sun4d_ipi_work *work = &__get_cpu_var(sun4d_ipi_work);
221
222 if (work->single) {
223 work->single = 0;
224 smp_call_function_single_interrupt();
225 }
226 if (work->msk) {
227 work->msk = 0;
228 smp_call_function_interrupt();
229 }
230 if (work->resched) {
231 work->resched = 0;
232 smp_resched_interrupt();
233 }
234}
235
236static void smp4d_ipi_single(int cpu)
237{
238 struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
239
240 /* Mark work */
241 work->single = 1;
242
243 /* Generate IRQ on the CPU */
244 sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
245}
246
247static void smp4d_ipi_mask_one(int cpu)
248{
249 struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
250
251 /* Mark work */
252 work->msk = 1;
253
254 /* Generate IRQ on the CPU */
255 sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
256}
257
258static void smp4d_ipi_resched(int cpu)
259{
260 struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
261
262 /* Mark work */
263 work->resched = 1;
264
265 /* Generate IRQ on the CPU (any IRQ will cause resched) */
266 sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static struct smp_funcall {
270 smpfunc_t func;
271 unsigned long arg1;
272 unsigned long arg2;
273 unsigned long arg3;
274 unsigned long arg4;
275 unsigned long arg5;
276 unsigned char processors_in[NR_CPUS]; /* Set when ipi entered. */
277 unsigned char processors_out[NR_CPUS]; /* Set when ipi exited. */
278} ccall_info __attribute__((aligned(8)));
279
280static DEFINE_SPINLOCK(cross_call_lock);
281
282/* Cross calls must be serialized, at least currently. */
David S. Miller66e4f8c2008-08-27 20:03:22 -0700283static void smp4d_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
284 unsigned long arg2, unsigned long arg3,
285 unsigned long arg4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Sam Ravnborge54f8542011-01-28 22:08:21 +0000287 if (smp_processors_ready) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 register int high = smp_highest_cpu;
289 unsigned long flags;
290
291 spin_lock_irqsave(&cross_call_lock, flags);
292
293 {
Sam Ravnborge54f8542011-01-28 22:08:21 +0000294 /*
295 * If you make changes here, make sure
296 * gcc generates proper code...
297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 register smpfunc_t f asm("i0") = func;
299 register unsigned long a1 asm("i1") = arg1;
300 register unsigned long a2 asm("i2") = arg2;
301 register unsigned long a3 asm("i3") = arg3;
302 register unsigned long a4 asm("i4") = arg4;
David S. Miller66e4f8c2008-08-27 20:03:22 -0700303 register unsigned long a5 asm("i5") = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 __asm__ __volatile__(
306 "std %0, [%6]\n\t"
307 "std %2, [%6 + 8]\n\t"
308 "std %4, [%6 + 16]\n\t" : :
309 "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
310 "r" (&ccall_info.func));
311 }
312
313 /* Init receive/complete mapping, plus fire the IPI's off. */
314 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 register int i;
316
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700317 cpumask_clear_cpu(smp_processor_id(), &mask);
318 cpumask_and(&mask, cpu_online_mask, &mask);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000319 for (i = 0; i <= high; i++) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700320 if (cpumask_test_cpu(i, &mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 ccall_info.processors_in[i] = 0;
322 ccall_info.processors_out[i] = 0;
323 sun4d_send_ipi(i, IRQ_CROSS_CALL);
324 }
325 }
326 }
327
328 {
329 register int i;
330
331 i = 0;
332 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700333 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700334 continue;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000335 while (!ccall_info.processors_in[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +0000337 } while (++i <= high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 i = 0;
340 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700341 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700342 continue;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000343 while (!ccall_info.processors_out[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +0000345 } while (++i <= high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 spin_unlock_irqrestore(&cross_call_lock, flags);
349 }
350}
351
352/* Running cross calls. */
353void smp4d_cross_call_irq(void)
354{
355 int i = hard_smp4d_processor_id();
356
357 ccall_info.processors_in[i] = 1;
358 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
359 ccall_info.arg4, ccall_info.arg5);
360 ccall_info.processors_out[i] = 1;
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
364{
Al Viro0d844382006-10-08 14:30:44 +0100365 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int cpu = hard_smp4d_processor_id();
Tkhai Kirill62f08282012-04-04 21:49:26 +0200367 struct clock_event_device *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 static int cpu_tick[NR_CPUS];
369 static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
370
Al Viro0d844382006-10-08 14:30:44 +0100371 old_regs = set_irq_regs(regs);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000372 bw_get_prof_limit(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 bw_clear_intr_mask(0, 1); /* INTR_TABLE[0] & 1 is Profile IRQ */
374
375 cpu_tick[cpu]++;
376 if (!(cpu_tick[cpu] & 15)) {
377 if (cpu_tick[cpu] == 0x60)
378 cpu_tick[cpu] = 0;
379 cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
380 show_leds(cpu);
381 }
382
Tkhai Kirill62f08282012-04-04 21:49:26 +0200383 ce = &per_cpu(sparc32_clockevent, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Tkhai Kirill62f08282012-04-04 21:49:26 +0200385 irq_enter();
386 ce->event_handler(ce);
387 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Al Viro0d844382006-10-08 14:30:44 +0100389 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392void __init smp4d_blackbox_id(unsigned *addr)
393{
394 int rd = *addr & 0x3e000000;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
Sam Ravnborge54f8542011-01-28 22:08:21 +0000397 addr[1] = 0x01000000; /* nop */
398 addr[2] = 0x01000000; /* nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401void __init smp4d_blackbox_current(unsigned *addr)
402{
403 int rd = *addr & 0x3e000000;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 addr[0] = 0xc0800800 | rd; /* lda [%g0] ASI_M_VIKING_TMP1, reg */
406 addr[2] = 0x81282002 | rd | (rd >> 11); /* sll reg, 2, reg */
407 addr[4] = 0x01000000; /* nop */
408}
409
410void __init sun4d_init_smp(void)
411{
412 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* Patch ipi15 trap table */
415 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_sun4d - linux_trap_ipi15_sun4m);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* And set btfixup... */
418 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4d_blackbox_id);
419 BTFIXUPSET_BLACKBOX(load_current, smp4d_blackbox_current);
420 BTFIXUPSET_CALL(smp_cross_call, smp4d_cross_call, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4d_processor_id, BTFIXUPCALL_NORM);
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000422 BTFIXUPSET_CALL(smp_ipi_resched, smp4d_ipi_resched, BTFIXUPCALL_NORM);
423 BTFIXUPSET_CALL(smp_ipi_single, smp4d_ipi_single, BTFIXUPCALL_NORM);
424 BTFIXUPSET_CALL(smp_ipi_mask_one, smp4d_ipi_mask_one, BTFIXUPCALL_NORM);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 for (i = 0; i < NR_CPUS; i++) {
427 ccall_info.processors_in[i] = 1;
428 ccall_info.processors_out[i] = 1;
429 }
430}