1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include "vmwgfx_reg.h"
30 #include <drm/ttm/ttm_bo_api.h>
31 #include <drm/ttm/ttm_placement.h>
33 #define VMW_RES_HT_ORDER 12
36 * struct vmw_resource_relocation - Relocation info for resources
38 * @head: List head for the software context's relocation list.
39 * @res: Non-ref-counted pointer to the resource.
40 * @offset: Offset of 4 byte entries into the command buffer where the
41 * id that needs fixup is located.
43 struct vmw_resource_relocation {
44 struct list_head head;
45 const struct vmw_resource *res;
50 * struct vmw_resource_val_node - Validation info for resources
52 * @head: List head for the software context's resource list.
53 * @hash: Hash entry for quick resouce to val_node lookup.
54 * @res: Ref-counted pointer to the resource.
55 * @switch_backup: Boolean whether to switch backup buffer on unreserve.
56 * @new_backup: Refcounted pointer to the new backup buffer.
57 * @new_backup_offset: New backup buffer offset if @new_backup is non-NUll.
58 * @first_usage: Set to true the first time the resource is referenced in
60 * @no_buffer_needed: Resources do not need to allocate buffer backup on
61 * reservation. The command stream will provide one.
63 struct vmw_resource_val_node {
64 struct list_head head;
65 struct drm_hash_item hash;
66 struct vmw_resource *res;
67 struct vmw_dma_buffer *new_backup;
68 unsigned long new_backup_offset;
70 bool no_buffer_needed;
74 * vmw_resource_unreserve - unreserve resources previously reserved for
77 * @list_head: list of resources to unreserve.
78 * @backoff: Whether command submission failed.
80 static void vmw_resource_list_unreserve(struct list_head *list,
83 struct vmw_resource_val_node *val;
85 list_for_each_entry(val, list, head) {
86 struct vmw_resource *res = val->res;
87 struct vmw_dma_buffer *new_backup =
88 backoff ? NULL : val->new_backup;
90 vmw_resource_unreserve(res, new_backup,
91 val->new_backup_offset);
92 vmw_dmabuf_unreference(&val->new_backup);
98 * vmw_resource_val_add - Add a resource to the software context's
99 * resource list if it's not already on it.
101 * @sw_context: Pointer to the software context.
102 * @res: Pointer to the resource.
103 * @p_node On successful return points to a valid pointer to a
104 * struct vmw_resource_val_node, if non-NULL on entry.
106 static int vmw_resource_val_add(struct vmw_sw_context *sw_context,
107 struct vmw_resource *res,
108 struct vmw_resource_val_node **p_node)
110 struct vmw_resource_val_node *node;
111 struct drm_hash_item *hash;
114 if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) res,
116 node = container_of(hash, struct vmw_resource_val_node, hash);
117 node->first_usage = false;
118 if (unlikely(p_node != NULL))
123 node = kzalloc(sizeof(*node), GFP_KERNEL);
124 if (unlikely(node == NULL)) {
125 DRM_ERROR("Failed to allocate a resource validation "
130 node->hash.key = (unsigned long) res;
131 ret = drm_ht_insert_item(&sw_context->res_ht, &node->hash);
132 if (unlikely(ret != 0)) {
133 DRM_ERROR("Failed to initialize a resource validation "
138 list_add_tail(&node->head, &sw_context->resource_list);
139 node->res = vmw_resource_reference(res);
140 node->first_usage = true;
142 if (unlikely(p_node != NULL))
149 * vmw_resource_relocation_add - Add a relocation to the relocation list
151 * @list: Pointer to head of relocation list.
152 * @res: The resource.
153 * @offset: Offset into the command buffer currently being parsed where the
154 * id that needs fixup is located. Granularity is 4 bytes.
156 static int vmw_resource_relocation_add(struct list_head *list,
157 const struct vmw_resource *res,
158 unsigned long offset)
160 struct vmw_resource_relocation *rel;
162 rel = kmalloc(sizeof(*rel), GFP_KERNEL);
163 if (unlikely(rel == NULL)) {
164 DRM_ERROR("Failed to allocate a resource relocation.\n");
169 rel->offset = offset;
170 list_add_tail(&rel->head, list);
176 * vmw_resource_relocations_free - Free all relocations on a list
178 * @list: Pointer to the head of the relocation list.
180 static void vmw_resource_relocations_free(struct list_head *list)
182 struct vmw_resource_relocation *rel, *n;
184 list_for_each_entry_safe(rel, n, list, head) {
185 list_del(&rel->head);
191 * vmw_resource_relocations_apply - Apply all relocations on a list
193 * @cb: Pointer to the start of the command buffer bein patch. This need
194 * not be the same buffer as the one being parsed when the relocation
195 * list was built, but the contents must be the same modulo the
197 * @list: Pointer to the head of the relocation list.
199 static void vmw_resource_relocations_apply(uint32_t *cb,
200 struct list_head *list)
202 struct vmw_resource_relocation *rel;
204 list_for_each_entry(rel, list, head)
205 cb[rel->offset] = rel->res->id;
208 static int vmw_cmd_invalid(struct vmw_private *dev_priv,
209 struct vmw_sw_context *sw_context,
210 SVGA3dCmdHeader *header)
212 return capable(CAP_SYS_ADMIN) ? : -EINVAL;
215 static int vmw_cmd_ok(struct vmw_private *dev_priv,
216 struct vmw_sw_context *sw_context,
217 SVGA3dCmdHeader *header)
223 * vmw_bo_to_validate_list - add a bo to a validate list
225 * @sw_context: The software context used for this command submission batch.
226 * @bo: The buffer object to add.
227 * @validate_as_mob: Validate this buffer as a MOB.
228 * @p_val_node: If non-NULL Will be updated with the validate node number
231 * Returns -EINVAL if the limit of number of buffer objects per command
232 * submission is reached.
234 static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
235 struct ttm_buffer_object *bo,
236 bool validate_as_mob,
237 uint32_t *p_val_node)
240 struct vmw_validate_buffer *vval_buf;
241 struct ttm_validate_buffer *val_buf;
242 struct drm_hash_item *hash;
245 if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) bo,
247 vval_buf = container_of(hash, struct vmw_validate_buffer,
249 if (unlikely(vval_buf->validate_as_mob != validate_as_mob)) {
250 DRM_ERROR("Inconsistent buffer usage.\n");
253 val_buf = &vval_buf->base;
254 val_node = vval_buf - sw_context->val_bufs;
256 val_node = sw_context->cur_val_buf;
257 if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
258 DRM_ERROR("Max number of DMA buffers per submission "
262 vval_buf = &sw_context->val_bufs[val_node];
263 vval_buf->hash.key = (unsigned long) bo;
264 ret = drm_ht_insert_item(&sw_context->res_ht, &vval_buf->hash);
265 if (unlikely(ret != 0)) {
266 DRM_ERROR("Failed to initialize a buffer validation "
270 ++sw_context->cur_val_buf;
271 val_buf = &vval_buf->base;
272 val_buf->bo = ttm_bo_reference(bo);
273 val_buf->reserved = false;
274 list_add_tail(&val_buf->head, &sw_context->validate_nodes);
275 vval_buf->validate_as_mob = validate_as_mob;
278 sw_context->fence_flags |= DRM_VMW_FENCE_FLAG_EXEC;
281 *p_val_node = val_node;
287 * vmw_resources_reserve - Reserve all resources on the sw_context's
290 * @sw_context: Pointer to the software context.
292 * Note that since vmware's command submission currently is protected by
293 * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
294 * since only a single thread at once will attempt this.
296 static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
298 struct vmw_resource_val_node *val;
301 list_for_each_entry(val, &sw_context->resource_list, head) {
302 struct vmw_resource *res = val->res;
304 ret = vmw_resource_reserve(res, val->no_buffer_needed);
305 if (unlikely(ret != 0))
309 struct ttm_buffer_object *bo = &res->backup->base;
311 ret = vmw_bo_to_validate_list
313 vmw_resource_needs_backup(res), NULL);
315 if (unlikely(ret != 0))
323 * vmw_resources_validate - Validate all resources on the sw_context's
326 * @sw_context: Pointer to the software context.
328 * Before this function is called, all resource backup buffers must have
331 static int vmw_resources_validate(struct vmw_sw_context *sw_context)
333 struct vmw_resource_val_node *val;
336 list_for_each_entry(val, &sw_context->resource_list, head) {
337 struct vmw_resource *res = val->res;
339 ret = vmw_resource_validate(res);
340 if (unlikely(ret != 0)) {
341 if (ret != -ERESTARTSYS)
342 DRM_ERROR("Failed to validate resource.\n");
350 * vmw_cmd_res_check - Check that a resource is present and if so, put it
351 * on the resource validate list unless it's already there.
353 * @dev_priv: Pointer to a device private structure.
354 * @sw_context: Pointer to the software context.
355 * @res_type: Resource type.
356 * @converter: User-space visisble type specific information.
357 * @id: Pointer to the location in the command buffer currently being
358 * parsed from where the user-space resource id handle is located.
360 static int vmw_cmd_res_check(struct vmw_private *dev_priv,
361 struct vmw_sw_context *sw_context,
362 enum vmw_res_type res_type,
363 const struct vmw_user_resource_conv *converter,
365 struct vmw_resource_val_node **p_val)
367 struct vmw_res_cache_entry *rcache =
368 &sw_context->res_cache[res_type];
369 struct vmw_resource *res;
370 struct vmw_resource_val_node *node;
373 if (*id == SVGA3D_INVALID_ID)
377 * Fastpath in case of repeated commands referencing the same
381 if (likely(rcache->valid && *id == rcache->handle)) {
382 const struct vmw_resource *res = rcache->res;
384 rcache->node->first_usage = false;
386 *p_val = rcache->node;
388 return vmw_resource_relocation_add
389 (&sw_context->res_relocations, res,
390 id - sw_context->buf_start);
393 ret = vmw_user_resource_lookup_handle(dev_priv,
398 if (unlikely(ret != 0)) {
399 DRM_ERROR("Could not find or use resource 0x%08x.\n",
405 rcache->valid = true;
407 rcache->handle = *id;
409 ret = vmw_resource_relocation_add(&sw_context->res_relocations,
411 id - sw_context->buf_start);
412 if (unlikely(ret != 0))
415 ret = vmw_resource_val_add(sw_context, res, &node);
416 if (unlikely(ret != 0))
422 vmw_resource_unreference(&res);
426 BUG_ON(sw_context->error_resource != NULL);
427 sw_context->error_resource = res;
433 * vmw_cmd_cid_check - Check a command header for valid context information.
435 * @dev_priv: Pointer to a device private structure.
436 * @sw_context: Pointer to the software context.
437 * @header: A command header with an embedded user-space context handle.
439 * Convenience function: Call vmw_cmd_res_check with the user-space context
440 * handle embedded in @header.
442 static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
443 struct vmw_sw_context *sw_context,
444 SVGA3dCmdHeader *header)
447 SVGA3dCmdHeader header;
451 cmd = container_of(header, struct vmw_cid_cmd, header);
452 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
453 user_context_converter, &cmd->cid, NULL);
456 static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
457 struct vmw_sw_context *sw_context,
458 SVGA3dCmdHeader *header)
461 SVGA3dCmdHeader header;
462 SVGA3dCmdSetRenderTarget body;
466 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
467 if (unlikely(ret != 0))
470 cmd = container_of(header, struct vmw_sid_cmd, header);
471 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
472 user_surface_converter,
473 &cmd->body.target.sid, NULL);
477 static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
478 struct vmw_sw_context *sw_context,
479 SVGA3dCmdHeader *header)
482 SVGA3dCmdHeader header;
483 SVGA3dCmdSurfaceCopy body;
487 cmd = container_of(header, struct vmw_sid_cmd, header);
488 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
489 user_surface_converter,
490 &cmd->body.src.sid, NULL);
491 if (unlikely(ret != 0))
493 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
494 user_surface_converter,
495 &cmd->body.dest.sid, NULL);
498 static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
499 struct vmw_sw_context *sw_context,
500 SVGA3dCmdHeader *header)
503 SVGA3dCmdHeader header;
504 SVGA3dCmdSurfaceStretchBlt body;
508 cmd = container_of(header, struct vmw_sid_cmd, header);
509 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
510 user_surface_converter,
511 &cmd->body.src.sid, NULL);
512 if (unlikely(ret != 0))
514 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
515 user_surface_converter,
516 &cmd->body.dest.sid, NULL);
519 static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
520 struct vmw_sw_context *sw_context,
521 SVGA3dCmdHeader *header)
524 SVGA3dCmdHeader header;
525 SVGA3dCmdBlitSurfaceToScreen body;
528 cmd = container_of(header, struct vmw_sid_cmd, header);
530 if (unlikely(!sw_context->kernel)) {
531 DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
535 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
536 user_surface_converter,
537 &cmd->body.srcImage.sid, NULL);
540 static int vmw_cmd_present_check(struct vmw_private *dev_priv,
541 struct vmw_sw_context *sw_context,
542 SVGA3dCmdHeader *header)
545 SVGA3dCmdHeader header;
546 SVGA3dCmdPresent body;
550 cmd = container_of(header, struct vmw_sid_cmd, header);
552 if (unlikely(!sw_context->kernel)) {
553 DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
557 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
558 user_surface_converter, &cmd->body.sid,
563 * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
565 * @dev_priv: The device private structure.
566 * @new_query_bo: The new buffer holding query results.
567 * @sw_context: The software context used for this command submission.
569 * This function checks whether @new_query_bo is suitable for holding
570 * query results, and if another buffer currently is pinned for query
571 * results. If so, the function prepares the state of @sw_context for
572 * switching pinned buffers after successful submission of the current
575 static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
576 struct ttm_buffer_object *new_query_bo,
577 struct vmw_sw_context *sw_context)
579 struct vmw_res_cache_entry *ctx_entry =
580 &sw_context->res_cache[vmw_res_context];
583 BUG_ON(!ctx_entry->valid);
584 sw_context->last_query_ctx = ctx_entry->res;
586 if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
588 if (unlikely(new_query_bo->num_pages > 4)) {
589 DRM_ERROR("Query buffer too large.\n");
593 if (unlikely(sw_context->cur_query_bo != NULL)) {
594 sw_context->needs_post_query_barrier = true;
595 ret = vmw_bo_to_validate_list(sw_context,
596 sw_context->cur_query_bo,
597 dev_priv->has_mob, NULL);
598 if (unlikely(ret != 0))
601 sw_context->cur_query_bo = new_query_bo;
603 ret = vmw_bo_to_validate_list(sw_context,
604 dev_priv->dummy_query_bo,
605 dev_priv->has_mob, NULL);
606 if (unlikely(ret != 0))
616 * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
618 * @dev_priv: The device private structure.
619 * @sw_context: The software context used for this command submission batch.
621 * This function will check if we're switching query buffers, and will then,
622 * issue a dummy occlusion query wait used as a query barrier. When the fence
623 * object following that query wait has signaled, we are sure that all
624 * preceding queries have finished, and the old query buffer can be unpinned.
625 * However, since both the new query buffer and the old one are fenced with
626 * that fence, we can do an asynchronus unpin now, and be sure that the
627 * old query buffer won't be moved until the fence has signaled.
629 * As mentioned above, both the new - and old query buffers need to be fenced
630 * using a sequence emitted *after* calling this function.
632 static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
633 struct vmw_sw_context *sw_context)
636 * The validate list should still hold references to all
640 if (sw_context->needs_post_query_barrier) {
641 struct vmw_res_cache_entry *ctx_entry =
642 &sw_context->res_cache[vmw_res_context];
643 struct vmw_resource *ctx;
646 BUG_ON(!ctx_entry->valid);
647 ctx = ctx_entry->res;
649 ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
651 if (unlikely(ret != 0))
652 DRM_ERROR("Out of fifo space for dummy query.\n");
655 if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
656 if (dev_priv->pinned_bo) {
657 vmw_bo_pin(dev_priv->pinned_bo, false);
658 ttm_bo_unref(&dev_priv->pinned_bo);
661 if (!sw_context->needs_post_query_barrier) {
662 vmw_bo_pin(sw_context->cur_query_bo, true);
665 * We pin also the dummy_query_bo buffer so that we
666 * don't need to validate it when emitting
667 * dummy queries in context destroy paths.
670 vmw_bo_pin(dev_priv->dummy_query_bo, true);
671 dev_priv->dummy_query_bo_pinned = true;
673 BUG_ON(sw_context->last_query_ctx == NULL);
674 dev_priv->query_cid = sw_context->last_query_ctx->id;
675 dev_priv->query_cid_valid = true;
676 dev_priv->pinned_bo =
677 ttm_bo_reference(sw_context->cur_query_bo);
683 * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
684 * handle to a MOB id.
686 * @dev_priv: Pointer to a device private structure.
687 * @sw_context: The software context used for this command batch validation.
688 * @id: Pointer to the user-space handle to be translated.
689 * @vmw_bo_p: Points to a location that, on successful return will carry
690 * a reference-counted pointer to the DMA buffer identified by the
691 * user-space handle in @id.
693 * This function saves information needed to translate a user-space buffer
694 * handle to a MOB id. The translation does not take place immediately, but
695 * during a call to vmw_apply_relocations(). This function builds a relocation
696 * list and a list of buffers to validate. The former needs to be freed using
697 * either vmw_apply_relocations() or vmw_free_relocations(). The latter
698 * needs to be freed using vmw_clear_validations.
700 static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
701 struct vmw_sw_context *sw_context,
703 struct vmw_dma_buffer **vmw_bo_p)
705 struct vmw_dma_buffer *vmw_bo = NULL;
706 struct ttm_buffer_object *bo;
707 uint32_t handle = *id;
708 struct vmw_relocation *reloc;
711 ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
712 if (unlikely(ret != 0)) {
713 DRM_ERROR("Could not find or use MOB buffer.\n");
718 if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
719 DRM_ERROR("Max number relocations per submission"
725 reloc = &sw_context->relocs[sw_context->cur_reloc++];
727 reloc->location = NULL;
729 ret = vmw_bo_to_validate_list(sw_context, bo, true, &reloc->index);
730 if (unlikely(ret != 0))
737 vmw_dmabuf_unreference(&vmw_bo);
743 * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
744 * handle to a valid SVGAGuestPtr
746 * @dev_priv: Pointer to a device private structure.
747 * @sw_context: The software context used for this command batch validation.
748 * @ptr: Pointer to the user-space handle to be translated.
749 * @vmw_bo_p: Points to a location that, on successful return will carry
750 * a reference-counted pointer to the DMA buffer identified by the
751 * user-space handle in @id.
753 * This function saves information needed to translate a user-space buffer
754 * handle to a valid SVGAGuestPtr. The translation does not take place
755 * immediately, but during a call to vmw_apply_relocations().
756 * This function builds a relocation list and a list of buffers to validate.
757 * The former needs to be freed using either vmw_apply_relocations() or
758 * vmw_free_relocations(). The latter needs to be freed using
759 * vmw_clear_validations.
761 static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
762 struct vmw_sw_context *sw_context,
764 struct vmw_dma_buffer **vmw_bo_p)
766 struct vmw_dma_buffer *vmw_bo = NULL;
767 struct ttm_buffer_object *bo;
768 uint32_t handle = ptr->gmrId;
769 struct vmw_relocation *reloc;
772 ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
773 if (unlikely(ret != 0)) {
774 DRM_ERROR("Could not find or use GMR region.\n");
779 if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
780 DRM_ERROR("Max number relocations per submission"
786 reloc = &sw_context->relocs[sw_context->cur_reloc++];
787 reloc->location = ptr;
789 ret = vmw_bo_to_validate_list(sw_context, bo, false, &reloc->index);
790 if (unlikely(ret != 0))
797 vmw_dmabuf_unreference(&vmw_bo);
803 * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_QUERY command.
805 * @dev_priv: Pointer to a device private struct.
806 * @sw_context: The software context used for this command submission.
807 * @header: Pointer to the command header in the command stream.
809 static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
810 struct vmw_sw_context *sw_context,
811 SVGA3dCmdHeader *header)
813 struct vmw_begin_gb_query_cmd {
814 SVGA3dCmdHeader header;
815 SVGA3dCmdBeginGBQuery q;
818 cmd = container_of(header, struct vmw_begin_gb_query_cmd,
821 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
822 user_context_converter, &cmd->q.cid,
827 * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
829 * @dev_priv: Pointer to a device private struct.
830 * @sw_context: The software context used for this command submission.
831 * @header: Pointer to the command header in the command stream.
833 static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
834 struct vmw_sw_context *sw_context,
835 SVGA3dCmdHeader *header)
837 struct vmw_begin_query_cmd {
838 SVGA3dCmdHeader header;
839 SVGA3dCmdBeginQuery q;
842 cmd = container_of(header, struct vmw_begin_query_cmd,
845 if (unlikely(dev_priv->has_mob)) {
847 SVGA3dCmdHeader header;
848 SVGA3dCmdBeginGBQuery q;
851 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
853 gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
854 gb_cmd.header.size = cmd->header.size;
855 gb_cmd.q.cid = cmd->q.cid;
856 gb_cmd.q.type = cmd->q.type;
858 memcpy(cmd, &gb_cmd, sizeof(*cmd));
859 return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
862 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
863 user_context_converter, &cmd->q.cid,
868 * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
870 * @dev_priv: Pointer to a device private struct.
871 * @sw_context: The software context used for this command submission.
872 * @header: Pointer to the command header in the command stream.
874 static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
875 struct vmw_sw_context *sw_context,
876 SVGA3dCmdHeader *header)
878 struct vmw_dma_buffer *vmw_bo;
879 struct vmw_query_cmd {
880 SVGA3dCmdHeader header;
881 SVGA3dCmdEndGBQuery q;
885 cmd = container_of(header, struct vmw_query_cmd, header);
886 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
887 if (unlikely(ret != 0))
890 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
893 if (unlikely(ret != 0))
896 ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
898 vmw_dmabuf_unreference(&vmw_bo);
903 * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
905 * @dev_priv: Pointer to a device private struct.
906 * @sw_context: The software context used for this command submission.
907 * @header: Pointer to the command header in the command stream.
909 static int vmw_cmd_end_query(struct vmw_private *dev_priv,
910 struct vmw_sw_context *sw_context,
911 SVGA3dCmdHeader *header)
913 struct vmw_dma_buffer *vmw_bo;
914 struct vmw_query_cmd {
915 SVGA3dCmdHeader header;
920 cmd = container_of(header, struct vmw_query_cmd, header);
921 if (dev_priv->has_mob) {
923 SVGA3dCmdHeader header;
924 SVGA3dCmdEndGBQuery q;
927 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
929 gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
930 gb_cmd.header.size = cmd->header.size;
931 gb_cmd.q.cid = cmd->q.cid;
932 gb_cmd.q.type = cmd->q.type;
933 gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
934 gb_cmd.q.offset = cmd->q.guestResult.offset;
936 memcpy(cmd, &gb_cmd, sizeof(*cmd));
937 return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
940 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
941 if (unlikely(ret != 0))
944 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
947 if (unlikely(ret != 0))
950 ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
952 vmw_dmabuf_unreference(&vmw_bo);
957 * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
959 * @dev_priv: Pointer to a device private struct.
960 * @sw_context: The software context used for this command submission.
961 * @header: Pointer to the command header in the command stream.
963 static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
964 struct vmw_sw_context *sw_context,
965 SVGA3dCmdHeader *header)
967 struct vmw_dma_buffer *vmw_bo;
968 struct vmw_query_cmd {
969 SVGA3dCmdHeader header;
970 SVGA3dCmdWaitForGBQuery q;
974 cmd = container_of(header, struct vmw_query_cmd, header);
975 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
976 if (unlikely(ret != 0))
979 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
982 if (unlikely(ret != 0))
985 vmw_dmabuf_unreference(&vmw_bo);
990 * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
992 * @dev_priv: Pointer to a device private struct.
993 * @sw_context: The software context used for this command submission.
994 * @header: Pointer to the command header in the command stream.
996 static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
997 struct vmw_sw_context *sw_context,
998 SVGA3dCmdHeader *header)
1000 struct vmw_dma_buffer *vmw_bo;
1001 struct vmw_query_cmd {
1002 SVGA3dCmdHeader header;
1003 SVGA3dCmdWaitForQuery q;
1007 cmd = container_of(header, struct vmw_query_cmd, header);
1008 if (dev_priv->has_mob) {
1010 SVGA3dCmdHeader header;
1011 SVGA3dCmdWaitForGBQuery q;
1014 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1016 gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
1017 gb_cmd.header.size = cmd->header.size;
1018 gb_cmd.q.cid = cmd->q.cid;
1019 gb_cmd.q.type = cmd->q.type;
1020 gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
1021 gb_cmd.q.offset = cmd->q.guestResult.offset;
1023 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1024 return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
1027 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1028 if (unlikely(ret != 0))
1031 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1032 &cmd->q.guestResult,
1034 if (unlikely(ret != 0))
1037 vmw_dmabuf_unreference(&vmw_bo);
1041 static int vmw_cmd_dma(struct vmw_private *dev_priv,
1042 struct vmw_sw_context *sw_context,
1043 SVGA3dCmdHeader *header)
1045 struct vmw_dma_buffer *vmw_bo = NULL;
1046 struct vmw_surface *srf = NULL;
1047 struct vmw_dma_cmd {
1048 SVGA3dCmdHeader header;
1049 SVGA3dCmdSurfaceDMA dma;
1053 cmd = container_of(header, struct vmw_dma_cmd, header);
1054 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1055 &cmd->dma.guest.ptr,
1057 if (unlikely(ret != 0))
1060 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1061 user_surface_converter, &cmd->dma.host.sid,
1063 if (unlikely(ret != 0)) {
1064 if (unlikely(ret != -ERESTARTSYS))
1065 DRM_ERROR("could not find surface for DMA.\n");
1066 goto out_no_surface;
1069 srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
1071 vmw_kms_cursor_snoop(srf, sw_context->tfile, &vmw_bo->base, header);
1074 vmw_dmabuf_unreference(&vmw_bo);
1078 static int vmw_cmd_draw(struct vmw_private *dev_priv,
1079 struct vmw_sw_context *sw_context,
1080 SVGA3dCmdHeader *header)
1082 struct vmw_draw_cmd {
1083 SVGA3dCmdHeader header;
1084 SVGA3dCmdDrawPrimitives body;
1086 SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
1087 (unsigned long)header + sizeof(*cmd));
1088 SVGA3dPrimitiveRange *range;
1093 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1094 if (unlikely(ret != 0))
1097 cmd = container_of(header, struct vmw_draw_cmd, header);
1098 maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
1100 if (unlikely(cmd->body.numVertexDecls > maxnum)) {
1101 DRM_ERROR("Illegal number of vertex declarations.\n");
1105 for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
1106 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1107 user_surface_converter,
1108 &decl->array.surfaceId, NULL);
1109 if (unlikely(ret != 0))
1113 maxnum = (header->size - sizeof(cmd->body) -
1114 cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
1115 if (unlikely(cmd->body.numRanges > maxnum)) {
1116 DRM_ERROR("Illegal number of index ranges.\n");
1120 range = (SVGA3dPrimitiveRange *) decl;
1121 for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
1122 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1123 user_surface_converter,
1124 &range->indexArray.surfaceId, NULL);
1125 if (unlikely(ret != 0))
1132 static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
1133 struct vmw_sw_context *sw_context,
1134 SVGA3dCmdHeader *header)
1136 struct vmw_tex_state_cmd {
1137 SVGA3dCmdHeader header;
1138 SVGA3dCmdSetTextureState state;
1141 SVGA3dTextureState *last_state = (SVGA3dTextureState *)
1142 ((unsigned long) header + header->size + sizeof(header));
1143 SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
1144 ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
1147 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1148 if (unlikely(ret != 0))
1151 for (; cur_state < last_state; ++cur_state) {
1152 if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
1155 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1156 user_surface_converter,
1157 &cur_state->value, NULL);
1158 if (unlikely(ret != 0))
1165 static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
1166 struct vmw_sw_context *sw_context,
1169 struct vmw_dma_buffer *vmw_bo;
1174 SVGAFifoCmdDefineGMRFB body;
1177 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1180 if (unlikely(ret != 0))
1183 vmw_dmabuf_unreference(&vmw_bo);
1189 * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
1191 * @dev_priv: Pointer to a device private struct.
1192 * @sw_context: The software context being used for this batch.
1193 * @res_type: The resource type.
1194 * @converter: Information about user-space binding for this resource type.
1195 * @res_id: Pointer to the user-space resource handle in the command stream.
1196 * @buf_id: Pointer to the user-space backup buffer handle in the command
1198 * @backup_offset: Offset of backup into MOB.
1200 * This function prepares for registering a switch of backup buffers
1201 * in the resource metadata just prior to unreserving.
1203 static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
1204 struct vmw_sw_context *sw_context,
1205 enum vmw_res_type res_type,
1206 const struct vmw_user_resource_conv
1210 unsigned long backup_offset)
1213 struct vmw_dma_buffer *dma_buf;
1214 struct vmw_resource_val_node *val_node;
1216 ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
1217 converter, res_id, &val_node);
1218 if (unlikely(ret != 0))
1221 ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &dma_buf);
1222 if (unlikely(ret != 0))
1225 if (val_node->first_usage)
1226 val_node->no_buffer_needed = true;
1228 vmw_dmabuf_unreference(&val_node->new_backup);
1229 val_node->new_backup = dma_buf;
1230 val_node->new_backup_offset = backup_offset;
1236 * vmw_cmd_bind_gb_surface - Validate an SVGA_3D_CMD_BIND_GB_SURFACE
1239 * @dev_priv: Pointer to a device private struct.
1240 * @sw_context: The software context being used for this batch.
1241 * @header: Pointer to the command header in the command stream.
1243 static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
1244 struct vmw_sw_context *sw_context,
1245 SVGA3dCmdHeader *header)
1247 struct vmw_bind_gb_surface_cmd {
1248 SVGA3dCmdHeader header;
1249 SVGA3dCmdBindGBSurface body;
1252 cmd = container_of(header, struct vmw_bind_gb_surface_cmd, header);
1254 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
1255 user_surface_converter,
1256 &cmd->body.sid, &cmd->body.mobid,
1261 * vmw_cmd_update_gb_image - Validate an SVGA_3D_CMD_UPDATE_GB_IMAGE
1264 * @dev_priv: Pointer to a device private struct.
1265 * @sw_context: The software context being used for this batch.
1266 * @header: Pointer to the command header in the command stream.
1268 static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
1269 struct vmw_sw_context *sw_context,
1270 SVGA3dCmdHeader *header)
1272 struct vmw_gb_surface_cmd {
1273 SVGA3dCmdHeader header;
1274 SVGA3dCmdUpdateGBImage body;
1277 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1279 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1280 user_surface_converter,
1281 &cmd->body.image.sid, NULL);
1285 * vmw_cmd_update_gb_surface - Validate an SVGA_3D_CMD_UPDATE_GB_SURFACE
1288 * @dev_priv: Pointer to a device private struct.
1289 * @sw_context: The software context being used for this batch.
1290 * @header: Pointer to the command header in the command stream.
1292 static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
1293 struct vmw_sw_context *sw_context,
1294 SVGA3dCmdHeader *header)
1296 struct vmw_gb_surface_cmd {
1297 SVGA3dCmdHeader header;
1298 SVGA3dCmdUpdateGBSurface body;
1301 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1303 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1304 user_surface_converter,
1305 &cmd->body.sid, NULL);
1309 * vmw_cmd_readback_gb_image - Validate an SVGA_3D_CMD_READBACK_GB_IMAGE
1312 * @dev_priv: Pointer to a device private struct.
1313 * @sw_context: The software context being used for this batch.
1314 * @header: Pointer to the command header in the command stream.
1316 static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
1317 struct vmw_sw_context *sw_context,
1318 SVGA3dCmdHeader *header)
1320 struct vmw_gb_surface_cmd {
1321 SVGA3dCmdHeader header;
1322 SVGA3dCmdReadbackGBImage body;
1325 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1327 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1328 user_surface_converter,
1329 &cmd->body.image.sid, NULL);
1333 * vmw_cmd_readback_gb_surface - Validate an SVGA_3D_CMD_READBACK_GB_SURFACE
1336 * @dev_priv: Pointer to a device private struct.
1337 * @sw_context: The software context being used for this batch.
1338 * @header: Pointer to the command header in the command stream.
1340 static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
1341 struct vmw_sw_context *sw_context,
1342 SVGA3dCmdHeader *header)
1344 struct vmw_gb_surface_cmd {
1345 SVGA3dCmdHeader header;
1346 SVGA3dCmdReadbackGBSurface body;
1349 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1351 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1352 user_surface_converter,
1353 &cmd->body.sid, NULL);
1357 * vmw_cmd_invalidate_gb_image - Validate an SVGA_3D_CMD_INVALIDATE_GB_IMAGE
1360 * @dev_priv: Pointer to a device private struct.
1361 * @sw_context: The software context being used for this batch.
1362 * @header: Pointer to the command header in the command stream.
1364 static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
1365 struct vmw_sw_context *sw_context,
1366 SVGA3dCmdHeader *header)
1368 struct vmw_gb_surface_cmd {
1369 SVGA3dCmdHeader header;
1370 SVGA3dCmdInvalidateGBImage body;
1373 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1375 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1376 user_surface_converter,
1377 &cmd->body.image.sid, NULL);
1381 * vmw_cmd_invalidate_gb_surface - Validate an
1382 * SVGA_3D_CMD_INVALIDATE_GB_SURFACE command
1384 * @dev_priv: Pointer to a device private struct.
1385 * @sw_context: The software context being used for this batch.
1386 * @header: Pointer to the command header in the command stream.
1388 static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
1389 struct vmw_sw_context *sw_context,
1390 SVGA3dCmdHeader *header)
1392 struct vmw_gb_surface_cmd {
1393 SVGA3dCmdHeader header;
1394 SVGA3dCmdInvalidateGBSurface body;
1397 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1399 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1400 user_surface_converter,
1401 &cmd->body.sid, NULL);
1405 * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
1408 * @dev_priv: Pointer to a device private struct.
1409 * @sw_context: The software context being used for this batch.
1410 * @header: Pointer to the command header in the command stream.
1412 static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
1413 struct vmw_sw_context *sw_context,
1414 SVGA3dCmdHeader *header)
1416 struct vmw_set_shader_cmd {
1417 SVGA3dCmdHeader header;
1418 SVGA3dCmdSetShader body;
1422 cmd = container_of(header, struct vmw_set_shader_cmd,
1425 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1426 if (unlikely(ret != 0))
1430 if (dev_priv->has_mob)
1431 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_shader,
1432 user_shader_converter,
1433 &cmd->body.shid, NULL);
1439 * vmw_cmd_bind_gb_shader - Validate an SVGA_3D_CMD_BIND_GB_SHADER
1442 * @dev_priv: Pointer to a device private struct.
1443 * @sw_context: The software context being used for this batch.
1444 * @header: Pointer to the command header in the command stream.
1446 static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
1447 struct vmw_sw_context *sw_context,
1448 SVGA3dCmdHeader *header)
1450 struct vmw_bind_gb_shader_cmd {
1451 SVGA3dCmdHeader header;
1452 SVGA3dCmdBindGBShader body;
1455 cmd = container_of(header, struct vmw_bind_gb_shader_cmd,
1458 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
1459 user_shader_converter,
1460 &cmd->body.shid, &cmd->body.mobid,
1461 cmd->body.offsetInBytes);
1464 static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
1465 struct vmw_sw_context *sw_context,
1466 void *buf, uint32_t *size)
1468 uint32_t size_remaining = *size;
1471 cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
1473 case SVGA_CMD_UPDATE:
1474 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
1476 case SVGA_CMD_DEFINE_GMRFB:
1477 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
1479 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
1480 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
1482 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
1483 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
1486 DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
1490 if (*size > size_remaining) {
1491 DRM_ERROR("Invalid SVGA command (size mismatch):"
1496 if (unlikely(!sw_context->kernel)) {
1497 DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
1501 if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
1502 return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
1507 typedef int (*vmw_cmd_func) (struct vmw_private *,
1508 struct vmw_sw_context *,
1511 #define VMW_CMD_DEF(cmd, func) \
1512 [cmd - SVGA_3D_CMD_BASE] = func
1514 static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
1515 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
1516 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
1517 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
1518 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
1519 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
1520 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
1521 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
1522 VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
1523 VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
1524 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
1525 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
1526 &vmw_cmd_set_render_target_check),
1527 VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
1528 VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
1529 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
1530 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
1531 VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
1532 VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
1533 VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
1534 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
1535 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
1536 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
1537 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader),
1538 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
1539 VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
1540 VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
1541 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query),
1542 VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query),
1543 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query),
1544 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
1545 VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
1546 &vmw_cmd_blt_surf_screen_check),
1547 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid),
1548 VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid),
1549 VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid),
1550 VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid),
1551 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid),
1552 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid),
1553 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface),
1554 VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid),
1555 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image),
1556 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
1557 &vmw_cmd_update_gb_surface),
1558 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
1559 &vmw_cmd_readback_gb_image),
1560 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
1561 &vmw_cmd_readback_gb_surface),
1562 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
1563 &vmw_cmd_invalidate_gb_image),
1564 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
1565 &vmw_cmd_invalidate_gb_surface),
1566 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid),
1567 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid),
1568 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid),
1569 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid),
1570 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid),
1571 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid),
1572 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader),
1573 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid),
1574 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query),
1575 VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query),
1576 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query),
1579 static int vmw_cmd_check(struct vmw_private *dev_priv,
1580 struct vmw_sw_context *sw_context,
1581 void *buf, uint32_t *size)
1584 uint32_t size_remaining = *size;
1585 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
1588 cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
1589 /* Handle any none 3D commands */
1590 if (unlikely(cmd_id < SVGA_CMD_MAX))
1591 return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
1594 cmd_id = le32_to_cpu(header->id);
1595 *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
1597 cmd_id -= SVGA_3D_CMD_BASE;
1598 if (unlikely(*size > size_remaining))
1601 if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
1604 ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
1605 if (unlikely(ret != 0))
1610 DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
1611 cmd_id + SVGA_3D_CMD_BASE);
1615 static int vmw_cmd_check_all(struct vmw_private *dev_priv,
1616 struct vmw_sw_context *sw_context,
1620 int32_t cur_size = size;
1623 sw_context->buf_start = buf;
1625 while (cur_size > 0) {
1627 ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
1628 if (unlikely(ret != 0))
1630 buf = (void *)((unsigned long) buf + size);
1634 if (unlikely(cur_size != 0)) {
1635 DRM_ERROR("Command verifier out of sync.\n");
1642 static void vmw_free_relocations(struct vmw_sw_context *sw_context)
1644 sw_context->cur_reloc = 0;
1647 static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
1650 struct vmw_relocation *reloc;
1651 struct ttm_validate_buffer *validate;
1652 struct ttm_buffer_object *bo;
1654 for (i = 0; i < sw_context->cur_reloc; ++i) {
1655 reloc = &sw_context->relocs[i];
1656 validate = &sw_context->val_bufs[reloc->index].base;
1658 switch (bo->mem.mem_type) {
1660 reloc->location->offset += bo->offset;
1661 reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
1664 reloc->location->gmrId = bo->mem.start;
1667 *reloc->mob_loc = bo->mem.start;
1673 vmw_free_relocations(sw_context);
1677 * vmw_resource_list_unrefererence - Free up a resource list and unreference
1678 * all resources referenced by it.
1680 * @list: The resource list.
1682 static void vmw_resource_list_unreference(struct list_head *list)
1684 struct vmw_resource_val_node *val, *val_next;
1687 * Drop references to resources held during command submission.
1690 list_for_each_entry_safe(val, val_next, list, head) {
1691 list_del_init(&val->head);
1692 vmw_resource_unreference(&val->res);
1697 static void vmw_clear_validations(struct vmw_sw_context *sw_context)
1699 struct vmw_validate_buffer *entry, *next;
1700 struct vmw_resource_val_node *val;
1703 * Drop references to DMA buffers held during command submission.
1705 list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
1707 list_del(&entry->base.head);
1708 ttm_bo_unref(&entry->base.bo);
1709 (void) drm_ht_remove_item(&sw_context->res_ht, &entry->hash);
1710 sw_context->cur_val_buf--;
1712 BUG_ON(sw_context->cur_val_buf != 0);
1714 list_for_each_entry(val, &sw_context->resource_list, head)
1715 (void) drm_ht_remove_item(&sw_context->res_ht, &val->hash);
1718 static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
1719 struct ttm_buffer_object *bo,
1720 bool validate_as_mob)
1726 * Don't validate pinned buffers.
1729 if (bo == dev_priv->pinned_bo ||
1730 (bo == dev_priv->dummy_query_bo &&
1731 dev_priv->dummy_query_bo_pinned))
1734 if (validate_as_mob)
1735 return ttm_bo_validate(bo, &vmw_mob_placement, true, false);
1738 * Put BO in VRAM if there is space, otherwise as a GMR.
1739 * If there is no space in VRAM and GMR ids are all used up,
1740 * start evicting GMRs to make room. If the DMA buffer can't be
1741 * used as a GMR, this will return -ENOMEM.
1744 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false);
1745 if (likely(ret == 0 || ret == -ERESTARTSYS))
1749 * If that failed, try VRAM again, this time evicting
1750 * previous contents.
1753 DRM_INFO("Falling through to VRAM.\n");
1754 ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false);
1758 static int vmw_validate_buffers(struct vmw_private *dev_priv,
1759 struct vmw_sw_context *sw_context)
1761 struct vmw_validate_buffer *entry;
1764 list_for_each_entry(entry, &sw_context->validate_nodes, base.head) {
1765 ret = vmw_validate_single_buffer(dev_priv, entry->base.bo,
1766 entry->validate_as_mob);
1767 if (unlikely(ret != 0))
1773 static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
1776 if (likely(sw_context->cmd_bounce_size >= size))
1779 if (sw_context->cmd_bounce_size == 0)
1780 sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
1782 while (sw_context->cmd_bounce_size < size) {
1783 sw_context->cmd_bounce_size =
1784 PAGE_ALIGN(sw_context->cmd_bounce_size +
1785 (sw_context->cmd_bounce_size >> 1));
1788 if (sw_context->cmd_bounce != NULL)
1789 vfree(sw_context->cmd_bounce);
1791 sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
1793 if (sw_context->cmd_bounce == NULL) {
1794 DRM_ERROR("Failed to allocate command bounce buffer.\n");
1795 sw_context->cmd_bounce_size = 0;
1803 * vmw_execbuf_fence_commands - create and submit a command stream fence
1805 * Creates a fence object and submits a command stream marker.
1806 * If this fails for some reason, We sync the fifo and return NULL.
1807 * It is then safe to fence buffers with a NULL pointer.
1809 * If @p_handle is not NULL @file_priv must also not be NULL. Creates
1810 * a userspace handle if @p_handle is not NULL, otherwise not.
1813 int vmw_execbuf_fence_commands(struct drm_file *file_priv,
1814 struct vmw_private *dev_priv,
1815 struct vmw_fence_obj **p_fence,
1820 bool synced = false;
1822 /* p_handle implies file_priv. */
1823 BUG_ON(p_handle != NULL && file_priv == NULL);
1825 ret = vmw_fifo_send_fence(dev_priv, &sequence);
1826 if (unlikely(ret != 0)) {
1827 DRM_ERROR("Fence submission error. Syncing.\n");
1831 if (p_handle != NULL)
1832 ret = vmw_user_fence_create(file_priv, dev_priv->fman,
1834 DRM_VMW_FENCE_FLAG_EXEC,
1837 ret = vmw_fence_create(dev_priv->fman, sequence,
1838 DRM_VMW_FENCE_FLAG_EXEC,
1841 if (unlikely(ret != 0 && !synced)) {
1842 (void) vmw_fallback_wait(dev_priv, false, false,
1844 VMW_FENCE_WAIT_TIMEOUT);
1852 * vmw_execbuf_copy_fence_user - copy fence object information to
1855 * @dev_priv: Pointer to a vmw_private struct.
1856 * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
1857 * @ret: Return value from fence object creation.
1858 * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
1859 * which the information should be copied.
1860 * @fence: Pointer to the fenc object.
1861 * @fence_handle: User-space fence handle.
1863 * This function copies fence information to user-space. If copying fails,
1864 * The user-space struct drm_vmw_fence_rep::error member is hopefully
1865 * left untouched, and if it's preloaded with an -EFAULT by user-space,
1866 * the error will hopefully be detected.
1867 * Also if copying fails, user-space will be unable to signal the fence
1868 * object so we wait for it immediately, and then unreference the
1869 * user-space reference.
1872 vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
1873 struct vmw_fpriv *vmw_fp,
1875 struct drm_vmw_fence_rep __user *user_fence_rep,
1876 struct vmw_fence_obj *fence,
1877 uint32_t fence_handle)
1879 struct drm_vmw_fence_rep fence_rep;
1881 if (user_fence_rep == NULL)
1884 memset(&fence_rep, 0, sizeof(fence_rep));
1886 fence_rep.error = ret;
1888 BUG_ON(fence == NULL);
1890 fence_rep.handle = fence_handle;
1891 fence_rep.seqno = fence->seqno;
1892 vmw_update_seqno(dev_priv, &dev_priv->fifo);
1893 fence_rep.passed_seqno = dev_priv->last_read_seqno;
1897 * copy_to_user errors will be detected by user space not
1898 * seeing fence_rep::error filled in. Typically
1899 * user-space would have pre-set that member to -EFAULT.
1901 ret = copy_to_user(user_fence_rep, &fence_rep,
1905 * User-space lost the fence object. We need to sync
1906 * and unreference the handle.
1908 if (unlikely(ret != 0) && (fence_rep.error == 0)) {
1909 ttm_ref_object_base_unref(vmw_fp->tfile,
1910 fence_handle, TTM_REF_USAGE);
1911 DRM_ERROR("Fence copy error. Syncing.\n");
1912 (void) vmw_fence_obj_wait(fence, fence->signal_mask,
1914 VMW_FENCE_WAIT_TIMEOUT);
1918 int vmw_execbuf_process(struct drm_file *file_priv,
1919 struct vmw_private *dev_priv,
1920 void __user *user_commands,
1921 void *kernel_commands,
1922 uint32_t command_size,
1923 uint64_t throttle_us,
1924 struct drm_vmw_fence_rep __user *user_fence_rep,
1925 struct vmw_fence_obj **out_fence)
1927 struct vmw_sw_context *sw_context = &dev_priv->ctx;
1928 struct vmw_fence_obj *fence = NULL;
1929 struct vmw_resource *error_resource;
1930 struct list_head resource_list;
1931 struct ww_acquire_ctx ticket;
1936 ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
1937 if (unlikely(ret != 0))
1938 return -ERESTARTSYS;
1940 if (kernel_commands == NULL) {
1941 sw_context->kernel = false;
1943 ret = vmw_resize_cmd_bounce(sw_context, command_size);
1944 if (unlikely(ret != 0))
1948 ret = copy_from_user(sw_context->cmd_bounce,
1949 user_commands, command_size);
1951 if (unlikely(ret != 0)) {
1953 DRM_ERROR("Failed copying commands.\n");
1956 kernel_commands = sw_context->cmd_bounce;
1958 sw_context->kernel = true;
1960 sw_context->tfile = vmw_fpriv(file_priv)->tfile;
1961 sw_context->cur_reloc = 0;
1962 sw_context->cur_val_buf = 0;
1963 sw_context->fence_flags = 0;
1964 INIT_LIST_HEAD(&sw_context->resource_list);
1965 sw_context->cur_query_bo = dev_priv->pinned_bo;
1966 sw_context->last_query_ctx = NULL;
1967 sw_context->needs_post_query_barrier = false;
1968 memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
1969 INIT_LIST_HEAD(&sw_context->validate_nodes);
1970 INIT_LIST_HEAD(&sw_context->res_relocations);
1971 if (!sw_context->res_ht_initialized) {
1972 ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
1973 if (unlikely(ret != 0))
1975 sw_context->res_ht_initialized = true;
1978 INIT_LIST_HEAD(&resource_list);
1979 ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
1981 if (unlikely(ret != 0))
1984 ret = vmw_resources_reserve(sw_context);
1985 if (unlikely(ret != 0))
1988 ret = ttm_eu_reserve_buffers(&ticket, &sw_context->validate_nodes);
1989 if (unlikely(ret != 0))
1992 ret = vmw_validate_buffers(dev_priv, sw_context);
1993 if (unlikely(ret != 0))
1996 ret = vmw_resources_validate(sw_context);
1997 if (unlikely(ret != 0))
2001 ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
2004 if (unlikely(ret != 0))
2008 cmd = vmw_fifo_reserve(dev_priv, command_size);
2009 if (unlikely(cmd == NULL)) {
2010 DRM_ERROR("Failed reserving fifo space for commands.\n");
2015 vmw_apply_relocations(sw_context);
2016 memcpy(cmd, kernel_commands, command_size);
2018 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
2019 vmw_resource_relocations_free(&sw_context->res_relocations);
2021 vmw_fifo_commit(dev_priv, command_size);
2023 vmw_query_bo_switch_commit(dev_priv, sw_context);
2024 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
2026 (user_fence_rep) ? &handle : NULL);
2028 * This error is harmless, because if fence submission fails,
2029 * vmw_fifo_send_fence will sync. The error will be propagated to
2030 * user-space in @fence_rep
2034 DRM_ERROR("Fence submission error. Syncing.\n");
2036 vmw_resource_list_unreserve(&sw_context->resource_list, false);
2037 ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
2040 if (unlikely(dev_priv->pinned_bo != NULL &&
2041 !dev_priv->query_cid_valid))
2042 __vmw_execbuf_release_pinned_bo(dev_priv, fence);
2044 vmw_clear_validations(sw_context);
2045 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
2046 user_fence_rep, fence, handle);
2048 /* Don't unreference when handing fence out */
2049 if (unlikely(out_fence != NULL)) {
2052 } else if (likely(fence != NULL)) {
2053 vmw_fence_obj_unreference(&fence);
2056 list_splice_init(&sw_context->resource_list, &resource_list);
2057 mutex_unlock(&dev_priv->cmdbuf_mutex);
2060 * Unreference resources outside of the cmdbuf_mutex to
2061 * avoid deadlocks in resource destruction paths.
2063 vmw_resource_list_unreference(&resource_list);
2068 vmw_resource_relocations_free(&sw_context->res_relocations);
2069 vmw_free_relocations(sw_context);
2070 ttm_eu_backoff_reservation(&ticket, &sw_context->validate_nodes);
2071 vmw_resource_list_unreserve(&sw_context->resource_list, true);
2072 vmw_clear_validations(sw_context);
2073 if (unlikely(dev_priv->pinned_bo != NULL &&
2074 !dev_priv->query_cid_valid))
2075 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
2077 list_splice_init(&sw_context->resource_list, &resource_list);
2078 error_resource = sw_context->error_resource;
2079 sw_context->error_resource = NULL;
2080 mutex_unlock(&dev_priv->cmdbuf_mutex);
2083 * Unreference resources outside of the cmdbuf_mutex to
2084 * avoid deadlocks in resource destruction paths.
2086 vmw_resource_list_unreference(&resource_list);
2087 if (unlikely(error_resource != NULL))
2088 vmw_resource_unreference(&error_resource);
2094 * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
2096 * @dev_priv: The device private structure.
2098 * This function is called to idle the fifo and unpin the query buffer
2099 * if the normal way to do this hits an error, which should typically be
2102 static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
2104 DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
2106 (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
2107 vmw_bo_pin(dev_priv->pinned_bo, false);
2108 vmw_bo_pin(dev_priv->dummy_query_bo, false);
2109 dev_priv->dummy_query_bo_pinned = false;
2114 * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
2117 * @dev_priv: The device private structure.
2118 * @fence: If non-NULL should point to a struct vmw_fence_obj issued
2119 * _after_ a query barrier that flushes all queries touching the current
2120 * buffer pointed to by @dev_priv->pinned_bo
2122 * This function should be used to unpin the pinned query bo, or
2123 * as a query barrier when we need to make sure that all queries have
2124 * finished before the next fifo command. (For example on hardware
2125 * context destructions where the hardware may otherwise leak unfinished
2128 * This function does not return any failure codes, but make attempts
2129 * to do safe unpinning in case of errors.
2131 * The function will synchronize on the previous query barrier, and will
2132 * thus not finish until that barrier has executed.
2134 * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
2135 * before calling this function.
2137 void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
2138 struct vmw_fence_obj *fence)
2141 struct list_head validate_list;
2142 struct ttm_validate_buffer pinned_val, query_val;
2143 struct vmw_fence_obj *lfence = NULL;
2144 struct ww_acquire_ctx ticket;
2146 if (dev_priv->pinned_bo == NULL)
2149 INIT_LIST_HEAD(&validate_list);
2151 pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
2152 list_add_tail(&pinned_val.head, &validate_list);
2154 query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
2155 list_add_tail(&query_val.head, &validate_list);
2158 ret = ttm_eu_reserve_buffers(&ticket, &validate_list);
2159 } while (ret == -ERESTARTSYS);
2161 if (unlikely(ret != 0)) {
2162 vmw_execbuf_unpin_panic(dev_priv);
2163 goto out_no_reserve;
2166 if (dev_priv->query_cid_valid) {
2167 BUG_ON(fence != NULL);
2168 ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
2169 if (unlikely(ret != 0)) {
2170 vmw_execbuf_unpin_panic(dev_priv);
2173 dev_priv->query_cid_valid = false;
2176 vmw_bo_pin(dev_priv->pinned_bo, false);
2177 vmw_bo_pin(dev_priv->dummy_query_bo, false);
2178 dev_priv->dummy_query_bo_pinned = false;
2180 if (fence == NULL) {
2181 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
2185 ttm_eu_fence_buffer_objects(&ticket, &validate_list, (void *) fence);
2187 vmw_fence_obj_unreference(&lfence);
2189 ttm_bo_unref(&query_val.bo);
2190 ttm_bo_unref(&pinned_val.bo);
2191 ttm_bo_unref(&dev_priv->pinned_bo);
2197 ttm_eu_backoff_reservation(&ticket, &validate_list);
2199 ttm_bo_unref(&query_val.bo);
2200 ttm_bo_unref(&pinned_val.bo);
2201 ttm_bo_unref(&dev_priv->pinned_bo);
2205 * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
2208 * @dev_priv: The device private structure.
2210 * This function should be used to unpin the pinned query bo, or
2211 * as a query barrier when we need to make sure that all queries have
2212 * finished before the next fifo command. (For example on hardware
2213 * context destructions where the hardware may otherwise leak unfinished
2216 * This function does not return any failure codes, but make attempts
2217 * to do safe unpinning in case of errors.
2219 * The function will synchronize on the previous query barrier, and will
2220 * thus not finish until that barrier has executed.
2222 void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
2224 mutex_lock(&dev_priv->cmdbuf_mutex);
2225 if (dev_priv->query_cid_valid)
2226 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
2227 mutex_unlock(&dev_priv->cmdbuf_mutex);
2231 int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
2232 struct drm_file *file_priv)
2234 struct vmw_private *dev_priv = vmw_priv(dev);
2235 struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
2236 struct vmw_master *vmaster = vmw_master(file_priv->master);
2240 * This will allow us to extend the ioctl argument while
2241 * maintaining backwards compatibility:
2242 * We take different code paths depending on the value of
2246 if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
2247 DRM_ERROR("Incorrect execbuf version.\n");
2248 DRM_ERROR("You're running outdated experimental "
2249 "vmwgfx user-space drivers.");
2253 ret = ttm_read_lock(&vmaster->lock, true);
2254 if (unlikely(ret != 0))
2257 ret = vmw_execbuf_process(file_priv, dev_priv,
2258 (void __user *)(unsigned long)arg->commands,
2259 NULL, arg->command_size, arg->throttle_us,
2260 (void __user *)(unsigned long)arg->fence_rep,
2263 if (unlikely(ret != 0))
2266 vmw_kms_cursor_post_execbuf(dev_priv);
2269 ttm_read_unlock(&vmaster->lock);