From: Zhi Wang <[email protected]>

BAR mappings do not necessarily cover an entire VRAM block. A region
view lets a mapping select a byte range within an existing allocation.

For vGPU, communication buffers occupy only part of the management heap
and need a BAR mapping of that range.

Add VramRegion as a bounds-checked view that borrows its backing
VramBlock. Carry the backing borrow through subregions so Rust prevents
these views from outliving the allocation. Creating or cloning a view
does not acquire shared ownership of the reservation.

The owner remains responsible for ending device accesses before dropping
the backing allocation.

Cc: Alistair Popple <[email protected]>
Signed-off-by: Zhi Wang <[email protected]>
Co-developed-by: Alexandre Courbot <[email protected]>
Signed-off-by: Alexandre Courbot <[email protected]>
Signed-off-by: Alok Kumar <[email protected]>
---
 drivers/gpu/nova-core/mm/vram.rs | 56 ++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/drivers/gpu/nova-core/mm/vram.rs b/drivers/gpu/nova-core/mm/vram.rs
index 63bc0ce93663..be3076d065c6 100644
--- a/drivers/gpu/nova-core/mm/vram.rs
+++ b/drivers/gpu/nova-core/mm/vram.rs
@@ -21,6 +21,21 @@
     PAGE_SIZE, //
 };
 
+/// Rebases the nonempty range `rel`, relative to the start of `base`, onto 
`base`.
+///
+/// Returns `EINVAL` if `rel` is empty or does not fit within `base`.
+fn rebase(base: &Range<u64>, rel: Range<u64>) -> Result<Range<u64>> {
+    let start = base.start.checked_add(rel.start).ok_or(EINVAL)?;
+    let end = base.start.checked_add(rel.end).ok_or(EINVAL)?;
+
+    // Check that the rebased range on not empty and fits within `base`.
+    if start >= end || end > base.end {
+        return Err(EINVAL);
+    }
+
+    Ok(start..end)
+}
+
 /// A physically contiguous VRAM allocation.
 ///
 /// # Invariants
@@ -38,6 +53,47 @@ impl VramBlock {
     pub(crate) const fn range(&self) -> &Range<u64> {
         &self.range
     }
+
+    /// Creates a byte range relative to this allocation, borrowing its 
backing storage.
+    pub(crate) fn region(&self, range: Range<u64>) -> Result<VramRegion<'_>> {
+        Ok(VramRegion {
+            backing: self,
+            // INVARIANT: `rebase` returns a nonempty range.
+            range: rebase(&self.range, range)?,
+        })
+    }
+}
+
+/// A byte range borrowing its backing VRAM allocation.
+///
+/// # Invariants
+///
+/// `range.start < range.end`, and `range` lies within the range of `backing`.
+#[derive(Clone)]
+pub(crate) struct VramRegion<'pool> {
+    backing: &'pool VramBlock,
+    range: Range<u64>,
+}
+
+impl VramRegion<'_> {
+    /// Returns the range covered by this region.
+    pub(crate) const fn range(&self) -> &Range<u64> {
+        &self.range
+    }
+
+    /// Returns the length of this region in bytes.
+    pub(crate) const fn len(&self) -> u64 {
+        self.range.end.saturating_sub(self.range.start)
+    }
+
+    /// Creates a nonempty subregion relative to this view's start.
+    pub(crate) fn subregion(&self, range: Range<u64>) -> Result<Self> {
+        Ok(Self {
+            backing: self.backing,
+            // INVARIANT: `rebase` returns a nonempty range.
+            range: rebase(&self.range, range)?,
+        })
+    }
 }
 
 impl GpuMm<'_> {

Reply via email to