isa_irqhandler.c revision 1.12 1 /* $NetBSD: isa_irqhandler.c,v 1.12 2007/02/20 01:51:16 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.12 2007/02/20 01:51:16 matt 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
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 }
126
127 /*
128 * Setup the irqmasks for the different Interrupt Priority Levels
129 * We will start with no bits set and these will be updated as handlers
130 * are installed at different IPL's.
131 */
132 for (loop = 0; loop < IPL_LEVELS; ++loop)
133 irqmasks[loop] = 0;
134
135 current_intr_depth = 0;
136 current_mask = 0x00000000;
137 disabled_mask = 0x00000000;
138 actual_mask = 0x00000000;
139 spl_mask = 0x00000000;
140
141 set_spl_masks();
142
143 /* Enable IRQ's and FIQ's */
144 enable_interrupts(I32_bit | F32_bit);
145 }
146
147
148 /*
149 * int irq_claim(int irq, irqhandler_t *handler)
150 *
151 * Enable an IRQ and install a handler for it.
152 */
153
154 int
155 irq_claim(irq, handler)
156 int irq;
157 irqhandler_t *handler;
158 {
159
160 #ifdef DIAGNOSTIC
161 /* Sanity check */
162 if (handler == NULL)
163 panic("NULL interrupt handler");
164 if (handler->ih_func == NULL)
165 panic("Interrupt handler does not have a function");
166 #endif /* DIAGNOSTIC */
167
168 /*
169 * IRQ_INSTRUCT indicates that we should get the irq number
170 * from the irq structure
171 */
172 if (irq == IRQ_INSTRUCT)
173 irq = handler->ih_num;
174
175 /* Make sure the irq number is valid */
176 if (irq < 0 || irq >= NIRQS)
177 return(-1);
178
179 /* Make sure the level is valid */
180 if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
181 return(-1);
182
183 /* Attach handler at top of chain */
184 handler->ih_next = irqhandlers[irq];
185 irqhandlers[irq] = handler;
186
187 /*
188 * Reset the flags for this handler.
189 * As the handler is now in the chain mark it as active.
190 */
191 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
192
193 /*
194 * Record the interrupt number for accounting.
195 * Done here as the accounting number may not be the same as the
196 * IRQ number though for the moment they are
197 */
198 handler->ih_num = irq;
199
200 irq_calculatemasks();
201
202 enable_irq(irq);
203 set_spl_masks();
204 return(0);
205 }
206
207
208 /*
209 * int irq_release(int irq, irqhandler_t *handler)
210 *
211 * Disable an IRQ and remove a handler for it.
212 */
213
214 int
215 irq_release(irq, handler)
216 int irq;
217 irqhandler_t *handler;
218 {
219 irqhandler_t *irqhand;
220 irqhandler_t **prehand;
221
222 /*
223 * IRQ_INSTRUCT indicates that we should get the irq number
224 * from the irq structure
225 */
226 if (irq == IRQ_INSTRUCT)
227 irq = handler->ih_num;
228
229 /* Make sure the irq number is valid */
230 if (irq < 0 || irq >= NIRQS)
231 return(-1);
232
233 /* Locate the handler */
234 irqhand = irqhandlers[irq];
235 prehand = &irqhandlers[irq];
236
237 while (irqhand && handler != irqhand) {
238 prehand = &irqhand;
239 irqhand = irqhand->ih_next;
240 }
241
242 /* Remove the handler if located */
243 if (irqhand)
244 *prehand = irqhand->ih_next;
245 else
246 return(-1);
247
248 /* Now the handler has been removed from the chain mark is as inactive */
249 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
250
251 /* Make sure the head of the handler list is active */
252 if (irqhandlers[irq])
253 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
254
255 irq_calculatemasks();
256
257 /*
258 * Disable the appropriate mask bit if there are no handlers left for
259 * this IRQ.
260 */
261 if (irqhandlers[irq] == NULL)
262 disable_irq(irq);
263
264 set_spl_masks();
265
266 return(0);
267 }
268
269 /* adapted from .../i386/isa/isa_machdep.c */
270 /*
271 * Recalculate the interrupt masks from scratch.
272 * We could code special registry and deregistry versions of this function that
273 * would be faster, but the code would be nastier, and we don't expect this to
274 * happen very much anyway.
275 */
276 void
277 irq_calculatemasks()
278 {
279 int irq, level;
280 irqhandler_t *ptr;
281 int irqlevel[NIRQS];
282
283 /* First, figure out which levels each IRQ uses. */
284 for (irq = 0; irq < NIRQS; irq++) {
285 int levels = 0;
286 for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next)
287 levels |= 1 << ptr->ih_level;
288 irqlevel[irq] = levels;
289 }
290
291 /* Then figure out which IRQs use each level. */
292 for (level = 0; level < IPL_LEVELS; level++) {
293 int irqs = 0;
294 for (irq = 0; irq < NIRQS; irq++)
295 if (irqlevel[irq] & (1 << level))
296 irqs |= 1 << irq;
297 irqmasks[level] = ~irqs;
298 }
299
300 /*
301 * Enforce a hierarchy that gives slow devices a better chance at not
302 * dropping data.
303 */
304 irqmasks[IPL_NET] &= irqmasks[IPL_BIO];
305 irqmasks[IPL_TTY] &= irqmasks[IPL_NET];
306
307 /*
308 * There are tty, network and disk drivers that use free() at interrupt
309 * time, so imp > (tty | net | bio).
310 */
311 irqmasks[IPL_VM] &= irqmasks[IPL_TTY];
312
313 irqmasks[IPL_AUDIO] &= irqmasks[IPL_VM];
314
315 /*
316 * Since run queues may be manipulated by both the statclock and tty,
317 * network, and disk drivers, statclock > (tty | net | bio).
318 */
319 irqmasks[IPL_CLOCK] &= irqmasks[IPL_AUDIO];
320
321 /*
322 * IPL_HIGH must block everything that can manipulate a run queue.
323 */
324 irqmasks[IPL_HIGH] &= irqmasks[IPL_CLOCK];
325
326 /*
327 * We need serial drivers to run at the absolute highest priority to
328 * avoid overruns, so serial > high.
329 */
330 irqmasks[IPL_SERIAL] &= irqmasks[IPL_HIGH];
331 }
332
333
334 void *
335 intr_claim(irq, level, name, ih_func, ih_arg)
336 int irq;
337 int level;
338 const char *name;
339 int (*ih_func) __P((void *));
340 void *ih_arg;
341 {
342 irqhandler_t *ih;
343
344 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT | M_ZERO);
345 if (!ih)
346 panic("intr_claim(): Cannot malloc handler memory");
347
348 ih->ih_level = level;
349 ih->ih_func = ih_func;
350 ih->ih_arg = ih_arg;
351 ih->ih_flags = 0;
352 if (name == NULL) {
353 snprintf(ih->ih_evname, sizeof(ih->ih_evname), "irq %2d", irq);
354 name = ih->ih_evname;
355 }
356 evcnt_attach_dynamic(&ih->ih_ev, EVCNT_TYPE_INTR, NULL, NULL, name);
357
358 if (irq_claim(irq, ih) != 0) {
359 evcnt_detach(&ih->ih_ev);
360 return(NULL);
361 }
362 return(ih);
363 }
364
365 int
366 intr_release(arg)
367 void *arg;
368 {
369 irqhandler_t *ih = (irqhandler_t *)arg;
370
371 if (irq_release(ih->ih_num, ih) == 0) {
372 evcnt_detach(&ih->ih_ev);
373 free(ih, M_DEVBUF);
374 return(0);
375 }
376 return(1);
377 }
378
379
380 /*
381 * void disable_irq(int irq)
382 *
383 * Disables a specific irq. The irq is removed from the master irq mask
384 */
385
386 void
387 disable_irq(irq)
388 int irq;
389 {
390 u_int oldirqstate;
391
392 oldirqstate = disable_interrupts(I32_bit);
393 current_mask &= ~(1 << irq);
394 irq_setmasks();
395 restore_interrupts(oldirqstate);
396 }
397
398
399 /*
400 * void enable_irq(int irq)
401 *
402 * Enables a specific irq. The irq is added to the master irq mask
403 * This routine should be used with caution. A handler should already
404 * be installed.
405 */
406
407 void
408 enable_irq(irq)
409 int irq;
410 {
411 u_int oldirqstate;
412
413 oldirqstate = disable_interrupts(I32_bit);
414 current_mask |= (1 << irq);
415 irq_setmasks();
416 restore_interrupts(oldirqstate);
417 }
418
419
420 /*
421 * void stray_irqhandler(u_int mask)
422 *
423 * Handler for stray interrupts. This gets called if a handler cannot be
424 * found for an interrupt.
425 */
426
427 void
428 stray_irqhandler(mask)
429 u_int mask;
430 {
431 static u_int stray_irqs = 0;
432
433 if (++stray_irqs <= 8)
434 log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
435 stray_irqs >= 8 ? ": stopped logging" : "");
436 }
437