public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Abdiel Janulgue" <abdiel.janulgue@gmail.com>
Cc: rust-for-linux@vger.kernel.org, daniel.almeida@collabora.com,
	dakr@kernel.org, robin.murphy@arm.com, aliceryhl@google.com,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Valentin Obst" <kernel@valentinobst.de>,
	linux-kernel@vger.kernel.org, "Christoph Hellwig" <hch@lst.de>,
	"Marek Szyprowski" <m.szyprowski@samsung.com>,
	airlied@redhat.com, iommu@lists.linux.dev
Subject: Re: [PATCH v13 4/7] rust: device: add dma addressing capabilities
Date: Fri, 07 Mar 2025 21:12:38 +0100	[thread overview]
Message-ID: <877c5038i1.fsf@kernel.org> (raw)
In-Reply-To: <20250307110821.1703422-5-abdiel.janulgue@gmail.com> (Abdiel Janulgue's message of "Fri, 07 Mar 2025 13:06:21 +0200")

"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



  reply	other threads:[~2025-03-07 20:13 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-07 11:06 [PATCH v13 0/7] rust: add dma coherent allocator abstraction Abdiel Janulgue
2025-03-07 11:06 ` [PATCH v13 1/7] rust: error: Add EOVERFLOW Abdiel Janulgue
2025-03-07 11:06 ` [PATCH v13 2/7] rust: add dma coherent allocator abstraction Abdiel Janulgue
2025-03-07 11:17   ` Alice Ryhl
2025-03-07 20:40   ` Andreas Hindborg
2025-03-21 17:23   ` Jason Gunthorpe
2025-03-21 17:34     ` Danilo Krummrich
2025-03-21 18:29       ` Jason Gunthorpe
2025-03-21 19:16         ` Danilo Krummrich
2025-03-07 11:06 ` [PATCH v13 3/7] rust: pci: impl AsMut<Device> for pci::Device Abdiel Janulgue
2025-03-07 11:18   ` Alice Ryhl
2025-03-07 11:45     ` Danilo Krummrich
2025-03-07 14:18   ` Greg KH
2025-03-07 17:53     ` Abdiel Janulgue
2025-03-07 11:06 ` [PATCH v13 4/7] rust: device: add dma addressing capabilities Abdiel Janulgue
2025-03-07 20:12   ` Andreas Hindborg [this message]
2025-03-11 17:45     ` Abdiel Janulgue
2025-03-11 18:35       ` Miguel Ojeda
2025-03-11 20:16         ` Andreas Hindborg
2025-03-07 11:06 ` [PATCH v13 5/7] samples: rust: add Rust dma test sample driver Abdiel Janulgue
2025-03-07 11:06 ` [PATCH v13 6/7] MAINTAINERS: add entry for Rust dma mapping helpers device driver API Abdiel Janulgue
2025-03-07 11:06 ` [PATCH v13 7/7] rust: dma: add as_slice/write functions for CoherentAllocation Abdiel Janulgue
2025-03-07 11:12 ` [PATCH v13 0/7] rust: add dma coherent allocator abstraction Danilo Krummrich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877c5038i1.fsf@kernel.org \
    --to=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=airlied@redhat.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=hch@lst.de \
    --cc=iommu@lists.linux.dev \
    --cc=kernel@valentinobst.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox