| 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 */ |
| 56 | struct 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 */ |
| 69 | struct 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 */ |
| 82 | struct acpi_xsdt { |
| 83 | struct acpi_sdth xsdt_hdr; |
| 84 | uint64_t xsdt_ents[1]; |
| 85 | } __packed; |
| 86 | |
| 87 | /* Root System Description Table */ |
| 88 | struct acpi_rsdt { |
| 89 | struct acpi_sdth rsdt_hdr; |
| 90 | uint32_t rsdt_ents[1]; |
| 91 | } __packed; |
| 92 | |
| 93 | /* Multiple APIC Description Table */ |
| 94 | struct 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 */ |
| 102 | struct 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 */ |
| 110 | struct 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 | |
| 119 | typedef vm_paddr_t (*madt_search_t)(vm_paddr_t); |
| 120 | |
| 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); |
| 127 | |
| 128 | extern u_long ebda_addr; |
| 129 | |
| 130 | vm_paddr_t |
| 131 | madt_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 0; |
| 164 | } else { |
| 165 | kprintf("madt: RSDP in BIOS mem\n"); |
| 166 | } |
| 167 | |
| 168 | found_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 0; |
| 182 | } |
| 183 | |
| 184 | /* Preliminary checks */ |
| 185 | if (madt_check(madt_paddr)) |
| 186 | return 0; |
| 187 | return madt_paddr; |
| 188 | } |
| 189 | |
| 190 | static const struct acpi_rsdp * |
| 191 | madt_rsdp_search(const uint8_t *target, int size) |
| 192 | { |
| 193 | const struct acpi_rsdp *rsdp; |
| 194 | int i; |
| 195 | |
| 196 | KKASSERT(size > sizeof(*rsdp)); |
| 197 | |
| 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) |
| 202 | return rsdp; |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | static void * |
| 208 | madt_sdth_map(vm_paddr_t paddr) |
| 209 | { |
| 210 | struct acpi_sdth *sdth; |
| 211 | vm_size_t mapsz; |
| 212 | |
| 213 | sdth = pmap_mapdev(paddr, sizeof(*sdth)); |
| 214 | mapsz = sdth->sdth_len; |
| 215 | pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth)); |
| 216 | |
| 217 | if (mapsz < sizeof(*sdth)) |
| 218 | return NULL; |
| 219 | |
| 220 | return pmap_mapdev(paddr, mapsz); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | madt_sdth_unmap(struct acpi_sdth *sdth) |
| 225 | { |
| 226 | pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len); |
| 227 | } |
| 228 | |
| 229 | static vm_paddr_t |
| 230 | madt_search_xsdt(vm_paddr_t xsdt_paddr) |
| 231 | { |
| 232 | struct acpi_xsdt *xsdt; |
| 233 | vm_paddr_t madt_paddr = 0; |
| 234 | int i, nent; |
| 235 | |
| 236 | if (xsdt_paddr == 0) { |
| 237 | kprintf("madt_search_xsdt: XSDT paddr == 0\n"); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | xsdt = madt_sdth_map(xsdt_paddr); |
| 242 | if (xsdt == NULL) { |
| 243 | kprintf("madt_search_xsdt: can't map XSDT\n"); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG, |
| 248 | ACPI_SDTH_SIGLEN) != 0) { |
| 249 | kprintf("madt_search_xsdt: not XSDT\n"); |
| 250 | goto back; |
| 251 | } |
| 252 | |
| 253 | if (xsdt->xsdt_hdr.sdth_rev != 1) { |
| 254 | kprintf("madt_search_xsdt: unsupported XSDT revision %d\n", |
| 255 | xsdt->xsdt_hdr.sdth_rev); |
| 256 | goto back; |
| 257 | } |
| 258 | |
| 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; |
| 263 | |
| 264 | if (xsdt->xsdt_ents[i] == 0) |
| 265 | continue; |
| 266 | |
| 267 | sdth = madt_sdth_map(xsdt->xsdt_ents[i]); |
| 268 | if (sdth != NULL) { |
| 269 | int ret; |
| 270 | |
| 271 | ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG, |
| 272 | ACPI_SDTH_SIGLEN); |
| 273 | madt_sdth_unmap(sdth); |
| 274 | |
| 275 | if (ret == 0) { |
| 276 | kprintf("madt: MADT in XSDT\n"); |
| 277 | madt_paddr = xsdt->xsdt_ents[i]; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | back: |
| 283 | madt_sdth_unmap(&xsdt->xsdt_hdr); |
| 284 | return madt_paddr; |
| 285 | } |
| 286 | |
| 287 | static vm_paddr_t |
| 288 | madt_search_rsdt(vm_paddr_t rsdt_paddr) |
| 289 | { |
| 290 | struct acpi_rsdt *rsdt; |
| 291 | vm_paddr_t madt_paddr = 0; |
| 292 | int i, nent; |
| 293 | |
| 294 | if (rsdt_paddr == 0) { |
| 295 | kprintf("madt_search_rsdt: RSDT paddr == 0\n"); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | rsdt = madt_sdth_map(rsdt_paddr); |
| 300 | if (rsdt == NULL) { |
| 301 | kprintf("madt_search_rsdt: can't map RSDT\n"); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG, |
| 306 | ACPI_SDTH_SIGLEN) != 0) { |
| 307 | kprintf("madt_search_rsdt: not RSDT\n"); |
| 308 | goto back; |
| 309 | } |
| 310 | |
| 311 | if (rsdt->rsdt_hdr.sdth_rev != 1) { |
| 312 | kprintf("madt_search_rsdt: unsupported RSDT revision %d\n", |
| 313 | rsdt->rsdt_hdr.sdth_rev); |
| 314 | goto back; |
| 315 | } |
| 316 | |
| 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; |
| 321 | |
| 322 | if (rsdt->rsdt_ents[i] == 0) |
| 323 | continue; |
| 324 | |
| 325 | sdth = madt_sdth_map(rsdt->rsdt_ents[i]); |
| 326 | if (sdth != NULL) { |
| 327 | int ret; |
| 328 | |
| 329 | ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG, |
| 330 | ACPI_SDTH_SIGLEN); |
| 331 | madt_sdth_unmap(sdth); |
| 332 | |
| 333 | if (ret == 0) { |
| 334 | kprintf("madt: MADT in RSDT\n"); |
| 335 | madt_paddr = rsdt->rsdt_ents[i]; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | back: |
| 341 | madt_sdth_unmap(&rsdt->rsdt_hdr); |
| 342 | return madt_paddr; |
| 343 | } |
| 344 | |
| 345 | vm_offset_t |
| 346 | madt_pass1(vm_paddr_t madt_paddr) |
| 347 | { |
| 348 | struct acpi_madt *madt; |
| 349 | vm_offset_t lapic_addr; |
| 350 | |
| 351 | KKASSERT(madt_paddr != 0); |
| 352 | |
| 353 | madt = madt_sdth_map(madt_paddr); |
| 354 | KKASSERT(madt != NULL); |
| 355 | |
| 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; |
| 359 | |
| 360 | madt_sdth_unmap(&madt->madt_hdr); |
| 361 | |
| 362 | return lapic_addr; |
| 363 | } |
| 364 | |
| 365 | int |
| 366 | madt_pass2(vm_paddr_t madt_paddr, int bsp_apic_id) |
| 367 | { |
| 368 | struct acpi_madt *madt; |
| 369 | int size, cur, error, cpu, found_bsp; |
| 370 | |
| 371 | kprintf("madt: BSP apic id %d\n", bsp_apic_id); |
| 372 | |
| 373 | KKASSERT(madt_paddr != 0); |
| 374 | |
| 375 | madt = madt_sdth_map(madt_paddr); |
| 376 | KKASSERT(madt != NULL); |
| 377 | |
| 378 | size = madt->madt_hdr.sdth_len - |
| 379 | (sizeof(*madt) - sizeof(madt->madt_ents)); |
| 380 | cur = 0; |
| 381 | error = 0; |
| 382 | found_bsp = 0; |
| 383 | cpu = 1; |
| 384 | |
| 385 | while (size - cur > sizeof(struct acpi_madt_ent)) { |
| 386 | const struct acpi_madt_ent *ent; |
| 387 | |
| 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", |
| 391 | ent->me_len); |
| 392 | error = EINVAL; |
| 393 | break; |
| 394 | } |
| 395 | if (ent->me_len > (size - cur)) { |
| 396 | kprintf("madt_pass2: invalid MADT entry len %d, " |
| 397 | "> table length\n", ent->me_len); |
| 398 | error = EINVAL; |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | cur += ent->me_len; |
| 403 | |
| 404 | if (ent->me_type == MADT_ENT_LAPIC) { |
| 405 | const struct acpi_madt_lapic *lapic_ent; |
| 406 | |
| 407 | if (ent->me_len < sizeof(*lapic_ent)) { |
| 408 | kprintf("madt_pass2: invalid MADT lapic entry " |
| 409 | "len %d\n", ent->me_len); |
| 410 | error = EINVAL; |
| 411 | break; |
| 412 | } |
| 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) { |
| 419 | mp_set_cpuids(0, |
| 420 | lapic_ent->ml_apic_id); |
| 421 | found_bsp = 1; |
| 422 | } else { |
| 423 | mp_set_cpuids(cpu, |
| 424 | lapic_ent->ml_apic_id); |
| 425 | ++cpu; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | if (!found_bsp) { |
| 431 | kprintf("madt_pass2: BSP is not found\n"); |
| 432 | error = EINVAL; |
| 433 | } |
| 434 | if (cpu == 1) { |
| 435 | kprintf("madt_pass2: no APs\n"); |
| 436 | error = EINVAL; |
| 437 | } |
| 438 | if (!error) |
| 439 | mp_naps = cpu - 1; |
| 440 | |
| 441 | madt_sdth_unmap(&madt->madt_hdr); |
| 442 | return error; |
| 443 | } |
| 444 | |
| 445 | static int |
| 446 | madt_check(vm_paddr_t madt_paddr) |
| 447 | { |
| 448 | struct acpi_madt *madt; |
| 449 | int error = 0; |
| 450 | |
| 451 | KKASSERT(madt_paddr != 0); |
| 452 | |
| 453 | madt = madt_sdth_map(madt_paddr); |
| 454 | KKASSERT(madt != NULL); |
| 455 | |
| 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); |
| 459 | error = EOPNOTSUPP; |
| 460 | goto back; |
| 461 | } |
| 462 | |
| 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); |
| 467 | error = EINVAL; |
| 468 | } |
| 469 | back: |
| 470 | madt_sdth_unmap(&madt->madt_hdr); |
| 471 | return error; |
| 472 | } |
| 473 | |
| 474 | |