blob: ddaea31de586d47e72c013a565000bdec6a9645f [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>
David S. Miller5d83d662012-05-13 20:49:31 -070013#include <linux/sched.h>
Robert Reif4245e592008-10-12 20:52:26 -070014#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Tkhai Kirill62f08282012-04-04 21:49:26 +020016#include <asm/cacheflush.h>
17#include <asm/switch_to.h>
18#include <asm/tlbflush.h>
19#include <asm/timer.h>
David S. Miller5d83d662012-05-13 20:49:31 -070020#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/sbi.h>
Sam Ravnborge54f8542011-01-28 22:08:21 +000022#include <asm/mmu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Sam Ravnborge54f8542011-01-28 22:08:21 +000024#include "kernel.h"
Al Viro32231a62007-07-21 19:18:57 -070025#include "irq.h"
Sam Ravnborge54f8542011-01-28 22:08:21 +000026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define IRQ_CROSS_CALL 15
28
Sam Ravnborge54f8542011-01-28 22:08:21 +000029static volatile int smp_processors_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int smp_highest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
David S. Millera638f252009-01-08 16:47:17 -080032static inline unsigned long sun4d_swap(volatile unsigned long *ptr, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 __asm__ __volatile__("swap [%1], %0\n\t" :
35 "=&r" (val), "=&r" (ptr) :
36 "0" (val), "1" (ptr));
37 return val;
38}
39
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +000040static void smp4d_ipi_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David S. Miller7b1af322008-09-02 01:17:41 -070042static unsigned char cpu_leds[32];
43
44static inline void show_leds(int cpuid)
45{
46 cpuid &= 0x1e;
47 __asm__ __volatile__ ("stba %0, [%1] %2" : :
48 "r" ((cpu_leds[cpuid] << 4) | cpu_leds[cpuid+1]),
49 "r" (ECSR_BASE(cpuid) | BB_LEDS),
50 "i" (ASI_M_CTL));
51}
52
Al Viro409832f2008-11-22 17:33:54 +000053void __cpuinit smp4d_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
David S. Millerc68e5d32012-05-13 23:09:04 -070055 int cpuid = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned long flags;
Sam Ravnborge54f8542011-01-28 22:08:21 +000057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* Show we are alive */
59 cpu_leds[cpuid] = 0x6;
60 show_leds(cpuid);
61
62 /* Enable level15 interrupt, disable level14 interrupt for now */
63 cc_set_imsk((cc_get_imsk() & ~0x8000) | 0x4000);
64
David S. Miller5d83d662012-05-13 20:49:31 -070065 local_ops->cache_all();
66 local_ops->tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Manfred Spraule545a612008-09-07 16:57:22 +020068 notify_cpu_starting(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /*
70 * Unblock the master CPU _only_ when the scheduler state
71 * of all secondary CPUs will be up-to-date, so after
72 * the SMP initialization the master will be just allowed
73 * to call the scheduler code.
74 */
75 /* Get our local ticker going. */
Tkhai Kirill62f08282012-04-04 21:49:26 +020076 register_percpu_ce(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 calibrate_delay();
79 smp_store_cpu_info(cpuid);
David S. Miller5d83d662012-05-13 20:49:31 -070080 local_ops->cache_all();
81 local_ops->tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* Allow master to continue. */
David S. Millera638f252009-01-08 16:47:17 -080084 sun4d_swap((unsigned long *)&cpu_callin_map[cpuid], 1);
David S. Miller5d83d662012-05-13 20:49:31 -070085 local_ops->cache_all();
86 local_ops->tlb_all();
Sam Ravnborge54f8542011-01-28 22:08:21 +000087
Sam Ravnborge54f8542011-01-28 22:08:21 +000088 while ((unsigned long)current_set[cpuid] < PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +000090
91 while (current_set[cpuid]->cpu != cpuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +000093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* Fix idle thread fields. */
95 __asm__ __volatile__("ld [%0], %%g6\n\t"
96 : : "r" (&current_set[cpuid])
97 : "memory" /* paranoid */);
98
99 cpu_leds[cpuid] = 0x9;
100 show_leds(cpuid);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 /* Attach to the address space of init_task. */
103 atomic_inc(&init_mm.mm_count);
104 current->active_mm = &init_mm;
105
David S. Miller5d83d662012-05-13 20:49:31 -0700106 local_ops->cache_all();
107 local_ops->tlb_all();
Sam Ravnborge54f8542011-01-28 22:08:21 +0000108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 local_irq_enable(); /* We don't allow PIL 14 yet */
Sam Ravnborge54f8542011-01-28 22:08:21 +0000110
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700111 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 barrier();
113
114 spin_lock_irqsave(&sun4d_imsk_lock, flags);
115 cc_set_imsk(cc_get_imsk() & ~0x4000); /* Allow PIL 14 as well */
116 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
Rusty Russellfe739712009-03-16 14:40:22 +1030117 set_cpu_online(cpuid, true);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * Cycle through the processors asking the PROM to start each one.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124void __init smp4d_boot_cpus(void)
125{
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000126 smp4d_ipi_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (boot_cpu_id)
128 current_set[0] = NULL;
David S. Miller5d83d662012-05-13 20:49:31 -0700129 local_ops->cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Thomas Gleixnerf0a2bc72012-04-20 13:05:56 +0000132int __cpuinit smp4d_boot_one_cpu(int i, struct task_struct *idle)
Raymond Burns8b3c8482006-07-17 21:57:09 -0700133{
Sam Ravnborge54f8542011-01-28 22:08:21 +0000134 unsigned long *entry = &sun4d_cpu_startup;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000135 int timeout;
136 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Sam Ravnborge54f8542011-01-28 22:08:21 +0000138 cpu_find_by_instance(i, &cpu_node, NULL);
Thomas Gleixnerf0a2bc72012-04-20 13:05:56 +0000139 current_set[i] = task_thread_info(idle);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000140 /*
141 * Initialize the contexts table
142 * Since the call to prom_startcpu() trashes the structure,
143 * we need to re-initialize it for each cpu
144 */
145 smp_penguin_ctable.which_io = 0;
146 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
147 smp_penguin_ctable.reg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Sam Ravnborge54f8542011-01-28 22:08:21 +0000149 /* whirrr, whirrr, whirrrrrrrrr... */
150 printk(KERN_INFO "Starting CPU %d at %p\n", i, entry);
David S. Miller5d83d662012-05-13 20:49:31 -0700151 local_ops->cache_all();
Sam Ravnborge54f8542011-01-28 22:08:21 +0000152 prom_startcpu(cpu_node,
153 &smp_penguin_ctable, 0, (char *)entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Sam Ravnborge54f8542011-01-28 22:08:21 +0000155 printk(KERN_INFO "prom_startcpu returned :)\n");
156
157 /* wheee... it's going... */
158 for (timeout = 0; timeout < 10000; timeout++) {
159 if (cpu_callin_map[i])
160 break;
161 udelay(200);
162 }
163
Raymond Burns8b3c8482006-07-17 21:57:09 -0700164 if (!(cpu_callin_map[i])) {
Sam Ravnborge54f8542011-01-28 22:08:21 +0000165 printk(KERN_ERR "Processor %d is stuck.\n", i);
Raymond Burns8b3c8482006-07-17 21:57:09 -0700166 return -ENODEV;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
David S. Miller5d83d662012-05-13 20:49:31 -0700169 local_ops->cache_all();
Raymond Burns8b3c8482006-07-17 21:57:09 -0700170 return 0;
171}
172
173void __init smp4d_smp_done(void)
174{
175 int i, first;
176 int *prev;
177
178 /* setup cpu list for irq rotation */
179 first = 0;
180 prev = &first;
Rusty Russellec7c14b2009-03-16 14:40:24 +1030181 for_each_online_cpu(i) {
182 *prev = i;
183 prev = &cpu_data(i).next;
184 }
Raymond Burns8b3c8482006-07-17 21:57:09 -0700185 *prev = first;
David S. Miller5d83d662012-05-13 20:49:31 -0700186 local_ops->cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Ok, they are spinning and ready to go. */
189 smp_processors_ready = 1;
190 sun4d_distribute_irqs();
191}
192
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000193/* Memory structure giving interrupt handler information about IPI generated */
194struct sun4d_ipi_work {
195 int single;
196 int msk;
197 int resched;
198};
199
200static DEFINE_PER_CPU_SHARED_ALIGNED(struct sun4d_ipi_work, sun4d_ipi_work);
201
202/* Initialize IPIs on the SUN4D SMP machine */
203static void __init smp4d_ipi_init(void)
204{
205 int cpu;
206 struct sun4d_ipi_work *work;
207
208 printk(KERN_INFO "smp4d: setup IPI at IRQ %d\n", SUN4D_IPI_IRQ);
209
210 for_each_possible_cpu(cpu) {
211 work = &per_cpu(sun4d_ipi_work, cpu);
212 work->single = work->msk = work->resched = 0;
213 }
214}
215
216void sun4d_ipi_interrupt(void)
217{
218 struct sun4d_ipi_work *work = &__get_cpu_var(sun4d_ipi_work);
219
220 if (work->single) {
221 work->single = 0;
222 smp_call_function_single_interrupt();
223 }
224 if (work->msk) {
225 work->msk = 0;
226 smp_call_function_interrupt();
227 }
228 if (work->resched) {
229 work->resched = 0;
230 smp_resched_interrupt();
231 }
232}
233
Sam Ravnborg4ba22b12012-05-14 15:14:36 +0200234/* +-------+-------------+-----------+------------------------------------+
235 * | bcast | devid | sid | levels mask |
236 * +-------+-------------+-----------+------------------------------------+
237 * 31 30 23 22 15 14 0
238 */
239#define IGEN_MESSAGE(bcast, devid, sid, levels) \
240 (((bcast) << 31) | ((devid) << 23) | ((sid) << 15) | (levels))
241
242static void sun4d_send_ipi(int cpu, int level)
243{
244 cc_set_igen(IGEN_MESSAGE(0, cpu << 3, 6 + ((level >> 1) & 7), 1 << (level - 1)));
245}
246
247static void sun4d_ipi_single(int cpu)
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000248{
249 struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
250
251 /* Mark work */
252 work->single = 1;
253
254 /* Generate IRQ on the CPU */
255 sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
256}
257
Sam Ravnborg4ba22b12012-05-14 15:14:36 +0200258static void sun4d_ipi_mask_one(int cpu)
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000259{
260 struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
261
262 /* Mark work */
263 work->msk = 1;
264
265 /* Generate IRQ on the CPU */
266 sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
267}
268
Sam Ravnborg4ba22b12012-05-14 15:14:36 +0200269static void sun4d_ipi_resched(int cpu)
Daniel Hellstrom55dd23e2011-05-02 00:08:54 +0000270{
271 struct sun4d_ipi_work *work = &per_cpu(sun4d_ipi_work, cpu);
272
273 /* Mark work */
274 work->resched = 1;
275
276 /* Generate IRQ on the CPU (any IRQ will cause resched) */
277 sun4d_send_ipi(cpu, SUN4D_IPI_IRQ);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static struct smp_funcall {
281 smpfunc_t func;
282 unsigned long arg1;
283 unsigned long arg2;
284 unsigned long arg3;
285 unsigned long arg4;
286 unsigned long arg5;
287 unsigned char processors_in[NR_CPUS]; /* Set when ipi entered. */
288 unsigned char processors_out[NR_CPUS]; /* Set when ipi exited. */
289} ccall_info __attribute__((aligned(8)));
290
291static DEFINE_SPINLOCK(cross_call_lock);
292
293/* Cross calls must be serialized, at least currently. */
Sam Ravnborg4ba22b12012-05-14 15:14:36 +0200294static void sun4d_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
David S. Miller66e4f8c2008-08-27 20:03:22 -0700295 unsigned long arg2, unsigned long arg3,
296 unsigned long arg4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Sam Ravnborge54f8542011-01-28 22:08:21 +0000298 if (smp_processors_ready) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 register int high = smp_highest_cpu;
300 unsigned long flags;
301
302 spin_lock_irqsave(&cross_call_lock, flags);
303
304 {
Sam Ravnborge54f8542011-01-28 22:08:21 +0000305 /*
306 * If you make changes here, make sure
307 * gcc generates proper code...
308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 register smpfunc_t f asm("i0") = func;
310 register unsigned long a1 asm("i1") = arg1;
311 register unsigned long a2 asm("i2") = arg2;
312 register unsigned long a3 asm("i3") = arg3;
313 register unsigned long a4 asm("i4") = arg4;
David S. Miller66e4f8c2008-08-27 20:03:22 -0700314 register unsigned long a5 asm("i5") = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 __asm__ __volatile__(
317 "std %0, [%6]\n\t"
318 "std %2, [%6 + 8]\n\t"
319 "std %4, [%6 + 16]\n\t" : :
320 "r"(f), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
321 "r" (&ccall_info.func));
322 }
323
324 /* Init receive/complete mapping, plus fire the IPI's off. */
325 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 register int i;
327
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700328 cpumask_clear_cpu(smp_processor_id(), &mask);
329 cpumask_and(&mask, cpu_online_mask, &mask);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000330 for (i = 0; i <= high; i++) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700331 if (cpumask_test_cpu(i, &mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ccall_info.processors_in[i] = 0;
333 ccall_info.processors_out[i] = 0;
334 sun4d_send_ipi(i, IRQ_CROSS_CALL);
335 }
336 }
337 }
338
339 {
340 register int i;
341
342 i = 0;
343 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700344 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700345 continue;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000346 while (!ccall_info.processors_in[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +0000348 } while (++i <= high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 i = 0;
351 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700352 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700353 continue;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000354 while (!ccall_info.processors_out[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 barrier();
Sam Ravnborge54f8542011-01-28 22:08:21 +0000356 } while (++i <= high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 spin_unlock_irqrestore(&cross_call_lock, flags);
360 }
361}
362
363/* Running cross calls. */
364void smp4d_cross_call_irq(void)
365{
David S. Millerc68e5d32012-05-13 23:09:04 -0700366 int i = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 ccall_info.processors_in[i] = 1;
369 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
370 ccall_info.arg4, ccall_info.arg5);
371 ccall_info.processors_out[i] = 1;
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374void smp4d_percpu_timer_interrupt(struct pt_regs *regs)
375{
Al Viro0d844382006-10-08 14:30:44 +0100376 struct pt_regs *old_regs;
David S. Millerc68e5d32012-05-13 23:09:04 -0700377 int cpu = hard_smp_processor_id();
Tkhai Kirill62f08282012-04-04 21:49:26 +0200378 struct clock_event_device *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 static int cpu_tick[NR_CPUS];
380 static char led_mask[] = { 0xe, 0xd, 0xb, 0x7, 0xb, 0xd };
381
Al Viro0d844382006-10-08 14:30:44 +0100382 old_regs = set_irq_regs(regs);
Sam Ravnborge54f8542011-01-28 22:08:21 +0000383 bw_get_prof_limit(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 bw_clear_intr_mask(0, 1); /* INTR_TABLE[0] & 1 is Profile IRQ */
385
386 cpu_tick[cpu]++;
387 if (!(cpu_tick[cpu] & 15)) {
388 if (cpu_tick[cpu] == 0x60)
389 cpu_tick[cpu] = 0;
390 cpu_leds[cpu] = led_mask[cpu_tick[cpu] >> 4];
391 show_leds(cpu);
392 }
393
Tkhai Kirill62f08282012-04-04 21:49:26 +0200394 ce = &per_cpu(sparc32_clockevent, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Tkhai Kirill62f08282012-04-04 21:49:26 +0200396 irq_enter();
397 ce->event_handler(ce);
398 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Al Viro0d844382006-10-08 14:30:44 +0100400 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Sam Ravnborg4ba22b12012-05-14 15:14:36 +0200403static const struct sparc32_ipi_ops sun4d_ipi_ops = {
404 .cross_call = sun4d_cross_call,
405 .resched = sun4d_ipi_resched,
406 .single = sun4d_ipi_single,
407 .mask_one = sun4d_ipi_mask_one,
408};
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410void __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
Sam Ravnborg4ba22b12012-05-14 15:14:36 +0200417 sparc32_ipi_ops = &sun4d_ipi_ops;
Sam Ravnborge54f8542011-01-28 22:08:21 +0000418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 for (i = 0; i < NR_CPUS; i++) {
420 ccall_info.processors_in[i] = 1;
421 ccall_info.processors_out[i] = 1;
422 }
423}