Skip to content

Commit

Permalink
Merge pull request #1244 from hermit-os/pci-isr-status
Browse files Browse the repository at this point in the history
refactor(virtio/pci): migrate `IsrStatusRaw` to `virtio-spec`
  • Loading branch information
mkroening authored Jun 2, 2024
2 parents e33f479 + a34b006 commit de9d80c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 63 deletions.
100 changes: 37 additions & 63 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use core::ptr::NonNull;
use core::sync::atomic::{fence, Ordering};
use core::{mem, ptr};

use virtio_spec::pci::{CommonCfg, CommonCfgVolatileFieldAccess, CommonCfgVolatileWideFieldAccess};
use virtio_spec::pci::{
CommonCfg, CommonCfgVolatileFieldAccess, CommonCfgVolatileWideFieldAccess,
IsrStatus as IsrStatusRaw,
};
use virtio_spec::DeviceStatus;
use volatile::VolatileRef;

Expand Down Expand Up @@ -249,6 +252,28 @@ impl PciCap {

Some(com_cfg_raw)
}

fn map_isr_status(&self) -> Option<VolatileRef<'static, IsrStatusRaw>> {
if self.bar.length < u64::from(self.length + self.offset) {
error!("ISR status config with id {} of device {:x}, does not fit into memory specified by bar {:x}!",
self.id,
self.origin.dev_id,
self.bar.index
);
return None;
}

let virt_addr_raw: VirtMemAddr = self.bar.mem_addr + self.offset;
let ptr = NonNull::new(ptr::with_exposed_provenance_mut::<IsrStatusRaw>(
virt_addr_raw.into(),
))
.unwrap();

// Create mutable reference to the PCI structure in the devices memory area
let isr_stat_raw = unsafe { VolatileRef::new(ptr) };

Some(isr_stat_raw)
}
}

/// Virtio's PCI capabilities structure.
Expand Down Expand Up @@ -794,89 +819,38 @@ impl NotifCtrl {
pub struct IsrStatus {
/// References the raw structure in PCI memory space. Is static as
/// long as the device is present, which is mandatory in order to let this code work.
isr_stat: &'static mut IsrStatusRaw,
isr_stat: VolatileRef<'static, IsrStatusRaw>,
/// Preferences of the device for this config. From 1 (highest) to 2^7-1 (lowest)
rank: u8,
}

impl IsrStatus {
fn new(raw: &'static mut IsrStatusRaw, rank: u8) -> Self {
fn new(raw: VolatileRef<'static, IsrStatusRaw>, rank: u8) -> Self {
IsrStatus {
isr_stat: raw,
rank,
}
}

pub fn is_interrupt(&self) -> bool {
self.isr_stat.flags & 1 << 0 == 1
self.isr_stat
.as_ptr()
.read()
.contains(IsrStatusRaw::QUEUE_INTERRUPT)
}

pub fn is_cfg_change(&self) -> bool {
self.isr_stat.flags & 1 << 1 == 1 << 1
self.isr_stat
.as_ptr()
.read()
.contains(IsrStatusRaw::DEVICE_CONFIGURATION_INTERRUPT)
}

pub fn acknowledge(&mut self) {
// nothing to do
}
}

/// ISR status structure of Virtio PCI devices.
/// See Virtio specification v1.1. - 4.1.4.5
///
/// Contains a single byte, containing the interrupt numbers used
/// for handling interrupts.
/// The 8-bit field is read as an bitmap and allows to distinguish between
/// interrupts triggered by changes in the configuration and interrupts
/// triggered by events of a virtqueue.
///
/// Bitmap layout (from least to most significant bit in the byte):
///
/// 0 : Queue interrupt
///
/// 1 : Device configuration interrupt
///
/// 2 - 31 : Reserved
#[repr(C)]
struct IsrStatusRaw {
flags: u8,
}

impl IsrStatusRaw {
/// Returns a mutable reference to the ISR status capability structure indicated by the
/// [PciCap] struct. Reference has a static lifetime as the structure is controlled by the
/// device and will not be moved.
fn map(cap: &PciCap) -> Option<&'static mut IsrStatusRaw> {
if cap.bar.length < u64::from(cap.length + cap.offset) {
error!("ISR status config with id {} of device {:x}, does not fit into memory specified by bar {:x}!",
cap.id,
cap.origin.dev_id,
cap.bar.index
);
return None;
}

let virt_addr_raw: VirtMemAddr = cap.bar.mem_addr + cap.offset;

// Create mutable reference to the PCI structure in the devices memory area
let isr_stat_raw: &mut IsrStatusRaw =
unsafe { &mut *(ptr::with_exposed_provenance_mut(virt_addr_raw.into())) };

Some(isr_stat_raw)
}

// returns true if second bit, from left is 1.
// read DOES reset flag
fn cfg_event() -> bool {
unimplemented!();
}

// returns true if first bit, from left is 1.
// read DOES reset flag
fn vqueue_event() -> bool {
unimplemented!();
}
}

/// PCI configuration access structure of Virtio PCI devices.
/// See Virtio specification v1.1. - 4.1.4.8
///
Expand Down Expand Up @@ -1252,7 +1226,7 @@ pub(crate) fn map_caps(device: &PciDevice<PciConfigRegion>) -> Result<UniCapsCol
pci_cap.id, device_id
),
},
CfgType::VIRTIO_PCI_CAP_ISR_CFG => match IsrStatusRaw::map(&pci_cap) {
CfgType::VIRTIO_PCI_CAP_ISR_CFG => match pci_cap.map_isr_status() {
Some(isr_stat) => caps.add_cfg_isr(IsrStatus::new(isr_stat, pci_cap.id)),
None => error!(
"ISR status config capability with id {}, of device {:x} could not be used!",
Expand Down
11 changes: 11 additions & 0 deletions virtio-spec/src/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,14 @@ impl_wide_field_access! {
queue_device: queue_device_low, queue_device_high;
}
}

virtio_bitflags! {
/// ISR Status
pub struct IsrStatus: u8 {
/// Queue Interrupt
const QUEUE_INTERRUPT = 1 << 0;

/// Device Configuration Interrupt
const DEVICE_CONFIGURATION_INTERRUPT = 1 << 1;
}
}

0 comments on commit de9d80c

Please sign in to comment.