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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cfdeedc was 6404aca, checked in by Jakub Jermar <jakub@…>, 7 years ago

Disambiguate doxygroup genarch*

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