xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/vmwgfx/vmwgfx_shader.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: vmwgfx_shader.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
2 
3 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 /**************************************************************************
5  *
6  * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_shader.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
32 
33 #include <drm/ttm/ttm_placement.h>
34 
35 #include "vmwgfx_drv.h"
36 #include "vmwgfx_resource_priv.h"
37 #include "vmwgfx_binding.h"
38 
39 struct vmw_shader {
40 	struct vmw_resource res;
41 	SVGA3dShaderType type;
42 	uint32_t size;
43 	uint8_t num_input_sig;
44 	uint8_t num_output_sig;
45 };
46 
47 struct vmw_user_shader {
48 	struct ttm_base_object base;
49 	struct vmw_shader shader;
50 };
51 
52 struct vmw_dx_shader {
53 	struct vmw_resource res;
54 	struct vmw_resource *ctx;
55 	struct vmw_resource *cotable;
56 	u32 id;
57 	bool committed;
58 	struct list_head cotable_head;
59 };
60 
61 static uint64_t vmw_user_shader_size;
62 static uint64_t vmw_shader_size;
63 static size_t vmw_shader_dx_size;
64 
65 static void vmw_user_shader_free(struct vmw_resource *res);
66 static struct vmw_resource *
67 vmw_user_shader_base_to_res(struct ttm_base_object *base);
68 
69 static int vmw_gb_shader_create(struct vmw_resource *res);
70 static int vmw_gb_shader_bind(struct vmw_resource *res,
71 			       struct ttm_validate_buffer *val_buf);
72 static int vmw_gb_shader_unbind(struct vmw_resource *res,
73 				 bool readback,
74 				 struct ttm_validate_buffer *val_buf);
75 static int vmw_gb_shader_destroy(struct vmw_resource *res);
76 
77 static int vmw_dx_shader_create(struct vmw_resource *res);
78 static int vmw_dx_shader_bind(struct vmw_resource *res,
79 			       struct ttm_validate_buffer *val_buf);
80 static int vmw_dx_shader_unbind(struct vmw_resource *res,
81 				 bool readback,
82 				 struct ttm_validate_buffer *val_buf);
83 static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
84 					enum vmw_cmdbuf_res_state state);
85 static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type);
86 static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type);
87 static uint64_t vmw_user_shader_size;
88 
89 static const struct vmw_user_resource_conv user_shader_conv = {
90 	.object_type = VMW_RES_SHADER,
91 	.base_obj_to_res = vmw_user_shader_base_to_res,
92 	.res_free = vmw_user_shader_free
93 };
94 
95 const struct vmw_user_resource_conv *user_shader_converter =
96 	&user_shader_conv;
97 
98 
99 static const struct vmw_res_func vmw_gb_shader_func = {
100 	.res_type = vmw_res_shader,
101 	.needs_backup = true,
102 	.may_evict = true,
103 	.prio = 3,
104 	.dirty_prio = 3,
105 	.type_name = "guest backed shaders",
106 	.backup_placement = &vmw_mob_placement,
107 	.create = vmw_gb_shader_create,
108 	.destroy = vmw_gb_shader_destroy,
109 	.bind = vmw_gb_shader_bind,
110 	.unbind = vmw_gb_shader_unbind
111 };
112 
113 static const struct vmw_res_func vmw_dx_shader_func = {
114 	.res_type = vmw_res_shader,
115 	.needs_backup = true,
116 	.may_evict = true,
117 	.prio = 3,
118 	.dirty_prio = 3,
119 	.type_name = "dx shaders",
120 	.backup_placement = &vmw_mob_placement,
121 	.create = vmw_dx_shader_create,
122 	/*
123 	 * The destroy callback is only called with a committed resource on
124 	 * context destroy, in which case we destroy the cotable anyway,
125 	 * so there's no need to destroy DX shaders separately.
126 	 */
127 	.destroy = NULL,
128 	.bind = vmw_dx_shader_bind,
129 	.unbind = vmw_dx_shader_unbind,
130 	.commit_notify = vmw_dx_shader_commit_notify,
131 };
132 
133 /**
134  * Shader management:
135  */
136 
137 static inline struct vmw_shader *
vmw_res_to_shader(struct vmw_resource * res)138 vmw_res_to_shader(struct vmw_resource *res)
139 {
140 	return container_of(res, struct vmw_shader, res);
141 }
142 
143 /**
144  * vmw_res_to_dx_shader - typecast a struct vmw_resource to a
145  * struct vmw_dx_shader
146  *
147  * @res: Pointer to the struct vmw_resource.
148  */
149 static inline struct vmw_dx_shader *
vmw_res_to_dx_shader(struct vmw_resource * res)150 vmw_res_to_dx_shader(struct vmw_resource *res)
151 {
152 	return container_of(res, struct vmw_dx_shader, res);
153 }
154 
vmw_hw_shader_destroy(struct vmw_resource * res)155 static void vmw_hw_shader_destroy(struct vmw_resource *res)
156 {
157 	if (likely(res->func->destroy))
158 		(void) res->func->destroy(res);
159 	else
160 		res->id = -1;
161 }
162 
163 
vmw_gb_shader_init(struct vmw_private * dev_priv,struct vmw_resource * res,uint32_t size,uint64_t offset,SVGA3dShaderType type,uint8_t num_input_sig,uint8_t num_output_sig,struct vmw_buffer_object * byte_code,void (* res_free)(struct vmw_resource * res))164 static int vmw_gb_shader_init(struct vmw_private *dev_priv,
165 			      struct vmw_resource *res,
166 			      uint32_t size,
167 			      uint64_t offset,
168 			      SVGA3dShaderType type,
169 			      uint8_t num_input_sig,
170 			      uint8_t num_output_sig,
171 			      struct vmw_buffer_object *byte_code,
172 			      void (*res_free) (struct vmw_resource *res))
173 {
174 	struct vmw_shader *shader = vmw_res_to_shader(res);
175 	int ret;
176 
177 	ret = vmw_resource_init(dev_priv, res, true, res_free,
178 				&vmw_gb_shader_func);
179 
180 	if (unlikely(ret != 0)) {
181 		if (res_free)
182 			res_free(res);
183 		else
184 			kfree(res);
185 		return ret;
186 	}
187 
188 	res->backup_size = size;
189 	if (byte_code) {
190 		res->backup = vmw_bo_reference(byte_code);
191 		res->backup_offset = offset;
192 	}
193 	shader->size = size;
194 	shader->type = type;
195 	shader->num_input_sig = num_input_sig;
196 	shader->num_output_sig = num_output_sig;
197 
198 	res->hw_destroy = vmw_hw_shader_destroy;
199 	return 0;
200 }
201 
202 /*
203  * GB shader code:
204  */
205 
vmw_gb_shader_create(struct vmw_resource * res)206 static int vmw_gb_shader_create(struct vmw_resource *res)
207 {
208 	struct vmw_private *dev_priv = res->dev_priv;
209 	struct vmw_shader *shader = vmw_res_to_shader(res);
210 	int ret;
211 	struct {
212 		SVGA3dCmdHeader header;
213 		SVGA3dCmdDefineGBShader body;
214 	} *cmd;
215 
216 	if (likely(res->id != -1))
217 		return 0;
218 
219 	ret = vmw_resource_alloc_id(res);
220 	if (unlikely(ret != 0)) {
221 		DRM_ERROR("Failed to allocate a shader id.\n");
222 		goto out_no_id;
223 	}
224 
225 	if (unlikely(res->id >= VMWGFX_NUM_GB_SHADER)) {
226 		ret = -EBUSY;
227 		goto out_no_fifo;
228 	}
229 
230 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
231 	if (unlikely(cmd == NULL)) {
232 		ret = -ENOMEM;
233 		goto out_no_fifo;
234 	}
235 
236 	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER;
237 	cmd->header.size = sizeof(cmd->body);
238 	cmd->body.shid = res->id;
239 	cmd->body.type = shader->type;
240 	cmd->body.sizeInBytes = shader->size;
241 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
242 	vmw_fifo_resource_inc(dev_priv);
243 
244 	return 0;
245 
246 out_no_fifo:
247 	vmw_resource_release_id(res);
248 out_no_id:
249 	return ret;
250 }
251 
vmw_gb_shader_bind(struct vmw_resource * res,struct ttm_validate_buffer * val_buf)252 static int vmw_gb_shader_bind(struct vmw_resource *res,
253 			      struct ttm_validate_buffer *val_buf)
254 {
255 	struct vmw_private *dev_priv = res->dev_priv;
256 	struct {
257 		SVGA3dCmdHeader header;
258 		SVGA3dCmdBindGBShader body;
259 	} *cmd;
260 	struct ttm_buffer_object *bo = val_buf->bo;
261 
262 	BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
263 
264 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
265 	if (unlikely(cmd == NULL))
266 		return -ENOMEM;
267 
268 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
269 	cmd->header.size = sizeof(cmd->body);
270 	cmd->body.shid = res->id;
271 	cmd->body.mobid = bo->mem.start;
272 	cmd->body.offsetInBytes = res->backup_offset;
273 	res->backup_dirty = false;
274 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
275 
276 	return 0;
277 }
278 
vmw_gb_shader_unbind(struct vmw_resource * res,bool readback,struct ttm_validate_buffer * val_buf)279 static int vmw_gb_shader_unbind(struct vmw_resource *res,
280 				bool readback,
281 				struct ttm_validate_buffer *val_buf)
282 {
283 	struct vmw_private *dev_priv = res->dev_priv;
284 	struct {
285 		SVGA3dCmdHeader header;
286 		SVGA3dCmdBindGBShader body;
287 	} *cmd;
288 	struct vmw_fence_obj *fence;
289 
290 	BUG_ON(res->backup->base.mem.mem_type != VMW_PL_MOB);
291 
292 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
293 	if (unlikely(cmd == NULL))
294 		return -ENOMEM;
295 
296 	cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
297 	cmd->header.size = sizeof(cmd->body);
298 	cmd->body.shid = res->id;
299 	cmd->body.mobid = SVGA3D_INVALID_ID;
300 	cmd->body.offsetInBytes = 0;
301 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
302 
303 	/*
304 	 * Create a fence object and fence the backup buffer.
305 	 */
306 
307 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
308 					  &fence, NULL);
309 
310 	vmw_bo_fence_single(val_buf->bo, fence);
311 
312 	if (likely(fence != NULL))
313 		vmw_fence_obj_unreference(&fence);
314 
315 	return 0;
316 }
317 
vmw_gb_shader_destroy(struct vmw_resource * res)318 static int vmw_gb_shader_destroy(struct vmw_resource *res)
319 {
320 	struct vmw_private *dev_priv = res->dev_priv;
321 	struct {
322 		SVGA3dCmdHeader header;
323 		SVGA3dCmdDestroyGBShader body;
324 	} *cmd;
325 
326 	if (likely(res->id == -1))
327 		return 0;
328 
329 	mutex_lock(&dev_priv->binding_mutex);
330 	vmw_binding_res_list_scrub(&res->binding_head);
331 
332 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
333 	if (unlikely(cmd == NULL)) {
334 		mutex_unlock(&dev_priv->binding_mutex);
335 		return -ENOMEM;
336 	}
337 
338 	cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER;
339 	cmd->header.size = sizeof(cmd->body);
340 	cmd->body.shid = res->id;
341 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
342 	mutex_unlock(&dev_priv->binding_mutex);
343 	vmw_resource_release_id(res);
344 	vmw_fifo_resource_dec(dev_priv);
345 
346 	return 0;
347 }
348 
349 /*
350  * DX shader code:
351  */
352 
353 /**
354  * vmw_dx_shader_commit_notify - Notify that a shader operation has been
355  * committed to hardware from a user-supplied command stream.
356  *
357  * @res: Pointer to the shader resource.
358  * @state: Indicating whether a creation or removal has been committed.
359  *
360  */
vmw_dx_shader_commit_notify(struct vmw_resource * res,enum vmw_cmdbuf_res_state state)361 static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
362 					enum vmw_cmdbuf_res_state state)
363 {
364 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
365 	struct vmw_private *dev_priv = res->dev_priv;
366 
367 	if (state == VMW_CMDBUF_RES_ADD) {
368 		mutex_lock(&dev_priv->binding_mutex);
369 		vmw_cotable_add_resource(shader->cotable,
370 					 &shader->cotable_head);
371 		shader->committed = true;
372 		res->id = shader->id;
373 		mutex_unlock(&dev_priv->binding_mutex);
374 	} else {
375 		mutex_lock(&dev_priv->binding_mutex);
376 		list_del_init(&shader->cotable_head);
377 		shader->committed = false;
378 		res->id = -1;
379 		mutex_unlock(&dev_priv->binding_mutex);
380 	}
381 }
382 
383 /**
384  * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader.
385  *
386  * @res: The shader resource
387  *
388  * This function reverts a scrub operation.
389  */
vmw_dx_shader_unscrub(struct vmw_resource * res)390 static int vmw_dx_shader_unscrub(struct vmw_resource *res)
391 {
392 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
393 	struct vmw_private *dev_priv = res->dev_priv;
394 	struct {
395 		SVGA3dCmdHeader header;
396 		SVGA3dCmdDXBindShader body;
397 	} *cmd;
398 
399 	if (!list_empty(&shader->cotable_head) || !shader->committed)
400 		return 0;
401 
402 	cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), shader->ctx->id);
403 	if (unlikely(cmd == NULL))
404 		return -ENOMEM;
405 
406 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
407 	cmd->header.size = sizeof(cmd->body);
408 	cmd->body.cid = shader->ctx->id;
409 	cmd->body.shid = shader->id;
410 	cmd->body.mobid = res->backup->base.mem.start;
411 	cmd->body.offsetInBytes = res->backup_offset;
412 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
413 
414 	vmw_cotable_add_resource(shader->cotable, &shader->cotable_head);
415 
416 	return 0;
417 }
418 
419 /**
420  * vmw_dx_shader_create - The DX shader create callback
421  *
422  * @res: The DX shader resource
423  *
424  * The create callback is called as part of resource validation and
425  * makes sure that we unscrub the shader if it's previously been scrubbed.
426  */
vmw_dx_shader_create(struct vmw_resource * res)427 static int vmw_dx_shader_create(struct vmw_resource *res)
428 {
429 	struct vmw_private *dev_priv = res->dev_priv;
430 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
431 	int ret = 0;
432 
433 	WARN_ON_ONCE(!shader->committed);
434 
435 	if (vmw_resource_mob_attached(res)) {
436 		mutex_lock(&dev_priv->binding_mutex);
437 		ret = vmw_dx_shader_unscrub(res);
438 		mutex_unlock(&dev_priv->binding_mutex);
439 	}
440 
441 	res->id = shader->id;
442 	return ret;
443 }
444 
445 /**
446  * vmw_dx_shader_bind - The DX shader bind callback
447  *
448  * @res: The DX shader resource
449  * @val_buf: Pointer to the validate buffer.
450  *
451  */
vmw_dx_shader_bind(struct vmw_resource * res,struct ttm_validate_buffer * val_buf)452 static int vmw_dx_shader_bind(struct vmw_resource *res,
453 			      struct ttm_validate_buffer *val_buf)
454 {
455 	struct vmw_private *dev_priv = res->dev_priv;
456 	struct ttm_buffer_object *bo = val_buf->bo;
457 
458 	BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
459 	mutex_lock(&dev_priv->binding_mutex);
460 	vmw_dx_shader_unscrub(res);
461 	mutex_unlock(&dev_priv->binding_mutex);
462 
463 	return 0;
464 }
465 
466 /**
467  * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader.
468  *
469  * @res: The shader resource
470  *
471  * This function unbinds a MOB from the DX shader without requiring the
472  * MOB dma_buffer to be reserved. The driver still considers the MOB bound.
473  * However, once the driver eventually decides to unbind the MOB, it doesn't
474  * need to access the context.
475  */
vmw_dx_shader_scrub(struct vmw_resource * res)476 static int vmw_dx_shader_scrub(struct vmw_resource *res)
477 {
478 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
479 	struct vmw_private *dev_priv = res->dev_priv;
480 	struct {
481 		SVGA3dCmdHeader header;
482 		SVGA3dCmdDXBindShader body;
483 	} *cmd;
484 
485 	if (list_empty(&shader->cotable_head))
486 		return 0;
487 
488 	WARN_ON_ONCE(!shader->committed);
489 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
490 	if (unlikely(cmd == NULL))
491 		return -ENOMEM;
492 
493 	cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
494 	cmd->header.size = sizeof(cmd->body);
495 	cmd->body.cid = shader->ctx->id;
496 	cmd->body.shid = res->id;
497 	cmd->body.mobid = SVGA3D_INVALID_ID;
498 	cmd->body.offsetInBytes = 0;
499 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
500 	res->id = -1;
501 	list_del_init(&shader->cotable_head);
502 
503 	return 0;
504 }
505 
506 /**
507  * vmw_dx_shader_unbind - The dx shader unbind callback.
508  *
509  * @res: The shader resource
510  * @readback: Whether this is a readback unbind. Currently unused.
511  * @val_buf: MOB buffer information.
512  */
vmw_dx_shader_unbind(struct vmw_resource * res,bool readback,struct ttm_validate_buffer * val_buf)513 static int vmw_dx_shader_unbind(struct vmw_resource *res,
514 				bool readback,
515 				struct ttm_validate_buffer *val_buf)
516 {
517 	struct vmw_private *dev_priv = res->dev_priv;
518 	struct vmw_fence_obj *fence;
519 	int ret;
520 
521 	BUG_ON(res->backup->base.mem.mem_type != VMW_PL_MOB);
522 
523 	mutex_lock(&dev_priv->binding_mutex);
524 	ret = vmw_dx_shader_scrub(res);
525 	mutex_unlock(&dev_priv->binding_mutex);
526 
527 	if (ret)
528 		return ret;
529 
530 	(void) vmw_execbuf_fence_commands(NULL, dev_priv,
531 					  &fence, NULL);
532 	vmw_bo_fence_single(val_buf->bo, fence);
533 
534 	if (likely(fence != NULL))
535 		vmw_fence_obj_unreference(&fence);
536 
537 	return 0;
538 }
539 
540 /**
541  * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for
542  * DX shaders.
543  *
544  * @dev_priv: Pointer to device private structure.
545  * @list: The list of cotable resources.
546  * @readback: Whether the call was part of a readback unbind.
547  *
548  * Scrubs all shader MOBs so that any subsequent shader unbind or shader
549  * destroy operation won't need to swap in the context.
550  */
vmw_dx_shader_cotable_list_scrub(struct vmw_private * dev_priv,struct list_head * list,bool readback)551 void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
552 				      struct list_head *list,
553 				      bool readback)
554 {
555 	struct vmw_dx_shader *entry, *next;
556 
557 	lockdep_assert_held_once(&dev_priv->binding_mutex);
558 
559 	list_for_each_entry_safe(entry, next, list, cotable_head) {
560 		WARN_ON(vmw_dx_shader_scrub(&entry->res));
561 		if (!readback)
562 			entry->committed = false;
563 	}
564 }
565 
566 /**
567  * vmw_dx_shader_res_free - The DX shader free callback
568  *
569  * @res: The shader resource
570  *
571  * Frees the DX shader resource and updates memory accounting.
572  */
vmw_dx_shader_res_free(struct vmw_resource * res)573 static void vmw_dx_shader_res_free(struct vmw_resource *res)
574 {
575 	struct vmw_private *dev_priv = res->dev_priv;
576 	struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
577 
578 	vmw_resource_unreference(&shader->cotable);
579 	kfree(shader);
580 	ttm_mem_global_free(vmw_mem_glob(dev_priv), vmw_shader_dx_size);
581 }
582 
583 /**
584  * vmw_dx_shader_add - Add a shader resource as a command buffer managed
585  * resource.
586  *
587  * @man: The command buffer resource manager.
588  * @ctx: Pointer to the context resource.
589  * @user_key: The id used for this shader.
590  * @shader_type: The shader type.
591  * @list: The list of staged command buffer managed resources.
592  */
vmw_dx_shader_add(struct vmw_cmdbuf_res_manager * man,struct vmw_resource * ctx,u32 user_key,SVGA3dShaderType shader_type,struct list_head * list)593 int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
594 		      struct vmw_resource *ctx,
595 		      u32 user_key,
596 		      SVGA3dShaderType shader_type,
597 		      struct list_head *list)
598 {
599 	struct vmw_dx_shader *shader;
600 	struct vmw_resource *res;
601 	struct vmw_private *dev_priv = ctx->dev_priv;
602 	struct ttm_operation_ctx ttm_opt_ctx = {
603 		.interruptible = true,
604 		.no_wait_gpu = false
605 	};
606 	int ret;
607 
608 	if (!vmw_shader_dx_size)
609 		vmw_shader_dx_size = ttm_round_pot(sizeof(*shader));
610 
611 	if (!vmw_shader_id_ok(user_key, shader_type))
612 		return -EINVAL;
613 
614 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), vmw_shader_dx_size,
615 				   &ttm_opt_ctx);
616 	if (ret) {
617 		if (ret != -ERESTARTSYS)
618 			DRM_ERROR("Out of graphics memory for shader "
619 				  "creation.\n");
620 		return ret;
621 	}
622 
623 	shader = kmalloc(sizeof(*shader), GFP_KERNEL);
624 	if (!shader) {
625 		ttm_mem_global_free(vmw_mem_glob(dev_priv), vmw_shader_dx_size);
626 		return -ENOMEM;
627 	}
628 
629 	res = &shader->res;
630 	shader->ctx = ctx;
631 	shader->cotable = vmw_resource_reference
632 		(vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER));
633 	shader->id = user_key;
634 	shader->committed = false;
635 	INIT_LIST_HEAD(&shader->cotable_head);
636 	ret = vmw_resource_init(dev_priv, res, true,
637 				vmw_dx_shader_res_free, &vmw_dx_shader_func);
638 	if (ret)
639 		goto out_resource_init;
640 
641 	/*
642 	 * The user_key name-space is not per shader type for DX shaders,
643 	 * so when hashing, use a single zero shader type.
644 	 */
645 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
646 				 vmw_shader_key(user_key, 0),
647 				 res, list);
648 	if (ret)
649 		goto out_resource_init;
650 
651 	res->id = shader->id;
652 	res->hw_destroy = vmw_hw_shader_destroy;
653 
654 out_resource_init:
655 	vmw_resource_unreference(&res);
656 
657 	return ret;
658 }
659 
660 
661 
662 /**
663  * User-space shader management:
664  */
665 
666 static struct vmw_resource *
vmw_user_shader_base_to_res(struct ttm_base_object * base)667 vmw_user_shader_base_to_res(struct ttm_base_object *base)
668 {
669 	return &(container_of(base, struct vmw_user_shader, base)->
670 		 shader.res);
671 }
672 
vmw_user_shader_free(struct vmw_resource * res)673 static void vmw_user_shader_free(struct vmw_resource *res)
674 {
675 	struct vmw_user_shader *ushader =
676 		container_of(res, struct vmw_user_shader, shader.res);
677 	struct vmw_private *dev_priv = res->dev_priv;
678 
679 	ttm_base_object_kfree(ushader, base);
680 	ttm_mem_global_free(vmw_mem_glob(dev_priv),
681 			    vmw_user_shader_size);
682 }
683 
vmw_shader_free(struct vmw_resource * res)684 static void vmw_shader_free(struct vmw_resource *res)
685 {
686 	struct vmw_shader *shader = vmw_res_to_shader(res);
687 	struct vmw_private *dev_priv = res->dev_priv;
688 
689 	kfree(shader);
690 	ttm_mem_global_free(vmw_mem_glob(dev_priv),
691 			    vmw_shader_size);
692 }
693 
694 /**
695  * This function is called when user space has no more references on the
696  * base object. It releases the base-object's reference on the resource object.
697  */
698 
vmw_user_shader_base_release(struct ttm_base_object ** p_base)699 static void vmw_user_shader_base_release(struct ttm_base_object **p_base)
700 {
701 	struct ttm_base_object *base = *p_base;
702 	struct vmw_resource *res = vmw_user_shader_base_to_res(base);
703 
704 	*p_base = NULL;
705 	vmw_resource_unreference(&res);
706 }
707 
vmw_shader_destroy_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)708 int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
709 			      struct drm_file *file_priv)
710 {
711 	struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data;
712 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
713 
714 	return ttm_ref_object_base_unref(tfile, arg->handle,
715 					 TTM_REF_USAGE);
716 }
717 
vmw_user_shader_alloc(struct vmw_private * dev_priv,struct vmw_buffer_object * buffer,size_t shader_size,size_t offset,SVGA3dShaderType shader_type,uint8_t num_input_sig,uint8_t num_output_sig,struct ttm_object_file * tfile,u32 * handle)718 static int vmw_user_shader_alloc(struct vmw_private *dev_priv,
719 				 struct vmw_buffer_object *buffer,
720 				 size_t shader_size,
721 				 size_t offset,
722 				 SVGA3dShaderType shader_type,
723 				 uint8_t num_input_sig,
724 				 uint8_t num_output_sig,
725 				 struct ttm_object_file *tfile,
726 				 u32 *handle)
727 {
728 	struct vmw_user_shader *ushader;
729 	struct vmw_resource *res, *tmp;
730 	struct ttm_operation_ctx ctx = {
731 		.interruptible = true,
732 		.no_wait_gpu = false
733 	};
734 	int ret;
735 
736 	if (unlikely(vmw_user_shader_size == 0))
737 		vmw_user_shader_size =
738 			ttm_round_pot(sizeof(struct vmw_user_shader)) +
739 			VMW_IDA_ACC_SIZE + TTM_OBJ_EXTRA_SIZE;
740 
741 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
742 				   vmw_user_shader_size,
743 				   &ctx);
744 	if (unlikely(ret != 0)) {
745 		if (ret != -ERESTARTSYS)
746 			DRM_ERROR("Out of graphics memory for shader "
747 				  "creation.\n");
748 		goto out;
749 	}
750 
751 	ushader = kzalloc(sizeof(*ushader), GFP_KERNEL);
752 	if (unlikely(!ushader)) {
753 		ttm_mem_global_free(vmw_mem_glob(dev_priv),
754 				    vmw_user_shader_size);
755 		ret = -ENOMEM;
756 		goto out;
757 	}
758 
759 	res = &ushader->shader.res;
760 	ushader->base.shareable = false;
761 	ushader->base.tfile = NULL;
762 
763 	/*
764 	 * From here on, the destructor takes over resource freeing.
765 	 */
766 
767 	ret = vmw_gb_shader_init(dev_priv, res, shader_size,
768 				 offset, shader_type, num_input_sig,
769 				 num_output_sig, buffer,
770 				 vmw_user_shader_free);
771 	if (unlikely(ret != 0))
772 		goto out;
773 
774 	tmp = vmw_resource_reference(res);
775 	ret = ttm_base_object_init(tfile, &ushader->base, false,
776 				   VMW_RES_SHADER,
777 				   &vmw_user_shader_base_release, NULL);
778 
779 	if (unlikely(ret != 0)) {
780 		vmw_resource_unreference(&tmp);
781 		goto out_err;
782 	}
783 
784 	if (handle)
785 		*handle = ushader->base.handle;
786 out_err:
787 	vmw_resource_unreference(&res);
788 out:
789 	return ret;
790 }
791 
792 
vmw_shader_alloc(struct vmw_private * dev_priv,struct vmw_buffer_object * buffer,size_t shader_size,size_t offset,SVGA3dShaderType shader_type)793 static struct vmw_resource *vmw_shader_alloc(struct vmw_private *dev_priv,
794 					     struct vmw_buffer_object *buffer,
795 					     size_t shader_size,
796 					     size_t offset,
797 					     SVGA3dShaderType shader_type)
798 {
799 	struct vmw_shader *shader;
800 	struct vmw_resource *res;
801 	struct ttm_operation_ctx ctx = {
802 		.interruptible = true,
803 		.no_wait_gpu = false
804 	};
805 	int ret;
806 
807 	if (unlikely(vmw_shader_size == 0))
808 		vmw_shader_size =
809 			ttm_round_pot(sizeof(struct vmw_shader)) +
810 			VMW_IDA_ACC_SIZE;
811 
812 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
813 				   vmw_shader_size,
814 				   &ctx);
815 	if (unlikely(ret != 0)) {
816 		if (ret != -ERESTARTSYS)
817 			DRM_ERROR("Out of graphics memory for shader "
818 				  "creation.\n");
819 		goto out_err;
820 	}
821 
822 	shader = kzalloc(sizeof(*shader), GFP_KERNEL);
823 	if (unlikely(!shader)) {
824 		ttm_mem_global_free(vmw_mem_glob(dev_priv),
825 				    vmw_shader_size);
826 		ret = -ENOMEM;
827 		goto out_err;
828 	}
829 
830 	res = &shader->res;
831 
832 	/*
833 	 * From here on, the destructor takes over resource freeing.
834 	 */
835 	ret = vmw_gb_shader_init(dev_priv, res, shader_size,
836 				 offset, shader_type, 0, 0, buffer,
837 				 vmw_shader_free);
838 
839 out_err:
840 	return ret ? ERR_PTR(ret) : res;
841 }
842 
843 
vmw_shader_define(struct drm_device * dev,struct drm_file * file_priv,enum drm_vmw_shader_type shader_type_drm,u32 buffer_handle,size_t size,size_t offset,uint8_t num_input_sig,uint8_t num_output_sig,uint32_t * shader_handle)844 static int vmw_shader_define(struct drm_device *dev, struct drm_file *file_priv,
845 			     enum drm_vmw_shader_type shader_type_drm,
846 			     u32 buffer_handle, size_t size, size_t offset,
847 			     uint8_t num_input_sig, uint8_t num_output_sig,
848 			     uint32_t *shader_handle)
849 {
850 	struct vmw_private *dev_priv = vmw_priv(dev);
851 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
852 	struct vmw_buffer_object *buffer = NULL;
853 	SVGA3dShaderType shader_type;
854 	int ret;
855 
856 	if (buffer_handle != SVGA3D_INVALID_ID) {
857 		ret = vmw_user_bo_lookup(tfile, buffer_handle,
858 					     &buffer, NULL);
859 		if (unlikely(ret != 0)) {
860 			VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n");
861 			return ret;
862 		}
863 
864 		if ((u64)buffer->base.num_pages * PAGE_SIZE <
865 		    (u64)size + (u64)offset) {
866 			VMW_DEBUG_USER("Illegal buffer- or shader size.\n");
867 			ret = -EINVAL;
868 			goto out_bad_arg;
869 		}
870 	}
871 
872 	switch (shader_type_drm) {
873 	case drm_vmw_shader_type_vs:
874 		shader_type = SVGA3D_SHADERTYPE_VS;
875 		break;
876 	case drm_vmw_shader_type_ps:
877 		shader_type = SVGA3D_SHADERTYPE_PS;
878 		break;
879 	default:
880 		VMW_DEBUG_USER("Illegal shader type.\n");
881 		ret = -EINVAL;
882 		goto out_bad_arg;
883 	}
884 
885 	ret = ttm_read_lock(&dev_priv->reservation_sem, true);
886 	if (unlikely(ret != 0))
887 		goto out_bad_arg;
888 
889 	ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset,
890 				    shader_type, num_input_sig,
891 				    num_output_sig, tfile, shader_handle);
892 
893 	ttm_read_unlock(&dev_priv->reservation_sem);
894 out_bad_arg:
895 	vmw_bo_unreference(&buffer);
896 	return ret;
897 }
898 
899 /**
900  * vmw_shader_id_ok - Check whether a compat shader user key and
901  * shader type are within valid bounds.
902  *
903  * @user_key: User space id of the shader.
904  * @shader_type: Shader type.
905  *
906  * Returns true if valid false if not.
907  */
vmw_shader_id_ok(u32 user_key,SVGA3dShaderType shader_type)908 static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type)
909 {
910 	return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16;
911 }
912 
913 /**
914  * vmw_shader_key - Compute a hash key suitable for a compat shader.
915  *
916  * @user_key: User space id of the shader.
917  * @shader_type: Shader type.
918  *
919  * Returns a hash key suitable for a command buffer managed resource
920  * manager hash table.
921  */
vmw_shader_key(u32 user_key,SVGA3dShaderType shader_type)922 static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type)
923 {
924 	return user_key | (shader_type << 20);
925 }
926 
927 /**
928  * vmw_shader_remove - Stage a compat shader for removal.
929  *
930  * @man: Pointer to the compat shader manager identifying the shader namespace.
931  * @user_key: The key that is used to identify the shader. The key is
932  * unique to the shader type.
933  * @shader_type: Shader type.
934  * @list: Caller's list of staged command buffer resource actions.
935  */
vmw_shader_remove(struct vmw_cmdbuf_res_manager * man,u32 user_key,SVGA3dShaderType shader_type,struct list_head * list)936 int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
937 		      u32 user_key, SVGA3dShaderType shader_type,
938 		      struct list_head *list)
939 {
940 	struct vmw_resource *dummy;
941 
942 	if (!vmw_shader_id_ok(user_key, shader_type))
943 		return -EINVAL;
944 
945 	return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader,
946 				     vmw_shader_key(user_key, shader_type),
947 				     list, &dummy);
948 }
949 
950 /**
951  * vmw_compat_shader_add - Create a compat shader and stage it for addition
952  * as a command buffer managed resource.
953  *
954  * @man: Pointer to the compat shader manager identifying the shader namespace.
955  * @user_key: The key that is used to identify the shader. The key is
956  * unique to the shader type.
957  * @bytecode: Pointer to the bytecode of the shader.
958  * @shader_type: Shader type.
959  * @tfile: Pointer to a struct ttm_object_file that the guest-backed shader is
960  * to be created with.
961  * @list: Caller's list of staged command buffer resource actions.
962  *
963  */
vmw_compat_shader_add(struct vmw_private * dev_priv,struct vmw_cmdbuf_res_manager * man,u32 user_key,const void * bytecode,SVGA3dShaderType shader_type,size_t size,struct list_head * list)964 int vmw_compat_shader_add(struct vmw_private *dev_priv,
965 			  struct vmw_cmdbuf_res_manager *man,
966 			  u32 user_key, const void *bytecode,
967 			  SVGA3dShaderType shader_type,
968 			  size_t size,
969 			  struct list_head *list)
970 {
971 	struct ttm_operation_ctx ctx = { false, true };
972 	struct vmw_buffer_object *buf;
973 	struct ttm_bo_kmap_obj map;
974 	bool is_iomem;
975 	int ret;
976 	struct vmw_resource *res;
977 
978 	if (!vmw_shader_id_ok(user_key, shader_type))
979 		return -EINVAL;
980 
981 	/* Allocate and pin a DMA buffer */
982 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
983 	if (unlikely(!buf))
984 		return -ENOMEM;
985 
986 	ret = vmw_bo_init(dev_priv, buf, size, &vmw_sys_ne_placement,
987 			      true, vmw_bo_bo_free);
988 	if (unlikely(ret != 0))
989 		goto out;
990 
991 	ret = ttm_bo_reserve(&buf->base, false, true, NULL);
992 	if (unlikely(ret != 0))
993 		goto no_reserve;
994 
995 	/* Map and copy shader bytecode. */
996 	ret = ttm_bo_kmap(&buf->base, 0, PAGE_ALIGN(size) >> PAGE_SHIFT,
997 			  &map);
998 	if (unlikely(ret != 0)) {
999 		ttm_bo_unreserve(&buf->base);
1000 		goto no_reserve;
1001 	}
1002 
1003 	memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size);
1004 	WARN_ON(is_iomem);
1005 
1006 	ttm_bo_kunmap(&map);
1007 	ret = ttm_bo_validate(&buf->base, &vmw_sys_placement, &ctx);
1008 	WARN_ON(ret != 0);
1009 	ttm_bo_unreserve(&buf->base);
1010 
1011 	res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type);
1012 	if (unlikely(ret != 0))
1013 		goto no_reserve;
1014 
1015 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
1016 				 vmw_shader_key(user_key, shader_type),
1017 				 res, list);
1018 	vmw_resource_unreference(&res);
1019 no_reserve:
1020 	vmw_bo_unreference(&buf);
1021 out:
1022 	return ret;
1023 }
1024 
1025 /**
1026  * vmw_shader_lookup - Look up a compat shader
1027  *
1028  * @man: Pointer to the command buffer managed resource manager identifying
1029  * the shader namespace.
1030  * @user_key: The user space id of the shader.
1031  * @shader_type: The shader type.
1032  *
1033  * Returns a refcounted pointer to a struct vmw_resource if the shader was
1034  * found. An error pointer otherwise.
1035  */
1036 struct vmw_resource *
vmw_shader_lookup(struct vmw_cmdbuf_res_manager * man,u32 user_key,SVGA3dShaderType shader_type)1037 vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
1038 		  u32 user_key,
1039 		  SVGA3dShaderType shader_type)
1040 {
1041 	if (!vmw_shader_id_ok(user_key, shader_type))
1042 		return ERR_PTR(-EINVAL);
1043 
1044 	return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader,
1045 				     vmw_shader_key(user_key, shader_type));
1046 }
1047 
vmw_shader_define_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1048 int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
1049 			     struct drm_file *file_priv)
1050 {
1051 	struct drm_vmw_shader_create_arg *arg =
1052 		(struct drm_vmw_shader_create_arg *)data;
1053 
1054 	return vmw_shader_define(dev, file_priv, arg->shader_type,
1055 				 arg->buffer_handle,
1056 				 arg->size, arg->offset,
1057 				 0, 0,
1058 				 &arg->shader_handle);
1059 }
1060