1 /*-
2 * Copyright (c) 2001 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 #include <stand.h>
29 #include <machine/stdarg.h>
30 #include <bootstrap.h>
31 #include <btxv86.h>
32 #include "libi386.h"
33
34 #include "platform/acfreebsd.h"
35 #include "acconfig.h"
36 #define ACPI_SYSTEM_XFACE
37 #include "actypes.h"
38 #include "actbl.h"
39
40 /*
41 * Detect ACPI and export information about the ACPI BIOS into the
42 * environment.
43 */
44
45 static ACPI_TABLE_RSDP *biosacpi_find_rsdp(void);
46 static ACPI_TABLE_RSDP *biosacpi_search_rsdp(char *base, int length);
47
48 #define RSDP_CHECKSUM_LENGTH 20
49
50 void
biosacpi_detect(void)51 biosacpi_detect(void)
52 {
53 ACPI_TABLE_RSDP *rsdp;
54 char buf[24];
55 int revision;
56
57 /* locate and validate the RSDP */
58 if ((rsdp = biosacpi_find_rsdp()) == NULL)
59 return;
60
61 /*
62 * Report the RSDP to the kernel. While this can be found with
63 * a BIOS boot, the RSDP may be elsewhere when booted from UEFI.
64 */
65 sprintf(buf, "0x%08x", VTOP(rsdp));
66 setenv("acpi.rsdp", buf, 1);
67 revision = rsdp->Revision;
68 if (revision == 0)
69 revision = 1;
70 sprintf(buf, "%d", revision);
71 setenv("acpi.revision", buf, 1);
72 strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
73 buf[sizeof(rsdp->OemId)] = '\0';
74 setenv("acpi.oem", buf, 1);
75 sprintf(buf, "0x%08x", rsdp->RsdtPhysicalAddress);
76 setenv("acpi.rsdt", buf, 1);
77 if (revision >= 2) {
78 /* XXX extended checksum? */
79 sprintf(buf, "0x%016llx", rsdp->XsdtPhysicalAddress);
80 setenv("acpi.xsdt", buf, 1);
81 sprintf(buf, "%d", rsdp->Length);
82 setenv("acpi.xsdt_length", buf, 1);
83 }
84 }
85
86 /*
87 * Find the RSDP in low memory. See section 5.2.2 of the ACPI spec.
88 */
89 static ACPI_TABLE_RSDP *
biosacpi_find_rsdp(void)90 biosacpi_find_rsdp(void)
91 {
92 ACPI_TABLE_RSDP *rsdp;
93 uint16_t *addr;
94
95 /* EBDA is the 1 KB addressed by the 16 bit pointer at 0x40E. */
96 addr = (uint16_t *)PTOV(0x40E);
97 if ((rsdp = biosacpi_search_rsdp((char *)(*addr << 4), 0x400)) != NULL)
98 return (rsdp);
99
100 /* Check the upper memory BIOS space, 0xe0000 - 0xfffff. */
101 if ((rsdp = biosacpi_search_rsdp((char *)0xe0000, 0x20000)) != NULL)
102 return (rsdp);
103
104 return (NULL);
105 }
106
107 static ACPI_TABLE_RSDP *
biosacpi_search_rsdp(char * base,int length)108 biosacpi_search_rsdp(char *base, int length)
109 {
110 ACPI_TABLE_RSDP *rsdp;
111 uint8_t *cp, sum;
112 int ofs, idx;
113
114 /* search on 16-byte boundaries */
115 for (ofs = 0; ofs < length; ofs += 16) {
116 rsdp = (ACPI_TABLE_RSDP *)PTOV(base + ofs);
117
118 /* compare signature, validate checksum */
119 if (!strncmp(rsdp->Signature, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP))) {
120 cp = (uint8_t *)rsdp;
121 sum = 0;
122 for (idx = 0; idx < RSDP_CHECKSUM_LENGTH; idx++)
123 sum += *(cp + idx);
124 if (sum != 0)
125 continue;
126 return(rsdp);
127 }
128 }
129 return(NULL);
130 }
131