{"id":18487,"url":"https://patchwork.libcamera.org/api/1.1/patches/18487/?format=json","web_url":"https://patchwork.libcamera.org/patch/18487/","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":"<20230328095534.3584-3-harveyycyang@gmail.com>","date":"2023-03-28T09:55:33","name":"[libcamera-devel,v4,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/18487/mbox/","series":[{"id":3823,"url":"https://patchwork.libcamera.org/api/1.1/series/3823/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3823","date":"2023-03-28T09:55:31","name":"Add HeapAllocator","version":4,"mbox":"https://patchwork.libcamera.org/series/3823/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18487/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18487/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 1B54EC0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 28 Mar 2023 09:55:45 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 13BDA62756;\n\tTue, 28 Mar 2023 11:55:44 +0200 (CEST)","from mail-pl1-x632.google.com (mail-pl1-x632.google.com\n\t[IPv6:2607:f8b0:4864:20::632])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 275D262749\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 Mar 2023 11:55:41 +0200 (CEST)","by mail-pl1-x632.google.com with SMTP id u10so11116751plz.7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 Mar 2023 02:55:41 -0700 (PDT)","from harveyyang.localdomain (1-163-26-93.dynamic-ip.hinet.net.\n\t[1.163.26.93]) by smtp.gmail.com with ESMTPSA id\n\tv21-20020aa78515000000b006259beddb63sm20241488pfn.44.2023.03.28.02.55.38\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 28 Mar 2023 02:55:39 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679997344;\n\tbh=Z1Mpl+qw1ze/oi6AZWCNx6OjkpDpzzECPjhC2lq7LPY=;\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=DygWzgTwo2/zbpOMHLOGADMBY363/3kf7gc+s6SRJxIOfoU4BiUVx3fdxhD48H70o\n\tc5WH5OWB99sVMQvWjImpec6vq+Qf48cJkzDrzY9KgK79M+DQWH3ZDU+H2Yd3cj1g48\n\t3UdH5/7DJfeYV0GVV8lBU2omVISr54uP2MLbBlua7ORqMi+6rogx4xMpRFlIJXA5e4\n\tnhgAUiYneU+/Gvqqd/vfsU6tYkTCyIZciArvac/sNjandGVlkjTunFSnTGdbiPVvoy\n\tf4eEWdxHZkbJbnTmIE5JdiKxUij9Qp1vnO2gZPCBMplayiDLOAGyd/JLQGwgtmzaeu\n\tUUJgQWvx81+gw==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1679997339;\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=4A9iq5ROqHE9gJpemOtkccgsirtSYRj6290oe2xGeM8=;\n\tb=m9VnCz3vVWZmfzTeyVBKKG/ah+mTY+KGPXP3uL4dKtLzveD+Cf3AQ84ay95+BZYP7B\n\ted7LXdL5tLEhXYTUJaULcMjwbvhvpIVFCIt7U0RR4NFFU2ZYN1jDzYktoybjtZg0IM0N\n\tW6aqWRahZcAGh8DeZPOHN83JTaFGVdPcLsvq8="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=chromium.org\n\theader.i=@chromium.org header.b=\"m9VnCz3v\"; \n\tdkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112; t=1679997339;\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=4A9iq5ROqHE9gJpemOtkccgsirtSYRj6290oe2xGeM8=;\n\tb=IVmQ9KfKAxqfR9hxW1BM/zVZKkEXlHsnMygf359W1Gp8tYfQJDbs/eIUkbGMYngzIk\n\tRnn0UBwFDz1EHuYaMuEYbeIrXrt1pXeJ3Od4w04irHFbfLK5jDkw0WDwFxNx1Z1L2nFl\n\tkN4wHyGLsJAN6MkxCNgrRkO6g0/7bhvOnLkhTxLgmfFvm8qTN3ReCwEa8QTvfFmmRXov\n\tWl2tY24Xvx/u8v3LC9TSPHbLPdGCcsbb/p2ApLoXw1oNfVwvwJUR9nyfDqJCnH0CLe2W\n\tuWqNCWuRoBdAFxIX1iyF1FIkpjzCJMKC6wT67xGSn7KWBcwr2dzIHmzxqsJQ5Mn83zzF\n\t1+aQ==","X-Gm-Message-State":"AO0yUKWvszMRKoNZdiip9xk0b66+Cnh8/7voBOM79UAfm9/tS7/wPfw3\n\tC2C+O7I05LM5nRdCrLGR+g9yTlzNCLSdXHshbOECNA==","X-Google-Smtp-Source":"AK7set89nZKlp+qdTme1kOQqjTVUIGhSZx1yz7JTMiURACpW9EV19uLR4jkrO/9o4D45/ybFGyON3g==","X-Received":"by 2002:a05:6a20:1aa1:b0:d9:11d2:383f with SMTP id\n\tci33-20020a056a201aa100b000d911d2383fmr11725740pzb.53.1679997339366; \n\tTue, 28 Mar 2023 02:55:39 -0700 (PDT)","X-Google-Original-From":"Harvey Yang <harveyycyang@gmail.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 28 Mar 2023 17:55:33 +0800","Message-Id":"<20230328095534.3584-3-harveyycyang@gmail.com>","X-Mailer":"git-send-email 2.40.0","In-Reply-To":"<20230328095534.3584-1-harveyycyang@gmail.com>","References":"<20230328095534.3584-1-harveyycyang@gmail.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 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":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Cheng-Hao Yang <chenghaoyang@chromium.org>\n\nIf 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","v4","2/3"]}