feat: simple device abstraction layer
[lunaix-os.git] / lunaix-os / hal / acpi / acpi.c
1 #include <hal/acpi/acpi.h>
2
3 #include <lunaix/mm/kalloc.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/syslog.h>
6
7 #include <klibc/string.h>
8
9 #include "parser/parser.h"
10
11 static acpi_context* ctx = NULL;
12
13 LOG_MODULE("ACPI")
14
15 int
16 acpi_rsdp_validate(acpi_rsdp_t* rsdp);
17
18 acpi_rsdp_t*
19 acpi_locate_rsdp(multiboot_info_t* mb_info);
20
21 int
22 acpi_init(multiboot_info_t* mb_info)
23 {
24     acpi_rsdp_t* rsdp = acpi_locate_rsdp(mb_info);
25
26     assert_msg(rsdp, "Fail to locate ACPI_RSDP");
27     assert_msg(acpi_rsdp_validate(rsdp), "Invalid ACPI_RSDP (checksum failed)");
28
29     acpi_rsdt_t* rsdt = rsdp->rsdt;
30
31     ctx = lxcalloc(1, sizeof(acpi_context));
32     assert_msg(ctx, "Fail to create ACPI context");
33
34     strncpy(ctx->oem_id, rsdt->header.oem_id, 6);
35     ctx->oem_id[6] = '\0';
36
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 = ((acpi_apic_t**)&(rsdt->entry))[i];
40         switch (sdthdr->signature) {
41             case ACPI_MADT_SIG:
42                 madt_parse((acpi_madt_t*)sdthdr, ctx);
43                 break;
44             case ACPI_FADT_SIG:
45                 // FADT just a plain structure, no need to parse.
46                 ctx->fadt = *(acpi_fadt_t*)sdthdr;
47                 break;
48             case ACPI_MCFG_SIG:
49                 mcfg_parse(sdthdr, ctx);
50                 break;
51             default:
52                 break;
53         }
54     }
55
56     kprintf(KINFO "ACPI: %s\n", ctx->oem_id);
57 }
58
59 acpi_context*
60 acpi_get_context()
61 {
62     assert_msg(ctx, "ACPI is not initialized");
63     return ctx;
64 }
65
66 int
67 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
68 {
69     uint8_t sum = 0;
70     uint8_t* rsdp_ptr = (uint8_t*)rsdp;
71     for (size_t i = 0; i < 20; i++) {
72         sum += *(rsdp_ptr + i);
73     }
74
75     return sum == 0;
76 }
77
78 #define VIRTUAL_BOX_PROBLEM
79
80 acpi_rsdp_t*
81 acpi_locate_rsdp(multiboot_info_t* mb_info)
82 {
83     acpi_rsdp_t* rsdp = NULL;
84
85     // You can't trust memory map from multiboot in virtual box!
86     // They put ACPI RSDP in the FUCKING 0xe0000 !!!
87     // Which is reported to be free area bt multiboot!
88     // SWEET CELESTIA!!!
89 #ifndef VIRTUAL_BOX_PROBLEM
90     multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mb_info->mmap_addr;
91     for (size_t i = 0, j = 0; j < mb_info->mmap_length && !rsdp;
92          i++, j += MB_MMAP_ENTRY_SIZE) {
93         multiboot_memory_map_t entry = mmap[i];
94         if (entry.type != MULTIBOOT_MEMORY_RESERVED ||
95             entry.addr_low > 0x100000) {
96             continue;
97         }
98
99         uint8_t* mem_start = entry.addr_low & ~0xf;
100         size_t len = entry.len_low;
101         for (size_t j = 0; j < len; j += 16) {
102             uint32_t sig_low = *((uint32_t*)(mem_start + j));
103             // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
104             if (sig_low == ACPI_RSDP_SIG_L) {
105                 rsdp = (acpi_rsdp_t*)(mem_start + j);
106                 break;
107             }
108         }
109     }
110 #else
111     // You know what, I just search the entire 1MiB for Celestia's sake.
112     uint8_t* mem_start = 0x4000;
113     for (; mem_start < 0x100000; mem_start += 16) {
114         uint32_t sig_low = *((uint32_t*)(mem_start));
115         // XXX: do we need to compare this as well?
116         // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
117         if (sig_low == ACPI_RSDP_SIG_L) {
118             rsdp = (acpi_rsdp_t*)(mem_start);
119             break;
120         }
121     }
122 #endif
123
124     return rsdp;
125 }