irq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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/idle.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. /* Function pointer for generic interrupt vector handling */
  28. void (*x86_platform_ipi_callback)(void) = NULL;
  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. #endif
  81. if (x86_platform_ipi_callback) {
  82. seq_printf(p, "%*s: ", prec, "PLT");
  83. for_each_online_cpu(j)
  84. seq_printf(p, "%10u ", irq_stats(j)->x86_platform_ipis);
  85. seq_puts(p, " Platform interrupts\n");
  86. }
  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. #if IS_ENABLED(CONFIG_HYPERV) || defined(CONFIG_XEN)
  130. if (test_bit(HYPERVISOR_CALLBACK_VECTOR, used_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. seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
  139. #if defined(CONFIG_X86_IO_APIC)
  140. seq_printf(p, "%*s: %10u\n", prec, "MIS", atomic_read(&irq_mis_count));
  141. #endif
  142. #ifdef CONFIG_HAVE_KVM
  143. seq_printf(p, "%*s: ", prec, "PIN");
  144. for_each_online_cpu(j)
  145. seq_printf(p, "%10u ", irq_stats(j)->kvm_posted_intr_ipis);
  146. seq_puts(p, " Posted-interrupt notification event\n");
  147. seq_printf(p, "%*s: ", prec, "PIW");
  148. for_each_online_cpu(j)
  149. seq_printf(p, "%10u ",
  150. irq_stats(j)->kvm_posted_intr_wakeup_ipis);
  151. seq_puts(p, " Posted-interrupt wakeup event\n");
  152. #endif
  153. return 0;
  154. }
  155. /*
  156. * /proc/stat helpers
  157. */
  158. u64 arch_irq_stat_cpu(unsigned int cpu)
  159. {
  160. u64 sum = irq_stats(cpu)->__nmi_count;
  161. #ifdef CONFIG_X86_LOCAL_APIC
  162. sum += irq_stats(cpu)->apic_timer_irqs;
  163. sum += irq_stats(cpu)->irq_spurious_count;
  164. sum += irq_stats(cpu)->apic_perf_irqs;
  165. sum += irq_stats(cpu)->apic_irq_work_irqs;
  166. sum += irq_stats(cpu)->icr_read_retry_count;
  167. #endif
  168. if (x86_platform_ipi_callback)
  169. sum += irq_stats(cpu)->x86_platform_ipis;
  170. #ifdef CONFIG_SMP
  171. sum += irq_stats(cpu)->irq_resched_count;
  172. sum += irq_stats(cpu)->irq_call_count;
  173. #endif
  174. #ifdef CONFIG_X86_THERMAL_VECTOR
  175. sum += irq_stats(cpu)->irq_thermal_count;
  176. #endif
  177. #ifdef CONFIG_X86_MCE_THRESHOLD
  178. sum += irq_stats(cpu)->irq_threshold_count;
  179. #endif
  180. #ifdef CONFIG_X86_MCE
  181. sum += per_cpu(mce_exception_count, cpu);
  182. sum += per_cpu(mce_poll_count, cpu);
  183. #endif
  184. return sum;
  185. }
  186. u64 arch_irq_stat(void)
  187. {
  188. u64 sum = atomic_read(&irq_err_count);
  189. return sum;
  190. }
  191. /*
  192. * do_IRQ handles all normal device IRQ's (the special
  193. * SMP cross-CPU interrupts have their own specific
  194. * handlers).
  195. */
  196. __visible unsigned int __irq_entry do_IRQ(struct pt_regs *regs)
  197. {
  198. struct pt_regs *old_regs = set_irq_regs(regs);
  199. struct irq_desc * desc;
  200. /* high bit used in ret_from_ code */
  201. unsigned vector = ~regs->orig_ax;
  202. /*
  203. * NB: Unlike exception entries, IRQ entries do not reliably
  204. * handle context tracking in the low-level entry code. This is
  205. * because syscall entries execute briefly with IRQs on before
  206. * updating context tracking state, so we can take an IRQ from
  207. * kernel mode with CONTEXT_USER. The low-level entry code only
  208. * updates the context if we came from user mode, so we won't
  209. * switch to CONTEXT_KERNEL. We'll fix that once the syscall
  210. * code is cleaned up enough that we can cleanly defer enabling
  211. * IRQs.
  212. */
  213. entering_irq();
  214. /* entering_irq() tells RCU that we're not quiescent. Check it. */
  215. RCU_LOCKDEP_WARN(!rcu_is_watching(), "IRQ failed to wake up RCU");
  216. desc = __this_cpu_read(vector_irq[vector]);
  217. if (!handle_irq(desc, regs)) {
  218. ack_APIC_irq();
  219. if (desc != VECTOR_RETRIGGERED) {
  220. pr_emerg_ratelimited("%s: %d.%d No irq handler for vector\n",
  221. __func__, smp_processor_id(),
  222. vector);
  223. } else {
  224. __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
  225. }
  226. }
  227. exiting_irq();
  228. set_irq_regs(old_regs);
  229. return 1;
  230. }
  231. /*
  232. * Handler for X86_PLATFORM_IPI_VECTOR.
  233. */
  234. void __smp_x86_platform_ipi(void)
  235. {
  236. inc_irq_stat(x86_platform_ipis);
  237. if (x86_platform_ipi_callback)
  238. x86_platform_ipi_callback();
  239. }
  240. __visible void smp_x86_platform_ipi(struct pt_regs *regs)
  241. {
  242. struct pt_regs *old_regs = set_irq_regs(regs);
  243. entering_ack_irq();
  244. __smp_x86_platform_ipi();
  245. exiting_irq();
  246. set_irq_regs(old_regs);
  247. }
  248. #ifdef CONFIG_HAVE_KVM
  249. static void dummy_handler(void) {}
  250. static void (*kvm_posted_intr_wakeup_handler)(void) = dummy_handler;
  251. void kvm_set_posted_intr_wakeup_handler(void (*handler)(void))
  252. {
  253. if (handler)
  254. kvm_posted_intr_wakeup_handler = handler;
  255. else
  256. kvm_posted_intr_wakeup_handler = dummy_handler;
  257. }
  258. EXPORT_SYMBOL_GPL(kvm_set_posted_intr_wakeup_handler);
  259. /*
  260. * Handler for POSTED_INTERRUPT_VECTOR.
  261. */
  262. __visible void smp_kvm_posted_intr_ipi(struct pt_regs *regs)
  263. {
  264. struct pt_regs *old_regs = set_irq_regs(regs);
  265. entering_ack_irq();
  266. inc_irq_stat(kvm_posted_intr_ipis);
  267. exiting_irq();
  268. set_irq_regs(old_regs);
  269. }
  270. /*
  271. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  272. */
  273. __visible void smp_kvm_posted_intr_wakeup_ipi(struct pt_regs *regs)
  274. {
  275. struct pt_regs *old_regs = set_irq_regs(regs);
  276. entering_ack_irq();
  277. inc_irq_stat(kvm_posted_intr_wakeup_ipis);
  278. kvm_posted_intr_wakeup_handler();
  279. exiting_irq();
  280. set_irq_regs(old_regs);
  281. }
  282. #endif
  283. __visible void smp_trace_x86_platform_ipi(struct pt_regs *regs)
  284. {
  285. struct pt_regs *old_regs = set_irq_regs(regs);
  286. entering_ack_irq();
  287. trace_x86_platform_ipi_entry(X86_PLATFORM_IPI_VECTOR);
  288. __smp_x86_platform_ipi();
  289. trace_x86_platform_ipi_exit(X86_PLATFORM_IPI_VECTOR);
  290. exiting_irq();
  291. set_irq_regs(old_regs);
  292. }
  293. EXPORT_SYMBOL_GPL(vector_used_by_percpu_irq);
  294. #ifdef CONFIG_HOTPLUG_CPU
  295. /* These two declarations are only used in check_irq_vectors_for_cpu_disable()
  296. * below, which is protected by stop_machine(). Putting them on the stack
  297. * results in a stack frame overflow. Dynamically allocating could result in a
  298. * failure so declare these two cpumasks as global.
  299. */
  300. static struct cpumask affinity_new, online_new;
  301. /*
  302. * This cpu is going to be removed and its vectors migrated to the remaining
  303. * online cpus. Check to see if there are enough vectors in the remaining cpus.
  304. * This function is protected by stop_machine().
  305. */
  306. int check_irq_vectors_for_cpu_disable(void)
  307. {
  308. unsigned int this_cpu, vector, this_count, count;
  309. struct irq_desc *desc;
  310. struct irq_data *data;
  311. int cpu;
  312. this_cpu = smp_processor_id();
  313. cpumask_copy(&online_new, cpu_online_mask);
  314. cpumask_clear_cpu(this_cpu, &online_new);
  315. this_count = 0;
  316. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  317. desc = __this_cpu_read(vector_irq[vector]);
  318. if (IS_ERR_OR_NULL(desc))
  319. continue;
  320. /*
  321. * Protect against concurrent action removal, affinity
  322. * changes etc.
  323. */
  324. raw_spin_lock(&desc->lock);
  325. data = irq_desc_get_irq_data(desc);
  326. cpumask_copy(&affinity_new,
  327. irq_data_get_affinity_mask(data));
  328. cpumask_clear_cpu(this_cpu, &affinity_new);
  329. /* Do not count inactive or per-cpu irqs. */
  330. if (!irq_desc_has_action(desc) || irqd_is_per_cpu(data)) {
  331. raw_spin_unlock(&desc->lock);
  332. continue;
  333. }
  334. raw_spin_unlock(&desc->lock);
  335. /*
  336. * A single irq may be mapped to multiple cpu's
  337. * vector_irq[] (for example IOAPIC cluster mode). In
  338. * this case we have two possibilities:
  339. *
  340. * 1) the resulting affinity mask is empty; that is
  341. * this the down'd cpu is the last cpu in the irq's
  342. * affinity mask, or
  343. *
  344. * 2) the resulting affinity mask is no longer a
  345. * subset of the online cpus but the affinity mask is
  346. * not zero; that is the down'd cpu is the last online
  347. * cpu in a user set affinity mask.
  348. */
  349. if (cpumask_empty(&affinity_new) ||
  350. !cpumask_subset(&affinity_new, &online_new))
  351. this_count++;
  352. }
  353. count = 0;
  354. for_each_online_cpu(cpu) {
  355. if (cpu == this_cpu)
  356. continue;
  357. /*
  358. * We scan from FIRST_EXTERNAL_VECTOR to first system
  359. * vector. If the vector is marked in the used vectors
  360. * bitmap or an irq is assigned to it, we don't count
  361. * it as available.
  362. *
  363. * As this is an inaccurate snapshot anyway, we can do
  364. * this w/o holding vector_lock.
  365. */
  366. for (vector = FIRST_EXTERNAL_VECTOR;
  367. vector < first_system_vector; vector++) {
  368. if (!test_bit(vector, used_vectors) &&
  369. IS_ERR_OR_NULL(per_cpu(vector_irq, cpu)[vector]))
  370. count++;
  371. }
  372. }
  373. if (count < this_count) {
  374. pr_warn("CPU %d disable failed: CPU has %u vectors assigned and there are only %u available.\n",
  375. this_cpu, this_count, count);
  376. return -ERANGE;
  377. }
  378. return 0;
  379. }
  380. /* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
  381. void fixup_irqs(void)
  382. {
  383. unsigned int irq, vector;
  384. static int warned;
  385. struct irq_desc *desc;
  386. struct irq_data *data;
  387. struct irq_chip *chip;
  388. int ret;
  389. for_each_irq_desc(irq, desc) {
  390. int break_affinity = 0;
  391. int set_affinity = 1;
  392. const struct cpumask *affinity;
  393. if (!desc)
  394. continue;
  395. if (irq == 2)
  396. continue;
  397. /* interrupt's are disabled at this point */
  398. raw_spin_lock(&desc->lock);
  399. data = irq_desc_get_irq_data(desc);
  400. affinity = irq_data_get_affinity_mask(data);
  401. if (!irq_has_action(irq) || irqd_is_per_cpu(data) ||
  402. cpumask_subset(affinity, cpu_online_mask)) {
  403. raw_spin_unlock(&desc->lock);
  404. continue;
  405. }
  406. /*
  407. * Complete the irq move. This cpu is going down and for
  408. * non intr-remapping case, we can't wait till this interrupt
  409. * arrives at this cpu before completing the irq move.
  410. */
  411. irq_force_complete_move(desc);
  412. if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) {
  413. break_affinity = 1;
  414. affinity = cpu_online_mask;
  415. }
  416. chip = irq_data_get_irq_chip(data);
  417. /*
  418. * The interrupt descriptor might have been cleaned up
  419. * already, but it is not yet removed from the radix tree
  420. */
  421. if (!chip) {
  422. raw_spin_unlock(&desc->lock);
  423. continue;
  424. }
  425. if (!irqd_can_move_in_process_context(data) && chip->irq_mask)
  426. chip->irq_mask(data);
  427. if (chip->irq_set_affinity) {
  428. ret = chip->irq_set_affinity(data, affinity, true);
  429. if (ret == -ENOSPC)
  430. pr_crit("IRQ %d set affinity failed because there are no available vectors. The device assigned to this IRQ is unstable.\n", irq);
  431. } else {
  432. if (!(warned++))
  433. set_affinity = 0;
  434. }
  435. /*
  436. * We unmask if the irq was not marked masked by the
  437. * core code. That respects the lazy irq disable
  438. * behaviour.
  439. */
  440. if (!irqd_can_move_in_process_context(data) &&
  441. !irqd_irq_masked(data) && chip->irq_unmask)
  442. chip->irq_unmask(data);
  443. raw_spin_unlock(&desc->lock);
  444. if (break_affinity && set_affinity)
  445. pr_notice("Broke affinity for irq %i\n", irq);
  446. else if (!set_affinity)
  447. pr_notice("Cannot set affinity for irq %i\n", irq);
  448. }
  449. /*
  450. * We can remove mdelay() and then send spuriuous interrupts to
  451. * new cpu targets for all the irqs that were handled previously by
  452. * this cpu. While it works, I have seen spurious interrupt messages
  453. * (nothing wrong but still...).
  454. *
  455. * So for now, retain mdelay(1) and check the IRR and then send those
  456. * interrupts to new targets as this cpu is already offlined...
  457. */
  458. mdelay(1);
  459. /*
  460. * We can walk the vector array of this cpu without holding
  461. * vector_lock because the cpu is already marked !online, so
  462. * nothing else will touch it.
  463. */
  464. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  465. unsigned int irr;
  466. if (IS_ERR_OR_NULL(__this_cpu_read(vector_irq[vector])))
  467. continue;
  468. irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
  469. if (irr & (1 << (vector % 32))) {
  470. desc = __this_cpu_read(vector_irq[vector]);
  471. raw_spin_lock(&desc->lock);
  472. data = irq_desc_get_irq_data(desc);
  473. chip = irq_data_get_irq_chip(data);
  474. if (chip->irq_retrigger) {
  475. chip->irq_retrigger(data);
  476. __this_cpu_write(vector_irq[vector], VECTOR_RETRIGGERED);
  477. }
  478. raw_spin_unlock(&desc->lock);
  479. }
  480. if (__this_cpu_read(vector_irq[vector]) != VECTOR_RETRIGGERED)
  481. __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
  482. }
  483. }
  484. #endif