@@ -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 */
397397static 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+ }
0 commit comments