blob: 6c0378c0b8b5cc179cbf06269e94a02a5d1283c2 [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 Mundt9ab3a152011-06-30 15:10:06 +090016#include <linux/ratelimit.h>
Paul Mundtbf3a00f2006-01-16 22:14:14 -080017#include <asm/processor.h>
Paul Mundtbe782df2007-03-12 14:09:35 +090018#include <asm/machvec.h>
Paul Mundta6a311392006-09-27 18:22:14 +090019#include <asm/uaccess.h>
20#include <asm/thread_info.h>
Paul Mundtf15cbe62008-07-29 08:09:44 +090021#include <cpu/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Paul Mundt35f3c512006-10-06 15:31:16 +090023atomic_t irq_err_count;
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
26 * 'what should we do if we get a hw irq event on an illegal vector'.
27 * each architecture has to answer this themselves, it doesn't deserve
28 * a generic callback i think.
29 */
30void ack_bad_irq(unsigned int irq)
31{
Paul Mundtbaf43262006-10-12 12:03:04 +090032 atomic_inc(&irq_err_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 printk("unexpected IRQ trap at vector %02x\n", irq);
34}
35
36#if defined(CONFIG_PROC_FS)
Paul Mundtfa1d43a2009-05-22 01:26:16 +090037/*
Paul Mundt3d44ae42011-03-17 17:31:51 +090038 * /proc/interrupts printing for arch specific interrupts
Paul Mundtfa1d43a2009-05-22 01:26:16 +090039 */
Paul Mundt3d44ae42011-03-17 17:31:51 +090040int arch_show_interrupts(struct seq_file *p, int prec)
Paul Mundtfa1d43a2009-05-22 01:26:16 +090041{
Paul Mundt731ba332009-10-14 16:42:28 +090042 int j;
43
44 seq_printf(p, "%*s: ", prec, "NMI");
45 for_each_online_cpu(j)
46 seq_printf(p, "%10u ", irq_stat[j].__nmi_count);
47 seq_printf(p, " Non-maskable interrupts\n");
48
Paul Mundtfa1d43a2009-05-22 01:26:16 +090049 seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
Paul Mundt731ba332009-10-14 16:42:28 +090050
Paul Mundtfa1d43a2009-05-22 01:26:16 +090051 return 0;
52}
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif
54
Paul Mundt110ed282007-11-02 12:16:51 +090055#ifdef CONFIG_IRQSTACKS
Paul Mundta6a311392006-09-27 18:22:14 +090056/*
57 * per-CPU IRQ handling contexts (thread information and stack)
58 */
59union irq_ctx {
60 struct thread_info tinfo;
61 u32 stack[THREAD_SIZE/sizeof(u32)];
62};
63
Paul Mundt1dc41e52006-11-24 19:46:18 +090064static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
65static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
Paul Mundta6a311392006-09-27 18:22:14 +090066
Paul Mundtdc825b12010-04-15 13:13:52 +090067static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
68static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
69
70static inline void handle_one_irq(unsigned int irq)
Paul Mundtbf3a00f2006-01-16 22:14:14 -080071{
Paul Mundta6a311392006-09-27 18:22:14 +090072 union irq_ctx *curctx, *irqctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paul Mundta6a311392006-09-27 18:22:14 +090074 curctx = (union irq_ctx *)current_thread_info();
75 irqctx = hardirq_ctx[smp_processor_id()];
76
77 /*
78 * this is where we switch to the IRQ stack. However, if we are
79 * already using the IRQ stack (because we interrupted a hardirq
80 * handler) we can't do that and just have to keep using the
81 * current stack (which is the irq stack already after all)
82 */
83 if (curctx != irqctx) {
84 u32 *isp;
85
86 isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
87 irqctx->tinfo.task = curctx->tinfo.task;
88 irqctx->tinfo.previous_sp = current_stack_pointer;
89
Paul Mundt1dc41e52006-11-24 19:46:18 +090090 /*
91 * Copy the softirq bits in preempt_count so that the
92 * softirq checks work in the hardirq context.
93 */
94 irqctx->tinfo.preempt_count =
95 (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
96 (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
97
Paul Mundta6a311392006-09-27 18:22:14 +090098 __asm__ __volatile__ (
99 "mov %0, r4 \n"
Paul Mundt1dc41e52006-11-24 19:46:18 +0900100 "mov r15, r8 \n"
Paul Mundtbaf43262006-10-12 12:03:04 +0900101 "jsr @%1 \n"
Paul Mundta6a311392006-09-27 18:22:14 +0900102 /* swith to the irq stack */
Paul Mundtbaf43262006-10-12 12:03:04 +0900103 " mov %2, r15 \n"
Paul Mundta6a311392006-09-27 18:22:14 +0900104 /* restore the stack (ring zero) */
Paul Mundt1dc41e52006-11-24 19:46:18 +0900105 "mov r8, r15 \n"
Paul Mundta6a311392006-09-27 18:22:14 +0900106 : /* no outputs */
Paul Mundt35f3c512006-10-06 15:31:16 +0900107 : "r" (irq), "r" (generic_handle_irq), "r" (isp)
Paul Mundta6a311392006-09-27 18:22:14 +0900108 : "memory", "r0", "r1", "r2", "r3", "r4",
109 "r5", "r6", "r7", "r8", "t", "pr"
110 );
111 } else
Paul Mundt35f3c512006-10-06 15:31:16 +0900112 generic_handle_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
Paul Mundta6a311392006-09-27 18:22:14 +0900114
Paul Mundta6a311392006-09-27 18:22:14 +0900115/*
116 * allocate per-cpu stacks for hardirq and for softirq processing
117 */
118void irq_ctx_init(int cpu)
119{
120 union irq_ctx *irqctx;
121
122 if (hardirq_ctx[cpu])
123 return;
124
125 irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
126 irqctx->tinfo.task = NULL;
Paul Mundta6a311392006-09-27 18:22:14 +0900127 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;
Paul Mundta6a311392006-09-27 18:22:14 +0900135 irqctx->tinfo.cpu = cpu;
Paul Mundt1dc41e52006-11-24 19:46:18 +0900136 irqctx->tinfo.preempt_count = 0;
Paul Mundta6a311392006-09-27 18:22:14 +0900137 irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
138
139 softirq_ctx[cpu] = irqctx;
140
141 printk("CPU %u irqstacks, hard=%p soft=%p\n",
142 cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
143}
144
145void irq_ctx_exit(int cpu)
146{
147 hardirq_ctx[cpu] = NULL;
148}
149
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200150void do_softirq_own_stack(void)
Paul Mundta6a311392006-09-27 18:22:14 +0900151{
Paul Mundta6a311392006-09-27 18:22:14 +0900152 struct thread_info *curctx;
153 union irq_ctx *irqctx;
154 u32 *isp;
155
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200156 curctx = current_thread_info();
157 irqctx = softirq_ctx[smp_processor_id()];
158 irqctx->tinfo.task = curctx->task;
159 irqctx->tinfo.previous_sp = current_stack_pointer;
Paul Mundta6a311392006-09-27 18:22:14 +0900160
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200161 /* build the stack frame on the softirq stack */
162 isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
Paul Mundta6a311392006-09-27 18:22:14 +0900163
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200164 __asm__ __volatile__ (
165 "mov r15, r9 \n"
166 "jsr @%0 \n"
167 /* switch to the softirq stack */
168 " mov %1, r15 \n"
169 /* restore the thread stack */
170 "mov r9, r15 \n"
171 : /* no outputs */
172 : "r" (__do_softirq), "r" (isp)
173 : "memory", "r0", "r1", "r2", "r3", "r4",
174 "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
175 );
Paul Mundta6a311392006-09-27 18:22:14 +0900176}
Paul Mundtdc825b12010-04-15 13:13:52 +0900177#else
178static inline void handle_one_irq(unsigned int irq)
179{
180 generic_handle_irq(irq);
181}
Paul Mundta6a311392006-09-27 18:22:14 +0900182#endif
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900183
Paul Mundtdc825b12010-04-15 13:13:52 +0900184asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
185{
186 struct pt_regs *old_regs = set_irq_regs(regs);
187
188 irq_enter();
189
190 irq = irq_demux(irq_lookup(irq));
191
192 if (irq != NO_IRQ_IGNORE) {
193 handle_one_irq(irq);
194 irq_finish(irq);
195 }
196
197 irq_exit();
198
199 set_irq_regs(old_regs);
200
201 return IRQ_HANDLED;
202}
203
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900204void __init init_IRQ(void)
205{
Magnus Damm90015c82007-07-18 17:57:34 +0900206 plat_irq_setup();
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900207
208 /* Perform the machine specific initialisation */
209 if (sh_mv.mv_init_irq)
210 sh_mv.mv_init_irq();
211
Paul Mundtc1e30ad2010-10-05 04:47:03 +0900212 intc_finalize();
213
Jamie Lenehanea0f8fe2006-12-06 12:05:02 +0900214 irq_ctx_init(smp_processor_id());
215}
Paul Mundtd8586ba2009-05-22 01:36:13 +0900216
Paul Mundt763142d2010-04-26 19:08:55 +0900217#ifdef CONFIG_HOTPLUG_CPU
Paul Mundt763142d2010-04-26 19:08:55 +0900218/*
219 * The CPU has been marked offline. Migrate IRQs off this CPU. If
220 * the affinity settings do not allow other CPUs, force them onto any
221 * available CPU.
222 */
223void migrate_irqs(void)
224{
Paul Mundt763142d2010-04-26 19:08:55 +0900225 unsigned int irq, cpu = smp_processor_id();
226
Paul Mundtfb41a492010-10-28 11:33:21 +0900227 for_each_active_irq(irq) {
228 struct irq_data *data = irq_get_irq_data(irq);
229
Jiang Liucde5c272015-07-13 20:51:20 +0000230 if (irq_data_get_node(data) == cpu) {
Thomas Gleixner8b8149d2015-07-13 20:51:23 +0000231 struct cpumask *mask = irq_data_get_affinity_mask(data);
232 unsigned int newcpu = cpumask_any_and(mask,
Paul Mundt763142d2010-04-26 19:08:55 +0900233 cpu_online_mask);
234 if (newcpu >= nr_cpu_ids) {
Paul Mundt9ab3a152011-06-30 15:10:06 +0900235 pr_info_ratelimited("IRQ%u no longer affine to CPU%u\n",
236 irq, cpu);
Paul Mundt763142d2010-04-26 19:08:55 +0900237
Thomas Gleixner8b8149d2015-07-13 20:51:23 +0000238 cpumask_setall(mask);
Paul Mundt763142d2010-04-26 19:08:55 +0900239 }
Thomas Gleixner8b8149d2015-07-13 20:51:23 +0000240 irq_set_affinity(irq, mask);
Paul Mundt763142d2010-04-26 19:08:55 +0900241 }
242 }
243}
244#endif