SimpleKernel 1.17.0
Loading...
Searching...
No Matches
virtio_driver.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <etl/io_port.h>
8#include <etl/singleton.h>
9#include <etl/span.h>
10
11#include <array>
12#include <optional>
13#include <variant>
14
15#include "device_manager.hpp"
16#include "device_node.hpp"
17#include "driver_registry.hpp"
18#include "expected.hpp"
19#include "io_buffer.hpp"
20#include "kernel_log.hpp"
21#include "kstd_memory"
24
33 public:
35 enum class DeviceId : uint32_t {
36 kNet = 1,
37 kBlock = 2,
38 kConsole = 3,
39 kEntropy = 4,
40 kGpu = 16,
41 kInput = 18,
42 };
43
44 static constexpr uint32_t kDefaultQueueCount = 1;
45 static constexpr uint32_t kDefaultQueueSize = 128;
46 static constexpr size_t kMinDmaBufferSize = 32768;
47
53 [[nodiscard]] static auto GetEntry() -> const DriverEntry& {
54 static const DriverEntry entry{
55 .name = "virtio",
56 .match_table = etl::span<const MatchEntry>(kMatchTable),
57 .match = etl::delegate<bool(
58 DeviceNode&)>::create<&VirtioDriver::MatchStatic>(),
59 .probe = etl::delegate<Expected<void>(DeviceNode&)>::create<
61 etl::singleton<VirtioDriver>::instance()),
62 .remove = etl::delegate<Expected<void>(DeviceNode&)>::create<
64 etl::singleton<VirtioDriver>::instance()),
65 };
66 return entry;
67 }
68
78 static auto MatchStatic(DeviceNode& node) -> bool;
79
88 [[nodiscard]] auto Probe(DeviceNode& node) -> Expected<void>;
89
95 [[nodiscard]] auto Remove([[maybe_unused]] DeviceNode& node)
97 for (size_t i = 0; i < blk_device_count_; ++i) {
98 blk_devices_[i].reset();
99 dma_buffers_[i].reset();
100 slot_buffers_[i].reset();
101 }
104 return {};
105 }
106
111 [[nodiscard]] auto GetBlkDevice() -> virtio::blk::VirtioBlk<>* {
112 return (blk_device_count_ > 0 && blk_devices_[0].has_value())
113 ? &blk_devices_[0].value()
114 : nullptr;
115 }
116
121 [[nodiscard]] auto GetIrq() const -> uint32_t {
122 return blk_device_count_ > 0 ? irqs_[0] : 0;
123 }
124
130 template <typename CompletionCallback>
131 auto HandleInterrupt(CompletionCallback&& on_complete) -> void {
132 for (size_t i = 0; i < blk_device_count_; ++i) {
133 if (blk_devices_[i].has_value()) {
134 blk_devices_[i].value().HandleInterrupt(
135 static_cast<CompletionCallback&&>(on_complete));
136 }
137 }
138 }
139
142 VirtioDriver() = default;
143 VirtioDriver(const VirtioDriver&) = delete;
145 auto operator=(const VirtioDriver&) -> VirtioDriver& = delete;
146 auto operator=(VirtioDriver&&) -> VirtioDriver& = delete;
147 ~VirtioDriver() = default;
149
150 private:
151 static constexpr MatchEntry kMatchTable[] = {
152 {BusType::kPlatform, "virtio,mmio"},
153 };
154
155 static constexpr size_t kMaxBlkDevices = 4;
156
157 std::array<std::optional<virtio::blk::VirtioBlk<>>, kMaxBlkDevices>
159 std::array<etl::unique_ptr<IoBuffer>, kMaxBlkDevices> dma_buffers_;
160 std::array<etl::unique_ptr<IoBuffer>, kMaxBlkDevices> slot_buffers_;
161 std::array<uint32_t, kMaxBlkDevices> irqs_{};
163
164 // Static adapter pool — one slot per probed blk device (kernel lifetime).
165 std::array<std::optional<virtio::blk::VirtioBlkVfsAdapter>, kMaxBlkDevices>
168};
169
170using VirtioDriverSingleton = etl::singleton<VirtioDriver>;
统一 VirtIO 驱动
size_t blk_adapter_count_
DeviceId
VirtIO 设备类型枚举(来自 VirtIO 1.2 规范)
static constexpr size_t kMinDmaBufferSize
static constexpr uint32_t kDefaultQueueSize
static constexpr uint32_t kDefaultQueueCount
VirtioDriver(const VirtioDriver &)=delete
static constexpr size_t kMaxBlkDevices
static auto GetEntry() -> const DriverEntry &
返回驱动注册入口
VirtioDriver(VirtioDriver &&)=delete
auto HandleInterrupt(CompletionCallback &&on_complete) -> void
处理所有块设备中断
auto GetIrq() const -> uint32_t
获取第一个块设备的 IRQ 号
auto Remove(DeviceNode &node) -> Expected< void >
卸载所有 VirtIO 块设备
std::array< etl::unique_ptr< IoBuffer >, kMaxBlkDevices > dma_buffers_
VirtioDriver()=default
auto Probe(DeviceNode &node) -> Expected< void >
初始化 VirtIO 设备
std::array< etl::unique_ptr< IoBuffer >, kMaxBlkDevices > slot_buffers_
static constexpr MatchEntry kMatchTable[]
std::array< std::optional< virtio::blk::VirtioBlk<> >, kMaxBlkDevices > blk_devices_
auto operator=(VirtioDriver &&) -> VirtioDriver &=delete
size_t blk_device_count_
std::array< std::optional< virtio::blk::VirtioBlkVfsAdapter >, kMaxBlkDevices > blk_adapters_
~VirtioDriver()=default
std::array< uint32_t, kMaxBlkDevices > irqs_
static auto MatchStatic(DeviceNode &node) -> bool
硬件检测:验证 VirtIO magic number
auto GetBlkDevice() -> virtio::blk::VirtioBlk<> *
获取第一个 VirtIO 块设备实例
auto operator=(const VirtioDriver &) -> VirtioDriver &=delete
Virtio 块设备驱动
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
单个设备的硬件资源描述。
类型擦除的驱动条目 — 每个已注册驱动对应一条。
const char * name
驱动静态匹配表中的一条记录
etl::singleton< VirtioDriver > VirtioDriverSingleton