isa_irqhandler.c revision 1.21 1 /* $NetBSD: isa_irqhandler.c,v 1.21 2008/04/27 18:58:47 matt Exp $ */
2
3 /*
4 * Copyright 1997
5 * Digital Equipment Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce
14 * and retain this copyright notice and list of conditions as
15 * they appear in the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Digital Equipment Corporation. Neither the "Digital Equipment
19 * Corporation" name nor any trademark or logo of Digital Equipment
20 * Corporation may be used to endorse or promote products derived
21 * from this software without the prior written permission of
22 * Digital Equipment Corporation.
23 *
24 * 3) This software is provided "AS-IS" and any express or implied
25 * warranties, including but not limited to, any implied warranties
26 * of merchantability, fitness for a particular purpose, or
27 * non-infringement are disclaimed. In no event shall DIGITAL be
28 * liable for any damages whatsoever, and in particular, DIGITAL
29 * shall not be liable for special, indirect, consequential, or
30 * incidental damages or damages for lost profits, loss of
31 * revenue or loss of use, whether such damages arise in contract,
32 * negligence, tort, under statute, in equity, at law or otherwise,
33 * even if advised of the possibility of such damage.
34 */
35
36 /*
37 * Copyright (c) 1994-1998 Mark Brinicombe.
38 * Copyright (c) 1994 Brini.
39 * All rights reserved.
40 *
41 * This code is derived from software written for Brini by Mark Brinicombe
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Mark Brinicombe
54 * for the NetBSD Project.
55 * 4. The name of the company nor the name of the author may be used to
56 * endorse or promote products derived from this software without specific
57 * prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 *
70 * IRQ/FIQ initialisation, claim, release and handler routines
71 *
72 * from: irqhandler.c
73 *
74 * Created : 30/09/94
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: isa_irqhandler.c,v 1.21 2008/04/27 18:58:47 matt Exp $");
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/syslog.h>
83 #include <sys/malloc.h>
84
85 #include <uvm/uvm_extern.h>
86
87 #include <machine/intr.h>
88 #include <machine/irqhandler.h>
89 #include <machine/cpu.h>
90
91 irqhandler_t *irqhandlers[NIRQS];
92
93 u_int current_mask;
94 u_int actual_mask;
95 u_int disabled_mask;
96 u_int irqmasks[NIPL];
97
98 /* Prototypes */
99
100 extern void set_spl_masks(void);
101 void irq_calculatemasks(void);
102 void stray_irqhandler(u_int);
103
104 #define WriteWord(a, b) *((volatile unsigned int *)(a)) = (b)
105
106 /*
107 * void irq_init(void)
108 *
109 * Initialise the IRQ/FIQ sub system
110 */
111
112 void
113 irq_init()
114 {
115 int loop;
116
117 /* Clear all the IRQ handlers and the irq block masks */
118 for (loop = 0; loop < NIRQS; ++loop) {
119 irqhandlers[loop] = NULL;
120 }
121
122 /*
123 * Setup the irqmasks for the different Interrupt Priority Levels
124 * We will start with no bits set and these will be updated as handlers
125 * are installed at different IPL's.
126 */
127 for (loop = 0; loop < NIPL; ++loop)
128 irqmasks[loop] = 0;
129
130 current_mask = 0x00000000;
131 disabled_mask = 0x00000000;
132 actual_mask = 0x00000000;
133
134 set_spl_masks();
135
136 /* Enable IRQ's and FIQ's */
137 enable_interrupts(I32_bit | F32_bit);
138 }
139
140
141 /*
142 * int irq_claim(int irq, irqhandler_t *handler)
143 *
144 * Enable an IRQ and install a handler for it.
145 */
146
147 int
148 irq_claim(irq, handler, group, name)
149 int irq;
150 irqhandler_t *handler;
151 const char *group;
152 const char *name;
153 {
154
155 #ifdef DIAGNOSTIC
156 /* Sanity check */
157 if (handler == NULL)
158 panic("NULL interrupt handler");
159 if (handler->ih_func == NULL)
160 panic("Interrupt handler does not have a function");
161 #endif /* DIAGNOSTIC */
162
163 /*
164 * IRQ_INSTRUCT indicates that we should get the irq number
165 * from the irq structure
166 */
167 if (irq == IRQ_INSTRUCT)
168 irq = handler->ih_num;
169
170 /* Make sure the irq number is valid */
171 if (irq < 0 || irq >= NIRQS)
172 return(-1);
173
174 /* Make sure the level is valid */
175 if (handler->ih_level < 0 || handler->ih_level >= NIPL)
176 return(-1);
177
178 /* Attach evcnt */
179 evcnt_attach_dynamic(&handler->ih_ev, EVCNT_TYPE_INTR, NULL,
180 group, name);
181
182 /* Attach handler at top of chain */
183 handler->ih_next = irqhandlers[irq];
184 irqhandlers[irq] = handler;
185
186 /*
187 * Reset the flags for this handler.
188 * As the handler is now in the chain mark it as active.
189 */
190 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
191
192 /*
193 * Record the interrupt number for accounting.
194 * Done here as the accounting number may not be the same as the
195 * IRQ number though for the moment they are
196 */
197 handler->ih_num = irq;
198
199 irq_calculatemasks();
200
201 enable_irq(irq);
202 set_spl_masks();
203 return(0);
204 }
205
206
207 /*
208 * int irq_release(int irq, irqhandler_t *handler)
209 *
210 * Disable an IRQ and remove a handler for it.
211 */
212
213 int
214 irq_release(irq, handler)
215 int irq;
216 irqhandler_t *handler;
217 {
218 irqhandler_t *irqhand;
219 irqhandler_t **prehand;
220
221 /*
222 * IRQ_INSTRUCT indicates that we should get the irq number
223 * from the irq structure
224 */
225 if (irq == IRQ_INSTRUCT)
226 irq = handler->ih_num;
227
228 /* Make sure the irq number is valid */
229 if (irq < 0 || irq >= NIRQS)
230 return(-1);
231
232 /* Locate the handler */
233 prehand = &irqhandlers[irq];
234 irqhand = *prehand;
235
236 while (irqhand && handler != irqhand) {
237 prehand = &irqhand->ih_next;
238 irqhand = *prehand;
239 }
240
241 /* Remove the handler if located */
242 if (irqhand)
243 *prehand = irqhand->ih_next;
244 else
245 return(-1);
246
247 /* The handler has been removed from the chain so mark it as inactive */
248 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
249
250 /* Make sure the head of the handler list is active */
251 if (irqhandlers[irq])
252 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
253
254 evcnt_detach(&irqhand->ih_ev);
255
256 irq_calculatemasks();
257
258 /*
259 * Disable the appropriate mask bit if there are no handlers left for
260 * this IRQ.
261 */
262 if (irqhandlers[irq] == NULL)
263 disable_irq(irq);
264
265 set_spl_masks();
266
267 return(0);
268 }
269
270 /* adapted from .../i386/isa/isa_machdep.c */
271 /*
272 * Recalculate the interrupt masks from scratch.
273 * We could code special registry and deregistry versions of this function that
274 * would be faster, but the code would be nastier, and we don't expect this to
275 * happen very much anyway.
276 */
277 void
278 irq_calculatemasks()
279 {
280 int irq, level;
281 irqhandler_t *ptr;
282 int irqlevel[NIRQS];
283
284 /* First, figure out which levels each IRQ uses. */
285 for (irq = 0; irq < NIRQS; irq++) {
286 int levels = 0;
287 for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next)
288 levels |= 1 << ptr->ih_level;
289 irqlevel[irq] = levels;
290 }
291
292 /* Then figure out which IRQs use each level. */
293 for (level = 0; level < NIPL; level++) {
294 int irqs = 0;
295 for (irq = 0; irq < NIRQS; irq++)
296 if (irqlevel[irq] & (1 << level))
297 irqs |= 1 << irq;
298 irqmasks[level] = ~irqs;
299 }
300
301 /*
302 * Enforce a hierarchy that gives slow devices a better chance at not
303 * dropping data.
304 */
305 KASSERT(irqmasks[IPL_NONE] == ~0);
306 irqmasks[IPL_SOFTCLOCK] &= irqmasks[IPL_NONE];
307 irqmasks[IPL_SOFTBIO] &= irqmasks[IPL_SOFTCLOCK];
308 irqmasks[IPL_SOFTNET] &= irqmasks[IPL_SOFTBIO];
309 irqmasks[IPL_SOFTSERIAL] &= irqmasks[IPL_SOFTNET];
310 irqmasks[IPL_VM] &= irqmasks[IPL_SOFTSERIAL];
311 irqmasks[IPL_CLOCK] &= irqmasks[IPL_VM];
312 irqmasks[IPL_HIGH] &= irqmasks[IPL_CLOCK];
313 }
314
315
316 void *
317 intr_claim(irq, level, ih_func, ih_arg, group, name)
318 int irq;
319 int level;
320 int (*ih_func)(void *);
321 void *ih_arg;
322 const char *group;
323 const char *name;
324 {
325 irqhandler_t *ih;
326
327 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT | M_ZERO);
328 if (!ih)
329 panic("intr_claim(): Cannot malloc handler memory");
330
331 ih->ih_level = level;
332 ih->ih_func = ih_func;
333 ih->ih_arg = ih_arg;
334 ih->ih_flags = 0;
335
336 if (irq_claim(irq, ih, group, name) != 0)
337 return(NULL);
338
339 return(ih);
340 }
341
342 int
343 intr_release(arg)
344 void *arg;
345 {
346 irqhandler_t *ih = (irqhandler_t *)arg;
347
348 if (irq_release(ih->ih_num, ih) == 0) {
349 free(ih, M_DEVBUF);
350 return(0);
351 }
352 return(1);
353 }
354
355
356 /*
357 * void disable_irq(int irq)
358 *
359 * Disables a specific irq. The irq is removed from the master irq mask
360 */
361
362 void
363 disable_irq(irq)
364 int irq;
365 {
366 u_int oldirqstate;
367
368 oldirqstate = disable_interrupts(I32_bit);
369 current_mask &= ~(1 << irq);
370 irq_setmasks();
371 restore_interrupts(oldirqstate);
372 }
373
374
375 /*
376 * void enable_irq(int irq)
377 *
378 * Enables a specific irq. The irq is added to the master irq mask
379 * This routine should be used with caution. A handler should already
380 * be installed.
381 */
382
383 void
384 enable_irq(irq)
385 int irq;
386 {
387 u_int oldirqstate;
388
389 oldirqstate = disable_interrupts(I32_bit);
390 current_mask |= (1 << irq);
391 irq_setmasks();
392 restore_interrupts(oldirqstate);
393 }
394
395
396 /*
397 * void stray_irqhandler(u_int mask)
398 *
399 * Handler for stray interrupts. This gets called if a handler cannot be
400 * found for an interrupt.
401 */
402
403 void
404 stray_irqhandler(mask)
405 u_int mask;
406 {
407 static u_int stray_irqs = 0;
408
409 if (++stray_irqs <= 8)
410 log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
411 stray_irqs >= 8 ? ": stopped logging" : "");
412 }
413