Timer re-worked!
[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/madt_parser.h"
10
11 static acpi_context* toc = 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(KINFO "RSDP found at %p, RSDT: %p\n", rsdp, rsdp->rsdt);
30
31     acpi_rsdt_t* rsdt = rsdp->rsdt;
32
33     toc = lxcalloc(1, sizeof(acpi_context));
34     assert_msg(toc, "Fail to create ACPI context");
35
36     strncpy(toc->oem_id, rsdt->header.oem_id, 6);
37     toc->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, toc);
45                 break;
46             default:
47                 break;
48         }
49     }
50
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);
54
55     for (size_t i = 0; i < 24; i++) {
56         acpi_intso_t* intso = toc->madt.irq_exception[i];
57         if (!intso)
58             continue;
59
60         kprintf(KINFO "IRQ #%u -> GSI #%u\n", intso->source, intso->gsi);
61     }
62 }
63
64 acpi_context*
65 acpi_get_context()
66 {
67     assert_msg(toc, "ACPI is not initialized");
68     return toc;
69 }
70
71 int
72 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
73 {
74     uint8_t sum = 0;
75     uint8_t* rsdp_ptr = (uint8_t*)rsdp;
76     for (size_t i = 0; i < 20; i++) {
77         sum += *(rsdp_ptr + i);
78     }
79
80     return sum == 0;
81 }
82
83 #define VIRTUAL_BOX_PROBLEM
84
85 acpi_rsdp_t*
86 acpi_locate_rsdp(multiboot_info_t* mb_info)
87 {
88     acpi_rsdp_t* rsdp = NULL;
89
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!
93     // SWEET CELESTIA!!!
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) {
101             continue;
102         }
103
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);
111                 break;
112             }
113         }
114     }
115 #else
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);
123             break;
124         }
125     }
126 #endif
127
128     return rsdp;
129 }