xref: /openbmc/linux/arch/powerpc/kernel/irq.c (revision 4fc4dca8)
1 /*
2  *  Derived from arch/i386/kernel/irq.c
3  *    Copyright (C) 1992 Linus Torvalds
4  *  Adapted from arch/i386 by Gary Thomas
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
7  *    Copyright (C) 1996-2001 Cort Dougan
8  *  Adapted for Power Macintosh by Paul Mackerras
9  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version
14  * 2 of the License, or (at your option) any later version.
15  *
16  * This file contains the code used by various IRQ handling routines:
17  * asking for different IRQ's should be done through these routines
18  * instead of just grabbing them. Thus setups with different IRQ numbers
19  * shouldn't result in any weird surprises, and installing new handlers
20  * should be easier.
21  *
22  * The MPC8xx has an interrupt mask in the SIU.  If a bit is set, the
23  * interrupt is _enabled_.  As expected, IRQ0 is bit 0 in the 32-bit
24  * mask register (of which only 16 are defined), hence the weird shifting
25  * and complement of the cached_irq_mask.  I want to be able to stuff
26  * this right into the SIU SMASK register.
27  * Many of the prep/chrp functions are conditional compiled on CONFIG_PPC_8xx
28  * to reduce code space and undefined function references.
29  */
30 
31 #undef DEBUG
32 
33 #include <linux/export.h>
34 #include <linux/threads.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/signal.h>
37 #include <linux/sched.h>
38 #include <linux/ptrace.h>
39 #include <linux/ioport.h>
40 #include <linux/interrupt.h>
41 #include <linux/timex.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/delay.h>
45 #include <linux/irq.h>
46 #include <linux/seq_file.h>
47 #include <linux/cpumask.h>
48 #include <linux/profile.h>
49 #include <linux/bitops.h>
50 #include <linux/list.h>
51 #include <linux/radix-tree.h>
52 #include <linux/mutex.h>
53 #include <linux/pci.h>
54 #include <linux/debugfs.h>
55 #include <linux/of.h>
56 #include <linux/of_irq.h>
57 
58 #include <linux/uaccess.h>
59 #include <asm/io.h>
60 #include <asm/pgtable.h>
61 #include <asm/irq.h>
62 #include <asm/cache.h>
63 #include <asm/prom.h>
64 #include <asm/ptrace.h>
65 #include <asm/machdep.h>
66 #include <asm/udbg.h>
67 #include <asm/smp.h>
68 #include <asm/livepatch.h>
69 #include <asm/asm-prototypes.h>
70 #include <asm/hw_irq.h>
71 
72 #ifdef CONFIG_PPC64
73 #include <asm/paca.h>
74 #include <asm/firmware.h>
75 #include <asm/lv1call.h>
76 #endif
77 #define CREATE_TRACE_POINTS
78 #include <asm/trace.h>
79 #include <asm/cpu_has_feature.h>
80 
81 DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
82 EXPORT_PER_CPU_SYMBOL(irq_stat);
83 
84 #ifdef CONFIG_PPC32
85 atomic_t ppc_n_lost_interrupts;
86 
87 #ifdef CONFIG_TAU_INT
88 extern int tau_initialized;
89 u32 tau_interrupts(unsigned long cpu);
90 #endif
91 #endif /* CONFIG_PPC32 */
92 
93 #ifdef CONFIG_PPC64
94 
95 int distribute_irqs = 1;
96 
97 static inline notrace unsigned long get_irq_happened(void)
98 {
99 	unsigned long happened;
100 
101 	__asm__ __volatile__("lbz %0,%1(13)"
102 	: "=r" (happened) : "i" (offsetof(struct paca_struct, irq_happened)));
103 
104 	return happened;
105 }
106 
107 static inline notrace int decrementer_check_overflow(void)
108 {
109  	u64 now = get_tb_or_rtc();
110 	u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
111 
112 	return now >= *next_tb;
113 }
114 
115 /* This is called whenever we are re-enabling interrupts
116  * and returns either 0 (nothing to do) or 500/900/280/a00/e80 if
117  * there's an EE, DEC or DBELL to generate.
118  *
119  * This is called in two contexts: From arch_local_irq_restore()
120  * before soft-enabling interrupts, and from the exception exit
121  * path when returning from an interrupt from a soft-disabled to
122  * a soft enabled context. In both case we have interrupts hard
123  * disabled.
124  *
125  * We take care of only clearing the bits we handled in the
126  * PACA irq_happened field since we can only re-emit one at a
127  * time and we don't want to "lose" one.
128  */
129 notrace unsigned int __check_irq_replay(void)
130 {
131 	/*
132 	 * We use local_paca rather than get_paca() to avoid all
133 	 * the debug_smp_processor_id() business in this low level
134 	 * function
135 	 */
136 	unsigned char happened = local_paca->irq_happened;
137 
138 	/*
139 	 * We are responding to the next interrupt, so interrupt-off
140 	 * latencies should be reset here.
141 	 */
142 	trace_hardirqs_on();
143 	trace_hardirqs_off();
144 
145 	/*
146 	 * We are always hard disabled here, but PACA_IRQ_HARD_DIS may
147 	 * not be set, which means interrupts have only just been hard
148 	 * disabled as part of the local_irq_restore or interrupt return
149 	 * code. In that case, skip the decrementr check becaus it's
150 	 * expensive to read the TB.
151 	 *
152 	 * HARD_DIS then gets cleared here, but it's reconciled later.
153 	 * Either local_irq_disable will replay the interrupt and that
154 	 * will reconcile state like other hard interrupts. Or interrupt
155 	 * retur will replay the interrupt and in that case it sets
156 	 * PACA_IRQ_HARD_DIS by hand (see comments in entry_64.S).
157 	 */
158 	if (happened & PACA_IRQ_HARD_DIS) {
159 		local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
160 
161 		/*
162 		 * We may have missed a decrementer interrupt if hard disabled.
163 		 * Check the decrementer register in case we had a rollover
164 		 * while hard disabled.
165 		 */
166 		if (!(happened & PACA_IRQ_DEC)) {
167 			if (decrementer_check_overflow()) {
168 				local_paca->irq_happened |= PACA_IRQ_DEC;
169 				happened |= PACA_IRQ_DEC;
170 			}
171 		}
172 	}
173 
174 	/*
175 	 * Force the delivery of pending soft-disabled interrupts on PS3.
176 	 * Any HV call will have this side effect.
177 	 */
178 	if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
179 		u64 tmp, tmp2;
180 		lv1_get_version_info(&tmp, &tmp2);
181 	}
182 
183 	/*
184 	 * Check if an hypervisor Maintenance interrupt happened.
185 	 * This is a higher priority interrupt than the others, so
186 	 * replay it first.
187 	 */
188 	if (happened & PACA_IRQ_HMI) {
189 		local_paca->irq_happened &= ~PACA_IRQ_HMI;
190 		return 0xe60;
191 	}
192 
193 	if (happened & PACA_IRQ_DEC) {
194 		local_paca->irq_happened &= ~PACA_IRQ_DEC;
195 		return 0x900;
196 	}
197 
198 	if (happened & PACA_IRQ_PMI) {
199 		local_paca->irq_happened &= ~PACA_IRQ_PMI;
200 		return 0xf00;
201 	}
202 
203 	if (happened & PACA_IRQ_EE) {
204 		local_paca->irq_happened &= ~PACA_IRQ_EE;
205 		return 0x500;
206 	}
207 
208 #ifdef CONFIG_PPC_BOOK3E
209 	/*
210 	 * Check if an EPR external interrupt happened this bit is typically
211 	 * set if we need to handle another "edge" interrupt from within the
212 	 * MPIC "EPR" handler.
213 	 */
214 	if (happened & PACA_IRQ_EE_EDGE) {
215 		local_paca->irq_happened &= ~PACA_IRQ_EE_EDGE;
216 		return 0x500;
217 	}
218 
219 	if (happened & PACA_IRQ_DBELL) {
220 		local_paca->irq_happened &= ~PACA_IRQ_DBELL;
221 		return 0x280;
222 	}
223 #else
224 	if (happened & PACA_IRQ_DBELL) {
225 		local_paca->irq_happened &= ~PACA_IRQ_DBELL;
226 		return 0xa00;
227 	}
228 #endif /* CONFIG_PPC_BOOK3E */
229 
230 	/* There should be nothing left ! */
231 	BUG_ON(local_paca->irq_happened != 0);
232 
233 	return 0;
234 }
235 
236 notrace void arch_local_irq_restore(unsigned long mask)
237 {
238 	unsigned char irq_happened;
239 	unsigned int replay;
240 
241 	/* Write the new soft-enabled value */
242 	irq_soft_mask_set(mask);
243 	if (mask)
244 		return;
245 
246 	/*
247 	 * From this point onward, we can take interrupts, preempt,
248 	 * etc... unless we got hard-disabled. We check if an event
249 	 * happened. If none happened, we know we can just return.
250 	 *
251 	 * We may have preempted before the check below, in which case
252 	 * we are checking the "new" CPU instead of the old one. This
253 	 * is only a problem if an event happened on the "old" CPU.
254 	 *
255 	 * External interrupt events will have caused interrupts to
256 	 * be hard-disabled, so there is no problem, we
257 	 * cannot have preempted.
258 	 */
259 	irq_happened = get_irq_happened();
260 	if (!irq_happened) {
261 #ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
262 		WARN_ON(!(mfmsr() & MSR_EE));
263 #endif
264 		return;
265 	}
266 
267 	/*
268 	 * We need to hard disable to get a trusted value from
269 	 * __check_irq_replay(). We also need to soft-disable
270 	 * again to avoid warnings in there due to the use of
271 	 * per-cpu variables.
272 	 */
273 	if (!(irq_happened & PACA_IRQ_HARD_DIS)) {
274 #ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
275 		WARN_ON(!(mfmsr() & MSR_EE));
276 #endif
277 		__hard_irq_disable();
278 #ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
279 	} else {
280 		/*
281 		 * We should already be hard disabled here. We had bugs
282 		 * where that wasn't the case so let's dbl check it and
283 		 * warn if we are wrong. Only do that when IRQ tracing
284 		 * is enabled as mfmsr() can be costly.
285 		 */
286 		if (WARN_ON(mfmsr() & MSR_EE))
287 			__hard_irq_disable();
288 #endif
289 	}
290 
291 	irq_soft_mask_set(IRQS_ALL_DISABLED);
292 	trace_hardirqs_off();
293 
294 	/*
295 	 * Check if anything needs to be re-emitted. We haven't
296 	 * soft-enabled yet to avoid warnings in decrementer_check_overflow
297 	 * accessing per-cpu variables
298 	 */
299 	replay = __check_irq_replay();
300 
301 	/* We can soft-enable now */
302 	trace_hardirqs_on();
303 	irq_soft_mask_set(IRQS_ENABLED);
304 
305 	/*
306 	 * And replay if we have to. This will return with interrupts
307 	 * hard-enabled.
308 	 */
309 	if (replay) {
310 		__replay_interrupt(replay);
311 		return;
312 	}
313 
314 	/* Finally, let's ensure we are hard enabled */
315 	__hard_irq_enable();
316 }
317 EXPORT_SYMBOL(arch_local_irq_restore);
318 
319 /*
320  * This is specifically called by assembly code to re-enable interrupts
321  * if they are currently disabled. This is typically called before
322  * schedule() or do_signal() when returning to userspace. We do it
323  * in C to avoid the burden of dealing with lockdep etc...
324  *
325  * NOTE: This is called with interrupts hard disabled but not marked
326  * as such in paca->irq_happened, so we need to resync this.
327  */
328 void notrace restore_interrupts(void)
329 {
330 	if (irqs_disabled()) {
331 		local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
332 		local_irq_enable();
333 	} else
334 		__hard_irq_enable();
335 }
336 
337 /*
338  * This is a helper to use when about to go into idle low-power
339  * when the latter has the side effect of re-enabling interrupts
340  * (such as calling H_CEDE under pHyp).
341  *
342  * You call this function with interrupts soft-disabled (this is
343  * already the case when ppc_md.power_save is called). The function
344  * will return whether to enter power save or just return.
345  *
346  * In the former case, it will have notified lockdep of interrupts
347  * being re-enabled and generally sanitized the lazy irq state,
348  * and in the latter case it will leave with interrupts hard
349  * disabled and marked as such, so the local_irq_enable() call
350  * in arch_cpu_idle() will properly re-enable everything.
351  */
352 bool prep_irq_for_idle(void)
353 {
354 	/*
355 	 * First we need to hard disable to ensure no interrupt
356 	 * occurs before we effectively enter the low power state
357 	 */
358 	__hard_irq_disable();
359 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
360 
361 	/*
362 	 * If anything happened while we were soft-disabled,
363 	 * we return now and do not enter the low power state.
364 	 */
365 	if (lazy_irq_pending())
366 		return false;
367 
368 	/* Tell lockdep we are about to re-enable */
369 	trace_hardirqs_on();
370 
371 	/*
372 	 * Mark interrupts as soft-enabled and clear the
373 	 * PACA_IRQ_HARD_DIS from the pending mask since we
374 	 * are about to hard enable as well as a side effect
375 	 * of entering the low power state.
376 	 */
377 	local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
378 	irq_soft_mask_set(IRQS_ENABLED);
379 
380 	/* Tell the caller to enter the low power state */
381 	return true;
382 }
383 
384 #ifdef CONFIG_PPC_BOOK3S
385 /*
386  * This is for idle sequences that return with IRQs off, but the
387  * idle state itself wakes on interrupt. Tell the irq tracer that
388  * IRQs are enabled for the duration of idle so it does not get long
389  * off times. Must be paired with fini_irq_for_idle_irqsoff.
390  */
391 bool prep_irq_for_idle_irqsoff(void)
392 {
393 	WARN_ON(!irqs_disabled());
394 
395 	/*
396 	 * First we need to hard disable to ensure no interrupt
397 	 * occurs before we effectively enter the low power state
398 	 */
399 	__hard_irq_disable();
400 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
401 
402 	/*
403 	 * If anything happened while we were soft-disabled,
404 	 * we return now and do not enter the low power state.
405 	 */
406 	if (lazy_irq_pending())
407 		return false;
408 
409 	/* Tell lockdep we are about to re-enable */
410 	trace_hardirqs_on();
411 
412 	return true;
413 }
414 
415 /*
416  * Take the SRR1 wakeup reason, index into this table to find the
417  * appropriate irq_happened bit.
418  *
419  * Sytem reset exceptions taken in idle state also come through here,
420  * but they are NMI interrupts so do not need to wait for IRQs to be
421  * restored, and should be taken as early as practical. These are marked
422  * with 0xff in the table. The Power ISA specifies 0100b as the system
423  * reset interrupt reason.
424  */
425 #define IRQ_SYSTEM_RESET	0xff
426 
427 static const u8 srr1_to_lazyirq[0x10] = {
428 	0, 0, 0,
429 	PACA_IRQ_DBELL,
430 	IRQ_SYSTEM_RESET,
431 	PACA_IRQ_DBELL,
432 	PACA_IRQ_DEC,
433 	0,
434 	PACA_IRQ_EE,
435 	PACA_IRQ_EE,
436 	PACA_IRQ_HMI,
437 	0, 0, 0, 0, 0 };
438 
439 void replay_system_reset(void)
440 {
441 	struct pt_regs regs;
442 
443 	ppc_save_regs(&regs);
444 	regs.trap = 0x100;
445 	get_paca()->in_nmi = 1;
446 	system_reset_exception(&regs);
447 	get_paca()->in_nmi = 0;
448 }
449 EXPORT_SYMBOL_GPL(replay_system_reset);
450 
451 void irq_set_pending_from_srr1(unsigned long srr1)
452 {
453 	unsigned int idx = (srr1 & SRR1_WAKEMASK_P8) >> 18;
454 	u8 reason = srr1_to_lazyirq[idx];
455 
456 	/*
457 	 * Take the system reset now, which is immediately after registers
458 	 * are restored from idle. It's an NMI, so interrupts need not be
459 	 * re-enabled before it is taken.
460 	 */
461 	if (unlikely(reason == IRQ_SYSTEM_RESET)) {
462 		replay_system_reset();
463 		return;
464 	}
465 
466 	/*
467 	 * The 0 index (SRR1[42:45]=b0000) must always evaluate to 0,
468 	 * so this can be called unconditionally with the SRR1 wake
469 	 * reason as returned by the idle code, which uses 0 to mean no
470 	 * interrupt.
471 	 *
472 	 * If a future CPU was to designate this as an interrupt reason,
473 	 * then a new index for no interrupt must be assigned.
474 	 */
475 	local_paca->irq_happened |= reason;
476 }
477 #endif /* CONFIG_PPC_BOOK3S */
478 
479 /*
480  * Force a replay of the external interrupt handler on this CPU.
481  */
482 void force_external_irq_replay(void)
483 {
484 	/*
485 	 * This must only be called with interrupts soft-disabled,
486 	 * the replay will happen when re-enabling.
487 	 */
488 	WARN_ON(!arch_irqs_disabled());
489 
490 	/*
491 	 * Interrupts must always be hard disabled before irq_happened is
492 	 * modified (to prevent lost update in case of interrupt between
493 	 * load and store).
494 	 */
495 	__hard_irq_disable();
496 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
497 
498 	/* Indicate in the PACA that we have an interrupt to replay */
499 	local_paca->irq_happened |= PACA_IRQ_EE;
500 }
501 
502 #endif /* CONFIG_PPC64 */
503 
504 int arch_show_interrupts(struct seq_file *p, int prec)
505 {
506 	int j;
507 
508 #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
509 	if (tau_initialized) {
510 		seq_printf(p, "%*s: ", prec, "TAU");
511 		for_each_online_cpu(j)
512 			seq_printf(p, "%10u ", tau_interrupts(j));
513 		seq_puts(p, "  PowerPC             Thermal Assist (cpu temp)\n");
514 	}
515 #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
516 
517 	seq_printf(p, "%*s: ", prec, "LOC");
518 	for_each_online_cpu(j)
519 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_event);
520         seq_printf(p, "  Local timer interrupts for timer event device\n");
521 
522 	seq_printf(p, "%*s: ", prec, "BCT");
523 	for_each_online_cpu(j)
524 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).broadcast_irqs_event);
525 	seq_printf(p, "  Broadcast timer interrupts for timer event device\n");
526 
527 	seq_printf(p, "%*s: ", prec, "LOC");
528 	for_each_online_cpu(j)
529 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_others);
530         seq_printf(p, "  Local timer interrupts for others\n");
531 
532 	seq_printf(p, "%*s: ", prec, "SPU");
533 	for_each_online_cpu(j)
534 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
535 	seq_printf(p, "  Spurious interrupts\n");
536 
537 	seq_printf(p, "%*s: ", prec, "PMI");
538 	for_each_online_cpu(j)
539 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
540 	seq_printf(p, "  Performance monitoring interrupts\n");
541 
542 	seq_printf(p, "%*s: ", prec, "MCE");
543 	for_each_online_cpu(j)
544 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
545 	seq_printf(p, "  Machine check exceptions\n");
546 
547 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
548 		seq_printf(p, "%*s: ", prec, "HMI");
549 		for_each_online_cpu(j)
550 			seq_printf(p, "%10u ",
551 					per_cpu(irq_stat, j).hmi_exceptions);
552 		seq_printf(p, "  Hypervisor Maintenance Interrupts\n");
553 	}
554 
555 	seq_printf(p, "%*s: ", prec, "NMI");
556 	for_each_online_cpu(j)
557 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).sreset_irqs);
558 	seq_printf(p, "  System Reset interrupts\n");
559 
560 #ifdef CONFIG_PPC_WATCHDOG
561 	seq_printf(p, "%*s: ", prec, "WDG");
562 	for_each_online_cpu(j)
563 		seq_printf(p, "%10u ", per_cpu(irq_stat, j).soft_nmi_irqs);
564 	seq_printf(p, "  Watchdog soft-NMI interrupts\n");
565 #endif
566 
567 #ifdef CONFIG_PPC_DOORBELL
568 	if (cpu_has_feature(CPU_FTR_DBELL)) {
569 		seq_printf(p, "%*s: ", prec, "DBL");
570 		for_each_online_cpu(j)
571 			seq_printf(p, "%10u ", per_cpu(irq_stat, j).doorbell_irqs);
572 		seq_printf(p, "  Doorbell interrupts\n");
573 	}
574 #endif
575 
576 	return 0;
577 }
578 
579 /*
580  * /proc/stat helpers
581  */
582 u64 arch_irq_stat_cpu(unsigned int cpu)
583 {
584 	u64 sum = per_cpu(irq_stat, cpu).timer_irqs_event;
585 
586 	sum += per_cpu(irq_stat, cpu).broadcast_irqs_event;
587 	sum += per_cpu(irq_stat, cpu).pmu_irqs;
588 	sum += per_cpu(irq_stat, cpu).mce_exceptions;
589 	sum += per_cpu(irq_stat, cpu).spurious_irqs;
590 	sum += per_cpu(irq_stat, cpu).timer_irqs_others;
591 	sum += per_cpu(irq_stat, cpu).hmi_exceptions;
592 	sum += per_cpu(irq_stat, cpu).sreset_irqs;
593 #ifdef CONFIG_PPC_WATCHDOG
594 	sum += per_cpu(irq_stat, cpu).soft_nmi_irqs;
595 #endif
596 #ifdef CONFIG_PPC_DOORBELL
597 	sum += per_cpu(irq_stat, cpu).doorbell_irqs;
598 #endif
599 
600 	return sum;
601 }
602 
603 static inline void check_stack_overflow(void)
604 {
605 #ifdef CONFIG_DEBUG_STACKOVERFLOW
606 	long sp;
607 
608 	sp = current_stack_pointer() & (THREAD_SIZE-1);
609 
610 	/* check for stack overflow: is there less than 2KB free? */
611 	if (unlikely(sp < 2048)) {
612 		pr_err("do_IRQ: stack overflow: %ld\n", sp);
613 		dump_stack();
614 	}
615 #endif
616 }
617 
618 void __do_irq(struct pt_regs *regs)
619 {
620 	unsigned int irq;
621 
622 	irq_enter();
623 
624 	trace_irq_entry(regs);
625 
626 	check_stack_overflow();
627 
628 	/*
629 	 * Query the platform PIC for the interrupt & ack it.
630 	 *
631 	 * This will typically lower the interrupt line to the CPU
632 	 */
633 	irq = ppc_md.get_irq();
634 
635 	/* We can hard enable interrupts now to allow perf interrupts */
636 	may_hard_irq_enable();
637 
638 	/* And finally process it */
639 	if (unlikely(!irq))
640 		__this_cpu_inc(irq_stat.spurious_irqs);
641 	else
642 		generic_handle_irq(irq);
643 
644 	trace_irq_exit(regs);
645 
646 	irq_exit();
647 }
648 
649 void do_IRQ(struct pt_regs *regs)
650 {
651 	struct pt_regs *old_regs = set_irq_regs(regs);
652 	void *cursp, *irqsp, *sirqsp;
653 
654 	/* Switch to the irq stack to handle this */
655 	cursp = (void *)(current_stack_pointer() & ~(THREAD_SIZE - 1));
656 	irqsp = hardirq_ctx[raw_smp_processor_id()];
657 	sirqsp = softirq_ctx[raw_smp_processor_id()];
658 
659 	/* Already there ? */
660 	if (unlikely(cursp == irqsp || cursp == sirqsp)) {
661 		__do_irq(regs);
662 		set_irq_regs(old_regs);
663 		return;
664 	}
665 	/* Switch stack and call */
666 	call_do_irq(regs, irqsp);
667 
668 	set_irq_regs(old_regs);
669 }
670 
671 void __init init_IRQ(void)
672 {
673 	if (ppc_md.init_IRQ)
674 		ppc_md.init_IRQ();
675 }
676 
677 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
678 void   *critirq_ctx[NR_CPUS] __read_mostly;
679 void    *dbgirq_ctx[NR_CPUS] __read_mostly;
680 void *mcheckirq_ctx[NR_CPUS] __read_mostly;
681 #endif
682 
683 void *softirq_ctx[NR_CPUS] __read_mostly;
684 void *hardirq_ctx[NR_CPUS] __read_mostly;
685 
686 void do_softirq_own_stack(void)
687 {
688 	call_do_softirq(softirq_ctx[smp_processor_id()]);
689 }
690 
691 irq_hw_number_t virq_to_hw(unsigned int virq)
692 {
693 	struct irq_data *irq_data = irq_get_irq_data(virq);
694 	return WARN_ON(!irq_data) ? 0 : irq_data->hwirq;
695 }
696 EXPORT_SYMBOL_GPL(virq_to_hw);
697 
698 #ifdef CONFIG_SMP
699 int irq_choose_cpu(const struct cpumask *mask)
700 {
701 	int cpuid;
702 
703 	if (cpumask_equal(mask, cpu_online_mask)) {
704 		static int irq_rover;
705 		static DEFINE_RAW_SPINLOCK(irq_rover_lock);
706 		unsigned long flags;
707 
708 		/* Round-robin distribution... */
709 do_round_robin:
710 		raw_spin_lock_irqsave(&irq_rover_lock, flags);
711 
712 		irq_rover = cpumask_next(irq_rover, cpu_online_mask);
713 		if (irq_rover >= nr_cpu_ids)
714 			irq_rover = cpumask_first(cpu_online_mask);
715 
716 		cpuid = irq_rover;
717 
718 		raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
719 	} else {
720 		cpuid = cpumask_first_and(mask, cpu_online_mask);
721 		if (cpuid >= nr_cpu_ids)
722 			goto do_round_robin;
723 	}
724 
725 	return get_hard_smp_processor_id(cpuid);
726 }
727 #else
728 int irq_choose_cpu(const struct cpumask *mask)
729 {
730 	return hard_smp_processor_id();
731 }
732 #endif
733 
734 #ifdef CONFIG_PPC64
735 static int __init setup_noirqdistrib(char *str)
736 {
737 	distribute_irqs = 0;
738 	return 1;
739 }
740 
741 __setup("noirqdistrib", setup_noirqdistrib);
742 #endif /* CONFIG_PPC64 */
743