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