From patchwork Fri Mar 17 12:12:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179001 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 66A65C6FD1D for ; Fri, 17 Mar 2023 12:13:27 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0D10C10EEEC; Fri, 17 Mar 2023 12:13:24 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id C389610EEEA for ; Fri, 17 Mar 2023 12:13:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=kJaQBIOfBDs0RWrdELn2youmyuMDTiMuE+q39Kq1BLg=; b=hTiBfpccfb2e+p5dsLtL8znqCw KybIWKF8huofaSYeHF5npijUa3pg8ERQM9bVNP/ctX3S+A6s8yaWMwo6UiTGKmEQ2EdpwZuoV1S1K S8QEaoD1s5gV5641IKz/3Px8Unlfnw5ipOt5B3MePzeAeYdLGqQZAkueB7rG5Ol7HlqFq/KBb2pua JheFiMmnwFQ4TdHX1vBIWXb/3qEyBmcjs8v0UjQaEIz6D1Hwj/62Mce0SVl9SkwFRguCj41s174LK OKlsA3RT1z/b7ZYx5VQrWuI0C7lPCoeaXnut9/8P8TWV/Rw9gusy3sQpZ0GlH5xDUCN1QA5jTfiL9 hWYird5A==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xA-00FrGz-Da; Fri, 17 Mar 2023 13:13:00 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 1/9] rust: dma_resv: add DMA Reservation abstraction Date: Fri, 17 Mar 2023 09:12:05 -0300 Message-Id: <20230317121213.93991-2-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The DMA reservation object provides a mechanism to manage a container of dma_fence object associated with a resource. So, add an abstraction to allow Rust drivers to interact with this subsystem. Signed-off-by: Maíra Canal --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 19 +++++++++ rust/kernel/dma_resv.rs | 75 +++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 4 files changed, 96 insertions(+) create mode 100644 rust/kernel/dma_resv.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 3fcf3ee330ad..1bf6d762d36e 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index 7cded4a36375..18c0c434ad73 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -437,6 +438,24 @@ void rust_helper_dma_fence_set_error(struct dma_fence *fence, int error) } EXPORT_SYMBOL_GPL(rust_helper_dma_fence_set_error); +enum dma_resv_usage rust_helper_dma_resv_usage_rw(bool write) +{ + return dma_resv_usage_rw(write); +} +EXPORT_SYMBOL_GPL(rust_helper_dma_resv_usage_rw); + +int rust_helper_dma_resv_lock(struct dma_resv *obj, struct ww_acquire_ctx *ctx) +{ + return dma_resv_lock(obj, ctx); +} +EXPORT_SYMBOL_GPL(rust_helper_dma_resv_lock); + +void rust_helper_dma_resv_unlock(struct dma_resv *obj) +{ + dma_resv_unlock(obj); +} +EXPORT_SYMBOL_GPL(rust_helper_dma_resv_unlock); + #endif #ifdef CONFIG_DRM diff --git a/rust/kernel/dma_resv.rs b/rust/kernel/dma_resv.rs new file mode 100644 index 000000000000..5e45aad4ae75 --- /dev/null +++ b/rust/kernel/dma_resv.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT + +//! DMA resv abstraction +//! +//! C header: [`include/linux/dma-resv.h`](../../include/linux/dma-resv.h) + +use crate::bindings; +use crate::dma_fence::RawDmaFence; +use crate::error::{Error, Result}; + +/// A generic DMA Resv Object +/// +/// # Invariants +/// ptr is a valid pointer to a dma_resv and we own a reference to it. +pub struct DmaResv { + ptr: *mut bindings::dma_resv, +} + +impl DmaResv { + /// Create a new DmaResv object from a raw pointer to a dma_resv. + /// + /// # Safety + /// The caller must own a reference to the dma_resv, which is transferred to the new object. + pub unsafe fn from_raw(ptr: *mut bindings::dma_resv) -> Self { + Self { ptr } + } + + /// Returns the implicit synchronization usage for write or read accesses. + pub fn usage_rw(&self, write: bool) -> bindings::dma_resv_usage { + // SAFETY: write is a valid bool. + unsafe { bindings::dma_resv_usage_rw(write) } + } + + /// Reserve space to add fences to a dma_resv object. + pub fn reserve_fences(&self, num_fences: u32) -> Result { + // SAFETY: We own a reference to this dma_resv. + let ret = unsafe { bindings::dma_resv_reserve_fences(self.ptr, num_fences) }; + + if ret != 0 { + return Err(Error::from_kernel_errno(ret)); + } + Ok(()) + } + + /// Add a fence to the dma_resv object + pub fn add_fences( + &self, + fence: &dyn RawDmaFence, + num_fences: u32, + usage: bindings::dma_resv_usage, + ) -> Result { + // SAFETY: We own a reference to this dma_resv. + unsafe { bindings::dma_resv_lock(self.ptr, core::ptr::null_mut()) }; + + let ret = self.reserve_fences(num_fences); + if ret.is_ok() { + // SAFETY: ptr is locked with dma_resv_lock(), and dma_resv_reserve_fences() + // has been called. + unsafe { + bindings::dma_resv_add_fence(self.ptr, fence.raw(), usage); + } + } + + // SAFETY: We own a reference to this dma_resv. + unsafe { bindings::dma_resv_unlock(self.ptr) }; + + ret + } + + /// Test if a reservation object’s fences have been signaled. + pub fn test_signaled(&self, usage: bindings::dma_resv_usage) -> bool { + // SAFETY: We own a reference to this dma_resv. + unsafe { bindings::dma_resv_test_signaled(self.ptr, usage) } + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index ba18deedf6b6..b9fbb07d0bdc 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -38,6 +38,7 @@ pub mod delay; pub mod device; #[cfg(CONFIG_DMA_SHARED_BUFFER)] pub mod dma_fence; +pub mod dma_resv; pub mod driver; #[cfg(CONFIG_DRM = "y")] pub mod drm; From patchwork Fri Mar 17 12:12:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179003 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 72DBEC74A5B for ; Fri, 17 Mar 2023 12:13:33 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7202B10EEF1; Fri, 17 Mar 2023 12:13:26 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3EE8A10EEF0 for ; Fri, 17 Mar 2023 12:13:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=hmWHlCmiofIyKwpwtriUZ+4xsHHrXTvx5Jjwzs3UrCw=; b=ZG8jDIQ/ZyfR4gr55yegtKZOhk h/qPeXAc5kJUB8mokOMDsC0gNKfFYHrKSBxp86ZO6euc8B/Uu7ngtj8UrCBp0BCgjDeysLqVSIDtr Kl4WFlW92pg+HFFBjXIRpgWgOWI7rvyhZDzP71l2OmtiYGRcBB3iqlu6y0kKcZnDU/lZ0FwUBXTjj RLimdGjJ40TAzlEwhwFFLboPwW0B6aMPsUF024UZ5uSOniqs5EZnCuCXlB16kJgu9iRKP2hNq0K6a jE58eM7QWYcWOirHwzvsaYxeCyrgeZDWlJC9LvvR3KKDK/vtgJGuXPBxZmVbGvNJIOJLOMaZefVgJ IbX9nB4Q==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xG-00FrGz-8J; Fri, 17 Mar 2023 13:13:06 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 2/9] rust: drm: gem: add method to return DmaResv from GEMObject Date: Fri, 17 Mar 2023 09:12:06 -0300 Message-Id: <20230317121213.93991-3-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" A GEMObject, related to struct drm_gem_object, holds a pointer to reservation object associated with the this GEM object. Therefore, expose this reservation object through the DmaResv safe abstraction. Signed-off-by: Maíra Canal --- rust/kernel/drm/gem/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index c4a42bb2f718..dae95e5748a7 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -11,6 +11,7 @@ use alloc::boxed::Box; use crate::{ bindings, + dma_resv::DmaResv, drm::{device, drv, file}, error::{to_result, Result}, prelude::*, @@ -136,6 +137,12 @@ pub trait BaseObject: IntoGEMObject { self.gem_obj().size } + /// Returns the pointer to reservation object associated with this GEM object. + fn resv(&self) -> DmaResv { + // SAFETY: Every GEM object holds a reference to a reservation object + unsafe { DmaResv::from_raw(self.gem_obj().resv) } + } + /// Sets the exportable flag, which controls whether the object can be exported via PRIME. fn set_exportable(&mut self, exportable: bool) { self.mut_gem_obj().exportable = exportable; From patchwork Fri Mar 17 12:12:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179002 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B97EAC74A5B for ; Fri, 17 Mar 2023 12:13:30 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5EBB010EEF3; Fri, 17 Mar 2023 12:13:25 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3420C10EEF0 for ; Fri, 17 Mar 2023 12:13:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=lzWvFbZbAyZruu9DSeVDnH0iLl0UAkl4YDHSvxPSfQA=; b=nWarxa9qPNcJhFpe1R68L9WvyX z09O9hFhl1RqkXSEyeEcXcm2fcXssUvRGIXxAbUu1TPhgZf2JBqB5jXFcHIUpxeF9aK9M8VJjVimL bCtULiLc4TpQoCrHQtAI/slfKrViBHe7TZJSnbtVnZPEwE/6Mhd8hp0DnFB38ci9DpVH7YyooIvkc Qy201DX93vHqtk6mGg/rqgGioXmHOns4aT1PizTZh0VybaF9lxCiEqblIws+om2g3AR9ncwfkTHpF Rp7L4jeWcSEKzH5WE4mfTeoFumYNfM731eHY0WhYK4+Rjiqntzu3PLyM5uPNV+TyZYDgFwXGI+OOh 8RIqGykg==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xM-00FrGz-EQ; Fri, 17 Mar 2023 13:13:12 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 3/9] rust: dma_fence: add method to return an indication if the fence is signaled Date: Fri, 17 Mar 2023 09:12:07 -0300 Message-Id: <20230317121213.93991-4-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To indicate the current status of the fence, there is a kernel function that returns an indication if the fence is signaled yet. Therefore, add a method in the Rust abstraction to return an indication if the fence is signaled. Signed-off-by: Maíra Canal --- rust/helpers.c | 6 ++++++ rust/kernel/dma_fence.rs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 18c0c434ad73..f77bfa10d5f5 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -420,6 +420,12 @@ void rust_helper_dma_fence_put(struct dma_fence *fence) } EXPORT_SYMBOL_GPL(rust_helper_dma_fence_put); +bool rust_helper_dma_fence_is_signaled(struct dma_fence *fence) +{ + return dma_fence_is_signaled(fence); +} +EXPORT_SYMBOL_GPL(rust_helper_dma_fence_is_signaled); + struct dma_fence_chain *rust_helper_dma_fence_chain_alloc(void) { return dma_fence_chain_alloc(); diff --git a/rust/kernel/dma_fence.rs b/rust/kernel/dma_fence.rs index ca93380d9da2..176e6d250e6c 100644 --- a/rust/kernel/dma_fence.rs +++ b/rust/kernel/dma_fence.rs @@ -65,6 +65,11 @@ pub trait RawDmaFence: crate::private::Sealed { to_result(unsafe { bindings::dma_fence_signal(self.raw()) }) } + /// Return an indication if the fence is signaled yet. + fn is_signaled(&self) -> bool { + unsafe { bindings::dma_fence_is_signaled(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()) }; From patchwork Fri Mar 17 12:12:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179004 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CA1B7C74A5B for ; Fri, 17 Mar 2023 12:13:35 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E69D610EEF4; Fri, 17 Mar 2023 12:13:31 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id BB45910EEF4 for ; Fri, 17 Mar 2023 12:13:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=wxlvNLGKgBNmusrXwv5nJvC1jT8zNLs4o63V3Oq55OI=; b=D8fYSVFHxRxkWM8ooaCtJrSslk iYawuRl6Efmm9iNfhGGKGnjSWBepYC0agou6YKRqLPTqodLqxjyH42HfNTn2QKLZbzQheCuIiZqpo uFGU1Zu6mWfGfFxXCogN1Rcs+SjFe7u35jS02p1WHa5NSIBFFHvflP6N1HXi7GpS4Qs8lnZWq5bKS n045cp1UUxElZqZdHAePrQhYAJSHSEfKDtOUo0EBiqOkIxqB3vHov1SKbn8IUDCrTSUQBD8UJym3y vqrsjn8+TqetKivDqwQ9i93DRneKt1yA7Rtu1Wcac8fQ29/MTIKPXF+tcEccLbLZz6gyy8qLR4jOJ Sn4pX8xg==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xS-00FrGz-J4; Fri, 17 Mar 2023 13:13:19 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 4/9] rust: dma_fence: expose the fence's seqno publically Date: Fri, 17 Mar 2023 09:12:08 -0300 Message-Id: <20230317121213.93991-5-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Each fence has a linear increasing sequence number inside the execution context, that can be used to decide which fence would be signaled later. So, expose this attribute to the Rust drivers through a method. Signed-off-by: Maíra Canal --- rust/kernel/dma_fence.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/dma_fence.rs b/rust/kernel/dma_fence.rs index 176e6d250e6c..94fead520274 100644 --- a/rust/kernel/dma_fence.rs +++ b/rust/kernel/dma_fence.rs @@ -60,6 +60,13 @@ pub trait RawDmaFence: crate::private::Sealed { } } + /// Return the seqno from this fence + fn seqno(&self) -> u64 { + // SAFETY: We hold a reference to a dma_fence and every dma_fence holds + // a seqno. + unsafe { (*self.raw()).seqno } + } + /// Signal completion of this fence fn signal(&self) -> Result { to_result(unsafe { bindings::dma_fence_signal(self.raw()) }) From patchwork Fri Mar 17 12:12:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179005 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7788AC76196 for ; Fri, 17 Mar 2023 12:13:40 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 73A3B10EEF7; Fri, 17 Mar 2023 12:13:39 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0E83710EEF7 for ; Fri, 17 Mar 2023 12:13:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=VCIAue9sPRRBRwR7VXuYy3+00JJGub0y0AdfNvYDsPA=; b=LlhCielTqX7+UtDofy9HguL/oQ rqCFRRUsBa3vHPRLBW381bTj/ELcx4pJL9haBsXunGpE7cYlJqqeW2XYC80LXDySAVpZk5DZ1HstL t+ziIa7QJjb5yVVM/gOv4VJKGX5aDPp/35VurpFRucRtD+PSq747ANhcL4xVx7K7cViuKPOZPlPcG yh5TXOCjA10lGy1glK0X94AYMr2Bsizj1rnhX5fsJQEMAzMhkQHbxjRM2QlDSxPyknSJt6Siocd9O rYsCW3oayxUwJNWvk4EoWZGGecUjlmGlCPbgflD8plm957/jwaRqnGgmforuBwuKz05cTPf3aA8fo 56To5DxQ==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xZ-00FrGz-7y; Fri, 17 Mar 2023 13:13:25 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 5/9] rust: drm: gem: shmem: set map_wc on gem_create_object callback Date: Fri, 17 Mar 2023 09:12:09 -0300 Message-Id: <20230317121213.93991-6-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Some drivers use the gem_create_object callback to define the mapping of the object write-combined (map_wc). Currently, the DRM Rust abstractions doesn't allow such operation. So, add a constant to the DriverObject trait to allow drivers to set map_wc on the gem_create_object callback. By default, the constant returns false, which is the shmem default value. Signed-off-by: Maíra Canal --- rust/kernel/drm/gem/shmem.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs index 8f17eba0be99..d6c56b3cfa71 100644 --- a/rust/kernel/drm/gem/shmem.rs +++ b/rust/kernel/drm/gem/shmem.rs @@ -24,6 +24,9 @@ use gem::BaseObject; pub trait DriverObject: gem::BaseDriverObject> { /// Parent `Driver` for this object. type Driver: drv::Driver; + + /// Define the map object write-combined + const MAP_WC: bool = false; } // FIXME: This is terrible and I don't know how to avoid it @@ -110,6 +113,8 @@ unsafe extern "C" fn gem_create_object( let new: &mut Object = unsafe { &mut *(p as *mut _) }; new.obj.base.funcs = &Object::::VTABLE; + new.obj.map_wc = ::MAP_WC; + &mut new.obj.base } From patchwork Fri Mar 17 12:12:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179006 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E0AF5C6FD1D for ; Fri, 17 Mar 2023 12:13:49 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 220E810EEF8; Fri, 17 Mar 2023 12:13:49 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8B5FE10EEF8 for ; Fri, 17 Mar 2023 12:13:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=/WKUdMa+rFnar+m5MEJR5FApwgbeESggxlzzv31hPyg=; b=YUSpuvwvXF7w6JCAchHXh5anX7 dDTByVK9yPgwGjG5XK9RP986Wre2Ezb4sKF75mJvDNaMByhSijDWSVpWvRq/V6wOeFSXnVCq94YWO 4vrv8KZjQeiCIhezvtzi182KP7xUQsOTcNoPAlP1UYCEc7r4D/w7kfq+JNtR6nfwPv85VgONOx4j+ paXk8FzlkZYTC7gZywj+Acgd1gyBbzgYFHxTBDFiP4ZrXfPgmyA/bco885ABF4D4db4j++88sObbl Vp/48QO2spyJn2jPDes9hDzfCNEWedBqn6zq9857QX6sv4d53OqN7Ajzpx5KPZoQefNQmDngKsAam J7ZjWCmQ==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xf-00FrGz-9a; Fri, 17 Mar 2023 13:13:31 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 6/9] drm/vgem: move IOCTLs numbers to enum Date: Fri, 17 Mar 2023 09:12:10 -0300 Message-Id: <20230317121213.93991-7-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Bindgen doesn't expand C macros, which makes DRM_IOCTL_VGEM_FENCE_ATTACH and DRM_IOCTL_VGEM_FENCE_SIGNAL not accessible through the Rust bindings. So, move the IOCTLs numbers to a enum, preserving the same values and the same name. This won't produce any functional changes to the UAPI and it will make it possible to use this UAPI in Rust. Signed-off-by: Maíra Canal --- include/uapi/drm/vgem_drm.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/uapi/drm/vgem_drm.h b/include/uapi/drm/vgem_drm.h index bf66f5db6da8..53ee3af0b25a 100644 --- a/include/uapi/drm/vgem_drm.h +++ b/include/uapi/drm/vgem_drm.h @@ -39,9 +39,6 @@ extern "C" { #define DRM_VGEM_FENCE_ATTACH 0x1 #define DRM_VGEM_FENCE_SIGNAL 0x2 -#define DRM_IOCTL_VGEM_FENCE_ATTACH DRM_IOWR( DRM_COMMAND_BASE + DRM_VGEM_FENCE_ATTACH, struct drm_vgem_fence_attach) -#define DRM_IOCTL_VGEM_FENCE_SIGNAL DRM_IOW( DRM_COMMAND_BASE + DRM_VGEM_FENCE_SIGNAL, struct drm_vgem_fence_signal) - struct drm_vgem_fence_attach { __u32 handle; __u32 flags; @@ -55,6 +52,12 @@ struct drm_vgem_fence_signal { __u32 flags; }; +/* Note: this is an enum so that it can be resolved by Rust bindgen. */ +enum { + DRM_IOCTL_VGEM_FENCE_ATTACH = DRM_IOWR(DRM_COMMAND_BASE + DRM_VGEM_FENCE_ATTACH, struct drm_vgem_fence_attach), + DRM_IOCTL_VGEM_FENCE_SIGNAL = DRM_IOW(DRM_COMMAND_BASE + DRM_VGEM_FENCE_SIGNAL, struct drm_vgem_fence_signal), +}; + #if defined(__cplusplus) } #endif From patchwork Fri Mar 17 12:12:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179007 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 91195C74A5B for ; Fri, 17 Mar 2023 12:13:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B9B3210EEFD; Fri, 17 Mar 2023 12:13:57 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0464F10EEFD for ; Fri, 17 Mar 2023 12:13:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=SG4ltpL9G6+swbrKGEeMx5f5tqfqBQLa9yPR9e99PN4=; b=fMvVOs++yDmAoD6K+JkpeXGzV8 8LeP2ua61vFkKeDzSZi9vehqVHPkneuOcSC1EjAMc7wevgOlxzyrQFa2YTk4QERtstEP/6EYw/A4T Bm0xs4RXocMKFUISl8wY4TtDoHa7xO8e6lrm3HaN/cbSSqBp9xclq18CdYemuY1STSpXnMJ8BqRP3 rGjfX0XO+PzdV4YG/l8+UxiS6LToN+ZFVjfGog4075elvpXnM8/yvA2AAeJBtB2j4oxn5vm+M6XCW FK2rSt5dlSx9LOm4YwGNhdPy9qKttoROsXi2sr3ONfxeZuQ4Bv+wh1qMOri6a1UE9nCj5hg5bO8AB sccLjlZQ==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xk-00FrGz-W5; Fri, 17 Mar 2023 13:13:37 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 7/9] drm/rustgem: implement a Rust version of VGEM Date: Fri, 17 Mar 2023 09:12:11 -0300 Message-Id: <20230317121213.93991-8-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The VGEM driver is a non-hardware backed GEM service and it is currently implemented in C on the DRM subsystem. This patch introduces a new version of the VGEM driver written in Rust. This driver provides the same functionalities and uses the same UAPI of the original VGEM driver. Signed-off-by: Maíra Canal --- drivers/gpu/drm/Kconfig | 11 +++ drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/rustgem/Makefile | 3 + drivers/gpu/drm/rustgem/fence.rs | 56 ++++++++++++++ drivers/gpu/drm/rustgem/file.rs | 128 +++++++++++++++++++++++++++++++ drivers/gpu/drm/rustgem/gem.rs | 31 ++++++++ drivers/gpu/drm/rustgem/vgem.rs | 104 +++++++++++++++++++++++++ rust/bindings/bindings_helper.h | 1 + 8 files changed, 335 insertions(+) create mode 100644 drivers/gpu/drm/rustgem/Makefile create mode 100644 drivers/gpu/drm/rustgem/fence.rs create mode 100644 drivers/gpu/drm/rustgem/file.rs create mode 100644 drivers/gpu/drm/rustgem/gem.rs create mode 100644 drivers/gpu/drm/rustgem/vgem.rs diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig index 315cbdf61979..0f886a33e377 100644 --- a/drivers/gpu/drm/Kconfig +++ b/drivers/gpu/drm/Kconfig @@ -245,6 +245,17 @@ source "drivers/gpu/drm/kmb/Kconfig" config DRM_VGEM tristate "Virtual GEM provider" + depends on !RUST + depends on DRM && MMU + select DRM_GEM_SHMEM_HELPER + help + Choose this option to get a virtual graphics memory manager, + as used by Mesa's software renderer for enhanced performance. + If M is selected the module will be called vgem. + +config DRM_RUSTGEM + tristate "Virtual GEM provider written in Rust" + depends on RUST depends on DRM && MMU select DRM_GEM_SHMEM_HELPER help diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile index cc637343d87b..8bfd40a2d341 100644 --- a/drivers/gpu/drm/Makefile +++ b/drivers/gpu/drm/Makefile @@ -150,6 +150,7 @@ obj-$(CONFIG_DRM_SAVAGE)+= savage/ obj-$(CONFIG_DRM_VMWGFX)+= vmwgfx/ obj-$(CONFIG_DRM_VIA) +=via/ obj-$(CONFIG_DRM_VGEM) += vgem/ +obj-$(CONFIG_DRM_RUSTGEM) += rustgem/ obj-$(CONFIG_DRM_VKMS) += vkms/ obj-$(CONFIG_DRM_NOUVEAU) +=nouveau/ obj-$(CONFIG_DRM_EXYNOS) +=exynos/ diff --git a/drivers/gpu/drm/rustgem/Makefile b/drivers/gpu/drm/rustgem/Makefile new file mode 100644 index 000000000000..94b67cec0377 --- /dev/null +++ b/drivers/gpu/drm/rustgem/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_DRM_RUSTGEM) += vgem.o diff --git a/drivers/gpu/drm/rustgem/fence.rs b/drivers/gpu/drm/rustgem/fence.rs new file mode 100644 index 000000000000..9ef1399548e2 --- /dev/null +++ b/drivers/gpu/drm/rustgem/fence.rs @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: MIT + +use core::fmt::Write; +use core::ops::Deref; +use kernel::c_str; +use kernel::dma_fence::*; +use kernel::prelude::*; +use kernel::sync::Arc; + +static QUEUE_NAME: &CStr = c_str!("vgem_fence"); +static QUEUE_CLASS_KEY: kernel::sync::LockClassKey = kernel::sync::LockClassKey::new(); + +pub(crate) struct Fence {} + +#[vtable] +impl FenceOps for Fence { + const USE_64BIT_SEQNO: bool = false; + + fn get_driver_name<'a>(self: &'a FenceObject) -> &'a CStr { + c_str!("vgem") + } + + fn get_timeline_name<'a>(self: &'a FenceObject) -> &'a CStr { + c_str!("unbound") + } + + fn fence_value_str(self: &FenceObject, output: &mut dyn Write) { + let _ = output.write_fmt(format_args!("{}", self.seqno())); + } + + fn timeline_value_str(self: &FenceObject, output: &mut dyn Write) { + let value = if self.is_signaled() { self.seqno() } else { 0 }; + let _ = output.write_fmt(format_args!("{}", value)); + } +} + +pub(crate) struct VgemFence { + fence: Arc>, +} + +impl VgemFence { + pub(crate) fn create() -> Result { + let fence_ctx = FenceContexts::new(1, QUEUE_NAME, &QUEUE_CLASS_KEY)?; + let fence = Arc::try_new(fence_ctx.new_fence(0, Fence {})?)?; + + Ok(Self { fence }) + } +} + +impl Deref for VgemFence { + type Target = UniqueFence; + + fn deref(&self) -> &Self::Target { + &self.fence + } +} diff --git a/drivers/gpu/drm/rustgem/file.rs b/drivers/gpu/drm/rustgem/file.rs new file mode 100644 index 000000000000..2552c7892b0e --- /dev/null +++ b/drivers/gpu/drm/rustgem/file.rs @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: MIT + +use crate::fence::VgemFence; +use crate::gem::DriverObject; +use crate::{VgemDevice, VgemDriver}; +use core::ops::Deref; +use kernel::dma_fence::RawDmaFence; +use kernel::drm::gem::BaseObject; +use kernel::prelude::*; +use kernel::{bindings, drm, drm::gem::shmem, xarray}; + +pub(crate) struct File { + fences: xarray::XArray>>, +} + +/// Convenience type alias for our DRM `File` type. +pub(crate) type DrmFile = drm::file::File; + +impl drm::file::DriverFile for File { + type Driver = VgemDriver; + + fn open(_device: &VgemDevice) -> Result> { + Ok(Box::try_new(Self { + fences: xarray::XArray::new(xarray::flags::ALLOC1)?, + })?) + } +} + +impl File { + /// vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH): + /// + /// Create and attach a fence to the vGEM handle. This fence is then exposed + /// via the dma-buf reservation object and visible to consumers of the exported + /// dma-buf. + /// + /// If the vGEM handle does not exist, attach returns -ENOENT. + /// + pub(crate) fn attach( + _device: &VgemDevice, + data: &mut bindings::drm_vgem_fence_attach, + file: &DrmFile, + ) -> Result { + if (data.flags & !bindings::VGEM_FENCE_WRITE) != 0 { + return Err(EINVAL); + } + + if data.pad != 0 { + return Err(EINVAL); + } + + let obj = shmem::Object::::lookup_handle(file, data.handle)?; + + let fence = VgemFence::create()?; + + // Check for a conflicting fence + let resv = obj.resv(); + let usage = resv.usage_rw(data.flags & bindings::VGEM_FENCE_WRITE != 0); + if !resv.test_signaled(usage) { + fence.signal()?; + return Err(EBUSY); + } + + let usage = if (data.flags & bindings::VGEM_FENCE_WRITE) != 0 { + bindings::dma_resv_usage_DMA_RESV_USAGE_WRITE + } else { + bindings::dma_resv_usage_DMA_RESV_USAGE_READ + }; + + // Expose the fence via the dma-buf + if resv.add_fences(fence.deref(), 1, usage).is_ok() { + // Record the fence in our xarray for later signaling + if let Ok(id) = file.fences.alloc(Some(Box::try_new(Some(fence))?)) { + data.out_fence = id as u32 + } + } else { + fence.signal()?; + } + + Ok(0) + } + + /// vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL): + /// + /// Signal and consume a fence earlier attached to a vGEM handle using + /// attach (DRM_IOCTL_VGEM_FENCE_ATTACH). + /// + /// Signaling a fence indicates to all consumers of the dma-buf that the + /// client has completed the operation associated with the fence, and that the + /// buffer is then ready for consumption. + /// + /// If the fence does not exist (or has already been signaled by the client), + /// signal returns -ENOENT. + /// + pub(crate) fn signal( + _device: &VgemDevice, + data: &mut bindings::drm_vgem_fence_signal, + file: &DrmFile, + ) -> Result { + if data.flags != 0 { + return Err(EINVAL); + } + + let fence = file + .fences + .replace(data.fence as usize, Box::try_new(None)?); + + let fence = match fence { + Err(ret) => { + return Err(ret); + } + Ok(None) => { + return Err(ENOENT); + } + Ok(fence) => { + let fence = fence.unwrap().unwrap(); + + if fence.is_signaled() { + return Err(ETIMEDOUT); + } + + fence + } + }; + + fence.signal()?; + Ok(0) + } +} diff --git a/drivers/gpu/drm/rustgem/gem.rs b/drivers/gpu/drm/rustgem/gem.rs new file mode 100644 index 000000000000..e20bfe4ee0cf --- /dev/null +++ b/drivers/gpu/drm/rustgem/gem.rs @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT + +use kernel::{ + drm::{gem, gem::shmem}, + error::Result, +}; + +use crate::file::DrmFile; +use crate::{VgemDevice, VgemDriver}; + +/// Represents the inner data of a GEM object for this driver. +pub(crate) struct DriverObject {} + +/// Type alias for the shmem GEM object type for this driver. +pub(crate) type Object = shmem::Object; + +impl gem::BaseDriverObject for DriverObject { + /// Callback to create the inner data of a GEM object + fn new(_dev: &VgemDevice, _size: usize) -> Result { + Ok(Self {}) + } + + /// Callback to drop all mappings for a GEM object owned by a given `File` + fn close(_obj: &Object, _file: &DrmFile) {} +} + +impl shmem::DriverObject for DriverObject { + type Driver = VgemDriver; + + const MAP_WC: bool = true; +} diff --git a/drivers/gpu/drm/rustgem/vgem.rs b/drivers/gpu/drm/rustgem/vgem.rs new file mode 100644 index 000000000000..c2fc55bb39bd --- /dev/null +++ b/drivers/gpu/drm/rustgem/vgem.rs @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: MIT + +//! Driver for a Virtual GEM service. + +use kernel::driver::DeviceRemoval; +use kernel::macros::vtable; +use kernel::{ + c_str, device, drm, + drm::{drv, ioctl}, + error::Result, + platform, + prelude::*, + sync::Arc, +}; + +mod fence; +mod file; +mod gem; + +/// Driver metadata +const INFO: drv::DriverInfo = drv::DriverInfo { + major: 1, + minor: 0, + patchlevel: 0, + name: c_str!("vgem"), + desc: c_str!("Virtual GEM provider"), + date: c_str!("20230201"), +}; + +struct Vgem { + data: Arc, + _resource: device::Resource, + _pdev: platform::Device, +} + +/// Empty struct representing this driver. +pub(crate) struct VgemDriver; + +/// Convenience type alias for the DRM device type for this driver. +pub(crate) type VgemDevice = kernel::drm::device::Device; + +///// Convenience type alias for the `device::Data` type for this driver. +type DeviceData = device::Data, (), ()>; + +#[vtable] +impl drv::Driver for VgemDriver { + /// Our `DeviceData` type, reference-counted + type Data = Arc; + /// Our `File` type. + type File = file::File; + /// Our `Object` type. + type Object = gem::Object; + + const INFO: drv::DriverInfo = INFO; + const FEATURES: u32 = drv::FEAT_GEM | drv::FEAT_RENDER; + + kernel::declare_drm_ioctls! { + (VGEM_FENCE_ATTACH, drm_vgem_fence_attach, ioctl::RENDER_ALLOW, file::File::attach), + (VGEM_FENCE_SIGNAL, drm_vgem_fence_signal, ioctl::RENDER_ALLOW, file::File::signal), + } +} + +impl kernel::Module for Vgem { + fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result { + let mut pdev = platform::Device::register(c_str!("vgem"), -1)?; + let dev = device::Device::from_dev(&pdev); + + let resource = dev.open_group(core::ptr::null_mut() as *mut core::ffi::c_void)?; + + pdev.coerse_dma_masks(u64::MAX)?; + + let reg = drm::drv::Registration::::new(&dev)?; + + let data = kernel::new_device_data!(reg, (), (), "Vgem::Registrations")?; + + let data = Arc::::from(data); + + kernel::drm_device_register!( + data.registrations().ok_or(ENXIO)?.as_pinned_mut(), + data.clone(), + 0 + )?; + + Ok(Vgem { + _pdev: pdev, + _resource: resource, + data, + }) + } +} + +impl Drop for Vgem { + fn drop(&mut self) { + self.data.device_remove(); + } +} + +module! { + type: Vgem, + name: "vgem", + author: "Maíra Canal", + description: "Virtual GEM provider", + license: "GPL", +} diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 1bf6d762d36e..dc44d3c3b9ad 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -37,6 +37,7 @@ #include #include #include +#include /* `bindgen` gets confused at certain things. */ const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL; From patchwork Fri Mar 17 12:12:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179008 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F0951C7618B for ; Fri, 17 Mar 2023 12:14:03 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2E58310EF02; Fri, 17 Mar 2023 12:14:03 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0BE8B10EF02 for ; Fri, 17 Mar 2023 12:14:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=YVEnRR/qQQunfEktPJrIHOIoV5MpZURwVPi1B2rMWCI=; b=OPvgHMMGoOZU3zXOpaM4pBey5G lS1U4yJN8OV/Lq66djgyDIp4Lkv8Znz98tj0VywYzEv8cU4RyFS/MW1AcRMuQrF8OlrnvOse4pldd o/YBs7Yh2HR/Ms2H5X4+OJSplA1MGwBD3lfKeo0sDZKJv4RbetINeL1RWYYbbmaU645frSoNVtS0f 4lLRn91LffP/PWa4pFx+prp6DVAHflGevnAC9FXcmdUIpE1JeVTT5yku/ImTE38/fhTNN75QYKPNs /3I4jXkh8YBX+BoVM1bcdVl/tLB5sqkXTynA9/P3/nySvwqOAJ3GLpn5DcKr9d5CMESy04whhvk7A a091Rouw==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xq-00FrGz-Mz; Fri, 17 Mar 2023 13:13:43 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 8/9] drm/rustgem: implement timeout to prevent hangs Date: Fri, 17 Mar 2023 09:12:12 -0300 Message-Id: <20230317121213.93991-9-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" In order to prevent possible hangs, if the fence is not signaled for more than 10 seconds, force the fence to expire by being signaled. Signed-off-by: Maíra Canal --- drivers/gpu/drm/rustgem/fence.rs | 31 +++++++++++++++++++++++++++++-- drivers/gpu/drm/rustgem/file.rs | 7 +++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/rustgem/fence.rs b/drivers/gpu/drm/rustgem/fence.rs index 9ef1399548e2..eb1d7faa7e8e 100644 --- a/drivers/gpu/drm/rustgem/fence.rs +++ b/drivers/gpu/drm/rustgem/fence.rs @@ -2,10 +2,13 @@ use core::fmt::Write; use core::ops::Deref; -use kernel::c_str; +use core::time::Duration; use kernel::dma_fence::*; use kernel::prelude::*; use kernel::sync::Arc; +use kernel::time::timer::*; +use kernel::time::*; +use kernel::{bindings, c_str, timer_init}; static QUEUE_NAME: &CStr = c_str!("vgem_fence"); static QUEUE_CLASS_KEY: kernel::sync::LockClassKey = kernel::sync::LockClassKey::new(); @@ -36,6 +39,7 @@ impl FenceOps for Fence { pub(crate) struct VgemFence { fence: Arc>, + _timer: Box Result + Sync>>>, } impl VgemFence { @@ -43,7 +47,30 @@ impl VgemFence { let fence_ctx = FenceContexts::new(1, QUEUE_NAME, &QUEUE_CLASS_KEY)?; let fence = Arc::try_new(fence_ctx.new_fence(0, Fence {})?)?; - Ok(Self { fence }) + // SAFETY: The caller calls [`FnTimer::init_timer`] before using the timer. + let t = Box::try_new(unsafe { + FnTimer::new(Box::try_new({ + let fence = fence.clone(); + move || { + let _ = fence.signal(); + Ok(Next::Done) + } + })? as Box<_>) + })?; + + // SAFETY: As FnTimer is inside a Box, it won't be moved. + let ptr = unsafe { core::pin::Pin::new_unchecked(&*t) }; + + timer_init!(ptr, 0, "vgem_timer"); + + // SAFETY: Duration.as_millis() returns a valid total number of whole milliseconds. + let timeout = + unsafe { bindings::msecs_to_jiffies(Duration::from_secs(10).as_millis().try_into()?) }; + + // We force the fence to expire within 10s to prevent driver hangs + ptr.raw_timer().schedule_at(jiffies_later(timeout)); + + Ok(Self { fence, _timer: t }) } } diff --git a/drivers/gpu/drm/rustgem/file.rs b/drivers/gpu/drm/rustgem/file.rs index 2552c7892b0e..a3714e8ca206 100644 --- a/drivers/gpu/drm/rustgem/file.rs +++ b/drivers/gpu/drm/rustgem/file.rs @@ -33,6 +33,10 @@ impl File { /// via the dma-buf reservation object and visible to consumers of the exported /// dma-buf. /// + /// This returns the handle for the new fence that must be signaled within 10 + /// seconds (or otherwise it will automatically expire). See + /// signal (DRM_IOCTL_VGEM_FENCE_SIGNAL). + /// /// If the vGEM handle does not exist, attach returns -ENOENT. /// pub(crate) fn attach( @@ -84,6 +88,9 @@ impl File { /// Signal and consume a fence earlier attached to a vGEM handle using /// attach (DRM_IOCTL_VGEM_FENCE_ATTACH). /// + /// All fences must be signaled within 10s of attachment or otherwise they + /// will automatically expire (and signal returns -ETIMEDOUT). + /// /// Signaling a fence indicates to all consumers of the dma-buf that the /// client has completed the operation associated with the fence, and that the /// buffer is then ready for consumption. From patchwork Fri Mar 17 12:12:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ma=C3=ADra_Canal?= X-Patchwork-Id: 13179009 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8BF2DC76195 for ; Fri, 17 Mar 2023 12:14:06 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F341510EF07; Fri, 17 Mar 2023 12:14:05 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id AD9D710EF07 for ; Fri, 17 Mar 2023 12:14:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=3MBGRyVcfZD+2SmE5BBIh6mlfln4sjziRqqN19lxLOA=; b=FTtyXD8St5MctcW02ez1YiXt8o P+y/eYpxKR51E0UxTHtaV8HV8cjjbly+jKpJ5+GePpclczZI4GunG4jT8Q2M9Wl/YLDQVK+k1a+Bj YrKAuUn15K95vTltUzUlqbY1Xy9l03bgYlAA5qAQo/x2TESecak2b+4SHIJoeymo5m60t9Veq+f5K WN64aM0dvksfpxGuJuISAewHmlaReZImsWxyELVIc3Q3OiLs1JAkeoTuZ9xVKjkHI2WAQKCPV8EXV /aKqKazL8v9v/pGBKj7LFAStm2sOTNo3mgmDsJjuyy80T6+OIv+HQuJnDOixUvTwhDIoMLhXSFLfS X81v2rbw==; Received: from [177.34.168.16] (helo=bowie..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1pd8xw-00FrGz-JZ; Fri, 17 Mar 2023 13:13:49 +0100 From: =?utf-8?q?Ma=C3=ADra_Canal?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Asahi Lina , Faith Ekstrand Subject: [RFC PATCH 9/9] drm/rustgem: create dummy IOCTL with number 0x00 Date: Fri, 17 Mar 2023 09:12:13 -0300 Message-Id: <20230317121213.93991-10-mcanal@igalia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230317121213.93991-1-mcanal@igalia.com> References: <20230317121213.93991-1-mcanal@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Melissa Wen , =?utf-8?q?Ma=C3=ADra_Canal?= , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" In order to declare IOCTLs with the current Rust abstractions, we use the kernel::declare_drm_ioctls! macro. This macro considers that the IOCTLs are in the right order and there are no gaps. This isn't the case for vgem, which start the IOCTLs with 0x01, instead of 0x00. With the intention to use the kernel::declare_drm_ioctls! macro, create a dummy IOCTL with number 0x00, that returns EINVAL. Signed-off-by: Maíra Canal --- drivers/gpu/drm/rustgem/file.rs | 8 ++++++++ drivers/gpu/drm/rustgem/vgem.rs | 1 + include/uapi/drm/vgem_drm.h | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/drivers/gpu/drm/rustgem/file.rs b/drivers/gpu/drm/rustgem/file.rs index a3714e8ca206..f26b74204361 100644 --- a/drivers/gpu/drm/rustgem/file.rs +++ b/drivers/gpu/drm/rustgem/file.rs @@ -27,6 +27,14 @@ impl drm::file::DriverFile for File { } impl File { + pub(crate) fn dummy( + _device: &VgemDevice, + _data: &mut bindings::drm_vgem_dummy, + _file: &DrmFile, + ) -> Result { + Err(EINVAL) + } + /// vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH): /// /// Create and attach a fence to the vGEM handle. This fence is then exposed diff --git a/drivers/gpu/drm/rustgem/vgem.rs b/drivers/gpu/drm/rustgem/vgem.rs index c2fc55bb39bd..64e8f1c2cbca 100644 --- a/drivers/gpu/drm/rustgem/vgem.rs +++ b/drivers/gpu/drm/rustgem/vgem.rs @@ -55,6 +55,7 @@ impl drv::Driver for VgemDriver { const FEATURES: u32 = drv::FEAT_GEM | drv::FEAT_RENDER; kernel::declare_drm_ioctls! { + (VGEM_DUMMY, drm_vgem_dummy, ioctl::RENDER_ALLOW, file::File::dummy), (VGEM_FENCE_ATTACH, drm_vgem_fence_attach, ioctl::RENDER_ALLOW, file::File::attach), (VGEM_FENCE_SIGNAL, drm_vgem_fence_signal, ioctl::RENDER_ALLOW, file::File::signal), } diff --git a/include/uapi/drm/vgem_drm.h b/include/uapi/drm/vgem_drm.h index 53ee3af0b25a..1348f8e819ed 100644 --- a/include/uapi/drm/vgem_drm.h +++ b/include/uapi/drm/vgem_drm.h @@ -36,9 +36,12 @@ extern "C" { /* Please note that modifications to all structs defined here are * subject to backwards-compatibility constraints. */ +#define DRM_VGEM_DUMMY 0x0 #define DRM_VGEM_FENCE_ATTACH 0x1 #define DRM_VGEM_FENCE_SIGNAL 0x2 +struct drm_vgem_dummy { }; + struct drm_vgem_fence_attach { __u32 handle; __u32 flags; @@ -54,6 +57,7 @@ struct drm_vgem_fence_signal { /* Note: this is an enum so that it can be resolved by Rust bindgen. */ enum { + DRM_IOCTL_VGEM_DUMMY = DRM_IOW(DRM_COMMAND_BASE + DRM_VGEM_DUMMY, struct drm_vgem_dummy), DRM_IOCTL_VGEM_FENCE_ATTACH = DRM_IOWR(DRM_COMMAND_BASE + DRM_VGEM_FENCE_ATTACH, struct drm_vgem_fence_attach), DRM_IOCTL_VGEM_FENCE_SIGNAL = DRM_IOW(DRM_COMMAND_BASE + DRM_VGEM_FENCE_SIGNAL, struct drm_vgem_fence_signal), };