From: Zhi Wang <[email protected]>

BAR mappings do not necessarily cover an entire VRAM block. A region
layer lets a mapping select a byte range while keeping the underlying
allocation alive.

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

Add VramRegion to describe ranges within a shared VramBlock and support
bounds-checked subregions.

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 6839960eeb2c..6054eed9aeb0 100644
--- a/drivers/gpu/nova-core/mm/vram.rs
+++ b/drivers/gpu/nova-core/mm/vram.rs
@@ -22,6 +22,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
@@ -37,6 +52,47 @@ impl VramBlock {
     pub(crate) const fn range(&self) -> &Range<u64> {
         &self.range
     }
+
+    /// Creates a byte range relative to this allocation, retaining its 
backing storage.
+    pub(crate) fn region(self: &Arc<Self>, range: Range<u64>) -> 
Result<VramRegion> {
+        Ok(VramRegion {
+            backing: self.clone(),
+            // INVARIANT: `rebase` returns a nonempty range.
+            range: rebase(&self.range, range)?,
+        })
+    }
+}
+
+/// A byte range that keeps its VRAM allocation alive.
+///
+/// # Invariants
+///
+/// `range.start < range.end`, and `range` lies within the range of `backing`.
+#[derive(Clone)]
+pub(crate) struct VramRegion {
+    backing: Arc<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.clone(),
+            // INVARIANT: `rebase` returns a nonempty range.
+            range: rebase(&self.range, range)?,
+        })
+    }
 }
 
 impl GpuMm<'_> {
-- 
2.53.0

Reply via email to