{"id":22052,"url":"https://patchwork.libcamera.org/api/patches/22052/?format=json","web_url":"https://patchwork.libcamera.org/patch/22052/","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":"<20241121055436.2502314-2-chenghaoyang@chromium.org>","date":"2024-11-21T05:51:31","name":"[v3,1/2] DmaBufAllocator: Add Dma Buffer synchronization function & helper class","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"4339338f982df41d9243410d1af1a99a1614972a","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/22052/mbox/","series":[{"id":4811,"url":"https://patchwork.libcamera.org/api/series/4811/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4811","date":"2024-11-21T05:51:30","name":"Add DmaSyncer","version":3,"mbox":"https://patchwork.libcamera.org/series/4811/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22052/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22052/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 AB51DC32F9\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 21 Nov 2024 05:54:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 64A8865F9F;\n\tThu, 21 Nov 2024 06:54:54 +0100 (CET)","from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com\n\t[IPv6:2607:f8b0:4864:20::42c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 88F9265FA8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 21 Nov 2024 06:54:45 +0100 (CET)","by mail-pf1-x42c.google.com with SMTP id\n\td2e1a72fcca58-720cb6ac25aso447358b3a.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 20 Nov 2024 21:54:45 -0800 (PST)","from chenghaoyang-low.c.googlers.com.com\n\t(27.247.221.35.bc.googleusercontent.com. [35.221.247.27])\n\tby smtp.gmail.com with ESMTPSA id\n\td2e1a72fcca58-724bef8da82sm2815815b3a.124.2024.11.20.21.54.41\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 20 Nov 2024 21:54:42 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"Eq6mzEpx\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1732168483; x=1732773283;\n\tdarn=lists.libcamera.org; \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=w3/doozRmHoESE9I/LoHUzPmNKrwEmnNFS2T9EoQZxg=;\n\tb=Eq6mzEpxJh9BrlSmXgbsqgRDqi/Q3VWEtO64t7E/yq4YOaX8bnqS1jcw4PttYWrvZ/\n\t6JENy5i6cVg1GYdkEn13XOrRHoVgQV877PCBFitRISXOW3hgJYC463UXYUDnUplYnAfm\n\tXcRfndfkZAqZtUQkuShDh4POaS3axP7Ef7i+U=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1732168483; x=1732773283;\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\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received":"by 2002:a05:6a00:3a12:b0:71e:5d1d:1aa2 with SMTP id\n\td2e1a72fcca58-724bec9ad56mr6330702b3a.7.1732168482743; \n\tWed, 20 Nov 2024 21:54:42 -0800 (PST)","From":"Harvey Yang <chenghaoyang@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Cc":"Harvey Yang <chenghaoyang@chromium.org>,\n\tHan-Lin Chen <hanlinchen@chromium.org>","Subject":"[PATCH v3 1/2] DmaBufAllocator: Add Dma Buffer synchronization\n\tfunction & helper class","Date":"Thu, 21 Nov 2024 05:51:31 +0000","Message-ID":"<20241121055436.2502314-2-chenghaoyang@chromium.org>","X-Mailer":"git-send-email 2.47.0.338.g60cca15819-goog","In-Reply-To":"<20241121055436.2502314-1-chenghaoyang@chromium.org>","References":"<20241121055436.2502314-1-chenghaoyang@chromium.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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":"To synchronize CPU access with mmap and hardware access on DMA buffers,\nusing `DMA_BUF_IOCTL_SYNC` is required. This patch adds a function and\na helper class to allow users to sync buffers more easily.\n\nSigned-off-by: Han-Lin Chen <hanlinchen@chromium.org>\nCo-developed-by: Harvey Yang <chenghaoyang@chromium.org>\nSigned-off-by: Harvey Yang <chenghaoyang@chromium.org>\n---\n .../libcamera/internal/dma_buf_allocator.h    | 21 ++++++\n src/libcamera/dma_buf_allocator.cpp           | 75 +++++++++++++++++++\n 2 files changed, 96 insertions(+)","diff":"diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h\nindex d2a0a0d19..e4073f668 100644\n--- a/include/libcamera/internal/dma_buf_allocator.h\n+++ b/include/libcamera/internal/dma_buf_allocator.h\n@@ -8,6 +8,7 @@\n #pragma once\n \n #include <libcamera/base/flags.h>\n+#include <libcamera/base/shared_fd.h>\n #include <libcamera/base/unique_fd.h>\n \n namespace libcamera {\n@@ -35,6 +36,26 @@ private:\n \tDmaBufAllocatorFlag type_;\n };\n \n+class DmaSyncer final\n+{\n+public:\n+\tenum class SyncType {\n+\t\tRead = 0,\n+\t\tWrite,\n+\t\tReadWrite,\n+\t};\n+\n+\texplicit DmaSyncer(SharedFD fd, SyncType type = SyncType::ReadWrite);\n+\n+\t~DmaSyncer();\n+\n+private:\n+\tvoid sync(uint64_t step);\n+\n+\tSharedFD fd_;\n+\tuint64_t flags_ = 0;\n+};\n+\n LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaBufAllocator::DmaBufAllocatorFlag)\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp\nindex be6efb89f..c1c2103d6 100644\n--- a/src/libcamera/dma_buf_allocator.cpp\n+++ b/src/libcamera/dma_buf_allocator.cpp\n@@ -205,4 +205,79 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n \t\treturn allocFromHeap(name, size);\n }\n \n+/**\n+ * \\class DmaSyncer\n+ * \\brief Helper class for dma-buf's synchronization\n+ *\n+ * This class wraps a userspace dma-buf's synchronization process with an\n+ * object's lifetime.\n+ *\n+ * It's used when the user needs to access a dma-buf with CPU, mostly mapped\n+ * with MappedFrameBuffer, so that the buffer is synchronized between CPU and\n+ * ISP.\n+ */\n+\n+/**\n+ * \\enum DmaSyncer::SyncType\n+ * \\brief Read and/or write access via the CPU map\n+ * \\var DmaSyncer::Read\n+ * \\brief Indicates that the mapped dma-buf will be read by the client via the\n+ * CPU map\n+ * \\var DmaSyncer::Write\n+ * \\brief Indicates that the mapped dm-buf will be written by the client via the\n+ * CPU map\n+ * \\var DmaSyncer::ReadWrite\n+ * \\brief Indicates that the mapped dma-buf will be read and written by the\n+ * client via the CPU map\n+ */\n+\n+/**\n+ * \\fn DmaSyncer::DmaSyncer(SharedFD fd, SyncType type = SyncType::ReadWrite)\n+ * \\brief Construct a DmaSyncer with a dma-buf's fd and the access type\n+ * \\param[in] fd The dma-buf's file descriptor to synchronize\n+ * \\param[in] type Read and/or write access via the CPU map\n+ */\n+DmaSyncer::DmaSyncer(SharedFD fd, SyncType type)\n+\t: fd_(fd)\n+{\n+\tswitch (type) {\n+\tcase SyncType::Read:\n+\t\tflags_ = DMA_BUF_SYNC_READ;\n+\t\tbreak;\n+\tcase SyncType::Write:\n+\t\tflags_ = DMA_BUF_SYNC_WRITE;\n+\t\tbreak;\n+\tcase SyncType::ReadWrite:\n+\t\tflags_ = DMA_BUF_SYNC_RW;\n+\t\tbreak;\n+\t}\n+\n+\tsync(DMA_BUF_SYNC_START);\n+}\n+\n+DmaSyncer::~DmaSyncer()\n+{\n+\tsync(DMA_BUF_SYNC_END);\n+}\n+\n+void DmaSyncer::sync(uint64_t step)\n+{\n+\tstruct dma_buf_sync sync = {\n+\t\t.flags = flags_ | step\n+\t};\n+\n+\tint ret;\n+\tdo {\n+\t\tret = ioctl(fd_.get(), DMA_BUF_IOCTL_SYNC, &sync);\n+\t} while (ret && (errno == EINTR || errno == EAGAIN));\n+\n+\tif (ret) {\n+\t\tret = errno;\n+\t\tLOG(DmaBufAllocator, Error)\n+\t\t\t<< \"Unable to sync dma fd: \" << fd_.get()\n+\t\t\t<< \", err: \" << strerror(ret)\n+\t\t\t<< \", flags: \" << sync.flags;\n+\t}\n+}\n+\n } /* namespace libcamera */\n","prefixes":["v3","1/2"]}