| 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 | |
| 59 | #include "icu.h" |
| 60 | #include "icu_ipl.h" |
| 61 | |
| 62 | extern inthand_t |
| 63 | IDTVEC(icu_intr0), IDTVEC(icu_intr1), |
| 64 | IDTVEC(icu_intr2), IDTVEC(icu_intr3), |
| 65 | IDTVEC(icu_intr4), IDTVEC(icu_intr5), |
| 66 | IDTVEC(icu_intr6), IDTVEC(icu_intr7), |
| 67 | IDTVEC(icu_intr8), IDTVEC(icu_intr9), |
| 68 | IDTVEC(icu_intr10), IDTVEC(icu_intr11), |
| 69 | IDTVEC(icu_intr12), IDTVEC(icu_intr13), |
| 70 | IDTVEC(icu_intr14), IDTVEC(icu_intr15); |
| 71 | |
| 72 | static inthand_t *icu_intr[ICU_HWI_VECTORS] = { |
| 73 | &IDTVEC(icu_intr0), &IDTVEC(icu_intr1), |
| 74 | &IDTVEC(icu_intr2), &IDTVEC(icu_intr3), |
| 75 | &IDTVEC(icu_intr4), &IDTVEC(icu_intr5), |
| 76 | &IDTVEC(icu_intr6), &IDTVEC(icu_intr7), |
| 77 | &IDTVEC(icu_intr8), &IDTVEC(icu_intr9), |
| 78 | &IDTVEC(icu_intr10), &IDTVEC(icu_intr11), |
| 79 | &IDTVEC(icu_intr12), &IDTVEC(icu_intr13), |
| 80 | &IDTVEC(icu_intr14), &IDTVEC(icu_intr15) |
| 81 | }; |
| 82 | |
| 83 | extern void ICU_INTREN(int); |
| 84 | extern void ICU_INTRDIS(int); |
| 85 | |
| 86 | static int icu_vectorctl(int, int, int); |
| 87 | static int icu_setvar(int, const void *); |
| 88 | static int icu_getvar(int, void *); |
| 89 | static void icu_finalize(void); |
| 90 | static void icu_cleanup(void); |
| 91 | static void icu_setdefault(void); |
| 92 | static void icu_stabilize(void); |
| 93 | |
| 94 | struct machintr_abi MachIntrABI_ICU = { |
| 95 | MACHINTR_ICU, |
| 96 | .intrdis = ICU_INTRDIS, |
| 97 | .intren = ICU_INTREN, |
| 98 | .vectorctl = icu_vectorctl, |
| 99 | .setvar = icu_setvar, |
| 100 | .getvar = icu_getvar, |
| 101 | .finalize = icu_finalize, |
| 102 | .cleanup = icu_cleanup, |
| 103 | .setdefault = icu_setdefault, |
| 104 | .stabilize = icu_stabilize |
| 105 | }; |
| 106 | |
| 107 | static int icu_imcr_present; |
| 108 | |
| 109 | /* |
| 110 | * WARNING! SMP builds can use the ICU now so this code must be MP safe. |
| 111 | */ |
| 112 | static int |
| 113 | icu_setvar(int varid, const void *buf) |
| 114 | { |
| 115 | int error = 0; |
| 116 | |
| 117 | switch (varid) { |
| 118 | case MACHINTR_VAR_IMCR_PRESENT: |
| 119 | icu_imcr_present = *(const int *)buf; |
| 120 | break; |
| 121 | |
| 122 | default: |
| 123 | error = ENOENT; |
| 124 | break; |
| 125 | } |
| 126 | return error; |
| 127 | } |
| 128 | |
| 129 | static int |
| 130 | icu_getvar(int varid, void *buf) |
| 131 | { |
| 132 | int error = 0; |
| 133 | |
| 134 | switch (varid) { |
| 135 | case MACHINTR_VAR_IMCR_PRESENT: |
| 136 | *(int *)buf = icu_imcr_present; |
| 137 | break; |
| 138 | |
| 139 | default: |
| 140 | error = ENOENT; |
| 141 | break; |
| 142 | } |
| 143 | return error; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Called before interrupts are physically enabled |
| 148 | */ |
| 149 | static void |
| 150 | icu_stabilize(void) |
| 151 | { |
| 152 | int intr; |
| 153 | |
| 154 | for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) |
| 155 | machintr_intrdis(intr); |
| 156 | machintr_intren(ICU_IRQ_SLAVE); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Called after interrupts physically enabled but before the |
| 161 | * critical section is released. |
| 162 | */ |
| 163 | static void |
| 164 | icu_cleanup(void) |
| 165 | { |
| 166 | bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending)); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Called after stablize and cleanup; critical section is not |
| 171 | * held and interrupts are not physically disabled. |
| 172 | * |
| 173 | * For SMP: |
| 174 | * Further delayed after BSP's LAPIC is initialized |
| 175 | */ |
| 176 | static void |
| 177 | icu_finalize(void) |
| 178 | { |
| 179 | KKASSERT(MachIntrABI.type == MACHINTR_ICU); |
| 180 | |
| 181 | #ifdef SMP |
| 182 | if (apic_io_enable) { |
| 183 | /* |
| 184 | * MachIntrABI switching will happen in |
| 185 | * MachIntrABI_IOAPIC.finalize() |
| 186 | */ |
| 187 | MachIntrABI_IOAPIC.setvar(MACHINTR_VAR_IMCR_PRESENT, |
| 188 | &icu_imcr_present); |
| 189 | MachIntrABI_IOAPIC.finalize(); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * If an IMCR is present, programming bit 0 disconnects the 8259 |
| 195 | * from the BSP. The 8259 may still be connected to LINT0 on the |
| 196 | * BSP's LAPIC. |
| 197 | * |
| 198 | * If we are running SMP the LAPIC is active, try to use virtual |
| 199 | * wire mode so we can use other interrupt sources within the LAPIC |
| 200 | * in addition to the 8259. |
| 201 | */ |
| 202 | if (icu_imcr_present) { |
| 203 | u_long ef; |
| 204 | |
| 205 | crit_enter(); |
| 206 | |
| 207 | ef = read_eflags(); |
| 208 | cpu_disable_intr(); |
| 209 | |
| 210 | outb(0x22, 0x70); |
| 211 | outb(0x23, 0x01); |
| 212 | |
| 213 | write_eflags(ef); |
| 214 | |
| 215 | crit_exit(); |
| 216 | } |
| 217 | #endif /* SMP */ |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | icu_vectorctl(int op, int intr, int flags) |
| 222 | { |
| 223 | int error; |
| 224 | u_long ef; |
| 225 | |
| 226 | if (intr < 0 || intr >= ICU_HWI_VECTORS || intr == ICU_IRQ_SLAVE) |
| 227 | return EINVAL; |
| 228 | |
| 229 | ef = read_eflags(); |
| 230 | cpu_disable_intr(); |
| 231 | error = 0; |
| 232 | |
| 233 | switch (op) { |
| 234 | case MACHINTR_VECTOR_SETUP: |
| 235 | setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT, |
| 236 | SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); |
| 237 | machintr_intren(intr); |
| 238 | break; |
| 239 | |
| 240 | case MACHINTR_VECTOR_TEARDOWN: |
| 241 | machintr_intrdis(intr); |
| 242 | setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT, |
| 243 | SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); |
| 244 | break; |
| 245 | |
| 246 | default: |
| 247 | error = EOPNOTSUPP; |
| 248 | break; |
| 249 | } |
| 250 | write_eflags(ef); |
| 251 | return error; |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | icu_setdefault(void) |
| 256 | { |
| 257 | int intr; |
| 258 | |
| 259 | for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) { |
| 260 | if (intr == ICU_IRQ_SLAVE) |
| 261 | continue; |
| 262 | setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYS386IGT, |
| 263 | SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); |
| 264 | } |
| 265 | } |