From patchwork Tue May 16 08:03:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 18637 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 F3461C3284 for ; Tue, 16 May 2023 08:05:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3260162865; Tue, 16 May 2023 10:05:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684224311; bh=hp6AasDtyYXfBbyBlBaboF2P3cxvD6pPwbnACzMoONU=; 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=QUqjfzIHgiRWGX62z6r7zjUCa4ifdR0GKYwcB8KGQ87QU+uL7BukkXcA6oguHNUpS 3xcswOw3h2ymfKJHav6fTRN5ed7l/h/eYiX9vUJYzzj7Icl8EeuiprV9le35owO2+F fSUVgJHmHFGfWbfTyg4dND18bhTxwF7ckAFZVS7j6v/PBWf0orM8bYmjZXYWtHASbI pbDOfk8Mu4qPv/Vz9hi0g9Pp5vMtGWmP5VToeWZOCu+hfLTouq8mjratRYz5NLlLdX NCH/RmX6EFO+OXIrk8qm/bKdYLqwfrAV9QwNJ8lQ1iJtQ7Jt391gXicJOD7/SyTFi3 X/UP7gNghNbdQ== Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4F08E627DC for ; Tue, 16 May 2023 10:05:09 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="lNy+pn7N"; dkim-atps=neutral Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-52cb78647ecso8702367a12.1 for ; Tue, 16 May 2023 01:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684224307; x=1686816307; 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=3fBg1wkczRxbLUVCBAHdLdU8OPNKRAjOXjhoze11edA=; b=lNy+pn7N9zfLZEN701Mqna66VxB3bIkAUOd1BkOMiQex2nmlew3lRrmQfLjDz2R6iS ErimenBtee8ZhMrNOXLoyY9jH9PnEPiNLFtUWzqS+9HloKaJ2aSW3YGcp13V9QEKEjlu ew1YbII5EYd9fSNh+37TbI5bdz2vxIPQLOLfY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684224307; x=1686816307; 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=3fBg1wkczRxbLUVCBAHdLdU8OPNKRAjOXjhoze11edA=; b=DBRvrlqC6HyMEA6T/Ml9BSu+o4z9Gws+RPQENNvHSQraHKUYpYGm3ixMiiUkTscsZ3 GNd8CuAqAN2dY7Zetf+t5HtqaJXvZcKl90aSNmal82DivdWMG1zEId3AwQcFG+p/kXAy b8MVWXp/e6niR9JjTnTv6PWxRkNt46leRa+fXWsG7mYIgRth30wgfAACDIME3S3OO/Od OFNTB1aPiNd6zTRxSQjrxSrflaFguSqgI3CQABr2JyvJqQFQ9KYrZ91FEKCq3JmvnS/1 igkT6sGWGM4sZ+4pIYYom9MUb1aMLjGj4D27GrwyD3D8scu5H+vuSpvWy7u+evzqxC2U evXg== X-Gm-Message-State: AC+VfDxpIarxORDQ3MOvbgu5ZCQ/1VWzS+0QsNw/LT8jbVpzGvd+5S3o Ok5dZZd2RZwdAIPgObPnYkioEDzXCUDMqb/3m3MdCg== X-Google-Smtp-Source: ACHHUZ6Yc11jgLnWuruTCJojhSH2lrQwguDW6Bn5NnOgwMMjK9LqunMXye3FZNdaYGd62XVSM93PgQ== X-Received: by 2002:a17:90a:880b:b0:250:484e:355f with SMTP id s11-20020a17090a880b00b00250484e355fmr32134459pjn.49.1684224307625; Tue, 16 May 2023 01:05:07 -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 kd14-20020a17090b348e00b00252df595c01sm996881pjb.52.2023.05.16.01.05.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 01:05:07 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 16 May 2023 08:03:18 +0000 Message-ID: <20230516080459.711347-3-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230516080459.711347-1-chenghaoyang@google.com> References: <20230516080459.711347-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 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: Cheng-Hao Yang Reply-To: Harvey Yang Cc: Cheng-Hao Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Cheng-Hao Yang If DmaHeap is not valid, fall back to UdmaHeap to allocate buffers. Signed-off-by: Harvey Yang Reviewed-by: Kieran Bingham --- src/libcamera/heap_allocator.cpp | 106 +++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index e9476de5..682a7e01 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -10,10 +10,14 @@ #include #include #include +#include +#include +#include #include #include #include +#include #include @@ -52,6 +56,14 @@ public: UniqueFD alloc(const char *name, std::size_t size) override; }; +class UdmaHeap : public Heap +{ +public: + UdmaHeap(); + ~UdmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + DmaHeap::DmaHeap() { for (const char *name : dmaHeapNames) { @@ -103,9 +115,103 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size) return allocFd; } +UdmaHeap::UdmaHeap() +{ + int ret = ::open("/dev/udmabuf", O_RDWR, 0); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to open allocator: " << strerror(ret); + + if (ret == EACCES) { + LOG(HeapAllocator, Info) + << "UdmaHeap: Consider making /dev/udmabuf accessible by the video group"; + LOG(HeapAllocator, Info) + << "UdmaHeap: Alternatively, add your user to the kvm group."; + } + + } else { + handle_ = UniqueFD(ret); + } +} + +UdmaHeap::~UdmaHeap() = default; + +UniqueFD UdmaHeap::alloc(const char *name, std::size_t size) +{ + if (!isValid()) { + LOG(HeapAllocator, Fatal) << "UdmaHeap: Allocation attempted without allocator" << name; + return {}; + } + + int ret; + + 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 {}; + } + + /* The underlying memfd is kept as as a reference in the kernel */ + UniqueFD uDma(ret); + + if (create.size != size) + LOG(HeapAllocator, Warning) + << "UdmaHeap allocated " << create.size << " bytes instead of " + << size << " bytes"; + + /* Fail if not suitable, the allocation will be free'd by UniqueFD */ + if (create.size < size) + return {}; + + LOG(HeapAllocator, Debug) << "UdmaHeap allocated " << create.size << " bytes"; + + return uDma; +} + HeapAllocator::HeapAllocator() { heap_ = std::make_unique(); + if (!isValid()) + heap_ = std::make_unique(); } HeapAllocator::~HeapAllocator() = default;