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