From patchwork Wed Mar 5 22:59:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lyude Paul X-Patchwork-Id: 14003652 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 64CE3C282EC for ; Wed, 5 Mar 2025 23:08:24 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C9EE710E85B; Wed, 5 Mar 2025 23:08:23 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="b2PFf3M2"; 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 AEE3C10E85B for ; Wed, 5 Mar 2025 23:08:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741216101; 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=jOW7KDNY/rnfsCrl7d4OXtnM2CoD0PhVS/YjiuzS72A=; b=b2PFf3M2BRO7uZz2vsBt+NOneflcmELj07vIYT4RLUSFOprbq4CX7YqMChE+FdVKy7Iblj MOwHJeSS5HhWKjd+kyKlgAmQnygkB8rAIk7FZRdkbYBV73ChgHtyYZMDA/vYRm+sBwoPCg /FjuOA6PtAjiN90vZMVUyze9mFKEfTQ= 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-660-SE5Zl_emMbu36FTP4ZVAKg-1; Wed, 05 Mar 2025 18:08:18 -0500 X-MC-Unique: SE5Zl_emMbu36FTP4ZVAKg-1 X-Mimecast-MFC-AGG-ID: SE5Zl_emMbu36FTP4ZVAKg_1741216095 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 CDB52180035E; Wed, 5 Mar 2025 23:08:15 +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 DCC21300019E; Wed, 5 Mar 2025 23:08:10 +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 , Asahi Lina , Wedson Almeida Filho , linux-kernel@vger.kernel.org (open list) Subject: [RFC v3 25/33] rust: drm/kms: Add drm_framebuffer bindings Date: Wed, 5 Mar 2025 17:59:41 -0500 Message-ID: <20250305230406.567126-26-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 adds some very simple bindings for drm_framebuffer. We don't use them much yet, but we'll eventually be using them when rvkms eventually gets CRC and writeback support. Just like Connector objects, these use RcModeObject. Signed-off-by: Lyude Paul --- V3: * Replace Framebuffer struct with tuple * Add safety comments for ModeObject implementation * Add comment for why we're using Sealed Signed-off-by: Lyude Paul --- rust/bindings/bindings_helper.h | 1 + rust/kernel/drm/kms.rs | 1 + rust/kernel/drm/kms/framebuffer.rs | 74 ++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 rust/kernel/drm/kms/framebuffer.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 846eb6eb8fc4c..2e80a62062fc8 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/drm/kms.rs b/rust/kernel/drm/kms.rs index 978bb6712ffbe..429ce28229c9e 100644 --- a/rust/kernel/drm/kms.rs +++ b/rust/kernel/drm/kms.rs @@ -6,6 +6,7 @@ pub mod connector; pub mod crtc; pub mod encoder; +pub mod framebuffer; pub mod plane; use crate::{ diff --git a/rust/kernel/drm/kms/framebuffer.rs b/rust/kernel/drm/kms/framebuffer.rs new file mode 100644 index 0000000000000..5a60b580fe0bf --- /dev/null +++ b/rust/kernel/drm/kms/framebuffer.rs @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT + +//! DRM framebuffers. +//! +//! C header: [`include/drm/drm_framebuffer.h`](srctree/include/drm/drm_framebuffer.h) + +use super::{KmsDriver, ModeObject, RcModeObject}; +use crate::{drm::device::Device, types::*}; +use bindings; +use core::{marker::*, ptr}; + +/// The main interface for [`struct drm_framebuffer`]. +/// +/// # Invariants +/// +/// - `self.0` is initialized for as long as this object is exposed to users. +/// - This type has an identical data layout to [`struct drm_framebuffer`] +/// +/// [`struct drm_framebuffer`]: srctree/include/drm/drm_framebuffer.h +#[repr(transparent)] +pub struct Framebuffer(Opaque, PhantomData); + +// SAFETY: +// - `self.0` is initialized for as long as this object is exposed to users +// - `base` is initialized by DRM when `self.0` is initialized, thus `raw_mode_obj()` always returns +// a valid pointer. +unsafe impl ModeObject for Framebuffer { + type Driver = T; + + fn drm_dev(&self) -> &Device { + // SAFETY: `dev` points to an initialized `struct drm_device` for as long as this type is + // initialized + unsafe { Device::borrow((*self.0.get()).dev) } + } + + fn raw_mode_obj(&self) -> *mut bindings::drm_mode_object { + // SAFETY: We don't expose Framebuffer to users before its initialized, so `base` is + // always initialized + unsafe { ptr::addr_of_mut!((*self.0.get()).base) } + } +} + +// SAFETY: Framebuffers are refcounted mode objects. +unsafe impl RcModeObject for Framebuffer {} + +// SAFETY: References to framebuffers are safe to be accessed from any thread +unsafe impl Send for Framebuffer {} +// SAFETY: References to framebuffers are safe to be accessed from any thread +unsafe impl Sync for Framebuffer {} + +// For implementing ModeObject +impl crate::private::Sealed for Framebuffer {} + +impl PartialEq for Framebuffer { + fn eq(&self, other: &Self) -> bool { + ptr::eq(self.0.get(), other.0.get()) + } +} +impl Eq for Framebuffer {} + +impl Framebuffer { + /// Convert a raw pointer to a `struct drm_framebuffer` into a [`Framebuffer`] + /// + /// # Safety + /// + /// The caller guarantews that `ptr` points to a initialized `struct drm_framebuffer` for at + /// least the entire lifetime of `'a`. + #[inline] + #[allow(dead_code)] + pub(super) unsafe fn from_raw<'a>(ptr: *const bindings::drm_framebuffer) -> &'a Self { + // SAFETY: Our data layout is identical to drm_framebuffer + unsafe { &*ptr.cast() } + } +}