2 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
40 #include <x86/apicreg.h>
41 #include <machine/intr_machdep.h>
42 #include <x86/apicvar.h>
43 #include <machine/md_var.h>
44 #include <x86/vmware.h>
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/aclocal.h>
48 #include <contrib/dev/acpica/include/actables.h>
50 #include <dev/acpica/acpivar.h>
51 #include <dev/pci/pcivar.h>
53 /* These two arrays are indexed by APIC IDs. */
59 static struct lapic_info {
62 } lapics[MAX_APIC_ID + 1];
64 int madt_found_sci_override;
65 static ACPI_TABLE_MADT *madt;
66 static vm_paddr_t madt_physaddr;
67 static vm_offset_t madt_length;
69 static MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items");
71 static enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source);
72 static enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source);
73 static int madt_find_cpu(u_int acpi_id, u_int *apic_id);
74 static int madt_find_interrupt(int intr, void **apic, u_int *pin);
75 static void madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg);
76 static void madt_parse_interrupt_override(
77 ACPI_MADT_INTERRUPT_OVERRIDE *intr);
78 static void madt_parse_ints(ACPI_SUBTABLE_HEADER *entry,
80 static void madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi);
81 static void madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi);
82 static int madt_probe(void);
83 static int madt_probe_cpus(void);
84 static void madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry,
86 static void madt_register(void *dummy);
87 static int madt_setup_local(void);
88 static int madt_setup_io(void);
89 static void madt_walk_table(acpi_subtable_handler *handler, void *arg);
91 static struct apic_enumerator madt_enumerator = {
100 * Look for an ACPI Multiple APIC Description Table ("APIC")
106 madt_physaddr = acpi_find_table(ACPI_SIG_MADT);
107 if (madt_physaddr == 0)
113 * Run through the MP table enumerating CPUs.
116 madt_probe_cpus(void)
119 madt = acpi_map_table(madt_physaddr, ACPI_SIG_MADT);
120 madt_length = madt->Header.Length;
121 KASSERT(madt != NULL, ("Unable to re-map MADT"));
122 madt_walk_table(madt_probe_cpus_handler, NULL);
123 acpi_unmap_table(madt);
129 * Initialize the local APIC on the BSP.
132 madt_setup_local(void)
134 ACPI_TABLE_DMAR *dmartbl;
135 vm_paddr_t dmartbl_physaddr;
142 madt = pmap_mapbios(madt_physaddr, madt_length);
143 if ((cpu_feature2 & CPUID2_X2APIC) != 0) {
147 * Automatically detect several configurations where
148 * x2APIC mode is known to cause troubles. User can
149 * override the setting with hw.x2apic_enable tunable.
151 dmartbl_physaddr = acpi_find_table(ACPI_SIG_DMAR);
152 if (dmartbl_physaddr != 0) {
153 dmartbl = acpi_map_table(dmartbl_physaddr,
155 if ((dmartbl->Flags & ACPI_DMAR_X2APIC_OPT_OUT) != 0)
156 reason = "by DMAR table";
157 acpi_unmap_table(dmartbl);
159 if (vm_guest == VM_GUEST_VMWARE) {
160 vmware_hvcall(VMW_HVCMD_GETVCPU_INFO, p);
161 if ((p[0] & VMW_VCPUINFO_VCPU_RESERVED) != 0 ||
162 (p[0] & VMW_VCPUINFO_LEGACY_X2APIC) == 0)
164 "inside VMWare without intr redirection";
165 } else if (vm_guest == VM_GUEST_XEN) {
166 reason = "due to running under XEN";
167 } else if (vm_guest == VM_GUEST_NO &&
168 CPUID_TO_FAMILY(cpu_id) == 0x6 &&
169 CPUID_TO_MODEL(cpu_id) == 0x2a) {
170 hw_vendor = kern_getenv("smbios.planar.maker");
172 * It seems that some Lenovo and ASUS
173 * SandyBridge-based notebook BIOSes have a
174 * bug which prevents booting AP in x2APIC
175 * mode. Since the only way to detect mobile
176 * CPU is to check northbridge pci id, which
177 * cannot be done that early, disable x2APIC
178 * for all Lenovo and ASUS SandyBridge
181 if (hw_vendor != NULL) {
182 if (!strcmp(hw_vendor, "LENOVO") ||
184 "ASUSTeK Computer Inc.")) {
186 "for a suspected SandyBridge BIOS bug";
191 bios_x2apic = lapic_is_x2apic();
192 if (reason != NULL && bios_x2apic) {
194 printf("x2APIC should be disabled %s but "
195 "already enabled by BIOS; enabling.\n",
201 else if (bootverbose)
202 printf("x2APIC available but disabled %s\n", reason);
203 user_x2apic = x2apic_mode;
204 TUNABLE_INT_FETCH("hw.x2apic_enable", &user_x2apic);
205 if (user_x2apic != x2apic_mode) {
206 if (bios_x2apic && !user_x2apic)
207 printf("x2APIC disabled by tunable and "
208 "enabled by BIOS; ignoring tunable.");
210 x2apic_mode = user_x2apic;
214 lapic_init(madt->Address);
215 printf("ACPI APIC Table: <%.*s %.*s>\n",
216 (int)sizeof(madt->Header.OemId), madt->Header.OemId,
217 (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
220 * We ignore 64-bit local APIC override entries. Should we
221 * perhaps emit a warning here if we find one?
227 * Enumerate I/O APICs and setup interrupt sources.
236 /* Try to initialize ACPI so that we can access the FADT. */
238 if (ACPI_FAILURE(i)) {
239 printf("MADT: ACPI Startup failed with %s\n",
240 AcpiFormatException(i));
241 printf("Try disabling either ACPI or apic support.\n");
242 panic("Using MADT but ACPI doesn't work");
245 ioapics = malloc(sizeof(*ioapics) * (MAX_APIC_ID + 1), M_MADT,
248 /* First, we run through adding I/O APIC's. */
249 madt_walk_table(madt_parse_apics, NULL);
251 /* Second, we run through the table tweaking interrupt sources. */
252 madt_walk_table(madt_parse_ints, NULL);
255 * If there was not an explicit override entry for the SCI,
256 * force it to use level trigger and active-low polarity.
258 if (!madt_found_sci_override) {
259 if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
261 printf("MADT: Could not find APIC for SCI IRQ %u\n",
262 AcpiGbl_FADT.SciInterrupt);
265 "MADT: Forcing active-low polarity and level trigger for SCI\n");
266 ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
267 ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
271 /* Third, we register all the I/O APIC's. */
272 for (i = 0; i <= MAX_APIC_ID; i++)
273 if (ioapics[i].io_apic != NULL)
274 ioapic_register(ioapics[i].io_apic);
276 /* Finally, we throw the switch to enable the I/O APIC's. */
277 acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
279 free(ioapics, M_MADT);
286 madt_register(void *dummy __unused)
289 apic_register_enumerator(&madt_enumerator);
291 SYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL);
294 * Call the handler routine for each entry in the MADT table.
297 madt_walk_table(acpi_subtable_handler *handler, void *arg)
300 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
305 madt_add_cpu(u_int acpi_id, u_int apic_id, u_int flags)
307 struct lapic_info *la;
310 * The MADT does not include a BSP flag, so we have to let the
311 * MP code figure out which CPU is the BSP on its own.
314 printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
315 apic_id, acpi_id, flags & ACPI_MADT_ENABLED ?
316 "enabled" : "disabled");
317 if (!(flags & ACPI_MADT_ENABLED))
319 if (apic_id > MAX_APIC_ID) {
320 printf("MADT: Ignoring local APIC ID %u (too high)\n",
325 la = &lapics[apic_id];
326 KASSERT(la->la_enabled == 0, ("Duplicate local APIC ID %u", apic_id));
328 la->la_acpi_id = acpi_id;
329 lapic_create(apic_id, 0);
333 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
335 ACPI_MADT_LOCAL_APIC *proc;
336 ACPI_MADT_LOCAL_X2APIC *x2apic;
338 switch (entry->Type) {
339 case ACPI_MADT_TYPE_LOCAL_APIC:
340 proc = (ACPI_MADT_LOCAL_APIC *)entry;
341 madt_add_cpu(proc->ProcessorId, proc->Id, proc->LapicFlags);
343 case ACPI_MADT_TYPE_LOCAL_X2APIC:
344 x2apic = (ACPI_MADT_LOCAL_X2APIC *)entry;
345 madt_add_cpu(x2apic->Uid, x2apic->LocalApicId,
353 * Add an I/O APIC from an entry in the table.
356 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
358 ACPI_MADT_IO_APIC *apic;
360 switch (entry->Type) {
361 case ACPI_MADT_TYPE_IO_APIC:
362 apic = (ACPI_MADT_IO_APIC *)entry;
365 "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
366 apic->Id, apic->GlobalIrqBase,
367 (void *)(uintptr_t)apic->Address);
368 if (apic->Id > MAX_APIC_ID)
369 panic("%s: I/O APIC ID %u too high", __func__,
371 if (ioapics[apic->Id].io_apic != NULL)
372 panic("%s: Double APIC ID %u", __func__, apic->Id);
373 if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
374 printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
377 ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
378 apic->Id, apic->GlobalIrqBase);
379 ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
387 * Determine properties of an interrupt source. Note that for ACPI these
388 * functions are only used for ISA interrupts, so we assume ISA bus values
389 * (Active Hi, Edge Triggered) for conforming values except for the ACPI
390 * SCI for which we use Active Lo, Level Triggered.
392 static enum intr_polarity
393 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
396 switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
398 printf("WARNING: Bogus Interrupt Polarity. Assume CONFORMS\n");
400 case ACPI_MADT_POLARITY_CONFORMS:
401 if (Source == AcpiGbl_FADT.SciInterrupt)
402 return (INTR_POLARITY_LOW);
404 return (INTR_POLARITY_HIGH);
405 case ACPI_MADT_POLARITY_ACTIVE_HIGH:
406 return (INTR_POLARITY_HIGH);
407 case ACPI_MADT_POLARITY_ACTIVE_LOW:
408 return (INTR_POLARITY_LOW);
412 static enum intr_trigger
413 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
416 switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
418 printf("WARNING: Bogus Interrupt Trigger Mode. Assume CONFORMS.\n");
420 case ACPI_MADT_TRIGGER_CONFORMS:
421 if (Source == AcpiGbl_FADT.SciInterrupt)
422 return (INTR_TRIGGER_LEVEL);
424 return (INTR_TRIGGER_EDGE);
425 case ACPI_MADT_TRIGGER_EDGE:
426 return (INTR_TRIGGER_EDGE);
427 case ACPI_MADT_TRIGGER_LEVEL:
428 return (INTR_TRIGGER_LEVEL);
433 * Find the local APIC ID associated with a given ACPI Processor ID.
436 madt_find_cpu(u_int acpi_id, u_int *apic_id)
440 for (i = 0; i <= MAX_APIC_ID; i++) {
441 if (!lapics[i].la_enabled)
443 if (lapics[i].la_acpi_id != acpi_id)
452 * Find the IO APIC and pin on that APIC associated with a given global
456 madt_find_interrupt(int intr, void **apic, u_int *pin)
461 for (i = 0; i <= MAX_APIC_ID; i++) {
462 if (ioapics[i].io_apic == NULL ||
463 ioapics[i].io_vector > intr)
466 ioapics[best].io_vector < ioapics[i].io_vector)
471 *apic = ioapics[best].io_apic;
472 *pin = intr - ioapics[best].io_vector;
474 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
480 madt_parse_interrupt_values(void *entry,
481 enum intr_trigger *trig, enum intr_polarity *pol)
483 ACPI_MADT_INTERRUPT_OVERRIDE *intr;
489 printf("MADT: Interrupt override: source %u, irq %u\n",
490 intr->SourceIrq, intr->GlobalIrq);
491 KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
494 * Lookup the appropriate trigger and polarity modes for this
497 *trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
498 *pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
501 * If the SCI is identity mapped but has edge trigger and
502 * active-hi polarity or the force_sci_lo tunable is set,
503 * force it to use level/lo.
505 if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
506 madt_found_sci_override = 1;
507 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
508 if (tolower(buf[0]) == 'e')
509 *trig = INTR_TRIGGER_EDGE;
510 else if (tolower(buf[0]) == 'l')
511 *trig = INTR_TRIGGER_LEVEL;
514 "Invalid trigger %s: must be 'edge' or 'level'",
516 printf("MADT: Forcing SCI to %s trigger\n",
517 *trig == INTR_TRIGGER_EDGE ? "edge" : "level");
519 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
520 if (tolower(buf[0]) == 'h')
521 *pol = INTR_POLARITY_HIGH;
522 else if (tolower(buf[0]) == 'l')
523 *pol = INTR_POLARITY_LOW;
526 "Invalid polarity %s: must be 'high' or 'low'",
528 printf("MADT: Forcing SCI to active %s polarity\n",
529 *pol == INTR_POLARITY_HIGH ? "high" : "low");
535 * Parse an interrupt source override for an ISA interrupt.
538 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
540 void *new_ioapic, *old_ioapic;
541 u_int new_pin, old_pin;
542 enum intr_trigger trig;
543 enum intr_polarity pol;
545 if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
546 intr->GlobalIrq == 2) {
548 printf("MADT: Skipping timer override\n");
552 if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
553 printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
554 intr->GlobalIrq, intr->SourceIrq);
558 madt_parse_interrupt_values(intr, &trig, &pol);
560 /* Remap the IRQ if it is mapped to a different interrupt vector. */
561 if (intr->SourceIrq != intr->GlobalIrq) {
563 * If the SCI is remapped to a non-ISA global interrupt,
564 * then override the vector we use to setup and allocate
567 if (intr->GlobalIrq > 15 &&
568 intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
569 acpi_OverrideInterruptLevel(intr->GlobalIrq);
571 ioapic_remap_vector(new_ioapic, new_pin,
573 if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
575 printf("MADT: Could not find APIC for source IRQ %u\n",
577 else if (ioapic_get_vector(old_ioapic, old_pin) ==
579 ioapic_disable_pin(old_ioapic, old_pin);
582 /* Program the polarity and trigger mode. */
583 ioapic_set_triggermode(new_ioapic, new_pin, trig);
584 ioapic_set_polarity(new_ioapic, new_pin, pol);
588 * Parse an entry for an NMI routed to an IO APIC.
591 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
596 if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
597 printf("MADT: Could not find APIC for vector %u\n",
602 ioapic_set_nmi(ioapic, pin);
603 if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
604 ioapic_set_triggermode(ioapic, pin,
605 interrupt_trigger(nmi->IntiFlags, 0));
606 if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
607 ioapic_set_polarity(ioapic, pin,
608 interrupt_polarity(nmi->IntiFlags, 0));
612 * Parse an entry for an NMI routed to a local APIC LVT pin.
615 madt_handle_local_nmi(u_int acpi_id, UINT8 Lint, UINT16 IntiFlags)
619 if (acpi_id == 0xffffffff)
620 apic_id = APIC_ID_ALL;
621 else if (madt_find_cpu(acpi_id, &apic_id) != 0) {
623 printf("MADT: Ignoring local NMI routed to "
624 "ACPI CPU %u\n", acpi_id);
628 pin = APIC_LVT_LINT0;
630 pin = APIC_LVT_LINT1;
631 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
632 if (!(IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
633 lapic_set_lvt_triggermode(apic_id, pin,
634 interrupt_trigger(IntiFlags, 0));
635 if (!(IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
636 lapic_set_lvt_polarity(apic_id, pin,
637 interrupt_polarity(IntiFlags, 0));
641 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
644 madt_handle_local_nmi(nmi->ProcessorId == 0xff ? 0xffffffff :
645 nmi->ProcessorId, nmi->Lint, nmi->IntiFlags);
649 madt_parse_local_x2apic_nmi(ACPI_MADT_LOCAL_X2APIC_NMI *nmi)
652 madt_handle_local_nmi(nmi->Uid, nmi->Lint, nmi->IntiFlags);
656 * Parse interrupt entries.
659 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
662 switch (entry->Type) {
663 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
664 madt_parse_interrupt_override(
665 (ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
667 case ACPI_MADT_TYPE_NMI_SOURCE:
668 madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
670 case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
671 madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
673 case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
674 madt_parse_local_x2apic_nmi(
675 (ACPI_MADT_LOCAL_X2APIC_NMI *)entry);
681 * Setup per-CPU ACPI IDs.
684 madt_set_ids(void *dummy)
686 struct lapic_info *la;
694 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
695 la = &lapics[pc->pc_apic_id];
697 panic("APIC: CPU with APIC ID %u is not enabled",
699 pc->pc_acpi_id = la->la_acpi_id;
701 printf("APIC: CPU %u has ACPI ID %u\n", i,
705 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, madt_set_ids, NULL);