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.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
41 #include <vm/vm_param.h>
44 #include <machine/apicreg.h>
45 #include <machine/frame.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/apicvar.h>
48 #include <machine/md_var.h>
49 #include <machine/specialreg.h>
51 #include <contrib/dev/acpica/acpi.h>
52 #include <contrib/dev/acpica/actables.h>
53 #include <dev/acpica/acpivar.h>
54 #include <dev/pci/pcivar.h>
56 typedef void madt_entry_handler(ACPI_SUBTABLE_HEADER *entry, void *arg);
58 /* These two arrays are indexed by APIC IDs. */
62 } ioapics[MAX_APIC_ID + 1];
67 } lapics[MAX_APIC_ID + 1];
69 static int madt_found_sci_override;
70 static ACPI_TABLE_MADT *madt;
71 static vm_paddr_t madt_physaddr;
72 static vm_offset_t madt_length;
74 MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items");
76 static enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source);
77 static enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source);
78 static int madt_find_cpu(u_int acpi_id, u_int *apic_id);
79 static int madt_find_interrupt(int intr, void **apic, u_int *pin);
80 static void *madt_map(vm_paddr_t pa, int offset, vm_offset_t length);
81 static void *madt_map_table(vm_paddr_t pa, int offset, const char *sig);
82 static void madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg);
83 static void madt_parse_interrupt_override(
84 ACPI_MADT_INTERRUPT_OVERRIDE *intr);
85 static void madt_parse_ints(ACPI_SUBTABLE_HEADER *entry,
87 static void madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi);
88 static void madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi);
89 static int madt_probe(void);
90 static int madt_probe_cpus(void);
91 static void madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry,
93 static int madt_probe_table(vm_paddr_t address);
94 static void madt_register(void *dummy);
95 static int madt_setup_local(void);
96 static int madt_setup_io(void);
97 static void madt_unmap(void *data, vm_offset_t length);
98 static void madt_unmap_table(void *table);
99 static void madt_walk_table(madt_entry_handler *handler, void *arg);
101 static struct apic_enumerator madt_enumerator = {
110 * Code to abuse the crashdump map to map in the tables for the early
111 * probe. We cheat and make the following assumptions about how we
112 * use this KVA: pages 0 and 1 are used to map in the header of each
113 * table found via the RSDT or XSDT and pages 2 to n are used to map
114 * in the RSDT or XSDT. We have to use 2 pages for the table headers
115 * in case a header spans a page boundary. The offset is in pages;
116 * the length is in bytes.
119 madt_map(vm_paddr_t pa, int offset, vm_offset_t length)
124 off = pa & PAGE_MASK;
125 length = roundup(length + off, PAGE_SIZE);
127 va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
128 (offset * PAGE_SIZE);
129 data = (void *)(va + off);
142 madt_unmap(void *data, vm_offset_t length)
146 va = (vm_offset_t)data;
147 off = va & PAGE_MASK;
148 length = roundup(length + off, PAGE_SIZE);
159 madt_map_table(vm_paddr_t pa, int offset, const char *sig)
161 ACPI_TABLE_HEADER *header;
165 header = madt_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
166 if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
167 madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
170 length = header->Length;
171 madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
172 table = madt_map(pa, offset, length);
173 if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
175 printf("MADT: Failed checksum for table %s\n", sig);
176 madt_unmap(table, length);
183 madt_unmap_table(void *table)
185 ACPI_TABLE_HEADER *header;
187 header = (ACPI_TABLE_HEADER *)table;
188 madt_unmap(table, header->Length);
192 * Look for an ACPI Multiple APIC Description Table ("APIC")
197 ACPI_PHYSICAL_ADDRESS rsdp_ptr;
198 ACPI_TABLE_RSDP *rsdp;
199 ACPI_TABLE_RSDT *rsdt;
200 ACPI_TABLE_XSDT *xsdt;
203 if (resource_disabled("acpi", 0))
207 * Map in the RSDP. Since ACPI uses AcpiOsMapMemory() which in turn
208 * calls pmap_mapbios() to find the RSDP, we assume that we can use
209 * pmap_mapbios() to map the RSDP.
211 if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
213 rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
216 printf("MADT: Failed to map RSDP\n");
221 * For ACPI >= 2.0, use the XSDT if it is available.
222 * Otherwise, use the RSDT. We map the XSDT or RSDT at page 1
223 * in the crashdump area. Page 0 is used to map in the
224 * headers of candidate ACPI tables.
226 if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
228 * AcpiOsGetRootPointer only verifies the checksum for
229 * the version 1.0 portion of the RSDP. Version 2.0 has
230 * an additional checksum that we verify first.
232 if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
234 printf("MADT: RSDP failed extended checksum\n");
237 xsdt = madt_map_table(rsdp->XsdtPhysicalAddress, 2,
241 printf("MADT: Failed to map XSDT\n");
244 count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
246 for (i = 0; i < count; i++)
247 if (madt_probe_table(xsdt->TableOffsetEntry[i]))
249 madt_unmap_table(xsdt);
251 rsdt = madt_map_table(rsdp->RsdtPhysicalAddress, 2,
255 printf("MADT: Failed to map RSDT\n");
258 count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
260 for (i = 0; i < count; i++)
261 if (madt_probe_table(rsdt->TableOffsetEntry[i]))
263 madt_unmap_table(rsdt);
265 pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
266 if (madt_physaddr == 0) {
268 printf("MADT: No MADT table found\n");
272 printf("MADT: Found table at 0x%jx\n",
273 (uintmax_t)madt_physaddr);
276 * Verify that we can map the full table and that its checksum is
279 madt = madt_map_table(madt_physaddr, 0, ACPI_SIG_MADT);
282 madt_unmap_table(madt);
289 * See if a given ACPI table is the MADT.
292 madt_probe_table(vm_paddr_t address)
294 ACPI_TABLE_HEADER *table;
296 table = madt_map(address, 0, sizeof(ACPI_TABLE_HEADER));
299 printf("MADT: Failed to map table at 0x%jx\n",
304 printf("Table '%.4s' at 0x%jx\n", table->Signature,
307 if (strncmp(table->Signature, ACPI_SIG_MADT, ACPI_NAME_SIZE) != 0) {
308 madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
311 madt_physaddr = address;
312 madt_length = table->Length;
313 madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
318 * Run through the MP table enumerating CPUs.
321 madt_probe_cpus(void)
324 madt = madt_map_table(madt_physaddr, 0, ACPI_SIG_MADT);
325 KASSERT(madt != NULL, ("Unable to re-map MADT"));
326 madt_walk_table(madt_probe_cpus_handler, NULL);
327 madt_unmap_table(madt);
333 * Initialize the local APIC on the BSP.
336 madt_setup_local(void)
339 madt = pmap_mapbios(madt_physaddr, madt_length);
340 lapic_init(madt->Address);
341 printf("ACPI APIC Table: <%.*s %.*s>\n",
342 (int)sizeof(madt->Header.OemId), madt->Header.OemId,
343 (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
346 * We ignore 64-bit local APIC override entries. Should we
347 * perhaps emit a warning here if we find one?
353 * Enumerate I/O APICs and setup interrupt sources.
362 /* Try to initialize ACPI so that we can access the FADT. */
364 if (ACPI_FAILURE(i)) {
365 printf("MADT: ACPI Startup failed with %s\n",
366 AcpiFormatException(i));
367 printf("Try disabling either ACPI or apic support.\n");
368 panic("Using MADT but ACPI doesn't work");
371 /* First, we run through adding I/O APIC's. */
372 madt_walk_table(madt_parse_apics, NULL);
374 /* Second, we run through the table tweaking interrupt sources. */
375 madt_walk_table(madt_parse_ints, NULL);
378 * If there was not an explicit override entry for the SCI,
379 * force it to use level trigger and active-low polarity.
381 if (!madt_found_sci_override) {
382 if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
384 printf("MADT: Could not find APIC for SCI IRQ %u\n",
385 AcpiGbl_FADT.SciInterrupt);
388 "MADT: Forcing active-low polarity and level trigger for SCI\n");
389 ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
390 ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
394 /* Third, we register all the I/O APIC's. */
395 for (i = 0; i <= MAX_APIC_ID; i++)
396 if (ioapics[i].io_apic != NULL)
397 ioapic_register(ioapics[i].io_apic);
399 /* Finally, we throw the switch to enable the I/O APIC's. */
400 acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
406 madt_register(void *dummy __unused)
409 apic_register_enumerator(&madt_enumerator);
411 SYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST,
412 madt_register, NULL);
415 * Call the handler routine for each entry in the MADT table.
418 madt_walk_table(madt_entry_handler *handler, void *arg)
420 ACPI_SUBTABLE_HEADER *entry;
423 end = (u_char *)(madt) + madt->Header.Length;
424 for (p = (u_char *)(madt + 1); p < end; ) {
425 entry = (ACPI_SUBTABLE_HEADER *)p;
432 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
434 ACPI_MADT_LOCAL_APIC *proc;
435 struct lapic_info *la;
437 switch (entry->Type) {
438 case ACPI_MADT_TYPE_LOCAL_APIC:
440 * The MADT does not include a BSP flag, so we have to
441 * let the MP code figure out which CPU is the BSP on
444 proc = (ACPI_MADT_LOCAL_APIC *)entry;
446 printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
447 proc->Id, proc->ProcessorId,
448 (proc->LapicFlags & ACPI_MADT_ENABLED) ?
449 "enabled" : "disabled");
450 if (!(proc->LapicFlags & ACPI_MADT_ENABLED))
452 if (proc->Id > MAX_APIC_ID)
453 panic("%s: CPU ID %u too high", __func__, proc->Id);
454 la = &lapics[proc->Id];
455 KASSERT(la->la_enabled == 0,
456 ("Duplicate local APIC ID %u", proc->Id));
458 la->la_acpi_id = proc->ProcessorId;
459 lapic_create(proc->Id, 0);
466 * Add an I/O APIC from an entry in the table.
469 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
471 ACPI_MADT_IO_APIC *apic;
473 switch (entry->Type) {
474 case ACPI_MADT_TYPE_IO_APIC:
475 apic = (ACPI_MADT_IO_APIC *)entry;
478 "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
479 apic->Id, apic->GlobalIrqBase,
480 (void *)(uintptr_t)apic->Address);
481 if (apic->Id > MAX_APIC_ID)
482 panic("%s: I/O APIC ID %u too high", __func__,
484 if (ioapics[apic->Id].io_apic != NULL)
485 panic("%s: Double APIC ID %u", __func__, apic->Id);
486 if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
487 printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
490 ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
491 apic->Id, apic->GlobalIrqBase);
492 ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
500 * Determine properties of an interrupt source. Note that for ACPI these
501 * functions are only used for ISA interrupts, so we assume ISA bus values
502 * (Active Hi, Edge Triggered) for conforming values except for the ACPI
503 * SCI for which we use Active Lo, Level Triggered.
505 static enum intr_polarity
506 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
509 switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
510 case ACPI_MADT_POLARITY_CONFORMS:
511 if (Source == AcpiGbl_FADT.SciInterrupt)
512 return (INTR_POLARITY_LOW);
514 return (INTR_POLARITY_HIGH);
515 case ACPI_MADT_POLARITY_ACTIVE_HIGH:
516 return (INTR_POLARITY_HIGH);
517 case ACPI_MADT_POLARITY_ACTIVE_LOW:
518 return (INTR_POLARITY_LOW);
520 panic("Bogus Interrupt Polarity");
524 static enum intr_trigger
525 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
528 switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
529 case ACPI_MADT_TRIGGER_CONFORMS:
530 if (Source == AcpiGbl_FADT.SciInterrupt)
531 return (INTR_TRIGGER_LEVEL);
533 return (INTR_TRIGGER_EDGE);
534 case ACPI_MADT_TRIGGER_EDGE:
535 return (INTR_TRIGGER_EDGE);
536 case ACPI_MADT_TRIGGER_LEVEL:
537 return (INTR_TRIGGER_LEVEL);
539 panic("Bogus Interrupt Trigger Mode");
544 * Find the local APIC ID associated with a given ACPI Processor ID.
547 madt_find_cpu(u_int acpi_id, u_int *apic_id)
551 for (i = 0; i <= MAX_APIC_ID; i++) {
552 if (!lapics[i].la_enabled)
554 if (lapics[i].la_acpi_id != acpi_id)
563 * Find the IO APIC and pin on that APIC associated with a given global
567 madt_find_interrupt(int intr, void **apic, u_int *pin)
572 for (i = 0; i <= MAX_APIC_ID; i++) {
573 if (ioapics[i].io_apic == NULL ||
574 ioapics[i].io_vector > intr)
577 ioapics[best].io_vector < ioapics[i].io_vector)
582 *apic = ioapics[best].io_apic;
583 *pin = intr - ioapics[best].io_vector;
585 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
591 * Parse an interrupt source override for an ISA interrupt.
594 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
596 void *new_ioapic, *old_ioapic;
597 u_int new_pin, old_pin;
598 enum intr_trigger trig;
599 enum intr_polarity pol;
602 if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
603 intr->GlobalIrq == 2) {
605 printf("MADT: Skipping timer override\n");
609 printf("MADT: Interrupt override: source %u, irq %u\n",
610 intr->SourceIrq, intr->GlobalIrq);
611 KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
612 if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
613 printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
614 intr->GlobalIrq, intr->SourceIrq);
619 * Lookup the appropriate trigger and polarity modes for this
622 trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
623 pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
626 * If the SCI is identity mapped but has edge trigger and
627 * active-hi polarity or the force_sci_lo tunable is set,
628 * force it to use level/lo.
630 if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
631 madt_found_sci_override = 1;
632 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
633 if (tolower(buf[0]) == 'e')
634 trig = INTR_TRIGGER_EDGE;
635 else if (tolower(buf[0]) == 'l')
636 trig = INTR_TRIGGER_LEVEL;
639 "Invalid trigger %s: must be 'edge' or 'level'",
641 printf("MADT: Forcing SCI to %s trigger\n",
642 trig == INTR_TRIGGER_EDGE ? "edge" : "level");
644 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
645 if (tolower(buf[0]) == 'h')
646 pol = INTR_POLARITY_HIGH;
647 else if (tolower(buf[0]) == 'l')
648 pol = INTR_POLARITY_LOW;
651 "Invalid polarity %s: must be 'high' or 'low'",
653 printf("MADT: Forcing SCI to active %s polarity\n",
654 pol == INTR_POLARITY_HIGH ? "high" : "low");
658 /* Remap the IRQ if it is mapped to a different interrupt vector. */
659 if (intr->SourceIrq != intr->GlobalIrq) {
661 * If the SCI is remapped to a non-ISA global interrupt,
662 * then override the vector we use to setup and allocate
665 if (intr->GlobalIrq > 15 &&
666 intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
667 acpi_OverrideInterruptLevel(intr->GlobalIrq);
669 ioapic_remap_vector(new_ioapic, new_pin,
671 if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
673 printf("MADT: Could not find APIC for source IRQ %u\n",
675 else if (ioapic_get_vector(old_ioapic, old_pin) ==
677 ioapic_disable_pin(old_ioapic, old_pin);
680 /* Program the polarity and trigger mode. */
681 ioapic_set_triggermode(new_ioapic, new_pin, trig);
682 ioapic_set_polarity(new_ioapic, new_pin, pol);
686 * Parse an entry for an NMI routed to an IO APIC.
689 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
694 if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
695 printf("MADT: Could not find APIC for vector %u\n",
700 ioapic_set_nmi(ioapic, pin);
701 if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
702 ioapic_set_triggermode(ioapic, pin,
703 interrupt_trigger(nmi->IntiFlags, 0));
704 if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
705 ioapic_set_polarity(ioapic, pin,
706 interrupt_polarity(nmi->IntiFlags, 0));
710 * Parse an entry for an NMI routed to a local APIC LVT pin.
713 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
717 if (nmi->ProcessorId == 0xff)
718 apic_id = APIC_ID_ALL;
719 else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
721 printf("MADT: Ignoring local NMI routed to "
722 "ACPI CPU %u\n", nmi->ProcessorId);
729 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
730 if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
731 lapic_set_lvt_triggermode(apic_id, pin,
732 interrupt_trigger(nmi->IntiFlags, 0));
733 if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
734 lapic_set_lvt_polarity(apic_id, pin,
735 interrupt_polarity(nmi->IntiFlags, 0));
739 * Parse interrupt entries.
742 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
745 switch (entry->Type) {
746 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
747 madt_parse_interrupt_override(
748 (ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
750 case ACPI_MADT_TYPE_NMI_SOURCE:
751 madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
753 case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
754 madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
760 * Setup per-CPU ACPI IDs.
763 madt_set_ids(void *dummy)
765 struct lapic_info *la;
771 for (i = 0; i < MAXCPU; i++) {
775 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
776 la = &lapics[pc->pc_apic_id];
778 panic("APIC: CPU with APIC ID %u is not enabled",
780 pc->pc_acpi_id = la->la_acpi_id;
782 printf("APIC: CPU %u has ACPI ID %u\n", i,
786 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_ANY, madt_set_ids, NULL);