Show a patch.

GET /api/patches/21875/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 21875,
    "url": "https://patchwork.libcamera.org/api/patches/21875/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21875/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20241112100051.4071443-2-chenghaoyang@chromium.org>",
    "date": "2024-11-12T09:56:54",
    "name": "[1/2] DmaBufAllocator: Add Dma Buffer synchronization function & helper class",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "762b161d5a2676b07867d6204219cbab0fb0d802",
    "submitter": {
        "id": 117,
        "url": "https://patchwork.libcamera.org/api/people/117/?format=api",
        "name": "Cheng-Hao Yang",
        "email": "chenghaoyang@chromium.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21875/mbox/",
    "series": [
        {
            "id": 4785,
            "url": "https://patchwork.libcamera.org/api/series/4785/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4785",
            "date": "2024-11-12T09:56:53",
            "name": "Add DmaSyncer",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/4785/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21875/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21875/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 22E11C324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 Nov 2024 10:01:04 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 50F39657EB;\n\tTue, 12 Nov 2024 11:01:01 +0100 (CET)",
            "from mail-il1-x12a.google.com (mail-il1-x12a.google.com\n\t[IPv6:2607:f8b0:4864:20::12a])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4A0F8657DF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Nov 2024 11:00:59 +0100 (CET)",
            "by mail-il1-x12a.google.com with SMTP id\n\te9e14a558f8ab-3a6bdc8f7a1so20639655ab.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Nov 2024 02:00:59 -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\t41be03b00d2f7-7f41f644214sm10157673a12.58.2024.11.12.02.00.56\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 12 Nov 2024 02:00:57 -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=\"ZFenOcEe\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1731405657; x=1732010457;\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=fqG1hHrjPaJRskza6EpesAQ3sy6sBqFljv05eEnhOaI=;\n\tb=ZFenOcEeFK+KkNfjfoadZrcTtFuT7aUGUejRB/k/f/wb/l2tseYjfMvbaWWoPXU+pb\n\tc8/oQ922cXRBf7fvv45VdzgYmHy8PchmdT91JysI3G76RHbuxdN5fzIbL/SfC02j53mN\n\tRSna095iSSt2qw0x25Z101+7XVJnUuO3xBgWE=",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1731405657; x=1732010457;\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=fqG1hHrjPaJRskza6EpesAQ3sy6sBqFljv05eEnhOaI=;\n\tb=dloHWTvXcLN4xwmw40oELj1eNPf+/cjp4J3Wh6NQTY2cOV0bNQpweNinT3Iam6nv9g\n\tup10Z6LQuDWP5RdqwcPduy86loRbzwyLvCIMqIjPI34WqxiS9sgmNwfT7ouA5jpsBszr\n\tyvwDMRx6pFkF/8SRwu9Q5XKbmvknIXCmM+E7XJ3P8V00COcyE8QAVKzz7lRqjj+6ETHz\n\tuIS8Dc1bDWp+dfmGfGQI/ZWlAQd2MFIjkj2Q4krQITGsP9aepbcBo75/WSfMxIfq9aQV\n\tGEwEXPh/pz1+AMglGwTcriiMRdmCZYt7dBxEBerwHTgP08i133ujxL6AUu+oNuej72b9\n\tJsmw==",
        "X-Gm-Message-State": "AOJu0YzVSu7r1q4XU1DS2mv42wUJYFV39PZ1TI4Z54GOIKd7QSHWhRnP\n\tRZi/me2+LFN3QjRPrdtUVKEcIfUsqhHMKdr9hCBDDllH0sDYNJUtEk298o87TMlCNSlK3CiAP+E\n\t=",
        "X-Google-Smtp-Source": "AGHT+IE0z8wmNyqEXJN0RGWPku13X9o8Gndfqj464k+tL3z2Ml5PiHYia1o/Z9qYeJAMBVg2Hb7fjQ==",
        "X-Received": "by 2002:a05:6e02:198f:b0:3a6:b1b0:6799 with SMTP id\n\te9e14a558f8ab-3a6f19e38e6mr150463765ab.10.1731405657454; \n\tTue, 12 Nov 2024 02:00:57 -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 1/2] DmaBufAllocator: Add Dma Buffer synchronization function\n\t& helper class",
        "Date": "Tue, 12 Nov 2024 09:56:54 +0000",
        "Message-ID": "<20241112100051.4071443-2-chenghaoyang@chromium.org>",
        "X-Mailer": "git-send-email 2.47.0.277.g8800431eea-goog",
        "In-Reply-To": "<20241112100051.4071443-1-chenghaoyang@chromium.org>",
        "References": "<20241112100051.4071443-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           | 88 +++++++++++++++++++\n 2 files changed, 121 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..3ae98c168 100644\n--- a/src/libcamera/dma_buf_allocator.cpp\n+++ b/src/libcamera/dma_buf_allocator.cpp\n@@ -78,6 +78,79 @@ 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 rad andd 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\tLOG(DmaBufAllocator, Error) << \"Unable to sync dma fd: \" << fd\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 +278,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": [
        "1/2"
    ]
}