From patchwork Wed Aug 2 06:51:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18922 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 C429FBDCBF for ; Wed, 2 Aug 2023 06:55:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 73689627EB; Wed, 2 Aug 2023 08:55:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1690959305; bh=vqo7+QhOPbQhtAzNZWEISoB9rGYX5iYooUeCBKzyYQM=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=jN6N73YrgouZJ4pURwJWv8rEPfUq0aS1PZwlRmDA5eV5WgrzGTNOK9qKdIo4Zmkmi /DIfWWC8K9snJcMKCZTpFDI7b3l1wvG/WeaA+GjLC4aoRPT903TGiacn4VWodxR6U2 UAiXRBHj4Cz2wJFr/JjXDs8aBUO67Ikf/yXrRmn7hvjarkXGzuxYyDAr4FYnfmNl53 1httRxm470tuXZ5igu2qa/q4SuDRJhkwNiq5Lbg3l8xLXSsc/4PM4nqBJf08twiRGr aXs7UYpo0LCBvc3O5BSdYFAU1RmETbMpXRtvm6jJAK/eWgmB650eoGw1vtCCjZXAMi hHjKBrwRCjjlg== Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 856AB627EE for ; Wed, 2 Aug 2023 08:55:01 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="YDS/NHiV"; dkim-atps=neutral Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-686ba29ccb1so410645b3a.1 for ; Tue, 01 Aug 2023 23:55:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690959299; x=1691564099; 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=of3V24Swk733umJsa+sZgpEEgBy/JwWUEV6egzUd+Dc=; b=YDS/NHiVK0uVbk7YW+LnucSteEdZJa+RShDttUQT2dO9q/1D2ySumgKfuKSJN7gtAz E/zLlc3V3nZHGqCTCSCk9qD0c4lon0RX8e8vUIrWBAE19eX/R9v3yDMREoIYa51dtEYs bEThX44BKrNHrUxA9VkJXLbE6N4ezH74YIoZ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690959299; x=1691564099; 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=of3V24Swk733umJsa+sZgpEEgBy/JwWUEV6egzUd+Dc=; b=PXUwPrwcb9EynKMtTRQZ3BqbgstkOytvPeZAeDoK4gu55QIT5Yawt+BqW/3tftZ/IN cT7RezMzyw978mKYzNBxLK/Ef24BEhhczSg6B86WGdBvy2H+d2yarogNkPQUpFtjj9De 78oRr+9sBZ3WSsVFkTWEsCCV7ujns4cerLH8GSEOxUDvWd8Tqx/+gpsJJG0+rtpr2x2T QQnd80t3/KoDYtZCyeLamI9gMIcvXhJVDvOdDHC9KL5HiMEtivlWgaG6movNTcX+dijC u5O7vhmT7BdWQPDA2fopYrdwCx0bumAJ22ElSRC72BRBlD6uz+fN0921/ZQv5EGXLxGt 2UFg== X-Gm-Message-State: ABy/qLZE4qcZb1HB7TQqp4JvNbjHBy110cu3DK1etpqiQ7t8eTLWA9pJ Qpglo9MarAdYIIN1p9DqoEW2HjRWjLQcJy4X72E= X-Google-Smtp-Source: APBJJlHDuoDsIoW7ncjvKQV4ldP9pVHlqq+5Yt6U8p5kIzXOc4rkQlro/cZVRCc7tjL4Eomv+0epLg== X-Received: by 2002:a05:6a00:2e8d:b0:667:d0ff:6a0f with SMTP id fd13-20020a056a002e8d00b00667d0ff6a0fmr18247228pfb.5.1690959299430; Tue, 01 Aug 2023 23:54:59 -0700 (PDT) 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 d14-20020aa7868e000000b0065980654baasm9142154pfo.130.2023.08.01.23.54.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 23:54:59 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 2 Aug 2023 06:51:44 +0000 Message-ID: <20230802065449.2904457-3-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog In-Reply-To: <20230802065449.2904457-1-chenghaoyang@chromium.org> References: <20230802065449.2904457-1-chenghaoyang@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 2/3] libcamera: Add UdmaHeap if DmaHeap is not valid 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: , X-Patchwork-Original-From: Harvey Yang via libcamera-devel From: Harvey Yang Reply-To: Harvey Yang Cc: Harvey Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" If DmaHeap is not valid, fall back to UdmaHeap to allocate buffers. Signed-off-by: Harvey Yang --- src/libcamera/heap_allocator.cpp | 105 +++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 2a825d93..1caa1469 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -11,10 +11,14 @@ #include #include #include +#include +#include +#include #include #include #include +#include #include @@ -84,6 +88,18 @@ public: UniqueFD alloc(const char *name, std::size_t size) override; }; +/** + * \class UdmaHeap + * \brief The derived class of Heap with Udmabuf implementation + */ +class UdmaHeap : public Heap +{ +public: + UdmaHeap(); + ~UdmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + /** * \brief Construct a DmaHeap with a list of |dmaHeapNames| */ @@ -98,6 +114,7 @@ DmaHeap::DmaHeap() continue; } + LOG(HeapAllocator, Info) << "Using DmaHeap allocator"; handle_ = UniqueFD(ret); break; } @@ -138,6 +155,92 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size) return allocFd; } +/** + * \brief Construct a UdmaHeap with `/udev/udmabuf`. + */ +UdmaHeap::UdmaHeap() +{ + int ret = ::open("/dev/udmabuf", O_RDWR); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to open allocator: " << strerror(ret); + return; + } + + LOG(HeapAllocator, Info) << "Using UdmaHeap allocator"; + handle_ = UniqueFD(ret); +} + +UdmaHeap::~UdmaHeap() = default; + +UniqueFD UdmaHeap::alloc(const char *name, std::size_t size) +{ + int ret = memfd_create(name, MFD_ALLOW_SEALING); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to allocate memfd storage: " + << strerror(ret); + return {}; + } + + UniqueFD memfd(ret); + + ret = ftruncate(memfd.get(), size); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to set memfd size: " << strerror(ret); + return {}; + } + + /* UdmaHeap Buffers *must* have the F_SEAL_SHRINK seal */ + ret = fcntl(memfd.get(), F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to seal the memfd: " << strerror(ret); + return {}; + } + + struct udmabuf_create create; + + create.memfd = memfd.get(); + create.flags = UDMABUF_FLAGS_CLOEXEC; + create.offset = 0; + create.size = size; + + ret = ::ioctl(handle_.get(), UDMABUF_CREATE, &create); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to allocate " << size << " bytes: " + << strerror(ret); + return {}; + } + + if (create.size < size) { + LOG(HeapAllocator, Error) + << "UdmaHeap allocated " << create.size << " bytes instead of " + << size << " bytes"; + return {}; + } + + if (create.size != size) + LOG(HeapAllocator, Warning) + << "UdmaHeap allocated " << create.size << " bytes, " + << "which is greater than requested : " << size << " bytes"; + + /* Fail if not suitable, the allocation will be free'd by UniqueFD */ + LOG(HeapAllocator, Debug) << "UdmaHeap allocated " << create.size << " bytes"; + + /* The underlying memfd is kept as as a reference in the kernel */ + UniqueFD uDma(ret); + + return uDma; +} + /** * \class HeapAllocator * \brief The allocator that allocates heap buffers @@ -160,6 +263,8 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size) HeapAllocator::HeapAllocator() { heap_ = std::make_unique(); + if (!isValid()) + heap_ = std::make_unique(); } HeapAllocator::~HeapAllocator() = default;