From: Danilo Krummrich <[email protected]>

VFIO manages DMA ownership through the userspace IOMMU interface. Its
PCI variant drivers must opt out of the driver's default DMA domain.

Add Driver::DRIVER_MANAGED_DMA and pass it to the PCI core. Make it an
unsafe marker as suggested, until the DMA setup tokens exist.

Signed-off-by: Danilo Krummrich <[email protected]>
Signed-off-by: Zhi Wang <[email protected]>
Signed-off-by: Simon Song <[email protected]>
---
 rust/kernel/pci.rs | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index fa67f2fc26bf..6013c89bfb5e 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -105,6 +105,7 @@ unsafe fn register(
             (*pdrv.get()).probe = Some(Self::probe_callback);
             (*pdrv.get()).remove = Some(Self::remove_callback);
             (*pdrv.get()).id_table = T::ID_TABLE.as_ptr();
+            (*pdrv.get()).driver_managed_dma = T::DRIVER_MANAGED_DMA.is_some();
             if T::HAS_SRIOV_ENABLE {
                 (*pdrv.get()).sriov_configure = 
Some(Self::sriov_configure_callback);
             }
@@ -293,6 +294,24 @@ unsafe impl RawDeviceIdIndex for DeviceId {
     const DRIVER_DATA_OFFSET: usize = 
core::mem::offset_of!(bindings::pci_device_id, driver_data);
 }
 
+/// Marker for a driver that manages DMA ownership of its devices by itself.
+///
+/// The driver core does not claim the default DMA domain for the devices of 
such a driver. See
+/// [`Driver::DRIVER_MANAGED_DMA`].
+pub struct DriverManagedDma<D: ?Sized>(PhantomData<fn() -> *mut D>);
+
+impl<D: ?Sized> DriverManagedDma<D> {
+    /// Declares that driver `D` manages DMA ownership of its devices by 
itself.
+    ///
+    /// # Safety
+    ///
+    /// The caller must ensure that devices bound to `D` are not used for DMA 
allocations or
+    /// mappings through the kernel DMA API, directly or through helpers.
+    pub const unsafe fn new() -> Self {
+        Self(PhantomData)
+    }
+}
+
 /// `IdTable` type for PCI.
 pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
 
@@ -356,6 +375,10 @@ pub trait Driver {
     /// The table of device ids supported by the driver.
     const ID_TABLE: IdTable<Self::IdInfo>;
 
+    /// Set if the driver manages DMA ownership of its devices by itself, 
instead of using the
+    /// default DMA domain. Construct with [`DriverManagedDma::new`].
+    const DRIVER_MANAGED_DMA: Option<DriverManagedDma<Self>> = None;
+
     /// PCI driver probe.
     ///
     /// Called when a new pci device is added or discovered. Implementers 
should
-- 
2.43.7

Reply via email to