2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
39 #include <machine/pmap.h>
40 #include <machine/smp.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
43 #include <machine_base/apic/mpapic.h>
45 #define ACPI_RSDP_EBDA_MAPSZ 1024
46 #define ACPI_RSDP_BIOS_MAPSZ 0x20000
47 #define ACPI_RSDP_BIOS_MAPADDR 0xe0000
49 #define ACPI_RSDP_ALIGN 16
51 #define ACPI_RSDP_SIGLEN 8
52 #define ACPI_RSDP_SIG "RSD PTR "
54 #define ACPI_SDTH_SIGLEN 4
55 #define ACPI_RSDT_SIG "RSDT"
56 #define ACPI_XSDT_SIG "XSDT"
57 #define ACPI_MADT_SIG "APIC"
59 /* Root System Description Pointer */
61 uint8_t rsdp_sig[ACPI_RSDP_SIGLEN];
63 uint8_t rsdp_oem_id[6];
68 uint8_t rsdp_ext_cksum;
72 /* System Description Table Header */
74 uint8_t sdth_sig[ACPI_SDTH_SIGLEN];
78 uint8_t sdth_oem_id[6];
79 uint8_t sdth_oem_tbid[8];
80 uint32_t sdth_oem_rev;
82 uint32_t sdth_crt_rev;
85 /* Extended System Description Table */
87 struct acpi_sdth xsdt_hdr;
88 uint64_t xsdt_ents[1];
91 /* Root System Description Table */
93 struct acpi_sdth rsdt_hdr;
94 uint32_t rsdt_ents[1];
97 /* Multiple APIC Description Table */
99 struct acpi_sdth madt_hdr;
100 uint32_t madt_lapic_addr;
102 uint8_t madt_ents[1];
105 /* Common parts of MADT APIC structure */
106 struct acpi_madt_ent {
107 uint8_t me_type; /* MADT_ENT_ */
111 #define MADT_ENT_LAPIC 0
112 #define MADT_ENT_IOAPIC 1
113 #define MADT_ENT_LAPIC_ADDR 5
115 /* MADT Processor Local APIC */
116 struct acpi_madt_lapic {
117 struct acpi_madt_ent ml_hdr;
120 uint32_t ml_flags; /* MADT_LAPIC_ */
123 #define MADT_LAPIC_ENABLED 0x1
126 struct acpi_madt_ioapic {
127 struct acpi_madt_ent mio_hdr;
129 uint8_t mio_reserved;
131 uint32_t mio_gsi_base;
134 /* MADT Local APIC Address Override */
135 struct acpi_madt_lapic_addr {
136 struct acpi_madt_ent mla_hdr;
137 uint16_t mla_reserved;
138 uint64_t mla_lapic_addr;
141 struct madt_lapic_enumerator {
142 struct lapic_enumerator enumerator;
143 vm_paddr_t madt_paddr;
146 typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
147 typedef int (*madt_iter_t)(void *,
148 const struct acpi_madt_ent *);
150 static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
151 static void *madt_sdth_map(vm_paddr_t);
152 static void madt_sdth_unmap(struct acpi_sdth *);
153 static vm_paddr_t madt_search_xsdt(vm_paddr_t);
154 static vm_paddr_t madt_search_rsdt(vm_paddr_t);
155 static int madt_check(vm_paddr_t);
156 static int madt_iterate_entries(struct acpi_madt *,
157 madt_iter_t, void *);
159 static vm_paddr_t madt_probe(void);
160 static vm_offset_t madt_pass1(vm_paddr_t);
161 static int madt_pass2(vm_paddr_t, int);
163 static void madt_lapic_enumerate(struct lapic_enumerator *);
164 static int madt_lapic_probe(struct lapic_enumerator *);
166 extern u_long ebda_addr;
171 const struct acpi_rsdp *rsdp;
172 madt_search_t search;
173 vm_paddr_t search_paddr, madt_paddr;
177 if (ebda_addr != 0) {
178 mapsz = ACPI_RSDP_EBDA_MAPSZ;
179 ptr = pmap_mapdev(ebda_addr, mapsz);
181 rsdp = madt_rsdp_search(ptr, mapsz);
183 kprintf("madt: RSDP not in EBDA\n");
184 pmap_unmapdev((vm_offset_t)ptr, mapsz);
189 kprintf("madt: RSDP in EBDA\n");
194 mapsz = ACPI_RSDP_BIOS_MAPSZ;
195 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
197 rsdp = madt_rsdp_search(ptr, mapsz);
199 kprintf("madt_probe: no RSDP\n");
200 pmap_unmapdev((vm_offset_t)ptr, mapsz);
203 kprintf("madt: RSDP in BIOS mem\n");
207 if (rsdp->rsdp_rev != 2) {
208 search_paddr = rsdp->rsdp_rsdt;
209 search = madt_search_rsdt;
211 search_paddr = rsdp->rsdp_xsdt;
212 search = madt_search_xsdt;
214 pmap_unmapdev((vm_offset_t)ptr, mapsz);
216 madt_paddr = search(search_paddr);
217 if (madt_paddr == 0) {
218 kprintf("madt_probe: can't locate MADT\n");
222 /* Preliminary checks */
223 if (madt_check(madt_paddr))
228 static const struct acpi_rsdp *
229 madt_rsdp_search(const uint8_t *target, int size)
231 const struct acpi_rsdp *rsdp;
234 KKASSERT(size > sizeof(*rsdp));
236 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
237 rsdp = (const struct acpi_rsdp *)&target[i];
238 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
239 ACPI_RSDP_SIGLEN) == 0)
246 madt_sdth_map(vm_paddr_t paddr)
248 struct acpi_sdth *sdth;
251 sdth = pmap_mapdev(paddr, sizeof(*sdth));
252 mapsz = sdth->sdth_len;
253 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
255 if (mapsz < sizeof(*sdth))
258 return pmap_mapdev(paddr, mapsz);
262 madt_sdth_unmap(struct acpi_sdth *sdth)
264 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
268 madt_search_xsdt(vm_paddr_t xsdt_paddr)
270 struct acpi_xsdt *xsdt;
271 vm_paddr_t madt_paddr = 0;
274 if (xsdt_paddr == 0) {
275 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
279 xsdt = madt_sdth_map(xsdt_paddr);
281 kprintf("madt_search_xsdt: can't map XSDT\n");
285 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
286 ACPI_SDTH_SIGLEN) != 0) {
287 kprintf("madt_search_xsdt: not XSDT\n");
291 if (xsdt->xsdt_hdr.sdth_rev != 1) {
292 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
293 xsdt->xsdt_hdr.sdth_rev);
297 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
298 sizeof(xsdt->xsdt_ents[0]);
299 for (i = 0; i < nent; ++i) {
300 struct acpi_sdth *sdth;
302 if (xsdt->xsdt_ents[i] == 0)
305 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
309 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
311 madt_sdth_unmap(sdth);
314 kprintf("madt: MADT in XSDT\n");
315 madt_paddr = xsdt->xsdt_ents[i];
321 madt_sdth_unmap(&xsdt->xsdt_hdr);
326 madt_search_rsdt(vm_paddr_t rsdt_paddr)
328 struct acpi_rsdt *rsdt;
329 vm_paddr_t madt_paddr = 0;
332 if (rsdt_paddr == 0) {
333 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
337 rsdt = madt_sdth_map(rsdt_paddr);
339 kprintf("madt_search_rsdt: can't map RSDT\n");
343 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
344 ACPI_SDTH_SIGLEN) != 0) {
345 kprintf("madt_search_rsdt: not RSDT\n");
349 if (rsdt->rsdt_hdr.sdth_rev != 1) {
350 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
351 rsdt->rsdt_hdr.sdth_rev);
355 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
356 sizeof(rsdt->rsdt_ents[0]);
357 for (i = 0; i < nent; ++i) {
358 struct acpi_sdth *sdth;
360 if (rsdt->rsdt_ents[i] == 0)
363 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
367 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
369 madt_sdth_unmap(sdth);
372 kprintf("madt: MADT in RSDT\n");
373 madt_paddr = rsdt->rsdt_ents[i];
379 madt_sdth_unmap(&rsdt->rsdt_hdr);
384 madt_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
386 const struct acpi_madt_lapic_addr *lapic_addr_ent;
387 uint64_t *addr64 = xarg;
389 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
391 if (ent->me_len < sizeof(*lapic_addr_ent)) {
392 kprintf("madt_pass1: invalid LAPIC address override length\n");
395 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
397 *addr64 = lapic_addr_ent->mla_lapic_addr;
402 madt_pass1(vm_paddr_t madt_paddr)
404 struct acpi_madt *madt;
405 vm_offset_t lapic_addr;
406 uint64_t lapic_addr64;
409 KKASSERT(madt_paddr != 0);
411 madt = madt_sdth_map(madt_paddr);
412 KKASSERT(madt != NULL);
414 kprintf("madt: LAPIC address 0x%08x, flags %#x\n",
415 madt->madt_lapic_addr, madt->madt_flags);
416 lapic_addr = madt->madt_lapic_addr;
419 error = madt_iterate_entries(madt, madt_pass1_callback, &lapic_addr64);
421 panic("madt_iterate_entries(pass1) failed\n");
423 if (lapic_addr64 != 0) {
424 kprintf("Warning: 64bits lapic address 0x%llx\n", lapic_addr64);
425 /* XXX vm_offset_t is 32bits on i386 */
426 lapic_addr = lapic_addr64;
429 madt_sdth_unmap(&madt->madt_hdr);
434 struct madt_pass2_cbarg {
441 madt_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
443 const struct acpi_madt_lapic *lapic_ent;
444 struct madt_pass2_cbarg *arg = xarg;
446 if (ent->me_type != MADT_ENT_LAPIC)
449 lapic_ent = (const struct acpi_madt_lapic *)ent;
450 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
451 kprintf("madt: cpu_id %d, apic_id %d\n",
452 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
453 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
454 mp_set_cpuids(0, lapic_ent->ml_apic_id);
457 mp_set_cpuids(arg->cpu, lapic_ent->ml_apic_id);
465 madt_pass2(vm_paddr_t madt_paddr, int bsp_apic_id)
467 struct acpi_madt *madt;
468 struct madt_pass2_cbarg arg;
471 kprintf("madt: BSP apic id %d\n", bsp_apic_id);
473 KKASSERT(madt_paddr != 0);
475 madt = madt_sdth_map(madt_paddr);
476 KKASSERT(madt != NULL);
478 bzero(&arg, sizeof(arg));
480 arg.bsp_apic_id = bsp_apic_id;
482 error = madt_iterate_entries(madt, madt_pass2_callback, &arg);
484 panic("madt_iterate_entries(pass2) failed\n");
486 KKASSERT(arg.bsp_found);
487 KKASSERT(arg.cpu > 1);
488 mp_naps = arg.cpu - 1; /* exclude BSP */
490 madt_sdth_unmap(&madt->madt_hdr);
495 struct madt_check_cbarg {
500 madt_check_callback(void *xarg, const struct acpi_madt_ent *ent)
502 struct madt_check_cbarg *arg = xarg;
503 const struct acpi_madt_lapic *lapic_ent;
505 if (ent->me_type != MADT_ENT_LAPIC)
507 lapic_ent = (const struct acpi_madt_lapic *)ent;
509 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
515 madt_check(vm_paddr_t madt_paddr)
517 struct madt_check_cbarg arg;
518 struct acpi_madt *madt;
521 KKASSERT(madt_paddr != 0);
523 madt = madt_sdth_map(madt_paddr);
524 KKASSERT(madt != NULL);
526 if (madt->madt_hdr.sdth_rev != 1 && madt->madt_hdr.sdth_rev != 2) {
527 kprintf("madt_check: unsupported MADT revision %d\n",
528 madt->madt_hdr.sdth_rev);
533 if (madt->madt_hdr.sdth_len <
534 sizeof(*madt) - sizeof(madt->madt_ents)) {
535 kprintf("madt_check: invalid MADT length %u\n",
536 madt->madt_hdr.sdth_len);
541 bzero(&arg, sizeof(arg));
543 error = madt_iterate_entries(madt, madt_check_callback, &arg);
545 if (arg.cpu_count <= 1) {
546 kprintf("madt_check: less than 2 CPUs is found\n");
551 madt_sdth_unmap(&madt->madt_hdr);
556 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
558 int size, cur, error;
560 size = madt->madt_hdr.sdth_len -
561 (sizeof(*madt) - sizeof(madt->madt_ents));
565 while (size - cur > sizeof(struct acpi_madt_ent)) {
566 const struct acpi_madt_ent *ent;
568 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
569 if (ent->me_len < sizeof(*ent)) {
570 kprintf("madt_iterate_entries: invalid MADT "
571 "entry len %d\n", ent->me_len);
575 if (ent->me_len > (size - cur)) {
576 kprintf("madt_iterate_entries: invalid MADT "
577 "entry len %d, > table length\n", ent->me_len);
585 * Only Local APIC and I/O APIC are defined in
586 * ACPI specification 1.0 - 3.0
588 switch (ent->me_type) {
590 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
591 kprintf("madt_iterate_entries: invalid MADT "
592 "lapic entry len %d\n", ent->me_len);
597 case MADT_ENT_IOAPIC:
598 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
599 kprintf("madt_iterate_entries: invalid MADT "
600 "ioapic entry len %d\n", ent->me_len);
608 error = func(arg, ent);
616 madt_lapic_probe(struct lapic_enumerator *e)
618 vm_paddr_t madt_paddr;
620 madt_paddr = madt_probe();
624 ((struct madt_lapic_enumerator *)e)->madt_paddr = madt_paddr;
629 madt_lapic_enumerate(struct lapic_enumerator *e)
631 vm_paddr_t madt_paddr;
632 vm_offset_t lapic_addr;
635 madt_paddr = ((struct madt_lapic_enumerator *)e)->madt_paddr;
636 KKASSERT(madt_paddr != 0);
638 lapic_addr = madt_pass1(madt_paddr);
640 panic("madt_lapic_enumerate no local apic\n");
642 lapic_init(lapic_addr);
644 bsp_apic_id = APIC_ID(lapic.id);
645 if (madt_pass2(madt_paddr, bsp_apic_id))
646 panic("mp_enable: madt_pass2 failed\n");
649 static struct madt_lapic_enumerator madt_lapic_enumerator = {
651 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
652 .lapic_probe = madt_lapic_probe,
653 .lapic_enumerate = madt_lapic_enumerate
658 madt_apic_register(void)
662 prio = LAPIC_ENUM_PRIO_MADT;
663 kgetenv_int("hw.madt_lapic_prio", &prio);
664 madt_lapic_enumerator.enumerator.lapic_prio = prio;
666 lapic_enumerator_register(&madt_lapic_enumerator.enumerator);
668 SYSINIT(madt, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_apic_register, 0);