Skip to content

Commit

Permalink
Merge pull request #1149 from hermit-os/virtio-def
Browse files Browse the repository at this point in the history
feat: add `virtio-def` crate for virtio definitions
  • Loading branch information
mkroening authored Apr 30, 2024
2 parents b1ae447 + 27f0467 commit 643d612
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 167 deletions.
27 changes: 25 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ shell = ["simple-shell"]

[dependencies]
hermit-macro = { path = "hermit-macro" }
virtio-def = { path = "virtio-def" }
ahash = { version = "0.8", default-features = false }
align-address = "0.1"
bit_field = "0.10"
Expand Down Expand Up @@ -154,8 +155,13 @@ llvm-tools = "0.1"
[workspace]
members = [
"hermit-macro",
"virtio-def",
"xtask",
]
exclude = [
"hermit-builtins",
]

[patch.crates-io]
# We need https://github.com/rust-osdev/volatile/pull/61
volatile = { git = "https://github.com/rust-osdev/volatile.git" }
42 changes: 0 additions & 42 deletions src/drivers/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,45 +165,3 @@ pub mod features {
}
}
}

/// A module containing virtios device specfific information.
pub mod device {
/// An enum of the device's status field interpretations.
#[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum Status {
ACKNOWLEDGE = 1,
DRIVER = 2,
DRIVER_OK = 4,
FEATURES_OK = 8,
DEVICE_NEEDS_RESET = 64,
FAILED = 128,
}

impl From<Status> for u8 {
fn from(stat: Status) -> Self {
match stat {
Status::ACKNOWLEDGE => 1,
Status::DRIVER => 2,
Status::DRIVER_OK => 4,
Status::FEATURES_OK => 8,
Status::DEVICE_NEEDS_RESET => 64,
Status::FAILED => 128,
}
}
}

impl From<Status> for u32 {
fn from(stat: Status) -> Self {
match stat {
Status::ACKNOWLEDGE => 1,
Status::DRIVER => 2,
Status::DRIVER_OK => 4,
Status::FEATURES_OK => 8,
Status::DEVICE_NEEDS_RESET => 64,
Status::FAILED => 128,
}
}
}
}
36 changes: 12 additions & 24 deletions src/drivers/virtio/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use core::ptr;
use core::ptr::{read_volatile, write_volatile};
use core::sync::atomic::{fence, Ordering};

use virtio_def::DeviceStatus;

#[cfg(any(feature = "tcp", feature = "udp"))]
use crate::arch::kernel::interrupts::*;
use crate::arch::mm::PhysAddr;
Expand All @@ -15,7 +17,6 @@ use crate::drivers::error::DriverError;
use crate::drivers::net::network_irqhandler;
#[cfg(any(feature = "tcp", feature = "udp"))]
use crate::drivers::net::virtio_net::VirtioNetDriver;
use crate::drivers::virtio::device;
use crate::drivers::virtio::error::VirtioError;

/// Virtio device ID's
Expand Down Expand Up @@ -141,13 +142,13 @@ impl ComCfg {

/// Returns the device status field.
pub fn dev_status(&self) -> u8 {
unsafe { read_volatile(&self.com_cfg.status).try_into().unwrap() }
unsafe { read_volatile(&self.com_cfg.status).bits() }
}

/// Resets the device status field to zero.
pub fn reset_dev(&mut self) {
unsafe {
write_volatile(&mut self.com_cfg.status, 0u32);
write_volatile(&mut self.com_cfg.status, DeviceStatus::empty());
}
}

Expand All @@ -156,7 +157,7 @@ impl ComCfg {
/// A driver MAY use the device again after a proper reset of the device.
pub fn set_failed(&mut self) {
unsafe {
write_volatile(&mut self.com_cfg.status, u32::from(device::Status::FAILED));
write_volatile(&mut self.com_cfg.status, DeviceStatus::FAILED);
}
}

Expand All @@ -165,10 +166,7 @@ impl ComCfg {
pub fn ack_dev(&mut self) {
unsafe {
let status = read_volatile(&self.com_cfg.status);
write_volatile(
&mut self.com_cfg.status,
status | u32::from(device::Status::ACKNOWLEDGE),
);
write_volatile(&mut self.com_cfg.status, status | DeviceStatus::ACKNOWLEDGE);
}
}

Expand All @@ -177,10 +175,7 @@ impl ComCfg {
pub fn set_drv(&mut self) {
unsafe {
let status = read_volatile(&self.com_cfg.status);
write_volatile(
&mut self.com_cfg.status,
status | u32::from(device::Status::DRIVER),
);
write_volatile(&mut self.com_cfg.status, status | DeviceStatus::DRIVER);
}
}

Expand All @@ -190,10 +185,7 @@ impl ComCfg {
pub fn features_ok(&mut self) {
unsafe {
let status = read_volatile(&self.com_cfg.status);
write_volatile(
&mut self.com_cfg.status,
status | u32::from(device::Status::FEATURES_OK),
);
write_volatile(&mut self.com_cfg.status, status | DeviceStatus::FEATURES_OK);
}
}

Expand All @@ -206,8 +198,7 @@ impl ComCfg {
pub fn check_features(&self) -> bool {
unsafe {
let status = read_volatile(&self.com_cfg.status);
status & u32::from(device::Status::FEATURES_OK)
== u32::from(device::Status::FEATURES_OK)
status.contains(DeviceStatus::FEATURES_OK)
}
}

Expand All @@ -217,10 +208,7 @@ impl ComCfg {
pub fn drv_ok(&mut self) {
unsafe {
let status = read_volatile(&self.com_cfg.status);
write_volatile(
&mut self.com_cfg.status,
status | u32::from(device::Status::DRIVER_OK),
);
write_volatile(&mut self.com_cfg.status, status | DeviceStatus::DRIVER_OK);
}
}

Expand Down Expand Up @@ -440,8 +428,8 @@ pub struct MmioRegisterLayout {
interrupt_ack: u32,
_reserved4: [u32; 2],

status: u32,
_reserved5: [u32; 3],
status: DeviceStatus,
_reserved5: [u8; 15],

queue_desc_low: u32, // non-legacy only
queue_desc_high: u32, // non-legacy only
Expand Down
Loading

0 comments on commit 643d612

Please sign in to comment.