Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 4, 2025

拉取/合并请求描述:(PR description)

为什么提交这份PR (why to submit this PR)

Current VirtIO driver only supports legacy v0.95 (version field 0x1). Modern QEMU defaults to VirtIO 1.0+ (version field 0x2) which uses 64-bit feature negotiation, separate queue areas, and enhanced status checks. Without modern support, devices fail to initialize on recent QEMU versions.

你的解决方案是什么 (what is your solution)

Core Changes:

  • Added runtime version detection - driver reads version from MMIO config and adapts behavior
  • Implemented 64-bit feature negotiation via device_features_sel/driver_features_sel registers
  • Updated queue setup to use separate descriptor/driver/device area registers for modern devices
  • Added FEATURES_OK status verification for modern device initialization
  • All device drivers negotiate VIRTIO_F_VERSION_1 feature when version==2

Key Implementation:

// Version-aware feature negotiation
rt_uint64_t virtio_get_features(struct virtio_device *dev) {
    if (dev->version == 1) {
        return dev->mmio_config->device_features;  // Legacy: 32-bit
    } else {
        // Modern: Read both halves via device_features_sel
        dev->mmio_config->device_features_sel = 0;
        rt_uint64_t features = dev->mmio_config->device_features;
        dev->mmio_config->device_features_sel = 1;
        features |= ((rt_uint64_t)dev->mmio_config->device_features) << 32;
        return features;
    }
}

// Queue setup differs by version
if (dev->version == 1) {
    dev->mmio_config->queue_pfn = addr >> 12;  // Legacy: single register
} else {
    // Modern: separate descriptor/driver/device areas
    dev->mmio_config->queue_desc_low = (rt_uint32_t)desc_addr;
    dev->mmio_config->queue_desc_high = (rt_uint32_t)(desc_addr >> 32);
    dev->mmio_config->queue_driver_low = (rt_uint32_t)avail_addr;
    dev->mmio_config->queue_driver_high = (rt_uint32_t)(avail_addr >> 32);
    dev->mmio_config->queue_device_low = (rt_uint32_t)used_addr;
    dev->mmio_config->queue_device_high = (rt_uint32_t)(used_addr >> 32);
    dev->mmio_config->queue_ready = 1;
}

Backward Compatibility:

  • Default config remains legacy (RT_USING_VIRTIO_LEGACY or RT_USING_VIRTIO10)
  • BSP drivers accept both version 1 and 2 devices automatically
  • Single binary supports both versions via runtime detection
  • No API changes, existing code continues to work

Affected Drivers:

  • virtio-blk, virtio-net, virtio-console, virtio-gpu, virtio-input (all updated)

Configuration:

menuconfig → RT-Thread Components → Device Drivers → Using VirtIO device drivers → VirtIO Version
- VirtIO Legacy (v0.95) [default]
- VirtIO Modern (v1.0+) [for QEMU 2.4+]

Documentation:

  • README.md: Simplified usage guide with essential information (English)
  • README_zh.md: Chinese version with language switching links
  • Includes VirtIO v1.0/1.1/1.2 specification links

Code Quality Fixes:

  • Fixed cppcheck AST errors in BSP drivers by rewriting version validation logic
  • Changed from (version != 1 && version != 2) to ((version < 1) || (version > 2))

请提供验证的bsp和config (provide the config and bsp)

  • BSP: bsp/qemu-virt64-riscv, bsp/qemu-virt64-aarch64

  • .config:

# Legacy mode (default, backward compatible)
CONFIG_RT_USING_VIRTIO=y
CONFIG_RT_USING_VIRTIO10=y
CONFIG_RT_USING_VIRTIO_BLK=y
CONFIG_RT_USING_VIRTIO_NET=y

# Modern mode (for testing with QEMU 2.4+)
CONFIG_RT_USING_VIRTIO=y
CONFIG_RT_USING_VIRTIO_MODERN=y
CONFIG_RT_USING_VIRTIO_BLK=y
CONFIG_RT_USING_VIRTIO_NET=y

QEMU Test Commands:

# Legacy mode
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
  -device virtio-blk-device,disable-modern=on,drive=blk0

# Modern mode
qemu-system-riscv64 -M virt -kernel rtthread.bin -nographic \
  -device virtio-blk-device,drive=blk0
  • action: N/A (requires QEMU runtime environment)

Files Changed: 13 files

  • Modified: 12 C/H/md files (core + 5 device drivers + 2 BSP drivers + Kconfig + README)
  • Added: README_zh.md (Chinese documentation)

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查
Original prompt

This section details on the original issue you should resolve

<issue_title>[Feature] virtio能否对modern版本适配?</issue_title>
<issue_description>### Describe problem solved by the proposed feature

现在virtio设备的驱动都是legacy版本,貌似很久没更新过了

Describe your preferred solution

No response

Describe possible alternatives

No response</issue_description>

<agent_instructions>根据issue的情况对virtio进行升级,但也保留选项对当前的代码进行支持,即:

  • 支持到新版本 v1.2.0
  • 加入选项,也能支持当前版本

需要确保qemu的几个bsp在使用老版本,新版本时都没问题。</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@BernardXiong 请给出期望,以及给出最新版本的virtio规范链接,谢谢。

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions
Copy link

github-actions bot commented Dec 4, 2025

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:copilot/update-virtio-to-modern-version
  • 设置PR number为 \ Set the PR number to:11021
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 copilot/update-virtio-to-modern-version 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the copilot/update-virtio-to-modern-version branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

Copilot AI and others added 2 commits December 4, 2025 15:41
- Updated Kconfig to support both legacy and modern versions
- Added version field to virtio_device structure
- Implemented 64-bit feature negotiation for modern virtio
- Updated queue initialization for modern virtio (separate desc/driver/device areas)
- Added FEATURES_OK check for modern virtio
- Updated all device drivers (blk, net, console, gpu, input) to use new APIs
- Updated BSP drivers to accept both version 1 (legacy) and version 2 (modern)

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
…ibility

- Added README.md with usage guide and migration instructions
- Updated Kconfig to keep RT_USING_VIRTIO10 as alias for backward compatibility
- Documented key differences between legacy and modern VirtIO
- Added troubleshooting and contributing guidelines

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Dec 4, 2025

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • components/drivers/virtio/Kconfig
  • components/drivers/virtio/README.md
  • components/drivers/virtio/README_zh.md
  • components/drivers/virtio/virtio.c
  • components/drivers/virtio/virtio.h
  • components/drivers/virtio/virtio_blk.c
  • components/drivers/virtio/virtio_console.c
  • components/drivers/virtio/virtio_gpu.c
  • components/drivers/virtio/virtio_input.c
  • components/drivers/virtio/virtio_net.c

📊 Current Review Status (Last Updated: 2025-12-05 06:45 CST)

  • Maihuanyi Pending Review

📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

Copilot AI and others added 2 commits December 4, 2025 15:53
- Added TESTING.md with test procedures for legacy and modern VirtIO
- Documented QEMU command lines for both modes
- Included device-specific testing instructions
- Added debugging tips and common issues section

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
- Added SPECIFICATIONS.md with official VirtIO spec links
- Documented v1.0, v1.1, and v1.2 specifications
- Included QEMU documentation references
- Added feature bits and status bits reference tables
- Addresses issue comment requesting specification links

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Copilot AI changed the title [WIP] Upgrade virtio to support modern versions and legacy code [virtio] Add modern VirtIO (v1.0-1.2) support with backward compatibility Dec 4, 2025
Copilot AI requested a review from BernardXiong December 4, 2025 16:02
Copilot finished work on behalf of BernardXiong December 4, 2025 16:02
@BernardXiong BernardXiong marked this pull request as ready for review December 4, 2025 16:28
@BernardXiong
Copy link
Member

@copilot

  • 可以移除 components/drivers/virtio/TESTING.md 文件;而规范的系列说明 components/drivers/virtio/SPECIFICATIONS.md 要么进行移除,要么以最简化的方式融入到 components/drivers/virtio/README.md 文件中。
  • components/drivers/virtio/README.md 文件可以同时也加入 中文版本 ,并在 README.md 中加入链接。
  • cppcheck报错了,可以进行修正:
[cpp_check.py 80 INFO] bsp/qemu-virt64-aarch64/drivers/drv_virtio.c:83:12: error: Syntax Error: AST broken, 'if' doesn't have two operands. [internalAstError]
        if (mmio_config->version != 1 && mmio_config->version != 2)
           ^
[cpp_check.py 80 INFO] bsp/qemu-virt64-riscv/driver/drv_virtio.c:89:12: error: Syntax Error: AST broken, 'if' doesn't have two operands. [internalAstError]
        if (mmio_config->version != 1 && mmio_config->version != 2)
           ^

…check

- Removed TESTING.md and SPECIFICATIONS.md as requested
- Simplified README.md with essential information only
- Added Chinese version README_zh.md with language links
- Fixed cppcheck AST error by rewriting version check condition
- Changed from (version != 1 && version != 2) to ((version < 1) || (version > 2))

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 4, 2025

@copilot

  • 可以移除 components/drivers/virtio/TESTING.md 文件;而规范的系列说明 components/drivers/virtio/SPECIFICATIONS.md 要么进行移除,要么以最简化的方式融入到 c...

All requested changes completed in commit a45ccc6:

  1. ✅ Removed components/drivers/virtio/TESTING.md
  2. ✅ Removed components/drivers/virtio/SPECIFICATIONS.md and integrated essential spec references into simplified README.md
  3. ✅ Added Chinese version README_zh.md with language switching links in both files
  4. ✅ Fixed cppcheck AST error by rewriting version check: (mmio_config->version != 1 && mmio_config->version != 2)((mmio_config->version < 1) || (mmio_config->version > 2))

The documentation is now simplified with only essential information, and both English and Chinese versions are available.

Copilot finished work on behalf of BernardXiong December 4, 2025 22:45
@Maihuanyi
Copy link
Contributor

CLA assistant check Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.You have signed the CLA already but the status is still pending? Let us recheck it.

你好,请签一下协议

@GuEe-GUI
Copy link
Contributor

GuEe-GUI commented Dec 5, 2025

virtio DM 已经重构了

@BernardXiong
Copy link
Member

virtio DM 已经重构了

请给出更多的信息,例如PR号,issue或代码的链接,谢谢

@GuEe-GUI
Copy link
Contributor

GuEe-GUI commented Dec 5, 2025

由于 qemu-virt64-riscv 依赖 virtio,而 riscv 的 dm 支持尚不完善,目前 virtio dm 暂没有 pr,主要更新内容如下:

  1. 支持 virtio 1.3 标准,split 和 packed vring 以及 dma 内存管理支持。
  2. 支持 dm bus 管理(自动匹配、自动初始化、热插拔等支持),virtio 传输层抽象管理,同时支持 mmio、pci 传输实现,用户可自定义传输层实现(比如 rpmsg 场景)。
  3. 新增 virtio 设备支持,并通过的 DM 框架进行重构,目前支持设备:net、blk、console、gpu、input、crypto、rng、rpmsg、9p、iommu、scsi、scmi、sound(开发中)。

如果当前 PR 是需要对 virtio 版本进行升级,可以考虑等等。

@BernardXiong
Copy link
Member

由于 qemu-virt64-riscv 依赖 virtio,而 riscv 的 dm 支持尚不完善,目前 virtio dm 暂没有 pr,主要更新内容如下:

  1. 支持 virtio 1.3 标准,split 和 packed vring 以及 dma 内存管理支持。
  2. 支持 dm bus 管理(自动匹配、自动初始化、热插拔等支持),virtio 传输层抽象管理,同时支持 mmio、pci 传输实现,用户可自定义传输层实现(比如 rpmsg 场景)。
  3. 新增 virtio 设备支持,并通过的 DM 框架进行重构,目前支持设备:net、blk、console、gpu、input、crypto、rng、rpmsg、9p、iommu、scsi、scmi、sound(开发中)。

如果当前 PR 是需要对 virtio 版本进行升级,可以考虑等等。

是的,这份PR是由github copilot根据 #11001 的情况来对virtio进行升级(并保留对原virtio的支持)。也可以针对这份PR进行review,并给指令 at copilot 进行调整。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] virtio能否对modern版本适配?

5 participants