irq.c 11 KB

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