From e0011d22f2cf6f3ef2465d864998ab3498931c8d Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 4 Apr 2024 14:14:28 +0300 Subject: [PATCH] Kernel: Move ACPI to its own directory and namespace --- kernel/CMakeLists.txt | 2 +- kernel/include/kernel/ACPI.h | 144 ------------------------- kernel/include/kernel/ACPI/ACPI.h | 38 +++++++ kernel/include/kernel/ACPI/Headers.h | 115 ++++++++++++++++++++ kernel/kernel/{ => ACPI}/ACPI.cpp | 12 +-- kernel/kernel/APIC.cpp | 4 +- kernel/kernel/Input/PS2/Controller.cpp | 4 +- kernel/kernel/Timer/HPET.cpp | 4 +- kernel/kernel/kernel.cpp | 4 +- kernel/kernel/lai_host.cpp | 4 +- 10 files changed, 170 insertions(+), 161 deletions(-) delete mode 100644 kernel/include/kernel/ACPI.h create mode 100644 kernel/include/kernel/ACPI/ACPI.h create mode 100644 kernel/include/kernel/ACPI/Headers.h rename kernel/kernel/{ => ACPI}/ACPI.cpp (95%) diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 6dcdefd88a..3f2ffa44b0 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -10,7 +10,7 @@ endif() set(KERNEL_SOURCES font/prefs.psf.o - kernel/ACPI.cpp + kernel/ACPI/ACPI.cpp kernel/APIC.cpp kernel/BootInfo.cpp kernel/CPUID.cpp diff --git a/kernel/include/kernel/ACPI.h b/kernel/include/kernel/ACPI.h deleted file mode 100644 index 3a953f8920..0000000000 --- a/kernel/include/kernel/ACPI.h +++ /dev/null @@ -1,144 +0,0 @@ -#pragma once - -#include -#include - -namespace Kernel -{ - - class ACPI - { - public: - struct GAS - { - uint8_t address_space_id; - uint8_t register_bit_width; - uint8_t register_bit_offset; - uint8_t access_size; - uint64_t address; - } __attribute__((packed)); - - struct SDTHeader - { - uint8_t signature[4]; - uint32_t length; - uint8_t revision; - uint8_t checksum; - uint8_t oemid[6]; - uint64_t oem_table_id; - uint32_t oem_revision; - uint32_t creator_id; - uint32_t creator_revision; - } __attribute__((packed)); - - struct FADT : public SDTHeader - { - uint32_t firmware_ctrl; - uint32_t dsdt; - uint8_t __reserved; - uint8_t preferred_pm_profile; - uint16_t sci_int; - uint32_t smi_cmd; - uint8_t acpi_enable; - uint8_t acpi_disable; - uint8_t s4bios_req; - uint8_t pstate_cnt; - uint32_t pm1a_evt_blk; - uint32_t pm1b_evt_blk; - uint32_t pm1a_cnt_blk; - uint32_t pm1b_cnt_blk; - uint32_t pm2_cnt_blk; - uint32_t pm_tmr_blk; - uint32_t gpe0_blk; - uint32_t gpe1_blk; - uint8_t pm1_evt_len; - uint8_t pm1_cnt_len; - uint8_t pm2_cnt_len; - uint8_t pm_tmr_len; - uint8_t gpe0_blk_len; - uint8_t gpe1_blk_len; - uint8_t gpe1_base; - uint8_t cst_cnt; - uint16_t p_lvl2_lat; - uint16_t p_lvl3_lat; - uint16_t flush_size; - uint16_t flush_stride; - uint8_t duty_offset; - uint8_t duty_width; - uint8_t day_alrm; - uint8_t mon_alrm; - uint8_t century; - uint16_t iapc_boot_arch; - uint8_t __reserved2; - uint32_t flags; - uint8_t reset_reg[12]; - uint8_t reset_value; - uint16_t arm_boot_arch; - uint8_t fadt_minor_version; - uint64_t x_firmware_version; - uint64_t x_dsdt; - uint8_t x_pm1a_evt_blk[12]; - uint8_t x_pm1b_evt_blk[12]; - uint8_t x_pm1a_cnt_blk[12]; - uint8_t x_pm1b_cnt_blk[12]; - uint8_t x_pm2_cnt_blk[12]; - uint8_t x_pm_tmr_blk[12]; - uint8_t x_gpe0_blk[12]; - uint8_t x_gpe1_blk[12]; - uint8_t sleep_control_reg[12]; - uint8_t sleep_status_reg[12]; - uint64_t hypervison_vendor_identity; - } __attribute__((packed)); - - struct HPET : public SDTHeader - { - uint8_t hardware_rev_id; - uint8_t comparator_count : 5; - uint8_t count_size_cap : 1; - uint8_t reserved : 1; - uint8_t legacy_replacement_irq_routing_cable : 1; - uint16_t pci_vendor_id; - GAS base_address; - uint8_t hpet_number; - uint16_t main_counter_minimum_clock_tick; - uint8_t page_protection_and_oem_attribute; - } __attribute__((packed)); - - public: - static BAN::ErrorOr initialize(); - static ACPI& get(); - - const SDTHeader* get_header(BAN::StringView signature, uint32_t index); - - private: - ACPI() = default; - BAN::ErrorOr initialize_impl(); - - private: - paddr_t m_header_table_paddr = 0; - vaddr_t m_header_table_vaddr = 0; - uint32_t m_entry_size = 0; - - struct MappedPage - { - Kernel::paddr_t paddr; - Kernel::vaddr_t vaddr; - - SDTHeader* as_header() { return (SDTHeader*)vaddr; } - }; - BAN::Vector m_mapped_headers; - }; - -} - -namespace BAN::Formatter -{ - template - void print_argument(F putc, const Kernel::ACPI::SDTHeader& header, const ValueFormat& format) - { - putc(header.signature[0]); - putc(header.signature[1]); - putc(header.signature[2]); - putc(header.signature[3]); - } -} diff --git a/kernel/include/kernel/ACPI/ACPI.h b/kernel/include/kernel/ACPI/ACPI.h new file mode 100644 index 0000000000..ebbc5a0e85 --- /dev/null +++ b/kernel/include/kernel/ACPI/ACPI.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include + +namespace Kernel::ACPI +{ + + class ACPI + { + public: + static BAN::ErrorOr initialize(); + static ACPI& get(); + + const SDTHeader* get_header(BAN::StringView signature, uint32_t index); + + private: + ACPI() = default; + BAN::ErrorOr initialize_impl(); + + private: + paddr_t m_header_table_paddr = 0; + vaddr_t m_header_table_vaddr = 0; + uint32_t m_entry_size = 0; + + struct MappedPage + { + Kernel::paddr_t paddr; + Kernel::vaddr_t vaddr; + + SDTHeader* as_header() { return (SDTHeader*)vaddr; } + }; + BAN::Vector m_mapped_headers; + }; + +} diff --git a/kernel/include/kernel/ACPI/Headers.h b/kernel/include/kernel/ACPI/Headers.h new file mode 100644 index 0000000000..66ebd8bb48 --- /dev/null +++ b/kernel/include/kernel/ACPI/Headers.h @@ -0,0 +1,115 @@ +#pragma once + +#include + +namespace Kernel::ACPI +{ + + struct GAS + { + uint8_t address_space_id; + uint8_t register_bit_width; + uint8_t register_bit_offset; + uint8_t access_size; + uint64_t address; + } __attribute__((packed)); + + struct SDTHeader + { + uint8_t signature[4]; + uint32_t length; + uint8_t revision; + uint8_t checksum; + uint8_t oemid[6]; + uint64_t oem_table_id; + uint32_t oem_revision; + uint32_t creator_id; + uint32_t creator_revision; + } __attribute__((packed)); + + struct FADT : public SDTHeader + { + uint32_t firmware_ctrl; + uint32_t dsdt; + uint8_t __reserved; + uint8_t preferred_pm_profile; + uint16_t sci_int; + uint32_t smi_cmd; + uint8_t acpi_enable; + uint8_t acpi_disable; + uint8_t s4bios_req; + uint8_t pstate_cnt; + uint32_t pm1a_evt_blk; + uint32_t pm1b_evt_blk; + uint32_t pm1a_cnt_blk; + uint32_t pm1b_cnt_blk; + uint32_t pm2_cnt_blk; + uint32_t pm_tmr_blk; + uint32_t gpe0_blk; + uint32_t gpe1_blk; + uint8_t pm1_evt_len; + uint8_t pm1_cnt_len; + uint8_t pm2_cnt_len; + uint8_t pm_tmr_len; + uint8_t gpe0_blk_len; + uint8_t gpe1_blk_len; + uint8_t gpe1_base; + uint8_t cst_cnt; + uint16_t p_lvl2_lat; + uint16_t p_lvl3_lat; + uint16_t flush_size; + uint16_t flush_stride; + uint8_t duty_offset; + uint8_t duty_width; + uint8_t day_alrm; + uint8_t mon_alrm; + uint8_t century; + uint16_t iapc_boot_arch; + uint8_t __reserved2; + uint32_t flags; + uint8_t reset_reg[12]; + uint8_t reset_value; + uint16_t arm_boot_arch; + uint8_t fadt_minor_version; + uint64_t x_firmware_version; + uint64_t x_dsdt; + uint8_t x_pm1a_evt_blk[12]; + uint8_t x_pm1b_evt_blk[12]; + uint8_t x_pm1a_cnt_blk[12]; + uint8_t x_pm1b_cnt_blk[12]; + uint8_t x_pm2_cnt_blk[12]; + uint8_t x_pm_tmr_blk[12]; + uint8_t x_gpe0_blk[12]; + uint8_t x_gpe1_blk[12]; + uint8_t sleep_control_reg[12]; + uint8_t sleep_status_reg[12]; + uint64_t hypervison_vendor_identity; + } __attribute__((packed)); + + struct HPET : public SDTHeader + { + uint8_t hardware_rev_id; + uint8_t comparator_count : 5; + uint8_t count_size_cap : 1; + uint8_t reserved : 1; + uint8_t legacy_replacement_irq_routing_cable : 1; + uint16_t pci_vendor_id; + GAS base_address; + uint8_t hpet_number; + uint16_t main_counter_minimum_clock_tick; + uint8_t page_protection_and_oem_attribute; + } __attribute__((packed)); + +} + +namespace BAN::Formatter +{ + template + void print_argument(F putc, const Kernel::ACPI::SDTHeader& header, const struct ValueFormat&) + { + putc(header.signature[0]); + putc(header.signature[1]); + putc(header.signature[2]); + putc(header.signature[3]); + } +} diff --git a/kernel/kernel/ACPI.cpp b/kernel/kernel/ACPI/ACPI.cpp similarity index 95% rename from kernel/kernel/ACPI.cpp rename to kernel/kernel/ACPI/ACPI.cpp index 2d8ccf33a3..c7f812112d 100644 --- a/kernel/kernel/ACPI.cpp +++ b/kernel/kernel/ACPI/ACPI.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -9,15 +9,15 @@ #define RSPD_SIZE 20 #define RSPDv2_SIZE 36 -namespace Kernel +namespace Kernel::ACPI { - struct RSDT : public ACPI::SDTHeader + struct RSDT : public SDTHeader { uint32_t entries[]; } __attribute__((packed)); - struct XSDT : public ACPI::SDTHeader + struct XSDT : public SDTHeader { uint64_t entries[]; } __attribute__((packed)); @@ -82,7 +82,7 @@ namespace Kernel return nullptr; } - static bool is_valid_std_header(const ACPI::SDTHeader* header) + static bool is_valid_std_header(const SDTHeader* header) { uint8_t sum = 0; for (uint32_t i = 0; i < header->length; i++) @@ -225,7 +225,7 @@ namespace Kernel return {}; } - const ACPI::SDTHeader* ACPI::get_header(BAN::StringView signature, uint32_t index) + const SDTHeader* ACPI::get_header(BAN::StringView signature, uint32_t index) { if (signature.size() != 4) { diff --git a/kernel/kernel/APIC.cpp b/kernel/kernel/APIC.cpp index 0c46d6513b..03b089b772 100644 --- a/kernel/kernel/APIC.cpp +++ b/kernel/kernel/APIC.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -132,7 +132,7 @@ namespace Kernel return nullptr; } - const MADT* madt = (const MADT*)Kernel::ACPI::get().get_header("APIC"sv, 0); + const MADT* madt = (const MADT*)ACPI::ACPI::get().get_header("APIC"sv, 0); if (madt == nullptr) { dprintln("Could not find MADT header"); diff --git a/kernel/kernel/Input/PS2/Controller.cpp b/kernel/kernel/Input/PS2/Controller.cpp index ec6f30f5c2..14cf3ab27b 100644 --- a/kernel/kernel/Input/PS2/Controller.cpp +++ b/kernel/kernel/Input/PS2/Controller.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -244,7 +244,7 @@ namespace Kernel::Input // FIXME: Initialise USB Controllers // Determine if the PS/2 Controller Exists - auto* fadt = static_cast(ACPI::get().get_header("FACP"sv, 0)); + auto* fadt = static_cast(ACPI::ACPI::get().get_header("FACP"sv, 0)); if (fadt && fadt->revision > 1 && !(fadt->iapc_boot_arch & (1 << 1))) { dwarnln_if(DEBUG_PS2, "No PS/2 available"); diff --git a/kernel/kernel/Timer/HPET.cpp b/kernel/kernel/Timer/HPET.cpp index c4fb0771c2..6d27eaed71 100644 --- a/kernel/kernel/Timer/HPET.cpp +++ b/kernel/kernel/Timer/HPET.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -131,7 +131,7 @@ namespace Kernel BAN::ErrorOr HPET::initialize(bool force_pic) { - auto* header = static_cast(ACPI::get().get_header("HPET"sv, 0)); + auto* header = static_cast(ACPI::ACPI::get().get_header("HPET"sv, 0)); if (header == nullptr) return BAN::Error::from_errno(ENODEV); diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 0bfc7fbaa4..a4a47697c3 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -116,7 +116,7 @@ extern "C" void kernel_main(uint32_t boot_magic, uint32_t boot_info) parse_command_line(); dprintln("command line parsed, root='{}', console='{}'", cmdline.root, cmdline.console); - MUST(ACPI::initialize()); + MUST(ACPI::ACPI::initialize()); dprintln("ACPI initialized"); InterruptController::initialize(cmdline.force_pic); diff --git a/kernel/kernel/lai_host.cpp b/kernel/kernel/lai_host.cpp index d7b854b2d6..0390e97ed9 100644 --- a/kernel/kernel/lai_host.cpp +++ b/kernel/kernel/lai_host.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -51,7 +51,7 @@ void laihost_panic(const char* msg) void* laihost_scan(const char* sig, size_t index) { - return (void*)ACPI::get().get_header(sig, index); + return (void*)ACPI::ACPI::get().get_header(sig, index); } void* laihost_map(size_t address, size_t count)