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