From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E2EB28E589 for ; Tue, 20 May 2025 20:36:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747773387; cv=none; b=il0BiE6BltKX8oPPc+o2YI25iFUjnMzYAWpKrXcUzA/fQ9+OeyiWEdw3dub6k2V3UPtjSbgyJRiY6vysxf7vf13FA6RyQgdtAauzdD9j8QV8hJERxZ4vzi6iqvfUdV3bz+7aaKOfkTAwZb2MmA3lPgJ8jFhoGrqf43fxCmUTLNA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747773387; c=relaxed/simple; bh=KjdYEOEajHA7LELVRkRgK7y2TfZ0pdc/8gj4sas3Y70=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BncnIKmE/kvdNa1s/7GX0KSKH5VXeG4xIiNxhxRtpcjxm/Jt3ikqgjnL0xGGzM1tDsT4QiVqELVdvtRS6tj9pska/fTOMopdbgTzlL26b8DAWkpfSgvflyoRRyFr08arsXt+jTcZgg4pI5QfJd2IXHguwos8zUeUEwkX2cinwuY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QTUNENAe; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QTUNENAe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747773384; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/FiP53qIGqK3kQ8uYRbm+tkR4/PouMqDuk4sdgE46BE=; b=QTUNENAeUxwCjR02T4ZzwXQTPYFLeODY32Vl9atEcQ82nUcw5iU8jW62IYmaI0DPHINE+k TwgJFn8sOawbGO2P3jgPI6LCHm5fpy7d9kT/rICnGrRQU7g7UyvWbRvNcaT2XEizRLFXjk iUgVXgUztKf5EDeaGb2DvheFrRMl1nI= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-387-ewVPIDvDNzC8IdKgTSlCag-1; Tue, 20 May 2025 16:36:22 -0400 X-MC-Unique: ewVPIDvDNzC8IdKgTSlCag-1 X-Mimecast-MFC-AGG-ID: ewVPIDvDNzC8IdKgTSlCag_1747773379 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6DD591800366; Tue, 20 May 2025 20:36:19 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.64.128]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AC4CD19560AD; Tue, 20 May 2025 20:36:14 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Cc: Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Daniel Almeida , Asahi Lina , Alyssa Rosenzweig , nouveau@lists.freedesktop.org (open list:DRM DRIVER FOR NVIDIA GPUS [RUST]) Subject: [PATCH v4 4/4] rust: drm: gem: Support driver-private GEM object types Date: Tue, 20 May 2025 16:35:30 -0400 Message-ID: <20250520203542.1391548-5-lyude@redhat.com> In-Reply-To: <20250520203542.1391548-1-lyude@redhat.com> References: <20250520203542.1391548-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 One of the original intents with the gem bindings was that drivers could specify additional gem implementations, in order to enable for driver private gem objects. This wasn't really possible however, as up until now our GEM bindings have always assumed that the only GEM object we would run into was driver::Driver::Object - meaning that implementing another GEM object type would result in all of the BaseDriverObject callbacks assuming the wrong type. This is a pretty easy fix though, all we need to do is specify a BaseDriverObject in driver::Driver instead of an AllocImpl, and then add an associated type for AllocImpl in BaseDriverObject. That way each BaseDriverObject has its own AllocImpl allowing it to know which type to provide in BaseDriverObject callbacks, and driver::Driver can simply go through the BaseDriverObject to its AllocImpl type in order to get access to ALLOC_OPS. So, let's do this and update Nova for these changes. Signed-off-by: Lyude Paul --- V4: * Update trait bounds. This looks gnarlier then it is: Self: AllocImpl, <-- Get the driver for this GEM object D: drm::Driver, <-- Get the driver's Object, File impl F: drm::file::DriverFile, O: BaseDriverObject, <-- Make sure we're the driver's main GEM object impl. (don't worry, the compiler can always figure out what D, F, O are) * Also, rename the commit. I realized I should be clearer about what this does so people can stop me if this isn't what was meant by private gem object implementations :). Signed-off-by: Lyude Paul --- drivers/gpu/drm/nova/driver.rs | 4 ++-- drivers/gpu/drm/nova/gem.rs | 1 + rust/kernel/drm/device.rs | 17 ++++++++++------- rust/kernel/drm/driver.rs | 2 +- rust/kernel/drm/gem/mod.rs | 21 +++++++++++++-------- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/drivers/gpu/drm/nova/driver.rs b/drivers/gpu/drm/nova/driver.rs index b28b2e05cc156..58e534cf3ed39 100644 --- a/drivers/gpu/drm/nova/driver.rs +++ b/drivers/gpu/drm/nova/driver.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 -use kernel::{auxiliary, c_str, device::Core, drm, drm::gem, drm::ioctl, prelude::*, types::ARef}; +use kernel::{auxiliary, c_str, device::Core, drm, drm::ioctl, prelude::*, types::ARef}; use crate::file::File; use crate::gem::NovaObject; @@ -57,7 +57,7 @@ fn probe(adev: &auxiliary::Device, _info: &Self::IdInfo) -> Result; + type Object = NovaObject; const INFO: drm::DriverInfo = INFO; diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs index 68f93c9675611..a3024922f0d90 100644 --- a/drivers/gpu/drm/nova/gem.rs +++ b/drivers/gpu/drm/nova/gem.rs @@ -18,6 +18,7 @@ pub(crate) struct NovaObject {} impl gem::BaseDriverObject for NovaObject { type Driver = NovaDriver; + type Object = gem::Object; fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit { try_pin_init!(NovaObject {}) diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index 74c9a3dd719e3..6fc6995be637d 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -60,6 +60,9 @@ pub struct Device { data: T::Data, } +/// A type alias for referring to the [`AllocImpl`] implementation for a DRM driver. +type DriverAllocImpl = <::Object as drm::gem::BaseDriverObject>::Object; + impl Device { const VTABLE: bindings::drm_driver = drm_legacy_fields! { load: None, @@ -70,13 +73,13 @@ impl Device { master_set: None, master_drop: None, debugfs_init: None, - gem_create_object: T::Object::ALLOC_OPS.gem_create_object, - prime_handle_to_fd: T::Object::ALLOC_OPS.prime_handle_to_fd, - prime_fd_to_handle: T::Object::ALLOC_OPS.prime_fd_to_handle, - gem_prime_import: T::Object::ALLOC_OPS.gem_prime_import, - gem_prime_import_sg_table: T::Object::ALLOC_OPS.gem_prime_import_sg_table, - dumb_create: T::Object::ALLOC_OPS.dumb_create, - dumb_map_offset: T::Object::ALLOC_OPS.dumb_map_offset, + gem_create_object: DriverAllocImpl::::ALLOC_OPS.gem_create_object, + prime_handle_to_fd: DriverAllocImpl::::ALLOC_OPS.prime_handle_to_fd, + prime_fd_to_handle: DriverAllocImpl::::ALLOC_OPS.prime_fd_to_handle, + gem_prime_import: DriverAllocImpl::::ALLOC_OPS.gem_prime_import, + gem_prime_import_sg_table: DriverAllocImpl::::ALLOC_OPS.gem_prime_import_sg_table, + dumb_create: DriverAllocImpl::::ALLOC_OPS.dumb_create, + dumb_map_offset: DriverAllocImpl::::ALLOC_OPS.dumb_map_offset, show_fdinfo: None, fbdev_probe: None, diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index 2be2a2d318e03..a77747edac80e 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -106,7 +106,7 @@ pub trait Driver { type Data: Sync + Send; /// The type used to manage memory for this driver. - type Object: AllocImpl; + type Object: drm::gem::BaseDriverObject; /// The type used to represent a DRM File (client) type File: drm::file::DriverFile; diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index c459829ce5a5d..992e098d0a3e2 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -26,16 +26,19 @@ pub trait BaseDriverObject: Sync + Send + Sized { /// Parent `Driver` for this object. type Driver: drm::Driver; + /// The GEM object type that will be passed to various callbacks. + type Object: AllocImpl; + /// Create a new driver data object for a GEM object of a given size. fn new(dev: &drm::Device, size: usize) -> impl PinInit; /// Open a new handle to an existing object, associated with a File. - fn open(_obj: &::Object, _file: &DriverFile) -> Result { + fn open(_obj: &Self::Object, _file: &DriverFile) -> Result { Ok(()) } /// Close a handle to an existing object, associated with a File. - fn close(_obj: &::Object, _file: &DriverFile) {} + fn close(_obj: &Self::Object, _file: &DriverFile) {} } /// Trait that represents a GEM object subtype @@ -83,7 +86,7 @@ extern "C" fn open_callback( // SAFETY: `open_callback` is specified in the AllocOps structure for `DriverObject`, // ensuring that `raw_obj` is contained within a `DriverObject` - let obj = unsafe { <::Object as IntoGEMObject>::as_ref(raw_obj) }; + let obj = unsafe { T::Object::as_ref(raw_obj) }; match T::open(obj, file) { Err(e) => e.to_errno(), @@ -100,7 +103,7 @@ extern "C" fn close_callback( // SAFETY: `close_callback` is specified in the AllocOps structure for `Object`, ensuring // that `raw_obj` is indeed contained within a `Object`. - let obj = unsafe { <::Object as IntoGEMObject>::as_ref(raw_obj) }; + let obj = unsafe { T::Object::as_ref(raw_obj) }; T::close(obj, file); } @@ -127,11 +130,12 @@ fn size(&self) -> usize { /// Creates a new handle for the object associated with a given `File` /// (or returns an existing one). - fn create_handle(&self, file: &drm::File) -> Result + fn create_handle(&self, file: &drm::File) -> Result where Self: AllocImpl, - D: drm::Driver, + D: drm::Driver, F: drm::file::DriverFile, + O: BaseDriverObject, { let mut handle: u32 = 0; // SAFETY: The arguments are all valid per the type invariants. @@ -142,11 +146,12 @@ fn create_handle(&self, file: &drm::File) -> Result } /// Looks up an object by its handle for a given `File`. - fn lookup_handle(file: &drm::File, handle: u32) -> Result> + fn lookup_handle(file: &drm::File, handle: u32) -> Result> where Self: AllocImpl, - D: drm::Driver, + D: drm::Driver, F: drm::file::DriverFile, + O: BaseDriverObject, { // SAFETY: The arguments are all valid per the type invariants. let ptr = unsafe { bindings::drm_gem_object_lookup(file.as_raw().cast(), handle) }; -- 2.49.0