footbridge_irqhandler.c revision 1.3 1 1.3 chris /* $NetBSD: footbridge_irqhandler.c,v 1.3 2002/01/05 22:41:48 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.2 matt #include <machine/intr.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.3 chris
66 1.3 chris void stray_irqhandler __P((void));
67 1.1 chris
68 1.1 chris void
69 1.1 chris irq_init(void)
70 1.1 chris {
71 1.1 chris int loop;
72 1.1 chris
73 1.1 chris /* Clear all the IRQ handlers and the irq block masks */
74 1.1 chris for (loop = 0; loop < NIRQS; ++loop) {
75 1.1 chris irqhandlers[loop] = NULL;
76 1.1 chris irqblock[loop] = 0;
77 1.1 chris }
78 1.1 chris
79 1.1 chris /*
80 1.1 chris * Setup the irqmasks for the different Interrupt Priority Levels
81 1.1 chris * We will start with no bits set and these will be updated as handlers
82 1.1 chris * are installed at different IPL's.
83 1.1 chris */
84 1.1 chris for (loop = 0; loop < IPL_LEVELS; ++loop)
85 1.1 chris irqmasks[loop] = 0;
86 1.1 chris
87 1.1 chris current_intr_depth = 0;
88 1.1 chris intr_claimed_mask = 0x00000000;
89 1.1 chris intr_disabled_mask = 0x00000000;
90 1.1 chris intr_current_mask = 0x00000000;
91 1.1 chris spl_mask = 0x00000000;
92 1.1 chris soft_interrupts = 0x00000000;
93 1.1 chris
94 1.1 chris set_spl_masks();
95 1.1 chris irq_setmasks();
96 1.1 chris
97 1.1 chris /* Enable IRQ's and FIQ's */
98 1.1 chris enable_interrupts(I32_bit | F32_bit);
99 1.1 chris }
100 1.1 chris
101 1.1 chris void
102 1.1 chris stray_irqhandler()
103 1.1 chris {
104 1.1 chris panic("stray irq\n");
105 1.1 chris }
106 1.1 chris
107 1.1 chris /*
108 1.1 chris * void disable_irq(int irq)
109 1.1 chris *
110 1.1 chris * Disables a specific irq. The irq is removed from the master irq mask
111 1.1 chris *
112 1.1 chris * Use of this function outside this file is deprecated.
113 1.1 chris */
114 1.1 chris
115 1.1 chris void
116 1.1 chris disable_irq(irq)
117 1.1 chris int irq;
118 1.1 chris {
119 1.1 chris int oldirqstate;
120 1.1 chris
121 1.1 chris oldirqstate = disable_interrupts(I32_bit);
122 1.1 chris intr_claimed_mask &= ~(1 << irq);
123 1.1 chris intr_current_mask = intr_claimed_mask & ~intr_disabled_mask;
124 1.1 chris irq_setmasks();
125 1.1 chris restore_interrupts(oldirqstate);
126 1.1 chris }
127 1.1 chris
128 1.1 chris
129 1.1 chris /*
130 1.1 chris * void enable_irq(int irq)
131 1.1 chris *
132 1.1 chris * Enables a specific irq. The irq is added to the master irq mask
133 1.1 chris * This routine should be used with caution. A handler should already
134 1.1 chris * be installed.
135 1.1 chris *
136 1.1 chris * Use of this function outside this file is deprecated.
137 1.1 chris */
138 1.1 chris
139 1.1 chris void
140 1.1 chris enable_irq(irq)
141 1.1 chris int irq;
142 1.1 chris {
143 1.1 chris u_int oldirqstate;
144 1.1 chris
145 1.1 chris oldirqstate = disable_interrupts(I32_bit);
146 1.1 chris intr_claimed_mask |= (1 << irq);
147 1.1 chris intr_current_mask = intr_claimed_mask & ~intr_disabled_mask;
148 1.1 chris irq_setmasks();
149 1.1 chris restore_interrupts(oldirqstate);
150 1.1 chris }
151 1.1 chris
152 1.1 chris /*
153 1.1 chris * int irq_claim(int irq, irqhandler_t *handler)
154 1.1 chris *
155 1.1 chris * Enable an IRQ and install a handler for it.
156 1.1 chris */
157 1.1 chris
158 1.1 chris int
159 1.1 chris irq_claim(irq, handler)
160 1.1 chris int irq;
161 1.1 chris irqhandler_t *handler;
162 1.1 chris {
163 1.1 chris int level;
164 1.1 chris int loop;
165 1.1 chris
166 1.1 chris #ifdef DIAGNOSTIC
167 1.1 chris /* Sanity check */
168 1.1 chris if (handler == NULL)
169 1.1 chris panic("NULL interrupt handler\n");
170 1.1 chris if (handler->ih_func == NULL)
171 1.1 chris panic("Interrupt handler does not have a function\n");
172 1.1 chris #endif /* DIAGNOSTIC */
173 1.1 chris
174 1.1 chris /*
175 1.1 chris * IRQ_INSTRUCT indicates that we should get the irq number
176 1.1 chris * from the irq structure
177 1.1 chris */
178 1.1 chris if (irq == IRQ_INSTRUCT)
179 1.1 chris irq = handler->ih_num;
180 1.1 chris
181 1.1 chris /* Make sure the irq number is valid */
182 1.1 chris if (irq < 0 || irq >= NIRQS)
183 1.1 chris return(-1);
184 1.1 chris
185 1.1 chris /* Make sure the level is valid */
186 1.1 chris if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
187 1.1 chris return(-1);
188 1.1 chris
189 1.1 chris /* Attach handler at top of chain */
190 1.1 chris handler->ih_next = irqhandlers[irq];
191 1.1 chris irqhandlers[irq] = handler;
192 1.1 chris
193 1.1 chris /*
194 1.1 chris * Reset the flags for this handler.
195 1.1 chris * As the handler is now in the chain mark it as active.
196 1.1 chris */
197 1.1 chris handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
198 1.1 chris
199 1.1 chris /*
200 1.1 chris * Record the interrupt number for accounting.
201 1.1 chris * Done here as the accounting number may not be the same as the IRQ number
202 1.1 chris * though for the moment they are
203 1.1 chris */
204 1.1 chris handler->ih_num = irq;
205 1.1 chris
206 1.1 chris #ifdef IRQSTATS
207 1.1 chris /* Get the interrupt name from the head of the list */
208 1.1 chris if (handler->ih_name) {
209 1.1 chris char *ptr = _intrnames + (irq * 14);
210 1.1 chris strcpy(ptr, " ");
211 1.1 chris strncpy(ptr, handler->ih_name,
212 1.1 chris min(strlen(handler->ih_name), 13));
213 1.1 chris } else {
214 1.1 chris char *ptr = _intrnames + (irq * 14);
215 1.1 chris sprintf(ptr, "irq %2d ", irq);
216 1.1 chris }
217 1.1 chris #endif /* IRQSTATS */
218 1.1 chris
219 1.1 chris /*
220 1.1 chris * Update the irq masks.
221 1.1 chris * If ih_level is out of range then don't bother to update
222 1.1 chris * the masks.
223 1.1 chris */
224 1.1 chris if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
225 1.1 chris irqhandler_t *ptr;
226 1.1 chris
227 1.1 chris /*
228 1.1 chris * Find the lowest interrupt priority on the irq chain.
229 1.1 chris * Interrupt is allowable at priorities lower than this.
230 1.1 chris */
231 1.1 chris ptr = irqhandlers[irq];
232 1.1 chris if (ptr) {
233 1.1 chris level = ptr->ih_level - 1;
234 1.1 chris while (ptr) {
235 1.1 chris if (ptr->ih_level - 1 < level)
236 1.1 chris level = ptr->ih_level - 1;
237 1.1 chris ptr = ptr->ih_next;
238 1.1 chris }
239 1.1 chris for (loop = 0; loop < IPL_LEVELS; ++loop) {
240 1.1 chris if (level >= loop)
241 1.1 chris irqmasks[loop] |= (1 << irq);
242 1.1 chris else
243 1.1 chris irqmasks[loop] &= ~(1 << irq);
244 1.1 chris }
245 1.1 chris }
246 1.1 chris
247 1.1 chris #include "sl.h"
248 1.1 chris #include "ppp.h"
249 1.1 chris #if NSL > 0 || NPPP > 0
250 1.1 chris /* In the presence of SLIP or PPP, splimp > spltty. */
251 1.1 chris irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
252 1.1 chris #endif
253 1.1 chris }
254 1.1 chris
255 1.1 chris /*
256 1.1 chris * We now need to update the irqblock array. This array indicates
257 1.1 chris * what other interrupts should be blocked when interrupt is asserted
258 1.1 chris * This basically emulates hardware interrupt priorities e.g. by blocking
259 1.1 chris * all other IPL_BIO interrupts with an IPL_BIO interrupt is asserted.
260 1.1 chris * For each interrupt we find the highest IPL and set the block mask to
261 1.1 chris * the interrupt mask for that level.
262 1.1 chris */
263 1.1 chris for (loop = 0; loop < NIRQS; ++loop) {
264 1.1 chris irqhandler_t *ptr;
265 1.1 chris
266 1.1 chris ptr = irqhandlers[loop];
267 1.1 chris if (ptr) {
268 1.1 chris /* There is at least 1 handler so scan the chain */
269 1.1 chris level = ptr->ih_level;
270 1.1 chris while (ptr) {
271 1.1 chris if (ptr->ih_level > level)
272 1.1 chris level = ptr->ih_level;
273 1.1 chris ptr = ptr->ih_next;
274 1.1 chris }
275 1.1 chris irqblock[loop] = ~irqmasks[level];
276 1.1 chris } else
277 1.1 chris /* No handlers for this irq so nothing to block */
278 1.1 chris irqblock[loop] = 0;
279 1.1 chris }
280 1.1 chris
281 1.1 chris enable_irq(irq);
282 1.1 chris set_spl_masks();
283 1.1 chris
284 1.1 chris return(0);
285 1.1 chris }
286 1.1 chris
287 1.1 chris
288 1.1 chris /*
289 1.1 chris * int irq_release(int irq, irqhandler_t *handler)
290 1.1 chris *
291 1.1 chris * Disable an IRQ and remove a handler for it.
292 1.1 chris */
293 1.1 chris
294 1.1 chris int
295 1.1 chris irq_release(irq, handler)
296 1.1 chris int irq;
297 1.1 chris irqhandler_t *handler;
298 1.1 chris {
299 1.1 chris int level;
300 1.1 chris int loop;
301 1.1 chris irqhandler_t *irqhand;
302 1.1 chris irqhandler_t **prehand;
303 1.1 chris #ifdef IRQSTATS
304 1.1 chris extern char *_intrnames;
305 1.1 chris #endif /* IRQSTATS */
306 1.1 chris /*
307 1.1 chris * IRQ_INSTRUCT indicates that we should get the irq number
308 1.1 chris * from the irq structure
309 1.1 chris */
310 1.1 chris if (irq == IRQ_INSTRUCT)
311 1.1 chris irq = handler->ih_num;
312 1.1 chris
313 1.1 chris /* Make sure the irq number is valid */
314 1.1 chris if (irq < 0 || irq >= NIRQS)
315 1.1 chris return(-1);
316 1.1 chris
317 1.1 chris /* Locate the handler */
318 1.1 chris irqhand = irqhandlers[irq];
319 1.1 chris prehand = &irqhandlers[irq];
320 1.1 chris
321 1.1 chris while (irqhand && handler != irqhand) {
322 1.1 chris prehand = &irqhand->ih_next;
323 1.1 chris irqhand = irqhand->ih_next;
324 1.1 chris }
325 1.1 chris
326 1.1 chris /* Remove the handler if located */
327 1.1 chris if (irqhand)
328 1.1 chris *prehand = irqhand->ih_next;
329 1.1 chris else
330 1.1 chris return(-1);
331 1.1 chris
332 1.1 chris /* Now the handler has been removed from the chain mark is as inactive */
333 1.1 chris irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
334 1.1 chris
335 1.1 chris /* Make sure the head of the handler list is active */
336 1.1 chris if (irqhandlers[irq])
337 1.1 chris irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
338 1.1 chris
339 1.1 chris #ifdef IRQSTATS
340 1.1 chris /* Get the interrupt name from the head of the list */
341 1.1 chris if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
342 1.1 chris char *ptr = _intrnames + (irq * 14);
343 1.1 chris strcpy(ptr, " ");
344 1.1 chris strncpy(ptr, irqhandlers[irq]->ih_name,
345 1.1 chris min(strlen(irqhandlers[irq]->ih_name), 13));
346 1.1 chris } else {
347 1.1 chris char *ptr = _intrnames + (irq * 14);
348 1.1 chris sprintf(ptr, "irq %2d ", irq);
349 1.1 chris }
350 1.1 chris #endif /* IRQSTATS */
351 1.1 chris
352 1.1 chris /*
353 1.1 chris * Update the irq masks.
354 1.1 chris * If ih_level is out of range then don't bother to update
355 1.1 chris * the masks.
356 1.1 chris */
357 1.1 chris if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
358 1.1 chris irqhandler_t *ptr;
359 1.1 chris
360 1.1 chris /*
361 1.1 chris * Find the lowest interrupt priority on the irq chain.
362 1.1 chris * Interrupt is allowable at priorities lower than this.
363 1.1 chris */
364 1.1 chris ptr = irqhandlers[irq];
365 1.1 chris if (ptr) {
366 1.1 chris level = ptr->ih_level - 1;
367 1.1 chris while (ptr) {
368 1.1 chris if (ptr->ih_level - 1 < level)
369 1.1 chris level = ptr->ih_level - 1;
370 1.1 chris ptr = ptr->ih_next;
371 1.1 chris }
372 1.1 chris for (loop = 0; loop < IPL_LEVELS; ++loop) {
373 1.1 chris if (level >= loop)
374 1.1 chris irqmasks[loop] |= (1 << irq);
375 1.1 chris else
376 1.1 chris irqmasks[loop] &= ~(1 << irq);
377 1.1 chris }
378 1.1 chris }
379 1.1 chris }
380 1.1 chris
381 1.1 chris /*
382 1.1 chris * We now need to update the irqblock array. This array indicates
383 1.1 chris * what other interrupts should be blocked when interrupt is asserted
384 1.1 chris * This basically emulates hardware interrupt priorities e.g. by
385 1.1 chris * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
386 1.1 chris * is asserted. For each interrupt we find the highest IPL and set
387 1.1 chris * the block mask to the interrupt mask for that level.
388 1.1 chris */
389 1.1 chris for (loop = 0; loop < NIRQS; ++loop) {
390 1.1 chris irqhandler_t *ptr;
391 1.1 chris
392 1.1 chris ptr = irqhandlers[loop];
393 1.1 chris if (ptr) {
394 1.1 chris /* There is at least 1 handler so scan the chain */
395 1.1 chris level = ptr->ih_level;
396 1.1 chris while (ptr) {
397 1.1 chris if (ptr->ih_level > level)
398 1.1 chris level = ptr->ih_level;
399 1.1 chris ptr = ptr->ih_next;
400 1.1 chris }
401 1.1 chris irqblock[loop] = ~irqmasks[level];
402 1.1 chris } else
403 1.1 chris /* No handlers for this irq so nothing to block */
404 1.1 chris irqblock[loop] = 0;
405 1.1 chris }
406 1.1 chris
407 1.1 chris /*
408 1.1 chris * Disable the appropriate mask bit if there are no handlers left for
409 1.1 chris * this IRQ.
410 1.1 chris */
411 1.1 chris if (irqhandlers[irq] == NULL)
412 1.1 chris disable_irq(irq);
413 1.1 chris
414 1.1 chris set_spl_masks();
415 1.1 chris
416 1.1 chris return(0);
417 1.1 chris }
418 1.1 chris
419 1.1 chris
420 1.1 chris void *
421 1.1 chris intr_claim(irq, level, name, ih_func, ih_arg)
422 1.1 chris int irq;
423 1.1 chris int level;
424 1.1 chris const char *name;
425 1.1 chris int (*ih_func) __P((void *));
426 1.1 chris void *ih_arg;
427 1.1 chris {
428 1.1 chris irqhandler_t *ih;
429 1.1 chris
430 1.1 chris ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
431 1.1 chris if (!ih)
432 1.1 chris panic("intr_claim(): Cannot malloc handler memory\n");
433 1.1 chris
434 1.1 chris ih->ih_level = level;
435 1.1 chris ih->ih_name = name;
436 1.1 chris ih->ih_func = ih_func;
437 1.1 chris ih->ih_arg = ih_arg;
438 1.1 chris ih->ih_flags = 0;
439 1.1 chris
440 1.1 chris if (irq_claim(irq, ih) != 0)
441 1.1 chris return(NULL);
442 1.1 chris return(ih);
443 1.1 chris }
444 1.1 chris
445 1.1 chris
446 1.1 chris int
447 1.1 chris intr_release(arg)
448 1.1 chris void *arg;
449 1.1 chris {
450 1.1 chris irqhandler_t *ih = (irqhandler_t *)arg;
451 1.1 chris
452 1.1 chris if (irq_release(ih->ih_num, ih) == 0) {
453 1.1 chris free(ih, M_DEVBUF);
454 1.1 chris return(0);
455 1.1 chris }
456 1.1 chris return(1);
457 1.1 chris }
458