d154849d41f908b4eed7affc455506269d3ddf44
[dragonfly.git] / usr.sbin / acpi / acpidump / acpi_user.c
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: src/usr.sbin/acpi/acpidump/acpi_user.c,v 1.12 2004/05/28 07:25:23 njl Exp $
28  *      $DragonFly: src/usr.sbin/acpi/acpidump/acpi_user.c,v 1.1 2004/07/05 00:22:43 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/mman.h>
33 #include <sys/queue.h>
34 #include <sys/stat.h>
35 #include <sys/sysctl.h>
36
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "acpidump.h"
44
45 static char     machdep_acpi_root[] = "machdep.acpi_root";
46 static int      acpi_mem_fd = -1;
47
48 struct acpi_user_mapping {
49         LIST_ENTRY(acpi_user_mapping) link;
50         vm_offset_t     pa;
51         caddr_t         va;
52         size_t          size;
53 };
54
55 LIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist;
56
57 static void
58 acpi_user_init(void)
59 {
60
61         if (acpi_mem_fd == -1) {
62                 acpi_mem_fd = open("/dev/mem", O_RDONLY);
63                 if (acpi_mem_fd == -1)
64                         err(1, "opening /dev/mem");
65                 LIST_INIT(&maplist);
66         }
67 }
68
69 static struct acpi_user_mapping *
70 acpi_user_find_mapping(vm_offset_t pa, size_t size)
71 {
72         struct  acpi_user_mapping *map;
73
74         /* First search for an existing mapping */
75         for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) {
76                 if (map->pa <= pa && map->size >= pa + size - map->pa)
77                         return (map);
78         }
79
80         /* Then create a new one */
81         size = round_page(pa + size) - trunc_page(pa);
82         pa = trunc_page(pa);
83         map = malloc(sizeof(struct acpi_user_mapping));
84         if (!map)
85                 errx(1, "out of memory");
86         map->pa = pa;
87         map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa);
88         map->size = size;
89         if ((intptr_t) map->va == -1)
90                 err(1, "can't map address");
91         LIST_INSERT_HEAD(&maplist, map, link);
92
93         return (map);
94 }
95
96 static struct ACPIrsdp *
97 acpi_get_rsdp(u_long addr)
98 {
99         struct ACPIrsdp rsdp;
100         size_t len;
101
102         /* Read in the table signature and check it. */
103         pread(acpi_mem_fd, &rsdp, 8, addr);
104         if (memcmp(rsdp.signature, "RSD PTR ", 8))
105                 return (NULL);
106
107         /* Read the entire table. */
108         pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr);
109
110         /* Run the checksum only over the version 1 header. */
111         if (acpi_checksum(&rsdp, 20))
112                 return (NULL);
113
114         /* If the revision is 0, assume a version 1 length. */
115         if (rsdp.revision == 0)
116                 len = 20;
117         else
118                 len = rsdp.length;
119
120         /* XXX Should handle ACPI 2.0 RSDP extended checksum here. */
121
122         return (acpi_map_physical(addr, len));
123 }
124
125 static struct ACPIrsdp *
126 acpi_scan_rsd_ptr(void)
127 {
128 #if defined(__i386__)
129         struct ACPIrsdp *rsdp;
130         u_long          addr, end;
131
132         /*
133          * On ia32, scan physical memory for the RSD PTR if above failed.
134          * According to section 5.2.2 of the ACPI spec, we only consider
135          * two regions for the base address:
136          * 1. EBDA (1 KB area addressed by the 16 bit pointer at 0x40E
137          * 2. High memory (0xE0000 - 0xFFFFF)
138          */
139         addr = RSDP_EBDA_PTR;
140         pread(acpi_mem_fd, &addr, sizeof(uint16_t), addr);
141         addr <<= 4;
142         end = addr + RSDP_EBDA_SIZE;
143         for (; addr < end; addr += 16)
144                 if ((rsdp = acpi_get_rsdp(addr)) != NULL)
145                         return (rsdp);
146         addr = RSDP_HI_START;
147         end = addr + RSDP_HI_SIZE;
148         for (; addr < end; addr += 16)
149                 if ((rsdp = acpi_get_rsdp(addr)) != NULL)
150                         return (rsdp);
151 #endif /* __i386__ */
152         return (NULL);
153 }
154
155 /*
156  * Public interfaces
157  */
158 struct ACPIrsdp *
159 acpi_find_rsd_ptr(void)
160 {
161         struct ACPIrsdp *rsdp;
162         u_long          addr;
163         size_t          len;
164
165         acpi_user_init();
166
167         /* Attempt to use sysctl to find RSD PTR record. */
168         len = sizeof(addr);
169         if (sysctlbyname(machdep_acpi_root, &addr, &len, NULL, 0) == 0) {       
170                 if ((rsdp = acpi_get_rsdp(addr)) != NULL)
171                         return (rsdp);
172                 else
173                         warnx("sysctl %s does not point to RSDP",
174                             machdep_acpi_root);
175         }
176
177         return (acpi_scan_rsd_ptr());
178 }
179
180 void *
181 acpi_map_physical(vm_offset_t pa, size_t size)
182 {
183         struct  acpi_user_mapping *map;
184
185         map = acpi_user_find_mapping(pa, size);
186         return (map->va + (pa - map->pa));
187 }
188
189 struct ACPIsdt *
190 dsdt_load_file(char *infile)
191 {
192         struct ACPIsdt  *sdt;
193         uint8_t         *dp;
194         struct stat      sb;
195
196         if ((acpi_mem_fd = open(infile, O_RDONLY)) == -1)
197                 errx(1, "opening %s", infile);
198
199         LIST_INIT(&maplist);
200
201         if (fstat(acpi_mem_fd, &sb) == -1)
202                 errx(1, "fstat %s", infile);
203
204         dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0);
205         if (dp == NULL)
206                 errx(1, "mmap %s", infile);
207
208         sdt = (struct ACPIsdt *)dp;
209         if (strncmp(dp, "DSDT", 4) != 0 || acpi_checksum(sdt, sdt->len) != 0)
210                 return (NULL);
211
212         return (sdt);
213 }