{"id":18269,"url":"https://patchwork.libcamera.org/api/1.1/patches/18269/?format=json","web_url":"https://patchwork.libcamera.org/patch/18269/","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":"<20230208095922.1471175-3-chenghaoyang@google.com>","date":"2023-02-08T09:59:21","name":"[libcamera-devel,v1,2/3] libcamera: Add UdmaHeap if DmaHeap is not valid","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"7f6cebfe092cacf4cc3630a41dbe02222d062524","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/1.1/people/117/?format=json","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18269/mbox/","series":[{"id":3744,"url":"https://patchwork.libcamera.org/api/1.1/series/3744/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3744","date":"2023-02-08T09:59:19","name":"Add HeapAllocator","version":1,"mbox":"https://patchwork.libcamera.org/series/3744/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18269/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18269/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 C532ABEFBE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  8 Feb 2023 09:59:35 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BC199625FA;\n\tWed,  8 Feb 2023 10:59:34 +0100 (CET)","from mail-pl1-x634.google.com (mail-pl1-x634.google.com\n\t[IPv6:2607:f8b0:4864:20::634])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 624A8625DD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  8 Feb 2023 10:59:32 +0100 (CET)","by mail-pl1-x634.google.com with SMTP id k13so18723496plg.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 08 Feb 2023 01:59:32 -0800 (PST)","from chenghaoyang-low.c.googlers.com.com\n\t(46.165.189.35.bc.googleusercontent.com. [35.189.165.46])\n\tby smtp.gmail.com with ESMTPSA id\n\tm2-20020a17090a4d8200b00230b8402760sm1077773pjh.38.2023.02.08.01.59.29\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 08 Feb 2023 01:59:30 -0800 (PST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1675850374;\n\tbh=iYUuK3LUwOTGkA1dDlInbKgFb+fMDZte7QHupxP6RvY=;\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=Q6Ad9CaVQROwGSeUfgN9vWVispwLVxdRYsDsrOgIE4p9HxJzjmhs2ijcNjDMtG7hk\n\tB7yfUi5+euRrO+tqeUm8VK9Jp9Pu1pOKuH2KRK5MfVER5fcW2ho67jZ7TVuk/RgHzD\n\tAGUUcxtEii3xK9u/ho7vAEVmwxmS0A5IlERFAiBX3uPfL4r7dcPTNAqAEmwT6sENVm\n\ttB3etjR9zo78xnvJkHW+w+7szkjJV/T/gBTVM3F21pZr8AZIG8LCdEfMYFrusBCtrw\n\tLrMyKoeKwpl9rU/0DmW//+f9RX09b5PDapF4j9FMoBV55jOD0r9OPmkQwpXi6/xHWG\n\ttda0aR6e/3Bjg==","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=uA1tNYpJ7gWm0FBElHLLWisTjWPNyd1HNlzREIPjWJE=;\n\tb=l/cmdkjN2iFzhfkMW4+QsX+u66+F/i/BkExR2HTavGivZUIC5lKH1NaU1FJqocSEVp\n\taie4TzL4liQthi2q9xPIBYomA8FcS2cMXCoup5FBcj4/P8zFiG1PwtOIDj5jHPyIDpsq\n\tMqzxOzEjJQgFwuA+Hi5WE013/a4Y0/xx4S2sE="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=chromium.org\n\theader.i=@chromium.org header.b=\"l/cmdkjN\"; \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=uA1tNYpJ7gWm0FBElHLLWisTjWPNyd1HNlzREIPjWJE=;\n\tb=bVsmh8VVnGZQUfeLEJARQGR9PruyCQ/tq6fsynU1FPbN76+QqLtWQJ+RNB3be+2GG8\n\tOHyAC2y4GsnRzlGcRshUOFpSahFj1BWCP2X+X5J0YT1xSctLzE6w6rI6WEbKJibJjtYH\n\tkJMLGIque2U2KiehWwairefV4b7FEJz3qHI7AD1sqc2ppAXW8oo4Qb0D7ZXvtzYVYpHY\n\tRJvY5BnvjygfSzwej5587PyCPFC0mzI3wRXY7czlqFEivlK97fOm3h3sUh8vGq2N35Ne\n\tpyqY9jvdW93MVHAWj8cIplEf8D5NIwxycLjkHGj8lnfdnM/DvwddwtTz80r2+EmqIGun\n\t3zDw==","X-Gm-Message-State":"AO0yUKUOM0u7FKqvfN9EKN3gcvgpZQ59b4HvogaRZwCsqSZba37RYvpQ\n\tfpV1vBXLQoOpxMO9Fhn3dBf7w/63D9soPW5y","X-Google-Smtp-Source":"AK7set83s9pcf5Ghtpl6MFXNYrZ6ACQ9c/yuN/Lr7TqigGtfQBvnUhkHvH8rg7jbAQwe+etbJa/b9w==","X-Received":"by 2002:a17:90b:1648:b0:230:d1dc:72d2 with SMTP id\n\til8-20020a17090b164800b00230d1dc72d2mr8170552pjb.38.1675850370756; \n\tWed, 08 Feb 2023 01:59:30 -0800 (PST)","X-Google-Original-From":"Harvey Yang <chenghaoyang@google.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  8 Feb 2023 09:59:21 +0000","Message-Id":"<20230208095922.1471175-3-chenghaoyang@google.com>","X-Mailer":"git-send-email 2.39.1.519.gcb327c4b5f-goog","In-Reply-To":"<20230208095922.1471175-1-chenghaoyang@google.com>","References":"<20230208095922.1471175-1-chenghaoyang@google.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v1 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..b5b86922\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, Google Inc.\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..2a470f6b\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, Google Inc.\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","v1","2/3"]}