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;
137 madt = pmap_mapbios(madt_physaddr, madt_length);
138 if ((cpu_feature2 & CPUID2_X2APIC) != 0) {
140 dmartbl_physaddr = acpi_find_table(ACPI_SIG_DMAR);
141 if (dmartbl_physaddr != 0) {
142 dmartbl = acpi_map_table(dmartbl_physaddr,
144 if ((dmartbl->Flags & ACPI_DMAR_X2APIC_OPT_OUT) != 0) {
148 "x2APIC available but disabled by DMAR table\n");
150 acpi_unmap_table(dmartbl);
152 if (vm_guest == VM_GUEST_VMWARE) {
153 vmware_hvcall(VMW_HVCMD_GETVCPU_INFO, p);
154 if ((p[0] & VMW_VCPUINFO_VCPU_RESERVED) != 0 ||
155 (p[0] & VMW_VCPUINFO_LEGACY_X2APIC) == 0) {
159 "x2APIC available but disabled inside VMWare without intr redirection\n");
161 } else if (vm_guest == VM_GUEST_XEN) {
164 TUNABLE_INT_FETCH("hw.x2apic_enable", &x2apic_mode);
167 lapic_init(madt->Address);
168 printf("ACPI APIC Table: <%.*s %.*s>\n",
169 (int)sizeof(madt->Header.OemId), madt->Header.OemId,
170 (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
173 * We ignore 64-bit local APIC override entries. Should we
174 * perhaps emit a warning here if we find one?
180 * Enumerate I/O APICs and setup interrupt sources.
189 /* Try to initialize ACPI so that we can access the FADT. */
191 if (ACPI_FAILURE(i)) {
192 printf("MADT: ACPI Startup failed with %s\n",
193 AcpiFormatException(i));
194 printf("Try disabling either ACPI or apic support.\n");
195 panic("Using MADT but ACPI doesn't work");
198 ioapics = malloc(sizeof(*ioapics) * (MAX_APIC_ID + 1), M_MADT,
201 /* First, we run through adding I/O APIC's. */
202 madt_walk_table(madt_parse_apics, NULL);
204 /* Second, we run through the table tweaking interrupt sources. */
205 madt_walk_table(madt_parse_ints, NULL);
208 * If there was not an explicit override entry for the SCI,
209 * force it to use level trigger and active-low polarity.
211 if (!madt_found_sci_override) {
212 if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
214 printf("MADT: Could not find APIC for SCI IRQ %u\n",
215 AcpiGbl_FADT.SciInterrupt);
218 "MADT: Forcing active-low polarity and level trigger for SCI\n");
219 ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
220 ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
224 /* Third, we register all the I/O APIC's. */
225 for (i = 0; i <= MAX_APIC_ID; i++)
226 if (ioapics[i].io_apic != NULL)
227 ioapic_register(ioapics[i].io_apic);
229 /* Finally, we throw the switch to enable the I/O APIC's. */
230 acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
232 free(ioapics, M_MADT);
239 madt_register(void *dummy __unused)
242 apic_register_enumerator(&madt_enumerator);
244 SYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL);
247 * Call the handler routine for each entry in the MADT table.
250 madt_walk_table(acpi_subtable_handler *handler, void *arg)
253 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
258 madt_add_cpu(u_int acpi_id, u_int apic_id, u_int flags)
260 struct lapic_info *la;
263 * The MADT does not include a BSP flag, so we have to let the
264 * MP code figure out which CPU is the BSP on its own.
267 printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
268 apic_id, acpi_id, flags & ACPI_MADT_ENABLED ?
269 "enabled" : "disabled");
270 if (!(flags & ACPI_MADT_ENABLED))
272 if (apic_id > MAX_APIC_ID) {
273 printf("MADT: Ignoring local APIC ID %u (too high)\n",
278 la = &lapics[apic_id];
279 KASSERT(la->la_enabled == 0, ("Duplicate local APIC ID %u", apic_id));
281 la->la_acpi_id = acpi_id;
282 lapic_create(apic_id, 0);
286 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
288 ACPI_MADT_LOCAL_APIC *proc;
289 ACPI_MADT_LOCAL_X2APIC *x2apic;
291 switch (entry->Type) {
292 case ACPI_MADT_TYPE_LOCAL_APIC:
293 proc = (ACPI_MADT_LOCAL_APIC *)entry;
294 madt_add_cpu(proc->ProcessorId, proc->Id, proc->LapicFlags);
296 case ACPI_MADT_TYPE_LOCAL_X2APIC:
297 x2apic = (ACPI_MADT_LOCAL_X2APIC *)entry;
298 madt_add_cpu(x2apic->Uid, x2apic->LocalApicId,
306 * Add an I/O APIC from an entry in the table.
309 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
311 ACPI_MADT_IO_APIC *apic;
313 switch (entry->Type) {
314 case ACPI_MADT_TYPE_IO_APIC:
315 apic = (ACPI_MADT_IO_APIC *)entry;
318 "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
319 apic->Id, apic->GlobalIrqBase,
320 (void *)(uintptr_t)apic->Address);
321 if (apic->Id > MAX_APIC_ID)
322 panic("%s: I/O APIC ID %u too high", __func__,
324 if (ioapics[apic->Id].io_apic != NULL)
325 panic("%s: Double APIC ID %u", __func__, apic->Id);
326 if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
327 printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
330 ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
331 apic->Id, apic->GlobalIrqBase);
332 ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
340 * Determine properties of an interrupt source. Note that for ACPI these
341 * functions are only used for ISA interrupts, so we assume ISA bus values
342 * (Active Hi, Edge Triggered) for conforming values except for the ACPI
343 * SCI for which we use Active Lo, Level Triggered.
345 static enum intr_polarity
346 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
349 switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
351 printf("WARNING: Bogus Interrupt Polarity. Assume CONFORMS\n");
353 case ACPI_MADT_POLARITY_CONFORMS:
354 if (Source == AcpiGbl_FADT.SciInterrupt)
355 return (INTR_POLARITY_LOW);
357 return (INTR_POLARITY_HIGH);
358 case ACPI_MADT_POLARITY_ACTIVE_HIGH:
359 return (INTR_POLARITY_HIGH);
360 case ACPI_MADT_POLARITY_ACTIVE_LOW:
361 return (INTR_POLARITY_LOW);
365 static enum intr_trigger
366 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
369 switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
371 printf("WARNING: Bogus Interrupt Trigger Mode. Assume CONFORMS.\n");
373 case ACPI_MADT_TRIGGER_CONFORMS:
374 if (Source == AcpiGbl_FADT.SciInterrupt)
375 return (INTR_TRIGGER_LEVEL);
377 return (INTR_TRIGGER_EDGE);
378 case ACPI_MADT_TRIGGER_EDGE:
379 return (INTR_TRIGGER_EDGE);
380 case ACPI_MADT_TRIGGER_LEVEL:
381 return (INTR_TRIGGER_LEVEL);
386 * Find the local APIC ID associated with a given ACPI Processor ID.
389 madt_find_cpu(u_int acpi_id, u_int *apic_id)
393 for (i = 0; i <= MAX_APIC_ID; i++) {
394 if (!lapics[i].la_enabled)
396 if (lapics[i].la_acpi_id != acpi_id)
405 * Find the IO APIC and pin on that APIC associated with a given global
409 madt_find_interrupt(int intr, void **apic, u_int *pin)
414 for (i = 0; i <= MAX_APIC_ID; i++) {
415 if (ioapics[i].io_apic == NULL ||
416 ioapics[i].io_vector > intr)
419 ioapics[best].io_vector < ioapics[i].io_vector)
424 *apic = ioapics[best].io_apic;
425 *pin = intr - ioapics[best].io_vector;
427 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
433 madt_parse_interrupt_values(void *entry,
434 enum intr_trigger *trig, enum intr_polarity *pol)
436 ACPI_MADT_INTERRUPT_OVERRIDE *intr;
442 printf("MADT: Interrupt override: source %u, irq %u\n",
443 intr->SourceIrq, intr->GlobalIrq);
444 KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
447 * Lookup the appropriate trigger and polarity modes for this
450 *trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
451 *pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
454 * If the SCI is identity mapped but has edge trigger and
455 * active-hi polarity or the force_sci_lo tunable is set,
456 * force it to use level/lo.
458 if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
459 madt_found_sci_override = 1;
460 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
461 if (tolower(buf[0]) == 'e')
462 *trig = INTR_TRIGGER_EDGE;
463 else if (tolower(buf[0]) == 'l')
464 *trig = INTR_TRIGGER_LEVEL;
467 "Invalid trigger %s: must be 'edge' or 'level'",
469 printf("MADT: Forcing SCI to %s trigger\n",
470 *trig == INTR_TRIGGER_EDGE ? "edge" : "level");
472 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
473 if (tolower(buf[0]) == 'h')
474 *pol = INTR_POLARITY_HIGH;
475 else if (tolower(buf[0]) == 'l')
476 *pol = INTR_POLARITY_LOW;
479 "Invalid polarity %s: must be 'high' or 'low'",
481 printf("MADT: Forcing SCI to active %s polarity\n",
482 *pol == INTR_POLARITY_HIGH ? "high" : "low");
488 * Parse an interrupt source override for an ISA interrupt.
491 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
493 void *new_ioapic, *old_ioapic;
494 u_int new_pin, old_pin;
495 enum intr_trigger trig;
496 enum intr_polarity pol;
498 if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
499 intr->GlobalIrq == 2) {
501 printf("MADT: Skipping timer override\n");
505 if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
506 printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
507 intr->GlobalIrq, intr->SourceIrq);
511 madt_parse_interrupt_values(intr, &trig, &pol);
513 /* Remap the IRQ if it is mapped to a different interrupt vector. */
514 if (intr->SourceIrq != intr->GlobalIrq) {
516 * If the SCI is remapped to a non-ISA global interrupt,
517 * then override the vector we use to setup and allocate
520 if (intr->GlobalIrq > 15 &&
521 intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
522 acpi_OverrideInterruptLevel(intr->GlobalIrq);
524 ioapic_remap_vector(new_ioapic, new_pin,
526 if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
528 printf("MADT: Could not find APIC for source IRQ %u\n",
530 else if (ioapic_get_vector(old_ioapic, old_pin) ==
532 ioapic_disable_pin(old_ioapic, old_pin);
535 /* Program the polarity and trigger mode. */
536 ioapic_set_triggermode(new_ioapic, new_pin, trig);
537 ioapic_set_polarity(new_ioapic, new_pin, pol);
541 * Parse an entry for an NMI routed to an IO APIC.
544 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
549 if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
550 printf("MADT: Could not find APIC for vector %u\n",
555 ioapic_set_nmi(ioapic, pin);
556 if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
557 ioapic_set_triggermode(ioapic, pin,
558 interrupt_trigger(nmi->IntiFlags, 0));
559 if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
560 ioapic_set_polarity(ioapic, pin,
561 interrupt_polarity(nmi->IntiFlags, 0));
565 * Parse an entry for an NMI routed to a local APIC LVT pin.
568 madt_handle_local_nmi(u_int acpi_id, UINT8 Lint, UINT16 IntiFlags)
572 if (acpi_id == 0xffffffff)
573 apic_id = APIC_ID_ALL;
574 else if (madt_find_cpu(acpi_id, &apic_id) != 0) {
576 printf("MADT: Ignoring local NMI routed to "
577 "ACPI CPU %u\n", acpi_id);
581 pin = APIC_LVT_LINT0;
583 pin = APIC_LVT_LINT1;
584 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
585 if (!(IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
586 lapic_set_lvt_triggermode(apic_id, pin,
587 interrupt_trigger(IntiFlags, 0));
588 if (!(IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
589 lapic_set_lvt_polarity(apic_id, pin,
590 interrupt_polarity(IntiFlags, 0));
594 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
597 madt_handle_local_nmi(nmi->ProcessorId == 0xff ? 0xffffffff :
598 nmi->ProcessorId, nmi->Lint, nmi->IntiFlags);
602 madt_parse_local_x2apic_nmi(ACPI_MADT_LOCAL_X2APIC_NMI *nmi)
605 madt_handle_local_nmi(nmi->Uid, nmi->Lint, nmi->IntiFlags);
609 * Parse interrupt entries.
612 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
615 switch (entry->Type) {
616 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
617 madt_parse_interrupt_override(
618 (ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
620 case ACPI_MADT_TYPE_NMI_SOURCE:
621 madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
623 case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
624 madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
626 case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
627 madt_parse_local_x2apic_nmi(
628 (ACPI_MADT_LOCAL_X2APIC_NMI *)entry);
634 * Setup per-CPU ACPI IDs.
637 madt_set_ids(void *dummy)
639 struct lapic_info *la;
647 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
648 la = &lapics[pc->pc_apic_id];
650 panic("APIC: CPU with APIC ID %u is not enabled",
652 pc->pc_acpi_id = la->la_acpi_id;
654 printf("APIC: CPU %u has ACPI ID %u\n", i,
658 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, madt_set_ids, NULL);