Skip to content

Commit 67725f5

Browse files
trusinowiczjlawryno
authored andcommitted
accel/ivpu: Allow to import single buffer into multiple contexts
Use ivpu_gem_prime_import() based on drm_gem_prime_import_dev() for importing buffers, removing optimization for same device imports. This optimization reused the same ivpu_bo object in multiple contexts but a single buffer can be MMU-mapped only to a single context. Each import now creates a new instance of ivpu_bo object that shares the same sg_table but have separate MMU mappings. Reviewed-by: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com> Signed-off-by: Tomasz Rusinowicz <tomasz.rusinowicz@intel.com> Signed-off-by: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20250204084622.2422544-5-jacek.lawrynowicz@linux.intel.com
1 parent 320323d commit 67725f5

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

drivers/accel/ivpu/ivpu_drv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ static const struct drm_driver driver = {
452452
.postclose = ivpu_postclose,
453453

454454
.gem_create_object = ivpu_gem_create_object,
455-
.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
455+
.gem_prime_import = ivpu_gem_prime_import,
456456

457457
.ioctls = ivpu_drm_ioctls,
458458
.num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),

drivers/accel/ivpu/ivpu_gem.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "ivpu_mmu.h"
2121
#include "ivpu_mmu_context.h"
2222

23+
MODULE_IMPORT_NS("DMA_BUF");
24+
2325
static const struct drm_gem_object_funcs ivpu_gem_funcs;
2426

2527
static inline void ivpu_dbg_bo(struct ivpu_device *vdev, struct ivpu_bo *bo, const char *action)
@@ -172,6 +174,47 @@ struct drm_gem_object *ivpu_gem_create_object(struct drm_device *dev, size_t siz
172174
return &bo->base.base;
173175
}
174176

177+
struct drm_gem_object *ivpu_gem_prime_import(struct drm_device *dev,
178+
struct dma_buf *dma_buf)
179+
{
180+
struct device *attach_dev = dev->dev;
181+
struct dma_buf_attachment *attach;
182+
struct sg_table *sgt;
183+
struct drm_gem_object *obj;
184+
int ret;
185+
186+
attach = dma_buf_attach(dma_buf, attach_dev);
187+
if (IS_ERR(attach))
188+
return ERR_CAST(attach);
189+
190+
get_dma_buf(dma_buf);
191+
192+
sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
193+
if (IS_ERR(sgt)) {
194+
ret = PTR_ERR(sgt);
195+
goto fail_detach;
196+
}
197+
198+
obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
199+
if (IS_ERR(obj)) {
200+
ret = PTR_ERR(obj);
201+
goto fail_unmap;
202+
}
203+
204+
obj->import_attach = attach;
205+
obj->resv = dma_buf->resv;
206+
207+
return obj;
208+
209+
fail_unmap:
210+
dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
211+
fail_detach:
212+
dma_buf_detach(dma_buf, attach);
213+
dma_buf_put(dma_buf);
214+
215+
return ERR_PTR(ret);
216+
}
217+
175218
static struct ivpu_bo *ivpu_bo_alloc(struct ivpu_device *vdev, u64 size, u32 flags)
176219
{
177220
struct drm_gem_shmem_object *shmem;

drivers/accel/ivpu/ivpu_gem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int ivpu_bo_pin(struct ivpu_bo *bo);
2828
void ivpu_bo_unbind_all_bos_from_context(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx);
2929

3030
struct drm_gem_object *ivpu_gem_create_object(struct drm_device *dev, size_t size);
31+
struct drm_gem_object *ivpu_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
3132
struct ivpu_bo *ivpu_bo_create(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
3233
struct ivpu_addr_range *range, u64 size, u32 flags);
3334
struct ivpu_bo *ivpu_bo_create_global(struct ivpu_device *vdev, u64 size, u32 flags);

0 commit comments

Comments
 (0)