irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2004, 2011
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  5. * Holger Smolinski <Holger.Smolinski@de.ibm.com>,
  6. * Thomas Spatzier <tspat@de.ibm.com>,
  7. *
  8. * This file contains interrupt related functions.
  9. */
  10. #include <linux/kernel_stat.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/profile.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/ftrace.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/cpu.h>
  22. #include <linux/irq.h>
  23. #include <linux/entry-common.h>
  24. #include <asm/irq_regs.h>
  25. #include <asm/cputime.h>
  26. #include <asm/lowcore.h>
  27. #include <asm/irq.h>
  28. #include <asm/hw_irq.h>
  29. #include <asm/stacktrace.h>
  30. #include <asm/softirq_stack.h>
  31. #include <asm/vtime.h>
  32. #include "entry.h"
  33. DEFINE_PER_CPU_SHARED_ALIGNED(struct irq_stat, irq_stat);
  34. EXPORT_PER_CPU_SYMBOL_GPL(irq_stat);
  35. struct irq_class {
  36. int irq;
  37. char *name;
  38. char *desc;
  39. };
  40. /*
  41. * The list of "main" irq classes on s390. This is the list of interrupts
  42. * that appear both in /proc/stat ("intr" line) and /proc/interrupts.
  43. * Historically only external and I/O interrupts have been part of /proc/stat.
  44. * We can't add the split external and I/O sub classes since the first field
  45. * in the "intr" line in /proc/stat is supposed to be the sum of all other
  46. * fields.
  47. * Since the external and I/O interrupt fields are already sums we would end
  48. * up with having a sum which accounts each interrupt twice.
  49. */
  50. static const struct irq_class irqclass_main_desc[NR_IRQS_BASE] = {
  51. {.irq = EXT_INTERRUPT, .name = "EXT"},
  52. {.irq = IO_INTERRUPT, .name = "I/O"},
  53. {.irq = THIN_INTERRUPT, .name = "AIO"},
  54. };
  55. /*
  56. * The list of split external and I/O interrupts that appear only in
  57. * /proc/interrupts.
  58. * In addition this list contains non external / I/O events like NMIs.
  59. */
  60. static const struct irq_class irqclass_sub_desc[] = {
  61. {.irq = IRQEXT_CLK, .name = "CLK", .desc = "[EXT] Clock Comparator"},
  62. {.irq = IRQEXT_EXC, .name = "EXC", .desc = "[EXT] External Call"},
  63. {.irq = IRQEXT_EMS, .name = "EMS", .desc = "[EXT] Emergency Signal"},
  64. {.irq = IRQEXT_TMR, .name = "TMR", .desc = "[EXT] CPU Timer"},
  65. {.irq = IRQEXT_TLA, .name = "TAL", .desc = "[EXT] Timing Alert"},
  66. {.irq = IRQEXT_PFL, .name = "PFL", .desc = "[EXT] Pseudo Page Fault"},
  67. {.irq = IRQEXT_DSD, .name = "DSD", .desc = "[EXT] DASD Diag"},
  68. {.irq = IRQEXT_VRT, .name = "VRT", .desc = "[EXT] Virtio"},
  69. {.irq = IRQEXT_SCP, .name = "SCP", .desc = "[EXT] Service Call"},
  70. {.irq = IRQEXT_IUC, .name = "IUC", .desc = "[EXT] IUCV"},
  71. {.irq = IRQEXT_CMS, .name = "CMS", .desc = "[EXT] CPU-Measurement: Sampling"},
  72. {.irq = IRQEXT_CMC, .name = "CMC", .desc = "[EXT] CPU-Measurement: Counter"},
  73. {.irq = IRQEXT_FTP, .name = "FTP", .desc = "[EXT] HMC FTP Service"},
  74. {.irq = IRQEXT_WTI, .name = "WTI", .desc = "[EXT] Warning Track"},
  75. {.irq = IRQIO_CIO, .name = "CIO", .desc = "[I/O] Common I/O Layer Interrupt"},
  76. {.irq = IRQIO_DAS, .name = "DAS", .desc = "[I/O] DASD"},
  77. {.irq = IRQIO_C15, .name = "C15", .desc = "[I/O] 3215"},
  78. {.irq = IRQIO_C70, .name = "C70", .desc = "[I/O] 3270"},
  79. {.irq = IRQIO_TAP, .name = "TAP", .desc = "[I/O] Tape"},
  80. {.irq = IRQIO_VMR, .name = "VMR", .desc = "[I/O] Unit Record Devices"},
  81. {.irq = IRQIO_LCS, .name = "LCS", .desc = "[I/O] LCS"},
  82. {.irq = IRQIO_CTC, .name = "CTC", .desc = "[I/O] CTC"},
  83. {.irq = IRQIO_ADM, .name = "ADM", .desc = "[I/O] EADM Subchannel"},
  84. {.irq = IRQIO_CSC, .name = "CSC", .desc = "[I/O] CHSC Subchannel"},
  85. {.irq = IRQIO_VIR, .name = "VIR", .desc = "[I/O] Virtual I/O Devices"},
  86. {.irq = IRQIO_QAI, .name = "QAI", .desc = "[AIO] QDIO Adapter Interrupt"},
  87. {.irq = IRQIO_APB, .name = "APB", .desc = "[AIO] AP Bus"},
  88. {.irq = IRQIO_PCF, .name = "PCF", .desc = "[AIO] PCI Floating Interrupt"},
  89. {.irq = IRQIO_PCD, .name = "PCD", .desc = "[AIO] PCI Directed Interrupt"},
  90. {.irq = IRQIO_MSI, .name = "MSI", .desc = "[AIO] MSI Interrupt"},
  91. {.irq = IRQIO_VAI, .name = "VAI", .desc = "[AIO] Virtual I/O Devices AI"},
  92. {.irq = IRQIO_GAL, .name = "GAL", .desc = "[AIO] GIB Alert"},
  93. {.irq = NMI_NMI, .name = "NMI", .desc = "[NMI] Machine Check"},
  94. {.irq = CPU_RST, .name = "RST", .desc = "[CPU] CPU Restart"},
  95. };
  96. static void do_IRQ(struct pt_regs *regs, int irq)
  97. {
  98. if (tod_after_eq(get_lowcore()->int_clock,
  99. get_lowcore()->clock_comparator))
  100. /* Serve timer interrupts first. */
  101. clock_comparator_work();
  102. generic_handle_irq(irq);
  103. }
  104. static int on_async_stack(void)
  105. {
  106. unsigned long frame = current_frame_address();
  107. return ((get_lowcore()->async_stack ^ frame) & ~(THREAD_SIZE - 1)) == 0;
  108. }
  109. static void do_irq_async(struct pt_regs *regs, int irq)
  110. {
  111. if (on_async_stack()) {
  112. do_IRQ(regs, irq);
  113. } else {
  114. call_on_stack(2, get_lowcore()->async_stack, void, do_IRQ,
  115. struct pt_regs *, regs, int, irq);
  116. }
  117. }
  118. static int irq_pending(struct pt_regs *regs)
  119. {
  120. int cc;
  121. asm volatile("tpi 0\n"
  122. "ipm %0" : "=d" (cc) : : "cc");
  123. return cc >> 28;
  124. }
  125. void noinstr do_io_irq(struct pt_regs *regs)
  126. {
  127. irqentry_state_t state = irqentry_enter(regs);
  128. struct pt_regs *old_regs = set_irq_regs(regs);
  129. bool from_idle;
  130. irq_enter_rcu();
  131. if (user_mode(regs)) {
  132. update_timer_sys();
  133. if (static_branch_likely(&cpu_has_bear))
  134. current->thread.last_break = regs->last_break;
  135. }
  136. from_idle = test_and_clear_cpu_flag(CIF_ENABLED_WAIT);
  137. if (from_idle)
  138. account_idle_time_irq();
  139. set_cpu_flag(CIF_NOHZ_DELAY);
  140. do {
  141. regs->tpi_info = get_lowcore()->tpi_info;
  142. if (get_lowcore()->tpi_info.adapter_IO)
  143. do_irq_async(regs, THIN_INTERRUPT);
  144. else
  145. do_irq_async(regs, IO_INTERRUPT);
  146. } while (MACHINE_IS_LPAR && irq_pending(regs));
  147. irq_exit_rcu();
  148. set_irq_regs(old_regs);
  149. irqentry_exit(regs, state);
  150. if (from_idle)
  151. regs->psw.mask &= ~(PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_WAIT);
  152. }
  153. void noinstr do_ext_irq(struct pt_regs *regs)
  154. {
  155. irqentry_state_t state = irqentry_enter(regs);
  156. struct pt_regs *old_regs = set_irq_regs(regs);
  157. bool from_idle;
  158. irq_enter_rcu();
  159. if (user_mode(regs)) {
  160. update_timer_sys();
  161. if (static_branch_likely(&cpu_has_bear))
  162. current->thread.last_break = regs->last_break;
  163. }
  164. regs->int_code = get_lowcore()->ext_int_code_addr;
  165. regs->int_parm = get_lowcore()->ext_params;
  166. regs->int_parm_long = get_lowcore()->ext_params2;
  167. from_idle = test_and_clear_cpu_flag(CIF_ENABLED_WAIT);
  168. if (from_idle)
  169. account_idle_time_irq();
  170. do_irq_async(regs, EXT_INTERRUPT);
  171. irq_exit_rcu();
  172. set_irq_regs(old_regs);
  173. irqentry_exit(regs, state);
  174. if (from_idle)
  175. regs->psw.mask &= ~(PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_WAIT);
  176. }
  177. static void show_msi_interrupt(struct seq_file *p, int irq)
  178. {
  179. struct irq_desc *desc;
  180. unsigned long flags;
  181. int cpu;
  182. rcu_read_lock();
  183. desc = irq_to_desc(irq);
  184. if (!desc)
  185. goto out;
  186. raw_spin_lock_irqsave(&desc->lock, flags);
  187. seq_printf(p, "%3d: ", irq);
  188. for_each_online_cpu(cpu)
  189. seq_printf(p, "%10u ", irq_desc_kstat_cpu(desc, cpu));
  190. if (desc->irq_data.chip)
  191. seq_printf(p, " %8s", desc->irq_data.chip->name);
  192. if (desc->action)
  193. seq_printf(p, " %s", desc->action->name);
  194. seq_putc(p, '\n');
  195. raw_spin_unlock_irqrestore(&desc->lock, flags);
  196. out:
  197. rcu_read_unlock();
  198. }
  199. /*
  200. * show_interrupts is needed by /proc/interrupts.
  201. */
  202. int show_interrupts(struct seq_file *p, void *v)
  203. {
  204. int index = *(loff_t *) v;
  205. int cpu, irq;
  206. cpus_read_lock();
  207. if (index == 0) {
  208. seq_puts(p, " ");
  209. for_each_online_cpu(cpu)
  210. seq_printf(p, "CPU%-8d", cpu);
  211. seq_putc(p, '\n');
  212. }
  213. if (index < NR_IRQS_BASE) {
  214. seq_printf(p, "%s: ", irqclass_main_desc[index].name);
  215. irq = irqclass_main_desc[index].irq;
  216. for_each_online_cpu(cpu)
  217. seq_printf(p, "%10u ", kstat_irqs_cpu(irq, cpu));
  218. seq_putc(p, '\n');
  219. goto out;
  220. }
  221. if (index < nr_irqs) {
  222. show_msi_interrupt(p, index);
  223. goto out;
  224. }
  225. for (index = 0; index < NR_ARCH_IRQS; index++) {
  226. seq_printf(p, "%s: ", irqclass_sub_desc[index].name);
  227. irq = irqclass_sub_desc[index].irq;
  228. for_each_online_cpu(cpu)
  229. seq_printf(p, "%10u ",
  230. per_cpu(irq_stat, cpu).irqs[irq]);
  231. if (irqclass_sub_desc[index].desc)
  232. seq_printf(p, " %s", irqclass_sub_desc[index].desc);
  233. seq_putc(p, '\n');
  234. }
  235. out:
  236. cpus_read_unlock();
  237. return 0;
  238. }
  239. unsigned int arch_dynirq_lower_bound(unsigned int from)
  240. {
  241. return from < NR_IRQS_BASE ? NR_IRQS_BASE : from;
  242. }
  243. /*
  244. * ext_int_hash[index] is the list head for all external interrupts that hash
  245. * to this index.
  246. */
  247. static struct hlist_head ext_int_hash[32] ____cacheline_aligned;
  248. struct ext_int_info {
  249. ext_int_handler_t handler;
  250. struct hlist_node entry;
  251. struct rcu_head rcu;
  252. u16 code;
  253. };
  254. /* ext_int_hash_lock protects the handler lists for external interrupts */
  255. static DEFINE_SPINLOCK(ext_int_hash_lock);
  256. static inline int ext_hash(u16 code)
  257. {
  258. BUILD_BUG_ON(!is_power_of_2(ARRAY_SIZE(ext_int_hash)));
  259. return (code + (code >> 9)) & (ARRAY_SIZE(ext_int_hash) - 1);
  260. }
  261. int register_external_irq(u16 code, ext_int_handler_t handler)
  262. {
  263. struct ext_int_info *p;
  264. unsigned long flags;
  265. int index;
  266. p = kmalloc(sizeof(*p), GFP_ATOMIC);
  267. if (!p)
  268. return -ENOMEM;
  269. p->code = code;
  270. p->handler = handler;
  271. index = ext_hash(code);
  272. spin_lock_irqsave(&ext_int_hash_lock, flags);
  273. hlist_add_head_rcu(&p->entry, &ext_int_hash[index]);
  274. spin_unlock_irqrestore(&ext_int_hash_lock, flags);
  275. return 0;
  276. }
  277. EXPORT_SYMBOL(register_external_irq);
  278. int unregister_external_irq(u16 code, ext_int_handler_t handler)
  279. {
  280. struct ext_int_info *p;
  281. unsigned long flags;
  282. int index = ext_hash(code);
  283. spin_lock_irqsave(&ext_int_hash_lock, flags);
  284. hlist_for_each_entry_rcu(p, &ext_int_hash[index], entry) {
  285. if (p->code == code && p->handler == handler) {
  286. hlist_del_rcu(&p->entry);
  287. kfree_rcu(p, rcu);
  288. }
  289. }
  290. spin_unlock_irqrestore(&ext_int_hash_lock, flags);
  291. return 0;
  292. }
  293. EXPORT_SYMBOL(unregister_external_irq);
  294. static irqreturn_t do_ext_interrupt(int irq, void *dummy)
  295. {
  296. struct pt_regs *regs = get_irq_regs();
  297. struct ext_code ext_code;
  298. struct ext_int_info *p;
  299. int index;
  300. ext_code.int_code = regs->int_code;
  301. if (ext_code.code != EXT_IRQ_CLK_COMP)
  302. set_cpu_flag(CIF_NOHZ_DELAY);
  303. index = ext_hash(ext_code.code);
  304. rcu_read_lock();
  305. hlist_for_each_entry_rcu(p, &ext_int_hash[index], entry) {
  306. if (unlikely(p->code != ext_code.code))
  307. continue;
  308. p->handler(ext_code, regs->int_parm, regs->int_parm_long);
  309. }
  310. rcu_read_unlock();
  311. return IRQ_HANDLED;
  312. }
  313. static void __init init_ext_interrupts(void)
  314. {
  315. int idx;
  316. for (idx = 0; idx < ARRAY_SIZE(ext_int_hash); idx++)
  317. INIT_HLIST_HEAD(&ext_int_hash[idx]);
  318. irq_set_chip_and_handler(EXT_INTERRUPT,
  319. &dummy_irq_chip, handle_percpu_irq);
  320. if (request_irq(EXT_INTERRUPT, do_ext_interrupt, 0, "EXT", NULL))
  321. panic("Failed to register EXT interrupt\n");
  322. }
  323. void __init init_IRQ(void)
  324. {
  325. BUILD_BUG_ON(ARRAY_SIZE(irqclass_sub_desc) != NR_ARCH_IRQS);
  326. init_cio_interrupts();
  327. init_airq_interrupts();
  328. init_ext_interrupts();
  329. }
  330. static DEFINE_SPINLOCK(irq_subclass_lock);
  331. static unsigned char irq_subclass_refcount[64];
  332. void irq_subclass_register(enum irq_subclass subclass)
  333. {
  334. spin_lock(&irq_subclass_lock);
  335. if (!irq_subclass_refcount[subclass])
  336. system_ctl_set_bit(0, subclass);
  337. irq_subclass_refcount[subclass]++;
  338. spin_unlock(&irq_subclass_lock);
  339. }
  340. EXPORT_SYMBOL(irq_subclass_register);
  341. void irq_subclass_unregister(enum irq_subclass subclass)
  342. {
  343. spin_lock(&irq_subclass_lock);
  344. irq_subclass_refcount[subclass]--;
  345. if (!irq_subclass_refcount[subclass])
  346. system_ctl_clear_bit(0, subclass);
  347. spin_unlock(&irq_subclass_lock);
  348. }
  349. EXPORT_SYMBOL(irq_subclass_unregister);