Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / i386 / apic / apic_abi.c
1 /*
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * Copyright (c) 1996, by Steve Passe.  All rights reserved.
4  * Copyright (c) 1991 The Regents of the University of California.
5  * All rights reserved.
6  * 
7  * This code is derived from software contributed to The DragonFly Project
8  * by Matthew Dillon <dillon@backplane.com>
9  *
10  * This code is derived from software contributed to Berkeley by
11  * William Jolitz.
12  * 
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  * 3. Neither the name of The DragonFly Project nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific, prior written permission.
26  * 
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
31  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
35  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * $DragonFly: src/sys/i386/apic/Attic/apic_abi.c,v 1.9 2005/11/04 19:46:07 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/machintr.h>
47 #include <sys/interrupt.h>
48 #include <sys/bus.h>
49 #include <machine/smp.h>
50 #include <machine/segments.h>
51 #include <machine/md_var.h>
52 #include <machine/clock.h>      /* apic_8254_intr */
53 #include <i386/isa/intr_machdep.h>
54 #include <i386/icu/icu.h>
55 #include "apic_ipl.h"
56
57 #ifdef APIC_IO
58
59 extern void APIC_INTREN(int);
60 extern void APIC_INTRDIS(int);
61
62 extern inthand_t
63         IDTVEC(apic_fastintr0), IDTVEC(apic_fastintr1),
64         IDTVEC(apic_fastintr2), IDTVEC(apic_fastintr3),
65         IDTVEC(apic_fastintr4), IDTVEC(apic_fastintr5),
66         IDTVEC(apic_fastintr6), IDTVEC(apic_fastintr7),
67         IDTVEC(apic_fastintr8), IDTVEC(apic_fastintr9),
68         IDTVEC(apic_fastintr10), IDTVEC(apic_fastintr11),
69         IDTVEC(apic_fastintr12), IDTVEC(apic_fastintr13),
70         IDTVEC(apic_fastintr14), IDTVEC(apic_fastintr15),
71         IDTVEC(apic_fastintr16), IDTVEC(apic_fastintr17),
72         IDTVEC(apic_fastintr18), IDTVEC(apic_fastintr19),
73         IDTVEC(apic_fastintr20), IDTVEC(apic_fastintr21),
74         IDTVEC(apic_fastintr22), IDTVEC(apic_fastintr23);
75
76 extern inthand_t
77         IDTVEC(apic_slowintr0), IDTVEC(apic_slowintr1),
78         IDTVEC(apic_slowintr2), IDTVEC(apic_slowintr3),
79         IDTVEC(apic_slowintr4), IDTVEC(apic_slowintr5),
80         IDTVEC(apic_slowintr6), IDTVEC(apic_slowintr7),
81         IDTVEC(apic_slowintr8), IDTVEC(apic_slowintr9),
82         IDTVEC(apic_slowintr10), IDTVEC(apic_slowintr11),
83         IDTVEC(apic_slowintr12), IDTVEC(apic_slowintr13),
84         IDTVEC(apic_slowintr14), IDTVEC(apic_slowintr15),
85         IDTVEC(apic_slowintr16), IDTVEC(apic_slowintr17),
86         IDTVEC(apic_slowintr18), IDTVEC(apic_slowintr19),
87         IDTVEC(apic_slowintr20), IDTVEC(apic_slowintr21),
88         IDTVEC(apic_slowintr22), IDTVEC(apic_slowintr23);
89
90 extern inthand_t
91         IDTVEC(apic_wrongintr0), IDTVEC(apic_wrongintr1),
92         IDTVEC(apic_wrongintr2), IDTVEC(apic_wrongintr3),
93         IDTVEC(apic_wrongintr4), IDTVEC(apic_wrongintr5),
94         IDTVEC(apic_wrongintr6), IDTVEC(apic_wrongintr7),
95         IDTVEC(apic_wrongintr8), IDTVEC(apic_wrongintr9),
96         IDTVEC(apic_wrongintr10), IDTVEC(apic_wrongintr11),
97         IDTVEC(apic_wrongintr12), IDTVEC(apic_wrongintr13),
98         IDTVEC(apic_wrongintr14), IDTVEC(apic_wrongintr15),
99         IDTVEC(apic_wrongintr16), IDTVEC(apic_wrongintr17),
100         IDTVEC(apic_wrongintr18), IDTVEC(apic_wrongintr19),
101         IDTVEC(apic_wrongintr20), IDTVEC(apic_wrongintr21),
102         IDTVEC(apic_wrongintr22), IDTVEC(apic_wrongintr23);
103
104 static int apic_setvar(int, const void *);
105 static int apic_getvar(int, void *);
106 static int apic_vectorctl(int, int, int);
107 static void apic_finalize(void);
108
109 static inthand_t *apic_fastintr[APIC_HWI_VECTORS] = {
110         &IDTVEC(apic_fastintr0), &IDTVEC(apic_fastintr1),
111         &IDTVEC(apic_fastintr2), &IDTVEC(apic_fastintr3),
112         &IDTVEC(apic_fastintr4), &IDTVEC(apic_fastintr5),
113         &IDTVEC(apic_fastintr6), &IDTVEC(apic_fastintr7),
114         &IDTVEC(apic_fastintr8), &IDTVEC(apic_fastintr9),
115         &IDTVEC(apic_fastintr10), &IDTVEC(apic_fastintr11),
116         &IDTVEC(apic_fastintr12), &IDTVEC(apic_fastintr13),
117         &IDTVEC(apic_fastintr14), &IDTVEC(apic_fastintr15),
118         &IDTVEC(apic_fastintr16), &IDTVEC(apic_fastintr17),
119         &IDTVEC(apic_fastintr18), &IDTVEC(apic_fastintr19),
120         &IDTVEC(apic_fastintr20), &IDTVEC(apic_fastintr21),
121         &IDTVEC(apic_fastintr22), &IDTVEC(apic_fastintr23)
122 };
123
124 static inthand_t *apic_slowintr[APIC_HWI_VECTORS] = {
125         &IDTVEC(apic_slowintr0), &IDTVEC(apic_slowintr1),
126         &IDTVEC(apic_slowintr2), &IDTVEC(apic_slowintr3),
127         &IDTVEC(apic_slowintr4), &IDTVEC(apic_slowintr5),
128         &IDTVEC(apic_slowintr6), &IDTVEC(apic_slowintr7),
129         &IDTVEC(apic_slowintr8), &IDTVEC(apic_slowintr9),
130         &IDTVEC(apic_slowintr10), &IDTVEC(apic_slowintr11),
131         &IDTVEC(apic_slowintr12), &IDTVEC(apic_slowintr13),
132         &IDTVEC(apic_slowintr14), &IDTVEC(apic_slowintr15),
133         &IDTVEC(apic_slowintr16), &IDTVEC(apic_slowintr17),
134         &IDTVEC(apic_slowintr18), &IDTVEC(apic_slowintr19),
135         &IDTVEC(apic_slowintr20), &IDTVEC(apic_slowintr21),
136         &IDTVEC(apic_slowintr22), &IDTVEC(apic_slowintr23)
137 };
138
139 static inthand_t *apic_wrongintr[APIC_HWI_VECTORS] = {
140         &IDTVEC(apic_wrongintr0), &IDTVEC(apic_wrongintr1),
141         &IDTVEC(apic_wrongintr2), &IDTVEC(apic_wrongintr3),
142         &IDTVEC(apic_wrongintr4), &IDTVEC(apic_wrongintr5),
143         &IDTVEC(apic_wrongintr6), &IDTVEC(apic_wrongintr7),
144         &IDTVEC(apic_wrongintr8), &IDTVEC(apic_wrongintr9),
145         &IDTVEC(apic_wrongintr10), &IDTVEC(apic_wrongintr11),
146         &IDTVEC(apic_wrongintr12), &IDTVEC(apic_wrongintr13),
147         &IDTVEC(apic_wrongintr14), &IDTVEC(apic_wrongintr15),
148         &IDTVEC(apic_wrongintr16), &IDTVEC(apic_wrongintr17),
149         &IDTVEC(apic_wrongintr18), &IDTVEC(apic_wrongintr19),
150         &IDTVEC(apic_wrongintr20), &IDTVEC(apic_wrongintr21),
151         &IDTVEC(apic_wrongintr22), &IDTVEC(apic_wrongintr23)
152 };
153
154 static int apic_imcr_present;
155
156 struct machintr_abi MachIntrABI = {
157         MACHINTR_APIC,
158         APIC_INTRDIS,
159         APIC_INTREN,
160         apic_vectorctl,
161         apic_setvar,
162         apic_getvar,
163         apic_finalize
164 };
165
166 static int
167 apic_setvar(int varid, const void *buf)
168 {
169     int error = 0;
170
171     switch(varid) {
172     case MACHINTR_VAR_IMCR_PRESENT:
173         apic_imcr_present = *(const int *)buf;
174         break;
175     default:
176         error = ENOENT;
177         break;
178     }
179     return (error);
180 }
181
182 static int
183 apic_getvar(int varid, void *buf)
184 {
185     int error = 0;
186
187     switch(varid) {
188     case MACHINTR_VAR_IMCR_PRESENT:
189         *(int *)buf = apic_imcr_present;
190         break;
191     default:
192         error = ENOENT;
193         break;
194     }
195     return (error);
196 }
197
198 /*
199  * Final configuration of the BSP's local APIC:
200  *  - disable 'pic mode'.
201  *  - disable 'virtual wire mode'.
202  *  - enable NMI.
203  */
204 static void
205 apic_finalize(void)
206 {
207     u_int32_t   temp;
208
209     /*
210      * If an IMCR is present, program bit 0 to disconnect the 8259
211      * from the BSP.  The 8259 may still be connected to LINT0 on
212      * the BSP's LAPIC.
213      */
214     if (apic_imcr_present) {
215         outb(0x22, 0x70);       /* select IMCR */
216         outb(0x23, 0x01);       /* disconnect 8259 */
217     }
218
219     /*
220      * Setup lint0 (the 8259 'virtual wire' connection).  We
221      * mask the interrupt, completing the disconnection of the
222      * 8259.
223      */
224     temp = lapic.lvt_lint0;
225     temp |= APIC_LVT_MASKED;
226     lapic.lvt_lint0 = temp;
227
228     /*
229      * setup lint1 to handle an NMI 
230      */
231     temp = lapic.lvt_lint1;
232     temp &= ~APIC_LVT_MASKED;
233     lapic.lvt_lint1 = temp;
234
235     if (bootverbose)
236         apic_dump("bsp_apic_configure()");
237 }
238
239 static
240 int
241 apic_vectorctl(int op, int intr, int flags)
242 {
243     int error;
244     int vector;
245     int select;
246     u_int32_t value;
247     u_long ef;
248
249     if (intr < 0 || intr >= APIC_HWI_VECTORS)
250         return (EINVAL);
251
252     ef = read_eflags();
253     cpu_disable_intr();
254     error = 0;
255
256     switch(op) {
257     case MACHINTR_VECTOR_SETUP:
258         /*
259          * Setup an interrupt vector.  First install the vector in the
260          * cpu's Interrupt Descriptor Table (IDT).
261          */
262         if (flags & INTR_FAST) {
263             vector = TPR_SLOW_INTS + intr;
264             setidt(vector, apic_wrongintr[intr],
265                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
266             vector = TPR_FAST_INTS + intr;
267             setidt(vector, apic_fastintr[intr],
268                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
269         } else {
270             vector = TPR_SLOW_INTS + intr;
271
272             /*
273              * This is probably not needed any more. XXX
274              */
275             if (intr == apic_8254_intr || intr == 8) {
276                 vector = TPR_FAST_INTS + intr;
277             }
278             setidt(vector, apic_slowintr[intr],
279                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
280         }
281
282         /*
283          * Now reprogram the vector in the IO APIC.  In order to avoid
284          * losing an EOI for a level interrupt, which is vector based,
285          * make sure that the IO APIC is programmed for edge-triggering
286          * first, then reprogrammed with the new vector.  This should
287          * clear the IRR bit.
288          */
289         if (int_to_apicintpin[intr].ioapic >= 0) {
290             imen_lock();
291             select = int_to_apicintpin[intr].redirindex;
292             value = io_apic_read(int_to_apicintpin[intr].ioapic, select);
293             io_apic_write(int_to_apicintpin[intr].ioapic,
294                           select, (value & ~APIC_TRIGMOD_MASK));
295             io_apic_write(int_to_apicintpin[intr].ioapic,
296                           select, (value & ~IOART_INTVEC) | vector);
297             imen_unlock();
298         }
299         machintr_intren(intr);
300         break;
301     case MACHINTR_VECTOR_TEARDOWN:
302         /*
303          * Teardown an interrupt vector.  The vector should already be
304          * installed in the cpu's IDT, but make sure.
305          */
306         machintr_intrdis(intr);
307         vector = TPR_SLOW_INTS + intr;
308         setidt(vector, apic_slowintr[intr], SDT_SYS386IGT, SEL_KPL,
309                 GSEL(GCODE_SEL, SEL_KPL));
310
311         /*
312          * And then reprogram the IO APIC to point to the SLOW vector (it may
313          * have previously been pointed to the FAST version of the vector).
314          * This will allow us to keep track of spurious interrupts.
315          *
316          * In order to avoid losing an EOI for a level interrupt, which is
317          * vector based, make sure that the IO APIC is programmed for 
318          * edge-triggering first, then reprogrammed with the new vector.
319          * This should clear the IRR bit.
320          */
321         if (int_to_apicintpin[intr].ioapic >= 0) {
322             imen_lock();
323             select = int_to_apicintpin[intr].redirindex;
324             value = io_apic_read(int_to_apicintpin[intr].ioapic, select);
325             io_apic_write(int_to_apicintpin[intr].ioapic,
326                           select, (value & ~APIC_TRIGMOD_MASK));
327             io_apic_write(int_to_apicintpin[intr].ioapic,
328                           select, (value & ~IOART_INTVEC) | vector);
329             imen_unlock();
330         }
331         break;
332     case MACHINTR_VECTOR_SETDEFAULT:
333         /*
334          * This is a just-in-case an int pin is running through the 8259
335          * when we don't expect it to, or an IO APIC pin somehow wound
336          * up getting enabled without us specifically programming it in
337          * this ABI.  Note that IO APIC pins are by default programmed
338          * to IDT_OFFSET + intr.
339          */
340         vector = IDT_OFFSET + intr;
341         setidt(vector, apic_slowintr[intr], SDT_SYS386IGT, SEL_KPL,
342                 GSEL(GCODE_SEL, SEL_KPL));
343         break;
344     default:
345         error = EOPNOTSUPP;
346         break;
347     }
348
349     write_eflags(ef);
350     return (error);
351 }
352
353 #endif
354