irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 <asm/apic.h>
  14. #include <asm/io_apic.h>
  15. #include <asm/irq.h>
  16. #include <asm/mce.h>
  17. #include <asm/hw_irq.h>
  18. #include <asm/desc.h>
  19. #define CREATE_TRACE_POINTS
  20. #include <asm/trace/irq_vectors.h>
  21. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  22. EXPORT_PER_CPU_SYMBOL(irq_stat);
  23. DEFINE_PER_CPU(struct pt_regs *, irq_regs);
  24. EXPORT_PER_CPU_SYMBOL(irq_regs);
  25. atomic_t irq_err_count;
  26. /* Function pointer for generic interrupt vector handling */
  27. void (*x86_platform_ipi_callback)(void) = NULL;
  28. /*
  29. * 'what should we do if we get a hw irq event on an illegal vector'.
  30. * each architecture has to answer this themselves.
  31. */
  32. void ack_bad_irq(unsigned int irq)
  33. {
  34. if (printk_ratelimit())
  35. pr_err("unexpected IRQ trap at vector %02x\n", irq);
  36. /*
  37. * Currently unexpected vectors happen only on SMP and APIC.
  38. * We _must_ ack these because every local APIC has only N
  39. * irq slots per priority level, and a 'hanging, unacked' IRQ
  40. * holds up an irq slot - in excessive cases (when multiple
  41. * unexpected vectors occur) that might lock up the APIC
  42. * completely.
  43. * But only ack when the APIC is enabled -AK
  44. */
  45. ack_APIC_irq();
  46. }
  47. #define irq_stats(x) (&per_cpu(irq_stat, x))
  48. /*
  49. * /proc/interrupts printing for arch specific interrupts
  50. */
  51. int arch_show_interrupts(struct seq_file *p, int prec)
  52. {
  53. int j;
  54. seq_printf(p, "%*s: ", prec, "NMI");
  55. for_each_online_cpu(j)
  56. seq_printf(p, "%10u ", irq_stats(j)->__nmi_count);
  57. seq_puts(p, " Non-maskable interrupts\n");
  58. #ifdef CONFIG_X86_LOCAL_APIC
  59. seq_printf(p, "%*s: ", prec, "LOC");
  60. for_each_online_cpu(j)
  61. seq_printf(p, "%10u ", irq_stats(j)->apic_timer_irqs);
  62. seq_puts(p, " Local timer interrupts\n");
  63. seq_printf(p, "%*s: ", prec, "SPU");
  64. for_each_online_cpu(j)
  65. seq_printf(p, "%10u ", irq_stats(j)->irq_spurious_count);
  66. seq_puts(p, " Spurious interrupts\n");
  67. seq_printf(p, "%*s: ", prec, "PMI");
  68. for_each_online_cpu(j)
  69. seq_printf(p, "%10u ", irq_stats(j)->apic_perf_irqs);
  70. seq_puts(p, " Performance monitoring interrupts\n");
  71. seq_printf(p, "%*s: ", prec, "IWI");
  72. for_each_online_cpu(j)
  73. seq_printf(p, "%10u ", irq_stats(j)->apic_irq_work_irqs);
  74. seq_puts(p, " IRQ work interrupts\n");
  75. seq_printf(p, "%*s: ", prec, "RTR");
  76. for_each_online_cpu(j)
  77. seq_printf(p, "%10u ", irq_stats(j)->icr_read_retry_count);
  78. seq_puts(p, " APIC ICR read retries\n");
  79. #endif
  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. #ifdef CONFIG_SMP
  87. seq_printf(p, "%*s: ", prec, "RES");
  88. for_each_online_cpu(j)
  89. seq_printf(p, "%10u ", irq_stats(j)->irq_resched_count);
  90. seq_puts(p, " Rescheduling interrupts\n");
  91. seq_printf(p, "%*s: ", prec, "CAL");
  92. for_each_online_cpu(j)
  93. seq_printf(p, "%10u ", irq_stats(j)->irq_call_count);
  94. seq_puts(p, " Function call interrupts\n");
  95. seq_printf(p, "%*s: ", prec, "TLB");
  96. for_each_online_cpu(j)
  97. seq_printf(p, "%10u ", irq_stats(j)->irq_tlb_count);
  98. seq_puts(p, " TLB shootdowns\n");
  99. #endif
  100. #ifdef CONFIG_X86_THERMAL_VECTOR
  101. seq_printf(p, "%*s: ", prec, "TRM");
  102. for_each_online_cpu(j)
  103. seq_printf(p, "%10u ", irq_stats(j)->irq_thermal_count);
  104. seq_puts(p, " Thermal event interrupts\n");
  105. #endif
  106. #ifdef CONFIG_X86_MCE_THRESHOLD
  107. seq_printf(p, "%*s: ", prec, "THR");
  108. for_each_online_cpu(j)
  109. seq_printf(p, "%10u ", irq_stats(j)->irq_threshold_count);
  110. seq_puts(p, " Threshold APIC interrupts\n");
  111. #endif
  112. #ifdef CONFIG_X86_MCE_AMD
  113. seq_printf(p, "%*s: ", prec, "DFR");
  114. for_each_online_cpu(j)
  115. seq_printf(p, "%10u ", irq_stats(j)->irq_deferred_error_count);
  116. seq_puts(p, " Deferred Error APIC interrupts\n");
  117. #endif
  118. #ifdef CONFIG_X86_MCE
  119. seq_printf(p, "%*s: ", prec, "MCE");
  120. for_each_online_cpu(j)
  121. seq_printf(p, "%10u ", per_cpu(mce_exception_count, j));
  122. seq_puts(p, " Machine check exceptions\n");
  123. seq_printf(p, "%*s: ", prec, "MCP");
  124. for_each_online_cpu(j)
  125. seq_printf(p, "%10u ", per_cpu(mce_poll_count, j));
  126. seq_puts(p, " Machine check polls\n");
  127. #endif
  128. #if IS_ENABLED(CONFIG_HYPERV) || defined(CONFIG_XEN)
  129. if (test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors)) {
  130. seq_printf(p, "%*s: ", prec, "HYP");
  131. for_each_online_cpu(j)
  132. seq_printf(p, "%10u ",
  133. irq_stats(j)->irq_hv_callback_count);
  134. seq_puts(p, " Hypervisor callback interrupts\n");
  135. }
  136. #endif
  137. seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
  138. #if defined(CONFIG_X86_IO_APIC)
  139. seq_printf(p, "%*s: %10u\n", prec, "MIS", atomic_read(&irq_mis_count));
  140. #endif
  141. #ifdef CONFIG_HAVE_KVM
  142. seq_printf(p, "%*s: ", prec, "PIN");
  143. for_each_online_cpu(j)
  144. seq_printf(p, "%10u ", irq_stats(j)->kvm_posted_intr_ipis);
  145. seq_puts(p, " Posted-interrupt notification event\n");
  146. seq_printf(p, "%*s: ", prec, "PIW");
  147. for_each_online_cpu(j)
  148. seq_printf(p, "%10u ",
  149. irq_stats(j)->kvm_posted_intr_wakeup_ipis);
  150. seq_puts(p, " Posted-interrupt wakeup event\n");
  151. #endif
  152. return 0;
  153. }
  154. /*
  155. * /proc/stat helpers
  156. */
  157. u64 arch_irq_stat_cpu(unsigned int cpu)
  158. {
  159. u64 sum = irq_stats(cpu)->__nmi_count;
  160. #ifdef CONFIG_X86_LOCAL_APIC
  161. sum += irq_stats(cpu)->apic_timer_irqs;
  162. sum += irq_stats(cpu)->irq_spurious_count;
  163. sum += irq_stats(cpu)->apic_perf_irqs;
  164. sum += irq_stats(cpu)->apic_irq_work_irqs;
  165. sum += irq_stats(cpu)->icr_read_retry_count;
  166. #endif
  167. if (x86_platform_ipi_callback)
  168. sum += irq_stats(cpu)->x86_platform_ipis;
  169. #ifdef CONFIG_SMP
  170. sum += irq_stats(cpu)->irq_resched_count;
  171. sum += irq_stats(cpu)->irq_call_count;
  172. #endif
  173. #ifdef CONFIG_X86_THERMAL_VECTOR
  174. sum += irq_stats(cpu)->irq_thermal_count;
  175. #endif
  176. #ifdef CONFIG_X86_MCE_THRESHOLD
  177. sum += irq_stats(cpu)->irq_threshold_count;
  178. #endif
  179. #ifdef CONFIG_X86_MCE
  180. sum += per_cpu(mce_exception_count, cpu);
  181. sum += per_cpu(mce_poll_count, cpu);
  182. #endif
  183. return sum;
  184. }
  185. u64 arch_irq_stat(void)
  186. {
  187. u64 sum = atomic_read(&irq_err_count);
  188. return sum;
  189. }
  190. /*
  191. * do_IRQ handles all normal device IRQ's (the special
  192. * SMP cross-CPU interrupts have their own specific
  193. * handlers).
  194. */
  195. __visible unsigned int __irq_entry do_IRQ(struct pt_regs *regs)
  196. {
  197. struct pt_regs *old_regs = set_irq_regs(regs);
  198. struct irq_desc * desc;
  199. /* high bit used in ret_from_ code */
  200. unsigned vector = ~regs->orig_ax;
  201. /*
  202. * NB: Unlike exception entries, IRQ entries do not reliably
  203. * handle context tracking in the low-level entry code. This is
  204. * because syscall entries execute briefly with IRQs on before
  205. * updating context tracking state, so we can take an IRQ from
  206. * kernel mode with CONTEXT_USER. The low-level entry code only
  207. * updates the context if we came from user mode, so we won't
  208. * switch to CONTEXT_KERNEL. We'll fix that once the syscall
  209. * code is cleaned up enough that we can cleanly defer enabling
  210. * IRQs.
  211. */
  212. entering_irq();
  213. /* entering_irq() tells RCU that we're not quiescent. Check it. */
  214. RCU_LOCKDEP_WARN(!rcu_is_watching(), "IRQ failed to wake up RCU");
  215. desc = __this_cpu_read(vector_irq[vector]);
  216. if (!handle_irq(desc, regs)) {
  217. ack_APIC_irq();
  218. if (desc != VECTOR_RETRIGGERED) {
  219. pr_emerg_ratelimited("%s: %d.%d No irq handler for vector\n",
  220. __func__, smp_processor_id(),
  221. vector);
  222. } else {
  223. __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
  224. }
  225. }
  226. exiting_irq();
  227. set_irq_regs(old_regs);
  228. return 1;
  229. }
  230. /*
  231. * Handler for X86_PLATFORM_IPI_VECTOR.
  232. */
  233. void __smp_x86_platform_ipi(void)
  234. {
  235. inc_irq_stat(x86_platform_ipis);
  236. if (x86_platform_ipi_callback)
  237. x86_platform_ipi_callback();
  238. }
  239. __visible void __irq_entry smp_x86_platform_ipi(struct pt_regs *regs)
  240. {
  241. struct pt_regs *old_regs = set_irq_regs(regs);
  242. entering_ack_irq();
  243. __smp_x86_platform_ipi();
  244. exiting_irq();
  245. set_irq_regs(old_regs);
  246. }
  247. #ifdef CONFIG_HAVE_KVM
  248. static void dummy_handler(void) {}
  249. static void (*kvm_posted_intr_wakeup_handler)(void) = dummy_handler;
  250. void kvm_set_posted_intr_wakeup_handler(void (*handler)(void))
  251. {
  252. if (handler)
  253. kvm_posted_intr_wakeup_handler = handler;
  254. else
  255. kvm_posted_intr_wakeup_handler = dummy_handler;
  256. }
  257. EXPORT_SYMBOL_GPL(kvm_set_posted_intr_wakeup_handler);
  258. /*
  259. * Handler for POSTED_INTERRUPT_VECTOR.
  260. */
  261. __visible void smp_kvm_posted_intr_ipi(struct pt_regs *regs)
  262. {
  263. struct pt_regs *old_regs = set_irq_regs(regs);
  264. entering_ack_irq();
  265. inc_irq_stat(kvm_posted_intr_ipis);
  266. exiting_irq();
  267. set_irq_regs(old_regs);
  268. }
  269. /*
  270. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  271. */
  272. __visible void smp_kvm_posted_intr_wakeup_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_wakeup_ipis);
  277. kvm_posted_intr_wakeup_handler();
  278. exiting_irq();
  279. set_irq_regs(old_regs);
  280. }
  281. #endif
  282. __visible void __irq_entry smp_trace_x86_platform_ipi(struct pt_regs *regs)
  283. {
  284. struct pt_regs *old_regs = set_irq_regs(regs);
  285. entering_ack_irq();
  286. trace_x86_platform_ipi_entry(X86_PLATFORM_IPI_VECTOR);
  287. __smp_x86_platform_ipi();
  288. trace_x86_platform_ipi_exit(X86_PLATFORM_IPI_VECTOR);
  289. exiting_irq();
  290. set_irq_regs(old_regs);
  291. }
  292. EXPORT_SYMBOL_GPL(vector_used_by_percpu_irq);
  293. #ifdef CONFIG_HOTPLUG_CPU
  294. /* These two declarations are only used in check_irq_vectors_for_cpu_disable()
  295. * below, which is protected by stop_machine(). Putting them on the stack
  296. * results in a stack frame overflow. Dynamically allocating could result in a
  297. * failure so declare these two cpumasks as global.
  298. */
  299. static struct cpumask affinity_new, online_new;
  300. /*
  301. * This cpu is going to be removed and its vectors migrated to the remaining
  302. * online cpus. Check to see if there are enough vectors in the remaining cpus.
  303. * This function is protected by stop_machine().
  304. */
  305. int check_irq_vectors_for_cpu_disable(void)
  306. {
  307. unsigned int this_cpu, vector, this_count, count;
  308. struct irq_desc *desc;
  309. struct irq_data *data;
  310. int cpu;
  311. this_cpu = smp_processor_id();
  312. cpumask_copy(&online_new, cpu_online_mask);
  313. cpumask_clear_cpu(this_cpu, &online_new);
  314. this_count = 0;
  315. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  316. desc = __this_cpu_read(vector_irq[vector]);
  317. if (IS_ERR_OR_NULL(desc))
  318. continue;
  319. /*
  320. * Protect against concurrent action removal, affinity
  321. * changes etc.
  322. */
  323. raw_spin_lock(&desc->lock);
  324. data = irq_desc_get_irq_data(desc);
  325. cpumask_copy(&affinity_new,
  326. irq_data_get_affinity_mask(data));
  327. cpumask_clear_cpu(this_cpu, &affinity_new);
  328. /* Do not count inactive or per-cpu irqs. */
  329. if (!irq_desc_has_action(desc) || irqd_is_per_cpu(data)) {
  330. raw_spin_unlock(&desc->lock);
  331. continue;
  332. }
  333. raw_spin_unlock(&desc->lock);
  334. /*
  335. * A single irq may be mapped to multiple cpu's
  336. * vector_irq[] (for example IOAPIC cluster mode). In
  337. * this case we have two possibilities:
  338. *
  339. * 1) the resulting affinity mask is empty; that is
  340. * this the down'd cpu is the last cpu in the irq's
  341. * affinity mask, or
  342. *
  343. * 2) the resulting affinity mask is no longer a
  344. * subset of the online cpus but the affinity mask is
  345. * not zero; that is the down'd cpu is the last online
  346. * cpu in a user set affinity mask.
  347. */
  348. if (cpumask_empty(&affinity_new) ||
  349. !cpumask_subset(&affinity_new, &online_new))
  350. this_count++;
  351. }
  352. /* No need to check any further. */
  353. if (!this_count)
  354. return 0;
  355. count = 0;
  356. for_each_online_cpu(cpu) {
  357. if (cpu == this_cpu)
  358. continue;
  359. /*
  360. * We scan from FIRST_EXTERNAL_VECTOR to first system
  361. * vector. If the vector is marked in the used vectors
  362. * bitmap or an irq is assigned to it, we don't count
  363. * it as available.
  364. *
  365. * As this is an inaccurate snapshot anyway, we can do
  366. * this w/o holding vector_lock.
  367. */
  368. for (vector = FIRST_EXTERNAL_VECTOR;
  369. vector < first_system_vector; vector++) {
  370. if (!test_bit(vector, used_vectors) &&
  371. IS_ERR_OR_NULL(per_cpu(vector_irq, cpu)[vector])) {
  372. if (++count == this_count)
  373. return 0;
  374. }
  375. }
  376. }
  377. if (count < this_count) {
  378. pr_warn("CPU %d disable failed: CPU has %u vectors assigned and there are only %u available.\n",
  379. this_cpu, this_count, count);
  380. return -ERANGE;
  381. }
  382. return 0;
  383. }
  384. /* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
  385. void fixup_irqs(void)
  386. {
  387. unsigned int irr, vector;
  388. struct irq_desc *desc;
  389. struct irq_data *data;
  390. struct irq_chip *chip;
  391. irq_migrate_all_off_this_cpu();
  392. /*
  393. * We can remove mdelay() and then send spuriuous interrupts to
  394. * new cpu targets for all the irqs that were handled previously by
  395. * this cpu. While it works, I have seen spurious interrupt messages
  396. * (nothing wrong but still...).
  397. *
  398. * So for now, retain mdelay(1) and check the IRR and then send those
  399. * interrupts to new targets as this cpu is already offlined...
  400. */
  401. mdelay(1);
  402. /*
  403. * We can walk the vector array of this cpu without holding
  404. * vector_lock because the cpu is already marked !online, so
  405. * nothing else will touch it.
  406. */
  407. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  408. if (IS_ERR_OR_NULL(__this_cpu_read(vector_irq[vector])))
  409. continue;
  410. irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
  411. if (irr & (1 << (vector % 32))) {
  412. desc = __this_cpu_read(vector_irq[vector]);
  413. raw_spin_lock(&desc->lock);
  414. data = irq_desc_get_irq_data(desc);
  415. chip = irq_data_get_irq_chip(data);
  416. if (chip->irq_retrigger) {
  417. chip->irq_retrigger(data);
  418. __this_cpu_write(vector_irq[vector], VECTOR_RETRIGGERED);
  419. }
  420. raw_spin_unlock(&desc->lock);
  421. }
  422. if (__this_cpu_read(vector_irq[vector]) != VECTOR_RETRIGGERED)
  423. __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
  424. }
  425. }
  426. #endif