irq.c 12 KB

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