From patchwork Wed Mar 5 22:59:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lyude Paul X-Patchwork-Id: 14003642 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 DC67DC28B25 for ; Wed, 5 Mar 2025 23:06:52 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2985A10E853; Wed, 5 Mar 2025 23:06:52 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="AAXJuDb0"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 96EF010E84F for ; Wed, 5 Mar 2025 23:06:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741216009; 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=FiII/UFZj0PJa3aqHS4adfK6RLGvexStwXEtTIs9AeA=; b=AAXJuDb0xn5cnkZ9lXlvrNX3xW839UpDifTcL/9ZHzrPtZF1WrOQ7SAHuaKlnY7zfecCen UDuhn1B/NC91bWMGjBshHIOoHZia1Am488Q4901SSsX5fwUUvU8iTV83flO26KBCuWeWFp Bx+j5viIl8/irDFP+dgxWEiD7tBSLHs= 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-354-Xad8I59GMX2nR9Rn7DYrPA-1; Wed, 05 Mar 2025 18:06:46 -0500 X-MC-Unique: Xad8I59GMX2nR9Rn7DYrPA-1 X-Mimecast-MFC-AGG-ID: Xad8I59GMX2nR9Rn7DYrPA_1741216004 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 F0DCA1800361; Wed, 5 Mar 2025 23:06:43 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.81]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ECAF5300019E; Wed, 5 Mar 2025 23:06:39 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org Cc: Danilo Krummrich , mcanal@igalia.com, Alice Ryhl , Maxime Ripard , Simona Vetter , Daniel Almeida , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org (open list) Subject: [RFC v3 15/33] rust: drm/kms: Add OpaqueCrtc and OpaqueCrtcState Date: Wed, 5 Mar 2025 17:59:31 -0500 Message-ID: <20250305230406.567126-16-lyude@redhat.com> In-Reply-To: <20250305230406.567126-1-lyude@redhat.com> References: <20250305230406.567126-1-lyude@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This is the same thing as OpaqueConnector and OpaqueConnectorState, but for CRTCs now. Signed-off-by: Lyude Paul --- V3: * Add safety comment for implementation of ModeObject * Add safety comments to AsRawCrtc implementation * Implement try_from_opaque() and from_opaque() using a macro Signed-off-by: Lyude Paul --- rust/kernel/drm/kms/crtc.rs | 167 +++++++++++++++++++++++++++++++++++- 1 file changed, 166 insertions(+), 1 deletion(-) diff --git a/rust/kernel/drm/kms/crtc.rs b/rust/kernel/drm/kms/crtc.rs index 9950b09754072..bcdd452ff7b10 100644 --- a/rust/kernel/drm/kms/crtc.rs +++ b/rust/kernel/drm/kms/crtc.rs @@ -4,7 +4,9 @@ //! //! C header: [`include/drm/drm_crtc.h`](srctree/include/drm/drm_crtc.h) -use super::{plane::*, KmsDriver, ModeObject, StaticModeObject, UnregisteredKmsDevice}; +use super::{ + plane::*, KmsDriver, ModeObject, ModeObjectVtable, StaticModeObject, UnregisteredKmsDevice, +}; use crate::{ alloc::KBox, bindings, @@ -185,6 +187,25 @@ fn raw_mode_obj(&self) -> *mut bindings::drm_mode_object { // SAFETY: CRTCs are non-refcounted modesetting objects unsafe impl StaticModeObject for Crtc {} +// SAFETY: `funcs` is initialized when the crtc is allocated +unsafe impl ModeObjectVtable for Crtc { + type Vtable = bindings::drm_crtc_funcs; + + fn vtable(&self) -> *const Self::Vtable { + // SAFETY: `as_raw()` always returns a valid pointer to a CRTC + unsafe { *self.as_raw() }.funcs + } +} + +impl Crtc { + super::impl_from_opaque_mode_obj! { + fn <'a, D>(&'a OpaqueCrtc) -> &'a Self; + use + T as DriverCrtc, + D as KmsDriver + } +} + /// A [`Crtc`] that has not yet been registered with userspace. /// /// KMS registration is single-threaded, so this object is not thread-safe. @@ -361,6 +382,86 @@ fn mask(&self) -> u32 { } impl RawCrtc for T {} +/// A [`struct drm_crtc`] without a known [`DriverCrtc`] implementation. +/// +/// This is mainly for situations where our bindings can't infer the [`DriverCrtc`] implementation +/// for a [`struct drm_crtc`] automatically. It is identical to [`Crtc`], except that it does not +/// provide access to the driver's private data. +/// +/// It may be upcasted to a full [`Crtc`] using [`Crtc::from_opaque`] or +/// [`Crtc::try_from_opaque`]. +/// +/// # Invariants +/// +/// - `crtc` is initialized for as long as this object is made available to users. +/// - The data layout of this structure is equivalent to [`struct drm_crtc`]. +/// +/// [`struct drm_crtc`]: srctree/include/drm/drm_crtc.h +#[repr(transparent)] +pub struct OpaqueCrtc { + crtc: Opaque, + _p: PhantomData, +} + +impl Sealed for OpaqueCrtc {} + +// SAFETY: +// - Via our type variants our data layout is identical to `drm_crtc` +// - Since we don't expose `OpaqueCrtc` to users before it has been initialized, this and our data +// layout ensure that `as_raw()` always returns a valid pointer to a `drm_crtc`. +unsafe impl AsRawCrtc for OpaqueCrtc { + fn as_raw(&self) -> *mut bindings::drm_crtc { + self.crtc.get() + } + + unsafe fn from_raw<'a>(ptr: *mut bindings::drm_crtc) -> &'a Self { + // SAFETY: Our data layout starts with `bindings::drm_crtc` + unsafe { &*ptr.cast() } + } +} + +// SAFETY: We only expose this object to users directly after KmsDriver::create_objects has been +// called. +unsafe impl ModesettableCrtc for OpaqueCrtc { + type State = OpaqueCrtcState; +} + +// SAFETY: We don't expose OpaqueCrtc to users before `base` is initialized in Crtc::::new(), +// so `raw_mode_obj` always returns a valid pointer to a bindings::drm_mode_object. +unsafe impl ModeObject for OpaqueCrtc { + type Driver = T; + + fn drm_dev(&self) -> &Device { + // SAFETY: The parent device for a DRM connector will never outlive the connector, and this + // pointer is invariant through the lifetime of the connector + unsafe { Device::borrow((*self.as_raw()).dev) } + } + + fn raw_mode_obj(&self) -> *mut bindings::drm_mode_object { + // SAFETY: We don't expose DRM connectors to users before `base` is initialized + unsafe { addr_of_mut!((*self.as_raw()).base) } + } +} + +// SAFETY: CRTCs are non-refcounted modesetting objects +unsafe impl StaticModeObject for OpaqueCrtc {} + +// SAFETY: Our CRTC interface is guaranteed to be thread-safe +unsafe impl Send for OpaqueCrtc {} + +// SAFETY: Our CRTC interface is guaranteed to be thread-safe +unsafe impl Sync for OpaqueCrtc {} + +// SAFETY: `funcs` is initialized when the CRTC is allocated +unsafe impl ModeObjectVtable for OpaqueCrtc { + type Vtable = bindings::drm_crtc_funcs; + + fn vtable(&self) -> *const Self::Vtable { + // SAFETY: `as_raw()` always returns a valid pointer to a crtc + unsafe { (*self.as_raw()).funcs } + } +} + unsafe impl Zeroable for bindings::drm_crtc_state {} impl Sealed for CrtcState {} @@ -429,6 +530,26 @@ fn deref_mut(&mut self) -> &mut Self::Target { } } +// SAFETY: Shares the safety guarantee of Crtc's ModeObjectVtable impl +unsafe impl ModeObjectVtable for CrtcState { + type Vtable = bindings::drm_crtc_funcs; + + fn vtable(&self) -> *const Self::Vtable { + self.crtc().vtable() + } +} + +impl CrtcState { + super::impl_from_opaque_mode_obj! { + fn <'a, D, C>(&'a OpaqueCrtcState) -> &'a Self + where + T: DriverCrtcState; + use + C as DriverCrtc, + D as KmsDriver + } +} + /// A trait implemented by any type which can produce a reference to a [`struct drm_crtc_state`]. /// /// This is implemented internally by DRM. @@ -502,6 +623,50 @@ unsafe fn from_raw<'a>(ptr: *const bindings::drm_crtc_state) -> &'a Self { } } +/// A [`struct drm_crtc_state`] without a known [`DriverCrtcState`] implementation. +/// +/// This is mainly for situations where our bindings can't infer the [`DriverCrtcState`] +/// implementation for a [`struct drm_crtc_state`] automatically. It is identical to [`Crtc`], +/// except that it does not provide access to the driver's private data. +/// +/// # Invariants +/// +/// - `state` is initialized for as long as this object is exposed to users. +/// - The data layout of this type is identical to [`struct drm_crtc_state`]. +/// +/// [`struct drm_crtc_state`]: srctree/include/drm/drm_crtc.h +#[repr(transparent)] +pub struct OpaqueCrtcState { + state: Opaque, + _p: PhantomData, +} + +impl AsRawCrtcState for OpaqueCrtcState { + type Crtc = OpaqueCrtc; +} + +impl private::AsRawCrtcState for OpaqueCrtcState { + fn as_raw(&self) -> *mut bindings::drm_crtc_state { + self.state.get() + } +} + +impl FromRawCrtcState for OpaqueCrtcState { + unsafe fn from_raw<'a>(ptr: *const bindings::drm_crtc_state) -> &'a Self { + // SAFETY: Our data layout is identical to `bindings::drm_crtc_state` + unsafe { &*(ptr.cast()) } + } +} + +// SAFETY: Shares the safety guarantees of OpaqueCrtc's ModeObjectVtable impl +unsafe impl ModeObjectVtable for OpaqueCrtcState { + type Vtable = bindings::drm_crtc_funcs; + + fn vtable(&self) -> *const Self::Vtable { + self.crtc().vtable() + } +} + unsafe extern "C" fn crtc_destroy_callback(crtc: *mut bindings::drm_crtc) { // SAFETY: DRM guarantees that `crtc` points to a valid initialized `drm_crtc`. unsafe { bindings::drm_crtc_cleanup(crtc) };