1 /* $NetBSD: ofw_irqhandler.c,v 1.16 2009/03/16 23:11:10 dsl Exp $ */
4 * Copyright (c) 1994-1998 Mark Brinicombe.
5 * Copyright (c) 1994 Brini.
8 * This code is derived from software written for Brini by Mark Brinicombe
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
39 * IRQ/FIQ initialisation, claim, release and handler routines
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: ofw_irqhandler.c,v 1.16 2009/03/16 23:11:10 dsl Exp $");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/syslog.h>
50 #include <sys/malloc.h>
52 #include <uvm/uvm_extern.h>
54 #include <machine/intr.h>
55 #include <machine/irqhandler.h>
56 #include <machine/cpu.h>
58 irqhandler_t
*irqhandlers
[NIRQS
];
63 u_int irqmasks
[IPL_LEVELS
];
64 extern u_int intrcnt
[];
66 extern char *_intrnames
;
70 int podule_irqhandler(void);
71 extern void set_spl_masks(void);
76 * Initialise the IRQ/FIQ sub system
84 /* Clear all the IRQ handlers and the irq block masks */
85 for (loop
= 0; loop
< NIRQS
; ++loop
) {
86 irqhandlers
[loop
] = NULL
;
90 * Setup the irqmasks for the different Interrupt Priority Levels
91 * We will start with no bits set and these will be updated as handlers
92 * are installed at different IPL's.
94 for (loop
= 0; loop
< IPL_LEVELS
; ++loop
)
97 current_mask
= 0x00000000;
98 disabled_mask
= 0x00000000;
99 actual_mask
= 0x00000000;
103 /* Enable IRQ's and FIQ's */
104 enable_interrupts(I32_bit
| F32_bit
);
109 * int irq_claim(int irq, irqhandler_t *handler)
111 * Enable an IRQ and install a handler for it.
115 irq_claim(int irq
, irqhandler_t
*handler
, const char *group
, const char *name
)
122 panic("NULL interrupt handler");
123 if (handler
->ih_func
== NULL
)
124 panic("Interrupt handler does not have a function");
125 #endif /* DIAGNOSTIC */
128 * IRQ_INSTRUCT indicates that we should get the irq number
129 * from the irq structure
131 if (irq
== IRQ_INSTRUCT
)
132 irq
= handler
->ih_num
;
134 /* Make sure the irq number is valid */
135 if (irq
< 0 || irq
>= NIRQS
)
138 /* Make sure the level is valid */
139 if (handler
->ih_level
< 0 || handler
->ih_level
>= IPL_LEVELS
)
142 evcnt_attach_dynamic(&handler
->ih_ev
, EVCNT_TYPE_INTR
, NULL
,
145 /* Attach handler at top of chain */
146 handler
->ih_next
= irqhandlers
[irq
];
147 irqhandlers
[irq
] = handler
;
150 * Reset the flags for this handler.
151 * As the handler is now in the chain mark it as active.
153 handler
->ih_flags
= 0 | IRQ_FLAG_ACTIVE
;
156 * Record the interrupt number for accounting.
157 * Done here as the accounting number may not be the same as the
158 * IRQ number though for the moment they are
160 handler
->ih_num
= irq
;
163 * Update the irq masks.
164 * Find the lowest interrupt priority on the irq chain.
165 * Interrupt is allowable at priorities lower than this.
166 * If ih_level is out of range then don't bother to update
169 if (handler
->ih_level
>= 0 && handler
->ih_level
< IPL_LEVELS
) {
173 * Find the lowest interrupt priority on the irq chain.
174 * Interrupt is allowable at priorities lower than this.
176 ptr
= irqhandlers
[irq
];
178 level
= ptr
->ih_level
- 1;
180 if (ptr
->ih_level
- 1 < level
)
181 level
= ptr
->ih_level
- 1;
185 irqmasks
[level
] |= (1 << irq
);
192 #if NSL > 0 || NPPP > 0
193 /* In the presence of SLIP or PPP, splimp > spltty. */
194 irqmasks
[IPL_NET
] &= irqmasks
[IPL_TTY
];
206 * int irq_release(int irq, irqhandler_t *handler)
208 * Disable an IRQ and remove a handler for it.
212 irq_release(int irq
, irqhandler_t
*handler
)
215 irqhandler_t
*irqhand
;
216 irqhandler_t
**prehand
;
219 * IRQ_INSTRUCT indicates that we should get the irq number
220 * from the irq structure
222 if (irq
== IRQ_INSTRUCT
)
223 irq
= handler
->ih_num
;
225 /* Make sure the irq number is valid */
226 if (irq
< 0 || irq
>= NIRQS
)
229 /* Locate the handler */
230 irqhand
= irqhandlers
[irq
];
231 prehand
= &irqhandlers
[irq
];
233 while (irqhand
&& handler
!= irqhand
) {
235 irqhand
= irqhand
->ih_next
;
238 /* Remove the handler if located */
240 *prehand
= irqhand
->ih_next
;
244 /* Now the handler has been removed from the chain mark is as inactive */
245 irqhand
->ih_flags
&= ~IRQ_FLAG_ACTIVE
;
247 /* Make sure the head of the handler list is active */
248 if (irqhandlers
[irq
])
249 irqhandlers
[irq
]->ih_flags
|= IRQ_FLAG_ACTIVE
;
252 * Update the irq masks.
253 * If ih_level is out of range then don't bother to update
256 if (handler
->ih_level
>= 0 && handler
->ih_level
< IPL_LEVELS
) {
259 /* Clean the bit from all the masks */
260 for (level
= 0; level
< IPL_LEVELS
; ++level
)
261 irqmasks
[level
] &= ~(1 << irq
);
264 * Find the lowest interrupt priority on the irq chain.
265 * Interrupt is allowable at priorities lower than this.
267 ptr
= irqhandlers
[irq
];
269 level
= ptr
->ih_level
- 1;
271 if (ptr
->ih_level
- 1 < level
)
272 level
= ptr
->ih_level
- 1;
276 irqmasks
[level
] |= (1 << irq
);
283 * Disable the appropriate mask bit if there are no handlers left for
286 if (irqhandlers
[irq
] == NULL
)
296 intr_claim(int irq
, int level
, int (*ih_func
)(void *), void *ih_arg
, const char *group
, const char *name
)
300 ih
= malloc(sizeof(*ih
), M_DEVBUF
, M_NOWAIT
|M_ZERO
);
302 panic("intr_claim(): Cannot malloc handler memory");
304 ih
->ih_level
= level
;
305 ih
->ih_func
= ih_func
;
309 if (irq_claim(irq
, ih
, group
, name
) != 0)
316 intr_release(void *arg
)
318 irqhandler_t
*ih
= (irqhandler_t
*)arg
;
320 if (irq_release(ih
->ih_num
, ih
) == 0) {
329 * void disable_irq(int irq)
331 * Disables a specific irq. The irq is removed from the master irq mask
337 register int oldirqstate
;
339 oldirqstate
= disable_interrupts(I32_bit
);
340 current_mask
&= ~(1 << irq
);
342 restore_interrupts(oldirqstate
);
347 * void enable_irq(int irq)
349 * Enables a specific irq. The irq is added to the master irq mask
350 * This routine should be used with caution. A handler should already
357 register u_int oldirqstate
;
359 oldirqstate
= disable_interrupts(I32_bit
);
360 current_mask
|= (1 << irq
);
362 restore_interrupts(oldirqstate
);
367 * void stray_irqhandler(u_int mask)
369 * Handler for stray interrupts. This gets called if a handler cannot be
370 * found for an interrupt.
373 void stray_irqhandler(u_int
); /* called from assembly */
376 stray_irqhandler(u_int mask
)
378 static u_int stray_irqs
= 0;
380 if (++stray_irqs
<= 8)
381 log(LOG_ERR
, "Stray interrupt %08x%s\n", mask
,
382 stray_irqs
>= 8 ? ": stopped logging" : "");