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 <mm/as.h>
|
---|
42 | #include <mm/page.h>
|
---|
43 | #include <print.h>
|
---|
44 |
|
---|
45 | #define RSDP_SIGNATURE "RSD PTR "
|
---|
46 | #define RSDP_REVISION_OFFS 15
|
---|
47 |
|
---|
48 | #define CMP_SIGNATURE(left, right) \
|
---|
49 | (((left)[0] == (right)[0]) && \
|
---|
50 | ((left)[1] == (right)[1]) && \
|
---|
51 | ((left)[2] == (right)[2]) && \
|
---|
52 | ((left)[3] == (right)[3]))
|
---|
53 |
|
---|
54 | struct acpi_rsdp *acpi_rsdp = NULL;
|
---|
55 | struct acpi_rsdt *acpi_rsdt = NULL;
|
---|
56 | struct acpi_xsdt *acpi_xsdt = NULL;
|
---|
57 |
|
---|
58 | struct acpi_signature_map signature_map[] = {
|
---|
59 | {
|
---|
60 | (uint8_t *) "APIC",
|
---|
61 | (void *) &acpi_madt,
|
---|
62 | "Multiple APIC Description Table"
|
---|
63 | }
|
---|
64 | };
|
---|
65 |
|
---|
66 | static int rsdp_check(uint8_t *rsdp) {
|
---|
67 | struct acpi_rsdp *r = (struct acpi_rsdp *) rsdp;
|
---|
68 | uint8_t sum = 0;
|
---|
69 | unsigned int i;
|
---|
70 |
|
---|
71 | for (i = 0; i < 20; i++)
|
---|
72 | sum = (uint8_t) (sum + rsdp[i]);
|
---|
73 |
|
---|
74 | if (sum)
|
---|
75 | return 0; /* bad checksum */
|
---|
76 |
|
---|
77 | if (r->revision == 0)
|
---|
78 | return 1; /* ACPI 1.0 */
|
---|
79 |
|
---|
80 | for (; i < r->length; i++)
|
---|
81 | sum = (uint8_t) (sum + rsdp[i]);
|
---|
82 |
|
---|
83 | return !sum;
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | int acpi_sdt_check(uint8_t *sdt)
|
---|
88 | {
|
---|
89 | struct acpi_sdt_header *h = (struct acpi_sdt_header *) sdt;
|
---|
90 | uint8_t sum = 0;
|
---|
91 | unsigned int i;
|
---|
92 |
|
---|
93 | for (i = 0; i < h->length; i++)
|
---|
94 | sum = (uint8_t) (sum + sdt[i]);
|
---|
95 |
|
---|
96 | return !sum;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static void map_sdt(struct acpi_sdt_header *sdt)
|
---|
100 | {
|
---|
101 | page_table_lock(AS_KERNEL, true);
|
---|
102 | page_mapping_insert(AS_KERNEL, (uintptr_t) sdt, (uintptr_t) sdt, PAGE_NOT_CACHEABLE | PAGE_WRITE);
|
---|
103 | map_structure((uintptr_t) sdt, sdt->length);
|
---|
104 | page_table_unlock(AS_KERNEL, true);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static void configure_via_rsdt(void)
|
---|
108 | {
|
---|
109 | unsigned int i, j, cnt = (acpi_rsdt->header.length - sizeof(struct acpi_sdt_header)) / sizeof(uint32_t);
|
---|
110 |
|
---|
111 | for (i = 0; i < cnt; i++) {
|
---|
112 | for (j = 0; j < sizeof(signature_map) / sizeof(struct acpi_signature_map); j++) {
|
---|
113 | struct acpi_sdt_header *h = (struct acpi_sdt_header *) (unative_t) acpi_rsdt->entry[i];
|
---|
114 |
|
---|
115 | map_sdt(h);
|
---|
116 | if (CMP_SIGNATURE(h->signature, signature_map[j].signature)) {
|
---|
117 | if (!acpi_sdt_check((uint8_t *) h))
|
---|
118 | goto next;
|
---|
119 | *signature_map[j].sdt_ptr = h;
|
---|
120 | LOG("%p: ACPI %s\n", *signature_map[j].sdt_ptr, signature_map[j].description);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | next:
|
---|
124 | ;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void configure_via_xsdt(void)
|
---|
129 | {
|
---|
130 | unsigned int i, j, cnt = (acpi_xsdt->header.length - sizeof(struct acpi_sdt_header)) / sizeof(uint64_t);
|
---|
131 |
|
---|
132 | for (i = 0; i < cnt; i++) {
|
---|
133 | for (j = 0; j < sizeof(signature_map) / sizeof(struct acpi_signature_map); j++) {
|
---|
134 | struct acpi_sdt_header *h = (struct acpi_sdt_header *) ((uintptr_t) acpi_rsdt->entry[i]);
|
---|
135 |
|
---|
136 | map_sdt(h);
|
---|
137 | if (CMP_SIGNATURE(h->signature, signature_map[j].signature)) {
|
---|
138 | if (!acpi_sdt_check((uint8_t *) h))
|
---|
139 | goto next;
|
---|
140 | *signature_map[j].sdt_ptr = h;
|
---|
141 | LOG("%p: ACPI %s\n", *signature_map[j].sdt_ptr, signature_map[j].description);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | next:
|
---|
145 | ;
|
---|
146 | }
|
---|
147 |
|
---|
148 | }
|
---|
149 |
|
---|
150 | void acpi_init(void)
|
---|
151 | {
|
---|
152 | uint8_t *addr[2] = { NULL, (uint8_t *) PA2KA(0xe0000) };
|
---|
153 | int i, j, length[2] = { 1024, 128*1024 };
|
---|
154 | uint64_t *sig = (uint64_t *) RSDP_SIGNATURE;
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * Find Root System Description Pointer
|
---|
158 | * 1. search first 1K of EBDA
|
---|
159 | * 2. search 128K starting at 0xe0000
|
---|
160 | */
|
---|
161 |
|
---|
162 | addr[0] = (uint8_t *) PA2KA(ebda);
|
---|
163 | for (i = (ebda ? 0 : 1); i < 2; i++) {
|
---|
164 | for (j = 0; j < length[i]; j += 16) {
|
---|
165 | if (*((uint64_t *) &addr[i][j]) == *sig && rsdp_check(&addr[i][j])) {
|
---|
166 | acpi_rsdp = (struct acpi_rsdp *) &addr[i][j];
|
---|
167 | goto rsdp_found;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | return;
|
---|
173 |
|
---|
174 | rsdp_found:
|
---|
175 | LOG("%p: ACPI Root System Description Pointer\n", acpi_rsdp);
|
---|
176 |
|
---|
177 | acpi_rsdt = (struct acpi_rsdt *) (unative_t) acpi_rsdp->rsdt_address;
|
---|
178 | if (acpi_rsdp->revision)
|
---|
179 | acpi_xsdt = (struct acpi_xsdt *) ((uintptr_t) acpi_rsdp->xsdt_address);
|
---|
180 |
|
---|
181 | if (acpi_rsdt)
|
---|
182 | map_sdt((struct acpi_sdt_header *) acpi_rsdt);
|
---|
183 | if (acpi_xsdt)
|
---|
184 | map_sdt((struct acpi_sdt_header *) acpi_xsdt);
|
---|
185 |
|
---|
186 | if (acpi_rsdt && !acpi_sdt_check((uint8_t *) acpi_rsdt)) {
|
---|
187 | printf("RSDT: bad checksum\n");
|
---|
188 | return;
|
---|
189 | }
|
---|
190 | if (acpi_xsdt && !acpi_sdt_check((uint8_t *) acpi_xsdt)) {
|
---|
191 | printf("XSDT: bad checksum\n");
|
---|
192 | return;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (acpi_xsdt)
|
---|
196 | configure_via_xsdt();
|
---|
197 | else if (acpi_rsdt)
|
---|
198 | configure_via_rsdt();
|
---|
199 |
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** @}
|
---|
203 | */
|
---|