From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.216.248.203 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795991104; b=GCTjIxcPHR6S7FB6ncM69WhPuc9Ux8ESYcNp0x5OVNBIAzhbONYL5sybkf6K3BxDHFiC OVbsd18S5qlaEl2VrwewdhYqYrAPmXAKYc32iBhLeZWb4Wm+4+OdXnuhsfhmOPwhln5uS 29X259uq4wAXmbYMF1QqdbU5/6FISpmyIpocxlZ7xAS1yoQpm1zaB8xOCimh/vmnLYb8F J6GQzbtP8Wac5Ux5VVlnwNo6MeheYGSK7p4zaXx3n/vKABt+ODXBthtT5x+amyi4eXugp Rp0NM+EG21HMTMA2LKCV+zp2UBiYVW87mzXUGpyTD5a8GCDDqS9YyIx2ImfWEVuQYBw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991104; h=DMARC-Filter:From:X-Lima-ML-UUID:DKIM-Signature:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=6KyG1cY6UGpy+eoeS5Sws6Tc3bzyggtskUbP+seZREc=; b=xCiDUeGZbU0F3fhNpbxDEphDihhf5lxvhiSzOca1FedgC+IT9SKIamnOPNiFyyCBYq0U oLAv4z0Pz1AmoSiVdyj4ZO87zMVAueggecOhrsp1l7Oq4Y3fAPrVf1GjliCz3D1sm2Bf2 XiWMET+zUXwJ4CrqgQQxZp2QaZyvwRhrETRyhrSK+nI74WGG7bJNIVszIMOkGO0PItlsM oPNiFbfJ9pSuT/R+j1uxpQ8TobUCshefjKOrz5//yYN6rPlL8jbIfGJXqCeCa75MGGCL1 u3tx8aoPSbJCB35qe5a56/znxTWOO2yrmDKdBozNY2UKkcGPqoXr8vpMfgbzKbDkRiA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=matt3o12.de; spf=pass smtp.mailfrom=matt3o12.de; dkim=pass header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=CKfaap2o; arc=none smtp.remote-ip=91.216.248.203 Received: from mx1.mail-out.lima-city.de (mx1.mail-out.lima-city.de [91.216.248.203]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81919606A31C; Sun, 29 Nov 2026 22:25:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 81919606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=CKfaap2o From: Matteo Kloiber X-Lima-ML-UUID: 0d682c02-6dbc-47e4-aa1d-cd2645a91481 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=matt3o12.de; s=securedbylima-20230709; t=1795991094; bh=4lwUY77SIhUB/5eOfAJMivJu4csiS9RFX9MDuttntes=; h=From:To:Cc:Subject:Date:From; b=CKfaap2oUuVV37SiOHVexHTcmPB5a87HVwjksp1IJipMAvmMIJ3QdUNMuSutMQiy4 4E6vuMgVOnrx+uM1KCMtDcwtDxUhvtD8rjkqss9CB68LXXBVTtCMzGMMawGby0n6LE khHjlulKRq6+fIQ8p3n+af1DRiOPGbBsFoj1O+pMvBMhMX4k287ZcTP7eAc5GO0T2S VOa3yaJD21sFW7g2SFahyhVwiV66uJSzHQef9065DmtVhdTKlfRIeB41ATEC4NE4ly JQo2DJI3XGpjjPTrV+ftnBJt20JuNL5r2KLRvzW4Ckb9Uef9u3a5jJ4WCig6zwtIvk rbUpVJYr/WVzg== To: Danilo Krummrich , Alexandre Courbot , Alice Ryhl , Abdiel Janulgue , Robin Murphy , Andreas Hindborg , Gary Guo Cc: Daniel Almeida , Miguel Ojeda , David Airlie , Simona Vetter , Bjorn Helgaas , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Greg Kroah-Hartman , 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 Subject: [RFC PATCH v2 00/10] rust: dma: require device configuration before DMA Date: Sun, 29 Nov 2026 23:24:19 +0100 Message-ID: <20261129222429.2380652-1-kernel@matt3o12.de> X-Mailer: git-send-email 2.51.2 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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::::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 adds dma::Setup, dma::Params and dma::Handle, and hands out the token through InitTokens. Nothing has to use them yet. - Patches 3-5 convert nova-core, Tyr and the DMA sample to configure() and pass the handle to the code that allocates or maps. The DMA APIs still take a device at this point, so the drivers hand them dma.device(). - Patches 6-9 make the DMA APIs take the handle, one API per patch: coherent allocations, SGTable::new(), IoPageTable::new() and the shmem sg_table(). - Patch 10 removes the dma::Device trait and its setters. The ordering is only enforced from here on, because until then the old setters still exist next to the new token. Changes in v2: - Split the main patch into the steps above (Danilo). - Drop "gpu: nova-core: build Gpu inside pin_init_scope". Gpu now stores the dma::Handle in a field, where the fields initialized after it can use it (Danilo). GspResources keeps a copy rather than a reference, since the handle is Copy. The DMA parameters are again set at the same point in Gpu::new() as before the series. - Add the missing INVARIANT comments to Setup::new() and Handle::new_for_test(), and mark the small Setup and Handle methods #[inline] (Sashiko). - Drop the DMA paragraph from the drm::Registration::new() documentation. It read like a requirement on an unsafe function, but it is not a safety requirement. - Rename the dma::Handle parameters of the coherent allocation constructors from dev to dma. - Rebase onto current drm-rust-next and pass InitTokens to the new SR-IOV sample drivers. v1: https://lore.kernel.org/20261123203926.1787169-1-kernel@matt3o12.de 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 in the non-RFC series. - pci::Driver::DRIVER_MANAGED_DMA was merged in the meantime as an unsafe marker "until the DMA setup tokens exist". Such a driver still receives a dma::Setup, which it has promised not to use. Withholding the token would let the marker become safe. I left that out of this RFC and can add it once the token design is settled. One limitation: DRM C callbacks that map memory on the driver's behalf do not go through the handle. For those the ordering is 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 and 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 (10): rust: pci, platform: pass InitTokens to probe() rust: dma: add Setup, Params and Handle gpu: nova-core: configure DMA through dma::Setup drm/tyr: configure DMA through dma::Setup samples: rust: dma: configure DMA through dma::Setup rust: dma: require a Handle for coherent allocations rust: scatterlist: require a dma::Handle for SGTable::new() rust: iommu: require a dma::Handle for IoPageTable::new() rust: drm: require a dma::Handle for shmem sg_table() rust: dma: remove the Device trait and its setters 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 | 47 ++- 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 | 334 ++++++++++-------- rust/kernel/drm/gem/shmem.rs | 23 +- rust/kernel/io/mem.rs | 2 + rust/kernel/iommu/pgtable.rs | 24 +- rust/kernel/pci.rs | 21 +- rust/kernel/platform.rs | 21 +- rust/kernel/scatterlist.rs | 22 +- 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_driver_sriov/pf.rs | 1 + samples/rust/rust_driver_sriov/vf.rs | 1 + samples/rust/rust_i2c_client.rs | 1 + samples/rust/rust_soc.rs | 1 + 40 files changed, 458 insertions(+), 343 deletions(-) base-commit: abfb640b2374e26de8ff81d488c39d8254db538e -- 2.51.2