irq.c 13 KB

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