madt.c revision 306628
1121992Sjhb/*- 2121992Sjhb * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org> 3121992Sjhb * All rights reserved. 4121992Sjhb * 5121992Sjhb * Redistribution and use in source and binary forms, with or without 6121992Sjhb * modification, are permitted provided that the following conditions 7121992Sjhb * are met: 8121992Sjhb * 1. Redistributions of source code must retain the above copyright 9121992Sjhb * notice, this list of conditions and the following disclaimer. 10121992Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11121992Sjhb * notice, this list of conditions and the following disclaimer in the 12121992Sjhb * documentation and/or other materials provided with the distribution. 13121992Sjhb * 14121992Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15121992Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16121992Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17121992Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18121992Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19121992Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20121992Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21121992Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22121992Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23121992Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24121992Sjhb * SUCH DAMAGE. 25121992Sjhb */ 26121992Sjhb 27121992Sjhb#include <sys/cdefs.h> 28121992Sjhb__FBSDID("$FreeBSD: stable/11/sys/x86/acpica/madt.c 306628 2016-10-03 09:39:46Z kib $"); 29121992Sjhb 30121992Sjhb#include <sys/param.h> 31121992Sjhb#include <sys/systm.h> 32121992Sjhb#include <sys/bus.h> 33121992Sjhb#include <sys/kernel.h> 34278749Skib#include <sys/limits.h> 35121992Sjhb#include <sys/malloc.h> 36121992Sjhb#include <sys/smp.h> 37121992Sjhb#include <vm/vm.h> 38121992Sjhb#include <vm/pmap.h> 39121992Sjhb 40214631Sjhb#include <x86/apicreg.h> 41121992Sjhb#include <machine/intr_machdep.h> 42261087Sjhb#include <x86/apicvar.h> 43278473Skib#include <machine/md_var.h> 44278749Skib#include <x86/vmware.h> 45121992Sjhb 46193530Sjkim#include <contrib/dev/acpica/include/acpi.h> 47193530Sjkim#include <contrib/dev/acpica/include/actables.h> 48193530Sjkim 49121992Sjhb#include <dev/acpica/acpivar.h> 50121992Sjhb#include <dev/pci/pcivar.h> 51121992Sjhb 52121992Sjhb/* These two arrays are indexed by APIC IDs. */ 53233623Sjhbstatic struct { 54121992Sjhb void *io_apic; 55121992Sjhb UINT32 io_vector; 56233623Sjhb} *ioapics; 57121992Sjhb 58233623Sjhbstatic struct lapic_info { 59284175Sjhb u_int la_enabled; 60284175Sjhb u_int la_acpi_id; 61233623Sjhb} lapics[MAX_APIC_ID + 1]; 62121992Sjhb 63269512Sroygerint madt_found_sci_override; 64167814Sjkimstatic ACPI_TABLE_MADT *madt; 65121992Sjhbstatic vm_paddr_t madt_physaddr; 66121992Sjhbstatic vm_offset_t madt_length; 67121992Sjhb 68227293Sedstatic MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items"); 69121992Sjhb 70167814Sjkimstatic enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source); 71167814Sjkimstatic enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source); 72121992Sjhbstatic int madt_find_cpu(u_int acpi_id, u_int *apic_id); 73121992Sjhbstatic int madt_find_interrupt(int intr, void **apic, u_int *pin); 74167814Sjkimstatic void madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg); 75167814Sjkimstatic void madt_parse_interrupt_override( 76167814Sjkim ACPI_MADT_INTERRUPT_OVERRIDE *intr); 77167814Sjkimstatic void madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, 78167814Sjkim void *arg __unused); 79167814Sjkimstatic void madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi); 80167814Sjkimstatic void madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi); 81121992Sjhbstatic int madt_probe(void); 82121992Sjhbstatic int madt_probe_cpus(void); 83167814Sjkimstatic void madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, 84167814Sjkim void *arg __unused); 85121992Sjhbstatic void madt_register(void *dummy); 86121992Sjhbstatic int madt_setup_local(void); 87121992Sjhbstatic int madt_setup_io(void); 88197439Sjhbstatic void madt_walk_table(acpi_subtable_handler *handler, void *arg); 89121992Sjhb 90121992Sjhbstatic struct apic_enumerator madt_enumerator = { 91121992Sjhb "MADT", 92121992Sjhb madt_probe, 93121992Sjhb madt_probe_cpus, 94121992Sjhb madt_setup_local, 95121992Sjhb madt_setup_io 96121992Sjhb}; 97121992Sjhb 98121992Sjhb/* 99121992Sjhb * Look for an ACPI Multiple APIC Description Table ("APIC") 100121992Sjhb */ 101121992Sjhbstatic int 102121992Sjhbmadt_probe(void) 103121992Sjhb{ 104121992Sjhb 105197439Sjhb madt_physaddr = acpi_find_table(ACPI_SIG_MADT); 106197439Sjhb if (madt_physaddr == 0) 107121992Sjhb return (ENXIO); 108269511Sroyger return (-50); 109121992Sjhb} 110121992Sjhb 111121992Sjhb/* 112121992Sjhb * Run through the MP table enumerating CPUs. 113121992Sjhb */ 114121992Sjhbstatic int 115121992Sjhbmadt_probe_cpus(void) 116121992Sjhb{ 117121992Sjhb 118197439Sjhb madt = acpi_map_table(madt_physaddr, ACPI_SIG_MADT); 119197439Sjhb madt_length = madt->Header.Length; 120121992Sjhb KASSERT(madt != NULL, ("Unable to re-map MADT")); 121121992Sjhb madt_walk_table(madt_probe_cpus_handler, NULL); 122197439Sjhb acpi_unmap_table(madt); 123121992Sjhb madt = NULL; 124121992Sjhb return (0); 125121992Sjhb} 126121992Sjhb 127121992Sjhb/* 128121992Sjhb * Initialize the local APIC on the BSP. 129121992Sjhb */ 130121992Sjhbstatic int 131121992Sjhbmadt_setup_local(void) 132121992Sjhb{ 133278473Skib ACPI_TABLE_DMAR *dmartbl; 134278473Skib vm_paddr_t dmartbl_physaddr; 135286994Skib const char *reason; 136286994Skib char *hw_vendor; 137278749Skib u_int p[4]; 138306628Skib int user_x2apic; 139306628Skib bool bios_x2apic; 140121992Sjhb 141161223Sjhb madt = pmap_mapbios(madt_physaddr, madt_length); 142278473Skib if ((cpu_feature2 & CPUID2_X2APIC) != 0) { 143286994Skib reason = NULL; 144286994Skib 145286994Skib /* 146286994Skib * Automatically detect several configurations where 147286994Skib * x2APIC mode is known to cause troubles. User can 148286994Skib * override the setting with hw.x2apic_enable tunable. 149286994Skib */ 150278473Skib dmartbl_physaddr = acpi_find_table(ACPI_SIG_DMAR); 151278473Skib if (dmartbl_physaddr != 0) { 152278473Skib dmartbl = acpi_map_table(dmartbl_physaddr, 153278473Skib ACPI_SIG_DMAR); 154306628Skib if ((dmartbl->Flags & ACPI_DMAR_X2APIC_OPT_OUT) != 0) 155286994Skib reason = "by DMAR table"; 156278473Skib acpi_unmap_table(dmartbl); 157278473Skib } 158278749Skib if (vm_guest == VM_GUEST_VMWARE) { 159278749Skib vmware_hvcall(VMW_HVCMD_GETVCPU_INFO, p); 160278749Skib if ((p[0] & VMW_VCPUINFO_VCPU_RESERVED) != 0 || 161306628Skib (p[0] & VMW_VCPUINFO_LEGACY_X2APIC) == 0) 162306628Skib reason = 163306628Skib "inside VMWare without intr redirection"; 164279286Skib } else if (vm_guest == VM_GUEST_XEN) { 165286994Skib reason = "due to running under XEN"; 166291686Skib } else if (vm_guest == VM_GUEST_NO && 167291686Skib CPUID_TO_FAMILY(cpu_id) == 0x6 && 168291686Skib CPUID_TO_MODEL(cpu_id) == 0x2a) { 169286994Skib hw_vendor = kern_getenv("smbios.planar.maker"); 170286994Skib /* 171291686Skib * It seems that some Lenovo and ASUS 172291686Skib * SandyBridge-based notebook BIOSes have a 173291686Skib * bug which prevents booting AP in x2APIC 174291686Skib * mode. Since the only way to detect mobile 175291686Skib * CPU is to check northbridge pci id, which 176291686Skib * cannot be done that early, disable x2APIC 177291686Skib * for all Lenovo and ASUS SandyBridge 178291686Skib * machines. 179286994Skib */ 180291686Skib if (hw_vendor != NULL) { 181291686Skib if (!strcmp(hw_vendor, "LENOVO") || 182291686Skib !strcmp(hw_vendor, 183291686Skib "ASUSTeK Computer Inc.")) { 184291686Skib reason = 185291686Skib "for a suspected SandyBridge BIOS bug"; 186291686Skib } 187291686Skib freeenv(hw_vendor); 188286994Skib } 189278749Skib } 190306628Skib bios_x2apic = lapic_is_x2apic(); 191306628Skib if (reason != NULL && bios_x2apic) { 192306628Skib if (bootverbose) 193306628Skib printf("x2APIC should be disabled %s but " 194306628Skib "already enabled by BIOS; enabling.\n", 195306628Skib reason); 196306628Skib reason = NULL; 197306628Skib } 198306628Skib if (reason == NULL) 199306628Skib x2apic_mode = 1; 200306628Skib else if (bootverbose) 201286994Skib printf("x2APIC available but disabled %s\n", reason); 202306628Skib user_x2apic = x2apic_mode; 203306628Skib TUNABLE_INT_FETCH("hw.x2apic_enable", &user_x2apic); 204306628Skib if (user_x2apic != x2apic_mode) { 205306628Skib if (bios_x2apic && !user_x2apic) 206306628Skib printf("x2APIC disabled by tunable and " 207306628Skib "enabled by BIOS; ignoring tunable."); 208306628Skib else 209306628Skib x2apic_mode = user_x2apic; 210306628Skib } 211278473Skib } 212278473Skib 213167814Sjkim lapic_init(madt->Address); 214121992Sjhb printf("ACPI APIC Table: <%.*s %.*s>\n", 215167814Sjkim (int)sizeof(madt->Header.OemId), madt->Header.OemId, 216167814Sjkim (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId); 217121992Sjhb 218121992Sjhb /* 219121992Sjhb * We ignore 64-bit local APIC override entries. Should we 220121992Sjhb * perhaps emit a warning here if we find one? 221121992Sjhb */ 222121992Sjhb return (0); 223121992Sjhb} 224121992Sjhb 225121992Sjhb/* 226125048Sjhb * Enumerate I/O APICs and setup interrupt sources. 227121992Sjhb */ 228121992Sjhbstatic int 229121992Sjhbmadt_setup_io(void) 230121992Sjhb{ 231128930Sjhb void *ioapic; 232128930Sjhb u_int pin; 233121992Sjhb int i; 234121992Sjhb 235125048Sjhb /* Try to initialize ACPI so that we can access the FADT. */ 236125048Sjhb i = acpi_Startup(); 237125048Sjhb if (ACPI_FAILURE(i)) { 238125048Sjhb printf("MADT: ACPI Startup failed with %s\n", 239125048Sjhb AcpiFormatException(i)); 240125048Sjhb printf("Try disabling either ACPI or apic support.\n"); 241125048Sjhb panic("Using MADT but ACPI doesn't work"); 242125048Sjhb } 243233623Sjhb 244233623Sjhb ioapics = malloc(sizeof(*ioapics) * (MAX_APIC_ID + 1), M_MADT, 245233623Sjhb M_WAITOK | M_ZERO); 246233623Sjhb 247121992Sjhb /* First, we run through adding I/O APIC's. */ 248121992Sjhb madt_walk_table(madt_parse_apics, NULL); 249121992Sjhb 250121992Sjhb /* Second, we run through the table tweaking interrupt sources. */ 251121992Sjhb madt_walk_table(madt_parse_ints, NULL); 252121992Sjhb 253128930Sjhb /* 254128930Sjhb * If there was not an explicit override entry for the SCI, 255128930Sjhb * force it to use level trigger and active-low polarity. 256128930Sjhb */ 257128930Sjhb if (!madt_found_sci_override) { 258167814Sjkim if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic, 259167814Sjkim &pin) != 0) 260167814Sjkim printf("MADT: Could not find APIC for SCI IRQ %u\n", 261167814Sjkim AcpiGbl_FADT.SciInterrupt); 262128930Sjhb else { 263128930Sjhb printf( 264128930Sjhb "MADT: Forcing active-low polarity and level trigger for SCI\n"); 265128930Sjhb ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW); 266128930Sjhb ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL); 267128930Sjhb } 268128930Sjhb } 269128930Sjhb 270121992Sjhb /* Third, we register all the I/O APIC's. */ 271169395Sjhb for (i = 0; i <= MAX_APIC_ID; i++) 272121992Sjhb if (ioapics[i].io_apic != NULL) 273121992Sjhb ioapic_register(ioapics[i].io_apic); 274121992Sjhb 275121992Sjhb /* Finally, we throw the switch to enable the I/O APIC's. */ 276121992Sjhb acpi_SetDefaultIntrModel(ACPI_INTR_APIC); 277121992Sjhb 278233623Sjhb free(ioapics, M_MADT); 279233623Sjhb ioapics = NULL; 280233623Sjhb 281121992Sjhb return (0); 282121992Sjhb} 283121992Sjhb 284121992Sjhbstatic void 285121992Sjhbmadt_register(void *dummy __unused) 286121992Sjhb{ 287121992Sjhb 288121992Sjhb apic_register_enumerator(&madt_enumerator); 289121992Sjhb} 290215009SjhbSYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL); 291121992Sjhb 292121992Sjhb/* 293121992Sjhb * Call the handler routine for each entry in the MADT table. 294121992Sjhb */ 295121992Sjhbstatic void 296197439Sjhbmadt_walk_table(acpi_subtable_handler *handler, void *arg) 297121992Sjhb{ 298121992Sjhb 299197439Sjhb acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 300197439Sjhb handler, arg); 301121992Sjhb} 302121992Sjhb 303121992Sjhbstatic void 304284175Sjhbmadt_add_cpu(u_int acpi_id, u_int apic_id, u_int flags) 305284175Sjhb{ 306284175Sjhb struct lapic_info *la; 307284175Sjhb 308284175Sjhb /* 309284175Sjhb * The MADT does not include a BSP flag, so we have to let the 310284175Sjhb * MP code figure out which CPU is the BSP on its own. 311284175Sjhb */ 312284175Sjhb if (bootverbose) 313284175Sjhb printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n", 314284175Sjhb apic_id, acpi_id, flags & ACPI_MADT_ENABLED ? 315284175Sjhb "enabled" : "disabled"); 316284175Sjhb if (!(flags & ACPI_MADT_ENABLED)) 317284175Sjhb return; 318284175Sjhb if (apic_id > MAX_APIC_ID) { 319284175Sjhb printf("MADT: Ignoring local APIC ID %u (too high)\n", 320284175Sjhb apic_id); 321284175Sjhb return; 322284175Sjhb } 323284175Sjhb 324284175Sjhb la = &lapics[apic_id]; 325284175Sjhb KASSERT(la->la_enabled == 0, ("Duplicate local APIC ID %u", apic_id)); 326284175Sjhb la->la_enabled = 1; 327284175Sjhb la->la_acpi_id = acpi_id; 328284175Sjhb lapic_create(apic_id, 0); 329284175Sjhb} 330284175Sjhb 331284175Sjhbstatic void 332167814Sjkimmadt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg) 333121992Sjhb{ 334167814Sjkim ACPI_MADT_LOCAL_APIC *proc; 335284175Sjhb ACPI_MADT_LOCAL_X2APIC *x2apic; 336121992Sjhb 337121992Sjhb switch (entry->Type) { 338167814Sjkim case ACPI_MADT_TYPE_LOCAL_APIC: 339167814Sjkim proc = (ACPI_MADT_LOCAL_APIC *)entry; 340284175Sjhb madt_add_cpu(proc->ProcessorId, proc->Id, proc->LapicFlags); 341121992Sjhb break; 342284175Sjhb case ACPI_MADT_TYPE_LOCAL_X2APIC: 343284175Sjhb x2apic = (ACPI_MADT_LOCAL_X2APIC *)entry; 344284175Sjhb madt_add_cpu(x2apic->Uid, x2apic->LocalApicId, 345284175Sjhb x2apic->LapicFlags); 346284175Sjhb break; 347121992Sjhb } 348121992Sjhb} 349121992Sjhb 350121992Sjhb 351121992Sjhb/* 352121992Sjhb * Add an I/O APIC from an entry in the table. 353121992Sjhb */ 354121992Sjhbstatic void 355167814Sjkimmadt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused) 356121992Sjhb{ 357167814Sjkim ACPI_MADT_IO_APIC *apic; 358121992Sjhb 359121992Sjhb switch (entry->Type) { 360167814Sjkim case ACPI_MADT_TYPE_IO_APIC: 361167814Sjkim apic = (ACPI_MADT_IO_APIC *)entry; 362121992Sjhb if (bootverbose) 363167814Sjkim printf( 364167814Sjkim "MADT: Found IO APIC ID %u, Interrupt %u at %p\n", 365167814Sjkim apic->Id, apic->GlobalIrqBase, 366123326Snjl (void *)(uintptr_t)apic->Address); 367169395Sjhb if (apic->Id > MAX_APIC_ID) 368167814Sjkim panic("%s: I/O APIC ID %u too high", __func__, 369167814Sjkim apic->Id); 370167814Sjkim if (ioapics[apic->Id].io_apic != NULL) 371167814Sjkim panic("%s: Double APIC ID %u", __func__, apic->Id); 372189404Sjhb if (apic->GlobalIrqBase >= FIRST_MSI_INT) { 373189404Sjhb printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id); 374189404Sjhb break; 375189404Sjhb } 376167814Sjkim ioapics[apic->Id].io_apic = ioapic_create(apic->Address, 377167814Sjkim apic->Id, apic->GlobalIrqBase); 378167814Sjkim ioapics[apic->Id].io_vector = apic->GlobalIrqBase; 379121992Sjhb break; 380121992Sjhb default: 381121992Sjhb break; 382121992Sjhb } 383121992Sjhb} 384121992Sjhb 385121992Sjhb/* 386129128Sjhb * Determine properties of an interrupt source. Note that for ACPI these 387129128Sjhb * functions are only used for ISA interrupts, so we assume ISA bus values 388128930Sjhb * (Active Hi, Edge Triggered) for conforming values except for the ACPI 389129128Sjhb * SCI for which we use Active Lo, Level Triggered. 390121992Sjhb */ 391128930Sjhbstatic enum intr_polarity 392167814Sjkiminterrupt_polarity(UINT16 IntiFlags, UINT8 Source) 393121992Sjhb{ 394121992Sjhb 395167814Sjkim switch (IntiFlags & ACPI_MADT_POLARITY_MASK) { 396263859Stakawata default: 397269184Sakiyama printf("WARNING: Bogus Interrupt Polarity. Assume CONFORMS\n"); 398263859Stakawata /* FALLTHROUGH*/ 399167814Sjkim case ACPI_MADT_POLARITY_CONFORMS: 400167814Sjkim if (Source == AcpiGbl_FADT.SciInterrupt) 401128930Sjhb return (INTR_POLARITY_LOW); 402128930Sjhb else 403128930Sjhb return (INTR_POLARITY_HIGH); 404167814Sjkim case ACPI_MADT_POLARITY_ACTIVE_HIGH: 405128930Sjhb return (INTR_POLARITY_HIGH); 406167814Sjkim case ACPI_MADT_POLARITY_ACTIVE_LOW: 407263859Stakawata return (INTR_POLARITY_LOW); 408121992Sjhb } 409121992Sjhb} 410121992Sjhb 411128930Sjhbstatic enum intr_trigger 412167814Sjkiminterrupt_trigger(UINT16 IntiFlags, UINT8 Source) 413121992Sjhb{ 414121992Sjhb 415167814Sjkim switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) { 416263859Stakawata default: 417269184Sakiyama printf("WARNING: Bogus Interrupt Trigger Mode. Assume CONFORMS.\n"); 418263859Stakawata /*FALLTHROUGH*/ 419167814Sjkim case ACPI_MADT_TRIGGER_CONFORMS: 420167814Sjkim if (Source == AcpiGbl_FADT.SciInterrupt) 421128930Sjhb return (INTR_TRIGGER_LEVEL); 422128930Sjhb else 423128930Sjhb return (INTR_TRIGGER_EDGE); 424167814Sjkim case ACPI_MADT_TRIGGER_EDGE: 425128930Sjhb return (INTR_TRIGGER_EDGE); 426167814Sjkim case ACPI_MADT_TRIGGER_LEVEL: 427263859Stakawata return (INTR_TRIGGER_LEVEL); 428121992Sjhb } 429121992Sjhb} 430121992Sjhb 431121992Sjhb/* 432121992Sjhb * Find the local APIC ID associated with a given ACPI Processor ID. 433121992Sjhb */ 434121992Sjhbstatic int 435121992Sjhbmadt_find_cpu(u_int acpi_id, u_int *apic_id) 436121992Sjhb{ 437129960Sjhb int i; 438121992Sjhb 439169395Sjhb for (i = 0; i <= MAX_APIC_ID; i++) { 440130310Sjhb if (!lapics[i].la_enabled) 441129960Sjhb continue; 442129960Sjhb if (lapics[i].la_acpi_id != acpi_id) 443129960Sjhb continue; 444129960Sjhb *apic_id = i; 445130310Sjhb return (0); 446129960Sjhb } 447129960Sjhb return (ENOENT); 448121992Sjhb} 449121992Sjhb 450121992Sjhb/* 451121992Sjhb * Find the IO APIC and pin on that APIC associated with a given global 452121992Sjhb * interrupt. 453121992Sjhb */ 454121992Sjhbstatic int 455121992Sjhbmadt_find_interrupt(int intr, void **apic, u_int *pin) 456121992Sjhb{ 457121992Sjhb int i, best; 458121992Sjhb 459121992Sjhb best = -1; 460169395Sjhb for (i = 0; i <= MAX_APIC_ID; i++) { 461121992Sjhb if (ioapics[i].io_apic == NULL || 462121992Sjhb ioapics[i].io_vector > intr) 463121992Sjhb continue; 464121992Sjhb if (best == -1 || 465121992Sjhb ioapics[best].io_vector < ioapics[i].io_vector) 466121992Sjhb best = i; 467121992Sjhb } 468121992Sjhb if (best == -1) 469121992Sjhb return (ENOENT); 470121992Sjhb *apic = ioapics[best].io_apic; 471121992Sjhb *pin = intr - ioapics[best].io_vector; 472121992Sjhb if (*pin > 32) 473121992Sjhb printf("WARNING: Found intpin of %u for vector %d\n", *pin, 474121992Sjhb intr); 475121992Sjhb return (0); 476121992Sjhb} 477121992Sjhb 478269512Sroygervoid 479269512Sroygermadt_parse_interrupt_values(void *entry, 480269512Sroyger enum intr_trigger *trig, enum intr_polarity *pol) 481121992Sjhb{ 482269512Sroyger ACPI_MADT_INTERRUPT_OVERRIDE *intr; 483128930Sjhb char buf[64]; 484121992Sjhb 485269512Sroyger intr = entry; 486269512Sroyger 487121992Sjhb if (bootverbose) 488142257Sjhb printf("MADT: Interrupt override: source %u, irq %u\n", 489167814Sjkim intr->SourceIrq, intr->GlobalIrq); 490121992Sjhb KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero")); 491121992Sjhb 492125048Sjhb /* 493128930Sjhb * Lookup the appropriate trigger and polarity modes for this 494128930Sjhb * entry. 495128930Sjhb */ 496269512Sroyger *trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq); 497269512Sroyger *pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq); 498269512Sroyger 499128930Sjhb /* 500125048Sjhb * If the SCI is identity mapped but has edge trigger and 501128329Sjhb * active-hi polarity or the force_sci_lo tunable is set, 502128329Sjhb * force it to use level/lo. 503125048Sjhb */ 504167814Sjkim if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) { 505128930Sjhb madt_found_sci_override = 1; 506128930Sjhb if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) { 507128930Sjhb if (tolower(buf[0]) == 'e') 508269512Sroyger *trig = INTR_TRIGGER_EDGE; 509128930Sjhb else if (tolower(buf[0]) == 'l') 510269512Sroyger *trig = INTR_TRIGGER_LEVEL; 511128930Sjhb else 512128930Sjhb panic( 513128930Sjhb "Invalid trigger %s: must be 'edge' or 'level'", 514128930Sjhb buf); 515128930Sjhb printf("MADT: Forcing SCI to %s trigger\n", 516269512Sroyger *trig == INTR_TRIGGER_EDGE ? "edge" : "level"); 517128930Sjhb } 518128930Sjhb if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) { 519128930Sjhb if (tolower(buf[0]) == 'h') 520269512Sroyger *pol = INTR_POLARITY_HIGH; 521128930Sjhb else if (tolower(buf[0]) == 'l') 522269512Sroyger *pol = INTR_POLARITY_LOW; 523128930Sjhb else 524128930Sjhb panic( 525128930Sjhb "Invalid polarity %s: must be 'high' or 'low'", 526128930Sjhb buf); 527128930Sjhb printf("MADT: Forcing SCI to active %s polarity\n", 528269512Sroyger *pol == INTR_POLARITY_HIGH ? "high" : "low"); 529128930Sjhb } 530128930Sjhb } 531269512Sroyger} 532125048Sjhb 533269512Sroyger/* 534269512Sroyger * Parse an interrupt source override for an ISA interrupt. 535269512Sroyger */ 536269512Sroygerstatic void 537269512Sroygermadt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr) 538269512Sroyger{ 539269512Sroyger void *new_ioapic, *old_ioapic; 540269512Sroyger u_int new_pin, old_pin; 541269512Sroyger enum intr_trigger trig; 542269512Sroyger enum intr_polarity pol; 543269512Sroyger 544269512Sroyger if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 && 545269512Sroyger intr->GlobalIrq == 2) { 546269512Sroyger if (bootverbose) 547269512Sroyger printf("MADT: Skipping timer override\n"); 548269512Sroyger return; 549269512Sroyger } 550269512Sroyger 551269512Sroyger if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) { 552269512Sroyger printf("MADT: Could not find APIC for vector %u (IRQ %u)\n", 553269512Sroyger intr->GlobalIrq, intr->SourceIrq); 554269512Sroyger return; 555269512Sroyger } 556269512Sroyger 557269512Sroyger madt_parse_interrupt_values(intr, &trig, &pol); 558269512Sroyger 559128930Sjhb /* Remap the IRQ if it is mapped to a different interrupt vector. */ 560167814Sjkim if (intr->SourceIrq != intr->GlobalIrq) { 561125048Sjhb /* 562125048Sjhb * If the SCI is remapped to a non-ISA global interrupt, 563125048Sjhb * then override the vector we use to setup and allocate 564125048Sjhb * the interrupt. 565125048Sjhb */ 566167814Sjkim if (intr->GlobalIrq > 15 && 567167814Sjkim intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) 568167814Sjkim acpi_OverrideInterruptLevel(intr->GlobalIrq); 569122502Sjhb else 570167814Sjkim ioapic_remap_vector(new_ioapic, new_pin, 571167814Sjkim intr->SourceIrq); 572167814Sjkim if (madt_find_interrupt(intr->SourceIrq, &old_ioapic, 573122149Sjhb &old_pin) != 0) 574167814Sjkim printf("MADT: Could not find APIC for source IRQ %u\n", 575167814Sjkim intr->SourceIrq); 576122172Sjhb else if (ioapic_get_vector(old_ioapic, old_pin) == 577167814Sjkim intr->SourceIrq) 578122149Sjhb ioapic_disable_pin(old_ioapic, old_pin); 579122149Sjhb } 580128930Sjhb 581128930Sjhb /* Program the polarity and trigger mode. */ 582128930Sjhb ioapic_set_triggermode(new_ioapic, new_pin, trig); 583128930Sjhb ioapic_set_polarity(new_ioapic, new_pin, pol); 584121992Sjhb} 585121992Sjhb 586121992Sjhb/* 587121992Sjhb * Parse an entry for an NMI routed to an IO APIC. 588121992Sjhb */ 589121992Sjhbstatic void 590167814Sjkimmadt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi) 591121992Sjhb{ 592121992Sjhb void *ioapic; 593121992Sjhb u_int pin; 594121992Sjhb 595167814Sjkim if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) { 596167814Sjkim printf("MADT: Could not find APIC for vector %u\n", 597167814Sjkim nmi->GlobalIrq); 598121992Sjhb return; 599121992Sjhb } 600121992Sjhb 601121992Sjhb ioapic_set_nmi(ioapic, pin); 602167814Sjkim if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS)) 603121992Sjhb ioapic_set_triggermode(ioapic, pin, 604167814Sjkim interrupt_trigger(nmi->IntiFlags, 0)); 605263794Stakawata if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS)) 606121992Sjhb ioapic_set_polarity(ioapic, pin, 607167814Sjkim interrupt_polarity(nmi->IntiFlags, 0)); 608121992Sjhb} 609121992Sjhb 610121992Sjhb/* 611121992Sjhb * Parse an entry for an NMI routed to a local APIC LVT pin. 612121992Sjhb */ 613121992Sjhbstatic void 614284175Sjhbmadt_handle_local_nmi(u_int acpi_id, UINT8 Lint, UINT16 IntiFlags) 615121992Sjhb{ 616121992Sjhb u_int apic_id, pin; 617121992Sjhb 618284175Sjhb if (acpi_id == 0xffffffff) 619121992Sjhb apic_id = APIC_ID_ALL; 620284175Sjhb else if (madt_find_cpu(acpi_id, &apic_id) != 0) { 621121992Sjhb if (bootverbose) 622167814Sjkim printf("MADT: Ignoring local NMI routed to " 623284175Sjhb "ACPI CPU %u\n", acpi_id); 624121992Sjhb return; 625121992Sjhb } 626284175Sjhb if (Lint == 0) 627259140Sjhb pin = APIC_LVT_LINT0; 628121992Sjhb else 629259140Sjhb pin = APIC_LVT_LINT1; 630121992Sjhb lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI); 631284175Sjhb if (!(IntiFlags & ACPI_MADT_TRIGGER_CONFORMS)) 632121992Sjhb lapic_set_lvt_triggermode(apic_id, pin, 633284175Sjhb interrupt_trigger(IntiFlags, 0)); 634284175Sjhb if (!(IntiFlags & ACPI_MADT_POLARITY_CONFORMS)) 635121992Sjhb lapic_set_lvt_polarity(apic_id, pin, 636284175Sjhb interrupt_polarity(IntiFlags, 0)); 637121992Sjhb} 638121992Sjhb 639284175Sjhbstatic void 640284175Sjhbmadt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi) 641284175Sjhb{ 642284175Sjhb 643284175Sjhb madt_handle_local_nmi(nmi->ProcessorId == 0xff ? 0xffffffff : 644284175Sjhb nmi->ProcessorId, nmi->Lint, nmi->IntiFlags); 645284175Sjhb} 646284175Sjhb 647284175Sjhbstatic void 648284175Sjhbmadt_parse_local_x2apic_nmi(ACPI_MADT_LOCAL_X2APIC_NMI *nmi) 649284175Sjhb{ 650284175Sjhb 651284175Sjhb madt_handle_local_nmi(nmi->Uid, nmi->Lint, nmi->IntiFlags); 652284175Sjhb} 653284175Sjhb 654121992Sjhb/* 655121992Sjhb * Parse interrupt entries. 656121992Sjhb */ 657121992Sjhbstatic void 658167814Sjkimmadt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused) 659121992Sjhb{ 660121992Sjhb 661121992Sjhb switch (entry->Type) { 662167814Sjkim case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE: 663121992Sjhb madt_parse_interrupt_override( 664167814Sjkim (ACPI_MADT_INTERRUPT_OVERRIDE *)entry); 665121992Sjhb break; 666167814Sjkim case ACPI_MADT_TYPE_NMI_SOURCE: 667167814Sjkim madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry); 668121992Sjhb break; 669167814Sjkim case ACPI_MADT_TYPE_LOCAL_APIC_NMI: 670167814Sjkim madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry); 671121992Sjhb break; 672284175Sjhb case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI: 673284175Sjhb madt_parse_local_x2apic_nmi( 674284175Sjhb (ACPI_MADT_LOCAL_X2APIC_NMI *)entry); 675284175Sjhb break; 676121992Sjhb } 677121992Sjhb} 678121992Sjhb 679121992Sjhb/* 680121992Sjhb * Setup per-CPU ACPI IDs. 681121992Sjhb */ 682121992Sjhbstatic void 683121992Sjhbmadt_set_ids(void *dummy) 684121992Sjhb{ 685129960Sjhb struct lapic_info *la; 686121992Sjhb struct pcpu *pc; 687129960Sjhb u_int i; 688121992Sjhb 689121992Sjhb if (madt == NULL) 690121992Sjhb return; 691209059Sjhb CPU_FOREACH(i) { 692121992Sjhb pc = pcpu_find(i); 693167814Sjkim KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 694129960Sjhb la = &lapics[pc->pc_apic_id]; 695130310Sjhb if (!la->la_enabled) 696129960Sjhb panic("APIC: CPU with APIC ID %u is not enabled", 697129960Sjhb pc->pc_apic_id); 698129960Sjhb pc->pc_acpi_id = la->la_acpi_id; 699129960Sjhb if (bootverbose) 700129960Sjhb printf("APIC: CPU %u has ACPI ID %u\n", i, 701129960Sjhb la->la_acpi_id); 702121992Sjhb } 703121992Sjhb} 704256073SgibbsSYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, madt_set_ids, NULL); 705