irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Common interrupt code for 32 and 64 bit
  3. */
  4. #include <linux/cpu.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/kernel_stat.h>
  7. #include <linux/of.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/smp.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/delay.h>
  12. #include <linux/export.h>
  13. #include <linux/irq.h>
  14. #include <asm/apic.h>
  15. #include <asm/io_apic.h>
  16. #include <asm/irq.h>
  17. #include <asm/mce.h>
  18. #include <asm/hw_irq.h>
  19. #include <asm/desc.h>
  20. #define CREATE_TRACE_POINTS
  21. #include <asm/trace/irq_vectors.h>
  22. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  23. EXPORT_PER_CPU_SYMBOL(irq_stat);
  24. DEFINE_PER_CPU(struct pt_regs *, irq_regs);
  25. EXPORT_PER_CPU_SYMBOL(irq_regs);
  26. atomic_t irq_err_count;
  27. /*
  28. * 'what should we do if we get a hw irq event on an illegal vector'.
  29. * each architecture has to answer this themselves.
  30. */
  31. void ack_bad_irq(unsigned int irq)
  32. {
  33. if (printk_ratelimit())
  34. pr_err("unexpected IRQ trap at vector %02x\n", irq);
  35. /*
  36. * Currently unexpected vectors happen only on SMP and APIC.
  37. * We _must_ ack these because every local APIC has only N
  38. * irq slots per priority level, and a 'hanging, unacked' IRQ
  39. * holds up an irq slot - in excessive cases (when multiple
  40. * unexpected vectors occur) that might lock up the APIC
  41. * completely.
  42. * But only ack when the APIC is enabled -AK
  43. */
  44. ack_APIC_irq();
  45. }
  46. #define irq_stats(x) (&per_cpu(irq_stat, x))
  47. /*
  48. * /proc/interrupts printing for arch specific interrupts
  49. */
  50. int arch_show_interrupts(struct seq_file *p, int prec)
  51. {
  52. int j;
  53. seq_printf(p, "%*s: ", prec, "NMI");
  54. for_each_online_cpu(j)
  55. seq_printf(p, "%10u ", irq_stats(j)->__nmi_count);
  56. seq_puts(p, " Non-maskable interrupts\n");
  57. #ifdef CONFIG_X86_LOCAL_APIC
  58. seq_printf(p, "%*s: ", prec, "LOC");
  59. for_each_online_cpu(j)
  60. seq_printf(p, "%10u ", irq_stats(j)->apic_timer_irqs);
  61. seq_puts(p, " Local timer interrupts\n");
  62. seq_printf(p, "%*s: ", prec, "SPU");
  63. for_each_online_cpu(j)
  64. seq_printf(p, "%10u ", irq_stats(j)->irq_spurious_count);
  65. seq_puts(p, " Spurious interrupts\n");
  66. seq_printf(p, "%*s: ", prec, "PMI");
  67. for_each_online_cpu(j)
  68. seq_printf(p, "%10u ", irq_stats(j)->apic_perf_irqs);
  69. seq_puts(p, " Performance monitoring interrupts\n");
  70. seq_printf(p, "%*s: ", prec, "IWI");
  71. for_each_online_cpu(j)
  72. seq_printf(p, "%10u ", irq_stats(j)->apic_irq_work_irqs);
  73. seq_puts(p, " IRQ work interrupts\n");
  74. seq_printf(p, "%*s: ", prec, "RTR");
  75. for_each_online_cpu(j)
  76. seq_printf(p, "%10u ", irq_stats(j)->icr_read_retry_count);
  77. seq_puts(p, " APIC ICR read retries\n");
  78. if (x86_platform_ipi_callback) {
  79. seq_printf(p, "%*s: ", prec, "PLT");
  80. for_each_online_cpu(j)
  81. seq_printf(p, "%10u ", irq_stats(j)->x86_platform_ipis);
  82. seq_puts(p, " Platform interrupts\n");
  83. }
  84. #endif
  85. #ifdef CONFIG_SMP
  86. seq_printf(p, "%*s: ", prec, "RES");
  87. for_each_online_cpu(j)
  88. seq_printf(p, "%10u ", irq_stats(j)->irq_resched_count);
  89. seq_puts(p, " Rescheduling interrupts\n");
  90. seq_printf(p, "%*s: ", prec, "CAL");
  91. for_each_online_cpu(j)
  92. seq_printf(p, "%10u ", irq_stats(j)->irq_call_count);
  93. seq_puts(p, " Function call interrupts\n");
  94. seq_printf(p, "%*s: ", prec, "TLB");
  95. for_each_online_cpu(j)
  96. seq_printf(p, "%10u ", irq_stats(j)->irq_tlb_count);
  97. seq_puts(p, " TLB shootdowns\n");
  98. #endif
  99. #ifdef CONFIG_X86_THERMAL_VECTOR
  100. seq_printf(p, "%*s: ", prec, "TRM");
  101. for_each_online_cpu(j)
  102. seq_printf(p, "%10u ", irq_stats(j)->irq_thermal_count);
  103. seq_puts(p, " Thermal event interrupts\n");
  104. #endif
  105. #ifdef CONFIG_X86_MCE_THRESHOLD
  106. seq_printf(p, "%*s: ", prec, "THR");
  107. for_each_online_cpu(j)
  108. seq_printf(p, "%10u ", irq_stats(j)->irq_threshold_count);
  109. seq_puts(p, " Threshold APIC interrupts\n");
  110. #endif
  111. #ifdef CONFIG_X86_MCE_AMD
  112. seq_printf(p, "%*s: ", prec, "DFR");
  113. for_each_online_cpu(j)
  114. seq_printf(p, "%10u ", irq_stats(j)->irq_deferred_error_count);
  115. seq_puts(p, " Deferred Error APIC interrupts\n");
  116. #endif
  117. #ifdef CONFIG_X86_MCE
  118. seq_printf(p, "%*s: ", prec, "MCE");
  119. for_each_online_cpu(j)
  120. seq_printf(p, "%10u ", per_cpu(mce_exception_count, j));
  121. seq_puts(p, " Machine check exceptions\n");
  122. seq_printf(p, "%*s: ", prec, "MCP");
  123. for_each_online_cpu(j)
  124. seq_printf(p, "%10u ", per_cpu(mce_poll_count, j));
  125. seq_puts(p, " Machine check polls\n");
  126. #endif
  127. #if IS_ENABLED(CONFIG_HYPERV) || defined(CONFIG_XEN)
  128. if (test_bit(HYPERVISOR_CALLBACK_VECTOR, system_vectors)) {
  129. seq_printf(p, "%*s: ", prec, "HYP");
  130. for_each_online_cpu(j)
  131. seq_printf(p, "%10u ",
  132. irq_stats(j)->irq_hv_callback_count);
  133. seq_puts(p, " Hypervisor callback interrupts\n");
  134. }
  135. #endif
  136. #if IS_ENABLED(CONFIG_HYPERV)
  137. if (test_bit(HYPERV_REENLIGHTENMENT_VECTOR, system_vectors)) {
  138. seq_printf(p, "%*s: ", prec, "HRE");
  139. for_each_online_cpu(j)
  140. seq_printf(p, "%10u ",
  141. irq_stats(j)->irq_hv_reenlightenment_count);
  142. seq_puts(p, " Hyper-V reenlightenment interrupts\n");
  143. }
  144. if (test_bit(HYPERV_STIMER0_VECTOR, system_vectors)) {
  145. seq_printf(p, "%*s: ", prec, "HVS");
  146. for_each_online_cpu(j)
  147. seq_printf(p, "%10u ",
  148. irq_stats(j)->hyperv_stimer0_count);
  149. seq_puts(p, " Hyper-V stimer0 interrupts\n");
  150. }
  151. #endif
  152. seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
  153. #if defined(CONFIG_X86_IO_APIC)
  154. seq_printf(p, "%*s: %10u\n", prec, "MIS", atomic_read(&irq_mis_count));
  155. #endif
  156. #ifdef CONFIG_HAVE_KVM
  157. seq_printf(p, "%*s: ", prec, "PIN");
  158. for_each_online_cpu(j)
  159. seq_printf(p, "%10u ", irq_stats(j)->kvm_posted_intr_ipis);
  160. seq_puts(p, " Posted-interrupt notification event\n");
  161. seq_printf(p, "%*s: ", prec, "NPI");
  162. for_each_online_cpu(j)
  163. seq_printf(p, "%10u ",
  164. irq_stats(j)->kvm_posted_intr_nested_ipis);
  165. seq_puts(p, " Nested posted-interrupt event\n");
  166. seq_printf(p, "%*s: ", prec, "PIW");
  167. for_each_online_cpu(j)
  168. seq_printf(p, "%10u ",
  169. irq_stats(j)->kvm_posted_intr_wakeup_ipis);
  170. seq_puts(p, " Posted-interrupt wakeup event\n");
  171. #endif
  172. return 0;
  173. }
  174. /*
  175. * /proc/stat helpers
  176. */
  177. u64 arch_irq_stat_cpu(unsigned int cpu)
  178. {
  179. u64 sum = irq_stats(cpu)->__nmi_count;
  180. #ifdef CONFIG_X86_LOCAL_APIC
  181. sum += irq_stats(cpu)->apic_timer_irqs;
  182. sum += irq_stats(cpu)->irq_spurious_count;
  183. sum += irq_stats(cpu)->apic_perf_irqs;
  184. sum += irq_stats(cpu)->apic_irq_work_irqs;
  185. sum += irq_stats(cpu)->icr_read_retry_count;
  186. if (x86_platform_ipi_callback)
  187. sum += irq_stats(cpu)->x86_platform_ipis;
  188. #endif
  189. #ifdef CONFIG_SMP
  190. sum += irq_stats(cpu)->irq_resched_count;
  191. sum += irq_stats(cpu)->irq_call_count;
  192. #endif
  193. #ifdef CONFIG_X86_THERMAL_VECTOR
  194. sum += irq_stats(cpu)->irq_thermal_count;
  195. #endif
  196. #ifdef CONFIG_X86_MCE_THRESHOLD
  197. sum += irq_stats(cpu)->irq_threshold_count;
  198. #endif
  199. #ifdef CONFIG_X86_MCE
  200. sum += per_cpu(mce_exception_count, cpu);
  201. sum += per_cpu(mce_poll_count, cpu);
  202. #endif
  203. return sum;
  204. }
  205. u64 arch_irq_stat(void)
  206. {
  207. u64 sum = atomic_read(&irq_err_count);
  208. return sum;
  209. }
  210. /*
  211. * do_IRQ handles all normal device IRQ's (the special
  212. * SMP cross-CPU interrupts have their own specific
  213. * handlers).
  214. */
  215. __visible unsigned int __irq_entry do_IRQ(struct pt_regs *regs)
  216. {
  217. struct pt_regs *old_regs = set_irq_regs(regs);
  218. struct irq_desc * desc;
  219. /* high bit used in ret_from_ code */
  220. unsigned vector = ~regs->orig_ax;
  221. entering_irq();
  222. /* entering_irq() tells RCU that we're not quiescent. Check it. */
  223. RCU_LOCKDEP_WARN(!rcu_is_watching(), "IRQ failed to wake up RCU");
  224. desc = __this_cpu_read(vector_irq[vector]);
  225. if (!handle_irq(desc, regs)) {
  226. ack_APIC_irq();
  227. if (desc != VECTOR_RETRIGGERED && desc != VECTOR_SHUTDOWN) {
  228. pr_emerg_ratelimited("%s: %d.%d No irq handler for vector\n",
  229. __func__, smp_processor_id(),
  230. vector);
  231. } else {
  232. __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
  233. }
  234. }
  235. exiting_irq();
  236. set_irq_regs(old_regs);
  237. return 1;
  238. }
  239. #ifdef CONFIG_X86_LOCAL_APIC
  240. /* Function pointer for generic interrupt vector handling */
  241. void (*x86_platform_ipi_callback)(void) = NULL;
  242. /*
  243. * Handler for X86_PLATFORM_IPI_VECTOR.
  244. */
  245. __visible void __irq_entry smp_x86_platform_ipi(struct pt_regs *regs)
  246. {
  247. struct pt_regs *old_regs = set_irq_regs(regs);
  248. entering_ack_irq();
  249. trace_x86_platform_ipi_entry(X86_PLATFORM_IPI_VECTOR);
  250. inc_irq_stat(x86_platform_ipis);
  251. if (x86_platform_ipi_callback)
  252. x86_platform_ipi_callback();
  253. trace_x86_platform_ipi_exit(X86_PLATFORM_IPI_VECTOR);
  254. exiting_irq();
  255. set_irq_regs(old_regs);
  256. }
  257. #endif
  258. #ifdef CONFIG_HAVE_KVM
  259. static void dummy_handler(void) {}
  260. static void (*kvm_posted_intr_wakeup_handler)(void) = dummy_handler;
  261. void kvm_set_posted_intr_wakeup_handler(void (*handler)(void))
  262. {
  263. if (handler)
  264. kvm_posted_intr_wakeup_handler = handler;
  265. else
  266. kvm_posted_intr_wakeup_handler = dummy_handler;
  267. }
  268. EXPORT_SYMBOL_GPL(kvm_set_posted_intr_wakeup_handler);
  269. /*
  270. * Handler for POSTED_INTERRUPT_VECTOR.
  271. */
  272. __visible void smp_kvm_posted_intr_ipi(struct pt_regs *regs)
  273. {
  274. struct pt_regs *old_regs = set_irq_regs(regs);
  275. entering_ack_irq();
  276. inc_irq_stat(kvm_posted_intr_ipis);
  277. exiting_irq();
  278. set_irq_regs(old_regs);
  279. }
  280. /*
  281. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  282. */
  283. __visible void smp_kvm_posted_intr_wakeup_ipi(struct pt_regs *regs)
  284. {
  285. struct pt_regs *old_regs = set_irq_regs(regs);
  286. entering_ack_irq();
  287. inc_irq_stat(kvm_posted_intr_wakeup_ipis);
  288. kvm_posted_intr_wakeup_handler();
  289. exiting_irq();
  290. set_irq_regs(old_regs);
  291. }
  292. /*
  293. * Handler for POSTED_INTERRUPT_NESTED_VECTOR.
  294. */
  295. __visible void smp_kvm_posted_intr_nested_ipi(struct pt_regs *regs)
  296. {
  297. struct pt_regs *old_regs = set_irq_regs(regs);
  298. entering_ack_irq();
  299. inc_irq_stat(kvm_posted_intr_nested_ipis);
  300. exiting_irq();
  301. set_irq_regs(old_regs);
  302. }
  303. #endif
  304. #ifdef CONFIG_HOTPLUG_CPU
  305. /* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
  306. void fixup_irqs(void)
  307. {
  308. unsigned int irr, vector;
  309. struct irq_desc *desc;
  310. struct irq_data *data;
  311. struct irq_chip *chip;
  312. irq_migrate_all_off_this_cpu();
  313. /*
  314. * We can remove mdelay() and then send spuriuous interrupts to
  315. * new cpu targets for all the irqs that were handled previously by
  316. * this cpu. While it works, I have seen spurious interrupt messages
  317. * (nothing wrong but still...).
  318. *
  319. * So for now, retain mdelay(1) and check the IRR and then send those
  320. * interrupts to new targets as this cpu is already offlined...
  321. */
  322. mdelay(1);
  323. /*
  324. * We can walk the vector array of this cpu without holding
  325. * vector_lock because the cpu is already marked !online, so
  326. * nothing else will touch it.
  327. */
  328. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  329. if (IS_ERR_OR_NULL(__this_cpu_read(vector_irq[vector])))
  330. continue;
  331. irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
  332. if (irr & (1 << (vector % 32))) {
  333. desc = __this_cpu_read(vector_irq[vector]);
  334. raw_spin_lock(&desc->lock);
  335. data = irq_desc_get_irq_data(desc);
  336. chip = irq_data_get_irq_chip(data);
  337. if (chip->irq_retrigger) {
  338. chip->irq_retrigger(data);
  339. __this_cpu_write(vector_irq[vector], VECTOR_RETRIGGERED);
  340. }
  341. raw_spin_unlock(&desc->lock);
  342. }
  343. if (__this_cpu_read(vector_irq[vector]) != VECTOR_RETRIGGERED)
  344. __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
  345. }
  346. }
  347. #endif