ACPI MADT: When locating lapic address, take Local APIC Address Override
[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>
ee5932ac
MN
40#include <machine/md_var.h>
41#include <machine/specialreg.h>
8e4c6923
MN
42
43#define ACPI_RSDP_EBDA_MAPSZ 1024
44#define ACPI_RSDP_BIOS_MAPSZ 0x20000
45#define ACPI_RSDP_BIOS_MAPADDR 0xe0000
46
47#define ACPI_RSDP_ALIGN 16
48
49#define ACPI_RSDP_SIGLEN 8
50#define ACPI_RSDP_SIG "RSD PTR "
51
52#define ACPI_SDTH_SIGLEN 4
53#define ACPI_RSDT_SIG "RSDT"
54#define ACPI_XSDT_SIG "XSDT"
55#define ACPI_MADT_SIG "APIC"
56
57/* Root System Description Pointer */
58struct acpi_rsdp {
59 uint8_t rsdp_sig[ACPI_RSDP_SIGLEN];
60 uint8_t rsdp_cksum;
61 uint8_t rsdp_oem_id[6];
62 uint8_t rsdp_rev;
63 uint32_t rsdp_rsdt;
64 uint32_t rsdp_len;
65 uint64_t rsdp_xsdt;
66 uint8_t rsdp_ext_cksum;
67 uint8_t rsdp_rsvd[3];
68} __packed;
69
70/* System Description Table Header */
71struct acpi_sdth {
72 uint8_t sdth_sig[ACPI_SDTH_SIGLEN];
73 uint32_t sdth_len;
74 uint8_t sdth_rev;
75 uint8_t sdth_cksum;
76 uint8_t sdth_oem_id[6];
77 uint8_t sdth_oem_tbid[8];
78 uint32_t sdth_oem_rev;
79 uint32_t sdth_crt_id;
80 uint32_t sdth_crt_rev;
81} __packed;
82
83/* Extended System Description Table */
84struct acpi_xsdt {
85 struct acpi_sdth xsdt_hdr;
86 uint64_t xsdt_ents[1];
87} __packed;
88
89/* Root System Description Table */
90struct acpi_rsdt {
91 struct acpi_sdth rsdt_hdr;
92 uint32_t rsdt_ents[1];
93} __packed;
94
95/* Multiple APIC Description Table */
96struct acpi_madt {
97 struct acpi_sdth madt_hdr;
98 uint32_t madt_lapic_addr;
99 uint32_t madt_flags;
100 uint8_t madt_ents[1];
101} __packed;
102
103/* Common parts of MADT APIC structure */
104struct acpi_madt_ent {
105 uint8_t me_type; /* MADT_ENT_ */
106 uint8_t me_len;
107} __packed;
108
109#define MADT_ENT_LAPIC 0
0492bd82 110#define MADT_ENT_IOAPIC 1
ebbde674 111#define MADT_ENT_LAPIC_ADDR 5
8e4c6923
MN
112
113/* MADT Processor Local APIC */
114struct acpi_madt_lapic {
115 struct acpi_madt_ent ml_hdr;
116 uint8_t ml_cpu_id;
117 uint8_t ml_apic_id;
118 uint32_t ml_flags; /* MADT_LAPIC_ */
119} __packed;
120
121#define MADT_LAPIC_ENABLED 0x1
122
0492bd82
MN
123/* MADT I/O APIC */
124struct acpi_madt_ioapic {
125 struct acpi_madt_ent mio_hdr;
126 uint8_t mio_apic_id;
127 uint8_t mio_reserved;
128 uint32_t mio_addr;
129 uint32_t mio_gsi_base;
130} __packed;
131
ebbde674
MN
132/* MADT Local APIC Address Override */
133struct acpi_madt_lapic_addr {
134 struct acpi_madt_ent mla_hdr;
135 uint16_t mla_reserved;
136 uint64_t mla_lapic_addr;
137} __packed;
138
8e4c6923 139typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
0492bd82
MN
140typedef int (*madt_iter_t)(void *,
141 const struct acpi_madt_ent *);
8e4c6923
MN
142
143static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
144static void *madt_sdth_map(vm_paddr_t);
145static void madt_sdth_unmap(struct acpi_sdth *);
146static vm_paddr_t madt_search_xsdt(vm_paddr_t);
147static vm_paddr_t madt_search_rsdt(vm_paddr_t);
4ad5917f 148static int madt_check(vm_paddr_t);
0492bd82
MN
149static int madt_iterate_entries(struct acpi_madt *,
150 madt_iter_t, void *);
8e4c6923
MN
151
152extern u_long ebda_addr;
153
4ad5917f 154vm_paddr_t
8e4c6923
MN
155madt_probe(void)
156{
157 const struct acpi_rsdp *rsdp;
158 madt_search_t search;
159 vm_paddr_t search_paddr, madt_paddr;
160 vm_size_t mapsz;
161 uint8_t *ptr;
162
163 if (ebda_addr != 0) {
164 mapsz = ACPI_RSDP_EBDA_MAPSZ;
165 ptr = pmap_mapdev(ebda_addr, mapsz);
166
167 rsdp = madt_rsdp_search(ptr, mapsz);
168 if (rsdp == NULL) {
169 kprintf("madt: RSDP not in EBDA\n");
170 pmap_unmapdev((vm_offset_t)ptr, mapsz);
171
172 ptr = NULL;
173 mapsz = 0;
174 } else {
175 kprintf("madt: RSDP in EBDA\n");
176 goto found_rsdp;
177 }
178 }
179
180 mapsz = ACPI_RSDP_BIOS_MAPSZ;
181 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
182
183 rsdp = madt_rsdp_search(ptr, mapsz);
184 if (rsdp == NULL) {
185 kprintf("madt_probe: no RSDP\n");
186 pmap_unmapdev((vm_offset_t)ptr, mapsz);
4ad5917f 187 return 0;
8e4c6923
MN
188 } else {
189 kprintf("madt: RSDP in BIOS mem\n");
190 }
191
192found_rsdp:
193 if (rsdp->rsdp_rev != 2) {
194 search_paddr = rsdp->rsdp_rsdt;
195 search = madt_search_rsdt;
196 } else {
197 search_paddr = rsdp->rsdp_xsdt;
198 search = madt_search_xsdt;
199 }
200 pmap_unmapdev((vm_offset_t)ptr, mapsz);
201
202 madt_paddr = search(search_paddr);
203 if (madt_paddr == 0) {
204 kprintf("madt_probe: can't locate MADT\n");
4ad5917f 205 return 0;
8e4c6923 206 }
4ad5917f
MN
207
208 /* Preliminary checks */
209 if (madt_check(madt_paddr))
210 return 0;
211 return madt_paddr;
8e4c6923
MN
212}
213
214static const struct acpi_rsdp *
215madt_rsdp_search(const uint8_t *target, int size)
216{
217 const struct acpi_rsdp *rsdp;
218 int i;
219
220 KKASSERT(size > sizeof(*rsdp));
221
222 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
223 rsdp = (const struct acpi_rsdp *)&target[i];
224 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
225 ACPI_RSDP_SIGLEN) == 0)
226 return rsdp;
227 }
228 return NULL;
229}
230
231static void *
232madt_sdth_map(vm_paddr_t paddr)
233{
234 struct acpi_sdth *sdth;
235 vm_size_t mapsz;
236
237 sdth = pmap_mapdev(paddr, sizeof(*sdth));
238 mapsz = sdth->sdth_len;
239 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
240
241 if (mapsz < sizeof(*sdth))
242 return NULL;
243
244 return pmap_mapdev(paddr, mapsz);
245}
246
247static void
248madt_sdth_unmap(struct acpi_sdth *sdth)
249{
250 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
251}
252
253static vm_paddr_t
254madt_search_xsdt(vm_paddr_t xsdt_paddr)
255{
256 struct acpi_xsdt *xsdt;
257 vm_paddr_t madt_paddr = 0;
258 int i, nent;
259
260 if (xsdt_paddr == 0) {
261 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
262 return 0;
263 }
264
265 xsdt = madt_sdth_map(xsdt_paddr);
266 if (xsdt == NULL) {
267 kprintf("madt_search_xsdt: can't map XSDT\n");
268 return 0;
269 }
270
271 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
272 ACPI_SDTH_SIGLEN) != 0) {
273 kprintf("madt_search_xsdt: not XSDT\n");
274 goto back;
275 }
276
277 if (xsdt->xsdt_hdr.sdth_rev != 1) {
278 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
279 xsdt->xsdt_hdr.sdth_rev);
280 goto back;
281 }
282
283 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
284 sizeof(xsdt->xsdt_ents[0]);
285 for (i = 0; i < nent; ++i) {
286 struct acpi_sdth *sdth;
287
288 if (xsdt->xsdt_ents[i] == 0)
289 continue;
290
291 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
292 if (sdth != NULL) {
293 int ret;
294
295 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
296 ACPI_SDTH_SIGLEN);
297 madt_sdth_unmap(sdth);
298
299 if (ret == 0) {
300 kprintf("madt: MADT in XSDT\n");
301 madt_paddr = xsdt->xsdt_ents[i];
302 break;
303 }
304 }
305 }
306back:
307 madt_sdth_unmap(&xsdt->xsdt_hdr);
308 return madt_paddr;
309}
310
311static vm_paddr_t
312madt_search_rsdt(vm_paddr_t rsdt_paddr)
313{
314 struct acpi_rsdt *rsdt;
315 vm_paddr_t madt_paddr = 0;
316 int i, nent;
317
318 if (rsdt_paddr == 0) {
319 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
320 return 0;
321 }
322
323 rsdt = madt_sdth_map(rsdt_paddr);
324 if (rsdt == NULL) {
325 kprintf("madt_search_rsdt: can't map RSDT\n");
326 return 0;
327 }
328
329 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
330 ACPI_SDTH_SIGLEN) != 0) {
331 kprintf("madt_search_rsdt: not RSDT\n");
332 goto back;
333 }
334
335 if (rsdt->rsdt_hdr.sdth_rev != 1) {
336 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
337 rsdt->rsdt_hdr.sdth_rev);
338 goto back;
339 }
340
341 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
342 sizeof(rsdt->rsdt_ents[0]);
343 for (i = 0; i < nent; ++i) {
344 struct acpi_sdth *sdth;
345
346 if (rsdt->rsdt_ents[i] == 0)
347 continue;
348
349 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
350 if (sdth != NULL) {
351 int ret;
352
353 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
354 ACPI_SDTH_SIGLEN);
355 madt_sdth_unmap(sdth);
356
357 if (ret == 0) {
358 kprintf("madt: MADT in RSDT\n");
359 madt_paddr = rsdt->rsdt_ents[i];
360 break;
361 }
362 }
363 }
364back:
365 madt_sdth_unmap(&rsdt->rsdt_hdr);
366 return madt_paddr;
367}
368
ebbde674
MN
369static int
370madt_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
371{
372 const struct acpi_madt_lapic_addr *lapic_addr_ent;
373 uint64_t *addr64 = xarg;
374
375 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
376 return 0;
377 if (ent->me_len < sizeof(*lapic_addr_ent)) {
378 kprintf("madt_pass1: invalid LAPIC address override length\n");
379 return 0;
380 }
381 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
382
383 *addr64 = lapic_addr_ent->mla_lapic_addr;
384 return 0;
385}
386
4ad5917f
MN
387vm_offset_t
388madt_pass1(vm_paddr_t madt_paddr)
8e4c6923
MN
389{
390 struct acpi_madt *madt;
4ad5917f 391 vm_offset_t lapic_addr;
ebbde674
MN
392 uint64_t lapic_addr64;
393 int error;
8e4c6923
MN
394
395 KKASSERT(madt_paddr != 0);
396
397 madt = madt_sdth_map(madt_paddr);
398 KKASSERT(madt != NULL);
399
8e4c6923
MN
400 kprintf("madt: LAPIC address 0x%08x, flags %#x\n",
401 madt->madt_lapic_addr, madt->madt_flags);
4ad5917f
MN
402 lapic_addr = madt->madt_lapic_addr;
403
ebbde674
MN
404 lapic_addr64 = 0;
405 error = madt_iterate_entries(madt, madt_pass1_callback, &lapic_addr64);
406 if (error)
407 panic("madt_iterate_entries(pass1) failed\n");
408
409 if (lapic_addr64 != 0) {
410 kprintf("Warning: 64bits lapic address 0x%llx\n", lapic_addr64);
411 /* XXX vm_offset_t is 32bits on i386 */
412 lapic_addr = lapic_addr64;
413 }
414
4ad5917f
MN
415 madt_sdth_unmap(&madt->madt_hdr);
416
417 return lapic_addr;
418}
419
420int
421madt_pass2(vm_paddr_t madt_paddr, int bsp_apic_id)
422{
423 struct acpi_madt *madt;
424 int size, cur, error, cpu, found_bsp;
425
426 kprintf("madt: BSP apic id %d\n", bsp_apic_id);
427
428 KKASSERT(madt_paddr != 0);
429
430 madt = madt_sdth_map(madt_paddr);
431 KKASSERT(madt != NULL);
8e4c6923
MN
432
433 size = madt->madt_hdr.sdth_len -
434 (sizeof(*madt) - sizeof(madt->madt_ents));
435 cur = 0;
436 error = 0;
4ad5917f
MN
437 found_bsp = 0;
438 cpu = 1;
8e4c6923
MN
439
440 while (size - cur > sizeof(struct acpi_madt_ent)) {
441 const struct acpi_madt_ent *ent;
442
443 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
444 if (ent->me_len < sizeof(*ent)) {
4ad5917f 445 kprintf("madt_pass2: invalid MADT entry len %d\n",
8e4c6923
MN
446 ent->me_len);
447 error = EINVAL;
448 break;
449 }
450 if (ent->me_len > (size - cur)) {
4ad5917f 451 kprintf("madt_pass2: invalid MADT entry len %d, "
8e4c6923
MN
452 "> table length\n", ent->me_len);
453 error = EINVAL;
454 break;
455 }
456
457 cur += ent->me_len;
458
459 if (ent->me_type == MADT_ENT_LAPIC) {
460 const struct acpi_madt_lapic *lapic_ent;
461
462 if (ent->me_len < sizeof(*lapic_ent)) {
4ad5917f 463 kprintf("madt_pass2: invalid MADT lapic entry "
8e4c6923
MN
464 "len %d\n", ent->me_len);
465 error = EINVAL;
466 break;
467 }
468 lapic_ent = (const struct acpi_madt_lapic *)ent;
469 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
470 kprintf("madt: cpu_id %d, apic_id %d\n",
471 lapic_ent->ml_cpu_id,
472 lapic_ent->ml_apic_id);
4ad5917f
MN
473 if (lapic_ent->ml_apic_id == bsp_apic_id) {
474 mp_set_cpuids(0,
475 lapic_ent->ml_apic_id);
476 found_bsp = 1;
477 } else {
478 mp_set_cpuids(cpu,
479 lapic_ent->ml_apic_id);
480 ++cpu;
481 }
8e4c6923
MN
482 }
483 }
484 }
4ad5917f
MN
485 if (!found_bsp) {
486 kprintf("madt_pass2: BSP is not found\n");
487 error = EINVAL;
488 }
489 if (cpu == 1) {
490 kprintf("madt_pass2: no APs\n");
699b0754 491 error = EINVAL;
4ad5917f 492 }
699b0754 493 if (!error)
4ad5917f
MN
494 mp_naps = cpu - 1;
495
496 madt_sdth_unmap(&madt->madt_hdr);
497 return error;
498}
499
0492bd82
MN
500struct madt_check_cbarg {
501 int cpu_count;
ee5932ac
MN
502 int bsp_found;
503 int bsp_apic_id;
0492bd82
MN
504};
505
506static int
507madt_check_callback(void *xarg, const struct acpi_madt_ent *ent)
508{
509 struct madt_check_cbarg *arg = xarg;
510 const struct acpi_madt_lapic *lapic_ent;
511
512 if (ent->me_type != MADT_ENT_LAPIC)
513 return 0;
514 lapic_ent = (const struct acpi_madt_lapic *)ent;
515
ee5932ac 516 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
0492bd82 517 arg->cpu_count++;
ee5932ac
MN
518 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
519 if (arg->bsp_found) {
520 kprintf("madt_check: more than one BSP?\n");
521 return EINVAL;
522 }
523 arg->bsp_found = 1;
524 }
525 }
0492bd82
MN
526 return 0;
527}
528
4ad5917f
MN
529static int
530madt_check(vm_paddr_t madt_paddr)
531{
0492bd82 532 struct madt_check_cbarg arg;
4ad5917f
MN
533 struct acpi_madt *madt;
534 int error = 0;
535
536 KKASSERT(madt_paddr != 0);
537
538 madt = madt_sdth_map(madt_paddr);
539 KKASSERT(madt != NULL);
540
541 if (madt->madt_hdr.sdth_rev != 1 && madt->madt_hdr.sdth_rev != 2) {
542 kprintf("madt_check: unsupported MADT revision %d\n",
543 madt->madt_hdr.sdth_rev);
544 error = EOPNOTSUPP;
545 goto back;
546 }
547
548 if (madt->madt_hdr.sdth_len <
549 sizeof(*madt) - sizeof(madt->madt_ents)) {
550 kprintf("madt_check: invalid MADT length %u\n",
551 madt->madt_hdr.sdth_len);
552 error = EINVAL;
0492bd82
MN
553 goto back;
554 }
555
556 bzero(&arg, sizeof(arg));
ee5932ac
MN
557 arg.bsp_apic_id = (cpu_procinfo & CPUID_LOCAL_APIC_ID) >> 24;
558
0492bd82
MN
559 error = madt_iterate_entries(madt, madt_check_callback, &arg);
560 if (!error) {
ee5932ac
MN
561 if (arg.cpu_count <= 1) {
562 kprintf("madt_check: less than 2 CPUs is found\n");
0492bd82 563 error = EOPNOTSUPP;
ee5932ac
MN
564 } else if (!arg.bsp_found) {
565 kprintf("madt_check: no BSP\n");
566 error = EINVAL;
567 }
4ad5917f 568 }
8e4c6923
MN
569back:
570 madt_sdth_unmap(&madt->madt_hdr);
571 return error;
572}
4ad5917f 573
0492bd82
MN
574static int
575madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
576{
577 int size, cur, error;
578
579 size = madt->madt_hdr.sdth_len -
580 (sizeof(*madt) - sizeof(madt->madt_ents));
581 cur = 0;
582 error = 0;
583
584 while (size - cur > sizeof(struct acpi_madt_ent)) {
585 const struct acpi_madt_ent *ent;
586
587 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
588 if (ent->me_len < sizeof(*ent)) {
589 kprintf("madt_iterate_entries: invalid MADT "
590 "entry len %d\n", ent->me_len);
591 error = EINVAL;
592 break;
593 }
594 if (ent->me_len > (size - cur)) {
595 kprintf("madt_iterate_entries: invalid MADT "
596 "entry len %d, > table length\n", ent->me_len);
597 error = EINVAL;
598 break;
599 }
600
601 cur += ent->me_len;
602
603 /*
604 * Only Local APIC and I/O APIC are defined in
605 * ACPI specification 1.0 - 3.0
606 */
607 switch (ent->me_type) {
608 case MADT_ENT_LAPIC:
609 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
610 kprintf("madt_iterate_entries: invalid MADT "
611 "lapic entry len %d\n", ent->me_len);
612 error = EINVAL;
613 }
614 break;
615
616 case MADT_ENT_IOAPIC:
617 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
618 kprintf("madt_iterate_entries: invalid MADT "
619 "ioapic entry len %d\n", ent->me_len);
620 error = EINVAL;
621 }
622 break;
623 }
624 if (error)
625 break;
626
627 error = func(arg, ent);
628 if (error)
629 break;
630 }
631 return error;
632}
633
4ad5917f 634