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