Skip to content

feat(usb): xHCI host controller bring-up (polled) - #737

Merged
ytakano merged 7 commits into
tier4:mainfrom
nokosaaan:usb-xhci/2-controller
Oct 20, 2026
Merged

ytakano merged 7 commits into
tier4:mainfrom
nokosaaan:usb-xhci/2-controller

Conversation

@nokosaaan

@nokosaaan nokosaaan commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Add a polled xHCI host controller driver that detects xHCI controllers and brings them up. This PR covers controller initialization only; device enumeration and the USB serial console come in follow-up PRs.

What this PR does:

  • Detection: PCIeInfo::attach() routes Serial Bus Controllers with sub-class 0x03 / prog-if 0x30 to pcie::usb::attach(), before vendor-based matching, so controllers are handled regardless of silicon vendor.
  • Bring-up (xHCI spec §4.2):
    • waits for CNR, halts the controller if it is running, and issues HCRST;
    • re-enables PCI Bus Master (it may be cleared by HCRST or at ExitBootServices) and checks that it stuck;
    • programs MaxSlotsEn, DCBAAP, CRCR and Interrupter 0 (ERSTSZ / ERSTBA / ERDP);
    • allocates scratchpad buffers and stores their array in DCBAA[0];
    • sets Run/Stop and waits for HCHalted to clear.
  • Smoke test: submits a No-Op command and waits for its Command Completion event, which confirms that the command and event rings work end to end.
  • Build: adds the xhci feature to awkernel_drivers and enables it in the kernel's x86 feature set, so CI compiles and lints the driver.

New files:

File Contents
pcie/usb/mod.rs class check, attach entry point, lint gates
pcie/usb/xhci/mod.rs controller core (command / event ring handling, doorbells) and initialization
pcie/usb/xhci/ring.rs producer ring (Command Ring), Event Ring + ERST, DCBAA
pcie/usb/xhci/regs.rs register offsets, TRB types/flags, completion codes

Related links

previous PR: #731

How was this PR tested?

  • scripts/ci.sh passes on my machine.
  • QEMU (-device qemu-xhci with a USB keyboard and usb-net attached) boots to the shell with the following log and no panic:
    xHCI: 0000:00:03.0 xHCI: controller running (polled)
    xHCI: 0000:00:03.0 xHCI: NOOP command OK — command ring is alive
    ...
    All CPUs are ready.
    
  • make qemu-x86_64

Notes for reviewers

nokosaaan and others added 3 commits October 16, 2026 13:02
Add `pcie::mmio::Mmio`, a panic-free accessor for an identity-mapped
memory BAR: every access is bounds- and overflow-checked and returns
`Result` instead of asserting, and `poll32` has an explicit iteration
budget.  Also add `PCIeInfo::enable_bus_master()`.

Both are used by the xHCI driver that follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Detect xHCI controllers (PCI class 0x0C/0x03/0x30) and bring them up:
controller reset, command/event rings, DCBAA, scratchpad buffers and
Run/Stop, then verify the command ring with a No-Op command.

The driver is polled (USBCMD.INTE = 0).  All DMA ring accesses are
volatile with Release/Acquire fences around doorbells and event
consumption, and the module denies panicking constructs, unchecked
arithmetic and lossy casts.  Enabled on x86 through the `xhci` feature.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nokosaaan
nokosaaan requested a review from ytakano October 19, 2026 05:57
@nokosaaan
nokosaaan marked this pull request as draft October 19, 2026 06:00
nokosaaan and others added 2 commits October 19, 2026 15:04
The repository now requires every action to be pinned to a full-length
commit SHA.  Pin the versions currently resolved by the floating tags
(no behavior change):

- actions/checkout@v3            -> v3.7.0 (a37ce912…)
- technote-space/assign-author@v1 -> v1.6.2 (9558557c…)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nokosaaan
nokosaaan marked this pull request as ready for review October 19, 2026 06:10
@ytakano

ytakano commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

@nokosaaan

Why don't you add -device qemu-xhci to QEMU_X86_ARGS of Makefile?

awkernel/Makefile

Lines 160 to 173 in 54d8757

QEMU_X86_ARGS= -drive if=pflash,format=raw,readonly=on,file=${OVMF_PATH}/code.fd
QEMU_X86_ARGS+= -drive if=pflash,format=raw,file=${OVMF_PATH}/vars_qemu.fd
QEMU_X86_ARGS+= -drive format=raw,file=x86_64_uefi.img
QEMU_X86_ARGS+= -machine q35
QEMU_X86_ARGS+= -serial stdio -smp 4 -monitor telnet::5556,server,nowait
QEMU_X86_ARGS+= -m 4G -smp cpus=16
QEMU_X86_ARGS+= -object memory-backend-ram,size=1G,id=m0
QEMU_X86_ARGS+= -object memory-backend-ram,size=1G,id=m1
QEMU_X86_ARGS+= -object memory-backend-ram,size=1G,id=m2
QEMU_X86_ARGS+= -object memory-backend-ram,size=1G,id=m3
QEMU_X86_ARGS+= -numa node,memdev=m0,cpus=0-3,nodeid=0
QEMU_X86_ARGS+= -numa node,memdev=m1,cpus=4-7,nodeid=1
QEMU_X86_ARGS+= -numa node,memdev=m2,cpus=8-11,nodeid=2
QEMU_X86_ARGS+= -numa node,memdev=m3,cpus=12-15,nodeid=3

@nokosaaan

nokosaaan commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@ytakano
Thanks, added -device qemu-xhci,id=xhci to QEMU_X86_ARGS (it also applies to qemu-x86_64-net / -nographic).

@ytakano
ytakano merged commit 94b05f4 into tier4:main Oct 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants