Skip to content

[libcpu][risc-v] Fix PLIC completion order in non-DM mode - #12011

Merged
Rbb666 merged 1 commit into
RT-Thread:masterfrom
boa-z:fix/riscv-plic-non-dm-completion
Nov 11, 2026
Merged

Rbb666 merged 1 commit into
RT-Thread:masterfrom
boa-z:fix/riscv-plic-non-dm-completion

Conversation

@boa-z

@boa-z boa-z commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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

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

In non-DM mode, handler_plic_irq() completes a PLIC interrupt before calling its handler. For level-triggered sources, this can cause duplicate interrupts if the source is still asserted.

Fixes #12010. Related to #11305.

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

  • Run the interrupt handler before completing the claimed interrupt.
  • Temporarily enable a masked source during completion, since the PLIC ignores completion for disabled sources. This follows the Linux SiFive PLIC driver (9c92006b896c).
  • The shared EOI helper also affects the DM path.

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

  • BSP: bsp/qemu-virt64-riscv with its default DM config for build/boot regression. It does not build the non-DM path, so the non-DM tests used a temporary out-of-tree M-mode qemu-system-riscv64 -M virt BSP (not included in this PR).
  • .config: non-DM test used CONFIG_ARCH_RISCV=y, CONFIG_ARCH_RISCV64=y, CONFIG_ARCH_RISCV_M_MODE=y, CONFIG_RT_USING_RISCV_NEW_COMMON=y, with CONFIG_RT_USING_DM and CONFIG_RT_USING_SMP not set. DM regression used the unmodified bsp/qemu-virt64-riscv .config.
  • action: local builds only; no GitHub Actions run was triggered for this branch.

验证结果 (Validation)

  • Non-DM, M-mode, no DM: a 16550 UART THRE level interrupt is dispatched once. A handler that masks its own PLIC source and is later unmasked and re-armed is dispatched again, without a stall.
  • QEMU -d exec tracing confirms the masked-source completion branch executes, not just compiles.
  • DM regression: bsp/qemu-virt64-riscv builds, boots through OpenSBI to RT-Thread, reaches msh, and help over the UART works.
  • Supplementary host-side PLIC gateway model: the original order produces 2 dispatches for one level event, the fixed order 1; without the temporary enable, a self-masking handler stalls after unmask.

实机验证 (Hardware validation, 2026-11-09)

  • Board: ArtInChip D213EC-DEMO-V4-0 (RV64 C906, M-mode, PIC_DISABLE_DM, no SMP), BSP bsp/artinchip/d213-demo128-nand, non-DM common PLIC path.
  • Reproducer: the GE (2D graphics engine) IRQ is level-triggered and its ISR clears GE_STATUS. With the original order, every GE operation was dispatched twice on silicon: the first IRQ read GE_STATUS=0x31, the immediate duplicate read GE_STATUS=0x00. The duplicate overwrote the completion status, so ge_start_and_wait() took its timeout path (256-register dump + engine reset) even though the operation had completed; one ge_test command took about 1.8 s, dominated by the UART dump.
  • A/B on the same board and image:
    • Original libcpu order: duplicate dispatch on every GE operation, [aic] ge timeout on every step.
    • This PR's libcpu change only, with the unpatched vendor GE HAL: ge_test 20/20 PASS (fillrect, bitblt, 2x scale, alpha blend, 90-degree rotate, pixel-checked), zero ge timeout, 124-125 ms per cycle.
    • This PR plus the BSP's defensive HAL guard (ignores a status-less duplicate should one reappear): full bsp.d213.* utest 15/16 PASS, only the pre-existing pure-BSP FPU context known-failure, and ge_test 20/20 PASS (123-127 ms per cycle).

未覆盖的场景与剩余风险 (Not covered / remaining risks)

  • QEMU's hw/intc/sifive_plic.c does not model gateway re-forwarding of an asserted level source on completion, nor ignoring completion for a disabled source. It cannot A/B the original duplicate dispatch; the runs above validate the non-DM path and the workaround execution, not the duplicate itself.
  • Edge-triggered interrupts were not independently regression-tested; the ordering change applies to both trigger modes.
  • No fence o,o is added around the completion. RT-Thread HWREG32 does not have the ordering semantics of Linux writel(), so ordering with the device write relies on the platform interconnect and is unverified on hardware.
  • RV32, SMP, and the SpacemiT K1 quirk were not run. K1 uses the DM path and masks by priority, so its enable bit stays set.
  • handler_irq() passes the raw hwirq to handlers while install/umask use the framework vector. This is pre-existing and intentionally unchanged.

当前拉取/合并请求的状态 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
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

👋 感谢您对 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

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix/riscv-plic-non-dm-completion
    Enter PR branch fix/riscv-plic-non-dm-completion in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix/riscv-plic-non-dm-completion 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/riscv-plic-non-dm-completion 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!

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: libcpu_riscv

Reviewers: @Yaochenger

Changed Files (Click to expand)
  • libcpu/risc-v/common/interrupt.c
  • libcpu/risc-v/common/pic/pic-sifive-plic.c

📊 Current Review Status (Last Updated: 2026-11-09 23:37 CST)


📝 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.

@github-actions github-actions Bot added Arch: RISC-V BSP related with risc-v libcpu labels Nov 9, 2026
handler_plic_irq() completes a claimed interrupt before dispatching its
handler. If a level-triggered source is still asserted at completion
time, the PLIC gateway forwards the request again, so the same interrupt
can be dispatched twice.

Run the handler first, then complete. Completing after the handler means
a handler may have masked its own source; the PLIC ignores a completion
for an interrupt that is not enabled for the target, so temporarily
enable the source around the completion, as the Linux SiFive PLIC driver
does.

Link: RT-Thread#12010
@boa-z
boa-z force-pushed the fix/riscv-plic-non-dm-completion branch from 9afba5f to 843ac9d Compare November 9, 2026 15:37
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

@boa-z
boa-z marked this pull request as ready for review November 9, 2026 16:08
@boa-z
boa-z marked this pull request as draft November 9, 2026 16:23
@boa-z
boa-z marked this pull request as ready for review November 9, 2026 22:50

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: the fix is sound; no blocking issue found.

Verified:

  • handler_plic_irq() now dispatches the handler before plic_irq_ack_or_eoi(), so an asserted level source cannot be re-dispatched before the handler has run.
  • Completion of a masked source is only ignored when the enable bit is clear; temporarily setting and restoring the bit under enable_lock with IRQs saved matches the PLIC rule and keeps the window interrupt-safe.
  • The DM path uses the same helper (irq_ack for edge before dispatch, irq_eoi for level after), so the ordering change is scoped and consistent.
  • CI: 107 pass / 3 skipping. The non-DM path is not covered by CI, as noted in the PR description.

Non-blocking: the fast path reads this hart's enable register without enable_lock (pic-sifive-plic.c#L187). In DM mode plic_irq_mask() clears enable bits on every CPU in the affinity set, so a concurrent mask targeting this hart between that read and the completion write can make the completion silently ignored — the same failure mode this patch fixes. Taking the lock on both paths (or re-checking after taking it) removes the race at the cost of one irqsave in the common path.

@Rbb666
Rbb666 merged commit 5caa519 into RT-Thread:master Nov 11, 2026
113 checks passed
@boa-z
boa-z deleted the fix/riscv-plic-non-dm-completion branch November 13, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: RISC-V BSP related with risc-v libcpu

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [RISC-V] PLIC completion before ISR dispatch in non-DM mode

3 participants