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