xref: /freebsd/usr.sbin/acpi/acpidump/acpi.c (revision c83f0f992a829358f6d088c5085357d3f1f55bc1)
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>
37e1e9a4bfSMitsuru IWASAKI #include <stdio.h>
38945137d9SNate Lawson #include <string.h>
39e1e9a4bfSMitsuru IWASAKI #include <unistd.h>
40e1e9a4bfSMitsuru IWASAKI 
41e1e9a4bfSMitsuru IWASAKI #include "acpidump.h"
42e1e9a4bfSMitsuru IWASAKI 
43c62f1cccSMitsuru IWASAKI #define BEGIN_COMMENT	"/*\n"
44c62f1cccSMitsuru IWASAKI #define END_COMMENT	" */\n"
45c62f1cccSMitsuru IWASAKI 
46945137d9SNate Lawson static void	acpi_print_string(char *s, size_t length);
478e6a8737SNate Lawson static void	acpi_print_gas(struct ACPIgas *gas);
488e6a8737SNate Lawson static void	acpi_handle_fadt(struct FADTbody *fadt);
49945137d9SNate Lawson static void	acpi_print_cpu(u_char cpu_id);
50945137d9SNate Lawson static void	acpi_print_local_apic(u_char cpu_id, u_char apic_id,
51945137d9SNate Lawson 				      u_int32_t flags);
52945137d9SNate Lawson static void	acpi_print_io_apic(u_char apic_id, u_int32_t int_base,
53945137d9SNate Lawson 				   u_int64_t apic_addr);
54945137d9SNate Lawson static void	acpi_print_mps_flags(u_int16_t flags);
55945137d9SNate Lawson static void	acpi_print_intr(u_int32_t intr, u_int16_t mps_flags);
56945137d9SNate Lawson static void	acpi_print_apic(struct MADT_APIC *mp);
57945137d9SNate Lawson static void	acpi_handle_apic(struct ACPIsdt *sdp);
58945137d9SNate Lawson static void	acpi_handle_hpet(struct ACPIsdt *sdp);
59773b6454SNate Lawson static void	acpi_print_sdt(struct ACPIsdt *sdp);
608e6a8737SNate Lawson static void	acpi_print_fadt(struct FADTbody *fadt);
618e6a8737SNate Lawson static void	acpi_print_facs(struct FACSbody *facs);
62945137d9SNate Lawson static void	acpi_print_dsdt(struct ACPIsdt *dsdp);
63a74172abSNate Lawson static struct ACPIsdt *acpi_map_sdt(vm_offset_t pa);
64945137d9SNate Lawson static void	acpi_print_rsd_ptr(struct ACPIrsdp *rp);
65945137d9SNate Lawson static void	acpi_handle_rsdt(struct ACPIsdt *rsdp);
66c62f1cccSMitsuru IWASAKI 
67773b6454SNate Lawson /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
68a74172abSNate Lawson static int addr_size;
69a74172abSNate Lawson 
70c83f0f99SNate Lawson /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
71c83f0f99SNate Lawson static int fadt_revision;
72c83f0f99SNate 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 
1218e6a8737SNate Lawson static void
1228e6a8737SNate Lawson acpi_handle_fadt(struct FADTbody *fadt)
123e1e9a4bfSMitsuru IWASAKI {
124e1e9a4bfSMitsuru IWASAKI 	struct ACPIsdt	*dsdp;
1258e6a8737SNate Lawson 	struct FACSbody	*facs;
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;
141c83f0f99SNate Lawson 	} else {
142c83f0f99SNate Lawson 		fadt_revision = 1;
143c83f0f99SNate Lawson 	}
144c83f0f99SNate Lawson 	acpi_print_fadt(fadt);
145c83f0f99SNate Lawson 
146c83f0f99SNate Lawson 	if (fadt_revision == 1)
1478e6a8737SNate Lawson 		facs = (struct FACSbody *)acpi_map_sdt(fadt->facs_ptr);
148773b6454SNate Lawson 	else
149773b6454SNate Lawson 		facs = (struct FACSbody *)acpi_map_sdt(fadt->x_facs_ptr);
1508e6a8737SNate Lawson 	if (memcmp(facs->signature, "FACS", 4) != 0 || facs->len < 64)
1518e6a8737SNate Lawson 		errx(1, "FACS is corrupt");
1528e6a8737SNate Lawson 	acpi_print_facs(facs);
1538e6a8737SNate Lawson 
154c83f0f99SNate Lawson 	if (fadt_revision == 1)
1558e6a8737SNate Lawson 		dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
156773b6454SNate Lawson 	else
157773b6454SNate Lawson 		dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
158e1e9a4bfSMitsuru IWASAKI 	if (acpi_checksum(dsdp, dsdp->len))
159945137d9SNate Lawson 		errx(1, "DSDT is corrupt");
160945137d9SNate Lawson 	acpi_print_dsdt(dsdp);
161c62f1cccSMitsuru IWASAKI }
162c62f1cccSMitsuru IWASAKI 
163c62f1cccSMitsuru IWASAKI static void
1640a473124SJohn Baldwin acpi_print_cpu(u_char cpu_id)
1650a473124SJohn Baldwin {
1660a473124SJohn Baldwin 
1670a473124SJohn Baldwin 	printf("\tACPI CPU=");
1680a473124SJohn Baldwin 	if (cpu_id == 0xff)
1690a473124SJohn Baldwin 		printf("ALL\n");
1700a473124SJohn Baldwin 	else
1710a473124SJohn Baldwin 		printf("%d\n", (u_int)cpu_id);
1720a473124SJohn Baldwin }
1730a473124SJohn Baldwin 
1740a473124SJohn Baldwin static void
1750a473124SJohn Baldwin acpi_print_local_apic(u_char cpu_id, u_char apic_id, u_int32_t flags)
1760a473124SJohn Baldwin {
1770a473124SJohn Baldwin 	acpi_print_cpu(cpu_id);
1780a473124SJohn Baldwin 	printf("\tFlags={");
1790a473124SJohn Baldwin 	if (flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED)
1800a473124SJohn Baldwin 		printf("ENABLED");
1810a473124SJohn Baldwin 	else
1820a473124SJohn Baldwin 		printf("DISABLED");
1830a473124SJohn Baldwin 	printf("}\n");
1840a473124SJohn Baldwin 	printf("\tAPIC ID=%d\n", (u_int)apic_id);
1850a473124SJohn Baldwin }
1860a473124SJohn Baldwin 
1870a473124SJohn Baldwin static void
1880a473124SJohn Baldwin acpi_print_io_apic(u_char apic_id, u_int32_t int_base, u_int64_t apic_addr)
1890a473124SJohn Baldwin {
1900a473124SJohn Baldwin 	printf("\tAPIC ID=%d\n", (u_int)apic_id);
1910a473124SJohn Baldwin 	printf("\tINT BASE=%d\n", int_base);
192945137d9SNate Lawson 	printf("\tADDR=0x%016jx\n", apic_addr);
1930a473124SJohn Baldwin }
1940a473124SJohn Baldwin 
1950a473124SJohn Baldwin static void
1960a473124SJohn Baldwin acpi_print_mps_flags(u_int16_t flags)
1970a473124SJohn Baldwin {
1980a473124SJohn Baldwin 
1990a473124SJohn Baldwin 	printf("\tFlags={Polarity=");
2000a473124SJohn Baldwin 	switch (flags & MPS_INT_FLAG_POLARITY_MASK) {
2010a473124SJohn Baldwin 	case MPS_INT_FLAG_POLARITY_CONFORM:
2020a473124SJohn Baldwin 		printf("conforming");
2030a473124SJohn Baldwin 		break;
2040a473124SJohn Baldwin 	case MPS_INT_FLAG_POLARITY_HIGH:
2050a473124SJohn Baldwin 		printf("active-hi");
2060a473124SJohn Baldwin 		break;
2070a473124SJohn Baldwin 	case MPS_INT_FLAG_POLARITY_LOW:
2080a473124SJohn Baldwin 		printf("active-lo");
2090a473124SJohn Baldwin 		break;
2100a473124SJohn Baldwin 	default:
2110a473124SJohn Baldwin 		printf("0x%x", flags & MPS_INT_FLAG_POLARITY_MASK);
2120a473124SJohn Baldwin 		break;
2130a473124SJohn Baldwin 	}
2140a473124SJohn Baldwin 	printf(", Trigger=");
2150a473124SJohn Baldwin 	switch (flags & MPS_INT_FLAG_TRIGGER_MASK) {
2160a473124SJohn Baldwin 	case MPS_INT_FLAG_TRIGGER_CONFORM:
2170a473124SJohn Baldwin 		printf("conforming");
2180a473124SJohn Baldwin 		break;
2190a473124SJohn Baldwin 	case MPS_INT_FLAG_TRIGGER_EDGE:
2200a473124SJohn Baldwin 		printf("edge");
2210a473124SJohn Baldwin 		break;
2220a473124SJohn Baldwin 	case MPS_INT_FLAG_TRIGGER_LEVEL:
2230a473124SJohn Baldwin 		printf("level");
2240a473124SJohn Baldwin 		break;
2250a473124SJohn Baldwin 	default:
2260a473124SJohn Baldwin 		printf("0x%x", (flags & MPS_INT_FLAG_TRIGGER_MASK) >> 2);
2270a473124SJohn Baldwin 	}
2280a473124SJohn Baldwin 	printf("}\n");
2290a473124SJohn Baldwin }
2300a473124SJohn Baldwin 
2310a473124SJohn Baldwin static void
2320a473124SJohn Baldwin acpi_print_intr(u_int32_t intr, u_int16_t mps_flags)
2330a473124SJohn Baldwin {
2340a473124SJohn Baldwin 
2350a473124SJohn Baldwin 	printf("\tINTR=%d\n", (u_int)intr);
2360a473124SJohn Baldwin 	acpi_print_mps_flags(mps_flags);
2370a473124SJohn Baldwin }
2380a473124SJohn Baldwin 
2390a473124SJohn Baldwin const char *apic_types[] = { "Local APIC", "IO APIC", "INT Override", "NMI",
2400a473124SJohn Baldwin 			     "Local NMI", "Local APIC Override", "IO SAPIC",
2410a473124SJohn Baldwin 			     "Local SAPIC", "Platform Interrupt" };
2420a473124SJohn Baldwin const char *platform_int_types[] = { "PMI", "INIT",
2430a473124SJohn Baldwin 				     "Corrected Platform Error" };
2440a473124SJohn Baldwin 
2450a473124SJohn Baldwin static void
2460a473124SJohn Baldwin acpi_print_apic(struct MADT_APIC *mp)
2470a473124SJohn Baldwin {
2480a473124SJohn Baldwin 
2490a473124SJohn Baldwin 	printf("\tType=%s\n", apic_types[mp->type]);
2500a473124SJohn Baldwin 	switch (mp->type) {
2510a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_APIC:
2520a473124SJohn Baldwin 		acpi_print_local_apic(mp->body.local_apic.cpu_id,
2530a473124SJohn Baldwin 		    mp->body.local_apic.apic_id, mp->body.local_apic.flags);
2540a473124SJohn Baldwin 		break;
2550a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_IO_APIC:
2560a473124SJohn Baldwin 		acpi_print_io_apic(mp->body.io_apic.apic_id,
2570a473124SJohn Baldwin 		    mp->body.io_apic.int_base,
2580a473124SJohn Baldwin 		    mp->body.io_apic.apic_addr);
2590a473124SJohn Baldwin 		break;
2600a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_INT_OVERRIDE:
2610a473124SJohn Baldwin 		printf("\tBUS=%d\n", (u_int)mp->body.int_override.bus);
2620a473124SJohn Baldwin 		printf("\tIRQ=%d\n", (u_int)mp->body.int_override.source);
2630a473124SJohn Baldwin 		acpi_print_intr(mp->body.int_override.intr,
2640a473124SJohn Baldwin 		    mp->body.int_override.mps_flags);
2650a473124SJohn Baldwin 		break;
2660a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_NMI:
2670a473124SJohn Baldwin 		acpi_print_intr(mp->body.nmi.intr, mp->body.nmi.mps_flags);
2680a473124SJohn Baldwin 		break;
2690a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_NMI:
2700a473124SJohn Baldwin 		acpi_print_cpu(mp->body.local_nmi.cpu_id);
2710a473124SJohn Baldwin 		printf("\tLINT Pin=%d\n", mp->body.local_nmi.lintpin);
2720a473124SJohn Baldwin 		acpi_print_mps_flags(mp->body.local_nmi.mps_flags);
2730a473124SJohn Baldwin 		break;
2740a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_OVERRIDE:
275945137d9SNate Lawson 		printf("\tLocal APIC ADDR=0x%016jx\n",
276945137d9SNate Lawson 		    mp->body.local_apic_override.apic_addr);
2770a473124SJohn Baldwin 		break;
2780a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_IO_SAPIC:
2790a473124SJohn Baldwin 		acpi_print_io_apic(mp->body.io_sapic.apic_id,
2800a473124SJohn Baldwin 		    mp->body.io_sapic.int_base,
2810a473124SJohn Baldwin 		    mp->body.io_sapic.apic_addr);
2820a473124SJohn Baldwin 		break;
2830a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_LOCAL_SAPIC:
2840a473124SJohn Baldwin 		acpi_print_local_apic(mp->body.local_sapic.cpu_id,
2850a473124SJohn Baldwin 		    mp->body.local_sapic.apic_id, mp->body.local_sapic.flags);
2860a473124SJohn Baldwin 		printf("\tAPIC EID=%d\n", (u_int)mp->body.local_sapic.apic_eid);
2870a473124SJohn Baldwin 		break;
2880a473124SJohn Baldwin 	case ACPI_MADT_APIC_TYPE_INT_SRC:
2890a473124SJohn Baldwin 		printf("\tType=%s\n",
2900a473124SJohn Baldwin 		    platform_int_types[mp->body.int_src.type]);
2910a473124SJohn Baldwin 		printf("\tCPU ID=%d\n", (u_int)mp->body.int_src.cpu_id);
2920a473124SJohn Baldwin 		printf("\tCPU EID=%d\n", (u_int)mp->body.int_src.cpu_id);
2930a473124SJohn Baldwin 		printf("\tSAPIC Vector=%d\n",
2940a473124SJohn Baldwin 		    (u_int)mp->body.int_src.sapic_vector);
2950a473124SJohn Baldwin 		acpi_print_intr(mp->body.int_src.intr,
2960a473124SJohn Baldwin 		    mp->body.int_src.mps_flags);
2970a473124SJohn Baldwin 		break;
2980a473124SJohn Baldwin 	default:
2990a473124SJohn Baldwin 		printf("\tUnknown type %d\n", (u_int)mp->type);
300945137d9SNate Lawson 		break;
3010a473124SJohn Baldwin 	}
3020a473124SJohn Baldwin }
3030a473124SJohn Baldwin 
3040a473124SJohn Baldwin static void
3050a473124SJohn Baldwin acpi_handle_apic(struct ACPIsdt *sdp)
3060a473124SJohn Baldwin {
3070a473124SJohn Baldwin 	struct MADTbody *madtp;
3080a473124SJohn Baldwin 	struct MADT_APIC *madt_apicp;
3090a473124SJohn Baldwin 
310773b6454SNate Lawson 	printf(BEGIN_COMMENT);
311773b6454SNate Lawson 	acpi_print_sdt(sdp);
3120a473124SJohn Baldwin 	madtp = (struct MADTbody *) sdp->body;
3130a473124SJohn Baldwin 	printf("\tLocal APIC ADDR=0x%08x\n", madtp->lapic_addr);
3140a473124SJohn Baldwin 	printf("\tFlags={");
3150a473124SJohn Baldwin 	if (madtp->flags & ACPI_APIC_FLAG_PCAT_COMPAT)
3160a473124SJohn Baldwin 		printf("PC-AT");
3170a473124SJohn Baldwin 	printf("}\n");
3180a473124SJohn Baldwin 	madt_apicp = (struct MADT_APIC *)madtp->body;
3190a473124SJohn Baldwin 	while (((uintptr_t)madt_apicp) - ((uintptr_t)sdp) < sdp->len) {
3200a473124SJohn Baldwin 		printf("\n");
3210a473124SJohn Baldwin 		acpi_print_apic(madt_apicp);
3220a473124SJohn Baldwin 		madt_apicp = (struct MADT_APIC *) ((char *)madt_apicp +
3230a473124SJohn Baldwin 		    madt_apicp->len);
3240a473124SJohn Baldwin 	}
3250a473124SJohn Baldwin 	printf(END_COMMENT);
3260a473124SJohn Baldwin }
3270a473124SJohn Baldwin 
3280a473124SJohn Baldwin static void
32979d7565cSPeter Wemm acpi_handle_hpet(struct ACPIsdt *sdp)
33079d7565cSPeter Wemm {
33179d7565cSPeter Wemm 	struct HPETbody *hpetp;
33279d7565cSPeter Wemm 
333773b6454SNate Lawson 	printf(BEGIN_COMMENT);
334773b6454SNate Lawson 	acpi_print_sdt(sdp);
33579d7565cSPeter Wemm 	hpetp = (struct HPETbody *) sdp->body;
33679d7565cSPeter Wemm 	printf("\tHPET Number=%d\n", hpetp->hpet_number);
33779d7565cSPeter Wemm 	printf("\tADDR=0x%08x\n", hpetp->base_addr);
33879d7565cSPeter Wemm 	printf("\tHW Rev=0x%x\n", hpetp->block_hwrev);
33979d7565cSPeter Wemm 	printf("\tComparitors=%d\n", hpetp->block_comparitors);
34079d7565cSPeter Wemm 	printf("\tCounter Size=%d\n", hpetp->block_counter_size);
34179d7565cSPeter Wemm 	printf("\tLegacy IRQ routing capable={");
34279d7565cSPeter Wemm 	if (hpetp->block_legacy_capable)
34379d7565cSPeter Wemm 		printf("TRUE}\n");
34479d7565cSPeter Wemm 	else
34579d7565cSPeter Wemm 		printf("FALSE}\n");
34679d7565cSPeter Wemm 	printf("\tPCI Vendor ID=0x%04x\n", hpetp->block_pcivendor);
34755da3c73SPeter Wemm 	printf("\tMinimal Tick=%d\n", hpetp->clock_tick);
34879d7565cSPeter Wemm 	printf(END_COMMENT);
34979d7565cSPeter Wemm }
35079d7565cSPeter Wemm 
35179d7565cSPeter Wemm static void
35255d7ff9eSNate Lawson acpi_handle_ecdt(struct ACPIsdt *sdp)
35355d7ff9eSNate Lawson {
35455d7ff9eSNate Lawson 	struct ECDTbody *ecdt;
35555d7ff9eSNate Lawson 
35655d7ff9eSNate Lawson 	printf(BEGIN_COMMENT);
35755d7ff9eSNate Lawson 	acpi_print_sdt(sdp);
35855d7ff9eSNate Lawson 	ecdt = (struct ECDTbody *) sdp->body;
35955d7ff9eSNate Lawson 	printf("\tEC_CONTROL=");
36055d7ff9eSNate Lawson 	acpi_print_gas(&ecdt->ec_control);
36155d7ff9eSNate Lawson 	printf("\n\tEC_DATA=");
36255d7ff9eSNate Lawson 	acpi_print_gas(&ecdt->ec_data);
36355d7ff9eSNate Lawson 	printf("\n\tUID=%#x, ", ecdt->uid);
36455d7ff9eSNate Lawson 	printf("GPE_BIT=%#x\n", ecdt->gpe_bit);
36555d7ff9eSNate Lawson 	printf("\tEC_ID=%s\n", ecdt->ec_id);
36655d7ff9eSNate Lawson 	printf(END_COMMENT);
36755d7ff9eSNate Lawson }
36855d7ff9eSNate Lawson 
36955d7ff9eSNate Lawson static void
370773b6454SNate Lawson acpi_print_sdt(struct ACPIsdt *sdp)
371c62f1cccSMitsuru IWASAKI {
372773b6454SNate Lawson 	printf("  ");
373e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->signature, 4);
374c62f1cccSMitsuru IWASAKI 	printf(": Length=%d, Revision=%d, Checksum=%d,\n",
375e1e9a4bfSMitsuru IWASAKI 	       sdp->len, sdp->rev, sdp->check);
376e1e9a4bfSMitsuru IWASAKI 	printf("\tOEMID=");
377e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->oemid, 6);
378e1e9a4bfSMitsuru IWASAKI 	printf(", OEM Table ID=");
379e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->oemtblid, 8);
380e1e9a4bfSMitsuru IWASAKI 	printf(", OEM Revision=0x%x,\n", sdp->oemrev);
381e1e9a4bfSMitsuru IWASAKI 	printf("\tCreator ID=");
382e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(sdp->creator, 4);
383e1e9a4bfSMitsuru IWASAKI 	printf(", Creator Revision=0x%x\n", sdp->crerev);
384e1e9a4bfSMitsuru IWASAKI }
385e1e9a4bfSMitsuru IWASAKI 
386945137d9SNate Lawson static void
387e1e9a4bfSMitsuru IWASAKI acpi_print_rsdt(struct ACPIsdt *rsdp)
388e1e9a4bfSMitsuru IWASAKI {
389e1e9a4bfSMitsuru IWASAKI 	int	i, entries;
390a74172abSNate Lawson 	u_long	addr;
391e1e9a4bfSMitsuru IWASAKI 
392773b6454SNate Lawson 	printf(BEGIN_COMMENT);
393773b6454SNate Lawson 	acpi_print_sdt(rsdp);
394a74172abSNate Lawson 	entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
395e1e9a4bfSMitsuru IWASAKI 	printf("\tEntries={ ");
396e1e9a4bfSMitsuru IWASAKI 	for (i = 0; i < entries; i++) {
397e1e9a4bfSMitsuru IWASAKI 		if (i > 0)
398e1e9a4bfSMitsuru IWASAKI 			printf(", ");
399a74172abSNate Lawson 		switch (addr_size) {
400a74172abSNate Lawson 		case 4:
401a74172abSNate Lawson 			addr = le32dec((char*)rsdp->body + i * addr_size);
402a74172abSNate Lawson 			break;
403a74172abSNate Lawson 		case 8:
404a74172abSNate Lawson 			addr = le64dec((char*)rsdp->body + i * addr_size);
405a74172abSNate Lawson 			break;
406a74172abSNate Lawson 		default:
407a74172abSNate Lawson 			addr = 0;
408a74172abSNate Lawson 		}
409a74172abSNate Lawson 		assert(addr != 0);
410a74172abSNate Lawson 		printf("0x%08lx", addr);
411e1e9a4bfSMitsuru IWASAKI 	}
412e1e9a4bfSMitsuru IWASAKI 	printf(" }\n");
413c62f1cccSMitsuru IWASAKI 	printf(END_COMMENT);
414e1e9a4bfSMitsuru IWASAKI }
415e1e9a4bfSMitsuru IWASAKI 
4168e6a8737SNate Lawson static const char *acpi_pm_profiles[] = {
4178e6a8737SNate Lawson 	"Unspecified", "Desktop", "Mobile", "Workstation",
4188e6a8737SNate Lawson 	"Enterprise Server", "SOHO Server", "Appliance PC"
4198e6a8737SNate Lawson };
4208e6a8737SNate Lawson 
421945137d9SNate Lawson static void
4228e6a8737SNate Lawson acpi_print_fadt(struct FADTbody *fadt)
423e1e9a4bfSMitsuru IWASAKI {
4248e6a8737SNate Lawson 	const char *pm;
425e1e9a4bfSMitsuru IWASAKI 	char	    sep;
426e1e9a4bfSMitsuru IWASAKI 
427c62f1cccSMitsuru IWASAKI 	printf(BEGIN_COMMENT);
4288e6a8737SNate Lawson 	printf("  FADT:\tFACS=0x%x, DSDT=0x%x\n", fadt->facs_ptr,
4298e6a8737SNate Lawson 	       fadt->dsdt_ptr);
4308e6a8737SNate Lawson 	printf("\tINT_MODEL=%s\n", fadt->int_model ? "APIC" : "PIC");
4318e6a8737SNate Lawson 	if (fadt->pm_profile >= sizeof(acpi_pm_profiles) / sizeof(char *))
4328e6a8737SNate Lawson 		pm = "Reserved";
4338e6a8737SNate Lawson 	else
4348e6a8737SNate Lawson 		pm = acpi_pm_profiles[fadt->pm_profile];
4358e6a8737SNate Lawson 	printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->pm_profile);
4368e6a8737SNate Lawson 	printf("\tSCI_INT=%d\n", fadt->sci_int);
4378e6a8737SNate Lawson 	printf("\tSMI_CMD=0x%x, ", fadt->smi_cmd);
4388e6a8737SNate Lawson 	printf("ACPI_ENABLE=0x%x, ", fadt->acpi_enable);
4398e6a8737SNate Lawson 	printf("ACPI_DISABLE=0x%x, ", fadt->acpi_disable);
4408e6a8737SNate Lawson 	printf("S4BIOS_REQ=0x%x\n", fadt->s4biosreq);
4418e6a8737SNate Lawson 	printf("\tPSTATE_CNT=0x%x\n", fadt->pstate_cnt);
442e1e9a4bfSMitsuru IWASAKI 	printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
4438e6a8737SNate Lawson 	       fadt->pm1a_evt_blk,
4448e6a8737SNate Lawson 	       fadt->pm1a_evt_blk + fadt->pm1_evt_len - 1);
4458e6a8737SNate Lawson 	if (fadt->pm1b_evt_blk != 0)
446e1e9a4bfSMitsuru IWASAKI 		printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
4478e6a8737SNate Lawson 		       fadt->pm1b_evt_blk,
4488e6a8737SNate Lawson 		       fadt->pm1b_evt_blk + fadt->pm1_evt_len - 1);
449e1e9a4bfSMitsuru IWASAKI 	printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
4508e6a8737SNate Lawson 	       fadt->pm1a_cnt_blk,
4518e6a8737SNate Lawson 	       fadt->pm1a_cnt_blk + fadt->pm1_cnt_len - 1);
4528e6a8737SNate Lawson 	if (fadt->pm1b_cnt_blk != 0)
453e1e9a4bfSMitsuru IWASAKI 		printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
4548e6a8737SNate Lawson 		       fadt->pm1b_cnt_blk,
4558e6a8737SNate Lawson 		       fadt->pm1b_cnt_blk + fadt->pm1_cnt_len - 1);
4568e6a8737SNate Lawson 	if (fadt->pm2_cnt_blk != 0)
457e1e9a4bfSMitsuru IWASAKI 		printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
4588e6a8737SNate Lawson 		       fadt->pm2_cnt_blk,
4598e6a8737SNate Lawson 		       fadt->pm2_cnt_blk + fadt->pm2_cnt_len - 1);
460c08c4e81SNate Lawson 	printf("\tPM_TMR_BLK=0x%x-0x%x\n",
4618e6a8737SNate Lawson 	       fadt->pm_tmr_blk,
4628e6a8737SNate Lawson 	       fadt->pm_tmr_blk + fadt->pm_tmr_len - 1);
4638e6a8737SNate Lawson 	if (fadt->gpe0_blk != 0)
4648e6a8737SNate Lawson 		printf("\tGPE0_BLK=0x%x-0x%x\n",
4658e6a8737SNate Lawson 		       fadt->gpe0_blk,
4668e6a8737SNate Lawson 		       fadt->gpe0_blk + fadt->gpe0_len - 1);
4678e6a8737SNate Lawson 	if (fadt->gpe1_blk != 0)
4688e6a8737SNate Lawson 		printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
4698e6a8737SNate Lawson 		       fadt->gpe1_blk,
4708e6a8737SNate Lawson 		       fadt->gpe1_blk + fadt->gpe1_len - 1,
4718e6a8737SNate Lawson 		       fadt->gpe1_base);
4728e6a8737SNate Lawson 	if (fadt->cst_cnt != 0)
4738e6a8737SNate Lawson 		printf("\tCST_CNT=0x%x\n", fadt->cst_cnt);
47451c1824fSNate Lawson 	printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
4758e6a8737SNate Lawson 	       fadt->p_lvl2_lat, fadt->p_lvl3_lat);
476e1e9a4bfSMitsuru IWASAKI 	printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
4778e6a8737SNate Lawson 	       fadt->flush_size, fadt->flush_stride);
478e1e9a4bfSMitsuru IWASAKI 	printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
4798e6a8737SNate Lawson 	       fadt->duty_off, fadt->duty_width);
480e1e9a4bfSMitsuru IWASAKI 	printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
4818e6a8737SNate Lawson 	       fadt->day_alrm, fadt->mon_alrm, fadt->century);
482e1e9a4bfSMitsuru IWASAKI 
4838e6a8737SNate Lawson #define PRINTFLAG(var, flag) do {			\
4848e6a8737SNate Lawson 	if ((var) & FADT_FLAG_## flag) {		\
4858e6a8737SNate Lawson 		printf("%c%s", sep, #flag); sep = ',';	\
486e1e9a4bfSMitsuru IWASAKI 	}						\
487e1e9a4bfSMitsuru IWASAKI } while (0)
488e1e9a4bfSMitsuru IWASAKI 
4898e6a8737SNate Lawson 	printf("\tIAPC_BOOT_ARCH=");
4908e6a8737SNate Lawson 	sep = '{';
4918e6a8737SNate Lawson 	PRINTFLAG(fadt->iapc_boot_arch, LEGACY_DEV);
4928e6a8737SNate Lawson 	PRINTFLAG(fadt->iapc_boot_arch, 8042);
49369a9febdSNate Lawson 	if (fadt->iapc_boot_arch != 0)
4944e36f5a1SNate Lawson 		printf("}");
4954e36f5a1SNate Lawson 	printf("\n");
4968e6a8737SNate Lawson 
4978e6a8737SNate Lawson 	printf("\tFlags=");
4988e6a8737SNate Lawson 	sep = '{';
4998e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, WBINVD);
5008e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, WBINVD_FLUSH);
5018e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, PROC_C1);
5028e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, P_LVL2_UP);
5038e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, PWR_BUTTON);
5048e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, SLP_BUTTON);
5058e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, FIX_RTC);
5068e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, RTC_S4);
5078e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, TMR_VAL_EXT);
5088e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, DCK_CAP);
5098e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, RESET_REG);
5108e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, SEALED_CASE);
5118e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, HEADLESS);
5128e6a8737SNate Lawson 	PRINTFLAG(fadt->flags, CPU_SW_SLP);
51369a9febdSNate Lawson 	if (fadt->flags != 0)
5148e6a8737SNate Lawson 		printf("}\n");
515e1e9a4bfSMitsuru IWASAKI 
516e1e9a4bfSMitsuru IWASAKI #undef PRINTFLAG
517e1e9a4bfSMitsuru IWASAKI 
5188e6a8737SNate Lawson 	if (fadt->flags & FADT_FLAG_RESET_REG) {
5198e6a8737SNate Lawson 		printf("\tRESET_REG=");
5208e6a8737SNate Lawson 		acpi_print_gas(&fadt->reset_reg);
5218e6a8737SNate Lawson 		printf(", RESET_VALUE=%#x\n", fadt->reset_value);
5228e6a8737SNate Lawson 	}
523c83f0f99SNate Lawson 	if (fadt_revision > 1) {
524773b6454SNate Lawson 		printf("\tX_FACS=0x%08lx, ", (u_long)fadt->x_facs_ptr);
525773b6454SNate Lawson 		printf("X_DSDT=0x%08lx\n", (u_long)fadt->x_dsdt_ptr);
526c08c4e81SNate Lawson 		printf("\tX_PM1a_EVT_BLK=");
527773b6454SNate Lawson 		acpi_print_gas(&fadt->x_pm1a_evt_blk);
528c08c4e81SNate Lawson 		if (fadt->x_pm1b_evt_blk.address != 0) {
529c08c4e81SNate Lawson 			printf("\n\tX_PM1b_EVT_BLK=");
530773b6454SNate Lawson 			acpi_print_gas(&fadt->x_pm1b_evt_blk);
531c08c4e81SNate Lawson 		}
532c08c4e81SNate Lawson 		printf("\n\tX_PM1a_CNT_BLK=");
533773b6454SNate Lawson 		acpi_print_gas(&fadt->x_pm1a_cnt_blk);
534c08c4e81SNate Lawson 		if (fadt->x_pm1b_cnt_blk.address != 0) {
535c08c4e81SNate Lawson 			printf("\n\tX_PM1b_CNT_BLK=");
536773b6454SNate Lawson 			acpi_print_gas(&fadt->x_pm1b_cnt_blk);
537c08c4e81SNate Lawson 		}
538c08c4e81SNate Lawson 		if (fadt->x_pm1b_cnt_blk.address != 0) {
539773b6454SNate Lawson 			printf("\n\tX_PM2_CNT_BLK=");
540773b6454SNate Lawson 			acpi_print_gas(&fadt->x_pm2_cnt_blk);
541c08c4e81SNate Lawson 		}
542773b6454SNate Lawson 		printf("\n\tX_PM_TMR_BLK=");
543773b6454SNate Lawson 		acpi_print_gas(&fadt->x_pm_tmr_blk);
544c08c4e81SNate Lawson 		if (fadt->x_gpe0_blk.address != 0) {
545773b6454SNate Lawson 			printf("\n\tX_GPE0_BLK=");
546773b6454SNate Lawson 			acpi_print_gas(&fadt->x_gpe0_blk);
547c08c4e81SNate Lawson 		}
548c08c4e81SNate Lawson 		if (fadt->x_gpe1_blk.address != 0) {
549773b6454SNate Lawson 			printf("\n\tX_GPE1_BLK=");
550773b6454SNate Lawson 			acpi_print_gas(&fadt->x_gpe1_blk);
551c08c4e81SNate Lawson 		}
552773b6454SNate Lawson 		printf("\n");
553773b6454SNate Lawson 	}
5548e6a8737SNate Lawson 
5558e6a8737SNate Lawson 	printf(END_COMMENT);
5568e6a8737SNate Lawson }
5578e6a8737SNate Lawson 
5588e6a8737SNate Lawson static void
5598e6a8737SNate Lawson acpi_print_facs(struct FACSbody *facs)
5608e6a8737SNate Lawson {
5618e6a8737SNate Lawson 	printf(BEGIN_COMMENT);
5628e6a8737SNate Lawson 	printf("  FACS:\tLength=%u, ", facs->len);
5638e6a8737SNate Lawson 	printf("HwSig=0x%08x, ", facs->hw_sig);
5648e6a8737SNate Lawson 	printf("Firm_Wake_Vec=0x%08x\n", facs->firm_wake_vec);
5658e6a8737SNate Lawson 
566773b6454SNate Lawson 	printf("\tGlobal_Lock=");
5678e6a8737SNate Lawson 	if (facs->global_lock != 0) {
5688e6a8737SNate Lawson 		if (facs->global_lock & FACS_FLAG_LOCK_PENDING)
5698e6a8737SNate Lawson 			printf("PENDING,");
5708e6a8737SNate Lawson 		if (facs->global_lock & FACS_FLAG_LOCK_OWNED)
5718e6a8737SNate Lawson 			printf("OWNED");
5728e6a8737SNate Lawson 	}
573773b6454SNate Lawson 	printf("\n");
5748e6a8737SNate Lawson 
575773b6454SNate Lawson 	printf("\tFlags=");
5768e6a8737SNate Lawson 	if (facs->flags & FACS_FLAG_S4BIOS_F)
5778e6a8737SNate Lawson 		printf("S4BIOS");
578773b6454SNate Lawson 	printf("\n");
5798e6a8737SNate Lawson 
5808e6a8737SNate Lawson 	if (facs->x_firm_wake_vec != 0) {
5818e6a8737SNate Lawson 		printf("\tX_Firm_Wake_Vec=%08lx\n",
5828e6a8737SNate Lawson 		       (u_long)facs->x_firm_wake_vec);
5838e6a8737SNate Lawson 	}
584773b6454SNate Lawson 	printf("\tVersion=%u\n", facs->version);
5858e6a8737SNate Lawson 
586c62f1cccSMitsuru IWASAKI 	printf(END_COMMENT);
587e1e9a4bfSMitsuru IWASAKI }
588e1e9a4bfSMitsuru IWASAKI 
589945137d9SNate Lawson static void
590e1e9a4bfSMitsuru IWASAKI acpi_print_dsdt(struct ACPIsdt *dsdp)
591e1e9a4bfSMitsuru IWASAKI {
592773b6454SNate Lawson 	printf(BEGIN_COMMENT);
593773b6454SNate Lawson 	acpi_print_sdt(dsdp);
594773b6454SNate Lawson 	printf(END_COMMENT);
595e1e9a4bfSMitsuru IWASAKI }
596e1e9a4bfSMitsuru IWASAKI 
597e1e9a4bfSMitsuru IWASAKI int
598e1e9a4bfSMitsuru IWASAKI acpi_checksum(void *p, size_t length)
599e1e9a4bfSMitsuru IWASAKI {
600e1e9a4bfSMitsuru IWASAKI 	u_int8_t	*bp;
601e1e9a4bfSMitsuru IWASAKI 	u_int8_t	sum;
602e1e9a4bfSMitsuru IWASAKI 
603e1e9a4bfSMitsuru IWASAKI 	bp = p;
604e1e9a4bfSMitsuru IWASAKI 	sum = 0;
605e1e9a4bfSMitsuru IWASAKI 	while (length--)
606e1e9a4bfSMitsuru IWASAKI 		sum += *bp++;
607e1e9a4bfSMitsuru IWASAKI 
608e1e9a4bfSMitsuru IWASAKI 	return (sum);
609e1e9a4bfSMitsuru IWASAKI }
610e1e9a4bfSMitsuru IWASAKI 
611945137d9SNate Lawson static struct ACPIsdt *
612e1e9a4bfSMitsuru IWASAKI acpi_map_sdt(vm_offset_t pa)
613e1e9a4bfSMitsuru IWASAKI {
614e1e9a4bfSMitsuru IWASAKI 	struct	ACPIsdt *sp;
615e1e9a4bfSMitsuru IWASAKI 
616e1e9a4bfSMitsuru IWASAKI 	sp = acpi_map_physical(pa, sizeof(struct ACPIsdt));
617e1e9a4bfSMitsuru IWASAKI 	sp = acpi_map_physical(pa, sp->len);
618e1e9a4bfSMitsuru IWASAKI 	return (sp);
619e1e9a4bfSMitsuru IWASAKI }
620e1e9a4bfSMitsuru IWASAKI 
621945137d9SNate Lawson static void
622e1e9a4bfSMitsuru IWASAKI acpi_print_rsd_ptr(struct ACPIrsdp *rp)
623e1e9a4bfSMitsuru IWASAKI {
624c62f1cccSMitsuru IWASAKI 	printf(BEGIN_COMMENT);
625a74172abSNate Lawson 	printf("  RSD PTR: OEM=");
626e1e9a4bfSMitsuru IWASAKI 	acpi_print_string(rp->oem, 6);
627773b6454SNate Lawson 	printf(", ACPI_Rev=%s (%d)\n", rp->revision < 2 ? "1.0x" : "2.0x",
628a74172abSNate Lawson 	       rp->revision);
629a74172abSNate Lawson 	if (rp->revision < 2) {
630a74172abSNate Lawson 		printf("\tRSDT=0x%08x, cksum=%u\n", rp->rsdt_addr, rp->sum);
631a74172abSNate Lawson 	} else {
632a74172abSNate Lawson 		printf("\tXSDT=0x%08lx, length=%u, cksum=%u\n",
633a74172abSNate Lawson 		    (u_long)rp->xsdt_addr, rp->length, rp->xsum);
634a74172abSNate Lawson 	}
635c62f1cccSMitsuru IWASAKI 	printf(END_COMMENT);
636e1e9a4bfSMitsuru IWASAKI }
637e1e9a4bfSMitsuru IWASAKI 
638945137d9SNate Lawson static void
639e1e9a4bfSMitsuru IWASAKI acpi_handle_rsdt(struct ACPIsdt *rsdp)
640e1e9a4bfSMitsuru IWASAKI {
641e1e9a4bfSMitsuru IWASAKI 	struct ACPIsdt *sdp;
642a74172abSNate Lawson 	vm_offset_t addr;
643a74172abSNate Lawson 	int entries, i;
644e1e9a4bfSMitsuru IWASAKI 
645e1e9a4bfSMitsuru IWASAKI 	acpi_print_rsdt(rsdp);
646a74172abSNate Lawson 	entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
647e1e9a4bfSMitsuru IWASAKI 	for (i = 0; i < entries; i++) {
648a74172abSNate Lawson 		switch (addr_size) {
649a74172abSNate Lawson 		case 4:
650a74172abSNate Lawson 			addr = le32dec((char*)rsdp->body + i * addr_size);
651a74172abSNate Lawson 			break;
652a74172abSNate Lawson 		case 8:
653a74172abSNate Lawson 			addr = le64dec((char*)rsdp->body + i * addr_size);
654a74172abSNate Lawson 			break;
655a74172abSNate Lawson 		default:
656a74172abSNate Lawson 			assert((addr = 0));
657a74172abSNate Lawson 		}
658a74172abSNate Lawson 
659a74172abSNate Lawson 		sdp = (struct ACPIsdt *)acpi_map_sdt(addr);
660e1e9a4bfSMitsuru IWASAKI 		if (acpi_checksum(sdp, sdp->len))
661945137d9SNate Lawson 			errx(1, "RSDT entry %d is corrupt", i);
662945137d9SNate Lawson 		if (!memcmp(sdp->signature, "FACP", 4))
6638e6a8737SNate Lawson 			acpi_handle_fadt((struct FADTbody *) sdp->body);
664945137d9SNate Lawson 		else if (!memcmp(sdp->signature, "APIC", 4))
6650a473124SJohn Baldwin 			acpi_handle_apic(sdp);
666945137d9SNate Lawson 		else if (!memcmp(sdp->signature, "HPET", 4))
66779d7565cSPeter Wemm 			acpi_handle_hpet(sdp);
66855d7ff9eSNate Lawson 		else if (!memcmp(sdp->signature, "ECDT", 4))
66955d7ff9eSNate Lawson 			acpi_handle_ecdt(sdp);
670773b6454SNate Lawson 		else {
671773b6454SNate Lawson 			printf(BEGIN_COMMENT);
672773b6454SNate Lawson 			acpi_print_sdt(sdp);
673773b6454SNate Lawson 			printf(END_COMMENT);
674773b6454SNate Lawson 		}
675e1e9a4bfSMitsuru IWASAKI 	}
676e1e9a4bfSMitsuru IWASAKI }
677c62f1cccSMitsuru IWASAKI 
678945137d9SNate Lawson struct ACPIsdt *
679945137d9SNate Lawson sdt_load_devmem()
680945137d9SNate Lawson {
681945137d9SNate Lawson 	struct	ACPIrsdp *rp;
682945137d9SNate Lawson 	struct	ACPIsdt *rsdp;
683945137d9SNate Lawson 
684945137d9SNate Lawson 	rp = acpi_find_rsd_ptr();
685945137d9SNate Lawson 	if (!rp)
686945137d9SNate Lawson 		errx(1, "Can't find ACPI information");
687945137d9SNate Lawson 
688945137d9SNate Lawson 	if (tflag)
689945137d9SNate Lawson 		acpi_print_rsd_ptr(rp);
690a74172abSNate Lawson 	if (rp->revision < 2) {
691945137d9SNate Lawson 		rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->rsdt_addr);
692945137d9SNate Lawson 		if (memcmp(rsdp->signature, "RSDT", 4) != 0 ||
693945137d9SNate Lawson 		    acpi_checksum(rsdp, rsdp->len) != 0)
694945137d9SNate Lawson 			errx(1, "RSDT is corrupted");
695a74172abSNate Lawson 		addr_size = sizeof(uint32_t);
696a74172abSNate Lawson 	} else {
697a74172abSNate Lawson 		rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->xsdt_addr);
698a74172abSNate Lawson 		if (memcmp(rsdp->signature, "XSDT", 4) != 0 ||
699a74172abSNate Lawson 		    acpi_checksum(rsdp, rsdp->len) != 0)
700a74172abSNate Lawson 			errx(1, "XSDT is corrupted");
701a74172abSNate Lawson 		addr_size = sizeof(uint64_t);
702a74172abSNate Lawson 	}
703945137d9SNate Lawson 	return (rsdp);
704945137d9SNate Lawson }
705c62f1cccSMitsuru IWASAKI 
706c62f1cccSMitsuru IWASAKI void
707945137d9SNate Lawson dsdt_save_file(char *outfile, struct ACPIsdt *dsdp)
708c62f1cccSMitsuru IWASAKI {
709945137d9SNate Lawson 	int	fd;
710945137d9SNate Lawson 	mode_t	mode;
711945137d9SNate Lawson 
712945137d9SNate Lawson 	assert(outfile != NULL);
713945137d9SNate Lawson 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
714945137d9SNate Lawson 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
715945137d9SNate Lawson 	if (fd == -1) {
716945137d9SNate Lawson 		perror("dsdt_save_file");
717945137d9SNate Lawson 		return;
718945137d9SNate Lawson 	}
719945137d9SNate Lawson 	write(fd, dsdp, SIZEOF_SDT_HDR);
720945137d9SNate Lawson 	write(fd, dsdp->body, dsdp->len - SIZEOF_SDT_HDR);
721945137d9SNate Lawson 	close(fd);
722c62f1cccSMitsuru IWASAKI }
723c62f1cccSMitsuru IWASAKI 
724945137d9SNate Lawson void
725945137d9SNate Lawson aml_disassemble(struct ACPIsdt *dsdp)
726c62f1cccSMitsuru IWASAKI {
727945137d9SNate Lawson 	char tmpstr[32], buf[256];
728945137d9SNate Lawson 	FILE *fp;
729945137d9SNate Lawson 	int fd, len;
730945137d9SNate Lawson 
731945137d9SNate Lawson 	strcpy(tmpstr, "/tmp/acpidump.XXXXXX");
732945137d9SNate Lawson 	fd = mkstemp(tmpstr);
733945137d9SNate Lawson 	if (fd < 0) {
734945137d9SNate Lawson 		perror("iasl tmp file");
735945137d9SNate Lawson 		return;
736c62f1cccSMitsuru IWASAKI 	}
737c62f1cccSMitsuru IWASAKI 
738945137d9SNate Lawson 	/* Dump DSDT to the temp file */
739945137d9SNate Lawson 	write(fd, dsdp, SIZEOF_SDT_HDR);
740945137d9SNate Lawson 	write(fd, dsdp->body, dsdp->len - SIZEOF_SDT_HDR);
741945137d9SNate Lawson 	close(fd);
742945137d9SNate Lawson 
743945137d9SNate Lawson 	/* Run iasl -d on the temp file */
744945137d9SNate Lawson 	if (fork() == 0) {
745945137d9SNate Lawson 		close(STDOUT_FILENO);
746945137d9SNate Lawson 		if (vflag == 0)
747945137d9SNate Lawson 			close(STDERR_FILENO);
748945137d9SNate Lawson 		execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, 0);
749945137d9SNate Lawson 		err(1, "exec");
750c62f1cccSMitsuru IWASAKI 	}
751c62f1cccSMitsuru IWASAKI 
752945137d9SNate Lawson 	wait(NULL);
753945137d9SNate Lawson 	unlink(tmpstr);
754945137d9SNate Lawson 
755945137d9SNate Lawson 	/* Dump iasl's output to stdout */
756945137d9SNate Lawson 	fp = fopen("acpidump.dsl", "r");
757945137d9SNate Lawson 	unlink("acpidump.dsl");
758945137d9SNate Lawson 	if (fp == NULL) {
759945137d9SNate Lawson 		perror("iasl tmp file (read)");
760945137d9SNate Lawson 		return;
761945137d9SNate Lawson 	}
762945137d9SNate Lawson 	while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
763945137d9SNate Lawson 		fwrite(buf, 1, len, stdout);
764945137d9SNate Lawson 	fclose(fp);
765c62f1cccSMitsuru IWASAKI }
766c62f1cccSMitsuru IWASAKI 
767945137d9SNate Lawson void
768945137d9SNate Lawson sdt_print_all(struct ACPIsdt *rsdp)
769c62f1cccSMitsuru IWASAKI {
770945137d9SNate Lawson 	acpi_handle_rsdt(rsdp);
771c62f1cccSMitsuru IWASAKI }
772c62f1cccSMitsuru IWASAKI 
773945137d9SNate Lawson /* Fetch a table matching the given signature via the RSDT */
774945137d9SNate Lawson struct ACPIsdt *
775945137d9SNate Lawson sdt_from_rsdt(struct ACPIsdt *rsdt, const char *sig)
776c62f1cccSMitsuru IWASAKI {
777945137d9SNate Lawson 	struct ACPIsdt *sdt;
778a74172abSNate Lawson 	vm_offset_t addr;
779a74172abSNate Lawson 	int entries, i;
780945137d9SNate Lawson 
781a74172abSNate Lawson 	entries = (rsdt->len - SIZEOF_SDT_HDR) / addr_size;
782945137d9SNate Lawson 	for (i = 0; i < entries; i++) {
783a74172abSNate Lawson 		switch (addr_size) {
784a74172abSNate Lawson 		case 4:
785a74172abSNate Lawson 			addr = le32dec((char*)rsdt->body + i * addr_size);
786a74172abSNate Lawson 			break;
787a74172abSNate Lawson 		case 8:
788a74172abSNate Lawson 			addr = le64dec((char*)rsdt->body + i * addr_size);
789a74172abSNate Lawson 			break;
790a74172abSNate Lawson 		default:
791a74172abSNate Lawson 			assert((addr = 0));
792a74172abSNate Lawson 		}
793a74172abSNate Lawson 		sdt = (struct ACPIsdt *)acpi_map_sdt(addr);
794a74172abSNate Lawson 		if (memcmp(sdt->signature, sig, strlen(sig)))
795a74172abSNate Lawson 			continue;
796945137d9SNate Lawson 		if (acpi_checksum(sdt, sdt->len))
797945137d9SNate Lawson 			errx(1, "RSDT entry %d is corrupt", i);
798945137d9SNate Lawson 		return (sdt);
799c62f1cccSMitsuru IWASAKI 	}
800c62f1cccSMitsuru IWASAKI 
801945137d9SNate Lawson 	return (NULL);
802c62f1cccSMitsuru IWASAKI }
803c62f1cccSMitsuru IWASAKI 
804945137d9SNate Lawson struct ACPIsdt *
8058e6a8737SNate Lawson dsdt_from_fadt(struct FADTbody *fadt)
806c62f1cccSMitsuru IWASAKI {
807945137d9SNate Lawson 	struct	ACPIsdt *sdt;
808c62f1cccSMitsuru IWASAKI 
809c83f0f99SNate Lawson 	/* Use the DSDT address if it is version 1, otherwise use X_DSDT. */
810c83f0f99SNate Lawson 	if (fadt_revision == 1)
8118e6a8737SNate Lawson 		sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
8122e71eb12SNate Lawson 	else
8132e71eb12SNate Lawson 		sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
814945137d9SNate Lawson 	if (acpi_checksum(sdt, sdt->len))
815945137d9SNate Lawson 		errx(1, "DSDT is corrupt\n");
816945137d9SNate Lawson 	return (sdt);
817c62f1cccSMitsuru IWASAKI }
818