| 1 |
/* acpi_parse_apic.h - ACPI-MADT table parser. Source file |
| 2 |
Copyright (C) 2018 Juan Bosco Garcia |
| 3 |
Copyright (C) 2019 2020 Almudena Garcia Jurado-Centurion |
| 4 |
Written by Juan Bosco Garcia and Almudena Garcia Jurado-Centurion |
| 5 |
|
| 6 |
This file is part of Min_SMP. |
| 7 |
|
| 8 |
Min_SMP is free software; you can redistribute it and/or modify it |
| 9 |
under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
Min_SMP is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ |
| 21 |
|
| 22 |
#include <string.h> /* memcmp, memcpy... */ |
| 23 |
|
| 24 |
#include <stdint.h> /* uint16_t, uint32_t... */ |
| 25 |
|
| 26 |
#include <mach/machine.h> /* machine_slot */ |
| 27 |
|
| 28 |
#include <kern/printf.h> /* printf */ |
| 29 |
#include <kern/debug.h> |
| 30 |
#include <i386/vm_param.h> /* phystokv */ |
| 31 |
#include <i386/apic.h> /* lapic, ioapic... */ |
| 32 |
#include <i386at/acpi_parse_apic.h> |
| 33 |
#include <vm/vm_kern.h> |
| 34 |
|
| 35 |
static struct acpi_apic *apic_madt = NULL; |
| 36 |
unsigned lapic_addr; |
| 37 |
uint32_t *hpet_addr; |
| 38 |
|
| 39 |
/* |
| 40 |
* acpi_print_info: shows by screen the ACPI's rsdp and rsdt virtual address |
| 41 |
* and the number of entries stored in RSDT table. |
| 42 |
* |
| 43 |
* Receives as input the references of RSDP and RSDT tables, |
| 44 |
* and the number of entries stored in RSDT. |
| 45 |
*/ |
| 46 |
void |
| 47 |
acpi_print_info(phys_addr_t rsdp, void *rsdt, int acpi_rsdt_n) |
| 48 |
{ |
| 49 |
|
| 50 |
printf("ACPI:\n"); |
| 51 |
printf(" rsdp = 0x%llx\n", (unsigned long long) rsdp); |
| 52 |
printf(" rsdt/xsdt = 0x%p (n = %d)\n", rsdt, acpi_rsdt_n); |
| 53 |
} |
| 54 |
|
| 55 |
/* |
| 56 |
* acpi_checksum: calculates the checksum of an ACPI table. |
| 57 |
* Receives as input the virtual address of the table. |
| 58 |
* |
| 59 |
* Returns 0 if success, other value if error. |
| 60 |
*/ |
| 61 |
static uint8_t |
| 62 |
acpi_checksum(void *addr, uint32_t length) |
| 63 |
{ |
| 64 |
uint8_t *bytes = addr; |
| 65 |
uint8_t checksum = 0; |
| 66 |
unsigned int i; |
| 67 |
|
| 68 |
/* Sum all bytes of addr */ |
| 69 |
for (i = 0; i < length; i++) |
| 70 |
checksum += bytes[i]; |
| 71 |
|
| 72 |
return checksum; |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* acpi_check_signature: check if a signature match with the signature of its table. |
| 77 |
* |
| 78 |
* Receive as parameter both signatures: table signature, the signature which needs to check, |
| 79 |
* and real signature, the genuine signature of the table. |
| 80 |
* |
| 81 |
* Return 0 if success, other if error. |
| 82 |
*/ |
| 83 |
|
| 84 |
static int |
| 85 |
acpi_check_signature(const uint8_t table_signature[], const char *real_signature, uint8_t length) |
| 86 |
{ |
| 87 |
return memcmp(table_signature, real_signature, length); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/* |
| 92 |
* acpi_check_rsdp: |
| 93 |
* check if the RDSP "candidate" table is the real RSDP table. |
| 94 |
* |
| 95 |
* Compare the table signature with the ACPI signature for this table |
| 96 |
* and check is the checksum is correct. |
| 97 |
* |
| 98 |
* Receives as input the reference of RSDT table. |
| 99 |
* |
| 100 |
* Preconditions: RSDP pointer must not be NULL. |
| 101 |
* |
| 102 |
* Returns 1 if ACPI 1.0 and sets sdt_base |
| 103 |
* Returns 2 if ACPI >= 2.0 and sets sdt_base |
| 104 |
*/ |
| 105 |
static int8_t |
| 106 |
acpi_check_rsdp(struct acpi_rsdp2 *rsdp, phys_addr_t *sdt_base) |
| 107 |
{ |
| 108 |
int is_rsdp; |
| 109 |
uint8_t cksum; |
| 110 |
|
| 111 |
/* Check if rsdp signature match with the ACPI RSDP signature. */ |
| 112 |
is_rsdp = acpi_check_signature(rsdp->v1.signature, ACPI_RSDP_SIG, 8*sizeof(uint8_t)); |
| 113 |
|
| 114 |
if (is_rsdp != ACPI_SUCCESS) |
| 115 |
return ACPI_BAD_SIGNATURE; |
| 116 |
|
| 117 |
if (rsdp->v1.revision == 0) { |
| 118 |
// ACPI 1.0 |
| 119 |
*sdt_base = rsdp->v1.rsdt_addr; |
| 120 |
printf("ACPI v1.0\n"); |
| 121 |
cksum = acpi_checksum((void *)(&rsdp->v1), sizeof(struct acpi_rsdp)); |
| 122 |
|
| 123 |
if (cksum != 0) |
| 124 |
return ACPI_BAD_CHECKSUM; |
| 125 |
|
| 126 |
return 1; |
| 127 |
|
| 128 |
} else if (rsdp->v1.revision == 2) { |
| 129 |
// ACPI >= 2.0 |
| 130 |
*sdt_base = rsdp->xsdt_addr; |
| 131 |
printf("ACPI >= v2.0\n"); |
| 132 |
cksum = acpi_checksum((void *)rsdp, sizeof(struct acpi_rsdp2)); |
| 133 |
|
| 134 |
if (cksum != 0) |
| 135 |
return ACPI_BAD_CHECKSUM; |
| 136 |
|
| 137 |
return 2; |
| 138 |
} |
| 139 |
|
| 140 |
return ACPI_NO_RSDP; |
| 141 |
} |
| 142 |
|
| 143 |
/* |
| 144 |
* acpi_check_rsdp_align: check if the RSDP address is aligned. |
| 145 |
* Preconditions: The address must not be NULL |
| 146 |
* |
| 147 |
* Returns ACPI_SUCCESS (0) if success, ACPI_BAD_ALIGN if error |
| 148 |
*/ |
| 149 |
|
| 150 |
static int8_t |
| 151 |
acpi_check_rsdp_align(void *addr) |
| 152 |
{ |
| 153 |
/* check alignment. */ |
| 154 |
if ((uintptr_t)addr & (ACPI_RSDP_ALIGN-1)) |
| 155 |
return ACPI_BAD_ALIGN; |
| 156 |
|
| 157 |
return ACPI_SUCCESS; |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* acpi_search_rsdp: search the rsdp table in a memory range. |
| 162 |
* |
| 163 |
* Receives as input the initial virtual address, and the lenght |
| 164 |
* of memory range. |
| 165 |
* |
| 166 |
* Preconditions: The start address (addr) must be aligned. |
| 167 |
* |
| 168 |
* Returns the physical address of rsdp structure if success, 0 if failure. |
| 169 |
*/ |
| 170 |
static phys_addr_t |
| 171 |
acpi_search_rsdp(void *addr, uint32_t length, int *is_64bit) |
| 172 |
{ |
| 173 |
void *end; |
| 174 |
int version = 0; |
| 175 |
phys_addr_t sdt_base = 0; |
| 176 |
|
| 177 |
/* Search RDSP in memory space between addr and addr+lenght. */ |
| 178 |
for (end = addr+length; addr < end; addr += ACPI_RSDP_ALIGN) { |
| 179 |
|
| 180 |
/* Check if the current memory block stores the RDSP. */ |
| 181 |
if ((addr != NULL) && ((version = acpi_check_rsdp(addr, &sdt_base)) > 0)) { |
| 182 |
/* If yes, return RSDT/XSDT address */ |
| 183 |
*is_64bit = (version == 2); |
| 184 |
return sdt_base; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
/* |
| 192 |
* acpi_get_rsdp: tries to find the RSDP table, |
| 193 |
* searching It in many memory ranges, as It's written in ACPI Specification. |
| 194 |
* |
| 195 |
* Returns the reference to RDSP structure if success, 0 if failure. |
| 196 |
*/ |
| 197 |
static phys_addr_t |
| 198 |
acpi_get_rsdp(int *is_64bit) |
| 199 |
{ |
| 200 |
uint16_t *start = 0; |
| 201 |
phys_addr_t base = 0; |
| 202 |
phys_addr_t rsdp = 0; |
| 203 |
|
| 204 |
/* EDBA start address. */ |
| 205 |
start = (uint16_t*) phystokv(0x040e); |
| 206 |
base = phystokv((*start) << 4); /* address = paragraph number * 16 */ |
| 207 |
|
| 208 |
/* check alignment. */ |
| 209 |
if (acpi_check_rsdp_align((void *)base) == ACPI_BAD_ALIGN) |
| 210 |
return 0; |
| 211 |
rsdp = acpi_search_rsdp((void *)base, 1024, is_64bit); |
| 212 |
|
| 213 |
if (rsdp == 0) { |
| 214 |
/* If RSDP isn't in EDBA, search in the BIOS read-only memory space between 0E0000h and 0FFFFFh */ |
| 215 |
rsdp = acpi_search_rsdp((void *)phystokv(0xe0000), 0x100000 - 0x0e0000, is_64bit); |
| 216 |
} |
| 217 |
|
| 218 |
return rsdp; |
| 219 |
} |
| 220 |
|
| 221 |
/* |
| 222 |
* acpi_get_rsdt: Get RSDT table reference from RSDP entries. |
| 223 |
* |
| 224 |
* Receives as input a reference for RSDP table |
| 225 |
* and a reference to store the number of entries of RSDT. |
| 226 |
* |
| 227 |
* Returns the reference to RSDT table if success, NULL if error. |
| 228 |
*/ |
| 229 |
static struct acpi_rsdt* |
| 230 |
acpi_get_rsdt(phys_addr_t rsdp_phys, int* acpi_rsdt_n) |
| 231 |
{ |
| 232 |
struct acpi_rsdt *rsdt = NULL; |
| 233 |
int signature_check; |
| 234 |
|
| 235 |
rsdt = (struct acpi_rsdt*) kmem_map_aligned_table(rsdp_phys, sizeof(struct acpi_rsdt), VM_PROT_READ); |
| 236 |
|
| 237 |
/* Check if the RSDT mapping is fine. */ |
| 238 |
if (rsdt == NULL) |
| 239 |
return NULL; |
| 240 |
|
| 241 |
/* Check is rsdt signature is equals to ACPI RSDT signature. */ |
| 242 |
signature_check = acpi_check_signature(rsdt->header.signature, ACPI_RSDT_SIG, |
| 243 |
4*sizeof(uint8_t)); |
| 244 |
|
| 245 |
if (signature_check != ACPI_SUCCESS) |
| 246 |
return NULL; |
| 247 |
|
| 248 |
/* Calculated number of elements stored in rsdt. */ |
| 249 |
*acpi_rsdt_n = (rsdt->header.length - sizeof(rsdt->header)) |
| 250 |
/ sizeof(rsdt->entry[0]); |
| 251 |
|
| 252 |
return rsdt; |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* acpi_get_xsdt: Get XSDT table reference from RSDPv2 entries. |
| 257 |
* |
| 258 |
* Receives as input a reference for RSDPv2 table |
| 259 |
* and a reference to store the number of entries of XSDT. |
| 260 |
* |
| 261 |
* Returns the reference to XSDT table if success, NULL if error. |
| 262 |
*/ |
| 263 |
static struct acpi_xsdt* |
| 264 |
acpi_get_xsdt(phys_addr_t rsdp_phys, int* acpi_xsdt_n) |
| 265 |
{ |
| 266 |
struct acpi_xsdt *xsdt = NULL; |
| 267 |
int signature_check; |
| 268 |
|
| 269 |
xsdt = (struct acpi_xsdt*) kmem_map_aligned_table(rsdp_phys, sizeof(struct acpi_xsdt), VM_PROT_READ); |
| 270 |
|
| 271 |
/* Check if the RSDT mapping is fine. */ |
| 272 |
if (xsdt == NULL) |
| 273 |
return NULL; |
| 274 |
|
| 275 |
/* Check is rsdt signature is equals to ACPI RSDT signature. */ |
| 276 |
signature_check = acpi_check_signature(xsdt->header.signature, ACPI_XSDT_SIG, |
| 277 |
4*sizeof(uint8_t)); |
| 278 |
|
| 279 |
if (signature_check != ACPI_SUCCESS) |
| 280 |
return NULL; |
| 281 |
|
| 282 |
/* Calculated number of elements stored in rsdt. */ |
| 283 |
*acpi_xsdt_n = (xsdt->header.length - sizeof(xsdt->header)) |
| 284 |
/ sizeof(xsdt->entry[0]); |
| 285 |
|
| 286 |
return xsdt; |
| 287 |
} |
| 288 |
|
| 289 |
/* |
| 290 |
* acpi_get_apic: get MADT/APIC table from RSDT entries. |
| 291 |
* |
| 292 |
* Receives as input the RSDT initial address, |
| 293 |
* and the number of entries of RSDT table. |
| 294 |
* |
| 295 |
* Returns a reference to APIC/MADT table if success, NULL if failure. |
| 296 |
* Also sets hpet_addr to base address of HPET. |
| 297 |
*/ |
| 298 |
static struct acpi_apic* |
| 299 |
acpi_get_apic(struct acpi_rsdt *rsdt, int acpi_rsdt_n) |
| 300 |
{ |
| 301 |
struct acpi_dhdr *descr_header; |
| 302 |
struct acpi_apic *madt = NULL; |
| 303 |
int check_signature; |
| 304 |
uint64_t map_addr; |
| 305 |
|
| 306 |
/* Search APIC entries in rsdt table. */ |
| 307 |
for (int i = 0; i < acpi_rsdt_n; i++) { |
| 308 |
descr_header = (struct acpi_dhdr*) kmem_map_aligned_table(rsdt->entry[i], sizeof(struct acpi_dhdr), |
| 309 |
VM_PROT_READ); |
| 310 |
|
| 311 |
/* Check if the entry is a MADT */ |
| 312 |
check_signature = acpi_check_signature(descr_header->signature, ACPI_APIC_SIG, 4*sizeof(uint8_t)); |
| 313 |
if (check_signature == ACPI_SUCCESS) |
| 314 |
madt = (struct acpi_apic*) descr_header; |
| 315 |
|
| 316 |
/* Check if the entry is a HPET */ |
| 317 |
check_signature = acpi_check_signature(descr_header->signature, ACPI_HPET_SIG, 4*sizeof(uint8_t)); |
| 318 |
if (check_signature == ACPI_SUCCESS) { |
| 319 |
map_addr = ((struct acpi_hpet *)descr_header)->address.addr64; |
| 320 |
assert (map_addr != 0); |
| 321 |
hpet_addr = (uint32_t *)kmem_map_aligned_table(map_addr, 1024, VM_PROT_READ | VM_PROT_WRITE); |
| 322 |
printf("HPET at physical address 0x%llx\n", map_addr); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
return madt; |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* acpi_get_apic2: get MADT/APIC table from XSDT entries. |
| 331 |
* |
| 332 |
* Receives as input the XSDT initial address, |
| 333 |
* and the number of entries of XSDT table. |
| 334 |
* |
| 335 |
* Returns a reference to APIC/MADT table if success, NULL if failure. |
| 336 |
* Also sets hpet_addr to base address of HPET. |
| 337 |
*/ |
| 338 |
static struct acpi_apic* |
| 339 |
acpi_get_apic2(struct acpi_xsdt *xsdt, int acpi_xsdt_n) |
| 340 |
{ |
| 341 |
struct acpi_dhdr *descr_header; |
| 342 |
struct acpi_apic *madt = NULL; |
| 343 |
int check_signature; |
| 344 |
uint64_t map_addr; |
| 345 |
|
| 346 |
/* Search APIC entries in rsdt table. */ |
| 347 |
for (int i = 0; i < acpi_xsdt_n; i++) { |
| 348 |
descr_header = (struct acpi_dhdr*) kmem_map_aligned_table(xsdt->entry[i], sizeof(struct acpi_dhdr), |
| 349 |
VM_PROT_READ); |
| 350 |
|
| 351 |
/* Check if the entry is an APIC. */ |
| 352 |
check_signature = acpi_check_signature(descr_header->signature, ACPI_APIC_SIG, 4*sizeof(uint8_t)); |
| 353 |
if (check_signature == ACPI_SUCCESS) |
| 354 |
madt = (struct acpi_apic *)descr_header; |
| 355 |
|
| 356 |
/* Check if the entry is a HPET. */ |
| 357 |
check_signature = acpi_check_signature(descr_header->signature, ACPI_HPET_SIG, 4*sizeof(uint8_t)); |
| 358 |
if (check_signature == ACPI_SUCCESS) { |
| 359 |
map_addr = ((struct acpi_hpet *)descr_header)->address.addr64; |
| 360 |
assert (map_addr != 0); |
| 361 |
hpet_addr = (uint32_t *)kmem_map_aligned_table(map_addr, 1024, VM_PROT_READ | VM_PROT_WRITE); |
| 362 |
printf("HPET at physical address 0x%llx\n", map_addr); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
return madt; |
| 367 |
} |
| 368 |
|
| 369 |
/* |
| 370 |
* acpi_add_lapic: add a new Local APIC to cpu_to_lapic array |
| 371 |
* and increase the number of cpus. |
| 372 |
* |
| 373 |
* Receives as input the Local APIC entry in MADT/APIC table. |
| 374 |
*/ |
| 375 |
static void |
| 376 |
acpi_apic_add_lapic(struct acpi_apic_lapic *lapic_entry) |
| 377 |
{ |
| 378 |
/* If cpu flag is correct */ |
| 379 |
if (lapic_entry->flags & (ACPI_LAPIC_FLAG_ENABLED | ACPI_LAPIC_FLAG_CAPABLE)) { |
| 380 |
/* Add cpu to processors' list. */ |
| 381 |
apic_add_cpu(lapic_entry->apic_id & apic_id_mask); |
| 382 |
} |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
/* |
| 387 |
* apic_add_ioapic: add a new IOAPIC to IOAPICS array |
| 388 |
* and increase the number of IOAPIC. |
| 389 |
* |
| 390 |
* Receives as input the IOAPIC entry in MADT/APIC table. |
| 391 |
*/ |
| 392 |
|
| 393 |
static void |
| 394 |
acpi_apic_add_ioapic(struct acpi_apic_ioapic *ioapic_entry) |
| 395 |
{ |
| 396 |
IoApicData io_apic; |
| 397 |
|
| 398 |
/* Fill IOAPIC structure with its main fields */ |
| 399 |
io_apic.apic_id = ioapic_entry->apic_id; |
| 400 |
io_apic.addr = ioapic_entry->addr; |
| 401 |
io_apic.gsi_base = ioapic_entry->gsi_base; |
| 402 |
io_apic.ioapic = (ApicIoUnit *)kmem_map_aligned_table(ioapic_entry->addr, |
| 403 |
sizeof(ApicIoUnit), |
| 404 |
VM_PROT_READ | VM_PROT_WRITE); |
| 405 |
io_apic.ioapic->select.r = APIC_IO_VERSION; |
| 406 |
io_apic.ngsis = ((io_apic.ioapic->window.r >> APIC_IO_ENTRIES_SHIFT) & 0xff) + 1; |
| 407 |
|
| 408 |
/* Insert IOAPIC in the list. */ |
| 409 |
apic_add_ioapic(io_apic); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
/* |
| 414 |
* apic_add_ioapic: add a new IOAPIC to IOAPICS list |
| 415 |
* and increase the number of IOAPIC. |
| 416 |
* |
| 417 |
* Receives as input the IOAPIC entry in MADT/APIC table. |
| 418 |
*/ |
| 419 |
|
| 420 |
static void |
| 421 |
acpi_apic_add_irq_override(struct acpi_apic_irq_override* irq_override) |
| 422 |
{ |
| 423 |
IrqOverrideData irq_over; |
| 424 |
|
| 425 |
/* Fills IRQ override structure with its fields */ |
| 426 |
irq_over.bus = irq_override->bus; |
| 427 |
irq_over.irq = irq_override->irq; |
| 428 |
irq_over.gsi = irq_override->gsi; |
| 429 |
irq_over.flags = irq_override->flags; |
| 430 |
|
| 431 |
/* Insert IRQ override in the list */ |
| 432 |
apic_add_irq_override(irq_over); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
/* |
| 437 |
* apic_parse_table: parse the MADT/APIC table. |
| 438 |
* |
| 439 |
* Read the APIC/MADT table entry to entry, |
| 440 |
* registering the APIC structures (Local APIC, IOAPIC or IRQ override) entries in their lists. |
| 441 |
*/ |
| 442 |
|
| 443 |
static int |
| 444 |
acpi_apic_parse_table(struct acpi_apic *apic) |
| 445 |
{ |
| 446 |
struct acpi_apic_dhdr *apic_entry = NULL; |
| 447 |
vm_offset_t end = 0; |
| 448 |
uint8_t numcpus = 1; |
| 449 |
|
| 450 |
/* Get the address of first APIC entry */ |
| 451 |
apic_entry = (struct acpi_apic_dhdr*) apic->entry; |
| 452 |
|
| 453 |
/* Get the end address of APIC table */ |
| 454 |
end = (vm_offset_t) apic + apic->header.length; |
| 455 |
|
| 456 |
printf("APIC entry=0x%p end=0x%x\n", apic_entry, end); |
| 457 |
|
| 458 |
/* Initialize number of cpus */ |
| 459 |
numcpus = apic_get_numcpus(); |
| 460 |
|
| 461 |
/* Search in APIC entry. */ |
| 462 |
while ((vm_offset_t)apic_entry < end) { |
| 463 |
struct acpi_apic_lapic *lapic_entry; |
| 464 |
struct acpi_apic_ioapic *ioapic_entry; |
| 465 |
struct acpi_apic_irq_override *irq_override_entry; |
| 466 |
|
| 467 |
printf("APIC entry=0x%p end=0x%x\n", apic_entry, end); |
| 468 |
/* Check entry type. */ |
| 469 |
switch(apic_entry->type) { |
| 470 |
|
| 471 |
/* If APIC entry is a CPU's Local APIC. */ |
| 472 |
case ACPI_APIC_ENTRY_LAPIC: |
| 473 |
if(numcpus < NCPUS) { |
| 474 |
/* Store Local APIC data. */ |
| 475 |
lapic_entry = (struct acpi_apic_lapic*) apic_entry; |
| 476 |
acpi_apic_add_lapic(lapic_entry); |
| 477 |
} |
| 478 |
break; |
| 479 |
|
| 480 |
/* If APIC entry is an IOAPIC. */ |
| 481 |
case ACPI_APIC_ENTRY_IOAPIC: |
| 482 |
|
| 483 |
/* Store IOAPIC data. */ |
| 484 |
ioapic_entry = (struct acpi_apic_ioapic*) apic_entry; |
| 485 |
acpi_apic_add_ioapic(ioapic_entry); |
| 486 |
|
| 487 |
break; |
| 488 |
|
| 489 |
/* If APIC entry is a IRQ Override. */ |
| 490 |
case ACPI_APIC_ENTRY_IRQ_OVERRIDE: |
| 491 |
|
| 492 |
/* Store IRQ Override data. */ |
| 493 |
irq_override_entry = (struct acpi_apic_irq_override*) apic_entry; |
| 494 |
acpi_apic_add_irq_override(irq_override_entry); |
| 495 |
break; |
| 496 |
|
| 497 |
/* FIXME: There is another unhandled case */ |
| 498 |
default: |
| 499 |
printf("Unhandled APIC entry type 0x%x\n", apic_entry->type); |
| 500 |
break; |
| 501 |
} |
| 502 |
|
| 503 |
/* Get next APIC entry. */ |
| 504 |
apic_entry = (struct acpi_apic_dhdr*)((vm_offset_t) apic_entry |
| 505 |
+ apic_entry->length); |
| 506 |
|
| 507 |
/* Update number of cpus. */ |
| 508 |
numcpus = apic_get_numcpus(); |
| 509 |
} |
| 510 |
|
| 511 |
return ACPI_SUCCESS; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
/* |
| 516 |
* acpi_apic_setup: parses the APIC/MADT table, to find the Local APIC and IOAPIC structures |
| 517 |
* and the common address for Local APIC. |
| 518 |
* |
| 519 |
* Receives as input a reference for APIC/MADT table. |
| 520 |
* Returns 0 if success. |
| 521 |
* |
| 522 |
* Fills the cpu_to_lapic and ioapics array, indexed by Kernel ID |
| 523 |
* with a relationship between Kernel ID and APIC ID, |
| 524 |
* and map the Local APIC common address, to fill the lapic reference. |
| 525 |
* |
| 526 |
* Precondition: The APIC pointer must not be NULL |
| 527 |
*/ |
| 528 |
|
| 529 |
static int |
| 530 |
acpi_apic_setup(struct acpi_apic *apic) |
| 531 |
{ |
| 532 |
ApicLocalUnit* lapic_unit; |
| 533 |
uint8_t ncpus, nioapics; |
| 534 |
|
| 535 |
/* map common lapic address */ |
| 536 |
lapic_addr = apic->lapic_addr; |
| 537 |
lapic_unit = kmem_map_aligned_table(apic->lapic_addr, sizeof(ApicLocalUnit), |
| 538 |
VM_PROT_READ | VM_PROT_WRITE); |
| 539 |
|
| 540 |
if (lapic_unit == NULL) |
| 541 |
return ACPI_NO_LAPIC; |
| 542 |
|
| 543 |
apic_lapic_init(lapic_unit); |
| 544 |
|
| 545 |
fix_apic_id_mask(); |
| 546 |
|
| 547 |
acpi_apic_parse_table(apic); |
| 548 |
|
| 549 |
ncpus = apic_get_numcpus(); |
| 550 |
nioapics = apic_get_num_ioapics(); |
| 551 |
|
| 552 |
if (ncpus == 0 || nioapics == 0 || ncpus > NCPUS) |
| 553 |
return ACPI_APIC_FAILURE; |
| 554 |
|
| 555 |
/* Refit the apic-cpu array. */ |
| 556 |
if(ncpus < NCPUS) { |
| 557 |
int refit = apic_refit_cpulist(); |
| 558 |
if (refit != 0) |
| 559 |
return ACPI_FIT_FAILURE; |
| 560 |
} |
| 561 |
|
| 562 |
apic_generate_cpu_id_lut(); |
| 563 |
|
| 564 |
return ACPI_SUCCESS; |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* acpi_apic_init: find the MADT/APIC table in ACPI tables |
| 569 |
* and parses It to find Local APIC and IOAPIC structures. |
| 570 |
* Each Local APIC stores the info and control structores for a cpu. |
| 571 |
* The IOAPIC controls the communication of the processors with the I/O devices. |
| 572 |
* |
| 573 |
* Returns 0 if success, -1 if error. |
| 574 |
*/ |
| 575 |
int |
| 576 |
acpi_apic_init(void) |
| 577 |
{ |
| 578 |
phys_addr_t rsdp = 0; |
| 579 |
struct acpi_rsdt *rsdt = 0; |
| 580 |
struct acpi_xsdt *xsdt = 0; |
| 581 |
int acpi_rsdt_n; |
| 582 |
int ret_acpi_setup; |
| 583 |
int apic_init_success = 0; |
| 584 |
int is_64bit = 0; |
| 585 |
uint8_t checksum; |
| 586 |
|
| 587 |
/* Try to get the RSDP physical address. */ |
| 588 |
rsdp = acpi_get_rsdp(&is_64bit); |
| 589 |
if (rsdp == 0) |
| 590 |
return ACPI_NO_RSDP; |
| 591 |
|
| 592 |
if (!is_64bit) { |
| 593 |
/* Try to get the RSDT pointer. */ |
| 594 |
rsdt = acpi_get_rsdt(rsdp, &acpi_rsdt_n); |
| 595 |
if (rsdt == NULL) |
| 596 |
return ACPI_NO_RSDT; |
| 597 |
|
| 598 |
checksum = acpi_checksum((void *)rsdt, rsdt->header.length); |
| 599 |
if (checksum != 0) |
| 600 |
return ACPI_BAD_CHECKSUM; |
| 601 |
|
| 602 |
/* Try to get the APIC table pointer. */ |
| 603 |
apic_madt = acpi_get_apic(rsdt, acpi_rsdt_n); |
| 604 |
if (apic_madt == NULL) |
| 605 |
return ACPI_NO_APIC; |
| 606 |
|
| 607 |
checksum = acpi_checksum((void *)apic_madt, apic_madt->header.length); |
| 608 |
if (checksum != 0) |
| 609 |
return ACPI_BAD_CHECKSUM; |
| 610 |
|
| 611 |
/* Print the ACPI tables addresses. */ |
| 612 |
acpi_print_info(rsdp, rsdt, acpi_rsdt_n); |
| 613 |
|
| 614 |
} else { |
| 615 |
/* Try to get the XSDT pointer. */ |
| 616 |
xsdt = acpi_get_xsdt(rsdp, &acpi_rsdt_n); |
| 617 |
if (xsdt == NULL) |
| 618 |
return ACPI_NO_RSDT; |
| 619 |
|
| 620 |
checksum = acpi_checksum((void *)xsdt, xsdt->header.length); |
| 621 |
if (checksum != 0) |
| 622 |
return ACPI_BAD_CHECKSUM; |
| 623 |
|
| 624 |
/* Try to get the APIC table pointer. */ |
| 625 |
apic_madt = acpi_get_apic2(xsdt, acpi_rsdt_n); |
| 626 |
if (apic_madt == NULL) |
| 627 |
return ACPI_NO_APIC; |
| 628 |
|
| 629 |
checksum = acpi_checksum((void *)apic_madt, apic_madt->header.length); |
| 630 |
if (checksum != 0) |
| 631 |
return ACPI_BAD_CHECKSUM; |
| 632 |
|
| 633 |
/* Print the ACPI tables addresses. */ |
| 634 |
acpi_print_info(rsdp, xsdt, acpi_rsdt_n); |
| 635 |
} |
| 636 |
|
| 637 |
apic_init_success = apic_data_init(); |
| 638 |
if (apic_init_success != ACPI_SUCCESS) |
| 639 |
return ACPI_APIC_FAILURE; |
| 640 |
|
| 641 |
/* |
| 642 |
* Starts the parsing of APIC table, to find the APIC structures. |
| 643 |
* and enumerate them. This function also find the common Local APIC address. |
| 644 |
*/ |
| 645 |
ret_acpi_setup = acpi_apic_setup(apic_madt); |
| 646 |
if (ret_acpi_setup != ACPI_SUCCESS) |
| 647 |
return ret_acpi_setup; |
| 648 |
|
| 649 |
/* Prints a table with the list of each cpu and each IOAPIC with its APIC ID. */ |
| 650 |
apic_print_info(); |
| 651 |
|
| 652 |
return ACPI_SUCCESS; |
| 653 |
} |