Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp516888rwl; Wed, 5 Apr 2023 04:14:54 -0700 (PDT) X-Google-Smtp-Source: AKy350ZpqOkHQl3QbNuP1pX/XMlfvETUSrygmWjdHHc8GYLwLV0gA/xC9m4QKoyXxdrGj9VpSDey X-Received: by 2002:a05:6a00:1a49:b0:627:f40a:fd35 with SMTP id h9-20020a056a001a4900b00627f40afd35mr2332312pfv.10.1680693293875; Wed, 05 Apr 2023 04:14:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680693293; cv=none; d=google.com; s=arc-20160816; b=tF9ACT3o6Ny1+lXkjxcU3tLT5wEeM+suN0znlLdSTwGIIr4kp1zWigOLKPGqNP6IDZ RxhWOtm5E+Tc7VQA86MA9IuPOrzSL7BfRK5koOCCRszqCTSr5Lk84ndOzYQH6SNeOZIN lidSQh45qKGlLMbhJOEAXavQrQjlZLK02B5V5vZJfr6W2n77bo/XEBz9Xp6QugbC1huw OQXk/QgteRTYYAg1GViZ5L1DHGQ9b3fldLb6lRgCBHr4zpQPlZlIIfPfpPzr1dDncmS3 SmPzVw3Lu5dLc1or4LqRcbcnmSvTVA6U6o+W/rJNON/tfpbIxydc2YoaKcfTCjHwUFJ0 DKWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature; bh=e0IJj83P580/QdjNGq/F+5jrN+Ih843C49nX5zWdsOw=; b=neAMFw+K/sSezh00t3uAbU01wMk3HECvSX6x6WLldldflIoo0BlGjnUaGp3wz21v5c rQsIDp2W+hEBnQU/MHkLfG1C0sBRTDeBQAzLB+HPcxiR4m84uNIaKv5pOOcV6mOZ97kE 5/mEPTQY2NaBsmWimhmC60flinwF7CTCNrqTMRJKfQB0TsC5hCWSILAnlWzWApbo3KpT mQ5pag7t9DtOGypw+p7yC38RhDn2A3Q4K2sO6q9biF5OB3cDNO67/ziPMea9M9ujf7UH e9dNG3+fbEz583S4ro5hS659Rc00pV5i60Y9nsI+TrCu7kjp4s3HFurYVsKOAML7UjIo 4epw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=W40GfZvv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g27-20020aa79ddb000000b0058d8e5bbbc2si12578482pfq.146.2023.04.05.04.14.41; Wed, 05 Apr 2023 04:14:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=W40GfZvv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237684AbjDELKd (ORCPT + 99 others); Wed, 5 Apr 2023 07:10:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237635AbjDELK3 (ORCPT ); Wed, 5 Apr 2023 07:10:29 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52EE255B3 for ; Wed, 5 Apr 2023 04:10:24 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5029d4d90fbso32579a12.0 for ; Wed, 05 Apr 2023 04:10:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; t=1680693023; x=1683285023; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date:from:to:cc :subject:date:message-id:reply-to; bh=e0IJj83P580/QdjNGq/F+5jrN+Ih843C49nX5zWdsOw=; b=W40GfZvvFa5P5NwiWmT0+UTEpl5An2+HYAQYzwgrUX5d5L3cGNvGWBF/m8RV9w1bJd ywORQ2hLPLfzL96QLkaRgnZlLIsmYoXCPPSfFkkpFP53sMXnHORt11RMmj6X8KxAcAZI WrlWpbpw77vnfs4IwhieKFQoxK38nxHJcK41A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680693023; x=1683285023; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=e0IJj83P580/QdjNGq/F+5jrN+Ih843C49nX5zWdsOw=; b=W0KAwKIp3+Q90N9q98zJdLvjVFddatmHGa5dJdpC9nY8QmGDFzMR5SyIQFWVp1JU4x WujssUl2VN3+UGoRVdBoCtJjJAplhjre956lP9Pk3J536tfp9PY+tIWI9k7yxxBO8XBI F8kTkjm/DJN3oT9dyyMxmyWygNBuUAHYvEb8mbsXBzelvPjl/PaPj8KVwGEL1DnrbA9Q Et//r/kyvdBgzQUsUZqkTq/D0YQXB7T5KPnmRYxjJFI9G3vjVJyJKnzhFj0ewMvGtQr/ JOy80/dligb+EuebNyITkIhf62PC9vjhTohblyketi+99UxFe7Kiq36lqdJMnA9CngRZ hXUA== X-Gm-Message-State: AAQBX9dOXULFuxcKVU+U3KVlz2W3Kt+TJtp2vuxViEQDVkSoYp0R3MMw FsU8fVrPI1ciMWY2MHzTwX64zZXIKfsbxb+ShoM= X-Received: by 2002:a17:906:d4:b0:947:404b:eb2 with SMTP id 20-20020a17090600d400b00947404b0eb2mr1218551eji.0.1680693022498; Wed, 05 Apr 2023 04:10:22 -0700 (PDT) Received: from phenom.ffwll.local (212-51-149-33.fiber7.init7.net. [212.51.149.33]) by smtp.gmail.com with ESMTPSA id i1-20020a170906444100b0091ec885e016sm7176978ejp.54.2023.04.05.04.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 04:10:22 -0700 (PDT) Date: Wed, 5 Apr 2023 13:10:20 +0200 From: Daniel Vetter To: Asahi Lina Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Sumit Semwal , Christian =?iso-8859-1?Q?K=F6nig?= , Luben Tuikov , Jarkko Sakkinen , Dave Hansen , Alyssa Rosenzweig , Karol Herbst , Ella Stanforth , Faith Ekstrand , Mary , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-sgx@vger.kernel.org, asahi@lists.linux.dev Subject: Re: [PATCH RFC 08/18] rust: dma_fence: Add DMA Fence abstraction Message-ID: Mail-Followup-To: Asahi Lina , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Sumit Semwal , Christian =?iso-8859-1?Q?K=F6nig?= , Luben Tuikov , Jarkko Sakkinen , Dave Hansen , Alyssa Rosenzweig , Karol Herbst , Ella Stanforth , Faith Ekstrand , Mary , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-sgx@vger.kernel.org, asahi@lists.linux.dev References: <20230307-rust-drm-v1-0-917ff5bc80a8@asahilina.net> <20230307-rust-drm-v1-8-917ff5bc80a8@asahilina.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230307-rust-drm-v1-8-917ff5bc80a8@asahilina.net> X-Operating-System: Linux phenom 6.1.0-7-amd64 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 07, 2023 at 11:25:33PM +0900, Asahi Lina wrote: > DMA fences are the internal synchronization primitive used for DMA > operations like GPU rendering, video en/decoding, etc. Add an > abstraction to allow Rust drivers to interact with this subsystem. > > Note: This uses a raw spinlock living next to the fence, since we do > not interact with it other than for initialization. > TODO: Expose this to the user at some point with a safe abstraction. > > Signed-off-by: Asahi Lina > --- > rust/bindings/bindings_helper.h | 2 + > rust/helpers.c | 53 ++++ > rust/kernel/dma_fence.rs | 532 ++++++++++++++++++++++++++++++++++++++++ This should probably be in the dma-buf namespace like on the C side? There's a pile of tightly coupled concepts that I expect we'll all need sooner or later (dma-fence/buf/resv at least). Also I guess same questions about separate files and MAINTAINER entries as for the drm stuff. -Daniel > rust/kernel/lib.rs | 2 + > 4 files changed, 589 insertions(+) > > diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h > index 9f152d373df8..705af292a5b4 100644 > --- a/rust/bindings/bindings_helper.h > +++ b/rust/bindings/bindings_helper.h > @@ -14,6 +14,8 @@ > #include > #include > #include > +#include > +#include > #include > #include > #include > diff --git a/rust/helpers.c b/rust/helpers.c > index 388ff1100ea5..8e906a7a7d8a 100644 > --- a/rust/helpers.c > +++ b/rust/helpers.c > @@ -23,6 +23,8 @@ > #include > #include > #include > +#include > +#include > #include > #include > #include > @@ -30,6 +32,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -388,6 +391,56 @@ int rust_helper_sg_dma_len(const struct scatterlist *sg) > } > EXPORT_SYMBOL_GPL(rust_helper_sg_dma_len); > > +void rust_helper___spin_lock_init(spinlock_t *lock, const char *name, > + struct lock_class_key *key) > +{ > +#ifdef CONFIG_DEBUG_SPINLOCK > +# ifndef CONFIG_PREEMPT_RT > + __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); > +# else > + rt_mutex_base_init(&lock->lock); > + __rt_spin_lock_init(lock, name, key, false); > +# endif > +#else > + spin_lock_init(lock); > +#endif > +} > +EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init); > + > +#ifdef CONFIG_DMA_SHARED_BUFFER > + > +void rust_helper_dma_fence_get(struct dma_fence *fence) > +{ > + dma_fence_get(fence); > +} > +EXPORT_SYMBOL_GPL(rust_helper_dma_fence_get); > + > +void rust_helper_dma_fence_put(struct dma_fence *fence) > +{ > + dma_fence_put(fence); > +} > +EXPORT_SYMBOL_GPL(rust_helper_dma_fence_put); > + > +struct dma_fence_chain *rust_helper_dma_fence_chain_alloc(void) > +{ > + return dma_fence_chain_alloc(); > +} > +EXPORT_SYMBOL_GPL(rust_helper_dma_fence_chain_alloc); > + > +void rust_helper_dma_fence_chain_free(struct dma_fence_chain *chain) > +{ > + dma_fence_chain_free(chain); > +} > +EXPORT_SYMBOL_GPL(rust_helper_dma_fence_chain_free); > + > +void rust_helper_dma_fence_set_error(struct dma_fence *fence, int error) > +{ > + dma_fence_set_error(fence, error); > +} > +EXPORT_SYMBOL_GPL(rust_helper_dma_fence_set_error); > + > +#endif > + > #ifdef CONFIG_DRM > > void rust_helper_drm_gem_object_get(struct drm_gem_object *obj) > diff --git a/rust/kernel/dma_fence.rs b/rust/kernel/dma_fence.rs > new file mode 100644 > index 000000000000..ca93380d9da2 > --- /dev/null > +++ b/rust/kernel/dma_fence.rs > @@ -0,0 +1,532 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! DMA fence abstraction. > +//! > +//! C header: [`include/linux/dma_fence.h`](../../include/linux/dma_fence.h) > + > +use crate::{ > + bindings, > + error::{to_result, Result}, > + prelude::*, > + sync::LockClassKey, > + types::Opaque, > +}; > +use core::fmt::Write; > +use core::ops::{Deref, DerefMut}; > +use core::ptr::addr_of_mut; > +use core::sync::atomic::{AtomicU64, Ordering}; > + > +/// Any kind of DMA Fence Object > +/// > +/// # Invariants > +/// raw() returns a valid pointer to a dma_fence and we own a reference to it. > +pub trait RawDmaFence: crate::private::Sealed { > + /// Returns the raw `struct dma_fence` pointer. > + fn raw(&self) -> *mut bindings::dma_fence; > + > + /// Returns the raw `struct dma_fence` pointer and consumes the object. > + /// > + /// The caller is responsible for dropping the reference. > + fn into_raw(self) -> *mut bindings::dma_fence > + where > + Self: Sized, > + { > + let ptr = self.raw(); > + core::mem::forget(self); > + ptr > + } > + > + /// Advances this fence to the chain node which will signal this sequence number. > + /// If no sequence number is provided, this returns `self` again. > + fn chain_find_seqno(self, seqno: u64) -> Result > + where > + Self: Sized, > + { > + let mut ptr = self.into_raw(); > + > + // SAFETY: This will safely fail if this DmaFence is not a chain. > + // `ptr` is valid per the type invariant. > + let ret = unsafe { bindings::dma_fence_chain_find_seqno(&mut ptr, seqno) }; > + > + if ret != 0 { > + // SAFETY: This is either an owned reference or NULL, dma_fence_put can handle both. > + unsafe { bindings::dma_fence_put(ptr) }; > + Err(Error::from_kernel_errno(ret)) > + } else if ptr.is_null() { > + Err(EINVAL) // When can this happen? > + } else { > + // SAFETY: ptr is valid and non-NULL as checked above. > + Ok(unsafe { Fence::from_raw(ptr) }) > + } > + } > + > + /// Signal completion of this fence > + fn signal(&self) -> Result { > + to_result(unsafe { bindings::dma_fence_signal(self.raw()) }) > + } > + > + /// Set the error flag on this fence > + fn set_error(&self, err: Error) { > + unsafe { bindings::dma_fence_set_error(self.raw(), err.to_kernel_errno()) }; > + } > +} > + > +/// A generic DMA Fence Object > +/// > +/// # Invariants > +/// ptr is a valid pointer to a dma_fence and we own a reference to it. > +pub struct Fence { > + ptr: *mut bindings::dma_fence, > +} > + > +impl Fence { > + /// Create a new Fence object from a raw pointer to a dma_fence. > + /// > + /// # Safety > + /// The caller must own a reference to the dma_fence, which is transferred to the new object. > + pub(crate) unsafe fn from_raw(ptr: *mut bindings::dma_fence) -> Fence { > + Fence { ptr } > + } > + > + /// Create a new Fence object from a raw pointer to a dma_fence. > + /// > + /// # Safety > + /// Takes a borrowed reference to the dma_fence, and increments the reference count. > + pub(crate) unsafe fn get_raw(ptr: *mut bindings::dma_fence) -> Fence { > + // SAFETY: Pointer is valid per the safety contract > + unsafe { bindings::dma_fence_get(ptr) }; > + Fence { ptr } > + } > + > + /// Create a new Fence object from a RawDmaFence. > + pub fn from_fence(fence: &dyn RawDmaFence) -> Fence { > + // SAFETY: Pointer is valid per the RawDmaFence contract > + unsafe { Self::get_raw(fence.raw()) } > + } > +} > + > +impl crate::private::Sealed for Fence {} > + > +impl RawDmaFence for Fence { > + fn raw(&self) -> *mut bindings::dma_fence { > + self.ptr > + } > +} > + > +impl Drop for Fence { > + fn drop(&mut self) { > + // SAFETY: We own a reference to this syncobj. > + unsafe { bindings::dma_fence_put(self.ptr) }; > + } > +} > + > +impl Clone for Fence { > + fn clone(&self) -> Self { > + // SAFETY: `ptr` is valid per the type invariant and we own a reference to it. > + unsafe { > + bindings::dma_fence_get(self.ptr); > + Self::from_raw(self.ptr) > + } > + } > +} > + > +unsafe impl Sync for Fence {} > +unsafe impl Send for Fence {} > + > +/// Trait which must be implemented by driver-specific fence objects. > +#[vtable] > +pub trait FenceOps: Sized + Send + Sync { > + /// True if this dma_fence implementation uses 64bit seqno, false otherwise. > + const USE_64BIT_SEQNO: bool; > + > + /// Returns the driver name. This is a callback to allow drivers to compute the name at > + /// runtime, without having it to store permanently for each fence, or build a cache of > + /// some sort. > + fn get_driver_name<'a>(self: &'a FenceObject) -> &'a CStr; > + > + /// Return the name of the context this fence belongs to. This is a callback to allow drivers > + /// to compute the name at runtime, without having it to store permanently for each fence, or > + /// build a cache of some sort. > + fn get_timeline_name<'a>(self: &'a FenceObject) -> &'a CStr; > + > + /// Enable software signaling of fence. > + fn enable_signaling(self: &FenceObject) -> bool { > + false > + } > + > + /// Peek whether the fence is signaled, as a fastpath optimization for e.g. dma_fence_wait() or > + /// dma_fence_add_callback(). > + fn signaled(self: &FenceObject) -> bool { > + false > + } > + > + /// Callback to fill in free-form debug info specific to this fence, like the sequence number. > + fn fence_value_str(self: &FenceObject, _output: &mut dyn Write) {} > + > + /// Fills in the current value of the timeline as a string, like the sequence number. Note that > + /// the specific fence passed to this function should not matter, drivers should only use it to > + /// look up the corresponding timeline structures. > + fn timeline_value_str(self: &FenceObject, _output: &mut dyn Write) {} > +} > + > +unsafe extern "C" fn get_driver_name_cb( > + fence: *mut bindings::dma_fence, > +) -> *const core::ffi::c_char { > + // SAFETY: All of our fences are FenceObject. > + let p = crate::container_of!(fence, FenceObject, fence) as *mut FenceObject; > + > + // SAFETY: The caller is responsible for passing a valid dma_fence subtype > + T::get_driver_name(unsafe { &mut *p }).as_char_ptr() > +} > + > +unsafe extern "C" fn get_timeline_name_cb( > + fence: *mut bindings::dma_fence, > +) -> *const core::ffi::c_char { > + // SAFETY: All of our fences are FenceObject. > + let p = crate::container_of!(fence, FenceObject, fence) as *mut FenceObject; > + > + // SAFETY: The caller is responsible for passing a valid dma_fence subtype > + T::get_timeline_name(unsafe { &mut *p }).as_char_ptr() > +} > + > +unsafe extern "C" fn enable_signaling_cb(fence: *mut bindings::dma_fence) -> bool { > + // SAFETY: All of our fences are FenceObject. > + let p = crate::container_of!(fence, FenceObject, fence) as *mut FenceObject; > + > + // SAFETY: The caller is responsible for passing a valid dma_fence subtype > + T::enable_signaling(unsafe { &mut *p }) > +} > + > +unsafe extern "C" fn signaled_cb(fence: *mut bindings::dma_fence) -> bool { > + // SAFETY: All of our fences are FenceObject. > + let p = crate::container_of!(fence, FenceObject, fence) as *mut FenceObject; > + > + // SAFETY: The caller is responsible for passing a valid dma_fence subtype > + T::signaled(unsafe { &mut *p }) > +} > + > +unsafe extern "C" fn release_cb(fence: *mut bindings::dma_fence) { > + // SAFETY: All of our fences are FenceObject. > + let p = crate::container_of!(fence, FenceObject, fence) as *mut FenceObject; > + > + // SAFETY: p is never used after this > + unsafe { > + core::ptr::drop_in_place(&mut (*p).inner); > + } > + > + // SAFETY: All of our fences are allocated using kmalloc, so this is safe. > + unsafe { bindings::dma_fence_free(fence) }; > +} > + > +unsafe extern "C" fn fence_value_str_cb( > + fence: *mut bindings::dma_fence, > + string: *mut core::ffi::c_char, > + size: core::ffi::c_int, > +) { > + let size: usize = size.try_into().unwrap_or(0); > + > + if size == 0 { > + return; > + } > + > + // SAFETY: All of our fences are FenceObject. > + let p = crate::container_of!(fence, FenceObject, fence) as *mut FenceObject; > + > + // SAFETY: The caller is responsible for the validity of string/size > + let mut f = unsafe { crate::str::Formatter::from_buffer(string as *mut _, size) }; > + > + // SAFETY: The caller is responsible for passing a valid dma_fence subtype > + T::fence_value_str(unsafe { &mut *p }, &mut f); > + let _ = f.write_str("\0"); > + > + // SAFETY: `size` is at least 1 per the check above > + unsafe { *string.add(size - 1) = 0 }; > +} > + > +unsafe extern "C" fn timeline_value_str_cb( > + fence: *mut bindings::dma_fence, > + string: *mut core::ffi::c_char, > + size: core::ffi::c_int, > +) { > + let size: usize = size.try_into().unwrap_or(0); > + > + if size == 0 { > + return; > + } > + > + // SAFETY: All of our fences are FenceObject. > + let p = crate::container_of!(fence, FenceObject, fence) as *mut FenceObject; > + > + // SAFETY: The caller is responsible for the validity of string/size > + let mut f = unsafe { crate::str::Formatter::from_buffer(string as *mut _, size) }; > + > + // SAFETY: The caller is responsible for passing a valid dma_fence subtype > + T::timeline_value_str(unsafe { &mut *p }, &mut f); > + let _ = f.write_str("\0"); > + > + // SAFETY: `size` is at least 1 per the check above > + unsafe { *string.add(size - 1) = 0 }; > +} > + > +// Allow FenceObject to be used as a self argument, for ergonomics > +impl core::ops::Receiver for FenceObject {} > + > +/// A driver-specific DMA Fence Object > +/// > +/// # Invariants > +/// ptr is a valid pointer to a dma_fence and we own a reference to it. > +#[repr(C)] > +pub struct FenceObject { > + fence: bindings::dma_fence, > + lock: Opaque, > + inner: T, > +} > + > +impl FenceObject { > + const SIZE: usize = core::mem::size_of::(); > + > + const VTABLE: bindings::dma_fence_ops = bindings::dma_fence_ops { > + use_64bit_seqno: T::USE_64BIT_SEQNO, > + get_driver_name: Some(get_driver_name_cb::), > + get_timeline_name: Some(get_timeline_name_cb::), > + enable_signaling: if T::HAS_ENABLE_SIGNALING { > + Some(enable_signaling_cb::) > + } else { > + None > + }, > + signaled: if T::HAS_SIGNALED { > + Some(signaled_cb::) > + } else { > + None > + }, > + wait: None, // Deprecated > + release: Some(release_cb::), > + fence_value_str: if T::HAS_FENCE_VALUE_STR { > + Some(fence_value_str_cb::) > + } else { > + None > + }, > + timeline_value_str: if T::HAS_TIMELINE_VALUE_STR { > + Some(timeline_value_str_cb::) > + } else { > + None > + }, > + }; > +} > + > +impl Deref for FenceObject { > + type Target = T; > + > + fn deref(&self) -> &T { > + &self.inner > + } > +} > + > +impl DerefMut for FenceObject { > + fn deref_mut(&mut self) -> &mut T { > + &mut self.inner > + } > +} > + > +impl crate::private::Sealed for FenceObject {} > +impl RawDmaFence for FenceObject { > + fn raw(&self) -> *mut bindings::dma_fence { > + &self.fence as *const _ as *mut _ > + } > +} > + > +/// A unique reference to a driver-specific fence object > +pub struct UniqueFence(*mut FenceObject); > + > +impl Deref for UniqueFence { > + type Target = FenceObject; > + > + fn deref(&self) -> &FenceObject { > + unsafe { &*self.0 } > + } > +} > + > +impl DerefMut for UniqueFence { > + fn deref_mut(&mut self) -> &mut FenceObject { > + unsafe { &mut *self.0 } > + } > +} > + > +impl crate::private::Sealed for UniqueFence {} > +impl RawDmaFence for UniqueFence { > + fn raw(&self) -> *mut bindings::dma_fence { > + unsafe { addr_of_mut!((*self.0).fence) } > + } > +} > + > +impl From> for UserFence { > + fn from(value: UniqueFence) -> Self { > + let ptr = value.0; > + core::mem::forget(value); > + > + UserFence(ptr) > + } > +} > + > +impl Drop for UniqueFence { > + fn drop(&mut self) { > + // SAFETY: We own a reference to this fence. > + unsafe { bindings::dma_fence_put(self.raw()) }; > + } > +} > + > +unsafe impl Sync for UniqueFence {} > +unsafe impl Send for UniqueFence {} > + > +/// A shared reference to a driver-specific fence object > +pub struct UserFence(*mut FenceObject); > + > +impl Deref for UserFence { > + type Target = FenceObject; > + > + fn deref(&self) -> &FenceObject { > + unsafe { &*self.0 } > + } > +} > + > +impl Clone for UserFence { > + fn clone(&self) -> Self { > + // SAFETY: `ptr` is valid per the type invariant and we own a reference to it. > + unsafe { > + bindings::dma_fence_get(self.raw()); > + Self(self.0) > + } > + } > +} > + > +impl crate::private::Sealed for UserFence {} > +impl RawDmaFence for UserFence { > + fn raw(&self) -> *mut bindings::dma_fence { > + unsafe { addr_of_mut!((*self.0).fence) } > + } > +} > + > +impl Drop for UserFence { > + fn drop(&mut self) { > + // SAFETY: We own a reference to this fence. > + unsafe { bindings::dma_fence_put(self.raw()) }; > + } > +} > + > +unsafe impl Sync for UserFence {} > +unsafe impl Send for UserFence {} > + > +/// An array of fence contexts, out of which fences can be created. > +pub struct FenceContexts { > + start: u64, > + count: u32, > + seqnos: Vec, > + lock_name: &'static CStr, > + lock_key: &'static LockClassKey, > +} > + > +impl FenceContexts { > + /// Create a new set of fence contexts. > + pub fn new( > + count: u32, > + name: &'static CStr, > + key: &'static LockClassKey, > + ) -> Result { > + let mut seqnos: Vec = Vec::new(); > + > + seqnos.try_reserve(count as usize)?; > + > + for _ in 0..count { > + seqnos.try_push(Default::default())?; > + } > + > + let start = unsafe { bindings::dma_fence_context_alloc(count as core::ffi::c_uint) }; > + > + Ok(FenceContexts { > + start, > + count, > + seqnos, > + lock_name: name, > + lock_key: key, > + }) > + } > + > + /// Create a new fence in a given context index. > + pub fn new_fence(&self, context: u32, inner: T) -> Result> { > + if context > self.count { > + return Err(EINVAL); > + } > + > + let p = unsafe { > + bindings::krealloc( > + core::ptr::null_mut(), > + FenceObject::::SIZE, > + bindings::GFP_KERNEL | bindings::__GFP_ZERO, > + ) as *mut FenceObject > + }; > + > + if p.is_null() { > + return Err(ENOMEM); > + } > + > + let seqno = self.seqnos[context as usize].fetch_add(1, Ordering::Relaxed); > + > + // SAFETY: The pointer is valid, so pointers to members are too. > + // After this, all fields are initialized. > + unsafe { > + addr_of_mut!((*p).inner).write(inner); > + bindings::__spin_lock_init( > + addr_of_mut!((*p).lock) as *mut _, > + self.lock_name.as_char_ptr(), > + self.lock_key.get(), > + ); > + bindings::dma_fence_init( > + addr_of_mut!((*p).fence), > + &FenceObject::::VTABLE, > + addr_of_mut!((*p).lock) as *mut _, > + self.start + context as u64, > + seqno, > + ); > + }; > + > + Ok(UniqueFence(p)) > + } > +} > + > +/// A DMA Fence Chain Object > +/// > +/// # Invariants > +/// ptr is a valid pointer to a dma_fence_chain which we own. > +pub struct FenceChain { > + ptr: *mut bindings::dma_fence_chain, > +} > + > +impl FenceChain { > + /// Create a new DmaFenceChain object. > + pub fn new() -> Result { > + // SAFETY: This function is safe to call and takes no arguments. > + let ptr = unsafe { bindings::dma_fence_chain_alloc() }; > + > + if ptr.is_null() { > + Err(ENOMEM) > + } else { > + Ok(FenceChain { ptr }) > + } > + } > + > + /// Convert the DmaFenceChain into the underlying raw pointer. > + /// > + /// This assumes the caller will take ownership of the object. > + pub(crate) fn into_raw(self) -> *mut bindings::dma_fence_chain { > + let ptr = self.ptr; > + core::mem::forget(self); > + ptr > + } > +} > + > +impl Drop for FenceChain { > + fn drop(&mut self) { > + // SAFETY: We own this dma_fence_chain. > + unsafe { bindings::dma_fence_chain_free(self.ptr) }; > + } > +} > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index cb23d24c6718..31866069e0bc 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -36,6 +36,8 @@ mod allocator; > mod build_assert; > pub mod delay; > pub mod device; > +#[cfg(CONFIG_DMA_SHARED_BUFFER)] > +pub mod dma_fence; > pub mod driver; > #[cfg(CONFIG_RUST_DRM)] > pub mod drm; > > -- > 2.35.1 > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch