diff options
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | kernel/kernel/bus/pci.hpp | 16 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/endpoint.cpp | 37 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/endpoint.hpp | 39 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/endpoint.tests.cpp | 269 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/function_access.hpp | 5 |
6 files changed, 365 insertions, 2 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 9feab671..92c731fe 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -44,6 +44,7 @@ target_sources("kernel_lib" PRIVATE "kernel/devices/block_device_utils.cpp" "kernel/devices/init.cpp" "kernel/devices/pci/ecam_segment.cpp" + "kernel/devices/pci/endpoint.cpp" "kernel/devices/pci/function_access.cpp" "kernel/devices/pci/init.cpp" "kernel/devices/pci/scan.cpp" diff --git a/kernel/kernel/bus/pci.hpp b/kernel/kernel/bus/pci.hpp index 782bd8b7..64697c7a 100644 --- a/kernel/kernel/bus/pci.hpp +++ b/kernel/kernel/bus/pci.hpp @@ -5,6 +5,7 @@ #include <kapi/capabilities/facet_id.hpp> #include <kapi/devices.hpp> +#include <kapi/memory.hpp> #include <kstd/result.hpp> @@ -139,6 +140,21 @@ namespace kernel::bus [[nodiscard]] virtual auto supported_ids() const noexcept -> std::span<pci_id const> = 0; }; + struct pci_config_access + { + //! The unique ID of this facet. + constexpr auto static id = kapi::capabilities::facet_id{"cfg.dev.pci"}; + + //! Virtual destructor to allow for safe deletion of derived objects through base pointers. + virtual ~pci_config_access() = default; + + //! Map the PCI configuration space for access. + //! + //! @return A pointer to the mapped PCI configuration space. + [[nodiscard]] virtual auto map_config(kapi::memory::mmio_window::access mode) const + -> kstd::result<kapi::memory::mmio_window> = 0; + }; + //! The base class for all PCI protocols. struct pci_protocol : kapi::devices::bus_protocol { diff --git a/kernel/kernel/devices/pci/endpoint.cpp b/kernel/kernel/devices/pci/endpoint.cpp new file mode 100644 index 00000000..8ca536f2 --- /dev/null +++ b/kernel/kernel/devices/pci/endpoint.cpp @@ -0,0 +1,37 @@ +#include <kernel/devices/pci/endpoint.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/function_info.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/format.hpp> +#include <kstd/memory.hpp> + +#include <utility> + +namespace kernel::devices::pci +{ + + endpoint::endpoint(function_info info, ecam_segment segment) + : device{kstd::format("{}", info.address)} + , m_access{std::move(info), std::move(segment)} + {} + + auto endpoint::query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> + { + if (facet == bus::pci_signature::id) + { + return kstd::make_observer<bus::pci_signature>(&m_access); + } + else if (facet == bus::pci_config_access::id) + { + return kstd::make_observer<bus::pci_config_access>(&m_access); + } + + return device::query_facet(facet); + } + +} // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/endpoint.hpp b/kernel/kernel/devices/pci/endpoint.hpp new file mode 100644 index 00000000..14d8f27b --- /dev/null +++ b/kernel/kernel/devices/pci/endpoint.hpp @@ -0,0 +1,39 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_ENDPOINT_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_ENDPOINT_HPP + +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/function_access.hpp> +#include <kernel/devices/pci/function_info.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/memory.hpp> + +namespace kernel::devices::pci +{ + + //! A PCI endpoint device, representing a single, non-bridge function on a PCI bus. + struct endpoint final : kapi::devices::device + { + //! Construct a new endpoint device using the given function information and ECAM segment. + //! + //! @param info The PCI function information for this endpoint. + //! @param segment The ECAM segment that contains this endpoint. + endpoint(function_info info, ecam_segment segment); + + protected: + //! Query a facet provided by this endpoint. + //! + //! @param facet The facet ID to query. + //! @return An observer pointer to the requested facet, or nullptr if not available. + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> final; + + private: + //! The function access object used to interact with this endpoint's configuration space. + function_access m_access; + }; + +} // namespace kernel::devices::pci + +#endif diff --git a/kernel/kernel/devices/pci/endpoint.tests.cpp b/kernel/kernel/devices/pci/endpoint.tests.cpp new file mode 100644 index 00000000..beb89e9a --- /dev/null +++ b/kernel/kernel/devices/pci/endpoint.tests.cpp @@ -0,0 +1,269 @@ +#include <kernel/devices/pci/endpoint.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/function_address.hpp> +#include <kernel/devices/pci/function_info.hpp> +#include <kernel/devices/pci/registers.hpp> +#include <kernel/test_support/memory.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> +#include <kapi/devices/driver_registry.hpp> +#include <kapi/memory.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/string.hpp> +#include <kstd/test_support/result.hpp> +#include <kstd/units.hpp> + +#include <catch2/catch_test_macros.hpp> + +#include <array> +#include <cstdint> +#include <span> +#include <string_view> +#include <utility> + +// NOLINTBEGIN(readability-magic-numbers) + +namespace +{ + using kernel::bus::pci_claim; + using kernel::bus::pci_config_access; + using kernel::bus::pci_id; + using kernel::bus::pci_signature; + using kernel::devices::pci::ecam_segment; + using kernel::devices::pci::endpoint; + using kernel::devices::pci::function_address; + using kernel::devices::pci::function_info; + using kernel::devices::pci::function_offset; + + constexpr auto ecam_test_base = kapi::memory::physical_address{}; + constexpr auto group = std::uint16_t{3}; + constexpr auto interrupt_line = kstd::bytes{0x3c}; + + //! A segment covering every bus of the test MMIO space. + constexpr auto descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = ecam_test_base, + .group_number = group, + .first_bus = 0, + .bus_count = kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus, + }; + + auto ahci_controller_at(function_address address) -> function_info + { + return function_info{ + .address = address, + .vendor_id = 0x8086, + .device_id = 0x2922, + .revision = 2, + .programming_interface = 1, + .subclass_code = 6, + .class_code = 1, + }; + } + + //! The state a driver keeps for a bound endpoint. + struct driver_state + { + kapi::memory::mmio_window config; + }; + + //! A driver that maps its function's configuration space and keeps the window in its driver state. + struct config_driver final : kapi::devices::driver, pci_claim + { + config_driver() + : m_ids{ + pci_id{.vendor_id = 0x8086, .device_id = 0x2922} + } + {} + + [[nodiscard]] auto probe(kapi::devices::device & device) -> kstd::result<void> override + { + ++probe_calls; + + auto access = device.facet<pci_config_access>(); + if (!access) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + auto window = access->map_config(kapi::memory::mmio_window::access::writable); + if (!window) + { + return kstd::failure(window.error()); + } + + window->write<std::uint8_t>(interrupt_line, 0x0b); + device.set_driver_data(kstd::make_shared<driver_state>(std::move(*window))); + + return kstd::success(); + } + + auto unbind(kapi::devices::device & device) -> void override + { + ++unbind_calls; + device.set_driver_data(nullptr); + } + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "PCI Config Test Driver"; + } + + [[nodiscard]] auto supported_ids() const noexcept -> std::span<pci_id const> override + { + return m_ids; + } + + unsigned probe_calls{}; + unsigned unbind_calls{}; + + protected: + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> override + { + if (facet == pci_claim::id) + { + return kstd::make_observer<pci_claim>(this); + } + + return driver::query_facet(facet); + } + + private: + std::array<pci_id, 1> m_ids{}; + }; + + struct test_protocol final : kernel::bus::pci_protocol + { + auto enumerate(kapi::devices::bus &) -> void override {} + } constinit protocol_instance; + + struct test_bus final : kapi::devices::bus + { + explicit test_bus(kstd::string name) + : bus{name, protocol_instance} + {} + }; +} // namespace + +SCENARIO("An endpoint is named after its function's address", "[devices][pci][endpoint]") +{ + GIVEN("an endpoint in a non-zero segment group") + { + auto const device = + kstd::make_shared<endpoint>(ahci_controller_at(function_address{group, 2, 5, 3}), ecam_segment{descriptor}); + + THEN("its name is the formatted function address") + { + REQUIRE(device->name() == "0003:02:05.3"); + } + + THEN("it has not been attached to a bus yet") + { + REQUIRE(device->state() == kapi::devices::state::uninitialized); + } + } +} + +SCENARIO("An endpoint offers PCI facets and no more", "[devices][pci][endpoint]") +{ + GIVEN("an endpoint") + { + auto const info = ahci_controller_at(function_address{group, 2, 5, 3}); + auto const device = kstd::make_shared<endpoint>(info, ecam_segment{descriptor}); + + THEN("it provides a PCI signature carrying its function's information") + { + auto const signature = device->facet<pci_signature>(); + + REQUIRE(signature); + REQUIRE(signature->info().address == info.address); + REQUIRE(signature->info().vendor_id == info.vendor_id); + REQUIRE(signature->info().device_id == info.device_id); + } + + THEN("it provides configuration space access") + { + REQUIRE(device->has_facet<pci_config_access>()); + } + + THEN("it is a leaf: it is not a bus and does not claim anything") + { + REQUIRE_FALSE(device->has_facet<kapi::devices::bus>()); + REQUIRE_FALSE(device->has_facet<pci_claim>()); + } + } +} + +SCENARIO("An endpoint's configuration space is reachable through its facet", "[devices][pci][endpoint]") +{ + GIVEN("an endpoint whose function has data in its configuration space") + { + constexpr auto marker_offset = kstd::bytes{0x40}; + constexpr auto marker = std::uint16_t{0xa5a5}; + + auto const address = function_address{group, 2, 5, 3}; + + auto const bus_base = ecam_test_base + address.bus * kernel::devices::pci::mmio_window_per_bus; + auto bus_window = kapi::memory::mmio_window::map(bus_base, kernel::devices::pci::mmio_window_per_bus, + kapi::memory::mmio_window::access::writable); + REQUIRE(bus_window); + bus_window->write<std::uint16_t>(function_offset(address.device, address.function) + marker_offset, marker); + + auto const device = kstd::make_shared<endpoint>(ahci_controller_at(address), ecam_segment{descriptor}); + + THEN("a mapping obtained through the facet shows that function's data") + { + auto const access = device->facet<pci_config_access>(); + REQUIRE(access); + + auto const window = kstd::tests::require_ok(access->map_config(kapi::memory::mmio_window::access::read_only)); + + REQUIRE(window.read<std::uint16_t>(marker_offset) == marker); + } + } +} + +SCENARIO("A driver keeps an endpoint's configuration window in its driver state", "[devices][pci][endpoint]") +{ + GIVEN("a bound driver that wrote the Interrupt Line register during probe") + { + auto const address = function_address{group, 1, 2, 3}; + auto const bus = kstd::make_shared<test_bus>("endpoint_config_bus"); + auto const driver = kstd::make_shared<config_driver>(); + auto const device = kstd::make_shared<endpoint>(ahci_controller_at(address), ecam_segment{descriptor}); + + kapi::devices::driver_registry::get().add(driver); + bus->add_child(device); + + auto const bus_base = ecam_test_base + address.bus * kernel::devices::pci::mmio_window_per_bus; + auto const bus_window = kstd::tests::require_ok(kapi::memory::mmio_window::map( + bus_base, kernel::devices::pci::mmio_window_per_bus, kapi::memory::mmio_window::access::read_only)); + auto const register_offset = function_offset(address.device, address.function) + interrupt_line; + + REQUIRE(device->state() == kapi::devices::state::bound); + REQUIRE(driver->probe_calls == 1); + REQUIRE(device->driver_data() != nullptr); + + REQUIRE(bus_window.read<std::uint8_t>(register_offset) == 0x0b); + + WHEN("the endpoint is removed from its bus") + { + REQUIRE(bus->remove_child(*device)); + + THEN("the driver is unbound and its state, with the window, is gone") + { + REQUIRE(driver->unbind_calls == 1); + REQUIRE(device->driver_data() == nullptr); + REQUIRE(device->state() == kapi::devices::state::removed); + + REQUIRE(bus_window.read<std::uint8_t>(register_offset) == 0x0b); + } + } + } +} + +// NOLINTEND(readability-magic-numbers) diff --git a/kernel/kernel/devices/pci/function_access.hpp b/kernel/kernel/devices/pci/function_access.hpp index 25f600fd..685102b3 100644 --- a/kernel/kernel/devices/pci/function_access.hpp +++ b/kernel/kernel/devices/pci/function_access.hpp @@ -13,7 +13,7 @@ namespace kernel::devices::pci { //! An accessor type to identify and access a PCI function. - struct function_access final : bus::pci_signature + struct function_access final : bus::pci_signature, bus::pci_config_access { //! Construct a new function access object for the given PCI function and ECAM segment. //! @@ -34,7 +34,8 @@ namespace kernel::devices::pci //! //! @param mode The access mode (read-only or writable) for the mapping. //! @return The mapped MMIO window on success, an error otherwise. - auto map_config(kapi::memory::mmio_window::access mode) const -> kstd::result<kapi::memory::mmio_window>; + [[nodiscard]] auto map_config(kapi::memory::mmio_window::access mode) const + -> kstd::result<kapi::memory::mmio_window> override; private: //! The identifying information for the PCI function. |
