1/*
2** SMP Support
3**
4** Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
5** Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
6** Copyright (C) 2001,2004 Grant Grundler <grundler@parisc-linux.org>
7**
8** Lots of stuff stolen from arch/alpha/kernel/smp.c
9** ...and then parisc stole from arch/ia64/kernel/smp.c. Thanks David! :^)
10**
11** Thanks to John Curry and Ullas Ponnadi. I learned a lot from their work.
12** -grant (1/12/2001)
13**
14**	This program is free software; you can redistribute it and/or modify
15**	it under the terms of the GNU General Public License as published by
16**      the Free Software Foundation; either version 2 of the License, or
17**      (at your option) any later version.
18*/
19#include <linux/types.h>
20#include <linux/spinlock.h>
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/sched.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/smp.h>
28#include <linux/kernel_stat.h>
29#include <linux/mm.h>
30#include <linux/err.h>
31#include <linux/delay.h>
32#include <linux/bitops.h>
33#include <linux/ftrace.h>
34#include <linux/cpu.h>
35
36#include <linux/atomic.h>
37#include <asm/current.h>
38#include <asm/delay.h>
39#include <asm/tlbflush.h>
40
41#include <asm/io.h>
42#include <asm/irq.h>		/* for CPU_IRQ_REGION and friends */
43#include <asm/mmu_context.h>
44#include <asm/page.h>
45#include <asm/pgtable.h>
46#include <asm/pgalloc.h>
47#include <asm/processor.h>
48#include <asm/ptrace.h>
49#include <asm/unistd.h>
50#include <asm/cacheflush.h>
51
52#undef DEBUG_SMP
53#ifdef DEBUG_SMP
54static int smp_debug_lvl = 0;
55#define smp_debug(lvl, printargs...)		\
56		if (lvl >= smp_debug_lvl)	\
57			printk(printargs);
58#else
59#define smp_debug(lvl, ...)	do { } while(0)
60#endif /* DEBUG_SMP */
61
62volatile struct task_struct *smp_init_current_idle_task;
63
64/* track which CPU is booting */
65static volatile int cpu_now_booting;
66
67static int parisc_max_cpus = 1;
68
69static DEFINE_PER_CPU(spinlock_t, ipi_lock);
70
71enum ipi_message_type {
72	IPI_NOP=0,
73	IPI_RESCHEDULE=1,
74	IPI_CALL_FUNC,
75	IPI_CPU_START,
76	IPI_CPU_STOP,
77	IPI_CPU_TEST
78};
79
80
81/********** SMP inter processor interrupt and communication routines */
82
83#undef PER_CPU_IRQ_REGION
84#ifdef PER_CPU_IRQ_REGION
85/* XXX REVISIT Ignore for now.
86**    *May* need this "hook" to register IPI handler
87**    once we have perCPU ExtIntr switch tables.
88*/
89static void
90ipi_init(int cpuid)
91{
92#error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
93
94	if(cpu_online(cpuid) )
95	{
96		switch_to_idle_task(current);
97	}
98
99	return;
100}
101#endif
102
103
104/*
105** Yoink this CPU from the runnable list...
106**
107*/
108static void
109halt_processor(void)
110{
111	/* REVISIT : redirect I/O Interrupts to another CPU? */
112	/* REVISIT : does PM *know* this CPU isn't available? */
113	set_cpu_online(smp_processor_id(), false);
114	local_irq_disable();
115	for (;;)
116		;
117}
118
119
120irqreturn_t __irq_entry
121ipi_interrupt(int irq, void *dev_id)
122{
123	int this_cpu = smp_processor_id();
124	struct cpuinfo_parisc *p = &per_cpu(cpu_data, this_cpu);
125	unsigned long ops;
126	unsigned long flags;
127
128	for (;;) {
129		spinlock_t *lock = &per_cpu(ipi_lock, this_cpu);
130		spin_lock_irqsave(lock, flags);
131		ops = p->pending_ipi;
132		p->pending_ipi = 0;
133		spin_unlock_irqrestore(lock, flags);
134
135		mb(); /* Order bit clearing and data access. */
136
137		if (!ops)
138		    break;
139
140		while (ops) {
141			unsigned long which = ffz(~ops);
142
143			ops &= ~(1 << which);
144
145			switch (which) {
146			case IPI_NOP:
147				smp_debug(100, KERN_DEBUG "CPU%d IPI_NOP\n", this_cpu);
148				break;
149
150			case IPI_RESCHEDULE:
151				smp_debug(100, KERN_DEBUG "CPU%d IPI_RESCHEDULE\n", this_cpu);
152				inc_irq_stat(irq_resched_count);
153				scheduler_ipi();
154				break;
155
156			case IPI_CALL_FUNC:
157				smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC\n", this_cpu);
158				generic_smp_call_function_interrupt();
159				break;
160
161			case IPI_CPU_START:
162				smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_START\n", this_cpu);
163				break;
164
165			case IPI_CPU_STOP:
166				smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_STOP\n", this_cpu);
167				halt_processor();
168				break;
169
170			case IPI_CPU_TEST:
171				smp_debug(100, KERN_DEBUG "CPU%d is alive!\n", this_cpu);
172				break;
173
174			default:
175				printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
176					this_cpu, which);
177				return IRQ_NONE;
178			} /* Switch */
179		/* let in any pending interrupts */
180		local_irq_enable();
181		local_irq_disable();
182		} /* while (ops) */
183	}
184	return IRQ_HANDLED;
185}
186
187
188static inline void
189ipi_send(int cpu, enum ipi_message_type op)
190{
191	struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpu);
192	spinlock_t *lock = &per_cpu(ipi_lock, cpu);
193	unsigned long flags;
194
195	spin_lock_irqsave(lock, flags);
196	p->pending_ipi |= 1 << op;
197	gsc_writel(IPI_IRQ - CPU_IRQ_BASE, p->hpa);
198	spin_unlock_irqrestore(lock, flags);
199}
200
201static void
202send_IPI_mask(const struct cpumask *mask, enum ipi_message_type op)
203{
204	int cpu;
205
206	for_each_cpu(cpu, mask)
207		ipi_send(cpu, op);
208}
209
210static inline void
211send_IPI_single(int dest_cpu, enum ipi_message_type op)
212{
213	BUG_ON(dest_cpu == NO_PROC_ID);
214
215	ipi_send(dest_cpu, op);
216}
217
218static inline void
219send_IPI_allbutself(enum ipi_message_type op)
220{
221	int i;
222
223	for_each_online_cpu(i) {
224		if (i != smp_processor_id())
225			send_IPI_single(i, op);
226	}
227}
228
229
230inline void
231smp_send_stop(void)	{ send_IPI_allbutself(IPI_CPU_STOP); }
232
233static inline void
234smp_send_start(void)	{ send_IPI_allbutself(IPI_CPU_START); }
235
236void
237smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
238
239void
240smp_send_all_nop(void)
241{
242	send_IPI_allbutself(IPI_NOP);
243}
244
245void arch_send_call_function_ipi_mask(const struct cpumask *mask)
246{
247	send_IPI_mask(mask, IPI_CALL_FUNC);
248}
249
250void arch_send_call_function_single_ipi(int cpu)
251{
252	send_IPI_single(cpu, IPI_CALL_FUNC);
253}
254
255/*
256 * Called by secondaries to update state and initialize CPU registers.
257 */
258static void __init
259smp_cpu_init(int cpunum)
260{
261	extern int init_per_cpu(int);  /* arch/parisc/kernel/processor.c */
262	extern void init_IRQ(void);    /* arch/parisc/kernel/irq.c */
263	extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
264
265	/* Set modes and Enable floating point coprocessor */
266	(void) init_per_cpu(cpunum);
267
268	disable_sr_hashing();
269
270	mb();
271
272	/* Well, support 2.4 linux scheme as well. */
273	if (cpu_online(cpunum))	{
274		extern void machine_halt(void); /* arch/parisc.../process.c */
275
276		printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
277		machine_halt();
278	}
279
280	notify_cpu_starting(cpunum);
281
282	set_cpu_online(cpunum, true);
283
284	/* Initialise the idle task for this CPU */
285	atomic_inc(&init_mm.mm_count);
286	current->active_mm = &init_mm;
287	BUG_ON(current->mm);
288	enter_lazy_tlb(&init_mm, current);
289
290	init_IRQ();   /* make sure no IRQs are enabled or pending */
291	start_cpu_itimer();
292}
293
294
295/*
296 * Slaves start using C here. Indirectly called from smp_slave_stext.
297 * Do what start_kernel() and main() do for boot strap processor (aka monarch)
298 */
299void __init smp_callin(void)
300{
301	int slave_id = cpu_now_booting;
302
303	smp_cpu_init(slave_id);
304	preempt_disable();
305
306	flush_cache_all_local(); /* start with known state */
307	flush_tlb_all_local(NULL);
308
309	local_irq_enable();  /* Interrupts have been off until now */
310
311	cpu_startup_entry(CPUHP_ONLINE);
312
313	/* NOTREACHED */
314	panic("smp_callin() AAAAaaaaahhhh....\n");
315}
316
317/*
318 * Bring one cpu online.
319 */
320int smp_boot_one_cpu(int cpuid, struct task_struct *idle)
321{
322	const struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpuid);
323	long timeout;
324
325	task_thread_info(idle)->cpu = cpuid;
326
327	/* Let _start know what logical CPU we're booting
328	** (offset into init_tasks[],cpu_data[])
329	*/
330	cpu_now_booting = cpuid;
331
332	/*
333	** boot strap code needs to know the task address since
334	** it also contains the process stack.
335	*/
336	smp_init_current_idle_task = idle ;
337	mb();
338
339	printk(KERN_INFO "Releasing cpu %d now, hpa=%lx\n", cpuid, p->hpa);
340
341	/*
342	** This gets PDC to release the CPU from a very tight loop.
343	**
344	** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
345	** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
346	** is executed after receiving the rendezvous signal (an interrupt to
347	** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
348	** contents of memory are valid."
349	*/
350	gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, p->hpa);
351	mb();
352
353	/*
354	 * OK, wait a bit for that CPU to finish staggering about.
355	 * Slave will set a bit when it reaches smp_cpu_init().
356	 * Once the "monarch CPU" sees the bit change, it can move on.
357	 */
358	for (timeout = 0; timeout < 10000; timeout++) {
359		if(cpu_online(cpuid)) {
360			/* Which implies Slave has started up */
361			cpu_now_booting = 0;
362			smp_init_current_idle_task = NULL;
363			goto alive ;
364		}
365		udelay(100);
366		barrier();
367	}
368	printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
369	return -1;
370
371alive:
372	/* Remember the Slave data */
373	smp_debug(100, KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
374		cpuid, timeout * 100);
375	return 0;
376}
377
378void __init smp_prepare_boot_cpu(void)
379{
380	int bootstrap_processor = per_cpu(cpu_data, 0).cpuid;
381
382	/* Setup BSP mappings */
383	printk(KERN_INFO "SMP: bootstrap CPU ID is %d\n", bootstrap_processor);
384
385	set_cpu_online(bootstrap_processor, true);
386	set_cpu_present(bootstrap_processor, true);
387}
388
389
390
391/*
392** inventory.c:do_inventory() hasn't yet been run and thus we
393** don't 'discover' the additional CPUs until later.
394*/
395void __init smp_prepare_cpus(unsigned int max_cpus)
396{
397	int cpu;
398
399	for_each_possible_cpu(cpu)
400		spin_lock_init(&per_cpu(ipi_lock, cpu));
401
402	init_cpu_present(cpumask_of(0));
403
404	parisc_max_cpus = max_cpus;
405	if (!max_cpus)
406		printk(KERN_INFO "SMP mode deactivated.\n");
407}
408
409
410void smp_cpus_done(unsigned int cpu_max)
411{
412	return;
413}
414
415
416int __cpu_up(unsigned int cpu, struct task_struct *tidle)
417{
418	if (cpu != 0 && cpu < parisc_max_cpus)
419		smp_boot_one_cpu(cpu, tidle);
420
421	return cpu_online(cpu) ? 0 : -ENOSYS;
422}
423
424#ifdef CONFIG_PROC_FS
425int __init
426setup_profiling_timer(unsigned int multiplier)
427{
428	return -EINVAL;
429}
430#endif
431