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