irq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/sh/kernel/irq.c
  4. *
  5. * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
  6. *
  7. *
  8. * SuperH version: Copyright (C) 1999 Niibe Yutaka
  9. */
  10. #include <linux/irq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/delay.h>
  17. #include <linux/ratelimit.h>
  18. #include <asm/processor.h>
  19. #include <asm/machvec.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/thread_info.h>
  22. #include <cpu/mmu_context.h>
  23. #include <asm/softirq_stack.h>
  24. atomic_t irq_err_count;
  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, it doesn't deserve
  28. * a generic callback i think.
  29. */
  30. void ack_bad_irq(unsigned int irq)
  31. {
  32. atomic_inc(&irq_err_count);
  33. printk("unexpected IRQ trap at vector %02x\n", irq);
  34. }
  35. #if defined(CONFIG_PROC_FS)
  36. /*
  37. * /proc/interrupts printing for arch specific interrupts
  38. */
  39. int arch_show_interrupts(struct seq_file *p, int prec)
  40. {
  41. int j;
  42. seq_printf(p, "%*s: ", prec, "NMI");
  43. for_each_online_cpu(j)
  44. seq_printf(p, "%10u ", per_cpu(irq_stat.__nmi_count, j));
  45. seq_printf(p, " Non-maskable interrupts\n");
  46. seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
  47. return 0;
  48. }
  49. #endif
  50. #ifdef CONFIG_IRQSTACKS
  51. /*
  52. * per-CPU IRQ handling contexts (thread information and stack)
  53. */
  54. union irq_ctx {
  55. struct thread_info tinfo;
  56. u32 stack[THREAD_SIZE/sizeof(u32)];
  57. };
  58. static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
  59. static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
  60. static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  61. static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  62. static inline void handle_one_irq(unsigned int irq)
  63. {
  64. union irq_ctx *curctx, *irqctx;
  65. curctx = (union irq_ctx *)current_thread_info();
  66. irqctx = hardirq_ctx[smp_processor_id()];
  67. /*
  68. * this is where we switch to the IRQ stack. However, if we are
  69. * already using the IRQ stack (because we interrupted a hardirq
  70. * handler) we can't do that and just have to keep using the
  71. * current stack (which is the irq stack already after all)
  72. */
  73. if (curctx != irqctx) {
  74. u32 *isp;
  75. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  76. irqctx->tinfo.task = curctx->tinfo.task;
  77. irqctx->tinfo.previous_sp = current_stack_pointer;
  78. /*
  79. * Copy the softirq bits in preempt_count so that the
  80. * softirq checks work in the hardirq context.
  81. */
  82. irqctx->tinfo.preempt_count =
  83. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  84. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  85. __asm__ __volatile__ (
  86. "mov %0, r4 \n"
  87. "mov r15, r8 \n"
  88. "jsr @%1 \n"
  89. /* switch to the irq stack */
  90. " mov %2, r15 \n"
  91. /* restore the stack (ring zero) */
  92. "mov r8, r15 \n"
  93. : /* no outputs */
  94. : "r" (irq), "r" (generic_handle_irq), "r" (isp)
  95. : "memory", "r0", "r1", "r2", "r3", "r4",
  96. "r5", "r6", "r7", "r8", "t", "pr"
  97. );
  98. } else
  99. generic_handle_irq(irq);
  100. }
  101. /*
  102. * allocate per-cpu stacks for hardirq and for softirq processing
  103. */
  104. void irq_ctx_init(int cpu)
  105. {
  106. union irq_ctx *irqctx;
  107. if (hardirq_ctx[cpu])
  108. return;
  109. irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
  110. irqctx->tinfo.task = NULL;
  111. irqctx->tinfo.cpu = cpu;
  112. irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
  113. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  114. hardirq_ctx[cpu] = irqctx;
  115. irqctx = (union irq_ctx *)&softirq_stack[cpu * THREAD_SIZE];
  116. irqctx->tinfo.task = NULL;
  117. irqctx->tinfo.cpu = cpu;
  118. irqctx->tinfo.preempt_count = 0;
  119. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  120. softirq_ctx[cpu] = irqctx;
  121. printk("CPU %u irqstacks, hard=%p soft=%p\n",
  122. cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
  123. }
  124. void irq_ctx_exit(int cpu)
  125. {
  126. hardirq_ctx[cpu] = NULL;
  127. }
  128. #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK
  129. void do_softirq_own_stack(void)
  130. {
  131. struct thread_info *curctx;
  132. union irq_ctx *irqctx;
  133. u32 *isp;
  134. curctx = current_thread_info();
  135. irqctx = softirq_ctx[smp_processor_id()];
  136. irqctx->tinfo.task = curctx->task;
  137. irqctx->tinfo.previous_sp = current_stack_pointer;
  138. /* build the stack frame on the softirq stack */
  139. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  140. __asm__ __volatile__ (
  141. "mov r15, r9 \n"
  142. "jsr @%0 \n"
  143. /* switch to the softirq stack */
  144. " mov %1, r15 \n"
  145. /* restore the thread stack */
  146. "mov r9, r15 \n"
  147. : /* no outputs */
  148. : "r" (__do_softirq), "r" (isp)
  149. : "memory", "r0", "r1", "r2", "r3", "r4",
  150. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  151. );
  152. }
  153. #endif
  154. #else
  155. static inline void handle_one_irq(unsigned int irq)
  156. {
  157. generic_handle_irq(irq);
  158. }
  159. #endif
  160. asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
  161. {
  162. struct pt_regs *old_regs = set_irq_regs(regs);
  163. irq_enter();
  164. irq = irq_demux(irq_lookup(irq));
  165. if (irq != NO_IRQ_IGNORE) {
  166. handle_one_irq(irq);
  167. irq_finish(irq);
  168. }
  169. irq_exit();
  170. set_irq_regs(old_regs);
  171. return IRQ_HANDLED;
  172. }
  173. void __init init_IRQ(void)
  174. {
  175. plat_irq_setup();
  176. /* Perform the machine specific initialisation */
  177. if (sh_mv.mv_init_irq)
  178. sh_mv.mv_init_irq();
  179. intc_finalize();
  180. irq_ctx_init(smp_processor_id());
  181. }
  182. #ifdef CONFIG_HOTPLUG_CPU
  183. /*
  184. * The CPU has been marked offline. Migrate IRQs off this CPU. If
  185. * the affinity settings do not allow other CPUs, force them onto any
  186. * available CPU.
  187. */
  188. void migrate_irqs(void)
  189. {
  190. unsigned int irq, cpu = smp_processor_id();
  191. for_each_active_irq(irq) {
  192. struct irq_data *data = irq_get_irq_data(irq);
  193. if (irq_data_get_node(data) == cpu) {
  194. const struct cpumask *mask = irq_data_get_affinity_mask(data);
  195. unsigned int newcpu = cpumask_any_and(mask,
  196. cpu_online_mask);
  197. if (newcpu >= nr_cpu_ids) {
  198. pr_info_ratelimited("IRQ%u no longer affine to CPU%u\n",
  199. irq, cpu);
  200. irq_set_affinity(irq, cpu_all_mask);
  201. } else {
  202. irq_set_affinity(irq, mask);
  203. }
  204. }
  205. }
  206. }
  207. #endif