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