2 * Copyright (c) 1998 Doug Rabson
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/endian.h>
34 #include <sys/queue.h>
36 #include <sys/sysctl.h>
48 #include "sysinstall.h"
50 static void acpi_handle_apic(struct ACPIsdt *sdp);
51 static struct ACPIsdt *acpi_map_sdt(vm_offset_t pa);
52 static void acpi_handle_rsdt(struct ACPIsdt *rsdp);
53 static struct acpi_user_mapping *acpi_user_find_mapping(vm_offset_t, size_t);
54 static void * acpi_map_physical(vm_offset_t, size_t);
56 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
62 acpi_handle_apic(struct ACPIsdt *sdp)
64 struct MADTbody *madtp;
66 struct MADT_local_apic *apic;
67 struct MADT_local_sapic *sapic;
69 madtp = (struct MADTbody *) sdp->body;
70 mp = (struct MADT_APIC *)madtp->body;
71 while (((uintptr_t)mp) - ((uintptr_t)sdp) < sdp->len) {
73 case ACPI_MADT_APIC_TYPE_LOCAL_APIC:
74 apic = &mp->body.local_apic;
75 msgDebug("MADT: Found CPU APIC ID %d %s\n",
77 apic->flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED ?
78 "enabled" : "disabled");
79 if (apic->flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED)
82 case ACPI_MADT_APIC_TYPE_LOCAL_SAPIC:
83 sapic = &mp->body.local_sapic;
84 msgDebug("MADT: Found CPU SAPIC ID %d %s\n",
86 sapic->flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED ?
87 "enabled" : "disabled");
88 /* XXX is enable flag the same? */
89 if (sapic->flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED)
95 mp = (struct MADT_APIC *) ((char *)mp + mp->len);
100 acpi_checksum(void *p, size_t length)
113 static struct ACPIsdt *
114 acpi_map_sdt(vm_offset_t pa)
118 sp = acpi_map_physical(pa, sizeof(struct ACPIsdt));
120 sp = acpi_map_physical(pa, sp->len);
125 acpi_handle_rsdt(struct ACPIsdt *rsdp)
131 entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
132 for (i = 0; i < entries; i++) {
135 addr = le32dec((char*)rsdp->body + i * addr_size);
138 addr = le64dec((char*)rsdp->body + i * addr_size);
144 sdp = (struct ACPIsdt *)acpi_map_sdt(addr);
146 msgDebug("%s: unable to map sdt\n", __func__);
149 if (acpi_checksum(sdp, sdp->len)) {
151 msgDebug("RSDT entry %d (sig %.4s) has bad checksum\n",
156 if (!memcmp(sdp->signature, "APIC", 4))
157 acpi_handle_apic(sdp);
161 static char machdep_acpi_root[] = "machdep.acpi_root";
162 static int acpi_mem_fd = -1;
164 struct acpi_user_mapping {
165 LIST_ENTRY(acpi_user_mapping) link;
171 LIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist;
177 if (acpi_mem_fd == -1) {
178 acpi_mem_fd = open(_PATH_MEM, O_RDONLY);
179 if (acpi_mem_fd == -1) {
180 msgDebug("%s: error opening %s: %s\n", __func__,
181 _PATH_MEM, strerror(errno));
189 static struct acpi_user_mapping *
190 acpi_user_find_mapping(vm_offset_t pa, size_t size)
192 struct acpi_user_mapping *map;
194 /* First search for an existing mapping */
195 for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) {
196 if (map->pa <= pa && map->size >= pa + size - map->pa)
200 /* Then create a new one */
201 size = round_page(pa + size) - trunc_page(pa);
203 map = malloc(sizeof(struct acpi_user_mapping));
205 msgDebug("%s: out of memory: %s\n", __func__, strerror(errno));
209 map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa);
211 if ((intptr_t) map->va == -1) {
212 msgDebug("%s: can't mmap address %lu size %lu: %s\n", __func__,
213 (unsigned long) pa, (unsigned long) size, strerror(errno));
217 LIST_INSERT_HEAD(&maplist, map, link);
223 acpi_map_physical(vm_offset_t pa, size_t size)
225 struct acpi_user_mapping *map;
227 map = acpi_user_find_mapping(pa, size);
228 return (map == NULL ? NULL : map->va + (pa - map->pa));
231 static struct ACPIrsdp *
232 acpi_get_rsdp(u_long addr)
234 struct ACPIrsdp rsdp;
237 /* Read in the table signature and check it. */
238 pread(acpi_mem_fd, &rsdp, 8, addr);
239 if (memcmp(rsdp.signature, "RSD PTR ", 8))
242 /* Read the entire table. */
243 pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr);
245 /* Run the checksum only over the version 1 header. */
246 if (acpi_checksum(&rsdp, 20))
249 /* If the revision is 0, assume a version 1 length. */
250 if (rsdp.revision == 0)
255 /* XXX Should handle ACPI 2.0 RSDP extended checksum here. */
257 return (acpi_map_physical(addr, len));
261 devstate(devinfo_state_t state)
265 return "not-present";
273 return "unknown-state";
278 acpi0_check(struct devinfo_dev *dd, void *arg)
280 printf("%s: %s %s\n", __func__, dd->dd_name, devstate(dd->dd_state));
281 /* NB: device must be present AND attached */
282 if (strcmp(dd->dd_name, "acpi0") == 0)
283 return (dd->dd_state == DIS_ATTACHED ||
284 dd->dd_state == DIS_BUSY);
285 return devinfo_foreach_device_child(dd, acpi0_check, arg);
291 struct devinfo_dev *root;
296 root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE);
298 found = devinfo_foreach_device_child(root, acpi0_check, NULL);
307 struct ACPIsdt *rsdp;
311 if (!acpi0_present()) {
312 msgDebug("%s: no acpi0 device located\n", __func__);
316 if (!acpi_user_init())
319 /* Attempt to use sysctl to find RSD PTR record. */
321 if (sysctlbyname(machdep_acpi_root, &addr, &len, NULL, 0) != 0) {
322 msgDebug("%s: cannot find ACPI information\n", __func__);
325 rp = acpi_get_rsdp(addr);
327 msgDebug("%s: cannot find ACPI information: "
328 "sysctl %s does not point to RSDP\n", __func__,
332 if (rp->revision < 2) {
333 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->rsdt_addr);
336 if (memcmp(rsdp->signature, "RSDT", 4) != 0 ||
337 acpi_checksum(rsdp, rsdp->len) != 0) {
338 msgDebug("%s: RSDT is corrupted\n", __func__);
341 addr_size = sizeof(uint32_t);
343 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->xsdt_addr);
346 if (memcmp(rsdp->signature, "XSDT", 4) != 0 ||
347 acpi_checksum(rsdp, rsdp->len) != 0) {
348 msgDebug("%s: XSDT is corrupted\n", __func__);
351 addr_size = sizeof(uint64_t);
354 acpi_handle_rsdt(rsdp);
355 return (ncpu == 0 ? 1 : ncpu);