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