Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / platform / pc32 / isa / intr_machdep.c
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      from: @(#)isa.c 7.2 (Berkeley) 5/13/91
37  * $FreeBSD: src/sys/i386/isa/intr_machdep.c,v 1.29.2.5 2001/10/14 06:54:27 luigi Exp $
38  * $DragonFly: src/sys/platform/pc32/isa/intr_machdep.c,v 1.2 2003/06/17 04:28:37 dillon Exp $
39  */
40 /*
41  * This file contains an aggregated module marked:
42  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
43  * All rights reserved.
44  * See the notice for details.
45  */
46
47 #include "opt_auto_eoi.h"
48
49 #include "isa.h"
50
51 #include <sys/param.h>
52 #ifndef SMP
53 #include <machine/lock.h>
54 #endif
55 #include <sys/systm.h>
56 #include <sys/syslog.h>
57 #include <sys/malloc.h>
58 #include <sys/errno.h>
59 #include <sys/interrupt.h>
60 #include <machine/ipl.h>
61 #include <machine/md_var.h>
62 #include <machine/segments.h>
63 #include <sys/bus.h> 
64
65 #if defined(APIC_IO)
66 #include <machine/smp.h>
67 #include <machine/smptests.h>                   /** FAST_HI */
68 #endif /* APIC_IO */
69 #ifdef PC98
70 #include <pc98/pc98/pc98.h>
71 #include <pc98/pc98/pc98_machdep.h>
72 #include <pc98/pc98/epsonio.h>
73 #else
74 #include <i386/isa/isa.h>
75 #endif
76 #include <i386/isa/icu.h>
77
78 #if NISA > 0
79 #include <isa/isavar.h>
80 #endif
81 #include <i386/isa/intr_machdep.h>
82 #include <sys/interrupt.h>
83 #ifdef APIC_IO
84 #include <machine/clock.h>
85 #endif
86
87 #include "mca.h"
88 #if NMCA > 0
89 #include <i386/isa/mca_machdep.h>
90 #endif
91
92 /* XXX should be in suitable include files */
93 #ifdef PC98
94 #define ICU_IMR_OFFSET          2               /* IO_ICU{1,2} + 2 */
95 #define ICU_SLAVEID                     7
96 #else
97 #define ICU_IMR_OFFSET          1               /* IO_ICU{1,2} + 1 */
98 #define ICU_SLAVEID                     2
99 #endif
100
101 #ifdef APIC_IO
102 /*
103  * This is to accommodate "mixed-mode" programming for 
104  * motherboards that don't connect the 8254 to the IO APIC.
105  */
106 #define AUTO_EOI_1      1
107 #endif
108
109 #define NR_INTRNAMES    (1 + ICU_LEN + 2 * ICU_LEN)
110
111 u_long  *intr_countp[ICU_LEN];
112 inthand2_t *intr_handler[ICU_LEN];
113 u_int   intr_mask[ICU_LEN];
114 static u_int*   intr_mptr[ICU_LEN];
115 void    *intr_unit[ICU_LEN];
116
117 static inthand_t *fastintr[ICU_LEN] = {
118         &IDTVEC(fastintr0), &IDTVEC(fastintr1),
119         &IDTVEC(fastintr2), &IDTVEC(fastintr3),
120         &IDTVEC(fastintr4), &IDTVEC(fastintr5),
121         &IDTVEC(fastintr6), &IDTVEC(fastintr7),
122         &IDTVEC(fastintr8), &IDTVEC(fastintr9),
123         &IDTVEC(fastintr10), &IDTVEC(fastintr11),
124         &IDTVEC(fastintr12), &IDTVEC(fastintr13),
125         &IDTVEC(fastintr14), &IDTVEC(fastintr15),
126 #if defined(APIC_IO)
127         &IDTVEC(fastintr16), &IDTVEC(fastintr17),
128         &IDTVEC(fastintr18), &IDTVEC(fastintr19),
129         &IDTVEC(fastintr20), &IDTVEC(fastintr21),
130         &IDTVEC(fastintr22), &IDTVEC(fastintr23),
131 #endif /* APIC_IO */
132 };
133
134 static inthand_t *slowintr[ICU_LEN] = {
135         &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
136         &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
137         &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
138         &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15),
139 #if defined(APIC_IO)
140         &IDTVEC(intr16), &IDTVEC(intr17), &IDTVEC(intr18), &IDTVEC(intr19),
141         &IDTVEC(intr20), &IDTVEC(intr21), &IDTVEC(intr22), &IDTVEC(intr23),
142 #endif /* APIC_IO */
143 };
144
145 static inthand2_t isa_strayintr;
146
147 #ifdef PC98
148 #define NMI_PARITY 0x04
149 #define NMI_EPARITY 0x02
150 #else
151 #define NMI_PARITY (1 << 7)
152 #define NMI_IOCHAN (1 << 6)
153 #define ENMI_WATCHDOG (1 << 7)
154 #define ENMI_BUSTIMER (1 << 6)
155 #define ENMI_IOSTATUS (1 << 5)
156 #endif
157
158 /*
159  * Handle a NMI, possibly a machine check.
160  * return true to panic system, false to ignore.
161  */
162 int
163 isa_nmi(cd)
164         int cd;
165 {
166         int retval = 0;
167 #ifdef PC98
168         int port = inb(0x33);
169
170         log(LOG_CRIT, "NMI PC98 port = %x\n", port);
171         if (epson_machine_id == 0x20)
172                 epson_outb(0xc16, epson_inb(0xc16) | 0x1);
173         if (port & NMI_PARITY) {
174                 log(LOG_CRIT, "BASE RAM parity error, likely hardware failure.");
175                 retval = 1;
176         } else if (port & NMI_EPARITY) {
177                 log(LOG_CRIT, "EXTENDED RAM parity error, likely hardware failure.");
178                 retval = 1;
179         } else {
180                 log(LOG_CRIT, "\nNMI Resume ??\n");
181         }
182 #else /* IBM-PC */
183         int isa_port = inb(0x61);
184         int eisa_port = inb(0x461);
185
186         log(LOG_CRIT, "NMI ISA %x, EISA %x\n", isa_port, eisa_port);
187 #if NMCA > 0
188         if (MCA_system && mca_bus_nmi())
189                 return(0);
190 #endif
191         
192         if (isa_port & NMI_PARITY) {
193                 log(LOG_CRIT, "RAM parity error, likely hardware failure.");
194                 retval = 1;
195         }
196
197         if (isa_port & NMI_IOCHAN) {
198                 log(LOG_CRIT, "I/O channel check, likely hardware failure.");
199                 retval = 1;
200         }
201
202         /*
203          * On a real EISA machine, this will never happen.  However it can
204          * happen on ISA machines which implement XT style floating point
205          * error handling (very rare).  Save them from a meaningless panic.
206          */
207         if (eisa_port == 0xff)
208                 return(retval);
209
210         if (eisa_port & ENMI_WATCHDOG) {
211                 log(LOG_CRIT, "EISA watchdog timer expired, likely hardware failure.");
212                 retval = 1;
213         }
214
215         if (eisa_port & ENMI_BUSTIMER) {
216                 log(LOG_CRIT, "EISA bus timeout, likely hardware failure.");
217                 retval = 1;
218         }
219
220         if (eisa_port & ENMI_IOSTATUS) {
221                 log(LOG_CRIT, "EISA I/O port status error.");
222                 retval = 1;
223         }
224 #endif
225         return(retval);
226 }
227
228 /*
229  * Fill in default interrupt table (in case of spuruious interrupt
230  * during configuration of kernel, setup interrupt control unit
231  */
232 void
233 isa_defaultirq()
234 {
235         int i;
236
237         /* icu vectors */
238         for (i = 0; i < ICU_LEN; i++)
239                 icu_unset(i, (inthand2_t *)NULL);
240
241         /* initialize 8259's */
242 #if NMCA > 0
243         if (MCA_system)
244                 outb(IO_ICU1, 0x19);            /* reset; program device, four bytes */
245         else
246 #endif
247                 outb(IO_ICU1, 0x11);            /* reset; program device, four bytes */
248
249         outb(IO_ICU1+ICU_IMR_OFFSET, NRSVIDT);  /* starting at this vector index */
250         outb(IO_ICU1+ICU_IMR_OFFSET, IRQ_SLAVE);                /* slave on line 7 */
251 #ifdef PC98
252 #ifdef AUTO_EOI_1
253         outb(IO_ICU1+ICU_IMR_OFFSET, 0x1f);             /* (master) auto EOI, 8086 mode */
254 #else
255         outb(IO_ICU1+ICU_IMR_OFFSET, 0x1d);             /* (master) 8086 mode */
256 #endif
257 #else /* IBM-PC */
258 #ifdef AUTO_EOI_1
259         outb(IO_ICU1+ICU_IMR_OFFSET, 2 | 1);            /* auto EOI, 8086 mode */
260 #else
261         outb(IO_ICU1+ICU_IMR_OFFSET, 1);                /* 8086 mode */
262 #endif
263 #endif /* PC98 */
264         outb(IO_ICU1+ICU_IMR_OFFSET, 0xff);             /* leave interrupts masked */
265         outb(IO_ICU1, 0x0a);            /* default to IRR on read */
266 #ifndef PC98
267         outb(IO_ICU1, 0xc0 | (3 - 1));  /* pri order 3-7, 0-2 (com2 first) */
268 #endif /* !PC98 */
269
270 #if NMCA > 0
271         if (MCA_system)
272                 outb(IO_ICU2, 0x19);            /* reset; program device, four bytes */
273         else
274 #endif
275                 outb(IO_ICU2, 0x11);            /* reset; program device, four bytes */
276
277         outb(IO_ICU2+ICU_IMR_OFFSET, NRSVIDT+8); /* staring at this vector index */
278         outb(IO_ICU2+ICU_IMR_OFFSET, ICU_SLAVEID);         /* my slave id is 7 */
279 #ifdef PC98
280         outb(IO_ICU2+ICU_IMR_OFFSET,9);              /* 8086 mode */
281 #else /* IBM-PC */
282 #ifdef AUTO_EOI_2
283         outb(IO_ICU2+ICU_IMR_OFFSET, 2 | 1);            /* auto EOI, 8086 mode */
284 #else
285         outb(IO_ICU2+ICU_IMR_OFFSET,1);         /* 8086 mode */
286 #endif
287 #endif /* PC98 */
288         outb(IO_ICU2+ICU_IMR_OFFSET, 0xff);          /* leave interrupts masked */
289         outb(IO_ICU2, 0x0a);            /* default to IRR on read */
290 }
291
292 /*
293  * Caught a stray interrupt, notify
294  */
295 static void
296 isa_strayintr(vcookiep)
297         void *vcookiep;
298 {
299         int intr = (void **)vcookiep - &intr_unit[0];
300
301         /* DON'T BOTHER FOR NOW! */
302         /* for some reason, we get bursts of intr #7, even if not enabled! */
303         /*
304          * Well the reason you got bursts of intr #7 is because someone
305          * raised an interrupt line and dropped it before the 8259 could
306          * prioritize it.  This is documented in the intel data book.  This
307          * means you have BAD hardware!  I have changed this so that only
308          * the first 5 get logged, then it quits logging them, and puts
309          * out a special message. rgrimes 3/25/1993
310          */
311         /*
312          * XXX TODO print a different message for #7 if it is for a
313          * glitch.  Glitches can be distinguished from real #7's by
314          * testing that the in-service bit is _not_ set.  The test
315          * must be done before sending an EOI so it can't be done if
316          * we are using AUTO_EOI_1.
317          */
318         if (intrcnt[1 + intr] <= 5)
319                 log(LOG_ERR, "stray irq %d\n", intr);
320         if (intrcnt[1 + intr] == 5)
321                 log(LOG_CRIT,
322                     "too many stray irq %d's; not logging any more\n", intr);
323 }
324
325 #if NISA > 0
326 /*
327  * Return a bitmap of the current interrupt requests.  This is 8259-specific
328  * and is only suitable for use at probe time.
329  */
330 intrmask_t
331 isa_irq_pending()
332 {
333         u_char irr1;
334         u_char irr2;
335
336         irr1 = inb(IO_ICU1);
337         irr2 = inb(IO_ICU2);
338         return ((irr2 << 8) | irr1);
339 }
340 #endif
341
342 int
343 update_intr_masks(void)
344 {
345         int intr, n=0;
346         u_int mask,*maskptr;
347
348         for (intr=0; intr < ICU_LEN; intr ++) {
349 #if defined(APIC_IO)
350                 /* no 8259 SLAVE to ignore */
351 #else
352                 if (intr==ICU_SLAVEID) continue;        /* ignore 8259 SLAVE output */
353 #endif /* APIC_IO */
354                 maskptr = intr_mptr[intr];
355                 if (!maskptr)
356                         continue;
357                 *maskptr |= SWI_CLOCK_MASK | (1 << intr);
358                 mask = *maskptr;
359                 if (mask != intr_mask[intr]) {
360 #if 0
361                         printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
362                                 intr, intr_mask[intr], mask, maskptr);
363 #endif
364                         intr_mask[intr]=mask;
365                         n++;
366                 }
367
368         }
369         return (n);
370 }
371
372 static void
373 update_intrname(int intr, char *name)
374 {
375         char buf[32];
376         char *cp;
377         int name_index, off, strayintr;
378
379         /*
380          * Initialise strings for bitbucket and stray interrupt counters.
381          * These have statically allocated indices 0 and 1 through ICU_LEN.
382          */
383         if (intrnames[0] == '\0') {
384                 off = sprintf(intrnames, "???") + 1;
385                 for (strayintr = 0; strayintr < ICU_LEN; strayintr++)
386                         off += sprintf(intrnames + off, "stray irq%d",
387                             strayintr) + 1;
388         }
389
390         if (name == NULL)
391                 name = "???";
392         if (snprintf(buf, sizeof(buf), "%s irq%d", name, intr) >= sizeof(buf))
393                 goto use_bitbucket;
394
395         /*
396          * Search for `buf' in `intrnames'.  In the usual case when it is
397          * not found, append it to the end if there is enough space (the \0
398          * terminator for the previous string, if any, becomes a separator).
399          */
400         for (cp = intrnames, name_index = 0;
401             cp != eintrnames && name_index < NR_INTRNAMES;
402             cp += strlen(cp) + 1, name_index++) {
403                 if (*cp == '\0') {
404                         if (strlen(buf) >= eintrnames - cp)
405                                 break;
406                         strcpy(cp, buf);
407                         goto found;
408                 }
409                 if (strcmp(cp, buf) == 0)
410                         goto found;
411         }
412
413 use_bitbucket:
414         printf("update_intrname: counting %s irq%d as %s\n", name, intr,
415             intrnames);
416         name_index = 0;
417 found:
418         intr_countp[intr] = &intrcnt[name_index];
419 }
420
421 int
422 icu_setup(int intr, inthand2_t *handler, void *arg, u_int *maskptr, int flags)
423 {
424 #ifdef FAST_HI
425         int             select;         /* the select register is 8 bits */
426         int             vector;
427         u_int32_t       value;          /* the window register is 32 bits */
428 #endif /* FAST_HI */
429         u_long  ef;
430         u_int   mask = (maskptr ? *maskptr : 0);
431
432 #if defined(APIC_IO)
433         if ((u_int)intr >= ICU_LEN)     /* no 8259 SLAVE to ignore */
434 #else
435         if ((u_int)intr >= ICU_LEN || intr == ICU_SLAVEID)
436 #endif /* APIC_IO */
437         if (intr_handler[intr] != isa_strayintr)
438                 return (EBUSY);
439
440         ef = read_eflags();
441         disable_intr();
442         intr_handler[intr] = handler;
443         intr_mptr[intr] = maskptr;
444         intr_mask[intr] = mask | SWI_CLOCK_MASK | (1 << intr);
445         intr_unit[intr] = arg;
446 #ifdef FAST_HI
447         if (flags & INTR_FAST) {
448                 vector = TPR_FAST_INTS + intr;
449                 setidt(vector, fastintr[intr],
450                        SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
451         }
452         else {
453                 vector = TPR_SLOW_INTS + intr;
454 #ifdef APIC_INTR_REORDER
455 #ifdef APIC_INTR_HIGHPRI_CLOCK
456                 /* XXX: Hack (kludge?) for more accurate clock. */
457                 if (intr == apic_8254_intr || intr == 8) {
458                         vector = TPR_FAST_INTS + intr;
459                 }
460 #endif
461 #endif
462                 setidt(vector, slowintr[intr],
463                        SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
464         }
465 #ifdef APIC_INTR_REORDER
466         set_lapic_isrloc(intr, vector);
467 #endif
468         /*
469          * Reprogram the vector in the IO APIC.
470          */
471         if (int_to_apicintpin[intr].ioapic >= 0) {
472                 select = int_to_apicintpin[intr].redirindex;
473                 value = io_apic_read(int_to_apicintpin[intr].ioapic, 
474                                      select) & ~IOART_INTVEC;
475                 io_apic_write(int_to_apicintpin[intr].ioapic, 
476                               select, value | vector);
477         }
478 #else
479         setidt(ICU_OFFSET + intr,
480                flags & INTR_FAST ? fastintr[intr] : slowintr[intr],
481                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
482 #endif /* FAST_HI */
483         INTREN(1 << intr);
484         MPINTR_UNLOCK();
485         write_eflags(ef);
486         return (0);
487 }
488
489 int
490 icu_unset(intr, handler)
491         int     intr;
492         inthand2_t *handler;
493 {
494         u_long  ef;
495
496         if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
497                 return (EINVAL);
498
499         INTRDIS(1 << intr);
500         ef = read_eflags();
501         disable_intr();
502         intr_countp[intr] = &intrcnt[1 + intr];
503         intr_handler[intr] = isa_strayintr;
504         intr_mptr[intr] = NULL;
505         intr_mask[intr] = HWI_MASK | SWI_MASK;
506         intr_unit[intr] = &intr_unit[intr];
507 #ifdef FAST_HI_XXX
508         /* XXX how do I re-create dvp here? */
509         setidt(flags & INTR_FAST ? TPR_FAST_INTS + intr : TPR_SLOW_INTS + intr,
510             slowintr[intr], SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
511 #else /* FAST_HI */
512 #ifdef APIC_INTR_REORDER
513         set_lapic_isrloc(intr, ICU_OFFSET + intr);
514 #endif
515         setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
516             GSEL(GCODE_SEL, SEL_KPL));
517 #endif /* FAST_HI */
518         MPINTR_UNLOCK();
519         write_eflags(ef);
520         return (0);
521 }
522
523 /* The following notice applies beyond this point in the file */
524
525 /*
526  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
527  * All rights reserved.
528  *
529  * Redistribution and use in source and binary forms, with or without
530  * modification, are permitted provided that the following conditions
531  * are met:
532  * 1. Redistributions of source code must retain the above copyright
533  *    notice unmodified, this list of conditions, and the following
534  *    disclaimer.
535  * 2. Redistributions in binary form must reproduce the above copyright
536  *    notice, this list of conditions and the following disclaimer in the
537  *    documentation and/or other materials provided with the distribution.
538  *
539  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
540  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
541  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
542  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
543  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
544  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
545  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
546  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
547  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
548  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
549  *
550  * $FreeBSD: src/sys/i386/isa/intr_machdep.c,v 1.29.2.5 2001/10/14 06:54:27 luigi Exp $
551  *
552  */
553
554 typedef struct intrec {
555         intrmask_t      mask;
556         inthand2_t      *handler;
557         void            *argument;
558         struct intrec   *next;
559         char            *name;
560         int             intr;
561         intrmask_t      *maskptr;
562         int             flags;
563 } intrec;
564
565 static intrec *intreclist_head[ICU_LEN];
566
567 /*
568  * The interrupt multiplexer calls each of the handlers in turn.  The
569  * ipl is initially quite low.  It is raised as necessary for each call
570  * and lowered after the call.  Thus out of order handling is possible
571  * even for interrupts of the same type.  This is probably no more
572  * harmful than out of order handling in general (not harmful except
573  * for real time response which we don't support anyway).
574  */
575 static void
576 intr_mux(void *arg)
577 {
578         intrec *p;
579         intrmask_t oldspl;
580
581         for (p = arg; p != NULL; p = p->next) {
582                 oldspl = splq(p->mask);
583                 p->handler(p->argument);
584                 splx(oldspl);
585         }
586 }
587
588 static intrec*
589 find_idesc(unsigned *maskptr, int irq)
590 {
591         intrec *p = intreclist_head[irq];
592
593         while (p && p->maskptr != maskptr)
594                 p = p->next;
595
596         return (p);
597 }
598
599 static intrec**
600 find_pred(intrec *idesc, int irq)
601 {
602         intrec **pp = &intreclist_head[irq];
603         intrec *p = *pp;
604
605         while (p != idesc) {
606                 if (p == NULL)
607                         return (NULL);
608                 pp = &p->next;
609                 p = *pp;
610         }
611         return (pp);
612 }
613
614 /*
615  * Both the low level handler and the shared interrupt multiplexer
616  * block out further interrupts as set in the handlers "mask", while
617  * the handler is running. In fact *maskptr should be used for this
618  * purpose, but since this requires one more pointer dereference on
619  * each interrupt, we rather bother update "mask" whenever *maskptr
620  * changes. The function "update_masks" should be called **after**
621  * all manipulation of the linked list of interrupt handlers hung
622  * off of intrdec_head[irq] is complete, since the chain of handlers
623  * will both determine the *maskptr values and the instances of mask
624  * that are fixed. This function should be called with the irq for
625  * which a new handler has been add blocked, since the masks may not
626  * yet know about the use of this irq for a device of a certain class.
627  */
628
629 static void
630 update_mux_masks(void)
631 {
632         int irq;
633         for (irq = 0; irq < ICU_LEN; irq++) {
634                 intrec *idesc = intreclist_head[irq];
635                 while (idesc != NULL) {
636                         if (idesc->maskptr != NULL) {
637                                 /* our copy of *maskptr may be stale, refresh */
638                                 idesc->mask = *idesc->maskptr;
639                         }
640                         idesc = idesc->next;
641                 }
642         }
643 }
644
645 static void
646 update_masks(intrmask_t *maskptr, int irq)
647 {
648         intrmask_t mask = 1 << irq;
649
650         if (maskptr == NULL)
651                 return;
652
653         if (find_idesc(maskptr, irq) == NULL) {
654                 /* no reference to this maskptr was found in this irq's chain */
655                 *maskptr &= ~mask;
656         } else {
657                 /* a reference to this maskptr was found in this irq's chain */
658                 *maskptr |= mask;
659         }
660         /* we need to update all values in the intr_mask[irq] array */
661         update_intr_masks();
662         /* update mask in chains of the interrupt multiplex handler as well */
663         update_mux_masks();
664 }
665
666 /*
667  * Add interrupt handler to linked list hung off of intreclist_head[irq]
668  * and install shared interrupt multiplex handler, if necessary
669  */
670
671 static int
672 add_intrdesc(intrec *idesc)
673 {
674         int irq = idesc->intr;
675
676         intrec *head = intreclist_head[irq];
677
678         if (head == NULL) {
679                 /* first handler for this irq, just install it */
680                 if (icu_setup(irq, idesc->handler, idesc->argument, 
681                               idesc->maskptr, idesc->flags) != 0)
682                         return (-1);
683
684                 update_intrname(irq, idesc->name);
685                 /* keep reference */
686                 intreclist_head[irq] = idesc;
687         } else {
688                 if ((idesc->flags & INTR_EXCL) != 0
689                     || (head->flags & INTR_EXCL) != 0) {
690                         /*
691                          * can't append new handler, if either list head or
692                          * new handler do not allow interrupts to be shared
693                          */
694                         if (bootverbose)
695                                 printf("\tdevice combination doesn't support "
696                                        "shared irq%d\n", irq);
697                         return (-1);
698                 }
699                 if (head->next == NULL) {
700                         /*
701                          * second handler for this irq, replace device driver's
702                          * handler by shared interrupt multiplexer function
703                          */
704                         icu_unset(irq, head->handler);
705                         if (icu_setup(irq, intr_mux, head, 0, 0) != 0)
706                                 return (-1);
707                         if (bootverbose)
708                                 printf("\tusing shared irq%d.\n", irq);
709                         update_intrname(irq, "mux");
710                 }
711                 /* just append to the end of the chain */
712                 while (head->next != NULL)
713                         head = head->next;
714                 head->next = idesc;
715         }
716         update_masks(idesc->maskptr, irq);
717         return (0);
718 }
719
720 /*
721  * Create and activate an interrupt handler descriptor data structure.
722  *
723  * The dev_instance pointer is required for resource management, and will
724  * only be passed through to resource_claim().
725  *
726  * There will be functions that derive a driver and unit name from a
727  * dev_instance variable, and those functions will be used to maintain the
728  * interrupt counter label array referenced by systat and vmstat to report
729  * device interrupt rates (->update_intrlabels).
730  *
731  * Add the interrupt handler descriptor data structure created by an
732  * earlier call of create_intr() to the linked list for its irq and
733  * adjust the interrupt masks if necessary.
734  *
735  * WARNING: This is an internal function and not to be used by device
736  * drivers.  It is subject to change without notice.
737  */
738
739 intrec *
740 inthand_add(const char *name, int irq, inthand2_t handler, void *arg,
741              intrmask_t *maskptr, int flags)
742 {
743         intrec *idesc;
744         int errcode = -1;
745         intrmask_t oldspl;
746
747         if (ICU_LEN > 8 * sizeof *maskptr) {
748                 printf("create_intr: ICU_LEN of %d too high for %d bit intrmask\n",
749                        ICU_LEN, 8 * sizeof *maskptr);
750                 return (NULL);
751         }
752         if ((unsigned)irq >= ICU_LEN) {
753                 printf("create_intr: requested irq%d too high, limit is %d\n",
754                        irq, ICU_LEN -1);
755                 return (NULL);
756         }
757
758         idesc = malloc(sizeof *idesc, M_DEVBUF, M_WAITOK);
759         if (idesc == NULL)
760                 return NULL;
761         bzero(idesc, sizeof *idesc);
762
763         if (name == NULL)
764                 name = "???";
765         idesc->name     = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK);
766         if (idesc->name == NULL) {
767                 free(idesc, M_DEVBUF);
768                 return NULL;
769         }
770         strcpy(idesc->name, name);
771
772         idesc->handler  = handler;
773         idesc->argument = arg;
774         idesc->maskptr  = maskptr;
775         idesc->intr     = irq;
776         idesc->flags    = flags;
777
778         /* block this irq */
779         oldspl = splq(1 << irq);
780
781         /* add irq to class selected by maskptr */
782         errcode = add_intrdesc(idesc);
783         splx(oldspl);
784
785         if (errcode != 0) {
786                 if (bootverbose)
787                         printf("\tintr_connect(irq%d) failed, result=%d\n", 
788                                irq, errcode);
789                 free(idesc->name, M_DEVBUF);
790                 free(idesc, M_DEVBUF);
791                 idesc = NULL;
792         }
793
794         return (idesc);
795 }
796
797 /*
798  * Deactivate and remove the interrupt handler descriptor data connected
799  * created by an earlier call of intr_connect() from the linked list and
800  * adjust theinterrupt masks if necessary.
801  *
802  * Return the memory held by the interrupt handler descriptor data structure
803  * to the system. Make sure, the handler is not actively used anymore, before.
804  */
805
806 int
807 inthand_remove(intrec *idesc)
808 {
809         intrec **hook, *head;
810         int irq;
811         int errcode = 0;
812         intrmask_t oldspl;
813
814         if (idesc == NULL)
815                 return (-1);
816
817         irq = idesc->intr;
818
819         /* find pointer that keeps the reference to this interrupt descriptor */
820         hook = find_pred(idesc, irq);
821         if (hook == NULL)
822                 return (-1);
823
824         /* make copy of original list head, the line after may overwrite it */
825         head = intreclist_head[irq];
826
827         /* unlink: make predecessor point to idesc->next instead of to idesc */
828         *hook = idesc->next;
829
830         /* now check whether the element we removed was the list head */
831         if (idesc == head) {
832
833                 oldspl = splq(1 << irq);
834
835                 /* check whether the new list head is the only element on list */
836                 head = intreclist_head[irq];
837                 if (head != NULL) {
838                         icu_unset(irq, intr_mux);
839                         if (head->next != NULL) {
840                                 /* install the multiplex handler with new list head as argument */
841                                 errcode = icu_setup(irq, intr_mux, head, 0, 0);
842                                 if (errcode == 0)
843                                         update_intrname(irq, NULL);
844                         } else {
845                                 /* install the one remaining handler for this irq */
846                                 errcode = icu_setup(irq, head->handler,
847                                                     head->argument,
848                                                     head->maskptr, head->flags);
849                                 if (errcode == 0)
850                                         update_intrname(irq, head->name);
851                         }
852                 } else {
853                         /* revert to old handler, eg: strayintr */
854                         icu_unset(irq, idesc->handler);
855                 }
856                 splx(oldspl);
857         }
858         update_masks(idesc->maskptr, irq);
859         free(idesc, M_DEVBUF);
860         return (0);
861 }