[PATCH v13 4/7] rust: device: add dma addressing capabilities

Abdiel Janulgue posted 7 patches 11 months, 1 week ago
There is a newer version of this series
[PATCH v13 4/7] rust: device: add dma addressing capabilities
Posted by Abdiel Janulgue 11 months, 1 week ago
Add functions to set the DMA mask to inform the kernel about the
device's DMA addressing capabilities.

Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>
---
 rust/helpers/dma.c     |  8 ++++++++
 rust/helpers/helpers.c |  1 +
 rust/kernel/device.rs  | 29 +++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)
 create mode 100644 rust/helpers/dma.c

diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c
new file mode 100644
index 000000000000..8eb482386f93
--- /dev/null
+++ b/rust/helpers/dma.c
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/dma-mapping.h>
+
+int rust_helper_dma_set_mask_and_coherent(struct device *dev, u64 mask)
+{
+	return dma_set_mask_and_coherent(dev, mask);
+}
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 0640b7e115be..8f3808c8b7fe 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -13,6 +13,7 @@
 #include "build_bug.c"
 #include "cred.c"
 #include "device.c"
+#include "dma.c"
 #include "err.c"
 #include "fs.c"
 #include "io.c"
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index db2d9658ba47..f9d3d4f60ddb 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -6,10 +6,12 @@
 
 use crate::{
     bindings,
+    error::Result,
     str::CStr,
     types::{ARef, Opaque},
 };
 use core::{fmt, ptr};
+use kernel::prelude::*;
 
 #[cfg(CONFIG_PRINTK)]
 use crate::c_str;
@@ -187,6 +189,33 @@ pub fn property_present(&self, name: &CStr) -> bool {
         // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
         unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
     }
+
+    /// Inform the kernel about the device's DMA addressing capabilities.
+    ///
+    /// Set both the DMA mask and the coherent DMA mask to the same thing.
+    /// Note that we don't check the return value from the C `dma_set_coherent_mask`
+    /// as the DMA API guarantees that the coherent DMA mask can be set to
+    /// the same or smaller than the streaming DMA mask.
+    pub fn dma_set_mask_and_coherent(&mut self, mask: u64) -> Result {
+        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
+        let ret = unsafe { bindings::dma_set_mask_and_coherent(self.as_raw(), mask) };
+        if ret != 0 {
+            Err(Error::from_errno(ret))
+        } else {
+            Ok(())
+        }
+    }
+
+    /// Same as [`dma_set_mask_and_coherent`], but set the mask only for streaming mappings.
+    pub fn dma_set_mask(&mut self, mask: u64) -> Result {
+        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
+        let ret = unsafe { bindings::dma_set_mask(self.as_raw(), mask) };
+        if ret != 0 {
+            Err(Error::from_errno(ret))
+        } else {
+            Ok(())
+        }
+    }
 }
 
 // SAFETY: Instances of `Device` are always reference-counted.
-- 
2.43.0
Re: [PATCH v13 4/7] rust: device: add dma addressing capabilities
Posted by Andreas Hindborg 11 months, 1 week ago
"Abdiel Janulgue" <abdiel.janulgue@gmail.com> writes:

> Add functions to set the DMA mask to inform the kernel about the
> device's DMA addressing capabilities.
>
> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>
> ---
>  rust/helpers/dma.c     |  8 ++++++++
>  rust/helpers/helpers.c |  1 +
>  rust/kernel/device.rs  | 29 +++++++++++++++++++++++++++++
>  3 files changed, 38 insertions(+)
>  create mode 100644 rust/helpers/dma.c
>
> diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c
> new file mode 100644
> index 000000000000..8eb482386f93
> --- /dev/null
> +++ b/rust/helpers/dma.c
> @@ -0,0 +1,8 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/dma-mapping.h>
> +
> +int rust_helper_dma_set_mask_and_coherent(struct device *dev, u64 mask)
> +{
> +	return dma_set_mask_and_coherent(dev, mask);
> +}
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 0640b7e115be..8f3808c8b7fe 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -13,6 +13,7 @@
>  #include "build_bug.c"
>  #include "cred.c"
>  #include "device.c"
> +#include "dma.c"
>  #include "err.c"
>  #include "fs.c"
>  #include "io.c"
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index db2d9658ba47..f9d3d4f60ddb 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
> @@ -6,10 +6,12 @@
>
>  use crate::{
>      bindings,
> +    error::Result,
>      str::CStr,
>      types::{ARef, Opaque},
>  };
>  use core::{fmt, ptr};
> +use kernel::prelude::*;
>
>  #[cfg(CONFIG_PRINTK)]
>  use crate::c_str;
> @@ -187,6 +189,33 @@ pub fn property_present(&self, name: &CStr) -> bool {
>          // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
>          unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
>      }
> +
> +    /// Inform the kernel about the device's DMA addressing capabilities.
> +    ///
> +    /// Set both the DMA mask and the coherent DMA mask to the same thing.
> +    /// Note that we don't check the return value from the C `dma_set_coherent_mask`
> +    /// as the DMA API guarantees that the coherent DMA mask can be set to
> +    /// the same or smaller than the streaming DMA mask.
> +    pub fn dma_set_mask_and_coherent(&mut self, mask: u64) -> Result {
> +        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
> +        let ret = unsafe { bindings::dma_set_mask_and_coherent(self.as_raw(), mask) };
> +        if ret != 0 {
> +            Err(Error::from_errno(ret))
> +        } else {
> +            Ok(())
> +        }
> +    }

I think we can use `Error::from_errno` here (and below). As far as I can
tell, these C functions return negative on error.

> +
> +    /// Same as [`dma_set_mask_and_coherent`], but set the mask only for streaming mappings.
> +    pub fn dma_set_mask(&mut self, mask: u64) -> Result {
> +        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
> +        let ret = unsafe { bindings::dma_set_mask(self.as_raw(), mask) };
> +        if ret != 0 {
> +            Err(Error::from_errno(ret))
> +        } else {
> +            Ok(())
> +        }
> +    }
>  }
>
>  // SAFETY: Instances of `Device` are always reference-counted.


Best regards,
Andreas Hindborg
Re: [PATCH v13 4/7] rust: device: add dma addressing capabilities
Posted by Abdiel Janulgue 11 months ago
Hi,

On 07/03/2025 22:12, Andreas Hindborg wrote:
> "Abdiel Janulgue" <abdiel.janulgue@gmail.com> writes:
> 
>> Add functions to set the DMA mask to inform the kernel about the
>> device's DMA addressing capabilities.
>>
>> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>
>> ---
>>   rust/helpers/dma.c     |  8 ++++++++
>>   rust/helpers/helpers.c |  1 +
>>   rust/kernel/device.rs  | 29 +++++++++++++++++++++++++++++
>>   3 files changed, 38 insertions(+)
>>   create mode 100644 rust/helpers/dma.c
>>
>> diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c
>> new file mode 100644
>> index 000000000000..8eb482386f93
>> --- /dev/null
>> +++ b/rust/helpers/dma.c
>> @@ -0,0 +1,8 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +#include <linux/dma-mapping.h>
>> +
>> +int rust_helper_dma_set_mask_and_coherent(struct device *dev, u64 mask)
>> +{
>> +	return dma_set_mask_and_coherent(dev, mask);
>> +}
>> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
>> index 0640b7e115be..8f3808c8b7fe 100644
>> --- a/rust/helpers/helpers.c
>> +++ b/rust/helpers/helpers.c
>> @@ -13,6 +13,7 @@
>>   #include "build_bug.c"
>>   #include "cred.c"
>>   #include "device.c"
>> +#include "dma.c"
>>   #include "err.c"
>>   #include "fs.c"
>>   #include "io.c"
>> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
>> index db2d9658ba47..f9d3d4f60ddb 100644
>> --- a/rust/kernel/device.rs
>> +++ b/rust/kernel/device.rs
>> @@ -6,10 +6,12 @@
>>
>>   use crate::{
>>       bindings,
>> +    error::Result,
>>       str::CStr,
>>       types::{ARef, Opaque},
>>   };
>>   use core::{fmt, ptr};
>> +use kernel::prelude::*;
>>
>>   #[cfg(CONFIG_PRINTK)]
>>   use crate::c_str;
>> @@ -187,6 +189,33 @@ pub fn property_present(&self, name: &CStr) -> bool {
>>           // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
>>           unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
>>       }
>> +
>> +    /// Inform the kernel about the device's DMA addressing capabilities.
>> +    ///
>> +    /// Set both the DMA mask and the coherent DMA mask to the same thing.
>> +    /// Note that we don't check the return value from the C `dma_set_coherent_mask`
>> +    /// as the DMA API guarantees that the coherent DMA mask can be set to
>> +    /// the same or smaller than the streaming DMA mask.
>> +    pub fn dma_set_mask_and_coherent(&mut self, mask: u64) -> Result {
>> +        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
>> +        let ret = unsafe { bindings::dma_set_mask_and_coherent(self.as_raw(), mask) };
>> +        if ret != 0 {
>> +            Err(Error::from_errno(ret))
>> +        } else {
>> +            Ok(())
>> +        }
>> +    }
> 
> I think we can use `Error::from_errno` here (and below). As far as I can
> tell, these C functions return negative on error.

This already uses `Error::from_errno`?

Thanks!

> 
>> +
>> +    /// Same as [`dma_set_mask_and_coherent`], but set the mask only for streaming mappings.
>> +    pub fn dma_set_mask(&mut self, mask: u64) -> Result {
>> +        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
>> +        let ret = unsafe { bindings::dma_set_mask(self.as_raw(), mask) };
>> +        if ret != 0 {
>> +            Err(Error::from_errno(ret))
>> +        } else {
>> +            Ok(())
>> +        }
>> +    }
>>   }
>>
>>   // SAFETY: Instances of `Device` are always reference-counted.
> 
> 
> Best regards,
> Andreas Hindborg
> 
>
Re: [PATCH v13 4/7] rust: device: add dma addressing capabilities
Posted by Miguel Ojeda 11 months ago
On Tue, Mar 11, 2025 at 6:45 PM Abdiel Janulgue
<abdiel.janulgue@gmail.com> wrote:
>
> This already uses `Error::from_errno`?

Maybe Andreas meant `to_result`?

Cheers,
Miguel
Re: [PATCH v13 4/7] rust: device: add dma addressing capabilities
Posted by Andreas Hindborg 11 months ago
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> writes:

> On Tue, Mar 11, 2025 at 6:45 PM Abdiel Janulgue
> <abdiel.janulgue@gmail.com> wrote:
>>
>> This already uses `Error::from_errno`?
>
> Maybe Andreas meant `to_result`?

Yes, sorry. It includes the if/else, but it tests for ret < 0. But I
think that is fine here.


Best regards,
Andreas Hindborg