mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ekansh Gupta <ekansh.gupta@oss.qualcomm.com>
To: Matthew Brost <matthew.brost@intel.com>
Cc: "Oded Gabbay" <ogabbay@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Joerg Roedel" <joro@8bytes.org>, "Will Deacon" <will@kernel.org>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	iommu@lists.linux.dev, linux-media@vger.kernel.org,
	linaro-mm-sig@lists.linaro.org,
	"Srinivas Kandagatla" <srinivas.kandagatla@oss.qualcomm.com>,
	"Dmitry Baryshkov" <dmitry.baryshkov@oss.qualcomm.com>,
	"Bharath Kumar" <quic_bkumar@quicinc.com>,
	"Chenna Kesava Raju" <quic_chennak@quicinc.com>
Subject: Re: [PATCH RFC 12/18] accel/qda: Add PRIME dma-buf import support
Date: Mon, 2 Mar 2026 14:49:02 +0530	[thread overview]
Message-ID: <1f140186-534c-4b4e-b68a-6c9b8d8511e2@oss.qualcomm.com> (raw)
In-Reply-To: <aZ1m0wOA9EBbJr+x@lstrano-desk.jf.intel.com>



On 2/24/2026 2:22 PM, Matthew Brost wrote:
> On Tue, Feb 24, 2026 at 12:39:06AM +0530, Ekansh Gupta wrote:
>> Add PRIME dma-buf import support for QDA GEM buffer objects and integrate
>> it with the existing per-process memory manager and IOMMU device model.
>>
>> The implementation extends qda_gem_obj to represent imported dma-bufs,
>> including dma_buf references, attachment state, scatter-gather tables
>> and an imported DMA address used for DSP-facing book-keeping. The
>> qda_gem_prime_import() path handles reimports of buffers originally
>> exported by QDA as well as imports of external dma-bufs, attaching them
>> to the assigned IOMMU device and mapping them through the memory manager
>> for DSP access. The GEM free path is updated to unmap and detach
>> imported buffers while preserving the existing behaviour for locally
>> allocated memory.
>>
>> The PRIME fd-to-handle path is implemented in qda_prime_fd_to_handle(),
>> which records the calling drm_file in a driver-private import context
>> before invoking the core DRM helpers. The GEM import callback retrieves
>> this context to ensure that an IOMMU device is assigned to the process
>> and that imported buffers follow the same per-process IOMMU selection
>> rules as natively allocated GEM objects.
>>
>> This patch prepares the driver for interoperable buffer sharing between
>> QDA and other dma-buf capable subsystems while keeping IOMMU mapping and
>> lifetime handling consistent with the existing GEM allocation flow.
>>
>> Signed-off-by: Ekansh Gupta <ekansh.gupta@oss.qualcomm.com>
>> ---
>>  drivers/accel/qda/Makefile             |   1 +
>>  drivers/accel/qda/qda_drv.c            |   8 ++
>>  drivers/accel/qda/qda_drv.h            |   4 +
>>  drivers/accel/qda/qda_gem.c            |  60 +++++++---
>>  drivers/accel/qda/qda_gem.h            |  10 ++
>>  drivers/accel/qda/qda_ioctl.c          |   7 ++
>>  drivers/accel/qda/qda_ioctl.h          |  15 +++
>>  drivers/accel/qda/qda_memory_manager.c |  42 ++++++-
>>  drivers/accel/qda/qda_memory_manager.h |  14 +++
>>  drivers/accel/qda/qda_prime.c          | 194 +++++++++++++++++++++++++++++++++
>>  drivers/accel/qda/qda_prime.h          |  43 ++++++++
>>  11 files changed, 377 insertions(+), 21 deletions(-)
>>
>> diff --git a/drivers/accel/qda/Makefile b/drivers/accel/qda/Makefile
>> index 88c324fa382c..8286f5279748 100644
>> --- a/drivers/accel/qda/Makefile
>> +++ b/drivers/accel/qda/Makefile
>> @@ -13,5 +13,6 @@ qda-y := \
>>  	qda_ioctl.o \
>>  	qda_gem.o \
>>  	qda_memory_dma.o \
>> +	qda_prime.o \
>>  
>>  obj-$(CONFIG_DRM_ACCEL_QDA_COMPUTE_BUS) += qda_compute_bus.o
>> diff --git a/drivers/accel/qda/qda_drv.c b/drivers/accel/qda/qda_drv.c
>> index 0dd0e2bb2c0f..4adee00b1f2c 100644
>> --- a/drivers/accel/qda/qda_drv.c
>> +++ b/drivers/accel/qda/qda_drv.c
>> @@ -10,9 +10,11 @@
>>  #include <drm/drm_gem.h>
>>  #include <drm/drm_ioctl.h>
>>  #include <drm/qda_accel.h>
>> +#include <drm/drm_prime.h>
>>  
>>  #include "qda_drv.h"
>>  #include "qda_gem.h"
>> +#include "qda_prime.h"
>>  #include "qda_ioctl.h"
>>  #include "qda_rpmsg.h"
>>  
>> @@ -166,6 +168,8 @@ static struct drm_driver qda_drm_driver = {
>>  	.postclose		= qda_postclose,
>>  	.ioctls = qda_ioctls,
>>  	.num_ioctls = ARRAY_SIZE(qda_ioctls),
>> +	.gem_prime_import = qda_gem_prime_import,
>> +	.prime_fd_to_handle = qda_ioctl_prime_fd_to_handle,
>>  	.name = DRIVER_NAME,
>>  	.desc = "Qualcomm DSP Accelerator Driver",
>>  };
>> @@ -174,6 +178,7 @@ static void cleanup_drm_private(struct qda_dev *qdev)
>>  {
>>  	if (qdev->drm_priv) {
>>  		qda_dbg(qdev, "Cleaning up DRM private data\n");
>> +		mutex_destroy(&qdev->drm_priv->import_lock);
>>  		kfree(qdev->drm_priv);
>>  	}
>>  }
>> @@ -240,6 +245,9 @@ static int init_drm_private(struct qda_dev *qdev)
>>  	if (!qdev->drm_priv)
>>  		return -ENOMEM;
>>  
>> +	mutex_init(&qdev->drm_priv->import_lock);
>> +	qdev->drm_priv->current_import_file_priv = NULL;
>> +
>>  	qda_dbg(qdev, "DRM private data initialized successfully\n");
>>  	return 0;
>>  }
>> diff --git a/drivers/accel/qda/qda_drv.h b/drivers/accel/qda/qda_drv.h
>> index 8a2cd474958b..bb0dd7e284c6 100644
>> --- a/drivers/accel/qda/qda_drv.h
>> +++ b/drivers/accel/qda/qda_drv.h
>> @@ -64,6 +64,10 @@ struct qda_drm_priv {
>>  	struct qda_memory_manager *iommu_mgr;
>>  	/* Back-pointer to qda_dev */
>>  	struct qda_dev *qdev;
>> +	/* Lock protecting import context */
>> +	struct mutex import_lock;
>> +	/* Current file_priv during prime import */
>> +	struct drm_file *current_import_file_priv;
>>  };
>>  
>>  /* struct qda_dev - Main device structure for QDA driver */
>> diff --git a/drivers/accel/qda/qda_gem.c b/drivers/accel/qda/qda_gem.c
>> index bbd54e2502d3..37279e8b46fe 100644
>> --- a/drivers/accel/qda/qda_gem.c
>> +++ b/drivers/accel/qda/qda_gem.c
>> @@ -8,6 +8,7 @@
>>  #include "qda_gem.h"
>>  #include "qda_memory_manager.h"
>>  #include "qda_memory_dma.h"
>> +#include "qda_prime.h"
>>  
>>  static int validate_gem_obj_for_mmap(struct qda_gem_obj *qda_gem_obj)
>>  {
>> @@ -15,23 +16,29 @@ static int validate_gem_obj_for_mmap(struct qda_gem_obj *qda_gem_obj)
>>  		qda_err(NULL, "Invalid GEM object size\n");
>>  		return -EINVAL;
>>  	}
>> -	if (!qda_gem_obj->iommu_dev || !qda_gem_obj->iommu_dev->dev) {
>> -		qda_err(NULL, "Allocated buffer missing IOMMU device\n");
>> -		return -EINVAL;
>> -	}
>> -	if (!qda_gem_obj->iommu_dev->dev) {
>> -		qda_err(NULL, "Allocated buffer missing IOMMU device\n");
>> -		return -EINVAL;
>> -	}
>> -	if (!qda_gem_obj->virt) {
>> -		qda_err(NULL, "Allocated buffer missing virtual address\n");
>> -		return -EINVAL;
>> -	}
>> -	if (qda_gem_obj->dma_addr == 0) {
>> -		qda_err(NULL, "Allocated buffer missing DMA address\n");
>> -		return -EINVAL;
>> +	if (qda_gem_obj->is_imported) {
>> +		if (!qda_gem_obj->sgt) {
>> +			qda_err(NULL, "Imported buffer missing sgt\n");
>> +			return -EINVAL;
>> +		}
>> +		if (!qda_gem_obj->iommu_dev || !qda_gem_obj->iommu_dev->dev) {
>> +			qda_err(NULL, "Imported buffer missing IOMMU device\n");
>> +			return -EINVAL;
>> +		}
>> +	} else {
>> +		if (!qda_gem_obj->iommu_dev || !qda_gem_obj->iommu_dev->dev) {
>> +			qda_err(NULL, "Allocated buffer missing IOMMU device\n");
>> +			return -EINVAL;
>> +		}
>> +		if (!qda_gem_obj->virt) {
>> +			qda_err(NULL, "Allocated buffer missing virtual address\n");
>> +			return -EINVAL;
>> +		}
>> +		if (qda_gem_obj->dma_addr == 0) {
>> +			qda_err(NULL, "Allocated buffer missing DMA address\n");
>> +			return -EINVAL;
>> +		}
>>  	}
>> -
>>  	return 0;
>>  }
>>  
>> @@ -60,9 +67,21 @@ void qda_gem_free_object(struct drm_gem_object *gem_obj)
>>  	struct qda_gem_obj *qda_gem_obj = to_qda_gem_obj(gem_obj);
>>  	struct qda_drm_priv *drm_priv = get_drm_priv_from_device(gem_obj->dev);
>>  
>> -	if (qda_gem_obj->virt) {
>> -		if (drm_priv && drm_priv->iommu_mgr)
>> +	if (qda_gem_obj->is_imported) {
>> +		if (qda_gem_obj->attachment && qda_gem_obj->sgt)
>> +			dma_buf_unmap_attachment_unlocked(qda_gem_obj->attachment,
>> +							  qda_gem_obj->sgt, DMA_BIDIRECTIONAL);
>> +		if (qda_gem_obj->attachment)
>> +			dma_buf_detach(qda_gem_obj->dma_buf, qda_gem_obj->attachment);
>> +		if (qda_gem_obj->dma_buf)
>> +			dma_buf_put(qda_gem_obj->dma_buf);
>> +		if (qda_gem_obj->iommu_dev && drm_priv && drm_priv->iommu_mgr)
>>  			qda_memory_manager_free(drm_priv->iommu_mgr, qda_gem_obj);
>> +	} else {
>> +		if (qda_gem_obj->virt) {
>> +			if (drm_priv && drm_priv->iommu_mgr)
>> +				qda_memory_manager_free(drm_priv->iommu_mgr, qda_gem_obj);
>> +		}
>>  	}
>>  
>>  	drm_gem_object_release(gem_obj);
>> @@ -174,6 +193,11 @@ struct drm_gem_object *qda_gem_create_object(struct drm_device *drm_dev,
>>  	qda_gem_obj = qda_gem_alloc_object(drm_dev, aligned_size);
>>  	if (IS_ERR(qda_gem_obj))
>>  		return (struct drm_gem_object *)qda_gem_obj;
>> +	qda_gem_obj->is_imported = false;
>> +	qda_gem_obj->dma_buf = NULL;
>> +	qda_gem_obj->attachment = NULL;
>> +	qda_gem_obj->sgt = NULL;
>> +	qda_gem_obj->imported_dma_addr = 0;
>>  
>>  	ret = qda_memory_manager_alloc(iommu_mgr, qda_gem_obj, file_priv);
>>  	if (ret) {
>> diff --git a/drivers/accel/qda/qda_gem.h b/drivers/accel/qda/qda_gem.h
>> index cbd5d0a58fa4..3566c5b2ad88 100644
>> --- a/drivers/accel/qda/qda_gem.h
>> +++ b/drivers/accel/qda/qda_gem.h
>> @@ -31,6 +31,16 @@ struct qda_gem_obj {
>>  	size_t size;
>>  	/* IOMMU device that performed the allocation */
>>  	struct qda_iommu_device *iommu_dev;
>> +	/* True if buffer is imported, false if allocated */
>> +	bool is_imported;
>> +	/* Reference to imported dma_buf */
>> +	struct dma_buf *dma_buf;
>> +	/* DMA buf attachment */
>> +	struct dma_buf_attachment *attachment;
>> +	/* Scatter-gather table */
>> +	struct sg_table *sgt;
>> +	/* DMA address of imported buffer */
>> +	dma_addr_t imported_dma_addr;
>>  };
>>  
>>  /*
>> diff --git a/drivers/accel/qda/qda_ioctl.c b/drivers/accel/qda/qda_ioctl.c
>> index ef3c9c691cb7..d91983048d6c 100644
>> --- a/drivers/accel/qda/qda_ioctl.c
>> +++ b/drivers/accel/qda/qda_ioctl.c
>> @@ -5,6 +5,7 @@
>>  #include <drm/qda_accel.h>
>>  #include "qda_drv.h"
>>  #include "qda_ioctl.h"
>> +#include "qda_prime.h"
>>  
>>  static int qda_validate_and_get_context(struct drm_device *dev, struct drm_file *file_priv,
>>  					struct qda_dev **qdev, struct qda_user **qda_user)
>> @@ -78,3 +79,9 @@ int qda_ioctl_gem_mmap_offset(struct drm_device *dev, void *data, struct drm_fil
>>  	drm_gem_object_put(gem_obj);
>>  	return ret;
>>  }
>> +
>> +int qda_ioctl_prime_fd_to_handle(struct drm_device *dev, struct drm_file *file_priv, int prime_fd,
>> +				 u32 *handle)
>> +{
>> +	return qda_prime_fd_to_handle(dev, file_priv, prime_fd, handle);
>> +}
>> diff --git a/drivers/accel/qda/qda_ioctl.h b/drivers/accel/qda/qda_ioctl.h
>> index 6bf3bcd28c0e..d454256f5fc5 100644
>> --- a/drivers/accel/qda/qda_ioctl.h
>> +++ b/drivers/accel/qda/qda_ioctl.h
>> @@ -23,4 +23,19 @@
>>   */
>>  int qda_ioctl_query(struct drm_device *dev, void *data, struct drm_file *file_priv);
>>  
>> +/**
>> + * qda_ioctl_prime_fd_to_handle - IOCTL handler for PRIME FD to handle conversion
>> + * @dev: DRM device structure
>> + * @file_priv: DRM file private data
>> + * @prime_fd: File descriptor of the PRIME buffer
>> + * @handle: Output parameter for the GEM handle
>> + *
>> + * This IOCTL handler converts a PRIME file descriptor to a GEM handle.
>> + * It serves as both the DRM driver callback and can be used directly.
>> + *
>> + * Return: 0 on success, negative error code on failure
>> + */
>> +int qda_ioctl_prime_fd_to_handle(struct drm_device *dev, struct drm_file *file_priv,
>> +				 int prime_fd, u32 *handle);
>> +
>>  #endif /* _QDA_IOCTL_H */
>> diff --git a/drivers/accel/qda/qda_memory_manager.c b/drivers/accel/qda/qda_memory_manager.c
>> index e225667557ee..3fd20f17c57b 100644
>> --- a/drivers/accel/qda/qda_memory_manager.c
>> +++ b/drivers/accel/qda/qda_memory_manager.c
>> @@ -154,8 +154,8 @@ static struct qda_iommu_device *get_process_iommu_device(struct qda_memory_manag
>>  	return qda_priv->assigned_iommu_dev;
>>  }
>>  
>> -static int qda_memory_manager_assign_device(struct qda_memory_manager *mem_mgr,
>> -					    struct drm_file *file_priv)
>> +int qda_memory_manager_assign_device(struct qda_memory_manager *mem_mgr,
>> +				     struct drm_file *file_priv)
>>  {
>>  	struct qda_file_priv *qda_priv;
>>  	struct qda_iommu_device *selected_dev = NULL;
>> @@ -223,6 +223,35 @@ static struct qda_iommu_device *get_or_assign_iommu_device(struct qda_memory_man
>>  	return NULL;
>>  }
>>  
>> +static int qda_memory_manager_map_imported(struct qda_memory_manager *mem_mgr,
>> +					   struct qda_gem_obj *gem_obj,
>> +					   struct qda_iommu_device *iommu_dev)
>> +{
>> +	struct scatterlist *sg;
>> +	dma_addr_t dma_addr;
>> +	int ret = 0;
>> +
>> +	if (!gem_obj->is_imported || !gem_obj->sgt || !iommu_dev) {
>> +		qda_err(NULL, "Invalid parameters for imported buffer mapping\n");
>> +		return -EINVAL;
>> +	}
>> +
>> +	gem_obj->iommu_dev = iommu_dev;
>> +
>> +	sg = gem_obj->sgt->sgl;
>> +	if (sg) {
>> +		dma_addr = sg_dma_address(sg);
>> +		dma_addr += ((u64)iommu_dev->sid << 32);
>> +
>> +		gem_obj->imported_dma_addr = dma_addr;
>> +	} else {
>> +		qda_err(NULL, "Invalid scatter-gather list for imported buffer\n");
>> +		ret = -EINVAL;
>> +	}
>> +
>> +	return ret;
>> +}
>> +
>>  int qda_memory_manager_alloc(struct qda_memory_manager *mem_mgr, struct qda_gem_obj *gem_obj,
>>  			     struct drm_file *file_priv)
>>  {
>> @@ -248,7 +277,10 @@ int qda_memory_manager_alloc(struct qda_memory_manager *mem_mgr, struct qda_gem_
>>  		return -ENOMEM;
>>  	}
>>  
>> -	ret = qda_dma_alloc(selected_dev, gem_obj, size);
>> +	if (gem_obj->is_imported)
>> +		ret = qda_memory_manager_map_imported(mem_mgr, gem_obj, selected_dev);
>> +	else
>> +		ret = qda_dma_alloc(selected_dev, gem_obj, size);
>>  
>>  	if (ret) {
>>  		qda_err(NULL, "Allocation failed: size=%zu, device_id=%u, ret=%d\n",
>> @@ -268,6 +300,10 @@ void qda_memory_manager_free(struct qda_memory_manager *mem_mgr, struct qda_gem_
>>  		return;
>>  	}
>>  
>> +	if (gem_obj->is_imported) {
>> +		qda_dbg(NULL, "Freed imported buffer tracking (no DMA free needed)\n");
>> +		return;
>> +	}
>>  	qda_dma_free(gem_obj);
>>  }
>>  
>> diff --git a/drivers/accel/qda/qda_memory_manager.h b/drivers/accel/qda/qda_memory_manager.h
>> index bac44284ef98..f6c7963cec42 100644
>> --- a/drivers/accel/qda/qda_memory_manager.h
>> +++ b/drivers/accel/qda/qda_memory_manager.h
>> @@ -106,6 +106,20 @@ int qda_memory_manager_register_device(struct qda_memory_manager *mem_mgr,
>>  void qda_memory_manager_unregister_device(struct qda_memory_manager *mem_mgr,
>>  					  struct qda_iommu_device *iommu_dev);
>>  
>> +/**
>> + * qda_memory_manager_assign_device() - Assign an IOMMU device to a process
>> + * @mem_mgr: Pointer to memory manager
>> + * @file_priv: DRM file private data for process association
>> + *
>> + * Assigns an IOMMU device to the calling process. If the process already has
>> + * a device assigned, returns success. If another file descriptor from the same
>> + * PID has a device, reuses it. Otherwise, finds an available device and assigns it.
>> + *
>> + * Return: 0 on success, negative error code on failure
>> + */
>> +int qda_memory_manager_assign_device(struct qda_memory_manager *mem_mgr,
>> +				     struct drm_file *file_priv);
>> +
>>  /**
>>   * qda_memory_manager_alloc() - Allocate memory for a GEM object
>>   * @mem_mgr: Pointer to memory manager
>> diff --git a/drivers/accel/qda/qda_prime.c b/drivers/accel/qda/qda_prime.c
>> new file mode 100644
>> index 000000000000..3d23842e48bb
>> --- /dev/null
>> +++ b/drivers/accel/qda/qda_prime.c
>> @@ -0,0 +1,194 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
>> +#include <drm/drm_gem.h>
>> +#include <drm/drm_prime.h>
>> +#include <linux/slab.h>
>> +#include <linux/dma-mapping.h>
>> +#include "qda_drv.h"
>> +#include "qda_gem.h"
>> +#include "qda_prime.h"
>> +#include "qda_memory_manager.h"
>> +
>> +static struct drm_gem_object *check_own_buffer(struct drm_device *dev, struct dma_buf *dma_buf)
>> +{
>> +	if (dma_buf->priv) {
>> +		struct drm_gem_object *existing_gem = dma_buf->priv;
> Randomly looking at your driver — you’ve broken the dma-buf cross-driver
> contract here. How do you know dma_buf->priv is a struct drm_gem_object?
> You don’t, because that is assigned by the exporter, and userspace could
> pass in a dma-buf from another device and blow up your driver.
>
> I think you just want to call drm_gem_is_prime_exported_dma_buf() here
> before doing anything.
>
> The rest of this dma-buf code also looks highly questionable. I’d study
> how other drivers implement their dma-buf paths and use those as a
> reference to improve yours.
>
> Matt
I had this concern while developing this patch but I was not able to find the right way
to handle this. I'll look into drm_gem_is_prime_exported_dma_buf() and see if it fits
here. For the rest of the dma-buf, the mapping part is something where I could not see my
requirements on any other driver(mapping to iommu device), so I might be trying to
implement something new here. That being said, I'll go through some more drivers and
check if my dma-buf could be improved.

Thanks for the review and your suggestion, Matt.
>
>> +
>> +		if (existing_gem->dev == dev) {
>> +			struct qda_gem_obj *existing_qda_gem = to_qda_gem_obj(existing_gem);
>> +
>> +			if (!existing_qda_gem->is_imported) {
>> +				drm_gem_object_get(existing_gem);
>> +				return existing_gem;
>> +			}
>> +		}
>> +	}
>> +	return NULL;
>> +}
>> +
>> +static struct qda_iommu_device *get_iommu_device_for_import(struct qda_drm_priv *drm_priv,
>> +							    struct drm_file **file_priv_out,
>> +							    struct qda_dev *qdev)
>> +{
>> +	struct drm_file *file_priv;
>> +	struct qda_file_priv *qda_file_priv;
>> +	struct qda_iommu_device *iommu_dev = NULL;
>> +	int ret;
>> +
>> +	file_priv = drm_priv->current_import_file_priv;
>> +	*file_priv_out = file_priv;
>> +
>> +	if (!file_priv || !file_priv->driver_priv)
>> +		return NULL;
>> +
>> +	qda_file_priv = (struct qda_file_priv *)file_priv->driver_priv;
>> +	iommu_dev = qda_file_priv->assigned_iommu_dev;
>> +
>> +	if (!iommu_dev) {
>> +		ret = qda_memory_manager_assign_device(drm_priv->iommu_mgr, file_priv);
>> +		if (ret) {
>> +			qda_err(qdev, "Failed to assign IOMMU device: %d\n", ret);
>> +			return NULL;
>> +		}
>> +
>> +		iommu_dev = qda_file_priv->assigned_iommu_dev;
>> +	}
>> +
>> +	return iommu_dev;
>> +}
>> +
>> +static int setup_dma_buf_mapping(struct qda_gem_obj *qda_gem_obj, struct dma_buf *dma_buf,
>> +				 struct device *attach_dev, struct qda_dev *qdev)
>> +{
>> +	struct dma_buf_attachment *attachment;
>> +	struct sg_table *sgt;
>> +	int ret;
>> +
>> +	attachment = dma_buf_attach(dma_buf, attach_dev);
>> +	if (IS_ERR(attachment)) {
>> +		ret = PTR_ERR(attachment);
>> +		qda_err(qdev, "Failed to attach dma_buf: %d\n", ret);
>> +		return ret;
>> +	}
>> +	qda_gem_obj->attachment = attachment;
>> +
>> +	sgt = dma_buf_map_attachment_unlocked(attachment, DMA_BIDIRECTIONAL);
>> +	if (IS_ERR(sgt)) {
>> +		ret = PTR_ERR(sgt);
>> +		qda_err(qdev, "Failed to map dma_buf attachment: %d\n", ret);
>> +		dma_buf_detach(dma_buf, attachment);
>> +		return ret;
>> +	}
>> +	qda_gem_obj->sgt = sgt;
>> +
>> +	return 0;
>> +}
>> +
>> +struct drm_gem_object *qda_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
>> +{
>> +	struct qda_drm_priv *drm_priv;
>> +	struct qda_gem_obj *qda_gem_obj;
>> +	struct drm_file *file_priv;
>> +	struct qda_iommu_device *iommu_dev;
>> +	struct qda_dev *qdev;
>> +	struct drm_gem_object *existing_gem;
>> +	size_t aligned_size;
>> +	int ret;
>> +
>> +	drm_priv = get_drm_priv_from_device(dev);
>> +	if (!drm_priv || !drm_priv->iommu_mgr) {
>> +		qda_err(NULL, "Invalid drm_priv or iommu_mgr\n");
>> +		return ERR_PTR(-EINVAL);
>> +	}
>> +
>> +	qdev = drm_priv->qdev;
>> +
>> +	existing_gem = check_own_buffer(dev, dma_buf);
>> +	if (existing_gem)
>> +		return existing_gem;
>> +
>> +	iommu_dev = get_iommu_device_for_import(drm_priv, &file_priv, qdev);
>> +	if (!iommu_dev || !iommu_dev->dev) {
>> +		qda_err(qdev, "No IOMMU device assigned for prime import\n");
>> +		return ERR_PTR(-ENODEV);
>> +	}
>> +
>> +	qda_dbg(qdev, "Using IOMMU device %u for prime import\n", iommu_dev->id);
>> +
>> +	aligned_size = PAGE_ALIGN(dma_buf->size);
>> +	qda_gem_obj = qda_gem_alloc_object(dev, aligned_size);
>> +	if (IS_ERR(qda_gem_obj))
>> +		return (struct drm_gem_object *)qda_gem_obj;
>> +
>> +	qda_gem_obj->is_imported = true;
>> +	qda_gem_obj->dma_buf = dma_buf;
>> +	qda_gem_obj->virt = NULL;
>> +	qda_gem_obj->dma_addr = 0;
>> +	qda_gem_obj->imported_dma_addr = 0;
>> +	qda_gem_obj->iommu_dev = iommu_dev;
>> +
>> +	get_dma_buf(dma_buf);
>> +
>> +	ret = setup_dma_buf_mapping(qda_gem_obj, dma_buf, iommu_dev->dev, qdev);
>> +	if (ret)
>> +		goto err_put_dma_buf;
>> +
>> +	ret = qda_memory_manager_alloc(drm_priv->iommu_mgr, qda_gem_obj, file_priv);
>> +	if (ret) {
>> +		qda_err(qdev, "Failed to allocate IOMMU mapping: %d\n", ret);
>> +		goto err_unmap;
>> +	}
>> +
>> +	qda_dbg(qdev, "Prime import completed successfully size=%zu\n", aligned_size);
>> +	return &qda_gem_obj->base;
>> +
>> +err_unmap:
>> +	dma_buf_unmap_attachment_unlocked(qda_gem_obj->attachment,
>> +					  qda_gem_obj->sgt, DMA_BIDIRECTIONAL);
>> +	dma_buf_detach(dma_buf, qda_gem_obj->attachment);
>> +err_put_dma_buf:
>> +	dma_buf_put(dma_buf);
>> +	qda_gem_cleanup_object(qda_gem_obj);
>> +	return ERR_PTR(ret);
>> +}
>> +
>> +int qda_prime_fd_to_handle(struct drm_device *dev, struct drm_file *file_priv,
>> +			   int prime_fd, u32 *handle)
>> +{
>> +	struct qda_drm_priv *drm_priv;
>> +	struct qda_dev *qdev;
>> +	int ret;
>> +
>> +	drm_priv = get_drm_priv_from_device(dev);
>> +	if (!drm_priv) {
>> +		qda_dbg(NULL, "Failed to get drm_priv from device\n");
>> +		return -EINVAL;
>> +	}
>> +
>> +	qdev = drm_priv->qdev;
>> +
>> +	if (file_priv && file_priv->driver_priv) {
>> +		struct qda_file_priv *qda_file_priv;
>> +
>> +		qda_file_priv = (struct qda_file_priv *)file_priv->driver_priv;
>> +	} else {
>> +		qda_dbg(qdev, "Called with NULL file_priv or driver_priv\n");
>> +	}
>> +
>> +	mutex_lock(&drm_priv->import_lock);
>> +	drm_priv->current_import_file_priv = file_priv;
>> +
>> +	ret = drm_gem_prime_fd_to_handle(dev, file_priv, prime_fd, handle);
>> +
>> +	drm_priv->current_import_file_priv = NULL;
>> +	mutex_unlock(&drm_priv->import_lock);
>> +
>> +	if (!ret)
>> +		qda_dbg(qdev, "Completed with ret=%d, handle=%u\n", ret, *handle);
>> +	else
>> +		qda_dbg(qdev, "Completed with ret=%d\n", ret);
>> +
>> +	return ret;
>> +}
>> +
>> +MODULE_IMPORT_NS("DMA_BUF");
>> diff --git a/drivers/accel/qda/qda_prime.h b/drivers/accel/qda/qda_prime.h
>> new file mode 100644
>> index 000000000000..939902454dcd
>> --- /dev/null
>> +++ b/drivers/accel/qda/qda_prime.h
>> @@ -0,0 +1,43 @@
>> +/* SPDX-License-Identifier: GPL-2.0-only */
>> +/*
>> + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
>> + */
>> +
>> +#ifndef _QDA_PRIME_H
>> +#define _QDA_PRIME_H
>> +
>> +#include <drm/drm_device.h>
>> +#include <drm/drm_file.h>
>> +#include <drm/drm_gem.h>
>> +#include <linux/dma-buf.h>
>> +
>> +/**
>> + * qda_gem_prime_import - Import a DMA-BUF as a GEM object
>> + * @dev: DRM device structure
>> + * @dma_buf: DMA-BUF to import
>> + *
>> + * This function imports an external DMA-BUF into the QDA driver as a GEM
>> + * object. It handles both re-imports of buffers originally from this driver
>> + * and imports of external buffers from other drivers.
>> + *
>> + * Return: Pointer to the imported GEM object on success, ERR_PTR on failure
>> + */
>> +struct drm_gem_object *qda_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
>> +
>> +/**
>> + * qda_prime_fd_to_handle - Core implementation for PRIME FD to GEM handle conversion
>> + * @dev: DRM device structure
>> + * @file_priv: DRM file private data
>> + * @prime_fd: File descriptor of the PRIME buffer
>> + * @handle: Output parameter for the GEM handle
>> + *
>> + * This core function sets up the necessary context before calling the
>> + * DRM framework's prime FD to handle conversion. It ensures proper IOMMU
>> + * device assignment and tracking for the import operation.
>> + *
>> + * Return: 0 on success, negative error code on failure
>> + */
>> +int qda_prime_fd_to_handle(struct drm_device *dev, struct drm_file *file_priv,
>> +			   int prime_fd, u32 *handle);
>> +
>> +#endif /* _QDA_PRIME_H */
>>
>> -- 
>> 2.34.1
>>


  reply	other threads:[~2026-03-02  9:19 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <vU2QyEVqOu-D3eGp7BZFICUeauxL32bwWzeidOAijoeVaJTk8KcRVsaQQD4MdFQEcaQTZ5RkzRsz9-Lhl1qsqg==@protonmail.internalid>
2026-02-23 19:08 ` [PATCH RFC 00/18] accel/qda: Introduce Qualcomm DSP Accelerator driver Ekansh Gupta
2026-02-23 19:08   ` [PATCH RFC 01/18] accel/qda: Add Qualcomm QDA DSP accelerator driver docs Ekansh Gupta
2026-02-23 21:17     ` Dmitry Baryshkov
2026-02-25 13:57       ` Ekansh Gupta
2026-02-25 17:17         ` Dmitry Baryshkov
2026-02-24  3:33     ` Trilok Soni
2026-02-25 14:17       ` Ekansh Gupta
2026-02-25 15:12         ` Bjorn Andersson
2026-02-25 19:16           ` Trilok Soni
2026-02-25 19:40             ` Dmitry Baryshkov
2026-02-25 23:18               ` Trilok Soni
2026-04-02  8:41                 ` Ekansh Gupta
2026-02-23 19:08   ` [PATCH RFC 02/18] accel/qda: Add Qualcomm DSP accelerator driver skeleton Ekansh Gupta
2026-02-23 21:52     ` Bjorn Andersson
2026-02-25 14:20       ` Ekansh Gupta
2026-02-23 19:08   ` [PATCH RFC 03/18] accel/qda: Add RPMsg transport for Qualcomm DSP accelerator Ekansh Gupta
2026-02-23 21:23     ` Dmitry Baryshkov
2026-02-23 21:50       ` Bjorn Andersson
2026-02-23 22:12         ` Dmitry Baryshkov
2026-02-23 22:25           ` Bjorn Andersson
2026-02-23 22:41             ` Dmitry Baryshkov
2026-02-25 17:16       ` Ekansh Gupta
2026-02-23 19:08   ` [PATCH RFC 04/18] accel/qda: Add built-in compute CB bus for QDA and integrate with IOMMU Ekansh Gupta
2026-02-23 22:44     ` Dmitry Baryshkov
2026-02-25 17:56       ` Ekansh Gupta
2026-02-25 19:09         ` Dmitry Baryshkov
2026-03-02  8:12           ` Ekansh Gupta
2026-02-26 10:46     ` Krzysztof Kozlowski
2026-02-23 19:08   ` [PATCH RFC 05/18] accel/qda: Create compute CB devices on QDA compute bus Ekansh Gupta
2026-02-23 22:49     ` Dmitry Baryshkov
2026-02-26  8:38       ` Ekansh Gupta
2026-02-26 10:46         ` Dmitry Baryshkov
2026-03-02  8:10           ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 06/18] accel/qda: Add memory manager for CB devices Ekansh Gupta
2026-02-23 22:50     ` Dmitry Baryshkov
2026-03-02  8:15       ` Ekansh Gupta
2026-03-04  4:22         ` Dmitry Baryshkov
2026-02-23 23:11     ` Bjorn Andersson
2026-03-02  8:30       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 07/18] accel/qda: Add DRM accel device registration for QDA driver Ekansh Gupta
2026-02-23 22:16     ` Dmitry Baryshkov
2026-03-02  8:33       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 08/18] accel/qda: Add per-file DRM context and open/close handling Ekansh Gupta
2026-02-23 22:20     ` Dmitry Baryshkov
2026-03-02  8:36       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 09/18] accel/qda: Add QUERY IOCTL and basic QDA UAPI header Ekansh Gupta
2026-02-23 22:24     ` Dmitry Baryshkov
2026-03-02  8:41       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 10/18] accel/qda: Add DMA-backed GEM objects and memory manager integration Ekansh Gupta
2026-02-23 22:36     ` Dmitry Baryshkov
2026-03-02  9:06       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 11/18] accel/qda: Add GEM_CREATE and GEM_MMAP_OFFSET IOCTLs Ekansh Gupta
2026-02-23 22:39     ` Dmitry Baryshkov
2026-03-02  9:07       ` Ekansh Gupta
2026-02-24  9:05     ` Christian König
2026-03-02  9:08       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 12/18] accel/qda: Add PRIME dma-buf import support Ekansh Gupta
2026-02-24  8:52     ` Matthew Brost
2026-03-02  9:19       ` Ekansh Gupta [this message]
2026-02-24  9:12     ` Christian König
2026-03-09  6:59       ` Ekansh Gupta
2026-04-02  8:36         ` Ekansh Gupta
2026-04-02  8:51           ` Christian König
2026-02-23 19:09   ` [PATCH RFC 13/18] accel/qda: Add initial FastRPC attach and release support Ekansh Gupta
2026-02-23 23:07     ` Dmitry Baryshkov
2026-03-09  6:50       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 14/18] accel/qda: Add FastRPC dynamic invocation support Ekansh Gupta
2026-02-23 23:10     ` Dmitry Baryshkov
2026-03-09  6:53       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 15/18] accel/qda: Add FastRPC DSP process creation support Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 16/18] accel/qda: Add FastRPC-based DSP memory mapping support Ekansh Gupta
2026-02-26 10:48     ` Krzysztof Kozlowski
2026-03-02  9:12       ` Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 17/18] accel/qda: Add FastRPC-based DSP memory unmapping support Ekansh Gupta
2026-02-23 19:09   ` [PATCH RFC 18/18] MAINTAINERS: Add MAINTAINERS entry for QDA driver Ekansh Gupta
2026-02-23 22:40     ` Dmitry Baryshkov
2026-03-02  8:41       ` Ekansh Gupta
2026-02-23 22:03   ` [PATCH RFC 00/18] accel/qda: Introduce Qualcomm DSP Accelerator driver Bjorn Andersson
2026-03-02  8:54     ` Ekansh Gupta
2026-02-24  3:37   ` Trilok Soni
2026-02-24  3:39   ` Trilok Soni
2026-03-02  8:43     ` Ekansh Gupta
2026-02-25 13:42   ` Bryan O'Donoghue
2026-02-25 19:12     ` Dmitry Baryshkov
2026-03-02 15:57   ` Srinivas Kandagatla
2026-03-09  8:07   ` Ekansh Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1f140186-534c-4b4e-b68a-6c9b8d8511e2@oss.qualcomm.com \
    --to=ekansh.gupta@oss.qualcomm.com \
    --cc=airlied@gmail.com \
    --cc=christian.koenig@amd.com \
    --cc=corbet@lwn.net \
    --cc=dmitry.baryshkov@oss.qualcomm.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.brost@intel.com \
    --cc=mripard@kernel.org \
    --cc=ogabbay@kernel.org \
    --cc=quic_bkumar@quicinc.com \
    --cc=quic_chennak@quicinc.com \
    --cc=robin.murphy@arm.com \
    --cc=simona@ffwll.ch \
    --cc=skhan@linuxfoundation.org \
    --cc=srinivas.kandagatla@oss.qualcomm.com \
    --cc=sumit.semwal@linaro.org \
    --cc=tzimmermann@suse.de \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®