refactor: make pci device driver loading passive, pci bus scanner will not load them...
[lunaix-os.git] / lunaix-os / hal / acpi / acpi.c
1 #include <hal/acpi/acpi.h>
2
3 #include <lunaix/device.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
6 #include <lunaix/syslog.h>
7
8 #include <klibc/string.h>
9
10 #include "parser/parser.h"
11
12 static acpi_context* ctx = NULL;
13
14 LOG_MODULE("ACPI")
15
16 int
17 acpi_rsdp_validate(acpi_rsdp_t* rsdp);
18
19 acpi_context*
20 acpi_get_context()
21 {
22     assert_msg(ctx, "ACPI is not initialized");
23     return ctx;
24 }
25
26 int
27 acpi_rsdp_validate(acpi_rsdp_t* rsdp)
28 {
29     u8_t sum = 0;
30     u8_t* rsdp_ptr = (u8_t*)rsdp;
31     for (size_t i = 0; i < 20; i++) {
32         sum += *(rsdp_ptr + i);
33     }
34
35     return sum == 0;
36 }
37
38 u8_t
39 acpi_gsimap(u8_t old_irq)
40 {
41     if (old_irq >= 24) {
42         return old_irq;
43     }
44     acpi_intso_t* int_override = ctx->madt.irq_exception[old_irq];
45     return int_override ? (u8_t)int_override->gsi : old_irq;
46 }
47
48 #define VIRTUAL_BOX_PROBLEM
49
50 acpi_rsdp_t*
51 acpi_locate_rsdp()
52 {
53     acpi_rsdp_t* rsdp = NULL;
54
55     ptr_t mem_start = 0x4000;
56     for (; mem_start < 0x100000; mem_start += 16) {
57         u32_t sig_low = *((u32_t*)mem_start);
58         // XXX: do we need to compare this as well?
59         // u32_t sig_high = *((u32_t*)(mem_start+j) + 1);
60
61         if (sig_low == ACPI_RSDP_SIG_L) {
62             rsdp = (acpi_rsdp_t*)mem_start;
63             break;
64         }
65     }
66
67     return rsdp;
68 }
69
70 static int
71 acpi_init(struct device_def* devdef)
72 {
73     acpi_rsdp_t* rsdp = acpi_locate_rsdp();
74
75     assert_msg(rsdp, "Fail to locate ACPI_RSDP");
76     assert_msg(acpi_rsdp_validate(rsdp), "Invalid ACPI_RSDP (checksum failed)");
77
78     acpi_rsdt_t* rsdt = rsdp->rsdt;
79
80     ctx = vzalloc(sizeof(acpi_context));
81     assert_msg(ctx, "Fail to create ACPI context");
82
83     strncpy(ctx->oem_id, rsdt->header.oem_id, 6);
84     ctx->oem_id[6] = '\0';
85
86     size_t entry_n = (rsdt->header.length - sizeof(acpi_sdthdr_t)) >> 2;
87     for (size_t i = 0; i < entry_n; i++) {
88         acpi_sdthdr_t* sdthdr =
89           (acpi_sdthdr_t*)((acpi_apic_t**)&(rsdt->entry))[i];
90         switch (sdthdr->signature) {
91             case ACPI_MADT_SIG:
92                 madt_parse((acpi_madt_t*)sdthdr, ctx);
93                 break;
94             case ACPI_FADT_SIG:
95                 // FADT just a plain structure, no need to parse.
96                 ctx->fadt = *(acpi_fadt_t*)sdthdr;
97                 break;
98             case ACPI_MCFG_SIG:
99                 mcfg_parse(sdthdr, ctx);
100                 break;
101             default:
102                 break;
103         }
104     }
105
106     return 0;
107 }
108
109 struct device_def acpi_sysdev = { .name = "ACPI Proxy",
110                                   .class =
111                                     DEVCLASS(DEVIF_FMW, DEVFN_CFG, DEV_ACPI),
112                                   .init = acpi_init };
113 EXPORT_DEVICE(acpi, &acpi_sysdev, load_sysconf);