blob: afcf6743f0eb859902161a2686183567119188c8 [file] [log] [blame]
Sam Ravnborgaba20a82011-01-28 22:08:20 +00001/*
2 * sun4m SMP support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 */
6
Tkhai Kirill62f08282012-04-04 21:49:26 +02007#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/profile.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -080010#include <linux/delay.h>
David S. Miller5d83d662012-05-13 20:49:31 -070011#include <linux/sched.h>
Robert Reif4245e592008-10-12 20:52:26 -070012#include <linux/cpu.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -080013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/cacheflush.h>
Sam Ravnborgbde4d8b2012-03-30 15:53:50 +020015#include <asm/switch_to.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/tlbflush.h>
Tkhai Kirill62f08282012-04-04 21:49:26 +020017#include <asm/timer.h>
David S. Miller5d83d662012-05-13 20:49:31 -070018#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Al Viro32231a62007-07-21 19:18:57 -070020#include "irq.h"
Sam Ravnborgaba20a82011-01-28 22:08:20 +000021#include "kernel.h"
Al Viro32231a62007-07-21 19:18:57 -070022
Daniel Hellstromecbc42b2011-05-02 00:08:53 +000023#define IRQ_IPI_SINGLE 12
24#define IRQ_IPI_MASK 13
25#define IRQ_IPI_RESCHED 14
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define IRQ_CROSS_CALL 15
27
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080028static inline unsigned long
29swap_ulong(volatile unsigned long *ptr, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 __asm__ __volatile__("swap [%1], %0\n\t" :
32 "=&r" (val), "=&r" (ptr) :
33 "0" (val), "1" (ptr));
34 return val;
35}
36
Daniel Hellstromecbc42b2011-05-02 00:08:53 +000037static void smp4m_ipi_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Bob Breuer92d452f2006-06-20 00:36:10 -070039void __cpuinit smp4m_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 int cpuid = hard_smp_processor_id();
42
David S. Miller5d83d662012-05-13 20:49:31 -070043 local_ops->cache_all();
44 local_ops->tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Manfred Spraule545a612008-09-07 16:57:22 +020046 notify_cpu_starting(cpuid);
47
Tkhai Kirill62f08282012-04-04 21:49:26 +020048 register_percpu_ce(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 calibrate_delay();
51 smp_store_cpu_info(cpuid);
52
David S. Miller5d83d662012-05-13 20:49:31 -070053 local_ops->cache_all();
54 local_ops->tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 /*
57 * Unblock the master CPU _only_ when the scheduler state
58 * of all secondary CPUs will be up-to-date, so after
59 * the SMP initialization the master will be just allowed
60 * to call the scheduler code.
61 */
62 /* Allow master to continue. */
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080063 swap_ulong(&cpu_callin_map[cpuid], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Bob Breuera54123e2006-03-23 22:36:19 -080065 /* XXX: What's up with all the flushes? */
David S. Miller5d83d662012-05-13 20:49:31 -070066 local_ops->cache_all();
67 local_ops->tlb_all();
Sam Ravnborgaba20a82011-01-28 22:08:20 +000068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /* Fix idle thread fields. */
70 __asm__ __volatile__("ld [%0], %%g6\n\t"
71 : : "r" (&current_set[cpuid])
72 : "memory" /* paranoid */);
73
74 /* Attach to the address space of init_task. */
75 atomic_inc(&init_mm.mm_count);
76 current->active_mm = &init_mm;
77
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -070078 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
Bob Breuera54123e2006-03-23 22:36:19 -080079 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 local_irq_enable();
Bob Breuera54123e2006-03-23 22:36:19 -080082
Rusty Russellfe739712009-03-16 14:40:22 +103083 set_cpu_online(cpuid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * Cycle through the processors asking the PROM to start each one.
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089void __init smp4m_boot_cpus(void)
90{
Daniel Hellstromecbc42b2011-05-02 00:08:53 +000091 smp4m_ipi_init();
Tkhai Kirill62f08282012-04-04 21:49:26 +020092 sun4m_unmask_profile_irq();
David S. Miller5d83d662012-05-13 20:49:31 -070093 local_ops->cache_all();
Bob Breuera54123e2006-03-23 22:36:19 -080094}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Bob Breuer92d452f2006-06-20 00:36:10 -070096int __cpuinit smp4m_boot_one_cpu(int i)
Bob Breuera54123e2006-03-23 22:36:19 -080097{
Bob Breuera54123e2006-03-23 22:36:19 -080098 unsigned long *entry = &sun4m_cpu_startup;
99 struct task_struct *p;
100 int timeout;
101 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Bob Breuera54123e2006-03-23 22:36:19 -0800103 cpu_find_by_mid(i, &cpu_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Bob Breuera54123e2006-03-23 22:36:19 -0800105 /* Cook up an idler for this guy. */
106 p = fork_idle(i);
107 current_set[i] = task_thread_info(p);
108 /* See trampoline.S for details... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000109 entry += ((i - 1) * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Bob Breuera54123e2006-03-23 22:36:19 -0800111 /*
112 * Initialize the contexts table
113 * Since the call to prom_startcpu() trashes the structure,
114 * we need to re-initialize it for each cpu
115 */
116 smp_penguin_ctable.which_io = 0;
117 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
118 smp_penguin_ctable.reg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Bob Breuera54123e2006-03-23 22:36:19 -0800120 /* whirrr, whirrr, whirrrrrrrrr... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000121 printk(KERN_INFO "Starting CPU %d at %p\n", i, entry);
David S. Miller5d83d662012-05-13 20:49:31 -0700122 local_ops->cache_all();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000123 prom_startcpu(cpu_node, &smp_penguin_ctable, 0, (char *)entry);
Bob Breuera54123e2006-03-23 22:36:19 -0800124
125 /* wheee... it's going... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000126 for (timeout = 0; timeout < 10000; timeout++) {
127 if (cpu_callin_map[i])
Bob Breuera54123e2006-03-23 22:36:19 -0800128 break;
129 udelay(200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131
Bob Breuera54123e2006-03-23 22:36:19 -0800132 if (!(cpu_callin_map[i])) {
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000133 printk(KERN_ERR "Processor %d is stuck.\n", i);
Bob Breuera54123e2006-03-23 22:36:19 -0800134 return -ENODEV;
135 }
136
David S. Miller5d83d662012-05-13 20:49:31 -0700137 local_ops->cache_all();
Bob Breuera54123e2006-03-23 22:36:19 -0800138 return 0;
139}
140
141void __init smp4m_smp_done(void)
142{
143 int i, first;
144 int *prev;
145
146 /* setup cpu list for irq rotation */
147 first = 0;
148 prev = &first;
Rusty Russellec7c14b2009-03-16 14:40:24 +1030149 for_each_online_cpu(i) {
150 *prev = i;
151 prev = &cpu_data(i).next;
Bob Breuera54123e2006-03-23 22:36:19 -0800152 }
153 *prev = first;
David S. Miller5d83d662012-05-13 20:49:31 -0700154 local_ops->cache_all();
Bob Breuera54123e2006-03-23 22:36:19 -0800155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* Ok, they are spinning and ready to go. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Daniel Hellstromecbc42b2011-05-02 00:08:53 +0000159
160/* Initialize IPIs on the SUN4M SMP machine */
161static void __init smp4m_ipi_init(void)
162{
163}
164
165static void smp4m_ipi_resched(int cpu)
166{
167 set_cpu_int(cpu, IRQ_IPI_RESCHED);
168}
169
170static void smp4m_ipi_single(int cpu)
171{
172 set_cpu_int(cpu, IRQ_IPI_SINGLE);
173}
174
175static void smp4m_ipi_mask_one(int cpu)
176{
177 set_cpu_int(cpu, IRQ_IPI_MASK);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct smp_funcall {
181 smpfunc_t func;
182 unsigned long arg1;
183 unsigned long arg2;
184 unsigned long arg3;
185 unsigned long arg4;
186 unsigned long arg5;
Bob Breuera54123e2006-03-23 22:36:19 -0800187 unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
188 unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189} ccall_info;
190
191static DEFINE_SPINLOCK(cross_call_lock);
192
193/* Cross calls must be serialized, at least currently. */
David S. Miller66e4f8c2008-08-27 20:03:22 -0700194static void smp4m_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700195 unsigned long arg2, unsigned long arg3,
David S. Miller66e4f8c2008-08-27 20:03:22 -0700196 unsigned long arg4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Bob Breuera54123e2006-03-23 22:36:19 -0800198 register int ncpus = SUN4M_NCPUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned long flags;
200
201 spin_lock_irqsave(&cross_call_lock, flags);
202
203 /* Init function glue. */
204 ccall_info.func = func;
205 ccall_info.arg1 = arg1;
206 ccall_info.arg2 = arg2;
207 ccall_info.arg3 = arg3;
208 ccall_info.arg4 = arg4;
David S. Miller66e4f8c2008-08-27 20:03:22 -0700209 ccall_info.arg5 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* Init receive/complete mapping, plus fire the IPI's off. */
212 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 register int i;
214
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700215 cpumask_clear_cpu(smp_processor_id(), &mask);
216 cpumask_and(&mask, cpu_online_mask, &mask);
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000217 for (i = 0; i < ncpus; i++) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700218 if (cpumask_test_cpu(i, &mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ccall_info.processors_in[i] = 0;
220 ccall_info.processors_out[i] = 0;
221 set_cpu_int(i, IRQ_CROSS_CALL);
222 } else {
223 ccall_info.processors_in[i] = 1;
224 ccall_info.processors_out[i] = 1;
225 }
226 }
227 }
228
229 {
230 register int i;
231
232 i = 0;
233 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700234 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700235 continue;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000236 while (!ccall_info.processors_in[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 barrier();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000238 } while (++i < ncpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 i = 0;
241 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700242 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700243 continue;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000244 while (!ccall_info.processors_out[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 barrier();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000246 } while (++i < ncpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 spin_unlock_irqrestore(&cross_call_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/* Running cross calls. */
252void smp4m_cross_call_irq(void)
253{
254 int i = smp_processor_id();
255
256 ccall_info.processors_in[i] = 1;
257 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
258 ccall_info.arg4, ccall_info.arg5);
259 ccall_info.processors_out[i] = 1;
260}
261
262void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
263{
Al Viro0d844382006-10-08 14:30:44 +0100264 struct pt_regs *old_regs;
Tkhai Kirill62f08282012-04-04 21:49:26 +0200265 struct clock_event_device *ce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int cpu = smp_processor_id();
267
Al Viro0d844382006-10-08 14:30:44 +0100268 old_regs = set_irq_regs(regs);
269
Tkhai Kirill62f08282012-04-04 21:49:26 +0200270 ce = &per_cpu(sparc32_clockevent, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Tkhai Kirill62f08282012-04-04 21:49:26 +0200272 if (ce->mode & CLOCK_EVT_MODE_PERIODIC)
273 sun4m_clear_profile_irq(cpu);
274 else
275 load_profile_irq(cpu, 0); /* Is this needless? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Tkhai Kirill62f08282012-04-04 21:49:26 +0200277 irq_enter();
278 ce->event_handler(ce);
279 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Al Viro0d844382006-10-08 14:30:44 +0100281 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700284static void __init smp4m_blackbox_id(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 int rd = *addr & 0x3e000000;
287 int rs1 = rd >> 11;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000290 addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
292}
293
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700294static void __init smp4m_blackbox_current(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 int rd = *addr & 0x3e000000;
297 int rs1 = rd >> 11;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000300 addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
David S. Miller4cad6912007-04-30 17:59:50 -0700301 addr[4] = 0x8008200c | rd | rs1; /* and reg, 0xc, reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304void __init sun4m_init_smp(void)
305{
306 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
307 BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
308 BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
Daniel Hellstromecbc42b2011-05-02 00:08:53 +0000310 BTFIXUPSET_CALL(smp_ipi_resched, smp4m_ipi_resched, BTFIXUPCALL_NORM);
311 BTFIXUPSET_CALL(smp_ipi_single, smp4m_ipi_single, BTFIXUPCALL_NORM);
312 BTFIXUPSET_CALL(smp_ipi_mask_one, smp4m_ipi_mask_one, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}