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