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