isa_irqhandler.c revision 1.6 1 /* $NetBSD: isa_irqhandler.c,v 1.6 2003/07/15 03:36:01 lukem 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.6 2003/07/15 03:36:01 lukem Exp $");
79
80 #include "opt_irqstats.h"
81
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/syslog.h>
85 #include <sys/malloc.h>
86
87 #include <uvm/uvm_extern.h>
88
89 #include <machine/intr.h>
90 #include <machine/cpu.h>
91
92 irqhandler_t *irqhandlers[NIRQS];
93
94 int current_intr_depth;
95 u_int current_mask;
96 u_int actual_mask;
97 u_int disabled_mask;
98 u_int spl_mask;
99 u_int irqmasks[IPL_LEVELS];
100 u_int irqblock[NIRQS];
101
102 extern u_int soft_interrupts; /* Only so we can initialise it */
103
104 extern char *_intrnames;
105
106 /* Prototypes */
107
108 extern void set_spl_masks(void);
109 void irq_calculatemasks(void);
110 void stray_irqhandler(u_int);
111
112 #define WriteWord(a, b) *((volatile unsigned int *)(a)) = (b)
113
114 /*
115 * void irq_init(void)
116 *
117 * Initialise the IRQ/FIQ sub system
118 */
119
120 void
121 irq_init()
122 {
123 int loop;
124
125 /* Clear all the IRQ handlers and the irq block masks */
126 for (loop = 0; loop < NIRQS; ++loop) {
127 irqhandlers[loop] = NULL;
128 irqblock[loop] = 0;
129 }
130
131 /*
132 * Setup the irqmasks for the different Interrupt Priority Levels
133 * We will start with no bits set and these will be updated as handlers
134 * are installed at different IPL's.
135 */
136 for (loop = 0; loop < IPL_LEVELS; ++loop)
137 irqmasks[loop] = 0;
138
139 current_intr_depth = 0;
140 current_mask = 0x00000000;
141 disabled_mask = 0x00000000;
142 actual_mask = 0x00000000;
143 spl_mask = 0x00000000;
144 soft_interrupts = 0x00000000;
145
146 set_spl_masks();
147
148 /* Enable IRQ's and FIQ's */
149 enable_interrupts(I32_bit | F32_bit);
150 }
151
152
153 /*
154 * int irq_claim(int irq, irqhandler_t *handler)
155 *
156 * Enable an IRQ and install a handler for it.
157 */
158
159 int
160 irq_claim(irq, handler)
161 int irq;
162 irqhandler_t *handler;
163 {
164
165 #ifdef DIAGNOSTIC
166 /* Sanity check */
167 if (handler == NULL)
168 panic("NULL interrupt handler");
169 if (handler->ih_func == NULL)
170 panic("Interrupt handler does not have a function");
171 #endif /* DIAGNOSTIC */
172
173 /*
174 * IRQ_INSTRUCT indicates that we should get the irq number
175 * from the irq structure
176 */
177 if (irq == IRQ_INSTRUCT)
178 irq = handler->ih_num;
179
180 /* Make sure the irq number is valid */
181 if (irq < 0 || irq >= NIRQS)
182 return(-1);
183
184 /* Make sure the level is valid */
185 if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
186 return(-1);
187
188 /* Attach handler at top of chain */
189 handler->ih_next = irqhandlers[irq];
190 irqhandlers[irq] = handler;
191
192 /*
193 * Reset the flags for this handler.
194 * As the handler is now in the chain mark it as active.
195 */
196 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
197
198 /*
199 * Record the interrupt number for accounting.
200 * Done here as the accounting number may not be the same as the
201 * IRQ number though for the moment they are
202 */
203 handler->ih_num = irq;
204
205 #ifdef IRQSTATS
206 /* Get the interrupt name from the head of the list */
207 if (handler->ih_name) {
208 char *ptr = _intrnames + (irq * 14);
209 strcpy(ptr, " ");
210 strncpy(ptr, handler->ih_name,
211 min(strlen(handler->ih_name), 13));
212 } else {
213 char *ptr = _intrnames + (irq * 14);
214 sprintf(ptr, "irq %2d ", irq);
215 }
216 #endif /* IRQSTATS */
217
218 irq_calculatemasks();
219
220 enable_irq(irq);
221 set_spl_masks();
222 return(0);
223 }
224
225
226 /*
227 * int irq_release(int irq, irqhandler_t *handler)
228 *
229 * Disable an IRQ and remove a handler for it.
230 */
231
232 int
233 irq_release(irq, handler)
234 int irq;
235 irqhandler_t *handler;
236 {
237 irqhandler_t *irqhand;
238 irqhandler_t **prehand;
239 #ifdef IRQSTATS
240 extern char *_intrnames;
241 #endif
242
243 /*
244 * IRQ_INSTRUCT indicates that we should get the irq number
245 * from the irq structure
246 */
247 if (irq == IRQ_INSTRUCT)
248 irq = handler->ih_num;
249
250 /* Make sure the irq number is valid */
251 if (irq < 0 || irq >= NIRQS)
252 return(-1);
253
254 /* Locate the handler */
255 irqhand = irqhandlers[irq];
256 prehand = &irqhandlers[irq];
257
258 while (irqhand && handler != irqhand) {
259 prehand = &irqhand;
260 irqhand = irqhand->ih_next;
261 }
262
263 /* Remove the handler if located */
264 if (irqhand)
265 *prehand = irqhand->ih_next;
266 else
267 return(-1);
268
269 /* Now the handler has been removed from the chain mark is as inactive */
270 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
271
272 /* Make sure the head of the handler list is active */
273 if (irqhandlers[irq])
274 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
275
276 #ifdef IRQSTATS
277 /* Get the interrupt name from the head of the list */
278 if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
279 char *ptr = _intrnames + (irq * 14);
280 strcpy(ptr, " ");
281 strncpy(ptr, irqhandlers[irq]->ih_name,
282 min(strlen(irqhandlers[irq]->ih_name), 13));
283 } else {
284 char *ptr = _intrnames + (irq * 14);
285 sprintf(ptr, "irq %2d ", irq);
286 }
287 #endif /* IRQSTATS */
288
289 irq_calculatemasks();
290
291 /*
292 * Disable the appropriate mask bit if there are no handlers left for
293 * this IRQ.
294 */
295 if (irqhandlers[irq] == NULL)
296 disable_irq(irq);
297
298 set_spl_masks();
299
300 return(0);
301 }
302
303 /* adapted from .../i386/isa/isa_machdep.c */
304 /*
305 * Recalculate the interrupt masks from scratch.
306 * We could code special registry and deregistry versions of this function that
307 * would be faster, but the code would be nastier, and we don't expect this to
308 * happen very much anyway.
309 */
310 void
311 irq_calculatemasks()
312 {
313 int irq, level;
314 irqhandler_t *ptr;
315 int irqlevel[NIRQS];
316
317 /* First, figure out which levels each IRQ uses. */
318 for (irq = 0; irq < NIRQS; irq++) {
319 int levels = 0;
320 for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next)
321 levels |= 1 << ptr->ih_level;
322 irqlevel[irq] = levels;
323 }
324
325 /* Then figure out which IRQs use each level. */
326 for (level = 0; level < IPL_LEVELS; level++) {
327 int irqs = 0;
328 for (irq = 0; irq < NIRQS; irq++)
329 if (irqlevel[irq] & (1 << level))
330 irqs |= 1 << irq;
331 irqmasks[level] = ~irqs;
332 }
333
334 /*
335 * Enforce a hierarchy that gives slow devices a better chance at not
336 * dropping data.
337 */
338 irqmasks[IPL_NET] &= irqmasks[IPL_BIO];
339 irqmasks[IPL_TTY] &= irqmasks[IPL_NET];
340
341 /*
342 * There are tty, network and disk drivers that use free() at interrupt
343 * time, so imp > (tty | net | bio).
344 */
345 irqmasks[IPL_VM] &= irqmasks[IPL_TTY];
346
347 irqmasks[IPL_AUDIO] &= irqmasks[IPL_VM];
348
349 /*
350 * Since run queues may be manipulated by both the statclock and tty,
351 * network, and disk drivers, statclock > (tty | net | bio).
352 */
353 irqmasks[IPL_CLOCK] &= irqmasks[IPL_AUDIO];
354
355 /*
356 * IPL_HIGH must block everything that can manipulate a run queue.
357 */
358 irqmasks[IPL_HIGH] &= irqmasks[IPL_CLOCK];
359
360 /*
361 * We need serial drivers to run at the absolute highest priority to
362 * avoid overruns, so serial > high.
363 */
364 irqmasks[IPL_SERIAL] &= irqmasks[IPL_HIGH];
365
366 /*
367 * We now need to update the irqblock array. This array indicates
368 * what other interrupts should be blocked when interrupt is asserted
369 * This basically emulates hardware interrupt priorities e.g. by
370 * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
371 * is asserted. For each interrupt we find the highest IPL and set
372 * the block mask to the interrupt mask for that level.
373 */
374
375 /* And eventually calculate the complete masks. */
376 for (irq = 0; irq < NIRQS; irq++) {
377 int irqs = 1 << irq;
378 for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next)
379 irqs |= ~(irqmasks[ptr->ih_level]);
380 irqblock[irq] = irqs;
381 }
382 }
383
384
385 void *
386 intr_claim(irq, level, name, ih_func, ih_arg)
387 int irq;
388 int level;
389 const char *name;
390 int (*ih_func) __P((void *));
391 void *ih_arg;
392 {
393 irqhandler_t *ih;
394
395 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
396 if (!ih)
397 panic("intr_claim(): Cannot malloc handler memory");
398
399 ih->ih_level = level;
400 ih->ih_name = name;
401 ih->ih_func = ih_func;
402 ih->ih_arg = ih_arg;
403 ih->ih_flags = 0;
404
405 if (irq_claim(irq, ih) != 0)
406 return(NULL);
407 return(ih);
408 }
409
410 int
411 intr_release(arg)
412 void *arg;
413 {
414 irqhandler_t *ih = (irqhandler_t *)arg;
415
416 if (irq_release(ih->ih_num, ih) == 0) {
417 free(ih, M_DEVBUF);
418 return(0);
419 }
420 return(1);
421 }
422
423
424 /*
425 * void disable_irq(int irq)
426 *
427 * Disables a specific irq. The irq is removed from the master irq mask
428 */
429
430 void
431 disable_irq(irq)
432 int irq;
433 {
434 u_int oldirqstate;
435
436 oldirqstate = disable_interrupts(I32_bit);
437 current_mask &= ~(1 << irq);
438 irq_setmasks();
439 restore_interrupts(oldirqstate);
440 }
441
442
443 /*
444 * void enable_irq(int irq)
445 *
446 * Enables a specific irq. The irq is added to the master irq mask
447 * This routine should be used with caution. A handler should already
448 * be installed.
449 */
450
451 void
452 enable_irq(irq)
453 int irq;
454 {
455 u_int oldirqstate;
456
457 oldirqstate = disable_interrupts(I32_bit);
458 current_mask |= (1 << irq);
459 irq_setmasks();
460 restore_interrupts(oldirqstate);
461 }
462
463
464 /*
465 * void stray_irqhandler(u_int mask)
466 *
467 * Handler for stray interrupts. This gets called if a handler cannot be
468 * found for an interrupt.
469 */
470
471 void
472 stray_irqhandler(mask)
473 u_int mask;
474 {
475 static u_int stray_irqs = 0;
476
477 if (++stray_irqs <= 8)
478 log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
479 stray_irqs >= 8 ? ": stopped logging" : "");
480 }
481