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/systm.h>
38 #include <machine/pmap.h>
39 #include <machine/smp.h>
41 #define ACPI_RSDP_EBDA_MAPSZ 1024
42 #define ACPI_RSDP_BIOS_MAPSZ 0x20000
43 #define ACPI_RSDP_BIOS_MAPADDR 0xe0000
45 #define ACPI_RSDP_ALIGN 16
47 #define ACPI_RSDP_SIGLEN 8
48 #define ACPI_RSDP_SIG "RSD PTR "
50 #define ACPI_SDTH_SIGLEN 4
51 #define ACPI_RSDT_SIG "RSDT"
52 #define ACPI_XSDT_SIG "XSDT"
53 #define ACPI_MADT_SIG "APIC"
55 /* Root System Description Pointer */
57 uint8_t rsdp_sig[ACPI_RSDP_SIGLEN];
59 uint8_t rsdp_oem_id[6];
64 uint8_t rsdp_ext_cksum;
68 /* System Description Table Header */
70 uint8_t sdth_sig[ACPI_SDTH_SIGLEN];
74 uint8_t sdth_oem_id[6];
75 uint8_t sdth_oem_tbid[8];
76 uint32_t sdth_oem_rev;
78 uint32_t sdth_crt_rev;
81 /* Extended System Description Table */
83 struct acpi_sdth xsdt_hdr;
84 uint64_t xsdt_ents[1];
87 /* Root System Description Table */
89 struct acpi_sdth rsdt_hdr;
90 uint32_t rsdt_ents[1];
93 /* Multiple APIC Description Table */
95 struct acpi_sdth madt_hdr;
96 uint32_t madt_lapic_addr;
101 /* Common parts of MADT APIC structure */
102 struct acpi_madt_ent {
103 uint8_t me_type; /* MADT_ENT_ */
107 #define MADT_ENT_LAPIC 0
109 /* MADT Processor Local APIC */
110 struct acpi_madt_lapic {
111 struct acpi_madt_ent ml_hdr;
114 uint32_t ml_flags; /* MADT_LAPIC_ */
117 #define MADT_LAPIC_ENABLED 0x1
119 typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
121 static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
122 static void *madt_sdth_map(vm_paddr_t);
123 static void madt_sdth_unmap(struct acpi_sdth *);
124 static vm_paddr_t madt_search_xsdt(vm_paddr_t);
125 static vm_paddr_t madt_search_rsdt(vm_paddr_t);
126 static int madt_check(vm_paddr_t);
128 extern u_long ebda_addr;
133 const struct acpi_rsdp *rsdp;
134 madt_search_t search;
135 vm_paddr_t search_paddr, madt_paddr;
139 if (ebda_addr != 0) {
140 mapsz = ACPI_RSDP_EBDA_MAPSZ;
141 ptr = pmap_mapdev(ebda_addr, mapsz);
143 rsdp = madt_rsdp_search(ptr, mapsz);
145 kprintf("madt: RSDP not in EBDA\n");
146 pmap_unmapdev((vm_offset_t)ptr, mapsz);
151 kprintf("madt: RSDP in EBDA\n");
156 mapsz = ACPI_RSDP_BIOS_MAPSZ;
157 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
159 rsdp = madt_rsdp_search(ptr, mapsz);
161 kprintf("madt_probe: no RSDP\n");
162 pmap_unmapdev((vm_offset_t)ptr, mapsz);
165 kprintf("madt: RSDP in BIOS mem\n");
169 if (rsdp->rsdp_rev != 2) {
170 search_paddr = rsdp->rsdp_rsdt;
171 search = madt_search_rsdt;
173 search_paddr = rsdp->rsdp_xsdt;
174 search = madt_search_xsdt;
176 pmap_unmapdev((vm_offset_t)ptr, mapsz);
178 madt_paddr = search(search_paddr);
179 if (madt_paddr == 0) {
180 kprintf("madt_probe: can't locate MADT\n");
184 /* Preliminary checks */
185 if (madt_check(madt_paddr))
190 static const struct acpi_rsdp *
191 madt_rsdp_search(const uint8_t *target, int size)
193 const struct acpi_rsdp *rsdp;
196 KKASSERT(size > sizeof(*rsdp));
198 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
199 rsdp = (const struct acpi_rsdp *)&target[i];
200 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
201 ACPI_RSDP_SIGLEN) == 0)
208 madt_sdth_map(vm_paddr_t paddr)
210 struct acpi_sdth *sdth;
213 sdth = pmap_mapdev(paddr, sizeof(*sdth));
214 mapsz = sdth->sdth_len;
215 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
217 if (mapsz < sizeof(*sdth))
220 return pmap_mapdev(paddr, mapsz);
224 madt_sdth_unmap(struct acpi_sdth *sdth)
226 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
230 madt_search_xsdt(vm_paddr_t xsdt_paddr)
232 struct acpi_xsdt *xsdt;
233 vm_paddr_t madt_paddr = 0;
236 if (xsdt_paddr == 0) {
237 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
241 xsdt = madt_sdth_map(xsdt_paddr);
243 kprintf("madt_search_xsdt: can't map XSDT\n");
247 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
248 ACPI_SDTH_SIGLEN) != 0) {
249 kprintf("madt_search_xsdt: not XSDT\n");
253 if (xsdt->xsdt_hdr.sdth_rev != 1) {
254 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
255 xsdt->xsdt_hdr.sdth_rev);
259 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
260 sizeof(xsdt->xsdt_ents[0]);
261 for (i = 0; i < nent; ++i) {
262 struct acpi_sdth *sdth;
264 if (xsdt->xsdt_ents[i] == 0)
267 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
271 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
273 madt_sdth_unmap(sdth);
276 kprintf("madt: MADT in XSDT\n");
277 madt_paddr = xsdt->xsdt_ents[i];
283 madt_sdth_unmap(&xsdt->xsdt_hdr);
288 madt_search_rsdt(vm_paddr_t rsdt_paddr)
290 struct acpi_rsdt *rsdt;
291 vm_paddr_t madt_paddr = 0;
294 if (rsdt_paddr == 0) {
295 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
299 rsdt = madt_sdth_map(rsdt_paddr);
301 kprintf("madt_search_rsdt: can't map RSDT\n");
305 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
306 ACPI_SDTH_SIGLEN) != 0) {
307 kprintf("madt_search_rsdt: not RSDT\n");
311 if (rsdt->rsdt_hdr.sdth_rev != 1) {
312 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
313 rsdt->rsdt_hdr.sdth_rev);
317 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
318 sizeof(rsdt->rsdt_ents[0]);
319 for (i = 0; i < nent; ++i) {
320 struct acpi_sdth *sdth;
322 if (rsdt->rsdt_ents[i] == 0)
325 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
329 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
331 madt_sdth_unmap(sdth);
334 kprintf("madt: MADT in RSDT\n");
335 madt_paddr = rsdt->rsdt_ents[i];
341 madt_sdth_unmap(&rsdt->rsdt_hdr);
346 madt_pass1(vm_paddr_t madt_paddr)
348 struct acpi_madt *madt;
349 vm_offset_t lapic_addr;
351 KKASSERT(madt_paddr != 0);
353 madt = madt_sdth_map(madt_paddr);
354 KKASSERT(madt != NULL);
356 kprintf("madt: LAPIC address 0x%08x, flags %#x\n",
357 madt->madt_lapic_addr, madt->madt_flags);
358 lapic_addr = madt->madt_lapic_addr;
360 madt_sdth_unmap(&madt->madt_hdr);
366 madt_pass2(vm_paddr_t madt_paddr, int bsp_apic_id)
368 struct acpi_madt *madt;
369 int size, cur, error, cpu, found_bsp;
371 kprintf("madt: BSP apic id %d\n", bsp_apic_id);
373 KKASSERT(madt_paddr != 0);
375 madt = madt_sdth_map(madt_paddr);
376 KKASSERT(madt != NULL);
378 size = madt->madt_hdr.sdth_len -
379 (sizeof(*madt) - sizeof(madt->madt_ents));
385 while (size - cur > sizeof(struct acpi_madt_ent)) {
386 const struct acpi_madt_ent *ent;
388 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
389 if (ent->me_len < sizeof(*ent)) {
390 kprintf("madt_pass2: invalid MADT entry len %d\n",
395 if (ent->me_len > (size - cur)) {
396 kprintf("madt_pass2: invalid MADT entry len %d, "
397 "> table length\n", ent->me_len);
404 if (ent->me_type == MADT_ENT_LAPIC) {
405 const struct acpi_madt_lapic *lapic_ent;
407 if (ent->me_len < sizeof(*lapic_ent)) {
408 kprintf("madt_pass2: invalid MADT lapic entry "
409 "len %d\n", ent->me_len);
413 lapic_ent = (const struct acpi_madt_lapic *)ent;
414 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
415 kprintf("madt: cpu_id %d, apic_id %d\n",
416 lapic_ent->ml_cpu_id,
417 lapic_ent->ml_apic_id);
418 if (lapic_ent->ml_apic_id == bsp_apic_id) {
420 lapic_ent->ml_apic_id);
424 lapic_ent->ml_apic_id);
431 kprintf("madt_pass2: BSP is not found\n");
435 kprintf("madt_pass2: no APs\n");
441 madt_sdth_unmap(&madt->madt_hdr);
446 madt_check(vm_paddr_t madt_paddr)
448 struct acpi_madt *madt;
451 KKASSERT(madt_paddr != 0);
453 madt = madt_sdth_map(madt_paddr);
454 KKASSERT(madt != NULL);
456 if (madt->madt_hdr.sdth_rev != 1 && madt->madt_hdr.sdth_rev != 2) {
457 kprintf("madt_check: unsupported MADT revision %d\n",
458 madt->madt_hdr.sdth_rev);
463 if (madt->madt_hdr.sdth_len <
464 sizeof(*madt) - sizeof(madt->madt_ents)) {
465 kprintf("madt_check: invalid MADT length %u\n",
466 madt->madt_hdr.sdth_len);
470 madt_sdth_unmap(&madt->madt_hdr);