From patchwork Mon Mar 11 14:15:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 19666 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 10A1FC32A3 for ; Mon, 11 Mar 2024 14:15:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BE8BB62868; Mon, 11 Mar 2024 15:15:38 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="U8LjPgjr"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AA5E762C88 for ; Mon, 11 Mar 2024 15:15:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710166535; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xB1JKMgKcvPP6XGmxDbDJKnN0KwKX7Op7EzdPDGztco=; b=U8LjPgjrft9c50BoKlbRMkfbR69DMTLFYnSkqELgdYEFjIFByDWcgxp7/GY7HUlnFUX/si 4r+Ss5gmvCmkYS1ZNuzvVGtCJhKDnhBB2hTHxQcojVRddRh72mkmitAMGsrQwNsnTGjieB FzhTWLYrWfQjsxro2r6Tz9DmVu1cTTg= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-132-txudEXaxPhGqDHbK0ucCLg-1; Mon, 11 Mar 2024 10:15:32 -0400 X-MC-Unique: txudEXaxPhGqDHbK0ucCLg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id E69B83C0CEF5; Mon, 11 Mar 2024 14:15:31 +0000 (UTC) Received: from x1.localdomain.com (unknown [10.39.195.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 488871C060CE; Mon, 11 Mar 2024 14:15:30 +0000 (UTC) From: Hans de Goede To: libcamera-devel@lists.libcamera.org Subject: [PATCH v5 03/18] libcamera: dma_heaps: extend DmaHeap class to support system heap Date: Mon, 11 Mar 2024 15:15:07 +0100 Message-ID: <20240311141524.27192-4-hdegoede@redhat.com> In-Reply-To: <20240311141524.27192-1-hdegoede@redhat.com> References: <20240311141524.27192-1-hdegoede@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxime Ripard , Pavel Machek , Bryan O'Donoghue Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Andrey Konovalov Add an argument to the constructor to specify dma heaps type(s) to use. Can be DmaHeapFlag::Cma and/or DmaHeapFlag::System. By default DmaHeapFlag::Cma is used. If both DmaHeapFlag::Cma and DmaHeapFlag::System are set, CMA heap is tried first. Tested-by: Bryan O'Donoghue # sc8280xp Lenovo x13s Tested-by: Pavel Machek Reviewed-by: Milan Zamazal Reviewed-by: Pavel Machek Signed-off-by: Andrey Konovalov Signed-off-by: Hans de Goede --- include/libcamera/internal/dma_heaps.h | 12 ++++- src/libcamera/dma_heaps.cpp | 67 ++++++++++++++++++++------ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/include/libcamera/internal/dma_heaps.h b/include/libcamera/internal/dma_heaps.h index cff8f140..80bf29e7 100644 --- a/include/libcamera/internal/dma_heaps.h +++ b/include/libcamera/internal/dma_heaps.h @@ -9,6 +9,7 @@ #include +#include #include namespace libcamera { @@ -16,7 +17,14 @@ namespace libcamera { class DmaHeap { public: - DmaHeap(); + enum class DmaHeapFlag { + Cma = 1 << 0, + System = 1 << 1, + }; + + using DmaHeapFlags = Flags; + + DmaHeap(DmaHeapFlags flags = DmaHeapFlag::Cma); ~DmaHeap(); bool isValid() const { return dmaHeapHandle_.isValid(); } UniqueFD alloc(const char *name, std::size_t size); @@ -25,4 +33,6 @@ private: UniqueFD dmaHeapHandle_; }; +LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaHeap::DmaHeapFlag) + } /* namespace libcamera */ diff --git a/src/libcamera/dma_heaps.cpp b/src/libcamera/dma_heaps.cpp index 38ef175a..d0e33ce6 100644 --- a/src/libcamera/dma_heaps.cpp +++ b/src/libcamera/dma_heaps.cpp @@ -19,9 +19,11 @@ /** * \file dma_heaps.cpp - * \brief CMA dma-heap allocator + * \brief dma-heap allocator */ +namespace libcamera { + /* * /dev/dma_heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma * to only have to worry about importing. @@ -29,42 +31,77 @@ * Annoyingly, should the cma heap size be specified on the kernel command line * instead of DT, the heap gets named "reserved" instead. */ -static constexpr std::array heapNames = { - "/dev/dma_heap/linux,cma", - "/dev/dma_heap/reserved" + +/** + * \struct DmaHeapInfo + * \brief Tells what type of dma-heap the dma-heap represented by the device node name is + * \var DmaHeapInfo::flag + * \brief The type of the dma-heap + * \var DmaHeapInfo::name + * \brief The dma-heap's device node name + */ +struct DmaHeapInfo { + DmaHeap::DmaHeapFlag flag; + const char *name; }; -namespace libcamera { +static constexpr std::array heapInfos = { + { /* CMA heap names first */ + { DmaHeap::DmaHeapFlag::Cma, "/dev/dma_heap/linux,cma" }, + { DmaHeap::DmaHeapFlag::Cma, "/dev/dma_heap/reserved" }, + { DmaHeap::DmaHeapFlag::System, "/dev/dma_heap/system" } } +}; LOG_DEFINE_CATEGORY(DmaHeap) /** * \class DmaHeap - * \brief Helper class for CMA dma-heap allocations + * \brief Helper class for dma-heap allocations */ /** - * \brief Construct a DmaHeap that owns a CMA dma-heap file descriptor + * \enum DmaHeap::DmaHeapFlag + * \brief Type of the dma-heap + * \var DmaHeap::Cma + * \brief Allocate from a CMA dma-heap + * \var DmaHeap::System + * \brief Allocate from the system dma-heap + */ + +/** + * \typedef DmaHeap::DmaHeapFlags + * \brief A bitwise combination of DmaHeap::DmaHeapFlag values + */ + +/** + * \brief Construct a DmaHeap that owns a CMA or system dma-heap file descriptor + * \param [in] flags The type(s) of the dma-heap(s) to allocate from * - * Goes through the internal list of possible names of the CMA dma-heap devices - * until a CMA dma-heap device is successfully opened. If it fails to open any - * dma-heap device, an invalid DmaHeap object is constructed. A valid DmaHeap - * object owns a wrapped dma-heap file descriptor. + * By default \a flags are set to DmaHeap::DmaHeapFlag::Cma. The constructor goes + * through the internal list of possible names of the CMA and system dma-heap devices + * until the dma-heap device of the requested type is successfully opened. If more + * than one dma-heap type is specified in flags the CMA heap is tried first. If it + * fails to open any dma-heap device an invalid DmaHeap object is constructed. + * A valid DmaHeap object owns a wrapped dma-heap file descriptor. * * Please check the new DmaHeap object with \ref DmaHeap::isValid before using it. */ -DmaHeap::DmaHeap() +DmaHeap::DmaHeap(DmaHeapFlags flags) { - for (const char *name : heapNames) { - int ret = ::open(name, O_RDWR | O_CLOEXEC, 0); + for (const auto &info : heapInfos) { + if (!(flags & info.flag)) + continue; + + int ret = ::open(info.name, O_RDWR | O_CLOEXEC, 0); if (ret < 0) { ret = errno; LOG(DmaHeap, Debug) - << "Failed to open " << name << ": " + << "Failed to open " << info.name << ": " << strerror(ret); continue; } + LOG(DmaHeap, Debug) << "Using " << info.name; dmaHeapHandle_ = UniqueFD(ret); break; }