blob: 64ea0b165399260665c68a63e5d3debba3b95356 [file] [log] [blame]
Paul Mundta6a311392006-09-27 18:22:14 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/irq.h>
Paul Mundtbf3a00f2006-01-16 22:14:14 -080010#include <linux/interrupt.h>
Paul Mundta6a311392006-09-27 18:22:14 +090011#include <linux/module.h>
Paul Mundtbf3a00f2006-01-16 22:14:14 -080012#include <linux/kernel_stat.h>
13#include <linux/seq_file.h>
Paul Mundtba934832009-10-26 09:58:31 +090014#include <linux/ftrace.h>
Paul Mundt763142d2010-04-26 19:08:55 +090015#include <linux/delay.h>
Paul Mundtbf3a00f2006-01-16 22:14:14 -080016#include <asm/processor.h>
Paul Mundtbe782df2007-03-12 14:09:35 +090017#include <asm/machvec.h>
Paul Mundta6a311392006-09-27 18:22:14 +090018#include <asm/uaccess.h>
19#include <asm/thread_info.h>
Paul Mundtf15cbe62008-07-29 08:09:44 +090020#include <cpu/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Paul Mundt35f3c512006-10-06 15:31:16 +090022atomic_t irq_err_count;
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
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 */
29void ack_bad_irq(unsigned int irq)
30{
Paul Mundtbaf43262006-10-12 12:03:04 +090031 atomic_inc(&irq_err_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 printk("unexpected IRQ trap at vector %02x\n", irq);
33}
34
35#if defined(CONFIG_PROC_FS)
Paul Mundtfa1d43a2009-05-22 01:26:16 +090036/*
Paul Mundt3d44ae42011-03-17 17:31:51 +090037 * /proc/interrupts printing for arch specific interrupts
Paul Mundtfa1d43a2009-05-22 01:26:16 +090038 */
Paul Mundt3d44ae42011-03-17 17:31:51 +090039int arch_show_interrupts(struct seq_file *p, int prec)
Paul Mundtfa1d43a2009-05-22 01:26:16 +090040{
Paul Mundt731ba332009-10-14 16:42:28 +090041 int j;
42
43 seq_printf(p, "%*s: ", prec, "NMI");
44 for_each_online_cpu(j)
45 seq_printf(p, "%10u ", irq_stat[j].__nmi_count);
46 seq_printf(p, " Non-maskable interrupts\n");
47
Paul Mundtfa1d43a2009-05-22 01:26:16 +090048 seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
Paul Mundt731ba332009-10-14 16:42:28 +090049
Paul Mundtfa1d43a2009-05-22 01:26:16 +090050 return 0;
51}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
53
Paul Mundt110ed282007-11-02 12:16:51 +090054#ifdef CONFIG_IRQSTACKS
Paul Mundta6a311392006-09-27 18:22:14 +090055/*
56 * per-CPU IRQ handling contexts (thread information and stack)
57 */
58union irq_ctx {
59 struct thread_info tinfo;
60 u32 stack[THREAD_SIZE/sizeof(u32)];
61};
62
Paul Mundt1dc41e52006-11-24 19:46:18 +090063static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
64static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
Paul Mundta6a311392006-09-27 18:22:14 +090065
Paul Mundtdc825b12010-04-15 13:13:52 +090066static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
67static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
68
69static inline void handle_one_irq(unsigned int irq)
Paul Mundtbf3a00f2006-01-16 22:14:14 -080070{
Paul Mundta6a311392006-09-27 18:22:14 +090071 union irq_ctx *curctx, *irqctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Paul Mundta6a311392006-09-27 18:22:14 +090073 curctx = (union irq_ctx *)current_thread_info();
74 irqctx = hardirq_ctx[smp_processor_id()];
75
76 /*
77 * this is where we switch to the IRQ stack. However, if we are
78 * already using the IRQ stack (because we interrupted a hardirq
79 * handler) we can't do that and just have to keep using the
80 * current stack (which is the irq stack already after all)
81 */
82 if (curctx != irqctx) {
83 u32 *isp;
84
85 isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
86 irqctx->tinfo.task = curctx->tinfo.task;
87 irqctx->tinfo.previous_sp = current_stack_pointer;
88
Paul Mundt1dc41e52006-11-24 19:46:18 +090089 /*
90 * Copy the softirq bits in preempt_count so that the
91 * softirq checks work in the hardirq context.
92 */
93 irqctx->tinfo.preempt_count =
94 (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
95 (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
96
Paul Mundta6a311392006-09-27 18:22:14 +090097 __asm__ __volatile__ (
98 "mov %0, r4 \n"
Paul Mundt1dc41e52006-11-24 19:46:18 +090099 "mov r15, r8 \n"
Paul Mundtbaf43262006-10-12 12:03:04 +0900100 "jsr @%1 \n"
Paul Mundta6a311392006-09-27 18:22:14 +0900101 /* swith to the irq stack */
Paul Mundtbaf43262006-10-12 12:03:04 +0900102 " mov %2, r15 \n"
Paul Mundta6a311392006-09-27 18:22:14 +0900103 /* restore the stack (ring zero) */
Paul Mundt1dc41e52006-11-24 19:46:18 +0900104 "mov r8, r15 \n"
Paul Mundta6a311392006-09-27 18:22:14 +0900105 : /* no outputs */
Paul Mundt35f3c512006-10-06 15:31:16 +0900106 : "r" (irq), "r" (generic_handle_irq), "r" (isp)
Paul Mundta6a311392006-09-27 18:22:14 +0900107 : "memory", "r0", "r1", "r2", "r3", "r4",
108 "r5", "r6", "r7", "r8", "t", "pr"
109 );
110 } else
Paul Mundt35f3c512006-10-06 15:31:16 +0900111 generic_handle_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Paul Mundta6a311392006-09-27 18:22:14 +0900113
Paul Mundta6a311392006-09-27 18:22:14 +0900114/*
115 * allocate per-cpu stacks for hardirq and for softirq processing
116 */
117void irq_ctx_init(int cpu)
118{
119 union irq_ctx *irqctx;
120
121 if (hardirq_ctx[cpu])
122 return;
123
124 irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
125 irqctx->tinfo.task = NULL;
126 irqctx->tinfo.exec_domain = NULL;
127 irqctx->tinfo.cpu = cpu;
128 irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
129 irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
130
131 hardirq_ctx[cpu] = irqctx;
132
133 irqctx = (union irq_ctx *)&softirq_stack[cpu * THREAD_SIZE];
134 irqctx->tinfo.task = NULL;
135 irqctx->tinfo.exec_domain = NULL;
136 irqctx->tinfo.cpu = cpu;
Paul Mundt1dc41e52006-11-24 19:46:18 +0900137 irqctx->tinfo.preempt_count = 0;
Paul Mundta6a311392006-09-27 18:22:14 +0900138 irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
139
140 softirq_ctx[cpu] = irqctx;
141
142 printk("CPU %u irqstacks, hard=%p soft=%p\n",
143 cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
144}
145
146void irq_ctx_exit(int cpu)
147{
148 hardirq_ctx[cpu] = NULL;
149}
150
Paul Mundta6a311392006-09-27 18:22:14 +0900151asmlinkage void do_softirq(void)
152{
153 unsigned long flags;
154 struct thread_info *curctx;
155 union irq_ctx *irqctx;
156 u32 *isp;
157
158 if (in_interrupt())
159 return;
160
161 local_irq_save(flags);
162
163 if (local_softirq_pending()) {
164 curctx = current_thread_info();
165 irqctx = softirq_ctx[smp_processor_id()];
166 irqctx->tinfo.task = curctx->task;
167 irqctx->tinfo.previous_sp = current_stack_pointer;
168
169 /* build the stack frame on the softirq stack */
170 isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
171
172 __asm__ __volatile__ (
173 "mov r15, r9 \n"
174 "jsr @%0 \n"
175 /* switch to the softirq stack */
176 " mov %1, r15 \n"
177 /* restore the thread stack */
178 "mov r9, r15 \n"
179 : /* no outputs */
180 : "r" (__do_softirq), "r" (isp)
Paul Mundta6a311392006-09-27 18:22:14 +0900181 : "memory", "r0", "r1", "r2", "r3", "r4",
182 "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
183 );
Paul Mundt1dc41e52006-11-24 19:46:18 +0900184
185 /*
186 * Shouldnt happen, we returned above if in_interrupt():
187 */
188 WARN_ON_ONCE(softirq_count());
Paul Mundta6a311392006-09-27 18:22:14 +0900189 }
190
191 local_irq_restore(flags);
192}
Paul Mundtdc825b12010-04-15 13:13:52 +0900193#else
194static inline void handle_one_irq(unsigned int irq)
195{
196 generic_handle_irq(irq);
197}
Paul Mundta6a311392006-09-27 18:22:14 +0900198#endif
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900199
Paul Mundtdc825b12010-04-15 13:13:52 +0900200asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
201{
202 struct pt_regs *old_regs = set_irq_regs(regs);
203
204 irq_enter();
205
206 irq = irq_demux(irq_lookup(irq));
207
208 if (irq != NO_IRQ_IGNORE) {
209 handle_one_irq(irq);
210 irq_finish(irq);
211 }
212
213 irq_exit();
214
215 set_irq_regs(old_regs);
216
217 return IRQ_HANDLED;
218}
219
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900220void __init init_IRQ(void)
221{
Magnus Damm90015c82007-07-18 17:57:34 +0900222 plat_irq_setup();
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900223
224 /* Perform the machine specific initialisation */
225 if (sh_mv.mv_init_irq)
226 sh_mv.mv_init_irq();
227
Paul Mundtc1e30ad2010-10-05 04:47:03 +0900228 intc_finalize();
229
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900230 irq_ctx_init(smp_processor_id());
231}
Paul Mundtd8586ba2009-05-22 01:36:13 +0900232
233#ifdef CONFIG_SPARSE_IRQ
234int __init arch_probe_nr_irqs(void)
235{
236 nr_irqs = sh_mv.mv_nr_irqs;
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200237 return NR_IRQS_LEGACY;
Paul Mundtd8586ba2009-05-22 01:36:13 +0900238}
239#endif
Paul Mundt763142d2010-04-26 19:08:55 +0900240
241#ifdef CONFIG_HOTPLUG_CPU
Paul Mundtfb41a492010-10-28 11:33:21 +0900242static void route_irq(struct irq_data *data, unsigned int irq, unsigned int cpu)
Paul Mundt763142d2010-04-26 19:08:55 +0900243{
Paul Mundtfb41a492010-10-28 11:33:21 +0900244 struct irq_desc *desc = irq_to_desc(irq);
245 struct irq_chip *chip = irq_data_get_irq_chip(data);
246
Paul Mundt763142d2010-04-26 19:08:55 +0900247 printk(KERN_INFO "IRQ%u: moving from cpu%u to cpu%u\n",
Paul Mundtfb41a492010-10-28 11:33:21 +0900248 irq, data->node, cpu);
Paul Mundt763142d2010-04-26 19:08:55 +0900249
250 raw_spin_lock_irq(&desc->lock);
Paul Mundtfb41a492010-10-28 11:33:21 +0900251 chip->irq_set_affinity(data, cpumask_of(cpu), false);
Paul Mundt763142d2010-04-26 19:08:55 +0900252 raw_spin_unlock_irq(&desc->lock);
253}
254
255/*
256 * The CPU has been marked offline. Migrate IRQs off this CPU. If
257 * the affinity settings do not allow other CPUs, force them onto any
258 * available CPU.
259 */
260void migrate_irqs(void)
261{
Paul Mundt763142d2010-04-26 19:08:55 +0900262 unsigned int irq, cpu = smp_processor_id();
263
Paul Mundtfb41a492010-10-28 11:33:21 +0900264 for_each_active_irq(irq) {
265 struct irq_data *data = irq_get_irq_data(irq);
266
267 if (data->node == cpu) {
268 unsigned int newcpu = cpumask_any_and(data->affinity,
Paul Mundt763142d2010-04-26 19:08:55 +0900269 cpu_online_mask);
270 if (newcpu >= nr_cpu_ids) {
271 if (printk_ratelimit())
272 printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n",
273 irq, cpu);
274
Paul Mundtfb41a492010-10-28 11:33:21 +0900275 cpumask_setall(data->affinity);
276 newcpu = cpumask_any_and(data->affinity,
Paul Mundt763142d2010-04-26 19:08:55 +0900277 cpu_online_mask);
278 }
279
Paul Mundtfb41a492010-10-28 11:33:21 +0900280 route_irq(data, irq, newcpu);
Paul Mundt763142d2010-04-26 19:08:55 +0900281 }
282 }
283}
284#endif