2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
3 * Copyright (c) 1991 The Regents of the University of California.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * This code is derived from software contributed to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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
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.
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
39 * $DragonFly: src/sys/platform/pc32/icu/icu_abi.c,v 1.14 2007/07/07 12:13:47 sephe Exp $
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>
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>
56 #include <sys/thread2.h>
58 #include <machine_base/icu/elcr_var.h>
60 #include <machine_base/icu/icu.h>
61 #include <machine_base/icu/icu_ipl.h>
62 #include <machine_base/apic/ioapic.h>
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);
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)
85 static struct icu_irqmap {
86 int im_type; /* ICU_IMT_ */
87 enum intr_trigger im_trig;
88 } icu_irqmaps[MAXCPU][IDT_HWI_VECTORS];
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
95 #define ICU_IMT_ISHWI(map) ((map)->im_type != ICU_IMT_RESERVED && \
96 (map)->im_type != ICU_IMT_SYSCALL)
98 extern void ICU_INTREN(int);
99 extern void ICU_INTRDIS(int);
101 extern int imcr_present;
103 static void icu_abi_intr_enable(int);
104 static void icu_abi_intr_disable(int);
105 static void icu_abi_intr_setup(int, int);
106 static void icu_abi_intr_teardown(int);
107 static void icu_abi_intr_config(int, enum intr_trigger, enum intr_polarity);
108 static int icu_abi_intr_cpuid(int);
110 static void icu_abi_finalize(void);
111 static void icu_abi_cleanup(void);
112 static void icu_abi_setdefault(void);
113 static void icu_abi_stabilize(void);
114 static void icu_abi_initmap(void);
115 static void icu_abi_rman_setup(struct rman *);
117 struct machintr_abi MachIntrABI_ICU = {
119 .intr_disable = icu_abi_intr_disable,
120 .intr_enable = icu_abi_intr_enable,
121 .intr_setup = icu_abi_intr_setup,
122 .intr_teardown = icu_abi_intr_teardown,
123 .intr_config = icu_abi_intr_config,
124 .intr_cpuid = icu_abi_intr_cpuid,
126 .finalize = icu_abi_finalize,
127 .cleanup = icu_abi_cleanup,
128 .setdefault = icu_abi_setdefault,
129 .stabilize = icu_abi_stabilize,
130 .initmap = icu_abi_initmap,
131 .rman_setup = icu_abi_rman_setup
135 * WARNING! SMP builds can use the ICU now so this code must be MP safe.
139 icu_abi_intr_enable(int irq)
141 const struct icu_irqmap *map;
143 KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
144 ("icu enable, invalid irq %d\n", irq));
146 map = &icu_irqmaps[mycpuid][irq];
147 KASSERT(ICU_IMT_ISHWI(map),
148 ("icu enable, not hwi irq %d, type %d, cpu%d\n",
149 irq, map->im_type, mycpuid));
150 if (map->im_type != ICU_IMT_LINE) {
151 kprintf("icu enable, irq %d cpu%d not LINE\n",
160 icu_abi_intr_disable(int irq)
162 const struct icu_irqmap *map;
164 KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
165 ("icu disable, invalid irq %d\n", irq));
167 map = &icu_irqmaps[mycpuid][irq];
168 KASSERT(ICU_IMT_ISHWI(map),
169 ("icu disable, not hwi irq %d, type %d, cpu%d\n",
170 irq, map->im_type, mycpuid));
171 if (map->im_type != ICU_IMT_LINE) {
172 kprintf("icu disable, irq %d cpu%d not LINE\n",
181 * Called before interrupts are physically enabled
184 icu_abi_stabilize(void)
188 for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
190 ICU_INTREN(ICU_IRQ_SLAVE);
194 * Called after interrupts physically enabled but before the
195 * critical section is released.
198 icu_abi_cleanup(void)
200 bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
204 * Called after stablize and cleanup; critical section is not
205 * held and interrupts are not physically disabled.
208 icu_abi_finalize(void)
210 KKASSERT(MachIntrABI.type == MACHINTR_ICU);
211 KKASSERT(!ioapic_enable);
214 * If an IMCR is present, programming bit 0 disconnects the 8259
215 * from the BSP. The 8259 may still be connected to LINT0 on the
218 * If we are running SMP the LAPIC is active, try to use virtual
219 * wire mode so we can use other interrupt sources within the LAPIC
220 * in addition to the 8259.
229 icu_abi_intr_setup(int intr, int flags __unused)
231 const struct icu_irqmap *map;
234 KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
235 ("icu setup, invalid irq %d\n", intr));
237 map = &icu_irqmaps[mycpuid][intr];
238 KASSERT(ICU_IMT_ISHWI(map),
239 ("icu setup, not hwi irq %d, type %d, cpu%d\n",
240 intr, map->im_type, mycpuid));
241 if (map->im_type != ICU_IMT_LINE) {
242 kprintf("icu setup, irq %d cpu%d not LINE\n",
256 icu_abi_intr_teardown(int intr)
258 const struct icu_irqmap *map;
261 KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
262 ("icu teardown, invalid irq %d\n", intr));
264 map = &icu_irqmaps[mycpuid][intr];
265 KASSERT(ICU_IMT_ISHWI(map),
266 ("icu teardown, not hwi irq %d, type %d, cpu%d\n",
267 intr, map->im_type, mycpuid));
268 if (map->im_type != ICU_IMT_LINE) {
269 kprintf("icu teardown, irq %d cpu%d not LINE\n",
283 icu_abi_setdefault(void)
287 for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
288 if (intr == ICU_IRQ_SLAVE)
290 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT,
291 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
296 icu_abi_initmap(void)
301 * NOTE: ncpus is not ready yet
303 for (cpu = 0; cpu < MAXCPU; ++cpu) {
307 for (i = 0; i < ICU_HWI_VECTORS; ++i)
308 icu_irqmaps[cpu][i].im_type = ICU_IMT_RESERVED;
310 for (i = 0; i < ICU_HWI_VECTORS; ++i)
311 icu_irqmaps[cpu][i].im_type = ICU_IMT_LINE;
312 icu_irqmaps[cpu][ICU_IRQ_SLAVE].im_type =
316 for (i = 0; i < ICU_HWI_VECTORS; ++i) {
317 icu_irqmaps[cpu][i].im_trig =
318 elcr_read_trigger(i);
322 * NOTE: Trigger mode does not matter at all
324 for (i = 0; i < ICU_HWI_VECTORS; ++i) {
325 icu_irqmaps[cpu][i].im_trig =
330 icu_irqmaps[cpu][IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type =
336 icu_abi_intr_config(int irq, enum intr_trigger trig,
337 enum intr_polarity pola __unused)
339 struct icu_irqmap *map;
341 KKASSERT(trig == INTR_TRIGGER_EDGE || trig == INTR_TRIGGER_LEVEL);
343 KKASSERT(irq >= 0 && irq < IDT_HWI_VECTORS);
344 map = &icu_irqmaps[0][irq];
346 KKASSERT(map->im_type == ICU_IMT_LINE);
348 /* TODO: Check whether it is configured or not */
350 if (trig == map->im_trig)
354 kprintf("ICU: irq %d, %s -> %s\n", irq,
355 intr_str_trigger(map->im_trig),
356 intr_str_trigger(trig));
362 kprintf("ICU: no ELCR, skip irq %d config\n", irq);
365 elcr_write_trigger(irq, map->im_trig);
369 icu_abi_intr_cpuid(int irq __unused)
375 icu_abi_rman_setup(struct rman *rm)
379 KASSERT(rm->rm_cpuid >= 0 && rm->rm_cpuid < MAXCPU,
380 ("invalid rman cpuid %d", rm->rm_cpuid));
383 for (i = 0; i < IDT_HWI_VECTORS; ++i) {
384 const struct icu_irqmap *map = &icu_irqmaps[rm->rm_cpuid][i];
387 if (ICU_IMT_ISHWI(map))
390 if (ICU_IMT_ISHWI(map)) {
395 kprintf("ICU: rman cpu%d %d - %d\n",
396 rm->rm_cpuid, start, end);
398 if (rman_manage_region(rm, start, end)) {
399 panic("rman_manage_region"
400 "(cpu%d %d - %d)", rm->rm_cpuid,
410 kprintf("ICU: rman cpu%d %d - %d\n",
411 rm->rm_cpuid, start, end);
413 if (rman_manage_region(rm, start, end)) {
414 panic("rman_manage_region(cpu%d %d - %d)",
415 rm->rm_cpuid, start, end);