1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
2c74c162fSThomas Hellstrom /**************************************************************************
3c74c162fSThomas Hellstrom  *
4dff96888SDirk Hohndel (VMware)  * Copyright 2009-2015 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 
30c74c162fSThomas Hellstrom #include "vmwgfx_drv.h"
31c74c162fSThomas Hellstrom #include "vmwgfx_resource_priv.h"
32d80efd5cSThomas Hellstrom #include "vmwgfx_binding.h"
33c74c162fSThomas Hellstrom 
34c74c162fSThomas Hellstrom struct vmw_shader {
35c74c162fSThomas Hellstrom 	struct vmw_resource res;
36c74c162fSThomas Hellstrom 	SVGA3dShaderType type;
37c74c162fSThomas Hellstrom 	uint32_t size;
38d80efd5cSThomas Hellstrom 	uint8_t num_input_sig;
39d80efd5cSThomas Hellstrom 	uint8_t num_output_sig;
40c74c162fSThomas Hellstrom };
41c74c162fSThomas Hellstrom 
42c74c162fSThomas Hellstrom struct vmw_user_shader {
43c74c162fSThomas Hellstrom 	struct ttm_base_object base;
44c74c162fSThomas Hellstrom 	struct vmw_shader shader;
45c74c162fSThomas Hellstrom };
46c74c162fSThomas Hellstrom 
47d80efd5cSThomas Hellstrom struct vmw_dx_shader {
48d80efd5cSThomas Hellstrom 	struct vmw_resource res;
49d80efd5cSThomas Hellstrom 	struct vmw_resource *ctx;
50d80efd5cSThomas Hellstrom 	struct vmw_resource *cotable;
51d80efd5cSThomas Hellstrom 	u32 id;
52d80efd5cSThomas Hellstrom 	bool committed;
53d80efd5cSThomas Hellstrom 	struct list_head cotable_head;
54d80efd5cSThomas Hellstrom };
55d80efd5cSThomas Hellstrom 
56c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res);
57c74c162fSThomas Hellstrom static struct vmw_resource *
58c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base);
59c74c162fSThomas Hellstrom 
60c74c162fSThomas Hellstrom static int vmw_gb_shader_create(struct vmw_resource *res);
61c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res,
62c74c162fSThomas Hellstrom 			       struct ttm_validate_buffer *val_buf);
63c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res,
64c74c162fSThomas Hellstrom 				 bool readback,
65c74c162fSThomas Hellstrom 				 struct ttm_validate_buffer *val_buf);
66c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res);
67c74c162fSThomas Hellstrom 
68d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res);
69d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res,
70d80efd5cSThomas Hellstrom 			       struct ttm_validate_buffer *val_buf);
71d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res,
72d80efd5cSThomas Hellstrom 				 bool readback,
73d80efd5cSThomas Hellstrom 				 struct ttm_validate_buffer *val_buf);
74d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
75d80efd5cSThomas Hellstrom 					enum vmw_cmdbuf_res_state state);
76d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type);
77d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type);
78d80efd5cSThomas Hellstrom 
79c74c162fSThomas Hellstrom static const struct vmw_user_resource_conv user_shader_conv = {
80c74c162fSThomas Hellstrom 	.object_type = VMW_RES_SHADER,
81c74c162fSThomas Hellstrom 	.base_obj_to_res = vmw_user_shader_base_to_res,
82c74c162fSThomas Hellstrom 	.res_free = vmw_user_shader_free
83c74c162fSThomas Hellstrom };
84c74c162fSThomas Hellstrom 
85c74c162fSThomas Hellstrom const struct vmw_user_resource_conv *user_shader_converter =
86c74c162fSThomas Hellstrom 	&user_shader_conv;
87c74c162fSThomas Hellstrom 
88c74c162fSThomas Hellstrom 
89c74c162fSThomas Hellstrom static const struct vmw_res_func vmw_gb_shader_func = {
90c74c162fSThomas Hellstrom 	.res_type = vmw_res_shader,
91c74c162fSThomas Hellstrom 	.needs_backup = true,
92c74c162fSThomas Hellstrom 	.may_evict = true,
93a0a63940SThomas Hellstrom 	.prio = 3,
94a0a63940SThomas Hellstrom 	.dirty_prio = 3,
95c74c162fSThomas Hellstrom 	.type_name = "guest backed shaders",
96c74c162fSThomas Hellstrom 	.backup_placement = &vmw_mob_placement,
97c74c162fSThomas Hellstrom 	.create = vmw_gb_shader_create,
98c74c162fSThomas Hellstrom 	.destroy = vmw_gb_shader_destroy,
99c74c162fSThomas Hellstrom 	.bind = vmw_gb_shader_bind,
100c74c162fSThomas Hellstrom 	.unbind = vmw_gb_shader_unbind
101c74c162fSThomas Hellstrom };
102c74c162fSThomas Hellstrom 
103d80efd5cSThomas Hellstrom static const struct vmw_res_func vmw_dx_shader_func = {
104d80efd5cSThomas Hellstrom 	.res_type = vmw_res_shader,
105d80efd5cSThomas Hellstrom 	.needs_backup = true,
106a0a63940SThomas Hellstrom 	.may_evict = true,
107a0a63940SThomas Hellstrom 	.prio = 3,
108a0a63940SThomas Hellstrom 	.dirty_prio = 3,
109d80efd5cSThomas Hellstrom 	.type_name = "dx shaders",
110d80efd5cSThomas Hellstrom 	.backup_placement = &vmw_mob_placement,
111d80efd5cSThomas Hellstrom 	.create = vmw_dx_shader_create,
112d80efd5cSThomas Hellstrom 	/*
113d80efd5cSThomas Hellstrom 	 * The destroy callback is only called with a committed resource on
114d80efd5cSThomas Hellstrom 	 * context destroy, in which case we destroy the cotable anyway,
115d80efd5cSThomas Hellstrom 	 * so there's no need to destroy DX shaders separately.
116d80efd5cSThomas Hellstrom 	 */
117d80efd5cSThomas Hellstrom 	.destroy = NULL,
118d80efd5cSThomas Hellstrom 	.bind = vmw_dx_shader_bind,
119d80efd5cSThomas Hellstrom 	.unbind = vmw_dx_shader_unbind,
120d80efd5cSThomas Hellstrom 	.commit_notify = vmw_dx_shader_commit_notify,
121d80efd5cSThomas Hellstrom };
122d80efd5cSThomas Hellstrom 
123ad2ae415SLee Jones /*
124c74c162fSThomas Hellstrom  * Shader management:
125c74c162fSThomas Hellstrom  */
126c74c162fSThomas Hellstrom 
127c74c162fSThomas Hellstrom static inline struct vmw_shader *
128c74c162fSThomas Hellstrom vmw_res_to_shader(struct vmw_resource *res)
129c74c162fSThomas Hellstrom {
130c74c162fSThomas Hellstrom 	return container_of(res, struct vmw_shader, res);
131c74c162fSThomas Hellstrom }
132c74c162fSThomas Hellstrom 
133d80efd5cSThomas Hellstrom /**
134d80efd5cSThomas Hellstrom  * vmw_res_to_dx_shader - typecast a struct vmw_resource to a
135d80efd5cSThomas Hellstrom  * struct vmw_dx_shader
136d80efd5cSThomas Hellstrom  *
137d80efd5cSThomas Hellstrom  * @res: Pointer to the struct vmw_resource.
138d80efd5cSThomas Hellstrom  */
139d80efd5cSThomas Hellstrom static inline struct vmw_dx_shader *
140d80efd5cSThomas Hellstrom vmw_res_to_dx_shader(struct vmw_resource *res)
141d80efd5cSThomas Hellstrom {
142d80efd5cSThomas Hellstrom 	return container_of(res, struct vmw_dx_shader, res);
143d80efd5cSThomas Hellstrom }
144d80efd5cSThomas Hellstrom 
145c74c162fSThomas Hellstrom static void vmw_hw_shader_destroy(struct vmw_resource *res)
146c74c162fSThomas Hellstrom {
147d80efd5cSThomas Hellstrom 	if (likely(res->func->destroy))
148d80efd5cSThomas Hellstrom 		(void) res->func->destroy(res);
149d80efd5cSThomas Hellstrom 	else
150d80efd5cSThomas Hellstrom 		res->id = -1;
151c74c162fSThomas Hellstrom }
152c74c162fSThomas Hellstrom 
153d80efd5cSThomas Hellstrom 
154c74c162fSThomas Hellstrom static int vmw_gb_shader_init(struct vmw_private *dev_priv,
155c74c162fSThomas Hellstrom 			      struct vmw_resource *res,
156c74c162fSThomas Hellstrom 			      uint32_t size,
157c74c162fSThomas Hellstrom 			      uint64_t offset,
158c74c162fSThomas Hellstrom 			      SVGA3dShaderType type,
159d80efd5cSThomas Hellstrom 			      uint8_t num_input_sig,
160d80efd5cSThomas Hellstrom 			      uint8_t num_output_sig,
161f1d34bfdSThomas Hellstrom 			      struct vmw_buffer_object *byte_code,
162c74c162fSThomas Hellstrom 			      void (*res_free) (struct vmw_resource *res))
163c74c162fSThomas Hellstrom {
164c74c162fSThomas Hellstrom 	struct vmw_shader *shader = vmw_res_to_shader(res);
165c74c162fSThomas Hellstrom 	int ret;
166c74c162fSThomas Hellstrom 
167d80efd5cSThomas Hellstrom 	ret = vmw_resource_init(dev_priv, res, true, res_free,
168d80efd5cSThomas Hellstrom 				&vmw_gb_shader_func);
169c74c162fSThomas Hellstrom 
170c74c162fSThomas Hellstrom 	if (unlikely(ret != 0)) {
171c74c162fSThomas Hellstrom 		if (res_free)
172c74c162fSThomas Hellstrom 			res_free(res);
173c74c162fSThomas Hellstrom 		else
174c74c162fSThomas Hellstrom 			kfree(res);
175c74c162fSThomas Hellstrom 		return ret;
176c74c162fSThomas Hellstrom 	}
177c74c162fSThomas Hellstrom 
178c74c162fSThomas Hellstrom 	res->backup_size = size;
179c74c162fSThomas Hellstrom 	if (byte_code) {
180f1d34bfdSThomas Hellstrom 		res->backup = vmw_bo_reference(byte_code);
181c74c162fSThomas Hellstrom 		res->backup_offset = offset;
182c74c162fSThomas Hellstrom 	}
183c74c162fSThomas Hellstrom 	shader->size = size;
184c74c162fSThomas Hellstrom 	shader->type = type;
185d80efd5cSThomas Hellstrom 	shader->num_input_sig = num_input_sig;
186d80efd5cSThomas Hellstrom 	shader->num_output_sig = num_output_sig;
187c74c162fSThomas Hellstrom 
18813289241SThomas Hellstrom 	res->hw_destroy = vmw_hw_shader_destroy;
189c74c162fSThomas Hellstrom 	return 0;
190c74c162fSThomas Hellstrom }
191c74c162fSThomas Hellstrom 
192d80efd5cSThomas Hellstrom /*
193d80efd5cSThomas Hellstrom  * GB shader code:
194d80efd5cSThomas Hellstrom  */
195d80efd5cSThomas Hellstrom 
196c74c162fSThomas Hellstrom static int vmw_gb_shader_create(struct vmw_resource *res)
197c74c162fSThomas Hellstrom {
198c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
199c74c162fSThomas Hellstrom 	struct vmw_shader *shader = vmw_res_to_shader(res);
200c74c162fSThomas Hellstrom 	int ret;
201c74c162fSThomas Hellstrom 	struct {
202c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
203c74c162fSThomas Hellstrom 		SVGA3dCmdDefineGBShader body;
204c74c162fSThomas Hellstrom 	} *cmd;
205c74c162fSThomas Hellstrom 
206c74c162fSThomas Hellstrom 	if (likely(res->id != -1))
207c74c162fSThomas Hellstrom 		return 0;
208c74c162fSThomas Hellstrom 
209c74c162fSThomas Hellstrom 	ret = vmw_resource_alloc_id(res);
210c74c162fSThomas Hellstrom 	if (unlikely(ret != 0)) {
211c74c162fSThomas Hellstrom 		DRM_ERROR("Failed to allocate a shader id.\n");
212c74c162fSThomas Hellstrom 		goto out_no_id;
213c74c162fSThomas Hellstrom 	}
214c74c162fSThomas Hellstrom 
215c74c162fSThomas Hellstrom 	if (unlikely(res->id >= VMWGFX_NUM_GB_SHADER)) {
216c74c162fSThomas Hellstrom 		ret = -EBUSY;
217c74c162fSThomas Hellstrom 		goto out_no_fifo;
218c74c162fSThomas Hellstrom 	}
219c74c162fSThomas Hellstrom 
2208426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
221c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
222c74c162fSThomas Hellstrom 		ret = -ENOMEM;
223c74c162fSThomas Hellstrom 		goto out_no_fifo;
224c74c162fSThomas Hellstrom 	}
225c74c162fSThomas Hellstrom 
226c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER;
227c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
228c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
229c74c162fSThomas Hellstrom 	cmd->body.type = shader->type;
230c74c162fSThomas Hellstrom 	cmd->body.sizeInBytes = shader->size;
2318426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
232153b3d5bSThomas Hellstrom 	vmw_fifo_resource_inc(dev_priv);
233c74c162fSThomas Hellstrom 
234c74c162fSThomas Hellstrom 	return 0;
235c74c162fSThomas Hellstrom 
236c74c162fSThomas Hellstrom out_no_fifo:
237c74c162fSThomas Hellstrom 	vmw_resource_release_id(res);
238c74c162fSThomas Hellstrom out_no_id:
239c74c162fSThomas Hellstrom 	return ret;
240c74c162fSThomas Hellstrom }
241c74c162fSThomas Hellstrom 
242c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res,
243c74c162fSThomas Hellstrom 			      struct ttm_validate_buffer *val_buf)
244c74c162fSThomas Hellstrom {
245c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
246c74c162fSThomas Hellstrom 	struct {
247c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
248c74c162fSThomas Hellstrom 		SVGA3dCmdBindGBShader body;
249c74c162fSThomas Hellstrom 	} *cmd;
250c74c162fSThomas Hellstrom 	struct ttm_buffer_object *bo = val_buf->bo;
251c74c162fSThomas Hellstrom 
252d3116756SChristian König 	BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
253c74c162fSThomas Hellstrom 
2548426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
25511c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
256c74c162fSThomas Hellstrom 		return -ENOMEM;
257c74c162fSThomas Hellstrom 
258c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
259c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
260c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
261d3116756SChristian König 	cmd->body.mobid = bo->resource->start;
262b8ccd1e4SThomas Hellstrom 	cmd->body.offsetInBytes = res->backup_offset;
263c74c162fSThomas Hellstrom 	res->backup_dirty = false;
2648426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
265c74c162fSThomas Hellstrom 
266c74c162fSThomas Hellstrom 	return 0;
267c74c162fSThomas Hellstrom }
268c74c162fSThomas Hellstrom 
269c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res,
270c74c162fSThomas Hellstrom 				bool readback,
271c74c162fSThomas Hellstrom 				struct ttm_validate_buffer *val_buf)
272c74c162fSThomas Hellstrom {
273c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
274c74c162fSThomas Hellstrom 	struct {
275c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
276c74c162fSThomas Hellstrom 		SVGA3dCmdBindGBShader body;
277c74c162fSThomas Hellstrom 	} *cmd;
278c74c162fSThomas Hellstrom 	struct vmw_fence_obj *fence;
279c74c162fSThomas Hellstrom 
280d3116756SChristian König 	BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB);
281c74c162fSThomas Hellstrom 
2828426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
28311c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
284c74c162fSThomas Hellstrom 		return -ENOMEM;
285c74c162fSThomas Hellstrom 
286c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
287c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
288c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
289c74c162fSThomas Hellstrom 	cmd->body.mobid = SVGA3D_INVALID_ID;
290c74c162fSThomas Hellstrom 	cmd->body.offsetInBytes = 0;
2918426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
292c74c162fSThomas Hellstrom 
293c74c162fSThomas Hellstrom 	/*
294c74c162fSThomas Hellstrom 	 * Create a fence object and fence the backup buffer.
295c74c162fSThomas Hellstrom 	 */
296c74c162fSThomas Hellstrom 
297c74c162fSThomas Hellstrom 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
298c74c162fSThomas Hellstrom 					  &fence, NULL);
299c74c162fSThomas Hellstrom 
300e9431ea5SThomas Hellstrom 	vmw_bo_fence_single(val_buf->bo, fence);
301c74c162fSThomas Hellstrom 
302c74c162fSThomas Hellstrom 	if (likely(fence != NULL))
303c74c162fSThomas Hellstrom 		vmw_fence_obj_unreference(&fence);
304c74c162fSThomas Hellstrom 
305c74c162fSThomas Hellstrom 	return 0;
306c74c162fSThomas Hellstrom }
307c74c162fSThomas Hellstrom 
308c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res)
309c74c162fSThomas Hellstrom {
310c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
311c74c162fSThomas Hellstrom 	struct {
312c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
313c74c162fSThomas Hellstrom 		SVGA3dCmdDestroyGBShader body;
314c74c162fSThomas Hellstrom 	} *cmd;
315c74c162fSThomas Hellstrom 
316c74c162fSThomas Hellstrom 	if (likely(res->id == -1))
317c74c162fSThomas Hellstrom 		return 0;
318c74c162fSThomas Hellstrom 
319173fb7d4SThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
320d80efd5cSThomas Hellstrom 	vmw_binding_res_list_scrub(&res->binding_head);
321173fb7d4SThomas Hellstrom 
3228426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
323c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
3243e894a62SThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
325c74c162fSThomas Hellstrom 		return -ENOMEM;
326c74c162fSThomas Hellstrom 	}
327c74c162fSThomas Hellstrom 
328c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER;
329c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
330c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
3318426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
332173fb7d4SThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
333c74c162fSThomas Hellstrom 	vmw_resource_release_id(res);
334153b3d5bSThomas Hellstrom 	vmw_fifo_resource_dec(dev_priv);
335c74c162fSThomas Hellstrom 
336c74c162fSThomas Hellstrom 	return 0;
337c74c162fSThomas Hellstrom }
338c74c162fSThomas Hellstrom 
339d80efd5cSThomas Hellstrom /*
340d80efd5cSThomas Hellstrom  * DX shader code:
341d80efd5cSThomas Hellstrom  */
342d80efd5cSThomas Hellstrom 
343d80efd5cSThomas Hellstrom /**
344d80efd5cSThomas Hellstrom  * vmw_dx_shader_commit_notify - Notify that a shader operation has been
345d80efd5cSThomas Hellstrom  * committed to hardware from a user-supplied command stream.
346d80efd5cSThomas Hellstrom  *
347d80efd5cSThomas Hellstrom  * @res: Pointer to the shader resource.
348d80efd5cSThomas Hellstrom  * @state: Indicating whether a creation or removal has been committed.
349d80efd5cSThomas Hellstrom  *
350d80efd5cSThomas Hellstrom  */
351d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
352d80efd5cSThomas Hellstrom 					enum vmw_cmdbuf_res_state state)
353d80efd5cSThomas Hellstrom {
354d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
355d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
356d80efd5cSThomas Hellstrom 
357d80efd5cSThomas Hellstrom 	if (state == VMW_CMDBUF_RES_ADD) {
358d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
359d80efd5cSThomas Hellstrom 		vmw_cotable_add_resource(shader->cotable,
360d80efd5cSThomas Hellstrom 					 &shader->cotable_head);
361d80efd5cSThomas Hellstrom 		shader->committed = true;
362d80efd5cSThomas Hellstrom 		res->id = shader->id;
363d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
364d80efd5cSThomas Hellstrom 	} else {
365d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
366d80efd5cSThomas Hellstrom 		list_del_init(&shader->cotable_head);
367d80efd5cSThomas Hellstrom 		shader->committed = false;
368d80efd5cSThomas Hellstrom 		res->id = -1;
369d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
370d80efd5cSThomas Hellstrom 	}
371d80efd5cSThomas Hellstrom }
372d80efd5cSThomas Hellstrom 
373d80efd5cSThomas Hellstrom /**
374d80efd5cSThomas Hellstrom  * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader.
375d80efd5cSThomas Hellstrom  *
376d80efd5cSThomas Hellstrom  * @res: The shader resource
377d80efd5cSThomas Hellstrom  *
378d80efd5cSThomas Hellstrom  * This function reverts a scrub operation.
379d80efd5cSThomas Hellstrom  */
380d80efd5cSThomas Hellstrom static int vmw_dx_shader_unscrub(struct vmw_resource *res)
381d80efd5cSThomas Hellstrom {
382d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
383d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
384d80efd5cSThomas Hellstrom 	struct {
385d80efd5cSThomas Hellstrom 		SVGA3dCmdHeader header;
386d80efd5cSThomas Hellstrom 		SVGA3dCmdDXBindShader body;
387d80efd5cSThomas Hellstrom 	} *cmd;
388d80efd5cSThomas Hellstrom 
389d80efd5cSThomas Hellstrom 	if (!list_empty(&shader->cotable_head) || !shader->committed)
390d80efd5cSThomas Hellstrom 		return 0;
391d80efd5cSThomas Hellstrom 
3928426ed9cSZack Rusin 	cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), shader->ctx->id);
39311c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
394d80efd5cSThomas Hellstrom 		return -ENOMEM;
395d80efd5cSThomas Hellstrom 
396d80efd5cSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
397d80efd5cSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
398d80efd5cSThomas Hellstrom 	cmd->body.cid = shader->ctx->id;
399d80efd5cSThomas Hellstrom 	cmd->body.shid = shader->id;
400d3116756SChristian König 	cmd->body.mobid = res->backup->base.resource->start;
401d80efd5cSThomas Hellstrom 	cmd->body.offsetInBytes = res->backup_offset;
4028426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
403d80efd5cSThomas Hellstrom 
404d80efd5cSThomas Hellstrom 	vmw_cotable_add_resource(shader->cotable, &shader->cotable_head);
405d80efd5cSThomas Hellstrom 
406d80efd5cSThomas Hellstrom 	return 0;
407d80efd5cSThomas Hellstrom }
408d80efd5cSThomas Hellstrom 
409d80efd5cSThomas Hellstrom /**
410d80efd5cSThomas Hellstrom  * vmw_dx_shader_create - The DX shader create callback
411d80efd5cSThomas Hellstrom  *
412d80efd5cSThomas Hellstrom  * @res: The DX shader resource
413d80efd5cSThomas Hellstrom  *
414d80efd5cSThomas Hellstrom  * The create callback is called as part of resource validation and
415d80efd5cSThomas Hellstrom  * makes sure that we unscrub the shader if it's previously been scrubbed.
416d80efd5cSThomas Hellstrom  */
417d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res)
418d80efd5cSThomas Hellstrom {
419d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
420d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
421d80efd5cSThomas Hellstrom 	int ret = 0;
422d80efd5cSThomas Hellstrom 
423d80efd5cSThomas Hellstrom 	WARN_ON_ONCE(!shader->committed);
424d80efd5cSThomas Hellstrom 
425a0a63940SThomas Hellstrom 	if (vmw_resource_mob_attached(res)) {
426d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
427d80efd5cSThomas Hellstrom 		ret = vmw_dx_shader_unscrub(res);
428d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
429d80efd5cSThomas Hellstrom 	}
430d80efd5cSThomas Hellstrom 
431d80efd5cSThomas Hellstrom 	res->id = shader->id;
432d80efd5cSThomas Hellstrom 	return ret;
433d80efd5cSThomas Hellstrom }
434d80efd5cSThomas Hellstrom 
435d80efd5cSThomas Hellstrom /**
436d80efd5cSThomas Hellstrom  * vmw_dx_shader_bind - The DX shader bind callback
437d80efd5cSThomas Hellstrom  *
438d80efd5cSThomas Hellstrom  * @res: The DX shader resource
439d80efd5cSThomas Hellstrom  * @val_buf: Pointer to the validate buffer.
440d80efd5cSThomas Hellstrom  *
441d80efd5cSThomas Hellstrom  */
442d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res,
443d80efd5cSThomas Hellstrom 			      struct ttm_validate_buffer *val_buf)
444d80efd5cSThomas Hellstrom {
445d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
446d80efd5cSThomas Hellstrom 	struct ttm_buffer_object *bo = val_buf->bo;
447d80efd5cSThomas Hellstrom 
448d3116756SChristian König 	BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
449d80efd5cSThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
450d80efd5cSThomas Hellstrom 	vmw_dx_shader_unscrub(res);
451d80efd5cSThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
452d80efd5cSThomas Hellstrom 
453d80efd5cSThomas Hellstrom 	return 0;
454d80efd5cSThomas Hellstrom }
455d80efd5cSThomas Hellstrom 
456d80efd5cSThomas Hellstrom /**
457d80efd5cSThomas Hellstrom  * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader.
458d80efd5cSThomas Hellstrom  *
459d80efd5cSThomas Hellstrom  * @res: The shader resource
460d80efd5cSThomas Hellstrom  *
461d80efd5cSThomas Hellstrom  * This function unbinds a MOB from the DX shader without requiring the
462d80efd5cSThomas Hellstrom  * MOB dma_buffer to be reserved. The driver still considers the MOB bound.
463d80efd5cSThomas Hellstrom  * However, once the driver eventually decides to unbind the MOB, it doesn't
464d80efd5cSThomas Hellstrom  * need to access the context.
465d80efd5cSThomas Hellstrom  */
466d80efd5cSThomas Hellstrom static int vmw_dx_shader_scrub(struct vmw_resource *res)
467d80efd5cSThomas Hellstrom {
468d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
469d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
470d80efd5cSThomas Hellstrom 	struct {
471d80efd5cSThomas Hellstrom 		SVGA3dCmdHeader header;
472d80efd5cSThomas Hellstrom 		SVGA3dCmdDXBindShader body;
473d80efd5cSThomas Hellstrom 	} *cmd;
474d80efd5cSThomas Hellstrom 
475d80efd5cSThomas Hellstrom 	if (list_empty(&shader->cotable_head))
476d80efd5cSThomas Hellstrom 		return 0;
477d80efd5cSThomas Hellstrom 
478d80efd5cSThomas Hellstrom 	WARN_ON_ONCE(!shader->committed);
4798426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
48011c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
481d80efd5cSThomas Hellstrom 		return -ENOMEM;
482d80efd5cSThomas Hellstrom 
483d80efd5cSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
484d80efd5cSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
485d80efd5cSThomas Hellstrom 	cmd->body.cid = shader->ctx->id;
486d80efd5cSThomas Hellstrom 	cmd->body.shid = res->id;
487d80efd5cSThomas Hellstrom 	cmd->body.mobid = SVGA3D_INVALID_ID;
488d80efd5cSThomas Hellstrom 	cmd->body.offsetInBytes = 0;
4898426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
490d80efd5cSThomas Hellstrom 	res->id = -1;
491d80efd5cSThomas Hellstrom 	list_del_init(&shader->cotable_head);
492d80efd5cSThomas Hellstrom 
493d80efd5cSThomas Hellstrom 	return 0;
494d80efd5cSThomas Hellstrom }
495d80efd5cSThomas Hellstrom 
496d80efd5cSThomas Hellstrom /**
497d80efd5cSThomas Hellstrom  * vmw_dx_shader_unbind - The dx shader unbind callback.
498d80efd5cSThomas Hellstrom  *
499d80efd5cSThomas Hellstrom  * @res: The shader resource
500d80efd5cSThomas Hellstrom  * @readback: Whether this is a readback unbind. Currently unused.
501d80efd5cSThomas Hellstrom  * @val_buf: MOB buffer information.
502d80efd5cSThomas Hellstrom  */
503d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res,
504d80efd5cSThomas Hellstrom 				bool readback,
505d80efd5cSThomas Hellstrom 				struct ttm_validate_buffer *val_buf)
506d80efd5cSThomas Hellstrom {
507d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
508d80efd5cSThomas Hellstrom 	struct vmw_fence_obj *fence;
509d80efd5cSThomas Hellstrom 	int ret;
510d80efd5cSThomas Hellstrom 
511d3116756SChristian König 	BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB);
512d80efd5cSThomas Hellstrom 
513d80efd5cSThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
514d80efd5cSThomas Hellstrom 	ret = vmw_dx_shader_scrub(res);
515d80efd5cSThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
516d80efd5cSThomas Hellstrom 
517d80efd5cSThomas Hellstrom 	if (ret)
518d80efd5cSThomas Hellstrom 		return ret;
519d80efd5cSThomas Hellstrom 
520d80efd5cSThomas Hellstrom 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
521d80efd5cSThomas Hellstrom 					  &fence, NULL);
522e9431ea5SThomas Hellstrom 	vmw_bo_fence_single(val_buf->bo, fence);
523d80efd5cSThomas Hellstrom 
524d80efd5cSThomas Hellstrom 	if (likely(fence != NULL))
525d80efd5cSThomas Hellstrom 		vmw_fence_obj_unreference(&fence);
526d80efd5cSThomas Hellstrom 
527d80efd5cSThomas Hellstrom 	return 0;
528d80efd5cSThomas Hellstrom }
529d80efd5cSThomas Hellstrom 
530d80efd5cSThomas Hellstrom /**
531d80efd5cSThomas Hellstrom  * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for
532d80efd5cSThomas Hellstrom  * DX shaders.
533d80efd5cSThomas Hellstrom  *
534d80efd5cSThomas Hellstrom  * @dev_priv: Pointer to device private structure.
535d80efd5cSThomas Hellstrom  * @list: The list of cotable resources.
536d80efd5cSThomas Hellstrom  * @readback: Whether the call was part of a readback unbind.
537d80efd5cSThomas Hellstrom  *
538d80efd5cSThomas Hellstrom  * Scrubs all shader MOBs so that any subsequent shader unbind or shader
539d80efd5cSThomas Hellstrom  * destroy operation won't need to swap in the context.
540d80efd5cSThomas Hellstrom  */
541d80efd5cSThomas Hellstrom void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
542d80efd5cSThomas Hellstrom 				      struct list_head *list,
543d80efd5cSThomas Hellstrom 				      bool readback)
544d80efd5cSThomas Hellstrom {
545d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *entry, *next;
546d80efd5cSThomas Hellstrom 
547d76ce03eSThomas Hellstrom 	lockdep_assert_held_once(&dev_priv->binding_mutex);
548d80efd5cSThomas Hellstrom 
549d80efd5cSThomas Hellstrom 	list_for_each_entry_safe(entry, next, list, cotable_head) {
550d80efd5cSThomas Hellstrom 		WARN_ON(vmw_dx_shader_scrub(&entry->res));
551d80efd5cSThomas Hellstrom 		if (!readback)
552d80efd5cSThomas Hellstrom 			entry->committed = false;
553d80efd5cSThomas Hellstrom 	}
554d80efd5cSThomas Hellstrom }
555d80efd5cSThomas Hellstrom 
556d80efd5cSThomas Hellstrom /**
557d80efd5cSThomas Hellstrom  * vmw_dx_shader_res_free - The DX shader free callback
558d80efd5cSThomas Hellstrom  *
559d80efd5cSThomas Hellstrom  * @res: The shader resource
560d80efd5cSThomas Hellstrom  *
561*8aadeb8aSZack Rusin  * Frees the DX shader resource.
562d80efd5cSThomas Hellstrom  */
563d80efd5cSThomas Hellstrom static void vmw_dx_shader_res_free(struct vmw_resource *res)
564d80efd5cSThomas Hellstrom {
565d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
566d80efd5cSThomas Hellstrom 
567d80efd5cSThomas Hellstrom 	vmw_resource_unreference(&shader->cotable);
568d80efd5cSThomas Hellstrom 	kfree(shader);
569d80efd5cSThomas Hellstrom }
570d80efd5cSThomas Hellstrom 
571d80efd5cSThomas Hellstrom /**
572d80efd5cSThomas Hellstrom  * vmw_dx_shader_add - Add a shader resource as a command buffer managed
573d80efd5cSThomas Hellstrom  * resource.
574d80efd5cSThomas Hellstrom  *
575d80efd5cSThomas Hellstrom  * @man: The command buffer resource manager.
576d80efd5cSThomas Hellstrom  * @ctx: Pointer to the context resource.
577d80efd5cSThomas Hellstrom  * @user_key: The id used for this shader.
578d80efd5cSThomas Hellstrom  * @shader_type: The shader type.
579d80efd5cSThomas Hellstrom  * @list: The list of staged command buffer managed resources.
580d80efd5cSThomas Hellstrom  */
581d80efd5cSThomas Hellstrom int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
582d80efd5cSThomas Hellstrom 		      struct vmw_resource *ctx,
583d80efd5cSThomas Hellstrom 		      u32 user_key,
584d80efd5cSThomas Hellstrom 		      SVGA3dShaderType shader_type,
585d80efd5cSThomas Hellstrom 		      struct list_head *list)
586d80efd5cSThomas Hellstrom {
587d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader;
588d80efd5cSThomas Hellstrom 	struct vmw_resource *res;
589d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = ctx->dev_priv;
590d80efd5cSThomas Hellstrom 	int ret;
591d80efd5cSThomas Hellstrom 
592d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
593d80efd5cSThomas Hellstrom 		return -EINVAL;
594d80efd5cSThomas Hellstrom 
595d80efd5cSThomas Hellstrom 	shader = kmalloc(sizeof(*shader), GFP_KERNEL);
596d80efd5cSThomas Hellstrom 	if (!shader) {
597d80efd5cSThomas Hellstrom 		return -ENOMEM;
598d80efd5cSThomas Hellstrom 	}
599d80efd5cSThomas Hellstrom 
600d80efd5cSThomas Hellstrom 	res = &shader->res;
601d80efd5cSThomas Hellstrom 	shader->ctx = ctx;
6021b9a01d6SThomas Hellstrom 	shader->cotable = vmw_resource_reference
6031b9a01d6SThomas Hellstrom 		(vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER));
604d80efd5cSThomas Hellstrom 	shader->id = user_key;
605d80efd5cSThomas Hellstrom 	shader->committed = false;
606d80efd5cSThomas Hellstrom 	INIT_LIST_HEAD(&shader->cotable_head);
607d80efd5cSThomas Hellstrom 	ret = vmw_resource_init(dev_priv, res, true,
608d80efd5cSThomas Hellstrom 				vmw_dx_shader_res_free, &vmw_dx_shader_func);
609d80efd5cSThomas Hellstrom 	if (ret)
610d80efd5cSThomas Hellstrom 		goto out_resource_init;
611d80efd5cSThomas Hellstrom 
612d80efd5cSThomas Hellstrom 	/*
613d80efd5cSThomas Hellstrom 	 * The user_key name-space is not per shader type for DX shaders,
614d80efd5cSThomas Hellstrom 	 * so when hashing, use a single zero shader type.
615d80efd5cSThomas Hellstrom 	 */
616d80efd5cSThomas Hellstrom 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
617d80efd5cSThomas Hellstrom 				 vmw_shader_key(user_key, 0),
618d80efd5cSThomas Hellstrom 				 res, list);
619d80efd5cSThomas Hellstrom 	if (ret)
620d80efd5cSThomas Hellstrom 		goto out_resource_init;
621d80efd5cSThomas Hellstrom 
622d80efd5cSThomas Hellstrom 	res->id = shader->id;
62313289241SThomas Hellstrom 	res->hw_destroy = vmw_hw_shader_destroy;
624d80efd5cSThomas Hellstrom 
625d80efd5cSThomas Hellstrom out_resource_init:
626d80efd5cSThomas Hellstrom 	vmw_resource_unreference(&res);
627d80efd5cSThomas Hellstrom 
628d80efd5cSThomas Hellstrom 	return ret;
629d80efd5cSThomas Hellstrom }
630d80efd5cSThomas Hellstrom 
631d80efd5cSThomas Hellstrom 
632d80efd5cSThomas Hellstrom 
633ad2ae415SLee Jones /*
634c74c162fSThomas Hellstrom  * User-space shader management:
635c74c162fSThomas Hellstrom  */
636c74c162fSThomas Hellstrom 
637c74c162fSThomas Hellstrom static struct vmw_resource *
638c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base)
639c74c162fSThomas Hellstrom {
640c74c162fSThomas Hellstrom 	return &(container_of(base, struct vmw_user_shader, base)->
641c74c162fSThomas Hellstrom 		 shader.res);
642c74c162fSThomas Hellstrom }
643c74c162fSThomas Hellstrom 
644c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res)
645c74c162fSThomas Hellstrom {
646c74c162fSThomas Hellstrom 	struct vmw_user_shader *ushader =
647c74c162fSThomas Hellstrom 		container_of(res, struct vmw_user_shader, shader.res);
648c74c162fSThomas Hellstrom 
649c74c162fSThomas Hellstrom 	ttm_base_object_kfree(ushader, base);
650c74c162fSThomas Hellstrom }
651c74c162fSThomas Hellstrom 
65218e4a466SThomas Hellstrom static void vmw_shader_free(struct vmw_resource *res)
65318e4a466SThomas Hellstrom {
65418e4a466SThomas Hellstrom 	struct vmw_shader *shader = vmw_res_to_shader(res);
65518e4a466SThomas Hellstrom 
65618e4a466SThomas Hellstrom 	kfree(shader);
65718e4a466SThomas Hellstrom }
65818e4a466SThomas Hellstrom 
659ad2ae415SLee Jones /*
660c74c162fSThomas Hellstrom  * This function is called when user space has no more references on the
661c74c162fSThomas Hellstrom  * base object. It releases the base-object's reference on the resource object.
662c74c162fSThomas Hellstrom  */
663c74c162fSThomas Hellstrom 
664c74c162fSThomas Hellstrom static void vmw_user_shader_base_release(struct ttm_base_object **p_base)
665c74c162fSThomas Hellstrom {
666c74c162fSThomas Hellstrom 	struct ttm_base_object *base = *p_base;
667c74c162fSThomas Hellstrom 	struct vmw_resource *res = vmw_user_shader_base_to_res(base);
668c74c162fSThomas Hellstrom 
669c74c162fSThomas Hellstrom 	*p_base = NULL;
670c74c162fSThomas Hellstrom 	vmw_resource_unreference(&res);
671c74c162fSThomas Hellstrom }
672c74c162fSThomas Hellstrom 
673c74c162fSThomas Hellstrom int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
674c74c162fSThomas Hellstrom 			      struct drm_file *file_priv)
675c74c162fSThomas Hellstrom {
676c74c162fSThomas Hellstrom 	struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data;
677c74c162fSThomas Hellstrom 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
678c74c162fSThomas Hellstrom 
679c74c162fSThomas Hellstrom 	return ttm_ref_object_base_unref(tfile, arg->handle,
680c74c162fSThomas Hellstrom 					 TTM_REF_USAGE);
681c74c162fSThomas Hellstrom }
682c74c162fSThomas Hellstrom 
68318e4a466SThomas Hellstrom static int vmw_user_shader_alloc(struct vmw_private *dev_priv,
684f1d34bfdSThomas Hellstrom 				 struct vmw_buffer_object *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,
721d5bde956SThomas Hellstrom 				   &vmw_user_shader_base_release, NULL);
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,
738f1d34bfdSThomas Hellstrom 					     struct vmw_buffer_object *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;
775f1d34bfdSThomas Hellstrom 	struct vmw_buffer_object *buffer = NULL;
776c74c162fSThomas Hellstrom 	SVGA3dShaderType shader_type;
777c74c162fSThomas Hellstrom 	int ret;
778c74c162fSThomas Hellstrom 
779d80efd5cSThomas Hellstrom 	if (buffer_handle != SVGA3D_INVALID_ID) {
780f1d34bfdSThomas Hellstrom 		ret = vmw_user_bo_lookup(tfile, buffer_handle,
78154c12bc3SThomas Hellstrom 					     &buffer, NULL);
782c74c162fSThomas Hellstrom 		if (unlikely(ret != 0)) {
7835724f899SDeepak Rawat 			VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n");
784c74c162fSThomas Hellstrom 			return ret;
785c74c162fSThomas Hellstrom 		}
786c74c162fSThomas Hellstrom 
787e11bfb99SChristian König 		if ((u64)buffer->base.base.size < (u64)size + (u64)offset) {
7885724f899SDeepak Rawat 			VMW_DEBUG_USER("Illegal buffer- or shader size.\n");
789c74c162fSThomas Hellstrom 			ret = -EINVAL;
790c74c162fSThomas Hellstrom 			goto out_bad_arg;
791c74c162fSThomas Hellstrom 		}
792c74c162fSThomas Hellstrom 	}
793c74c162fSThomas Hellstrom 
794d80efd5cSThomas Hellstrom 	switch (shader_type_drm) {
795c74c162fSThomas Hellstrom 	case drm_vmw_shader_type_vs:
796c74c162fSThomas Hellstrom 		shader_type = SVGA3D_SHADERTYPE_VS;
797c74c162fSThomas Hellstrom 		break;
798c74c162fSThomas Hellstrom 	case drm_vmw_shader_type_ps:
799c74c162fSThomas Hellstrom 		shader_type = SVGA3D_SHADERTYPE_PS;
800c74c162fSThomas Hellstrom 		break;
801c74c162fSThomas Hellstrom 	default:
8025724f899SDeepak Rawat 		VMW_DEBUG_USER("Illegal shader type.\n");
803c74c162fSThomas Hellstrom 		ret = -EINVAL;
804c74c162fSThomas Hellstrom 		goto out_bad_arg;
805c74c162fSThomas Hellstrom 	}
806c74c162fSThomas Hellstrom 
807d80efd5cSThomas Hellstrom 	ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset,
808d80efd5cSThomas Hellstrom 				    shader_type, num_input_sig,
809d80efd5cSThomas Hellstrom 				    num_output_sig, tfile, shader_handle);
810c74c162fSThomas Hellstrom out_bad_arg:
811f1d34bfdSThomas Hellstrom 	vmw_bo_unreference(&buffer);
812d5bde956SThomas Hellstrom 	return ret;
813d5bde956SThomas Hellstrom }
814c74c162fSThomas Hellstrom 
815d5bde956SThomas Hellstrom /**
816d80efd5cSThomas Hellstrom  * vmw_shader_id_ok - Check whether a compat shader user key and
81718e4a466SThomas Hellstrom  * shader type are within valid bounds.
818d5bde956SThomas Hellstrom  *
81918e4a466SThomas Hellstrom  * @user_key: User space id of the shader.
82018e4a466SThomas Hellstrom  * @shader_type: Shader type.
821d5bde956SThomas Hellstrom  *
82218e4a466SThomas Hellstrom  * Returns true if valid false if not.
823d5bde956SThomas Hellstrom  */
824d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type)
825d5bde956SThomas Hellstrom {
82618e4a466SThomas Hellstrom 	return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16;
827d5bde956SThomas Hellstrom }
828d5bde956SThomas Hellstrom 
829d5bde956SThomas Hellstrom /**
830d80efd5cSThomas Hellstrom  * vmw_shader_key - Compute a hash key suitable for a compat shader.
831d5bde956SThomas Hellstrom  *
83218e4a466SThomas Hellstrom  * @user_key: User space id of the shader.
83318e4a466SThomas Hellstrom  * @shader_type: Shader type.
834d5bde956SThomas Hellstrom  *
83518e4a466SThomas Hellstrom  * Returns a hash key suitable for a command buffer managed resource
83618e4a466SThomas Hellstrom  * manager hash table.
837d5bde956SThomas Hellstrom  */
838d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type)
839d5bde956SThomas Hellstrom {
84018e4a466SThomas Hellstrom 	return user_key | (shader_type << 20);
841d5bde956SThomas Hellstrom }
842d5bde956SThomas Hellstrom 
843d5bde956SThomas Hellstrom /**
844d80efd5cSThomas Hellstrom  * vmw_shader_remove - Stage a compat shader for removal.
845d5bde956SThomas Hellstrom  *
84618e4a466SThomas Hellstrom  * @man: Pointer to the compat shader manager identifying the shader namespace.
847d5bde956SThomas Hellstrom  * @user_key: The key that is used to identify the shader. The key is
848d5bde956SThomas Hellstrom  * unique to the shader type.
849d5bde956SThomas Hellstrom  * @shader_type: Shader type.
85018e4a466SThomas Hellstrom  * @list: Caller's list of staged command buffer resource actions.
851d5bde956SThomas Hellstrom  */
852d80efd5cSThomas Hellstrom int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
853d5bde956SThomas Hellstrom 		      u32 user_key, SVGA3dShaderType shader_type,
854d5bde956SThomas Hellstrom 		      struct list_head *list)
855d5bde956SThomas Hellstrom {
856d80efd5cSThomas Hellstrom 	struct vmw_resource *dummy;
857d80efd5cSThomas Hellstrom 
858d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
859d5bde956SThomas Hellstrom 		return -EINVAL;
860d5bde956SThomas Hellstrom 
861d80efd5cSThomas Hellstrom 	return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader,
862d80efd5cSThomas Hellstrom 				     vmw_shader_key(user_key, shader_type),
863d80efd5cSThomas Hellstrom 				     list, &dummy);
864d5bde956SThomas Hellstrom }
865d5bde956SThomas Hellstrom 
866d5bde956SThomas Hellstrom /**
86718e4a466SThomas Hellstrom  * vmw_compat_shader_add - Create a compat shader and stage it for addition
86818e4a466SThomas Hellstrom  * as a command buffer managed resource.
869d5bde956SThomas Hellstrom  *
870ad2ae415SLee Jones  * @dev_priv: Pointer to device private structure.
87118e4a466SThomas Hellstrom  * @man: Pointer to the compat shader manager identifying the shader namespace.
872d5bde956SThomas Hellstrom  * @user_key: The key that is used to identify the shader. The key is
873d5bde956SThomas Hellstrom  * unique to the shader type.
874d5bde956SThomas Hellstrom  * @bytecode: Pointer to the bytecode of the shader.
875d5bde956SThomas Hellstrom  * @shader_type: Shader type.
876ad2ae415SLee Jones  * @size: Command size.
87718e4a466SThomas Hellstrom  * @list: Caller's list of staged command buffer resource actions.
878d5bde956SThomas Hellstrom  *
879d5bde956SThomas Hellstrom  */
88018e4a466SThomas Hellstrom int vmw_compat_shader_add(struct vmw_private *dev_priv,
88118e4a466SThomas Hellstrom 			  struct vmw_cmdbuf_res_manager *man,
882d5bde956SThomas Hellstrom 			  u32 user_key, const void *bytecode,
883d5bde956SThomas Hellstrom 			  SVGA3dShaderType shader_type,
884d5bde956SThomas Hellstrom 			  size_t size,
885d5bde956SThomas Hellstrom 			  struct list_head *list)
886d5bde956SThomas Hellstrom {
88719be5570SChristian König 	struct ttm_operation_ctx ctx = { false, true };
888f1d34bfdSThomas Hellstrom 	struct vmw_buffer_object *buf;
889d5bde956SThomas Hellstrom 	struct ttm_bo_kmap_obj map;
890d5bde956SThomas Hellstrom 	bool is_iomem;
891d5bde956SThomas Hellstrom 	int ret;
89218e4a466SThomas Hellstrom 	struct vmw_resource *res;
893d5bde956SThomas Hellstrom 
894d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
895d5bde956SThomas Hellstrom 		return -EINVAL;
896d5bde956SThomas Hellstrom 
897d5bde956SThomas Hellstrom 	/* Allocate and pin a DMA buffer */
898d5bde956SThomas Hellstrom 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
8991a4adb05SRavikant B Sharma 	if (unlikely(!buf))
900d5bde956SThomas Hellstrom 		return -ENOMEM;
901d5bde956SThomas Hellstrom 
902fbe86ca5SChristian König 	ret = vmw_bo_init(dev_priv, buf, size, &vmw_sys_placement,
903fbe86ca5SChristian König 			      true, true, vmw_bo_bo_free);
904d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
905d5bde956SThomas Hellstrom 		goto out;
906d5bde956SThomas Hellstrom 
907dfd5e50eSChristian König 	ret = ttm_bo_reserve(&buf->base, false, true, NULL);
908d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
909d5bde956SThomas Hellstrom 		goto no_reserve;
910d5bde956SThomas Hellstrom 
911d5bde956SThomas Hellstrom 	/* Map and copy shader bytecode. */
912bc65754cSCai Huoqing 	ret = ttm_bo_kmap(&buf->base, 0, PFN_UP(size), &map);
913d5bde956SThomas Hellstrom 	if (unlikely(ret != 0)) {
914d5bde956SThomas Hellstrom 		ttm_bo_unreserve(&buf->base);
915d5bde956SThomas Hellstrom 		goto no_reserve;
916d5bde956SThomas Hellstrom 	}
917d5bde956SThomas Hellstrom 
918d5bde956SThomas Hellstrom 	memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size);
919d5bde956SThomas Hellstrom 	WARN_ON(is_iomem);
920d5bde956SThomas Hellstrom 
921d5bde956SThomas Hellstrom 	ttm_bo_kunmap(&map);
92219be5570SChristian König 	ret = ttm_bo_validate(&buf->base, &vmw_sys_placement, &ctx);
923d5bde956SThomas Hellstrom 	WARN_ON(ret != 0);
924d5bde956SThomas Hellstrom 	ttm_bo_unreserve(&buf->base);
925d5bde956SThomas Hellstrom 
92618e4a466SThomas Hellstrom 	res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type);
927d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
928d5bde956SThomas Hellstrom 		goto no_reserve;
929d5bde956SThomas Hellstrom 
930d80efd5cSThomas Hellstrom 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
931d80efd5cSThomas Hellstrom 				 vmw_shader_key(user_key, shader_type),
93218e4a466SThomas Hellstrom 				 res, list);
93318e4a466SThomas Hellstrom 	vmw_resource_unreference(&res);
934d5bde956SThomas Hellstrom no_reserve:
935f1d34bfdSThomas Hellstrom 	vmw_bo_unreference(&buf);
936d5bde956SThomas Hellstrom out:
937d5bde956SThomas Hellstrom 	return ret;
938d5bde956SThomas Hellstrom }
939d5bde956SThomas Hellstrom 
940d5bde956SThomas Hellstrom /**
941d80efd5cSThomas Hellstrom  * vmw_shader_lookup - Look up a compat shader
942d5bde956SThomas Hellstrom  *
94318e4a466SThomas Hellstrom  * @man: Pointer to the command buffer managed resource manager identifying
94418e4a466SThomas Hellstrom  * the shader namespace.
94518e4a466SThomas Hellstrom  * @user_key: The user space id of the shader.
94618e4a466SThomas Hellstrom  * @shader_type: The shader type.
947d5bde956SThomas Hellstrom  *
94818e4a466SThomas Hellstrom  * Returns a refcounted pointer to a struct vmw_resource if the shader was
94918e4a466SThomas Hellstrom  * found. An error pointer otherwise.
950d5bde956SThomas Hellstrom  */
95118e4a466SThomas Hellstrom struct vmw_resource *
952d80efd5cSThomas Hellstrom vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
95318e4a466SThomas Hellstrom 		  u32 user_key,
95418e4a466SThomas Hellstrom 		  SVGA3dShaderType shader_type)
955d5bde956SThomas Hellstrom {
956d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
95718e4a466SThomas Hellstrom 		return ERR_PTR(-EINVAL);
958d5bde956SThomas Hellstrom 
959d80efd5cSThomas Hellstrom 	return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader,
960d80efd5cSThomas Hellstrom 				     vmw_shader_key(user_key, shader_type));
961d80efd5cSThomas Hellstrom }
962d80efd5cSThomas Hellstrom 
963d80efd5cSThomas Hellstrom int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
964d80efd5cSThomas Hellstrom 			     struct drm_file *file_priv)
965d80efd5cSThomas Hellstrom {
966d80efd5cSThomas Hellstrom 	struct drm_vmw_shader_create_arg *arg =
967d80efd5cSThomas Hellstrom 		(struct drm_vmw_shader_create_arg *)data;
968d80efd5cSThomas Hellstrom 
969d80efd5cSThomas Hellstrom 	return vmw_shader_define(dev, file_priv, arg->shader_type,
970d80efd5cSThomas Hellstrom 				 arg->buffer_handle,
971d80efd5cSThomas Hellstrom 				 arg->size, arg->offset,
972d80efd5cSThomas Hellstrom 				 0, 0,
973d80efd5cSThomas Hellstrom 				 &arg->shader_handle);
974c74c162fSThomas Hellstrom }
975