irq.c 5.7 KB

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