ofw_irqhandler.c revision 1.7 1 /* $NetBSD: ofw_irqhandler.c,v 1.7 2007/02/18 07:21:32 matt Exp $ */
2
3 /*
4 * Copyright (c) 1994-1998 Mark Brinicombe.
5 * Copyright (c) 1994 Brini.
6 * All rights reserved.
7 *
8 * This code is derived from software written for Brini by Mark Brinicombe
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Mark Brinicombe
21 * for the NetBSD Project.
22 * 4. The name of the company nor the name of the author may be used to
23 * endorse or promote products derived from this software without specific
24 * prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * from: irqhandler.c
38 *
39 * IRQ/FIQ initialisation, claim, release and handler routines
40 *
41 * Created : 30/09/94
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: ofw_irqhandler.c,v 1.7 2007/02/18 07:21:32 matt Exp $");
46
47 #include "opt_irqstats.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/syslog.h>
52 #include <sys/malloc.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <machine/intr.h>
57 #include <machine/cpu.h>
58
59 irqhandler_t *irqhandlers[NIRQS];
60
61 int current_intr_depth;
62 u_int current_mask;
63 u_int actual_mask;
64 u_int disabled_mask;
65 u_int spl_mask;
66 u_int irqmasks[IPL_LEVELS];
67 extern u_int intrcnt[];
68
69 extern u_int soft_interrupts; /* Only so we can initialise it */
70
71 extern char *_intrnames;
72
73 /* Prototypes */
74
75 int podule_irqhandler __P((void));
76 extern void set_spl_masks __P((void));
77
78 /*
79 * void irq_init(void)
80 *
81 * Initialise the IRQ/FIQ sub system
82 */
83
84 void
85 irq_init()
86 {
87 int loop;
88
89 /* Clear all the IRQ handlers and the irq block masks */
90 for (loop = 0; loop < NIRQS; ++loop) {
91 irqhandlers[loop] = NULL;
92 }
93
94 /*
95 * Setup the irqmasks for the different Interrupt Priority Levels
96 * We will start with no bits set and these will be updated as handlers
97 * are installed at different IPL's.
98 */
99 for (loop = 0; loop < IPL_LEVELS; ++loop)
100 irqmasks[loop] = 0;
101
102 current_intr_depth = 0;
103 current_mask = 0x00000000;
104 disabled_mask = 0x00000000;
105 actual_mask = 0x00000000;
106 spl_mask = 0x00000000;
107 soft_interrupts = 0x00000000;
108 softintr_init();
109
110 set_spl_masks();
111
112 /* Enable IRQ's and FIQ's */
113 enable_interrupts(I32_bit | F32_bit);
114 }
115
116
117 /*
118 * int irq_claim(int irq, irqhandler_t *handler)
119 *
120 * Enable an IRQ and install a handler for it.
121 */
122
123 int
124 irq_claim(irq, handler)
125 int irq;
126 irqhandler_t *handler;
127 {
128 int level;
129 int loop;
130
131 #ifdef DIAGNOSTIC
132 /* Sanity check */
133 if (handler == NULL)
134 panic("NULL interrupt handler");
135 if (handler->ih_func == NULL)
136 panic("Interrupt handler does not have a function");
137 #endif /* DIAGNOSTIC */
138
139 /*
140 * IRQ_INSTRUCT indicates that we should get the irq number
141 * from the irq structure
142 */
143 if (irq == IRQ_INSTRUCT)
144 irq = handler->ih_num;
145
146 /* Make sure the irq number is valid */
147 if (irq < 0 || irq >= NIRQS)
148 return(-1);
149
150 /* Make sure the level is valid */
151 if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
152 return(-1);
153
154 /* Attach handler at top of chain */
155 handler->ih_next = irqhandlers[irq];
156 irqhandlers[irq] = handler;
157
158 /*
159 * Reset the flags for this handler.
160 * As the handler is now in the chain mark it as active.
161 */
162 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
163
164 /*
165 * Record the interrupt number for accounting.
166 * Done here as the accounting number may not be the same as the
167 * IRQ number though for the moment they are
168 */
169 handler->ih_num = irq;
170
171 #ifdef IRQSTATS
172 /* Get the interrupt name from the head of the list */
173 if (handler->ih_name) {
174 char *ptr = _intrnames + (irq * 14);
175 strcpy(ptr, " ");
176 strncpy(ptr, handler->ih_name,
177 min(strlen(handler->ih_name), 13));
178 } else {
179 char *ptr = _intrnames + (irq * 14);
180 sprintf(ptr, "irq %2d ", irq);
181 }
182 #endif /* IRQSTATS */
183
184 /*
185 * Update the irq masks.
186 * Find the lowest interrupt priority on the irq chain.
187 * Interrupt is allowable at priorities lower than this.
188 * If ih_level is out of range then don't bother to update
189 * the masks.
190 */
191 if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
192 irqhandler_t *ptr;
193
194 /*
195 * Find the lowest interrupt priority on the irq chain.
196 * Interrupt is allowable at priorities lower than this.
197 */
198 ptr = irqhandlers[irq];
199 if (ptr) {
200 level = ptr->ih_level - 1;
201 while (ptr) {
202 if (ptr->ih_level - 1 < level)
203 level = ptr->ih_level - 1;
204 ptr = ptr->ih_next;
205 }
206 while (level >= 0) {
207 irqmasks[level] |= (1 << irq);
208 --level;
209 }
210 }
211
212 #include "sl.h"
213 #include "ppp.h"
214 #if NSL > 0 || NPPP > 0
215 /* In the presence of SLIP or PPP, splimp > spltty. */
216 irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
217 #endif
218 }
219
220 enable_irq(irq);
221 set_spl_masks();
222
223 return(0);
224 }
225
226
227 /*
228 * int irq_release(int irq, irqhandler_t *handler)
229 *
230 * Disable an IRQ and remove a handler for it.
231 */
232
233 int
234 irq_release(irq, handler)
235 int irq;
236 irqhandler_t *handler;
237 {
238 int level;
239 int loop;
240 irqhandler_t *irqhand;
241 irqhandler_t **prehand;
242 extern char *_intrnames;
243
244 /*
245 * IRQ_INSTRUCT indicates that we should get the irq number
246 * from the irq structure
247 */
248 if (irq == IRQ_INSTRUCT)
249 irq = handler->ih_num;
250
251 /* Make sure the irq number is valid */
252 if (irq < 0 || irq >= NIRQS)
253 return(-1);
254
255 /* Locate the handler */
256 irqhand = irqhandlers[irq];
257 prehand = &irqhandlers[irq];
258
259 while (irqhand && handler != irqhand) {
260 prehand = &irqhand;
261 irqhand = irqhand->ih_next;
262 }
263
264 /* Remove the handler if located */
265 if (irqhand)
266 *prehand = irqhand->ih_next;
267 else
268 return(-1);
269
270 /* Now the handler has been removed from the chain mark is as inactive */
271 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
272
273 /* Make sure the head of the handler list is active */
274 if (irqhandlers[irq])
275 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
276
277 #ifdef IRQSTATS
278 /* Get the interrupt name from the head of the list */
279 if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
280 char *ptr = _intrnames + (irq * 14);
281 strcpy(ptr, " ");
282 strncpy(ptr, irqhandlers[irq]->ih_name,
283 min(strlen(irqhandlers[irq]->ih_name), 13));
284 } else {
285 char *ptr = _intrnames + (irq * 14);
286 sprintf(ptr, "irq %2d ", irq);
287 }
288 #endif /* IRQSTATS */
289
290 /*
291 * Update the irq masks.
292 * If ih_level is out of range then don't bother to update
293 * the masks.
294 */
295 if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
296 irqhandler_t *ptr;
297
298 /* Clean the bit from all the masks */
299 for (level = 0; level < IPL_LEVELS; ++level)
300 irqmasks[level] &= ~(1 << irq);
301
302 /*
303 * Find the lowest interrupt priority on the irq chain.
304 * Interrupt is allowable at priorities lower than this.
305 */
306 ptr = irqhandlers[irq];
307 if (ptr) {
308 level = ptr->ih_level - 1;
309 while (ptr) {
310 if (ptr->ih_level - 1 < level)
311 level = ptr->ih_level - 1;
312 ptr = ptr->ih_next;
313 }
314 while (level >= 0) {
315 irqmasks[level] |= (1 << irq);
316 --level;
317 }
318 }
319 }
320
321 /*
322 * Disable the appropriate mask bit if there are no handlers left for
323 * this IRQ.
324 */
325 if (irqhandlers[irq] == NULL)
326 disable_irq(irq);
327
328 set_spl_masks();
329
330 return(0);
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);
345 if (!ih)
346 panic("intr_claim(): Cannot malloc handler memory");
347
348 ih->ih_level = level;
349 ih->ih_name = name;
350 ih->ih_func = ih_func;
351 ih->ih_arg = ih_arg;
352 ih->ih_flags = 0;
353
354 if (irq_claim(irq, ih) != 0)
355 return(NULL);
356 return(ih);
357 }
358
359
360 int
361 intr_release(arg)
362 void *arg;
363 {
364 irqhandler_t *ih = (irqhandler_t *)arg;
365
366 if (irq_release(ih->ih_num, ih) == 0) {
367 free(ih, M_DEVBUF);
368 return(0);
369 }
370 return(1);
371 }
372
373
374 /*
375 * void disable_irq(int irq)
376 *
377 * Disables a specific irq. The irq is removed from the master irq mask
378 */
379
380 void
381 disable_irq(irq)
382 int irq;
383 {
384 register int oldirqstate;
385
386 oldirqstate = disable_interrupts(I32_bit);
387 current_mask &= ~(1 << irq);
388 irq_setmasks();
389 restore_interrupts(oldirqstate);
390 }
391
392
393 /*
394 * void enable_irq(int irq)
395 *
396 * Enables a specific irq. The irq is added to the master irq mask
397 * This routine should be used with caution. A handler should already
398 * be installed.
399 */
400
401 void
402 enable_irq(irq)
403 int irq;
404 {
405 register u_int oldirqstate;
406
407 oldirqstate = disable_interrupts(I32_bit);
408 current_mask |= (1 << irq);
409 irq_setmasks();
410 restore_interrupts(oldirqstate);
411 }
412
413
414 /*
415 * void stray_irqhandler(u_int mask)
416 *
417 * Handler for stray interrupts. This gets called if a handler cannot be
418 * found for an interrupt.
419 */
420
421 void stray_irqhandler(u_int); /* called from assembly */
422
423 void
424 stray_irqhandler(mask)
425 u_int mask;
426 {
427 static u_int stray_irqs = 0;
428
429 if (++stray_irqs <= 8)
430 log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
431 stray_irqs >= 8 ? ": stopped logging" : "");
432 }
433