2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-03-06 16:55:17 +02:00
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
2021-03-12 11:27:59 +01:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-03-12 17:29:37 +01:00
|
|
|
#include <AK/Format.h>
|
2020-03-23 13:45:10 +01:00
|
|
|
#include <AK/StringView.h>
|
2020-04-09 18:17:27 +02:00
|
|
|
#include <Kernel/ACPI/Parser.h>
|
2020-05-22 12:55:23 +02:00
|
|
|
#include <Kernel/Arch/PC/BIOS.h>
|
2021-06-21 17:34:09 +02:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
2021-06-25 09:46:17 +03:00
|
|
|
#include <Kernel/Bus/PCI/Access.h>
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2020-05-22 12:55:23 +02:00
|
|
|
#include <Kernel/IO.h>
|
2021-06-22 17:40:16 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2020-05-22 12:55:23 +02:00
|
|
|
#include <Kernel/StdLib.h>
|
2020-04-09 18:15:02 +02:00
|
|
|
#include <Kernel/VM/TypedMapping.h>
|
Kernel: Introduce the ACPI subsystem
ACPI subsystem includes 3 types of parsers that are created during
runtime, each one capable of parsing ACPI tables at different level.
ACPIParser is the most basic parser which is essentialy a parser that
can't parse anything useful, due to a user request to disable ACPI
support in a kernel boot parameter.
ACPIStaticParser is a derived class from ACPIParser, which is able to
parse only static data (e.g. FADT, HPET, MCFG and other tables), thus
making it not able to parse AML (ACPI Machine Language) nor to support
handling of hardware events and power management. This type of parser
can be created with a kernel boot parameter.
ACPIDynamicParser is a derived class from ACPIStaticParser, which
includes all the capabilities of the latter, but *should* implement an
AML interpretation, (by building the ACPI AML namespace) and handling
power & hardware events. Currently the methods to support AML
interpretation are not implemented.
This type of parser is created automatically during runtime if the user
didn't specify a boot parameter related to ACPI initialization.
Also, adding strncmp function definition in StdLib.h, to be able to use
it in ACPIStaticParser class.
2019-12-31 13:01:09 +02:00
|
|
|
|
2021-07-11 01:36:30 +02:00
|
|
|
namespace Kernel::ACPI {
|
2020-04-09 14:10:44 +02:00
|
|
|
|
2020-03-22 13:12:45 +13:00
|
|
|
static Parser* s_acpi_parser;
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2020-04-09 14:31:47 +02:00
|
|
|
Parser* Parser::the()
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2020-04-09 14:31:47 +02:00
|
|
|
return s_acpi_parser;
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
Kernel: Introduce the ACPI subsystem
ACPI subsystem includes 3 types of parsers that are created during
runtime, each one capable of parsing ACPI tables at different level.
ACPIParser is the most basic parser which is essentialy a parser that
can't parse anything useful, due to a user request to disable ACPI
support in a kernel boot parameter.
ACPIStaticParser is a derived class from ACPIParser, which is able to
parse only static data (e.g. FADT, HPET, MCFG and other tables), thus
making it not able to parse AML (ACPI Machine Language) nor to support
handling of hardware events and power management. This type of parser
can be created with a kernel boot parameter.
ACPIDynamicParser is a derived class from ACPIStaticParser, which
includes all the capabilities of the latter, but *should* implement an
AML interpretation, (by building the ACPI AML namespace) and handling
power & hardware events. Currently the methods to support AML
interpretation are not implemented.
This type of parser is created automatically during runtime if the user
didn't specify a boot parameter related to ACPI initialization.
Also, adding strncmp function definition in StdLib.h, to be able to use
it in ACPIStaticParser class.
2019-12-31 13:01:09 +02:00
|
|
|
|
2021-07-11 01:35:03 +02:00
|
|
|
UNMAP_AFTER_INIT NonnullRefPtr<ACPISysFSComponent> ACPISysFSComponent::create(String name, PhysicalAddress paddr, size_t table_size)
|
2021-03-13 12:01:44 +02:00
|
|
|
{
|
2021-07-11 01:35:03 +02:00
|
|
|
return adopt_ref(*new (nothrow) ACPISysFSComponent(name, paddr, table_size));
|
2021-03-13 12:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-11 01:35:03 +02:00
|
|
|
KResultOr<size_t> ACPISysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription*) const
|
2021-03-13 12:01:44 +02:00
|
|
|
{
|
|
|
|
|
auto blob = try_to_generate_buffer();
|
|
|
|
|
if (!blob)
|
|
|
|
|
return KResult(EFAULT);
|
|
|
|
|
|
|
|
|
|
if ((size_t)offset >= blob->size())
|
|
|
|
|
return KSuccess;
|
|
|
|
|
|
|
|
|
|
ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
|
|
|
|
|
if (!buffer.write(blob->data() + offset, nread))
|
|
|
|
|
return KResult(EFAULT);
|
|
|
|
|
return nread;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 01:35:03 +02:00
|
|
|
OwnPtr<KBuffer> ACPISysFSComponent::try_to_generate_buffer() const
|
2021-03-13 12:01:44 +02:00
|
|
|
{
|
|
|
|
|
auto acpi_blob = map_typed<u8>((m_paddr), m_length);
|
|
|
|
|
return KBuffer::try_create_with_bytes(Span<u8> { acpi_blob.ptr(), m_length });
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 01:35:03 +02:00
|
|
|
UNMAP_AFTER_INIT ACPISysFSComponent::ACPISysFSComponent(String name, PhysicalAddress paddr, size_t table_size)
|
2021-07-11 01:06:27 +02:00
|
|
|
: SysFSComponent(name)
|
2021-03-13 12:01:44 +02:00
|
|
|
, m_paddr(paddr)
|
|
|
|
|
, m_length(table_size)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 01:35:03 +02:00
|
|
|
UNMAP_AFTER_INIT void ACPISysFSDirectory::initialize()
|
2021-03-13 12:01:44 +02:00
|
|
|
{
|
2021-07-17 23:15:52 +02:00
|
|
|
auto acpi_directory = adopt_ref(*new (nothrow) ACPISysFSDirectory());
|
|
|
|
|
SysFSComponentRegistry::the().register_new_component(acpi_directory);
|
2021-03-13 12:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-11 01:35:03 +02:00
|
|
|
UNMAP_AFTER_INIT ACPISysFSDirectory::ACPISysFSDirectory()
|
2021-07-17 23:15:52 +02:00
|
|
|
: SysFSDirectory("acpi", SysFSComponentRegistry::the().root_directory())
|
2021-03-13 12:01:44 +02:00
|
|
|
{
|
2021-07-11 01:06:27 +02:00
|
|
|
NonnullRefPtrVector<SysFSComponent> components;
|
2021-03-13 12:01:44 +02:00
|
|
|
size_t ssdt_count = 0;
|
|
|
|
|
ACPI::Parser::the()->enumerate_static_tables([&](const StringView& signature, PhysicalAddress p_table, size_t length) {
|
|
|
|
|
if (signature == "SSDT") {
|
2021-07-11 01:35:03 +02:00
|
|
|
components.append(ACPISysFSComponent::create(String::formatted("{:4s}{}", signature.characters_without_null_termination(), ssdt_count), p_table, length));
|
2021-03-13 12:01:44 +02:00
|
|
|
ssdt_count++;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-11 01:35:03 +02:00
|
|
|
components.append(ACPISysFSComponent::create(signature, p_table, length));
|
2021-03-13 12:01:44 +02:00
|
|
|
});
|
|
|
|
|
m_components = components;
|
|
|
|
|
|
|
|
|
|
auto rsdp = map_typed<Structures::RSDPDescriptor20>(ACPI::Parser::the()->rsdp());
|
2021-07-11 01:35:03 +02:00
|
|
|
m_components.append(ACPISysFSComponent::create("RSDP", ACPI::Parser::the()->rsdp(), rsdp->base.revision == 0 ? sizeof(Structures::RSDPDescriptor) : rsdp->length));
|
2021-03-13 12:01:44 +02:00
|
|
|
|
|
|
|
|
auto main_system_description_table = map_typed<Structures::SDTHeader>(ACPI::Parser::the()->main_system_description_table());
|
|
|
|
|
if (ACPI::Parser::the()->is_xsdt_supported()) {
|
2021-07-11 01:35:03 +02:00
|
|
|
m_components.append(ACPISysFSComponent::create("XSDT", ACPI::Parser::the()->main_system_description_table(), main_system_description_table->length));
|
2021-03-13 12:01:44 +02:00
|
|
|
} else {
|
2021-07-11 01:35:03 +02:00
|
|
|
m_components.append(ACPISysFSComponent::create("RSDT", ACPI::Parser::the()->main_system_description_table(), main_system_description_table->length));
|
2021-03-13 12:01:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Parser::enumerate_static_tables(Function<void(const StringView&, PhysicalAddress, size_t)> callback)
|
|
|
|
|
{
|
|
|
|
|
for (auto& p_table : m_sdt_pointers) {
|
|
|
|
|
auto table = map_typed<Structures::SDTHeader>(p_table);
|
|
|
|
|
callback({ table->sig, 4 }, p_table, table->length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 14:10:44 +02:00
|
|
|
void Parser::set_the(Parser& parser)
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!s_acpi_parser);
|
2020-04-09 14:10:44 +02:00
|
|
|
s_acpi_parser = &parser;
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
Kernel: Introduce the ACPI subsystem
ACPI subsystem includes 3 types of parsers that are created during
runtime, each one capable of parsing ACPI tables at different level.
ACPIParser is the most basic parser which is essentialy a parser that
can't parse anything useful, due to a user request to disable ACPI
support in a kernel boot parameter.
ACPIStaticParser is a derived class from ACPIParser, which is able to
parse only static data (e.g. FADT, HPET, MCFG and other tables), thus
making it not able to parse AML (ACPI Machine Language) nor to support
handling of hardware events and power management. This type of parser
can be created with a kernel boot parameter.
ACPIDynamicParser is a derived class from ACPIStaticParser, which
includes all the capabilities of the latter, but *should* implement an
AML interpretation, (by building the ACPI AML namespace) and handling
power & hardware events. Currently the methods to support AML
interpretation are not implemented.
This type of parser is created automatically during runtime if the user
didn't specify a boot parameter related to ACPI initialization.
Also, adding strncmp function definition in StdLib.h, to be able to use
it in ACPIStaticParser class.
2019-12-31 13:01:09 +02:00
|
|
|
|
2020-04-09 18:15:02 +02:00
|
|
|
static bool match_table_signature(PhysicalAddress table_header, const StringView& signature);
|
|
|
|
|
static PhysicalAddress search_table_in_xsdt(PhysicalAddress xsdt, const StringView& signature);
|
|
|
|
|
static PhysicalAddress search_table_in_rsdt(PhysicalAddress rsdt, const StringView& signature);
|
|
|
|
|
static bool validate_table(const Structures::SDTHeader&, size_t length);
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT void Parser::locate_static_data()
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
|
|
|
|
locate_main_system_description_table();
|
|
|
|
|
initialize_main_system_description_table();
|
|
|
|
|
init_fadt();
|
|
|
|
|
init_facs();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT PhysicalAddress Parser::find_table(const StringView& signature)
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Calling Find Table method!");
|
2020-04-09 18:15:02 +02:00
|
|
|
for (auto p_sdt : m_sdt_pointers) {
|
|
|
|
|
auto sdt = map_typed<Structures::SDTHeader>(p_sdt);
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Examining Table @ {}", p_sdt);
|
2020-04-09 18:15:02 +02:00
|
|
|
if (!strncmp(sdt->sig, signature.characters_without_null_termination(), 4)) {
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Found Table @ {}", p_sdt);
|
2020-04-09 18:15:02 +02:00
|
|
|
return p_sdt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT void Parser::init_facs()
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
|
|
|
|
m_facs = find_table("FACS");
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT void Parser::init_fadt()
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: Initializing Fixed ACPI data");
|
|
|
|
|
dmesgln("ACPI: Searching for the Fixed ACPI Data Table");
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
m_fadt = find_table("FACP");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_fadt.is_null());
|
2020-04-09 18:15:02 +02:00
|
|
|
|
2021-07-02 08:39:41 +03:00
|
|
|
auto sdt = map_typed<const volatile Structures::FADT>(m_fadt);
|
2020-04-09 18:15:02 +02:00
|
|
|
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: FADT @ V{}, {}", &sdt, m_fadt);
|
2021-01-13 21:16:18 +01:00
|
|
|
|
2021-05-08 16:00:28 +03:00
|
|
|
auto* header = &sdt.ptr()->h;
|
|
|
|
|
dmesgln("ACPI: Fixed ACPI data, Revision {}, length: {} bytes", (size_t)header->revision, (size_t)header->length);
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: DSDT {}", PhysicalAddress(sdt->dsdt_ptr));
|
2020-04-09 18:15:02 +02:00
|
|
|
m_x86_specific_flags.cmos_rtc_not_present = (sdt->ia_pc_boot_arch_flags & (u8)FADTFlags::IA_PC_Flags::CMOS_RTC_Not_Present);
|
2020-12-17 21:37:37 +02:00
|
|
|
|
|
|
|
|
// FIXME: QEMU doesn't report that we have an i8042 controller in these flags, even if it should (when FADT revision is 3),
|
|
|
|
|
// Later on, we need to make sure that we enumerate the ACPI namespace (AML encoded), instead of just using this value.
|
|
|
|
|
m_x86_specific_flags.keyboard_8042 = (sdt->h.revision <= 3) || (sdt->ia_pc_boot_arch_flags & (u8)FADTFlags::IA_PC_Flags::PS2_8042);
|
|
|
|
|
|
2020-04-09 18:15:02 +02:00
|
|
|
m_x86_specific_flags.legacy_devices = (sdt->ia_pc_boot_arch_flags & (u8)FADTFlags::IA_PC_Flags::Legacy_Devices);
|
|
|
|
|
m_x86_specific_flags.msi_not_supported = (sdt->ia_pc_boot_arch_flags & (u8)FADTFlags::IA_PC_Flags::MSI_Not_Supported);
|
|
|
|
|
m_x86_specific_flags.vga_not_present = (sdt->ia_pc_boot_arch_flags & (u8)FADTFlags::IA_PC_Flags::VGA_Not_Present);
|
|
|
|
|
|
|
|
|
|
m_hardware_flags.cpu_software_sleep = (sdt->flags & (u32)FADTFlags::FeatureFlags::CPU_SW_SLP);
|
|
|
|
|
m_hardware_flags.docking_capability = (sdt->flags & (u32)FADTFlags::FeatureFlags::DCK_CAP);
|
|
|
|
|
m_hardware_flags.fix_rtc = (sdt->flags & (u32)FADTFlags::FeatureFlags::FIX_RTC);
|
|
|
|
|
m_hardware_flags.force_apic_cluster_model = (sdt->flags & (u32)FADTFlags::FeatureFlags::FORCE_APIC_CLUSTER_MODEL);
|
|
|
|
|
m_hardware_flags.force_apic_physical_destination_mode = (sdt->flags & (u32)FADTFlags::FeatureFlags::FORCE_APIC_PHYSICAL_DESTINATION_MODE);
|
|
|
|
|
m_hardware_flags.hardware_reduced_acpi = (sdt->flags & (u32)FADTFlags::FeatureFlags::HW_REDUCED_ACPI);
|
|
|
|
|
m_hardware_flags.headless = (sdt->flags & (u32)FADTFlags::FeatureFlags::HEADLESS);
|
|
|
|
|
m_hardware_flags.low_power_s0_idle_capable = (sdt->flags & (u32)FADTFlags::FeatureFlags::LOW_POWER_S0_IDLE_CAPABLE);
|
|
|
|
|
m_hardware_flags.multiprocessor_c2 = (sdt->flags & (u32)FADTFlags::FeatureFlags::P_LVL2_UP);
|
|
|
|
|
m_hardware_flags.pci_express_wake = (sdt->flags & (u32)FADTFlags::FeatureFlags::PCI_EXP_WAK);
|
|
|
|
|
m_hardware_flags.power_button = (sdt->flags & (u32)FADTFlags::FeatureFlags::PWR_BUTTON);
|
|
|
|
|
m_hardware_flags.processor_c1 = (sdt->flags & (u32)FADTFlags::FeatureFlags::PROC_C1);
|
|
|
|
|
m_hardware_flags.remote_power_on_capable = (sdt->flags & (u32)FADTFlags::FeatureFlags::REMOTE_POWER_ON_CAPABLE);
|
|
|
|
|
m_hardware_flags.reset_register_supported = (sdt->flags & (u32)FADTFlags::FeatureFlags::RESET_REG_SUPPORTED);
|
|
|
|
|
m_hardware_flags.rtc_s4 = (sdt->flags & (u32)FADTFlags::FeatureFlags::RTC_s4);
|
|
|
|
|
m_hardware_flags.s4_rtc_status_valid = (sdt->flags & (u32)FADTFlags::FeatureFlags::S4_RTC_STS_VALID);
|
|
|
|
|
m_hardware_flags.sealed_case = (sdt->flags & (u32)FADTFlags::FeatureFlags::SEALED_CASE);
|
|
|
|
|
m_hardware_flags.sleep_button = (sdt->flags & (u32)FADTFlags::FeatureFlags::SLP_BUTTON);
|
|
|
|
|
m_hardware_flags.timer_value_extension = (sdt->flags & (u32)FADTFlags::FeatureFlags::TMR_VAL_EXT);
|
|
|
|
|
m_hardware_flags.use_platform_clock = (sdt->flags & (u32)FADTFlags::FeatureFlags::USE_PLATFORM_CLOCK);
|
|
|
|
|
m_hardware_flags.wbinvd = (sdt->flags & (u32)FADTFlags::FeatureFlags::WBINVD);
|
|
|
|
|
m_hardware_flags.wbinvd_flush = (sdt->flags & (u32)FADTFlags::FeatureFlags::WBINVD_FLUSH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Parser::can_reboot()
|
|
|
|
|
{
|
|
|
|
|
auto fadt = map_typed<Structures::FADT>(m_fadt);
|
|
|
|
|
if (fadt->h.revision < 2)
|
|
|
|
|
return false;
|
|
|
|
|
return m_hardware_flags.reset_register_supported;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Parser::access_generic_address(const Structures::GenericAddressStructure& structure, u32 value)
|
|
|
|
|
{
|
|
|
|
|
switch ((GenericAddressStructure::AddressSpace)structure.address_space) {
|
|
|
|
|
case GenericAddressStructure::AddressSpace::SystemIO: {
|
|
|
|
|
IOAddress address(structure.address);
|
2021-01-25 12:52:31 +01:00
|
|
|
dbgln("ACPI: Sending value {:x} to {}", value, address);
|
2020-04-09 18:15:02 +02:00
|
|
|
switch (structure.access_size) {
|
|
|
|
|
case (u8)GenericAddressStructure::AccessSize::QWord: {
|
2021-01-09 00:42:44 +01:00
|
|
|
dbgln("Trying to send QWord to IO port");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-04-09 18:15:02 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case (u8)GenericAddressStructure::AccessSize::Undefined: {
|
2021-01-09 00:42:44 +01:00
|
|
|
dbgln("ACPI Warning: Unknown access size {}", structure.access_size);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(structure.bit_width != (u8)GenericAddressStructure::BitWidth::QWord);
|
|
|
|
|
VERIFY(structure.bit_width != (u8)GenericAddressStructure::BitWidth::Undefined);
|
2021-01-10 14:24:59 +01:00
|
|
|
dbgln("ACPI: Bit Width - {} bits", structure.bit_width);
|
2020-04-09 18:15:02 +02:00
|
|
|
address.out(value, structure.bit_width);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
address.out(value, (8 << (structure.access_size - 1)));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case GenericAddressStructure::AddressSpace::SystemMemory: {
|
2021-01-09 00:42:44 +01:00
|
|
|
dbgln("ACPI: Sending value {:x} to {}", value, PhysicalAddress(structure.address));
|
2020-04-09 18:15:02 +02:00
|
|
|
switch ((GenericAddressStructure::AccessSize)structure.access_size) {
|
|
|
|
|
case GenericAddressStructure::AccessSize::Byte:
|
|
|
|
|
*map_typed<u8>(PhysicalAddress(structure.address)) = value;
|
|
|
|
|
break;
|
|
|
|
|
case GenericAddressStructure::AccessSize::Word:
|
|
|
|
|
*map_typed<u16>(PhysicalAddress(structure.address)) = value;
|
|
|
|
|
break;
|
|
|
|
|
case GenericAddressStructure::AccessSize::DWord:
|
|
|
|
|
*map_typed<u32>(PhysicalAddress(structure.address)) = value;
|
|
|
|
|
break;
|
|
|
|
|
case GenericAddressStructure::AccessSize::QWord: {
|
|
|
|
|
*map_typed<u64>(PhysicalAddress(structure.address)) = value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-04-09 18:15:02 +02:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case GenericAddressStructure::AddressSpace::PCIConfigurationSpace: {
|
2021-05-31 18:25:27 +01:00
|
|
|
// According to https://uefi.org/specs/ACPI/6.4/05_ACPI_Software_Programming_Model/ACPI_Software_Programming_Model.html#address-space-format,
|
|
|
|
|
// PCI addresses must be confined to devices on Segment group 0, bus 0.
|
2020-04-09 18:15:02 +02:00
|
|
|
auto pci_address = PCI::Address(0, 0, ((structure.address >> 24) & 0xFF), ((structure.address >> 16) & 0xFF));
|
2021-01-09 00:42:44 +01:00
|
|
|
dbgln("ACPI: Sending value {:x} to {}", value, pci_address);
|
2020-04-09 18:15:02 +02:00
|
|
|
u32 offset_in_pci_address = structure.address & 0xFFFF;
|
|
|
|
|
if (structure.access_size == (u8)GenericAddressStructure::AccessSize::QWord) {
|
2021-01-09 00:42:44 +01:00
|
|
|
dbgln("Trying to send QWord to PCI configuration space");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-04-09 18:15:02 +02:00
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(structure.access_size != (u8)GenericAddressStructure::AccessSize::Undefined);
|
2020-04-09 18:15:02 +02:00
|
|
|
PCI::raw_access(pci_address, offset_in_pci_address, (1 << (structure.access_size - 1)), value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-04-09 18:15:02 +02:00
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
bool Parser::validate_reset_register()
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2021-05-31 18:25:27 +01:00
|
|
|
// According to https://uefi.org/specs/ACPI/6.4/04_ACPI_Hardware_Specification/ACPI_Hardware_Specification.html#reset-register,
|
|
|
|
|
// the reset register can only be located in I/O bus, PCI bus or memory-mapped.
|
2020-04-09 18:15:02 +02:00
|
|
|
auto fadt = map_typed<Structures::FADT>(m_fadt);
|
|
|
|
|
return (fadt->reset_reg.address_space == (u8)GenericAddressStructure::AddressSpace::PCIConfigurationSpace || fadt->reset_reg.address_space == (u8)GenericAddressStructure::AddressSpace::SystemMemory || fadt->reset_reg.address_space == (u8)GenericAddressStructure::AddressSpace::SystemIO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Parser::try_acpi_reboot()
|
|
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
if (!can_reboot()) {
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: Reboot not supported!");
|
2020-04-09 18:15:02 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2021-03-12 11:27:59 +01:00
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Rebooting, probing FADT ({})", m_fadt);
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
auto fadt = map_typed<Structures::FADT>(m_fadt);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(validate_reset_register());
|
2020-04-09 18:15:02 +02:00
|
|
|
access_generic_address(fadt->reset_reg, fadt->reset_value);
|
2020-07-06 07:27:22 -06:00
|
|
|
Processor::halt();
|
2020-04-09 18:15:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Parser::try_acpi_shutdown()
|
|
|
|
|
{
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: Shutdown is not supported with the current configuration, aborting!");
|
2020-04-09 18:15:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Parser::get_table_size(PhysicalAddress table_header)
|
|
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2021-05-01 21:10:08 +02:00
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Checking SDT Length");
|
2020-04-09 18:15:02 +02:00
|
|
|
return map_typed<Structures::SDTHeader>(table_header)->length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u8 Parser::get_table_revision(PhysicalAddress table_header)
|
|
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2021-05-01 21:10:08 +02:00
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Checking SDT Revision");
|
2020-04-09 18:15:02 +02:00
|
|
|
return map_typed<Structures::SDTHeader>(table_header)->revision;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT void Parser::initialize_main_system_description_table()
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
2021-05-01 21:10:08 +02:00
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Checking Main SDT Length to choose the correct mapping size");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_main_system_description_table.is_null());
|
2020-04-09 18:15:02 +02:00
|
|
|
auto length = get_table_size(m_main_system_description_table);
|
|
|
|
|
auto revision = get_table_revision(m_main_system_description_table);
|
|
|
|
|
|
|
|
|
|
auto sdt = map_typed<Structures::SDTHeader>(m_main_system_description_table, length);
|
|
|
|
|
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: Main Description Table valid? {}", validate_table(*sdt, length));
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
if (m_xsdt_supported) {
|
|
|
|
|
auto& xsdt = (const Structures::XSDT&)*sdt;
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: Using XSDT, enumerating tables @ {}", m_main_system_description_table);
|
|
|
|
|
dmesgln("ACPI: XSDT revision {}, total length: {}", revision, length);
|
|
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: XSDT pointer @ {}", VirtualAddress { &xsdt });
|
2020-04-09 18:15:02 +02:00
|
|
|
for (u32 i = 0; i < ((length - sizeof(Structures::SDTHeader)) / sizeof(u64)); i++) {
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Found new table [{0}], @ V{1:p} - P{1:p}", i, &xsdt.table_ptrs[i]);
|
2020-04-09 18:15:02 +02:00
|
|
|
m_sdt_pointers.append(PhysicalAddress(xsdt.table_ptrs[i]));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
auto& rsdt = (const Structures::RSDT&)*sdt;
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: Using RSDT, enumerating tables @ {}", m_main_system_description_table);
|
|
|
|
|
dmesgln("ACPI: RSDT revision {}, total length: {}", revision, length);
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: RSDT pointer @ V{}", &rsdt);
|
2020-04-09 18:15:02 +02:00
|
|
|
for (u32 i = 0; i < ((length - sizeof(Structures::SDTHeader)) / sizeof(u32)); i++) {
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(ACPI_DEBUG, "ACPI: Found new table [{0}], @ V{1:p} - P{1:p}", i, &rsdt.table_ptrs[i]);
|
2020-04-09 18:15:02 +02:00
|
|
|
m_sdt_pointers.append(PhysicalAddress(rsdt.table_ptrs[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT void Parser::locate_main_system_description_table()
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
|
|
|
|
auto rsdp = map_typed<Structures::RSDPDescriptor20>(m_rsdp);
|
|
|
|
|
if (rsdp->base.revision == 0) {
|
|
|
|
|
m_xsdt_supported = false;
|
|
|
|
|
} else if (rsdp->base.revision >= 2) {
|
|
|
|
|
if (rsdp->xsdt_ptr != (u64) nullptr) {
|
|
|
|
|
m_xsdt_supported = true;
|
|
|
|
|
} else {
|
|
|
|
|
m_xsdt_supported = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!m_xsdt_supported) {
|
|
|
|
|
m_main_system_description_table = PhysicalAddress(rsdp->base.rsdt_ptr);
|
|
|
|
|
} else {
|
|
|
|
|
m_main_system_description_table = PhysicalAddress(rsdp->xsdt_ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT Parser::Parser(PhysicalAddress rsdp)
|
2020-04-09 18:15:02 +02:00
|
|
|
: m_rsdp(rsdp)
|
|
|
|
|
{
|
2021-03-12 11:27:59 +01:00
|
|
|
dmesgln("ACPI: Using RSDP @ {}", rsdp);
|
2020-04-09 18:15:02 +02:00
|
|
|
locate_static_data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool validate_table(const Structures::SDTHeader& v_header, size_t length)
|
|
|
|
|
{
|
|
|
|
|
u8 checksum = 0;
|
|
|
|
|
auto* sdt = (const u8*)&v_header;
|
|
|
|
|
for (size_t i = 0; i < length; i++)
|
|
|
|
|
checksum += sdt[i];
|
|
|
|
|
if (checksum == 0)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 18:25:27 +01:00
|
|
|
// https://uefi.org/specs/ACPI/6.4/05_ACPI_Software_Programming_Model/ACPI_Software_Programming_Model.html#finding-the-rsdp-on-ia-pc-systems
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT Optional<PhysicalAddress> StaticParsing::find_rsdp()
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
2020-05-22 13:34:53 +02:00
|
|
|
StringView signature("RSD PTR ");
|
|
|
|
|
auto rsdp = map_ebda().find_chunk_starting_with(signature, 16);
|
|
|
|
|
if (rsdp.has_value())
|
2020-04-09 18:15:02 +02:00
|
|
|
return rsdp;
|
2020-05-22 13:34:53 +02:00
|
|
|
return map_bios().find_chunk_starting_with(signature, 16);
|
2020-04-09 18:15:02 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT PhysicalAddress StaticParsing::find_table(PhysicalAddress rsdp_address, const StringView& signature)
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
|
|
|
|
// FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(signature.length() == 4);
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
auto rsdp = map_typed<Structures::RSDPDescriptor20>(rsdp_address);
|
|
|
|
|
|
|
|
|
|
if (rsdp->base.revision == 0)
|
|
|
|
|
return search_table_in_rsdt(PhysicalAddress(rsdp->base.rsdt_ptr), signature);
|
|
|
|
|
|
|
|
|
|
if (rsdp->base.revision >= 2) {
|
|
|
|
|
if (rsdp->xsdt_ptr)
|
|
|
|
|
return search_table_in_xsdt(PhysicalAddress(rsdp->xsdt_ptr), signature);
|
|
|
|
|
return search_table_in_rsdt(PhysicalAddress(rsdp->base.rsdt_ptr), signature);
|
|
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-09 18:15:02 +02:00
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT static PhysicalAddress search_table_in_xsdt(PhysicalAddress xsdt_address, const StringView& signature)
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
|
|
|
|
// FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(signature.length() == 4);
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
auto xsdt = map_typed<Structures::XSDT>(xsdt_address);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ((xsdt->h.length - sizeof(Structures::SDTHeader)) / sizeof(u64)); ++i) {
|
|
|
|
|
if (match_table_signature(PhysicalAddress((FlatPtr)xsdt->table_ptrs[i]), signature))
|
|
|
|
|
return PhysicalAddress((FlatPtr)xsdt->table_ptrs[i]);
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool match_table_signature(PhysicalAddress table_header, const StringView& signature)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(signature.length() == 4);
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
auto table = map_typed<Structures::RSDT>(table_header);
|
|
|
|
|
return !strncmp(table->h.sig, signature.characters_without_null_termination(), 4);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT static PhysicalAddress search_table_in_rsdt(PhysicalAddress rsdt_address, const StringView& signature)
|
2020-04-09 18:15:02 +02:00
|
|
|
{
|
|
|
|
|
// FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(signature.length() == 4);
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
auto rsdt = map_typed<Structures::RSDT>(rsdt_address);
|
|
|
|
|
|
|
|
|
|
for (u32 i = 0; i < ((rsdt->h.length - sizeof(Structures::SDTHeader)) / sizeof(u32)); i++) {
|
|
|
|
|
if (match_table_signature(PhysicalAddress((FlatPtr)rsdt->table_ptrs[i]), signature))
|
|
|
|
|
return PhysicalAddress((FlatPtr)rsdt->table_ptrs[i]);
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Parser::enable_aml_interpretation()
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
void Parser::enable_aml_interpretation(File&)
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
void Parser::enable_aml_interpretation(u8*, u32)
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-09 18:15:02 +02:00
|
|
|
|
|
|
|
|
void Parser::disable_aml_interpretation()
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-09 18:15:02 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|