Add an iterator over VA mappings so drivers can walk and dump GPU VA
state in debugfs.

Signed-off-by: Alvin Sun <[email protected]>
---
 rust/kernel/drm/gpuvm/mod.rs |  6 +++++
 rust/kernel/drm/gpuvm/va.rs  | 53 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/rust/kernel/drm/gpuvm/mod.rs b/rust/kernel/drm/gpuvm/mod.rs
index 3186df9f740cf..41b99d468f9d9 100644
--- a/rust/kernel/drm/gpuvm/mod.rs
+++ b/rust/kernel/drm/gpuvm/mod.rs
@@ -301,6 +301,12 @@ pub fn data(&mut self) -> &mut T {
         // SAFETY: By the type invariants we may access `core`.
         unsafe { &mut *self.0.data.get() }
     }
+
+    /// Returns an iterator over the VA mappings in this GpuVm.
+    #[inline]
+    pub fn va_mappings(&self) -> GpuVaIter<'_, T> {
+        GpuVaIter::new(&self.0)
+    }
 }
 
 impl<T: DriverGpuVm> Deref for GpuVmCore<T> {
diff --git a/rust/kernel/drm/gpuvm/va.rs b/rust/kernel/drm/gpuvm/va.rs
index 8b8fd500b3c5f..3210047f880e3 100644
--- a/rust/kernel/drm/gpuvm/va.rs
+++ b/rust/kernel/drm/gpuvm/va.rs
@@ -1,6 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0 OR MIT
 
 use super::*;
+use crate::interop::list::{
+    CList,
+    CListIter, //
+};
+use core::{
+    marker::PhantomData,
+    mem::offset_of, //
+};
 
 /// Represents that a range of a GEM object is mapped in this [`GpuVm`] 
instance.
 ///
@@ -118,6 +126,51 @@ pub fn length(&self) -> u64 {
     }
 }
 
+const DRM_GPUVA_LIST_ENTRY_OFFSET: usize = offset_of!(bindings::drm_gpuva, 
rb.entry);
+type RawGpuVaIter<'a> = CListIter<'a, RawGpuVa, DRM_GPUVA_LIST_ENTRY_OFFSET>;
+
+/// An iterator over the VA mappings in a [`GpuVm`].
+pub struct GpuVaIter<'a, T: DriverGpuVm> {
+    raw_gpuva_iter: RawGpuVaIter<'a>,
+    kern_gpuva_ptr: *mut bindings::drm_gpuva,
+    _marker: PhantomData<&'a T>,
+}
+
+impl<'a, T: DriverGpuVm> GpuVaIter<'a, T> {
+    #[inline]
+    pub(crate) fn new(gpuvm: &'a GpuVm<T>) -> Self {
+        // SAFETY: `gpuvm` is valid.
+        let head = unsafe { &raw mut (*gpuvm.as_raw()).rb.list };
+        // SAFETY: `head` is a valid pointer to a drm_gpuva list head.
+        let clist = unsafe { CList::<RawGpuVa, 
DRM_GPUVA_LIST_ENTRY_OFFSET>::from_raw(head) };
+        let kern_gpuva_ptr = gpuvm.kernel_alloc_va().as_raw();
+
+        Self {
+            raw_gpuva_iter: clist.iter(),
+            kern_gpuva_ptr,
+            _marker: PhantomData,
+        }
+    }
+}
+
+impl<'a, T: DriverGpuVm> Iterator for GpuVaIter<'a, T> {
+    type Item = &'a GpuVa<T>;
+
+    #[inline]
+    fn next(&mut self) -> Option<Self::Item> {
+        let mut curr = self.raw_gpuva_iter.next()?;
+
+        if curr.as_raw() == self.kern_gpuva_ptr {
+            // Skip kernel reserved node.
+            curr = self.raw_gpuva_iter.next()?;
+        }
+
+        // SAFETY: We have skipped the kernel reserved node, all remaining
+        // entries are valid GpuVa<T> instances.
+        Some(unsafe { GpuVa::from_raw(curr.as_raw()) })
+    }
+}
+
 /// A pre-allocated [`GpuVa`] object.
 ///
 /// # Invariants

-- 
2.43.0


Reply via email to