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