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
;
35 struct vmw_ctx_binding_state cbs
;
40 typedef int (*vmw_scrub_func
)(struct vmw_ctx_bindinfo
*, bool);
42 static void vmw_user_context_free(struct vmw_resource
*res
);
43 static struct vmw_resource
*
44 vmw_user_context_base_to_res(struct ttm_base_object
*base
);
46 static int vmw_gb_context_create(struct vmw_resource
*res
);
47 static int vmw_gb_context_bind(struct vmw_resource
*res
,
48 struct ttm_validate_buffer
*val_buf
);
49 static int vmw_gb_context_unbind(struct vmw_resource
*res
,
51 struct ttm_validate_buffer
*val_buf
);
52 static int vmw_gb_context_destroy(struct vmw_resource
*res
);
53 static int vmw_context_scrub_shader(struct vmw_ctx_bindinfo
*bi
, bool rebind
);
54 static int vmw_context_scrub_render_target(struct vmw_ctx_bindinfo
*bi
,
56 static int vmw_context_scrub_texture(struct vmw_ctx_bindinfo
*bi
, bool rebind
);
57 static void vmw_context_binding_state_scrub(struct vmw_ctx_binding_state
*cbs
);
58 static void vmw_context_binding_state_kill(struct vmw_ctx_binding_state
*cbs
);
59 static uint64_t vmw_user_context_size
;
61 static const struct vmw_user_resource_conv user_context_conv
= {
62 .object_type
= VMW_RES_CONTEXT
,
63 .base_obj_to_res
= vmw_user_context_base_to_res
,
64 .res_free
= vmw_user_context_free
67 const struct vmw_user_resource_conv
*user_context_converter
=
71 static const struct vmw_res_func vmw_legacy_context_func
= {
72 .res_type
= vmw_res_context
,
73 .needs_backup
= false,
75 .type_name
= "legacy contexts",
76 .backup_placement
= NULL
,
83 static const struct vmw_res_func vmw_gb_context_func
= {
84 .res_type
= vmw_res_context
,
87 .type_name
= "guest backed contexts",
88 .backup_placement
= &vmw_mob_placement
,
89 .create
= vmw_gb_context_create
,
90 .destroy
= vmw_gb_context_destroy
,
91 .bind
= vmw_gb_context_bind
,
92 .unbind
= vmw_gb_context_unbind
95 static const vmw_scrub_func vmw_scrub_funcs
[vmw_ctx_binding_max
] = {
96 [vmw_ctx_binding_shader
] = vmw_context_scrub_shader
,
97 [vmw_ctx_binding_rt
] = vmw_context_scrub_render_target
,
98 [vmw_ctx_binding_tex
] = vmw_context_scrub_texture
};
101 * Context management:
104 static void vmw_hw_context_destroy(struct vmw_resource
*res
)
107 struct vmw_private
*dev_priv
= res
->dev_priv
;
109 SVGA3dCmdHeader header
;
110 SVGA3dCmdDestroyContext body
;
114 if (res
->func
->destroy
== vmw_gb_context_destroy
) {
115 mutex_lock(&dev_priv
->cmdbuf_mutex
);
116 mutex_lock(&dev_priv
->binding_mutex
);
117 (void) vmw_context_binding_state_kill
118 (&container_of(res
, struct vmw_user_context
, res
)->cbs
);
119 (void) vmw_gb_context_destroy(res
);
120 mutex_unlock(&dev_priv
->binding_mutex
);
121 if (dev_priv
->pinned_bo
!= NULL
&&
122 !dev_priv
->query_cid_valid
)
123 __vmw_execbuf_release_pinned_bo(dev_priv
, NULL
);
124 mutex_unlock(&dev_priv
->cmdbuf_mutex
);
128 vmw_execbuf_release_pinned_bo(dev_priv
);
129 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
130 if (unlikely(cmd
== NULL
)) {
131 DRM_ERROR("Failed reserving FIFO space for surface "
136 cmd
->header
.id
= cpu_to_le32(SVGA_3D_CMD_CONTEXT_DESTROY
);
137 cmd
->header
.size
= cpu_to_le32(sizeof(cmd
->body
));
138 cmd
->body
.cid
= cpu_to_le32(res
->id
);
140 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
141 vmw_3d_resource_dec(dev_priv
, false);
144 static int vmw_gb_context_init(struct vmw_private
*dev_priv
,
145 struct vmw_resource
*res
,
146 void (*res_free
) (struct vmw_resource
*res
))
149 struct vmw_user_context
*uctx
=
150 container_of(res
, struct vmw_user_context
, res
);
152 ret
= vmw_resource_init(dev_priv
, res
, true,
153 res_free
, &vmw_gb_context_func
);
154 res
->backup_size
= SVGA3D_CONTEXT_DATA_SIZE
;
156 if (unlikely(ret
!= 0)) {
164 memset(&uctx
->cbs
, 0, sizeof(uctx
->cbs
));
165 INIT_LIST_HEAD(&uctx
->cbs
.list
);
167 vmw_resource_activate(res
, vmw_hw_context_destroy
);
171 static int vmw_context_init(struct vmw_private
*dev_priv
,
172 struct vmw_resource
*res
,
173 void (*res_free
) (struct vmw_resource
*res
))
178 SVGA3dCmdHeader header
;
179 SVGA3dCmdDefineContext body
;
182 if (dev_priv
->has_mob
)
183 return vmw_gb_context_init(dev_priv
, res
, res_free
);
185 ret
= vmw_resource_init(dev_priv
, res
, false,
186 res_free
, &vmw_legacy_context_func
);
188 if (unlikely(ret
!= 0)) {
189 DRM_ERROR("Failed to allocate a resource id.\n");
193 if (unlikely(res
->id
>= SVGA3D_MAX_CONTEXT_IDS
)) {
194 DRM_ERROR("Out of hw context ids.\n");
195 vmw_resource_unreference(&res
);
199 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
200 if (unlikely(cmd
== NULL
)) {
201 DRM_ERROR("Fifo reserve failed.\n");
202 vmw_resource_unreference(&res
);
206 cmd
->header
.id
= cpu_to_le32(SVGA_3D_CMD_CONTEXT_DEFINE
);
207 cmd
->header
.size
= cpu_to_le32(sizeof(cmd
->body
));
208 cmd
->body
.cid
= cpu_to_le32(res
->id
);
210 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
211 (void) vmw_3d_resource_inc(dev_priv
, false);
212 vmw_resource_activate(res
, vmw_hw_context_destroy
);
216 if (res_free
== NULL
)
223 struct vmw_resource
*vmw_context_alloc(struct vmw_private
*dev_priv
)
225 struct vmw_resource
*res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
228 if (unlikely(res
== NULL
))
231 ret
= vmw_context_init(dev_priv
, res
, NULL
);
233 return (ret
== 0) ? res
: NULL
;
237 static int vmw_gb_context_create(struct vmw_resource
*res
)
239 struct vmw_private
*dev_priv
= res
->dev_priv
;
242 SVGA3dCmdHeader header
;
243 SVGA3dCmdDefineGBContext body
;
246 if (likely(res
->id
!= -1))
249 ret
= vmw_resource_alloc_id(res
);
250 if (unlikely(ret
!= 0)) {
251 DRM_ERROR("Failed to allocate a context id.\n");
255 if (unlikely(res
->id
>= VMWGFX_NUM_GB_CONTEXT
)) {
260 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
261 if (unlikely(cmd
== NULL
)) {
262 DRM_ERROR("Failed reserving FIFO space for context "
268 cmd
->header
.id
= SVGA_3D_CMD_DEFINE_GB_CONTEXT
;
269 cmd
->header
.size
= sizeof(cmd
->body
);
270 cmd
->body
.cid
= res
->id
;
271 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
272 (void) vmw_3d_resource_inc(dev_priv
, false);
277 vmw_resource_release_id(res
);
282 static int vmw_gb_context_bind(struct vmw_resource
*res
,
283 struct ttm_validate_buffer
*val_buf
)
285 struct vmw_private
*dev_priv
= res
->dev_priv
;
287 SVGA3dCmdHeader header
;
288 SVGA3dCmdBindGBContext body
;
290 struct ttm_buffer_object
*bo
= val_buf
->bo
;
292 BUG_ON(bo
->mem
.mem_type
!= VMW_PL_MOB
);
294 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
295 if (unlikely(cmd
== NULL
)) {
296 DRM_ERROR("Failed reserving FIFO space for context "
301 cmd
->header
.id
= SVGA_3D_CMD_BIND_GB_CONTEXT
;
302 cmd
->header
.size
= sizeof(cmd
->body
);
303 cmd
->body
.cid
= res
->id
;
304 cmd
->body
.mobid
= bo
->mem
.start
;
305 cmd
->body
.validContents
= res
->backup_dirty
;
306 res
->backup_dirty
= false;
307 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
312 static int vmw_gb_context_unbind(struct vmw_resource
*res
,
314 struct ttm_validate_buffer
*val_buf
)
316 struct vmw_private
*dev_priv
= res
->dev_priv
;
317 struct ttm_buffer_object
*bo
= val_buf
->bo
;
318 struct vmw_fence_obj
*fence
;
319 struct vmw_user_context
*uctx
=
320 container_of(res
, struct vmw_user_context
, res
);
323 SVGA3dCmdHeader header
;
324 SVGA3dCmdReadbackGBContext body
;
327 SVGA3dCmdHeader header
;
328 SVGA3dCmdBindGBContext body
;
330 uint32_t submit_size
;
334 BUG_ON(bo
->mem
.mem_type
!= VMW_PL_MOB
);
336 mutex_lock(&dev_priv
->binding_mutex
);
337 vmw_context_binding_state_scrub(&uctx
->cbs
);
339 submit_size
= sizeof(*cmd2
) + (readback
? sizeof(*cmd1
) : 0);
341 cmd
= vmw_fifo_reserve(dev_priv
, submit_size
);
342 if (unlikely(cmd
== NULL
)) {
343 DRM_ERROR("Failed reserving FIFO space for context "
345 mutex_unlock(&dev_priv
->binding_mutex
);
352 cmd1
->header
.id
= SVGA_3D_CMD_READBACK_GB_CONTEXT
;
353 cmd1
->header
.size
= sizeof(cmd1
->body
);
354 cmd1
->body
.cid
= res
->id
;
355 cmd2
= (void *) (&cmd1
[1]);
357 cmd2
->header
.id
= SVGA_3D_CMD_BIND_GB_CONTEXT
;
358 cmd2
->header
.size
= sizeof(cmd2
->body
);
359 cmd2
->body
.cid
= res
->id
;
360 cmd2
->body
.mobid
= SVGA3D_INVALID_ID
;
362 vmw_fifo_commit(dev_priv
, submit_size
);
363 mutex_unlock(&dev_priv
->binding_mutex
);
366 * Create a fence object and fence the backup buffer.
369 (void) vmw_execbuf_fence_commands(NULL
, dev_priv
,
372 vmw_fence_single_bo(bo
, fence
);
374 if (likely(fence
!= NULL
))
375 vmw_fence_obj_unreference(&fence
);
380 static int vmw_gb_context_destroy(struct vmw_resource
*res
)
382 struct vmw_private
*dev_priv
= res
->dev_priv
;
384 SVGA3dCmdHeader header
;
385 SVGA3dCmdDestroyGBContext body
;
388 if (likely(res
->id
== -1))
391 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
392 if (unlikely(cmd
== NULL
)) {
393 DRM_ERROR("Failed reserving FIFO space for context "
398 cmd
->header
.id
= SVGA_3D_CMD_DESTROY_GB_CONTEXT
;
399 cmd
->header
.size
= sizeof(cmd
->body
);
400 cmd
->body
.cid
= res
->id
;
401 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
402 if (dev_priv
->query_cid
== res
->id
)
403 dev_priv
->query_cid_valid
= false;
404 vmw_resource_release_id(res
);
405 vmw_3d_resource_dec(dev_priv
, false);
411 * User-space context management:
414 static struct vmw_resource
*
415 vmw_user_context_base_to_res(struct ttm_base_object
*base
)
417 return &(container_of(base
, struct vmw_user_context
, base
)->res
);
420 static void vmw_user_context_free(struct vmw_resource
*res
)
422 struct vmw_user_context
*ctx
=
423 container_of(res
, struct vmw_user_context
, res
);
424 struct vmw_private
*dev_priv
= res
->dev_priv
;
426 ttm_base_object_kfree(ctx
, base
);
427 ttm_mem_global_free(vmw_mem_glob(dev_priv
),
428 vmw_user_context_size
);
432 * This function is called when user space has no more references on the
433 * base object. It releases the base-object's reference on the resource object.
436 static void vmw_user_context_base_release(struct ttm_base_object
**p_base
)
438 struct ttm_base_object
*base
= *p_base
;
439 struct vmw_user_context
*ctx
=
440 container_of(base
, struct vmw_user_context
, base
);
441 struct vmw_resource
*res
= &ctx
->res
;
444 vmw_resource_unreference(&res
);
447 int vmw_context_destroy_ioctl(struct drm_device
*dev
, void *data
,
448 struct drm_file
*file_priv
)
450 struct drm_vmw_context_arg
*arg
= (struct drm_vmw_context_arg
*)data
;
451 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
453 return ttm_ref_object_base_unref(tfile
, arg
->cid
, TTM_REF_USAGE
);
456 int vmw_context_define_ioctl(struct drm_device
*dev
, void *data
,
457 struct drm_file
*file_priv
)
459 struct vmw_private
*dev_priv
= vmw_priv(dev
);
460 struct vmw_user_context
*ctx
;
461 struct vmw_resource
*res
;
462 struct vmw_resource
*tmp
;
463 struct drm_vmw_context_arg
*arg
= (struct drm_vmw_context_arg
*)data
;
464 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
469 * Approximate idr memory usage with 128 bytes. It will be limited
470 * by maximum number_of contexts anyway.
473 if (unlikely(vmw_user_context_size
== 0))
474 vmw_user_context_size
= ttm_round_pot(sizeof(*ctx
)) + 128;
476 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
477 if (unlikely(ret
!= 0))
480 ret
= ttm_mem_global_alloc(vmw_mem_glob(dev_priv
),
481 vmw_user_context_size
,
483 if (unlikely(ret
!= 0)) {
484 if (ret
!= -ERESTARTSYS
)
485 DRM_ERROR("Out of graphics memory for context"
490 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
491 if (unlikely(ctx
== NULL
)) {
492 ttm_mem_global_free(vmw_mem_glob(dev_priv
),
493 vmw_user_context_size
);
499 ctx
->base
.shareable
= false;
500 ctx
->base
.tfile
= NULL
;
503 * From here on, the destructor takes over resource freeing.
506 ret
= vmw_context_init(dev_priv
, res
, vmw_user_context_free
);
507 if (unlikely(ret
!= 0))
510 tmp
= vmw_resource_reference(&ctx
->res
);
511 ret
= ttm_base_object_init(tfile
, &ctx
->base
, false, VMW_RES_CONTEXT
,
512 &vmw_user_context_base_release
, NULL
);
514 if (unlikely(ret
!= 0)) {
515 vmw_resource_unreference(&tmp
);
519 arg
->cid
= ctx
->base
.hash
.key
;
521 vmw_resource_unreference(&res
);
523 ttm_read_unlock(&dev_priv
->reservation_sem
);
529 * vmw_context_scrub_shader - scrub a shader binding from a context.
531 * @bi: single binding information.
532 * @rebind: Whether to issue a bind instead of scrub command.
534 static int vmw_context_scrub_shader(struct vmw_ctx_bindinfo
*bi
, bool rebind
)
536 struct vmw_private
*dev_priv
= bi
->ctx
->dev_priv
;
538 SVGA3dCmdHeader header
;
539 SVGA3dCmdSetShader body
;
542 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
543 if (unlikely(cmd
== NULL
)) {
544 DRM_ERROR("Failed reserving FIFO space for shader "
549 cmd
->header
.id
= SVGA_3D_CMD_SET_SHADER
;
550 cmd
->header
.size
= sizeof(cmd
->body
);
551 cmd
->body
.cid
= bi
->ctx
->id
;
552 cmd
->body
.type
= bi
->i1
.shader_type
;
553 cmd
->body
.shid
= ((rebind
) ? bi
->res
->id
: SVGA3D_INVALID_ID
);
554 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
560 * vmw_context_scrub_render_target - scrub a render target binding
563 * @bi: single binding information.
564 * @rebind: Whether to issue a bind instead of scrub command.
566 static int vmw_context_scrub_render_target(struct vmw_ctx_bindinfo
*bi
,
569 struct vmw_private
*dev_priv
= bi
->ctx
->dev_priv
;
571 SVGA3dCmdHeader header
;
572 SVGA3dCmdSetRenderTarget body
;
575 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
576 if (unlikely(cmd
== NULL
)) {
577 DRM_ERROR("Failed reserving FIFO space for render target "
582 cmd
->header
.id
= SVGA_3D_CMD_SETRENDERTARGET
;
583 cmd
->header
.size
= sizeof(cmd
->body
);
584 cmd
->body
.cid
= bi
->ctx
->id
;
585 cmd
->body
.type
= bi
->i1
.rt_type
;
586 cmd
->body
.target
.sid
= ((rebind
) ? bi
->res
->id
: SVGA3D_INVALID_ID
);
587 cmd
->body
.target
.face
= 0;
588 cmd
->body
.target
.mipmap
= 0;
589 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
595 * vmw_context_scrub_texture - scrub a texture binding from a context.
597 * @bi: single binding information.
598 * @rebind: Whether to issue a bind instead of scrub command.
600 * TODO: Possibly complement this function with a function that takes
601 * a list of texture bindings and combines them to a single command.
603 static int vmw_context_scrub_texture(struct vmw_ctx_bindinfo
*bi
,
606 struct vmw_private
*dev_priv
= bi
->ctx
->dev_priv
;
608 SVGA3dCmdHeader header
;
610 SVGA3dCmdSetTextureState c
;
611 SVGA3dTextureState s1
;
615 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
616 if (unlikely(cmd
== NULL
)) {
617 DRM_ERROR("Failed reserving FIFO space for texture "
623 cmd
->header
.id
= SVGA_3D_CMD_SETTEXTURESTATE
;
624 cmd
->header
.size
= sizeof(cmd
->body
);
625 cmd
->body
.c
.cid
= bi
->ctx
->id
;
626 cmd
->body
.s1
.stage
= bi
->i1
.texture_stage
;
627 cmd
->body
.s1
.name
= SVGA3D_TS_BIND_TEXTURE
;
628 cmd
->body
.s1
.value
= ((rebind
) ? bi
->res
->id
: SVGA3D_INVALID_ID
);
629 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
635 * vmw_context_binding_drop: Stop tracking a context binding
637 * @cb: Pointer to binding tracker storage.
639 * Stops tracking a context binding, and re-initializes its storage.
640 * Typically used when the context binding is replaced with a binding to
641 * another (or the same, for that matter) resource.
643 static void vmw_context_binding_drop(struct vmw_ctx_binding
*cb
)
645 list_del(&cb
->ctx_list
);
646 if (!list_empty(&cb
->res_list
))
647 list_del(&cb
->res_list
);
652 * vmw_context_binding_add: Start tracking a context binding
654 * @cbs: Pointer to the context binding state tracker.
655 * @bi: Information about the binding to track.
657 * Performs basic checks on the binding to make sure arguments are within
658 * bounds and then starts tracking the binding in the context binding
659 * state structure @cbs.
661 int vmw_context_binding_add(struct vmw_ctx_binding_state
*cbs
,
662 const struct vmw_ctx_bindinfo
*bi
)
664 struct vmw_ctx_binding
*loc
;
667 case vmw_ctx_binding_rt
:
668 if (unlikely((unsigned)bi
->i1
.rt_type
>= SVGA3D_RT_MAX
)) {
669 DRM_ERROR("Illegal render target type %u.\n",
670 (unsigned) bi
->i1
.rt_type
);
673 loc
= &cbs
->render_targets
[bi
->i1
.rt_type
];
675 case vmw_ctx_binding_tex
:
676 if (unlikely((unsigned)bi
->i1
.texture_stage
>=
677 SVGA3D_NUM_TEXTURE_UNITS
)) {
678 DRM_ERROR("Illegal texture/sampler unit %u.\n",
679 (unsigned) bi
->i1
.texture_stage
);
682 loc
= &cbs
->texture_units
[bi
->i1
.texture_stage
];
684 case vmw_ctx_binding_shader
:
685 if (unlikely((unsigned)bi
->i1
.shader_type
>=
686 SVGA3D_SHADERTYPE_MAX
)) {
687 DRM_ERROR("Illegal shader type %u.\n",
688 (unsigned) bi
->i1
.shader_type
);
691 loc
= &cbs
->shaders
[bi
->i1
.shader_type
];
697 if (loc
->bi
.ctx
!= NULL
)
698 vmw_context_binding_drop(loc
);
701 loc
->bi
.scrubbed
= false;
702 list_add_tail(&loc
->ctx_list
, &cbs
->list
);
703 INIT_LIST_HEAD(&loc
->res_list
);
709 * vmw_context_binding_transfer: Transfer a context binding tracking entry.
711 * @cbs: Pointer to the persistent context binding state tracker.
712 * @bi: Information about the binding to track.
715 static void vmw_context_binding_transfer(struct vmw_ctx_binding_state
*cbs
,
716 const struct vmw_ctx_bindinfo
*bi
)
718 struct vmw_ctx_binding
*loc
;
721 case vmw_ctx_binding_rt
:
722 loc
= &cbs
->render_targets
[bi
->i1
.rt_type
];
724 case vmw_ctx_binding_tex
:
725 loc
= &cbs
->texture_units
[bi
->i1
.texture_stage
];
727 case vmw_ctx_binding_shader
:
728 loc
= &cbs
->shaders
[bi
->i1
.shader_type
];
734 if (loc
->bi
.ctx
!= NULL
)
735 vmw_context_binding_drop(loc
);
737 if (bi
->res
!= NULL
) {
739 list_add_tail(&loc
->ctx_list
, &cbs
->list
);
740 list_add_tail(&loc
->res_list
, &bi
->res
->binding_head
);
745 * vmw_context_binding_kill - Kill a binding on the device
746 * and stop tracking it.
748 * @cb: Pointer to binding tracker storage.
750 * Emits FIFO commands to scrub a binding represented by @cb.
751 * Then stops tracking the binding and re-initializes its storage.
753 static void vmw_context_binding_kill(struct vmw_ctx_binding
*cb
)
755 if (!cb
->bi
.scrubbed
) {
756 (void) vmw_scrub_funcs
[cb
->bi
.bt
](&cb
->bi
, false);
757 cb
->bi
.scrubbed
= true;
759 vmw_context_binding_drop(cb
);
763 * vmw_context_binding_state_kill - Kill all bindings associated with a
764 * struct vmw_ctx_binding state structure, and re-initialize the structure.
766 * @cbs: Pointer to the context binding state tracker.
768 * Emits commands to scrub all bindings associated with the
769 * context binding state tracker. Then re-initializes the whole structure.
771 static void vmw_context_binding_state_kill(struct vmw_ctx_binding_state
*cbs
)
773 struct vmw_ctx_binding
*entry
, *next
;
775 list_for_each_entry_safe(entry
, next
, &cbs
->list
, ctx_list
)
776 vmw_context_binding_kill(entry
);
780 * vmw_context_binding_state_scrub - Scrub all bindings associated with a
781 * struct vmw_ctx_binding state structure.
783 * @cbs: Pointer to the context binding state tracker.
785 * Emits commands to scrub all bindings associated with the
786 * context binding state tracker.
788 static void vmw_context_binding_state_scrub(struct vmw_ctx_binding_state
*cbs
)
790 struct vmw_ctx_binding
*entry
;
792 list_for_each_entry(entry
, &cbs
->list
, ctx_list
) {
793 if (!entry
->bi
.scrubbed
) {
794 (void) vmw_scrub_funcs
[entry
->bi
.bt
](&entry
->bi
, false);
795 entry
->bi
.scrubbed
= true;
801 * vmw_context_binding_res_list_kill - Kill all bindings on a
802 * resource binding list
804 * @head: list head of resource binding list
806 * Kills all bindings associated with a specific resource. Typically
807 * called before the resource is destroyed.
809 void vmw_context_binding_res_list_kill(struct list_head
*head
)
811 struct vmw_ctx_binding
*entry
, *next
;
813 list_for_each_entry_safe(entry
, next
, head
, res_list
)
814 vmw_context_binding_kill(entry
);
818 * vmw_context_binding_res_list_scrub - Scrub all bindings on a
819 * resource binding list
821 * @head: list head of resource binding list
823 * Scrub all bindings associated with a specific resource. Typically
824 * called before the resource is evicted.
826 void vmw_context_binding_res_list_scrub(struct list_head
*head
)
828 struct vmw_ctx_binding
*entry
;
830 list_for_each_entry(entry
, head
, res_list
) {
831 if (!entry
->bi
.scrubbed
) {
832 (void) vmw_scrub_funcs
[entry
->bi
.bt
](&entry
->bi
, false);
833 entry
->bi
.scrubbed
= true;
839 * vmw_context_binding_state_transfer - Commit staged binding info
841 * @ctx: Pointer to context to commit the staged binding info to.
842 * @from: Staged binding info built during execbuf.
844 * Transfers binding info from a temporary structure to the persistent
845 * structure in the context. This can be done once commands
847 void vmw_context_binding_state_transfer(struct vmw_resource
*ctx
,
848 struct vmw_ctx_binding_state
*from
)
850 struct vmw_user_context
*uctx
=
851 container_of(ctx
, struct vmw_user_context
, res
);
852 struct vmw_ctx_binding
*entry
, *next
;
854 list_for_each_entry_safe(entry
, next
, &from
->list
, ctx_list
)
855 vmw_context_binding_transfer(&uctx
->cbs
, &entry
->bi
);
859 * vmw_context_rebind_all - Rebind all scrubbed bindings of a context
861 * @ctx: The context resource
863 * Walks through the context binding list and rebinds all scrubbed
866 int vmw_context_rebind_all(struct vmw_resource
*ctx
)
868 struct vmw_ctx_binding
*entry
;
869 struct vmw_user_context
*uctx
=
870 container_of(ctx
, struct vmw_user_context
, res
);
871 struct vmw_ctx_binding_state
*cbs
= &uctx
->cbs
;
874 list_for_each_entry(entry
, &cbs
->list
, ctx_list
) {
875 if (likely(!entry
->bi
.scrubbed
))
878 if (WARN_ON(entry
->bi
.res
== NULL
|| entry
->bi
.res
->id
==
882 ret
= vmw_scrub_funcs
[entry
->bi
.bt
](&entry
->bi
, true);
883 if (unlikely(ret
!= 0))
886 entry
->bi
.scrubbed
= false;
893 * vmw_context_binding_list - Return a list of context bindings
895 * @ctx: The context resource
897 * Returns the current list of bindings of the given context. Note that
898 * this list becomes stale as soon as the dev_priv::binding_mutex is unlocked.
900 struct list_head
*vmw_context_binding_list(struct vmw_resource
*ctx
)
902 return &(container_of(ctx
, struct vmw_user_context
, res
)->cbs
.list
);