Skip to content

Commit

Permalink
drm/virtio: Add a helper to map and note the dma addrs and lengths
Browse files Browse the repository at this point in the history
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 <kraxel@redhat.com>
Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Cc: Rob Clark <robdclark@gmail.com>
Cc: Gurchetan Singh <gurchetansingh@chromium.org>
Cc: Chia-I Wu <olvaffe@gmail.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
Tested-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Reviewed-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20241126031643.3490496-3-vivek.kasireddy@intel.com
  • Loading branch information
Vivek Kasireddy authored and Dmitry Osipenko committed Nov 26, 2024
1 parent 06a0f77 commit 25c3fd1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions drivers/gpu/drm/virtio/virtgpu_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ 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;
Expand Down Expand Up @@ -477,6 +478,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);
int 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);
Expand Down
42 changes: 42 additions & 0 deletions drivers/gpu/drm/virtio/virtgpu_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -142,6 +144,46 @@ struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj,
return buf;
}

int 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 < 0 ? ret : -ETIMEDOUT;

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;
return 0;
}

struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
struct dma_buf *buf)
{
Expand Down

0 comments on commit 25c3fd1

Please sign in to comment.