From patchwork Tue Feb 14 09:37:35 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: 18282 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 C612CBDB1C for ; Tue, 14 Feb 2023 09:37:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7E4EB625F1; Tue, 14 Feb 2023 10:37:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1676367469; bh=LyDwwOzJh3NNCme0YWpxnLmWxx2LE7Yt59dY93zJl9s=; 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=WA27QZVxhPhDuxkv4lBoHPwZVQFWtI60+F38CCDMEQHG28SDfTgZpAQCes5Zqm634 8B7503Qjl4E2HtOrkKRYB4LKLg1InMG43tcwQvLx2sTOE4iH7BLmfj30Bg9gizng2U SukHRtUF5uGZTyhyp5N2IoHGgjtLbnxnSlXBLIWEwX+QeBaZdK4xNEFzGgfL6Bc23A B4OAvjLoh0h09VRZNgZ5Z9T9AbKtZteX0Sn0C8UgP79vQaw6pCMqS0pOJTAyi43DjX oJSljEIwutQ1TD0PUCRSjsz9BBnAOuUxcXjip7EkRsCQ4NDOnaoutG07+xKOkeBD0G 9ghmh8o1EFLZg== Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E081625F8 for ; Tue, 14 Feb 2023 10:37:45 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="MEy2Ru6C"; dkim-atps=neutral Received: by mail-pj1-x1036.google.com with SMTP id bg2so5251136pjb.4 for ; Tue, 14 Feb 2023 01:37:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=QpaG0t6PXjS7w3qMEAfXxXkv8IRrMJWTik1fs5WR5l8=; b=MEy2Ru6Ci/YG5rNw1dZcFRUbT4t313s+HL+XLrfNwIFVfU3UsUMJXkMuRB4QRswhxn 90Eg3V2tCyBo+X9Or42W5XSST5yb1d3RCRMur1oEqGA4MY2X9JMWlRbaP4s7x5I9Sv2X 85H8rwpCDSrkw/ChCPps/hxORK1mrlE4zrCpE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=QpaG0t6PXjS7w3qMEAfXxXkv8IRrMJWTik1fs5WR5l8=; b=NV7MveF0mYoTy8MWJ69w3191fl1/ztNkMZgXOSjmT/q11yPEuEpM2c9AsDhbMp3f8b d3YNUrtW0RCMj+x9ON7uN+6zS70AudvuJ+6gH5pJMN/bSR/erMkWB2LkgQLPlQQpN+UD GC2OGzqwzgTk7bA1GXkqrj/scDPhKGnXFSqHm8QzMSONIkWX9pCapOPtL6/CP84sJsFZ +umHOE+lAB4lVtDSGwQIgAerkTkJFdXn/u5DggiLQ3G0YMzoCyXzNMsau1qm3boOZ56C EVbojD6akqBkw9ne+WLQCGm2LdWVZeMaFz0tQfdapKGqKvVVvSf4K/xXVp150e9Wc5BI y4Og== X-Gm-Message-State: AO0yUKUo42nx+FcVdzjk6h62dcrzbBaUFVh/D4q0rQeqWw6DtbgLqDcG ugeQp9cIIttDmVhaUB756nrZB5VoN8FTxJxe X-Google-Smtp-Source: AK7set+zE1oZCSx/fHSsqM4ZfcLSZJbK896BMsr4dcrf3z1EU+pFRk09UZXeqV7p0DC36JDkVf6O0w== X-Received: by 2002:a05:6a20:691a:b0:be:e0c3:5003 with SMTP id q26-20020a056a20691a00b000bee0c35003mr1992302pzj.5.1676367463423; Tue, 14 Feb 2023 01:37:43 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (46.165.189.35.bc.googleusercontent.com. [35.189.165.46]) by smtp.gmail.com with ESMTPSA id z9-20020aa791c9000000b0059416691b64sm9696172pfa.19.2023.02.14.01.37.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 01:37:43 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Feb 2023 09:37:35 +0000 Message-Id: <20230214093736.4067033-3-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog In-Reply-To: <20230214093736.4067033-1-chenghaoyang@google.com> References: <20230214093736.4067033-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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: Harvey Yang , 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 --- include/libcamera/meson.build | 1 + include/libcamera/udma_heap.h | 20 ++++++ src/libcamera/heap_allocator.cpp | 3 + src/libcamera/meson.build | 1 + src/libcamera/udma_heap.cpp | 117 +++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 include/libcamera/udma_heap.h create mode 100644 src/libcamera/udma_heap.cpp diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index f486630a..a1414a57 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -19,6 +19,7 @@ libcamera_public_headers = files([ 'request.h', 'stream.h', 'transform.h', + 'udma_heap.h', ]) subdir('base') diff --git a/include/libcamera/udma_heap.h b/include/libcamera/udma_heap.h new file mode 100644 index 00000000..3ae46dc7 --- /dev/null +++ b/include/libcamera/udma_heap.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Ideas on Board Oy + * + * udma_heap.h - Udma Heap implementation. + */ + +#include + +namespace libcamera { + +class UdmaHeap : public Heap +{ +public: + UdmaHeap(); + ~UdmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 594b1d6a..179c2c21 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -18,6 +18,7 @@ #include #include +#include namespace libcamera { @@ -26,6 +27,8 @@ LOG_DEFINE_CATEGORY(HeapAllocator) HeapAllocator::HeapAllocator() { heap_ = std::make_unique(); + if (!isValid()) + heap_ = std::make_unique(); } HeapAllocator::~HeapAllocator() = default; diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index ee586c0d..bce26d4b 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -45,6 +45,7 @@ libcamera_sources = files([ 'stream.cpp', 'sysfs.cpp', 'transform.cpp', + 'udma_heap.cpp', 'v4l2_device.cpp', 'v4l2_pixelformat.cpp', 'v4l2_subdevice.cpp', diff --git a/src/libcamera/udma_heap.cpp b/src/libcamera/udma_heap.cpp new file mode 100644 index 00000000..050ab80f --- /dev/null +++ b/src/libcamera/udma_heap.cpp @@ -0,0 +1,117 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Ideas on Board Oy + * + * dma_heap.h - Dma Heap implementation. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace libcamera { + +LOG_DEFINE_CATEGORY(UdmaHeap) + +UdmaHeap::UdmaHeap() +{ + int ret = ::open("/dev/udmabuf", O_RDWR, 0); + if (ret < 0) { + ret = errno; + LOG(UdmaHeap, Error) + << "Failed to open allocator: " << strerror(ret); + + if (ret == EACCES) { + LOG(UdmaHeap, Info) + << "Consider making /dev/udmabuf accessible by the video group"; + LOG(UdmaHeap, Info) + << "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(UdmaHeap, Fatal) << "Allocation attempted without allocator" << name; + return {}; + } + + int ret; + + ret = memfd_create(name, MFD_ALLOW_SEALING); + if (ret < 0) { + ret = errno; + LOG(UdmaHeap, Error) + << "Failed to allocate memfd storage: " + << strerror(ret); + return {}; + } + + UniqueFD memfd(ret); + + ret = ftruncate(memfd.get(), size); + if (ret < 0) { + ret = errno; + LOG(UdmaHeap, Error) + << "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(UdmaHeap, Error) + << "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(UdmaHeap, Error) + << "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(UdmaHeap, Warning) + << "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(UdmaHeap, Debug) << "Allocated " << create.size << " bytes"; + + return uDma; +} + +} /* namespace libcamera */