Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bsp/qemu-virt64-aarch64/drivers/drv_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ int rt_virtio_devices_init(void)
mmio_config = (struct virtio_mmio_config *)mmio_base;

if (mmio_config->magic != VIRTIO_MAGIC_VALUE ||
mmio_config->version != RT_USING_VIRTIO_VERSION ||
mmio_config->vendor_id != VIRTIO_VENDOR_ID)
{
continue;
}

/* Support both legacy (0x1) and modern (0x2) versions */
if ((mmio_config->version < 1) || (mmio_config->version > 2))
{
continue;
}

init_handler = virtio_device_init_handlers[mmio_config->device_id];

if (init_handler != RT_NULL)
Expand Down
7 changes: 6 additions & 1 deletion bsp/qemu-virt64-riscv/driver/drv_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ int rt_virtio_devices_init(void)
mmio_config = (struct virtio_mmio_config *)mmio_base;

if (mmio_config->magic != VIRTIO_MAGIC_VALUE ||
mmio_config->version != RT_USING_VIRTIO_VERSION ||
mmio_config->vendor_id != VIRTIO_VENDOR_ID)
{
continue;
}

/* Support both legacy (0x1) and modern (0x2) versions */
if ((mmio_config->version < 1) || (mmio_config->version > 2))
{
continue;
}

init_handler = virtio_device_init_handlers[mmio_config->device_id];

if (init_handler != RT_NULL)
Expand Down
20 changes: 18 additions & 2 deletions components/drivers/virtio/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ menuconfig RT_USING_VIRTIO
if RT_USING_VIRTIO
choice RT_USING_VIRTIO_VERSION
prompt "VirtIO Version"
default RT_USING_VIRTIO10
default RT_USING_VIRTIO_LEGACY

config RT_USING_VIRTIO_LEGACY
bool "VirtIO Legacy (v0.95)"
help
Support for VirtIO legacy interface (version 0x1).
This is the older version compatible with most existing QEMU versions.

config RT_USING_VIRTIO10
bool "VirtIO v1.0"
bool "VirtIO Legacy (v1.0 - alias for compatibility)"
help
Alias for RT_USING_VIRTIO_LEGACY for backward compatibility.
Support for VirtIO legacy interface (version 0x1).

config RT_USING_VIRTIO_MODERN
bool "VirtIO Modern (v1.0+)"
help
Support for VirtIO modern interface (version 0x2).
This version supports VirtIO 1.0, 1.1, and 1.2 specifications.
Requires QEMU 2.4+ or compatible hypervisor.
endchoice

config RT_USING_VIRTIO_MMIO_ALIGN
Expand Down
101 changes: 101 additions & 0 deletions components/drivers/virtio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# VirtIO Driver for RT-Thread

[中文](README_zh.md) | English

## Overview

The VirtIO driver provides support for virtual I/O devices in RT-Thread, primarily used in virtualized environments like QEMU.

## Supported Versions

The driver now supports both legacy and modern VirtIO specifications:

- **Legacy VirtIO (v0.95)**: Version field 0x1, compatible with older QEMU versions
- **Modern VirtIO (v1.0+)**: Version field 0x2, supports VirtIO 1.0, 1.1, and 1.2 specifications

## Supported Devices

- **VirtIO Block (virtio-blk)**: Virtual block device
- **VirtIO Network (virtio-net)**: Virtual network interface
- **VirtIO Console (virtio-console)**: Virtual serial console
- **VirtIO GPU (virtio-gpu)**: Virtual graphics device
- **VirtIO Input (virtio-input)**: Virtual input device (keyboard, mouse, tablet)

## Configuration

Use `menuconfig` to configure VirtIO support:

```
RT-Thread Components → Device Drivers → Using VirtIO device drivers
```

### Version Selection

Choose between legacy and modern VirtIO:

```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
```

Options:
- **VirtIO Legacy (v0.95)**: For compatibility with older QEMU versions (default)
- **VirtIO Modern (v1.0+)**: For newer QEMU versions (2.4+) with enhanced features

### Device Selection

Enable individual VirtIO devices:

- `RT_USING_VIRTIO_BLK`: VirtIO block device support
- `RT_USING_VIRTIO_NET`: VirtIO network device support
- `RT_USING_VIRTIO_CONSOLE`: VirtIO console device support
- `RT_USING_VIRTIO_GPU`: VirtIO GPU device support
- `RT_USING_VIRTIO_INPUT`: VirtIO input device support

## Key Differences

### Legacy VirtIO (v0.95)
- 32-bit feature negotiation
- Single queue descriptor area
- Simple status flags
- Guest page size configuration

### Modern VirtIO (v1.0+)
- 64-bit feature negotiation (supports more features)
- Separate descriptor/driver/device queue areas
- Enhanced status flow with FEATURES_OK check
- Better memory alignment and atomicity guarantees

## Migration Guide

### From Legacy to Modern

1. Update your QEMU command line to use modern VirtIO devices (most recent QEMU versions default to modern)
2. Change the VirtIO version in menuconfig:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
→ Select "VirtIO Modern (v1.0+)"
```
3. Rebuild your application
4. The driver will automatically negotiate the VERSION_1 feature with the device

### Backward Compatibility

The driver automatically detects the device version from the MMIO config and adapts its behavior accordingly. Both legacy (version 0x1) and modern (version 0x2) devices are supported in the same build.

## BSP Support

The following BSPs have been updated to support both legacy and modern VirtIO:

- `qemu-virt64-riscv`: QEMU RISC-V 64-bit
- `qemu-virt64-aarch64`: QEMU ARM64 (AArch64)

## Reference Specifications

- [VirtIO Specification v1.2](https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html) (Latest, 2022)
- [VirtIO Specification v1.1](https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html) (2019)
- [VirtIO Specification v1.0](https://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html) (2016)
- [OASIS VirtIO TC](https://www.oasis-open.org/committees/virtio/)

## License

Apache-2.0
101 changes: 101 additions & 0 deletions components/drivers/virtio/README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# RT-Thread VirtIO 驱动

[English](README.md) | 中文

## 概述

VirtIO 驱动为 RT-Thread 提供虚拟 I/O 设备支持,主要用于 QEMU 等虚拟化环境。

## 支持的版本

驱动现已支持传统版和现代版 VirtIO 规范:

- **传统版 VirtIO (v0.95)**:版本字段为 0x1,兼容较旧的 QEMU 版本
- **现代版 VirtIO (v1.0+)**:版本字段为 0x2,支持 VirtIO 1.0、1.1 和 1.2 规范

## 支持的设备

- **VirtIO 块设备 (virtio-blk)**:虚拟块设备
- **VirtIO 网络设备 (virtio-net)**:虚拟网络接口
- **VirtIO 控制台 (virtio-console)**:虚拟串口控制台
- **VirtIO GPU (virtio-gpu)**:虚拟图形设备
- **VirtIO 输入设备 (virtio-input)**:虚拟输入设备(键盘、鼠标、触摸板)

## 配置

使用 `menuconfig` 配置 VirtIO 支持:

```
RT-Thread Components → Device Drivers → Using VirtIO device drivers
```

### 版本选择

在传统版和现代版 VirtIO 之间选择:

```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
```

选项:
- **VirtIO Legacy (v0.95)**:与较旧的 QEMU 版本兼容(默认)
- **VirtIO Modern (v1.0+)**:用于较新的 QEMU 版本(2.4+),具有增强功能

### 设备选择

启用各个 VirtIO 设备:

- `RT_USING_VIRTIO_BLK`:VirtIO 块设备支持
- `RT_USING_VIRTIO_NET`:VirtIO 网络设备支持
- `RT_USING_VIRTIO_CONSOLE`:VirtIO 控制台支持
- `RT_USING_VIRTIO_GPU`:VirtIO GPU 支持
- `RT_USING_VIRTIO_INPUT`:VirtIO 输入设备支持

## 主要区别

### 传统版 VirtIO (v0.95)
- 32 位特性协商
- 单一队列描述符区域
- 简单状态标志
- 客户机页面大小配置

### 现代版 VirtIO (v1.0+)
- 64 位特性协商(支持更多特性)
- 独立的描述符/驱动/设备队列区域
- 增强的状态流程,带 FEATURES_OK 检查
- 更好的内存对齐和原子性保证

## 迁移指南

### 从传统版迁移到现代版

1. 更新 QEMU 命令行以使用现代版 VirtIO 设备(最新的 QEMU 版本默认使用现代版)
2. 在 menuconfig 中更改 VirtIO 版本:
```
RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
→ 选择 "VirtIO Modern (v1.0+)"
```
3. 重新构建应用程序
4. 驱动将自动与设备协商 VERSION_1 特性

### 向后兼容性

驱动从 MMIO 配置中自动检测设备版本,并相应调整其行为。传统版(版本 0x1)和现代版(版本 0x2)设备在同一构建中都受支持。

## BSP 支持

以下 BSP 已更新以支持传统版和现代版 VirtIO:

- `qemu-virt64-riscv`:QEMU RISC-V 64 位
- `qemu-virt64-aarch64`:QEMU ARM64 (AArch64)

## 规范参考

- [VirtIO 规范 v1.2](https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html)(最新,2022)
- [VirtIO 规范 v1.1](https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html)(2019)
- [VirtIO 规范 v1.0](https://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html)(2016)
- [OASIS VirtIO 技术委员会](https://www.oasis-open.org/committees/virtio/)

## 许可证

Apache-2.0
Loading
Loading