feat: disk read/write support for both ATA and ATAPI device
[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     kprintf(KDEBUG "RSDP found at %p, RSDT: %p\n", rsdp, rsdp->rsdt);
30
31     acpi_rsdt_t* rsdt = rsdp->rsdt;
32
33     ctx = lxcalloc(1, sizeof(acpi_context));
34     assert_msg(ctx, "Fail to create ACPI context");
35
36     strncpy(ctx->oem_id, rsdt->header.oem_id, 6);
37     ctx->oem_id[6] = '\0';
38
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) {
43             case ACPI_MADT_SIG:
44                 madt_parse((acpi_madt_t*)sdthdr, ctx);
45                 break;
46             case ACPI_FADT_SIG:
47                 // FADT just a plain structure, no need to parse.
48                 ctx->fadt = *(acpi_fadt_t*)sdthdr;
49                 break;
50             case ACPI_MCFG_SIG:
51                 mcfg_parse(sdthdr, ctx);
52                 break;
53             default:
54                 break;
55         }
56     }
57
58     kprintf(KINFO "OEM: %s\n", ctx->oem_id);
59
60     for (size_t i = 0; i < 24; i++) {
61         acpi_intso_t* intso = ctx->madt.irq_exception[i];
62         if (!intso)
63             continue;
64
65         kprintf(KDEBUG "IRQ #%u -> GSI #%u\n", intso->source, intso->gsi);
66     }
67 }
68
69 acpi_context*
70 acpi_get_context()
71 {
72     assert_msg(ctx, "ACPI is not initialized");
73     return ctx;
74 }
75
76 int
77 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
78 {
79     uint8_t sum = 0;
80     uint8_t* rsdp_ptr = (uint8_t*)rsdp;
81     for (size_t i = 0; i < 20; i++) {
82         sum += *(rsdp_ptr + i);
83     }
84
85     return sum == 0;
86 }
87
88 #define VIRTUAL_BOX_PROBLEM
89
90 acpi_rsdp_t*
91 acpi_locate_rsdp(multiboot_info_t* mb_info)
92 {
93     acpi_rsdp_t* rsdp = NULL;
94
95     // You can't trust memory map from multiboot in virtual box!
96     // They put ACPI RSDP in the FUCKING 0xe0000 !!!
97     // Which is reported to be free area bt multiboot!
98     // SWEET CELESTIA!!!
99 #ifndef VIRTUAL_BOX_PROBLEM
100     multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mb_info->mmap_addr;
101     for (size_t i = 0, j = 0; j < mb_info->mmap_length && !rsdp;
102          i++, j += MB_MMAP_ENTRY_SIZE) {
103         multiboot_memory_map_t entry = mmap[i];
104         if (entry.type != MULTIBOOT_MEMORY_RESERVED ||
105             entry.addr_low > 0x100000) {
106             continue;
107         }
108
109         uint8_t* mem_start = entry.addr_low & ~0xf;
110         size_t len = entry.len_low;
111         for (size_t j = 0; j < len; j += 16) {
112             uint32_t sig_low = *((uint32_t*)(mem_start + j));
113             // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
114             if (sig_low == ACPI_RSDP_SIG_L) {
115                 rsdp = (acpi_rsdp_t*)(mem_start + j);
116                 break;
117             }
118         }
119     }
120 #else
121     // You know what, I just search the entire 1MiB for Celestia's sake.
122     uint8_t* mem_start = 0x4000;
123     for (; mem_start < 0x100000; mem_start += 16) {
124         uint32_t sig_low = *((uint32_t*)(mem_start));
125         // XXX: do we need to compare this as well?
126         // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
127         if (sig_low == ACPI_RSDP_SIG_L) {
128             rsdp = (acpi_rsdp_t*)(mem_start);
129             break;
130         }
131     }
132 #endif
133
134     return rsdp;
135 }