xref: /freebsd/usr.sbin/acpi/acpidump/acpi.c (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 Doug Rabson
5  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
6  * Copyright (c) 2020 Alexander Motin <mav@FreeBSD.org>
7  * Copyright (c) 2024 The FreeBSD Foundation
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Konstantin Belousov
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/endian.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <assert.h>
40 #include <err.h>
41 #include <fcntl.h>
42 #include <paths.h>
43 #include <stdbool.h>
44 #include <stdio.h>
45 #include <stdint.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <uuid.h>
50 
51 #include "acpidump.h"
52 
53 #define BEGIN_COMMENT	"/*\n"
54 #define END_COMMENT	" */\n"
55 
56 static void	acpi_print_string(char *s, size_t length);
57 static void	acpi_print_gas(ACPI_GENERIC_ADDRESS *gas);
58 static int	acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt);
59 static void	acpi_handle_fadt(ACPI_TABLE_HEADER *fadt);
60 static void	acpi_print_cpu(u_char cpu_id);
61 static void	acpi_print_cpu_uid(uint32_t uid, char *uid_string);
62 static void	acpi_print_local_apic(uint32_t apic_id, uint32_t flags);
63 static void	acpi_print_io_apic(uint32_t apic_id, uint32_t int_base,
64 		    uint64_t apic_addr);
65 static void	acpi_print_mps_flags(uint16_t flags);
66 static void	acpi_print_intr(uint32_t intr, uint16_t mps_flags);
67 static void	acpi_print_local_nmi(u_int lint, uint16_t mps_flags);
68 static void	acpi_print_madt(ACPI_SUBTABLE_HEADER *mp);
69 static void	acpi_handle_madt(ACPI_TABLE_HEADER *sdp);
70 static void	acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp);
71 static void	acpi_handle_hpet(ACPI_TABLE_HEADER *sdp);
72 static void	acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp);
73 static void	acpi_handle_slit(ACPI_TABLE_HEADER *sdp);
74 static void	acpi_handle_wddt(ACPI_TABLE_HEADER *sdp);
75 static void	acpi_handle_lpit(ACPI_TABLE_HEADER *sdp);
76 static void	acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
77 		    uint32_t flags);
78 static void	acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp);
79 static void	acpi_print_srat(ACPI_SUBTABLE_HEADER *srat);
80 static void	acpi_handle_srat(ACPI_TABLE_HEADER *sdp);
81 static void	acpi_handle_tcpa(ACPI_TABLE_HEADER *sdp);
82 static void	acpi_print_nfit(ACPI_NFIT_HEADER *nfit);
83 static void	acpi_handle_nfit(ACPI_TABLE_HEADER *sdp);
84 static void	acpi_print_sdt(ACPI_TABLE_HEADER *sdp);
85 static void	acpi_print_fadt(ACPI_TABLE_HEADER *sdp);
86 static void	acpi_print_facs(ACPI_TABLE_FACS *facs);
87 static void	acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp);
88 static ACPI_TABLE_HEADER *acpi_map_sdt(vm_offset_t pa);
89 static void	acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp);
90 static void	acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp);
91 static void	acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
92 		    void (*action)(ACPI_SUBTABLE_HEADER *));
93 static void	acpi_walk_nfit(ACPI_TABLE_HEADER *table, void *first,
94 		    void (*action)(ACPI_NFIT_HEADER *));
95 
96 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
97 static int addr_size;
98 
99 /* Strings used in the TCPA table */
100 static const char *tcpa_event_type_strings[] = {
101 	"PREBOOT Certificate",
102 	"POST Code",
103 	"Unused",
104 	"No Action",
105 	"Separator",
106 	"Action",
107 	"Event Tag",
108 	"S-CRTM Contents",
109 	"S-CRTM Version",
110 	"CPU Microcode",
111 	"Platform Config Flags",
112 	"Table of Devices",
113 	"Compact Hash",
114 	"IPL",
115 	"IPL Partition Data",
116 	"Non-Host Code",
117 	"Non-Host Config",
118 	"Non-Host Info"
119 };
120 
121 static const char *TCPA_pcclient_strings[] = {
122 	"<undefined>",
123 	"SMBIOS",
124 	"BIS Certificate",
125 	"POST BIOS ROM Strings",
126 	"ESCD",
127 	"CMOS",
128 	"NVRAM",
129 	"Option ROM Execute",
130 	"Option ROM Configurateion",
131 	"<undefined>",
132 	"Option ROM Microcode Update ",
133 	"S-CRTM Version String",
134 	"S-CRTM Contents",
135 	"POST Contents",
136 	"Table of Devices",
137 };
138 
139 #define	PRINTFLAG_END()		printflag_end()
140 
141 static char pf_sep = '{';
142 
143 static void
144 printflag_end(void)
145 {
146 
147 	if (pf_sep != '{') {
148 		printf("}");
149 		pf_sep = '{';
150 	}
151 	printf("\n");
152 }
153 
154 static void
155 printflag(uint64_t var, uint64_t mask, const char *name)
156 {
157 
158 	if (var & mask) {
159 		printf("%c%s", pf_sep, name);
160 		pf_sep = ',';
161 	}
162 }
163 
164 static void
165 printfield(uint64_t var, int lbit, int hbit, const char *name)
166 {
167 	uint64_t mask;
168 	int len;
169 
170 	len = hbit - lbit + 1;
171 	mask = ((1 << (len + 1)) - 1) << lbit;
172 	printf("%c%s=%#jx", pf_sep, name, (uintmax_t)((var & mask) >> lbit));
173 	pf_sep = ',';
174 }
175 
176 static void
177 acpi_print_string(char *s, size_t length)
178 {
179 	int	c;
180 
181 	/* Trim trailing spaces and NULLs */
182 	while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
183 		length--;
184 
185 	while (length--) {
186 		c = *s++;
187 		putchar(c);
188 	}
189 }
190 
191 static void
192 acpi_print_gas(ACPI_GENERIC_ADDRESS *gas)
193 {
194 	switch(gas->SpaceId) {
195 	case ACPI_GAS_MEMORY:
196 		printf("0x%016jx:%u[%u] (Memory)", (uintmax_t)gas->Address,
197 		    gas->BitOffset, gas->BitWidth);
198 		break;
199 	case ACPI_GAS_IO:
200 		printf("0x%02jx:%u[%u] (IO)", (uintmax_t)gas->Address,
201 		    gas->BitOffset, gas->BitWidth);
202 		break;
203 	case ACPI_GAS_PCI:
204 		printf("%x:%x+0x%x:%u[%u] (PCI)", (uint16_t)(gas->Address >> 32),
205 		       (uint16_t)((gas->Address >> 16) & 0xffff),
206 		       (uint16_t)gas->Address, gas->BitOffset, gas->BitWidth);
207 		break;
208 	/* XXX How to handle these below? */
209 	case ACPI_GAS_EMBEDDED:
210 		printf("0x%x:%u[%u] (EC)", (uint16_t)gas->Address,
211 		       gas->BitOffset, gas->BitWidth);
212 		break;
213 	case ACPI_GAS_SMBUS:
214 		printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->Address,
215 		       gas->BitOffset, gas->BitWidth);
216 		break;
217 	case ACPI_GAS_CMOS:
218 	case ACPI_GAS_PCIBAR:
219 	case ACPI_GAS_DATATABLE:
220 	case ACPI_GAS_FIXED:
221 	default:
222 		printf("0x%016jx (?)", (uintmax_t)gas->Address);
223 		break;
224 	}
225 }
226 
227 /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
228 static int
229 acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt __unused)
230 {
231 	int fadt_revision;
232 
233 	/* Set the FADT revision separately from the RSDP version. */
234 	if (addr_size == 8) {
235 		fadt_revision = 2;
236 
237 #if defined(__i386__)
238 		/*
239 		 * A few systems (e.g., IBM T23) have an RSDP that claims
240 		 * revision 2 but the 64 bit addresses are invalid.  If
241 		 * revision 2 and the 32 bit address is non-zero but the
242 		 * 32 and 64 bit versions don't match, prefer the 32 bit
243 		 * version for all subsequent tables.
244 		 *
245 		 * The only known ACPI systems this affects are early
246 		 * implementations on 32-bit x86. Because of this limit the
247 		 * workaround to i386.
248 		 */
249 		if (fadt->Facs != 0 &&
250 		    (fadt->XFacs & 0xffffffff) != fadt->Facs)
251 			fadt_revision = 1;
252 #endif
253 	} else
254 		fadt_revision = 1;
255 	return (fadt_revision);
256 }
257 
258 static void
259 acpi_handle_fadt(ACPI_TABLE_HEADER *sdp)
260 {
261 	ACPI_TABLE_HEADER *dsdp;
262 	ACPI_TABLE_FACS	*facs;
263 	ACPI_TABLE_FADT *fadt;
264 	vm_offset_t	addr;
265 	int		fadt_revision;
266 
267 	fadt = (ACPI_TABLE_FADT *)sdp;
268 	acpi_print_fadt(sdp);
269 
270 	fadt_revision = acpi_get_fadt_revision(fadt);
271 	if (fadt_revision == 1)
272 		addr = fadt->Facs;
273 	else
274 		addr = fadt->XFacs;
275 	if (addr != 0) {
276 		facs = (ACPI_TABLE_FACS *)acpi_map_sdt(addr);
277 
278 		if (memcmp(facs->Signature, ACPI_SIG_FACS, 4) != 0 ||
279 		    facs->Length < 64)
280 			errx(1, "FACS is corrupt");
281 		acpi_print_facs(facs);
282 	}
283 
284 	if (fadt_revision == 1)
285 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
286 	else
287 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
288 	if (acpi_checksum(dsdp, dsdp->Length))
289 		errx(1, "DSDT is corrupt");
290 	acpi_print_dsdt(dsdp);
291 }
292 
293 static void
294 acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
295     void (*action)(ACPI_SUBTABLE_HEADER *))
296 {
297 	ACPI_SUBTABLE_HEADER *subtable;
298 	char *end;
299 
300 	subtable = first;
301 	end = (char *)table + table->Length;
302 	while ((char *)subtable < end) {
303 		printf("\n");
304 		if (subtable->Length < sizeof(ACPI_SUBTABLE_HEADER)) {
305 			warnx("invalid subtable length %u", subtable->Length);
306 			return;
307 		}
308 		action(subtable);
309 		subtable = (ACPI_SUBTABLE_HEADER *)((char *)subtable +
310 		    subtable->Length);
311 	}
312 }
313 
314 static void
315 acpi_walk_nfit(ACPI_TABLE_HEADER *table, void *first,
316     void (*action)(ACPI_NFIT_HEADER *))
317 {
318 	ACPI_NFIT_HEADER *subtable;
319 	char *end;
320 
321 	subtable = first;
322 	end = (char *)table + table->Length;
323 	while ((char *)subtable < end) {
324 		printf("\n");
325 		if (subtable->Length < sizeof(ACPI_NFIT_HEADER)) {
326 			warnx("invalid subtable length %u", subtable->Length);
327 			return;
328 		}
329 		action(subtable);
330 		subtable = (ACPI_NFIT_HEADER *)((char *)subtable +
331 		    subtable->Length);
332 	}
333 }
334 
335 static void
336 acpi_print_cpu(u_char cpu_id)
337 {
338 
339 	printf("\tACPI CPU=");
340 	if (cpu_id == 0xff)
341 		printf("ALL\n");
342 	else
343 		printf("%d\n", (u_int)cpu_id);
344 }
345 
346 static void
347 acpi_print_cpu_uid(uint32_t uid, char *uid_string)
348 {
349 
350 	printf("\tUID=%d", uid);
351 	if (uid_string != NULL)
352 		printf(" (%s)", uid_string);
353 	printf("\n");
354 }
355 
356 static void
357 acpi_print_local_apic(uint32_t apic_id, uint32_t flags)
358 {
359 
360 	printf("\tFlags={");
361 	if (flags & ACPI_MADT_ENABLED)
362 		printf("ENABLED");
363 	else
364 		printf("DISABLED");
365 	printf("}\n");
366 	printf("\tAPIC ID=%d\n", apic_id);
367 }
368 
369 static void
370 acpi_print_io_apic(uint32_t apic_id, uint32_t int_base, uint64_t apic_addr)
371 {
372 
373 	printf("\tAPIC ID=%d\n", apic_id);
374 	printf("\tINT BASE=%d\n", int_base);
375 	printf("\tADDR=0x%016jx\n", (uintmax_t)apic_addr);
376 }
377 
378 static void
379 acpi_print_mps_flags(uint16_t flags)
380 {
381 
382 	printf("\tFlags={Polarity=");
383 	switch (flags & ACPI_MADT_POLARITY_MASK) {
384 	case ACPI_MADT_POLARITY_CONFORMS:
385 		printf("conforming");
386 		break;
387 	case ACPI_MADT_POLARITY_ACTIVE_HIGH:
388 		printf("active-hi");
389 		break;
390 	case ACPI_MADT_POLARITY_ACTIVE_LOW:
391 		printf("active-lo");
392 		break;
393 	default:
394 		printf("0x%x", flags & ACPI_MADT_POLARITY_MASK);
395 		break;
396 	}
397 	printf(", Trigger=");
398 	switch (flags & ACPI_MADT_TRIGGER_MASK) {
399 	case ACPI_MADT_TRIGGER_CONFORMS:
400 		printf("conforming");
401 		break;
402 	case ACPI_MADT_TRIGGER_EDGE:
403 		printf("edge");
404 		break;
405 	case ACPI_MADT_TRIGGER_LEVEL:
406 		printf("level");
407 		break;
408 	default:
409 		printf("0x%x", (flags & ACPI_MADT_TRIGGER_MASK) >> 2);
410 	}
411 	printf("}\n");
412 }
413 
414 static void
415 acpi_print_gicc_flags(uint32_t flags)
416 {
417 
418 	printf("\tFlags={Performance intr=");
419 	if (flags & ACPI_MADT_PERFORMANCE_IRQ_MODE)
420 		printf("edge");
421 	else
422 		printf("level");
423 	printf(", VGIC intr=");
424 	if (flags & ACPI_MADT_VGIC_IRQ_MODE)
425 		printf("edge");
426 	else
427 		printf("level");
428 	printf("}\n");
429 }
430 
431 static void
432 acpi_print_intr(uint32_t intr, uint16_t mps_flags)
433 {
434 
435 	printf("\tINTR=%d\n", intr);
436 	acpi_print_mps_flags(mps_flags);
437 }
438 
439 static void
440 acpi_print_local_nmi(u_int lint, uint16_t mps_flags)
441 {
442 
443 	printf("\tLINT Pin=%d\n", lint);
444 	acpi_print_mps_flags(mps_flags);
445 }
446 
447 static const char *apic_types[] = {
448     [ACPI_MADT_TYPE_LOCAL_APIC] = "Local APIC",
449     [ACPI_MADT_TYPE_IO_APIC] = "IO APIC",
450     [ACPI_MADT_TYPE_INTERRUPT_OVERRIDE] = "INT Override",
451     [ACPI_MADT_TYPE_NMI_SOURCE] = "NMI",
452     [ACPI_MADT_TYPE_LOCAL_APIC_NMI] = "Local APIC NMI",
453     [ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE] = "Local APIC Override",
454     [ACPI_MADT_TYPE_IO_SAPIC] = "IO SAPIC",
455     [ACPI_MADT_TYPE_LOCAL_SAPIC] = "Local SAPIC",
456     [ACPI_MADT_TYPE_INTERRUPT_SOURCE] = "Platform Interrupt",
457     [ACPI_MADT_TYPE_LOCAL_X2APIC] = "Local X2APIC",
458     [ACPI_MADT_TYPE_LOCAL_X2APIC_NMI] = "Local X2APIC NMI",
459     [ACPI_MADT_TYPE_GENERIC_INTERRUPT] = "GIC CPU Interface Structure",
460     [ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR] = "GIC Distributor Structure",
461     [ACPI_MADT_TYPE_GENERIC_MSI_FRAME] = "GICv2m MSI Frame",
462     [ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR] = "GIC Redistributor Structure",
463     [ACPI_MADT_TYPE_GENERIC_TRANSLATOR] = "GIC ITS Structure"
464 };
465 
466 static const char *platform_int_types[] = { "0 (unknown)", "PMI", "INIT",
467 					    "Corrected Platform Error" };
468 
469 static void
470 acpi_print_madt(ACPI_SUBTABLE_HEADER *mp)
471 {
472 	ACPI_MADT_LOCAL_APIC *lapic;
473 	ACPI_MADT_IO_APIC *ioapic;
474 	ACPI_MADT_INTERRUPT_OVERRIDE *over;
475 	ACPI_MADT_NMI_SOURCE *nmi;
476 	ACPI_MADT_LOCAL_APIC_NMI *lapic_nmi;
477 	ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_over;
478 	ACPI_MADT_IO_SAPIC *iosapic;
479 	ACPI_MADT_LOCAL_SAPIC *lsapic;
480 	ACPI_MADT_INTERRUPT_SOURCE *isrc;
481 	ACPI_MADT_LOCAL_X2APIC *x2apic;
482 	ACPI_MADT_LOCAL_X2APIC_NMI *x2apic_nmi;
483 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
484 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
485 	ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
486 	ACPI_MADT_GENERIC_TRANSLATOR *gict;
487 
488 	if (mp->Type < nitems(apic_types))
489 		printf("\tType=%s\n", apic_types[mp->Type]);
490 	else
491 		printf("\tType=%d (unknown)\n", mp->Type);
492 	switch (mp->Type) {
493 	case ACPI_MADT_TYPE_LOCAL_APIC:
494 		lapic = (ACPI_MADT_LOCAL_APIC *)mp;
495 		acpi_print_cpu(lapic->ProcessorId);
496 		acpi_print_local_apic(lapic->Id, lapic->LapicFlags);
497 		break;
498 	case ACPI_MADT_TYPE_IO_APIC:
499 		ioapic = (ACPI_MADT_IO_APIC *)mp;
500 		acpi_print_io_apic(ioapic->Id, ioapic->GlobalIrqBase,
501 		    ioapic->Address);
502 		break;
503 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
504 		over = (ACPI_MADT_INTERRUPT_OVERRIDE *)mp;
505 		printf("\tBUS=%d\n", (u_int)over->Bus);
506 		printf("\tIRQ=%d\n", (u_int)over->SourceIrq);
507 		acpi_print_intr(over->GlobalIrq, over->IntiFlags);
508 		break;
509 	case ACPI_MADT_TYPE_NMI_SOURCE:
510 		nmi = (ACPI_MADT_NMI_SOURCE *)mp;
511 		acpi_print_intr(nmi->GlobalIrq, nmi->IntiFlags);
512 		break;
513 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
514 		lapic_nmi = (ACPI_MADT_LOCAL_APIC_NMI *)mp;
515 		acpi_print_cpu(lapic_nmi->ProcessorId);
516 		acpi_print_local_nmi(lapic_nmi->Lint, lapic_nmi->IntiFlags);
517 		break;
518 	case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
519 		lapic_over = (ACPI_MADT_LOCAL_APIC_OVERRIDE *)mp;
520 		printf("\tLocal APIC ADDR=0x%016jx\n",
521 		    (uintmax_t)lapic_over->Address);
522 		break;
523 	case ACPI_MADT_TYPE_IO_SAPIC:
524 		iosapic = (ACPI_MADT_IO_SAPIC *)mp;
525 		acpi_print_io_apic(iosapic->Id, iosapic->GlobalIrqBase,
526 		    iosapic->Address);
527 		break;
528 	case ACPI_MADT_TYPE_LOCAL_SAPIC:
529 		lsapic = (ACPI_MADT_LOCAL_SAPIC *)mp;
530 		acpi_print_cpu(lsapic->ProcessorId);
531 		acpi_print_local_apic(lsapic->Id, lsapic->LapicFlags);
532 		printf("\tAPIC EID=%d\n", (u_int)lsapic->Eid);
533 		if (mp->Length > __offsetof(ACPI_MADT_LOCAL_SAPIC, Uid))
534 			acpi_print_cpu_uid(lsapic->Uid, lsapic->UidString);
535 		break;
536 	case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
537 		isrc = (ACPI_MADT_INTERRUPT_SOURCE *)mp;
538 		if (isrc->Type < nitems(platform_int_types))
539 			printf("\tType=%s\n", platform_int_types[isrc->Type]);
540 		else
541 			printf("\tType=%d (unknown)\n", isrc->Type);
542 		printf("\tAPIC ID=%d\n", (u_int)isrc->Id);
543 		printf("\tAPIC EID=%d\n", (u_int)isrc->Eid);
544 		printf("\tSAPIC Vector=%d\n", (u_int)isrc->IoSapicVector);
545 		acpi_print_intr(isrc->GlobalIrq, isrc->IntiFlags);
546 		break;
547 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
548 		x2apic = (ACPI_MADT_LOCAL_X2APIC *)mp;
549 		acpi_print_cpu_uid(x2apic->Uid, NULL);
550 		acpi_print_local_apic(x2apic->LocalApicId, x2apic->LapicFlags);
551 		break;
552 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
553 		x2apic_nmi = (ACPI_MADT_LOCAL_X2APIC_NMI *)mp;
554 		acpi_print_cpu_uid(x2apic_nmi->Uid, NULL);
555 		acpi_print_local_nmi(x2apic_nmi->Lint, x2apic_nmi->IntiFlags);
556 		break;
557 	case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
558 		gicc = (ACPI_MADT_GENERIC_INTERRUPT *)mp;
559 		acpi_print_cpu_uid(gicc->Uid, NULL);
560 		printf("\tCPU INTERFACE=%x\n", gicc->CpuInterfaceNumber);
561 		acpi_print_gicc_flags(gicc->Flags);
562 		printf("\tParking Protocol Version=%x\n", gicc->ParkingVersion);
563 		printf("\tPERF INTR=%d\n", gicc->PerformanceInterrupt);
564 		printf("\tParked ADDR=%016jx\n",
565 		    (uintmax_t)gicc->ParkedAddress);
566 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gicc->BaseAddress);
567 		printf("\tGICV=%016jx\n", (uintmax_t)gicc->GicvBaseAddress);
568 		printf("\tGICH=%016jx\n", (uintmax_t)gicc->GichBaseAddress);
569 		printf("\tVGIC INTR=%d\n", gicc->VgicInterrupt);
570 		printf("\tGICR ADDR=%016jx\n",
571 		    (uintmax_t)gicc->GicrBaseAddress);
572 		printf("\tMPIDR=%jx\n", (uintmax_t)gicc->ArmMpidr);
573 		printf("\tEfficiency Class=%d\n", (u_int)gicc->EfficiencyClass);
574 		printf("\tSPE INTR=%d\n", gicc->SpeInterrupt);
575 		break;
576 	case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
577 		gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)mp;
578 		printf("\tGIC ID=%d\n", (u_int)gicd->GicId);
579 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gicd->BaseAddress);
580 		printf("\tVector Base=%d\n", gicd->GlobalIrqBase);
581 		printf("\tGIC VERSION=%d\n", (u_int)gicd->Version);
582 		break;
583 	case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
584 		gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)mp;
585 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gicr->BaseAddress);
586 		printf("\tLength=%08x\n", gicr->Length);
587 		break;
588 	case ACPI_MADT_TYPE_GENERIC_TRANSLATOR:
589 		gict = (ACPI_MADT_GENERIC_TRANSLATOR *)mp;
590 		printf("\tGIC ITS ID=%d\n", gict->TranslationId);
591 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gict->BaseAddress);
592 		break;
593 	}
594 }
595 
596 static void
597 acpi_handle_madt(ACPI_TABLE_HEADER *sdp)
598 {
599 	ACPI_TABLE_MADT *madt;
600 
601 	printf(BEGIN_COMMENT);
602 	acpi_print_sdt(sdp);
603 	madt = (ACPI_TABLE_MADT *)sdp;
604 	printf("\tLocal APIC ADDR=0x%08x\n", madt->Address);
605 	printf("\tFlags={");
606 	if (madt->Flags & ACPI_MADT_PCAT_COMPAT)
607 		printf("PC-AT");
608 	printf("}\n");
609 	acpi_walk_subtables(sdp, (madt + 1), acpi_print_madt);
610 	printf(END_COMMENT);
611 }
612 
613 static void
614 acpi_handle_bert(ACPI_TABLE_HEADER *sdp)
615 {
616 	ACPI_TABLE_BERT *bert;
617 
618 	printf(BEGIN_COMMENT);
619 	acpi_print_sdt(sdp);
620 	bert = (ACPI_TABLE_BERT *)sdp;
621 	printf("\tRegionLength=%d\n", bert->RegionLength);
622 	printf("\tAddress=0x%016jx\n", bert->Address);
623 	printf(END_COMMENT);
624 }
625 
626 static void
627 acpi_print_whea(ACPI_WHEA_HEADER *w)
628 {
629 
630 	printf("\n\tAction=%d\n", w->Action);
631 	printf("\tInstruction=%d\n", w->Instruction);
632 	printf("\tFlags=%02x\n", w->Flags);
633 	printf("\tRegisterRegion=");
634 	acpi_print_gas(&w->RegisterRegion);
635 	printf("\n\tValue=0x%016jx\n", w->Value);
636 	printf("\tMask=0x%016jx\n", w->Mask);
637 }
638 
639 static void
640 acpi_handle_einj(ACPI_TABLE_HEADER *sdp)
641 {
642 	ACPI_TABLE_EINJ *einj;
643 	ACPI_WHEA_HEADER *w;
644 	u_int i;
645 
646 	printf(BEGIN_COMMENT);
647 	acpi_print_sdt(sdp);
648 	einj = (ACPI_TABLE_EINJ *)sdp;
649 	printf("\tHeaderLength=%d\n", einj->HeaderLength);
650 	printf("\tFlags=0x%02x\n", einj->Flags);
651 	printf("\tEntries=%d\n", einj->Entries);
652 	w = (ACPI_WHEA_HEADER *)(einj + 1);
653 	for (i = 0; i < MIN(einj->Entries, (sdp->Length -
654 	    sizeof(ACPI_TABLE_EINJ)) / sizeof(ACPI_WHEA_HEADER)); i++)
655 		acpi_print_whea(w + i);
656 	printf(END_COMMENT);
657 }
658 
659 static void
660 acpi_handle_erst(ACPI_TABLE_HEADER *sdp)
661 {
662 	ACPI_TABLE_ERST *erst;
663 	ACPI_WHEA_HEADER *w;
664 	u_int i;
665 
666 	printf(BEGIN_COMMENT);
667 	acpi_print_sdt(sdp);
668 	erst = (ACPI_TABLE_ERST *)sdp;
669 	printf("\tHeaderLength=%d\n", erst->HeaderLength);
670 	printf("\tEntries=%d\n", erst->Entries);
671 	w = (ACPI_WHEA_HEADER *)(erst + 1);
672 	for (i = 0; i < MIN(erst->Entries, (sdp->Length -
673 	    sizeof(ACPI_TABLE_ERST)) / sizeof(ACPI_WHEA_HEADER)); i++)
674 		acpi_print_whea(w + i);
675 	printf(END_COMMENT);
676 }
677 
678 static void
679 acpi_print_hest_bank(ACPI_HEST_IA_ERROR_BANK *b)
680 {
681 
682 	printf("\tBank:\n");
683 	printf("\t\tBankNumber=%d\n", b->BankNumber);
684 	printf("\t\tClearStatusOnInit=%d\n", b->ClearStatusOnInit);
685 	printf("\t\tStatusFormat=%d\n", b->StatusFormat);
686 	printf("\t\tControlRegister=%x\n", b->ControlRegister);
687 	printf("\t\tControlData=%jx\n", b->ControlData);
688 	printf("\t\tStatusRegister=%x\n", b->StatusRegister);
689 	printf("\t\tAddressRegister=%x\n", b->AddressRegister);
690 	printf("\t\tMiscRegister=%x\n", b->MiscRegister);
691 }
692 
693 static void
694 acpi_print_hest_notify(ACPI_HEST_NOTIFY *n)
695 {
696 
697 	printf("\t\tType=%d\n", n->Type);
698 	printf("\t\tLength=%d\n", n->Length);
699 	printf("\t\tConfigWriteEnable=%04x\n", n->ConfigWriteEnable);
700 	printf("\t\tPollInterval=%d\n", n->PollInterval);
701 	printf("\t\tVector=%d\n", n->Vector);
702 	printf("\t\tPollingThresholdValue=%d\n", n->PollingThresholdValue);
703 	printf("\t\tPollingThresholdWindow=%d\n", n->PollingThresholdWindow);
704 	printf("\t\tErrorThresholdValue=%d\n", n->ErrorThresholdValue);
705 	printf("\t\tErrorThresholdWindow=%d\n", n->ErrorThresholdWindow);
706 }
707 
708 static void
709 acpi_print_hest_aer(ACPI_HEST_AER_COMMON *a)
710 {
711 
712 	printf("\tFlags=%02x\n", a->Flags);
713 	printf("\tEnabled=%d\n", a->Enabled);
714 	printf("\tRecordsToPreallocate=%d\n", a->RecordsToPreallocate);
715 	printf("\tMaxSectionsPerRecord=%d\n", a->MaxSectionsPerRecord);
716 	printf("\tBus=%d\n", a->Bus);
717 	printf("\tDevice=%d\n", a->Device);
718 	printf("\tFunction=%d\n", a->Function);
719 	printf("\tDeviceControl=%d\n", a->DeviceControl);
720 	printf("\tUncorrectableMask=%d\n", a->UncorrectableMask);
721 	printf("\tUncorrectableSeverity=%d\n", a->UncorrectableSeverity);
722 	printf("\tCorrectableMask=%d\n", a->CorrectableMask);
723 	printf("\tAdvancedCapabilities=%d\n", a->AdvancedCapabilities);
724 }
725 
726 static int
727 acpi_handle_hest_structure(void *addr, int remaining)
728 {
729 	ACPI_HEST_HEADER *hdr = addr;
730 	int i;
731 
732 	if (remaining < (int)sizeof(ACPI_HEST_HEADER))
733 		return (-1);
734 
735 	printf("\n\tType=%d\n", hdr->Type);
736 	printf("\tSourceId=%d\n", hdr->SourceId);
737 	switch (hdr->Type) {
738 	case ACPI_HEST_TYPE_IA32_CHECK: {
739 		ACPI_HEST_IA_MACHINE_CHECK *s = addr;
740 		printf("\tFlags=%02x\n", s->Flags);
741 		printf("\tEnabled=%d\n", s->Enabled);
742 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
743 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
744 		printf("\tGlobalCapabilityData=%jd\n", s->GlobalCapabilityData);
745 		printf("\tGlobalControlData=%jd\n", s->GlobalControlData);
746 		printf("\tNumHardwareBanks=%d\n", s->NumHardwareBanks);
747 		for (i = 0; i < s->NumHardwareBanks; i++) {
748 			acpi_print_hest_bank((ACPI_HEST_IA_ERROR_BANK *)
749 			    (s + 1) + i);
750 		}
751 		return (sizeof(*s) + s->NumHardwareBanks *
752 		    sizeof(ACPI_HEST_IA_ERROR_BANK));
753 	}
754 	case ACPI_HEST_TYPE_IA32_CORRECTED_CHECK: {
755 		ACPI_HEST_IA_CORRECTED *s = addr;
756 		printf("\tFlags=%02x\n", s->Flags);
757 		printf("\tEnabled=%d\n", s->Enabled);
758 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
759 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
760 		printf("\tNotify:\n");
761 		acpi_print_hest_notify(&s->Notify);
762 		printf("\tNumHardwareBanks=%d\n", s->NumHardwareBanks);
763 		for (i = 0; i < s->NumHardwareBanks; i++) {
764 			acpi_print_hest_bank((ACPI_HEST_IA_ERROR_BANK *)
765 			    (s + 1) + i);
766 		}
767 		return (sizeof(*s) + s->NumHardwareBanks *
768 		    sizeof(ACPI_HEST_IA_ERROR_BANK));
769 	}
770 	case ACPI_HEST_TYPE_IA32_NMI: {
771 		ACPI_HEST_IA_NMI *s = addr;
772 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
773 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
774 		printf("\tMaxRawDataLength=%d\n", s->MaxRawDataLength);
775 		return (sizeof(*s));
776 	}
777 	case ACPI_HEST_TYPE_AER_ROOT_PORT: {
778 		ACPI_HEST_AER_ROOT *s = addr;
779 		acpi_print_hest_aer(&s->Aer);
780 		printf("\tRootErrorCommand=%d\n", s->RootErrorCommand);
781 		return (sizeof(*s));
782 	}
783 	case ACPI_HEST_TYPE_AER_ENDPOINT: {
784 		ACPI_HEST_AER *s = addr;
785 		acpi_print_hest_aer(&s->Aer);
786 		return (sizeof(*s));
787 	}
788 	case ACPI_HEST_TYPE_AER_BRIDGE: {
789 		ACPI_HEST_AER_BRIDGE *s = addr;
790 		acpi_print_hest_aer(&s->Aer);
791 		printf("\tUncorrectableMask2=%d\n", s->UncorrectableMask2);
792 		printf("\tUncorrectableSeverity2=%d\n", s->UncorrectableSeverity2);
793 		printf("\tAdvancedCapabilities2=%d\n", s->AdvancedCapabilities2);
794 		return (sizeof(*s));
795 	}
796 	case ACPI_HEST_TYPE_GENERIC_ERROR: {
797 		ACPI_HEST_GENERIC *s = addr;
798 		printf("\tRelatedSourceId=%d\n", s->RelatedSourceId);
799 		printf("\tEnabled=%d\n", s->Enabled);
800 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
801 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
802 		printf("\tMaxRawDataLength=%d\n", s->MaxRawDataLength);
803 		printf("\tErrorStatusAddress=");
804 		acpi_print_gas(&s->ErrorStatusAddress);
805 		printf("\n");
806 		printf("\tNotify:\n");
807 		acpi_print_hest_notify(&s->Notify);
808 		printf("\tErrorBlockLength=%d\n", s->ErrorBlockLength);
809 		return (sizeof(*s));
810 	}
811 	case ACPI_HEST_TYPE_GENERIC_ERROR_V2: {
812 		ACPI_HEST_GENERIC_V2 *s = addr;
813 		printf("\tRelatedSourceId=%d\n", s->RelatedSourceId);
814 		printf("\tEnabled=%d\n", s->Enabled);
815 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
816 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
817 		printf("\tMaxRawDataLength=%d\n", s->MaxRawDataLength);
818 		printf("\tErrorStatusAddress=");
819 		acpi_print_gas(&s->ErrorStatusAddress);
820 		printf("\n");
821 		printf("\tNotify:\n");
822 		acpi_print_hest_notify(&s->Notify);
823 		printf("\tErrorBlockLength=%d\n", s->ErrorBlockLength);
824 		printf("\tReadAckRegister=");
825 		acpi_print_gas(&s->ReadAckRegister);
826 		printf("\n");
827 		printf("\tReadAckPreserve=%jd\n", s->ReadAckPreserve);
828 		printf("\tReadAckWrite=%jd\n", s->ReadAckWrite);
829 		return (sizeof(*s));
830 	}
831 	case ACPI_HEST_TYPE_IA32_DEFERRED_CHECK: {
832 		ACPI_HEST_IA_DEFERRED_CHECK *s = addr;
833 		printf("\tFlags=%02x\n", s->Flags);
834 		printf("\tEnabled=%d\n", s->Enabled);
835 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
836 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
837 		printf("\tNotify:\n");
838 		acpi_print_hest_notify(&s->Notify);
839 		printf("\tNumHardwareBanks=%d\n", s->NumHardwareBanks);
840 		for (i = 0; i < s->NumHardwareBanks; i++) {
841 			acpi_print_hest_bank((ACPI_HEST_IA_ERROR_BANK *)
842 			    (s + 1) + i);
843 		}
844 		return (sizeof(*s) + s->NumHardwareBanks *
845 		    sizeof(ACPI_HEST_IA_ERROR_BANK));
846 	}
847 	default:
848 		return (-1);
849 	}
850 }
851 
852 static void
853 acpi_handle_hest(ACPI_TABLE_HEADER *sdp)
854 {
855 	char *cp;
856 	int remaining, consumed;
857 	ACPI_TABLE_HEST *hest;
858 
859 	printf(BEGIN_COMMENT);
860 	acpi_print_sdt(sdp);
861 	hest = (ACPI_TABLE_HEST *)sdp;
862 	printf("\tErrorSourceCount=%d\n", hest->ErrorSourceCount);
863 
864 	remaining = sdp->Length - sizeof(ACPI_TABLE_HEST);
865 	while (remaining > 0) {
866 		cp = (char *)sdp + sdp->Length - remaining;
867 		consumed = acpi_handle_hest_structure(cp, remaining);
868 		if (consumed <= 0)
869 			break;
870 		else
871 			remaining -= consumed;
872 	}
873 	printf(END_COMMENT);
874 }
875 
876 static void
877 acpi_handle_hpet(ACPI_TABLE_HEADER *sdp)
878 {
879 	ACPI_TABLE_HPET *hpet;
880 
881 	printf(BEGIN_COMMENT);
882 	acpi_print_sdt(sdp);
883 	hpet = (ACPI_TABLE_HPET *)sdp;
884 	printf("\tHPET Number=%d\n", hpet->Sequence);
885 	printf("\tADDR=");
886 	acpi_print_gas(&hpet->Address);
887 	printf("\n\tHW Rev=0x%x\n", hpet->Id & ACPI_HPET_ID_HARDWARE_REV_ID);
888 	printf("\tComparators=%d\n", (hpet->Id & ACPI_HPET_ID_COMPARATORS) >>
889 	    8);
890 	printf("\tCounter Size=%d\n", hpet->Id & ACPI_HPET_ID_COUNT_SIZE_CAP ?
891 	    1 : 0);
892 	printf("\tLegacy IRQ routing capable={");
893 	if (hpet->Id & ACPI_HPET_ID_LEGACY_CAPABLE)
894 		printf("TRUE}\n");
895 	else
896 		printf("FALSE}\n");
897 	printf("\tPCI Vendor ID=0x%04x\n", hpet->Id >> 16);
898 	printf("\tMinimal Tick=%d\n", hpet->MinimumTick);
899 	printf("\tFlags=0x%02x\n", hpet->Flags);
900 	printf(END_COMMENT);
901 }
902 
903 static void
904 acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp)
905 {
906 	ACPI_TABLE_ECDT *ecdt;
907 
908 	printf(BEGIN_COMMENT);
909 	acpi_print_sdt(sdp);
910 	ecdt = (ACPI_TABLE_ECDT *)sdp;
911 	printf("\tEC_CONTROL=");
912 	acpi_print_gas(&ecdt->Control);
913 	printf("\n\tEC_DATA=");
914 	acpi_print_gas(&ecdt->Data);
915 	printf("\n\tUID=%#x, ", ecdt->Uid);
916 	printf("GPE_BIT=%#x\n", ecdt->Gpe);
917 	printf("\tEC_ID=%s\n", ecdt->Id);
918 	printf(END_COMMENT);
919 }
920 
921 static void
922 acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp)
923 {
924 	ACPI_TABLE_MCFG *mcfg;
925 	ACPI_MCFG_ALLOCATION *alloc;
926 	u_int i, entries;
927 
928 	printf(BEGIN_COMMENT);
929 	acpi_print_sdt(sdp);
930 	mcfg = (ACPI_TABLE_MCFG *)sdp;
931 	entries = (sdp->Length - sizeof(ACPI_TABLE_MCFG)) /
932 	    sizeof(ACPI_MCFG_ALLOCATION);
933 	alloc = (ACPI_MCFG_ALLOCATION *)(mcfg + 1);
934 	for (i = 0; i < entries; i++, alloc++) {
935 		printf("\n");
936 		printf("\tBase Address=0x%016jx\n", (uintmax_t)alloc->Address);
937 		printf("\tSegment Group=0x%04x\n", alloc->PciSegment);
938 		printf("\tStart Bus=%d\n", alloc->StartBusNumber);
939 		printf("\tEnd Bus=%d\n", alloc->EndBusNumber);
940 	}
941 	printf(END_COMMENT);
942 }
943 
944 static void
945 acpi_handle_slit(ACPI_TABLE_HEADER *sdp)
946 {
947 	ACPI_TABLE_SLIT *slit;
948 	UINT64 i, j;
949 
950 	printf(BEGIN_COMMENT);
951 	acpi_print_sdt(sdp);
952 	slit = (ACPI_TABLE_SLIT *)sdp;
953 	printf("\tLocality Count=%ju\n", (uintmax_t)slit->LocalityCount);
954 	printf("\n\t      ");
955 	for (i = 0; i < slit->LocalityCount; i++)
956 		printf(" %3ju", (uintmax_t)i);
957 	printf("\n\t     +");
958 	for (i = 0; i < slit->LocalityCount; i++)
959 		printf("----");
960 	printf("\n");
961 	for (i = 0; i < slit->LocalityCount; i++) {
962 		printf("\t %3ju |", (uintmax_t)i);
963 		for (j = 0; j < slit->LocalityCount; j++)
964 			printf(" %3d",
965 			    slit->Entry[i * slit->LocalityCount + j]);
966 		printf("\n");
967 	}
968 	printf(END_COMMENT);
969 }
970 
971 static void
972 acpi_handle_wddt(ACPI_TABLE_HEADER *sdp)
973 {
974 	ACPI_TABLE_WDDT *wddt;
975 
976 	printf(BEGIN_COMMENT);
977 	acpi_print_sdt(sdp);
978 	wddt = (ACPI_TABLE_WDDT *)sdp;
979 	printf("\tSpecVersion=0x%04x, TableVersion=0x%04x\n",
980 	    wddt->SpecVersion, wddt->TableVersion);
981 	printf("\tPciVendorId=0x%04x, Address=", wddt->PciVendorId);
982 	acpi_print_gas(&wddt->Address);
983 	printf("\n\tMaxCount=%u, MinCount=%u, Period=%ums\n",
984 	    wddt->MaxCount, wddt->MinCount, wddt->Period);
985 
986 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_WDDT_## flag, #flag)
987 	printf("\tStatus=");
988 	PRINTFLAG(wddt->Status, AVAILABLE);
989 	PRINTFLAG(wddt->Status, ACTIVE);
990 	PRINTFLAG(wddt->Status, TCO_OS_OWNED);
991 	PRINTFLAG(wddt->Status, USER_RESET);
992 	PRINTFLAG(wddt->Status, WDT_RESET);
993 	PRINTFLAG(wddt->Status, POWER_FAIL);
994 	PRINTFLAG(wddt->Status, UNKNOWN_RESET);
995 	PRINTFLAG_END();
996 	printf("\tCapability=");
997 	PRINTFLAG(wddt->Capability, AUTO_RESET);
998 	PRINTFLAG(wddt->Capability, ALERT_SUPPORT);
999 	PRINTFLAG_END();
1000 #undef PRINTFLAG
1001 
1002 	printf(END_COMMENT);
1003 }
1004 
1005 static void
1006 acpi_print_native_lpit(ACPI_LPIT_NATIVE *nl)
1007 {
1008 	printf("\tEntryTrigger=");
1009 	acpi_print_gas(&nl->EntryTrigger);
1010 	printf("\n\tResidency=%u\n", nl->Residency);
1011 	printf("\tLatency=%u\n", nl->Latency);
1012 	if (nl->Header.Flags & ACPI_LPIT_NO_COUNTER)
1013 		printf("\tResidencyCounter=Not Present");
1014 	else {
1015 		printf("\tResidencyCounter=");
1016 		acpi_print_gas(&nl->ResidencyCounter);
1017 		printf("\n");
1018 	}
1019 	if (nl->CounterFrequency)
1020 		printf("\tCounterFrequency=%ju\n", nl->CounterFrequency);
1021 	else
1022 		printf("\tCounterFrequency=TSC\n");
1023 }
1024 
1025 static void
1026 acpi_print_lpit(ACPI_LPIT_HEADER *lpit)
1027 {
1028 	if (lpit->Type == ACPI_LPIT_TYPE_NATIVE_CSTATE)
1029 		printf("\tType=ACPI_LPIT_TYPE_NATIVE_CSTATE\n");
1030 	else
1031 		warnx("unknown LPIT type %u", lpit->Type);
1032 
1033 	printf("\tLength=%u\n", lpit->Length);
1034 	printf("\tUniqueId=0x%04x\n", lpit->UniqueId);
1035 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_LPIT_## flag, #flag)
1036 	printf("\tFlags=");
1037 	PRINTFLAG(lpit->Flags, STATE_DISABLED);
1038 	PRINTFLAG_END();
1039 #undef PRINTFLAG
1040 
1041 	if (lpit->Type == ACPI_LPIT_TYPE_NATIVE_CSTATE)
1042 		return acpi_print_native_lpit((ACPI_LPIT_NATIVE *)lpit);
1043 }
1044 
1045 static void
1046 acpi_walk_lpit(ACPI_TABLE_HEADER *table, void *first,
1047     void (*action)(ACPI_LPIT_HEADER *))
1048 {
1049 	ACPI_LPIT_HEADER *subtable;
1050 	char *end;
1051 
1052 	subtable = first;
1053 	end = (char *)table + table->Length;
1054 	while ((char *)subtable < end) {
1055 		printf("\n");
1056 		if (subtable->Length < sizeof(ACPI_LPIT_HEADER)) {
1057 			warnx("invalid subtable length %u", subtable->Length);
1058 			return;
1059 		}
1060 		action(subtable);
1061 		subtable = (ACPI_LPIT_HEADER *)((char *)subtable +
1062 		    subtable->Length);
1063 	}
1064 }
1065 
1066 static void
1067 acpi_handle_lpit(ACPI_TABLE_HEADER *sdp)
1068 {
1069 	ACPI_TABLE_LPIT *lpit;
1070 
1071 	printf(BEGIN_COMMENT);
1072 	acpi_print_sdt(sdp);
1073 	lpit = (ACPI_TABLE_LPIT *)sdp;
1074 	acpi_walk_lpit(sdp, (lpit + 1), acpi_print_lpit);
1075 
1076 	printf(END_COMMENT);
1077 }
1078 
1079 static void
1080 acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
1081     uint32_t flags)
1082 {
1083 
1084 	printf("\tFlags={");
1085 	if (flags & ACPI_SRAT_CPU_ENABLED)
1086 		printf("ENABLED");
1087 	else
1088 		printf("DISABLED");
1089 	printf("}\n");
1090 	printf("\tAPIC ID=%d\n", apic_id);
1091 	printf("\tProximity Domain=%d\n", proximity_domain);
1092 }
1093 
1094 static char *
1095 acpi_tcpa_evname(struct TCPAevent *event)
1096 {
1097 	struct TCPApc_event *pc_event;
1098 	char *eventname = NULL;
1099 
1100 	pc_event = (struct TCPApc_event *)(event + 1);
1101 
1102 	switch(event->event_type) {
1103 	case PREBOOT:
1104 	case POST_CODE:
1105 	case UNUSED:
1106 	case NO_ACTION:
1107 	case SEPARATOR:
1108 	case SCRTM_CONTENTS:
1109 	case SCRTM_VERSION:
1110 	case CPU_MICROCODE:
1111 	case PLATFORM_CONFIG_FLAGS:
1112 	case TABLE_OF_DEVICES:
1113 	case COMPACT_HASH:
1114 	case IPL:
1115 	case IPL_PARTITION_DATA:
1116 	case NONHOST_CODE:
1117 	case NONHOST_CONFIG:
1118 	case NONHOST_INFO:
1119 		asprintf(&eventname, "%s",
1120 		    tcpa_event_type_strings[event->event_type]);
1121 		break;
1122 
1123 	case ACTION:
1124 		eventname = calloc(event->event_size + 1, sizeof(char));
1125 		memcpy(eventname, pc_event, event->event_size);
1126 		break;
1127 
1128 	case EVENT_TAG:
1129 		switch (pc_event->event_id) {
1130 		case SMBIOS:
1131 		case BIS_CERT:
1132 		case CMOS:
1133 		case NVRAM:
1134 		case OPTION_ROM_EXEC:
1135 		case OPTION_ROM_CONFIG:
1136 		case S_CRTM_VERSION:
1137 		case POST_BIOS_ROM:
1138 		case ESCD:
1139 		case OPTION_ROM_MICROCODE:
1140 		case S_CRTM_CONTENTS:
1141 		case POST_CONTENTS:
1142 			asprintf(&eventname, "%s",
1143 			    TCPA_pcclient_strings[pc_event->event_id]);
1144 			break;
1145 
1146 		default:
1147 			asprintf(&eventname, "<unknown tag 0x%02x>",
1148 			    pc_event->event_id);
1149 			break;
1150 		}
1151 		break;
1152 
1153 	default:
1154 		asprintf(&eventname, "<unknown 0x%02x>", event->event_type);
1155 		break;
1156 	}
1157 
1158 	return eventname;
1159 }
1160 
1161 static void
1162 acpi_print_tcpa(struct TCPAevent *event)
1163 {
1164 	int i;
1165 	char *eventname;
1166 
1167 	eventname = acpi_tcpa_evname(event);
1168 
1169 	printf("\t%d", event->pcr_index);
1170 	printf(" 0x");
1171 	for (i = 0; i < 20; i++)
1172 		printf("%02x", event->pcr_value[i]);
1173 	printf(" [%s]\n", eventname ? eventname : "<unknown>");
1174 
1175 	free(eventname);
1176 }
1177 
1178 static void
1179 acpi_handle_tcpa(ACPI_TABLE_HEADER *sdp)
1180 {
1181 	struct TCPAbody *tcpa;
1182 	struct TCPAevent *event;
1183 	uintmax_t len, paddr;
1184 	unsigned char *vaddr = NULL;
1185 	unsigned char *vend = NULL;
1186 
1187 	printf(BEGIN_COMMENT);
1188 	acpi_print_sdt(sdp);
1189 	tcpa = (struct TCPAbody *) sdp;
1190 
1191 	switch (tcpa->platform_class) {
1192 	case ACPI_TCPA_BIOS_CLIENT:
1193 		len = tcpa->client.log_max_len;
1194 		paddr = tcpa->client.log_start_addr;
1195 		break;
1196 
1197 	case ACPI_TCPA_BIOS_SERVER:
1198 		len = tcpa->server.log_max_len;
1199 		paddr = tcpa->server.log_start_addr;
1200 		break;
1201 
1202 	default:
1203 		printf("XXX");
1204 		printf(END_COMMENT);
1205 		return;
1206 	}
1207 	printf("\tClass %u Base Address 0x%jx Length %ju\n\n",
1208 	    tcpa->platform_class, paddr, len);
1209 
1210 	if (len == 0) {
1211 		printf("\tEmpty TCPA table\n");
1212 		printf(END_COMMENT);
1213 		return;
1214 	}
1215 	if(sdp->Revision == 1){
1216 		printf("\tOLD TCPA spec log found. Dumping not supported.\n");
1217 		printf(END_COMMENT);
1218 		return;
1219 	}
1220 
1221 	vaddr = (unsigned char *)acpi_map_physical(paddr, len);
1222 	vend = vaddr + len;
1223 
1224 	while (vaddr != NULL) {
1225 		if ((vaddr + sizeof(struct TCPAevent) >= vend)||
1226 		    (vaddr + sizeof(struct TCPAevent) < vaddr))
1227 			break;
1228 		event = (struct TCPAevent *)(void *)vaddr;
1229 		if (vaddr + event->event_size >= vend)
1230 			break;
1231 		if (vaddr + event->event_size < vaddr)
1232 			break;
1233 		if (event->event_type == 0 && event->event_size == 0)
1234 			break;
1235 #if 0
1236 		{
1237 		unsigned int i, j, k;
1238 
1239 		printf("\n\tsize %d\n\t\t%p ", event->event_size, vaddr);
1240 		for (j = 0, i = 0; i <
1241 		    sizeof(struct TCPAevent) + event->event_size; i++) {
1242 			printf("%02x ", vaddr[i]);
1243 			if ((i+1) % 8 == 0) {
1244 				for (k = 0; k < 8; k++)
1245 					printf("%c", isprint(vaddr[j+k]) ?
1246 					    vaddr[j+k] : '.');
1247 				printf("\n\t\t%p ", &vaddr[i + 1]);
1248 				j = i + 1;
1249 			}
1250 		}
1251 		printf("\n"); }
1252 #endif
1253 		acpi_print_tcpa(event);
1254 
1255 		vaddr += sizeof(struct TCPAevent) + event->event_size;
1256 	}
1257 
1258 	printf(END_COMMENT);
1259 }
1260 static void acpi_handle_tpm2(ACPI_TABLE_HEADER *sdp)
1261 {
1262 	ACPI_TABLE_TPM2 *tpm2;
1263 
1264 	printf (BEGIN_COMMENT);
1265 	acpi_print_sdt(sdp);
1266 	tpm2 = (ACPI_TABLE_TPM2 *) sdp;
1267 	printf ("\t\tControlArea=%jx\n", tpm2->ControlAddress);
1268 	printf ("\t\tStartMethod=%x\n", tpm2->StartMethod);
1269 	printf (END_COMMENT);
1270 }
1271 
1272 static const char *
1273 devscope_type2str(int type)
1274 {
1275 	static char typebuf[16];
1276 
1277 	switch (type) {
1278 	case 1:
1279 		return ("PCI Endpoint Device");
1280 	case 2:
1281 		return ("PCI Sub-Hierarchy");
1282 	case 3:
1283 		return ("IOAPIC");
1284 	case 4:
1285 		return ("HPET");
1286 	default:
1287 		snprintf(typebuf, sizeof(typebuf), "%d", type);
1288 		return (typebuf);
1289 	}
1290 }
1291 
1292 static int
1293 acpi_handle_dmar_devscope(void *addr, int remaining)
1294 {
1295 	char sep;
1296 	int pathlen;
1297 	ACPI_DMAR_PCI_PATH *path, *pathend;
1298 	ACPI_DMAR_DEVICE_SCOPE *devscope = addr;
1299 
1300 	if (remaining < (int)sizeof(ACPI_DMAR_DEVICE_SCOPE))
1301 		return (-1);
1302 
1303 	if (remaining < devscope->Length)
1304 		return (-1);
1305 
1306 	printf("\n");
1307 	printf("\t\tType=%s\n", devscope_type2str(devscope->EntryType));
1308 	printf("\t\tLength=%d\n", devscope->Length);
1309 	printf("\t\tEnumerationId=%d\n", devscope->EnumerationId);
1310 	printf("\t\tStartBusNumber=%d\n", devscope->Bus);
1311 
1312 	path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
1313 	pathlen = devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE);
1314 	pathend = path + pathlen / sizeof(ACPI_DMAR_PCI_PATH);
1315 	if (path < pathend) {
1316 		sep = '{';
1317 		printf("\t\tPath=");
1318 		do {
1319 			printf("%c%d:%d", sep, path->Device, path->Function);
1320 			sep=',';
1321 			path++;
1322 		} while (path < pathend);
1323 		printf("}\n");
1324 	}
1325 
1326 	return (devscope->Length);
1327 }
1328 
1329 static void
1330 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
1331 {
1332 	char *cp;
1333 	int remaining, consumed;
1334 
1335 	printf("\n");
1336 	printf("\tType=DRHD\n");
1337 	printf("\tLength=%d\n", drhd->Header.Length);
1338 
1339 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1340 
1341 	printf("\tFlags=");
1342 	PRINTFLAG(drhd->Flags, INCLUDE_ALL);
1343 	PRINTFLAG_END();
1344 
1345 #undef PRINTFLAG
1346 
1347 	printf("\tSegment=%d\n", drhd->Segment);
1348 	printf("\tAddress=0x%016jx\n", (uintmax_t)drhd->Address);
1349 
1350 	remaining = drhd->Header.Length - sizeof(ACPI_DMAR_HARDWARE_UNIT);
1351 	if (remaining > 0)
1352 		printf("\tDevice Scope:");
1353 	while (remaining > 0) {
1354 		cp = (char *)drhd + drhd->Header.Length - remaining;
1355 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1356 		if (consumed <= 0)
1357 			break;
1358 		else
1359 			remaining -= consumed;
1360 	}
1361 }
1362 
1363 static void
1364 acpi_handle_dmar_rmrr(ACPI_DMAR_RESERVED_MEMORY *rmrr)
1365 {
1366 	char *cp;
1367 	int remaining, consumed;
1368 
1369 	printf("\n");
1370 	printf("\tType=RMRR\n");
1371 	printf("\tLength=%d\n", rmrr->Header.Length);
1372 	printf("\tSegment=%d\n", rmrr->Segment);
1373 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rmrr->BaseAddress);
1374 	printf("\tLimitAddress=0x%016jx\n", (uintmax_t)rmrr->EndAddress);
1375 
1376 	remaining = rmrr->Header.Length - sizeof(ACPI_DMAR_RESERVED_MEMORY);
1377 	if (remaining > 0)
1378 		printf("\tDevice Scope:");
1379 	while (remaining > 0) {
1380 		cp = (char *)rmrr + rmrr->Header.Length - remaining;
1381 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1382 		if (consumed <= 0)
1383 			break;
1384 		else
1385 			remaining -= consumed;
1386 	}
1387 }
1388 
1389 static void
1390 acpi_handle_dmar_atsr(ACPI_DMAR_ATSR *atsr)
1391 {
1392 	char *cp;
1393 	int remaining, consumed;
1394 
1395 	printf("\n");
1396 	printf("\tType=ATSR\n");
1397 	printf("\tLength=%d\n", atsr->Header.Length);
1398 
1399 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1400 
1401 	printf("\tFlags=");
1402 	PRINTFLAG(atsr->Flags, ALL_PORTS);
1403 	PRINTFLAG_END();
1404 
1405 #undef PRINTFLAG
1406 
1407 	printf("\tSegment=%d\n", atsr->Segment);
1408 
1409 	remaining = atsr->Header.Length - sizeof(ACPI_DMAR_ATSR);
1410 	if (remaining > 0)
1411 		printf("\tDevice Scope:");
1412 	while (remaining > 0) {
1413 		cp = (char *)atsr + atsr->Header.Length - remaining;
1414 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1415 		if (consumed <= 0)
1416 			break;
1417 		else
1418 			remaining -= consumed;
1419 	}
1420 }
1421 
1422 static void
1423 acpi_handle_dmar_rhsa(ACPI_DMAR_RHSA *rhsa)
1424 {
1425 
1426 	printf("\n");
1427 	printf("\tType=RHSA\n");
1428 	printf("\tLength=%d\n", rhsa->Header.Length);
1429 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rhsa->BaseAddress);
1430 	printf("\tProximityDomain=0x%08x\n", rhsa->ProximityDomain);
1431 }
1432 
1433 static int
1434 acpi_handle_dmar_remapping_structure(void *addr, int remaining)
1435 {
1436 	ACPI_DMAR_HEADER *hdr = addr;
1437 
1438 	if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
1439 		return (-1);
1440 
1441 	if (remaining < hdr->Length)
1442 		return (-1);
1443 
1444 	switch (hdr->Type) {
1445 	case ACPI_DMAR_TYPE_HARDWARE_UNIT:
1446 		acpi_handle_dmar_drhd(addr);
1447 		break;
1448 	case ACPI_DMAR_TYPE_RESERVED_MEMORY:
1449 		acpi_handle_dmar_rmrr(addr);
1450 		break;
1451 	case ACPI_DMAR_TYPE_ROOT_ATS:
1452 		acpi_handle_dmar_atsr(addr);
1453 		break;
1454 	case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
1455 		acpi_handle_dmar_rhsa(addr);
1456 		break;
1457 	default:
1458 		printf("\n");
1459 		printf("\tType=%d\n", hdr->Type);
1460 		printf("\tLength=%d\n", hdr->Length);
1461 		break;
1462 	}
1463 	return (hdr->Length);
1464 }
1465 
1466 #ifndef ACPI_DMAR_X2APIC_OPT_OUT
1467 #define	ACPI_DMAR_X2APIC_OPT_OUT	(0x2)
1468 #endif
1469 
1470 static void
1471 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
1472 {
1473 	char *cp;
1474 	int remaining, consumed;
1475 	ACPI_TABLE_DMAR *dmar;
1476 
1477 	printf(BEGIN_COMMENT);
1478 	acpi_print_sdt(sdp);
1479 	dmar = (ACPI_TABLE_DMAR *)sdp;
1480 	printf("\tHost Address Width=%d\n", dmar->Width + 1);
1481 
1482 #define PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1483 
1484 	printf("\tFlags=");
1485 	PRINTFLAG(dmar->Flags, INTR_REMAP);
1486 	PRINTFLAG(dmar->Flags, X2APIC_OPT_OUT);
1487 	PRINTFLAG_END();
1488 
1489 #undef PRINTFLAG
1490 
1491 	remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR);
1492 	while (remaining > 0) {
1493 		cp = (char *)sdp + sdp->Length - remaining;
1494 		consumed = acpi_handle_dmar_remapping_structure(cp, remaining);
1495 		if (consumed <= 0)
1496 			break;
1497 		else
1498 			remaining -= consumed;
1499 	}
1500 
1501 	printf(END_COMMENT);
1502 }
1503 
1504 static void
1505 acpi_handle_ivrs_ivhd_header(ACPI_IVRS_HEADER *addr)
1506 {
1507 	printf("\n\tIVHD Type=%#x IOMMUId=%x\n\tFlags=",
1508 	    addr->Type, addr->DeviceId);
1509 #define PRINTFLAG(flag, name) printflag(addr->Flags, flag, #name)
1510 	PRINTFLAG(ACPI_IVHD_TT_ENABLE, HtTunEn);
1511 	PRINTFLAG(ACPI_IVHD_ISOC, PassPW);
1512 	PRINTFLAG(ACPI_IVHD_RES_PASS_PW, ResPassPW);
1513 	PRINTFLAG(ACPI_IVHD_ISOC, Isoc);
1514 	PRINTFLAG(ACPI_IVHD_TT_ENABLE, IotlbSup);
1515 	PRINTFLAG((1 << 5), Coherent);
1516 	PRINTFLAG((1 << 6), PreFSup);
1517 	PRINTFLAG((1 << 7), PPRSup);
1518 #undef PRINTFLAG
1519 	PRINTFLAG_END();
1520 }
1521 
1522 static void
1523 acpi_handle_ivrs_ivhd_dte(UINT8 dte)
1524 {
1525 	if (dte == 0) {
1526 		printf("\n");
1527 		return;
1528 	}
1529 	printf(" DTE=");
1530 #define PRINTFLAG(flag, name) printflag(dte, flag, #name)
1531 	PRINTFLAG(ACPI_IVHD_INIT_PASS, INITPass);
1532 	PRINTFLAG(ACPI_IVHD_EINT_PASS, EIntPass);
1533 	PRINTFLAG(ACPI_IVHD_NMI_PASS, NMIPass);
1534 	PRINTFLAG(ACPI_IVHD_SYSTEM_MGMT, SysMgtPass);
1535 	PRINTFLAG(ACPI_IVHD_LINT0_PASS, Lint0Pass);
1536 	PRINTFLAG(ACPI_IVHD_LINT1_PASS, Lint1Pass);
1537 #undef PRINTFLAG
1538 	PRINTFLAG_END();
1539 }
1540 
1541 static void
1542 acpi_handle_ivrs_ivhd_edte(UINT32 edte)
1543 {
1544 	if (edte == 0)
1545 		return;
1546 	printf("\t\t ExtDTE=");
1547 #define PRINTFLAG(flag, name) printflag(edte, flag, #name)
1548 	PRINTFLAG(ACPI_IVHD_ATS_DISABLED, AtsDisabled);
1549 #undef PRINTFLAG
1550 	PRINTFLAG_END();
1551 }
1552 
1553 static const char *
1554 acpi_handle_ivrs_ivhd_variety(UINT8 v)
1555 {
1556 	switch (v) {
1557 	case ACPI_IVHD_IOAPIC:
1558 		return ("IOAPIC");
1559 	case ACPI_IVHD_HPET:
1560 		return ("HPET");
1561 	default:
1562 		return ("UNKNOWN");
1563 	}
1564 }
1565 
1566 static void
1567 acpi_handle_ivrs_ivhd_devs(ACPI_IVRS_DE_HEADER *d, char *de)
1568 {
1569 	char *db;
1570 	ACPI_IVRS_DEVICE4 *d4;
1571 	ACPI_IVRS_DEVICE8A *d8a;
1572 	ACPI_IVRS_DEVICE8B *d8b;
1573 	ACPI_IVRS_DEVICE8C *d8c;
1574 	ACPI_IVRS_DEVICE_HID *dh;
1575 	size_t len;
1576 	UINT32 x32;
1577 
1578 	for (; (char *)d < de; d = (ACPI_IVRS_DE_HEADER *)(db + len)) {
1579 		db = (char *)d;
1580 		if (d->Type == ACPI_IVRS_TYPE_PAD4) {
1581 			len = sizeof(*d4);
1582 		} else if (d->Type == ACPI_IVRS_TYPE_ALL) {
1583 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1584 			len = sizeof(*d4);
1585 			printf("\t\tDev Type=%#x Id=ALL", d4->Header.Type);
1586 			acpi_handle_ivrs_ivhd_dte(d4->Header.DataSetting);
1587 		} else if (d->Type == ACPI_IVRS_TYPE_SELECT) {
1588 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1589 			len = sizeof(*d4);
1590 			printf("\t\tDev Type=%#x Id=%#06x", d4->Header.Type,
1591 			    d4->Header.Id);
1592 			acpi_handle_ivrs_ivhd_dte(d4->Header.DataSetting);
1593 		} else if (d->Type == ACPI_IVRS_TYPE_START) {
1594 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1595 			len = 2 * sizeof(*d4);
1596 			printf("\t\tDev Type=%#x Id=%#06x-%#06x",
1597 			    d4->Header.Type,
1598 			    d4->Header.Id, (d4 + 1)->Header.Id);
1599 			acpi_handle_ivrs_ivhd_dte(d4->Header.DataSetting);
1600 		} else if (d->Type == ACPI_IVRS_TYPE_END) {
1601 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1602 			len = 2 * sizeof(*d4);
1603 			printf("\t\tDev Type=%#x Id=%#06x BIOS BUG\n",
1604 			    d4->Header.Type, d4->Header.Id);
1605 		} else if (d->Type == ACPI_IVRS_TYPE_PAD8) {
1606 			len = sizeof(*d8a);
1607 		} else if (d->Type == ACPI_IVRS_TYPE_ALIAS_SELECT) {
1608 			d8a = (ACPI_IVRS_DEVICE8A *)db;
1609 			len = sizeof(*d8a);
1610 			printf("\t\tDev Type=%#x Id=%#06x AliasId=%#06x",
1611 			    d8a->Header.Type, d8a->Header.Id, d8a->UsedId);
1612 			acpi_handle_ivrs_ivhd_dte(d8a->Header.DataSetting);
1613 		} else if (d->Type == ACPI_IVRS_TYPE_ALIAS_START) {
1614 			d8a = (ACPI_IVRS_DEVICE8A *)db;
1615 			d4 = (ACPI_IVRS_DEVICE4 *)(db + sizeof(*d8a));
1616 			len = sizeof(*d8a) + sizeof(*d4);
1617 			printf("\t\tDev Type=%#x Id=%#06x-%#06x AliasId=%#06x",
1618 			    d8a->Header.Type, d8a->Header.Id, d4->Header.Id,
1619 			    d8a->UsedId);
1620 			acpi_handle_ivrs_ivhd_dte(d8a->Header.DataSetting);
1621 		} else if (d->Type == ACPI_IVRS_TYPE_EXT_SELECT) {
1622 			d8b = (ACPI_IVRS_DEVICE8B *)db;
1623 			len = sizeof(*d8b);
1624 			printf("\t\tDev Type=%#x Id=%#06x",
1625 			    d8a->Header.Type, d8a->Header.Id);
1626 			acpi_handle_ivrs_ivhd_dte(d8b->Header.DataSetting);
1627 			printf("\t\t");
1628 			acpi_handle_ivrs_ivhd_edte(d8b->ExtendedData);
1629 		} else if (d->Type == ACPI_IVRS_TYPE_EXT_START) {
1630 			d8b = (ACPI_IVRS_DEVICE8B *)db;
1631 			len = sizeof(*d8b);
1632 			d4 = (ACPI_IVRS_DEVICE4 *)(db + sizeof(*d8a));
1633 			len = sizeof(*d8a) + sizeof(*d4);
1634 			printf("\t\tDev Type=%#x Id=%#06x-%#06x",
1635 			    d8a->Header.Type, d8a->Header.Id, d4->Header.Id);
1636 			acpi_handle_ivrs_ivhd_dte(d8b->Header.DataSetting);
1637 			acpi_handle_ivrs_ivhd_edte(d8b->ExtendedData);
1638 		} else if (d->Type == ACPI_IVRS_TYPE_SPECIAL) {
1639 			d8c = (ACPI_IVRS_DEVICE8C *)db;
1640 			len = sizeof(*d8c);
1641 			printf("\t\tDev Type=%#x Id=%#06x Handle=%#x "
1642 			    "Variety=%d(%s)",
1643 			    d8c->Header.Type, d8c->UsedId, d8c->Handle,
1644 			    d8c->Variety,
1645 			    acpi_handle_ivrs_ivhd_variety(d8c->Variety));
1646 			acpi_handle_ivrs_ivhd_dte(d8c->Header.DataSetting);
1647 		} else if (d->Type == ACPI_IVRS_TYPE_HID) {
1648 			dh = (ACPI_IVRS_DEVICE_HID *)db;
1649 			len = sizeof(*dh) + dh->UidLength;
1650 			printf("\t\tDev Type=%#x Id=%#06x HID=",
1651 			    dh->Header.Type, dh->Header.Id);
1652 			acpi_print_string((char *)&dh->AcpiHid,
1653 			    sizeof(dh->AcpiHid));
1654 			printf(" CID=");
1655 			acpi_print_string((char *)&dh->AcpiCid,
1656 			    sizeof(dh->AcpiCid));
1657 			printf(" UID=");
1658 			switch (dh->UidType) {
1659 			case ACPI_IVRS_UID_NOT_PRESENT:
1660 			default:
1661 				printf("none");
1662 				break;
1663 			case ACPI_IVRS_UID_IS_INTEGER:
1664 				memcpy(&x32, dh + 1, sizeof(x32));
1665 				printf("%#x", x32);
1666 				break;
1667 			case ACPI_IVRS_UID_IS_STRING:
1668 				acpi_print_string((char *)(dh + 1),
1669 				    dh->UidLength);
1670 				break;
1671 			}
1672 			acpi_handle_ivrs_ivhd_dte(dh->Header.DataSetting);
1673 		} else {
1674 			printf("\t\tDev Type=%#x Unknown\n", d->Type);
1675 			if (d->Type <= 63)
1676 				len = sizeof(*d4);
1677 			else if (d->Type <= 127)
1678 				len = sizeof(*d8a);
1679 			else {
1680 				printf("Abort, cannot advance iterator.\n");
1681 				return;
1682 			}
1683 		}
1684 	}
1685 }
1686 
1687 static void
1688 acpi_handle_ivrs_ivhd_10(ACPI_IVRS_HARDWARE1 *addr, bool efrsup)
1689 {
1690 	acpi_handle_ivrs_ivhd_header(&addr->Header);
1691 	printf("\tCapOffset=%#x Base=%#jx PCISeg=%#x Unit=%#x MSIlog=%d\n",
1692 	    addr->CapabilityOffset, (uintmax_t)addr->BaseAddress,
1693 	    addr->PciSegmentGroup, (addr->Info & ACPI_IVHD_UNIT_ID_MASK) >> 8,
1694 	    addr->Info & ACPI_IVHD_MSI_NUMBER_MASK);
1695 	if (efrsup) {
1696 #define PRINTFLAG(flag, name) printflag(addr->FeatureReporting, flag, #name)
1697 #define PRINTFIELD(lbit, hbit, name) \
1698     printfield(addr->FeatureReporting, lbit, hbit, #name)
1699 		PRINTFIELD(30, 31, HATS);
1700 		PRINTFIELD(28, 29, GATS);
1701 		PRINTFIELD(23, 27, MsiNumPPR);
1702 		PRINTFIELD(17, 22, PNBanks);
1703 		PRINTFIELD(13, 16, PNCounters);
1704 		PRINTFIELD(8, 12, PASmax);
1705 		PRINTFLAG(1 << 7, HESup);
1706 		PRINTFLAG(1 << 6, GASup);
1707 		PRINTFLAG(1 << 5, UASup);
1708 		PRINTFIELD(3, 2, GLXSup);
1709 		PRINTFLAG(1 << 1, NXSup);
1710 		PRINTFLAG(1 << 0, XTSup);
1711 #undef PRINTFLAG
1712 #undef PRINTFIELD
1713 		PRINTFLAG_END();
1714 	}
1715 	acpi_handle_ivrs_ivhd_devs((ACPI_IVRS_DE_HEADER *)(addr + 1),
1716 	    (char *)addr + addr->Header.Length);
1717 }
1718 
1719 static void
1720 acpi_handle_ivrs_ivhd_info_11(ACPI_IVRS_HARDWARE2 *addr)
1721 {
1722 	acpi_handle_ivrs_ivhd_header(&addr->Header);
1723 	printf("\tCapOffset=%#x Base=%#jx PCISeg=%#x Unit=%#x MSIlog=%d\n",
1724 	    addr->CapabilityOffset, (uintmax_t)addr->BaseAddress,
1725 	    addr->PciSegmentGroup, (addr->Info >> 8) & 0x1f,
1726 	    addr->Info & 0x5);
1727 	printf("\tAttr=");
1728 #define PRINTFIELD(lbit, hbit, name) \
1729     printfield(addr->Attributes, lbit, hbit, #name)
1730 	PRINTFIELD(23, 27, MsiNumPPR);
1731 	PRINTFIELD(17, 22, PNBanks);
1732 	PRINTFIELD(13, 16, PNCounters);
1733 #undef PRINTFIELD
1734 	PRINTFLAG_END();
1735 }
1736 
1737 static void
1738 acpi_handle_ivrs_ivhd_11(ACPI_IVRS_HARDWARE2 *addr)
1739 {
1740 	acpi_handle_ivrs_ivhd_info_11(addr);
1741 	printf("\tEFRreg=%#018jx\n", (uintmax_t)addr->EfrRegisterImage);
1742 	acpi_handle_ivrs_ivhd_devs((ACPI_IVRS_DE_HEADER *)(addr + 1),
1743 	    (char *)addr + addr->Header.Length);
1744 }
1745 
1746 static void
1747 acpi_handle_ivrs_ivhd_40(ACPI_IVRS_HARDWARE2 *addr)
1748 {
1749 	acpi_handle_ivrs_ivhd_info_11(addr);
1750 	printf("\tEFRreg=%#018jx EFR2reg=%#018jx\n",
1751 	    (uintmax_t)addr->EfrRegisterImage, (uintmax_t)addr->Reserved);
1752 	acpi_handle_ivrs_ivhd_devs((ACPI_IVRS_DE_HEADER *)(addr + 1),
1753 	    (char *)addr + addr->Header.Length);
1754 }
1755 
1756 static const char *
1757 acpi_handle_ivrs_ivmd_type(ACPI_IVRS_MEMORY *addr)
1758 {
1759 	switch (addr->Header.Type) {
1760 	case ACPI_IVRS_TYPE_MEMORY1:
1761 		return ("ALL");
1762 	case ACPI_IVRS_TYPE_MEMORY2:
1763 		return ("specified");
1764 	case ACPI_IVRS_TYPE_MEMORY3:
1765 		return ("range");
1766 	default:
1767 		return ("unknown");
1768 	}
1769 }
1770 
1771 static void
1772 acpi_handle_ivrs_ivmd(ACPI_IVRS_MEMORY *addr)
1773 {
1774 	printf("\tMem Type=%#x(%s) ",
1775 	    addr->Header.Type, acpi_handle_ivrs_ivmd_type(addr));
1776 	switch (addr->Header.Type) {
1777 	case ACPI_IVRS_TYPE_MEMORY2:
1778 		printf("Id=%#06x PCISeg=%#x ", addr->Header.DeviceId,
1779 		    *(UINT16 *)&addr->Reserved);
1780 		break;
1781 	case ACPI_IVRS_TYPE_MEMORY3:
1782 		printf("Id=%#06x-%#06x PCISeg=%#x", addr->Header.DeviceId,
1783 		    addr->AuxData, *(UINT16 *)&addr->Reserved);
1784 		break;
1785 	}
1786 	printf("Start=%#18jx Length=%#jx Flags=",
1787 	    (uintmax_t)addr->StartAddress, (uintmax_t)addr->MemoryLength);
1788 #define PRINTFLAG(flag, name) printflag(addr->Header.Flags, flag, #name)
1789 	PRINTFLAG(ACPI_IVMD_EXCLUSION_RANGE, ExclusionRange);
1790 	PRINTFLAG(ACPI_IVMD_WRITE, IW);
1791 	PRINTFLAG(ACPI_IVMD_READ, IR);
1792 	PRINTFLAG(ACPI_IVMD_UNITY, Unity);
1793 #undef PRINTFLAG
1794 	PRINTFLAG_END();
1795 }
1796 
1797 static int
1798 acpi_handle_ivrs_blocks(void *addr, int remaining, bool efrsup)
1799 {
1800 	ACPI_IVRS_HEADER *hdr = addr;
1801 
1802 	if (remaining < (int)sizeof(ACPI_IVRS_HEADER))
1803 		return (-1);
1804 
1805 	if (remaining < hdr->Length)
1806 		return (-1);
1807 
1808 	switch (hdr->Type) {
1809 	case ACPI_IVRS_TYPE_HARDWARE1:
1810 		acpi_handle_ivrs_ivhd_10(addr, efrsup);
1811 		break;
1812 	case ACPI_IVRS_TYPE_HARDWARE2:
1813 		if (!efrsup)
1814 			printf("\t!! Found IVHD block 0x11 but !EFRsup\n");
1815 		acpi_handle_ivrs_ivhd_11(addr);
1816 		break;
1817 	case ACPI_IVRS_TYPE_HARDWARE3:
1818 		if (!efrsup)
1819 			printf("\t!! Found IVHD block 0x40 but !EFRsup\n");
1820 		acpi_handle_ivrs_ivhd_40(addr);
1821 		break;
1822 	case ACPI_IVRS_TYPE_MEMORY1:
1823 	case ACPI_IVRS_TYPE_MEMORY2:
1824 	case ACPI_IVRS_TYPE_MEMORY3:
1825 		acpi_handle_ivrs_ivmd(addr);
1826 		break;
1827 	default:
1828 		printf("\n");
1829 		printf("\tType=%d\n", hdr->Type);
1830 		printf("\tLength=%d\n", hdr->Length);
1831 		break;
1832 	}
1833 	return (hdr->Length);
1834 }
1835 
1836 #define	ACPI_IVRS_DMAREMAP	0x00000002
1837 #define	ACPI_IVRS_EFRSUP	0x00000001
1838 #define	ACPI_IVRS_GVA_SIZE	0x000000e0
1839 
1840 static void
1841 acpi_handle_ivrs(ACPI_TABLE_HEADER *sdp)
1842 {
1843 	ACPI_TABLE_IVRS *ivrs;
1844 	char *cp;
1845 	int remaining, consumed;
1846 	bool efrsup;
1847 
1848 	printf(BEGIN_COMMENT);
1849 	acpi_print_sdt(sdp);
1850 	ivrs = (ACPI_TABLE_IVRS *)sdp;
1851 	efrsup = (ivrs->Info & ACPI_IVRS_EFRSUP) != 0;
1852 	printf("\tVAsize=%d PAsize=%d GVAsize=%d\n",
1853 	    (ivrs->Info & ACPI_IVRS_VIRTUAL_SIZE) >> 15,
1854 	    (ivrs->Info & ACPI_IVRS_PHYSICAL_SIZE) >> 8,
1855 	    (ivrs->Info & ACPI_IVRS_GVA_SIZE) >> 5);
1856 	printf("\tATS_resp_res=%d DMA_preboot_remap=%d EFRsup=%d\n",
1857 	    (ivrs->Info & ACPI_IVRS_ATS_RESERVED) != 0,
1858 	    (ivrs->Info & ACPI_IVRS_DMAREMAP) != 0, efrsup);
1859 
1860 	remaining = sdp->Length - sizeof(ACPI_TABLE_IVRS);
1861 	while (remaining > 0) {
1862 		cp = (char *)sdp + sdp->Length - remaining;
1863 		consumed = acpi_handle_ivrs_blocks(cp, remaining, efrsup);
1864 		if (consumed <= 0)
1865 			break;
1866 		else
1867 			remaining -= consumed;
1868 	}
1869 
1870 	printf(END_COMMENT);
1871 }
1872 
1873 static void
1874 acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp)
1875 {
1876 
1877 	printf("\tFlags={");
1878 	if (mp->Flags & ACPI_SRAT_MEM_ENABLED)
1879 		printf("ENABLED");
1880 	else
1881 		printf("DISABLED");
1882 	if (mp->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
1883 		printf(",HOT_PLUGGABLE");
1884 	if (mp->Flags & ACPI_SRAT_MEM_NON_VOLATILE)
1885 		printf(",NON_VOLATILE");
1886 	printf("}\n");
1887 	printf("\tBase Address=0x%016jx\n", (uintmax_t)mp->BaseAddress);
1888 	printf("\tLength=0x%016jx\n", (uintmax_t)mp->Length);
1889 	printf("\tProximity Domain=%d\n", mp->ProximityDomain);
1890 }
1891 
1892 static const char *srat_types[] = {
1893     [ACPI_SRAT_TYPE_CPU_AFFINITY] = "CPU",
1894     [ACPI_SRAT_TYPE_MEMORY_AFFINITY] = "Memory",
1895     [ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY] = "X2APIC",
1896     [ACPI_SRAT_TYPE_GICC_AFFINITY] = "GICC",
1897     [ACPI_SRAT_TYPE_GIC_ITS_AFFINITY] = "GIC ITS",
1898 };
1899 
1900 static void
1901 acpi_print_srat(ACPI_SUBTABLE_HEADER *srat)
1902 {
1903 	ACPI_SRAT_CPU_AFFINITY *cpu;
1904 	ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
1905 	ACPI_SRAT_GICC_AFFINITY *gic;
1906 
1907 	if (srat->Type < nitems(srat_types))
1908 		printf("\tType=%s\n", srat_types[srat->Type]);
1909 	else
1910 		printf("\tType=%d (unknown)\n", srat->Type);
1911 	switch (srat->Type) {
1912 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
1913 		cpu = (ACPI_SRAT_CPU_AFFINITY *)srat;
1914 		acpi_print_srat_cpu(cpu->ApicId,
1915 		    cpu->ProximityDomainHi[2] << 24 |
1916 		    cpu->ProximityDomainHi[1] << 16 |
1917 		    cpu->ProximityDomainHi[0] << 0 |
1918 		    cpu->ProximityDomainLo, cpu->Flags);
1919 		break;
1920 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
1921 		acpi_print_srat_memory((ACPI_SRAT_MEM_AFFINITY *)srat);
1922 		break;
1923 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
1924 		x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)srat;
1925 		acpi_print_srat_cpu(x2apic->ApicId, x2apic->ProximityDomain,
1926 		    x2apic->Flags);
1927 		break;
1928 	case ACPI_SRAT_TYPE_GICC_AFFINITY:
1929 		gic = (ACPI_SRAT_GICC_AFFINITY *)srat;
1930 		acpi_print_srat_cpu(gic->AcpiProcessorUid, gic->ProximityDomain,
1931 		    gic->Flags);
1932 		break;
1933 	}
1934 }
1935 
1936 static void
1937 acpi_handle_srat(ACPI_TABLE_HEADER *sdp)
1938 {
1939 	ACPI_TABLE_SRAT *srat;
1940 
1941 	printf(BEGIN_COMMENT);
1942 	acpi_print_sdt(sdp);
1943 	srat = (ACPI_TABLE_SRAT *)sdp;
1944 	printf("\tTable Revision=%d\n", srat->TableRevision);
1945 	acpi_walk_subtables(sdp, (srat + 1), acpi_print_srat);
1946 	printf(END_COMMENT);
1947 }
1948 
1949 static const char *nfit_types[] = {
1950     [ACPI_NFIT_TYPE_SYSTEM_ADDRESS] = "System Address",
1951     [ACPI_NFIT_TYPE_MEMORY_MAP] = "Memory Map",
1952     [ACPI_NFIT_TYPE_INTERLEAVE] = "Interleave",
1953     [ACPI_NFIT_TYPE_SMBIOS] = "SMBIOS",
1954     [ACPI_NFIT_TYPE_CONTROL_REGION] = "Control Region",
1955     [ACPI_NFIT_TYPE_DATA_REGION] = "Data Region",
1956     [ACPI_NFIT_TYPE_FLUSH_ADDRESS] = "Flush Address",
1957     [ACPI_NFIT_TYPE_CAPABILITIES] = "Platform Capabilities"
1958 };
1959 
1960 
1961 static void
1962 acpi_print_nfit(ACPI_NFIT_HEADER *nfit)
1963 {
1964 	char *uuidstr;
1965 	uint32_t m, status;
1966 
1967 	ACPI_NFIT_SYSTEM_ADDRESS *sysaddr;
1968 	ACPI_NFIT_MEMORY_MAP *mmap;
1969 	ACPI_NFIT_INTERLEAVE *ileave;
1970 	ACPI_NFIT_CONTROL_REGION *ctlreg;
1971 	ACPI_NFIT_DATA_REGION *datareg;
1972 	ACPI_NFIT_FLUSH_ADDRESS *fladdr;
1973 	ACPI_NFIT_CAPABILITIES *caps;
1974 
1975 	if (nfit->Type < nitems(nfit_types))
1976 		printf("\tType=%s\n", nfit_types[nfit->Type]);
1977 	else
1978 		printf("\tType=%u (unknown)\n", nfit->Type);
1979 	switch (nfit->Type) {
1980 	case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
1981 		sysaddr = (ACPI_NFIT_SYSTEM_ADDRESS *)nfit;
1982 		printf("\tRangeIndex=%u\n", (u_int)sysaddr->RangeIndex);
1983 		printf("\tProximityDomain=%u\n",
1984 		    (u_int)sysaddr->ProximityDomain);
1985 		uuid_to_string((uuid_t *)(uintptr_t)(sysaddr->RangeGuid),
1986 		    &uuidstr, &status);
1987 		if (status != uuid_s_ok)
1988 			errx(1, "uuid_to_string: status=%u", status);
1989 		printf("\tRangeGuid=%s\n", uuidstr);
1990 		free(uuidstr);
1991 		printf("\tAddress=0x%016jx\n", (uintmax_t)sysaddr->Address);
1992 		printf("\tLength=0x%016jx\n", (uintmax_t)sysaddr->Length);
1993 		printf("\tMemoryMapping=0x%016jx\n",
1994 		    (uintmax_t)sysaddr->MemoryMapping);
1995 
1996 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_## flag, #flag)
1997 
1998 		printf("\tFlags=");
1999 		PRINTFLAG(sysaddr->Flags, ADD_ONLINE_ONLY);
2000 		PRINTFLAG(sysaddr->Flags, PROXIMITY_VALID);
2001 		PRINTFLAG_END();
2002 
2003 #undef PRINTFLAG
2004 
2005 		break;
2006 	case ACPI_NFIT_TYPE_MEMORY_MAP:
2007 		mmap = (ACPI_NFIT_MEMORY_MAP *)nfit;
2008 		printf("\tDeviceHandle=0x%x\n", (u_int)mmap->DeviceHandle);
2009 		printf("\tPhysicalId=0x%04x\n", (u_int)mmap->PhysicalId);
2010 		printf("\tRegionId=%u\n", (u_int)mmap->RegionId);
2011 		printf("\tRangeIndex=%u\n", (u_int)mmap->RangeIndex);
2012 		printf("\tRegionIndex=%u\n", (u_int)mmap->RegionIndex);
2013 		printf("\tRegionSize=0x%016jx\n", (uintmax_t)mmap->RegionSize);
2014 		printf("\tRegionOffset=0x%016jx\n",
2015 		    (uintmax_t)mmap->RegionOffset);
2016 		printf("\tAddress=0x%016jx\n", (uintmax_t)mmap->Address);
2017 		printf("\tInterleaveIndex=%u\n", (u_int)mmap->InterleaveIndex);
2018 		printf("\tInterleaveWays=%u\n", (u_int)mmap->InterleaveWays);
2019 
2020 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_MEM_## flag, #flag)
2021 
2022 		printf("\tFlags=");
2023 		PRINTFLAG(mmap->Flags, SAVE_FAILED);
2024 		PRINTFLAG(mmap->Flags, RESTORE_FAILED);
2025 		PRINTFLAG(mmap->Flags, FLUSH_FAILED);
2026 		PRINTFLAG(mmap->Flags, NOT_ARMED);
2027 		PRINTFLAG(mmap->Flags, HEALTH_OBSERVED);
2028 		PRINTFLAG(mmap->Flags, HEALTH_ENABLED);
2029 		PRINTFLAG(mmap->Flags, MAP_FAILED);
2030 		PRINTFLAG_END();
2031 
2032 #undef PRINTFLAG
2033 
2034 		break;
2035 	case ACPI_NFIT_TYPE_INTERLEAVE:
2036 		ileave = (ACPI_NFIT_INTERLEAVE *)nfit;
2037 		printf("\tInterleaveIndex=%u\n",
2038 		    (u_int)ileave->InterleaveIndex);
2039 		printf("\tLineCount=%u\n", (u_int)ileave->LineCount);
2040 		printf("\tLineSize=%u\n", (u_int)ileave->LineSize);
2041 		for (m = 0; m < ileave->LineCount; m++) {
2042 			printf("\tLine%uOffset=0x%08x\n", (u_int)m + 1,
2043 			    (u_int)ileave->LineOffset[m]);
2044 		}
2045 		break;
2046 	case ACPI_NFIT_TYPE_SMBIOS:
2047 		/* XXX smbios->Data[x] output is not supported */
2048 		break;
2049 	case ACPI_NFIT_TYPE_CONTROL_REGION:
2050 		ctlreg = (ACPI_NFIT_CONTROL_REGION *)nfit;
2051 		printf("\tRegionIndex=%u\n", (u_int)ctlreg->RegionIndex);
2052 		printf("\tVendorId=0x%04x\n", (u_int)ctlreg->VendorId);
2053 		printf("\tDeviceId=0x%04x\n", (u_int)ctlreg->DeviceId);
2054 		printf("\tRevisionId=0x%02x\n", (u_int)ctlreg->RevisionId);
2055 		printf("\tSubsystemVendorId=0x%04x\n",
2056 		    (u_int)ctlreg->SubsystemVendorId);
2057 		printf("\tSubsystemDeviceId=0x%04x\n",
2058 		    (u_int)ctlreg->SubsystemDeviceId);
2059 		printf("\tSubsystemRevisionId=0x%02x\n",
2060 		    (u_int)ctlreg->SubsystemRevisionId);
2061 		printf("\tValidFields=0x%02x\n", (u_int)ctlreg->ValidFields);
2062 		printf("\tManufacturingLocation=0x%02x\n",
2063 		    (u_int)ctlreg->ManufacturingLocation);
2064 		printf("\tManufacturingDate=%04x\n",
2065 		    (u_int)be16toh(ctlreg->ManufacturingDate));
2066 		printf("\tSerialNumber=%08X\n",
2067 		    (u_int)be32toh(ctlreg->SerialNumber));
2068 		printf("\tCode=0x%04x\n", (u_int)ctlreg->Code);
2069 		printf("\tWindows=%u\n", (u_int)ctlreg->Windows);
2070 		printf("\tWindowSize=0x%016jx\n",
2071 		    (uintmax_t)ctlreg->WindowSize);
2072 		printf("\tCommandOffset=0x%016jx\n",
2073 		    (uintmax_t)ctlreg->CommandOffset);
2074 		printf("\tCommandSize=0x%016jx\n",
2075 		    (uintmax_t)ctlreg->CommandSize);
2076 		printf("\tStatusOffset=0x%016jx\n",
2077 		    (uintmax_t)ctlreg->StatusOffset);
2078 		printf("\tStatusSize=0x%016jx\n",
2079 		    (uintmax_t)ctlreg->StatusSize);
2080 
2081 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_## flag, #flag)
2082 
2083 		printf("\tFlags=");
2084 		PRINTFLAG(ctlreg->Flags, CONTROL_BUFFERED);
2085 		PRINTFLAG_END();
2086 
2087 #undef PRINTFLAG
2088 
2089 		break;
2090 	case ACPI_NFIT_TYPE_DATA_REGION:
2091 		datareg = (ACPI_NFIT_DATA_REGION *)nfit;
2092 		printf("\tRegionIndex=%u\n", (u_int)datareg->RegionIndex);
2093 		printf("\tWindows=%u\n", (u_int)datareg->Windows);
2094 		printf("\tOffset=0x%016jx\n", (uintmax_t)datareg->Offset);
2095 		printf("\tSize=0x%016jx\n", (uintmax_t)datareg->Size);
2096 		printf("\tCapacity=0x%016jx\n", (uintmax_t)datareg->Capacity);
2097 		printf("\tStartAddress=0x%016jx\n",
2098 		    (uintmax_t)datareg->StartAddress);
2099 		break;
2100 	case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
2101 		fladdr = (ACPI_NFIT_FLUSH_ADDRESS *)nfit;
2102 		printf("\tDeviceHandle=%u\n", (u_int)fladdr->DeviceHandle);
2103 		printf("\tHintCount=%u\n", (u_int)fladdr->HintCount);
2104 		for (m = 0; m < fladdr->HintCount; m++) {
2105 			printf("\tHintAddress%u=0x%016jx\n", (u_int)m + 1,
2106 			    (uintmax_t)fladdr->HintAddress[m]);
2107 		}
2108 		break;
2109 	case ACPI_NFIT_TYPE_CAPABILITIES:
2110 		caps = (ACPI_NFIT_CAPABILITIES *)nfit;
2111 		printf("\tHighestCapability=%u\n", (u_int)caps->HighestCapability);
2112 
2113 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_CAPABILITY_## flag, #flag)
2114 
2115 		printf("\tCapabilities=");
2116 		PRINTFLAG(caps->Capabilities, CACHE_FLUSH);
2117 		PRINTFLAG(caps->Capabilities, MEM_FLUSH);
2118 		PRINTFLAG(caps->Capabilities, MEM_MIRRORING);
2119 		PRINTFLAG_END();
2120 
2121 #undef PRINTFLAG
2122 		break;
2123 	}
2124 }
2125 
2126 static void
2127 acpi_handle_nfit(ACPI_TABLE_HEADER *sdp)
2128 {
2129 	ACPI_TABLE_NFIT *nfit;
2130 
2131 	printf(BEGIN_COMMENT);
2132 	acpi_print_sdt(sdp);
2133 	nfit = (ACPI_TABLE_NFIT *)sdp;
2134 	acpi_walk_nfit(sdp, (nfit + 1), acpi_print_nfit);
2135 	printf(END_COMMENT);
2136 }
2137 
2138 static void
2139 acpi_print_sdt(ACPI_TABLE_HEADER *sdp)
2140 {
2141 	printf("  ");
2142 	acpi_print_string(sdp->Signature, ACPI_NAMESEG_SIZE);
2143 	printf(": Length=%d, Revision=%d, Checksum=%d,\n",
2144 	       sdp->Length, sdp->Revision, sdp->Checksum);
2145 	printf("\tOEMID=");
2146 	acpi_print_string(sdp->OemId, ACPI_OEM_ID_SIZE);
2147 	printf(", OEM Table ID=");
2148 	acpi_print_string(sdp->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
2149 	printf(", OEM Revision=0x%x,\n", sdp->OemRevision);
2150 	printf("\tCreator ID=");
2151 	acpi_print_string(sdp->AslCompilerId, ACPI_NAMESEG_SIZE);
2152 	printf(", Creator Revision=0x%x\n", sdp->AslCompilerRevision);
2153 }
2154 
2155 static void
2156 acpi_print_rsdt(ACPI_TABLE_HEADER *rsdp)
2157 {
2158 	ACPI_TABLE_RSDT *rsdt;
2159 	ACPI_TABLE_XSDT *xsdt;
2160 	int	i, entries;
2161 
2162 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2163 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2164 	printf(BEGIN_COMMENT);
2165 	acpi_print_sdt(rsdp);
2166 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2167 	printf("\tEntries={ ");
2168 	for (i = 0; i < entries; i++) {
2169 		if (i > 0)
2170 			printf(", ");
2171 		if (addr_size == 4)
2172 			printf("0x%08x", le32toh(rsdt->TableOffsetEntry[i]));
2173 		else
2174 			printf("0x%016jx",
2175 			    (uintmax_t)le64toh(xsdt->TableOffsetEntry[i]));
2176 	}
2177 	printf(" }\n");
2178 	printf(END_COMMENT);
2179 }
2180 
2181 static const char *acpi_pm_profiles[] = {
2182 	"Unspecified", "Desktop", "Mobile", "Workstation",
2183 	"Enterprise Server", "SOHO Server", "Appliance PC"
2184 };
2185 
2186 static void
2187 acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
2188 {
2189 	ACPI_TABLE_FADT *fadt;
2190 	const char *pm;
2191 
2192 	fadt = (ACPI_TABLE_FADT *)sdp;
2193 	printf(BEGIN_COMMENT);
2194 	acpi_print_sdt(sdp);
2195 	printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->Facs,
2196 	       fadt->Dsdt);
2197 	printf("\tINT_MODEL=%s\n", fadt->Model ? "APIC" : "PIC");
2198 	if (fadt->PreferredProfile >= sizeof(acpi_pm_profiles) / sizeof(char *))
2199 		pm = "Reserved";
2200 	else
2201 		pm = acpi_pm_profiles[fadt->PreferredProfile];
2202 	printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->PreferredProfile);
2203 	printf("\tSCI_INT=%d\n", fadt->SciInterrupt);
2204 	printf("\tSMI_CMD=0x%x, ", fadt->SmiCommand);
2205 	printf("ACPI_ENABLE=0x%x, ", fadt->AcpiEnable);
2206 	printf("ACPI_DISABLE=0x%x, ", fadt->AcpiDisable);
2207 	printf("S4BIOS_REQ=0x%x\n", fadt->S4BiosRequest);
2208 	printf("\tPSTATE_CNT=0x%x\n", fadt->PstateControl);
2209 	printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
2210 	       fadt->Pm1aEventBlock,
2211 	       fadt->Pm1aEventBlock + fadt->Pm1EventLength - 1);
2212 	if (fadt->Pm1bEventBlock != 0)
2213 		printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
2214 		       fadt->Pm1bEventBlock,
2215 		       fadt->Pm1bEventBlock + fadt->Pm1EventLength - 1);
2216 	printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
2217 	       fadt->Pm1aControlBlock,
2218 	       fadt->Pm1aControlBlock + fadt->Pm1ControlLength - 1);
2219 	if (fadt->Pm1bControlBlock != 0)
2220 		printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
2221 		       fadt->Pm1bControlBlock,
2222 		       fadt->Pm1bControlBlock + fadt->Pm1ControlLength - 1);
2223 	if (fadt->Pm2ControlBlock != 0)
2224 		printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
2225 		       fadt->Pm2ControlBlock,
2226 		       fadt->Pm2ControlBlock + fadt->Pm2ControlLength - 1);
2227 	printf("\tPM_TMR_BLK=0x%x-0x%x\n",
2228 	       fadt->PmTimerBlock,
2229 	       fadt->PmTimerBlock + fadt->PmTimerLength - 1);
2230 	if (fadt->Gpe0Block != 0)
2231 		printf("\tGPE0_BLK=0x%x-0x%x\n",
2232 		       fadt->Gpe0Block,
2233 		       fadt->Gpe0Block + fadt->Gpe0BlockLength - 1);
2234 	if (fadt->Gpe1Block != 0)
2235 		printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
2236 		       fadt->Gpe1Block,
2237 		       fadt->Gpe1Block + fadt->Gpe1BlockLength - 1,
2238 		       fadt->Gpe1Base);
2239 	if (fadt->CstControl != 0)
2240 		printf("\tCST_CNT=0x%x\n", fadt->CstControl);
2241 	printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
2242 	       fadt->C2Latency, fadt->C3Latency);
2243 	printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
2244 	       fadt->FlushSize, fadt->FlushStride);
2245 	printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
2246 	       fadt->DutyOffset, fadt->DutyWidth);
2247 	printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
2248 	       fadt->DayAlarm, fadt->MonthAlarm, fadt->Century);
2249 
2250 #define PRINTFLAG(var, flag)	printflag((var), ACPI_FADT_## flag, #flag)
2251 
2252 	printf("\tIAPC_BOOT_ARCH=");
2253 	PRINTFLAG(fadt->BootFlags, LEGACY_DEVICES);
2254 	PRINTFLAG(fadt->BootFlags, 8042);
2255 	PRINTFLAG(fadt->BootFlags, NO_VGA);
2256 	PRINTFLAG(fadt->BootFlags, NO_MSI);
2257 	PRINTFLAG(fadt->BootFlags, NO_ASPM);
2258 	PRINTFLAG(fadt->BootFlags, NO_CMOS_RTC);
2259 	PRINTFLAG_END();
2260 
2261 	printf("\tFlags=");
2262 	PRINTFLAG(fadt->Flags, WBINVD);
2263 	PRINTFLAG(fadt->Flags, WBINVD_FLUSH);
2264 	PRINTFLAG(fadt->Flags, C1_SUPPORTED);
2265 	PRINTFLAG(fadt->Flags, C2_MP_SUPPORTED);
2266 	PRINTFLAG(fadt->Flags, POWER_BUTTON);
2267 	PRINTFLAG(fadt->Flags, SLEEP_BUTTON);
2268 	PRINTFLAG(fadt->Flags, FIXED_RTC);
2269 	PRINTFLAG(fadt->Flags, S4_RTC_WAKE);
2270 	PRINTFLAG(fadt->Flags, 32BIT_TIMER);
2271 	PRINTFLAG(fadt->Flags, DOCKING_SUPPORTED);
2272 	PRINTFLAG(fadt->Flags, RESET_REGISTER);
2273 	PRINTFLAG(fadt->Flags, SEALED_CASE);
2274 	PRINTFLAG(fadt->Flags, HEADLESS);
2275 	PRINTFLAG(fadt->Flags, SLEEP_TYPE);
2276 	PRINTFLAG(fadt->Flags, PCI_EXPRESS_WAKE);
2277 	PRINTFLAG(fadt->Flags, PLATFORM_CLOCK);
2278 	PRINTFLAG(fadt->Flags, S4_RTC_VALID);
2279 	PRINTFLAG(fadt->Flags, REMOTE_POWER_ON);
2280 	PRINTFLAG(fadt->Flags, APIC_CLUSTER);
2281 	PRINTFLAG(fadt->Flags, APIC_PHYSICAL);
2282 	PRINTFLAG(fadt->Flags, HW_REDUCED);
2283 	PRINTFLAG(fadt->Flags, LOW_POWER_S0);
2284 	PRINTFLAG_END();
2285 
2286 #undef PRINTFLAG
2287 
2288 	if (fadt->Flags & ACPI_FADT_RESET_REGISTER) {
2289 		printf("\tRESET_REG=");
2290 		acpi_print_gas(&fadt->ResetRegister);
2291 		printf(", RESET_VALUE=%#x\n", fadt->ResetValue);
2292 	}
2293 	if (acpi_get_fadt_revision(fadt) > 1) {
2294 		printf("\tX_FACS=0x%016jx, ", (uintmax_t)fadt->XFacs);
2295 		printf("X_DSDT=0x%016jx\n", (uintmax_t)fadt->XDsdt);
2296 		printf("\tX_PM1a_EVT_BLK=");
2297 		acpi_print_gas(&fadt->XPm1aEventBlock);
2298 		if (fadt->XPm1bEventBlock.Address != 0) {
2299 			printf("\n\tX_PM1b_EVT_BLK=");
2300 			acpi_print_gas(&fadt->XPm1bEventBlock);
2301 		}
2302 		printf("\n\tX_PM1a_CNT_BLK=");
2303 		acpi_print_gas(&fadt->XPm1aControlBlock);
2304 		if (fadt->XPm1bControlBlock.Address != 0) {
2305 			printf("\n\tX_PM1b_CNT_BLK=");
2306 			acpi_print_gas(&fadt->XPm1bControlBlock);
2307 		}
2308 		if (fadt->XPm2ControlBlock.Address != 0) {
2309 			printf("\n\tX_PM2_CNT_BLK=");
2310 			acpi_print_gas(&fadt->XPm2ControlBlock);
2311 		}
2312 		printf("\n\tX_PM_TMR_BLK=");
2313 		acpi_print_gas(&fadt->XPmTimerBlock);
2314 		if (fadt->XGpe0Block.Address != 0) {
2315 			printf("\n\tX_GPE0_BLK=");
2316 			acpi_print_gas(&fadt->XGpe0Block);
2317 		}
2318 		if (fadt->XGpe1Block.Address != 0) {
2319 			printf("\n\tX_GPE1_BLK=");
2320 			acpi_print_gas(&fadt->XGpe1Block);
2321 		}
2322 		printf("\n");
2323 	}
2324 
2325 	printf(END_COMMENT);
2326 }
2327 
2328 static void
2329 acpi_print_facs(ACPI_TABLE_FACS *facs)
2330 {
2331 	printf(BEGIN_COMMENT);
2332 	printf("  FACS:\tLength=%u, ", facs->Length);
2333 	printf("HwSig=0x%08x, ", facs->HardwareSignature);
2334 	printf("Firm_Wake_Vec=0x%08x\n", facs->FirmwareWakingVector);
2335 
2336 	printf("\tGlobal_Lock=");
2337 	if (facs->GlobalLock != 0) {
2338 		if (facs->GlobalLock & ACPI_GLOCK_PENDING)
2339 			printf("PENDING,");
2340 		if (facs->GlobalLock & ACPI_GLOCK_OWNED)
2341 			printf("OWNED");
2342 	}
2343 	printf("\n");
2344 
2345 	printf("\tFlags=");
2346 	if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
2347 		printf("S4BIOS");
2348 	printf("\n");
2349 
2350 	if (facs->XFirmwareWakingVector != 0)
2351 		printf("\tX_Firm_Wake_Vec=%016jx\n",
2352 		    (uintmax_t)facs->XFirmwareWakingVector);
2353 	printf("\tVersion=%u\n", facs->Version);
2354 
2355 	printf(END_COMMENT);
2356 }
2357 
2358 static void
2359 acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp)
2360 {
2361 	printf(BEGIN_COMMENT);
2362 	acpi_print_sdt(dsdp);
2363 	printf(END_COMMENT);
2364 }
2365 
2366 int
2367 acpi_checksum(void *p, size_t length)
2368 {
2369 	uint8_t *bp;
2370 	uint8_t sum;
2371 
2372 	bp = p;
2373 	sum = 0;
2374 	while (length--)
2375 		sum += *bp++;
2376 
2377 	return (sum);
2378 }
2379 
2380 static ACPI_TABLE_HEADER *
2381 acpi_map_sdt(vm_offset_t pa)
2382 {
2383 	ACPI_TABLE_HEADER *sp;
2384 
2385 	sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
2386 	sp = acpi_map_physical(pa, sp->Length);
2387 	return (sp);
2388 }
2389 
2390 static void
2391 acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp)
2392 {
2393 	printf(BEGIN_COMMENT);
2394 	printf("  RSD PTR: OEM=");
2395 	acpi_print_string(rp->OemId, ACPI_OEM_ID_SIZE);
2396 	printf(", ACPI_Rev=%s (%d)\n", rp->Revision < 2 ? "1.0x" : "2.0x",
2397 	       rp->Revision);
2398 	if (rp->Revision < 2) {
2399 		printf("\tRSDT=0x%08x, cksum=%u\n", rp->RsdtPhysicalAddress,
2400 		    rp->Checksum);
2401 	} else {
2402 		printf("\tXSDT=0x%016jx, length=%u, cksum=%u\n",
2403 		    (uintmax_t)rp->XsdtPhysicalAddress, rp->Length,
2404 		    rp->ExtendedChecksum);
2405 	}
2406 	printf(END_COMMENT);
2407 }
2408 
2409 static void
2410 acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp)
2411 {
2412 	ACPI_TABLE_HEADER *sdp;
2413 	ACPI_TABLE_RSDT *rsdt;
2414 	ACPI_TABLE_XSDT *xsdt;
2415 	vm_offset_t addr;
2416 	int entries, i;
2417 
2418 	acpi_print_rsdt(rsdp);
2419 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2420 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2421 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2422 	for (i = 0; i < entries; i++) {
2423 		if (addr_size == 4)
2424 			addr = le32toh(rsdt->TableOffsetEntry[i]);
2425 		else
2426 			addr = le64toh(xsdt->TableOffsetEntry[i]);
2427 		if (addr == 0)
2428 			continue;
2429 		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
2430 		if (acpi_checksum(sdp, sdp->Length)) {
2431 			warnx("RSDT entry %d (sig %.4s) is corrupt", i,
2432 			    sdp->Signature);
2433 			continue;
2434 		}
2435 		if (!memcmp(sdp->Signature, ACPI_SIG_BERT, 4))
2436 			acpi_handle_bert(sdp);
2437 		else if (!memcmp(sdp->Signature, ACPI_SIG_EINJ, 4))
2438 			acpi_handle_einj(sdp);
2439 		else if (!memcmp(sdp->Signature, ACPI_SIG_ERST, 4))
2440 			acpi_handle_erst(sdp);
2441 		else if (!memcmp(sdp->Signature, ACPI_SIG_FADT, 4))
2442 			acpi_handle_fadt(sdp);
2443 		else if (!memcmp(sdp->Signature, ACPI_SIG_MADT, 4))
2444 			acpi_handle_madt(sdp);
2445 		else if (!memcmp(sdp->Signature, ACPI_SIG_HEST, 4))
2446 			acpi_handle_hest(sdp);
2447 		else if (!memcmp(sdp->Signature, ACPI_SIG_HPET, 4))
2448 			acpi_handle_hpet(sdp);
2449 		else if (!memcmp(sdp->Signature, ACPI_SIG_ECDT, 4))
2450 			acpi_handle_ecdt(sdp);
2451 		else if (!memcmp(sdp->Signature, ACPI_SIG_MCFG, 4))
2452 			acpi_handle_mcfg(sdp);
2453 		else if (!memcmp(sdp->Signature, ACPI_SIG_SLIT, 4))
2454 			acpi_handle_slit(sdp);
2455 		else if (!memcmp(sdp->Signature, ACPI_SIG_SRAT, 4))
2456 			acpi_handle_srat(sdp);
2457 		else if (!memcmp(sdp->Signature, ACPI_SIG_TCPA, 4))
2458 			acpi_handle_tcpa(sdp);
2459 		else if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
2460 			acpi_handle_dmar(sdp);
2461 		else if (!memcmp(sdp->Signature, ACPI_SIG_IVRS, 4))
2462 			acpi_handle_ivrs(sdp);
2463 		else if (!memcmp(sdp->Signature, ACPI_SIG_NFIT, 4))
2464 			acpi_handle_nfit(sdp);
2465 		else if (!memcmp(sdp->Signature, ACPI_SIG_WDDT, 4))
2466 			acpi_handle_wddt(sdp);
2467 		else if (!memcmp(sdp->Signature, ACPI_SIG_LPIT, 4))
2468 			acpi_handle_lpit(sdp);
2469 		else if (!memcmp(sdp->Signature, ACPI_SIG_TPM2, 4))
2470 			acpi_handle_tpm2(sdp);
2471 		else {
2472 			printf(BEGIN_COMMENT);
2473 			acpi_print_sdt(sdp);
2474 			printf(END_COMMENT);
2475 		}
2476 	}
2477 }
2478 
2479 ACPI_TABLE_HEADER *
2480 sdt_load_devmem(void)
2481 {
2482 	ACPI_TABLE_RSDP *rp;
2483 	ACPI_TABLE_HEADER *rsdp;
2484 
2485 	rp = acpi_find_rsd_ptr();
2486 	if (!rp)
2487 		errx(1, "Can't find ACPI information");
2488 
2489 	if (tflag)
2490 		acpi_print_rsd_ptr(rp);
2491 	if (rp->Revision < 2) {
2492 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->RsdtPhysicalAddress);
2493 		if (memcmp(rsdp->Signature, "RSDT", 4) != 0 ||
2494 		    acpi_checksum(rsdp, rsdp->Length) != 0)
2495 			errx(1, "RSDT is corrupted");
2496 		addr_size = sizeof(uint32_t);
2497 	} else {
2498 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->XsdtPhysicalAddress);
2499 		if (memcmp(rsdp->Signature, "XSDT", 4) != 0 ||
2500 		    acpi_checksum(rsdp, rsdp->Length) != 0)
2501 			errx(1, "XSDT is corrupted");
2502 		addr_size = sizeof(uint64_t);
2503 	}
2504 	return (rsdp);
2505 }
2506 
2507 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
2508 static int
2509 write_dsdt(int fd, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdt)
2510 {
2511 	ACPI_TABLE_HEADER sdt;
2512 	ACPI_TABLE_HEADER *ssdt;
2513 	uint8_t sum;
2514 
2515 	/* Create a new checksum to account for the DSDT and any SSDTs. */
2516 	sdt = *dsdt;
2517 	if (rsdt != NULL) {
2518 		sdt.Checksum = 0;
2519 		sum = acpi_checksum(dsdt + 1, dsdt->Length -
2520 		    sizeof(ACPI_TABLE_HEADER));
2521 		ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, NULL);
2522 		while (ssdt != NULL) {
2523 			sdt.Length += ssdt->Length - sizeof(ACPI_TABLE_HEADER);
2524 			sum += acpi_checksum(ssdt + 1,
2525 			    ssdt->Length - sizeof(ACPI_TABLE_HEADER));
2526 			ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, ssdt);
2527 		}
2528 		sum += acpi_checksum(&sdt, sizeof(ACPI_TABLE_HEADER));
2529 		sdt.Checksum -= sum;
2530 	}
2531 
2532 	/* Write out the DSDT header and body. */
2533 	write(fd, &sdt, sizeof(ACPI_TABLE_HEADER));
2534 	write(fd, dsdt + 1, dsdt->Length - sizeof(ACPI_TABLE_HEADER));
2535 
2536 	/* Write out any SSDTs (if present.) */
2537 	if (rsdt != NULL) {
2538 		ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
2539 		while (ssdt != NULL) {
2540 			write(fd, ssdt + 1, ssdt->Length -
2541 			    sizeof(ACPI_TABLE_HEADER));
2542 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
2543 		}
2544 	}
2545 	return (0);
2546 }
2547 
2548 void
2549 dsdt_save_file(char *outfile, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
2550 {
2551 	int	fd;
2552 	mode_t	mode;
2553 
2554 	assert(outfile != NULL);
2555 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2556 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
2557 	if (fd == -1) {
2558 		perror("dsdt_save_file");
2559 		return;
2560 	}
2561 	write_dsdt(fd, rsdt, dsdp);
2562 	close(fd);
2563 }
2564 
2565 void
2566 aml_disassemble(ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
2567 {
2568 	char buf[PATH_MAX], tmpstr[PATH_MAX], wrkdir[PATH_MAX];
2569 	const char *iname = "/acpdump.din";
2570 	const char *oname = "/acpdump.dsl";
2571 	const char *tmpdir;
2572 	FILE *fp;
2573 	size_t len;
2574 	int fd, status;
2575 	pid_t pid;
2576 
2577 	tmpdir = getenv("TMPDIR");
2578 	if (tmpdir == NULL)
2579 		tmpdir = _PATH_TMP;
2580 	if (realpath(tmpdir, buf) == NULL) {
2581 		perror("realpath tmp dir");
2582 		return;
2583 	}
2584 	len = sizeof(wrkdir) - strlen(iname);
2585 	if ((size_t)snprintf(wrkdir, len, "%s/acpidump.XXXXXX", buf) > len-1 ) {
2586 		fprintf(stderr, "$TMPDIR too long\n");
2587 		return;
2588 	}
2589 	if  (mkdtemp(wrkdir) == NULL) {
2590 		perror("mkdtemp tmp working dir");
2591 		return;
2592 	}
2593 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, iname);
2594 	assert(len <= sizeof(tmpstr) - 1);
2595 	fd = open(tmpstr, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
2596 	if (fd < 0) {
2597 		perror("iasl tmp file");
2598 		return;
2599 	}
2600 	write_dsdt(fd, rsdt, dsdp);
2601 	close(fd);
2602 
2603 	/* Run iasl -d on the temp file */
2604 	if ((pid = fork()) == 0) {
2605 		close(STDOUT_FILENO);
2606 		if (vflag == 0)
2607 			close(STDERR_FILENO);
2608 		execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
2609 		err(1, "exec");
2610 	}
2611 	if (pid > 0)
2612 		wait(&status);
2613 	if (unlink(tmpstr) < 0) {
2614 		perror("unlink");
2615 		goto out;
2616 	}
2617 	if (pid < 0) {
2618 		perror("fork");
2619 		goto out;
2620 	}
2621 	if (status != 0) {
2622 		fprintf(stderr, "iasl exit status = %d\n", status);
2623 	}
2624 
2625 	/* Dump iasl's output to stdout */
2626 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, oname);
2627 	assert(len <= sizeof(tmpstr) - 1);
2628 	fp = fopen(tmpstr, "r");
2629 	if (unlink(tmpstr) < 0) {
2630 		perror("unlink");
2631 		goto out;
2632 	}
2633 	if (fp == NULL) {
2634 		perror("iasl tmp file (read)");
2635 		goto out;
2636 	}
2637 	while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
2638 		fwrite(buf, 1, len, stdout);
2639 	fclose(fp);
2640 
2641     out:
2642 	if (rmdir(wrkdir) < 0)
2643 		perror("rmdir");
2644 }
2645 
2646 void
2647 sdt_print_all(ACPI_TABLE_HEADER *rsdp)
2648 {
2649 	acpi_handle_rsdt(rsdp);
2650 }
2651 
2652 /* Fetch a table matching the given signature via the RSDT. */
2653 ACPI_TABLE_HEADER *
2654 sdt_from_rsdt(ACPI_TABLE_HEADER *rsdp, const char *sig, ACPI_TABLE_HEADER *last)
2655 {
2656 	ACPI_TABLE_HEADER *sdt;
2657 	ACPI_TABLE_RSDT *rsdt;
2658 	ACPI_TABLE_XSDT *xsdt;
2659 	vm_offset_t addr;
2660 	int entries, i;
2661 
2662 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2663 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2664 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2665 	for (i = 0; i < entries; i++) {
2666 		if (addr_size == 4)
2667 			addr = le32toh(rsdt->TableOffsetEntry[i]);
2668 		else
2669 			addr = le64toh(xsdt->TableOffsetEntry[i]);
2670 		if (addr == 0)
2671 			continue;
2672 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
2673 		if (last != NULL) {
2674 			if (sdt == last)
2675 				last = NULL;
2676 			continue;
2677 		}
2678 		if (memcmp(sdt->Signature, sig, strlen(sig)))
2679 			continue;
2680 		if (acpi_checksum(sdt, sdt->Length))
2681 			errx(1, "RSDT entry %d is corrupt", i);
2682 		return (sdt);
2683 	}
2684 
2685 	return (NULL);
2686 }
2687 
2688 ACPI_TABLE_HEADER *
2689 dsdt_from_fadt(ACPI_TABLE_FADT *fadt)
2690 {
2691 	ACPI_TABLE_HEADER	*sdt;
2692 
2693 	/* Use the DSDT address if it is version 1, otherwise use XDSDT. */
2694 	if (acpi_get_fadt_revision(fadt) == 1)
2695 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
2696 	else
2697 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
2698 	if (acpi_checksum(sdt, sdt->Length))
2699 		errx(1, "DSDT is corrupt\n");
2700 	return (sdt);
2701 }
2702