iomd_irqhandler.c revision 1.7 1 /* $NetBSD: iomd_irqhandler.c,v 1.7 2003/07/15 00:24:45 lukem 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 * IRQ/FIQ initialisation, claim, release and handler routines
38 *
39 * from: irqhandler.c,v 1.14 1997/04/02 21:52:19 christos Exp $
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: iomd_irqhandler.c,v 1.7 2003/07/15 00:24:45 lukem Exp $");
44
45 #include "opt_irqstats.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/syslog.h>
50 #include <sys/malloc.h>
51 #include <uvm/uvm_extern.h>
52
53 #include <arm/iomd/iomdreg.h>
54 #include <arm/iomd/iomdvar.h>
55
56 #include <machine/intr.h>
57 #include <machine/cpu.h>
58 #include <arm/arm32/katelib.h>
59
60 irqhandler_t *irqhandlers[NIRQS];
61
62 int current_intr_depth;
63 u_int current_mask;
64 u_int actual_mask;
65 u_int disabled_mask;
66 u_int spl_mask;
67 u_int irqmasks[IPL_LEVELS];
68 u_int irqblock[NIRQS];
69
70 extern u_int soft_interrupts; /* Only so we can initialise it */
71
72 extern char *_intrnames;
73
74 /* Prototypes */
75
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 irqblock[loop] = 0;
93 }
94
95 /* Clear the IRQ/FIQ masks in the IOMD */
96 IOMD_WRITE_BYTE(IOMD_IRQMSKA, 0x00);
97 IOMD_WRITE_BYTE(IOMD_IRQMSKB, 0x00);
98
99 switch (IOMD_ID) {
100 case RPC600_IOMD_ID:
101 break;
102 case ARM7500_IOC_ID:
103 case ARM7500FE_IOC_ID:
104 IOMD_WRITE_BYTE(IOMD_IRQMSKC, 0x00);
105 IOMD_WRITE_BYTE(IOMD_IRQMSKD, 0x00);
106 break;
107 default:
108 printf("Unknown IOMD id (%d) found in irq_init()\n", IOMD_ID);
109 };
110
111 IOMD_WRITE_BYTE(IOMD_FIQMSK, 0x00);
112 IOMD_WRITE_BYTE(IOMD_DMAMSK, 0x00);
113
114 /*
115 * Setup the irqmasks for the different Interrupt Priority Levels
116 * We will start with no bits set and these will be updated as handlers
117 * are installed at different IPL's.
118 */
119 for (loop = 0; loop < IPL_LEVELS; ++loop)
120 irqmasks[loop] = 0;
121
122 current_intr_depth = 0;
123 current_mask = 0x00000000;
124 disabled_mask = 0x00000000;
125 actual_mask = 0x00000000;
126 spl_mask = 0x00000000;
127 soft_interrupts = 0x00000000;
128
129 set_spl_masks();
130
131 /* Enable IRQ's and FIQ's */
132 enable_interrupts(I32_bit | F32_bit);
133 }
134
135
136 /*
137 * int irq_claim(int irq, irqhandler_t *handler)
138 *
139 * Enable an IRQ and install a handler for it.
140 */
141
142 int
143 irq_claim(irq, handler)
144 int irq;
145 irqhandler_t *handler;
146 {
147 int level;
148 int loop;
149
150 #ifdef DIAGNOSTIC
151 /* Sanity check */
152 if (handler == NULL)
153 panic("NULL interrupt handler");
154 if (handler->ih_func == NULL)
155 panic("Interrupt handler does not have a function");
156 #endif /* DIAGNOSTIC */
157
158 /*
159 * IRQ_INSTRUCT indicates that we should get the irq number
160 * from the irq structure
161 */
162 if (irq == IRQ_INSTRUCT)
163 irq = handler->ih_num;
164
165 /* Make sure the irq number is valid */
166 if (irq < 0 || irq >= NIRQS)
167 return(-1);
168
169 /* Make sure the level is valid */
170 if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
171 return(-1);
172
173 /* Attach handler at top of chain */
174 handler->ih_next = irqhandlers[irq];
175 irqhandlers[irq] = handler;
176
177 /*
178 * Reset the flags for this handler.
179 * As the handler is now in the chain mark it as active.
180 */
181 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
182
183 /*
184 * Record the interrupt number for accounting.
185 * Done here as the accounting number may not be the same as the
186 * IRQ number though for the moment they are
187 */
188 handler->ih_num = irq;
189
190 #ifdef IRQSTATS
191 /* Get the interrupt name from the head of the list */
192 if (handler->ih_name) {
193 char *ptr = _intrnames + (irq * 14);
194 strcpy(ptr, " ");
195 strncpy(ptr, handler->ih_name,
196 min(strlen(handler->ih_name), 13));
197 } else {
198 char *ptr = _intrnames + (irq * 14);
199 sprintf(ptr, "irq %2d ", irq);
200 }
201 #endif /* IRQSTATS */
202
203 /*
204 * Update the irq masks.
205 * Find the lowest interrupt priority on the irq chain.
206 * Interrupt is allowable at priorities lower than this.
207 * If ih_level is out of range then don't bother to update
208 * the masks.
209 */
210 if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
211 irqhandler_t *ptr;
212
213 /*
214 * Find the lowest interrupt priority on the irq chain.
215 * Interrupt is allowable at priorities lower than this.
216 */
217 ptr = irqhandlers[irq];
218 if (ptr) {
219 int max_level;
220
221 level = ptr->ih_level - 1;
222 max_level = ptr->ih_level - 1;
223 while (ptr) {
224 if (ptr->ih_level - 1 < level)
225 level = ptr->ih_level - 1;
226 else if (ptr->ih_level - 1 > max_level)
227 max_level = ptr->ih_level - 1;
228 ptr = ptr->ih_next;
229 }
230 /* Clear out any levels that we cannot now allow */
231 while (max_level >=0 && max_level > level) {
232 irqmasks[max_level] &= ~(1 << irq);
233 --max_level;
234 }
235 while (level >= 0) {
236 irqmasks[level] |= (1 << irq);
237 --level;
238 }
239 }
240
241 #include "sl.h"
242 #include "ppp.h"
243 #if NSL > 0 || NPPP > 0
244 /* In the presence of SLIP or PPP, splimp > spltty. */
245 irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
246 #endif
247 }
248
249 /*
250 * We now need to update the irqblock array. This array indicates
251 * what other interrupts should be blocked when interrupt is asserted
252 * This basically emulates hardware interrupt priorities e.g. by
253 * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
254 * is asserted. For each interrupt we find the highest IPL and set
255 * the block mask to the interrupt mask for that level.
256 */
257 for (loop = 0; loop < NIRQS; ++loop) {
258 irqhandler_t *ptr;
259
260 ptr = irqhandlers[loop];
261 if (ptr) {
262 /* There is at least 1 handler so scan the chain */
263 level = ptr->ih_level;
264 while (ptr) {
265 if (ptr->ih_level > level)
266 level = ptr->ih_level;
267 ptr = ptr->ih_next;
268 }
269 irqblock[loop] = ~irqmasks[level];
270 } else
271 /* No handlers for this irq so nothing to block */
272 irqblock[loop] = 0;
273 }
274
275 enable_irq(irq);
276 set_spl_masks();
277
278 return(0);
279 }
280
281
282 /*
283 * int irq_release(int irq, irqhandler_t *handler)
284 *
285 * Disable an IRQ and remove a handler for it.
286 */
287
288 int
289 irq_release(irq, handler)
290 int irq;
291 irqhandler_t *handler;
292 {
293 int level;
294 int loop;
295 irqhandler_t *irqhand;
296 irqhandler_t **prehand;
297 #ifdef IRQSTATS
298 extern char *_intrnames;
299 #endif
300
301 /*
302 * IRQ_INSTRUCT indicates that we should get the irq number
303 * from the irq structure
304 */
305 if (irq == IRQ_INSTRUCT)
306 irq = handler->ih_num;
307
308 /* Make sure the irq number is valid */
309 if (irq < 0 || irq >= NIRQS)
310 return(-1);
311
312 /* Locate the handler */
313 irqhand = irqhandlers[irq];
314 prehand = &irqhandlers[irq];
315
316 while (irqhand && handler != irqhand) {
317 prehand = &irqhand->ih_next;
318 irqhand = irqhand->ih_next;
319 }
320
321 /* Remove the handler if located */
322 if (irqhand)
323 *prehand = irqhand->ih_next;
324 else
325 return(-1);
326
327 /* Now the handler has been removed from the chain mark is as inactive */
328 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
329
330 /* Make sure the head of the handler list is active */
331 if (irqhandlers[irq])
332 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
333
334 #ifdef IRQSTATS
335 /* Get the interrupt name from the head of the list */
336 if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
337 char *ptr = _intrnames + (irq * 14);
338 strcpy(ptr, " ");
339 strncpy(ptr, irqhandlers[irq]->ih_name,
340 min(strlen(irqhandlers[irq]->ih_name), 13));
341 } else {
342 char *ptr = _intrnames + (irq * 14);
343 sprintf(ptr, "irq %2d ", irq);
344 }
345 #endif /* IRQSTATS */
346
347 /*
348 * Update the irq masks.
349 * If ih_level is out of range then don't bother to update
350 * the masks.
351 */
352 if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
353 irqhandler_t *ptr;
354
355 /* Clean the bit from all the masks */
356 for (level = 0; level < IPL_LEVELS; ++level)
357 irqmasks[level] &= ~(1 << irq);
358
359 /*
360 * Find the lowest interrupt priority on the irq chain.
361 * Interrupt is allowable at priorities lower than this.
362 */
363 ptr = irqhandlers[irq];
364 if (ptr) {
365 level = ptr->ih_level - 1;
366 while (ptr) {
367 if (ptr->ih_level - 1 < level)
368 level = ptr->ih_level - 1;
369 ptr = ptr->ih_next;
370 }
371 while (level >= 0) {
372 irqmasks[level] |= (1 << irq);
373 --level;
374 }
375 }
376 }
377
378 /*
379 * We now need to update the irqblock array. This array indicates
380 * what other interrupts should be blocked when interrupt is asserted
381 * This basically emulates hardware interrupt priorities e.g. by
382 * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
383 * is asserted. For each interrupt we find the highest IPL and set
384 * the block mask to the interrupt mask for that level.
385 */
386 for (loop = 0; loop < NIRQS; ++loop) {
387 irqhandler_t *ptr;
388
389 ptr = irqhandlers[loop];
390 if (ptr) {
391 /* There is at least 1 handler so scan the chain */
392 level = ptr->ih_level;
393 while (ptr) {
394 if (ptr->ih_level > level)
395 level = ptr->ih_level;
396 ptr = ptr->ih_next;
397 }
398 irqblock[loop] = ~irqmasks[level];
399 } else
400 /* No handlers for this irq so nothing to block */
401 irqblock[loop] = 0;
402 }
403
404 /*
405 * Disable the appropriate mask bit if there are no handlers left for
406 * this IRQ.
407 */
408 if (irqhandlers[irq] == NULL)
409 disable_irq(irq);
410
411 set_spl_masks();
412
413 return(0);
414 }
415
416
417 void *
418 intr_claim(irq, level, name, ih_func, ih_arg)
419 int irq;
420 int level;
421 const char *name;
422 int (*ih_func) __P((void *));
423 void *ih_arg;
424 {
425 irqhandler_t *ih;
426
427 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
428 if (!ih)
429 panic("intr_claim(): Cannot malloc handler memory");
430
431 ih->ih_level = level;
432 ih->ih_name = name;
433 ih->ih_func = ih_func;
434 ih->ih_arg = ih_arg;
435 ih->ih_flags = 0;
436
437 if (irq_claim(irq, ih) != 0)
438 return(NULL);
439 return(ih);
440 }
441
442
443 int
444 intr_release(arg)
445 void *arg;
446 {
447 irqhandler_t *ih = (irqhandler_t *)arg;
448
449 if (irq_release(ih->ih_num, ih) == 0) {
450 free(ih, M_DEVBUF);
451 return(0);
452 }
453 return(1);
454 }
455
456 #if 0
457 u_int
458 disable_interrupts(mask)
459 u_int mask;
460 {
461 u_int cpsr;
462
463 cpsr = SetCPSR(mask, mask);
464 return(cpsr);
465 }
466
467
468 u_int
469 restore_interrupts(old_cpsr)
470 u_int old_cpsr;
471 {
472 int mask = I32_bit | F32_bit;
473 return(SetCPSR(mask, old_cpsr & mask));
474 }
475
476
477 u_int
478 enable_interrupts(mask)
479 u_int mask;
480 {
481 return(SetCPSR(mask, 0));
482 }
483 #endif
484
485 /*
486 * void disable_irq(int irq)
487 *
488 * Disables a specific irq. The irq is removed from the master irq mask
489 */
490
491 void
492 disable_irq(irq)
493 int irq;
494 {
495 u_int oldirqstate;
496
497 oldirqstate = disable_interrupts(I32_bit);
498 current_mask &= ~(1 << irq);
499 irq_setmasks();
500 restore_interrupts(oldirqstate);
501 }
502
503
504 /*
505 * void enable_irq(int irq)
506 *
507 * Enables a specific irq. The irq is added to the master irq mask
508 * This routine should be used with caution. A handler should already
509 * be installed.
510 */
511
512 void
513 enable_irq(irq)
514 int irq;
515 {
516 u_int oldirqstate;
517
518 oldirqstate = disable_interrupts(I32_bit);
519 current_mask |= (1 << irq);
520 irq_setmasks();
521 restore_interrupts(oldirqstate);
522 }
523
524
525 /*
526 * void stray_irqhandler(u_int mask)
527 *
528 * Handler for stray interrupts. This gets called if a handler cannot be
529 * found for an interrupt.
530 */
531
532 void
533 stray_irqhandler(mask)
534 u_int mask;
535 {
536 static u_int stray_irqs = 0;
537
538 if (++stray_irqs <= 8)
539 log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
540 stray_irqs >= 8 ? ": stopped logging" : "");
541 }
542