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