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