blob: 594768686525a9770b72ea09ef3aef19138ae29d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/profile.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -08009#include <linux/delay.h>
Robert Reif4245e592008-10-12 20:52:26 -070010#include <linux/cpu.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -080011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/cacheflush.h>
13#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Al Viro32231a62007-07-21 19:18:57 -070015#include "irq.h"
Sam Ravnborgaba20a82011-01-28 22:08:20 +000016#include "kernel.h"
Al Viro32231a62007-07-21 19:18:57 -070017
Daniel Hellstromecbc42b2011-05-02 00:08:53 +000018#define IRQ_IPI_SINGLE 12
19#define IRQ_IPI_MASK 13
20#define IRQ_IPI_RESCHED 14
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define IRQ_CROSS_CALL 15
22
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080023static inline unsigned long
24swap_ulong(volatile unsigned long *ptr, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 __asm__ __volatile__("swap [%1], %0\n\t" :
27 "=&r" (val), "=&r" (ptr) :
28 "0" (val), "1" (ptr));
29 return val;
30}
31
Daniel Hellstromecbc42b2011-05-02 00:08:53 +000032static void smp4m_ipi_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static void smp_setup_percpu_timer(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Bob Breuer92d452f2006-06-20 00:36:10 -070035void __cpuinit smp4m_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int cpuid = hard_smp_processor_id();
38
39 local_flush_cache_all();
40 local_flush_tlb_all();
41
Manfred Spraule545a612008-09-07 16:57:22 +020042 notify_cpu_starting(cpuid);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 /* Get our local ticker going. */
45 smp_setup_percpu_timer();
46
47 calibrate_delay();
48 smp_store_cpu_info(cpuid);
49
50 local_flush_cache_all();
51 local_flush_tlb_all();
52
53 /*
54 * Unblock the master CPU _only_ when the scheduler state
55 * of all secondary CPUs will be up-to-date, so after
56 * the SMP initialization the master will be just allowed
57 * to call the scheduler code.
58 */
59 /* Allow master to continue. */
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080060 swap_ulong(&cpu_callin_map[cpuid], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Bob Breuera54123e2006-03-23 22:36:19 -080062 /* XXX: What's up with all the flushes? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 local_flush_cache_all();
64 local_flush_tlb_all();
Sam Ravnborgaba20a82011-01-28 22:08:20 +000065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* Fix idle thread fields. */
67 __asm__ __volatile__("ld [%0], %%g6\n\t"
68 : : "r" (&current_set[cpuid])
69 : "memory" /* paranoid */);
70
71 /* Attach to the address space of init_task. */
72 atomic_inc(&init_mm.mm_count);
73 current->active_mm = &init_mm;
74
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -070075 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
Bob Breuera54123e2006-03-23 22:36:19 -080076 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 local_irq_enable();
Bob Breuera54123e2006-03-23 22:36:19 -080079
Rusty Russellfe739712009-03-16 14:40:22 +103080 set_cpu_online(cpuid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * Cycle through the processors asking the PROM to start each one.
85 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086void __init smp4m_boot_cpus(void)
87{
Daniel Hellstromecbc42b2011-05-02 00:08:53 +000088 smp4m_ipi_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 smp_setup_percpu_timer();
90 local_flush_cache_all();
Bob Breuera54123e2006-03-23 22:36:19 -080091}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Bob Breuer92d452f2006-06-20 00:36:10 -070093int __cpuinit smp4m_boot_one_cpu(int i)
Bob Breuera54123e2006-03-23 22:36:19 -080094{
Bob Breuera54123e2006-03-23 22:36:19 -080095 unsigned long *entry = &sun4m_cpu_startup;
96 struct task_struct *p;
97 int timeout;
98 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Bob Breuera54123e2006-03-23 22:36:19 -0800100 cpu_find_by_mid(i, &cpu_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Bob Breuera54123e2006-03-23 22:36:19 -0800102 /* Cook up an idler for this guy. */
103 p = fork_idle(i);
104 current_set[i] = task_thread_info(p);
105 /* See trampoline.S for details... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000106 entry += ((i - 1) * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Bob Breuera54123e2006-03-23 22:36:19 -0800108 /*
109 * Initialize the contexts table
110 * Since the call to prom_startcpu() trashes the structure,
111 * we need to re-initialize it for each cpu
112 */
113 smp_penguin_ctable.which_io = 0;
114 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
115 smp_penguin_ctable.reg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Bob Breuera54123e2006-03-23 22:36:19 -0800117 /* whirrr, whirrr, whirrrrrrrrr... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000118 printk(KERN_INFO "Starting CPU %d at %p\n", i, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 local_flush_cache_all();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000120 prom_startcpu(cpu_node, &smp_penguin_ctable, 0, (char *)entry);
Bob Breuera54123e2006-03-23 22:36:19 -0800121
122 /* wheee... it's going... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000123 for (timeout = 0; timeout < 10000; timeout++) {
124 if (cpu_callin_map[i])
Bob Breuera54123e2006-03-23 22:36:19 -0800125 break;
126 udelay(200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
Bob Breuera54123e2006-03-23 22:36:19 -0800129 if (!(cpu_callin_map[i])) {
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000130 printk(KERN_ERR "Processor %d is stuck.\n", i);
Bob Breuera54123e2006-03-23 22:36:19 -0800131 return -ENODEV;
132 }
133
134 local_flush_cache_all();
135 return 0;
136}
137
138void __init smp4m_smp_done(void)
139{
140 int i, first;
141 int *prev;
142
143 /* setup cpu list for irq rotation */
144 first = 0;
145 prev = &first;
Rusty Russellec7c14b2009-03-16 14:40:24 +1030146 for_each_online_cpu(i) {
147 *prev = i;
148 prev = &cpu_data(i).next;
Bob Breuera54123e2006-03-23 22:36:19 -0800149 }
150 *prev = first;
151 local_flush_cache_all();
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Ok, they are spinning and ready to go. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Daniel Hellstromecbc42b2011-05-02 00:08:53 +0000156
157/* Initialize IPIs on the SUN4M SMP machine */
158static void __init smp4m_ipi_init(void)
159{
160}
161
162static void smp4m_ipi_resched(int cpu)
163{
164 set_cpu_int(cpu, IRQ_IPI_RESCHED);
165}
166
167static void smp4m_ipi_single(int cpu)
168{
169 set_cpu_int(cpu, IRQ_IPI_SINGLE);
170}
171
172static void smp4m_ipi_mask_one(int cpu)
173{
174 set_cpu_int(cpu, IRQ_IPI_MASK);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static struct smp_funcall {
178 smpfunc_t func;
179 unsigned long arg1;
180 unsigned long arg2;
181 unsigned long arg3;
182 unsigned long arg4;
183 unsigned long arg5;
Bob Breuera54123e2006-03-23 22:36:19 -0800184 unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
185 unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186} ccall_info;
187
188static DEFINE_SPINLOCK(cross_call_lock);
189
190/* Cross calls must be serialized, at least currently. */
David S. Miller66e4f8c2008-08-27 20:03:22 -0700191static void smp4m_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700192 unsigned long arg2, unsigned long arg3,
David S. Miller66e4f8c2008-08-27 20:03:22 -0700193 unsigned long arg4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Bob Breuera54123e2006-03-23 22:36:19 -0800195 register int ncpus = SUN4M_NCPUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned long flags;
197
198 spin_lock_irqsave(&cross_call_lock, flags);
199
200 /* Init function glue. */
201 ccall_info.func = func;
202 ccall_info.arg1 = arg1;
203 ccall_info.arg2 = arg2;
204 ccall_info.arg3 = arg3;
205 ccall_info.arg4 = arg4;
David S. Miller66e4f8c2008-08-27 20:03:22 -0700206 ccall_info.arg5 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* Init receive/complete mapping, plus fire the IPI's off. */
209 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 register int i;
211
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700212 cpumask_clear_cpu(smp_processor_id(), &mask);
213 cpumask_and(&mask, cpu_online_mask, &mask);
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000214 for (i = 0; i < ncpus; i++) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700215 if (cpumask_test_cpu(i, &mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ccall_info.processors_in[i] = 0;
217 ccall_info.processors_out[i] = 0;
218 set_cpu_int(i, IRQ_CROSS_CALL);
219 } else {
220 ccall_info.processors_in[i] = 1;
221 ccall_info.processors_out[i] = 1;
222 }
223 }
224 }
225
226 {
227 register int i;
228
229 i = 0;
230 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700231 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700232 continue;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000233 while (!ccall_info.processors_in[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 barrier();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000235 } while (++i < ncpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 i = 0;
238 do {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700239 if (!cpumask_test_cpu(i, &mask))
David S. Miller66e4f8c2008-08-27 20:03:22 -0700240 continue;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000241 while (!ccall_info.processors_out[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 barrier();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000243 } while (++i < ncpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 spin_unlock_irqrestore(&cross_call_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/* Running cross calls. */
249void smp4m_cross_call_irq(void)
250{
251 int i = smp_processor_id();
252
253 ccall_info.processors_in[i] = 1;
254 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
255 ccall_info.arg4, ccall_info.arg5);
256 ccall_info.processors_out[i] = 1;
257}
258
259void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
260{
Al Viro0d844382006-10-08 14:30:44 +0100261 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int cpu = smp_processor_id();
263
Al Viro0d844382006-10-08 14:30:44 +0100264 old_regs = set_irq_regs(regs);
265
David S. Miller1de937a2008-09-13 22:07:56 -0700266 sun4m_clear_profile_irq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Al Viro0d844382006-10-08 14:30:44 +0100268 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000270 if (!--prof_counter(cpu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int user = user_mode(regs);
272
273 irq_enter();
274 update_process_times(user);
275 irq_exit();
276
277 prof_counter(cpu) = prof_multiplier(cpu);
278 }
Al Viro0d844382006-10-08 14:30:44 +0100279 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Al Viro409832f2008-11-22 17:33:54 +0000282static void __cpuinit smp_setup_percpu_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 int cpu = smp_processor_id();
285
286 prof_counter(cpu) = prof_multiplier(cpu) = 1;
287 load_profile_irq(cpu, lvl14_resolution);
288
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000289 if (cpu == boot_cpu_id)
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000290 sun4m_unmask_profile_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700293static void __init smp4m_blackbox_id(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 int rd = *addr & 0x3e000000;
296 int rs1 = rd >> 11;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000299 addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
301}
302
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700303static void __init smp4m_blackbox_current(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 int rd = *addr & 0x3e000000;
306 int rs1 = rd >> 11;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000309 addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
David S. Miller4cad6912007-04-30 17:59:50 -0700310 addr[4] = 0x8008200c | rd | rs1; /* and reg, 0xc, reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313void __init sun4m_init_smp(void)
314{
315 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
316 BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
317 BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
Daniel Hellstromecbc42b2011-05-02 00:08:53 +0000319 BTFIXUPSET_CALL(smp_ipi_resched, smp4m_ipi_resched, BTFIXUPCALL_NORM);
320 BTFIXUPSET_CALL(smp_ipi_single, smp4m_ipi_single, BTFIXUPCALL_NORM);
321 BTFIXUPSET_CALL(smp_ipi_mask_one, smp4m_ipi_mask_one, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}