1*677dec6eSriastradh /* $NetBSD: vmwgfx_cotable.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $ */
2d350ecf5Sriastradh
3*677dec6eSriastradh // SPDX-License-Identifier: GPL-2.0 OR MIT
4d350ecf5Sriastradh /**************************************************************************
5d350ecf5Sriastradh *
6*677dec6eSriastradh * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
7d350ecf5Sriastradh *
8d350ecf5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a
9d350ecf5Sriastradh * copy of this software and associated documentation files (the
10d350ecf5Sriastradh * "Software"), to deal in the Software without restriction, including
11d350ecf5Sriastradh * without limitation the rights to use, copy, modify, merge, publish,
12d350ecf5Sriastradh * distribute, sub license, and/or sell copies of the Software, and to
13d350ecf5Sriastradh * permit persons to whom the Software is furnished to do so, subject to
14d350ecf5Sriastradh * the following conditions:
15d350ecf5Sriastradh *
16d350ecf5Sriastradh * The above copyright notice and this permission notice (including the
17d350ecf5Sriastradh * next paragraph) shall be included in all copies or substantial portions
18d350ecf5Sriastradh * of the Software.
19d350ecf5Sriastradh *
20d350ecf5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21d350ecf5Sriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22d350ecf5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23d350ecf5Sriastradh * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24d350ecf5Sriastradh * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25d350ecf5Sriastradh * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26d350ecf5Sriastradh * USE OR OTHER DEALINGS IN THE SOFTWARE.
27d350ecf5Sriastradh *
28d350ecf5Sriastradh **************************************************************************/
29d350ecf5Sriastradh /*
30d350ecf5Sriastradh * Treat context OTables as resources to make use of the resource
31d350ecf5Sriastradh * backing MOB eviction mechanism, that is used to read back the COTable
32d350ecf5Sriastradh * whenever the backing MOB is evicted.
33d350ecf5Sriastradh */
34d350ecf5Sriastradh
35d350ecf5Sriastradh #include <sys/cdefs.h>
36*677dec6eSriastradh __KERNEL_RCSID(0, "$NetBSD: vmwgfx_cotable.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
37*677dec6eSriastradh
38*677dec6eSriastradh #include <drm/ttm/ttm_placement.h>
39d350ecf5Sriastradh
40d350ecf5Sriastradh #include "vmwgfx_drv.h"
41d350ecf5Sriastradh #include "vmwgfx_resource_priv.h"
42d350ecf5Sriastradh #include "vmwgfx_so.h"
43d350ecf5Sriastradh
44d350ecf5Sriastradh /**
45d350ecf5Sriastradh * struct vmw_cotable - Context Object Table resource
46d350ecf5Sriastradh *
47d350ecf5Sriastradh * @res: struct vmw_resource we are deriving from.
48d350ecf5Sriastradh * @ctx: non-refcounted pointer to the owning context.
49d350ecf5Sriastradh * @size_read_back: Size of data read back during eviction.
50d350ecf5Sriastradh * @seen_entries: Seen entries in command stream for this cotable.
51d350ecf5Sriastradh * @type: The cotable type.
52d350ecf5Sriastradh * @scrubbed: Whether the cotable has been scrubbed.
53d350ecf5Sriastradh * @resource_list: List of resources in the cotable.
54d350ecf5Sriastradh */
55d350ecf5Sriastradh struct vmw_cotable {
56d350ecf5Sriastradh struct vmw_resource res;
57d350ecf5Sriastradh struct vmw_resource *ctx;
58d350ecf5Sriastradh size_t size_read_back;
59d350ecf5Sriastradh int seen_entries;
60d350ecf5Sriastradh u32 type;
61d350ecf5Sriastradh bool scrubbed;
62d350ecf5Sriastradh struct list_head resource_list;
63d350ecf5Sriastradh };
64d350ecf5Sriastradh
65d350ecf5Sriastradh /**
66d350ecf5Sriastradh * struct vmw_cotable_info - Static info about cotable types
67d350ecf5Sriastradh *
68d350ecf5Sriastradh * @min_initial_entries: Min number of initial intries at cotable allocation
69d350ecf5Sriastradh * for this cotable type.
70d350ecf5Sriastradh * @size: Size of each entry.
71d350ecf5Sriastradh */
72d350ecf5Sriastradh struct vmw_cotable_info {
73d350ecf5Sriastradh u32 min_initial_entries;
74d350ecf5Sriastradh u32 size;
75d350ecf5Sriastradh void (*unbind_func)(struct vmw_private *, struct list_head *,
76d350ecf5Sriastradh bool);
77d350ecf5Sriastradh };
78d350ecf5Sriastradh
79d350ecf5Sriastradh static const struct vmw_cotable_info co_info[] = {
80d350ecf5Sriastradh {1, sizeof(SVGACOTableDXRTViewEntry), &vmw_view_cotable_list_destroy},
81d350ecf5Sriastradh {1, sizeof(SVGACOTableDXDSViewEntry), &vmw_view_cotable_list_destroy},
82d350ecf5Sriastradh {1, sizeof(SVGACOTableDXSRViewEntry), &vmw_view_cotable_list_destroy},
83d350ecf5Sriastradh {1, sizeof(SVGACOTableDXElementLayoutEntry), NULL},
84d350ecf5Sriastradh {1, sizeof(SVGACOTableDXBlendStateEntry), NULL},
85d350ecf5Sriastradh {1, sizeof(SVGACOTableDXDepthStencilEntry), NULL},
86d350ecf5Sriastradh {1, sizeof(SVGACOTableDXRasterizerStateEntry), NULL},
87d350ecf5Sriastradh {1, sizeof(SVGACOTableDXSamplerEntry), NULL},
88d350ecf5Sriastradh {1, sizeof(SVGACOTableDXStreamOutputEntry), NULL},
89d350ecf5Sriastradh {1, sizeof(SVGACOTableDXQueryEntry), NULL},
90d350ecf5Sriastradh {1, sizeof(SVGACOTableDXShaderEntry), &vmw_dx_shader_cotable_list_scrub}
91d350ecf5Sriastradh };
92d350ecf5Sriastradh
93d350ecf5Sriastradh /*
94d350ecf5Sriastradh * Cotables with bindings that we remove must be scrubbed first,
95d350ecf5Sriastradh * otherwise, the device will swap in an invalid context when we remove
96d350ecf5Sriastradh * bindings before scrubbing a cotable...
97d350ecf5Sriastradh */
98d350ecf5Sriastradh const SVGACOTableType vmw_cotable_scrub_order[] = {
99d350ecf5Sriastradh SVGA_COTABLE_RTVIEW,
100d350ecf5Sriastradh SVGA_COTABLE_DSVIEW,
101d350ecf5Sriastradh SVGA_COTABLE_SRVIEW,
102d350ecf5Sriastradh SVGA_COTABLE_DXSHADER,
103d350ecf5Sriastradh SVGA_COTABLE_ELEMENTLAYOUT,
104d350ecf5Sriastradh SVGA_COTABLE_BLENDSTATE,
105d350ecf5Sriastradh SVGA_COTABLE_DEPTHSTENCIL,
106d350ecf5Sriastradh SVGA_COTABLE_RASTERIZERSTATE,
107d350ecf5Sriastradh SVGA_COTABLE_SAMPLER,
108d350ecf5Sriastradh SVGA_COTABLE_STREAMOUTPUT,
109d350ecf5Sriastradh SVGA_COTABLE_DXQUERY,
110d350ecf5Sriastradh };
111d350ecf5Sriastradh
112d350ecf5Sriastradh static int vmw_cotable_bind(struct vmw_resource *res,
113d350ecf5Sriastradh struct ttm_validate_buffer *val_buf);
114d350ecf5Sriastradh static int vmw_cotable_unbind(struct vmw_resource *res,
115d350ecf5Sriastradh bool readback,
116d350ecf5Sriastradh struct ttm_validate_buffer *val_buf);
117d350ecf5Sriastradh static int vmw_cotable_create(struct vmw_resource *res);
118d350ecf5Sriastradh static int vmw_cotable_destroy(struct vmw_resource *res);
119d350ecf5Sriastradh
120d350ecf5Sriastradh static const struct vmw_res_func vmw_cotable_func = {
121d350ecf5Sriastradh .res_type = vmw_res_cotable,
122d350ecf5Sriastradh .needs_backup = true,
123d350ecf5Sriastradh .may_evict = true,
124*677dec6eSriastradh .prio = 3,
125*677dec6eSriastradh .dirty_prio = 3,
126d350ecf5Sriastradh .type_name = "context guest backed object tables",
127d350ecf5Sriastradh .backup_placement = &vmw_mob_placement,
128d350ecf5Sriastradh .create = vmw_cotable_create,
129d350ecf5Sriastradh .destroy = vmw_cotable_destroy,
130d350ecf5Sriastradh .bind = vmw_cotable_bind,
131d350ecf5Sriastradh .unbind = vmw_cotable_unbind,
132d350ecf5Sriastradh };
133d350ecf5Sriastradh
134d350ecf5Sriastradh /**
135d350ecf5Sriastradh * vmw_cotable - Convert a struct vmw_resource pointer to a struct
136d350ecf5Sriastradh * vmw_cotable pointer
137d350ecf5Sriastradh *
138d350ecf5Sriastradh * @res: Pointer to the resource.
139d350ecf5Sriastradh */
vmw_cotable(struct vmw_resource * res)140d350ecf5Sriastradh static struct vmw_cotable *vmw_cotable(struct vmw_resource *res)
141d350ecf5Sriastradh {
142d350ecf5Sriastradh return container_of(res, struct vmw_cotable, res);
143d350ecf5Sriastradh }
144d350ecf5Sriastradh
145d350ecf5Sriastradh /**
146d350ecf5Sriastradh * vmw_cotable_destroy - Cotable resource destroy callback
147d350ecf5Sriastradh *
148d350ecf5Sriastradh * @res: Pointer to the cotable resource.
149d350ecf5Sriastradh *
150d350ecf5Sriastradh * There is no device cotable destroy command, so this function only
151d350ecf5Sriastradh * makes sure that the resource id is set to invalid.
152d350ecf5Sriastradh */
vmw_cotable_destroy(struct vmw_resource * res)153d350ecf5Sriastradh static int vmw_cotable_destroy(struct vmw_resource *res)
154d350ecf5Sriastradh {
155d350ecf5Sriastradh res->id = -1;
156d350ecf5Sriastradh return 0;
157d350ecf5Sriastradh }
158d350ecf5Sriastradh
159d350ecf5Sriastradh /**
160d350ecf5Sriastradh * vmw_cotable_unscrub - Undo a cotable unscrub operation
161d350ecf5Sriastradh *
162d350ecf5Sriastradh * @res: Pointer to the cotable resource
163d350ecf5Sriastradh *
164d350ecf5Sriastradh * This function issues commands to (re)bind the cotable to
165d350ecf5Sriastradh * its backing mob, which needs to be validated and reserved at this point.
166d350ecf5Sriastradh * This is identical to bind() except the function interface looks different.
167d350ecf5Sriastradh */
vmw_cotable_unscrub(struct vmw_resource * res)168d350ecf5Sriastradh static int vmw_cotable_unscrub(struct vmw_resource *res)
169d350ecf5Sriastradh {
170d350ecf5Sriastradh struct vmw_cotable *vcotbl = vmw_cotable(res);
171d350ecf5Sriastradh struct vmw_private *dev_priv = res->dev_priv;
172d350ecf5Sriastradh struct ttm_buffer_object *bo = &res->backup->base;
173d350ecf5Sriastradh struct {
174d350ecf5Sriastradh SVGA3dCmdHeader header;
175d350ecf5Sriastradh SVGA3dCmdDXSetCOTable body;
176d350ecf5Sriastradh } *cmd;
177d350ecf5Sriastradh
178d350ecf5Sriastradh WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
179*677dec6eSriastradh dma_resv_assert_held(bo->base.resv);
180d350ecf5Sriastradh
181*677dec6eSriastradh cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
182*677dec6eSriastradh if (!cmd)
183d350ecf5Sriastradh return -ENOMEM;
184d350ecf5Sriastradh
185d350ecf5Sriastradh WARN_ON(vcotbl->ctx->id == SVGA3D_INVALID_ID);
186d350ecf5Sriastradh WARN_ON(bo->mem.mem_type != VMW_PL_MOB);
187d350ecf5Sriastradh cmd->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
188d350ecf5Sriastradh cmd->header.size = sizeof(cmd->body);
189d350ecf5Sriastradh cmd->body.cid = vcotbl->ctx->id;
190d350ecf5Sriastradh cmd->body.type = vcotbl->type;
191d350ecf5Sriastradh cmd->body.mobid = bo->mem.start;
192d350ecf5Sriastradh cmd->body.validSizeInBytes = vcotbl->size_read_back;
193d350ecf5Sriastradh
194d350ecf5Sriastradh vmw_fifo_commit_flush(dev_priv, sizeof(*cmd));
195d350ecf5Sriastradh vcotbl->scrubbed = false;
196d350ecf5Sriastradh
197d350ecf5Sriastradh return 0;
198d350ecf5Sriastradh }
199d350ecf5Sriastradh
200d350ecf5Sriastradh /**
201d350ecf5Sriastradh * vmw_cotable_bind - Undo a cotable unscrub operation
202d350ecf5Sriastradh *
203d350ecf5Sriastradh * @res: Pointer to the cotable resource
204d350ecf5Sriastradh * @val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
205d350ecf5Sriastradh * for convenience / fencing.
206d350ecf5Sriastradh *
207d350ecf5Sriastradh * This function issues commands to (re)bind the cotable to
208d350ecf5Sriastradh * its backing mob, which needs to be validated and reserved at this point.
209d350ecf5Sriastradh */
vmw_cotable_bind(struct vmw_resource * res,struct ttm_validate_buffer * val_buf)210d350ecf5Sriastradh static int vmw_cotable_bind(struct vmw_resource *res,
211d350ecf5Sriastradh struct ttm_validate_buffer *val_buf)
212d350ecf5Sriastradh {
213d350ecf5Sriastradh /*
214d350ecf5Sriastradh * The create() callback may have changed @res->backup without
215d350ecf5Sriastradh * the caller noticing, and with val_buf->bo still pointing to
216d350ecf5Sriastradh * the old backup buffer. Although hackish, and not used currently,
217d350ecf5Sriastradh * take the opportunity to correct the value here so that it's not
218d350ecf5Sriastradh * misused in the future.
219d350ecf5Sriastradh */
220d350ecf5Sriastradh val_buf->bo = &res->backup->base;
221d350ecf5Sriastradh
222d350ecf5Sriastradh return vmw_cotable_unscrub(res);
223d350ecf5Sriastradh }
224d350ecf5Sriastradh
225d350ecf5Sriastradh /**
226d350ecf5Sriastradh * vmw_cotable_scrub - Scrub the cotable from the device.
227d350ecf5Sriastradh *
228d350ecf5Sriastradh * @res: Pointer to the cotable resource.
229d350ecf5Sriastradh * @readback: Whether initiate a readback of the cotable data to the backup
230d350ecf5Sriastradh * buffer.
231d350ecf5Sriastradh *
232d350ecf5Sriastradh * In some situations (context swapouts) it might be desirable to make the
233d350ecf5Sriastradh * device forget about the cotable without performing a full unbind. A full
234d350ecf5Sriastradh * unbind requires reserved backup buffers and it might not be possible to
235d350ecf5Sriastradh * reserve them due to locking order violation issues. The vmw_cotable_scrub
236d350ecf5Sriastradh * function implements a partial unbind() without that requirement but with the
237d350ecf5Sriastradh * following restrictions.
238d350ecf5Sriastradh * 1) Before the cotable is again used by the GPU, vmw_cotable_unscrub() must
239d350ecf5Sriastradh * be called.
240d350ecf5Sriastradh * 2) Before the cotable backing buffer is used by the CPU, or during the
241d350ecf5Sriastradh * resource destruction, vmw_cotable_unbind() must be called.
242d350ecf5Sriastradh */
vmw_cotable_scrub(struct vmw_resource * res,bool readback)243d350ecf5Sriastradh int vmw_cotable_scrub(struct vmw_resource *res, bool readback)
244d350ecf5Sriastradh {
245d350ecf5Sriastradh struct vmw_cotable *vcotbl = vmw_cotable(res);
246d350ecf5Sriastradh struct vmw_private *dev_priv = res->dev_priv;
247d350ecf5Sriastradh size_t submit_size;
248d350ecf5Sriastradh
249d350ecf5Sriastradh struct {
250d350ecf5Sriastradh SVGA3dCmdHeader header;
251d350ecf5Sriastradh SVGA3dCmdDXReadbackCOTable body;
252d350ecf5Sriastradh } *cmd0;
253d350ecf5Sriastradh struct {
254d350ecf5Sriastradh SVGA3dCmdHeader header;
255d350ecf5Sriastradh SVGA3dCmdDXSetCOTable body;
256d350ecf5Sriastradh } *cmd1;
257d350ecf5Sriastradh
258d350ecf5Sriastradh if (vcotbl->scrubbed)
259d350ecf5Sriastradh return 0;
260d350ecf5Sriastradh
261d350ecf5Sriastradh if (co_info[vcotbl->type].unbind_func)
262d350ecf5Sriastradh co_info[vcotbl->type].unbind_func(dev_priv,
263d350ecf5Sriastradh &vcotbl->resource_list,
264d350ecf5Sriastradh readback);
265d350ecf5Sriastradh submit_size = sizeof(*cmd1);
266d350ecf5Sriastradh if (readback)
267d350ecf5Sriastradh submit_size += sizeof(*cmd0);
268d350ecf5Sriastradh
269*677dec6eSriastradh cmd1 = VMW_FIFO_RESERVE(dev_priv, submit_size);
270*677dec6eSriastradh if (!cmd1)
271d350ecf5Sriastradh return -ENOMEM;
272d350ecf5Sriastradh
273d350ecf5Sriastradh vcotbl->size_read_back = 0;
274d350ecf5Sriastradh if (readback) {
275d350ecf5Sriastradh cmd0 = (void *) cmd1;
276d350ecf5Sriastradh cmd0->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
277d350ecf5Sriastradh cmd0->header.size = sizeof(cmd0->body);
278d350ecf5Sriastradh cmd0->body.cid = vcotbl->ctx->id;
279d350ecf5Sriastradh cmd0->body.type = vcotbl->type;
280d350ecf5Sriastradh cmd1 = (void *) &cmd0[1];
281d350ecf5Sriastradh vcotbl->size_read_back = res->backup_size;
282d350ecf5Sriastradh }
283d350ecf5Sriastradh cmd1->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
284d350ecf5Sriastradh cmd1->header.size = sizeof(cmd1->body);
285d350ecf5Sriastradh cmd1->body.cid = vcotbl->ctx->id;
286d350ecf5Sriastradh cmd1->body.type = vcotbl->type;
287d350ecf5Sriastradh cmd1->body.mobid = SVGA3D_INVALID_ID;
288d350ecf5Sriastradh cmd1->body.validSizeInBytes = 0;
289d350ecf5Sriastradh vmw_fifo_commit_flush(dev_priv, submit_size);
290d350ecf5Sriastradh vcotbl->scrubbed = true;
291d350ecf5Sriastradh
292d350ecf5Sriastradh /* Trigger a create() on next validate. */
293d350ecf5Sriastradh res->id = -1;
294d350ecf5Sriastradh
295d350ecf5Sriastradh return 0;
296d350ecf5Sriastradh }
297d350ecf5Sriastradh
298d350ecf5Sriastradh /**
299d350ecf5Sriastradh * vmw_cotable_unbind - Cotable resource unbind callback
300d350ecf5Sriastradh *
301d350ecf5Sriastradh * @res: Pointer to the cotable resource.
302d350ecf5Sriastradh * @readback: Whether to read back cotable data to the backup buffer.
303d350ecf5Sriastradh * val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
304d350ecf5Sriastradh * for convenience / fencing.
305d350ecf5Sriastradh *
306d350ecf5Sriastradh * Unbinds the cotable from the device and fences the backup buffer.
307d350ecf5Sriastradh */
vmw_cotable_unbind(struct vmw_resource * res,bool readback,struct ttm_validate_buffer * val_buf)308d350ecf5Sriastradh static int vmw_cotable_unbind(struct vmw_resource *res,
309d350ecf5Sriastradh bool readback,
310d350ecf5Sriastradh struct ttm_validate_buffer *val_buf)
311d350ecf5Sriastradh {
312d350ecf5Sriastradh struct vmw_cotable *vcotbl = vmw_cotable(res);
313d350ecf5Sriastradh struct vmw_private *dev_priv = res->dev_priv;
314d350ecf5Sriastradh struct ttm_buffer_object *bo = val_buf->bo;
315d350ecf5Sriastradh struct vmw_fence_obj *fence;
316d350ecf5Sriastradh
317*677dec6eSriastradh if (!vmw_resource_mob_attached(res))
318d350ecf5Sriastradh return 0;
319d350ecf5Sriastradh
320d350ecf5Sriastradh WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
321*677dec6eSriastradh dma_resv_assert_held(bo->base.resv);
322d350ecf5Sriastradh
323d350ecf5Sriastradh mutex_lock(&dev_priv->binding_mutex);
324d350ecf5Sriastradh if (!vcotbl->scrubbed)
325d350ecf5Sriastradh vmw_dx_context_scrub_cotables(vcotbl->ctx, readback);
326d350ecf5Sriastradh mutex_unlock(&dev_priv->binding_mutex);
327d350ecf5Sriastradh (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
328*677dec6eSriastradh vmw_bo_fence_single(bo, fence);
329d350ecf5Sriastradh if (likely(fence != NULL))
330d350ecf5Sriastradh vmw_fence_obj_unreference(&fence);
331d350ecf5Sriastradh
332d350ecf5Sriastradh return 0;
333d350ecf5Sriastradh }
334d350ecf5Sriastradh
335d350ecf5Sriastradh /**
336d350ecf5Sriastradh * vmw_cotable_readback - Read back a cotable without unbinding.
337d350ecf5Sriastradh *
338d350ecf5Sriastradh * @res: The cotable resource.
339d350ecf5Sriastradh *
340d350ecf5Sriastradh * Reads back a cotable to its backing mob without scrubbing the MOB from
341d350ecf5Sriastradh * the cotable. The MOB is fenced for subsequent CPU access.
342d350ecf5Sriastradh */
vmw_cotable_readback(struct vmw_resource * res)343d350ecf5Sriastradh static int vmw_cotable_readback(struct vmw_resource *res)
344d350ecf5Sriastradh {
345d350ecf5Sriastradh struct vmw_cotable *vcotbl = vmw_cotable(res);
346d350ecf5Sriastradh struct vmw_private *dev_priv = res->dev_priv;
347d350ecf5Sriastradh
348d350ecf5Sriastradh struct {
349d350ecf5Sriastradh SVGA3dCmdHeader header;
350d350ecf5Sriastradh SVGA3dCmdDXReadbackCOTable body;
351d350ecf5Sriastradh } *cmd;
352d350ecf5Sriastradh struct vmw_fence_obj *fence;
353d350ecf5Sriastradh
354d350ecf5Sriastradh if (!vcotbl->scrubbed) {
355*677dec6eSriastradh cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
356*677dec6eSriastradh if (!cmd)
357d350ecf5Sriastradh return -ENOMEM;
358*677dec6eSriastradh
359d350ecf5Sriastradh cmd->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
360d350ecf5Sriastradh cmd->header.size = sizeof(cmd->body);
361d350ecf5Sriastradh cmd->body.cid = vcotbl->ctx->id;
362d350ecf5Sriastradh cmd->body.type = vcotbl->type;
363d350ecf5Sriastradh vcotbl->size_read_back = res->backup_size;
364d350ecf5Sriastradh vmw_fifo_commit(dev_priv, sizeof(*cmd));
365d350ecf5Sriastradh }
366d350ecf5Sriastradh
367d350ecf5Sriastradh (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
368*677dec6eSriastradh vmw_bo_fence_single(&res->backup->base, fence);
369d350ecf5Sriastradh vmw_fence_obj_unreference(&fence);
370d350ecf5Sriastradh
371d350ecf5Sriastradh return 0;
372d350ecf5Sriastradh }
373d350ecf5Sriastradh
374d350ecf5Sriastradh /**
375d350ecf5Sriastradh * vmw_cotable_resize - Resize a cotable.
376d350ecf5Sriastradh *
377d350ecf5Sriastradh * @res: The cotable resource.
378d350ecf5Sriastradh * @new_size: The new size.
379d350ecf5Sriastradh *
380d350ecf5Sriastradh * Resizes a cotable and binds the new backup buffer.
381d350ecf5Sriastradh * On failure the cotable is left intact.
382d350ecf5Sriastradh * Important! This function may not fail once the MOB switch has been
383d350ecf5Sriastradh * committed to hardware. That would put the device context in an
384d350ecf5Sriastradh * invalid state which we can't currently recover from.
385d350ecf5Sriastradh */
vmw_cotable_resize(struct vmw_resource * res,size_t new_size)386d350ecf5Sriastradh static int vmw_cotable_resize(struct vmw_resource *res, size_t new_size)
387d350ecf5Sriastradh {
388*677dec6eSriastradh struct ttm_operation_ctx ctx = { false, false };
389d350ecf5Sriastradh struct vmw_private *dev_priv = res->dev_priv;
390d350ecf5Sriastradh struct vmw_cotable *vcotbl = vmw_cotable(res);
391*677dec6eSriastradh struct vmw_buffer_object *buf, *old_buf = res->backup;
392d350ecf5Sriastradh struct ttm_buffer_object *bo, *old_bo = &res->backup->base;
393d350ecf5Sriastradh size_t old_size = res->backup_size;
394d350ecf5Sriastradh size_t old_size_read_back = vcotbl->size_read_back;
395d350ecf5Sriastradh size_t cur_size_read_back;
396d350ecf5Sriastradh struct ttm_bo_kmap_obj old_map, new_map;
397d350ecf5Sriastradh int ret;
398d350ecf5Sriastradh size_t i;
399d350ecf5Sriastradh
400d350ecf5Sriastradh ret = vmw_cotable_readback(res);
401d350ecf5Sriastradh if (ret)
402d350ecf5Sriastradh return ret;
403d350ecf5Sriastradh
404d350ecf5Sriastradh cur_size_read_back = vcotbl->size_read_back;
405d350ecf5Sriastradh vcotbl->size_read_back = old_size_read_back;
406d350ecf5Sriastradh
407d350ecf5Sriastradh /*
408d350ecf5Sriastradh * While device is processing, Allocate and reserve a buffer object
409d350ecf5Sriastradh * for the new COTable. Initially pin the buffer object to make sure
410d350ecf5Sriastradh * we can use tryreserve without failure.
411d350ecf5Sriastradh */
412d350ecf5Sriastradh buf = kzalloc(sizeof(*buf), GFP_KERNEL);
413d350ecf5Sriastradh if (!buf)
414d350ecf5Sriastradh return -ENOMEM;
415d350ecf5Sriastradh
416*677dec6eSriastradh ret = vmw_bo_init(dev_priv, buf, new_size, &vmw_mob_ne_placement,
417*677dec6eSriastradh true, vmw_bo_bo_free);
418d350ecf5Sriastradh if (ret) {
419d350ecf5Sriastradh DRM_ERROR("Failed initializing new cotable MOB.\n");
420d350ecf5Sriastradh return ret;
421d350ecf5Sriastradh }
422d350ecf5Sriastradh
423d350ecf5Sriastradh bo = &buf->base;
424*677dec6eSriastradh WARN_ON_ONCE(ttm_bo_reserve(bo, false, true, NULL));
425d350ecf5Sriastradh
426*677dec6eSriastradh ret = ttm_bo_wait(old_bo, false, false);
427d350ecf5Sriastradh if (unlikely(ret != 0)) {
428d350ecf5Sriastradh DRM_ERROR("Failed waiting for cotable unbind.\n");
429d350ecf5Sriastradh goto out_wait;
430d350ecf5Sriastradh }
431d350ecf5Sriastradh
432d350ecf5Sriastradh /*
433d350ecf5Sriastradh * Do a page by page copy of COTables. This eliminates slow vmap()s.
434d350ecf5Sriastradh * This should really be a TTM utility.
435d350ecf5Sriastradh */
436d350ecf5Sriastradh for (i = 0; i < old_bo->num_pages; ++i) {
437d350ecf5Sriastradh bool dummy;
438d350ecf5Sriastradh
439d350ecf5Sriastradh ret = ttm_bo_kmap(old_bo, i, 1, &old_map);
440d350ecf5Sriastradh if (unlikely(ret != 0)) {
441d350ecf5Sriastradh DRM_ERROR("Failed mapping old COTable on resize.\n");
442d350ecf5Sriastradh goto out_wait;
443d350ecf5Sriastradh }
444d350ecf5Sriastradh ret = ttm_bo_kmap(bo, i, 1, &new_map);
445d350ecf5Sriastradh if (unlikely(ret != 0)) {
446d350ecf5Sriastradh DRM_ERROR("Failed mapping new COTable on resize.\n");
447d350ecf5Sriastradh goto out_map_new;
448d350ecf5Sriastradh }
449d350ecf5Sriastradh memcpy(ttm_kmap_obj_virtual(&new_map, &dummy),
450d350ecf5Sriastradh ttm_kmap_obj_virtual(&old_map, &dummy),
451d350ecf5Sriastradh PAGE_SIZE);
452d350ecf5Sriastradh ttm_bo_kunmap(&new_map);
453d350ecf5Sriastradh ttm_bo_kunmap(&old_map);
454d350ecf5Sriastradh }
455d350ecf5Sriastradh
456d350ecf5Sriastradh /* Unpin new buffer, and switch backup buffers. */
457*677dec6eSriastradh ret = ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
458d350ecf5Sriastradh if (unlikely(ret != 0)) {
459d350ecf5Sriastradh DRM_ERROR("Failed validating new COTable backup buffer.\n");
460d350ecf5Sriastradh goto out_wait;
461d350ecf5Sriastradh }
462d350ecf5Sriastradh
463*677dec6eSriastradh vmw_resource_mob_detach(res);
464d350ecf5Sriastradh res->backup = buf;
465d350ecf5Sriastradh res->backup_size = new_size;
466d350ecf5Sriastradh vcotbl->size_read_back = cur_size_read_back;
467d350ecf5Sriastradh
468d350ecf5Sriastradh /*
469d350ecf5Sriastradh * Now tell the device to switch. If this fails, then we need to
470d350ecf5Sriastradh * revert the full resize.
471d350ecf5Sriastradh */
472d350ecf5Sriastradh ret = vmw_cotable_unscrub(res);
473d350ecf5Sriastradh if (ret) {
474d350ecf5Sriastradh DRM_ERROR("Failed switching COTable backup buffer.\n");
475d350ecf5Sriastradh res->backup = old_buf;
476d350ecf5Sriastradh res->backup_size = old_size;
477d350ecf5Sriastradh vcotbl->size_read_back = old_size_read_back;
478*677dec6eSriastradh vmw_resource_mob_attach(res);
479d350ecf5Sriastradh goto out_wait;
480d350ecf5Sriastradh }
481d350ecf5Sriastradh
482*677dec6eSriastradh vmw_resource_mob_attach(res);
483d350ecf5Sriastradh /* Let go of the old mob. */
484*677dec6eSriastradh vmw_bo_unreference(&old_buf);
485d350ecf5Sriastradh res->id = vcotbl->type;
486d350ecf5Sriastradh
487d350ecf5Sriastradh return 0;
488d350ecf5Sriastradh
489d350ecf5Sriastradh out_map_new:
490d350ecf5Sriastradh ttm_bo_kunmap(&old_map);
491d350ecf5Sriastradh out_wait:
492d350ecf5Sriastradh ttm_bo_unreserve(bo);
493*677dec6eSriastradh vmw_bo_unreference(&buf);
494d350ecf5Sriastradh
495d350ecf5Sriastradh return ret;
496d350ecf5Sriastradh }
497d350ecf5Sriastradh
498d350ecf5Sriastradh /**
499d350ecf5Sriastradh * vmw_cotable_create - Cotable resource create callback
500d350ecf5Sriastradh *
501d350ecf5Sriastradh * @res: Pointer to a cotable resource.
502d350ecf5Sriastradh *
503d350ecf5Sriastradh * There is no separate create command for cotables, so this callback, which
504d350ecf5Sriastradh * is called before bind() in the validation sequence is instead used for two
505d350ecf5Sriastradh * things.
506d350ecf5Sriastradh * 1) Unscrub the cotable if it is scrubbed and still attached to a backup
507*677dec6eSriastradh * buffer.
508d350ecf5Sriastradh * 2) Resize the cotable if needed.
509d350ecf5Sriastradh */
vmw_cotable_create(struct vmw_resource * res)510d350ecf5Sriastradh static int vmw_cotable_create(struct vmw_resource *res)
511d350ecf5Sriastradh {
512d350ecf5Sriastradh struct vmw_cotable *vcotbl = vmw_cotable(res);
513d350ecf5Sriastradh size_t new_size = res->backup_size;
514d350ecf5Sriastradh size_t needed_size;
515d350ecf5Sriastradh int ret;
516d350ecf5Sriastradh
517d350ecf5Sriastradh /* Check whether we need to resize the cotable */
518d350ecf5Sriastradh needed_size = (vcotbl->seen_entries + 1) * co_info[vcotbl->type].size;
519d350ecf5Sriastradh while (needed_size > new_size)
520d350ecf5Sriastradh new_size *= 2;
521d350ecf5Sriastradh
522d350ecf5Sriastradh if (likely(new_size <= res->backup_size)) {
523*677dec6eSriastradh if (vcotbl->scrubbed && vmw_resource_mob_attached(res)) {
524d350ecf5Sriastradh ret = vmw_cotable_unscrub(res);
525d350ecf5Sriastradh if (ret)
526d350ecf5Sriastradh return ret;
527d350ecf5Sriastradh }
528d350ecf5Sriastradh res->id = vcotbl->type;
529d350ecf5Sriastradh return 0;
530d350ecf5Sriastradh }
531d350ecf5Sriastradh
532d350ecf5Sriastradh return vmw_cotable_resize(res, new_size);
533d350ecf5Sriastradh }
534d350ecf5Sriastradh
535d350ecf5Sriastradh /**
536d350ecf5Sriastradh * vmw_hw_cotable_destroy - Cotable hw_destroy callback
537d350ecf5Sriastradh *
538d350ecf5Sriastradh * @res: Pointer to a cotable resource.
539d350ecf5Sriastradh *
540d350ecf5Sriastradh * The final (part of resource destruction) destroy callback.
541d350ecf5Sriastradh */
vmw_hw_cotable_destroy(struct vmw_resource * res)542d350ecf5Sriastradh static void vmw_hw_cotable_destroy(struct vmw_resource *res)
543d350ecf5Sriastradh {
544d350ecf5Sriastradh (void) vmw_cotable_destroy(res);
545d350ecf5Sriastradh }
546d350ecf5Sriastradh
547d350ecf5Sriastradh static size_t cotable_acc_size;
548d350ecf5Sriastradh
549d350ecf5Sriastradh /**
550d350ecf5Sriastradh * vmw_cotable_free - Cotable resource destructor
551d350ecf5Sriastradh *
552d350ecf5Sriastradh * @res: Pointer to a cotable resource.
553d350ecf5Sriastradh */
vmw_cotable_free(struct vmw_resource * res)554d350ecf5Sriastradh static void vmw_cotable_free(struct vmw_resource *res)
555d350ecf5Sriastradh {
556d350ecf5Sriastradh struct vmw_private *dev_priv = res->dev_priv;
557d350ecf5Sriastradh
558d350ecf5Sriastradh kfree(res);
559d350ecf5Sriastradh ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
560d350ecf5Sriastradh }
561d350ecf5Sriastradh
562d350ecf5Sriastradh /**
563d350ecf5Sriastradh * vmw_cotable_alloc - Create a cotable resource
564d350ecf5Sriastradh *
565d350ecf5Sriastradh * @dev_priv: Pointer to a device private struct.
566d350ecf5Sriastradh * @ctx: Pointer to the context resource.
567d350ecf5Sriastradh * The cotable resource will not add a refcount.
568d350ecf5Sriastradh * @type: The cotable type.
569d350ecf5Sriastradh */
vmw_cotable_alloc(struct vmw_private * dev_priv,struct vmw_resource * ctx,u32 type)570d350ecf5Sriastradh struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
571d350ecf5Sriastradh struct vmw_resource *ctx,
572d350ecf5Sriastradh u32 type)
573d350ecf5Sriastradh {
574d350ecf5Sriastradh struct vmw_cotable *vcotbl;
575*677dec6eSriastradh struct ttm_operation_ctx ttm_opt_ctx = {
576*677dec6eSriastradh .interruptible = true,
577*677dec6eSriastradh .no_wait_gpu = false
578*677dec6eSriastradh };
579d350ecf5Sriastradh int ret;
580d350ecf5Sriastradh u32 num_entries;
581d350ecf5Sriastradh
582d350ecf5Sriastradh if (unlikely(cotable_acc_size == 0))
583d350ecf5Sriastradh cotable_acc_size = ttm_round_pot(sizeof(struct vmw_cotable));
584d350ecf5Sriastradh
585d350ecf5Sriastradh ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
586*677dec6eSriastradh cotable_acc_size, &ttm_opt_ctx);
587d350ecf5Sriastradh if (unlikely(ret))
588d350ecf5Sriastradh return ERR_PTR(ret);
589d350ecf5Sriastradh
590d350ecf5Sriastradh vcotbl = kzalloc(sizeof(*vcotbl), GFP_KERNEL);
591*677dec6eSriastradh if (unlikely(!vcotbl)) {
592d350ecf5Sriastradh ret = -ENOMEM;
593d350ecf5Sriastradh goto out_no_alloc;
594d350ecf5Sriastradh }
595d350ecf5Sriastradh
596d350ecf5Sriastradh ret = vmw_resource_init(dev_priv, &vcotbl->res, true,
597d350ecf5Sriastradh vmw_cotable_free, &vmw_cotable_func);
598d350ecf5Sriastradh if (unlikely(ret != 0))
599d350ecf5Sriastradh goto out_no_init;
600d350ecf5Sriastradh
601d350ecf5Sriastradh INIT_LIST_HEAD(&vcotbl->resource_list);
602d350ecf5Sriastradh vcotbl->res.id = type;
603d350ecf5Sriastradh vcotbl->res.backup_size = PAGE_SIZE;
604d350ecf5Sriastradh num_entries = PAGE_SIZE / co_info[type].size;
605d350ecf5Sriastradh if (num_entries < co_info[type].min_initial_entries) {
606d350ecf5Sriastradh vcotbl->res.backup_size = co_info[type].min_initial_entries *
607d350ecf5Sriastradh co_info[type].size;
608d350ecf5Sriastradh vcotbl->res.backup_size =
609d350ecf5Sriastradh (vcotbl->res.backup_size + PAGE_SIZE - 1) & PAGE_MASK;
610d350ecf5Sriastradh }
611d350ecf5Sriastradh
612d350ecf5Sriastradh vcotbl->scrubbed = true;
613d350ecf5Sriastradh vcotbl->seen_entries = -1;
614d350ecf5Sriastradh vcotbl->type = type;
615d350ecf5Sriastradh vcotbl->ctx = ctx;
616d350ecf5Sriastradh
617*677dec6eSriastradh vcotbl->res.hw_destroy = vmw_hw_cotable_destroy;
618d350ecf5Sriastradh
619d350ecf5Sriastradh return &vcotbl->res;
620d350ecf5Sriastradh
621d350ecf5Sriastradh out_no_init:
622d350ecf5Sriastradh kfree(vcotbl);
623d350ecf5Sriastradh out_no_alloc:
624d350ecf5Sriastradh ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
625d350ecf5Sriastradh return ERR_PTR(ret);
626d350ecf5Sriastradh }
627d350ecf5Sriastradh
628d350ecf5Sriastradh /**
629d350ecf5Sriastradh * vmw_cotable_notify - Notify the cotable about an item creation
630d350ecf5Sriastradh *
631d350ecf5Sriastradh * @res: Pointer to a cotable resource.
632d350ecf5Sriastradh * @id: Item id.
633d350ecf5Sriastradh */
vmw_cotable_notify(struct vmw_resource * res,int id)634d350ecf5Sriastradh int vmw_cotable_notify(struct vmw_resource *res, int id)
635d350ecf5Sriastradh {
636d350ecf5Sriastradh struct vmw_cotable *vcotbl = vmw_cotable(res);
637d350ecf5Sriastradh
638d350ecf5Sriastradh if (id < 0 || id >= SVGA_COTABLE_MAX_IDS) {
639d350ecf5Sriastradh DRM_ERROR("Illegal COTable id. Type is %u. Id is %d\n",
640d350ecf5Sriastradh (unsigned) vcotbl->type, id);
641d350ecf5Sriastradh return -EINVAL;
642d350ecf5Sriastradh }
643d350ecf5Sriastradh
644d350ecf5Sriastradh if (vcotbl->seen_entries < id) {
645d350ecf5Sriastradh /* Trigger a call to create() on next validate */
646d350ecf5Sriastradh res->id = -1;
647d350ecf5Sriastradh vcotbl->seen_entries = id;
648d350ecf5Sriastradh }
649d350ecf5Sriastradh
650d350ecf5Sriastradh return 0;
651d350ecf5Sriastradh }
652d350ecf5Sriastradh
653d350ecf5Sriastradh /**
654d350ecf5Sriastradh * vmw_cotable_add_view - add a view to the cotable's list of active views.
655d350ecf5Sriastradh *
656d350ecf5Sriastradh * @res: pointer struct vmw_resource representing the cotable.
657d350ecf5Sriastradh * @head: pointer to the struct list_head member of the resource, dedicated
658d350ecf5Sriastradh * to the cotable active resource list.
659d350ecf5Sriastradh */
vmw_cotable_add_resource(struct vmw_resource * res,struct list_head * head)660d350ecf5Sriastradh void vmw_cotable_add_resource(struct vmw_resource *res, struct list_head *head)
661d350ecf5Sriastradh {
662d350ecf5Sriastradh struct vmw_cotable *vcotbl =
663d350ecf5Sriastradh container_of(res, struct vmw_cotable, res);
664d350ecf5Sriastradh
665d350ecf5Sriastradh list_add_tail(head, &vcotbl->resource_list);
666d350ecf5Sriastradh }
667