refactor: extract shared hw-virtio crate, eliminate VirtIO protocol duplication

Two VirtIO block and two VirtIO GPU implementations (adapter + driver)
shared 90% of their protocol logic. Extracted the common code into
hw-virtio, parameterized by MmioAccess trait.

New crate: hw-virtio (no_std, zero deps)
  - mmio.rs: MmioAccess trait, DirectMmio, init_legacy_device(), scan
  - queue.rs: VirtioQueue with descriptor chain submit + poll
  - block.rs: block read protocol (3-descriptor chain)
  - gpu.rs: GPU commands (create_2d, attach_backing, set_scanout,
            transfer_to_host, resource_flush)

Adapter thin wrappers (identity-mapped, statics for DMA):
  - virtio_blk.rs: 220 → 53 lines
  - virtio_gpu.rs: 165 → 37 lines

Driver thin wrappers (mapped VAs, phys-info for DMA):
  - drivers/block/virtio: 139 → 70 lines
  - drivers/framebuffer/virtio: 146 → 95 lines

Also deleted input-server/input_device.rs (1-line re-export,
superseded by port-input-device).
This commit is contained in:
2026-10-18 13:29:28 +02:00
parent 8d53fa9ac5
commit 3a0395453b
21 changed files with 483 additions and 562 deletions

View File

@@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
port-block-device = { workspace = true }
hw-virtio = { workspace = true }

View File

@@ -1,87 +1,56 @@
#![no_std]
use hw_virtio::mmio::MmioAccess;
use hw_virtio::queue::{QueueAddrs, VirtioQueue};
use hw_virtio::block::{self, DmaAddrs};
use port_block_device::{BlockDevice, SectorId};
const QUEUE_SIZE: usize = 8;
const SECTOR_SIZE: usize = 512;
const MMIO_BASE: usize = 0x20000;
const VQUEUE_VA: usize = 0x21000;
const REQ_HDR_VA: usize = 0x23000;
const PHYS_INFO_VA: usize = 0x24000;
const PHYSINFO_VA: usize = 0x24000;
const SECTOR_SIZE: usize = 512;
#[repr(C)]
#[derive(Clone, Copy)]
struct VirtqDesc {
addr: u64,
len: u32,
flags: u16,
next: u16,
}
struct UserMmio;
#[repr(C)]
struct VirtqAvail {
flags: u16,
idx: u16,
ring: [u16; QUEUE_SIZE],
used_event: u16,
}
#[repr(C)]
#[derive(Clone, Copy)]
struct VirtqUsedElem {
id: u32,
len: u32,
}
#[repr(C)]
struct VirtqUsed {
flags: u16,
idx: u16,
ring: [VirtqUsedElem; QUEUE_SIZE],
}
#[repr(C)]
struct VirtioBlkReqHeader {
req_type: u32,
reserved: u32,
sector: u64,
}
fn mmio_write(offset: usize, val: u32) {
unsafe { core::ptr::write_volatile((MMIO_BASE + offset) as *mut u32, val) }
}
fn descs() -> *mut VirtqDesc { VQUEUE_VA as *mut VirtqDesc }
fn avail() -> *mut VirtqAvail { (VQUEUE_VA + QUEUE_SIZE * 16) as *mut VirtqAvail }
fn used() -> *mut VirtqUsed { (VQUEUE_VA + 0x1000) as *mut VirtqUsed }
fn req_header() -> *mut VirtioBlkReqHeader { REQ_HDR_VA as *mut VirtioBlkReqHeader }
fn req_status() -> *mut u8 { (REQ_HDR_VA + 16) as *mut u8 }
struct PhysAddrs {
req_hdr: u64,
status: u64,
sector_buf: u64,
}
fn read_phys_addrs() -> PhysAddrs {
unsafe {
let base = PHYS_INFO_VA as *const u64;
PhysAddrs {
req_hdr: core::ptr::read_volatile(base),
status: core::ptr::read_volatile(base.add(1)),
sector_buf: core::ptr::read_volatile(base.add(2)),
}
impl MmioAccess for UserMmio {
fn read(&self, offset: usize) -> u32 {
unsafe { core::ptr::read_volatile((MMIO_BASE + offset) as *const u32) }
}
fn write(&self, offset: usize, val: u32) {
unsafe { core::ptr::write_volatile((MMIO_BASE + offset) as *mut u32, val) }
}
}
pub struct VirtioBlockDevice {
phys: PhysAddrs,
mmio: UserMmio,
queue: VirtioQueue,
dma: DmaAddrs,
}
impl VirtioBlockDevice {
pub fn new() -> Self {
Self { phys: read_phys_addrs() }
let queue = VirtioQueue::new(QueueAddrs::from_base(VQUEUE_VA));
let (hdr_pa, status_pa, data_pa) = unsafe {
let p = PHYSINFO_VA as *const u64;
(
core::ptr::read_volatile(p),
core::ptr::read_volatile(p.add(1)),
core::ptr::read_volatile(p.add(2)),
)
};
let dma = DmaAddrs {
header_pa: hdr_pa,
header_va: REQ_HDR_VA,
status_pa,
status_va: REQ_HDR_VA + 16,
data_pa,
data_va: REQ_HDR_VA + 32,
};
Self { mmio: UserMmio, queue, dma }
}
}
@@ -91,49 +60,13 @@ impl BlockDevice for VirtioBlockDevice {
fn read_sector(&mut self, sector: SectorId, buf: &mut [u8]) -> bool {
if buf.len() < SECTOR_SIZE { return false; }
unsafe {
let hdr = req_header();
(*hdr).req_type = 0;
(*hdr).reserved = 0;
(*hdr).sector = sector.raw() as u64;
core::ptr::write_volatile(req_status(), 0xFF);
let sector_buf_va = (REQ_HDR_VA + 32) as *mut u8;
let d = descs();
(*d.add(0)) = VirtqDesc { addr: self.phys.req_hdr, len: 16, flags: 1, next: 1 };
(*d.add(1)) = VirtqDesc { addr: self.phys.sector_buf, len: SECTOR_SIZE as u32, flags: 2 | 1, next: 2 };
(*d.add(2)) = VirtqDesc { addr: self.phys.status, len: 1, flags: 2, next: 0 };
let a = avail();
let avail_idx = core::ptr::read_volatile(&(*a).idx);
(*a).ring[(avail_idx as usize) % QUEUE_SIZE] = 0;
core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst);
core::ptr::write_volatile(&mut (*a).idx, avail_idx.wrapping_add(1));
core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst);
mmio_write(0x050, 0);
let expected = avail_idx.wrapping_add(1);
let u = used();
let mut timeout = 10_000_000u32;
while core::ptr::read_volatile(&(*u).idx) != expected {
core::hint::spin_loop();
timeout -= 1;
if timeout == 0 { return false; }
}
if core::ptr::read_volatile(req_status()) != 0 { return false; }
core::ptr::copy_nonoverlapping(sector_buf_va, buf.as_mut_ptr(), SECTOR_SIZE);
let mut sector_buf = [0u8; SECTOR_SIZE];
if !block::read_sector(&self.mmio, &self.queue, &self.dma, sector.raw(), &mut sector_buf) {
return false;
}
buf[..SECTOR_SIZE].copy_from_slice(&sector_buf);
true
}
fn write_sector(&mut self, _sector: SectorId, _buf: &[u8]) -> bool {
false
}
fn write_sector(&mut self, _sector: SectorId, _buf: &[u8]) -> bool { false }
}

View File

@@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
port-framebuffer = { workspace = true }
hw-virtio = { workspace = true }

View File

@@ -1,5 +1,8 @@
#![no_std]
use hw_virtio::mmio::MmioAccess;
use hw_virtio::queue::{QueueAddrs, VirtioQueue};
use hw_virtio::gpu::{self, GpuDmaAddrs};
use port_framebuffer::Framebuffer;
const GPU_MMIO_VA: usize = 0x20000;
@@ -8,13 +11,23 @@ const CMDBUF_VA: usize = 0x23000;
const PHYSINFO_VA: usize = 0x24000;
const FB_VA: usize = 0x100000;
const FB_BPP: usize = 4;
const QUEUE_SIZE: usize = 8;
struct GpuMmio;
impl MmioAccess for GpuMmio {
fn read(&self, offset: usize) -> u32 {
unsafe { core::ptr::read_volatile((GPU_MMIO_VA + offset) as *const u32) }
}
fn write(&self, offset: usize, val: u32) {
unsafe { core::ptr::write_volatile((GPU_MMIO_VA + offset) as *mut u32, val) }
}
}
struct PhysInfo {
cmd_pa: u64,
resp_pa: u64,
width: u64,
height: u64,
width: u16,
height: u16,
}
fn read_phys_info() -> PhysInfo {
@@ -23,95 +36,34 @@ fn read_phys_info() -> PhysInfo {
PhysInfo {
cmd_pa: core::ptr::read_volatile(p),
resp_pa: core::ptr::read_volatile(p.add(1)),
width: core::ptr::read_volatile(p.add(3)),
height: core::ptr::read_volatile(p.add(4)),
width: core::ptr::read_volatile(p.add(3)) as u16,
height: core::ptr::read_volatile(p.add(4)) as u16,
}
}
}
fn mmio_write(offset: usize, val: u32) {
unsafe { core::ptr::write_volatile((GPU_MMIO_VA + offset) as *mut u32, val) }
}
pub struct VirtioFramebuffer {
mmio: GpuMmio,
queue: VirtioQueue,
dma: GpuDmaAddrs,
width: u16,
height: u16,
stride: usize,
phys: PhysInfo,
}
impl VirtioFramebuffer {
pub fn new() -> Self {
let phys = read_phys_info();
let width = phys.width as u16;
let height = phys.height as u16;
let stride = width as usize * FB_BPP;
Self { width, height, stride, phys }
}
fn send_gpu_cmd(&self, cmd_len: u32, resp_len: u32) {
unsafe {
let desc0 = VQUEUE_VA as *mut u8;
core::ptr::write_volatile(desc0 as *mut u64, self.phys.cmd_pa);
core::ptr::write_volatile(desc0.add(8) as *mut u32, cmd_len);
core::ptr::write_volatile(desc0.add(12) as *mut u16, 1);
core::ptr::write_volatile(desc0.add(14) as *mut u16, 1);
let desc1 = desc0.add(16);
core::ptr::write_volatile(desc1 as *mut u64, self.phys.resp_pa);
core::ptr::write_volatile(desc1.add(8) as *mut u32, resp_len);
core::ptr::write_volatile(desc1.add(12) as *mut u16, 2);
core::ptr::write_volatile(desc1.add(14) as *mut u16, 0);
let avail = VQUEUE_VA + QUEUE_SIZE * 16;
let avail_idx = core::ptr::read_volatile((avail + 2) as *const u16);
core::ptr::write_volatile(
(avail + 4 + (avail_idx as usize % QUEUE_SIZE) * 2) as *mut u16,
0,
);
core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst);
core::ptr::write_volatile((avail + 2) as *mut u16, avail_idx.wrapping_add(1));
core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst);
mmio_write(0x050, 0);
let used = VQUEUE_VA + 4096;
let expected = avail_idx.wrapping_add(1);
let mut timeout = 10_000_000u32;
while core::ptr::read_volatile((used + 2) as *const u16) != expected {
core::hint::spin_loop();
timeout -= 1;
if timeout == 0 { return; }
}
let stride = phys.width as usize * FB_BPP;
Self {
mmio: GpuMmio,
queue: VirtioQueue::new(QueueAddrs::from_base(VQUEUE_VA)),
dma: GpuDmaAddrs { cmd_pa: phys.cmd_pa, cmd_va: CMDBUF_VA, resp_pa: phys.resp_pa },
width: phys.width,
height: phys.height,
stride,
}
}
fn transfer_and_flush(&self, x: u32, y: u32, w: u32, h: u32) {
unsafe {
let p = CMDBUF_VA as *mut u8;
core::ptr::write_bytes(p, 0, 512);
core::ptr::write_volatile(p as *mut u32, 0x0105);
core::ptr::write_volatile(p.add(24) as *mut u32, x);
core::ptr::write_volatile(p.add(28) as *mut u32, y);
core::ptr::write_volatile(p.add(32) as *mut u32, w);
core::ptr::write_volatile(p.add(36) as *mut u32, h);
core::ptr::write_volatile(p.add(40) as *mut u64, 0);
core::ptr::write_volatile(p.add(48) as *mut u32, 1);
}
self.send_gpu_cmd(56, 24);
unsafe {
let p = CMDBUF_VA as *mut u8;
core::ptr::write_bytes(p, 0, 512);
core::ptr::write_volatile(p as *mut u32, 0x0106);
core::ptr::write_volatile(p.add(24) as *mut u32, x);
core::ptr::write_volatile(p.add(28) as *mut u32, y);
core::ptr::write_volatile(p.add(32) as *mut u32, w);
core::ptr::write_volatile(p.add(36) as *mut u32, h);
core::ptr::write_volatile(p.add(40) as *mut u32, 1);
}
self.send_gpu_cmd(48, 24);
}
}
impl Framebuffer for VirtioFramebuffer {
@@ -125,22 +77,20 @@ impl Framebuffer for VirtioFramebuffer {
}
fn fill_rect(&mut self, x: u16, y: u16, w: u16, h: u16, color: u32) {
for dy in 0..h {
for dx in 0..w {
self.write_pixel(x + dx, y + dy, color);
}
}
for dy in 0..h { for dx in 0..w { self.write_pixel(x + dx, y + dy, color); } }
}
fn clear(&mut self, color: u32) {
let total = self.width as usize * self.height as usize;
let buf = FB_VA as *mut u32;
for i in 0..total {
unsafe { core::ptr::write_volatile(buf.add(i), color); }
unsafe { core::ptr::write_volatile((FB_VA as *mut u32).add(i), color); }
}
}
fn flush(&mut self) {
self.transfer_and_flush(0, 0, self.width as u32, self.height as u32);
let w = self.width as u32;
let h = self.height as u32;
gpu::transfer_to_host(&self.mmio, &self.queue, &self.dma, 1, 0, 0, w, h);
gpu::resource_flush(&self.mmio, &self.queue, &self.dma, 1, 0, 0, w, h);
}
}