Skip to content

Commit dfadbbd

Browse files
marekolsakairlied
authored andcommitted
drm/ttm: add a way to bo_wait for either the last read or last write
Sometimes we want to know whether a buffer is busy and wait for it (bo_wait). However, sometimes it would be more useful to be able to query whether a buffer is busy and being either read or written, and wait until it's stopped being either read or written. The point of this is to be able to avoid unnecessary waiting, e.g. if a GPU has written something to a buffer and is now reading that buffer, and a CPU wants to map that buffer for read, it needs to only wait for the last write. If there were no write, there wouldn't be any waiting needed. This, or course, requires user space drivers to send read/write flags with each relocation (like we have read/write domains in radeon, so we can actually use those for something useful now). Now how this patch works: The read/write flags should passed to ttm_validate_buffer. TTM maintains separate sync objects of the last read and write for each buffer, in addition to the sync object of the last use of a buffer. ttm_bo_wait then operates with one the sync objects. Signed-off-by: Marek Olšák <maraeo@gmail.com> Reviewed-by: Jerome Glisse <jglisse@redhat.com> Signed-off-by: Dave Airlie <airlied@redhat.com>
1 parent b464e9a commit dfadbbd

11 files changed

Lines changed: 139 additions & 39 deletions

File tree

‎drivers/gpu/drm/nouveau/nouveau_bo.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,8 @@ nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
11041104
if (vma->node) {
11051105
if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) {
11061106
spin_lock(&nvbo->bo.bdev->fence_lock);
1107-
ttm_bo_wait(&nvbo->bo, false, false, false);
1107+
ttm_bo_wait(&nvbo->bo, false, false, false,
1108+
TTM_USAGE_READWRITE);
11081109
spin_unlock(&nvbo->bo.bdev->fence_lock);
11091110
nouveau_vm_unmap(vma);
11101111
}

‎drivers/gpu/drm/nouveau/nouveau_gem.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
589589
}
590590

591591
spin_lock(&nvbo->bo.bdev->fence_lock);
592-
ret = ttm_bo_wait(&nvbo->bo, false, false, false);
592+
ret = ttm_bo_wait(&nvbo->bo, false, false, false,
593+
TTM_USAGE_READWRITE);
593594
spin_unlock(&nvbo->bo.bdev->fence_lock);
594595
if (ret) {
595596
NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
@@ -825,7 +826,7 @@ nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
825826
nvbo = nouveau_gem_object(gem);
826827

827828
spin_lock(&nvbo->bo.bdev->fence_lock);
828-
ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
829+
ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait, TTM_USAGE_READWRITE);
829830
spin_unlock(&nvbo->bo.bdev->fence_lock);
830831
drm_gem_object_unreference_unlocked(gem);
831832
return ret;

‎drivers/gpu/drm/radeon/radeon_cs.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
8080
p->relocs[i].lobj.wdomain = r->write_domain;
8181
p->relocs[i].lobj.rdomain = r->read_domains;
8282
p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
83+
p->relocs[i].lobj.tv.usage = TTM_USAGE_READWRITE;
8384
p->relocs[i].handle = r->handle;
8485
p->relocs[i].flags = r->flags;
8586
radeon_bo_list_add_object(&p->relocs[i].lobj,

‎drivers/gpu/drm/radeon/radeon_object.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static inline int radeon_bo_wait(struct radeon_bo *bo, u32 *mem_type,
130130
if (mem_type)
131131
*mem_type = bo->tbo.mem.mem_type;
132132
if (bo->tbo.sync_obj)
133-
r = ttm_bo_wait(&bo->tbo, true, true, no_wait);
133+
r = ttm_bo_wait(&bo->tbo, true, true, no_wait, TTM_USAGE_READWRITE);
134134
spin_unlock(&bo->tbo.bdev->fence_lock);
135135
ttm_bo_unreserve(&bo->tbo);
136136
return r;

‎drivers/gpu/drm/ttm/ttm_bo.c‎

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
498498
int ret;
499499

500500
spin_lock(&bdev->fence_lock);
501-
(void) ttm_bo_wait(bo, false, false, true);
501+
(void) ttm_bo_wait(bo, false, false, true, TTM_USAGE_READWRITE);
502502
if (!bo->sync_obj) {
503503

504504
spin_lock(&glob->lru_lock);
@@ -566,7 +566,8 @@ static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
566566

567567
retry:
568568
spin_lock(&bdev->fence_lock);
569-
ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
569+
ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu,
570+
TTM_USAGE_READWRITE);
570571
spin_unlock(&bdev->fence_lock);
571572

572573
if (unlikely(ret != 0))
@@ -725,7 +726,8 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
725726
int ret = 0;
726727

727728
spin_lock(&bdev->fence_lock);
728-
ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
729+
ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu,
730+
TTM_USAGE_READWRITE);
729731
spin_unlock(&bdev->fence_lock);
730732

731733
if (unlikely(ret != 0)) {
@@ -1072,7 +1074,8 @@ int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
10721074
* instead of doing it here.
10731075
*/
10741076
spin_lock(&bdev->fence_lock);
1075-
ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1077+
ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu,
1078+
TTM_USAGE_READWRITE);
10761079
spin_unlock(&bdev->fence_lock);
10771080
if (ret)
10781081
return ret;
@@ -1692,34 +1695,83 @@ static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
16921695
return ret;
16931696
}
16941697

1698+
static void ttm_bo_unref_sync_obj_locked(struct ttm_buffer_object *bo,
1699+
void *sync_obj,
1700+
void **extra_sync_obj)
1701+
{
1702+
struct ttm_bo_device *bdev = bo->bdev;
1703+
struct ttm_bo_driver *driver = bdev->driver;
1704+
void *tmp_obj = NULL, *tmp_obj_read = NULL, *tmp_obj_write = NULL;
1705+
1706+
/* We must unref the sync obj wherever it's ref'd.
1707+
* Note that if we unref bo->sync_obj, we can unref both the read
1708+
* and write sync objs too, because they can't be newer than
1709+
* bo->sync_obj, so they are no longer relevant. */
1710+
if (sync_obj == bo->sync_obj ||
1711+
sync_obj == bo->sync_obj_read) {
1712+
tmp_obj_read = bo->sync_obj_read;
1713+
bo->sync_obj_read = NULL;
1714+
}
1715+
if (sync_obj == bo->sync_obj ||
1716+
sync_obj == bo->sync_obj_write) {
1717+
tmp_obj_write = bo->sync_obj_write;
1718+
bo->sync_obj_write = NULL;
1719+
}
1720+
if (sync_obj == bo->sync_obj) {
1721+
tmp_obj = bo->sync_obj;
1722+
bo->sync_obj = NULL;
1723+
}
1724+
1725+
clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1726+
spin_unlock(&bdev->fence_lock);
1727+
if (tmp_obj)
1728+
driver->sync_obj_unref(&tmp_obj);
1729+
if (tmp_obj_read)
1730+
driver->sync_obj_unref(&tmp_obj_read);
1731+
if (tmp_obj_write)
1732+
driver->sync_obj_unref(&tmp_obj_write);
1733+
if (extra_sync_obj)
1734+
driver->sync_obj_unref(extra_sync_obj);
1735+
spin_lock(&bdev->fence_lock);
1736+
}
1737+
16951738
int ttm_bo_wait(struct ttm_buffer_object *bo,
1696-
bool lazy, bool interruptible, bool no_wait)
1739+
bool lazy, bool interruptible, bool no_wait,
1740+
enum ttm_buffer_usage usage)
16971741
{
16981742
struct ttm_bo_driver *driver = bo->bdev->driver;
16991743
struct ttm_bo_device *bdev = bo->bdev;
17001744
void *sync_obj;
17011745
void *sync_obj_arg;
17021746
int ret = 0;
1747+
void **bo_sync_obj;
17031748

1704-
if (likely(bo->sync_obj == NULL))
1749+
switch (usage) {
1750+
case TTM_USAGE_READ:
1751+
bo_sync_obj = &bo->sync_obj_read;
1752+
break;
1753+
case TTM_USAGE_WRITE:
1754+
bo_sync_obj = &bo->sync_obj_write;
1755+
break;
1756+
case TTM_USAGE_READWRITE:
1757+
default:
1758+
bo_sync_obj = &bo->sync_obj;
1759+
}
1760+
1761+
if (likely(*bo_sync_obj == NULL))
17051762
return 0;
17061763

1707-
while (bo->sync_obj) {
1764+
while (*bo_sync_obj) {
17081765

1709-
if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1710-
void *tmp_obj = bo->sync_obj;
1711-
bo->sync_obj = NULL;
1712-
clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1713-
spin_unlock(&bdev->fence_lock);
1714-
driver->sync_obj_unref(&tmp_obj);
1715-
spin_lock(&bdev->fence_lock);
1766+
if (driver->sync_obj_signaled(*bo_sync_obj, bo->sync_obj_arg)) {
1767+
ttm_bo_unref_sync_obj_locked(bo, *bo_sync_obj, NULL);
17161768
continue;
17171769
}
17181770

17191771
if (no_wait)
17201772
return -EBUSY;
17211773

1722-
sync_obj = driver->sync_obj_ref(bo->sync_obj);
1774+
sync_obj = driver->sync_obj_ref(*bo_sync_obj);
17231775
sync_obj_arg = bo->sync_obj_arg;
17241776
spin_unlock(&bdev->fence_lock);
17251777
ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
@@ -1730,16 +1782,9 @@ int ttm_bo_wait(struct ttm_buffer_object *bo,
17301782
return ret;
17311783
}
17321784
spin_lock(&bdev->fence_lock);
1733-
if (likely(bo->sync_obj == sync_obj &&
1785+
if (likely(*bo_sync_obj == sync_obj &&
17341786
bo->sync_obj_arg == sync_obj_arg)) {
1735-
void *tmp_obj = bo->sync_obj;
1736-
bo->sync_obj = NULL;
1737-
clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1738-
&bo->priv_flags);
1739-
spin_unlock(&bdev->fence_lock);
1740-
driver->sync_obj_unref(&sync_obj);
1741-
driver->sync_obj_unref(&tmp_obj);
1742-
spin_lock(&bdev->fence_lock);
1787+
ttm_bo_unref_sync_obj_locked(bo, *bo_sync_obj, &sync_obj);
17431788
} else {
17441789
spin_unlock(&bdev->fence_lock);
17451790
driver->sync_obj_unref(&sync_obj);
@@ -1763,7 +1808,7 @@ int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
17631808
if (unlikely(ret != 0))
17641809
return ret;
17651810
spin_lock(&bdev->fence_lock);
1766-
ret = ttm_bo_wait(bo, false, true, no_wait);
1811+
ret = ttm_bo_wait(bo, false, true, no_wait, TTM_USAGE_READWRITE);
17671812
spin_unlock(&bdev->fence_lock);
17681813
if (likely(ret == 0))
17691814
atomic_inc(&bo->cpu_writers);
@@ -1837,7 +1882,7 @@ static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
18371882
*/
18381883

18391884
spin_lock(&bo->bdev->fence_lock);
1840-
ret = ttm_bo_wait(bo, false, false, false);
1885+
ret = ttm_bo_wait(bo, false, false, false, TTM_USAGE_READWRITE);
18411886
spin_unlock(&bo->bdev->fence_lock);
18421887

18431888
if (unlikely(ret != 0))

‎drivers/gpu/drm/ttm/ttm_bo_util.c‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
436436
atomic_set(&fbo->cpu_writers, 0);
437437

438438
fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
439+
fbo->sync_obj_read = driver->sync_obj_ref(bo->sync_obj_read);
440+
fbo->sync_obj_write = driver->sync_obj_ref(bo->sync_obj_write);
439441
kref_init(&fbo->list_kref);
440442
kref_init(&fbo->kref);
441443
fbo->destroy = &ttm_transfered_destroy;
@@ -618,20 +620,30 @@ int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
618620
struct ttm_mem_reg *old_mem = &bo->mem;
619621
int ret;
620622
struct ttm_buffer_object *ghost_obj;
621-
void *tmp_obj = NULL;
623+
void *tmp_obj = NULL, *tmp_obj_read = NULL, *tmp_obj_write = NULL;
622624

623625
spin_lock(&bdev->fence_lock);
624-
if (bo->sync_obj) {
626+
if (bo->sync_obj)
625627
tmp_obj = bo->sync_obj;
626-
bo->sync_obj = NULL;
627-
}
628+
if (bo->sync_obj_read)
629+
tmp_obj_read = bo->sync_obj_read;
630+
if (bo->sync_obj_write)
631+
tmp_obj_write = bo->sync_obj_write;
632+
628633
bo->sync_obj = driver->sync_obj_ref(sync_obj);
634+
bo->sync_obj_read = driver->sync_obj_ref(sync_obj);
635+
bo->sync_obj_write = driver->sync_obj_ref(sync_obj);
629636
bo->sync_obj_arg = sync_obj_arg;
630637
if (evict) {
631-
ret = ttm_bo_wait(bo, false, false, false);
638+
ret = ttm_bo_wait(bo, false, false, false,
639+
TTM_USAGE_READWRITE);
632640
spin_unlock(&bdev->fence_lock);
633641
if (tmp_obj)
634642
driver->sync_obj_unref(&tmp_obj);
643+
if (tmp_obj_read)
644+
driver->sync_obj_unref(&tmp_obj_read);
645+
if (tmp_obj_write)
646+
driver->sync_obj_unref(&tmp_obj_write);
635647
if (ret)
636648
return ret;
637649

@@ -655,6 +667,10 @@ int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
655667
spin_unlock(&bdev->fence_lock);
656668
if (tmp_obj)
657669
driver->sync_obj_unref(&tmp_obj);
670+
if (tmp_obj_read)
671+
driver->sync_obj_unref(&tmp_obj_read);
672+
if (tmp_obj_write)
673+
driver->sync_obj_unref(&tmp_obj_write);
658674

659675
ret = ttm_buffer_object_transfer(bo, &ghost_obj);
660676
if (ret)

‎drivers/gpu/drm/ttm/ttm_bo_vm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
122122

123123
spin_lock(&bdev->fence_lock);
124124
if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
125-
ret = ttm_bo_wait(bo, false, true, false);
125+
ret = ttm_bo_wait(bo, false, true, false, TTM_USAGE_READWRITE);
126126
spin_unlock(&bdev->fence_lock);
127127
if (unlikely(ret != 0)) {
128128
retval = (ret != -ERESTARTSYS) ?

‎drivers/gpu/drm/ttm/ttm_execbuf_util.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,18 @@ void ttm_eu_fence_buffer_objects(struct list_head *list, void *sync_obj)
221221

222222
list_for_each_entry(entry, list, head) {
223223
bo = entry->bo;
224+
entry->old_sync_obj_read = NULL;
225+
entry->old_sync_obj_write = NULL;
224226
entry->old_sync_obj = bo->sync_obj;
225227
bo->sync_obj = driver->sync_obj_ref(sync_obj);
228+
if (entry->usage & TTM_USAGE_READ) {
229+
entry->old_sync_obj_read = bo->sync_obj_read;
230+
bo->sync_obj_read = driver->sync_obj_ref(sync_obj);
231+
}
232+
if (entry->usage & TTM_USAGE_WRITE) {
233+
entry->old_sync_obj_write = bo->sync_obj_write;
234+
bo->sync_obj_write = driver->sync_obj_ref(sync_obj);
235+
}
226236
bo->sync_obj_arg = entry->new_sync_obj_arg;
227237
ttm_bo_unreserve_locked(bo);
228238
entry->reserved = false;
@@ -231,8 +241,15 @@ void ttm_eu_fence_buffer_objects(struct list_head *list, void *sync_obj)
231241
spin_unlock(&bdev->fence_lock);
232242

233243
list_for_each_entry(entry, list, head) {
234-
if (entry->old_sync_obj)
244+
if (entry->old_sync_obj) {
235245
driver->sync_obj_unref(&entry->old_sync_obj);
246+
}
247+
if (entry->old_sync_obj_read) {
248+
driver->sync_obj_unref(&entry->old_sync_obj_read);
249+
}
250+
if (entry->old_sync_obj_write) {
251+
driver->sync_obj_unref(&entry->old_sync_obj_write);
252+
}
236253
}
237254
}
238255
EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);

‎drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
224224
if (unlikely(cur_validate_node == sw_context->cur_val_buf)) {
225225
val_buf = &sw_context->val_bufs[cur_validate_node];
226226
val_buf->bo = ttm_bo_reference(bo);
227+
val_buf->usage = TTM_USAGE_READWRITE;
227228
val_buf->new_sync_obj_arg = (void *) dev_priv;
228229
list_add_tail(&val_buf->head, &sw_context->validate_nodes);
229230
++sw_context->cur_val_buf;

‎include/drm/ttm/ttm_bo_api.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ struct ttm_bo_device;
4444

4545
struct drm_mm_node;
4646

47+
enum ttm_buffer_usage {
48+
TTM_USAGE_READ = 1,
49+
TTM_USAGE_WRITE = 2,
50+
TTM_USAGE_READWRITE = TTM_USAGE_READ | TTM_USAGE_WRITE
51+
};
4752

4853
/**
4954
* struct ttm_placement
@@ -174,7 +179,10 @@ struct ttm_tt;
174179
* the bo_device::lru_lock.
175180
* @reserved: Deadlock-free lock used for synchronization state transitions.
176181
* @sync_obj_arg: Opaque argument to synchronization object function.
177-
* @sync_obj: Pointer to a synchronization object.
182+
* @sync_obj: Pointer to a synchronization object of a last read or write,
183+
* whichever is later.
184+
* @sync_obj_read: Pointer to a synchronization object of a last read.
185+
* @sync_obj_write: Pointer to a synchronization object of a last write.
178186
* @priv_flags: Flags describing buffer object internal state.
179187
* @vm_rb: Rb node for the vm rb tree.
180188
* @vm_node: Address space manager node.
@@ -258,6 +266,8 @@ struct ttm_buffer_object {
258266

259267
void *sync_obj_arg;
260268
void *sync_obj;
269+
void *sync_obj_read;
270+
void *sync_obj_write;
261271
unsigned long priv_flags;
262272

263273
/**
@@ -325,6 +335,7 @@ ttm_bo_reference(struct ttm_buffer_object *bo)
325335
* @bo: The buffer object.
326336
* @interruptible: Use interruptible wait.
327337
* @no_wait: Return immediately if buffer is busy.
338+
* @usage: Whether to wait for the last read and/or the last write.
328339
*
329340
* This function must be called with the bo::mutex held, and makes
330341
* sure any previous rendering to the buffer is completed.
@@ -334,7 +345,8 @@ ttm_bo_reference(struct ttm_buffer_object *bo)
334345
* Returns -ERESTARTSYS if interrupted by a signal.
335346
*/
336347
extern int ttm_bo_wait(struct ttm_buffer_object *bo, bool lazy,
337-
bool interruptible, bool no_wait);
348+
bool interruptible, bool no_wait,
349+
enum ttm_buffer_usage usage);
338350
/**
339351
* ttm_bo_validate
340352
*

0 commit comments

Comments
 (0)