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 MADT_VPRINTF(fmt, arg...) \
48 kprintf("ACPI MADT: " fmt , ##arg); \
51 #define ACPI_RSDP_EBDA_MAPSZ 1024
52 #define ACPI_RSDP_BIOS_MAPSZ 0x20000
53 #define ACPI_RSDP_BIOS_MAPADDR 0xe0000
55 #define ACPI_RSDP_ALIGN 16
57 #define ACPI_RSDP_SIGLEN 8
58 #define ACPI_RSDP_SIG "RSD PTR "
60 #define ACPI_SDTH_SIGLEN 4
61 #define ACPI_RSDT_SIG "RSDT"
62 #define ACPI_XSDT_SIG "XSDT"
63 #define ACPI_MADT_SIG "APIC"
65 /* Root System Description Pointer */
67 uint8_t rsdp_sig[ACPI_RSDP_SIGLEN];
69 uint8_t rsdp_oem_id[6];
74 uint8_t rsdp_ext_cksum;
78 /* System Description Table Header */
80 uint8_t sdth_sig[ACPI_SDTH_SIGLEN];
84 uint8_t sdth_oem_id[6];
85 uint8_t sdth_oem_tbid[8];
86 uint32_t sdth_oem_rev;
88 uint32_t sdth_crt_rev;
91 /* Extended System Description Table */
93 struct acpi_sdth xsdt_hdr;
94 uint64_t xsdt_ents[1];
97 /* Root System Description Table */
99 struct acpi_sdth rsdt_hdr;
100 uint32_t rsdt_ents[1];
103 /* Multiple APIC Description Table */
105 struct acpi_sdth madt_hdr;
106 uint32_t madt_lapic_addr;
108 uint8_t madt_ents[1];
111 /* Common parts of MADT APIC structure */
112 struct acpi_madt_ent {
113 uint8_t me_type; /* MADT_ENT_ */
117 #define MADT_ENT_LAPIC 0
118 #define MADT_ENT_IOAPIC 1
119 #define MADT_ENT_INTSRC_OVR 2
120 #define MADT_ENT_LAPIC_ADDR 5
122 /* MADT Processor Local APIC */
123 struct acpi_madt_lapic {
124 struct acpi_madt_ent ml_hdr;
127 uint32_t ml_flags; /* MADT_LAPIC_ */
130 #define MADT_LAPIC_ENABLED 0x1
133 struct acpi_madt_ioapic {
134 struct acpi_madt_ent mio_hdr;
136 uint8_t mio_reserved;
138 uint32_t mio_gsi_base;
141 /* MADT Interrupt Source Override */
142 struct acpi_madt_intsrc_ovr {
143 struct acpi_madt_ent miso_hdr;
147 uint16_t miso_flags; /* MADT_ISO_ */
150 #define MADT_ISO_POLA_MASK 0x3
151 #define MADT_ISO_POLA_SHIFT 0
152 #define MADT_ISO_POLA_CONFORM 0
153 #define MADT_ISO_POLA_HIGH 1
154 #define MADT_ISO_POLA_RSVD 2
155 #define MADT_ISO_POLA_LOW 3
156 #define MADT_ISO_TRIG_MASK 0xc
157 #define MADT_ISO_TRIG_SHIFT 2
158 #define MADT_ISO_TRIG_CONFORM 0
159 #define MADT_ISO_TRIG_EDGE 1
160 #define MADT_ISO_TRIG_RSVD 2
161 #define MADT_ISO_TRIG_LEVEL 3
163 /* MADT Local APIC Address Override */
164 struct acpi_madt_lapic_addr {
165 struct acpi_madt_ent mla_hdr;
166 uint16_t mla_reserved;
167 uint64_t mla_lapic_addr;
170 typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
171 typedef int (*madt_iter_t)(void *,
172 const struct acpi_madt_ent *);
174 static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
175 static void *madt_sdth_map(vm_paddr_t);
176 static void madt_sdth_unmap(struct acpi_sdth *);
177 static vm_paddr_t madt_search_xsdt(vm_paddr_t);
178 static vm_paddr_t madt_search_rsdt(vm_paddr_t);
179 static int madt_check(vm_paddr_t);
180 static int madt_iterate_entries(struct acpi_madt *,
181 madt_iter_t, void *);
183 static vm_offset_t madt_lapic_pass1(void);
184 static int madt_lapic_pass2(int);
186 static void madt_lapic_enumerate(struct lapic_enumerator *);
187 static int madt_lapic_probe(struct lapic_enumerator *);
189 extern u_long ebda_addr;
191 static vm_paddr_t madt_phyaddr;
196 const struct acpi_rsdp *rsdp;
197 madt_search_t search;
198 vm_paddr_t search_paddr, madt_paddr;
202 KKASSERT(madt_phyaddr == 0);
204 if (ebda_addr != 0) {
205 mapsz = ACPI_RSDP_EBDA_MAPSZ;
206 ptr = pmap_mapdev(ebda_addr, mapsz);
208 rsdp = madt_rsdp_search(ptr, mapsz);
210 MADT_VPRINTF("RSDP not in EBDA\n");
211 pmap_unmapdev((vm_offset_t)ptr, mapsz);
216 MADT_VPRINTF("RSDP in EBDA\n");
221 mapsz = ACPI_RSDP_BIOS_MAPSZ;
222 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
224 rsdp = madt_rsdp_search(ptr, mapsz);
226 kprintf("madt_probe: no RSDP\n");
227 pmap_unmapdev((vm_offset_t)ptr, mapsz);
230 MADT_VPRINTF("RSDP in BIOS mem\n");
234 if (rsdp->rsdp_rev != 2) {
235 search_paddr = rsdp->rsdp_rsdt;
236 search = madt_search_rsdt;
238 search_paddr = rsdp->rsdp_xsdt;
239 search = madt_search_xsdt;
241 pmap_unmapdev((vm_offset_t)ptr, mapsz);
243 madt_paddr = search(search_paddr);
244 if (madt_paddr == 0) {
245 kprintf("madt_probe: can't locate MADT\n");
249 /* Preliminary checks */
250 if (madt_check(madt_paddr)) {
251 kprintf("madt_probe: madt_check failed\n");
255 madt_phyaddr = madt_paddr;
257 SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, madt_probe, 0);
259 static const struct acpi_rsdp *
260 madt_rsdp_search(const uint8_t *target, int size)
262 const struct acpi_rsdp *rsdp;
265 KKASSERT(size > sizeof(*rsdp));
267 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
268 rsdp = (const struct acpi_rsdp *)&target[i];
269 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
270 ACPI_RSDP_SIGLEN) == 0)
277 madt_sdth_map(vm_paddr_t paddr)
279 struct acpi_sdth *sdth;
282 sdth = pmap_mapdev(paddr, sizeof(*sdth));
283 mapsz = sdth->sdth_len;
284 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
286 if (mapsz < sizeof(*sdth))
289 return pmap_mapdev(paddr, mapsz);
293 madt_sdth_unmap(struct acpi_sdth *sdth)
295 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
299 madt_search_xsdt(vm_paddr_t xsdt_paddr)
301 struct acpi_xsdt *xsdt;
302 vm_paddr_t madt_paddr = 0;
305 if (xsdt_paddr == 0) {
306 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
310 xsdt = madt_sdth_map(xsdt_paddr);
312 kprintf("madt_search_xsdt: can't map XSDT\n");
316 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
317 ACPI_SDTH_SIGLEN) != 0) {
318 kprintf("madt_search_xsdt: not XSDT\n");
322 if (xsdt->xsdt_hdr.sdth_rev != 1) {
323 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
324 xsdt->xsdt_hdr.sdth_rev);
328 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
329 sizeof(xsdt->xsdt_ents[0]);
330 for (i = 0; i < nent; ++i) {
331 struct acpi_sdth *sdth;
333 if (xsdt->xsdt_ents[i] == 0)
336 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
340 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
342 madt_sdth_unmap(sdth);
345 MADT_VPRINTF("MADT in XSDT\n");
346 madt_paddr = xsdt->xsdt_ents[i];
352 madt_sdth_unmap(&xsdt->xsdt_hdr);
357 madt_search_rsdt(vm_paddr_t rsdt_paddr)
359 struct acpi_rsdt *rsdt;
360 vm_paddr_t madt_paddr = 0;
363 if (rsdt_paddr == 0) {
364 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
368 rsdt = madt_sdth_map(rsdt_paddr);
370 kprintf("madt_search_rsdt: can't map RSDT\n");
374 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
375 ACPI_SDTH_SIGLEN) != 0) {
376 kprintf("madt_search_rsdt: not RSDT\n");
380 if (rsdt->rsdt_hdr.sdth_rev != 1) {
381 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
382 rsdt->rsdt_hdr.sdth_rev);
386 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
387 sizeof(rsdt->rsdt_ents[0]);
388 for (i = 0; i < nent; ++i) {
389 struct acpi_sdth *sdth;
391 if (rsdt->rsdt_ents[i] == 0)
394 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
398 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
400 madt_sdth_unmap(sdth);
403 MADT_VPRINTF("MADT in RSDT\n");
404 madt_paddr = rsdt->rsdt_ents[i];
410 madt_sdth_unmap(&rsdt->rsdt_hdr);
415 madt_check(vm_paddr_t madt_paddr)
417 struct acpi_madt *madt;
420 KKASSERT(madt_paddr != 0);
422 madt = madt_sdth_map(madt_paddr);
423 KKASSERT(madt != NULL);
425 if (madt->madt_hdr.sdth_rev != 1 && madt->madt_hdr.sdth_rev != 2) {
426 kprintf("madt_check: unsupported MADT revision %d\n",
427 madt->madt_hdr.sdth_rev);
432 if (madt->madt_hdr.sdth_len <
433 sizeof(*madt) - sizeof(madt->madt_ents)) {
434 kprintf("madt_check: invalid MADT length %u\n",
435 madt->madt_hdr.sdth_len);
440 madt_sdth_unmap(&madt->madt_hdr);
445 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
447 int size, cur, error;
449 size = madt->madt_hdr.sdth_len -
450 (sizeof(*madt) - sizeof(madt->madt_ents));
454 while (size - cur > sizeof(struct acpi_madt_ent)) {
455 const struct acpi_madt_ent *ent;
457 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
458 if (ent->me_len < sizeof(*ent)) {
459 kprintf("madt_iterate_entries: invalid MADT "
460 "entry len %d\n", ent->me_len);
464 if (ent->me_len > (size - cur)) {
465 kprintf("madt_iterate_entries: invalid MADT "
466 "entry len %d, > table length\n", ent->me_len);
474 * Only Local APIC, I/O APIC and Interrupt Source Override
475 * are defined in ACPI specification 1.0 - 4.0
477 switch (ent->me_type) {
479 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
480 kprintf("madt_iterate_entries: invalid MADT "
481 "lapic entry len %d\n", ent->me_len);
486 case MADT_ENT_IOAPIC:
487 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
488 kprintf("madt_iterate_entries: invalid MADT "
489 "ioapic entry len %d\n", ent->me_len);
494 case MADT_ENT_INTSRC_OVR:
495 if (ent->me_len < sizeof(struct acpi_madt_intsrc_ovr)) {
496 kprintf("madt_iterate_entries: invalid MADT "
497 "intsrc_ovr entry len %d\n",
506 error = func(arg, ent);
514 madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
516 const struct acpi_madt_lapic_addr *lapic_addr_ent;
517 uint64_t *addr64 = xarg;
519 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
521 if (ent->me_len < sizeof(*lapic_addr_ent)) {
522 kprintf("madt_lapic_pass1: "
523 "invalid LAPIC address override length\n");
526 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
528 *addr64 = lapic_addr_ent->mla_lapic_addr;
533 madt_lapic_pass1(void)
535 struct acpi_madt *madt;
536 vm_offset_t lapic_addr;
537 uint64_t lapic_addr64;
540 KKASSERT(madt_phyaddr != 0);
542 madt = madt_sdth_map(madt_phyaddr);
543 KKASSERT(madt != NULL);
545 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
546 madt->madt_lapic_addr, madt->madt_flags);
547 lapic_addr = madt->madt_lapic_addr;
550 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
553 panic("madt_iterate_entries(pass1) failed\n");
555 if (lapic_addr64 != 0) {
556 kprintf("Warning: 64bits lapic address 0x%llx\n", lapic_addr64);
557 /* XXX vm_offset_t is 32bits on i386 */
558 lapic_addr = lapic_addr64;
561 madt_sdth_unmap(&madt->madt_hdr);
566 struct madt_lapic_pass2_cbarg {
573 madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
575 const struct acpi_madt_lapic *lapic_ent;
576 struct madt_lapic_pass2_cbarg *arg = xarg;
578 if (ent->me_type != MADT_ENT_LAPIC)
581 lapic_ent = (const struct acpi_madt_lapic *)ent;
582 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
583 MADT_VPRINTF("cpu_id %d, apic_id %d\n",
584 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
585 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
586 mp_set_cpuids(0, lapic_ent->ml_apic_id);
589 mp_set_cpuids(arg->cpu, lapic_ent->ml_apic_id);
597 madt_lapic_pass2(int bsp_apic_id)
599 struct acpi_madt *madt;
600 struct madt_lapic_pass2_cbarg arg;
603 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
605 KKASSERT(madt_phyaddr != 0);
607 madt = madt_sdth_map(madt_phyaddr);
608 KKASSERT(madt != NULL);
610 bzero(&arg, sizeof(arg));
612 arg.bsp_apic_id = bsp_apic_id;
614 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
616 panic("madt_iterate_entries(pass2) failed\n");
618 KKASSERT(arg.bsp_found);
619 KKASSERT(arg.cpu > 1);
620 mp_naps = arg.cpu - 1; /* exclude BSP */
622 madt_sdth_unmap(&madt->madt_hdr);
627 struct madt_lapic_probe_cbarg {
629 vm_offset_t lapic_addr;
633 madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
635 struct madt_lapic_probe_cbarg *arg = xarg;
637 if (ent->me_type == MADT_ENT_LAPIC) {
638 const struct acpi_madt_lapic *lapic_ent;
640 lapic_ent = (const struct acpi_madt_lapic *)ent;
641 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
643 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
644 const struct acpi_madt_lapic_addr *lapic_addr_ent;
646 if (ent->me_len < sizeof(*lapic_addr_ent)) {
647 kprintf("madt_lapic_probe: "
648 "invalid LAPIC address override length\n");
651 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
653 if (lapic_addr_ent->mla_lapic_addr != 0)
654 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
660 madt_lapic_probe(struct lapic_enumerator *e)
662 struct madt_lapic_probe_cbarg arg;
663 struct acpi_madt *madt;
666 if (madt_phyaddr == 0)
669 madt = madt_sdth_map(madt_phyaddr);
670 KKASSERT(madt != NULL);
672 bzero(&arg, sizeof(arg));
673 arg.lapic_addr = madt->madt_lapic_addr;
675 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
677 if (arg.cpu_count <= 1) {
678 kprintf("madt_lapic_probe: "
679 "less than 2 CPUs is found\n");
682 if (arg.lapic_addr == 0) {
683 kprintf("madt_lapic_probe: zero LAPIC address\n");
688 madt_sdth_unmap(&madt->madt_hdr);
693 madt_lapic_enumerate(struct lapic_enumerator *e)
695 vm_offset_t lapic_addr;
698 KKASSERT(madt_phyaddr != 0);
700 lapic_addr = madt_lapic_pass1();
702 panic("madt_lapic_enumerate: no local apic\n");
704 lapic_map(lapic_addr);
706 bsp_apic_id = APIC_ID(lapic.id);
707 if (madt_lapic_pass2(bsp_apic_id))
708 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
711 static struct lapic_enumerator madt_lapic_enumerator = {
712 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
713 .lapic_probe = madt_lapic_probe,
714 .lapic_enumerate = madt_lapic_enumerate
718 madt_lapic_enum_register(void)
722 prio = LAPIC_ENUM_PRIO_MADT;
723 kgetenv_int("hw.madt_lapic_prio", &prio);
724 madt_lapic_enumerator.lapic_prio = prio;
726 lapic_enumerator_register(&madt_lapic_enumerator);
728 SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);