irq.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * linux/arch/sh/kernel/irq.c
  3. *
  4. * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
  5. *
  6. *
  7. * SuperH version: Copyright (C) 1999 Niibe Yutaka
  8. */
  9. #include <linux/irq.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/ftrace.h>
  15. #include <linux/delay.h>
  16. #include <linux/ratelimit.h>
  17. #include <asm/processor.h>
  18. #include <asm/machvec.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/thread_info.h>
  21. #include <cpu/mmu_context.h>
  22. atomic_t irq_err_count;
  23. /*
  24. * 'what should we do if we get a hw irq event on an illegal vector'.
  25. * each architecture has to answer this themselves, it doesn't deserve
  26. * a generic callback i think.
  27. */
  28. void ack_bad_irq(unsigned int irq)
  29. {
  30. atomic_inc(&irq_err_count);
  31. printk("unexpected IRQ trap at vector %02x\n", irq);
  32. }
  33. #if defined(CONFIG_PROC_FS)
  34. /*
  35. * /proc/interrupts printing for arch specific interrupts
  36. */
  37. int arch_show_interrupts(struct seq_file *p, int prec)
  38. {
  39. int j;
  40. seq_printf(p, "%*s: ", prec, "NMI");
  41. for_each_online_cpu(j)
  42. seq_printf(p, "%10u ", irq_stat[j].__nmi_count);
  43. seq_printf(p, " Non-maskable interrupts\n");
  44. seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
  45. return 0;
  46. }
  47. #endif
  48. #ifdef CONFIG_IRQSTACKS
  49. /*
  50. * per-CPU IRQ handling contexts (thread information and stack)
  51. */
  52. union irq_ctx {
  53. struct thread_info tinfo;
  54. u32 stack[THREAD_SIZE/sizeof(u32)];
  55. };
  56. static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
  57. static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
  58. static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  59. static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  60. static inline void handle_one_irq(unsigned int irq)
  61. {
  62. union irq_ctx *curctx, *irqctx;
  63. curctx = (union irq_ctx *)current_thread_info();
  64. irqctx = hardirq_ctx[smp_processor_id()];
  65. /*
  66. * this is where we switch to the IRQ stack. However, if we are
  67. * already using the IRQ stack (because we interrupted a hardirq
  68. * handler) we can't do that and just have to keep using the
  69. * current stack (which is the irq stack already after all)
  70. */
  71. if (curctx != irqctx) {
  72. u32 *isp;
  73. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  74. irqctx->tinfo.task = curctx->tinfo.task;
  75. irqctx->tinfo.previous_sp = current_stack_pointer;
  76. /*
  77. * Copy the softirq bits in preempt_count so that the
  78. * softirq checks work in the hardirq context.
  79. */
  80. irqctx->tinfo.preempt_count =
  81. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  82. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  83. __asm__ __volatile__ (
  84. "mov %0, r4 \n"
  85. "mov r15, r8 \n"
  86. "jsr @%1 \n"
  87. /* swith to the irq stack */
  88. " mov %2, r15 \n"
  89. /* restore the stack (ring zero) */
  90. "mov r8, r15 \n"
  91. : /* no outputs */
  92. : "r" (irq), "r" (generic_handle_irq), "r" (isp)
  93. : "memory", "r0", "r1", "r2", "r3", "r4",
  94. "r5", "r6", "r7", "r8", "t", "pr"
  95. );
  96. } else
  97. generic_handle_irq(irq);
  98. }
  99. /*
  100. * allocate per-cpu stacks for hardirq and for softirq processing
  101. */
  102. void irq_ctx_init(int cpu)
  103. {
  104. union irq_ctx *irqctx;
  105. if (hardirq_ctx[cpu])
  106. return;
  107. irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
  108. irqctx->tinfo.task = NULL;
  109. irqctx->tinfo.exec_domain = 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.exec_domain = 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. asmlinkage void do_softirq(void)
  129. {
  130. unsigned long flags;
  131. struct thread_info *curctx;
  132. union irq_ctx *irqctx;
  133. u32 *isp;
  134. if (in_interrupt())
  135. return;
  136. local_irq_save(flags);
  137. if (local_softirq_pending()) {
  138. curctx = current_thread_info();
  139. irqctx = softirq_ctx[smp_processor_id()];
  140. irqctx->tinfo.task = curctx->task;
  141. irqctx->tinfo.previous_sp = current_stack_pointer;
  142. /* build the stack frame on the softirq stack */
  143. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  144. __asm__ __volatile__ (
  145. "mov r15, r9 \n"
  146. "jsr @%0 \n"
  147. /* switch to the softirq stack */
  148. " mov %1, r15 \n"
  149. /* restore the thread stack */
  150. "mov r9, r15 \n"
  151. : /* no outputs */
  152. : "r" (__do_softirq), "r" (isp)
  153. : "memory", "r0", "r1", "r2", "r3", "r4",
  154. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  155. );
  156. /*
  157. * Shouldn't happen, we returned above if in_interrupt():
  158. */
  159. WARN_ON_ONCE(softirq_count());
  160. }
  161. local_irq_restore(flags);
  162. }
  163. #else
  164. static inline void handle_one_irq(unsigned int irq)
  165. {
  166. generic_handle_irq(irq);
  167. }
  168. #endif
  169. asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
  170. {
  171. struct pt_regs *old_regs = set_irq_regs(regs);
  172. irq_enter();
  173. irq = irq_demux(irq_lookup(irq));
  174. if (irq != NO_IRQ_IGNORE) {
  175. handle_one_irq(irq);
  176. irq_finish(irq);
  177. }
  178. irq_exit();
  179. set_irq_regs(old_regs);
  180. return IRQ_HANDLED;
  181. }
  182. void __init init_IRQ(void)
  183. {
  184. plat_irq_setup();
  185. /* Perform the machine specific initialisation */
  186. if (sh_mv.mv_init_irq)
  187. sh_mv.mv_init_irq();
  188. intc_finalize();
  189. irq_ctx_init(smp_processor_id());
  190. }
  191. #ifdef CONFIG_SPARSE_IRQ
  192. int __init arch_probe_nr_irqs(void)
  193. {
  194. nr_irqs = sh_mv.mv_nr_irqs;
  195. return NR_IRQS_LEGACY;
  196. }
  197. #endif
  198. #ifdef CONFIG_HOTPLUG_CPU
  199. static void route_irq(struct irq_data *data, unsigned int irq, unsigned int cpu)
  200. {
  201. struct irq_desc *desc = irq_to_desc(irq);
  202. struct irq_chip *chip = irq_data_get_irq_chip(data);
  203. printk(KERN_INFO "IRQ%u: moving from cpu%u to cpu%u\n",
  204. irq, data->node, cpu);
  205. raw_spin_lock_irq(&desc->lock);
  206. chip->irq_set_affinity(data, cpumask_of(cpu), false);
  207. raw_spin_unlock_irq(&desc->lock);
  208. }
  209. /*
  210. * The CPU has been marked offline. Migrate IRQs off this CPU. If
  211. * the affinity settings do not allow other CPUs, force them onto any
  212. * available CPU.
  213. */
  214. void migrate_irqs(void)
  215. {
  216. unsigned int irq, cpu = smp_processor_id();
  217. for_each_active_irq(irq) {
  218. struct irq_data *data = irq_get_irq_data(irq);
  219. if (data->node == cpu) {
  220. unsigned int newcpu = cpumask_any_and(data->affinity,
  221. cpu_online_mask);
  222. if (newcpu >= nr_cpu_ids) {
  223. pr_info_ratelimited("IRQ%u no longer affine to CPU%u\n",
  224. irq, cpu);
  225. cpumask_setall(data->affinity);
  226. newcpu = cpumask_any_and(data->affinity,
  227. cpu_online_mask);
  228. }
  229. route_irq(data, irq, newcpu);
  230. }
  231. }
  232. }
  233. #endif