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