Skip to content

Commit 25c3fd1

Browse files
vivekkreddydigetx
authored andcommitted
drm/virtio: Add a helper to map and note the dma addrs and lengths
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 <[email protected]> Cc: Dmitry Osipenko <[email protected]> Cc: Rob Clark <[email protected]> Cc: Gurchetan Singh <[email protected]> Cc: Chia-I Wu <[email protected]> Signed-off-by: Vivek Kasireddy <[email protected]> Tested-by: Dmitry Osipenko <[email protected]> Reviewed-by: Dmitry Osipenko <[email protected]> Signed-off-by: Dmitry Osipenko <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 06a0f77 commit 25c3fd1

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

drivers/gpu/drm/virtio/virtgpu_drv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct virtio_gpu_object_params {
8989

9090
struct virtio_gpu_object {
9191
struct drm_gem_shmem_object base;
92+
struct sg_table *sgt;
9293
uint32_t hw_res_handle;
9394
bool dumb;
9495
bool created;
@@ -477,6 +478,10 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
477478
struct drm_gem_object *virtgpu_gem_prime_import_sg_table(
478479
struct drm_device *dev, struct dma_buf_attachment *attach,
479480
struct sg_table *sgt);
481+
int virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
482+
unsigned int *nents,
483+
struct virtio_gpu_object *bo,
484+
struct dma_buf_attachment *attach);
480485

481486
/* virtgpu_debugfs.c */
482487
void virtio_gpu_debugfs_init(struct drm_minor *minor);

drivers/gpu/drm/virtio/virtgpu_prime.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include "virtgpu_drv.h"
2929

30+
MODULE_IMPORT_NS(DMA_BUF);
31+
3032
static int virtgpu_virtio_get_uuid(struct dma_buf *buf,
3133
uuid_t *uuid)
3234
{
@@ -142,6 +144,46 @@ struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj,
142144
return buf;
143145
}
144146

147+
int virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
148+
unsigned int *nents,
149+
struct virtio_gpu_object *bo,
150+
struct dma_buf_attachment *attach)
151+
{
152+
struct scatterlist *sl;
153+
struct sg_table *sgt;
154+
long i, ret;
155+
156+
dma_resv_assert_held(attach->dmabuf->resv);
157+
158+
ret = dma_resv_wait_timeout(attach->dmabuf->resv,
159+
DMA_RESV_USAGE_KERNEL,
160+
false, MAX_SCHEDULE_TIMEOUT);
161+
if (ret <= 0)
162+
return ret < 0 ? ret : -ETIMEDOUT;
163+
164+
sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
165+
if (IS_ERR(sgt))
166+
return PTR_ERR(sgt);
167+
168+
*ents = kvmalloc_array(sgt->nents,
169+
sizeof(struct virtio_gpu_mem_entry),
170+
GFP_KERNEL);
171+
if (!(*ents)) {
172+
dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
173+
return -ENOMEM;
174+
}
175+
176+
*nents = sgt->nents;
177+
for_each_sgtable_dma_sg(sgt, sl, i) {
178+
(*ents)[i].addr = cpu_to_le64(sg_dma_address(sl));
179+
(*ents)[i].length = cpu_to_le32(sg_dma_len(sl));
180+
(*ents)[i].padding = 0;
181+
}
182+
183+
bo->sgt = sgt;
184+
return 0;
185+
}
186+
145187
struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
146188
struct dma_buf *buf)
147189
{

0 commit comments

Comments
 (0)