xref: /freebsd/usr.sbin/acpi/acpidump/acpi.c (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
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 
1261 static void acpi_handle_tpm2(ACPI_TABLE_HEADER *sdp)
1262 {
1263 	ACPI_TABLE_TPM2 *tpm2;
1264 
1265 	printf (BEGIN_COMMENT);
1266 	acpi_print_sdt(sdp);
1267 	tpm2 = (ACPI_TABLE_TPM2 *) sdp;
1268 	printf ("\t\tControlArea=%jx\n", tpm2->ControlAddress);
1269 	printf ("\t\tStartMethod=%x\n", tpm2->StartMethod);
1270 	printf (END_COMMENT);
1271 }
1272 
1273 static int spcr_xlate_baud(uint8_t r)
1274 {
1275 	static int rates[] = { 9600, 19200, -1, 57600, 115200 };
1276 	_Static_assert(nitems(rates) == 7 - 3 + 1, "rates array size incorrect");
1277 
1278 	if (r == 0)
1279 		return (0);
1280 
1281 	if (r < 3 || r > 7)
1282 		return (-1);
1283 
1284 	return (rates[r - 3]);
1285 }
1286 
1287 static const char *spcr_interface_type(int ift)
1288 {
1289 	static const char *if_names[] = {
1290 		[0x00] = "Fully 16550-compatible",
1291 		[0x01] = "16550 subset compatible with DBGP Revision 1",
1292 		[0x02] = "MAX311xE SPI UART",
1293 		[0x03] = "Arm PL011 UART",
1294 		[0x04] = "MSM8x60 (e.g. 8960)",
1295 		[0x05] = "Nvidia 16550",
1296 		[0x06] = "TI OMAP",
1297 		[0x07] = "Reserved (Do Not Use)",
1298 		[0x08] = "APM88xxxx",
1299 		[0x09] = "MSM8974",
1300 		[0x0a] = "SAM5250",
1301 		[0x0b] = "Intel USIF",
1302 		[0x0c] = "i.MX 6",
1303 		[0x0d] = "(deprecated) Arm SBSA (2.x only) Generic UART supporting only 32-bit accesses",
1304 		[0x0e] = "Arm SBSA Generic UART",
1305 		[0x0f] = "Arm DCC",
1306 		[0x10] = "BCM2835",
1307 		[0x11] = "SDM845 with clock rate of 1.8432 MHz",
1308 		[0x12] = "16550-compatible with parameters defined in Generic Address Structure",
1309 		[0x13] = "SDM845 with clock rate of 7.372 MHz",
1310 		[0x14] = "Intel LPSS",
1311 		[0x15] = "RISC-V SBI console (any supported SBI mechanism)",
1312 	};
1313 
1314 	if (ift >= (int)nitems(if_names) || if_names[ift] == NULL)
1315 		return ("Reserved");
1316 	return (if_names[ift]);
1317 }
1318 
1319 static const char *spcr_interrupt_type(int ift)
1320 {
1321 	static char buf[100];
1322 
1323 #define APPEND(b,s) \
1324 	if ((ift & (b)) != 0) { \
1325 		if (strlen(buf) > 0) \
1326 			strlcat(buf, ",", sizeof(buf)); \
1327 		strlcat(buf, s, sizeof(buf)); \
1328 	}
1329 
1330 	*buf = '\0';
1331 	APPEND(0x01, "PC/AT IRQ");
1332 	APPEND(0x02, "I/O APIC");
1333 	APPEND(0x04, "I/O SAPIC");
1334 	APPEND(0x08, "ARMH GIC");
1335 	APPEND(0x10, "RISC-V PLIC/APLIC");
1336 
1337 #undef APPEND
1338 
1339 	return (buf);
1340 }
1341 
1342 static const char *spcr_terminal_type(int type)
1343 {
1344 	static const char *term_names[] = {
1345 		[0] = "VT100",
1346 		[1] = "Extended VT100",
1347 		[2] = "VT-UTF8",
1348 		[3] = "ANSI",
1349 	};
1350 
1351 	if (type >= (int)nitems(term_names) || term_names[type] == NULL)
1352 		return ("Reserved");
1353 	return (term_names[type]);
1354 }
1355 
1356 static void acpi_handle_spcr(ACPI_TABLE_HEADER *sdp)
1357 {
1358 	ACPI_TABLE_SPCR *spcr;
1359 
1360 	printf (BEGIN_COMMENT);
1361 	acpi_print_sdt(sdp);
1362 
1363 	/* Rev 1 and 2 are the same size */
1364 	spcr = (ACPI_TABLE_SPCR *) sdp;
1365 	printf ("\tInterfaceType=%d (%s)\n", spcr->InterfaceType,
1366 	    spcr_interface_type(spcr->InterfaceType));
1367 	printf ("\tSerialPort=");
1368 	acpi_print_gas(&spcr->SerialPort);
1369 	printf ("\n\tInterruptType=%#x (%s)\n", spcr->InterruptType,
1370 	    spcr_interrupt_type(spcr->InterruptType));
1371 	printf ("\tPcInterrupt=%d (%s)\n", spcr->PcInterrupt,
1372 	    (spcr->InterruptType & 0x1) ? "Valid" : "Invalid");
1373 	printf ("\tInterrupt=%d\n", spcr->Interrupt);
1374 	printf ("\tBaudRate=%d (%d)\n", spcr_xlate_baud(spcr->BaudRate), spcr->BaudRate);
1375 	printf ("\tParity=%d\n", spcr->Parity);
1376 	printf ("\tStopBits=%d\n", spcr->StopBits);
1377 	printf ("\tFlowControl=%d\n", spcr->FlowControl);
1378 	printf ("\tTerminalType=%d (%s)\n", spcr->TerminalType,
1379 	    spcr_terminal_type(spcr->TerminalType));
1380 	printf ("\tPciDeviceId=%#04x\n", spcr->PciDeviceId);
1381 	printf ("\tPciVendorId=%#04x\n", spcr->PciVendorId);
1382 	printf ("\tPciBus=%d\n", spcr->PciBus);
1383 	printf ("\tPciDevice=%d\n", spcr->PciDevice);
1384 	printf ("\tPciFunction=%d\n", spcr->PciFunction);
1385 	printf ("\tPciFlags=%d\n", spcr->PciFlags);
1386 	printf ("\tPciSegment=%d\n", spcr->PciSegment);
1387 
1388 	/* Rev 3 added UartClkFrequency */
1389 	if (sdp->Revision >= 3) {
1390 		printf("\tLanguage=%d\n", spcr->Language);
1391 		printf("\tUartClkFreq=%jd",
1392 		    (uintmax_t)spcr->UartClkFreq);
1393 	}
1394 
1395 	/* Rev 4 added PreciseBaudrate and NameSpace* */
1396 	if (sdp->Revision >= 4) {
1397 		printf("\tPreciseBaudrate=%jd",
1398 		    (uintmax_t)spcr->PreciseBaudrate);
1399 		if (spcr->NameSpaceStringLength > 0 &&
1400 		    spcr->NameSpaceStringOffset >= sizeof(*spcr) &&
1401 		    sdp->Length >= spcr->NameSpaceStringOffset +
1402 		        spcr->NameSpaceStringLength) {
1403 			printf ("\tNameSpaceString='%s'\n",
1404 			    (char *)sdp + spcr->NameSpaceStringOffset);
1405 		}
1406 	}
1407 
1408 	printf (END_COMMENT);
1409 }
1410 
1411 static const char *
1412 devscope_type2str(int type)
1413 {
1414 	static char typebuf[16];
1415 
1416 	switch (type) {
1417 	case 1:
1418 		return ("PCI Endpoint Device");
1419 	case 2:
1420 		return ("PCI Sub-Hierarchy");
1421 	case 3:
1422 		return ("IOAPIC");
1423 	case 4:
1424 		return ("HPET");
1425 	default:
1426 		snprintf(typebuf, sizeof(typebuf), "%d", type);
1427 		return (typebuf);
1428 	}
1429 }
1430 
1431 static int
1432 acpi_handle_dmar_devscope(void *addr, int remaining)
1433 {
1434 	char sep;
1435 	int pathlen;
1436 	ACPI_DMAR_PCI_PATH *path, *pathend;
1437 	ACPI_DMAR_DEVICE_SCOPE *devscope = addr;
1438 
1439 	if (remaining < (int)sizeof(ACPI_DMAR_DEVICE_SCOPE))
1440 		return (-1);
1441 
1442 	if (remaining < devscope->Length)
1443 		return (-1);
1444 
1445 	printf("\n");
1446 	printf("\t\tType=%s\n", devscope_type2str(devscope->EntryType));
1447 	printf("\t\tLength=%d\n", devscope->Length);
1448 	printf("\t\tEnumerationId=%d\n", devscope->EnumerationId);
1449 	printf("\t\tStartBusNumber=%d\n", devscope->Bus);
1450 
1451 	path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
1452 	pathlen = devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE);
1453 	pathend = path + pathlen / sizeof(ACPI_DMAR_PCI_PATH);
1454 	if (path < pathend) {
1455 		sep = '{';
1456 		printf("\t\tPath=");
1457 		do {
1458 			printf("%c%d:%d", sep, path->Device, path->Function);
1459 			sep=',';
1460 			path++;
1461 		} while (path < pathend);
1462 		printf("}\n");
1463 	}
1464 
1465 	return (devscope->Length);
1466 }
1467 
1468 static void
1469 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
1470 {
1471 	char *cp;
1472 	int remaining, consumed;
1473 
1474 	printf("\n");
1475 	printf("\tType=DRHD\n");
1476 	printf("\tLength=%d\n", drhd->Header.Length);
1477 
1478 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1479 
1480 	printf("\tFlags=");
1481 	PRINTFLAG(drhd->Flags, INCLUDE_ALL);
1482 	PRINTFLAG_END();
1483 
1484 #undef PRINTFLAG
1485 
1486 	printf("\tSegment=%d\n", drhd->Segment);
1487 	printf("\tAddress=0x%016jx\n", (uintmax_t)drhd->Address);
1488 
1489 	remaining = drhd->Header.Length - sizeof(ACPI_DMAR_HARDWARE_UNIT);
1490 	if (remaining > 0)
1491 		printf("\tDevice Scope:");
1492 	while (remaining > 0) {
1493 		cp = (char *)drhd + drhd->Header.Length - remaining;
1494 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1495 		if (consumed <= 0)
1496 			break;
1497 		else
1498 			remaining -= consumed;
1499 	}
1500 }
1501 
1502 static void
1503 acpi_handle_dmar_rmrr(ACPI_DMAR_RESERVED_MEMORY *rmrr)
1504 {
1505 	char *cp;
1506 	int remaining, consumed;
1507 
1508 	printf("\n");
1509 	printf("\tType=RMRR\n");
1510 	printf("\tLength=%d\n", rmrr->Header.Length);
1511 	printf("\tSegment=%d\n", rmrr->Segment);
1512 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rmrr->BaseAddress);
1513 	printf("\tLimitAddress=0x%016jx\n", (uintmax_t)rmrr->EndAddress);
1514 
1515 	remaining = rmrr->Header.Length - sizeof(ACPI_DMAR_RESERVED_MEMORY);
1516 	if (remaining > 0)
1517 		printf("\tDevice Scope:");
1518 	while (remaining > 0) {
1519 		cp = (char *)rmrr + rmrr->Header.Length - remaining;
1520 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1521 		if (consumed <= 0)
1522 			break;
1523 		else
1524 			remaining -= consumed;
1525 	}
1526 }
1527 
1528 static void
1529 acpi_handle_dmar_atsr(ACPI_DMAR_ATSR *atsr)
1530 {
1531 	char *cp;
1532 	int remaining, consumed;
1533 
1534 	printf("\n");
1535 	printf("\tType=ATSR\n");
1536 	printf("\tLength=%d\n", atsr->Header.Length);
1537 
1538 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1539 
1540 	printf("\tFlags=");
1541 	PRINTFLAG(atsr->Flags, ALL_PORTS);
1542 	PRINTFLAG_END();
1543 
1544 #undef PRINTFLAG
1545 
1546 	printf("\tSegment=%d\n", atsr->Segment);
1547 
1548 	remaining = atsr->Header.Length - sizeof(ACPI_DMAR_ATSR);
1549 	if (remaining > 0)
1550 		printf("\tDevice Scope:");
1551 	while (remaining > 0) {
1552 		cp = (char *)atsr + atsr->Header.Length - remaining;
1553 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1554 		if (consumed <= 0)
1555 			break;
1556 		else
1557 			remaining -= consumed;
1558 	}
1559 }
1560 
1561 static void
1562 acpi_handle_dmar_rhsa(ACPI_DMAR_RHSA *rhsa)
1563 {
1564 
1565 	printf("\n");
1566 	printf("\tType=RHSA\n");
1567 	printf("\tLength=%d\n", rhsa->Header.Length);
1568 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rhsa->BaseAddress);
1569 	printf("\tProximityDomain=0x%08x\n", rhsa->ProximityDomain);
1570 }
1571 
1572 static int
1573 acpi_handle_dmar_remapping_structure(void *addr, int remaining)
1574 {
1575 	ACPI_DMAR_HEADER *hdr = addr;
1576 
1577 	if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
1578 		return (-1);
1579 
1580 	if (remaining < hdr->Length)
1581 		return (-1);
1582 
1583 	switch (hdr->Type) {
1584 	case ACPI_DMAR_TYPE_HARDWARE_UNIT:
1585 		acpi_handle_dmar_drhd(addr);
1586 		break;
1587 	case ACPI_DMAR_TYPE_RESERVED_MEMORY:
1588 		acpi_handle_dmar_rmrr(addr);
1589 		break;
1590 	case ACPI_DMAR_TYPE_ROOT_ATS:
1591 		acpi_handle_dmar_atsr(addr);
1592 		break;
1593 	case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
1594 		acpi_handle_dmar_rhsa(addr);
1595 		break;
1596 	default:
1597 		printf("\n");
1598 		printf("\tType=%d\n", hdr->Type);
1599 		printf("\tLength=%d\n", hdr->Length);
1600 		break;
1601 	}
1602 	return (hdr->Length);
1603 }
1604 
1605 #ifndef ACPI_DMAR_X2APIC_OPT_OUT
1606 #define	ACPI_DMAR_X2APIC_OPT_OUT	(0x2)
1607 #endif
1608 
1609 static void
1610 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
1611 {
1612 	char *cp;
1613 	int remaining, consumed;
1614 	ACPI_TABLE_DMAR *dmar;
1615 
1616 	printf(BEGIN_COMMENT);
1617 	acpi_print_sdt(sdp);
1618 	dmar = (ACPI_TABLE_DMAR *)sdp;
1619 	printf("\tHost Address Width=%d\n", dmar->Width + 1);
1620 
1621 #define PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1622 
1623 	printf("\tFlags=");
1624 	PRINTFLAG(dmar->Flags, INTR_REMAP);
1625 	PRINTFLAG(dmar->Flags, X2APIC_OPT_OUT);
1626 	PRINTFLAG_END();
1627 
1628 #undef PRINTFLAG
1629 
1630 	remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR);
1631 	while (remaining > 0) {
1632 		cp = (char *)sdp + sdp->Length - remaining;
1633 		consumed = acpi_handle_dmar_remapping_structure(cp, remaining);
1634 		if (consumed <= 0)
1635 			break;
1636 		else
1637 			remaining -= consumed;
1638 	}
1639 
1640 	printf(END_COMMENT);
1641 }
1642 
1643 static void
1644 acpi_handle_ivrs_ivhd_header(ACPI_IVRS_HEADER *addr)
1645 {
1646 	printf("\n\tIVHD Type=%#x IOMMU DeviceId=%#06x\n\tFlags=",
1647 	    addr->Type, addr->DeviceId);
1648 #define PRINTFLAG(flag, name) printflag(addr->Flags, flag, #name)
1649 	PRINTFLAG(ACPI_IVHD_TT_ENABLE, HtTunEn);
1650 	PRINTFLAG(ACPI_IVHD_ISOC, PassPW);
1651 	PRINTFLAG(ACPI_IVHD_RES_PASS_PW, ResPassPW);
1652 	PRINTFLAG(ACPI_IVHD_ISOC, Isoc);
1653 	PRINTFLAG(ACPI_IVHD_TT_ENABLE, IotlbSup);
1654 	PRINTFLAG((1 << 5), Coherent);
1655 	PRINTFLAG((1 << 6), PreFSup);
1656 	PRINTFLAG((1 << 7), PPRSup);
1657 #undef PRINTFLAG
1658 	PRINTFLAG_END();
1659 }
1660 
1661 static void
1662 acpi_handle_ivrs_ivhd_dte(UINT8 dte)
1663 {
1664 	if (dte == 0) {
1665 		printf("\n");
1666 		return;
1667 	}
1668 	printf(" DTE=");
1669 #define PRINTFLAG(flag, name) printflag(dte, flag, #name)
1670 	PRINTFLAG(ACPI_IVHD_INIT_PASS, INITPass);
1671 	PRINTFLAG(ACPI_IVHD_EINT_PASS, EIntPass);
1672 	PRINTFLAG(ACPI_IVHD_NMI_PASS, NMIPass);
1673 	PRINTFLAG(ACPI_IVHD_SYSTEM_MGMT, SysMgtPass);
1674 	PRINTFLAG(ACPI_IVHD_LINT0_PASS, Lint0Pass);
1675 	PRINTFLAG(ACPI_IVHD_LINT1_PASS, Lint1Pass);
1676 #undef PRINTFLAG
1677 	PRINTFLAG_END();
1678 }
1679 
1680 static void
1681 acpi_handle_ivrs_ivhd_edte(UINT32 edte)
1682 {
1683 	if (edte == 0)
1684 		return;
1685 	printf("\t\t ExtDTE=");
1686 #define PRINTFLAG(flag, name) printflag(edte, flag, #name)
1687 	PRINTFLAG(ACPI_IVHD_ATS_DISABLED, AtsDisabled);
1688 #undef PRINTFLAG
1689 	PRINTFLAG_END();
1690 }
1691 
1692 static const char *
1693 acpi_handle_ivrs_ivhd_variety(UINT8 v)
1694 {
1695 	switch (v) {
1696 	case ACPI_IVHD_IOAPIC:
1697 		return ("IOAPIC");
1698 	case ACPI_IVHD_HPET:
1699 		return ("HPET");
1700 	default:
1701 		return ("UNKNOWN");
1702 	}
1703 }
1704 
1705 static void
1706 acpi_handle_ivrs_ivhd_devs(ACPI_IVRS_DE_HEADER *d, char *de)
1707 {
1708 	char *db;
1709 	ACPI_IVRS_DEVICE4 *d4;
1710 	ACPI_IVRS_DEVICE8A *d8a;
1711 	ACPI_IVRS_DEVICE8B *d8b;
1712 	ACPI_IVRS_DEVICE8C *d8c;
1713 	ACPI_IVRS_DEVICE_HID *dh;
1714 	size_t len;
1715 	UINT32 x32;
1716 
1717 	for (; (char *)d < de; d = (ACPI_IVRS_DE_HEADER *)(db + len)) {
1718 		db = (char *)d;
1719 		if (d->Type == ACPI_IVRS_TYPE_PAD4) {
1720 			len = sizeof(*d4);
1721 		} else if (d->Type == ACPI_IVRS_TYPE_ALL) {
1722 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1723 			len = sizeof(*d4);
1724 			printf("\t\tDev Type=%#x Id=ALL", d4->Header.Type);
1725 			acpi_handle_ivrs_ivhd_dte(d4->Header.DataSetting);
1726 		} else if (d->Type == ACPI_IVRS_TYPE_SELECT) {
1727 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1728 			len = sizeof(*d4);
1729 			printf("\t\tDev Type=%#x Id=%#06x", d4->Header.Type,
1730 			    d4->Header.Id);
1731 			acpi_handle_ivrs_ivhd_dte(d4->Header.DataSetting);
1732 		} else if (d->Type == ACPI_IVRS_TYPE_START) {
1733 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1734 			len = 2 * sizeof(*d4);
1735 			printf("\t\tDev Type=%#x Id=%#06x-%#06x",
1736 			    d4->Header.Type,
1737 			    d4->Header.Id, (d4 + 1)->Header.Id);
1738 			acpi_handle_ivrs_ivhd_dte(d4->Header.DataSetting);
1739 		} else if (d->Type == ACPI_IVRS_TYPE_END) {
1740 			d4 = (ACPI_IVRS_DEVICE4 *)db;
1741 			len = 2 * sizeof(*d4);
1742 			printf("\t\tDev Type=%#x Id=%#06x BIOS BUG\n",
1743 			    d4->Header.Type, d4->Header.Id);
1744 		} else if (d->Type == ACPI_IVRS_TYPE_PAD8) {
1745 			len = sizeof(*d8a);
1746 		} else if (d->Type == ACPI_IVRS_TYPE_ALIAS_SELECT) {
1747 			d8a = (ACPI_IVRS_DEVICE8A *)db;
1748 			len = sizeof(*d8a);
1749 			printf("\t\tDev Type=%#x Id=%#06x AliasId=%#06x",
1750 			    d8a->Header.Type, d8a->Header.Id, d8a->UsedId);
1751 			acpi_handle_ivrs_ivhd_dte(d8a->Header.DataSetting);
1752 		} else if (d->Type == ACPI_IVRS_TYPE_ALIAS_START) {
1753 			d8a = (ACPI_IVRS_DEVICE8A *)db;
1754 			d4 = (ACPI_IVRS_DEVICE4 *)(db + sizeof(*d8a));
1755 			len = sizeof(*d8a) + sizeof(*d4);
1756 			printf("\t\tDev Type=%#x Id=%#06x-%#06x AliasId=%#06x",
1757 			    d8a->Header.Type, d8a->Header.Id, d4->Header.Id,
1758 			    d8a->UsedId);
1759 			acpi_handle_ivrs_ivhd_dte(d8a->Header.DataSetting);
1760 		} else if (d->Type == ACPI_IVRS_TYPE_EXT_SELECT) {
1761 			d8b = (ACPI_IVRS_DEVICE8B *)db;
1762 			len = sizeof(*d8b);
1763 			printf("\t\tDev Type=%#x Id=%#06x",
1764 			    d8a->Header.Type, d8a->Header.Id);
1765 			acpi_handle_ivrs_ivhd_dte(d8b->Header.DataSetting);
1766 			printf("\t\t");
1767 			acpi_handle_ivrs_ivhd_edte(d8b->ExtendedData);
1768 		} else if (d->Type == ACPI_IVRS_TYPE_EXT_START) {
1769 			d8b = (ACPI_IVRS_DEVICE8B *)db;
1770 			len = sizeof(*d8b);
1771 			d4 = (ACPI_IVRS_DEVICE4 *)(db + sizeof(*d8b));
1772 			len = sizeof(*d8b) + sizeof(*d4);
1773 			printf("\t\tDev Type=%#x Id=%#06x-%#06x",
1774 			    d8a->Header.Type, d8a->Header.Id, d4->Header.Id);
1775 			acpi_handle_ivrs_ivhd_dte(d8b->Header.DataSetting);
1776 			acpi_handle_ivrs_ivhd_edte(d8b->ExtendedData);
1777 		} else if (d->Type == ACPI_IVRS_TYPE_SPECIAL) {
1778 			d8c = (ACPI_IVRS_DEVICE8C *)db;
1779 			len = sizeof(*d8c);
1780 			printf("\t\tDev Type=%#x Id=%#06x Handle=%#x "
1781 			    "Variety=%d(%s)",
1782 			    d8c->Header.Type, d8c->UsedId, d8c->Handle,
1783 			    d8c->Variety,
1784 			    acpi_handle_ivrs_ivhd_variety(d8c->Variety));
1785 			acpi_handle_ivrs_ivhd_dte(d8c->Header.DataSetting);
1786 		} else if (d->Type == ACPI_IVRS_TYPE_HID) {
1787 			dh = (ACPI_IVRS_DEVICE_HID *)db;
1788 			len = sizeof(*dh) + dh->UidLength;
1789 			printf("\t\tDev Type=%#x Id=%#06x HID=",
1790 			    dh->Header.Type, dh->Header.Id);
1791 			acpi_print_string((char *)&dh->AcpiHid,
1792 			    sizeof(dh->AcpiHid));
1793 			printf(" CID=");
1794 			acpi_print_string((char *)&dh->AcpiCid,
1795 			    sizeof(dh->AcpiCid));
1796 			printf(" UID=");
1797 			switch (dh->UidType) {
1798 			case ACPI_IVRS_UID_NOT_PRESENT:
1799 			default:
1800 				printf("none");
1801 				break;
1802 			case ACPI_IVRS_UID_IS_INTEGER:
1803 				memcpy(&x32, dh + 1, sizeof(x32));
1804 				printf("%#x", x32);
1805 				break;
1806 			case ACPI_IVRS_UID_IS_STRING:
1807 				acpi_print_string((char *)(dh + 1),
1808 				    dh->UidLength);
1809 				break;
1810 			}
1811 			acpi_handle_ivrs_ivhd_dte(dh->Header.DataSetting);
1812 		} else {
1813 			printf("\t\tDev Type=%#x Unknown\n", d->Type);
1814 			if (d->Type <= 63)
1815 				len = sizeof(*d4);
1816 			else if (d->Type <= 127)
1817 				len = sizeof(*d8a);
1818 			else {
1819 				printf("Abort, cannot advance iterator.\n");
1820 				return;
1821 			}
1822 		}
1823 	}
1824 }
1825 
1826 static void
1827 acpi_handle_ivrs_ivhd_10(ACPI_IVRS_HARDWARE1 *addr, bool efrsup)
1828 {
1829 	acpi_handle_ivrs_ivhd_header(&addr->Header);
1830 	printf("\tCapOffset=%#x Base=%#jx PCISeg=%#x Unit=%#x MSIlog=%d\n",
1831 	    addr->CapabilityOffset, (uintmax_t)addr->BaseAddress,
1832 	    addr->PciSegmentGroup, (addr->Info & ACPI_IVHD_UNIT_ID_MASK) >> 8,
1833 	    addr->Info & ACPI_IVHD_MSI_NUMBER_MASK);
1834 	if (efrsup) {
1835 #define PRINTFLAG(flag, name) printflag(addr->FeatureReporting, flag, #name)
1836 #define PRINTFIELD(lbit, hbit, name) \
1837     printfield(addr->FeatureReporting, lbit, hbit, #name)
1838 		PRINTFIELD(30, 31, HATS);
1839 		PRINTFIELD(28, 29, GATS);
1840 		PRINTFIELD(23, 27, MsiNumPPR);
1841 		PRINTFIELD(17, 22, PNBanks);
1842 		PRINTFIELD(13, 16, PNCounters);
1843 		PRINTFIELD(8, 12, PASmax);
1844 		PRINTFLAG(1 << 7, HESup);
1845 		PRINTFLAG(1 << 6, GASup);
1846 		PRINTFLAG(1 << 5, UASup);
1847 		PRINTFIELD(3, 2, GLXSup);
1848 		PRINTFLAG(1 << 1, NXSup);
1849 		PRINTFLAG(1 << 0, XTSup);
1850 #undef PRINTFLAG
1851 #undef PRINTFIELD
1852 		PRINTFLAG_END();
1853 	}
1854 	acpi_handle_ivrs_ivhd_devs((ACPI_IVRS_DE_HEADER *)(addr + 1),
1855 	    (char *)addr + addr->Header.Length);
1856 }
1857 
1858 static void
1859 acpi_handle_ivrs_ivhd_info_11(ACPI_IVRS_HARDWARE2 *addr)
1860 {
1861 	acpi_handle_ivrs_ivhd_header(&addr->Header);
1862 	printf("\tCapOffset=%#x Base=%#jx PCISeg=%#x Unit=%#x MSIlog=%d\n",
1863 	    addr->CapabilityOffset, (uintmax_t)addr->BaseAddress,
1864 	    addr->PciSegmentGroup, (addr->Info >> 8) & 0x1f,
1865 	    addr->Info & 0x5);
1866 	printf("\tAttr=");
1867 #define PRINTFIELD(lbit, hbit, name) \
1868     printfield(addr->Attributes, lbit, hbit, #name)
1869 	PRINTFIELD(23, 27, MsiNumPPR);
1870 	PRINTFIELD(17, 22, PNBanks);
1871 	PRINTFIELD(13, 16, PNCounters);
1872 #undef PRINTFIELD
1873 	PRINTFLAG_END();
1874 }
1875 
1876 static void
1877 acpi_handle_ivrs_ivhd_11(ACPI_IVRS_HARDWARE2 *addr)
1878 {
1879 	acpi_handle_ivrs_ivhd_info_11(addr);
1880 	printf("\tEFRreg=%#018jx\n", (uintmax_t)addr->EfrRegisterImage);
1881 	acpi_handle_ivrs_ivhd_devs((ACPI_IVRS_DE_HEADER *)(addr + 1),
1882 	    (char *)addr + addr->Header.Length);
1883 }
1884 
1885 static void
1886 acpi_handle_ivrs_ivhd_40(ACPI_IVRS_HARDWARE2 *addr)
1887 {
1888 	acpi_handle_ivrs_ivhd_info_11(addr);
1889 	printf("\tEFRreg=%#018jx EFR2reg=%#018jx\n",
1890 	    (uintmax_t)addr->EfrRegisterImage, (uintmax_t)addr->Reserved);
1891 	acpi_handle_ivrs_ivhd_devs((ACPI_IVRS_DE_HEADER *)(addr + 1),
1892 	    (char *)addr + addr->Header.Length);
1893 }
1894 
1895 static const char *
1896 acpi_handle_ivrs_ivmd_type(ACPI_IVRS_MEMORY *addr)
1897 {
1898 	switch (addr->Header.Type) {
1899 	case ACPI_IVRS_TYPE_MEMORY1:
1900 		return ("ALL");
1901 	case ACPI_IVRS_TYPE_MEMORY2:
1902 		return ("specified");
1903 	case ACPI_IVRS_TYPE_MEMORY3:
1904 		return ("range");
1905 	default:
1906 		return ("unknown");
1907 	}
1908 }
1909 
1910 static void
1911 acpi_handle_ivrs_ivmd(ACPI_IVRS_MEMORY *addr)
1912 {
1913 	printf("\tMem Type=%#x(%s) ",
1914 	    addr->Header.Type, acpi_handle_ivrs_ivmd_type(addr));
1915 	switch (addr->Header.Type) {
1916 	case ACPI_IVRS_TYPE_MEMORY2:
1917 		printf("Id=%#06x PCISeg=%#x ", addr->Header.DeviceId,
1918 		    *(UINT16 *)&addr->Reserved);
1919 		break;
1920 	case ACPI_IVRS_TYPE_MEMORY3:
1921 		printf("Id=%#06x-%#06x PCISeg=%#x", addr->Header.DeviceId,
1922 		    addr->AuxData, *(UINT16 *)&addr->Reserved);
1923 		break;
1924 	}
1925 	printf("Start=%#18jx Length=%#jx Flags=",
1926 	    (uintmax_t)addr->StartAddress, (uintmax_t)addr->MemoryLength);
1927 #define PRINTFLAG(flag, name) printflag(addr->Header.Flags, flag, #name)
1928 	PRINTFLAG(ACPI_IVMD_EXCLUSION_RANGE, ExclusionRange);
1929 	PRINTFLAG(ACPI_IVMD_WRITE, IW);
1930 	PRINTFLAG(ACPI_IVMD_READ, IR);
1931 	PRINTFLAG(ACPI_IVMD_UNITY, Unity);
1932 #undef PRINTFLAG
1933 	PRINTFLAG_END();
1934 }
1935 
1936 static int
1937 acpi_handle_ivrs_blocks(void *addr, int remaining, bool efrsup)
1938 {
1939 	ACPI_IVRS_HEADER *hdr = addr;
1940 
1941 	if (remaining < (int)sizeof(ACPI_IVRS_HEADER))
1942 		return (-1);
1943 
1944 	if (remaining < hdr->Length)
1945 		return (-1);
1946 
1947 	switch (hdr->Type) {
1948 	case ACPI_IVRS_TYPE_HARDWARE1:
1949 		acpi_handle_ivrs_ivhd_10(addr, efrsup);
1950 		break;
1951 	case ACPI_IVRS_TYPE_HARDWARE2:
1952 		if (!efrsup)
1953 			printf("\t!! Found IVHD block 0x11 but !EFRsup\n");
1954 		acpi_handle_ivrs_ivhd_11(addr);
1955 		break;
1956 	case ACPI_IVRS_TYPE_HARDWARE3:
1957 		if (!efrsup)
1958 			printf("\t!! Found IVHD block 0x40 but !EFRsup\n");
1959 		acpi_handle_ivrs_ivhd_40(addr);
1960 		break;
1961 	case ACPI_IVRS_TYPE_MEMORY1:
1962 	case ACPI_IVRS_TYPE_MEMORY2:
1963 	case ACPI_IVRS_TYPE_MEMORY3:
1964 		acpi_handle_ivrs_ivmd(addr);
1965 		break;
1966 	default:
1967 		printf("\n");
1968 		printf("\tType=%d\n", hdr->Type);
1969 		printf("\tLength=%d\n", hdr->Length);
1970 		break;
1971 	}
1972 	return (hdr->Length);
1973 }
1974 
1975 #define	ACPI_IVRS_DMAREMAP	0x00000002
1976 #define	ACPI_IVRS_EFRSUP	0x00000001
1977 #define	ACPI_IVRS_GVA_SIZE	0x000000e0
1978 
1979 static void
1980 acpi_handle_ivrs(ACPI_TABLE_HEADER *sdp)
1981 {
1982 	ACPI_TABLE_IVRS *ivrs;
1983 	char *cp;
1984 	int remaining, consumed;
1985 	bool efrsup;
1986 
1987 	printf(BEGIN_COMMENT);
1988 	acpi_print_sdt(sdp);
1989 	ivrs = (ACPI_TABLE_IVRS *)sdp;
1990 	efrsup = (ivrs->Info & ACPI_IVRS_EFRSUP) != 0;
1991 	printf("\tVAsize=%d PAsize=%d GVAsize=%d\n",
1992 	    (ivrs->Info & ACPI_IVRS_VIRTUAL_SIZE) >> 15,
1993 	    (ivrs->Info & ACPI_IVRS_PHYSICAL_SIZE) >> 8,
1994 	    (ivrs->Info & ACPI_IVRS_GVA_SIZE) >> 5);
1995 	printf("\tATS_resp_res=%d DMA_preboot_remap=%d EFRsup=%d\n",
1996 	    (ivrs->Info & ACPI_IVRS_ATS_RESERVED) != 0,
1997 	    (ivrs->Info & ACPI_IVRS_DMAREMAP) != 0, efrsup);
1998 
1999 	remaining = sdp->Length - sizeof(ACPI_TABLE_IVRS);
2000 	while (remaining > 0) {
2001 		cp = (char *)sdp + sdp->Length - remaining;
2002 		consumed = acpi_handle_ivrs_blocks(cp, remaining, efrsup);
2003 		if (consumed <= 0)
2004 			break;
2005 		else
2006 			remaining -= consumed;
2007 	}
2008 
2009 	printf(END_COMMENT);
2010 }
2011 
2012 static void
2013 acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp)
2014 {
2015 
2016 	printf("\tFlags={");
2017 	if (mp->Flags & ACPI_SRAT_MEM_ENABLED)
2018 		printf("ENABLED");
2019 	else
2020 		printf("DISABLED");
2021 	if (mp->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
2022 		printf(",HOT_PLUGGABLE");
2023 	if (mp->Flags & ACPI_SRAT_MEM_NON_VOLATILE)
2024 		printf(",NON_VOLATILE");
2025 	printf("}\n");
2026 	printf("\tBase Address=0x%016jx\n", (uintmax_t)mp->BaseAddress);
2027 	printf("\tLength=0x%016jx\n", (uintmax_t)mp->Length);
2028 	printf("\tProximity Domain=%d\n", mp->ProximityDomain);
2029 }
2030 
2031 static const char *srat_types[] = {
2032     [ACPI_SRAT_TYPE_CPU_AFFINITY] = "CPU",
2033     [ACPI_SRAT_TYPE_MEMORY_AFFINITY] = "Memory",
2034     [ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY] = "X2APIC",
2035     [ACPI_SRAT_TYPE_GICC_AFFINITY] = "GICC",
2036     [ACPI_SRAT_TYPE_GIC_ITS_AFFINITY] = "GIC ITS",
2037 };
2038 
2039 static void
2040 acpi_print_srat(ACPI_SUBTABLE_HEADER *srat)
2041 {
2042 	ACPI_SRAT_CPU_AFFINITY *cpu;
2043 	ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
2044 	ACPI_SRAT_GICC_AFFINITY *gic;
2045 
2046 	if (srat->Type < nitems(srat_types))
2047 		printf("\tType=%s\n", srat_types[srat->Type]);
2048 	else
2049 		printf("\tType=%d (unknown)\n", srat->Type);
2050 	switch (srat->Type) {
2051 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
2052 		cpu = (ACPI_SRAT_CPU_AFFINITY *)srat;
2053 		acpi_print_srat_cpu(cpu->ApicId,
2054 		    cpu->ProximityDomainHi[2] << 24 |
2055 		    cpu->ProximityDomainHi[1] << 16 |
2056 		    cpu->ProximityDomainHi[0] << 0 |
2057 		    cpu->ProximityDomainLo, cpu->Flags);
2058 		break;
2059 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
2060 		acpi_print_srat_memory((ACPI_SRAT_MEM_AFFINITY *)srat);
2061 		break;
2062 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
2063 		x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)srat;
2064 		acpi_print_srat_cpu(x2apic->ApicId, x2apic->ProximityDomain,
2065 		    x2apic->Flags);
2066 		break;
2067 	case ACPI_SRAT_TYPE_GICC_AFFINITY:
2068 		gic = (ACPI_SRAT_GICC_AFFINITY *)srat;
2069 		acpi_print_srat_cpu(gic->AcpiProcessorUid, gic->ProximityDomain,
2070 		    gic->Flags);
2071 		break;
2072 	}
2073 }
2074 
2075 static void
2076 acpi_handle_srat(ACPI_TABLE_HEADER *sdp)
2077 {
2078 	ACPI_TABLE_SRAT *srat;
2079 
2080 	printf(BEGIN_COMMENT);
2081 	acpi_print_sdt(sdp);
2082 	srat = (ACPI_TABLE_SRAT *)sdp;
2083 	printf("\tTable Revision=%d\n", srat->TableRevision);
2084 	acpi_walk_subtables(sdp, (srat + 1), acpi_print_srat);
2085 	printf(END_COMMENT);
2086 }
2087 
2088 static const char *nfit_types[] = {
2089     [ACPI_NFIT_TYPE_SYSTEM_ADDRESS] = "System Address",
2090     [ACPI_NFIT_TYPE_MEMORY_MAP] = "Memory Map",
2091     [ACPI_NFIT_TYPE_INTERLEAVE] = "Interleave",
2092     [ACPI_NFIT_TYPE_SMBIOS] = "SMBIOS",
2093     [ACPI_NFIT_TYPE_CONTROL_REGION] = "Control Region",
2094     [ACPI_NFIT_TYPE_DATA_REGION] = "Data Region",
2095     [ACPI_NFIT_TYPE_FLUSH_ADDRESS] = "Flush Address",
2096     [ACPI_NFIT_TYPE_CAPABILITIES] = "Platform Capabilities"
2097 };
2098 
2099 
2100 static void
2101 acpi_print_nfit(ACPI_NFIT_HEADER *nfit)
2102 {
2103 	char *uuidstr;
2104 	uint32_t m, status;
2105 
2106 	ACPI_NFIT_SYSTEM_ADDRESS *sysaddr;
2107 	ACPI_NFIT_MEMORY_MAP *mmap;
2108 	ACPI_NFIT_INTERLEAVE *ileave;
2109 	ACPI_NFIT_CONTROL_REGION *ctlreg;
2110 	ACPI_NFIT_DATA_REGION *datareg;
2111 	ACPI_NFIT_FLUSH_ADDRESS *fladdr;
2112 	ACPI_NFIT_CAPABILITIES *caps;
2113 
2114 	if (nfit->Type < nitems(nfit_types))
2115 		printf("\tType=%s\n", nfit_types[nfit->Type]);
2116 	else
2117 		printf("\tType=%u (unknown)\n", nfit->Type);
2118 	switch (nfit->Type) {
2119 	case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
2120 		sysaddr = (ACPI_NFIT_SYSTEM_ADDRESS *)nfit;
2121 		printf("\tRangeIndex=%u\n", (u_int)sysaddr->RangeIndex);
2122 		printf("\tProximityDomain=%u\n",
2123 		    (u_int)sysaddr->ProximityDomain);
2124 		uuid_to_string((uuid_t *)(uintptr_t)(sysaddr->RangeGuid),
2125 		    &uuidstr, &status);
2126 		if (status != uuid_s_ok)
2127 			errx(1, "uuid_to_string: status=%u", status);
2128 		printf("\tRangeGuid=%s\n", uuidstr);
2129 		free(uuidstr);
2130 		printf("\tAddress=0x%016jx\n", (uintmax_t)sysaddr->Address);
2131 		printf("\tLength=0x%016jx\n", (uintmax_t)sysaddr->Length);
2132 		printf("\tMemoryMapping=0x%016jx\n",
2133 		    (uintmax_t)sysaddr->MemoryMapping);
2134 
2135 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_## flag, #flag)
2136 
2137 		printf("\tFlags=");
2138 		PRINTFLAG(sysaddr->Flags, ADD_ONLINE_ONLY);
2139 		PRINTFLAG(sysaddr->Flags, PROXIMITY_VALID);
2140 		PRINTFLAG_END();
2141 
2142 #undef PRINTFLAG
2143 
2144 		break;
2145 	case ACPI_NFIT_TYPE_MEMORY_MAP:
2146 		mmap = (ACPI_NFIT_MEMORY_MAP *)nfit;
2147 		printf("\tDeviceHandle=0x%x\n", (u_int)mmap->DeviceHandle);
2148 		printf("\tPhysicalId=0x%04x\n", (u_int)mmap->PhysicalId);
2149 		printf("\tRegionId=%u\n", (u_int)mmap->RegionId);
2150 		printf("\tRangeIndex=%u\n", (u_int)mmap->RangeIndex);
2151 		printf("\tRegionIndex=%u\n", (u_int)mmap->RegionIndex);
2152 		printf("\tRegionSize=0x%016jx\n", (uintmax_t)mmap->RegionSize);
2153 		printf("\tRegionOffset=0x%016jx\n",
2154 		    (uintmax_t)mmap->RegionOffset);
2155 		printf("\tAddress=0x%016jx\n", (uintmax_t)mmap->Address);
2156 		printf("\tInterleaveIndex=%u\n", (u_int)mmap->InterleaveIndex);
2157 		printf("\tInterleaveWays=%u\n", (u_int)mmap->InterleaveWays);
2158 
2159 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_MEM_## flag, #flag)
2160 
2161 		printf("\tFlags=");
2162 		PRINTFLAG(mmap->Flags, SAVE_FAILED);
2163 		PRINTFLAG(mmap->Flags, RESTORE_FAILED);
2164 		PRINTFLAG(mmap->Flags, FLUSH_FAILED);
2165 		PRINTFLAG(mmap->Flags, NOT_ARMED);
2166 		PRINTFLAG(mmap->Flags, HEALTH_OBSERVED);
2167 		PRINTFLAG(mmap->Flags, HEALTH_ENABLED);
2168 		PRINTFLAG(mmap->Flags, MAP_FAILED);
2169 		PRINTFLAG_END();
2170 
2171 #undef PRINTFLAG
2172 
2173 		break;
2174 	case ACPI_NFIT_TYPE_INTERLEAVE:
2175 		ileave = (ACPI_NFIT_INTERLEAVE *)nfit;
2176 		printf("\tInterleaveIndex=%u\n",
2177 		    (u_int)ileave->InterleaveIndex);
2178 		printf("\tLineCount=%u\n", (u_int)ileave->LineCount);
2179 		printf("\tLineSize=%u\n", (u_int)ileave->LineSize);
2180 		for (m = 0; m < ileave->LineCount; m++) {
2181 			printf("\tLine%uOffset=0x%08x\n", (u_int)m + 1,
2182 			    (u_int)ileave->LineOffset[m]);
2183 		}
2184 		break;
2185 	case ACPI_NFIT_TYPE_SMBIOS:
2186 		/* XXX smbios->Data[x] output is not supported */
2187 		break;
2188 	case ACPI_NFIT_TYPE_CONTROL_REGION:
2189 		ctlreg = (ACPI_NFIT_CONTROL_REGION *)nfit;
2190 		printf("\tRegionIndex=%u\n", (u_int)ctlreg->RegionIndex);
2191 		printf("\tVendorId=0x%04x\n", (u_int)ctlreg->VendorId);
2192 		printf("\tDeviceId=0x%04x\n", (u_int)ctlreg->DeviceId);
2193 		printf("\tRevisionId=0x%02x\n", (u_int)ctlreg->RevisionId);
2194 		printf("\tSubsystemVendorId=0x%04x\n",
2195 		    (u_int)ctlreg->SubsystemVendorId);
2196 		printf("\tSubsystemDeviceId=0x%04x\n",
2197 		    (u_int)ctlreg->SubsystemDeviceId);
2198 		printf("\tSubsystemRevisionId=0x%02x\n",
2199 		    (u_int)ctlreg->SubsystemRevisionId);
2200 		printf("\tValidFields=0x%02x\n", (u_int)ctlreg->ValidFields);
2201 		printf("\tManufacturingLocation=0x%02x\n",
2202 		    (u_int)ctlreg->ManufacturingLocation);
2203 		printf("\tManufacturingDate=%04x\n",
2204 		    (u_int)be16toh(ctlreg->ManufacturingDate));
2205 		printf("\tSerialNumber=%08X\n",
2206 		    (u_int)be32toh(ctlreg->SerialNumber));
2207 		printf("\tCode=0x%04x\n", (u_int)ctlreg->Code);
2208 		printf("\tWindows=%u\n", (u_int)ctlreg->Windows);
2209 		printf("\tWindowSize=0x%016jx\n",
2210 		    (uintmax_t)ctlreg->WindowSize);
2211 		printf("\tCommandOffset=0x%016jx\n",
2212 		    (uintmax_t)ctlreg->CommandOffset);
2213 		printf("\tCommandSize=0x%016jx\n",
2214 		    (uintmax_t)ctlreg->CommandSize);
2215 		printf("\tStatusOffset=0x%016jx\n",
2216 		    (uintmax_t)ctlreg->StatusOffset);
2217 		printf("\tStatusSize=0x%016jx\n",
2218 		    (uintmax_t)ctlreg->StatusSize);
2219 
2220 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_## flag, #flag)
2221 
2222 		printf("\tFlags=");
2223 		PRINTFLAG(ctlreg->Flags, CONTROL_BUFFERED);
2224 		PRINTFLAG_END();
2225 
2226 #undef PRINTFLAG
2227 
2228 		break;
2229 	case ACPI_NFIT_TYPE_DATA_REGION:
2230 		datareg = (ACPI_NFIT_DATA_REGION *)nfit;
2231 		printf("\tRegionIndex=%u\n", (u_int)datareg->RegionIndex);
2232 		printf("\tWindows=%u\n", (u_int)datareg->Windows);
2233 		printf("\tOffset=0x%016jx\n", (uintmax_t)datareg->Offset);
2234 		printf("\tSize=0x%016jx\n", (uintmax_t)datareg->Size);
2235 		printf("\tCapacity=0x%016jx\n", (uintmax_t)datareg->Capacity);
2236 		printf("\tStartAddress=0x%016jx\n",
2237 		    (uintmax_t)datareg->StartAddress);
2238 		break;
2239 	case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
2240 		fladdr = (ACPI_NFIT_FLUSH_ADDRESS *)nfit;
2241 		printf("\tDeviceHandle=%u\n", (u_int)fladdr->DeviceHandle);
2242 		printf("\tHintCount=%u\n", (u_int)fladdr->HintCount);
2243 		for (m = 0; m < fladdr->HintCount; m++) {
2244 			printf("\tHintAddress%u=0x%016jx\n", (u_int)m + 1,
2245 			    (uintmax_t)fladdr->HintAddress[m]);
2246 		}
2247 		break;
2248 	case ACPI_NFIT_TYPE_CAPABILITIES:
2249 		caps = (ACPI_NFIT_CAPABILITIES *)nfit;
2250 		printf("\tHighestCapability=%u\n", (u_int)caps->HighestCapability);
2251 
2252 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_CAPABILITY_## flag, #flag)
2253 
2254 		printf("\tCapabilities=");
2255 		PRINTFLAG(caps->Capabilities, CACHE_FLUSH);
2256 		PRINTFLAG(caps->Capabilities, MEM_FLUSH);
2257 		PRINTFLAG(caps->Capabilities, MEM_MIRRORING);
2258 		PRINTFLAG_END();
2259 
2260 #undef PRINTFLAG
2261 		break;
2262 	}
2263 }
2264 
2265 static void
2266 acpi_handle_nfit(ACPI_TABLE_HEADER *sdp)
2267 {
2268 	ACPI_TABLE_NFIT *nfit;
2269 
2270 	printf(BEGIN_COMMENT);
2271 	acpi_print_sdt(sdp);
2272 	nfit = (ACPI_TABLE_NFIT *)sdp;
2273 	acpi_walk_nfit(sdp, (nfit + 1), acpi_print_nfit);
2274 	printf(END_COMMENT);
2275 }
2276 
2277 static void
2278 acpi_print_sdt(ACPI_TABLE_HEADER *sdp)
2279 {
2280 	printf("  ");
2281 	acpi_print_string(sdp->Signature, ACPI_NAMESEG_SIZE);
2282 	printf(": Length=%d, Revision=%d, Checksum=%d,\n",
2283 	       sdp->Length, sdp->Revision, sdp->Checksum);
2284 	printf("\tOEMID=");
2285 	acpi_print_string(sdp->OemId, ACPI_OEM_ID_SIZE);
2286 	printf(", OEM Table ID=");
2287 	acpi_print_string(sdp->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
2288 	printf(", OEM Revision=0x%x,\n", sdp->OemRevision);
2289 	printf("\tCreator ID=");
2290 	acpi_print_string(sdp->AslCompilerId, ACPI_NAMESEG_SIZE);
2291 	printf(", Creator Revision=0x%x\n", sdp->AslCompilerRevision);
2292 }
2293 
2294 static void
2295 acpi_print_rsdt(ACPI_TABLE_HEADER *rsdp)
2296 {
2297 	ACPI_TABLE_RSDT *rsdt;
2298 	ACPI_TABLE_XSDT *xsdt;
2299 	int	i, entries;
2300 
2301 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2302 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2303 	printf(BEGIN_COMMENT);
2304 	acpi_print_sdt(rsdp);
2305 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2306 	printf("\tEntries={ ");
2307 	for (i = 0; i < entries; i++) {
2308 		if (i > 0)
2309 			printf(", ");
2310 		if (addr_size == 4)
2311 			printf("0x%08x", le32toh(rsdt->TableOffsetEntry[i]));
2312 		else
2313 			printf("0x%016jx",
2314 			    (uintmax_t)le64toh(xsdt->TableOffsetEntry[i]));
2315 	}
2316 	printf(" }\n");
2317 	printf(END_COMMENT);
2318 }
2319 
2320 static const char *acpi_pm_profiles[] = {
2321 	"Unspecified", "Desktop", "Mobile", "Workstation",
2322 	"Enterprise Server", "SOHO Server", "Appliance PC"
2323 };
2324 
2325 static void
2326 acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
2327 {
2328 	ACPI_TABLE_FADT *fadt;
2329 	const char *pm;
2330 
2331 	fadt = (ACPI_TABLE_FADT *)sdp;
2332 	printf(BEGIN_COMMENT);
2333 	acpi_print_sdt(sdp);
2334 	printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->Facs,
2335 	       fadt->Dsdt);
2336 	printf("\tINT_MODEL=%s\n", fadt->Model ? "APIC" : "PIC");
2337 	if (fadt->PreferredProfile >= sizeof(acpi_pm_profiles) / sizeof(char *))
2338 		pm = "Reserved";
2339 	else
2340 		pm = acpi_pm_profiles[fadt->PreferredProfile];
2341 	printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->PreferredProfile);
2342 	printf("\tSCI_INT=%d\n", fadt->SciInterrupt);
2343 	printf("\tSMI_CMD=0x%x, ", fadt->SmiCommand);
2344 	printf("ACPI_ENABLE=0x%x, ", fadt->AcpiEnable);
2345 	printf("ACPI_DISABLE=0x%x, ", fadt->AcpiDisable);
2346 	printf("S4BIOS_REQ=0x%x\n", fadt->S4BiosRequest);
2347 	printf("\tPSTATE_CNT=0x%x\n", fadt->PstateControl);
2348 	printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
2349 	       fadt->Pm1aEventBlock,
2350 	       fadt->Pm1aEventBlock + fadt->Pm1EventLength - 1);
2351 	if (fadt->Pm1bEventBlock != 0)
2352 		printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
2353 		       fadt->Pm1bEventBlock,
2354 		       fadt->Pm1bEventBlock + fadt->Pm1EventLength - 1);
2355 	printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
2356 	       fadt->Pm1aControlBlock,
2357 	       fadt->Pm1aControlBlock + fadt->Pm1ControlLength - 1);
2358 	if (fadt->Pm1bControlBlock != 0)
2359 		printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
2360 		       fadt->Pm1bControlBlock,
2361 		       fadt->Pm1bControlBlock + fadt->Pm1ControlLength - 1);
2362 	if (fadt->Pm2ControlBlock != 0)
2363 		printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
2364 		       fadt->Pm2ControlBlock,
2365 		       fadt->Pm2ControlBlock + fadt->Pm2ControlLength - 1);
2366 	printf("\tPM_TMR_BLK=0x%x-0x%x\n",
2367 	       fadt->PmTimerBlock,
2368 	       fadt->PmTimerBlock + fadt->PmTimerLength - 1);
2369 	if (fadt->Gpe0Block != 0)
2370 		printf("\tGPE0_BLK=0x%x-0x%x\n",
2371 		       fadt->Gpe0Block,
2372 		       fadt->Gpe0Block + fadt->Gpe0BlockLength - 1);
2373 	if (fadt->Gpe1Block != 0)
2374 		printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
2375 		       fadt->Gpe1Block,
2376 		       fadt->Gpe1Block + fadt->Gpe1BlockLength - 1,
2377 		       fadt->Gpe1Base);
2378 	if (fadt->CstControl != 0)
2379 		printf("\tCST_CNT=0x%x\n", fadt->CstControl);
2380 	printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
2381 	       fadt->C2Latency, fadt->C3Latency);
2382 	printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
2383 	       fadt->FlushSize, fadt->FlushStride);
2384 	printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
2385 	       fadt->DutyOffset, fadt->DutyWidth);
2386 	printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
2387 	       fadt->DayAlarm, fadt->MonthAlarm, fadt->Century);
2388 
2389 #define PRINTFLAG(var, flag)	printflag((var), ACPI_FADT_## flag, #flag)
2390 
2391 	printf("\tIAPC_BOOT_ARCH=");
2392 	PRINTFLAG(fadt->BootFlags, LEGACY_DEVICES);
2393 	PRINTFLAG(fadt->BootFlags, 8042);
2394 	PRINTFLAG(fadt->BootFlags, NO_VGA);
2395 	PRINTFLAG(fadt->BootFlags, NO_MSI);
2396 	PRINTFLAG(fadt->BootFlags, NO_ASPM);
2397 	PRINTFLAG(fadt->BootFlags, NO_CMOS_RTC);
2398 	PRINTFLAG_END();
2399 
2400 	printf("\tFlags=");
2401 	PRINTFLAG(fadt->Flags, WBINVD);
2402 	PRINTFLAG(fadt->Flags, WBINVD_FLUSH);
2403 	PRINTFLAG(fadt->Flags, C1_SUPPORTED);
2404 	PRINTFLAG(fadt->Flags, C2_MP_SUPPORTED);
2405 	PRINTFLAG(fadt->Flags, POWER_BUTTON);
2406 	PRINTFLAG(fadt->Flags, SLEEP_BUTTON);
2407 	PRINTFLAG(fadt->Flags, FIXED_RTC);
2408 	PRINTFLAG(fadt->Flags, S4_RTC_WAKE);
2409 	PRINTFLAG(fadt->Flags, 32BIT_TIMER);
2410 	PRINTFLAG(fadt->Flags, DOCKING_SUPPORTED);
2411 	PRINTFLAG(fadt->Flags, RESET_REGISTER);
2412 	PRINTFLAG(fadt->Flags, SEALED_CASE);
2413 	PRINTFLAG(fadt->Flags, HEADLESS);
2414 	PRINTFLAG(fadt->Flags, SLEEP_TYPE);
2415 	PRINTFLAG(fadt->Flags, PCI_EXPRESS_WAKE);
2416 	PRINTFLAG(fadt->Flags, PLATFORM_CLOCK);
2417 	PRINTFLAG(fadt->Flags, S4_RTC_VALID);
2418 	PRINTFLAG(fadt->Flags, REMOTE_POWER_ON);
2419 	PRINTFLAG(fadt->Flags, APIC_CLUSTER);
2420 	PRINTFLAG(fadt->Flags, APIC_PHYSICAL);
2421 	PRINTFLAG(fadt->Flags, HW_REDUCED);
2422 	PRINTFLAG(fadt->Flags, LOW_POWER_S0);
2423 	PRINTFLAG_END();
2424 
2425 #undef PRINTFLAG
2426 
2427 	if (fadt->Flags & ACPI_FADT_RESET_REGISTER) {
2428 		printf("\tRESET_REG=");
2429 		acpi_print_gas(&fadt->ResetRegister);
2430 		printf(", RESET_VALUE=%#x\n", fadt->ResetValue);
2431 	}
2432 	if (acpi_get_fadt_revision(fadt) > 1) {
2433 		printf("\tX_FACS=0x%016jx, ", (uintmax_t)fadt->XFacs);
2434 		printf("X_DSDT=0x%016jx\n", (uintmax_t)fadt->XDsdt);
2435 		printf("\tX_PM1a_EVT_BLK=");
2436 		acpi_print_gas(&fadt->XPm1aEventBlock);
2437 		if (fadt->XPm1bEventBlock.Address != 0) {
2438 			printf("\n\tX_PM1b_EVT_BLK=");
2439 			acpi_print_gas(&fadt->XPm1bEventBlock);
2440 		}
2441 		printf("\n\tX_PM1a_CNT_BLK=");
2442 		acpi_print_gas(&fadt->XPm1aControlBlock);
2443 		if (fadt->XPm1bControlBlock.Address != 0) {
2444 			printf("\n\tX_PM1b_CNT_BLK=");
2445 			acpi_print_gas(&fadt->XPm1bControlBlock);
2446 		}
2447 		if (fadt->XPm2ControlBlock.Address != 0) {
2448 			printf("\n\tX_PM2_CNT_BLK=");
2449 			acpi_print_gas(&fadt->XPm2ControlBlock);
2450 		}
2451 		printf("\n\tX_PM_TMR_BLK=");
2452 		acpi_print_gas(&fadt->XPmTimerBlock);
2453 		if (fadt->XGpe0Block.Address != 0) {
2454 			printf("\n\tX_GPE0_BLK=");
2455 			acpi_print_gas(&fadt->XGpe0Block);
2456 		}
2457 		if (fadt->XGpe1Block.Address != 0) {
2458 			printf("\n\tX_GPE1_BLK=");
2459 			acpi_print_gas(&fadt->XGpe1Block);
2460 		}
2461 		printf("\n");
2462 	}
2463 
2464 	printf(END_COMMENT);
2465 }
2466 
2467 static void
2468 acpi_print_facs(ACPI_TABLE_FACS *facs)
2469 {
2470 	printf(BEGIN_COMMENT);
2471 	printf("  FACS:\tLength=%u, ", facs->Length);
2472 	printf("HwSig=0x%08x, ", facs->HardwareSignature);
2473 	printf("Firm_Wake_Vec=0x%08x\n", facs->FirmwareWakingVector);
2474 
2475 	printf("\tGlobal_Lock=");
2476 	if (facs->GlobalLock != 0) {
2477 		if (facs->GlobalLock & ACPI_GLOCK_PENDING)
2478 			printf("PENDING,");
2479 		if (facs->GlobalLock & ACPI_GLOCK_OWNED)
2480 			printf("OWNED");
2481 	}
2482 	printf("\n");
2483 
2484 	printf("\tFlags=");
2485 	if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
2486 		printf("S4BIOS");
2487 	printf("\n");
2488 
2489 	if (facs->XFirmwareWakingVector != 0)
2490 		printf("\tX_Firm_Wake_Vec=%016jx\n",
2491 		    (uintmax_t)facs->XFirmwareWakingVector);
2492 	printf("\tVersion=%u\n", facs->Version);
2493 
2494 	printf(END_COMMENT);
2495 }
2496 
2497 static void
2498 acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp)
2499 {
2500 	printf(BEGIN_COMMENT);
2501 	acpi_print_sdt(dsdp);
2502 	printf(END_COMMENT);
2503 }
2504 
2505 int
2506 acpi_checksum(void *p, size_t length)
2507 {
2508 	uint8_t *bp;
2509 	uint8_t sum;
2510 
2511 	bp = p;
2512 	sum = 0;
2513 	while (length--)
2514 		sum += *bp++;
2515 
2516 	return (sum);
2517 }
2518 
2519 static ACPI_TABLE_HEADER *
2520 acpi_map_sdt(vm_offset_t pa)
2521 {
2522 	ACPI_TABLE_HEADER *sp;
2523 
2524 	sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
2525 	sp = acpi_map_physical(pa, sp->Length);
2526 	return (sp);
2527 }
2528 
2529 static void
2530 acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp)
2531 {
2532 	printf(BEGIN_COMMENT);
2533 	printf("  RSD PTR: OEM=");
2534 	acpi_print_string(rp->OemId, ACPI_OEM_ID_SIZE);
2535 	printf(", ACPI_Rev=%s (%d)\n", rp->Revision < 2 ? "1.0x" : "2.0x",
2536 	       rp->Revision);
2537 	if (rp->Revision < 2) {
2538 		printf("\tRSDT=0x%08x, cksum=%u\n", rp->RsdtPhysicalAddress,
2539 		    rp->Checksum);
2540 	} else {
2541 		printf("\tXSDT=0x%016jx, length=%u, cksum=%u\n",
2542 		    (uintmax_t)rp->XsdtPhysicalAddress, rp->Length,
2543 		    rp->ExtendedChecksum);
2544 	}
2545 	printf(END_COMMENT);
2546 }
2547 
2548 static void
2549 acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp)
2550 {
2551 	ACPI_TABLE_HEADER *sdp;
2552 	ACPI_TABLE_RSDT *rsdt;
2553 	ACPI_TABLE_XSDT *xsdt;
2554 	vm_offset_t addr;
2555 	int entries, i;
2556 
2557 	acpi_print_rsdt(rsdp);
2558 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2559 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2560 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2561 	for (i = 0; i < entries; i++) {
2562 		if (addr_size == 4)
2563 			addr = le32toh(rsdt->TableOffsetEntry[i]);
2564 		else
2565 			addr = le64toh(xsdt->TableOffsetEntry[i]);
2566 		if (addr == 0)
2567 			continue;
2568 		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
2569 		if (acpi_checksum(sdp, sdp->Length)) {
2570 			warnx("RSDT entry %d (sig %.4s) is corrupt", i,
2571 			    sdp->Signature);
2572 			continue;
2573 		}
2574 		if (!memcmp(sdp->Signature, ACPI_SIG_BERT, 4))
2575 			acpi_handle_bert(sdp);
2576 		else if (!memcmp(sdp->Signature, ACPI_SIG_EINJ, 4))
2577 			acpi_handle_einj(sdp);
2578 		else if (!memcmp(sdp->Signature, ACPI_SIG_ERST, 4))
2579 			acpi_handle_erst(sdp);
2580 		else if (!memcmp(sdp->Signature, ACPI_SIG_FADT, 4))
2581 			acpi_handle_fadt(sdp);
2582 		else if (!memcmp(sdp->Signature, ACPI_SIG_MADT, 4))
2583 			acpi_handle_madt(sdp);
2584 		else if (!memcmp(sdp->Signature, ACPI_SIG_HEST, 4))
2585 			acpi_handle_hest(sdp);
2586 		else if (!memcmp(sdp->Signature, ACPI_SIG_HPET, 4))
2587 			acpi_handle_hpet(sdp);
2588 		else if (!memcmp(sdp->Signature, ACPI_SIG_ECDT, 4))
2589 			acpi_handle_ecdt(sdp);
2590 		else if (!memcmp(sdp->Signature, ACPI_SIG_MCFG, 4))
2591 			acpi_handle_mcfg(sdp);
2592 		else if (!memcmp(sdp->Signature, ACPI_SIG_SLIT, 4))
2593 			acpi_handle_slit(sdp);
2594 		else if (!memcmp(sdp->Signature, ACPI_SIG_SRAT, 4))
2595 			acpi_handle_srat(sdp);
2596 		else if (!memcmp(sdp->Signature, ACPI_SIG_TCPA, 4))
2597 			acpi_handle_tcpa(sdp);
2598 		else if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
2599 			acpi_handle_dmar(sdp);
2600 		else if (!memcmp(sdp->Signature, ACPI_SIG_IVRS, 4))
2601 			acpi_handle_ivrs(sdp);
2602 		else if (!memcmp(sdp->Signature, ACPI_SIG_NFIT, 4))
2603 			acpi_handle_nfit(sdp);
2604 		else if (!memcmp(sdp->Signature, ACPI_SIG_WDDT, 4))
2605 			acpi_handle_wddt(sdp);
2606 		else if (!memcmp(sdp->Signature, ACPI_SIG_LPIT, 4))
2607 			acpi_handle_lpit(sdp);
2608 		else if (!memcmp(sdp->Signature, ACPI_SIG_TPM2, 4))
2609 			acpi_handle_tpm2(sdp);
2610 		else if (!memcmp(sdp->Signature, ACPI_SIG_SPCR, 4))
2611 			acpi_handle_spcr(sdp);
2612 		else {
2613 			printf(BEGIN_COMMENT);
2614 			acpi_print_sdt(sdp);
2615 			printf(END_COMMENT);
2616 		}
2617 	}
2618 }
2619 
2620 ACPI_TABLE_HEADER *
2621 sdt_load_devmem(void)
2622 {
2623 	ACPI_TABLE_RSDP *rp;
2624 	ACPI_TABLE_HEADER *rsdp;
2625 
2626 	rp = acpi_find_rsd_ptr();
2627 	if (!rp)
2628 		errx(1, "Can't find ACPI information");
2629 
2630 	if (tflag)
2631 		acpi_print_rsd_ptr(rp);
2632 	if (rp->Revision < 2) {
2633 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->RsdtPhysicalAddress);
2634 		if (memcmp(rsdp->Signature, "RSDT", 4) != 0 ||
2635 		    acpi_checksum(rsdp, rsdp->Length) != 0)
2636 			errx(1, "RSDT is corrupted");
2637 		addr_size = sizeof(uint32_t);
2638 	} else {
2639 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->XsdtPhysicalAddress);
2640 		if (memcmp(rsdp->Signature, "XSDT", 4) != 0 ||
2641 		    acpi_checksum(rsdp, rsdp->Length) != 0)
2642 			errx(1, "XSDT is corrupted");
2643 		addr_size = sizeof(uint64_t);
2644 	}
2645 	return (rsdp);
2646 }
2647 
2648 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
2649 static int
2650 write_dsdt(int fd, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdt)
2651 {
2652 	ACPI_TABLE_HEADER sdt;
2653 	ACPI_TABLE_HEADER *ssdt;
2654 	uint8_t sum;
2655 
2656 	/* Create a new checksum to account for the DSDT and any SSDTs. */
2657 	sdt = *dsdt;
2658 	if (rsdt != NULL) {
2659 		sdt.Checksum = 0;
2660 		sum = acpi_checksum(dsdt + 1, dsdt->Length -
2661 		    sizeof(ACPI_TABLE_HEADER));
2662 		ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, NULL);
2663 		while (ssdt != NULL) {
2664 			sdt.Length += ssdt->Length - sizeof(ACPI_TABLE_HEADER);
2665 			sum += acpi_checksum(ssdt + 1,
2666 			    ssdt->Length - sizeof(ACPI_TABLE_HEADER));
2667 			ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, ssdt);
2668 		}
2669 		sum += acpi_checksum(&sdt, sizeof(ACPI_TABLE_HEADER));
2670 		sdt.Checksum -= sum;
2671 	}
2672 
2673 	/* Write out the DSDT header and body. */
2674 	write(fd, &sdt, sizeof(ACPI_TABLE_HEADER));
2675 	write(fd, dsdt + 1, dsdt->Length - sizeof(ACPI_TABLE_HEADER));
2676 
2677 	/* Write out any SSDTs (if present.) */
2678 	if (rsdt != NULL) {
2679 		ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
2680 		while (ssdt != NULL) {
2681 			write(fd, ssdt + 1, ssdt->Length -
2682 			    sizeof(ACPI_TABLE_HEADER));
2683 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
2684 		}
2685 	}
2686 	return (0);
2687 }
2688 
2689 void
2690 dsdt_save_file(char *outfile, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
2691 {
2692 	int	fd;
2693 	mode_t	mode;
2694 
2695 	assert(outfile != NULL);
2696 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2697 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
2698 	if (fd == -1) {
2699 		perror("dsdt_save_file");
2700 		return;
2701 	}
2702 	write_dsdt(fd, rsdt, dsdp);
2703 	close(fd);
2704 }
2705 
2706 void
2707 aml_disassemble(ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
2708 {
2709 	char buf[PATH_MAX], tmpstr[PATH_MAX], wrkdir[PATH_MAX];
2710 	const char *iname = "/acpdump.din";
2711 	const char *oname = "/acpdump.dsl";
2712 	const char *tmpdir;
2713 	FILE *fp;
2714 	size_t len;
2715 	int fd, status;
2716 	pid_t pid;
2717 
2718 	tmpdir = getenv("TMPDIR");
2719 	if (tmpdir == NULL)
2720 		tmpdir = _PATH_TMP;
2721 	if (realpath(tmpdir, buf) == NULL) {
2722 		perror("realpath tmp dir");
2723 		return;
2724 	}
2725 	len = sizeof(wrkdir) - strlen(iname);
2726 	if ((size_t)snprintf(wrkdir, len, "%s/acpidump.XXXXXX", buf) > len-1 ) {
2727 		fprintf(stderr, "$TMPDIR too long\n");
2728 		return;
2729 	}
2730 	if  (mkdtemp(wrkdir) == NULL) {
2731 		perror("mkdtemp tmp working dir");
2732 		return;
2733 	}
2734 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, iname);
2735 	assert(len <= sizeof(tmpstr) - 1);
2736 	fd = open(tmpstr, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
2737 	if (fd < 0) {
2738 		perror("iasl tmp file");
2739 		return;
2740 	}
2741 	write_dsdt(fd, rsdt, dsdp);
2742 	close(fd);
2743 
2744 	/* Run iasl -d on the temp file */
2745 	if ((pid = fork()) == 0) {
2746 		close(STDOUT_FILENO);
2747 		if (vflag == 0)
2748 			close(STDERR_FILENO);
2749 		execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
2750 		err(1, "exec");
2751 	}
2752 	if (pid > 0)
2753 		wait(&status);
2754 	if (unlink(tmpstr) < 0) {
2755 		perror("unlink");
2756 		goto out;
2757 	}
2758 	if (pid < 0) {
2759 		perror("fork");
2760 		goto out;
2761 	}
2762 	if (status != 0) {
2763 		fprintf(stderr, "iasl exit status = %d\n", status);
2764 	}
2765 
2766 	/* Dump iasl's output to stdout */
2767 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, oname);
2768 	assert(len <= sizeof(tmpstr) - 1);
2769 	fp = fopen(tmpstr, "r");
2770 	if (unlink(tmpstr) < 0) {
2771 		perror("unlink");
2772 		goto out;
2773 	}
2774 	if (fp == NULL) {
2775 		perror("iasl tmp file (read)");
2776 		goto out;
2777 	}
2778 	while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
2779 		fwrite(buf, 1, len, stdout);
2780 	fclose(fp);
2781 
2782     out:
2783 	if (rmdir(wrkdir) < 0)
2784 		perror("rmdir");
2785 }
2786 
2787 void
2788 aml_disassemble_separate(ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
2789 {
2790 	ACPI_TABLE_HEADER *ssdt = NULL;
2791 
2792 	aml_disassemble(NULL, dsdp);
2793 	if (rsdt != NULL) {
2794 		for (;;) {
2795 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
2796 			if (ssdt == NULL)
2797 				break;
2798 			aml_disassemble(NULL, ssdt);
2799 		}
2800 	}
2801 }
2802 
2803 void
2804 sdt_print_all(ACPI_TABLE_HEADER *rsdp)
2805 {
2806 	acpi_handle_rsdt(rsdp);
2807 }
2808 
2809 /* Fetch a table matching the given signature via the RSDT. */
2810 ACPI_TABLE_HEADER *
2811 sdt_from_rsdt(ACPI_TABLE_HEADER *rsdp, const char *sig, ACPI_TABLE_HEADER *last)
2812 {
2813 	ACPI_TABLE_HEADER *sdt;
2814 	ACPI_TABLE_RSDT *rsdt;
2815 	ACPI_TABLE_XSDT *xsdt;
2816 	vm_offset_t addr;
2817 	int entries, i;
2818 
2819 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2820 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2821 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2822 	for (i = 0; i < entries; i++) {
2823 		if (addr_size == 4)
2824 			addr = le32toh(rsdt->TableOffsetEntry[i]);
2825 		else
2826 			addr = le64toh(xsdt->TableOffsetEntry[i]);
2827 		if (addr == 0)
2828 			continue;
2829 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
2830 		if (last != NULL) {
2831 			if (sdt == last)
2832 				last = NULL;
2833 			continue;
2834 		}
2835 		if (memcmp(sdt->Signature, sig, strlen(sig)))
2836 			continue;
2837 		if (acpi_checksum(sdt, sdt->Length))
2838 			errx(1, "RSDT entry %d is corrupt", i);
2839 		return (sdt);
2840 	}
2841 
2842 	return (NULL);
2843 }
2844 
2845 ACPI_TABLE_HEADER *
2846 dsdt_from_fadt(ACPI_TABLE_FADT *fadt)
2847 {
2848 	ACPI_TABLE_HEADER	*sdt;
2849 
2850 	/* Use the DSDT address if it is version 1, otherwise use XDSDT. */
2851 	if (acpi_get_fadt_revision(fadt) == 1)
2852 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
2853 	else
2854 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
2855 	if (acpi_checksum(sdt, sdt->Length))
2856 		errx(1, "DSDT is corrupt\n");
2857 	return (sdt);
2858 }
2859