Rust for Linux List
 help / color / mirror / Atom feed
From: Matteo Kloiber <kernel@matt3o12.de>
To: Danilo Krummrich <dakr@kernel.org>,
	Alexandre Courbot <acourbot@nvidia.com>,
	Alice Ryhl <aliceryhl@google.com>,
	Abdiel Janulgue <abdiel.janulgue@gmail.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	Gary Guo <gary@garyguo.net>
Cc: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev,
	dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev,
	linux-kernel@vger.kernel.org,
	"Matteo Kloiber" <kernel@matt3o12.de>
Subject: [RFC PATCH 0/3] rust: dma: require device configuration before DMA
Date: Mon, 23 Nov 2026 21:39:23 +0100	[thread overview]
Message-ID: <20261123203926.1787169-1-kernel@matt3o12.de> (raw)

This is the RFC that came out of the max_seg_size discussion [1]. It
implements the DMA setup token Alexandre proposed there, passed to
probe() through the InitTokens struct that he and Danilo suggested in
[2].

A driver now has to configure DMA before it can allocate or map
anything:

    let pci::InitTokens { dma: setup, .. } = tokens;

    let dma = setup.configure(dma::Params {
        streaming_mask: Some(mask),
        coherent_mask: Some(mask),
        max_seg_size: Some(NonZeroU32::MAX),
    })?;

    let buf = Coherent::<u64>::zeroed(dma, GFP_KERNEL)?;

configure() consumes the token and returns a copyable dma::Handle.
Coherent allocations, SGTable::new(), IoPageTable::new() and the shmem
sg_table() take that handle instead of a device, so they cannot be
called before configuration and the parameters cannot change
afterwards. The unsafe dma::Device setters are gone. Drivers that do no
DMA ignore the token.

- Patch 1 adds InitTokens to the PCI and platform probe() signature.
  It is mechanical and the only patch that touches drivers without DMA.
- Patch 2 reshuffles Gpu::new() in nova-core so that patch 3 has no
  reindentation in it.
- Patch 3 is the actual change.

Design decisions (feedback welcome):

- Params has no Default, so adding a field later requires updating every
  in-tree DMA driver. Because the kernel is a mono repo, I think this is the
  right tradeoff. It forces a deliberate choice between None and an explicit
  value for each driver rather than silently preserving existing behaviour.
  InitTokens is #[non_exhaustive] because most drivers will not care about
  unrelated tokens.

- configure() consumes the token even on error, so a driver cannot retry
  with a different mask. dma-api-howto.rst states that drivers should
  fail probe if setting the mask fails, and consuming the token enforces
  that. If a driver ever needs to probe supported masks first, a
  separate query method fits better than returning the token on error.

- Only PCI and platform probe() hand out a token. An auxiliary driver
  that performs DMA on behalf of its parent should receive the parent's
  dma::Handle rather than configuring DMA itself. No in-tree auxiliary
  driver needs this yet.

- Tyr keeps its current segment size (max_seg_size: None, with a TODO).
  Panthor sets UINT_MAX for the same hardware, so Tyr probably should
  too, but I left that out to avoid behaviour changes in this series. I
  can either send this as a separate patch or include it for the final
  patch.

One limitation: DRM C callbacks that map memory on the driver's behalf
do not go through the handle. For those the ordering is documented on
drm::Registration::new() but not enforced.

Every patch builds with CLIPPY=1, and the Rust KUnit suites and
doctests pass on each of them (x86_64, QEMU). nova-core and Tyr are
compile-tested only.

An LLM assisted with the design, implementation, including writing all comments
and text. I have reviewed all of it.

Based on drm-rust-next.

[1] https://lore.kernel.org/DLHH0K25NLX5.616R4D860TXU@nvidia.com
[2] https://lore.kernel.org/DLR1JCUWS824.35QJU45D2W9ZE@nvidia.com

Matteo Kloiber (3):
  rust: pci, platform: pass InitTokens to probe()
  gpu: nova-core: build Gpu inside pin_init_scope
  rust: dma: require device configuration before DMA

 drivers/cpufreq/rcpufreq_dt.rs                |   1 +
 drivers/gpu/drm/tyr/driver.rs                 |  18 +-
 drivers/gpu/drm/tyr/fw.rs                     |  11 +-
 drivers/gpu/drm/tyr/mmu/address_space.rs      |   7 +-
 drivers/gpu/drm/tyr/vm.rs                     |  28 +-
 drivers/gpu/nova-core/driver.rs               |   4 +-
 drivers/gpu/nova-core/falcon.rs               |  13 +-
 drivers/gpu/nova-core/fb.rs                   |  11 +-
 .../nova-core/firmware/fwsec/bootloader.rs    |  13 +-
 drivers/gpu/nova-core/firmware/gsp.rs         |  18 +-
 drivers/gpu/nova-core/firmware/gsp_fmc.rs     |  10 +-
 drivers/gpu/nova-core/firmware/radix3.rs      |  17 +-
 drivers/gpu/nova-core/firmware/riscv.rs       |  11 +-
 drivers/gpu/nova-core/fsp.rs                  |  20 +-
 drivers/gpu/nova-core/gpu.rs                  | 241 ++++++++--------
 drivers/gpu/nova-core/gsp.rs                  |  19 +-
 drivers/gpu/nova-core/gsp/boot.rs             |   2 +-
 drivers/gpu/nova-core/gsp/cmdq.rs             |  14 +-
 drivers/gpu/nova-core/gsp/hal/gh100.rs        |   9 +-
 drivers/gpu/nova-core/gsp/hal/tu102.rs        |  24 +-
 drivers/gpu/nova-core/gsp/logbuffer.rs        |  23 +-
 drivers/pwm/pwm_th1520.rs                     |   1 +
 rust/helpers/dma.c                            |   6 -
 rust/kernel/cpufreq.rs                        |   1 +
 rust/kernel/dma.rs                            | 264 +++++++++---------
 rust/kernel/drm/driver.rs                     |   4 +
 rust/kernel/drm/gem/shmem.rs                  |  23 +-
 rust/kernel/io/mem.rs                         |   2 +
 rust/kernel/iommu/pgtable.rs                  |  24 +-
 rust/kernel/pci.rs                            |  20 +-
 rust/kernel/platform.rs                       |  20 +-
 rust/kernel/scatterlist.rs                    |  20 +-
 samples/rust/rust_debugfs.rs                  |   1 +
 samples/rust/rust_dma.rs                      |  18 +-
 samples/rust/rust_driver_auxiliary.rs         |   1 +
 samples/rust/rust_driver_pci.rs               |   1 +
 samples/rust/rust_driver_platform.rs          |   1 +
 samples/rust/rust_i2c_client.rs               |   1 +
 samples/rust/rust_soc.rs                      |   1 +
 39 files changed, 514 insertions(+), 409 deletions(-)

-- 
2.51.2


             reply	other threads:[~2026-11-23 20:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 20:39 Matteo Kloiber [this message]
2026-11-23 20:39 ` [RFC PATCH 1/3] rust: pci, platform: pass InitTokens to probe() Matteo Kloiber
2026-11-23 20:39 ` [RFC PATCH 2/3] gpu: nova-core: build Gpu inside pin_init_scope Matteo Kloiber
2026-11-23 20:39 ` [RFC PATCH 3/3] rust: dma: require device configuration before DMA Matteo Kloiber

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=20261123203926.1787169-1-kernel@matt3o12.de \
    --to=kernel@matt3o12.de \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    /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