Skip to content

Commit d58e3b0

Browse files
committed
drm/vmwgfx: Implement an infrastructure for read-coherent resources
Similar to write-coherent resources, make sure that from the user-space point of view, GPU rendered contents is automatically available for reading by the CPU. Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com> Reviewed-by: Deepak Rawat <drawat@vmware.com>
1 parent 86aeaa0 commit d58e3b0

5 files changed

Lines changed: 177 additions & 11 deletions

File tree

‎drivers/gpu/drm/vmwgfx/vmwgfx_drv.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ extern void vmw_resource_unreference(struct vmw_resource **p_res);
689689
extern struct vmw_resource *vmw_resource_reference(struct vmw_resource *res);
690690
extern struct vmw_resource *
691691
vmw_resource_reference_unless_doomed(struct vmw_resource *res);
692-
extern int vmw_resource_validate(struct vmw_resource *res, bool intr);
692+
extern int vmw_resource_validate(struct vmw_resource *res, bool intr,
693+
bool dirtying);
693694
extern int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
694695
bool no_backup);
695696
extern bool vmw_resource_needs_backup(const struct vmw_resource *res);
@@ -733,6 +734,8 @@ void vmw_resource_mob_attach(struct vmw_resource *res);
733734
void vmw_resource_mob_detach(struct vmw_resource *res);
734735
void vmw_resource_dirty_update(struct vmw_resource *res, pgoff_t start,
735736
pgoff_t end);
737+
int vmw_resources_clean(struct vmw_buffer_object *vbo, pgoff_t start,
738+
pgoff_t end, pgoff_t *num_prefault);
736739

737740
/**
738741
* vmw_resource_mob_attached - Whether a resource currently has a mob attached
@@ -1427,6 +1430,8 @@ int vmw_bo_dirty_add(struct vmw_buffer_object *vbo);
14271430
void vmw_bo_dirty_transfer_to_res(struct vmw_resource *res);
14281431
void vmw_bo_dirty_clear_res(struct vmw_resource *res);
14291432
void vmw_bo_dirty_release(struct vmw_buffer_object *vbo);
1433+
void vmw_bo_dirty_unmap(struct vmw_buffer_object *vbo,
1434+
pgoff_t start, pgoff_t end);
14301435
vm_fault_t vmw_bo_vm_fault(struct vm_fault *vmf);
14311436
vm_fault_t vmw_bo_vm_mkwrite(struct vm_fault *vmf);
14321437

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

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ static void vmw_bo_dirty_scan_mkwrite(struct vmw_buffer_object *vbo)
153153
}
154154
}
155155

156-
157156
/**
158157
* vmw_bo_dirty_scan - Scan for dirty pages and add them to the dirty
159158
* tracking structure
@@ -171,6 +170,51 @@ void vmw_bo_dirty_scan(struct vmw_buffer_object *vbo)
171170
vmw_bo_dirty_scan_mkwrite(vbo);
172171
}
173172

173+
/**
174+
* vmw_bo_dirty_pre_unmap - write-protect and pick up dirty pages before
175+
* an unmap_mapping_range operation.
176+
* @vbo: The buffer object,
177+
* @start: First page of the range within the buffer object.
178+
* @end: Last page of the range within the buffer object + 1.
179+
*
180+
* If we're using the _PAGETABLE scan method, we may leak dirty pages
181+
* when calling unmap_mapping_range(). This function makes sure we pick
182+
* up all dirty pages.
183+
*/
184+
static void vmw_bo_dirty_pre_unmap(struct vmw_buffer_object *vbo,
185+
pgoff_t start, pgoff_t end)
186+
{
187+
struct vmw_bo_dirty *dirty = vbo->dirty;
188+
unsigned long offset = drm_vma_node_start(&vbo->base.vma_node);
189+
struct address_space *mapping = vbo->base.bdev->dev_mapping;
190+
191+
if (dirty->method != VMW_BO_DIRTY_PAGETABLE || start >= end)
192+
return;
193+
194+
apply_as_wrprotect(mapping, start + offset, end - start);
195+
apply_as_clean(mapping, start + offset, end - start, offset,
196+
&dirty->bitmap[0], &dirty->start, &dirty->end);
197+
}
198+
199+
/**
200+
* vmw_bo_dirty_unmap - Clear all ptes pointing to a range within a bo
201+
* @vbo: The buffer object,
202+
* @start: First page of the range within the buffer object.
203+
* @end: Last page of the range within the buffer object + 1.
204+
*
205+
* This is similar to ttm_bo_unmap_virtual_locked() except it takes a subrange.
206+
*/
207+
void vmw_bo_dirty_unmap(struct vmw_buffer_object *vbo,
208+
pgoff_t start, pgoff_t end)
209+
{
210+
unsigned long offset = drm_vma_node_start(&vbo->base.vma_node);
211+
struct address_space *mapping = vbo->base.bdev->dev_mapping;
212+
213+
vmw_bo_dirty_pre_unmap(vbo, start, end);
214+
unmap_shared_mapping_range(mapping, (offset + start) << PAGE_SHIFT,
215+
(loff_t) (end - start) << PAGE_SHIFT);
216+
}
217+
174218
/**
175219
* vmw_bo_dirty_add - Add a dirty-tracking user to a buffer object
176220
* @vbo: The buffer object
@@ -389,21 +433,40 @@ vm_fault_t vmw_bo_vm_fault(struct vm_fault *vmf)
389433
if (ret)
390434
return ret;
391435

436+
num_prefault = (vma->vm_flags & VM_RAND_READ) ? 1 :
437+
TTM_BO_VM_NUM_PREFAULT;
438+
439+
if (vbo->dirty) {
440+
pgoff_t allowed_prefault;
441+
unsigned long page_offset;
442+
443+
page_offset = vmf->pgoff - drm_vma_node_start(&bo->vma_node);
444+
if (page_offset >= bo->num_pages ||
445+
vmw_resources_clean(vbo, page_offset,
446+
page_offset + PAGE_SIZE,
447+
&allowed_prefault)) {
448+
ret = VM_FAULT_SIGBUS;
449+
goto out_unlock;
450+
}
451+
452+
num_prefault = min(num_prefault, allowed_prefault);
453+
}
454+
392455
/*
393-
* This will cause mkwrite() to be called for each pte on
394-
* write-enable vmas.
456+
* If we don't track dirty using the MKWRITE method, make sure
457+
* sure the page protection is write-enabled so we don't get
458+
* a lot of unnecessary write faults.
395459
*/
396460
if (vbo->dirty && vbo->dirty->method == VMW_BO_DIRTY_MKWRITE)
397461
prot = vma->vm_page_prot;
398462
else
399463
prot = vm_get_page_prot(vma->vm_flags);
400464

401-
num_prefault = (vma->vm_flags & VM_RAND_READ) ? 0 :
402-
TTM_BO_VM_NUM_PREFAULT;
403465
ret = ttm_bo_vm_fault_reserved(vmf, prot, num_prefault);
404466
if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
405467
return ret;
406468

469+
out_unlock:
407470
reservation_object_unlock(bo->resv);
408471
return ret;
409472
}

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

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ static int vmw_resource_buf_alloc(struct vmw_resource *res,
395395
* should be retried once resources have been freed up.
396396
*/
397397
static int vmw_resource_do_validate(struct vmw_resource *res,
398-
struct ttm_validate_buffer *val_buf)
398+
struct ttm_validate_buffer *val_buf,
399+
bool dirtying)
399400
{
400401
int ret = 0;
401402
const struct vmw_res_func *func = res->func;
@@ -437,6 +438,15 @@ static int vmw_resource_do_validate(struct vmw_resource *res,
437438
* the resource.
438439
*/
439440
if (res->dirty) {
441+
if (dirtying && !res->res_dirty) {
442+
pgoff_t start = res->backup_offset >> PAGE_SHIFT;
443+
pgoff_t end = __KERNEL_DIV_ROUND_UP
444+
(res->backup_offset + res->backup_size,
445+
PAGE_SIZE);
446+
447+
vmw_bo_dirty_unmap(res->backup, start, end);
448+
}
449+
440450
vmw_bo_dirty_transfer_to_res(res);
441451
return func->dirty_sync(res);
442452
}
@@ -681,6 +691,7 @@ static int vmw_resource_do_evict(struct ww_acquire_ctx *ticket,
681691
* to the device.
682692
* @res: The resource to make visible to the device.
683693
* @intr: Perform waits interruptible if possible.
694+
* @dirtying: Pending GPU operation will dirty the resource
684695
*
685696
* On succesful return, any backup DMA buffer pointed to by @res->backup will
686697
* be reserved and validated.
@@ -690,7 +701,8 @@ static int vmw_resource_do_evict(struct ww_acquire_ctx *ticket,
690701
* Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
691702
* on failure.
692703
*/
693-
int vmw_resource_validate(struct vmw_resource *res, bool intr)
704+
int vmw_resource_validate(struct vmw_resource *res, bool intr,
705+
bool dirtying)
694706
{
695707
int ret;
696708
struct vmw_resource *evict_res;
@@ -707,7 +719,7 @@ int vmw_resource_validate(struct vmw_resource *res, bool intr)
707719
if (res->backup)
708720
val_buf.bo = &res->backup->base;
709721
do {
710-
ret = vmw_resource_do_validate(res, &val_buf);
722+
ret = vmw_resource_do_validate(res, &val_buf, dirtying);
711723
if (likely(ret != -EBUSY))
712724
break;
713725

@@ -1007,7 +1019,7 @@ int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
10071019
/* Do we really need to pin the MOB as well? */
10081020
vmw_bo_pin_reserved(vbo, true);
10091021
}
1010-
ret = vmw_resource_validate(res, interruptible);
1022+
ret = vmw_resource_validate(res, interruptible, true);
10111023
if (vbo)
10121024
ttm_bo_unreserve(&vbo->base);
10131025
if (ret)
@@ -1082,3 +1094,86 @@ void vmw_resource_dirty_update(struct vmw_resource *res, pgoff_t start,
10821094
res->func->dirty_range_add(res, start << PAGE_SHIFT,
10831095
end << PAGE_SHIFT);
10841096
}
1097+
1098+
/**
1099+
* vmw_resources_clean - Clean resources intersecting a mob range
1100+
* @vbo: The mob buffer object
1101+
* @start: The mob page offset starting the range
1102+
* @end: The mob page offset ending the range
1103+
* @num_prefault: Returns how many pages including the first have been
1104+
* cleaned and are ok to prefault
1105+
*/
1106+
int vmw_resources_clean(struct vmw_buffer_object *vbo, pgoff_t start,
1107+
pgoff_t end, pgoff_t *num_prefault)
1108+
{
1109+
struct rb_node *cur = vbo->res_tree.rb_node;
1110+
struct vmw_resource *found = NULL;
1111+
unsigned long res_start = start << PAGE_SHIFT;
1112+
unsigned long res_end = end << PAGE_SHIFT;
1113+
unsigned long last_cleaned = 0;
1114+
1115+
/*
1116+
* Find the resource with lowest backup_offset that intersects the
1117+
* range.
1118+
*/
1119+
while (cur) {
1120+
struct vmw_resource *cur_res =
1121+
container_of(cur, struct vmw_resource, mob_node);
1122+
1123+
if (cur_res->backup_offset >= res_end) {
1124+
cur = cur->rb_left;
1125+
} else if (cur_res->backup_offset + cur_res->backup_size <=
1126+
res_start) {
1127+
cur = cur->rb_right;
1128+
} else {
1129+
found = cur_res;
1130+
cur = cur->rb_left;
1131+
/* Continue to look for resources with lower offsets */
1132+
}
1133+
}
1134+
1135+
/*
1136+
* In order of increasing backup_offset, clean dirty resorces
1137+
* intersecting the range.
1138+
*/
1139+
while (found) {
1140+
if (found->res_dirty) {
1141+
int ret;
1142+
1143+
if (!found->func->clean)
1144+
return -EINVAL;
1145+
1146+
ret = found->func->clean(found);
1147+
if (ret)
1148+
return ret;
1149+
1150+
found->res_dirty = false;
1151+
}
1152+
last_cleaned = found->backup_offset + found->backup_size;
1153+
cur = rb_next(&found->mob_node);
1154+
if (!cur)
1155+
break;
1156+
1157+
found = container_of(cur, struct vmw_resource, mob_node);
1158+
if (found->backup_offset >= res_end)
1159+
break;
1160+
}
1161+
1162+
/*
1163+
* Set number of pages allowed prefaulting and fence the buffer object
1164+
*/
1165+
*num_prefault = 1;
1166+
if (last_cleaned > res_start) {
1167+
struct ttm_buffer_object *bo = &vbo->base;
1168+
1169+
*num_prefault = __KERNEL_DIV_ROUND_UP(last_cleaned - res_start,
1170+
PAGE_SIZE);
1171+
vmw_bo_fence_single(bo, NULL);
1172+
if (bo->moving)
1173+
dma_fence_put(bo->moving);
1174+
bo->moving = dma_fence_get
1175+
(reservation_object_get_excl(bo->resv));
1176+
}
1177+
1178+
return 0;
1179+
}

‎drivers/gpu/drm/vmwgfx/vmwgfx_resource_priv.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct vmw_user_resource_conv {
7777
* @dirty_sync: Upload the dirty mob contents to the resource.
7878
* @dirty_add_range: Add a sequential dirty range to the resource
7979
* dirty tracker.
80+
* @clean: Clean the resource.
8081
*/
8182
struct vmw_res_func {
8283
enum vmw_res_type res_type;
@@ -101,6 +102,7 @@ struct vmw_res_func {
101102
int (*dirty_sync)(struct vmw_resource *res);
102103
void (*dirty_range_add)(struct vmw_resource *res, size_t start,
103104
size_t end);
105+
int (*clean)(struct vmw_resource *res);
104106
};
105107

106108
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
641641
struct vmw_resource *res = val->res;
642642
struct vmw_buffer_object *backup = res->backup;
643643

644-
ret = vmw_resource_validate(res, intr);
644+
ret = vmw_resource_validate(res, intr, val->dirty_set &&
645+
val->dirty);
645646
if (ret) {
646647
if (ret != -ERESTARTSYS)
647648
DRM_ERROR("Failed to validate resource.\n");

0 commit comments

Comments
 (0)