From patchwork Thu Mar 28 08:32:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13608221 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 39ABDC54E64 for ; Thu, 28 Mar 2024 09:01:14 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3B27711237B; Thu, 28 Mar 2024 09:01:11 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="gxs5Sy3z"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id F3040112374 for ; Thu, 28 Mar 2024 09:01:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711616464; x=1743152464; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pGMd8TtU3EGogr2eLgRKB7WBGRnzNynqNECm62cTBho=; b=gxs5Sy3z5CBfawvbubSJGJgg/SgGUjKpBhwY4l2ZyPqu/nhX/1GPDenU 4zcgRdBzNVeUdNW6evVPtZbuc4i9bBGkH4IbmJamrn6N5S9Hn03PxxYzS WeT1i6EqTKB4hnqr5nDIyiwrW8ZTi/e7ElFdYFNx/Uf+42wfVXUYFEhIX W5vqqqZGUv0l1T3QkfSbNOcjJ8Ls1ivA7FhNFTzRJ3BA266OUwKNYkQfa R8Gu/vf2Tp5oOcIG+f5rlCpJyERSy/LtFY+lIA9HsEA765OMp7S80GVi6 Lh3pUAQFUd4oIf9/ku4wdt4rXYqtU349ZW14PhTx50xgFMD6pDVUADpjS w==; X-CSE-ConnectionGUID: +5tRG/kVQ5OUmYIbYMi5Ig== X-CSE-MsgGUID: Kenc5iJSROifTfrA02cbFg== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="7355691" X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="7355691" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="21078214" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:02 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Cc: Vivek Kasireddy , Gerd Hoffmann Subject: [RFC 1/7] drm/virtio: Implement VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING cmd Date: Thu, 28 Mar 2024 01:32:54 -0700 Message-ID: <20240328083615.2662516-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240328083615.2662516-1-vivek.kasireddy@intel.com> References: <20240328083615.2662516-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This cmd is useful to let the VMM (i.e, Qemu) know that the backing store associated with a resource is no longer valid, so that the VMM can perform any cleanup or unmap operations. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/gpu/drm/virtio/virtgpu_drv.h | 2 ++ drivers/gpu/drm/virtio/virtgpu_vq.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h index bb7d86a0c6a1..7347835e4fbe 100644 --- a/drivers/gpu/drm/virtio/virtgpu_drv.h +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h @@ -349,6 +349,8 @@ void virtio_gpu_object_attach(struct virtio_gpu_device *vgdev, struct virtio_gpu_object *obj, struct virtio_gpu_mem_entry *ents, unsigned int nents); +void virtio_gpu_cmd_resource_detach_backing(struct virtio_gpu_device *vgdev, + uint32_t resource_id); void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev, struct virtio_gpu_output *output); int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device *vgdev); diff --git a/drivers/gpu/drm/virtio/virtgpu_vq.c b/drivers/gpu/drm/virtio/virtgpu_vq.c index b1a00c0c25a7..17e2e7ab231a 100644 --- a/drivers/gpu/drm/virtio/virtgpu_vq.c +++ b/drivers/gpu/drm/virtio/virtgpu_vq.c @@ -645,6 +645,21 @@ virtio_gpu_cmd_resource_attach_backing(struct virtio_gpu_device *vgdev, virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence); } +void virtio_gpu_cmd_resource_detach_backing(struct virtio_gpu_device *vgdev, + uint32_t resource_id) +{ + struct virtio_gpu_resource_detach_backing *cmd_p; + struct virtio_gpu_vbuffer *vbuf; + + cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p)); + memset(cmd_p, 0, sizeof(*cmd_p)); + + cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING); + cmd_p->resource_id = cpu_to_le32(resource_id); + + virtio_gpu_queue_ctrl_buffer(vgdev, vbuf); +} + static void virtio_gpu_cmd_get_display_info_cb(struct virtio_gpu_device *vgdev, struct virtio_gpu_vbuffer *vbuf) { From patchwork Thu Mar 28 08:32:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13608220 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C42E4CD11DD for ; Thu, 28 Mar 2024 09:01:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C19C411237A; Thu, 28 Mar 2024 09:01:10 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="F8eGXweu"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9F13C112374 for ; Thu, 28 Mar 2024 09:01:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711616465; x=1743152465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZLevj/OjmhfV4rZUv+DlBsumNlKZPNXGupqqcKwe/Bw=; b=F8eGXweuQxFE6tkfY4lrY4B8MG/lJMEeJykqQCq9qjNZ1EduVeeNcm75 rAnE/OqEUa0R5MNMe9nO+gtt0fSooP+10I31uhfwYyX/UYfsjWL4fMHKs 1cOXQDgnt7YhfTxds/Iq+1Zbt2I4RcnQGa0ij0fLheGlZfrpiaToPZ45J MdIcGf14ipiF8GLiBFzuXwZzmcUxm40JXVUEPj2Zf1IPFoUi9zgIaLp8K K7tEHkcCFVsibZMvFFH+zNi2IeRVyO1wAM6+wO9RIt0l9BqHjrHQK3kg5 ukWQUrPkvWQjgB+J1A1lGYYdFAk2TkK0HRXEWhyUAQ0sB44Ik4JXmFm0P g==; X-CSE-ConnectionGUID: 2kVHWYD8SguoJF0Ssg2Qgg== X-CSE-MsgGUID: XGMDdV4NQWyYSRu8MTS21A== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="7355693" X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="7355693" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="21078217" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Cc: Vivek Kasireddy , Gerd Hoffmann Subject: [RFC 2/7] drm/virtio: Add a helper to map and note the dma addrs and lengths Date: Thu, 28 Mar 2024 01:32:55 -0700 Message-ID: <20240328083615.2662516-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240328083615.2662516-1-vivek.kasireddy@intel.com> References: <20240328083615.2662516-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This helper would be used when first initializing the object as part of import and also when updating the plane where we need to ensure that the imported object's backing is valid. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/gpu/drm/virtio/virtgpu_drv.h | 6 ++++ drivers/gpu/drm/virtio/virtgpu_prime.c | 44 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h index 7347835e4fbe..ca4cb166b509 100644 --- a/drivers/gpu/drm/virtio/virtgpu_drv.h +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h @@ -89,9 +89,11 @@ struct virtio_gpu_object_params { struct virtio_gpu_object { struct drm_gem_shmem_object base; + struct sg_table *sgt; uint32_t hw_res_handle; bool dumb; bool created; + bool has_backing; bool host3d_blob, guest_blob; uint32_t blob_mem, blob_flags; @@ -470,6 +472,10 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, struct drm_gem_object *virtgpu_gem_prime_import_sg_table( struct drm_device *dev, struct dma_buf_attachment *attach, struct sg_table *sgt); +long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents, + unsigned int *nents, + struct virtio_gpu_object *bo, + struct dma_buf_attachment *attach); /* virtgpu_debugfs.c */ void virtio_gpu_debugfs_init(struct drm_minor *minor); diff --git a/drivers/gpu/drm/virtio/virtgpu_prime.c b/drivers/gpu/drm/virtio/virtgpu_prime.c index 44425f20d91a..2a90df39c5de 100644 --- a/drivers/gpu/drm/virtio/virtgpu_prime.c +++ b/drivers/gpu/drm/virtio/virtgpu_prime.c @@ -27,6 +27,8 @@ #include "virtgpu_drv.h" +MODULE_IMPORT_NS(DMA_BUF); + static int virtgpu_virtio_get_uuid(struct dma_buf *buf, uuid_t *uuid) { @@ -142,6 +144,48 @@ struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, return buf; } +long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents, + unsigned int *nents, + struct virtio_gpu_object *bo, + struct dma_buf_attachment *attach) +{ + struct scatterlist *sl; + struct sg_table *sgt; + long i, ret; + + dma_resv_assert_held(attach->dmabuf->resv); + + ret = dma_resv_wait_timeout(attach->dmabuf->resv, + DMA_RESV_USAGE_KERNEL, + false, MAX_SCHEDULE_TIMEOUT); + if (ret < 0) + return ret; + + sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); + if (IS_ERR(sgt)) + return PTR_ERR(sgt); + + *ents = kvmalloc_array(sgt->nents, + sizeof(struct virtio_gpu_mem_entry), + GFP_KERNEL); + if (!(*ents)) { + dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); + return -ENOMEM; + } + + *nents = sgt->nents; + for_each_sgtable_dma_sg(sgt, sl, i) { + (*ents)[i].addr = cpu_to_le64(sg_dma_address(sl)); + (*ents)[i].length = cpu_to_le32(sg_dma_len(sl)); + (*ents)[i].padding = 0; + } + + bo->sgt = sgt; + bo->has_backing = true; + + return 0; +} + struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, struct dma_buf *buf) { From patchwork Thu Mar 28 08:32:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13608219 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C6194CD11DD for ; Thu, 28 Mar 2024 09:01:06 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BE0B2112374; Thu, 28 Mar 2024 09:01:05 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="ccc/MoST"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id BD802112375 for ; Thu, 28 Mar 2024 09:01:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711616465; x=1743152465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s8pAANdZtCNN1XiR0mAMSVFYGgUSJqQwR21Of+VmR58=; b=ccc/MoSTtbW0pa4WCFbicjtLGKqjifgPpd5vLZ9AO3qsDP9fyeFLgBu2 rHQORJm485zXOyJSzFS+hsZhRp1aD5BP7i3GiHs11pvd+HTolC4fVxYkj 88VuUCe5IorrSYsTnY249Lk1yEpUdP0dAoMi+WGB4yflBk8hWsBMiTnxP H4PByRTHblcCQChUQVlGg80ghmnUamv0elfYOfYP7umf35PW+2bxm+rcn NEqnYMXgPYkS2RsWmuLwWXcmjcfAZcehvFWfBFuLbHGMWuqEfmqjnZCrc HOYpFOsN/MTpoahKYPl+0nzPZkY38Bri4hHrYIq4UJPpYi+GtQq+b6jRN A==; X-CSE-ConnectionGUID: fB7RSZYtRvekkaiWeJC5gA== X-CSE-MsgGUID: b6ZfTn3HTfyiXzbCO890uA== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="7355696" X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="7355696" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="21078222" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Cc: Vivek Kasireddy , Gerd Hoffmann Subject: [RFC 3/7] drm/virtio: Add helpers to initialize and free the imported object Date: Thu, 28 Mar 2024 01:32:56 -0700 Message-ID: <20240328083615.2662516-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240328083615.2662516-1-vivek.kasireddy@intel.com> References: <20240328083615.2662516-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The imported object can be considered a guest blob resource; therefore, we use create_blob cmd while creating it. These helpers are used in the next patch which does the actual import. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/gpu/drm/virtio/virtgpu_prime.c | 69 ++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/drivers/gpu/drm/virtio/virtgpu_prime.c b/drivers/gpu/drm/virtio/virtgpu_prime.c index 2a90df39c5de..1e87dbc9a897 100644 --- a/drivers/gpu/drm/virtio/virtgpu_prime.c +++ b/drivers/gpu/drm/virtio/virtgpu_prime.c @@ -186,6 +186,75 @@ long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents, return 0; } +static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj) +{ + struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); + struct virtio_gpu_device *vgdev = obj->dev->dev_private; + struct dma_buf_attachment *attach = obj->import_attach; + + if (bo->created) { + virtio_gpu_cmd_unref_resource(vgdev, bo); + virtio_gpu_notify(vgdev); + } + + if (attach) { + dma_buf_detach(attach->dmabuf, attach); + dma_buf_put(attach->dmabuf); + } + + drm_gem_object_release(&bo->base.base); + kfree(bo); +} + +static int virtgpu_dma_buf_init_obj(struct drm_device *dev, + struct virtio_gpu_object *bo, + struct dma_buf_attachment *attach) +{ + struct virtio_gpu_device *vgdev = dev->dev_private; + struct virtio_gpu_object_params params = { 0 }; + struct dma_resv *resv = attach->dmabuf->resv; + struct virtio_gpu_mem_entry *ents = NULL; + unsigned int nents; + int ret; + + ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle); + if (ret) { + virtgpu_dma_buf_free_obj(&bo->base.base); + return ret; + } + + dma_resv_lock(resv, NULL); + + ret = dma_buf_pin(attach); + if (ret) + goto err_pin; + + ret = virtgpu_dma_buf_import_sgt(&ents, &nents, bo, attach); + if (ret) + goto err_import; + + bo->guest_blob = true; + params.blob = true; + params.blob_mem = VIRTGPU_BLOB_MEM_GUEST; + params.blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE; + params.size = attach->dmabuf->size; + + virtio_gpu_cmd_resource_create_blob(vgdev, bo, ¶ms, + ents, nents); + dma_buf_unpin(attach); + dma_resv_unlock(resv); + + return 0; + +err_import: + dma_buf_unpin(attach); +err_pin: + dma_resv_unlock(resv); + ida_free(&vgdev->resource_ida, bo->hw_res_handle - 1); + virtgpu_dma_buf_free_obj(&bo->base.base); + return ret; +} + struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, struct dma_buf *buf) { From patchwork Thu Mar 28 08:32:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13608223 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C28EBC54E64 for ; Thu, 28 Mar 2024 09:01:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0275111237D; Thu, 28 Mar 2024 09:01:23 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="K0cJNGKK"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id D023511237A for ; Thu, 28 Mar 2024 09:01:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711616465; x=1743152465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=igOa1YqCrusDmITVY46d0ciSsUPOrUNWa/VKP9eNVUg=; b=K0cJNGKKmYjA1jxVy9fxYcaWf3qI4lLQPWdrd4bBSi1TwBfnc1ILrgdC 9ZEa1jPHkOHPQvpaRqdwlWkNYBsXehqQqSC6yGlEwLLS+E/31S3nlV5Vb FwCekWuN0oZy5m2IdraGllLGkxTZRpfbkDC4DqWdziu2KHDG6965weH7o SgEm76fsL1SwVudmQYY8XhAEejgnzXfIs8Jv4vzz32tnp0WnJXw5s7RKx UvLWGpWsow16g/JOSSSg2AZ8Q48293YgamgCtiYx0GF0JiNh8YkXE7vXO Z1wTr7expMFEqQbzL8A1L8h7M7jqs7yEJISwhYXb97ugYYCWx3M4XpMSZ Q==; X-CSE-ConnectionGUID: W10Ur8loRb2VDb/PnBQGDw== X-CSE-MsgGUID: Uw3/58UESsGZv5N9+qxpGA== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="7355698" X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="7355698" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="21078228" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Cc: Vivek Kasireddy , Gerd Hoffmann Subject: [RFC 4/7] drm/virtio: Import prime buffers from other devices as guest blobs Date: Thu, 28 Mar 2024 01:32:57 -0700 Message-ID: <20240328083615.2662516-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240328083615.2662516-1-vivek.kasireddy@intel.com> References: <20240328083615.2662516-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" By importing scanout buffers from other devices, we should be able to use the virtio-gpu driver in KMS only mode. Note that we attach dynamically and register a move_notify() callback so that we can let the VMM know of any location changes associated with the backing store of the imported object by sending detach_backing cmd. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/gpu/drm/virtio/virtgpu_prime.c | 54 +++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/virtio/virtgpu_prime.c b/drivers/gpu/drm/virtio/virtgpu_prime.c index 1e87dbc9a897..c65dacc1b2b5 100644 --- a/drivers/gpu/drm/virtio/virtgpu_prime.c +++ b/drivers/gpu/drm/virtio/virtgpu_prime.c @@ -255,10 +255,36 @@ static int virtgpu_dma_buf_init_obj(struct drm_device *dev, return ret; } +static const struct drm_gem_object_funcs virtgpu_gem_dma_buf_funcs = { + .free = virtgpu_dma_buf_free_obj, +}; + +static void virtgpu_dma_buf_move_notify(struct dma_buf_attachment *attach) +{ + struct drm_gem_object *obj = attach->importer_priv; + struct virtio_gpu_device *vgdev = obj->dev->dev_private; + struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); + + if (bo->created) { + virtio_gpu_cmd_resource_detach_backing(vgdev, + bo->hw_res_handle); + bo->has_backing = false; + } +} + +static const struct dma_buf_attach_ops virtgpu_dma_buf_attach_ops = { + .allow_peer2peer = true, + .move_notify = virtgpu_dma_buf_move_notify +}; + struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, struct dma_buf *buf) { + struct virtio_gpu_device *vgdev = dev->dev_private; + struct dma_buf_attachment *attach; + struct virtio_gpu_object *bo; struct drm_gem_object *obj; + int ret; if (buf->ops == &virtgpu_dmabuf_ops.ops) { obj = buf->priv; @@ -272,7 +298,32 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, } } - return drm_gem_prime_import(dev, buf); + if (!vgdev->has_resource_blob || vgdev->has_virgl_3d) + return drm_gem_prime_import(dev, buf); + + bo = kzalloc(sizeof(*bo), GFP_KERNEL); + if (!bo) + return ERR_PTR(-ENOMEM); + + obj = &bo->base.base; + obj->funcs = &virtgpu_gem_dma_buf_funcs; + drm_gem_private_object_init(dev, obj, buf->size); + + attach = dma_buf_dynamic_attach(buf, dev->dev, + &virtgpu_dma_buf_attach_ops, obj); + if (IS_ERR(attach)) { + kfree(bo); + return ERR_CAST(attach); + } + + obj->import_attach = attach; + get_dma_buf(buf); + + ret = virtgpu_dma_buf_init_obj(dev, bo, attach); + if (ret < 0) + return ERR_PTR(ret); + + return obj; } struct drm_gem_object *virtgpu_gem_prime_import_sg_table( @@ -281,3 +332,4 @@ struct drm_gem_object *virtgpu_gem_prime_import_sg_table( { return ERR_PTR(-ENODEV); } + From patchwork Thu Mar 28 08:32:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13608226 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 25371CD11DD for ; Thu, 28 Mar 2024 09:01:37 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5602E112385; Thu, 28 Mar 2024 09:01:36 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="bam6dXt1"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id EEFA911237B for ; Thu, 28 Mar 2024 09:01:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711616465; x=1743152465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=beWUsaWB/DnP65fBEVBr266dc3wxk+XJ3t4STXQsvcM=; b=bam6dXt1MuMFhz5YVGRL3LMcrjr0t9xXASH1j8EyLBPR3UgunzubZGTD gY26vL80o9PWvr90wBJW4tNIAtoF+PbYnu79htRGhRrN0fwl/7UQHf5WO f9pdM35p8S5srYMkfYgq4fobt5EKpI+0i618BE+59gZQBPQi8cEObkg03 MhCbbY7+L1e35bLD3E87CwX28W6s5hyX9C7K+IEca9eFoOrksJ3ZNEWE9 WD8dOOospD7QfLrhkqz8XBTdf8oZC1jrPW0y89LETjs/y9im+1V+0z8CF MyW52mkOe7D3vDwCwmjKTwAK8+ojE7DhP8TrGpAtEuTqNSVe3tDf6EzxL g==; X-CSE-ConnectionGUID: lkUAg+K/QImP5/q1D8E72w== X-CSE-MsgGUID: lT/TM+3dRyaUggob7mcN5Q== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="7355702" X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="7355702" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="21078235" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Cc: Vivek Kasireddy , Gerd Hoffmann Subject: [RFC 5/7] drm/virtio: Ensure that bo's backing store is valid while updating plane Date: Thu, 28 Mar 2024 01:32:58 -0700 Message-ID: <20240328083615.2662516-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240328083615.2662516-1-vivek.kasireddy@intel.com> References: <20240328083615.2662516-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To make sure that the imported bo's backing store is valid, we first pin the associated dmabuf, import the sgt if need be and then unpin it after the update is complete. Note that we pin/unpin the dmabuf even when the backing store is valid to ensure that it does not move when the host update (resource_flush) is in progress. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/gpu/drm/virtio/virtgpu_plane.c | 56 +++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/virtio/virtgpu_plane.c b/drivers/gpu/drm/virtio/virtgpu_plane.c index a72a2dbda031..3ccf88f9addc 100644 --- a/drivers/gpu/drm/virtio/virtgpu_plane.c +++ b/drivers/gpu/drm/virtio/virtgpu_plane.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "virtgpu_drv.h" @@ -131,6 +132,45 @@ static void virtio_gpu_update_dumb_bo(struct virtio_gpu_device *vgdev, objs, NULL); } +static bool virtio_gpu_update_dmabuf_bo(struct virtio_gpu_device *vgdev, + struct drm_gem_object *obj) +{ + struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); + struct dma_buf_attachment *attach = obj->import_attach; + struct dma_resv *resv = attach->dmabuf->resv; + struct virtio_gpu_mem_entry *ents = NULL; + unsigned int nents; + int ret; + + dma_resv_lock(resv, NULL); + + ret = dma_buf_pin(attach); + if (ret) { + dma_resv_unlock(resv); + return false; + } + + if (!bo->has_backing) { + if (bo->sgt) + dma_buf_unmap_attachment(attach, + bo->sgt, + DMA_BIDIRECTIONAL); + + ret = virtgpu_dma_buf_import_sgt(&ents, &nents, + bo, attach); + if (ret) + goto err_import; + + virtio_gpu_object_attach(vgdev, bo, ents, nents); + } + return true; + +err_import: + dma_buf_unpin(attach); + dma_resv_unlock(resv); + return false; +} + static void virtio_gpu_resource_flush(struct drm_plane *plane, uint32_t x, uint32_t y, uint32_t width, uint32_t height) @@ -174,7 +214,9 @@ static void virtio_gpu_primary_plane_update(struct drm_plane *plane, struct virtio_gpu_device *vgdev = dev->dev_private; struct virtio_gpu_output *output = NULL; struct virtio_gpu_object *bo; + struct drm_gem_object *obj; struct drm_rect rect; + bool updated = false; if (plane->state->crtc) output = drm_crtc_to_virtio_gpu_output(plane->state->crtc); @@ -196,10 +238,17 @@ static void virtio_gpu_primary_plane_update(struct drm_plane *plane, if (!drm_atomic_helper_damage_merged(old_state, plane->state, &rect)) return; - bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]); + obj = plane->state->fb->obj[0]; + bo = gem_to_virtio_gpu_obj(obj); if (bo->dumb) virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect); + if (obj->import_attach) { + updated = virtio_gpu_update_dmabuf_bo(vgdev, obj); + if (!updated) + return; + } + if (plane->state->fb != old_state->fb || plane->state->src_w != old_state->src_w || plane->state->src_h != old_state->src_h || @@ -239,6 +288,11 @@ static void virtio_gpu_primary_plane_update(struct drm_plane *plane, rect.y1, rect.x2 - rect.x1, rect.y2 - rect.y1); + + if (obj->import_attach && updated) { + dma_buf_unpin(obj->import_attach); + dma_resv_unlock(obj->import_attach->dmabuf->resv); + } } static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane, From patchwork Thu Mar 28 08:32:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13608225 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3BA95C54E64 for ; Thu, 28 Mar 2024 09:01:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C5A2F112383; Thu, 28 Mar 2024 09:01:23 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="Fo2gJ5yQ"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id DC57D112374 for ; Thu, 28 Mar 2024 09:01:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711616465; x=1743152465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5vlf6vqKAYhk0ag6OYHcNY4wXaM3qL+5LRzjghAnNm4=; b=Fo2gJ5yQPLhGcjIlSIDwp8z/T1j74yUbgKe45CuRuxTCnNtVK09R3Cpa DRsicYYFAEB/PGnTixDoSZCkdaw1ZfIfiy16JD66jSvSqzKf/L5kyN52E 0Zd+y/f7awQHTKHKotsnC5MUXal8DLx8KHhLi41REMdibhGy5h9fAJ5vn SG5WS5HEFtvE2s2DzUUnjvDkaOjXtVISyFC58A3G/BV6jfSHLcCW92ssl JsRbuQSe++RejDPSi4jDpM/s4Q6dlwop2GIFC9pTion/5cOwwFxogqeMe 0Mh0mZ8b/iaJUMbtHQj8D803M9kGpUW3Nk66ZPQHPcJo8Kkx1DiVV9XD5 Q==; X-CSE-ConnectionGUID: 1rgikLtKTcmhBdQDbjto2Q== X-CSE-MsgGUID: ONRk70acTjiGZgR2R6gsfg== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="7355701" X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="7355701" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="21078240" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Cc: Vivek Kasireddy , Gerd Hoffmann Subject: [RFC 6/7] udmabuf/uapi: Add new ioctl to create a dmabuf from PCI bar regions Date: Thu, 28 Mar 2024 01:32:59 -0700 Message-ID: <20240328083615.2662516-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240328083615.2662516-1-vivek.kasireddy@intel.com> References: <20240328083615.2662516-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This new ioctl can be used by a VMM such as Qemu or other userspace applications to create a dmabuf from a PCI device's memory regions. The PCI device's id that the userspace app is required to provide needs to be encoded in the format specified by the following macro (defined in include/linux/pci.h): define PCI_DEVID(bus, devfn) ((((u16)(bus)) << 8) | (devfn)) where devfn is defined (in include/uapi/linux/pci.h) as define PCI_DEVFN(slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07)) In addition to the devid, the userspace needs to include the offsets and sizes and also the bar number as part of this request. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- include/uapi/linux/udmabuf.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/udmabuf.h b/include/uapi/linux/udmabuf.h index 46b6532ed855..16fe41fdc4b9 100644 --- a/include/uapi/linux/udmabuf.h +++ b/include/uapi/linux/udmabuf.h @@ -15,7 +15,15 @@ struct udmabuf_create { }; struct udmabuf_create_item { - __u32 memfd; + union { + struct { + __u32 memfd; + }; + struct { + __u16 devid; + __u16 bar; + }; + }; __u32 __pad; __u64 offset; __u64 size; @@ -29,5 +37,6 @@ struct udmabuf_create_list { #define UDMABUF_CREATE _IOW('u', 0x42, struct udmabuf_create) #define UDMABUF_CREATE_LIST _IOW('u', 0x43, struct udmabuf_create_list) +#define UDMABUF_CREATE_LIST_FOR_PCIDEV _IOW('u', 0x44, struct udmabuf_create_list) #endif /* _UAPI_LINUX_UDMABUF_H */ From patchwork Thu Mar 28 08:33:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13608224 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E51A8CD1283 for ; Thu, 28 Mar 2024 09:01:24 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 806B6112381; Thu, 28 Mar 2024 09:01:23 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="gZo4qpWJ"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by gabe.freedesktop.org (Postfix) with ESMTPS id 13DC3112375 for ; Thu, 28 Mar 2024 09:01:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711616465; x=1743152465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PM0jrZSP5cK8l59vQzDJsm6cRvYMNU6P+jdCf8vOtyI=; b=gZo4qpWJ0csSlco7r+uVUHXnJ/RUhs2r49tP0MlRulBG+x+v1C3CzBEK PCSR0AsNojkomYOXu1BfKRQmG3PdLF9U7+9jLPDJvtAMfTM+FnQn56Qu2 8wJY/p6Q7gzG5tVz7aHDB9hQq1+KnP5fw/ru9HAvbcOP3h06xUFctvKgd NAy0c2aH/nFjoUTuiqfnXKgwiAclv7mINdeEe+/ulSmU3LPakIzIFHX5f IVxUnEbzgGKgAcIMAaGlvtL28DvPlS9mv7Ir324Txpy3Eyt1JbWyPPy+f V8nxj/WywHLBJ5V8+1Hyum/sTE+Yv4m0p+T9Ue38xPt1Nl9OBPWwGmY8h Q==; X-CSE-ConnectionGUID: woQIefbMSPi3XV/g+IFhbQ== X-CSE-MsgGUID: RgZ1zzCcRtaNBO58KxiYGg== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="7355703" X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="7355703" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,161,1708416000"; d="scan'208";a="21078244" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2024 02:01:04 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Cc: Vivek Kasireddy , Gerd Hoffmann Subject: [RFC 7/7] udmabuf: Implement UDMABUF_CREATE_LIST_FOR_PCIDEV ioctl Date: Thu, 28 Mar 2024 01:33:00 -0700 Message-ID: <20240328083615.2662516-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240328083615.2662516-1-vivek.kasireddy@intel.com> References: <20240328083615.2662516-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" By implementing this request, the udmabuf driver would be able to support creating a dmabuf from a PCI device's bar region. This would facilitate P2P DMA operations between any two PCI devices as long as they are compatible. Based on the information (devid, bar) provided by the VMM, once the PCI device known as the provider is identified, we create a page pool associated with the requested bar region by calling pci_p2pdma_add_resource(). We then populate the ubuf->pages[] array with the pages from the pool that would eventually be included in a sgt which would be shared with the importers. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 122 +++++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 8 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 274defd3fa3e..7355451ed337 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,7 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is struct udmabuf { pgoff_t pagecount; struct page **pages; + struct pci_dev *pdev; struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; @@ -129,9 +131,28 @@ static void put_sg_table(struct device *dev, struct sg_table *sg, kfree(sg); } +static int check_p2p_support(struct dma_buf_attachment *attach) +{ + struct udmabuf *ubuf = attach->dmabuf->priv; + struct pci_dev *provider = ubuf->pdev; + struct device *client = attach->dev; + int ret = -1; + + if (!provider) + return 0; + + if (attach->peer2peer) + ret = pci_p2pdma_distance(provider, client, true); + + return ret < 0 ? ret : 0; +} + static struct sg_table *map_udmabuf(struct dma_buf_attachment *at, enum dma_data_direction direction) { + if (check_p2p_support(at) < 0) + return ERR_PTR(-EOPNOTSUPP); + return get_sg_table(at->dev, at->dmabuf, direction); } @@ -151,8 +172,15 @@ static void release_udmabuf(struct dma_buf *buf) if (ubuf->sg) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); - for (pg = 0; pg < ubuf->pagecount; pg++) - put_page(ubuf->pages[pg]); + for (pg = 0; pg < ubuf->pagecount; pg++) { + if (ubuf->pdev) + pci_free_p2pmem(ubuf->pdev, + page_to_virt(ubuf->pages[pg]), + PAGE_SIZE); + else + put_page(ubuf->pages[pg]); + } + kfree(ubuf->offsets); kfree(ubuf->pages); kfree(ubuf); @@ -269,9 +297,74 @@ static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, return 0; } +static int handle_pcidev_pages(struct udmabuf *ubuf, + struct udmabuf_create_list *head, + struct udmabuf_create_item *list) +{ + struct pci_dev *pdev = NULL; + resource_size_t bar_size; + pgoff_t pgbuf = 0; + struct page *page; + int i, ret; + size_t size; + void *addr; + + for (i = 0; i < head->count; i++) { + if (!ubuf->pdev) { + pdev = pci_get_domain_bus_and_slot(0, + PCI_BUS_NUM(list[i].devid), + list[i].devid & 0xff); + if (!pdev) { + ret = -ENODEV; + goto err; + } + + ubuf->pdev = pdev; + } + + bar_size = pci_resource_len(pdev, list[i].bar); + if (list[i].offset > bar_size || + list[i].offset + list[i].size > bar_size) { + ret = -EINVAL; + goto err; + } + + ret = pci_p2pdma_add_resource(pdev, + list[i].bar, + list[i].size, + list[i].offset); + if (ret) + goto err; + + addr = pci_alloc_p2pmem(pdev, list[i].size); + if (!addr) { + ret = -EINVAL; + goto err; + } + + size = 0; + while (size < list[i].size) { + page = virt_to_page((unsigned long)addr + size); + ubuf->pages[pgbuf++] = page; + + size += PAGE_SIZE; + } + } + +err: + while (pgbuf > 0 && ubuf->pages[--pgbuf]) + pci_free_p2pmem(pdev, + page_to_virt(ubuf->pages[pgbuf]), + PAGE_SIZE); + if (pdev) + pci_dev_put(pdev); + return ret; +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, - struct udmabuf_create_item *list) + struct udmabuf_create_item *list, + bool for_pcidev) { DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct file *memfd = NULL; @@ -312,6 +405,14 @@ static long udmabuf_create(struct miscdevice *device, goto err; } + if (for_pcidev) { + ret = handle_pcidev_pages(ubuf, head, list); + if (ret) + goto err; + + goto create_dmabuf; + } + pgbuf = 0; for (i = 0; i < head->count; i++) { ret = -EBADFD; @@ -344,6 +445,7 @@ static long udmabuf_create(struct miscdevice *device, memfd = NULL; } +create_dmabuf: exp_info.ops = &udmabuf_ops; exp_info.size = ubuf->pagecount << PAGE_SHIFT; exp_info.priv = ubuf; @@ -362,7 +464,7 @@ static long udmabuf_create(struct miscdevice *device, return dma_buf_fd(buf, flags); err: - while (pgbuf > 0) + while (pgbuf > 0 && !ubuf->pdev) put_page(ubuf->pages[--pgbuf]); if (memfd) fput(memfd); @@ -388,10 +490,11 @@ static long udmabuf_ioctl_create(struct file *filp, unsigned long arg) list.offset = create.offset; list.size = create.size; - return udmabuf_create(filp->private_data, &head, &list); + return udmabuf_create(filp->private_data, &head, &list, false); } -static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg) +static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg, + bool for_pcidev) { struct udmabuf_create_list head; struct udmabuf_create_item *list; @@ -407,7 +510,7 @@ static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg) if (IS_ERR(list)) return PTR_ERR(list); - ret = udmabuf_create(filp->private_data, &head, list); + ret = udmabuf_create(filp->private_data, &head, list, for_pcidev); kfree(list); return ret; } @@ -422,7 +525,10 @@ static long udmabuf_ioctl(struct file *filp, unsigned int ioctl, ret = udmabuf_ioctl_create(filp, arg); break; case UDMABUF_CREATE_LIST: - ret = udmabuf_ioctl_create_list(filp, arg); + ret = udmabuf_ioctl_create_list(filp, arg, false); + break; + case UDMABUF_CREATE_LIST_FOR_PCIDEV: + ret = udmabuf_ioctl_create_list(filp, arg, true); break; default: ret = -ENOTTY;