Skip to contentSkip to conversation

rust: dma: allow drivers to tune max segment size

PATCH In review Beata Michalska 4 participants 4 messages Jan 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 rust: dma: allow drivers to tune max segment size

Make dma_set_max_seg_size() available to Rust so drivers can perform standard DMA setup steps.

---
Apologies for delay.

v3:
- Added __rust_helper

v2:
- Aligned safety requirements

 rust/helpers/dma.c |  6 ++++++
 rust/kernel/dma.rs | 17 +++++++++++++++++
 2 files changed, 23 insertions(+)
rust/helpers/dma.c +6 -0
index e7defeecda71..20232ac64850 100644
@@ -43,3 +43,9 @@ size_t rust_helper_dma_max_mapping_size(struct device *dev)
 {
 	return dma_max_mapping_size(dev);
 }
+
+__rust_helper void rust_helper_dma_set_max_seg_size(struct device *dev,
+						    unsigned int size)
+{
+	dma_set_max_seg_size(dev, size);
+}
rust/kernel/dma.rs +17 -0
index acc65b1e0f24..909d56fd5118 100644
@@ -85,6 +85,23 @@ unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {
             bindings::dma_set_mask_and_coherent(self.as_ref().as_raw(), mask.value())
         })
     }
+
+    /// Set the maximum size of a single DMA segment the device may request.
+    ///
+    /// This method is usually called once from `probe()` as soon as the device capabilities are
+    /// known.
+    ///
+    /// # Safety
+    ///
+    /// This method must not be called concurrently with any DMA allocation or mapping primitives,
+    /// such as [`CoherentAllocation::alloc_attrs`].
+    unsafe fn dma_set_max_seg_size(&self, size: u32) {
+        // SAFETY:
+        // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
+        // - The safety requirement of this function guarantees that there are no concurrent calls
+        //   to DMA allocation and mapping primitives using this parameter.
+        unsafe { bindings::dma_set_max_seg_size(self.as_ref().as_raw(), size) }
+    }
 }
 
 /// A DMA mask that holds a bitmask with the lowest `n` bits set.
Signature · 1 line
2.25.1
reviewer Acked-by on v3 1/1
#2 lore raw
In reply to Beata Michalska
Quoted 2 lines · Beata Michalska

Make dma_set_max_seg_size() available to Rust so drivers can perform standard DMA setup steps.

Yup, API-wise this goes hand-in-hand with dma_set_mask calls, so keeping all the requirements in sync is the right thing to do. FWIW,

Quoted 56 lines

---
Apologies for delay.

v3:
- Added __rust_helper

v2:
- Aligned safety requirements

  rust/helpers/dma.c |  6 ++++++
  rust/kernel/dma.rs | 17 +++++++++++++++++
  2 files changed, 23 insertions(+)
rust/helpers/dma.c +6 -0
index e7defeecda71..20232ac64850 100644
@@ -43,3 +43,9 @@ size_t rust_helper_dma_max_mapping_size(struct device *dev)
  {
  	return dma_max_mapping_size(dev);
  }
+
+__rust_helper void rust_helper_dma_set_max_seg_size(struct device *dev,
+						    unsigned int size)
+{
+	dma_set_max_seg_size(dev, size);
+}
rust/kernel/dma.rs +17 -0
index acc65b1e0f24..909d56fd5118 100644
@@ -85,6 +85,23 @@ unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {
              bindings::dma_set_mask_and_coherent(self.as_ref().as_raw(), mask.value())
          })
      }
+
+    /// Set the maximum size of a single DMA segment the device may request.
+    ///
+    /// This method is usually called once from `probe()` as soon as the device capabilities are
+    /// known.
+    ///
+    /// # Safety
+    ///
+    /// This method must not be called concurrently with any DMA allocation or mapping primitives,
+    /// such as [`CoherentAllocation::alloc_attrs`].
+    unsafe fn dma_set_max_seg_size(&self, size: u32) {
+        // SAFETY:
+        // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
+        // - The safety requirement of this function guarantees that there are no concurrent calls
+        //   to DMA allocation and mapping primitives using this parameter.
+        unsafe { bindings::dma_set_max_seg_size(self.as_ref().as_raw(), size) }
+    }
  }
 
  /// A DMA mask that holds a bitmask with the lowest `n` bits set.