blob: 5545d10d50cd20f324ece1d1de2890d10428713b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#define IRQ_CROSS_CALL 15
19
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080020static inline unsigned long
21swap_ulong(volatile unsigned long *ptr, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 __asm__ __volatile__("swap [%1], %0\n\t" :
24 "=&r" (val), "=&r" (ptr) :
25 "0" (val), "1" (ptr));
26 return val;
27}
28
29static void smp_setup_percpu_timer(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Bob Breuer92d452f2006-06-20 00:36:10 -070031void __cpuinit smp4m_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 int cpuid = hard_smp_processor_id();
34
35 local_flush_cache_all();
36 local_flush_tlb_all();
37
Manfred Spraule545a612008-09-07 16:57:22 +020038 notify_cpu_starting(cpuid);
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /* Get our local ticker going. */
41 smp_setup_percpu_timer();
42
43 calibrate_delay();
44 smp_store_cpu_info(cpuid);
45
46 local_flush_cache_all();
47 local_flush_tlb_all();
48
49 /*
50 * Unblock the master CPU _only_ when the scheduler state
51 * of all secondary CPUs will be up-to-date, so after
52 * the SMP initialization the master will be just allowed
53 * to call the scheduler code.
54 */
55 /* Allow master to continue. */
Wu Fengguang1a8a27c2009-01-07 18:09:10 -080056 swap_ulong(&cpu_callin_map[cpuid], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Bob Breuera54123e2006-03-23 22:36:19 -080058 /* XXX: What's up with all the flushes? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 local_flush_cache_all();
60 local_flush_tlb_all();
Sam Ravnborgaba20a82011-01-28 22:08:20 +000061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Fix idle thread fields. */
63 __asm__ __volatile__("ld [%0], %%g6\n\t"
64 : : "r" (&current_set[cpuid])
65 : "memory" /* paranoid */);
66
67 /* Attach to the address space of init_task. */
68 atomic_inc(&init_mm.mm_count);
69 current->active_mm = &init_mm;
70
Bob Breuera54123e2006-03-23 22:36:19 -080071 while (!cpu_isset(cpuid, smp_commenced_mask))
72 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 local_irq_enable();
Bob Breuera54123e2006-03-23 22:36:19 -080075
Rusty Russellfe739712009-03-16 14:40:22 +103076 set_cpu_online(cpuid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Cycle through the processors asking the PROM to start each one.
81 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082void __init smp4m_boot_cpus(void)
83{
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 smp_setup_percpu_timer();
85 local_flush_cache_all();
Bob Breuera54123e2006-03-23 22:36:19 -080086}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Bob Breuer92d452f2006-06-20 00:36:10 -070088int __cpuinit smp4m_boot_one_cpu(int i)
Bob Breuera54123e2006-03-23 22:36:19 -080089{
Bob Breuera54123e2006-03-23 22:36:19 -080090 unsigned long *entry = &sun4m_cpu_startup;
91 struct task_struct *p;
92 int timeout;
93 int cpu_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Bob Breuera54123e2006-03-23 22:36:19 -080095 cpu_find_by_mid(i, &cpu_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Bob Breuera54123e2006-03-23 22:36:19 -080097 /* Cook up an idler for this guy. */
98 p = fork_idle(i);
99 current_set[i] = task_thread_info(p);
100 /* See trampoline.S for details... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000101 entry += ((i - 1) * 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Bob Breuera54123e2006-03-23 22:36:19 -0800103 /*
104 * Initialize the contexts table
105 * Since the call to prom_startcpu() trashes the structure,
106 * we need to re-initialize it for each cpu
107 */
108 smp_penguin_ctable.which_io = 0;
109 smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
110 smp_penguin_ctable.reg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Bob Breuera54123e2006-03-23 22:36:19 -0800112 /* whirrr, whirrr, whirrrrrrrrr... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000113 printk(KERN_INFO "Starting CPU %d at %p\n", i, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 local_flush_cache_all();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000115 prom_startcpu(cpu_node, &smp_penguin_ctable, 0, (char *)entry);
Bob Breuera54123e2006-03-23 22:36:19 -0800116
117 /* wheee... it's going... */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000118 for (timeout = 0; timeout < 10000; timeout++) {
119 if (cpu_callin_map[i])
Bob Breuera54123e2006-03-23 22:36:19 -0800120 break;
121 udelay(200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Bob Breuera54123e2006-03-23 22:36:19 -0800124 if (!(cpu_callin_map[i])) {
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000125 printk(KERN_ERR "Processor %d is stuck.\n", i);
Bob Breuera54123e2006-03-23 22:36:19 -0800126 return -ENODEV;
127 }
128
129 local_flush_cache_all();
130 return 0;
131}
132
133void __init smp4m_smp_done(void)
134{
135 int i, first;
136 int *prev;
137
138 /* setup cpu list for irq rotation */
139 first = 0;
140 prev = &first;
Rusty Russellec7c14b2009-03-16 14:40:24 +1030141 for_each_online_cpu(i) {
142 *prev = i;
143 prev = &cpu_data(i).next;
Bob Breuera54123e2006-03-23 22:36:19 -0800144 }
145 *prev = first;
146 local_flush_cache_all();
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Ok, they are spinning and ready to go. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct smp_funcall {
152 smpfunc_t func;
153 unsigned long arg1;
154 unsigned long arg2;
155 unsigned long arg3;
156 unsigned long arg4;
157 unsigned long arg5;
Bob Breuera54123e2006-03-23 22:36:19 -0800158 unsigned long processors_in[SUN4M_NCPUS]; /* Set when ipi entered. */
159 unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160} ccall_info;
161
162static DEFINE_SPINLOCK(cross_call_lock);
163
164/* Cross calls must be serialized, at least currently. */
David S. Miller66e4f8c2008-08-27 20:03:22 -0700165static void smp4m_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700166 unsigned long arg2, unsigned long arg3,
David S. Miller66e4f8c2008-08-27 20:03:22 -0700167 unsigned long arg4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Bob Breuera54123e2006-03-23 22:36:19 -0800169 register int ncpus = SUN4M_NCPUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned long flags;
171
172 spin_lock_irqsave(&cross_call_lock, flags);
173
174 /* Init function glue. */
175 ccall_info.func = func;
176 ccall_info.arg1 = arg1;
177 ccall_info.arg2 = arg2;
178 ccall_info.arg3 = arg3;
179 ccall_info.arg4 = arg4;
David S. Miller66e4f8c2008-08-27 20:03:22 -0700180 ccall_info.arg5 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* Init receive/complete mapping, plus fire the IPI's off. */
183 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 register int i;
185
186 cpu_clear(smp_processor_id(), mask);
David S. Miller66e4f8c2008-08-27 20:03:22 -0700187 cpus_and(mask, cpu_online_map, mask);
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000188 for (i = 0; i < ncpus; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (cpu_isset(i, mask)) {
190 ccall_info.processors_in[i] = 0;
191 ccall_info.processors_out[i] = 0;
192 set_cpu_int(i, IRQ_CROSS_CALL);
193 } else {
194 ccall_info.processors_in[i] = 1;
195 ccall_info.processors_out[i] = 1;
196 }
197 }
198 }
199
200 {
201 register int i;
202
203 i = 0;
204 do {
David S. Miller66e4f8c2008-08-27 20:03:22 -0700205 if (!cpu_isset(i, mask))
206 continue;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000207 while (!ccall_info.processors_in[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 barrier();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000209 } while (++i < ncpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 i = 0;
212 do {
David S. Miller66e4f8c2008-08-27 20:03:22 -0700213 if (!cpu_isset(i, mask))
214 continue;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000215 while (!ccall_info.processors_out[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 barrier();
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000217 } while (++i < ncpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 spin_unlock_irqrestore(&cross_call_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/* Running cross calls. */
223void smp4m_cross_call_irq(void)
224{
225 int i = smp_processor_id();
226
227 ccall_info.processors_in[i] = 1;
228 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
229 ccall_info.arg4, ccall_info.arg5);
230 ccall_info.processors_out[i] = 1;
231}
232
233void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
234{
Al Viro0d844382006-10-08 14:30:44 +0100235 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 int cpu = smp_processor_id();
237
Al Viro0d844382006-10-08 14:30:44 +0100238 old_regs = set_irq_regs(regs);
239
David S. Miller1de937a2008-09-13 22:07:56 -0700240 sun4m_clear_profile_irq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Al Viro0d844382006-10-08 14:30:44 +0100242 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000244 if (!--prof_counter(cpu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int user = user_mode(regs);
246
247 irq_enter();
248 update_process_times(user);
249 irq_exit();
250
251 prof_counter(cpu) = prof_multiplier(cpu);
252 }
Al Viro0d844382006-10-08 14:30:44 +0100253 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Al Viro409832f2008-11-22 17:33:54 +0000256static void __cpuinit smp_setup_percpu_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 int cpu = smp_processor_id();
259
260 prof_counter(cpu) = prof_multiplier(cpu) = 1;
261 load_profile_irq(cpu, lvl14_resolution);
262
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000263 if (cpu == boot_cpu_id)
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000264 sun4m_unmask_profile_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700267static void __init smp4m_blackbox_id(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 int rd = *addr & 0x3e000000;
270 int rs1 = rd >> 11;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000273 addr[1] = 0x8130200c | rd | rs1; /* srl reg, 0xc, reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 addr[2] = 0x80082003 | rd | rs1; /* and reg, 3, reg */
275}
276
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700277static void __init smp4m_blackbox_current(unsigned *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 int rd = *addr & 0x3e000000;
280 int rs1 = rd >> 11;
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 addr[0] = 0x81580000 | rd; /* rd %tbr, reg */
Sam Ravnborgaba20a82011-01-28 22:08:20 +0000283 addr[2] = 0x8130200a | rd | rs1; /* srl reg, 0xa, reg */
David S. Miller4cad6912007-04-30 17:59:50 -0700284 addr[4] = 0x8008200c | rd | rs1; /* and reg, 0xc, reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287void __init sun4m_init_smp(void)
288{
289 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
290 BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
291 BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
293}