@@ -256,7 +256,7 @@ static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
256256 val_buf = & sw_context -> val_bufs [cur_validate_node ];
257257 val_buf -> bo = ttm_bo_reference (bo );
258258 val_buf -> usage = TTM_USAGE_READWRITE ;
259- val_buf -> new_sync_obj_arg = (void * ) dev_priv ;
259+ val_buf -> new_sync_obj_arg = (void * ) DRM_VMW_FENCE_FLAG_EXEC ;
260260 list_add_tail (& val_buf -> head , & sw_context -> validate_nodes );
261261 ++ sw_context -> cur_val_buf ;
262262 }
@@ -321,7 +321,6 @@ static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
321321 return 0 ;
322322}
323323
324-
325324static int vmw_cmd_dma (struct vmw_private * dev_priv ,
326325 struct vmw_sw_context * sw_context ,
327326 SVGA3dCmdHeader * header )
@@ -676,6 +675,50 @@ static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
676675 return 0 ;
677676}
678677
678+ /**
679+ * vmw_execbuf_fence_commands - create and submit a command stream fence
680+ *
681+ * Creates a fence object and submits a command stream marker.
682+ * If this fails for some reason, We sync the fifo and return NULL.
683+ * It is then safe to fence buffers with a NULL pointer.
684+ */
685+
686+ int vmw_execbuf_fence_commands (struct drm_file * file_priv ,
687+ struct vmw_private * dev_priv ,
688+ struct vmw_fence_obj * * p_fence ,
689+ uint32_t * p_handle )
690+ {
691+ uint32_t sequence ;
692+ int ret ;
693+ bool synced = false;
694+
695+
696+ ret = vmw_fifo_send_fence (dev_priv , & sequence );
697+ if (unlikely (ret != 0 )) {
698+ DRM_ERROR ("Fence submission error. Syncing.\n" );
699+ synced = true;
700+ }
701+
702+ if (p_handle != NULL )
703+ ret = vmw_user_fence_create (file_priv , dev_priv -> fman ,
704+ sequence ,
705+ DRM_VMW_FENCE_FLAG_EXEC ,
706+ p_fence , p_handle );
707+ else
708+ ret = vmw_fence_create (dev_priv -> fman , sequence ,
709+ DRM_VMW_FENCE_FLAG_EXEC ,
710+ p_fence );
711+
712+ if (unlikely (ret != 0 && !synced )) {
713+ (void ) vmw_fallback_wait (dev_priv , false, false,
714+ sequence , false,
715+ VMW_FENCE_WAIT_TIMEOUT );
716+ * p_fence = NULL ;
717+ }
718+
719+ return 0 ;
720+ }
721+
679722int vmw_execbuf_ioctl (struct drm_device * dev , void * data ,
680723 struct drm_file * file_priv )
681724{
@@ -686,9 +729,10 @@ int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
686729 int ret ;
687730 void * user_cmd ;
688731 void * cmd ;
689- uint32_t seqno ;
690732 struct vmw_sw_context * sw_context = & dev_priv -> ctx ;
691733 struct vmw_master * vmaster = vmw_master (file_priv -> master );
734+ struct vmw_fence_obj * fence ;
735+ uint32_t handle ;
692736
693737 ret = ttm_read_lock (& vmaster -> lock , true);
694738 if (unlikely (ret != 0 ))
@@ -755,34 +799,60 @@ int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
755799 memcpy (cmd , sw_context -> cmd_bounce , arg -> command_size );
756800 vmw_fifo_commit (dev_priv , arg -> command_size );
757801
758- ret = vmw_fifo_send_fence (dev_priv , & seqno );
759-
760- ttm_eu_fence_buffer_objects (& sw_context -> validate_nodes ,
761- (void * )(unsigned long ) seqno );
762- vmw_clear_validations (sw_context );
763- mutex_unlock (& dev_priv -> cmdbuf_mutex );
764-
802+ user_fence_rep = (struct drm_vmw_fence_rep __user * )
803+ (unsigned long )arg -> fence_rep ;
804+ ret = vmw_execbuf_fence_commands (file_priv , dev_priv ,
805+ & fence ,
806+ (user_fence_rep ) ? & handle : NULL );
765807 /*
766808 * This error is harmless, because if fence submission fails,
767- * vmw_fifo_send_fence will sync.
809+ * vmw_fifo_send_fence will sync. The error will be propagated to
810+ * user-space in @fence_rep
768811 */
769812
770813 if (ret != 0 )
771814 DRM_ERROR ("Fence submission error. Syncing.\n" );
772815
773- fence_rep .error = ret ;
774- fence_rep .fence_seq = (uint64_t ) seqno ;
775- fence_rep .pad64 = 0 ;
816+ ttm_eu_fence_buffer_objects (& sw_context -> validate_nodes ,
817+ (void * ) fence );
776818
777- user_fence_rep = ( struct drm_vmw_fence_rep __user * )
778- ( unsigned long ) arg -> fence_rep ;
819+ vmw_clear_validations ( sw_context );
820+ mutex_unlock ( & dev_priv -> cmdbuf_mutex ) ;
779821
780- /*
781- * copy_to_user errors will be detected by user space not
782- * seeing fence_rep::error filled in.
783- */
822+ if (user_fence_rep ) {
823+ fence_rep .error = ret ;
824+ fence_rep .handle = handle ;
825+ fence_rep .seqno = fence -> seqno ;
826+ vmw_update_seqno (dev_priv , & dev_priv -> fifo );
827+ fence_rep .passed_seqno = dev_priv -> last_read_seqno ;
828+
829+ /*
830+ * copy_to_user errors will be detected by user space not
831+ * seeing fence_rep::error filled in. Typically
832+ * user-space would have pre-set that member to -EFAULT.
833+ */
834+ ret = copy_to_user (user_fence_rep , & fence_rep ,
835+ sizeof (fence_rep ));
836+
837+ /*
838+ * User-space lost the fence object. We need to sync
839+ * and unreference the handle.
840+ */
841+ if (unlikely (ret != 0 ) && (fence_rep .error == 0 )) {
842+ BUG_ON (fence == NULL );
843+
844+ ttm_ref_object_base_unref (vmw_fpriv (file_priv )-> tfile ,
845+ handle , TTM_REF_USAGE );
846+ DRM_ERROR ("Fence copy error. Syncing.\n" );
847+ (void ) vmw_fence_obj_wait (fence ,
848+ fence -> signal_mask ,
849+ false, false,
850+ VMW_FENCE_WAIT_TIMEOUT );
851+ }
852+ }
784853
785- ret = copy_to_user (user_fence_rep , & fence_rep , sizeof (fence_rep ));
854+ if (likely (fence != NULL ))
855+ vmw_fence_obj_unreference (& fence );
786856
787857 vmw_kms_cursor_post_execbuf (dev_priv );
788858 ttm_read_unlock (& vmaster -> lock );
0 commit comments