]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/intr_machdep.c
Convert mca (microchannel bus support) from something that we count
[FreeBSD/FreeBSD.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$
38  */
39
40 #include "opt_auto_eoi.h"
41 #include "opt_isa.h"
42 #include "opt_mca.h"
43
44 #include <sys/param.h>
45 #include <sys/bus.h>
46 #ifndef SMP
47 #include <machine/lock.h>
48 #endif
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/syslog.h>
52 #include <sys/ipl.h>
53 #include <sys/kernel.h>
54 #include <sys/kthread.h>
55 #include <sys/malloc.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/unistd.h>
59 #include <sys/errno.h>
60 #include <sys/interrupt.h>
61 #include <machine/md_var.h>
62 #include <machine/segments.h>
63 #include <sys/bus.h> 
64
65 #if defined(APIC_IO)
66 #include <machine/smptests.h>                   /** FAST_HI */
67 #include <machine/smp.h>
68 #include <machine/resource.h>
69 #endif /* APIC_IO */
70 #ifdef PC98
71 #include <pc98/pc98/pc98.h>
72 #include <pc98/pc98/pc98_machdep.h>
73 #include <pc98/pc98/epsonio.h>
74 #else
75 #include <i386/isa/isa.h>
76 #endif
77 #include <i386/isa/icu.h>
78
79 #ifdef DEV_ISA
80 #include <isa/isavar.h>
81 #endif
82 #include <i386/isa/intr_machdep.h>
83 #include <sys/interrupt.h>
84 #ifdef APIC_IO
85 #include <machine/clock.h>
86 #endif
87
88 #ifdef DEV_MCA
89 #include <i386/isa/mca_machdep.h>
90 #endif
91
92 /*
93  * Per-interrupt data.
94  */
95 u_long  *intr_countp[ICU_LEN];          /* pointers to interrupt counters */
96 driver_intr_t *intr_handler[ICU_LEN];   /* first level interrupt handler */
97 struct ithd *ithds[ICU_LEN];            /* real interrupt handler */
98 void    *intr_unit[ICU_LEN];
99
100 static inthand_t *fastintr[ICU_LEN] = {
101         &IDTVEC(fastintr0), &IDTVEC(fastintr1),
102         &IDTVEC(fastintr2), &IDTVEC(fastintr3),
103         &IDTVEC(fastintr4), &IDTVEC(fastintr5),
104         &IDTVEC(fastintr6), &IDTVEC(fastintr7),
105         &IDTVEC(fastintr8), &IDTVEC(fastintr9),
106         &IDTVEC(fastintr10), &IDTVEC(fastintr11),
107         &IDTVEC(fastintr12), &IDTVEC(fastintr13),
108         &IDTVEC(fastintr14), &IDTVEC(fastintr15),
109 #if defined(APIC_IO)
110         &IDTVEC(fastintr16), &IDTVEC(fastintr17),
111         &IDTVEC(fastintr18), &IDTVEC(fastintr19),
112         &IDTVEC(fastintr20), &IDTVEC(fastintr21),
113         &IDTVEC(fastintr22), &IDTVEC(fastintr23),
114         &IDTVEC(fastintr24), &IDTVEC(fastintr25),
115         &IDTVEC(fastintr26), &IDTVEC(fastintr27),
116         &IDTVEC(fastintr28), &IDTVEC(fastintr29),
117         &IDTVEC(fastintr30), &IDTVEC(fastintr31),
118 #endif /* APIC_IO */
119 };
120
121 static inthand_t *slowintr[ICU_LEN] = {
122         &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
123         &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
124         &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
125         &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15),
126 #if defined(APIC_IO)
127         &IDTVEC(intr16), &IDTVEC(intr17), &IDTVEC(intr18), &IDTVEC(intr19),
128         &IDTVEC(intr20), &IDTVEC(intr21), &IDTVEC(intr22), &IDTVEC(intr23),
129         &IDTVEC(intr24), &IDTVEC(intr25), &IDTVEC(intr26), &IDTVEC(intr27),
130         &IDTVEC(intr28), &IDTVEC(intr29), &IDTVEC(intr30), &IDTVEC(intr31),
131 #endif /* APIC_IO */
132 };
133
134 static driver_intr_t isa_strayintr;
135
136 #ifdef PC98
137 #define NMI_PARITY 0x04
138 #define NMI_EPARITY 0x02
139 #else
140 #define NMI_PARITY (1 << 7)
141 #define NMI_IOCHAN (1 << 6)
142 #define ENMI_WATCHDOG (1 << 7)
143 #define ENMI_BUSTIMER (1 << 6)
144 #define ENMI_IOSTATUS (1 << 5)
145 #endif
146
147 /*
148  * Bus attachment for the ISA PIC.
149  */
150 static struct isa_pnp_id atpic_ids[] = {
151         { 0x0000d041 /* PNP0000 */, "AT interrupt controller" },
152         { 0 }
153 };
154
155 static int
156 atpic_probe(device_t dev)
157 {
158         int result;
159         
160         if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atpic_ids)) <= 0)
161                 device_quiet(dev);
162         return(result);
163 }
164
165 /*
166  * In the APIC_IO case we might be granted IRQ 2, as this is typically
167  * consumed by chaining between the two PIC components.  If we're using
168  * the APIC, however, this may not be the case, and as such we should
169  * free the resource.  (XXX untested)
170  *
171  * The generic ISA attachment code will handle allocating any other resources
172  * that we don't explicitly claim here.
173  */
174 static int
175 atpic_attach(device_t dev)
176 {
177 #ifdef APIC_IO
178         int             rid;
179         struct resource *res;
180
181         /* try to allocate our IRQ and then free it */
182         rid = 0;
183         res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 0);
184         if (res != NULL)
185                 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
186 #endif
187         return(0);
188 }
189
190 static device_method_t atpic_methods[] = {
191         /* Device interface */
192         DEVMETHOD(device_probe,         atpic_probe),
193         DEVMETHOD(device_attach,        atpic_attach),
194         DEVMETHOD(device_detach,        bus_generic_detach),
195         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
196         DEVMETHOD(device_suspend,       bus_generic_suspend),
197         DEVMETHOD(device_resume,        bus_generic_resume),
198         { 0, 0 }
199 };
200
201 static driver_t atpic_driver = {
202         "atpic",
203         atpic_methods,
204         1,              /* no softc */
205 };
206
207 static devclass_t atpic_devclass;
208
209 DRIVER_MODULE(atpic, isa, atpic_driver, atpic_devclass, 0, 0);
210
211 /*
212  * Handle a NMI, possibly a machine check.
213  * return true to panic system, false to ignore.
214  */
215 int
216 isa_nmi(cd)
217         int cd;
218 {
219         int retval = 0;
220 #ifdef PC98
221         int port = inb(0x33);
222
223         log(LOG_CRIT, "NMI PC98 port = %x\n", port);
224         if (epson_machine_id == 0x20)
225                 epson_outb(0xc16, epson_inb(0xc16) | 0x1);
226         if (port & NMI_PARITY) {
227                 log(LOG_CRIT, "BASE RAM parity error, likely hardware failure.");
228                 retval = 1;
229         } else if (port & NMI_EPARITY) {
230                 log(LOG_CRIT, "EXTENDED RAM parity error, likely hardware failure.");
231                 retval = 1;
232         } else {
233                 log(LOG_CRIT, "\nNMI Resume ??\n");
234         }
235 #else /* IBM-PC */
236         int isa_port = inb(0x61);
237         int eisa_port = inb(0x461);
238
239         log(LOG_CRIT, "NMI ISA %x, EISA %x\n", isa_port, eisa_port);
240 #ifdef DEV_MCA
241         if (MCA_system && mca_bus_nmi())
242                 return(0);
243 #endif
244         
245         if (isa_port & NMI_PARITY) {
246                 log(LOG_CRIT, "RAM parity error, likely hardware failure.");
247                 retval = 1;
248         }
249
250         if (isa_port & NMI_IOCHAN) {
251                 log(LOG_CRIT, "I/O channel check, likely hardware failure.");
252                 retval = 1;
253         }
254
255         /*
256          * On a real EISA machine, this will never happen.  However it can
257          * happen on ISA machines which implement XT style floating point
258          * error handling (very rare).  Save them from a meaningless panic.
259          */
260         if (eisa_port == 0xff)
261                 return(retval);
262
263         if (eisa_port & ENMI_WATCHDOG) {
264                 log(LOG_CRIT, "EISA watchdog timer expired, likely hardware failure.");
265                 retval = 1;
266         }
267
268         if (eisa_port & ENMI_BUSTIMER) {
269                 log(LOG_CRIT, "EISA bus timeout, likely hardware failure.");
270                 retval = 1;
271         }
272
273         if (eisa_port & ENMI_IOSTATUS) {
274                 log(LOG_CRIT, "EISA I/O port status error.");
275                 retval = 1;
276         }
277 #endif
278         return(retval);
279 }
280
281 /*
282  * Create a default interrupt table to avoid problems caused by
283  * spurious interrupts during configuration of kernel, then setup
284  * interrupt control unit.
285  */
286 void
287 isa_defaultirq()
288 {
289         int i;
290
291         /* icu vectors */
292         for (i = 0; i < ICU_LEN; i++)
293                 icu_unset(i, (driver_intr_t *)NULL);
294
295         /* initialize 8259's */
296 #ifdef DEV_MCA
297         if (MCA_system)
298                 outb(IO_ICU1, 0x19);            /* reset; program device, four bytes */
299         else
300 #endif
301                 outb(IO_ICU1, 0x11);            /* reset; program device, four bytes */
302
303         outb(IO_ICU1+ICU_IMR_OFFSET, NRSVIDT);  /* starting at this vector index */
304         outb(IO_ICU1+ICU_IMR_OFFSET, IRQ_SLAVE);                /* slave on line 7 */
305 #ifdef PC98
306 #ifdef AUTO_EOI_1
307         outb(IO_ICU1+ICU_IMR_OFFSET, 0x1f);             /* (master) auto EOI, 8086 mode */
308 #else
309         outb(IO_ICU1+ICU_IMR_OFFSET, 0x1d);             /* (master) 8086 mode */
310 #endif
311 #else /* IBM-PC */
312 #ifdef AUTO_EOI_1
313         outb(IO_ICU1+ICU_IMR_OFFSET, 2 | 1);            /* auto EOI, 8086 mode */
314 #else
315         outb(IO_ICU1+ICU_IMR_OFFSET, 1);                /* 8086 mode */
316 #endif
317 #endif /* PC98 */
318         outb(IO_ICU1+ICU_IMR_OFFSET, 0xff);             /* leave interrupts masked */
319         outb(IO_ICU1, 0x0a);            /* default to IRR on read */
320 #ifndef PC98
321         outb(IO_ICU1, 0xc0 | (3 - 1));  /* pri order 3-7, 0-2 (com2 first) */
322 #endif /* !PC98 */
323
324 #ifdef DEV_MCA
325         if (MCA_system)
326                 outb(IO_ICU2, 0x19);            /* reset; program device, four bytes */
327         else
328 #endif
329                 outb(IO_ICU2, 0x11);            /* reset; program device, four bytes */
330
331         outb(IO_ICU2+ICU_IMR_OFFSET, NRSVIDT+8); /* staring at this vector index */
332         outb(IO_ICU2+ICU_IMR_OFFSET, ICU_SLAVEID);         /* my slave id is 7 */
333 #ifdef PC98
334         outb(IO_ICU2+ICU_IMR_OFFSET,9);              /* 8086 mode */
335 #else /* IBM-PC */
336 #ifdef AUTO_EOI_2
337         outb(IO_ICU2+ICU_IMR_OFFSET, 2 | 1);            /* auto EOI, 8086 mode */
338 #else
339         outb(IO_ICU2+ICU_IMR_OFFSET,1);         /* 8086 mode */
340 #endif
341 #endif /* PC98 */
342         outb(IO_ICU2+ICU_IMR_OFFSET, 0xff);          /* leave interrupts masked */
343         outb(IO_ICU2, 0x0a);            /* default to IRR on read */
344 }
345
346 /*
347  * Caught a stray interrupt, notify
348  */
349 static void
350 isa_strayintr(vcookiep)
351         void *vcookiep;
352 {
353         int intr = (void **)vcookiep - &intr_unit[0];
354
355         /*
356          * XXX TODO print a different message for #7 if it is for a
357          * glitch.  Glitches can be distinguished from real #7's by
358          * testing that the in-service bit is _not_ set.  The test
359          * must be done before sending an EOI so it can't be done if
360          * we are using AUTO_EOI_1.
361          */
362         if (intrcnt[1 + intr] <= 5)
363                 log(LOG_ERR, "stray irq %d\n", intr);
364         if (intrcnt[1 + intr] == 5)
365                 log(LOG_CRIT,
366                     "too many stray irq %d's; not logging any more\n", intr);
367 }
368
369 #ifdef DEV_ISA
370 /*
371  * Return a bitmap of the current interrupt requests.  This is 8259-specific
372  * and is only suitable for use at probe time.
373  */
374 intrmask_t
375 isa_irq_pending()
376 {
377         u_char irr1;
378         u_char irr2;
379
380         irr1 = inb(IO_ICU1);
381         irr2 = inb(IO_ICU2);
382         return ((irr2 << 8) | irr1);
383 }
384 #endif
385
386 /*
387  * Update intrnames array with the specified name.  This is used by
388  * vmstat(8) and the like.
389  */
390 static void
391 update_intrname(int intr, char *name)
392 {
393         char buf[32];
394         char *cp;
395         int name_index, off, strayintr;
396
397         /*
398          * Initialise strings for bitbucket and stray interrupt counters.
399          * These have statically allocated indices 0 and 1 through ICU_LEN.
400          */
401         if (intrnames[0] == '\0') {
402                 off = sprintf(intrnames, "???") + 1;
403                 for (strayintr = 0; strayintr < ICU_LEN; strayintr++)
404                         off += sprintf(intrnames + off, "stray irq%d",
405                             strayintr) + 1;
406         }
407
408         if (name == NULL)
409                 name = "???";
410         if (snprintf(buf, sizeof(buf), "%s irq%d", name, intr) >= sizeof(buf))
411                 goto use_bitbucket;
412
413         /*
414          * Search for `buf' in `intrnames'.  In the usual case when it is
415          * not found, append it to the end if there is enough space (the \0
416          * terminator for the previous string, if any, becomes a separator).
417          */
418         for (cp = intrnames, name_index = 0;
419             cp != eintrnames && name_index < NR_INTRNAMES;
420             cp += strlen(cp) + 1, name_index++) {
421                 if (*cp == '\0') {
422                         if (strlen(buf) >= eintrnames - cp)
423                                 break;
424                         strcpy(cp, buf);
425                         goto found;
426                 }
427                 if (strcmp(cp, buf) == 0)
428                         goto found;
429         }
430
431 use_bitbucket:
432         printf("update_intrname: counting %s irq%d as %s\n", name, intr,
433             intrnames);
434         name_index = 0;
435 found:
436         intr_countp[intr] = &intrcnt[name_index];
437 }
438
439 int
440 icu_setup(int intr, driver_intr_t *handler, void *arg, int flags)
441 {
442 #ifdef FAST_HI
443         int             select;         /* the select register is 8 bits */
444         int             vector;
445         u_int32_t       value;          /* the window register is 32 bits */
446 #endif /* FAST_HI */
447         u_long  ef;
448
449 #if defined(APIC_IO)
450         if ((u_int)intr >= ICU_LEN)     /* no 8259 SLAVE to ignore */
451 #else
452         if ((u_int)intr >= ICU_LEN || intr == ICU_SLAVEID)
453 #endif /* APIC_IO */
454         if (intr_handler[intr] != isa_strayintr)
455                 return (EBUSY);
456
457         ef = read_eflags();
458         disable_intr();
459         intr_handler[intr] = handler;
460         intr_unit[intr] = arg;
461 #ifdef FAST_HI
462         if (flags & INTR_FAST) {
463                 vector = TPR_FAST_INTS + intr;
464                 setidt(vector, fastintr[intr],
465                        SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
466         }
467         else {
468                 vector = TPR_SLOW_INTS + intr;
469 #ifdef APIC_INTR_REORDER
470 #ifdef APIC_INTR_HIGHPRI_CLOCK
471                 /* XXX: Hack (kludge?) for more accurate clock. */
472                 if (intr == apic_8254_intr || intr == 8) {
473                         vector = TPR_FAST_INTS + intr;
474                 }
475 #endif
476 #endif
477                 setidt(vector, slowintr[intr],
478                        SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
479         }
480 #ifdef APIC_INTR_REORDER
481         set_lapic_isrloc(intr, vector);
482 #endif
483         /*
484          * Reprogram the vector in the IO APIC.
485          */
486         if (int_to_apicintpin[intr].ioapic >= 0) {
487                 select = int_to_apicintpin[intr].redirindex;
488                 value = io_apic_read(int_to_apicintpin[intr].ioapic, 
489                                      select) & ~IOART_INTVEC;
490                 io_apic_write(int_to_apicintpin[intr].ioapic, 
491                               select, value | vector);
492         }
493 #else
494         setidt(ICU_OFFSET + intr,
495                flags & INTR_FAST ? fastintr[intr] : slowintr[intr],
496                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
497 #endif /* FAST_HI */
498         INTREN(1 << intr);
499         write_eflags(ef);
500         return (0);
501 }
502
503 /*
504  * Dissociate an interrupt handler from an IRQ and set the handler to
505  * the stray interrupt handler.  The 'handler' parameter is used only
506  * for consistency checking.
507  */
508 int
509 icu_unset(intr, handler)
510         int     intr;
511         driver_intr_t *handler;
512 {
513         u_long  ef;
514
515         if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
516                 return (EINVAL);
517
518         INTRDIS(1 << intr);
519         ef = read_eflags();
520         disable_intr();
521         intr_countp[intr] = &intrcnt[1 + intr];
522         intr_handler[intr] = isa_strayintr;
523         intr_unit[intr] = &intr_unit[intr];
524 #ifdef FAST_HI_XXX
525         /* XXX how do I re-create dvp here? */
526         setidt(flags & INTR_FAST ? TPR_FAST_INTS + intr : TPR_SLOW_INTS + intr,
527             slowintr[intr], SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
528 #else /* FAST_HI */
529 #ifdef APIC_INTR_REORDER
530         set_lapic_isrloc(intr, ICU_OFFSET + intr);
531 #endif
532         setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
533             GSEL(GCODE_SEL, SEL_KPL));
534 #endif /* FAST_HI */
535         write_eflags(ef);
536         return (0);
537 }
538
539 struct intrhand *
540 inthand_add(const char *name, int irq, driver_intr_t handler, void *arg,
541              int pri, int flags)
542 {
543         struct ithd *ithd = ithds[irq]; /* descriptor for the IRQ */
544         struct intrhand *head;          /* chain of handlers for IRQ */
545         struct intrhand *idesc;         /* descriptor for this handler */
546         struct proc *p;                 /* interrupt thread */
547         int errcode = 0;
548
549         if (name == NULL)               /* no name? */
550                 panic ("anonymous interrupt");
551         if (ithd == NULL || ithd->it_ih == NULL) {
552                 /* first handler for this irq. */
553                 if (ithd == NULL) {
554                         ithd = malloc(sizeof (struct ithd), M_DEVBUF,
555                             M_WAITOK | M_ZERO);
556                         if (ithd == NULL)
557                                 return (NULL);
558                         ithd->irq = irq;
559                         ithds[irq] = ithd;
560                 }
561                 /*
562                  * If we have a fast interrupt, we need to set the
563                  * handler address directly.  Do that below.  For a
564                  * slow interrupt, we don't need to know more details,
565                  * so do it here because it's tidier.
566                  */
567                 if ((flags & INTR_FAST) == 0) {
568                         /*
569                          * Only create a kernel thread if we don't already
570                          * have one.
571                          */
572                         if (ithd->it_proc == NULL) {
573                                 errcode = kthread_create(ithd_loop, NULL, &p,
574                                     RFSTOPPED | RFHIGHPID, "irq%d: %s", irq,
575                                     name);
576                                 if (errcode)
577                                         panic("inthand_add: Can't create "
578                                               "interrupt thread");
579                                 p->p_intr_nesting_level = 1;
580                                 p->p_rtprio.type = RTP_PRIO_ITHREAD;
581                                 p->p_stat = SWAIT; /* we're idle */
582
583                                 /* Put in linkages. */
584                                 ithd->it_proc = p;
585                                 p->p_ithd = ithd;
586                         } else
587                                 snprintf(ithd->it_proc->p_comm, MAXCOMLEN,
588                                     "irq%d: %s", irq, name);
589                         p->p_rtprio.prio = pri;
590
591                         /*
592                          * The interrupt process must be in place, but
593                          * not necessarily schedulable, before we
594                          * initialize the ICU, since it may cause an
595                          * immediate interrupt.
596                          */
597                         if (icu_setup(irq, &sched_ithd, arg, flags) != 0)
598                                 panic("inthand_add: Can't initialize ICU");
599                 }
600         } else if ((flags & INTR_EXCL) != 0
601                    || (ithd->it_ih->ih_flags & INTR_EXCL) != 0) {
602                 /*
603                  * We can't append the new handler if either
604                  * list ithd or new handler do not allow
605                  * interrupts to be shared.
606                  */
607                 if (bootverbose)
608                         printf("\tdevice combination %s and %s "
609                                "doesn't support shared irq%d\n",
610                                ithd->it_ih->ih_name, name, irq);
611                 return(NULL);
612         } else if (flags & INTR_FAST) {
613                  /* We can only have one fast interrupt by itself. */
614                 if (bootverbose)
615                         printf("\tCan't add fast interrupt %s"
616                                " to normal interrupt %s on irq%d",
617                                name, ithd->it_ih->ih_name, irq);
618                 return (NULL);
619         } else {                        /* update p_comm */
620                 p = ithd->it_proc;
621                 if (strlen(p->p_comm) + strlen(name) < MAXCOMLEN) {
622                         strcat(p->p_comm, " ");
623                         strcat(p->p_comm, name);
624                 } else if (strlen(p->p_comm) == MAXCOMLEN)
625                         p->p_comm[MAXCOMLEN - 1] = '+';
626                 else
627                         strcat(p->p_comm, "+");
628         }
629         idesc = malloc(sizeof (struct intrhand), M_DEVBUF, M_WAITOK | M_ZERO);
630         if (idesc == NULL)
631                 return (NULL);
632
633         idesc->ih_handler = handler;
634         idesc->ih_argument = arg;
635         idesc->ih_flags = flags;
636         idesc->ih_ithd = ithd;
637
638         idesc->ih_name = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK);
639         if (idesc->ih_name == NULL) {
640                 free(idesc, M_DEVBUF);
641                 return (NULL);
642         }
643         strcpy(idesc->ih_name, name);
644
645         /* Slow interrupts got set up above. */
646         if ((flags & INTR_FAST)
647                 && (icu_setup(irq, idesc->ih_handler, idesc->ih_argument,
648                               idesc->ih_flags) != 0) ) {
649                 if (bootverbose)
650                         printf("\tinthand_add(irq%d) failed, result=%d\n", 
651                                irq, errcode);
652                 free(idesc->ih_name, M_DEVBUF);
653                 free(idesc, M_DEVBUF);
654                 return NULL;
655         }
656         head = ithd->it_ih;             /* look at chain of handlers */
657         if (head) {
658                 while (head->ih_next != NULL)
659                         head = head->ih_next; /* find the end */
660                 head->ih_next = idesc;  /* hook it in there */
661         } else
662                 ithd->it_ih = idesc;    /* put it up front */
663         update_intrname(irq, idesc->ih_name);
664         return (idesc);
665 }
666
667 /*
668  * Deactivate and remove linked list the interrupt handler descriptor
669  * data connected created by an earlier call of inthand_add(), then
670  * adjust the interrupt masks if necessary.
671  *
672  * Return the memory held by the interrupt handler descriptor data
673  * structure to the system.  First ensure the handler is not actively
674  * in use.
675  */
676
677 int
678 inthand_remove(struct intrhand *idesc)
679 {
680         struct ithd *ithd;              /* descriptor for the IRQ */
681         struct intrhand *ih;            /* chain of handlers */
682
683         if (idesc == NULL)
684                 return (-1);
685         ithd = idesc->ih_ithd;
686         ih = ithd->it_ih;
687
688         if (ih == idesc)                /* first in the chain */
689                 ithd->it_ih = idesc->ih_next; /* unhook it */
690         else {
691                 while ((ih != NULL)
692                         && (ih->ih_next != idesc) )
693                         ih = ih->ih_next;
694                 if (ih->ih_next != idesc)
695                         return (-1);
696                 ih->ih_next = ih->ih_next->ih_next;
697         }
698         
699         if (ithd->it_ih == NULL) {      /* no handlers left, */
700                 icu_unset(ithd->irq, idesc->ih_handler);
701                 ithds[ithd->irq] = NULL;
702
703                 if ((idesc->ih_flags & INTR_FAST) == 0) {
704                         mtx_enter(&sched_lock, MTX_SPIN);
705                         if (ithd->it_proc->p_stat == SWAIT) {
706                                 ithd->it_proc->p_intr_nesting_level = 0;
707                                 ithd->it_proc->p_stat = SRUN;
708                                 setrunqueue(ithd->it_proc);
709                                 /*
710                                  * We don't do an ast here because we really
711                                  * don't care when it runs next.
712                                  *
713                                  * XXX: should we lower the threads priority?
714                                  */
715                         }
716                         mtx_exit(&sched_lock, MTX_SPIN);
717                 }
718         }
719         free(idesc->ih_name, M_DEVBUF);
720         free(idesc, M_DEVBUF);
721         return (0);
722 }