419b1360fc17c16f7aaec63f795a2d632b3537c7
[lunaix-os.git] / lunaix-os / hal / acpi / acpi.c
1 #include <hal/acpi/acpi.h>
2
3 #include <lunaix/mm/valloc.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 = vzalloc(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 =
40           (acpi_sdthdr_t*)((acpi_apic_t**)&(rsdt->entry))[i];
41         switch (sdthdr->signature) {
42             case ACPI_MADT_SIG:
43                 kprintf(KINFO "MADT: %p\n", sdthdr);
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                 kprintf(KINFO "FADT: %p\n", sdthdr);
49                 ctx->fadt = *(acpi_fadt_t*)sdthdr;
50                 break;
51             case ACPI_MCFG_SIG:
52                 kprintf(KINFO "MCFG: %p\n", sdthdr);
53                 mcfg_parse(sdthdr, ctx);
54                 break;
55             default:
56                 break;
57         }
58     }
59
60     kprintf(KINFO "ACPI: %s\n", ctx->oem_id);
61 }
62
63 acpi_context*
64 acpi_get_context()
65 {
66     assert_msg(ctx, "ACPI is not initialized");
67     return ctx;
68 }
69
70 int
71 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
72 {
73     uint8_t sum = 0;
74     uint8_t* rsdp_ptr = (uint8_t*)rsdp;
75     for (size_t i = 0; i < 20; i++) {
76         sum += *(rsdp_ptr + i);
77     }
78
79     return sum == 0;
80 }
81
82 uint8_t
83 acpi_gistranslate(uint8_t old_irq)
84 {
85     if (old_irq >= 24) {
86         return old_irq;
87     }
88     acpi_intso_t* int_override = ctx->madt.irq_exception[old_irq];
89     return int_override ? (uint8_t)int_override->gsi : old_irq;
90 }
91
92 #define VIRTUAL_BOX_PROBLEM
93
94 acpi_rsdp_t*
95 acpi_locate_rsdp(multiboot_info_t* mb_info)
96 {
97     acpi_rsdp_t* rsdp = NULL;
98
99     // You can't trust memory map from multiboot in virtual box!
100     // They put ACPI RSDP in the FUCKING 0xe0000 !!!
101     // Which is reported to be free area bt multiboot!
102     // SWEET CELESTIA!!!
103 #ifndef VIRTUAL_BOX_PROBLEM
104     multiboot_memory_map_t* mmap = (multiboot_memory_map_t*)mb_info->mmap_addr;
105     for (size_t i = 0, j = 0; j < mb_info->mmap_length && !rsdp;
106          i++, j += MB_MMAP_ENTRY_SIZE) {
107         multiboot_memory_map_t entry = mmap[i];
108         if (entry.type != MULTIBOOT_MEMORY_RESERVED ||
109             entry.addr_low > 0x100000) {
110             continue;
111         }
112
113         uint8_t* mem_start = entry.addr_low & ~0xf;
114         size_t len = entry.len_low;
115         for (size_t j = 0; j < len; j += 16) {
116             u32_t sig_low = *((u32_t*)(mem_start + j));
117             // u32_t sig_high = *((u32_t*)(mem_start+j) + 1);
118             if (sig_low == ACPI_RSDP_SIG_L) {
119                 rsdp = (acpi_rsdp_t*)(mem_start + j);
120                 break;
121             }
122         }
123     }
124 #else
125     // You know what, I just search the entire 1MiB for Celestia's sake.
126     uint8_t* mem_start = 0x4000;
127     for (; mem_start < 0x100000; mem_start += 16) {
128         u32_t sig_low = *((u32_t*)(mem_start));
129         // XXX: do we need to compare this as well?
130         // u32_t sig_high = *((u32_t*)(mem_start+j) + 1);
131         if (sig_low == ACPI_RSDP_SIG_L) {
132             rsdp = (acpi_rsdp_t*)(mem_start);
133             break;
134         }
135     }
136 #endif
137
138     return rsdp;
139 }