{"id":21879,"url":"https://patchwork.libcamera.org/api/1.1/patches/21879/?format=json","web_url":"https://patchwork.libcamera.org/patch/21879/","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":"<20241113055524.3099340-2-chenghaoyang@chromium.org>","date":"2024-11-13T05:54:32","name":"[v2,1/2] DmaBufAllocator: Add Dma Buffer synchronization function & helper class","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"e7542be23495966d7e4fd0d45d4684e5d86195be","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/21879/mbox/","series":[{"id":4787,"url":"https://patchwork.libcamera.org/api/1.1/series/4787/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4787","date":"2024-11-13T05:54:31","name":"Add DmaSyncer","version":2,"mbox":"https://patchwork.libcamera.org/series/4787/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21879/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21879/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 0606ABE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 13 Nov 2024 05:55:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 82523657FB;\n\tWed, 13 Nov 2024 06:55:36 +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 7294A657F8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Nov 2024 06:55:32 +0100 (CET)","by mail-pf1-x42c.google.com with SMTP id\n\td2e1a72fcca58-71e625b00bcso5280647b3a.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Nov 2024 21:55:32 -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-7240785ffb6sm12382544b3a.40.2024.11.12.21.55.29\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 12 Nov 2024 21:55:30 -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=\"h/EyXBZ4\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1731477330; x=1732082130;\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=kHPGmdFKP/nCGpmz205iSjC2QS8UqeRS8piXuDKNZD4=;\n\tb=h/EyXBZ4kWuO86/UsWvxdeSiT7CNjufkvZyYih8ZkZEU422U38IspHLlIsUQmN5NVc\n\tT5f1WQwxy9M6kBxHegVY0xJjzNEio1QoR3ZB8Mpg9NHgOJcYRoEpaw+PIXnlKZFMHTgC\n\tLpAkLhxd3Z/VRwk06EX9pz6K0wVZ42vetZ/d0=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1731477330; x=1732082130;\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=kHPGmdFKP/nCGpmz205iSjC2QS8UqeRS8piXuDKNZD4=;\n\tb=I6SY8Ov+gOg8cpOWW2kdvF3GCkFkmn+VlyQbxQ00a3YBKjhfEYnHEamCuCC6HJcO44\n\tQbuRZvhcxdkZ8xjaMoXNzdvS4kws3BNA+7ZrAhNuPJ4QGZvCuXp5nkVR8hvYg7gDRsXu\n\t8D4A2iKOInfjTBC5iVXZRNA9Mn5GOpcYHmVQSyt46C1mFuRpPbmZn7ee9lYHl2L0tPOB\n\tDGeXXlL1tdPTv1ardmFSt5h66XWtoiuauvvWlZ0i9rpKsayuvwbuJfLNBQqHYLYH3HW3\n\tov1fzkZcVvu9hncHYGSJY6M2KlksVV8yKe+85vZeE5N7expR3J5VeL/Toww0S8GDnajx\n\thpzQ==","X-Gm-Message-State":"AOJu0YwLIyt+b8fgrvTeF/kbkZ4l0fdl69Ta9iGMa95Q6IJUItH2XE6u\n\tYlM4Q7l2QY5d3nuXDrCSgdHFjOkRsD9R5fYVor2TbyEc2S3BbY/StCeqCNpXAG0roi/QWXFbVIw\n\t=","X-Google-Smtp-Source":"AGHT+IE5ZRTcAcp7BopDr66Fkk87+ACKYvcvzTBokuZohIF+4imUWVunorlPz6Zc1063rgEZzOJnrw==","X-Received":"by 2002:a05:6a00:1794:b0:71e:4cff:2654 with SMTP id\n\td2e1a72fcca58-7245799d6ebmr2462529b3a.6.1731477330511; \n\tTue, 12 Nov 2024 21:55:30 -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 v2 1/2] DmaBufAllocator: Add Dma Buffer synchronization\n\tfunction & helper class","Date":"Wed, 13 Nov 2024 05:54:32 +0000","Message-ID":"<20241113055524.3099340-2-chenghaoyang@chromium.org>","X-Mailer":"git-send-email 2.47.0.277.g8800431eea-goog","In-Reply-To":"<20241113055524.3099340-1-chenghaoyang@chromium.org>","References":"<20241113055524.3099340-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    | 33 +++++++\n src/libcamera/dma_buf_allocator.cpp           | 90 +++++++++++++++++++\n 2 files changed, 123 insertions(+)","diff":"diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h\nindex d2a0a0d19..2b9b99678 100644\n--- a/include/libcamera/internal/dma_buf_allocator.h\n+++ b/include/libcamera/internal/dma_buf_allocator.h\n@@ -23,6 +23,19 @@ public:\n \n \tusing DmaBufAllocatorFlags = Flags<DmaBufAllocatorFlag>;\n \n+\tenum class SyncStep {\n+\t\tStart = 0,\n+\t\tEnd\n+\t};\n+\n+\tenum class SyncType {\n+\t\tRead = 0,\n+\t\tWrite,\n+\t\tReadWrite,\n+\t};\n+\n+\tstatic void sync(int fd, SyncStep step, SyncType type);\n+\n \tDmaBufAllocator(DmaBufAllocatorFlags flags = DmaBufAllocatorFlag::CmaHeap);\n \t~DmaBufAllocator();\n \tbool isValid() const { return providerHandle_.isValid(); }\n@@ -35,6 +48,26 @@ private:\n \tDmaBufAllocatorFlag type_;\n };\n \n+class DmaSyncer final\n+{\n+public:\n+\texplicit DmaSyncer(int fd,\n+\t\t\t   DmaBufAllocator::SyncType type = DmaBufAllocator::SyncType::ReadWrite)\n+\t\t: fd_(fd), type_(type)\n+\t{\n+\t\tDmaBufAllocator::sync(fd_, DmaBufAllocator::SyncStep::Start, type_);\n+\t}\n+\n+\t~DmaSyncer()\n+\t{\n+\t\tDmaBufAllocator::sync(fd_, DmaBufAllocator::SyncStep::End, type_);\n+\t}\n+\n+private:\n+\tint fd_;\n+\tDmaBufAllocator::SyncType type_;\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..f0f068cc1 100644\n--- a/src/libcamera/dma_buf_allocator.cpp\n+++ b/src/libcamera/dma_buf_allocator.cpp\n@@ -78,6 +78,81 @@ LOG_DEFINE_CATEGORY(DmaBufAllocator)\n  * \\brief A bitwise combination of DmaBufAllocator::DmaBufAllocatorFlag values\n  */\n \n+/**\n+ * \\enum DmaBufAllocator::SyncStep\n+ * \\brief Either start or end of the synchronization\n+ * \\var DmaBufAllocator::Start\n+ * \\brief Indicates the start of a map access session\n+ * \\var DmaBufAllocator::End\n+ * \\brief Indicates the end of a map access session\n+ */\n+\n+/**\n+ * \\enum DmaBufAllocator::SyncType\n+ * \\brief Read and/or write access via the CPU map\n+ * \\var DmaBufAllocator::Read\n+ * \\brief Indicates that the mapped dma-buf will be read by the client via the\n+ * CPU map\n+ * \\var DmaBufAllocator::Write\n+ * \\brief Indicates that the mapped dm-buf will be written by the client via the\n+ * CPU map\n+ * \\var DmaBufAllocator::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+ * \\brief Synchronize CPU access and hardware access\n+ * \\param[in] fd The dma-buf's file descriptor to be synchronized\n+ * \\param[in] step Either start or end of the synchronization\n+ * \\param[in] type Read and/or write access during CPU mmap\n+ *\n+ * The client is expected to call this function with\n+ * DmaBufAllocator::SyncStep::Start and DmaBufAllocator::SyncStep::End at the\n+ * start and end of buffer access respectively.\n+ */\n+void DmaBufAllocator::sync(int fd, DmaBufAllocator::SyncStep step, DmaBufAllocator::SyncType type)\n+{\n+\tuint64_t flags = 0;\n+\tswitch (step) {\n+\tcase DmaBufAllocator::SyncStep::Start:\n+\t\tflags = DMA_BUF_SYNC_START;\n+\t\tbreak;\n+\tcase DmaBufAllocator::SyncStep::End:\n+\t\tflags = DMA_BUF_SYNC_END;\n+\t\tbreak;\n+\t}\n+\n+\tswitch (type) {\n+\tcase DmaBufAllocator::SyncType::Read:\n+\t\tflags = flags | DMA_BUF_SYNC_READ;\n+\t\tbreak;\n+\tcase DmaBufAllocator::SyncType::Write:\n+\t\tflags = flags | DMA_BUF_SYNC_WRITE;\n+\t\tbreak;\n+\tcase DmaBufAllocator::SyncType::ReadWrite:\n+\t\tflags = flags | DMA_BUF_SYNC_RW;\n+\t\tbreak;\n+\t}\n+\n+\tstruct dma_buf_sync sync = {\n+\t\t.flags = flags\n+\t};\n+\n+\tint ret;\n+\tdo {\n+\t\tret = ioctl(fd, 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) << \"Unable to sync dma fd: \" << fd\n+\t\t\t\t\t    << \", err: \" << strerror(ret)\n+\t\t\t\t\t    << \", step: \" << static_cast<int>(step)\n+\t\t\t\t\t    << \", type: \" << static_cast<int>(type);\n+\t}\n+}\n+\n /**\n  * \\brief Construct a DmaBufAllocator of a given type\n  * \\param[in] type The type(s) of the dma-buf providers to allocate from\n@@ -205,4 +280,19 @@ 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+\n+/**\n+ * \\fn DmaSyncer::DmaSyncer(int fd, DmaBufAllocator::SyncType type = DmaBufAllocator::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+\n } /* namespace libcamera */\n","prefixes":["v2","1/2"]}