source: mainline/kernel/genarch/src/acpi/acpi.c@ 780edc7

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

Fix test for memcmp() match

  • 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#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
173static uint8_t *search_rsdp(uint8_t *base, size_t len)
174{
175 for (size_t i = 0; i < len; i += 16) {
176 if (!__builtin_memcmp(&base[i], RSDP_SIGNATURE, 8) &&
177 rsdp_check(&base[i]))
178 return &base[i];
179 }
180
181 return NULL;
182}
183
184void acpi_init(void)
185{
186 /*
187 * Find Root System Description Pointer
188 * 1. search first 1K of EBDA
189 * 2. search 128K starting at 0xe0000
190 */
191
192 uint8_t *rsdp = NULL;
193
194 if (ebda)
195 rsdp = search_rsdp((uint8_t *) PA2KA(ebda), 1024);
196
197 if (!rsdp)
198 rsdp = search_rsdp((uint8_t *) PA2KA(0xe0000), 128 * 1024);
199
200 if (!rsdp)
201 return;
202
203 acpi_rsdp = (struct acpi_rsdp *) rsdp;
204
205 LOG("%p: ACPI Root System Description Pointer", acpi_rsdp);
206
207 uintptr_t acpi_rsdt_p = (uintptr_t) acpi_rsdp->rsdt_address;
208 uintptr_t acpi_xsdt_p = 0;
209
210 if (acpi_rsdp->revision)
211 acpi_xsdt_p = (uintptr_t) acpi_rsdp->xsdt_address;
212
213 if (acpi_rsdt_p)
214 acpi_rsdt = (struct acpi_rsdt *) map_sdt(
215 (struct acpi_sdt_header *) acpi_rsdt_p);
216
217 if (acpi_xsdt_p)
218 acpi_xsdt = (struct acpi_xsdt *) map_sdt(
219 (struct acpi_sdt_header *) acpi_xsdt_p);
220
221 if ((acpi_rsdt) && (!acpi_sdt_check((uint8_t *) acpi_rsdt))) {
222 log(LF_ARCH, LVL_ERROR, "RSDT: bad checksum");
223 return;
224 }
225
226 if ((acpi_xsdt) && (!acpi_sdt_check((uint8_t *) acpi_xsdt))) {
227 log(LF_ARCH, LVL_ERROR, "XSDT: bad checksum");
228 return;
229 }
230
231 if (acpi_xsdt)
232 configure_via_xsdt();
233 else if (acpi_rsdt)
234 configure_via_rsdt();
235}
236
237/** @}
238 */
Note: See TracBrowser for help on using the repository browser.