i386/icu: Implement rman_setup MachIntrABI method
[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/rman.h>
48 #include <sys/bus.h>
49
50 #include <machine/segments.h>
51 #include <machine/md_var.h>
52 #include <machine/intr_machdep.h>
53 #include <machine/globaldata.h>
54 #include <machine/smp.h>
55
56 #include <sys/thread2.h>
57
58 #include <machine_base/icu/elcr_var.h>
59
60 #include <machine_base/icu/icu.h>
61 #include <machine_base/icu/icu_ipl.h>
62 #include <machine_base/apic/ioapic.h>
63
64 extern inthand_t
65         IDTVEC(icu_intr0),      IDTVEC(icu_intr1),
66         IDTVEC(icu_intr2),      IDTVEC(icu_intr3),
67         IDTVEC(icu_intr4),      IDTVEC(icu_intr5),
68         IDTVEC(icu_intr6),      IDTVEC(icu_intr7),
69         IDTVEC(icu_intr8),      IDTVEC(icu_intr9),
70         IDTVEC(icu_intr10),     IDTVEC(icu_intr11),
71         IDTVEC(icu_intr12),     IDTVEC(icu_intr13),
72         IDTVEC(icu_intr14),     IDTVEC(icu_intr15);
73
74 static inthand_t *icu_intr[ICU_HWI_VECTORS] = {
75         &IDTVEC(icu_intr0),     &IDTVEC(icu_intr1),
76         &IDTVEC(icu_intr2),     &IDTVEC(icu_intr3),
77         &IDTVEC(icu_intr4),     &IDTVEC(icu_intr5),
78         &IDTVEC(icu_intr6),     &IDTVEC(icu_intr7),
79         &IDTVEC(icu_intr8),     &IDTVEC(icu_intr9),
80         &IDTVEC(icu_intr10),    &IDTVEC(icu_intr11),
81         &IDTVEC(icu_intr12),    &IDTVEC(icu_intr13),
82         &IDTVEC(icu_intr14),    &IDTVEC(icu_intr15)
83 };
84
85 static struct icu_irqmap {
86         int                     im_type;        /* ICU_IMT_ */
87         enum intr_trigger       im_trig;
88 } icu_irqmaps[MAXCPU][IDT_HWI_VECTORS];
89
90 #define ICU_IMT_UNUSED          0       /* KEEP THIS */
91 #define ICU_IMT_RESERVED        1
92 #define ICU_IMT_LINE            2
93 #define ICU_IMT_SYSCALL         3
94
95 #define ICU_IMT_ISHWI(map)      ((map)->im_type != ICU_IMT_RESERVED && \
96                                  (map)->im_type != ICU_IMT_SYSCALL)
97
98 extern void     ICU_INTREN(int);
99 extern void     ICU_INTRDIS(int);
100
101 extern int      imcr_present;
102
103 static void     icu_abi_intr_setup(int, int);
104 static void     icu_abi_intr_teardown(int);
105 static void     icu_abi_intr_config(int, enum intr_trigger, enum intr_polarity);
106 static int      icu_abi_intr_cpuid(int);
107
108 static void     icu_abi_finalize(void);
109 static void     icu_abi_cleanup(void);
110 static void     icu_abi_setdefault(void);
111 static void     icu_abi_stabilize(void);
112 static void     icu_abi_initmap(void);
113 static void     icu_abi_rman_setup(struct rman *);
114
115 struct machintr_abi MachIntrABI_ICU = {
116         MACHINTR_ICU,
117
118         .intr_disable   = ICU_INTRDIS,
119         .intr_enable    = ICU_INTREN,
120         .intr_setup     = icu_abi_intr_setup,
121         .intr_teardown  = icu_abi_intr_teardown,
122         .intr_config    = icu_abi_intr_config,
123         .intr_cpuid     = icu_abi_intr_cpuid,
124
125         .finalize       = icu_abi_finalize,
126         .cleanup        = icu_abi_cleanup,
127         .setdefault     = icu_abi_setdefault,
128         .stabilize      = icu_abi_stabilize,
129         .initmap        = icu_abi_initmap,
130         .rman_setup     = icu_abi_rman_setup
131 };
132
133 /*
134  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
135  */
136
137 /*
138  * Called before interrupts are physically enabled
139  */
140 static void
141 icu_abi_stabilize(void)
142 {
143         int intr;
144
145         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
146                 machintr_intr_disable(intr);
147         machintr_intr_enable(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_abi_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 static void
165 icu_abi_finalize(void)
166 {
167         KKASSERT(MachIntrABI.type == MACHINTR_ICU);
168         KKASSERT(!ioapic_enable);
169
170         /*
171          * If an IMCR is present, programming bit 0 disconnects the 8259
172          * from the BSP.  The 8259 may still be connected to LINT0 on the
173          * BSP's LAPIC.
174          *
175          * If we are running SMP the LAPIC is active, try to use virtual
176          * wire mode so we can use other interrupt sources within the LAPIC
177          * in addition to the 8259.
178          */
179         if (imcr_present) {
180                 outb(0x22, 0x70);
181                 outb(0x23, 0x01);
182         }
183 }
184
185 static void
186 icu_abi_intr_setup(int intr, int flags __unused)
187 {
188         u_long ef;
189
190         KKASSERT(intr >= 0 && intr < ICU_HWI_VECTORS && intr != ICU_IRQ_SLAVE);
191
192         ef = read_eflags();
193         cpu_disable_intr();
194
195         machintr_intr_enable(intr);
196
197         write_eflags(ef);
198 }
199
200 static void
201 icu_abi_intr_teardown(int intr)
202 {
203         u_long ef;
204
205         KKASSERT(intr >= 0 && intr < ICU_HWI_VECTORS && intr != ICU_IRQ_SLAVE);
206
207         ef = read_eflags();
208         cpu_disable_intr();
209
210         machintr_intr_disable(intr);
211
212         write_eflags(ef);
213 }
214
215 static void
216 icu_abi_setdefault(void)
217 {
218         int intr;
219
220         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
221                 if (intr == ICU_IRQ_SLAVE)
222                         continue;
223                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT,
224                        SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
225         }
226 }
227
228 static void
229 icu_abi_initmap(void)
230 {
231         int cpu;
232
233         /*
234          * NOTE: ncpus is not ready yet
235          */
236         for (cpu = 0; cpu < MAXCPU; ++cpu) {
237                 int i;
238
239                 if (cpu != 0) {
240                         for (i = 0; i < ICU_HWI_VECTORS; ++i)
241                                 icu_irqmaps[cpu][i].im_type = ICU_IMT_RESERVED;
242                 } else {
243                         for (i = 0; i < ICU_HWI_VECTORS; ++i)
244                                 icu_irqmaps[cpu][i].im_type = ICU_IMT_LINE;
245                         icu_irqmaps[cpu][ICU_IRQ_SLAVE].im_type =
246                             ICU_IMT_RESERVED;
247
248                         if (elcr_found) {
249                                 for (i = 0; i < ICU_HWI_VECTORS; ++i) {
250                                         icu_irqmaps[cpu][i].im_trig =
251                                             elcr_read_trigger(i);
252                                 }
253                         } else {
254                                 /*
255                                  * NOTE: Trigger mode does not matter at all
256                                  */
257                                 for (i = 0; i < ICU_HWI_VECTORS; ++i) {
258                                         icu_irqmaps[cpu][i].im_trig =
259                                             INTR_TRIGGER_EDGE;
260                                 }
261                         }
262                 }
263                 icu_irqmaps[cpu][IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type =
264                     ICU_IMT_SYSCALL;
265         }
266 }
267
268 static void
269 icu_abi_intr_config(int irq, enum intr_trigger trig,
270     enum intr_polarity pola __unused)
271 {
272         struct icu_irqmap *map;
273
274         KKASSERT(trig == INTR_TRIGGER_EDGE || trig == INTR_TRIGGER_LEVEL);
275
276         KKASSERT(irq >= 0 && irq < IDT_HWI_VECTORS);
277         map = &icu_irqmaps[0][irq];
278
279         KKASSERT(map->im_type == ICU_IMT_LINE);
280
281         /* TODO: Check whether it is configured or not */
282
283         if (trig == map->im_trig)
284                 return;
285
286         if (bootverbose) {
287                 kprintf("ICU: irq %d, %s -> %s\n", irq,
288                         intr_str_trigger(map->im_trig),
289                         intr_str_trigger(trig));
290         }
291         map->im_trig = trig;
292
293         if (!elcr_found) {
294                 if (bootverbose)
295                         kprintf("ICU: no ELCR, skip irq %d config\n", irq);
296                 return;
297         }
298         elcr_write_trigger(irq, map->im_trig);
299 }
300
301 static int
302 icu_abi_intr_cpuid(int irq __unused)
303 {
304         return 0;
305 }
306
307 static void
308 icu_abi_rman_setup(struct rman *rm)
309 {
310         int start, end, i;
311
312         KASSERT(rm->rm_cpuid >= 0 && rm->rm_cpuid < MAXCPU,
313             ("invalid rman cpuid %d", rm->rm_cpuid));
314
315         start = end = -1;
316         for (i = 0; i < IDT_HWI_VECTORS; ++i) {
317                 const struct icu_irqmap *map = &icu_irqmaps[rm->rm_cpuid][i];
318
319                 if (start < 0) {
320                         if (ICU_IMT_ISHWI(map))
321                                 start = end = i;
322                 } else {
323                         if (ICU_IMT_ISHWI(map)) {
324                                 end = i;
325                         } else {
326                                 KKASSERT(end >= 0);
327                                 if (bootverbose) {
328                                         kprintf("ICU: rman cpu%d %d - %d\n",
329                                             rm->rm_cpuid, start, end);
330                                 }
331                                 if (rman_manage_region(rm, start, end)) {
332                                         panic("rman_manage_region"
333                                             "(cpu%d %d - %d)", rm->rm_cpuid,
334                                             start, end);
335                                 }
336                                 start = end = -1;
337                         }
338                 }
339         }
340         if (start >= 0) {
341                 KKASSERT(end >= 0);
342                 if (bootverbose) {
343                         kprintf("ICU: rman cpu%d %d - %d\n",
344                             rm->rm_cpuid, start, end);
345                 }
346                 if (rman_manage_region(rm, start, end)) {
347                         panic("rman_manage_region(cpu%d %d - %d)",
348                             rm->rm_cpuid, start, end);
349                 }
350         }
351 }