blob: f291f7826abc65fe27bd9502ff45d922fa3f9e4e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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)
Stephen Rothwell756e7102005-11-09 18:07:45 +11006 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
7 * Copyright (C) 1996-2001 Cort Dougan
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Adapted for Power Macintosh by Paul Mackerras
9 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
Stephen Rothwell756e7102005-11-09 18:07:45 +110010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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.
Stephen Rothwell756e7102005-11-09 18:07:45 +110021 *
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_8xx
28 * to reduce code space and undefined function references.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100031#undef DEBUG
32
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040033#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/threads.h>
35#include <linux/kernel_stat.h>
36#include <linux/signal.h>
37#include <linux/sched.h>
Stephen Rothwell756e7102005-11-09 18:07:45 +110038#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/ioport.h>
40#include <linux/interrupt.h>
41#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
43#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/irq.h>
Stephen Rothwell756e7102005-11-09 18:07:45 +110046#include <linux/seq_file.h>
47#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/profile.h>
49#include <linux/bitops.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100050#include <linux/list.h>
51#include <linux/radix-tree.h>
52#include <linux/mutex.h>
Jake Moilanen45934c42006-07-27 13:17:25 -050053#include <linux/pci.h>
Michael Ellerman60b332e2007-08-28 18:47:57 +100054#include <linux/debugfs.h>
Grant Likelye3873442010-06-18 11:09:59 -060055#include <linux/of.h>
56#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/machdep.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100066#include <asm/udbg.h>
Dave Kleikamp3e7f45a2010-08-18 06:44:25 +000067#include <asm/smp.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110068#include <asm/livepatch.h>
Daniel Axtens0545d542016-09-06 15:32:43 +100069#include <asm/asm-prototypes.h>
Benjamin Herrenschmidt89c81792010-07-09 15:31:28 +100070
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100071#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/paca.h>
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100073#include <asm/firmware.h>
Takao Shinohara0874dd42007-05-01 07:01:07 +100074#include <asm/lv1call.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
Anton Blanchard1bf4af12009-10-26 18:47:42 +000076#define CREATE_TRACE_POINTS
77#include <asm/trace.h>
Kevin Haob92a2262016-07-23 14:42:40 +053078#include <asm/cpu_has_feature.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Anton Blanchard8c007bf2010-01-31 20:30:23 +000080DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
81EXPORT_PER_CPU_SYMBOL(irq_stat);
82
Stephen Rothwell868accb2005-11-10 18:38:46 +110083int __irq_offset_value;
Stephen Rothwell756e7102005-11-09 18:07:45 +110084
Stephen Rothwell756e7102005-11-09 18:07:45 +110085#ifdef CONFIG_PPC32
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100086EXPORT_SYMBOL(__irq_offset_value);
Stephen Rothwell756e7102005-11-09 18:07:45 +110087atomic_t ppc_n_lost_interrupts;
88
89#ifdef CONFIG_TAU_INT
90extern int tau_initialized;
91extern int tau_interrupts(int);
92#endif
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100093#endif /* CONFIG_PPC32 */
Stephen Rothwell756e7102005-11-09 18:07:45 +110094
Stephen Rothwell756e7102005-11-09 18:07:45 +110095#ifdef CONFIG_PPC64
Michael Ellermancd015702009-10-13 19:45:03 +000096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097int distribute_irqs = 1;
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100098
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +110099static inline notrace unsigned long get_irq_happened(void)
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000100{
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100101 unsigned long happened;
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000102
103 __asm__ __volatile__("lbz %0,%1(13)"
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100104 : "=r" (happened) : "i" (offsetof(struct paca_struct, irq_happened)));
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000105
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100106 return happened;
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000107}
108
Steven Rostedt4e491d12008-05-14 23:49:44 -0400109static inline notrace void set_soft_enabled(unsigned long enable)
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000110{
111 __asm__ __volatile__("stb %0,%1(13)"
112 : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
113}
114
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100115static inline notrace int decrementer_check_overflow(void)
Anton Blanchard7df10272011-11-23 20:07:22 +0000116{
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100117 u64 now = get_tb_or_rtc();
Christoph Lameter69111ba2014-10-21 15:23:25 -0500118 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100119
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100120 return now >= *next_tb;
Anton Blanchard7df10272011-11-23 20:07:22 +0000121}
122
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100123/* This is called whenever we are re-enabling interrupts
Ian Munsiefe9e1d52012-11-14 18:49:48 +0000124 * and returns either 0 (nothing to do) or 500/900/280/a00/e80 if
125 * there's an EE, DEC or DBELL to generate.
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100126 *
127 * This is called in two contexts: From arch_local_irq_restore()
128 * before soft-enabling interrupts, and from the exception exit
129 * path when returning from an interrupt from a soft-disabled to
130 * a soft enabled context. In both case we have interrupts hard
131 * disabled.
132 *
133 * We take care of only clearing the bits we handled in the
134 * PACA irq_happened field since we can only re-emit one at a
135 * time and we don't want to "lose" one.
136 */
137notrace unsigned int __check_irq_replay(void)
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000138{
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000139 /*
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100140 * We use local_paca rather than get_paca() to avoid all
141 * the debug_smp_processor_id() business in this low level
142 * function
Hugh Dickinsef2b3432006-11-10 21:32:40 +0000143 */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100144 unsigned char happened = local_paca->irq_happened;
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000145
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100146 /* Clear bit 0 which we wouldn't clear otherwise */
147 local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
Nicholas Piggin3db40c32017-08-01 23:59:28 +1000148 if (happened & PACA_IRQ_HARD_DIS) {
149 /*
150 * We may have missed a decrementer interrupt if hard disabled.
151 * Check the decrementer register in case we had a rollover
152 * while hard disabled.
153 */
154 if (!(happened & PACA_IRQ_DEC)) {
155 if (decrementer_check_overflow()) {
156 local_paca->irq_happened |= PACA_IRQ_DEC;
157 happened |= PACA_IRQ_DEC;
158 }
159 }
160 }
Takao Shinohara0874dd42007-05-01 07:01:07 +1000161
162 /*
163 * Force the delivery of pending soft-disabled interrupts on PS3.
164 * Any HV call will have this side effect.
165 */
166 if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
Geoff Levand816cb49a2011-11-29 15:38:50 +0000167 u64 tmp, tmp2;
168 lv1_get_version_info(&tmp, &tmp2);
Takao Shinohara0874dd42007-05-01 07:01:07 +1000169 }
170
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100171 /*
Nicholas Piggine0e0d6b2016-09-14 13:01:21 +1000172 * Check if an hypervisor Maintenance interrupt happened.
173 * This is a higher priority interrupt than the others, so
174 * replay it first.
175 */
176 local_paca->irq_happened &= ~PACA_IRQ_HMI;
177 if (happened & PACA_IRQ_HMI)
178 return 0xe60;
179
180 /*
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100181 * We may have missed a decrementer interrupt. We check the
182 * decrementer itself rather than the paca irq_happened field
183 * in case we also had a rollover while hard disabled
184 */
185 local_paca->irq_happened &= ~PACA_IRQ_DEC;
Nicholas Piggin3db40c32017-08-01 23:59:28 +1000186 if (happened & PACA_IRQ_DEC)
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100187 return 0x900;
188
189 /* Finally check if an external interrupt happened */
190 local_paca->irq_happened &= ~PACA_IRQ_EE;
191 if (happened & PACA_IRQ_EE)
192 return 0x500;
193
194#ifdef CONFIG_PPC_BOOK3E
195 /* Finally check if an EPR external interrupt happened
196 * this bit is typically set if we need to handle another
197 * "edge" interrupt from within the MPIC "EPR" handler
198 */
199 local_paca->irq_happened &= ~PACA_IRQ_EE_EDGE;
200 if (happened & PACA_IRQ_EE_EDGE)
201 return 0x500;
202
203 local_paca->irq_happened &= ~PACA_IRQ_DBELL;
204 if (happened & PACA_IRQ_DBELL)
205 return 0x280;
Ian Munsiefe9e1d52012-11-14 18:49:48 +0000206#else
207 local_paca->irq_happened &= ~PACA_IRQ_DBELL;
208 if (happened & PACA_IRQ_DBELL) {
209 if (cpu_has_feature(CPU_FTR_HVMODE))
210 return 0xe80;
211 return 0xa00;
212 }
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100213#endif /* CONFIG_PPC_BOOK3E */
214
215 /* There should be nothing left ! */
216 BUG_ON(local_paca->irq_happened != 0);
217
218 return 0;
219}
220
221notrace void arch_local_irq_restore(unsigned long en)
222{
223 unsigned char irq_happened;
224 unsigned int replay;
225
226 /* Write the new soft-enabled value */
227 set_soft_enabled(en);
228 if (!en)
229 return;
230 /*
231 * From this point onward, we can take interrupts, preempt,
232 * etc... unless we got hard-disabled. We check if an event
233 * happened. If none happened, we know we can just return.
234 *
235 * We may have preempted before the check below, in which case
236 * we are checking the "new" CPU instead of the old one. This
237 * is only a problem if an event happened on the "old" CPU.
238 *
Stephen Rothwell1d9a4732012-03-21 18:23:27 +0000239 * External interrupt events will have caused interrupts to
240 * be hard-disabled, so there is no problem, we
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100241 * cannot have preempted.
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100242 */
243 irq_happened = get_irq_happened();
244 if (!irq_happened)
245 return;
246
247 /*
248 * We need to hard disable to get a trusted value from
249 * __check_irq_replay(). We also need to soft-disable
250 * again to avoid warnings in there due to the use of
251 * per-cpu variables.
252 *
253 * We know that if the value in irq_happened is exactly 0x01
254 * then we are already hard disabled (there are other less
255 * common cases that we'll ignore for now), so we skip the
256 * (expensive) mtmsrd.
257 */
258 if (unlikely(irq_happened != PACA_IRQ_HARD_DIS))
259 __hard_irq_disable();
Benjamin Herrenschmidt21b2de32012-07-10 18:37:56 +1000260#ifdef CONFIG_TRACE_IRQFLAGS
Benjamin Herrenschmidt7c0482e2012-05-10 16:12:38 +0000261 else {
262 /*
263 * We should already be hard disabled here. We had bugs
264 * where that wasn't the case so let's dbl check it and
265 * warn if we are wrong. Only do that when IRQ tracing
266 * is enabled as mfmsr() can be costly.
267 */
268 if (WARN_ON(mfmsr() & MSR_EE))
269 __hard_irq_disable();
270 }
Andrew Donnellanfa2cff3f2016-07-05 16:12:34 +1000271#endif /* CONFIG_TRACE_IRQFLAGS */
Benjamin Herrenschmidt7c0482e2012-05-10 16:12:38 +0000272
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100273 set_soft_enabled(0);
274
275 /*
276 * Check if anything needs to be re-emitted. We haven't
277 * soft-enabled yet to avoid warnings in decrementer_check_overflow
278 * accessing per-cpu variables
279 */
280 replay = __check_irq_replay();
281
282 /* We can soft-enable now */
283 set_soft_enabled(1);
284
285 /*
286 * And replay if we have to. This will return with interrupts
287 * hard-enabled.
288 */
289 if (replay) {
290 __replay_interrupt(replay);
291 return;
292 }
293
294 /* Finally, let's ensure we are hard enabled */
Benjamin Herrenschmidte1fa2e12007-05-10 22:22:45 -0700295 __hard_irq_enable();
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000296}
David Howellsdf9ee292010-10-07 14:08:55 +0100297EXPORT_SYMBOL(arch_local_irq_restore);
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100298
299/*
300 * This is specifically called by assembly code to re-enable interrupts
301 * if they are currently disabled. This is typically called before
302 * schedule() or do_signal() when returning to userspace. We do it
303 * in C to avoid the burden of dealing with lockdep etc...
Benjamin Herrenschmidt56dfa7f2012-05-08 13:31:59 +1000304 *
305 * NOTE: This is called with interrupts hard disabled but not marked
306 * as such in paca->irq_happened, so we need to resync this.
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100307 */
Steven Rostedt2d773aa2012-06-04 16:27:54 +0000308void notrace restore_interrupts(void)
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100309{
Benjamin Herrenschmidt56dfa7f2012-05-08 13:31:59 +1000310 if (irqs_disabled()) {
311 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100312 local_irq_enable();
Benjamin Herrenschmidt56dfa7f2012-05-08 13:31:59 +1000313 } else
314 __hard_irq_enable();
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100315}
316
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +1000317/*
318 * This is a helper to use when about to go into idle low-power
319 * when the latter has the side effect of re-enabling interrupts
320 * (such as calling H_CEDE under pHyp).
321 *
322 * You call this function with interrupts soft-disabled (this is
323 * already the case when ppc_md.power_save is called). The function
324 * will return whether to enter power save or just return.
325 *
326 * In the former case, it will have notified lockdep of interrupts
327 * being re-enabled and generally sanitized the lazy irq state,
328 * and in the latter case it will leave with interrupts hard
329 * disabled and marked as such, so the local_irq_enable() call
Geert Uytterhoeven0d2b7ea2014-06-06 14:38:33 -0700330 * in arch_cpu_idle() will properly re-enable everything.
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +1000331 */
332bool prep_irq_for_idle(void)
333{
334 /*
335 * First we need to hard disable to ensure no interrupt
336 * occurs before we effectively enter the low power state
337 */
Nicholas Piggin2201f992017-06-13 23:05:45 +1000338 __hard_irq_disable();
339 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
Benjamin Herrenschmidtbe2cf202012-07-10 18:36:40 +1000340
341 /*
342 * If anything happened while we were soft-disabled,
343 * we return now and do not enter the low power state.
344 */
345 if (lazy_irq_pending())
346 return false;
347
348 /* Tell lockdep we are about to re-enable */
349 trace_hardirqs_on();
350
351 /*
352 * Mark interrupts as soft-enabled and clear the
353 * PACA_IRQ_HARD_DIS from the pending mask since we
354 * are about to hard enable as well as a side effect
355 * of entering the low power state.
356 */
357 local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
358 local_paca->soft_enabled = 1;
359
360 /* Tell the caller to enter the low power state */
361 return true;
362}
363
Nicholas Piggin771d4302017-06-13 23:05:47 +1000364#ifdef CONFIG_PPC_BOOK3S
Benjamin Herrenschmidt1d607bb2016-07-08 16:37:07 +1000365/*
Nicholas Piggin2201f992017-06-13 23:05:45 +1000366 * This is for idle sequences that return with IRQs off, but the
367 * idle state itself wakes on interrupt. Tell the irq tracer that
368 * IRQs are enabled for the duration of idle so it does not get long
369 * off times. Must be paired with fini_irq_for_idle_irqsoff.
370 */
371bool prep_irq_for_idle_irqsoff(void)
372{
373 WARN_ON(!irqs_disabled());
374
375 /*
376 * First we need to hard disable to ensure no interrupt
377 * occurs before we effectively enter the low power state
378 */
379 __hard_irq_disable();
380 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
381
382 /*
383 * If anything happened while we were soft-disabled,
384 * we return now and do not enter the low power state.
385 */
386 if (lazy_irq_pending())
387 return false;
388
389 /* Tell lockdep we are about to re-enable */
390 trace_hardirqs_on();
391
392 return true;
393}
394
395/*
Nicholas Piggin771d4302017-06-13 23:05:47 +1000396 * Take the SRR1 wakeup reason, index into this table to find the
397 * appropriate irq_happened bit.
398 */
399static const u8 srr1_to_lazyirq[0x10] = {
400 0, 0, 0,
401 PACA_IRQ_DBELL,
402 0,
403 PACA_IRQ_DBELL,
404 PACA_IRQ_DEC,
405 0,
406 PACA_IRQ_EE,
407 PACA_IRQ_EE,
408 PACA_IRQ_HMI,
409 0, 0, 0, 0, 0 };
410
411void irq_set_pending_from_srr1(unsigned long srr1)
412{
413 unsigned int idx = (srr1 & SRR1_WAKEMASK_P8) >> 18;
414
415 /*
416 * The 0 index (SRR1[42:45]=b0000) must always evaluate to 0,
417 * so this can be called unconditionally with srr1 wake reason.
418 */
419 local_paca->irq_happened |= srr1_to_lazyirq[idx];
420}
421#endif /* CONFIG_PPC_BOOK3S */
422
423/*
Benjamin Herrenschmidt1d607bb2016-07-08 16:37:07 +1000424 * Force a replay of the external interrupt handler on this CPU.
425 */
426void force_external_irq_replay(void)
427{
428 /*
429 * This must only be called with interrupts soft-disabled,
430 * the replay will happen when re-enabling.
431 */
432 WARN_ON(!arch_irqs_disabled());
433
434 /* Indicate in the PACA that we have an interrupt to replay */
435 local_paca->irq_happened |= PACA_IRQ_EE;
436}
437
Stephen Rothwell756e7102005-11-09 18:07:45 +1100438#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Thomas Gleixner433c9c62011-03-25 17:04:59 +0100440int arch_show_interrupts(struct seq_file *p, int prec)
Anton Blanchardc86845e2010-01-31 20:33:18 +0000441{
442 int j;
443
444#if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
445 if (tau_initialized) {
446 seq_printf(p, "%*s: ", prec, "TAU");
447 for_each_online_cpu(j)
448 seq_printf(p, "%10u ", tau_interrupts(j));
449 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
450 }
451#endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
452
Anton Blanchard89713ed2010-01-31 20:34:06 +0000453 seq_printf(p, "%*s: ", prec, "LOC");
454 for_each_online_cpu(j)
fan.duc041cfa2013-01-23 16:06:11 +0800455 seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_event);
456 seq_printf(p, " Local timer interrupts for timer event device\n");
457
458 seq_printf(p, "%*s: ", prec, "LOC");
459 for_each_online_cpu(j)
460 seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_others);
461 seq_printf(p, " Local timer interrupts for others\n");
Anton Blanchard89713ed2010-01-31 20:34:06 +0000462
Anton Blanchard170811022010-01-31 20:34:36 +0000463 seq_printf(p, "%*s: ", prec, "SPU");
464 for_each_online_cpu(j)
465 seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
466 seq_printf(p, " Spurious interrupts\n");
467
Michael Ellermane8e813e2013-06-04 14:21:17 +1000468 seq_printf(p, "%*s: ", prec, "PMI");
Anton Blanchard89713ed2010-01-31 20:34:06 +0000469 for_each_online_cpu(j)
470 seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
471 seq_printf(p, " Performance monitoring interrupts\n");
472
473 seq_printf(p, "%*s: ", prec, "MCE");
474 for_each_online_cpu(j)
475 seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
476 seq_printf(p, " Machine check exceptions\n");
477
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530478 if (cpu_has_feature(CPU_FTR_HVMODE)) {
479 seq_printf(p, "%*s: ", prec, "HMI");
480 for_each_online_cpu(j)
481 seq_printf(p, "%10u ",
482 per_cpu(irq_stat, j).hmi_exceptions);
483 seq_printf(p, " Hypervisor Maintenance Interrupts\n");
484 }
485
Ian Munsiea6a058e2013-03-21 19:22:52 +0000486#ifdef CONFIG_PPC_DOORBELL
487 if (cpu_has_feature(CPU_FTR_DBELL)) {
488 seq_printf(p, "%*s: ", prec, "DBL");
489 for_each_online_cpu(j)
490 seq_printf(p, "%10u ", per_cpu(irq_stat, j).doorbell_irqs);
491 seq_printf(p, " Doorbell interrupts\n");
492 }
493#endif
494
Anton Blanchardc86845e2010-01-31 20:33:18 +0000495 return 0;
496}
497
Anton Blanchard89713ed2010-01-31 20:34:06 +0000498/*
499 * /proc/stat helpers
500 */
501u64 arch_irq_stat_cpu(unsigned int cpu)
502{
fan.duc041cfa2013-01-23 16:06:11 +0800503 u64 sum = per_cpu(irq_stat, cpu).timer_irqs_event;
Anton Blanchard89713ed2010-01-31 20:34:06 +0000504
505 sum += per_cpu(irq_stat, cpu).pmu_irqs;
506 sum += per_cpu(irq_stat, cpu).mce_exceptions;
Anton Blanchard170811022010-01-31 20:34:36 +0000507 sum += per_cpu(irq_stat, cpu).spurious_irqs;
fan.duc041cfa2013-01-23 16:06:11 +0800508 sum += per_cpu(irq_stat, cpu).timer_irqs_others;
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530509 sum += per_cpu(irq_stat, cpu).hmi_exceptions;
Ian Munsiea6a058e2013-03-21 19:22:52 +0000510#ifdef CONFIG_PPC_DOORBELL
511 sum += per_cpu(irq_stat, cpu).doorbell_irqs;
512#endif
Anton Blanchard89713ed2010-01-31 20:34:06 +0000513
514 return sum;
515}
516
Michael Ellermand7cb10d2009-04-22 15:31:37 +0000517static inline void check_stack_overflow(void)
518{
519#ifdef CONFIG_DEBUG_STACKOVERFLOW
520 long sp;
521
Anton Blanchardacf620e2014-10-13 19:41:39 +1100522 sp = current_stack_pointer() & (THREAD_SIZE-1);
Michael Ellermand7cb10d2009-04-22 15:31:37 +0000523
524 /* check for stack overflow: is there less than 2KB free? */
525 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
Anton Blancharda7696b32014-09-17 14:39:39 +1000526 pr_err("do_IRQ: stack overflow: %ld\n",
Michael Ellermand7cb10d2009-04-22 15:31:37 +0000527 sp - sizeof(struct thread_info));
528 dump_stack();
529 }
530#endif
531}
532
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000533void __do_irq(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000535 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Scott Wood4b218e92007-08-21 02:36:19 +1000537 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Li Zhonge72bbba2012-09-10 15:37:43 +0000539 trace_irq_entry(regs);
540
Michael Ellermand7cb10d2009-04-22 15:31:37 +0000541 check_stack_overflow();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100543 /*
544 * Query the platform PIC for the interrupt & ack it.
545 *
546 * This will typically lower the interrupt line to the CPU
547 */
Olaf Hering35a84c22006-10-07 22:08:26 +1000548 irq = ppc_md.get_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000550 /* We can hard enable interrupts now to allow perf interrupts */
Benjamin Herrenschmidt7230c562012-03-06 18:27:59 +1100551 may_hard_irq_enable();
552
553 /* And finally process it */
Michael Ellermanef24ba72016-09-06 21:53:24 +1000554 if (unlikely(!irq))
Christoph Lameter69111ba2014-10-21 15:23:25 -0500555 __this_cpu_inc(irq_stat.spurious_irqs);
Thomas Gleixnera4e04c92014-02-23 21:40:08 +0000556 else
Michael Ellerman0edc2ca2017-06-15 16:20:46 +1000557 generic_handle_irq(irq);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100558
Li Zhonge72bbba2012-09-10 15:37:43 +0000559 trace_irq_exit(regs);
560
Scott Wood4b218e92007-08-21 02:36:19 +1000561 irq_exit();
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000562}
563
564void do_IRQ(struct pt_regs *regs)
565{
566 struct pt_regs *old_regs = set_irq_regs(regs);
Benjamin Herrenschmidt8b5ede62013-10-08 08:08:24 +1100567 struct thread_info *curtp, *irqtp, *sirqtp;
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000568
569 /* Switch to the irq stack to handle this */
570 curtp = current_thread_info();
571 irqtp = hardirq_ctx[raw_smp_processor_id()];
Benjamin Herrenschmidt8b5ede62013-10-08 08:08:24 +1100572 sirqtp = softirq_ctx[raw_smp_processor_id()];
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000573
574 /* Already there ? */
Benjamin Herrenschmidt8b5ede62013-10-08 08:08:24 +1100575 if (unlikely(curtp == irqtp || curtp == sirqtp)) {
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000576 __do_irq(regs);
577 set_irq_regs(old_regs);
578 return;
579 }
580
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000581 /* Prepare the thread_info in the irq stack */
582 irqtp->task = curtp->task;
583 irqtp->flags = 0;
584
585 /* Copy the preempt_count so that the [soft]irq checks work. */
586 irqtp->preempt_count = curtp->preempt_count;
587
588 /* Switch stack and call */
589 call_do_irq(regs, irqtp);
590
591 /* Restore stack limit */
Benjamin Herrenschmidt0366a1c2013-09-23 14:29:11 +1000592 irqtp->task = NULL;
593
594 /* Copy back updates to the thread_info */
595 if (irqtp->flags)
596 set_bits(irqtp->flags, &curtp->flags);
597
David Howells7d12e782006-10-05 14:55:46 +0100598 set_irq_regs(old_regs);
Stephen Rothwelle1995002005-11-16 18:53:29 +1100599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601void __init init_IRQ(void)
602{
Sonny Rao70584572007-07-10 03:31:44 +1000603 if (ppc_md.init_IRQ)
604 ppc_md.init_IRQ();
Kumar Galabcf0b082008-04-30 03:49:55 -0500605
606 exc_lvl_ctx_init();
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 irq_ctx_init();
609}
610
Kumar Galabcf0b082008-04-30 03:49:55 -0500611#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
612struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
613struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
614struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
615
616void exc_lvl_ctx_init(void)
617{
618 struct thread_info *tp;
Michael Ellermanca1769f2011-04-14 22:32:04 +0000619 int i, cpu_nr;
Kumar Galabcf0b082008-04-30 03:49:55 -0500620
621 for_each_possible_cpu(i) {
Michael Ellermanca1769f2011-04-14 22:32:04 +0000622#ifdef CONFIG_PPC64
623 cpu_nr = i;
624#else
Kevin Hao04a34112014-01-29 18:24:54 +0800625#ifdef CONFIG_SMP
Michael Ellermanca1769f2011-04-14 22:32:04 +0000626 cpu_nr = get_hard_smp_processor_id(i);
Kevin Hao04a34112014-01-29 18:24:54 +0800627#else
628 cpu_nr = 0;
Michael Ellermanca1769f2011-04-14 22:32:04 +0000629#endif
Kevin Hao04a34112014-01-29 18:24:54 +0800630#endif
631
Michael Ellermanca1769f2011-04-14 22:32:04 +0000632 memset((void *)critirq_ctx[cpu_nr], 0, THREAD_SIZE);
633 tp = critirq_ctx[cpu_nr];
634 tp->cpu = cpu_nr;
Kumar Galabcf0b082008-04-30 03:49:55 -0500635 tp->preempt_count = 0;
636
637#ifdef CONFIG_BOOKE
Michael Ellermanca1769f2011-04-14 22:32:04 +0000638 memset((void *)dbgirq_ctx[cpu_nr], 0, THREAD_SIZE);
639 tp = dbgirq_ctx[cpu_nr];
640 tp->cpu = cpu_nr;
Kumar Galabcf0b082008-04-30 03:49:55 -0500641 tp->preempt_count = 0;
642
Michael Ellermanca1769f2011-04-14 22:32:04 +0000643 memset((void *)mcheckirq_ctx[cpu_nr], 0, THREAD_SIZE);
644 tp = mcheckirq_ctx[cpu_nr];
645 tp->cpu = cpu_nr;
Kumar Galabcf0b082008-04-30 03:49:55 -0500646 tp->preempt_count = HARDIRQ_OFFSET;
647#endif
648 }
649}
650#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Andreas Mohr22722052006-06-23 02:05:30 -0700652struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
653struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655void irq_ctx_init(void)
656{
657 struct thread_info *tp;
658 int i;
659
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800660 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
662 tp = softirq_ctx[i];
663 tp->cpu = i;
Michael Ellerman5d31a962016-03-24 22:04:04 +1100664 klp_init_thread_info(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
667 tp = hardirq_ctx[i];
668 tp->cpu = i;
Michael Ellerman5d31a962016-03-24 22:04:04 +1100669 klp_init_thread_info(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671}
672
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200673void do_softirq_own_stack(void)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100674{
675 struct thread_info *curtp, *irqtp;
676
677 curtp = current_thread_info();
678 irqtp = softirq_ctx[smp_processor_id()];
679 irqtp->task = curtp->task;
Benjamin Herrenschmidt50d2a422011-07-18 17:17:22 +0000680 irqtp->flags = 0;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100681 call_do_softirq(irqtp);
682 irqtp->task = NULL;
Benjamin Herrenschmidt50d2a422011-07-18 17:17:22 +0000683
684 /* Set any flag that may have been set on the
685 * alternate stack
686 */
687 if (irqtp->flags)
688 set_bits(irqtp->flags, &curtp->flags);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100689}
690
Olof Johansson35923f122007-06-04 14:47:04 +1000691irq_hw_number_t virq_to_hw(unsigned int virq)
692{
Grant Likely4bbdd452012-02-14 14:06:51 -0700693 struct irq_data *irq_data = irq_get_irq_data(virq);
694 return WARN_ON(!irq_data) ? 0 : irq_data->hwirq;
Olof Johansson35923f122007-06-04 14:47:04 +1000695}
696EXPORT_SYMBOL_GPL(virq_to_hw);
697
Stuart Yoder6ec36b52011-05-19 08:54:26 -0500698#ifdef CONFIG_SMP
699int irq_choose_cpu(const struct cpumask *mask)
700{
701 int cpuid;
702
Kim Phillips2074b1d2012-05-17 15:11:45 +0000703 if (cpumask_equal(mask, cpu_online_mask)) {
Stuart Yoder6ec36b52011-05-19 08:54:26 -0500704 static int irq_rover;
705 static DEFINE_RAW_SPINLOCK(irq_rover_lock);
706 unsigned long flags;
707
708 /* Round-robin distribution... */
709do_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
728int irq_choose_cpu(const struct cpumask *mask)
729{
730 return hard_smp_processor_id();
731}
732#endif
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000733
Michael Ellermancd015702009-10-13 19:45:03 +0000734int arch_early_irq_init(void)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000735{
Michael Ellermancd015702009-10-13 19:45:03 +0000736 return 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000737}
738
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100739#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int __init setup_noirqdistrib(char *str)
741{
742 distribute_irqs = 0;
743 return 1;
744}
745
746__setup("noirqdistrib", setup_noirqdistrib);
Stephen Rothwell756e7102005-11-09 18:07:45 +1100747#endif /* CONFIG_PPC64 */