BCAL 驱动框架设计文档
注意:当前处于草稿状态。欢迎通过 issue 反馈建议或问题。
概述
BCAL (Board Control Abstraction Layer) 驱动框架是一个用户态驱动管理框架,采用工厂模式和动态加载机制,为 BMC 系统提供灵活、可扩展的硬件抽象层。
接口设计
目标
本次定义的硬件抽象层,其目标是总结openUBMC对BMC SOC硬件的依赖,提供一层抽象,屏蔽BMC芯片差异。
达成的效果:用户(这里指驱动开发者,后文同)在实现完硬件抽象层制定的API后,能够把openUBMC的核心服务拉起。
核心服务指:
- 框架
- 设备驱动框架:hwproxy/hwdiscovery/devmon/component_drivers,基本的资源树接口(Chip, Bus)
- 应用管理框架:libmcpp/libmc4lua
- 基础服务子系统
- 其他闭源组件
语言与命名规范
- 语言: 采用 C++ 作为接口语言
- 类命名: 统一使用
Xyz作为驱动接口类名,采用大驼峰 - namespace: 驱动
Xyz的 namespace 为DRIVER_XYZ - 语义: 每个
Xyz实例对应的硬件形态在头文件中注明,如一个Adc实例代表一个 ADC 芯片,包含多个 ADC 通道
IDriver
IDriver是 BCAL 的基类,所有驱动都继承自 IDriver。
class IDriver {
public:
virtual ~IDriver() = 0; // 纯虚析构,必须实现
virtual void init(void *args, uint32_t size) = 0; // 初始化接口
virtual void lock(void) = 0; // 加锁
virtual void unlock(void) = 0; // 解锁
virtual string_t get_version(void) = 0; // 获取版本
virtual string_t get_driver_name(void) const = 0; // 获取驱动名称
virtual int32_t control(uint32_t cmd, void *args_in, void *args_out) = 0;
};
BCAL 仅以头文件的形式提供接口,当驱动开发者进行实现时,需要对具体的驱动接口进行继承。
classDiagram
class IDriver {
<<interface>>
+driver_name: char*
+init(args: void*) void
+config(config: void*) void
+lock() void
+unlock() void
}
class Adc {
<<interface>>
+read(index: uint32_t) uint32_t
}
class Bt {
<<interface>>
+read(index: uint32_t, len: uint32_t, timeout: uint32_t) string_t
+write(index: uint32_t, val: string_view&, timeout: uint32_t) void
}
class AdcImpl {
+read() uint32_t
}
class BtImpl {
+read(index: uint32_t, len: uint32_t, timeout: uint32_t) string_t
+write(index: uint32_t, val: string_view&, timeout: uint32_t) void
}
IDriver <|.. Adc : 继承
IDriver <|.. Bt : 继承
Adc <|.. AdcImpl : 实现
Bt <|.. BtImpl : 实现
note for Bt "BCAL 规定的驱动接口"
note for BtImpl "驱动开发者进行实现"
style IDriver fill:#ffffff
style Adc fill:#fff3e0
style Bt fill:#fff3e0
style AdcImpl fill:#c9f2c7
style BtImpl fill:#c9f2c7
接口设计须知
-
每个驱动类都应明确其对应的物理设备的形态,在注释中写明。
-
驱动接口必须是线程安全的。
lock() 和 unlock() 接口仅用于保证对资源的独占访问
-
允许接口抛出异常。调用者在访问 bcal 接口时,需要使用 try-catch 进行异常处理
驱动管理设计
整体架构图
- 驱动工厂: 负责驱动的加载和生命周期管理
- 驱动模块: 每个驱动是一个独立的 so 文件,名称为 lib{driver_name}.so
┌─────────────────────────────────────────────────────────────────────────────┐
│ Application Layer │
│ (业务组件:hwproxy, devmon等) │
└─────────────────────────────────┬───────────────────────────────────────────┘
│
│ get_driver() / destroy_driver()
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ IDriverFactory (驱动工厂) │
│ ┌───────────────────────────────────────────────────────────────────────┐ │
│ │ driver_factory │ │
│ │ ┌───────────────────┐ ┌───────────────────────────────────────┐ │ │
│ │ │ m_driver_handles │ │ m_load_info │ │ │
│ │ │ (已加载驱动句柄) │ │ (驱动加载状态信息) │ │ │
│ │ └───────────────────┘ └───────────────────────────────────────┘ │ │
│ └───────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────┬───────────────────────────────────────────┘
│
│ dlopen() / dlsym()
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ Driver Modules (.so) │
│ ┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐ │
│ │ libgpio.so │ │ libuart.so │ │ libi2c.so │ │
│ │ ┌─────────────┐ │ │ ┌─────────────┐ │ │ ┌─────────────┐ │ │
│ │ │ create_func │ │ │ │ create_func │ │ │ │ create_func │ │ │
│ │ │ destroy_func│ │ │ │ destroy_func│ │ │ │ destroy_func│ │ │
│ │ │ GpioImpl │ │ │ │ UartImpl │ │ │ │ I2cImpl │ │ │
│ │ └─────────────┘ │ │ └─────────────┘ │ │ └─────────────┘ │ │
│ └───────────────────┘ └───────────────────┘ └───────────────────┘ │
└─────────────────────────────────┬───────────────────────────────────────────┘
│
│ system calls / hardware access
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ Hardware Layer │
│ (GPIO、UART、I2C、SPI等硬件设备) │
└─────────────────────────────────────────────────────────────────────────────┘
驱动生命周期
┌──────────┐ ┌──────────┐ ┌──────────┐
│ create │───▶│ init │───▶│ destroy │
└──────────┘ └──────────┘ └──────────┘
│ │ │
▼ ▼ ▼
创建实例 初始化设备 销毁实例
(必须) (必须) (必须)
模块加载流程
load_driver(driver_name)
│
▼
┌─────────────────────────┐
│ 检查是否已加载 │
│ m_driver_handles │
└────────────┬────────────┘
│ 未加载
▼
┌─────────────────────────┐
│ dlopen() 加载 .so │
│ lib<driver_name>.so │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ dlsym() 查找符号 │
│ - create_driver │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ 记录加载状态 │
│ m_load_info │
└─────────────────────────┘
默认驱动模块路径: /opt/bmc/bcal
可通过 set_driver_path() 修改:
factory.set_driver_path("/custom/path");
驱动实例创建流程
get_driver(driver_name, index)
│
▼
┌─────────────────────────┐
│ 加载驱动模块 │
│ load_driver(name) │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ dlsym() 获取 create │
│ create_driver(index) │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ 返回驱动实例 │
│ IDriver* │
└─────────────────────────┘
IDriverFactory 接口
class IDriverFactory {
public:
virtual void set_driver_path(const string_t& driver_path) = 0;
virtual IDriver* get_driver(const string_t& driver_name, int32_t index) = 0;
virtual std::vector<string_t> get_driver_list(void) = 0;
virtual std::vector<driver_load_info> get_driver_load_info(void) = 0;
virtual void destroy_driver(IDriver* driver) = 0;
static IDriverFactory& get_instance(); // 单例获取
};
驱动模块必须导出的符号
| 符号名 | 类型 | 说明 |
|---|---|---|
create_driver |
函数 | 创建驱动实例,签名: IDriver* (*)(int32_t index) |
destroy_driver |
函数 | 销毁驱动实例,签名: void (*)(IDriver* driver) |
驱动模块开发指南
模块结构
一个完整的驱动模块需要包含以下组件:
// 1. 驱动实现类
class GpioImpl : public Gpio {
public:
explicit GpioImpl(int32_t gpio_num);
~GpioImpl() override; // 析构函数
void init(void *args, uint32_t size) override;
void lock(void) override;
void unlock(void) override;
string_t get_version(void) override;
string_t get_driver_name(void) const override;
uint8_t read(void) override;
int32_t write(uint8_t gpio_level) override;
};
// 2. 模块导出函数
extern "C" {
/**
* @brief 创建驱动实例
* @param index 设备索引
* @return 驱动实例指针
*/
IDriver* create_driver(int32_t index) {
return new GpioImpl(index);
}
/**
* @brief 销毁驱动实例
* @param driver 驱动实例指针
*/
void destroy_driver(IDriver* driver) {
delete driver;
}
}
驱动使用示例
#include "bcal/gpio.h"
#include "bcal/driver.h"
using namespace DRIVER_GPIO;
using namespace bcal;
int main() {
// 1. 获取工厂实例(单例)
IDriverFactory& factory = IDriverFactory::get_instance();
// 2. 设置驱动路径(可选,默认 /opt/bmc/bcal)
factory.set_driver_path("/opt/bmc/bcal");
// 3. 获取驱动实例(自动加载 libgpio.so)
IDriver* driver = factory.get_driver("gpio", 192);
if (!driver) {
return -1;
}
// 4. 类型转换
Gpio* gpio = dynamic_cast<Gpio*>(driver);
if (!gpio) {
factory.destroy_driver(driver);
return -1;
}
// 5. 初始化设备
GpioConfig config;
config.direction = 1; // 输出模式
gpio->lock();
gpio->init(&config, sizeof(config));
// 6. 使用设备
gpio->write(1); // 设置高电平
uint8_t value = gpio->read();
gpio->unlock();
// 7. 释放驱动实例
factory.destroy_driver(driver);
return 0;
}
支持的驱动列表
| 驱动名称 | 备注 |
|---|---|
| adc | 模数转换 |
| bt | 基础传输 |
| canbus | CAN总线 |
| edma | 增强DMA |
| efuse | 电子保险丝 |
| gpio | 通用输入输出 |
| h2b | Host-to-BMC 共享内存 |
| i2c | I2C总线 |
| ipmb | IPMB协议 |
| jtag | JTAG调试 |
| kcs | KCS接口 |
| localbus | 本地总线 |
| lpc_snoop | LPC snoop(POST 码) |
| mctp | PcieVdm |
| mdio | MDIO管理接口 |
| mem | 内存映射 |
| mmc | MMC/SD卡 |
| peci | PECI接口 |
| pwm | 脉冲宽度调制 |
| sol | 串口-over-LAN |
| spi | SPI总线 |
| trng | 真随机数发生器 |
| uart | 串口 |
| usb_drd | USB DRD |
| usb_driver | USB驱动 |
| wdt | 看门狗 |
编译
BCAL 使用 meson 构建系统:
meson setup build
ninja -C build
也可使用 Doxygen 生成 HTML 文档:
doxygen Doxyfile
dfx 工具
参见 dfx
异常处理
bcal 不限定接口实现是否抛异常,业务组件对 bcal 接口进行访问时,应当使用 try-catch ,避免程序崩溃。