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