• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/arch/sparc/kernel/
1/* 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>
12#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>
19#include <linux/delay.h>
20#include <linux/cpu.h>
21
22#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
24#include <asm/irq_regs.h>
25
26#include <asm/ptrace.h>
27#include <asm/atomic.h>
28
29#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
36#include "irq.h"
37
38#define IRQ_CROSS_CALL		15
39
40extern ctxd_t *srmmu_ctx_table_phys;
41
42extern volatile unsigned long cpu_callin_map[NR_CPUS];
43extern unsigned char boot_cpu_id;
44
45extern cpumask_t smp_commenced_mask;
46
47extern 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
57static inline unsigned long
58swap_ulong(volatile unsigned long *ptr, unsigned long val)
59{
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
69void __cpuinit smp4m_callin(void)
70{
71	int cpuid = hard_smp_processor_id();
72
73	local_flush_cache_all();
74	local_flush_tlb_all();
75
76	notify_cpu_starting(cpuid);
77
78	/* 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. */
94	swap_ulong(&cpu_callin_map[cpuid], 1);
95
96	local_flush_cache_all();
97	local_flush_tlb_all();
98
99	cpu_probe();
100
101	/* Fix idle thread fields. */
102	__asm__ __volatile__("ld [%0], %%g6\n\t"
103			     : : "r" (&current_set[cpuid])
104			     : "memory" /* paranoid */);
105
106	/* Attach to the address space of init_task. */
107	atomic_inc(&init_mm.mm_count);
108	current->active_mm = &init_mm;
109
110	while (!cpu_isset(cpuid, smp_commenced_mask))
111		mb();
112
113	local_irq_enable();
114
115	set_cpu_online(cpuid, true);
116}
117
118/*
119 *	Cycle through the processors asking the PROM to start each one.
120 */
121
122extern struct linux_prom_registers smp_penguin_ctable;
123
124void __init smp4m_boot_cpus(void)
125{
126	smp_setup_percpu_timer();
127	local_flush_cache_all();
128}
129
130int __cpuinit smp4m_boot_one_cpu(int i)
131{
132	extern unsigned long sun4m_cpu_startup;
133	unsigned long *entry = &sun4m_cpu_startup;
134	struct task_struct *p;
135	int timeout;
136	int cpu_node;
137
138	cpu_find_by_mid(i, &cpu_node);
139
140	/* Cook up an idler for this guy. */
141	p = fork_idle(i);
142	current_set[i] = task_thread_info(p);
143	/* See trampoline.S for details... */
144	entry += ((i-1) * 3);
145
146	/*
147	 * Initialize the contexts table
148	 * Since the call to prom_startcpu() trashes the structure,
149	 * we need to re-initialize it for each cpu
150	 */
151	smp_penguin_ctable.which_io = 0;
152	smp_penguin_ctable.phys_addr = (unsigned int) srmmu_ctx_table_phys;
153	smp_penguin_ctable.reg_size = 0;
154
155	/* whirrr, whirrr, whirrrrrrrrr... */
156	printk("Starting CPU %d at %p\n", i, entry);
157	local_flush_cache_all();
158	prom_startcpu(cpu_node,
159		      &smp_penguin_ctable, 0, (char *)entry);
160
161	/* wheee... it's going... */
162	for(timeout = 0; timeout < 10000; timeout++) {
163		if(cpu_callin_map[i])
164			break;
165		udelay(200);
166	}
167
168	if (!(cpu_callin_map[i])) {
169		printk("Processor %d is stuck.\n", i);
170		return -ENODEV;
171	}
172
173	local_flush_cache_all();
174	return 0;
175}
176
177void __init smp4m_smp_done(void)
178{
179	int i, first;
180	int *prev;
181
182	/* setup cpu list for irq rotation */
183	first = 0;
184	prev = &first;
185	for_each_online_cpu(i) {
186		*prev = i;
187		prev = &cpu_data(i).next;
188	}
189	*prev = first;
190	local_flush_cache_all();
191
192	/* Ok, they are spinning and ready to go. */
193}
194
195void smp4m_irq_rotate(int cpu)
196{
197	int next = cpu_data(cpu).next;
198	if (next != cpu)
199		set_irq_udt(next);
200}
201
202static struct smp_funcall {
203	smpfunc_t func;
204	unsigned long arg1;
205	unsigned long arg2;
206	unsigned long arg3;
207	unsigned long arg4;
208	unsigned long arg5;
209	unsigned long processors_in[SUN4M_NCPUS];  /* Set when ipi entered. */
210	unsigned long processors_out[SUN4M_NCPUS]; /* Set when ipi exited. */
211} ccall_info;
212
213static DEFINE_SPINLOCK(cross_call_lock);
214
215/* Cross calls must be serialized, at least currently. */
216static void smp4m_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1,
217			     unsigned long arg2, unsigned long arg3,
218			     unsigned long arg4)
219{
220		register int ncpus = SUN4M_NCPUS;
221		unsigned long flags;
222
223		spin_lock_irqsave(&cross_call_lock, flags);
224
225		/* Init function glue. */
226		ccall_info.func = func;
227		ccall_info.arg1 = arg1;
228		ccall_info.arg2 = arg2;
229		ccall_info.arg3 = arg3;
230		ccall_info.arg4 = arg4;
231		ccall_info.arg5 = 0;
232
233		/* Init receive/complete mapping, plus fire the IPI's off. */
234		{
235			register int i;
236
237			cpu_clear(smp_processor_id(), mask);
238			cpus_and(mask, cpu_online_map, mask);
239			for(i = 0; i < ncpus; i++) {
240				if (cpu_isset(i, mask)) {
241					ccall_info.processors_in[i] = 0;
242					ccall_info.processors_out[i] = 0;
243					set_cpu_int(i, IRQ_CROSS_CALL);
244				} else {
245					ccall_info.processors_in[i] = 1;
246					ccall_info.processors_out[i] = 1;
247				}
248			}
249		}
250
251		{
252			register int i;
253
254			i = 0;
255			do {
256				if (!cpu_isset(i, mask))
257					continue;
258				while(!ccall_info.processors_in[i])
259					barrier();
260			} while(++i < ncpus);
261
262			i = 0;
263			do {
264				if (!cpu_isset(i, mask))
265					continue;
266				while(!ccall_info.processors_out[i])
267					barrier();
268			} while(++i < ncpus);
269		}
270
271		spin_unlock_irqrestore(&cross_call_lock, flags);
272}
273
274/* Running cross calls. */
275void smp4m_cross_call_irq(void)
276{
277	int i = smp_processor_id();
278
279	ccall_info.processors_in[i] = 1;
280	ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3,
281			ccall_info.arg4, ccall_info.arg5);
282	ccall_info.processors_out[i] = 1;
283}
284
285extern void sun4m_clear_profile_irq(int cpu);
286
287void smp4m_percpu_timer_interrupt(struct pt_regs *regs)
288{
289	struct pt_regs *old_regs;
290	int cpu = smp_processor_id();
291
292	old_regs = set_irq_regs(regs);
293
294	sun4m_clear_profile_irq(cpu);
295
296	profile_tick(CPU_PROFILING);
297
298	if(!--prof_counter(cpu)) {
299		int user = user_mode(regs);
300
301		irq_enter();
302		update_process_times(user);
303		irq_exit();
304
305		prof_counter(cpu) = prof_multiplier(cpu);
306	}
307	set_irq_regs(old_regs);
308}
309
310extern unsigned int lvl14_resolution;
311
312static void __cpuinit smp_setup_percpu_timer(void)
313{
314	int cpu = smp_processor_id();
315
316	prof_counter(cpu) = prof_multiplier(cpu) = 1;
317	load_profile_irq(cpu, lvl14_resolution);
318
319	if(cpu == boot_cpu_id)
320		enable_pil_irq(14);
321}
322
323static void __init smp4m_blackbox_id(unsigned *addr)
324{
325	int rd = *addr & 0x3e000000;
326	int rs1 = rd >> 11;
327
328	addr[0] = 0x81580000 | rd;		/* rd %tbr, reg */
329	addr[1] = 0x8130200c | rd | rs1;    	/* srl reg, 0xc, reg */
330	addr[2] = 0x80082003 | rd | rs1;	/* and reg, 3, reg */
331}
332
333static void __init smp4m_blackbox_current(unsigned *addr)
334{
335	int rd = *addr & 0x3e000000;
336	int rs1 = rd >> 11;
337
338	addr[0] = 0x81580000 | rd;		/* rd %tbr, reg */
339	addr[2] = 0x8130200a | rd | rs1;    	/* srl reg, 0xa, reg */
340	addr[4] = 0x8008200c | rd | rs1;	/* and reg, 0xc, reg */
341}
342
343void __init sun4m_init_smp(void)
344{
345	BTFIXUPSET_BLACKBOX(hard_smp_processor_id, smp4m_blackbox_id);
346	BTFIXUPSET_BLACKBOX(load_current, smp4m_blackbox_current);
347	BTFIXUPSET_CALL(smp_cross_call, smp4m_cross_call, BTFIXUPCALL_NORM);
348	BTFIXUPSET_CALL(__hard_smp_processor_id, __smp4m_processor_id, BTFIXUPCALL_NORM);
349}
350