edf4d2d213365568a67b1935ebc05d88fc8b4bd3
[safe/jmp/linux-2.6] / arch / sh / kernel / irq.c
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/io.h>
15 #include <asm/irq.h>
16 #include <asm/processor.h>
17 #include <asm/uaccess.h>
18 #include <asm/thread_info.h>
19 #include <asm/cpu/mmu_context.h>
20
21 atomic_t irq_err_count;
22
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
34 #if defined(CONFIG_PROC_FS)
35 int show_interrupts(struct seq_file *p, void *v)
36 {
37         int i = *(loff_t *) v, j;
38         struct irqaction * action;
39         unsigned long flags;
40
41         if (i == 0) {
42                 seq_puts(p, "           ");
43                 for_each_online_cpu(j)
44                         seq_printf(p, "CPU%d       ",j);
45                 seq_putc(p, '\n');
46         }
47
48         if (i < NR_IRQS) {
49                 spin_lock_irqsave(&irq_desc[i].lock, flags);
50                 action = irq_desc[i].action;
51                 if (!action)
52                         goto unlock;
53                 seq_printf(p, "%3d: ",i);
54                 for_each_online_cpu(j)
55                         seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
56                 seq_printf(p, " %14s", irq_desc[i].chip->name);
57                 seq_printf(p, "-%-8s", irq_desc[i].name);
58                 seq_printf(p, "  %s", action->name);
59
60                 for (action=action->next; action; action = action->next)
61                         seq_printf(p, ", %s", action->name);
62                 seq_putc(p, '\n');
63 unlock:
64                 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
65         } else if (i == NR_IRQS)
66                 seq_printf(p, "Err: %10u\n", atomic_read(&irq_err_count));
67
68         return 0;
69 }
70 #endif
71
72 #ifdef CONFIG_4KSTACKS
73 /*
74  * per-CPU IRQ handling contexts (thread information and stack)
75  */
76 union irq_ctx {
77         struct thread_info      tinfo;
78         u32                     stack[THREAD_SIZE/sizeof(u32)];
79 };
80
81 static union irq_ctx *hardirq_ctx[NR_CPUS];
82 static union irq_ctx *softirq_ctx[NR_CPUS];
83 #endif
84
85 asmlinkage int do_IRQ(unsigned long r4, unsigned long r5,
86                       unsigned long r6, unsigned long r7,
87                       struct pt_regs __regs)
88 {
89         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
90         struct pt_regs *old_regs = set_irq_regs(regs);
91         int irq;
92 #ifdef CONFIG_4KSTACKS
93         union irq_ctx *curctx, *irqctx;
94 #endif
95
96         irq_enter();
97
98 #ifdef CONFIG_DEBUG_STACKOVERFLOW
99         /* Debugging check for stack overflow: is there less than 1KB free? */
100         {
101                 long sp;
102
103                 __asm__ __volatile__ ("and r15, %0" :
104                                         "=r" (sp) : "0" (THREAD_SIZE - 1));
105
106                 if (unlikely(sp < (sizeof(struct thread_info) + STACK_WARN))) {
107                         printk("do_IRQ: stack overflow: %ld\n",
108                                sp - sizeof(struct thread_info));
109                         dump_stack();
110                 }
111         }
112 #endif
113
114 #ifdef CONFIG_CPU_HAS_INTEVT
115         irq = (ctrl_inl(INTEVT) >> 5) - 16;
116 #else
117         irq = r4;
118 #endif
119
120         irq = irq_demux(irq);
121
122 #ifdef CONFIG_4KSTACKS
123         curctx = (union irq_ctx *)current_thread_info();
124         irqctx = hardirq_ctx[smp_processor_id()];
125
126         /*
127          * this is where we switch to the IRQ stack. However, if we are
128          * already using the IRQ stack (because we interrupted a hardirq
129          * handler) we can't do that and just have to keep using the
130          * current stack (which is the irq stack already after all)
131          */
132         if (curctx != irqctx) {
133                 u32 *isp;
134
135                 isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
136                 irqctx->tinfo.task = curctx->tinfo.task;
137                 irqctx->tinfo.previous_sp = current_stack_pointer;
138
139                 __asm__ __volatile__ (
140                         "mov    %0, r4          \n"
141                         "mov    r15, r9         \n"
142                         "jsr    @%1             \n"
143                         /* swith to the irq stack */
144                         " mov   %2, r15         \n"
145                         /* restore the stack (ring zero) */
146                         "mov    r9, r15         \n"
147                         : /* no outputs */
148                         : "r" (irq), "r" (generic_handle_irq), "r" (isp)
149                         /* XXX: A somewhat excessive clobber list? -PFM */
150                         : "memory", "r0", "r1", "r2", "r3", "r4",
151                           "r5", "r6", "r7", "r8", "t", "pr"
152                 );
153         } else
154 #endif
155                 generic_handle_irq(irq);
156
157         irq_exit();
158
159         set_irq_regs(old_regs);
160         return 1;
161 }
162
163 #ifdef CONFIG_4KSTACKS
164 /*
165  * These should really be __section__(".bss.page_aligned") as well, but
166  * gcc's 3.0 and earlier don't handle that correctly.
167  */
168 static char softirq_stack[NR_CPUS * THREAD_SIZE]
169                 __attribute__((__aligned__(THREAD_SIZE)));
170
171 static char hardirq_stack[NR_CPUS * THREAD_SIZE]
172                 __attribute__((__aligned__(THREAD_SIZE)));
173
174 /*
175  * allocate per-cpu stacks for hardirq and for softirq processing
176  */
177 void irq_ctx_init(int cpu)
178 {
179         union irq_ctx *irqctx;
180
181         if (hardirq_ctx[cpu])
182                 return;
183
184         irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
185         irqctx->tinfo.task              = NULL;
186         irqctx->tinfo.exec_domain       = NULL;
187         irqctx->tinfo.cpu               = cpu;
188         irqctx->tinfo.preempt_count     = HARDIRQ_OFFSET;
189         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
190
191         hardirq_ctx[cpu] = irqctx;
192
193         irqctx = (union irq_ctx *)&softirq_stack[cpu * THREAD_SIZE];
194         irqctx->tinfo.task              = NULL;
195         irqctx->tinfo.exec_domain       = NULL;
196         irqctx->tinfo.cpu               = cpu;
197         irqctx->tinfo.preempt_count     = SOFTIRQ_OFFSET;
198         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
199
200         softirq_ctx[cpu] = irqctx;
201
202         printk("CPU %u irqstacks, hard=%p soft=%p\n",
203                 cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
204 }
205
206 void irq_ctx_exit(int cpu)
207 {
208         hardirq_ctx[cpu] = NULL;
209 }
210
211 extern asmlinkage void __do_softirq(void);
212
213 asmlinkage void do_softirq(void)
214 {
215         unsigned long flags;
216         struct thread_info *curctx;
217         union irq_ctx *irqctx;
218         u32 *isp;
219
220         if (in_interrupt())
221                 return;
222
223         local_irq_save(flags);
224
225         if (local_softirq_pending()) {
226                 curctx = current_thread_info();
227                 irqctx = softirq_ctx[smp_processor_id()];
228                 irqctx->tinfo.task = curctx->task;
229                 irqctx->tinfo.previous_sp = current_stack_pointer;
230
231                 /* build the stack frame on the softirq stack */
232                 isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
233
234                 __asm__ __volatile__ (
235                         "mov    r15, r9         \n"
236                         "jsr    @%0             \n"
237                         /* switch to the softirq stack */
238                         " mov   %1, r15         \n"
239                         /* restore the thread stack */
240                         "mov    r9, r15         \n"
241                         : /* no outputs */
242                         : "r" (__do_softirq), "r" (isp)
243                         /* XXX: A somewhat excessive clobber list? -PFM */
244                         : "memory", "r0", "r1", "r2", "r3", "r4",
245                           "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
246                 );
247         }
248
249         local_irq_restore(flags);
250 }
251 EXPORT_SYMBOL(do_softirq);
252 #endif