xref: /freebsd/usr.sbin/acpi/acpidump/acpi.c (revision 990651167bba1040b77402ae2ae44c1451192d56)
1e1e9a4bfSMitsuru IWASAKI /*-
2e1e9a4bfSMitsuru IWASAKI  * Copyright (c) 1998 Doug Rabson
3e1e9a4bfSMitsuru IWASAKI  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
4e1e9a4bfSMitsuru IWASAKI  * All rights reserved.
5e1e9a4bfSMitsuru IWASAKI  *
6e1e9a4bfSMitsuru IWASAKI  * Redistribution and use in source and binary forms, with or without
7e1e9a4bfSMitsuru IWASAKI  * modification, are permitted provided that the following conditions
8e1e9a4bfSMitsuru IWASAKI  * are met:
9e1e9a4bfSMitsuru IWASAKI  * 1. Redistributions of source code must retain the above copyright
10e1e9a4bfSMitsuru IWASAKI  *    notice, this list of conditions and the following disclaimer.
11e1e9a4bfSMitsuru IWASAKI  * 2. Redistributions in binary form must reproduce the above copyright
12e1e9a4bfSMitsuru IWASAKI  *    notice, this list of conditions and the following disclaimer in the
13e1e9a4bfSMitsuru IWASAKI  *    documentation and/or other materials provided with the distribution.
14e1e9a4bfSMitsuru IWASAKI  *
15e1e9a4bfSMitsuru IWASAKI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e1e9a4bfSMitsuru IWASAKI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e1e9a4bfSMitsuru IWASAKI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e1e9a4bfSMitsuru IWASAKI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e1e9a4bfSMitsuru IWASAKI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e1e9a4bfSMitsuru IWASAKI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e1e9a4bfSMitsuru IWASAKI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e1e9a4bfSMitsuru IWASAKI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e1e9a4bfSMitsuru IWASAKI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e1e9a4bfSMitsuru IWASAKI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e1e9a4bfSMitsuru IWASAKI  * SUCH DAMAGE.
26e1e9a4bfSMitsuru IWASAKI  *
27e1e9a4bfSMitsuru IWASAKI  *	$FreeBSD$
28e1e9a4bfSMitsuru IWASAKI  */
29e1e9a4bfSMitsuru IWASAKI 
30e1e9a4bfSMitsuru IWASAKI #include <sys/param.h>
31a74172abSNate Lawson #include <sys/endian.h>
32e1e9a4bfSMitsuru IWASAKI #include <sys/stat.h>
33945137d9SNate Lawson #include <sys/wait.h>
34e1e9a4bfSMitsuru IWASAKI #include <assert.h>
35e1e9a4bfSMitsuru IWASAKI #include <err.h>
36e1e9a4bfSMitsuru IWASAKI #include <fcntl.h>
3799065116SJung-uk Kim #include <paths.h>
38e1e9a4bfSMitsuru IWASAKI #include <stdio.h>
3999065116SJung-uk Kim #include <stdlib.h>
40945137d9SNate Lawson #include <string.h>
41e1e9a4bfSMitsuru IWASAKI #include <unistd.h>
42e1e9a4bfSMitsuru IWASAKI 
43e1e9a4bfSMitsuru IWASAKI #include "acpidump.h"
44e1e9a4bfSMitsuru IWASAKI 
45c62f1cccSMitsuru IWASAKI #define BEGIN_COMMENT	"/*\n"
46c62f1cccSMitsuru IWASAKI #define END_COMMENT	" */\n"
47c62f1cccSMitsuru IWASAKI 
48945137d9SNate Lawson static void	acpi_print_string(char *s, size_t length);
498e6a8737SNate Lawson static void	acpi_print_gas(struct ACPIgas *gas);
50c2962974SNate Lawson static int	acpi_get_fadt_revision(struct FADTbody *fadt);
512177d4e6SNate Lawson static void	acpi_handle_fadt(struct ACPIsdt *fadt);
52945137d9SNate Lawson static void	acpi_print_cpu(u_char cpu_id);
53945137d9SNate Lawson static void	acpi_print_local_apic(u_char cpu_id, u_char apic_id,
54945137d9SNate Lawson 				      u_int32_t flags);
55945137d9SNate Lawson static void	acpi_print_io_apic(u_char apic_id, u_int32_t int_base,
56945137d9SNate Lawson 				   u_int64_t apic_addr);
57945137d9SNate Lawson static void	acpi_print_mps_flags(u_int16_t flags);
58945137d9SNate Lawson static void	acpi_print_intr(u_int32_t intr, u_int16_t mps_flags);
59945137d9SNate Lawson static void	acpi_print_apic(struct MADT_APIC *mp);
60945137d9SNate Lawson static void	acpi_handle_apic(struct ACPIsdt *sdp);
61945137d9SNate Lawson static void	acpi_handle_hpet(struct ACPIsdt *sdp);
62773b6454SNate Lawson static void	acpi_print_sdt(struct ACPIsdt *sdp);
632177d4e6SNate Lawson static void	acpi_print_fadt(struct ACPIsdt *sdp);
648e6a8737SNate Lawson static void	acpi_print_facs(struct FACSbody *facs);
65945137d9SNate Lawson static void	acpi_print_dsdt(struct ACPIsdt *dsdp);
66a74172abSNate Lawson static struct ACPIsdt *acpi_map_sdt(vm_offset_t pa);
67945137d9SNate Lawson static void	acpi_print_rsd_ptr(struct ACPIrsdp *rp);
68945137d9SNate Lawson static void	acpi_handle_rsdt(struct ACPIsdt *rsdp);
69c62f1cccSMitsuru IWASAKI 
70773b6454SNate Lawson /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
71a74172abSNate Lawson static int addr_size;
72a74172abSNate Lawson 
73e1e9a4bfSMitsuru IWASAKI static void
74e1e9a4bfSMitsuru IWASAKI acpi_print_string(char *s, size_t length)
75e1e9a4bfSMitsuru IWASAKI {
76e1e9a4bfSMitsuru IWASAKI 	int	c;
77e1e9a4bfSMitsuru IWASAKI 
78e1e9a4bfSMitsuru IWASAKI 	/* Trim trailing spaces and NULLs */
79e1e9a4bfSMitsuru IWASAKI 	while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
80e1e9a4bfSMitsuru IWASAKI 		length--;
81e1e9a4bfSMitsuru IWASAKI 
82e1e9a4bfSMitsuru IWASAKI 	while (length--) {
83e1e9a4bfSMitsuru IWASAKI 		c = *s++;
84e1e9a4bfSMitsuru IWASAKI 		putchar(c);
85e1e9a4bfSMitsuru IWASAKI 	}
86e1e9a4bfSMitsuru IWASAKI }
87e1e9a4bfSMitsuru IWASAKI 
88e1e9a4bfSMitsuru IWASAKI static void
898e6a8737SNate Lawson acpi_print_gas(struct ACPIgas *gas)
908e6a8737SNate Lawson {
918e6a8737SNate Lawson 	switch(gas->address_space_id) {
928e6a8737SNate Lawson 	case ACPI_GAS_MEMORY:
93773b6454SNate Lawson 		printf("0x%08lx:%u[%u] (Memory)", (u_long)gas->address,
948e6a8737SNate Lawson 		       gas->bit_offset, gas->bit_width);
958e6a8737SNate Lawson 		break;
968e6a8737SNate Lawson 	case ACPI_GAS_IO:
97e47f1780SNate Lawson 		printf("0x%02lx:%u[%u] (IO)", (u_long)gas->address,
988e6a8737SNate Lawson 		       gas->bit_offset, gas->bit_width);
998e6a8737SNate Lawson 		break;
1008e6a8737SNate Lawson 	case ACPI_GAS_PCI:
101e47f1780SNate Lawson 		printf("%x:%x+0x%x (PCI)", (uint16_t)(gas->address >> 32),
1028e6a8737SNate Lawson 		       (uint16_t)((gas->address >> 16) & 0xffff),
1038e6a8737SNate Lawson 		       (uint16_t)gas->address);
1048e6a8737SNate Lawson 		break;
1058e6a8737SNate Lawson 	/* XXX How to handle these below? */
1068e6a8737SNate Lawson 	case ACPI_GAS_EMBEDDED:
107e47f1780SNate Lawson 		printf("0x%x:%u[%u] (EC)", (uint16_t)gas->address,
1088e6a8737SNate Lawson 		       gas->bit_offset, gas->bit_width);
1098e6a8737SNate Lawson 		break;
1108e6a8737SNate Lawson 	case ACPI_GAS_SMBUS:
111e47f1780SNate Lawson 		printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->address,
1128e6a8737SNate Lawson 		       gas->bit_offset, gas->bit_width);
1138e6a8737SNate Lawson 		break;
1148e6a8737SNate Lawson 	case ACPI_GAS_FIXED:
1158e6a8737SNate Lawson 	default:
116773b6454SNate Lawson 		printf("0x%08lx (?)", (u_long)gas->address);
1178e6a8737SNate Lawson 		break;
1188e6a8737SNate Lawson 	}
1198e6a8737SNate Lawson }
1208e6a8737SNate Lawson 
121c2962974SNate Lawson /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
122c2962974SNate Lawson static int
123c2962974SNate Lawson acpi_get_fadt_revision(struct FADTbody *fadt)
124e1e9a4bfSMitsuru IWASAKI {
125c2962974SNate Lawson 	int fadt_revision;
126e1e9a4bfSMitsuru IWASAKI 
127c83f0f99SNate Lawson 	/* Set the FADT revision separately from the RSDP version. */
128c83f0f99SNate Lawson 	if (addr_size == 8) {
129c83f0f99SNate Lawson 		fadt_revision = 2;
1308e6a8737SNate Lawson 
131773b6454SNate Lawson 		/*
132c83f0f99SNate Lawson 		 * A few systems (e.g., IBM T23) have an RSDP that claims
133c83f0f99SNate Lawson 		 * revision 2 but the 64 bit addresses are invalid.  If
134c83f0f99SNate Lawson 		 * revision 2 and the 32 bit address is non-zero but the
135c83f0f99SNate Lawson 		 * 32 and 64 bit versions don't match, prefer the 32 bit
136c83f0f99SNate Lawson 		 * version for all subsequent tables.
137773b6454SNate Lawson 		 */
138c83f0f99SNate Lawson 		if (fadt->facs_ptr != 0 &&
139c83f0f99SNate Lawson 		    (fadt->x_facs_ptr & 0xffffffff) != fadt->facs_ptr)
140c83f0f99SNate Lawson 			fadt_revision = 1;
141c2962974SNate Lawson 	} else
142c83f0f99SNate Lawson 		fadt_revision = 1;
143c2962974SNate Lawson 	return (fadt_revision);
144c83f0f99SNate Lawson }
145c2962974SNate Lawson 
146c2962974SNate Lawson static void
1472177d4e6SNate Lawson acpi_handle_fadt(struct ACPIsdt *sdp)
148c2962974SNate Lawson {
149c2962974SNate Lawson 	struct ACPIsdt	*dsdp;
150c2962974SNate Lawson 	struct FACSbody	*facs;
1512177d4e6SNate Lawson 	struct FADTbody *fadt;
152c2962974SNate Lawson 	int		fadt_revision;
153c2962974SNate Lawson 
1542177d4e6SNate Lawson 	fadt = (struct FADTbody *)sdp->body;
1552177d4e6SNate Lawson 	acpi_print_fadt(sdp);
156c83f0f99SNate Lawson 
157c2962974SNate Lawson 	fadt_revision = acpi_get_fadt_revision(fadt);
158c83f0f99SNate Lawson 	if (fadt_revision == 1)
1598e6a8737SNate Lawson 		facs = (struct FACSbody *)acpi_map_sdt(fadt->facs_ptr);
160773b6454SNate Lawson 	else
161773b6454SNate Lawson 		facs = (struct FACSbody *)acpi_map_sdt(fadt->x_facs_ptr);
1628e6a8737SNate Lawson 	if (memcmp(facs->signature, "FACS", 4) != 0 || facs->len < 64)
1638e6a8737SNate Lawson 		errx(1, "FACS is corrupt");
1648e6a8737SNate Lawson 	acpi_print_facs(facs);
1658e6a8737SNate Lawson 
166c83f0f99SNate Lawson 	if (fadt_revision == 1)
1678e6a8737SNate Lawson 		dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
168773b6454SNate Lawson 	else
169773b6454SNate Lawson 		dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
170e1e9a4bfSMitsuru IWASAKI 	if (acpi_checksum(dsdp, dsdp->len))
171945137d9SNate Lawson 		errx(1, "DSDT is corrupt");
172945137d9SNate Lawson 	acpi_print_dsdt(dsdp);
173c62f1cccSMitsuru IWASAKI }
174c62f1cccSMitsuru IWASAKI 
175c62f1cccSMitsuru IWASAKI static void
1760a473124SJohn Baldwin acpi_print_cpu(u_char cpu_id)
1770a473124SJohn Baldwin {
1780a473124SJohn Baldwin 
1790a473124SJohn Baldwin 	printf("\tACPI CPU=");
1800a473124SJohn Baldwin 	if (cpu_id == 0xff)
1810a473124SJohn Baldwin 		printf("ALL\n");
1820a473124SJohn Baldwin 	else
1830a473124SJohn Baldwin 		printf("%d\n", (u_int)cpu_id);
1840a473124SJohn Baldwin }
1850a473124SJohn Baldwin 
1860a473124SJohn Baldwin static void
1870a473124SJohn Baldwin acpi_print_local_apic(u_char cpu_id, u_char apic_id, u_int32_t flags)
1880a473124SJohn Baldwin {
1890a473124SJohn Baldwin 	acpi_print_cpu(cpu_id);
1900a473124SJohn Baldwin 	printf("\tFlags={");
1910a473124SJohn Baldwin 	if (flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED)
1920a473124SJohn Baldwin 		printf("ENABLED");
1930a473124SJohn Baldwin 	else
1940a473124SJohn Baldwin 		printf("DISABLED");
1950a473124SJohn Baldwin 	printf("}\n");
1960a473124SJohn Baldwin 	printf("\tAPIC ID=%d\n", (u_int)apic_id);
1970a473124SJohn Baldwin }
1980a473124SJohn Baldwin 
1990a473124SJohn Baldwin static void
2000a473124SJohn Baldwin acpi_print_io_apic(u_char apic_id, u_int32_t int_base, u_int64_t apic_addr)
2010a473124SJohn Baldwin {
2020a473124SJohn Baldwin 	printf("\tAPIC ID=%d\n", (u_int)apic_id);
2030a473124SJohn Baldwin 	printf("\tINT BASE=%d\n", int_base);
204945137d9SNate Lawson 	printf("\tADDR=0x%016jx\n", apic_addr);
2050a473124SJohn Baldwin }
2060a473124SJohn Baldwin 
2070a473124SJohn Baldwin static void
2080a473124SJohn Baldwin acpi_print_mps_flags(u_int16_t flags)
2090a473124SJohn Baldwin {
2100a473124SJohn Baldwin 
2110a473124SJohn Baldwin 	printf("\tFlags={Polarity=");
2120a473124SJohn Baldwin 	switch (flags & MPS_INT_FLAG_POLARITY_MASK) {
2130a473124SJohn Baldwin 	case MPS_INT_FLAG_POLARITY_CONFORM:
2140a473124SJohn Baldwin 		printf("conforming");
2150a473124SJohn Baldwin 		break;
2160a473124SJohn Baldwin 	case MPS_INT_FLAG_POLARITY_HIGH:
2170a473124SJohn Baldwin 		printf("active-hi");
2180a473124SJohn Baldwin 		break;
2190a473124SJohn Baldwin 	case MPS_INT_FLAG_POLARITY_LOW:
2200a473124SJohn Baldwin 		printf("active-lo");
2210a473124SJohn Baldwin 		break;
2220a473124SJohn Baldwin 	default:
2230a473124SJohn Baldwin 		printf("0x%x", flags & MPS_INT_FLAG_POLARITY_MASK);
2240a473124SJohn Baldwin 		break;
2250a473124SJohn Baldwin 	}
2260a473124SJohn Baldwin 	printf(", Trigger=");
2270a473124SJohn Baldwin 	switch (flags & MPS_INT_FLAG_TRIGGER_MASK) {
2280a473124SJohn Baldwin 	case MPS_INT_FLAG_TRIGGER_CONFORM:
2290a473124SJohn Baldwin 		printf("conforming");
2300a473124SJohn Baldwin 		break;
2310a473124SJohn Baldwin 	case MPS_INT_FLAG_TRIGGER_EDGE:
2320a473124SJohn Baldwin 		printf("edge");
2330a473124SJohn Baldwin 		break;
2340a473124SJohn Baldwin 	case MPS_INT_FLAG_TRIGGER_LEVEL:
2350a473124SJohn Baldwin 		printf("level");
2360a473124SJohn Baldwin 		break;
2370a473124SJohn Baldwin 	default:
2380a473124SJohn Baldwin 		printf("0x%x", (flags & MPS_INT_FLAG_TRIGGER_MASK) >> 2);
2390a473124SJohn Baldwin 	}
2400a473124SJohn Baldwin 	printf("}\n");
2410a473124SJohn Baldwin }
2420a473124SJohn Baldwin 
2430a473124SJohn Baldwin static void
2440a473124SJohn Baldwin acpi_print_intr(u_int32_t intr, u_int16_t mps_flags)
2450a473124SJohn Baldwin {
2460a473124SJohn Baldwin 
2470a473124SJohn Baldwin 	printf("\tINTR=%d\n", (u_int)intr);
2480a473124SJohn Baldwin 	acpi_print_mps_flags(mps_flags);
2490a473124SJohn Baldwin }
2500a473124SJohn Baldwin 
2510a473124SJohn Baldwin const char *apic_types[] = { "Local APIC", "IO APIC", "INT Override", "NMI",
2520a473124SJohn Baldwin 			     "Local NMI", "Local APIC Override", "IO SAPIC",
2530a473124SJohn Baldwin 			     "Local SAPIC", "Platform Interrupt" };
2540a473124SJohn Baldwin const char *platform_int_types[] = { "PMI", "INIT",
2550a473124SJohn Baldwin 				     "Corrected Platform Error" };
2560a473124SJohn Baldwin 
2570a473124SJohn Baldwin static void
2580a473124SJohn Baldwin acpi_print_apic(struct MADT_APIC *mp)
2590a473124SJohn Baldwin {
2600a473124SJohn Baldwin 
2610a473124SJohn Baldwin 	printf("\tType=%s\n", apic_types[mp->type]);
2620a473124SJohn Baldwin 	switch (mp->type) {
2630a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_APIC:
2640a473124SJohn Baldwin 		acpi_print_local_apic(mp->body.local_apic.cpu_id,
2650a473124SJohn Baldwin 		    mp->body.local_apic.apic_id, mp->body.local_apic.flags);
2660a473124SJohn Baldwin 		break;
2670a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_IO_APIC:
2680a473124SJohn Baldwin 		acpi_print_io_apic(mp->body.io_apic.apic_id,
2690a473124SJohn Baldwin 		    mp->body.io_apic.int_base,
2700a473124SJohn Baldwin 		    mp->body.io_apic.apic_addr);
2710a473124SJohn Baldwin 		break;
2720a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_INT_OVERRIDE:
2730a473124SJohn Baldwin 		printf("\tBUS=%d\n", (u_int)mp->body.int_override.bus);
2740a473124SJohn Baldwin 		printf("\tIRQ=%d\n", (u_int)mp->body.int_override.source);
2750a473124SJohn Baldwin 		acpi_print_intr(mp->body.int_override.intr,
2760a473124SJohn Baldwin 		    mp->body.int_override.mps_flags);
2770a473124SJohn Baldwin 		break;
2780a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_NMI:
2790a473124SJohn Baldwin 		acpi_print_intr(mp->body.nmi.intr, mp->body.nmi.mps_flags);
2800a473124SJohn Baldwin 		break;
2810a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_NMI:
2820a473124SJohn Baldwin 		acpi_print_cpu(mp->body.local_nmi.cpu_id);
2830a473124SJohn Baldwin 		printf("\tLINT Pin=%d\n", mp->body.local_nmi.lintpin);
2840a473124SJohn Baldwin 		acpi_print_mps_flags(mp->body.local_nmi.mps_flags);
2850a473124SJohn Baldwin 		break;
2860a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_OVERRIDE:
287945137d9SNate Lawson 		printf("\tLocal APIC ADDR=0x%016jx\n",
288945137d9SNate Lawson 		    mp->body.local_apic_override.apic_addr);
2890a473124SJohn Baldwin 		break;
2900a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_IO_SAPIC:
2910a473124SJohn Baldwin 		acpi_print_io_apic(mp->body.io_sapic.apic_id,
2920a473124SJohn Baldwin 		    mp->body.io_sapic.int_base,
2930a473124SJohn Baldwin 		    mp->body.io_sapic.apic_addr);
2940a473124SJohn Baldwin 		break;
2950a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_SAPIC:
2960a473124SJohn Baldwin 		acpi_print_local_apic(mp->body.local_sapic.cpu_id,
2970a473124SJohn Baldwin 		    mp->body.local_sapic.apic_id, mp->body.local_sapic.flags);
2980a473124SJohn Baldwin 		printf("\tAPIC EID=%d\n", (u_int)mp->body.local_sapic.apic_eid);
2990a473124SJohn Baldwin 		break;
3000a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_INT_SRC:
3010a473124SJohn Baldwin 		printf("\tType=%s\n",
3020a473124SJohn Baldwin 		    platform_int_types[mp->body.int_src.type]);
3030a473124SJohn Baldwin 		printf("\tCPU ID=%d\n", (u_int)mp->body.int_src.cpu_id);
3040a473124SJohn Baldwin 		printf("\tCPU EID=%d\n", (u_int)mp->body.int_src.cpu_id);
3050a473124SJohn Baldwin 		printf("\tSAPIC Vector=%d\n",
3060a473124SJohn Baldwin 		    (u_int)mp->body.int_src.sapic_vector);
3070a473124SJohn Baldwin 		acpi_print_intr(mp->body.int_src.intr,
3080a473124SJohn Baldwin 		    mp->body.int_src.mps_flags);
3090a473124SJohn Baldwin 		break;
3100a473124SJohn Baldwin 	default:
3110a473124SJohn Baldwin 		printf("\tUnknown type %d\n", (u_int)mp->type);
312945137d9SNate Lawson 		break;
3130a473124SJohn Baldwin 	}
3140a473124SJohn Baldwin }
3150a473124SJohn Baldwin 
3160a473124SJohn Baldwin static void
3170a473124SJohn Baldwin acpi_handle_apic(struct ACPIsdt *sdp)
3180a473124SJohn Baldwin {
3190a473124SJohn Baldwin 	struct MADTbody *madtp;
3200a473124SJohn Baldwin 	struct MADT_APIC *madt_apicp;
3210a473124SJohn Baldwin 
322773b6454SNate Lawson 	printf(BEGIN_COMMENT);
323773b6454SNate Lawson 	acpi_print_sdt(sdp);
3240a473124SJohn Baldwin 	madtp = (struct MADTbody *) sdp->body;
3250a473124SJohn Baldwin 	printf("\tLocal APIC ADDR=0x%08x\n", madtp->lapic_addr);
3260a473124SJohn Baldwin 	printf("\tFlags={");
3270a473124SJohn Baldwin 	if (madtp->flags & ACPI_APIC_FLAG_PCAT_COMPAT)
3280a473124SJohn Baldwin 		printf("PC-AT");
3290a473124SJohn Baldwin 	printf("}\n");
3300a473124SJohn Baldwin 	madt_apicp = (struct MADT_APIC *)madtp->body;
3310a473124SJohn Baldwin 	while (((uintptr_t)madt_apicp) - ((uintptr_t)sdp) < sdp->len) {
3320a473124SJohn Baldwin 		printf("\n");
3330a473124SJohn Baldwin 		acpi_print_apic(madt_apicp);
3340a473124SJohn Baldwin 		madt_apicp = (struct MADT_APIC *) ((char *)madt_apicp +
3350a473124SJohn Baldwin 		    madt_apicp->len);
3360a473124SJohn Baldwin 	}
3370a473124SJohn Baldwin 	printf(END_COMMENT);
3380a473124SJohn Baldwin }
3390a473124SJohn Baldwin 
3400a473124SJohn Baldwin static void
34179d7565cSPeter Wemm acpi_handle_hpet(struct ACPIsdt *sdp)
34279d7565cSPeter Wemm {
34379d7565cSPeter Wemm 	struct HPETbody *hpetp;
34479d7565cSPeter Wemm 
345773b6454SNate Lawson 	printf(BEGIN_COMMENT);
346773b6454SNate Lawson 	acpi_print_sdt(sdp);
34779d7565cSPeter Wemm 	hpetp = (struct HPETbody *) sdp->body;
34879d7565cSPeter Wemm 	printf("\tHPET Number=%d\n", hpetp->hpet_number);
34987f9f09aSTakanori Watanabe 	printf("\tADDR=");
35087f9f09aSTakanori Watanabe 	acpi_print_gas(&hpetp->genaddr);
35179d7565cSPeter Wemm 	printf("\tHW Rev=0x%x\n", hpetp->block_hwrev);
35279d7565cSPeter Wemm 	printf("\tComparitors=%d\n", hpetp->block_comparitors);
35379d7565cSPeter Wemm 	printf("\tCounter Size=%d\n", hpetp->block_counter_size);
35479d7565cSPeter Wemm 	printf("\tLegacy IRQ routing capable={");
35579d7565cSPeter Wemm 	if (hpetp->block_legacy_capable)
35679d7565cSPeter Wemm 		printf("TRUE}\n");
35779d7565cSPeter Wemm 	else
35879d7565cSPeter Wemm 		printf("FALSE}\n");
35979d7565cSPeter Wemm 	printf("\tPCI Vendor ID=0x%04x\n", hpetp->block_pcivendor);
36055da3c73SPeter Wemm 	printf("\tMinimal Tick=%d\n", hpetp->clock_tick);
36179d7565cSPeter Wemm 	printf(END_COMMENT);
36279d7565cSPeter Wemm }
36379d7565cSPeter Wemm 
36479d7565cSPeter Wemm static void
36555d7ff9eSNate Lawson acpi_handle_ecdt(struct ACPIsdt *sdp)
36655d7ff9eSNate Lawson {
36755d7ff9eSNate Lawson 	struct ECDTbody *ecdt;
36855d7ff9eSNate Lawson 
36955d7ff9eSNate Lawson 	printf(BEGIN_COMMENT);
37055d7ff9eSNate Lawson 	acpi_print_sdt(sdp);
37155d7ff9eSNate Lawson 	ecdt = (struct ECDTbody *) sdp->body;
37255d7ff9eSNate Lawson 	printf("\tEC_CONTROL=");
37355d7ff9eSNate Lawson 	acpi_print_gas(&ecdt->ec_control);
37455d7ff9eSNate Lawson 	printf("\n\tEC_DATA=");
37555d7ff9eSNate Lawson 	acpi_print_gas(&ecdt->ec_data);
37655d7ff9eSNate Lawson 	printf("\n\tUID=%#x, ", ecdt->uid);
37755d7ff9eSNate Lawson 	printf("GPE_BIT=%#x\n", ecdt->gpe_bit);
37855d7ff9eSNate Lawson 	printf("\tEC_ID=%s\n", ecdt->ec_id);
37955d7ff9eSNate Lawson 	printf(END_COMMENT);
38055d7ff9eSNate Lawson }
38155d7ff9eSNate Lawson 
38255d7ff9eSNate Lawson static void
383a47e681bSScott Long acpi_handle_mcfg(struct ACPIsdt *sdp)
384a47e681bSScott Long {
385a47e681bSScott Long 	struct MCFGbody *mcfg;
386a47e681bSScott Long 	u_int i, e;
387a47e681bSScott Long 
388a47e681bSScott Long 	printf(BEGIN_COMMENT);
389a47e681bSScott Long 	acpi_print_sdt(sdp);
390a47e681bSScott Long 	mcfg = (struct MCFGbody *) sdp->body;
391a47e681bSScott Long 
392a47e681bSScott Long 	e = (sdp->len - ((caddr_t)&mcfg->s[0] - (caddr_t)sdp)) /
393a47e681bSScott Long 	    sizeof(*mcfg->s);
394a47e681bSScott Long 	for (i = 0; i < e; i++, mcfg++) {
395a47e681bSScott Long 		printf("\n");
396a47e681bSScott Long 		printf("\tBase Address= 0x%016jx\n", mcfg->s[i].baseaddr);
397a47e681bSScott Long 		printf("\tSegment Group= 0x%04x\n", mcfg->s[i].seg_grp);
398a47e681bSScott Long 		printf("\tStart Bus= %d\n", mcfg->s[i].start);
399a47e681bSScott Long 		printf("\tEnd Bus= %d\n", mcfg->s[i].end);
400a47e681bSScott Long 	}
401a47e681bSScott Long 	printf(END_COMMENT);
402a47e681bSScott Long }
403a47e681bSScott Long 
404a47e681bSScott Long static void
405773b6454SNate Lawson acpi_print_sdt(struct ACPIsdt *sdp)
406c62f1cccSMitsuru IWASAKI {
407773b6454SNate Lawson 	printf("  ");
408e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->signature, 4);
409c62f1cccSMitsuru IWASAKI 	printf(": Length=%d, Revision=%d, Checksum=%d,\n",
410e1e9a4bfSMitsuru IWASAKI 	       sdp->len, sdp->rev, sdp->check);
411e1e9a4bfSMitsuru IWASAKI 	printf("\tOEMID=");
412e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->oemid, 6);
413e1e9a4bfSMitsuru IWASAKI 	printf(", OEM Table ID=");
414e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->oemtblid, 8);
415e1e9a4bfSMitsuru IWASAKI 	printf(", OEM Revision=0x%x,\n", sdp->oemrev);
416e1e9a4bfSMitsuru IWASAKI 	printf("\tCreator ID=");
417e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->creator, 4);
418e1e9a4bfSMitsuru IWASAKI 	printf(", Creator Revision=0x%x\n", sdp->crerev);
419e1e9a4bfSMitsuru IWASAKI }
420e1e9a4bfSMitsuru IWASAKI 
421945137d9SNate Lawson static void
422e1e9a4bfSMitsuru IWASAKI acpi_print_rsdt(struct ACPIsdt *rsdp)
423e1e9a4bfSMitsuru IWASAKI {
424e1e9a4bfSMitsuru IWASAKI 	int	i, entries;
425a74172abSNate Lawson 	u_long	addr;
426e1e9a4bfSMitsuru IWASAKI 
427773b6454SNate Lawson 	printf(BEGIN_COMMENT);
428773b6454SNate Lawson 	acpi_print_sdt(rsdp);
429a74172abSNate Lawson 	entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
430e1e9a4bfSMitsuru IWASAKI 	printf("\tEntries={ ");
431e1e9a4bfSMitsuru IWASAKI 	for (i = 0; i < entries; i++) {
432e1e9a4bfSMitsuru IWASAKI 		if (i > 0)
433e1e9a4bfSMitsuru IWASAKI 			printf(", ");
434a74172abSNate Lawson 		switch (addr_size) {
435a74172abSNate Lawson 		case 4:
436a74172abSNate Lawson 			addr = le32dec((char*)rsdp->body + i * addr_size);
437a74172abSNate Lawson 			break;
438a74172abSNate Lawson 		case 8:
439a74172abSNate Lawson 			addr = le64dec((char*)rsdp->body + i * addr_size);
440a74172abSNate Lawson 			break;
441a74172abSNate Lawson 		default:
442a74172abSNate Lawson 			addr = 0;
443a74172abSNate Lawson 		}
444a74172abSNate Lawson 		assert(addr != 0);
445a74172abSNate Lawson 		printf("0x%08lx", addr);
446e1e9a4bfSMitsuru IWASAKI 	}
447e1e9a4bfSMitsuru IWASAKI 	printf(" }\n");
448c62f1cccSMitsuru IWASAKI 	printf(END_COMMENT);
449e1e9a4bfSMitsuru IWASAKI }
450e1e9a4bfSMitsuru IWASAKI 
4518e6a8737SNate Lawson static const char *acpi_pm_profiles[] = {
4528e6a8737SNate Lawson 	"Unspecified", "Desktop", "Mobile", "Workstation",
4538e6a8737SNate Lawson 	"Enterprise Server", "SOHO Server", "Appliance PC"
4548e6a8737SNate Lawson };
4558e6a8737SNate Lawson 
456945137d9SNate Lawson static void
4572177d4e6SNate Lawson acpi_print_fadt(struct ACPIsdt *sdp)
458e1e9a4bfSMitsuru IWASAKI {
4592177d4e6SNate Lawson 	struct FADTbody *fadt;
4608e6a8737SNate Lawson 	const char *pm;
461e1e9a4bfSMitsuru IWASAKI 	char	    sep;
462e1e9a4bfSMitsuru IWASAKI 
4632177d4e6SNate Lawson 	fadt = (struct FADTbody *)sdp->body;
464c62f1cccSMitsuru IWASAKI 	printf(BEGIN_COMMENT);
4652177d4e6SNate Lawson 	acpi_print_sdt(sdp);
4662177d4e6SNate Lawson 	printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->facs_ptr,
4678e6a8737SNate Lawson 	       fadt->dsdt_ptr);
4688e6a8737SNate Lawson 	printf("\tINT_MODEL=%s\n", fadt->int_model ? "APIC" : "PIC");
4698e6a8737SNate Lawson 	if (fadt->pm_profile >= sizeof(acpi_pm_profiles) / sizeof(char *))
4708e6a8737SNate Lawson 		pm = "Reserved";
4718e6a8737SNate Lawson 	else
4728e6a8737SNate Lawson 		pm = acpi_pm_profiles[fadt->pm_profile];
4738e6a8737SNate Lawson 	printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->pm_profile);
4748e6a8737SNate Lawson 	printf("\tSCI_INT=%d\n", fadt->sci_int);
4758e6a8737SNate Lawson 	printf("\tSMI_CMD=0x%x, ", fadt->smi_cmd);
4768e6a8737SNate Lawson 	printf("ACPI_ENABLE=0x%x, ", fadt->acpi_enable);
4778e6a8737SNate Lawson 	printf("ACPI_DISABLE=0x%x, ", fadt->acpi_disable);
4788e6a8737SNate Lawson 	printf("S4BIOS_REQ=0x%x\n", fadt->s4biosreq);
4798e6a8737SNate Lawson 	printf("\tPSTATE_CNT=0x%x\n", fadt->pstate_cnt);
480e1e9a4bfSMitsuru IWASAKI 	printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
4818e6a8737SNate Lawson 	       fadt->pm1a_evt_blk,
4828e6a8737SNate Lawson 	       fadt->pm1a_evt_blk + fadt->pm1_evt_len - 1);
4838e6a8737SNate Lawson 	if (fadt->pm1b_evt_blk != 0)
484e1e9a4bfSMitsuru IWASAKI 		printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
4858e6a8737SNate Lawson 		       fadt->pm1b_evt_blk,
4868e6a8737SNate Lawson 		       fadt->pm1b_evt_blk + fadt->pm1_evt_len - 1);
487e1e9a4bfSMitsuru IWASAKI 	printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
4888e6a8737SNate Lawson 	       fadt->pm1a_cnt_blk,
4898e6a8737SNate Lawson 	       fadt->pm1a_cnt_blk + fadt->pm1_cnt_len - 1);
4908e6a8737SNate Lawson 	if (fadt->pm1b_cnt_blk != 0)
491e1e9a4bfSMitsuru IWASAKI 		printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
4928e6a8737SNate Lawson 		       fadt->pm1b_cnt_blk,
4938e6a8737SNate Lawson 		       fadt->pm1b_cnt_blk + fadt->pm1_cnt_len - 1);
4948e6a8737SNate Lawson 	if (fadt->pm2_cnt_blk != 0)
495e1e9a4bfSMitsuru IWASAKI 		printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
4968e6a8737SNate Lawson 		       fadt->pm2_cnt_blk,
4978e6a8737SNate Lawson 		       fadt->pm2_cnt_blk + fadt->pm2_cnt_len - 1);
498c08c4e81SNate Lawson 	printf("\tPM_TMR_BLK=0x%x-0x%x\n",
4998e6a8737SNate Lawson 	       fadt->pm_tmr_blk,
5008e6a8737SNate Lawson 	       fadt->pm_tmr_blk + fadt->pm_tmr_len - 1);
5018e6a8737SNate Lawson 	if (fadt->gpe0_blk != 0)
5028e6a8737SNate Lawson 		printf("\tGPE0_BLK=0x%x-0x%x\n",
5038e6a8737SNate Lawson 		       fadt->gpe0_blk,
5048e6a8737SNate Lawson 		       fadt->gpe0_blk + fadt->gpe0_len - 1);
5058e6a8737SNate Lawson 	if (fadt->gpe1_blk != 0)
5068e6a8737SNate Lawson 		printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
5078e6a8737SNate Lawson 		       fadt->gpe1_blk,
5088e6a8737SNate Lawson 		       fadt->gpe1_blk + fadt->gpe1_len - 1,
5098e6a8737SNate Lawson 		       fadt->gpe1_base);
5108e6a8737SNate Lawson 	if (fadt->cst_cnt != 0)
5118e6a8737SNate Lawson 		printf("\tCST_CNT=0x%x\n", fadt->cst_cnt);
51251c1824fSNate Lawson 	printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
5138e6a8737SNate Lawson 	       fadt->p_lvl2_lat, fadt->p_lvl3_lat);
514e1e9a4bfSMitsuru IWASAKI 	printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
5158e6a8737SNate Lawson 	       fadt->flush_size, fadt->flush_stride);
516e1e9a4bfSMitsuru IWASAKI 	printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
5178e6a8737SNate Lawson 	       fadt->duty_off, fadt->duty_width);
518e1e9a4bfSMitsuru IWASAKI 	printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
5198e6a8737SNate Lawson 	       fadt->day_alrm, fadt->mon_alrm, fadt->century);
520e1e9a4bfSMitsuru IWASAKI 
5218e6a8737SNate Lawson #define PRINTFLAG(var, flag) do {			\
5228e6a8737SNate Lawson 	if ((var) & FADT_FLAG_## flag) {		\
5238e6a8737SNate Lawson 		printf("%c%s", sep, #flag); sep = ',';	\
524e1e9a4bfSMitsuru IWASAKI 	}						\
525e1e9a4bfSMitsuru IWASAKI } while (0)
526e1e9a4bfSMitsuru IWASAKI 
5278e6a8737SNate Lawson 	printf("\tIAPC_BOOT_ARCH=");
5288e6a8737SNate Lawson 	sep = '{';
5298e6a8737SNate Lawson 	PRINTFLAG(fadt->iapc_boot_arch, LEGACY_DEV);
5308e6a8737SNate Lawson 	PRINTFLAG(fadt->iapc_boot_arch, 8042);
53169a9febdSNate Lawson 	if (fadt->iapc_boot_arch != 0)
5324e36f5a1SNate Lawson 		printf("}");
5334e36f5a1SNate Lawson 	printf("\n");
5348e6a8737SNate Lawson 
5358e6a8737SNate Lawson 	printf("\tFlags=");
5368e6a8737SNate Lawson 	sep = '{';
5378e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, WBINVD);
5388e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, WBINVD_FLUSH);
5398e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, PROC_C1);
5408e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, P_LVL2_UP);
5418e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, PWR_BUTTON);
5428e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, SLP_BUTTON);
5438e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, FIX_RTC);
5448e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, RTC_S4);
5458e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, TMR_VAL_EXT);
5468e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, DCK_CAP);
5478e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, RESET_REG);
5488e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, SEALED_CASE);
5498e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, HEADLESS);
5508e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, CPU_SW_SLP);
55169a9febdSNate Lawson 	if (fadt->flags != 0)
5528e6a8737SNate Lawson 		printf("}\n");
553e1e9a4bfSMitsuru IWASAKI 
554e1e9a4bfSMitsuru IWASAKI #undef PRINTFLAG
555e1e9a4bfSMitsuru IWASAKI 
5568e6a8737SNate Lawson 	if (fadt->flags & FADT_FLAG_RESET_REG) {
5578e6a8737SNate Lawson 		printf("\tRESET_REG=");
5588e6a8737SNate Lawson 		acpi_print_gas(&fadt->reset_reg);
5598e6a8737SNate Lawson 		printf(", RESET_VALUE=%#x\n", fadt->reset_value);
5608e6a8737SNate Lawson 	}
561c2962974SNate Lawson 	if (acpi_get_fadt_revision(fadt) > 1) {
562773b6454SNate Lawson 		printf("\tX_FACS=0x%08lx, ", (u_long)fadt->x_facs_ptr);
563773b6454SNate Lawson 		printf("X_DSDT=0x%08lx\n", (u_long)fadt->x_dsdt_ptr);
564c08c4e81SNate Lawson 		printf("\tX_PM1a_EVT_BLK=");
565773b6454SNate Lawson 		acpi_print_gas(&fadt->x_pm1a_evt_blk);
566c08c4e81SNate Lawson 		if (fadt->x_pm1b_evt_blk.address != 0) {
567c08c4e81SNate Lawson 			printf("\n\tX_PM1b_EVT_BLK=");
568773b6454SNate Lawson 			acpi_print_gas(&fadt->x_pm1b_evt_blk);
569c08c4e81SNate Lawson 		}
570c08c4e81SNate Lawson 		printf("\n\tX_PM1a_CNT_BLK=");
571773b6454SNate Lawson 		acpi_print_gas(&fadt->x_pm1a_cnt_blk);
572c08c4e81SNate Lawson 		if (fadt->x_pm1b_cnt_blk.address != 0) {
573c08c4e81SNate Lawson 			printf("\n\tX_PM1b_CNT_BLK=");
574773b6454SNate Lawson 			acpi_print_gas(&fadt->x_pm1b_cnt_blk);
575c08c4e81SNate Lawson 		}
576c08c4e81SNate Lawson 		if (fadt->x_pm1b_cnt_blk.address != 0) {
577773b6454SNate Lawson 			printf("\n\tX_PM2_CNT_BLK=");
578773b6454SNate Lawson 			acpi_print_gas(&fadt->x_pm2_cnt_blk);
579c08c4e81SNate Lawson 		}
580773b6454SNate Lawson 		printf("\n\tX_PM_TMR_BLK=");
581773b6454SNate Lawson 		acpi_print_gas(&fadt->x_pm_tmr_blk);
582c08c4e81SNate Lawson 		if (fadt->x_gpe0_blk.address != 0) {
583773b6454SNate Lawson 			printf("\n\tX_GPE0_BLK=");
584773b6454SNate Lawson 			acpi_print_gas(&fadt->x_gpe0_blk);
585c08c4e81SNate Lawson 		}
586c08c4e81SNate Lawson 		if (fadt->x_gpe1_blk.address != 0) {
587773b6454SNate Lawson 			printf("\n\tX_GPE1_BLK=");
588773b6454SNate Lawson 			acpi_print_gas(&fadt->x_gpe1_blk);
589c08c4e81SNate Lawson 		}
590773b6454SNate Lawson 		printf("\n");
591773b6454SNate Lawson 	}
5928e6a8737SNate Lawson 
5938e6a8737SNate Lawson 	printf(END_COMMENT);
5948e6a8737SNate Lawson }
5958e6a8737SNate Lawson 
5968e6a8737SNate Lawson static void
5978e6a8737SNate Lawson acpi_print_facs(struct FACSbody *facs)
5988e6a8737SNate Lawson {
5998e6a8737SNate Lawson 	printf(BEGIN_COMMENT);
6008e6a8737SNate Lawson 	printf("  FACS:\tLength=%u, ", facs->len);
6018e6a8737SNate Lawson 	printf("HwSig=0x%08x, ", facs->hw_sig);
6028e6a8737SNate Lawson 	printf("Firm_Wake_Vec=0x%08x\n", facs->firm_wake_vec);
6038e6a8737SNate Lawson 
604773b6454SNate Lawson 	printf("\tGlobal_Lock=");
6058e6a8737SNate Lawson 	if (facs->global_lock != 0) {
6068e6a8737SNate Lawson 		if (facs->global_lock & FACS_FLAG_LOCK_PENDING)
6078e6a8737SNate Lawson 			printf("PENDING,");
6088e6a8737SNate Lawson 		if (facs->global_lock & FACS_FLAG_LOCK_OWNED)
6098e6a8737SNate Lawson 			printf("OWNED");
6108e6a8737SNate Lawson 	}
611773b6454SNate Lawson 	printf("\n");
6128e6a8737SNate Lawson 
613773b6454SNate Lawson 	printf("\tFlags=");
6148e6a8737SNate Lawson 	if (facs->flags & FACS_FLAG_S4BIOS_F)
6158e6a8737SNate Lawson 		printf("S4BIOS");
616773b6454SNate Lawson 	printf("\n");
6178e6a8737SNate Lawson 
6188e6a8737SNate Lawson 	if (facs->x_firm_wake_vec != 0) {
6198e6a8737SNate Lawson 		printf("\tX_Firm_Wake_Vec=%08lx\n",
6208e6a8737SNate Lawson 		       (u_long)facs->x_firm_wake_vec);
6218e6a8737SNate Lawson 	}
622773b6454SNate Lawson 	printf("\tVersion=%u\n", facs->version);
6238e6a8737SNate Lawson 
624c62f1cccSMitsuru IWASAKI 	printf(END_COMMENT);
625e1e9a4bfSMitsuru IWASAKI }
626e1e9a4bfSMitsuru IWASAKI 
627945137d9SNate Lawson static void
628e1e9a4bfSMitsuru IWASAKI acpi_print_dsdt(struct ACPIsdt *dsdp)
629e1e9a4bfSMitsuru IWASAKI {
630773b6454SNate Lawson 	printf(BEGIN_COMMENT);
631773b6454SNate Lawson 	acpi_print_sdt(dsdp);
632773b6454SNate Lawson 	printf(END_COMMENT);
633e1e9a4bfSMitsuru IWASAKI }
634e1e9a4bfSMitsuru IWASAKI 
635e1e9a4bfSMitsuru IWASAKI int
636e1e9a4bfSMitsuru IWASAKI acpi_checksum(void *p, size_t length)
637e1e9a4bfSMitsuru IWASAKI {
638e1e9a4bfSMitsuru IWASAKI 	u_int8_t	*bp;
639e1e9a4bfSMitsuru IWASAKI 	u_int8_t	sum;
640e1e9a4bfSMitsuru IWASAKI 
641e1e9a4bfSMitsuru IWASAKI 	bp = p;
642e1e9a4bfSMitsuru IWASAKI 	sum = 0;
643e1e9a4bfSMitsuru IWASAKI 	while (length--)
644e1e9a4bfSMitsuru IWASAKI 		sum += *bp++;
645e1e9a4bfSMitsuru IWASAKI 
646e1e9a4bfSMitsuru IWASAKI 	return (sum);
647e1e9a4bfSMitsuru IWASAKI }
648e1e9a4bfSMitsuru IWASAKI 
649945137d9SNate Lawson static struct ACPIsdt *
650e1e9a4bfSMitsuru IWASAKI acpi_map_sdt(vm_offset_t pa)
651e1e9a4bfSMitsuru IWASAKI {
652e1e9a4bfSMitsuru IWASAKI 	struct	ACPIsdt *sp;
653e1e9a4bfSMitsuru IWASAKI 
654e1e9a4bfSMitsuru IWASAKI 	sp = acpi_map_physical(pa, sizeof(struct ACPIsdt));
655e1e9a4bfSMitsuru IWASAKI 	sp = acpi_map_physical(pa, sp->len);
656e1e9a4bfSMitsuru IWASAKI 	return (sp);
657e1e9a4bfSMitsuru IWASAKI }
658e1e9a4bfSMitsuru IWASAKI 
659945137d9SNate Lawson static void
660e1e9a4bfSMitsuru IWASAKI acpi_print_rsd_ptr(struct ACPIrsdp *rp)
661e1e9a4bfSMitsuru IWASAKI {
662c62f1cccSMitsuru IWASAKI 	printf(BEGIN_COMMENT);
663a74172abSNate Lawson 	printf("  RSD PTR: OEM=");
664e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(rp->oem, 6);
665773b6454SNate Lawson 	printf(", ACPI_Rev=%s (%d)\n", rp->revision < 2 ? "1.0x" : "2.0x",
666a74172abSNate Lawson 	       rp->revision);
667a74172abSNate Lawson 	if (rp->revision < 2) {
668a74172abSNate Lawson 		printf("\tRSDT=0x%08x, cksum=%u\n", rp->rsdt_addr, rp->sum);
669a74172abSNate Lawson 	} else {
670a74172abSNate Lawson 		printf("\tXSDT=0x%08lx, length=%u, cksum=%u\n",
671a74172abSNate Lawson 		    (u_long)rp->xsdt_addr, rp->length, rp->xsum);
672a74172abSNate Lawson 	}
673c62f1cccSMitsuru IWASAKI 	printf(END_COMMENT);
674e1e9a4bfSMitsuru IWASAKI }
675e1e9a4bfSMitsuru IWASAKI 
676945137d9SNate Lawson static void
677e1e9a4bfSMitsuru IWASAKI acpi_handle_rsdt(struct ACPIsdt *rsdp)
678e1e9a4bfSMitsuru IWASAKI {
679e1e9a4bfSMitsuru IWASAKI 	struct ACPIsdt *sdp;
680a74172abSNate Lawson 	vm_offset_t addr;
681a74172abSNate Lawson 	int entries, i;
682e1e9a4bfSMitsuru IWASAKI 
683e1e9a4bfSMitsuru IWASAKI 	acpi_print_rsdt(rsdp);
684a74172abSNate Lawson 	entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
685e1e9a4bfSMitsuru IWASAKI 	for (i = 0; i < entries; i++) {
686a74172abSNate Lawson 		switch (addr_size) {
687a74172abSNate Lawson 		case 4:
688a74172abSNate Lawson 			addr = le32dec((char*)rsdp->body + i * addr_size);
689a74172abSNate Lawson 			break;
690a74172abSNate Lawson 		case 8:
691a74172abSNate Lawson 			addr = le64dec((char*)rsdp->body + i * addr_size);
692a74172abSNate Lawson 			break;
693a74172abSNate Lawson 		default:
694a74172abSNate Lawson 			assert((addr = 0));
695a74172abSNate Lawson 		}
696a74172abSNate Lawson 
697a74172abSNate Lawson 		sdp = (struct ACPIsdt *)acpi_map_sdt(addr);
6985cf6d493SNate Lawson 		if (acpi_checksum(sdp, sdp->len)) {
6995cf6d493SNate Lawson 			warnx("RSDT entry %d (sig %.4s) is corrupt", i,
7005cf6d493SNate Lawson 			    sdp->signature);
7015cf6d493SNate Lawson 			continue;
7025cf6d493SNate Lawson 		}
703945137d9SNate Lawson 		if (!memcmp(sdp->signature, "FACP", 4))
7042177d4e6SNate Lawson 			acpi_handle_fadt(sdp);
705945137d9SNate Lawson 		else if (!memcmp(sdp->signature, "APIC", 4))
7060a473124SJohn Baldwin 			acpi_handle_apic(sdp);
707945137d9SNate Lawson 		else if (!memcmp(sdp->signature, "HPET", 4))
70879d7565cSPeter Wemm 			acpi_handle_hpet(sdp);
70955d7ff9eSNate Lawson 		else if (!memcmp(sdp->signature, "ECDT", 4))
71055d7ff9eSNate Lawson 			acpi_handle_ecdt(sdp);
711a47e681bSScott Long 		else if (!memcmp(sdp->signature, "MCFG", 4))
712a47e681bSScott Long 			acpi_handle_mcfg(sdp);
713773b6454SNate Lawson 		else {
714773b6454SNate Lawson 			printf(BEGIN_COMMENT);
715773b6454SNate Lawson 			acpi_print_sdt(sdp);
716773b6454SNate Lawson 			printf(END_COMMENT);
717773b6454SNate Lawson 		}
718e1e9a4bfSMitsuru IWASAKI 	}
719e1e9a4bfSMitsuru IWASAKI }
720c62f1cccSMitsuru IWASAKI 
721945137d9SNate Lawson struct ACPIsdt *
722476daaecSDag-Erling Smørgrav sdt_load_devmem(void)
723945137d9SNate Lawson {
724945137d9SNate Lawson 	struct	ACPIrsdp *rp;
725945137d9SNate Lawson 	struct	ACPIsdt *rsdp;
726945137d9SNate Lawson 
727945137d9SNate Lawson 	rp = acpi_find_rsd_ptr();
728945137d9SNate Lawson 	if (!rp)
729945137d9SNate Lawson 		errx(1, "Can't find ACPI information");
730945137d9SNate Lawson 
731945137d9SNate Lawson 	if (tflag)
732945137d9SNate Lawson 		acpi_print_rsd_ptr(rp);
733a74172abSNate Lawson 	if (rp->revision < 2) {
734945137d9SNate Lawson 		rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->rsdt_addr);
735945137d9SNate Lawson 		if (memcmp(rsdp->signature, "RSDT", 4) != 0 ||
736945137d9SNate Lawson 		    acpi_checksum(rsdp, rsdp->len) != 0)
737945137d9SNate Lawson 			errx(1, "RSDT is corrupted");
738a74172abSNate Lawson 		addr_size = sizeof(uint32_t);
739a74172abSNate Lawson 	} else {
740a74172abSNate Lawson 		rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->xsdt_addr);
741a74172abSNate Lawson 		if (memcmp(rsdp->signature, "XSDT", 4) != 0 ||
742a74172abSNate Lawson 		    acpi_checksum(rsdp, rsdp->len) != 0)
743a74172abSNate Lawson 			errx(1, "XSDT is corrupted");
744a74172abSNate Lawson 		addr_size = sizeof(uint64_t);
745a74172abSNate Lawson 	}
746945137d9SNate Lawson 	return (rsdp);
747945137d9SNate Lawson }
748c62f1cccSMitsuru IWASAKI 
74962c7bde1SNate Lawson /* Write the DSDT to a file, concatenating any SSDTs (if present). */
750bfa3f012SMarcel Moolenaar static int
751bfa3f012SMarcel Moolenaar write_dsdt(int fd, struct ACPIsdt *rsdt, struct ACPIsdt *dsdt)
752bfa3f012SMarcel Moolenaar {
753bfa3f012SMarcel Moolenaar 	struct ACPIsdt sdt;
754bfa3f012SMarcel Moolenaar 	struct ACPIsdt *ssdt;
755bfa3f012SMarcel Moolenaar 	uint8_t sum;
756bfa3f012SMarcel Moolenaar 
75762c7bde1SNate Lawson 	/* Create a new checksum to account for the DSDT and any SSDTs. */
758bfa3f012SMarcel Moolenaar 	sdt = *dsdt;
759bfa3f012SMarcel Moolenaar 	if (rsdt != NULL) {
760bfa3f012SMarcel Moolenaar 		sdt.check = 0;
761bfa3f012SMarcel Moolenaar 		sum = acpi_checksum(dsdt->body, dsdt->len - SIZEOF_SDT_HDR);
762bfa3f012SMarcel Moolenaar 		ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
763f7675a56SNate Lawson 		while (ssdt != NULL) {
764bfa3f012SMarcel Moolenaar 			sdt.len += ssdt->len - SIZEOF_SDT_HDR;
765bfa3f012SMarcel Moolenaar 			sum += acpi_checksum(ssdt->body,
766bfa3f012SMarcel Moolenaar 			    ssdt->len - SIZEOF_SDT_HDR);
767bfa3f012SMarcel Moolenaar 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
768bfa3f012SMarcel Moolenaar 		}
769bfa3f012SMarcel Moolenaar 		sum += acpi_checksum(&sdt, SIZEOF_SDT_HDR);
770bfa3f012SMarcel Moolenaar 		sdt.check -= sum;
771bfa3f012SMarcel Moolenaar 	}
77262c7bde1SNate Lawson 
77362c7bde1SNate Lawson 	/* Write out the DSDT header and body. */
774bfa3f012SMarcel Moolenaar 	write(fd, &sdt, SIZEOF_SDT_HDR);
775bfa3f012SMarcel Moolenaar 	write(fd, dsdt->body, dsdt->len - SIZEOF_SDT_HDR);
77662c7bde1SNate Lawson 
777b64e1b67SNate Lawson 	/* Write out any SSDTs (if present.) */
778f7675a56SNate Lawson 	if (rsdt != NULL) {
779bfa3f012SMarcel Moolenaar 		ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
780bfa3f012SMarcel Moolenaar 		while (ssdt != NULL) {
781bfa3f012SMarcel Moolenaar 			write(fd, ssdt->body, ssdt->len - SIZEOF_SDT_HDR);
782bfa3f012SMarcel Moolenaar 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
783bfa3f012SMarcel Moolenaar 		}
784bfa3f012SMarcel Moolenaar 	}
785bfa3f012SMarcel Moolenaar 	return (0);
786bfa3f012SMarcel Moolenaar }
787bfa3f012SMarcel Moolenaar 
788c62f1cccSMitsuru IWASAKI void
789bfa3f012SMarcel Moolenaar dsdt_save_file(char *outfile, struct ACPIsdt *rsdt, struct ACPIsdt *dsdp)
790c62f1cccSMitsuru IWASAKI {
791945137d9SNate Lawson 	int	fd;
792945137d9SNate Lawson 	mode_t	mode;
793945137d9SNate Lawson 
794945137d9SNate Lawson 	assert(outfile != NULL);
795945137d9SNate Lawson 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
796945137d9SNate Lawson 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
797945137d9SNate Lawson 	if (fd == -1) {
798945137d9SNate Lawson 		perror("dsdt_save_file");
799945137d9SNate Lawson 		return;
800945137d9SNate Lawson 	}
801bfa3f012SMarcel Moolenaar 	write_dsdt(fd, rsdt, dsdp);
802945137d9SNate Lawson 	close(fd);
803c62f1cccSMitsuru IWASAKI }
804c62f1cccSMitsuru IWASAKI 
805945137d9SNate Lawson void
806bfa3f012SMarcel Moolenaar aml_disassemble(struct ACPIsdt *rsdt, struct ACPIsdt *dsdp)
807c62f1cccSMitsuru IWASAKI {
80899065116SJung-uk Kim 	char buf[PATH_MAX], tmpstr[PATH_MAX];
80999065116SJung-uk Kim 	const char *tmpdir;
81099065116SJung-uk Kim 	char *tmpext;
811945137d9SNate Lawson 	FILE *fp;
81299065116SJung-uk Kim 	size_t len;
81399065116SJung-uk Kim 	int fd;
814945137d9SNate Lawson 
81599065116SJung-uk Kim 	tmpdir = getenv("TMPDIR");
81699065116SJung-uk Kim 	if (tmpdir == NULL)
81799065116SJung-uk Kim 		tmpdir = _PATH_TMP;
81899065116SJung-uk Kim 	strncpy(tmpstr, tmpdir, sizeof(tmpstr));
81999065116SJung-uk Kim 	strncat(tmpstr, "/acpidump.", sizeof(tmpstr) - strlen(tmpdir));
82099065116SJung-uk Kim 	if (realpath(tmpstr, buf) == NULL) {
82199065116SJung-uk Kim 		perror("realpath tmp file");
82299065116SJung-uk Kim 		return;
82399065116SJung-uk Kim 	}
82499065116SJung-uk Kim 	strncpy(tmpstr, buf, sizeof(tmpstr));
82599065116SJung-uk Kim 	len = strlen(buf);
82699065116SJung-uk Kim 	tmpext = tmpstr + len;
82799065116SJung-uk Kim 	strncpy(tmpext, "XXXXXX", sizeof(tmpstr) - len);
828945137d9SNate Lawson 	fd = mkstemp(tmpstr);
829945137d9SNate Lawson 	if (fd < 0) {
830945137d9SNate Lawson 		perror("iasl tmp file");
831945137d9SNate Lawson 		return;
832c62f1cccSMitsuru IWASAKI 	}
833bfa3f012SMarcel Moolenaar 	write_dsdt(fd, rsdt, dsdp);
834945137d9SNate Lawson 	close(fd);
835945137d9SNate Lawson 
836945137d9SNate Lawson 	/* Run iasl -d on the temp file */
837945137d9SNate Lawson 	if (fork() == 0) {
838945137d9SNate Lawson 		close(STDOUT_FILENO);
839945137d9SNate Lawson 		if (vflag == 0)
840945137d9SNate Lawson 			close(STDERR_FILENO);
84199065116SJung-uk Kim 		execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
842945137d9SNate Lawson 		err(1, "exec");
843c62f1cccSMitsuru IWASAKI 	}
844c62f1cccSMitsuru IWASAKI 
845945137d9SNate Lawson 	wait(NULL);
846945137d9SNate Lawson 	unlink(tmpstr);
847945137d9SNate Lawson 
848945137d9SNate Lawson 	/* Dump iasl's output to stdout */
84999065116SJung-uk Kim 	strncpy(tmpext, "dsl", sizeof(tmpstr) - len);
85099065116SJung-uk Kim 	fp = fopen(tmpstr, "r");
85199065116SJung-uk Kim 	unlink(tmpstr);
852945137d9SNate Lawson 	if (fp == NULL) {
853945137d9SNate Lawson 		perror("iasl tmp file (read)");
854945137d9SNate Lawson 		return;
855945137d9SNate Lawson 	}
856945137d9SNate Lawson 	while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
857945137d9SNate Lawson 		fwrite(buf, 1, len, stdout);
858945137d9SNate Lawson 	fclose(fp);
859c62f1cccSMitsuru IWASAKI }
860c62f1cccSMitsuru IWASAKI 
861945137d9SNate Lawson void
862945137d9SNate Lawson sdt_print_all(struct ACPIsdt *rsdp)
863c62f1cccSMitsuru IWASAKI {
864945137d9SNate Lawson 	acpi_handle_rsdt(rsdp);
865c62f1cccSMitsuru IWASAKI }
866c62f1cccSMitsuru IWASAKI 
867bfa3f012SMarcel Moolenaar /* Fetch a table matching the given signature via the RSDT. */
868945137d9SNate Lawson struct ACPIsdt *
869bfa3f012SMarcel Moolenaar sdt_from_rsdt(struct ACPIsdt *rsdt, const char *sig, struct ACPIsdt *last)
870c62f1cccSMitsuru IWASAKI {
871945137d9SNate Lawson 	struct ACPIsdt *sdt;
872a74172abSNate Lawson 	vm_offset_t addr;
873a74172abSNate Lawson 	int entries, i;
874945137d9SNate Lawson 
875a74172abSNate Lawson 	entries = (rsdt->len - SIZEOF_SDT_HDR) / addr_size;
876945137d9SNate Lawson 	for (i = 0; i < entries; i++) {
877a74172abSNate Lawson 		switch (addr_size) {
878a74172abSNate Lawson 		case 4:
879a74172abSNate Lawson 			addr = le32dec((char*)rsdt->body + i * addr_size);
880a74172abSNate Lawson 			break;
881a74172abSNate Lawson 		case 8:
882a74172abSNate Lawson 			addr = le64dec((char*)rsdt->body + i * addr_size);
883a74172abSNate Lawson 			break;
884a74172abSNate Lawson 		default:
885a74172abSNate Lawson 			assert((addr = 0));
886a74172abSNate Lawson 		}
887a74172abSNate Lawson 		sdt = (struct ACPIsdt *)acpi_map_sdt(addr);
888bfa3f012SMarcel Moolenaar 		if (last != NULL) {
889bfa3f012SMarcel Moolenaar 			if (sdt == last)
890bfa3f012SMarcel Moolenaar 				last = NULL;
891bfa3f012SMarcel Moolenaar 			continue;
892bfa3f012SMarcel Moolenaar 		}
893a74172abSNate Lawson 		if (memcmp(sdt->signature, sig, strlen(sig)))
894a74172abSNate Lawson 			continue;
895945137d9SNate Lawson 		if (acpi_checksum(sdt, sdt->len))
896945137d9SNate Lawson 			errx(1, "RSDT entry %d is corrupt", i);
897945137d9SNate Lawson 		return (sdt);
898c62f1cccSMitsuru IWASAKI 	}
899c62f1cccSMitsuru IWASAKI 
900945137d9SNate Lawson 	return (NULL);
901c62f1cccSMitsuru IWASAKI }
902c62f1cccSMitsuru IWASAKI 
903945137d9SNate Lawson struct ACPIsdt *
9048e6a8737SNate Lawson dsdt_from_fadt(struct FADTbody *fadt)
905c62f1cccSMitsuru IWASAKI {
906945137d9SNate Lawson 	struct	ACPIsdt	*sdt;
907c62f1cccSMitsuru IWASAKI 
908c83f0f99SNate Lawson 	/* Use the DSDT address if it is version 1, otherwise use X_DSDT. */
909c2962974SNate Lawson 	if (acpi_get_fadt_revision(fadt) == 1)
9108e6a8737SNate Lawson 		sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
9112e71eb12SNate Lawson 	else
9122e71eb12SNate Lawson 		sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
913945137d9SNate Lawson 	if (acpi_checksum(sdt, sdt->len))
914945137d9SNate Lawson 		errx(1, "DSDT is corrupt\n");
915945137d9SNate Lawson 	return (sdt);
916c62f1cccSMitsuru IWASAKI }
917