Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 173fb7d4 authored by Thomas Hellstrom's avatar Thomas Hellstrom
Browse files

drm/vmwgfx: Persistent tracking of context bindings



Only scrub context bindings when a bound resource is destroyed, or when
the MOB backing the context is unbound.

Signed-off-by: default avatarThomas Hellstrom <thellstrom@vmware.com>
Reviewed-by: default avatarZack Rusin <zackr@vmware.com>
parent b5c3b1a6
Loading
Loading
Loading
Loading
+97 −4
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@
struct vmw_user_context {
struct vmw_user_context {
	struct ttm_base_object base;
	struct ttm_base_object base;
	struct vmw_resource res;
	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_shader(struct vmw_ctx_bindinfo *bi);
static int vmw_context_scrub_render_target(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 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 uint64_t vmw_user_context_size;


static const struct vmw_user_resource_conv user_context_conv = {
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))
			       void (*res_free) (struct vmw_resource *res))
{
{
	int ret;
	int ret;
	struct vmw_user_context *uctx =
		container_of(res, struct vmw_user_context, res);


	ret = vmw_resource_init(dev_priv, res, true,
	ret = vmw_resource_init(dev_priv, res, true,
				res_free, &vmw_gb_context_func);
				res_free, &vmw_gb_context_func);
@@ -152,6 +155,9 @@ static int vmw_gb_context_init(struct vmw_private *dev_priv,
		return ret;
		return ret;
	}
	}


	memset(&uctx->cbs, 0, sizeof(uctx->cbs));
	INIT_LIST_HEAD(&uctx->cbs.list);

	vmw_resource_activate(res, vmw_hw_context_destroy);
	vmw_resource_activate(res, vmw_hw_context_destroy);
	return 0;
	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 vmw_private *dev_priv = res->dev_priv;
	struct ttm_buffer_object *bo = val_buf->bo;
	struct ttm_buffer_object *bo = val_buf->bo;
	struct vmw_fence_obj *fence;
	struct vmw_fence_obj *fence;
	struct vmw_user_context *uctx =
		container_of(res, struct vmw_user_context, res);


	struct {
	struct {
		SVGA3dCmdHeader header;
		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);
	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);
	submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);


	cmd = vmw_fifo_reserve(dev_priv, submit_size);
	cmd = vmw_fifo_reserve(dev_priv, submit_size);
	if (unlikely(cmd == NULL)) {
	if (unlikely(cmd == NULL)) {
		DRM_ERROR("Failed reserving FIFO space for context "
		DRM_ERROR("Failed reserving FIFO space for context "
			  "unbinding.\n");
			  "unbinding.\n");
		mutex_unlock(&dev_priv->binding_mutex);
		return -ENOMEM;
		return -ENOMEM;
	}
	}


@@ -342,6 +354,7 @@ static int vmw_gb_context_unbind(struct vmw_resource *res,
	cmd2->body.mobid = SVGA3D_INVALID_ID;
	cmd2->body.mobid = SVGA3D_INVALID_ID;


	vmw_fifo_commit(dev_priv, submit_size);
	vmw_fifo_commit(dev_priv, submit_size);
	mutex_unlock(&dev_priv->binding_mutex);


	/*
	/*
	 * Create a fence object and fence the backup buffer.
	 * 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;
		SVGA3dCmdHeader header;
		SVGA3dCmdDestroyGBContext body;
		SVGA3dCmdDestroyGBContext body;
	} *cmd;
	} *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))
	if (likely(res->id == -1))
		return 0;
		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)
static void vmw_context_binding_drop(struct vmw_ctx_binding *cb)
{
{
	list_del(&cb->ctx_list);
	list_del(&cb->ctx_list);
	if (!list_empty(&cb->res_list))
		list_del(&cb->res_list);
	cb->bi.ctx = NULL;
	cb->bi.ctx = NULL;
}
}


@@ -674,10 +693,48 @@ int vmw_context_binding_add(struct vmw_ctx_binding_state *cbs,


	loc->bi = *bi;
	loc->bi = *bi;
	list_add_tail(&loc->ctx_list, &cbs->list);
	list_add_tail(&loc->ctx_list, &cbs->list);
	INIT_LIST_HEAD(&loc->res_list);


	return 0;
	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
 * vmw_context_binding_kill - Kill a binding on the device
 * and stop tracking it.
 * 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
 * Emits commands to scrub all bindings associated with the
 * context binding state tracker. Then re-initializes the whole structure.
 * 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)
		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;
	struct vmw_ctx_binding *entry, *next;


	list_for_each_entry_safe(entry, next, &cbs->list, ctx_list) {
	list_for_each_entry_safe(entry, next, head, res_list)
		vmw_context_binding_kill(entry);
		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);
}
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -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->hw_mutex);
	mutex_init(&dev_priv->cmdbuf_mutex);
	mutex_init(&dev_priv->cmdbuf_mutex);
	mutex_init(&dev_priv->release_mutex);
	mutex_init(&dev_priv->release_mutex);
	mutex_init(&dev_priv->binding_mutex);
	rwlock_init(&dev_priv->resource_lock);
	rwlock_init(&dev_priv->resource_lock);


	for (i = vmw_res_context; i < vmw_res_max; ++i) {
	for (i = vmw_res_context; i < vmw_res_max; ++i) {
+10 −1
Original line number Original line Diff line number Diff line
@@ -112,6 +112,7 @@ struct vmw_resource {
	const struct vmw_res_func *func;
	const struct vmw_res_func *func;
	struct list_head lru_head; /* Protected by the resource lock */
	struct list_head lru_head; /* Protected by the resource lock */
	struct list_head mob_head; /* Protected by @backup reserved */
	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 (*res_free) (struct vmw_resource *res);
	void (*hw_destroy) (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
 * @ctx: Pointer to the context structure. NULL means the binding is not
 * active.
 * active.
 * @res: Non ref-counted pointer to the bound resource.
 * @bt: The binding type.
 * @bt: The binding type.
 * @i1: Union of information needed to unbind.
 * @i1: Union of information needed to unbind.
 */
 */
struct vmw_ctx_bindinfo {
struct vmw_ctx_bindinfo {
	struct vmw_resource *ctx;
	struct vmw_resource *ctx;
	struct vmw_resource *res;
	enum vmw_ctx_binding_type bt;
	enum vmw_ctx_binding_type bt;
	union {
	union {
		SVGA3dShaderType shader_type;
		SVGA3dShaderType shader_type;
@@ -278,10 +281,12 @@ struct vmw_ctx_bindinfo {
 *                        - suitable for tracking in a context
 *                        - suitable for tracking in a context
 *
 *
 * @ctx_list: List head for context.
 * @ctx_list: List head for context.
 * @res_list: List head for bound resource.
 * @bi: Binding info
 * @bi: Binding info
 */
 */
struct vmw_ctx_binding {
struct vmw_ctx_binding {
	struct list_head ctx_list;
	struct list_head ctx_list;
	struct list_head res_list;
	struct vmw_ctx_bindinfo bi;
	struct vmw_ctx_bindinfo bi;
};
};


@@ -450,6 +455,7 @@ struct vmw_private {


	struct vmw_sw_context ctx;
	struct vmw_sw_context ctx;
	struct mutex cmdbuf_mutex;
	struct mutex cmdbuf_mutex;
	struct mutex binding_mutex;


	/**
	/**
	 * Operating mode.
	 * Operating mode.
@@ -940,7 +946,10 @@ extern int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
				     struct drm_file *file_priv);
				     struct drm_file *file_priv);
extern int vmw_context_binding_add(struct vmw_ctx_binding_state *cbs,
extern int vmw_context_binding_add(struct vmw_ctx_binding_state *cbs,
				   const struct vmw_ctx_bindinfo *ci);
				   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
 * Surface management - vmwgfx_surface.c
+26 −5
Original line number Original line Diff line number Diff line
@@ -109,8 +109,13 @@ static void vmw_resource_list_unreserve(struct list_head *list,
		struct vmw_dma_buffer *new_backup =
		struct vmw_dma_buffer *new_backup =
			backoff ? NULL : val->new_backup;
			backoff ? NULL : val->new_backup;


		/*
		 * Transfer staged context bindings to the
		 * persistent context binding tracker.
		 */
		if (unlikely(val->staged_bindings)) {
		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);
			kfree(val->staged_bindings);
			val->staged_bindings = NULL;
			val->staged_bindings = NULL;
		}
		}
@@ -508,6 +513,7 @@ static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
		SVGA3dCmdSetRenderTarget body;
		SVGA3dCmdSetRenderTarget body;
	} *cmd;
	} *cmd;
	struct vmw_resource_val_node *ctx_node;
	struct vmw_resource_val_node *ctx_node;
	struct vmw_resource_val_node *res_node;
	int ret;
	int ret;


	cmd = container_of(header, struct vmw_sid_cmd, header);
	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,
	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
				user_surface_converter,
				user_surface_converter,
				&cmd->body.target.sid, NULL);
				&cmd->body.target.sid, &res_node);
	if (unlikely(ret != 0))
	if (unlikely(ret != 0))
		return ret;
		return ret;


@@ -528,6 +534,7 @@ static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
		struct vmw_ctx_bindinfo bi;
		struct vmw_ctx_bindinfo bi;


		bi.ctx = ctx_node->res;
		bi.ctx = ctx_node->res;
		bi.res = res_node ? res_node->res : NULL;
		bi.bt = vmw_ctx_binding_rt;
		bi.bt = vmw_ctx_binding_rt;
		bi.i1.rt_type = cmd->body.type;
		bi.i1.rt_type = cmd->body.type;
		return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
		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 *)
	SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
		((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
		((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
	struct vmw_resource_val_node *ctx_node;
	struct vmw_resource_val_node *ctx_node;
	struct vmw_resource_val_node *res_node;
	int ret;
	int ret;


	cmd = container_of(header, struct vmw_tex_state_cmd,
	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,
		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
					user_surface_converter,
					user_surface_converter,
					&cur_state->value, NULL);
					&cur_state->value, &res_node);
		if (unlikely(ret != 0))
		if (unlikely(ret != 0))
			return ret;
			return ret;


@@ -1220,6 +1228,7 @@ static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
			struct vmw_ctx_bindinfo bi;
			struct vmw_ctx_bindinfo bi;


			bi.ctx = ctx_node->res;
			bi.ctx = ctx_node->res;
			bi.res = res_node ? res_node->res : NULL;
			bi.bt = vmw_ctx_binding_tex;
			bi.bt = vmw_ctx_binding_tex;
			bi.i1.texture_stage = cur_state->stage;
			bi.i1.texture_stage = cur_state->stage;
			vmw_context_binding_add(ctx_node->staged_bindings,
			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) {
	if (dev_priv->has_mob) {
		struct vmw_ctx_bindinfo bi;
		struct vmw_ctx_bindinfo bi;
		struct vmw_resource_val_node *res_node;


		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_shader,
		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_shader,
					user_shader_converter,
					user_shader_converter,
					&cmd->body.shid, NULL);
					&cmd->body.shid, &res_node);
		if (unlikely(ret != 0))
		if (unlikely(ret != 0))
			return ret;
			return ret;


		bi.ctx = ctx_node->res;
		bi.ctx = ctx_node->res;
		bi.res = res_node ? res_node->res : NULL;
		bi.bt = vmw_ctx_binding_shader;
		bi.bt = vmw_ctx_binding_shader;
		bi.i1.shader_type = cmd->body.type;
		bi.i1.shader_type = cmd->body.type;
		return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
		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;
			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);
	cmd = vmw_fifo_reserve(dev_priv, command_size);
	if (unlikely(cmd == NULL)) {
	if (unlikely(cmd == NULL)) {
		DRM_ERROR("Failed reserving fifo space for commands.\n");
		DRM_ERROR("Failed reserving fifo space for commands.\n");
		ret = -ENOMEM;
		ret = -ENOMEM;
		goto out_err;
		goto out_unlock_binding;
	}
	}


	vmw_apply_relocations(sw_context);
	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");
		DRM_ERROR("Fence submission error. Syncing.\n");


	vmw_resource_list_unreserve(&sw_context->resource_list, false);
	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,
	ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
				    (void *) fence);
				    (void *) fence);


@@ -2267,6 +2286,8 @@ int vmw_execbuf_process(struct drm_file *file_priv,


	return 0;
	return 0;


out_unlock_binding:
	mutex_unlock(&dev_priv->binding_mutex);
out_err:
out_err:
	vmw_resource_relocations_free(&sw_context->res_relocations);
	vmw_resource_relocations_free(&sw_context->res_relocations);
	vmw_free_relocations(sw_context);
	vmw_free_relocations(sw_context);
+1 −0
Original line number Original line Diff line number Diff line
@@ -215,6 +215,7 @@ int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
	res->func = func;
	res->func = func;
	INIT_LIST_HEAD(&res->lru_head);
	INIT_LIST_HEAD(&res->lru_head);
	INIT_LIST_HEAD(&res->mob_head);
	INIT_LIST_HEAD(&res->mob_head);
	INIT_LIST_HEAD(&res->binding_head);
	res->id = -1;
	res->id = -1;
	res->backup = NULL;
	res->backup = NULL;
	res->backup_offset = 0;
	res->backup_offset = 0;
Loading