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* toc = 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 toc = lxcalloc(1, sizeof(acpi_context));
34 assert_msg(toc, "Fail to create ACPI context");
36 strncpy(toc->oem_id, rsdt->header.oem_id, 6);
37 toc->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, toc);
51 kprintf(KINFO "OEM: %s\n", toc->oem_id);
52 kprintf(KINFO "IOAPIC address: %p\n", toc->madt.ioapic->ioapic_addr);
53 kprintf(KINFO "APIC address: %p\n", toc->madt.apic_addr);
55 for (size_t i = 0; i < 24; i++) {
56 acpi_intso_t* intso = toc->madt.irq_exception[i];
60 kprintf(KINFO "IRQ #%u -> GSI #%u\n", intso->source, intso->gsi);
67 assert_msg(toc, "ACPI is not initialized");
72 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
75 uint8_t* rsdp_ptr = (uint8_t*)rsdp;
76 for (size_t i = 0; i < 20; i++) {
77 sum += *(rsdp_ptr + i);
83 #define VIRTUAL_BOX_PROBLEM
86 acpi_locate_rsdp(multiboot_info_t* mb_info)
88 acpi_rsdp_t* rsdp = NULL;
90 // You can't trust memory map from multiboot in virtual box!
91 // They put ACPI RSDP in the FUCKING 0xe0000 !!!
92 // Which is reported to be free area bt multiboot!
94 #ifndef VIRTUAL_BOX_PROBLEM
95 multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mb_info->mmap_addr;
96 for (size_t i = 0, j = 0; j < mb_info->mmap_length && !rsdp;
97 i++, j += MB_MMAP_ENTRY_SIZE) {
98 multiboot_memory_map_t entry = mmap[i];
99 if (entry.type != MULTIBOOT_MEMORY_RESERVED ||
100 entry.addr_low > 0x100000) {
104 uint8_t* mem_start = entry.addr_low & ~0xf;
105 size_t len = entry.len_low;
106 for (size_t j = 0; j < len; j += 16) {
107 uint32_t sig_low = *((uint32_t*)(mem_start + j));
108 // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
109 if (sig_low == ACPI_RSDP_SIG_L) {
110 rsdp = (acpi_rsdp_t*)(mem_start + j);
116 // You know what, I just search the entire 1MiB for Celestia's sake.
117 uint8_t* mem_start = 0x4000;
118 for (size_t j = 0; j < 0x100000; j += 16) {
119 uint32_t sig_low = *((uint32_t*)(mem_start + j));
120 // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
121 if (sig_low == ACPI_RSDP_SIG_L) {
122 rsdp = (acpi_rsdp_t*)(mem_start + j);