1 /**************************************************************************
3 * Copyright © 2009-2012 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include "vmwgfx_resource_priv.h"
30 #include "ttm/ttm_placement.h"
32 struct vmw_user_context {
33 struct ttm_base_object base;
34 struct vmw_resource res;
39 typedef int (*vmw_scrub_func)(struct vmw_ctx_bindinfo *);
41 static void vmw_user_context_free(struct vmw_resource *res);
42 static struct vmw_resource *
43 vmw_user_context_base_to_res(struct ttm_base_object *base);
45 static int vmw_gb_context_create(struct vmw_resource *res);
46 static int vmw_gb_context_bind(struct vmw_resource *res,
47 struct ttm_validate_buffer *val_buf);
48 static int vmw_gb_context_unbind(struct vmw_resource *res,
50 struct ttm_validate_buffer *val_buf);
51 static int vmw_gb_context_destroy(struct vmw_resource *res);
52 static int vmw_context_scrub_shader(struct vmw_ctx_bindinfo *bi);
53 static int vmw_context_scrub_render_target(struct vmw_ctx_bindinfo *bi);
54 static int vmw_context_scrub_texture(struct vmw_ctx_bindinfo *bi);
56 static uint64_t vmw_user_context_size;
58 static const struct vmw_user_resource_conv user_context_conv = {
59 .object_type = VMW_RES_CONTEXT,
60 .base_obj_to_res = vmw_user_context_base_to_res,
61 .res_free = vmw_user_context_free
64 const struct vmw_user_resource_conv *user_context_converter =
68 static const struct vmw_res_func vmw_legacy_context_func = {
69 .res_type = vmw_res_context,
70 .needs_backup = false,
72 .type_name = "legacy contexts",
73 .backup_placement = NULL,
80 static const struct vmw_res_func vmw_gb_context_func = {
81 .res_type = vmw_res_context,
84 .type_name = "guest backed contexts",
85 .backup_placement = &vmw_mob_placement,
86 .create = vmw_gb_context_create,
87 .destroy = vmw_gb_context_destroy,
88 .bind = vmw_gb_context_bind,
89 .unbind = vmw_gb_context_unbind
92 static const vmw_scrub_func vmw_scrub_funcs[vmw_ctx_binding_max] = {
93 [vmw_ctx_binding_shader] = vmw_context_scrub_shader,
94 [vmw_ctx_binding_rt] = vmw_context_scrub_render_target,
95 [vmw_ctx_binding_tex] = vmw_context_scrub_texture };
101 static void vmw_hw_context_destroy(struct vmw_resource *res)
104 struct vmw_private *dev_priv = res->dev_priv;
106 SVGA3dCmdHeader header;
107 SVGA3dCmdDestroyContext body;
111 if (res->func->destroy == vmw_gb_context_destroy) {
112 mutex_lock(&dev_priv->cmdbuf_mutex);
113 (void) vmw_gb_context_destroy(res);
114 if (dev_priv->pinned_bo != NULL &&
115 !dev_priv->query_cid_valid)
116 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
117 mutex_unlock(&dev_priv->cmdbuf_mutex);
121 vmw_execbuf_release_pinned_bo(dev_priv);
122 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
123 if (unlikely(cmd == NULL)) {
124 DRM_ERROR("Failed reserving FIFO space for surface "
129 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DESTROY);
130 cmd->header.size = cpu_to_le32(sizeof(cmd->body));
131 cmd->body.cid = cpu_to_le32(res->id);
133 vmw_fifo_commit(dev_priv, sizeof(*cmd));
134 vmw_3d_resource_dec(dev_priv, false);
137 static int vmw_gb_context_init(struct vmw_private *dev_priv,
138 struct vmw_resource *res,
139 void (*res_free) (struct vmw_resource *res))
143 ret = vmw_resource_init(dev_priv, res, true,
144 res_free, &vmw_gb_context_func);
145 res->backup_size = SVGA3D_CONTEXT_DATA_SIZE;
147 if (unlikely(ret != 0)) {
155 vmw_resource_activate(res, vmw_hw_context_destroy);
159 static int vmw_context_init(struct vmw_private *dev_priv,
160 struct vmw_resource *res,
161 void (*res_free) (struct vmw_resource *res))
166 SVGA3dCmdHeader header;
167 SVGA3dCmdDefineContext body;
170 if (dev_priv->has_mob)
171 return vmw_gb_context_init(dev_priv, res, res_free);
173 ret = vmw_resource_init(dev_priv, res, false,
174 res_free, &vmw_legacy_context_func);
176 if (unlikely(ret != 0)) {
177 DRM_ERROR("Failed to allocate a resource id.\n");
181 if (unlikely(res->id >= SVGA3D_MAX_CONTEXT_IDS)) {
182 DRM_ERROR("Out of hw context ids.\n");
183 vmw_resource_unreference(&res);
187 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
188 if (unlikely(cmd == NULL)) {
189 DRM_ERROR("Fifo reserve failed.\n");
190 vmw_resource_unreference(&res);
194 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DEFINE);
195 cmd->header.size = cpu_to_le32(sizeof(cmd->body));
196 cmd->body.cid = cpu_to_le32(res->id);
198 vmw_fifo_commit(dev_priv, sizeof(*cmd));
199 (void) vmw_3d_resource_inc(dev_priv, false);
200 vmw_resource_activate(res, vmw_hw_context_destroy);
204 if (res_free == NULL)
211 struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv)
213 struct vmw_resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
216 if (unlikely(res == NULL))
219 ret = vmw_context_init(dev_priv, res, NULL);
221 return (ret == 0) ? res : NULL;
225 static int vmw_gb_context_create(struct vmw_resource *res)
227 struct vmw_private *dev_priv = res->dev_priv;
230 SVGA3dCmdHeader header;
231 SVGA3dCmdDefineGBContext body;
234 if (likely(res->id != -1))
237 ret = vmw_resource_alloc_id(res);
238 if (unlikely(ret != 0)) {
239 DRM_ERROR("Failed to allocate a context id.\n");
243 if (unlikely(res->id >= VMWGFX_NUM_GB_CONTEXT)) {
248 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
249 if (unlikely(cmd == NULL)) {
250 DRM_ERROR("Failed reserving FIFO space for context "
256 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_CONTEXT;
257 cmd->header.size = sizeof(cmd->body);
258 cmd->body.cid = res->id;
259 vmw_fifo_commit(dev_priv, sizeof(*cmd));
260 (void) vmw_3d_resource_inc(dev_priv, false);
265 vmw_resource_release_id(res);
270 static int vmw_gb_context_bind(struct vmw_resource *res,
271 struct ttm_validate_buffer *val_buf)
273 struct vmw_private *dev_priv = res->dev_priv;
275 SVGA3dCmdHeader header;
276 SVGA3dCmdBindGBContext body;
278 struct ttm_buffer_object *bo = val_buf->bo;
280 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
282 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
283 if (unlikely(cmd == NULL)) {
284 DRM_ERROR("Failed reserving FIFO space for context "
289 cmd->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
290 cmd->header.size = sizeof(cmd->body);
291 cmd->body.cid = res->id;
292 cmd->body.mobid = bo->mem.start;
293 cmd->body.validContents = res->backup_dirty;
294 res->backup_dirty = false;
295 vmw_fifo_commit(dev_priv, sizeof(*cmd));
300 static int vmw_gb_context_unbind(struct vmw_resource *res,
302 struct ttm_validate_buffer *val_buf)
304 struct vmw_private *dev_priv = res->dev_priv;
305 struct ttm_buffer_object *bo = val_buf->bo;
306 struct vmw_fence_obj *fence;
309 SVGA3dCmdHeader header;
310 SVGA3dCmdReadbackGBContext body;
313 SVGA3dCmdHeader header;
314 SVGA3dCmdBindGBContext body;
316 uint32_t submit_size;
320 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
322 submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
324 cmd = vmw_fifo_reserve(dev_priv, submit_size);
325 if (unlikely(cmd == NULL)) {
326 DRM_ERROR("Failed reserving FIFO space for context "
334 cmd1->header.id = SVGA_3D_CMD_READBACK_GB_CONTEXT;
335 cmd1->header.size = sizeof(cmd1->body);
336 cmd1->body.cid = res->id;
337 cmd2 = (void *) (&cmd1[1]);
339 cmd2->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
340 cmd2->header.size = sizeof(cmd2->body);
341 cmd2->body.cid = res->id;
342 cmd2->body.mobid = SVGA3D_INVALID_ID;
344 vmw_fifo_commit(dev_priv, submit_size);
347 * Create a fence object and fence the backup buffer.
350 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
353 vmw_fence_single_bo(bo, fence);
355 if (likely(fence != NULL))
356 vmw_fence_obj_unreference(&fence);
361 static int vmw_gb_context_destroy(struct vmw_resource *res)
363 struct vmw_private *dev_priv = res->dev_priv;
365 SVGA3dCmdHeader header;
366 SVGA3dCmdDestroyGBContext body;
369 if (likely(res->id == -1))
372 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
373 if (unlikely(cmd == NULL)) {
374 DRM_ERROR("Failed reserving FIFO space for context "
379 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_CONTEXT;
380 cmd->header.size = sizeof(cmd->body);
381 cmd->body.cid = res->id;
382 vmw_fifo_commit(dev_priv, sizeof(*cmd));
383 if (dev_priv->query_cid == res->id)
384 dev_priv->query_cid_valid = false;
385 vmw_resource_release_id(res);
386 vmw_3d_resource_dec(dev_priv, false);
392 * User-space context management:
395 static struct vmw_resource *
396 vmw_user_context_base_to_res(struct ttm_base_object *base)
398 return &(container_of(base, struct vmw_user_context, base)->res);
401 static void vmw_user_context_free(struct vmw_resource *res)
403 struct vmw_user_context *ctx =
404 container_of(res, struct vmw_user_context, res);
405 struct vmw_private *dev_priv = res->dev_priv;
407 ttm_base_object_kfree(ctx, base);
408 ttm_mem_global_free(vmw_mem_glob(dev_priv),
409 vmw_user_context_size);
413 * This function is called when user space has no more references on the
414 * base object. It releases the base-object's reference on the resource object.
417 static void vmw_user_context_base_release(struct ttm_base_object **p_base)
419 struct ttm_base_object *base = *p_base;
420 struct vmw_user_context *ctx =
421 container_of(base, struct vmw_user_context, base);
422 struct vmw_resource *res = &ctx->res;
425 vmw_resource_unreference(&res);
428 int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
429 struct drm_file *file_priv)
431 struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
432 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
434 return ttm_ref_object_base_unref(tfile, arg->cid, TTM_REF_USAGE);
437 int vmw_context_define_ioctl(struct drm_device *dev, void *data,
438 struct drm_file *file_priv)
440 struct vmw_private *dev_priv = vmw_priv(dev);
441 struct vmw_user_context *ctx;
442 struct vmw_resource *res;
443 struct vmw_resource *tmp;
444 struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
445 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
446 struct vmw_master *vmaster = vmw_master(file_priv->master);
451 * Approximate idr memory usage with 128 bytes. It will be limited
452 * by maximum number_of contexts anyway.
455 if (unlikely(vmw_user_context_size == 0))
456 vmw_user_context_size = ttm_round_pot(sizeof(*ctx)) + 128;
458 ret = ttm_read_lock(&vmaster->lock, true);
459 if (unlikely(ret != 0))
462 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
463 vmw_user_context_size,
465 if (unlikely(ret != 0)) {
466 if (ret != -ERESTARTSYS)
467 DRM_ERROR("Out of graphics memory for context"
472 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
473 if (unlikely(ctx == NULL)) {
474 ttm_mem_global_free(vmw_mem_glob(dev_priv),
475 vmw_user_context_size);
481 ctx->base.shareable = false;
482 ctx->base.tfile = NULL;
485 * From here on, the destructor takes over resource freeing.
488 ret = vmw_context_init(dev_priv, res, vmw_user_context_free);
489 if (unlikely(ret != 0))
492 tmp = vmw_resource_reference(&ctx->res);
493 ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
494 &vmw_user_context_base_release, NULL);
496 if (unlikely(ret != 0)) {
497 vmw_resource_unreference(&tmp);
501 arg->cid = ctx->base.hash.key;
503 vmw_resource_unreference(&res);
505 ttm_read_unlock(&vmaster->lock);
511 * vmw_context_scrub_shader - scrub a shader binding from a context.
513 * @bi: single binding information.
515 static int vmw_context_scrub_shader(struct vmw_ctx_bindinfo *bi)
517 struct vmw_private *dev_priv = bi->ctx->dev_priv;
519 SVGA3dCmdHeader header;
520 SVGA3dCmdSetShader body;
523 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
524 if (unlikely(cmd == NULL)) {
525 DRM_ERROR("Failed reserving FIFO space for shader "
530 cmd->header.id = SVGA_3D_CMD_SET_SHADER;
531 cmd->header.size = sizeof(cmd->body);
532 cmd->body.cid = bi->ctx->id;
533 cmd->body.type = bi->i1.shader_type;
534 cmd->body.shid = SVGA3D_INVALID_ID;
535 vmw_fifo_commit(dev_priv, sizeof(*cmd));
541 * vmw_context_scrub_render_target - scrub a render target binding
544 * @bi: single binding information.
546 static int vmw_context_scrub_render_target(struct vmw_ctx_bindinfo *bi)
548 struct vmw_private *dev_priv = bi->ctx->dev_priv;
550 SVGA3dCmdHeader header;
551 SVGA3dCmdSetRenderTarget body;
554 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
555 if (unlikely(cmd == NULL)) {
556 DRM_ERROR("Failed reserving FIFO space for render target "
561 cmd->header.id = SVGA_3D_CMD_SETRENDERTARGET;
562 cmd->header.size = sizeof(cmd->body);
563 cmd->body.cid = bi->ctx->id;
564 cmd->body.type = bi->i1.rt_type;
565 cmd->body.target.sid = SVGA3D_INVALID_ID;
566 cmd->body.target.face = 0;
567 cmd->body.target.mipmap = 0;
568 vmw_fifo_commit(dev_priv, sizeof(*cmd));
574 * vmw_context_scrub_texture - scrub a texture binding from a context.
576 * @bi: single binding information.
578 * TODO: Possibly complement this function with a function that takes
579 * a list of texture bindings and combines them to a single command.
581 static int vmw_context_scrub_texture(struct vmw_ctx_bindinfo *bi)
583 struct vmw_private *dev_priv = bi->ctx->dev_priv;
585 SVGA3dCmdHeader header;
587 SVGA3dCmdSetTextureState c;
588 SVGA3dTextureState s1;
592 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
593 if (unlikely(cmd == NULL)) {
594 DRM_ERROR("Failed reserving FIFO space for texture "
600 cmd->header.id = SVGA_3D_CMD_SETTEXTURESTATE;
601 cmd->header.size = sizeof(cmd->body);
602 cmd->body.c.cid = bi->ctx->id;
603 cmd->body.s1.stage = bi->i1.texture_stage;
604 cmd->body.s1.name = SVGA3D_TS_BIND_TEXTURE;
605 cmd->body.s1.value = (uint32) SVGA3D_INVALID_ID;
606 vmw_fifo_commit(dev_priv, sizeof(*cmd));
612 * vmw_context_binding_drop: Stop tracking a context binding
614 * @cb: Pointer to binding tracker storage.
616 * Stops tracking a context binding, and re-initializes its storage.
617 * Typically used when the context binding is replaced with a binding to
618 * another (or the same, for that matter) resource.
620 static void vmw_context_binding_drop(struct vmw_ctx_binding *cb)
622 list_del(&cb->ctx_list);
627 * vmw_context_binding_add: Start tracking a context binding
629 * @cbs: Pointer to the context binding state tracker.
630 * @bi: Information about the binding to track.
632 * Performs basic checks on the binding to make sure arguments are within
633 * bounds and then starts tracking the binding in the context binding
634 * state structure @cbs.
636 int vmw_context_binding_add(struct vmw_ctx_binding_state *cbs,
637 const struct vmw_ctx_bindinfo *bi)
639 struct vmw_ctx_binding *loc;
642 case vmw_ctx_binding_rt:
643 if (unlikely((unsigned)bi->i1.rt_type >= SVGA3D_RT_MAX)) {
644 DRM_ERROR("Illegal render target type %u.\n",
645 (unsigned) bi->i1.rt_type);
648 loc = &cbs->render_targets[bi->i1.rt_type];
650 case vmw_ctx_binding_tex:
651 if (unlikely((unsigned)bi->i1.texture_stage >=
652 SVGA3D_NUM_TEXTURE_UNITS)) {
653 DRM_ERROR("Illegal texture/sampler unit %u.\n",
654 (unsigned) bi->i1.texture_stage);
657 loc = &cbs->texture_units[bi->i1.texture_stage];
659 case vmw_ctx_binding_shader:
660 if (unlikely((unsigned)bi->i1.shader_type >=
661 SVGA3D_SHADERTYPE_MAX)) {
662 DRM_ERROR("Illegal shader type %u.\n",
663 (unsigned) bi->i1.shader_type);
666 loc = &cbs->shaders[bi->i1.shader_type];
672 if (loc->bi.ctx != NULL)
673 vmw_context_binding_drop(loc);
676 list_add_tail(&loc->ctx_list, &cbs->list);
682 * vmw_context_binding_kill - Kill a binding on the device
683 * and stop tracking it.
685 * @cb: Pointer to binding tracker storage.
687 * Emits FIFO commands to scrub a binding represented by @cb.
688 * Then stops tracking the binding and re-initializes its storage.
690 void vmw_context_binding_kill(struct vmw_ctx_binding *cb)
692 (void) vmw_scrub_funcs[cb->bi.bt](&cb->bi);
693 vmw_context_binding_drop(cb);
697 * vmw_context_binding_state_kill - Kill all bindings associated with a
698 * struct vmw_ctx_binding state structure, and re-initialize the structure.
700 * @cbs: Pointer to the context binding state tracker.
702 * Emits commands to scrub all bindings associated with the
703 * context binding state tracker. Then re-initializes the whole structure.
705 void vmw_context_binding_state_kill(struct vmw_ctx_binding_state *cbs)
707 struct vmw_ctx_binding *entry, *next;
709 list_for_each_entry_safe(entry, next, &cbs->list, ctx_list) {
710 vmw_context_binding_kill(entry);