Patch Detail
Show a patch.
GET /api/1.1/patches/8798/?format=api
{ "id": 8798, "url": "https://patchwork.libcamera.org/api/1.1/patches/8798/?format=api", "web_url": "https://patchwork.libcamera.org/patch/8798/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20200714120844.520732-3-naush@raspberrypi.com>", "date": "2020-07-14T12:08:44", "name": "[libcamera-devel,2/2] libcamera: pipeline: ipa: raspberrypi: Use dma heap allocs for LS tables", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "2c7966731d4b1802779bd13819afb4cf26850b5d", "submitter": { "id": 34, "url": "https://patchwork.libcamera.org/api/1.1/people/34/?format=api", "name": "Naushir Patuck", "email": "naush@raspberrypi.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/8798/mbox/", "series": [ { "id": 1105, "url": "https://patchwork.libcamera.org/api/1.1/series/1105/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1105", "date": "2020-07-14T12:08:42", "name": "Replace vcsm with dmabuf alloc", "version": 1, "mbox": "https://patchwork.libcamera.org/series/1105/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/8798/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/8798/checks/", "tags": {}, "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 ECF27BD792\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 14 Jul 2020 12:16:56 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 707FB60745;\n\tTue, 14 Jul 2020 14:16:56 +0200 (CEST)", "from mail-wm1-x334.google.com (mail-wm1-x334.google.com\n\t[IPv6:2a00:1450:4864:20::334])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 748E2605B0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 14 Jul 2020 14:16:55 +0200 (CEST)", "by mail-wm1-x334.google.com with SMTP id w3so5406093wmi.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 14 Jul 2020 05:16:55 -0700 (PDT)", "from naushir-VirtualBox.patuck.local ([88.97.76.4])\n\tby smtp.gmail.com with ESMTPSA id\n\ta15sm33123217wrh.54.2020.07.14.05.08.52\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 14 Jul 2020 05:08:52 -0700 (PDT)" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"lNoJVFC5\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=/RLHdpifo2Gdem+Ot91PO/bGXKxbDCDWoySl4v0NPiw=;\n\tb=lNoJVFC5HOWAstHvRO1IpVNUQl1tBPFYPG/D2QrfMtPKqo/BbmK1ZCveL5mCSW9EHi\n\to4so4Gg4kpAF0ks7NlPVzzhBSdC7LbgwFttrsibKlVFxewAqpDMy8aknVuYZ1JHk9+4Z\n\tbmOQgfWwfgyxHoe0Ub6kzy9KQ2lsu2Yqm7aRfal5upIPT8qit8vAEdbhcEkwtgXyZk5X\n\tDUsrOys9noNzZFTJe+9CmpiU2w2qiEgjNXaCB9Vv9FW47q5K0V99U+xxoxTiGYlmlVXc\n\tSBAht1V5bFRDhed7T6WMzBfLBzq+cxv0HyDmTxIpdR52VrYNcEcEw97jeDioJxIZL/uZ\n\t/8pw==", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=/RLHdpifo2Gdem+Ot91PO/bGXKxbDCDWoySl4v0NPiw=;\n\tb=R0pLlNv//rjvIGm350MMM+hcH1X2nQD5BldmjiI785bof+JabUbnj3ALDArOC4cbbm\n\tWOOeC9JmLNKfl+i2T5U6tMqeCGmy1y79JE1aMcbY65i8FgUQlrhRvLGKYEOjLhTcWwFB\n\tIgNuXnv9iED5jBouoadSHk/Wf8TANtsmt+NuHBWIbRSNrKfrj8uqIvl4e4GsxXLYMwez\n\tG5fNA1T866Zq5+cGs0Wpyq663Thy09sbLYuToHa3w1GKwa3yC+E+980TygteAUvC9ygf\n\tu9MunZ7FtCB8XuWCDrK4YgkVnnl1DjuhVU5hrIY6ICGjucY1laLD9z4nnYfWuMtFPwXG\n\t6pIg==", "X-Gm-Message-State": "AOAM530ixxWnM/w9sZsdnaPwjgGOB5hpmy5icwoZmFvKCMgGbE+928Uq\n\txUD+aDsVKDqABe0NQnoDj6iI23iuYKk=", "X-Google-Smtp-Source": "ABdhPJwVJ95YxrGjbIyuiymRnuJ34ODgMPTIizVR1yWtxiUMRaEIBFdHfu3V6NxO8d0FT7YBVn7IzA==", "X-Received": "by 2002:a05:600c:20d3:: with SMTP id\n\ty19mr4313246wmm.10.1594728533137; \n\tTue, 14 Jul 2020 05:08:53 -0700 (PDT)", "From": "Naushir Patuck <naush@raspberrypi.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Tue, 14 Jul 2020 13:08:44 +0100", "Message-Id": "<20200714120844.520732-3-naush@raspberrypi.com>", "X-Mailer": "git-send-email 2.25.1", "In-Reply-To": "<20200714120844.520732-1-naush@raspberrypi.com>", "References": "<20200714120844.520732-1-naush@raspberrypi.com>", "MIME-Version": "1.0", "Subject": "[libcamera-devel] [PATCH 2/2] libcamera: pipeline: ipa:\n\traspberrypi: Use dma heap allocs for LS tables", "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>", "Content-Type": "text/plain; charset=\"us-ascii\"", "Content-Transfer-Encoding": "7bit", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Remove use of vcsm allocations and replace with dma heap allocations.\nThe pipeline handler now passes the fd of the allocation over to the IPA\ninstead of the raw pointer.\n\nAlso use libcamera::FileDescriptor for fd lifetime management.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n include/linux/bcm2835-isp.h | 4 +-\n include/linux/vc_sm_cma_ioctl.h | 135 ----------------\n src/ipa/raspberrypi/raspberrypi.cpp | 27 +++-\n .../pipeline/raspberrypi/dma-heaps.h | 102 ++++++++++++\n .../pipeline/raspberrypi/raspberrypi.cpp | 44 ++----\n src/libcamera/pipeline/raspberrypi/vcsm.h | 149 ------------------\n 6 files changed, 138 insertions(+), 323 deletions(-)\n delete mode 100644 include/linux/vc_sm_cma_ioctl.h\n create mode 100644 src/libcamera/pipeline/raspberrypi/dma-heaps.h\n delete mode 100644 src/libcamera/pipeline/raspberrypi/vcsm.h", "diff": "diff --git a/include/linux/bcm2835-isp.h b/include/linux/bcm2835-isp.h\nindex e7afc367..45abb681 100644\n--- a/include/linux/bcm2835-isp.h\n+++ b/include/linux/bcm2835-isp.h\n@@ -108,7 +108,7 @@ enum bcm2835_isp_gain_format {\n * @grid_stride:\tRow to row distance (in grid cells) between grid cells\n *\t\t\tin the same horizontal location.\n * @grid_height:\tHeight of lens shading tables in grid cells.\n- * @mem_handle_table:\tMemory handle to the tables.\n+ * @dmabuf:\t\tdmabuf file handle containing the table.\n * @ref_transform:\tReference transform - unsupported, please pass zero.\n * @corner_sampled:\tWhether the gains are sampled at the corner points\n *\t\t\tof the grid cells or in the cell centres.\n@@ -120,7 +120,7 @@ struct bcm2835_isp_lens_shading {\n \t__u32 grid_width;\n \t__u32 grid_stride;\n \t__u32 grid_height;\n-\t__u32 mem_handle_table;\n+\t__s32 dmabuf;\n \t__u32 ref_transform;\n \t__u32 corner_sampled;\n \t__u32 gain_format;\ndiff --git a/include/linux/vc_sm_cma_ioctl.h b/include/linux/vc_sm_cma_ioctl.h\ndeleted file mode 100644\nindex 21b8758e..00000000\n--- a/include/linux/vc_sm_cma_ioctl.h\n+++ /dev/null\n@@ -1,135 +0,0 @@\n-/*\n-Copyright (c) 2012, Broadcom Europe Ltd\n-All rights reserved.\n-\n-Redistribution and use in source and binary forms, with or without\n-modification, are permitted provided that the following conditions are met:\n- * Redistributions of source code must retain the above copyright\n- notice, this list of conditions and the following disclaimer.\n- * Redistributions in binary form must reproduce the above copyright\n- notice, this list of conditions and the following disclaimer in the\n- documentation and/or other materials provided with the distribution.\n- * Neither the name of the copyright holder nor the\n- names of its contributors may be used to endorse or promote products\n- derived from this software without specific prior written permission.\n-\n-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n-DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY\n-DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n-(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n-LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n-ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n-SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n-*/\n-\n-/*\n- * Copyright 2019 Raspberry Pi (Trading) Ltd. All rights reserved.\n- *\n- * Based on vmcs_sm_ioctl.h Copyright Broadcom Corporation.\n- */\n-\n-#ifndef __VC_SM_CMA_IOCTL_H\n-#define __VC_SM_CMA_IOCTL_H\n-\n-/* ---- Include Files ---------------------------------------------------- */\n-\n-#include <linux/types.h>\t/* Needed for standard types */\n-\n-#include <linux/ioctl.h>\n-\n-/* ---- Constants and Types ---------------------------------------------- */\n-\n-#define VC_SM_CMA_RESOURCE_NAME 32\n-#define VC_SM_CMA_RESOURCE_NAME_DEFAULT \"sm-host-resource\"\n-\n-/* Type define used to create unique IOCTL number */\n-#define VC_SM_CMA_MAGIC_TYPE 'J'\n-\n-/* IOCTL commands on /dev/vc-sm-cma */\n-enum vc_sm_cma_cmd_e {\n-\tVC_SM_CMA_CMD_ALLOC = 0x5A,\t/* Start at 0x5A arbitrarily */\n-\n-\tVC_SM_CMA_CMD_IMPORT_DMABUF,\n-\n-\tVC_SM_CMA_CMD_CLEAN_INVALID2,\n-\n-\tVC_SM_CMA_CMD_LAST\t/* Do not delete */\n-};\n-\n-/* Cache type supported, conveniently matches the user space definition in\n- * user-vcsm.h.\n- */\n-enum vc_sm_cma_cache_e {\n-\tVC_SM_CMA_CACHE_NONE,\n-\tVC_SM_CMA_CACHE_HOST,\n-\tVC_SM_CMA_CACHE_VC,\n-\tVC_SM_CMA_CACHE_BOTH,\n-};\n-\n-/* IOCTL Data structures */\n-struct vc_sm_cma_ioctl_alloc {\n-\t/* user -> kernel */\n-\t__u32 size;\n-\t__u32 num;\n-\t__u32 cached;\t\t/* enum vc_sm_cma_cache_e */\n-\t__u32 pad;\n-\t__u8 name[VC_SM_CMA_RESOURCE_NAME];\n-\n-\t/* kernel -> user */\n-\t__s32 handle;\n-\t__u32 vc_handle;\n-\t__u64 dma_addr;\n-};\n-\n-struct vc_sm_cma_ioctl_import_dmabuf {\n-\t/* user -> kernel */\n-\t__s32 dmabuf_fd;\n-\t__u32 cached;\t\t/* enum vc_sm_cma_cache_e */\n-\t__u8 name[VC_SM_CMA_RESOURCE_NAME];\n-\n-\t/* kernel -> user */\n-\t__s32 handle;\n-\t__u32 vc_handle;\n-\t__u32 size;\n-\t__u32 pad;\n-\t__u64 dma_addr;\n-};\n-\n-/*\n- * Cache functions to be set to struct vc_sm_cma_ioctl_clean_invalid2\n- * invalidate_mode.\n- */\n-#define VC_SM_CACHE_OP_NOP 0x00\n-#define VC_SM_CACHE_OP_INV 0x01\n-#define VC_SM_CACHE_OP_CLEAN 0x02\n-#define VC_SM_CACHE_OP_FLUSH 0x03\n-\n-struct vc_sm_cma_ioctl_clean_invalid2 {\n-\t__u32 op_count;\n-\t__u32 pad;\n-\tstruct vc_sm_cma_ioctl_clean_invalid_block {\n-\t\t__u32 invalidate_mode;\n-\t\t__u32 block_count;\n-\t\tvoid * /*__user */start_address;\n-\t\t__u32 block_size;\n-\t\t__u32 inter_block_stride;\n-\t} s[0];\n-};\n-\n-/* IOCTL numbers */\n-#define VC_SM_CMA_IOCTL_MEM_ALLOC\\\n-\t_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_ALLOC,\\\n-\t struct vc_sm_cma_ioctl_alloc)\n-\n-#define VC_SM_CMA_IOCTL_MEM_IMPORT_DMABUF\\\n-\t_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_IMPORT_DMABUF,\\\n-\t struct vc_sm_cma_ioctl_import_dmabuf)\n-\n-#define VC_SM_CMA_IOCTL_MEM_CLEAN_INVALID2\\\n-\t_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_CLEAN_INVALID2,\\\n-\t struct vc_sm_cma_ioctl_clean_invalid2)\n-\n-#endif /* __VC_SM_CMA_IOCTL_H */\ndiff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\nindex b1f27861..562907e5 100644\n--- a/src/ipa/raspberrypi/raspberrypi.cpp\n+++ b/src/ipa/raspberrypi/raspberrypi.cpp\n@@ -15,6 +15,7 @@\n #include <libcamera/buffer.h>\n #include <libcamera/control_ids.h>\n #include <libcamera/controls.h>\n+#include <libcamera/file_descriptor.h>\n #include <libcamera/ipa/ipa_interface.h>\n #include <libcamera/ipa/ipa_module_info.h>\n #include <libcamera/ipa/raspberrypi.h>\n@@ -65,12 +66,14 @@ public:\n \tIPARPi()\n \t\t: lastMode_({}), controller_(), controllerInit_(false),\n \t\t frame_count_(0), check_count_(0), hide_count_(0),\n-\t\t mistrust_count_(0), lsTableHandle_(0), lsTable_(nullptr)\n+\t\t mistrust_count_(0), lsTableHandle_(FileDescriptor()), lsTable_(nullptr)\n \t{\n \t}\n \n \t~IPARPi()\n \t{\n+\t\tif (lsTable_)\n+\t\t\tmunmap(lsTable_, MAX_LS_GRID_SIZE);\n \t}\n \n \tint init(const IPASettings &settings) override;\n@@ -137,7 +140,7 @@ private:\n \t/* How many frames we should avoid running control algos on. */\n \tunsigned int mistrust_count_;\n \t/* LS table allocation passed in from the pipeline handler. */\n-\tuint32_t lsTableHandle_;\n+\tFileDescriptor lsTableHandle_;\n \tvoid *lsTable_;\n };\n \n@@ -357,8 +360,22 @@ void IPARPi::processEvent(const IPAOperationData &event)\n \t}\n \n \tcase RPI_IPA_EVENT_LS_TABLE_ALLOCATION: {\n-\t\tlsTable_ = reinterpret_cast<void *>(event.data[0]);\n-\t\tlsTableHandle_ = event.data[1];\n+\t\t/* Remove any previous table, if there was one. */\n+\t\tif (lsTable_)\n+\t\t\tmunmap(lsTable_, MAX_LS_GRID_SIZE);\n+\n+\t\t/* Map the LS table buffer into user space. */\n+\t\tlsTableHandle_ = FileDescriptor(event.data[0]);\n+\t\tif (lsTableHandle_.isValid()) {\n+\t\t\tlsTable_ = mmap(nullptr, MAX_LS_GRID_SIZE, PROT_READ | PROT_WRITE,\n+\t\t\t\t\tMAP_SHARED, lsTableHandle_.fd(), 0);\n+\n+\t\t\tif (lsTable_ == MAP_FAILED) {\n+\t\t\t\tLOG(IPARPI, Error) << \"dmaHeap mmap failure for LS table.\";\n+\t\t\t\tlsTable_ = nullptr;\n+\t\t\t}\n+\t\t}\n+\n \t\tbreak;\n \t}\n \n@@ -1026,7 +1043,7 @@ void IPARPi::applyLS(const struct AlscStatus *lsStatus, ControlList &ctrls)\n \t\t.grid_width = w,\n \t\t.grid_stride = w,\n \t\t.grid_height = h,\n-\t\t.mem_handle_table = lsTableHandle_,\n+\t\t.dmabuf = lsTableHandle_.fd(),\n \t\t.ref_transform = 0,\n \t\t.corner_sampled = 1,\n \t\t.gain_format = GAIN_FORMAT_U4P10\ndiff --git a/src/libcamera/pipeline/raspberrypi/dma-heaps.h b/src/libcamera/pipeline/raspberrypi/dma-heaps.h\nnew file mode 100644\nindex 00000000..9f9137d8\n--- /dev/null\n+++ b/src/libcamera/pipeline/raspberrypi/dma-heaps.h\n@@ -0,0 +1,102 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Raspberry Pi (Trading) Limited\n+ *\n+ * dma-heap.h - Helper class for dma-heap allocations.\n+ */\n+#ifndef __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__\n+#define __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__\n+\n+#include <mutex>\n+\n+#include <fcntl.h>\n+#include <linux/dma-buf.h>\n+#include <linux/dma-heap.h>\n+#include <sys/ioctl.h>\n+#include <sys/mman.h>\n+#include <unistd.h>\n+\n+#include <libcamera/file_descriptor.h>\n+#include <libcamera/logging.h>\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(RPI)\n+\n+namespace RPi {\n+\n+/*\n+ * /dev/dma-heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma to\n+ * only have to worry about importing.\n+ *\n+ * Annoyingly, should the cma heap size be specified on the kernel command line\n+ * instead of DT, the heap gets named \"reserved\" instead.\n+ */\n+#define DMA_HEAP_CMA_NAME \"/dev/dma_heap/linux,cma\"\n+#define DMA_HEAP_CMA_ALT_NAME \"/dev/dma_heap/reserved\"\n+\n+class DmaHeap\n+{\n+public:\n+\tDmaHeap()\n+\t{\n+\t\tdmaHeapHandle_ = ::open(DMA_HEAP_CMA_NAME, O_RDWR, 0);\n+\t\tif (dmaHeapHandle_ == -1) {\n+\t\t\tdmaHeapHandle_ = ::open(DMA_HEAP_CMA_ALT_NAME, O_RDWR, 0);\n+\t\t\tif (dmaHeapHandle_ == -1) {\n+\t\t\t\tLOG(RPI, Error) << \"Could not open dmaHeap device\";\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\t~DmaHeap()\n+\t{\n+\t\tif (dmaHeapHandle_)\n+\t\t\t::close(dmaHeapHandle_);\n+\t}\n+\n+\tFileDescriptor alloc(const char *name, unsigned int size)\n+\t{\n+\t\tunsigned int pageSize = getpagesize();\n+\t\tint ret;\n+\n+\t\tif (!name)\n+\t\t\treturn FileDescriptor();\n+\n+\t\t/* Ask for page aligned allocation. */\n+\t\tsize = (size + pageSize - 1) & ~(pageSize - 1);\n+\n+\t\tstruct dma_heap_allocation_data alloc;\n+\n+\t\tmemset(&alloc, 0, sizeof(alloc));\n+\t\talloc.len = size;\n+\t\talloc.fd_flags = O_CLOEXEC | O_RDWR;\n+\n+\t\tret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc);\n+\n+\t\tif (ret < 0) {\n+\t\t\tLOG(RPI, Error) << \"dmaHeap allocation failure for \"\n+\t\t\t\t\t<< name;\n+\t\t\treturn FileDescriptor();\n+\t\t}\n+\n+\t\tret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);\n+\t\tif (ret < 0) {\n+\t\t\tLOG(RPI, Error) << \"dmaHeap naming failure for \"\n+\t\t\t\t\t<< name;\n+\t\t\t::close(alloc.fd);\n+\t\t\treturn FileDescriptor();\n+\t\t}\n+\n+\t\treturn FileDescriptor(std::move(alloc.fd));\n+\t}\n+\n+private:\n+\tint dmaHeapHandle_;\n+};\n+\n+} /* namespace RPi */\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__ */\ndiff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 7450062f..4486e04e 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -13,6 +13,7 @@\n \n #include <libcamera/camera.h>\n #include <libcamera/control_ids.h>\n+#include <libcamera/file_descriptor.h>\n #include <libcamera/formats.h>\n #include <libcamera/ipa/raspberrypi.h>\n #include <libcamera/logging.h>\n@@ -30,8 +31,8 @@\n #include \"libcamera/internal/v4l2_controls.h\"\n #include \"libcamera/internal/v4l2_videodevice.h\"\n \n+#include \"dma-heaps.h\"\n #include \"staggered_ctrl.h\"\n-#include \"vcsm.h\"\n \n namespace libcamera {\n \n@@ -285,23 +286,13 @@ class RPiCameraData : public CameraData\n {\n public:\n \tRPiCameraData(PipelineHandler *pipe)\n-\t\t: CameraData(pipe), sensor_(nullptr), lsTable_(nullptr),\n+\t\t: CameraData(pipe), sensor_(nullptr), lsTable_(FileDescriptor()),\n \t\t state_(State::Stopped), dropFrame_(false), ispOutputCount_(0)\n \t{\n \t}\n \n \t~RPiCameraData()\n \t{\n-\t\t/*\n-\t\t * Free the LS table if we have allocated one. Another\n-\t\t * allocation will occur in applyLS() with the appropriate\n-\t\t * size.\n-\t\t */\n-\t\tif (lsTable_) {\n-\t\t\tvcsm_.free(lsTable_);\n-\t\t\tlsTable_ = nullptr;\n-\t\t}\n-\n \t\t/* Stop the IPA proxy thread. */\n \t\tif (ipa_)\n \t\t\tipa_->stop();\n@@ -330,9 +321,9 @@ public:\n \t/* Buffers passed to the IPA. */\n \tstd::vector<IPABuffer> ipaBuffers_;\n \n-\t/* VCSM allocation helper. */\n-\t::RPi::Vcsm vcsm_;\n-\tvoid *lsTable_;\n+\t/* DMAHEAP allocation helper. */\n+\tRPi::DmaHeap dmaHeap_;\n+\tFileDescriptor lsTable_;\n \n \tRPi::StaggeredCtrl staggeredCtrl_;\n \tuint32_t expectedSequence_;\n@@ -1006,27 +997,16 @@ int PipelineHandlerRPi::configureIPA(Camera *camera)\n \tentityControls.emplace(0, data->unicam_[Unicam::Image].dev()->controls());\n \tentityControls.emplace(1, data->isp_[Isp::Input].dev()->controls());\n \n-\t/* Allocate the lens shading table via vcsm and pass to the IPA. */\n-\tif (!data->lsTable_) {\n-\t\tdata->lsTable_ = data->vcsm_.alloc(\"ls_grid\", MAX_LS_GRID_SIZE);\n-\t\tuintptr_t ptr = reinterpret_cast<uintptr_t>(data->lsTable_);\n-\n-\t\tif (!data->lsTable_)\n+\t/* Allocate the lens shading table via dmaHeap and pass to the IPA. */\n+\tif (!data->lsTable_.isValid()) {\n+\t\tdata->lsTable_ = data->dmaHeap_.alloc(\"ls_grid\", MAX_LS_GRID_SIZE);\n+\t\tif (!data->lsTable_.isValid())\n \t\t\treturn -ENOMEM;\n \n-\t\t/*\n-\t\t * The vcsm allocation will always be in the memory region\n-\t\t * < 32-bits to allow Videocore to access the memory.\n-\t\t *\n-\t\t * \\todo Sending a pointer to the IPA is a workaround for\n-\t\t * vc_sm_cma not yet supporting dmabuf. This will not work with\n-\t\t * IPA module isolation and should be reworked when vc_sma_cma\n-\t\t * will permit.\n-\t\t */\n+\t\t/* Allow the IPA to mmap the LS table via the file descriptor. */\n \t\tIPAOperationData op;\n \t\top.operation = RPI_IPA_EVENT_LS_TABLE_ALLOCATION;\n-\t\top.data = { static_cast<uint32_t>(ptr & 0xffffffff),\n-\t\t\t data->vcsm_.getVCHandle(data->lsTable_) };\n+\t\top.data = { static_cast<unsigned int>(data->lsTable_.fd()) };\n \t\tdata->ipa_->processEvent(op);\n \t}\n \ndiff --git a/src/libcamera/pipeline/raspberrypi/vcsm.h b/src/libcamera/pipeline/raspberrypi/vcsm.h\ndeleted file mode 100644\nindex bebe86a7..00000000\n--- a/src/libcamera/pipeline/raspberrypi/vcsm.h\n+++ /dev/null\n@@ -1,149 +0,0 @@\n-/* SPDX-License-Identifier: LGPL-2.1-or-later */\n-/*\n- * Copyright (C) 2019, Raspberry Pi (Trading) Limited\n- *\n- * vcsm.h - Helper class for vcsm allocations.\n- */\n-#ifndef __LIBCAMERA_PIPELINE_RASPBERRYPI_VCSM_H__\n-#define __LIBCAMERA_PIPELINE_RASPBERRYPI_VCSM_H__\n-\n-#include <iostream>\n-#include <mutex>\n-\n-#include <fcntl.h>\n-#include <linux/vc_sm_cma_ioctl.h>\n-#include <sys/ioctl.h>\n-#include <sys/mman.h>\n-#include <unistd.h>\n-\n-namespace RPi {\n-\n-#define VCSM_CMA_DEVICE_NAME \"/dev/vcsm-cma\"\n-\n-class Vcsm\n-{\n-public:\n-\tVcsm()\n-\t{\n-\t\tvcsmHandle_ = ::open(VCSM_CMA_DEVICE_NAME, O_RDWR, 0);\n-\t\tif (vcsmHandle_ == -1) {\n-\t\t\tstd::cerr << \"Could not open vcsm device: \"\n-\t\t\t\t << VCSM_CMA_DEVICE_NAME;\n-\t\t}\n-\t}\n-\n-\t~Vcsm()\n-\t{\n-\t\t/* Free all existing allocations. */\n-\t\tauto it = allocMap_.begin();\n-\t\twhile (it != allocMap_.end())\n-\t\t\tit = remove(it->first);\n-\n-\t\tif (vcsmHandle_)\n-\t\t\t::close(vcsmHandle_);\n-\t}\n-\n-\tvoid *alloc(const char *name, unsigned int size,\n-\t\t vc_sm_cma_cache_e cache = VC_SM_CMA_CACHE_NONE)\n-\t{\n-\t\tunsigned int pageSize = getpagesize();\n-\t\tvoid *user_ptr;\n-\t\tint ret;\n-\n-\t\tif (!name)\n-\t\t\treturn nullptr;\n-\n-\t\t/* Ask for page aligned allocation. */\n-\t\tsize = (size + pageSize - 1) & ~(pageSize - 1);\n-\n-\t\tstruct vc_sm_cma_ioctl_alloc alloc;\n-\t\tmemset(&alloc, 0, sizeof(alloc));\n-\t\talloc.size = size;\n-\t\talloc.num = 1;\n-\t\talloc.cached = cache;\n-\t\talloc.handle = 0;\n-\t\tmemcpy(alloc.name, name, 32);\n-\n-\t\tret = ::ioctl(vcsmHandle_, VC_SM_CMA_IOCTL_MEM_ALLOC, &alloc);\n-\n-\t\tif (ret < 0 || alloc.handle < 0) {\n-\t\t\tstd::cerr << \"vcsm allocation failure for \"\n-\t\t\t\t << name << std::endl;\n-\t\t\treturn nullptr;\n-\t\t}\n-\n-\t\t/* Map the buffer into user space. */\n-\t\tuser_ptr = ::mmap(0, alloc.size, PROT_READ | PROT_WRITE,\n-\t\t\t\t MAP_SHARED, alloc.handle, 0);\n-\n-\t\tif (user_ptr == MAP_FAILED) {\n-\t\t\tstd::cerr << \"vcsm mmap failure for \" << name << std::endl;\n-\t\t\t::close(alloc.handle);\n-\t\t\treturn nullptr;\n-\t\t}\n-\n-\t\tstd::lock_guard<std::mutex> lock(lock_);\n-\t\tallocMap_.emplace(user_ptr, AllocInfo(alloc.handle,\n-\t\t\t\t\t\t alloc.size, alloc.vc_handle));\n-\n-\t\treturn user_ptr;\n-\t}\n-\n-\tvoid free(void *user_ptr)\n-\t{\n-\t\tstd::lock_guard<std::mutex> lock(lock_);\n-\t\tremove(user_ptr);\n-\t}\n-\n-\tunsigned int getVCHandle(void *user_ptr)\n-\t{\n-\t\tstd::lock_guard<std::mutex> lock(lock_);\n-\t\tauto it = allocMap_.find(user_ptr);\n-\t\tif (it != allocMap_.end())\n-\t\t\treturn it->second.vcHandle;\n-\n-\t\treturn 0;\n-\t}\n-\n-private:\n-\tstruct AllocInfo {\n-\t\tAllocInfo(int handle_, int size_, int vcHandle_)\n-\t\t\t: handle(handle_), size(size_), vcHandle(vcHandle_)\n-\t\t{\n-\t\t}\n-\n-\t\tint handle;\n-\t\tint size;\n-\t\tuint32_t vcHandle;\n-\t};\n-\n-\t/* Map of all allocations that have been requested. */\n-\tusing AllocMap = std::map<void *, AllocInfo>;\n-\n-\tAllocMap::iterator remove(void *user_ptr)\n-\t{\n-\t\tauto it = allocMap_.find(user_ptr);\n-\t\tif (it != allocMap_.end()) {\n-\t\t\tint handle = it->second.handle;\n-\t\t\tint size = it->second.size;\n-\t\t\t::munmap(user_ptr, size);\n-\t\t\t::close(handle);\n-\t\t\t/*\n-\t\t\t * Remove the allocation from the map. This returns\n-\t\t\t * an iterator to the next element.\n-\t\t\t */\n-\t\t\tit = allocMap_.erase(it);\n-\t\t}\n-\n-\t\t/* Returns an iterator to the next element. */\n-\t\treturn it;\n-\t}\n-\n-\tAllocMap allocMap_;\n-\tint vcsmHandle_;\n-\tstd::mutex lock_;\n-};\n-\n-} /* namespace RPi */\n-\n-#endif /* __LIBCAMERA_PIPELINE_RASPBERRYPI_VCSM_H__ */\n", "prefixes": [ "libcamera-devel", "2/2" ] }