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