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
29 * $FreeBSD: src/sys/i386/acpica/madt.c,v 1.10 2004/01/26 19:34:24 jhb Exp $
30 * $DragonFly: src/sys/i386/acpica5/Attic/madt.c,v 1.1 2004/02/21 06:48:05 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/globaldata.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/md_var.h>
48 #include <machine/apicvar.h>
49 #include <machine/specialreg.h>
50 #include <machine/smp.h>
51 #include <machine/globaldata.h>
55 #include <bus/pci/pcivar.h>
57 #define NIOAPICS 32 /* Max number of I/O APICs */
58 #define NLAPICS 32 /* Max number of local APICs */
65 typedef void madt_entry_handler(APIC_HEADER *entry, void *arg);
67 /* These two arrays are indexed by APIC IDs. */
77 } lapics[NLAPICS + 1];
79 static MULTIPLE_APIC_TABLE *madt;
80 static vm_paddr_t madt_physaddr;
81 static vm_offset_t madt_length;
83 MALLOC_DEFINE(M_MADT, "MADT Table", "ACPI MADT Table Items");
85 static u_char interrupt_polarity(UINT16 Polarity);
86 static u_char interrupt_trigger(UINT16 TriggerMode);
87 static int madt_find_cpu(u_int acpi_id, u_int *apic_id);
88 static int madt_find_interrupt(int intr, void **apic, u_int *pin);
89 static void *madt_map(vm_paddr_t pa, int offset, vm_offset_t length);
90 static void *madt_map_table(vm_paddr_t pa, int offset, const char *sig);
91 static void madt_parse_apics(APIC_HEADER *entry, void *arg);
92 static void madt_parse_interrupt_override(MADT_INTERRUPT_OVERRIDE *intr);
93 static void madt_parse_ints(APIC_HEADER *entry, void *arg __unused);
94 static void madt_parse_local_nmi(MADT_LOCAL_APIC_NMI *nmi);
95 static void madt_parse_nmi(MADT_NMI_SOURCE *nmi);
96 static int madt_probe(void);
97 static int madt_probe_cpus(void);
98 static void madt_probe_cpus_handler(APIC_HEADER *entry, void *arg __unused);
99 static int madt_probe_table(vm_paddr_t address);
100 static void madt_register(void *dummy);
101 static int madt_setup_local(void);
102 static int madt_setup_io(void);
103 static void madt_unmap(void *data, vm_offset_t length);
104 static void madt_unmap_table(void *table);
105 static void madt_walk_table(madt_entry_handler *handler, void *arg);
107 static struct apic_enumerator madt_enumerator = {
116 * Code to abuse the crashdump map to map in the tables for the early
117 * probe. We cheat and make the following assumptions about how we
118 * use this KVA: page 0 is used to map in the first page of each table
119 * found via the RSDT or XSDT and pages 1 to n are used to map in the
120 * RSDT or XSDT. The offset is in pages; the length is in bytes.
123 madt_map(vm_paddr_t pa, int offset, vm_offset_t length)
128 off = pa & PAGE_MASK;
129 length = roundup(length + off, PAGE_SIZE);
131 va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
132 (offset * PAGE_SIZE);
133 data = (void *)(va + off);
140 cpu_invlpg((void *)va);
146 madt_unmap(void *data, vm_offset_t length)
150 va = (vm_offset_t)data;
151 off = va & PAGE_MASK;
152 length = roundup(length + off, PAGE_SIZE);
156 cpu_invlpg((void *)va);
163 madt_map_table(vm_paddr_t pa, int offset, const char *sig)
165 ACPI_TABLE_HEADER *header;
168 header = madt_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
169 if (strncmp(header->Signature, sig, 4) != 0) {
170 madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
173 length = header->Length;
174 madt_unmap(header, sizeof(ACPI_TABLE_HEADER));
175 return (madt_map(pa, offset, length));
179 madt_unmap_table(void *table)
181 ACPI_TABLE_HEADER *header;
183 header = (ACPI_TABLE_HEADER *)table;
184 madt_unmap(table, header->Length);
188 * Look for an ACPI Multiple APIC Description Table ("APIC")
193 ACPI_POINTER rsdp_ptr;
194 RSDP_DESCRIPTOR *rsdp;
195 RSDT_DESCRIPTOR *rsdt;
196 XSDT_DESCRIPTOR *xsdt;
200 if (resource_disabled("acpi", 0))
205 * Map in the RSDP. Since ACPI uses AcpiOsMapMemory() which in turn
206 * calls pmap_mapdev() to find the RSDP, we assume that we can use
207 * pmap_mapdev() to map the RSDP.
209 if (AcpiOsGetRootPointer(ACPI_LOGICAL_ADDRESSING, &rsdp_ptr) != AE_OK)
212 KASSERT(rsdp_ptr.Pointer.Physical < KERNLOAD, ("RSDP too high"));
214 rsdp = pmap_mapdev(rsdp_ptr.Pointer.Physical, sizeof(RSDP_DESCRIPTOR));
217 printf("MADT: Failed to map RSDP\n");
222 * For ACPI < 2.0, use the RSDT. For ACPI >= 2.0, use the XSDT.
223 * We map the XSDT and RSDT at page 1 in the crashdump area.
224 * Page 0 is used to map in the headers of candidate ACPI tables.
226 if (rsdp->Revision >= 2) {
227 xsdt = madt_map_table(rsdp->XsdtPhysicalAddress, 1, XSDT_SIG);
230 printf("MADT: Failed to map XSDT\n");
233 count = (xsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
235 for (i = 0; i < count; i++)
236 if (madt_probe_table(xsdt->TableOffsetEntry[i]))
238 madt_unmap_table(xsdt);
240 rsdt = madt_map_table(rsdp->RsdtPhysicalAddress, 1, RSDT_SIG);
243 printf("MADT: Failed to map RSDT\n");
246 count = (rsdt->Length - sizeof(ACPI_TABLE_HEADER)) /
248 for (i = 0; i < count; i++)
249 if (madt_probe_table(rsdt->TableOffsetEntry[i]))
251 madt_unmap_table(rsdt);
253 pmap_unmapdev((vm_offset_t)rsdp, sizeof(RSDP_DESCRIPTOR));
254 if (madt_physaddr == 0) {
256 printf("MADT: No MADT table found\n");
260 printf("MADT: Found table at 0x%jx\n",
261 (uintmax_t)madt_physaddr);
267 * See if a given ACPI table is the MADT.
270 madt_probe_table(vm_paddr_t address)
272 ACPI_TABLE_HEADER *table;
274 table = madt_map(address, 0, sizeof(ACPI_TABLE_HEADER));
277 printf("MADT: Failed to map table at 0x%jx\n",
282 printf("Table '%.4s' at 0x%jx\n", table->Signature,
285 /* XXX: Verify checksum? */
286 if (strncmp(table->Signature, APIC_SIG, 4) != 0) {
287 madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
290 madt_physaddr = address;
291 madt_length = table->Length;
292 madt_unmap(table, sizeof(ACPI_TABLE_HEADER));
297 * Run through the MP table enumerating CPUs.
300 madt_probe_cpus(void)
303 madt = madt_map_table(madt_physaddr, 0, APIC_SIG);
304 KASSERT(madt != NULL, ("Unable to re-map MADT"));
305 madt_walk_table(madt_probe_cpus_handler, NULL);
306 madt_unmap_table(madt);
312 * Initialize the local APIC on the BSP.
315 madt_setup_local(void)
318 madt = pmap_mapdev(madt_physaddr, madt_length);
319 lapic_init((uintptr_t)madt->LocalApicAddress);
320 printf("ACPI APIC Table: <%.*s %.*s>\n",
321 (int)sizeof(madt->OemId), madt->OemId,
322 (int)sizeof(madt->OemTableId), madt->OemTableId);
325 * We ignore 64-bit local APIC override entries. Should we
326 * perhaps emit a warning here if we find one?
332 * Enumerate I/O APICs and setup interrupt sources.
339 /* Try to initialize ACPI so that we can access the FADT. */
341 if (ACPI_FAILURE(i)) {
342 printf("MADT: ACPI Startup failed with %s\n",
343 AcpiFormatException(i));
344 printf("Try disabling either ACPI or apic support.\n");
345 panic("Using MADT but ACPI doesn't work");
348 /* First, we run through adding I/O APIC's. */
349 madt_walk_table(madt_parse_apics, NULL);
351 /* Second, we run through the table tweaking interrupt sources. */
352 madt_walk_table(madt_parse_ints, NULL);
354 /* Third, we register all the I/O APIC's. */
355 for (i = 0; i < NIOAPICS; i++)
356 if (ioapics[i].io_apic != NULL)
357 ioapic_register(ioapics[i].io_apic);
359 /* Finally, we throw the switch to enable the I/O APIC's. */
360 acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
366 madt_register(void *dummy __unused)
369 apic_register_enumerator(&madt_enumerator);
371 SYSINIT(madt_register, SI_SUB_CPU - 1, SI_ORDER_FIRST, madt_register, NULL)
374 * Call the handler routine for each entry in the MADT table.
377 madt_walk_table(madt_entry_handler *handler, void *arg)
382 end = (u_char *)(madt) + madt->Length;
383 for (p = (u_char *)(madt + 1); p < end; ) {
384 entry = (APIC_HEADER *)p;
391 madt_probe_cpus_handler(APIC_HEADER *entry, void *arg)
393 MADT_PROCESSOR_APIC *proc;
394 struct lapic_info *la;
396 switch (entry->Type) {
399 * The MADT does not include a BSP flag, so we have to
400 * let the MP code figure out which CPU is the BSP on
403 proc = (MADT_PROCESSOR_APIC *)entry;
405 printf("MADT: Found CPU APIC ID %d ACPI ID %d: %s\n",
406 proc->LocalApicId, proc->ProcessorId,
407 proc->ProcessorEnabled ? "enabled" : "disabled");
408 if (proc->ProcessorId > NLAPICS)
409 panic("%s: CPU ID %d too high", __func__,
411 la = &lapics[proc->ProcessorId];
412 KASSERT(la->la_present == 0,
413 ("Duplicate local ACPI ID %d", proc->ProcessorId));
415 la->la_apic_id = proc->LocalApicId;
416 if (proc->ProcessorEnabled) {
418 lapic_create(proc->LocalApicId, 0);
426 * Add an I/O APIC from an entry in the table.
429 madt_parse_apics(APIC_HEADER *entry, void *arg __unused)
433 switch (entry->Type) {
435 apic = (MADT_IO_APIC *)entry;
437 printf("MADT: Found IO APIC ID %d, Interrupt %d at %p\n",
438 apic->IoApicId, apic->Interrupt,
439 (void *)(uintptr_t)apic->Address);
440 if (apic->IoApicId >= NIOAPICS)
441 panic("%s: I/O APIC ID %d too high", __func__,
443 if (ioapics[apic->IoApicId].io_apic != NULL)
444 panic("%s: Double APIC ID %d", __func__,
446 ioapics[apic->IoApicId].io_apic = ioapic_create(
447 (uintptr_t)apic->Address, apic->IoApicId,
449 ioapics[apic->IoApicId].io_vector = apic->Interrupt;
457 * Determine properties of an interrupt source. Note that for ACPI,
458 * these are only used for ISA interrupts, so we assume ISA bus values
459 * (Active Hi, Edge Triggered) for conforming values.
462 interrupt_polarity(UINT16 Polarity)
466 case POLARITY_CONFORMS:
467 case POLARITY_ACTIVE_HIGH:
469 case POLARITY_ACTIVE_LOW:
472 panic("Bogus Interrupt Polarity");
477 interrupt_trigger(UINT16 TriggerMode)
480 switch (TriggerMode) {
481 case TRIGGER_CONFORMS:
487 panic("Bogus Interrupt Trigger Mode");
492 * Find the local APIC ID associated with a given ACPI Processor ID.
495 madt_find_cpu(u_int acpi_id, u_int *apic_id)
498 if (!lapics[acpi_id].la_present)
500 *apic_id = lapics[acpi_id].la_apic_id;
501 if (lapics[acpi_id].la_enabled)
508 * Find the IO APIC and pin on that APIC associated with a given global
512 madt_find_interrupt(int intr, void **apic, u_int *pin)
517 for (i = 0; i < NIOAPICS; i++) {
518 if (ioapics[i].io_apic == NULL ||
519 ioapics[i].io_vector > intr)
522 ioapics[best].io_vector < ioapics[i].io_vector)
527 *apic = ioapics[best].io_apic;
528 *pin = intr - ioapics[best].io_vector;
530 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
536 * Parse an interrupt source override for an ISA interrupt.
539 madt_parse_interrupt_override(MADT_INTERRUPT_OVERRIDE *intr)
541 void *new_ioapic, *old_ioapic;
542 u_int new_pin, old_pin;
546 printf("MADT: intr override: source %u, irq %u\n",
547 intr->Source, intr->Interrupt);
548 KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
549 if (madt_find_interrupt(intr->Interrupt, &new_ioapic,
551 printf("MADT: Could not find APIC for vector %d (IRQ %d)\n",
552 intr->Interrupt, intr->Source);
557 * If the SCI is remapped to a non-ISA global interrupt,
558 * force it to level trigger and active-lo polarity.
559 * If the SCI is identity mapped but has edge trigger and
560 * active-hi polarity, also force it to use level/lo.
563 if (intr->Source == AcpiGbl_FADT->SciInt)
564 if (intr->Interrupt > 15 || (intr->Interrupt == intr->Source &&
565 intr->TriggerMode == TRIGGER_EDGE &&
566 intr->Polarity == POLARITY_ACTIVE_HIGH))
569 if (intr->Source != intr->Interrupt) {
571 * If the SCI is remapped to a non-ISA global interrupt,
572 * then override the vector we use to setup and allocate
575 if (intr->Interrupt > 15 &&
576 intr->Source == AcpiGbl_FADT->SciInt)
577 acpi_OverrideInterruptLevel(intr->Interrupt);
579 ioapic_remap_vector(new_ioapic, new_pin, intr->Source);
580 if (madt_find_interrupt(intr->Source, &old_ioapic,
582 printf("MADT: Could not find APIC for source IRQ %d\n",
584 else if (ioapic_get_vector(old_ioapic, old_pin) ==
586 ioapic_disable_pin(old_ioapic, old_pin);
590 "MADT: Forcing active-lo polarity and level trigger for IRQ %d\n",
592 ioapic_set_polarity(new_ioapic, new_pin, 0);
593 ioapic_set_triggermode(new_ioapic, new_pin, 0);
595 ioapic_set_polarity(new_ioapic, new_pin,
596 interrupt_polarity(intr->Polarity));
597 ioapic_set_triggermode(new_ioapic, new_pin,
598 interrupt_trigger(intr->TriggerMode));
603 * Parse an entry for an NMI routed to an IO APIC.
606 madt_parse_nmi(MADT_NMI_SOURCE *nmi)
611 if (madt_find_interrupt(nmi->Interrupt, &ioapic, &pin) != 0) {
612 printf("MADT: Could not find APIC for vector %d\n",
617 ioapic_set_nmi(ioapic, pin);
618 if (nmi->TriggerMode != TRIGGER_CONFORMS)
619 ioapic_set_triggermode(ioapic, pin,
620 interrupt_trigger(nmi->TriggerMode));
621 if (nmi->Polarity != TRIGGER_CONFORMS)
622 ioapic_set_polarity(ioapic, pin,
623 interrupt_polarity(nmi->Polarity));
627 * Parse an entry for an NMI routed to a local APIC LVT pin.
630 madt_parse_local_nmi(MADT_LOCAL_APIC_NMI *nmi)
634 if (nmi->ProcessorId == 0xff)
635 apic_id = APIC_ID_ALL;
636 else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
638 printf("MADT: Ignoring local NMI routed to ACPI CPU %u\n",
646 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
647 if (nmi->TriggerMode != TRIGGER_CONFORMS)
648 lapic_set_lvt_triggermode(apic_id, pin,
649 interrupt_trigger(nmi->TriggerMode));
650 if (nmi->Polarity != POLARITY_CONFORMS)
651 lapic_set_lvt_polarity(apic_id, pin,
652 interrupt_polarity(nmi->Polarity));
656 * Parse interrupt entries.
659 madt_parse_ints(APIC_HEADER *entry, void *arg __unused)
662 switch (entry->Type) {
663 case APIC_XRUPT_OVERRIDE:
664 madt_parse_interrupt_override(
665 (MADT_INTERRUPT_OVERRIDE *)entry);
668 madt_parse_nmi((MADT_NMI_SOURCE *)entry);
671 madt_parse_local_nmi((MADT_LOCAL_APIC_NMI *)entry);
677 * Setup per-CPU ACPI IDs.
680 madt_set_ids(void *dummy)
682 struct mdglobaldata *md;
687 for (i = 0; i < ncpus; i++) {
688 if ((smp_active_mask & (1 << i)) == 0)
690 md = (struct mdglobaldata *)globaldata_find(i);
691 KKASSERT(md != NULL);
692 for (j = 0; j < NLAPICS + 1; j++) {
693 if (!lapics[j].la_present || !lapics[j].la_enabled)
695 if (lapics[j].la_apic_id == md->gd_apic_id) {
698 printf("APIC: CPU %u has ACPI ID %u\n",
703 if (j == NLAPICS + 1)
704 panic("Unable to find ACPI ID for CPU %d", i);
707 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_ANY, madt_set_ids, NULL)