xref: /freebsd/usr.sbin/acpi/acpidump/acpi.c (revision 7447ca0eb235974642312b9555caec00b57d8fc1)
1 /*-
2  * Copyright (c) 1998 Doug Rabson
3  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/endian.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <assert.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "acpidump.h"
45 
46 #define BEGIN_COMMENT	"/*\n"
47 #define END_COMMENT	" */\n"
48 
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,
57 		    uint64_t apic_addr);
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_handle_slit(ACPI_TABLE_HEADER *sdp);
67 static void	acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
68 		    uint32_t flags);
69 static void	acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp);
70 static void	acpi_print_srat(ACPI_SUBTABLE_HEADER *srat);
71 static void	acpi_handle_srat(ACPI_TABLE_HEADER *sdp);
72 static void	acpi_handle_tcpa(ACPI_TABLE_HEADER *sdp);
73 static void	acpi_print_sdt(ACPI_TABLE_HEADER *sdp);
74 static void	acpi_print_fadt(ACPI_TABLE_HEADER *sdp);
75 static void	acpi_print_facs(ACPI_TABLE_FACS *facs);
76 static void	acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp);
77 static ACPI_TABLE_HEADER *acpi_map_sdt(vm_offset_t pa);
78 static void	acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp);
79 static void	acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp);
80 static void	acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
81 		    void (*action)(ACPI_SUBTABLE_HEADER *));
82 
83 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
84 static int addr_size;
85 
86 /* Strings used in the TCPA table */
87 static const char *tcpa_event_type_strings[] = {
88 	"PREBOOT Certificate",
89 	"POST Code",
90 	"Unused",
91 	"No Action",
92 	"Separator",
93 	"Action",
94 	"Event Tag",
95 	"S-CRTM Contents",
96 	"S-CRTM Version",
97 	"CPU Microcode",
98 	"Platform Config Flags",
99 	"Table of Devices",
100 	"Compact Hash",
101 	"IPL",
102 	"IPL Partition Data",
103 	"Non-Host Code",
104 	"Non-Host Config",
105 	"Non-Host Info"
106 };
107 
108 static const char *TCPA_pcclient_strings[] = {
109 	"<undefined>",
110 	"SMBIOS",
111 	"BIS Certificate",
112 	"POST BIOS ROM Strings",
113 	"ESCD",
114 	"CMOS",
115 	"NVRAM",
116 	"Option ROM Execute",
117 	"Option ROM Configurateion",
118 	"<undefined>",
119 	"Option ROM Microcode Update ",
120 	"S-CRTM Version String",
121 	"S-CRTM Contents",
122 	"POST Contents",
123 	"Table of Devices",
124 };
125 
126 #define	PRINTFLAG_END()		printflag_end()
127 
128 static char pf_sep = '{';
129 
130 static void
131 printflag_end(void)
132 {
133 
134 	if (pf_sep != '{') {
135 		printf("}");
136 		pf_sep = '{';
137 	}
138 	printf("\n");
139 }
140 
141 static void
142 printflag(uint64_t var, uint64_t mask, const char *name)
143 {
144 
145 	if (var & mask) {
146 		printf("%c%s", pf_sep, name);
147 		pf_sep = ',';
148 	}
149 }
150 
151 static void
152 acpi_print_string(char *s, size_t length)
153 {
154 	int	c;
155 
156 	/* Trim trailing spaces and NULLs */
157 	while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
158 		length--;
159 
160 	while (length--) {
161 		c = *s++;
162 		putchar(c);
163 	}
164 }
165 
166 static void
167 acpi_print_gas(ACPI_GENERIC_ADDRESS *gas)
168 {
169 	switch(gas->SpaceId) {
170 	case ACPI_GAS_MEMORY:
171 		if (gas->BitWidth <= 32)
172 			printf("0x%08x:%u[%u] (Memory)",
173 			    (u_int)gas->Address, gas->BitOffset,
174 			    gas->BitWidth);
175 		else
176 			printf("0x%016jx:%u[%u] (Memory)",
177 			    (uintmax_t)gas->Address, gas->BitOffset,
178 			    gas->BitWidth);
179 		break;
180 	case ACPI_GAS_IO:
181 		printf("0x%02x:%u[%u] (IO)", (u_int)gas->Address,
182 		    gas->BitOffset, gas->BitWidth);
183 		break;
184 	case ACPI_GAS_PCI:
185 		printf("%x:%x+0x%x (PCI)", (uint16_t)(gas->Address >> 32),
186 		       (uint16_t)((gas->Address >> 16) & 0xffff),
187 		       (uint16_t)gas->Address);
188 		break;
189 	/* XXX How to handle these below? */
190 	case ACPI_GAS_EMBEDDED:
191 		printf("0x%x:%u[%u] (EC)", (uint16_t)gas->Address,
192 		       gas->BitOffset, gas->BitWidth);
193 		break;
194 	case ACPI_GAS_SMBUS:
195 		printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->Address,
196 		       gas->BitOffset, gas->BitWidth);
197 		break;
198 	case ACPI_GAS_CMOS:
199 	case ACPI_GAS_PCIBAR:
200 	case ACPI_GAS_DATATABLE:
201 	case ACPI_GAS_FIXED:
202 	default:
203 		printf("0x%016jx (?)", (uintmax_t)gas->Address);
204 		break;
205 	}
206 }
207 
208 /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
209 static int
210 acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt)
211 {
212 	int fadt_revision;
213 
214 	/* Set the FADT revision separately from the RSDP version. */
215 	if (addr_size == 8) {
216 		fadt_revision = 2;
217 
218 		/*
219 		 * A few systems (e.g., IBM T23) have an RSDP that claims
220 		 * revision 2 but the 64 bit addresses are invalid.  If
221 		 * revision 2 and the 32 bit address is non-zero but the
222 		 * 32 and 64 bit versions don't match, prefer the 32 bit
223 		 * version for all subsequent tables.
224 		 */
225 		if (fadt->Facs != 0 &&
226 		    (fadt->XFacs & 0xffffffff) != fadt->Facs)
227 			fadt_revision = 1;
228 	} else
229 		fadt_revision = 1;
230 	return (fadt_revision);
231 }
232 
233 static void
234 acpi_handle_fadt(ACPI_TABLE_HEADER *sdp)
235 {
236 	ACPI_TABLE_HEADER *dsdp;
237 	ACPI_TABLE_FACS	*facs;
238 	ACPI_TABLE_FADT *fadt;
239 	int		fadt_revision;
240 
241 	fadt = (ACPI_TABLE_FADT *)sdp;
242 	acpi_print_fadt(sdp);
243 
244 	fadt_revision = acpi_get_fadt_revision(fadt);
245 	if (fadt_revision == 1)
246 		facs = (ACPI_TABLE_FACS *)acpi_map_sdt(fadt->Facs);
247 	else
248 		facs = (ACPI_TABLE_FACS *)acpi_map_sdt(fadt->XFacs);
249 	if (memcmp(facs->Signature, ACPI_SIG_FACS, 4) != 0 || facs->Length < 64)
250 		errx(1, "FACS is corrupt");
251 	acpi_print_facs(facs);
252 
253 	if (fadt_revision == 1)
254 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
255 	else
256 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
257 	if (acpi_checksum(dsdp, dsdp->Length))
258 		errx(1, "DSDT is corrupt");
259 	acpi_print_dsdt(dsdp);
260 }
261 
262 static void
263 acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
264     void (*action)(ACPI_SUBTABLE_HEADER *))
265 {
266 	ACPI_SUBTABLE_HEADER *subtable;
267 	char *end;
268 
269 	subtable = first;
270 	end = (char *)table + table->Length;
271 	while ((char *)subtable < end) {
272 		printf("\n");
273 		action(subtable);
274 		subtable = (ACPI_SUBTABLE_HEADER *)((char *)subtable +
275 		    subtable->Length);
276 	}
277 }
278 
279 static void
280 acpi_print_cpu(u_char cpu_id)
281 {
282 
283 	printf("\tACPI CPU=");
284 	if (cpu_id == 0xff)
285 		printf("ALL\n");
286 	else
287 		printf("%d\n", (u_int)cpu_id);
288 }
289 
290 static void
291 acpi_print_cpu_uid(uint32_t uid, char *uid_string)
292 {
293 
294 	printf("\tUID=%d", uid);
295 	if (uid_string != NULL)
296 		printf(" (%s)", uid_string);
297 	printf("\n");
298 }
299 
300 static void
301 acpi_print_local_apic(uint32_t apic_id, uint32_t flags)
302 {
303 
304 	printf("\tFlags={");
305 	if (flags & ACPI_MADT_ENABLED)
306 		printf("ENABLED");
307 	else
308 		printf("DISABLED");
309 	printf("}\n");
310 	printf("\tAPIC ID=%d\n", apic_id);
311 }
312 
313 static void
314 acpi_print_io_apic(uint32_t apic_id, uint32_t int_base, uint64_t apic_addr)
315 {
316 
317 	printf("\tAPIC ID=%d\n", apic_id);
318 	printf("\tINT BASE=%d\n", int_base);
319 	printf("\tADDR=0x%016jx\n", (uintmax_t)apic_addr);
320 }
321 
322 static void
323 acpi_print_mps_flags(uint16_t flags)
324 {
325 
326 	printf("\tFlags={Polarity=");
327 	switch (flags & ACPI_MADT_POLARITY_MASK) {
328 	case ACPI_MADT_POLARITY_CONFORMS:
329 		printf("conforming");
330 		break;
331 	case ACPI_MADT_POLARITY_ACTIVE_HIGH:
332 		printf("active-hi");
333 		break;
334 	case ACPI_MADT_POLARITY_ACTIVE_LOW:
335 		printf("active-lo");
336 		break;
337 	default:
338 		printf("0x%x", flags & ACPI_MADT_POLARITY_MASK);
339 		break;
340 	}
341 	printf(", Trigger=");
342 	switch (flags & ACPI_MADT_TRIGGER_MASK) {
343 	case ACPI_MADT_TRIGGER_CONFORMS:
344 		printf("conforming");
345 		break;
346 	case ACPI_MADT_TRIGGER_EDGE:
347 		printf("edge");
348 		break;
349 	case ACPI_MADT_TRIGGER_LEVEL:
350 		printf("level");
351 		break;
352 	default:
353 		printf("0x%x", (flags & ACPI_MADT_TRIGGER_MASK) >> 2);
354 	}
355 	printf("}\n");
356 }
357 
358 static void
359 acpi_print_intr(uint32_t intr, uint16_t mps_flags)
360 {
361 
362 	printf("\tINTR=%d\n", intr);
363 	acpi_print_mps_flags(mps_flags);
364 }
365 
366 static void
367 acpi_print_local_nmi(u_int lint, uint16_t mps_flags)
368 {
369 
370 	printf("\tLINT Pin=%d\n", lint);
371 	acpi_print_mps_flags(mps_flags);
372 }
373 
374 static const char *apic_types[] = { "Local APIC", "IO APIC", "INT Override",
375 				    "NMI", "Local APIC NMI",
376 				    "Local APIC Override", "IO SAPIC",
377 				    "Local SAPIC", "Platform Interrupt",
378 				    "Local X2APIC", "Local X2APIC NMI" };
379 static const char *platform_int_types[] = { "0 (unknown)", "PMI", "INIT",
380 					    "Corrected Platform Error" };
381 
382 static void
383 acpi_print_madt(ACPI_SUBTABLE_HEADER *mp)
384 {
385 	ACPI_MADT_LOCAL_APIC *lapic;
386 	ACPI_MADT_IO_APIC *ioapic;
387 	ACPI_MADT_INTERRUPT_OVERRIDE *over;
388 	ACPI_MADT_NMI_SOURCE *nmi;
389 	ACPI_MADT_LOCAL_APIC_NMI *lapic_nmi;
390 	ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_over;
391 	ACPI_MADT_IO_SAPIC *iosapic;
392 	ACPI_MADT_LOCAL_SAPIC *lsapic;
393 	ACPI_MADT_INTERRUPT_SOURCE *isrc;
394 	ACPI_MADT_LOCAL_X2APIC *x2apic;
395 	ACPI_MADT_LOCAL_X2APIC_NMI *x2apic_nmi;
396 
397 	if (mp->Type < sizeof(apic_types) / sizeof(apic_types[0]))
398 		printf("\tType=%s\n", apic_types[mp->Type]);
399 	else
400 		printf("\tType=%d (unknown)\n", mp->Type);
401 	switch (mp->Type) {
402 	case ACPI_MADT_TYPE_LOCAL_APIC:
403 		lapic = (ACPI_MADT_LOCAL_APIC *)mp;
404 		acpi_print_cpu(lapic->ProcessorId);
405 		acpi_print_local_apic(lapic->Id, lapic->LapicFlags);
406 		break;
407 	case ACPI_MADT_TYPE_IO_APIC:
408 		ioapic = (ACPI_MADT_IO_APIC *)mp;
409 		acpi_print_io_apic(ioapic->Id, ioapic->GlobalIrqBase,
410 		    ioapic->Address);
411 		break;
412 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
413 		over = (ACPI_MADT_INTERRUPT_OVERRIDE *)mp;
414 		printf("\tBUS=%d\n", (u_int)over->Bus);
415 		printf("\tIRQ=%d\n", (u_int)over->SourceIrq);
416 		acpi_print_intr(over->GlobalIrq, over->IntiFlags);
417 		break;
418 	case ACPI_MADT_TYPE_NMI_SOURCE:
419 		nmi = (ACPI_MADT_NMI_SOURCE *)mp;
420 		acpi_print_intr(nmi->GlobalIrq, nmi->IntiFlags);
421 		break;
422 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
423 		lapic_nmi = (ACPI_MADT_LOCAL_APIC_NMI *)mp;
424 		acpi_print_cpu(lapic_nmi->ProcessorId);
425 		acpi_print_local_nmi(lapic_nmi->Lint, lapic_nmi->IntiFlags);
426 		break;
427 	case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
428 		lapic_over = (ACPI_MADT_LOCAL_APIC_OVERRIDE *)mp;
429 		printf("\tLocal APIC ADDR=0x%016jx\n",
430 		    (uintmax_t)lapic_over->Address);
431 		break;
432 	case ACPI_MADT_TYPE_IO_SAPIC:
433 		iosapic = (ACPI_MADT_IO_SAPIC *)mp;
434 		acpi_print_io_apic(iosapic->Id, iosapic->GlobalIrqBase,
435 		    iosapic->Address);
436 		break;
437 	case ACPI_MADT_TYPE_LOCAL_SAPIC:
438 		lsapic = (ACPI_MADT_LOCAL_SAPIC *)mp;
439 		acpi_print_cpu(lsapic->ProcessorId);
440 		acpi_print_local_apic(lsapic->Id, lsapic->LapicFlags);
441 		printf("\tAPIC EID=%d\n", (u_int)lsapic->Eid);
442 		if (mp->Length > __offsetof(ACPI_MADT_LOCAL_SAPIC, Uid))
443 			acpi_print_cpu_uid(lsapic->Uid, lsapic->UidString);
444 		break;
445 	case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
446 		isrc = (ACPI_MADT_INTERRUPT_SOURCE *)mp;
447 		if (isrc->Type < sizeof(platform_int_types) /
448 		    sizeof(platform_int_types[0]))
449 			printf("\tType=%s\n", platform_int_types[isrc->Type]);
450 		else
451 			printf("\tType=%d (unknown)\n", isrc->Type);
452 		printf("\tAPIC ID=%d\n", (u_int)isrc->Id);
453 		printf("\tAPIC EID=%d\n", (u_int)isrc->Eid);
454 		printf("\tSAPIC Vector=%d\n", (u_int)isrc->IoSapicVector);
455 		acpi_print_intr(isrc->GlobalIrq, isrc->IntiFlags);
456 		break;
457 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
458 		x2apic = (ACPI_MADT_LOCAL_X2APIC *)mp;
459 		acpi_print_cpu_uid(x2apic->Uid, NULL);
460 		acpi_print_local_apic(x2apic->LocalApicId, x2apic->LapicFlags);
461 		break;
462 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
463 		x2apic_nmi = (ACPI_MADT_LOCAL_X2APIC_NMI *)mp;
464 		acpi_print_cpu_uid(x2apic_nmi->Uid, NULL);
465 		acpi_print_local_nmi(x2apic_nmi->Lint, x2apic_nmi->IntiFlags);
466 		break;
467 	}
468 }
469 
470 static void
471 acpi_handle_madt(ACPI_TABLE_HEADER *sdp)
472 {
473 	ACPI_TABLE_MADT *madt;
474 
475 	printf(BEGIN_COMMENT);
476 	acpi_print_sdt(sdp);
477 	madt = (ACPI_TABLE_MADT *)sdp;
478 	printf("\tLocal APIC ADDR=0x%08x\n", madt->Address);
479 	printf("\tFlags={");
480 	if (madt->Flags & ACPI_MADT_PCAT_COMPAT)
481 		printf("PC-AT");
482 	printf("}\n");
483 	acpi_walk_subtables(sdp, (madt + 1), acpi_print_madt);
484 	printf(END_COMMENT);
485 }
486 
487 static void
488 acpi_handle_hpet(ACPI_TABLE_HEADER *sdp)
489 {
490 	ACPI_TABLE_HPET *hpet;
491 
492 	printf(BEGIN_COMMENT);
493 	acpi_print_sdt(sdp);
494 	hpet = (ACPI_TABLE_HPET *)sdp;
495 	printf("\tHPET Number=%d\n", hpet->Sequence);
496 	printf("\tADDR=");
497 	acpi_print_gas(&hpet->Address);
498 	printf("\tHW Rev=0x%x\n", hpet->Id & ACPI_HPET_ID_HARDWARE_REV_ID);
499 	printf("\tComparators=%d\n", (hpet->Id & ACPI_HPET_ID_COMPARATORS) >>
500 	    8);
501 	printf("\tCounter Size=%d\n", hpet->Id & ACPI_HPET_ID_COUNT_SIZE_CAP ?
502 	    1 : 0);
503 	printf("\tLegacy IRQ routing capable={");
504 	if (hpet->Id & ACPI_HPET_ID_LEGACY_CAPABLE)
505 		printf("TRUE}\n");
506 	else
507 		printf("FALSE}\n");
508 	printf("\tPCI Vendor ID=0x%04x\n", hpet->Id >> 16);
509 	printf("\tMinimal Tick=%d\n", hpet->MinimumTick);
510 	printf("\tFlags=0x%02x\n", hpet->Flags);
511 	printf(END_COMMENT);
512 }
513 
514 static void
515 acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp)
516 {
517 	ACPI_TABLE_ECDT *ecdt;
518 
519 	printf(BEGIN_COMMENT);
520 	acpi_print_sdt(sdp);
521 	ecdt = (ACPI_TABLE_ECDT *)sdp;
522 	printf("\tEC_CONTROL=");
523 	acpi_print_gas(&ecdt->Control);
524 	printf("\n\tEC_DATA=");
525 	acpi_print_gas(&ecdt->Data);
526 	printf("\n\tUID=%#x, ", ecdt->Uid);
527 	printf("GPE_BIT=%#x\n", ecdt->Gpe);
528 	printf("\tEC_ID=%s\n", ecdt->Id);
529 	printf(END_COMMENT);
530 }
531 
532 static void
533 acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp)
534 {
535 	ACPI_TABLE_MCFG *mcfg;
536 	ACPI_MCFG_ALLOCATION *alloc;
537 	u_int i, entries;
538 
539 	printf(BEGIN_COMMENT);
540 	acpi_print_sdt(sdp);
541 	mcfg = (ACPI_TABLE_MCFG *)sdp;
542 	entries = (sdp->Length - sizeof(ACPI_TABLE_MCFG)) /
543 	    sizeof(ACPI_MCFG_ALLOCATION);
544 	alloc = (ACPI_MCFG_ALLOCATION *)(mcfg + 1);
545 	for (i = 0; i < entries; i++, alloc++) {
546 		printf("\n");
547 		printf("\tBase Address=0x%016jx\n", (uintmax_t)alloc->Address);
548 		printf("\tSegment Group=0x%04x\n", alloc->PciSegment);
549 		printf("\tStart Bus=%d\n", alloc->StartBusNumber);
550 		printf("\tEnd Bus=%d\n", alloc->EndBusNumber);
551 	}
552 	printf(END_COMMENT);
553 }
554 
555 static void
556 acpi_handle_slit(ACPI_TABLE_HEADER *sdp)
557 {
558 	ACPI_TABLE_SLIT *slit;
559 	UINT64 i, j;
560 
561 	printf(BEGIN_COMMENT);
562 	acpi_print_sdt(sdp);
563 	slit = (ACPI_TABLE_SLIT *)sdp;
564 	printf("\tLocality Count=%ju\n", (uintmax_t)slit->LocalityCount);
565 	printf("\n\t      ");
566 	for (i = 0; i < slit->LocalityCount; i++)
567 		printf(" %3ju", (uintmax_t)i);
568 	printf("\n\t     +");
569 	for (i = 0; i < slit->LocalityCount; i++)
570 		printf("----");
571 	printf("\n");
572 	for (i = 0; i < slit->LocalityCount; i++) {
573 		printf("\t %3ju |", (uintmax_t)i);
574 		for (j = 0; j < slit->LocalityCount; j++)
575 			printf(" %3d",
576 			    slit->Entry[i * slit->LocalityCount + j]);
577 		printf("\n");
578 	}
579 	printf(END_COMMENT);
580 }
581 
582 static void
583 acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
584     uint32_t flags)
585 {
586 
587 	printf("\tFlags={");
588 	if (flags & ACPI_SRAT_CPU_ENABLED)
589 		printf("ENABLED");
590 	else
591 		printf("DISABLED");
592 	printf("}\n");
593 	printf("\tAPIC ID=%d\n", apic_id);
594 	printf("\tProximity Domain=%d\n", proximity_domain);
595 }
596 
597 static char *
598 acpi_tcpa_evname(struct TCPAevent *event)
599 {
600 	struct TCPApc_event *pc_event;
601 	char *eventname = NULL;
602 
603 	pc_event = (struct TCPApc_event *)(event + 1);
604 
605 	switch(event->event_type) {
606 	case PREBOOT:
607 	case POST_CODE:
608 	case UNUSED:
609 	case NO_ACTION:
610 	case SEPARATOR:
611 	case SCRTM_CONTENTS:
612 	case SCRTM_VERSION:
613 	case CPU_MICROCODE:
614 	case PLATFORM_CONFIG_FLAGS:
615 	case TABLE_OF_DEVICES:
616 	case COMPACT_HASH:
617 	case IPL:
618 	case IPL_PARTITION_DATA:
619 	case NONHOST_CODE:
620 	case NONHOST_CONFIG:
621 	case NONHOST_INFO:
622 		asprintf(&eventname, "%s",
623 		    tcpa_event_type_strings[event->event_type]);
624 		break;
625 
626 	case ACTION:
627 		eventname = calloc(event->event_size + 1, sizeof(char));
628 		memcpy(eventname, pc_event, event->event_size);
629 		break;
630 
631 	case EVENT_TAG:
632 		switch (pc_event->event_id) {
633 		case SMBIOS:
634 		case BIS_CERT:
635 		case CMOS:
636 		case NVRAM:
637 		case OPTION_ROM_EXEC:
638 		case OPTION_ROM_CONFIG:
639 		case S_CRTM_VERSION:
640 		case POST_BIOS_ROM:
641 		case ESCD:
642 		case OPTION_ROM_MICROCODE:
643 		case S_CRTM_CONTENTS:
644 		case POST_CONTENTS:
645 			asprintf(&eventname, "%s",
646 			    TCPA_pcclient_strings[pc_event->event_id]);
647 			break;
648 
649 		default:
650 			asprintf(&eventname, "<unknown tag 0x%02x>",
651 			    pc_event->event_id);
652 			break;
653 		}
654 		break;
655 
656 	default:
657 		asprintf(&eventname, "<unknown 0x%02x>", event->event_type);
658 		break;
659 	}
660 
661 	return eventname;
662 }
663 
664 static void
665 acpi_print_tcpa(struct TCPAevent *event)
666 {
667 	int i;
668 	char *eventname;
669 
670 	eventname = acpi_tcpa_evname(event);
671 
672 	printf("\t%d", event->pcr_index);
673 	printf(" 0x");
674 	for (i = 0; i < 20; i++)
675 		printf("%02x", event->pcr_value[i]);
676 	printf(" [%s]\n", eventname ? eventname : "<unknown>");
677 
678 	free(eventname);
679 }
680 
681 static void
682 acpi_handle_tcpa(ACPI_TABLE_HEADER *sdp)
683 {
684 	struct TCPAbody *tcpa;
685 	struct TCPAevent *event;
686 	uintmax_t len, paddr;
687 	unsigned char *vaddr = NULL;
688 	unsigned char *vend = NULL;
689 
690 	printf(BEGIN_COMMENT);
691 	acpi_print_sdt(sdp);
692 	tcpa = (struct TCPAbody *) sdp;
693 
694 	switch (tcpa->platform_class) {
695 	case ACPI_TCPA_BIOS_CLIENT:
696 		len = tcpa->client.log_max_len;
697 		paddr = tcpa->client.log_start_addr;
698 		break;
699 
700 	case ACPI_TCPA_BIOS_SERVER:
701 		len = tcpa->server.log_max_len;
702 		paddr = tcpa->server.log_start_addr;
703 		break;
704 
705 	default:
706 		printf("XXX");
707 		printf(END_COMMENT);
708 		return;
709 	}
710 	printf("\tClass %u Base Address 0x%jx Length %ju\n\n",
711 	    tcpa->platform_class, paddr, len);
712 
713 	if (len == 0) {
714 		printf("\tEmpty TCPA table\n");
715 		printf(END_COMMENT);
716 		return;
717 	}
718 	if(sdp->Revision == 1){
719 		printf("\tOLD TCPA spec log found. Dumping not supported.\n");
720 		printf(END_COMMENT);
721 		return;
722 	}
723 
724 	vaddr = (unsigned char *)acpi_map_physical(paddr, len);
725 	vend = vaddr + len;
726 
727 	while (vaddr != NULL) {
728 		if ((vaddr + sizeof(struct TCPAevent) >= vend)||
729 		    (vaddr + sizeof(struct TCPAevent) < vaddr))
730 			break;
731 		event = (struct TCPAevent *)(void *)vaddr;
732 		if (vaddr + event->event_size >= vend)
733 			break;
734 		if (vaddr + event->event_size < vaddr)
735 			break;
736 		if (event->event_type == 0 && event->event_size == 0)
737 			break;
738 #if 0
739 		{
740 		unsigned int i, j, k;
741 
742 		printf("\n\tsize %d\n\t\t%p ", event->event_size, vaddr);
743 		for (j = 0, i = 0; i <
744 		    sizeof(struct TCPAevent) + event->event_size; i++) {
745 			printf("%02x ", vaddr[i]);
746 			if ((i+1) % 8 == 0) {
747 				for (k = 0; k < 8; k++)
748 					printf("%c", isprint(vaddr[j+k]) ?
749 					    vaddr[j+k] : '.');
750 				printf("\n\t\t%p ", &vaddr[i + 1]);
751 				j = i + 1;
752 			}
753 		}
754 		printf("\n"); }
755 #endif
756 		acpi_print_tcpa(event);
757 
758 		vaddr += sizeof(struct TCPAevent) + event->event_size;
759 	}
760 
761 	printf(END_COMMENT);
762 }
763 
764 static const char *
765 devscope_type2str(int type)
766 {
767 	static char typebuf[16];
768 
769 	switch (type) {
770 	case 1:
771 		return ("PCI Endpoint Device");
772 	case 2:
773 		return ("PCI Sub-Hierarchy");
774 	case 3:
775 		return ("IOAPIC");
776 	case 4:
777 		return ("HPET");
778 	default:
779 		snprintf(typebuf, sizeof(typebuf), "%d", type);
780 		return (typebuf);
781 	}
782 }
783 
784 static int
785 acpi_handle_dmar_devscope(void *addr, int remaining)
786 {
787 	char sep;
788 	int pathlen;
789 	ACPI_DMAR_PCI_PATH *path, *pathend;
790 	ACPI_DMAR_DEVICE_SCOPE *devscope = addr;
791 
792 	if (remaining < (int)sizeof(ACPI_DMAR_DEVICE_SCOPE))
793 		return (-1);
794 
795 	if (remaining < devscope->Length)
796 		return (-1);
797 
798 	printf("\n");
799 	printf("\t\tType=%s\n", devscope_type2str(devscope->EntryType));
800 	printf("\t\tLength=%d\n", devscope->Length);
801 	printf("\t\tEnumerationId=%d\n", devscope->EnumerationId);
802 	printf("\t\tStartBusNumber=%d\n", devscope->Bus);
803 
804 	path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
805 	pathlen = devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE);
806 	pathend = path + pathlen / sizeof(ACPI_DMAR_PCI_PATH);
807 	if (path < pathend) {
808 		sep = '{';
809 		printf("\t\tPath=");
810 		do {
811 			printf("%c%d:%d", sep, path->Device, path->Function);
812 			sep=',';
813 			path++;
814 		} while (path < pathend);
815 		printf("}\n");
816 	}
817 
818 	return (devscope->Length);
819 }
820 
821 static void
822 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
823 {
824 	char *cp;
825 	int remaining, consumed;
826 
827 	printf("\n");
828 	printf("\tType=DRHD\n");
829 	printf("\tLength=%d\n", drhd->Header.Length);
830 
831 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
832 
833 	printf("\tFlags=");
834 	PRINTFLAG(drhd->Flags, INCLUDE_ALL);
835 	PRINTFLAG_END();
836 
837 #undef PRINTFLAG
838 
839 	printf("\tSegment=%d\n", drhd->Segment);
840 	printf("\tAddress=0x%016jx\n", (uintmax_t)drhd->Address);
841 
842 	remaining = drhd->Header.Length - sizeof(ACPI_DMAR_HARDWARE_UNIT);
843 	if (remaining > 0)
844 		printf("\tDevice Scope:");
845 	while (remaining > 0) {
846 		cp = (char *)drhd + drhd->Header.Length - remaining;
847 		consumed = acpi_handle_dmar_devscope(cp, remaining);
848 		if (consumed <= 0)
849 			break;
850 		else
851 			remaining -= consumed;
852 	}
853 }
854 
855 static void
856 acpi_handle_dmar_rmrr(ACPI_DMAR_RESERVED_MEMORY *rmrr)
857 {
858 	char *cp;
859 	int remaining, consumed;
860 
861 	printf("\n");
862 	printf("\tType=RMRR\n");
863 	printf("\tLength=%d\n", rmrr->Header.Length);
864 	printf("\tSegment=%d\n", rmrr->Segment);
865 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rmrr->BaseAddress);
866 	printf("\tLimitAddress=0x%016jx\n", (uintmax_t)rmrr->EndAddress);
867 
868 	remaining = rmrr->Header.Length - sizeof(ACPI_DMAR_RESERVED_MEMORY);
869 	if (remaining > 0)
870 		printf("\tDevice Scope:");
871 	while (remaining > 0) {
872 		cp = (char *)rmrr + rmrr->Header.Length - remaining;
873 		consumed = acpi_handle_dmar_devscope(cp, remaining);
874 		if (consumed <= 0)
875 			break;
876 		else
877 			remaining -= consumed;
878 	}
879 }
880 
881 static void
882 acpi_handle_dmar_atsr(ACPI_DMAR_ATSR *atsr)
883 {
884 	char *cp;
885 	int remaining, consumed;
886 
887 	printf("\n");
888 	printf("\tType=ATSR\n");
889 	printf("\tLength=%d\n", atsr->Header.Length);
890 
891 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
892 
893 	printf("\tFlags=");
894 	PRINTFLAG(atsr->Flags, ALL_PORTS);
895 	PRINTFLAG_END();
896 
897 #undef PRINTFLAG
898 
899 	printf("\tSegment=%d\n", atsr->Segment);
900 
901 	remaining = atsr->Header.Length - sizeof(ACPI_DMAR_ATSR);
902 	if (remaining > 0)
903 		printf("\tDevice Scope:");
904 	while (remaining > 0) {
905 		cp = (char *)atsr + atsr->Header.Length - remaining;
906 		consumed = acpi_handle_dmar_devscope(cp, remaining);
907 		if (consumed <= 0)
908 			break;
909 		else
910 			remaining -= consumed;
911 	}
912 }
913 
914 static void
915 acpi_handle_dmar_rhsa(ACPI_DMAR_RHSA *rhsa)
916 {
917 
918 	printf("\n");
919 	printf("\tType=RHSA\n");
920 	printf("\tLength=%d\n", rhsa->Header.Length);
921 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rhsa->BaseAddress);
922 	printf("\tProximityDomain=0x%08x\n", rhsa->ProximityDomain);
923 }
924 
925 static int
926 acpi_handle_dmar_remapping_structure(void *addr, int remaining)
927 {
928 	ACPI_DMAR_HEADER *hdr = addr;
929 
930 	if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
931 		return (-1);
932 
933 	if (remaining < hdr->Length)
934 		return (-1);
935 
936 	switch (hdr->Type) {
937 	case ACPI_DMAR_TYPE_HARDWARE_UNIT:
938 		acpi_handle_dmar_drhd(addr);
939 		break;
940 	case ACPI_DMAR_TYPE_RESERVED_MEMORY:
941 		acpi_handle_dmar_rmrr(addr);
942 		break;
943 	case ACPI_DMAR_TYPE_ROOT_ATS:
944 		acpi_handle_dmar_atsr(addr);
945 		break;
946 	case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
947 		acpi_handle_dmar_rhsa(addr);
948 		break;
949 	default:
950 		printf("\n");
951 		printf("\tType=%d\n", hdr->Type);
952 		printf("\tLength=%d\n", hdr->Length);
953 		break;
954 	}
955 	return (hdr->Length);
956 }
957 
958 #ifndef ACPI_DMAR_X2APIC_OPT_OUT
959 #define	ACPI_DMAR_X2APIC_OPT_OUT	(0x2)
960 #endif
961 
962 static void
963 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
964 {
965 	char *cp;
966 	int remaining, consumed;
967 	ACPI_TABLE_DMAR *dmar;
968 
969 	printf(BEGIN_COMMENT);
970 	acpi_print_sdt(sdp);
971 	dmar = (ACPI_TABLE_DMAR *)sdp;
972 	printf("\tHost Address Width=%d\n", dmar->Width + 1);
973 
974 #define PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
975 
976 	printf("\tFlags=");
977 	PRINTFLAG(dmar->Flags, INTR_REMAP);
978 	PRINTFLAG(dmar->Flags, X2APIC_OPT_OUT);
979 	PRINTFLAG_END();
980 
981 #undef PRINTFLAG
982 
983 	remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR);
984 	while (remaining > 0) {
985 		cp = (char *)sdp + sdp->Length - remaining;
986 		consumed = acpi_handle_dmar_remapping_structure(cp, remaining);
987 		if (consumed <= 0)
988 			break;
989 		else
990 			remaining -= consumed;
991 	}
992 
993 	printf(END_COMMENT);
994 }
995 
996 static void
997 acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp)
998 {
999 
1000 	printf("\tFlags={");
1001 	if (mp->Flags & ACPI_SRAT_MEM_ENABLED)
1002 		printf("ENABLED");
1003 	else
1004 		printf("DISABLED");
1005 	if (mp->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
1006 		printf(",HOT_PLUGGABLE");
1007 	if (mp->Flags & ACPI_SRAT_MEM_NON_VOLATILE)
1008 		printf(",NON_VOLATILE");
1009 	printf("}\n");
1010 	printf("\tBase Address=0x%016jx\n", (uintmax_t)mp->BaseAddress);
1011 	printf("\tLength=0x%016jx\n", (uintmax_t)mp->Length);
1012 	printf("\tProximity Domain=%d\n", mp->ProximityDomain);
1013 }
1014 
1015 static const char *srat_types[] = { "CPU", "Memory", "X2APIC" };
1016 
1017 static void
1018 acpi_print_srat(ACPI_SUBTABLE_HEADER *srat)
1019 {
1020 	ACPI_SRAT_CPU_AFFINITY *cpu;
1021 	ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
1022 
1023 	if (srat->Type < sizeof(srat_types) / sizeof(srat_types[0]))
1024 		printf("\tType=%s\n", srat_types[srat->Type]);
1025 	else
1026 		printf("\tType=%d (unknown)\n", srat->Type);
1027 	switch (srat->Type) {
1028 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
1029 		cpu = (ACPI_SRAT_CPU_AFFINITY *)srat;
1030 		acpi_print_srat_cpu(cpu->ApicId,
1031 		    cpu->ProximityDomainHi[2] << 24 |
1032 		    cpu->ProximityDomainHi[1] << 16 |
1033 		    cpu->ProximityDomainHi[0] << 0 |
1034 		    cpu->ProximityDomainLo, cpu->Flags);
1035 		break;
1036 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
1037 		acpi_print_srat_memory((ACPI_SRAT_MEM_AFFINITY *)srat);
1038 		break;
1039 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
1040 		x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)srat;
1041 		acpi_print_srat_cpu(x2apic->ApicId, x2apic->ProximityDomain,
1042 		    x2apic->Flags);
1043 		break;
1044 	}
1045 }
1046 
1047 static void
1048 acpi_handle_srat(ACPI_TABLE_HEADER *sdp)
1049 {
1050 	ACPI_TABLE_SRAT *srat;
1051 
1052 	printf(BEGIN_COMMENT);
1053 	acpi_print_sdt(sdp);
1054 	srat = (ACPI_TABLE_SRAT *)sdp;
1055 	printf("\tTable Revision=%d\n", srat->TableRevision);
1056 	acpi_walk_subtables(sdp, (srat + 1), acpi_print_srat);
1057 	printf(END_COMMENT);
1058 }
1059 
1060 static void
1061 acpi_print_sdt(ACPI_TABLE_HEADER *sdp)
1062 {
1063 	printf("  ");
1064 	acpi_print_string(sdp->Signature, ACPI_NAME_SIZE);
1065 	printf(": Length=%d, Revision=%d, Checksum=%d,\n",
1066 	       sdp->Length, sdp->Revision, sdp->Checksum);
1067 	printf("\tOEMID=");
1068 	acpi_print_string(sdp->OemId, ACPI_OEM_ID_SIZE);
1069 	printf(", OEM Table ID=");
1070 	acpi_print_string(sdp->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
1071 	printf(", OEM Revision=0x%x,\n", sdp->OemRevision);
1072 	printf("\tCreator ID=");
1073 	acpi_print_string(sdp->AslCompilerId, ACPI_NAME_SIZE);
1074 	printf(", Creator Revision=0x%x\n", sdp->AslCompilerRevision);
1075 }
1076 
1077 static void
1078 acpi_print_rsdt(ACPI_TABLE_HEADER *rsdp)
1079 {
1080 	ACPI_TABLE_RSDT *rsdt;
1081 	ACPI_TABLE_XSDT *xsdt;
1082 	int	i, entries;
1083 
1084 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
1085 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
1086 	printf(BEGIN_COMMENT);
1087 	acpi_print_sdt(rsdp);
1088 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
1089 	printf("\tEntries={ ");
1090 	for (i = 0; i < entries; i++) {
1091 		if (i > 0)
1092 			printf(", ");
1093 		if (addr_size == 4)
1094 			printf("0x%08x", le32toh(rsdt->TableOffsetEntry[i]));
1095 		else
1096 			printf("0x%016jx",
1097 			    (uintmax_t)le64toh(xsdt->TableOffsetEntry[i]));
1098 	}
1099 	printf(" }\n");
1100 	printf(END_COMMENT);
1101 }
1102 
1103 static const char *acpi_pm_profiles[] = {
1104 	"Unspecified", "Desktop", "Mobile", "Workstation",
1105 	"Enterprise Server", "SOHO Server", "Appliance PC"
1106 };
1107 
1108 static void
1109 acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
1110 {
1111 	ACPI_TABLE_FADT *fadt;
1112 	const char *pm;
1113 
1114 	fadt = (ACPI_TABLE_FADT *)sdp;
1115 	printf(BEGIN_COMMENT);
1116 	acpi_print_sdt(sdp);
1117 	printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->Facs,
1118 	       fadt->Dsdt);
1119 	printf("\tINT_MODEL=%s\n", fadt->Model ? "APIC" : "PIC");
1120 	if (fadt->PreferredProfile >= sizeof(acpi_pm_profiles) / sizeof(char *))
1121 		pm = "Reserved";
1122 	else
1123 		pm = acpi_pm_profiles[fadt->PreferredProfile];
1124 	printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->PreferredProfile);
1125 	printf("\tSCI_INT=%d\n", fadt->SciInterrupt);
1126 	printf("\tSMI_CMD=0x%x, ", fadt->SmiCommand);
1127 	printf("ACPI_ENABLE=0x%x, ", fadt->AcpiEnable);
1128 	printf("ACPI_DISABLE=0x%x, ", fadt->AcpiDisable);
1129 	printf("S4BIOS_REQ=0x%x\n", fadt->S4BiosRequest);
1130 	printf("\tPSTATE_CNT=0x%x\n", fadt->PstateControl);
1131 	printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
1132 	       fadt->Pm1aEventBlock,
1133 	       fadt->Pm1aEventBlock + fadt->Pm1EventLength - 1);
1134 	if (fadt->Pm1bEventBlock != 0)
1135 		printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
1136 		       fadt->Pm1bEventBlock,
1137 		       fadt->Pm1bEventBlock + fadt->Pm1EventLength - 1);
1138 	printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
1139 	       fadt->Pm1aControlBlock,
1140 	       fadt->Pm1aControlBlock + fadt->Pm1ControlLength - 1);
1141 	if (fadt->Pm1bControlBlock != 0)
1142 		printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
1143 		       fadt->Pm1bControlBlock,
1144 		       fadt->Pm1bControlBlock + fadt->Pm1ControlLength - 1);
1145 	if (fadt->Pm2ControlBlock != 0)
1146 		printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
1147 		       fadt->Pm2ControlBlock,
1148 		       fadt->Pm2ControlBlock + fadt->Pm2ControlLength - 1);
1149 	printf("\tPM_TMR_BLK=0x%x-0x%x\n",
1150 	       fadt->PmTimerBlock,
1151 	       fadt->PmTimerBlock + fadt->PmTimerLength - 1);
1152 	if (fadt->Gpe0Block != 0)
1153 		printf("\tGPE0_BLK=0x%x-0x%x\n",
1154 		       fadt->Gpe0Block,
1155 		       fadt->Gpe0Block + fadt->Gpe0BlockLength - 1);
1156 	if (fadt->Gpe1Block != 0)
1157 		printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
1158 		       fadt->Gpe1Block,
1159 		       fadt->Gpe1Block + fadt->Gpe1BlockLength - 1,
1160 		       fadt->Gpe1Base);
1161 	if (fadt->CstControl != 0)
1162 		printf("\tCST_CNT=0x%x\n", fadt->CstControl);
1163 	printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
1164 	       fadt->C2Latency, fadt->C3Latency);
1165 	printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
1166 	       fadt->FlushSize, fadt->FlushStride);
1167 	printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
1168 	       fadt->DutyOffset, fadt->DutyWidth);
1169 	printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
1170 	       fadt->DayAlarm, fadt->MonthAlarm, fadt->Century);
1171 
1172 #define PRINTFLAG(var, flag)	printflag((var), ACPI_FADT_## flag, #flag)
1173 
1174 	printf("\tIAPC_BOOT_ARCH=");
1175 	PRINTFLAG(fadt->BootFlags, LEGACY_DEVICES);
1176 	PRINTFLAG(fadt->BootFlags, 8042);
1177 	PRINTFLAG(fadt->BootFlags, NO_VGA);
1178 	PRINTFLAG(fadt->BootFlags, NO_MSI);
1179 	PRINTFLAG(fadt->BootFlags, NO_ASPM);
1180 	PRINTFLAG(fadt->BootFlags, NO_CMOS_RTC);
1181 	PRINTFLAG_END();
1182 
1183 	printf("\tFlags=");
1184 	PRINTFLAG(fadt->Flags, WBINVD);
1185 	PRINTFLAG(fadt->Flags, WBINVD_FLUSH);
1186 	PRINTFLAG(fadt->Flags, C1_SUPPORTED);
1187 	PRINTFLAG(fadt->Flags, C2_MP_SUPPORTED);
1188 	PRINTFLAG(fadt->Flags, POWER_BUTTON);
1189 	PRINTFLAG(fadt->Flags, SLEEP_BUTTON);
1190 	PRINTFLAG(fadt->Flags, FIXED_RTC);
1191 	PRINTFLAG(fadt->Flags, S4_RTC_WAKE);
1192 	PRINTFLAG(fadt->Flags, 32BIT_TIMER);
1193 	PRINTFLAG(fadt->Flags, DOCKING_SUPPORTED);
1194 	PRINTFLAG(fadt->Flags, RESET_REGISTER);
1195 	PRINTFLAG(fadt->Flags, SEALED_CASE);
1196 	PRINTFLAG(fadt->Flags, HEADLESS);
1197 	PRINTFLAG(fadt->Flags, SLEEP_TYPE);
1198 	PRINTFLAG(fadt->Flags, PCI_EXPRESS_WAKE);
1199 	PRINTFLAG(fadt->Flags, PLATFORM_CLOCK);
1200 	PRINTFLAG(fadt->Flags, S4_RTC_VALID);
1201 	PRINTFLAG(fadt->Flags, REMOTE_POWER_ON);
1202 	PRINTFLAG(fadt->Flags, APIC_CLUSTER);
1203 	PRINTFLAG(fadt->Flags, APIC_PHYSICAL);
1204 	PRINTFLAG(fadt->Flags, HW_REDUCED);
1205 	PRINTFLAG(fadt->Flags, LOW_POWER_S0);
1206 	PRINTFLAG_END();
1207 
1208 #undef PRINTFLAG
1209 
1210 	if (fadt->Flags & ACPI_FADT_RESET_REGISTER) {
1211 		printf("\tRESET_REG=");
1212 		acpi_print_gas(&fadt->ResetRegister);
1213 		printf(", RESET_VALUE=%#x\n", fadt->ResetValue);
1214 	}
1215 	if (acpi_get_fadt_revision(fadt) > 1) {
1216 		printf("\tX_FACS=0x%016jx, ", (uintmax_t)fadt->XFacs);
1217 		printf("X_DSDT=0x%016jx\n", (uintmax_t)fadt->XDsdt);
1218 		printf("\tX_PM1a_EVT_BLK=");
1219 		acpi_print_gas(&fadt->XPm1aEventBlock);
1220 		if (fadt->XPm1bEventBlock.Address != 0) {
1221 			printf("\n\tX_PM1b_EVT_BLK=");
1222 			acpi_print_gas(&fadt->XPm1bEventBlock);
1223 		}
1224 		printf("\n\tX_PM1a_CNT_BLK=");
1225 		acpi_print_gas(&fadt->XPm1aControlBlock);
1226 		if (fadt->XPm1bControlBlock.Address != 0) {
1227 			printf("\n\tX_PM1b_CNT_BLK=");
1228 			acpi_print_gas(&fadt->XPm1bControlBlock);
1229 		}
1230 		if (fadt->XPm2ControlBlock.Address != 0) {
1231 			printf("\n\tX_PM2_CNT_BLK=");
1232 			acpi_print_gas(&fadt->XPm2ControlBlock);
1233 		}
1234 		printf("\n\tX_PM_TMR_BLK=");
1235 		acpi_print_gas(&fadt->XPmTimerBlock);
1236 		if (fadt->XGpe0Block.Address != 0) {
1237 			printf("\n\tX_GPE0_BLK=");
1238 			acpi_print_gas(&fadt->XGpe0Block);
1239 		}
1240 		if (fadt->XGpe1Block.Address != 0) {
1241 			printf("\n\tX_GPE1_BLK=");
1242 			acpi_print_gas(&fadt->XGpe1Block);
1243 		}
1244 		printf("\n");
1245 	}
1246 
1247 	printf(END_COMMENT);
1248 }
1249 
1250 static void
1251 acpi_print_facs(ACPI_TABLE_FACS *facs)
1252 {
1253 	printf(BEGIN_COMMENT);
1254 	printf("  FACS:\tLength=%u, ", facs->Length);
1255 	printf("HwSig=0x%08x, ", facs->HardwareSignature);
1256 	printf("Firm_Wake_Vec=0x%08x\n", facs->FirmwareWakingVector);
1257 
1258 	printf("\tGlobal_Lock=");
1259 	if (facs->GlobalLock != 0) {
1260 		if (facs->GlobalLock & ACPI_GLOCK_PENDING)
1261 			printf("PENDING,");
1262 		if (facs->GlobalLock & ACPI_GLOCK_OWNED)
1263 			printf("OWNED");
1264 	}
1265 	printf("\n");
1266 
1267 	printf("\tFlags=");
1268 	if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
1269 		printf("S4BIOS");
1270 	printf("\n");
1271 
1272 	if (facs->XFirmwareWakingVector != 0)
1273 		printf("\tX_Firm_Wake_Vec=%016jx\n",
1274 		    (uintmax_t)facs->XFirmwareWakingVector);
1275 	printf("\tVersion=%u\n", facs->Version);
1276 
1277 	printf(END_COMMENT);
1278 }
1279 
1280 static void
1281 acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp)
1282 {
1283 	printf(BEGIN_COMMENT);
1284 	acpi_print_sdt(dsdp);
1285 	printf(END_COMMENT);
1286 }
1287 
1288 int
1289 acpi_checksum(void *p, size_t length)
1290 {
1291 	uint8_t *bp;
1292 	uint8_t sum;
1293 
1294 	bp = p;
1295 	sum = 0;
1296 	while (length--)
1297 		sum += *bp++;
1298 
1299 	return (sum);
1300 }
1301 
1302 static ACPI_TABLE_HEADER *
1303 acpi_map_sdt(vm_offset_t pa)
1304 {
1305 	ACPI_TABLE_HEADER *sp;
1306 
1307 	sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
1308 	sp = acpi_map_physical(pa, sp->Length);
1309 	return (sp);
1310 }
1311 
1312 static void
1313 acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp)
1314 {
1315 	printf(BEGIN_COMMENT);
1316 	printf("  RSD PTR: OEM=");
1317 	acpi_print_string(rp->OemId, ACPI_OEM_ID_SIZE);
1318 	printf(", ACPI_Rev=%s (%d)\n", rp->Revision < 2 ? "1.0x" : "2.0x",
1319 	       rp->Revision);
1320 	if (rp->Revision < 2) {
1321 		printf("\tRSDT=0x%08x, cksum=%u\n", rp->RsdtPhysicalAddress,
1322 		    rp->Checksum);
1323 	} else {
1324 		printf("\tXSDT=0x%016jx, length=%u, cksum=%u\n",
1325 		    (uintmax_t)rp->XsdtPhysicalAddress, rp->Length,
1326 		    rp->ExtendedChecksum);
1327 	}
1328 	printf(END_COMMENT);
1329 }
1330 
1331 static void
1332 acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp)
1333 {
1334 	ACPI_TABLE_HEADER *sdp;
1335 	ACPI_TABLE_RSDT *rsdt;
1336 	ACPI_TABLE_XSDT *xsdt;
1337 	vm_offset_t addr;
1338 	int entries, i;
1339 
1340 	acpi_print_rsdt(rsdp);
1341 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
1342 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
1343 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
1344 	for (i = 0; i < entries; i++) {
1345 		if (addr_size == 4)
1346 			addr = le32toh(rsdt->TableOffsetEntry[i]);
1347 		else
1348 			addr = le64toh(xsdt->TableOffsetEntry[i]);
1349 		if (addr == 0)
1350 			continue;
1351 		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
1352 		if (acpi_checksum(sdp, sdp->Length)) {
1353 			warnx("RSDT entry %d (sig %.4s) is corrupt", i,
1354 			    sdp->Signature);
1355 			continue;
1356 		}
1357 		if (!memcmp(sdp->Signature, ACPI_SIG_FADT, 4))
1358 			acpi_handle_fadt(sdp);
1359 		else if (!memcmp(sdp->Signature, ACPI_SIG_MADT, 4))
1360 			acpi_handle_madt(sdp);
1361 		else if (!memcmp(sdp->Signature, ACPI_SIG_HPET, 4))
1362 			acpi_handle_hpet(sdp);
1363 		else if (!memcmp(sdp->Signature, ACPI_SIG_ECDT, 4))
1364 			acpi_handle_ecdt(sdp);
1365 		else if (!memcmp(sdp->Signature, ACPI_SIG_MCFG, 4))
1366 			acpi_handle_mcfg(sdp);
1367 		else if (!memcmp(sdp->Signature, ACPI_SIG_SLIT, 4))
1368 			acpi_handle_slit(sdp);
1369 		else if (!memcmp(sdp->Signature, ACPI_SIG_SRAT, 4))
1370 			acpi_handle_srat(sdp);
1371 		else if (!memcmp(sdp->Signature, ACPI_SIG_TCPA, 4))
1372 			acpi_handle_tcpa(sdp);
1373 		else if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
1374 			acpi_handle_dmar(sdp);
1375 		else {
1376 			printf(BEGIN_COMMENT);
1377 			acpi_print_sdt(sdp);
1378 			printf(END_COMMENT);
1379 		}
1380 	}
1381 }
1382 
1383 ACPI_TABLE_HEADER *
1384 sdt_load_devmem(void)
1385 {
1386 	ACPI_TABLE_RSDP *rp;
1387 	ACPI_TABLE_HEADER *rsdp;
1388 
1389 	rp = acpi_find_rsd_ptr();
1390 	if (!rp)
1391 		errx(1, "Can't find ACPI information");
1392 
1393 	if (tflag)
1394 		acpi_print_rsd_ptr(rp);
1395 	if (rp->Revision < 2) {
1396 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->RsdtPhysicalAddress);
1397 		if (memcmp(rsdp->Signature, "RSDT", 4) != 0 ||
1398 		    acpi_checksum(rsdp, rsdp->Length) != 0)
1399 			errx(1, "RSDT is corrupted");
1400 		addr_size = sizeof(uint32_t);
1401 	} else {
1402 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->XsdtPhysicalAddress);
1403 		if (memcmp(rsdp->Signature, "XSDT", 4) != 0 ||
1404 		    acpi_checksum(rsdp, rsdp->Length) != 0)
1405 			errx(1, "XSDT is corrupted");
1406 		addr_size = sizeof(uint64_t);
1407 	}
1408 	return (rsdp);
1409 }
1410 
1411 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
1412 static int
1413 write_dsdt(int fd, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdt)
1414 {
1415 	ACPI_TABLE_HEADER sdt;
1416 	ACPI_TABLE_HEADER *ssdt;
1417 	uint8_t sum;
1418 
1419 	/* Create a new checksum to account for the DSDT and any SSDTs. */
1420 	sdt = *dsdt;
1421 	if (rsdt != NULL) {
1422 		sdt.Checksum = 0;
1423 		sum = acpi_checksum(dsdt + 1, dsdt->Length -
1424 		    sizeof(ACPI_TABLE_HEADER));
1425 		ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, NULL);
1426 		while (ssdt != NULL) {
1427 			sdt.Length += ssdt->Length - sizeof(ACPI_TABLE_HEADER);
1428 			sum += acpi_checksum(ssdt + 1,
1429 			    ssdt->Length - sizeof(ACPI_TABLE_HEADER));
1430 			ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, ssdt);
1431 		}
1432 		sum += acpi_checksum(&sdt, sizeof(ACPI_TABLE_HEADER));
1433 		sdt.Checksum -= sum;
1434 	}
1435 
1436 	/* Write out the DSDT header and body. */
1437 	write(fd, &sdt, sizeof(ACPI_TABLE_HEADER));
1438 	write(fd, dsdt + 1, dsdt->Length - sizeof(ACPI_TABLE_HEADER));
1439 
1440 	/* Write out any SSDTs (if present.) */
1441 	if (rsdt != NULL) {
1442 		ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
1443 		while (ssdt != NULL) {
1444 			write(fd, ssdt + 1, ssdt->Length -
1445 			    sizeof(ACPI_TABLE_HEADER));
1446 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
1447 		}
1448 	}
1449 	return (0);
1450 }
1451 
1452 void
1453 dsdt_save_file(char *outfile, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
1454 {
1455 	int	fd;
1456 	mode_t	mode;
1457 
1458 	assert(outfile != NULL);
1459 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1460 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
1461 	if (fd == -1) {
1462 		perror("dsdt_save_file");
1463 		return;
1464 	}
1465 	write_dsdt(fd, rsdt, dsdp);
1466 	close(fd);
1467 }
1468 
1469 void
1470 aml_disassemble(ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
1471 {
1472 	char buf[PATH_MAX], tmpstr[PATH_MAX], wrkdir[PATH_MAX];
1473 	const char *iname = "/acpdump.din";
1474 	const char *oname = "/acpdump.dsl";
1475 	const char *tmpdir;
1476 	FILE *fp;
1477 	size_t len;
1478 	int fd, status;
1479 	pid_t pid;
1480 
1481 	tmpdir = getenv("TMPDIR");
1482 	if (tmpdir == NULL)
1483 		tmpdir = _PATH_TMP;
1484 	if (realpath(tmpdir, buf) == NULL) {
1485 		perror("realpath tmp dir");
1486 		return;
1487 	}
1488 	len = sizeof(wrkdir) - strlen(iname);
1489 	if ((size_t)snprintf(wrkdir, len, "%s/acpidump.XXXXXX", buf) > len-1 ) {
1490 		fprintf(stderr, "$TMPDIR too long\n");
1491 		return;
1492 	}
1493 	if  (mkdtemp(wrkdir) == NULL) {
1494 		perror("mkdtemp tmp working dir");
1495 		return;
1496 	}
1497 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, iname);
1498 	assert(len <= sizeof(tmpstr) - 1);
1499 	fd = open(tmpstr, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
1500 	if (fd < 0) {
1501 		perror("iasl tmp file");
1502 		return;
1503 	}
1504 	write_dsdt(fd, rsdt, dsdp);
1505 	close(fd);
1506 
1507 	/* Run iasl -d on the temp file */
1508 	if ((pid = fork()) == 0) {
1509 		close(STDOUT_FILENO);
1510 		if (vflag == 0)
1511 			close(STDERR_FILENO);
1512 		execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
1513 		err(1, "exec");
1514 	}
1515 	if (pid > 0)
1516 		wait(&status);
1517 	if (unlink(tmpstr) < 0) {
1518 		perror("unlink");
1519 		goto out;
1520 	}
1521 	if (pid < 0) {
1522 		perror("fork");
1523 		goto out;
1524 	}
1525 	if (status != 0) {
1526 		fprintf(stderr, "iast exit status = %d\n", status);
1527 	}
1528 
1529 	/* Dump iasl's output to stdout */
1530 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, oname);
1531 	assert(len <= sizeof(tmpstr) - 1);
1532 	fp = fopen(tmpstr, "r");
1533 	if (unlink(tmpstr) < 0) {
1534 		perror("unlink");
1535 		goto out;
1536 	}
1537 	if (fp == NULL) {
1538 		perror("iasl tmp file (read)");
1539 		goto out;
1540 	}
1541 	while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
1542 		fwrite(buf, 1, len, stdout);
1543 	fclose(fp);
1544 
1545     out:
1546 	if (rmdir(wrkdir) < 0)
1547 		perror("rmdir");
1548 }
1549 
1550 void
1551 sdt_print_all(ACPI_TABLE_HEADER *rsdp)
1552 {
1553 	acpi_handle_rsdt(rsdp);
1554 }
1555 
1556 /* Fetch a table matching the given signature via the RSDT. */
1557 ACPI_TABLE_HEADER *
1558 sdt_from_rsdt(ACPI_TABLE_HEADER *rsdp, const char *sig, ACPI_TABLE_HEADER *last)
1559 {
1560 	ACPI_TABLE_HEADER *sdt;
1561 	ACPI_TABLE_RSDT *rsdt;
1562 	ACPI_TABLE_XSDT *xsdt;
1563 	vm_offset_t addr;
1564 	int entries, i;
1565 
1566 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
1567 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
1568 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
1569 	for (i = 0; i < entries; i++) {
1570 		if (addr_size == 4)
1571 			addr = le32toh(rsdt->TableOffsetEntry[i]);
1572 		else
1573 			addr = le64toh(xsdt->TableOffsetEntry[i]);
1574 		if (addr == 0)
1575 			continue;
1576 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
1577 		if (last != NULL) {
1578 			if (sdt == last)
1579 				last = NULL;
1580 			continue;
1581 		}
1582 		if (memcmp(sdt->Signature, sig, strlen(sig)))
1583 			continue;
1584 		if (acpi_checksum(sdt, sdt->Length))
1585 			errx(1, "RSDT entry %d is corrupt", i);
1586 		return (sdt);
1587 	}
1588 
1589 	return (NULL);
1590 }
1591 
1592 ACPI_TABLE_HEADER *
1593 dsdt_from_fadt(ACPI_TABLE_FADT *fadt)
1594 {
1595 	ACPI_TABLE_HEADER	*sdt;
1596 
1597 	/* Use the DSDT address if it is version 1, otherwise use XDSDT. */
1598 	if (acpi_get_fadt_revision(fadt) == 1)
1599 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
1600 	else
1601 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
1602 	if (acpi_checksum(sdt, sdt->Length))
1603 		errx(1, "DSDT is corrupt\n");
1604 	return (sdt);
1605 }
1606