1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright © 2018-2019 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include <drm/ttm/ttm_placement.h>
31 #include "vmwgfx_drv.h"
32 #include "vmwgfx_resource_priv.h"
33 #include "vmwgfx_binding.h"
36 * struct vmw_dx_streamoutput - Streamoutput resource metadata.
37 * @res: Base resource struct.
38 * @ctx: Non-refcounted context to which @res belong.
39 * @cotable: Refcounted cotable holding this Streamoutput.
40 * @cotable_head: List head for cotable-so_res list.
41 * @id: User-space provided identifier.
42 * @size: User-space provided mob size.
43 * @committed: Whether streamoutput is actually created or pending creation.
45 struct vmw_dx_streamoutput {
46 struct vmw_resource res;
47 struct vmw_resource *ctx;
48 struct vmw_resource *cotable;
49 struct list_head cotable_head;
55 static int vmw_dx_streamoutput_create(struct vmw_resource *res);
56 static int vmw_dx_streamoutput_bind(struct vmw_resource *res,
57 struct ttm_validate_buffer *val_buf);
58 static int vmw_dx_streamoutput_unbind(struct vmw_resource *res, bool readback,
59 struct ttm_validate_buffer *val_buf);
60 static void vmw_dx_streamoutput_commit_notify(struct vmw_resource *res,
61 enum vmw_cmdbuf_res_state state);
63 static const struct vmw_res_func vmw_dx_streamoutput_func = {
64 .res_type = vmw_res_streamoutput,
67 .type_name = "DX streamoutput",
68 .backup_placement = &vmw_mob_placement,
69 .create = vmw_dx_streamoutput_create,
70 .destroy = NULL, /* Command buffer managed resource. */
71 .bind = vmw_dx_streamoutput_bind,
72 .unbind = vmw_dx_streamoutput_unbind,
73 .commit_notify = vmw_dx_streamoutput_commit_notify,
76 static inline struct vmw_dx_streamoutput *
77 vmw_res_to_dx_streamoutput(struct vmw_resource *res)
79 return container_of(res, struct vmw_dx_streamoutput, res);
83 * vmw_dx_streamoutput_unscrub - Reattach the MOB to streamoutput.
84 * @res: The streamoutput resource.
86 * Return: 0 on success, negative error code on failure.
88 static int vmw_dx_streamoutput_unscrub(struct vmw_resource *res)
90 struct vmw_dx_streamoutput *so = vmw_res_to_dx_streamoutput(res);
91 struct vmw_private *dev_priv = res->dev_priv;
93 SVGA3dCmdHeader header;
94 SVGA3dCmdDXBindStreamOutput body;
97 if (!list_empty(&so->cotable_head) || !so->committed )
100 cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), so->ctx->id);
104 cmd->header.id = SVGA_3D_CMD_DX_BIND_STREAMOUTPUT;
105 cmd->header.size = sizeof(cmd->body);
106 cmd->body.soid = so->id;
107 cmd->body.mobid = res->backup->base.resource->start;
108 cmd->body.offsetInBytes = res->backup_offset;
109 cmd->body.sizeInBytes = so->size;
110 vmw_cmd_commit(dev_priv, sizeof(*cmd));
112 vmw_cotable_add_resource(so->cotable, &so->cotable_head);
117 static int vmw_dx_streamoutput_create(struct vmw_resource *res)
119 struct vmw_private *dev_priv = res->dev_priv;
120 struct vmw_dx_streamoutput *so = vmw_res_to_dx_streamoutput(res);
123 WARN_ON_ONCE(!so->committed);
125 if (vmw_resource_mob_attached(res)) {
126 mutex_lock(&dev_priv->binding_mutex);
127 ret = vmw_dx_streamoutput_unscrub(res);
128 mutex_unlock(&dev_priv->binding_mutex);
136 static int vmw_dx_streamoutput_bind(struct vmw_resource *res,
137 struct ttm_validate_buffer *val_buf)
139 struct vmw_private *dev_priv = res->dev_priv;
140 struct ttm_buffer_object *bo = val_buf->bo;
143 if (WARN_ON(bo->resource->mem_type != VMW_PL_MOB))
146 mutex_lock(&dev_priv->binding_mutex);
147 ret = vmw_dx_streamoutput_unscrub(res);
148 mutex_unlock(&dev_priv->binding_mutex);
154 * vmw_dx_streamoutput_scrub - Unbind the MOB from streamoutput.
155 * @res: The streamoutput resource.
157 * Return: 0 on success, negative error code on failure.
159 static int vmw_dx_streamoutput_scrub(struct vmw_resource *res)
161 struct vmw_private *dev_priv = res->dev_priv;
162 struct vmw_dx_streamoutput *so = vmw_res_to_dx_streamoutput(res);
164 SVGA3dCmdHeader header;
165 SVGA3dCmdDXBindStreamOutput body;
168 if (list_empty(&so->cotable_head))
171 WARN_ON_ONCE(!so->committed);
173 cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), so->ctx->id);
177 cmd->header.id = SVGA_3D_CMD_DX_BIND_STREAMOUTPUT;
178 cmd->header.size = sizeof(cmd->body);
179 cmd->body.soid = res->id;
180 cmd->body.mobid = SVGA3D_INVALID_ID;
181 cmd->body.offsetInBytes = 0;
182 cmd->body.sizeInBytes = so->size;
183 vmw_cmd_commit(dev_priv, sizeof(*cmd));
186 list_del_init(&so->cotable_head);
191 static int vmw_dx_streamoutput_unbind(struct vmw_resource *res, bool readback,
192 struct ttm_validate_buffer *val_buf)
194 struct vmw_private *dev_priv = res->dev_priv;
195 struct vmw_fence_obj *fence;
198 if (WARN_ON(res->backup->base.resource->mem_type != VMW_PL_MOB))
201 mutex_lock(&dev_priv->binding_mutex);
202 ret = vmw_dx_streamoutput_scrub(res);
203 mutex_unlock(&dev_priv->binding_mutex);
208 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
209 vmw_bo_fence_single(val_buf->bo, fence);
212 vmw_fence_obj_unreference(&fence);
217 static void vmw_dx_streamoutput_commit_notify(struct vmw_resource *res,
218 enum vmw_cmdbuf_res_state state)
220 struct vmw_private *dev_priv = res->dev_priv;
221 struct vmw_dx_streamoutput *so = vmw_res_to_dx_streamoutput(res);
223 if (state == VMW_CMDBUF_RES_ADD) {
224 mutex_lock(&dev_priv->binding_mutex);
225 vmw_cotable_add_resource(so->cotable, &so->cotable_head);
226 so->committed = true;
228 mutex_unlock(&dev_priv->binding_mutex);
230 mutex_lock(&dev_priv->binding_mutex);
231 list_del_init(&so->cotable_head);
232 so->committed = false;
234 mutex_unlock(&dev_priv->binding_mutex);
239 * vmw_dx_streamoutput_lookup - Do a streamoutput resource lookup by user key.
240 * @man: Command buffer managed resource manager for current context.
241 * @user_key: User-space identifier for lookup.
243 * Return: Valid refcounted vmw_resource on success, error pointer on failure.
245 struct vmw_resource *
246 vmw_dx_streamoutput_lookup(struct vmw_cmdbuf_res_manager *man,
249 return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_streamoutput,
253 static void vmw_dx_streamoutput_res_free(struct vmw_resource *res)
255 struct vmw_dx_streamoutput *so = vmw_res_to_dx_streamoutput(res);
257 vmw_resource_unreference(&so->cotable);
261 static void vmw_dx_streamoutput_hw_destroy(struct vmw_resource *res)
263 /* Destroyed by user-space cmd buf or as part of context takedown. */
268 * vmw_dx_streamoutput_add - Add a streamoutput as a cmd buf managed resource.
269 * @man: Command buffer managed resource manager for current context.
270 * @ctx: Pointer to context resource.
271 * @user_key: The identifier for this streamoutput.
272 * @list: The list of staged command buffer managed resources.
274 * Return: 0 on success, negative error code on failure.
276 int vmw_dx_streamoutput_add(struct vmw_cmdbuf_res_manager *man,
277 struct vmw_resource *ctx, u32 user_key,
278 struct list_head *list)
280 struct vmw_dx_streamoutput *so;
281 struct vmw_resource *res;
282 struct vmw_private *dev_priv = ctx->dev_priv;
285 so = kmalloc(sizeof(*so), GFP_KERNEL);
292 so->cotable = vmw_resource_reference
293 (vmw_context_cotable(ctx, SVGA_COTABLE_STREAMOUTPUT));
295 so->committed = false;
296 INIT_LIST_HEAD(&so->cotable_head);
297 ret = vmw_resource_init(dev_priv, res, true,
298 vmw_dx_streamoutput_res_free,
299 &vmw_dx_streamoutput_func);
301 goto out_resource_init;
303 ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_streamoutput, user_key,
306 goto out_resource_init;
309 res->hw_destroy = vmw_dx_streamoutput_hw_destroy;
312 vmw_resource_unreference(&res);
318 * vmw_dx_streamoutput_set_size - Sets streamoutput mob size in res struct.
319 * @res: The streamoutput res for which need to set size.
320 * @size: The size provided by user-space to set.
322 void vmw_dx_streamoutput_set_size(struct vmw_resource *res, u32 size)
324 struct vmw_dx_streamoutput *so = vmw_res_to_dx_streamoutput(res);
330 * vmw_dx_streamoutput_remove - Stage streamoutput for removal.
331 * @man: Command buffer managed resource manager for current context.
332 * @user_key: The identifier for this streamoutput.
333 * @list: The list of staged command buffer managed resources.
335 * Return: 0 on success, negative error code on failure.
337 int vmw_dx_streamoutput_remove(struct vmw_cmdbuf_res_manager *man,
339 struct list_head *list)
341 struct vmw_resource *r;
343 return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_streamoutput,
344 (u32)user_key, list, &r);
348 * vmw_dx_streamoutput_cotable_list_scrub - cotable unbind_func callback.
349 * @dev_priv: Device private.
350 * @list: The list of cotable resources.
351 * @readback: Whether the call was part of a readback unbind.
353 void vmw_dx_streamoutput_cotable_list_scrub(struct vmw_private *dev_priv,
354 struct list_head *list,
357 struct vmw_dx_streamoutput *entry, *next;
359 lockdep_assert_held_once(&dev_priv->binding_mutex);
361 list_for_each_entry_safe(entry, next, list, cotable_head) {
362 WARN_ON(vmw_dx_streamoutput_scrub(&entry->res));
364 entry->committed =false;