{"id":18282,"url":"https://patchwork.libcamera.org/api/1.1/patches/18282/?format=json","web_url":"https://patchwork.libcamera.org/patch/18282/","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":"<20230214093736.4067033-3-chenghaoyang@google.com>","date":"2023-02-14T09:37:35","name":"[libcamera-devel,v2,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/1.1/people/117/?format=json","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18282/mbox/","series":[{"id":3754,"url":"https://patchwork.libcamera.org/api/1.1/series/3754/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3754","date":"2023-02-14T09:37:33","name":"Add HeapAllocator","version":2,"mbox":"https://patchwork.libcamera.org/series/3754/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18282/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18282/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 C612CBDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 14 Feb 2023 09:37:49 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7E4EB625F1;\n\tTue, 14 Feb 2023 10:37:49 +0100 (CET)","from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com\n\t[IPv6:2607:f8b0:4864:20::1036])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E081625F8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 14 Feb 2023 10:37:45 +0100 (CET)","by mail-pj1-x1036.google.com with SMTP id bg2so5251136pjb.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 14 Feb 2023 01:37:44 -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\tz9-20020aa791c9000000b0059416691b64sm9696172pfa.19.2023.02.14.01.37.42\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 14 Feb 2023 01:37:43 -0800 (PST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1676367469;\n\tbh=LyDwwOzJh3NNCme0YWpxnLmWxx2LE7Yt59dY93zJl9s=;\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=WA27QZVxhPhDuxkv4lBoHPwZVQFWtI60+F38CCDMEQHG28SDfTgZpAQCes5Zqm634\n\t8B7503Qjl4E2HtOrkKRYB4LKLg1InMG43tcwQvLx2sTOE4iH7BLmfj30Bg9gizng2U\n\tSukHRtUF5uGZTyhyp5N2IoHGgjtLbnxnSlXBLIWEwX+QeBaZdK4xNEFzGgfL6Bc23A\n\tB4OAvjLoh0h09VRZNgZ5Z9T9AbKtZteX0Sn0C8UgP79vQaw6pCMqS0pOJTAyi43DjX\n\toJSljEIwutQ1TD0PUCRSjsz9BBnAOuUxcXjip7EkRsCQ4NDOnaoutG07+xKOkeBD0G\n\t9ghmh8o1EFLZg==","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=QpaG0t6PXjS7w3qMEAfXxXkv8IRrMJWTik1fs5WR5l8=;\n\tb=MEy2Ru6Ci/YG5rNw1dZcFRUbT4t313s+HL+XLrfNwIFVfU3UsUMJXkMuRB4QRswhxn\n\t90Eg3V2tCyBo+X9Or42W5XSST5yb1d3RCRMur1oEqGA4MY2X9JMWlRbaP4s7x5I9Sv2X\n\t85H8rwpCDSrkw/ChCPps/hxORK1mrlE4zrCpE="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=chromium.org\n\theader.i=@chromium.org header.b=\"MEy2Ru6C\"; \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=QpaG0t6PXjS7w3qMEAfXxXkv8IRrMJWTik1fs5WR5l8=;\n\tb=NV7MveF0mYoTy8MWJ69w3191fl1/ztNkMZgXOSjmT/q11yPEuEpM2c9AsDhbMp3f8b\n\td3YNUrtW0RCMj+x9ON7uN+6zS70AudvuJ+6gH5pJMN/bSR/erMkWB2LkgQLPlQQpN+UD\n\tGC2OGzqwzgTk7bA1GXkqrj/scDPhKGnXFSqHm8QzMSONIkWX9pCapOPtL6/CP84sJsFZ\n\t+umHOE+lAB4lVtDSGwQIgAerkTkJFdXn/u5DggiLQ3G0YMzoCyXzNMsau1qm3boOZ56C\n\tEVbojD6akqBkw9ne+WLQCGm2LdWVZeMaFz0tQfdapKGqKvVVvSf4K/xXVp150e9Wc5BI\n\ty4Og==","X-Gm-Message-State":"AO0yUKUo42nx+FcVdzjk6h62dcrzbBaUFVh/D4q0rQeqWw6DtbgLqDcG\n\tugeQp9cIIttDmVhaUB756nrZB5VoN8FTxJxe","X-Google-Smtp-Source":"AK7set+zE1oZCSx/fHSsqM4ZfcLSZJbK896BMsr4dcrf3z1EU+pFRk09UZXeqV7p0DC36JDkVf6O0w==","X-Received":"by 2002:a05:6a20:691a:b0:be:e0c3:5003 with SMTP id\n\tq26-20020a056a20691a00b000bee0c35003mr1992302pzj.5.1676367463423; \n\tTue, 14 Feb 2023 01:37:43 -0800 (PST)","X-Google-Original-From":"Harvey Yang <chenghaoyang@google.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 14 Feb 2023 09:37:35 +0000","Message-Id":"<20230214093736.4067033-3-chenghaoyang@google.com>","X-Mailer":"git-send-email 2.39.1.581.gbfd45094c4-goog","In-Reply-To":"<20230214093736.4067033-1-chenghaoyang@google.com>","References":"<20230214093736.4067033-1-chenghaoyang@google.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 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","v2","2/3"]}