irq.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <asm/processor.h>
  17. #include <asm/machvec.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/thread_info.h>
  20. #include <cpu/mmu_context.h>
  21. atomic_t irq_err_count;
  22. /*
  23. * 'what should we do if we get a hw irq event on an illegal vector'.
  24. * each architecture has to answer this themselves, it doesn't deserve
  25. * a generic callback i think.
  26. */
  27. void ack_bad_irq(unsigned int irq)
  28. {
  29. atomic_inc(&irq_err_count);
  30. printk("unexpected IRQ trap at vector %02x\n", irq);
  31. }
  32. #if defined(CONFIG_PROC_FS)
  33. /*
  34. * /proc/interrupts printing:
  35. */
  36. static int show_other_interrupts(struct seq_file *p, int prec)
  37. {
  38. int j;
  39. seq_printf(p, "%*s: ", prec, "NMI");
  40. for_each_online_cpu(j)
  41. seq_printf(p, "%10u ", irq_stat[j].__nmi_count);
  42. seq_printf(p, " Non-maskable interrupts\n");
  43. seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
  44. return 0;
  45. }
  46. int show_interrupts(struct seq_file *p, void *v)
  47. {
  48. unsigned long flags, any_count = 0;
  49. int i = *(loff_t *)v, j, prec;
  50. struct irqaction *action;
  51. struct irq_desc *desc;
  52. if (i > nr_irqs)
  53. return 0;
  54. for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
  55. j *= 10;
  56. if (i == nr_irqs)
  57. return show_other_interrupts(p, prec);
  58. if (i == 0) {
  59. seq_printf(p, "%*s", prec + 8, "");
  60. for_each_online_cpu(j)
  61. seq_printf(p, "CPU%-8d", j);
  62. seq_putc(p, '\n');
  63. }
  64. desc = irq_to_desc(i);
  65. if (!desc)
  66. return 0;
  67. raw_spin_lock_irqsave(&desc->lock, flags);
  68. for_each_online_cpu(j)
  69. any_count |= kstat_irqs_cpu(i, j);
  70. action = desc->action;
  71. if (!action && !any_count)
  72. goto out;
  73. seq_printf(p, "%*d: ", prec, i);
  74. for_each_online_cpu(j)
  75. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  76. seq_printf(p, " %14s", desc->chip->name);
  77. seq_printf(p, "-%-8s", desc->name);
  78. if (action) {
  79. seq_printf(p, " %s", action->name);
  80. while ((action = action->next) != NULL)
  81. seq_printf(p, ", %s", action->name);
  82. }
  83. seq_putc(p, '\n');
  84. out:
  85. raw_spin_unlock_irqrestore(&desc->lock, flags);
  86. return 0;
  87. }
  88. #endif
  89. #ifdef CONFIG_IRQSTACKS
  90. /*
  91. * per-CPU IRQ handling contexts (thread information and stack)
  92. */
  93. union irq_ctx {
  94. struct thread_info tinfo;
  95. u32 stack[THREAD_SIZE/sizeof(u32)];
  96. };
  97. static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
  98. static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
  99. static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  100. static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  101. static inline void handle_one_irq(unsigned int irq)
  102. {
  103. union irq_ctx *curctx, *irqctx;
  104. curctx = (union irq_ctx *)current_thread_info();
  105. irqctx = hardirq_ctx[smp_processor_id()];
  106. /*
  107. * this is where we switch to the IRQ stack. However, if we are
  108. * already using the IRQ stack (because we interrupted a hardirq
  109. * handler) we can't do that and just have to keep using the
  110. * current stack (which is the irq stack already after all)
  111. */
  112. if (curctx != irqctx) {
  113. u32 *isp;
  114. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  115. irqctx->tinfo.task = curctx->tinfo.task;
  116. irqctx->tinfo.previous_sp = current_stack_pointer;
  117. /*
  118. * Copy the softirq bits in preempt_count so that the
  119. * softirq checks work in the hardirq context.
  120. */
  121. irqctx->tinfo.preempt_count =
  122. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  123. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  124. __asm__ __volatile__ (
  125. "mov %0, r4 \n"
  126. "mov r15, r8 \n"
  127. "jsr @%1 \n"
  128. /* swith to the irq stack */
  129. " mov %2, r15 \n"
  130. /* restore the stack (ring zero) */
  131. "mov r8, r15 \n"
  132. : /* no outputs */
  133. : "r" (irq), "r" (generic_handle_irq), "r" (isp)
  134. : "memory", "r0", "r1", "r2", "r3", "r4",
  135. "r5", "r6", "r7", "r8", "t", "pr"
  136. );
  137. } else
  138. generic_handle_irq(irq);
  139. }
  140. /*
  141. * allocate per-cpu stacks for hardirq and for softirq processing
  142. */
  143. void irq_ctx_init(int cpu)
  144. {
  145. union irq_ctx *irqctx;
  146. if (hardirq_ctx[cpu])
  147. return;
  148. irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
  149. irqctx->tinfo.task = NULL;
  150. irqctx->tinfo.exec_domain = NULL;
  151. irqctx->tinfo.cpu = cpu;
  152. irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
  153. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  154. hardirq_ctx[cpu] = irqctx;
  155. irqctx = (union irq_ctx *)&softirq_stack[cpu * THREAD_SIZE];
  156. irqctx->tinfo.task = NULL;
  157. irqctx->tinfo.exec_domain = NULL;
  158. irqctx->tinfo.cpu = cpu;
  159. irqctx->tinfo.preempt_count = 0;
  160. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  161. softirq_ctx[cpu] = irqctx;
  162. printk("CPU %u irqstacks, hard=%p soft=%p\n",
  163. cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
  164. }
  165. void irq_ctx_exit(int cpu)
  166. {
  167. hardirq_ctx[cpu] = NULL;
  168. }
  169. asmlinkage void do_softirq(void)
  170. {
  171. unsigned long flags;
  172. struct thread_info *curctx;
  173. union irq_ctx *irqctx;
  174. u32 *isp;
  175. if (in_interrupt())
  176. return;
  177. local_irq_save(flags);
  178. if (local_softirq_pending()) {
  179. curctx = current_thread_info();
  180. irqctx = softirq_ctx[smp_processor_id()];
  181. irqctx->tinfo.task = curctx->task;
  182. irqctx->tinfo.previous_sp = current_stack_pointer;
  183. /* build the stack frame on the softirq stack */
  184. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  185. __asm__ __volatile__ (
  186. "mov r15, r9 \n"
  187. "jsr @%0 \n"
  188. /* switch to the softirq stack */
  189. " mov %1, r15 \n"
  190. /* restore the thread stack */
  191. "mov r9, r15 \n"
  192. : /* no outputs */
  193. : "r" (__do_softirq), "r" (isp)
  194. : "memory", "r0", "r1", "r2", "r3", "r4",
  195. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  196. );
  197. /*
  198. * Shouldnt happen, we returned above if in_interrupt():
  199. */
  200. WARN_ON_ONCE(softirq_count());
  201. }
  202. local_irq_restore(flags);
  203. }
  204. #else
  205. static inline void handle_one_irq(unsigned int irq)
  206. {
  207. generic_handle_irq(irq);
  208. }
  209. #endif
  210. asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
  211. {
  212. struct pt_regs *old_regs = set_irq_regs(regs);
  213. irq_enter();
  214. irq = irq_demux(irq_lookup(irq));
  215. if (irq != NO_IRQ_IGNORE) {
  216. handle_one_irq(irq);
  217. irq_finish(irq);
  218. }
  219. irq_exit();
  220. set_irq_regs(old_regs);
  221. return IRQ_HANDLED;
  222. }
  223. void __init init_IRQ(void)
  224. {
  225. plat_irq_setup();
  226. /*
  227. * Pin any of the legacy IRQ vectors that haven't already been
  228. * grabbed by the platform
  229. */
  230. reserve_irq_legacy();
  231. /* Perform the machine specific initialisation */
  232. if (sh_mv.mv_init_irq)
  233. sh_mv.mv_init_irq();
  234. irq_ctx_init(smp_processor_id());
  235. }
  236. #ifdef CONFIG_SPARSE_IRQ
  237. int __init arch_probe_nr_irqs(void)
  238. {
  239. nr_irqs = sh_mv.mv_nr_irqs;
  240. return 0;
  241. }
  242. #endif
  243. #ifdef CONFIG_HOTPLUG_CPU
  244. static void route_irq(struct irq_desc *desc, unsigned int irq, unsigned int cpu)
  245. {
  246. printk(KERN_INFO "IRQ%u: moving from cpu%u to cpu%u\n",
  247. irq, desc->node, cpu);
  248. raw_spin_lock_irq(&desc->lock);
  249. desc->chip->set_affinity(irq, cpumask_of(cpu));
  250. raw_spin_unlock_irq(&desc->lock);
  251. }
  252. /*
  253. * The CPU has been marked offline. Migrate IRQs off this CPU. If
  254. * the affinity settings do not allow other CPUs, force them onto any
  255. * available CPU.
  256. */
  257. void migrate_irqs(void)
  258. {
  259. struct irq_desc *desc;
  260. unsigned int irq, cpu = smp_processor_id();
  261. for_each_irq_desc(irq, desc) {
  262. if (desc->node == cpu) {
  263. unsigned int newcpu = cpumask_any_and(desc->affinity,
  264. cpu_online_mask);
  265. if (newcpu >= nr_cpu_ids) {
  266. if (printk_ratelimit())
  267. printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n",
  268. irq, cpu);
  269. cpumask_setall(desc->affinity);
  270. newcpu = cpumask_any_and(desc->affinity,
  271. cpu_online_mask);
  272. }
  273. route_irq(desc, irq, newcpu);
  274. }
  275. }
  276. }
  277. #endif