1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2009-2015 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 <drm/ttm/ttm_placement.h>
30 #include "vmwgfx_drv.h"
31 #include "vmwgfx_resource_priv.h"
32 #include "vmwgfx_binding.h"
34 struct vmw_user_context {
35 struct ttm_base_object base;
36 struct vmw_resource res;
37 struct vmw_ctx_binding_state *cbs;
38 struct vmw_cmdbuf_res_manager *man;
39 struct vmw_resource *cotables[SVGA_COTABLE_MAX];
40 spinlock_t cotable_lock;
41 struct vmw_buffer_object *dx_query_mob;
44 static void vmw_user_context_free(struct vmw_resource *res);
45 static struct vmw_resource *
46 vmw_user_context_base_to_res(struct ttm_base_object *base);
48 static int vmw_gb_context_create(struct vmw_resource *res);
49 static int vmw_gb_context_bind(struct vmw_resource *res,
50 struct ttm_validate_buffer *val_buf);
51 static int vmw_gb_context_unbind(struct vmw_resource *res,
53 struct ttm_validate_buffer *val_buf);
54 static int vmw_gb_context_destroy(struct vmw_resource *res);
55 static int vmw_dx_context_create(struct vmw_resource *res);
56 static int vmw_dx_context_bind(struct vmw_resource *res,
57 struct ttm_validate_buffer *val_buf);
58 static int vmw_dx_context_unbind(struct vmw_resource *res,
60 struct ttm_validate_buffer *val_buf);
61 static int vmw_dx_context_destroy(struct vmw_resource *res);
63 static const struct vmw_user_resource_conv user_context_conv = {
64 .object_type = VMW_RES_CONTEXT,
65 .base_obj_to_res = vmw_user_context_base_to_res,
66 .res_free = vmw_user_context_free
69 const struct vmw_user_resource_conv *user_context_converter =
73 static const struct vmw_res_func vmw_legacy_context_func = {
74 .res_type = vmw_res_context,
75 .needs_backup = false,
77 .type_name = "legacy contexts",
78 .backup_placement = NULL,
85 static const struct vmw_res_func vmw_gb_context_func = {
86 .res_type = vmw_res_context,
91 .type_name = "guest backed contexts",
92 .backup_placement = &vmw_mob_placement,
93 .create = vmw_gb_context_create,
94 .destroy = vmw_gb_context_destroy,
95 .bind = vmw_gb_context_bind,
96 .unbind = vmw_gb_context_unbind
99 static const struct vmw_res_func vmw_dx_context_func = {
100 .res_type = vmw_res_dx_context,
101 .needs_backup = true,
105 .type_name = "dx contexts",
106 .backup_placement = &vmw_mob_placement,
107 .create = vmw_dx_context_create,
108 .destroy = vmw_dx_context_destroy,
109 .bind = vmw_dx_context_bind,
110 .unbind = vmw_dx_context_unbind
114 * Context management:
117 static void vmw_context_cotables_unref(struct vmw_private *dev_priv,
118 struct vmw_user_context *uctx)
120 struct vmw_resource *res;
122 u32 cotable_max = has_sm5_context(dev_priv) ?
123 SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
125 for (i = 0; i < cotable_max; ++i) {
126 spin_lock(&uctx->cotable_lock);
127 res = uctx->cotables[i];
128 uctx->cotables[i] = NULL;
129 spin_unlock(&uctx->cotable_lock);
132 vmw_resource_unreference(&res);
136 static void vmw_hw_context_destroy(struct vmw_resource *res)
138 struct vmw_user_context *uctx =
139 container_of(res, struct vmw_user_context, res);
140 struct vmw_private *dev_priv = res->dev_priv;
142 SVGA3dCmdHeader header;
143 SVGA3dCmdDestroyContext body;
147 if (res->func->destroy == vmw_gb_context_destroy ||
148 res->func->destroy == vmw_dx_context_destroy) {
149 mutex_lock(&dev_priv->cmdbuf_mutex);
150 vmw_cmdbuf_res_man_destroy(uctx->man);
151 mutex_lock(&dev_priv->binding_mutex);
152 vmw_binding_state_kill(uctx->cbs);
153 (void) res->func->destroy(res);
154 mutex_unlock(&dev_priv->binding_mutex);
155 if (dev_priv->pinned_bo != NULL &&
156 !dev_priv->query_cid_valid)
157 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
158 mutex_unlock(&dev_priv->cmdbuf_mutex);
159 vmw_context_cotables_unref(dev_priv, uctx);
163 vmw_execbuf_release_pinned_bo(dev_priv);
164 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
165 if (unlikely(cmd == NULL))
168 cmd->header.id = SVGA_3D_CMD_CONTEXT_DESTROY;
169 cmd->header.size = sizeof(cmd->body);
170 cmd->body.cid = res->id;
172 vmw_cmd_commit(dev_priv, sizeof(*cmd));
173 vmw_fifo_resource_dec(dev_priv);
176 static int vmw_gb_context_init(struct vmw_private *dev_priv,
178 struct vmw_resource *res,
179 void (*res_free)(struct vmw_resource *res))
182 struct vmw_user_context *uctx =
183 container_of(res, struct vmw_user_context, res);
185 res->backup_size = (dx ? sizeof(SVGADXContextMobFormat) :
186 sizeof(SVGAGBContextData));
187 ret = vmw_resource_init(dev_priv, res, true,
189 dx ? &vmw_dx_context_func :
190 &vmw_gb_context_func);
191 if (unlikely(ret != 0))
194 if (dev_priv->has_mob) {
195 uctx->man = vmw_cmdbuf_res_man_create(dev_priv);
196 if (IS_ERR(uctx->man)) {
197 ret = PTR_ERR(uctx->man);
203 uctx->cbs = vmw_binding_state_alloc(dev_priv);
204 if (IS_ERR(uctx->cbs)) {
205 ret = PTR_ERR(uctx->cbs);
209 spin_lock_init(&uctx->cotable_lock);
212 u32 cotable_max = has_sm5_context(dev_priv) ?
213 SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
214 for (i = 0; i < cotable_max; ++i) {
215 uctx->cotables[i] = vmw_cotable_alloc(dev_priv,
217 if (IS_ERR(uctx->cotables[i])) {
218 ret = PTR_ERR(uctx->cotables[i]);
224 res->hw_destroy = vmw_hw_context_destroy;
228 vmw_context_cotables_unref(dev_priv, uctx);
237 static int vmw_context_init(struct vmw_private *dev_priv,
238 struct vmw_resource *res,
239 void (*res_free)(struct vmw_resource *res),
245 SVGA3dCmdHeader header;
246 SVGA3dCmdDefineContext body;
249 if (dev_priv->has_mob)
250 return vmw_gb_context_init(dev_priv, dx, res, res_free);
252 ret = vmw_resource_init(dev_priv, res, false,
253 res_free, &vmw_legacy_context_func);
255 if (unlikely(ret != 0)) {
256 DRM_ERROR("Failed to allocate a resource id.\n");
260 if (unlikely(res->id >= SVGA3D_HB_MAX_CONTEXT_IDS)) {
261 DRM_ERROR("Out of hw context ids.\n");
262 vmw_resource_unreference(&res);
266 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
267 if (unlikely(cmd == NULL)) {
268 vmw_resource_unreference(&res);
272 cmd->header.id = SVGA_3D_CMD_CONTEXT_DEFINE;
273 cmd->header.size = sizeof(cmd->body);
274 cmd->body.cid = res->id;
276 vmw_cmd_commit(dev_priv, sizeof(*cmd));
277 vmw_fifo_resource_inc(dev_priv);
278 res->hw_destroy = vmw_hw_context_destroy;
282 if (res_free == NULL)
294 static int vmw_gb_context_create(struct vmw_resource *res)
296 struct vmw_private *dev_priv = res->dev_priv;
299 SVGA3dCmdHeader header;
300 SVGA3dCmdDefineGBContext body;
303 if (likely(res->id != -1))
306 ret = vmw_resource_alloc_id(res);
307 if (unlikely(ret != 0)) {
308 DRM_ERROR("Failed to allocate a context id.\n");
312 if (unlikely(res->id >= VMWGFX_NUM_GB_CONTEXT)) {
317 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
318 if (unlikely(cmd == NULL)) {
323 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_CONTEXT;
324 cmd->header.size = sizeof(cmd->body);
325 cmd->body.cid = res->id;
326 vmw_cmd_commit(dev_priv, sizeof(*cmd));
327 vmw_fifo_resource_inc(dev_priv);
332 vmw_resource_release_id(res);
337 static int vmw_gb_context_bind(struct vmw_resource *res,
338 struct ttm_validate_buffer *val_buf)
340 struct vmw_private *dev_priv = res->dev_priv;
342 SVGA3dCmdHeader header;
343 SVGA3dCmdBindGBContext body;
345 struct ttm_buffer_object *bo = val_buf->bo;
347 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
349 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
350 if (unlikely(cmd == NULL))
353 cmd->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
354 cmd->header.size = sizeof(cmd->body);
355 cmd->body.cid = res->id;
356 cmd->body.mobid = bo->resource->start;
357 cmd->body.validContents = res->backup_dirty;
358 res->backup_dirty = false;
359 vmw_cmd_commit(dev_priv, sizeof(*cmd));
364 static int vmw_gb_context_unbind(struct vmw_resource *res,
366 struct ttm_validate_buffer *val_buf)
368 struct vmw_private *dev_priv = res->dev_priv;
369 struct ttm_buffer_object *bo = val_buf->bo;
370 struct vmw_fence_obj *fence;
371 struct vmw_user_context *uctx =
372 container_of(res, struct vmw_user_context, res);
375 SVGA3dCmdHeader header;
376 SVGA3dCmdReadbackGBContext body;
379 SVGA3dCmdHeader header;
380 SVGA3dCmdBindGBContext body;
382 uint32_t submit_size;
386 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
388 mutex_lock(&dev_priv->binding_mutex);
389 vmw_binding_state_scrub(uctx->cbs);
391 submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
393 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
394 if (unlikely(cmd == NULL)) {
395 mutex_unlock(&dev_priv->binding_mutex);
402 cmd1->header.id = SVGA_3D_CMD_READBACK_GB_CONTEXT;
403 cmd1->header.size = sizeof(cmd1->body);
404 cmd1->body.cid = res->id;
405 cmd2 = (void *) (&cmd1[1]);
407 cmd2->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
408 cmd2->header.size = sizeof(cmd2->body);
409 cmd2->body.cid = res->id;
410 cmd2->body.mobid = SVGA3D_INVALID_ID;
412 vmw_cmd_commit(dev_priv, submit_size);
413 mutex_unlock(&dev_priv->binding_mutex);
416 * Create a fence object and fence the backup buffer.
419 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
422 vmw_bo_fence_single(bo, fence);
424 if (likely(fence != NULL))
425 vmw_fence_obj_unreference(&fence);
430 static int vmw_gb_context_destroy(struct vmw_resource *res)
432 struct vmw_private *dev_priv = res->dev_priv;
434 SVGA3dCmdHeader header;
435 SVGA3dCmdDestroyGBContext body;
438 if (likely(res->id == -1))
441 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
442 if (unlikely(cmd == NULL))
445 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_CONTEXT;
446 cmd->header.size = sizeof(cmd->body);
447 cmd->body.cid = res->id;
448 vmw_cmd_commit(dev_priv, sizeof(*cmd));
449 if (dev_priv->query_cid == res->id)
450 dev_priv->query_cid_valid = false;
451 vmw_resource_release_id(res);
452 vmw_fifo_resource_dec(dev_priv);
461 static int vmw_dx_context_create(struct vmw_resource *res)
463 struct vmw_private *dev_priv = res->dev_priv;
466 SVGA3dCmdHeader header;
467 SVGA3dCmdDXDefineContext body;
470 if (likely(res->id != -1))
473 ret = vmw_resource_alloc_id(res);
474 if (unlikely(ret != 0)) {
475 DRM_ERROR("Failed to allocate a context id.\n");
479 if (unlikely(res->id >= VMWGFX_NUM_DXCONTEXT)) {
484 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
485 if (unlikely(cmd == NULL)) {
490 cmd->header.id = SVGA_3D_CMD_DX_DEFINE_CONTEXT;
491 cmd->header.size = sizeof(cmd->body);
492 cmd->body.cid = res->id;
493 vmw_cmd_commit(dev_priv, sizeof(*cmd));
494 vmw_fifo_resource_inc(dev_priv);
499 vmw_resource_release_id(res);
504 static int vmw_dx_context_bind(struct vmw_resource *res,
505 struct ttm_validate_buffer *val_buf)
507 struct vmw_private *dev_priv = res->dev_priv;
509 SVGA3dCmdHeader header;
510 SVGA3dCmdDXBindContext body;
512 struct ttm_buffer_object *bo = val_buf->bo;
514 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
516 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
517 if (unlikely(cmd == NULL))
520 cmd->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT;
521 cmd->header.size = sizeof(cmd->body);
522 cmd->body.cid = res->id;
523 cmd->body.mobid = bo->resource->start;
524 cmd->body.validContents = res->backup_dirty;
525 res->backup_dirty = false;
526 vmw_cmd_commit(dev_priv, sizeof(*cmd));
533 * vmw_dx_context_scrub_cotables - Scrub all bindings and
534 * cotables from a context
536 * @ctx: Pointer to the context resource
537 * @readback: Whether to save the otable contents on scrubbing.
539 * COtables must be unbound before their context, but unbinding requires
540 * the backup buffer being reserved, whereas scrubbing does not.
541 * This function scrubs all cotables of a context, potentially reading back
542 * the contents into their backup buffers. However, scrubbing cotables
543 * also makes the device context invalid, so scrub all bindings first so
544 * that doesn't have to be done later with an invalid context.
546 void vmw_dx_context_scrub_cotables(struct vmw_resource *ctx,
549 struct vmw_user_context *uctx =
550 container_of(ctx, struct vmw_user_context, res);
551 u32 cotable_max = has_sm5_context(ctx->dev_priv) ?
552 SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
555 vmw_binding_state_scrub(uctx->cbs);
556 for (i = 0; i < cotable_max; ++i) {
557 struct vmw_resource *res;
559 /* Avoid racing with ongoing cotable destruction. */
560 spin_lock(&uctx->cotable_lock);
561 res = uctx->cotables[vmw_cotable_scrub_order[i]];
563 res = vmw_resource_reference_unless_doomed(res);
564 spin_unlock(&uctx->cotable_lock);
568 WARN_ON(vmw_cotable_scrub(res, readback));
569 vmw_resource_unreference(&res);
573 static int vmw_dx_context_unbind(struct vmw_resource *res,
575 struct ttm_validate_buffer *val_buf)
577 struct vmw_private *dev_priv = res->dev_priv;
578 struct ttm_buffer_object *bo = val_buf->bo;
579 struct vmw_fence_obj *fence;
580 struct vmw_user_context *uctx =
581 container_of(res, struct vmw_user_context, res);
584 SVGA3dCmdHeader header;
585 SVGA3dCmdDXReadbackContext body;
588 SVGA3dCmdHeader header;
589 SVGA3dCmdDXBindContext body;
591 uint32_t submit_size;
595 BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
597 mutex_lock(&dev_priv->binding_mutex);
598 vmw_dx_context_scrub_cotables(res, readback);
600 if (uctx->dx_query_mob && uctx->dx_query_mob->dx_query_ctx &&
602 WARN_ON(uctx->dx_query_mob->dx_query_ctx != res);
603 if (vmw_query_readback_all(uctx->dx_query_mob))
604 DRM_ERROR("Failed to read back query states\n");
607 submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
609 cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
610 if (unlikely(cmd == NULL)) {
611 mutex_unlock(&dev_priv->binding_mutex);
618 cmd1->header.id = SVGA_3D_CMD_DX_READBACK_CONTEXT;
619 cmd1->header.size = sizeof(cmd1->body);
620 cmd1->body.cid = res->id;
621 cmd2 = (void *) (&cmd1[1]);
623 cmd2->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT;
624 cmd2->header.size = sizeof(cmd2->body);
625 cmd2->body.cid = res->id;
626 cmd2->body.mobid = SVGA3D_INVALID_ID;
628 vmw_cmd_commit(dev_priv, submit_size);
629 mutex_unlock(&dev_priv->binding_mutex);
632 * Create a fence object and fence the backup buffer.
635 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
638 vmw_bo_fence_single(bo, fence);
640 if (likely(fence != NULL))
641 vmw_fence_obj_unreference(&fence);
646 static int vmw_dx_context_destroy(struct vmw_resource *res)
648 struct vmw_private *dev_priv = res->dev_priv;
650 SVGA3dCmdHeader header;
651 SVGA3dCmdDXDestroyContext body;
654 if (likely(res->id == -1))
657 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
658 if (unlikely(cmd == NULL))
661 cmd->header.id = SVGA_3D_CMD_DX_DESTROY_CONTEXT;
662 cmd->header.size = sizeof(cmd->body);
663 cmd->body.cid = res->id;
664 vmw_cmd_commit(dev_priv, sizeof(*cmd));
665 if (dev_priv->query_cid == res->id)
666 dev_priv->query_cid_valid = false;
667 vmw_resource_release_id(res);
668 vmw_fifo_resource_dec(dev_priv);
674 * User-space context management:
677 static struct vmw_resource *
678 vmw_user_context_base_to_res(struct ttm_base_object *base)
680 return &(container_of(base, struct vmw_user_context, base)->res);
683 static void vmw_user_context_free(struct vmw_resource *res)
685 struct vmw_user_context *ctx =
686 container_of(res, struct vmw_user_context, res);
689 vmw_binding_state_free(ctx->cbs);
691 (void) vmw_context_bind_dx_query(res, NULL);
693 ttm_base_object_kfree(ctx, base);
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.
701 static void vmw_user_context_base_release(struct ttm_base_object **p_base)
703 struct ttm_base_object *base = *p_base;
704 struct vmw_user_context *ctx =
705 container_of(base, struct vmw_user_context, base);
706 struct vmw_resource *res = &ctx->res;
709 vmw_resource_unreference(&res);
712 int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
713 struct drm_file *file_priv)
715 struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
716 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
718 return ttm_ref_object_base_unref(tfile, arg->cid);
721 static int vmw_context_define(struct drm_device *dev, void *data,
722 struct drm_file *file_priv, bool dx)
724 struct vmw_private *dev_priv = vmw_priv(dev);
725 struct vmw_user_context *ctx;
726 struct vmw_resource *res;
727 struct vmw_resource *tmp;
728 struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
729 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
732 if (!has_sm4_context(dev_priv) && dx) {
733 VMW_DEBUG_USER("DX contexts not supported by device.\n");
737 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
738 if (unlikely(!ctx)) {
744 ctx->base.shareable = false;
745 ctx->base.tfile = NULL;
748 * From here on, the destructor takes over resource freeing.
751 ret = vmw_context_init(dev_priv, res, vmw_user_context_free, dx);
752 if (unlikely(ret != 0))
755 tmp = vmw_resource_reference(&ctx->res);
756 ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
757 &vmw_user_context_base_release);
759 if (unlikely(ret != 0)) {
760 vmw_resource_unreference(&tmp);
764 arg->cid = ctx->base.handle;
766 vmw_resource_unreference(&res);
771 int vmw_context_define_ioctl(struct drm_device *dev, void *data,
772 struct drm_file *file_priv)
774 return vmw_context_define(dev, data, file_priv, false);
777 int vmw_extended_context_define_ioctl(struct drm_device *dev, void *data,
778 struct drm_file *file_priv)
780 union drm_vmw_extended_context_arg *arg = (typeof(arg)) data;
781 struct drm_vmw_context_arg *rep = &arg->rep;
784 case drm_vmw_context_legacy:
785 return vmw_context_define(dev, rep, file_priv, false);
786 case drm_vmw_context_dx:
787 return vmw_context_define(dev, rep, file_priv, true);
795 * vmw_context_binding_list - Return a list of context bindings
797 * @ctx: The context resource
799 * Returns the current list of bindings of the given context. Note that
800 * this list becomes stale as soon as the dev_priv::binding_mutex is unlocked.
802 struct list_head *vmw_context_binding_list(struct vmw_resource *ctx)
804 struct vmw_user_context *uctx =
805 container_of(ctx, struct vmw_user_context, res);
807 return vmw_binding_state_list(uctx->cbs);
810 struct vmw_cmdbuf_res_manager *vmw_context_res_man(struct vmw_resource *ctx)
812 return container_of(ctx, struct vmw_user_context, res)->man;
815 struct vmw_resource *vmw_context_cotable(struct vmw_resource *ctx,
816 SVGACOTableType cotable_type)
818 u32 cotable_max = has_sm5_context(ctx->dev_priv) ?
819 SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
821 if (cotable_type >= cotable_max)
822 return ERR_PTR(-EINVAL);
824 return container_of(ctx, struct vmw_user_context, res)->
825 cotables[cotable_type];
829 * vmw_context_binding_state -
830 * Return a pointer to a context binding state structure
832 * @ctx: The context resource
834 * Returns the current state of bindings of the given context. Note that
835 * this state becomes stale as soon as the dev_priv::binding_mutex is unlocked.
837 struct vmw_ctx_binding_state *
838 vmw_context_binding_state(struct vmw_resource *ctx)
840 return container_of(ctx, struct vmw_user_context, res)->cbs;
844 * vmw_context_bind_dx_query -
845 * Sets query MOB for the context. If @mob is NULL, then this function will
846 * remove the association between the MOB and the context. This function
847 * assumes the binding_mutex is held.
849 * @ctx_res: The context resource
850 * @mob: a reference to the query MOB
852 * Returns -EINVAL if a MOB has already been set and does not match the one
853 * specified in the parameter. 0 otherwise.
855 int vmw_context_bind_dx_query(struct vmw_resource *ctx_res,
856 struct vmw_buffer_object *mob)
858 struct vmw_user_context *uctx =
859 container_of(ctx_res, struct vmw_user_context, res);
862 if (uctx->dx_query_mob) {
863 uctx->dx_query_mob->dx_query_ctx = NULL;
864 vmw_bo_unreference(&uctx->dx_query_mob);
865 uctx->dx_query_mob = NULL;
871 /* Can only have one MOB per context for queries */
872 if (uctx->dx_query_mob && uctx->dx_query_mob != mob)
875 mob->dx_query_ctx = ctx_res;
877 if (!uctx->dx_query_mob)
878 uctx->dx_query_mob = vmw_bo_reference(mob);
884 * vmw_context_get_dx_query_mob - Returns non-counted reference to DX query mob
886 * @ctx_res: The context resource
888 struct vmw_buffer_object *
889 vmw_context_get_dx_query_mob(struct vmw_resource *ctx_res)
891 struct vmw_user_context *uctx =
892 container_of(ctx_res, struct vmw_user_context, res);
894 return uctx->dx_query_mob;