i386/MachIntrABI: Remove unnecessary setidt in intr_setup/teardown
[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/icu/elcr_var.h>
58
59 #include <machine_base/icu/icu.h>
60 #include <machine_base/icu/icu_ipl.h>
61 #include <machine_base/apic/ioapic.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[IDT_HWI_VECTORS];
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 extern int      imcr_present;
98
99 static void     icu_abi_intr_setup(int, int);
100 static void     icu_abi_intr_teardown(int);
101 static void     icu_abi_intr_config(int, enum intr_trigger, enum intr_polarity);
102 static int      icu_abi_intr_cpuid(int);
103
104 static void     icu_abi_finalize(void);
105 static void     icu_abi_cleanup(void);
106 static void     icu_abi_setdefault(void);
107 static void     icu_abi_stabilize(void);
108 static void     icu_abi_initmap(void);
109
110 struct machintr_abi MachIntrABI_ICU = {
111         MACHINTR_ICU,
112
113         .intr_disable   = ICU_INTRDIS,
114         .intr_enable    = ICU_INTREN,
115         .intr_setup     = icu_abi_intr_setup,
116         .intr_teardown  = icu_abi_intr_teardown,
117         .intr_config    = icu_abi_intr_config,
118         .intr_cpuid     = icu_abi_intr_cpuid,
119
120         .finalize       = icu_abi_finalize,
121         .cleanup        = icu_abi_cleanup,
122         .setdefault     = icu_abi_setdefault,
123         .stabilize      = icu_abi_stabilize,
124         .initmap        = icu_abi_initmap
125 };
126
127 /*
128  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
129  */
130
131 /*
132  * Called before interrupts are physically enabled
133  */
134 static void
135 icu_abi_stabilize(void)
136 {
137         int intr;
138
139         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
140                 machintr_intr_disable(intr);
141         machintr_intr_enable(ICU_IRQ_SLAVE);
142 }
143
144 /*
145  * Called after interrupts physically enabled but before the
146  * critical section is released.
147  */
148 static void
149 icu_abi_cleanup(void)
150 {
151         bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
152 }
153
154 /*
155  * Called after stablize and cleanup; critical section is not
156  * held and interrupts are not physically disabled.
157  */
158 static void
159 icu_abi_finalize(void)
160 {
161         KKASSERT(MachIntrABI.type == MACHINTR_ICU);
162         KKASSERT(!ioapic_enable);
163
164         /*
165          * If an IMCR is present, programming bit 0 disconnects the 8259
166          * from the BSP.  The 8259 may still be connected to LINT0 on the
167          * BSP's LAPIC.
168          *
169          * If we are running SMP the LAPIC is active, try to use virtual
170          * wire mode so we can use other interrupt sources within the LAPIC
171          * in addition to the 8259.
172          */
173         if (imcr_present) {
174                 outb(0x22, 0x70);
175                 outb(0x23, 0x01);
176         }
177 }
178
179 static void
180 icu_abi_intr_setup(int intr, int flags __unused)
181 {
182         u_long ef;
183
184         KKASSERT(intr >= 0 && intr < ICU_HWI_VECTORS && intr != ICU_IRQ_SLAVE);
185
186         ef = read_eflags();
187         cpu_disable_intr();
188
189         machintr_intr_enable(intr);
190
191         write_eflags(ef);
192 }
193
194 static void
195 icu_abi_intr_teardown(int intr)
196 {
197         u_long ef;
198
199         KKASSERT(intr >= 0 && intr < ICU_HWI_VECTORS && intr != ICU_IRQ_SLAVE);
200
201         ef = read_eflags();
202         cpu_disable_intr();
203
204         machintr_intr_disable(intr);
205
206         write_eflags(ef);
207 }
208
209 static void
210 icu_abi_setdefault(void)
211 {
212         int intr;
213
214         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
215                 if (intr == ICU_IRQ_SLAVE)
216                         continue;
217                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT,
218                        SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
219         }
220 }
221
222 static void
223 icu_abi_initmap(void)
224 {
225         int i;
226
227         for (i = 0; i < ICU_HWI_VECTORS; ++i)
228                 icu_irqmaps[i].im_type = ICU_IMT_LINE;
229         icu_irqmaps[ICU_IRQ_SLAVE].im_type = ICU_IMT_RESERVED;
230
231         if (elcr_found) {
232                 for (i = 0; i < ICU_HWI_VECTORS; ++i)
233                         icu_irqmaps[i].im_trig = elcr_read_trigger(i);
234         } else {
235                 /*
236                  * NOTE: Trigger mode does not matter at all
237                  */
238                 for (i = 0; i < ICU_HWI_VECTORS; ++i)
239                         icu_irqmaps[i].im_trig = INTR_TRIGGER_EDGE;
240         }
241         icu_irqmaps[IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type = ICU_IMT_SYSCALL;
242 }
243
244 static void
245 icu_abi_intr_config(int irq, enum intr_trigger trig,
246     enum intr_polarity pola __unused)
247 {
248         struct icu_irqmap *map;
249
250         KKASSERT(trig == INTR_TRIGGER_EDGE || trig == INTR_TRIGGER_LEVEL);
251
252         KKASSERT(irq >= 0 && irq < IDT_HWI_VECTORS);
253         map = &icu_irqmaps[irq];
254
255         KKASSERT(map->im_type == ICU_IMT_LINE);
256
257         /* TODO: Check whether it is configured or not */
258
259         if (trig == map->im_trig)
260                 return;
261
262         if (bootverbose) {
263                 kprintf("ICU: irq %d, %s -> %s\n", irq,
264                         intr_str_trigger(map->im_trig),
265                         intr_str_trigger(trig));
266         }
267         map->im_trig = trig;
268
269         if (!elcr_found) {
270                 if (bootverbose)
271                         kprintf("ICU: no ELCR, skip irq %d config\n", irq);
272                 return;
273         }
274         elcr_write_trigger(irq, map->im_trig);
275 }
276
277 static int
278 icu_abi_intr_cpuid(int irq __unused)
279 {
280         return 0;
281 }