diff --git a/include/kernel/infrastructure/acpi/asm/tables.h b/include/kernel/infrastructure/acpi/asm/tables.h new file mode 100644 index 00000000..e0fc7066 --- /dev/null +++ b/include/kernel/infrastructure/acpi/asm/tables.h @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2025 Philippe Aubertin. + * All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the author nor the names of other contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef JINUE_KERNEL_INFRASTRUCTURE_ACPI_ASM_TABLES_H +#define JINUE_KERNEL_INFRASTRUCTURE_ACPI_ASM_TABLES_H + +/* ACPI 6.4 section 5.2.3.2 Generic Address Structure (GAS) address space ID */ + +#define ACPI_GAS_ADDR_SPACE_SYSTEM_MEMORY 0x00 + +#define ACPI_GAS_ADDR_SPACE_SYSTEM_IO_SPACE 0x01 + +#define ACPI_GAS_ADDR_SPACE_PCI_CONFIG 0x02 + +#define ACPI_GAS_ADDR_SPACE_EMBEDDED_CONTROLLER 0x03 + +#define ACPI_GAS_ADDR_SPACE_SMBUS 0x04 + +#define ACPI_GAS_ADDR_SPACE_SYSTEM_CMOS 0x05 + +#define ACPI_GAS_ADDR_SPACE_PCI_BAR_TARGET 0x06 + +#define ACPI_GAS_ADDR_SPACE_IPMI 0x07 + +#define ACPI_GAS_ADDR_SPACE_GPIO 0x08 + +#define ACPI_GAS_ADDR_SPACE_GENERAL_SERIAL_BUS 0x09 + +#define ACPI_GAS_ADDR_SPACE_PCC 0x0a + +#define ACPI_GAS_ADDR_SPACE_FUNCTIONAL_FIXED_HARDWARE 0x7f + +/* ACPI 6.4 section 5.2.3.2 GAS access size */ + +#define ACPI_GAS_ACCESS_SIZE_UNDEFINED 0 + +#define ACPI_GAS_ACCESS_SIZE_BYTE_8 1 + +#define ACPI_GAS_ACCESS_SIZE_WORD_16 2 + +#define ACPI_GAS_ACCESS_SIZE_DWORD_32 3 + +#define ACPI_GAS_ACCESS_SIZE_QWORD_64 4 + +/* ACPI 6.4 section 5.2.9 values for Preferred_PM_Profile field of FADT */ + +#define ACPI_PM_PROFILE_UNSPECIFIED 0 + +#define ACPI_PM_PROFILE_DESKTOP 1 + +#define ACPI_PM_PROFILE_MOBILE 2 + +#define ACPI_PM_PROFILE_WORKSTATION 3 + +#define ACPI_PM_PROFILE_ENTERPRISE_SERVER 4 + +#define ACPI_PM_PROFILE_SOHO_SERVER 5 + +#define ACPI_PM_PROFILE_APPLIANCE_PC 6 + +#define ACPI_PM_PROFILE_PERFORMANCE_SERVER 7 + +#define ACPI_PM_PROFILE_TABLET 8 + +/* ACPI 6.4 section 5.2.9 + * Table 5.10 Fixed ACPI Description Table Fixed Feature Flags */ + +#define ACPI_FADT_FLAG_WBINVD (1 << 0) + +#define ACPI_FADT_FLAG_WBINVD_FLUSH (1 << 1) + +#define ACPI_FADT_FLAG_PROC_C1 (1 << 2) + +#define ACPI_FADT_FLAG_P_LVL2_UP (1 << 3) + +#define ACPI_FADT_FLAG_PWR_BUTTON (1 << 4) + +#define ACPI_FADT_FLAG_SLP_BUTTON (1 << 5) + +#define ACPI_FADT_FLAG_FIX_RTC (1 << 6) + +#define ACPI_FADT_FLAG_FIX_S4 (1 << 7) + +#define ACPI_FADT_FLAG_TMR_VAL_EXT (1 << 8) + +#define ACPI_FADT_FLAG_DCK_CAP (1 << 9) + +#define ACPI_FADT_FLAG_RESET_REG_SUP (1 << 10) + +#define ACPI_FADT_FLAG_SEALED_CASE (1 << 11) + +#define ACPI_FADT_FLAG_HEADLESS (1 << 12) + +#define ACPI_FADT_FLAG_CPU_SW_SLP (1 << 13) + +#define ACPI_FADT_FLAG_PCI_EXP_WAK (1 << 14) + +#define ACPI_FADT_FLAG_USE_PLATFORM_CLOCK (1 << 15) + +#define ACPI_FADT_FLAG_S4_RTC_STS_VALID (1 << 16) + +#define ACPI_FADT_FLAG_REMOTE_POWER_ON_CAPABLE (1 << 17) + +#define ACPI_FADT_FLAG_FORCE_APIC_CLUSTER_MODEL (1 << 18) + +#define ACPI_FADT_FLAG_FORCE_APIC_PHYS_DEST_MODE (1 << 19) + +#define ACPI_FADT_FLAG_HW_REDUCED_ACPI (1 << 20) + +#define ACPI_FADT_FLAG_LOW_POWER_S0_IDLE_CAPABLE (1 << 21) + +/* ACPI 6.4 section 5.2.9.3 IA-PC Boot Architecture Flags */ + +#define ACPI_IAPC_BOOT_ARCH_LEGACY_DEVICES (1 << 0) + +#define ACPI_IAPC_BOOT_ARCH_8042 (1 << 1) + +#define ACPI_IAPC_BOOT_ARCH_VGA_NOT_PRESENT (1 << 2) + +#define ACPI_IAPC_BOOT_ARCH_MSI_NOT_SUPPORTED (1 << 3) + +#define ACPI_IAPC_BOOT_ARCH_PCIE_ASPM_CONTROLS (1 << 4) + +#define ACPI_IAPC_BOOT_ARCH_CMOS_RTC_NOT_PRESENT (1 << 5) + +/* ACPI 6.4 section 5.2.9.4 ARM Architecture Boot Flags */ + +#define ACPI_ARM_BOOT_ARCH_PSCI_COMPLIANT (1 << 0) + +#define ACPI_ARM_BOOT_ARCH_PSCI_USE_HVC (1 << 1) + +/* ACPI 6.4 section 5.2.12 MADT flags */ + +#define ACPI_MADT_FLAG_PCAT_COMPAT (1 << 0) + +/* ACPI 6.4 section 5.2.12 MADT interrupt controller structure types */ + +#define ACPI_MADT_ENTRY_LOCAL_APIC 0 + +#define ACPI_MADT_ENTRY_IO_APIC 1 + +#define ACPI_MADT_ENTRY_SOURCE_OVERRIDE 2 + +#define ACPI_MADT_ENTRY_NMI_SOURCE 3 + +#define ACPI_MADT_ENTRY_LOCAL_APIC_NMI 4 + +#define ACPI_MADT_ENTRY_LOCAL_APIC_ADDR_OVERRIDE 5 + +#define ACPI_MADT_ENTRY_IO_SAPIC 6 + +#define ACPI_MADT_ENTRY_LOCAL_SAPIC 7 + +#define ACPI_MADT_ENTRY_PLATFORM_INTR_SOURCES 8 + +#define ACPI_MADT_ENTRY_LOCAL_X2APIC 9 + +#define ACPI_MADT_ENTRY_LOCAL_X2APIC_NMI 0xa + +#define ACPI_MADT_ENTRY_GIC_CPU_INTERFACE_GICC 0xb + +#define ACPI_MADT_ENTRY_GIC_DISTRIBUTOR_GICD 0xc + +#define ACPI_MADT_ENTRY_GIC_MSI_FRAME 0xd + +#define ACPI_MADT_ENTRY_GIC_REDISTRIBUTOR_GICR 0xe + +#define ACPI_MADT_ENTRY_GIC_INTR_TRANSLATION_ITS 0xf + +#define ACPI_MADT_ENTRY_MULTIPROCESSOR_WAKEUP 0x10 + +/* ACPI 6.4 section 5.2.12.2 MADT local APIC flags */ + +#define ACPI_MADT_LOCAL_APIC_FLAG_ENABLED (1 << 0) + +#define ACPI_MADT_LOCAL_APIC_FLAG_ONLINE_CAPABLE (1 << 1) + +/* ACPI 6.4 section 5.2.12.5 interrupt source override flags + * + * These flags are identical to the equivalent ones from version 1.4 of Intel's + * MultiProcessor Specification. + * + * These flags are described in table 5.26 of the ACPI specification. They are + * set in the flags field of the following structures in the MADT: + * - Interrupt Source Override Structure (type 2) + * - Non-Maskable Interrupt (NMI) Source Structure (type 3) + * - Local APIC NMI Structure (type 6) + * - Platform Interrupt Source Structure (type 8) + * - Local x2APIC NMI Structure (type 10/0xa) */ + +#define ACPI_MADT_MPS_INTI_FLAG_POLARITY (1 << 0) + +#define ACPI_MADT_MPS_INTI_TRIGGER_MODE_BUS (0 << 1) + +#define ACPI_MADT_MPS_INTI_TRIGGER_EDGE (1 << 1) + +#define ACPI_MADT_MPS_INTI_TRIGGER_LEVEL (3 << 1) + +#endif diff --git a/include/kernel/infrastructure/acpi/tables.h b/include/kernel/infrastructure/acpi/tables.h index e80a259e..f614b33b 100644 --- a/include/kernel/infrastructure/acpi/tables.h +++ b/include/kernel/infrastructure/acpi/tables.h @@ -32,10 +32,14 @@ #ifndef JINUE_KERNEL_INFRASTRUCTURE_ACPI_TABLES_H #define JINUE_KERNEL_INFRASTRUCTURE_ACPI_TABLES_H +#include #include +#include #include -typedef struct { +/* ACPI 6.4 section 5.2.5.3 Root System Description Pointer (RSDP) Structure */ + +typedef PACKED_STRUCT { char signature[8]; uint8_t checksum; char oemid[6]; @@ -47,24 +51,209 @@ typedef struct { uint8_t reserved[3]; } acpi_rsdp_t; +/* ACPI 6.4 section 5.2.6 System Description Table Header */ + typedef struct { + char signature[4]; + uint32_t length; + uint8_t revision; + uint8_t checksum; + char oemid[6]; + char oem_table_id[8]; + uint32_t oem_revision; + uint32_t creator_id; + uint32_t creator_revision; +} acpi_table_header_t; + +/* ACPI 6.4 section 5.2.3.2 Generic Address Structure (GAS) */ + +typedef PACKED_STRUCT { + uint8_t addr_space_id; + uint8_t reg_width; + uint8_t reg_offset; + uint8_t access_size; + uint64_t address; +} acpi_gas_t; + +/* ACPI 6.4 section 5.2.7 Root System Description Table (RSDT) and 5.2.8 + * Extended System Description Table (XSDT) */ + +typedef PACKED_STRUCT { acpi_table_header_t header; uint32_t entries[]; } acpi_rsdt_t; -typedef struct { +/* ACPI 6.4 section 5.2.9 Fixed ACPI Description Table (FADT) */ + +typedef PACKED_STRUCT { acpi_table_header_t header; - /* TODO */ + uint32_t firmware_ctrl; + uint32_t dsdt; + uint8_t reserved1; + uint8_t preferred_pm_profile; + uint16_t sci_int; + uint32_t sci_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; + acpi_gas_t reset_reg; + uint8_t reset_value; + uint16_t arm_boot_arch; + uint8_t fadt_minor_version; + uint64_t x_firmware_ctrl; + uint64_t x_dsdt; + acpi_gas_t x_pm1a_evt_blk; + acpi_gas_t x_pm1b_evt_blk; + acpi_gas_t x_pm1a_cnt_blk; + acpi_gas_t x_pm1b_cnt_blk; + acpi_gas_t x_pm2_cnt_blk; + acpi_gas_t x_pm_tmr_blk; + acpi_gas_t x_gpe0_blk; + acpi_gas_t x_gpe1_blk; + acpi_gas_t sleep_control_reg; + acpi_gas_t sleep_status_reg; + uint64_t hypervisor_vendor_identity; } acpi_fadt_t; -typedef struct { +/* ACPI 6.4 section 5.2.12 Multiple APIC Description Table (MADT) */ + +typedef PACKED_STRUCT { acpi_table_header_t header; - /* TODO */ + uint32_t local_intr_controller_addr; + uint32_t flags; + char entries[]; } acpi_madt_t; -typedef struct { +typedef PACKED_STRUCT { + uint8_t type; + uint8_t length; +} madt_entry_header_t; + +/* ACPI 6.4 section 5.2.12.2 Processor Local APIC Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint8_t processor_uid; + uint8_t apic_id; + uint32_t flags; +} acpi_madt_lapic_t; + +/* ACPI 6.4 section 5.2.12.3 I/O APIC Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint8_t apic_id; + uint8_t reserved; + uint32_t addr; + uint32_t intr_base; +} acpi_madt_ioapic_t; + +/* ACPI 6.4 section 5.2.12.5 Interrupt Source Override Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint8_t bus; + uint8_t source; + uint32_t global_sys_interrupt; + uint16_t flags; +} acpi_madt_src_override_t; + +/* ACPI 6.4 section 5.2.12.6 Non-Maskable Interrupt (NMI) Source Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint16_t flags; + uint32_t global_sys_interrupt; +} acpi_madt_nmi_source_t; + +/* ACPI 6.4 section 5.2.12.7 Local APIC NMI Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint8_t processor_uid; + uint16_t flags; + uint8_t lint_num; +} acpi_madt_lapic_nmi_t; + +/* ACPI 6.4 section 5.2.12.8 Local APIC Address Override Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint16_t reserved; + uint64_t lapic_addr; +} acpi_madt_lapic_addr_t; + +/* ACPI 6.4 section 5.2.12.12 Processor Local x2APIC Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint16_t reserved; + uint32_t apic_id; + uint32_t flags; + uint32_t processor_uid; +} acpi_madt_x2apic_t; + +/* ACPI 6.4 section 5.2.12.13. Local x2APIC NMI Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint16_t flags; + uint32_t processor_uid; + uint8_t lint_num; + uint8_t reserved[3]; +} acpi_madt_x2apic_nmi_t; + +/* ACPI 6.4 section 5.2.12.19 Multiprocessor Wakeup Structure */ + +typedef PACKED_STRUCT { + madt_entry_header_t header; + uint16_t mailbox_version; + uint32_t reserved; + uint64_t mailbox_addr; +} acpi_madt_wakeup_t; + +/* IA-PC HPET (High Precision Event Timers) Specification + * Section 3.2.4 "The ACPI 2.0 HPET Description Table (HPET)" + * + * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf + */ + +typedef PACKED_STRUCT { acpi_table_header_t header; - /* TODO */ + uint32_t event_timer_block_id; + acpi_gas_t base_address; + uint8_t hpet_number; + uint16_t periodic_min_tick; + uint8_t prot_and_oem; } acpi_hpet_t; #endif diff --git a/include/kernel/infrastructure/acpi/types.h b/include/kernel/infrastructure/acpi/types.h index 66c7c856..ef2218bc 100644 --- a/include/kernel/infrastructure/acpi/types.h +++ b/include/kernel/infrastructure/acpi/types.h @@ -34,24 +34,14 @@ #include +/* ACPI 6.4 section 15.1 INT 15H, E820H - Query System Address Map */ + typedef struct { uint64_t addr; uint64_t size; uint32_t type; } acpi_addr_range_t; -typedef struct { - char signature[4]; - uint32_t length; - uint8_t revision; - uint8_t checksum; - char oemid[6]; - char oem_table_id[8]; - uint32_t oem_revision; - uint32_t creator_id; - uint32_t creator_revision; -} acpi_table_header_t; - typedef struct { const char *signature; const void **ptr; diff --git a/include/kernel/infrastructure/compiler.h b/include/kernel/infrastructure/compiler.h index d71263c0..abca7519 100644 --- a/include/kernel/infrastructure/compiler.h +++ b/include/kernel/infrastructure/compiler.h @@ -38,4 +38,6 @@ #define NORETURN #endif +#define PACKED_STRUCT struct __attribute__((__packed__)) + #endif