acpi_user.c revision 1.1 1 1.1 christos /* $NetBSD: acpi_user.c,v 1.1 2007/01/14 04:36:13 christos 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.1 christos * Id: acpi_user.c,v 1.5 2000/08/09 14:47:52 iwasaki Exp
30 1.1 christos * $FreeBSD: src/usr.sbin/acpi/acpidump/acpi_user.c,v 1.4 2001/10/22 17:25:25 iwasaki Exp $
31 1.1 christos */
32 1.1 christos #include <sys/cdefs.h>
33 1.1 christos __RCSID("$NetBSD: acpi_user.c,v 1.1 2007/01/14 04:36:13 christos 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.1 christos
40 1.1 christos #include <err.h>
41 1.1 christos #include <fcntl.h>
42 1.1 christos #include <stdlib.h>
43 1.1 christos #include <string.h>
44 1.1 christos #include <unistd.h>
45 1.1 christos
46 1.1 christos #include <acpi_common.h>
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.1 christos err(1, "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.1 christos 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.1 christos errx(1, "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.1 christos err(1, "can't map address");
94 1.1 christos LIST_INSERT_HEAD(&maplist, map, link);
95 1.1 christos
96 1.1 christos return (map);
97 1.1 christos }
98 1.1 christos
99 1.1 christos /*
100 1.1 christos * Public interfaces
101 1.1 christos */
102 1.1 christos
103 1.1 christos struct ACPIrsdp *
104 1.1 christos acpi_find_rsd_ptr()
105 1.1 christos {
106 1.1 christos int i;
107 1.1 christos u_int8_t buf[sizeof(struct ACPIrsdp)];
108 1.1 christos
109 1.1 christos acpi_user_init();
110 1.1 christos for (i = 0; i < 1024 * 1024; i += 16) {
111 1.1 christos read(acpi_mem_fd, buf, 16);
112 1.1 christos if (!memcmp(buf, "RSD PTR ", 8)) {
113 1.1 christos /* Read the rest of the structure */
114 1.1 christos read(acpi_mem_fd, buf + 16, sizeof(struct ACPIrsdp) - 16);
115 1.1 christos
116 1.1 christos /* Verify checksum before accepting it. */
117 1.1 christos if (acpi_checksum(buf, sizeof(struct ACPIrsdp)))
118 1.1 christos continue;
119 1.1 christos return (acpi_map_physical(i, sizeof(struct ACPIrsdp)));
120 1.1 christos }
121 1.1 christos }
122 1.1 christos
123 1.1 christos return (0);
124 1.1 christos }
125 1.1 christos
126 1.1 christos void *
127 1.1 christos acpi_map_physical(vm_offset_t pa, size_t size)
128 1.1 christos {
129 1.1 christos struct acpi_user_mapping *map;
130 1.1 christos
131 1.1 christos map = acpi_user_find_mapping(pa, size);
132 1.1 christos return (map->va + (pa - map->pa));
133 1.1 christos }
134 1.1 christos
135 1.1 christos void
136 1.1 christos acpi_load_dsdt(char *dumpfile, u_int8_t **dpp, u_int8_t **endp)
137 1.1 christos {
138 1.1 christos u_int8_t *dp;
139 1.1 christos u_int8_t *end;
140 1.1 christos struct stat sb;
141 1.1 christos
142 1.1 christos if ((acpi_mem_fd = open(dumpfile, O_RDONLY)) == -1) {
143 1.1 christos errx(1, "opening %s\n", dumpfile);
144 1.1 christos }
145 1.1 christos
146 1.1 christos LIST_INIT(&maplist);
147 1.1 christos
148 1.1 christos if (fstat(acpi_mem_fd, &sb) == -1) {
149 1.1 christos errx(1, "fstat %s\n", dumpfile);
150 1.1 christos }
151 1.1 christos
152 1.1 christos dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0);
153 1.1 christos if (dp == NULL) {
154 1.1 christos errx(1, "mmap %s\n", dumpfile);
155 1.1 christos }
156 1.1 christos
157 1.1 christos if (strncmp((const char *)dp, "DSDT", 4) == 0) {
158 1.1 christos memcpy(&dsdt_header, dp, SIZEOF_SDT_HDR);
159 1.1 christos dp += SIZEOF_SDT_HDR;
160 1.1 christos sb.st_size -= SIZEOF_SDT_HDR;
161 1.1 christos }
162 1.1 christos
163 1.1 christos end = (u_int8_t *) dp + sb.st_size;
164 1.1 christos *dpp = dp;
165 1.1 christos *endp = end;
166 1.1 christos }
167