source: mainline/kernel/genarch/src/acpi/acpi.c@ cbf528f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cbf528f was cbf528f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Fix rsdp search

Forgot that sizeof(string) includes the NUL byte.

  • Property mode set to 100644
File size: 6.2 KB
Line 
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 kernel_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#include <mem.h>
46
47#define RSDP_SIGNATURE "RSD PTR "
48#define RSDP_REVISION_OFFS 15
49
50#define CMP_SIGNATURE(left, right) \
51 (((left)[0] == (right)[0]) && \
52 ((left)[1] == (right)[1]) && \
53 ((left)[2] == (right)[2]) && \
54 ((left)[3] == (right)[3]))
55
56struct acpi_rsdp *acpi_rsdp = NULL;
57struct acpi_rsdt *acpi_rsdt = NULL;
58struct acpi_xsdt *acpi_xsdt = NULL;
59
60struct acpi_signature_map signature_map[] = {
61 {
62 (uint8_t *) "APIC",
63 (void *) &acpi_madt,
64 "Multiple APIC Description Table"
65 }
66};
67
68static int rsdp_check(uint8_t *_rsdp)
69{
70 struct acpi_rsdp *rsdp = (struct acpi_rsdp *) _rsdp;
71 uint8_t sum = 0;
72 uint32_t i;
73
74 for (i = 0; i < 20; i++)
75 sum = (uint8_t) (sum + _rsdp[i]);
76
77 if (sum)
78 return 0; /* bad checksum */
79
80 if (rsdp->revision == 0)
81 return 1; /* ACPI 1.0 */
82
83 for (; i < rsdp->length; i++)
84 sum = (uint8_t) (sum + _rsdp[i]);
85
86 return !sum;
87}
88
89int acpi_sdt_check(uint8_t *sdt)
90{
91 struct acpi_sdt_header *hdr = (struct acpi_sdt_header *) sdt;
92 uint8_t sum = 0;
93 unsigned int i;
94
95 for (i = 0; i < hdr->length; i++)
96 sum = (uint8_t) (sum + sdt[i]);
97
98 return !sum;
99}
100
101static struct acpi_sdt_header *map_sdt(struct acpi_sdt_header *psdt)
102{
103 struct acpi_sdt_header *vhdr;
104 struct acpi_sdt_header *vsdt;
105
106 /* Start with mapping the header only. */
107 vhdr = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
108 sizeof(struct acpi_sdt_header), KM_NATURAL_ALIGNMENT,
109 PAGE_READ | PAGE_NOT_CACHEABLE);
110
111 /* Now we can map the entire structure. */
112 vsdt = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
113 vhdr->length, KM_NATURAL_ALIGNMENT,
114 PAGE_WRITE | PAGE_NOT_CACHEABLE);
115
116 // TODO: do not leak vtmp
117
118 return vsdt;
119}
120
121static void configure_via_rsdt(void)
122{
123 size_t i;
124 size_t j;
125 size_t cnt = (acpi_rsdt->header.length - sizeof(struct acpi_sdt_header)) /
126 sizeof(uint32_t);
127
128 for (i = 0; i < cnt; i++) {
129 for (j = 0; j < sizeof(signature_map) /
130 sizeof(struct acpi_signature_map); j++) {
131 struct acpi_sdt_header *hdr =
132 (struct acpi_sdt_header *) (sysarg_t) acpi_rsdt->entry[i];
133
134 struct acpi_sdt_header *vhdr = map_sdt(hdr);
135 if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
136 if (!acpi_sdt_check((uint8_t *) vhdr))
137 break;
138
139 *signature_map[j].sdt_ptr = vhdr;
140 LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
141 signature_map[j].description);
142 }
143 }
144 }
145}
146
147static void configure_via_xsdt(void)
148{
149 size_t i;
150 size_t j;
151 size_t cnt = (acpi_xsdt->header.length - sizeof(struct acpi_sdt_header)) /
152 sizeof(uint64_t);
153
154 for (i = 0; i < cnt; i++) {
155 for (j = 0; j < sizeof(signature_map) /
156 sizeof(struct acpi_signature_map); j++) {
157 struct acpi_sdt_header *hdr =
158 (struct acpi_sdt_header *) ((uintptr_t) acpi_xsdt->entry[i]);
159
160 struct acpi_sdt_header *vhdr = map_sdt(hdr);
161 if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
162 if (!acpi_sdt_check((uint8_t *) vhdr))
163 break;
164
165 *signature_map[j].sdt_ptr = vhdr;
166 LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
167 signature_map[j].description);
168 }
169 }
170 }
171}
172
173typedef uint64_t _a64_t __attribute__((may_alias));
174
175static uint8_t *search_rsdp(uint8_t *base, size_t len)
176{
177 uint64_t signature = *(_a64_t *) RSDP_SIGNATURE;
178
179 for (size_t i = 0; i < len; i += 16) {
180 if (*((_a64_t *) &base[i]) == signature && rsdp_check(&base[i]))
181 return &base[i];
182 }
183
184 return NULL;
185}
186
187void acpi_init(void)
188{
189 /*
190 * Find Root System Description Pointer
191 * 1. search first 1K of EBDA
192 * 2. search 128K starting at 0xe0000
193 */
194
195 uint8_t *rsdp = NULL;
196
197 if (ebda)
198 rsdp = search_rsdp((uint8_t *) PA2KA(ebda), 1024);
199
200 if (!rsdp)
201 rsdp = search_rsdp((uint8_t *) PA2KA(0xe0000), 128 * 1024);
202
203 if (!rsdp)
204 return;
205
206 acpi_rsdp = (struct acpi_rsdp *) rsdp;
207
208 LOG("%p: ACPI Root System Description Pointer", acpi_rsdp);
209
210 uintptr_t acpi_rsdt_p = (uintptr_t) acpi_rsdp->rsdt_address;
211 uintptr_t acpi_xsdt_p = 0;
212
213 if (acpi_rsdp->revision)
214 acpi_xsdt_p = (uintptr_t) acpi_rsdp->xsdt_address;
215
216 if (acpi_rsdt_p)
217 acpi_rsdt = (struct acpi_rsdt *) map_sdt(
218 (struct acpi_sdt_header *) acpi_rsdt_p);
219
220 if (acpi_xsdt_p)
221 acpi_xsdt = (struct acpi_xsdt *) map_sdt(
222 (struct acpi_sdt_header *) acpi_xsdt_p);
223
224 if ((acpi_rsdt) && (!acpi_sdt_check((uint8_t *) acpi_rsdt))) {
225 log(LF_ARCH, LVL_ERROR, "RSDT: bad checksum");
226 return;
227 }
228
229 if ((acpi_xsdt) && (!acpi_sdt_check((uint8_t *) acpi_xsdt))) {
230 log(LF_ARCH, LVL_ERROR, "XSDT: bad checksum");
231 return;
232 }
233
234 if (acpi_xsdt)
235 configure_via_xsdt();
236 else if (acpi_rsdt)
237 configure_via_rsdt();
238}
239
240/** @}
241 */
Note: See TracBrowser for help on using the repository browser.