blob: cc8b1d943c55d1c1362855a4f8dc5a266ec4d114 [file] [log] [blame]
Dirk Hohndel (VMware)dff96882018-05-07 01:16:26 +02001// SPDX-License-Identifier: GPL-2.0 OR MIT
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00002/**************************************************************************
3 *
Dirk Hohndel (VMware)dff96882018-05-07 01:16:26 +02004 * Copyright 2009 - 2015 VMware, Inc., Palo Alto, CA., USA
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00005 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
Sinclair Yeh585851162017-07-05 01:45:40 -070027#include <linux/sync_file.h>
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000028
29#include "vmwgfx_drv.h"
30#include "vmwgfx_reg.h"
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/ttm/ttm_bo_api.h>
32#include <drm/ttm/ttm_placement.h>
Thomas Hellstromd80efd52015-08-10 10:39:35 -070033#include "vmwgfx_so.h"
34#include "vmwgfx_binding.h"
Martin Krastev7a7a9332021-06-09 13:23:00 -040035#include "vmwgfx_mksstat.h"
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000036
Thomas Hellstromc0951b72012-11-20 12:19:35 +000037#define VMW_RES_HT_ORDER 12
38
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020039/*
Deepak Rawat6f74fd92019-02-08 12:53:57 -080040 * Helper macro to get dx_ctx_node if available otherwise print an error
41 * message. This is for use in command verifier function where if dx_ctx_node
42 * is not set then command is invalid.
43 */
44#define VMW_GET_CTX_NODE(__sw_context) \
45({ \
46 __sw_context->dx_ctx_node ? __sw_context->dx_ctx_node : ({ \
Deepak Rawat5724f892019-02-11 11:46:27 -080047 VMW_DEBUG_USER("SM context is not set at %s\n", __func__); \
Deepak Rawat6f74fd92019-02-08 12:53:57 -080048 __sw_context->dx_ctx_node; \
49 }); \
50})
51
Deepak Rawatd01316d2019-02-08 15:50:40 -080052#define VMW_DECLARE_CMD_VAR(__var, __type) \
53 struct { \
54 SVGA3dCmdHeader header; \
55 __type body; \
56 } __var
57
Deepak Rawat680360a2019-02-13 13:20:42 -080058/**
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020059 * struct vmw_relocation - Buffer object relocation
60 *
61 * @head: List head for the command submission context's relocation list
Thomas Hellstromcc1e3b72018-09-26 15:38:13 +020062 * @vbo: Non ref-counted pointer to buffer object
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020063 * @mob_loc: Pointer to location for mob id to be modified
64 * @location: Pointer to location for guest pointer to be modified
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020065 */
66struct vmw_relocation {
67 struct list_head head;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020068 struct vmw_buffer_object *vbo;
Thomas Hellstromcc1e3b72018-09-26 15:38:13 +020069 union {
70 SVGAMobId *mob_loc;
71 SVGAGuestPtr *location;
72 };
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020073};
74
Thomas Hellstromc0951b72012-11-20 12:19:35 +000075/**
Thomas Hellstroma1944032016-10-10 11:06:45 -070076 * enum vmw_resource_relocation_type - Relocation type for resources
77 *
78 * @vmw_res_rel_normal: Traditional relocation. The resource id in the
79 * command stream is replaced with the actual id after validation.
80 * @vmw_res_rel_nop: NOP relocation. The command is unconditionally replaced
81 * with a NOP.
Deepak Rawat680360a2019-02-13 13:20:42 -080082 * @vmw_res_rel_cond_nop: Conditional NOP relocation. If the resource id after
83 * validation is -1, the command is replaced with a NOP. Otherwise no action.
Lee Jones7450bf72021-01-15 18:12:36 +000084 * @vmw_res_rel_max: Last value in the enum - used for error checking
85*/
Thomas Hellstroma1944032016-10-10 11:06:45 -070086enum vmw_resource_relocation_type {
87 vmw_res_rel_normal,
88 vmw_res_rel_nop,
89 vmw_res_rel_cond_nop,
90 vmw_res_rel_max
91};
92
93/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +000094 * struct vmw_resource_relocation - Relocation info for resources
95 *
96 * @head: List head for the software context's relocation list.
97 * @res: Non-ref-counted pointer to the resource.
Deepak Rawat680360a2019-02-13 13:20:42 -080098 * @offset: Offset of single byte entries into the command buffer where the id
99 * that needs fixup is located.
Thomas Hellstroma1944032016-10-10 11:06:45 -0700100 * @rel_type: Type of relocation.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000101 */
102struct vmw_resource_relocation {
103 struct list_head head;
104 const struct vmw_resource *res;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700105 u32 offset:29;
106 enum vmw_resource_relocation_type rel_type:3;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000107};
108
Deepak Rawat680360a2019-02-13 13:20:42 -0800109/**
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200110 * struct vmw_ctx_validation_info - Extra validation metadata for contexts
Deepak Rawat680360a2019-02-13 13:20:42 -0800111 *
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200112 * @head: List head of context list
113 * @ctx: The context resource
114 * @cur: The context's persistent binding state
115 * @staged: The binding state changes of this command buffer
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000116 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200117struct vmw_ctx_validation_info {
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000118 struct list_head head;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200119 struct vmw_resource *ctx;
120 struct vmw_ctx_binding_state *cur;
121 struct vmw_ctx_binding_state *staged;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000122};
123
124/**
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100125 * struct vmw_cmd_entry - Describe a command for the verifier
126 *
Lee Jones7450bf72021-01-15 18:12:36 +0000127 * @func: Call-back to handle the command.
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100128 * @user_allow: Whether allowed from the execbuf ioctl.
129 * @gb_disable: Whether disabled if guest-backed objects are available.
130 * @gb_enable: Whether enabled iff guest-backed objects are available.
Lee Jones7450bf72021-01-15 18:12:36 +0000131 * @cmd_name: Name of the command.
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100132 */
133struct vmw_cmd_entry {
134 int (*func) (struct vmw_private *, struct vmw_sw_context *,
135 SVGA3dCmdHeader *);
136 bool user_allow;
137 bool gb_disable;
138 bool gb_enable;
Thomas Hellstrom65b97a22017-08-24 08:06:29 +0200139 const char *cmd_name;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100140};
141
142#define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable) \
143 [(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
Thomas Hellstrom65b97a22017-08-24 08:06:29 +0200144 (_gb_disable), (_gb_enable), #_cmd}
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100145
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700146static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
147 struct vmw_sw_context *sw_context,
148 struct vmw_resource *ctx);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700149static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
150 struct vmw_sw_context *sw_context,
151 SVGAMobId *id,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200152 struct vmw_buffer_object **vmw_bo_p);
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700153/**
154 * vmw_ptr_diff - Compute the offset from a to b in bytes
155 *
156 * @a: A starting pointer.
157 * @b: A pointer offset in the same address space.
158 *
159 * Returns: The offset in bytes between the two pointers.
160 */
161static size_t vmw_ptr_diff(void *a, void *b)
162{
163 return (unsigned long) b - (unsigned long) a;
164}
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700165
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100166/**
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200167 * vmw_execbuf_bindings_commit - Commit modified binding state
Deepak Rawat680360a2019-02-13 13:20:42 -0800168 *
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200169 * @sw_context: The command submission context
Deepak Rawat680360a2019-02-13 13:20:42 -0800170 * @backoff: Whether this is part of the error path and binding state changes
171 * should be ignored
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000172 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200173static void vmw_execbuf_bindings_commit(struct vmw_sw_context *sw_context,
174 bool backoff)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000175{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200176 struct vmw_ctx_validation_info *entry;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700177
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200178 list_for_each_entry(entry, &sw_context->ctx_list, head) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200179 if (!backoff)
180 vmw_binding_state_commit(entry->cur, entry->staged);
Deepak Rawat680360a2019-02-13 13:20:42 -0800181
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200182 if (entry->staged != sw_context->staged_bindings)
183 vmw_binding_state_free(entry->staged);
184 else
185 sw_context->staged_bindings_inuse = false;
186 }
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200187
188 /* List entries are freed with the validation context */
189 INIT_LIST_HEAD(&sw_context->ctx_list);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200190}
191
192/**
193 * vmw_bind_dx_query_mob - Bind the DX query MOB if referenced
Deepak Rawat680360a2019-02-13 13:20:42 -0800194 *
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200195 * @sw_context: The command submission context
196 */
197static void vmw_bind_dx_query_mob(struct vmw_sw_context *sw_context)
198{
199 if (sw_context->dx_query_mob)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700200 vmw_context_bind_dx_query(sw_context->dx_query_ctx,
201 sw_context->dx_query_mob);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000202}
203
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700204/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800205 * vmw_cmd_ctx_first_setup - Perform the setup needed when a context is added to
206 * the validate list.
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700207 *
208 * @dev_priv: Pointer to the device private:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200209 * @sw_context: The command submission context
Lee Jones7450bf72021-01-15 18:12:36 +0000210 * @res: Pointer to the resource
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200211 * @node: The validation node holding the context resource metadata
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700212 */
213static int vmw_cmd_ctx_first_setup(struct vmw_private *dev_priv,
214 struct vmw_sw_context *sw_context,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200215 struct vmw_resource *res,
216 struct vmw_ctx_validation_info *node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700217{
218 int ret;
219
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200220 ret = vmw_resource_context_res_add(dev_priv, sw_context, res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700221 if (unlikely(ret != 0))
222 goto out_err;
223
224 if (!sw_context->staged_bindings) {
Deepak Rawat680360a2019-02-13 13:20:42 -0800225 sw_context->staged_bindings = vmw_binding_state_alloc(dev_priv);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700226 if (IS_ERR(sw_context->staged_bindings)) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700227 ret = PTR_ERR(sw_context->staged_bindings);
228 sw_context->staged_bindings = NULL;
229 goto out_err;
230 }
231 }
232
233 if (sw_context->staged_bindings_inuse) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200234 node->staged = vmw_binding_state_alloc(dev_priv);
235 if (IS_ERR(node->staged)) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200236 ret = PTR_ERR(node->staged);
237 node->staged = NULL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700238 goto out_err;
239 }
240 } else {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200241 node->staged = sw_context->staged_bindings;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700242 sw_context->staged_bindings_inuse = true;
243 }
244
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200245 node->ctx = res;
246 node->cur = vmw_context_binding_state(res);
247 list_add_tail(&node->head, &sw_context->ctx_list);
248
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700249 return 0;
Deepak Rawat680360a2019-02-13 13:20:42 -0800250
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700251out_err:
252 return ret;
253}
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000254
255/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800256 * vmw_execbuf_res_size - calculate extra size fore the resource validation node
257 *
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200258 * @dev_priv: Pointer to the device private struct.
259 * @res_type: The resource type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000260 *
Deepak Rawat680360a2019-02-13 13:20:42 -0800261 * Guest-backed contexts and DX contexts require extra size to store execbuf
262 * private information in the validation node. Typically the binding manager
263 * associated data structures.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200264 *
265 * Returns: The extra size requirement based on resource type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000266 */
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200267static unsigned int vmw_execbuf_res_size(struct vmw_private *dev_priv,
268 enum vmw_res_type res_type)
269{
270 return (res_type == vmw_res_dx_context ||
271 (res_type == vmw_res_context && dev_priv->has_mob)) ?
272 sizeof(struct vmw_ctx_validation_info) : 0;
273}
274
275/**
276 * vmw_execbuf_rcache_update - Update a resource-node cache entry
277 *
278 * @rcache: Pointer to the entry to update.
279 * @res: Pointer to the resource.
Deepak Rawat680360a2019-02-13 13:20:42 -0800280 * @private: Pointer to the execbuf-private space in the resource validation
281 * node.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200282 */
283static void vmw_execbuf_rcache_update(struct vmw_res_cache_entry *rcache,
284 struct vmw_resource *res,
285 void *private)
286{
287 rcache->res = res;
288 rcache->private = private;
289 rcache->valid = 1;
290 rcache->valid_handle = 0;
291}
292
293/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800294 * vmw_execbuf_res_noref_val_add - Add a resource described by an unreferenced
295 * rcu-protected pointer to the validation list.
296 *
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200297 * @sw_context: Pointer to the software context.
298 * @res: Unreferenced rcu-protected pointer to the resource.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100299 * @dirty: Whether to change dirty status.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200300 *
Deepak Rawat680360a2019-02-13 13:20:42 -0800301 * Returns: 0 on success. Negative error code on failure. Typical error codes
302 * are %-EINVAL on inconsistency and %-ESRCH if the resource was doomed.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200303 */
304static int vmw_execbuf_res_noref_val_add(struct vmw_sw_context *sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100305 struct vmw_resource *res,
306 u32 dirty)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000307{
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700308 struct vmw_private *dev_priv = res->dev_priv;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000309 int ret;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200310 enum vmw_res_type res_type = vmw_res_type(res);
311 struct vmw_res_cache_entry *rcache;
312 struct vmw_ctx_validation_info *ctx_info;
313 bool first_usage;
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200314 unsigned int priv_size;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000315
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200316 rcache = &sw_context->res_cache[res_type];
317 if (likely(rcache->valid && rcache->res == res)) {
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100318 if (dirty)
319 vmw_validation_res_set_dirty(sw_context->ctx,
320 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200321 vmw_user_resource_noref_release();
322 return 0;
323 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000324
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200325 priv_size = vmw_execbuf_res_size(dev_priv, res_type);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200326 ret = vmw_validation_add_resource(sw_context->ctx, res, priv_size,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100327 dirty, (void **)&ctx_info,
328 &first_usage);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200329 vmw_user_resource_noref_release();
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200330 if (ret)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000331 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000332
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200333 if (priv_size && first_usage) {
334 ret = vmw_cmd_ctx_first_setup(dev_priv, sw_context, res,
335 ctx_info);
Deepak Rawatb2898402019-02-11 14:59:57 -0800336 if (ret) {
337 VMW_DEBUG_USER("Failed first usage context setup.\n");
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200338 return ret;
Deepak Rawatb2898402019-02-11 14:59:57 -0800339 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700340 }
341
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200342 vmw_execbuf_rcache_update(rcache, res, ctx_info);
343 return 0;
344}
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700345
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200346/**
347 * vmw_execbuf_res_noctx_val_add - Add a non-context resource to the resource
348 * validation list if it's not already on it
Deepak Rawat680360a2019-02-13 13:20:42 -0800349 *
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200350 * @sw_context: Pointer to the software context.
351 * @res: Pointer to the resource.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100352 * @dirty: Whether to change dirty status.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200353 *
354 * Returns: Zero on success. Negative error code on failure.
355 */
356static int vmw_execbuf_res_noctx_val_add(struct vmw_sw_context *sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100357 struct vmw_resource *res,
358 u32 dirty)
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200359{
360 struct vmw_res_cache_entry *rcache;
361 enum vmw_res_type res_type = vmw_res_type(res);
362 void *ptr;
363 int ret;
364
365 rcache = &sw_context->res_cache[res_type];
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100366 if (likely(rcache->valid && rcache->res == res)) {
367 if (dirty)
368 vmw_validation_res_set_dirty(sw_context->ctx,
369 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200370 return 0;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100371 }
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200372
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100373 ret = vmw_validation_add_resource(sw_context->ctx, res, 0, dirty,
374 &ptr, NULL);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200375 if (ret)
376 return ret;
377
378 vmw_execbuf_rcache_update(rcache, res, ptr);
379
380 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700381}
382
383/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800384 * vmw_view_res_val_add - Add a view and the surface it's pointing to to the
385 * validation list
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700386 *
387 * @sw_context: The software context holding the validation list.
388 * @view: Pointer to the view resource.
389 *
390 * Returns 0 if success, negative error code otherwise.
391 */
392static int vmw_view_res_val_add(struct vmw_sw_context *sw_context,
393 struct vmw_resource *view)
394{
395 int ret;
396
397 /*
Deepak Rawat680360a2019-02-13 13:20:42 -0800398 * First add the resource the view is pointing to, otherwise it may be
399 * swapped out when the view is validated.
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700400 */
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100401 ret = vmw_execbuf_res_noctx_val_add(sw_context, vmw_view_srf(view),
402 vmw_view_dirtying(view));
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700403 if (ret)
404 return ret;
405
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100406 return vmw_execbuf_res_noctx_val_add(sw_context, view,
407 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700408}
409
410/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800411 * vmw_view_id_val_add - Look up a view and add it and the surface it's pointing
412 * to to the validation list.
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700413 *
414 * @sw_context: The software context holding the validation list.
415 * @view_type: The view type to look up.
416 * @id: view id of the view.
417 *
Deepak Rawat680360a2019-02-13 13:20:42 -0800418 * The view is represented by a view id and the DX context it's created on, or
419 * scheduled for creation on. If there is no DX context set, the function will
420 * return an -EINVAL error pointer.
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200421 *
422 * Returns: Unreferenced pointer to the resource on success, negative error
423 * pointer on failure.
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700424 */
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200425static struct vmw_resource *
426vmw_view_id_val_add(struct vmw_sw_context *sw_context,
427 enum vmw_view_type view_type, u32 id)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700428{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200429 struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700430 struct vmw_resource *view;
431 int ret;
432
Deepak Rawatb2898402019-02-11 14:59:57 -0800433 if (!ctx_node)
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200434 return ERR_PTR(-EINVAL);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700435
436 view = vmw_view_lookup(sw_context->man, view_type, id);
437 if (IS_ERR(view))
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200438 return view;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700439
440 ret = vmw_view_res_val_add(sw_context, view);
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200441 if (ret)
442 return ERR_PTR(ret);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700443
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200444 return view;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000445}
446
447/**
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100448 * vmw_resource_context_res_add - Put resources previously bound to a context on
449 * the validation list
450 *
451 * @dev_priv: Pointer to a device private structure
452 * @sw_context: Pointer to a software context used for this command submission
453 * @ctx: Pointer to the context resource
454 *
Deepak Rawat680360a2019-02-13 13:20:42 -0800455 * This function puts all resources that were previously bound to @ctx on the
456 * resource validation list. This is part of the context state reemission
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100457 */
458static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
459 struct vmw_sw_context *sw_context,
460 struct vmw_resource *ctx)
461{
462 struct list_head *binding_list;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700463 struct vmw_ctx_bindinfo *entry;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100464 int ret = 0;
465 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700466 u32 i;
Deepak Rawat5e8ec0d2018-12-13 13:51:08 -0800467 u32 cotable_max = has_sm5_context(ctx->dev_priv) ?
468 SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100469
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700470 /* Add all cotables to the validation list. */
Deepak Rawat878c6ec2018-12-13 11:44:42 -0800471 if (has_sm4_context(dev_priv) &&
472 vmw_res_type(ctx) == vmw_res_dx_context) {
Deepak Rawat5e8ec0d2018-12-13 13:51:08 -0800473 for (i = 0; i < cotable_max; ++i) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700474 res = vmw_context_cotable(ctx, i);
475 if (IS_ERR(res))
476 continue;
477
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100478 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
479 VMW_RES_DIRTY_SET);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700480 if (unlikely(ret != 0))
481 return ret;
482 }
483 }
484
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700485 /* Add all resources bound to the context to the validation list */
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100486 mutex_lock(&dev_priv->binding_mutex);
487 binding_list = vmw_context_binding_list(ctx);
488
489 list_for_each_entry(entry, binding_list, ctx_list) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700490 if (vmw_res_type(entry->res) == vmw_res_view)
491 ret = vmw_view_res_val_add(sw_context, entry->res);
492 else
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100493 ret = vmw_execbuf_res_noctx_val_add
494 (sw_context, entry->res,
495 vmw_binding_dirtying(entry->bt));
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100496 if (unlikely(ret != 0))
497 break;
498 }
499
Deepak Rawat878c6ec2018-12-13 11:44:42 -0800500 if (has_sm4_context(dev_priv) &&
501 vmw_res_type(ctx) == vmw_res_dx_context) {
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200502 struct vmw_buffer_object *dx_query_mob;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700503
504 dx_query_mob = vmw_context_get_dx_query_mob(ctx);
505 if (dx_query_mob)
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200506 ret = vmw_validation_add_bo(sw_context->ctx,
507 dx_query_mob, true, false);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700508 }
509
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100510 mutex_unlock(&dev_priv->binding_mutex);
511 return ret;
512}
513
514/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000515 * vmw_resource_relocation_add - Add a relocation to the relocation list
516 *
Lee Jones7450bf72021-01-15 18:12:36 +0000517 * @sw_context: Pointer to the software context.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000518 * @res: The resource.
Deepak Rawat680360a2019-02-13 13:20:42 -0800519 * @offset: Offset into the command buffer currently being parsed where the id
520 * that needs fixup is located. Granularity is one byte.
Thomas Hellstroma1944032016-10-10 11:06:45 -0700521 * @rel_type: Relocation type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000522 */
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200523static int vmw_resource_relocation_add(struct vmw_sw_context *sw_context,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000524 const struct vmw_resource *res,
Thomas Hellstroma1944032016-10-10 11:06:45 -0700525 unsigned long offset,
526 enum vmw_resource_relocation_type
527 rel_type)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000528{
529 struct vmw_resource_relocation *rel;
530
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200531 rel = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*rel));
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530532 if (unlikely(!rel)) {
Deepak Rawat5724f892019-02-11 11:46:27 -0800533 VMW_DEBUG_USER("Failed to allocate a resource relocation.\n");
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000534 return -ENOMEM;
535 }
536
537 rel->res = res;
538 rel->offset = offset;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700539 rel->rel_type = rel_type;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200540 list_add_tail(&rel->head, &sw_context->res_relocations);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000541
542 return 0;
543}
544
545/**
546 * vmw_resource_relocations_free - Free all relocations on a list
547 *
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200548 * @list: Pointer to the head of the relocation list
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000549 */
550static void vmw_resource_relocations_free(struct list_head *list)
551{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200552 /* Memory is validation context memory, so no need to free it */
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200553 INIT_LIST_HEAD(list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000554}
555
556/**
557 * vmw_resource_relocations_apply - Apply all relocations on a list
558 *
Deepak Rawat680360a2019-02-13 13:20:42 -0800559 * @cb: Pointer to the start of the command buffer bein patch. This need not be
560 * the same buffer as the one being parsed when the relocation list was built,
561 * but the contents must be the same modulo the resource ids.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000562 * @list: Pointer to the head of the relocation list.
563 */
564static void vmw_resource_relocations_apply(uint32_t *cb,
565 struct list_head *list)
566{
567 struct vmw_resource_relocation *rel;
568
Thomas Hellstroma1944032016-10-10 11:06:45 -0700569 /* Validate the struct vmw_resource_relocation member size */
570 BUILD_BUG_ON(SVGA_CB_MAX_SIZE >= (1 << 29));
571 BUILD_BUG_ON(vmw_res_rel_max >= (1 << 3));
572
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100573 list_for_each_entry(rel, list, head) {
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700574 u32 *addr = (u32 *)((unsigned long) cb + rel->offset);
Thomas Hellstroma1944032016-10-10 11:06:45 -0700575 switch (rel->rel_type) {
576 case vmw_res_rel_normal:
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700577 *addr = rel->res->id;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700578 break;
579 case vmw_res_rel_nop:
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700580 *addr = SVGA_3D_CMD_NOP;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700581 break;
582 default:
583 if (rel->res->id == -1)
584 *addr = SVGA_3D_CMD_NOP;
585 break;
586 }
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100587 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000588}
589
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000590static int vmw_cmd_invalid(struct vmw_private *dev_priv,
591 struct vmw_sw_context *sw_context,
592 SVGA3dCmdHeader *header)
593{
Sinclair Yehfcfffdd2017-07-17 23:28:36 -0700594 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000595}
596
597static int vmw_cmd_ok(struct vmw_private *dev_priv,
598 struct vmw_sw_context *sw_context,
599 SVGA3dCmdHeader *header)
600{
601 return 0;
602}
603
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200604/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800605 * vmw_resources_reserve - Reserve all resources on the sw_context's resource
606 * list.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000607 *
608 * @sw_context: Pointer to the software context.
609 *
Deepak Rawat680360a2019-02-13 13:20:42 -0800610 * Note that since vmware's command submission currently is protected by the
611 * cmdbuf mutex, no fancy deadlock avoidance is required for resources, since
612 * only a single thread at once will attempt this.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000613 */
614static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
615{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200616 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000617
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200618 ret = vmw_validation_res_reserve(sw_context->ctx, true);
619 if (ret)
620 return ret;
Charmaine Lee2f633e52015-08-10 10:45:11 -0700621
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700622 if (sw_context->dx_query_mob) {
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200623 struct vmw_buffer_object *expected_dx_query_mob;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700624
625 expected_dx_query_mob =
626 vmw_context_get_dx_query_mob(sw_context->dx_query_ctx);
627 if (expected_dx_query_mob &&
628 expected_dx_query_mob != sw_context->dx_query_mob) {
629 ret = -EINVAL;
630 }
631 }
632
633 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000634}
635
636/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800637 * vmw_cmd_res_check - Check that a resource is present and if so, put it on the
638 * resource validate list unless it's already there.
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100639 *
640 * @dev_priv: Pointer to a device private structure.
641 * @sw_context: Pointer to the software context.
642 * @res_type: Resource type.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100643 * @dirty: Whether to change dirty status.
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100644 * @converter: User-space visisble type specific information.
Deepak Rawat680360a2019-02-13 13:20:42 -0800645 * @id_loc: Pointer to the location in the command buffer currently being parsed
646 * from where the user-space resource id handle is located.
Lee Jones7450bf72021-01-15 18:12:36 +0000647 * @p_res: Pointer to pointer to resource validalidation node. Populated on
Deepak Rawat680360a2019-02-13 13:20:42 -0800648 * exit.
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100649 */
650static int
651vmw_cmd_res_check(struct vmw_private *dev_priv,
652 struct vmw_sw_context *sw_context,
653 enum vmw_res_type res_type,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100654 u32 dirty,
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100655 const struct vmw_user_resource_conv *converter,
656 uint32_t *id_loc,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200657 struct vmw_resource **p_res)
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100658{
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200659 struct vmw_res_cache_entry *rcache = &sw_context->res_cache[res_type];
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200660 struct vmw_resource *res;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200661 int ret;
662
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200663 if (p_res)
664 *p_res = NULL;
665
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200666 if (*id_loc == SVGA3D_INVALID_ID) {
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200667 if (res_type == vmw_res_context) {
Deepak Rawat5724f892019-02-11 11:46:27 -0800668 VMW_DEBUG_USER("Illegal context invalid id.\n");
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200669 return -EINVAL;
670 }
671 return 0;
672 }
673
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200674 if (likely(rcache->valid_handle && *id_loc == rcache->handle)) {
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200675 res = rcache->res;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100676 if (dirty)
677 vmw_validation_res_set_dirty(sw_context->ctx,
678 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200679 } else {
680 unsigned int size = vmw_execbuf_res_size(dev_priv, res_type);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200681
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200682 ret = vmw_validation_preload_res(sw_context->ctx, size);
683 if (ret)
684 return ret;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200685
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200686 res = vmw_user_resource_noref_lookup_handle
687 (dev_priv, sw_context->fp->tfile, *id_loc, converter);
Chengguang Xu4efa6662019-03-01 10:14:06 -0800688 if (IS_ERR(res)) {
Deepak Rawat5724f892019-02-11 11:46:27 -0800689 VMW_DEBUG_USER("Could not find/use resource 0x%08x.\n",
690 (unsigned int) *id_loc);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200691 return PTR_ERR(res);
692 }
693
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100694 ret = vmw_execbuf_res_noref_val_add(sw_context, res, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200695 if (unlikely(ret != 0))
696 return ret;
697
698 if (rcache->valid && rcache->res == res) {
699 rcache->valid_handle = true;
700 rcache->handle = *id_loc;
701 }
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200702 }
703
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200704 ret = vmw_resource_relocation_add(sw_context, res,
705 vmw_ptr_diff(sw_context->buf_start,
706 id_loc),
707 vmw_res_rel_normal);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200708 if (p_res)
709 *p_res = res;
710
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200711 return 0;
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100712}
713
714/**
Zack Rusin2cd80db2021-05-05 15:10:07 -0400715 * vmw_rebind_all_dx_query - Rebind DX query associated with the context
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700716 *
717 * @ctx_res: context the query belongs to
718 *
719 * This function assumes binding_mutex is held.
720 */
721static int vmw_rebind_all_dx_query(struct vmw_resource *ctx_res)
722{
723 struct vmw_private *dev_priv = ctx_res->dev_priv;
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200724 struct vmw_buffer_object *dx_query_mob;
Deepak Rawatd01316d2019-02-08 15:50:40 -0800725 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindAllQuery);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700726
727 dx_query_mob = vmw_context_get_dx_query_mob(ctx_res);
728
729 if (!dx_query_mob || dx_query_mob->dx_query_ctx)
730 return 0;
731
Zack Rusin8426ed92020-11-18 12:54:19 -0500732 cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), ctx_res->id);
Deepak Rawatb2898402019-02-11 14:59:57 -0800733 if (cmd == NULL)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700734 return -ENOMEM;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700735
736 cmd->header.id = SVGA_3D_CMD_DX_BIND_ALL_QUERY;
737 cmd->header.size = sizeof(cmd->body);
738 cmd->body.cid = ctx_res->id;
Christian Königd3116752021-04-12 15:11:47 +0200739 cmd->body.mobid = dx_query_mob->base.resource->start;
Zack Rusin8426ed92020-11-18 12:54:19 -0500740 vmw_cmd_commit(dev_priv, sizeof(*cmd));
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700741
742 vmw_context_bind_dx_query(ctx_res, dx_query_mob);
743
744 return 0;
745}
746
747/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800748 * vmw_rebind_contexts - Rebind all resources previously bound to referenced
749 * contexts.
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100750 *
751 * @sw_context: Pointer to the software context.
752 *
753 * Rebind context binding points that have been scrubbed because of eviction.
754 */
755static int vmw_rebind_contexts(struct vmw_sw_context *sw_context)
756{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200757 struct vmw_ctx_validation_info *val;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100758 int ret;
759
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200760 list_for_each_entry(val, &sw_context->ctx_list, head) {
761 ret = vmw_binding_rebind_all(val->cur);
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100762 if (unlikely(ret != 0)) {
763 if (ret != -ERESTARTSYS)
Deepak Rawat5724f892019-02-11 11:46:27 -0800764 VMW_DEBUG_USER("Failed to rebind context.\n");
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100765 return ret;
766 }
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700767
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200768 ret = vmw_rebind_all_dx_query(val->ctx);
Deepak Rawatb2898402019-02-11 14:59:57 -0800769 if (ret != 0) {
770 VMW_DEBUG_USER("Failed to rebind queries.\n");
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700771 return ret;
Deepak Rawatb2898402019-02-11 14:59:57 -0800772 }
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100773 }
774
775 return 0;
776}
777
778/**
Deepak Rawat680360a2019-02-13 13:20:42 -0800779 * vmw_view_bindings_add - Add an array of view bindings to a context binding
780 * state tracker.
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700781 *
782 * @sw_context: The execbuf state used for this command.
783 * @view_type: View type for the bindings.
784 * @binding_type: Binding type for the bindings.
785 * @shader_slot: The shader slot to user for the bindings.
786 * @view_ids: Array of view ids to be bound.
787 * @num_views: Number of view ids in @view_ids.
788 * @first_slot: The binding slot to be used for the first view id in @view_ids.
789 */
790static int vmw_view_bindings_add(struct vmw_sw_context *sw_context,
791 enum vmw_view_type view_type,
792 enum vmw_ctx_binding_type binding_type,
793 uint32 shader_slot,
794 uint32 view_ids[], u32 num_views,
795 u32 first_slot)
796{
Deepak Rawat6f74fd92019-02-08 12:53:57 -0800797 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700798 u32 i;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700799
Deepak Rawat6f74fd92019-02-08 12:53:57 -0800800 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700801 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700802
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700803 for (i = 0; i < num_views; ++i) {
804 struct vmw_ctx_bindinfo_view binding;
805 struct vmw_resource *view = NULL;
806
807 if (view_ids[i] != SVGA3D_INVALID_ID) {
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200808 view = vmw_view_id_val_add(sw_context, view_type,
809 view_ids[i]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700810 if (IS_ERR(view)) {
Deepak Rawat5724f892019-02-11 11:46:27 -0800811 VMW_DEBUG_USER("View not found.\n");
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700812 return PTR_ERR(view);
813 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700814 }
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200815 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700816 binding.bi.res = view;
817 binding.bi.bt = binding_type;
818 binding.shader_slot = shader_slot;
819 binding.slot = first_slot + i;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200820 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700821 shader_slot, binding.slot);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700822 }
823
824 return 0;
825}
826
827/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000828 * vmw_cmd_cid_check - Check a command header for valid context information.
829 *
830 * @dev_priv: Pointer to a device private structure.
831 * @sw_context: Pointer to the software context.
832 * @header: A command header with an embedded user-space context handle.
833 *
834 * Convenience function: Call vmw_cmd_res_check with the user-space context
835 * handle embedded in @header.
836 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000837static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
838 struct vmw_sw_context *sw_context,
839 SVGA3dCmdHeader *header)
840{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800841 VMW_DECLARE_CMD_VAR(*cmd, uint32_t) =
842 container_of(header, typeof(*cmd), header);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000843
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000844 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100845 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -0800846 &cmd->body, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000847}
848
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200849/**
850 * vmw_execbuf_info_from_res - Get the private validation metadata for a
851 * recently validated resource
Deepak Rawat680360a2019-02-13 13:20:42 -0800852 *
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200853 * @sw_context: Pointer to the command submission context
854 * @res: The resource
855 *
856 * The resource pointed to by @res needs to be present in the command submission
857 * context's resource cache and hence the last resource of that type to be
858 * processed by the validation code.
859 *
Deepak Rawat680360a2019-02-13 13:20:42 -0800860 * Return: a pointer to the private metadata of the resource, or NULL if it
861 * wasn't found
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200862 */
863static struct vmw_ctx_validation_info *
864vmw_execbuf_info_from_res(struct vmw_sw_context *sw_context,
865 struct vmw_resource *res)
866{
867 struct vmw_res_cache_entry *rcache =
868 &sw_context->res_cache[vmw_res_type(res)];
869
870 if (rcache->valid && rcache->res == res)
871 return rcache->private;
872
873 WARN_ON_ONCE(true);
874 return NULL;
875}
876
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000877static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
878 struct vmw_sw_context *sw_context,
879 SVGA3dCmdHeader *header)
880{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800881 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetRenderTarget);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200882 struct vmw_resource *ctx;
883 struct vmw_resource *res;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000884 int ret;
885
Deepak Rawatd01316d2019-02-08 15:50:40 -0800886 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700887
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700888 if (cmd->body.type >= SVGA3D_RT_MAX) {
Deepak Rawat5724f892019-02-11 11:46:27 -0800889 VMW_DEBUG_USER("Illegal render target type %u.\n",
890 (unsigned int) cmd->body.type);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700891 return -EINVAL;
892 }
893
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700894 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100895 VMW_RES_DIRTY_SET, user_context_converter,
896 &cmd->body.cid, &ctx);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000897 if (unlikely(ret != 0))
898 return ret;
899
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000900 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100901 VMW_RES_DIRTY_SET, user_surface_converter,
902 &cmd->body.target.sid, &res);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200903 if (unlikely(ret))
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700904 return ret;
905
906 if (dev_priv->has_mob) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700907 struct vmw_ctx_bindinfo_view binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200908 struct vmw_ctx_validation_info *node;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700909
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200910 node = vmw_execbuf_info_from_res(sw_context, ctx);
911 if (!node)
912 return -EINVAL;
913
914 binding.bi.ctx = ctx;
915 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700916 binding.bi.bt = vmw_ctx_binding_rt;
917 binding.slot = cmd->body.type;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200918 vmw_binding_add(node->staged, &binding.bi, 0, binding.slot);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700919 }
920
921 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000922}
923
924static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
925 struct vmw_sw_context *sw_context,
926 SVGA3dCmdHeader *header)
927{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800928 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceCopy);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000929 int ret;
930
Deepak Rawatd01316d2019-02-08 15:50:40 -0800931 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromc9146cd2015-03-02 23:45:04 -0800932
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700933 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100934 VMW_RES_DIRTY_NONE, user_surface_converter,
935 &cmd->body.src.sid, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700936 if (ret)
937 return ret;
Thomas Hellstromc9146cd2015-03-02 23:45:04 -0800938
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000939 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100940 VMW_RES_DIRTY_SET, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000941 &cmd->body.dest.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000942}
943
Neha Bhende0fca749e2015-08-10 10:51:07 -0700944static int vmw_cmd_buffer_copy_check(struct vmw_private *dev_priv,
Deepak Rawat680360a2019-02-13 13:20:42 -0800945 struct vmw_sw_context *sw_context,
946 SVGA3dCmdHeader *header)
Neha Bhende0fca749e2015-08-10 10:51:07 -0700947{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800948 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBufferCopy);
Neha Bhende0fca749e2015-08-10 10:51:07 -0700949 int ret;
950
951 cmd = container_of(header, typeof(*cmd), header);
952 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100953 VMW_RES_DIRTY_NONE, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700954 &cmd->body.src, NULL);
955 if (ret != 0)
956 return ret;
957
958 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100959 VMW_RES_DIRTY_SET, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700960 &cmd->body.dest, NULL);
961}
962
963static int vmw_cmd_pred_copy_check(struct vmw_private *dev_priv,
964 struct vmw_sw_context *sw_context,
965 SVGA3dCmdHeader *header)
966{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800967 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXPredCopyRegion);
Neha Bhende0fca749e2015-08-10 10:51:07 -0700968 int ret;
969
970 cmd = container_of(header, typeof(*cmd), header);
971 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100972 VMW_RES_DIRTY_NONE, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700973 &cmd->body.srcSid, NULL);
974 if (ret != 0)
975 return ret;
976
977 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100978 VMW_RES_DIRTY_SET, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700979 &cmd->body.dstSid, NULL);
980}
981
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000982static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
983 struct vmw_sw_context *sw_context,
984 SVGA3dCmdHeader *header)
985{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800986 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceStretchBlt);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000987 int ret;
988
Deepak Rawatd01316d2019-02-08 15:50:40 -0800989 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000990 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100991 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000992 &cmd->body.src.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000993 if (unlikely(ret != 0))
994 return ret;
Deepak Rawat680360a2019-02-13 13:20:42 -0800995
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000996 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100997 VMW_RES_DIRTY_SET, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000998 &cmd->body.dest.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000999}
1000
1001static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
1002 struct vmw_sw_context *sw_context,
1003 SVGA3dCmdHeader *header)
1004{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001005 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBlitSurfaceToScreen) =
1006 container_of(header, typeof(*cmd), header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02001007
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001008 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001009 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001010 &cmd->body.srcImage.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001011}
1012
1013static int vmw_cmd_present_check(struct vmw_private *dev_priv,
1014 struct vmw_sw_context *sw_context,
1015 SVGA3dCmdHeader *header)
1016{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001017 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdPresent) =
1018 container_of(header, typeof(*cmd), header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02001019
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001020 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001021 VMW_RES_DIRTY_NONE, user_surface_converter,
1022 &cmd->body.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001023}
1024
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001025/**
1026 * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
1027 *
1028 * @dev_priv: The device private structure.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001029 * @new_query_bo: The new buffer holding query results.
1030 * @sw_context: The software context used for this command submission.
1031 *
Deepak Rawat680360a2019-02-13 13:20:42 -08001032 * This function checks whether @new_query_bo is suitable for holding query
1033 * results, and if another buffer currently is pinned for query results. If so,
1034 * the function prepares the state of @sw_context for switching pinned buffers
1035 * after successful submission of the current command batch.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001036 */
1037static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001038 struct vmw_buffer_object *new_query_bo,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001039 struct vmw_sw_context *sw_context)
1040{
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001041 struct vmw_res_cache_entry *ctx_entry =
1042 &sw_context->res_cache[vmw_res_context];
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001043 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001044
1045 BUG_ON(!ctx_entry->valid);
1046 sw_context->last_query_ctx = ctx_entry->res;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001047
1048 if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
1049
Christian Königd3116752021-04-12 15:11:47 +02001050 if (unlikely(new_query_bo->base.resource->num_pages > 4)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001051 VMW_DEBUG_USER("Query buffer too large.\n");
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001052 return -EINVAL;
1053 }
1054
1055 if (unlikely(sw_context->cur_query_bo != NULL)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001056 sw_context->needs_post_query_barrier = true;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001057 ret = vmw_validation_add_bo(sw_context->ctx,
1058 sw_context->cur_query_bo,
1059 dev_priv->has_mob, false);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001060 if (unlikely(ret != 0))
1061 return ret;
1062 }
1063 sw_context->cur_query_bo = new_query_bo;
1064
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001065 ret = vmw_validation_add_bo(sw_context->ctx,
1066 dev_priv->dummy_query_bo,
1067 dev_priv->has_mob, false);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001068 if (unlikely(ret != 0))
1069 return ret;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001070 }
1071
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001072 return 0;
1073}
1074
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001075/**
1076 * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
1077 *
1078 * @dev_priv: The device private structure.
1079 * @sw_context: The software context used for this command submission batch.
1080 *
1081 * This function will check if we're switching query buffers, and will then,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001082 * issue a dummy occlusion query wait used as a query barrier. When the fence
Deepak Rawat680360a2019-02-13 13:20:42 -08001083 * object following that query wait has signaled, we are sure that all preceding
1084 * queries have finished, and the old query buffer can be unpinned. However,
1085 * since both the new query buffer and the old one are fenced with that fence,
1086 * we can do an asynchronus unpin now, and be sure that the old query buffer
1087 * won't be moved until the fence has signaled.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001088 *
1089 * As mentioned above, both the new - and old query buffers need to be fenced
1090 * using a sequence emitted *after* calling this function.
1091 */
1092static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
1093 struct vmw_sw_context *sw_context)
1094{
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001095 /*
1096 * The validate list should still hold references to all
1097 * contexts here.
1098 */
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001099 if (sw_context->needs_post_query_barrier) {
1100 struct vmw_res_cache_entry *ctx_entry =
1101 &sw_context->res_cache[vmw_res_context];
1102 struct vmw_resource *ctx;
1103 int ret;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001104
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001105 BUG_ON(!ctx_entry->valid);
1106 ctx = ctx_entry->res;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001107
Zack Rusin8426ed92020-11-18 12:54:19 -05001108 ret = vmw_cmd_emit_dummy_query(dev_priv, ctx->id);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001109
1110 if (unlikely(ret != 0))
Deepak Rawat5724f892019-02-11 11:46:27 -08001111 VMW_DEBUG_USER("Out of fifo space for dummy query.\n");
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001112 }
1113
1114 if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
1115 if (dev_priv->pinned_bo) {
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001116 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001117 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001118 }
1119
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001120 if (!sw_context->needs_post_query_barrier) {
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001121 vmw_bo_pin_reserved(sw_context->cur_query_bo, true);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001122
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001123 /*
1124 * We pin also the dummy_query_bo buffer so that we
Deepak Rawat680360a2019-02-13 13:20:42 -08001125 * don't need to validate it when emitting dummy queries
1126 * in context destroy paths.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001127 */
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001128 if (!dev_priv->dummy_query_bo_pinned) {
1129 vmw_bo_pin_reserved(dev_priv->dummy_query_bo,
1130 true);
1131 dev_priv->dummy_query_bo_pinned = true;
1132 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001133
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001134 BUG_ON(sw_context->last_query_ctx == NULL);
1135 dev_priv->query_cid = sw_context->last_query_ctx->id;
1136 dev_priv->query_cid_valid = true;
1137 dev_priv->pinned_bo =
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001138 vmw_bo_reference(sw_context->cur_query_bo);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001139 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001140 }
1141}
1142
1143/**
Zack Rusin2cd80db2021-05-05 15:10:07 -04001144 * vmw_translate_mob_ptr - Prepare to translate a user-space buffer handle
Deepak Rawat680360a2019-02-13 13:20:42 -08001145 * to a MOB id.
Thomas Hellstromddcda242012-11-21 11:26:55 +01001146 *
1147 * @dev_priv: Pointer to a device private structure.
1148 * @sw_context: The software context used for this command batch validation.
1149 * @id: Pointer to the user-space handle to be translated.
Deepak Rawat680360a2019-02-13 13:20:42 -08001150 * @vmw_bo_p: Points to a location that, on successful return will carry a
1151 * non-reference-counted pointer to the buffer object identified by the
Thomas Hellstromddcda242012-11-21 11:26:55 +01001152 * user-space handle in @id.
1153 *
1154 * This function saves information needed to translate a user-space buffer
1155 * handle to a MOB id. The translation does not take place immediately, but
Deepak Rawat680360a2019-02-13 13:20:42 -08001156 * during a call to vmw_apply_relocations().
1157 *
1158 * This function builds a relocation list and a list of buffers to validate. The
1159 * former needs to be freed using either vmw_apply_relocations() or
1160 * vmw_free_relocations(). The latter needs to be freed using
1161 * vmw_clear_validations.
Thomas Hellstromddcda242012-11-21 11:26:55 +01001162 */
1163static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
1164 struct vmw_sw_context *sw_context,
1165 SVGAMobId *id,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001166 struct vmw_buffer_object **vmw_bo_p)
Thomas Hellstromddcda242012-11-21 11:26:55 +01001167{
Thomas Hellstromb139d432018-09-26 16:27:54 +02001168 struct vmw_buffer_object *vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001169 uint32_t handle = *id;
1170 struct vmw_relocation *reloc;
1171 int ret;
1172
Thomas Hellstromb139d432018-09-26 16:27:54 +02001173 vmw_validation_preload_bo(sw_context->ctx);
1174 vmw_bo = vmw_user_bo_noref_lookup(sw_context->fp->tfile, handle);
1175 if (IS_ERR(vmw_bo)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001176 VMW_DEBUG_USER("Could not find or use MOB buffer.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001177 return PTR_ERR(vmw_bo);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001178 }
Thomas Hellstromddcda242012-11-21 11:26:55 +01001179
Thomas Hellstromb139d432018-09-26 16:27:54 +02001180 ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo, true, false);
1181 vmw_user_bo_noref_release();
1182 if (unlikely(ret != 0))
1183 return ret;
1184
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001185 reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1186 if (!reloc)
Thomas Hellstromb139d432018-09-26 16:27:54 +02001187 return -ENOMEM;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001188
Thomas Hellstromddcda242012-11-21 11:26:55 +01001189 reloc->mob_loc = id;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001190 reloc->vbo = vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001191
Thomas Hellstromddcda242012-11-21 11:26:55 +01001192 *vmw_bo_p = vmw_bo;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001193 list_add_tail(&reloc->head, &sw_context->bo_relocations);
1194
Thomas Hellstromddcda242012-11-21 11:26:55 +01001195 return 0;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001196}
1197
1198/**
Zack Rusin2cd80db2021-05-05 15:10:07 -04001199 * vmw_translate_guest_ptr - Prepare to translate a user-space buffer handle
Deepak Rawat680360a2019-02-13 13:20:42 -08001200 * to a valid SVGAGuestPtr
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001201 *
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001202 * @dev_priv: Pointer to a device private structure.
1203 * @sw_context: The software context used for this command batch validation.
1204 * @ptr: Pointer to the user-space handle to be translated.
Deepak Rawat680360a2019-02-13 13:20:42 -08001205 * @vmw_bo_p: Points to a location that, on successful return will carry a
1206 * non-reference-counted pointer to the DMA buffer identified by the user-space
1207 * handle in @id.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001208 *
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001209 * This function saves information needed to translate a user-space buffer
1210 * handle to a valid SVGAGuestPtr. The translation does not take place
1211 * immediately, but during a call to vmw_apply_relocations().
Deepak Rawat680360a2019-02-13 13:20:42 -08001212 *
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001213 * This function builds a relocation list and a list of buffers to validate.
1214 * The former needs to be freed using either vmw_apply_relocations() or
1215 * vmw_free_relocations(). The latter needs to be freed using
1216 * vmw_clear_validations.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001217 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001218static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
1219 struct vmw_sw_context *sw_context,
1220 SVGAGuestPtr *ptr,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001221 struct vmw_buffer_object **vmw_bo_p)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001222{
Thomas Hellstromb139d432018-09-26 16:27:54 +02001223 struct vmw_buffer_object *vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001224 uint32_t handle = ptr->gmrId;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001225 struct vmw_relocation *reloc;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001226 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001227
Thomas Hellstromb139d432018-09-26 16:27:54 +02001228 vmw_validation_preload_bo(sw_context->ctx);
1229 vmw_bo = vmw_user_bo_noref_lookup(sw_context->fp->tfile, handle);
1230 if (IS_ERR(vmw_bo)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001231 VMW_DEBUG_USER("Could not find or use GMR region.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001232 return PTR_ERR(vmw_bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001233 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001234
Thomas Hellstromb139d432018-09-26 16:27:54 +02001235 ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo, false, false);
1236 vmw_user_bo_noref_release();
1237 if (unlikely(ret != 0))
1238 return ret;
1239
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001240 reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1241 if (!reloc)
Thomas Hellstromb139d432018-09-26 16:27:54 +02001242 return -ENOMEM;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001243
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001244 reloc->location = ptr;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001245 reloc->vbo = vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001246 *vmw_bo_p = vmw_bo;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001247 list_add_tail(&reloc->head, &sw_context->bo_relocations);
1248
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001249 return 0;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001250}
1251
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001252/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001253 * vmw_cmd_dx_define_query - validate SVGA_3D_CMD_DX_DEFINE_QUERY command.
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001254 *
1255 * @dev_priv: Pointer to a device private struct.
1256 * @sw_context: The software context used for this command submission.
1257 * @header: Pointer to the command header in the command stream.
1258 *
1259 * This function adds the new query into the query COTABLE
1260 */
1261static int vmw_cmd_dx_define_query(struct vmw_private *dev_priv,
1262 struct vmw_sw_context *sw_context,
1263 SVGA3dCmdHeader *header)
1264{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001265 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDefineQuery);
Deepak Rawat6f74fd92019-02-08 12:53:57 -08001266 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001267 struct vmw_resource *cotable_res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001268 int ret;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001269
Deepak Rawat6f74fd92019-02-08 12:53:57 -08001270 if (!ctx_node)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001271 return -EINVAL;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001272
Deepak Rawatd01316d2019-02-08 15:50:40 -08001273 cmd = container_of(header, typeof(*cmd), header);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001274
Deepak Rawatd01316d2019-02-08 15:50:40 -08001275 if (cmd->body.type < SVGA3D_QUERYTYPE_MIN ||
1276 cmd->body.type >= SVGA3D_QUERYTYPE_MAX)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001277 return -EINVAL;
1278
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001279 cotable_res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXQUERY);
Deepak Rawatd01316d2019-02-08 15:50:40 -08001280 ret = vmw_cotable_notify(cotable_res, cmd->body.queryId);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001281
1282 return ret;
1283}
1284
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001285/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001286 * vmw_cmd_dx_bind_query - validate SVGA_3D_CMD_DX_BIND_QUERY command.
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001287 *
1288 * @dev_priv: Pointer to a device private struct.
1289 * @sw_context: The software context used for this command submission.
1290 * @header: Pointer to the command header in the command stream.
1291 *
Deepak Rawat680360a2019-02-13 13:20:42 -08001292 * The query bind operation will eventually associate the query ID with its
1293 * backing MOB. In this function, we take the user mode MOB ID and use
1294 * vmw_translate_mob_ptr() to translate it to its kernel mode equivalent.
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001295 */
1296static int vmw_cmd_dx_bind_query(struct vmw_private *dev_priv,
1297 struct vmw_sw_context *sw_context,
1298 SVGA3dCmdHeader *header)
1299{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001300 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindQuery);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001301 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001302 int ret;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001303
Deepak Rawatd01316d2019-02-08 15:50:40 -08001304 cmd = container_of(header, typeof(*cmd), header);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001305
1306 /*
1307 * Look up the buffer pointed to by q.mobid, put it on the relocation
1308 * list so its kernel mode MOB ID can be filled in later
1309 */
Deepak Rawatd01316d2019-02-08 15:50:40 -08001310 ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->body.mobid,
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001311 &vmw_bo);
1312
1313 if (ret != 0)
1314 return ret;
1315
1316 sw_context->dx_query_mob = vmw_bo;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001317 sw_context->dx_query_ctx = sw_context->dx_ctx_node->ctx;
Thomas Hellstromb139d432018-09-26 16:27:54 +02001318 return 0;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001319}
1320
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001321/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001322 * vmw_cmd_begin_gb_query - validate SVGA_3D_CMD_BEGIN_GB_QUERY command.
Thomas Hellstromddcda242012-11-21 11:26:55 +01001323 *
1324 * @dev_priv: Pointer to a device private struct.
1325 * @sw_context: The software context used for this command submission.
1326 * @header: Pointer to the command header in the command stream.
1327 */
1328static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
1329 struct vmw_sw_context *sw_context,
1330 SVGA3dCmdHeader *header)
1331{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001332 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBeginGBQuery) =
1333 container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001334
1335 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001336 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001337 &cmd->body.cid, NULL);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001338}
1339
1340/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001341 * vmw_cmd_begin_query - validate SVGA_3D_CMD_BEGIN_QUERY command.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001342 *
1343 * @dev_priv: Pointer to a device private struct.
1344 * @sw_context: The software context used for this command submission.
1345 * @header: Pointer to the command header in the command stream.
1346 */
1347static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
1348 struct vmw_sw_context *sw_context,
1349 SVGA3dCmdHeader *header)
1350{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001351 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBeginQuery) =
1352 container_of(header, typeof(*cmd), header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001353
Thomas Hellstromddcda242012-11-21 11:26:55 +01001354 if (unlikely(dev_priv->has_mob)) {
Deepak Rawatd01316d2019-02-08 15:50:40 -08001355 VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdBeginGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001356
1357 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1358
1359 gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
1360 gb_cmd.header.size = cmd->header.size;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001361 gb_cmd.body.cid = cmd->body.cid;
1362 gb_cmd.body.type = cmd->body.type;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001363
1364 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1365 return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
1366 }
1367
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001368 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001369 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001370 &cmd->body.cid, NULL);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001371}
1372
1373/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001374 * vmw_cmd_end_gb_query - validate SVGA_3D_CMD_END_GB_QUERY command.
Thomas Hellstromddcda242012-11-21 11:26:55 +01001375 *
1376 * @dev_priv: Pointer to a device private struct.
1377 * @sw_context: The software context used for this command submission.
1378 * @header: Pointer to the command header in the command stream.
1379 */
1380static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
1381 struct vmw_sw_context *sw_context,
1382 SVGA3dCmdHeader *header)
1383{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001384 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001385 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdEndGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001386 int ret;
1387
Deepak Rawatd01316d2019-02-08 15:50:40 -08001388 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001389 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1390 if (unlikely(ret != 0))
1391 return ret;
1392
Deepak Rawat680360a2019-02-13 13:20:42 -08001393 ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->body.mobid,
Thomas Hellstromddcda242012-11-21 11:26:55 +01001394 &vmw_bo);
1395 if (unlikely(ret != 0))
1396 return ret;
1397
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001398 ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001399
Thomas Hellstromddcda242012-11-21 11:26:55 +01001400 return ret;
1401}
1402
1403/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001404 * vmw_cmd_end_query - validate SVGA_3D_CMD_END_QUERY command.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001405 *
1406 * @dev_priv: Pointer to a device private struct.
1407 * @sw_context: The software context used for this command submission.
1408 * @header: Pointer to the command header in the command stream.
1409 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001410static int vmw_cmd_end_query(struct vmw_private *dev_priv,
1411 struct vmw_sw_context *sw_context,
1412 SVGA3dCmdHeader *header)
1413{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001414 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001415 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdEndQuery);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001416 int ret;
1417
Deepak Rawatd01316d2019-02-08 15:50:40 -08001418 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001419 if (dev_priv->has_mob) {
Deepak Rawatd01316d2019-02-08 15:50:40 -08001420 VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdEndGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001421
1422 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1423
1424 gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
1425 gb_cmd.header.size = cmd->header.size;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001426 gb_cmd.body.cid = cmd->body.cid;
1427 gb_cmd.body.type = cmd->body.type;
1428 gb_cmd.body.mobid = cmd->body.guestResult.gmrId;
1429 gb_cmd.body.offset = cmd->body.guestResult.offset;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001430
1431 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1432 return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
1433 }
1434
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001435 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1436 if (unlikely(ret != 0))
1437 return ret;
1438
1439 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
Deepak Rawat680360a2019-02-13 13:20:42 -08001440 &cmd->body.guestResult, &vmw_bo);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001441 if (unlikely(ret != 0))
1442 return ret;
1443
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001444 ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001445
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001446 return ret;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001447}
1448
Thomas Hellstromddcda242012-11-21 11:26:55 +01001449/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001450 * vmw_cmd_wait_gb_query - validate SVGA_3D_CMD_WAIT_GB_QUERY command.
Thomas Hellstromddcda242012-11-21 11:26:55 +01001451 *
1452 * @dev_priv: Pointer to a device private struct.
1453 * @sw_context: The software context used for this command submission.
1454 * @header: Pointer to the command header in the command stream.
1455 */
1456static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
1457 struct vmw_sw_context *sw_context,
1458 SVGA3dCmdHeader *header)
1459{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001460 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001461 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdWaitForGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001462 int ret;
1463
Deepak Rawatd01316d2019-02-08 15:50:40 -08001464 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001465 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1466 if (unlikely(ret != 0))
1467 return ret;
1468
Deepak Rawat680360a2019-02-13 13:20:42 -08001469 ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->body.mobid,
Thomas Hellstromddcda242012-11-21 11:26:55 +01001470 &vmw_bo);
1471 if (unlikely(ret != 0))
1472 return ret;
1473
Thomas Hellstromddcda242012-11-21 11:26:55 +01001474 return 0;
1475}
1476
1477/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001478 * vmw_cmd_wait_query - validate SVGA_3D_CMD_WAIT_QUERY command.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001479 *
1480 * @dev_priv: Pointer to a device private struct.
1481 * @sw_context: The software context used for this command submission.
1482 * @header: Pointer to the command header in the command stream.
1483 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001484static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
1485 struct vmw_sw_context *sw_context,
1486 SVGA3dCmdHeader *header)
1487{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001488 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001489 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdWaitForQuery);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001490 int ret;
1491
Deepak Rawatd01316d2019-02-08 15:50:40 -08001492 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001493 if (dev_priv->has_mob) {
Deepak Rawatd01316d2019-02-08 15:50:40 -08001494 VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdWaitForGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001495
1496 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1497
1498 gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
1499 gb_cmd.header.size = cmd->header.size;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001500 gb_cmd.body.cid = cmd->body.cid;
1501 gb_cmd.body.type = cmd->body.type;
1502 gb_cmd.body.mobid = cmd->body.guestResult.gmrId;
1503 gb_cmd.body.offset = cmd->body.guestResult.offset;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001504
1505 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1506 return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
1507 }
1508
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001509 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1510 if (unlikely(ret != 0))
1511 return ret;
1512
1513 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
Deepak Rawat680360a2019-02-13 13:20:42 -08001514 &cmd->body.guestResult, &vmw_bo);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001515 if (unlikely(ret != 0))
1516 return ret;
1517
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001518 return 0;
1519}
1520
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001521static int vmw_cmd_dma(struct vmw_private *dev_priv,
1522 struct vmw_sw_context *sw_context,
1523 SVGA3dCmdHeader *header)
1524{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001525 struct vmw_buffer_object *vmw_bo = NULL;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001526 struct vmw_surface *srf = NULL;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001527 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceDMA);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001528 int ret;
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001529 SVGA3dCmdSurfaceDMASuffix *suffix;
1530 uint32_t bo_size;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001531 bool dirty;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001532
Deepak Rawatd01316d2019-02-08 15:50:40 -08001533 cmd = container_of(header, typeof(*cmd), header);
1534 suffix = (SVGA3dCmdSurfaceDMASuffix *)((unsigned long) &cmd->body +
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001535 header->size - sizeof(*suffix));
1536
1537 /* Make sure device and verifier stays in sync. */
1538 if (unlikely(suffix->suffixSize != sizeof(*suffix))) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001539 VMW_DEBUG_USER("Invalid DMA suffix size.\n");
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001540 return -EINVAL;
1541 }
1542
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001543 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
Deepak Rawat680360a2019-02-13 13:20:42 -08001544 &cmd->body.guest.ptr, &vmw_bo);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001545 if (unlikely(ret != 0))
1546 return ret;
1547
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001548 /* Make sure DMA doesn't cross BO boundaries. */
Christian Könige11bfb92020-12-09 15:07:50 +01001549 bo_size = vmw_bo->base.base.size;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001550 if (unlikely(cmd->body.guest.ptr.offset > bo_size)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001551 VMW_DEBUG_USER("Invalid DMA offset.\n");
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001552 return -EINVAL;
1553 }
1554
Deepak Rawatd01316d2019-02-08 15:50:40 -08001555 bo_size -= cmd->body.guest.ptr.offset;
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001556 if (unlikely(suffix->maximumOffset > bo_size))
1557 suffix->maximumOffset = bo_size;
1558
Deepak Rawatd01316d2019-02-08 15:50:40 -08001559 dirty = (cmd->body.transfer == SVGA3D_WRITE_HOST_VRAM) ?
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001560 VMW_RES_DIRTY_SET : 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001561 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001562 dirty, user_surface_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001563 &cmd->body.host.sid, NULL);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +02001564 if (unlikely(ret != 0)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001565 if (unlikely(ret != -ERESTARTSYS))
Deepak Rawat5724f892019-02-11 11:46:27 -08001566 VMW_DEBUG_USER("could not find surface for DMA.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001567 return ret;
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +02001568 }
1569
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001570 srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001571
Deepak Rawat680360a2019-02-13 13:20:42 -08001572 vmw_kms_cursor_snoop(srf, sw_context->fp->tfile, &vmw_bo->base, header);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001573
Thomas Hellstromb139d432018-09-26 16:27:54 +02001574 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001575}
1576
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001577static int vmw_cmd_draw(struct vmw_private *dev_priv,
1578 struct vmw_sw_context *sw_context,
1579 SVGA3dCmdHeader *header)
1580{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001581 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDrawPrimitives);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001582 SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
1583 (unsigned long)header + sizeof(*cmd));
1584 SVGA3dPrimitiveRange *range;
1585 uint32_t i;
1586 uint32_t maxnum;
1587 int ret;
1588
1589 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1590 if (unlikely(ret != 0))
1591 return ret;
1592
Deepak Rawatd01316d2019-02-08 15:50:40 -08001593 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001594 maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
1595
1596 if (unlikely(cmd->body.numVertexDecls > maxnum)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001597 VMW_DEBUG_USER("Illegal number of vertex declarations.\n");
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001598 return -EINVAL;
1599 }
1600
1601 for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001602 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001603 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001604 user_surface_converter,
1605 &decl->array.surfaceId, NULL);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001606 if (unlikely(ret != 0))
1607 return ret;
1608 }
1609
1610 maxnum = (header->size - sizeof(cmd->body) -
1611 cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
1612 if (unlikely(cmd->body.numRanges > maxnum)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001613 VMW_DEBUG_USER("Illegal number of index ranges.\n");
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001614 return -EINVAL;
1615 }
1616
1617 range = (SVGA3dPrimitiveRange *) decl;
1618 for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001619 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001620 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001621 user_surface_converter,
1622 &range->indexArray.surfaceId, NULL);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001623 if (unlikely(ret != 0))
1624 return ret;
1625 }
1626 return 0;
1627}
1628
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001629static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
1630 struct vmw_sw_context *sw_context,
1631 SVGA3dCmdHeader *header)
1632{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001633 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetTextureState);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001634 SVGA3dTextureState *last_state = (SVGA3dTextureState *)
1635 ((unsigned long) header + header->size + sizeof(header));
1636 SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
Deepak Rawatd01316d2019-02-08 15:50:40 -08001637 ((unsigned long) header + sizeof(*cmd));
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001638 struct vmw_resource *ctx;
1639 struct vmw_resource *res;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001640 int ret;
1641
Deepak Rawatd01316d2019-02-08 15:50:40 -08001642 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001643
1644 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001645 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001646 &cmd->body.cid, &ctx);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001647 if (unlikely(ret != 0))
1648 return ret;
1649
1650 for (; cur_state < last_state; ++cur_state) {
1651 if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
1652 continue;
1653
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001654 if (cur_state->stage >= SVGA3D_NUM_TEXTURE_UNITS) {
Deepak Rawat5724f892019-02-11 11:46:27 -08001655 VMW_DEBUG_USER("Illegal texture/sampler unit %u.\n",
1656 (unsigned int) cur_state->stage);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001657 return -EINVAL;
1658 }
1659
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001660 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001661 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001662 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001663 &cur_state->value, &res);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001664 if (unlikely(ret != 0))
1665 return ret;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001666
1667 if (dev_priv->has_mob) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001668 struct vmw_ctx_bindinfo_tex binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001669 struct vmw_ctx_validation_info *node;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001670
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001671 node = vmw_execbuf_info_from_res(sw_context, ctx);
1672 if (!node)
1673 return -EINVAL;
1674
1675 binding.bi.ctx = ctx;
1676 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001677 binding.bi.bt = vmw_ctx_binding_tex;
1678 binding.texture_stage = cur_state->stage;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001679 vmw_binding_add(node->staged, &binding.bi, 0,
1680 binding.texture_stage);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001681 }
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001682 }
1683
1684 return 0;
1685}
1686
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001687static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
1688 struct vmw_sw_context *sw_context,
1689 void *buf)
1690{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001691 struct vmw_buffer_object *vmw_bo;
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001692
1693 struct {
1694 uint32_t header;
1695 SVGAFifoCmdDefineGMRFB body;
1696 } *cmd = buf;
1697
Deepak Rawat680360a2019-02-13 13:20:42 -08001698 return vmw_translate_guest_ptr(dev_priv, sw_context, &cmd->body.ptr,
Thomas Hellstromb139d432018-09-26 16:27:54 +02001699 &vmw_bo);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001700}
1701
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001702/**
1703 * vmw_cmd_res_switch_backup - Utility function to handle backup buffer
1704 * switching
1705 *
1706 * @dev_priv: Pointer to a device private struct.
1707 * @sw_context: The software context being used for this batch.
Lee Jones7450bf72021-01-15 18:12:36 +00001708 * @res: Pointer to the resource.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001709 * @buf_id: Pointer to the user-space backup buffer handle in the command
1710 * stream.
1711 * @backup_offset: Offset of backup into MOB.
1712 *
Deepak Rawat680360a2019-02-13 13:20:42 -08001713 * This function prepares for registering a switch of backup buffers in the
1714 * resource metadata just prior to unreserving. It's basically a wrapper around
1715 * vmw_cmd_res_switch_backup with a different interface.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001716 */
1717static int vmw_cmd_res_switch_backup(struct vmw_private *dev_priv,
1718 struct vmw_sw_context *sw_context,
Deepak Rawat680360a2019-02-13 13:20:42 -08001719 struct vmw_resource *res, uint32_t *buf_id,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001720 unsigned long backup_offset)
1721{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001722 struct vmw_buffer_object *vbo;
1723 void *info;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001724 int ret;
1725
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001726 info = vmw_execbuf_info_from_res(sw_context, res);
1727 if (!info)
1728 return -EINVAL;
1729
1730 ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &vbo);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001731 if (ret)
1732 return ret;
1733
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001734 vmw_validation_res_switch_backup(sw_context->ctx, info, vbo,
1735 backup_offset);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001736 return 0;
1737}
1738
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001739/**
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001740 * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
1741 *
1742 * @dev_priv: Pointer to a device private struct.
1743 * @sw_context: The software context being used for this batch.
1744 * @res_type: The resource type.
1745 * @converter: Information about user-space binding for this resource type.
1746 * @res_id: Pointer to the user-space resource handle in the command stream.
1747 * @buf_id: Pointer to the user-space backup buffer handle in the command
1748 * stream.
1749 * @backup_offset: Offset of backup into MOB.
1750 *
Deepak Rawat680360a2019-02-13 13:20:42 -08001751 * This function prepares for registering a switch of backup buffers in the
1752 * resource metadata just prior to unreserving. It's basically a wrapper around
1753 * vmw_cmd_res_switch_backup with a different interface.
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001754 */
1755static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
1756 struct vmw_sw_context *sw_context,
1757 enum vmw_res_type res_type,
1758 const struct vmw_user_resource_conv
Deepak Rawat680360a2019-02-13 13:20:42 -08001759 *converter, uint32_t *res_id, uint32_t *buf_id,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001760 unsigned long backup_offset)
1761{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001762 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001763 int ret;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001764
1765 ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001766 VMW_RES_DIRTY_NONE, converter, res_id, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001767 if (ret)
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001768 return ret;
1769
Deepak Rawat680360a2019-02-13 13:20:42 -08001770 return vmw_cmd_res_switch_backup(dev_priv, sw_context, res, buf_id,
1771 backup_offset);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001772}
1773
1774/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001775 * vmw_cmd_bind_gb_surface - Validate SVGA_3D_CMD_BIND_GB_SURFACE command
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001776 *
1777 * @dev_priv: Pointer to a device private struct.
1778 * @sw_context: The software context being used for this batch.
1779 * @header: Pointer to the command header in the command stream.
1780 */
1781static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
1782 struct vmw_sw_context *sw_context,
1783 SVGA3dCmdHeader *header)
1784{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001785 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBindGBSurface) =
1786 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001787
1788 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
Deepak Rawat680360a2019-02-13 13:20:42 -08001789 user_surface_converter, &cmd->body.sid,
1790 &cmd->body.mobid, 0);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001791}
1792
1793/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001794 * vmw_cmd_update_gb_image - Validate SVGA_3D_CMD_UPDATE_GB_IMAGE command
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001795 *
1796 * @dev_priv: Pointer to a device private struct.
1797 * @sw_context: The software context being used for this batch.
1798 * @header: Pointer to the command header in the command stream.
1799 */
1800static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
1801 struct vmw_sw_context *sw_context,
1802 SVGA3dCmdHeader *header)
1803{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001804 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdUpdateGBImage) =
1805 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001806
1807 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001808 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001809 &cmd->body.image.sid, NULL);
1810}
1811
1812/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001813 * vmw_cmd_update_gb_surface - Validate SVGA_3D_CMD_UPDATE_GB_SURFACE command
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001814 *
1815 * @dev_priv: Pointer to a device private struct.
1816 * @sw_context: The software context being used for this batch.
1817 * @header: Pointer to the command header in the command stream.
1818 */
1819static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
1820 struct vmw_sw_context *sw_context,
1821 SVGA3dCmdHeader *header)
1822{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001823 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdUpdateGBSurface) =
1824 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001825
1826 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001827 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001828 &cmd->body.sid, NULL);
1829}
1830
1831/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001832 * vmw_cmd_readback_gb_image - Validate SVGA_3D_CMD_READBACK_GB_IMAGE command
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001833 *
1834 * @dev_priv: Pointer to a device private struct.
1835 * @sw_context: The software context being used for this batch.
1836 * @header: Pointer to the command header in the command stream.
1837 */
1838static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
1839 struct vmw_sw_context *sw_context,
1840 SVGA3dCmdHeader *header)
1841{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001842 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdReadbackGBImage) =
1843 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001844
1845 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001846 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001847 &cmd->body.image.sid, NULL);
1848}
1849
1850/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001851 * vmw_cmd_readback_gb_surface - Validate SVGA_3D_CMD_READBACK_GB_SURFACE
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001852 * command
1853 *
1854 * @dev_priv: Pointer to a device private struct.
1855 * @sw_context: The software context being used for this batch.
1856 * @header: Pointer to the command header in the command stream.
1857 */
1858static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
1859 struct vmw_sw_context *sw_context,
1860 SVGA3dCmdHeader *header)
1861{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001862 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdReadbackGBSurface) =
1863 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001864
1865 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001866 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001867 &cmd->body.sid, NULL);
1868}
1869
1870/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001871 * vmw_cmd_invalidate_gb_image - Validate SVGA_3D_CMD_INVALIDATE_GB_IMAGE
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001872 * command
1873 *
1874 * @dev_priv: Pointer to a device private struct.
1875 * @sw_context: The software context being used for this batch.
1876 * @header: Pointer to the command header in the command stream.
1877 */
1878static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
1879 struct vmw_sw_context *sw_context,
1880 SVGA3dCmdHeader *header)
1881{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001882 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdInvalidateGBImage) =
1883 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001884
1885 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001886 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001887 &cmd->body.image.sid, NULL);
1888}
1889
1890/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001891 * vmw_cmd_invalidate_gb_surface - Validate SVGA_3D_CMD_INVALIDATE_GB_SURFACE
1892 * command
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001893 *
1894 * @dev_priv: Pointer to a device private struct.
1895 * @sw_context: The software context being used for this batch.
1896 * @header: Pointer to the command header in the command stream.
1897 */
1898static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
1899 struct vmw_sw_context *sw_context,
1900 SVGA3dCmdHeader *header)
1901{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001902 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdInvalidateGBSurface) =
1903 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001904
1905 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001906 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001907 &cmd->body.sid, NULL);
1908}
1909
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001910/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001911 * vmw_cmd_shader_define - Validate SVGA_3D_CMD_SHADER_DEFINE command
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001912 *
1913 * @dev_priv: Pointer to a device private struct.
1914 * @sw_context: The software context being used for this batch.
1915 * @header: Pointer to the command header in the command stream.
1916 */
1917static int vmw_cmd_shader_define(struct vmw_private *dev_priv,
1918 struct vmw_sw_context *sw_context,
1919 SVGA3dCmdHeader *header)
1920{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001921 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDefineShader);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001922 int ret;
1923 size_t size;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001924 struct vmw_resource *ctx;
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001925
Deepak Rawatd01316d2019-02-08 15:50:40 -08001926 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001927
1928 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001929 VMW_RES_DIRTY_SET, user_context_converter,
1930 &cmd->body.cid, &ctx);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001931 if (unlikely(ret != 0))
1932 return ret;
1933
1934 if (unlikely(!dev_priv->has_mob))
1935 return 0;
1936
1937 size = cmd->header.size - sizeof(cmd->body);
Deepak Rawat680360a2019-02-13 13:20:42 -08001938 ret = vmw_compat_shader_add(dev_priv, vmw_context_res_man(ctx),
1939 cmd->body.shid, cmd + 1, cmd->body.type,
1940 size, &sw_context->staged_cmd_res);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001941 if (unlikely(ret != 0))
1942 return ret;
1943
Deepak Rawat680360a2019-02-13 13:20:42 -08001944 return vmw_resource_relocation_add(sw_context, NULL,
Thomas Hellstrome7a45282016-10-10 10:44:00 -07001945 vmw_ptr_diff(sw_context->buf_start,
Thomas Hellstroma1944032016-10-10 11:06:45 -07001946 &cmd->header.id),
1947 vmw_res_rel_nop);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001948}
1949
1950/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001951 * vmw_cmd_shader_destroy - Validate SVGA_3D_CMD_SHADER_DESTROY command
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001952 *
1953 * @dev_priv: Pointer to a device private struct.
1954 * @sw_context: The software context being used for this batch.
1955 * @header: Pointer to the command header in the command stream.
1956 */
1957static int vmw_cmd_shader_destroy(struct vmw_private *dev_priv,
1958 struct vmw_sw_context *sw_context,
1959 SVGA3dCmdHeader *header)
1960{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001961 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDestroyShader);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001962 int ret;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001963 struct vmw_resource *ctx;
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001964
Deepak Rawatd01316d2019-02-08 15:50:40 -08001965 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001966
1967 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001968 VMW_RES_DIRTY_SET, user_context_converter,
1969 &cmd->body.cid, &ctx);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001970 if (unlikely(ret != 0))
1971 return ret;
1972
1973 if (unlikely(!dev_priv->has_mob))
1974 return 0;
1975
Deepak Rawat680360a2019-02-13 13:20:42 -08001976 ret = vmw_shader_remove(vmw_context_res_man(ctx), cmd->body.shid,
1977 cmd->body.type, &sw_context->staged_cmd_res);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001978 if (unlikely(ret != 0))
1979 return ret;
1980
Deepak Rawat680360a2019-02-13 13:20:42 -08001981 return vmw_resource_relocation_add(sw_context, NULL,
Thomas Hellstrome7a45282016-10-10 10:44:00 -07001982 vmw_ptr_diff(sw_context->buf_start,
Thomas Hellstroma1944032016-10-10 11:06:45 -07001983 &cmd->header.id),
1984 vmw_res_rel_nop);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001985}
1986
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001987/**
Deepak Rawat680360a2019-02-13 13:20:42 -08001988 * vmw_cmd_set_shader - Validate SVGA_3D_CMD_SET_SHADER command
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001989 *
1990 * @dev_priv: Pointer to a device private struct.
1991 * @sw_context: The software context being used for this batch.
1992 * @header: Pointer to the command header in the command stream.
1993 */
1994static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
1995 struct vmw_sw_context *sw_context,
1996 SVGA3dCmdHeader *header)
1997{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001998 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetShader);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001999 struct vmw_ctx_bindinfo_shader binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002000 struct vmw_resource *ctx, *res = NULL;
2001 struct vmw_ctx_validation_info *ctx_info;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002002 int ret;
2003
Deepak Rawatd01316d2019-02-08 15:50:40 -08002004 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002005
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002006 if (cmd->body.type >= SVGA3D_SHADERTYPE_PREDX_MAX) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002007 VMW_DEBUG_USER("Illegal shader type %u.\n",
2008 (unsigned int) cmd->body.type);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002009 return -EINVAL;
2010 }
2011
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07002012 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002013 VMW_RES_DIRTY_SET, user_context_converter,
2014 &cmd->body.cid, &ctx);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002015 if (unlikely(ret != 0))
2016 return ret;
2017
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002018 if (!dev_priv->has_mob)
2019 return 0;
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002020
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002021 if (cmd->body.shid != SVGA3D_INVALID_ID) {
Thomas Hellstrome41c20c2019-04-04 13:25:43 +00002022 /*
2023 * This is the compat shader path - Per device guest-backed
2024 * shaders, but user-space thinks it's per context host-
2025 * backed shaders.
2026 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002027 res = vmw_shader_lookup(vmw_context_res_man(ctx),
Deepak Rawat680360a2019-02-13 13:20:42 -08002028 cmd->body.shid, cmd->body.type);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002029 if (!IS_ERR(res)) {
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002030 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2031 VMW_RES_DIRTY_NONE);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002032 if (unlikely(ret != 0))
2033 return ret;
Thomas Hellstrome41c20c2019-04-04 13:25:43 +00002034
2035 ret = vmw_resource_relocation_add
2036 (sw_context, res,
2037 vmw_ptr_diff(sw_context->buf_start,
2038 &cmd->body.shid),
2039 vmw_res_rel_normal);
2040 if (unlikely(ret != 0))
2041 return ret;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002042 }
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07002043 }
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002044
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002045 if (IS_ERR_OR_NULL(res)) {
Deepak Rawat680360a2019-02-13 13:20:42 -08002046 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_shader,
2047 VMW_RES_DIRTY_NONE,
2048 user_shader_converter, &cmd->body.shid,
2049 &res);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002050 if (unlikely(ret != 0))
2051 return ret;
2052 }
2053
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002054 ctx_info = vmw_execbuf_info_from_res(sw_context, ctx);
2055 if (!ctx_info)
2056 return -EINVAL;
2057
2058 binding.bi.ctx = ctx;
2059 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002060 binding.bi.bt = vmw_ctx_binding_shader;
2061 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
Deepak Rawat680360a2019-02-13 13:20:42 -08002062 vmw_binding_add(ctx_info->staged, &binding.bi, binding.shader_slot, 0);
2063
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002064 return 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002065}
2066
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002067/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002068 * vmw_cmd_set_shader_const - Validate SVGA_3D_CMD_SET_SHADER_CONST command
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002069 *
2070 * @dev_priv: Pointer to a device private struct.
2071 * @sw_context: The software context being used for this batch.
2072 * @header: Pointer to the command header in the command stream.
2073 */
2074static int vmw_cmd_set_shader_const(struct vmw_private *dev_priv,
2075 struct vmw_sw_context *sw_context,
2076 SVGA3dCmdHeader *header)
2077{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002078 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetShaderConst);
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002079 int ret;
2080
Deepak Rawatd01316d2019-02-08 15:50:40 -08002081 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002082
2083 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002084 VMW_RES_DIRTY_SET, user_context_converter,
2085 &cmd->body.cid, NULL);
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002086 if (unlikely(ret != 0))
2087 return ret;
2088
2089 if (dev_priv->has_mob)
2090 header->id = SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE;
2091
2092 return 0;
2093}
2094
2095/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002096 * vmw_cmd_bind_gb_shader - Validate SVGA_3D_CMD_BIND_GB_SHADER command
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002097 *
2098 * @dev_priv: Pointer to a device private struct.
2099 * @sw_context: The software context being used for this batch.
2100 * @header: Pointer to the command header in the command stream.
2101 */
2102static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
2103 struct vmw_sw_context *sw_context,
2104 SVGA3dCmdHeader *header)
2105{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002106 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBindGBShader) =
2107 container_of(header, typeof(*cmd), header);
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002108
2109 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
Deepak Rawat680360a2019-02-13 13:20:42 -08002110 user_shader_converter, &cmd->body.shid,
2111 &cmd->body.mobid, cmd->body.offsetInBytes);
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002112}
2113
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002114/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002115 * vmw_cmd_dx_set_single_constant_buffer - Validate
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002116 * SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER command.
2117 *
2118 * @dev_priv: Pointer to a device private struct.
2119 * @sw_context: The software context being used for this batch.
2120 * @header: Pointer to the command header in the command stream.
2121 */
2122static int
2123vmw_cmd_dx_set_single_constant_buffer(struct vmw_private *dev_priv,
2124 struct vmw_sw_context *sw_context,
2125 SVGA3dCmdHeader *header)
2126{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002127 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetSingleConstantBuffer);
Deepak Rawatd2e90ab2018-12-13 13:43:20 -08002128 SVGA3dShaderType max_shader_num = has_sm5_context(dev_priv) ?
2129 SVGA3D_NUM_SHADERTYPE : SVGA3D_NUM_SHADERTYPE_DX10;
2130
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002131 struct vmw_resource *res = NULL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002132 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002133 struct vmw_ctx_bindinfo_cb binding;
2134 int ret;
2135
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002136 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002137 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002138
2139 cmd = container_of(header, typeof(*cmd), header);
2140 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002141 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002142 &cmd->body.sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002143 if (unlikely(ret != 0))
2144 return ret;
2145
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002146 binding.bi.ctx = ctx_node->ctx;
2147 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002148 binding.bi.bt = vmw_ctx_binding_cb;
2149 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2150 binding.offset = cmd->body.offsetInBytes;
2151 binding.size = cmd->body.sizeInBytes;
2152 binding.slot = cmd->body.slot;
2153
Deepak Rawatd2e90ab2018-12-13 13:43:20 -08002154 if (binding.shader_slot >= max_shader_num ||
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002155 binding.slot >= SVGA3D_DX_MAX_CONSTBUFFERS) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002156 VMW_DEBUG_USER("Illegal const buffer shader %u slot %u.\n",
2157 (unsigned int) cmd->body.type,
2158 (unsigned int) binding.slot);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002159 return -EINVAL;
2160 }
2161
Deepak Rawat680360a2019-02-13 13:20:42 -08002162 vmw_binding_add(ctx_node->staged, &binding.bi, binding.shader_slot,
2163 binding.slot);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002164
2165 return 0;
2166}
2167
2168/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002169 * vmw_cmd_dx_set_shader_res - Validate SVGA_3D_CMD_DX_SET_SHADER_RESOURCES
2170 * command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002171 *
2172 * @dev_priv: Pointer to a device private struct.
2173 * @sw_context: The software context being used for this batch.
2174 * @header: Pointer to the command header in the command stream.
2175 */
2176static int vmw_cmd_dx_set_shader_res(struct vmw_private *dev_priv,
2177 struct vmw_sw_context *sw_context,
2178 SVGA3dCmdHeader *header)
2179{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002180 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetShaderResources) =
2181 container_of(header, typeof(*cmd), header);
Deepak Rawatd2e90ab2018-12-13 13:43:20 -08002182 SVGA3dShaderType max_allowed = has_sm5_context(dev_priv) ?
2183 SVGA3D_SHADERTYPE_MAX : SVGA3D_SHADERTYPE_DX10_MAX;
2184
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002185 u32 num_sr_view = (cmd->header.size - sizeof(cmd->body)) /
2186 sizeof(SVGA3dShaderResourceViewId);
2187
2188 if ((u64) cmd->body.startView + (u64) num_sr_view >
2189 (u64) SVGA3D_DX_MAX_SRVIEWS ||
Deepak Rawatd2e90ab2018-12-13 13:43:20 -08002190 cmd->body.type >= max_allowed) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002191 VMW_DEBUG_USER("Invalid shader binding.\n");
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002192 return -EINVAL;
2193 }
2194
2195 return vmw_view_bindings_add(sw_context, vmw_view_sr,
2196 vmw_ctx_binding_sr,
2197 cmd->body.type - SVGA3D_SHADERTYPE_MIN,
2198 (void *) &cmd[1], num_sr_view,
2199 cmd->body.startView);
2200}
2201
2202/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002203 * vmw_cmd_dx_set_shader - Validate SVGA_3D_CMD_DX_SET_SHADER command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002204 *
2205 * @dev_priv: Pointer to a device private struct.
2206 * @sw_context: The software context being used for this batch.
2207 * @header: Pointer to the command header in the command stream.
2208 */
2209static int vmw_cmd_dx_set_shader(struct vmw_private *dev_priv,
2210 struct vmw_sw_context *sw_context,
2211 SVGA3dCmdHeader *header)
2212{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002213 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetShader);
Deepak Rawatd2e90ab2018-12-13 13:43:20 -08002214 SVGA3dShaderType max_allowed = has_sm5_context(dev_priv) ?
2215 SVGA3D_SHADERTYPE_MAX : SVGA3D_SHADERTYPE_DX10_MAX;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002216 struct vmw_resource *res = NULL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002217 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002218 struct vmw_ctx_bindinfo_shader binding;
2219 int ret = 0;
2220
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002221 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002222 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002223
2224 cmd = container_of(header, typeof(*cmd), header);
2225
Deepak Rawatd2e90ab2018-12-13 13:43:20 -08002226 if (cmd->body.type >= max_allowed ||
Murray McAllister5ed7f4b2019-05-20 21:57:34 +12002227 cmd->body.type < SVGA3D_SHADERTYPE_MIN) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002228 VMW_DEBUG_USER("Illegal shader type %u.\n",
2229 (unsigned int) cmd->body.type);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002230 return -EINVAL;
2231 }
2232
2233 if (cmd->body.shaderId != SVGA3D_INVALID_ID) {
2234 res = vmw_shader_lookup(sw_context->man, cmd->body.shaderId, 0);
2235 if (IS_ERR(res)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002236 VMW_DEBUG_USER("Could not find shader for binding.\n");
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002237 return PTR_ERR(res);
2238 }
2239
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002240 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2241 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002242 if (ret)
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002243 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002244 }
2245
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002246 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002247 binding.bi.res = res;
2248 binding.bi.bt = vmw_ctx_binding_dx_shader;
2249 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2250
Deepak Rawat680360a2019-02-13 13:20:42 -08002251 vmw_binding_add(ctx_node->staged, &binding.bi, binding.shader_slot, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002252
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002253 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002254}
2255
2256/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002257 * vmw_cmd_dx_set_vertex_buffers - Validates SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS
2258 * command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002259 *
2260 * @dev_priv: Pointer to a device private struct.
2261 * @sw_context: The software context being used for this batch.
2262 * @header: Pointer to the command header in the command stream.
2263 */
2264static int vmw_cmd_dx_set_vertex_buffers(struct vmw_private *dev_priv,
2265 struct vmw_sw_context *sw_context,
2266 SVGA3dCmdHeader *header)
2267{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002268 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002269 struct vmw_ctx_bindinfo_vb binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002270 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002271 struct {
2272 SVGA3dCmdHeader header;
2273 SVGA3dCmdDXSetVertexBuffers body;
2274 SVGA3dVertexBuffer buf[];
2275 } *cmd;
2276 int i, ret, num;
2277
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002278 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002279 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002280
2281 cmd = container_of(header, typeof(*cmd), header);
2282 num = (cmd->header.size - sizeof(cmd->body)) /
2283 sizeof(SVGA3dVertexBuffer);
2284 if ((u64)num + (u64)cmd->body.startBuffer >
2285 (u64)SVGA3D_DX_MAX_VERTEXBUFFERS) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002286 VMW_DEBUG_USER("Invalid number of vertex buffers.\n");
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002287 return -EINVAL;
2288 }
2289
2290 for (i = 0; i < num; i++) {
2291 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002292 VMW_RES_DIRTY_NONE,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002293 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002294 &cmd->buf[i].sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002295 if (unlikely(ret != 0))
2296 return ret;
2297
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002298 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002299 binding.bi.bt = vmw_ctx_binding_vb;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002300 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002301 binding.offset = cmd->buf[i].offset;
2302 binding.stride = cmd->buf[i].stride;
2303 binding.slot = i + cmd->body.startBuffer;
2304
Deepak Rawat680360a2019-02-13 13:20:42 -08002305 vmw_binding_add(ctx_node->staged, &binding.bi, 0, binding.slot);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002306 }
2307
2308 return 0;
2309}
2310
2311/**
Zack Rusin2cd80db2021-05-05 15:10:07 -04002312 * vmw_cmd_dx_set_index_buffer - Validate
Brian Paul8bd62872017-07-17 07:36:10 -07002313 * SVGA_3D_CMD_DX_IA_SET_INDEX_BUFFER command.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002314 *
2315 * @dev_priv: Pointer to a device private struct.
2316 * @sw_context: The software context being used for this batch.
2317 * @header: Pointer to the command header in the command stream.
2318 */
2319static int vmw_cmd_dx_set_index_buffer(struct vmw_private *dev_priv,
2320 struct vmw_sw_context *sw_context,
2321 SVGA3dCmdHeader *header)
2322{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002323 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002324 struct vmw_ctx_bindinfo_ib binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002325 struct vmw_resource *res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08002326 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetIndexBuffer);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002327 int ret;
2328
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002329 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002330 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002331
2332 cmd = container_of(header, typeof(*cmd), header);
2333 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002334 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002335 &cmd->body.sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002336 if (unlikely(ret != 0))
2337 return ret;
2338
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002339 binding.bi.ctx = ctx_node->ctx;
2340 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002341 binding.bi.bt = vmw_ctx_binding_ib;
2342 binding.offset = cmd->body.offset;
2343 binding.format = cmd->body.format;
2344
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002345 vmw_binding_add(ctx_node->staged, &binding.bi, 0, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002346
2347 return 0;
2348}
2349
2350/**
Zack Rusin2cd80db2021-05-05 15:10:07 -04002351 * vmw_cmd_dx_set_rendertargets - Validate SVGA_3D_CMD_DX_SET_RENDERTARGETS
Deepak Rawat680360a2019-02-13 13:20:42 -08002352 * command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002353 *
2354 * @dev_priv: Pointer to a device private struct.
2355 * @sw_context: The software context being used for this batch.
2356 * @header: Pointer to the command header in the command stream.
2357 */
2358static int vmw_cmd_dx_set_rendertargets(struct vmw_private *dev_priv,
2359 struct vmw_sw_context *sw_context,
2360 SVGA3dCmdHeader *header)
2361{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002362 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetRenderTargets) =
2363 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002364 u32 num_rt_view = (cmd->header.size - sizeof(cmd->body)) /
2365 sizeof(SVGA3dRenderTargetViewId);
Deepak Rawatd01316d2019-02-08 15:50:40 -08002366 int ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002367
2368 if (num_rt_view > SVGA3D_MAX_SIMULTANEOUS_RENDER_TARGETS) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002369 VMW_DEBUG_USER("Invalid DX Rendertarget binding.\n");
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002370 return -EINVAL;
2371 }
2372
Deepak Rawat680360a2019-02-13 13:20:42 -08002373 ret = vmw_view_bindings_add(sw_context, vmw_view_ds, vmw_ctx_binding_ds,
2374 0, &cmd->body.depthStencilViewId, 1, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002375 if (ret)
2376 return ret;
2377
2378 return vmw_view_bindings_add(sw_context, vmw_view_rt,
Deepak Rawat680360a2019-02-13 13:20:42 -08002379 vmw_ctx_binding_dx_rt, 0, (void *)&cmd[1],
2380 num_rt_view, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002381}
2382
2383/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002384 * vmw_cmd_dx_clear_rendertarget_view - Validate
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002385 * SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW command
2386 *
2387 * @dev_priv: Pointer to a device private struct.
2388 * @sw_context: The software context being used for this batch.
2389 * @header: Pointer to the command header in the command stream.
2390 */
2391static int vmw_cmd_dx_clear_rendertarget_view(struct vmw_private *dev_priv,
2392 struct vmw_sw_context *sw_context,
2393 SVGA3dCmdHeader *header)
2394{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002395 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXClearRenderTargetView) =
2396 container_of(header, typeof(*cmd), header);
Lukas Bulwahna26ca962019-12-08 11:53:28 +01002397 struct vmw_resource *ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002398
Lukas Bulwahna26ca962019-12-08 11:53:28 +01002399 ret = vmw_view_id_val_add(sw_context, vmw_view_rt,
2400 cmd->body.renderTargetViewId);
2401
2402 return PTR_ERR_OR_ZERO(ret);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002403}
2404
2405/**
Zack Rusin2cd80db2021-05-05 15:10:07 -04002406 * vmw_cmd_dx_clear_depthstencil_view - Validate
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002407 * SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW command
2408 *
2409 * @dev_priv: Pointer to a device private struct.
2410 * @sw_context: The software context being used for this batch.
2411 * @header: Pointer to the command header in the command stream.
2412 */
2413static int vmw_cmd_dx_clear_depthstencil_view(struct vmw_private *dev_priv,
2414 struct vmw_sw_context *sw_context,
2415 SVGA3dCmdHeader *header)
2416{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002417 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXClearDepthStencilView) =
2418 container_of(header, typeof(*cmd), header);
Lukas Bulwahna26ca962019-12-08 11:53:28 +01002419 struct vmw_resource *ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002420
Lukas Bulwahna26ca962019-12-08 11:53:28 +01002421 ret = vmw_view_id_val_add(sw_context, vmw_view_ds,
2422 cmd->body.depthStencilViewId);
2423
2424 return PTR_ERR_OR_ZERO(ret);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002425}
2426
2427static int vmw_cmd_dx_view_define(struct vmw_private *dev_priv,
2428 struct vmw_sw_context *sw_context,
2429 SVGA3dCmdHeader *header)
2430{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002431 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002432 struct vmw_resource *srf;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002433 struct vmw_resource *res;
2434 enum vmw_view_type view_type;
2435 int ret;
2436 /*
Deepak Rawat680360a2019-02-13 13:20:42 -08002437 * This is based on the fact that all affected define commands have the
2438 * same initial command body layout.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002439 */
2440 struct {
2441 SVGA3dCmdHeader header;
2442 uint32 defined_id;
2443 uint32 sid;
2444 } *cmd;
2445
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002446 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002447 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002448
2449 view_type = vmw_view_cmd_to_type(header->id);
Dan Carpenter0d9cac02018-01-10 12:40:04 +03002450 if (view_type == vmw_view_max)
2451 return -EINVAL;
Deepak Rawat680360a2019-02-13 13:20:42 -08002452
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002453 cmd = container_of(header, typeof(*cmd), header);
Murray McAllisterbcd6aa72019-05-11 18:01:37 +12002454 if (unlikely(cmd->sid == SVGA3D_INVALID_ID)) {
2455 VMW_DEBUG_USER("Invalid surface id.\n");
2456 return -EINVAL;
2457 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002458 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002459 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002460 &cmd->sid, &srf);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002461 if (unlikely(ret != 0))
2462 return ret;
2463
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002464 res = vmw_context_cotable(ctx_node->ctx, vmw_view_cotables[view_type]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002465 ret = vmw_cotable_notify(res, cmd->defined_id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002466 if (unlikely(ret != 0))
2467 return ret;
2468
Deepak Rawat680360a2019-02-13 13:20:42 -08002469 return vmw_view_add(sw_context->man, ctx_node->ctx, srf, view_type,
2470 cmd->defined_id, header,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002471 header->size + sizeof(*header),
2472 &sw_context->staged_cmd_res);
2473}
2474
Charmaine Lee2f633e52015-08-10 10:45:11 -07002475/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002476 * vmw_cmd_dx_set_so_targets - Validate SVGA_3D_CMD_DX_SET_SOTARGETS command.
Charmaine Lee2f633e52015-08-10 10:45:11 -07002477 *
2478 * @dev_priv: Pointer to a device private struct.
2479 * @sw_context: The software context being used for this batch.
2480 * @header: Pointer to the command header in the command stream.
2481 */
2482static int vmw_cmd_dx_set_so_targets(struct vmw_private *dev_priv,
2483 struct vmw_sw_context *sw_context,
2484 SVGA3dCmdHeader *header)
2485{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002486 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Deepak Rawat403fef52018-12-18 10:13:13 -08002487 struct vmw_ctx_bindinfo_so_target binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002488 struct vmw_resource *res;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002489 struct {
2490 SVGA3dCmdHeader header;
2491 SVGA3dCmdDXSetSOTargets body;
2492 SVGA3dSoTarget targets[];
2493 } *cmd;
2494 int i, ret, num;
2495
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002496 if (!ctx_node)
Charmaine Lee2f633e52015-08-10 10:45:11 -07002497 return -EINVAL;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002498
2499 cmd = container_of(header, typeof(*cmd), header);
Deepak Rawat680360a2019-02-13 13:20:42 -08002500 num = (cmd->header.size - sizeof(cmd->body)) / sizeof(SVGA3dSoTarget);
Charmaine Lee2f633e52015-08-10 10:45:11 -07002501
2502 if (num > SVGA3D_DX_MAX_SOTARGETS) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002503 VMW_DEBUG_USER("Invalid DX SO binding.\n");
Charmaine Lee2f633e52015-08-10 10:45:11 -07002504 return -EINVAL;
2505 }
2506
2507 for (i = 0; i < num; i++) {
2508 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002509 VMW_RES_DIRTY_SET,
Charmaine Lee2f633e52015-08-10 10:45:11 -07002510 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002511 &cmd->targets[i].sid, &res);
Charmaine Lee2f633e52015-08-10 10:45:11 -07002512 if (unlikely(ret != 0))
2513 return ret;
2514
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002515 binding.bi.ctx = ctx_node->ctx;
2516 binding.bi.res = res;
Zheng Yongjuned2684e2020-12-11 16:57:51 +08002517 binding.bi.bt = vmw_ctx_binding_so_target;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002518 binding.offset = cmd->targets[i].offset;
2519 binding.size = cmd->targets[i].sizeInBytes;
2520 binding.slot = i;
2521
Deepak Rawat680360a2019-02-13 13:20:42 -08002522 vmw_binding_add(ctx_node->staged, &binding.bi, 0, binding.slot);
Charmaine Lee2f633e52015-08-10 10:45:11 -07002523 }
2524
2525 return 0;
2526}
2527
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002528static int vmw_cmd_dx_so_define(struct vmw_private *dev_priv,
2529 struct vmw_sw_context *sw_context,
2530 SVGA3dCmdHeader *header)
2531{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002532 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002533 struct vmw_resource *res;
2534 /*
2535 * This is based on the fact that all affected define commands have
2536 * the same initial command body layout.
2537 */
2538 struct {
2539 SVGA3dCmdHeader header;
2540 uint32 defined_id;
2541 } *cmd;
2542 enum vmw_so_type so_type;
2543 int ret;
2544
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002545 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002546 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002547
2548 so_type = vmw_so_cmd_to_type(header->id);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002549 res = vmw_context_cotable(ctx_node->ctx, vmw_so_cotables[so_type]);
Zack Rusin74231042021-06-09 13:23:02 -04002550 if (IS_ERR(res))
2551 return PTR_ERR(res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002552 cmd = container_of(header, typeof(*cmd), header);
2553 ret = vmw_cotable_notify(res, cmd->defined_id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002554
2555 return ret;
2556}
2557
2558/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002559 * vmw_cmd_dx_check_subresource - Validate SVGA_3D_CMD_DX_[X]_SUBRESOURCE
2560 * command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002561 *
2562 * @dev_priv: Pointer to a device private struct.
2563 * @sw_context: The software context being used for this batch.
2564 * @header: Pointer to the command header in the command stream.
2565 */
2566static int vmw_cmd_dx_check_subresource(struct vmw_private *dev_priv,
2567 struct vmw_sw_context *sw_context,
2568 SVGA3dCmdHeader *header)
2569{
2570 struct {
2571 SVGA3dCmdHeader header;
2572 union {
2573 SVGA3dCmdDXReadbackSubResource r_body;
2574 SVGA3dCmdDXInvalidateSubResource i_body;
2575 SVGA3dCmdDXUpdateSubResource u_body;
2576 SVGA3dSurfaceId sid;
2577 };
2578 } *cmd;
2579
2580 BUILD_BUG_ON(offsetof(typeof(*cmd), r_body.sid) !=
2581 offsetof(typeof(*cmd), sid));
2582 BUILD_BUG_ON(offsetof(typeof(*cmd), i_body.sid) !=
2583 offsetof(typeof(*cmd), sid));
2584 BUILD_BUG_ON(offsetof(typeof(*cmd), u_body.sid) !=
2585 offsetof(typeof(*cmd), sid));
2586
2587 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002588 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002589 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002590 &cmd->sid, NULL);
2591}
2592
2593static int vmw_cmd_dx_cid_check(struct vmw_private *dev_priv,
2594 struct vmw_sw_context *sw_context,
2595 SVGA3dCmdHeader *header)
2596{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002597 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002598
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002599 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002600 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002601
2602 return 0;
2603}
2604
2605/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002606 * vmw_cmd_dx_view_remove - validate a view remove command and schedule the view
2607 * resource for removal.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002608 *
2609 * @dev_priv: Pointer to a device private struct.
2610 * @sw_context: The software context being used for this batch.
2611 * @header: Pointer to the command header in the command stream.
2612 *
Deepak Rawat680360a2019-02-13 13:20:42 -08002613 * Check that the view exists, and if it was not created using this command
2614 * batch, conditionally make this command a NOP.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002615 */
2616static int vmw_cmd_dx_view_remove(struct vmw_private *dev_priv,
2617 struct vmw_sw_context *sw_context,
2618 SVGA3dCmdHeader *header)
2619{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002620 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002621 struct {
2622 SVGA3dCmdHeader header;
2623 union vmw_view_destroy body;
2624 } *cmd = container_of(header, typeof(*cmd), header);
2625 enum vmw_view_type view_type = vmw_view_cmd_to_type(header->id);
2626 struct vmw_resource *view;
2627 int ret;
2628
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002629 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002630 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002631
Deepak Rawat680360a2019-02-13 13:20:42 -08002632 ret = vmw_view_remove(sw_context->man, cmd->body.view_id, view_type,
2633 &sw_context->staged_cmd_res, &view);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002634 if (ret || !view)
2635 return ret;
2636
2637 /*
Thomas Hellstroma1944032016-10-10 11:06:45 -07002638 * If the view wasn't created during this command batch, it might
2639 * have been removed due to a context swapout, so add a
2640 * relocation to conditionally make this command a NOP to avoid
2641 * device errors.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002642 */
Deepak Rawat680360a2019-02-13 13:20:42 -08002643 return vmw_resource_relocation_add(sw_context, view,
Thomas Hellstroma1944032016-10-10 11:06:45 -07002644 vmw_ptr_diff(sw_context->buf_start,
2645 &cmd->header.id),
2646 vmw_res_rel_cond_nop);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002647}
2648
2649/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002650 * vmw_cmd_dx_define_shader - Validate SVGA_3D_CMD_DX_DEFINE_SHADER command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002651 *
2652 * @dev_priv: Pointer to a device private struct.
2653 * @sw_context: The software context being used for this batch.
2654 * @header: Pointer to the command header in the command stream.
2655 */
2656static int vmw_cmd_dx_define_shader(struct vmw_private *dev_priv,
2657 struct vmw_sw_context *sw_context,
2658 SVGA3dCmdHeader *header)
2659{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002660 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002661 struct vmw_resource *res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08002662 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDefineShader) =
2663 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002664 int ret;
2665
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002666 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002667 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002668
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002669 res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXSHADER);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002670 ret = vmw_cotable_notify(res, cmd->body.shaderId);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002671 if (ret)
2672 return ret;
2673
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002674 return vmw_dx_shader_add(sw_context->man, ctx_node->ctx,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002675 cmd->body.shaderId, cmd->body.type,
2676 &sw_context->staged_cmd_res);
2677}
2678
2679/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002680 * vmw_cmd_dx_destroy_shader - Validate SVGA_3D_CMD_DX_DESTROY_SHADER command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002681 *
2682 * @dev_priv: Pointer to a device private struct.
2683 * @sw_context: The software context being used for this batch.
2684 * @header: Pointer to the command header in the command stream.
2685 */
2686static int vmw_cmd_dx_destroy_shader(struct vmw_private *dev_priv,
2687 struct vmw_sw_context *sw_context,
2688 SVGA3dCmdHeader *header)
2689{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002690 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Deepak Rawatd01316d2019-02-08 15:50:40 -08002691 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDestroyShader) =
2692 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002693 int ret;
2694
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002695 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002696 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002697
2698 ret = vmw_shader_remove(sw_context->man, cmd->body.shaderId, 0,
2699 &sw_context->staged_cmd_res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002700
2701 return ret;
2702}
2703
2704/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002705 * vmw_cmd_dx_bind_shader - Validate SVGA_3D_CMD_DX_BIND_SHADER command
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002706 *
2707 * @dev_priv: Pointer to a device private struct.
2708 * @sw_context: The software context being used for this batch.
2709 * @header: Pointer to the command header in the command stream.
2710 */
2711static int vmw_cmd_dx_bind_shader(struct vmw_private *dev_priv,
2712 struct vmw_sw_context *sw_context,
2713 SVGA3dCmdHeader *header)
2714{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002715 struct vmw_resource *ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002716 struct vmw_resource *res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08002717 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindShader) =
2718 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002719 int ret;
2720
2721 if (cmd->body.cid != SVGA3D_INVALID_ID) {
2722 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002723 VMW_RES_DIRTY_SET,
2724 user_context_converter, &cmd->body.cid,
2725 &ctx);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002726 if (ret)
2727 return ret;
2728 } else {
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002729 struct vmw_ctx_validation_info *ctx_node =
2730 VMW_GET_CTX_NODE(sw_context);
2731
2732 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002733 return -EINVAL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002734
2735 ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002736 }
2737
Deepak Rawat680360a2019-02-13 13:20:42 -08002738 res = vmw_shader_lookup(vmw_context_res_man(ctx), cmd->body.shid, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002739 if (IS_ERR(res)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002740 VMW_DEBUG_USER("Could not find shader to bind.\n");
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002741 return PTR_ERR(res);
2742 }
2743
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002744 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2745 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002746 if (ret) {
Deepak Rawat5724f892019-02-11 11:46:27 -08002747 VMW_DEBUG_USER("Error creating resource validation node.\n");
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002748 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002749 }
2750
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002751 return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
2752 &cmd->body.mobid,
2753 cmd->body.offsetInBytes);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002754}
2755
Charmaine Leef3b335502016-02-12 08:11:56 +01002756/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002757 * vmw_cmd_dx_genmips - Validate SVGA_3D_CMD_DX_GENMIPS command
Charmaine Leef3b335502016-02-12 08:11:56 +01002758 *
2759 * @dev_priv: Pointer to a device private struct.
2760 * @sw_context: The software context being used for this batch.
2761 * @header: Pointer to the command header in the command stream.
2762 */
2763static int vmw_cmd_dx_genmips(struct vmw_private *dev_priv,
2764 struct vmw_sw_context *sw_context,
2765 SVGA3dCmdHeader *header)
2766{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002767 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXGenMips) =
2768 container_of(header, typeof(*cmd), header);
Thomas Hellstrom75156a82021-05-04 23:57:36 -04002769 struct vmw_resource *view;
2770 struct vmw_res_cache_entry *rcache;
Charmaine Leef3b335502016-02-12 08:11:56 +01002771
Thomas Hellstrom75156a82021-05-04 23:57:36 -04002772 view = vmw_view_id_val_add(sw_context, vmw_view_sr,
2773 cmd->body.shaderResourceViewId);
2774 if (IS_ERR(view))
2775 return PTR_ERR(view);
Lukas Bulwahna26ca962019-12-08 11:53:28 +01002776
Thomas Hellstrom75156a82021-05-04 23:57:36 -04002777 /*
2778 * Normally the shader-resource view is not gpu-dirtying, but for
2779 * this particular command it is...
2780 * So mark the last looked-up surface, which is the surface
2781 * the view points to, gpu-dirty.
2782 */
2783 rcache = &sw_context->res_cache[vmw_res_surface];
2784 vmw_validation_res_set_dirty(sw_context->ctx, rcache->private,
2785 VMW_RES_DIRTY_SET);
2786 return 0;
Charmaine Leef3b335502016-02-12 08:11:56 +01002787}
2788
Charmaine Lee1f982e42016-10-10 10:37:03 -07002789/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002790 * vmw_cmd_dx_transfer_from_buffer - Validate
2791 * SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER command
Charmaine Lee1f982e42016-10-10 10:37:03 -07002792 *
2793 * @dev_priv: Pointer to a device private struct.
2794 * @sw_context: The software context being used for this batch.
2795 * @header: Pointer to the command header in the command stream.
2796 */
2797static int vmw_cmd_dx_transfer_from_buffer(struct vmw_private *dev_priv,
2798 struct vmw_sw_context *sw_context,
2799 SVGA3dCmdHeader *header)
2800{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002801 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXTransferFromBuffer) =
2802 container_of(header, typeof(*cmd), header);
Charmaine Lee1f982e42016-10-10 10:37:03 -07002803 int ret;
2804
2805 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002806 VMW_RES_DIRTY_NONE, user_surface_converter,
Charmaine Lee1f982e42016-10-10 10:37:03 -07002807 &cmd->body.srcSid, NULL);
2808 if (ret != 0)
2809 return ret;
2810
2811 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002812 VMW_RES_DIRTY_SET, user_surface_converter,
Charmaine Lee1f982e42016-10-10 10:37:03 -07002813 &cmd->body.destSid, NULL);
2814}
2815
Neha Bhende0d81d342018-06-18 17:14:56 -07002816/**
Deepak Rawat680360a2019-02-13 13:20:42 -08002817 * vmw_cmd_intra_surface_copy - Validate SVGA_3D_CMD_INTRA_SURFACE_COPY command
Neha Bhende0d81d342018-06-18 17:14:56 -07002818 *
2819 * @dev_priv: Pointer to a device private struct.
2820 * @sw_context: The software context being used for this batch.
2821 * @header: Pointer to the command header in the command stream.
2822 */
2823static int vmw_cmd_intra_surface_copy(struct vmw_private *dev_priv,
2824 struct vmw_sw_context *sw_context,
2825 SVGA3dCmdHeader *header)
2826{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002827 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdIntraSurfaceCopy) =
2828 container_of(header, typeof(*cmd), header);
Neha Bhende0d81d342018-06-18 17:14:56 -07002829
2830 if (!(dev_priv->capabilities2 & SVGA_CAP2_INTRA_SURFACE_COPY))
2831 return -EINVAL;
2832
2833 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002834 VMW_RES_DIRTY_SET, user_surface_converter,
2835 &cmd->body.surface.sid, NULL);
Neha Bhende0d81d342018-06-18 17:14:56 -07002836}
2837
Deepak Rawatb6fad732018-12-13 14:00:18 -08002838static int vmw_cmd_sm5(struct vmw_private *dev_priv,
2839 struct vmw_sw_context *sw_context,
2840 SVGA3dCmdHeader *header)
2841{
2842 if (!has_sm5_context(dev_priv))
2843 return -EINVAL;
2844
2845 return 0;
2846}
2847
Deepak Rawat5e8ec0d2018-12-13 13:51:08 -08002848static int vmw_cmd_sm5_view_define(struct vmw_private *dev_priv,
2849 struct vmw_sw_context *sw_context,
2850 SVGA3dCmdHeader *header)
2851{
2852 if (!has_sm5_context(dev_priv))
2853 return -EINVAL;
2854
2855 return vmw_cmd_dx_view_define(dev_priv, sw_context, header);
2856}
2857
2858static int vmw_cmd_sm5_view_remove(struct vmw_private *dev_priv,
2859 struct vmw_sw_context *sw_context,
2860 SVGA3dCmdHeader *header)
2861{
2862 if (!has_sm5_context(dev_priv))
2863 return -EINVAL;
2864
2865 return vmw_cmd_dx_view_remove(dev_priv, sw_context, header);
2866}
2867
2868static int vmw_cmd_clear_uav_uint(struct vmw_private *dev_priv,
2869 struct vmw_sw_context *sw_context,
2870 SVGA3dCmdHeader *header)
2871{
2872 struct {
2873 SVGA3dCmdHeader header;
2874 SVGA3dCmdDXClearUAViewUint body;
2875 } *cmd = container_of(header, typeof(*cmd), header);
2876 struct vmw_resource *ret;
2877
2878 if (!has_sm5_context(dev_priv))
2879 return -EINVAL;
2880
2881 ret = vmw_view_id_val_add(sw_context, vmw_view_ua,
2882 cmd->body.uaViewId);
2883
2884 return PTR_ERR_OR_ZERO(ret);
2885}
2886
2887static int vmw_cmd_clear_uav_float(struct vmw_private *dev_priv,
2888 struct vmw_sw_context *sw_context,
2889 SVGA3dCmdHeader *header)
2890{
2891 struct {
2892 SVGA3dCmdHeader header;
2893 SVGA3dCmdDXClearUAViewFloat body;
2894 } *cmd = container_of(header, typeof(*cmd), header);
2895 struct vmw_resource *ret;
2896
2897 if (!has_sm5_context(dev_priv))
2898 return -EINVAL;
2899
2900 ret = vmw_view_id_val_add(sw_context, vmw_view_ua,
2901 cmd->body.uaViewId);
2902
2903 return PTR_ERR_OR_ZERO(ret);
2904}
2905
2906static int vmw_cmd_set_uav(struct vmw_private *dev_priv,
2907 struct vmw_sw_context *sw_context,
2908 SVGA3dCmdHeader *header)
2909{
2910 struct {
2911 SVGA3dCmdHeader header;
2912 SVGA3dCmdDXSetUAViews body;
2913 } *cmd = container_of(header, typeof(*cmd), header);
2914 u32 num_uav = (cmd->header.size - sizeof(cmd->body)) /
2915 sizeof(SVGA3dUAViewId);
2916 int ret;
2917
2918 if (!has_sm5_context(dev_priv))
2919 return -EINVAL;
2920
2921 if (num_uav > SVGA3D_MAX_UAVIEWS) {
2922 VMW_DEBUG_USER("Invalid UAV binding.\n");
2923 return -EINVAL;
2924 }
2925
2926 ret = vmw_view_bindings_add(sw_context, vmw_view_ua,
2927 vmw_ctx_binding_uav, 0, (void *)&cmd[1],
2928 num_uav, 0);
2929 if (ret)
2930 return ret;
2931
2932 vmw_binding_add_uav_index(sw_context->dx_ctx_node->staged, 0,
2933 cmd->body.uavSpliceIndex);
2934
2935 return ret;
2936}
2937
2938static int vmw_cmd_set_cs_uav(struct vmw_private *dev_priv,
2939 struct vmw_sw_context *sw_context,
2940 SVGA3dCmdHeader *header)
2941{
2942 struct {
2943 SVGA3dCmdHeader header;
2944 SVGA3dCmdDXSetCSUAViews body;
2945 } *cmd = container_of(header, typeof(*cmd), header);
2946 u32 num_uav = (cmd->header.size - sizeof(cmd->body)) /
2947 sizeof(SVGA3dUAViewId);
2948 int ret;
2949
2950 if (!has_sm5_context(dev_priv))
2951 return -EINVAL;
2952
2953 if (num_uav > SVGA3D_MAX_UAVIEWS) {
2954 VMW_DEBUG_USER("Invalid UAV binding.\n");
2955 return -EINVAL;
2956 }
2957
2958 ret = vmw_view_bindings_add(sw_context, vmw_view_ua,
2959 vmw_ctx_binding_cs_uav, 0, (void *)&cmd[1],
2960 num_uav, 0);
2961 if (ret)
2962 return ret;
2963
2964 vmw_binding_add_uav_index(sw_context->dx_ctx_node->staged, 1,
2965 cmd->body.startIndex);
2966
2967 return ret;
2968}
2969
Deepak Rawate8bead92018-12-13 14:04:31 -08002970static int vmw_cmd_dx_define_streamoutput(struct vmw_private *dev_priv,
2971 struct vmw_sw_context *sw_context,
2972 SVGA3dCmdHeader *header)
2973{
2974 struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
2975 struct vmw_resource *res;
2976 struct {
2977 SVGA3dCmdHeader header;
2978 SVGA3dCmdDXDefineStreamOutputWithMob body;
2979 } *cmd = container_of(header, typeof(*cmd), header);
2980 int ret;
2981
2982 if (!has_sm5_context(dev_priv))
2983 return -EINVAL;
2984
2985 if (!ctx_node) {
2986 DRM_ERROR("DX Context not set.\n");
2987 return -EINVAL;
2988 }
2989
2990 res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_STREAMOUTPUT);
2991 ret = vmw_cotable_notify(res, cmd->body.soid);
2992 if (ret)
2993 return ret;
2994
2995 return vmw_dx_streamoutput_add(sw_context->man, ctx_node->ctx,
2996 cmd->body.soid,
2997 &sw_context->staged_cmd_res);
2998}
2999
3000static int vmw_cmd_dx_destroy_streamoutput(struct vmw_private *dev_priv,
3001 struct vmw_sw_context *sw_context,
3002 SVGA3dCmdHeader *header)
3003{
3004 struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
3005 struct vmw_resource *res;
3006 struct {
3007 SVGA3dCmdHeader header;
3008 SVGA3dCmdDXDestroyStreamOutput body;
3009 } *cmd = container_of(header, typeof(*cmd), header);
3010
3011 if (!ctx_node) {
3012 DRM_ERROR("DX Context not set.\n");
3013 return -EINVAL;
3014 }
3015
3016 /*
3017 * When device does not support SM5 then streamoutput with mob command is
3018 * not available to user-space. Simply return in this case.
3019 */
3020 if (!has_sm5_context(dev_priv))
3021 return 0;
3022
3023 /*
3024 * With SM5 capable device if lookup fails then user-space probably used
3025 * old streamoutput define command. Return without an error.
3026 */
3027 res = vmw_dx_streamoutput_lookup(vmw_context_res_man(ctx_node->ctx),
3028 cmd->body.soid);
3029 if (IS_ERR(res))
3030 return 0;
3031
3032 return vmw_dx_streamoutput_remove(sw_context->man, cmd->body.soid,
3033 &sw_context->staged_cmd_res);
3034}
3035
3036static int vmw_cmd_dx_bind_streamoutput(struct vmw_private *dev_priv,
3037 struct vmw_sw_context *sw_context,
3038 SVGA3dCmdHeader *header)
3039{
3040 struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
3041 struct vmw_resource *res;
3042 struct {
3043 SVGA3dCmdHeader header;
3044 SVGA3dCmdDXBindStreamOutput body;
3045 } *cmd = container_of(header, typeof(*cmd), header);
3046 int ret;
3047
3048 if (!has_sm5_context(dev_priv))
3049 return -EINVAL;
3050
3051 if (!ctx_node) {
3052 DRM_ERROR("DX Context not set.\n");
3053 return -EINVAL;
3054 }
3055
3056 res = vmw_dx_streamoutput_lookup(vmw_context_res_man(ctx_node->ctx),
3057 cmd->body.soid);
3058 if (IS_ERR(res)) {
Colin Ian King1ae96fc2020-08-05 12:31:55 +01003059 DRM_ERROR("Could not find streamoutput to bind.\n");
Deepak Rawate8bead92018-12-13 14:04:31 -08003060 return PTR_ERR(res);
3061 }
3062
3063 vmw_dx_streamoutput_set_size(res, cmd->body.sizeInBytes);
3064
3065 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
3066 VMW_RES_DIRTY_NONE);
3067 if (ret) {
3068 DRM_ERROR("Error creating resource validation node.\n");
3069 return ret;
3070 }
3071
3072 return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
3073 &cmd->body.mobid,
3074 cmd->body.offsetInBytes);
3075}
3076
3077static int vmw_cmd_dx_set_streamoutput(struct vmw_private *dev_priv,
3078 struct vmw_sw_context *sw_context,
3079 SVGA3dCmdHeader *header)
3080{
3081 struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
3082 struct vmw_resource *res;
3083 struct vmw_ctx_bindinfo_so binding;
3084 struct {
3085 SVGA3dCmdHeader header;
3086 SVGA3dCmdDXSetStreamOutput body;
3087 } *cmd = container_of(header, typeof(*cmd), header);
3088 int ret;
3089
3090 if (!ctx_node) {
3091 DRM_ERROR("DX Context not set.\n");
3092 return -EINVAL;
3093 }
3094
3095 if (cmd->body.soid == SVGA3D_INVALID_ID)
3096 return 0;
3097
3098 /*
3099 * When device does not support SM5 then streamoutput with mob command is
3100 * not available to user-space. Simply return in this case.
3101 */
3102 if (!has_sm5_context(dev_priv))
3103 return 0;
3104
3105 /*
3106 * With SM5 capable device if lookup fails then user-space probably used
3107 * old streamoutput define command. Return without an error.
3108 */
3109 res = vmw_dx_streamoutput_lookup(vmw_context_res_man(ctx_node->ctx),
3110 cmd->body.soid);
3111 if (IS_ERR(res)) {
3112 return 0;
3113 }
3114
3115 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
3116 VMW_RES_DIRTY_NONE);
3117 if (ret) {
3118 DRM_ERROR("Error creating resource validation node.\n");
3119 return ret;
3120 }
3121
3122 binding.bi.ctx = ctx_node->ctx;
3123 binding.bi.res = res;
3124 binding.bi.bt = vmw_ctx_binding_so;
3125 binding.slot = 0; /* Only one SO set to context at a time. */
3126
3127 vmw_binding_add(sw_context->dx_ctx_node->staged, &binding.bi, 0,
3128 binding.slot);
3129
3130 return ret;
3131}
3132
Deepak Rawatb6fad732018-12-13 14:00:18 -08003133static int vmw_cmd_indexed_instanced_indirect(struct vmw_private *dev_priv,
3134 struct vmw_sw_context *sw_context,
3135 SVGA3dCmdHeader *header)
3136{
3137 struct vmw_draw_indexed_instanced_indirect_cmd {
3138 SVGA3dCmdHeader header;
3139 SVGA3dCmdDXDrawIndexedInstancedIndirect body;
3140 } *cmd = container_of(header, typeof(*cmd), header);
3141
3142 if (!has_sm5_context(dev_priv))
3143 return -EINVAL;
3144
3145 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3146 VMW_RES_DIRTY_NONE, user_surface_converter,
3147 &cmd->body.argsBufferSid, NULL);
3148}
3149
3150static int vmw_cmd_instanced_indirect(struct vmw_private *dev_priv,
3151 struct vmw_sw_context *sw_context,
3152 SVGA3dCmdHeader *header)
3153{
3154 struct vmw_draw_instanced_indirect_cmd {
3155 SVGA3dCmdHeader header;
3156 SVGA3dCmdDXDrawInstancedIndirect body;
3157 } *cmd = container_of(header, typeof(*cmd), header);
3158
3159 if (!has_sm5_context(dev_priv))
3160 return -EINVAL;
3161
3162 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3163 VMW_RES_DIRTY_NONE, user_surface_converter,
3164 &cmd->body.argsBufferSid, NULL);
3165}
3166
3167static int vmw_cmd_dispatch_indirect(struct vmw_private *dev_priv,
3168 struct vmw_sw_context *sw_context,
3169 SVGA3dCmdHeader *header)
3170{
3171 struct vmw_dispatch_indirect_cmd {
3172 SVGA3dCmdHeader header;
3173 SVGA3dCmdDXDispatchIndirect body;
3174 } *cmd = container_of(header, typeof(*cmd), header);
3175
3176 if (!has_sm5_context(dev_priv))
3177 return -EINVAL;
3178
3179 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
3180 VMW_RES_DIRTY_NONE, user_surface_converter,
3181 &cmd->body.argsBufferSid, NULL);
3182}
3183
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003184static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
3185 struct vmw_sw_context *sw_context,
3186 void *buf, uint32_t *size)
3187{
3188 uint32_t size_remaining = *size;
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003189 uint32_t cmd_id;
3190
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003191 cmd_id = ((uint32_t *)buf)[0];
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003192 switch (cmd_id) {
3193 case SVGA_CMD_UPDATE:
3194 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003195 break;
3196 case SVGA_CMD_DEFINE_GMRFB:
3197 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
3198 break;
3199 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3200 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3201 break;
3202 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3203 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3204 break;
3205 default:
Deepak Rawat5724f892019-02-11 11:46:27 -08003206 VMW_DEBUG_USER("Unsupported SVGA command: %u.\n", cmd_id);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003207 return -EINVAL;
3208 }
3209
3210 if (*size > size_remaining) {
Deepak Rawat5724f892019-02-11 11:46:27 -08003211 VMW_DEBUG_USER("Invalid SVGA command (size mismatch): %u.\n",
3212 cmd_id);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003213 return -EINVAL;
3214 }
3215
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02003216 if (unlikely(!sw_context->kernel)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08003217 VMW_DEBUG_USER("Kernel only SVGA command: %u.\n", cmd_id);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003218 return -EPERM;
3219 }
3220
3221 if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
3222 return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
3223
3224 return 0;
3225}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003226
Thomas Hellstrom4fbd9d22014-02-12 12:37:01 +01003227static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003228 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
3229 false, false, false),
3230 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
3231 false, false, false),
3232 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
3233 true, false, false),
3234 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
3235 true, false, false),
3236 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
3237 true, false, false),
3238 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
3239 false, false, false),
3240 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
3241 false, false, false),
3242 VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
3243 true, false, false),
3244 VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
3245 true, false, false),
3246 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
3247 true, false, false),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003248 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003249 &vmw_cmd_set_render_target_check, true, false, false),
3250 VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
3251 true, false, false),
3252 VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
3253 true, false, false),
3254 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
3255 true, false, false),
3256 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
3257 true, false, false),
3258 VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
3259 true, false, false),
3260 VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
3261 true, false, false),
3262 VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
3263 true, false, false),
3264 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
3265 false, false, false),
Thomas Hellstromd5bde952014-01-31 10:12:10 +01003266 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_shader_define,
3267 true, false, false),
3268 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_shader_destroy,
3269 true, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003270 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
3271 true, false, false),
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01003272 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_set_shader_const,
3273 true, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003274 VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
3275 true, false, false),
3276 VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
3277 true, false, false),
3278 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
3279 true, false, false),
3280 VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
3281 true, false, false),
3282 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
3283 true, false, false),
3284 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
3285 true, false, false),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003286 VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003287 &vmw_cmd_blt_surf_screen_check, false, false, false),
3288 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
3289 false, false, false),
3290 VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
3291 false, false, false),
3292 VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
3293 false, false, false),
3294 VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
3295 false, false, false),
3296 VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
3297 false, false, false),
Deepak Rawatdc75e732018-06-13 13:53:28 -07003298 VMW_CMD_DEF(SVGA_3D_CMD_DEAD1, &vmw_cmd_invalid,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003299 false, false, false),
Deepak Rawatdc75e732018-06-13 13:53:28 -07003300 VMW_CMD_DEF(SVGA_3D_CMD_DEAD2, &vmw_cmd_invalid,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003301 false, false, false),
Deepak Rawat3d143952018-12-13 11:55:57 -08003302 VMW_CMD_DEF(SVGA_3D_CMD_DEAD12, &vmw_cmd_invalid, false, false, false),
3303 VMW_CMD_DEF(SVGA_3D_CMD_DEAD13, &vmw_cmd_invalid, false, false, false),
3304 VMW_CMD_DEF(SVGA_3D_CMD_DEAD14, &vmw_cmd_invalid, false, false, false),
3305 VMW_CMD_DEF(SVGA_3D_CMD_DEAD15, &vmw_cmd_invalid, false, false, false),
3306 VMW_CMD_DEF(SVGA_3D_CMD_DEAD16, &vmw_cmd_invalid, false, false, false),
3307 VMW_CMD_DEF(SVGA_3D_CMD_DEAD17, &vmw_cmd_invalid, false, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003308 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
3309 false, false, true),
3310 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
3311 false, false, true),
3312 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
3313 false, false, true),
3314 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
3315 false, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003316 VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB64, &vmw_cmd_invalid,
3317 false, false, true),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003318 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
3319 false, false, true),
3320 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
3321 false, false, true),
3322 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
3323 false, false, true),
3324 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
3325 true, false, true),
3326 VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
3327 false, false, true),
3328 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
3329 true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003330 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003331 &vmw_cmd_update_gb_surface, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003332 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003333 &vmw_cmd_readback_gb_image, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003334 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003335 &vmw_cmd_readback_gb_surface, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003336 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003337 &vmw_cmd_invalidate_gb_image, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003338 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003339 &vmw_cmd_invalidate_gb_surface, true, false, true),
3340 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
3341 false, false, true),
3342 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
3343 false, false, true),
3344 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
3345 false, false, true),
3346 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
3347 false, false, true),
3348 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
3349 false, false, true),
3350 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
3351 false, false, true),
3352 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
3353 true, false, true),
3354 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
3355 false, false, true),
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +01003356 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
Thomas Hellstrom8ba07312013-10-08 02:25:35 -07003357 false, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003358 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
3359 true, false, true),
3360 VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
3361 true, false, true),
3362 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
3363 true, false, true),
3364 VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
3365 true, false, true),
Thomas Hellstrom5f55be5f2017-08-24 08:06:30 +02003366 VMW_CMD_DEF(SVGA_3D_CMD_NOP_ERROR, &vmw_cmd_ok,
3367 true, false, true),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003368 VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
3369 false, false, true),
3370 VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
3371 false, false, true),
3372 VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
3373 false, false, true),
3374 VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
3375 false, false, true),
3376 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
3377 false, false, true),
3378 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
3379 false, false, true),
3380 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
3381 false, false, true),
3382 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
3383 false, false, true),
3384 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3385 false, false, true),
3386 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3387 false, false, true),
3388 VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003389 true, false, true),
3390 VMW_CMD_DEF(SVGA_3D_CMD_GB_SCREEN_DMA, &vmw_cmd_invalid,
3391 false, false, true),
3392 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE_WITH_PITCH, &vmw_cmd_invalid,
3393 false, false, true),
3394 VMW_CMD_DEF(SVGA_3D_CMD_GB_MOB_FENCE, &vmw_cmd_invalid,
3395 false, false, true),
3396 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V2, &vmw_cmd_invalid,
3397 false, false, true),
3398
Deepak Rawat680360a2019-02-13 13:20:42 -08003399 /* SM commands */
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003400 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_CONTEXT, &vmw_cmd_invalid,
3401 false, false, true),
3402 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_CONTEXT, &vmw_cmd_invalid,
3403 false, false, true),
3404 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_CONTEXT, &vmw_cmd_invalid,
3405 false, false, true),
3406 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_CONTEXT, &vmw_cmd_invalid,
3407 false, false, true),
3408 VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_CONTEXT, &vmw_cmd_invalid,
3409 false, false, true),
3410 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER,
3411 &vmw_cmd_dx_set_single_constant_buffer, true, false, true),
3412 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER_RESOURCES,
3413 &vmw_cmd_dx_set_shader_res, true, false, true),
3414 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER, &vmw_cmd_dx_set_shader,
3415 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003416 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SAMPLERS, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003417 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003418 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003419 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003420 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED, &vmw_cmd_dx_cid_check,
3421 true, false, true),
3422 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INSTANCED, &vmw_cmd_dx_cid_check,
3423 true, false, true),
3424 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED_INSTANCED,
3425 &vmw_cmd_dx_cid_check, true, false, true),
3426 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_AUTO, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003427 true, false, true),
3428 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS,
3429 &vmw_cmd_dx_set_vertex_buffers, true, false, true),
3430 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INDEX_BUFFER,
3431 &vmw_cmd_dx_set_index_buffer, true, false, true),
3432 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RENDERTARGETS,
3433 &vmw_cmd_dx_set_rendertargets, true, false, true),
3434 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_BLEND_STATE, &vmw_cmd_dx_cid_check,
3435 true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003436 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_DEPTHSTENCIL_STATE,
Charmaine Lee2f633e52015-08-10 10:45:11 -07003437 &vmw_cmd_dx_cid_check, true, false, true),
3438 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RASTERIZER_STATE,
3439 &vmw_cmd_dx_cid_check, true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003440 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_QUERY, &vmw_cmd_dx_define_query,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003441 true, false, true),
Charmaine Leee02e5882016-04-12 08:19:08 -07003442 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003443 true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003444 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_QUERY, &vmw_cmd_dx_bind_query,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003445 true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003446 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_QUERY_OFFSET,
Charmaine Leee02e5882016-04-12 08:19:08 -07003447 &vmw_cmd_dx_cid_check, true, false, true),
3448 VMW_CMD_DEF(SVGA_3D_CMD_DX_BEGIN_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003449 true, false, true),
Charmaine Leee02e5882016-04-12 08:19:08 -07003450 VMW_CMD_DEF(SVGA_3D_CMD_DX_END_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003451 true, false, true),
3452 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_QUERY, &vmw_cmd_invalid,
3453 true, false, true),
Charmaine Lee18835982016-04-12 08:14:23 -07003454 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_PREDICATION, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003455 true, false, true),
3456 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VIEWPORTS, &vmw_cmd_dx_cid_check,
3457 true, false, true),
3458 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SCISSORRECTS, &vmw_cmd_dx_cid_check,
3459 true, false, true),
3460 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW,
3461 &vmw_cmd_dx_clear_rendertarget_view, true, false, true),
3462 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW,
3463 &vmw_cmd_dx_clear_depthstencil_view, true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003464 VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY, &vmw_cmd_invalid,
3465 true, false, true),
Charmaine Leef3b335502016-02-12 08:11:56 +01003466 VMW_CMD_DEF(SVGA_3D_CMD_DX_GENMIPS, &vmw_cmd_dx_genmips,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003467 true, false, true),
3468 VMW_CMD_DEF(SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE,
3469 &vmw_cmd_dx_check_subresource, true, false, true),
3470 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_SUBRESOURCE,
3471 &vmw_cmd_dx_check_subresource, true, false, true),
3472 VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_SUBRESOURCE,
3473 &vmw_cmd_dx_check_subresource, true, false, true),
3474 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW,
3475 &vmw_cmd_dx_view_define, true, false, true),
3476 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
3477 &vmw_cmd_dx_view_remove, true, false, true),
3478 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW,
3479 &vmw_cmd_dx_view_define, true, false, true),
3480 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
3481 &vmw_cmd_dx_view_remove, true, false, true),
3482 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW,
3483 &vmw_cmd_dx_view_define, true, false, true),
3484 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
3485 &vmw_cmd_dx_view_remove, true, false, true),
3486 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_ELEMENTLAYOUT,
3487 &vmw_cmd_dx_so_define, true, false, true),
3488 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_ELEMENTLAYOUT,
3489 &vmw_cmd_dx_cid_check, true, false, true),
3490 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_BLEND_STATE,
3491 &vmw_cmd_dx_so_define, true, false, true),
3492 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_BLEND_STATE,
3493 &vmw_cmd_dx_cid_check, true, false, true),
3494 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_STATE,
3495 &vmw_cmd_dx_so_define, true, false, true),
3496 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_STATE,
3497 &vmw_cmd_dx_cid_check, true, false, true),
3498 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RASTERIZER_STATE,
3499 &vmw_cmd_dx_so_define, true, false, true),
3500 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RASTERIZER_STATE,
3501 &vmw_cmd_dx_cid_check, true, false, true),
3502 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SAMPLER_STATE,
3503 &vmw_cmd_dx_so_define, true, false, true),
3504 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SAMPLER_STATE,
3505 &vmw_cmd_dx_cid_check, true, false, true),
3506 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADER,
3507 &vmw_cmd_dx_define_shader, true, false, true),
3508 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADER,
3509 &vmw_cmd_dx_destroy_shader, true, false, true),
3510 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_SHADER,
3511 &vmw_cmd_dx_bind_shader, true, false, true),
3512 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_STREAMOUTPUT,
3513 &vmw_cmd_dx_so_define, true, false, true),
3514 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_STREAMOUTPUT,
Deepak Rawate8bead92018-12-13 14:04:31 -08003515 &vmw_cmd_dx_destroy_streamoutput, true, false, true),
3516 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_STREAMOUTPUT,
3517 &vmw_cmd_dx_set_streamoutput, true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003518 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SOTARGETS,
3519 &vmw_cmd_dx_set_so_targets, true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003520 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INPUT_LAYOUT,
3521 &vmw_cmd_dx_cid_check, true, false, true),
3522 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_TOPOLOGY,
3523 &vmw_cmd_dx_cid_check, true, false, true),
Neha Bhende0fca749e2015-08-10 10:51:07 -07003524 VMW_CMD_DEF(SVGA_3D_CMD_DX_BUFFER_COPY,
3525 &vmw_cmd_buffer_copy_check, true, false, true),
3526 VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY_REGION,
3527 &vmw_cmd_pred_copy_check, true, false, true),
Charmaine Lee1f982e42016-10-10 10:37:03 -07003528 VMW_CMD_DEF(SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER,
3529 &vmw_cmd_dx_transfer_from_buffer,
3530 true, false, true),
Neha Bhende0d81d342018-06-18 17:14:56 -07003531 VMW_CMD_DEF(SVGA_3D_CMD_INTRA_SURFACE_COPY, &vmw_cmd_intra_surface_copy,
3532 true, false, true),
Deepak Rawat5e8ec0d2018-12-13 13:51:08 -08003533
3534 /*
3535 * SM5 commands
3536 */
3537 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_UA_VIEW, &vmw_cmd_sm5_view_define,
3538 true, false, true),
3539 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_UA_VIEW, &vmw_cmd_sm5_view_remove,
3540 true, false, true),
3541 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_UA_VIEW_UINT, &vmw_cmd_clear_uav_uint,
3542 true, false, true),
3543 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_UA_VIEW_FLOAT,
3544 &vmw_cmd_clear_uav_float, true, false, true),
3545 VMW_CMD_DEF(SVGA_3D_CMD_DX_COPY_STRUCTURE_COUNT, &vmw_cmd_invalid, true,
3546 false, true),
3547 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_UA_VIEWS, &vmw_cmd_set_uav, true, false,
3548 true),
Deepak Rawatb6fad732018-12-13 14:00:18 -08003549 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED_INSTANCED_INDIRECT,
3550 &vmw_cmd_indexed_instanced_indirect, true, false, true),
3551 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INSTANCED_INDIRECT,
3552 &vmw_cmd_instanced_indirect, true, false, true),
3553 VMW_CMD_DEF(SVGA_3D_CMD_DX_DISPATCH, &vmw_cmd_sm5, true, false, true),
3554 VMW_CMD_DEF(SVGA_3D_CMD_DX_DISPATCH_INDIRECT,
3555 &vmw_cmd_dispatch_indirect, true, false, true),
Deepak Rawat5e8ec0d2018-12-13 13:51:08 -08003556 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_CS_UA_VIEWS, &vmw_cmd_set_cs_uav, true,
3557 false, true),
Deepak Rawatb6fad732018-12-13 14:00:18 -08003558 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW_V2,
3559 &vmw_cmd_sm5_view_define, true, false, true),
Deepak Rawate8bead92018-12-13 14:04:31 -08003560 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_STREAMOUTPUT_WITH_MOB,
3561 &vmw_cmd_dx_define_streamoutput, true, false, true),
3562 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_STREAMOUTPUT,
3563 &vmw_cmd_dx_bind_streamoutput, true, false, true),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003564};
3565
Thomas Hellstrom65b97a22017-08-24 08:06:29 +02003566bool vmw_cmd_describe(const void *buf, u32 *size, char const **cmd)
3567{
3568 u32 cmd_id = ((u32 *) buf)[0];
3569
3570 if (cmd_id >= SVGA_CMD_MAX) {
3571 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3572 const struct vmw_cmd_entry *entry;
3573
3574 *size = header->size + sizeof(SVGA3dCmdHeader);
3575 cmd_id = header->id;
3576 if (cmd_id >= SVGA_3D_CMD_MAX)
3577 return false;
3578
3579 cmd_id -= SVGA_3D_CMD_BASE;
3580 entry = &vmw_cmd_entries[cmd_id];
3581 *cmd = entry->cmd_name;
3582 return true;
3583 }
3584
3585 switch (cmd_id) {
3586 case SVGA_CMD_UPDATE:
3587 *cmd = "SVGA_CMD_UPDATE";
3588 *size = sizeof(u32) + sizeof(SVGAFifoCmdUpdate);
3589 break;
3590 case SVGA_CMD_DEFINE_GMRFB:
3591 *cmd = "SVGA_CMD_DEFINE_GMRFB";
3592 *size = sizeof(u32) + sizeof(SVGAFifoCmdDefineGMRFB);
3593 break;
3594 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3595 *cmd = "SVGA_CMD_BLIT_GMRFB_TO_SCREEN";
3596 *size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3597 break;
3598 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3599 *cmd = "SVGA_CMD_BLIT_SCREEN_TO_GMRFB";
3600 *size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3601 break;
3602 default:
3603 *cmd = "UNKNOWN";
3604 *size = 0;
3605 return false;
3606 }
3607
3608 return true;
3609}
3610
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003611static int vmw_cmd_check(struct vmw_private *dev_priv,
Deepak Rawat680360a2019-02-13 13:20:42 -08003612 struct vmw_sw_context *sw_context, void *buf,
3613 uint32_t *size)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003614{
3615 uint32_t cmd_id;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003616 uint32_t size_remaining = *size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003617 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3618 int ret;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003619 const struct vmw_cmd_entry *entry;
3620 bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003621
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003622 cmd_id = ((uint32_t *)buf)[0];
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003623 /* Handle any none 3D commands */
3624 if (unlikely(cmd_id < SVGA_CMD_MAX))
3625 return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
3626
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003627
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003628 cmd_id = header->id;
3629 *size = header->size + sizeof(SVGA3dCmdHeader);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003630
3631 cmd_id -= SVGA_3D_CMD_BASE;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003632 if (unlikely(*size > size_remaining))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003633 goto out_invalid;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003634
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003635 if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003636 goto out_invalid;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003637
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003638 entry = &vmw_cmd_entries[cmd_id];
Thomas Hellstrom36e952c2014-02-12 13:19:36 +01003639 if (unlikely(!entry->func))
3640 goto out_invalid;
3641
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003642 if (unlikely(!entry->user_allow && !sw_context->kernel))
3643 goto out_privileged;
3644
3645 if (unlikely(entry->gb_disable && gb))
3646 goto out_old;
3647
3648 if (unlikely(entry->gb_enable && !gb))
3649 goto out_new;
3650
3651 ret = entry->func(dev_priv, sw_context, header);
Deepak Rawat45399b12019-02-11 12:57:38 -08003652 if (unlikely(ret != 0)) {
3653 VMW_DEBUG_USER("SVGA3D command: %d failed with error %d\n",
3654 cmd_id + SVGA_3D_CMD_BASE, ret);
3655 return ret;
3656 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003657
3658 return 0;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003659out_invalid:
Deepak Rawat5724f892019-02-11 11:46:27 -08003660 VMW_DEBUG_USER("Invalid SVGA3D command: %d\n",
3661 cmd_id + SVGA_3D_CMD_BASE);
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003662 return -EINVAL;
3663out_privileged:
Deepak Rawat5724f892019-02-11 11:46:27 -08003664 VMW_DEBUG_USER("Privileged SVGA3D command: %d\n",
3665 cmd_id + SVGA_3D_CMD_BASE);
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003666 return -EPERM;
3667out_old:
Deepak Rawat5724f892019-02-11 11:46:27 -08003668 VMW_DEBUG_USER("Deprecated (disallowed) SVGA3D command: %d\n",
3669 cmd_id + SVGA_3D_CMD_BASE);
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003670 return -EINVAL;
3671out_new:
Deepak Rawat5724f892019-02-11 11:46:27 -08003672 VMW_DEBUG_USER("SVGA3D command: %d not supported by virtual device.\n",
3673 cmd_id + SVGA_3D_CMD_BASE);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003674 return -EINVAL;
3675}
3676
3677static int vmw_cmd_check_all(struct vmw_private *dev_priv,
Deepak Rawat680360a2019-02-13 13:20:42 -08003678 struct vmw_sw_context *sw_context, void *buf,
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003679 uint32_t size)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003680{
3681 int32_t cur_size = size;
3682 int ret;
3683
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003684 sw_context->buf_start = buf;
3685
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003686 while (cur_size > 0) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003687 size = cur_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003688 ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
3689 if (unlikely(ret != 0))
3690 return ret;
3691 buf = (void *)((unsigned long) buf + size);
3692 cur_size -= size;
3693 }
3694
3695 if (unlikely(cur_size != 0)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08003696 VMW_DEBUG_USER("Command verifier out of sync.\n");
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003697 return -EINVAL;
3698 }
3699
3700 return 0;
3701}
3702
3703static void vmw_free_relocations(struct vmw_sw_context *sw_context)
3704{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003705 /* Memory is validation context memory, so no need to free it */
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003706 INIT_LIST_HEAD(&sw_context->bo_relocations);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003707}
3708
3709static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
3710{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003711 struct vmw_relocation *reloc;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003712 struct ttm_buffer_object *bo;
3713
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003714 list_for_each_entry(reloc, &sw_context->bo_relocations, head) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003715 bo = &reloc->vbo->base;
Christian Königd3116752021-04-12 15:11:47 +02003716 switch (bo->resource->mem_type) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003717 case TTM_PL_VRAM:
Christian Königd3116752021-04-12 15:11:47 +02003718 reloc->location->offset += bo->resource->start << PAGE_SHIFT;
Thomas Hellstrom135cba02010-10-26 21:21:47 +02003719 reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003720 break;
3721 case VMW_PL_GMR:
Christian Königd3116752021-04-12 15:11:47 +02003722 reloc->location->gmrId = bo->resource->start;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003723 break;
Thomas Hellstromddcda242012-11-21 11:26:55 +01003724 case VMW_PL_MOB:
Christian Königd3116752021-04-12 15:11:47 +02003725 *reloc->mob_loc = bo->resource->start;
Thomas Hellstromddcda242012-11-21 11:26:55 +01003726 break;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003727 default:
3728 BUG();
3729 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003730 }
3731 vmw_free_relocations(sw_context);
3732}
3733
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003734static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
3735 uint32_t size)
3736{
3737 if (likely(sw_context->cmd_bounce_size >= size))
3738 return 0;
3739
3740 if (sw_context->cmd_bounce_size == 0)
3741 sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
3742
3743 while (sw_context->cmd_bounce_size < size) {
3744 sw_context->cmd_bounce_size =
3745 PAGE_ALIGN(sw_context->cmd_bounce_size +
3746 (sw_context->cmd_bounce_size >> 1));
3747 }
3748
Markus Elfring0bc32992016-07-22 13:31:00 +02003749 vfree(sw_context->cmd_bounce);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003750 sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
3751
3752 if (sw_context->cmd_bounce == NULL) {
Deepak Rawat5724f892019-02-11 11:46:27 -08003753 VMW_DEBUG_USER("Failed to allocate command bounce buffer.\n");
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003754 sw_context->cmd_bounce_size = 0;
3755 return -ENOMEM;
3756 }
3757
3758 return 0;
3759}
3760
Lee Jones7450bf72021-01-15 18:12:36 +00003761/*
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003762 * vmw_execbuf_fence_commands - create and submit a command stream fence
3763 *
3764 * Creates a fence object and submits a command stream marker.
3765 * If this fails for some reason, We sync the fifo and return NULL.
3766 * It is then safe to fence buffers with a NULL pointer.
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +02003767 *
Deepak Rawat680360a2019-02-13 13:20:42 -08003768 * If @p_handle is not NULL @file_priv must also not be NULL. Creates a
3769 * userspace handle if @p_handle is not NULL, otherwise not.
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003770 */
3771
3772int vmw_execbuf_fence_commands(struct drm_file *file_priv,
3773 struct vmw_private *dev_priv,
3774 struct vmw_fence_obj **p_fence,
3775 uint32_t *p_handle)
3776{
3777 uint32_t sequence;
3778 int ret;
3779 bool synced = false;
3780
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +02003781 /* p_handle implies file_priv. */
3782 BUG_ON(p_handle != NULL && file_priv == NULL);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003783
Zack Rusin8426ed92020-11-18 12:54:19 -05003784 ret = vmw_cmd_send_fence(dev_priv, &sequence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003785 if (unlikely(ret != 0)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08003786 VMW_DEBUG_USER("Fence submission error. Syncing.\n");
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003787 synced = true;
3788 }
3789
3790 if (p_handle != NULL)
3791 ret = vmw_user_fence_create(file_priv, dev_priv->fman,
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003792 sequence, p_fence, p_handle);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003793 else
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003794 ret = vmw_fence_create(dev_priv->fman, sequence, p_fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003795
3796 if (unlikely(ret != 0 && !synced)) {
Deepak Rawat680360a2019-02-13 13:20:42 -08003797 (void) vmw_fallback_wait(dev_priv, false, false, sequence,
3798 false, VMW_FENCE_WAIT_TIMEOUT);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003799 *p_fence = NULL;
3800 }
3801
Thomas Hellstrom728354c2019-01-31 10:55:37 +01003802 return ret;
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003803}
3804
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003805/**
Deepak Rawat680360a2019-02-13 13:20:42 -08003806 * vmw_execbuf_copy_fence_user - copy fence object information to user-space.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003807 *
3808 * @dev_priv: Pointer to a vmw_private struct.
3809 * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
3810 * @ret: Return value from fence object creation.
Deepak Rawat680360a2019-02-13 13:20:42 -08003811 * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to which
3812 * the information should be copied.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003813 * @fence: Pointer to the fenc object.
3814 * @fence_handle: User-space fence handle.
Sinclair Yehc906965d2017-07-05 01:49:32 -07003815 * @out_fence_fd: exported file descriptor for the fence. -1 if not used
3816 * @sync_file: Only used to clean up in case of an error in this function.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003817 *
Deepak Rawat680360a2019-02-13 13:20:42 -08003818 * This function copies fence information to user-space. If copying fails, the
3819 * user-space struct drm_vmw_fence_rep::error member is hopefully left
3820 * untouched, and if it's preloaded with an -EFAULT by user-space, the error
3821 * will hopefully be detected.
3822 *
3823 * Also if copying fails, user-space will be unable to signal the fence object
3824 * so we wait for it immediately, and then unreference the user-space reference.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003825 */
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02003826void
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003827vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
Deepak Rawat680360a2019-02-13 13:20:42 -08003828 struct vmw_fpriv *vmw_fp, int ret,
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003829 struct drm_vmw_fence_rep __user *user_fence_rep,
Deepak Rawat680360a2019-02-13 13:20:42 -08003830 struct vmw_fence_obj *fence, uint32_t fence_handle,
3831 int32_t out_fence_fd, struct sync_file *sync_file)
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003832{
3833 struct drm_vmw_fence_rep fence_rep;
3834
3835 if (user_fence_rep == NULL)
3836 return;
3837
Dan Carpenter80d9b242011-10-18 09:10:12 +03003838 memset(&fence_rep, 0, sizeof(fence_rep));
3839
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003840 fence_rep.error = ret;
Sinclair Yehc906965d2017-07-05 01:49:32 -07003841 fence_rep.fd = out_fence_fd;
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003842 if (ret == 0) {
3843 BUG_ON(fence == NULL);
3844
3845 fence_rep.handle = fence_handle;
Maarten Lankhorst2298e802014-03-26 14:07:44 +01003846 fence_rep.seqno = fence->base.seqno;
Zack Rusin2cd80db2021-05-05 15:10:07 -04003847 vmw_update_seqno(dev_priv);
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003848 fence_rep.passed_seqno = dev_priv->last_read_seqno;
3849 }
3850
3851 /*
Deepak Rawat680360a2019-02-13 13:20:42 -08003852 * copy_to_user errors will be detected by user space not seeing
3853 * fence_rep::error filled in. Typically user-space would have pre-set
3854 * that member to -EFAULT.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003855 */
3856 ret = copy_to_user(user_fence_rep, &fence_rep,
3857 sizeof(fence_rep));
3858
3859 /*
Deepak Rawat680360a2019-02-13 13:20:42 -08003860 * User-space lost the fence object. We need to sync and unreference the
3861 * handle.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003862 */
3863 if (unlikely(ret != 0) && (fence_rep.error == 0)) {
Sinclair Yehc906965d2017-07-05 01:49:32 -07003864 if (sync_file)
3865 fput(sync_file->file);
3866
3867 if (fence_rep.fd != -1) {
3868 put_unused_fd(fence_rep.fd);
3869 fence_rep.fd = -1;
3870 }
3871
Deepak Rawat680360a2019-02-13 13:20:42 -08003872 ttm_ref_object_base_unref(vmw_fp->tfile, fence_handle,
3873 TTM_REF_USAGE);
Deepak Rawat5724f892019-02-11 11:46:27 -08003874 VMW_DEBUG_USER("Fence copy error. Syncing.\n");
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003875 (void) vmw_fence_obj_wait(fence, false, false,
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003876 VMW_FENCE_WAIT_TIMEOUT);
3877 }
3878}
3879
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003880/**
Deepak Rawat680360a2019-02-13 13:20:42 -08003881 * vmw_execbuf_submit_fifo - Patch a command batch and submit it using the fifo.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003882 *
3883 * @dev_priv: Pointer to a device private structure.
3884 * @kernel_commands: Pointer to the unpatched command batch.
3885 * @command_size: Size of the unpatched command batch.
3886 * @sw_context: Structure holding the relocation lists.
3887 *
Deepak Rawat680360a2019-02-13 13:20:42 -08003888 * Side effects: If this function returns 0, then the command batch pointed to
3889 * by @kernel_commands will have been modified.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003890 */
3891static int vmw_execbuf_submit_fifo(struct vmw_private *dev_priv,
Deepak Rawat680360a2019-02-13 13:20:42 -08003892 void *kernel_commands, u32 command_size,
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003893 struct vmw_sw_context *sw_context)
3894{
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003895 void *cmd;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003896
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003897 if (sw_context->dx_ctx_node)
Zack Rusin8426ed92020-11-18 12:54:19 -05003898 cmd = VMW_CMD_CTX_RESERVE(dev_priv, command_size,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003899 sw_context->dx_ctx_node->ctx->id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003900 else
Zack Rusin8426ed92020-11-18 12:54:19 -05003901 cmd = VMW_CMD_RESERVE(dev_priv, command_size);
Deepak Rawat11c45412019-02-14 16:15:39 -08003902
3903 if (!cmd)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003904 return -ENOMEM;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003905
3906 vmw_apply_relocations(sw_context);
3907 memcpy(cmd, kernel_commands, command_size);
3908 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
3909 vmw_resource_relocations_free(&sw_context->res_relocations);
Zack Rusin8426ed92020-11-18 12:54:19 -05003910 vmw_cmd_commit(dev_priv, command_size);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003911
3912 return 0;
3913}
3914
3915/**
Deepak Rawat680360a2019-02-13 13:20:42 -08003916 * vmw_execbuf_submit_cmdbuf - Patch a command batch and submit it using the
3917 * command buffer manager.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003918 *
3919 * @dev_priv: Pointer to a device private structure.
3920 * @header: Opaque handle to the command buffer allocation.
3921 * @command_size: Size of the unpatched command batch.
3922 * @sw_context: Structure holding the relocation lists.
3923 *
Deepak Rawat680360a2019-02-13 13:20:42 -08003924 * Side effects: If this function returns 0, then the command buffer represented
3925 * by @header will have been modified.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003926 */
3927static int vmw_execbuf_submit_cmdbuf(struct vmw_private *dev_priv,
3928 struct vmw_cmdbuf_header *header,
3929 u32 command_size,
3930 struct vmw_sw_context *sw_context)
3931{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003932 u32 id = ((sw_context->dx_ctx_node) ? sw_context->dx_ctx_node->ctx->id :
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003933 SVGA3D_INVALID_ID);
Deepak Rawat680360a2019-02-13 13:20:42 -08003934 void *cmd = vmw_cmdbuf_reserve(dev_priv->cman, command_size, id, false,
3935 header);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003936
3937 vmw_apply_relocations(sw_context);
3938 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
3939 vmw_resource_relocations_free(&sw_context->res_relocations);
3940 vmw_cmdbuf_commit(dev_priv->cman, command_size, header, false);
3941
3942 return 0;
3943}
3944
3945/**
3946 * vmw_execbuf_cmdbuf - Prepare, if possible, a user-space command batch for
3947 * submission using a command buffer.
3948 *
3949 * @dev_priv: Pointer to a device private structure.
3950 * @user_commands: User-space pointer to the commands to be submitted.
3951 * @command_size: Size of the unpatched command batch.
3952 * @header: Out parameter returning the opaque pointer to the command buffer.
3953 *
3954 * This function checks whether we can use the command buffer manager for
Deepak Rawat680360a2019-02-13 13:20:42 -08003955 * submission and if so, creates a command buffer of suitable size and copies
3956 * the user data into that buffer.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003957 *
3958 * On successful return, the function returns a pointer to the data in the
3959 * command buffer and *@header is set to non-NULL.
Deepak Rawat680360a2019-02-13 13:20:42 -08003960 *
Lee Jones7450bf72021-01-15 18:12:36 +00003961 * @kernel_commands: If command buffers could not be used, the function will
3962 * return the value of @kernel_commands on function call. That value may be
3963 * NULL. In that case, the value of *@header will be set to NULL.
Deepak Rawat680360a2019-02-13 13:20:42 -08003964 *
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003965 * If an error is encountered, the function will return a pointer error value.
3966 * If the function is interrupted by a signal while sleeping, it will return
3967 * -ERESTARTSYS casted to a pointer error value.
3968 */
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003969static void *vmw_execbuf_cmdbuf(struct vmw_private *dev_priv,
3970 void __user *user_commands,
Deepak Rawat680360a2019-02-13 13:20:42 -08003971 void *kernel_commands, u32 command_size,
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003972 struct vmw_cmdbuf_header **header)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003973{
3974 size_t cmdbuf_size;
3975 int ret;
3976
3977 *header = NULL;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003978 if (command_size > SVGA_CB_MAX_SIZE) {
Deepak Rawat5724f892019-02-11 11:46:27 -08003979 VMW_DEBUG_USER("Command buffer is too large.\n");
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003980 return ERR_PTR(-EINVAL);
3981 }
3982
Thomas Hellstrom51ab70b2016-10-10 10:51:24 -07003983 if (!dev_priv->cman || kernel_commands)
3984 return kernel_commands;
3985
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003986 /* If possible, add a little space for fencing. */
3987 cmdbuf_size = command_size + 512;
3988 cmdbuf_size = min_t(size_t, cmdbuf_size, SVGA_CB_MAX_SIZE);
Deepak Rawat680360a2019-02-13 13:20:42 -08003989 kernel_commands = vmw_cmdbuf_alloc(dev_priv->cman, cmdbuf_size, true,
3990 header);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003991 if (IS_ERR(kernel_commands))
3992 return kernel_commands;
3993
Deepak Rawat680360a2019-02-13 13:20:42 -08003994 ret = copy_from_user(kernel_commands, user_commands, command_size);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003995 if (ret) {
Deepak Rawat5724f892019-02-11 11:46:27 -08003996 VMW_DEBUG_USER("Failed copying commands.\n");
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003997 vmw_cmdbuf_header_free(*header);
3998 *header = NULL;
3999 return ERR_PTR(-EFAULT);
4000 }
4001
4002 return kernel_commands;
4003}
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02004004
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004005static int vmw_execbuf_tie_context(struct vmw_private *dev_priv,
4006 struct vmw_sw_context *sw_context,
4007 uint32_t handle)
4008{
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004009 struct vmw_resource *res;
4010 int ret;
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02004011 unsigned int size;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004012
4013 if (handle == SVGA3D_INVALID_ID)
4014 return 0;
4015
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02004016 size = vmw_execbuf_res_size(dev_priv, vmw_res_dx_context);
4017 ret = vmw_validation_preload_res(sw_context->ctx, size);
4018 if (ret)
4019 return ret;
4020
4021 res = vmw_user_resource_noref_lookup_handle
4022 (dev_priv, sw_context->fp->tfile, handle,
4023 user_context_converter);
Chengguang Xu4efa6662019-03-01 10:14:06 -08004024 if (IS_ERR(res)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08004025 VMW_DEBUG_USER("Could not find or user DX context 0x%08x.\n",
4026 (unsigned int) handle);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02004027 return PTR_ERR(res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004028 }
4029
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01004030 ret = vmw_execbuf_res_noref_val_add(sw_context, res, VMW_RES_DIRTY_SET);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004031 if (unlikely(ret != 0))
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02004032 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004033
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004034 sw_context->dx_ctx_node = vmw_execbuf_info_from_res(sw_context, res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004035 sw_context->man = vmw_context_res_man(res);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02004036
4037 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004038}
4039
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004040int vmw_execbuf_process(struct drm_file *file_priv,
4041 struct vmw_private *dev_priv,
Deepak Rawat680360a2019-02-13 13:20:42 -08004042 void __user *user_commands, void *kernel_commands,
4043 uint32_t command_size, uint64_t throttle_us,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004044 uint32_t dx_context_handle,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01004045 struct drm_vmw_fence_rep __user *user_fence_rep,
Deepak Rawat680360a2019-02-13 13:20:42 -08004046 struct vmw_fence_obj **out_fence, uint32_t flags)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004047{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004048 struct vmw_sw_context *sw_context = &dev_priv->ctx;
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01004049 struct vmw_fence_obj *fence = NULL;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004050 struct vmw_cmdbuf_header *header;
Nathan Chancellora5020f42019-03-11 20:24:46 -07004051 uint32_t handle = 0;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004052 int ret;
Sinclair Yehc906965d2017-07-05 01:49:32 -07004053 int32_t out_fence_fd = -1;
4054 struct sync_file *sync_file = NULL;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004055 DECLARE_VAL_CONTEXT(val_ctx, &sw_context->res_ht, 1);
Sinclair Yehc906965d2017-07-05 01:49:32 -07004056
Thomas Hellstromfd567462018-12-12 11:52:08 +01004057 vmw_validation_set_val_mem(&val_ctx, &dev_priv->vvm);
4058
Sinclair Yehc906965d2017-07-05 01:49:32 -07004059 if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
4060 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
4061 if (out_fence_fd < 0) {
Deepak Rawat5724f892019-02-11 11:46:27 -08004062 VMW_DEBUG_USER("Failed to get a fence fd.\n");
Sinclair Yehc906965d2017-07-05 01:49:32 -07004063 return out_fence_fd;
4064 }
4065 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004066
Charmaine Lee2f633e52015-08-10 10:45:11 -07004067 if (throttle_us) {
Zack Rusin359dc602020-11-10 22:14:46 -05004068 VMW_DEBUG_USER("Throttling is no longer supported.\n");
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004069 }
Charmaine Lee2f633e52015-08-10 10:45:11 -07004070
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004071 kernel_commands = vmw_execbuf_cmdbuf(dev_priv, user_commands,
4072 kernel_commands, command_size,
4073 &header);
Sinclair Yehc906965d2017-07-05 01:49:32 -07004074 if (IS_ERR(kernel_commands)) {
4075 ret = PTR_ERR(kernel_commands);
4076 goto out_free_fence_fd;
4077 }
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004078
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004079 ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004080 if (ret) {
4081 ret = -ERESTARTSYS;
4082 goto out_free_header;
4083 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004084
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004085 sw_context->kernel = false;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004086 if (kernel_commands == NULL) {
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004087 ret = vmw_resize_cmd_bounce(sw_context, command_size);
4088 if (unlikely(ret != 0))
4089 goto out_unlock;
4090
Deepak Rawat680360a2019-02-13 13:20:42 -08004091 ret = copy_from_user(sw_context->cmd_bounce, user_commands,
4092 command_size);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004093 if (unlikely(ret != 0)) {
4094 ret = -EFAULT;
Deepak Rawat5724f892019-02-11 11:46:27 -08004095 VMW_DEBUG_USER("Failed copying commands.\n");
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004096 goto out_unlock;
4097 }
Deepak Rawat680360a2019-02-13 13:20:42 -08004098
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004099 kernel_commands = sw_context->cmd_bounce;
Deepak Rawat680360a2019-02-13 13:20:42 -08004100 } else if (!header) {
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004101 sw_context->kernel = true;
Deepak Rawat680360a2019-02-13 13:20:42 -08004102 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004103
Thomas Hellstromd5bde952014-01-31 10:12:10 +01004104 sw_context->fp = vmw_fpriv(file_priv);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004105 INIT_LIST_HEAD(&sw_context->ctx_list);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004106 sw_context->cur_query_bo = dev_priv->pinned_bo;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004107 sw_context->last_query_ctx = NULL;
4108 sw_context->needs_post_query_barrier = false;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004109 sw_context->dx_ctx_node = NULL;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07004110 sw_context->dx_query_mob = NULL;
4111 sw_context->dx_query_ctx = NULL;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004112 memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004113 INIT_LIST_HEAD(&sw_context->res_relocations);
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02004114 INIT_LIST_HEAD(&sw_context->bo_relocations);
Deepak Rawat680360a2019-02-13 13:20:42 -08004115
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004116 if (sw_context->staged_bindings)
4117 vmw_binding_state_reset(sw_context->staged_bindings);
4118
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004119 if (!sw_context->res_ht_initialized) {
4120 ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
4121 if (unlikely(ret != 0))
4122 goto out_unlock;
Deepak Rawat680360a2019-02-13 13:20:42 -08004123
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004124 sw_context->res_ht_initialized = true;
4125 }
Deepak Rawat680360a2019-02-13 13:20:42 -08004126
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02004127 INIT_LIST_HEAD(&sw_context->staged_cmd_res);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004128 sw_context->ctx = &val_ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004129 ret = vmw_execbuf_tie_context(dev_priv, sw_context, dx_context_handle);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004130 if (unlikely(ret != 0))
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004131 goto out_err_nores;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004132
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004133 ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
4134 command_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004135 if (unlikely(ret != 0))
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01004136 goto out_err_nores;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00004137
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004138 ret = vmw_resources_reserve(sw_context);
4139 if (unlikely(ret != 0))
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01004140 goto out_err_nores;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004141
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004142 ret = vmw_validation_bo_reserve(&val_ctx, true);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004143 if (unlikely(ret != 0))
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004144 goto out_err_nores;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004145
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004146 ret = vmw_validation_bo_validate(&val_ctx, true);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004147 if (unlikely(ret != 0))
4148 goto out_err;
4149
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004150 ret = vmw_validation_res_validate(&val_ctx, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004151 if (unlikely(ret != 0))
4152 goto out_err;
Deepak Rawat680360a2019-02-13 13:20:42 -08004153
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004154 vmw_validation_drop_ht(&val_ctx);
Thomas Hellstrom1925d452010-05-28 11:21:57 +02004155
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07004156 ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
4157 if (unlikely(ret != 0)) {
4158 ret = -ERESTARTSYS;
4159 goto out_err;
4160 }
4161
Thomas Hellstrom30f82d812014-02-05 08:13:56 +01004162 if (dev_priv->has_mob) {
4163 ret = vmw_rebind_contexts(sw_context);
4164 if (unlikely(ret != 0))
Dan Carpenterb2ad9882014-02-11 19:03:47 +03004165 goto out_unlock_binding;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +01004166 }
4167
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004168 if (!header) {
4169 ret = vmw_execbuf_submit_fifo(dev_priv, kernel_commands,
4170 command_size, sw_context);
4171 } else {
4172 ret = vmw_execbuf_submit_cmdbuf(dev_priv, header, command_size,
4173 sw_context);
4174 header = NULL;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00004175 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004176 mutex_unlock(&dev_priv->binding_mutex);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004177 if (ret)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004178 goto out_err;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004179
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004180 vmw_query_bo_switch_commit(dev_priv, sw_context);
Deepak Rawat680360a2019-02-13 13:20:42 -08004181 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
Thomas Hellstromae2a1042011-09-01 20:18:44 +00004182 (user_fence_rep) ? &handle : NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004183 /*
4184 * This error is harmless, because if fence submission fails,
Thomas Hellstromae2a1042011-09-01 20:18:44 +00004185 * vmw_fifo_send_fence will sync. The error will be propagated to
4186 * user-space in @fence_rep
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004187 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004188 if (ret != 0)
Deepak Rawat5724f892019-02-11 11:46:27 -08004189 VMW_DEBUG_USER("Fence submission error. Syncing.\n");
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004190
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004191 vmw_execbuf_bindings_commit(sw_context, false);
4192 vmw_bind_dx_query_mob(sw_context);
4193 vmw_validation_res_unreserve(&val_ctx, false);
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07004194
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004195 vmw_validation_bo_fence(sw_context->ctx, fence);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004196
Deepak Rawat680360a2019-02-13 13:20:42 -08004197 if (unlikely(dev_priv->pinned_bo != NULL && !dev_priv->query_cid_valid))
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004198 __vmw_execbuf_release_pinned_bo(dev_priv, fence);
4199
Sinclair Yehc906965d2017-07-05 01:49:32 -07004200 /*
Deepak Rawat680360a2019-02-13 13:20:42 -08004201 * If anything fails here, give up trying to export the fence and do a
4202 * sync since the user mode will not be able to sync the fence itself.
4203 * This ensures we are still functionally correct.
Sinclair Yehc906965d2017-07-05 01:49:32 -07004204 */
4205 if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
4206
4207 sync_file = sync_file_create(&fence->base);
4208 if (!sync_file) {
Deepak Rawat5724f892019-02-11 11:46:27 -08004209 VMW_DEBUG_USER("Sync file create failed for fence\n");
Sinclair Yehc906965d2017-07-05 01:49:32 -07004210 put_unused_fd(out_fence_fd);
4211 out_fence_fd = -1;
4212
4213 (void) vmw_fence_obj_wait(fence, false, false,
4214 VMW_FENCE_WAIT_TIMEOUT);
4215 } else {
4216 /* Link the fence with the FD created earlier */
4217 fd_install(out_fence_fd, sync_file->file);
4218 }
4219 }
4220
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02004221 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
Deepak Rawat680360a2019-02-13 13:20:42 -08004222 user_fence_rep, fence, handle, out_fence_fd,
4223 sync_file);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00004224
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01004225 /* Don't unreference when handing fence out */
4226 if (unlikely(out_fence != NULL)) {
4227 *out_fence = fence;
4228 fence = NULL;
4229 } else if (likely(fence != NULL)) {
Thomas Hellstromae2a1042011-09-01 20:18:44 +00004230 vmw_fence_obj_unreference(&fence);
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01004231 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004232
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02004233 vmw_cmdbuf_res_commit(&sw_context->staged_cmd_res);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004234 mutex_unlock(&dev_priv->cmdbuf_mutex);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004235
4236 /*
Deepak Rawat680360a2019-02-13 13:20:42 -08004237 * Unreference resources outside of the cmdbuf_mutex to avoid deadlocks
4238 * in resource destruction paths.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004239 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004240 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004241
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004242 return 0;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004243
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07004244out_unlock_binding:
4245 mutex_unlock(&dev_priv->binding_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004246out_err:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004247 vmw_validation_bo_backoff(&val_ctx);
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01004248out_err_nores:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004249 vmw_execbuf_bindings_commit(sw_context, true);
4250 vmw_validation_res_unreserve(&val_ctx, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004251 vmw_resource_relocations_free(&sw_context->res_relocations);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004252 vmw_free_relocations(sw_context);
Deepak Rawat680360a2019-02-13 13:20:42 -08004253 if (unlikely(dev_priv->pinned_bo != NULL && !dev_priv->query_cid_valid))
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004254 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004255out_unlock:
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02004256 vmw_cmdbuf_res_revert(&sw_context->staged_cmd_res);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004257 vmw_validation_drop_ht(&val_ctx);
4258 WARN_ON(!list_empty(&sw_context->ctx_list));
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004259 mutex_unlock(&dev_priv->cmdbuf_mutex);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004260
4261 /*
Deepak Rawat680360a2019-02-13 13:20:42 -08004262 * Unreference resources outside of the cmdbuf_mutex to avoid deadlocks
4263 * in resource destruction paths.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004264 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004265 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004266out_free_header:
4267 if (header)
4268 vmw_cmdbuf_header_free(header);
Sinclair Yehc906965d2017-07-05 01:49:32 -07004269out_free_fence_fd:
4270 if (out_fence_fd >= 0)
4271 put_unused_fd(out_fence_fd);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004272
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004273 return ret;
4274}
4275
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004276/**
4277 * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
4278 *
4279 * @dev_priv: The device private structure.
4280 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004281 * This function is called to idle the fifo and unpin the query buffer if the
4282 * normal way to do this hits an error, which should typically be extremely
4283 * rare.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004284 */
4285static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
4286{
Deepak Rawat5724f892019-02-11 11:46:27 -08004287 VMW_DEBUG_USER("Can't unpin query buffer. Trying to recover.\n");
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004288
4289 (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07004290 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
4291 if (dev_priv->dummy_query_bo_pinned) {
4292 vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
4293 dev_priv->dummy_query_bo_pinned = false;
4294 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004295}
4296
4297
4298/**
Deepak Rawat680360a2019-02-13 13:20:42 -08004299 * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned query
4300 * bo.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004301 *
4302 * @dev_priv: The device private structure.
Deepak Rawat680360a2019-02-13 13:20:42 -08004303 * @fence: If non-NULL should point to a struct vmw_fence_obj issued _after_ a
4304 * query barrier that flushes all queries touching the current buffer pointed to
4305 * by @dev_priv->pinned_bo
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004306 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004307 * This function should be used to unpin the pinned query bo, or as a query
4308 * barrier when we need to make sure that all queries have finished before the
4309 * next fifo command. (For example on hardware context destructions where the
4310 * hardware may otherwise leak unfinished queries).
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004311 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004312 * This function does not return any failure codes, but make attempts to do safe
4313 * unpinning in case of errors.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004314 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004315 * The function will synchronize on the previous query barrier, and will thus
4316 * not finish until that barrier has executed.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004317 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004318 * the @dev_priv->cmdbuf_mutex needs to be held by the current thread before
4319 * calling this function.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004320 */
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004321void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
4322 struct vmw_fence_obj *fence)
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004323{
4324 int ret = 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004325 struct vmw_fence_obj *lfence = NULL;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004326 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004327
4328 if (dev_priv->pinned_bo == NULL)
4329 goto out_unlock;
4330
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004331 ret = vmw_validation_add_bo(&val_ctx, dev_priv->pinned_bo, false,
4332 false);
4333 if (ret)
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004334 goto out_no_reserve;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004335
4336 ret = vmw_validation_add_bo(&val_ctx, dev_priv->dummy_query_bo, false,
4337 false);
4338 if (ret)
4339 goto out_no_reserve;
4340
4341 ret = vmw_validation_bo_reserve(&val_ctx, false);
4342 if (ret)
4343 goto out_no_reserve;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004344
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004345 if (dev_priv->query_cid_valid) {
4346 BUG_ON(fence != NULL);
Zack Rusin8426ed92020-11-18 12:54:19 -05004347 ret = vmw_cmd_emit_dummy_query(dev_priv, dev_priv->query_cid);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004348 if (ret)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004349 goto out_no_emit;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004350 dev_priv->query_cid_valid = false;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004351 }
4352
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07004353 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
4354 if (dev_priv->dummy_query_bo_pinned) {
4355 vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
4356 dev_priv->dummy_query_bo_pinned = false;
4357 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004358 if (fence == NULL) {
4359 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
4360 NULL);
4361 fence = lfence;
4362 }
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004363 vmw_validation_bo_fence(&val_ctx, fence);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004364 if (lfence != NULL)
4365 vmw_fence_obj_unreference(&lfence);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004366
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004367 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02004368 vmw_bo_unreference(&dev_priv->pinned_bo);
Deepak Rawat680360a2019-02-13 13:20:42 -08004369
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004370out_unlock:
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004371 return;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004372out_no_emit:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004373 vmw_validation_bo_backoff(&val_ctx);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004374out_no_reserve:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004375 vmw_validation_unref_lists(&val_ctx);
4376 vmw_execbuf_unpin_panic(dev_priv);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02004377 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004378}
4379
4380/**
Deepak Rawat680360a2019-02-13 13:20:42 -08004381 * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned query bo.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004382 *
4383 * @dev_priv: The device private structure.
4384 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004385 * This function should be used to unpin the pinned query bo, or as a query
4386 * barrier when we need to make sure that all queries have finished before the
4387 * next fifo command. (For example on hardware context destructions where the
4388 * hardware may otherwise leak unfinished queries).
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004389 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004390 * This function does not return any failure codes, but make attempts to do safe
4391 * unpinning in case of errors.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004392 *
Deepak Rawat680360a2019-02-13 13:20:42 -08004393 * The function will synchronize on the previous query barrier, and will thus
4394 * not finish until that barrier has executed.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004395 */
4396void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
4397{
4398 mutex_lock(&dev_priv->cmdbuf_mutex);
4399 if (dev_priv->query_cid_valid)
4400 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004401 mutex_unlock(&dev_priv->cmdbuf_mutex);
4402}
4403
Emil Velikovcbfbe472019-05-22 17:41:17 +01004404int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
4405 struct drm_file *file_priv)
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004406{
4407 struct vmw_private *dev_priv = vmw_priv(dev);
Emil Velikovcbfbe472019-05-22 17:41:17 +01004408 struct drm_vmw_execbuf_arg *arg = data;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004409 int ret;
Sinclair Yeh585851162017-07-05 01:45:40 -07004410 struct dma_fence *in_fence = NULL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004411
Martin Krastev7a7a9332021-06-09 13:23:00 -04004412 MKS_STAT_TIME_DECL(MKSSTAT_KERN_EXECBUF);
4413 MKS_STAT_TIME_PUSH(MKSSTAT_KERN_EXECBUF);
4414
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004415 /*
Deepak Rawat680360a2019-02-13 13:20:42 -08004416 * Extend the ioctl argument while maintaining backwards compatibility:
Emil Velikovcbfbe472019-05-22 17:41:17 +01004417 * We take different code paths depending on the value of arg->version.
4418 *
4419 * Note: The ioctl argument is extended and zeropadded by core DRM.
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004420 */
Emil Velikovcbfbe472019-05-22 17:41:17 +01004421 if (unlikely(arg->version > DRM_VMW_EXECBUF_VERSION ||
4422 arg->version == 0)) {
Deepak Rawat5724f892019-02-11 11:46:27 -08004423 VMW_DEBUG_USER("Incorrect execbuf version.\n");
Martin Krastev7a7a9332021-06-09 13:23:00 -04004424 ret = -EINVAL;
4425 goto mksstats_out;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004426 }
4427
Emil Velikovcbfbe472019-05-22 17:41:17 +01004428 switch (arg->version) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004429 case 1:
Emil Velikovcbfbe472019-05-22 17:41:17 +01004430 /* For v1 core DRM have extended + zeropadded the data */
4431 arg->context_handle = (uint32_t) -1;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004432 break;
4433 case 2:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004434 default:
Emil Velikovcbfbe472019-05-22 17:41:17 +01004435 /* For v2 and later core DRM would have correctly copied it */
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004436 break;
4437 }
4438
Sinclair Yeh585851162017-07-05 01:45:40 -07004439 /* If imported a fence FD from elsewhere, then wait on it */
Emil Velikovcbfbe472019-05-22 17:41:17 +01004440 if (arg->flags & DRM_VMW_EXECBUF_FLAG_IMPORT_FENCE_FD) {
4441 in_fence = sync_file_get_fence(arg->imported_fence_fd);
Sinclair Yeh585851162017-07-05 01:45:40 -07004442
4443 if (!in_fence) {
Deepak Rawat5724f892019-02-11 11:46:27 -08004444 VMW_DEBUG_USER("Cannot get imported fence\n");
Martin Krastev7a7a9332021-06-09 13:23:00 -04004445 ret = -EINVAL;
4446 goto mksstats_out;
Sinclair Yeh585851162017-07-05 01:45:40 -07004447 }
4448
4449 ret = vmw_wait_dma_fence(dev_priv->fman, in_fence);
4450 if (ret)
4451 goto out;
4452 }
4453
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004454 ret = vmw_execbuf_process(file_priv, dev_priv,
Emil Velikovcbfbe472019-05-22 17:41:17 +01004455 (void __user *)(unsigned long)arg->commands,
4456 NULL, arg->command_size, arg->throttle_us,
4457 arg->context_handle,
4458 (void __user *)(unsigned long)arg->fence_rep,
4459 NULL, arg->flags);
Deepak Rawat680360a2019-02-13 13:20:42 -08004460
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004461 if (unlikely(ret != 0))
Sinclair Yeh585851162017-07-05 01:45:40 -07004462 goto out;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004463
4464 vmw_kms_cursor_post_execbuf(dev_priv);
4465
Sinclair Yeh585851162017-07-05 01:45:40 -07004466out:
4467 if (in_fence)
4468 dma_fence_put(in_fence);
Martin Krastev7a7a9332021-06-09 13:23:00 -04004469
4470mksstats_out:
4471 MKS_STAT_TIME_POP(MKSSTAT_KERN_EXECBUF);
Sinclair Yeh585851162017-07-05 01:45:40 -07004472 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004473}