sa11x0_irqhandler.c revision 1.8.2.2 1 1.8.2.2 yamt /* $NetBSD: sa11x0_irqhandler.c,v 1.8.2.2 2006/05/24 10:56:35 yamt Exp $ */
2 1.8.2.2 yamt
3 1.8.2.2 yamt /*-
4 1.8.2.2 yamt * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
5 1.8.2.2 yamt * All rights reserved.
6 1.8.2.2 yamt *
7 1.8.2.2 yamt * This code is derived from software contributed to the NetBSD Foundation
8 1.8.2.2 yamt * by IWAMOTO Toshihiro.
9 1.8.2.2 yamt *
10 1.8.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
11 1.8.2.2 yamt * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
12 1.8.2.2 yamt * Simulation Facility, NASA Ames Research Center.
13 1.8.2.2 yamt *
14 1.8.2.2 yamt * Redistribution and use in source and binary forms, with or without
15 1.8.2.2 yamt * modification, are permitted provided that the following conditions
16 1.8.2.2 yamt * are met:
17 1.8.2.2 yamt * 1. Redistributions of source code must retain the above copyright
18 1.8.2.2 yamt * notice, this list of conditions and the following disclaimer.
19 1.8.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
20 1.8.2.2 yamt * notice, this list of conditions and the following disclaimer in the
21 1.8.2.2 yamt * documentation and/or other materials provided with the distribution.
22 1.8.2.2 yamt * 3. All advertising materials mentioning features or use of this software
23 1.8.2.2 yamt * must display the following acknowledgement:
24 1.8.2.2 yamt * This product includes software developed by the NetBSD
25 1.8.2.2 yamt * Foundation, Inc. and its contributors.
26 1.8.2.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
27 1.8.2.2 yamt * contributors may be used to endorse or promote products derived
28 1.8.2.2 yamt * from this software without specific prior written permission.
29 1.8.2.2 yamt *
30 1.8.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 1.8.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 1.8.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 1.8.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 1.8.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 1.8.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 1.8.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 1.8.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 1.8.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.8.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.8.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
41 1.8.2.2 yamt */
42 1.8.2.2 yamt
43 1.8.2.2 yamt /*-
44 1.8.2.2 yamt * Copyright (c) 1991 The Regents of the University of California.
45 1.8.2.2 yamt * All rights reserved.
46 1.8.2.2 yamt *
47 1.8.2.2 yamt * This code is derived from software contributed to Berkeley by
48 1.8.2.2 yamt * William Jolitz.
49 1.8.2.2 yamt *
50 1.8.2.2 yamt * Redistribution and use in source and binary forms, with or without
51 1.8.2.2 yamt * modification, are permitted provided that the following conditions
52 1.8.2.2 yamt * are met:
53 1.8.2.2 yamt * 1. Redistributions of source code must retain the above copyright
54 1.8.2.2 yamt * notice, this list of conditions and the following disclaimer.
55 1.8.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
56 1.8.2.2 yamt * notice, this list of conditions and the following disclaimer in the
57 1.8.2.2 yamt * documentation and/or other materials provided with the distribution.
58 1.8.2.2 yamt * 3. Neither the name of the University nor the names of its contributors
59 1.8.2.2 yamt * may be used to endorse or promote products derived from this software
60 1.8.2.2 yamt * without specific prior written permission.
61 1.8.2.2 yamt *
62 1.8.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.8.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.8.2.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.8.2.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.8.2.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.8.2.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.8.2.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.8.2.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.8.2.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.8.2.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.8.2.2 yamt * SUCH DAMAGE.
73 1.8.2.2 yamt *
74 1.8.2.2 yamt * @(#)isa.c 7.2 (Berkeley) 5/13/91
75 1.8.2.2 yamt */
76 1.8.2.2 yamt
77 1.8.2.2 yamt
78 1.8.2.2 yamt #include <sys/cdefs.h>
79 1.8.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: sa11x0_irqhandler.c,v 1.8.2.2 2006/05/24 10:56:35 yamt Exp $");
80 1.8.2.2 yamt
81 1.8.2.2 yamt #include "opt_irqstats.h"
82 1.8.2.2 yamt
83 1.8.2.2 yamt #include <sys/param.h>
84 1.8.2.2 yamt #include <sys/kernel.h>
85 1.8.2.2 yamt #include <sys/systm.h>
86 1.8.2.2 yamt #include <sys/syslog.h>
87 1.8.2.2 yamt #include <sys/malloc.h>
88 1.8.2.2 yamt #include <uvm/uvm_extern.h>
89 1.8.2.2 yamt
90 1.8.2.2 yamt #include <arm/sa11x0/sa11x0_reg.h>
91 1.8.2.2 yamt #include <arm/sa11x0/sa11x0_var.h>
92 1.8.2.2 yamt
93 1.8.2.2 yamt #include <machine/intr.h>
94 1.8.2.2 yamt #include <machine/cpu.h>
95 1.8.2.2 yamt
96 1.8.2.2 yamt irqhandler_t *irqhandlers[NIRQS];
97 1.8.2.2 yamt
98 1.8.2.2 yamt int current_intr_depth;
99 1.8.2.2 yamt u_int actual_mask;
100 1.8.2.2 yamt #ifdef hpcarm
101 1.8.2.2 yamt #define IPL_LEVELS (NIPL+1)
102 1.8.2.2 yamt u_int imask[NIPL];
103 1.8.2.2 yamt #else
104 1.8.2.2 yamt u_int spl_mask;
105 1.8.2.2 yamt u_int irqmasks[IPL_LEVELS];
106 1.8.2.2 yamt #endif
107 1.8.2.2 yamt
108 1.8.2.2 yamt
109 1.8.2.2 yamt extern void set_spl_masks(void);
110 1.8.2.2 yamt static int fakeintr(void *);
111 1.8.2.2 yamt #ifdef INTR_DEBUG
112 1.8.2.2 yamt static int dumpirqhandlers(void);
113 1.8.2.2 yamt #endif
114 1.8.2.2 yamt void intr_calculatemasks(void);
115 1.8.2.2 yamt
116 1.8.2.2 yamt const struct evcnt *sa11x0_intr_evcnt(sa11x0_chipset_tag_t, int);
117 1.8.2.2 yamt void stray_irqhandler(void *);
118 1.8.2.2 yamt
119 1.8.2.2 yamt /*
120 1.8.2.2 yamt * Recalculate the interrupt masks from scratch.
121 1.8.2.2 yamt * We could code special registry and deregistry versions of this function that
122 1.8.2.2 yamt * would be faster, but the code would be nastier, and we don't expect this to
123 1.8.2.2 yamt * happen very much anyway.
124 1.8.2.2 yamt */
125 1.8.2.2 yamt void
126 1.8.2.2 yamt intr_calculatemasks(void)
127 1.8.2.2 yamt {
128 1.8.2.2 yamt int irq, level;
129 1.8.2.2 yamt struct irqhandler *q;
130 1.8.2.2 yamt int intrlevel[ICU_LEN];
131 1.8.2.2 yamt
132 1.8.2.2 yamt /* First, figure out which levels each IRQ uses. */
133 1.8.2.2 yamt for (irq = 0; irq < ICU_LEN; irq++) {
134 1.8.2.2 yamt int levels = 0;
135 1.8.2.2 yamt for (q = irqhandlers[irq]; q; q = q->ih_next)
136 1.8.2.2 yamt levels |= 1 << q->ih_level;
137 1.8.2.2 yamt intrlevel[irq] = levels;
138 1.8.2.2 yamt }
139 1.8.2.2 yamt
140 1.8.2.2 yamt /* Then figure out which IRQs use each level. */
141 1.8.2.2 yamt #ifdef hpcarm
142 1.8.2.2 yamt for (level = 0; level < NIPL; level++) {
143 1.8.2.2 yamt #else
144 1.8.2.2 yamt for (level = 0; level <= IPL_LEVELS; level++) {
145 1.8.2.2 yamt #endif
146 1.8.2.2 yamt int irqs = 0;
147 1.8.2.2 yamt for (irq = 0; irq < ICU_LEN; irq++)
148 1.8.2.2 yamt if (intrlevel[irq] & (1 << level))
149 1.8.2.2 yamt irqs |= 1 << irq;
150 1.8.2.2 yamt #ifdef hpcarm
151 1.8.2.2 yamt imask[level] = irqs;
152 1.8.2.2 yamt #else
153 1.8.2.2 yamt irqmasks[level] = irqs;
154 1.8.2.2 yamt #endif
155 1.8.2.2 yamt }
156 1.8.2.2 yamt
157 1.8.2.2 yamt /*
158 1.8.2.2 yamt * Enforce a hierarchy that gives slow devices a better chance at not
159 1.8.2.2 yamt * dropping data.
160 1.8.2.2 yamt */
161 1.8.2.2 yamt #ifdef hpcarm
162 1.8.2.2 yamt for (level = NIPL - 1; level > 0; level--)
163 1.8.2.2 yamt imask[level - 1] |= imask[level];
164 1.8.2.2 yamt #else
165 1.8.2.2 yamt for (level = IPL_LEVELS; level > 0; level--)
166 1.8.2.2 yamt irqmasks[level - 1] |= irqmasks[level];
167 1.8.2.2 yamt #endif
168 1.8.2.2 yamt }
169 1.8.2.2 yamt
170 1.8.2.2 yamt
171 1.8.2.2 yamt const struct evcnt *
172 1.8.2.2 yamt sa11x0_intr_evcnt(sa11x0_chipset_tag_t ic, int irq)
173 1.8.2.2 yamt {
174 1.8.2.2 yamt
175 1.8.2.2 yamt /* XXX for now, no evcnt parent reported */
176 1.8.2.2 yamt return NULL;
177 1.8.2.2 yamt }
178 1.8.2.2 yamt
179 1.8.2.2 yamt void *
180 1.8.2.2 yamt sa11x0_intr_establish(sa11x0_chipset_tag_t ic, int irq, int type, int level,
181 1.8.2.2 yamt int (*ih_fun)(void *), void *ih_arg)
182 1.8.2.2 yamt {
183 1.8.2.2 yamt int saved_cpsr;
184 1.8.2.2 yamt struct irqhandler **p, *q, *ih;
185 1.8.2.2 yamt static struct irqhandler fakehand = {fakeintr};
186 1.8.2.2 yamt
187 1.8.2.2 yamt /* no point in sleeping unless someone can free memory. */
188 1.8.2.2 yamt ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
189 1.8.2.2 yamt if (ih == NULL)
190 1.8.2.2 yamt panic("sa11x0_intr_establish: can't malloc handler info");
191 1.8.2.2 yamt
192 1.8.2.2 yamt if (irq < 0 || irq >= ICU_LEN || type == IST_NONE)
193 1.8.2.2 yamt panic("intr_establish: bogus irq or type");
194 1.8.2.2 yamt
195 1.8.2.2 yamt /* All interrupts are level intrs. */
196 1.8.2.2 yamt
197 1.8.2.2 yamt /*
198 1.8.2.2 yamt * Figure out where to put the handler.
199 1.8.2.2 yamt * This is O(N^2), but we want to preserve the order, and N is
200 1.8.2.2 yamt * generally small.
201 1.8.2.2 yamt */
202 1.8.2.2 yamt for (p = &irqhandlers[irq]; (q = *p) != NULL; p = &q->ih_next)
203 1.8.2.2 yamt ;
204 1.8.2.2 yamt
205 1.8.2.2 yamt /*
206 1.8.2.2 yamt * Actually install a fake handler momentarily, since we might be doing
207 1.8.2.2 yamt * this with interrupts enabled and don't want the real routine called
208 1.8.2.2 yamt * until masking is set up.
209 1.8.2.2 yamt */
210 1.8.2.2 yamt fakehand.ih_level = level;
211 1.8.2.2 yamt *p = &fakehand;
212 1.8.2.2 yamt
213 1.8.2.2 yamt intr_calculatemasks();
214 1.8.2.2 yamt
215 1.8.2.2 yamt /*
216 1.8.2.2 yamt * Poke the real handler in now.
217 1.8.2.2 yamt */
218 1.8.2.2 yamt ih->ih_func = ih_fun;
219 1.8.2.2 yamt ih->ih_arg = ih_arg;
220 1.8.2.2 yamt #ifdef hpcarm
221 1.8.2.2 yamt ih->ih_count = 0;
222 1.8.2.2 yamt #else
223 1.8.2.2 yamt ih->ih_num = 0;
224 1.8.2.2 yamt #endif
225 1.8.2.2 yamt ih->ih_next = NULL;
226 1.8.2.2 yamt ih->ih_level = level;
227 1.8.2.2 yamt #ifdef hpcarm
228 1.8.2.2 yamt ih->ih_irq = irq;
229 1.8.2.2 yamt #endif
230 1.8.2.2 yamt ih->ih_name = NULL; /* XXX */
231 1.8.2.2 yamt *p = ih;
232 1.8.2.2 yamt
233 1.8.2.2 yamt saved_cpsr = SetCPSR(I32_bit, I32_bit);
234 1.8.2.2 yamt set_spl_masks();
235 1.8.2.2 yamt
236 1.8.2.2 yamt irq_setmasks();
237 1.8.2.2 yamt
238 1.8.2.2 yamt SetCPSR(I32_bit, saved_cpsr & I32_bit);
239 1.8.2.2 yamt #ifdef INTR_DEBUG
240 1.8.2.2 yamt dumpirqhandlers();
241 1.8.2.2 yamt #endif
242 1.8.2.2 yamt return (ih);
243 1.8.2.2 yamt }
244 1.8.2.2 yamt
245 1.8.2.2 yamt #ifdef hpcarm
246 1.8.2.2 yamt /*
247 1.8.2.2 yamt * Deregister an interrupt handler.
248 1.8.2.2 yamt */
249 1.8.2.2 yamt void
250 1.8.2.2 yamt sa11x0_intr_disestablish(sa11x0_chipset_tag_t ic, void *arg)
251 1.8.2.2 yamt {
252 1.8.2.2 yamt struct irqhandler *ih = arg;
253 1.8.2.2 yamt int irq = ih->ih_irq;
254 1.8.2.2 yamt int saved_cpsr;
255 1.8.2.2 yamt struct irqhandler **p, *q;
256 1.8.2.2 yamt
257 1.8.2.2 yamt #if DIAGNOSTIC
258 1.8.2.2 yamt if (irq < 0 || irq >= ICU_LEN)
259 1.8.2.2 yamt panic("intr_disestablish: bogus irq");
260 1.8.2.2 yamt #endif
261 1.8.2.2 yamt
262 1.8.2.2 yamt /*
263 1.8.2.2 yamt * Remove the handler from the chain.
264 1.8.2.2 yamt * This is O(n^2), too.
265 1.8.2.2 yamt */
266 1.8.2.2 yamt for (p = &irqhandlers[irq]; (q = *p) != NULL && q != ih;
267 1.8.2.2 yamt p = &q->ih_next)
268 1.8.2.2 yamt ;
269 1.8.2.2 yamt if (q)
270 1.8.2.2 yamt *p = q->ih_next;
271 1.8.2.2 yamt else
272 1.8.2.2 yamt panic("intr_disestablish: handler not registered");
273 1.8.2.2 yamt free(ih, M_DEVBUF);
274 1.8.2.2 yamt
275 1.8.2.2 yamt intr_calculatemasks();
276 1.8.2.2 yamt saved_cpsr = SetCPSR(I32_bit, I32_bit);
277 1.8.2.2 yamt set_spl_masks();
278 1.8.2.2 yamt
279 1.8.2.2 yamt irq_setmasks();
280 1.8.2.2 yamt SetCPSR(I32_bit, saved_cpsr & I32_bit);
281 1.8.2.2 yamt
282 1.8.2.2 yamt }
283 1.8.2.2 yamt #endif
284 1.8.2.2 yamt
285 1.8.2.2 yamt void
286 1.8.2.2 yamt stray_irqhandler(void *p)
287 1.8.2.2 yamt {
288 1.8.2.2 yamt
289 1.8.2.2 yamt printf("stray interrupt\n");
290 1.8.2.2 yamt }
291 1.8.2.2 yamt
292 1.8.2.2 yamt int
293 1.8.2.2 yamt fakeintr(void *p)
294 1.8.2.2 yamt {
295 1.8.2.2 yamt
296 1.8.2.2 yamt return 0;
297 1.8.2.2 yamt }
298 1.8.2.2 yamt
299 1.8.2.2 yamt #ifdef INTR_DEBUG
300 1.8.2.2 yamt int
301 1.8.2.2 yamt dumpirqhandlers(void)
302 1.8.2.2 yamt {
303 1.8.2.2 yamt int irq;
304 1.8.2.2 yamt struct irqhandler *p;
305 1.8.2.2 yamt
306 1.8.2.2 yamt for (irq = 0; irq < ICU_LEN; irq++) {
307 1.8.2.2 yamt printf("irq %d:", irq);
308 1.8.2.2 yamt p = irqhandlers[irq];
309 1.8.2.2 yamt for (; p; p = p->ih_next)
310 1.8.2.2 yamt printf("ih_func: 0x%lx, ", (unsigned long)p->ih_func);
311 1.8.2.2 yamt printf("\n");
312 1.8.2.2 yamt }
313 1.8.2.2 yamt return 0;
314 1.8.2.2 yamt }
315 1.8.2.2 yamt #endif
316 1.8.2.2 yamt /* End of irqhandler.c */
317