dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / vmwgfx / vmwgfx_context.c
blob14bd760a62fd7fda3053d9d244826984f6c7b5f7
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include <drm/ttm/ttm_placement.h>
30 #include "vmwgfx_drv.h"
31 #include "vmwgfx_resource_priv.h"
32 #include "vmwgfx_binding.h"
34 struct vmw_user_context {
35 struct ttm_base_object base;
36 struct vmw_resource res;
37 struct vmw_ctx_binding_state *cbs;
38 struct vmw_cmdbuf_res_manager *man;
39 struct vmw_resource *cotables[SVGA_COTABLE_DX10_MAX];
40 spinlock_t cotable_lock;
41 struct vmw_buffer_object *dx_query_mob;
44 static void vmw_user_context_free(struct vmw_resource *res);
45 static struct vmw_resource *
46 vmw_user_context_base_to_res(struct ttm_base_object *base);
48 static int vmw_gb_context_create(struct vmw_resource *res);
49 static int vmw_gb_context_bind(struct vmw_resource *res,
50 struct ttm_validate_buffer *val_buf);
51 static int vmw_gb_context_unbind(struct vmw_resource *res,
52 bool readback,
53 struct ttm_validate_buffer *val_buf);
54 static int vmw_gb_context_destroy(struct vmw_resource *res);
55 static int vmw_dx_context_create(struct vmw_resource *res);
56 static int vmw_dx_context_bind(struct vmw_resource *res,
57 struct ttm_validate_buffer *val_buf);
58 static int vmw_dx_context_unbind(struct vmw_resource *res,
59 bool readback,
60 struct ttm_validate_buffer *val_buf);
61 static int vmw_dx_context_destroy(struct vmw_resource *res);
63 static uint64_t vmw_user_context_size;
65 static const struct vmw_user_resource_conv user_context_conv = {
66 .object_type = VMW_RES_CONTEXT,
67 .base_obj_to_res = vmw_user_context_base_to_res,
68 .res_free = vmw_user_context_free
71 const struct vmw_user_resource_conv *user_context_converter =
72 &user_context_conv;
75 static const struct vmw_res_func vmw_legacy_context_func = {
76 .res_type = vmw_res_context,
77 .needs_backup = false,
78 .may_evict = false,
79 .type_name = "legacy contexts",
80 .backup_placement = NULL,
81 .create = NULL,
82 .destroy = NULL,
83 .bind = NULL,
84 .unbind = NULL
87 static const struct vmw_res_func vmw_gb_context_func = {
88 .res_type = vmw_res_context,
89 .needs_backup = true,
90 .may_evict = true,
91 .type_name = "guest backed contexts",
92 .backup_placement = &vmw_mob_placement,
93 .create = vmw_gb_context_create,
94 .destroy = vmw_gb_context_destroy,
95 .bind = vmw_gb_context_bind,
96 .unbind = vmw_gb_context_unbind
99 static const struct vmw_res_func vmw_dx_context_func = {
100 .res_type = vmw_res_dx_context,
101 .needs_backup = true,
102 .may_evict = true,
103 .type_name = "dx contexts",
104 .backup_placement = &vmw_mob_placement,
105 .create = vmw_dx_context_create,
106 .destroy = vmw_dx_context_destroy,
107 .bind = vmw_dx_context_bind,
108 .unbind = vmw_dx_context_unbind
112 * Context management:
115 static void vmw_context_cotables_unref(struct vmw_user_context *uctx)
117 struct vmw_resource *res;
118 int i;
120 for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) {
121 spin_lock(&uctx->cotable_lock);
122 res = uctx->cotables[i];
123 uctx->cotables[i] = NULL;
124 spin_unlock(&uctx->cotable_lock);
126 if (res)
127 vmw_resource_unreference(&res);
131 static void vmw_hw_context_destroy(struct vmw_resource *res)
133 struct vmw_user_context *uctx =
134 container_of(res, struct vmw_user_context, res);
135 struct vmw_private *dev_priv = res->dev_priv;
136 struct {
137 SVGA3dCmdHeader header;
138 SVGA3dCmdDestroyContext body;
139 } *cmd;
142 if (res->func->destroy == vmw_gb_context_destroy ||
143 res->func->destroy == vmw_dx_context_destroy) {
144 mutex_lock(&dev_priv->cmdbuf_mutex);
145 vmw_cmdbuf_res_man_destroy(uctx->man);
146 mutex_lock(&dev_priv->binding_mutex);
147 vmw_binding_state_kill(uctx->cbs);
148 (void) res->func->destroy(res);
149 mutex_unlock(&dev_priv->binding_mutex);
150 if (dev_priv->pinned_bo != NULL &&
151 !dev_priv->query_cid_valid)
152 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
153 mutex_unlock(&dev_priv->cmdbuf_mutex);
154 vmw_context_cotables_unref(uctx);
155 return;
158 vmw_execbuf_release_pinned_bo(dev_priv);
159 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
160 if (unlikely(cmd == NULL)) {
161 DRM_ERROR("Failed reserving FIFO space for surface "
162 "destruction.\n");
163 return;
166 cmd->header.id = SVGA_3D_CMD_CONTEXT_DESTROY;
167 cmd->header.size = sizeof(cmd->body);
168 cmd->body.cid = res->id;
170 vmw_fifo_commit(dev_priv, sizeof(*cmd));
171 vmw_fifo_resource_dec(dev_priv);
174 static int vmw_gb_context_init(struct vmw_private *dev_priv,
175 bool dx,
176 struct vmw_resource *res,
177 void (*res_free)(struct vmw_resource *res))
179 int ret, i;
180 struct vmw_user_context *uctx =
181 container_of(res, struct vmw_user_context, res);
183 res->backup_size = (dx ? sizeof(SVGADXContextMobFormat) :
184 SVGA3D_CONTEXT_DATA_SIZE);
185 ret = vmw_resource_init(dev_priv, res, true,
186 res_free,
187 dx ? &vmw_dx_context_func :
188 &vmw_gb_context_func);
189 if (unlikely(ret != 0))
190 goto out_err;
192 if (dev_priv->has_mob) {
193 uctx->man = vmw_cmdbuf_res_man_create(dev_priv);
194 if (IS_ERR(uctx->man)) {
195 ret = PTR_ERR(uctx->man);
196 uctx->man = NULL;
197 goto out_err;
201 uctx->cbs = vmw_binding_state_alloc(dev_priv);
202 if (IS_ERR(uctx->cbs)) {
203 ret = PTR_ERR(uctx->cbs);
204 goto out_err;
207 spin_lock_init(&uctx->cotable_lock);
209 if (dx) {
210 for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) {
211 uctx->cotables[i] = vmw_cotable_alloc(dev_priv,
212 &uctx->res, i);
213 if (unlikely(IS_ERR(uctx->cotables[i]))) {
214 ret = PTR_ERR(uctx->cotables[i]);
215 goto out_cotables;
220 res->hw_destroy = vmw_hw_context_destroy;
221 return 0;
223 out_cotables:
224 vmw_context_cotables_unref(uctx);
225 out_err:
226 if (res_free)
227 res_free(res);
228 else
229 kfree(res);
230 return ret;
233 static int vmw_context_init(struct vmw_private *dev_priv,
234 struct vmw_resource *res,
235 void (*res_free)(struct vmw_resource *res),
236 bool dx)
238 int ret;
240 struct {
241 SVGA3dCmdHeader header;
242 SVGA3dCmdDefineContext body;
243 } *cmd;
245 if (dev_priv->has_mob)
246 return vmw_gb_context_init(dev_priv, dx, res, res_free);
248 ret = vmw_resource_init(dev_priv, res, false,
249 res_free, &vmw_legacy_context_func);
251 if (unlikely(ret != 0)) {
252 DRM_ERROR("Failed to allocate a resource id.\n");
253 goto out_early;
256 if (unlikely(res->id >= SVGA3D_MAX_CONTEXT_IDS)) {
257 DRM_ERROR("Out of hw context ids.\n");
258 vmw_resource_unreference(&res);
259 return -ENOMEM;
262 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
263 if (unlikely(cmd == NULL)) {
264 DRM_ERROR("Fifo reserve failed.\n");
265 vmw_resource_unreference(&res);
266 return -ENOMEM;
269 cmd->header.id = SVGA_3D_CMD_CONTEXT_DEFINE;
270 cmd->header.size = sizeof(cmd->body);
271 cmd->body.cid = res->id;
273 vmw_fifo_commit(dev_priv, sizeof(*cmd));
274 vmw_fifo_resource_inc(dev_priv);
275 res->hw_destroy = vmw_hw_context_destroy;
276 return 0;
278 out_early:
279 if (res_free == NULL)
280 kfree(res);
281 else
282 res_free(res);
283 return ret;
288 * GB context.
291 static int vmw_gb_context_create(struct vmw_resource *res)
293 struct vmw_private *dev_priv = res->dev_priv;
294 int ret;
295 struct {
296 SVGA3dCmdHeader header;
297 SVGA3dCmdDefineGBContext body;
298 } *cmd;
300 if (likely(res->id != -1))
301 return 0;
303 ret = vmw_resource_alloc_id(res);
304 if (unlikely(ret != 0)) {
305 DRM_ERROR("Failed to allocate a context id.\n");
306 goto out_no_id;
309 if (unlikely(res->id >= VMWGFX_NUM_GB_CONTEXT)) {
310 ret = -EBUSY;
311 goto out_no_fifo;
314 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
315 if (unlikely(cmd == NULL)) {
316 DRM_ERROR("Failed reserving FIFO space for context "
317 "creation.\n");
318 ret = -ENOMEM;
319 goto out_no_fifo;
322 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_CONTEXT;
323 cmd->header.size = sizeof(cmd->body);
324 cmd->body.cid = res->id;
325 vmw_fifo_commit(dev_priv, sizeof(*cmd));
326 vmw_fifo_resource_inc(dev_priv);
328 return 0;
330 out_no_fifo:
331 vmw_resource_release_id(res);
332 out_no_id:
333 return ret;
336 static int vmw_gb_context_bind(struct vmw_resource *res,
337 struct ttm_validate_buffer *val_buf)
339 struct vmw_private *dev_priv = res->dev_priv;
340 struct {
341 SVGA3dCmdHeader header;
342 SVGA3dCmdBindGBContext body;
343 } *cmd;
344 struct ttm_buffer_object *bo = val_buf->bo;
346 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
348 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
349 if (unlikely(cmd == NULL)) {
350 DRM_ERROR("Failed reserving FIFO space for context "
351 "binding.\n");
352 return -ENOMEM;
354 cmd->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
355 cmd->header.size = sizeof(cmd->body);
356 cmd->body.cid = res->id;
357 cmd->body.mobid = bo->mem.start;
358 cmd->body.validContents = res->backup_dirty;
359 res->backup_dirty = false;
360 vmw_fifo_commit(dev_priv, sizeof(*cmd));
362 return 0;
365 static int vmw_gb_context_unbind(struct vmw_resource *res,
366 bool readback,
367 struct ttm_validate_buffer *val_buf)
369 struct vmw_private *dev_priv = res->dev_priv;
370 struct ttm_buffer_object *bo = val_buf->bo;
371 struct vmw_fence_obj *fence;
372 struct vmw_user_context *uctx =
373 container_of(res, struct vmw_user_context, res);
375 struct {
376 SVGA3dCmdHeader header;
377 SVGA3dCmdReadbackGBContext body;
378 } *cmd1;
379 struct {
380 SVGA3dCmdHeader header;
381 SVGA3dCmdBindGBContext body;
382 } *cmd2;
383 uint32_t submit_size;
384 uint8_t *cmd;
387 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
389 mutex_lock(&dev_priv->binding_mutex);
390 vmw_binding_state_scrub(uctx->cbs);
392 submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
394 cmd = vmw_fifo_reserve(dev_priv, submit_size);
395 if (unlikely(cmd == NULL)) {
396 DRM_ERROR("Failed reserving FIFO space for context "
397 "unbinding.\n");
398 mutex_unlock(&dev_priv->binding_mutex);
399 return -ENOMEM;
402 cmd2 = (void *) cmd;
403 if (readback) {
404 cmd1 = (void *) cmd;
405 cmd1->header.id = SVGA_3D_CMD_READBACK_GB_CONTEXT;
406 cmd1->header.size = sizeof(cmd1->body);
407 cmd1->body.cid = res->id;
408 cmd2 = (void *) (&cmd1[1]);
410 cmd2->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
411 cmd2->header.size = sizeof(cmd2->body);
412 cmd2->body.cid = res->id;
413 cmd2->body.mobid = SVGA3D_INVALID_ID;
415 vmw_fifo_commit(dev_priv, submit_size);
416 mutex_unlock(&dev_priv->binding_mutex);
419 * Create a fence object and fence the backup buffer.
422 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
423 &fence, NULL);
425 vmw_bo_fence_single(bo, fence);
427 if (likely(fence != NULL))
428 vmw_fence_obj_unreference(&fence);
430 return 0;
433 static int vmw_gb_context_destroy(struct vmw_resource *res)
435 struct vmw_private *dev_priv = res->dev_priv;
436 struct {
437 SVGA3dCmdHeader header;
438 SVGA3dCmdDestroyGBContext body;
439 } *cmd;
441 if (likely(res->id == -1))
442 return 0;
444 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
445 if (unlikely(cmd == NULL)) {
446 DRM_ERROR("Failed reserving FIFO space for context "
447 "destruction.\n");
448 return -ENOMEM;
451 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_CONTEXT;
452 cmd->header.size = sizeof(cmd->body);
453 cmd->body.cid = res->id;
454 vmw_fifo_commit(dev_priv, sizeof(*cmd));
455 if (dev_priv->query_cid == res->id)
456 dev_priv->query_cid_valid = false;
457 vmw_resource_release_id(res);
458 vmw_fifo_resource_dec(dev_priv);
460 return 0;
464 * DX context.
467 static int vmw_dx_context_create(struct vmw_resource *res)
469 struct vmw_private *dev_priv = res->dev_priv;
470 int ret;
471 struct {
472 SVGA3dCmdHeader header;
473 SVGA3dCmdDXDefineContext body;
474 } *cmd;
476 if (likely(res->id != -1))
477 return 0;
479 ret = vmw_resource_alloc_id(res);
480 if (unlikely(ret != 0)) {
481 DRM_ERROR("Failed to allocate a context id.\n");
482 goto out_no_id;
485 if (unlikely(res->id >= VMWGFX_NUM_DXCONTEXT)) {
486 ret = -EBUSY;
487 goto out_no_fifo;
490 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
491 if (unlikely(cmd == NULL)) {
492 DRM_ERROR("Failed reserving FIFO space for context "
493 "creation.\n");
494 ret = -ENOMEM;
495 goto out_no_fifo;
498 cmd->header.id = SVGA_3D_CMD_DX_DEFINE_CONTEXT;
499 cmd->header.size = sizeof(cmd->body);
500 cmd->body.cid = res->id;
501 vmw_fifo_commit(dev_priv, sizeof(*cmd));
502 vmw_fifo_resource_inc(dev_priv);
504 return 0;
506 out_no_fifo:
507 vmw_resource_release_id(res);
508 out_no_id:
509 return ret;
512 static int vmw_dx_context_bind(struct vmw_resource *res,
513 struct ttm_validate_buffer *val_buf)
515 struct vmw_private *dev_priv = res->dev_priv;
516 struct {
517 SVGA3dCmdHeader header;
518 SVGA3dCmdDXBindContext body;
519 } *cmd;
520 struct ttm_buffer_object *bo = val_buf->bo;
522 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
524 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
525 if (unlikely(cmd == NULL)) {
526 DRM_ERROR("Failed reserving FIFO space for context "
527 "binding.\n");
528 return -ENOMEM;
531 cmd->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT;
532 cmd->header.size = sizeof(cmd->body);
533 cmd->body.cid = res->id;
534 cmd->body.mobid = bo->mem.start;
535 cmd->body.validContents = res->backup_dirty;
536 res->backup_dirty = false;
537 vmw_fifo_commit(dev_priv, sizeof(*cmd));
540 return 0;
544 * vmw_dx_context_scrub_cotables - Scrub all bindings and
545 * cotables from a context
547 * @ctx: Pointer to the context resource
548 * @readback: Whether to save the otable contents on scrubbing.
550 * COtables must be unbound before their context, but unbinding requires
551 * the backup buffer being reserved, whereas scrubbing does not.
552 * This function scrubs all cotables of a context, potentially reading back
553 * the contents into their backup buffers. However, scrubbing cotables
554 * also makes the device context invalid, so scrub all bindings first so
555 * that doesn't have to be done later with an invalid context.
557 void vmw_dx_context_scrub_cotables(struct vmw_resource *ctx,
558 bool readback)
560 struct vmw_user_context *uctx =
561 container_of(ctx, struct vmw_user_context, res);
562 int i;
564 vmw_binding_state_scrub(uctx->cbs);
565 for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) {
566 struct vmw_resource *res;
568 /* Avoid racing with ongoing cotable destruction. */
569 spin_lock(&uctx->cotable_lock);
570 res = uctx->cotables[vmw_cotable_scrub_order[i]];
571 if (res)
572 res = vmw_resource_reference_unless_doomed(res);
573 spin_unlock(&uctx->cotable_lock);
574 if (!res)
575 continue;
577 WARN_ON(vmw_cotable_scrub(res, readback));
578 vmw_resource_unreference(&res);
582 static int vmw_dx_context_unbind(struct vmw_resource *res,
583 bool readback,
584 struct ttm_validate_buffer *val_buf)
586 struct vmw_private *dev_priv = res->dev_priv;
587 struct ttm_buffer_object *bo = val_buf->bo;
588 struct vmw_fence_obj *fence;
589 struct vmw_user_context *uctx =
590 container_of(res, struct vmw_user_context, res);
592 struct {
593 SVGA3dCmdHeader header;
594 SVGA3dCmdDXReadbackContext body;
595 } *cmd1;
596 struct {
597 SVGA3dCmdHeader header;
598 SVGA3dCmdDXBindContext body;
599 } *cmd2;
600 uint32_t submit_size;
601 uint8_t *cmd;
604 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
606 mutex_lock(&dev_priv->binding_mutex);
607 vmw_dx_context_scrub_cotables(res, readback);
609 if (uctx->dx_query_mob && uctx->dx_query_mob->dx_query_ctx &&
610 readback) {
611 WARN_ON(uctx->dx_query_mob->dx_query_ctx != res);
612 if (vmw_query_readback_all(uctx->dx_query_mob))
613 DRM_ERROR("Failed to read back query states\n");
616 submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
618 cmd = vmw_fifo_reserve(dev_priv, submit_size);
619 if (unlikely(cmd == NULL)) {
620 DRM_ERROR("Failed reserving FIFO space for context "
621 "unbinding.\n");
622 mutex_unlock(&dev_priv->binding_mutex);
623 return -ENOMEM;
626 cmd2 = (void *) cmd;
627 if (readback) {
628 cmd1 = (void *) cmd;
629 cmd1->header.id = SVGA_3D_CMD_DX_READBACK_CONTEXT;
630 cmd1->header.size = sizeof(cmd1->body);
631 cmd1->body.cid = res->id;
632 cmd2 = (void *) (&cmd1[1]);
634 cmd2->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT;
635 cmd2->header.size = sizeof(cmd2->body);
636 cmd2->body.cid = res->id;
637 cmd2->body.mobid = SVGA3D_INVALID_ID;
639 vmw_fifo_commit(dev_priv, submit_size);
640 mutex_unlock(&dev_priv->binding_mutex);
643 * Create a fence object and fence the backup buffer.
646 (void) vmw_execbuf_fence_commands(NULL, dev_priv,
647 &fence, NULL);
649 vmw_bo_fence_single(bo, fence);
651 if (likely(fence != NULL))
652 vmw_fence_obj_unreference(&fence);
654 return 0;
657 static int vmw_dx_context_destroy(struct vmw_resource *res)
659 struct vmw_private *dev_priv = res->dev_priv;
660 struct {
661 SVGA3dCmdHeader header;
662 SVGA3dCmdDXDestroyContext body;
663 } *cmd;
665 if (likely(res->id == -1))
666 return 0;
668 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
669 if (unlikely(cmd == NULL)) {
670 DRM_ERROR("Failed reserving FIFO space for context "
671 "destruction.\n");
672 return -ENOMEM;
675 cmd->header.id = SVGA_3D_CMD_DX_DESTROY_CONTEXT;
676 cmd->header.size = sizeof(cmd->body);
677 cmd->body.cid = res->id;
678 vmw_fifo_commit(dev_priv, sizeof(*cmd));
679 if (dev_priv->query_cid == res->id)
680 dev_priv->query_cid_valid = false;
681 vmw_resource_release_id(res);
682 vmw_fifo_resource_dec(dev_priv);
684 return 0;
688 * User-space context management:
691 static struct vmw_resource *
692 vmw_user_context_base_to_res(struct ttm_base_object *base)
694 return &(container_of(base, struct vmw_user_context, base)->res);
697 static void vmw_user_context_free(struct vmw_resource *res)
699 struct vmw_user_context *ctx =
700 container_of(res, struct vmw_user_context, res);
701 struct vmw_private *dev_priv = res->dev_priv;
703 if (ctx->cbs)
704 vmw_binding_state_free(ctx->cbs);
706 (void) vmw_context_bind_dx_query(res, NULL);
708 ttm_base_object_kfree(ctx, base);
709 ttm_mem_global_free(vmw_mem_glob(dev_priv),
710 vmw_user_context_size);
714 * This function is called when user space has no more references on the
715 * base object. It releases the base-object's reference on the resource object.
718 static void vmw_user_context_base_release(struct ttm_base_object **p_base)
720 struct ttm_base_object *base = *p_base;
721 struct vmw_user_context *ctx =
722 container_of(base, struct vmw_user_context, base);
723 struct vmw_resource *res = &ctx->res;
725 *p_base = NULL;
726 vmw_resource_unreference(&res);
729 int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
730 struct drm_file *file_priv)
732 struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
733 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
735 return ttm_ref_object_base_unref(tfile, arg->cid, TTM_REF_USAGE);
738 static int vmw_context_define(struct drm_device *dev, void *data,
739 struct drm_file *file_priv, bool dx)
741 struct vmw_private *dev_priv = vmw_priv(dev);
742 struct vmw_user_context *ctx;
743 struct vmw_resource *res;
744 struct vmw_resource *tmp;
745 struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
746 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
747 struct ttm_operation_ctx ttm_opt_ctx = {
748 .interruptible = true,
749 .no_wait_gpu = false
751 int ret;
753 if (!dev_priv->has_dx && dx) {
754 DRM_ERROR("DX contexts not supported by device.\n");
755 return -EINVAL;
758 if (unlikely(vmw_user_context_size == 0))
759 vmw_user_context_size = ttm_round_pot(sizeof(*ctx)) +
760 ((dev_priv->has_mob) ? vmw_cmdbuf_res_man_size() : 0) +
761 + VMW_IDA_ACC_SIZE + TTM_OBJ_EXTRA_SIZE;
763 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
764 if (unlikely(ret != 0))
765 return ret;
767 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
768 vmw_user_context_size,
769 &ttm_opt_ctx);
770 if (unlikely(ret != 0)) {
771 if (ret != -ERESTARTSYS)
772 DRM_ERROR("Out of graphics memory for context"
773 " creation.\n");
774 goto out_unlock;
777 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
778 if (unlikely(!ctx)) {
779 ttm_mem_global_free(vmw_mem_glob(dev_priv),
780 vmw_user_context_size);
781 ret = -ENOMEM;
782 goto out_unlock;
785 res = &ctx->res;
786 ctx->base.shareable = false;
787 ctx->base.tfile = NULL;
790 * From here on, the destructor takes over resource freeing.
793 ret = vmw_context_init(dev_priv, res, vmw_user_context_free, dx);
794 if (unlikely(ret != 0))
795 goto out_unlock;
797 tmp = vmw_resource_reference(&ctx->res);
798 ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
799 &vmw_user_context_base_release, NULL);
801 if (unlikely(ret != 0)) {
802 vmw_resource_unreference(&tmp);
803 goto out_err;
806 arg->cid = ctx->base.handle;
807 out_err:
808 vmw_resource_unreference(&res);
809 out_unlock:
810 ttm_read_unlock(&dev_priv->reservation_sem);
811 return ret;
814 int vmw_context_define_ioctl(struct drm_device *dev, void *data,
815 struct drm_file *file_priv)
817 return vmw_context_define(dev, data, file_priv, false);
820 int vmw_extended_context_define_ioctl(struct drm_device *dev, void *data,
821 struct drm_file *file_priv)
823 union drm_vmw_extended_context_arg *arg = (typeof(arg)) data;
824 struct drm_vmw_context_arg *rep = &arg->rep;
826 switch (arg->req) {
827 case drm_vmw_context_legacy:
828 return vmw_context_define(dev, rep, file_priv, false);
829 case drm_vmw_context_dx:
830 return vmw_context_define(dev, rep, file_priv, true);
831 default:
832 break;
834 return -EINVAL;
838 * vmw_context_binding_list - Return a list of context bindings
840 * @ctx: The context resource
842 * Returns the current list of bindings of the given context. Note that
843 * this list becomes stale as soon as the dev_priv::binding_mutex is unlocked.
845 struct list_head *vmw_context_binding_list(struct vmw_resource *ctx)
847 struct vmw_user_context *uctx =
848 container_of(ctx, struct vmw_user_context, res);
850 return vmw_binding_state_list(uctx->cbs);
853 struct vmw_cmdbuf_res_manager *vmw_context_res_man(struct vmw_resource *ctx)
855 return container_of(ctx, struct vmw_user_context, res)->man;
858 struct vmw_resource *vmw_context_cotable(struct vmw_resource *ctx,
859 SVGACOTableType cotable_type)
861 if (cotable_type >= SVGA_COTABLE_DX10_MAX)
862 return ERR_PTR(-EINVAL);
864 return container_of(ctx, struct vmw_user_context, res)->
865 cotables[cotable_type];
869 * vmw_context_binding_state -
870 * Return a pointer to a context binding state structure
872 * @ctx: The context resource
874 * Returns the current state of bindings of the given context. Note that
875 * this state becomes stale as soon as the dev_priv::binding_mutex is unlocked.
877 struct vmw_ctx_binding_state *
878 vmw_context_binding_state(struct vmw_resource *ctx)
880 return container_of(ctx, struct vmw_user_context, res)->cbs;
884 * vmw_context_bind_dx_query -
885 * Sets query MOB for the context. If @mob is NULL, then this function will
886 * remove the association between the MOB and the context. This function
887 * assumes the binding_mutex is held.
889 * @ctx_res: The context resource
890 * @mob: a reference to the query MOB
892 * Returns -EINVAL if a MOB has already been set and does not match the one
893 * specified in the parameter. 0 otherwise.
895 int vmw_context_bind_dx_query(struct vmw_resource *ctx_res,
896 struct vmw_buffer_object *mob)
898 struct vmw_user_context *uctx =
899 container_of(ctx_res, struct vmw_user_context, res);
901 if (mob == NULL) {
902 if (uctx->dx_query_mob) {
903 uctx->dx_query_mob->dx_query_ctx = NULL;
904 vmw_bo_unreference(&uctx->dx_query_mob);
905 uctx->dx_query_mob = NULL;
908 return 0;
911 /* Can only have one MOB per context for queries */
912 if (uctx->dx_query_mob && uctx->dx_query_mob != mob)
913 return -EINVAL;
915 mob->dx_query_ctx = ctx_res;
917 if (!uctx->dx_query_mob)
918 uctx->dx_query_mob = vmw_bo_reference(mob);
920 return 0;
924 * vmw_context_get_dx_query_mob - Returns non-counted reference to DX query mob
926 * @ctx_res: The context resource
928 struct vmw_buffer_object *
929 vmw_context_get_dx_query_mob(struct vmw_resource *ctx_res)
931 struct vmw_user_context *uctx =
932 container_of(ctx_res, struct vmw_user_context, res);
934 return uctx->dx_query_mob;