1 /* $OpenBSD: acpimadt.c,v 1.22 2009/02/16 20:11:06 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2006 Mark Kettenis <kettenis@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/malloc.h> 22 23 #include <machine/apicvar.h> 24 #include <machine/cpuvar.h> 25 #include <machine/bus.h> 26 27 #include <dev/acpi/acpireg.h> 28 #include <dev/acpi/acpivar.h> 29 #include <dev/acpi/acpidev.h> 30 #include <dev/acpi/amltypes.h> 31 #include <dev/acpi/dsdt.h> 32 33 #include <machine/i8259.h> 34 #include <machine/i82093reg.h> 35 #include <machine/i82093var.h> 36 #include <machine/i82489reg.h> 37 #include <machine/i82489var.h> 38 39 #include <machine/mpbiosvar.h> 40 41 #include "ioapic.h" 42 43 u_int8_t acpi_lapic_flags[LAPIC_MAP_SIZE]; 44 45 int acpimadt_match(struct device *, void *, void *); 46 void acpimadt_attach(struct device *, struct device *, void *); 47 48 struct cfattach acpimadt_ca = { 49 sizeof(struct device), acpimadt_match, acpimadt_attach 50 }; 51 52 struct cfdriver acpimadt_cd = { 53 NULL, "acpimadt", DV_DULL 54 }; 55 56 int acpimadt_validate(struct acpi_madt *); 57 void acpimadt_cfg_intr(int, u_int32_t *); 58 int acpimadt_print(void *, const char *); 59 60 int 61 acpimadt_match(struct device *parent, void *match, void *aux) 62 { 63 struct acpi_attach_args *aaa = aux; 64 struct acpi_table_header *hdr; 65 66 /* 67 * If we do not have a table, it is not us 68 */ 69 if (aaa->aaa_table == NULL) 70 return (0); 71 72 /* 73 * If it is an MADT table, we can attach 74 */ 75 hdr = (struct acpi_table_header *)aaa->aaa_table; 76 if (memcmp(hdr->signature, MADT_SIG, sizeof(MADT_SIG) - 1) != 0) 77 return (0); 78 79 return (1); 80 } 81 82 int 83 acpimadt_validate(struct acpi_madt *madt) 84 { 85 caddr_t addr = (caddr_t)(madt + 1); 86 87 while (addr < (caddr_t)madt + madt->hdr.length) { 88 union acpi_madt_entry *entry = (union acpi_madt_entry *)addr; 89 u_int8_t length = entry->madt_lapic.length; 90 91 if (length < 2) 92 return (0); 93 94 if (addr + length > (caddr_t)madt + madt->hdr.length) 95 return (0); 96 97 switch (entry->madt_lapic.apic_type) { 98 case ACPI_MADT_LAPIC: 99 if (length != sizeof(entry->madt_lapic)) 100 return (0); 101 break; 102 case ACPI_MADT_IOAPIC: 103 if (length != sizeof(entry->madt_ioapic)) 104 return (0); 105 break; 106 case ACPI_MADT_OVERRIDE: 107 if (length != sizeof(entry->madt_override)) 108 return (0); 109 break; 110 case ACPI_MADT_NMI: 111 if (length != sizeof(entry->madt_nmi)) 112 return (0); 113 break; 114 case ACPI_MADT_LAPIC_NMI: 115 if (length != sizeof(entry->madt_lapic_nmi)) 116 return (0); 117 break; 118 case ACPI_MADT_LAPIC_OVERRIDE: 119 if (length != sizeof(entry->madt_lapic_override)) 120 return (0); 121 break; 122 case ACPI_MADT_IO_SAPIC: 123 if (length != sizeof(entry->madt_io_sapic)) 124 return (0); 125 break; 126 case ACPI_MADT_LOCAL_SAPIC: 127 if (length != sizeof(entry->madt_local_sapic)) 128 return (0); 129 break; 130 case ACPI_MADT_PLATFORM_INT: 131 if (length != sizeof(entry->madt_platform_int)) 132 return (0); 133 break; 134 } 135 136 addr += length; 137 } 138 139 return (1); 140 } 141 142 struct mp_bus acpimadt_busses[256]; 143 struct mp_bus acpimadt_isa_bus; 144 145 void 146 acpimadt_cfg_intr(int flags, u_int32_t *redir) 147 { 148 int mpspo = (flags >> MPS_INTPO_SHIFT) & MPS_INTPO_MASK; 149 int mpstrig = (flags >> MPS_INTTR_SHIFT) & MPS_INTTR_MASK; 150 151 *redir &= ~IOAPIC_REDLO_DEL_MASK; 152 switch (mpspo) { 153 case MPS_INTPO_DEF: 154 case MPS_INTPO_ACTHI: 155 *redir &= ~IOAPIC_REDLO_ACTLO; 156 break; 157 case MPS_INTPO_ACTLO: 158 *redir |= IOAPIC_REDLO_ACTLO; 159 break; 160 default: 161 panic("unknown MPS interrupt polarity %d", mpspo); 162 } 163 164 *redir |= (IOAPIC_REDLO_DEL_LOPRI << IOAPIC_REDLO_DEL_SHIFT); 165 166 switch (mpstrig) { 167 case MPS_INTTR_LEVEL: 168 *redir |= IOAPIC_REDLO_LEVEL; 169 break; 170 case MPS_INTTR_DEF: 171 case MPS_INTTR_EDGE: 172 *redir &= ~IOAPIC_REDLO_LEVEL; 173 break; 174 default: 175 panic("unknown MPS interrupt trigger %d", mpstrig); 176 } 177 } 178 179 static u_int8_t lapic_map[256]; 180 181 void 182 acpimadt_attach(struct device *parent, struct device *self, void *aux) 183 { 184 struct acpi_softc *acpi_sc = (struct acpi_softc *)parent; 185 struct device *mainbus = parent->dv_parent->dv_parent; 186 struct acpi_attach_args *aaa = aux; 187 struct acpi_madt *madt = (struct acpi_madt *)aaa->aaa_table; 188 caddr_t addr = (caddr_t)(madt + 1); 189 struct aml_value arg; 190 struct mp_intr_map *map; 191 struct ioapic_softc *apic; 192 int nlapic_nmis = 0; 193 int pin; 194 195 /* Do some sanity checks before committing to run in APIC mode. */ 196 if (!acpimadt_validate(madt)) { 197 printf(": invalid, skipping\n"); 198 return; 199 } 200 201 printf(" addr 0x%x", madt->local_apic_address); 202 if (madt->flags & ACPI_APIC_PCAT_COMPAT) 203 printf(": PC-AT compat"); 204 printf("\n"); 205 206 /* Tell the BIOS we will be using APIC mode. */ 207 memset(&arg, 0, sizeof(arg)); 208 arg.type = AML_OBJTYPE_INTEGER; 209 arg.v_integer = 1; 210 211 if (aml_evalname(acpi_sc, NULL, "\\_PIC", 1, &arg, NULL) != 0) 212 return; 213 214 mp_busses = acpimadt_busses; 215 mp_isa_bus = &acpimadt_isa_bus; 216 217 lapic_boot_init(madt->local_apic_address); 218 219 /* 1st pass, get CPUs and IOAPICs */ 220 while (addr < (caddr_t)madt + madt->hdr.length) { 221 union acpi_madt_entry *entry = (union acpi_madt_entry *)addr; 222 struct cpu_attach_args caa; 223 struct apic_attach_args aaa; 224 225 switch (entry->madt_lapic.apic_type) { 226 case ACPI_MADT_LAPIC: 227 dprintf("%s: LAPIC: acpi_proc_id %x, apic_id %x, flags 0x%x\n", 228 self->dv_xname, entry->madt_lapic.acpi_proc_id, 229 entry->madt_lapic.apic_id, 230 entry->madt_lapic.flags); 231 232 lapic_map[entry->madt_lapic.acpi_proc_id] = 233 entry->madt_lapic.apic_id; 234 acpi_lapic_flags[entry->madt_lapic.acpi_proc_id] = 235 entry->madt_lapic.flags; 236 237 if ((entry->madt_lapic.flags & ACPI_PROC_ENABLE) == 0) 238 break; 239 240 memset(&caa, 0, sizeof(struct cpu_attach_args)); 241 if (lapic_cpu_number() == entry->madt_lapic.apic_id) 242 caa.cpu_role = CPU_ROLE_BP; 243 else 244 caa.cpu_role = CPU_ROLE_AP; 245 caa.caa_name = "cpu"; 246 caa.cpu_number = entry->madt_lapic.apic_id; 247 #ifdef MULTIPROCESSOR 248 caa.cpu_func = &mp_cpu_funcs; 249 #endif 250 #ifdef __i386__ 251 /* 252 * XXX utterly wrong. These are the 253 * cpu_feature/cpu_id from the BSP cpu, now 254 * being given to another cpu. This is 255 * bullshit. 256 */ 257 extern int cpu_id, cpu_feature; 258 caa.cpu_signature = cpu_id; 259 caa.feature_flags = cpu_feature; 260 #endif 261 262 config_found(mainbus, &caa, acpimadt_print); 263 break; 264 case ACPI_MADT_IOAPIC: 265 dprintf("%s: IOAPIC: acpi_ioapic_id %x, address 0x%x, global_int_base 0x%x\n", 266 self->dv_xname, entry->madt_ioapic.acpi_ioapic_id, 267 entry->madt_ioapic.address, 268 entry->madt_ioapic.global_int_base); 269 270 memset(&aaa, 0, sizeof(struct apic_attach_args)); 271 aaa.aaa_name = "ioapic"; 272 aaa.apic_id = entry->madt_ioapic.acpi_ioapic_id; 273 aaa.apic_address = entry->madt_ioapic.address; 274 aaa.apic_vecbase = entry->madt_ioapic.global_int_base; 275 276 config_found(mainbus, &aaa, acpimadt_print); 277 break; 278 case ACPI_MADT_LAPIC_NMI: 279 nlapic_nmis++; 280 break; 281 } 282 addr += entry->madt_lapic.length; 283 } 284 285 mp_intrs = malloc(nlapic_nmis * sizeof (struct mp_intr_map), M_DEVBUF, M_NOWAIT); 286 if (mp_intrs == NULL) 287 return; 288 289 /* 2nd pass, get interrupt overrides */ 290 addr = (caddr_t)(madt + 1); 291 while (addr < (caddr_t)madt + madt->hdr.length) { 292 union acpi_madt_entry *entry = (union acpi_madt_entry *)addr; 293 294 switch (entry->madt_lapic.apic_type) { 295 case ACPI_MADT_LAPIC: 296 case ACPI_MADT_IOAPIC: 297 break; 298 299 case ACPI_MADT_OVERRIDE: 300 dprintf("%s: OVERRIDE: bus %x, source %x, global_int %x, flags %x\n", 301 self->dv_xname, entry->madt_override.bus, 302 entry->madt_override.source, 303 entry->madt_override.global_int, 304 entry->madt_override.flags); 305 306 pin = entry->madt_override.global_int; 307 apic = ioapic_find_bybase(pin); 308 309 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT | M_ZERO); 310 if (map == NULL) 311 return; 312 313 map->ioapic = apic; 314 map->ioapic_pin = pin - apic->sc_apic_vecbase; 315 map->bus_pin = entry->madt_override.source; 316 map->flags = entry->madt_override.flags; 317 #ifdef __amd64__ /* XXX */ 318 map->global_int = entry->madt_override.global_int; 319 #endif 320 acpimadt_cfg_intr(entry->madt_override.flags, &map->redir); 321 322 map->ioapic_ih = APIC_INT_VIA_APIC | 323 ((apic->sc_apicid << APIC_INT_APIC_SHIFT) | 324 (pin << APIC_INT_PIN_SHIFT)); 325 326 apic->sc_pins[pin].ip_map = map; 327 328 map->next = mp_isa_bus->mb_intrs; 329 mp_isa_bus->mb_intrs = map; 330 break; 331 332 case ACPI_MADT_LAPIC_NMI: 333 dprintf("%s: LAPIC_NMI: acpi_proc_id %x, local_apic_lint %x, flags %x\n", 334 self->dv_xname, entry->madt_lapic_nmi.acpi_proc_id, 335 entry->madt_lapic_nmi.local_apic_lint, 336 entry->madt_lapic_nmi.flags); 337 338 pin = entry->madt_lapic_nmi.local_apic_lint; 339 340 map = &mp_intrs[mp_nintrs++]; 341 memset(map, 0, sizeof *map); 342 map->cpu_id = lapic_map[entry->madt_lapic_nmi.acpi_proc_id]; 343 map->ioapic_pin = pin; 344 map->flags = entry->madt_lapic_nmi.flags; 345 346 acpimadt_cfg_intr(entry->madt_lapic_nmi.flags, &map->redir); 347 map->redir &= ~IOAPIC_REDLO_DEL_MASK; 348 map->redir |= (IOAPIC_REDLO_DEL_NMI << IOAPIC_REDLO_DEL_SHIFT); 349 break; 350 351 default: 352 printf("%s: unknown apic structure type %x\n", 353 self->dv_xname, entry->madt_lapic.apic_type); 354 } 355 356 addr += entry->madt_lapic.length; 357 } 358 359 /* 360 * ISA interrupts are supposed to be identity mapped unless 361 * there is an override, in which case we will already have a 362 * mapping for the interrupt. 363 */ 364 for (pin = 0; pin < ICU_LEN; pin++) { 365 /* Skip if we already have a mapping for this interrupt. */ 366 for (map = mp_isa_bus->mb_intrs; map != NULL; map = map->next) 367 if (map->bus_pin == pin) 368 break; 369 if (map != NULL) 370 continue; 371 372 apic = ioapic_find_bybase(pin); 373 374 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT | M_ZERO); 375 if (map == NULL) 376 return; 377 378 map->ioapic = apic; 379 map->ioapic_pin = pin; 380 map->bus_pin = pin; 381 #ifdef __amd64__ /* XXX */ 382 map->global_int = -1; 383 #endif 384 map->redir = (IOAPIC_REDLO_DEL_LOPRI << IOAPIC_REDLO_DEL_SHIFT); 385 386 map->ioapic_ih = APIC_INT_VIA_APIC | 387 ((apic->sc_apicid << APIC_INT_APIC_SHIFT) | 388 (pin << APIC_INT_PIN_SHIFT)); 389 390 apic->sc_pins[pin].ip_map = map; 391 392 map->next = mp_isa_bus->mb_intrs; 393 mp_isa_bus->mb_intrs = map; 394 } 395 } 396 397 int 398 acpimadt_print(void *aux, const char *pnp) 399 { 400 struct apic_attach_args *aaa = aux; 401 402 if (pnp) 403 printf("%s at %s:", aaa->aaa_name, pnp); 404 405 return (UNCONF); 406 } 407