xref: /freebsd/usr.sbin/acpi/acpidump/acpi_user.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*-
2  * Copyright (c) 1999 Doug Rabson
3  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/mman.h>
32 #include <sys/queue.h>
33 #include <sys/stat.h>
34 #include <sys/sysctl.h>
35 
36 #include <err.h>
37 #include <fcntl.h>
38 #include <kenv.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "acpidump.h"
44 
45 static char	hint_acpi_0_rsdp[] = "hint.acpi.0.rsdp";
46 static char	machdep_acpi_root[] = "machdep.acpi_root";
47 static int      acpi_mem_fd = -1;
48 
49 struct acpi_user_mapping {
50 	LIST_ENTRY(acpi_user_mapping) link;
51 	vm_offset_t     pa;
52 	caddr_t         va;
53 	size_t          size;
54 };
55 
56 static LIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist;
57 
58 static void
59 acpi_user_init(void)
60 {
61 
62 	if (acpi_mem_fd == -1) {
63 		acpi_mem_fd = open("/dev/mem", O_RDONLY);
64 		if (acpi_mem_fd == -1)
65 			err(1, "opening /dev/mem");
66 		LIST_INIT(&maplist);
67 	}
68 }
69 
70 static struct acpi_user_mapping *
71 acpi_user_find_mapping(vm_offset_t pa, size_t size)
72 {
73 	struct	acpi_user_mapping *map;
74 
75 	/* First search for an existing mapping */
76 	for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) {
77 		if (map->pa <= pa && map->size >= pa + size - map->pa)
78 			return (map);
79 	}
80 
81 	/* Then create a new one */
82 	size = round_page(pa + size) - trunc_page(pa);
83 	pa = trunc_page(pa);
84 	map = malloc(sizeof(struct acpi_user_mapping));
85 	if (!map)
86 		errx(1, "out of memory");
87 	map->pa = pa;
88 	map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa);
89 	map->size = size;
90 	if ((intptr_t) map->va == -1)
91 		err(1, "can't map address");
92 	LIST_INSERT_HEAD(&maplist, map, link);
93 
94 	return (map);
95 }
96 
97 static ACPI_TABLE_RSDP *
98 acpi_get_rsdp(u_long addr)
99 {
100 	ACPI_TABLE_RSDP rsdp;
101 	size_t len;
102 
103 	/* Read in the table signature and check it. */
104 	pread(acpi_mem_fd, &rsdp, 8, addr);
105 	if (memcmp(rsdp.Signature, "RSD PTR ", 8))
106 		return (NULL);
107 
108 	/* Read the entire table. */
109 	pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr);
110 
111 	/* Check the standard checksum. */
112 	if (acpi_checksum(&rsdp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
113 		return (NULL);
114 
115 	/* Check extended checksum if table version >= 2. */
116 	if (rsdp.Revision >= 2 &&
117 	    acpi_checksum(&rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
118 		return (NULL);
119 
120 	/* If the revision is 0, assume a version 1 length. */
121 	if (rsdp.Revision == 0)
122 		len = sizeof(ACPI_RSDP_COMMON);
123 	else
124 		len = rsdp.Length;
125 
126 	return (acpi_map_physical(addr, len));
127 }
128 
129 static ACPI_TABLE_RSDP *
130 acpi_scan_rsd_ptr(void)
131 {
132 #if defined(__amd64__) || defined(__i386__)
133 	ACPI_TABLE_RSDP *rsdp;
134 	u_long		addr, end;
135 
136 	/*
137 	 * On ia32, scan physical memory for the RSD PTR if above failed.
138 	 * According to section 5.2.2 of the ACPI spec, we only consider
139 	 * two regions for the base address:
140 	 * 1. EBDA (1 KB area addressed by the 16 bit pointer at 0x40E
141 	 * 2. High memory (0xE0000 - 0xFFFFF)
142 	 */
143 	addr = ACPI_EBDA_PTR_LOCATION;
144 	pread(acpi_mem_fd, &addr, sizeof(uint16_t), addr);
145 	addr <<= 4;
146 	end = addr + ACPI_EBDA_WINDOW_SIZE;
147 	for (; addr < end; addr += 16)
148 		if ((rsdp = acpi_get_rsdp(addr)) != NULL)
149 			return (rsdp);
150 	addr = ACPI_HI_RSDP_WINDOW_BASE;
151 	end = addr + ACPI_HI_RSDP_WINDOW_SIZE;
152 	for (; addr < end; addr += 16)
153 		if ((rsdp = acpi_get_rsdp(addr)) != NULL)
154 			return (rsdp);
155 #endif /* __amd64__ || __i386__ */
156 	return (NULL);
157 }
158 
159 /*
160  * Public interfaces
161  */
162 ACPI_TABLE_RSDP *
163 acpi_find_rsd_ptr(void)
164 {
165 	ACPI_TABLE_RSDP *rsdp;
166 	char		buf[20];
167 	u_long		addr;
168 	size_t		len;
169 
170 	acpi_user_init();
171 
172 	addr = 0;
173 
174 	/* Attempt to use kenv or sysctl to find RSD PTR record. */
175 	if (kenv(KENV_GET, hint_acpi_0_rsdp, buf, 20) > 0)
176 		addr = strtoul(buf, NULL, 0);
177 	if (addr == 0) {
178 		len = sizeof(addr);
179 		if (sysctlbyname(machdep_acpi_root, &addr, &len, NULL, 0) != 0)
180 			addr = 0;
181 	}
182 	if (addr != 0 && (rsdp = acpi_get_rsdp(addr)) != NULL)
183 		return (rsdp);
184 
185 	return (acpi_scan_rsd_ptr());
186 }
187 
188 void *
189 acpi_map_physical(vm_offset_t pa, size_t size)
190 {
191 	struct	acpi_user_mapping *map;
192 
193 	map = acpi_user_find_mapping(pa, size);
194 	return (map->va + (pa - map->pa));
195 }
196 
197 ACPI_TABLE_HEADER *
198 dsdt_load_file(char *infile)
199 {
200 	ACPI_TABLE_HEADER *sdt;
201 	uint8_t		*dp;
202 	struct stat	 sb;
203 
204 	if ((acpi_mem_fd = open(infile, O_RDONLY)) == -1)
205 		errx(1, "opening %s", infile);
206 
207 	LIST_INIT(&maplist);
208 
209 	if (fstat(acpi_mem_fd, &sb) == -1)
210 		errx(1, "fstat %s", infile);
211 
212 	dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0);
213 	if (dp == NULL)
214 		errx(1, "mmap %s", infile);
215 
216 	sdt = (ACPI_TABLE_HEADER *)dp;
217 	if (strncmp(dp, ACPI_SIG_DSDT, 4) != 0 ||
218 	    acpi_checksum(sdt, sdt->Length) != 0)
219 		return (NULL);
220 
221 	return (sdt);
222 }
223