SimpleKernel 1.17.0
Loading...
Searching...
No Matches
platform_bus.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include "device_node.hpp"
8#include "expected.hpp"
9#include "kernel_fdt.hpp"
10#include "kernel_log.hpp"
11#include "kstd_cstring"
12
15 public:
16 explicit PlatformBus(KernelFdt& fdt) : fdt_(fdt) {}
17
18 [[nodiscard]] static auto GetName() -> const char* { return "platform"; }
19
27 auto Enumerate(DeviceNode* out, size_t max) -> Expected<size_t> {
28 size_t count = 0;
29
30 auto result = fdt_.ForEachDeviceNode(
31 [&out, &count, max](const char* node_name, const char* compatible_data,
32 size_t compatible_len, uint64_t mmio_base,
33 size_t mmio_size, uint32_t irq) -> bool {
34 if (count >= max) return false;
35 if (compatible_data == nullptr || compatible_len == 0) return true;
36
37 auto& node = out[count];
38
39 kstd::strncpy(node.name, node_name, sizeof(node.name) - 1);
40 node.name[sizeof(node.name) - 1] = '\0';
41
42 node.bus_type = BusType::kPlatform;
43 node.type = DeviceType::kPlatform;
44 node.mmio_base = mmio_base;
45 node.mmio_size = mmio_size;
46 node.irq = irq;
47
48 size_t copy_len = compatible_len < sizeof(node.compatible)
49 ? compatible_len
50 : sizeof(node.compatible);
51 if (compatible_len > sizeof(node.compatible)) {
52 klog::Warn("PlatformBus: compatible truncated {}\u2192{} for '{}'",
53 compatible_len, sizeof(node.compatible), node_name);
54 }
55 kstd::memcpy(node.compatible, compatible_data, copy_len);
56 node.compatible_len = copy_len;
57
59 "PlatformBus: found '{}' compatible='{}' mmio={:#x} size={:#x} "
60 "irq={}",
61 node_name, compatible_data, mmio_base, mmio_size, irq);
62
63 ++count;
64 return true;
65 });
66
67 if (!result.has_value()) {
68 return std::unexpected(result.error());
69 }
70 return count;
71 }
72
75 PlatformBus() = delete;
76 ~PlatformBus() = default;
77 PlatformBus(const PlatformBus&) = delete;
79 auto operator=(const PlatformBus&) -> PlatformBus& = delete;
80 auto operator=(PlatformBus&&) -> PlatformBus& = delete;
82
83 private:
85};
FDT(Flattened Device Tree)解析器
auto ForEachDeviceNode(Callback &&callback) const -> Expected< void >
遍历 FDT 中所有"叶设备"节点,自动跳过基础设施节点。
平台总线 — 从扁平设备树(FDT)枚举设备
KernelFdt & fdt_
PlatformBus(KernelFdt &fdt)
~PlatformBus()=default
auto Enumerate(DeviceNode *out, size_t max) -> Expected< size_t >
枚举所有含 compatible 字符串的 FDT 设备节点。
auto operator=(PlatformBus &&) -> PlatformBus &=delete
auto operator=(const PlatformBus &) -> PlatformBus &=delete
PlatformBus(const PlatformBus &)=delete
static auto GetName() -> const char *
PlatformBus(PlatformBus &&)=delete
PlatformBus()=delete
@ kPlatform
平台设备(中断控制器、定时器等)
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto Warn(etl::format_string< Args... > fmt, Args &&... args) -> void
以 WARN 级别记录日志
单个设备的硬件资源描述。