xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c (revision c1e6267b9dd3e04f9dc29e500414cc4fc237dec1)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright (c) 2009-2025 Broadcom. All Rights Reserved. The term
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
6  *
7  **************************************************************************/
8 
9 #include "vmwgfx_binding.h"
10 #include "vmwgfx_bo.h"
11 #include "vmwgfx_drv.h"
12 #include "vmwgfx_mksstat.h"
13 #include "vmwgfx_so.h"
14 
15 #include <drm/ttm/ttm_bo.h>
16 #include <drm/ttm/ttm_placement.h>
17 
18 #include <linux/sync_file.h>
19 #include <linux/hashtable.h>
20 #include <linux/vmalloc.h>
21 
22 /*
23  * Helper macro to get dx_ctx_node if available otherwise print an error
24  * message. This is for use in command verifier function where if dx_ctx_node
25  * is not set then command is invalid.
26  */
27 #define VMW_GET_CTX_NODE(__sw_context)                                        \
28 ({                                                                            \
29 	__sw_context->dx_ctx_node ? __sw_context->dx_ctx_node : ({            \
30 		VMW_DEBUG_USER("SM context is not set at %s\n", __func__);    \
31 		__sw_context->dx_ctx_node;                                    \
32 	});                                                                   \
33 })
34 
35 #define VMW_DECLARE_CMD_VAR(__var, __type)                                    \
36 	struct {                                                              \
37 		SVGA3dCmdHeader header;                                       \
38 		__type body;                                                  \
39 	} __var
40 
41 /**
42  * struct vmw_relocation - Buffer object relocation
43  *
44  * @head: List head for the command submission context's relocation list
45  * @vbo: Non ref-counted pointer to buffer object
46  * @mob_loc: Pointer to location for mob id to be modified
47  * @location: Pointer to location for guest pointer to be modified
48  */
49 struct vmw_relocation {
50 	struct list_head head;
51 	struct vmw_bo *vbo;
52 	union {
53 		SVGAMobId *mob_loc;
54 		SVGAGuestPtr *location;
55 	};
56 };
57 
58 /**
59  * enum vmw_resource_relocation_type - Relocation type for resources
60  *
61  * @vmw_res_rel_normal: Traditional relocation. The resource id in the
62  * command stream is replaced with the actual id after validation.
63  * @vmw_res_rel_nop: NOP relocation. The command is unconditionally replaced
64  * with a NOP.
65  * @vmw_res_rel_cond_nop: Conditional NOP relocation. If the resource id after
66  * validation is -1, the command is replaced with a NOP. Otherwise no action.
67  * @vmw_res_rel_max: Last value in the enum - used for error checking
68 */
69 enum vmw_resource_relocation_type {
70 	vmw_res_rel_normal,
71 	vmw_res_rel_nop,
72 	vmw_res_rel_cond_nop,
73 	vmw_res_rel_max
74 };
75 
76 /**
77  * struct vmw_resource_relocation - Relocation info for resources
78  *
79  * @head: List head for the software context's relocation list.
80  * @res: Non-ref-counted pointer to the resource.
81  * @offset: Offset of single byte entries into the command buffer where the id
82  * that needs fixup is located.
83  * @rel_type: Type of relocation.
84  */
85 struct vmw_resource_relocation {
86 	struct list_head head;
87 	const struct vmw_resource *res;
88 	u32 offset:29;
89 	enum vmw_resource_relocation_type rel_type:3;
90 };
91 
92 /**
93  * struct vmw_ctx_validation_info - Extra validation metadata for contexts
94  *
95  * @head: List head of context list
96  * @ctx: The context resource
97  * @cur: The context's persistent binding state
98  * @staged: The binding state changes of this command buffer
99  */
100 struct vmw_ctx_validation_info {
101 	struct list_head head;
102 	struct vmw_resource *ctx;
103 	struct vmw_ctx_binding_state *cur;
104 	struct vmw_ctx_binding_state *staged;
105 };
106 
107 /**
108  * struct vmw_cmd_entry - Describe a command for the verifier
109  *
110  * @func: Call-back to handle the command.
111  * @user_allow: Whether allowed from the execbuf ioctl.
112  * @gb_disable: Whether disabled if guest-backed objects are available.
113  * @gb_enable: Whether enabled iff guest-backed objects are available.
114  * @cmd_name: Name of the command.
115  */
116 struct vmw_cmd_entry {
117 	int (*func) (struct vmw_private *, struct vmw_sw_context *,
118 		     SVGA3dCmdHeader *);
119 	bool user_allow;
120 	bool gb_disable;
121 	bool gb_enable;
122 	const char *cmd_name;
123 };
124 
125 #define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable)	\
126 	[(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
127 				       (_gb_disable), (_gb_enable), #_cmd}
128 
129 static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
130 					struct vmw_sw_context *sw_context,
131 					struct vmw_resource *ctx);
132 static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
133 				 struct vmw_sw_context *sw_context,
134 				 SVGAMobId *id,
135 				 struct vmw_bo **vmw_bo_p);
136 /**
137  * vmw_ptr_diff - Compute the offset from a to b in bytes
138  *
139  * @a: A starting pointer.
140  * @b: A pointer offset in the same address space.
141  *
142  * Returns: The offset in bytes between the two pointers.
143  */
144 static size_t vmw_ptr_diff(void *a, void *b)
145 {
146 	return (unsigned long) b - (unsigned long) a;
147 }
148 
149 /**
150  * vmw_execbuf_bindings_commit - Commit modified binding state
151  *
152  * @sw_context: The command submission context
153  * @backoff: Whether this is part of the error path and binding state changes
154  * should be ignored
155  */
156 static void vmw_execbuf_bindings_commit(struct vmw_sw_context *sw_context,
157 					bool backoff)
158 {
159 	struct vmw_ctx_validation_info *entry;
160 
161 	list_for_each_entry(entry, &sw_context->ctx_list, head) {
162 		if (!backoff)
163 			vmw_binding_state_commit(entry->cur, entry->staged);
164 
165 		if (entry->staged != sw_context->staged_bindings)
166 			vmw_binding_state_free(entry->staged);
167 		else
168 			sw_context->staged_bindings_inuse = false;
169 	}
170 
171 	/* List entries are freed with the validation context */
172 	INIT_LIST_HEAD(&sw_context->ctx_list);
173 }
174 
175 /**
176  * vmw_bind_dx_query_mob - Bind the DX query MOB if referenced
177  *
178  * @sw_context: The command submission context
179  */
180 static void vmw_bind_dx_query_mob(struct vmw_sw_context *sw_context)
181 {
182 	if (sw_context->dx_query_mob)
183 		vmw_context_bind_dx_query(sw_context->dx_query_ctx,
184 					  sw_context->dx_query_mob);
185 }
186 
187 /**
188  * vmw_cmd_ctx_first_setup - Perform the setup needed when a context is added to
189  * the validate list.
190  *
191  * @dev_priv: Pointer to the device private:
192  * @sw_context: The command submission context
193  * @res: Pointer to the resource
194  * @node: The validation node holding the context resource metadata
195  */
196 static int vmw_cmd_ctx_first_setup(struct vmw_private *dev_priv,
197 				   struct vmw_sw_context *sw_context,
198 				   struct vmw_resource *res,
199 				   struct vmw_ctx_validation_info *node)
200 {
201 	int ret;
202 
203 	ret = vmw_resource_context_res_add(dev_priv, sw_context, res);
204 	if (unlikely(ret != 0))
205 		goto out_err;
206 
207 	if (!sw_context->staged_bindings) {
208 		sw_context->staged_bindings = vmw_binding_state_alloc(dev_priv);
209 		if (IS_ERR(sw_context->staged_bindings)) {
210 			ret = PTR_ERR(sw_context->staged_bindings);
211 			sw_context->staged_bindings = NULL;
212 			goto out_err;
213 		}
214 	}
215 
216 	if (sw_context->staged_bindings_inuse) {
217 		node->staged = vmw_binding_state_alloc(dev_priv);
218 		if (IS_ERR(node->staged)) {
219 			ret = PTR_ERR(node->staged);
220 			node->staged = NULL;
221 			goto out_err;
222 		}
223 	} else {
224 		node->staged = sw_context->staged_bindings;
225 		sw_context->staged_bindings_inuse = true;
226 	}
227 
228 	node->ctx = res;
229 	node->cur = vmw_context_binding_state(res);
230 	list_add_tail(&node->head, &sw_context->ctx_list);
231 
232 	return 0;
233 
234 out_err:
235 	return ret;
236 }
237 
238 /**
239  * vmw_execbuf_res_size - calculate extra size fore the resource validation node
240  *
241  * @dev_priv: Pointer to the device private struct.
242  * @res_type: The resource type.
243  *
244  * Guest-backed contexts and DX contexts require extra size to store execbuf
245  * private information in the validation node. Typically the binding manager
246  * associated data structures.
247  *
248  * Returns: The extra size requirement based on resource type.
249  */
250 static unsigned int vmw_execbuf_res_size(struct vmw_private *dev_priv,
251 					 enum vmw_res_type res_type)
252 {
253 	return (res_type == vmw_res_dx_context ||
254 		(res_type == vmw_res_context && dev_priv->has_mob)) ?
255 		sizeof(struct vmw_ctx_validation_info) : 0;
256 }
257 
258 /**
259  * vmw_execbuf_rcache_update - Update a resource-node cache entry
260  *
261  * @rcache: Pointer to the entry to update.
262  * @res: Pointer to the resource.
263  * @private: Pointer to the execbuf-private space in the resource validation
264  * node.
265  */
266 static void vmw_execbuf_rcache_update(struct vmw_res_cache_entry *rcache,
267 				      struct vmw_resource *res,
268 				      void *private)
269 {
270 	rcache->res = res;
271 	rcache->private = private;
272 	rcache->valid = 1;
273 	rcache->valid_handle = 0;
274 }
275 
276 enum vmw_val_add_flags {
277 	vmw_val_add_flag_none  =      0,
278 	vmw_val_add_flag_noctx = 1 << 0,
279 };
280 
281 /**
282  * vmw_execbuf_res_val_add - Add a resource to the validation list.
283  *
284  * @sw_context: Pointer to the software context.
285  * @res: Unreferenced rcu-protected pointer to the resource.
286  * @dirty: Whether to change dirty status.
287  * @flags: specifies whether to use the context or not
288  *
289  * Returns: 0 on success. Negative error code on failure. Typical error codes
290  * are %-EINVAL on inconsistency and %-ESRCH if the resource was doomed.
291  */
292 static int vmw_execbuf_res_val_add(struct vmw_sw_context *sw_context,
293 				   struct vmw_resource *res,
294 				   u32 dirty,
295 				   u32 flags)
296 {
297 	struct vmw_private *dev_priv = res->dev_priv;
298 	int ret;
299 	enum vmw_res_type res_type = vmw_res_type(res);
300 	struct vmw_res_cache_entry *rcache;
301 	struct vmw_ctx_validation_info *ctx_info;
302 	bool first_usage;
303 	unsigned int priv_size;
304 
305 	rcache = &sw_context->res_cache[res_type];
306 	if (likely(rcache->valid && rcache->res == res)) {
307 		if (dirty)
308 			vmw_validation_res_set_dirty(sw_context->ctx,
309 						     rcache->private, dirty);
310 		return 0;
311 	}
312 
313 	if ((flags & vmw_val_add_flag_noctx) != 0) {
314 		ret = vmw_validation_add_resource(sw_context->ctx, res, 0, dirty,
315 						  (void **)&ctx_info, NULL);
316 		if (ret)
317 			return ret;
318 
319 	} else {
320 		priv_size = vmw_execbuf_res_size(dev_priv, res_type);
321 		ret = vmw_validation_add_resource(sw_context->ctx, res, priv_size,
322 						  dirty, (void **)&ctx_info,
323 						  &first_usage);
324 		if (ret)
325 			return ret;
326 
327 		if (priv_size && first_usage) {
328 			ret = vmw_cmd_ctx_first_setup(dev_priv, sw_context, res,
329 						      ctx_info);
330 			if (ret) {
331 				VMW_DEBUG_USER("Failed first usage context setup.\n");
332 				return ret;
333 			}
334 		}
335 	}
336 
337 	vmw_execbuf_rcache_update(rcache, res, ctx_info);
338 	return 0;
339 }
340 
341 /**
342  * vmw_view_res_val_add - Add a view and the surface it's pointing to to the
343  * validation list
344  *
345  * @sw_context: The software context holding the validation list.
346  * @view: Pointer to the view resource.
347  *
348  * Returns 0 if success, negative error code otherwise.
349  */
350 static int vmw_view_res_val_add(struct vmw_sw_context *sw_context,
351 				struct vmw_resource *view)
352 {
353 	int ret;
354 
355 	/*
356 	 * First add the resource the view is pointing to, otherwise it may be
357 	 * swapped out when the view is validated.
358 	 */
359 	ret = vmw_execbuf_res_val_add(sw_context, vmw_view_srf(view),
360 				      vmw_view_dirtying(view), vmw_val_add_flag_noctx);
361 	if (ret)
362 		return ret;
363 
364 	return vmw_execbuf_res_val_add(sw_context, view, VMW_RES_DIRTY_NONE,
365 				       vmw_val_add_flag_noctx);
366 }
367 
368 /**
369  * vmw_view_id_val_add - Look up a view and add it and the surface it's pointing
370  * to to the validation list.
371  *
372  * @sw_context: The software context holding the validation list.
373  * @view_type: The view type to look up.
374  * @id: view id of the view.
375  *
376  * The view is represented by a view id and the DX context it's created on, or
377  * scheduled for creation on. If there is no DX context set, the function will
378  * return an -EINVAL error pointer.
379  *
380  * Returns: Unreferenced pointer to the resource on success, negative error
381  * pointer on failure.
382  */
383 static struct vmw_resource *
384 vmw_view_id_val_add(struct vmw_sw_context *sw_context,
385 		    enum vmw_view_type view_type, u32 id)
386 {
387 	struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
388 	struct vmw_resource *view;
389 	int ret;
390 
391 	if (!ctx_node)
392 		return ERR_PTR(-EINVAL);
393 
394 	view = vmw_view_lookup(sw_context->man, view_type, id);
395 	if (IS_ERR(view))
396 		return view;
397 
398 	ret = vmw_view_res_val_add(sw_context, view);
399 	if (ret)
400 		return ERR_PTR(ret);
401 
402 	return view;
403 }
404 
405 /**
406  * vmw_resource_context_res_add - Put resources previously bound to a context on
407  * the validation list
408  *
409  * @dev_priv: Pointer to a device private structure
410  * @sw_context: Pointer to a software context used for this command submission
411  * @ctx: Pointer to the context resource
412  *
413  * This function puts all resources that were previously bound to @ctx on the
414  * resource validation list. This is part of the context state reemission
415  */
416 static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
417 					struct vmw_sw_context *sw_context,
418 					struct vmw_resource *ctx)
419 {
420 	struct list_head *binding_list;
421 	struct vmw_ctx_bindinfo *entry;
422 	int ret = 0;
423 	struct vmw_resource *res;
424 	u32 i;
425 	u32 cotable_max = has_sm5_context(ctx->dev_priv) ?
426 		SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
427 
428 	/* Add all cotables to the validation list. */
429 	if (has_sm4_context(dev_priv) &&
430 	    vmw_res_type(ctx) == vmw_res_dx_context) {
431 		for (i = 0; i < cotable_max; ++i) {
432 			res = vmw_context_cotable(ctx, i);
433 			if (IS_ERR_OR_NULL(res))
434 				continue;
435 
436 			ret = vmw_execbuf_res_val_add(sw_context, res,
437 						      VMW_RES_DIRTY_SET,
438 						      vmw_val_add_flag_noctx);
439 			if (unlikely(ret != 0))
440 				return ret;
441 		}
442 	}
443 
444 	/* Add all resources bound to the context to the validation list */
445 	mutex_lock(&dev_priv->binding_mutex);
446 	binding_list = vmw_context_binding_list(ctx);
447 
448 	list_for_each_entry(entry, binding_list, ctx_list) {
449 		if (vmw_res_type(entry->res) == vmw_res_view)
450 			ret = vmw_view_res_val_add(sw_context, entry->res);
451 		else
452 			ret = vmw_execbuf_res_val_add(sw_context, entry->res,
453 						      vmw_binding_dirtying(entry->bt),
454 						      vmw_val_add_flag_noctx);
455 		if (unlikely(ret != 0))
456 			break;
457 	}
458 
459 	if (has_sm4_context(dev_priv) &&
460 	    vmw_res_type(ctx) == vmw_res_dx_context) {
461 		struct vmw_bo *dx_query_mob;
462 
463 		dx_query_mob = vmw_context_get_dx_query_mob(ctx);
464 		if (dx_query_mob) {
465 			vmw_bo_placement_set(dx_query_mob,
466 					     VMW_BO_DOMAIN_MOB,
467 					     VMW_BO_DOMAIN_MOB);
468 			ret = vmw_validation_add_bo(sw_context->ctx,
469 						    dx_query_mob);
470 		}
471 	}
472 
473 	mutex_unlock(&dev_priv->binding_mutex);
474 	return ret;
475 }
476 
477 /**
478  * vmw_resource_relocation_add - Add a relocation to the relocation list
479  *
480  * @sw_context: Pointer to the software context.
481  * @res: The resource.
482  * @offset: Offset into the command buffer currently being parsed where the id
483  * that needs fixup is located. Granularity is one byte.
484  * @rel_type: Relocation type.
485  */
486 static int vmw_resource_relocation_add(struct vmw_sw_context *sw_context,
487 				       const struct vmw_resource *res,
488 				       unsigned long offset,
489 				       enum vmw_resource_relocation_type
490 				       rel_type)
491 {
492 	struct vmw_resource_relocation *rel;
493 
494 	rel = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*rel));
495 	if (unlikely(!rel)) {
496 		VMW_DEBUG_USER("Failed to allocate a resource relocation.\n");
497 		return -ENOMEM;
498 	}
499 
500 	rel->res = res;
501 	rel->offset = offset;
502 	rel->rel_type = rel_type;
503 	list_add_tail(&rel->head, &sw_context->res_relocations);
504 
505 	return 0;
506 }
507 
508 /**
509  * vmw_resource_relocations_free - Free all relocations on a list
510  *
511  * @list: Pointer to the head of the relocation list
512  */
513 static void vmw_resource_relocations_free(struct list_head *list)
514 {
515 	/* Memory is validation context memory, so no need to free it */
516 	INIT_LIST_HEAD(list);
517 }
518 
519 /**
520  * vmw_resource_relocations_apply - Apply all relocations on a list
521  *
522  * @cb: Pointer to the start of the command buffer bein patch. This need not be
523  * the same buffer as the one being parsed when the relocation list was built,
524  * but the contents must be the same modulo the resource ids.
525  * @list: Pointer to the head of the relocation list.
526  */
527 static void vmw_resource_relocations_apply(uint32_t *cb,
528 					   struct list_head *list)
529 {
530 	struct vmw_resource_relocation *rel;
531 
532 	/* Validate the struct vmw_resource_relocation member size */
533 	BUILD_BUG_ON(SVGA_CB_MAX_SIZE >= (1 << 29));
534 	BUILD_BUG_ON(vmw_res_rel_max >= (1 << 3));
535 
536 	list_for_each_entry(rel, list, head) {
537 		u32 *addr = (u32 *)((unsigned long) cb + rel->offset);
538 		switch (rel->rel_type) {
539 		case vmw_res_rel_normal:
540 			*addr = rel->res->id;
541 			break;
542 		case vmw_res_rel_nop:
543 			*addr = SVGA_3D_CMD_NOP;
544 			break;
545 		default:
546 			if (rel->res->id == -1)
547 				*addr = SVGA_3D_CMD_NOP;
548 			break;
549 		}
550 	}
551 }
552 
553 static int vmw_cmd_invalid(struct vmw_private *dev_priv,
554 			   struct vmw_sw_context *sw_context,
555 			   SVGA3dCmdHeader *header)
556 {
557 	return -EINVAL;
558 }
559 
560 static int vmw_cmd_ok(struct vmw_private *dev_priv,
561 		      struct vmw_sw_context *sw_context,
562 		      SVGA3dCmdHeader *header)
563 {
564 	return 0;
565 }
566 
567 /**
568  * vmw_cmd_body_fits - Check that a command body covers what a handler reads
569  *
570  * @header: Pointer to the command header in the command stream.
571  * @size: Number of body bytes that the command handler interprets.
572  *
573  * vmw_cmd_check() only verifies that @header->size fits into the submitted
574  * command buffer. A handler that reads past the declared body reads the next
575  * command, which is verified separately. A relocation recorded there would
576  * rewrite the already verified command after verification has finished.
577  *
578  * Return: true if the body is at least @size bytes, false otherwise.
579  */
580 static bool vmw_cmd_body_fits(const SVGA3dCmdHeader *header, size_t size)
581 {
582 	if (likely(header->size >= size))
583 		return true;
584 
585 	VMW_DEBUG_USER("SVGA3D command %u: body size %u is smaller than %zu.\n",
586 		       header->id, header->size, size);
587 	return false;
588 }
589 
590 /**
591  * vmw_resources_reserve - Reserve all resources on the sw_context's resource
592  * list.
593  *
594  * @sw_context: Pointer to the software context.
595  *
596  * Note that since vmware's command submission currently is protected by the
597  * cmdbuf mutex, no fancy deadlock avoidance is required for resources, since
598  * only a single thread at once will attempt this.
599  */
600 static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
601 {
602 	int ret;
603 
604 	ret = vmw_validation_res_reserve(sw_context->ctx, true);
605 	if (ret)
606 		return ret;
607 
608 	if (sw_context->dx_query_mob) {
609 		struct vmw_bo *expected_dx_query_mob;
610 
611 		expected_dx_query_mob =
612 			vmw_context_get_dx_query_mob(sw_context->dx_query_ctx);
613 		if (expected_dx_query_mob &&
614 		    expected_dx_query_mob != sw_context->dx_query_mob) {
615 			ret = -EINVAL;
616 		}
617 	}
618 
619 	return ret;
620 }
621 
622 /**
623  * vmw_cmd_res_check - Check that a resource is present and if so, put it on the
624  * resource validate list unless it's already there.
625  *
626  * @dev_priv: Pointer to a device private structure.
627  * @sw_context: Pointer to the software context.
628  * @res_type: Resource type.
629  * @dirty: Whether to change dirty status.
630  * @converter: User-space visible type specific information.
631  * @id_loc: Pointer to the location in the command buffer currently being parsed
632  * from where the user-space resource id handle is located.
633  * @p_res: Pointer to pointer to resource validation node. Populated on
634  * exit.
635  */
636 static int
637 vmw_cmd_res_check(struct vmw_private *dev_priv,
638 		  struct vmw_sw_context *sw_context,
639 		  enum vmw_res_type res_type,
640 		  u32 dirty,
641 		  const struct vmw_user_resource_conv *converter,
642 		  uint32_t *id_loc,
643 		  struct vmw_resource **p_res)
644 {
645 	struct vmw_res_cache_entry *rcache = &sw_context->res_cache[res_type];
646 	struct vmw_resource *res;
647 	int ret = 0;
648 	bool needs_unref = false;
649 
650 	if (p_res)
651 		*p_res = NULL;
652 
653 	if (*id_loc == SVGA3D_INVALID_ID) {
654 		if (res_type == vmw_res_context) {
655 			VMW_DEBUG_USER("Illegal context invalid id.\n");
656 			return -EINVAL;
657 		}
658 		return 0;
659 	}
660 
661 	if (likely(rcache->valid_handle && *id_loc == rcache->handle)) {
662 		res = rcache->res;
663 		if (dirty)
664 			vmw_validation_res_set_dirty(sw_context->ctx,
665 						     rcache->private, dirty);
666 	} else {
667 		unsigned int size = vmw_execbuf_res_size(dev_priv, res_type);
668 
669 		ret = vmw_validation_preload_res(sw_context->ctx, size);
670 		if (ret)
671 			return ret;
672 
673 		ret = vmw_user_resource_lookup_handle
674 			(dev_priv, sw_context->fp->tfile, *id_loc, converter, &res);
675 		if (ret != 0) {
676 			VMW_DEBUG_USER("Could not find/use resource 0x%08x.\n",
677 				       (unsigned int) *id_loc);
678 			return ret;
679 		}
680 		needs_unref = true;
681 
682 		ret = vmw_execbuf_res_val_add(sw_context, res, dirty, vmw_val_add_flag_none);
683 		if (unlikely(ret != 0))
684 			goto res_check_done;
685 
686 		if (rcache->valid && rcache->res == res) {
687 			rcache->valid_handle = true;
688 			rcache->handle = *id_loc;
689 		}
690 	}
691 
692 	ret = vmw_resource_relocation_add(sw_context, res,
693 					  vmw_ptr_diff(sw_context->buf_start,
694 						       id_loc),
695 					  vmw_res_rel_normal);
696 	if (p_res)
697 		*p_res = res;
698 
699 res_check_done:
700 	if (needs_unref)
701 		vmw_resource_unreference(&res);
702 
703 	return ret;
704 }
705 
706 /**
707  * vmw_rebind_all_dx_query - Rebind DX query associated with the context
708  *
709  * @ctx_res: context the query belongs to
710  *
711  * This function assumes binding_mutex is held.
712  */
713 static int vmw_rebind_all_dx_query(struct vmw_resource *ctx_res)
714 {
715 	struct vmw_private *dev_priv = ctx_res->dev_priv;
716 	struct vmw_bo *dx_query_mob;
717 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindAllQuery);
718 
719 	dx_query_mob = vmw_context_get_dx_query_mob(ctx_res);
720 
721 	if (!dx_query_mob || dx_query_mob->dx_query_ctx)
722 		return 0;
723 
724 	cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), ctx_res->id);
725 	if (cmd == NULL)
726 		return -ENOMEM;
727 
728 	cmd->header.id = SVGA_3D_CMD_DX_BIND_ALL_QUERY;
729 	cmd->header.size = sizeof(cmd->body);
730 	cmd->body.cid = ctx_res->id;
731 	cmd->body.mobid = dx_query_mob->tbo.resource->start;
732 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
733 
734 	vmw_context_bind_dx_query(ctx_res, dx_query_mob);
735 
736 	return 0;
737 }
738 
739 /**
740  * vmw_rebind_contexts - Rebind all resources previously bound to referenced
741  * contexts.
742  *
743  * @sw_context: Pointer to the software context.
744  *
745  * Rebind context binding points that have been scrubbed because of eviction.
746  */
747 static int vmw_rebind_contexts(struct vmw_sw_context *sw_context)
748 {
749 	struct vmw_ctx_validation_info *val;
750 	int ret;
751 
752 	list_for_each_entry(val, &sw_context->ctx_list, head) {
753 		ret = vmw_binding_rebind_all(val->cur);
754 		if (unlikely(ret != 0)) {
755 			if (ret != -ERESTARTSYS)
756 				VMW_DEBUG_USER("Failed to rebind context.\n");
757 			return ret;
758 		}
759 
760 		ret = vmw_rebind_all_dx_query(val->ctx);
761 		if (ret != 0) {
762 			VMW_DEBUG_USER("Failed to rebind queries.\n");
763 			return ret;
764 		}
765 	}
766 
767 	return 0;
768 }
769 
770 /**
771  * vmw_view_bindings_add - Add an array of view bindings to a context binding
772  * state tracker.
773  *
774  * @sw_context: The execbuf state used for this command.
775  * @view_type: View type for the bindings.
776  * @binding_type: Binding type for the bindings.
777  * @shader_slot: The shader slot to user for the bindings.
778  * @view_ids: Array of view ids to be bound.
779  * @num_views: Number of view ids in @view_ids.
780  * @first_slot: The binding slot to be used for the first view id in @view_ids.
781  */
782 static int vmw_view_bindings_add(struct vmw_sw_context *sw_context,
783 				 enum vmw_view_type view_type,
784 				 enum vmw_ctx_binding_type binding_type,
785 				 uint32 shader_slot,
786 				 uint32 view_ids[], u32 num_views,
787 				 u32 first_slot)
788 {
789 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
790 	u32 i;
791 
792 	if (!ctx_node)
793 		return -EINVAL;
794 
795 	for (i = 0; i < num_views; ++i) {
796 		struct vmw_ctx_bindinfo_view binding;
797 		struct vmw_resource *view = NULL;
798 
799 		if (view_ids[i] != SVGA3D_INVALID_ID) {
800 			view = vmw_view_id_val_add(sw_context, view_type,
801 						   view_ids[i]);
802 			if (IS_ERR(view)) {
803 				VMW_DEBUG_USER("View not found.\n");
804 				return PTR_ERR(view);
805 			}
806 		}
807 		binding.bi.ctx = ctx_node->ctx;
808 		binding.bi.res = view;
809 		binding.bi.bt = binding_type;
810 		binding.shader_slot = shader_slot;
811 		binding.slot = first_slot + i;
812 		vmw_binding_add(ctx_node->staged, &binding.bi,
813 				shader_slot, binding.slot);
814 	}
815 
816 	return 0;
817 }
818 
819 /**
820  * vmw_cmd_cid_check - Check a command header for valid context information.
821  *
822  * @dev_priv: Pointer to a device private structure.
823  * @sw_context: Pointer to the software context.
824  * @header: A command header with an embedded user-space context handle.
825  *
826  * Convenience function: Call vmw_cmd_res_check with the user-space context
827  * handle embedded in @header.
828  */
829 static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
830 			     struct vmw_sw_context *sw_context,
831 			     SVGA3dCmdHeader *header)
832 {
833 	VMW_DECLARE_CMD_VAR(*cmd, uint32_t) =
834 		container_of(header, typeof(*cmd), header);
835 
836 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
837 		return -EINVAL;
838 
839 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
840 				 VMW_RES_DIRTY_SET, user_context_converter,
841 				 &cmd->body, NULL);
842 }
843 
844 /**
845  * vmw_execbuf_info_from_res - Get the private validation metadata for a
846  * recently validated resource
847  *
848  * @sw_context: Pointer to the command submission context
849  * @res: The resource
850  *
851  * The resource pointed to by @res needs to be present in the command submission
852  * context's resource cache and hence the last resource of that type to be
853  * processed by the validation code.
854  *
855  * Return: a pointer to the private metadata of the resource, or NULL if it
856  * wasn't found
857  */
858 static struct vmw_ctx_validation_info *
859 vmw_execbuf_info_from_res(struct vmw_sw_context *sw_context,
860 			  struct vmw_resource *res)
861 {
862 	struct vmw_res_cache_entry *rcache =
863 		&sw_context->res_cache[vmw_res_type(res)];
864 
865 	if (rcache->valid && rcache->res == res)
866 		return rcache->private;
867 
868 	WARN_ON_ONCE(true);
869 	return NULL;
870 }
871 
872 static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
873 					   struct vmw_sw_context *sw_context,
874 					   SVGA3dCmdHeader *header)
875 {
876 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetRenderTarget);
877 	struct vmw_resource *ctx;
878 	struct vmw_resource *res;
879 	int ret;
880 
881 	cmd = container_of(header, typeof(*cmd), header);
882 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
883 		return -EINVAL;
884 
885 	if (cmd->body.type >= SVGA3D_RT_MAX) {
886 		VMW_DEBUG_USER("Illegal render target type %u.\n",
887 			       (unsigned int) cmd->body.type);
888 		return -EINVAL;
889 	}
890 
891 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
892 				VMW_RES_DIRTY_SET, user_context_converter,
893 				&cmd->body.cid, &ctx);
894 	if (unlikely(ret != 0))
895 		return ret;
896 
897 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
898 				VMW_RES_DIRTY_SET, user_surface_converter,
899 				&cmd->body.target.sid, &res);
900 	if (unlikely(ret))
901 		return ret;
902 
903 	if (dev_priv->has_mob) {
904 		struct vmw_ctx_bindinfo_view binding;
905 		struct vmw_ctx_validation_info *node;
906 
907 		node = vmw_execbuf_info_from_res(sw_context, ctx);
908 		if (!node)
909 			return -EINVAL;
910 
911 		binding.bi.ctx = ctx;
912 		binding.bi.res = res;
913 		binding.bi.bt = vmw_ctx_binding_rt;
914 		binding.slot = cmd->body.type;
915 		vmw_binding_add(node->staged, &binding.bi, 0, binding.slot);
916 	}
917 
918 	return 0;
919 }
920 
921 static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
922 				      struct vmw_sw_context *sw_context,
923 				      SVGA3dCmdHeader *header)
924 {
925 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceCopy);
926 	int ret;
927 
928 	cmd = container_of(header, typeof(*cmd), header);
929 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
930 		return -EINVAL;
931 
932 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
933 				VMW_RES_DIRTY_NONE, user_surface_converter,
934 				&cmd->body.src.sid, NULL);
935 	if (ret)
936 		return ret;
937 
938 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
939 				 VMW_RES_DIRTY_SET, user_surface_converter,
940 				 &cmd->body.dest.sid, NULL);
941 }
942 
943 static int vmw_cmd_buffer_copy_check(struct vmw_private *dev_priv,
944 				     struct vmw_sw_context *sw_context,
945 				     SVGA3dCmdHeader *header)
946 {
947 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBufferCopy);
948 	int ret;
949 
950 	cmd = container_of(header, typeof(*cmd), header);
951 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
952 		return -EINVAL;
953 
954 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
955 				VMW_RES_DIRTY_NONE, user_surface_converter,
956 				&cmd->body.src, NULL);
957 	if (ret != 0)
958 		return ret;
959 
960 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
961 				 VMW_RES_DIRTY_SET, user_surface_converter,
962 				 &cmd->body.dest, NULL);
963 }
964 
965 static int vmw_cmd_pred_copy_check(struct vmw_private *dev_priv,
966 				   struct vmw_sw_context *sw_context,
967 				   SVGA3dCmdHeader *header)
968 {
969 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXPredCopyRegion);
970 	int ret;
971 
972 	cmd = container_of(header, typeof(*cmd), header);
973 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
974 		return -EINVAL;
975 
976 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
977 				VMW_RES_DIRTY_NONE, user_surface_converter,
978 				&cmd->body.srcSid, NULL);
979 	if (ret != 0)
980 		return ret;
981 
982 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
983 				 VMW_RES_DIRTY_SET, user_surface_converter,
984 				 &cmd->body.dstSid, NULL);
985 }
986 
987 static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
988 				     struct vmw_sw_context *sw_context,
989 				     SVGA3dCmdHeader *header)
990 {
991 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceStretchBlt);
992 	int ret;
993 
994 	cmd = container_of(header, typeof(*cmd), header);
995 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
996 		return -EINVAL;
997 
998 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
999 				VMW_RES_DIRTY_NONE, user_surface_converter,
1000 				&cmd->body.src.sid, NULL);
1001 	if (unlikely(ret != 0))
1002 		return ret;
1003 
1004 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1005 				 VMW_RES_DIRTY_SET, user_surface_converter,
1006 				 &cmd->body.dest.sid, NULL);
1007 }
1008 
1009 static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
1010 					 struct vmw_sw_context *sw_context,
1011 					 SVGA3dCmdHeader *header)
1012 {
1013 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBlitSurfaceToScreen) =
1014 		container_of(header, typeof(*cmd), header);
1015 
1016 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1017 		return -EINVAL;
1018 
1019 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1020 				 VMW_RES_DIRTY_NONE, user_surface_converter,
1021 				 &cmd->body.srcImage.sid, NULL);
1022 }
1023 
1024 static int vmw_cmd_present_check(struct vmw_private *dev_priv,
1025 				 struct vmw_sw_context *sw_context,
1026 				 SVGA3dCmdHeader *header)
1027 {
1028 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdPresent) =
1029 		container_of(header, typeof(*cmd), header);
1030 
1031 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1032 		return -EINVAL;
1033 
1034 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1035 				 VMW_RES_DIRTY_NONE, user_surface_converter,
1036 				 &cmd->body.sid, NULL);
1037 }
1038 
1039 /**
1040  * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
1041  *
1042  * @dev_priv: The device private structure.
1043  * @new_query_bo: The new buffer holding query results.
1044  * @sw_context: The software context used for this command submission.
1045  *
1046  * This function checks whether @new_query_bo is suitable for holding query
1047  * results, and if another buffer currently is pinned for query results. If so,
1048  * the function prepares the state of @sw_context for switching pinned buffers
1049  * after successful submission of the current command batch.
1050  */
1051 static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
1052 				       struct vmw_bo *new_query_bo,
1053 				       struct vmw_sw_context *sw_context)
1054 {
1055 	struct vmw_res_cache_entry *ctx_entry =
1056 		&sw_context->res_cache[vmw_res_context];
1057 	int ret;
1058 
1059 	BUG_ON(!ctx_entry->valid);
1060 	sw_context->last_query_ctx = ctx_entry->res;
1061 
1062 	if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
1063 
1064 		if (unlikely(PFN_UP(new_query_bo->tbo.resource->size) > 4)) {
1065 			VMW_DEBUG_USER("Query buffer too large.\n");
1066 			return -EINVAL;
1067 		}
1068 
1069 		if (unlikely(sw_context->cur_query_bo != NULL)) {
1070 			sw_context->needs_post_query_barrier = true;
1071 			vmw_bo_placement_set_default_accelerated(sw_context->cur_query_bo);
1072 			ret = vmw_validation_add_bo(sw_context->ctx,
1073 						    sw_context->cur_query_bo);
1074 			if (unlikely(ret != 0))
1075 				return ret;
1076 		}
1077 		sw_context->cur_query_bo = new_query_bo;
1078 
1079 		vmw_bo_placement_set_default_accelerated(dev_priv->dummy_query_bo);
1080 		ret = vmw_validation_add_bo(sw_context->ctx,
1081 					    dev_priv->dummy_query_bo);
1082 		if (unlikely(ret != 0))
1083 			return ret;
1084 	}
1085 
1086 	return 0;
1087 }
1088 
1089 /**
1090  * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
1091  *
1092  * @dev_priv: The device private structure.
1093  * @sw_context: The software context used for this command submission batch.
1094  *
1095  * This function will check if we're switching query buffers, and will then,
1096  * issue a dummy occlusion query wait used as a query barrier. When the fence
1097  * object following that query wait has signaled, we are sure that all preceding
1098  * queries have finished, and the old query buffer can be unpinned. However,
1099  * since both the new query buffer and the old one are fenced with that fence,
1100  * we can do an asynchronus unpin now, and be sure that the old query buffer
1101  * won't be moved until the fence has signaled.
1102  *
1103  * As mentioned above, both the new - and old query buffers need to be fenced
1104  * using a sequence emitted *after* calling this function.
1105  */
1106 static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
1107 				     struct vmw_sw_context *sw_context)
1108 {
1109 	/*
1110 	 * The validate list should still hold references to all
1111 	 * contexts here.
1112 	 */
1113 	if (sw_context->needs_post_query_barrier) {
1114 		struct vmw_res_cache_entry *ctx_entry =
1115 			&sw_context->res_cache[vmw_res_context];
1116 		struct vmw_resource *ctx;
1117 		int ret;
1118 
1119 		BUG_ON(!ctx_entry->valid);
1120 		ctx = ctx_entry->res;
1121 
1122 		ret = vmw_cmd_emit_dummy_query(dev_priv, ctx->id);
1123 
1124 		if (unlikely(ret != 0))
1125 			VMW_DEBUG_USER("Out of fifo space for dummy query.\n");
1126 	}
1127 
1128 	if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
1129 		if (dev_priv->pinned_bo) {
1130 			vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
1131 			vmw_bo_unreference(&dev_priv->pinned_bo);
1132 		}
1133 
1134 		if (!sw_context->needs_post_query_barrier) {
1135 			vmw_bo_pin_reserved(sw_context->cur_query_bo, true);
1136 
1137 			/*
1138 			 * We pin also the dummy_query_bo buffer so that we
1139 			 * don't need to validate it when emitting dummy queries
1140 			 * in context destroy paths.
1141 			 */
1142 			if (!dev_priv->dummy_query_bo_pinned) {
1143 				vmw_bo_pin_reserved(dev_priv->dummy_query_bo,
1144 						    true);
1145 				dev_priv->dummy_query_bo_pinned = true;
1146 			}
1147 
1148 			BUG_ON(sw_context->last_query_ctx == NULL);
1149 			dev_priv->query_cid = sw_context->last_query_ctx->id;
1150 			dev_priv->query_cid_valid = true;
1151 			dev_priv->pinned_bo =
1152 				vmw_bo_reference(sw_context->cur_query_bo);
1153 		}
1154 	}
1155 }
1156 
1157 /**
1158  * vmw_translate_mob_ptr - Prepare to translate a user-space buffer handle
1159  * to a MOB id.
1160  *
1161  * @dev_priv: Pointer to a device private structure.
1162  * @sw_context: The software context used for this command batch validation.
1163  * @id: Pointer to the user-space handle to be translated.
1164  * @vmw_bo_p: Points to a location that, on successful return will carry a
1165  * non-reference-counted pointer to the buffer object identified by the
1166  * user-space handle in @id.
1167  *
1168  * This function saves information needed to translate a user-space buffer
1169  * handle to a MOB id. The translation does not take place immediately, but
1170  * during a call to vmw_apply_relocations().
1171  *
1172  * This function builds a relocation list and a list of buffers to validate. The
1173  * former needs to be freed using either vmw_apply_relocations() or
1174  * vmw_free_relocations(). The latter needs to be freed using
1175  * vmw_clear_validations.
1176  */
1177 static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
1178 				 struct vmw_sw_context *sw_context,
1179 				 SVGAMobId *id,
1180 				 struct vmw_bo **vmw_bo_p)
1181 {
1182 	struct vmw_bo *vmw_bo, *tmp_bo;
1183 	uint32_t handle = *id;
1184 	struct vmw_relocation *reloc;
1185 	int ret;
1186 
1187 	vmw_validation_preload_bo(sw_context->ctx);
1188 	ret = vmw_user_bo_lookup(sw_context->filp, handle, &vmw_bo);
1189 	if (ret != 0) {
1190 		drm_dbg(&dev_priv->drm, "Could not find or use MOB buffer.\n");
1191 		return ret;
1192 	}
1193 	vmw_bo_placement_set(vmw_bo, VMW_BO_DOMAIN_MOB, VMW_BO_DOMAIN_MOB);
1194 	ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo);
1195 	tmp_bo = vmw_bo;
1196 	vmw_user_bo_unref(&tmp_bo);
1197 	if (unlikely(ret != 0))
1198 		return ret;
1199 
1200 	reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1201 	if (!reloc)
1202 		return -ENOMEM;
1203 
1204 	reloc->mob_loc = id;
1205 	reloc->vbo = vmw_bo;
1206 
1207 	*vmw_bo_p = vmw_bo;
1208 	list_add_tail(&reloc->head, &sw_context->bo_relocations);
1209 
1210 	return 0;
1211 }
1212 
1213 /**
1214  * vmw_translate_guest_ptr - Prepare to translate a user-space buffer handle
1215  * to a valid SVGAGuestPtr
1216  *
1217  * @dev_priv: Pointer to a device private structure.
1218  * @sw_context: The software context used for this command batch validation.
1219  * @ptr: Pointer to the user-space handle to be translated.
1220  * @vmw_bo_p: Points to a location that, on successful return will carry a
1221  * non-reference-counted pointer to the DMA buffer identified by the user-space
1222  * handle in @id.
1223  *
1224  * This function saves information needed to translate a user-space buffer
1225  * handle to a valid SVGAGuestPtr. The translation does not take place
1226  * immediately, but during a call to vmw_apply_relocations().
1227  *
1228  * This function builds a relocation list and a list of buffers to validate.
1229  * The former needs to be freed using either vmw_apply_relocations() or
1230  * vmw_free_relocations(). The latter needs to be freed using
1231  * vmw_clear_validations.
1232  */
1233 static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
1234 				   struct vmw_sw_context *sw_context,
1235 				   SVGAGuestPtr *ptr,
1236 				   struct vmw_bo **vmw_bo_p)
1237 {
1238 	struct vmw_bo *vmw_bo, *tmp_bo;
1239 	uint32_t handle = ptr->gmrId;
1240 	struct vmw_relocation *reloc;
1241 	int ret;
1242 
1243 	vmw_validation_preload_bo(sw_context->ctx);
1244 	ret = vmw_user_bo_lookup(sw_context->filp, handle, &vmw_bo);
1245 	if (ret != 0) {
1246 		drm_dbg(&dev_priv->drm, "Could not find or use GMR region.\n");
1247 		return ret;
1248 	}
1249 	vmw_bo_placement_set(vmw_bo, VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
1250 			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
1251 	ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo);
1252 	tmp_bo = vmw_bo;
1253 	vmw_user_bo_unref(&tmp_bo);
1254 	if (unlikely(ret != 0))
1255 		return ret;
1256 
1257 	reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1258 	if (!reloc)
1259 		return -ENOMEM;
1260 
1261 	reloc->location = ptr;
1262 	reloc->vbo = vmw_bo;
1263 	*vmw_bo_p = vmw_bo;
1264 	list_add_tail(&reloc->head, &sw_context->bo_relocations);
1265 
1266 	return 0;
1267 }
1268 
1269 /**
1270  * vmw_cmd_dx_define_query - validate SVGA_3D_CMD_DX_DEFINE_QUERY command.
1271  *
1272  * @dev_priv: Pointer to a device private struct.
1273  * @sw_context: The software context used for this command submission.
1274  * @header: Pointer to the command header in the command stream.
1275  *
1276  * This function adds the new query into the query COTABLE
1277  */
1278 static int vmw_cmd_dx_define_query(struct vmw_private *dev_priv,
1279 				   struct vmw_sw_context *sw_context,
1280 				   SVGA3dCmdHeader *header)
1281 {
1282 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDefineQuery);
1283 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
1284 	struct vmw_resource *cotable_res;
1285 	int ret;
1286 
1287 	if (!ctx_node)
1288 		return -EINVAL;
1289 
1290 	cmd = container_of(header, typeof(*cmd), header);
1291 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1292 		return -EINVAL;
1293 
1294 	if (cmd->body.type <  SVGA3D_QUERYTYPE_MIN ||
1295 	    cmd->body.type >= SVGA3D_QUERYTYPE_MAX)
1296 		return -EINVAL;
1297 
1298 	cotable_res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXQUERY);
1299 	if (IS_ERR_OR_NULL(cotable_res))
1300 		return cotable_res ? PTR_ERR(cotable_res) : -EINVAL;
1301 	ret = vmw_cotable_notify(cotable_res, cmd->body.queryId);
1302 
1303 	return ret;
1304 }
1305 
1306 /**
1307  * vmw_cmd_dx_bind_query - validate SVGA_3D_CMD_DX_BIND_QUERY command.
1308  *
1309  * @dev_priv: Pointer to a device private struct.
1310  * @sw_context: The software context used for this command submission.
1311  * @header: Pointer to the command header in the command stream.
1312  *
1313  * The query bind operation will eventually associate the query ID with its
1314  * backing MOB.  In this function, we take the user mode MOB ID and use
1315  * vmw_translate_mob_ptr() to translate it to its kernel mode equivalent.
1316  */
1317 static int vmw_cmd_dx_bind_query(struct vmw_private *dev_priv,
1318 				 struct vmw_sw_context *sw_context,
1319 				 SVGA3dCmdHeader *header)
1320 {
1321 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindQuery);
1322 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
1323 	struct vmw_bo *vmw_bo;
1324 	int ret;
1325 
1326 	if (!ctx_node)
1327 		return -EINVAL;
1328 
1329 	cmd = container_of(header, typeof(*cmd), header);
1330 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1331 		return -EINVAL;
1332 
1333 	/*
1334 	 * Look up the buffer pointed to by q.mobid, put it on the relocation
1335 	 * list so its kernel mode MOB ID can be filled in later
1336 	 */
1337 	ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->body.mobid,
1338 				    &vmw_bo);
1339 
1340 	if (ret != 0)
1341 		return ret;
1342 
1343 	sw_context->dx_query_mob = vmw_bo;
1344 	sw_context->dx_query_ctx = ctx_node->ctx;
1345 	return 0;
1346 }
1347 
1348 /**
1349  * vmw_cmd_begin_gb_query - validate SVGA_3D_CMD_BEGIN_GB_QUERY command.
1350  *
1351  * @dev_priv: Pointer to a device private struct.
1352  * @sw_context: The software context used for this command submission.
1353  * @header: Pointer to the command header in the command stream.
1354  */
1355 static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
1356 				  struct vmw_sw_context *sw_context,
1357 				  SVGA3dCmdHeader *header)
1358 {
1359 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBeginGBQuery) =
1360 		container_of(header, typeof(*cmd), header);
1361 
1362 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1363 		return -EINVAL;
1364 
1365 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
1366 				 VMW_RES_DIRTY_SET, user_context_converter,
1367 				 &cmd->body.cid, NULL);
1368 }
1369 
1370 /**
1371  * vmw_cmd_begin_query - validate SVGA_3D_CMD_BEGIN_QUERY command.
1372  *
1373  * @dev_priv: Pointer to a device private struct.
1374  * @sw_context: The software context used for this command submission.
1375  * @header: Pointer to the command header in the command stream.
1376  */
1377 static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
1378 			       struct vmw_sw_context *sw_context,
1379 			       SVGA3dCmdHeader *header)
1380 {
1381 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBeginQuery) =
1382 		container_of(header, typeof(*cmd), header);
1383 
1384 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1385 		return -EINVAL;
1386 
1387 	if (unlikely(dev_priv->has_mob)) {
1388 		VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdBeginGBQuery);
1389 
1390 		BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1391 
1392 		gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
1393 		gb_cmd.header.size = cmd->header.size;
1394 		gb_cmd.body.cid = cmd->body.cid;
1395 		gb_cmd.body.type = cmd->body.type;
1396 
1397 		memcpy(cmd, &gb_cmd, sizeof(*cmd));
1398 		return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
1399 	}
1400 
1401 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
1402 				 VMW_RES_DIRTY_SET, user_context_converter,
1403 				 &cmd->body.cid, NULL);
1404 }
1405 
1406 /**
1407  * vmw_cmd_end_gb_query - validate SVGA_3D_CMD_END_GB_QUERY command.
1408  *
1409  * @dev_priv: Pointer to a device private struct.
1410  * @sw_context: The software context used for this command submission.
1411  * @header: Pointer to the command header in the command stream.
1412  */
1413 static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
1414 				struct vmw_sw_context *sw_context,
1415 				SVGA3dCmdHeader *header)
1416 {
1417 	struct vmw_bo *vmw_bo;
1418 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdEndGBQuery);
1419 	int ret;
1420 
1421 	cmd = container_of(header, typeof(*cmd), header);
1422 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1423 		return -EINVAL;
1424 
1425 	ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1426 	if (unlikely(ret != 0))
1427 		return ret;
1428 
1429 	ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->body.mobid,
1430 				    &vmw_bo);
1431 	if (unlikely(ret != 0))
1432 		return ret;
1433 
1434 	ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
1435 
1436 	return ret;
1437 }
1438 
1439 /**
1440  * vmw_cmd_end_query - validate SVGA_3D_CMD_END_QUERY command.
1441  *
1442  * @dev_priv: Pointer to a device private struct.
1443  * @sw_context: The software context used for this command submission.
1444  * @header: Pointer to the command header in the command stream.
1445  */
1446 static int vmw_cmd_end_query(struct vmw_private *dev_priv,
1447 			     struct vmw_sw_context *sw_context,
1448 			     SVGA3dCmdHeader *header)
1449 {
1450 	struct vmw_bo *vmw_bo;
1451 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdEndQuery);
1452 	int ret;
1453 
1454 	cmd = container_of(header, typeof(*cmd), header);
1455 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1456 		return -EINVAL;
1457 
1458 	if (dev_priv->has_mob) {
1459 		VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdEndGBQuery);
1460 
1461 		BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1462 
1463 		gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
1464 		gb_cmd.header.size = cmd->header.size;
1465 		gb_cmd.body.cid = cmd->body.cid;
1466 		gb_cmd.body.type = cmd->body.type;
1467 		gb_cmd.body.mobid = cmd->body.guestResult.gmrId;
1468 		gb_cmd.body.offset = cmd->body.guestResult.offset;
1469 
1470 		memcpy(cmd, &gb_cmd, sizeof(*cmd));
1471 		return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
1472 	}
1473 
1474 	ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1475 	if (unlikely(ret != 0))
1476 		return ret;
1477 
1478 	ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1479 				      &cmd->body.guestResult, &vmw_bo);
1480 	if (unlikely(ret != 0))
1481 		return ret;
1482 
1483 	ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
1484 
1485 	return ret;
1486 }
1487 
1488 /**
1489  * vmw_cmd_wait_gb_query - validate SVGA_3D_CMD_WAIT_GB_QUERY command.
1490  *
1491  * @dev_priv: Pointer to a device private struct.
1492  * @sw_context: The software context used for this command submission.
1493  * @header: Pointer to the command header in the command stream.
1494  */
1495 static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
1496 				 struct vmw_sw_context *sw_context,
1497 				 SVGA3dCmdHeader *header)
1498 {
1499 	struct vmw_bo *vmw_bo;
1500 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdWaitForGBQuery);
1501 	int ret;
1502 
1503 	cmd = container_of(header, typeof(*cmd), header);
1504 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1505 		return -EINVAL;
1506 
1507 	ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1508 	if (unlikely(ret != 0))
1509 		return ret;
1510 
1511 	ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->body.mobid,
1512 				    &vmw_bo);
1513 	if (unlikely(ret != 0))
1514 		return ret;
1515 
1516 	return 0;
1517 }
1518 
1519 /**
1520  * vmw_cmd_wait_query - validate SVGA_3D_CMD_WAIT_QUERY command.
1521  *
1522  * @dev_priv: Pointer to a device private struct.
1523  * @sw_context: The software context used for this command submission.
1524  * @header: Pointer to the command header in the command stream.
1525  */
1526 static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
1527 			      struct vmw_sw_context *sw_context,
1528 			      SVGA3dCmdHeader *header)
1529 {
1530 	struct vmw_bo *vmw_bo;
1531 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdWaitForQuery);
1532 	int ret;
1533 
1534 	cmd = container_of(header, typeof(*cmd), header);
1535 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1536 		return -EINVAL;
1537 
1538 	if (dev_priv->has_mob) {
1539 		VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdWaitForGBQuery);
1540 
1541 		BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1542 
1543 		gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
1544 		gb_cmd.header.size = cmd->header.size;
1545 		gb_cmd.body.cid = cmd->body.cid;
1546 		gb_cmd.body.type = cmd->body.type;
1547 		gb_cmd.body.mobid = cmd->body.guestResult.gmrId;
1548 		gb_cmd.body.offset = cmd->body.guestResult.offset;
1549 
1550 		memcpy(cmd, &gb_cmd, sizeof(*cmd));
1551 		return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
1552 	}
1553 
1554 	ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1555 	if (unlikely(ret != 0))
1556 		return ret;
1557 
1558 	ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1559 				      &cmd->body.guestResult, &vmw_bo);
1560 	if (unlikely(ret != 0))
1561 		return ret;
1562 
1563 	return 0;
1564 }
1565 
1566 static int vmw_cmd_dma(struct vmw_private *dev_priv,
1567 		       struct vmw_sw_context *sw_context,
1568 		       SVGA3dCmdHeader *header)
1569 {
1570 	struct vmw_bo *vmw_bo = NULL;
1571 	struct vmw_resource *res;
1572 	struct vmw_surface *srf = NULL;
1573 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceDMA);
1574 	int ret;
1575 	SVGA3dCmdSurfaceDMASuffix *suffix;
1576 	uint32_t bo_size;
1577 	bool dirty;
1578 
1579 	cmd = container_of(header, typeof(*cmd), header);
1580 
1581 	if (unlikely(header->size < sizeof(cmd->body) + sizeof(*suffix))) {
1582 		VMW_DEBUG_USER("Illegal SVGA_3D_CMD_SURFACE_DMA size.\n");
1583 		return -EINVAL;
1584 	}
1585 
1586 	suffix = (SVGA3dCmdSurfaceDMASuffix *)((unsigned long) &cmd->body +
1587 					       header->size - sizeof(*suffix));
1588 
1589 	/* Make sure device and verifier stays in sync. */
1590 	if (unlikely(suffix->suffixSize != sizeof(*suffix))) {
1591 		VMW_DEBUG_USER("Invalid DMA suffix size.\n");
1592 		return -EINVAL;
1593 	}
1594 
1595 	ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1596 				      &cmd->body.guest.ptr, &vmw_bo);
1597 	if (unlikely(ret != 0))
1598 		return ret;
1599 
1600 	/* Make sure DMA doesn't cross BO boundaries. */
1601 	bo_size = vmw_bo->tbo.base.size;
1602 	if (unlikely(cmd->body.guest.ptr.offset > bo_size)) {
1603 		VMW_DEBUG_USER("Invalid DMA offset.\n");
1604 		return -EINVAL;
1605 	}
1606 
1607 	bo_size -= cmd->body.guest.ptr.offset;
1608 	if (unlikely(suffix->maximumOffset > bo_size))
1609 		suffix->maximumOffset = bo_size;
1610 
1611 	dirty = (cmd->body.transfer == SVGA3D_WRITE_HOST_VRAM) ?
1612 		VMW_RES_DIRTY_SET : 0;
1613 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface, dirty,
1614 				user_surface_converter, &cmd->body.host.sid,
1615 				NULL);
1616 	if (unlikely(ret != 0)) {
1617 		if (unlikely(ret != -ERESTARTSYS))
1618 			VMW_DEBUG_USER("could not find surface for DMA.\n");
1619 		return ret;
1620 	}
1621 
1622 	res = sw_context->res_cache[vmw_res_surface].res;
1623 	if (!res) {
1624 		VMW_DEBUG_USER("Invalid DMA surface.\n");
1625 		return -EINVAL;
1626 	}
1627 
1628 	srf = vmw_res_to_srf(res);
1629 	vmw_kms_cursor_snoop(srf, sw_context->fp->tfile, &vmw_bo->tbo,
1630 			     header);
1631 
1632 	return 0;
1633 }
1634 
1635 static int vmw_cmd_draw(struct vmw_private *dev_priv,
1636 			struct vmw_sw_context *sw_context,
1637 			SVGA3dCmdHeader *header)
1638 {
1639 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDrawPrimitives);
1640 	SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
1641 		(unsigned long)header + sizeof(*cmd));
1642 	SVGA3dPrimitiveRange *range;
1643 	uint32_t i;
1644 	uint32_t maxnum;
1645 	int ret;
1646 
1647 	cmd = container_of(header, typeof(*cmd), header);
1648 
1649 	if (unlikely(header->size < sizeof(cmd->body))) {
1650 		VMW_DEBUG_USER("Illegal DRAW_PRIMITIVES header size.\n");
1651 		return -EINVAL;
1652 	}
1653 
1654 	ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1655 	if (unlikely(ret != 0))
1656 		return ret;
1657 
1658 	maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
1659 
1660 	if (unlikely(cmd->body.numVertexDecls > maxnum)) {
1661 		VMW_DEBUG_USER("Illegal number of vertex declarations.\n");
1662 		return -EINVAL;
1663 	}
1664 
1665 	for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
1666 		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1667 					VMW_RES_DIRTY_NONE,
1668 					user_surface_converter,
1669 					&decl->array.surfaceId, NULL);
1670 		if (unlikely(ret != 0))
1671 			return ret;
1672 	}
1673 
1674 	maxnum = (header->size - sizeof(cmd->body) -
1675 		  cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
1676 	if (unlikely(cmd->body.numRanges > maxnum)) {
1677 		VMW_DEBUG_USER("Illegal number of index ranges.\n");
1678 		return -EINVAL;
1679 	}
1680 
1681 	range = (SVGA3dPrimitiveRange *) decl;
1682 	for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
1683 		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1684 					VMW_RES_DIRTY_NONE,
1685 					user_surface_converter,
1686 					&range->indexArray.surfaceId, NULL);
1687 		if (unlikely(ret != 0))
1688 			return ret;
1689 	}
1690 	return 0;
1691 }
1692 
1693 static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
1694 			     struct vmw_sw_context *sw_context,
1695 			     SVGA3dCmdHeader *header)
1696 {
1697 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetTextureState);
1698 	SVGA3dTextureState *last_state = (SVGA3dTextureState *)
1699 	  ((unsigned long) header + header->size + sizeof(*header));
1700 	SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
1701 		((unsigned long) header + sizeof(*cmd));
1702 	struct vmw_resource *ctx;
1703 	struct vmw_resource *res;
1704 	int ret;
1705 
1706 	cmd = container_of(header, typeof(*cmd), header);
1707 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1708 		return -EINVAL;
1709 
1710 	/* The loop below must not read a truncated trailing state. */
1711 	if ((header->size - sizeof(cmd->body)) % sizeof(*cur_state)) {
1712 		VMW_DEBUG_USER("Invalid texture state size %u.\n",
1713 			       header->size);
1714 		return -EINVAL;
1715 	}
1716 
1717 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
1718 				VMW_RES_DIRTY_SET, user_context_converter,
1719 				&cmd->body.cid, &ctx);
1720 	if (unlikely(ret != 0))
1721 		return ret;
1722 
1723 	for (; cur_state < last_state; ++cur_state) {
1724 		if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
1725 			continue;
1726 
1727 		if (cur_state->stage >= SVGA3D_NUM_TEXTURE_UNITS) {
1728 			VMW_DEBUG_USER("Illegal texture/sampler unit %u.\n",
1729 				       (unsigned int) cur_state->stage);
1730 			return -EINVAL;
1731 		}
1732 
1733 		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1734 					VMW_RES_DIRTY_NONE,
1735 					user_surface_converter,
1736 					&cur_state->value, &res);
1737 		if (unlikely(ret != 0))
1738 			return ret;
1739 
1740 		if (dev_priv->has_mob) {
1741 			struct vmw_ctx_bindinfo_tex binding;
1742 			struct vmw_ctx_validation_info *node;
1743 
1744 			node = vmw_execbuf_info_from_res(sw_context, ctx);
1745 			if (!node)
1746 				return -EINVAL;
1747 
1748 			binding.bi.ctx = ctx;
1749 			binding.bi.res = res;
1750 			binding.bi.bt = vmw_ctx_binding_tex;
1751 			binding.texture_stage = cur_state->stage;
1752 			vmw_binding_add(node->staged, &binding.bi, 0,
1753 					binding.texture_stage);
1754 		}
1755 	}
1756 
1757 	return 0;
1758 }
1759 
1760 static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
1761 				      struct vmw_sw_context *sw_context,
1762 				      void *buf)
1763 {
1764 	struct vmw_bo *vmw_bo;
1765 
1766 	struct {
1767 		uint32_t header;
1768 		SVGAFifoCmdDefineGMRFB body;
1769 	} *cmd = buf;
1770 
1771 	return vmw_translate_guest_ptr(dev_priv, sw_context, &cmd->body.ptr,
1772 				       &vmw_bo);
1773 }
1774 
1775 /**
1776  * vmw_cmd_res_switch_backup - Utility function to handle backup buffer
1777  * switching
1778  *
1779  * @dev_priv: Pointer to a device private struct.
1780  * @sw_context: The software context being used for this batch.
1781  * @res: Pointer to the resource.
1782  * @buf_id: Pointer to the user-space backup buffer handle in the command
1783  * stream.
1784  * @backup_offset: Offset of backup into MOB.
1785  *
1786  * This function prepares for registering a switch of backup buffers in the
1787  * resource metadata just prior to unreserving. It's basically a wrapper around
1788  * vmw_cmd_res_switch_backup with a different interface.
1789  */
1790 static int vmw_cmd_res_switch_backup(struct vmw_private *dev_priv,
1791 				     struct vmw_sw_context *sw_context,
1792 				     struct vmw_resource *res, uint32_t *buf_id,
1793 				     unsigned long backup_offset)
1794 {
1795 	struct vmw_bo *vbo;
1796 	void *info;
1797 	u64 end;
1798 	int ret;
1799 
1800 	info = vmw_execbuf_info_from_res(sw_context, res);
1801 	if (!info)
1802 		return -EINVAL;
1803 
1804 	ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &vbo);
1805 	if (ret)
1806 		return ret;
1807 
1808 	if (check_add_overflow((u64)backup_offset,
1809 			       (u64)res->guest_memory_size, &end) ||
1810 	    end > vbo->tbo.base.size) {
1811 		VMW_DEBUG_USER("Backup buffer too small for resource.\n");
1812 		return -EINVAL;
1813 	}
1814 
1815 	vmw_validation_res_switch_backup(sw_context->ctx, info, vbo,
1816 					 backup_offset);
1817 	return 0;
1818 }
1819 
1820 /**
1821  * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
1822  *
1823  * @dev_priv: Pointer to a device private struct.
1824  * @sw_context: The software context being used for this batch.
1825  * @res_type: The resource type.
1826  * @converter: Information about user-space binding for this resource type.
1827  * @res_id: Pointer to the user-space resource handle in the command stream.
1828  * @buf_id: Pointer to the user-space backup buffer handle in the command
1829  * stream.
1830  * @backup_offset: Offset of backup into MOB.
1831  *
1832  * This function prepares for registering a switch of backup buffers in the
1833  * resource metadata just prior to unreserving. It's basically a wrapper around
1834  * vmw_cmd_res_switch_backup with a different interface.
1835  */
1836 static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
1837 				 struct vmw_sw_context *sw_context,
1838 				 enum vmw_res_type res_type,
1839 				 const struct vmw_user_resource_conv
1840 				 *converter, uint32_t *res_id, uint32_t *buf_id,
1841 				 unsigned long backup_offset)
1842 {
1843 	struct vmw_resource *res;
1844 	int ret;
1845 
1846 	ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
1847 				VMW_RES_DIRTY_NONE, converter, res_id, &res);
1848 	if (ret)
1849 		return ret;
1850 
1851 	return vmw_cmd_res_switch_backup(dev_priv, sw_context, res, buf_id,
1852 					 backup_offset);
1853 }
1854 
1855 /**
1856  * vmw_cmd_bind_gb_surface - Validate SVGA_3D_CMD_BIND_GB_SURFACE command
1857  *
1858  * @dev_priv: Pointer to a device private struct.
1859  * @sw_context: The software context being used for this batch.
1860  * @header: Pointer to the command header in the command stream.
1861  */
1862 static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
1863 				   struct vmw_sw_context *sw_context,
1864 				   SVGA3dCmdHeader *header)
1865 {
1866 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBindGBSurface) =
1867 		container_of(header, typeof(*cmd), header);
1868 
1869 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1870 		return -EINVAL;
1871 
1872 	return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
1873 				     user_surface_converter, &cmd->body.sid,
1874 				     &cmd->body.mobid, 0);
1875 }
1876 
1877 /**
1878  * vmw_cmd_update_gb_image - Validate SVGA_3D_CMD_UPDATE_GB_IMAGE command
1879  *
1880  * @dev_priv: Pointer to a device private struct.
1881  * @sw_context: The software context being used for this batch.
1882  * @header: Pointer to the command header in the command stream.
1883  */
1884 static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
1885 				   struct vmw_sw_context *sw_context,
1886 				   SVGA3dCmdHeader *header)
1887 {
1888 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdUpdateGBImage) =
1889 		container_of(header, typeof(*cmd), header);
1890 
1891 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1892 		return -EINVAL;
1893 
1894 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1895 				 VMW_RES_DIRTY_NONE, user_surface_converter,
1896 				 &cmd->body.image.sid, NULL);
1897 }
1898 
1899 /**
1900  * vmw_cmd_update_gb_surface - Validate SVGA_3D_CMD_UPDATE_GB_SURFACE command
1901  *
1902  * @dev_priv: Pointer to a device private struct.
1903  * @sw_context: The software context being used for this batch.
1904  * @header: Pointer to the command header in the command stream.
1905  */
1906 static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
1907 				     struct vmw_sw_context *sw_context,
1908 				     SVGA3dCmdHeader *header)
1909 {
1910 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdUpdateGBSurface) =
1911 		container_of(header, typeof(*cmd), header);
1912 
1913 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1914 		return -EINVAL;
1915 
1916 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1917 				 VMW_RES_DIRTY_CLEAR, user_surface_converter,
1918 				 &cmd->body.sid, NULL);
1919 }
1920 
1921 /**
1922  * vmw_cmd_readback_gb_image - Validate SVGA_3D_CMD_READBACK_GB_IMAGE command
1923  *
1924  * @dev_priv: Pointer to a device private struct.
1925  * @sw_context: The software context being used for this batch.
1926  * @header: Pointer to the command header in the command stream.
1927  */
1928 static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
1929 				     struct vmw_sw_context *sw_context,
1930 				     SVGA3dCmdHeader *header)
1931 {
1932 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdReadbackGBImage) =
1933 		container_of(header, typeof(*cmd), header);
1934 
1935 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1936 		return -EINVAL;
1937 
1938 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1939 				 VMW_RES_DIRTY_NONE, user_surface_converter,
1940 				 &cmd->body.image.sid, NULL);
1941 }
1942 
1943 /**
1944  * vmw_cmd_readback_gb_surface - Validate SVGA_3D_CMD_READBACK_GB_SURFACE
1945  * command
1946  *
1947  * @dev_priv: Pointer to a device private struct.
1948  * @sw_context: The software context being used for this batch.
1949  * @header: Pointer to the command header in the command stream.
1950  */
1951 static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
1952 				       struct vmw_sw_context *sw_context,
1953 				       SVGA3dCmdHeader *header)
1954 {
1955 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdReadbackGBSurface) =
1956 		container_of(header, typeof(*cmd), header);
1957 
1958 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1959 		return -EINVAL;
1960 
1961 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1962 				 VMW_RES_DIRTY_CLEAR, user_surface_converter,
1963 				 &cmd->body.sid, NULL);
1964 }
1965 
1966 /**
1967  * vmw_cmd_invalidate_gb_image - Validate SVGA_3D_CMD_INVALIDATE_GB_IMAGE
1968  * command
1969  *
1970  * @dev_priv: Pointer to a device private struct.
1971  * @sw_context: The software context being used for this batch.
1972  * @header: Pointer to the command header in the command stream.
1973  */
1974 static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
1975 				       struct vmw_sw_context *sw_context,
1976 				       SVGA3dCmdHeader *header)
1977 {
1978 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdInvalidateGBImage) =
1979 		container_of(header, typeof(*cmd), header);
1980 
1981 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
1982 		return -EINVAL;
1983 
1984 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
1985 				 VMW_RES_DIRTY_NONE, user_surface_converter,
1986 				 &cmd->body.image.sid, NULL);
1987 }
1988 
1989 /**
1990  * vmw_cmd_invalidate_gb_surface - Validate SVGA_3D_CMD_INVALIDATE_GB_SURFACE
1991  * command
1992  *
1993  * @dev_priv: Pointer to a device private struct.
1994  * @sw_context: The software context being used for this batch.
1995  * @header: Pointer to the command header in the command stream.
1996  */
1997 static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
1998 					 struct vmw_sw_context *sw_context,
1999 					 SVGA3dCmdHeader *header)
2000 {
2001 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdInvalidateGBSurface) =
2002 		container_of(header, typeof(*cmd), header);
2003 
2004 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2005 		return -EINVAL;
2006 
2007 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
2008 				 VMW_RES_DIRTY_CLEAR, user_surface_converter,
2009 				 &cmd->body.sid, NULL);
2010 }
2011 
2012 /**
2013  * vmw_cmd_shader_define - Validate SVGA_3D_CMD_SHADER_DEFINE command
2014  *
2015  * @dev_priv: Pointer to a device private struct.
2016  * @sw_context: The software context being used for this batch.
2017  * @header: Pointer to the command header in the command stream.
2018  */
2019 static int vmw_cmd_shader_define(struct vmw_private *dev_priv,
2020 				 struct vmw_sw_context *sw_context,
2021 				 SVGA3dCmdHeader *header)
2022 {
2023 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDefineShader);
2024 	int ret;
2025 	size_t size;
2026 	struct vmw_resource *ctx;
2027 
2028 	cmd = container_of(header, typeof(*cmd), header);
2029 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2030 		return -EINVAL;
2031 
2032 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
2033 				VMW_RES_DIRTY_SET, user_context_converter,
2034 				&cmd->body.cid, &ctx);
2035 	if (unlikely(ret != 0))
2036 		return ret;
2037 
2038 	if (unlikely(!dev_priv->has_mob))
2039 		return 0;
2040 
2041 	size = cmd->header.size - sizeof(cmd->body);
2042 	ret = vmw_compat_shader_add(dev_priv, vmw_context_res_man(ctx),
2043 				    cmd->body.shid, cmd + 1, cmd->body.type,
2044 				    size, &sw_context->staged_cmd_res);
2045 	if (unlikely(ret != 0))
2046 		return ret;
2047 
2048 	return vmw_resource_relocation_add(sw_context, NULL,
2049 					   vmw_ptr_diff(sw_context->buf_start,
2050 							&cmd->header.id),
2051 					   vmw_res_rel_nop);
2052 }
2053 
2054 /**
2055  * vmw_cmd_shader_destroy - Validate SVGA_3D_CMD_SHADER_DESTROY command
2056  *
2057  * @dev_priv: Pointer to a device private struct.
2058  * @sw_context: The software context being used for this batch.
2059  * @header: Pointer to the command header in the command stream.
2060  */
2061 static int vmw_cmd_shader_destroy(struct vmw_private *dev_priv,
2062 				  struct vmw_sw_context *sw_context,
2063 				  SVGA3dCmdHeader *header)
2064 {
2065 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDestroyShader);
2066 	int ret;
2067 	struct vmw_resource *ctx;
2068 
2069 	cmd = container_of(header, typeof(*cmd), header);
2070 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2071 		return -EINVAL;
2072 
2073 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
2074 				VMW_RES_DIRTY_SET, user_context_converter,
2075 				&cmd->body.cid, &ctx);
2076 	if (unlikely(ret != 0))
2077 		return ret;
2078 
2079 	if (unlikely(!dev_priv->has_mob))
2080 		return 0;
2081 
2082 	ret = vmw_shader_remove(vmw_context_res_man(ctx), cmd->body.shid,
2083 				cmd->body.type, &sw_context->staged_cmd_res);
2084 	if (unlikely(ret != 0))
2085 		return ret;
2086 
2087 	return vmw_resource_relocation_add(sw_context, NULL,
2088 					   vmw_ptr_diff(sw_context->buf_start,
2089 							&cmd->header.id),
2090 					   vmw_res_rel_nop);
2091 }
2092 
2093 /**
2094  * vmw_cmd_set_shader - Validate SVGA_3D_CMD_SET_SHADER command
2095  *
2096  * @dev_priv: Pointer to a device private struct.
2097  * @sw_context: The software context being used for this batch.
2098  * @header: Pointer to the command header in the command stream.
2099  */
2100 static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
2101 			      struct vmw_sw_context *sw_context,
2102 			      SVGA3dCmdHeader *header)
2103 {
2104 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetShader);
2105 	struct vmw_ctx_bindinfo_shader binding;
2106 	struct vmw_resource *ctx, *res = NULL;
2107 	struct vmw_ctx_validation_info *ctx_info;
2108 	int ret;
2109 
2110 	cmd = container_of(header, typeof(*cmd), header);
2111 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2112 		return -EINVAL;
2113 
2114 	if (!vmw_shadertype_is_valid(VMW_SM_LEGACY, cmd->body.type)) {
2115 		VMW_DEBUG_USER("Illegal shader type %u.\n",
2116 			       (unsigned int) cmd->body.type);
2117 		return -EINVAL;
2118 	}
2119 
2120 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
2121 				VMW_RES_DIRTY_SET, user_context_converter,
2122 				&cmd->body.cid, &ctx);
2123 	if (unlikely(ret != 0))
2124 		return ret;
2125 
2126 	if (!dev_priv->has_mob)
2127 		return 0;
2128 
2129 	if (cmd->body.shid != SVGA3D_INVALID_ID) {
2130 		/*
2131 		 * This is the compat shader path - Per device guest-backed
2132 		 * shaders, but user-space thinks it's per context host-
2133 		 * backed shaders.
2134 		 */
2135 		res = vmw_shader_lookup(vmw_context_res_man(ctx),
2136 					cmd->body.shid, cmd->body.type);
2137 		if (!IS_ERR(res)) {
2138 			ret = vmw_execbuf_res_val_add(sw_context, res,
2139 						      VMW_RES_DIRTY_NONE,
2140 						      vmw_val_add_flag_noctx);
2141 			if (unlikely(ret != 0))
2142 				return ret;
2143 
2144 			ret = vmw_resource_relocation_add
2145 				(sw_context, res,
2146 				 vmw_ptr_diff(sw_context->buf_start,
2147 					      &cmd->body.shid),
2148 				 vmw_res_rel_normal);
2149 			if (unlikely(ret != 0))
2150 				return ret;
2151 		}
2152 	}
2153 
2154 	if (IS_ERR_OR_NULL(res)) {
2155 		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_shader,
2156 					VMW_RES_DIRTY_NONE,
2157 					user_shader_converter, &cmd->body.shid,
2158 					&res);
2159 		if (unlikely(ret != 0))
2160 			return ret;
2161 	}
2162 
2163 	ctx_info = vmw_execbuf_info_from_res(sw_context, ctx);
2164 	if (!ctx_info)
2165 		return -EINVAL;
2166 
2167 	binding.bi.ctx = ctx;
2168 	binding.bi.res = res;
2169 	binding.bi.bt = vmw_ctx_binding_shader;
2170 	binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2171 	vmw_binding_add(ctx_info->staged, &binding.bi, binding.shader_slot, 0);
2172 
2173 	return 0;
2174 }
2175 
2176 /**
2177  * vmw_cmd_set_shader_const - Validate SVGA_3D_CMD_SET_SHADER_CONST command
2178  *
2179  * @dev_priv: Pointer to a device private struct.
2180  * @sw_context: The software context being used for this batch.
2181  * @header: Pointer to the command header in the command stream.
2182  */
2183 static int vmw_cmd_set_shader_const(struct vmw_private *dev_priv,
2184 				    struct vmw_sw_context *sw_context,
2185 				    SVGA3dCmdHeader *header)
2186 {
2187 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetShaderConst);
2188 	int ret;
2189 
2190 	cmd = container_of(header, typeof(*cmd), header);
2191 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2192 		return -EINVAL;
2193 
2194 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
2195 				VMW_RES_DIRTY_SET, user_context_converter,
2196 				&cmd->body.cid, NULL);
2197 	if (unlikely(ret != 0))
2198 		return ret;
2199 
2200 	if (dev_priv->has_mob)
2201 		header->id = SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE;
2202 
2203 	return 0;
2204 }
2205 
2206 /**
2207  * vmw_cmd_bind_gb_shader - Validate SVGA_3D_CMD_BIND_GB_SHADER command
2208  *
2209  * @dev_priv: Pointer to a device private struct.
2210  * @sw_context: The software context being used for this batch.
2211  * @header: Pointer to the command header in the command stream.
2212  */
2213 static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
2214 				  struct vmw_sw_context *sw_context,
2215 				  SVGA3dCmdHeader *header)
2216 {
2217 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBindGBShader) =
2218 		container_of(header, typeof(*cmd), header);
2219 
2220 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2221 		return -EINVAL;
2222 
2223 	return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
2224 				     user_shader_converter, &cmd->body.shid,
2225 				     &cmd->body.mobid, cmd->body.offsetInBytes);
2226 }
2227 
2228 /**
2229  * vmw_cmd_dx_set_single_constant_buffer - Validate
2230  * SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER command.
2231  *
2232  * @dev_priv: Pointer to a device private struct.
2233  * @sw_context: The software context being used for this batch.
2234  * @header: Pointer to the command header in the command stream.
2235  */
2236 static int
2237 vmw_cmd_dx_set_single_constant_buffer(struct vmw_private *dev_priv,
2238 				      struct vmw_sw_context *sw_context,
2239 				      SVGA3dCmdHeader *header)
2240 {
2241 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetSingleConstantBuffer);
2242 
2243 	struct vmw_resource *res = NULL;
2244 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2245 	struct vmw_ctx_bindinfo_cb binding;
2246 	int ret;
2247 
2248 	if (!ctx_node)
2249 		return -EINVAL;
2250 
2251 	cmd = container_of(header, typeof(*cmd), header);
2252 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2253 		return -EINVAL;
2254 
2255 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
2256 				VMW_RES_DIRTY_NONE, user_surface_converter,
2257 				&cmd->body.sid, &res);
2258 	if (unlikely(ret != 0))
2259 		return ret;
2260 
2261 	if (!vmw_shadertype_is_valid(dev_priv->sm_type, cmd->body.type) ||
2262 	    cmd->body.slot >= SVGA3D_DX_MAX_CONSTBUFFERS) {
2263 		VMW_DEBUG_USER("Illegal const buffer shader %u slot %u.\n",
2264 			       (unsigned int) cmd->body.type,
2265 			       (unsigned int) cmd->body.slot);
2266 		return -EINVAL;
2267 	}
2268 
2269 	binding.bi.ctx = ctx_node->ctx;
2270 	binding.bi.res = res;
2271 	binding.bi.bt = vmw_ctx_binding_cb;
2272 	binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2273 	binding.offset = cmd->body.offsetInBytes;
2274 	binding.size = cmd->body.sizeInBytes;
2275 	binding.slot = cmd->body.slot;
2276 
2277 	vmw_binding_add(ctx_node->staged, &binding.bi, binding.shader_slot,
2278 			binding.slot);
2279 
2280 	return 0;
2281 }
2282 
2283 /**
2284  * vmw_cmd_dx_set_constant_buffer_offset - Validate
2285  * SVGA_3D_CMD_DX_SET_VS/PS/GS/HS/DS/CS_CONSTANT_BUFFER_OFFSET command.
2286  *
2287  * @dev_priv: Pointer to a device private struct.
2288  * @sw_context: The software context being used for this batch.
2289  * @header: Pointer to the command header in the command stream.
2290  */
2291 static int
2292 vmw_cmd_dx_set_constant_buffer_offset(struct vmw_private *dev_priv,
2293 				      struct vmw_sw_context *sw_context,
2294 				      SVGA3dCmdHeader *header)
2295 {
2296 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetConstantBufferOffset);
2297 
2298 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2299 	u32 shader_slot;
2300 
2301 	if (!has_sm5_context(dev_priv))
2302 		return -EINVAL;
2303 
2304 	if (!ctx_node)
2305 		return -EINVAL;
2306 
2307 	cmd = container_of(header, typeof(*cmd), header);
2308 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2309 		return -EINVAL;
2310 
2311 	if (cmd->body.slot >= SVGA3D_DX_MAX_CONSTBUFFERS) {
2312 		VMW_DEBUG_USER("Illegal const buffer slot %u.\n",
2313 			       (unsigned int) cmd->body.slot);
2314 		return -EINVAL;
2315 	}
2316 
2317 	shader_slot = cmd->header.id - SVGA_3D_CMD_DX_SET_VS_CONSTANT_BUFFER_OFFSET;
2318 	vmw_binding_cb_offset_update(ctx_node->staged, shader_slot,
2319 				     cmd->body.slot, cmd->body.offsetInBytes);
2320 
2321 	return 0;
2322 }
2323 
2324 /**
2325  * vmw_cmd_dx_set_shader_res - Validate SVGA_3D_CMD_DX_SET_SHADER_RESOURCES
2326  * command
2327  *
2328  * @dev_priv: Pointer to a device private struct.
2329  * @sw_context: The software context being used for this batch.
2330  * @header: Pointer to the command header in the command stream.
2331  */
2332 static int vmw_cmd_dx_set_shader_res(struct vmw_private *dev_priv,
2333 				     struct vmw_sw_context *sw_context,
2334 				     SVGA3dCmdHeader *header)
2335 {
2336 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetShaderResources) =
2337 		container_of(header, typeof(*cmd), header);
2338 
2339 	u32 num_sr_view = (cmd->header.size - sizeof(cmd->body)) /
2340 		sizeof(SVGA3dShaderResourceViewId);
2341 
2342 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2343 		return -EINVAL;
2344 
2345 	if ((u64) cmd->body.startView + (u64) num_sr_view >
2346 	    (u64) SVGA3D_DX_MAX_SRVIEWS ||
2347 	    !vmw_shadertype_is_valid(dev_priv->sm_type, cmd->body.type)) {
2348 		VMW_DEBUG_USER("Invalid shader binding.\n");
2349 		return -EINVAL;
2350 	}
2351 
2352 	return vmw_view_bindings_add(sw_context, vmw_view_sr,
2353 				     vmw_ctx_binding_sr,
2354 				     cmd->body.type - SVGA3D_SHADERTYPE_MIN,
2355 				     (void *) &cmd[1], num_sr_view,
2356 				     cmd->body.startView);
2357 }
2358 
2359 /**
2360  * vmw_cmd_dx_set_shader - Validate SVGA_3D_CMD_DX_SET_SHADER command
2361  *
2362  * @dev_priv: Pointer to a device private struct.
2363  * @sw_context: The software context being used for this batch.
2364  * @header: Pointer to the command header in the command stream.
2365  */
2366 static int vmw_cmd_dx_set_shader(struct vmw_private *dev_priv,
2367 				 struct vmw_sw_context *sw_context,
2368 				 SVGA3dCmdHeader *header)
2369 {
2370 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetShader);
2371 	struct vmw_resource *res = NULL;
2372 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2373 	struct vmw_ctx_bindinfo_shader binding;
2374 	int ret = 0;
2375 
2376 	if (!ctx_node)
2377 		return -EINVAL;
2378 
2379 	cmd = container_of(header, typeof(*cmd), header);
2380 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2381 		return -EINVAL;
2382 
2383 	if (!vmw_shadertype_is_valid(dev_priv->sm_type, cmd->body.type)) {
2384 		VMW_DEBUG_USER("Illegal shader type %u.\n",
2385 			       (unsigned int) cmd->body.type);
2386 		return -EINVAL;
2387 	}
2388 
2389 	if (cmd->body.shaderId != SVGA3D_INVALID_ID) {
2390 		res = vmw_shader_lookup(sw_context->man, cmd->body.shaderId, 0);
2391 		if (IS_ERR(res)) {
2392 			VMW_DEBUG_USER("Could not find shader for binding.\n");
2393 			return PTR_ERR(res);
2394 		}
2395 
2396 		ret = vmw_execbuf_res_val_add(sw_context, res,
2397 					      VMW_RES_DIRTY_NONE,
2398 					      vmw_val_add_flag_noctx);
2399 		if (ret)
2400 			return ret;
2401 	}
2402 
2403 	binding.bi.ctx = ctx_node->ctx;
2404 	binding.bi.res = res;
2405 	binding.bi.bt = vmw_ctx_binding_dx_shader;
2406 	binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2407 
2408 	vmw_binding_add(ctx_node->staged, &binding.bi, binding.shader_slot, 0);
2409 
2410 	return 0;
2411 }
2412 
2413 /**
2414  * vmw_cmd_dx_set_vertex_buffers - Validates SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS
2415  * command
2416  *
2417  * @dev_priv: Pointer to a device private struct.
2418  * @sw_context: The software context being used for this batch.
2419  * @header: Pointer to the command header in the command stream.
2420  */
2421 static int vmw_cmd_dx_set_vertex_buffers(struct vmw_private *dev_priv,
2422 					 struct vmw_sw_context *sw_context,
2423 					 SVGA3dCmdHeader *header)
2424 {
2425 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2426 	struct vmw_ctx_bindinfo_vb binding;
2427 	struct vmw_resource *res;
2428 	struct {
2429 		SVGA3dCmdHeader header;
2430 		SVGA3dCmdDXSetVertexBuffers body;
2431 		SVGA3dVertexBuffer buf[];
2432 	} *cmd;
2433 	int i, ret, num;
2434 
2435 	if (!ctx_node)
2436 		return -EINVAL;
2437 
2438 	cmd = container_of(header, typeof(*cmd), header);
2439 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2440 		return -EINVAL;
2441 
2442 	num = (cmd->header.size - sizeof(cmd->body)) /
2443 		sizeof(SVGA3dVertexBuffer);
2444 	if ((u64)num + (u64)cmd->body.startBuffer >
2445 	    (u64)SVGA3D_DX_MAX_VERTEXBUFFERS) {
2446 		VMW_DEBUG_USER("Invalid number of vertex buffers.\n");
2447 		return -EINVAL;
2448 	}
2449 
2450 	for (i = 0; i < num; i++) {
2451 		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
2452 					VMW_RES_DIRTY_NONE,
2453 					user_surface_converter,
2454 					&cmd->buf[i].sid, &res);
2455 		if (unlikely(ret != 0))
2456 			return ret;
2457 
2458 		binding.bi.ctx = ctx_node->ctx;
2459 		binding.bi.bt = vmw_ctx_binding_vb;
2460 		binding.bi.res = res;
2461 		binding.offset = cmd->buf[i].offset;
2462 		binding.stride = cmd->buf[i].stride;
2463 		binding.slot = i + cmd->body.startBuffer;
2464 
2465 		vmw_binding_add(ctx_node->staged, &binding.bi, 0, binding.slot);
2466 	}
2467 
2468 	return 0;
2469 }
2470 
2471 /**
2472  * vmw_cmd_dx_set_index_buffer - Validate
2473  * SVGA_3D_CMD_DX_IA_SET_INDEX_BUFFER command.
2474  *
2475  * @dev_priv: Pointer to a device private struct.
2476  * @sw_context: The software context being used for this batch.
2477  * @header: Pointer to the command header in the command stream.
2478  */
2479 static int vmw_cmd_dx_set_index_buffer(struct vmw_private *dev_priv,
2480 				       struct vmw_sw_context *sw_context,
2481 				       SVGA3dCmdHeader *header)
2482 {
2483 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2484 	struct vmw_ctx_bindinfo_ib binding;
2485 	struct vmw_resource *res;
2486 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetIndexBuffer);
2487 	int ret;
2488 
2489 	if (!ctx_node)
2490 		return -EINVAL;
2491 
2492 	cmd = container_of(header, typeof(*cmd), header);
2493 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2494 		return -EINVAL;
2495 
2496 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
2497 				VMW_RES_DIRTY_NONE, user_surface_converter,
2498 				&cmd->body.sid, &res);
2499 	if (unlikely(ret != 0))
2500 		return ret;
2501 
2502 	binding.bi.ctx = ctx_node->ctx;
2503 	binding.bi.res = res;
2504 	binding.bi.bt = vmw_ctx_binding_ib;
2505 	binding.offset = cmd->body.offset;
2506 	binding.format = cmd->body.format;
2507 
2508 	vmw_binding_add(ctx_node->staged, &binding.bi, 0, 0);
2509 
2510 	return 0;
2511 }
2512 
2513 /**
2514  * vmw_cmd_dx_set_rendertargets - Validate SVGA_3D_CMD_DX_SET_RENDERTARGETS
2515  * command
2516  *
2517  * @dev_priv: Pointer to a device private struct.
2518  * @sw_context: The software context being used for this batch.
2519  * @header: Pointer to the command header in the command stream.
2520  */
2521 static int vmw_cmd_dx_set_rendertargets(struct vmw_private *dev_priv,
2522 					struct vmw_sw_context *sw_context,
2523 					SVGA3dCmdHeader *header)
2524 {
2525 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetRenderTargets) =
2526 		container_of(header, typeof(*cmd), header);
2527 	u32 num_rt_view = (cmd->header.size - sizeof(cmd->body)) /
2528 		sizeof(SVGA3dRenderTargetViewId);
2529 	int ret;
2530 
2531 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2532 		return -EINVAL;
2533 
2534 	if (num_rt_view > SVGA3D_DX_MAX_RENDER_TARGETS) {
2535 		VMW_DEBUG_USER("Invalid DX Rendertarget binding.\n");
2536 		return -EINVAL;
2537 	}
2538 
2539 	ret = vmw_view_bindings_add(sw_context, vmw_view_ds, vmw_ctx_binding_ds,
2540 				    0, &cmd->body.depthStencilViewId, 1, 0);
2541 	if (ret)
2542 		return ret;
2543 
2544 	return vmw_view_bindings_add(sw_context, vmw_view_rt,
2545 				     vmw_ctx_binding_dx_rt, 0, (void *)&cmd[1],
2546 				     num_rt_view, 0);
2547 }
2548 
2549 /**
2550  * vmw_cmd_dx_clear_rendertarget_view - Validate
2551  * SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW command
2552  *
2553  * @dev_priv: Pointer to a device private struct.
2554  * @sw_context: The software context being used for this batch.
2555  * @header: Pointer to the command header in the command stream.
2556  */
2557 static int vmw_cmd_dx_clear_rendertarget_view(struct vmw_private *dev_priv,
2558 					      struct vmw_sw_context *sw_context,
2559 					      SVGA3dCmdHeader *header)
2560 {
2561 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXClearRenderTargetView) =
2562 		container_of(header, typeof(*cmd), header);
2563 	struct vmw_resource *ret;
2564 
2565 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2566 		return -EINVAL;
2567 
2568 	ret = vmw_view_id_val_add(sw_context, vmw_view_rt,
2569 				  cmd->body.renderTargetViewId);
2570 
2571 	return PTR_ERR_OR_ZERO(ret);
2572 }
2573 
2574 /**
2575  * vmw_cmd_dx_clear_depthstencil_view - Validate
2576  * SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW command
2577  *
2578  * @dev_priv: Pointer to a device private struct.
2579  * @sw_context: The software context being used for this batch.
2580  * @header: Pointer to the command header in the command stream.
2581  */
2582 static int vmw_cmd_dx_clear_depthstencil_view(struct vmw_private *dev_priv,
2583 					      struct vmw_sw_context *sw_context,
2584 					      SVGA3dCmdHeader *header)
2585 {
2586 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXClearDepthStencilView) =
2587 		container_of(header, typeof(*cmd), header);
2588 	struct vmw_resource *ret;
2589 
2590 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2591 		return -EINVAL;
2592 
2593 	ret = vmw_view_id_val_add(sw_context, vmw_view_ds,
2594 				  cmd->body.depthStencilViewId);
2595 
2596 	return PTR_ERR_OR_ZERO(ret);
2597 }
2598 
2599 static int vmw_cmd_dx_view_define(struct vmw_private *dev_priv,
2600 				  struct vmw_sw_context *sw_context,
2601 				  SVGA3dCmdHeader *header)
2602 {
2603 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2604 	struct vmw_resource *srf;
2605 	struct vmw_resource *res;
2606 	enum vmw_view_type view_type;
2607 	int ret;
2608 	/*
2609 	 * This is based on the fact that all affected define commands have the
2610 	 * same initial command body layout.
2611 	 */
2612 	struct {
2613 		SVGA3dCmdHeader header;
2614 		uint32 defined_id;
2615 		uint32 sid;
2616 	} *cmd;
2617 
2618 	if (!ctx_node)
2619 		return -EINVAL;
2620 
2621 	view_type = vmw_view_cmd_to_type(header->id);
2622 	if (view_type == vmw_view_max)
2623 		return -EINVAL;
2624 
2625 	cmd = container_of(header, typeof(*cmd), header);
2626 	if (!vmw_cmd_body_fits(header, sizeof(*cmd) - sizeof(*header)))
2627 		return -EINVAL;
2628 
2629 	if (unlikely(cmd->sid == SVGA3D_INVALID_ID)) {
2630 		VMW_DEBUG_USER("Invalid surface id.\n");
2631 		return -EINVAL;
2632 	}
2633 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
2634 				VMW_RES_DIRTY_NONE, user_surface_converter,
2635 				&cmd->sid, &srf);
2636 	if (unlikely(ret != 0))
2637 		return ret;
2638 
2639 	res = vmw_context_cotable(ctx_node->ctx, vmw_view_cotables[view_type]);
2640 	if (IS_ERR_OR_NULL(res))
2641 		return res ? PTR_ERR(res) : -EINVAL;
2642 	ret = vmw_cotable_notify(res, cmd->defined_id);
2643 	if (unlikely(ret != 0))
2644 		return ret;
2645 
2646 	return vmw_view_add(sw_context->man, ctx_node->ctx, srf, view_type,
2647 			    cmd->defined_id, header,
2648 			    header->size + sizeof(*header),
2649 			    &sw_context->staged_cmd_res);
2650 }
2651 
2652 /**
2653  * vmw_cmd_dx_set_so_targets - Validate SVGA_3D_CMD_DX_SET_SOTARGETS command.
2654  *
2655  * @dev_priv: Pointer to a device private struct.
2656  * @sw_context: The software context being used for this batch.
2657  * @header: Pointer to the command header in the command stream.
2658  */
2659 static int vmw_cmd_dx_set_so_targets(struct vmw_private *dev_priv,
2660 				     struct vmw_sw_context *sw_context,
2661 				     SVGA3dCmdHeader *header)
2662 {
2663 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2664 	struct vmw_ctx_bindinfo_so_target binding;
2665 	struct vmw_resource *res;
2666 	struct {
2667 		SVGA3dCmdHeader header;
2668 		SVGA3dCmdDXSetSOTargets body;
2669 		SVGA3dSoTarget targets[];
2670 	} *cmd;
2671 	int i, ret, num;
2672 
2673 	if (!ctx_node)
2674 		return -EINVAL;
2675 
2676 	cmd = container_of(header, typeof(*cmd), header);
2677 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2678 		return -EINVAL;
2679 
2680 	num = (cmd->header.size - sizeof(cmd->body)) / sizeof(SVGA3dSoTarget);
2681 
2682 	if (num > SVGA3D_DX_MAX_SOTARGETS) {
2683 		VMW_DEBUG_USER("Invalid DX SO binding.\n");
2684 		return -EINVAL;
2685 	}
2686 
2687 	for (i = 0; i < num; i++) {
2688 		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
2689 					VMW_RES_DIRTY_SET,
2690 					user_surface_converter,
2691 					&cmd->targets[i].sid, &res);
2692 		if (unlikely(ret != 0))
2693 			return ret;
2694 
2695 		binding.bi.ctx = ctx_node->ctx;
2696 		binding.bi.res = res;
2697 		binding.bi.bt = vmw_ctx_binding_so_target;
2698 		binding.offset = cmd->targets[i].offset;
2699 		binding.size = cmd->targets[i].sizeInBytes;
2700 		binding.slot = i;
2701 
2702 		vmw_binding_add(ctx_node->staged, &binding.bi, 0, binding.slot);
2703 	}
2704 
2705 	return 0;
2706 }
2707 
2708 static int vmw_cmd_dx_so_define(struct vmw_private *dev_priv,
2709 				struct vmw_sw_context *sw_context,
2710 				SVGA3dCmdHeader *header)
2711 {
2712 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2713 	struct vmw_resource *res;
2714 	/*
2715 	 * This is based on the fact that all affected define commands have
2716 	 * the same initial command body layout.
2717 	 */
2718 	struct {
2719 		SVGA3dCmdHeader header;
2720 		uint32 defined_id;
2721 	} *cmd;
2722 	enum vmw_so_type so_type;
2723 	int ret;
2724 
2725 	if (!ctx_node)
2726 		return -EINVAL;
2727 
2728 	so_type = vmw_so_cmd_to_type(header->id);
2729 	res = vmw_context_cotable(ctx_node->ctx, vmw_so_cotables[so_type]);
2730 	if (IS_ERR_OR_NULL(res))
2731 		return res ? PTR_ERR(res) : -EINVAL;
2732 	cmd = container_of(header, typeof(*cmd), header);
2733 	if (!vmw_cmd_body_fits(header, sizeof(*cmd) - sizeof(*header)))
2734 		return -EINVAL;
2735 
2736 	ret = vmw_cotable_notify(res, cmd->defined_id);
2737 
2738 	return ret;
2739 }
2740 
2741 /**
2742  * vmw_cmd_dx_check_subresource - Validate SVGA_3D_CMD_DX_[X]_SUBRESOURCE
2743  * command
2744  *
2745  * @dev_priv: Pointer to a device private struct.
2746  * @sw_context: The software context being used for this batch.
2747  * @header: Pointer to the command header in the command stream.
2748  */
2749 static int vmw_cmd_dx_check_subresource(struct vmw_private *dev_priv,
2750 					struct vmw_sw_context *sw_context,
2751 					SVGA3dCmdHeader *header)
2752 {
2753 	struct {
2754 		SVGA3dCmdHeader header;
2755 		union {
2756 			SVGA3dCmdDXReadbackSubResource r_body;
2757 			SVGA3dCmdDXInvalidateSubResource i_body;
2758 			SVGA3dCmdDXUpdateSubResource u_body;
2759 			SVGA3dSurfaceId sid;
2760 		};
2761 	} *cmd;
2762 
2763 	BUILD_BUG_ON(offsetof(typeof(*cmd), r_body.sid) !=
2764 		     offsetof(typeof(*cmd), sid));
2765 	BUILD_BUG_ON(offsetof(typeof(*cmd), i_body.sid) !=
2766 		     offsetof(typeof(*cmd), sid));
2767 	BUILD_BUG_ON(offsetof(typeof(*cmd), u_body.sid) !=
2768 		     offsetof(typeof(*cmd), sid));
2769 
2770 	cmd = container_of(header, typeof(*cmd), header);
2771 	if (!vmw_cmd_body_fits(header, sizeof(cmd->sid)))
2772 		return -EINVAL;
2773 
2774 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
2775 				 VMW_RES_DIRTY_NONE, user_surface_converter,
2776 				 &cmd->sid, NULL);
2777 }
2778 
2779 static int vmw_cmd_dx_cid_check(struct vmw_private *dev_priv,
2780 				struct vmw_sw_context *sw_context,
2781 				SVGA3dCmdHeader *header)
2782 {
2783 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2784 
2785 	if (!ctx_node)
2786 		return -EINVAL;
2787 
2788 	return 0;
2789 }
2790 
2791 /**
2792  * vmw_cmd_dx_view_remove - validate a view remove command and schedule the view
2793  * resource for removal.
2794  *
2795  * @dev_priv: Pointer to a device private struct.
2796  * @sw_context: The software context being used for this batch.
2797  * @header: Pointer to the command header in the command stream.
2798  *
2799  * Check that the view exists, and if it was not created using this command
2800  * batch, conditionally make this command a NOP.
2801  */
2802 static int vmw_cmd_dx_view_remove(struct vmw_private *dev_priv,
2803 				  struct vmw_sw_context *sw_context,
2804 				  SVGA3dCmdHeader *header)
2805 {
2806 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2807 	struct {
2808 		SVGA3dCmdHeader header;
2809 		union vmw_view_destroy body;
2810 	} *cmd = container_of(header, typeof(*cmd), header);
2811 	enum vmw_view_type view_type = vmw_view_cmd_to_type(header->id);
2812 	struct vmw_resource *view;
2813 	int ret;
2814 
2815 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2816 		return -EINVAL;
2817 
2818 	if (!ctx_node)
2819 		return -EINVAL;
2820 
2821 	ret = vmw_view_remove(sw_context->man, cmd->body.view_id, view_type,
2822 			      &sw_context->staged_cmd_res, &view);
2823 	if (ret || !view)
2824 		return ret;
2825 
2826 	/*
2827 	 * If the view wasn't created during this command batch, it might
2828 	 * have been removed due to a context swapout, so add a
2829 	 * relocation to conditionally make this command a NOP to avoid
2830 	 * device errors.
2831 	 */
2832 	return vmw_resource_relocation_add(sw_context, view,
2833 					   vmw_ptr_diff(sw_context->buf_start,
2834 							&cmd->header.id),
2835 					   vmw_res_rel_cond_nop);
2836 }
2837 
2838 /**
2839  * vmw_cmd_dx_define_shader - Validate SVGA_3D_CMD_DX_DEFINE_SHADER command
2840  *
2841  * @dev_priv: Pointer to a device private struct.
2842  * @sw_context: The software context being used for this batch.
2843  * @header: Pointer to the command header in the command stream.
2844  */
2845 static int vmw_cmd_dx_define_shader(struct vmw_private *dev_priv,
2846 				    struct vmw_sw_context *sw_context,
2847 				    SVGA3dCmdHeader *header)
2848 {
2849 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2850 	struct vmw_resource *res;
2851 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDefineShader) =
2852 		container_of(header, typeof(*cmd), header);
2853 	int ret;
2854 
2855 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2856 		return -EINVAL;
2857 
2858 	if (!ctx_node)
2859 		return -EINVAL;
2860 
2861 	res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXSHADER);
2862 	if (IS_ERR_OR_NULL(res))
2863 		return res ? PTR_ERR(res) : -EINVAL;
2864 	ret = vmw_cotable_notify(res, cmd->body.shaderId);
2865 	if (ret)
2866 		return ret;
2867 
2868 	return vmw_dx_shader_add(sw_context->man, ctx_node->ctx,
2869 				 cmd->body.shaderId, cmd->body.type,
2870 				 &sw_context->staged_cmd_res);
2871 }
2872 
2873 /**
2874  * vmw_cmd_dx_destroy_shader - Validate SVGA_3D_CMD_DX_DESTROY_SHADER command
2875  *
2876  * @dev_priv: Pointer to a device private struct.
2877  * @sw_context: The software context being used for this batch.
2878  * @header: Pointer to the command header in the command stream.
2879  */
2880 static int vmw_cmd_dx_destroy_shader(struct vmw_private *dev_priv,
2881 				     struct vmw_sw_context *sw_context,
2882 				     SVGA3dCmdHeader *header)
2883 {
2884 	struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
2885 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDestroyShader) =
2886 		container_of(header, typeof(*cmd), header);
2887 	int ret;
2888 
2889 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2890 		return -EINVAL;
2891 
2892 	if (!ctx_node)
2893 		return -EINVAL;
2894 
2895 	ret = vmw_shader_remove(sw_context->man, cmd->body.shaderId, 0,
2896 				&sw_context->staged_cmd_res);
2897 
2898 	return ret;
2899 }
2900 
2901 /**
2902  * vmw_cmd_dx_bind_shader - Validate SVGA_3D_CMD_DX_BIND_SHADER command
2903  *
2904  * @dev_priv: Pointer to a device private struct.
2905  * @sw_context: The software context being used for this batch.
2906  * @header: Pointer to the command header in the command stream.
2907  */
2908 static int vmw_cmd_dx_bind_shader(struct vmw_private *dev_priv,
2909 				  struct vmw_sw_context *sw_context,
2910 				  SVGA3dCmdHeader *header)
2911 {
2912 	struct vmw_resource *ctx;
2913 	struct vmw_resource *res;
2914 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindShader) =
2915 		container_of(header, typeof(*cmd), header);
2916 	int ret;
2917 
2918 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2919 		return -EINVAL;
2920 
2921 	if (cmd->body.cid != SVGA3D_INVALID_ID) {
2922 		ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
2923 					VMW_RES_DIRTY_SET,
2924 					user_context_converter, &cmd->body.cid,
2925 					&ctx);
2926 		if (ret)
2927 			return ret;
2928 	} else {
2929 		struct vmw_ctx_validation_info *ctx_node =
2930 			VMW_GET_CTX_NODE(sw_context);
2931 
2932 		if (!ctx_node)
2933 			return -EINVAL;
2934 
2935 		ctx = ctx_node->ctx;
2936 	}
2937 
2938 	res = vmw_shader_lookup(vmw_context_res_man(ctx), cmd->body.shid, 0);
2939 	if (IS_ERR(res)) {
2940 		VMW_DEBUG_USER("Could not find shader to bind.\n");
2941 		return PTR_ERR(res);
2942 	}
2943 
2944 	ret = vmw_execbuf_res_val_add(sw_context, res, VMW_RES_DIRTY_NONE,
2945 				      vmw_val_add_flag_noctx);
2946 	if (ret) {
2947 		VMW_DEBUG_USER("Error creating resource validation node.\n");
2948 		return ret;
2949 	}
2950 
2951 	return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
2952 					 &cmd->body.mobid,
2953 					 cmd->body.offsetInBytes);
2954 }
2955 
2956 /**
2957  * vmw_cmd_dx_genmips - Validate SVGA_3D_CMD_DX_GENMIPS command
2958  *
2959  * @dev_priv: Pointer to a device private struct.
2960  * @sw_context: The software context being used for this batch.
2961  * @header: Pointer to the command header in the command stream.
2962  */
2963 static int vmw_cmd_dx_genmips(struct vmw_private *dev_priv,
2964 			      struct vmw_sw_context *sw_context,
2965 			      SVGA3dCmdHeader *header)
2966 {
2967 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXGenMips) =
2968 		container_of(header, typeof(*cmd), header);
2969 	struct vmw_resource *view;
2970 	struct vmw_res_cache_entry *rcache;
2971 
2972 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
2973 		return -EINVAL;
2974 
2975 	view = vmw_view_id_val_add(sw_context, vmw_view_sr,
2976 				   cmd->body.shaderResourceViewId);
2977 	if (IS_ERR(view))
2978 		return PTR_ERR(view);
2979 
2980 	/*
2981 	 * Normally the shader-resource view is not gpu-dirtying, but for
2982 	 * this particular command it is...
2983 	 * So mark the last looked-up surface, which is the surface
2984 	 * the view points to, gpu-dirty.
2985 	 */
2986 	rcache = &sw_context->res_cache[vmw_res_surface];
2987 	vmw_validation_res_set_dirty(sw_context->ctx, rcache->private,
2988 				     VMW_RES_DIRTY_SET);
2989 	return 0;
2990 }
2991 
2992 /**
2993  * vmw_cmd_dx_transfer_from_buffer - Validate
2994  * SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER command
2995  *
2996  * @dev_priv: Pointer to a device private struct.
2997  * @sw_context: The software context being used for this batch.
2998  * @header: Pointer to the command header in the command stream.
2999  */
3000 static int vmw_cmd_dx_transfer_from_buffer(struct vmw_private *dev_priv,
3001 					   struct vmw_sw_context *sw_context,
3002 					   SVGA3dCmdHeader *header)
3003 {
3004 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXTransferFromBuffer) =
3005 		container_of(header, typeof(*cmd), header);
3006 	int ret;
3007 
3008 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3009 		return -EINVAL;
3010 
3011 	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3012 				VMW_RES_DIRTY_NONE, user_surface_converter,
3013 				&cmd->body.srcSid, NULL);
3014 	if (ret != 0)
3015 		return ret;
3016 
3017 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3018 				 VMW_RES_DIRTY_SET, user_surface_converter,
3019 				 &cmd->body.destSid, NULL);
3020 }
3021 
3022 /**
3023  * vmw_cmd_intra_surface_copy - Validate SVGA_3D_CMD_INTRA_SURFACE_COPY command
3024  *
3025  * @dev_priv: Pointer to a device private struct.
3026  * @sw_context: The software context being used for this batch.
3027  * @header: Pointer to the command header in the command stream.
3028  */
3029 static int vmw_cmd_intra_surface_copy(struct vmw_private *dev_priv,
3030 					   struct vmw_sw_context *sw_context,
3031 					   SVGA3dCmdHeader *header)
3032 {
3033 	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdIntraSurfaceCopy) =
3034 		container_of(header, typeof(*cmd), header);
3035 
3036 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3037 		return -EINVAL;
3038 
3039 	if (!(dev_priv->capabilities2 & SVGA_CAP2_INTRA_SURFACE_COPY))
3040 		return -EINVAL;
3041 
3042 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3043 				 VMW_RES_DIRTY_SET, user_surface_converter,
3044 				 &cmd->body.surface.sid, NULL);
3045 }
3046 
3047 static int vmw_cmd_sm5(struct vmw_private *dev_priv,
3048 		       struct vmw_sw_context *sw_context,
3049 		       SVGA3dCmdHeader *header)
3050 {
3051 	if (!has_sm5_context(dev_priv))
3052 		return -EINVAL;
3053 
3054 	return 0;
3055 }
3056 
3057 static int vmw_cmd_sm5_view_define(struct vmw_private *dev_priv,
3058 				   struct vmw_sw_context *sw_context,
3059 				   SVGA3dCmdHeader *header)
3060 {
3061 	if (!has_sm5_context(dev_priv))
3062 		return -EINVAL;
3063 
3064 	return vmw_cmd_dx_view_define(dev_priv, sw_context, header);
3065 }
3066 
3067 static int vmw_cmd_sm5_view_remove(struct vmw_private *dev_priv,
3068 				   struct vmw_sw_context *sw_context,
3069 				   SVGA3dCmdHeader *header)
3070 {
3071 	if (!has_sm5_context(dev_priv))
3072 		return -EINVAL;
3073 
3074 	return vmw_cmd_dx_view_remove(dev_priv, sw_context, header);
3075 }
3076 
3077 static int vmw_cmd_clear_uav_uint(struct vmw_private *dev_priv,
3078 				  struct vmw_sw_context *sw_context,
3079 				  SVGA3dCmdHeader *header)
3080 {
3081 	struct {
3082 		SVGA3dCmdHeader header;
3083 		SVGA3dCmdDXClearUAViewUint body;
3084 	} *cmd = container_of(header, typeof(*cmd), header);
3085 	struct vmw_resource *ret;
3086 
3087 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3088 		return -EINVAL;
3089 
3090 	if (!has_sm5_context(dev_priv))
3091 		return -EINVAL;
3092 
3093 	ret = vmw_view_id_val_add(sw_context, vmw_view_ua,
3094 				  cmd->body.uaViewId);
3095 
3096 	return PTR_ERR_OR_ZERO(ret);
3097 }
3098 
3099 static int vmw_cmd_clear_uav_float(struct vmw_private *dev_priv,
3100 				   struct vmw_sw_context *sw_context,
3101 				   SVGA3dCmdHeader *header)
3102 {
3103 	struct {
3104 		SVGA3dCmdHeader header;
3105 		SVGA3dCmdDXClearUAViewFloat body;
3106 	} *cmd = container_of(header, typeof(*cmd), header);
3107 	struct vmw_resource *ret;
3108 
3109 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3110 		return -EINVAL;
3111 
3112 	if (!has_sm5_context(dev_priv))
3113 		return -EINVAL;
3114 
3115 	ret = vmw_view_id_val_add(sw_context, vmw_view_ua,
3116 				  cmd->body.uaViewId);
3117 
3118 	return PTR_ERR_OR_ZERO(ret);
3119 }
3120 
3121 static int vmw_cmd_set_uav(struct vmw_private *dev_priv,
3122 			   struct vmw_sw_context *sw_context,
3123 			   SVGA3dCmdHeader *header)
3124 {
3125 	struct {
3126 		SVGA3dCmdHeader header;
3127 		SVGA3dCmdDXSetUAViews body;
3128 	} *cmd = container_of(header, typeof(*cmd), header);
3129 	u32 num_uav = (cmd->header.size - sizeof(cmd->body)) /
3130 		sizeof(SVGA3dUAViewId);
3131 	int ret;
3132 
3133 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3134 		return -EINVAL;
3135 
3136 	if (!has_sm5_context(dev_priv))
3137 		return -EINVAL;
3138 
3139 	if (num_uav > vmw_max_num_uavs(dev_priv)) {
3140 		VMW_DEBUG_USER("Invalid UAV binding.\n");
3141 		return -EINVAL;
3142 	}
3143 
3144 	ret = vmw_view_bindings_add(sw_context, vmw_view_ua,
3145 				    vmw_ctx_binding_uav, 0, (void *)&cmd[1],
3146 				    num_uav, 0);
3147 	if (ret)
3148 		return ret;
3149 
3150 	vmw_binding_add_uav_index(sw_context->dx_ctx_node->staged, 0,
3151 					 cmd->body.uavSpliceIndex);
3152 
3153 	return ret;
3154 }
3155 
3156 static int vmw_cmd_set_cs_uav(struct vmw_private *dev_priv,
3157 			      struct vmw_sw_context *sw_context,
3158 			      SVGA3dCmdHeader *header)
3159 {
3160 	struct {
3161 		SVGA3dCmdHeader header;
3162 		SVGA3dCmdDXSetCSUAViews body;
3163 	} *cmd = container_of(header, typeof(*cmd), header);
3164 	u32 num_uav = (cmd->header.size - sizeof(cmd->body)) /
3165 		sizeof(SVGA3dUAViewId);
3166 	int ret;
3167 
3168 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3169 		return -EINVAL;
3170 
3171 	if (!has_sm5_context(dev_priv))
3172 		return -EINVAL;
3173 
3174 	if (num_uav > vmw_max_num_uavs(dev_priv)) {
3175 		VMW_DEBUG_USER("Invalid UAV binding.\n");
3176 		return -EINVAL;
3177 	}
3178 
3179 	ret = vmw_view_bindings_add(sw_context, vmw_view_ua,
3180 				    vmw_ctx_binding_cs_uav, 0, (void *)&cmd[1],
3181 				    num_uav, 0);
3182 	if (ret)
3183 		return ret;
3184 
3185 	vmw_binding_add_uav_index(sw_context->dx_ctx_node->staged, 1,
3186 				  cmd->body.startIndex);
3187 
3188 	return ret;
3189 }
3190 
3191 static int vmw_cmd_dx_define_streamoutput(struct vmw_private *dev_priv,
3192 					  struct vmw_sw_context *sw_context,
3193 					  SVGA3dCmdHeader *header)
3194 {
3195 	struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
3196 	struct vmw_resource *res;
3197 	struct {
3198 		SVGA3dCmdHeader header;
3199 		SVGA3dCmdDXDefineStreamOutputWithMob body;
3200 	} *cmd = container_of(header, typeof(*cmd), header);
3201 	int ret;
3202 
3203 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3204 		return -EINVAL;
3205 
3206 	if (!has_sm5_context(dev_priv))
3207 		return -EINVAL;
3208 
3209 	if (!ctx_node) {
3210 		DRM_ERROR("DX Context not set.\n");
3211 		return -EINVAL;
3212 	}
3213 
3214 	res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_STREAMOUTPUT);
3215 	if (IS_ERR_OR_NULL(res))
3216 		return res ? PTR_ERR(res) : -EINVAL;
3217 	ret = vmw_cotable_notify(res, cmd->body.soid);
3218 	if (ret)
3219 		return ret;
3220 
3221 	return vmw_dx_streamoutput_add(sw_context->man, ctx_node->ctx,
3222 				       cmd->body.soid,
3223 				       &sw_context->staged_cmd_res);
3224 }
3225 
3226 static int vmw_cmd_dx_destroy_streamoutput(struct vmw_private *dev_priv,
3227 					   struct vmw_sw_context *sw_context,
3228 					   SVGA3dCmdHeader *header)
3229 {
3230 	struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
3231 	struct vmw_resource *res;
3232 	struct {
3233 		SVGA3dCmdHeader header;
3234 		SVGA3dCmdDXDestroyStreamOutput body;
3235 	} *cmd = container_of(header, typeof(*cmd), header);
3236 
3237 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3238 		return -EINVAL;
3239 
3240 	if (!ctx_node) {
3241 		DRM_ERROR("DX Context not set.\n");
3242 		return -EINVAL;
3243 	}
3244 
3245 	/*
3246 	 * When device does not support SM5 then streamoutput with mob command is
3247 	 * not available to user-space. Simply return in this case.
3248 	 */
3249 	if (!has_sm5_context(dev_priv))
3250 		return 0;
3251 
3252 	/*
3253 	 * With SM5 capable device if lookup fails then user-space probably used
3254 	 * old streamoutput define command. Return without an error.
3255 	 */
3256 	res = vmw_dx_streamoutput_lookup(vmw_context_res_man(ctx_node->ctx),
3257 					 cmd->body.soid);
3258 	if (IS_ERR(res))
3259 		return 0;
3260 
3261 	return vmw_dx_streamoutput_remove(sw_context->man, cmd->body.soid,
3262 					  &sw_context->staged_cmd_res);
3263 }
3264 
3265 static int vmw_cmd_dx_bind_streamoutput(struct vmw_private *dev_priv,
3266 					struct vmw_sw_context *sw_context,
3267 					SVGA3dCmdHeader *header)
3268 {
3269 	struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
3270 	struct vmw_resource *res;
3271 	struct {
3272 		SVGA3dCmdHeader header;
3273 		SVGA3dCmdDXBindStreamOutput body;
3274 	} *cmd = container_of(header, typeof(*cmd), header);
3275 	int ret;
3276 
3277 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3278 		return -EINVAL;
3279 
3280 	if (!has_sm5_context(dev_priv))
3281 		return -EINVAL;
3282 
3283 	if (!ctx_node) {
3284 		DRM_ERROR("DX Context not set.\n");
3285 		return -EINVAL;
3286 	}
3287 
3288 	res = vmw_dx_streamoutput_lookup(vmw_context_res_man(ctx_node->ctx),
3289 					 cmd->body.soid);
3290 	if (IS_ERR(res)) {
3291 		DRM_ERROR("Could not find streamoutput to bind.\n");
3292 		return PTR_ERR(res);
3293 	}
3294 
3295 	vmw_dx_streamoutput_set_size(res, cmd->body.sizeInBytes);
3296 
3297 	ret = vmw_execbuf_res_val_add(sw_context, res, VMW_RES_DIRTY_NONE,
3298 				      vmw_val_add_flag_noctx);
3299 	if (ret) {
3300 		DRM_ERROR("Error creating resource validation node.\n");
3301 		return ret;
3302 	}
3303 
3304 	return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
3305 					 &cmd->body.mobid,
3306 					 cmd->body.offsetInBytes);
3307 }
3308 
3309 static int vmw_cmd_dx_set_streamoutput(struct vmw_private *dev_priv,
3310 				       struct vmw_sw_context *sw_context,
3311 				       SVGA3dCmdHeader *header)
3312 {
3313 	struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
3314 	struct vmw_resource *res;
3315 	struct vmw_ctx_bindinfo_so binding;
3316 	struct {
3317 		SVGA3dCmdHeader header;
3318 		SVGA3dCmdDXSetStreamOutput body;
3319 	} *cmd = container_of(header, typeof(*cmd), header);
3320 	int ret;
3321 
3322 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3323 		return -EINVAL;
3324 
3325 	if (!ctx_node) {
3326 		DRM_ERROR("DX Context not set.\n");
3327 		return -EINVAL;
3328 	}
3329 
3330 	if (cmd->body.soid == SVGA3D_INVALID_ID)
3331 		return 0;
3332 
3333 	/*
3334 	 * When device does not support SM5 then streamoutput with mob command is
3335 	 * not available to user-space. Simply return in this case.
3336 	 */
3337 	if (!has_sm5_context(dev_priv))
3338 		return 0;
3339 
3340 	/*
3341 	 * With SM5 capable device if lookup fails then user-space probably used
3342 	 * old streamoutput define command. Return without an error.
3343 	 */
3344 	res = vmw_dx_streamoutput_lookup(vmw_context_res_man(ctx_node->ctx),
3345 					 cmd->body.soid);
3346 	if (IS_ERR(res)) {
3347 		return 0;
3348 	}
3349 
3350 	ret = vmw_execbuf_res_val_add(sw_context, res, VMW_RES_DIRTY_NONE,
3351 				      vmw_val_add_flag_noctx);
3352 	if (ret) {
3353 		DRM_ERROR("Error creating resource validation node.\n");
3354 		return ret;
3355 	}
3356 
3357 	binding.bi.ctx = ctx_node->ctx;
3358 	binding.bi.res = res;
3359 	binding.bi.bt = vmw_ctx_binding_so;
3360 	binding.slot = 0; /* Only one SO set to context at a time. */
3361 
3362 	vmw_binding_add(sw_context->dx_ctx_node->staged, &binding.bi, 0,
3363 			binding.slot);
3364 
3365 	return ret;
3366 }
3367 
3368 static int vmw_cmd_indexed_instanced_indirect(struct vmw_private *dev_priv,
3369 					      struct vmw_sw_context *sw_context,
3370 					      SVGA3dCmdHeader *header)
3371 {
3372 	struct vmw_draw_indexed_instanced_indirect_cmd {
3373 		SVGA3dCmdHeader header;
3374 		SVGA3dCmdDXDrawIndexedInstancedIndirect body;
3375 	} *cmd = container_of(header, typeof(*cmd), header);
3376 
3377 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3378 		return -EINVAL;
3379 
3380 	if (!has_sm5_context(dev_priv))
3381 		return -EINVAL;
3382 
3383 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3384 				 VMW_RES_DIRTY_NONE, user_surface_converter,
3385 				 &cmd->body.argsBufferSid, NULL);
3386 }
3387 
3388 static int vmw_cmd_instanced_indirect(struct vmw_private *dev_priv,
3389 				      struct vmw_sw_context *sw_context,
3390 				      SVGA3dCmdHeader *header)
3391 {
3392 	struct vmw_draw_instanced_indirect_cmd {
3393 		SVGA3dCmdHeader header;
3394 		SVGA3dCmdDXDrawInstancedIndirect body;
3395 	} *cmd = container_of(header, typeof(*cmd), header);
3396 
3397 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3398 		return -EINVAL;
3399 
3400 	if (!has_sm5_context(dev_priv))
3401 		return -EINVAL;
3402 
3403 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3404 				 VMW_RES_DIRTY_NONE, user_surface_converter,
3405 				 &cmd->body.argsBufferSid, NULL);
3406 }
3407 
3408 static int vmw_cmd_dispatch_indirect(struct vmw_private *dev_priv,
3409 				     struct vmw_sw_context *sw_context,
3410 				     SVGA3dCmdHeader *header)
3411 {
3412 	struct vmw_dispatch_indirect_cmd {
3413 		SVGA3dCmdHeader header;
3414 		SVGA3dCmdDXDispatchIndirect body;
3415 	} *cmd = container_of(header, typeof(*cmd), header);
3416 
3417 	if (!vmw_cmd_body_fits(header, sizeof(cmd->body)))
3418 		return -EINVAL;
3419 
3420 	if (!has_sm5_context(dev_priv))
3421 		return -EINVAL;
3422 
3423 	return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3424 				 VMW_RES_DIRTY_NONE, user_surface_converter,
3425 				 &cmd->body.argsBufferSid, NULL);
3426 }
3427 
3428 static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
3429 				struct vmw_sw_context *sw_context,
3430 				void *buf, uint32_t *size)
3431 {
3432 	uint32_t size_remaining = *size;
3433 	uint32_t cmd_id;
3434 
3435 	cmd_id = ((uint32_t *)buf)[0];
3436 	switch (cmd_id) {
3437 	case SVGA_CMD_UPDATE:
3438 		*size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
3439 		break;
3440 	case SVGA_CMD_DEFINE_GMRFB:
3441 		*size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
3442 		break;
3443 	case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3444 		*size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3445 		break;
3446 	case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3447 		*size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3448 		break;
3449 	default:
3450 		VMW_DEBUG_USER("Unsupported SVGA command: %u.\n", cmd_id);
3451 		return -EINVAL;
3452 	}
3453 
3454 	if (*size > size_remaining) {
3455 		VMW_DEBUG_USER("Invalid SVGA command (size mismatch): %u.\n",
3456 			       cmd_id);
3457 		return -EINVAL;
3458 	}
3459 
3460 	if (unlikely(!sw_context->kernel)) {
3461 		VMW_DEBUG_USER("Kernel only SVGA command: %u.\n", cmd_id);
3462 		return -EPERM;
3463 	}
3464 
3465 	if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
3466 		return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
3467 
3468 	return 0;
3469 }
3470 
3471 static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
3472 	VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
3473 		    false, false, false),
3474 	VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
3475 		    false, false, false),
3476 	VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
3477 		    true, false, false),
3478 	VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
3479 		    true, false, false),
3480 	VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
3481 		    true, false, false),
3482 	VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
3483 		    false, false, false),
3484 	VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
3485 		    false, false, false),
3486 	VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
3487 		    true, false, false),
3488 	VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
3489 		    true, false, false),
3490 	VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
3491 		    true, false, false),
3492 	VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
3493 		    &vmw_cmd_set_render_target_check, true, false, false),
3494 	VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
3495 		    true, false, false),
3496 	VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
3497 		    true, false, false),
3498 	VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
3499 		    true, false, false),
3500 	VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
3501 		    true, false, false),
3502 	VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
3503 		    true, false, false),
3504 	VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
3505 		    true, false, false),
3506 	VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
3507 		    true, false, false),
3508 	VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
3509 		    false, false, false),
3510 	VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_shader_define,
3511 		    true, false, false),
3512 	VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_shader_destroy,
3513 		    true, false, false),
3514 	VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
3515 		    true, false, false),
3516 	VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_set_shader_const,
3517 		    true, false, false),
3518 	VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
3519 		    true, false, false),
3520 	VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
3521 		    true, false, false),
3522 	VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
3523 		    true, false, false),
3524 	VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
3525 		    true, false, false),
3526 	VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
3527 		    true, false, false),
3528 	VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
3529 		    true, false, false),
3530 	VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
3531 		    &vmw_cmd_blt_surf_screen_check, false, false, false),
3532 	VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
3533 		    false, false, false),
3534 	VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
3535 		    false, false, false),
3536 	VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
3537 		    false, false, false),
3538 	VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
3539 		    false, false, false),
3540 	VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
3541 		    false, false, false),
3542 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD1, &vmw_cmd_invalid,
3543 		    false, false, false),
3544 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD2, &vmw_cmd_invalid,
3545 		    false, false, false),
3546 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD12, &vmw_cmd_invalid, false, false, false),
3547 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD13, &vmw_cmd_invalid, false, false, false),
3548 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD14, &vmw_cmd_invalid, false, false, false),
3549 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD15, &vmw_cmd_invalid, false, false, false),
3550 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD16, &vmw_cmd_invalid, false, false, false),
3551 	VMW_CMD_DEF(SVGA_3D_CMD_DEAD17, &vmw_cmd_invalid, false, false, false),
3552 	VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
3553 		    false, false, true),
3554 	VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
3555 		    false, false, true),
3556 	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
3557 		    false, false, true),
3558 	VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
3559 		    false, false, true),
3560 	VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB64, &vmw_cmd_invalid,
3561 		    false, false, true),
3562 	VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
3563 		    false, false, true),
3564 	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
3565 		    false, false, true),
3566 	VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
3567 		    false, false, true),
3568 	VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
3569 		    true, false, true),
3570 	VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
3571 		    false, false, true),
3572 	VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
3573 		    true, false, true),
3574 	VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
3575 		    &vmw_cmd_update_gb_surface, true, false, true),
3576 	VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
3577 		    &vmw_cmd_readback_gb_image, true, false, true),
3578 	VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
3579 		    &vmw_cmd_readback_gb_surface, true, false, true),
3580 	VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
3581 		    &vmw_cmd_invalidate_gb_image, true, false, true),
3582 	VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
3583 		    &vmw_cmd_invalidate_gb_surface, true, false, true),
3584 	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
3585 		    false, false, true),
3586 	VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
3587 		    false, false, true),
3588 	VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
3589 		    false, false, true),
3590 	VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
3591 		    false, false, true),
3592 	VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
3593 		    false, false, true),
3594 	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
3595 		    false, false, true),
3596 	VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
3597 		    true, false, true),
3598 	VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
3599 		    false, false, true),
3600 	VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
3601 		    false, false, false),
3602 	VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
3603 		    true, false, true),
3604 	VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
3605 		    true, false, true),
3606 	VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
3607 		    true, false, true),
3608 	VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
3609 		    true, false, true),
3610 	VMW_CMD_DEF(SVGA_3D_CMD_NOP_ERROR, &vmw_cmd_ok,
3611 		    true, false, true),
3612 	VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
3613 		    false, false, true),
3614 	VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
3615 		    false, false, true),
3616 	VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
3617 		    false, false, true),
3618 	VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
3619 		    false, false, true),
3620 	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
3621 		    false, false, true),
3622 	VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
3623 		    false, false, true),
3624 	VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
3625 		    false, false, true),
3626 	VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
3627 		    false, false, true),
3628 	VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3629 		    false, false, true),
3630 	VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3631 		    false, false, true),
3632 	VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
3633 		    true, false, true),
3634 	VMW_CMD_DEF(SVGA_3D_CMD_GB_SCREEN_DMA, &vmw_cmd_invalid,
3635 		    false, false, true),
3636 	VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE_WITH_PITCH, &vmw_cmd_invalid,
3637 		    false, false, true),
3638 	VMW_CMD_DEF(SVGA_3D_CMD_GB_MOB_FENCE, &vmw_cmd_invalid,
3639 		    false, false, true),
3640 	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V2, &vmw_cmd_invalid,
3641 		    false, false, true),
3642 
3643 	/* SM commands */
3644 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_CONTEXT, &vmw_cmd_invalid,
3645 		    false, false, true),
3646 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_CONTEXT, &vmw_cmd_invalid,
3647 		    false, false, true),
3648 	VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_CONTEXT, &vmw_cmd_invalid,
3649 		    false, false, true),
3650 	VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_CONTEXT, &vmw_cmd_invalid,
3651 		    false, false, true),
3652 	VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_CONTEXT, &vmw_cmd_invalid,
3653 		    false, false, true),
3654 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER,
3655 		    &vmw_cmd_dx_set_single_constant_buffer, true, false, true),
3656 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER_RESOURCES,
3657 		    &vmw_cmd_dx_set_shader_res, true, false, true),
3658 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER, &vmw_cmd_dx_set_shader,
3659 		    true, false, true),
3660 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SAMPLERS, &vmw_cmd_dx_cid_check,
3661 		    true, false, true),
3662 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW, &vmw_cmd_dx_cid_check,
3663 		    true, false, true),
3664 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED, &vmw_cmd_dx_cid_check,
3665 		    true, false, true),
3666 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INSTANCED, &vmw_cmd_dx_cid_check,
3667 		    true, false, true),
3668 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED_INSTANCED,
3669 		    &vmw_cmd_dx_cid_check, true, false, true),
3670 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_AUTO, &vmw_cmd_dx_cid_check,
3671 		    true, false, true),
3672 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS,
3673 		    &vmw_cmd_dx_set_vertex_buffers, true, false, true),
3674 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INDEX_BUFFER,
3675 		    &vmw_cmd_dx_set_index_buffer, true, false, true),
3676 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RENDERTARGETS,
3677 		    &vmw_cmd_dx_set_rendertargets, true, false, true),
3678 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_BLEND_STATE, &vmw_cmd_dx_cid_check,
3679 		    true, false, true),
3680 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_DEPTHSTENCIL_STATE,
3681 		    &vmw_cmd_dx_cid_check, true, false, true),
3682 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RASTERIZER_STATE,
3683 		    &vmw_cmd_dx_cid_check, true, false, true),
3684 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_QUERY, &vmw_cmd_dx_define_query,
3685 		    true, false, true),
3686 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_QUERY, &vmw_cmd_dx_cid_check,
3687 		    true, false, true),
3688 	VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_QUERY, &vmw_cmd_dx_bind_query,
3689 		    true, false, true),
3690 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_QUERY_OFFSET,
3691 		    &vmw_cmd_dx_cid_check, true, false, true),
3692 	VMW_CMD_DEF(SVGA_3D_CMD_DX_BEGIN_QUERY, &vmw_cmd_dx_cid_check,
3693 		    true, false, true),
3694 	VMW_CMD_DEF(SVGA_3D_CMD_DX_END_QUERY, &vmw_cmd_dx_cid_check,
3695 		    true, false, true),
3696 	VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_QUERY, &vmw_cmd_invalid,
3697 		    true, false, true),
3698 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_PREDICATION, &vmw_cmd_dx_cid_check,
3699 		    true, false, true),
3700 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VIEWPORTS, &vmw_cmd_dx_cid_check,
3701 		    true, false, true),
3702 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SCISSORRECTS, &vmw_cmd_dx_cid_check,
3703 		    true, false, true),
3704 	VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW,
3705 		    &vmw_cmd_dx_clear_rendertarget_view, true, false, true),
3706 	VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW,
3707 		    &vmw_cmd_dx_clear_depthstencil_view, true, false, true),
3708 	VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY, &vmw_cmd_invalid,
3709 		    true, false, true),
3710 	VMW_CMD_DEF(SVGA_3D_CMD_DX_GENMIPS, &vmw_cmd_dx_genmips,
3711 		    true, false, true),
3712 	VMW_CMD_DEF(SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE,
3713 		    &vmw_cmd_dx_check_subresource, true, false, true),
3714 	VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_SUBRESOURCE,
3715 		    &vmw_cmd_dx_check_subresource, true, false, true),
3716 	VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_SUBRESOURCE,
3717 		    &vmw_cmd_dx_check_subresource, true, false, true),
3718 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW,
3719 		    &vmw_cmd_dx_view_define, true, false, true),
3720 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
3721 		    &vmw_cmd_dx_view_remove, true, false, true),
3722 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW,
3723 		    &vmw_cmd_dx_view_define, true, false, true),
3724 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
3725 		    &vmw_cmd_dx_view_remove, true, false, true),
3726 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW,
3727 		    &vmw_cmd_dx_view_define, true, false, true),
3728 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
3729 		    &vmw_cmd_dx_view_remove, true, false, true),
3730 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_ELEMENTLAYOUT,
3731 		    &vmw_cmd_dx_so_define, true, false, true),
3732 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_ELEMENTLAYOUT,
3733 		    &vmw_cmd_dx_cid_check, true, false, true),
3734 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_BLEND_STATE,
3735 		    &vmw_cmd_dx_so_define, true, false, true),
3736 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_BLEND_STATE,
3737 		    &vmw_cmd_dx_cid_check, true, false, true),
3738 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_STATE,
3739 		    &vmw_cmd_dx_so_define, true, false, true),
3740 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_STATE,
3741 		    &vmw_cmd_dx_cid_check, true, false, true),
3742 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RASTERIZER_STATE,
3743 		    &vmw_cmd_dx_so_define, true, false, true),
3744 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RASTERIZER_STATE,
3745 		    &vmw_cmd_dx_cid_check, true, false, true),
3746 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SAMPLER_STATE,
3747 		    &vmw_cmd_dx_so_define, true, false, true),
3748 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SAMPLER_STATE,
3749 		    &vmw_cmd_dx_cid_check, true, false, true),
3750 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADER,
3751 		    &vmw_cmd_dx_define_shader, true, false, true),
3752 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADER,
3753 		    &vmw_cmd_dx_destroy_shader, true, false, true),
3754 	VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_SHADER,
3755 		    &vmw_cmd_dx_bind_shader, true, false, true),
3756 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_STREAMOUTPUT,
3757 		    &vmw_cmd_dx_so_define, true, false, true),
3758 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_STREAMOUTPUT,
3759 		    &vmw_cmd_dx_destroy_streamoutput, true, false, true),
3760 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_STREAMOUTPUT,
3761 		    &vmw_cmd_dx_set_streamoutput, true, false, true),
3762 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SOTARGETS,
3763 		    &vmw_cmd_dx_set_so_targets, true, false, true),
3764 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INPUT_LAYOUT,
3765 		    &vmw_cmd_dx_cid_check, true, false, true),
3766 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_TOPOLOGY,
3767 		    &vmw_cmd_dx_cid_check, true, false, true),
3768 	VMW_CMD_DEF(SVGA_3D_CMD_DX_BUFFER_COPY,
3769 		    &vmw_cmd_buffer_copy_check, true, false, true),
3770 	VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY_REGION,
3771 		    &vmw_cmd_pred_copy_check, true, false, true),
3772 	VMW_CMD_DEF(SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER,
3773 		    &vmw_cmd_dx_transfer_from_buffer,
3774 		    true, false, true),
3775 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VS_CONSTANT_BUFFER_OFFSET,
3776 		    &vmw_cmd_dx_set_constant_buffer_offset,
3777 		    true, false, true),
3778 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_PS_CONSTANT_BUFFER_OFFSET,
3779 		    &vmw_cmd_dx_set_constant_buffer_offset,
3780 		    true, false, true),
3781 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_GS_CONSTANT_BUFFER_OFFSET,
3782 		    &vmw_cmd_dx_set_constant_buffer_offset,
3783 		    true, false, true),
3784 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_HS_CONSTANT_BUFFER_OFFSET,
3785 		    &vmw_cmd_dx_set_constant_buffer_offset,
3786 		    true, false, true),
3787 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_DS_CONSTANT_BUFFER_OFFSET,
3788 		    &vmw_cmd_dx_set_constant_buffer_offset,
3789 		    true, false, true),
3790 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_CS_CONSTANT_BUFFER_OFFSET,
3791 		    &vmw_cmd_dx_set_constant_buffer_offset,
3792 		    true, false, true),
3793 	VMW_CMD_DEF(SVGA_3D_CMD_INTRA_SURFACE_COPY, &vmw_cmd_intra_surface_copy,
3794 		    true, false, true),
3795 
3796 	/*
3797 	 * SM5 commands
3798 	 */
3799 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_UA_VIEW, &vmw_cmd_sm5_view_define,
3800 		    true, false, true),
3801 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_UA_VIEW, &vmw_cmd_sm5_view_remove,
3802 		    true, false, true),
3803 	VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_UA_VIEW_UINT, &vmw_cmd_clear_uav_uint,
3804 		    true, false, true),
3805 	VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_UA_VIEW_FLOAT,
3806 		    &vmw_cmd_clear_uav_float, true, false, true),
3807 	VMW_CMD_DEF(SVGA_3D_CMD_DX_COPY_STRUCTURE_COUNT, &vmw_cmd_invalid, true,
3808 		    false, true),
3809 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_UA_VIEWS, &vmw_cmd_set_uav, true, false,
3810 		    true),
3811 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED_INSTANCED_INDIRECT,
3812 		    &vmw_cmd_indexed_instanced_indirect, true, false, true),
3813 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INSTANCED_INDIRECT,
3814 		    &vmw_cmd_instanced_indirect, true, false, true),
3815 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DISPATCH, &vmw_cmd_sm5, true, false, true),
3816 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DISPATCH_INDIRECT,
3817 		    &vmw_cmd_dispatch_indirect, true, false, true),
3818 	VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_CS_UA_VIEWS, &vmw_cmd_set_cs_uav, true,
3819 		    false, true),
3820 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW_V2,
3821 		    &vmw_cmd_sm5_view_define, true, false, true),
3822 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_STREAMOUTPUT_WITH_MOB,
3823 		    &vmw_cmd_dx_define_streamoutput, true, false, true),
3824 	VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_STREAMOUTPUT,
3825 		    &vmw_cmd_dx_bind_streamoutput, true, false, true),
3826 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RASTERIZER_STATE_V2,
3827 		    &vmw_cmd_dx_so_define, true, false, true),
3828 	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V4,
3829 		    &vmw_cmd_invalid, false, false, true),
3830 };
3831 
3832 bool vmw_cmd_describe(const void *buf, u32 *size, char const **cmd)
3833 {
3834 	u32 cmd_id = ((u32 *) buf)[0];
3835 
3836 	if (cmd_id >= SVGA_CMD_MAX) {
3837 		SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3838 		const struct vmw_cmd_entry *entry;
3839 
3840 		*size = header->size + sizeof(SVGA3dCmdHeader);
3841 		cmd_id = header->id;
3842 		if (cmd_id >= SVGA_3D_CMD_MAX)
3843 			return false;
3844 
3845 		cmd_id -= SVGA_3D_CMD_BASE;
3846 		entry = &vmw_cmd_entries[cmd_id];
3847 		*cmd = entry->cmd_name;
3848 		return true;
3849 	}
3850 
3851 	switch (cmd_id) {
3852 	case SVGA_CMD_UPDATE:
3853 		*cmd = "SVGA_CMD_UPDATE";
3854 		*size = sizeof(u32) + sizeof(SVGAFifoCmdUpdate);
3855 		break;
3856 	case SVGA_CMD_DEFINE_GMRFB:
3857 		*cmd = "SVGA_CMD_DEFINE_GMRFB";
3858 		*size = sizeof(u32) + sizeof(SVGAFifoCmdDefineGMRFB);
3859 		break;
3860 	case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3861 		*cmd = "SVGA_CMD_BLIT_GMRFB_TO_SCREEN";
3862 		*size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3863 		break;
3864 	case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3865 		*cmd = "SVGA_CMD_BLIT_SCREEN_TO_GMRFB";
3866 		*size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3867 		break;
3868 	default:
3869 		*cmd = "UNKNOWN";
3870 		*size = 0;
3871 		return false;
3872 	}
3873 
3874 	return true;
3875 }
3876 
3877 static int vmw_cmd_check(struct vmw_private *dev_priv,
3878 			 struct vmw_sw_context *sw_context, void *buf,
3879 			 uint32_t *size)
3880 {
3881 	uint32_t cmd_id;
3882 	uint32_t size_remaining = *size;
3883 	SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3884 	int ret;
3885 	const struct vmw_cmd_entry *entry;
3886 	bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
3887 
3888 	cmd_id = ((uint32_t *)buf)[0];
3889 	/* Handle any none 3D commands */
3890 	if (unlikely(cmd_id < SVGA_CMD_MAX))
3891 		return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
3892 
3893 
3894 	cmd_id = header->id;
3895 	if (header->size > SVGA_CMD_MAX_DATASIZE) {
3896 		VMW_DEBUG_USER("SVGA3D command: %d is too big.\n",
3897 			       cmd_id + SVGA_3D_CMD_BASE);
3898 		return -E2BIG;
3899 	}
3900 	*size = header->size + sizeof(SVGA3dCmdHeader);
3901 
3902 	cmd_id -= SVGA_3D_CMD_BASE;
3903 	if (unlikely(*size > size_remaining))
3904 		goto out_invalid;
3905 
3906 	if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
3907 		goto out_invalid;
3908 
3909 	entry = &vmw_cmd_entries[cmd_id];
3910 	if (unlikely(!entry->func))
3911 		goto out_invalid;
3912 
3913 	if (unlikely(!entry->user_allow && !sw_context->kernel))
3914 		goto out_privileged;
3915 
3916 	if (unlikely(entry->gb_disable && gb))
3917 		goto out_old;
3918 
3919 	if (unlikely(entry->gb_enable && !gb))
3920 		goto out_new;
3921 
3922 	ret = entry->func(dev_priv, sw_context, header);
3923 	if (unlikely(ret != 0)) {
3924 		VMW_DEBUG_USER("SVGA3D command: %d failed with error %d\n",
3925 			       cmd_id + SVGA_3D_CMD_BASE, ret);
3926 		return ret;
3927 	}
3928 
3929 	return 0;
3930 out_invalid:
3931 	VMW_DEBUG_USER("Invalid SVGA3D command: %d\n",
3932 		       cmd_id + SVGA_3D_CMD_BASE);
3933 	return -EINVAL;
3934 out_privileged:
3935 	VMW_DEBUG_USER("Privileged SVGA3D command: %d\n",
3936 		       cmd_id + SVGA_3D_CMD_BASE);
3937 	return -EPERM;
3938 out_old:
3939 	VMW_DEBUG_USER("Deprecated (disallowed) SVGA3D command: %d\n",
3940 		       cmd_id + SVGA_3D_CMD_BASE);
3941 	return -EINVAL;
3942 out_new:
3943 	VMW_DEBUG_USER("SVGA3D command: %d not supported by virtual device.\n",
3944 		       cmd_id + SVGA_3D_CMD_BASE);
3945 	return -EINVAL;
3946 }
3947 
3948 static int vmw_cmd_check_all(struct vmw_private *dev_priv,
3949 			     struct vmw_sw_context *sw_context, void *buf,
3950 			     uint32_t size)
3951 {
3952 	int32_t cur_size = size;
3953 	int ret;
3954 
3955 	sw_context->buf_start = buf;
3956 
3957 	while (cur_size > 0) {
3958 		size = cur_size;
3959 		ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
3960 		if (unlikely(ret != 0))
3961 			return ret;
3962 		buf = (void *)((unsigned long) buf + size);
3963 		cur_size -= size;
3964 	}
3965 
3966 	if (unlikely(cur_size != 0)) {
3967 		VMW_DEBUG_USER("Command verifier out of sync.\n");
3968 		return -EINVAL;
3969 	}
3970 
3971 	return 0;
3972 }
3973 
3974 static void vmw_free_relocations(struct vmw_sw_context *sw_context)
3975 {
3976 	/* Memory is validation context memory, so no need to free it */
3977 	INIT_LIST_HEAD(&sw_context->bo_relocations);
3978 }
3979 
3980 static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
3981 {
3982 	struct vmw_relocation *reloc;
3983 	struct ttm_buffer_object *bo;
3984 
3985 	list_for_each_entry(reloc, &sw_context->bo_relocations, head) {
3986 		bo = &reloc->vbo->tbo;
3987 		switch (bo->resource->mem_type) {
3988 		case TTM_PL_VRAM:
3989 			reloc->location->offset += bo->resource->start << PAGE_SHIFT;
3990 			reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
3991 			break;
3992 		case VMW_PL_GMR:
3993 			reloc->location->gmrId = bo->resource->start;
3994 			break;
3995 		case VMW_PL_MOB:
3996 			*reloc->mob_loc = bo->resource->start;
3997 			break;
3998 		default:
3999 			BUG();
4000 		}
4001 	}
4002 	vmw_free_relocations(sw_context);
4003 }
4004 
4005 static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
4006 				 uint32_t size)
4007 {
4008 	if (likely(sw_context->cmd_bounce_size >= size))
4009 		return 0;
4010 
4011 	if (sw_context->cmd_bounce_size == 0)
4012 		sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
4013 
4014 	while (sw_context->cmd_bounce_size < size) {
4015 		sw_context->cmd_bounce_size =
4016 			PAGE_ALIGN(sw_context->cmd_bounce_size +
4017 				   (sw_context->cmd_bounce_size >> 1));
4018 	}
4019 
4020 	vfree(sw_context->cmd_bounce);
4021 	sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
4022 
4023 	if (sw_context->cmd_bounce == NULL) {
4024 		VMW_DEBUG_USER("Failed to allocate command bounce buffer.\n");
4025 		sw_context->cmd_bounce_size = 0;
4026 		return -ENOMEM;
4027 	}
4028 
4029 	return 0;
4030 }
4031 
4032 /*
4033  * vmw_execbuf_fence_commands - create and submit a command stream fence
4034  *
4035  * Creates a fence object and submits a command stream marker.
4036  * If this fails for some reason, We sync the fifo and return NULL.
4037  * It is then safe to fence buffers with a NULL pointer.
4038  *
4039  * If @p_handle is not NULL @file_priv must also not be NULL. Creates a
4040  * userspace handle if @p_handle is not NULL, otherwise not.
4041  */
4042 
4043 int vmw_execbuf_fence_commands(struct drm_file *file_priv,
4044 			       struct vmw_private *dev_priv,
4045 			       struct vmw_fence_obj **p_fence,
4046 			       uint32_t *p_handle)
4047 {
4048 	uint32_t sequence;
4049 	int ret;
4050 	bool synced = false;
4051 
4052 	/* p_handle implies file_priv. */
4053 	BUG_ON(p_handle != NULL && file_priv == NULL);
4054 
4055 	ret = vmw_cmd_send_fence(dev_priv, &sequence);
4056 	if (unlikely(ret != 0)) {
4057 		VMW_DEBUG_USER("Fence submission error. Syncing.\n");
4058 		synced = true;
4059 	}
4060 
4061 	if (p_handle != NULL)
4062 		ret = vmw_user_fence_create(file_priv, dev_priv->fman,
4063 					    sequence, p_fence, p_handle);
4064 	else
4065 		ret = vmw_fence_create(dev_priv->fman, sequence, p_fence);
4066 
4067 	if (unlikely(ret != 0 && !synced)) {
4068 		(void) vmw_fallback_wait(dev_priv, false, false, sequence,
4069 					 false, VMW_FENCE_WAIT_TIMEOUT);
4070 		*p_fence = NULL;
4071 	}
4072 
4073 	return ret;
4074 }
4075 
4076 /**
4077  * vmw_execbuf_copy_fence_user - copy fence object information to user-space.
4078  *
4079  * @dev_priv: Pointer to a vmw_private struct.
4080  * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
4081  * @ret: Return value from fence object creation.
4082  * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to which
4083  * the information should be copied.
4084  * @fence: Pointer to the fenc object.
4085  * @fence_handle: User-space fence handle.
4086  * @out_fence_fd: exported file descriptor for the fence.  -1 if not used
4087  *
4088  * This function copies fence information to user-space. If copying fails, the
4089  * user-space struct drm_vmw_fence_rep::error member is hopefully left
4090  * untouched, and if it's preloaded with an -EFAULT by user-space, the error
4091  * will hopefully be detected.
4092  *
4093  * Also if copying fails, user-space will be unable to signal the fence object
4094  * so we wait for it immediately, and then unreference the user-space reference.
4095  */
4096 int
4097 vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
4098 			    struct vmw_fpriv *vmw_fp, int ret,
4099 			    struct drm_vmw_fence_rep __user *user_fence_rep,
4100 			    struct vmw_fence_obj *fence, uint32_t fence_handle,
4101 			    int32_t out_fence_fd)
4102 {
4103 	struct drm_vmw_fence_rep fence_rep;
4104 
4105 	if (user_fence_rep == NULL)
4106 		return 0;
4107 
4108 	memset(&fence_rep, 0, sizeof(fence_rep));
4109 
4110 	fence_rep.error = ret;
4111 	fence_rep.fd = out_fence_fd;
4112 	if (ret == 0) {
4113 		BUG_ON(fence == NULL);
4114 
4115 		fence_rep.handle = fence_handle;
4116 		fence_rep.seqno = fence->base.seqno;
4117 		fence_rep.passed_seqno = vmw_fences_update(dev_priv->fman);
4118 	}
4119 
4120 	/*
4121 	 * copy_to_user errors will be detected by user space not seeing
4122 	 * fence_rep::error filled in. Typically user-space would have pre-set
4123 	 * that member to -EFAULT.
4124 	 */
4125 	ret = copy_to_user(user_fence_rep, &fence_rep,
4126 			   sizeof(fence_rep));
4127 
4128 	/*
4129 	 * User-space lost the fence object. We need to sync and unreference the
4130 	 * handle.
4131 	 */
4132 	if (unlikely(ret != 0) && (fence_rep.error == 0)) {
4133 		ttm_ref_object_base_unref(vmw_fp->tfile, fence_handle);
4134 		VMW_DEBUG_USER("Fence copy error. Syncing.\n");
4135 		(void) vmw_fence_obj_wait(fence, false, false,
4136 					  VMW_FENCE_WAIT_TIMEOUT);
4137 	}
4138 
4139 	return ret ? -EFAULT : 0;
4140 }
4141 
4142 /**
4143  * vmw_execbuf_submit_fifo - Patch a command batch and submit it using the fifo.
4144  *
4145  * @dev_priv: Pointer to a device private structure.
4146  * @kernel_commands: Pointer to the unpatched command batch.
4147  * @command_size: Size of the unpatched command batch.
4148  * @sw_context: Structure holding the relocation lists.
4149  *
4150  * Side effects: If this function returns 0, then the command batch pointed to
4151  * by @kernel_commands will have been modified.
4152  */
4153 static int vmw_execbuf_submit_fifo(struct vmw_private *dev_priv,
4154 				   void *kernel_commands, u32 command_size,
4155 				   struct vmw_sw_context *sw_context)
4156 {
4157 	void *cmd;
4158 
4159 	if (sw_context->dx_ctx_node)
4160 		cmd = VMW_CMD_CTX_RESERVE(dev_priv, command_size,
4161 					  sw_context->dx_ctx_node->ctx->id);
4162 	else
4163 		cmd = VMW_CMD_RESERVE(dev_priv, command_size);
4164 
4165 	if (!cmd)
4166 		return -ENOMEM;
4167 
4168 	vmw_apply_relocations(sw_context);
4169 	memcpy(cmd, kernel_commands, command_size);
4170 	vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
4171 	vmw_resource_relocations_free(&sw_context->res_relocations);
4172 	vmw_cmd_commit(dev_priv, command_size);
4173 
4174 	return 0;
4175 }
4176 
4177 /**
4178  * vmw_execbuf_submit_cmdbuf - Patch a command batch and submit it using the
4179  * command buffer manager.
4180  *
4181  * @dev_priv: Pointer to a device private structure.
4182  * @header: Opaque handle to the command buffer allocation.
4183  * @command_size: Size of the unpatched command batch.
4184  * @sw_context: Structure holding the relocation lists.
4185  *
4186  * Side effects: If this function returns 0, then the command buffer represented
4187  * by @header will have been modified.
4188  */
4189 static int vmw_execbuf_submit_cmdbuf(struct vmw_private *dev_priv,
4190 				     struct vmw_cmdbuf_header *header,
4191 				     u32 command_size,
4192 				     struct vmw_sw_context *sw_context)
4193 {
4194 	u32 id = ((sw_context->dx_ctx_node) ? sw_context->dx_ctx_node->ctx->id :
4195 		  SVGA3D_INVALID_ID);
4196 	void *cmd = vmw_cmdbuf_reserve(dev_priv->cman, command_size, id, false,
4197 				       header);
4198 
4199 	vmw_apply_relocations(sw_context);
4200 	vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
4201 	vmw_resource_relocations_free(&sw_context->res_relocations);
4202 	vmw_cmdbuf_commit(dev_priv->cman, command_size, header, false);
4203 
4204 	return 0;
4205 }
4206 
4207 /**
4208  * vmw_execbuf_cmdbuf - Prepare, if possible, a user-space command batch for
4209  * submission using a command buffer.
4210  *
4211  * @dev_priv: Pointer to a device private structure.
4212  * @user_commands: User-space pointer to the commands to be submitted.
4213  * @command_size: Size of the unpatched command batch.
4214  * @header: Out parameter returning the opaque pointer to the command buffer.
4215  *
4216  * This function checks whether we can use the command buffer manager for
4217  * submission and if so, creates a command buffer of suitable size and copies
4218  * the user data into that buffer.
4219  *
4220  * On successful return, the function returns a pointer to the data in the
4221  * command buffer and *@header is set to non-NULL.
4222  *
4223  * @kernel_commands: If command buffers could not be used, the function will
4224  * return the value of @kernel_commands on function call. That value may be
4225  * NULL. In that case, the value of *@header will be set to NULL.
4226  *
4227  * If an error is encountered, the function will return a pointer error value.
4228  * If the function is interrupted by a signal while sleeping, it will return
4229  * -ERESTARTSYS casted to a pointer error value.
4230  */
4231 static void *vmw_execbuf_cmdbuf(struct vmw_private *dev_priv,
4232 				void __user *user_commands,
4233 				void *kernel_commands, u32 command_size,
4234 				struct vmw_cmdbuf_header **header)
4235 {
4236 	size_t cmdbuf_size;
4237 	int ret;
4238 
4239 	*header = NULL;
4240 	if (command_size > SVGA_CB_MAX_SIZE) {
4241 		VMW_DEBUG_USER("Command buffer is too large.\n");
4242 		return ERR_PTR(-EINVAL);
4243 	}
4244 
4245 	if (!dev_priv->cman || kernel_commands)
4246 		return kernel_commands;
4247 
4248 	/* If possible, add a little space for fencing. */
4249 	cmdbuf_size = command_size + 512;
4250 	cmdbuf_size = min_t(size_t, cmdbuf_size, SVGA_CB_MAX_SIZE);
4251 	kernel_commands = vmw_cmdbuf_alloc(dev_priv->cman, cmdbuf_size, true,
4252 					   header);
4253 	if (IS_ERR(kernel_commands))
4254 		return kernel_commands;
4255 
4256 	ret = copy_from_user(kernel_commands, user_commands, command_size);
4257 	if (ret) {
4258 		VMW_DEBUG_USER("Failed copying commands.\n");
4259 		vmw_cmdbuf_header_free(*header);
4260 		*header = NULL;
4261 		return ERR_PTR(-EFAULT);
4262 	}
4263 
4264 	return kernel_commands;
4265 }
4266 
4267 static int vmw_execbuf_tie_context(struct vmw_private *dev_priv,
4268 				   struct vmw_sw_context *sw_context,
4269 				   uint32_t handle)
4270 {
4271 	struct vmw_resource *res;
4272 	int ret;
4273 	unsigned int size;
4274 
4275 	if (handle == SVGA3D_INVALID_ID)
4276 		return 0;
4277 
4278 	size = vmw_execbuf_res_size(dev_priv, vmw_res_dx_context);
4279 	ret = vmw_validation_preload_res(sw_context->ctx, size);
4280 	if (ret)
4281 		return ret;
4282 
4283 	ret = vmw_user_resource_lookup_handle
4284 		(dev_priv, sw_context->fp->tfile, handle,
4285 		 user_context_converter, &res);
4286 	if (ret != 0) {
4287 		VMW_DEBUG_USER("Could not find or user DX context 0x%08x.\n",
4288 			       (unsigned int) handle);
4289 		return ret;
4290 	}
4291 
4292 	ret = vmw_execbuf_res_val_add(sw_context, res, VMW_RES_DIRTY_SET,
4293 				      vmw_val_add_flag_none);
4294 	if (unlikely(ret != 0)) {
4295 		vmw_resource_unreference(&res);
4296 		return ret;
4297 	}
4298 
4299 	sw_context->dx_ctx_node = vmw_execbuf_info_from_res(sw_context, res);
4300 	sw_context->man = vmw_context_res_man(res);
4301 
4302 	vmw_resource_unreference(&res);
4303 	return 0;
4304 }
4305 
4306 int vmw_execbuf_process(struct drm_file *file_priv,
4307 			struct vmw_private *dev_priv,
4308 			void __user *user_commands, void *kernel_commands,
4309 			uint32_t command_size, uint64_t throttle_us,
4310 			uint32_t dx_context_handle,
4311 			struct drm_vmw_fence_rep __user *user_fence_rep,
4312 			struct vmw_fence_obj **out_fence, uint32_t flags)
4313 {
4314 	struct vmw_sw_context *sw_context = &dev_priv->ctx;
4315 	struct vmw_fence_obj *fence = NULL;
4316 	struct vmw_cmdbuf_header *header;
4317 	uint32_t handle = 0;
4318 	int ret;
4319 	int32_t out_fence_fd = -1;
4320 	struct sync_file *sync_file = NULL;
4321 	DECLARE_VAL_CONTEXT(val_ctx, sw_context, 1);
4322 
4323 	if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
4324 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
4325 		if (out_fence_fd < 0) {
4326 			VMW_DEBUG_USER("Failed to get a fence fd.\n");
4327 			return out_fence_fd;
4328 		}
4329 	}
4330 
4331 	if (throttle_us) {
4332 		VMW_DEBUG_USER("Throttling is no longer supported.\n");
4333 	}
4334 
4335 	kernel_commands = vmw_execbuf_cmdbuf(dev_priv, user_commands,
4336 					     kernel_commands, command_size,
4337 					     &header);
4338 	if (IS_ERR(kernel_commands)) {
4339 		ret = PTR_ERR(kernel_commands);
4340 		goto out_free_fence_fd;
4341 	}
4342 
4343 	ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
4344 	if (ret) {
4345 		ret = -ERESTARTSYS;
4346 		goto out_free_header;
4347 	}
4348 
4349 	sw_context->kernel = false;
4350 	if (kernel_commands == NULL) {
4351 		ret = vmw_resize_cmd_bounce(sw_context, command_size);
4352 		if (unlikely(ret != 0))
4353 			goto out_unlock;
4354 
4355 		ret = copy_from_user(sw_context->cmd_bounce, user_commands,
4356 				     command_size);
4357 		if (unlikely(ret != 0)) {
4358 			ret = -EFAULT;
4359 			VMW_DEBUG_USER("Failed copying commands.\n");
4360 			goto out_unlock;
4361 		}
4362 
4363 		kernel_commands = sw_context->cmd_bounce;
4364 	} else if (!header) {
4365 		sw_context->kernel = true;
4366 	}
4367 
4368 	sw_context->filp = file_priv;
4369 	sw_context->fp = vmw_fpriv(file_priv);
4370 	INIT_LIST_HEAD(&sw_context->ctx_list);
4371 	sw_context->cur_query_bo = dev_priv->pinned_bo;
4372 	sw_context->last_query_ctx = NULL;
4373 	sw_context->needs_post_query_barrier = false;
4374 	sw_context->dx_ctx_node = NULL;
4375 	sw_context->dx_query_mob = NULL;
4376 	sw_context->dx_query_ctx = NULL;
4377 	memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
4378 	INIT_LIST_HEAD(&sw_context->res_relocations);
4379 	INIT_LIST_HEAD(&sw_context->bo_relocations);
4380 
4381 	if (sw_context->staged_bindings)
4382 		vmw_binding_state_reset(sw_context->staged_bindings);
4383 
4384 	INIT_LIST_HEAD(&sw_context->staged_cmd_res);
4385 	sw_context->ctx = &val_ctx;
4386 	ret = vmw_execbuf_tie_context(dev_priv, sw_context, dx_context_handle);
4387 	if (unlikely(ret != 0))
4388 		goto out_err_nores;
4389 
4390 	ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
4391 				command_size);
4392 	if (unlikely(ret != 0))
4393 		goto out_err_nores;
4394 
4395 	ret = vmw_resources_reserve(sw_context);
4396 	if (unlikely(ret != 0))
4397 		goto out_err_nores;
4398 
4399 	ret = vmw_validation_bo_reserve(&val_ctx, true);
4400 	if (unlikely(ret != 0))
4401 		goto out_err_nores;
4402 
4403 	ret = vmw_validation_bo_validate(&val_ctx, true);
4404 	if (unlikely(ret != 0))
4405 		goto out_err;
4406 
4407 	ret = vmw_validation_res_validate(&val_ctx, true);
4408 	if (unlikely(ret != 0))
4409 		goto out_err;
4410 
4411 	vmw_validation_drop_ht(&val_ctx);
4412 
4413 	ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
4414 	if (unlikely(ret != 0)) {
4415 		ret = -ERESTARTSYS;
4416 		goto out_err;
4417 	}
4418 
4419 	if (dev_priv->has_mob) {
4420 		ret = vmw_rebind_contexts(sw_context);
4421 		if (unlikely(ret != 0))
4422 			goto out_unlock_binding;
4423 	}
4424 
4425 	if (!header) {
4426 		ret = vmw_execbuf_submit_fifo(dev_priv, kernel_commands,
4427 					      command_size, sw_context);
4428 	} else {
4429 		ret = vmw_execbuf_submit_cmdbuf(dev_priv, header, command_size,
4430 						sw_context);
4431 		header = NULL;
4432 	}
4433 	mutex_unlock(&dev_priv->binding_mutex);
4434 	if (ret)
4435 		goto out_err;
4436 
4437 	vmw_query_bo_switch_commit(dev_priv, sw_context);
4438 	ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
4439 					 (user_fence_rep) ? &handle : NULL);
4440 	/*
4441 	 * This error is harmless, because if fence submission fails,
4442 	 * vmw_fifo_send_fence will sync. The error will be propagated to
4443 	 * user-space in @fence_rep
4444 	 */
4445 	if (ret != 0)
4446 		VMW_DEBUG_USER("Fence submission error. Syncing.\n");
4447 
4448 	vmw_execbuf_bindings_commit(sw_context, false);
4449 	vmw_bind_dx_query_mob(sw_context);
4450 	vmw_validation_res_unreserve(&val_ctx, false);
4451 
4452 	vmw_validation_bo_fence(sw_context->ctx, fence);
4453 
4454 	if (unlikely(dev_priv->pinned_bo != NULL && !dev_priv->query_cid_valid))
4455 		__vmw_execbuf_release_pinned_bo(dev_priv, fence);
4456 
4457 	/*
4458 	 * If anything fails here, give up trying to export the fence and do a
4459 	 * sync since the user mode will not be able to sync the fence itself.
4460 	 * This ensures we are still functionally correct.
4461 	 */
4462 	if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
4463 
4464 		sync_file = sync_file_create(&fence->base);
4465 		if (!sync_file) {
4466 			VMW_DEBUG_USER("Sync file create failed for fence\n");
4467 			put_unused_fd(out_fence_fd);
4468 			out_fence_fd = -1;
4469 
4470 			(void) vmw_fence_obj_wait(fence, false, false,
4471 						  VMW_FENCE_WAIT_TIMEOUT);
4472 		}
4473 	}
4474 
4475 	ret = vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
4476 				    user_fence_rep, fence, handle, out_fence_fd);
4477 
4478 	if (sync_file) {
4479 		if (ret) {
4480 			/* usercopy of fence failed, put the file object */
4481 			fput(sync_file->file);
4482 			put_unused_fd(out_fence_fd);
4483 		} else {
4484 			/* Link the fence with the FD created earlier */
4485 			fd_install(out_fence_fd, sync_file->file);
4486 		}
4487 	}
4488 
4489 	/* Don't unreference when handing fence out */
4490 	if (unlikely(out_fence != NULL)) {
4491 		*out_fence = fence;
4492 		fence = NULL;
4493 	} else if (likely(fence != NULL)) {
4494 		vmw_fence_obj_unreference(&fence);
4495 	}
4496 
4497 	vmw_cmdbuf_res_commit(&sw_context->staged_cmd_res);
4498 	mutex_unlock(&dev_priv->cmdbuf_mutex);
4499 
4500 	/*
4501 	 * Unreference resources outside of the cmdbuf_mutex to avoid deadlocks
4502 	 * in resource destruction paths.
4503 	 */
4504 	vmw_validation_unref_lists(&val_ctx);
4505 
4506 	return ret;
4507 
4508 out_unlock_binding:
4509 	mutex_unlock(&dev_priv->binding_mutex);
4510 out_err:
4511 	vmw_validation_bo_backoff(&val_ctx);
4512 out_err_nores:
4513 	vmw_execbuf_bindings_commit(sw_context, true);
4514 	vmw_validation_res_unreserve(&val_ctx, true);
4515 	vmw_resource_relocations_free(&sw_context->res_relocations);
4516 	vmw_free_relocations(sw_context);
4517 	if (unlikely(dev_priv->pinned_bo != NULL && !dev_priv->query_cid_valid))
4518 		__vmw_execbuf_release_pinned_bo(dev_priv, NULL);
4519 out_unlock:
4520 	vmw_cmdbuf_res_revert(&sw_context->staged_cmd_res);
4521 	vmw_validation_drop_ht(&val_ctx);
4522 	WARN_ON(!list_empty(&sw_context->ctx_list));
4523 	mutex_unlock(&dev_priv->cmdbuf_mutex);
4524 
4525 	/*
4526 	 * Unreference resources outside of the cmdbuf_mutex to avoid deadlocks
4527 	 * in resource destruction paths.
4528 	 */
4529 	vmw_validation_unref_lists(&val_ctx);
4530 out_free_header:
4531 	if (header)
4532 		vmw_cmdbuf_header_free(header);
4533 out_free_fence_fd:
4534 	if (out_fence_fd >= 0)
4535 		put_unused_fd(out_fence_fd);
4536 
4537 	return ret;
4538 }
4539 
4540 /**
4541  * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
4542  *
4543  * @dev_priv: The device private structure.
4544  *
4545  * This function is called to idle the fifo and unpin the query buffer if the
4546  * normal way to do this hits an error, which should typically be extremely
4547  * rare.
4548  */
4549 static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
4550 {
4551 	VMW_DEBUG_USER("Can't unpin query buffer. Trying to recover.\n");
4552 
4553 	(void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
4554 	vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
4555 	if (dev_priv->dummy_query_bo_pinned) {
4556 		vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
4557 		dev_priv->dummy_query_bo_pinned = false;
4558 	}
4559 }
4560 
4561 
4562 /**
4563  * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned query
4564  * bo.
4565  *
4566  * @dev_priv: The device private structure.
4567  * @fence: If non-NULL should point to a struct vmw_fence_obj issued _after_ a
4568  * query barrier that flushes all queries touching the current buffer pointed to
4569  * by @dev_priv->pinned_bo
4570  *
4571  * This function should be used to unpin the pinned query bo, or as a query
4572  * barrier when we need to make sure that all queries have finished before the
4573  * next fifo command. (For example on hardware context destructions where the
4574  * hardware may otherwise leak unfinished queries).
4575  *
4576  * This function does not return any failure codes, but make attempts to do safe
4577  * unpinning in case of errors.
4578  *
4579  * The function will synchronize on the previous query barrier, and will thus
4580  * not finish until that barrier has executed.
4581  *
4582  * the @dev_priv->cmdbuf_mutex needs to be held by the current thread before
4583  * calling this function.
4584  */
4585 void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
4586 				     struct vmw_fence_obj *fence)
4587 {
4588 	int ret = 0;
4589 	struct vmw_fence_obj *lfence = NULL;
4590 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
4591 
4592 	if (dev_priv->pinned_bo == NULL)
4593 		goto out_unlock;
4594 
4595 	vmw_bo_placement_set(dev_priv->pinned_bo,
4596 			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
4597 			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
4598 	ret = vmw_validation_add_bo(&val_ctx, dev_priv->pinned_bo);
4599 	if (ret)
4600 		goto out_no_reserve;
4601 
4602 	vmw_bo_placement_set(dev_priv->dummy_query_bo,
4603 			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
4604 			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
4605 	ret = vmw_validation_add_bo(&val_ctx, dev_priv->dummy_query_bo);
4606 	if (ret)
4607 		goto out_no_reserve;
4608 
4609 	ret = vmw_validation_bo_reserve(&val_ctx, false);
4610 	if (ret)
4611 		goto out_no_reserve;
4612 
4613 	if (dev_priv->query_cid_valid) {
4614 		BUG_ON(fence != NULL);
4615 		ret = vmw_cmd_emit_dummy_query(dev_priv, dev_priv->query_cid);
4616 		if (ret)
4617 			goto out_no_emit;
4618 		dev_priv->query_cid_valid = false;
4619 	}
4620 
4621 	vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
4622 	if (dev_priv->dummy_query_bo_pinned) {
4623 		vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
4624 		dev_priv->dummy_query_bo_pinned = false;
4625 	}
4626 	if (fence == NULL) {
4627 		(void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
4628 						  NULL);
4629 		fence = lfence;
4630 	}
4631 	vmw_validation_bo_fence(&val_ctx, fence);
4632 	if (lfence != NULL)
4633 		vmw_fence_obj_unreference(&lfence);
4634 
4635 	vmw_validation_unref_lists(&val_ctx);
4636 	vmw_bo_unreference(&dev_priv->pinned_bo);
4637 
4638 out_unlock:
4639 	return;
4640 out_no_emit:
4641 	vmw_validation_bo_backoff(&val_ctx);
4642 out_no_reserve:
4643 	vmw_validation_unref_lists(&val_ctx);
4644 	vmw_execbuf_unpin_panic(dev_priv);
4645 	vmw_bo_unreference(&dev_priv->pinned_bo);
4646 }
4647 
4648 /**
4649  * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned query bo.
4650  *
4651  * @dev_priv: The device private structure.
4652  *
4653  * This function should be used to unpin the pinned query bo, or as a query
4654  * barrier when we need to make sure that all queries have finished before the
4655  * next fifo command. (For example on hardware context destructions where the
4656  * hardware may otherwise leak unfinished queries).
4657  *
4658  * This function does not return any failure codes, but make attempts to do safe
4659  * unpinning in case of errors.
4660  *
4661  * The function will synchronize on the previous query barrier, and will thus
4662  * not finish until that barrier has executed.
4663  */
4664 void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
4665 {
4666 	mutex_lock(&dev_priv->cmdbuf_mutex);
4667 	if (dev_priv->query_cid_valid)
4668 		__vmw_execbuf_release_pinned_bo(dev_priv, NULL);
4669 	mutex_unlock(&dev_priv->cmdbuf_mutex);
4670 }
4671 
4672 int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
4673 		      struct drm_file *file_priv)
4674 {
4675 	struct vmw_private *dev_priv = vmw_priv(dev);
4676 	struct drm_vmw_execbuf_arg *arg = data;
4677 	int ret;
4678 	struct dma_fence *in_fence = NULL;
4679 
4680 	MKS_STAT_TIME_DECL(MKSSTAT_KERN_EXECBUF);
4681 	MKS_STAT_TIME_PUSH(MKSSTAT_KERN_EXECBUF);
4682 
4683 	/*
4684 	 * Extend the ioctl argument while maintaining backwards compatibility:
4685 	 * We take different code paths depending on the value of arg->version.
4686 	 *
4687 	 * Note: The ioctl argument is extended and zeropadded by core DRM.
4688 	 */
4689 	if (unlikely(arg->version > DRM_VMW_EXECBUF_VERSION ||
4690 		     arg->version == 0)) {
4691 		VMW_DEBUG_USER("Incorrect execbuf version.\n");
4692 		ret = -EINVAL;
4693 		goto mksstats_out;
4694 	}
4695 
4696 	switch (arg->version) {
4697 	case 1:
4698 		/* For v1 core DRM have extended + zeropadded the data */
4699 		arg->context_handle = (uint32_t) -1;
4700 		break;
4701 	case 2:
4702 	default:
4703 		/* For v2 and later core DRM would have correctly copied it */
4704 		break;
4705 	}
4706 
4707 	/* If imported a fence FD from elsewhere, then wait on it */
4708 	if (arg->flags & DRM_VMW_EXECBUF_FLAG_IMPORT_FENCE_FD) {
4709 		in_fence = sync_file_get_fence(arg->imported_fence_fd);
4710 
4711 		if (!in_fence) {
4712 			VMW_DEBUG_USER("Cannot get imported fence\n");
4713 			ret = -EINVAL;
4714 			goto mksstats_out;
4715 		}
4716 
4717 		ret = dma_fence_wait(in_fence, true);
4718 		if (ret)
4719 			goto out;
4720 	}
4721 
4722 	ret = vmw_execbuf_process(file_priv, dev_priv,
4723 				  (void __user *)(unsigned long)arg->commands,
4724 				  NULL, arg->command_size, arg->throttle_us,
4725 				  arg->context_handle,
4726 				  (void __user *)(unsigned long)arg->fence_rep,
4727 				  NULL, arg->flags);
4728 
4729 	if (unlikely(ret != 0))
4730 		goto out;
4731 
4732 out:
4733 	if (in_fence)
4734 		dma_fence_put(in_fence);
4735 
4736 mksstats_out:
4737 	MKS_STAT_TIME_POP(MKSSTAT_KERN_EXECBUF);
4738 	return ret;
4739 }
4740