drm/vmwgfx: Persistent tracking of context bindings
authorThomas Hellstrom <thellstrom@vmware.com>
Tue, 8 Oct 2013 09:32:36 +0000 (02:32 -0700)
committerThomas Hellstrom <thellstrom@vmware.com>
Fri, 17 Jan 2014 06:52:36 +0000 (07:52 +0100)
Only scrub context bindings when a bound resource is destroyed, or when
the MOB backing the context is unbound.

Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>
Reviewed-by: Zack Rusin <zackr@vmware.com>
drivers/gpu/drm/vmwgfx/vmwgfx_context.c
drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c
drivers/gpu/drm/vmwgfx/vmwgfx_shader.c
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c

index b4de756112d416d9af90adb0a12ad0ab1e41fa19..97aa55159107f790ce3322b129f50a3d2588f1fd 100644 (file)
@@ -32,6 +32,7 @@
 struct vmw_user_context {
        struct ttm_base_object base;
        struct vmw_resource res;
+       struct vmw_ctx_binding_state cbs;
 };
 
 
@@ -52,7 +53,7 @@ static int vmw_gb_context_destroy(struct vmw_resource *res);
 static int vmw_context_scrub_shader(struct vmw_ctx_bindinfo *bi);
 static int vmw_context_scrub_render_target(struct vmw_ctx_bindinfo *bi);
 static int vmw_context_scrub_texture(struct vmw_ctx_bindinfo *bi);
-
+static void vmw_context_binding_state_kill(struct vmw_ctx_binding_state *cbs);
 static uint64_t vmw_user_context_size;
 
 static const struct vmw_user_resource_conv user_context_conv = {
@@ -139,6 +140,8 @@ static int vmw_gb_context_init(struct vmw_private *dev_priv,
                               void (*res_free) (struct vmw_resource *res))
 {
        int ret;
+       struct vmw_user_context *uctx =
+               container_of(res, struct vmw_user_context, res);
 
        ret = vmw_resource_init(dev_priv, res, true,
                                res_free, &vmw_gb_context_func);
@@ -152,6 +155,9 @@ static int vmw_gb_context_init(struct vmw_private *dev_priv,
                return ret;
        }
 
+       memset(&uctx->cbs, 0, sizeof(uctx->cbs));
+       INIT_LIST_HEAD(&uctx->cbs.list);
+
        vmw_resource_activate(res, vmw_hw_context_destroy);
        return 0;
 }
@@ -304,6 +310,8 @@ static int vmw_gb_context_unbind(struct vmw_resource *res,
        struct vmw_private *dev_priv = res->dev_priv;
        struct ttm_buffer_object *bo = val_buf->bo;
        struct vmw_fence_obj *fence;
+       struct vmw_user_context *uctx =
+               container_of(res, struct vmw_user_context, res);
 
        struct {
                SVGA3dCmdHeader header;
@@ -319,12 +327,16 @@ static int vmw_gb_context_unbind(struct vmw_resource *res,
 
        BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
 
+       mutex_lock(&dev_priv->binding_mutex);
+       vmw_context_binding_state_kill(&uctx->cbs);
+
        submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
 
        cmd = vmw_fifo_reserve(dev_priv, submit_size);
        if (unlikely(cmd == NULL)) {
                DRM_ERROR("Failed reserving FIFO space for context "
                          "unbinding.\n");
+               mutex_unlock(&dev_priv->binding_mutex);
                return -ENOMEM;
        }
 
@@ -342,6 +354,7 @@ static int vmw_gb_context_unbind(struct vmw_resource *res,
        cmd2->body.mobid = SVGA3D_INVALID_ID;
 
        vmw_fifo_commit(dev_priv, submit_size);
+       mutex_unlock(&dev_priv->binding_mutex);
 
        /*
         * Create a fence object and fence the backup buffer.
@@ -365,6 +378,10 @@ static int vmw_gb_context_destroy(struct vmw_resource *res)
                SVGA3dCmdHeader header;
                SVGA3dCmdDestroyGBContext body;
        } *cmd;
+       struct vmw_user_context *uctx =
+               container_of(res, struct vmw_user_context, res);
+
+       BUG_ON(!list_empty(&uctx->cbs.list));
 
        if (likely(res->id == -1))
                return 0;
@@ -620,6 +637,8 @@ static int vmw_context_scrub_texture(struct vmw_ctx_bindinfo *bi)
 static void vmw_context_binding_drop(struct vmw_ctx_binding *cb)
 {
        list_del(&cb->ctx_list);
+       if (!list_empty(&cb->res_list))
+               list_del(&cb->res_list);
        cb->bi.ctx = NULL;
 }
 
@@ -674,10 +693,48 @@ int vmw_context_binding_add(struct vmw_ctx_binding_state *cbs,
 
        loc->bi = *bi;
        list_add_tail(&loc->ctx_list, &cbs->list);
+       INIT_LIST_HEAD(&loc->res_list);
 
        return 0;
 }
 
+/**
+ * vmw_context_binding_transfer: Transfer a context binding tracking entry.
+ *
+ * @cbs: Pointer to the persistent context binding state tracker.
+ * @bi: Information about the binding to track.
+ *
+ */
+static void vmw_context_binding_transfer(struct vmw_ctx_binding_state *cbs,
+                                        const struct vmw_ctx_bindinfo *bi)
+{
+       struct vmw_ctx_binding *loc;
+
+       switch (bi->bt) {
+       case vmw_ctx_binding_rt:
+               loc = &cbs->render_targets[bi->i1.rt_type];
+               break;
+       case vmw_ctx_binding_tex:
+               loc = &cbs->texture_units[bi->i1.texture_stage];
+               break;
+       case vmw_ctx_binding_shader:
+               loc = &cbs->shaders[bi->i1.shader_type];
+               break;
+       default:
+               BUG();
+       }
+
+       if (loc->bi.ctx != NULL)
+               vmw_context_binding_drop(loc);
+
+       loc->bi = *bi;
+       list_add_tail(&loc->ctx_list, &cbs->list);
+       if (bi->res != NULL)
+               list_add_tail(&loc->res_list, &bi->res->binding_head);
+       else
+               INIT_LIST_HEAD(&loc->res_list);
+}
+
 /**
  * vmw_context_binding_kill - Kill a binding on the device
  * and stop tracking it.
@@ -702,11 +759,47 @@ void vmw_context_binding_kill(struct vmw_ctx_binding *cb)
  * Emits commands to scrub all bindings associated with the
  * context binding state tracker. Then re-initializes the whole structure.
  */
-void vmw_context_binding_state_kill(struct vmw_ctx_binding_state *cbs)
+static void vmw_context_binding_state_kill(struct vmw_ctx_binding_state *cbs)
 {
        struct vmw_ctx_binding *entry, *next;
 
-       list_for_each_entry_safe(entry, next, &cbs->list, ctx_list) {
+       list_for_each_entry_safe(entry, next, &cbs->list, ctx_list)
                vmw_context_binding_kill(entry);
-       }
+}
+
+/**
+ * vmw_context_binding_res_list_kill - Kill all bindings on a
+ * resource binding list
+ *
+ * @head: list head of resource binding list
+ *
+ * Kills all bindings associated with a specific resource. Typically
+ * called before the resource is destroyed.
+ */
+void vmw_context_binding_res_list_kill(struct list_head *head)
+{
+       struct vmw_ctx_binding *entry, *next;
+
+       list_for_each_entry_safe(entry, next, head, res_list)
+               vmw_context_binding_kill(entry);
+}
+
+/**
+ * vmw_context_binding_state_transfer - Commit staged binding info
+ *
+ * @ctx: Pointer to context to commit the staged binding info to.
+ * @from: Staged binding info built during execbuf.
+ *
+ * Transfers binding info from a temporary structure to the persistent
+ * structure in the context. This can be done once commands
+ */
+void vmw_context_binding_state_transfer(struct vmw_resource *ctx,
+                                       struct vmw_ctx_binding_state *from)
+{
+       struct vmw_user_context *uctx =
+               container_of(ctx, struct vmw_user_context, res);
+       struct vmw_ctx_binding *entry, *next;
+
+       list_for_each_entry_safe(entry, next, &from->list, ctx_list)
+               vmw_context_binding_transfer(&uctx->cbs, &entry->bi);
 }
index fb56676ed3eeb3fbb58412a69a20cd19e841ed74..9008a56e2a971e6c6827933fb193aeff9c023c15 100644 (file)
@@ -612,6 +612,7 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
        mutex_init(&dev_priv->hw_mutex);
        mutex_init(&dev_priv->cmdbuf_mutex);
        mutex_init(&dev_priv->release_mutex);
+       mutex_init(&dev_priv->binding_mutex);
        rwlock_init(&dev_priv->resource_lock);
 
        for (i = vmw_res_context; i < vmw_res_max; ++i) {
index a962e4c12a75e182f83ed5adc6884c981872562c..18ece4f53c42ace3152cbb2016e229d911d983ab 100644 (file)
@@ -112,6 +112,7 @@ struct vmw_resource {
        const struct vmw_res_func *func;
        struct list_head lru_head; /* Protected by the resource lock */
        struct list_head mob_head; /* Protected by @backup reserved */
+       struct list_head binding_head; /* Protected by binding_mutex */
        void (*res_free) (struct vmw_resource *res);
        void (*hw_destroy) (struct vmw_resource *res);
 };
@@ -260,11 +261,13 @@ enum vmw_ctx_binding_type {
  *
  * @ctx: Pointer to the context structure. NULL means the binding is not
  * active.
+ * @res: Non ref-counted pointer to the bound resource.
  * @bt: The binding type.
  * @i1: Union of information needed to unbind.
  */
 struct vmw_ctx_bindinfo {
        struct vmw_resource *ctx;
+       struct vmw_resource *res;
        enum vmw_ctx_binding_type bt;
        union {
                SVGA3dShaderType shader_type;
@@ -278,10 +281,12 @@ struct vmw_ctx_bindinfo {
  *                        - suitable for tracking in a context
  *
  * @ctx_list: List head for context.
+ * @res_list: List head for bound resource.
  * @bi: Binding info
  */
 struct vmw_ctx_binding {
        struct list_head ctx_list;
+       struct list_head res_list;
        struct vmw_ctx_bindinfo bi;
 };
 
@@ -450,6 +455,7 @@ struct vmw_private {
 
        struct vmw_sw_context ctx;
        struct mutex cmdbuf_mutex;
+       struct mutex binding_mutex;
 
        /**
         * Operating mode.
@@ -940,7 +946,10 @@ extern int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
                                     struct drm_file *file_priv);
 extern int vmw_context_binding_add(struct vmw_ctx_binding_state *cbs,
                                   const struct vmw_ctx_bindinfo *ci);
-extern void vmw_context_binding_state_kill(struct vmw_ctx_binding_state *cbs);
+extern void
+vmw_context_binding_state_transfer(struct vmw_resource *res,
+                                  struct vmw_ctx_binding_state *cbs);
+extern void vmw_context_binding_res_list_kill(struct list_head *head);
 
 /*
  * Surface management - vmwgfx_surface.c
index 8eb87d855781cd00487fe5a4d8e5c4fd0c453623..b924fd6e6edd1f349f6f93ed18c67622f3baab14 100644 (file)
@@ -109,8 +109,13 @@ static void vmw_resource_list_unreserve(struct list_head *list,
                struct vmw_dma_buffer *new_backup =
                        backoff ? NULL : val->new_backup;
 
+               /*
+                * Transfer staged context bindings to the
+                * persistent context binding tracker.
+                */
                if (unlikely(val->staged_bindings)) {
-                       vmw_context_binding_state_kill(val->staged_bindings);
+                       vmw_context_binding_state_transfer
+                               (val->res, val->staged_bindings);
                        kfree(val->staged_bindings);
                        val->staged_bindings = NULL;
                }
@@ -508,6 +513,7 @@ static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
                SVGA3dCmdSetRenderTarget body;
        } *cmd;
        struct vmw_resource_val_node *ctx_node;
+       struct vmw_resource_val_node *res_node;
        int ret;
 
        cmd = container_of(header, struct vmw_sid_cmd, header);
@@ -520,7 +526,7 @@ static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
 
        ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
                                user_surface_converter,
-                               &cmd->body.target.sid, NULL);
+                               &cmd->body.target.sid, &res_node);
        if (unlikely(ret != 0))
                return ret;
 
@@ -528,6 +534,7 @@ static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
                struct vmw_ctx_bindinfo bi;
 
                bi.ctx = ctx_node->res;
+               bi.res = res_node ? res_node->res : NULL;
                bi.bt = vmw_ctx_binding_rt;
                bi.i1.rt_type = cmd->body.type;
                return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
@@ -1195,6 +1202,7 @@ static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
        SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
                ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
        struct vmw_resource_val_node *ctx_node;
+       struct vmw_resource_val_node *res_node;
        int ret;
 
        cmd = container_of(header, struct vmw_tex_state_cmd,
@@ -1212,7 +1220,7 @@ static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
 
                ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
                                        user_surface_converter,
-                                       &cur_state->value, NULL);
+                                       &cur_state->value, &res_node);
                if (unlikely(ret != 0))
                        return ret;
 
@@ -1220,6 +1228,7 @@ static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
                        struct vmw_ctx_bindinfo bi;
 
                        bi.ctx = ctx_node->res;
+                       bi.res = res_node ? res_node->res : NULL;
                        bi.bt = vmw_ctx_binding_tex;
                        bi.i1.texture_stage = cur_state->stage;
                        vmw_context_binding_add(ctx_node->staged_bindings,
@@ -1499,14 +1508,16 @@ static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
 
        if (dev_priv->has_mob) {
                struct vmw_ctx_bindinfo bi;
+               struct vmw_resource_val_node *res_node;
 
                ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_shader,
                                        user_shader_converter,
-                                       &cmd->body.shid, NULL);
+                                       &cmd->body.shid, &res_node);
                if (unlikely(ret != 0))
                        return ret;
 
                bi.ctx = ctx_node->res;
+               bi.res = res_node ? res_node->res : NULL;
                bi.bt = vmw_ctx_binding_shader;
                bi.i1.shader_type = cmd->body.type;
                return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
@@ -2208,11 +2219,17 @@ int vmw_execbuf_process(struct drm_file *file_priv,
                        goto out_err;
        }
 
+       ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
+       if (unlikely(ret != 0)) {
+               ret = -ERESTARTSYS;
+               goto out_err;
+       }
+
        cmd = vmw_fifo_reserve(dev_priv, command_size);
        if (unlikely(cmd == NULL)) {
                DRM_ERROR("Failed reserving fifo space for commands.\n");
                ret = -ENOMEM;
-               goto out_err;
+               goto out_unlock_binding;
        }
 
        vmw_apply_relocations(sw_context);
@@ -2237,6 +2254,8 @@ int vmw_execbuf_process(struct drm_file *file_priv,
                DRM_ERROR("Fence submission error. Syncing.\n");
 
        vmw_resource_list_unreserve(&sw_context->resource_list, false);
+       mutex_unlock(&dev_priv->binding_mutex);
+
        ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
                                    (void *) fence);
 
@@ -2267,6 +2286,8 @@ int vmw_execbuf_process(struct drm_file *file_priv,
 
        return 0;
 
+out_unlock_binding:
+       mutex_unlock(&dev_priv->binding_mutex);
 out_err:
        vmw_resource_relocations_free(&sw_context->res_relocations);
        vmw_free_relocations(sw_context);
index 12e68e58d9e4b85474f78101a7f263172bc8f7f7..6fdd82d42f6549d2af208516ad22b26d45785e28 100644 (file)
@@ -215,6 +215,7 @@ int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
        res->func = func;
        INIT_LIST_HEAD(&res->lru_head);
        INIT_LIST_HEAD(&res->mob_head);
+       INIT_LIST_HEAD(&res->binding_head);
        res->id = -1;
        res->backup = NULL;
        res->backup_offset = 0;
index 76d3541014033d1cc297750fd7e0f125dfbe5c1f..813bd0a2abafb919c5353cc428474ee668f6c8cc 100644 (file)
@@ -257,6 +257,9 @@ static int vmw_gb_shader_destroy(struct vmw_resource *res)
        if (likely(res->id == -1))
                return 0;
 
+       mutex_lock(&dev_priv->binding_mutex);
+       vmw_context_binding_res_list_kill(&res->binding_head);
+
        cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
        if (unlikely(cmd == NULL)) {
                DRM_ERROR("Failed reserving FIFO space for shader "
@@ -268,6 +271,7 @@ static int vmw_gb_shader_destroy(struct vmw_resource *res)
        cmd->header.size = sizeof(cmd->body);
        cmd->body.shid = res->id;
        vmw_fifo_commit(dev_priv, sizeof(*cmd));
+       mutex_unlock(&dev_priv->binding_mutex);
        vmw_resource_release_id(res);
        vmw_3d_resource_dec(dev_priv, false);
 
index 739a93dc941e1a9a535a6f4a95f1dcbe1db46d85..a729b20ee14d635aa8d8325bc14fb33f414bcb99 100644 (file)
@@ -1100,6 +1100,9 @@ static int vmw_gb_surface_destroy(struct vmw_resource *res)
        if (likely(res->id == -1))
                return 0;
 
+       mutex_lock(&dev_priv->binding_mutex);
+       vmw_context_binding_res_list_kill(&res->binding_head);
+
        cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
        if (unlikely(cmd == NULL)) {
                DRM_ERROR("Failed reserving FIFO space for surface "
@@ -1111,6 +1114,7 @@ static int vmw_gb_surface_destroy(struct vmw_resource *res)
        cmd->header.size = sizeof(cmd->body);
        cmd->body.sid = res->id;
        vmw_fifo_commit(dev_priv, sizeof(*cmd));
+       mutex_unlock(&dev_priv->binding_mutex);
        vmw_resource_release_id(res);
        vmw_3d_resource_dec(dev_priv, false);