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