2 * Copyright (c) 1998 Doug Rabson
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/endian.h>
46 #define BEGIN_COMMENT "/*\n"
47 #define END_COMMENT " */\n"
49 static void acpi_print_string(char *s, size_t length);
50 static void acpi_print_gas(ACPI_GENERIC_ADDRESS *gas);
51 static int acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt);
52 static void acpi_handle_fadt(ACPI_TABLE_HEADER *fadt);
53 static void acpi_print_cpu(u_char cpu_id);
54 static void acpi_print_cpu_uid(uint32_t uid, char *uid_string);
55 static void acpi_print_local_apic(uint32_t apic_id, uint32_t flags);
56 static void acpi_print_io_apic(uint32_t apic_id, uint32_t int_base,
58 static void acpi_print_mps_flags(uint16_t flags);
59 static void acpi_print_intr(uint32_t intr, uint16_t mps_flags);
60 static void acpi_print_local_nmi(u_int lint, uint16_t mps_flags);
61 static void acpi_print_madt(ACPI_SUBTABLE_HEADER *mp);
62 static void acpi_handle_madt(ACPI_TABLE_HEADER *sdp);
63 static void acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp);
64 static void acpi_handle_hpet(ACPI_TABLE_HEADER *sdp);
65 static void acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp);
66 static void acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
68 static void acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp);
69 static void acpi_print_srat(ACPI_SUBTABLE_HEADER *srat);
70 static void acpi_handle_srat(ACPI_TABLE_HEADER *sdp);
71 static void acpi_print_sdt(ACPI_TABLE_HEADER *sdp);
72 static void acpi_print_fadt(ACPI_TABLE_HEADER *sdp);
73 static void acpi_print_facs(ACPI_TABLE_FACS *facs);
74 static void acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp);
75 static ACPI_TABLE_HEADER *acpi_map_sdt(vm_offset_t pa);
76 static void acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp);
77 static void acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp);
78 static void acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
79 void (*action)(ACPI_SUBTABLE_HEADER *));
81 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
85 acpi_print_string(char *s, size_t length)
89 /* Trim trailing spaces and NULLs */
90 while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
100 acpi_print_gas(ACPI_GENERIC_ADDRESS *gas)
102 switch(gas->SpaceId) {
103 case ACPI_GAS_MEMORY:
104 printf("0x%08lx:%u[%u] (Memory)", (u_long)gas->Address,
105 gas->BitOffset, gas->BitWidth);
108 printf("0x%02lx:%u[%u] (IO)", (u_long)gas->Address,
109 gas->BitOffset, gas->BitWidth);
112 printf("%x:%x+0x%x (PCI)", (uint16_t)(gas->Address >> 32),
113 (uint16_t)((gas->Address >> 16) & 0xffff),
114 (uint16_t)gas->Address);
116 /* XXX How to handle these below? */
117 case ACPI_GAS_EMBEDDED:
118 printf("0x%x:%u[%u] (EC)", (uint16_t)gas->Address,
119 gas->BitOffset, gas->BitWidth);
122 printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->Address,
123 gas->BitOffset, gas->BitWidth);
126 case ACPI_GAS_PCIBAR:
127 case ACPI_GAS_DATATABLE:
130 printf("0x%08lx (?)", (u_long)gas->Address);
135 /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
137 acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt)
141 /* Set the FADT revision separately from the RSDP version. */
142 if (addr_size == 8) {
146 * A few systems (e.g., IBM T23) have an RSDP that claims
147 * revision 2 but the 64 bit addresses are invalid. If
148 * revision 2 and the 32 bit address is non-zero but the
149 * 32 and 64 bit versions don't match, prefer the 32 bit
150 * version for all subsequent tables.
152 if (fadt->Facs != 0 &&
153 (fadt->XFacs & 0xffffffff) != fadt->Facs)
157 return (fadt_revision);
161 acpi_handle_fadt(ACPI_TABLE_HEADER *sdp)
163 ACPI_TABLE_HEADER *dsdp;
164 ACPI_TABLE_FACS *facs;
165 ACPI_TABLE_FADT *fadt;
168 fadt = (ACPI_TABLE_FADT *)sdp;
169 acpi_print_fadt(sdp);
171 fadt_revision = acpi_get_fadt_revision(fadt);
172 if (fadt_revision == 1)
173 facs = (ACPI_TABLE_FACS *)acpi_map_sdt(fadt->Facs);
175 facs = (ACPI_TABLE_FACS *)acpi_map_sdt(fadt->XFacs);
176 if (memcmp(facs->Signature, ACPI_SIG_FACS, 4) != 0 || facs->Length < 64)
177 errx(1, "FACS is corrupt");
178 acpi_print_facs(facs);
180 if (fadt_revision == 1)
181 dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
183 dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
184 if (acpi_checksum(dsdp, dsdp->Length))
185 errx(1, "DSDT is corrupt");
186 acpi_print_dsdt(dsdp);
190 acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
191 void (*action)(ACPI_SUBTABLE_HEADER *))
193 ACPI_SUBTABLE_HEADER *subtable;
197 end = (char *)table + table->Length;
198 while ((char *)subtable < end) {
201 subtable = (ACPI_SUBTABLE_HEADER *)((char *)subtable +
207 acpi_print_cpu(u_char cpu_id)
210 printf("\tACPI CPU=");
214 printf("%d\n", (u_int)cpu_id);
218 acpi_print_cpu_uid(uint32_t uid, char *uid_string)
221 printf("\tUID=%d", uid);
222 if (uid_string != NULL)
223 printf(" (%s)", uid_string);
228 acpi_print_local_apic(uint32_t apic_id, uint32_t flags)
232 if (flags & ACPI_MADT_ENABLED)
237 printf("\tAPIC ID=%d\n", apic_id);
241 acpi_print_io_apic(uint32_t apic_id, uint32_t int_base, uint64_t apic_addr)
244 printf("\tAPIC ID=%d\n", apic_id);
245 printf("\tINT BASE=%d\n", int_base);
246 printf("\tADDR=0x%016jx\n", (uintmax_t)apic_addr);
250 acpi_print_mps_flags(uint16_t flags)
253 printf("\tFlags={Polarity=");
254 switch (flags & ACPI_MADT_POLARITY_MASK) {
255 case ACPI_MADT_POLARITY_CONFORMS:
256 printf("conforming");
258 case ACPI_MADT_POLARITY_ACTIVE_HIGH:
261 case ACPI_MADT_POLARITY_ACTIVE_LOW:
265 printf("0x%x", flags & ACPI_MADT_POLARITY_MASK);
268 printf(", Trigger=");
269 switch (flags & ACPI_MADT_TRIGGER_MASK) {
270 case ACPI_MADT_TRIGGER_CONFORMS:
271 printf("conforming");
273 case ACPI_MADT_TRIGGER_EDGE:
276 case ACPI_MADT_TRIGGER_LEVEL:
280 printf("0x%x", (flags & ACPI_MADT_TRIGGER_MASK) >> 2);
286 acpi_print_intr(uint32_t intr, uint16_t mps_flags)
289 printf("\tINTR=%d\n", intr);
290 acpi_print_mps_flags(mps_flags);
294 acpi_print_local_nmi(u_int lint, uint16_t mps_flags)
297 printf("\tLINT Pin=%d\n", lint);
298 acpi_print_mps_flags(mps_flags);
301 const char *apic_types[] = { "Local APIC", "IO APIC", "INT Override", "NMI",
302 "Local APIC NMI", "Local APIC Override",
303 "IO SAPIC", "Local SAPIC", "Platform Interrupt",
304 "Local X2APIC", "Local X2APIC NMI" };
305 const char *platform_int_types[] = { "0 (unknown)", "PMI", "INIT",
306 "Corrected Platform Error" };
309 acpi_print_madt(ACPI_SUBTABLE_HEADER *mp)
311 ACPI_MADT_LOCAL_APIC *lapic;
312 ACPI_MADT_IO_APIC *ioapic;
313 ACPI_MADT_INTERRUPT_OVERRIDE *over;
314 ACPI_MADT_NMI_SOURCE *nmi;
315 ACPI_MADT_LOCAL_APIC_NMI *lapic_nmi;
316 ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_over;
317 ACPI_MADT_IO_SAPIC *iosapic;
318 ACPI_MADT_LOCAL_SAPIC *lsapic;
319 ACPI_MADT_INTERRUPT_SOURCE *isrc;
320 ACPI_MADT_LOCAL_X2APIC *x2apic;
321 ACPI_MADT_LOCAL_X2APIC_NMI *x2apic_nmi;
323 if (mp->Type < sizeof(apic_types) / sizeof(apic_types[0]))
324 printf("\tType=%s\n", apic_types[mp->Type]);
326 printf("\tType=%d (unknown)\n", mp->Type);
328 case ACPI_MADT_TYPE_LOCAL_APIC:
329 lapic = (ACPI_MADT_LOCAL_APIC *)mp;
330 acpi_print_cpu(lapic->ProcessorId);
331 acpi_print_local_apic(lapic->Id, lapic->LapicFlags);
333 case ACPI_MADT_TYPE_IO_APIC:
334 ioapic = (ACPI_MADT_IO_APIC *)mp;
335 acpi_print_io_apic(ioapic->Id, ioapic->GlobalIrqBase,
338 case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
339 over = (ACPI_MADT_INTERRUPT_OVERRIDE *)mp;
340 printf("\tBUS=%d\n", (u_int)over->Bus);
341 printf("\tIRQ=%d\n", (u_int)over->SourceIrq);
342 acpi_print_intr(over->GlobalIrq, over->IntiFlags);
344 case ACPI_MADT_TYPE_NMI_SOURCE:
345 nmi = (ACPI_MADT_NMI_SOURCE *)mp;
346 acpi_print_intr(nmi->GlobalIrq, nmi->IntiFlags);
348 case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
349 lapic_nmi = (ACPI_MADT_LOCAL_APIC_NMI *)mp;
350 acpi_print_cpu(lapic_nmi->ProcessorId);
351 acpi_print_local_nmi(lapic_nmi->Lint, lapic_nmi->IntiFlags);
353 case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
354 lapic_over = (ACPI_MADT_LOCAL_APIC_OVERRIDE *)mp;
355 printf("\tLocal APIC ADDR=0x%016jx\n",
356 (uintmax_t)lapic_over->Address);
358 case ACPI_MADT_TYPE_IO_SAPIC:
359 iosapic = (ACPI_MADT_IO_SAPIC *)mp;
360 acpi_print_io_apic(iosapic->Id, iosapic->GlobalIrqBase,
363 case ACPI_MADT_TYPE_LOCAL_SAPIC:
364 lsapic = (ACPI_MADT_LOCAL_SAPIC *)mp;
365 acpi_print_cpu(lsapic->ProcessorId);
366 acpi_print_local_apic(lsapic->Id, lsapic->LapicFlags);
367 printf("\tAPIC EID=%d\n", (u_int)lsapic->Eid);
368 if (mp->Length > __offsetof(ACPI_MADT_LOCAL_SAPIC, Uid))
369 acpi_print_cpu_uid(lsapic->Uid, lsapic->UidString);
371 case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
372 isrc = (ACPI_MADT_INTERRUPT_SOURCE *)mp;
373 if (isrc->Type < sizeof(platform_int_types) /
374 sizeof(platform_int_types[0]))
375 printf("\tType=%s\n", platform_int_types[isrc->Type]);
377 printf("\tType=%d (unknown)\n", isrc->Type);
378 printf("\tAPIC ID=%d\n", (u_int)isrc->Id);
379 printf("\tAPIC EID=%d\n", (u_int)isrc->Eid);
380 printf("\tSAPIC Vector=%d\n", (u_int)isrc->IoSapicVector);
381 acpi_print_intr(isrc->GlobalIrq, isrc->IntiFlags);
383 case ACPI_MADT_TYPE_LOCAL_X2APIC:
384 x2apic = (ACPI_MADT_LOCAL_X2APIC *)mp;
385 acpi_print_cpu_uid(x2apic->Uid, NULL);
386 acpi_print_local_apic(x2apic->LocalApicId, x2apic->LapicFlags);
388 case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
389 x2apic_nmi = (ACPI_MADT_LOCAL_X2APIC_NMI *)mp;
390 acpi_print_cpu_uid(x2apic_nmi->Uid, NULL);
391 acpi_print_local_nmi(x2apic_nmi->Lint, x2apic_nmi->IntiFlags);
397 acpi_handle_madt(ACPI_TABLE_HEADER *sdp)
399 ACPI_TABLE_MADT *madt;
401 printf(BEGIN_COMMENT);
403 madt = (ACPI_TABLE_MADT *)sdp;
404 printf("\tLocal APIC ADDR=0x%08x\n", madt->Address);
406 if (madt->Flags & ACPI_MADT_PCAT_COMPAT)
409 acpi_walk_subtables(sdp, (madt + 1), acpi_print_madt);
414 acpi_handle_hpet(ACPI_TABLE_HEADER *sdp)
416 ACPI_TABLE_HPET *hpet;
418 printf(BEGIN_COMMENT);
420 hpet = (ACPI_TABLE_HPET *)sdp;
421 printf("\tHPET Number=%d\n", hpet->Sequence);
423 acpi_print_gas(&hpet->Address);
424 printf("\tHW Rev=0x%x\n", hpet->Id & ACPI_HPET_ID_HARDWARE_REV_ID);
425 printf("\tComparators=%d\n", (hpet->Id & ACPI_HPET_ID_COMPARATORS) >>
427 printf("\tCounter Size=%d\n", hpet->Id & ACPI_HPET_ID_COUNT_SIZE_CAP ?
429 printf("\tLegacy IRQ routing capable={");
430 if (hpet->Id & ACPI_HPET_ID_LEGACY_CAPABLE)
434 printf("\tPCI Vendor ID=0x%04x\n", hpet->Id >> 16);
435 printf("\tMinimal Tick=%d\n", hpet->MinimumTick);
440 acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp)
442 ACPI_TABLE_ECDT *ecdt;
444 printf(BEGIN_COMMENT);
446 ecdt = (ACPI_TABLE_ECDT *)sdp;
447 printf("\tEC_CONTROL=");
448 acpi_print_gas(&ecdt->Control);
449 printf("\n\tEC_DATA=");
450 acpi_print_gas(&ecdt->Data);
451 printf("\n\tUID=%#x, ", ecdt->Uid);
452 printf("GPE_BIT=%#x\n", ecdt->Gpe);
453 printf("\tEC_ID=%s\n", ecdt->Id);
458 acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp)
460 ACPI_TABLE_MCFG *mcfg;
461 ACPI_MCFG_ALLOCATION *alloc;
464 printf(BEGIN_COMMENT);
466 mcfg = (ACPI_TABLE_MCFG *)sdp;
467 entries = (sdp->Length - sizeof(ACPI_TABLE_MCFG)) /
468 sizeof(ACPI_MCFG_ALLOCATION);
469 alloc = (ACPI_MCFG_ALLOCATION *)(mcfg + 1);
470 for (i = 0; i < entries; i++, alloc++) {
472 printf("\tBase Address=0x%016jx\n", alloc->Address);
473 printf("\tSegment Group=0x%04x\n", alloc->PciSegment);
474 printf("\tStart Bus=%d\n", alloc->StartBusNumber);
475 printf("\tEnd Bus=%d\n", alloc->EndBusNumber);
481 acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
486 if (flags & ACPI_SRAT_CPU_ENABLED)
491 printf("\tAPIC ID=%d\n", apic_id);
492 printf("\tProximity Domain=%d\n", proximity_domain);
496 acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp)
500 if (mp->Flags & ACPI_SRAT_MEM_ENABLED)
504 if (mp->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
505 printf(",HOT_PLUGGABLE");
506 if (mp->Flags & ACPI_SRAT_MEM_NON_VOLATILE)
507 printf(",NON_VOLATILE");
509 printf("\tBase Address=0x%016jx\n", (uintmax_t)mp->BaseAddress);
510 printf("\tLength=0x%016jx\n", (uintmax_t)mp->Length);
511 printf("\tProximity Domain=%d\n", mp->ProximityDomain);
514 const char *srat_types[] = { "CPU", "Memory", "X2APIC" };
517 acpi_print_srat(ACPI_SUBTABLE_HEADER *srat)
519 ACPI_SRAT_CPU_AFFINITY *cpu;
520 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
522 if (srat->Type < sizeof(srat_types) / sizeof(srat_types[0]))
523 printf("\tType=%s\n", srat_types[srat->Type]);
525 printf("\tType=%d (unknown)\n", srat->Type);
526 switch (srat->Type) {
527 case ACPI_SRAT_TYPE_CPU_AFFINITY:
528 cpu = (ACPI_SRAT_CPU_AFFINITY *)srat;
529 acpi_print_srat_cpu(cpu->ApicId,
530 cpu->ProximityDomainHi[2] << 24 |
531 cpu->ProximityDomainHi[1] << 16 |
532 cpu->ProximityDomainHi[0] << 0 |
533 cpu->ProximityDomainLo, cpu->Flags);
535 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
536 acpi_print_srat_memory((ACPI_SRAT_MEM_AFFINITY *)srat);
538 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
539 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)srat;
540 acpi_print_srat_cpu(x2apic->ApicId, x2apic->ProximityDomain,
547 acpi_handle_srat(ACPI_TABLE_HEADER *sdp)
549 ACPI_TABLE_SRAT *srat;
551 printf(BEGIN_COMMENT);
553 srat = (ACPI_TABLE_SRAT *)sdp;
554 printf("\tTable Revision=%d\n", srat->TableRevision);
555 acpi_walk_subtables(sdp, (srat + 1), acpi_print_srat);
560 acpi_print_sdt(ACPI_TABLE_HEADER *sdp)
563 acpi_print_string(sdp->Signature, ACPI_NAME_SIZE);
564 printf(": Length=%d, Revision=%d, Checksum=%d,\n",
565 sdp->Length, sdp->Revision, sdp->Checksum);
567 acpi_print_string(sdp->OemId, ACPI_OEM_ID_SIZE);
568 printf(", OEM Table ID=");
569 acpi_print_string(sdp->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
570 printf(", OEM Revision=0x%x,\n", sdp->OemRevision);
571 printf("\tCreator ID=");
572 acpi_print_string(sdp->AslCompilerId, ACPI_NAME_SIZE);
573 printf(", Creator Revision=0x%x\n", sdp->AslCompilerRevision);
577 acpi_print_rsdt(ACPI_TABLE_HEADER *rsdp)
579 ACPI_TABLE_RSDT *rsdt;
580 ACPI_TABLE_XSDT *xsdt;
584 rsdt = (ACPI_TABLE_RSDT *)rsdp;
585 xsdt = (ACPI_TABLE_XSDT *)rsdp;
586 printf(BEGIN_COMMENT);
587 acpi_print_sdt(rsdp);
588 entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
589 printf("\tEntries={ ");
590 for (i = 0; i < entries; i++) {
595 addr = le32toh(rsdt->TableOffsetEntry[i]);
598 addr = le64toh(xsdt->TableOffsetEntry[i]);
604 printf("0x%08lx", addr);
610 static const char *acpi_pm_profiles[] = {
611 "Unspecified", "Desktop", "Mobile", "Workstation",
612 "Enterprise Server", "SOHO Server", "Appliance PC"
616 acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
618 ACPI_TABLE_FADT *fadt;
622 fadt = (ACPI_TABLE_FADT *)sdp;
623 printf(BEGIN_COMMENT);
625 printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->Facs,
627 printf("\tINT_MODEL=%s\n", fadt->Model ? "APIC" : "PIC");
628 if (fadt->PreferredProfile >= sizeof(acpi_pm_profiles) / sizeof(char *))
631 pm = acpi_pm_profiles[fadt->PreferredProfile];
632 printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->PreferredProfile);
633 printf("\tSCI_INT=%d\n", fadt->SciInterrupt);
634 printf("\tSMI_CMD=0x%x, ", fadt->SmiCommand);
635 printf("ACPI_ENABLE=0x%x, ", fadt->AcpiEnable);
636 printf("ACPI_DISABLE=0x%x, ", fadt->AcpiDisable);
637 printf("S4BIOS_REQ=0x%x\n", fadt->S4BiosRequest);
638 printf("\tPSTATE_CNT=0x%x\n", fadt->PstateControl);
639 printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
640 fadt->Pm1aEventBlock,
641 fadt->Pm1aEventBlock + fadt->Pm1EventLength - 1);
642 if (fadt->Pm1bEventBlock != 0)
643 printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
644 fadt->Pm1bEventBlock,
645 fadt->Pm1bEventBlock + fadt->Pm1EventLength - 1);
646 printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
647 fadt->Pm1aControlBlock,
648 fadt->Pm1aControlBlock + fadt->Pm1ControlLength - 1);
649 if (fadt->Pm1bControlBlock != 0)
650 printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
651 fadt->Pm1bControlBlock,
652 fadt->Pm1bControlBlock + fadt->Pm1ControlLength - 1);
653 if (fadt->Pm2ControlBlock != 0)
654 printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
655 fadt->Pm2ControlBlock,
656 fadt->Pm2ControlBlock + fadt->Pm2ControlLength - 1);
657 printf("\tPM_TMR_BLK=0x%x-0x%x\n",
659 fadt->PmTimerBlock + fadt->PmTimerLength - 1);
660 if (fadt->Gpe0Block != 0)
661 printf("\tGPE0_BLK=0x%x-0x%x\n",
663 fadt->Gpe0Block + fadt->Gpe0BlockLength - 1);
664 if (fadt->Gpe1Block != 0)
665 printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
667 fadt->Gpe1Block + fadt->Gpe1BlockLength - 1,
669 if (fadt->CstControl != 0)
670 printf("\tCST_CNT=0x%x\n", fadt->CstControl);
671 printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
672 fadt->C2Latency, fadt->C3Latency);
673 printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
674 fadt->FlushSize, fadt->FlushStride);
675 printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
676 fadt->DutyOffset, fadt->DutyWidth);
677 printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
678 fadt->DayAlarm, fadt->MonthAlarm, fadt->Century);
680 #define PRINTFLAG(var, flag) do { \
681 if ((var) & ACPI_FADT_## flag) { \
682 printf("%c%s", sep, #flag); sep = ','; \
686 printf("\tIAPC_BOOT_ARCH=");
688 PRINTFLAG(fadt->BootFlags, LEGACY_DEVICES);
689 PRINTFLAG(fadt->BootFlags, 8042);
690 PRINTFLAG(fadt->BootFlags, NO_VGA);
691 PRINTFLAG(fadt->BootFlags, NO_MSI);
692 PRINTFLAG(fadt->BootFlags, NO_ASPM);
693 if (fadt->BootFlags != 0)
699 PRINTFLAG(fadt->Flags, WBINVD);
700 PRINTFLAG(fadt->Flags, WBINVD_FLUSH);
701 PRINTFLAG(fadt->Flags, C1_SUPPORTED);
702 PRINTFLAG(fadt->Flags, C2_MP_SUPPORTED);
703 PRINTFLAG(fadt->Flags, POWER_BUTTON);
704 PRINTFLAG(fadt->Flags, SLEEP_BUTTON);
705 PRINTFLAG(fadt->Flags, FIXED_RTC);
706 PRINTFLAG(fadt->Flags, S4_RTC_WAKE);
707 PRINTFLAG(fadt->Flags, 32BIT_TIMER);
708 PRINTFLAG(fadt->Flags, DOCKING_SUPPORTED);
709 PRINTFLAG(fadt->Flags, RESET_REGISTER);
710 PRINTFLAG(fadt->Flags, SEALED_CASE);
711 PRINTFLAG(fadt->Flags, HEADLESS);
712 PRINTFLAG(fadt->Flags, SLEEP_TYPE);
713 PRINTFLAG(fadt->Flags, PCI_EXPRESS_WAKE);
714 PRINTFLAG(fadt->Flags, PLATFORM_CLOCK);
715 PRINTFLAG(fadt->Flags, S4_RTC_VALID);
716 PRINTFLAG(fadt->Flags, REMOTE_POWER_ON);
717 PRINTFLAG(fadt->Flags, APIC_CLUSTER);
718 PRINTFLAG(fadt->Flags, APIC_PHYSICAL);
719 if (fadt->Flags != 0)
724 if (fadt->Flags & ACPI_FADT_RESET_REGISTER) {
725 printf("\tRESET_REG=");
726 acpi_print_gas(&fadt->ResetRegister);
727 printf(", RESET_VALUE=%#x\n", fadt->ResetValue);
729 if (acpi_get_fadt_revision(fadt) > 1) {
730 printf("\tX_FACS=0x%08lx, ", (u_long)fadt->XFacs);
731 printf("X_DSDT=0x%08lx\n", (u_long)fadt->XDsdt);
732 printf("\tX_PM1a_EVT_BLK=");
733 acpi_print_gas(&fadt->XPm1aEventBlock);
734 if (fadt->XPm1bEventBlock.Address != 0) {
735 printf("\n\tX_PM1b_EVT_BLK=");
736 acpi_print_gas(&fadt->XPm1bEventBlock);
738 printf("\n\tX_PM1a_CNT_BLK=");
739 acpi_print_gas(&fadt->XPm1aControlBlock);
740 if (fadt->XPm1bControlBlock.Address != 0) {
741 printf("\n\tX_PM1b_CNT_BLK=");
742 acpi_print_gas(&fadt->XPm1bControlBlock);
744 if (fadt->XPm2ControlBlock.Address != 0) {
745 printf("\n\tX_PM2_CNT_BLK=");
746 acpi_print_gas(&fadt->XPm2ControlBlock);
748 printf("\n\tX_PM_TMR_BLK=");
749 acpi_print_gas(&fadt->XPmTimerBlock);
750 if (fadt->XGpe0Block.Address != 0) {
751 printf("\n\tX_GPE0_BLK=");
752 acpi_print_gas(&fadt->XGpe0Block);
754 if (fadt->XGpe1Block.Address != 0) {
755 printf("\n\tX_GPE1_BLK=");
756 acpi_print_gas(&fadt->XGpe1Block);
765 acpi_print_facs(ACPI_TABLE_FACS *facs)
767 printf(BEGIN_COMMENT);
768 printf(" FACS:\tLength=%u, ", facs->Length);
769 printf("HwSig=0x%08x, ", facs->HardwareSignature);
770 printf("Firm_Wake_Vec=0x%08x\n", facs->FirmwareWakingVector);
772 printf("\tGlobal_Lock=");
773 if (facs->GlobalLock != 0) {
774 if (facs->GlobalLock & ACPI_GLOCK_PENDING)
776 if (facs->GlobalLock & ACPI_GLOCK_OWNED)
782 if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
786 if (facs->XFirmwareWakingVector != 0) {
787 printf("\tX_Firm_Wake_Vec=%08lx\n",
788 (u_long)facs->XFirmwareWakingVector);
790 printf("\tVersion=%u\n", facs->Version);
796 acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp)
798 printf(BEGIN_COMMENT);
799 acpi_print_sdt(dsdp);
804 acpi_checksum(void *p, size_t length)
817 static ACPI_TABLE_HEADER *
818 acpi_map_sdt(vm_offset_t pa)
820 ACPI_TABLE_HEADER *sp;
822 sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
823 sp = acpi_map_physical(pa, sp->Length);
828 acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp)
830 printf(BEGIN_COMMENT);
831 printf(" RSD PTR: OEM=");
832 acpi_print_string(rp->OemId, ACPI_OEM_ID_SIZE);
833 printf(", ACPI_Rev=%s (%d)\n", rp->Revision < 2 ? "1.0x" : "2.0x",
835 if (rp->Revision < 2) {
836 printf("\tRSDT=0x%08x, cksum=%u\n", rp->RsdtPhysicalAddress,
839 printf("\tXSDT=0x%08lx, length=%u, cksum=%u\n",
840 (u_long)rp->XsdtPhysicalAddress, rp->Length,
841 rp->ExtendedChecksum);
847 acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp)
849 ACPI_TABLE_HEADER *sdp;
850 ACPI_TABLE_RSDT *rsdt;
851 ACPI_TABLE_XSDT *xsdt;
855 acpi_print_rsdt(rsdp);
856 rsdt = (ACPI_TABLE_RSDT *)rsdp;
857 xsdt = (ACPI_TABLE_XSDT *)rsdp;
858 entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
859 for (i = 0; i < entries; i++) {
862 addr = le32toh(rsdt->TableOffsetEntry[i]);
865 addr = le64toh(xsdt->TableOffsetEntry[i]);
871 sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
872 if (acpi_checksum(sdp, sdp->Length)) {
873 warnx("RSDT entry %d (sig %.4s) is corrupt", i,
877 if (!memcmp(sdp->Signature, ACPI_SIG_FADT, 4))
878 acpi_handle_fadt(sdp);
879 else if (!memcmp(sdp->Signature, ACPI_SIG_MADT, 4))
880 acpi_handle_madt(sdp);
881 else if (!memcmp(sdp->Signature, ACPI_SIG_HPET, 4))
882 acpi_handle_hpet(sdp);
883 else if (!memcmp(sdp->Signature, ACPI_SIG_ECDT, 4))
884 acpi_handle_ecdt(sdp);
885 else if (!memcmp(sdp->Signature, ACPI_SIG_MCFG, 4))
886 acpi_handle_mcfg(sdp);
887 else if (!memcmp(sdp->Signature, ACPI_SIG_SRAT, 4))
888 acpi_handle_srat(sdp);
890 printf(BEGIN_COMMENT);
898 sdt_load_devmem(void)
901 ACPI_TABLE_HEADER *rsdp;
903 rp = acpi_find_rsd_ptr();
905 errx(1, "Can't find ACPI information");
908 acpi_print_rsd_ptr(rp);
909 if (rp->Revision < 2) {
910 rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->RsdtPhysicalAddress);
911 if (memcmp(rsdp->Signature, "RSDT", 4) != 0 ||
912 acpi_checksum(rsdp, rsdp->Length) != 0)
913 errx(1, "RSDT is corrupted");
914 addr_size = sizeof(uint32_t);
916 rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->XsdtPhysicalAddress);
917 if (memcmp(rsdp->Signature, "XSDT", 4) != 0 ||
918 acpi_checksum(rsdp, rsdp->Length) != 0)
919 errx(1, "XSDT is corrupted");
920 addr_size = sizeof(uint64_t);
925 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
927 write_dsdt(int fd, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdt)
929 ACPI_TABLE_HEADER sdt;
930 ACPI_TABLE_HEADER *ssdt;
933 /* Create a new checksum to account for the DSDT and any SSDTs. */
937 sum = acpi_checksum(dsdt + 1, dsdt->Length -
938 sizeof(ACPI_TABLE_HEADER));
939 ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, NULL);
940 while (ssdt != NULL) {
941 sdt.Length += ssdt->Length - sizeof(ACPI_TABLE_HEADER);
942 sum += acpi_checksum(ssdt + 1,
943 ssdt->Length - sizeof(ACPI_TABLE_HEADER));
944 ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, ssdt);
946 sum += acpi_checksum(&sdt, sizeof(ACPI_TABLE_HEADER));
950 /* Write out the DSDT header and body. */
951 write(fd, &sdt, sizeof(ACPI_TABLE_HEADER));
952 write(fd, dsdt + 1, dsdt->Length - sizeof(ACPI_TABLE_HEADER));
954 /* Write out any SSDTs (if present.) */
956 ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
957 while (ssdt != NULL) {
958 write(fd, ssdt + 1, ssdt->Length -
959 sizeof(ACPI_TABLE_HEADER));
960 ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
967 dsdt_save_file(char *outfile, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
972 assert(outfile != NULL);
973 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
974 fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
976 perror("dsdt_save_file");
979 write_dsdt(fd, rsdt, dsdp);
984 aml_disassemble(ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
986 char buf[PATH_MAX], tmpstr[PATH_MAX];
993 tmpdir = getenv("TMPDIR");
996 strncpy(tmpstr, tmpdir, sizeof(tmpstr));
997 strncat(tmpstr, "/acpidump.", sizeof(tmpstr) - strlen(tmpdir));
998 if (realpath(tmpstr, buf) == NULL) {
999 perror("realpath tmp file");
1002 strncpy(tmpstr, buf, sizeof(tmpstr));
1004 tmpext = tmpstr + len;
1005 strncpy(tmpext, "XXXXXX", sizeof(tmpstr) - len);
1006 fd = mkstemp(tmpstr);
1008 perror("iasl tmp file");
1011 write_dsdt(fd, rsdt, dsdp);
1014 /* Run iasl -d on the temp file */
1016 close(STDOUT_FILENO);
1018 close(STDERR_FILENO);
1019 execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
1026 /* Dump iasl's output to stdout */
1027 strncpy(tmpext, "dsl", sizeof(tmpstr) - len);
1028 fp = fopen(tmpstr, "r");
1031 perror("iasl tmp file (read)");
1034 while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
1035 fwrite(buf, 1, len, stdout);
1040 sdt_print_all(ACPI_TABLE_HEADER *rsdp)
1042 acpi_handle_rsdt(rsdp);
1045 /* Fetch a table matching the given signature via the RSDT. */
1047 sdt_from_rsdt(ACPI_TABLE_HEADER *rsdp, const char *sig, ACPI_TABLE_HEADER *last)
1049 ACPI_TABLE_HEADER *sdt;
1050 ACPI_TABLE_RSDT *rsdt;
1051 ACPI_TABLE_XSDT *xsdt;
1055 rsdt = (ACPI_TABLE_RSDT *)rsdp;
1056 xsdt = (ACPI_TABLE_XSDT *)rsdp;
1057 entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
1058 for (i = 0; i < entries; i++) {
1059 switch (addr_size) {
1061 addr = le32toh(rsdt->TableOffsetEntry[i]);
1064 addr = le64toh(xsdt->TableOffsetEntry[i]);
1069 sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
1075 if (memcmp(sdt->Signature, sig, strlen(sig)))
1077 if (acpi_checksum(sdt, sdt->Length))
1078 errx(1, "RSDT entry %d is corrupt", i);
1086 dsdt_from_fadt(ACPI_TABLE_FADT *fadt)
1088 ACPI_TABLE_HEADER *sdt;
1090 /* Use the DSDT address if it is version 1, otherwise use XDSDT. */
1091 if (acpi_get_fadt_revision(fadt) == 1)
1092 sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
1094 sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
1095 if (acpi_checksum(sdt, sdt->Length))
1096 errx(1, "DSDT is corrupt\n");