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