irq.c 11 KB

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