irq.c 15 KB

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