ofw_irqhandler.c revision 1.4.12.1 1 /* $NetBSD: ofw_irqhandler.c,v 1.4.12.1 2006/06/21 14:49:34 yamt 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.4.12.1 2006/06/21 14:49:34 yamt 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
109 set_spl_masks();
110
111 /* Enable IRQ's and FIQ's */
112 enable_interrupts(I32_bit | F32_bit);
113 }
114
115
116 /*
117 * int irq_claim(int irq, irqhandler_t *handler)
118 *
119 * Enable an IRQ and install a handler for it.
120 */
121
122 int
123 irq_claim(irq, handler)
124 int irq;
125 irqhandler_t *handler;
126 {
127 int level;
128 int loop;
129
130 #ifdef DIAGNOSTIC
131 /* Sanity check */
132 if (handler == NULL)
133 panic("NULL interrupt handler");
134 if (handler->ih_func == NULL)
135 panic("Interrupt handler does not have a function");
136 #endif /* DIAGNOSTIC */
137
138 /*
139 * IRQ_INSTRUCT indicates that we should get the irq number
140 * from the irq structure
141 */
142 if (irq == IRQ_INSTRUCT)
143 irq = handler->ih_num;
144
145 /* Make sure the irq number is valid */
146 if (irq < 0 || irq >= NIRQS)
147 return(-1);
148
149 /* Make sure the level is valid */
150 if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
151 return(-1);
152
153 /* Attach handler at top of chain */
154 handler->ih_next = irqhandlers[irq];
155 irqhandlers[irq] = handler;
156
157 /*
158 * Reset the flags for this handler.
159 * As the handler is now in the chain mark it as active.
160 */
161 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
162
163 /*
164 * Record the interrupt number for accounting.
165 * Done here as the accounting number may not be the same as the
166 * IRQ number though for the moment they are
167 */
168 handler->ih_num = irq;
169
170 #ifdef IRQSTATS
171 /* Get the interrupt name from the head of the list */
172 if (handler->ih_name) {
173 char *ptr = _intrnames + (irq * 14);
174 strcpy(ptr, " ");
175 strncpy(ptr, handler->ih_name,
176 min(strlen(handler->ih_name), 13));
177 } else {
178 char *ptr = _intrnames + (irq * 14);
179 sprintf(ptr, "irq %2d ", irq);
180 }
181 #endif /* IRQSTATS */
182
183 /*
184 * Update the irq masks.
185 * Find the lowest interrupt priority on the irq chain.
186 * Interrupt is allowable at priorities lower than this.
187 * If ih_level is out of range then don't bother to update
188 * the masks.
189 */
190 if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
191 irqhandler_t *ptr;
192
193 /*
194 * Find the lowest interrupt priority on the irq chain.
195 * Interrupt is allowable at priorities lower than this.
196 */
197 ptr = irqhandlers[irq];
198 if (ptr) {
199 level = ptr->ih_level - 1;
200 while (ptr) {
201 if (ptr->ih_level - 1 < level)
202 level = ptr->ih_level - 1;
203 ptr = ptr->ih_next;
204 }
205 while (level >= 0) {
206 irqmasks[level] |= (1 << irq);
207 --level;
208 }
209 }
210
211 #include "sl.h"
212 #include "ppp.h"
213 #if NSL > 0 || NPPP > 0
214 /* In the presence of SLIP or PPP, splimp > spltty. */
215 irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
216 #endif
217 }
218
219 enable_irq(irq);
220 set_spl_masks();
221
222 return(0);
223 }
224
225
226 /*
227 * int irq_release(int irq, irqhandler_t *handler)
228 *
229 * Disable an IRQ and remove a handler for it.
230 */
231
232 int
233 irq_release(irq, handler)
234 int irq;
235 irqhandler_t *handler;
236 {
237 int level;
238 int loop;
239 irqhandler_t *irqhand;
240 irqhandler_t **prehand;
241 extern char *_intrnames;
242
243 /*
244 * IRQ_INSTRUCT indicates that we should get the irq number
245 * from the irq structure
246 */
247 if (irq == IRQ_INSTRUCT)
248 irq = handler->ih_num;
249
250 /* Make sure the irq number is valid */
251 if (irq < 0 || irq >= NIRQS)
252 return(-1);
253
254 /* Locate the handler */
255 irqhand = irqhandlers[irq];
256 prehand = &irqhandlers[irq];
257
258 while (irqhand && handler != irqhand) {
259 prehand = &irqhand;
260 irqhand = irqhand->ih_next;
261 }
262
263 /* Remove the handler if located */
264 if (irqhand)
265 *prehand = irqhand->ih_next;
266 else
267 return(-1);
268
269 /* Now the handler has been removed from the chain mark is as inactive */
270 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
271
272 /* Make sure the head of the handler list is active */
273 if (irqhandlers[irq])
274 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
275
276 #ifdef IRQSTATS
277 /* Get the interrupt name from the head of the list */
278 if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
279 char *ptr = _intrnames + (irq * 14);
280 strcpy(ptr, " ");
281 strncpy(ptr, irqhandlers[irq]->ih_name,
282 min(strlen(irqhandlers[irq]->ih_name), 13));
283 } else {
284 char *ptr = _intrnames + (irq * 14);
285 sprintf(ptr, "irq %2d ", irq);
286 }
287 #endif /* IRQSTATS */
288
289 /*
290 * Update the irq masks.
291 * If ih_level is out of range then don't bother to update
292 * the masks.
293 */
294 if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
295 irqhandler_t *ptr;
296
297 /* Clean the bit from all the masks */
298 for (level = 0; level < IPL_LEVELS; ++level)
299 irqmasks[level] &= ~(1 << irq);
300
301 /*
302 * Find the lowest interrupt priority on the irq chain.
303 * Interrupt is allowable at priorities lower than this.
304 */
305 ptr = irqhandlers[irq];
306 if (ptr) {
307 level = ptr->ih_level - 1;
308 while (ptr) {
309 if (ptr->ih_level - 1 < level)
310 level = ptr->ih_level - 1;
311 ptr = ptr->ih_next;
312 }
313 while (level >= 0) {
314 irqmasks[level] |= (1 << irq);
315 --level;
316 }
317 }
318 }
319
320 /*
321 * Disable the appropriate mask bit if there are no handlers left for
322 * this IRQ.
323 */
324 if (irqhandlers[irq] == NULL)
325 disable_irq(irq);
326
327 set_spl_masks();
328
329 return(0);
330 }
331
332
333 void *
334 intr_claim(irq, level, name, ih_func, ih_arg)
335 int irq;
336 int level;
337 const char *name;
338 int (*ih_func) __P((void *));
339 void *ih_arg;
340 {
341 irqhandler_t *ih;
342
343 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
344 if (!ih)
345 panic("intr_claim(): Cannot malloc handler memory");
346
347 ih->ih_level = level;
348 ih->ih_name = name;
349 ih->ih_func = ih_func;
350 ih->ih_arg = ih_arg;
351 ih->ih_flags = 0;
352
353 if (irq_claim(irq, ih) != 0)
354 return(NULL);
355 return(ih);
356 }
357
358
359 int
360 intr_release(arg)
361 void *arg;
362 {
363 irqhandler_t *ih = (irqhandler_t *)arg;
364
365 if (irq_release(ih->ih_num, ih) == 0) {
366 free(ih, M_DEVBUF);
367 return(0);
368 }
369 return(1);
370 }
371
372
373 /*
374 * void disable_irq(int irq)
375 *
376 * Disables a specific irq. The irq is removed from the master irq mask
377 */
378
379 void
380 disable_irq(irq)
381 int irq;
382 {
383 register int oldirqstate;
384
385 oldirqstate = disable_interrupts(I32_bit);
386 current_mask &= ~(1 << irq);
387 irq_setmasks();
388 restore_interrupts(oldirqstate);
389 }
390
391
392 /*
393 * void enable_irq(int irq)
394 *
395 * Enables a specific irq. The irq is added to the master irq mask
396 * This routine should be used with caution. A handler should already
397 * be installed.
398 */
399
400 void
401 enable_irq(irq)
402 int irq;
403 {
404 register u_int oldirqstate;
405
406 oldirqstate = disable_interrupts(I32_bit);
407 current_mask |= (1 << irq);
408 irq_setmasks();
409 restore_interrupts(oldirqstate);
410 }
411
412
413 /*
414 * void stray_irqhandler(u_int mask)
415 *
416 * Handler for stray interrupts. This gets called if a handler cannot be
417 * found for an interrupt.
418 */
419
420 void stray_irqhandler(u_int); /* called from assembly */
421
422 void
423 stray_irqhandler(mask)
424 u_int mask;
425 {
426 static u_int stray_irqs = 0;
427
428 if (++stray_irqs <= 8)
429 log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
430 stray_irqs >= 8 ? ": stopped logging" : "");
431 }
432