1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2014-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 * Treat context OTables as resources to make use of the resource
29 * backing MOB eviction mechanism, that is used to read back the COTable
30 * whenever the backing MOB is evicted.
33 #include <drm/ttm/ttm_placement.h>
35 #include "vmwgfx_drv.h"
36 #include "vmwgfx_resource_priv.h"
37 #include "vmwgfx_so.h"
40 * struct vmw_cotable - Context Object Table resource
42 * @res: struct vmw_resource we are deriving from.
43 * @ctx: non-refcounted pointer to the owning context.
44 * @size_read_back: Size of data read back during eviction.
45 * @seen_entries: Seen entries in command stream for this cotable.
46 * @type: The cotable type.
47 * @scrubbed: Whether the cotable has been scrubbed.
48 * @resource_list: List of resources in the cotable.
51 struct vmw_resource res;
52 struct vmw_resource *ctx;
53 size_t size_read_back;
57 struct list_head resource_list;
61 * struct vmw_cotable_info - Static info about cotable types
63 * @min_initial_entries: Min number of initial intries at cotable allocation
64 * for this cotable type.
65 * @size: Size of each entry.
67 struct vmw_cotable_info {
68 u32 min_initial_entries;
70 void (*unbind_func)(struct vmw_private *, struct list_head *,
74 static const struct vmw_cotable_info co_info[] = {
75 {1, sizeof(SVGACOTableDXRTViewEntry), &vmw_view_cotable_list_destroy},
76 {1, sizeof(SVGACOTableDXDSViewEntry), &vmw_view_cotable_list_destroy},
77 {1, sizeof(SVGACOTableDXSRViewEntry), &vmw_view_cotable_list_destroy},
78 {1, sizeof(SVGACOTableDXElementLayoutEntry), NULL},
79 {1, sizeof(SVGACOTableDXBlendStateEntry), NULL},
80 {1, sizeof(SVGACOTableDXDepthStencilEntry), NULL},
81 {1, sizeof(SVGACOTableDXRasterizerStateEntry), NULL},
82 {1, sizeof(SVGACOTableDXSamplerEntry), NULL},
83 {1, sizeof(SVGACOTableDXStreamOutputEntry), NULL},
84 {1, sizeof(SVGACOTableDXQueryEntry), NULL},
85 {1, sizeof(SVGACOTableDXShaderEntry), &vmw_dx_shader_cotable_list_scrub}
89 * Cotables with bindings that we remove must be scrubbed first,
90 * otherwise, the device will swap in an invalid context when we remove
91 * bindings before scrubbing a cotable...
93 const SVGACOTableType vmw_cotable_scrub_order[] = {
97 SVGA_COTABLE_DXSHADER,
98 SVGA_COTABLE_ELEMENTLAYOUT,
99 SVGA_COTABLE_BLENDSTATE,
100 SVGA_COTABLE_DEPTHSTENCIL,
101 SVGA_COTABLE_RASTERIZERSTATE,
102 SVGA_COTABLE_SAMPLER,
103 SVGA_COTABLE_STREAMOUTPUT,
104 SVGA_COTABLE_DXQUERY,
107 static int vmw_cotable_bind(struct vmw_resource *res,
108 struct ttm_validate_buffer *val_buf);
109 static int vmw_cotable_unbind(struct vmw_resource *res,
111 struct ttm_validate_buffer *val_buf);
112 static int vmw_cotable_create(struct vmw_resource *res);
113 static int vmw_cotable_destroy(struct vmw_resource *res);
115 static const struct vmw_res_func vmw_cotable_func = {
116 .res_type = vmw_res_cotable,
117 .needs_backup = true,
119 .type_name = "context guest backed object tables",
120 .backup_placement = &vmw_mob_placement,
121 .create = vmw_cotable_create,
122 .destroy = vmw_cotable_destroy,
123 .bind = vmw_cotable_bind,
124 .unbind = vmw_cotable_unbind,
128 * vmw_cotable - Convert a struct vmw_resource pointer to a struct
129 * vmw_cotable pointer
131 * @res: Pointer to the resource.
133 static struct vmw_cotable *vmw_cotable(struct vmw_resource *res)
135 return container_of(res, struct vmw_cotable, res);
139 * vmw_cotable_destroy - Cotable resource destroy callback
141 * @res: Pointer to the cotable resource.
143 * There is no device cotable destroy command, so this function only
144 * makes sure that the resource id is set to invalid.
146 static int vmw_cotable_destroy(struct vmw_resource *res)
153 * vmw_cotable_unscrub - Undo a cotable unscrub operation
155 * @res: Pointer to the cotable resource
157 * This function issues commands to (re)bind the cotable to
158 * its backing mob, which needs to be validated and reserved at this point.
159 * This is identical to bind() except the function interface looks different.
161 static int vmw_cotable_unscrub(struct vmw_resource *res)
163 struct vmw_cotable *vcotbl = vmw_cotable(res);
164 struct vmw_private *dev_priv = res->dev_priv;
165 struct ttm_buffer_object *bo = &res->backup->base;
167 SVGA3dCmdHeader header;
168 SVGA3dCmdDXSetCOTable body;
171 WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
172 dma_resv_assert_held(bo->base.resv);
174 cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
178 WARN_ON(vcotbl->ctx->id == SVGA3D_INVALID_ID);
179 WARN_ON(bo->mem.mem_type != VMW_PL_MOB);
180 cmd->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
181 cmd->header.size = sizeof(cmd->body);
182 cmd->body.cid = vcotbl->ctx->id;
183 cmd->body.type = vcotbl->type;
184 cmd->body.mobid = bo->mem.start;
185 cmd->body.validSizeInBytes = vcotbl->size_read_back;
187 vmw_fifo_commit_flush(dev_priv, sizeof(*cmd));
188 vcotbl->scrubbed = false;
194 * vmw_cotable_bind - Undo a cotable unscrub operation
196 * @res: Pointer to the cotable resource
197 * @val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
198 * for convenience / fencing.
200 * This function issues commands to (re)bind the cotable to
201 * its backing mob, which needs to be validated and reserved at this point.
203 static int vmw_cotable_bind(struct vmw_resource *res,
204 struct ttm_validate_buffer *val_buf)
207 * The create() callback may have changed @res->backup without
208 * the caller noticing, and with val_buf->bo still pointing to
209 * the old backup buffer. Although hackish, and not used currently,
210 * take the opportunity to correct the value here so that it's not
211 * misused in the future.
213 val_buf->bo = &res->backup->base;
215 return vmw_cotable_unscrub(res);
219 * vmw_cotable_scrub - Scrub the cotable from the device.
221 * @res: Pointer to the cotable resource.
222 * @readback: Whether initiate a readback of the cotable data to the backup
225 * In some situations (context swapouts) it might be desirable to make the
226 * device forget about the cotable without performing a full unbind. A full
227 * unbind requires reserved backup buffers and it might not be possible to
228 * reserve them due to locking order violation issues. The vmw_cotable_scrub
229 * function implements a partial unbind() without that requirement but with the
230 * following restrictions.
231 * 1) Before the cotable is again used by the GPU, vmw_cotable_unscrub() must
233 * 2) Before the cotable backing buffer is used by the CPU, or during the
234 * resource destruction, vmw_cotable_unbind() must be called.
236 int vmw_cotable_scrub(struct vmw_resource *res, bool readback)
238 struct vmw_cotable *vcotbl = vmw_cotable(res);
239 struct vmw_private *dev_priv = res->dev_priv;
243 SVGA3dCmdHeader header;
244 SVGA3dCmdDXReadbackCOTable body;
247 SVGA3dCmdHeader header;
248 SVGA3dCmdDXSetCOTable body;
251 if (vcotbl->scrubbed)
254 if (co_info[vcotbl->type].unbind_func)
255 co_info[vcotbl->type].unbind_func(dev_priv,
256 &vcotbl->resource_list,
258 submit_size = sizeof(*cmd1);
260 submit_size += sizeof(*cmd0);
262 cmd1 = VMW_FIFO_RESERVE(dev_priv, submit_size);
266 vcotbl->size_read_back = 0;
268 cmd0 = (void *) cmd1;
269 cmd0->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
270 cmd0->header.size = sizeof(cmd0->body);
271 cmd0->body.cid = vcotbl->ctx->id;
272 cmd0->body.type = vcotbl->type;
273 cmd1 = (void *) &cmd0[1];
274 vcotbl->size_read_back = res->backup_size;
276 cmd1->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
277 cmd1->header.size = sizeof(cmd1->body);
278 cmd1->body.cid = vcotbl->ctx->id;
279 cmd1->body.type = vcotbl->type;
280 cmd1->body.mobid = SVGA3D_INVALID_ID;
281 cmd1->body.validSizeInBytes = 0;
282 vmw_fifo_commit_flush(dev_priv, submit_size);
283 vcotbl->scrubbed = true;
285 /* Trigger a create() on next validate. */
292 * vmw_cotable_unbind - Cotable resource unbind callback
294 * @res: Pointer to the cotable resource.
295 * @readback: Whether to read back cotable data to the backup buffer.
296 * val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
297 * for convenience / fencing.
299 * Unbinds the cotable from the device and fences the backup buffer.
301 static int vmw_cotable_unbind(struct vmw_resource *res,
303 struct ttm_validate_buffer *val_buf)
305 struct vmw_cotable *vcotbl = vmw_cotable(res);
306 struct vmw_private *dev_priv = res->dev_priv;
307 struct ttm_buffer_object *bo = val_buf->bo;
308 struct vmw_fence_obj *fence;
310 if (list_empty(&res->mob_head))
313 WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
314 dma_resv_assert_held(bo->base.resv);
316 mutex_lock(&dev_priv->binding_mutex);
317 if (!vcotbl->scrubbed)
318 vmw_dx_context_scrub_cotables(vcotbl->ctx, readback);
319 mutex_unlock(&dev_priv->binding_mutex);
320 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
321 vmw_bo_fence_single(bo, fence);
322 if (likely(fence != NULL))
323 vmw_fence_obj_unreference(&fence);
329 * vmw_cotable_readback - Read back a cotable without unbinding.
331 * @res: The cotable resource.
333 * Reads back a cotable to its backing mob without scrubbing the MOB from
334 * the cotable. The MOB is fenced for subsequent CPU access.
336 static int vmw_cotable_readback(struct vmw_resource *res)
338 struct vmw_cotable *vcotbl = vmw_cotable(res);
339 struct vmw_private *dev_priv = res->dev_priv;
342 SVGA3dCmdHeader header;
343 SVGA3dCmdDXReadbackCOTable body;
345 struct vmw_fence_obj *fence;
347 if (!vcotbl->scrubbed) {
348 cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
352 cmd->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
353 cmd->header.size = sizeof(cmd->body);
354 cmd->body.cid = vcotbl->ctx->id;
355 cmd->body.type = vcotbl->type;
356 vcotbl->size_read_back = res->backup_size;
357 vmw_fifo_commit(dev_priv, sizeof(*cmd));
360 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
361 vmw_bo_fence_single(&res->backup->base, fence);
362 vmw_fence_obj_unreference(&fence);
368 * vmw_cotable_resize - Resize a cotable.
370 * @res: The cotable resource.
371 * @new_size: The new size.
373 * Resizes a cotable and binds the new backup buffer.
374 * On failure the cotable is left intact.
375 * Important! This function may not fail once the MOB switch has been
376 * committed to hardware. That would put the device context in an
377 * invalid state which we can't currently recover from.
379 static int vmw_cotable_resize(struct vmw_resource *res, size_t new_size)
381 struct ttm_operation_ctx ctx = { false, false };
382 struct vmw_private *dev_priv = res->dev_priv;
383 struct vmw_cotable *vcotbl = vmw_cotable(res);
384 struct vmw_buffer_object *buf, *old_buf = res->backup;
385 struct ttm_buffer_object *bo, *old_bo = &res->backup->base;
386 size_t old_size = res->backup_size;
387 size_t old_size_read_back = vcotbl->size_read_back;
388 size_t cur_size_read_back;
389 struct ttm_bo_kmap_obj old_map, new_map;
393 ret = vmw_cotable_readback(res);
397 cur_size_read_back = vcotbl->size_read_back;
398 vcotbl->size_read_back = old_size_read_back;
401 * While device is processing, Allocate and reserve a buffer object
402 * for the new COTable. Initially pin the buffer object to make sure
403 * we can use tryreserve without failure.
405 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
409 ret = vmw_bo_init(dev_priv, buf, new_size, &vmw_mob_ne_placement,
410 true, vmw_bo_bo_free);
412 DRM_ERROR("Failed initializing new cotable MOB.\n");
417 WARN_ON_ONCE(ttm_bo_reserve(bo, false, true, NULL));
419 ret = ttm_bo_wait(old_bo, false, false);
420 if (unlikely(ret != 0)) {
421 DRM_ERROR("Failed waiting for cotable unbind.\n");
426 * Do a page by page copy of COTables. This eliminates slow vmap()s.
427 * This should really be a TTM utility.
429 for (i = 0; i < old_bo->num_pages; ++i) {
432 ret = ttm_bo_kmap(old_bo, i, 1, &old_map);
433 if (unlikely(ret != 0)) {
434 DRM_ERROR("Failed mapping old COTable on resize.\n");
437 ret = ttm_bo_kmap(bo, i, 1, &new_map);
438 if (unlikely(ret != 0)) {
439 DRM_ERROR("Failed mapping new COTable on resize.\n");
442 memcpy(ttm_kmap_obj_virtual(&new_map, &dummy),
443 ttm_kmap_obj_virtual(&old_map, &dummy),
445 ttm_bo_kunmap(&new_map);
446 ttm_bo_kunmap(&old_map);
449 /* Unpin new buffer, and switch backup buffers. */
450 ret = ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
451 if (unlikely(ret != 0)) {
452 DRM_ERROR("Failed validating new COTable backup buffer.\n");
457 res->backup_size = new_size;
458 vcotbl->size_read_back = cur_size_read_back;
461 * Now tell the device to switch. If this fails, then we need to
462 * revert the full resize.
464 ret = vmw_cotable_unscrub(res);
466 DRM_ERROR("Failed switching COTable backup buffer.\n");
467 res->backup = old_buf;
468 res->backup_size = old_size;
469 vcotbl->size_read_back = old_size_read_back;
473 /* Let go of the old mob. */
474 list_del(&res->mob_head);
475 list_add_tail(&res->mob_head, &buf->res_list);
476 vmw_bo_unreference(&old_buf);
477 res->id = vcotbl->type;
482 ttm_bo_kunmap(&old_map);
484 ttm_bo_unreserve(bo);
485 vmw_bo_unreference(&buf);
491 * vmw_cotable_create - Cotable resource create callback
493 * @res: Pointer to a cotable resource.
495 * There is no separate create command for cotables, so this callback, which
496 * is called before bind() in the validation sequence is instead used for two
498 * 1) Unscrub the cotable if it is scrubbed and still attached to a backup
499 * buffer, that is, if @res->mob_head is non-empty.
500 * 2) Resize the cotable if needed.
502 static int vmw_cotable_create(struct vmw_resource *res)
504 struct vmw_cotable *vcotbl = vmw_cotable(res);
505 size_t new_size = res->backup_size;
509 /* Check whether we need to resize the cotable */
510 needed_size = (vcotbl->seen_entries + 1) * co_info[vcotbl->type].size;
511 while (needed_size > new_size)
514 if (likely(new_size <= res->backup_size)) {
515 if (vcotbl->scrubbed && !list_empty(&res->mob_head)) {
516 ret = vmw_cotable_unscrub(res);
520 res->id = vcotbl->type;
524 return vmw_cotable_resize(res, new_size);
528 * vmw_hw_cotable_destroy - Cotable hw_destroy callback
530 * @res: Pointer to a cotable resource.
532 * The final (part of resource destruction) destroy callback.
534 static void vmw_hw_cotable_destroy(struct vmw_resource *res)
536 (void) vmw_cotable_destroy(res);
539 static size_t cotable_acc_size;
542 * vmw_cotable_free - Cotable resource destructor
544 * @res: Pointer to a cotable resource.
546 static void vmw_cotable_free(struct vmw_resource *res)
548 struct vmw_private *dev_priv = res->dev_priv;
551 ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
555 * vmw_cotable_alloc - Create a cotable resource
557 * @dev_priv: Pointer to a device private struct.
558 * @ctx: Pointer to the context resource.
559 * The cotable resource will not add a refcount.
560 * @type: The cotable type.
562 struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
563 struct vmw_resource *ctx,
566 struct vmw_cotable *vcotbl;
567 struct ttm_operation_ctx ttm_opt_ctx = {
568 .interruptible = true,
574 if (unlikely(cotable_acc_size == 0))
575 cotable_acc_size = ttm_round_pot(sizeof(struct vmw_cotable));
577 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
578 cotable_acc_size, &ttm_opt_ctx);
582 vcotbl = kzalloc(sizeof(*vcotbl), GFP_KERNEL);
583 if (unlikely(!vcotbl)) {
588 ret = vmw_resource_init(dev_priv, &vcotbl->res, true,
589 vmw_cotable_free, &vmw_cotable_func);
590 if (unlikely(ret != 0))
593 INIT_LIST_HEAD(&vcotbl->resource_list);
594 vcotbl->res.id = type;
595 vcotbl->res.backup_size = PAGE_SIZE;
596 num_entries = PAGE_SIZE / co_info[type].size;
597 if (num_entries < co_info[type].min_initial_entries) {
598 vcotbl->res.backup_size = co_info[type].min_initial_entries *
600 vcotbl->res.backup_size =
601 (vcotbl->res.backup_size + PAGE_SIZE - 1) & PAGE_MASK;
604 vcotbl->scrubbed = true;
605 vcotbl->seen_entries = -1;
609 vcotbl->res.hw_destroy = vmw_hw_cotable_destroy;
616 ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
621 * vmw_cotable_notify - Notify the cotable about an item creation
623 * @res: Pointer to a cotable resource.
626 int vmw_cotable_notify(struct vmw_resource *res, int id)
628 struct vmw_cotable *vcotbl = vmw_cotable(res);
630 if (id < 0 || id >= SVGA_COTABLE_MAX_IDS) {
631 DRM_ERROR("Illegal COTable id. Type is %u. Id is %d\n",
632 (unsigned) vcotbl->type, id);
636 if (vcotbl->seen_entries < id) {
637 /* Trigger a call to create() on next validate */
639 vcotbl->seen_entries = id;
646 * vmw_cotable_add_view - add a view to the cotable's list of active views.
648 * @res: pointer struct vmw_resource representing the cotable.
649 * @head: pointer to the struct list_head member of the resource, dedicated
650 * to the cotable active resource list.
652 void vmw_cotable_add_resource(struct vmw_resource *res, struct list_head *head)
654 struct vmw_cotable *vcotbl =
655 container_of(res, struct vmw_cotable, res);
657 list_add_tail(head, &vcotbl->resource_list);