Skip to content

Commit

Permalink
refactor(virtio/transport): move notification location calculation in…
Browse files Browse the repository at this point in the history
…to `NotifCfg`

Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Jun 1, 2024
1 parent b12a6c2 commit 94f5c27
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 41 deletions.
26 changes: 4 additions & 22 deletions src/drivers/virtio/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ impl<'a> VqCfgHandler<'a> {
self.raw.as_mut_ptr().queue_device().write(addr.0.into());
}

pub fn notif_off(&mut self) -> u16 {
// we don't need an offset
0
}

pub fn enable_queue(&mut self) {
self.select_queue();

Expand Down Expand Up @@ -306,31 +301,18 @@ impl ComCfg {
/// See Virtio specification v1.1 - 4.1.4.4
pub struct NotifCfg {
/// Start addr, from where the notification addresses for the virtqueues are computed
queue_notify: *mut u32,
queue_notify: *mut le32,
}

impl NotifCfg {
pub fn new(mut registers: VolatileRef<'_, DeviceRegisters>) -> Self {
let raw = registers
.as_mut_ptr()
.queue_notify()
.as_raw_ptr()
.as_ptr()
.cast();
let raw = registers.as_mut_ptr().queue_notify().as_raw_ptr().as_ptr();

NotifCfg { queue_notify: raw }
}

/// Returns base address of notification area as an usize
pub fn base(&self) -> usize {
self.queue_notify as usize
}

/// Returns the multiplier, needed in order to calculate the
/// notification address for a specific queue.
pub fn multiplier(&self) -> u32 {
// we don't need a multiplier
0
pub fn notification_location(&self, _vq_cfg_handler: &mut VqCfgHandler<'_>) -> *mut le32 {
self.queue_notify
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,10 @@ impl NotifCfg {
})
}

/// Returns base address of notification area as an usize
pub fn base(&self) -> usize {
usize::from(self.base_addr)
}

/// Returns the multiplier, needed in order to calculate the
/// notification address for a specific queue.
pub fn multiplier(&self) -> u32 {
self.notify_off_multiplier
pub fn notification_location(&self, vq_cfg_handler: &mut VqCfgHandler<'_>) -> *mut le32 {
let addend = u32::from(vq_cfg_handler.notif_off()) * self.notify_off_multiplier;
let addr = usize::from(self.base_addr) + usize::try_from(addend).unwrap();
ptr::with_exposed_provenance_mut(addr)
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/drivers/virtio/virtqueue/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,11 +1129,7 @@ impl Virtq for PackedVq {
raw: dev_event,
};

let mut notif_ctrl = NotifCtrl::new(ptr::with_exposed_provenance_mut(
notif_cfg.base()
+ usize::from(vq_handler.notif_off())
* usize::try_from(notif_cfg.multiplier()).unwrap(),
));
let mut notif_ctrl = NotifCtrl::new(notif_cfg.notification_location(&mut vq_handler));

if features.contains(virtio_spec::F::NOTIFICATION_DATA) {
notif_ctrl.enable_notif_data();
Expand Down
6 changes: 1 addition & 5 deletions src/drivers/virtio/virtqueue/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,7 @@ impl Virtq for SplitVq {
used_ring_cell,
};

let mut notif_ctrl = NotifCtrl::new(ptr::with_exposed_provenance_mut(
notif_cfg.base()
+ usize::from(vq_handler.notif_off())
* usize::try_from(notif_cfg.multiplier()).unwrap(),
));
let mut notif_ctrl = NotifCtrl::new(notif_cfg.notification_location(&mut vq_handler));

if features.contains(virtio_spec::F::NOTIFICATION_DATA) {
notif_ctrl.enable_notif_data();
Expand Down

0 comments on commit 94f5c27

Please sign in to comment.