{"id":20102,"url":"https://patchwork.libcamera.org/api/1.1/patches/20102/?format=json","web_url":"https://patchwork.libcamera.org/patch/20102/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20240527141647.336633-1-hdegoede@redhat.com>","date":"2024-05-27T14:16:47","name":"libcamera: dma_heaps: Add support for using udmabuf to alloc DMA heaps","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"28af91faf4366da4031d277ca6498d403a2bd3d7","submitter":{"id":102,"url":"https://patchwork.libcamera.org/api/1.1/people/102/?format=json","name":"Hans de Goede","email":"hdegoede@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/20102/mbox/","series":[{"id":4328,"url":"https://patchwork.libcamera.org/api/1.1/series/4328/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4328","date":"2024-05-27T14:16:47","name":"libcamera: dma_heaps: Add support for using udmabuf to alloc DMA heaps","version":1,"mbox":"https://patchwork.libcamera.org/series/4328/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/20102/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/20102/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 23A4DBDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 27 May 2024 14:16:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B3518634B1;\n\tMon, 27 May 2024 16:16:56 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EA467634AD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 May 2024 16:16:54 +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-120-uwLEeCBiNoC0TaDSGKeRFA-1; Mon, 27 May 2024 10:16:50 -0400","from smtp.corp.redhat.com\n\t(int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3])\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 B53FF800169;\n\tMon, 27 May 2024 14:16:49 +0000 (UTC)","from x1.localdomain.com (unknown [10.39.194.78])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 540F810004FF;\n\tMon, 27 May 2024 14:16:48 +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=\"jO9i8IGH\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1716819413;\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\tbh=JNROLB6OZsBKoMRl++54+tyMHVwjrzJ/egqlurgeDbc=;\n\tb=jO9i8IGHGMJFI+m88CuenMMVCwDdHSRWaIf2AaesEFtNtJEGHwud97vcnZ0snr/a7Xsd2y\n\tRBt84N5aqepWy6BAGAB1h9HlpXkUST4vxFZbj8Y8AFTp3ABpkbNCHM3UbAOXZa4N2WdihS\n\t0utuaC8egwXUBO49p5AzcL+dOuq3OE8=","X-MC-Unique":"uwLEeCBiNoC0TaDSGKeRFA-1","From":"Hans de Goede <hdegoede@redhat.com>","To":"libcamera-devel@lists.libcamera.org,\n\tSakari Ailus <sakari.ailus@linux.intel.com>","Cc":"Maxime Ripard <mripard@redhat.com>, Milan Zamazal <mzamazal@redhat.com>, \n\tDennis Bonke <admin@dennisbonke.com>, Hans de Goede <hdegoede@redhat.com>,\n\tHarvey Yang <chenghaoyang@chromium.org>","Subject":"[PATCH] libcamera: dma_heaps: Add support for using udmabuf to alloc\n\tDMA heaps","Date":"Mon, 27 May 2024 16:16:47 +0200","Message-ID":"<20240527141647.336633-1-hdegoede@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.4.1 on 10.11.54.3","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":"Add support for using udmabuf to alloc DMA heaps, this is basically:\nhttps://patchwork.libcamera.org/patch/18922/\n\nported from the never merged HeapAllocator class to the current DmaHeap\nclass.\n\nCo-developed-by: Harvey Yang <chenghaoyang@chromium.org>\nSigned-off-by: Harvey Yang <chenghaoyang@chromium.org>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\n include/libcamera/internal/dma_heaps.h |   3 +\n src/libcamera/dma_heaps.cpp            | 127 +++++++++++++++++++++----\n 2 files changed, 109 insertions(+), 21 deletions(-)","diff":"diff --git a/include/libcamera/internal/dma_heaps.h b/include/libcamera/internal/dma_heaps.h\nindex f0a8aa5d..7e1271ed 100644\n--- a/include/libcamera/internal/dma_heaps.h\n+++ b/include/libcamera/internal/dma_heaps.h\n@@ -30,7 +30,10 @@ public:\n \tUniqueFD alloc(const char *name, std::size_t size);\n \n private:\n+\tUniqueFD allocFromHeap(const char *name, std::size_t size);\n+\tUniqueFD allocFromUDmaBuf(const char *name, std::size_t size);\n \tUniqueFD dmaHeapHandle_;\n+\tbool useUDmaBuf_;\n };\n \n LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaHeap::DmaHeapFlag)\ndiff --git a/src/libcamera/dma_heaps.cpp b/src/libcamera/dma_heaps.cpp\nindex d4cb880b..bb707786 100644\n--- a/src/libcamera/dma_heaps.cpp\n+++ b/src/libcamera/dma_heaps.cpp\n@@ -10,10 +10,14 @@\n #include <array>\n #include <fcntl.h>\n #include <sys/ioctl.h>\n+#include <sys/mman.h>\n+#include <sys/stat.h>\n+#include <sys/types.h>\n #include <unistd.h>\n \n #include <linux/dma-buf.h>\n #include <linux/dma-heap.h>\n+#include <linux/udmabuf.h>\n \n #include <libcamera/base/log.h>\n \n@@ -36,13 +40,15 @@ namespace libcamera {\n struct DmaHeapInfo {\n \tDmaHeap::DmaHeapFlag type;\n \tconst char *deviceNodeName;\n+\tbool useUDmaBuf;\n };\n #endif\n \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+static constexpr std::array<DmaHeapInfo, 4> heapInfos = { {\n+\t{ DmaHeap::DmaHeapFlag::Cma, \"/dev/dma_heap/linux,cma\", false },\n+\t{ DmaHeap::DmaHeapFlag::Cma, \"/dev/dma_heap/reserved\", false },\n+\t{ DmaHeap::DmaHeapFlag::System, \"/dev/dma_heap/system\", false },\n+\t{ DmaHeap::DmaHeapFlag::System, \"/dev/udmabuf\", true },\n } };\n \n LOG_DEFINE_CATEGORY(DmaHeap)\n@@ -105,6 +111,7 @@ DmaHeap::DmaHeap(DmaHeapFlags type)\n \n \t\tLOG(DmaHeap, Debug) << \"Using \" << info.deviceNodeName;\n \t\tdmaHeapHandle_ = UniqueFD(ret);\n+\t\tuseUDmaBuf_ = info.useUDmaBuf;\n \t\tbreak;\n \t}\n \n@@ -123,25 +130,10 @@ DmaHeap::~DmaHeap() = default;\n  * \\return True if the DmaHeap is valid, false otherwise\n  */\n \n-/**\n- * \\brief Allocate a dma-buf from the DmaHeap\n- * \\param [in] name The name to set for the allocated buffer\n- * \\param [in] size The size of the buffer to allocate\n- *\n- * Allocates a dma-buf with read/write access.\n- *\n- * If the allocation fails, return an invalid UniqueFD.\n- *\n- * \\return The UniqueFD of the allocated buffer\n- */\n-UniqueFD DmaHeap::alloc(const char *name, std::size_t size)\n+UniqueFD DmaHeap::allocFromHeap(const char *name, std::size_t size)\n {\n-\tint ret;\n-\n-\tif (!name)\n-\t\treturn {};\n-\n \tstruct dma_heap_allocation_data alloc = {};\n+\tint ret;\n \n \talloc.len = size;\n \talloc.fd_flags = O_CLOEXEC | O_RDWR;\n@@ -162,4 +154,97 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size)\n \treturn allocFd;\n }\n \n+UniqueFD DmaHeap::allocFromUDmaBuf(const char *name, std::size_t size)\n+{\n+\t/* size must be a multiple of the page-size round it up */\n+\tstd::size_t pageMask = sysconf(_SC_PAGESIZE) - 1;\n+\tsize = (size + pageMask) & ~pageMask;\n+\n+\tint ret = memfd_create(name, MFD_ALLOW_SEALING);\n+\tif (ret < 0) {\n+\t\tret = errno;\n+\t\tLOG(DmaHeap, Error)\n+\t\t\t<< \"UdmaHeap failed to allocate memfd storage: \"\n+\t\t\t<< strerror(ret);\n+\t\treturn {};\n+\t}\n+\n+\tUniqueFD memfd(ret);\n+\n+\tret = ftruncate(memfd.get(), size);\n+\tif (ret < 0) {\n+\t\tret = errno;\n+\t\tLOG(DmaHeap, Error)\n+\t\t\t<< \"UdmaHeap failed to set memfd size: \" << strerror(ret);\n+\t\treturn {};\n+\t}\n+\n+\t/* UdmaHeap Buffers *must* have the F_SEAL_SHRINK seal */\n+\tret = fcntl(memfd.get(), F_ADD_SEALS, F_SEAL_SHRINK);\n+\tif (ret < 0) {\n+\t\tret = errno;\n+\t\tLOG(DmaHeap, Error)\n+\t\t\t<< \"UdmaHeap failed to seal the memfd: \" << strerror(ret);\n+\t\treturn {};\n+\t}\n+\n+\tstruct udmabuf_create create;\n+\n+\tcreate.memfd = memfd.get();\n+\tcreate.flags = UDMABUF_FLAGS_CLOEXEC;\n+\tcreate.offset = 0;\n+\tcreate.size = size;\n+\n+\tret = ::ioctl(dmaHeapHandle_.get(), UDMABUF_CREATE, &create);\n+\tif (ret < 0) {\n+\t\tret = errno;\n+\t\tLOG(DmaHeap, Error)\n+\t\t\t<< \"UdmaHeap failed to allocate \" << size << \" bytes: \"\n+\t\t\t<< strerror(ret);\n+\t\treturn {};\n+\t}\n+\n+\tif (create.size < size) {\n+\t\tLOG(DmaHeap, Error)\n+\t\t\t<< \"UdmaHeap allocated \" << create.size << \" bytes instead of \"\n+\t\t\t<< size << \" bytes\";\n+\t\treturn {};\n+\t}\n+\n+\tif (create.size != size)\n+\t\tLOG(DmaHeap, Warning)\n+\t\t\t<< \"UdmaHeap allocated \" << create.size << \" bytes, \"\n+\t\t\t<< \"which is greater than requested : \" << size << \" bytes\";\n+\n+\t/* Fail if not suitable, the allocation will be free'd by UniqueFD */\n+\tLOG(DmaHeap, Debug) << \"UdmaHeap allocated \" << create.size << \" bytes\";\n+\n+\t/* The underlying memfd is kept as as a reference in the kernel */\n+\tUniqueFD uDma(ret);\n+\n+\treturn uDma;\n+}\n+\n+/**\n+ * \\brief Allocate a dma-buf from the DmaHeap\n+ * \\param [in] name The name to set for the allocated buffer\n+ * \\param [in] size The size of the buffer to allocate\n+ *\n+ * Allocates a dma-buf with read/write access.\n+ *\n+ * If the allocation fails, return an invalid UniqueFD.\n+ *\n+ * \\return The UniqueFD of the allocated buffer\n+ */\n+UniqueFD DmaHeap::alloc(const char *name, std::size_t size)\n+{\n+\tif (!name)\n+\t\treturn {};\n+\n+\tif (useUDmaBuf_)\n+\t\treturn allocFromUDmaBuf(name, size);\n+\telse\n+\t\treturn allocFromHeap(name, size);\n+}\n+\n } /* namespace libcamera */\n","prefixes":[]}