blob: 960b113d0006f4ff61de665c2383dd4b522a4622 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* sun4m_smp.c: Sparc SUN4M SMP support.
2 *
3 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
4 */
5
6#include <asm/head.h>
7
8#include <linux/kernel.h>
9#include <linux/sched.h>
10#include <linux/threads.h>
11#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/interrupt.h>
13#include <linux/kernel_stat.h>
14#include <linux/init.h>
15#include <linux/spinlock.h>
16#include <linux/mm.h>
17#include <linux/swap.h>
18#include <linux/profile.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -080019#include <linux/delay.h>
Robert Reif4245e592008-10-12 20:52:26 -070020#include <linux/cpu.h>
Adrian Bunk6c81c322008-02-06 01:37:51 -080021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
Al Viro0d844382006-10-08 14:30:44 +010024#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ptrace.h>
27#include <asm/atomic.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/irq.h>
30#include <asm/page.h>
31#include <asm/pgalloc.h>
32#include <asm/pgtable.h>
33#include <asm/oplib.h>
34#include <asm/cpudata.h>
35
Al Viro32231a62007-07-21 19:18:57 -070036#include "irq.h"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define IRQ_CROSS_CALL 15
39
40extern ctxd_t *srmmu_ctx_table_phys;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042extern volatile unsigned long cpu_callin_map[NR_CPUS];
43extern unsigned char boot_cpu_id;
Bob Breuera54123e2006-03-23 22:36:19 -080044
45extern cpumask_t smp_commenced_mask;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047extern int __smp4m_processor_id(void);
48
49/*#define SMP_DEBUG*/
50
51#ifdef SMP_DEBUG
52#define SMP_PRINTK(x) printk x
53#else
54#define SMP_PRINTK(x)
55#endif
56
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080057static inline unsigned long
58swap_ulong(volatile unsigned long *ptr, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 __asm__ __volatile__("swap [%1], %0\n\t" :
61 "=&r" (val), "=&r" (ptr) :
62 "0" (val), "1" (ptr));
63 return val;
64}
65
66static void smp_setup_percpu_timer(void);
67extern void cpu_probe(void);
68
Bob Breuer92d452f2006-06-20 00:36:10 -070069void __cpuinit smp4m_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 int cpuid = hard_smp_processor_id();
72
73 local_flush_cache_all();
74 local_flush_tlb_all();
75
Manfred Spraule545a612008-09-07 16:57:22 +020076 notify_cpu_starting(cpuid);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* Get our local ticker going. */
79 smp_setup_percpu_timer();
80
81 calibrate_delay();
82 smp_store_cpu_info(cpuid);
83
84 local_flush_cache_all();
85 local_flush_tlb_all();
86
87 /*
88 * Unblock the master CPU _only_ when the scheduler state
89 * of all secondary CPUs will be up-to-date, so after
90 * the SMP initialization the master will be just allowed
91 * to call the scheduler code.
92 */
93 /* Allow master to continue. */
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080094 swap_ulong(&cpu_callin_map[cpuid], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Bob Breuera54123e2006-03-23 22:36:19 -080096 /* XXX: What's up with all the flushes? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 local_flush_cache_all();
98 local_flush_tlb_all();
99
100 cpu_probe();
101
102 /* Fix idle thread fields. */
103 __asm__ __volatile__("ld [%0], %%g6\n\t"
104 : : "r" (&current_set[cpuid])
105 : "memory" /* paranoid */);
106
107 /* Attach to the address space of init_task. */
108 atomic_inc(&init_mm.mm_count);
109 current->active_mm = &init_mm;
110
Bob Breuera54123e2006-03-23 22:36:19 -0800111 while (!cpu_isset(cpuid, smp_commenced_mask))
112 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 local_irq_enable();
Bob Breuera54123e2006-03-23 22:36:19 -0800115
Rusty Russellfe739712009-03-16 14:40:22 +1030116 set_cpu_online(cpuid, true);
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 */
122
123extern struct linux_prom_registers smp_penguin_ctable;
124extern unsigned long trapbase_cpu1[];
125extern unsigned long trapbase_cpu2[];
126extern unsigned long trapbase_cpu3[];
127
128void __init smp4m_boot_cpus(void)
129{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 smp_setup_percpu_timer();
131 local_flush_cache_all();
Bob Breuera54123e2006-03-23 22:36:19 -0800132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Bob Breuer92d452f2006-06-20 00:36:10 -0700134int __cpuinit smp4m_boot_one_cpu(int i)
Bob Breuera54123e2006-03-23 22:36:19 -0800135{
136 extern unsigned long sun4m_cpu_startup;
137 unsigned long *entry = &sun4m_cpu_startup;
138 struct task_struct *p;
139 int timeout;
140 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Bob Breuera54123e2006-03-23 22:36:19 -0800142 cpu_find_by_mid(i, &cpu_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Bob Breuera54123e2006-03-23 22:36:19 -0800144 /* Cook up an idler for this guy. */
145 p = fork_idle(i);
146 current_set[i] = task_thread_info(p);
147 /* See trampoline.S for details... */
148 entry += ((i-1) * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Bob Breuera54123e2006-03-23 22:36:19 -0800150 /*
151 * Initialize the contexts table
152 * Since the call to prom_startcpu() trashes the structure,
153 * we need to re-initialize it for each cpu
154 */
155 smp_penguin_ctable.which_io = 0;
156 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
157 smp_penguin_ctable.reg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Bob Breuera54123e2006-03-23 22:36:19 -0800159 /* whirrr, whirrr, whirrrrrrrrr... */
160 printk("Starting CPU %d at %p\n", i, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 local_flush_cache_all();
Bob Breuera54123e2006-03-23 22:36:19 -0800162 prom_startcpu(cpu_node,
163 &smp_penguin_ctable, 0, (char *)entry);
164
165 /* wheee... it's going... */
166 for(timeout = 0; timeout < 10000; timeout++) {
167 if(cpu_callin_map[i])
168 break;
169 udelay(200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Bob Breuera54123e2006-03-23 22:36:19 -0800172 if (!(cpu_callin_map[i])) {
173 printk("Processor %d is stuck.\n", i);
174 return -ENODEV;
175 }
176
177 local_flush_cache_all();
178 return 0;
179}
180
181void __init smp4m_smp_done(void)
182{
183 int i, first;
184 int *prev;
185
186 /* setup cpu list for irq rotation */
187 first = 0;
188 prev = &first;
Rusty Russellec7c14b2009-03-16 14:40:24 +1030189 for_each_online_cpu(i) {
190 *prev = i;
191 prev = &cpu_data(i).next;
Bob Breuera54123e2006-03-23 22:36:19 -0800192 }
193 *prev = first;
194 local_flush_cache_all();
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Free unneeded trap tables */
Bob Breuera54123e2006-03-23 22:36:19 -0800197 if (!cpu_isset(1, cpu_present_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ClearPageReserved(virt_to_page(trapbase_cpu1));
Nick Piggin7835e982006-03-22 00:08:40 -0800199 init_page_count(virt_to_page(trapbase_cpu1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 free_page((unsigned long)trapbase_cpu1);
201 totalram_pages++;
202 num_physpages++;
203 }
204 if (!cpu_isset(2, cpu_present_map)) {
205 ClearPageReserved(virt_to_page(trapbase_cpu2));
Nick Piggin7835e982006-03-22 00:08:40 -0800206 init_page_count(virt_to_page(trapbase_cpu2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 free_page((unsigned long)trapbase_cpu2);
208 totalram_pages++;
209 num_physpages++;
210 }
211 if (!cpu_isset(3, cpu_present_map)) {
212 ClearPageReserved(virt_to_page(trapbase_cpu3));
Nick Piggin7835e982006-03-22 00:08:40 -0800213 init_page_count(virt_to_page(trapbase_cpu3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 free_page((unsigned long)trapbase_cpu3);
215 totalram_pages++;
216 num_physpages++;
217 }
218
219 /* Ok, they are spinning and ready to go. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/* At each hardware IRQ, we get this called to forward IRQ reception
223 * to the next processor. The caller must disable the IRQ level being
224 * serviced globally so that there are no double interrupts received.
225 *
226 * XXX See sparc64 irq.c.
227 */
228void smp4m_irq_rotate(int cpu)
229{
Bob Breuera54123e2006-03-23 22:36:19 -0800230 int next = cpu_data(cpu).next;
231 if (next != cpu)
232 set_irq_udt(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static struct smp_funcall {
236 smpfunc_t func;
237 unsigned long arg1;
238 unsigned long arg2;
239 unsigned long arg3;
240 unsigned long arg4;
241 unsigned long arg5;
Bob Breuera54123e2006-03-23 22:36:19 -0800242 unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
243 unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244} ccall_info;
245
246static DEFINE_SPINLOCK(cross_call_lock);
247
248/* Cross calls must be serialized, at least currently. */
David S. Miller66e4f8c2008-08-27 20:03:22 -0700249static void smp4m_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700250 unsigned long arg2, unsigned long arg3,
David S. Miller66e4f8c2008-08-27 20:03:22 -0700251 unsigned long arg4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Bob Breuera54123e2006-03-23 22:36:19 -0800253 register int ncpus = SUN4M_NCPUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned long flags;
255
256 spin_lock_irqsave(&cross_call_lock, flags);
257
258 /* Init function glue. */
259 ccall_info.func = func;
260 ccall_info.arg1 = arg1;
261 ccall_info.arg2 = arg2;
262 ccall_info.arg3 = arg3;
263 ccall_info.arg4 = arg4;
David S. Miller66e4f8c2008-08-27 20:03:22 -0700264 ccall_info.arg5 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Init receive/complete mapping, plus fire the IPI's off. */
267 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 register int i;
269
270 cpu_clear(smp_processor_id(), mask);
David S. Miller66e4f8c2008-08-27 20:03:22 -0700271 cpus_and(mask, cpu_online_map, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 for(i = 0; i < ncpus; i++) {
273 if (cpu_isset(i, mask)) {
274 ccall_info.processors_in[i] = 0;
275 ccall_info.processors_out[i] = 0;
276 set_cpu_int(i, IRQ_CROSS_CALL);
277 } else {
278 ccall_info.processors_in[i] = 1;
279 ccall_info.processors_out[i] = 1;
280 }
281 }
282 }
283
284 {
285 register int i;
286
287 i = 0;
288 do {
David S. Miller66e4f8c2008-08-27 20:03:22 -0700289 if (!cpu_isset(i, mask))
290 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 while(!ccall_info.processors_in[i])
292 barrier();
293 } while(++i < ncpus);
294
295 i = 0;
296 do {
David S. Miller66e4f8c2008-08-27 20:03:22 -0700297 if (!cpu_isset(i, mask))
298 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 while(!ccall_info.processors_out[i])
300 barrier();
301 } while(++i < ncpus);
302 }
303
304 spin_unlock_irqrestore(&cross_call_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/* Running cross calls. */
308void smp4m_cross_call_irq(void)
309{
310 int i = smp_processor_id();
311
312 ccall_info.processors_in[i] = 1;
313 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
314 ccall_info.arg4, ccall_info.arg5);
315 ccall_info.processors_out[i] = 1;
316}
317
David S. Miller1de937a2008-09-13 22:07:56 -0700318extern void sun4m_clear_profile_irq(int cpu);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
321{
Al Viro0d844382006-10-08 14:30:44 +0100322 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int cpu = smp_processor_id();
324
Al Viro0d844382006-10-08 14:30:44 +0100325 old_regs = set_irq_regs(regs);
326
David S. Miller1de937a2008-09-13 22:07:56 -0700327 sun4m_clear_profile_irq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Al Viro0d844382006-10-08 14:30:44 +0100329 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if(!--prof_counter(cpu)) {
332 int user = user_mode(regs);
333
334 irq_enter();
335 update_process_times(user);
336 irq_exit();
337
338 prof_counter(cpu) = prof_multiplier(cpu);
339 }
Al Viro0d844382006-10-08 14:30:44 +0100340 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343extern unsigned int lvl14_resolution;
344
Al Viro409832f2008-11-22 17:33:54 +0000345static void __cpuinit smp_setup_percpu_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 int cpu = smp_processor_id();
348
349 prof_counter(cpu) = prof_multiplier(cpu) = 1;
350 load_profile_irq(cpu, lvl14_resolution);
351
352 if(cpu == boot_cpu_id)
353 enable_pil_irq(14);
354}
355
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700356static void __init smp4m_blackbox_id(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int rd = *addr & 0x3e000000;
359 int rs1 = rd >> 11;
360
361 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
362 addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
363 addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
364}
365
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700366static void __init smp4m_blackbox_current(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 int rd = *addr & 0x3e000000;
369 int rs1 = rd >> 11;
370
371 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
372 addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
David S. Miller4cad6912007-04-30 17:59:50 -0700373 addr[4] = 0x8008200c | rd | rs1; /* and reg, 0xc, reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376void __init sun4m_init_smp(void)
377{
378 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
379 BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
380 BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
382}