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/actables.h>
49 #include <dev/acpica/acpivar.h>
50 #include <dev/pci/pcivar.h>
52 /* These two arrays are indexed by APIC IDs. */
58 static struct lapic_info {
61 } lapics[MAX_APIC_ID + 1];
63 int madt_found_sci_override;
64 static ACPI_TABLE_MADT *madt;
65 static vm_paddr_t madt_physaddr;
66 static vm_offset_t madt_length;
68 static MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items");
70 static enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source);
71 static enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source);
72 static int madt_find_cpu(u_int acpi_id, u_int *apic_id);
73 static int madt_find_interrupt(int intr, void **apic, u_int *pin);
74 static void madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg);
75 static void madt_parse_interrupt_override(
76 ACPI_MADT_INTERRUPT_OVERRIDE *intr);
77 static void madt_parse_ints(ACPI_SUBTABLE_HEADER *entry,
79 static void madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi);
80 static void madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi);
81 static int madt_probe(void);
82 static int madt_probe_cpus(void);
83 static void madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry,
85 static void madt_register(void *dummy);
86 static int madt_setup_local(void);
87 static int madt_setup_io(void);
88 static void madt_walk_table(acpi_subtable_handler *handler, void *arg);
90 static struct apic_enumerator madt_enumerator = {
99 * Look for an ACPI Multiple APIC Description Table ("APIC")
105 madt_physaddr = acpi_find_table(ACPI_SIG_MADT);
106 if (madt_physaddr == 0)
112 * Run through the MP table enumerating CPUs.
115 madt_probe_cpus(void)
118 madt = acpi_map_table(madt_physaddr, ACPI_SIG_MADT);
119 madt_length = madt->Header.Length;
120 KASSERT(madt != NULL, ("Unable to re-map MADT"));
121 madt_walk_table(madt_probe_cpus_handler, NULL);
122 acpi_unmap_table(madt);
128 * Initialize the local APIC on the BSP.
131 madt_setup_local(void)
133 ACPI_TABLE_DMAR *dmartbl;
134 vm_paddr_t dmartbl_physaddr;
141 madt = pmap_mapbios(madt_physaddr, madt_length);
142 if ((cpu_feature2 & CPUID2_X2APIC) != 0) {
146 * Automatically detect several configurations where
147 * x2APIC mode is known to cause troubles. User can
148 * override the setting with hw.x2apic_enable tunable.
150 dmartbl_physaddr = acpi_find_table(ACPI_SIG_DMAR);
151 if (dmartbl_physaddr != 0) {
152 dmartbl = acpi_map_table(dmartbl_physaddr,
154 if ((dmartbl->Flags & ACPI_DMAR_X2APIC_OPT_OUT) != 0)
155 reason = "by DMAR table";
156 acpi_unmap_table(dmartbl);
158 if (vm_guest == VM_GUEST_VMWARE) {
159 vmware_hvcall(VMW_HVCMD_GETVCPU_INFO, p);
160 if ((p[0] & VMW_VCPUINFO_VCPU_RESERVED) != 0 ||
161 (p[0] & VMW_VCPUINFO_LEGACY_X2APIC) == 0)
163 "inside VMWare without intr redirection";
164 } else if (vm_guest == VM_GUEST_XEN) {
165 reason = "due to running under XEN";
166 } else if (vm_guest == VM_GUEST_NO &&
167 CPUID_TO_FAMILY(cpu_id) == 0x6 &&
168 CPUID_TO_MODEL(cpu_id) == 0x2a) {
169 hw_vendor = kern_getenv("smbios.planar.maker");
171 * It seems that some Lenovo and ASUS
172 * SandyBridge-based notebook BIOSes have a
173 * bug which prevents booting AP in x2APIC
174 * mode. Since the only way to detect mobile
175 * CPU is to check northbridge pci id, which
176 * cannot be done that early, disable x2APIC
177 * for all Lenovo and ASUS SandyBridge
180 if (hw_vendor != NULL) {
181 if (!strcmp(hw_vendor, "LENOVO") ||
183 "ASUSTeK Computer Inc.")) {
185 "for a suspected SandyBridge BIOS bug";
190 bios_x2apic = lapic_is_x2apic();
191 if (reason != NULL && bios_x2apic) {
193 printf("x2APIC should be disabled %s but "
194 "already enabled by BIOS; enabling.\n",
200 else if (bootverbose)
201 printf("x2APIC available but disabled %s\n", reason);
202 user_x2apic = x2apic_mode;
203 TUNABLE_INT_FETCH("hw.x2apic_enable", &user_x2apic);
204 if (user_x2apic != x2apic_mode) {
205 if (bios_x2apic && !user_x2apic)
206 printf("x2APIC disabled by tunable and "
207 "enabled by BIOS; ignoring tunable.");
209 x2apic_mode = user_x2apic;
213 lapic_init(madt->Address);
214 printf("ACPI APIC Table: <%.*s %.*s>\n",
215 (int)sizeof(madt->Header.OemId), madt->Header.OemId,
216 (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
219 * We ignore 64-bit local APIC override entries. Should we
220 * perhaps emit a warning here if we find one?
226 * Enumerate I/O APICs and setup interrupt sources.
235 /* Try to initialize ACPI so that we can access the FADT. */
237 if (ACPI_FAILURE(i)) {
238 printf("MADT: ACPI Startup failed with %s\n",
239 AcpiFormatException(i));
240 printf("Try disabling either ACPI or apic support.\n");
241 panic("Using MADT but ACPI doesn't work");
244 ioapics = malloc(sizeof(*ioapics) * (MAX_APIC_ID + 1), M_MADT,
247 /* First, we run through adding I/O APIC's. */
248 madt_walk_table(madt_parse_apics, NULL);
250 /* Second, we run through the table tweaking interrupt sources. */
251 madt_walk_table(madt_parse_ints, NULL);
254 * If there was not an explicit override entry for the SCI,
255 * force it to use level trigger and active-low polarity.
257 if (!madt_found_sci_override) {
258 if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
260 printf("MADT: Could not find APIC for SCI IRQ %u\n",
261 AcpiGbl_FADT.SciInterrupt);
264 "MADT: Forcing active-low polarity and level trigger for SCI\n");
265 ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
266 ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
270 /* Third, we register all the I/O APIC's. */
271 for (i = 0; i <= MAX_APIC_ID; i++)
272 if (ioapics[i].io_apic != NULL)
273 ioapic_register(ioapics[i].io_apic);
275 /* Finally, we throw the switch to enable the I/O APIC's. */
276 acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
278 free(ioapics, M_MADT);
285 madt_register(void *dummy __unused)
288 apic_register_enumerator(&madt_enumerator);
290 SYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL);
293 * Call the handler routine for each entry in the MADT table.
296 madt_walk_table(acpi_subtable_handler *handler, void *arg)
299 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
304 madt_add_cpu(u_int acpi_id, u_int apic_id, u_int flags)
306 struct lapic_info *la;
309 * The MADT does not include a BSP flag, so we have to let the
310 * MP code figure out which CPU is the BSP on its own.
313 printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
314 apic_id, acpi_id, flags & ACPI_MADT_ENABLED ?
315 "enabled" : "disabled");
316 if (!(flags & ACPI_MADT_ENABLED))
318 if (apic_id > MAX_APIC_ID) {
319 printf("MADT: Ignoring local APIC ID %u (too high)\n",
324 la = &lapics[apic_id];
325 KASSERT(la->la_enabled == 0, ("Duplicate local APIC ID %u", apic_id));
327 la->la_acpi_id = acpi_id;
328 lapic_create(apic_id, 0);
332 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
334 ACPI_MADT_LOCAL_APIC *proc;
335 ACPI_MADT_LOCAL_X2APIC *x2apic;
337 switch (entry->Type) {
338 case ACPI_MADT_TYPE_LOCAL_APIC:
339 proc = (ACPI_MADT_LOCAL_APIC *)entry;
340 madt_add_cpu(proc->ProcessorId, proc->Id, proc->LapicFlags);
342 case ACPI_MADT_TYPE_LOCAL_X2APIC:
343 x2apic = (ACPI_MADT_LOCAL_X2APIC *)entry;
344 madt_add_cpu(x2apic->Uid, x2apic->LocalApicId,
352 * Add an I/O APIC from an entry in the table.
355 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
357 ACPI_MADT_IO_APIC *apic;
359 switch (entry->Type) {
360 case ACPI_MADT_TYPE_IO_APIC:
361 apic = (ACPI_MADT_IO_APIC *)entry;
364 "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
365 apic->Id, apic->GlobalIrqBase,
366 (void *)(uintptr_t)apic->Address);
367 if (apic->Id > MAX_APIC_ID)
368 panic("%s: I/O APIC ID %u too high", __func__,
370 if (ioapics[apic->Id].io_apic != NULL)
371 panic("%s: Double APIC ID %u", __func__, apic->Id);
372 if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
373 printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
376 ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
377 apic->Id, apic->GlobalIrqBase);
378 ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
386 * Determine properties of an interrupt source. Note that for ACPI these
387 * functions are only used for ISA interrupts, so we assume ISA bus values
388 * (Active Hi, Edge Triggered) for conforming values except for the ACPI
389 * SCI for which we use Active Lo, Level Triggered.
391 static enum intr_polarity
392 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
395 switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
397 printf("WARNING: Bogus Interrupt Polarity. Assume CONFORMS\n");
399 case ACPI_MADT_POLARITY_CONFORMS:
400 if (Source == AcpiGbl_FADT.SciInterrupt)
401 return (INTR_POLARITY_LOW);
403 return (INTR_POLARITY_HIGH);
404 case ACPI_MADT_POLARITY_ACTIVE_HIGH:
405 return (INTR_POLARITY_HIGH);
406 case ACPI_MADT_POLARITY_ACTIVE_LOW:
407 return (INTR_POLARITY_LOW);
411 static enum intr_trigger
412 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
415 switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
417 printf("WARNING: Bogus Interrupt Trigger Mode. Assume CONFORMS.\n");
419 case ACPI_MADT_TRIGGER_CONFORMS:
420 if (Source == AcpiGbl_FADT.SciInterrupt)
421 return (INTR_TRIGGER_LEVEL);
423 return (INTR_TRIGGER_EDGE);
424 case ACPI_MADT_TRIGGER_EDGE:
425 return (INTR_TRIGGER_EDGE);
426 case ACPI_MADT_TRIGGER_LEVEL:
427 return (INTR_TRIGGER_LEVEL);
432 * Find the local APIC ID associated with a given ACPI Processor ID.
435 madt_find_cpu(u_int acpi_id, u_int *apic_id)
439 for (i = 0; i <= MAX_APIC_ID; i++) {
440 if (!lapics[i].la_enabled)
442 if (lapics[i].la_acpi_id != acpi_id)
451 * Find the IO APIC and pin on that APIC associated with a given global
455 madt_find_interrupt(int intr, void **apic, u_int *pin)
460 for (i = 0; i <= MAX_APIC_ID; i++) {
461 if (ioapics[i].io_apic == NULL ||
462 ioapics[i].io_vector > intr)
465 ioapics[best].io_vector < ioapics[i].io_vector)
470 *apic = ioapics[best].io_apic;
471 *pin = intr - ioapics[best].io_vector;
473 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
479 madt_parse_interrupt_values(void *entry,
480 enum intr_trigger *trig, enum intr_polarity *pol)
482 ACPI_MADT_INTERRUPT_OVERRIDE *intr;
488 printf("MADT: Interrupt override: source %u, irq %u\n",
489 intr->SourceIrq, intr->GlobalIrq);
490 KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
493 * Lookup the appropriate trigger and polarity modes for this
496 *trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
497 *pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
500 * If the SCI is identity mapped but has edge trigger and
501 * active-hi polarity or the force_sci_lo tunable is set,
502 * force it to use level/lo.
504 if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
505 madt_found_sci_override = 1;
506 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
507 if (tolower(buf[0]) == 'e')
508 *trig = INTR_TRIGGER_EDGE;
509 else if (tolower(buf[0]) == 'l')
510 *trig = INTR_TRIGGER_LEVEL;
513 "Invalid trigger %s: must be 'edge' or 'level'",
515 printf("MADT: Forcing SCI to %s trigger\n",
516 *trig == INTR_TRIGGER_EDGE ? "edge" : "level");
518 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
519 if (tolower(buf[0]) == 'h')
520 *pol = INTR_POLARITY_HIGH;
521 else if (tolower(buf[0]) == 'l')
522 *pol = INTR_POLARITY_LOW;
525 "Invalid polarity %s: must be 'high' or 'low'",
527 printf("MADT: Forcing SCI to active %s polarity\n",
528 *pol == INTR_POLARITY_HIGH ? "high" : "low");
534 * Parse an interrupt source override for an ISA interrupt.
537 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
539 void *new_ioapic, *old_ioapic;
540 u_int new_pin, old_pin;
541 enum intr_trigger trig;
542 enum intr_polarity pol;
544 if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
545 intr->GlobalIrq == 2) {
547 printf("MADT: Skipping timer override\n");
551 if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
552 printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
553 intr->GlobalIrq, intr->SourceIrq);
557 madt_parse_interrupt_values(intr, &trig, &pol);
559 /* Remap the IRQ if it is mapped to a different interrupt vector. */
560 if (intr->SourceIrq != intr->GlobalIrq) {
562 * If the SCI is remapped to a non-ISA global interrupt,
563 * then override the vector we use to setup and allocate
566 if (intr->GlobalIrq > 15 &&
567 intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
568 acpi_OverrideInterruptLevel(intr->GlobalIrq);
570 ioapic_remap_vector(new_ioapic, new_pin,
572 if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
574 printf("MADT: Could not find APIC for source IRQ %u\n",
576 else if (ioapic_get_vector(old_ioapic, old_pin) ==
578 ioapic_disable_pin(old_ioapic, old_pin);
581 /* Program the polarity and trigger mode. */
582 ioapic_set_triggermode(new_ioapic, new_pin, trig);
583 ioapic_set_polarity(new_ioapic, new_pin, pol);
587 * Parse an entry for an NMI routed to an IO APIC.
590 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
595 if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
596 printf("MADT: Could not find APIC for vector %u\n",
601 ioapic_set_nmi(ioapic, pin);
602 if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
603 ioapic_set_triggermode(ioapic, pin,
604 interrupt_trigger(nmi->IntiFlags, 0));
605 if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
606 ioapic_set_polarity(ioapic, pin,
607 interrupt_polarity(nmi->IntiFlags, 0));
611 * Parse an entry for an NMI routed to a local APIC LVT pin.
614 madt_handle_local_nmi(u_int acpi_id, UINT8 Lint, UINT16 IntiFlags)
618 if (acpi_id == 0xffffffff)
619 apic_id = APIC_ID_ALL;
620 else if (madt_find_cpu(acpi_id, &apic_id) != 0) {
622 printf("MADT: Ignoring local NMI routed to "
623 "ACPI CPU %u\n", acpi_id);
627 pin = APIC_LVT_LINT0;
629 pin = APIC_LVT_LINT1;
630 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
631 if (!(IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
632 lapic_set_lvt_triggermode(apic_id, pin,
633 interrupt_trigger(IntiFlags, 0));
634 if (!(IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
635 lapic_set_lvt_polarity(apic_id, pin,
636 interrupt_polarity(IntiFlags, 0));
640 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
643 madt_handle_local_nmi(nmi->ProcessorId == 0xff ? 0xffffffff :
644 nmi->ProcessorId, nmi->Lint, nmi->IntiFlags);
648 madt_parse_local_x2apic_nmi(ACPI_MADT_LOCAL_X2APIC_NMI *nmi)
651 madt_handle_local_nmi(nmi->Uid, nmi->Lint, nmi->IntiFlags);
655 * Parse interrupt entries.
658 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
661 switch (entry->Type) {
662 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
663 madt_parse_interrupt_override(
664 (ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
666 case ACPI_MADT_TYPE_NMI_SOURCE:
667 madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
669 case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
670 madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
672 case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
673 madt_parse_local_x2apic_nmi(
674 (ACPI_MADT_LOCAL_X2APIC_NMI *)entry);
680 * Setup per-CPU ACPI IDs.
683 madt_set_ids(void *dummy)
685 struct lapic_info *la;
693 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
694 la = &lapics[pc->pc_apic_id];
696 panic("APIC: CPU with APIC ID %u is not enabled",
698 pc->pc_acpi_id = la->la_acpi_id;
700 printf("APIC: CPU %u has ACPI ID %u\n", i,
704 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, madt_set_ids, NULL);