63399bd9452af51603ff89216198cf9999e67804
[dragonfly.git] / sys / platform / pc32 / icu / icu_abi.c
1 /*
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  * 
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * This code is derived from software contributed to Berkeley by
10  * William Jolitz.
11  * 
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  * 3. Neither the name of The DragonFly Project nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific, prior written permission.
25  * 
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
30  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
32  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  * 
39  * $DragonFly: src/sys/platform/pc32/icu/icu_abi.c,v 1.14 2007/07/07 12:13:47 sephe Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/machintr.h>
46 #include <sys/interrupt.h>
47 #include <sys/bus.h>
48
49 #include <machine/segments.h>
50 #include <machine/md_var.h>
51 #include <machine/intr_machdep.h>
52 #include <machine/globaldata.h>
53 #include <machine/smp.h>
54
55 #include <sys/thread2.h>
56
57 #include <machine_base/apic/ioapic_abi.h>
58 #include <machine_base/isa/elcr_var.h>
59
60 #include "icu.h"
61 #include "icu_ipl.h"
62
63 extern inthand_t
64         IDTVEC(icu_intr0),      IDTVEC(icu_intr1),
65         IDTVEC(icu_intr2),      IDTVEC(icu_intr3),
66         IDTVEC(icu_intr4),      IDTVEC(icu_intr5),
67         IDTVEC(icu_intr6),      IDTVEC(icu_intr7),
68         IDTVEC(icu_intr8),      IDTVEC(icu_intr9),
69         IDTVEC(icu_intr10),     IDTVEC(icu_intr11),
70         IDTVEC(icu_intr12),     IDTVEC(icu_intr13),
71         IDTVEC(icu_intr14),     IDTVEC(icu_intr15);
72
73 static inthand_t *icu_intr[ICU_HWI_VECTORS] = {
74         &IDTVEC(icu_intr0),     &IDTVEC(icu_intr1),
75         &IDTVEC(icu_intr2),     &IDTVEC(icu_intr3),
76         &IDTVEC(icu_intr4),     &IDTVEC(icu_intr5),
77         &IDTVEC(icu_intr6),     &IDTVEC(icu_intr7),
78         &IDTVEC(icu_intr8),     &IDTVEC(icu_intr9),
79         &IDTVEC(icu_intr10),    &IDTVEC(icu_intr11),
80         &IDTVEC(icu_intr12),    &IDTVEC(icu_intr13),
81         &IDTVEC(icu_intr14),    &IDTVEC(icu_intr15)
82 };
83
84 static struct icu_irqmap {
85         int                     im_type;        /* ICU_IMT_ */
86         enum intr_trigger       im_trig;
87 } icu_irqmaps[MAX_HARDINTS];    /* XXX MAX_HARDINTS may not be correct */
88
89 #define ICU_IMT_UNUSED          0       /* KEEP THIS */
90 #define ICU_IMT_RESERVED        1
91 #define ICU_IMT_LINE            2
92 #define ICU_IMT_SYSCALL         3
93
94 extern void     ICU_INTREN(int);
95 extern void     ICU_INTRDIS(int);
96
97 static int      icu_vectorctl(int, int, int);
98 static int      icu_setvar(int, const void *);
99 static int      icu_getvar(int, void *);
100 static void     icu_finalize(void);
101 static void     icu_cleanup(void);
102 static void     icu_setdefault(void);
103 static void     icu_stabilize(void);
104 static void     icu_initmap(void);
105 static void     icu_intr_config(int, enum intr_trigger, enum intr_polarity);
106
107 struct machintr_abi MachIntrABI_ICU = {
108         MACHINTR_ICU,
109         .intrdis        = ICU_INTRDIS,
110         .intren         = ICU_INTREN,
111         .vectorctl      = icu_vectorctl,
112         .setvar         = icu_setvar,
113         .getvar         = icu_getvar,
114         .finalize       = icu_finalize,
115         .cleanup        = icu_cleanup,
116         .setdefault     = icu_setdefault,
117         .stabilize      = icu_stabilize,
118         .initmap        = icu_initmap,
119         .intr_config    = icu_intr_config
120 };
121
122 /*
123  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
124  */
125 static int
126 icu_setvar(int varid, const void *buf)
127 {
128         return ENOENT;
129 }
130
131 static int
132 icu_getvar(int varid, void *buf)
133 {
134         return ENOENT;
135 }
136
137 /*
138  * Called before interrupts are physically enabled
139  */
140 static void
141 icu_stabilize(void)
142 {
143         int intr;
144
145         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
146                 machintr_intrdis(intr);
147         machintr_intren(ICU_IRQ_SLAVE);
148 }
149
150 /*
151  * Called after interrupts physically enabled but before the
152  * critical section is released.
153  */
154 static void
155 icu_cleanup(void)
156 {
157         bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
158 }
159
160 /*
161  * Called after stablize and cleanup; critical section is not
162  * held and interrupts are not physically disabled.
163  *
164  * For SMP:
165  * Further delayed after BSP's LAPIC is initialized
166  */
167 static void
168 icu_finalize(void)
169 {
170         KKASSERT(MachIntrABI.type == MACHINTR_ICU);
171
172 #ifdef SMP
173         if (apic_io_enable) {
174                 /*
175                  * MachIntrABI switching will happen in
176                  * MachIntrABI_IOAPIC.finalize()
177                  */
178                 MachIntrABI_IOAPIC.finalize();
179                 return;
180         }
181
182         /*
183          * If an IMCR is present, programming bit 0 disconnects the 8259
184          * from the BSP.  The 8259 may still be connected to LINT0 on the
185          * BSP's LAPIC.
186          *
187          * If we are running SMP the LAPIC is active, try to use virtual
188          * wire mode so we can use other interrupt sources within the LAPIC
189          * in addition to the 8259.
190          */
191         if (imcr_present) {
192                 u_long ef;
193
194                 crit_enter();
195
196                 ef = read_eflags();
197                 cpu_disable_intr();
198
199                 outb(0x22, 0x70);
200                 outb(0x23, 0x01);
201
202                 write_eflags(ef);
203
204                 crit_exit();
205         }
206 #endif  /* SMP */
207 }
208
209 static int
210 icu_vectorctl(int op, int intr, int flags)
211 {
212         int error;
213         u_long ef;
214
215         if (intr < 0 || intr >= ICU_HWI_VECTORS || intr == ICU_IRQ_SLAVE)
216                 return EINVAL;
217
218         ef = read_eflags();
219         cpu_disable_intr();
220         error = 0;
221
222         switch (op) {
223         case MACHINTR_VECTOR_SETUP:
224                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT,
225                        SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
226                 machintr_intren(intr);
227                 break;
228
229         case MACHINTR_VECTOR_TEARDOWN:
230                 machintr_intrdis(intr);
231                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT,
232                        SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
233                 break;
234
235         default:
236                 error = EOPNOTSUPP;
237                 break;
238         }
239         write_eflags(ef);
240         return error;
241 }
242
243 static void
244 icu_setdefault(void)
245 {
246         int intr;
247
248         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
249                 if (intr == ICU_IRQ_SLAVE)
250                         continue;
251                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT,
252                        SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
253         }
254 }
255
256 static void
257 icu_initmap(void)
258 {
259         int i;
260
261         for (i = 0; i < ICU_HWI_VECTORS; ++i)
262                 icu_irqmaps[i].im_type = ICU_IMT_LINE;
263         icu_irqmaps[ICU_IRQ_SLAVE].im_type = ICU_IMT_RESERVED;
264
265         if (elcr_found) {
266                 for (i = 0; i < ICU_HWI_VECTORS; ++i)
267                         icu_irqmaps[i].im_trig = elcr_read_trigger(i);
268         } else {
269                 for (i = 0; i < ICU_HWI_VECTORS; ++i) {
270                         switch (i) {
271                         case 0:
272                         case 1:
273                         case 2:
274                         case 8:
275                         case 13:
276                                 icu_irqmaps[i].im_trig = INTR_TRIGGER_EDGE;
277                                 break;
278
279                         default:
280                                 icu_irqmaps[i].im_trig = INTR_TRIGGER_LEVEL;
281                                 break;
282                         }
283                 }
284         }
285         icu_irqmaps[IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type = ICU_IMT_SYSCALL;
286 }
287
288 static void
289 icu_intr_config(int irq __unused, enum intr_trigger trig __unused,
290     enum intr_polarity pola __unused)
291 {
292 }