1 #include <hal/acpi/acpi.h>
3 #include <lunaix/mm/kalloc.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/syslog.h>
7 #include <klibc/string.h>
9 #include "parser/madt_parser.h"
11 static acpi_context* ctx = NULL;
16 acpi_rsdp_validate(acpi_rsdp_t* rsdp);
19 acpi_locate_rsdp(multiboot_info_t* mb_info);
22 acpi_init(multiboot_info_t* mb_info)
24 acpi_rsdp_t* rsdp = acpi_locate_rsdp(mb_info);
26 assert_msg(rsdp, "Fail to locate ACPI_RSDP");
27 assert_msg(acpi_rsdp_validate(rsdp), "Invalid ACPI_RSDP (checksum failed)");
29 kprintf(KINFO "RSDP found at %p, RSDT: %p\n", rsdp, rsdp->rsdt);
31 acpi_rsdt_t* rsdt = rsdp->rsdt;
33 ctx = lxcalloc(1, sizeof(acpi_context));
34 assert_msg(ctx, "Fail to create ACPI context");
36 strncpy(ctx->oem_id, rsdt->header.oem_id, 6);
37 ctx->oem_id[6] = '\0';
39 size_t entry_n = (rsdt->header.length - sizeof(acpi_sdthdr_t)) >> 2;
40 for (size_t i = 0; i < entry_n; i++) {
41 acpi_sdthdr_t* sdthdr = ((acpi_apic_t**)&(rsdt->entry))[i];
42 switch (sdthdr->signature) {
44 madt_parse((acpi_madt_t*)sdthdr, ctx);
47 // FADT just a plain structure, no need to parse.
48 ctx->fadt = *(acpi_fadt_t*)sdthdr;
55 kprintf(KINFO "OEM: %s\n", ctx->oem_id);
56 kprintf(KINFO "IOAPIC address: %p\n", ctx->madt.ioapic->ioapic_addr);
57 kprintf(KINFO "APIC address: %p\n", ctx->madt.apic_addr);
59 for (size_t i = 0; i < 24; i++) {
60 acpi_intso_t* intso = ctx->madt.irq_exception[i];
64 kprintf(KINFO "IRQ #%u -> GSI #%u\n", intso->source, intso->gsi);
71 assert_msg(ctx, "ACPI is not initialized");
76 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
79 uint8_t* rsdp_ptr = (uint8_t*)rsdp;
80 for (size_t i = 0; i < 20; i++) {
81 sum += *(rsdp_ptr + i);
87 #define VIRTUAL_BOX_PROBLEM
90 acpi_locate_rsdp(multiboot_info_t* mb_info)
92 acpi_rsdp_t* rsdp = NULL;
94 // You can't trust memory map from multiboot in virtual box!
95 // They put ACPI RSDP in the FUCKING 0xe0000 !!!
96 // Which is reported to be free area bt multiboot!
98 #ifndef VIRTUAL_BOX_PROBLEM
99 multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mb_info->mmap_addr;
100 for (size_t i = 0, j = 0; j < mb_info->mmap_length && !rsdp;
101 i++, j += MB_MMAP_ENTRY_SIZE) {
102 multiboot_memory_map_t entry = mmap[i];
103 if (entry.type != MULTIBOOT_MEMORY_RESERVED ||
104 entry.addr_low > 0x100000) {
108 uint8_t* mem_start = entry.addr_low & ~0xf;
109 size_t len = entry.len_low;
110 for (size_t j = 0; j < len; j += 16) {
111 uint32_t sig_low = *((uint32_t*)(mem_start + j));
112 // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
113 if (sig_low == ACPI_RSDP_SIG_L) {
114 rsdp = (acpi_rsdp_t*)(mem_start + j);
120 // You know what, I just search the entire 1MiB for Celestia's sake.
121 uint8_t* mem_start = 0x4000;
122 for (; mem_start < 0x100000; mem_start += 16) {
123 uint32_t sig_low = *((uint32_t*)(mem_start));
124 // XXX: do we need to compare this as well?
125 // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
126 if (sig_low == ACPI_RSDP_SIG_L) {
127 rsdp = (acpi_rsdp_t*)(mem_start);