1 #include <hal/acpi/acpi.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/syslog.h>
7 #include <klibc/string.h>
9 #include "parser/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 acpi_rsdt_t* rsdt = rsdp->rsdt;
31 ctx = vzalloc(sizeof(acpi_context));
32 assert_msg(ctx, "Fail to create ACPI context");
34 strncpy(ctx->oem_id, rsdt->header.oem_id, 6);
35 ctx->oem_id[6] = '\0';
37 size_t entry_n = (rsdt->header.length - sizeof(acpi_sdthdr_t)) >> 2;
38 for (size_t i = 0; i < entry_n; i++) {
39 acpi_sdthdr_t* sdthdr =
40 (acpi_sdthdr_t*)((acpi_apic_t**)&(rsdt->entry))[i];
41 switch (sdthdr->signature) {
43 madt_parse((acpi_madt_t*)sdthdr, ctx);
46 // FADT just a plain structure, no need to parse.
47 ctx->fadt = *(acpi_fadt_t*)sdthdr;
50 mcfg_parse(sdthdr, ctx);
57 kprintf(KINFO "ACPI: %s\n", ctx->oem_id);
63 assert_msg(ctx, "ACPI is not initialized");
68 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
71 uint8_t* rsdp_ptr = (uint8_t*)rsdp;
72 for (size_t i = 0; i < 20; i++) {
73 sum += *(rsdp_ptr + i);
80 acpi_gistranslate(uint8_t old_irq)
85 acpi_intso_t* int_override = ctx->madt.irq_exception[old_irq];
86 return int_override ? (uint8_t)int_override->gsi : old_irq;
89 #define VIRTUAL_BOX_PROBLEM
92 acpi_locate_rsdp(multiboot_info_t* mb_info)
94 acpi_rsdp_t* rsdp = NULL;
96 // You can't trust memory map from multiboot in virtual box!
97 // They put ACPI RSDP in the FUCKING 0xe0000 !!!
98 // Which is reported to be free area bt multiboot!
100 #ifndef VIRTUAL_BOX_PROBLEM
101 multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mb_info->mmap_addr;
102 for (size_t i = 0, j = 0; j < mb_info->mmap_length && !rsdp;
103 i++, j += MB_MMAP_ENTRY_SIZE) {
104 multiboot_memory_map_t entry = mmap[i];
105 if (entry.type != MULTIBOOT_MEMORY_RESERVED ||
106 entry.addr_low > 0x100000) {
110 uint8_t* mem_start = entry.addr_low & ~0xf;
111 size_t len = entry.len_low;
112 for (size_t j = 0; j < len; j += 16) {
113 uint32_t sig_low = *((uint32_t*)(mem_start + j));
114 // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
115 if (sig_low == ACPI_RSDP_SIG_L) {
116 rsdp = (acpi_rsdp_t*)(mem_start + j);
122 // You know what, I just search the entire 1MiB for Celestia's sake.
123 uint8_t* mem_start = 0x4000;
124 for (; mem_start < 0x100000; mem_start += 16) {
125 uint32_t sig_low = *((uint32_t*)(mem_start));
126 // XXX: do we need to compare this as well?
127 // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
128 if (sig_low == ACPI_RSDP_SIG_L) {
129 rsdp = (acpi_rsdp_t*)(mem_start);