{"id":18325,"url":"https://patchwork.libcamera.org/api/patches/18325/?format=json","web_url":"https://patchwork.libcamera.org/patch/18325/","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":"<20230302081349.3334290-3-chenghaoyang@google.com>","date":"2023-03-02T08:13:48","name":"[libcamera-devel,v3,2/3] libcamera: Add UdmaHeap if DmaHeap is not valid","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"b75e3799a9972185a8b40141cb6770fc325fb44d","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/people/117/?format=json","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18325/mbox/","series":[{"id":3780,"url":"https://patchwork.libcamera.org/api/series/3780/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3780","date":"2023-03-02T08:13:46","name":"Add HeapAllocator","version":3,"mbox":"https://patchwork.libcamera.org/series/3780/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18325/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18325/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 4A3C0C3261\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  2 Mar 2023 08:14:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 81E43626C6;\n\tThu,  2 Mar 2023 09:14:00 +0100 (CET)","from mail-pf1-x432.google.com (mail-pf1-x432.google.com\n\t[IPv6:2607:f8b0:4864:20::432])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 37BD9626C0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  2 Mar 2023 09:13:59 +0100 (CET)","by mail-pf1-x432.google.com with SMTP id n5so9554252pfv.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 02 Mar 2023 00:13:59 -0800 (PST)","from chenghaoyang-low.c.googlers.com.com\n\t(112.157.221.35.bc.googleusercontent.com. [35.221.157.112])\n\tby smtp.gmail.com with ESMTPSA id\n\tp9-20020aa78609000000b005a8bdc18453sm9179755pfn.35.2023.03.02.00.13.56\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 02 Mar 2023 00:13:57 -0800 (PST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1677744840;\n\tbh=xXtbsxI0eWmqoGx3nXOrPPZbsjPCa3YF70/YiFpcAUs=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=k+z08ZZXsP6glH0YGKVGcgbm+35FVvrywgt0wRzNRXmJVPS6hf9ENqwgWVUoxJlrq\n\tmV6S91SXL/HRluwwh0BeOU3W3XJMHR37y4/cXy4M0s8EZV6ORZwnD2h1ArkhSGx7dW\n\tLZ7x1JZYMPvVtgPzh29zj5XF4P8LGFNYpV7D/4+Zvt+bhJ9vobZwC+m5rscqLDO/9C\n\ttMXb4U2guIaEdbp+CIkbHtwrZr7pv0sja9r7TXuEG5ljDemwJxzGy6hB6TgE0tNo5L\n\tE6x4AFeHEv/JcbkqxIbjmKvWClTJ4X6TGenEqVp4B3tCzpi6cKzEyREkfj3DNlfCLH\n\t9B4K56QNxdJow==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=M2Xpln7iZYP5FYEPjKtHSVNkBNC2q8U/NR/LoMx0CF8=;\n\tb=FL2X6hYi6F0kIvbJcO29P1lxUOqiOYuLQ2ZvEuYsjdjAsz1H9aJwzZhNCDHAx8TFrE\n\tEk1/9h9RwKA6hpNFUFEgXWJPUvxF/cEyrtV1JWHQLYEyxHgjY1PU2PWrwEyUksrRvdHp\n\tIy5ng4hy+iq/K1Zwgfd/6cSEb61ZRuu1INRoU="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=chromium.org\n\theader.i=@chromium.org header.b=\"FL2X6hYi\"; \n\tdkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=M2Xpln7iZYP5FYEPjKtHSVNkBNC2q8U/NR/LoMx0CF8=;\n\tb=PuhGeUHrLnroC3L+eypwvhibGC+HB+Da7bvlWYg8F6/JeFKQ0UueSCCTXrw8nokBbX\n\t8FqEg2Vm32CyRJOGFGGX04AXmojm13YiJwFcb/I2B53ar9pgx5YUwQgq9mOTG2FUwZGg\n\tGkhPR5iQ9cXtM53mZMR7nKqilpwzrTWB8FK+yGDlOheVSLKm3aJ3IaejNJ1krUBfelO2\n\trjqois6t1DO5N+nrAyxHpyg6Il8vgJjiHj3SZI1IWqfs6vRyQmQ/fpYQWYfhlQzkPuaW\n\t4DAne3iiUHvDXuxu9s1RsDrVUEH8OWErOCUtNdoYraxLVETnG25o9hDHql6rV9TEC6yz\n\tfJfQ==","X-Gm-Message-State":"AO0yUKWIzbZoDlgKiIewl4mdniq83VL5ZJRb+Z0VqNb+jq4Vrlp6ahtX\n\tU5Fcvr3TWjbHe+y4YfZJShmIXpe7iPw4gUJ6f64=","X-Google-Smtp-Source":"AK7set81y+z+JO7C1DSg4QtjkP9ALTSBlb885yFjo4rv3x/wMjOz0hopD1XSDkfqP+mzLtAc37E8DQ==","X-Received":"by 2002:aa7:9a4a:0:b0:5f1:f57a:b0c3 with SMTP id\n\tx10-20020aa79a4a000000b005f1f57ab0c3mr8885389pfj.5.1677744837601; \n\tThu, 02 Mar 2023 00:13:57 -0800 (PST)","X-Google-Original-From":"Harvey Yang <chenghaoyang@google.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  2 Mar 2023 08:13:48 +0000","Message-Id":"<20230302081349.3334290-3-chenghaoyang@google.com>","X-Mailer":"git-send-email 2.39.2.722.g9855ee24e9-goog","In-Reply-To":"<20230302081349.3334290-1-chenghaoyang@google.com>","References":"<20230302081349.3334290-1-chenghaoyang@google.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 2/3] libcamera: Add UdmaHeap if DmaHeap\n\tis not valid","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>","From":"Harvey Yang via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Harvey Yang <chenghaoyang@chromium.org>","Cc":"Harvey Yang <chenghaoyang@google.com>,\n\tHarvey Yang <chenghaoyang@chromium.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"If DmaHeap is not valid, fall back to UdmaHeap to allocate buffers.\n\nSigned-off-by: Harvey Yang <chenghaoyang@chromium.org>\n---\n include/libcamera/meson.build    |   1 +\n include/libcamera/udma_heap.h    |  20 ++++++\n src/libcamera/heap_allocator.cpp |   3 +\n src/libcamera/meson.build        |   1 +\n src/libcamera/udma_heap.cpp      | 117 +++++++++++++++++++++++++++++++\n 5 files changed, 142 insertions(+)\n create mode 100644 include/libcamera/udma_heap.h\n create mode 100644 src/libcamera/udma_heap.cpp","diff":"diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\nindex f486630a..a1414a57 100644\n--- a/include/libcamera/meson.build\n+++ b/include/libcamera/meson.build\n@@ -19,6 +19,7 @@ libcamera_public_headers = files([\n     'request.h',\n     'stream.h',\n     'transform.h',\n+    'udma_heap.h',\n ])\n \n subdir('base')\ndiff --git a/include/libcamera/udma_heap.h b/include/libcamera/udma_heap.h\nnew file mode 100644\nindex 00000000..3ae46dc7\n--- /dev/null\n+++ b/include/libcamera/udma_heap.h\n@@ -0,0 +1,20 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2023, Ideas on Board Oy\n+ *\n+ * udma_heap.h - Udma Heap implementation.\n+ */\n+\n+#include <libcamera/heap.h>\n+\n+namespace libcamera {\n+\n+class UdmaHeap : public Heap\n+{\n+public:\n+\tUdmaHeap();\n+\t~UdmaHeap();\n+\tUniqueFD alloc(const char *name, std::size_t size) override;\n+};\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp\nindex 594b1d6a..179c2c21 100644\n--- a/src/libcamera/heap_allocator.cpp\n+++ b/src/libcamera/heap_allocator.cpp\n@@ -18,6 +18,7 @@\n #include <libcamera/base/log.h>\n \n #include <libcamera/dma_heap.h>\n+#include <libcamera/udma_heap.h>\n \n namespace libcamera {\n \n@@ -26,6 +27,8 @@ LOG_DEFINE_CATEGORY(HeapAllocator)\n HeapAllocator::HeapAllocator()\n {\n \theap_ = std::make_unique<DmaHeap>();\n+\tif (!isValid())\n+\t\theap_ = std::make_unique<UdmaHeap>();\n }\n \n HeapAllocator::~HeapAllocator() = default;\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex ee586c0d..bce26d4b 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -45,6 +45,7 @@ libcamera_sources = files([\n     'stream.cpp',\n     'sysfs.cpp',\n     'transform.cpp',\n+    'udma_heap.cpp',\n     'v4l2_device.cpp',\n     'v4l2_pixelformat.cpp',\n     'v4l2_subdevice.cpp',\ndiff --git a/src/libcamera/udma_heap.cpp b/src/libcamera/udma_heap.cpp\nnew file mode 100644\nindex 00000000..050ab80f\n--- /dev/null\n+++ b/src/libcamera/udma_heap.cpp\n@@ -0,0 +1,117 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2023, Ideas on Board Oy\n+ *\n+ * dma_heap.h - Dma Heap implementation.\n+ */\n+\n+#include <libcamera/udma_heap.h>\n+\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/udmabuf.h>\n+\n+#include <libcamera/base/log.h>\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(UdmaHeap)\n+\n+UdmaHeap::UdmaHeap()\n+{\n+\tint ret = ::open(\"/dev/udmabuf\", O_RDWR, 0);\n+\tif (ret < 0) {\n+\t\tret = errno;\n+\t\tLOG(UdmaHeap, Error)\n+\t\t\t<< \"Failed to open allocator: \" << strerror(ret);\n+\n+\t\tif (ret == EACCES) {\n+\t\t\tLOG(UdmaHeap, Info)\n+\t\t\t\t<< \"Consider making /dev/udmabuf accessible by the video group\";\n+\t\t\tLOG(UdmaHeap, Info)\n+\t\t\t\t<< \"Alternatively, add your user to the kvm group.\";\n+\t\t}\n+\n+\t} else {\n+\t\thandle_ = UniqueFD(ret);\n+\t}\n+}\n+\n+UdmaHeap::~UdmaHeap() = default;\n+\n+UniqueFD UdmaHeap::alloc(const char *name, std::size_t size)\n+{\n+\tif (!isValid()) {\n+\t\tLOG(UdmaHeap, Fatal) << \"Allocation attempted without allocator\" << name;\n+\t\treturn {};\n+\t}\n+\n+\tint ret;\n+\n+\tret = memfd_create(name, MFD_ALLOW_SEALING);\n+\tif (ret < 0) {\n+\t\tret = errno;\n+\t\tLOG(UdmaHeap, Error)\n+\t\t\t<< \"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(UdmaHeap, Error)\n+\t\t\t<< \"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(UdmaHeap, Error)\n+\t\t\t<< \"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(handle_.get(), UDMABUF_CREATE, &create);\n+\tif (ret < 0) {\n+\t\tret = errno;\n+\t\tLOG(UdmaHeap, Error)\n+\t\t\t<< \"Failed to allocate \" << size << \" bytes: \"\n+\t\t\t<< strerror(ret);\n+\t\treturn {};\n+\t}\n+\n+\t/* The underlying memfd is kept as as a reference in the kernel */\n+\tUniqueFD uDma(ret);\n+\n+\tif (create.size != size)\n+\t\tLOG(UdmaHeap, Warning)\n+\t\t\t<< \"Allocated \" << create.size << \" bytes instead of \"\n+\t\t\t<< size << \" bytes\";\n+\n+\t/* Fail if not suitable, the allocation will be free'd by UniqueFD */\n+\tif (create.size < size)\n+\t\treturn {};\n+\n+\tLOG(UdmaHeap, Debug) << \"Allocated \" << create.size << \" bytes\";\n+\n+\treturn uDma;\n+}\n+\n+} /* namespace libcamera */\n","prefixes":["libcamera-devel","v3","2/3"]}