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