Fallback to ACPI MADT CPU enumeration, if BIOS does not provide MP table.
[dragonfly.git] / sys / platform / pc64 / x86_64 / mp_madt.c
CommitLineData
8e4c6923
MN
1/*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37
38#include <machine/pmap.h>
39#include <machine/smp.h>
40
41#define ACPI_RSDP_EBDA_MAPSZ 1024
42#define ACPI_RSDP_BIOS_MAPSZ 0x20000
43#define ACPI_RSDP_BIOS_MAPADDR 0xe0000
44
45#define ACPI_RSDP_ALIGN 16
46
47#define ACPI_RSDP_SIGLEN 8
48#define ACPI_RSDP_SIG "RSD PTR "
49
50#define ACPI_SDTH_SIGLEN 4
51#define ACPI_RSDT_SIG "RSDT"
52#define ACPI_XSDT_SIG "XSDT"
53#define ACPI_MADT_SIG "APIC"
54
55/* Root System Description Pointer */
56struct acpi_rsdp {
57 uint8_t rsdp_sig[ACPI_RSDP_SIGLEN];
58 uint8_t rsdp_cksum;
59 uint8_t rsdp_oem_id[6];
60 uint8_t rsdp_rev;
61 uint32_t rsdp_rsdt;
62 uint32_t rsdp_len;
63 uint64_t rsdp_xsdt;
64 uint8_t rsdp_ext_cksum;
65 uint8_t rsdp_rsvd[3];
66} __packed;
67
68/* System Description Table Header */
69struct acpi_sdth {
70 uint8_t sdth_sig[ACPI_SDTH_SIGLEN];
71 uint32_t sdth_len;
72 uint8_t sdth_rev;
73 uint8_t sdth_cksum;
74 uint8_t sdth_oem_id[6];
75 uint8_t sdth_oem_tbid[8];
76 uint32_t sdth_oem_rev;
77 uint32_t sdth_crt_id;
78 uint32_t sdth_crt_rev;
79} __packed;
80
81/* Extended System Description Table */
82struct acpi_xsdt {
83 struct acpi_sdth xsdt_hdr;
84 uint64_t xsdt_ents[1];
85} __packed;
86
87/* Root System Description Table */
88struct acpi_rsdt {
89 struct acpi_sdth rsdt_hdr;
90 uint32_t rsdt_ents[1];
91} __packed;
92
93/* Multiple APIC Description Table */
94struct acpi_madt {
95 struct acpi_sdth madt_hdr;
96 uint32_t madt_lapic_addr;
97 uint32_t madt_flags;
98 uint8_t madt_ents[1];
99} __packed;
100
101/* Common parts of MADT APIC structure */
102struct acpi_madt_ent {
103 uint8_t me_type; /* MADT_ENT_ */
104 uint8_t me_len;
105} __packed;
106
107#define MADT_ENT_LAPIC 0
108
109/* MADT Processor Local APIC */
110struct acpi_madt_lapic {
111 struct acpi_madt_ent ml_hdr;
112 uint8_t ml_cpu_id;
113 uint8_t ml_apic_id;
114 uint32_t ml_flags; /* MADT_LAPIC_ */
115} __packed;
116
117#define MADT_LAPIC_ENABLED 0x1
118
119typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
120
121static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
122static void *madt_sdth_map(vm_paddr_t);
123static void madt_sdth_unmap(struct acpi_sdth *);
124static vm_paddr_t madt_search_xsdt(vm_paddr_t);
125static vm_paddr_t madt_search_rsdt(vm_paddr_t);
126static int madt_parse(vm_paddr_t);
127
128extern u_long ebda_addr;
129
130int
131madt_probe(void)
132{
133 const struct acpi_rsdp *rsdp;
134 madt_search_t search;
135 vm_paddr_t search_paddr, madt_paddr;
136 vm_size_t mapsz;
137 uint8_t *ptr;
138
139 if (ebda_addr != 0) {
140 mapsz = ACPI_RSDP_EBDA_MAPSZ;
141 ptr = pmap_mapdev(ebda_addr, mapsz);
142
143 rsdp = madt_rsdp_search(ptr, mapsz);
144 if (rsdp == NULL) {
145 kprintf("madt: RSDP not in EBDA\n");
146 pmap_unmapdev((vm_offset_t)ptr, mapsz);
147
148 ptr = NULL;
149 mapsz = 0;
150 } else {
151 kprintf("madt: RSDP in EBDA\n");
152 goto found_rsdp;
153 }
154 }
155
156 mapsz = ACPI_RSDP_BIOS_MAPSZ;
157 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
158
159 rsdp = madt_rsdp_search(ptr, mapsz);
160 if (rsdp == NULL) {
161 kprintf("madt_probe: no RSDP\n");
162 pmap_unmapdev((vm_offset_t)ptr, mapsz);
163 return ENOENT;
164 } else {
165 kprintf("madt: RSDP in BIOS mem\n");
166 }
167
168found_rsdp:
169 if (rsdp->rsdp_rev != 2) {
170 search_paddr = rsdp->rsdp_rsdt;
171 search = madt_search_rsdt;
172 } else {
173 search_paddr = rsdp->rsdp_xsdt;
174 search = madt_search_xsdt;
175 }
176 pmap_unmapdev((vm_offset_t)ptr, mapsz);
177
178 madt_paddr = search(search_paddr);
179 if (madt_paddr == 0) {
180 kprintf("madt_probe: can't locate MADT\n");
181 return ENOENT;
182 }
183 return madt_parse(madt_paddr);
184}
185
186static const struct acpi_rsdp *
187madt_rsdp_search(const uint8_t *target, int size)
188{
189 const struct acpi_rsdp *rsdp;
190 int i;
191
192 KKASSERT(size > sizeof(*rsdp));
193
194 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
195 rsdp = (const struct acpi_rsdp *)&target[i];
196 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
197 ACPI_RSDP_SIGLEN) == 0)
198 return rsdp;
199 }
200 return NULL;
201}
202
203static void *
204madt_sdth_map(vm_paddr_t paddr)
205{
206 struct acpi_sdth *sdth;
207 vm_size_t mapsz;
208
209 sdth = pmap_mapdev(paddr, sizeof(*sdth));
210 mapsz = sdth->sdth_len;
211 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
212
213 if (mapsz < sizeof(*sdth))
214 return NULL;
215
216 return pmap_mapdev(paddr, mapsz);
217}
218
219static void
220madt_sdth_unmap(struct acpi_sdth *sdth)
221{
222 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
223}
224
225static vm_paddr_t
226madt_search_xsdt(vm_paddr_t xsdt_paddr)
227{
228 struct acpi_xsdt *xsdt;
229 vm_paddr_t madt_paddr = 0;
230 int i, nent;
231
232 if (xsdt_paddr == 0) {
233 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
234 return 0;
235 }
236
237 xsdt = madt_sdth_map(xsdt_paddr);
238 if (xsdt == NULL) {
239 kprintf("madt_search_xsdt: can't map XSDT\n");
240 return 0;
241 }
242
243 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
244 ACPI_SDTH_SIGLEN) != 0) {
245 kprintf("madt_search_xsdt: not XSDT\n");
246 goto back;
247 }
248
249 if (xsdt->xsdt_hdr.sdth_rev != 1) {
250 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
251 xsdt->xsdt_hdr.sdth_rev);
252 goto back;
253 }
254
255 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
256 sizeof(xsdt->xsdt_ents[0]);
257 for (i = 0; i < nent; ++i) {
258 struct acpi_sdth *sdth;
259
260 if (xsdt->xsdt_ents[i] == 0)
261 continue;
262
263 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
264 if (sdth != NULL) {
265 int ret;
266
267 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
268 ACPI_SDTH_SIGLEN);
269 madt_sdth_unmap(sdth);
270
271 if (ret == 0) {
272 kprintf("madt: MADT in XSDT\n");
273 madt_paddr = xsdt->xsdt_ents[i];
274 break;
275 }
276 }
277 }
278back:
279 madt_sdth_unmap(&xsdt->xsdt_hdr);
280 return madt_paddr;
281}
282
283static vm_paddr_t
284madt_search_rsdt(vm_paddr_t rsdt_paddr)
285{
286 struct acpi_rsdt *rsdt;
287 vm_paddr_t madt_paddr = 0;
288 int i, nent;
289
290 if (rsdt_paddr == 0) {
291 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
292 return 0;
293 }
294
295 rsdt = madt_sdth_map(rsdt_paddr);
296 if (rsdt == NULL) {
297 kprintf("madt_search_rsdt: can't map RSDT\n");
298 return 0;
299 }
300
301 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
302 ACPI_SDTH_SIGLEN) != 0) {
303 kprintf("madt_search_rsdt: not RSDT\n");
304 goto back;
305 }
306
307 if (rsdt->rsdt_hdr.sdth_rev != 1) {
308 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
309 rsdt->rsdt_hdr.sdth_rev);
310 goto back;
311 }
312
313 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
314 sizeof(rsdt->rsdt_ents[0]);
315 for (i = 0; i < nent; ++i) {
316 struct acpi_sdth *sdth;
317
318 if (rsdt->rsdt_ents[i] == 0)
319 continue;
320
321 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
322 if (sdth != NULL) {
323 int ret;
324
325 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
326 ACPI_SDTH_SIGLEN);
327 madt_sdth_unmap(sdth);
328
329 if (ret == 0) {
330 kprintf("madt: MADT in RSDT\n");
331 madt_paddr = rsdt->rsdt_ents[i];
332 break;
333 }
334 }
335 }
336back:
337 madt_sdth_unmap(&rsdt->rsdt_hdr);
338 return madt_paddr;
339}
340
341static int
342madt_parse(vm_paddr_t madt_paddr)
343{
344 struct acpi_madt *madt;
699b0754 345 int size, cur, error, cpu_count;
8e4c6923
MN
346
347 KKASSERT(madt_paddr != 0);
348
349 madt = madt_sdth_map(madt_paddr);
350 KKASSERT(madt != NULL);
351
352 if (madt->madt_hdr.sdth_rev != 1 && madt->madt_hdr.sdth_rev != 2) {
353 kprintf("madt_parse: unsupported MADT revision %d\n",
354 madt->madt_hdr.sdth_rev);
355 error = EOPNOTSUPP;
356 goto back;
357 }
358
359 if (madt->madt_hdr.sdth_len <
360 sizeof(*madt) - sizeof(madt->madt_ents)) {
361 kprintf("madt_parse: invalid MADT length %u\n",
362 madt->madt_hdr.sdth_len);
363 error = EINVAL;
364 goto back;
365 }
366
367 kprintf("madt: LAPIC address 0x%08x, flags %#x\n",
368 madt->madt_lapic_addr, madt->madt_flags);
699b0754 369 cpu_apic_address = madt->madt_lapic_addr;
8e4c6923
MN
370
371 size = madt->madt_hdr.sdth_len -
372 (sizeof(*madt) - sizeof(madt->madt_ents));
373 cur = 0;
374 error = 0;
699b0754 375 cpu_count = 0;
8e4c6923
MN
376
377 while (size - cur > sizeof(struct acpi_madt_ent)) {
378 const struct acpi_madt_ent *ent;
379
380 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
381 if (ent->me_len < sizeof(*ent)) {
382 kprintf("madt_parse: invalid MADT entry len %d\n",
383 ent->me_len);
384 error = EINVAL;
385 break;
386 }
387 if (ent->me_len > (size - cur)) {
388 kprintf("madt_parse: invalid MADT entry len %d, "
389 "> table length\n", ent->me_len);
390 error = EINVAL;
391 break;
392 }
393
394 cur += ent->me_len;
395
396 if (ent->me_type == MADT_ENT_LAPIC) {
397 const struct acpi_madt_lapic *lapic_ent;
398
399 if (ent->me_len < sizeof(*lapic_ent)) {
400 kprintf("madt_parse: invalid MADT lapic entry "
401 "len %d\n", ent->me_len);
402 error = EINVAL;
403 break;
404 }
405 lapic_ent = (const struct acpi_madt_lapic *)ent;
406 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
407 kprintf("madt: cpu_id %d, apic_id %d\n",
408 lapic_ent->ml_cpu_id,
409 lapic_ent->ml_apic_id);
699b0754
MN
410 mp_set_cpuids(lapic_ent->ml_cpu_id,
411 lapic_ent->ml_apic_id);
412 ++cpu_count;
8e4c6923
MN
413 }
414 }
415 }
699b0754
MN
416 if (cpu_count == 0)
417 error = EINVAL;
418 if (!error)
419 mp_naps = cpu_count - 1;
8e4c6923
MN
420back:
421 madt_sdth_unmap(&madt->madt_hdr);
422 return error;
423}