footbridge_irqhandler.c revision 1.1 1 1.1 chris /* $NetBSD: footbridge_irqhandler.c,v 1.1 2001/06/09 10:29:13 chris Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.1 chris * Copyright (c) 1994-1998 Mark Brinicombe.
5 1.1 chris * Copyright (c) 1997 Causality Limited
6 1.1 chris * Copyright (c) 1994 Brini.
7 1.1 chris * All rights reserved.
8 1.1 chris *
9 1.1 chris * Redistribution and use in source and binary forms, with or without
10 1.1 chris * modification, are permitted provided that the following conditions
11 1.1 chris * are met:
12 1.1 chris * 1. Redistributions of source code must retain the above copyright
13 1.1 chris * notice, this list of conditions and the following disclaimer.
14 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 chris * notice, this list of conditions and the following disclaimer in the
16 1.1 chris * documentation and/or other materials provided with the distribution.
17 1.1 chris * 3. All advertising materials mentioning features or use of this software
18 1.1 chris * must display the following acknowledgement:
19 1.1 chris * This product includes software developed by Mark Brinicombe
20 1.1 chris * for the NetBSD Project.
21 1.1 chris * 4. The name of the company nor the name of the author may be used to
22 1.1 chris * endorse or promote products derived from this software without specific
23 1.1 chris * prior written permission.
24 1.1 chris *
25 1.1 chris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 1.1 chris * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 1.1 chris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.1 chris * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 1.1 chris * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 1.1 chris * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 chris * SUCH DAMAGE.
36 1.1 chris *
37 1.1 chris * from: iomd_irqhandler.c,v 1.16 $
38 1.1 chris */
39 1.1 chris
40 1.1 chris #include "opt_irqstats.h"
41 1.1 chris
42 1.1 chris #include <sys/param.h>
43 1.1 chris #include <sys/systm.h>
44 1.1 chris #include <sys/syslog.h>
45 1.1 chris #include <sys/malloc.h>
46 1.1 chris #include <uvm/uvm_extern.h>
47 1.1 chris
48 1.1 chris #include <machine/irqhandler.h>
49 1.1 chris #include <machine/cpu.h>
50 1.1 chris
51 1.1 chris irqhandler_t *irqhandlers[NIRQS];
52 1.1 chris
53 1.1 chris int current_intr_depth; /* Depth of interrupt nesting */
54 1.1 chris u_int intr_claimed_mask; /* Interrupts that are claimed */
55 1.1 chris u_int intr_disabled_mask; /* Interrupts that are temporarily disabled */
56 1.1 chris u_int intr_current_mask; /* Interrupts currently allowable */
57 1.1 chris u_int spl_mask;
58 1.1 chris u_int irqmasks[IPL_LEVELS];
59 1.1 chris u_int irqblock[NIRQS];
60 1.1 chris
61 1.1 chris extern u_int soft_interrupts; /* Only so we can initialise it */
62 1.1 chris
63 1.1 chris extern char *_intrnames;
64 1.1 chris extern void set_spl_masks __P((void));
65 1.1 chris
66 1.1 chris void
67 1.1 chris irq_init(void)
68 1.1 chris {
69 1.1 chris int loop;
70 1.1 chris
71 1.1 chris /* Clear all the IRQ handlers and the irq block masks */
72 1.1 chris for (loop = 0; loop < NIRQS; ++loop) {
73 1.1 chris irqhandlers[loop] = NULL;
74 1.1 chris irqblock[loop] = 0;
75 1.1 chris }
76 1.1 chris
77 1.1 chris /*
78 1.1 chris * Setup the irqmasks for the different Interrupt Priority Levels
79 1.1 chris * We will start with no bits set and these will be updated as handlers
80 1.1 chris * are installed at different IPL's.
81 1.1 chris */
82 1.1 chris for (loop = 0; loop < IPL_LEVELS; ++loop)
83 1.1 chris irqmasks[loop] = 0;
84 1.1 chris
85 1.1 chris current_intr_depth = 0;
86 1.1 chris intr_claimed_mask = 0x00000000;
87 1.1 chris intr_disabled_mask = 0x00000000;
88 1.1 chris intr_current_mask = 0x00000000;
89 1.1 chris spl_mask = 0x00000000;
90 1.1 chris soft_interrupts = 0x00000000;
91 1.1 chris
92 1.1 chris set_spl_masks();
93 1.1 chris irq_setmasks();
94 1.1 chris
95 1.1 chris /* Enable IRQ's and FIQ's */
96 1.1 chris enable_interrupts(I32_bit | F32_bit);
97 1.1 chris }
98 1.1 chris
99 1.1 chris void
100 1.1 chris stray_irqhandler()
101 1.1 chris {
102 1.1 chris panic("stray irq\n");
103 1.1 chris }
104 1.1 chris
105 1.1 chris /*
106 1.1 chris * void disable_irq(int irq)
107 1.1 chris *
108 1.1 chris * Disables a specific irq. The irq is removed from the master irq mask
109 1.1 chris *
110 1.1 chris * Use of this function outside this file is deprecated.
111 1.1 chris */
112 1.1 chris
113 1.1 chris void
114 1.1 chris disable_irq(irq)
115 1.1 chris int irq;
116 1.1 chris {
117 1.1 chris int oldirqstate;
118 1.1 chris
119 1.1 chris oldirqstate = disable_interrupts(I32_bit);
120 1.1 chris intr_claimed_mask &= ~(1 << irq);
121 1.1 chris intr_current_mask = intr_claimed_mask & ~intr_disabled_mask;
122 1.1 chris irq_setmasks();
123 1.1 chris restore_interrupts(oldirqstate);
124 1.1 chris }
125 1.1 chris
126 1.1 chris
127 1.1 chris /*
128 1.1 chris * void enable_irq(int irq)
129 1.1 chris *
130 1.1 chris * Enables a specific irq. The irq is added to the master irq mask
131 1.1 chris * This routine should be used with caution. A handler should already
132 1.1 chris * be installed.
133 1.1 chris *
134 1.1 chris * Use of this function outside this file is deprecated.
135 1.1 chris */
136 1.1 chris
137 1.1 chris void
138 1.1 chris enable_irq(irq)
139 1.1 chris int irq;
140 1.1 chris {
141 1.1 chris u_int oldirqstate;
142 1.1 chris
143 1.1 chris oldirqstate = disable_interrupts(I32_bit);
144 1.1 chris intr_claimed_mask |= (1 << irq);
145 1.1 chris intr_current_mask = intr_claimed_mask & ~intr_disabled_mask;
146 1.1 chris irq_setmasks();
147 1.1 chris restore_interrupts(oldirqstate);
148 1.1 chris }
149 1.1 chris
150 1.1 chris /*
151 1.1 chris * int irq_claim(int irq, irqhandler_t *handler)
152 1.1 chris *
153 1.1 chris * Enable an IRQ and install a handler for it.
154 1.1 chris */
155 1.1 chris
156 1.1 chris int
157 1.1 chris irq_claim(irq, handler)
158 1.1 chris int irq;
159 1.1 chris irqhandler_t *handler;
160 1.1 chris {
161 1.1 chris int level;
162 1.1 chris int loop;
163 1.1 chris
164 1.1 chris #ifdef DIAGNOSTIC
165 1.1 chris /* Sanity check */
166 1.1 chris if (handler == NULL)
167 1.1 chris panic("NULL interrupt handler\n");
168 1.1 chris if (handler->ih_func == NULL)
169 1.1 chris panic("Interrupt handler does not have a function\n");
170 1.1 chris #endif /* DIAGNOSTIC */
171 1.1 chris
172 1.1 chris /*
173 1.1 chris * IRQ_INSTRUCT indicates that we should get the irq number
174 1.1 chris * from the irq structure
175 1.1 chris */
176 1.1 chris if (irq == IRQ_INSTRUCT)
177 1.1 chris irq = handler->ih_num;
178 1.1 chris
179 1.1 chris /* Make sure the irq number is valid */
180 1.1 chris if (irq < 0 || irq >= NIRQS)
181 1.1 chris return(-1);
182 1.1 chris
183 1.1 chris /* Make sure the level is valid */
184 1.1 chris if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
185 1.1 chris return(-1);
186 1.1 chris
187 1.1 chris /* Attach handler at top of chain */
188 1.1 chris handler->ih_next = irqhandlers[irq];
189 1.1 chris irqhandlers[irq] = handler;
190 1.1 chris
191 1.1 chris /*
192 1.1 chris * Reset the flags for this handler.
193 1.1 chris * As the handler is now in the chain mark it as active.
194 1.1 chris */
195 1.1 chris handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
196 1.1 chris
197 1.1 chris /*
198 1.1 chris * Record the interrupt number for accounting.
199 1.1 chris * Done here as the accounting number may not be the same as the IRQ number
200 1.1 chris * though for the moment they are
201 1.1 chris */
202 1.1 chris handler->ih_num = irq;
203 1.1 chris
204 1.1 chris #ifdef IRQSTATS
205 1.1 chris /* Get the interrupt name from the head of the list */
206 1.1 chris if (handler->ih_name) {
207 1.1 chris char *ptr = _intrnames + (irq * 14);
208 1.1 chris strcpy(ptr, " ");
209 1.1 chris strncpy(ptr, handler->ih_name,
210 1.1 chris min(strlen(handler->ih_name), 13));
211 1.1 chris } else {
212 1.1 chris char *ptr = _intrnames + (irq * 14);
213 1.1 chris sprintf(ptr, "irq %2d ", irq);
214 1.1 chris }
215 1.1 chris #endif /* IRQSTATS */
216 1.1 chris
217 1.1 chris /*
218 1.1 chris * Update the irq masks.
219 1.1 chris * If ih_level is out of range then don't bother to update
220 1.1 chris * the masks.
221 1.1 chris */
222 1.1 chris if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
223 1.1 chris irqhandler_t *ptr;
224 1.1 chris
225 1.1 chris /*
226 1.1 chris * Find the lowest interrupt priority on the irq chain.
227 1.1 chris * Interrupt is allowable at priorities lower than this.
228 1.1 chris */
229 1.1 chris ptr = irqhandlers[irq];
230 1.1 chris if (ptr) {
231 1.1 chris level = ptr->ih_level - 1;
232 1.1 chris while (ptr) {
233 1.1 chris if (ptr->ih_level - 1 < level)
234 1.1 chris level = ptr->ih_level - 1;
235 1.1 chris ptr = ptr->ih_next;
236 1.1 chris }
237 1.1 chris for (loop = 0; loop < IPL_LEVELS; ++loop) {
238 1.1 chris if (level >= loop)
239 1.1 chris irqmasks[loop] |= (1 << irq);
240 1.1 chris else
241 1.1 chris irqmasks[loop] &= ~(1 << irq);
242 1.1 chris }
243 1.1 chris }
244 1.1 chris
245 1.1 chris #include "sl.h"
246 1.1 chris #include "ppp.h"
247 1.1 chris #if NSL > 0 || NPPP > 0
248 1.1 chris /* In the presence of SLIP or PPP, splimp > spltty. */
249 1.1 chris irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
250 1.1 chris #endif
251 1.1 chris }
252 1.1 chris
253 1.1 chris /*
254 1.1 chris * We now need to update the irqblock array. This array indicates
255 1.1 chris * what other interrupts should be blocked when interrupt is asserted
256 1.1 chris * This basically emulates hardware interrupt priorities e.g. by blocking
257 1.1 chris * all other IPL_BIO interrupts with an IPL_BIO interrupt is asserted.
258 1.1 chris * For each interrupt we find the highest IPL and set the block mask to
259 1.1 chris * the interrupt mask for that level.
260 1.1 chris */
261 1.1 chris for (loop = 0; loop < NIRQS; ++loop) {
262 1.1 chris irqhandler_t *ptr;
263 1.1 chris
264 1.1 chris ptr = irqhandlers[loop];
265 1.1 chris if (ptr) {
266 1.1 chris /* There is at least 1 handler so scan the chain */
267 1.1 chris level = ptr->ih_level;
268 1.1 chris while (ptr) {
269 1.1 chris if (ptr->ih_level > level)
270 1.1 chris level = ptr->ih_level;
271 1.1 chris ptr = ptr->ih_next;
272 1.1 chris }
273 1.1 chris irqblock[loop] = ~irqmasks[level];
274 1.1 chris } else
275 1.1 chris /* No handlers for this irq so nothing to block */
276 1.1 chris irqblock[loop] = 0;
277 1.1 chris }
278 1.1 chris
279 1.1 chris enable_irq(irq);
280 1.1 chris set_spl_masks();
281 1.1 chris
282 1.1 chris return(0);
283 1.1 chris }
284 1.1 chris
285 1.1 chris
286 1.1 chris /*
287 1.1 chris * int irq_release(int irq, irqhandler_t *handler)
288 1.1 chris *
289 1.1 chris * Disable an IRQ and remove a handler for it.
290 1.1 chris */
291 1.1 chris
292 1.1 chris int
293 1.1 chris irq_release(irq, handler)
294 1.1 chris int irq;
295 1.1 chris irqhandler_t *handler;
296 1.1 chris {
297 1.1 chris int level;
298 1.1 chris int loop;
299 1.1 chris irqhandler_t *irqhand;
300 1.1 chris irqhandler_t **prehand;
301 1.1 chris #ifdef IRQSTATS
302 1.1 chris extern char *_intrnames;
303 1.1 chris #endif /* IRQSTATS */
304 1.1 chris /*
305 1.1 chris * IRQ_INSTRUCT indicates that we should get the irq number
306 1.1 chris * from the irq structure
307 1.1 chris */
308 1.1 chris if (irq == IRQ_INSTRUCT)
309 1.1 chris irq = handler->ih_num;
310 1.1 chris
311 1.1 chris /* Make sure the irq number is valid */
312 1.1 chris if (irq < 0 || irq >= NIRQS)
313 1.1 chris return(-1);
314 1.1 chris
315 1.1 chris /* Locate the handler */
316 1.1 chris irqhand = irqhandlers[irq];
317 1.1 chris prehand = &irqhandlers[irq];
318 1.1 chris
319 1.1 chris while (irqhand && handler != irqhand) {
320 1.1 chris prehand = &irqhand->ih_next;
321 1.1 chris irqhand = irqhand->ih_next;
322 1.1 chris }
323 1.1 chris
324 1.1 chris /* Remove the handler if located */
325 1.1 chris if (irqhand)
326 1.1 chris *prehand = irqhand->ih_next;
327 1.1 chris else
328 1.1 chris return(-1);
329 1.1 chris
330 1.1 chris /* Now the handler has been removed from the chain mark is as inactive */
331 1.1 chris irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
332 1.1 chris
333 1.1 chris /* Make sure the head of the handler list is active */
334 1.1 chris if (irqhandlers[irq])
335 1.1 chris irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
336 1.1 chris
337 1.1 chris #ifdef IRQSTATS
338 1.1 chris /* Get the interrupt name from the head of the list */
339 1.1 chris if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
340 1.1 chris char *ptr = _intrnames + (irq * 14);
341 1.1 chris strcpy(ptr, " ");
342 1.1 chris strncpy(ptr, irqhandlers[irq]->ih_name,
343 1.1 chris min(strlen(irqhandlers[irq]->ih_name), 13));
344 1.1 chris } else {
345 1.1 chris char *ptr = _intrnames + (irq * 14);
346 1.1 chris sprintf(ptr, "irq %2d ", irq);
347 1.1 chris }
348 1.1 chris #endif /* IRQSTATS */
349 1.1 chris
350 1.1 chris /*
351 1.1 chris * Update the irq masks.
352 1.1 chris * If ih_level is out of range then don't bother to update
353 1.1 chris * the masks.
354 1.1 chris */
355 1.1 chris if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
356 1.1 chris irqhandler_t *ptr;
357 1.1 chris
358 1.1 chris /*
359 1.1 chris * Find the lowest interrupt priority on the irq chain.
360 1.1 chris * Interrupt is allowable at priorities lower than this.
361 1.1 chris */
362 1.1 chris ptr = irqhandlers[irq];
363 1.1 chris if (ptr) {
364 1.1 chris level = ptr->ih_level - 1;
365 1.1 chris while (ptr) {
366 1.1 chris if (ptr->ih_level - 1 < level)
367 1.1 chris level = ptr->ih_level - 1;
368 1.1 chris ptr = ptr->ih_next;
369 1.1 chris }
370 1.1 chris for (loop = 0; loop < IPL_LEVELS; ++loop) {
371 1.1 chris if (level >= loop)
372 1.1 chris irqmasks[loop] |= (1 << irq);
373 1.1 chris else
374 1.1 chris irqmasks[loop] &= ~(1 << irq);
375 1.1 chris }
376 1.1 chris }
377 1.1 chris }
378 1.1 chris
379 1.1 chris /*
380 1.1 chris * We now need to update the irqblock array. This array indicates
381 1.1 chris * what other interrupts should be blocked when interrupt is asserted
382 1.1 chris * This basically emulates hardware interrupt priorities e.g. by
383 1.1 chris * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
384 1.1 chris * is asserted. For each interrupt we find the highest IPL and set
385 1.1 chris * the block mask to the interrupt mask for that level.
386 1.1 chris */
387 1.1 chris for (loop = 0; loop < NIRQS; ++loop) {
388 1.1 chris irqhandler_t *ptr;
389 1.1 chris
390 1.1 chris ptr = irqhandlers[loop];
391 1.1 chris if (ptr) {
392 1.1 chris /* There is at least 1 handler so scan the chain */
393 1.1 chris level = ptr->ih_level;
394 1.1 chris while (ptr) {
395 1.1 chris if (ptr->ih_level > level)
396 1.1 chris level = ptr->ih_level;
397 1.1 chris ptr = ptr->ih_next;
398 1.1 chris }
399 1.1 chris irqblock[loop] = ~irqmasks[level];
400 1.1 chris } else
401 1.1 chris /* No handlers for this irq so nothing to block */
402 1.1 chris irqblock[loop] = 0;
403 1.1 chris }
404 1.1 chris
405 1.1 chris /*
406 1.1 chris * Disable the appropriate mask bit if there are no handlers left for
407 1.1 chris * this IRQ.
408 1.1 chris */
409 1.1 chris if (irqhandlers[irq] == NULL)
410 1.1 chris disable_irq(irq);
411 1.1 chris
412 1.1 chris set_spl_masks();
413 1.1 chris
414 1.1 chris return(0);
415 1.1 chris }
416 1.1 chris
417 1.1 chris
418 1.1 chris void *
419 1.1 chris intr_claim(irq, level, name, ih_func, ih_arg)
420 1.1 chris int irq;
421 1.1 chris int level;
422 1.1 chris const char *name;
423 1.1 chris int (*ih_func) __P((void *));
424 1.1 chris void *ih_arg;
425 1.1 chris {
426 1.1 chris irqhandler_t *ih;
427 1.1 chris
428 1.1 chris ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
429 1.1 chris if (!ih)
430 1.1 chris panic("intr_claim(): Cannot malloc handler memory\n");
431 1.1 chris
432 1.1 chris ih->ih_level = level;
433 1.1 chris ih->ih_name = name;
434 1.1 chris ih->ih_func = ih_func;
435 1.1 chris ih->ih_arg = ih_arg;
436 1.1 chris ih->ih_flags = 0;
437 1.1 chris
438 1.1 chris if (irq_claim(irq, ih) != 0)
439 1.1 chris return(NULL);
440 1.1 chris return(ih);
441 1.1 chris }
442 1.1 chris
443 1.1 chris
444 1.1 chris int
445 1.1 chris intr_release(arg)
446 1.1 chris void *arg;
447 1.1 chris {
448 1.1 chris irqhandler_t *ih = (irqhandler_t *)arg;
449 1.1 chris
450 1.1 chris if (irq_release(ih->ih_num, ih) == 0) {
451 1.1 chris free(ih, M_DEVBUF);
452 1.1 chris return(0);
453 1.1 chris }
454 1.1 chris return(1);
455 1.1 chris }
456