Show a patch.

GET /api/patches/8837/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 8837,
    "url": "https://patchwork.libcamera.org/api/patches/8837/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/8837/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20200717001518.20285-3-laurent.pinchart@ideasonboard.com>",
    "date": "2020-07-17T00:15:18",
    "name": "[libcamera-devel,v3,2/2] libcamera: pipeline: ipa: raspberrypi: Use dma heap allocs for LS tables",
    "commit_ref": "3e7aa49344ad5233f644703559898572b57f9613",
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "cfeced62099333c64776f4046ce84f1d66f6d18f",
    "submitter": {
        "id": 2,
        "url": "https://patchwork.libcamera.org/api/people/2/?format=api",
        "name": "Laurent Pinchart",
        "email": "laurent.pinchart@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/8837/mbox/",
    "series": [
        {
            "id": 1113,
            "url": "https://patchwork.libcamera.org/api/series/1113/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1113",
            "date": "2020-07-17T00:15:16",
            "name": "Replace Raspberry Pi VCSM CMA with DMA heaps",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/1113/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/8837/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/8837/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 ED226C2E7A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 17 Jul 2020 00:15:36 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B52C760561;\n\tFri, 17 Jul 2020 02:15:36 +0200 (CEST)",
            "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 F3E3C60498\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 17 Jul 2020 02:15:33 +0200 (CEST)",
            "from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 60043A5E;\n\tFri, 17 Jul 2020 02:15:33 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"fPl8CiwI\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1594944933;\n\tbh=jqj7FKaWRPODsP9WpewpmbeZLEQTFRcnerLekhFCooo=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=fPl8CiwIwyJWxGs5lszpAGEmV/dkYekM3d/5DM7JhP9u56qeZhokzoyeAgmC2L2We\n\tKIcwFxFbmhrxC/uwe8Y6s9PP/zsmlm8jadQoHXzgRvajOxzo8VUxDWWQ78yKqz4VTj\n\t7zwWoEfJaGWTpnHekJdxNnUXaNDQnWGL0fsy+mNU=",
        "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "To": "Naushir Patuck <naush@raspberrypi.com>",
        "Date": "Fri, 17 Jul 2020 03:15:18 +0300",
        "Message-Id": "<20200717001518.20285-3-laurent.pinchart@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.27.0",
        "In-Reply-To": "<20200717001518.20285-1-laurent.pinchart@ideasonboard.com>",
        "References": "<20200717001518.20285-1-laurent.pinchart@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Subject": "[libcamera-devel] [PATCH v3 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>",
        "Cc": "libcamera-devel@lists.libcamera.org",
        "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": "From: Naushir Patuck <naush@raspberrypi.com>\n\nRemove 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\nThis commit must be built alongside the accompanying BCM2835 ISP kernel\ndriver changes at https://github.com/raspberrypi/linux/pull/3715.\nOtherwise a mismatch will cause undefined behavior.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/linux/bcm2835-isp.h                   |   4 +-\n include/linux/vc_sm_cma_ioctl.h               | 135 ----------------\n src/ipa/raspberrypi/raspberrypi.cpp           |  28 +++-\n .../pipeline/raspberrypi/dma_heaps.cpp        |  84 ++++++++++\n .../pipeline/raspberrypi/dma_heaps.h          |  31 ++++\n .../pipeline/raspberrypi/meson.build          |   1 +\n .../pipeline/raspberrypi/raspberrypi.cpp      |  49 ++----\n src/libcamera/pipeline/raspberrypi/vcsm.h     | 149 ------------------\n 8 files changed, 154 insertions(+), 327 deletions(-)\n delete mode 100644 include/linux/vc_sm_cma_ioctl.h\n create mode 100644 src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\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 e7afc367fd76..45abb681517e 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 21b8758ea03f..000000000000\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 2fcbc782f8b8..7bd0488089e8 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), 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@@ -139,7 +142,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@@ -280,8 +283,23 @@ void IPARPi::configure(const CameraSensorInfo &sensorInfo,\n \n \t/* Store the lens shading table pointer and handle if available. */\n \tif (ipaConfig.operation & RPI_IPA_CONFIG_LS_TABLE) {\n-\t\tlsTable_ = reinterpret_cast<void *>(ipaConfig.data[0]);\n-\t\tlsTableHandle_ = ipaConfig.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+\t\t\tlsTable_ = nullptr;\n+\t\t}\n+\n+\t\t/* Map the LS table buffer into user space. */\n+\t\tlsTableHandle_ = FileDescriptor(ipaConfig.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 \t}\n }\n \n@@ -1030,7 +1048,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.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\nnew file mode 100644\nindex 000000000000..6769c04640f1\n--- /dev/null\n+++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n@@ -0,0 +1,84 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Raspberry Pi (Trading) Limited\n+ *\n+ * dma_heaps.h - Helper class for dma-heap allocations.\n+ */\n+\n+#include \"dma_heaps.h\"\n+\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 \"libcamera/internal/log.h\"\n+\n+/*\n+ * /dev/dma-heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma\n+ * to 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+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(RPI)\n+\n+namespace RPi {\n+\n+DmaHeap::DmaHeap()\n+{\n+\tdmaHeapHandle_ = ::open(DMA_HEAP_CMA_NAME, O_RDWR, 0);\n+\tif (dmaHeapHandle_ == -1) {\n+\t\tdmaHeapHandle_ = ::open(DMA_HEAP_CMA_ALT_NAME, O_RDWR, 0);\n+\t\tif (dmaHeapHandle_ == -1) {\n+\t\t\tLOG(RPI, Error) << \"Could not open dmaHeap device\";\n+\t\t}\n+\t}\n+}\n+\n+DmaHeap::~DmaHeap()\n+{\n+\tif (dmaHeapHandle_)\n+\t\t::close(dmaHeapHandle_);\n+}\n+\n+FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)\n+{\n+\tint ret;\n+\n+\tif (!name)\n+\t\treturn FileDescriptor();\n+\n+\tstruct dma_heap_allocation_data alloc = {};\n+\n+\talloc.len = size;\n+\talloc.fd_flags = O_CLOEXEC | O_RDWR;\n+\n+\tret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc);\n+\n+\tif (ret < 0) {\n+\t\tLOG(RPI, Error) << \"dmaHeap allocation failure for \"\n+\t\t\t\t<< name;\n+\t\treturn FileDescriptor();\n+\t}\n+\n+\tret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);\n+\tif (ret < 0) {\n+\t\tLOG(RPI, Error) << \"dmaHeap naming failure for \"\n+\t\t\t\t<< name;\n+\t\t::close(alloc.fd);\n+\t\treturn FileDescriptor();\n+\t}\n+\n+\treturn FileDescriptor(std::move(alloc.fd));\n+}\n+\n+} /* namespace RPi */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\nnew file mode 100644\nindex 000000000000..ae6be1135f17\n--- /dev/null\n+++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h\n@@ -0,0 +1,31 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Raspberry Pi (Trading) Limited\n+ *\n+ * dma_heaps.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 <libcamera/file_descriptor.h>\n+\n+namespace libcamera {\n+\n+namespace RPi {\n+\n+class DmaHeap\n+{\n+public:\n+\tDmaHeap();\n+\t~DmaHeap();\n+\tFileDescriptor alloc(const char *name, std::size_t size);\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/meson.build b/src/libcamera/pipeline/raspberrypi/meson.build\nindex dcfe07c5b720..ae0aed3b5234 100644\n--- a/src/libcamera/pipeline/raspberrypi/meson.build\n+++ b/src/libcamera/pipeline/raspberrypi/meson.build\n@@ -1,6 +1,7 @@\n # SPDX-License-Identifier: CC0-1.0\n \n libcamera_sources += files([\n+    'dma_heaps.cpp',\n     'raspberrypi.cpp',\n     'staggered_ctrl.cpp',\n ])\ndiff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 718749af5a6e..bf1c77144f85 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@@ -31,8 +32,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@@ -286,24 +287,11 @@ class RPiCameraData : public CameraData\n {\n public:\n \tRPiCameraData(PipelineHandler *pipe)\n-\t\t: CameraData(pipe), sensor_(nullptr), lsTable_(nullptr),\n-\t\t  state_(State::Stopped), dropFrame_(false), ispOutputCount_(0)\n+\t\t: CameraData(pipe), sensor_(nullptr), state_(State::Stopped),\n+\t\t  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-\t}\n-\n \tvoid frameStarted(uint32_t sequence);\n \n \tint loadIPA();\n@@ -329,9 +317,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@@ -1142,26 +1130,15 @@ int RPiCameraData::configureIPA()\n \tentityControls.emplace(0, unicam_[Unicam::Image].dev()->controls());\n \tentityControls.emplace(1, isp_[Isp::Input].dev()->controls());\n \n-\t/* Allocate the lens shading table via vcsm and pass to the IPA. */\n-\tif (!lsTable_) {\n-\t\tlsTable_ = vcsm_.alloc(\"ls_grid\", MAX_LS_GRID_SIZE);\n-\t\tuintptr_t ptr = reinterpret_cast<uintptr_t>(lsTable_);\n-\n-\t\tif (!lsTable_)\n+\t/* Allocate the lens shading table via dmaHeap and pass to the IPA. */\n+\tif (!lsTable_.isValid()) {\n+\t\tlsTable_ = dmaHeap_.alloc(\"ls_grid\", MAX_LS_GRID_SIZE);\n+\t\tif (!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\tipaConfig.operation = RPI_IPA_CONFIG_LS_TABLE;\n-\t\tipaConfig.data = { static_cast<uint32_t>(ptr & 0xffffffff),\n-\t\t\t\t   vcsm_.getVCHandle(lsTable_) };\n+\t\tipaConfig.data = { static_cast<unsigned int>(lsTable_.fd()) };\n \t}\n \n \tCameraSensorInfo sensorInfo = {};\ndiff --git a/src/libcamera/pipeline/raspberrypi/vcsm.h b/src/libcamera/pipeline/raspberrypi/vcsm.h\ndeleted file mode 100644\nindex bebe86a76450..000000000000\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",
        "v3",
        "2/2"
    ]
}