aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/CMakeLists.txt3
-rw-r--r--kernel/kernel/devices/pci/ecam_segment.cpp59
-rw-r--r--kernel/kernel/devices/pci/ecam_segment.hpp77
-rw-r--r--kernel/kernel/devices/pci/ecam_segment.tests.cpp317
-rw-r--r--kernel/kernel/devices/pci/init.cpp83
-rw-r--r--kernel/kernel/devices/pci/init.hpp14
-rw-r--r--kernel/kernel/devices/pci/registers.hpp51
-rw-r--r--kernel/kernel/devices/pci_express.cpp112
-rw-r--r--kernel/kernel/devices/pci_express.hpp14
-rw-r--r--kernel/kernel/main.cpp4
10 files changed, 605 insertions, 129 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index 705f3a85..df65ed20 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -43,7 +43,8 @@ target_sources("kernel_lib" PRIVATE
# Device Subsystem
"kernel/devices/block_device_utils.cpp"
"kernel/devices/init.cpp"
- "kernel/devices/pci_express.cpp"
+ "kernel/devices/pci/ecam_segment.cpp"
+ "kernel/devices/pci/init.cpp"
"kernel/devices/pseudo.cpp"
"kernel/devices/storage.cpp"
diff --git a/kernel/kernel/devices/pci/ecam_segment.cpp b/kernel/kernel/devices/pci/ecam_segment.cpp
new file mode 100644
index 00000000..5464ec4b
--- /dev/null
+++ b/kernel/kernel/devices/pci/ecam_segment.cpp
@@ -0,0 +1,59 @@
+#include <kernel/devices/pci/ecam_segment.hpp>
+
+#include <kernel/devices/pci/registers.hpp>
+
+#include <kapi/devices.hpp>
+#include <kapi/memory.hpp>
+
+#include <kstd/print.hpp>
+#include <kstd/result.hpp>
+#include <kstd/system_error.hpp>
+#include <kstd/units.hpp>
+
+#include <cstddef>
+#include <cstdint>
+
+namespace kernel::devices::pci
+{
+
+ ecam_segment::ecam_segment(kapi::devices::pcie_segment_descriptor const & descriptor)
+ : m_base{descriptor.base_address}
+ , m_count{descriptor.bus_count}
+ , m_group{descriptor.group_number}
+ , m_start{descriptor.first_bus}
+ {
+ if (m_count > 256uz - m_start)
+ {
+ kstd::println(kstd::print_sink::stderr, "[OS:DEV] Invalid PCI bus count '{}' for start bus '{}'",
+ descriptor.bus_count, m_start);
+ m_count = 0;
+ }
+ }
+
+ auto ecam_segment::contains(std::size_t bus) const noexcept -> bool
+ {
+ return bus >= m_start && bus < (m_start + m_count);
+ }
+
+ auto ecam_segment::first_bus() const noexcept -> std::uint8_t
+ {
+ return m_start;
+ }
+
+ auto ecam_segment::group() const noexcept -> std::uint16_t
+ {
+ return m_group;
+ }
+
+ auto ecam_segment::map_bus(std::uint8_t bus) const -> kstd::result<kapi::memory::mmio_window>
+ {
+ if (!contains(bus))
+ {
+ return kstd::failure(make_error_code(kstd::errc::invalid_argument));
+ }
+
+ return kapi::memory::mmio_window::map(m_base + bus * mmio_window_per_bus, mmio_window_per_bus,
+ kapi::memory::mmio_window::access::read_only);
+ }
+
+} // namespace kernel::devices::pci
diff --git a/kernel/kernel/devices/pci/ecam_segment.hpp b/kernel/kernel/devices/pci/ecam_segment.hpp
new file mode 100644
index 00000000..8f9e18bd
--- /dev/null
+++ b/kernel/kernel/devices/pci/ecam_segment.hpp
@@ -0,0 +1,77 @@
+#ifndef TEACHOS_KERNEL_DEVICES_PCI_ECAM_SEGMENT_HPP
+#define TEACHOS_KERNEL_DEVICES_PCI_ECAM_SEGMENT_HPP
+
+#include <kernel/devices/pci/registers.hpp>
+
+#include <kapi/devices.hpp>
+#include <kapi/memory.hpp>
+
+#include <kstd/result.hpp>
+#include <kstd/units.hpp>
+
+#include <cstddef>
+#include <cstdint>
+
+namespace kernel::devices::pci
+{
+
+ //! An ECAM (Enhanced Configuration Access Mechanism) segment representing the configuration space of a PCI segment.
+ struct ecam_segment
+ {
+ //! Construct a new ECAM segment from a PCIe segment descriptor provided by the platform.
+ explicit ecam_segment(kapi::devices::pcie_segment_descriptor const & descriptor);
+
+ //! Check if this ECAM segment contains the given bus number.
+ //!
+ //! @param bus The bus number to check.
+ //! @return @p true iff. the bus is within this segment, false otherwise.
+ [[nodiscard]] auto contains(std::size_t bus) const noexcept -> bool;
+
+ //! Get the number of the first bus in this ECAM segment.
+ //!
+ //! @return The number of the first bus.
+ [[nodiscard]] auto first_bus() const noexcept -> std::uint8_t;
+
+ //! Get the PCI segment group number of this ECAM segment.
+ //!
+ //! @return The PCI segment group number.
+ [[nodiscard]] auto group() const noexcept -> std::uint16_t;
+
+ //! Create an MMIO mapping for the configuration space of the given bus.
+ //!
+ //! @warning This function will panic if the MMIO subsystem has not been initialized.
+ //!
+ //! @param bus The bus number to map.
+ //! @return An MMIO window for the bus if successful, an error otherwise.
+ [[nodiscard]] auto map_bus(std::uint8_t bus) const -> kstd::result<kapi::memory::mmio_window>;
+
+ private:
+ //! The hardware base address of the segment.
+ kapi::memory::physical_address m_base;
+ //! The number of buses in the segment.
+ std::size_t m_count;
+ //! The PCI segment group number.
+ std::uint16_t m_group;
+ //! The starting bus number of the segment.
+ std::uint8_t m_start;
+ };
+
+ //! Calculate the offset of a specific device function inside the configuration space of a bus.
+ //!
+ //! @param device The device number.
+ //! @param function The function number.
+ //! @return The byte offset of the function within the device's configuration space.
+ constexpr auto function_offset(std::uint8_t device, std::uint8_t function) noexcept -> kstd::bytes
+ {
+ return kstd::bytes((device << device_address_shift) + (function << function_address_shift));
+ }
+
+ static_assert(function_offset(0, 0) == kstd::bytes{0});
+ static_assert(function_offset(1, 0) == kstd::bytes{0x8000});
+ static_assert(function_offset(0, 1) == kstd::bytes{0x1000});
+ // NOLINTNEXTLINE(readability-magic-numbers)
+ static_assert(function_offset(devices_per_bus - 1, functions_per_device - 1) == kstd::bytes{0xFF000});
+
+} // namespace kernel::devices::pci
+
+#endif
diff --git a/kernel/kernel/devices/pci/ecam_segment.tests.cpp b/kernel/kernel/devices/pci/ecam_segment.tests.cpp
new file mode 100644
index 00000000..513ce6d1
--- /dev/null
+++ b/kernel/kernel/devices/pci/ecam_segment.tests.cpp
@@ -0,0 +1,317 @@
+#include <kernel/devices/pci/ecam_segment.hpp>
+
+#include <kernel/devices/pci/registers.hpp>
+#include <kernel/test_support/memory.hpp>
+
+#include <kapi/devices.hpp>
+#include <kapi/memory.hpp>
+
+#include <kstd/system_error.hpp>
+#include <kstd/units.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/generators/catch_generators.hpp>
+
+#include <cstddef>
+#include <cstdint>
+#include <limits>
+
+namespace
+{
+ constexpr auto ecam_test_base = kapi::memory::physical_address{};
+
+ struct test_descriptor_data
+ {
+ std::uint8_t first_bus;
+ std::size_t bus_count;
+ };
+} // namespace
+
+SCENARIO("ECAM segment basic operations", "[devices][pci][ecam]")
+{
+ GIVEN("A valid segment group descriptor")
+ {
+ auto const test_data = GENERATE(test_descriptor_data{0, 256}, // maximum bus count
+ test_descriptor_data{255, 1}, // single bus
+ test_descriptor_data{16, 32} // multiple buses, far from the edge
+ );
+
+ auto const descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = ecam_test_base,
+ .group_number = 3,
+ .first_bus = test_data.first_bus,
+ .bus_count = test_data.bus_count,
+ };
+
+ WHEN("constructing an ECAM segment with the descriptor")
+ {
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ THEN("the segment contains its buses")
+ {
+ for (std::size_t bus = descriptor.first_bus; bus < descriptor.first_bus + descriptor.bus_count; ++bus)
+ {
+ REQUIRE(segment.contains(bus));
+ }
+ }
+
+ THEN("the segment does not contain buses beyond its last bus")
+ {
+ for (std::size_t bus = descriptor.first_bus + descriptor.bus_count; bus < 256 + 1; ++bus)
+ {
+ REQUIRE_FALSE(segment.contains(bus));
+ }
+ }
+
+ THEN("the segment does not contain any buses before its first bus")
+ {
+ for (std::size_t bus = 0; bus < descriptor.first_bus; ++bus)
+ {
+ REQUIRE_FALSE(segment.contains(bus));
+ }
+ }
+
+ THEN("the segment group is correct")
+ {
+ REQUIRE(segment.group() == descriptor.group_number);
+ }
+
+ THEN("the first bus is correct")
+ {
+ REQUIRE(segment.first_bus() == descriptor.first_bus);
+ }
+ }
+ }
+
+ GIVEN("A segment with zero buses")
+ {
+ auto const descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = ecam_test_base,
+ .group_number = 0,
+ .first_bus = 64,
+ .bus_count = 0,
+ };
+
+ WHEN("constructing an ECAM segment with the descriptor")
+ {
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ THEN("the segment does not contain any buses")
+ {
+ for (std::size_t bus = 0; bus < 256; ++bus)
+ {
+ REQUIRE_FALSE(segment.contains(bus));
+ }
+ }
+ }
+ }
+
+ GIVEN("An invalid segment group descriptor")
+ {
+ auto const test_data =
+ GENERATE(test_descriptor_data{255, 2}, // too many buses
+ test_descriptor_data{1, 256}, // too many buses
+ test_descriptor_data{0, std::numeric_limits<std::size_t>::max()} // way too many buses
+ );
+
+ auto const descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = ecam_test_base,
+ .group_number = 0,
+ .first_bus = test_data.first_bus,
+ .bus_count = test_data.bus_count,
+ };
+
+ WHEN("constructing an ECAM segment with the descriptor")
+ {
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ THEN("the segment does not contain any buses")
+ {
+ for (std::size_t bus = 0; bus < 256; ++bus)
+ {
+ REQUIRE_FALSE(segment.contains(bus));
+ }
+ }
+ }
+ }
+}
+
+SCENARIO("ECAM segment mapping", "[devices][pci][ecam]")
+{
+ GIVEN("A valid, non-empty segment")
+ {
+ constexpr auto descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = ecam_test_base,
+ .group_number = 0,
+ .first_bus = 1,
+ .bus_count = (kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus) - 1,
+ };
+
+ STATIC_REQUIRE(descriptor.bus_count > 0);
+
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ THEN("all of its buses can be mapped")
+ {
+ for (std::size_t bus = descriptor.first_bus; bus < descriptor.first_bus + descriptor.bus_count; ++bus)
+ {
+ REQUIRE(segment.map_bus(bus));
+ }
+ }
+
+ THEN("buses outside the segment cannot be mapped")
+ {
+ auto const lower_result = segment.map_bus(descriptor.first_bus + descriptor.bus_count);
+ REQUIRE_FALSE(lower_result);
+ REQUIRE(lower_result.error() == kstd::errc::invalid_argument);
+
+ auto const upper_result = segment.map_bus(descriptor.first_bus - 1);
+ REQUIRE_FALSE(upper_result);
+ REQUIRE(upper_result.error() == kstd::errc::invalid_argument);
+ }
+
+ WHEN("there is data to read")
+ {
+ // We calculate the base address ourselves to ensure we don't rely on potentially faulty constants.
+ //
+ // Each device's ECAM space is 4 KiB * 8 [functions per devices] large.
+ // Each bus' ECAM space is 32 [max devices per bus] * [device ECAM space size] large.
+ // A bus' ECAM space starts at the base address of the segment + [bus number] * [bus ECAM space size]
+ // Therefore we get:
+ constexpr auto bytes_per_device = kstd::KiB(4u) * 8;
+ constexpr auto bytes_per_bus = 32 * bytes_per_device;
+ constexpr auto hw_base = ecam_test_base + (descriptor.first_bus * bytes_per_bus);
+
+ auto data_window =
+ kapi::memory::mmio_window::map(hw_base, bytes_per_bus, kapi::memory::mmio_window::access::writable);
+
+ REQUIRE(data_window);
+
+ data_window->write<std::uint16_t>(kstd::bytes{0}, 0x1000);
+ data_window->write<std::uint16_t>(kstd::bytes{2}, 0x2000);
+ data_window->write<std::uint16_t>(kstd::bytes{4}, 0x4000);
+
+ THEN("the data can be read back correctly")
+ {
+ auto bus = segment.map_bus(descriptor.first_bus);
+
+ REQUIRE(bus->read<std::uint16_t>(kstd::bytes{0}) == 0x1000);
+ REQUIRE(bus->read<std::uint16_t>(kstd::bytes{2}) == 0x2000);
+ REQUIRE(bus->read<std::uint16_t>(kstd::bytes{4}) == 0x4000);
+ }
+ }
+ }
+
+ GIVEN("A valid, non-empty segment at a non-zero base address")
+ {
+ constexpr auto non_zero_base = ecam_test_base + kstd::MiB(1u);
+
+ constexpr auto descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = non_zero_base,
+ .group_number = 0,
+ .first_bus = 1,
+ .bus_count = (kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus) - 2,
+ };
+
+ STATIC_REQUIRE(descriptor.bus_count > 0);
+
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ WHEN("there is data to read")
+ {
+ // We calculate the base address ourselves to ensure we don't rely on potentially faulty constants.
+ //
+ // Each device's ECAM space is 4 KiB * 8 [functions per devices] large.
+ // Each bus' ECAM space is 32 [max devices per bus] * [device ECAM space size] large.
+ // A bus' ECAM space starts at the base address of the segment + [bus number] * [bus ECAM space size]
+ // Therefore we get:
+ constexpr auto bytes_per_device = kstd::KiB(4u) * 8;
+ constexpr auto bytes_per_bus = 32 * bytes_per_device;
+ constexpr auto hw_base = non_zero_base + (descriptor.first_bus * bytes_per_bus);
+
+ auto data_window =
+ kapi::memory::mmio_window::map(hw_base, bytes_per_bus, kapi::memory::mmio_window::access::writable);
+
+ REQUIRE(data_window);
+
+ data_window->write<std::uint16_t>(kstd::bytes{0}, 0x1000);
+ data_window->write<std::uint16_t>(kstd::bytes{2}, 0x2000);
+ data_window->write<std::uint16_t>(kstd::bytes{4}, 0x4000);
+
+ THEN("the data can be read back correctly")
+ {
+ auto bus = segment.map_bus(descriptor.first_bus);
+
+ REQUIRE(bus->read<std::uint16_t>(kstd::bytes{0}) == 0x1000);
+ REQUIRE(bus->read<std::uint16_t>(kstd::bytes{2}) == 0x2000);
+ REQUIRE(bus->read<std::uint16_t>(kstd::bytes{4}) == 0x4000);
+ }
+ }
+ }
+
+ GIVEN("A valid segment whose buses fall outside the device space")
+ {
+ auto const descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = ecam_test_base + kernel::tests::memory::mmio_size,
+ .group_number = 0,
+ .first_bus = (kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus) / 2,
+ .bus_count = 256 - ((kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus) / 2),
+ };
+
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ THEN("no buses can be mapped")
+ {
+ for (std::size_t bus = descriptor.first_bus; bus < descriptor.first_bus + descriptor.bus_count; ++bus)
+ {
+ auto const result = segment.map_bus(bus);
+ REQUIRE_FALSE(result);
+ REQUIRE(result.error() == kstd::errc::not_enough_memory);
+ }
+ }
+ }
+
+ GIVEN("An empty segment")
+ {
+ auto const descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = ecam_test_base,
+ .group_number = 0,
+ .first_bus = 0,
+ .bus_count = 0,
+ };
+
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ THEN("no buses can be mapped")
+ {
+ for (std::size_t bus = 0; bus < 256; ++bus)
+ {
+ auto const result = segment.map_bus(bus);
+ REQUIRE_FALSE(result);
+ REQUIRE(result.error() == kstd::errc::invalid_argument);
+ }
+ }
+ }
+
+ GIVEN("An invalid segment")
+ {
+ auto const descriptor = kapi::devices::pcie_segment_descriptor{
+ .base_address = ecam_test_base,
+ .group_number = 0,
+ .first_bus = 255,
+ .bus_count = 2,
+ };
+
+ auto const segment = kernel::devices::pci::ecam_segment{descriptor};
+
+ THEN("no buses can be mapped")
+ {
+ for (std::size_t bus = 0; bus < 256; ++bus)
+ {
+ auto const result = segment.map_bus(bus);
+ REQUIRE_FALSE(result);
+ REQUIRE(result.error() == kstd::errc::invalid_argument);
+ }
+ }
+ }
+}
diff --git a/kernel/kernel/devices/pci/init.cpp b/kernel/kernel/devices/pci/init.cpp
new file mode 100644
index 00000000..05f5e7d1
--- /dev/null
+++ b/kernel/kernel/devices/pci/init.cpp
@@ -0,0 +1,83 @@
+#include <kernel/devices/pci/init.hpp>
+
+#include <kernel/devices/pci/ecam_segment.hpp>
+#include <kernel/devices/pci/registers.hpp>
+
+#include <kapi/devices.hpp>
+#include <kapi/memory.hpp>
+
+#include <kstd/print.hpp>
+#include <kstd/vector.hpp>
+
+#include <cstddef>
+#include <cstdint>
+
+namespace kernel::devices::pci
+{
+
+ namespace
+ {
+
+ auto enumerate(kapi::devices::pcie_segment_descriptor const & descriptor) -> void
+ {
+ auto segment = ecam_segment{descriptor};
+
+ for (std::size_t bus = segment.first_bus(); segment.contains(bus); ++bus)
+ {
+ // Note: ecam_segment::contains(...) == true guarantees the bus number fits in a uint8_t
+ auto bus_window = segment.map_bus(static_cast<std::uint8_t>(bus));
+
+ if (!bus_window)
+ {
+ kstd::println("[OS:DEV] Failed to map MMIO window for bus {}", bus);
+ continue;
+ }
+
+ for (std::uint8_t device = 0; device < devices_per_bus; ++device)
+ {
+ auto device_base = function_offset(device, 0);
+ auto device_vendor_id = bus_window->read<std::uint16_t>(device_base + vendor_id_register_offset);
+ if (device_vendor_id == invalid_vendor_id)
+ {
+ continue;
+ }
+
+ auto device_header_type = bus_window->read<std::uint8_t>(device_base + header_type_register_offset);
+ auto function_count = (device_header_type & 0x80) ? functions_per_device : 1u;
+
+ for (std::uint8_t function = 0; function < function_count; ++function)
+ {
+ auto function_base = function_offset(device, function);
+ auto function_vendor_id = bus_window->read<std::uint16_t>(function_base + vendor_id_register_offset);
+ if (function_vendor_id == invalid_vendor_id)
+ {
+ continue;
+ }
+
+ auto function_device_id = bus_window->read<std::uint16_t>(function_base + device_id_register_offset);
+ kstd::println("[OS:DEV] Detected PCI Express device: {:04x}:{:02x}:{:02x}.{:1} [{:04x}:{:04x}]",
+ segment.group(), bus, device, function, function_vendor_id, function_device_id);
+ }
+ }
+ }
+ }
+
+ } // namespace
+
+ auto init() -> void
+ {
+ auto segments = kapi::devices::get_pci_express_segment_descriptors();
+
+ if (segments.empty())
+ {
+ return;
+ }
+
+ kstd::println("[OS:DEV] Detected number of PCI Express segments: {}", segments.size());
+ for (auto & segment : segments)
+ {
+ enumerate(segment);
+ }
+ }
+
+} // namespace kernel::devices::pci
diff --git a/kernel/kernel/devices/pci/init.hpp b/kernel/kernel/devices/pci/init.hpp
new file mode 100644
index 00000000..8a49acbc
--- /dev/null
+++ b/kernel/kernel/devices/pci/init.hpp
@@ -0,0 +1,14 @@
+#ifndef TEACHOS_KERNEL_DEVICES_PCI_INIT_HPP
+#define TEACHOS_KERNEL_DEVICES_PCI_INIT_HPP
+
+namespace kernel::devices::pci
+{
+
+ //! Initialize all PCI/PCIe devices in the system.
+ //!
+ //! This function scans and initializes all PCI and PCI Express devices present in the system.
+ auto init() -> void;
+
+} // namespace kernel::devices::pci
+
+#endif
diff --git a/kernel/kernel/devices/pci/registers.hpp b/kernel/kernel/devices/pci/registers.hpp
new file mode 100644
index 00000000..2e426be1
--- /dev/null
+++ b/kernel/kernel/devices/pci/registers.hpp
@@ -0,0 +1,51 @@
+#ifndef TEACHOS_KERNEL_DEVICES_PCI_REGISTERS_HPP
+#define TEACHOS_KERNEL_DEVICES_PCI_REGISTERS_HPP
+
+#include <kstd/units.hpp>
+
+#include <cstdint>
+
+namespace kernel::devices::pci
+{
+
+ //! The maximum number of devices per PCI bus.
+ constexpr auto inline devices_per_bus = 32u;
+
+ //! The maximum number of functions per PCI device.
+ constexpr auto inline functions_per_device = 8u;
+
+ //! The size of the configuration space for a single PCI function.
+ constexpr auto inline config_space_per_function = kstd::KiB(4u);
+
+ //! The size of the MMIO window for a single PCI bus.
+ constexpr auto inline mmio_window_per_bus = devices_per_bus * functions_per_device * config_space_per_function;
+
+ //! The number of bits to shift the bus number when creating a PCI window address.
+ constexpr auto inline bus_address_shift = 20u;
+
+ //! The number of bits to shift the device number when creating a PCI window address.
+ constexpr auto inline device_address_shift = 15u;
+
+ //! The number of bits to shift the function number when creating a PCI window address.
+ constexpr auto inline function_address_shift = 12u;
+
+ static_assert(config_space_per_function * devices_per_bus * functions_per_device == mmio_window_per_bus);
+ static_assert(kstd::bytes{1 << bus_address_shift} == mmio_window_per_bus);
+ static_assert(kstd::bytes{1 << device_address_shift} == functions_per_device * config_space_per_function);
+ static_assert(kstd::bytes{1 << function_address_shift} == config_space_per_function);
+
+ //! The vendor ID value indicating an invalid or non-existent device.
+ constexpr auto inline invalid_vendor_id = std::uint16_t{0xffff};
+
+ //! The offset of the vendor ID register within the PCI configuration space.
+ constexpr auto inline vendor_id_register_offset = kstd::bytes{0x00};
+
+ //! The offset of the device ID register within the PCI configuration space.
+ constexpr auto inline device_id_register_offset = kstd::bytes{0x02};
+
+ //! The offset of the header type register within the PCI configuration space.
+ constexpr auto inline header_type_register_offset = kstd::bytes{0x0e};
+
+} // namespace kernel::devices::pci
+
+#endif
diff --git a/kernel/kernel/devices/pci_express.cpp b/kernel/kernel/devices/pci_express.cpp
deleted file mode 100644
index 46ed2c1c..00000000
--- a/kernel/kernel/devices/pci_express.cpp
+++ /dev/null
@@ -1,112 +0,0 @@
-#include <kernel/devices/pci_express.hpp>
-
-#include <kapi/devices.hpp>
-#include <kapi/memory.hpp>
-
-#include <kstd/print.hpp>
-#include <kstd/units.hpp>
-#include <kstd/vector.hpp>
-
-#include <cstddef>
-#include <cstdint>
-
-namespace kernel::devices
-{
-
- namespace
- {
-
- struct pcie_config_space_header
- {
- std::uint16_t vendor_id{};
- std::uint16_t device_id{};
- std::uint16_t command{};
- std::uint16_t status{};
- std::uint8_t revision_id{};
- std::uint8_t programming_interface{};
- std::uint8_t subclass{};
- std::uint8_t class_code{};
- std::uint8_t cache_line_size{};
- std::uint8_t latency_timer{};
- std::uint8_t header_type{};
- std::uint8_t built_in_self_test{};
- };
-
- constexpr auto static invalid_vendor_id = 0xffff;
-
- constexpr auto static devices_per_bus = 32;
- constexpr auto static functions_per_device = 8;
-
- constexpr auto static bus_address_shift = 20;
- constexpr auto static device_address_shift = 15;
- constexpr auto static function_address_shift = 12;
-
- auto enumerate(kapi::devices::pcie_segment_descriptor const & segment) -> void
- {
- auto segment_base = kapi::memory::physical_address{segment.base_address};
-
- for (auto bus = static_cast<std::size_t>(segment.first_bus); bus < segment.first_bus + segment.bus_count; ++bus)
- {
- auto bus_pages = devices_per_bus * functions_per_device;
- auto bus_address = kapi::memory::physical_address{segment_base + (bus << bus_address_shift)};
- auto bus_window = kapi::memory::mmio_window::map(bus_address, bus_pages * kapi::memory::page::size,
- kapi::memory::mmio_window::access::read_only);
-
- if (!bus_window)
- {
- kstd::println("[OS:DEV] Failed to map MMIO window for bus {}", bus);
- continue;
- }
-
- for (auto device = 0; device < devices_per_bus; ++device)
- {
- auto device_vendor_id = bus_window->read<std::uint16_t>(
- kstd::bytes((device << device_address_shift) + offsetof(pcie_config_space_header, vendor_id)));
- if (device_vendor_id == invalid_vendor_id)
- {
- continue;
- }
-
- auto device_header_type = bus_window->read<std::uint8_t>(
- kstd::bytes((device << device_address_shift) + offsetof(pcie_config_space_header, header_type)));
- auto function_count = (device_header_type & 0x80) ? functions_per_device : 1;
-
- for (auto function = 0; function < function_count; ++function)
- {
- auto function_vendor_id = bus_window->read<std::uint16_t>(
- kstd::bytes((device << device_address_shift) + (function << function_address_shift) +
- offsetof(pcie_config_space_header, vendor_id)));
- if (function_vendor_id == invalid_vendor_id)
- {
- continue;
- }
-
- auto function_device_id = bus_window->read<std::uint16_t>(
- kstd::bytes((device << device_address_shift) + (function << function_address_shift) +
- offsetof(pcie_config_space_header, device_id)));
- kstd::println("[OS:DEV] Detected PCI Express device: {:04x}:{:02x}:{:02x}.{:1} [{:04x}:{:04x}]",
- segment.group_number, bus, device, function, function_vendor_id, function_device_id);
- }
- }
- }
- }
-
- } // namespace
-
- auto init_pci_express_devices() -> void
- {
- auto segments = kapi::devices::get_pci_express_segment_descriptors();
-
- if (segments.empty())
- {
- return;
- }
-
- kstd::println("[OS:DEV] Detected number of PCI Express segments: {}", segments.size());
- for (auto & segment : segments)
- {
- enumerate(segment);
- }
- }
-
-} // namespace kernel::devices \ No newline at end of file
diff --git a/kernel/kernel/devices/pci_express.hpp b/kernel/kernel/devices/pci_express.hpp
deleted file mode 100644
index 596791b1..00000000
--- a/kernel/kernel/devices/pci_express.hpp
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef TEACHOS_KERNEL_DEVICES_PCI_EXPRESS_HPP
-#define TEACHOS_KERNEL_DEVICES_PCI_EXPRESS_HPP
-
-namespace kernel::devices
-{
-
- //! Initialize all PCI Express devices in the system.
- //!
- //! This function scans and initializes all PCI Express devices present in the system.
- auto init_pci_express_devices() -> void;
-
-} // namespace kernel::devices
-
-#endif \ No newline at end of file
diff --git a/kernel/kernel/main.cpp b/kernel/kernel/main.cpp
index 47db0537..ea55451f 100644
--- a/kernel/kernel/main.cpp
+++ b/kernel/kernel/main.cpp
@@ -1,5 +1,5 @@
#include <kernel/devices/init.hpp>
-#include <kernel/devices/pci_express.hpp>
+#include <kernel/devices/pci/init.hpp>
#include <kernel/devices/storage.hpp>
#include <kernel/drivers/init.hpp>
#include <kernel/memory.hpp>
@@ -297,7 +297,7 @@ auto main() -> int
kapi::interrupts::enabled(true);
kstd::println("[OS] Interrupts enabled.");
- kernel::devices::init_pci_express_devices();
+ kernel::devices::pci::init();
kstd::println("[OS] PCI Express devices initialized.");
auto modules_bus = kernel::devices::storage::init();