ofw_irqhandler.c revision 1.18
1/*	$NetBSD: ofw_irqhandler.c,v 1.18 2010/12/20 00:25:28 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.18 2010/12/20 00:25:28 matt Exp $");
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/syslog.h>
50#include <sys/malloc.h>
51
52#include <machine/intr.h>
53#include <machine/irqhandler.h>
54#include <machine/cpu.h>
55
56irqhandler_t *irqhandlers[NIRQS];
57
58u_int current_mask;
59u_int actual_mask;
60u_int disabled_mask;
61u_int irqmasks[IPL_LEVELS];
62extern u_int intrcnt[];
63
64extern char *_intrnames;
65
66/* Prototypes */
67
68int podule_irqhandler(void);
69extern void set_spl_masks(void);
70
71/*
72 * void irq_init(void)
73 *
74 * Initialise the IRQ/FIQ sub system
75 */
76
77void
78irq_init(void)
79{
80	int loop;
81
82	/* Clear all the IRQ handlers and the irq block masks */
83	for (loop = 0; loop < NIRQS; ++loop) {
84		irqhandlers[loop] = NULL;
85	}
86
87	/*
88	 * Setup the irqmasks for the different Interrupt Priority Levels
89	 * We will start with no bits set and these will be updated as handlers
90	 * are installed at different IPL's.
91	 */
92	for (loop = 0; loop < IPL_LEVELS; ++loop)
93		irqmasks[loop] = 0;
94
95	current_mask = 0x00000000;
96	disabled_mask = 0x00000000;
97	actual_mask = 0x00000000;
98
99	set_spl_masks();
100
101	/* Enable IRQ's and FIQ's */
102	enable_interrupts(I32_bit | F32_bit);
103}
104
105
106/*
107 * int irq_claim(int irq, irqhandler_t *handler)
108 *
109 * Enable an IRQ and install a handler for it.
110 */
111
112int
113irq_claim(int irq, irqhandler_t *handler, const char *group, const char *name)
114{
115	int level;
116
117#ifdef DIAGNOSTIC
118	/* Sanity check */
119	if (handler == NULL)
120		panic("NULL interrupt handler");
121	if (handler->ih_func == NULL)
122		panic("Interrupt handler does not have a function");
123#endif	/* DIAGNOSTIC */
124
125	/*
126	 * IRQ_INSTRUCT indicates that we should get the irq number
127	 * from the irq structure
128	 */
129	if (irq == IRQ_INSTRUCT)
130		irq = handler->ih_num;
131
132	/* Make sure the irq number is valid */
133	if (irq < 0 || irq >= NIRQS)
134		return(-1);
135
136	/* Make sure the level is valid */
137	if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
138    	        return(-1);
139
140	evcnt_attach_dynamic(&handler->ih_ev, EVCNT_TYPE_INTR, NULL,
141	    group, name);
142
143	/* Attach handler at top of chain */
144	handler->ih_next = irqhandlers[irq];
145	irqhandlers[irq] = handler;
146
147	/*
148	 * Reset the flags for this handler.
149	 * As the handler is now in the chain mark it as active.
150	 */
151	handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
152
153	/*
154	 * Record the interrupt number for accounting.
155	 * Done here as the accounting number may not be the same as the
156	 * IRQ number though for the moment they are
157	 */
158	handler->ih_num = irq;
159
160	/*
161	 * Update the irq masks.
162	 * Find the lowest interrupt priority on the irq chain.
163	 * Interrupt is allowable at priorities lower than this.
164	 * If ih_level is out of range then don't bother to update
165	 * the masks.
166	 */
167	if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
168		irqhandler_t *ptr;
169
170		/*
171		 * Find the lowest interrupt priority on the irq chain.
172		 * Interrupt is allowable at priorities lower than this.
173		 */
174		ptr = irqhandlers[irq];
175		if (ptr) {
176			level = ptr->ih_level - 1;
177			while (ptr) {
178				if (ptr->ih_level - 1 < level)
179					level = ptr->ih_level - 1;
180				ptr = ptr->ih_next;
181			}
182			while (level >= 0) {
183				irqmasks[level] |= (1 << irq);
184				--level;
185			}
186		}
187
188#include "sl.h"
189#include "ppp.h"
190#if NSL > 0 || NPPP > 0
191		/* In the presence of SLIP or PPP, splimp > spltty. */
192		irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
193#endif
194	}
195
196	enable_irq(irq);
197	set_spl_masks();
198
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
209int
210irq_release(int irq, irqhandler_t *handler)
211{
212	int level;
213	irqhandler_t *irqhand;
214	irqhandler_t **prehand;
215
216	/*
217	 * IRQ_INSTRUCT indicates that we should get the irq number
218	 * from the irq structure
219	 */
220	if (irq == IRQ_INSTRUCT)
221		irq = handler->ih_num;
222
223	/* Make sure the irq number is valid */
224	if (irq < 0 || irq >= NIRQS)
225		return(-1);
226
227	/* Locate the handler */
228	irqhand = irqhandlers[irq];
229	prehand = &irqhandlers[irq];
230
231	while (irqhand && handler != irqhand) {
232		prehand = &irqhand;
233		irqhand = irqhand->ih_next;
234	}
235
236	/* Remove the handler if located */
237	if (irqhand)
238		*prehand = irqhand->ih_next;
239	else
240		return(-1);
241
242	/* Now the handler has been removed from the chain mark is as inactive */
243	irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
244
245	/* Make sure the head of the handler list is active */
246	if (irqhandlers[irq])
247		irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
248
249	/*
250	 * Update the irq masks.
251	 * If ih_level is out of range then don't bother to update
252	 * the masks.
253	 */
254	if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
255		irqhandler_t *ptr;
256
257		/* Clean the bit from all the masks */
258		for (level = 0; level < IPL_LEVELS; ++level)
259			irqmasks[level] &= ~(1 << irq);
260
261		/*
262		 * Find the lowest interrupt priority on the irq chain.
263		 * Interrupt is allowable at priorities lower than this.
264		 */
265		ptr = irqhandlers[irq];
266		if (ptr) {
267			level = ptr->ih_level - 1;
268			while (ptr) {
269				if (ptr->ih_level - 1 < level)
270					level = ptr->ih_level - 1;
271				ptr = ptr->ih_next;
272			}
273			while (level >= 0) {
274				irqmasks[level] |= (1 << irq);
275				--level;
276			}
277		}
278	}
279
280	/*
281	 * Disable the appropriate mask bit if there are no handlers left for
282	 * this IRQ.
283	 */
284	if (irqhandlers[irq] == NULL)
285		disable_irq(irq);
286
287	set_spl_masks();
288
289	return(0);
290}
291
292
293void *
294intr_claim(int irq, int level, int (*ih_func)(void *), void *ih_arg, const char *group, const char *name)
295{
296	irqhandler_t *ih;
297
298	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT|M_ZERO);
299	if (!ih)
300		panic("intr_claim(): Cannot malloc handler memory");
301
302	ih->ih_level = level;
303	ih->ih_func = ih_func;
304	ih->ih_arg = ih_arg;
305	ih->ih_flags = 0;
306
307	if (irq_claim(irq, ih, group, name) != 0)
308		return(NULL);
309	return(ih);
310}
311
312
313int
314intr_release(void *arg)
315{
316	irqhandler_t *ih = (irqhandler_t *)arg;
317
318	if (irq_release(ih->ih_num, ih) == 0) {
319		free(ih, M_DEVBUF);
320		return(0);
321	}
322	return(1);
323}
324
325
326/*
327 * void disable_irq(int irq)
328 *
329 * Disables a specific irq. The irq is removed from the master irq mask
330 */
331
332void
333disable_irq(int irq)
334{
335	register int oldirqstate;
336
337	oldirqstate = disable_interrupts(I32_bit);
338	current_mask &= ~(1 << irq);
339	irq_setmasks();
340	restore_interrupts(oldirqstate);
341}
342
343
344/*
345 * void enable_irq(int irq)
346 *
347 * Enables a specific irq. The irq is added to the master irq mask
348 * This routine should be used with caution. A handler should already
349 * be installed.
350 */
351
352void
353enable_irq(int irq)
354{
355	register u_int oldirqstate;
356
357	oldirqstate = disable_interrupts(I32_bit);
358	current_mask |= (1 << irq);
359	irq_setmasks();
360	restore_interrupts(oldirqstate);
361}
362
363
364/*
365 * void stray_irqhandler(u_int mask)
366 *
367 * Handler for stray interrupts. This gets called if a handler cannot be
368 * found for an interrupt.
369 */
370
371void	stray_irqhandler(u_int);	/* called from assembly */
372
373void
374stray_irqhandler(u_int mask)
375{
376	static u_int stray_irqs = 0;
377
378	if (++stray_irqs <= 8)
379		log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
380		    stray_irqs >= 8 ? ": stopped logging" : "");
381}
382