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 * $Id: acpi_user.c,v 1.5 2000/08/09 14:47:52 iwasaki Exp $ 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/mman.h> 33 #include <sys/queue.h> 34 #include <sys/stat.h> 35 36 #include <err.h> 37 #include <fcntl.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 #include "acpidump.h" 43 44 static char machdep_acpi_root[] = "machdep.acpi_root"; 45 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() 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 /* 97 * Public interfaces 98 */ 99 100 struct ACPIrsdp * 101 acpi_find_rsd_ptr() 102 { 103 struct ACPIrsdp rsdp; 104 u_long addr; 105 int len; 106 107 acpi_user_init(); 108 109 len = sizeof(addr); 110 if (sysctlbyname(machdep_acpi_root, &addr, &len, NULL, 0) == 0) { 111 pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr); 112 if (memcmp(rsdp.signature, "RSD PTR ", 8)) 113 errx(1, "sysctl %s does not point to RSDP\n", 114 machdep_acpi_root); 115 len = 20; /* size of ACPI 1.0 table */ 116 if (acpi_checksum(&rsdp, len)) 117 warnx("RSDP has invalid checksum\n"); 118 if (rsdp.revision > 0) 119 len = rsdp.length; 120 return (acpi_map_physical(addr, len)); 121 } 122 123 #if !defined(__ia64__) 124 for (addr = 0UL; addr < 1024UL * 1024UL; addr += 16UL) { 125 pread(acpi_mem_fd, &rsdp, 8, addr); 126 if (memcmp(rsdp.signature, "RSD PTR ", 8)) 127 continue; 128 /* Read the entire table. */ 129 pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr); 130 len = 20; /* size of ACPI 1.0 table */ 131 if (acpi_checksum(&rsdp, len)) 132 continue; 133 if (rsdp.revision > 0) 134 len = rsdp.length; 135 return (acpi_map_physical(addr, len)); 136 } 137 #endif 138 139 return (0); 140 } 141 142 void * 143 acpi_map_physical(vm_offset_t pa, size_t size) 144 { 145 struct acpi_user_mapping *map; 146 147 map = acpi_user_find_mapping(pa, size); 148 return (map->va + (pa - map->pa)); 149 } 150 151 void 152 acpi_load_dsdt(char *dumpfile, u_int8_t **dpp, u_int8_t **endp) 153 { 154 u_int8_t *dp; 155 u_int8_t *end; 156 struct stat sb; 157 158 if ((acpi_mem_fd = open(dumpfile, O_RDONLY)) == -1) { 159 errx(1, "opening %s\n", dumpfile); 160 } 161 162 LIST_INIT(&maplist); 163 164 if (fstat(acpi_mem_fd, &sb) == -1) { 165 errx(1, "fstat %s\n", dumpfile); 166 } 167 168 dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0); 169 if (dp == NULL) { 170 errx(1, "mmap %s\n", dumpfile); 171 } 172 173 if (strncmp(dp, "DSDT", 4) == 0) { 174 memcpy(&dsdt_header, dp, SIZEOF_SDT_HDR); 175 dp += SIZEOF_SDT_HDR; 176 sb.st_size -= SIZEOF_SDT_HDR; 177 } 178 179 end = (u_int8_t *) dp + sb.st_size; 180 *dpp = dp; 181 *endp = end; 182 } 183