[{"id":26521,"web_url":"https://patchwork.libcamera.org/comment/26521/","msgid":"<CAEB1ahvoF=Y807_N6Vc0gMhYOF+iO=5s5nB3J1h=0O2U=uiYFA@mail.gmail.com>","date":"2023-03-02T08:25:00","subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/people/117/","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"content":"Hi Kieran,\n\nSorry that I'm new to dma heap: I was trying to test virtual pipeline\nhandler on dma heap\n(or the udma heap I add in the following patch), while I find that my\nworkstation doesn't\nhave the dma heap directory (`/dev/dma_heap` or `/dev/udmabuf`). I heard\nfrom Han-lin\nthat we might need to enable it in the linux kernel. Is that right? Is\nthere a doc/tutorial that\nI can follow?\n\nThanks!\nHarvey\n\n\n\nOn Thu, Mar 2, 2023 at 4:13 PM Harvey Yang <chenghaoyang@chromium.org>\nwrote:\n\n> As other components (like the WIP virtual pipeline handler) also need a\n> heap allocator, move DmaHeap from raspberry pi pipeline handler to a\n> general HeapAllocator as a base class.\n>\n> Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> ---\n>  include/libcamera/dma_heap.h                  | 20 +++++++++\n>  include/libcamera/heap.h                      | 27 ++++++++++++\n>  include/libcamera/heap_allocator.h            | 30 +++++++++++++\n>  include/libcamera/meson.build                 |  3 ++\n>  .../dma_heaps.cpp => dma_heap.cpp}            | 35 +++++++--------\n>  src/libcamera/heap_allocator.cpp              | 43 +++++++++++++++++++\n>  src/libcamera/meson.build                     |  2 +\n>  .../pipeline/raspberrypi/dma_heaps.h          | 32 --------------\n>  .../pipeline/raspberrypi/meson.build          |  1 -\n>  .../pipeline/raspberrypi/raspberrypi.cpp      | 10 ++---\n>  10 files changed, 146 insertions(+), 57 deletions(-)\n>  create mode 100644 include/libcamera/dma_heap.h\n>  create mode 100644 include/libcamera/heap.h\n>  create mode 100644 include/libcamera/heap_allocator.h\n>  rename src/libcamera/{pipeline/raspberrypi/dma_heaps.cpp => dma_heap.cpp}\n> (69%)\n>  create mode 100644 src/libcamera/heap_allocator.cpp\n>  delete mode 100644 src/libcamera/pipeline/raspberrypi/dma_heaps.h\n>\n> diff --git a/include/libcamera/dma_heap.h b/include/libcamera/dma_heap.h\n> new file mode 100644\n> index 00000000..a663c317\n> --- /dev/null\n> +++ b/include/libcamera/dma_heap.h\n> @@ -0,0 +1,20 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2020, Raspberry Pi Ltd\n> + *\n> + * dma_heap.h - Dma Heap implementation.\n> + */\n> +\n> +#include <libcamera/heap.h>\n> +\n> +namespace libcamera {\n> +\n> +class DmaHeap : public Heap\n> +{\n> +public:\n> +       DmaHeap();\n> +       ~DmaHeap();\n> +       UniqueFD alloc(const char *name, std::size_t size) override;\n> +};\n> +\n> +} /* namespace libcamera */\n> diff --git a/include/libcamera/heap.h b/include/libcamera/heap.h\n> new file mode 100644\n> index 00000000..c49a2ac3\n> --- /dev/null\n> +++ b/include/libcamera/heap.h\n> @@ -0,0 +1,27 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2023, Google Inc.\n> + *\n> + * heap.h - Heap interface.\n> + */\n> +\n> +#pragma once\n> +\n> +#include <stddef.h>\n> +\n> +#include <libcamera/base/unique_fd.h>\n> +\n> +namespace libcamera {\n> +\n> +class Heap\n> +{\n> +public:\n> +       virtual ~Heap() = default;\n> +       bool isValid() const { return handle_.isValid(); }\n> +       virtual UniqueFD alloc(const char *name, std::size_t size) = 0;\n> +\n> +protected:\n> +       UniqueFD handle_;\n> +};\n> +\n> +} /* namespace libcamera */\n> diff --git a/include/libcamera/heap_allocator.h\n> b/include/libcamera/heap_allocator.h\n> new file mode 100644\n> index 00000000..cd7ed1a3\n> --- /dev/null\n> +++ b/include/libcamera/heap_allocator.h\n> @@ -0,0 +1,30 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2023, Google Inc.\n> + *\n> + * heap_allocator.h - Helper class for heap buffer allocations.\n> + */\n> +\n> +#pragma once\n> +\n> +#include <stddef.h>\n> +\n> +#include <libcamera/base/unique_fd.h>\n> +\n> +#include <libcamera/heap.h>\n> +\n> +namespace libcamera {\n> +\n> +class HeapAllocator\n> +{\n> +public:\n> +       HeapAllocator();\n> +       ~HeapAllocator();\n> +       bool isValid() const { return heap_->isValid(); }\n> +       UniqueFD alloc(const char *name, std::size_t size);\n> +\n> +private:\n> +       std::unique_ptr<Heap> heap_;\n> +};\n> +\n> +} /* namespace libcamera */\n> diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\n> index 408b7acf..f486630a 100644\n> --- a/include/libcamera/meson.build\n> +++ b/include/libcamera/meson.build\n> @@ -7,10 +7,13 @@ libcamera_public_headers = files([\n>      'camera_manager.h',\n>      'color_space.h',\n>      'controls.h',\n> +    'dma_heap.h',\n>      'fence.h',\n>      'framebuffer.h',\n>      'framebuffer_allocator.h',\n>      'geometry.h',\n> +    'heap.h',\n> +    'heap_allocator.h',\n>      'logging.h',\n>      'pixel_format.h',\n>      'request.h',\n> diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> b/src/libcamera/dma_heap.cpp\n> similarity index 69%\n> rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> rename to src/libcamera/dma_heap.cpp\n> index 6b644406..02415975 100644\n> --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> +++ b/src/libcamera/dma_heap.cpp\n> @@ -2,18 +2,19 @@\n>  /*\n>   * Copyright (C) 2020, Raspberry Pi Ltd\n>   *\n> - * dma_heaps.h - Helper class for dma-heap allocations.\n> + * dma_heap.h - Dma Heap implementation.\n>   */\n>\n> -#include \"dma_heaps.h\"\n> +#include <libcamera/dma_heap.h>\n>\n>  #include <array>\n>  #include <fcntl.h>\n> -#include <linux/dma-buf.h>\n> -#include <linux/dma-heap.h>\n>  #include <sys/ioctl.h>\n>  #include <unistd.h>\n>\n> +#include <linux/dma-buf.h>\n> +#include <linux/dma-heap.h>\n> +\n>  #include <libcamera/base/log.h>\n>\n>  /*\n> @@ -30,9 +31,7 @@ static constexpr std::array<const char *, 2> heapNames =\n> {\n>\n>  namespace libcamera {\n>\n> -LOG_DECLARE_CATEGORY(RPI)\n> -\n> -namespace RPi {\n> +LOG_DEFINE_CATEGORY(DmaHeap)\n>\n>  DmaHeap::DmaHeap()\n>  {\n> @@ -40,17 +39,17 @@ DmaHeap::DmaHeap()\n>                 int ret = ::open(name, O_RDWR, 0);\n>                 if (ret < 0) {\n>                         ret = errno;\n> -                       LOG(RPI, Debug) << \"Failed to open \" << name << \":\n> \"\n> -                                       << strerror(ret);\n> +                       LOG(DmaHeap, Debug) << \"Failed to open \" << name\n> << \": \"\n> +                                           << strerror(ret);\n>                         continue;\n>                 }\n>\n> -               dmaHeapHandle_ = UniqueFD(ret);\n> +               handle_ = UniqueFD(ret);\n>                 break;\n>         }\n>\n> -       if (!dmaHeapHandle_.isValid())\n> -               LOG(RPI, Error) << \"Could not open any dmaHeap device\";\n> +       if (!handle_.isValid())\n> +               LOG(DmaHeap, Error) << \"Could not open any dmaHeap device\";\n>  }\n>\n>  DmaHeap::~DmaHeap() = default;\n> @@ -67,24 +66,22 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t\n> size)\n>         alloc.len = size;\n>         alloc.fd_flags = O_CLOEXEC | O_RDWR;\n>\n> -       ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n> +       ret = ::ioctl(handle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n>         if (ret < 0) {\n> -               LOG(RPI, Error) << \"dmaHeap allocation failure for \"\n> -                               << name;\n> +               LOG(DmaHeap, Error) << \"dmaHeap allocation failure for \"\n> +                                   << name;\n>                 return {};\n>         }\n>\n>         UniqueFD allocFd(alloc.fd);\n>         ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n>         if (ret < 0) {\n> -               LOG(RPI, Error) << \"dmaHeap naming failure for \"\n> -                               << name;\n> +               LOG(DmaHeap, Error) << \"dmaHeap naming failure for \"\n> +                                   << name;\n>                 return {};\n>         }\n>\n>         return allocFd;\n>  }\n>\n> -} /* namespace RPi */\n> -\n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/heap_allocator.cpp\n> b/src/libcamera/heap_allocator.cpp\n> new file mode 100644\n> index 00000000..594b1d6a\n> --- /dev/null\n> +++ b/src/libcamera/heap_allocator.cpp\n> @@ -0,0 +1,43 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2023, Google Inc.\n> + *\n> + * heap_allocator.cpp - Helper class for heap buffer allocations.\n> + */\n> +\n> +#include <libcamera/heap_allocator.h>\n> +\n> +#include <array>\n> +#include <fcntl.h>\n> +#include <sys/ioctl.h>\n> +#include <unistd.h>\n> +\n> +#include <linux/dma-buf.h>\n> +#include <linux/dma-heap.h>\n> +\n> +#include <libcamera/base/log.h>\n> +\n> +#include <libcamera/dma_heap.h>\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(HeapAllocator)\n> +\n> +HeapAllocator::HeapAllocator()\n> +{\n> +       heap_ = std::make_unique<DmaHeap>();\n> +}\n> +\n> +HeapAllocator::~HeapAllocator() = default;\n> +\n> +UniqueFD HeapAllocator::alloc(const char *name, std::size_t size)\n> +{\n> +       if (!isValid()) {\n> +               LOG(HeapAllocator, Fatal) << \"Allocation attempted without\n> allocator\" << name;\n> +               return {};\n> +       }\n> +\n> +       return heap_->alloc(name, size);\n> +}\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index 9869bfe7..ee586c0d 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -17,11 +17,13 @@ libcamera_sources = files([\n>      'delayed_controls.cpp',\n>      'device_enumerator.cpp',\n>      'device_enumerator_sysfs.cpp',\n> +    'dma_heap.cpp',\n>      'fence.cpp',\n>      'formats.cpp',\n>      'framebuffer.cpp',\n>      'framebuffer_allocator.cpp',\n>      'geometry.cpp',\n> +    'heap_allocator.cpp',\n>      'ipa_controls.cpp',\n>      'ipa_data_serializer.cpp',\n>      'ipa_interface.cpp',\n> diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> deleted file mode 100644\n> index 0a4a8d86..00000000\n> --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> +++ /dev/null\n> @@ -1,32 +0,0 @@\n> -/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> -/*\n> - * Copyright (C) 2020, Raspberry Pi Ltd\n> - *\n> - * dma_heaps.h - Helper class for dma-heap allocations.\n> - */\n> -\n> -#pragma once\n> -\n> -#include <stddef.h>\n> -\n> -#include <libcamera/base/unique_fd.h>\n> -\n> -namespace libcamera {\n> -\n> -namespace RPi {\n> -\n> -class DmaHeap\n> -{\n> -public:\n> -       DmaHeap();\n> -       ~DmaHeap();\n> -       bool isValid() const { return dmaHeapHandle_.isValid(); }\n> -       UniqueFD alloc(const char *name, std::size_t size);\n> -\n> -private:\n> -       UniqueFD dmaHeapHandle_;\n> -};\n> -\n> -} /* namespace RPi */\n> -\n> -} /* namespace libcamera */\n> diff --git a/src/libcamera/pipeline/raspberrypi/meson.build\n> b/src/libcamera/pipeline/raspberrypi/meson.build\n> index 59e8fb18..7322f0e8 100644\n> --- a/src/libcamera/pipeline/raspberrypi/meson.build\n> +++ b/src/libcamera/pipeline/raspberrypi/meson.build\n> @@ -2,7 +2,6 @@\n>\n>  libcamera_sources += files([\n>      'delayed_controls.cpp',\n> -    'dma_heaps.cpp',\n>      'raspberrypi.cpp',\n>      'rpi_stream.cpp',\n>  ])\n> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> index 84120954..b7e0d031 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -21,6 +21,7 @@\n>  #include <libcamera/camera.h>\n>  #include <libcamera/control_ids.h>\n>  #include <libcamera/formats.h>\n> +#include <libcamera/heap_allocator.h>\n>  #include <libcamera/ipa/raspberrypi_ipa_interface.h>\n>  #include <libcamera/ipa/raspberrypi_ipa_proxy.h>\n>  #include <libcamera/logging.h>\n> @@ -44,7 +45,6 @@\n>  #include \"libcamera/internal/yaml_parser.h\"\n>\n>  #include \"delayed_controls.h\"\n> -#include \"dma_heaps.h\"\n>  #include \"rpi_stream.h\"\n>\n>  using namespace std::chrono_literals;\n> @@ -245,7 +245,7 @@ public:\n>         std::vector<std::pair<std::unique_ptr<V4L2Subdevice>, MediaLink\n> *>> bridgeDevices_;\n>\n>         /* DMAHEAP allocation helper. */\n> -       RPi::DmaHeap dmaHeap_;\n> +       HeapAllocator heapAllocator_;\n>         SharedFD lsTable_;\n>\n>         std::unique_ptr<RPi::DelayedControls> delayedCtrls_;\n> @@ -1293,7 +1293,7 @@ int PipelineHandlerRPi::registerCamera(MediaDevice\n> *unicam, MediaDevice *isp, Me\n>  {\n>         std::unique_ptr<RPiCameraData> data =\n> std::make_unique<RPiCameraData>(this);\n>\n> -       if (!data->dmaHeap_.isValid())\n> +       if (!data->heapAllocator_.isValid())\n>                 return -ENOMEM;\n>\n>         MediaEntity *unicamImage = unicam->getEntityByName(\"unicam-image\");\n> @@ -1680,9 +1680,9 @@ int RPiCameraData::configureIPA(const\n> CameraConfiguration *config, ipa::RPi::IPA\n>         /* Always send the user transform to the IPA. */\n>         ipaConfig.transform = static_cast<unsigned int>(config->transform);\n>\n> -       /* Allocate the lens shading table via dmaHeap and pass to the\n> IPA. */\n> +       /* Allocate the lens shading table via heapAllocator and pass to\n> the IPA. */\n>         if (!lsTable_.isValid()) {\n> -               lsTable_ = SharedFD(dmaHeap_.alloc(\"ls_grid\",\n> ipa::RPi::MaxLsGridSize));\n> +               lsTable_ = SharedFD(heapAllocator_.alloc(\"ls_grid\",\n> ipa::RPi::MaxLsGridSize));\n>                 if (!lsTable_.isValid())\n>                         return -ENOMEM;\n>\n> --\n> 2.39.2.722.g9855ee24e9-goog\n>\n>","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 04AA6BE080\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  2 Mar 2023 08:25:15 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 49C92626C0;\n\tThu,  2 Mar 2023 09:25:14 +0100 (CET)","from mail-ua1-x931.google.com (mail-ua1-x931.google.com\n\t[IPv6:2607:f8b0:4864:20::931])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 67CDB6265E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  2 Mar 2023 09:25:12 +0100 (CET)","by mail-ua1-x931.google.com with SMTP id x40so5617469uaf.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 02 Mar 2023 00:25:12 -0800 (PST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1677745514;\n\tbh=gSHF4iwVg8UINywbXeXNKHu4SgEf9kih+MFOyUzEQf0=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=vPX0oy3O0Q6q2JEqR4BQ89M6HJrzZVEk/NzYwahHF1opn4UFK6zEe9v+A1JnShFy5\n\tfxfNOLeCAOqhp5UR2ZzXXC8bq/6zIVpJX+Hl9o7Y2gyr4jd0+WzSUbGckfxTNDWZAE\n\tKNJb+hnum8Z01/GDefj6l4zoINnWo4FzN/FTdPCR1AbjmnRRJrNbKeE3J8cwbdFTKh\n\tS1LCmfYGAea4YjKUg5q2MunGETmh/gBtrI4B9bQa30qXTcx/n+w9IDGKnJrqkMkD4U\n\ttTCR2+UFchbgzVmbrh0wsWHeUuVcV01nd7TBAc0gxBxct+cGyZ/roKBNKTuNFT0BPV\n\tJceRiErcEThyg==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=ys9533GjlvQGFZm4yKHa8dVJti9/xsK+th0aaNt5TcY=;\n\tb=IY1lGXd5Tfiu8UJ59pLacJHhANTuNQOiCVIf7hiD5FT2mIWkhGjfwRoVnsdHUtQmqG\n\t/+NiIojR2uSMTGhqpjV9u/nQ78fTKNmvxURLu/Rt8h5mHCl42UyeW+jsK4IRWp8r6MDR\n\tOHrPFih+M+vNAJ4OlNtX+par89coCp5aKsMhI="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=chromium.org\n\theader.i=@chromium.org header.b=\"IY1lGXd5\"; \n\tdkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=ys9533GjlvQGFZm4yKHa8dVJti9/xsK+th0aaNt5TcY=;\n\tb=n4QIHPZyR2uYzzXItOI4Spf3+5MorGx+aHXWgBLye4mDiW8QQoXF3Xcyun3bD9AHYH\n\tNLOr5u7QXtwr0Lcj0LJ5hzI2b4uEF9NLDo6VJLrDMWxZMvh5cxMhD/kvEiPe2721w7nJ\n\t1y10kcvipTyNsE4RNB2MX3AHB8DEK4gn6rYkxGG/gqix1ZPAwYp+M6V28ZArcT1gEIMm\n\tWi/FAbhPiSNQAbZZIK8ht+gbcZHypgVoQRTFl+cjpQApM/ueTNspnj6bvl0/Ky3FGNpV\n\tT8WCR9HGVkqAmXoH9MKia2CgLoobu4wVR7q/IBtk+AvEDTuswAXXaXx8i9OVyIGeMPqq\n\tjnRg==","X-Gm-Message-State":"AO0yUKX5HfvG+nxvjxC6ojCP86zkws1zcsPIGPNwo1wp3Hav2jy7KnBy\n\tX+NF20FDX/mhgUcEv54Ic0Ftj6pZj+lvQD/AE4ErmHr+3zvtpw==","X-Google-Smtp-Source":"AK7set8QLNU0cXHhnwR31kn/lCr8L10Q042PT9ZI/bEI4P+3td1mRhXoiNWAaNQriS1ocdDx0EmDQFp4XHify7I3Kdc=","X-Received":"by 2002:ab0:16d8:0:b0:68a:702a:2494 with SMTP id\n\tg24-20020ab016d8000000b0068a702a2494mr6122845uaf.0.1677745511272;\n\tThu, 02 Mar 2023 00:25:11 -0800 (PST)","MIME-Version":"1.0","References":"<20230302081349.3334290-1-chenghaoyang@google.com>\n\t<20230302081349.3334290-2-chenghaoyang@google.com>","In-Reply-To":"<20230302081349.3334290-2-chenghaoyang@google.com>","Date":"Thu, 2 Mar 2023 16:25:00 +0800","Message-ID":"<CAEB1ahvoF=Y807_N6Vc0gMhYOF+iO=5s5nB3J1h=0O2U=uiYFA@mail.gmail.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Content-Type":"multipart/alternative; boundary=\"0000000000003926d805f5e691a2\"","Subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","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>","From":"Cheng-Hao Yang via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26533,"web_url":"https://patchwork.libcamera.org/comment/26533/","msgid":"<167775847158.93391.1555103143960612315@Monstersaurus>","date":"2023-03-02T12:01:11","subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Cheng-Hao Yang (2023-03-02 08:25:00)\n> Hi Kieran,\n> \n> Sorry that I'm new to dma heap: I was trying to test virtual pipeline\n> handler on dma heap\n> (or the udma heap I add in the following patch), while I find that my\n> workstation doesn't\n> have the dma heap directory (`/dev/dma_heap` or `/dev/udmabuf`). I heard\n> from Han-lin\n> that we might need to enable it in the linux kernel. Is that right? Is\n> there a doc/tutorial that\n> I can follow?\n\nIt could be kernel specific indeed. I guess it's up to your\ndistribution?\n\nUDMABUF\n - https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L33\n\nDMABUF_HEAPS\n- https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L68\n\n\n--\nRegards\n\nKieran\n\n\n\n> \n> Thanks!\n> Harvey\n> \n> \n> \n> On Thu, Mar 2, 2023 at 4:13 PM Harvey Yang <chenghaoyang@chromium.org>\n> wrote:\n> \n> > As other components (like the WIP virtual pipeline handler) also need a\n> > heap allocator, move DmaHeap from raspberry pi pipeline handler to a\n> > general HeapAllocator as a base class.\n> >\n> > Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> > ---\n> >  include/libcamera/dma_heap.h                  | 20 +++++++++\n> >  include/libcamera/heap.h                      | 27 ++++++++++++\n> >  include/libcamera/heap_allocator.h            | 30 +++++++++++++\n> >  include/libcamera/meson.build                 |  3 ++\n> >  .../dma_heaps.cpp => dma_heap.cpp}            | 35 +++++++--------\n> >  src/libcamera/heap_allocator.cpp              | 43 +++++++++++++++++++\n> >  src/libcamera/meson.build                     |  2 +\n> >  .../pipeline/raspberrypi/dma_heaps.h          | 32 --------------\n> >  .../pipeline/raspberrypi/meson.build          |  1 -\n> >  .../pipeline/raspberrypi/raspberrypi.cpp      | 10 ++---\n> >  10 files changed, 146 insertions(+), 57 deletions(-)\n> >  create mode 100644 include/libcamera/dma_heap.h\n> >  create mode 100644 include/libcamera/heap.h\n> >  create mode 100644 include/libcamera/heap_allocator.h\n> >  rename src/libcamera/{pipeline/raspberrypi/dma_heaps.cpp => dma_heap.cpp}\n> > (69%)\n> >  create mode 100644 src/libcamera/heap_allocator.cpp\n> >  delete mode 100644 src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> >\n> > diff --git a/include/libcamera/dma_heap.h b/include/libcamera/dma_heap.h\n> > new file mode 100644\n> > index 00000000..a663c317\n> > --- /dev/null\n> > +++ b/include/libcamera/dma_heap.h\n> > @@ -0,0 +1,20 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2020, Raspberry Pi Ltd\n> > + *\n> > + * dma_heap.h - Dma Heap implementation.\n> > + */\n> > +\n> > +#include <libcamera/heap.h>\n> > +\n> > +namespace libcamera {\n> > +\n> > +class DmaHeap : public Heap\n> > +{\n> > +public:\n> > +       DmaHeap();\n> > +       ~DmaHeap();\n> > +       UniqueFD alloc(const char *name, std::size_t size) override;\n> > +};\n> > +\n> > +} /* namespace libcamera */\n> > diff --git a/include/libcamera/heap.h b/include/libcamera/heap.h\n> > new file mode 100644\n> > index 00000000..c49a2ac3\n> > --- /dev/null\n> > +++ b/include/libcamera/heap.h\n> > @@ -0,0 +1,27 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2023, Google Inc.\n> > + *\n> > + * heap.h - Heap interface.\n> > + */\n> > +\n> > +#pragma once\n> > +\n> > +#include <stddef.h>\n> > +\n> > +#include <libcamera/base/unique_fd.h>\n> > +\n> > +namespace libcamera {\n> > +\n> > +class Heap\n> > +{\n> > +public:\n> > +       virtual ~Heap() = default;\n> > +       bool isValid() const { return handle_.isValid(); }\n> > +       virtual UniqueFD alloc(const char *name, std::size_t size) = 0;\n> > +\n> > +protected:\n> > +       UniqueFD handle_;\n> > +};\n> > +\n> > +} /* namespace libcamera */\n> > diff --git a/include/libcamera/heap_allocator.h\n> > b/include/libcamera/heap_allocator.h\n> > new file mode 100644\n> > index 00000000..cd7ed1a3\n> > --- /dev/null\n> > +++ b/include/libcamera/heap_allocator.h\n> > @@ -0,0 +1,30 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2023, Google Inc.\n> > + *\n> > + * heap_allocator.h - Helper class for heap buffer allocations.\n> > + */\n> > +\n> > +#pragma once\n> > +\n> > +#include <stddef.h>\n> > +\n> > +#include <libcamera/base/unique_fd.h>\n> > +\n> > +#include <libcamera/heap.h>\n> > +\n> > +namespace libcamera {\n> > +\n> > +class HeapAllocator\n> > +{\n> > +public:\n> > +       HeapAllocator();\n> > +       ~HeapAllocator();\n> > +       bool isValid() const { return heap_->isValid(); }\n> > +       UniqueFD alloc(const char *name, std::size_t size);\n> > +\n> > +private:\n> > +       std::unique_ptr<Heap> heap_;\n> > +};\n> > +\n> > +} /* namespace libcamera */\n> > diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\n> > index 408b7acf..f486630a 100644\n> > --- a/include/libcamera/meson.build\n> > +++ b/include/libcamera/meson.build\n> > @@ -7,10 +7,13 @@ libcamera_public_headers = files([\n> >      'camera_manager.h',\n> >      'color_space.h',\n> >      'controls.h',\n> > +    'dma_heap.h',\n> >      'fence.h',\n> >      'framebuffer.h',\n> >      'framebuffer_allocator.h',\n> >      'geometry.h',\n> > +    'heap.h',\n> > +    'heap_allocator.h',\n> >      'logging.h',\n> >      'pixel_format.h',\n> >      'request.h',\n> > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > b/src/libcamera/dma_heap.cpp\n> > similarity index 69%\n> > rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > rename to src/libcamera/dma_heap.cpp\n> > index 6b644406..02415975 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > +++ b/src/libcamera/dma_heap.cpp\n> > @@ -2,18 +2,19 @@\n> >  /*\n> >   * Copyright (C) 2020, Raspberry Pi Ltd\n> >   *\n> > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > + * dma_heap.h - Dma Heap implementation.\n> >   */\n> >\n> > -#include \"dma_heaps.h\"\n> > +#include <libcamera/dma_heap.h>\n> >\n> >  #include <array>\n> >  #include <fcntl.h>\n> > -#include <linux/dma-buf.h>\n> > -#include <linux/dma-heap.h>\n> >  #include <sys/ioctl.h>\n> >  #include <unistd.h>\n> >\n> > +#include <linux/dma-buf.h>\n> > +#include <linux/dma-heap.h>\n> > +\n> >  #include <libcamera/base/log.h>\n> >\n> >  /*\n> > @@ -30,9 +31,7 @@ static constexpr std::array<const char *, 2> heapNames =\n> > {\n> >\n> >  namespace libcamera {\n> >\n> > -LOG_DECLARE_CATEGORY(RPI)\n> > -\n> > -namespace RPi {\n> > +LOG_DEFINE_CATEGORY(DmaHeap)\n> >\n> >  DmaHeap::DmaHeap()\n> >  {\n> > @@ -40,17 +39,17 @@ DmaHeap::DmaHeap()\n> >                 int ret = ::open(name, O_RDWR, 0);\n> >                 if (ret < 0) {\n> >                         ret = errno;\n> > -                       LOG(RPI, Debug) << \"Failed to open \" << name << \":\n> > \"\n> > -                                       << strerror(ret);\n> > +                       LOG(DmaHeap, Debug) << \"Failed to open \" << name\n> > << \": \"\n> > +                                           << strerror(ret);\n> >                         continue;\n> >                 }\n> >\n> > -               dmaHeapHandle_ = UniqueFD(ret);\n> > +               handle_ = UniqueFD(ret);\n> >                 break;\n> >         }\n> >\n> > -       if (!dmaHeapHandle_.isValid())\n> > -               LOG(RPI, Error) << \"Could not open any dmaHeap device\";\n> > +       if (!handle_.isValid())\n> > +               LOG(DmaHeap, Error) << \"Could not open any dmaHeap device\";\n> >  }\n> >\n> >  DmaHeap::~DmaHeap() = default;\n> > @@ -67,24 +66,22 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t\n> > size)\n> >         alloc.len = size;\n> >         alloc.fd_flags = O_CLOEXEC | O_RDWR;\n> >\n> > -       ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n> > +       ret = ::ioctl(handle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n> >         if (ret < 0) {\n> > -               LOG(RPI, Error) << \"dmaHeap allocation failure for \"\n> > -                               << name;\n> > +               LOG(DmaHeap, Error) << \"dmaHeap allocation failure for \"\n> > +                                   << name;\n> >                 return {};\n> >         }\n> >\n> >         UniqueFD allocFd(alloc.fd);\n> >         ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n> >         if (ret < 0) {\n> > -               LOG(RPI, Error) << \"dmaHeap naming failure for \"\n> > -                               << name;\n> > +               LOG(DmaHeap, Error) << \"dmaHeap naming failure for \"\n> > +                                   << name;\n> >                 return {};\n> >         }\n> >\n> >         return allocFd;\n> >  }\n> >\n> > -} /* namespace RPi */\n> > -\n> >  } /* namespace libcamera */\n> > diff --git a/src/libcamera/heap_allocator.cpp\n> > b/src/libcamera/heap_allocator.cpp\n> > new file mode 100644\n> > index 00000000..594b1d6a\n> > --- /dev/null\n> > +++ b/src/libcamera/heap_allocator.cpp\n> > @@ -0,0 +1,43 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2023, Google Inc.\n> > + *\n> > + * heap_allocator.cpp - Helper class for heap buffer allocations.\n> > + */\n> > +\n> > +#include <libcamera/heap_allocator.h>\n> > +\n> > +#include <array>\n> > +#include <fcntl.h>\n> > +#include <sys/ioctl.h>\n> > +#include <unistd.h>\n> > +\n> > +#include <linux/dma-buf.h>\n> > +#include <linux/dma-heap.h>\n> > +\n> > +#include <libcamera/base/log.h>\n> > +\n> > +#include <libcamera/dma_heap.h>\n> > +\n> > +namespace libcamera {\n> > +\n> > +LOG_DEFINE_CATEGORY(HeapAllocator)\n> > +\n> > +HeapAllocator::HeapAllocator()\n> > +{\n> > +       heap_ = std::make_unique<DmaHeap>();\n> > +}\n> > +\n> > +HeapAllocator::~HeapAllocator() = default;\n> > +\n> > +UniqueFD HeapAllocator::alloc(const char *name, std::size_t size)\n> > +{\n> > +       if (!isValid()) {\n> > +               LOG(HeapAllocator, Fatal) << \"Allocation attempted without\n> > allocator\" << name;\n> > +               return {};\n> > +       }\n> > +\n> > +       return heap_->alloc(name, size);\n> > +}\n> > +\n> > +} /* namespace libcamera */\n> > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> > index 9869bfe7..ee586c0d 100644\n> > --- a/src/libcamera/meson.build\n> > +++ b/src/libcamera/meson.build\n> > @@ -17,11 +17,13 @@ libcamera_sources = files([\n> >      'delayed_controls.cpp',\n> >      'device_enumerator.cpp',\n> >      'device_enumerator_sysfs.cpp',\n> > +    'dma_heap.cpp',\n> >      'fence.cpp',\n> >      'formats.cpp',\n> >      'framebuffer.cpp',\n> >      'framebuffer_allocator.cpp',\n> >      'geometry.cpp',\n> > +    'heap_allocator.cpp',\n> >      'ipa_controls.cpp',\n> >      'ipa_data_serializer.cpp',\n> >      'ipa_interface.cpp',\n> > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > deleted file mode 100644\n> > index 0a4a8d86..00000000\n> > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > +++ /dev/null\n> > @@ -1,32 +0,0 @@\n> > -/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > -/*\n> > - * Copyright (C) 2020, Raspberry Pi Ltd\n> > - *\n> > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > - */\n> > -\n> > -#pragma once\n> > -\n> > -#include <stddef.h>\n> > -\n> > -#include <libcamera/base/unique_fd.h>\n> > -\n> > -namespace libcamera {\n> > -\n> > -namespace RPi {\n> > -\n> > -class DmaHeap\n> > -{\n> > -public:\n> > -       DmaHeap();\n> > -       ~DmaHeap();\n> > -       bool isValid() const { return dmaHeapHandle_.isValid(); }\n> > -       UniqueFD alloc(const char *name, std::size_t size);\n> > -\n> > -private:\n> > -       UniqueFD dmaHeapHandle_;\n> > -};\n> > -\n> > -} /* namespace RPi */\n> > -\n> > -} /* namespace libcamera */\n> > diff --git a/src/libcamera/pipeline/raspberrypi/meson.build\n> > b/src/libcamera/pipeline/raspberrypi/meson.build\n> > index 59e8fb18..7322f0e8 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/meson.build\n> > +++ b/src/libcamera/pipeline/raspberrypi/meson.build\n> > @@ -2,7 +2,6 @@\n> >\n> >  libcamera_sources += files([\n> >      'delayed_controls.cpp',\n> > -    'dma_heaps.cpp',\n> >      'raspberrypi.cpp',\n> >      'rpi_stream.cpp',\n> >  ])\n> > diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > index 84120954..b7e0d031 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > @@ -21,6 +21,7 @@\n> >  #include <libcamera/camera.h>\n> >  #include <libcamera/control_ids.h>\n> >  #include <libcamera/formats.h>\n> > +#include <libcamera/heap_allocator.h>\n> >  #include <libcamera/ipa/raspberrypi_ipa_interface.h>\n> >  #include <libcamera/ipa/raspberrypi_ipa_proxy.h>\n> >  #include <libcamera/logging.h>\n> > @@ -44,7 +45,6 @@\n> >  #include \"libcamera/internal/yaml_parser.h\"\n> >\n> >  #include \"delayed_controls.h\"\n> > -#include \"dma_heaps.h\"\n> >  #include \"rpi_stream.h\"\n> >\n> >  using namespace std::chrono_literals;\n> > @@ -245,7 +245,7 @@ public:\n> >         std::vector<std::pair<std::unique_ptr<V4L2Subdevice>, MediaLink\n> > *>> bridgeDevices_;\n> >\n> >         /* DMAHEAP allocation helper. */\n> > -       RPi::DmaHeap dmaHeap_;\n> > +       HeapAllocator heapAllocator_;\n> >         SharedFD lsTable_;\n> >\n> >         std::unique_ptr<RPi::DelayedControls> delayedCtrls_;\n> > @@ -1293,7 +1293,7 @@ int PipelineHandlerRPi::registerCamera(MediaDevice\n> > *unicam, MediaDevice *isp, Me\n> >  {\n> >         std::unique_ptr<RPiCameraData> data =\n> > std::make_unique<RPiCameraData>(this);\n> >\n> > -       if (!data->dmaHeap_.isValid())\n> > +       if (!data->heapAllocator_.isValid())\n> >                 return -ENOMEM;\n> >\n> >         MediaEntity *unicamImage = unicam->getEntityByName(\"unicam-image\");\n> > @@ -1680,9 +1680,9 @@ int RPiCameraData::configureIPA(const\n> > CameraConfiguration *config, ipa::RPi::IPA\n> >         /* Always send the user transform to the IPA. */\n> >         ipaConfig.transform = static_cast<unsigned int>(config->transform);\n> >\n> > -       /* Allocate the lens shading table via dmaHeap and pass to the\n> > IPA. */\n> > +       /* Allocate the lens shading table via heapAllocator and pass to\n> > the IPA. */\n> >         if (!lsTable_.isValid()) {\n> > -               lsTable_ = SharedFD(dmaHeap_.alloc(\"ls_grid\",\n> > ipa::RPi::MaxLsGridSize));\n> > +               lsTable_ = SharedFD(heapAllocator_.alloc(\"ls_grid\",\n> > ipa::RPi::MaxLsGridSize));\n> >                 if (!lsTable_.isValid())\n> >                         return -ENOMEM;\n> >\n> > --\n> > 2.39.2.722.g9855ee24e9-goog\n> >\n> >","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 908E5BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  2 Mar 2023 12:01:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D34CD626D7;\n\tThu,  2 Mar 2023 13:01:15 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4628B6269C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  2 Mar 2023 13:01:14 +0100 (CET)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CC8B256A;\n\tThu,  2 Mar 2023 13:01:13 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1677758475;\n\tbh=nq+B3Q28TCF3aLI6iqEDO4MBxJ84JVjykbqBU636iWA=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=SgSRQTq31UR55SFq1+6DQAn6oOTDle2/U+24alKXn068tbtvEOAOGTW3hzLSkRUIw\n\t8YncWUCfOJpaXRdDtq0A9N2sQ7oiGKuHIYaEzDiil9gf/NX6mnejf6b6IfUkhiEew5\n\t/YvHyjO4RM5kTGJWoN9i77TXL23VYiUnzb1q6t+dZUmFz1IkIjntp046eDvicU6JP+\n\tqLwi6SwJz5N2XcL0tupQzrjEDOZOf54I5JX/VxXVopgbIGOoUAMEE0ipl/Uf+t1LbX\n\ts2G0gxodHKrGOZS2N82Sts9hvKWQWurQOGaCTf0GZcLqTBBpKTpoJ7/fHWaBk4YAZ6\n\t5+A0vEdBGsltw==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1677758473;\n\tbh=nq+B3Q28TCF3aLI6iqEDO4MBxJ84JVjykbqBU636iWA=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=GHu31Id34uN+oTDToGFImClCDmc3tGdSKnFjK96TKRP2bjgWQWyktXU/v5SUXnQ01\n\tDDjZXTfC/ePRC+zMFBX5PGdxqb2pYxcym/hJXX1W8sxwZ9TLofSw3xkTDdd1l9STqH\n\tCBdNIr65iX9hrOcPnjjXfQpsd3xlIOj7yfSEfwVg="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"GHu31Id3\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<CAEB1ahvoF=Y807_N6Vc0gMhYOF+iO=5s5nB3J1h=0O2U=uiYFA@mail.gmail.com>","References":"<20230302081349.3334290-1-chenghaoyang@google.com>\n\t<20230302081349.3334290-2-chenghaoyang@google.com>\n\t<CAEB1ahvoF=Y807_N6Vc0gMhYOF+iO=5s5nB3J1h=0O2U=uiYFA@mail.gmail.com>","To":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Date":"Thu, 02 Mar 2023 12:01:11 +0000","Message-ID":"<167775847158.93391.1555103143960612315@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","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>","From":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26659,"web_url":"https://patchwork.libcamera.org/comment/26659/","msgid":"<CAEB1ahvBnGzeFpLZus4-usSz2wdOujj-bK5aU7B8LE=XLG=8tA@mail.gmail.com>","date":"2023-03-16T15:50:29","subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/people/117/","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"content":"Thanks Kieran for the input. I found it difficult to enable dma/udma in\nGoogle's linux distribution though, so I tested it in my personal Arch\nlinux instead (udmabuf is available).\n\nHowever, I found that udmabuf only works if the requested size is power of\n2 (ex: 4194304 for 1920*1080). Is it expected, or there's something wrong\nwith my linux udmabuf support?\n\nBR,\nHarvey\n\nOn Thu, Mar 2, 2023 at 8:01 PM Kieran Bingham <\nkieran.bingham@ideasonboard.com> wrote:\n\n> Quoting Cheng-Hao Yang (2023-03-02 08:25:00)\n> > Hi Kieran,\n> >\n> > Sorry that I'm new to dma heap: I was trying to test virtual pipeline\n> > handler on dma heap\n> > (or the udma heap I add in the following patch), while I find that my\n> > workstation doesn't\n> > have the dma heap directory (`/dev/dma_heap` or `/dev/udmabuf`). I heard\n> > from Han-lin\n> > that we might need to enable it in the linux kernel. Is that right? Is\n> > there a doc/tutorial that\n> > I can follow?\n>\n> It could be kernel specific indeed. I guess it's up to your\n> distribution?\n>\n> UDMABUF\n>  -\n> https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L33\n>\n> DMABUF_HEAPS\n> -\n> https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L68\n>\n>\n> --\n> Regards\n>\n> Kieran\n>\n>\n>\n> >\n> > Thanks!\n> > Harvey\n> >\n> >\n> >\n> > On Thu, Mar 2, 2023 at 4:13 PM Harvey Yang <chenghaoyang@chromium.org>\n> > wrote:\n> >\n> > > As other components (like the WIP virtual pipeline handler) also need a\n> > > heap allocator, move DmaHeap from raspberry pi pipeline handler to a\n> > > general HeapAllocator as a base class.\n> > >\n> > > Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> > > ---\n> > >  include/libcamera/dma_heap.h                  | 20 +++++++++\n> > >  include/libcamera/heap.h                      | 27 ++++++++++++\n> > >  include/libcamera/heap_allocator.h            | 30 +++++++++++++\n> > >  include/libcamera/meson.build                 |  3 ++\n> > >  .../dma_heaps.cpp => dma_heap.cpp}            | 35 +++++++--------\n> > >  src/libcamera/heap_allocator.cpp              | 43 +++++++++++++++++++\n> > >  src/libcamera/meson.build                     |  2 +\n> > >  .../pipeline/raspberrypi/dma_heaps.h          | 32 --------------\n> > >  .../pipeline/raspberrypi/meson.build          |  1 -\n> > >  .../pipeline/raspberrypi/raspberrypi.cpp      | 10 ++---\n> > >  10 files changed, 146 insertions(+), 57 deletions(-)\n> > >  create mode 100644 include/libcamera/dma_heap.h\n> > >  create mode 100644 include/libcamera/heap.h\n> > >  create mode 100644 include/libcamera/heap_allocator.h\n> > >  rename src/libcamera/{pipeline/raspberrypi/dma_heaps.cpp =>\n> dma_heap.cpp}\n> > > (69%)\n> > >  create mode 100644 src/libcamera/heap_allocator.cpp\n> > >  delete mode 100644 src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > >\n> > > diff --git a/include/libcamera/dma_heap.h\n> b/include/libcamera/dma_heap.h\n> > > new file mode 100644\n> > > index 00000000..a663c317\n> > > --- /dev/null\n> > > +++ b/include/libcamera/dma_heap.h\n> > > @@ -0,0 +1,20 @@\n> > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > +/*\n> > > + * Copyright (C) 2020, Raspberry Pi Ltd\n> > > + *\n> > > + * dma_heap.h - Dma Heap implementation.\n> > > + */\n> > > +\n> > > +#include <libcamera/heap.h>\n> > > +\n> > > +namespace libcamera {\n> > > +\n> > > +class DmaHeap : public Heap\n> > > +{\n> > > +public:\n> > > +       DmaHeap();\n> > > +       ~DmaHeap();\n> > > +       UniqueFD alloc(const char *name, std::size_t size) override;\n> > > +};\n> > > +\n> > > +} /* namespace libcamera */\n> > > diff --git a/include/libcamera/heap.h b/include/libcamera/heap.h\n> > > new file mode 100644\n> > > index 00000000..c49a2ac3\n> > > --- /dev/null\n> > > +++ b/include/libcamera/heap.h\n> > > @@ -0,0 +1,27 @@\n> > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > +/*\n> > > + * Copyright (C) 2023, Google Inc.\n> > > + *\n> > > + * heap.h - Heap interface.\n> > > + */\n> > > +\n> > > +#pragma once\n> > > +\n> > > +#include <stddef.h>\n> > > +\n> > > +#include <libcamera/base/unique_fd.h>\n> > > +\n> > > +namespace libcamera {\n> > > +\n> > > +class Heap\n> > > +{\n> > > +public:\n> > > +       virtual ~Heap() = default;\n> > > +       bool isValid() const { return handle_.isValid(); }\n> > > +       virtual UniqueFD alloc(const char *name, std::size_t size) = 0;\n> > > +\n> > > +protected:\n> > > +       UniqueFD handle_;\n> > > +};\n> > > +\n> > > +} /* namespace libcamera */\n> > > diff --git a/include/libcamera/heap_allocator.h\n> > > b/include/libcamera/heap_allocator.h\n> > > new file mode 100644\n> > > index 00000000..cd7ed1a3\n> > > --- /dev/null\n> > > +++ b/include/libcamera/heap_allocator.h\n> > > @@ -0,0 +1,30 @@\n> > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > +/*\n> > > + * Copyright (C) 2023, Google Inc.\n> > > + *\n> > > + * heap_allocator.h - Helper class for heap buffer allocations.\n> > > + */\n> > > +\n> > > +#pragma once\n> > > +\n> > > +#include <stddef.h>\n> > > +\n> > > +#include <libcamera/base/unique_fd.h>\n> > > +\n> > > +#include <libcamera/heap.h>\n> > > +\n> > > +namespace libcamera {\n> > > +\n> > > +class HeapAllocator\n> > > +{\n> > > +public:\n> > > +       HeapAllocator();\n> > > +       ~HeapAllocator();\n> > > +       bool isValid() const { return heap_->isValid(); }\n> > > +       UniqueFD alloc(const char *name, std::size_t size);\n> > > +\n> > > +private:\n> > > +       std::unique_ptr<Heap> heap_;\n> > > +};\n> > > +\n> > > +} /* namespace libcamera */\n> > > diff --git a/include/libcamera/meson.build\n> b/include/libcamera/meson.build\n> > > index 408b7acf..f486630a 100644\n> > > --- a/include/libcamera/meson.build\n> > > +++ b/include/libcamera/meson.build\n> > > @@ -7,10 +7,13 @@ libcamera_public_headers = files([\n> > >      'camera_manager.h',\n> > >      'color_space.h',\n> > >      'controls.h',\n> > > +    'dma_heap.h',\n> > >      'fence.h',\n> > >      'framebuffer.h',\n> > >      'framebuffer_allocator.h',\n> > >      'geometry.h',\n> > > +    'heap.h',\n> > > +    'heap_allocator.h',\n> > >      'logging.h',\n> > >      'pixel_format.h',\n> > >      'request.h',\n> > > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > b/src/libcamera/dma_heap.cpp\n> > > similarity index 69%\n> > > rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > rename to src/libcamera/dma_heap.cpp\n> > > index 6b644406..02415975 100644\n> > > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > +++ b/src/libcamera/dma_heap.cpp\n> > > @@ -2,18 +2,19 @@\n> > >  /*\n> > >   * Copyright (C) 2020, Raspberry Pi Ltd\n> > >   *\n> > > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > > + * dma_heap.h - Dma Heap implementation.\n> > >   */\n> > >\n> > > -#include \"dma_heaps.h\"\n> > > +#include <libcamera/dma_heap.h>\n> > >\n> > >  #include <array>\n> > >  #include <fcntl.h>\n> > > -#include <linux/dma-buf.h>\n> > > -#include <linux/dma-heap.h>\n> > >  #include <sys/ioctl.h>\n> > >  #include <unistd.h>\n> > >\n> > > +#include <linux/dma-buf.h>\n> > > +#include <linux/dma-heap.h>\n> > > +\n> > >  #include <libcamera/base/log.h>\n> > >\n> > >  /*\n> > > @@ -30,9 +31,7 @@ static constexpr std::array<const char *, 2>\n> heapNames =\n> > > {\n> > >\n> > >  namespace libcamera {\n> > >\n> > > -LOG_DECLARE_CATEGORY(RPI)\n> > > -\n> > > -namespace RPi {\n> > > +LOG_DEFINE_CATEGORY(DmaHeap)\n> > >\n> > >  DmaHeap::DmaHeap()\n> > >  {\n> > > @@ -40,17 +39,17 @@ DmaHeap::DmaHeap()\n> > >                 int ret = ::open(name, O_RDWR, 0);\n> > >                 if (ret < 0) {\n> > >                         ret = errno;\n> > > -                       LOG(RPI, Debug) << \"Failed to open \" << name\n> << \":\n> > > \"\n> > > -                                       << strerror(ret);\n> > > +                       LOG(DmaHeap, Debug) << \"Failed to open \" <<\n> name\n> > > << \": \"\n> > > +                                           << strerror(ret);\n> > >                         continue;\n> > >                 }\n> > >\n> > > -               dmaHeapHandle_ = UniqueFD(ret);\n> > > +               handle_ = UniqueFD(ret);\n> > >                 break;\n> > >         }\n> > >\n> > > -       if (!dmaHeapHandle_.isValid())\n> > > -               LOG(RPI, Error) << \"Could not open any dmaHeap device\";\n> > > +       if (!handle_.isValid())\n> > > +               LOG(DmaHeap, Error) << \"Could not open any dmaHeap\n> device\";\n> > >  }\n> > >\n> > >  DmaHeap::~DmaHeap() = default;\n> > > @@ -67,24 +66,22 @@ UniqueFD DmaHeap::alloc(const char *name,\n> std::size_t\n> > > size)\n> > >         alloc.len = size;\n> > >         alloc.fd_flags = O_CLOEXEC | O_RDWR;\n> > >\n> > > -       ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC,\n> &alloc);\n> > > +       ret = ::ioctl(handle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n> > >         if (ret < 0) {\n> > > -               LOG(RPI, Error) << \"dmaHeap allocation failure for \"\n> > > -                               << name;\n> > > +               LOG(DmaHeap, Error) << \"dmaHeap allocation failure for\n> \"\n> > > +                                   << name;\n> > >                 return {};\n> > >         }\n> > >\n> > >         UniqueFD allocFd(alloc.fd);\n> > >         ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n> > >         if (ret < 0) {\n> > > -               LOG(RPI, Error) << \"dmaHeap naming failure for \"\n> > > -                               << name;\n> > > +               LOG(DmaHeap, Error) << \"dmaHeap naming failure for \"\n> > > +                                   << name;\n> > >                 return {};\n> > >         }\n> > >\n> > >         return allocFd;\n> > >  }\n> > >\n> > > -} /* namespace RPi */\n> > > -\n> > >  } /* namespace libcamera */\n> > > diff --git a/src/libcamera/heap_allocator.cpp\n> > > b/src/libcamera/heap_allocator.cpp\n> > > new file mode 100644\n> > > index 00000000..594b1d6a\n> > > --- /dev/null\n> > > +++ b/src/libcamera/heap_allocator.cpp\n> > > @@ -0,0 +1,43 @@\n> > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > +/*\n> > > + * Copyright (C) 2023, Google Inc.\n> > > + *\n> > > + * heap_allocator.cpp - Helper class for heap buffer allocations.\n> > > + */\n> > > +\n> > > +#include <libcamera/heap_allocator.h>\n> > > +\n> > > +#include <array>\n> > > +#include <fcntl.h>\n> > > +#include <sys/ioctl.h>\n> > > +#include <unistd.h>\n> > > +\n> > > +#include <linux/dma-buf.h>\n> > > +#include <linux/dma-heap.h>\n> > > +\n> > > +#include <libcamera/base/log.h>\n> > > +\n> > > +#include <libcamera/dma_heap.h>\n> > > +\n> > > +namespace libcamera {\n> > > +\n> > > +LOG_DEFINE_CATEGORY(HeapAllocator)\n> > > +\n> > > +HeapAllocator::HeapAllocator()\n> > > +{\n> > > +       heap_ = std::make_unique<DmaHeap>();\n> > > +}\n> > > +\n> > > +HeapAllocator::~HeapAllocator() = default;\n> > > +\n> > > +UniqueFD HeapAllocator::alloc(const char *name, std::size_t size)\n> > > +{\n> > > +       if (!isValid()) {\n> > > +               LOG(HeapAllocator, Fatal) << \"Allocation attempted\n> without\n> > > allocator\" << name;\n> > > +               return {};\n> > > +       }\n> > > +\n> > > +       return heap_->alloc(name, size);\n> > > +}\n> > > +\n> > > +} /* namespace libcamera */\n> > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> > > index 9869bfe7..ee586c0d 100644\n> > > --- a/src/libcamera/meson.build\n> > > +++ b/src/libcamera/meson.build\n> > > @@ -17,11 +17,13 @@ libcamera_sources = files([\n> > >      'delayed_controls.cpp',\n> > >      'device_enumerator.cpp',\n> > >      'device_enumerator_sysfs.cpp',\n> > > +    'dma_heap.cpp',\n> > >      'fence.cpp',\n> > >      'formats.cpp',\n> > >      'framebuffer.cpp',\n> > >      'framebuffer_allocator.cpp',\n> > >      'geometry.cpp',\n> > > +    'heap_allocator.cpp',\n> > >      'ipa_controls.cpp',\n> > >      'ipa_data_serializer.cpp',\n> > >      'ipa_interface.cpp',\n> > > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > deleted file mode 100644\n> > > index 0a4a8d86..00000000\n> > > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > +++ /dev/null\n> > > @@ -1,32 +0,0 @@\n> > > -/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > -/*\n> > > - * Copyright (C) 2020, Raspberry Pi Ltd\n> > > - *\n> > > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > > - */\n> > > -\n> > > -#pragma once\n> > > -\n> > > -#include <stddef.h>\n> > > -\n> > > -#include <libcamera/base/unique_fd.h>\n> > > -\n> > > -namespace libcamera {\n> > > -\n> > > -namespace RPi {\n> > > -\n> > > -class DmaHeap\n> > > -{\n> > > -public:\n> > > -       DmaHeap();\n> > > -       ~DmaHeap();\n> > > -       bool isValid() const { return dmaHeapHandle_.isValid(); }\n> > > -       UniqueFD alloc(const char *name, std::size_t size);\n> > > -\n> > > -private:\n> > > -       UniqueFD dmaHeapHandle_;\n> > > -};\n> > > -\n> > > -} /* namespace RPi */\n> > > -\n> > > -} /* namespace libcamera */\n> > > diff --git a/src/libcamera/pipeline/raspberrypi/meson.build\n> > > b/src/libcamera/pipeline/raspberrypi/meson.build\n> > > index 59e8fb18..7322f0e8 100644\n> > > --- a/src/libcamera/pipeline/raspberrypi/meson.build\n> > > +++ b/src/libcamera/pipeline/raspberrypi/meson.build\n> > > @@ -2,7 +2,6 @@\n> > >\n> > >  libcamera_sources += files([\n> > >      'delayed_controls.cpp',\n> > > -    'dma_heaps.cpp',\n> > >      'raspberrypi.cpp',\n> > >      'rpi_stream.cpp',\n> > >  ])\n> > > diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > index 84120954..b7e0d031 100644\n> > > --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > @@ -21,6 +21,7 @@\n> > >  #include <libcamera/camera.h>\n> > >  #include <libcamera/control_ids.h>\n> > >  #include <libcamera/formats.h>\n> > > +#include <libcamera/heap_allocator.h>\n> > >  #include <libcamera/ipa/raspberrypi_ipa_interface.h>\n> > >  #include <libcamera/ipa/raspberrypi_ipa_proxy.h>\n> > >  #include <libcamera/logging.h>\n> > > @@ -44,7 +45,6 @@\n> > >  #include \"libcamera/internal/yaml_parser.h\"\n> > >\n> > >  #include \"delayed_controls.h\"\n> > > -#include \"dma_heaps.h\"\n> > >  #include \"rpi_stream.h\"\n> > >\n> > >  using namespace std::chrono_literals;\n> > > @@ -245,7 +245,7 @@ public:\n> > >         std::vector<std::pair<std::unique_ptr<V4L2Subdevice>, MediaLink\n> > > *>> bridgeDevices_;\n> > >\n> > >         /* DMAHEAP allocation helper. */\n> > > -       RPi::DmaHeap dmaHeap_;\n> > > +       HeapAllocator heapAllocator_;\n> > >         SharedFD lsTable_;\n> > >\n> > >         std::unique_ptr<RPi::DelayedControls> delayedCtrls_;\n> > > @@ -1293,7 +1293,7 @@ int\n> PipelineHandlerRPi::registerCamera(MediaDevice\n> > > *unicam, MediaDevice *isp, Me\n> > >  {\n> > >         std::unique_ptr<RPiCameraData> data =\n> > > std::make_unique<RPiCameraData>(this);\n> > >\n> > > -       if (!data->dmaHeap_.isValid())\n> > > +       if (!data->heapAllocator_.isValid())\n> > >                 return -ENOMEM;\n> > >\n> > >         MediaEntity *unicamImage =\n> unicam->getEntityByName(\"unicam-image\");\n> > > @@ -1680,9 +1680,9 @@ int RPiCameraData::configureIPA(const\n> > > CameraConfiguration *config, ipa::RPi::IPA\n> > >         /* Always send the user transform to the IPA. */\n> > >         ipaConfig.transform = static_cast<unsigned\n> int>(config->transform);\n> > >\n> > > -       /* Allocate the lens shading table via dmaHeap and pass to the\n> > > IPA. */\n> > > +       /* Allocate the lens shading table via heapAllocator and pass\n> to\n> > > the IPA. */\n> > >         if (!lsTable_.isValid()) {\n> > > -               lsTable_ = SharedFD(dmaHeap_.alloc(\"ls_grid\",\n> > > ipa::RPi::MaxLsGridSize));\n> > > +               lsTable_ = SharedFD(heapAllocator_.alloc(\"ls_grid\",\n> > > ipa::RPi::MaxLsGridSize));\n> > >                 if (!lsTable_.isValid())\n> > >                         return -ENOMEM;\n> > >\n> > > --\n> > > 2.39.2.722.g9855ee24e9-goog\n> > >\n> > >\n>","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 199E7C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 16 Mar 2023 15:50:44 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 46D7762709;\n\tThu, 16 Mar 2023 16:50:43 +0100 (CET)","from mail-ua1-x92c.google.com (mail-ua1-x92c.google.com\n\t[IPv6:2607:f8b0:4864:20::92c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6A56C62705\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 16 Mar 2023 16:50:41 +0100 (CET)","by mail-ua1-x92c.google.com with SMTP id h34so1456000uag.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 16 Mar 2023 08:50:41 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1678981843;\n\tbh=TxQnzxRIkZav/jpusM77OtNtstg3BXZFJjhIKNFU0mM=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=Zz6qgWAROcN7PBR1wugnmlMRReiXk/8aa1O/aLaNGf4S8hkf436pfKg7yZoFqKac1\n\tiFqDlotKRE9vYtJl/QYGX1jPqgyKBQW9UeDpMDmBmprtux9eMxxp8xI3bh5DHAT+b6\n\t1umj2u1JS0T8N2CdTSv4VnkCWZsYh6zngS93uvC6HMncZMMwlxY1pnBLoW1+wZBZCb\n\t40L+8bi25cWWK9PuUZ5FrqiK2dQUeD1hddMwNYSBE4rDcjLpGxCR3Z/juBMTtB5AGV\n\tCMkp5xgLmlRd54PoKvJvfXvDvFVb08zDAArldC34QNdJM2h9J2vWIH41GFemmnc0tJ\n\trTHOrGsPQN/6w==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1678981840;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=f34mrLeFyUURAmIHlVVs3+eI2kirpapFSxdGeZ+Kzj0=;\n\tb=nae2QMMXD9898HbN8cN+lfS53O41M8oYDcE13J3oHBLK2teCmHagPp/OJ/d+qS/dYl\n\tXUs0dbdkn0YahxfORCG3H3Zym1dc1K71NUkAbHfNiGLBRhV2AL28vdOtyiSkvEKCjEYP\n\tdClMa1S43A2VnCf0/F6i6xb/XoxPvf2cn3NHI="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=chromium.org\n\theader.i=@chromium.org header.b=\"nae2QMMX\"; \n\tdkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112; t=1678981840;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=f34mrLeFyUURAmIHlVVs3+eI2kirpapFSxdGeZ+Kzj0=;\n\tb=YKFVgigwpoRo0M7qTQaQiSDSVcXajDTFYJGM7h8Vj0ObwqSHs5as07brCtnqluwtjg\n\tQKckJi9CTFoeE544WAIDpkqwYrncwpK/Pz/Nyy6k39tnYzLVZ/wnKYF4uZpyS8FB6Ew1\n\tAG8Ixgro1cYdHz5TKUK0U7LJYTxjfW0mcWlDkvi9iXB/rWGj4kZyF8rPxUKnku0DBjeM\n\tKtAlaYX4QUDe4DZm7R0uueFJ9zfNHknyzBqh+npONdYV/T0XVb/eh1Y5pz1EkO1UUO0d\n\tZ/rsMubsI29ju3LAj+DEsTq2ebe+BbgjGlMCMQWEzQDzC8RUSfPBTPtI6K+J2KzmxuD3\n\tNsxw==","X-Gm-Message-State":"AO0yUKWA3HW68ch8GWs2UST8jMHOpGHC47OdbfAh0vCrkodDkFSHKjUP\n\t4beR5JktQ6++lB2NUcTMZ4qQswcjFpoDNPzvvHLaaNkEwV3hcLL0","X-Google-Smtp-Source":"AK7set+nvTmHz2wYJAxcZAryPhF/BVrKsrU1H6bqrUbKMoeaCvFKpa67MkrjHqb5HHYido9QMj07CKCg6YxyJ59yPhg=","X-Received":"by 2002:a1f:a250:0:b0:3ea:b7db:61c with SMTP id\n\tl77-20020a1fa250000000b003eab7db061cmr27926190vke.2.1678981839928;\n\tThu, 16 Mar 2023 08:50:39 -0700 (PDT)","MIME-Version":"1.0","References":"<20230302081349.3334290-1-chenghaoyang@google.com>\n\t<20230302081349.3334290-2-chenghaoyang@google.com>\n\t<CAEB1ahvoF=Y807_N6Vc0gMhYOF+iO=5s5nB3J1h=0O2U=uiYFA@mail.gmail.com>\n\t<167775847158.93391.1555103143960612315@Monstersaurus>","In-Reply-To":"<167775847158.93391.1555103143960612315@Monstersaurus>","Date":"Thu, 16 Mar 2023 23:50:29 +0800","Message-ID":"<CAEB1ahvBnGzeFpLZus4-usSz2wdOujj-bK5aU7B8LE=XLG=8tA@mail.gmail.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Content-Type":"multipart/alternative; boundary=\"0000000000002754c805f7066cc2\"","Subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","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>","From":"Cheng-Hao Yang via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26661,"web_url":"https://patchwork.libcamera.org/comment/26661/","msgid":"<167908971816.2537120.13888882718500541416@Monstersaurus>","date":"2023-03-17T21:48:38","subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Cheng-Hao Yang (2023-03-16 15:50:29)\n> Thanks Kieran for the input. I found it difficult to enable dma/udma in\n> Google's linux distribution though, so I tested it in my personal Arch\n> linux instead (udmabuf is available).\n> \n> However, I found that udmabuf only works if the requested size is power of\n> 2 (ex: 4194304 for 1920*1080). Is it expected, or there's something wrong\n> with my linux udmabuf support?\n\nI would expect that it can only allocate a multiple of the PAGE_SIZE\nconfigured on the Kernel.\n\nYou could investigate that and decide to round up to the next PAGE_SIZE,\nor if the kernel does this, just let the allocation succeed as long as\nit is equal or larger than the requested size?\n\n--\nKieran\n\n\n> \n> BR,\n> Harvey\n> \n> On Thu, Mar 2, 2023 at 8:01 PM Kieran Bingham <\n> kieran.bingham@ideasonboard.com> wrote:\n> \n> > Quoting Cheng-Hao Yang (2023-03-02 08:25:00)\n> > > Hi Kieran,\n> > >\n> > > Sorry that I'm new to dma heap: I was trying to test virtual pipeline\n> > > handler on dma heap\n> > > (or the udma heap I add in the following patch), while I find that my\n> > > workstation doesn't\n> > > have the dma heap directory (`/dev/dma_heap` or `/dev/udmabuf`). I heard\n> > > from Han-lin\n> > > that we might need to enable it in the linux kernel. Is that right? Is\n> > > there a doc/tutorial that\n> > > I can follow?\n> >\n> > It could be kernel specific indeed. I guess it's up to your\n> > distribution?\n> >\n> > UDMABUF\n> >  -\n> > https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L33\n> >\n> > DMABUF_HEAPS\n> > -\n> > https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L68\n> >\n> >\n> > --\n> > Regards\n> >\n> > Kieran\n> >\n> >\n> >\n> > >\n> > > Thanks!\n> > > Harvey\n> > >\n> > >\n> > >\n> > > On Thu, Mar 2, 2023 at 4:13 PM Harvey Yang <chenghaoyang@chromium.org>\n> > > wrote:\n> > >\n> > > > As other components (like the WIP virtual pipeline handler) also need a\n> > > > heap allocator, move DmaHeap from raspberry pi pipeline handler to a\n> > > > general HeapAllocator as a base class.\n> > > >\n> > > > Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> > > > ---\n> > > >  include/libcamera/dma_heap.h                  | 20 +++++++++\n> > > >  include/libcamera/heap.h                      | 27 ++++++++++++\n> > > >  include/libcamera/heap_allocator.h            | 30 +++++++++++++\n> > > >  include/libcamera/meson.build                 |  3 ++\n> > > >  .../dma_heaps.cpp => dma_heap.cpp}            | 35 +++++++--------\n> > > >  src/libcamera/heap_allocator.cpp              | 43 +++++++++++++++++++\n> > > >  src/libcamera/meson.build                     |  2 +\n> > > >  .../pipeline/raspberrypi/dma_heaps.h          | 32 --------------\n> > > >  .../pipeline/raspberrypi/meson.build          |  1 -\n> > > >  .../pipeline/raspberrypi/raspberrypi.cpp      | 10 ++---\n> > > >  10 files changed, 146 insertions(+), 57 deletions(-)\n> > > >  create mode 100644 include/libcamera/dma_heap.h\n> > > >  create mode 100644 include/libcamera/heap.h\n> > > >  create mode 100644 include/libcamera/heap_allocator.h\n> > > >  rename src/libcamera/{pipeline/raspberrypi/dma_heaps.cpp =>\n> > dma_heap.cpp}\n> > > > (69%)\n> > > >  create mode 100644 src/libcamera/heap_allocator.cpp\n> > > >  delete mode 100644 src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > >\n> > > > diff --git a/include/libcamera/dma_heap.h\n> > b/include/libcamera/dma_heap.h\n> > > > new file mode 100644\n> > > > index 00000000..a663c317\n> > > > --- /dev/null\n> > > > +++ b/include/libcamera/dma_heap.h\n> > > > @@ -0,0 +1,20 @@\n> > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > +/*\n> > > > + * Copyright (C) 2020, Raspberry Pi Ltd\n> > > > + *\n> > > > + * dma_heap.h - Dma Heap implementation.\n> > > > + */\n> > > > +\n> > > > +#include <libcamera/heap.h>\n> > > > +\n> > > > +namespace libcamera {\n> > > > +\n> > > > +class DmaHeap : public Heap\n> > > > +{\n> > > > +public:\n> > > > +       DmaHeap();\n> > > > +       ~DmaHeap();\n> > > > +       UniqueFD alloc(const char *name, std::size_t size) override;\n> > > > +};\n> > > > +\n> > > > +} /* namespace libcamera */\n> > > > diff --git a/include/libcamera/heap.h b/include/libcamera/heap.h\n> > > > new file mode 100644\n> > > > index 00000000..c49a2ac3\n> > > > --- /dev/null\n> > > > +++ b/include/libcamera/heap.h\n> > > > @@ -0,0 +1,27 @@\n> > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > +/*\n> > > > + * Copyright (C) 2023, Google Inc.\n> > > > + *\n> > > > + * heap.h - Heap interface.\n> > > > + */\n> > > > +\n> > > > +#pragma once\n> > > > +\n> > > > +#include <stddef.h>\n> > > > +\n> > > > +#include <libcamera/base/unique_fd.h>\n> > > > +\n> > > > +namespace libcamera {\n> > > > +\n> > > > +class Heap\n> > > > +{\n> > > > +public:\n> > > > +       virtual ~Heap() = default;\n> > > > +       bool isValid() const { return handle_.isValid(); }\n> > > > +       virtual UniqueFD alloc(const char *name, std::size_t size) = 0;\n> > > > +\n> > > > +protected:\n> > > > +       UniqueFD handle_;\n> > > > +};\n> > > > +\n> > > > +} /* namespace libcamera */\n> > > > diff --git a/include/libcamera/heap_allocator.h\n> > > > b/include/libcamera/heap_allocator.h\n> > > > new file mode 100644\n> > > > index 00000000..cd7ed1a3\n> > > > --- /dev/null\n> > > > +++ b/include/libcamera/heap_allocator.h\n> > > > @@ -0,0 +1,30 @@\n> > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > +/*\n> > > > + * Copyright (C) 2023, Google Inc.\n> > > > + *\n> > > > + * heap_allocator.h - Helper class for heap buffer allocations.\n> > > > + */\n> > > > +\n> > > > +#pragma once\n> > > > +\n> > > > +#include <stddef.h>\n> > > > +\n> > > > +#include <libcamera/base/unique_fd.h>\n> > > > +\n> > > > +#include <libcamera/heap.h>\n> > > > +\n> > > > +namespace libcamera {\n> > > > +\n> > > > +class HeapAllocator\n> > > > +{\n> > > > +public:\n> > > > +       HeapAllocator();\n> > > > +       ~HeapAllocator();\n> > > > +       bool isValid() const { return heap_->isValid(); }\n> > > > +       UniqueFD alloc(const char *name, std::size_t size);\n> > > > +\n> > > > +private:\n> > > > +       std::unique_ptr<Heap> heap_;\n> > > > +};\n> > > > +\n> > > > +} /* namespace libcamera */\n> > > > diff --git a/include/libcamera/meson.build\n> > b/include/libcamera/meson.build\n> > > > index 408b7acf..f486630a 100644\n> > > > --- a/include/libcamera/meson.build\n> > > > +++ b/include/libcamera/meson.build\n> > > > @@ -7,10 +7,13 @@ libcamera_public_headers = files([\n> > > >      'camera_manager.h',\n> > > >      'color_space.h',\n> > > >      'controls.h',\n> > > > +    'dma_heap.h',\n> > > >      'fence.h',\n> > > >      'framebuffer.h',\n> > > >      'framebuffer_allocator.h',\n> > > >      'geometry.h',\n> > > > +    'heap.h',\n> > > > +    'heap_allocator.h',\n> > > >      'logging.h',\n> > > >      'pixel_format.h',\n> > > >      'request.h',\n> > > > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > > b/src/libcamera/dma_heap.cpp\n> > > > similarity index 69%\n> > > > rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > > rename to src/libcamera/dma_heap.cpp\n> > > > index 6b644406..02415975 100644\n> > > > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > > +++ b/src/libcamera/dma_heap.cpp\n> > > > @@ -2,18 +2,19 @@\n> > > >  /*\n> > > >   * Copyright (C) 2020, Raspberry Pi Ltd\n> > > >   *\n> > > > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > > > + * dma_heap.h - Dma Heap implementation.\n> > > >   */\n> > > >\n> > > > -#include \"dma_heaps.h\"\n> > > > +#include <libcamera/dma_heap.h>\n> > > >\n> > > >  #include <array>\n> > > >  #include <fcntl.h>\n> > > > -#include <linux/dma-buf.h>\n> > > > -#include <linux/dma-heap.h>\n> > > >  #include <sys/ioctl.h>\n> > > >  #include <unistd.h>\n> > > >\n> > > > +#include <linux/dma-buf.h>\n> > > > +#include <linux/dma-heap.h>\n> > > > +\n> > > >  #include <libcamera/base/log.h>\n> > > >\n> > > >  /*\n> > > > @@ -30,9 +31,7 @@ static constexpr std::array<const char *, 2>\n> > heapNames =\n> > > > {\n> > > >\n> > > >  namespace libcamera {\n> > > >\n> > > > -LOG_DECLARE_CATEGORY(RPI)\n> > > > -\n> > > > -namespace RPi {\n> > > > +LOG_DEFINE_CATEGORY(DmaHeap)\n> > > >\n> > > >  DmaHeap::DmaHeap()\n> > > >  {\n> > > > @@ -40,17 +39,17 @@ DmaHeap::DmaHeap()\n> > > >                 int ret = ::open(name, O_RDWR, 0);\n> > > >                 if (ret < 0) {\n> > > >                         ret = errno;\n> > > > -                       LOG(RPI, Debug) << \"Failed to open \" << name\n> > << \":\n> > > > \"\n> > > > -                                       << strerror(ret);\n> > > > +                       LOG(DmaHeap, Debug) << \"Failed to open \" <<\n> > name\n> > > > << \": \"\n> > > > +                                           << strerror(ret);\n> > > >                         continue;\n> > > >                 }\n> > > >\n> > > > -               dmaHeapHandle_ = UniqueFD(ret);\n> > > > +               handle_ = UniqueFD(ret);\n> > > >                 break;\n> > > >         }\n> > > >\n> > > > -       if (!dmaHeapHandle_.isValid())\n> > > > -               LOG(RPI, Error) << \"Could not open any dmaHeap device\";\n> > > > +       if (!handle_.isValid())\n> > > > +               LOG(DmaHeap, Error) << \"Could not open any dmaHeap\n> > device\";\n> > > >  }\n> > > >\n> > > >  DmaHeap::~DmaHeap() = default;\n> > > > @@ -67,24 +66,22 @@ UniqueFD DmaHeap::alloc(const char *name,\n> > std::size_t\n> > > > size)\n> > > >         alloc.len = size;\n> > > >         alloc.fd_flags = O_CLOEXEC | O_RDWR;\n> > > >\n> > > > -       ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC,\n> > &alloc);\n> > > > +       ret = ::ioctl(handle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n> > > >         if (ret < 0) {\n> > > > -               LOG(RPI, Error) << \"dmaHeap allocation failure for \"\n> > > > -                               << name;\n> > > > +               LOG(DmaHeap, Error) << \"dmaHeap allocation failure for\n> > \"\n> > > > +                                   << name;\n> > > >                 return {};\n> > > >         }\n> > > >\n> > > >         UniqueFD allocFd(alloc.fd);\n> > > >         ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n> > > >         if (ret < 0) {\n> > > > -               LOG(RPI, Error) << \"dmaHeap naming failure for \"\n> > > > -                               << name;\n> > > > +               LOG(DmaHeap, Error) << \"dmaHeap naming failure for \"\n> > > > +                                   << name;\n> > > >                 return {};\n> > > >         }\n> > > >\n> > > >         return allocFd;\n> > > >  }\n> > > >\n> > > > -} /* namespace RPi */\n> > > > -\n> > > >  } /* namespace libcamera */\n> > > > diff --git a/src/libcamera/heap_allocator.cpp\n> > > > b/src/libcamera/heap_allocator.cpp\n> > > > new file mode 100644\n> > > > index 00000000..594b1d6a\n> > > > --- /dev/null\n> > > > +++ b/src/libcamera/heap_allocator.cpp\n> > > > @@ -0,0 +1,43 @@\n> > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > +/*\n> > > > + * Copyright (C) 2023, Google Inc.\n> > > > + *\n> > > > + * heap_allocator.cpp - Helper class for heap buffer allocations.\n> > > > + */\n> > > > +\n> > > > +#include <libcamera/heap_allocator.h>\n> > > > +\n> > > > +#include <array>\n> > > > +#include <fcntl.h>\n> > > > +#include <sys/ioctl.h>\n> > > > +#include <unistd.h>\n> > > > +\n> > > > +#include <linux/dma-buf.h>\n> > > > +#include <linux/dma-heap.h>\n> > > > +\n> > > > +#include <libcamera/base/log.h>\n> > > > +\n> > > > +#include <libcamera/dma_heap.h>\n> > > > +\n> > > > +namespace libcamera {\n> > > > +\n> > > > +LOG_DEFINE_CATEGORY(HeapAllocator)\n> > > > +\n> > > > +HeapAllocator::HeapAllocator()\n> > > > +{\n> > > > +       heap_ = std::make_unique<DmaHeap>();\n> > > > +}\n> > > > +\n> > > > +HeapAllocator::~HeapAllocator() = default;\n> > > > +\n> > > > +UniqueFD HeapAllocator::alloc(const char *name, std::size_t size)\n> > > > +{\n> > > > +       if (!isValid()) {\n> > > > +               LOG(HeapAllocator, Fatal) << \"Allocation attempted\n> > without\n> > > > allocator\" << name;\n> > > > +               return {};\n> > > > +       }\n> > > > +\n> > > > +       return heap_->alloc(name, size);\n> > > > +}\n> > > > +\n> > > > +} /* namespace libcamera */\n> > > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> > > > index 9869bfe7..ee586c0d 100644\n> > > > --- a/src/libcamera/meson.build\n> > > > +++ b/src/libcamera/meson.build\n> > > > @@ -17,11 +17,13 @@ libcamera_sources = files([\n> > > >      'delayed_controls.cpp',\n> > > >      'device_enumerator.cpp',\n> > > >      'device_enumerator_sysfs.cpp',\n> > > > +    'dma_heap.cpp',\n> > > >      'fence.cpp',\n> > > >      'formats.cpp',\n> > > >      'framebuffer.cpp',\n> > > >      'framebuffer_allocator.cpp',\n> > > >      'geometry.cpp',\n> > > > +    'heap_allocator.cpp',\n> > > >      'ipa_controls.cpp',\n> > > >      'ipa_data_serializer.cpp',\n> > > >      'ipa_interface.cpp',\n> > > > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > > b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > > deleted file mode 100644\n> > > > index 0a4a8d86..00000000\n> > > > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > > +++ /dev/null\n> > > > @@ -1,32 +0,0 @@\n> > > > -/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > -/*\n> > > > - * Copyright (C) 2020, Raspberry Pi Ltd\n> > > > - *\n> > > > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > > > - */\n> > > > -\n> > > > -#pragma once\n> > > > -\n> > > > -#include <stddef.h>\n> > > > -\n> > > > -#include <libcamera/base/unique_fd.h>\n> > > > -\n> > > > -namespace libcamera {\n> > > > -\n> > > > -namespace RPi {\n> > > > -\n> > > > -class DmaHeap\n> > > > -{\n> > > > -public:\n> > > > -       DmaHeap();\n> > > > -       ~DmaHeap();\n> > > > -       bool isValid() const { return dmaHeapHandle_.isValid(); }\n> > > > -       UniqueFD alloc(const char *name, std::size_t size);\n> > > > -\n> > > > -private:\n> > > > -       UniqueFD dmaHeapHandle_;\n> > > > -};\n> > > > -\n> > > > -} /* namespace RPi */\n> > > > -\n> > > > -} /* namespace libcamera */\n> > > > diff --git a/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > b/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > index 59e8fb18..7322f0e8 100644\n> > > > --- a/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > +++ b/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > @@ -2,7 +2,6 @@\n> > > >\n> > > >  libcamera_sources += files([\n> > > >      'delayed_controls.cpp',\n> > > > -    'dma_heaps.cpp',\n> > > >      'raspberrypi.cpp',\n> > > >      'rpi_stream.cpp',\n> > > >  ])\n> > > > diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > index 84120954..b7e0d031 100644\n> > > > --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > @@ -21,6 +21,7 @@\n> > > >  #include <libcamera/camera.h>\n> > > >  #include <libcamera/control_ids.h>\n> > > >  #include <libcamera/formats.h>\n> > > > +#include <libcamera/heap_allocator.h>\n> > > >  #include <libcamera/ipa/raspberrypi_ipa_interface.h>\n> > > >  #include <libcamera/ipa/raspberrypi_ipa_proxy.h>\n> > > >  #include <libcamera/logging.h>\n> > > > @@ -44,7 +45,6 @@\n> > > >  #include \"libcamera/internal/yaml_parser.h\"\n> > > >\n> > > >  #include \"delayed_controls.h\"\n> > > > -#include \"dma_heaps.h\"\n> > > >  #include \"rpi_stream.h\"\n> > > >\n> > > >  using namespace std::chrono_literals;\n> > > > @@ -245,7 +245,7 @@ public:\n> > > >         std::vector<std::pair<std::unique_ptr<V4L2Subdevice>, MediaLink\n> > > > *>> bridgeDevices_;\n> > > >\n> > > >         /* DMAHEAP allocation helper. */\n> > > > -       RPi::DmaHeap dmaHeap_;\n> > > > +       HeapAllocator heapAllocator_;\n> > > >         SharedFD lsTable_;\n> > > >\n> > > >         std::unique_ptr<RPi::DelayedControls> delayedCtrls_;\n> > > > @@ -1293,7 +1293,7 @@ int\n> > PipelineHandlerRPi::registerCamera(MediaDevice\n> > > > *unicam, MediaDevice *isp, Me\n> > > >  {\n> > > >         std::unique_ptr<RPiCameraData> data =\n> > > > std::make_unique<RPiCameraData>(this);\n> > > >\n> > > > -       if (!data->dmaHeap_.isValid())\n> > > > +       if (!data->heapAllocator_.isValid())\n> > > >                 return -ENOMEM;\n> > > >\n> > > >         MediaEntity *unicamImage =\n> > unicam->getEntityByName(\"unicam-image\");\n> > > > @@ -1680,9 +1680,9 @@ int RPiCameraData::configureIPA(const\n> > > > CameraConfiguration *config, ipa::RPi::IPA\n> > > >         /* Always send the user transform to the IPA. */\n> > > >         ipaConfig.transform = static_cast<unsigned\n> > int>(config->transform);\n> > > >\n> > > > -       /* Allocate the lens shading table via dmaHeap and pass to the\n> > > > IPA. */\n> > > > +       /* Allocate the lens shading table via heapAllocator and pass\n> > to\n> > > > the IPA. */\n> > > >         if (!lsTable_.isValid()) {\n> > > > -               lsTable_ = SharedFD(dmaHeap_.alloc(\"ls_grid\",\n> > > > ipa::RPi::MaxLsGridSize));\n> > > > +               lsTable_ = SharedFD(heapAllocator_.alloc(\"ls_grid\",\n> > > > ipa::RPi::MaxLsGridSize));\n> > > >                 if (!lsTable_.isValid())\n> > > >                         return -ENOMEM;\n> > > >\n> > > > --\n> > > > 2.39.2.722.g9855ee24e9-goog\n> > > >\n> > > >\n> >","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 5BE9CBD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 17 Mar 2023 21:48:44 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 62E70626E5;\n\tFri, 17 Mar 2023 22:48:43 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 15F91626DB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 17 Mar 2023 22:48:41 +0100 (CET)","from pendragon.ideasonboard.com\n\t(aztw-30-b2-v4wan-166917-cust845.vm26.cable.virginm.net\n\t[82.37.23.78])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 733655AA;\n\tFri, 17 Mar 2023 22:48:40 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679089723;\n\tbh=uAd3/rDDYI1mqj57TMvapl5f+P/PxjiY/1PxtjCtXl8=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=prEy4yGJMcI69LfsVhVcqaP9SVI1ELY+yyb2KT9tg+T5rAIbbHxMPp4+FRQ6yXtek\n\tw3NtHSA1S/h19vgTvFJLTVqT+3rsLvwnDSJ9S+IRG7YPjdbt+KBaDCDxBXCxxz/iBB\n\tjhquW65cZo6SzVkdYuPigxo55ZbA7v24lvoSQFFdk2SA2GjiVmpEZtwhL7X4p+JSrK\n\tVrlQnLAlKJxscyWaEnScVwckLk9TwecXvgocIFHZN2SPLvy1mRQY7VJa+NJi1MfDCe\n\ttKSQ1p7P0PTDtSFmHXR59jEaKlYmsfi6rXBwqKdkoV+dUJxqpcBOxgcAEZGHrXlwD8\n\tWs+8g7BS0khew==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1679089720;\n\tbh=uAd3/rDDYI1mqj57TMvapl5f+P/PxjiY/1PxtjCtXl8=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=wGzgMvp5vBHLbfmeLVAdB/394Ey1XJo5UET4UUULttJvDSDb5ZdbN/SgrFV6gESjo\n\toaakPCFDAL+/NW+iCTf1EmxQQXJcTqKNVMOEJtsatrb7iZI0N/MOIJs6Uy087wG2sE\n\tIRask4u10J6qyt8lrfVIXu/bEvua3FHy1evlNliw="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"wGzgMvp5\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<CAEB1ahvBnGzeFpLZus4-usSz2wdOujj-bK5aU7B8LE=XLG=8tA@mail.gmail.com>","References":"<20230302081349.3334290-1-chenghaoyang@google.com>\n\t<20230302081349.3334290-2-chenghaoyang@google.com>\n\t<CAEB1ahvoF=Y807_N6Vc0gMhYOF+iO=5s5nB3J1h=0O2U=uiYFA@mail.gmail.com>\n\t<167775847158.93391.1555103143960612315@Monstersaurus>\n\t<CAEB1ahvBnGzeFpLZus4-usSz2wdOujj-bK5aU7B8LE=XLG=8tA@mail.gmail.com>","To":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Date":"Fri, 17 Mar 2023 21:48:38 +0000","Message-ID":"<167908971816.2537120.13888882718500541416@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","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>","From":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":26662,"web_url":"https://patchwork.libcamera.org/comment/26662/","msgid":"<167909073972.2537120.2246344740495978247@Monstersaurus>","date":"2023-03-17T22:05:39","subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Kieran Bingham (2023-03-17 21:48:38)\n> Quoting Cheng-Hao Yang (2023-03-16 15:50:29)\n> > Thanks Kieran for the input. I found it difficult to enable dma/udma in\n> > Google's linux distribution though, so I tested it in my personal Arch\n> > linux instead (udmabuf is available).\n> > \n> > However, I found that udmabuf only works if the requested size is power of\n> > 2 (ex: 4194304 for 1920*1080). Is it expected, or there's something wrong\n> > with my linux udmabuf support?\n> \n> I would expect that it can only allocate a multiple of the PAGE_SIZE\n> configured on the Kernel.\n> \n> You could investigate that and decide to round up to the next PAGE_SIZE,\n> or if the kernel does this, just let the allocation succeed as long as\n> it is equal or larger than the requested size?\n\nIn fact, yes - looking at the unit tests for udmabuf:\n\nhttps://elixir.bootlin.com/linux/latest/source/tools/testing/selftests/drivers/dma-buf/udmabuf.c#L72\n\n\t/* should fail (size not multiple of page) */\n\tcreate.memfd  = memfd;\n\tcreate.offset = 0;\n\tcreate.size   = getpagesize()/2;\n\tbuf = ioctl(devfd, UDMABUF_CREATE, &create);\n\tif (buf >= 0) {\n\t\tprintf(\"%s: [FAIL,test-2]\\n\", TEST_PREFIX);\n\t\texit(1);\n\t}\n\nThe allocation appears to be expected to be a direct multiple of\nPAGE_SIZE..\n\nSo it looks like we would need to manage this rounding in the allocator\nwith:\n\n       #include <unistd.h>\n       int getpagesize(void);\n\n--\nKieran\n\n\n> \n> --\n> Kieran\n> \n> \n> > \n> > BR,\n> > Harvey\n> > \n> > On Thu, Mar 2, 2023 at 8:01 PM Kieran Bingham <\n> > kieran.bingham@ideasonboard.com> wrote:\n> > \n> > > Quoting Cheng-Hao Yang (2023-03-02 08:25:00)\n> > > > Hi Kieran,\n> > > >\n> > > > Sorry that I'm new to dma heap: I was trying to test virtual pipeline\n> > > > handler on dma heap\n> > > > (or the udma heap I add in the following patch), while I find that my\n> > > > workstation doesn't\n> > > > have the dma heap directory (`/dev/dma_heap` or `/dev/udmabuf`). I heard\n> > > > from Han-lin\n> > > > that we might need to enable it in the linux kernel. Is that right? Is\n> > > > there a doc/tutorial that\n> > > > I can follow?\n> > >\n> > > It could be kernel specific indeed. I guess it's up to your\n> > > distribution?\n> > >\n> > > UDMABUF\n> > >  -\n> > > https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L33\n> > >\n> > > DMABUF_HEAPS\n> > > -\n> > > https://github.com/torvalds/linux/blob/ee3f96b164688dae21e2466a57f2e806b64e8a37/drivers/dma-buf/Kconfig#L68\n> > >\n> > >\n> > > --\n> > > Regards\n> > >\n> > > Kieran\n> > >\n> > >\n> > >\n> > > >\n> > > > Thanks!\n> > > > Harvey\n> > > >\n> > > >\n> > > >\n> > > > On Thu, Mar 2, 2023 at 4:13 PM Harvey Yang <chenghaoyang@chromium.org>\n> > > > wrote:\n> > > >\n> > > > > As other components (like the WIP virtual pipeline handler) also need a\n> > > > > heap allocator, move DmaHeap from raspberry pi pipeline handler to a\n> > > > > general HeapAllocator as a base class.\n> > > > >\n> > > > > Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> > > > > ---\n> > > > >  include/libcamera/dma_heap.h                  | 20 +++++++++\n> > > > >  include/libcamera/heap.h                      | 27 ++++++++++++\n> > > > >  include/libcamera/heap_allocator.h            | 30 +++++++++++++\n> > > > >  include/libcamera/meson.build                 |  3 ++\n> > > > >  .../dma_heaps.cpp => dma_heap.cpp}            | 35 +++++++--------\n> > > > >  src/libcamera/heap_allocator.cpp              | 43 +++++++++++++++++++\n> > > > >  src/libcamera/meson.build                     |  2 +\n> > > > >  .../pipeline/raspberrypi/dma_heaps.h          | 32 --------------\n> > > > >  .../pipeline/raspberrypi/meson.build          |  1 -\n> > > > >  .../pipeline/raspberrypi/raspberrypi.cpp      | 10 ++---\n> > > > >  10 files changed, 146 insertions(+), 57 deletions(-)\n> > > > >  create mode 100644 include/libcamera/dma_heap.h\n> > > > >  create mode 100644 include/libcamera/heap.h\n> > > > >  create mode 100644 include/libcamera/heap_allocator.h\n> > > > >  rename src/libcamera/{pipeline/raspberrypi/dma_heaps.cpp =>\n> > > dma_heap.cpp}\n> > > > > (69%)\n> > > > >  create mode 100644 src/libcamera/heap_allocator.cpp\n> > > > >  delete mode 100644 src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > > >\n> > > > > diff --git a/include/libcamera/dma_heap.h\n> > > b/include/libcamera/dma_heap.h\n> > > > > new file mode 100644\n> > > > > index 00000000..a663c317\n> > > > > --- /dev/null\n> > > > > +++ b/include/libcamera/dma_heap.h\n> > > > > @@ -0,0 +1,20 @@\n> > > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > > +/*\n> > > > > + * Copyright (C) 2020, Raspberry Pi Ltd\n> > > > > + *\n> > > > > + * dma_heap.h - Dma Heap implementation.\n> > > > > + */\n> > > > > +\n> > > > > +#include <libcamera/heap.h>\n> > > > > +\n> > > > > +namespace libcamera {\n> > > > > +\n> > > > > +class DmaHeap : public Heap\n> > > > > +{\n> > > > > +public:\n> > > > > +       DmaHeap();\n> > > > > +       ~DmaHeap();\n> > > > > +       UniqueFD alloc(const char *name, std::size_t size) override;\n> > > > > +};\n> > > > > +\n> > > > > +} /* namespace libcamera */\n> > > > > diff --git a/include/libcamera/heap.h b/include/libcamera/heap.h\n> > > > > new file mode 100644\n> > > > > index 00000000..c49a2ac3\n> > > > > --- /dev/null\n> > > > > +++ b/include/libcamera/heap.h\n> > > > > @@ -0,0 +1,27 @@\n> > > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > > +/*\n> > > > > + * Copyright (C) 2023, Google Inc.\n> > > > > + *\n> > > > > + * heap.h - Heap interface.\n> > > > > + */\n> > > > > +\n> > > > > +#pragma once\n> > > > > +\n> > > > > +#include <stddef.h>\n> > > > > +\n> > > > > +#include <libcamera/base/unique_fd.h>\n> > > > > +\n> > > > > +namespace libcamera {\n> > > > > +\n> > > > > +class Heap\n> > > > > +{\n> > > > > +public:\n> > > > > +       virtual ~Heap() = default;\n> > > > > +       bool isValid() const { return handle_.isValid(); }\n> > > > > +       virtual UniqueFD alloc(const char *name, std::size_t size) = 0;\n> > > > > +\n> > > > > +protected:\n> > > > > +       UniqueFD handle_;\n> > > > > +};\n> > > > > +\n> > > > > +} /* namespace libcamera */\n> > > > > diff --git a/include/libcamera/heap_allocator.h\n> > > > > b/include/libcamera/heap_allocator.h\n> > > > > new file mode 100644\n> > > > > index 00000000..cd7ed1a3\n> > > > > --- /dev/null\n> > > > > +++ b/include/libcamera/heap_allocator.h\n> > > > > @@ -0,0 +1,30 @@\n> > > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > > +/*\n> > > > > + * Copyright (C) 2023, Google Inc.\n> > > > > + *\n> > > > > + * heap_allocator.h - Helper class for heap buffer allocations.\n> > > > > + */\n> > > > > +\n> > > > > +#pragma once\n> > > > > +\n> > > > > +#include <stddef.h>\n> > > > > +\n> > > > > +#include <libcamera/base/unique_fd.h>\n> > > > > +\n> > > > > +#include <libcamera/heap.h>\n> > > > > +\n> > > > > +namespace libcamera {\n> > > > > +\n> > > > > +class HeapAllocator\n> > > > > +{\n> > > > > +public:\n> > > > > +       HeapAllocator();\n> > > > > +       ~HeapAllocator();\n> > > > > +       bool isValid() const { return heap_->isValid(); }\n> > > > > +       UniqueFD alloc(const char *name, std::size_t size);\n> > > > > +\n> > > > > +private:\n> > > > > +       std::unique_ptr<Heap> heap_;\n> > > > > +};\n> > > > > +\n> > > > > +} /* namespace libcamera */\n> > > > > diff --git a/include/libcamera/meson.build\n> > > b/include/libcamera/meson.build\n> > > > > index 408b7acf..f486630a 100644\n> > > > > --- a/include/libcamera/meson.build\n> > > > > +++ b/include/libcamera/meson.build\n> > > > > @@ -7,10 +7,13 @@ libcamera_public_headers = files([\n> > > > >      'camera_manager.h',\n> > > > >      'color_space.h',\n> > > > >      'controls.h',\n> > > > > +    'dma_heap.h',\n> > > > >      'fence.h',\n> > > > >      'framebuffer.h',\n> > > > >      'framebuffer_allocator.h',\n> > > > >      'geometry.h',\n> > > > > +    'heap.h',\n> > > > > +    'heap_allocator.h',\n> > > > >      'logging.h',\n> > > > >      'pixel_format.h',\n> > > > >      'request.h',\n> > > > > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > > > b/src/libcamera/dma_heap.cpp\n> > > > > similarity index 69%\n> > > > > rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > > > rename to src/libcamera/dma_heap.cpp\n> > > > > index 6b644406..02415975 100644\n> > > > > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n> > > > > +++ b/src/libcamera/dma_heap.cpp\n> > > > > @@ -2,18 +2,19 @@\n> > > > >  /*\n> > > > >   * Copyright (C) 2020, Raspberry Pi Ltd\n> > > > >   *\n> > > > > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > > > > + * dma_heap.h - Dma Heap implementation.\n> > > > >   */\n> > > > >\n> > > > > -#include \"dma_heaps.h\"\n> > > > > +#include <libcamera/dma_heap.h>\n> > > > >\n> > > > >  #include <array>\n> > > > >  #include <fcntl.h>\n> > > > > -#include <linux/dma-buf.h>\n> > > > > -#include <linux/dma-heap.h>\n> > > > >  #include <sys/ioctl.h>\n> > > > >  #include <unistd.h>\n> > > > >\n> > > > > +#include <linux/dma-buf.h>\n> > > > > +#include <linux/dma-heap.h>\n> > > > > +\n> > > > >  #include <libcamera/base/log.h>\n> > > > >\n> > > > >  /*\n> > > > > @@ -30,9 +31,7 @@ static constexpr std::array<const char *, 2>\n> > > heapNames =\n> > > > > {\n> > > > >\n> > > > >  namespace libcamera {\n> > > > >\n> > > > > -LOG_DECLARE_CATEGORY(RPI)\n> > > > > -\n> > > > > -namespace RPi {\n> > > > > +LOG_DEFINE_CATEGORY(DmaHeap)\n> > > > >\n> > > > >  DmaHeap::DmaHeap()\n> > > > >  {\n> > > > > @@ -40,17 +39,17 @@ DmaHeap::DmaHeap()\n> > > > >                 int ret = ::open(name, O_RDWR, 0);\n> > > > >                 if (ret < 0) {\n> > > > >                         ret = errno;\n> > > > > -                       LOG(RPI, Debug) << \"Failed to open \" << name\n> > > << \":\n> > > > > \"\n> > > > > -                                       << strerror(ret);\n> > > > > +                       LOG(DmaHeap, Debug) << \"Failed to open \" <<\n> > > name\n> > > > > << \": \"\n> > > > > +                                           << strerror(ret);\n> > > > >                         continue;\n> > > > >                 }\n> > > > >\n> > > > > -               dmaHeapHandle_ = UniqueFD(ret);\n> > > > > +               handle_ = UniqueFD(ret);\n> > > > >                 break;\n> > > > >         }\n> > > > >\n> > > > > -       if (!dmaHeapHandle_.isValid())\n> > > > > -               LOG(RPI, Error) << \"Could not open any dmaHeap device\";\n> > > > > +       if (!handle_.isValid())\n> > > > > +               LOG(DmaHeap, Error) << \"Could not open any dmaHeap\n> > > device\";\n> > > > >  }\n> > > > >\n> > > > >  DmaHeap::~DmaHeap() = default;\n> > > > > @@ -67,24 +66,22 @@ UniqueFD DmaHeap::alloc(const char *name,\n> > > std::size_t\n> > > > > size)\n> > > > >         alloc.len = size;\n> > > > >         alloc.fd_flags = O_CLOEXEC | O_RDWR;\n> > > > >\n> > > > > -       ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC,\n> > > &alloc);\n> > > > > +       ret = ::ioctl(handle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);\n> > > > >         if (ret < 0) {\n> > > > > -               LOG(RPI, Error) << \"dmaHeap allocation failure for \"\n> > > > > -                               << name;\n> > > > > +               LOG(DmaHeap, Error) << \"dmaHeap allocation failure for\n> > > \"\n> > > > > +                                   << name;\n> > > > >                 return {};\n> > > > >         }\n> > > > >\n> > > > >         UniqueFD allocFd(alloc.fd);\n> > > > >         ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);\n> > > > >         if (ret < 0) {\n> > > > > -               LOG(RPI, Error) << \"dmaHeap naming failure for \"\n> > > > > -                               << name;\n> > > > > +               LOG(DmaHeap, Error) << \"dmaHeap naming failure for \"\n> > > > > +                                   << name;\n> > > > >                 return {};\n> > > > >         }\n> > > > >\n> > > > >         return allocFd;\n> > > > >  }\n> > > > >\n> > > > > -} /* namespace RPi */\n> > > > > -\n> > > > >  } /* namespace libcamera */\n> > > > > diff --git a/src/libcamera/heap_allocator.cpp\n> > > > > b/src/libcamera/heap_allocator.cpp\n> > > > > new file mode 100644\n> > > > > index 00000000..594b1d6a\n> > > > > --- /dev/null\n> > > > > +++ b/src/libcamera/heap_allocator.cpp\n> > > > > @@ -0,0 +1,43 @@\n> > > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > > +/*\n> > > > > + * Copyright (C) 2023, Google Inc.\n> > > > > + *\n> > > > > + * heap_allocator.cpp - Helper class for heap buffer allocations.\n> > > > > + */\n> > > > > +\n> > > > > +#include <libcamera/heap_allocator.h>\n> > > > > +\n> > > > > +#include <array>\n> > > > > +#include <fcntl.h>\n> > > > > +#include <sys/ioctl.h>\n> > > > > +#include <unistd.h>\n> > > > > +\n> > > > > +#include <linux/dma-buf.h>\n> > > > > +#include <linux/dma-heap.h>\n> > > > > +\n> > > > > +#include <libcamera/base/log.h>\n> > > > > +\n> > > > > +#include <libcamera/dma_heap.h>\n> > > > > +\n> > > > > +namespace libcamera {\n> > > > > +\n> > > > > +LOG_DEFINE_CATEGORY(HeapAllocator)\n> > > > > +\n> > > > > +HeapAllocator::HeapAllocator()\n> > > > > +{\n> > > > > +       heap_ = std::make_unique<DmaHeap>();\n> > > > > +}\n> > > > > +\n> > > > > +HeapAllocator::~HeapAllocator() = default;\n> > > > > +\n> > > > > +UniqueFD HeapAllocator::alloc(const char *name, std::size_t size)\n> > > > > +{\n> > > > > +       if (!isValid()) {\n> > > > > +               LOG(HeapAllocator, Fatal) << \"Allocation attempted\n> > > without\n> > > > > allocator\" << name;\n> > > > > +               return {};\n> > > > > +       }\n> > > > > +\n> > > > > +       return heap_->alloc(name, size);\n> > > > > +}\n> > > > > +\n> > > > > +} /* namespace libcamera */\n> > > > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> > > > > index 9869bfe7..ee586c0d 100644\n> > > > > --- a/src/libcamera/meson.build\n> > > > > +++ b/src/libcamera/meson.build\n> > > > > @@ -17,11 +17,13 @@ libcamera_sources = files([\n> > > > >      'delayed_controls.cpp',\n> > > > >      'device_enumerator.cpp',\n> > > > >      'device_enumerator_sysfs.cpp',\n> > > > > +    'dma_heap.cpp',\n> > > > >      'fence.cpp',\n> > > > >      'formats.cpp',\n> > > > >      'framebuffer.cpp',\n> > > > >      'framebuffer_allocator.cpp',\n> > > > >      'geometry.cpp',\n> > > > > +    'heap_allocator.cpp',\n> > > > >      'ipa_controls.cpp',\n> > > > >      'ipa_data_serializer.cpp',\n> > > > >      'ipa_interface.cpp',\n> > > > > diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > > > b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > > > deleted file mode 100644\n> > > > > index 0a4a8d86..00000000\n> > > > > --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n> > > > > +++ /dev/null\n> > > > > @@ -1,32 +0,0 @@\n> > > > > -/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > > > > -/*\n> > > > > - * Copyright (C) 2020, Raspberry Pi Ltd\n> > > > > - *\n> > > > > - * dma_heaps.h - Helper class for dma-heap allocations.\n> > > > > - */\n> > > > > -\n> > > > > -#pragma once\n> > > > > -\n> > > > > -#include <stddef.h>\n> > > > > -\n> > > > > -#include <libcamera/base/unique_fd.h>\n> > > > > -\n> > > > > -namespace libcamera {\n> > > > > -\n> > > > > -namespace RPi {\n> > > > > -\n> > > > > -class DmaHeap\n> > > > > -{\n> > > > > -public:\n> > > > > -       DmaHeap();\n> > > > > -       ~DmaHeap();\n> > > > > -       bool isValid() const { return dmaHeapHandle_.isValid(); }\n> > > > > -       UniqueFD alloc(const char *name, std::size_t size);\n> > > > > -\n> > > > > -private:\n> > > > > -       UniqueFD dmaHeapHandle_;\n> > > > > -};\n> > > > > -\n> > > > > -} /* namespace RPi */\n> > > > > -\n> > > > > -} /* namespace libcamera */\n> > > > > diff --git a/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > > b/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > > index 59e8fb18..7322f0e8 100644\n> > > > > --- a/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > > +++ b/src/libcamera/pipeline/raspberrypi/meson.build\n> > > > > @@ -2,7 +2,6 @@\n> > > > >\n> > > > >  libcamera_sources += files([\n> > > > >      'delayed_controls.cpp',\n> > > > > -    'dma_heaps.cpp',\n> > > > >      'raspberrypi.cpp',\n> > > > >      'rpi_stream.cpp',\n> > > > >  ])\n> > > > > diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > > b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > > index 84120954..b7e0d031 100644\n> > > > > --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > > +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > > > > @@ -21,6 +21,7 @@\n> > > > >  #include <libcamera/camera.h>\n> > > > >  #include <libcamera/control_ids.h>\n> > > > >  #include <libcamera/formats.h>\n> > > > > +#include <libcamera/heap_allocator.h>\n> > > > >  #include <libcamera/ipa/raspberrypi_ipa_interface.h>\n> > > > >  #include <libcamera/ipa/raspberrypi_ipa_proxy.h>\n> > > > >  #include <libcamera/logging.h>\n> > > > > @@ -44,7 +45,6 @@\n> > > > >  #include \"libcamera/internal/yaml_parser.h\"\n> > > > >\n> > > > >  #include \"delayed_controls.h\"\n> > > > > -#include \"dma_heaps.h\"\n> > > > >  #include \"rpi_stream.h\"\n> > > > >\n> > > > >  using namespace std::chrono_literals;\n> > > > > @@ -245,7 +245,7 @@ public:\n> > > > >         std::vector<std::pair<std::unique_ptr<V4L2Subdevice>, MediaLink\n> > > > > *>> bridgeDevices_;\n> > > > >\n> > > > >         /* DMAHEAP allocation helper. */\n> > > > > -       RPi::DmaHeap dmaHeap_;\n> > > > > +       HeapAllocator heapAllocator_;\n> > > > >         SharedFD lsTable_;\n> > > > >\n> > > > >         std::unique_ptr<RPi::DelayedControls> delayedCtrls_;\n> > > > > @@ -1293,7 +1293,7 @@ int\n> > > PipelineHandlerRPi::registerCamera(MediaDevice\n> > > > > *unicam, MediaDevice *isp, Me\n> > > > >  {\n> > > > >         std::unique_ptr<RPiCameraData> data =\n> > > > > std::make_unique<RPiCameraData>(this);\n> > > > >\n> > > > > -       if (!data->dmaHeap_.isValid())\n> > > > > +       if (!data->heapAllocator_.isValid())\n> > > > >                 return -ENOMEM;\n> > > > >\n> > > > >         MediaEntity *unicamImage =\n> > > unicam->getEntityByName(\"unicam-image\");\n> > > > > @@ -1680,9 +1680,9 @@ int RPiCameraData::configureIPA(const\n> > > > > CameraConfiguration *config, ipa::RPi::IPA\n> > > > >         /* Always send the user transform to the IPA. */\n> > > > >         ipaConfig.transform = static_cast<unsigned\n> > > int>(config->transform);\n> > > > >\n> > > > > -       /* Allocate the lens shading table via dmaHeap and pass to the\n> > > > > IPA. */\n> > > > > +       /* Allocate the lens shading table via heapAllocator and pass\n> > > to\n> > > > > the IPA. */\n> > > > >         if (!lsTable_.isValid()) {\n> > > > > -               lsTable_ = SharedFD(dmaHeap_.alloc(\"ls_grid\",\n> > > > > ipa::RPi::MaxLsGridSize));\n> > > > > +               lsTable_ = SharedFD(heapAllocator_.alloc(\"ls_grid\",\n> > > > > ipa::RPi::MaxLsGridSize));\n> > > > >                 if (!lsTable_.isValid())\n> > > > >                         return -ENOMEM;\n> > > > >\n> > > > > --\n> > > > > 2.39.2.722.g9855ee24e9-goog\n> > > > >\n> > > > >\n> > >","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 2A1F9C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 17 Mar 2023 22:05:45 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 79F16626E5;\n\tFri, 17 Mar 2023 23:05:44 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7B42A626DB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 17 Mar 2023 23:05:42 +0100 (CET)","from pendragon.ideasonboard.com\n\t(aztw-30-b2-v4wan-166917-cust845.vm26.cable.virginm.net\n\t[82.37.23.78])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E795C5AA;\n\tFri, 17 Mar 2023 23:05:41 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679090744;\n\tbh=yoAWTKdIthozV6C8fEtKJCJsD+G2YyXCidp5njDanbo=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=GkOW2TBVKf8uANbvemv3Lu4B4O2/3mkrhIhOTN+vz2Jj+KQAn2Hzb7f8L+/PFZaHf\n\t9G/LqbPwhzC1eZPcMneCTJkzcixdZUDTUIUBTZ4MeEWJuuitwY+226xRUS8FcafQhN\n\t3n7iDzc++2Ij0yw15Mq90hCMsQqs8Cf2xyyJl2+tjBGckZsxvw/kj7GCw95b+xTgcS\n\tJMnvUalnebVdmMbmo5hB8C9Z0cn63vni/l+HLi6bwnLXowFf3Ik+6bGcqogrsmbg69\n\tQO/MscTqdKqn+IknUjWdM5scZ5MbkgpXr3tlxpG68rxUom/icfN1B9AEyi/bkKcsYy\n\t4uFFx6mHLxaxA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1679090742;\n\tbh=yoAWTKdIthozV6C8fEtKJCJsD+G2YyXCidp5njDanbo=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=f1Q82rHKZEaaZSlSmRVqdnAW6uZV5MnkY0B4UhDNmYLIfW4Ccr4rvhN+6DhJdIdLq\n\tef0gpAXVrJNINuV1YHHD4yxbKNFgsogTjcLTW5NlO3ia9ULJA26zlYLMhoJOsZjAUu\n\tBVVe+s+5LZ6upvO5cR9YB9WveuKyrWo9E5HeKLD8="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"f1Q82rHK\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<167908971816.2537120.13888882718500541416@Monstersaurus>","References":"<20230302081349.3334290-1-chenghaoyang@google.com>\n\t<20230302081349.3334290-2-chenghaoyang@google.com>\n\t<CAEB1ahvoF=Y807_N6Vc0gMhYOF+iO=5s5nB3J1h=0O2U=uiYFA@mail.gmail.com>\n\t<167775847158.93391.1555103143960612315@Monstersaurus>\n\t<CAEB1ahvBnGzeFpLZus4-usSz2wdOujj-bK5aU7B8LE=XLG=8tA@mail.gmail.com>\n\t<167908971816.2537120.13888882718500541416@Monstersaurus>","To":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Date":"Fri, 17 Mar 2023 22:05:39 +0000","Message-ID":"<167909073972.2537120.2246344740495978247@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [PATCH v3 1/3] libcamera: Move DmaHeap to\n\tHeapAllocator as a base class","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>","From":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]