1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
2c74c162fSThomas Hellstrom /**************************************************************************
3c74c162fSThomas Hellstrom  *
4*09881d29SZack Rusin  * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
5c74c162fSThomas Hellstrom  *
6c74c162fSThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
7c74c162fSThomas Hellstrom  * copy of this software and associated documentation files (the
8c74c162fSThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
9c74c162fSThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
10c74c162fSThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
11c74c162fSThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
12c74c162fSThomas Hellstrom  * the following conditions:
13c74c162fSThomas Hellstrom  *
14c74c162fSThomas Hellstrom  * The above copyright notice and this permission notice (including the
15c74c162fSThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
16c74c162fSThomas Hellstrom  * of the Software.
17c74c162fSThomas Hellstrom  *
18c74c162fSThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19c74c162fSThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20c74c162fSThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21c74c162fSThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22c74c162fSThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23c74c162fSThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24c74c162fSThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25c74c162fSThomas Hellstrom  *
26c74c162fSThomas Hellstrom  **************************************************************************/
27c74c162fSThomas Hellstrom 
28008be682SMasahiro Yamada #include <drm/ttm/ttm_placement.h>
29008be682SMasahiro Yamada 
30*09881d29SZack Rusin #include "vmwgfx_binding.h"
31*09881d29SZack Rusin #include "vmwgfx_bo.h"
32c74c162fSThomas Hellstrom #include "vmwgfx_drv.h"
33c74c162fSThomas Hellstrom #include "vmwgfx_resource_priv.h"
34c74c162fSThomas Hellstrom 
35c74c162fSThomas Hellstrom struct vmw_shader {
36c74c162fSThomas Hellstrom 	struct vmw_resource res;
37c74c162fSThomas Hellstrom 	SVGA3dShaderType type;
38c74c162fSThomas Hellstrom 	uint32_t size;
39d80efd5cSThomas Hellstrom 	uint8_t num_input_sig;
40d80efd5cSThomas Hellstrom 	uint8_t num_output_sig;
41c74c162fSThomas Hellstrom };
42c74c162fSThomas Hellstrom 
43c74c162fSThomas Hellstrom struct vmw_user_shader {
44c74c162fSThomas Hellstrom 	struct ttm_base_object base;
45c74c162fSThomas Hellstrom 	struct vmw_shader shader;
46c74c162fSThomas Hellstrom };
47c74c162fSThomas Hellstrom 
48d80efd5cSThomas Hellstrom struct vmw_dx_shader {
49d80efd5cSThomas Hellstrom 	struct vmw_resource res;
50d80efd5cSThomas Hellstrom 	struct vmw_resource *ctx;
51d80efd5cSThomas Hellstrom 	struct vmw_resource *cotable;
52d80efd5cSThomas Hellstrom 	u32 id;
53d80efd5cSThomas Hellstrom 	bool committed;
54d80efd5cSThomas Hellstrom 	struct list_head cotable_head;
55d80efd5cSThomas Hellstrom };
56d80efd5cSThomas Hellstrom 
57c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res);
58c74c162fSThomas Hellstrom static struct vmw_resource *
59c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base);
60c74c162fSThomas Hellstrom 
61c74c162fSThomas Hellstrom static int vmw_gb_shader_create(struct vmw_resource *res);
62c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res,
63c74c162fSThomas Hellstrom 			       struct ttm_validate_buffer *val_buf);
64c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res,
65c74c162fSThomas Hellstrom 				 bool readback,
66c74c162fSThomas Hellstrom 				 struct ttm_validate_buffer *val_buf);
67c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res);
68c74c162fSThomas Hellstrom 
69d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res);
70d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res,
71d80efd5cSThomas Hellstrom 			       struct ttm_validate_buffer *val_buf);
72d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res,
73d80efd5cSThomas Hellstrom 				 bool readback,
74d80efd5cSThomas Hellstrom 				 struct ttm_validate_buffer *val_buf);
75d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
76d80efd5cSThomas Hellstrom 					enum vmw_cmdbuf_res_state state);
77d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type);
78d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type);
79d80efd5cSThomas Hellstrom 
80c74c162fSThomas Hellstrom static const struct vmw_user_resource_conv user_shader_conv = {
81c74c162fSThomas Hellstrom 	.object_type = VMW_RES_SHADER,
82c74c162fSThomas Hellstrom 	.base_obj_to_res = vmw_user_shader_base_to_res,
83c74c162fSThomas Hellstrom 	.res_free = vmw_user_shader_free
84c74c162fSThomas Hellstrom };
85c74c162fSThomas Hellstrom 
86c74c162fSThomas Hellstrom const struct vmw_user_resource_conv *user_shader_converter =
87c74c162fSThomas Hellstrom 	&user_shader_conv;
88c74c162fSThomas Hellstrom 
89c74c162fSThomas Hellstrom 
90c74c162fSThomas Hellstrom static const struct vmw_res_func vmw_gb_shader_func = {
91c74c162fSThomas Hellstrom 	.res_type = vmw_res_shader,
92c74c162fSThomas Hellstrom 	.needs_backup = true,
93c74c162fSThomas Hellstrom 	.may_evict = true,
94a0a63940SThomas Hellstrom 	.prio = 3,
95a0a63940SThomas Hellstrom 	.dirty_prio = 3,
96c74c162fSThomas Hellstrom 	.type_name = "guest backed shaders",
97c74c162fSThomas Hellstrom 	.backup_placement = &vmw_mob_placement,
98c74c162fSThomas Hellstrom 	.create = vmw_gb_shader_create,
99c74c162fSThomas Hellstrom 	.destroy = vmw_gb_shader_destroy,
100c74c162fSThomas Hellstrom 	.bind = vmw_gb_shader_bind,
101c74c162fSThomas Hellstrom 	.unbind = vmw_gb_shader_unbind
102c74c162fSThomas Hellstrom };
103c74c162fSThomas Hellstrom 
104d80efd5cSThomas Hellstrom static const struct vmw_res_func vmw_dx_shader_func = {
105d80efd5cSThomas Hellstrom 	.res_type = vmw_res_shader,
106d80efd5cSThomas Hellstrom 	.needs_backup = true,
107a0a63940SThomas Hellstrom 	.may_evict = true,
108a0a63940SThomas Hellstrom 	.prio = 3,
109a0a63940SThomas Hellstrom 	.dirty_prio = 3,
110d80efd5cSThomas Hellstrom 	.type_name = "dx shaders",
111d80efd5cSThomas Hellstrom 	.backup_placement = &vmw_mob_placement,
112d80efd5cSThomas Hellstrom 	.create = vmw_dx_shader_create,
113d80efd5cSThomas Hellstrom 	/*
114d80efd5cSThomas Hellstrom 	 * The destroy callback is only called with a committed resource on
115d80efd5cSThomas Hellstrom 	 * context destroy, in which case we destroy the cotable anyway,
116d80efd5cSThomas Hellstrom 	 * so there's no need to destroy DX shaders separately.
117d80efd5cSThomas Hellstrom 	 */
118d80efd5cSThomas Hellstrom 	.destroy = NULL,
119d80efd5cSThomas Hellstrom 	.bind = vmw_dx_shader_bind,
120d80efd5cSThomas Hellstrom 	.unbind = vmw_dx_shader_unbind,
121d80efd5cSThomas Hellstrom 	.commit_notify = vmw_dx_shader_commit_notify,
122d80efd5cSThomas Hellstrom };
123d80efd5cSThomas Hellstrom 
124ad2ae415SLee Jones /*
125c74c162fSThomas Hellstrom  * Shader management:
126c74c162fSThomas Hellstrom  */
127c74c162fSThomas Hellstrom 
128c74c162fSThomas Hellstrom static inline struct vmw_shader *
129c74c162fSThomas Hellstrom vmw_res_to_shader(struct vmw_resource *res)
130c74c162fSThomas Hellstrom {
131c74c162fSThomas Hellstrom 	return container_of(res, struct vmw_shader, res);
132c74c162fSThomas Hellstrom }
133c74c162fSThomas Hellstrom 
134d80efd5cSThomas Hellstrom /**
135d80efd5cSThomas Hellstrom  * vmw_res_to_dx_shader - typecast a struct vmw_resource to a
136d80efd5cSThomas Hellstrom  * struct vmw_dx_shader
137d80efd5cSThomas Hellstrom  *
138d80efd5cSThomas Hellstrom  * @res: Pointer to the struct vmw_resource.
139d80efd5cSThomas Hellstrom  */
140d80efd5cSThomas Hellstrom static inline struct vmw_dx_shader *
141d80efd5cSThomas Hellstrom vmw_res_to_dx_shader(struct vmw_resource *res)
142d80efd5cSThomas Hellstrom {
143d80efd5cSThomas Hellstrom 	return container_of(res, struct vmw_dx_shader, res);
144d80efd5cSThomas Hellstrom }
145d80efd5cSThomas Hellstrom 
146c74c162fSThomas Hellstrom static void vmw_hw_shader_destroy(struct vmw_resource *res)
147c74c162fSThomas Hellstrom {
148d80efd5cSThomas Hellstrom 	if (likely(res->func->destroy))
149d80efd5cSThomas Hellstrom 		(void) res->func->destroy(res);
150d80efd5cSThomas Hellstrom 	else
151d80efd5cSThomas Hellstrom 		res->id = -1;
152c74c162fSThomas Hellstrom }
153c74c162fSThomas Hellstrom 
154d80efd5cSThomas Hellstrom 
155c74c162fSThomas Hellstrom static int vmw_gb_shader_init(struct vmw_private *dev_priv,
156c74c162fSThomas Hellstrom 			      struct vmw_resource *res,
157c74c162fSThomas Hellstrom 			      uint32_t size,
158c74c162fSThomas Hellstrom 			      uint64_t offset,
159c74c162fSThomas Hellstrom 			      SVGA3dShaderType type,
160d80efd5cSThomas Hellstrom 			      uint8_t num_input_sig,
161d80efd5cSThomas Hellstrom 			      uint8_t num_output_sig,
162*09881d29SZack Rusin 			      struct vmw_bo *byte_code,
163c74c162fSThomas Hellstrom 			      void (*res_free) (struct vmw_resource *res))
164c74c162fSThomas Hellstrom {
165c74c162fSThomas Hellstrom 	struct vmw_shader *shader = vmw_res_to_shader(res);
166c74c162fSThomas Hellstrom 	int ret;
167c74c162fSThomas Hellstrom 
168d80efd5cSThomas Hellstrom 	ret = vmw_resource_init(dev_priv, res, true, res_free,
169d80efd5cSThomas Hellstrom 				&vmw_gb_shader_func);
170c74c162fSThomas Hellstrom 
171c74c162fSThomas Hellstrom 	if (unlikely(ret != 0)) {
172c74c162fSThomas Hellstrom 		if (res_free)
173c74c162fSThomas Hellstrom 			res_free(res);
174c74c162fSThomas Hellstrom 		else
175c74c162fSThomas Hellstrom 			kfree(res);
176c74c162fSThomas Hellstrom 		return ret;
177c74c162fSThomas Hellstrom 	}
178c74c162fSThomas Hellstrom 
179c74c162fSThomas Hellstrom 	res->backup_size = size;
180c74c162fSThomas Hellstrom 	if (byte_code) {
181f1d34bfdSThomas Hellstrom 		res->backup = vmw_bo_reference(byte_code);
182c74c162fSThomas Hellstrom 		res->backup_offset = offset;
183c74c162fSThomas Hellstrom 	}
184c74c162fSThomas Hellstrom 	shader->size = size;
185c74c162fSThomas Hellstrom 	shader->type = type;
186d80efd5cSThomas Hellstrom 	shader->num_input_sig = num_input_sig;
187d80efd5cSThomas Hellstrom 	shader->num_output_sig = num_output_sig;
188c74c162fSThomas Hellstrom 
18913289241SThomas Hellstrom 	res->hw_destroy = vmw_hw_shader_destroy;
190c74c162fSThomas Hellstrom 	return 0;
191c74c162fSThomas Hellstrom }
192c74c162fSThomas Hellstrom 
193d80efd5cSThomas Hellstrom /*
194d80efd5cSThomas Hellstrom  * GB shader code:
195d80efd5cSThomas Hellstrom  */
196d80efd5cSThomas Hellstrom 
197c74c162fSThomas Hellstrom static int vmw_gb_shader_create(struct vmw_resource *res)
198c74c162fSThomas Hellstrom {
199c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
200c74c162fSThomas Hellstrom 	struct vmw_shader *shader = vmw_res_to_shader(res);
201c74c162fSThomas Hellstrom 	int ret;
202c74c162fSThomas Hellstrom 	struct {
203c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
204c74c162fSThomas Hellstrom 		SVGA3dCmdDefineGBShader body;
205c74c162fSThomas Hellstrom 	} *cmd;
206c74c162fSThomas Hellstrom 
207c74c162fSThomas Hellstrom 	if (likely(res->id != -1))
208c74c162fSThomas Hellstrom 		return 0;
209c74c162fSThomas Hellstrom 
210c74c162fSThomas Hellstrom 	ret = vmw_resource_alloc_id(res);
211c74c162fSThomas Hellstrom 	if (unlikely(ret != 0)) {
212c74c162fSThomas Hellstrom 		DRM_ERROR("Failed to allocate a shader id.\n");
213c74c162fSThomas Hellstrom 		goto out_no_id;
214c74c162fSThomas Hellstrom 	}
215c74c162fSThomas Hellstrom 
216c74c162fSThomas Hellstrom 	if (unlikely(res->id >= VMWGFX_NUM_GB_SHADER)) {
217c74c162fSThomas Hellstrom 		ret = -EBUSY;
218c74c162fSThomas Hellstrom 		goto out_no_fifo;
219c74c162fSThomas Hellstrom 	}
220c74c162fSThomas Hellstrom 
2218426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
222c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
223c74c162fSThomas Hellstrom 		ret = -ENOMEM;
224c74c162fSThomas Hellstrom 		goto out_no_fifo;
225c74c162fSThomas Hellstrom 	}
226c74c162fSThomas Hellstrom 
227c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER;
228c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
229c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
230c74c162fSThomas Hellstrom 	cmd->body.type = shader->type;
231c74c162fSThomas Hellstrom 	cmd->body.sizeInBytes = shader->size;
2328426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
233153b3d5bSThomas Hellstrom 	vmw_fifo_resource_inc(dev_priv);
234c74c162fSThomas Hellstrom 
235c74c162fSThomas Hellstrom 	return 0;
236c74c162fSThomas Hellstrom 
237c74c162fSThomas Hellstrom out_no_fifo:
238c74c162fSThomas Hellstrom 	vmw_resource_release_id(res);
239c74c162fSThomas Hellstrom out_no_id:
240c74c162fSThomas Hellstrom 	return ret;
241c74c162fSThomas Hellstrom }
242c74c162fSThomas Hellstrom 
243c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res,
244c74c162fSThomas Hellstrom 			      struct ttm_validate_buffer *val_buf)
245c74c162fSThomas Hellstrom {
246c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
247c74c162fSThomas Hellstrom 	struct {
248c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
249c74c162fSThomas Hellstrom 		SVGA3dCmdBindGBShader body;
250c74c162fSThomas Hellstrom 	} *cmd;
251c74c162fSThomas Hellstrom 	struct ttm_buffer_object *bo = val_buf->bo;
252c74c162fSThomas Hellstrom 
253d3116756SChristian König 	BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
254c74c162fSThomas Hellstrom 
2558426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
25611c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
257c74c162fSThomas Hellstrom 		return -ENOMEM;
258c74c162fSThomas Hellstrom 
259c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
260c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
261c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
262d3116756SChristian König 	cmd->body.mobid = bo->resource->start;
263b8ccd1e4SThomas Hellstrom 	cmd->body.offsetInBytes = res->backup_offset;
264c74c162fSThomas Hellstrom 	res->backup_dirty = false;
2658426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
266c74c162fSThomas Hellstrom 
267c74c162fSThomas Hellstrom 	return 0;
268c74c162fSThomas Hellstrom }
269c74c162fSThomas Hellstrom 
270c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res,
271c74c162fSThomas Hellstrom 				bool readback,
272c74c162fSThomas Hellstrom 				struct ttm_validate_buffer *val_buf)
273c74c162fSThomas Hellstrom {
274c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
275c74c162fSThomas Hellstrom 	struct {
276c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
277c74c162fSThomas Hellstrom 		SVGA3dCmdBindGBShader body;
278c74c162fSThomas Hellstrom 	} *cmd;
279c74c162fSThomas Hellstrom 	struct vmw_fence_obj *fence;
280c74c162fSThomas Hellstrom 
281d3116756SChristian König 	BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB);
282c74c162fSThomas Hellstrom 
2838426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
28411c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
285c74c162fSThomas Hellstrom 		return -ENOMEM;
286c74c162fSThomas Hellstrom 
287c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
288c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
289c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
290c74c162fSThomas Hellstrom 	cmd->body.mobid = SVGA3D_INVALID_ID;
291c74c162fSThomas Hellstrom 	cmd->body.offsetInBytes = 0;
2928426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
293c74c162fSThomas Hellstrom 
294c74c162fSThomas Hellstrom 	/*
295c74c162fSThomas Hellstrom 	 * Create a fence object and fence the backup buffer.
296c74c162fSThomas Hellstrom 	 */
297c74c162fSThomas Hellstrom 
298c74c162fSThomas Hellstrom 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
299c74c162fSThomas Hellstrom 					  &fence, NULL);
300c74c162fSThomas Hellstrom 
301e9431ea5SThomas Hellstrom 	vmw_bo_fence_single(val_buf->bo, fence);
302c74c162fSThomas Hellstrom 
303c74c162fSThomas Hellstrom 	if (likely(fence != NULL))
304c74c162fSThomas Hellstrom 		vmw_fence_obj_unreference(&fence);
305c74c162fSThomas Hellstrom 
306c74c162fSThomas Hellstrom 	return 0;
307c74c162fSThomas Hellstrom }
308c74c162fSThomas Hellstrom 
309c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res)
310c74c162fSThomas Hellstrom {
311c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
312c74c162fSThomas Hellstrom 	struct {
313c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
314c74c162fSThomas Hellstrom 		SVGA3dCmdDestroyGBShader body;
315c74c162fSThomas Hellstrom 	} *cmd;
316c74c162fSThomas Hellstrom 
317c74c162fSThomas Hellstrom 	if (likely(res->id == -1))
318c74c162fSThomas Hellstrom 		return 0;
319c74c162fSThomas Hellstrom 
320173fb7d4SThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
321d80efd5cSThomas Hellstrom 	vmw_binding_res_list_scrub(&res->binding_head);
322173fb7d4SThomas Hellstrom 
3238426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
324c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
3253e894a62SThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
326c74c162fSThomas Hellstrom 		return -ENOMEM;
327c74c162fSThomas Hellstrom 	}
328c74c162fSThomas Hellstrom 
329c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER;
330c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
331c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
3328426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
333173fb7d4SThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
334c74c162fSThomas Hellstrom 	vmw_resource_release_id(res);
335153b3d5bSThomas Hellstrom 	vmw_fifo_resource_dec(dev_priv);
336c74c162fSThomas Hellstrom 
337c74c162fSThomas Hellstrom 	return 0;
338c74c162fSThomas Hellstrom }
339c74c162fSThomas Hellstrom 
340d80efd5cSThomas Hellstrom /*
341d80efd5cSThomas Hellstrom  * DX shader code:
342d80efd5cSThomas Hellstrom  */
343d80efd5cSThomas Hellstrom 
344d80efd5cSThomas Hellstrom /**
345d80efd5cSThomas Hellstrom  * vmw_dx_shader_commit_notify - Notify that a shader operation has been
346d80efd5cSThomas Hellstrom  * committed to hardware from a user-supplied command stream.
347d80efd5cSThomas Hellstrom  *
348d80efd5cSThomas Hellstrom  * @res: Pointer to the shader resource.
349d80efd5cSThomas Hellstrom  * @state: Indicating whether a creation or removal has been committed.
350d80efd5cSThomas Hellstrom  *
351d80efd5cSThomas Hellstrom  */
352d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
353d80efd5cSThomas Hellstrom 					enum vmw_cmdbuf_res_state state)
354d80efd5cSThomas Hellstrom {
355d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
356d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
357d80efd5cSThomas Hellstrom 
358d80efd5cSThomas Hellstrom 	if (state == VMW_CMDBUF_RES_ADD) {
359d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
360d80efd5cSThomas Hellstrom 		vmw_cotable_add_resource(shader->cotable,
361d80efd5cSThomas Hellstrom 					 &shader->cotable_head);
362d80efd5cSThomas Hellstrom 		shader->committed = true;
363d80efd5cSThomas Hellstrom 		res->id = shader->id;
364d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
365d80efd5cSThomas Hellstrom 	} else {
366d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
367d80efd5cSThomas Hellstrom 		list_del_init(&shader->cotable_head);
368d80efd5cSThomas Hellstrom 		shader->committed = false;
369d80efd5cSThomas Hellstrom 		res->id = -1;
370d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
371d80efd5cSThomas Hellstrom 	}
372d80efd5cSThomas Hellstrom }
373d80efd5cSThomas Hellstrom 
374d80efd5cSThomas Hellstrom /**
375d80efd5cSThomas Hellstrom  * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader.
376d80efd5cSThomas Hellstrom  *
377d80efd5cSThomas Hellstrom  * @res: The shader resource
378d80efd5cSThomas Hellstrom  *
379d80efd5cSThomas Hellstrom  * This function reverts a scrub operation.
380d80efd5cSThomas Hellstrom  */
381d80efd5cSThomas Hellstrom static int vmw_dx_shader_unscrub(struct vmw_resource *res)
382d80efd5cSThomas Hellstrom {
383d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
384d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
385d80efd5cSThomas Hellstrom 	struct {
386d80efd5cSThomas Hellstrom 		SVGA3dCmdHeader header;
387d80efd5cSThomas Hellstrom 		SVGA3dCmdDXBindShader body;
388d80efd5cSThomas Hellstrom 	} *cmd;
389d80efd5cSThomas Hellstrom 
390d80efd5cSThomas Hellstrom 	if (!list_empty(&shader->cotable_head) || !shader->committed)
391d80efd5cSThomas Hellstrom 		return 0;
392d80efd5cSThomas Hellstrom 
3938426ed9cSZack Rusin 	cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), shader->ctx->id);
39411c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
395d80efd5cSThomas Hellstrom 		return -ENOMEM;
396d80efd5cSThomas Hellstrom 
397d80efd5cSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
398d80efd5cSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
399d80efd5cSThomas Hellstrom 	cmd->body.cid = shader->ctx->id;
400d80efd5cSThomas Hellstrom 	cmd->body.shid = shader->id;
401d3116756SChristian König 	cmd->body.mobid = res->backup->base.resource->start;
402d80efd5cSThomas Hellstrom 	cmd->body.offsetInBytes = res->backup_offset;
4038426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
404d80efd5cSThomas Hellstrom 
405d80efd5cSThomas Hellstrom 	vmw_cotable_add_resource(shader->cotable, &shader->cotable_head);
406d80efd5cSThomas Hellstrom 
407d80efd5cSThomas Hellstrom 	return 0;
408d80efd5cSThomas Hellstrom }
409d80efd5cSThomas Hellstrom 
410d80efd5cSThomas Hellstrom /**
411d80efd5cSThomas Hellstrom  * vmw_dx_shader_create - The DX shader create callback
412d80efd5cSThomas Hellstrom  *
413d80efd5cSThomas Hellstrom  * @res: The DX shader resource
414d80efd5cSThomas Hellstrom  *
415d80efd5cSThomas Hellstrom  * The create callback is called as part of resource validation and
416d80efd5cSThomas Hellstrom  * makes sure that we unscrub the shader if it's previously been scrubbed.
417d80efd5cSThomas Hellstrom  */
418d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res)
419d80efd5cSThomas Hellstrom {
420d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
421d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
422d80efd5cSThomas Hellstrom 	int ret = 0;
423d80efd5cSThomas Hellstrom 
424d80efd5cSThomas Hellstrom 	WARN_ON_ONCE(!shader->committed);
425d80efd5cSThomas Hellstrom 
426a0a63940SThomas Hellstrom 	if (vmw_resource_mob_attached(res)) {
427d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
428d80efd5cSThomas Hellstrom 		ret = vmw_dx_shader_unscrub(res);
429d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
430d80efd5cSThomas Hellstrom 	}
431d80efd5cSThomas Hellstrom 
432d80efd5cSThomas Hellstrom 	res->id = shader->id;
433d80efd5cSThomas Hellstrom 	return ret;
434d80efd5cSThomas Hellstrom }
435d80efd5cSThomas Hellstrom 
436d80efd5cSThomas Hellstrom /**
437d80efd5cSThomas Hellstrom  * vmw_dx_shader_bind - The DX shader bind callback
438d80efd5cSThomas Hellstrom  *
439d80efd5cSThomas Hellstrom  * @res: The DX shader resource
440d80efd5cSThomas Hellstrom  * @val_buf: Pointer to the validate buffer.
441d80efd5cSThomas Hellstrom  *
442d80efd5cSThomas Hellstrom  */
443d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res,
444d80efd5cSThomas Hellstrom 			      struct ttm_validate_buffer *val_buf)
445d80efd5cSThomas Hellstrom {
446d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
447d80efd5cSThomas Hellstrom 	struct ttm_buffer_object *bo = val_buf->bo;
448d80efd5cSThomas Hellstrom 
449d3116756SChristian König 	BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
450d80efd5cSThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
451d80efd5cSThomas Hellstrom 	vmw_dx_shader_unscrub(res);
452d80efd5cSThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
453d80efd5cSThomas Hellstrom 
454d80efd5cSThomas Hellstrom 	return 0;
455d80efd5cSThomas Hellstrom }
456d80efd5cSThomas Hellstrom 
457d80efd5cSThomas Hellstrom /**
458d80efd5cSThomas Hellstrom  * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader.
459d80efd5cSThomas Hellstrom  *
460d80efd5cSThomas Hellstrom  * @res: The shader resource
461d80efd5cSThomas Hellstrom  *
462d80efd5cSThomas Hellstrom  * This function unbinds a MOB from the DX shader without requiring the
463d80efd5cSThomas Hellstrom  * MOB dma_buffer to be reserved. The driver still considers the MOB bound.
464d80efd5cSThomas Hellstrom  * However, once the driver eventually decides to unbind the MOB, it doesn't
465d80efd5cSThomas Hellstrom  * need to access the context.
466d80efd5cSThomas Hellstrom  */
467d80efd5cSThomas Hellstrom static int vmw_dx_shader_scrub(struct vmw_resource *res)
468d80efd5cSThomas Hellstrom {
469d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
470d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
471d80efd5cSThomas Hellstrom 	struct {
472d80efd5cSThomas Hellstrom 		SVGA3dCmdHeader header;
473d80efd5cSThomas Hellstrom 		SVGA3dCmdDXBindShader body;
474d80efd5cSThomas Hellstrom 	} *cmd;
475d80efd5cSThomas Hellstrom 
476d80efd5cSThomas Hellstrom 	if (list_empty(&shader->cotable_head))
477d80efd5cSThomas Hellstrom 		return 0;
478d80efd5cSThomas Hellstrom 
479d80efd5cSThomas Hellstrom 	WARN_ON_ONCE(!shader->committed);
4808426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
48111c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
482d80efd5cSThomas Hellstrom 		return -ENOMEM;
483d80efd5cSThomas Hellstrom 
484d80efd5cSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
485d80efd5cSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
486d80efd5cSThomas Hellstrom 	cmd->body.cid = shader->ctx->id;
487d80efd5cSThomas Hellstrom 	cmd->body.shid = res->id;
488d80efd5cSThomas Hellstrom 	cmd->body.mobid = SVGA3D_INVALID_ID;
489d80efd5cSThomas Hellstrom 	cmd->body.offsetInBytes = 0;
4908426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
491d80efd5cSThomas Hellstrom 	res->id = -1;
492d80efd5cSThomas Hellstrom 	list_del_init(&shader->cotable_head);
493d80efd5cSThomas Hellstrom 
494d80efd5cSThomas Hellstrom 	return 0;
495d80efd5cSThomas Hellstrom }
496d80efd5cSThomas Hellstrom 
497d80efd5cSThomas Hellstrom /**
498d80efd5cSThomas Hellstrom  * vmw_dx_shader_unbind - The dx shader unbind callback.
499d80efd5cSThomas Hellstrom  *
500d80efd5cSThomas Hellstrom  * @res: The shader resource
501d80efd5cSThomas Hellstrom  * @readback: Whether this is a readback unbind. Currently unused.
502d80efd5cSThomas Hellstrom  * @val_buf: MOB buffer information.
503d80efd5cSThomas Hellstrom  */
504d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res,
505d80efd5cSThomas Hellstrom 				bool readback,
506d80efd5cSThomas Hellstrom 				struct ttm_validate_buffer *val_buf)
507d80efd5cSThomas Hellstrom {
508d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
509d80efd5cSThomas Hellstrom 	struct vmw_fence_obj *fence;
510d80efd5cSThomas Hellstrom 	int ret;
511d80efd5cSThomas Hellstrom 
512d3116756SChristian König 	BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB);
513d80efd5cSThomas Hellstrom 
514d80efd5cSThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
515d80efd5cSThomas Hellstrom 	ret = vmw_dx_shader_scrub(res);
516d80efd5cSThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
517d80efd5cSThomas Hellstrom 
518d80efd5cSThomas Hellstrom 	if (ret)
519d80efd5cSThomas Hellstrom 		return ret;
520d80efd5cSThomas Hellstrom 
521d80efd5cSThomas Hellstrom 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
522d80efd5cSThomas Hellstrom 					  &fence, NULL);
523e9431ea5SThomas Hellstrom 	vmw_bo_fence_single(val_buf->bo, fence);
524d80efd5cSThomas Hellstrom 
525d80efd5cSThomas Hellstrom 	if (likely(fence != NULL))
526d80efd5cSThomas Hellstrom 		vmw_fence_obj_unreference(&fence);
527d80efd5cSThomas Hellstrom 
528d80efd5cSThomas Hellstrom 	return 0;
529d80efd5cSThomas Hellstrom }
530d80efd5cSThomas Hellstrom 
531d80efd5cSThomas Hellstrom /**
532d80efd5cSThomas Hellstrom  * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for
533d80efd5cSThomas Hellstrom  * DX shaders.
534d80efd5cSThomas Hellstrom  *
535d80efd5cSThomas Hellstrom  * @dev_priv: Pointer to device private structure.
536d80efd5cSThomas Hellstrom  * @list: The list of cotable resources.
537d80efd5cSThomas Hellstrom  * @readback: Whether the call was part of a readback unbind.
538d80efd5cSThomas Hellstrom  *
539d80efd5cSThomas Hellstrom  * Scrubs all shader MOBs so that any subsequent shader unbind or shader
540d80efd5cSThomas Hellstrom  * destroy operation won't need to swap in the context.
541d80efd5cSThomas Hellstrom  */
542d80efd5cSThomas Hellstrom void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
543d80efd5cSThomas Hellstrom 				      struct list_head *list,
544d80efd5cSThomas Hellstrom 				      bool readback)
545d80efd5cSThomas Hellstrom {
546d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *entry, *next;
547d80efd5cSThomas Hellstrom 
548d76ce03eSThomas Hellstrom 	lockdep_assert_held_once(&dev_priv->binding_mutex);
549d80efd5cSThomas Hellstrom 
550d80efd5cSThomas Hellstrom 	list_for_each_entry_safe(entry, next, list, cotable_head) {
551d80efd5cSThomas Hellstrom 		WARN_ON(vmw_dx_shader_scrub(&entry->res));
552d80efd5cSThomas Hellstrom 		if (!readback)
553d80efd5cSThomas Hellstrom 			entry->committed = false;
554d80efd5cSThomas Hellstrom 	}
555d80efd5cSThomas Hellstrom }
556d80efd5cSThomas Hellstrom 
557d80efd5cSThomas Hellstrom /**
558d80efd5cSThomas Hellstrom  * vmw_dx_shader_res_free - The DX shader free callback
559d80efd5cSThomas Hellstrom  *
560d80efd5cSThomas Hellstrom  * @res: The shader resource
561d80efd5cSThomas Hellstrom  *
5628aadeb8aSZack Rusin  * Frees the DX shader resource.
563d80efd5cSThomas Hellstrom  */
564d80efd5cSThomas Hellstrom static void vmw_dx_shader_res_free(struct vmw_resource *res)
565d80efd5cSThomas Hellstrom {
566d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
567d80efd5cSThomas Hellstrom 
568d80efd5cSThomas Hellstrom 	vmw_resource_unreference(&shader->cotable);
569d80efd5cSThomas Hellstrom 	kfree(shader);
570d80efd5cSThomas Hellstrom }
571d80efd5cSThomas Hellstrom 
572d80efd5cSThomas Hellstrom /**
573d80efd5cSThomas Hellstrom  * vmw_dx_shader_add - Add a shader resource as a command buffer managed
574d80efd5cSThomas Hellstrom  * resource.
575d80efd5cSThomas Hellstrom  *
576d80efd5cSThomas Hellstrom  * @man: The command buffer resource manager.
577d80efd5cSThomas Hellstrom  * @ctx: Pointer to the context resource.
578d80efd5cSThomas Hellstrom  * @user_key: The id used for this shader.
579d80efd5cSThomas Hellstrom  * @shader_type: The shader type.
580d80efd5cSThomas Hellstrom  * @list: The list of staged command buffer managed resources.
581d80efd5cSThomas Hellstrom  */
582d80efd5cSThomas Hellstrom int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
583d80efd5cSThomas Hellstrom 		      struct vmw_resource *ctx,
584d80efd5cSThomas Hellstrom 		      u32 user_key,
585d80efd5cSThomas Hellstrom 		      SVGA3dShaderType shader_type,
586d80efd5cSThomas Hellstrom 		      struct list_head *list)
587d80efd5cSThomas Hellstrom {
588d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader;
589d80efd5cSThomas Hellstrom 	struct vmw_resource *res;
590d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = ctx->dev_priv;
591d80efd5cSThomas Hellstrom 	int ret;
592d80efd5cSThomas Hellstrom 
593d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
594d80efd5cSThomas Hellstrom 		return -EINVAL;
595d80efd5cSThomas Hellstrom 
596d80efd5cSThomas Hellstrom 	shader = kmalloc(sizeof(*shader), GFP_KERNEL);
597d80efd5cSThomas Hellstrom 	if (!shader) {
598d80efd5cSThomas Hellstrom 		return -ENOMEM;
599d80efd5cSThomas Hellstrom 	}
600d80efd5cSThomas Hellstrom 
601d80efd5cSThomas Hellstrom 	res = &shader->res;
602d80efd5cSThomas Hellstrom 	shader->ctx = ctx;
6031b9a01d6SThomas Hellstrom 	shader->cotable = vmw_resource_reference
6041b9a01d6SThomas Hellstrom 		(vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER));
605d80efd5cSThomas Hellstrom 	shader->id = user_key;
606d80efd5cSThomas Hellstrom 	shader->committed = false;
607d80efd5cSThomas Hellstrom 	INIT_LIST_HEAD(&shader->cotable_head);
608d80efd5cSThomas Hellstrom 	ret = vmw_resource_init(dev_priv, res, true,
609d80efd5cSThomas Hellstrom 				vmw_dx_shader_res_free, &vmw_dx_shader_func);
610d80efd5cSThomas Hellstrom 	if (ret)
611d80efd5cSThomas Hellstrom 		goto out_resource_init;
612d80efd5cSThomas Hellstrom 
613d80efd5cSThomas Hellstrom 	/*
614d80efd5cSThomas Hellstrom 	 * The user_key name-space is not per shader type for DX shaders,
615d80efd5cSThomas Hellstrom 	 * so when hashing, use a single zero shader type.
616d80efd5cSThomas Hellstrom 	 */
617d80efd5cSThomas Hellstrom 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
618d80efd5cSThomas Hellstrom 				 vmw_shader_key(user_key, 0),
619d80efd5cSThomas Hellstrom 				 res, list);
620d80efd5cSThomas Hellstrom 	if (ret)
621d80efd5cSThomas Hellstrom 		goto out_resource_init;
622d80efd5cSThomas Hellstrom 
623d80efd5cSThomas Hellstrom 	res->id = shader->id;
62413289241SThomas Hellstrom 	res->hw_destroy = vmw_hw_shader_destroy;
625d80efd5cSThomas Hellstrom 
626d80efd5cSThomas Hellstrom out_resource_init:
627d80efd5cSThomas Hellstrom 	vmw_resource_unreference(&res);
628d80efd5cSThomas Hellstrom 
629d80efd5cSThomas Hellstrom 	return ret;
630d80efd5cSThomas Hellstrom }
631d80efd5cSThomas Hellstrom 
632d80efd5cSThomas Hellstrom 
633d80efd5cSThomas Hellstrom 
634ad2ae415SLee Jones /*
635c74c162fSThomas Hellstrom  * User-space shader management:
636c74c162fSThomas Hellstrom  */
637c74c162fSThomas Hellstrom 
638c74c162fSThomas Hellstrom static struct vmw_resource *
639c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base)
640c74c162fSThomas Hellstrom {
641c74c162fSThomas Hellstrom 	return &(container_of(base, struct vmw_user_shader, base)->
642c74c162fSThomas Hellstrom 		 shader.res);
643c74c162fSThomas Hellstrom }
644c74c162fSThomas Hellstrom 
645c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res)
646c74c162fSThomas Hellstrom {
647c74c162fSThomas Hellstrom 	struct vmw_user_shader *ushader =
648c74c162fSThomas Hellstrom 		container_of(res, struct vmw_user_shader, shader.res);
649c74c162fSThomas Hellstrom 
650c74c162fSThomas Hellstrom 	ttm_base_object_kfree(ushader, base);
651c74c162fSThomas Hellstrom }
652c74c162fSThomas Hellstrom 
65318e4a466SThomas Hellstrom static void vmw_shader_free(struct vmw_resource *res)
65418e4a466SThomas Hellstrom {
65518e4a466SThomas Hellstrom 	struct vmw_shader *shader = vmw_res_to_shader(res);
65618e4a466SThomas Hellstrom 
65718e4a466SThomas Hellstrom 	kfree(shader);
65818e4a466SThomas Hellstrom }
65918e4a466SThomas Hellstrom 
660ad2ae415SLee Jones /*
661c74c162fSThomas Hellstrom  * This function is called when user space has no more references on the
662c74c162fSThomas Hellstrom  * base object. It releases the base-object's reference on the resource object.
663c74c162fSThomas Hellstrom  */
664c74c162fSThomas Hellstrom 
665c74c162fSThomas Hellstrom static void vmw_user_shader_base_release(struct ttm_base_object **p_base)
666c74c162fSThomas Hellstrom {
667c74c162fSThomas Hellstrom 	struct ttm_base_object *base = *p_base;
668c74c162fSThomas Hellstrom 	struct vmw_resource *res = vmw_user_shader_base_to_res(base);
669c74c162fSThomas Hellstrom 
670c74c162fSThomas Hellstrom 	*p_base = NULL;
671c74c162fSThomas Hellstrom 	vmw_resource_unreference(&res);
672c74c162fSThomas Hellstrom }
673c74c162fSThomas Hellstrom 
674c74c162fSThomas Hellstrom int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
675c74c162fSThomas Hellstrom 			      struct drm_file *file_priv)
676c74c162fSThomas Hellstrom {
677c74c162fSThomas Hellstrom 	struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data;
678c74c162fSThomas Hellstrom 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
679c74c162fSThomas Hellstrom 
6808afa13a0SZack Rusin 	return ttm_ref_object_base_unref(tfile, arg->handle);
681c74c162fSThomas Hellstrom }
682c74c162fSThomas Hellstrom 
68318e4a466SThomas Hellstrom static int vmw_user_shader_alloc(struct vmw_private *dev_priv,
684*09881d29SZack Rusin 				 struct vmw_bo *buffer,
685d5bde956SThomas Hellstrom 				 size_t shader_size,
686d5bde956SThomas Hellstrom 				 size_t offset,
687d5bde956SThomas Hellstrom 				 SVGA3dShaderType shader_type,
688d80efd5cSThomas Hellstrom 				 uint8_t num_input_sig,
689d80efd5cSThomas Hellstrom 				 uint8_t num_output_sig,
690d5bde956SThomas Hellstrom 				 struct ttm_object_file *tfile,
691d5bde956SThomas Hellstrom 				 u32 *handle)
692d5bde956SThomas Hellstrom {
693d5bde956SThomas Hellstrom 	struct vmw_user_shader *ushader;
694d5bde956SThomas Hellstrom 	struct vmw_resource *res, *tmp;
695d5bde956SThomas Hellstrom 	int ret;
696d5bde956SThomas Hellstrom 
697d5bde956SThomas Hellstrom 	ushader = kzalloc(sizeof(*ushader), GFP_KERNEL);
6981a4adb05SRavikant B Sharma 	if (unlikely(!ushader)) {
699d5bde956SThomas Hellstrom 		ret = -ENOMEM;
700d5bde956SThomas Hellstrom 		goto out;
701d5bde956SThomas Hellstrom 	}
702d5bde956SThomas Hellstrom 
703d5bde956SThomas Hellstrom 	res = &ushader->shader.res;
704d5bde956SThomas Hellstrom 	ushader->base.shareable = false;
705d5bde956SThomas Hellstrom 	ushader->base.tfile = NULL;
706d5bde956SThomas Hellstrom 
707d5bde956SThomas Hellstrom 	/*
708d5bde956SThomas Hellstrom 	 * From here on, the destructor takes over resource freeing.
709d5bde956SThomas Hellstrom 	 */
710d5bde956SThomas Hellstrom 
711d5bde956SThomas Hellstrom 	ret = vmw_gb_shader_init(dev_priv, res, shader_size,
712d80efd5cSThomas Hellstrom 				 offset, shader_type, num_input_sig,
713d80efd5cSThomas Hellstrom 				 num_output_sig, buffer,
714d5bde956SThomas Hellstrom 				 vmw_user_shader_free);
715d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
716d5bde956SThomas Hellstrom 		goto out;
717d5bde956SThomas Hellstrom 
718d5bde956SThomas Hellstrom 	tmp = vmw_resource_reference(res);
719d5bde956SThomas Hellstrom 	ret = ttm_base_object_init(tfile, &ushader->base, false,
720d5bde956SThomas Hellstrom 				   VMW_RES_SHADER,
7218afa13a0SZack Rusin 				   &vmw_user_shader_base_release);
722d5bde956SThomas Hellstrom 
723d5bde956SThomas Hellstrom 	if (unlikely(ret != 0)) {
724d5bde956SThomas Hellstrom 		vmw_resource_unreference(&tmp);
725d5bde956SThomas Hellstrom 		goto out_err;
726d5bde956SThomas Hellstrom 	}
727d5bde956SThomas Hellstrom 
728d5bde956SThomas Hellstrom 	if (handle)
729c7eae626SThomas Hellstrom 		*handle = ushader->base.handle;
730d5bde956SThomas Hellstrom out_err:
731d5bde956SThomas Hellstrom 	vmw_resource_unreference(&res);
732d5bde956SThomas Hellstrom out:
733d5bde956SThomas Hellstrom 	return ret;
734d5bde956SThomas Hellstrom }
735d5bde956SThomas Hellstrom 
736d5bde956SThomas Hellstrom 
737b9eb1a61SThomas Hellstrom static struct vmw_resource *vmw_shader_alloc(struct vmw_private *dev_priv,
738*09881d29SZack Rusin 					     struct vmw_bo *buffer,
73918e4a466SThomas Hellstrom 					     size_t shader_size,
74018e4a466SThomas Hellstrom 					     size_t offset,
74118e4a466SThomas Hellstrom 					     SVGA3dShaderType shader_type)
74218e4a466SThomas Hellstrom {
74318e4a466SThomas Hellstrom 	struct vmw_shader *shader;
74418e4a466SThomas Hellstrom 	struct vmw_resource *res;
74518e4a466SThomas Hellstrom 	int ret;
74618e4a466SThomas Hellstrom 
74718e4a466SThomas Hellstrom 	shader = kzalloc(sizeof(*shader), GFP_KERNEL);
7481a4adb05SRavikant B Sharma 	if (unlikely(!shader)) {
74918e4a466SThomas Hellstrom 		ret = -ENOMEM;
75018e4a466SThomas Hellstrom 		goto out_err;
75118e4a466SThomas Hellstrom 	}
75218e4a466SThomas Hellstrom 
75318e4a466SThomas Hellstrom 	res = &shader->res;
75418e4a466SThomas Hellstrom 
75518e4a466SThomas Hellstrom 	/*
75618e4a466SThomas Hellstrom 	 * From here on, the destructor takes over resource freeing.
75718e4a466SThomas Hellstrom 	 */
75818e4a466SThomas Hellstrom 	ret = vmw_gb_shader_init(dev_priv, res, shader_size,
759d80efd5cSThomas Hellstrom 				 offset, shader_type, 0, 0, buffer,
76018e4a466SThomas Hellstrom 				 vmw_shader_free);
76118e4a466SThomas Hellstrom 
76218e4a466SThomas Hellstrom out_err:
76318e4a466SThomas Hellstrom 	return ret ? ERR_PTR(ret) : res;
76418e4a466SThomas Hellstrom }
76518e4a466SThomas Hellstrom 
76618e4a466SThomas Hellstrom 
767d80efd5cSThomas Hellstrom static int vmw_shader_define(struct drm_device *dev, struct drm_file *file_priv,
768d80efd5cSThomas Hellstrom 			     enum drm_vmw_shader_type shader_type_drm,
769d80efd5cSThomas Hellstrom 			     u32 buffer_handle, size_t size, size_t offset,
770d80efd5cSThomas Hellstrom 			     uint8_t num_input_sig, uint8_t num_output_sig,
771d80efd5cSThomas Hellstrom 			     uint32_t *shader_handle)
772c74c162fSThomas Hellstrom {
773c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = vmw_priv(dev);
774c74c162fSThomas Hellstrom 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
775*09881d29SZack Rusin 	struct vmw_bo *buffer = NULL;
776c74c162fSThomas Hellstrom 	SVGA3dShaderType shader_type;
777c74c162fSThomas Hellstrom 	int ret;
778c74c162fSThomas Hellstrom 
779d80efd5cSThomas Hellstrom 	if (buffer_handle != SVGA3D_INVALID_ID) {
7808afa13a0SZack Rusin 		ret = vmw_user_bo_lookup(file_priv, buffer_handle, &buffer);
781c74c162fSThomas Hellstrom 		if (unlikely(ret != 0)) {
7825724f899SDeepak Rawat 			VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n");
783c74c162fSThomas Hellstrom 			return ret;
784c74c162fSThomas Hellstrom 		}
785c74c162fSThomas Hellstrom 
786e11bfb99SChristian König 		if ((u64)buffer->base.base.size < (u64)size + (u64)offset) {
7875724f899SDeepak Rawat 			VMW_DEBUG_USER("Illegal buffer- or shader size.\n");
788c74c162fSThomas Hellstrom 			ret = -EINVAL;
789c74c162fSThomas Hellstrom 			goto out_bad_arg;
790c74c162fSThomas Hellstrom 		}
791c74c162fSThomas Hellstrom 	}
792c74c162fSThomas Hellstrom 
793d80efd5cSThomas Hellstrom 	switch (shader_type_drm) {
794c74c162fSThomas Hellstrom 	case drm_vmw_shader_type_vs:
795c74c162fSThomas Hellstrom 		shader_type = SVGA3D_SHADERTYPE_VS;
796c74c162fSThomas Hellstrom 		break;
797c74c162fSThomas Hellstrom 	case drm_vmw_shader_type_ps:
798c74c162fSThomas Hellstrom 		shader_type = SVGA3D_SHADERTYPE_PS;
799c74c162fSThomas Hellstrom 		break;
800c74c162fSThomas Hellstrom 	default:
8015724f899SDeepak Rawat 		VMW_DEBUG_USER("Illegal shader type.\n");
802c74c162fSThomas Hellstrom 		ret = -EINVAL;
803c74c162fSThomas Hellstrom 		goto out_bad_arg;
804c74c162fSThomas Hellstrom 	}
805c74c162fSThomas Hellstrom 
806d80efd5cSThomas Hellstrom 	ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset,
807d80efd5cSThomas Hellstrom 				    shader_type, num_input_sig,
808d80efd5cSThomas Hellstrom 				    num_output_sig, tfile, shader_handle);
809c74c162fSThomas Hellstrom out_bad_arg:
810f1d34bfdSThomas Hellstrom 	vmw_bo_unreference(&buffer);
811d5bde956SThomas Hellstrom 	return ret;
812d5bde956SThomas Hellstrom }
813c74c162fSThomas Hellstrom 
814d5bde956SThomas Hellstrom /**
815d80efd5cSThomas Hellstrom  * vmw_shader_id_ok - Check whether a compat shader user key and
81618e4a466SThomas Hellstrom  * shader type are within valid bounds.
817d5bde956SThomas Hellstrom  *
81818e4a466SThomas Hellstrom  * @user_key: User space id of the shader.
81918e4a466SThomas Hellstrom  * @shader_type: Shader type.
820d5bde956SThomas Hellstrom  *
82118e4a466SThomas Hellstrom  * Returns true if valid false if not.
822d5bde956SThomas Hellstrom  */
823d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type)
824d5bde956SThomas Hellstrom {
82518e4a466SThomas Hellstrom 	return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16;
826d5bde956SThomas Hellstrom }
827d5bde956SThomas Hellstrom 
828d5bde956SThomas Hellstrom /**
829d80efd5cSThomas Hellstrom  * vmw_shader_key - Compute a hash key suitable for a compat shader.
830d5bde956SThomas Hellstrom  *
83118e4a466SThomas Hellstrom  * @user_key: User space id of the shader.
83218e4a466SThomas Hellstrom  * @shader_type: Shader type.
833d5bde956SThomas Hellstrom  *
83418e4a466SThomas Hellstrom  * Returns a hash key suitable for a command buffer managed resource
83518e4a466SThomas Hellstrom  * manager hash table.
836d5bde956SThomas Hellstrom  */
837d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type)
838d5bde956SThomas Hellstrom {
83918e4a466SThomas Hellstrom 	return user_key | (shader_type << 20);
840d5bde956SThomas Hellstrom }
841d5bde956SThomas Hellstrom 
842d5bde956SThomas Hellstrom /**
843d80efd5cSThomas Hellstrom  * vmw_shader_remove - Stage a compat shader for removal.
844d5bde956SThomas Hellstrom  *
84518e4a466SThomas Hellstrom  * @man: Pointer to the compat shader manager identifying the shader namespace.
846d5bde956SThomas Hellstrom  * @user_key: The key that is used to identify the shader. The key is
847d5bde956SThomas Hellstrom  * unique to the shader type.
848d5bde956SThomas Hellstrom  * @shader_type: Shader type.
84918e4a466SThomas Hellstrom  * @list: Caller's list of staged command buffer resource actions.
850d5bde956SThomas Hellstrom  */
851d80efd5cSThomas Hellstrom int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
852d5bde956SThomas Hellstrom 		      u32 user_key, SVGA3dShaderType shader_type,
853d5bde956SThomas Hellstrom 		      struct list_head *list)
854d5bde956SThomas Hellstrom {
855d80efd5cSThomas Hellstrom 	struct vmw_resource *dummy;
856d80efd5cSThomas Hellstrom 
857d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
858d5bde956SThomas Hellstrom 		return -EINVAL;
859d5bde956SThomas Hellstrom 
860d80efd5cSThomas Hellstrom 	return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader,
861d80efd5cSThomas Hellstrom 				     vmw_shader_key(user_key, shader_type),
862d80efd5cSThomas Hellstrom 				     list, &dummy);
863d5bde956SThomas Hellstrom }
864d5bde956SThomas Hellstrom 
865d5bde956SThomas Hellstrom /**
86618e4a466SThomas Hellstrom  * vmw_compat_shader_add - Create a compat shader and stage it for addition
86718e4a466SThomas Hellstrom  * as a command buffer managed resource.
868d5bde956SThomas Hellstrom  *
869ad2ae415SLee Jones  * @dev_priv: Pointer to device private structure.
87018e4a466SThomas Hellstrom  * @man: Pointer to the compat shader manager identifying the shader namespace.
871d5bde956SThomas Hellstrom  * @user_key: The key that is used to identify the shader. The key is
872d5bde956SThomas Hellstrom  * unique to the shader type.
873d5bde956SThomas Hellstrom  * @bytecode: Pointer to the bytecode of the shader.
874d5bde956SThomas Hellstrom  * @shader_type: Shader type.
875ad2ae415SLee Jones  * @size: Command size.
87618e4a466SThomas Hellstrom  * @list: Caller's list of staged command buffer resource actions.
877d5bde956SThomas Hellstrom  *
878d5bde956SThomas Hellstrom  */
87918e4a466SThomas Hellstrom int vmw_compat_shader_add(struct vmw_private *dev_priv,
88018e4a466SThomas Hellstrom 			  struct vmw_cmdbuf_res_manager *man,
881d5bde956SThomas Hellstrom 			  u32 user_key, const void *bytecode,
882d5bde956SThomas Hellstrom 			  SVGA3dShaderType shader_type,
883d5bde956SThomas Hellstrom 			  size_t size,
884d5bde956SThomas Hellstrom 			  struct list_head *list)
885d5bde956SThomas Hellstrom {
88619be5570SChristian König 	struct ttm_operation_ctx ctx = { false, true };
887*09881d29SZack Rusin 	struct vmw_bo *buf;
888d5bde956SThomas Hellstrom 	struct ttm_bo_kmap_obj map;
889d5bde956SThomas Hellstrom 	bool is_iomem;
890d5bde956SThomas Hellstrom 	int ret;
89118e4a466SThomas Hellstrom 	struct vmw_resource *res;
892d5bde956SThomas Hellstrom 
893d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
894d5bde956SThomas Hellstrom 		return -EINVAL;
895d5bde956SThomas Hellstrom 
8968afa13a0SZack Rusin 	ret = vmw_bo_create(dev_priv, size, &vmw_sys_placement,
8976b2e8aa4SZack Rusin 			    true, true, &buf);
898d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
899d5bde956SThomas Hellstrom 		goto out;
900d5bde956SThomas Hellstrom 
901dfd5e50eSChristian König 	ret = ttm_bo_reserve(&buf->base, false, true, NULL);
902d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
903d5bde956SThomas Hellstrom 		goto no_reserve;
904d5bde956SThomas Hellstrom 
905d5bde956SThomas Hellstrom 	/* Map and copy shader bytecode. */
906bc65754cSCai Huoqing 	ret = ttm_bo_kmap(&buf->base, 0, PFN_UP(size), &map);
907d5bde956SThomas Hellstrom 	if (unlikely(ret != 0)) {
908d5bde956SThomas Hellstrom 		ttm_bo_unreserve(&buf->base);
909d5bde956SThomas Hellstrom 		goto no_reserve;
910d5bde956SThomas Hellstrom 	}
911d5bde956SThomas Hellstrom 
912d5bde956SThomas Hellstrom 	memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size);
913d5bde956SThomas Hellstrom 	WARN_ON(is_iomem);
914d5bde956SThomas Hellstrom 
915d5bde956SThomas Hellstrom 	ttm_bo_kunmap(&map);
91619be5570SChristian König 	ret = ttm_bo_validate(&buf->base, &vmw_sys_placement, &ctx);
917d5bde956SThomas Hellstrom 	WARN_ON(ret != 0);
918d5bde956SThomas Hellstrom 	ttm_bo_unreserve(&buf->base);
919d5bde956SThomas Hellstrom 
92018e4a466SThomas Hellstrom 	res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type);
921d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
922d5bde956SThomas Hellstrom 		goto no_reserve;
923d5bde956SThomas Hellstrom 
924d80efd5cSThomas Hellstrom 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
925d80efd5cSThomas Hellstrom 				 vmw_shader_key(user_key, shader_type),
92618e4a466SThomas Hellstrom 				 res, list);
92718e4a466SThomas Hellstrom 	vmw_resource_unreference(&res);
928d5bde956SThomas Hellstrom no_reserve:
929f1d34bfdSThomas Hellstrom 	vmw_bo_unreference(&buf);
930d5bde956SThomas Hellstrom out:
931d5bde956SThomas Hellstrom 	return ret;
932d5bde956SThomas Hellstrom }
933d5bde956SThomas Hellstrom 
934d5bde956SThomas Hellstrom /**
935d80efd5cSThomas Hellstrom  * vmw_shader_lookup - Look up a compat shader
936d5bde956SThomas Hellstrom  *
93718e4a466SThomas Hellstrom  * @man: Pointer to the command buffer managed resource manager identifying
93818e4a466SThomas Hellstrom  * the shader namespace.
93918e4a466SThomas Hellstrom  * @user_key: The user space id of the shader.
94018e4a466SThomas Hellstrom  * @shader_type: The shader type.
941d5bde956SThomas Hellstrom  *
94218e4a466SThomas Hellstrom  * Returns a refcounted pointer to a struct vmw_resource if the shader was
94318e4a466SThomas Hellstrom  * found. An error pointer otherwise.
944d5bde956SThomas Hellstrom  */
94518e4a466SThomas Hellstrom struct vmw_resource *
946d80efd5cSThomas Hellstrom vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
94718e4a466SThomas Hellstrom 		  u32 user_key,
94818e4a466SThomas Hellstrom 		  SVGA3dShaderType shader_type)
949d5bde956SThomas Hellstrom {
950d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
95118e4a466SThomas Hellstrom 		return ERR_PTR(-EINVAL);
952d5bde956SThomas Hellstrom 
953d80efd5cSThomas Hellstrom 	return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader,
954d80efd5cSThomas Hellstrom 				     vmw_shader_key(user_key, shader_type));
955d80efd5cSThomas Hellstrom }
956d80efd5cSThomas Hellstrom 
957d80efd5cSThomas Hellstrom int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
958d80efd5cSThomas Hellstrom 			     struct drm_file *file_priv)
959d80efd5cSThomas Hellstrom {
960d80efd5cSThomas Hellstrom 	struct drm_vmw_shader_create_arg *arg =
961d80efd5cSThomas Hellstrom 		(struct drm_vmw_shader_create_arg *)data;
962d80efd5cSThomas Hellstrom 
963d80efd5cSThomas Hellstrom 	return vmw_shader_define(dev, file_priv, arg->shader_type,
964d80efd5cSThomas Hellstrom 				 arg->buffer_handle,
965d80efd5cSThomas Hellstrom 				 arg->size, arg->offset,
966d80efd5cSThomas Hellstrom 				 0, 0,
967d80efd5cSThomas Hellstrom 				 &arg->shader_handle);
968c74c162fSThomas Hellstrom }
969