aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-11-12 23:57:50 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-11-22 14:20:35 +0100
commit1cd8cd46eb330319d3c615601358a4406ce89de8 (patch)
tree0ae68a5b64a705904a75b68bd29aa03bdc81a0da /kernel
parent27a58a338ad2f61a063df02488fc497405afc492 (diff)
downloadkernel-1cd8cd46eb330319d3c615601358a4406ce89de8.tar.xz
kernel-1cd8cd46eb330319d3c615601358a4406ce89de8.zip
dev/pcie: implement basic enumeration algorithm
Diffstat (limited to 'kernel')
-rw-r--r--kernel/kernel/devices/pci_express.cpp68
1 files changed, 63 insertions, 5 deletions
diff --git a/kernel/kernel/devices/pci_express.cpp b/kernel/kernel/devices/pci_express.cpp
index 2fcb17b5..a4bfb8c0 100644
--- a/kernel/kernel/devices/pci_express.cpp
+++ b/kernel/kernel/devices/pci_express.cpp
@@ -1,12 +1,74 @@
#include <kernel/devices/pci_express.hpp>
#include <kapi/devices.hpp>
+#include <kapi/memory.hpp>
+#include <kstd/memory.hpp>
#include <kstd/print.hpp>
+#include <kstd/vector.hpp>
+
+#include <cstddef>
+#include <cstdint>
namespace kernel::devices
{
+ namespace
+ {
+ 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;
+ } // namespace
+
+ auto enumerate(kapi::devices::pcie_segment_descriptor const & segment) -> void
+ {
+ auto segment_base = kapi::memory::physical_address{segment.base_address};
+ auto segment_bus_count = segment.last_bus - segment.first_bus + 1;
+ auto segment_mmio_pages = segment_bus_count * devices_per_bus * functions_per_device;
+ auto segment_config_region = kapi::memory::allocate_mmio_region(segment_mmio_pages);
+ auto segment_config_space = kapi::memory::map_mmio_region(segment_config_region, segment_base);
+
+ kstd::println("[OS:DEV] Enumerating PCI Express segment [{:#018x} - {:#018x}]", segment_config_region.first.raw(),
+ (segment_config_region.first + segment_mmio_pages * kapi::memory::page_size).raw());
+
+ for (auto bus = static_cast<std::size_t>(segment.first_bus); bus <= segment.last_bus; ++bus)
+ {
+ auto bus_base = segment_config_space + (bus << bus_address_shift);
+
+ for (auto device = 0; device < devices_per_bus; ++device)
+ {
+ auto device_base = bus_base + (device << device_address_shift);
+ auto device_config = reinterpret_pointer_cast<std::uint16_t>(device_base);
+
+ if (*device_config == invalid_vendor_id)
+ {
+ continue;
+ }
+
+ for (auto function = 0; function < functions_per_device; ++function)
+ {
+ auto function_base = device_base + (function << function_address_shift);
+ auto function_config = reinterpret_pointer_cast<std::uint16_t>(function_base);
+
+ auto vendor_id = *function_config;
+ if (vendor_id == invalid_vendor_id)
+ {
+ continue;
+ }
+
+ auto device_id = *(function_config + 1);
+ kstd::println("[OS:DEV] Detected PCI Express device: {:02x}:{:02x}.{:1} [{:04x}:{:04x}]", bus, device,
+ function, vendor_id, device_id);
+ }
+ }
+ }
+ }
+
auto init_pci_express_devices() -> void
{
auto segments = kapi::devices::get_pci_express_segment_descriptors();
@@ -19,11 +81,7 @@ namespace kernel::devices
kstd::println("[OS:DEV] Detected number of PCI Express segments: {}", segments.size());
for (auto & segment : segments)
{
- kstd::println("[OS:DEV] PCI Express segment: \n"
- "\tECAM base address = {:#018x}\n"
- "\tFirst bus = {:#04x}\n"
- "\tLast bus = {:#04x}",
- segment.base_address, segment.first_bus, segment.last_bus);
+ enumerate(segment);
}
}