irq.c 22 KB

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