refactor: more compact log message
[lunaix-os.git] / lunaix-os / hal / acpi / acpi.c
index a99827803a14e729bb9b4fdb07591fdd767ad57d..67f5adebcd0c0ccf4e0255b5575292e466cf7129 100644 (file)
@@ -1,14 +1,14 @@
 #include <hal/acpi/acpi.h>
 
-#include <lunaix/mm/kalloc.h>
+#include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 
 #include <klibc/string.h>
 
-#include "parser/madt_parser.h"
+#include "parser/parser.h"
 
-static acpi_context* toc = NULL;
+static acpi_context* ctx = NULL;
 
 LOG_MODULE("ACPI")
 
@@ -26,46 +26,45 @@ acpi_init(multiboot_info_t* mb_info)
     assert_msg(rsdp, "Fail to locate ACPI_RSDP");
     assert_msg(acpi_rsdp_validate(rsdp), "Invalid ACPI_RSDP (checksum failed)");
 
-    kprintf(KINFO "RSDP found at %p, RSDT: %p\n", rsdp, rsdp->rsdt);
-
     acpi_rsdt_t* rsdt = rsdp->rsdt;
 
-    toc = lxcalloc(1, sizeof(acpi_context));
-    assert_msg(toc, "Fail to create ACPI context");
+    ctx = vzalloc(sizeof(acpi_context));
+    assert_msg(ctx, "Fail to create ACPI context");
 
-    strncpy(toc->oem_id, rsdt->header.oem_id, 6);
-    toc->oem_id[6] = '\0';
+    strncpy(ctx->oem_id, rsdt->header.oem_id, 6);
+    ctx->oem_id[6] = '\0';
 
     size_t entry_n = (rsdt->header.length - sizeof(acpi_sdthdr_t)) >> 2;
     for (size_t i = 0; i < entry_n; i++) {
-        acpi_sdthdr_t* sdthdr = ((acpi_apic_t**)&(rsdt->entry))[i];
+        acpi_sdthdr_t* sdthdr =
+          (acpi_sdthdr_t*)((acpi_apic_t**)&(rsdt->entry))[i];
         switch (sdthdr->signature) {
             case ACPI_MADT_SIG:
-                madt_parse((acpi_madt_t*)sdthdr, toc);
+                kprintf(KINFO "MADT: %p\n", sdthdr);
+                madt_parse((acpi_madt_t*)sdthdr, ctx);
+                break;
+            case ACPI_FADT_SIG:
+                // FADT just a plain structure, no need to parse.
+                kprintf(KINFO "FADT: %p\n", sdthdr);
+                ctx->fadt = *(acpi_fadt_t*)sdthdr;
+                break;
+            case ACPI_MCFG_SIG:
+                kprintf(KINFO "MCFG: %p\n", sdthdr);
+                mcfg_parse(sdthdr, ctx);
                 break;
             default:
                 break;
         }
     }
 
-    kprintf(KINFO "OEM: %s\n", toc->oem_id);
-    kprintf(KINFO "IOAPIC address: %p\n", toc->madt.ioapic->ioapic_addr);
-    kprintf(KINFO "APIC address: %p\n", toc->madt.apic_addr);
-
-    for (size_t i = 0; i < 24; i++) {
-        acpi_intso_t* intso = toc->madt.irq_exception[i];
-        if (!intso)
-            continue;
-
-        kprintf(KINFO "IRQ #%u -> GSI #%u\n", intso->source, intso->gsi);
-    }
+    kprintf(KINFO "ACPI: %s\n", ctx->oem_id);
 }
 
 acpi_context*
 acpi_get_context()
 {
-    assert_msg(toc, "ACPI is not initialized");
-    return toc;
+    assert_msg(ctx, "ACPI is not initialized");
+    return ctx;
 }
 
 int
@@ -80,6 +79,16 @@ acpi_rsdp_validate(acpi_rsdp_t* rsdp)
     return sum == 0;
 }
 
+uint8_t
+acpi_gistranslate(uint8_t old_irq)
+{
+    if (old_irq >= 24) {
+        return old_irq;
+    }
+    acpi_intso_t* int_override = ctx->madt.irq_exception[old_irq];
+    return int_override ? (uint8_t)int_override->gsi : old_irq;
+}
+
 #define VIRTUAL_BOX_PROBLEM
 
 acpi_rsdp_t*
@@ -117,6 +126,7 @@ acpi_locate_rsdp(multiboot_info_t* mb_info)
     uint8_t* mem_start = 0x4000;
     for (; mem_start < 0x100000; mem_start += 16) {
         uint32_t sig_low = *((uint32_t*)(mem_start));
+        // XXX: do we need to compare this as well?
         // uint32_t sig_high = *((uint32_t*)(mem_start+j) + 1);
         if (sig_low == ACPI_RSDP_SIG_L) {
             rsdp = (acpi_rsdp_t*)(mem_start);