From patchwork Tue Nov 12 09:56:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21875 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 22E11C324C for ; Tue, 12 Nov 2024 10:01:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 50F39657EB; Tue, 12 Nov 2024 11:01:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="ZFenOcEe"; dkim-atps=neutral Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4A0F8657DF for ; Tue, 12 Nov 2024 11:00:59 +0100 (CET) Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-3a6bdc8f7a1so20639655ab.1 for ; Tue, 12 Nov 2024 02:00:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731405657; x=1732010457; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fqG1hHrjPaJRskza6EpesAQ3sy6sBqFljv05eEnhOaI=; b=ZFenOcEeFK+KkNfjfoadZrcTtFuT7aUGUejRB/k/f/wb/l2tseYjfMvbaWWoPXU+pb c8/oQ922cXRBf7fvv45VdzgYmHy8PchmdT91JysI3G76RHbuxdN5fzIbL/SfC02j53mN RSna095iSSt2qw0x25Z101+7XVJnUuO3xBgWE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731405657; x=1732010457; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fqG1hHrjPaJRskza6EpesAQ3sy6sBqFljv05eEnhOaI=; b=dloHWTvXcLN4xwmw40oELj1eNPf+/cjp4J3Wh6NQTY2cOV0bNQpweNinT3Iam6nv9g up10Z6LQuDWP5RdqwcPduy86loRbzwyLvCIMqIjPI34WqxiS9sgmNwfT7ouA5jpsBszr yvwDMRx6pFkF/8SRwu9Q5XKbmvknIXCmM+E7XJ3P8V00COcyE8QAVKzz7lRqjj+6ETHz uIS8Dc1bDWp+dfmGfGQI/ZWlAQd2MFIjkj2Q4krQITGsP9aepbcBo75/WSfMxIfq9aQV GEwEXPh/pz1+AMglGwTcriiMRdmCZYt7dBxEBerwHTgP08i133ujxL6AUu+oNuej72b9 Jsmw== X-Gm-Message-State: AOJu0YzVSu7r1q4XU1DS2mv42wUJYFV39PZ1TI4Z54GOIKd7QSHWhRnP RZi/me2+LFN3QjRPrdtUVKEcIfUsqhHMKdr9hCBDDllH0sDYNJUtEk298o87TMlCNSlK3CiAP+E = X-Google-Smtp-Source: AGHT+IE0z8wmNyqEXJN0RGWPku13X9o8Gndfqj464k+tL3z2Ml5PiHYia1o/Z9qYeJAMBVg2Hb7fjQ== X-Received: by 2002:a05:6e02:198f:b0:3a6:b1b0:6799 with SMTP id e9e14a558f8ab-3a6f19e38e6mr150463765ab.10.1731405657454; Tue, 12 Nov 2024 02:00:57 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f644214sm10157673a12.58.2024.11.12.02.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 02:00:57 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH 1/2] DmaBufAllocator: Add Dma Buffer synchronization function & 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 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" To synchronize CPU access with mmap and hardware access on DMA buffers, using `DMA_BUF_IOCTL_SYNC` is required. This patch adds a function and a helper class to allow users to sync buffers more easily. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang --- .../libcamera/internal/dma_buf_allocator.h | 33 +++++++ src/libcamera/dma_buf_allocator.cpp | 88 +++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index d2a0a0d19..2b9b99678 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -23,6 +23,19 @@ public: using DmaBufAllocatorFlags = Flags; + enum class SyncStep { + Start = 0, + End + }; + + enum class SyncType { + Read = 0, + Write, + ReadWrite, + }; + + static void sync(int fd, SyncStep step, SyncType type); + DmaBufAllocator(DmaBufAllocatorFlags flags = DmaBufAllocatorFlag::CmaHeap); ~DmaBufAllocator(); bool isValid() const { return providerHandle_.isValid(); } @@ -35,6 +48,26 @@ private: DmaBufAllocatorFlag type_; }; +class DmaSyncer final +{ +public: + explicit DmaSyncer(int fd, + DmaBufAllocator::SyncType type = DmaBufAllocator::SyncType::ReadWrite) + : fd_(fd), type_(type) + { + DmaBufAllocator::sync(fd_, DmaBufAllocator::SyncStep::Start, type_); + } + + ~DmaSyncer() + { + DmaBufAllocator::sync(fd_, DmaBufAllocator::SyncStep::End, type_); + } + +private: + int fd_; + DmaBufAllocator::SyncType type_; +}; + LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaBufAllocator::DmaBufAllocatorFlag) } /* namespace libcamera */ diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp index be6efb89f..3ae98c168 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -78,6 +78,79 @@ LOG_DEFINE_CATEGORY(DmaBufAllocator) * \brief A bitwise combination of DmaBufAllocator::DmaBufAllocatorFlag values */ +/** + * \enum DmaBufAllocator::SyncStep + * \brief Either start or end of the synchronization + * \var DmaBufAllocator::Start + * \brief Indicates the start of a map access session + * \var DmaBufAllocator::End + * \brief Indicates the end of a map access session + */ + +/** + * \enum DmaBufAllocator::SyncType + * \brief Read and/or write access via the CPU map + * \var DmaBufAllocator::Read + * \brief Indicates that the mapped dma-buf will be read by the client via the + * CPU map + * \var DmaBufAllocator::Write + * \brief Indicates that the mapped dm-buf will be written by the client via the + * CPU map + * \var DmaBufAllocator::ReadWrite + * \brief Indicates that the mapped dma-buf will be rad andd written by the + * client via the CPU map + */ + +/** + * \brief Synchronize CPU access and hardware access + * \param[in] fd The dma-buf's file descriptor to be synchronized + * \param[in] step Either start or end of the synchronization + * \param[in] type Read and/or write access during CPU mmap + * + * The client is expected to call this function with + * DmaBufAllocator::SyncStep::Start and DmaBufAllocator::SyncStep::End at the + * start and end of buffer access respectively. + */ +void DmaBufAllocator::sync(int fd, DmaBufAllocator::SyncStep step, DmaBufAllocator::SyncType type) +{ + uint64_t flags = 0; + switch (step) { + case DmaBufAllocator::SyncStep::Start: + flags = DMA_BUF_SYNC_START; + break; + case DmaBufAllocator::SyncStep::End: + flags = DMA_BUF_SYNC_END; + break; + } + + switch (type) { + case DmaBufAllocator::SyncType::Read: + flags = flags | DMA_BUF_SYNC_READ; + break; + case DmaBufAllocator::SyncType::Write: + flags = flags | DMA_BUF_SYNC_WRITE; + break; + case DmaBufAllocator::SyncType::ReadWrite: + flags = flags | DMA_BUF_SYNC_RW; + break; + } + + struct dma_buf_sync sync = { + .flags = flags + }; + + int ret; + do { + ret = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync); + } while (!ret && (errno == EINTR || errno == EAGAIN)); + + if (ret) { + LOG(DmaBufAllocator, Error) << "Unable to sync dma fd: " << fd + << ", step: " << static_cast(step) + << ", type: " << static_cast(type); + } +} + /** * \brief Construct a DmaBufAllocator of a given type * \param[in] type The type(s) of the dma-buf providers to allocate from @@ -205,4 +278,19 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size) return allocFromHeap(name, size); } +/** + * \class DmaSyncer + * \brief Helper class for dma-buf's synchronization + * + * This class wraps a userspace dma-buf's synchronization process with an + * object's lifetime. + */ + +/** + * \fn DmaSyncer::DmaSyncer(int fd, DmaBufAllocator::SyncType type = DmaBufAllocator::SyncType::ReadWrite) + * \brief Construct a DmaSyncer with a dma-buf's fd and the access type + * \param[in] fd The dma-buf's file descriptor to synchronize + * \param[in] type Read and/or write access via the CPU map + */ + } /* namespace libcamera */