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 * @staged_bindings: If @res is a context, tracks bindings set up during
58 * the command batch. Otherwise NULL.
59 * @new_backup_offset: New backup buffer offset if @new_backup is non-NUll.
60 * @first_usage: Set to true the first time the resource is referenced in
62 * @no_buffer_needed: Resources do not need to allocate buffer backup on
63 * reservation. The command stream will provide one.
65 struct vmw_resource_val_node {
66 struct list_head head;
67 struct drm_hash_item hash;
68 struct vmw_resource *res;
69 struct vmw_dma_buffer *new_backup;
70 struct vmw_ctx_binding_state *staged_bindings;
71 unsigned long new_backup_offset;
73 bool no_buffer_needed;
77 * struct vmw_cmd_entry - Describe a command for the verifier
79 * @user_allow: Whether allowed from the execbuf ioctl.
80 * @gb_disable: Whether disabled if guest-backed objects are available.
81 * @gb_enable: Whether enabled iff guest-backed objects are available.
83 struct vmw_cmd_entry {
84 int (*func) (struct vmw_private *, struct vmw_sw_context *,
91 #define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable) \
92 [(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
93 (_gb_disable), (_gb_enable)}
96 * vmw_resource_unreserve - unreserve resources previously reserved for
99 * @list_head: list of resources to unreserve.
100 * @backoff: Whether command submission failed.
102 static void vmw_resource_list_unreserve(struct list_head *list,
105 struct vmw_resource_val_node *val;
107 list_for_each_entry(val, list, head) {
108 struct vmw_resource *res = val->res;
109 struct vmw_dma_buffer *new_backup =
110 backoff ? NULL : val->new_backup;
113 * Transfer staged context bindings to the
114 * persistent context binding tracker.
116 if (unlikely(val->staged_bindings)) {
118 vmw_context_binding_state_transfer
119 (val->res, val->staged_bindings);
121 kfree(val->staged_bindings);
122 val->staged_bindings = NULL;
124 vmw_resource_unreserve(res, new_backup,
125 val->new_backup_offset);
126 vmw_dmabuf_unreference(&val->new_backup);
132 * vmw_resource_val_add - Add a resource to the software context's
133 * resource list if it's not already on it.
135 * @sw_context: Pointer to the software context.
136 * @res: Pointer to the resource.
137 * @p_node On successful return points to a valid pointer to a
138 * struct vmw_resource_val_node, if non-NULL on entry.
140 static int vmw_resource_val_add(struct vmw_sw_context *sw_context,
141 struct vmw_resource *res,
142 struct vmw_resource_val_node **p_node)
144 struct vmw_resource_val_node *node;
145 struct drm_hash_item *hash;
148 if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) res,
150 node = container_of(hash, struct vmw_resource_val_node, hash);
151 node->first_usage = false;
152 if (unlikely(p_node != NULL))
157 node = kzalloc(sizeof(*node), GFP_KERNEL);
158 if (unlikely(node == NULL)) {
159 DRM_ERROR("Failed to allocate a resource validation "
164 node->hash.key = (unsigned long) res;
165 ret = drm_ht_insert_item(&sw_context->res_ht, &node->hash);
166 if (unlikely(ret != 0)) {
167 DRM_ERROR("Failed to initialize a resource validation "
172 list_add_tail(&node->head, &sw_context->resource_list);
173 node->res = vmw_resource_reference(res);
174 node->first_usage = true;
176 if (unlikely(p_node != NULL))
183 * vmw_resource_relocation_add - Add a relocation to the relocation list
185 * @list: Pointer to head of relocation list.
186 * @res: The resource.
187 * @offset: Offset into the command buffer currently being parsed where the
188 * id that needs fixup is located. Granularity is 4 bytes.
190 static int vmw_resource_relocation_add(struct list_head *list,
191 const struct vmw_resource *res,
192 unsigned long offset)
194 struct vmw_resource_relocation *rel;
196 rel = kmalloc(sizeof(*rel), GFP_KERNEL);
197 if (unlikely(rel == NULL)) {
198 DRM_ERROR("Failed to allocate a resource relocation.\n");
203 rel->offset = offset;
204 list_add_tail(&rel->head, list);
210 * vmw_resource_relocations_free - Free all relocations on a list
212 * @list: Pointer to the head of the relocation list.
214 static void vmw_resource_relocations_free(struct list_head *list)
216 struct vmw_resource_relocation *rel, *n;
218 list_for_each_entry_safe(rel, n, list, head) {
219 list_del(&rel->head);
225 * vmw_resource_relocations_apply - Apply all relocations on a list
227 * @cb: Pointer to the start of the command buffer bein patch. This need
228 * not be the same buffer as the one being parsed when the relocation
229 * list was built, but the contents must be the same modulo the
231 * @list: Pointer to the head of the relocation list.
233 static void vmw_resource_relocations_apply(uint32_t *cb,
234 struct list_head *list)
236 struct vmw_resource_relocation *rel;
238 list_for_each_entry(rel, list, head)
239 cb[rel->offset] = rel->res->id;
242 static int vmw_cmd_invalid(struct vmw_private *dev_priv,
243 struct vmw_sw_context *sw_context,
244 SVGA3dCmdHeader *header)
246 return capable(CAP_SYS_ADMIN) ? : -EINVAL;
249 static int vmw_cmd_ok(struct vmw_private *dev_priv,
250 struct vmw_sw_context *sw_context,
251 SVGA3dCmdHeader *header)
257 * vmw_bo_to_validate_list - add a bo to a validate list
259 * @sw_context: The software context used for this command submission batch.
260 * @bo: The buffer object to add.
261 * @validate_as_mob: Validate this buffer as a MOB.
262 * @p_val_node: If non-NULL Will be updated with the validate node number
265 * Returns -EINVAL if the limit of number of buffer objects per command
266 * submission is reached.
268 static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
269 struct ttm_buffer_object *bo,
270 bool validate_as_mob,
271 uint32_t *p_val_node)
274 struct vmw_validate_buffer *vval_buf;
275 struct ttm_validate_buffer *val_buf;
276 struct drm_hash_item *hash;
279 if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) bo,
281 vval_buf = container_of(hash, struct vmw_validate_buffer,
283 if (unlikely(vval_buf->validate_as_mob != validate_as_mob)) {
284 DRM_ERROR("Inconsistent buffer usage.\n");
287 val_buf = &vval_buf->base;
288 val_node = vval_buf - sw_context->val_bufs;
290 val_node = sw_context->cur_val_buf;
291 if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
292 DRM_ERROR("Max number of DMA buffers per submission "
296 vval_buf = &sw_context->val_bufs[val_node];
297 vval_buf->hash.key = (unsigned long) bo;
298 ret = drm_ht_insert_item(&sw_context->res_ht, &vval_buf->hash);
299 if (unlikely(ret != 0)) {
300 DRM_ERROR("Failed to initialize a buffer validation "
304 ++sw_context->cur_val_buf;
305 val_buf = &vval_buf->base;
306 val_buf->bo = ttm_bo_reference(bo);
307 val_buf->reserved = false;
308 list_add_tail(&val_buf->head, &sw_context->validate_nodes);
309 vval_buf->validate_as_mob = validate_as_mob;
312 sw_context->fence_flags |= DRM_VMW_FENCE_FLAG_EXEC;
315 *p_val_node = val_node;
321 * vmw_resources_reserve - Reserve all resources on the sw_context's
324 * @sw_context: Pointer to the software context.
326 * Note that since vmware's command submission currently is protected by
327 * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
328 * since only a single thread at once will attempt this.
330 static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
332 struct vmw_resource_val_node *val;
335 list_for_each_entry(val, &sw_context->resource_list, head) {
336 struct vmw_resource *res = val->res;
338 ret = vmw_resource_reserve(res, val->no_buffer_needed);
339 if (unlikely(ret != 0))
343 struct ttm_buffer_object *bo = &res->backup->base;
345 ret = vmw_bo_to_validate_list
347 vmw_resource_needs_backup(res), NULL);
349 if (unlikely(ret != 0))
357 * vmw_resources_validate - Validate all resources on the sw_context's
360 * @sw_context: Pointer to the software context.
362 * Before this function is called, all resource backup buffers must have
365 static int vmw_resources_validate(struct vmw_sw_context *sw_context)
367 struct vmw_resource_val_node *val;
370 list_for_each_entry(val, &sw_context->resource_list, head) {
371 struct vmw_resource *res = val->res;
373 ret = vmw_resource_validate(res);
374 if (unlikely(ret != 0)) {
375 if (ret != -ERESTARTSYS)
376 DRM_ERROR("Failed to validate resource.\n");
384 * vmw_cmd_res_check - Check that a resource is present and if so, put it
385 * on the resource validate list unless it's already there.
387 * @dev_priv: Pointer to a device private structure.
388 * @sw_context: Pointer to the software context.
389 * @res_type: Resource type.
390 * @converter: User-space visisble type specific information.
391 * @id: Pointer to the location in the command buffer currently being
392 * parsed from where the user-space resource id handle is located.
394 static int vmw_cmd_res_check(struct vmw_private *dev_priv,
395 struct vmw_sw_context *sw_context,
396 enum vmw_res_type res_type,
397 const struct vmw_user_resource_conv *converter,
399 struct vmw_resource_val_node **p_val)
401 struct vmw_res_cache_entry *rcache =
402 &sw_context->res_cache[res_type];
403 struct vmw_resource *res;
404 struct vmw_resource_val_node *node;
407 if (*id == SVGA3D_INVALID_ID) {
410 if (res_type == vmw_res_context) {
411 DRM_ERROR("Illegal context invalid id.\n");
418 * Fastpath in case of repeated commands referencing the same
422 if (likely(rcache->valid && *id == rcache->handle)) {
423 const struct vmw_resource *res = rcache->res;
425 rcache->node->first_usage = false;
427 *p_val = rcache->node;
429 return vmw_resource_relocation_add
430 (&sw_context->res_relocations, res,
431 id - sw_context->buf_start);
434 ret = vmw_user_resource_lookup_handle(dev_priv,
439 if (unlikely(ret != 0)) {
440 DRM_ERROR("Could not find or use resource 0x%08x.\n",
446 rcache->valid = true;
448 rcache->handle = *id;
450 ret = vmw_resource_relocation_add(&sw_context->res_relocations,
452 id - sw_context->buf_start);
453 if (unlikely(ret != 0))
456 ret = vmw_resource_val_add(sw_context, res, &node);
457 if (unlikely(ret != 0))
464 if (node->first_usage && res_type == vmw_res_context) {
465 node->staged_bindings =
466 kzalloc(sizeof(*node->staged_bindings), GFP_KERNEL);
467 if (node->staged_bindings == NULL) {
468 DRM_ERROR("Failed to allocate context binding "
472 INIT_LIST_HEAD(&node->staged_bindings->list);
475 vmw_resource_unreference(&res);
479 BUG_ON(sw_context->error_resource != NULL);
480 sw_context->error_resource = res;
486 * vmw_cmd_cid_check - Check a command header for valid context information.
488 * @dev_priv: Pointer to a device private structure.
489 * @sw_context: Pointer to the software context.
490 * @header: A command header with an embedded user-space context handle.
492 * Convenience function: Call vmw_cmd_res_check with the user-space context
493 * handle embedded in @header.
495 static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
496 struct vmw_sw_context *sw_context,
497 SVGA3dCmdHeader *header)
500 SVGA3dCmdHeader header;
504 cmd = container_of(header, struct vmw_cid_cmd, header);
505 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
506 user_context_converter, &cmd->cid, NULL);
509 static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
510 struct vmw_sw_context *sw_context,
511 SVGA3dCmdHeader *header)
514 SVGA3dCmdHeader header;
515 SVGA3dCmdSetRenderTarget body;
517 struct vmw_resource_val_node *ctx_node;
518 struct vmw_resource_val_node *res_node;
521 cmd = container_of(header, struct vmw_sid_cmd, header);
523 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
524 user_context_converter, &cmd->body.cid,
526 if (unlikely(ret != 0))
529 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
530 user_surface_converter,
531 &cmd->body.target.sid, &res_node);
532 if (unlikely(ret != 0))
535 if (dev_priv->has_mob) {
536 struct vmw_ctx_bindinfo bi;
538 bi.ctx = ctx_node->res;
539 bi.res = res_node ? res_node->res : NULL;
540 bi.bt = vmw_ctx_binding_rt;
541 bi.i1.rt_type = cmd->body.type;
542 return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
548 static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
549 struct vmw_sw_context *sw_context,
550 SVGA3dCmdHeader *header)
553 SVGA3dCmdHeader header;
554 SVGA3dCmdSurfaceCopy body;
558 cmd = container_of(header, struct vmw_sid_cmd, header);
559 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
560 user_surface_converter,
561 &cmd->body.src.sid, NULL);
562 if (unlikely(ret != 0))
564 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
565 user_surface_converter,
566 &cmd->body.dest.sid, NULL);
569 static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
570 struct vmw_sw_context *sw_context,
571 SVGA3dCmdHeader *header)
574 SVGA3dCmdHeader header;
575 SVGA3dCmdSurfaceStretchBlt body;
579 cmd = container_of(header, struct vmw_sid_cmd, header);
580 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
581 user_surface_converter,
582 &cmd->body.src.sid, NULL);
583 if (unlikely(ret != 0))
585 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
586 user_surface_converter,
587 &cmd->body.dest.sid, NULL);
590 static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
591 struct vmw_sw_context *sw_context,
592 SVGA3dCmdHeader *header)
595 SVGA3dCmdHeader header;
596 SVGA3dCmdBlitSurfaceToScreen body;
599 cmd = container_of(header, struct vmw_sid_cmd, header);
601 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
602 user_surface_converter,
603 &cmd->body.srcImage.sid, NULL);
606 static int vmw_cmd_present_check(struct vmw_private *dev_priv,
607 struct vmw_sw_context *sw_context,
608 SVGA3dCmdHeader *header)
611 SVGA3dCmdHeader header;
612 SVGA3dCmdPresent body;
616 cmd = container_of(header, struct vmw_sid_cmd, header);
618 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
619 user_surface_converter, &cmd->body.sid,
624 * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
626 * @dev_priv: The device private structure.
627 * @new_query_bo: The new buffer holding query results.
628 * @sw_context: The software context used for this command submission.
630 * This function checks whether @new_query_bo is suitable for holding
631 * query results, and if another buffer currently is pinned for query
632 * results. If so, the function prepares the state of @sw_context for
633 * switching pinned buffers after successful submission of the current
636 static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
637 struct ttm_buffer_object *new_query_bo,
638 struct vmw_sw_context *sw_context)
640 struct vmw_res_cache_entry *ctx_entry =
641 &sw_context->res_cache[vmw_res_context];
644 BUG_ON(!ctx_entry->valid);
645 sw_context->last_query_ctx = ctx_entry->res;
647 if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
649 if (unlikely(new_query_bo->num_pages > 4)) {
650 DRM_ERROR("Query buffer too large.\n");
654 if (unlikely(sw_context->cur_query_bo != NULL)) {
655 sw_context->needs_post_query_barrier = true;
656 ret = vmw_bo_to_validate_list(sw_context,
657 sw_context->cur_query_bo,
658 dev_priv->has_mob, NULL);
659 if (unlikely(ret != 0))
662 sw_context->cur_query_bo = new_query_bo;
664 ret = vmw_bo_to_validate_list(sw_context,
665 dev_priv->dummy_query_bo,
666 dev_priv->has_mob, NULL);
667 if (unlikely(ret != 0))
677 * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
679 * @dev_priv: The device private structure.
680 * @sw_context: The software context used for this command submission batch.
682 * This function will check if we're switching query buffers, and will then,
683 * issue a dummy occlusion query wait used as a query barrier. When the fence
684 * object following that query wait has signaled, we are sure that all
685 * preceding queries have finished, and the old query buffer can be unpinned.
686 * However, since both the new query buffer and the old one are fenced with
687 * that fence, we can do an asynchronus unpin now, and be sure that the
688 * old query buffer won't be moved until the fence has signaled.
690 * As mentioned above, both the new - and old query buffers need to be fenced
691 * using a sequence emitted *after* calling this function.
693 static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
694 struct vmw_sw_context *sw_context)
697 * The validate list should still hold references to all
701 if (sw_context->needs_post_query_barrier) {
702 struct vmw_res_cache_entry *ctx_entry =
703 &sw_context->res_cache[vmw_res_context];
704 struct vmw_resource *ctx;
707 BUG_ON(!ctx_entry->valid);
708 ctx = ctx_entry->res;
710 ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
712 if (unlikely(ret != 0))
713 DRM_ERROR("Out of fifo space for dummy query.\n");
716 if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
717 if (dev_priv->pinned_bo) {
718 vmw_bo_pin(dev_priv->pinned_bo, false);
719 ttm_bo_unref(&dev_priv->pinned_bo);
722 if (!sw_context->needs_post_query_barrier) {
723 vmw_bo_pin(sw_context->cur_query_bo, true);
726 * We pin also the dummy_query_bo buffer so that we
727 * don't need to validate it when emitting
728 * dummy queries in context destroy paths.
731 vmw_bo_pin(dev_priv->dummy_query_bo, true);
732 dev_priv->dummy_query_bo_pinned = true;
734 BUG_ON(sw_context->last_query_ctx == NULL);
735 dev_priv->query_cid = sw_context->last_query_ctx->id;
736 dev_priv->query_cid_valid = true;
737 dev_priv->pinned_bo =
738 ttm_bo_reference(sw_context->cur_query_bo);
744 * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
745 * handle to a MOB id.
747 * @dev_priv: Pointer to a device private structure.
748 * @sw_context: The software context used for this command batch validation.
749 * @id: Pointer to the user-space handle to be translated.
750 * @vmw_bo_p: Points to a location that, on successful return will carry
751 * a reference-counted pointer to the DMA buffer identified by the
752 * user-space handle in @id.
754 * This function saves information needed to translate a user-space buffer
755 * handle to a MOB id. The translation does not take place immediately, but
756 * during a call to vmw_apply_relocations(). This function builds a relocation
757 * list and a list of buffers to validate. The former needs to be freed using
758 * either vmw_apply_relocations() or vmw_free_relocations(). The latter
759 * needs to be freed using vmw_clear_validations.
761 static int vmw_translate_mob_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 = *id;
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 MOB buffer.\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++];
788 reloc->location = NULL;
790 ret = vmw_bo_to_validate_list(sw_context, bo, true, &reloc->index);
791 if (unlikely(ret != 0))
798 vmw_dmabuf_unreference(&vmw_bo);
804 * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
805 * handle to a valid SVGAGuestPtr
807 * @dev_priv: Pointer to a device private structure.
808 * @sw_context: The software context used for this command batch validation.
809 * @ptr: Pointer to the user-space handle to be translated.
810 * @vmw_bo_p: Points to a location that, on successful return will carry
811 * a reference-counted pointer to the DMA buffer identified by the
812 * user-space handle in @id.
814 * This function saves information needed to translate a user-space buffer
815 * handle to a valid SVGAGuestPtr. The translation does not take place
816 * immediately, but during a call to vmw_apply_relocations().
817 * This function builds a relocation list and a list of buffers to validate.
818 * The former needs to be freed using either vmw_apply_relocations() or
819 * vmw_free_relocations(). The latter needs to be freed using
820 * vmw_clear_validations.
822 static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
823 struct vmw_sw_context *sw_context,
825 struct vmw_dma_buffer **vmw_bo_p)
827 struct vmw_dma_buffer *vmw_bo = NULL;
828 struct ttm_buffer_object *bo;
829 uint32_t handle = ptr->gmrId;
830 struct vmw_relocation *reloc;
833 ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
834 if (unlikely(ret != 0)) {
835 DRM_ERROR("Could not find or use GMR region.\n");
840 if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
841 DRM_ERROR("Max number relocations per submission"
847 reloc = &sw_context->relocs[sw_context->cur_reloc++];
848 reloc->location = ptr;
850 ret = vmw_bo_to_validate_list(sw_context, bo, false, &reloc->index);
851 if (unlikely(ret != 0))
858 vmw_dmabuf_unreference(&vmw_bo);
864 * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_QUERY command.
866 * @dev_priv: Pointer to a device private struct.
867 * @sw_context: The software context used for this command submission.
868 * @header: Pointer to the command header in the command stream.
870 static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
871 struct vmw_sw_context *sw_context,
872 SVGA3dCmdHeader *header)
874 struct vmw_begin_gb_query_cmd {
875 SVGA3dCmdHeader header;
876 SVGA3dCmdBeginGBQuery q;
879 cmd = container_of(header, struct vmw_begin_gb_query_cmd,
882 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
883 user_context_converter, &cmd->q.cid,
888 * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
890 * @dev_priv: Pointer to a device private struct.
891 * @sw_context: The software context used for this command submission.
892 * @header: Pointer to the command header in the command stream.
894 static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
895 struct vmw_sw_context *sw_context,
896 SVGA3dCmdHeader *header)
898 struct vmw_begin_query_cmd {
899 SVGA3dCmdHeader header;
900 SVGA3dCmdBeginQuery q;
903 cmd = container_of(header, struct vmw_begin_query_cmd,
906 if (unlikely(dev_priv->has_mob)) {
908 SVGA3dCmdHeader header;
909 SVGA3dCmdBeginGBQuery q;
912 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
914 gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
915 gb_cmd.header.size = cmd->header.size;
916 gb_cmd.q.cid = cmd->q.cid;
917 gb_cmd.q.type = cmd->q.type;
919 memcpy(cmd, &gb_cmd, sizeof(*cmd));
920 return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
923 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
924 user_context_converter, &cmd->q.cid,
929 * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
931 * @dev_priv: Pointer to a device private struct.
932 * @sw_context: The software context used for this command submission.
933 * @header: Pointer to the command header in the command stream.
935 static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
936 struct vmw_sw_context *sw_context,
937 SVGA3dCmdHeader *header)
939 struct vmw_dma_buffer *vmw_bo;
940 struct vmw_query_cmd {
941 SVGA3dCmdHeader header;
942 SVGA3dCmdEndGBQuery q;
946 cmd = container_of(header, struct vmw_query_cmd, header);
947 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
948 if (unlikely(ret != 0))
951 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
954 if (unlikely(ret != 0))
957 ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
959 vmw_dmabuf_unreference(&vmw_bo);
964 * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
966 * @dev_priv: Pointer to a device private struct.
967 * @sw_context: The software context used for this command submission.
968 * @header: Pointer to the command header in the command stream.
970 static int vmw_cmd_end_query(struct vmw_private *dev_priv,
971 struct vmw_sw_context *sw_context,
972 SVGA3dCmdHeader *header)
974 struct vmw_dma_buffer *vmw_bo;
975 struct vmw_query_cmd {
976 SVGA3dCmdHeader header;
981 cmd = container_of(header, struct vmw_query_cmd, header);
982 if (dev_priv->has_mob) {
984 SVGA3dCmdHeader header;
985 SVGA3dCmdEndGBQuery q;
988 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
990 gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
991 gb_cmd.header.size = cmd->header.size;
992 gb_cmd.q.cid = cmd->q.cid;
993 gb_cmd.q.type = cmd->q.type;
994 gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
995 gb_cmd.q.offset = cmd->q.guestResult.offset;
997 memcpy(cmd, &gb_cmd, sizeof(*cmd));
998 return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
1001 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1002 if (unlikely(ret != 0))
1005 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1006 &cmd->q.guestResult,
1008 if (unlikely(ret != 0))
1011 ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
1013 vmw_dmabuf_unreference(&vmw_bo);
1018 * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
1020 * @dev_priv: Pointer to a device private struct.
1021 * @sw_context: The software context used for this command submission.
1022 * @header: Pointer to the command header in the command stream.
1024 static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
1025 struct vmw_sw_context *sw_context,
1026 SVGA3dCmdHeader *header)
1028 struct vmw_dma_buffer *vmw_bo;
1029 struct vmw_query_cmd {
1030 SVGA3dCmdHeader header;
1031 SVGA3dCmdWaitForGBQuery q;
1035 cmd = container_of(header, struct vmw_query_cmd, header);
1036 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1037 if (unlikely(ret != 0))
1040 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
1043 if (unlikely(ret != 0))
1046 vmw_dmabuf_unreference(&vmw_bo);
1051 * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
1053 * @dev_priv: Pointer to a device private struct.
1054 * @sw_context: The software context used for this command submission.
1055 * @header: Pointer to the command header in the command stream.
1057 static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
1058 struct vmw_sw_context *sw_context,
1059 SVGA3dCmdHeader *header)
1061 struct vmw_dma_buffer *vmw_bo;
1062 struct vmw_query_cmd {
1063 SVGA3dCmdHeader header;
1064 SVGA3dCmdWaitForQuery q;
1068 cmd = container_of(header, struct vmw_query_cmd, header);
1069 if (dev_priv->has_mob) {
1071 SVGA3dCmdHeader header;
1072 SVGA3dCmdWaitForGBQuery q;
1075 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1077 gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
1078 gb_cmd.header.size = cmd->header.size;
1079 gb_cmd.q.cid = cmd->q.cid;
1080 gb_cmd.q.type = cmd->q.type;
1081 gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
1082 gb_cmd.q.offset = cmd->q.guestResult.offset;
1084 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1085 return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
1088 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1089 if (unlikely(ret != 0))
1092 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1093 &cmd->q.guestResult,
1095 if (unlikely(ret != 0))
1098 vmw_dmabuf_unreference(&vmw_bo);
1102 static int vmw_cmd_dma(struct vmw_private *dev_priv,
1103 struct vmw_sw_context *sw_context,
1104 SVGA3dCmdHeader *header)
1106 struct vmw_dma_buffer *vmw_bo = NULL;
1107 struct vmw_surface *srf = NULL;
1108 struct vmw_dma_cmd {
1109 SVGA3dCmdHeader header;
1110 SVGA3dCmdSurfaceDMA dma;
1114 cmd = container_of(header, struct vmw_dma_cmd, header);
1115 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1116 &cmd->dma.guest.ptr,
1118 if (unlikely(ret != 0))
1121 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1122 user_surface_converter, &cmd->dma.host.sid,
1124 if (unlikely(ret != 0)) {
1125 if (unlikely(ret != -ERESTARTSYS))
1126 DRM_ERROR("could not find surface for DMA.\n");
1127 goto out_no_surface;
1130 srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
1132 vmw_kms_cursor_snoop(srf, sw_context->tfile, &vmw_bo->base, header);
1135 vmw_dmabuf_unreference(&vmw_bo);
1139 static int vmw_cmd_draw(struct vmw_private *dev_priv,
1140 struct vmw_sw_context *sw_context,
1141 SVGA3dCmdHeader *header)
1143 struct vmw_draw_cmd {
1144 SVGA3dCmdHeader header;
1145 SVGA3dCmdDrawPrimitives body;
1147 SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
1148 (unsigned long)header + sizeof(*cmd));
1149 SVGA3dPrimitiveRange *range;
1154 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1155 if (unlikely(ret != 0))
1158 cmd = container_of(header, struct vmw_draw_cmd, header);
1159 maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
1161 if (unlikely(cmd->body.numVertexDecls > maxnum)) {
1162 DRM_ERROR("Illegal number of vertex declarations.\n");
1166 for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
1167 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1168 user_surface_converter,
1169 &decl->array.surfaceId, NULL);
1170 if (unlikely(ret != 0))
1174 maxnum = (header->size - sizeof(cmd->body) -
1175 cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
1176 if (unlikely(cmd->body.numRanges > maxnum)) {
1177 DRM_ERROR("Illegal number of index ranges.\n");
1181 range = (SVGA3dPrimitiveRange *) decl;
1182 for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
1183 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1184 user_surface_converter,
1185 &range->indexArray.surfaceId, NULL);
1186 if (unlikely(ret != 0))
1193 static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
1194 struct vmw_sw_context *sw_context,
1195 SVGA3dCmdHeader *header)
1197 struct vmw_tex_state_cmd {
1198 SVGA3dCmdHeader header;
1199 SVGA3dCmdSetTextureState state;
1202 SVGA3dTextureState *last_state = (SVGA3dTextureState *)
1203 ((unsigned long) header + header->size + sizeof(header));
1204 SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
1205 ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
1206 struct vmw_resource_val_node *ctx_node;
1207 struct vmw_resource_val_node *res_node;
1210 cmd = container_of(header, struct vmw_tex_state_cmd,
1213 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
1214 user_context_converter, &cmd->state.cid,
1216 if (unlikely(ret != 0))
1219 for (; cur_state < last_state; ++cur_state) {
1220 if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
1223 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1224 user_surface_converter,
1225 &cur_state->value, &res_node);
1226 if (unlikely(ret != 0))
1229 if (dev_priv->has_mob) {
1230 struct vmw_ctx_bindinfo bi;
1232 bi.ctx = ctx_node->res;
1233 bi.res = res_node ? res_node->res : NULL;
1234 bi.bt = vmw_ctx_binding_tex;
1235 bi.i1.texture_stage = cur_state->stage;
1236 vmw_context_binding_add(ctx_node->staged_bindings,
1244 static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
1245 struct vmw_sw_context *sw_context,
1248 struct vmw_dma_buffer *vmw_bo;
1253 SVGAFifoCmdDefineGMRFB body;
1256 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1259 if (unlikely(ret != 0))
1262 vmw_dmabuf_unreference(&vmw_bo);
1268 * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
1270 * @dev_priv: Pointer to a device private struct.
1271 * @sw_context: The software context being used for this batch.
1272 * @res_type: The resource type.
1273 * @converter: Information about user-space binding for this resource type.
1274 * @res_id: Pointer to the user-space resource handle in the command stream.
1275 * @buf_id: Pointer to the user-space backup buffer handle in the command
1277 * @backup_offset: Offset of backup into MOB.
1279 * This function prepares for registering a switch of backup buffers
1280 * in the resource metadata just prior to unreserving.
1282 static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
1283 struct vmw_sw_context *sw_context,
1284 enum vmw_res_type res_type,
1285 const struct vmw_user_resource_conv
1289 unsigned long backup_offset)
1292 struct vmw_dma_buffer *dma_buf;
1293 struct vmw_resource_val_node *val_node;
1295 ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
1296 converter, res_id, &val_node);
1297 if (unlikely(ret != 0))
1300 ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &dma_buf);
1301 if (unlikely(ret != 0))
1304 if (val_node->first_usage)
1305 val_node->no_buffer_needed = true;
1307 vmw_dmabuf_unreference(&val_node->new_backup);
1308 val_node->new_backup = dma_buf;
1309 val_node->new_backup_offset = backup_offset;
1315 * vmw_cmd_bind_gb_surface - Validate an SVGA_3D_CMD_BIND_GB_SURFACE
1318 * @dev_priv: Pointer to a device private struct.
1319 * @sw_context: The software context being used for this batch.
1320 * @header: Pointer to the command header in the command stream.
1322 static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
1323 struct vmw_sw_context *sw_context,
1324 SVGA3dCmdHeader *header)
1326 struct vmw_bind_gb_surface_cmd {
1327 SVGA3dCmdHeader header;
1328 SVGA3dCmdBindGBSurface body;
1331 cmd = container_of(header, struct vmw_bind_gb_surface_cmd, header);
1333 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
1334 user_surface_converter,
1335 &cmd->body.sid, &cmd->body.mobid,
1340 * vmw_cmd_update_gb_image - Validate an SVGA_3D_CMD_UPDATE_GB_IMAGE
1343 * @dev_priv: Pointer to a device private struct.
1344 * @sw_context: The software context being used for this batch.
1345 * @header: Pointer to the command header in the command stream.
1347 static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
1348 struct vmw_sw_context *sw_context,
1349 SVGA3dCmdHeader *header)
1351 struct vmw_gb_surface_cmd {
1352 SVGA3dCmdHeader header;
1353 SVGA3dCmdUpdateGBImage body;
1356 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1358 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1359 user_surface_converter,
1360 &cmd->body.image.sid, NULL);
1364 * vmw_cmd_update_gb_surface - Validate an SVGA_3D_CMD_UPDATE_GB_SURFACE
1367 * @dev_priv: Pointer to a device private struct.
1368 * @sw_context: The software context being used for this batch.
1369 * @header: Pointer to the command header in the command stream.
1371 static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
1372 struct vmw_sw_context *sw_context,
1373 SVGA3dCmdHeader *header)
1375 struct vmw_gb_surface_cmd {
1376 SVGA3dCmdHeader header;
1377 SVGA3dCmdUpdateGBSurface body;
1380 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1382 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1383 user_surface_converter,
1384 &cmd->body.sid, NULL);
1388 * vmw_cmd_readback_gb_image - Validate an SVGA_3D_CMD_READBACK_GB_IMAGE
1391 * @dev_priv: Pointer to a device private struct.
1392 * @sw_context: The software context being used for this batch.
1393 * @header: Pointer to the command header in the command stream.
1395 static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
1396 struct vmw_sw_context *sw_context,
1397 SVGA3dCmdHeader *header)
1399 struct vmw_gb_surface_cmd {
1400 SVGA3dCmdHeader header;
1401 SVGA3dCmdReadbackGBImage body;
1404 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1406 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1407 user_surface_converter,
1408 &cmd->body.image.sid, NULL);
1412 * vmw_cmd_readback_gb_surface - Validate an SVGA_3D_CMD_READBACK_GB_SURFACE
1415 * @dev_priv: Pointer to a device private struct.
1416 * @sw_context: The software context being used for this batch.
1417 * @header: Pointer to the command header in the command stream.
1419 static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
1420 struct vmw_sw_context *sw_context,
1421 SVGA3dCmdHeader *header)
1423 struct vmw_gb_surface_cmd {
1424 SVGA3dCmdHeader header;
1425 SVGA3dCmdReadbackGBSurface body;
1428 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1430 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1431 user_surface_converter,
1432 &cmd->body.sid, NULL);
1436 * vmw_cmd_invalidate_gb_image - Validate an SVGA_3D_CMD_INVALIDATE_GB_IMAGE
1439 * @dev_priv: Pointer to a device private struct.
1440 * @sw_context: The software context being used for this batch.
1441 * @header: Pointer to the command header in the command stream.
1443 static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
1444 struct vmw_sw_context *sw_context,
1445 SVGA3dCmdHeader *header)
1447 struct vmw_gb_surface_cmd {
1448 SVGA3dCmdHeader header;
1449 SVGA3dCmdInvalidateGBImage body;
1452 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1454 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1455 user_surface_converter,
1456 &cmd->body.image.sid, NULL);
1460 * vmw_cmd_invalidate_gb_surface - Validate an
1461 * SVGA_3D_CMD_INVALIDATE_GB_SURFACE command
1463 * @dev_priv: Pointer to a device private struct.
1464 * @sw_context: The software context being used for this batch.
1465 * @header: Pointer to the command header in the command stream.
1467 static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
1468 struct vmw_sw_context *sw_context,
1469 SVGA3dCmdHeader *header)
1471 struct vmw_gb_surface_cmd {
1472 SVGA3dCmdHeader header;
1473 SVGA3dCmdInvalidateGBSurface body;
1476 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1478 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1479 user_surface_converter,
1480 &cmd->body.sid, NULL);
1484 * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
1487 * @dev_priv: Pointer to a device private struct.
1488 * @sw_context: The software context being used for this batch.
1489 * @header: Pointer to the command header in the command stream.
1491 static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
1492 struct vmw_sw_context *sw_context,
1493 SVGA3dCmdHeader *header)
1495 struct vmw_set_shader_cmd {
1496 SVGA3dCmdHeader header;
1497 SVGA3dCmdSetShader body;
1499 struct vmw_resource_val_node *ctx_node;
1502 cmd = container_of(header, struct vmw_set_shader_cmd,
1505 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
1506 user_context_converter, &cmd->body.cid,
1508 if (unlikely(ret != 0))
1511 if (dev_priv->has_mob) {
1512 struct vmw_ctx_bindinfo bi;
1513 struct vmw_resource_val_node *res_node;
1515 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_shader,
1516 user_shader_converter,
1517 &cmd->body.shid, &res_node);
1518 if (unlikely(ret != 0))
1521 bi.ctx = ctx_node->res;
1522 bi.res = res_node ? res_node->res : NULL;
1523 bi.bt = vmw_ctx_binding_shader;
1524 bi.i1.shader_type = cmd->body.type;
1525 return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
1532 * vmw_cmd_bind_gb_shader - Validate an SVGA_3D_CMD_BIND_GB_SHADER
1535 * @dev_priv: Pointer to a device private struct.
1536 * @sw_context: The software context being used for this batch.
1537 * @header: Pointer to the command header in the command stream.
1539 static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
1540 struct vmw_sw_context *sw_context,
1541 SVGA3dCmdHeader *header)
1543 struct vmw_bind_gb_shader_cmd {
1544 SVGA3dCmdHeader header;
1545 SVGA3dCmdBindGBShader body;
1548 cmd = container_of(header, struct vmw_bind_gb_shader_cmd,
1551 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
1552 user_shader_converter,
1553 &cmd->body.shid, &cmd->body.mobid,
1554 cmd->body.offsetInBytes);
1557 static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
1558 struct vmw_sw_context *sw_context,
1559 void *buf, uint32_t *size)
1561 uint32_t size_remaining = *size;
1564 cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
1566 case SVGA_CMD_UPDATE:
1567 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
1569 case SVGA_CMD_DEFINE_GMRFB:
1570 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
1572 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
1573 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
1575 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
1576 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
1579 DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
1583 if (*size > size_remaining) {
1584 DRM_ERROR("Invalid SVGA command (size mismatch):"
1589 if (unlikely(!sw_context->kernel)) {
1590 DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
1594 if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
1595 return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
1600 static const struct vmw_cmd_entry const vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
1601 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
1602 false, false, false),
1603 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
1604 false, false, false),
1605 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
1606 true, false, false),
1607 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
1608 true, false, false),
1609 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
1610 true, false, false),
1611 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
1612 false, false, false),
1613 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
1614 false, false, false),
1615 VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
1616 true, false, false),
1617 VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
1618 true, false, false),
1619 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
1620 true, false, false),
1621 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
1622 &vmw_cmd_set_render_target_check, true, false, false),
1623 VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
1624 true, false, false),
1625 VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
1626 true, false, false),
1627 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
1628 true, false, false),
1629 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
1630 true, false, false),
1631 VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
1632 true, false, false),
1633 VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
1634 true, false, false),
1635 VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
1636 true, false, false),
1637 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
1638 false, false, false),
1639 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check,
1641 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check,
1643 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
1644 true, false, false),
1645 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check,
1647 VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
1648 true, false, false),
1649 VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
1650 true, false, false),
1651 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
1652 true, false, false),
1653 VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
1654 true, false, false),
1655 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
1656 true, false, false),
1657 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
1658 true, false, false),
1659 VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
1660 &vmw_cmd_blt_surf_screen_check, false, false, false),
1661 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
1662 false, false, false),
1663 VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
1664 false, false, false),
1665 VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
1666 false, false, false),
1667 VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
1668 false, false, false),
1669 VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
1670 false, false, false),
1671 VMW_CMD_DEF(SVGA_3D_CMD_SET_UNITY_SURFACE_COOKIE, &vmw_cmd_invalid,
1672 false, false, false),
1673 VMW_CMD_DEF(SVGA_3D_CMD_OPEN_CONTEXT_SURFACE, &vmw_cmd_invalid,
1674 false, false, false),
1675 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_BITBLT, &vmw_cmd_invalid,
1676 false, false, false),
1677 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_TRANSBLT, &vmw_cmd_invalid,
1678 false, false, false),
1679 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_STRETCHBLT, &vmw_cmd_invalid,
1680 false, false, false),
1681 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_COLORFILL, &vmw_cmd_invalid,
1682 false, false, false),
1683 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_ALPHABLEND, &vmw_cmd_invalid,
1684 false, false, false),
1685 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_CLEARTYPEBLEND, &vmw_cmd_invalid,
1686 false, false, false),
1687 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
1688 false, false, true),
1689 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
1690 false, false, true),
1691 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
1692 false, false, true),
1693 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
1694 false, false, true),
1695 VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB, &vmw_cmd_invalid,
1696 false, false, true),
1697 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
1698 false, false, true),
1699 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
1700 false, false, true),
1701 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
1702 false, false, true),
1703 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
1705 VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
1706 false, false, true),
1707 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
1709 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
1710 &vmw_cmd_update_gb_surface, true, false, true),
1711 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
1712 &vmw_cmd_readback_gb_image, true, false, true),
1713 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
1714 &vmw_cmd_readback_gb_surface, true, false, true),
1715 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
1716 &vmw_cmd_invalidate_gb_image, true, false, true),
1717 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
1718 &vmw_cmd_invalidate_gb_surface, true, false, true),
1719 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
1720 false, false, true),
1721 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
1722 false, false, true),
1723 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
1724 false, false, true),
1725 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
1726 false, false, true),
1727 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
1728 false, false, true),
1729 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
1730 false, false, true),
1731 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
1733 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
1734 false, false, true),
1735 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
1736 false, false, false),
1737 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
1739 VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
1741 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
1743 VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
1745 VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
1746 false, false, true),
1747 VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
1748 false, false, true),
1749 VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
1750 false, false, true),
1751 VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
1752 false, false, true),
1753 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
1754 false, false, true),
1755 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
1756 false, false, true),
1757 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
1758 false, false, true),
1759 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
1760 false, false, true),
1761 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
1762 false, false, true),
1763 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
1764 false, false, true),
1765 VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
1769 static int vmw_cmd_check(struct vmw_private *dev_priv,
1770 struct vmw_sw_context *sw_context,
1771 void *buf, uint32_t *size)
1774 uint32_t size_remaining = *size;
1775 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
1777 const struct vmw_cmd_entry *entry;
1778 bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
1780 cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
1781 /* Handle any none 3D commands */
1782 if (unlikely(cmd_id < SVGA_CMD_MAX))
1783 return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
1786 cmd_id = le32_to_cpu(header->id);
1787 *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
1789 cmd_id -= SVGA_3D_CMD_BASE;
1790 if (unlikely(*size > size_remaining))
1793 if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
1796 entry = &vmw_cmd_entries[cmd_id];
1797 if (unlikely(!entry->user_allow && !sw_context->kernel))
1798 goto out_privileged;
1800 if (unlikely(entry->gb_disable && gb))
1803 if (unlikely(entry->gb_enable && !gb))
1806 ret = entry->func(dev_priv, sw_context, header);
1807 if (unlikely(ret != 0))
1812 DRM_ERROR("Invalid SVGA3D command: %d\n",
1813 cmd_id + SVGA_3D_CMD_BASE);
1816 DRM_ERROR("Privileged SVGA3D command: %d\n",
1817 cmd_id + SVGA_3D_CMD_BASE);
1820 DRM_ERROR("Deprecated (disallowed) SVGA3D command: %d\n",
1821 cmd_id + SVGA_3D_CMD_BASE);
1824 DRM_ERROR("SVGA3D command: %d not supported by virtual hardware.\n",
1825 cmd_id + SVGA_3D_CMD_BASE);
1829 static int vmw_cmd_check_all(struct vmw_private *dev_priv,
1830 struct vmw_sw_context *sw_context,
1834 int32_t cur_size = size;
1837 sw_context->buf_start = buf;
1839 while (cur_size > 0) {
1841 ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
1842 if (unlikely(ret != 0))
1844 buf = (void *)((unsigned long) buf + size);
1848 if (unlikely(cur_size != 0)) {
1849 DRM_ERROR("Command verifier out of sync.\n");
1856 static void vmw_free_relocations(struct vmw_sw_context *sw_context)
1858 sw_context->cur_reloc = 0;
1861 static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
1864 struct vmw_relocation *reloc;
1865 struct ttm_validate_buffer *validate;
1866 struct ttm_buffer_object *bo;
1868 for (i = 0; i < sw_context->cur_reloc; ++i) {
1869 reloc = &sw_context->relocs[i];
1870 validate = &sw_context->val_bufs[reloc->index].base;
1872 switch (bo->mem.mem_type) {
1874 reloc->location->offset += bo->offset;
1875 reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
1878 reloc->location->gmrId = bo->mem.start;
1881 *reloc->mob_loc = bo->mem.start;
1887 vmw_free_relocations(sw_context);
1891 * vmw_resource_list_unrefererence - Free up a resource list and unreference
1892 * all resources referenced by it.
1894 * @list: The resource list.
1896 static void vmw_resource_list_unreference(struct list_head *list)
1898 struct vmw_resource_val_node *val, *val_next;
1901 * Drop references to resources held during command submission.
1904 list_for_each_entry_safe(val, val_next, list, head) {
1905 list_del_init(&val->head);
1906 vmw_resource_unreference(&val->res);
1907 if (unlikely(val->staged_bindings))
1908 kfree(val->staged_bindings);
1913 static void vmw_clear_validations(struct vmw_sw_context *sw_context)
1915 struct vmw_validate_buffer *entry, *next;
1916 struct vmw_resource_val_node *val;
1919 * Drop references to DMA buffers held during command submission.
1921 list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
1923 list_del(&entry->base.head);
1924 ttm_bo_unref(&entry->base.bo);
1925 (void) drm_ht_remove_item(&sw_context->res_ht, &entry->hash);
1926 sw_context->cur_val_buf--;
1928 BUG_ON(sw_context->cur_val_buf != 0);
1930 list_for_each_entry(val, &sw_context->resource_list, head)
1931 (void) drm_ht_remove_item(&sw_context->res_ht, &val->hash);
1934 static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
1935 struct ttm_buffer_object *bo,
1936 bool validate_as_mob)
1942 * Don't validate pinned buffers.
1945 if (bo == dev_priv->pinned_bo ||
1946 (bo == dev_priv->dummy_query_bo &&
1947 dev_priv->dummy_query_bo_pinned))
1950 if (validate_as_mob)
1951 return ttm_bo_validate(bo, &vmw_mob_placement, true, false);
1954 * Put BO in VRAM if there is space, otherwise as a GMR.
1955 * If there is no space in VRAM and GMR ids are all used up,
1956 * start evicting GMRs to make room. If the DMA buffer can't be
1957 * used as a GMR, this will return -ENOMEM.
1960 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false);
1961 if (likely(ret == 0 || ret == -ERESTARTSYS))
1965 * If that failed, try VRAM again, this time evicting
1966 * previous contents.
1969 DRM_INFO("Falling through to VRAM.\n");
1970 ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false);
1974 static int vmw_validate_buffers(struct vmw_private *dev_priv,
1975 struct vmw_sw_context *sw_context)
1977 struct vmw_validate_buffer *entry;
1980 list_for_each_entry(entry, &sw_context->validate_nodes, base.head) {
1981 ret = vmw_validate_single_buffer(dev_priv, entry->base.bo,
1982 entry->validate_as_mob);
1983 if (unlikely(ret != 0))
1989 static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
1992 if (likely(sw_context->cmd_bounce_size >= size))
1995 if (sw_context->cmd_bounce_size == 0)
1996 sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
1998 while (sw_context->cmd_bounce_size < size) {
1999 sw_context->cmd_bounce_size =
2000 PAGE_ALIGN(sw_context->cmd_bounce_size +
2001 (sw_context->cmd_bounce_size >> 1));
2004 if (sw_context->cmd_bounce != NULL)
2005 vfree(sw_context->cmd_bounce);
2007 sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
2009 if (sw_context->cmd_bounce == NULL) {
2010 DRM_ERROR("Failed to allocate command bounce buffer.\n");
2011 sw_context->cmd_bounce_size = 0;
2019 * vmw_execbuf_fence_commands - create and submit a command stream fence
2021 * Creates a fence object and submits a command stream marker.
2022 * If this fails for some reason, We sync the fifo and return NULL.
2023 * It is then safe to fence buffers with a NULL pointer.
2025 * If @p_handle is not NULL @file_priv must also not be NULL. Creates
2026 * a userspace handle if @p_handle is not NULL, otherwise not.
2029 int vmw_execbuf_fence_commands(struct drm_file *file_priv,
2030 struct vmw_private *dev_priv,
2031 struct vmw_fence_obj **p_fence,
2036 bool synced = false;
2038 /* p_handle implies file_priv. */
2039 BUG_ON(p_handle != NULL && file_priv == NULL);
2041 ret = vmw_fifo_send_fence(dev_priv, &sequence);
2042 if (unlikely(ret != 0)) {
2043 DRM_ERROR("Fence submission error. Syncing.\n");
2047 if (p_handle != NULL)
2048 ret = vmw_user_fence_create(file_priv, dev_priv->fman,
2050 DRM_VMW_FENCE_FLAG_EXEC,
2053 ret = vmw_fence_create(dev_priv->fman, sequence,
2054 DRM_VMW_FENCE_FLAG_EXEC,
2057 if (unlikely(ret != 0 && !synced)) {
2058 (void) vmw_fallback_wait(dev_priv, false, false,
2060 VMW_FENCE_WAIT_TIMEOUT);
2068 * vmw_execbuf_copy_fence_user - copy fence object information to
2071 * @dev_priv: Pointer to a vmw_private struct.
2072 * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
2073 * @ret: Return value from fence object creation.
2074 * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
2075 * which the information should be copied.
2076 * @fence: Pointer to the fenc object.
2077 * @fence_handle: User-space fence handle.
2079 * This function copies fence information to user-space. If copying fails,
2080 * The user-space struct drm_vmw_fence_rep::error member is hopefully
2081 * left untouched, and if it's preloaded with an -EFAULT by user-space,
2082 * the error will hopefully be detected.
2083 * Also if copying fails, user-space will be unable to signal the fence
2084 * object so we wait for it immediately, and then unreference the
2085 * user-space reference.
2088 vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
2089 struct vmw_fpriv *vmw_fp,
2091 struct drm_vmw_fence_rep __user *user_fence_rep,
2092 struct vmw_fence_obj *fence,
2093 uint32_t fence_handle)
2095 struct drm_vmw_fence_rep fence_rep;
2097 if (user_fence_rep == NULL)
2100 memset(&fence_rep, 0, sizeof(fence_rep));
2102 fence_rep.error = ret;
2104 BUG_ON(fence == NULL);
2106 fence_rep.handle = fence_handle;
2107 fence_rep.seqno = fence->seqno;
2108 vmw_update_seqno(dev_priv, &dev_priv->fifo);
2109 fence_rep.passed_seqno = dev_priv->last_read_seqno;
2113 * copy_to_user errors will be detected by user space not
2114 * seeing fence_rep::error filled in. Typically
2115 * user-space would have pre-set that member to -EFAULT.
2117 ret = copy_to_user(user_fence_rep, &fence_rep,
2121 * User-space lost the fence object. We need to sync
2122 * and unreference the handle.
2124 if (unlikely(ret != 0) && (fence_rep.error == 0)) {
2125 ttm_ref_object_base_unref(vmw_fp->tfile,
2126 fence_handle, TTM_REF_USAGE);
2127 DRM_ERROR("Fence copy error. Syncing.\n");
2128 (void) vmw_fence_obj_wait(fence, fence->signal_mask,
2130 VMW_FENCE_WAIT_TIMEOUT);
2134 int vmw_execbuf_process(struct drm_file *file_priv,
2135 struct vmw_private *dev_priv,
2136 void __user *user_commands,
2137 void *kernel_commands,
2138 uint32_t command_size,
2139 uint64_t throttle_us,
2140 struct drm_vmw_fence_rep __user *user_fence_rep,
2141 struct vmw_fence_obj **out_fence)
2143 struct vmw_sw_context *sw_context = &dev_priv->ctx;
2144 struct vmw_fence_obj *fence = NULL;
2145 struct vmw_resource *error_resource;
2146 struct list_head resource_list;
2147 struct ww_acquire_ctx ticket;
2152 ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
2153 if (unlikely(ret != 0))
2154 return -ERESTARTSYS;
2156 if (kernel_commands == NULL) {
2157 sw_context->kernel = false;
2159 ret = vmw_resize_cmd_bounce(sw_context, command_size);
2160 if (unlikely(ret != 0))
2164 ret = copy_from_user(sw_context->cmd_bounce,
2165 user_commands, command_size);
2167 if (unlikely(ret != 0)) {
2169 DRM_ERROR("Failed copying commands.\n");
2172 kernel_commands = sw_context->cmd_bounce;
2174 sw_context->kernel = true;
2176 sw_context->tfile = vmw_fpriv(file_priv)->tfile;
2177 sw_context->cur_reloc = 0;
2178 sw_context->cur_val_buf = 0;
2179 sw_context->fence_flags = 0;
2180 INIT_LIST_HEAD(&sw_context->resource_list);
2181 sw_context->cur_query_bo = dev_priv->pinned_bo;
2182 sw_context->last_query_ctx = NULL;
2183 sw_context->needs_post_query_barrier = false;
2184 memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
2185 INIT_LIST_HEAD(&sw_context->validate_nodes);
2186 INIT_LIST_HEAD(&sw_context->res_relocations);
2187 if (!sw_context->res_ht_initialized) {
2188 ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
2189 if (unlikely(ret != 0))
2191 sw_context->res_ht_initialized = true;
2194 INIT_LIST_HEAD(&resource_list);
2195 ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
2197 if (unlikely(ret != 0))
2200 ret = vmw_resources_reserve(sw_context);
2201 if (unlikely(ret != 0))
2204 ret = ttm_eu_reserve_buffers(&ticket, &sw_context->validate_nodes);
2205 if (unlikely(ret != 0))
2208 ret = vmw_validate_buffers(dev_priv, sw_context);
2209 if (unlikely(ret != 0))
2212 ret = vmw_resources_validate(sw_context);
2213 if (unlikely(ret != 0))
2217 ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
2220 if (unlikely(ret != 0))
2224 ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
2225 if (unlikely(ret != 0)) {
2230 cmd = vmw_fifo_reserve(dev_priv, command_size);
2231 if (unlikely(cmd == NULL)) {
2232 DRM_ERROR("Failed reserving fifo space for commands.\n");
2234 goto out_unlock_binding;
2237 vmw_apply_relocations(sw_context);
2238 memcpy(cmd, kernel_commands, command_size);
2240 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
2241 vmw_resource_relocations_free(&sw_context->res_relocations);
2243 vmw_fifo_commit(dev_priv, command_size);
2245 vmw_query_bo_switch_commit(dev_priv, sw_context);
2246 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
2248 (user_fence_rep) ? &handle : NULL);
2250 * This error is harmless, because if fence submission fails,
2251 * vmw_fifo_send_fence will sync. The error will be propagated to
2252 * user-space in @fence_rep
2256 DRM_ERROR("Fence submission error. Syncing.\n");
2258 vmw_resource_list_unreserve(&sw_context->resource_list, false);
2259 mutex_unlock(&dev_priv->binding_mutex);
2261 ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
2264 if (unlikely(dev_priv->pinned_bo != NULL &&
2265 !dev_priv->query_cid_valid))
2266 __vmw_execbuf_release_pinned_bo(dev_priv, fence);
2268 vmw_clear_validations(sw_context);
2269 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
2270 user_fence_rep, fence, handle);
2272 /* Don't unreference when handing fence out */
2273 if (unlikely(out_fence != NULL)) {
2276 } else if (likely(fence != NULL)) {
2277 vmw_fence_obj_unreference(&fence);
2280 list_splice_init(&sw_context->resource_list, &resource_list);
2281 mutex_unlock(&dev_priv->cmdbuf_mutex);
2284 * Unreference resources outside of the cmdbuf_mutex to
2285 * avoid deadlocks in resource destruction paths.
2287 vmw_resource_list_unreference(&resource_list);
2292 mutex_unlock(&dev_priv->binding_mutex);
2294 vmw_resource_relocations_free(&sw_context->res_relocations);
2295 vmw_free_relocations(sw_context);
2296 ttm_eu_backoff_reservation(&ticket, &sw_context->validate_nodes);
2297 vmw_resource_list_unreserve(&sw_context->resource_list, true);
2298 vmw_clear_validations(sw_context);
2299 if (unlikely(dev_priv->pinned_bo != NULL &&
2300 !dev_priv->query_cid_valid))
2301 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
2303 list_splice_init(&sw_context->resource_list, &resource_list);
2304 error_resource = sw_context->error_resource;
2305 sw_context->error_resource = NULL;
2306 mutex_unlock(&dev_priv->cmdbuf_mutex);
2309 * Unreference resources outside of the cmdbuf_mutex to
2310 * avoid deadlocks in resource destruction paths.
2312 vmw_resource_list_unreference(&resource_list);
2313 if (unlikely(error_resource != NULL))
2314 vmw_resource_unreference(&error_resource);
2320 * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
2322 * @dev_priv: The device private structure.
2324 * This function is called to idle the fifo and unpin the query buffer
2325 * if the normal way to do this hits an error, which should typically be
2328 static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
2330 DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
2332 (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
2333 vmw_bo_pin(dev_priv->pinned_bo, false);
2334 vmw_bo_pin(dev_priv->dummy_query_bo, false);
2335 dev_priv->dummy_query_bo_pinned = false;
2340 * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
2343 * @dev_priv: The device private structure.
2344 * @fence: If non-NULL should point to a struct vmw_fence_obj issued
2345 * _after_ a query barrier that flushes all queries touching the current
2346 * buffer pointed to by @dev_priv->pinned_bo
2348 * This function should be used to unpin the pinned query bo, or
2349 * as a query barrier when we need to make sure that all queries have
2350 * finished before the next fifo command. (For example on hardware
2351 * context destructions where the hardware may otherwise leak unfinished
2354 * This function does not return any failure codes, but make attempts
2355 * to do safe unpinning in case of errors.
2357 * The function will synchronize on the previous query barrier, and will
2358 * thus not finish until that barrier has executed.
2360 * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
2361 * before calling this function.
2363 void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
2364 struct vmw_fence_obj *fence)
2367 struct list_head validate_list;
2368 struct ttm_validate_buffer pinned_val, query_val;
2369 struct vmw_fence_obj *lfence = NULL;
2370 struct ww_acquire_ctx ticket;
2372 if (dev_priv->pinned_bo == NULL)
2375 INIT_LIST_HEAD(&validate_list);
2377 pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
2378 list_add_tail(&pinned_val.head, &validate_list);
2380 query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
2381 list_add_tail(&query_val.head, &validate_list);
2384 ret = ttm_eu_reserve_buffers(&ticket, &validate_list);
2385 } while (ret == -ERESTARTSYS);
2387 if (unlikely(ret != 0)) {
2388 vmw_execbuf_unpin_panic(dev_priv);
2389 goto out_no_reserve;
2392 if (dev_priv->query_cid_valid) {
2393 BUG_ON(fence != NULL);
2394 ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
2395 if (unlikely(ret != 0)) {
2396 vmw_execbuf_unpin_panic(dev_priv);
2399 dev_priv->query_cid_valid = false;
2402 vmw_bo_pin(dev_priv->pinned_bo, false);
2403 vmw_bo_pin(dev_priv->dummy_query_bo, false);
2404 dev_priv->dummy_query_bo_pinned = false;
2406 if (fence == NULL) {
2407 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
2411 ttm_eu_fence_buffer_objects(&ticket, &validate_list, (void *) fence);
2413 vmw_fence_obj_unreference(&lfence);
2415 ttm_bo_unref(&query_val.bo);
2416 ttm_bo_unref(&pinned_val.bo);
2417 ttm_bo_unref(&dev_priv->pinned_bo);
2423 ttm_eu_backoff_reservation(&ticket, &validate_list);
2425 ttm_bo_unref(&query_val.bo);
2426 ttm_bo_unref(&pinned_val.bo);
2427 ttm_bo_unref(&dev_priv->pinned_bo);
2431 * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
2434 * @dev_priv: The device private structure.
2436 * This function should be used to unpin the pinned query bo, or
2437 * as a query barrier when we need to make sure that all queries have
2438 * finished before the next fifo command. (For example on hardware
2439 * context destructions where the hardware may otherwise leak unfinished
2442 * This function does not return any failure codes, but make attempts
2443 * to do safe unpinning in case of errors.
2445 * The function will synchronize on the previous query barrier, and will
2446 * thus not finish until that barrier has executed.
2448 void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
2450 mutex_lock(&dev_priv->cmdbuf_mutex);
2451 if (dev_priv->query_cid_valid)
2452 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
2453 mutex_unlock(&dev_priv->cmdbuf_mutex);
2457 int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
2458 struct drm_file *file_priv)
2460 struct vmw_private *dev_priv = vmw_priv(dev);
2461 struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
2462 struct vmw_master *vmaster = vmw_master(file_priv->master);
2466 * This will allow us to extend the ioctl argument while
2467 * maintaining backwards compatibility:
2468 * We take different code paths depending on the value of
2472 if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
2473 DRM_ERROR("Incorrect execbuf version.\n");
2474 DRM_ERROR("You're running outdated experimental "
2475 "vmwgfx user-space drivers.");
2479 ret = ttm_read_lock(&vmaster->lock, true);
2480 if (unlikely(ret != 0))
2483 ret = vmw_execbuf_process(file_priv, dev_priv,
2484 (void __user *)(unsigned long)arg->commands,
2485 NULL, arg->command_size, arg->throttle_us,
2486 (void __user *)(unsigned long)arg->fence_rep,
2489 if (unlikely(ret != 0))
2492 vmw_kms_cursor_post_execbuf(dev_priv);
2495 ttm_read_unlock(&vmaster->lock);