unbreak GENERIC building
[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.33 2005/10/13 00:02:47 dillon Exp $
39  */
40 /*
41  * This file contains an aggregated module marked:
42  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
43  * All rights reserved.
44  * See the notice for details.
45  */
46
47 #include "use_isa.h"
48 #include "opt_auto_eoi.h"
49
50 #include <sys/param.h>
51 #ifndef SMP
52 #include <machine/lock.h>
53 #endif
54 #include <sys/systm.h>
55 #include <sys/syslog.h>
56 #include <sys/malloc.h>
57 #include <sys/errno.h>
58 #include <sys/interrupt.h>
59 #include <machine/ipl.h>
60 #include <machine/md_var.h>
61 #include <machine/segments.h>
62 #include <sys/bus.h> 
63 #include <machine/globaldata.h>
64 #include <sys/proc.h>
65 #include <sys/thread2.h>
66
67 #include <machine/smptests.h>                   /** FAST_HI */
68 #include <machine/smp.h>
69 #include <bus/isa/i386/isa.h>
70 #include <i386/isa/icu.h>
71
72 #if NISA > 0
73 #include <bus/isa/isavar.h>
74 #endif
75 #include <i386/isa/intr_machdep.h>
76 #include <bus/isa/isavar.h>
77 #include <sys/interrupt.h>
78 #ifdef APIC_IO
79 #include <machine/clock.h>
80 #endif
81 #include <machine/cpu.h>
82
83 /* XXX should be in suitable include files */
84 #define ICU_IMR_OFFSET          1               /* IO_ICU{1,2} + 1 */
85 #define ICU_SLAVEID                     2
86
87 #ifdef APIC_IO
88 /*
89  * This is to accommodate "mixed-mode" programming for 
90  * motherboards that don't connect the 8254 to the IO APIC.
91  */
92 #define AUTO_EOI_1      1
93 #endif
94
95 #define NR_INTRNAMES    (1 + ICU_LEN + 2 * ICU_LEN)
96
97 static void     init_i8259(void);
98 static int      icu_unset(int intr);
99 static int      icu_setup(int intr, int flags);
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 #endif /* APIC_IO */
116 };
117
118 unpendhand_t *fastunpend[ICU_LEN] = {
119         IDTVEC(fastunpend0), IDTVEC(fastunpend1),
120         IDTVEC(fastunpend2), IDTVEC(fastunpend3),
121         IDTVEC(fastunpend4), IDTVEC(fastunpend5),
122         IDTVEC(fastunpend6), IDTVEC(fastunpend7),
123         IDTVEC(fastunpend8), IDTVEC(fastunpend9),
124         IDTVEC(fastunpend10), IDTVEC(fastunpend11),
125         IDTVEC(fastunpend12), IDTVEC(fastunpend13),
126         IDTVEC(fastunpend14), IDTVEC(fastunpend15),
127 #if defined(APIC_IO)
128         IDTVEC(fastunpend16), IDTVEC(fastunpend17),
129         IDTVEC(fastunpend18), IDTVEC(fastunpend19),
130         IDTVEC(fastunpend20), IDTVEC(fastunpend21),
131         IDTVEC(fastunpend22), IDTVEC(fastunpend23),
132 #endif
133 };
134
135 static inthand_t *slowintr[ICU_LEN] = {
136         &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
137         &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
138         &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
139         &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15),
140 #if defined(APIC_IO)
141         &IDTVEC(intr16), &IDTVEC(intr17), &IDTVEC(intr18), &IDTVEC(intr19),
142         &IDTVEC(intr20), &IDTVEC(intr21), &IDTVEC(intr22), &IDTVEC(intr23),
143 #endif /* APIC_IO */
144 };
145
146 #if defined(APIC_IO)
147
148 static inthand_t *wrongintr[ICU_LEN] = {
149         &IDTVEC(wrongintr0), &IDTVEC(wrongintr1), &IDTVEC(wrongintr2),
150         &IDTVEC(wrongintr3), &IDTVEC(wrongintr4), &IDTVEC(wrongintr5),
151         &IDTVEC(wrongintr6), &IDTVEC(wrongintr7), &IDTVEC(wrongintr8),
152         &IDTVEC(wrongintr9), &IDTVEC(wrongintr10), &IDTVEC(wrongintr11),
153         &IDTVEC(wrongintr12), &IDTVEC(wrongintr13), &IDTVEC(wrongintr14),
154         &IDTVEC(wrongintr15),
155         &IDTVEC(wrongintr16), &IDTVEC(wrongintr17), &IDTVEC(wrongintr18),
156         &IDTVEC(wrongintr19), &IDTVEC(wrongintr20), &IDTVEC(wrongintr21),
157         &IDTVEC(wrongintr22), &IDTVEC(wrongintr23)
158 };
159
160 #endif /* APIC_IO */
161
162 #define NMI_PARITY (1 << 7)
163 #define NMI_IOCHAN (1 << 6)
164 #define ENMI_WATCHDOG (1 << 7)
165 #define ENMI_BUSTIMER (1 << 6)
166 #define ENMI_IOSTATUS (1 << 5)
167
168 /*
169  * Handle a NMI, possibly a machine check.
170  * return true to panic system, false to ignore.
171  */
172 int
173 isa_nmi(cd)
174         int cd;
175 {
176         int retval = 0;
177         int isa_port = inb(0x61);
178         int eisa_port = inb(0x461);
179
180         log(LOG_CRIT, "NMI ISA %x, EISA %x\n", isa_port, eisa_port);
181         
182         if (isa_port & NMI_PARITY) {
183                 log(LOG_CRIT, "RAM parity error, likely hardware failure.");
184                 retval = 1;
185         }
186
187         if (isa_port & NMI_IOCHAN) {
188                 log(LOG_CRIT, "I/O channel check, likely hardware failure.");
189                 retval = 1;
190         }
191
192         /*
193          * On a real EISA machine, this will never happen.  However it can
194          * happen on ISA machines which implement XT style floating point
195          * error handling (very rare).  Save them from a meaningless panic.
196          */
197         if (eisa_port == 0xff)
198                 return(retval);
199
200         if (eisa_port & ENMI_WATCHDOG) {
201                 log(LOG_CRIT, "EISA watchdog timer expired, likely hardware failure.");
202                 retval = 1;
203         }
204
205         if (eisa_port & ENMI_BUSTIMER) {
206                 log(LOG_CRIT, "EISA bus timeout, likely hardware failure.");
207                 retval = 1;
208         }
209
210         if (eisa_port & ENMI_IOSTATUS) {
211                 log(LOG_CRIT, "EISA I/O port status error.");
212                 retval = 1;
213         }
214         return(retval);
215 }
216
217 /*
218  *  ICU reinitialize when ICU configuration has lost.
219  */
220 void
221 icu_reinit()
222 {
223         int i;
224
225         init_i8259();
226         for (i = 0; i < ICU_LEN; ++i) {
227                 if (count_registered_ints(i))
228                         INTREN(1 << i);
229         }
230 }
231
232 /*
233  * Fill in default interrupt table (in case of spurious interrupt
234  * during configuration of kernel, setup interrupt control unit
235  */
236 void
237 isa_defaultirq()
238 {
239         int i;
240
241         /* icu vectors */
242         for (i = 0; i < ICU_LEN; i++)
243                 icu_unset(i);
244         init_i8259();
245 }
246
247 static void
248 init_i8259(void)
249 {
250
251         /* initialize 8259's */
252         outb(IO_ICU1, 0x11);            /* reset; program device, four bytes */
253         outb(IO_ICU1+ICU_IMR_OFFSET, NRSVIDT);  /* starting at this vector index */
254         outb(IO_ICU1+ICU_IMR_OFFSET, IRQ_SLAVE);                /* slave on line 7 */
255 #ifdef AUTO_EOI_1
256         outb(IO_ICU1+ICU_IMR_OFFSET, 2 | 1);            /* auto EOI, 8086 mode */
257 #else
258         outb(IO_ICU1+ICU_IMR_OFFSET, 1);                /* 8086 mode */
259 #endif
260         outb(IO_ICU1+ICU_IMR_OFFSET, 0xff);             /* leave interrupts masked */
261         outb(IO_ICU1, 0x0a);            /* default to IRR on read */
262         outb(IO_ICU1, 0xc0 | (3 - 1));  /* pri order 3-7, 0-2 (com2 first) */
263         outb(IO_ICU2, 0x11);            /* reset; program device, four bytes */
264         outb(IO_ICU2+ICU_IMR_OFFSET, NRSVIDT+8); /* staring at this vector index */
265         outb(IO_ICU2+ICU_IMR_OFFSET, ICU_SLAVEID);         /* my slave id is 7 */
266 #ifdef AUTO_EOI_2
267         outb(IO_ICU2+ICU_IMR_OFFSET, 2 | 1);            /* auto EOI, 8086 mode */
268 #else
269         outb(IO_ICU2+ICU_IMR_OFFSET,1);         /* 8086 mode */
270 #endif
271         outb(IO_ICU2+ICU_IMR_OFFSET, 0xff);          /* leave interrupts masked */
272         outb(IO_ICU2, 0x0a);            /* default to IRR on read */
273 }
274
275 #if NISA > 0
276 /*
277  * Return a bitmap of the current interrupt requests.  This is 8259-specific
278  * and is only suitable for use at probe time.
279  */
280 intrmask_t
281 isa_irq_pending(void)
282 {
283         u_char irr1;
284         u_char irr2;
285
286         irr1 = inb(IO_ICU1);
287         irr2 = inb(IO_ICU2);
288         return ((irr2 << 8) | irr1);
289 }
290 #endif
291
292 static
293 int
294 icu_setup(int intr, int flags)
295 {
296 #if defined(FAST_HI) && defined(APIC_IO)
297         int             select;         /* the select register is 8 bits */
298         int             vector;
299         u_int32_t       value;          /* the window register is 32 bits */
300 #endif /* FAST_HI */
301         u_long  ef;
302
303 #if defined(APIC_IO)
304         if ((u_int)intr >= ICU_LEN)     /* no 8259 SLAVE to ignore */
305 #else
306         if ((u_int)intr >= ICU_LEN || intr == ICU_SLAVEID)
307 #endif /* APIC_IO */
308                 return (EINVAL);
309
310         ef = read_eflags();
311         cpu_disable_intr();     /* YYY */
312 #if defined(FAST_HI) && defined(APIC_IO)
313         if (flags & INTR_FAST) {
314                 /*
315                  * Install a spurious interrupt in the low space in case
316                  * the IO apic is not properly reprogrammed.
317                  */
318                 vector = TPR_SLOW_INTS + intr;
319                 setidt(vector, wrongintr[intr],
320                        SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
321                 vector = TPR_FAST_INTS + intr;
322                 setidt(vector, fastintr[intr],
323                        SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
324         } else {
325                 vector = TPR_SLOW_INTS + intr;
326 #ifdef APIC_INTR_REORDER
327 #ifdef APIC_INTR_HIGHPRI_CLOCK
328                 /* XXX: Hack (kludge?) for more accurate clock. */
329                 if (intr == apic_8254_intr || intr == 8) {
330                         vector = TPR_FAST_INTS + intr;
331                 }
332 #endif
333 #endif
334                 setidt(vector, slowintr[intr],
335                        SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
336         }
337 #ifdef APIC_INTR_REORDER
338         set_lapic_isrloc(intr, vector);
339 #endif
340         /*
341          * Reprogram the vector in the IO APIC.
342          *
343          * XXX EOI/mask a pending (stray) interrupt on the old vector?
344          */
345         if (int_to_apicintpin[intr].ioapic >= 0) {
346                 select = int_to_apicintpin[intr].redirindex;
347                 value = io_apic_read(int_to_apicintpin[intr].ioapic, 
348                                      select) & ~IOART_INTVEC;
349                 io_apic_write(int_to_apicintpin[intr].ioapic, 
350                               select, value | vector);
351         }
352 #else
353         setidt(ICU_OFFSET + intr,
354                flags & INTR_FAST ? fastintr[intr] : slowintr[intr],
355                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
356 #endif /* FAST_HI && APIC_IO */
357         INTREN(1 << intr);
358         write_eflags(ef);
359         return (0);
360 }
361
362 static int
363 icu_unset(int intr)
364 {
365         u_long  ef;
366
367         KKASSERT((u_int)intr < ICU_LEN);
368         INTRDIS(1 << intr);
369         ef = read_eflags();
370         cpu_disable_intr();     /* YYY */
371 #ifdef FAST_HI_XXX
372         /* XXX how do I re-create dvp here? */
373         setidt(flags & INTR_FAST ? TPR_FAST_INTS + intr : TPR_SLOW_INTS + intr,
374             slowintr[intr], SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
375 #else /* FAST_HI */
376 #ifdef APIC_INTR_REORDER
377         set_lapic_isrloc(intr, ICU_OFFSET + intr);
378 #endif
379         setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
380             GSEL(GCODE_SEL, SEL_KPL));
381 #endif /* FAST_HI */
382         write_eflags(ef);
383         return (0);
384 }
385
386
387 /* The following notice applies beyond this point in the file */
388
389 /*
390  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
391  * All rights reserved.
392  *
393  * Redistribution and use in source and binary forms, with or without
394  * modification, are permitted provided that the following conditions
395  * are met:
396  * 1. Redistributions of source code must retain the above copyright
397  *    notice unmodified, this list of conditions, and the following
398  *    disclaimer.
399  * 2. Redistributions in binary form must reproduce the above copyright
400  *    notice, this list of conditions and the following disclaimer in the
401  *    documentation and/or other materials provided with the distribution.
402  *
403  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
404  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
405  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
406  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
407  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
408  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
409  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
410  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
411  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
412  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
413  *
414  * $FreeBSD: src/sys/i386/isa/intr_machdep.c,v 1.29.2.5 2001/10/14 06:54:27 luigi Exp $
415  *
416  */
417
418 /*
419  * Create and activate an interrupt handler descriptor data structure.
420  *
421  * The dev_instance pointer is required for resource management, and will
422  * only be passed through to resource_claim().
423  *
424  * There will be functions that derive a driver and unit name from a
425  * dev_instance variable, and those functions will be used to maintain the
426  * interrupt counter label array referenced by systat and vmstat to report
427  * device interrupt rates (->update_intrlabels).
428  *
429  * Add the interrupt handler descriptor data structure created by an
430  * earlier call of create_intr() to the linked list for its irq.
431  *
432  * WARNING: This is an internal function and not to be used by device
433  * drivers.  It is subject to change without notice.
434  */
435
436 void *
437 inthand_add(const char *name, int irq, inthand2_t handler, void *arg,
438              int flags, lwkt_serialize_t serializer)
439 {
440         int errcode = 0;
441         void *id;
442
443         if ((unsigned)irq >= ICU_LEN) {
444                 printf("create_intr: requested irq%d too high, limit is %d\n",
445                        irq, ICU_LEN -1);
446                 return (NULL);
447         }
448         /*
449          * Register the interrupt, then setup the ICU
450          */
451         id = register_int(irq, handler, arg, name, serializer, flags);
452
453         if (id == NULL) {
454                 printf("Unable to install handler for %s\n", name);
455                 printf("\tdevice combination not supported on irq %d\n", irq);
456                 return(NULL);
457         }
458
459         crit_enter();
460         if (count_registered_ints(irq) == 1) {
461                 if (icu_setup(irq, flags))
462                         errcode = -1;
463         }
464         crit_exit();
465
466         /*
467          * Cleanup
468          */
469         if (errcode != 0) {
470                 if (bootverbose) {
471                         printf("\tinthand_add(irq%d) failed, result=%d\n", 
472                                irq, errcode);
473                 }
474                 unregister_int(id);
475                 id = NULL;
476         }
477         return (id);
478 }
479
480 /*
481  * Deactivate and remove the interrupt handler descriptor data connected
482  * created by an earlier call of intr_connect() from the linked list.
483  *
484  * Return the memory held by the interrupt handler descriptor data structure
485  * to the system. Make sure, the handler is not actively used anymore, before.
486  */
487 int
488 inthand_remove(void *id)
489 {
490         int irq;
491
492         if (id == NULL)
493                 return(-1);
494
495         crit_enter();
496         irq = get_registered_intr(id);
497         if (unregister_int(id) == 0) {
498                 icu_unset(irq);
499         }
500         crit_exit();
501         return (0);
502 }
503
504 /*
505  * ithread_done()
506  *
507  *      This function is called by an interrupt thread when it has completed
508  *      processing a loop.  We re-enable interrupts and interlock with
509  *      ipending.
510  *
511  *      See kern/kern_intr.c for more information.
512  */
513 void
514 ithread_done(int irq)
515 {
516     struct mdglobaldata *gd = mdcpu;
517     int mask = 1 << irq;
518     thread_t td;
519
520     td = gd->mi.gd_curthread;
521
522     KKASSERT(td->td_pri >= TDPRI_CRIT);
523     lwkt_deschedule_self(td);
524     INTREN(mask);
525     if (gd->gd_ipending & mask) {
526         atomic_clear_int_nonlocked(&gd->gd_ipending, mask);
527         INTRDIS(mask);
528         lwkt_schedule_self(td);
529     } else {
530         lwkt_switch();
531     }
532 }
533
534 #ifdef SMP
535 /*
536  * forward_fast_remote()
537  *
538  *      This function is called from the receiving end of an IPIQ when a
539  *      remote cpu wishes to forward a fast interrupt to us.  All we have to
540  *      do is set the interrupt pending and let the IPI's doreti deal with it.
541  */
542 void
543 forward_fastint_remote(void *arg)
544 {
545     int irq = (int)arg;
546     struct mdglobaldata *gd = mdcpu;
547
548     atomic_set_int_nonlocked(&gd->gd_fpending, 1 << irq);
549     atomic_set_int_nonlocked(&gd->mi.gd_reqflags, RQF_INTPEND);
550 }
551
552 #endif