Skip to contentSkip to conversation

accel/ivpu: Add support for userptr buffer objects

PATCH Superseded Karol Wachowski 2 participants 3 messages Oct 28, 2025 – Oct 29, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Karol Wachowski

author maintainer
#0 lore raw

PATCH 1/1 accel/ivpu: Add support for userptr buffer objects

From: Jacek Lawrynowicz <[email protected]>

Add support for creating buffer objects from user pointers in the Intel NPU driver.

Introduce a new ioctl drm_ivpu_bo_create_from_userptr that allows users to create a GEM buffer object from a user pointer to a memory region. The user pointer must be page-aligned and the memory region must remain valid for the lifetime of the buffer object.

---
 drivers/accel/ivpu/Makefile           |   1 +
 drivers/accel/ivpu/ivpu_drv.c         |   3 +
 drivers/accel/ivpu/ivpu_gem.c         |   2 +-
 drivers/accel/ivpu/ivpu_gem.h         |   7 +
 drivers/accel/ivpu/ivpu_gem_userptr.c | 207 ++++++++++++++++++++++++++
 drivers/accel/ivpu/ivpu_mmu_context.c |   4 +-
 drivers/accel/ivpu/ivpu_mmu_context.h |   2 +-
 include/uapi/drm/ivpu_accel.h         |  52 +++++++
 8 files changed, 275 insertions(+), 3 deletions(-)
 create mode 100644 drivers/accel/ivpu/ivpu_gem_userptr.c
drivers/accel/ivpu/Makefile +1 -0
index 1029e0bab061..dbf76b8a5b4c 100644
@@ -6,6 +6,7 @@ intel_vpu-y := \
 	ivpu_fw.o \
 	ivpu_fw_log.o \
 	ivpu_gem.o \
+	ivpu_gem_userptr.o \
 	ivpu_hw.o \
 	ivpu_hw_btrs.o \
 	ivpu_hw_ip.o \
drivers/accel/ivpu/ivpu_drv.c +3 -0
index c6fe7a408912..ca68730dee88 100644
@@ -134,6 +134,8 @@ bool ivpu_is_capable(struct ivpu_device *vdev, u32 capability)
 		return true;
 	case DRM_IVPU_CAP_DMA_MEMORY_RANGE:
 		return true;
+	case DRM_IVPU_CAP_BO_CREATE_FROM_USERPTR:
+		return true;
 	case DRM_IVPU_CAP_MANAGE_CMDQ:
 		return vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW;
 	default:
@@ -313,6 +315,7 @@ static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(IVPU_CMDQ_CREATE, ivpu_cmdq_create_ioctl, 0),
 	DRM_IOCTL_DEF_DRV(IVPU_CMDQ_DESTROY, ivpu_cmdq_destroy_ioctl, 0),
 	DRM_IOCTL_DEF_DRV(IVPU_CMDQ_SUBMIT, ivpu_cmdq_submit_ioctl, 0),
+	DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE_FROM_USERPTR, ivpu_bo_create_from_userptr_ioctl, 0),
 };
 
 static int ivpu_wait_for_ready(struct ivpu_device *vdev)
drivers/accel/ivpu/ivpu_gem.c +1 -1
index 516e2b05c089..b00af1ce11aa 100644
@@ -96,7 +96,7 @@ int __must_check ivpu_bo_bind(struct ivpu_bo *bo)
 	if (!bo->mmu_mapped) {
 		drm_WARN_ON(&vdev->drm, !bo->ctx);
 		ret = ivpu_mmu_context_map_sgt(vdev, bo->ctx, bo->vpu_addr, sgt,
-					       ivpu_bo_is_snooped(bo));
+					       ivpu_bo_is_snooped(bo), ivpu_bo_is_read_only(bo));
 		if (ret) {
 			ivpu_err(vdev, "Failed to map BO in MMU: %d\n", ret);
 			goto unlock;
drivers/accel/ivpu/ivpu_gem.h +7 -0
index 54452eb8a41f..2dcd7eba9cb7 100644
@@ -38,6 +38,8 @@ void ivpu_bo_free(struct ivpu_bo *bo);
 int ivpu_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
 int ivpu_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
 int ivpu_bo_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
+int ivpu_bo_create_from_userptr_ioctl(struct drm_device *dev, void *data,
+				      struct drm_file *file);
 
 void ivpu_bo_list(struct drm_device *dev, struct drm_printer *p);
 void ivpu_bo_list_print(struct drm_device *dev);
@@ -75,6 +77,11 @@ static inline bool ivpu_bo_is_snooped(struct ivpu_bo *bo)
 	return ivpu_bo_cache_mode(bo) == DRM_IVPU_BO_CACHED;
 }
 
+static inline bool ivpu_bo_is_read_only(struct ivpu_bo *bo)
+{
+	return bo->flags & DRM_IVPU_BO_READ_ONLY;
+}
+
 static inline void *ivpu_to_cpu_addr(struct ivpu_bo *bo, u32 vpu_addr)
 {
 	if (vpu_addr < bo->vpu_addr)
drivers/accel/ivpu/ivpu_gem_userptr.c new file +207 -0

207 lines not on this page · raw message

drivers/accel/ivpu/ivpu_mmu_context.c +3 -1
index 4ffc783426be..d128e8961688 100644
@@ -430,7 +430,7 @@ static void ivpu_mmu_context_unmap_pages(struct ivpu_mmu_context *ctx, u64 vpu_a
 
 int
 ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
-			 u64 vpu_addr, struct sg_table *sgt,  bool llc_coherent)
+			 u64 vpu_addr, struct sg_table *sgt, bool llc_coherent, bool read_only)
 {
 	size_t start_vpu_addr = vpu_addr;
 	struct scatterlist *sg;
@@ -450,6 +450,8 @@ ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
 	prot = IVPU_MMU_ENTRY_MAPPED;
 	if (llc_coherent)
 		prot |= IVPU_MMU_ENTRY_FLAG_LLC_COHERENT;
+	if (read_only)
+		prot |= IVPU_MMU_ENTRY_FLAG_RO;
 
 	mutex_lock(&ctx->lock);
 
drivers/accel/ivpu/ivpu_mmu_context.h +1 -1
index f255310968cf..663a11a9db11 100644
@@ -42,7 +42,7 @@ int ivpu_mmu_context_insert_node(struct ivpu_mmu_context *ctx, const struct ivpu
 void ivpu_mmu_context_remove_node(struct ivpu_mmu_context *ctx, struct drm_mm_node *node);
 
 int ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
-			     u64 vpu_addr, struct sg_table *sgt, bool llc_coherent);
+			     u64 vpu_addr, struct sg_table *sgt, bool llc_coherent, bool read_only);
 void ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
 				u64 vpu_addr, struct sg_table *sgt);
 int ivpu_mmu_context_set_pages_ro(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
include/uapi/drm/ivpu_accel.h +52 -0

88 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
Quoted 4 lines · Karol Wachowski

From: Jacek Lawrynowicz <[email protected]>

Add support for creating buffer objects from user pointers in the Intel NPU driver.

This feels redundant. $SUBJECT already mentions adding the support, and in the Intel NPU driver is a given.

Quoted 4 lines

Introduce a new ioctl drm_ivpu_bo_create_from_userptr that allows users to create a GEM buffer object from a user pointer to a memory region. The user pointer must be page-aligned and the memory region must remain valid for the lifetime of the buffer object.

This seems good, but perhaps incomplete. Why do this? What benifit would this new IOCTL give the user? Increases performance when the user has a specific usecase?

Quoted 18 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +12 -0
new file mode 100644
index 000000000000..c5b64e0f2b13
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2025 Intel Corporation
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/err.h>
+#include <linux/highmem.h>
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>

drivers/accel/ivpu/ivpu_gem_userptr.c line 22

Quoted 10 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +10 -0
+#include <linux/capability.h>
+
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+#include <drm/drm_gem.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_gem.h"
+
+#define IVPU_BO_USERPTR_MAX_SIZE (4ULL * SZ_1G)

Is this not the same as SZ_4G?

drivers/accel/ivpu/ivpu_gem_userptr.c line 32

Quoted 10 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +10 -0
+/**
+ * struct drm_ivpu_bo_create_from_userptr - Create dma-buf from user pointer
+ *
+ * Create a GEM buffer object from a user pointer to a memory region.
+ */
+struct drm_ivpu_bo_create_from_userptr {
+	/** @user_ptr: User pointer to memory region (must be page aligned) */
+	__u64 user_ptr;
+
+	/** @size: Size of the memory region in bytes (must be page aligned) */

Mention max size?

Quoted 31 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +28 -0
+	__u64 size;
+
+	/**
+	 * @flags:
+	 *
+	 * Supported flags:
+	 *
+	 * %DRM_IVPU_BO_HIGH_MEM:
+	 *
+	 * Allocate VPU address from >4GB range.
+	 *
+	 * %DRM_IVPU_BO_DMA_MEM:
+	 *
+	 * Allocate from DMA memory range accessible by hardware DMA.
+	 *
+	 * %DRM_IVPU_BO_READ_ONLY:
+	 *
+	 * Allocate as a read-only buffer object.
+	 */
+	__u32 flags;
+
+	/** @handle: Returned GEM object handle */
+	__u32 handle;
+
+	/** @vpu_addr: Returned VPU virtual address */
+	__u64 vpu_addr;
+};
+
  /**
   * struct drm_ivpu_bo_info - Query buffer object info
   */
author maintainer
#2 lore raw
Quoted 8 lines · Jeff Hugo
Quoted 4 lines · Karol Wachowski

From: Jacek Lawrynowicz <[email protected]>

Add support for creating buffer objects from user pointers in the Intel NPU driver.

This feels redundant. $SUBJECT already mentions adding the support, and in the Intel NPU driver is a given.

Removed.

Quoted 10 lines
Quoted 6 lines

Introduce a new ioctl drm_ivpu_bo_create_from_userptr that allows users to create a GEM buffer object from a user pointer to a memory region.
The user pointer must be page-aligned and the memory region must remain valid for the lifetime of the buffer object.

This seems good, but perhaps incomplete. Why do this? What benifit would this new IOCTL give the user? Increases performance when the user has a specific usecase?

I have added paragraph describing that this allows to limit memory used and simplifies NPU usage for user applications allowing to mmap files directly into NPU's buffer objects.

Quoted 24 lines
Quoted 24 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +16 -0
new file mode 100644
index 000000000000..c5b64e0f2b13
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2025 Intel Corporation
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/err.h>
+#include <linux/highmem.h>
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/capability.h>
+
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
Quoted 8 lines

drivers/accel/ivpu/ivpu_gem_userptr.c line 22

Quoted 6 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +6 -0
+#include <drm/drm_gem.h>
+
+#include "ivpu_drv.h"
+#include "ivpu_gem.h"
+
+#define IVPU_BO_USERPTR_MAX_SIZE (4ULL * SZ_1G)

Is this not the same as SZ_4G?

After consideration I have decided to remove this limitation. So I have removed this define and check completely.
Maximum memory usage for both userptr and generic buffer object is limited by driver's defined ranges anyway.

Quoted 16 lines
Quoted 6 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +2 -0
+/**
+ * struct drm_ivpu_bo_create_from_userptr - Create dma-buf from user

pointer
+ *
+ * Create a GEM buffer object from a user pointer to a memory region.

Quoted 8 lines

+ */
+struct drm_ivpu_bo_create_from_userptr {
+    /** @user_ptr: User pointer to memory region (must be page aligned) */
+    __u64 user_ptr;
+
+    /** @size: Size of the memory region in bytes (must be page aligned) */

Mention max size?

As above, I have removed the limitation.

Quoted 24 lines
Quoted 24 lines

+    __u64 size;

drivers/accel/ivpu/ivpu_gem_userptr.c new file +22 -0
+
+    /**
+     * @flags:
+     *
+     * Supported flags:
+     *
+     * %DRM_IVPU_BO_HIGH_MEM:
+     *
+     * Allocate VPU address from >4GB range.
+     *
+     * %DRM_IVPU_BO_DMA_MEM:
+     *
+     * Allocate from DMA memory range accessible by hardware DMA.
+     *
+     * %DRM_IVPU_BO_READ_ONLY:
+     *
+     * Allocate as a read-only buffer object.
+     */
+    __u32 flags;
+
+    /** @handle: Returned GEM object handle */
+    __u32 handle;
Quoted 9 lines
Quoted 8 lines
drivers/accel/ivpu/ivpu_gem_userptr.c new file +5 -0
+
+    /** @vpu_addr: Returned VPU virtual address */
+    __u64 vpu_addr;
+};
+

  /**
   * struct drm_ivpu_bo_info - Query buffer object info */

Thank you for the review, all above are addressed in v2. -Karol