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