From 99bac4f34faf8ea3615ef684e7b73c954f12c226 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 22 Apr 2022 10:17:13 +0300 Subject: Kernel/SysFS: Split bulky SysFSPCI file into separate files --- Kernel/Bus/PCI/Initializer.cpp | 4 +- Kernel/CMakeLists.txt | 4 +- .../SysFS/Subsystems/Bus/PCI/BusDirectory.cpp | 31 +++++ .../SysFS/Subsystems/Bus/PCI/BusDirectory.h | 23 ++++ .../SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp | 95 ++++++++++++++ .../SysFS/Subsystems/Bus/PCI/DeviceAttribute.h | 33 +++++ .../SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp | 45 +++++++ .../SysFS/Subsystems/Bus/PCI/DeviceDirectory.h | 30 +++++ .../SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp | 139 --------------------- .../FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.h | 56 --------- 10 files changed, 262 insertions(+), 198 deletions(-) create mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp create mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h create mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp create mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.h create mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp create mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h delete mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp delete mode 100644 Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.h diff --git a/Kernel/Bus/PCI/Initializer.cpp b/Kernel/Bus/PCI/Initializer.cpp index fd51bdd488..2d3de9deac 100644 --- a/Kernel/Bus/PCI/Initializer.cpp +++ b/Kernel/Bus/PCI/Initializer.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -59,7 +59,7 @@ UNMAP_AFTER_INIT void initialize() VERIFY_NOT_REACHED(); } - PCI::PCIBusSysFSDirectory::initialize(); + PCIBusSysFSDirectory::initialize(); MUST(PCI::enumerate([&](DeviceIdentifier const& device_identifier) { dmesgln("{} {}", device_identifier.address(), device_identifier.hardware_id()); diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index b386093a00..be72abbe59 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -143,7 +143,9 @@ set(KERNEL_SOURCES FileSystem/ProcFS.cpp FileSystem/SysFS.cpp FileSystem/SysFS/Component.cpp - FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp + FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp + FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp + FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.cpp FileSystem/SysFS/Subsystems/Firmware/BIOS.cpp diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp new file mode 100644 index 0000000000..968c209485 --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include + +namespace Kernel { + +UNMAP_AFTER_INIT void PCIBusSysFSDirectory::initialize() +{ + auto pci_directory = adopt_ref(*new (nothrow) PCIBusSysFSDirectory()); + SysFSComponentRegistry::the().register_new_bus_directory(pci_directory); +} + +UNMAP_AFTER_INIT PCIBusSysFSDirectory::PCIBusSysFSDirectory() + : SysFSDirectory(SysFSComponentRegistry::the().buses_directory()) +{ + MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) { + auto pci_device = PCIDeviceSysFSDirectory::create(*this, device_identifier.address()); + m_components.append(pci_device); + })); +} + +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h new file mode 100644 index 0000000000..5f765a354a --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Kernel { + +class PCIBusSysFSDirectory final : public SysFSDirectory { +public: + static void initialize(); + virtual StringView name() const override { return "pci"sv; } + +private: + PCIBusSysFSDirectory(); +}; + +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp new file mode 100644 index 0000000000..652dfb5064 --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2021, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace Kernel { + +StringView PCIDeviceAttributeSysFSComponent::name() const +{ + switch (m_offset) { + case PCI::RegisterOffset::VENDOR_ID: + return "vendor"sv; + case PCI::RegisterOffset::DEVICE_ID: + return "device_id"sv; + case PCI::RegisterOffset::CLASS: + return "class"sv; + case PCI::RegisterOffset::SUBCLASS: + return "subclass"sv; + case PCI::RegisterOffset::REVISION_ID: + return "revision"sv; + case PCI::RegisterOffset::PROG_IF: + return "progif"sv; + case PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID: + return "subsystem_vendor"sv; + case PCI::RegisterOffset::SUBSYSTEM_ID: + return "subsystem_id"sv; + case PCI::RegisterOffset::BAR0: + return "bar0"sv; + case PCI::RegisterOffset::BAR1: + return "bar1"sv; + case PCI::RegisterOffset::BAR2: + return "bar2"sv; + case PCI::RegisterOffset::BAR3: + return "bar3"sv; + case PCI::RegisterOffset::BAR4: + return "bar4"sv; + case PCI::RegisterOffset::BAR5: + return "bar5"sv; + default: + VERIFY_NOT_REACHED(); + } +} + +NonnullRefPtr PCIDeviceAttributeSysFSComponent::create(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width) +{ + return adopt_ref(*new (nothrow) PCIDeviceAttributeSysFSComponent(device, offset, field_bytes_width)); +} + +PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width) + : SysFSComponent() + , m_device(device) + , m_offset(offset) + , m_field_bytes_width(field_bytes_width) +{ +} + +ErrorOr PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const +{ + auto blob = TRY(try_to_generate_buffer()); + + if ((size_t)offset >= blob->size()) + return 0; + + ssize_t nread = min(static_cast(blob->size() - offset), static_cast(count)); + TRY(buffer.write(blob->data() + offset, nread)); + return nread; +} + +ErrorOr> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const +{ + OwnPtr value; + switch (m_field_bytes_width) { + case 1: + value = TRY(KString::formatted("{:#x}", PCI::read8(m_device->address(), m_offset))); + break; + case 2: + value = TRY(KString::formatted("{:#x}", PCI::read16(m_device->address(), m_offset))); + break; + case 4: + value = TRY(KString::formatted("{:#x}", PCI::read32(m_device->address(), m_offset))); + break; + default: + VERIFY_NOT_REACHED(); + } + + return KBuffer::try_create_with_bytes(value->view().bytes()); +} +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.h b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.h new file mode 100644 index 0000000000..b6e198e80c --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Kernel { + +class PCIDeviceAttributeSysFSComponent : public SysFSComponent { +public: + static NonnullRefPtr create(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width); + + virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; + virtual ~PCIDeviceAttributeSysFSComponent() {}; + + virtual StringView name() const override; + +protected: + ErrorOr> try_to_generate_buffer() const; + PCIDeviceAttributeSysFSComponent(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width); + NonnullRefPtr m_device; + PCI::RegisterOffset m_offset; + size_t m_field_bytes_width; +}; + +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp new file mode 100644 index 0000000000..1431e68ac4 --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include + +namespace Kernel { + +UNMAP_AFTER_INIT NonnullRefPtr PCIDeviceSysFSDirectory::create(SysFSDirectory const& parent_directory, PCI::Address address) +{ + // FIXME: Handle allocation failure gracefully + auto device_name = MUST(KString::formatted("{:04x}:{:02x}:{:02x}.{}", address.domain(), address.bus(), address.device(), address.function())); + return adopt_ref(*new (nothrow) PCIDeviceSysFSDirectory(move(device_name), parent_directory, address)); +} + +UNMAP_AFTER_INIT PCIDeviceSysFSDirectory::PCIDeviceSysFSDirectory(NonnullOwnPtr device_directory_name, SysFSDirectory const& parent_directory, PCI::Address address) + : SysFSDirectory(parent_directory) + , m_address(address) + , m_device_directory_name(move(device_directory_name)) +{ + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::VENDOR_ID, 2)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::DEVICE_ID, 2)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::CLASS, 1)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBCLASS, 1)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::REVISION_ID, 1)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::PROG_IF, 1)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID, 2)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_ID, 2)); + + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR0, 4)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR1, 4)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR2, 4)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR3, 4)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR4, 4)); + m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR5, 4)); +} + +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h new file mode 100644 index 0000000000..700b86a170 --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Kernel { + +class PCIDeviceSysFSDirectory final : public SysFSDirectory { +public: + static NonnullRefPtr create(SysFSDirectory const&, PCI::Address); + PCI::Address const& address() const { return m_address; } + + virtual StringView name() const override { return m_device_directory_name->view(); } + +private: + PCIDeviceSysFSDirectory(NonnullOwnPtr device_directory_name, SysFSDirectory const&, PCI::Address); + + PCI::Address m_address; + + NonnullOwnPtr m_device_directory_name; +}; + +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp deleted file mode 100644 index 90948c5cb8..0000000000 --- a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2021, Liav A. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#include -#include - -namespace Kernel::PCI { - -UNMAP_AFTER_INIT NonnullRefPtr PCIDeviceSysFSDirectory::create(SysFSDirectory const& parent_directory, Address address) -{ - // FIXME: Handle allocation failure gracefully - auto device_name = MUST(KString::formatted("{:04x}:{:02x}:{:02x}.{}", address.domain(), address.bus(), address.device(), address.function())); - return adopt_ref(*new (nothrow) PCIDeviceSysFSDirectory(move(device_name), parent_directory, address)); -} - -UNMAP_AFTER_INIT PCIDeviceSysFSDirectory::PCIDeviceSysFSDirectory(NonnullOwnPtr device_directory_name, SysFSDirectory const& parent_directory, Address address) - : SysFSDirectory(parent_directory) - , m_address(address) - , m_device_directory_name(move(device_directory_name)) -{ - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::VENDOR_ID, 2)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::DEVICE_ID, 2)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::CLASS, 1)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBCLASS, 1)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::REVISION_ID, 1)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::PROG_IF, 1)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID, 2)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_ID, 2)); - - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR0, 4)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR1, 4)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR2, 4)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR3, 4)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR4, 4)); - m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR5, 4)); -} - -UNMAP_AFTER_INIT void PCIBusSysFSDirectory::initialize() -{ - auto pci_directory = adopt_ref(*new (nothrow) PCIBusSysFSDirectory()); - SysFSComponentRegistry::the().register_new_bus_directory(pci_directory); -} - -UNMAP_AFTER_INIT PCIBusSysFSDirectory::PCIBusSysFSDirectory() - : SysFSDirectory(SysFSComponentRegistry::the().buses_directory()) -{ - MUST(PCI::enumerate([&](DeviceIdentifier const& device_identifier) { - auto pci_device = PCI::PCIDeviceSysFSDirectory::create(*this, device_identifier.address()); - m_components.append(pci_device); - })); -} - -StringView PCIDeviceAttributeSysFSComponent::name() const -{ - switch (m_offset) { - case PCI::RegisterOffset::VENDOR_ID: - return "vendor"sv; - case PCI::RegisterOffset::DEVICE_ID: - return "device_id"sv; - case PCI::RegisterOffset::CLASS: - return "class"sv; - case PCI::RegisterOffset::SUBCLASS: - return "subclass"sv; - case PCI::RegisterOffset::REVISION_ID: - return "revision"sv; - case PCI::RegisterOffset::PROG_IF: - return "progif"sv; - case PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID: - return "subsystem_vendor"sv; - case PCI::RegisterOffset::SUBSYSTEM_ID: - return "subsystem_id"sv; - case PCI::RegisterOffset::BAR0: - return "bar0"sv; - case PCI::RegisterOffset::BAR1: - return "bar1"sv; - case PCI::RegisterOffset::BAR2: - return "bar2"sv; - case PCI::RegisterOffset::BAR3: - return "bar3"sv; - case PCI::RegisterOffset::BAR4: - return "bar4"sv; - case PCI::RegisterOffset::BAR5: - return "bar5"sv; - default: - VERIFY_NOT_REACHED(); - } -} - -NonnullRefPtr PCIDeviceAttributeSysFSComponent::create(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width) -{ - return adopt_ref(*new (nothrow) PCIDeviceAttributeSysFSComponent(device, offset, field_bytes_width)); -} - -PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width) - : SysFSComponent() - , m_device(device) - , m_offset(offset) - , m_field_bytes_width(field_bytes_width) -{ -} - -ErrorOr PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const -{ - auto blob = TRY(try_to_generate_buffer()); - - if ((size_t)offset >= blob->size()) - return 0; - - ssize_t nread = min(static_cast(blob->size() - offset), static_cast(count)); - TRY(buffer.write(blob->data() + offset, nread)); - return nread; -} - -ErrorOr> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const -{ - OwnPtr value; - switch (m_field_bytes_width) { - case 1: - value = TRY(KString::formatted("{:#x}", PCI::read8(m_device->address(), m_offset))); - break; - case 2: - value = TRY(KString::formatted("{:#x}", PCI::read16(m_device->address(), m_offset))); - break; - case 4: - value = TRY(KString::formatted("{:#x}", PCI::read32(m_device->address(), m_offset))); - break; - default: - VERIFY_NOT_REACHED(); - } - - return KBuffer::try_create_with_bytes(value->view().bytes()); -} -} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.h b/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.h deleted file mode 100644 index 80c09e46e5..0000000000 --- a/Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2021, Liav A. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include - -namespace Kernel::PCI { - -class PCIBusSysFSDirectory final : public SysFSDirectory { -public: - static void initialize(); - virtual StringView name() const override { return "pci"sv; } - -private: - PCIBusSysFSDirectory(); -}; - -class PCIDeviceSysFSDirectory final : public SysFSDirectory { -public: - static NonnullRefPtr create(SysFSDirectory const&, Address); - Address const& address() const { return m_address; } - - virtual StringView name() const override { return m_device_directory_name->view(); } - -private: - PCIDeviceSysFSDirectory(NonnullOwnPtr device_directory_name, SysFSDirectory const&, Address); - - Address m_address; - - NonnullOwnPtr m_device_directory_name; -}; - -class PCIDeviceAttributeSysFSComponent : public SysFSComponent { -public: - static NonnullRefPtr create(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width); - - virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override; - virtual ~PCIDeviceAttributeSysFSComponent() {}; - - virtual StringView name() const override; - -protected: - ErrorOr> try_to_generate_buffer() const; - PCIDeviceAttributeSysFSComponent(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width); - NonnullRefPtr m_device; - PCI::RegisterOffset m_offset; - size_t m_field_bytes_width; -}; - -} -- cgit v1.2.3