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