irq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. struct irq_data *data;
  53. struct irq_chip *chip;
  54. if (i > nr_irqs)
  55. return 0;
  56. for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
  57. j *= 10;
  58. if (i == nr_irqs)
  59. return show_other_interrupts(p, prec);
  60. if (i == 0) {
  61. seq_printf(p, "%*s", prec + 8, "");
  62. for_each_online_cpu(j)
  63. seq_printf(p, "CPU%-8d", j);
  64. seq_putc(p, '\n');
  65. }
  66. desc = irq_to_desc(i);
  67. if (!desc)
  68. return 0;
  69. data = irq_get_irq_data(i);
  70. chip = irq_data_get_irq_chip(data);
  71. raw_spin_lock_irqsave(&desc->lock, flags);
  72. for_each_online_cpu(j)
  73. any_count |= kstat_irqs_cpu(i, j);
  74. action = desc->action;
  75. if (!action && !any_count)
  76. goto out;
  77. seq_printf(p, "%*d: ", prec, i);
  78. for_each_online_cpu(j)
  79. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  80. seq_printf(p, " %14s", chip->name);
  81. seq_printf(p, "-%-8s", desc->name);
  82. if (action) {
  83. seq_printf(p, " %s", action->name);
  84. while ((action = action->next) != NULL)
  85. seq_printf(p, ", %s", action->name);
  86. }
  87. seq_putc(p, '\n');
  88. out:
  89. raw_spin_unlock_irqrestore(&desc->lock, flags);
  90. return 0;
  91. }
  92. #endif
  93. #ifdef CONFIG_IRQSTACKS
  94. /*
  95. * per-CPU IRQ handling contexts (thread information and stack)
  96. */
  97. union irq_ctx {
  98. struct thread_info tinfo;
  99. u32 stack[THREAD_SIZE/sizeof(u32)];
  100. };
  101. static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
  102. static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
  103. static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  104. static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  105. static inline void handle_one_irq(unsigned int irq)
  106. {
  107. union irq_ctx *curctx, *irqctx;
  108. curctx = (union irq_ctx *)current_thread_info();
  109. irqctx = hardirq_ctx[smp_processor_id()];
  110. /*
  111. * this is where we switch to the IRQ stack. However, if we are
  112. * already using the IRQ stack (because we interrupted a hardirq
  113. * handler) we can't do that and just have to keep using the
  114. * current stack (which is the irq stack already after all)
  115. */
  116. if (curctx != irqctx) {
  117. u32 *isp;
  118. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  119. irqctx->tinfo.task = curctx->tinfo.task;
  120. irqctx->tinfo.previous_sp = current_stack_pointer;
  121. /*
  122. * Copy the softirq bits in preempt_count so that the
  123. * softirq checks work in the hardirq context.
  124. */
  125. irqctx->tinfo.preempt_count =
  126. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  127. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  128. __asm__ __volatile__ (
  129. "mov %0, r4 \n"
  130. "mov r15, r8 \n"
  131. "jsr @%1 \n"
  132. /* swith to the irq stack */
  133. " mov %2, r15 \n"
  134. /* restore the stack (ring zero) */
  135. "mov r8, r15 \n"
  136. : /* no outputs */
  137. : "r" (irq), "r" (generic_handle_irq), "r" (isp)
  138. : "memory", "r0", "r1", "r2", "r3", "r4",
  139. "r5", "r6", "r7", "r8", "t", "pr"
  140. );
  141. } else
  142. generic_handle_irq(irq);
  143. }
  144. /*
  145. * allocate per-cpu stacks for hardirq and for softirq processing
  146. */
  147. void irq_ctx_init(int cpu)
  148. {
  149. union irq_ctx *irqctx;
  150. if (hardirq_ctx[cpu])
  151. return;
  152. irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
  153. irqctx->tinfo.task = NULL;
  154. irqctx->tinfo.exec_domain = NULL;
  155. irqctx->tinfo.cpu = cpu;
  156. irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
  157. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  158. hardirq_ctx[cpu] = irqctx;
  159. irqctx = (union irq_ctx *)&softirq_stack[cpu * THREAD_SIZE];
  160. irqctx->tinfo.task = NULL;
  161. irqctx->tinfo.exec_domain = NULL;
  162. irqctx->tinfo.cpu = cpu;
  163. irqctx->tinfo.preempt_count = 0;
  164. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  165. softirq_ctx[cpu] = irqctx;
  166. printk("CPU %u irqstacks, hard=%p soft=%p\n",
  167. cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
  168. }
  169. void irq_ctx_exit(int cpu)
  170. {
  171. hardirq_ctx[cpu] = NULL;
  172. }
  173. asmlinkage void do_softirq(void)
  174. {
  175. unsigned long flags;
  176. struct thread_info *curctx;
  177. union irq_ctx *irqctx;
  178. u32 *isp;
  179. if (in_interrupt())
  180. return;
  181. local_irq_save(flags);
  182. if (local_softirq_pending()) {
  183. curctx = current_thread_info();
  184. irqctx = softirq_ctx[smp_processor_id()];
  185. irqctx->tinfo.task = curctx->task;
  186. irqctx->tinfo.previous_sp = current_stack_pointer;
  187. /* build the stack frame on the softirq stack */
  188. isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
  189. __asm__ __volatile__ (
  190. "mov r15, r9 \n"
  191. "jsr @%0 \n"
  192. /* switch to the softirq stack */
  193. " mov %1, r15 \n"
  194. /* restore the thread stack */
  195. "mov r9, r15 \n"
  196. : /* no outputs */
  197. : "r" (__do_softirq), "r" (isp)
  198. : "memory", "r0", "r1", "r2", "r3", "r4",
  199. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  200. );
  201. /*
  202. * Shouldnt happen, we returned above if in_interrupt():
  203. */
  204. WARN_ON_ONCE(softirq_count());
  205. }
  206. local_irq_restore(flags);
  207. }
  208. #else
  209. static inline void handle_one_irq(unsigned int irq)
  210. {
  211. generic_handle_irq(irq);
  212. }
  213. #endif
  214. asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
  215. {
  216. struct pt_regs *old_regs = set_irq_regs(regs);
  217. irq_enter();
  218. irq = irq_demux(irq_lookup(irq));
  219. if (irq != NO_IRQ_IGNORE) {
  220. handle_one_irq(irq);
  221. irq_finish(irq);
  222. }
  223. irq_exit();
  224. set_irq_regs(old_regs);
  225. return IRQ_HANDLED;
  226. }
  227. void __init init_IRQ(void)
  228. {
  229. plat_irq_setup();
  230. /* Perform the machine specific initialisation */
  231. if (sh_mv.mv_init_irq)
  232. sh_mv.mv_init_irq();
  233. intc_finalize();
  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 NR_IRQS_LEGACY;
  241. }
  242. #endif
  243. #ifdef CONFIG_HOTPLUG_CPU
  244. static void route_irq(struct irq_data *data, unsigned int irq, unsigned int cpu)
  245. {
  246. struct irq_desc *desc = irq_to_desc(irq);
  247. struct irq_chip *chip = irq_data_get_irq_chip(data);
  248. printk(KERN_INFO "IRQ%u: moving from cpu%u to cpu%u\n",
  249. irq, data->node, cpu);
  250. raw_spin_lock_irq(&desc->lock);
  251. chip->irq_set_affinity(data, cpumask_of(cpu), false);
  252. raw_spin_unlock_irq(&desc->lock);
  253. }
  254. /*
  255. * The CPU has been marked offline. Migrate IRQs off this CPU. If
  256. * the affinity settings do not allow other CPUs, force them onto any
  257. * available CPU.
  258. */
  259. void migrate_irqs(void)
  260. {
  261. unsigned int irq, cpu = smp_processor_id();
  262. for_each_active_irq(irq) {
  263. struct irq_data *data = irq_get_irq_data(irq);
  264. if (data->node == cpu) {
  265. unsigned int newcpu = cpumask_any_and(data->affinity,
  266. cpu_online_mask);
  267. if (newcpu >= nr_cpu_ids) {
  268. if (printk_ratelimit())
  269. printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n",
  270. irq, cpu);
  271. cpumask_setall(data->affinity);
  272. newcpu = cpumask_any_and(data->affinity,
  273. cpu_online_mask);
  274. }
  275. route_irq(data, irq, newcpu);
  276. }
  277. }
  278. }
  279. #endif