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