1 | /* |
---|
2 | * hal_acpi.c - ACPI parser |
---|
3 | * |
---|
4 | * Copyright (c) 2017 Maxime Villard |
---|
5 | * |
---|
6 | * This file is part of ALMOS-MKH. |
---|
7 | * |
---|
8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
9 | * under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; version 2.0 of the License. |
---|
11 | * |
---|
12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | * General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | */ |
---|
21 | |
---|
22 | #include <hal_types.h> |
---|
23 | #include <hal_boot.h> |
---|
24 | #include <hal_acpi.h> |
---|
25 | #include <hal_internal.h> |
---|
26 | |
---|
27 | #include <memcpy.h> |
---|
28 | #include <thread.h> |
---|
29 | #include <string.h> |
---|
30 | #include <process.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <vmm.h> |
---|
33 | #include <core.h> |
---|
34 | #include <cluster.h> |
---|
35 | |
---|
36 | /* XXX XXX libk XXX XXX */ |
---|
37 | int memcmp(char *s1, char *s2, size_t n) |
---|
38 | { |
---|
39 | size_t i; |
---|
40 | for(i = 0; i < n; i++) { |
---|
41 | if (s1[i] != s2[i]) |
---|
42 | return 1; |
---|
43 | } |
---|
44 | return 0; |
---|
45 | } |
---|
46 | |
---|
47 | /* -------------------------------------------------------------------------- */ |
---|
48 | |
---|
49 | #define RSDT_NENTRIES(rsdt) \ |
---|
50 | ( (rsdt->Header.Length - sizeof(rsdt_t) + sizeof(uint32_t)) / sizeof(uint32_t) ) |
---|
51 | #define RSDT_ENTRIES(rsdt) \ |
---|
52 | ((uint32_t *)&rsdt->TableOffsetEntry[0]) |
---|
53 | |
---|
54 | static bool_t hal_acpi_table_correct(header_t *header) |
---|
55 | { |
---|
56 | uint8_t *ptr, sum = 0; |
---|
57 | size_t i, n; |
---|
58 | |
---|
59 | ptr = (uint8_t *)header; |
---|
60 | n = header->Length; |
---|
61 | |
---|
62 | /* Verify checksum */ |
---|
63 | for (i = 0; i < n; i++) { |
---|
64 | sum += ptr[i]; |
---|
65 | } |
---|
66 | |
---|
67 | return (sum == 0); |
---|
68 | } |
---|
69 | |
---|
70 | static vaddr_t hal_acpi_map_table(paddr_t headerpa, const char *sig) |
---|
71 | { |
---|
72 | paddr_t basepa, pa; |
---|
73 | vaddr_t baseva, retva, va; |
---|
74 | size_t off, size; |
---|
75 | size_t npages, ngrow, n; |
---|
76 | header_t *header; |
---|
77 | |
---|
78 | /* Allocate the VA for the header */ |
---|
79 | basepa = rounddown(headerpa, PAGE_SIZE); |
---|
80 | npages = roundup(headerpa + sizeof(header_t), PAGE_SIZE) - basepa; |
---|
81 | baseva = hal_gpt_bootstrap_valloc(npages); |
---|
82 | off = headerpa - basepa; |
---|
83 | |
---|
84 | /* Enter the VA, and get the total size of the structure */ |
---|
85 | hal_gpt_enter_range(baseva, basepa, npages); |
---|
86 | retva = (vaddr_t)(baseva + off); |
---|
87 | header = (header_t *)retva; |
---|
88 | size = header->Length; |
---|
89 | XASSERT(size >= sizeof(header_t)); |
---|
90 | |
---|
91 | /* Check the signature */ |
---|
92 | if (memcmp((void *)&header->Signature, sig, ACPI_NAME_SIZE)) |
---|
93 | return 0; |
---|
94 | |
---|
95 | /* Grow the VA to map the rest */ |
---|
96 | ngrow = roundup(headerpa + size, PAGE_SIZE) - basepa; |
---|
97 | n = ngrow - npages; |
---|
98 | va = hal_gpt_bootstrap_valloc(n); |
---|
99 | pa = basepa + npages * PAGE_SIZE; |
---|
100 | hal_gpt_enter_range(va, pa, n); |
---|
101 | |
---|
102 | /* Verify the checksum */ |
---|
103 | if (!hal_acpi_table_correct(header)) |
---|
104 | x86_panic("Wrong checksum for table"); |
---|
105 | |
---|
106 | return retva; |
---|
107 | } |
---|
108 | |
---|
109 | /* -------------------------------------------------------------------------- */ |
---|
110 | |
---|
111 | static void hal_acpi_parse_madt(madt_t *madt) |
---|
112 | { |
---|
113 | extern paddr_t lapic_pa; |
---|
114 | lapic_pa = (paddr_t)madt->Address; |
---|
115 | x86_printf("-> LAPIC address: %Z\n", lapic_pa); |
---|
116 | } |
---|
117 | |
---|
118 | static madt_t *hal_acpi_map_madt(rsdt_t *rsdt) |
---|
119 | { |
---|
120 | vaddr_t va; |
---|
121 | paddr_t pa; |
---|
122 | uint32_t *ent; |
---|
123 | size_t i, n; |
---|
124 | |
---|
125 | n = RSDT_NENTRIES(rsdt); |
---|
126 | ent = RSDT_ENTRIES(rsdt); |
---|
127 | |
---|
128 | for (i = 0; i < n; i++) { |
---|
129 | pa = (paddr_t)ent[i]; |
---|
130 | va = hal_acpi_map_table(pa, "APIC"); |
---|
131 | if (va == 0) |
---|
132 | continue; |
---|
133 | return (madt_t *)va; |
---|
134 | } |
---|
135 | |
---|
136 | return NULL; |
---|
137 | } |
---|
138 | |
---|
139 | /* -------------------------------------------------------------------------- */ |
---|
140 | |
---|
141 | static rsdt_t *hal_acpi_map_rsdt(rsdp_t *rsdp) |
---|
142 | { |
---|
143 | paddr_t rsdt_pa; |
---|
144 | rsdt_t *rsdt; |
---|
145 | |
---|
146 | rsdt_pa = (paddr_t)rsdp->RsdtPhysicalAddress; |
---|
147 | rsdt = (rsdt_t *)hal_acpi_map_table(rsdt_pa, "RSDT"); |
---|
148 | if (rsdt == 0) |
---|
149 | x86_panic("RSDT invalid"); |
---|
150 | |
---|
151 | return rsdt; |
---|
152 | } |
---|
153 | |
---|
154 | /* -------------------------------------------------------------------------- */ |
---|
155 | |
---|
156 | static bool_t hal_acpi_rsdp_correct(uint8_t *ptr) |
---|
157 | { |
---|
158 | uint8_t sum = 0; |
---|
159 | size_t i; |
---|
160 | |
---|
161 | /* Verify checksum */ |
---|
162 | for (i = 0; i < 20; i++) { |
---|
163 | sum += ptr[i]; |
---|
164 | } |
---|
165 | |
---|
166 | return (sum == 0); |
---|
167 | } |
---|
168 | |
---|
169 | static rsdp_t *hal_acpi_find_rsdp(vaddr_t va_start, vaddr_t va_end) |
---|
170 | { |
---|
171 | rsdp_t *rsdp; |
---|
172 | vaddr_t va = va_start; |
---|
173 | size_t i, n = PAGE_SIZE / ACPI_RSDP_ALIGN; |
---|
174 | char oem[ACPI_OEM_ID_SIZE + 1]; |
---|
175 | uint8_t *ptr; |
---|
176 | |
---|
177 | /* The table is on a 16bit boundary */ |
---|
178 | for (va = va_start; va < va_end; va += PAGE_SIZE) { |
---|
179 | for (i = 0; i < n; i++) { |
---|
180 | ptr = (uint8_t *)va + i * ACPI_RSDP_ALIGN; |
---|
181 | if (memcmp(ptr, ACPI_RSDP_SIGNATURE, ACPI_RSDP_SIGNATURE_SIZE)) |
---|
182 | continue; |
---|
183 | if (!hal_acpi_rsdp_correct(ptr)) |
---|
184 | continue; |
---|
185 | goto found; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | return NULL; |
---|
190 | |
---|
191 | found: |
---|
192 | rsdp = (rsdp_t *)ptr; |
---|
193 | |
---|
194 | memcpy(&oem, rsdp->OemId, ACPI_OEM_ID_SIZE); |
---|
195 | oem[ACPI_OEM_ID_SIZE] = '\0'; |
---|
196 | x86_printf("-> OEM: %s\n", oem); |
---|
197 | if (rsdp->Revision != 0) |
---|
198 | x86_printf("[!] Using ACPI 1.0\n"); |
---|
199 | |
---|
200 | return rsdp; |
---|
201 | } |
---|
202 | |
---|
203 | /* -------------------------------------------------------------------------- */ |
---|
204 | |
---|
205 | void hal_acpi_init() |
---|
206 | { |
---|
207 | rsdp_t *rsdp; |
---|
208 | rsdt_t *rsdt; |
---|
209 | madt_t *madt; |
---|
210 | paddr_t bios_min = 0x0E0000; |
---|
211 | paddr_t bios_max = 0x100000; |
---|
212 | vaddr_t vabase; |
---|
213 | size_t npages; |
---|
214 | |
---|
215 | npages = (bios_max - bios_min) / PAGE_SIZE; |
---|
216 | vabase = hal_gpt_bootstrap_valloc(npages); |
---|
217 | |
---|
218 | hal_gpt_enter_range(vabase, bios_min, npages); |
---|
219 | |
---|
220 | /* First, find RSDP */ |
---|
221 | rsdp = hal_acpi_find_rsdp(vabase, vabase + npages * PAGE_SIZE); |
---|
222 | if (rsdp == NULL) |
---|
223 | x86_panic("RSDP not found"); |
---|
224 | |
---|
225 | /* Then, map RSDT */ |
---|
226 | rsdt = hal_acpi_map_rsdt(rsdp); |
---|
227 | if (rsdt == NULL) |
---|
228 | x86_panic("RSDT not found"); |
---|
229 | |
---|
230 | /* Now, map MADT */ |
---|
231 | madt = hal_acpi_map_madt(rsdt); |
---|
232 | if (madt == NULL) |
---|
233 | x86_panic("MADT not found"); |
---|
234 | |
---|
235 | /* Parse it */ |
---|
236 | hal_acpi_parse_madt(madt); |
---|
237 | } |
---|
238 | |
---|