{"id":19821,"url":"https://patchwork.libcamera.org/api/patches/19821/?format=json","web_url":"https://patchwork.libcamera.org/patch/19821/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240404084657.353464-4-mzamazal@redhat.com>","date":"2024-04-04T08:46:40","name":"[v7,03/18] libcamera: dma_heaps: extend DmaHeap class to support system heap","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"824ae53f2ac91c845d58fa116abf7b0ff88d9963","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/people/177/?format=json","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19821/mbox/","series":[{"id":4246,"url":"https://patchwork.libcamera.org/api/series/4246/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4246","date":"2024-04-04T08:46:37","name":"libcamera: introduce Software ISP and Software IPA","version":7,"mbox":"https://patchwork.libcamera.org/series/4246/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19821/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19821/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 02954C0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  4 Apr 2024 08:47:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B336163373;\n\tThu,  4 Apr 2024 10:47:36 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5DC1D6336D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  4 Apr 2024 10:47:34 +0200 (CEST)","from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com\n\t[66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-689-vtjVh1L8NPSpIl7bGVaT3g-1; Thu, 04 Apr 2024 04:47:31 -0400","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mimecast-mx02.redhat.com (Postfix) with ESMTPS id A2AF4863017;\n\tThu,  4 Apr 2024 08:47:30 +0000 (UTC)","from nuthatch.brq.redhat.com (unknown [10.43.17.39])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 49C718173;\n\tThu,  4 Apr 2024 08:47:29 +0000 (UTC)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"G8hEjjq3\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1712220453;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=QK+1+oSKmj1gR1qnBux1j02v7JZt/vfWqNtE5KT9bms=;\n\tb=G8hEjjq3af/DlAX8eYqujcGAa7CjuFoTEmAfJZr/a1ZB3d9urq0KXqJ1EunO8mz/dG7czL\n\ttp2eeiZJJerwSPM+feAkLqKYoTpAg3z45eWvupUdR+u04kULC99fl/13Z9RvqkycFfioNX\n\tXKN5HQgn3jXHJdnWRxsil5lNHeL2rDs=","X-MC-Unique":"vtjVh1L8NPSpIl7bGVaT3g-1","From":"Milan Zamazal <mzamazal@redhat.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Andrey Konovalov <andrey.konovalov@linaro.org>,\n\tAndrei Konovalov <andrey.konovalov.ynk@gmail.com>,\n\tBryan O'Donoghue <bryan.odonoghue@linaro.org>,\n\tMaxime Ripard <mripard@redhat.com>, Milan Zamazal <mzamazal@redhat.com>, \n\tPavel Machek <pavel@ucw.cz>, Hans de Goede <hdegoede@redhat.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>","Subject":"[PATCH v7 03/18] libcamera: dma_heaps: extend DmaHeap class to\n\tsupport system heap","Date":"Thu,  4 Apr 2024 10:46:40 +0200","Message-ID":"<20240404084657.353464-4-mzamazal@redhat.com>","In-Reply-To":"<20240404084657.353464-1-mzamazal@redhat.com>","References":"<20240404084657.353464-1-mzamazal@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.4.1 on 10.11.54.5","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain; charset=\"US-ASCII\"; x-default=true","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Andrey Konovalov <andrey.konovalov@linaro.org>\n\nAdd an argument to the constructor to specify dma heaps type(s)\nto use. Can be DmaHeapFlag::Cma and/or DmaHeapFlag::System.\nBy default DmaHeapFlag::Cma is used. If both DmaHeapFlag::Cma and\nDmaHeapFlag::System are set, CMA heap is tried first.\n\nTested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> # sc8280xp Lenovo x13s\nTested-by: Pavel Machek <pavel@ucw.cz>\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\nReviewed-by: Pavel Machek <pavel@ucw.cz>\nSigned-off-by: Andrey Konovalov <andrey.konovalov@linaro.org>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\n include/libcamera/internal/dma_heaps.h | 12 +++-\n src/libcamera/dma_heaps.cpp            | 76 +++++++++++++++++++++-----\n 2 files changed, 73 insertions(+), 15 deletions(-)","diff":"diff --git a/include/libcamera/internal/dma_heaps.h b/include/libcamera/internal/dma_heaps.h\nindex cff8f140..80bf29e7 100644\n--- a/include/libcamera/internal/dma_heaps.h\n+++ b/include/libcamera/internal/dma_heaps.h\n@@ -9,6 +9,7 @@\n \n #include <stddef.h>\n \n+#include <libcamera/base/flags.h>\n #include <libcamera/base/unique_fd.h>\n \n namespace libcamera {\n@@ -16,7 +17,14 @@ namespace libcamera {\n class DmaHeap\n {\n public:\n-\tDmaHeap();\n+\tenum class DmaHeapFlag {\n+\t\tCma = 1 << 0,\n+\t\tSystem = 1 << 1,\n+\t};\n+\n+\tusing DmaHeapFlags = Flags<DmaHeapFlag>;\n+\n+\tDmaHeap(DmaHeapFlags flags = DmaHeapFlag::Cma);\n \t~DmaHeap();\n \tbool isValid() const { return dmaHeapHandle_.isValid(); }\n \tUniqueFD alloc(const char *name, std::size_t size);\n@@ -25,4 +33,6 @@ private:\n \tUniqueFD dmaHeapHandle_;\n };\n \n+LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaHeap::DmaHeapFlag)\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/dma_heaps.cpp b/src/libcamera/dma_heaps.cpp\nindex 70da6f3a..2503e4c5 100644\n--- a/src/libcamera/dma_heaps.cpp\n+++ b/src/libcamera/dma_heaps.cpp\n@@ -19,9 +19,11 @@\n \n /**\n  * \\file dma_heaps.cpp\n- * \\brief CMA dma-heap allocator\n+ * \\brief dma-heap allocator\n  */\n \n+namespace libcamera {\n+\n /*\n  * /dev/dma_heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma\n  * to only have to worry about importing.\n@@ -29,40 +31,86 @@\n  * Annoyingly, should the cma heap size be specified on the kernel command line\n  * instead of DT, the heap gets named \"reserved\" instead.\n  */\n-static constexpr std::array<const char *, 2> heapNames = {\n-\t\"/dev/dma_heap/linux,cma\",\n-\t\"/dev/dma_heap/reserved\"\n+\n+/**\n+ * \\brief Specification of a heap info entry\n+ */\n+struct DmaHeapInfo {\n+\t/**\n+\t * \\brief Flag to match for considering the entry\n+\t */\n+\tDmaHeap::DmaHeapFlag type;\n+\t/**\n+\t * \\brief Path to a heap device\n+\t */\n+\tconst char *deviceNodeName;\n };\n \n-namespace libcamera {\n+static constexpr std::array<DmaHeapInfo, 3> heapInfos = { {\n+\t{ DmaHeap::DmaHeapFlag::Cma, \"/dev/dma_heap/linux,cma\" },\n+\t{ DmaHeap::DmaHeapFlag::Cma, \"/dev/dma_heap/reserved\" },\n+\t{ DmaHeap::DmaHeapFlag::System, \"/dev/dma_heap/system\" },\n+} };\n \n LOG_DEFINE_CATEGORY(DmaHeap)\n \n /**\n  * \\class DmaHeap\n- * \\brief Helper class for CMA dma-heap allocations\n+ * \\brief Helper class for dma-heap allocations\n+ *\n+ * DMA heaps are kernel devices that provide an API to allocate memory from\n+ * different pools called \"heaps\", wrap each allocated piece of memory in a\n+ * dmabuf object, and return the dmabuf file descriptor to userspace. Multiple\n+ * heaps can be provided by the system, with different properties for the\n+ * underlying memory.\n+ *\n+ * This class wraps a DMA heap selected at construction time, and exposes\n+ * functions to manage memory allocation.\n  */\n \n /**\n- * \\brief Construct a DmaHeap that owns a CMA dma-heap file descriptor\n+ * \\enum DmaHeap::DmaHeapFlag\n+ * \\brief Type of the dma-heap\n+ * \\var DmaHeap::Cma\n+ * \\brief Allocate from a CMA dma-heap, providing physically-contiguous memory\n+ * \\var DmaHeap::System\n+ * \\brief Allocate from the system dma-heap, using the page allocator\n+ */\n+\n+/**\n+ * \\typedef DmaHeap::DmaHeapFlags\n+ * \\brief A bitwise combination of DmaHeap::DmaHeapFlag values\n+ */\n+\n+/**\n+ * \\brief Construct a DmaHeap of a given type\n+ * \\param [in] type The type(s) of the dma-heap(s) to allocate from\n  *\n- * Looks for a CMA dma-heap device to use. If it fails to open any dma-heap\n- * device, an invalid DmaHeap object is constructed.\n+ * The DMA heap type is selected with the \\a type parameter, which defaults to\n+ * the CMA heap. If no heap of the given type can be accessed, the constructed\n+ * DmaHeap instance is invalid as indicated by the isValid() function.\n  *\n- * Check the new DmaHeap object with DmaHeap::isValid before using it.\n+ * Multiple types can be selected by combining type flags, in which case the\n+ * constructed DmaHeap will match one of the types. If the system provides\n+ * multiple heaps that match the requested types, which heap is used is\n+ * undefined.\n  */\n-DmaHeap::DmaHeap()\n+DmaHeap::DmaHeap(DmaHeapFlags type)\n {\n-\tfor (const char *name : heapNames) {\n-\t\tint ret = ::open(name, O_RDWR | O_CLOEXEC, 0);\n+\tfor (const auto &info : heapInfos) {\n+\t\tif (!(type & info.type))\n+\t\t\tcontinue;\n+\n+\t\tint ret = ::open(info.deviceNodeName, O_RDWR | O_CLOEXEC, 0);\n \t\tif (ret < 0) {\n \t\t\tret = errno;\n \t\t\tLOG(DmaHeap, Debug)\n-\t\t\t\t<< \"Failed to open \" << name << \": \"\n+\t\t\t\t<< \"Failed to open \" << info.deviceNodeName << \": \"\n \t\t\t\t<< strerror(ret);\n \t\t\tcontinue;\n \t\t}\n \n+\t\tLOG(DmaHeap, Debug) << \"Using \" << info.deviceNodeName;\n \t\tdmaHeapHandle_ = UniqueFD(ret);\n \t\tbreak;\n \t}\n","prefixes":["v7","03/18"]}