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 
5618e4a466SThomas Hellstrom static uint64_t vmw_user_shader_size;
5718e4a466SThomas Hellstrom static uint64_t vmw_shader_size;
58d80efd5cSThomas Hellstrom static size_t vmw_shader_dx_size;
59d5bde956SThomas Hellstrom 
60c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res);
61c74c162fSThomas Hellstrom static struct vmw_resource *
62c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base);
63c74c162fSThomas Hellstrom 
64c74c162fSThomas Hellstrom static int vmw_gb_shader_create(struct vmw_resource *res);
65c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res,
66c74c162fSThomas Hellstrom 			       struct ttm_validate_buffer *val_buf);
67c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res,
68c74c162fSThomas Hellstrom 				 bool readback,
69c74c162fSThomas Hellstrom 				 struct ttm_validate_buffer *val_buf);
70c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res);
71c74c162fSThomas Hellstrom 
72d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res);
73d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res,
74d80efd5cSThomas Hellstrom 			       struct ttm_validate_buffer *val_buf);
75d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res,
76d80efd5cSThomas Hellstrom 				 bool readback,
77d80efd5cSThomas Hellstrom 				 struct ttm_validate_buffer *val_buf);
78d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
79d80efd5cSThomas Hellstrom 					enum vmw_cmdbuf_res_state state);
80d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type);
81d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type);
82d80efd5cSThomas Hellstrom static uint64_t vmw_user_shader_size;
83d80efd5cSThomas Hellstrom 
84c74c162fSThomas Hellstrom static const struct vmw_user_resource_conv user_shader_conv = {
85c74c162fSThomas Hellstrom 	.object_type = VMW_RES_SHADER,
86c74c162fSThomas Hellstrom 	.base_obj_to_res = vmw_user_shader_base_to_res,
87c74c162fSThomas Hellstrom 	.res_free = vmw_user_shader_free
88c74c162fSThomas Hellstrom };
89c74c162fSThomas Hellstrom 
90c74c162fSThomas Hellstrom const struct vmw_user_resource_conv *user_shader_converter =
91c74c162fSThomas Hellstrom 	&user_shader_conv;
92c74c162fSThomas Hellstrom 
93c74c162fSThomas Hellstrom 
94c74c162fSThomas Hellstrom static const struct vmw_res_func vmw_gb_shader_func = {
95c74c162fSThomas Hellstrom 	.res_type = vmw_res_shader,
96c74c162fSThomas Hellstrom 	.needs_backup = true,
97c74c162fSThomas Hellstrom 	.may_evict = true,
98c74c162fSThomas Hellstrom 	.type_name = "guest backed shaders",
99c74c162fSThomas Hellstrom 	.backup_placement = &vmw_mob_placement,
100c74c162fSThomas Hellstrom 	.create = vmw_gb_shader_create,
101c74c162fSThomas Hellstrom 	.destroy = vmw_gb_shader_destroy,
102c74c162fSThomas Hellstrom 	.bind = vmw_gb_shader_bind,
103c74c162fSThomas Hellstrom 	.unbind = vmw_gb_shader_unbind
104c74c162fSThomas Hellstrom };
105c74c162fSThomas Hellstrom 
106d80efd5cSThomas Hellstrom static const struct vmw_res_func vmw_dx_shader_func = {
107d80efd5cSThomas Hellstrom 	.res_type = vmw_res_shader,
108d80efd5cSThomas Hellstrom 	.needs_backup = true,
109d80efd5cSThomas Hellstrom 	.may_evict = false,
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 
124c74c162fSThomas Hellstrom /**
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,
162f1d34bfdSThomas Hellstrom 			      struct vmw_buffer_object *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 
221c74c162fSThomas Hellstrom 	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
222c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
223c74c162fSThomas Hellstrom 		DRM_ERROR("Failed reserving FIFO space for shader "
224c74c162fSThomas Hellstrom 			  "creation.\n");
225c74c162fSThomas Hellstrom 		ret = -ENOMEM;
226c74c162fSThomas Hellstrom 		goto out_no_fifo;
227c74c162fSThomas Hellstrom 	}
228c74c162fSThomas Hellstrom 
229c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER;
230c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
231c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
232c74c162fSThomas Hellstrom 	cmd->body.type = shader->type;
233c74c162fSThomas Hellstrom 	cmd->body.sizeInBytes = shader->size;
234c74c162fSThomas Hellstrom 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
235153b3d5bSThomas Hellstrom 	vmw_fifo_resource_inc(dev_priv);
236c74c162fSThomas Hellstrom 
237c74c162fSThomas Hellstrom 	return 0;
238c74c162fSThomas Hellstrom 
239c74c162fSThomas Hellstrom out_no_fifo:
240c74c162fSThomas Hellstrom 	vmw_resource_release_id(res);
241c74c162fSThomas Hellstrom out_no_id:
242c74c162fSThomas Hellstrom 	return ret;
243c74c162fSThomas Hellstrom }
244c74c162fSThomas Hellstrom 
245c74c162fSThomas Hellstrom static int vmw_gb_shader_bind(struct vmw_resource *res,
246c74c162fSThomas Hellstrom 			      struct ttm_validate_buffer *val_buf)
247c74c162fSThomas Hellstrom {
248c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
249c74c162fSThomas Hellstrom 	struct {
250c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
251c74c162fSThomas Hellstrom 		SVGA3dCmdBindGBShader body;
252c74c162fSThomas Hellstrom 	} *cmd;
253c74c162fSThomas Hellstrom 	struct ttm_buffer_object *bo = val_buf->bo;
254c74c162fSThomas Hellstrom 
255c74c162fSThomas Hellstrom 	BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
256c74c162fSThomas Hellstrom 
257c74c162fSThomas Hellstrom 	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
258c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
259c74c162fSThomas Hellstrom 		DRM_ERROR("Failed reserving FIFO space for shader "
260c74c162fSThomas Hellstrom 			  "binding.\n");
261c74c162fSThomas Hellstrom 		return -ENOMEM;
262c74c162fSThomas Hellstrom 	}
263c74c162fSThomas Hellstrom 
264c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
265c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
266c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
267c74c162fSThomas Hellstrom 	cmd->body.mobid = bo->mem.start;
268b8ccd1e4SThomas Hellstrom 	cmd->body.offsetInBytes = res->backup_offset;
269c74c162fSThomas Hellstrom 	res->backup_dirty = false;
270c74c162fSThomas Hellstrom 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
271c74c162fSThomas Hellstrom 
272c74c162fSThomas Hellstrom 	return 0;
273c74c162fSThomas Hellstrom }
274c74c162fSThomas Hellstrom 
275c74c162fSThomas Hellstrom static int vmw_gb_shader_unbind(struct vmw_resource *res,
276c74c162fSThomas Hellstrom 				bool readback,
277c74c162fSThomas Hellstrom 				struct ttm_validate_buffer *val_buf)
278c74c162fSThomas Hellstrom {
279c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
280c74c162fSThomas Hellstrom 	struct {
281c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
282c74c162fSThomas Hellstrom 		SVGA3dCmdBindGBShader body;
283c74c162fSThomas Hellstrom 	} *cmd;
284c74c162fSThomas Hellstrom 	struct vmw_fence_obj *fence;
285c74c162fSThomas Hellstrom 
286c74c162fSThomas Hellstrom 	BUG_ON(res->backup->base.mem.mem_type != VMW_PL_MOB);
287c74c162fSThomas Hellstrom 
288c74c162fSThomas Hellstrom 	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
289c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
290c74c162fSThomas Hellstrom 		DRM_ERROR("Failed reserving FIFO space for shader "
291c74c162fSThomas Hellstrom 			  "unbinding.\n");
292c74c162fSThomas Hellstrom 		return -ENOMEM;
293c74c162fSThomas Hellstrom 	}
294c74c162fSThomas Hellstrom 
295c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
296c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
297c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
298c74c162fSThomas Hellstrom 	cmd->body.mobid = SVGA3D_INVALID_ID;
299c74c162fSThomas Hellstrom 	cmd->body.offsetInBytes = 0;
300c74c162fSThomas Hellstrom 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
301c74c162fSThomas Hellstrom 
302c74c162fSThomas Hellstrom 	/*
303c74c162fSThomas Hellstrom 	 * Create a fence object and fence the backup buffer.
304c74c162fSThomas Hellstrom 	 */
305c74c162fSThomas Hellstrom 
306c74c162fSThomas Hellstrom 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
307c74c162fSThomas Hellstrom 					  &fence, NULL);
308c74c162fSThomas Hellstrom 
309e9431ea5SThomas Hellstrom 	vmw_bo_fence_single(val_buf->bo, fence);
310c74c162fSThomas Hellstrom 
311c74c162fSThomas Hellstrom 	if (likely(fence != NULL))
312c74c162fSThomas Hellstrom 		vmw_fence_obj_unreference(&fence);
313c74c162fSThomas Hellstrom 
314c74c162fSThomas Hellstrom 	return 0;
315c74c162fSThomas Hellstrom }
316c74c162fSThomas Hellstrom 
317c74c162fSThomas Hellstrom static int vmw_gb_shader_destroy(struct vmw_resource *res)
318c74c162fSThomas Hellstrom {
319c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
320c74c162fSThomas Hellstrom 	struct {
321c74c162fSThomas Hellstrom 		SVGA3dCmdHeader header;
322c74c162fSThomas Hellstrom 		SVGA3dCmdDestroyGBShader body;
323c74c162fSThomas Hellstrom 	} *cmd;
324c74c162fSThomas Hellstrom 
325c74c162fSThomas Hellstrom 	if (likely(res->id == -1))
326c74c162fSThomas Hellstrom 		return 0;
327c74c162fSThomas Hellstrom 
328173fb7d4SThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
329d80efd5cSThomas Hellstrom 	vmw_binding_res_list_scrub(&res->binding_head);
330173fb7d4SThomas Hellstrom 
331c74c162fSThomas Hellstrom 	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
332c74c162fSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
333c74c162fSThomas Hellstrom 		DRM_ERROR("Failed reserving FIFO space for shader "
334c74c162fSThomas Hellstrom 			  "destruction.\n");
3353e894a62SThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
336c74c162fSThomas Hellstrom 		return -ENOMEM;
337c74c162fSThomas Hellstrom 	}
338c74c162fSThomas Hellstrom 
339c74c162fSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER;
340c74c162fSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
341c74c162fSThomas Hellstrom 	cmd->body.shid = res->id;
342c74c162fSThomas Hellstrom 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
343173fb7d4SThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
344c74c162fSThomas Hellstrom 	vmw_resource_release_id(res);
345153b3d5bSThomas Hellstrom 	vmw_fifo_resource_dec(dev_priv);
346c74c162fSThomas Hellstrom 
347c74c162fSThomas Hellstrom 	return 0;
348c74c162fSThomas Hellstrom }
349c74c162fSThomas Hellstrom 
350d80efd5cSThomas Hellstrom /*
351d80efd5cSThomas Hellstrom  * DX shader code:
352d80efd5cSThomas Hellstrom  */
353d80efd5cSThomas Hellstrom 
354d80efd5cSThomas Hellstrom /**
355d80efd5cSThomas Hellstrom  * vmw_dx_shader_commit_notify - Notify that a shader operation has been
356d80efd5cSThomas Hellstrom  * committed to hardware from a user-supplied command stream.
357d80efd5cSThomas Hellstrom  *
358d80efd5cSThomas Hellstrom  * @res: Pointer to the shader resource.
359d80efd5cSThomas Hellstrom  * @state: Indicating whether a creation or removal has been committed.
360d80efd5cSThomas Hellstrom  *
361d80efd5cSThomas Hellstrom  */
362d80efd5cSThomas Hellstrom static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
363d80efd5cSThomas Hellstrom 					enum vmw_cmdbuf_res_state state)
364d80efd5cSThomas Hellstrom {
365d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
366d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
367d80efd5cSThomas Hellstrom 
368d80efd5cSThomas Hellstrom 	if (state == VMW_CMDBUF_RES_ADD) {
369d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
370d80efd5cSThomas Hellstrom 		vmw_cotable_add_resource(shader->cotable,
371d80efd5cSThomas Hellstrom 					 &shader->cotable_head);
372d80efd5cSThomas Hellstrom 		shader->committed = true;
373d80efd5cSThomas Hellstrom 		res->id = shader->id;
374d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
375d80efd5cSThomas Hellstrom 	} else {
376d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
377d80efd5cSThomas Hellstrom 		list_del_init(&shader->cotable_head);
378d80efd5cSThomas Hellstrom 		shader->committed = false;
379d80efd5cSThomas Hellstrom 		res->id = -1;
380d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
381d80efd5cSThomas Hellstrom 	}
382d80efd5cSThomas Hellstrom }
383d80efd5cSThomas Hellstrom 
384d80efd5cSThomas Hellstrom /**
385d80efd5cSThomas Hellstrom  * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader.
386d80efd5cSThomas Hellstrom  *
387d80efd5cSThomas Hellstrom  * @res: The shader resource
388d80efd5cSThomas Hellstrom  *
389d80efd5cSThomas Hellstrom  * This function reverts a scrub operation.
390d80efd5cSThomas Hellstrom  */
391d80efd5cSThomas Hellstrom static int vmw_dx_shader_unscrub(struct vmw_resource *res)
392d80efd5cSThomas Hellstrom {
393d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
394d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
395d80efd5cSThomas Hellstrom 	struct {
396d80efd5cSThomas Hellstrom 		SVGA3dCmdHeader header;
397d80efd5cSThomas Hellstrom 		SVGA3dCmdDXBindShader body;
398d80efd5cSThomas Hellstrom 	} *cmd;
399d80efd5cSThomas Hellstrom 
400d80efd5cSThomas Hellstrom 	if (!list_empty(&shader->cotable_head) || !shader->committed)
401d80efd5cSThomas Hellstrom 		return 0;
402d80efd5cSThomas Hellstrom 
403d80efd5cSThomas Hellstrom 	cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd),
404d80efd5cSThomas Hellstrom 				  shader->ctx->id);
405d80efd5cSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
406d80efd5cSThomas Hellstrom 		DRM_ERROR("Failed reserving FIFO space for shader "
407d80efd5cSThomas Hellstrom 			  "scrubbing.\n");
408d80efd5cSThomas Hellstrom 		return -ENOMEM;
409d80efd5cSThomas Hellstrom 	}
410d80efd5cSThomas Hellstrom 
411d80efd5cSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
412d80efd5cSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
413d80efd5cSThomas Hellstrom 	cmd->body.cid = shader->ctx->id;
414d80efd5cSThomas Hellstrom 	cmd->body.shid = shader->id;
415d80efd5cSThomas Hellstrom 	cmd->body.mobid = res->backup->base.mem.start;
416d80efd5cSThomas Hellstrom 	cmd->body.offsetInBytes = res->backup_offset;
417d80efd5cSThomas Hellstrom 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
418d80efd5cSThomas Hellstrom 
419d80efd5cSThomas Hellstrom 	vmw_cotable_add_resource(shader->cotable, &shader->cotable_head);
420d80efd5cSThomas Hellstrom 
421d80efd5cSThomas Hellstrom 	return 0;
422d80efd5cSThomas Hellstrom }
423d80efd5cSThomas Hellstrom 
424d80efd5cSThomas Hellstrom /**
425d80efd5cSThomas Hellstrom  * vmw_dx_shader_create - The DX shader create callback
426d80efd5cSThomas Hellstrom  *
427d80efd5cSThomas Hellstrom  * @res: The DX shader resource
428d80efd5cSThomas Hellstrom  *
429d80efd5cSThomas Hellstrom  * The create callback is called as part of resource validation and
430d80efd5cSThomas Hellstrom  * makes sure that we unscrub the shader if it's previously been scrubbed.
431d80efd5cSThomas Hellstrom  */
432d80efd5cSThomas Hellstrom static int vmw_dx_shader_create(struct vmw_resource *res)
433d80efd5cSThomas Hellstrom {
434d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
435d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
436d80efd5cSThomas Hellstrom 	int ret = 0;
437d80efd5cSThomas Hellstrom 
438d80efd5cSThomas Hellstrom 	WARN_ON_ONCE(!shader->committed);
439d80efd5cSThomas Hellstrom 
440d80efd5cSThomas Hellstrom 	if (!list_empty(&res->mob_head)) {
441d80efd5cSThomas Hellstrom 		mutex_lock(&dev_priv->binding_mutex);
442d80efd5cSThomas Hellstrom 		ret = vmw_dx_shader_unscrub(res);
443d80efd5cSThomas Hellstrom 		mutex_unlock(&dev_priv->binding_mutex);
444d80efd5cSThomas Hellstrom 	}
445d80efd5cSThomas Hellstrom 
446d80efd5cSThomas Hellstrom 	res->id = shader->id;
447d80efd5cSThomas Hellstrom 	return ret;
448d80efd5cSThomas Hellstrom }
449d80efd5cSThomas Hellstrom 
450d80efd5cSThomas Hellstrom /**
451d80efd5cSThomas Hellstrom  * vmw_dx_shader_bind - The DX shader bind callback
452d80efd5cSThomas Hellstrom  *
453d80efd5cSThomas Hellstrom  * @res: The DX shader resource
454d80efd5cSThomas Hellstrom  * @val_buf: Pointer to the validate buffer.
455d80efd5cSThomas Hellstrom  *
456d80efd5cSThomas Hellstrom  */
457d80efd5cSThomas Hellstrom static int vmw_dx_shader_bind(struct vmw_resource *res,
458d80efd5cSThomas Hellstrom 			      struct ttm_validate_buffer *val_buf)
459d80efd5cSThomas Hellstrom {
460d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
461d80efd5cSThomas Hellstrom 	struct ttm_buffer_object *bo = val_buf->bo;
462d80efd5cSThomas Hellstrom 
463d80efd5cSThomas Hellstrom 	BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
464d80efd5cSThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
465d80efd5cSThomas Hellstrom 	vmw_dx_shader_unscrub(res);
466d80efd5cSThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
467d80efd5cSThomas Hellstrom 
468d80efd5cSThomas Hellstrom 	return 0;
469d80efd5cSThomas Hellstrom }
470d80efd5cSThomas Hellstrom 
471d80efd5cSThomas Hellstrom /**
472d80efd5cSThomas Hellstrom  * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader.
473d80efd5cSThomas Hellstrom  *
474d80efd5cSThomas Hellstrom  * @res: The shader resource
475d80efd5cSThomas Hellstrom  *
476d80efd5cSThomas Hellstrom  * This function unbinds a MOB from the DX shader without requiring the
477d80efd5cSThomas Hellstrom  * MOB dma_buffer to be reserved. The driver still considers the MOB bound.
478d80efd5cSThomas Hellstrom  * However, once the driver eventually decides to unbind the MOB, it doesn't
479d80efd5cSThomas Hellstrom  * need to access the context.
480d80efd5cSThomas Hellstrom  */
481d80efd5cSThomas Hellstrom static int vmw_dx_shader_scrub(struct vmw_resource *res)
482d80efd5cSThomas Hellstrom {
483d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
484d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
485d80efd5cSThomas Hellstrom 	struct {
486d80efd5cSThomas Hellstrom 		SVGA3dCmdHeader header;
487d80efd5cSThomas Hellstrom 		SVGA3dCmdDXBindShader body;
488d80efd5cSThomas Hellstrom 	} *cmd;
489d80efd5cSThomas Hellstrom 
490d80efd5cSThomas Hellstrom 	if (list_empty(&shader->cotable_head))
491d80efd5cSThomas Hellstrom 		return 0;
492d80efd5cSThomas Hellstrom 
493d80efd5cSThomas Hellstrom 	WARN_ON_ONCE(!shader->committed);
494d80efd5cSThomas Hellstrom 	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
495d80efd5cSThomas Hellstrom 	if (unlikely(cmd == NULL)) {
496d80efd5cSThomas Hellstrom 		DRM_ERROR("Failed reserving FIFO space for shader "
497d80efd5cSThomas Hellstrom 			  "scrubbing.\n");
498d80efd5cSThomas Hellstrom 		return -ENOMEM;
499d80efd5cSThomas Hellstrom 	}
500d80efd5cSThomas Hellstrom 
501d80efd5cSThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
502d80efd5cSThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
503d80efd5cSThomas Hellstrom 	cmd->body.cid = shader->ctx->id;
504d80efd5cSThomas Hellstrom 	cmd->body.shid = res->id;
505d80efd5cSThomas Hellstrom 	cmd->body.mobid = SVGA3D_INVALID_ID;
506d80efd5cSThomas Hellstrom 	cmd->body.offsetInBytes = 0;
507d80efd5cSThomas Hellstrom 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
508d80efd5cSThomas Hellstrom 	res->id = -1;
509d80efd5cSThomas Hellstrom 	list_del_init(&shader->cotable_head);
510d80efd5cSThomas Hellstrom 
511d80efd5cSThomas Hellstrom 	return 0;
512d80efd5cSThomas Hellstrom }
513d80efd5cSThomas Hellstrom 
514d80efd5cSThomas Hellstrom /**
515d80efd5cSThomas Hellstrom  * vmw_dx_shader_unbind - The dx shader unbind callback.
516d80efd5cSThomas Hellstrom  *
517d80efd5cSThomas Hellstrom  * @res: The shader resource
518d80efd5cSThomas Hellstrom  * @readback: Whether this is a readback unbind. Currently unused.
519d80efd5cSThomas Hellstrom  * @val_buf: MOB buffer information.
520d80efd5cSThomas Hellstrom  */
521d80efd5cSThomas Hellstrom static int vmw_dx_shader_unbind(struct vmw_resource *res,
522d80efd5cSThomas Hellstrom 				bool readback,
523d80efd5cSThomas Hellstrom 				struct ttm_validate_buffer *val_buf)
524d80efd5cSThomas Hellstrom {
525d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
526d80efd5cSThomas Hellstrom 	struct vmw_fence_obj *fence;
527d80efd5cSThomas Hellstrom 	int ret;
528d80efd5cSThomas Hellstrom 
529d80efd5cSThomas Hellstrom 	BUG_ON(res->backup->base.mem.mem_type != VMW_PL_MOB);
530d80efd5cSThomas Hellstrom 
531d80efd5cSThomas Hellstrom 	mutex_lock(&dev_priv->binding_mutex);
532d80efd5cSThomas Hellstrom 	ret = vmw_dx_shader_scrub(res);
533d80efd5cSThomas Hellstrom 	mutex_unlock(&dev_priv->binding_mutex);
534d80efd5cSThomas Hellstrom 
535d80efd5cSThomas Hellstrom 	if (ret)
536d80efd5cSThomas Hellstrom 		return ret;
537d80efd5cSThomas Hellstrom 
538d80efd5cSThomas Hellstrom 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
539d80efd5cSThomas Hellstrom 					  &fence, NULL);
540e9431ea5SThomas Hellstrom 	vmw_bo_fence_single(val_buf->bo, fence);
541d80efd5cSThomas Hellstrom 
542d80efd5cSThomas Hellstrom 	if (likely(fence != NULL))
543d80efd5cSThomas Hellstrom 		vmw_fence_obj_unreference(&fence);
544d80efd5cSThomas Hellstrom 
545d80efd5cSThomas Hellstrom 	return 0;
546d80efd5cSThomas Hellstrom }
547d80efd5cSThomas Hellstrom 
548d80efd5cSThomas Hellstrom /**
549d80efd5cSThomas Hellstrom  * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for
550d80efd5cSThomas Hellstrom  * DX shaders.
551d80efd5cSThomas Hellstrom  *
552d80efd5cSThomas Hellstrom  * @dev_priv: Pointer to device private structure.
553d80efd5cSThomas Hellstrom  * @list: The list of cotable resources.
554d80efd5cSThomas Hellstrom  * @readback: Whether the call was part of a readback unbind.
555d80efd5cSThomas Hellstrom  *
556d80efd5cSThomas Hellstrom  * Scrubs all shader MOBs so that any subsequent shader unbind or shader
557d80efd5cSThomas Hellstrom  * destroy operation won't need to swap in the context.
558d80efd5cSThomas Hellstrom  */
559d80efd5cSThomas Hellstrom void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
560d80efd5cSThomas Hellstrom 				      struct list_head *list,
561d80efd5cSThomas Hellstrom 				      bool readback)
562d80efd5cSThomas Hellstrom {
563d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *entry, *next;
564d80efd5cSThomas Hellstrom 
565d76ce03eSThomas Hellstrom 	lockdep_assert_held_once(&dev_priv->binding_mutex);
566d80efd5cSThomas Hellstrom 
567d80efd5cSThomas Hellstrom 	list_for_each_entry_safe(entry, next, list, cotable_head) {
568d80efd5cSThomas Hellstrom 		WARN_ON(vmw_dx_shader_scrub(&entry->res));
569d80efd5cSThomas Hellstrom 		if (!readback)
570d80efd5cSThomas Hellstrom 			entry->committed = false;
571d80efd5cSThomas Hellstrom 	}
572d80efd5cSThomas Hellstrom }
573d80efd5cSThomas Hellstrom 
574d80efd5cSThomas Hellstrom /**
575d80efd5cSThomas Hellstrom  * vmw_dx_shader_res_free - The DX shader free callback
576d80efd5cSThomas Hellstrom  *
577d80efd5cSThomas Hellstrom  * @res: The shader resource
578d80efd5cSThomas Hellstrom  *
579d80efd5cSThomas Hellstrom  * Frees the DX shader resource and updates memory accounting.
580d80efd5cSThomas Hellstrom  */
581d80efd5cSThomas Hellstrom static void vmw_dx_shader_res_free(struct vmw_resource *res)
582d80efd5cSThomas Hellstrom {
583d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
584d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
585d80efd5cSThomas Hellstrom 
586d80efd5cSThomas Hellstrom 	vmw_resource_unreference(&shader->cotable);
587d80efd5cSThomas Hellstrom 	kfree(shader);
588d80efd5cSThomas Hellstrom 	ttm_mem_global_free(vmw_mem_glob(dev_priv), vmw_shader_dx_size);
589d80efd5cSThomas Hellstrom }
590d80efd5cSThomas Hellstrom 
591d80efd5cSThomas Hellstrom /**
592d80efd5cSThomas Hellstrom  * vmw_dx_shader_add - Add a shader resource as a command buffer managed
593d80efd5cSThomas Hellstrom  * resource.
594d80efd5cSThomas Hellstrom  *
595d80efd5cSThomas Hellstrom  * @man: The command buffer resource manager.
596d80efd5cSThomas Hellstrom  * @ctx: Pointer to the context resource.
597d80efd5cSThomas Hellstrom  * @user_key: The id used for this shader.
598d80efd5cSThomas Hellstrom  * @shader_type: The shader type.
599d80efd5cSThomas Hellstrom  * @list: The list of staged command buffer managed resources.
600d80efd5cSThomas Hellstrom  */
601d80efd5cSThomas Hellstrom int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
602d80efd5cSThomas Hellstrom 		      struct vmw_resource *ctx,
603d80efd5cSThomas Hellstrom 		      u32 user_key,
604d80efd5cSThomas Hellstrom 		      SVGA3dShaderType shader_type,
605d80efd5cSThomas Hellstrom 		      struct list_head *list)
606d80efd5cSThomas Hellstrom {
607d80efd5cSThomas Hellstrom 	struct vmw_dx_shader *shader;
608d80efd5cSThomas Hellstrom 	struct vmw_resource *res;
609d80efd5cSThomas Hellstrom 	struct vmw_private *dev_priv = ctx->dev_priv;
610279c01f6SRoger He 	struct ttm_operation_ctx ttm_opt_ctx = {
611279c01f6SRoger He 		.interruptible = true,
612279c01f6SRoger He 		.no_wait_gpu = false
613279c01f6SRoger He 	};
614d80efd5cSThomas Hellstrom 	int ret;
615d80efd5cSThomas Hellstrom 
616d80efd5cSThomas Hellstrom 	if (!vmw_shader_dx_size)
617d80efd5cSThomas Hellstrom 		vmw_shader_dx_size = ttm_round_pot(sizeof(*shader));
618d80efd5cSThomas Hellstrom 
619d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
620d80efd5cSThomas Hellstrom 		return -EINVAL;
621d80efd5cSThomas Hellstrom 
622d80efd5cSThomas Hellstrom 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), vmw_shader_dx_size,
623279c01f6SRoger He 				   &ttm_opt_ctx);
624d80efd5cSThomas Hellstrom 	if (ret) {
625d80efd5cSThomas Hellstrom 		if (ret != -ERESTARTSYS)
626d80efd5cSThomas Hellstrom 			DRM_ERROR("Out of graphics memory for shader "
627d80efd5cSThomas Hellstrom 				  "creation.\n");
628d80efd5cSThomas Hellstrom 		return ret;
629d80efd5cSThomas Hellstrom 	}
630d80efd5cSThomas Hellstrom 
631d80efd5cSThomas Hellstrom 	shader = kmalloc(sizeof(*shader), GFP_KERNEL);
632d80efd5cSThomas Hellstrom 	if (!shader) {
633d80efd5cSThomas Hellstrom 		ttm_mem_global_free(vmw_mem_glob(dev_priv), vmw_shader_dx_size);
634d80efd5cSThomas Hellstrom 		return -ENOMEM;
635d80efd5cSThomas Hellstrom 	}
636d80efd5cSThomas Hellstrom 
637d80efd5cSThomas Hellstrom 	res = &shader->res;
638d80efd5cSThomas Hellstrom 	shader->ctx = ctx;
6391b9a01d6SThomas Hellstrom 	shader->cotable = vmw_resource_reference
6401b9a01d6SThomas Hellstrom 		(vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER));
641d80efd5cSThomas Hellstrom 	shader->id = user_key;
642d80efd5cSThomas Hellstrom 	shader->committed = false;
643d80efd5cSThomas Hellstrom 	INIT_LIST_HEAD(&shader->cotable_head);
644d80efd5cSThomas Hellstrom 	ret = vmw_resource_init(dev_priv, res, true,
645d80efd5cSThomas Hellstrom 				vmw_dx_shader_res_free, &vmw_dx_shader_func);
646d80efd5cSThomas Hellstrom 	if (ret)
647d80efd5cSThomas Hellstrom 		goto out_resource_init;
648d80efd5cSThomas Hellstrom 
649d80efd5cSThomas Hellstrom 	/*
650d80efd5cSThomas Hellstrom 	 * The user_key name-space is not per shader type for DX shaders,
651d80efd5cSThomas Hellstrom 	 * so when hashing, use a single zero shader type.
652d80efd5cSThomas Hellstrom 	 */
653d80efd5cSThomas Hellstrom 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
654d80efd5cSThomas Hellstrom 				 vmw_shader_key(user_key, 0),
655d80efd5cSThomas Hellstrom 				 res, list);
656d80efd5cSThomas Hellstrom 	if (ret)
657d80efd5cSThomas Hellstrom 		goto out_resource_init;
658d80efd5cSThomas Hellstrom 
659d80efd5cSThomas Hellstrom 	res->id = shader->id;
66013289241SThomas Hellstrom 	res->hw_destroy = vmw_hw_shader_destroy;
661d80efd5cSThomas Hellstrom 
662d80efd5cSThomas Hellstrom out_resource_init:
663d80efd5cSThomas Hellstrom 	vmw_resource_unreference(&res);
664d80efd5cSThomas Hellstrom 
665d80efd5cSThomas Hellstrom 	return ret;
666d80efd5cSThomas Hellstrom }
667d80efd5cSThomas Hellstrom 
668d80efd5cSThomas Hellstrom 
669d80efd5cSThomas Hellstrom 
670c74c162fSThomas Hellstrom /**
671c74c162fSThomas Hellstrom  * User-space shader management:
672c74c162fSThomas Hellstrom  */
673c74c162fSThomas Hellstrom 
674c74c162fSThomas Hellstrom static struct vmw_resource *
675c74c162fSThomas Hellstrom vmw_user_shader_base_to_res(struct ttm_base_object *base)
676c74c162fSThomas Hellstrom {
677c74c162fSThomas Hellstrom 	return &(container_of(base, struct vmw_user_shader, base)->
678c74c162fSThomas Hellstrom 		 shader.res);
679c74c162fSThomas Hellstrom }
680c74c162fSThomas Hellstrom 
681c74c162fSThomas Hellstrom static void vmw_user_shader_free(struct vmw_resource *res)
682c74c162fSThomas Hellstrom {
683c74c162fSThomas Hellstrom 	struct vmw_user_shader *ushader =
684c74c162fSThomas Hellstrom 		container_of(res, struct vmw_user_shader, shader.res);
685c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
686c74c162fSThomas Hellstrom 
687c74c162fSThomas Hellstrom 	ttm_base_object_kfree(ushader, base);
688c74c162fSThomas Hellstrom 	ttm_mem_global_free(vmw_mem_glob(dev_priv),
689c74c162fSThomas Hellstrom 			    vmw_user_shader_size);
690c74c162fSThomas Hellstrom }
691c74c162fSThomas Hellstrom 
69218e4a466SThomas Hellstrom static void vmw_shader_free(struct vmw_resource *res)
69318e4a466SThomas Hellstrom {
69418e4a466SThomas Hellstrom 	struct vmw_shader *shader = vmw_res_to_shader(res);
69518e4a466SThomas Hellstrom 	struct vmw_private *dev_priv = res->dev_priv;
69618e4a466SThomas Hellstrom 
69718e4a466SThomas Hellstrom 	kfree(shader);
69818e4a466SThomas Hellstrom 	ttm_mem_global_free(vmw_mem_glob(dev_priv),
69918e4a466SThomas Hellstrom 			    vmw_shader_size);
70018e4a466SThomas Hellstrom }
70118e4a466SThomas Hellstrom 
702c74c162fSThomas Hellstrom /**
703c74c162fSThomas Hellstrom  * This function is called when user space has no more references on the
704c74c162fSThomas Hellstrom  * base object. It releases the base-object's reference on the resource object.
705c74c162fSThomas Hellstrom  */
706c74c162fSThomas Hellstrom 
707c74c162fSThomas Hellstrom static void vmw_user_shader_base_release(struct ttm_base_object **p_base)
708c74c162fSThomas Hellstrom {
709c74c162fSThomas Hellstrom 	struct ttm_base_object *base = *p_base;
710c74c162fSThomas Hellstrom 	struct vmw_resource *res = vmw_user_shader_base_to_res(base);
711c74c162fSThomas Hellstrom 
712c74c162fSThomas Hellstrom 	*p_base = NULL;
713c74c162fSThomas Hellstrom 	vmw_resource_unreference(&res);
714c74c162fSThomas Hellstrom }
715c74c162fSThomas Hellstrom 
716c74c162fSThomas Hellstrom int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
717c74c162fSThomas Hellstrom 			      struct drm_file *file_priv)
718c74c162fSThomas Hellstrom {
719c74c162fSThomas Hellstrom 	struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data;
720c74c162fSThomas Hellstrom 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
721c74c162fSThomas Hellstrom 
722c74c162fSThomas Hellstrom 	return ttm_ref_object_base_unref(tfile, arg->handle,
723c74c162fSThomas Hellstrom 					 TTM_REF_USAGE);
724c74c162fSThomas Hellstrom }
725c74c162fSThomas Hellstrom 
72618e4a466SThomas Hellstrom static int vmw_user_shader_alloc(struct vmw_private *dev_priv,
727f1d34bfdSThomas Hellstrom 				 struct vmw_buffer_object *buffer,
728d5bde956SThomas Hellstrom 				 size_t shader_size,
729d5bde956SThomas Hellstrom 				 size_t offset,
730d5bde956SThomas Hellstrom 				 SVGA3dShaderType shader_type,
731d80efd5cSThomas Hellstrom 				 uint8_t num_input_sig,
732d80efd5cSThomas Hellstrom 				 uint8_t num_output_sig,
733d5bde956SThomas Hellstrom 				 struct ttm_object_file *tfile,
734d5bde956SThomas Hellstrom 				 u32 *handle)
735d5bde956SThomas Hellstrom {
736d5bde956SThomas Hellstrom 	struct vmw_user_shader *ushader;
737d5bde956SThomas Hellstrom 	struct vmw_resource *res, *tmp;
738279c01f6SRoger He 	struct ttm_operation_ctx ctx = {
739279c01f6SRoger He 		.interruptible = true,
740279c01f6SRoger He 		.no_wait_gpu = false
741279c01f6SRoger He 	};
742d5bde956SThomas Hellstrom 	int ret;
743d5bde956SThomas Hellstrom 
744d5bde956SThomas Hellstrom 	if (unlikely(vmw_user_shader_size == 0))
745d5bde956SThomas Hellstrom 		vmw_user_shader_size =
746c7eae626SThomas Hellstrom 			ttm_round_pot(sizeof(struct vmw_user_shader)) +
747c7eae626SThomas Hellstrom 			VMW_IDA_ACC_SIZE + TTM_OBJ_EXTRA_SIZE;
748d5bde956SThomas Hellstrom 
749d5bde956SThomas Hellstrom 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
750d5bde956SThomas Hellstrom 				   vmw_user_shader_size,
751279c01f6SRoger He 				   &ctx);
752d5bde956SThomas Hellstrom 	if (unlikely(ret != 0)) {
753d5bde956SThomas Hellstrom 		if (ret != -ERESTARTSYS)
754d5bde956SThomas Hellstrom 			DRM_ERROR("Out of graphics memory for shader "
755d5bde956SThomas Hellstrom 				  "creation.\n");
756d5bde956SThomas Hellstrom 		goto out;
757d5bde956SThomas Hellstrom 	}
758d5bde956SThomas Hellstrom 
759d5bde956SThomas Hellstrom 	ushader = kzalloc(sizeof(*ushader), GFP_KERNEL);
7601a4adb05SRavikant B Sharma 	if (unlikely(!ushader)) {
761d5bde956SThomas Hellstrom 		ttm_mem_global_free(vmw_mem_glob(dev_priv),
762d5bde956SThomas Hellstrom 				    vmw_user_shader_size);
763d5bde956SThomas Hellstrom 		ret = -ENOMEM;
764d5bde956SThomas Hellstrom 		goto out;
765d5bde956SThomas Hellstrom 	}
766d5bde956SThomas Hellstrom 
767d5bde956SThomas Hellstrom 	res = &ushader->shader.res;
768d5bde956SThomas Hellstrom 	ushader->base.shareable = false;
769d5bde956SThomas Hellstrom 	ushader->base.tfile = NULL;
770d5bde956SThomas Hellstrom 
771d5bde956SThomas Hellstrom 	/*
772d5bde956SThomas Hellstrom 	 * From here on, the destructor takes over resource freeing.
773d5bde956SThomas Hellstrom 	 */
774d5bde956SThomas Hellstrom 
775d5bde956SThomas Hellstrom 	ret = vmw_gb_shader_init(dev_priv, res, shader_size,
776d80efd5cSThomas Hellstrom 				 offset, shader_type, num_input_sig,
777d80efd5cSThomas Hellstrom 				 num_output_sig, buffer,
778d5bde956SThomas Hellstrom 				 vmw_user_shader_free);
779d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
780d5bde956SThomas Hellstrom 		goto out;
781d5bde956SThomas Hellstrom 
782d5bde956SThomas Hellstrom 	tmp = vmw_resource_reference(res);
783d5bde956SThomas Hellstrom 	ret = ttm_base_object_init(tfile, &ushader->base, false,
784d5bde956SThomas Hellstrom 				   VMW_RES_SHADER,
785d5bde956SThomas Hellstrom 				   &vmw_user_shader_base_release, NULL);
786d5bde956SThomas Hellstrom 
787d5bde956SThomas Hellstrom 	if (unlikely(ret != 0)) {
788d5bde956SThomas Hellstrom 		vmw_resource_unreference(&tmp);
789d5bde956SThomas Hellstrom 		goto out_err;
790d5bde956SThomas Hellstrom 	}
791d5bde956SThomas Hellstrom 
792d5bde956SThomas Hellstrom 	if (handle)
793c7eae626SThomas Hellstrom 		*handle = ushader->base.handle;
794d5bde956SThomas Hellstrom out_err:
795d5bde956SThomas Hellstrom 	vmw_resource_unreference(&res);
796d5bde956SThomas Hellstrom out:
797d5bde956SThomas Hellstrom 	return ret;
798d5bde956SThomas Hellstrom }
799d5bde956SThomas Hellstrom 
800d5bde956SThomas Hellstrom 
801b9eb1a61SThomas Hellstrom static struct vmw_resource *vmw_shader_alloc(struct vmw_private *dev_priv,
802f1d34bfdSThomas Hellstrom 					     struct vmw_buffer_object *buffer,
80318e4a466SThomas Hellstrom 					     size_t shader_size,
80418e4a466SThomas Hellstrom 					     size_t offset,
80518e4a466SThomas Hellstrom 					     SVGA3dShaderType shader_type)
80618e4a466SThomas Hellstrom {
80718e4a466SThomas Hellstrom 	struct vmw_shader *shader;
80818e4a466SThomas Hellstrom 	struct vmw_resource *res;
809279c01f6SRoger He 	struct ttm_operation_ctx ctx = {
810279c01f6SRoger He 		.interruptible = true,
811279c01f6SRoger He 		.no_wait_gpu = false
812279c01f6SRoger He 	};
81318e4a466SThomas Hellstrom 	int ret;
81418e4a466SThomas Hellstrom 
81518e4a466SThomas Hellstrom 	if (unlikely(vmw_shader_size == 0))
81618e4a466SThomas Hellstrom 		vmw_shader_size =
817c7eae626SThomas Hellstrom 			ttm_round_pot(sizeof(struct vmw_shader)) +
818c7eae626SThomas Hellstrom 			VMW_IDA_ACC_SIZE;
81918e4a466SThomas Hellstrom 
82018e4a466SThomas Hellstrom 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
82118e4a466SThomas Hellstrom 				   vmw_shader_size,
822279c01f6SRoger He 				   &ctx);
82318e4a466SThomas Hellstrom 	if (unlikely(ret != 0)) {
82418e4a466SThomas Hellstrom 		if (ret != -ERESTARTSYS)
82518e4a466SThomas Hellstrom 			DRM_ERROR("Out of graphics memory for shader "
82618e4a466SThomas Hellstrom 				  "creation.\n");
82718e4a466SThomas Hellstrom 		goto out_err;
82818e4a466SThomas Hellstrom 	}
82918e4a466SThomas Hellstrom 
83018e4a466SThomas Hellstrom 	shader = kzalloc(sizeof(*shader), GFP_KERNEL);
8311a4adb05SRavikant B Sharma 	if (unlikely(!shader)) {
83218e4a466SThomas Hellstrom 		ttm_mem_global_free(vmw_mem_glob(dev_priv),
83318e4a466SThomas Hellstrom 				    vmw_shader_size);
83418e4a466SThomas Hellstrom 		ret = -ENOMEM;
83518e4a466SThomas Hellstrom 		goto out_err;
83618e4a466SThomas Hellstrom 	}
83718e4a466SThomas Hellstrom 
83818e4a466SThomas Hellstrom 	res = &shader->res;
83918e4a466SThomas Hellstrom 
84018e4a466SThomas Hellstrom 	/*
84118e4a466SThomas Hellstrom 	 * From here on, the destructor takes over resource freeing.
84218e4a466SThomas Hellstrom 	 */
84318e4a466SThomas Hellstrom 	ret = vmw_gb_shader_init(dev_priv, res, shader_size,
844d80efd5cSThomas Hellstrom 				 offset, shader_type, 0, 0, buffer,
84518e4a466SThomas Hellstrom 				 vmw_shader_free);
84618e4a466SThomas Hellstrom 
84718e4a466SThomas Hellstrom out_err:
84818e4a466SThomas Hellstrom 	return ret ? ERR_PTR(ret) : res;
84918e4a466SThomas Hellstrom }
85018e4a466SThomas Hellstrom 
85118e4a466SThomas Hellstrom 
852d80efd5cSThomas Hellstrom static int vmw_shader_define(struct drm_device *dev, struct drm_file *file_priv,
853d80efd5cSThomas Hellstrom 			     enum drm_vmw_shader_type shader_type_drm,
854d80efd5cSThomas Hellstrom 			     u32 buffer_handle, size_t size, size_t offset,
855d80efd5cSThomas Hellstrom 			     uint8_t num_input_sig, uint8_t num_output_sig,
856d80efd5cSThomas Hellstrom 			     uint32_t *shader_handle)
857c74c162fSThomas Hellstrom {
858c74c162fSThomas Hellstrom 	struct vmw_private *dev_priv = vmw_priv(dev);
859c74c162fSThomas Hellstrom 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
860f1d34bfdSThomas Hellstrom 	struct vmw_buffer_object *buffer = NULL;
861c74c162fSThomas Hellstrom 	SVGA3dShaderType shader_type;
862c74c162fSThomas Hellstrom 	int ret;
863c74c162fSThomas Hellstrom 
864d80efd5cSThomas Hellstrom 	if (buffer_handle != SVGA3D_INVALID_ID) {
865f1d34bfdSThomas Hellstrom 		ret = vmw_user_bo_lookup(tfile, buffer_handle,
86654c12bc3SThomas Hellstrom 					     &buffer, NULL);
867c74c162fSThomas Hellstrom 		if (unlikely(ret != 0)) {
8685724f899SDeepak Rawat 			VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n");
869c74c162fSThomas Hellstrom 			return ret;
870c74c162fSThomas Hellstrom 		}
871c74c162fSThomas Hellstrom 
872c74c162fSThomas Hellstrom 		if ((u64)buffer->base.num_pages * PAGE_SIZE <
873d80efd5cSThomas Hellstrom 		    (u64)size + (u64)offset) {
8745724f899SDeepak Rawat 			VMW_DEBUG_USER("Illegal buffer- or shader size.\n");
875c74c162fSThomas Hellstrom 			ret = -EINVAL;
876c74c162fSThomas Hellstrom 			goto out_bad_arg;
877c74c162fSThomas Hellstrom 		}
878c74c162fSThomas Hellstrom 	}
879c74c162fSThomas Hellstrom 
880d80efd5cSThomas Hellstrom 	switch (shader_type_drm) {
881c74c162fSThomas Hellstrom 	case drm_vmw_shader_type_vs:
882c74c162fSThomas Hellstrom 		shader_type = SVGA3D_SHADERTYPE_VS;
883c74c162fSThomas Hellstrom 		break;
884c74c162fSThomas Hellstrom 	case drm_vmw_shader_type_ps:
885c74c162fSThomas Hellstrom 		shader_type = SVGA3D_SHADERTYPE_PS;
886c74c162fSThomas Hellstrom 		break;
887c74c162fSThomas Hellstrom 	default:
8885724f899SDeepak Rawat 		VMW_DEBUG_USER("Illegal shader type.\n");
889c74c162fSThomas Hellstrom 		ret = -EINVAL;
890c74c162fSThomas Hellstrom 		goto out_bad_arg;
891c74c162fSThomas Hellstrom 	}
892c74c162fSThomas Hellstrom 
893294adf7dSThomas Hellstrom 	ret = ttm_read_lock(&dev_priv->reservation_sem, true);
894c74c162fSThomas Hellstrom 	if (unlikely(ret != 0))
895d5bde956SThomas Hellstrom 		goto out_bad_arg;
896c74c162fSThomas Hellstrom 
897d80efd5cSThomas Hellstrom 	ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset,
898d80efd5cSThomas Hellstrom 				    shader_type, num_input_sig,
899d80efd5cSThomas Hellstrom 				    num_output_sig, tfile, shader_handle);
900c74c162fSThomas Hellstrom 
901294adf7dSThomas Hellstrom 	ttm_read_unlock(&dev_priv->reservation_sem);
902c74c162fSThomas Hellstrom out_bad_arg:
903f1d34bfdSThomas Hellstrom 	vmw_bo_unreference(&buffer);
904d5bde956SThomas Hellstrom 	return ret;
905d5bde956SThomas Hellstrom }
906c74c162fSThomas Hellstrom 
907d5bde956SThomas Hellstrom /**
908d80efd5cSThomas Hellstrom  * vmw_shader_id_ok - Check whether a compat shader user key and
90918e4a466SThomas Hellstrom  * shader type are within valid bounds.
910d5bde956SThomas Hellstrom  *
91118e4a466SThomas Hellstrom  * @user_key: User space id of the shader.
91218e4a466SThomas Hellstrom  * @shader_type: Shader type.
913d5bde956SThomas Hellstrom  *
91418e4a466SThomas Hellstrom  * Returns true if valid false if not.
915d5bde956SThomas Hellstrom  */
916d80efd5cSThomas Hellstrom static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type)
917d5bde956SThomas Hellstrom {
91818e4a466SThomas Hellstrom 	return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16;
919d5bde956SThomas Hellstrom }
920d5bde956SThomas Hellstrom 
921d5bde956SThomas Hellstrom /**
922d80efd5cSThomas Hellstrom  * vmw_shader_key - Compute a hash key suitable for a compat shader.
923d5bde956SThomas Hellstrom  *
92418e4a466SThomas Hellstrom  * @user_key: User space id of the shader.
92518e4a466SThomas Hellstrom  * @shader_type: Shader type.
926d5bde956SThomas Hellstrom  *
92718e4a466SThomas Hellstrom  * Returns a hash key suitable for a command buffer managed resource
92818e4a466SThomas Hellstrom  * manager hash table.
929d5bde956SThomas Hellstrom  */
930d80efd5cSThomas Hellstrom static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type)
931d5bde956SThomas Hellstrom {
93218e4a466SThomas Hellstrom 	return user_key | (shader_type << 20);
933d5bde956SThomas Hellstrom }
934d5bde956SThomas Hellstrom 
935d5bde956SThomas Hellstrom /**
936d80efd5cSThomas Hellstrom  * vmw_shader_remove - Stage a compat shader for removal.
937d5bde956SThomas Hellstrom  *
93818e4a466SThomas Hellstrom  * @man: Pointer to the compat shader manager identifying the shader namespace.
939d5bde956SThomas Hellstrom  * @user_key: The key that is used to identify the shader. The key is
940d5bde956SThomas Hellstrom  * unique to the shader type.
941d5bde956SThomas Hellstrom  * @shader_type: Shader type.
94218e4a466SThomas Hellstrom  * @list: Caller's list of staged command buffer resource actions.
943d5bde956SThomas Hellstrom  */
944d80efd5cSThomas Hellstrom int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
945d5bde956SThomas Hellstrom 		      u32 user_key, SVGA3dShaderType shader_type,
946d5bde956SThomas Hellstrom 		      struct list_head *list)
947d5bde956SThomas Hellstrom {
948d80efd5cSThomas Hellstrom 	struct vmw_resource *dummy;
949d80efd5cSThomas Hellstrom 
950d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
951d5bde956SThomas Hellstrom 		return -EINVAL;
952d5bde956SThomas Hellstrom 
953d80efd5cSThomas Hellstrom 	return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader,
954d80efd5cSThomas Hellstrom 				     vmw_shader_key(user_key, shader_type),
955d80efd5cSThomas Hellstrom 				     list, &dummy);
956d5bde956SThomas Hellstrom }
957d5bde956SThomas Hellstrom 
958d5bde956SThomas Hellstrom /**
95918e4a466SThomas Hellstrom  * vmw_compat_shader_add - Create a compat shader and stage it for addition
96018e4a466SThomas Hellstrom  * as a command buffer managed resource.
961d5bde956SThomas Hellstrom  *
96218e4a466SThomas Hellstrom  * @man: Pointer to the compat shader manager identifying the shader namespace.
963d5bde956SThomas Hellstrom  * @user_key: The key that is used to identify the shader. The key is
964d5bde956SThomas Hellstrom  * unique to the shader type.
965d5bde956SThomas Hellstrom  * @bytecode: Pointer to the bytecode of the shader.
966d5bde956SThomas Hellstrom  * @shader_type: Shader type.
967d5bde956SThomas Hellstrom  * @tfile: Pointer to a struct ttm_object_file that the guest-backed shader is
968d5bde956SThomas Hellstrom  * to be created with.
96918e4a466SThomas Hellstrom  * @list: Caller's list of staged command buffer resource actions.
970d5bde956SThomas Hellstrom  *
971d5bde956SThomas Hellstrom  */
97218e4a466SThomas Hellstrom int vmw_compat_shader_add(struct vmw_private *dev_priv,
97318e4a466SThomas Hellstrom 			  struct vmw_cmdbuf_res_manager *man,
974d5bde956SThomas Hellstrom 			  u32 user_key, const void *bytecode,
975d5bde956SThomas Hellstrom 			  SVGA3dShaderType shader_type,
976d5bde956SThomas Hellstrom 			  size_t size,
977d5bde956SThomas Hellstrom 			  struct list_head *list)
978d5bde956SThomas Hellstrom {
97919be5570SChristian König 	struct ttm_operation_ctx ctx = { false, true };
980f1d34bfdSThomas Hellstrom 	struct vmw_buffer_object *buf;
981d5bde956SThomas Hellstrom 	struct ttm_bo_kmap_obj map;
982d5bde956SThomas Hellstrom 	bool is_iomem;
983d5bde956SThomas Hellstrom 	int ret;
98418e4a466SThomas Hellstrom 	struct vmw_resource *res;
985d5bde956SThomas Hellstrom 
986d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
987d5bde956SThomas Hellstrom 		return -EINVAL;
988d5bde956SThomas Hellstrom 
989d5bde956SThomas Hellstrom 	/* Allocate and pin a DMA buffer */
990d5bde956SThomas Hellstrom 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
9911a4adb05SRavikant B Sharma 	if (unlikely(!buf))
992d5bde956SThomas Hellstrom 		return -ENOMEM;
993d5bde956SThomas Hellstrom 
994f1d34bfdSThomas Hellstrom 	ret = vmw_bo_init(dev_priv, buf, size, &vmw_sys_ne_placement,
995f1d34bfdSThomas Hellstrom 			      true, vmw_bo_bo_free);
996d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
997d5bde956SThomas Hellstrom 		goto out;
998d5bde956SThomas Hellstrom 
999dfd5e50eSChristian König 	ret = ttm_bo_reserve(&buf->base, false, true, NULL);
1000d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
1001d5bde956SThomas Hellstrom 		goto no_reserve;
1002d5bde956SThomas Hellstrom 
1003d5bde956SThomas Hellstrom 	/* Map and copy shader bytecode. */
1004d5bde956SThomas Hellstrom 	ret = ttm_bo_kmap(&buf->base, 0, PAGE_ALIGN(size) >> PAGE_SHIFT,
1005d5bde956SThomas Hellstrom 			  &map);
1006d5bde956SThomas Hellstrom 	if (unlikely(ret != 0)) {
1007d5bde956SThomas Hellstrom 		ttm_bo_unreserve(&buf->base);
1008d5bde956SThomas Hellstrom 		goto no_reserve;
1009d5bde956SThomas Hellstrom 	}
1010d5bde956SThomas Hellstrom 
1011d5bde956SThomas Hellstrom 	memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size);
1012d5bde956SThomas Hellstrom 	WARN_ON(is_iomem);
1013d5bde956SThomas Hellstrom 
1014d5bde956SThomas Hellstrom 	ttm_bo_kunmap(&map);
101519be5570SChristian König 	ret = ttm_bo_validate(&buf->base, &vmw_sys_placement, &ctx);
1016d5bde956SThomas Hellstrom 	WARN_ON(ret != 0);
1017d5bde956SThomas Hellstrom 	ttm_bo_unreserve(&buf->base);
1018d5bde956SThomas Hellstrom 
101918e4a466SThomas Hellstrom 	res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type);
1020d5bde956SThomas Hellstrom 	if (unlikely(ret != 0))
1021d5bde956SThomas Hellstrom 		goto no_reserve;
1022d5bde956SThomas Hellstrom 
1023d80efd5cSThomas Hellstrom 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
1024d80efd5cSThomas Hellstrom 				 vmw_shader_key(user_key, shader_type),
102518e4a466SThomas Hellstrom 				 res, list);
102618e4a466SThomas Hellstrom 	vmw_resource_unreference(&res);
1027d5bde956SThomas Hellstrom no_reserve:
1028f1d34bfdSThomas Hellstrom 	vmw_bo_unreference(&buf);
1029d5bde956SThomas Hellstrom out:
1030d5bde956SThomas Hellstrom 	return ret;
1031d5bde956SThomas Hellstrom }
1032d5bde956SThomas Hellstrom 
1033d5bde956SThomas Hellstrom /**
1034d80efd5cSThomas Hellstrom  * vmw_shader_lookup - Look up a compat shader
1035d5bde956SThomas Hellstrom  *
103618e4a466SThomas Hellstrom  * @man: Pointer to the command buffer managed resource manager identifying
103718e4a466SThomas Hellstrom  * the shader namespace.
103818e4a466SThomas Hellstrom  * @user_key: The user space id of the shader.
103918e4a466SThomas Hellstrom  * @shader_type: The shader type.
1040d5bde956SThomas Hellstrom  *
104118e4a466SThomas Hellstrom  * Returns a refcounted pointer to a struct vmw_resource if the shader was
104218e4a466SThomas Hellstrom  * found. An error pointer otherwise.
1043d5bde956SThomas Hellstrom  */
104418e4a466SThomas Hellstrom struct vmw_resource *
1045d80efd5cSThomas Hellstrom vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
104618e4a466SThomas Hellstrom 		  u32 user_key,
104718e4a466SThomas Hellstrom 		  SVGA3dShaderType shader_type)
1048d5bde956SThomas Hellstrom {
1049d80efd5cSThomas Hellstrom 	if (!vmw_shader_id_ok(user_key, shader_type))
105018e4a466SThomas Hellstrom 		return ERR_PTR(-EINVAL);
1051d5bde956SThomas Hellstrom 
1052d80efd5cSThomas Hellstrom 	return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader,
1053d80efd5cSThomas Hellstrom 				     vmw_shader_key(user_key, shader_type));
1054d80efd5cSThomas Hellstrom }
1055d80efd5cSThomas Hellstrom 
1056d80efd5cSThomas Hellstrom int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
1057d80efd5cSThomas Hellstrom 			     struct drm_file *file_priv)
1058d80efd5cSThomas Hellstrom {
1059d80efd5cSThomas Hellstrom 	struct drm_vmw_shader_create_arg *arg =
1060d80efd5cSThomas Hellstrom 		(struct drm_vmw_shader_create_arg *)data;
1061d80efd5cSThomas Hellstrom 
1062d80efd5cSThomas Hellstrom 	return vmw_shader_define(dev, file_priv, arg->shader_type,
1063d80efd5cSThomas Hellstrom 				 arg->buffer_handle,
1064d80efd5cSThomas Hellstrom 				 arg->size, arg->offset,
1065d80efd5cSThomas Hellstrom 				 0, 0,
1066d80efd5cSThomas Hellstrom 				 &arg->shader_handle);
1067c74c162fSThomas Hellstrom }
1068