1 | /*
|
---|
2 | * Copyright (c) 2005 Jakub Jermar
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genarch
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Advanced Configuration and Power Interface (ACPI) initialization.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <genarch/acpi/acpi.h>
|
---|
39 | #include <genarch/acpi/madt.h>
|
---|
40 | #include <arch/bios/bios.h>
|
---|
41 | #include <debug.h>
|
---|
42 | #include <mm/page.h>
|
---|
43 | #include <mm/km.h>
|
---|
44 | #include <log.h>
|
---|
45 |
|
---|
46 | #define RSDP_SIGNATURE "RSD PTR "
|
---|
47 | #define RSDP_REVISION_OFFS 15
|
---|
48 |
|
---|
49 | #define CMP_SIGNATURE(left, right) \
|
---|
50 | (((left)[0] == (right)[0]) && \
|
---|
51 | ((left)[1] == (right)[1]) && \
|
---|
52 | ((left)[2] == (right)[2]) && \
|
---|
53 | ((left)[3] == (right)[3]))
|
---|
54 |
|
---|
55 | struct acpi_rsdp *acpi_rsdp = NULL;
|
---|
56 | struct acpi_rsdt *acpi_rsdt = NULL;
|
---|
57 | struct acpi_xsdt *acpi_xsdt = NULL;
|
---|
58 |
|
---|
59 | struct acpi_signature_map signature_map[] = {
|
---|
60 | {
|
---|
61 | (uint8_t *) "APIC",
|
---|
62 | (void *) &acpi_madt,
|
---|
63 | "Multiple APIC Description Table"
|
---|
64 | }
|
---|
65 | };
|
---|
66 |
|
---|
67 | static int rsdp_check(uint8_t *_rsdp) {
|
---|
68 | struct acpi_rsdp *rsdp = (struct acpi_rsdp *) _rsdp;
|
---|
69 | uint8_t sum = 0;
|
---|
70 | uint32_t i;
|
---|
71 |
|
---|
72 | for (i = 0; i < 20; i++)
|
---|
73 | sum = (uint8_t) (sum + _rsdp[i]);
|
---|
74 |
|
---|
75 | if (sum)
|
---|
76 | return 0; /* bad checksum */
|
---|
77 |
|
---|
78 | if (rsdp->revision == 0)
|
---|
79 | return 1; /* ACPI 1.0 */
|
---|
80 |
|
---|
81 | for (; i < rsdp->length; i++)
|
---|
82 | sum = (uint8_t) (sum + _rsdp[i]);
|
---|
83 |
|
---|
84 | return !sum;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int acpi_sdt_check(uint8_t *sdt)
|
---|
88 | {
|
---|
89 | struct acpi_sdt_header *hdr = (struct acpi_sdt_header *) sdt;
|
---|
90 | uint8_t sum = 0;
|
---|
91 | unsigned int i;
|
---|
92 |
|
---|
93 | for (i = 0; i < hdr->length; i++)
|
---|
94 | sum = (uint8_t) (sum + sdt[i]);
|
---|
95 |
|
---|
96 | return !sum;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static struct acpi_sdt_header *map_sdt(struct acpi_sdt_header *psdt)
|
---|
100 | {
|
---|
101 | struct acpi_sdt_header *vhdr;
|
---|
102 | struct acpi_sdt_header *vsdt;
|
---|
103 |
|
---|
104 | /* Start with mapping the header only. */
|
---|
105 | vhdr = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
|
---|
106 | sizeof(struct acpi_sdt_header), 0);
|
---|
107 |
|
---|
108 | /* Now we can map the entire structure. */
|
---|
109 | vsdt = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
|
---|
110 | vhdr->length, PAGE_WRITE);
|
---|
111 |
|
---|
112 | // TODO: do not leak vtmp
|
---|
113 |
|
---|
114 | return vsdt;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static void configure_via_rsdt(void)
|
---|
118 | {
|
---|
119 | size_t i;
|
---|
120 | size_t j;
|
---|
121 | size_t cnt = (acpi_rsdt->header.length - sizeof(struct acpi_sdt_header))
|
---|
122 | / sizeof(uint32_t);
|
---|
123 |
|
---|
124 | for (i = 0; i < cnt; i++) {
|
---|
125 | for (j = 0; j < sizeof(signature_map)
|
---|
126 | / sizeof(struct acpi_signature_map); j++) {
|
---|
127 | struct acpi_sdt_header *hdr =
|
---|
128 | (struct acpi_sdt_header *) (sysarg_t) acpi_rsdt->entry[i];
|
---|
129 |
|
---|
130 | struct acpi_sdt_header *vhdr = map_sdt(hdr);
|
---|
131 | if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
|
---|
132 | if (!acpi_sdt_check((uint8_t *) vhdr))
|
---|
133 | break;
|
---|
134 |
|
---|
135 | *signature_map[j].sdt_ptr = vhdr;
|
---|
136 | LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
|
---|
137 | signature_map[j].description);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void configure_via_xsdt(void)
|
---|
144 | {
|
---|
145 | size_t i;
|
---|
146 | size_t j;
|
---|
147 | size_t cnt = (acpi_xsdt->header.length - sizeof(struct acpi_sdt_header))
|
---|
148 | / sizeof(uint64_t);
|
---|
149 |
|
---|
150 | for (i = 0; i < cnt; i++) {
|
---|
151 | for (j = 0; j < sizeof(signature_map)
|
---|
152 | / sizeof(struct acpi_signature_map); j++) {
|
---|
153 | struct acpi_sdt_header *hdr =
|
---|
154 | (struct acpi_sdt_header *) ((uintptr_t) acpi_xsdt->entry[i]);
|
---|
155 |
|
---|
156 | struct acpi_sdt_header *vhdr = map_sdt(hdr);
|
---|
157 | if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
|
---|
158 | if (!acpi_sdt_check((uint8_t *) vhdr))
|
---|
159 | break;
|
---|
160 |
|
---|
161 | *signature_map[j].sdt_ptr = vhdr;
|
---|
162 | LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
|
---|
163 | signature_map[j].description);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | void acpi_init(void)
|
---|
170 | {
|
---|
171 | uint8_t *addr[2] = { NULL, (uint8_t *) PA2KA(0xe0000) };
|
---|
172 | unsigned int i;
|
---|
173 | unsigned int j;
|
---|
174 | unsigned int length[2] = { 1024, 128 * 1024 };
|
---|
175 | uint64_t *sig = (uint64_t *) RSDP_SIGNATURE;
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Find Root System Description Pointer
|
---|
179 | * 1. search first 1K of EBDA
|
---|
180 | * 2. search 128K starting at 0xe0000
|
---|
181 | */
|
---|
182 |
|
---|
183 | addr[0] = (uint8_t *) PA2KA(ebda);
|
---|
184 | for (i = (ebda ? 0 : 1); i < 2; i++) {
|
---|
185 | for (j = 0; j < length[i]; j += 16) {
|
---|
186 | if ((*((uint64_t *) &addr[i][j]) == *sig)
|
---|
187 | && (rsdp_check(&addr[i][j]))) {
|
---|
188 | acpi_rsdp = (struct acpi_rsdp *) &addr[i][j];
|
---|
189 | goto rsdp_found;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | return;
|
---|
195 |
|
---|
196 | rsdp_found:
|
---|
197 | LOG("%p: ACPI Root System Description Pointer", acpi_rsdp);
|
---|
198 |
|
---|
199 | uintptr_t acpi_rsdt_p = (uintptr_t) acpi_rsdp->rsdt_address;
|
---|
200 | uintptr_t acpi_xsdt_p = 0;
|
---|
201 |
|
---|
202 | if (acpi_rsdp->revision)
|
---|
203 | acpi_xsdt_p = (uintptr_t) acpi_rsdp->xsdt_address;
|
---|
204 |
|
---|
205 | if (acpi_rsdt_p)
|
---|
206 | acpi_rsdt = (struct acpi_rsdt *) map_sdt(
|
---|
207 | (struct acpi_sdt_header *) acpi_rsdt_p);
|
---|
208 |
|
---|
209 | if (acpi_xsdt_p)
|
---|
210 | acpi_xsdt = (struct acpi_xsdt *) map_sdt(
|
---|
211 | (struct acpi_sdt_header *) acpi_xsdt_p);
|
---|
212 |
|
---|
213 | if ((acpi_rsdt) && (!acpi_sdt_check((uint8_t *) acpi_rsdt))) {
|
---|
214 | log(LF_ARCH, LVL_ERROR, "RSDT: bad checksum");
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | if ((acpi_xsdt) && (!acpi_sdt_check((uint8_t *) acpi_xsdt))) {
|
---|
219 | log(LF_ARCH, LVL_ERROR, "XSDT: bad checksum");
|
---|
220 | return;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (acpi_xsdt)
|
---|
224 | configure_via_xsdt();
|
---|
225 | else if (acpi_rsdt)
|
---|
226 | configure_via_rsdt();
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** @}
|
---|
230 | */
|
---|