blob: 2c92744817eca8f7e16401f7c1fba43dffa8157e [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"
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000035
Thomas Hellstromc0951b72012-11-20 12:19:35 +000036#define VMW_RES_HT_ORDER 12
37
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020038/*
Deepak Rawat6f74fd92019-02-08 12:53:57 -080039 * Helper macro to get dx_ctx_node if available otherwise print an error
40 * message. This is for use in command verifier function where if dx_ctx_node
41 * is not set then command is invalid.
42 */
43#define VMW_GET_CTX_NODE(__sw_context) \
44({ \
45 __sw_context->dx_ctx_node ? __sw_context->dx_ctx_node : ({ \
46 DRM_ERROR("SM context is not set at %s\n", __func__); \
47 __sw_context->dx_ctx_node; \
48 }); \
49})
50
Deepak Rawatd01316d2019-02-08 15:50:40 -080051#define VMW_DECLARE_CMD_VAR(__var, __type) \
52 struct { \
53 SVGA3dCmdHeader header; \
54 __type body; \
55 } __var
56
Deepak Rawat6f74fd92019-02-08 12:53:57 -080057/*
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020058 * struct vmw_relocation - Buffer object relocation
59 *
60 * @head: List head for the command submission context's relocation list
Thomas Hellstromcc1e3b72018-09-26 15:38:13 +020061 * @vbo: Non ref-counted pointer to buffer object
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020062 * @mob_loc: Pointer to location for mob id to be modified
63 * @location: Pointer to location for guest pointer to be modified
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020064 */
65struct vmw_relocation {
66 struct list_head head;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020067 struct vmw_buffer_object *vbo;
Thomas Hellstromcc1e3b72018-09-26 15:38:13 +020068 union {
69 SVGAMobId *mob_loc;
70 SVGAGuestPtr *location;
71 };
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020072};
73
Thomas Hellstromc0951b72012-11-20 12:19:35 +000074/**
Thomas Hellstroma1944032016-10-10 11:06:45 -070075 * enum vmw_resource_relocation_type - Relocation type for resources
76 *
77 * @vmw_res_rel_normal: Traditional relocation. The resource id in the
78 * command stream is replaced with the actual id after validation.
79 * @vmw_res_rel_nop: NOP relocation. The command is unconditionally replaced
80 * with a NOP.
81 * @vmw_res_rel_cond_nop: Conditional NOP relocation. If the resource id
82 * after validation is -1, the command is replaced with a NOP. Otherwise no
83 * action.
84 */
85enum vmw_resource_relocation_type {
86 vmw_res_rel_normal,
87 vmw_res_rel_nop,
88 vmw_res_rel_cond_nop,
89 vmw_res_rel_max
90};
91
92/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +000093 * struct vmw_resource_relocation - Relocation info for resources
94 *
95 * @head: List head for the software context's relocation list.
96 * @res: Non-ref-counted pointer to the resource.
Thomas Hellstrome7a45282016-10-10 10:44:00 -070097 * @offset: Offset of single byte entries into the command buffer where the
Thomas Hellstromc0951b72012-11-20 12:19:35 +000098 * id that needs fixup is located.
Thomas Hellstroma1944032016-10-10 11:06:45 -070099 * @rel_type: Type of relocation.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000100 */
101struct vmw_resource_relocation {
102 struct list_head head;
103 const struct vmw_resource *res;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700104 u32 offset:29;
105 enum vmw_resource_relocation_type rel_type:3;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000106};
107
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200108/*
109 * struct vmw_ctx_validation_info - Extra validation metadata for contexts
110 * @head: List head of context list
111 * @ctx: The context resource
112 * @cur: The context's persistent binding state
113 * @staged: The binding state changes of this command buffer
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000114 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200115struct vmw_ctx_validation_info {
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000116 struct list_head head;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200117 struct vmw_resource *ctx;
118 struct vmw_ctx_binding_state *cur;
119 struct vmw_ctx_binding_state *staged;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000120};
121
122/**
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100123 * struct vmw_cmd_entry - Describe a command for the verifier
124 *
125 * @user_allow: Whether allowed from the execbuf ioctl.
126 * @gb_disable: Whether disabled if guest-backed objects are available.
127 * @gb_enable: Whether enabled iff guest-backed objects are available.
128 */
129struct vmw_cmd_entry {
130 int (*func) (struct vmw_private *, struct vmw_sw_context *,
131 SVGA3dCmdHeader *);
132 bool user_allow;
133 bool gb_disable;
134 bool gb_enable;
Thomas Hellstrom65b97a22017-08-24 08:06:29 +0200135 const char *cmd_name;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100136};
137
138#define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable) \
139 [(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
Thomas Hellstrom65b97a22017-08-24 08:06:29 +0200140 (_gb_disable), (_gb_enable), #_cmd}
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100141
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700142static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
143 struct vmw_sw_context *sw_context,
144 struct vmw_resource *ctx);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700145static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
146 struct vmw_sw_context *sw_context,
147 SVGAMobId *id,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200148 struct vmw_buffer_object **vmw_bo_p);
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700149/**
150 * vmw_ptr_diff - Compute the offset from a to b in bytes
151 *
152 * @a: A starting pointer.
153 * @b: A pointer offset in the same address space.
154 *
155 * Returns: The offset in bytes between the two pointers.
156 */
157static size_t vmw_ptr_diff(void *a, void *b)
158{
159 return (unsigned long) b - (unsigned long) a;
160}
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700161
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100162/**
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200163 * vmw_execbuf_bindings_commit - Commit modified binding state
164 * @sw_context: The command submission context
165 * @backoff: Whether this is part of the error path and binding state
166 * changes should be ignored
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000167 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200168static void vmw_execbuf_bindings_commit(struct vmw_sw_context *sw_context,
169 bool backoff)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000170{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200171 struct vmw_ctx_validation_info *entry;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700172
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200173 list_for_each_entry(entry, &sw_context->ctx_list, head) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200174 if (!backoff)
175 vmw_binding_state_commit(entry->cur, entry->staged);
176 if (entry->staged != sw_context->staged_bindings)
177 vmw_binding_state_free(entry->staged);
178 else
179 sw_context->staged_bindings_inuse = false;
180 }
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200181
182 /* List entries are freed with the validation context */
183 INIT_LIST_HEAD(&sw_context->ctx_list);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200184}
185
186/**
187 * vmw_bind_dx_query_mob - Bind the DX query MOB if referenced
188 * @sw_context: The command submission context
189 */
190static void vmw_bind_dx_query_mob(struct vmw_sw_context *sw_context)
191{
192 if (sw_context->dx_query_mob)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700193 vmw_context_bind_dx_query(sw_context->dx_query_ctx,
194 sw_context->dx_query_mob);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000195}
196
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700197/**
198 * vmw_cmd_ctx_first_setup - Perform the setup needed when a context is
199 * added to the validate list.
200 *
201 * @dev_priv: Pointer to the device private:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200202 * @sw_context: The command submission context
203 * @node: The validation node holding the context resource metadata
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700204 */
205static int vmw_cmd_ctx_first_setup(struct vmw_private *dev_priv,
206 struct vmw_sw_context *sw_context,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200207 struct vmw_resource *res,
208 struct vmw_ctx_validation_info *node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700209{
210 int ret;
211
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200212 ret = vmw_resource_context_res_add(dev_priv, sw_context, res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700213 if (unlikely(ret != 0))
214 goto out_err;
215
216 if (!sw_context->staged_bindings) {
217 sw_context->staged_bindings =
218 vmw_binding_state_alloc(dev_priv);
219 if (IS_ERR(sw_context->staged_bindings)) {
220 DRM_ERROR("Failed to allocate context binding "
221 "information.\n");
222 ret = PTR_ERR(sw_context->staged_bindings);
223 sw_context->staged_bindings = NULL;
224 goto out_err;
225 }
226 }
227
228 if (sw_context->staged_bindings_inuse) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200229 node->staged = vmw_binding_state_alloc(dev_priv);
230 if (IS_ERR(node->staged)) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700231 DRM_ERROR("Failed to allocate context binding "
232 "information.\n");
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200233 ret = PTR_ERR(node->staged);
234 node->staged = NULL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700235 goto out_err;
236 }
237 } else {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200238 node->staged = sw_context->staged_bindings;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700239 sw_context->staged_bindings_inuse = true;
240 }
241
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200242 node->ctx = res;
243 node->cur = vmw_context_binding_state(res);
244 list_add_tail(&node->head, &sw_context->ctx_list);
245
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700246 return 0;
247out_err:
248 return ret;
249}
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000250
251/**
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200252 * vmw_execbuf_res_size - calculate extra size fore the resource validation
253 * node
254 * @dev_priv: Pointer to the device private struct.
255 * @res_type: The resource type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000256 *
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200257 * Guest-backed contexts and DX contexts require extra size to store
258 * execbuf private information in the validation node. Typically the
259 * binding manager associated data structures.
260 *
261 * Returns: The extra size requirement based on resource type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000262 */
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200263static unsigned int vmw_execbuf_res_size(struct vmw_private *dev_priv,
264 enum vmw_res_type res_type)
265{
266 return (res_type == vmw_res_dx_context ||
267 (res_type == vmw_res_context && dev_priv->has_mob)) ?
268 sizeof(struct vmw_ctx_validation_info) : 0;
269}
270
271/**
272 * vmw_execbuf_rcache_update - Update a resource-node cache entry
273 *
274 * @rcache: Pointer to the entry to update.
275 * @res: Pointer to the resource.
276 * @private: Pointer to the execbuf-private space in the resource
277 * validation node.
278 */
279static void vmw_execbuf_rcache_update(struct vmw_res_cache_entry *rcache,
280 struct vmw_resource *res,
281 void *private)
282{
283 rcache->res = res;
284 rcache->private = private;
285 rcache->valid = 1;
286 rcache->valid_handle = 0;
287}
288
289/**
290 * vmw_execbuf_res_noref_val_add - Add a resource described by an
291 * unreferenced rcu-protected pointer to the validation list.
292 * @sw_context: Pointer to the software context.
293 * @res: Unreferenced rcu-protected pointer to the resource.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100294 * @dirty: Whether to change dirty status.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200295 *
296 * Returns: 0 on success. Negative error code on failure. Typical error
297 * codes are %-EINVAL on inconsistency and %-ESRCH if the resource was
298 * doomed.
299 */
300static int vmw_execbuf_res_noref_val_add(struct vmw_sw_context *sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100301 struct vmw_resource *res,
302 u32 dirty)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000303{
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700304 struct vmw_private *dev_priv = res->dev_priv;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000305 int ret;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200306 enum vmw_res_type res_type = vmw_res_type(res);
307 struct vmw_res_cache_entry *rcache;
308 struct vmw_ctx_validation_info *ctx_info;
309 bool first_usage;
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200310 unsigned int priv_size;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000311
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200312 rcache = &sw_context->res_cache[res_type];
313 if (likely(rcache->valid && rcache->res == res)) {
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100314 if (dirty)
315 vmw_validation_res_set_dirty(sw_context->ctx,
316 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200317 vmw_user_resource_noref_release();
318 return 0;
319 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000320
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200321 priv_size = vmw_execbuf_res_size(dev_priv, res_type);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200322 ret = vmw_validation_add_resource(sw_context->ctx, res, priv_size,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100323 dirty, (void **)&ctx_info,
324 &first_usage);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200325 vmw_user_resource_noref_release();
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200326 if (ret)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000327 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000328
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200329 if (priv_size && first_usage) {
330 ret = vmw_cmd_ctx_first_setup(dev_priv, sw_context, res,
331 ctx_info);
332 if (ret)
333 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700334 }
335
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200336 vmw_execbuf_rcache_update(rcache, res, ctx_info);
337 return 0;
338}
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700339
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200340/**
341 * vmw_execbuf_res_noctx_val_add - Add a non-context resource to the resource
342 * validation list if it's not already on it
343 * @sw_context: Pointer to the software context.
344 * @res: Pointer to the resource.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100345 * @dirty: Whether to change dirty status.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200346 *
347 * Returns: Zero on success. Negative error code on failure.
348 */
349static int vmw_execbuf_res_noctx_val_add(struct vmw_sw_context *sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100350 struct vmw_resource *res,
351 u32 dirty)
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200352{
353 struct vmw_res_cache_entry *rcache;
354 enum vmw_res_type res_type = vmw_res_type(res);
355 void *ptr;
356 int ret;
357
358 rcache = &sw_context->res_cache[res_type];
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100359 if (likely(rcache->valid && rcache->res == res)) {
360 if (dirty)
361 vmw_validation_res_set_dirty(sw_context->ctx,
362 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200363 return 0;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100364 }
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200365
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100366 ret = vmw_validation_add_resource(sw_context->ctx, res, 0, dirty,
367 &ptr, NULL);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200368 if (ret)
369 return ret;
370
371 vmw_execbuf_rcache_update(rcache, res, ptr);
372
373 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700374}
375
376/**
377 * vmw_view_res_val_add - Add a view and the surface it's pointing to
378 * to the validation list
379 *
380 * @sw_context: The software context holding the validation list.
381 * @view: Pointer to the view resource.
382 *
383 * Returns 0 if success, negative error code otherwise.
384 */
385static int vmw_view_res_val_add(struct vmw_sw_context *sw_context,
386 struct vmw_resource *view)
387{
388 int ret;
389
390 /*
391 * First add the resource the view is pointing to, otherwise
392 * it may be swapped out when the view is validated.
393 */
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100394 ret = vmw_execbuf_res_noctx_val_add(sw_context, vmw_view_srf(view),
395 vmw_view_dirtying(view));
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700396 if (ret)
397 return ret;
398
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100399 return vmw_execbuf_res_noctx_val_add(sw_context, view,
400 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700401}
402
403/**
404 * vmw_view_id_val_add - Look up a view and add it and the surface it's
405 * pointing to to the validation list.
406 *
407 * @sw_context: The software context holding the validation list.
408 * @view_type: The view type to look up.
409 * @id: view id of the view.
410 *
411 * The view is represented by a view id and the DX context it's created on,
412 * or scheduled for creation on. If there is no DX context set, the function
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200413 * will return an -EINVAL error pointer.
414 *
415 * Returns: Unreferenced pointer to the resource on success, negative error
416 * pointer on failure.
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700417 */
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200418static struct vmw_resource *
419vmw_view_id_val_add(struct vmw_sw_context *sw_context,
420 enum vmw_view_type view_type, u32 id)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700421{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200422 struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700423 struct vmw_resource *view;
424 int ret;
425
426 if (!ctx_node) {
427 DRM_ERROR("DX Context not set.\n");
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200428 return ERR_PTR(-EINVAL);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700429 }
430
431 view = vmw_view_lookup(sw_context->man, view_type, id);
432 if (IS_ERR(view))
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200433 return view;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700434
435 ret = vmw_view_res_val_add(sw_context, view);
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200436 if (ret)
437 return ERR_PTR(ret);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700438
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200439 return view;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000440}
441
442/**
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100443 * vmw_resource_context_res_add - Put resources previously bound to a context on
444 * the validation list
445 *
446 * @dev_priv: Pointer to a device private structure
447 * @sw_context: Pointer to a software context used for this command submission
448 * @ctx: Pointer to the context resource
449 *
450 * This function puts all resources that were previously bound to @ctx on
451 * the resource validation list. This is part of the context state reemission
452 */
453static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
454 struct vmw_sw_context *sw_context,
455 struct vmw_resource *ctx)
456{
457 struct list_head *binding_list;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700458 struct vmw_ctx_bindinfo *entry;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100459 int ret = 0;
460 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700461 u32 i;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100462
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700463 /* Add all cotables to the validation list. */
464 if (dev_priv->has_dx && vmw_res_type(ctx) == vmw_res_dx_context) {
465 for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) {
466 res = vmw_context_cotable(ctx, i);
467 if (IS_ERR(res))
468 continue;
469
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100470 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
471 VMW_RES_DIRTY_SET);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700472 if (unlikely(ret != 0))
473 return ret;
474 }
475 }
476
477
478 /* Add all resources bound to the context to the validation list */
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100479 mutex_lock(&dev_priv->binding_mutex);
480 binding_list = vmw_context_binding_list(ctx);
481
482 list_for_each_entry(entry, binding_list, ctx_list) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700483 if (vmw_res_type(entry->res) == vmw_res_view)
484 ret = vmw_view_res_val_add(sw_context, entry->res);
485 else
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100486 ret = vmw_execbuf_res_noctx_val_add
487 (sw_context, entry->res,
488 vmw_binding_dirtying(entry->bt));
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100489 if (unlikely(ret != 0))
490 break;
491 }
492
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700493 if (dev_priv->has_dx && vmw_res_type(ctx) == vmw_res_dx_context) {
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200494 struct vmw_buffer_object *dx_query_mob;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700495
496 dx_query_mob = vmw_context_get_dx_query_mob(ctx);
497 if (dx_query_mob)
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200498 ret = vmw_validation_add_bo(sw_context->ctx,
499 dx_query_mob, true, false);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700500 }
501
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100502 mutex_unlock(&dev_priv->binding_mutex);
503 return ret;
504}
505
506/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000507 * vmw_resource_relocation_add - Add a relocation to the relocation list
508 *
509 * @list: Pointer to head of relocation list.
510 * @res: The resource.
511 * @offset: Offset into the command buffer currently being parsed where the
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700512 * id that needs fixup is located. Granularity is one byte.
Thomas Hellstroma1944032016-10-10 11:06:45 -0700513 * @rel_type: Relocation type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000514 */
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200515static int vmw_resource_relocation_add(struct vmw_sw_context *sw_context,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000516 const struct vmw_resource *res,
Thomas Hellstroma1944032016-10-10 11:06:45 -0700517 unsigned long offset,
518 enum vmw_resource_relocation_type
519 rel_type)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000520{
521 struct vmw_resource_relocation *rel;
522
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200523 rel = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*rel));
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530524 if (unlikely(!rel)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000525 DRM_ERROR("Failed to allocate a resource relocation.\n");
526 return -ENOMEM;
527 }
528
529 rel->res = res;
530 rel->offset = offset;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700531 rel->rel_type = rel_type;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200532 list_add_tail(&rel->head, &sw_context->res_relocations);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000533
534 return 0;
535}
536
537/**
538 * vmw_resource_relocations_free - Free all relocations on a list
539 *
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200540 * @list: Pointer to the head of the relocation list
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000541 */
542static void vmw_resource_relocations_free(struct list_head *list)
543{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200544 /* Memory is validation context memory, so no need to free it */
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000545
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200546 INIT_LIST_HEAD(list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000547}
548
549/**
550 * vmw_resource_relocations_apply - Apply all relocations on a list
551 *
552 * @cb: Pointer to the start of the command buffer bein patch. This need
553 * not be the same buffer as the one being parsed when the relocation
554 * list was built, but the contents must be the same modulo the
555 * resource ids.
556 * @list: Pointer to the head of the relocation list.
557 */
558static void vmw_resource_relocations_apply(uint32_t *cb,
559 struct list_head *list)
560{
561 struct vmw_resource_relocation *rel;
562
Thomas Hellstroma1944032016-10-10 11:06:45 -0700563 /* Validate the struct vmw_resource_relocation member size */
564 BUILD_BUG_ON(SVGA_CB_MAX_SIZE >= (1 << 29));
565 BUILD_BUG_ON(vmw_res_rel_max >= (1 << 3));
566
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100567 list_for_each_entry(rel, list, head) {
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700568 u32 *addr = (u32 *)((unsigned long) cb + rel->offset);
Thomas Hellstroma1944032016-10-10 11:06:45 -0700569 switch (rel->rel_type) {
570 case vmw_res_rel_normal:
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700571 *addr = rel->res->id;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700572 break;
573 case vmw_res_rel_nop:
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700574 *addr = SVGA_3D_CMD_NOP;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700575 break;
576 default:
577 if (rel->res->id == -1)
578 *addr = SVGA_3D_CMD_NOP;
579 break;
580 }
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100581 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000582}
583
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000584static int vmw_cmd_invalid(struct vmw_private *dev_priv,
585 struct vmw_sw_context *sw_context,
586 SVGA3dCmdHeader *header)
587{
Sinclair Yehfcfffdd2017-07-17 23:28:36 -0700588 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000589}
590
591static int vmw_cmd_ok(struct vmw_private *dev_priv,
592 struct vmw_sw_context *sw_context,
593 SVGA3dCmdHeader *header)
594{
595 return 0;
596}
597
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200598/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000599 * vmw_resources_reserve - Reserve all resources on the sw_context's
600 * resource list.
601 *
602 * @sw_context: Pointer to the software context.
603 *
604 * Note that since vmware's command submission currently is protected by
605 * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
606 * since only a single thread at once will attempt this.
607 */
608static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
609{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200610 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000611
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200612 ret = vmw_validation_res_reserve(sw_context->ctx, true);
613 if (ret)
614 return ret;
Charmaine Lee2f633e52015-08-10 10:45:11 -0700615
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700616 if (sw_context->dx_query_mob) {
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200617 struct vmw_buffer_object *expected_dx_query_mob;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700618
619 expected_dx_query_mob =
620 vmw_context_get_dx_query_mob(sw_context->dx_query_ctx);
621 if (expected_dx_query_mob &&
622 expected_dx_query_mob != sw_context->dx_query_mob) {
623 ret = -EINVAL;
624 }
625 }
626
627 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000628}
629
630/**
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100631 * vmw_cmd_res_check - Check that a resource is present and if so, put it
632 * on the resource validate list unless it's already there.
633 *
634 * @dev_priv: Pointer to a device private structure.
635 * @sw_context: Pointer to the software context.
636 * @res_type: Resource type.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100637 * @dirty: Whether to change dirty status.
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100638 * @converter: User-space visisble type specific information.
639 * @id_loc: Pointer to the location in the command buffer currently being
640 * parsed from where the user-space resource id handle is located.
641 * @p_val: Pointer to pointer to resource validalidation node. Populated
642 * on exit.
643 */
644static int
645vmw_cmd_res_check(struct vmw_private *dev_priv,
646 struct vmw_sw_context *sw_context,
647 enum vmw_res_type res_type,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100648 u32 dirty,
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100649 const struct vmw_user_resource_conv *converter,
650 uint32_t *id_loc,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200651 struct vmw_resource **p_res)
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100652{
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200653 struct vmw_res_cache_entry *rcache = &sw_context->res_cache[res_type];
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200654 struct vmw_resource *res;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200655 int ret;
656
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200657 if (p_res)
658 *p_res = NULL;
659
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200660 if (*id_loc == SVGA3D_INVALID_ID) {
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200661 if (res_type == vmw_res_context) {
662 DRM_ERROR("Illegal context invalid id.\n");
663 return -EINVAL;
664 }
665 return 0;
666 }
667
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200668 if (likely(rcache->valid_handle && *id_loc == rcache->handle)) {
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200669 res = rcache->res;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100670 if (dirty)
671 vmw_validation_res_set_dirty(sw_context->ctx,
672 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200673 } else {
674 unsigned int size = vmw_execbuf_res_size(dev_priv, res_type);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200675
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200676 ret = vmw_validation_preload_res(sw_context->ctx, size);
677 if (ret)
678 return ret;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200679
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200680 res = vmw_user_resource_noref_lookup_handle
681 (dev_priv, sw_context->fp->tfile, *id_loc, converter);
Chengguang Xu4efa6662019-03-01 10:14:06 -0800682 if (IS_ERR(res)) {
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200683 DRM_ERROR("Could not find or use resource 0x%08x.\n",
684 (unsigned int) *id_loc);
685 return PTR_ERR(res);
686 }
687
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100688 ret = vmw_execbuf_res_noref_val_add(sw_context, res, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200689 if (unlikely(ret != 0))
690 return ret;
691
692 if (rcache->valid && rcache->res == res) {
693 rcache->valid_handle = true;
694 rcache->handle = *id_loc;
695 }
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200696 }
697
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200698 ret = vmw_resource_relocation_add(sw_context, res,
699 vmw_ptr_diff(sw_context->buf_start,
700 id_loc),
701 vmw_res_rel_normal);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200702 if (p_res)
703 *p_res = res;
704
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200705 return 0;
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100706}
707
708/**
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700709 * vmw_rebind_dx_query - Rebind DX query associated with the context
710 *
711 * @ctx_res: context the query belongs to
712 *
713 * This function assumes binding_mutex is held.
714 */
715static int vmw_rebind_all_dx_query(struct vmw_resource *ctx_res)
716{
717 struct vmw_private *dev_priv = ctx_res->dev_priv;
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200718 struct vmw_buffer_object *dx_query_mob;
Deepak Rawatd01316d2019-02-08 15:50:40 -0800719 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindAllQuery);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700720
721 dx_query_mob = vmw_context_get_dx_query_mob(ctx_res);
722
723 if (!dx_query_mob || dx_query_mob->dx_query_ctx)
724 return 0;
725
726 cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), ctx_res->id);
727
728 if (cmd == NULL) {
729 DRM_ERROR("Failed to rebind queries.\n");
730 return -ENOMEM;
731 }
732
733 cmd->header.id = SVGA_3D_CMD_DX_BIND_ALL_QUERY;
734 cmd->header.size = sizeof(cmd->body);
735 cmd->body.cid = ctx_res->id;
736 cmd->body.mobid = dx_query_mob->base.mem.start;
737 vmw_fifo_commit(dev_priv, sizeof(*cmd));
738
739 vmw_context_bind_dx_query(ctx_res, dx_query_mob);
740
741 return 0;
742}
743
744/**
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100745 * vmw_rebind_contexts - Rebind all resources previously bound to
746 * referenced contexts.
747 *
748 * @sw_context: Pointer to the software context.
749 *
750 * Rebind context binding points that have been scrubbed because of eviction.
751 */
752static int vmw_rebind_contexts(struct vmw_sw_context *sw_context)
753{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200754 struct vmw_ctx_validation_info *val;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100755 int ret;
756
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200757 list_for_each_entry(val, &sw_context->ctx_list, head) {
758 ret = vmw_binding_rebind_all(val->cur);
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100759 if (unlikely(ret != 0)) {
760 if (ret != -ERESTARTSYS)
761 DRM_ERROR("Failed to rebind context.\n");
762 return ret;
763 }
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700764
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200765 ret = vmw_rebind_all_dx_query(val->ctx);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700766 if (ret != 0)
767 return ret;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100768 }
769
770 return 0;
771}
772
773/**
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700774 * vmw_view_bindings_add - Add an array of view bindings to a context
775 * binding state tracker.
776 *
777 * @sw_context: The execbuf state used for this command.
778 * @view_type: View type for the bindings.
779 * @binding_type: Binding type for the bindings.
780 * @shader_slot: The shader slot to user for the bindings.
781 * @view_ids: Array of view ids to be bound.
782 * @num_views: Number of view ids in @view_ids.
783 * @first_slot: The binding slot to be used for the first view id in @view_ids.
784 */
785static int vmw_view_bindings_add(struct vmw_sw_context *sw_context,
786 enum vmw_view_type view_type,
787 enum vmw_ctx_binding_type binding_type,
788 uint32 shader_slot,
789 uint32 view_ids[], u32 num_views,
790 u32 first_slot)
791{
Deepak Rawat6f74fd92019-02-08 12:53:57 -0800792 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700793 u32 i;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700794
Deepak Rawat6f74fd92019-02-08 12:53:57 -0800795 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700796 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700797
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700798 for (i = 0; i < num_views; ++i) {
799 struct vmw_ctx_bindinfo_view binding;
800 struct vmw_resource *view = NULL;
801
802 if (view_ids[i] != SVGA3D_INVALID_ID) {
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200803 view = vmw_view_id_val_add(sw_context, view_type,
804 view_ids[i]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700805 if (IS_ERR(view)) {
806 DRM_ERROR("View not found.\n");
807 return PTR_ERR(view);
808 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700809 }
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200810 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700811 binding.bi.res = view;
812 binding.bi.bt = binding_type;
813 binding.shader_slot = shader_slot;
814 binding.slot = first_slot + i;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200815 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700816 shader_slot, binding.slot);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700817 }
818
819 return 0;
820}
821
822/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000823 * vmw_cmd_cid_check - Check a command header for valid context information.
824 *
825 * @dev_priv: Pointer to a device private structure.
826 * @sw_context: Pointer to the software context.
827 * @header: A command header with an embedded user-space context handle.
828 *
829 * Convenience function: Call vmw_cmd_res_check with the user-space context
830 * handle embedded in @header.
831 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000832static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
833 struct vmw_sw_context *sw_context,
834 SVGA3dCmdHeader *header)
835{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800836 VMW_DECLARE_CMD_VAR(*cmd, uint32_t) =
837 container_of(header, typeof(*cmd), header);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000838
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000839 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100840 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -0800841 &cmd->body, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000842}
843
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200844/**
845 * vmw_execbuf_info_from_res - Get the private validation metadata for a
846 * recently validated resource
847 * @sw_context: Pointer to the command submission context
848 * @res: The resource
849 *
850 * The resource pointed to by @res needs to be present in the command submission
851 * context's resource cache and hence the last resource of that type to be
852 * processed by the validation code.
853 *
854 * Return: a pointer to the private metadata of the resource, or NULL
855 * if it wasn't found
856 */
857static struct vmw_ctx_validation_info *
858vmw_execbuf_info_from_res(struct vmw_sw_context *sw_context,
859 struct vmw_resource *res)
860{
861 struct vmw_res_cache_entry *rcache =
862 &sw_context->res_cache[vmw_res_type(res)];
863
864 if (rcache->valid && rcache->res == res)
865 return rcache->private;
866
867 WARN_ON_ONCE(true);
868 return NULL;
869}
870
871
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000872static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
873 struct vmw_sw_context *sw_context,
874 SVGA3dCmdHeader *header)
875{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800876 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetRenderTarget);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200877 struct vmw_resource *ctx;
878 struct vmw_resource *res;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000879 int ret;
880
Deepak Rawatd01316d2019-02-08 15:50:40 -0800881 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700882
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700883 if (cmd->body.type >= SVGA3D_RT_MAX) {
884 DRM_ERROR("Illegal render target type %u.\n",
885 (unsigned) cmd->body.type);
886 return -EINVAL;
887 }
888
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700889 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100890 VMW_RES_DIRTY_SET, user_context_converter,
891 &cmd->body.cid, &ctx);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000892 if (unlikely(ret != 0))
893 return ret;
894
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000895 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100896 VMW_RES_DIRTY_SET, user_surface_converter,
897 &cmd->body.target.sid, &res);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200898 if (unlikely(ret))
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700899 return ret;
900
901 if (dev_priv->has_mob) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700902 struct vmw_ctx_bindinfo_view binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200903 struct vmw_ctx_validation_info *node;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700904
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200905 node = vmw_execbuf_info_from_res(sw_context, ctx);
906 if (!node)
907 return -EINVAL;
908
909 binding.bi.ctx = ctx;
910 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700911 binding.bi.bt = vmw_ctx_binding_rt;
912 binding.slot = cmd->body.type;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200913 vmw_binding_add(node->staged, &binding.bi, 0, binding.slot);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700914 }
915
916 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000917}
918
919static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
920 struct vmw_sw_context *sw_context,
921 SVGA3dCmdHeader *header)
922{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800923 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceCopy);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000924 int ret;
925
Deepak Rawatd01316d2019-02-08 15:50:40 -0800926 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromc9146cd2015-03-02 23:45:04 -0800927
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700928 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100929 VMW_RES_DIRTY_NONE, user_surface_converter,
930 &cmd->body.src.sid, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700931 if (ret)
932 return ret;
Thomas Hellstromc9146cd2015-03-02 23:45:04 -0800933
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000934 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100935 VMW_RES_DIRTY_SET, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000936 &cmd->body.dest.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000937}
938
Neha Bhende0fca749e2015-08-10 10:51:07 -0700939static int vmw_cmd_buffer_copy_check(struct vmw_private *dev_priv,
940 struct vmw_sw_context *sw_context,
941 SVGA3dCmdHeader *header)
942{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800943 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBufferCopy);
Neha Bhende0fca749e2015-08-10 10:51:07 -0700944 int ret;
945
946 cmd = container_of(header, typeof(*cmd), header);
947 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100948 VMW_RES_DIRTY_NONE, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700949 &cmd->body.src, NULL);
950 if (ret != 0)
951 return ret;
952
953 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100954 VMW_RES_DIRTY_SET, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700955 &cmd->body.dest, NULL);
956}
957
958static int vmw_cmd_pred_copy_check(struct vmw_private *dev_priv,
959 struct vmw_sw_context *sw_context,
960 SVGA3dCmdHeader *header)
961{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800962 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXPredCopyRegion);
Neha Bhende0fca749e2015-08-10 10:51:07 -0700963 int ret;
964
965 cmd = container_of(header, typeof(*cmd), header);
966 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100967 VMW_RES_DIRTY_NONE, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700968 &cmd->body.srcSid, NULL);
969 if (ret != 0)
970 return ret;
971
972 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100973 VMW_RES_DIRTY_SET, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700974 &cmd->body.dstSid, NULL);
975}
976
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000977static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
978 struct vmw_sw_context *sw_context,
979 SVGA3dCmdHeader *header)
980{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800981 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceStretchBlt);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000982 int ret;
983
Deepak Rawatd01316d2019-02-08 15:50:40 -0800984 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000985 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100986 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000987 &cmd->body.src.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000988 if (unlikely(ret != 0))
989 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000990 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100991 VMW_RES_DIRTY_SET, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000992 &cmd->body.dest.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000993}
994
995static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
996 struct vmw_sw_context *sw_context,
997 SVGA3dCmdHeader *header)
998{
Deepak Rawatd01316d2019-02-08 15:50:40 -0800999 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBlitSurfaceToScreen) =
1000 container_of(header, typeof(*cmd), header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02001001
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001002 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001003 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001004 &cmd->body.srcImage.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001005}
1006
1007static int vmw_cmd_present_check(struct vmw_private *dev_priv,
1008 struct vmw_sw_context *sw_context,
1009 SVGA3dCmdHeader *header)
1010{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001011 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdPresent) =
1012 container_of(header, typeof(*cmd), header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02001013
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001014 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001015 VMW_RES_DIRTY_NONE, user_surface_converter,
1016 &cmd->body.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001017}
1018
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001019/**
1020 * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
1021 *
1022 * @dev_priv: The device private structure.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001023 * @new_query_bo: The new buffer holding query results.
1024 * @sw_context: The software context used for this command submission.
1025 *
1026 * This function checks whether @new_query_bo is suitable for holding
1027 * query results, and if another buffer currently is pinned for query
1028 * results. If so, the function prepares the state of @sw_context for
1029 * switching pinned buffers after successful submission of the current
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001030 * command batch.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001031 */
1032static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001033 struct vmw_buffer_object *new_query_bo,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001034 struct vmw_sw_context *sw_context)
1035{
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001036 struct vmw_res_cache_entry *ctx_entry =
1037 &sw_context->res_cache[vmw_res_context];
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001038 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001039
1040 BUG_ON(!ctx_entry->valid);
1041 sw_context->last_query_ctx = ctx_entry->res;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001042
1043 if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
1044
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001045 if (unlikely(new_query_bo->base.num_pages > 4)) {
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001046 DRM_ERROR("Query buffer too large.\n");
1047 return -EINVAL;
1048 }
1049
1050 if (unlikely(sw_context->cur_query_bo != NULL)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001051 sw_context->needs_post_query_barrier = true;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001052 ret = vmw_validation_add_bo(sw_context->ctx,
1053 sw_context->cur_query_bo,
1054 dev_priv->has_mob, false);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001055 if (unlikely(ret != 0))
1056 return ret;
1057 }
1058 sw_context->cur_query_bo = new_query_bo;
1059
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001060 ret = vmw_validation_add_bo(sw_context->ctx,
1061 dev_priv->dummy_query_bo,
1062 dev_priv->has_mob, false);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001063 if (unlikely(ret != 0))
1064 return ret;
1065
1066 }
1067
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001068 return 0;
1069}
1070
1071
1072/**
1073 * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
1074 *
1075 * @dev_priv: The device private structure.
1076 * @sw_context: The software context used for this command submission batch.
1077 *
1078 * This function will check if we're switching query buffers, and will then,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001079 * issue a dummy occlusion query wait used as a query barrier. When the fence
1080 * object following that query wait has signaled, we are sure that all
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001081 * preceding queries have finished, and the old query buffer can be unpinned.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001082 * However, since both the new query buffer and the old one are fenced with
1083 * that fence, we can do an asynchronus unpin now, and be sure that the
1084 * old query buffer won't be moved until the fence has signaled.
1085 *
1086 * As mentioned above, both the new - and old query buffers need to be fenced
1087 * using a sequence emitted *after* calling this function.
1088 */
1089static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
1090 struct vmw_sw_context *sw_context)
1091{
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001092 /*
1093 * The validate list should still hold references to all
1094 * contexts here.
1095 */
1096
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001097 if (sw_context->needs_post_query_barrier) {
1098 struct vmw_res_cache_entry *ctx_entry =
1099 &sw_context->res_cache[vmw_res_context];
1100 struct vmw_resource *ctx;
1101 int ret;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001102
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001103 BUG_ON(!ctx_entry->valid);
1104 ctx = ctx_entry->res;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001105
1106 ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
1107
1108 if (unlikely(ret != 0))
1109 DRM_ERROR("Out of fifo space for dummy query.\n");
1110 }
1111
1112 if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
1113 if (dev_priv->pinned_bo) {
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001114 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001115 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001116 }
1117
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001118 if (!sw_context->needs_post_query_barrier) {
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001119 vmw_bo_pin_reserved(sw_context->cur_query_bo, true);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001120
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001121 /*
1122 * We pin also the dummy_query_bo buffer so that we
1123 * don't need to validate it when emitting
1124 * dummy queries in context destroy paths.
1125 */
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001126
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001127 if (!dev_priv->dummy_query_bo_pinned) {
1128 vmw_bo_pin_reserved(dev_priv->dummy_query_bo,
1129 true);
1130 dev_priv->dummy_query_bo_pinned = true;
1131 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001132
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001133 BUG_ON(sw_context->last_query_ctx == NULL);
1134 dev_priv->query_cid = sw_context->last_query_ctx->id;
1135 dev_priv->query_cid_valid = true;
1136 dev_priv->pinned_bo =
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001137 vmw_bo_reference(sw_context->cur_query_bo);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001138 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001139 }
1140}
1141
1142/**
Thomas Hellstromddcda242012-11-21 11:26:55 +01001143 * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
1144 * handle to a MOB id.
1145 *
1146 * @dev_priv: Pointer to a device private structure.
1147 * @sw_context: The software context used for this command batch validation.
1148 * @id: Pointer to the user-space handle to be translated.
1149 * @vmw_bo_p: Points to a location that, on successful return will carry
Thomas Hellstromb139d432018-09-26 16:27:54 +02001150 * a non-reference-counted pointer to the buffer object identified by the
Thomas Hellstromddcda242012-11-21 11:26:55 +01001151 * user-space handle in @id.
1152 *
1153 * This function saves information needed to translate a user-space buffer
1154 * handle to a MOB id. The translation does not take place immediately, but
1155 * during a call to vmw_apply_relocations(). This function builds a relocation
1156 * list and a list of buffers to validate. The former needs to be freed using
1157 * either vmw_apply_relocations() or vmw_free_relocations(). The latter
1158 * needs to be freed using vmw_clear_validations.
1159 */
1160static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
1161 struct vmw_sw_context *sw_context,
1162 SVGAMobId *id,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001163 struct vmw_buffer_object **vmw_bo_p)
Thomas Hellstromddcda242012-11-21 11:26:55 +01001164{
Thomas Hellstromb139d432018-09-26 16:27:54 +02001165 struct vmw_buffer_object *vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001166 uint32_t handle = *id;
1167 struct vmw_relocation *reloc;
1168 int ret;
1169
Thomas Hellstromb139d432018-09-26 16:27:54 +02001170 vmw_validation_preload_bo(sw_context->ctx);
1171 vmw_bo = vmw_user_bo_noref_lookup(sw_context->fp->tfile, handle);
1172 if (IS_ERR(vmw_bo)) {
Thomas Hellstromddcda242012-11-21 11:26:55 +01001173 DRM_ERROR("Could not find or use MOB buffer.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001174 return PTR_ERR(vmw_bo);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001175 }
Thomas Hellstromddcda242012-11-21 11:26:55 +01001176
Thomas Hellstromb139d432018-09-26 16:27:54 +02001177 ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo, true, false);
1178 vmw_user_bo_noref_release();
1179 if (unlikely(ret != 0))
1180 return ret;
1181
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001182 reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1183 if (!reloc)
Thomas Hellstromb139d432018-09-26 16:27:54 +02001184 return -ENOMEM;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001185
Thomas Hellstromddcda242012-11-21 11:26:55 +01001186 reloc->mob_loc = id;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001187 reloc->vbo = vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001188
Thomas Hellstromddcda242012-11-21 11:26:55 +01001189 *vmw_bo_p = vmw_bo;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001190 list_add_tail(&reloc->head, &sw_context->bo_relocations);
1191
Thomas Hellstromddcda242012-11-21 11:26:55 +01001192 return 0;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001193}
1194
1195/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001196 * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
1197 * handle to a valid SVGAGuestPtr
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001198 *
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001199 * @dev_priv: Pointer to a device private structure.
1200 * @sw_context: The software context used for this command batch validation.
1201 * @ptr: Pointer to the user-space handle to be translated.
1202 * @vmw_bo_p: Points to a location that, on successful return will carry
Thomas Hellstromb139d432018-09-26 16:27:54 +02001203 * a non-reference-counted pointer to the DMA buffer identified by the
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001204 * user-space handle in @id.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001205 *
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001206 * This function saves information needed to translate a user-space buffer
1207 * handle to a valid SVGAGuestPtr. The translation does not take place
1208 * immediately, but during a call to vmw_apply_relocations().
1209 * This function builds a relocation list and a list of buffers to validate.
1210 * The former needs to be freed using either vmw_apply_relocations() or
1211 * vmw_free_relocations(). The latter needs to be freed using
1212 * vmw_clear_validations.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001213 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001214static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
1215 struct vmw_sw_context *sw_context,
1216 SVGAGuestPtr *ptr,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001217 struct vmw_buffer_object **vmw_bo_p)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001218{
Thomas Hellstromb139d432018-09-26 16:27:54 +02001219 struct vmw_buffer_object *vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001220 uint32_t handle = ptr->gmrId;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001221 struct vmw_relocation *reloc;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001222 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001223
Thomas Hellstromb139d432018-09-26 16:27:54 +02001224 vmw_validation_preload_bo(sw_context->ctx);
1225 vmw_bo = vmw_user_bo_noref_lookup(sw_context->fp->tfile, handle);
1226 if (IS_ERR(vmw_bo)) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001227 DRM_ERROR("Could not find or use GMR region.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001228 return PTR_ERR(vmw_bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001229 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001230
Thomas Hellstromb139d432018-09-26 16:27:54 +02001231 ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo, false, false);
1232 vmw_user_bo_noref_release();
1233 if (unlikely(ret != 0))
1234 return ret;
1235
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001236 reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1237 if (!reloc)
Thomas Hellstromb139d432018-09-26 16:27:54 +02001238 return -ENOMEM;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001239
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001240 reloc->location = ptr;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001241 reloc->vbo = vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001242 *vmw_bo_p = vmw_bo;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001243 list_add_tail(&reloc->head, &sw_context->bo_relocations);
1244
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001245 return 0;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001246}
1247
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001248
1249
1250/**
1251 * vmw_cmd_dx_define_query - validate a SVGA_3D_CMD_DX_DEFINE_QUERY command.
1252 *
1253 * @dev_priv: Pointer to a device private struct.
1254 * @sw_context: The software context used for this command submission.
1255 * @header: Pointer to the command header in the command stream.
1256 *
1257 * This function adds the new query into the query COTABLE
1258 */
1259static int vmw_cmd_dx_define_query(struct vmw_private *dev_priv,
1260 struct vmw_sw_context *sw_context,
1261 SVGA3dCmdHeader *header)
1262{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001263 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDefineQuery);
Deepak Rawat6f74fd92019-02-08 12:53:57 -08001264 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001265 struct vmw_resource *cotable_res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001266 int ret;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001267
Deepak Rawat6f74fd92019-02-08 12:53:57 -08001268 if (!ctx_node)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001269 return -EINVAL;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001270
Deepak Rawatd01316d2019-02-08 15:50:40 -08001271 cmd = container_of(header, typeof(*cmd), header);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001272
Deepak Rawatd01316d2019-02-08 15:50:40 -08001273 if (cmd->body.type < SVGA3D_QUERYTYPE_MIN ||
1274 cmd->body.type >= SVGA3D_QUERYTYPE_MAX)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001275 return -EINVAL;
1276
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001277 cotable_res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXQUERY);
Deepak Rawatd01316d2019-02-08 15:50:40 -08001278 ret = vmw_cotable_notify(cotable_res, cmd->body.queryId);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001279
1280 return ret;
1281}
1282
1283
1284
1285/**
1286 * vmw_cmd_dx_bind_query - validate a SVGA_3D_CMD_DX_BIND_QUERY command.
1287 *
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 *
1292 * The query bind operation will eventually associate the query ID
1293 * with its backing MOB. In this function, we take the user mode
1294 * MOB ID and use vmw_translate_mob_ptr() to translate it to its
1295 * kernel mode equivalent.
1296 */
1297static int vmw_cmd_dx_bind_query(struct vmw_private *dev_priv,
1298 struct vmw_sw_context *sw_context,
1299 SVGA3dCmdHeader *header)
1300{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001301 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindQuery);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001302 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001303 int ret;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001304
Deepak Rawatd01316d2019-02-08 15:50:40 -08001305 cmd = container_of(header, typeof(*cmd), header);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001306
1307 /*
1308 * Look up the buffer pointed to by q.mobid, put it on the relocation
1309 * list so its kernel mode MOB ID can be filled in later
1310 */
Deepak Rawatd01316d2019-02-08 15:50:40 -08001311 ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->body.mobid,
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001312 &vmw_bo);
1313
1314 if (ret != 0)
1315 return ret;
1316
1317 sw_context->dx_query_mob = vmw_bo;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001318 sw_context->dx_query_ctx = sw_context->dx_ctx_node->ctx;
Thomas Hellstromb139d432018-09-26 16:27:54 +02001319 return 0;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001320}
1321
1322
1323
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001324/**
Thomas Hellstromddcda242012-11-21 11:26:55 +01001325 * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_QUERY command.
1326 *
1327 * @dev_priv: Pointer to a device private struct.
1328 * @sw_context: The software context used for this command submission.
1329 * @header: Pointer to the command header in the command stream.
1330 */
1331static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
1332 struct vmw_sw_context *sw_context,
1333 SVGA3dCmdHeader *header)
1334{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001335 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBeginGBQuery) =
1336 container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001337
1338 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001339 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001340 &cmd->body.cid, NULL);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001341}
1342
1343/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001344 * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
1345 *
1346 * @dev_priv: Pointer to a device private struct.
1347 * @sw_context: The software context used for this command submission.
1348 * @header: Pointer to the command header in the command stream.
1349 */
1350static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
1351 struct vmw_sw_context *sw_context,
1352 SVGA3dCmdHeader *header)
1353{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001354 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBeginQuery) =
1355 container_of(header, typeof(*cmd), header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001356
Thomas Hellstromddcda242012-11-21 11:26:55 +01001357 if (unlikely(dev_priv->has_mob)) {
Deepak Rawatd01316d2019-02-08 15:50:40 -08001358 VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdBeginGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001359
1360 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1361
1362 gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
1363 gb_cmd.header.size = cmd->header.size;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001364 gb_cmd.body.cid = cmd->body.cid;
1365 gb_cmd.body.type = cmd->body.type;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001366
1367 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1368 return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
1369 }
1370
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001371 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001372 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001373 &cmd->body.cid, NULL);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001374}
1375
1376/**
Thomas Hellstromddcda242012-11-21 11:26:55 +01001377 * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
1378 *
1379 * @dev_priv: Pointer to a device private struct.
1380 * @sw_context: The software context used for this command submission.
1381 * @header: Pointer to the command header in the command stream.
1382 */
1383static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
1384 struct vmw_sw_context *sw_context,
1385 SVGA3dCmdHeader *header)
1386{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001387 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001388 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdEndGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001389 int ret;
1390
Deepak Rawatd01316d2019-02-08 15:50:40 -08001391 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001392 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1393 if (unlikely(ret != 0))
1394 return ret;
1395
1396 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001397 &cmd->body.mobid,
Thomas Hellstromddcda242012-11-21 11:26:55 +01001398 &vmw_bo);
1399 if (unlikely(ret != 0))
1400 return ret;
1401
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001402 ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001403
Thomas Hellstromddcda242012-11-21 11:26:55 +01001404 return ret;
1405}
1406
1407/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001408 * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
1409 *
1410 * @dev_priv: Pointer to a device private struct.
1411 * @sw_context: The software context used for this command submission.
1412 * @header: Pointer to the command header in the command stream.
1413 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001414static int vmw_cmd_end_query(struct vmw_private *dev_priv,
1415 struct vmw_sw_context *sw_context,
1416 SVGA3dCmdHeader *header)
1417{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001418 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001419 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdEndQuery);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001420 int ret;
1421
Deepak Rawatd01316d2019-02-08 15:50:40 -08001422 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001423 if (dev_priv->has_mob) {
Deepak Rawatd01316d2019-02-08 15:50:40 -08001424 VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdEndGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001425
1426 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1427
1428 gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
1429 gb_cmd.header.size = cmd->header.size;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001430 gb_cmd.body.cid = cmd->body.cid;
1431 gb_cmd.body.type = cmd->body.type;
1432 gb_cmd.body.mobid = cmd->body.guestResult.gmrId;
1433 gb_cmd.body.offset = cmd->body.guestResult.offset;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001434
1435 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1436 return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
1437 }
1438
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001439 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1440 if (unlikely(ret != 0))
1441 return ret;
1442
1443 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001444 &cmd->body.guestResult,
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001445 &vmw_bo);
1446 if (unlikely(ret != 0))
1447 return ret;
1448
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001449 ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001450
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001451 return ret;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001452}
1453
Thomas Hellstromddcda242012-11-21 11:26:55 +01001454/**
1455 * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
1456 *
1457 * @dev_priv: Pointer to a device private struct.
1458 * @sw_context: The software context used for this command submission.
1459 * @header: Pointer to the command header in the command stream.
1460 */
1461static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
1462 struct vmw_sw_context *sw_context,
1463 SVGA3dCmdHeader *header)
1464{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001465 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001466 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdWaitForGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001467 int ret;
1468
Deepak Rawatd01316d2019-02-08 15:50:40 -08001469 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001470 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1471 if (unlikely(ret != 0))
1472 return ret;
1473
1474 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001475 &cmd->body.mobid,
Thomas Hellstromddcda242012-11-21 11:26:55 +01001476 &vmw_bo);
1477 if (unlikely(ret != 0))
1478 return ret;
1479
Thomas Hellstromddcda242012-11-21 11:26:55 +01001480 return 0;
1481}
1482
1483/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001484 * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
1485 *
1486 * @dev_priv: Pointer to a device private struct.
1487 * @sw_context: The software context used for this command submission.
1488 * @header: Pointer to the command header in the command stream.
1489 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001490static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
1491 struct vmw_sw_context *sw_context,
1492 SVGA3dCmdHeader *header)
1493{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001494 struct vmw_buffer_object *vmw_bo;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001495 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdWaitForQuery);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001496 int ret;
1497
Deepak Rawatd01316d2019-02-08 15:50:40 -08001498 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001499 if (dev_priv->has_mob) {
Deepak Rawatd01316d2019-02-08 15:50:40 -08001500 VMW_DECLARE_CMD_VAR(gb_cmd, SVGA3dCmdWaitForGBQuery);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001501
1502 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1503
1504 gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
1505 gb_cmd.header.size = cmd->header.size;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001506 gb_cmd.body.cid = cmd->body.cid;
1507 gb_cmd.body.type = cmd->body.type;
1508 gb_cmd.body.mobid = cmd->body.guestResult.gmrId;
1509 gb_cmd.body.offset = cmd->body.guestResult.offset;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001510
1511 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1512 return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
1513 }
1514
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001515 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1516 if (unlikely(ret != 0))
1517 return ret;
1518
1519 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001520 &cmd->body.guestResult,
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001521 &vmw_bo);
1522 if (unlikely(ret != 0))
1523 return ret;
1524
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001525 return 0;
1526}
1527
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001528static int vmw_cmd_dma(struct vmw_private *dev_priv,
1529 struct vmw_sw_context *sw_context,
1530 SVGA3dCmdHeader *header)
1531{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001532 struct vmw_buffer_object *vmw_bo = NULL;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001533 struct vmw_surface *srf = NULL;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001534 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSurfaceDMA);
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001535 int ret;
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001536 SVGA3dCmdSurfaceDMASuffix *suffix;
1537 uint32_t bo_size;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001538 bool dirty;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001539
Deepak Rawatd01316d2019-02-08 15:50:40 -08001540 cmd = container_of(header, typeof(*cmd), header);
1541 suffix = (SVGA3dCmdSurfaceDMASuffix *)((unsigned long) &cmd->body +
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001542 header->size - sizeof(*suffix));
1543
1544 /* Make sure device and verifier stays in sync. */
1545 if (unlikely(suffix->suffixSize != sizeof(*suffix))) {
1546 DRM_ERROR("Invalid DMA suffix size.\n");
1547 return -EINVAL;
1548 }
1549
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001550 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001551 &cmd->body.guest.ptr,
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001552 &vmw_bo);
1553 if (unlikely(ret != 0))
1554 return ret;
1555
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001556 /* Make sure DMA doesn't cross BO boundaries. */
1557 bo_size = vmw_bo->base.num_pages * PAGE_SIZE;
Deepak Rawatd01316d2019-02-08 15:50:40 -08001558 if (unlikely(cmd->body.guest.ptr.offset > bo_size)) {
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001559 DRM_ERROR("Invalid DMA offset.\n");
1560 return -EINVAL;
1561 }
1562
Deepak Rawatd01316d2019-02-08 15:50:40 -08001563 bo_size -= cmd->body.guest.ptr.offset;
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001564 if (unlikely(suffix->maximumOffset > bo_size))
1565 suffix->maximumOffset = bo_size;
1566
Deepak Rawatd01316d2019-02-08 15:50:40 -08001567 dirty = (cmd->body.transfer == SVGA3D_WRITE_HOST_VRAM) ?
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001568 VMW_RES_DIRTY_SET : 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001569 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001570 dirty, user_surface_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001571 &cmd->body.host.sid, NULL);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +02001572 if (unlikely(ret != 0)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001573 if (unlikely(ret != -ERESTARTSYS))
1574 DRM_ERROR("could not find surface for DMA.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001575 return ret;
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +02001576 }
1577
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001578 srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001579
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001580 vmw_kms_cursor_snoop(srf, sw_context->fp->tfile, &vmw_bo->base,
1581 header);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001582
Thomas Hellstromb139d432018-09-26 16:27:54 +02001583 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001584}
1585
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001586static int vmw_cmd_draw(struct vmw_private *dev_priv,
1587 struct vmw_sw_context *sw_context,
1588 SVGA3dCmdHeader *header)
1589{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001590 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDrawPrimitives);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001591 SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
1592 (unsigned long)header + sizeof(*cmd));
1593 SVGA3dPrimitiveRange *range;
1594 uint32_t i;
1595 uint32_t maxnum;
1596 int ret;
1597
1598 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1599 if (unlikely(ret != 0))
1600 return ret;
1601
Deepak Rawatd01316d2019-02-08 15:50:40 -08001602 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001603 maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
1604
1605 if (unlikely(cmd->body.numVertexDecls > maxnum)) {
1606 DRM_ERROR("Illegal number of vertex declarations.\n");
1607 return -EINVAL;
1608 }
1609
1610 for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001611 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001612 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001613 user_surface_converter,
1614 &decl->array.surfaceId, NULL);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001615 if (unlikely(ret != 0))
1616 return ret;
1617 }
1618
1619 maxnum = (header->size - sizeof(cmd->body) -
1620 cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
1621 if (unlikely(cmd->body.numRanges > maxnum)) {
1622 DRM_ERROR("Illegal number of index ranges.\n");
1623 return -EINVAL;
1624 }
1625
1626 range = (SVGA3dPrimitiveRange *) decl;
1627 for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001628 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001629 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001630 user_surface_converter,
1631 &range->indexArray.surfaceId, NULL);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001632 if (unlikely(ret != 0))
1633 return ret;
1634 }
1635 return 0;
1636}
1637
1638
1639static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
1640 struct vmw_sw_context *sw_context,
1641 SVGA3dCmdHeader *header)
1642{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001643 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetTextureState);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001644 SVGA3dTextureState *last_state = (SVGA3dTextureState *)
1645 ((unsigned long) header + header->size + sizeof(header));
1646 SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
Deepak Rawatd01316d2019-02-08 15:50:40 -08001647 ((unsigned long) header + sizeof(*cmd));
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001648 struct vmw_resource *ctx;
1649 struct vmw_resource *res;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001650 int ret;
1651
Deepak Rawatd01316d2019-02-08 15:50:40 -08001652 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001653
1654 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001655 VMW_RES_DIRTY_SET, user_context_converter,
Deepak Rawatd01316d2019-02-08 15:50:40 -08001656 &cmd->body.cid, &ctx);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001657 if (unlikely(ret != 0))
1658 return ret;
1659
1660 for (; cur_state < last_state; ++cur_state) {
1661 if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
1662 continue;
1663
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001664 if (cur_state->stage >= SVGA3D_NUM_TEXTURE_UNITS) {
1665 DRM_ERROR("Illegal texture/sampler unit %u.\n",
1666 (unsigned) cur_state->stage);
1667 return -EINVAL;
1668 }
1669
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001670 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001671 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001672 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001673 &cur_state->value, &res);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001674 if (unlikely(ret != 0))
1675 return ret;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001676
1677 if (dev_priv->has_mob) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001678 struct vmw_ctx_bindinfo_tex binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001679 struct vmw_ctx_validation_info *node;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001680
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001681 node = vmw_execbuf_info_from_res(sw_context, ctx);
1682 if (!node)
1683 return -EINVAL;
1684
1685 binding.bi.ctx = ctx;
1686 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001687 binding.bi.bt = vmw_ctx_binding_tex;
1688 binding.texture_stage = cur_state->stage;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001689 vmw_binding_add(node->staged, &binding.bi, 0,
1690 binding.texture_stage);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001691 }
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001692 }
1693
1694 return 0;
1695}
1696
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001697static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
1698 struct vmw_sw_context *sw_context,
1699 void *buf)
1700{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001701 struct vmw_buffer_object *vmw_bo;
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001702
1703 struct {
1704 uint32_t header;
1705 SVGAFifoCmdDefineGMRFB body;
1706 } *cmd = buf;
1707
Thomas Hellstromb139d432018-09-26 16:27:54 +02001708 return vmw_translate_guest_ptr(dev_priv, sw_context,
1709 &cmd->body.ptr,
1710 &vmw_bo);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001711}
1712
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001713
1714/**
1715 * vmw_cmd_res_switch_backup - Utility function to handle backup buffer
1716 * switching
1717 *
1718 * @dev_priv: Pointer to a device private struct.
1719 * @sw_context: The software context being used for this batch.
1720 * @val_node: The validation node representing the resource.
1721 * @buf_id: Pointer to the user-space backup buffer handle in the command
1722 * stream.
1723 * @backup_offset: Offset of backup into MOB.
1724 *
1725 * This function prepares for registering a switch of backup buffers
1726 * in the resource metadata just prior to unreserving. It's basically a wrapper
1727 * around vmw_cmd_res_switch_backup with a different interface.
1728 */
1729static int vmw_cmd_res_switch_backup(struct vmw_private *dev_priv,
1730 struct vmw_sw_context *sw_context,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001731 struct vmw_resource *res,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001732 uint32_t *buf_id,
1733 unsigned long backup_offset)
1734{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001735 struct vmw_buffer_object *vbo;
1736 void *info;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001737 int ret;
1738
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001739 info = vmw_execbuf_info_from_res(sw_context, res);
1740 if (!info)
1741 return -EINVAL;
1742
1743 ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &vbo);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001744 if (ret)
1745 return ret;
1746
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001747 vmw_validation_res_switch_backup(sw_context->ctx, info, vbo,
1748 backup_offset);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001749 return 0;
1750}
1751
1752
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001753/**
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001754 * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
1755 *
1756 * @dev_priv: Pointer to a device private struct.
1757 * @sw_context: The software context being used for this batch.
1758 * @res_type: The resource type.
1759 * @converter: Information about user-space binding for this resource type.
1760 * @res_id: Pointer to the user-space resource handle in the command stream.
1761 * @buf_id: Pointer to the user-space backup buffer handle in the command
1762 * stream.
1763 * @backup_offset: Offset of backup into MOB.
1764 *
1765 * This function prepares for registering a switch of backup buffers
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001766 * in the resource metadata just prior to unreserving. It's basically a wrapper
1767 * around vmw_cmd_res_switch_backup with a different interface.
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001768 */
1769static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
1770 struct vmw_sw_context *sw_context,
1771 enum vmw_res_type res_type,
1772 const struct vmw_user_resource_conv
1773 *converter,
1774 uint32_t *res_id,
1775 uint32_t *buf_id,
1776 unsigned long backup_offset)
1777{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001778 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001779 int ret;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001780
1781 ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001782 VMW_RES_DIRTY_NONE, converter, res_id, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001783 if (ret)
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001784 return ret;
1785
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001786 return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001787 buf_id, backup_offset);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001788}
1789
1790/**
1791 * vmw_cmd_bind_gb_surface - Validate an SVGA_3D_CMD_BIND_GB_SURFACE
1792 * command
1793 *
1794 * @dev_priv: Pointer to a device private struct.
1795 * @sw_context: The software context being used for this batch.
1796 * @header: Pointer to the command header in the command stream.
1797 */
1798static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
1799 struct vmw_sw_context *sw_context,
1800 SVGA3dCmdHeader *header)
1801{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001802 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBindGBSurface) =
1803 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001804
1805 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
1806 user_surface_converter,
1807 &cmd->body.sid, &cmd->body.mobid,
1808 0);
1809}
1810
1811/**
1812 * vmw_cmd_update_gb_image - Validate an SVGA_3D_CMD_UPDATE_GB_IMAGE
1813 * command
1814 *
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_image(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, SVGA3dCmdUpdateGBImage) =
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_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001828 &cmd->body.image.sid, NULL);
1829}
1830
1831/**
1832 * vmw_cmd_update_gb_surface - Validate an SVGA_3D_CMD_UPDATE_GB_SURFACE
1833 * command
1834 *
1835 * @dev_priv: Pointer to a device private struct.
1836 * @sw_context: The software context being used for this batch.
1837 * @header: Pointer to the command header in the command stream.
1838 */
1839static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
1840 struct vmw_sw_context *sw_context,
1841 SVGA3dCmdHeader *header)
1842{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001843 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdUpdateGBSurface) =
1844 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001845
1846 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001847 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001848 &cmd->body.sid, NULL);
1849}
1850
1851/**
1852 * vmw_cmd_readback_gb_image - Validate an SVGA_3D_CMD_READBACK_GB_IMAGE
1853 * command
1854 *
1855 * @dev_priv: Pointer to a device private struct.
1856 * @sw_context: The software context being used for this batch.
1857 * @header: Pointer to the command header in the command stream.
1858 */
1859static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
1860 struct vmw_sw_context *sw_context,
1861 SVGA3dCmdHeader *header)
1862{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001863 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdReadbackGBImage) =
1864 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001865
1866 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001867 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001868 &cmd->body.image.sid, NULL);
1869}
1870
1871/**
1872 * vmw_cmd_readback_gb_surface - Validate an SVGA_3D_CMD_READBACK_GB_SURFACE
1873 * command
1874 *
1875 * @dev_priv: Pointer to a device private struct.
1876 * @sw_context: The software context being used for this batch.
1877 * @header: Pointer to the command header in the command stream.
1878 */
1879static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
1880 struct vmw_sw_context *sw_context,
1881 SVGA3dCmdHeader *header)
1882{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001883 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdReadbackGBSurface) =
1884 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001885
1886 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001887 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001888 &cmd->body.sid, NULL);
1889}
1890
1891/**
1892 * vmw_cmd_invalidate_gb_image - Validate an SVGA_3D_CMD_INVALIDATE_GB_IMAGE
1893 * command
1894 *
1895 * @dev_priv: Pointer to a device private struct.
1896 * @sw_context: The software context being used for this batch.
1897 * @header: Pointer to the command header in the command stream.
1898 */
1899static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
1900 struct vmw_sw_context *sw_context,
1901 SVGA3dCmdHeader *header)
1902{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001903 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdInvalidateGBImage) =
1904 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001905
1906 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001907 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001908 &cmd->body.image.sid, NULL);
1909}
1910
1911/**
1912 * vmw_cmd_invalidate_gb_surface - Validate an
1913 * SVGA_3D_CMD_INVALIDATE_GB_SURFACE command
1914 *
1915 * @dev_priv: Pointer to a device private struct.
1916 * @sw_context: The software context being used for this batch.
1917 * @header: Pointer to the command header in the command stream.
1918 */
1919static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
1920 struct vmw_sw_context *sw_context,
1921 SVGA3dCmdHeader *header)
1922{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001923 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdInvalidateGBSurface) =
1924 container_of(header, typeof(*cmd), header);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001925
1926 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001927 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001928 &cmd->body.sid, NULL);
1929}
1930
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001931
1932/**
1933 * vmw_cmd_shader_define - Validate an SVGA_3D_CMD_SHADER_DEFINE
1934 * command
1935 *
1936 * @dev_priv: Pointer to a device private struct.
1937 * @sw_context: The software context being used for this batch.
1938 * @header: Pointer to the command header in the command stream.
1939 */
1940static int vmw_cmd_shader_define(struct vmw_private *dev_priv,
1941 struct vmw_sw_context *sw_context,
1942 SVGA3dCmdHeader *header)
1943{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001944 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDefineShader);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001945 int ret;
1946 size_t size;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001947 struct vmw_resource *ctx;
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001948
Deepak Rawatd01316d2019-02-08 15:50:40 -08001949 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001950
1951 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001952 VMW_RES_DIRTY_SET, user_context_converter,
1953 &cmd->body.cid, &ctx);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001954 if (unlikely(ret != 0))
1955 return ret;
1956
1957 if (unlikely(!dev_priv->has_mob))
1958 return 0;
1959
1960 size = cmd->header.size - sizeof(cmd->body);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02001961 ret = vmw_compat_shader_add(dev_priv,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001962 vmw_context_res_man(ctx),
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001963 cmd->body.shid, cmd + 1,
1964 cmd->body.type, size,
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02001965 &sw_context->staged_cmd_res);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001966 if (unlikely(ret != 0))
1967 return ret;
1968
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001969 return vmw_resource_relocation_add(sw_context,
Thomas Hellstrome7a45282016-10-10 10:44:00 -07001970 NULL,
1971 vmw_ptr_diff(sw_context->buf_start,
Thomas Hellstroma1944032016-10-10 11:06:45 -07001972 &cmd->header.id),
1973 vmw_res_rel_nop);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001974}
1975
1976/**
1977 * vmw_cmd_shader_destroy - Validate an SVGA_3D_CMD_SHADER_DESTROY
1978 * command
1979 *
1980 * @dev_priv: Pointer to a device private struct.
1981 * @sw_context: The software context being used for this batch.
1982 * @header: Pointer to the command header in the command stream.
1983 */
1984static int vmw_cmd_shader_destroy(struct vmw_private *dev_priv,
1985 struct vmw_sw_context *sw_context,
1986 SVGA3dCmdHeader *header)
1987{
Deepak Rawatd01316d2019-02-08 15:50:40 -08001988 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDestroyShader);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001989 int ret;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001990 struct vmw_resource *ctx;
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001991
Deepak Rawatd01316d2019-02-08 15:50:40 -08001992 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001993
1994 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001995 VMW_RES_DIRTY_SET, user_context_converter,
1996 &cmd->body.cid, &ctx);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001997 if (unlikely(ret != 0))
1998 return ret;
1999
2000 if (unlikely(!dev_priv->has_mob))
2001 return 0;
2002
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002003 ret = vmw_shader_remove(vmw_context_res_man(ctx),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002004 cmd->body.shid,
2005 cmd->body.type,
2006 &sw_context->staged_cmd_res);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002007 if (unlikely(ret != 0))
2008 return ret;
2009
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02002010 return vmw_resource_relocation_add(sw_context,
Thomas Hellstrome7a45282016-10-10 10:44:00 -07002011 NULL,
2012 vmw_ptr_diff(sw_context->buf_start,
Thomas Hellstroma1944032016-10-10 11:06:45 -07002013 &cmd->header.id),
2014 vmw_res_rel_nop);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002015}
2016
Thomas Hellstroma97e2192012-11-21 11:45:13 +01002017/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002018 * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
2019 * command
2020 *
2021 * @dev_priv: Pointer to a device private struct.
2022 * @sw_context: The software context being used for this batch.
2023 * @header: Pointer to the command header in the command stream.
2024 */
2025static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
2026 struct vmw_sw_context *sw_context,
2027 SVGA3dCmdHeader *header)
2028{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002029 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetShader);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002030 struct vmw_ctx_bindinfo_shader binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002031 struct vmw_resource *ctx, *res = NULL;
2032 struct vmw_ctx_validation_info *ctx_info;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002033 int ret;
2034
Deepak Rawatd01316d2019-02-08 15:50:40 -08002035 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002036
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002037 if (cmd->body.type >= SVGA3D_SHADERTYPE_PREDX_MAX) {
2038 DRM_ERROR("Illegal shader type %u.\n",
2039 (unsigned) cmd->body.type);
2040 return -EINVAL;
2041 }
2042
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07002043 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002044 VMW_RES_DIRTY_SET, user_context_converter,
2045 &cmd->body.cid, &ctx);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002046 if (unlikely(ret != 0))
2047 return ret;
2048
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002049 if (!dev_priv->has_mob)
2050 return 0;
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002051
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002052 if (cmd->body.shid != SVGA3D_INVALID_ID) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002053 res = vmw_shader_lookup(vmw_context_res_man(ctx),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002054 cmd->body.shid,
2055 cmd->body.type);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002056
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002057 if (!IS_ERR(res)) {
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002058 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2059 VMW_RES_DIRTY_NONE);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002060 if (unlikely(ret != 0))
2061 return ret;
2062 }
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07002063 }
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002064
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002065 if (IS_ERR_OR_NULL(res)) {
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002066 ret = vmw_cmd_res_check(dev_priv, sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002067 vmw_res_shader, VMW_RES_DIRTY_NONE,
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002068 user_shader_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002069 &cmd->body.shid, &res);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002070 if (unlikely(ret != 0))
2071 return ret;
2072 }
2073
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002074 ctx_info = vmw_execbuf_info_from_res(sw_context, ctx);
2075 if (!ctx_info)
2076 return -EINVAL;
2077
2078 binding.bi.ctx = ctx;
2079 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002080 binding.bi.bt = vmw_ctx_binding_shader;
2081 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002082 vmw_binding_add(ctx_info->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002083 binding.shader_slot, 0);
2084 return 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002085}
2086
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002087/**
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002088 * vmw_cmd_set_shader_const - Validate an SVGA_3D_CMD_SET_SHADER_CONST
2089 * command
2090 *
2091 * @dev_priv: Pointer to a device private struct.
2092 * @sw_context: The software context being used for this batch.
2093 * @header: Pointer to the command header in the command stream.
2094 */
2095static int vmw_cmd_set_shader_const(struct vmw_private *dev_priv,
2096 struct vmw_sw_context *sw_context,
2097 SVGA3dCmdHeader *header)
2098{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002099 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdSetShaderConst);
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002100 int ret;
2101
Deepak Rawatd01316d2019-02-08 15:50:40 -08002102 cmd = container_of(header, typeof(*cmd), header);
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002103
2104 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002105 VMW_RES_DIRTY_SET, user_context_converter,
2106 &cmd->body.cid, NULL);
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002107 if (unlikely(ret != 0))
2108 return ret;
2109
2110 if (dev_priv->has_mob)
2111 header->id = SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE;
2112
2113 return 0;
2114}
2115
2116/**
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002117 * vmw_cmd_bind_gb_shader - Validate an SVGA_3D_CMD_BIND_GB_SHADER
2118 * command
2119 *
2120 * @dev_priv: Pointer to a device private struct.
2121 * @sw_context: The software context being used for this batch.
2122 * @header: Pointer to the command header in the command stream.
2123 */
2124static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
2125 struct vmw_sw_context *sw_context,
2126 SVGA3dCmdHeader *header)
2127{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002128 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdBindGBShader) =
2129 container_of(header, typeof(*cmd), header);
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002130
2131 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
2132 user_shader_converter,
2133 &cmd->body.shid, &cmd->body.mobid,
2134 cmd->body.offsetInBytes);
2135}
2136
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002137/**
2138 * vmw_cmd_dx_set_single_constant_buffer - Validate an
2139 * SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER command.
2140 *
2141 * @dev_priv: Pointer to a device private struct.
2142 * @sw_context: The software context being used for this batch.
2143 * @header: Pointer to the command header in the command stream.
2144 */
2145static int
2146vmw_cmd_dx_set_single_constant_buffer(struct vmw_private *dev_priv,
2147 struct vmw_sw_context *sw_context,
2148 SVGA3dCmdHeader *header)
2149{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002150 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetSingleConstantBuffer);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002151 struct vmw_resource *res = NULL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002152 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002153 struct vmw_ctx_bindinfo_cb binding;
2154 int ret;
2155
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002156 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002157 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002158
2159 cmd = container_of(header, typeof(*cmd), header);
2160 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002161 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002162 &cmd->body.sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002163 if (unlikely(ret != 0))
2164 return ret;
2165
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002166 binding.bi.ctx = ctx_node->ctx;
2167 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002168 binding.bi.bt = vmw_ctx_binding_cb;
2169 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2170 binding.offset = cmd->body.offsetInBytes;
2171 binding.size = cmd->body.sizeInBytes;
2172 binding.slot = cmd->body.slot;
2173
2174 if (binding.shader_slot >= SVGA3D_NUM_SHADERTYPE_DX10 ||
2175 binding.slot >= SVGA3D_DX_MAX_CONSTBUFFERS) {
2176 DRM_ERROR("Illegal const buffer shader %u slot %u.\n",
2177 (unsigned) cmd->body.type,
2178 (unsigned) binding.slot);
2179 return -EINVAL;
2180 }
2181
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002182 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002183 binding.shader_slot, binding.slot);
2184
2185 return 0;
2186}
2187
2188/**
2189 * vmw_cmd_dx_set_shader_res - Validate an
2190 * SVGA_3D_CMD_DX_SET_SHADER_RESOURCES command
2191 *
2192 * @dev_priv: Pointer to a device private struct.
2193 * @sw_context: The software context being used for this batch.
2194 * @header: Pointer to the command header in the command stream.
2195 */
2196static int vmw_cmd_dx_set_shader_res(struct vmw_private *dev_priv,
2197 struct vmw_sw_context *sw_context,
2198 SVGA3dCmdHeader *header)
2199{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002200 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetShaderResources) =
2201 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002202 u32 num_sr_view = (cmd->header.size - sizeof(cmd->body)) /
2203 sizeof(SVGA3dShaderResourceViewId);
2204
2205 if ((u64) cmd->body.startView + (u64) num_sr_view >
2206 (u64) SVGA3D_DX_MAX_SRVIEWS ||
2207 cmd->body.type >= SVGA3D_SHADERTYPE_DX10_MAX) {
2208 DRM_ERROR("Invalid shader binding.\n");
2209 return -EINVAL;
2210 }
2211
2212 return vmw_view_bindings_add(sw_context, vmw_view_sr,
2213 vmw_ctx_binding_sr,
2214 cmd->body.type - SVGA3D_SHADERTYPE_MIN,
2215 (void *) &cmd[1], num_sr_view,
2216 cmd->body.startView);
2217}
2218
2219/**
2220 * vmw_cmd_dx_set_shader - Validate an SVGA_3D_CMD_DX_SET_SHADER
2221 * command
2222 *
2223 * @dev_priv: Pointer to a device private struct.
2224 * @sw_context: The software context being used for this batch.
2225 * @header: Pointer to the command header in the command stream.
2226 */
2227static int vmw_cmd_dx_set_shader(struct vmw_private *dev_priv,
2228 struct vmw_sw_context *sw_context,
2229 SVGA3dCmdHeader *header)
2230{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002231 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetShader);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002232 struct vmw_resource *res = NULL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002233 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002234 struct vmw_ctx_bindinfo_shader binding;
2235 int ret = 0;
2236
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002237 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002238 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002239
2240 cmd = container_of(header, typeof(*cmd), header);
2241
2242 if (cmd->body.type >= SVGA3D_SHADERTYPE_DX10_MAX) {
2243 DRM_ERROR("Illegal shader type %u.\n",
2244 (unsigned) cmd->body.type);
2245 return -EINVAL;
2246 }
2247
2248 if (cmd->body.shaderId != SVGA3D_INVALID_ID) {
2249 res = vmw_shader_lookup(sw_context->man, cmd->body.shaderId, 0);
2250 if (IS_ERR(res)) {
2251 DRM_ERROR("Could not find shader for binding.\n");
2252 return PTR_ERR(res);
2253 }
2254
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002255 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2256 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002257 if (ret)
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002258 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002259 }
2260
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002261 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002262 binding.bi.res = res;
2263 binding.bi.bt = vmw_ctx_binding_dx_shader;
2264 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2265
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002266 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002267 binding.shader_slot, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002268
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002269 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002270}
2271
2272/**
2273 * vmw_cmd_dx_set_vertex_buffers - Validates an
2274 * SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS command
2275 *
2276 * @dev_priv: Pointer to a device private struct.
2277 * @sw_context: The software context being used for this batch.
2278 * @header: Pointer to the command header in the command stream.
2279 */
2280static int vmw_cmd_dx_set_vertex_buffers(struct vmw_private *dev_priv,
2281 struct vmw_sw_context *sw_context,
2282 SVGA3dCmdHeader *header)
2283{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002284 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002285 struct vmw_ctx_bindinfo_vb binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002286 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002287 struct {
2288 SVGA3dCmdHeader header;
2289 SVGA3dCmdDXSetVertexBuffers body;
2290 SVGA3dVertexBuffer buf[];
2291 } *cmd;
2292 int i, ret, num;
2293
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002294 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002295 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002296
2297 cmd = container_of(header, typeof(*cmd), header);
2298 num = (cmd->header.size - sizeof(cmd->body)) /
2299 sizeof(SVGA3dVertexBuffer);
2300 if ((u64)num + (u64)cmd->body.startBuffer >
2301 (u64)SVGA3D_DX_MAX_VERTEXBUFFERS) {
2302 DRM_ERROR("Invalid number of vertex buffers.\n");
2303 return -EINVAL;
2304 }
2305
2306 for (i = 0; i < num; i++) {
2307 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002308 VMW_RES_DIRTY_NONE,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002309 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002310 &cmd->buf[i].sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002311 if (unlikely(ret != 0))
2312 return ret;
2313
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002314 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002315 binding.bi.bt = vmw_ctx_binding_vb;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002316 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002317 binding.offset = cmd->buf[i].offset;
2318 binding.stride = cmd->buf[i].stride;
2319 binding.slot = i + cmd->body.startBuffer;
2320
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002321 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002322 0, binding.slot);
2323 }
2324
2325 return 0;
2326}
2327
2328/**
2329 * vmw_cmd_dx_ia_set_vertex_buffers - Validate an
Brian Paul8bd62872017-07-17 07:36:10 -07002330 * SVGA_3D_CMD_DX_IA_SET_INDEX_BUFFER command.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002331 *
2332 * @dev_priv: Pointer to a device private struct.
2333 * @sw_context: The software context being used for this batch.
2334 * @header: Pointer to the command header in the command stream.
2335 */
2336static int vmw_cmd_dx_set_index_buffer(struct vmw_private *dev_priv,
2337 struct vmw_sw_context *sw_context,
2338 SVGA3dCmdHeader *header)
2339{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002340 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002341 struct vmw_ctx_bindinfo_ib binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002342 struct vmw_resource *res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08002343 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetIndexBuffer);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002344 int ret;
2345
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002346 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002347 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002348
2349 cmd = container_of(header, typeof(*cmd), header);
2350 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002351 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002352 &cmd->body.sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002353 if (unlikely(ret != 0))
2354 return ret;
2355
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002356 binding.bi.ctx = ctx_node->ctx;
2357 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002358 binding.bi.bt = vmw_ctx_binding_ib;
2359 binding.offset = cmd->body.offset;
2360 binding.format = cmd->body.format;
2361
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002362 vmw_binding_add(ctx_node->staged, &binding.bi, 0, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002363
2364 return 0;
2365}
2366
2367/**
2368 * vmw_cmd_dx_set_rendertarget - Validate an
2369 * SVGA_3D_CMD_DX_SET_RENDERTARGETS command
2370 *
2371 * @dev_priv: Pointer to a device private struct.
2372 * @sw_context: The software context being used for this batch.
2373 * @header: Pointer to the command header in the command stream.
2374 */
2375static int vmw_cmd_dx_set_rendertargets(struct vmw_private *dev_priv,
2376 struct vmw_sw_context *sw_context,
2377 SVGA3dCmdHeader *header)
2378{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002379 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXSetRenderTargets) =
2380 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002381 u32 num_rt_view = (cmd->header.size - sizeof(cmd->body)) /
2382 sizeof(SVGA3dRenderTargetViewId);
Deepak Rawatd01316d2019-02-08 15:50:40 -08002383 int ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002384
2385 if (num_rt_view > SVGA3D_MAX_SIMULTANEOUS_RENDER_TARGETS) {
2386 DRM_ERROR("Invalid DX Rendertarget binding.\n");
2387 return -EINVAL;
2388 }
2389
2390 ret = vmw_view_bindings_add(sw_context, vmw_view_ds,
2391 vmw_ctx_binding_ds, 0,
2392 &cmd->body.depthStencilViewId, 1, 0);
2393 if (ret)
2394 return ret;
2395
2396 return vmw_view_bindings_add(sw_context, vmw_view_rt,
2397 vmw_ctx_binding_dx_rt, 0,
2398 (void *)&cmd[1], num_rt_view, 0);
2399}
2400
2401/**
2402 * vmw_cmd_dx_clear_rendertarget_view - Validate an
2403 * SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW command
2404 *
2405 * @dev_priv: Pointer to a device private struct.
2406 * @sw_context: The software context being used for this batch.
2407 * @header: Pointer to the command header in the command stream.
2408 */
2409static int vmw_cmd_dx_clear_rendertarget_view(struct vmw_private *dev_priv,
2410 struct vmw_sw_context *sw_context,
2411 SVGA3dCmdHeader *header)
2412{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002413 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXClearRenderTargetView) =
2414 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002415
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002416 return PTR_RET(vmw_view_id_val_add(sw_context, vmw_view_rt,
2417 cmd->body.renderTargetViewId));
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002418}
2419
2420/**
2421 * vmw_cmd_dx_clear_rendertarget_view - Validate an
2422 * SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW command
2423 *
2424 * @dev_priv: Pointer to a device private struct.
2425 * @sw_context: The software context being used for this batch.
2426 * @header: Pointer to the command header in the command stream.
2427 */
2428static int vmw_cmd_dx_clear_depthstencil_view(struct vmw_private *dev_priv,
2429 struct vmw_sw_context *sw_context,
2430 SVGA3dCmdHeader *header)
2431{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002432 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXClearDepthStencilView) =
2433 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002434
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002435 return PTR_RET(vmw_view_id_val_add(sw_context, vmw_view_ds,
2436 cmd->body.depthStencilViewId));
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002437}
2438
2439static int vmw_cmd_dx_view_define(struct vmw_private *dev_priv,
2440 struct vmw_sw_context *sw_context,
2441 SVGA3dCmdHeader *header)
2442{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002443 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002444 struct vmw_resource *srf;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002445 struct vmw_resource *res;
2446 enum vmw_view_type view_type;
2447 int ret;
2448 /*
2449 * This is based on the fact that all affected define commands have
2450 * the same initial command body layout.
2451 */
2452 struct {
2453 SVGA3dCmdHeader header;
2454 uint32 defined_id;
2455 uint32 sid;
2456 } *cmd;
2457
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002458 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002459 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002460
2461 view_type = vmw_view_cmd_to_type(header->id);
Dan Carpenter0d9cac02018-01-10 12:40:04 +03002462 if (view_type == vmw_view_max)
2463 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002464 cmd = container_of(header, typeof(*cmd), header);
2465 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002466 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002467 &cmd->sid, &srf);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002468 if (unlikely(ret != 0))
2469 return ret;
2470
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002471 res = vmw_context_cotable(ctx_node->ctx, vmw_view_cotables[view_type]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002472 ret = vmw_cotable_notify(res, cmd->defined_id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002473 if (unlikely(ret != 0))
2474 return ret;
2475
2476 return vmw_view_add(sw_context->man,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002477 ctx_node->ctx,
2478 srf,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002479 view_type,
2480 cmd->defined_id,
2481 header,
2482 header->size + sizeof(*header),
2483 &sw_context->staged_cmd_res);
2484}
2485
Charmaine Lee2f633e52015-08-10 10:45:11 -07002486/**
2487 * vmw_cmd_dx_set_so_targets - Validate an
2488 * SVGA_3D_CMD_DX_SET_SOTARGETS command.
2489 *
2490 * @dev_priv: Pointer to a device private struct.
2491 * @sw_context: The software context being used for this batch.
2492 * @header: Pointer to the command header in the command stream.
2493 */
2494static int vmw_cmd_dx_set_so_targets(struct vmw_private *dev_priv,
2495 struct vmw_sw_context *sw_context,
2496 SVGA3dCmdHeader *header)
2497{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002498 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Charmaine Lee2f633e52015-08-10 10:45:11 -07002499 struct vmw_ctx_bindinfo_so binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002500 struct vmw_resource *res;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002501 struct {
2502 SVGA3dCmdHeader header;
2503 SVGA3dCmdDXSetSOTargets body;
2504 SVGA3dSoTarget targets[];
2505 } *cmd;
2506 int i, ret, num;
2507
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002508 if (!ctx_node)
Charmaine Lee2f633e52015-08-10 10:45:11 -07002509 return -EINVAL;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002510
2511 cmd = container_of(header, typeof(*cmd), header);
2512 num = (cmd->header.size - sizeof(cmd->body)) /
2513 sizeof(SVGA3dSoTarget);
2514
2515 if (num > SVGA3D_DX_MAX_SOTARGETS) {
2516 DRM_ERROR("Invalid DX SO binding.\n");
2517 return -EINVAL;
2518 }
2519
2520 for (i = 0; i < num; i++) {
2521 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002522 VMW_RES_DIRTY_SET,
Charmaine Lee2f633e52015-08-10 10:45:11 -07002523 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002524 &cmd->targets[i].sid, &res);
Charmaine Lee2f633e52015-08-10 10:45:11 -07002525 if (unlikely(ret != 0))
2526 return ret;
2527
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002528 binding.bi.ctx = ctx_node->ctx;
2529 binding.bi.res = res;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002530 binding.bi.bt = vmw_ctx_binding_so,
2531 binding.offset = cmd->targets[i].offset;
2532 binding.size = cmd->targets[i].sizeInBytes;
2533 binding.slot = i;
2534
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002535 vmw_binding_add(ctx_node->staged, &binding.bi,
Charmaine Lee2f633e52015-08-10 10:45:11 -07002536 0, binding.slot);
2537 }
2538
2539 return 0;
2540}
2541
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002542static int vmw_cmd_dx_so_define(struct vmw_private *dev_priv,
2543 struct vmw_sw_context *sw_context,
2544 SVGA3dCmdHeader *header)
2545{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002546 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002547 struct vmw_resource *res;
2548 /*
2549 * This is based on the fact that all affected define commands have
2550 * the same initial command body layout.
2551 */
2552 struct {
2553 SVGA3dCmdHeader header;
2554 uint32 defined_id;
2555 } *cmd;
2556 enum vmw_so_type so_type;
2557 int ret;
2558
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002559 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002560 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002561
2562 so_type = vmw_so_cmd_to_type(header->id);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002563 res = vmw_context_cotable(ctx_node->ctx, vmw_so_cotables[so_type]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002564 cmd = container_of(header, typeof(*cmd), header);
2565 ret = vmw_cotable_notify(res, cmd->defined_id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002566
2567 return ret;
2568}
2569
2570/**
2571 * vmw_cmd_dx_check_subresource - Validate an
2572 * SVGA_3D_CMD_DX_[X]_SUBRESOURCE command
2573 *
2574 * @dev_priv: Pointer to a device private struct.
2575 * @sw_context: The software context being used for this batch.
2576 * @header: Pointer to the command header in the command stream.
2577 */
2578static int vmw_cmd_dx_check_subresource(struct vmw_private *dev_priv,
2579 struct vmw_sw_context *sw_context,
2580 SVGA3dCmdHeader *header)
2581{
2582 struct {
2583 SVGA3dCmdHeader header;
2584 union {
2585 SVGA3dCmdDXReadbackSubResource r_body;
2586 SVGA3dCmdDXInvalidateSubResource i_body;
2587 SVGA3dCmdDXUpdateSubResource u_body;
2588 SVGA3dSurfaceId sid;
2589 };
2590 } *cmd;
2591
2592 BUILD_BUG_ON(offsetof(typeof(*cmd), r_body.sid) !=
2593 offsetof(typeof(*cmd), sid));
2594 BUILD_BUG_ON(offsetof(typeof(*cmd), i_body.sid) !=
2595 offsetof(typeof(*cmd), sid));
2596 BUILD_BUG_ON(offsetof(typeof(*cmd), u_body.sid) !=
2597 offsetof(typeof(*cmd), sid));
2598
2599 cmd = container_of(header, typeof(*cmd), header);
2600
2601 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002602 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002603 &cmd->sid, NULL);
2604}
2605
2606static int vmw_cmd_dx_cid_check(struct vmw_private *dev_priv,
2607 struct vmw_sw_context *sw_context,
2608 SVGA3dCmdHeader *header)
2609{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002610 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002611
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002612 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002613 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002614
2615 return 0;
2616}
2617
2618/**
2619 * vmw_cmd_dx_view_remove - validate a view remove command and
2620 * schedule the view resource for removal.
2621 *
2622 * @dev_priv: Pointer to a device private struct.
2623 * @sw_context: The software context being used for this batch.
2624 * @header: Pointer to the command header in the command stream.
2625 *
2626 * Check that the view exists, and if it was not created using this
Thomas Hellstroma1944032016-10-10 11:06:45 -07002627 * command batch, conditionally make this command a NOP.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002628 */
2629static int vmw_cmd_dx_view_remove(struct vmw_private *dev_priv,
2630 struct vmw_sw_context *sw_context,
2631 SVGA3dCmdHeader *header)
2632{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002633 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002634 struct {
2635 SVGA3dCmdHeader header;
2636 union vmw_view_destroy body;
2637 } *cmd = container_of(header, typeof(*cmd), header);
2638 enum vmw_view_type view_type = vmw_view_cmd_to_type(header->id);
2639 struct vmw_resource *view;
2640 int ret;
2641
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002642 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002643 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002644
2645 ret = vmw_view_remove(sw_context->man,
2646 cmd->body.view_id, view_type,
2647 &sw_context->staged_cmd_res,
2648 &view);
2649 if (ret || !view)
2650 return ret;
2651
2652 /*
Thomas Hellstroma1944032016-10-10 11:06:45 -07002653 * If the view wasn't created during this command batch, it might
2654 * have been removed due to a context swapout, so add a
2655 * relocation to conditionally make this command a NOP to avoid
2656 * device errors.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002657 */
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02002658 return vmw_resource_relocation_add(sw_context,
Thomas Hellstroma1944032016-10-10 11:06:45 -07002659 view,
2660 vmw_ptr_diff(sw_context->buf_start,
2661 &cmd->header.id),
2662 vmw_res_rel_cond_nop);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002663}
2664
2665/**
2666 * vmw_cmd_dx_define_shader - Validate an SVGA_3D_CMD_DX_DEFINE_SHADER
2667 * command
2668 *
2669 * @dev_priv: Pointer to a device private struct.
2670 * @sw_context: The software context being used for this batch.
2671 * @header: Pointer to the command header in the command stream.
2672 */
2673static int vmw_cmd_dx_define_shader(struct vmw_private *dev_priv,
2674 struct vmw_sw_context *sw_context,
2675 SVGA3dCmdHeader *header)
2676{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002677 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002678 struct vmw_resource *res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08002679 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDefineShader) =
2680 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002681 int ret;
2682
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002683 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002684 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002685
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002686 res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXSHADER);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002687 ret = vmw_cotable_notify(res, cmd->body.shaderId);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002688 if (ret)
2689 return ret;
2690
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002691 return vmw_dx_shader_add(sw_context->man, ctx_node->ctx,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002692 cmd->body.shaderId, cmd->body.type,
2693 &sw_context->staged_cmd_res);
2694}
2695
2696/**
2697 * vmw_cmd_dx_destroy_shader - Validate an SVGA_3D_CMD_DX_DESTROY_SHADER
2698 * command
2699 *
2700 * @dev_priv: Pointer to a device private struct.
2701 * @sw_context: The software context being used for this batch.
2702 * @header: Pointer to the command header in the command stream.
2703 */
2704static int vmw_cmd_dx_destroy_shader(struct vmw_private *dev_priv,
2705 struct vmw_sw_context *sw_context,
2706 SVGA3dCmdHeader *header)
2707{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002708 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Deepak Rawatd01316d2019-02-08 15:50:40 -08002709 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXDestroyShader) =
2710 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002711 int ret;
2712
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002713 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002714 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002715
2716 ret = vmw_shader_remove(sw_context->man, cmd->body.shaderId, 0,
2717 &sw_context->staged_cmd_res);
2718 if (ret)
2719 DRM_ERROR("Could not find shader to remove.\n");
2720
2721 return ret;
2722}
2723
2724/**
2725 * vmw_cmd_dx_bind_shader - Validate an SVGA_3D_CMD_DX_BIND_SHADER
2726 * command
2727 *
2728 * @dev_priv: Pointer to a device private struct.
2729 * @sw_context: The software context being used for this batch.
2730 * @header: Pointer to the command header in the command stream.
2731 */
2732static int vmw_cmd_dx_bind_shader(struct vmw_private *dev_priv,
2733 struct vmw_sw_context *sw_context,
2734 SVGA3dCmdHeader *header)
2735{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002736 struct vmw_resource *ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002737 struct vmw_resource *res;
Deepak Rawatd01316d2019-02-08 15:50:40 -08002738 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXBindShader) =
2739 container_of(header, typeof(*cmd), header);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002740 int ret;
2741
2742 if (cmd->body.cid != SVGA3D_INVALID_ID) {
2743 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002744 VMW_RES_DIRTY_SET,
2745 user_context_converter, &cmd->body.cid,
2746 &ctx);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002747 if (ret)
2748 return ret;
2749 } else {
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002750 struct vmw_ctx_validation_info *ctx_node =
2751 VMW_GET_CTX_NODE(sw_context);
2752
2753 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002754 return -EINVAL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002755
2756 ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002757 }
2758
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002759 res = vmw_shader_lookup(vmw_context_res_man(ctx),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002760 cmd->body.shid, 0);
2761 if (IS_ERR(res)) {
2762 DRM_ERROR("Could not find shader to bind.\n");
2763 return PTR_ERR(res);
2764 }
2765
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002766 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2767 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002768 if (ret) {
2769 DRM_ERROR("Error creating resource validation node.\n");
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002770 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002771 }
2772
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002773 return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
2774 &cmd->body.mobid,
2775 cmd->body.offsetInBytes);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002776}
2777
Charmaine Leef3b335502016-02-12 08:11:56 +01002778/**
2779 * vmw_cmd_dx_genmips - Validate an SVGA_3D_CMD_DX_GENMIPS command
2780 *
2781 * @dev_priv: Pointer to a device private struct.
2782 * @sw_context: The software context being used for this batch.
2783 * @header: Pointer to the command header in the command stream.
2784 */
2785static int vmw_cmd_dx_genmips(struct vmw_private *dev_priv,
2786 struct vmw_sw_context *sw_context,
2787 SVGA3dCmdHeader *header)
2788{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002789 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXGenMips) =
2790 container_of(header, typeof(*cmd), header);
Charmaine Leef3b335502016-02-12 08:11:56 +01002791
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002792 return PTR_RET(vmw_view_id_val_add(sw_context, vmw_view_sr,
2793 cmd->body.shaderResourceViewId));
Charmaine Leef3b335502016-02-12 08:11:56 +01002794}
2795
Charmaine Lee1f982e42016-10-10 10:37:03 -07002796/**
2797 * vmw_cmd_dx_transfer_from_buffer -
2798 * Validate an SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER command
2799 *
2800 * @dev_priv: Pointer to a device private struct.
2801 * @sw_context: The software context being used for this batch.
2802 * @header: Pointer to the command header in the command stream.
2803 */
2804static int vmw_cmd_dx_transfer_from_buffer(struct vmw_private *dev_priv,
2805 struct vmw_sw_context *sw_context,
2806 SVGA3dCmdHeader *header)
2807{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002808 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDXTransferFromBuffer) =
2809 container_of(header, typeof(*cmd), header);
Charmaine Lee1f982e42016-10-10 10:37:03 -07002810 int ret;
2811
2812 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002813 VMW_RES_DIRTY_NONE, user_surface_converter,
Charmaine Lee1f982e42016-10-10 10:37:03 -07002814 &cmd->body.srcSid, NULL);
2815 if (ret != 0)
2816 return ret;
2817
2818 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002819 VMW_RES_DIRTY_SET, user_surface_converter,
Charmaine Lee1f982e42016-10-10 10:37:03 -07002820 &cmd->body.destSid, NULL);
2821}
2822
Neha Bhende0d81d342018-06-18 17:14:56 -07002823/**
2824 * vmw_cmd_intra_surface_copy -
2825 * Validate an SVGA_3D_CMD_INTRA_SURFACE_COPY command
2826 *
2827 * @dev_priv: Pointer to a device private struct.
2828 * @sw_context: The software context being used for this batch.
2829 * @header: Pointer to the command header in the command stream.
2830 */
2831static int vmw_cmd_intra_surface_copy(struct vmw_private *dev_priv,
2832 struct vmw_sw_context *sw_context,
2833 SVGA3dCmdHeader *header)
2834{
Deepak Rawatd01316d2019-02-08 15:50:40 -08002835 VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdIntraSurfaceCopy) =
2836 container_of(header, typeof(*cmd), header);
Neha Bhende0d81d342018-06-18 17:14:56 -07002837
2838 if (!(dev_priv->capabilities2 & SVGA_CAP2_INTRA_SURFACE_COPY))
2839 return -EINVAL;
2840
2841 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002842 VMW_RES_DIRTY_SET, user_surface_converter,
2843 &cmd->body.surface.sid, NULL);
Neha Bhende0d81d342018-06-18 17:14:56 -07002844}
2845
2846
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02002847static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
2848 struct vmw_sw_context *sw_context,
2849 void *buf, uint32_t *size)
2850{
2851 uint32_t size_remaining = *size;
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02002852 uint32_t cmd_id;
2853
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07002854 cmd_id = ((uint32_t *)buf)[0];
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02002855 switch (cmd_id) {
2856 case SVGA_CMD_UPDATE:
2857 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02002858 break;
2859 case SVGA_CMD_DEFINE_GMRFB:
2860 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
2861 break;
2862 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
2863 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
2864 break;
2865 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
2866 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
2867 break;
2868 default:
2869 DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
2870 return -EINVAL;
2871 }
2872
2873 if (*size > size_remaining) {
2874 DRM_ERROR("Invalid SVGA command (size mismatch):"
2875 " %u.\n", cmd_id);
2876 return -EINVAL;
2877 }
2878
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02002879 if (unlikely(!sw_context->kernel)) {
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02002880 DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
2881 return -EPERM;
2882 }
2883
2884 if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
2885 return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
2886
2887 return 0;
2888}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00002889
Thomas Hellstrom4fbd9d22014-02-12 12:37:01 +01002890static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002891 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
2892 false, false, false),
2893 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
2894 false, false, false),
2895 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
2896 true, false, false),
2897 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
2898 true, false, false),
2899 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
2900 true, false, false),
2901 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
2902 false, false, false),
2903 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
2904 false, false, false),
2905 VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
2906 true, false, false),
2907 VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
2908 true, false, false),
2909 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
2910 true, false, false),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00002911 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002912 &vmw_cmd_set_render_target_check, true, false, false),
2913 VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
2914 true, false, false),
2915 VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
2916 true, false, false),
2917 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
2918 true, false, false),
2919 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
2920 true, false, false),
2921 VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
2922 true, false, false),
2923 VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
2924 true, false, false),
2925 VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
2926 true, false, false),
2927 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
2928 false, false, false),
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002929 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_shader_define,
2930 true, false, false),
2931 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_shader_destroy,
2932 true, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002933 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
2934 true, false, false),
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002935 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_set_shader_const,
2936 true, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002937 VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
2938 true, false, false),
2939 VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
2940 true, false, false),
2941 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
2942 true, false, false),
2943 VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
2944 true, false, false),
2945 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
2946 true, false, false),
2947 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
2948 true, false, false),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00002949 VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002950 &vmw_cmd_blt_surf_screen_check, false, false, false),
2951 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
2952 false, false, false),
2953 VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
2954 false, false, false),
2955 VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
2956 false, false, false),
2957 VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
2958 false, false, false),
2959 VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
2960 false, false, false),
Deepak Rawatdc75e732018-06-13 13:53:28 -07002961 VMW_CMD_DEF(SVGA_3D_CMD_DEAD1, &vmw_cmd_invalid,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002962 false, false, false),
Deepak Rawatdc75e732018-06-13 13:53:28 -07002963 VMW_CMD_DEF(SVGA_3D_CMD_DEAD2, &vmw_cmd_invalid,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002964 false, false, false),
2965 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_BITBLT, &vmw_cmd_invalid,
2966 false, false, false),
2967 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_TRANSBLT, &vmw_cmd_invalid,
2968 false, false, false),
2969 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_STRETCHBLT, &vmw_cmd_invalid,
2970 false, false, false),
2971 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_COLORFILL, &vmw_cmd_invalid,
2972 false, false, false),
2973 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_ALPHABLEND, &vmw_cmd_invalid,
2974 false, false, false),
2975 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_CLEARTYPEBLEND, &vmw_cmd_invalid,
2976 false, false, false),
2977 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
2978 false, false, true),
2979 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
2980 false, false, true),
2981 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
2982 false, false, true),
2983 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
2984 false, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07002985 VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB64, &vmw_cmd_invalid,
2986 false, false, true),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01002987 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
2988 false, false, true),
2989 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
2990 false, false, true),
2991 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
2992 false, false, true),
2993 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
2994 true, false, true),
2995 VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
2996 false, false, true),
2997 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
2998 true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01002999 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003000 &vmw_cmd_update_gb_surface, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003001 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003002 &vmw_cmd_readback_gb_image, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003003 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003004 &vmw_cmd_readback_gb_surface, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003005 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003006 &vmw_cmd_invalidate_gb_image, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003007 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003008 &vmw_cmd_invalidate_gb_surface, true, false, true),
3009 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
3010 false, false, true),
3011 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
3012 false, false, true),
3013 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
3014 false, false, true),
3015 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
3016 false, false, true),
3017 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
3018 false, false, true),
3019 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
3020 false, false, true),
3021 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
3022 true, false, true),
3023 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
3024 false, false, true),
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +01003025 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
Thomas Hellstrom8ba07312013-10-08 02:25:35 -07003026 false, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003027 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
3028 true, false, true),
3029 VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
3030 true, false, true),
3031 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
3032 true, false, true),
3033 VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
3034 true, false, true),
Thomas Hellstrom5f55be5f2017-08-24 08:06:30 +02003035 VMW_CMD_DEF(SVGA_3D_CMD_NOP_ERROR, &vmw_cmd_ok,
3036 true, false, true),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003037 VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
3038 false, false, true),
3039 VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
3040 false, false, true),
3041 VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
3042 false, false, true),
3043 VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
3044 false, false, true),
3045 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
3046 false, false, true),
3047 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
3048 false, false, true),
3049 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
3050 false, false, true),
3051 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
3052 false, false, true),
3053 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3054 false, false, true),
3055 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3056 false, false, true),
3057 VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003058 true, false, true),
3059 VMW_CMD_DEF(SVGA_3D_CMD_GB_SCREEN_DMA, &vmw_cmd_invalid,
3060 false, false, true),
3061 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE_WITH_PITCH, &vmw_cmd_invalid,
3062 false, false, true),
3063 VMW_CMD_DEF(SVGA_3D_CMD_GB_MOB_FENCE, &vmw_cmd_invalid,
3064 false, false, true),
3065 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V2, &vmw_cmd_invalid,
3066 false, false, true),
3067
3068 /*
3069 * DX commands
3070 */
3071 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_CONTEXT, &vmw_cmd_invalid,
3072 false, false, true),
3073 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_CONTEXT, &vmw_cmd_invalid,
3074 false, false, true),
3075 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_CONTEXT, &vmw_cmd_invalid,
3076 false, false, true),
3077 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_CONTEXT, &vmw_cmd_invalid,
3078 false, false, true),
3079 VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_CONTEXT, &vmw_cmd_invalid,
3080 false, false, true),
3081 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER,
3082 &vmw_cmd_dx_set_single_constant_buffer, true, false, true),
3083 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER_RESOURCES,
3084 &vmw_cmd_dx_set_shader_res, true, false, true),
3085 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER, &vmw_cmd_dx_set_shader,
3086 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003087 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SAMPLERS, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003088 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003089 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003090 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003091 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED, &vmw_cmd_dx_cid_check,
3092 true, false, true),
3093 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INSTANCED, &vmw_cmd_dx_cid_check,
3094 true, false, true),
3095 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED_INSTANCED,
3096 &vmw_cmd_dx_cid_check, true, false, true),
3097 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_AUTO, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003098 true, false, true),
3099 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS,
3100 &vmw_cmd_dx_set_vertex_buffers, true, false, true),
3101 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INDEX_BUFFER,
3102 &vmw_cmd_dx_set_index_buffer, true, false, true),
3103 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RENDERTARGETS,
3104 &vmw_cmd_dx_set_rendertargets, true, false, true),
3105 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_BLEND_STATE, &vmw_cmd_dx_cid_check,
3106 true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003107 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_DEPTHSTENCIL_STATE,
Charmaine Lee2f633e52015-08-10 10:45:11 -07003108 &vmw_cmd_dx_cid_check, true, false, true),
3109 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RASTERIZER_STATE,
3110 &vmw_cmd_dx_cid_check, true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003111 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_QUERY, &vmw_cmd_dx_define_query,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003112 true, false, true),
Charmaine Leee02e5882016-04-12 08:19:08 -07003113 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003114 true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003115 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_QUERY, &vmw_cmd_dx_bind_query,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003116 true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003117 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_QUERY_OFFSET,
Charmaine Leee02e5882016-04-12 08:19:08 -07003118 &vmw_cmd_dx_cid_check, true, false, true),
3119 VMW_CMD_DEF(SVGA_3D_CMD_DX_BEGIN_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003120 true, false, true),
Charmaine Leee02e5882016-04-12 08:19:08 -07003121 VMW_CMD_DEF(SVGA_3D_CMD_DX_END_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003122 true, false, true),
3123 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_QUERY, &vmw_cmd_invalid,
3124 true, false, true),
Charmaine Lee18835982016-04-12 08:14:23 -07003125 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_PREDICATION, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003126 true, false, true),
3127 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VIEWPORTS, &vmw_cmd_dx_cid_check,
3128 true, false, true),
3129 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SCISSORRECTS, &vmw_cmd_dx_cid_check,
3130 true, false, true),
3131 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW,
3132 &vmw_cmd_dx_clear_rendertarget_view, true, false, true),
3133 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW,
3134 &vmw_cmd_dx_clear_depthstencil_view, true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003135 VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY, &vmw_cmd_invalid,
3136 true, false, true),
Charmaine Leef3b335502016-02-12 08:11:56 +01003137 VMW_CMD_DEF(SVGA_3D_CMD_DX_GENMIPS, &vmw_cmd_dx_genmips,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003138 true, false, true),
3139 VMW_CMD_DEF(SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE,
3140 &vmw_cmd_dx_check_subresource, true, false, true),
3141 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_SUBRESOURCE,
3142 &vmw_cmd_dx_check_subresource, true, false, true),
3143 VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_SUBRESOURCE,
3144 &vmw_cmd_dx_check_subresource, true, false, true),
3145 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW,
3146 &vmw_cmd_dx_view_define, true, false, true),
3147 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
3148 &vmw_cmd_dx_view_remove, true, false, true),
3149 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW,
3150 &vmw_cmd_dx_view_define, true, false, true),
3151 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
3152 &vmw_cmd_dx_view_remove, true, false, true),
3153 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW,
3154 &vmw_cmd_dx_view_define, true, false, true),
3155 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
3156 &vmw_cmd_dx_view_remove, true, false, true),
3157 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_ELEMENTLAYOUT,
3158 &vmw_cmd_dx_so_define, true, false, true),
3159 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_ELEMENTLAYOUT,
3160 &vmw_cmd_dx_cid_check, true, false, true),
3161 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_BLEND_STATE,
3162 &vmw_cmd_dx_so_define, true, false, true),
3163 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_BLEND_STATE,
3164 &vmw_cmd_dx_cid_check, true, false, true),
3165 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_STATE,
3166 &vmw_cmd_dx_so_define, true, false, true),
3167 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_STATE,
3168 &vmw_cmd_dx_cid_check, true, false, true),
3169 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RASTERIZER_STATE,
3170 &vmw_cmd_dx_so_define, true, false, true),
3171 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RASTERIZER_STATE,
3172 &vmw_cmd_dx_cid_check, true, false, true),
3173 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SAMPLER_STATE,
3174 &vmw_cmd_dx_so_define, true, false, true),
3175 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SAMPLER_STATE,
3176 &vmw_cmd_dx_cid_check, true, false, true),
3177 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADER,
3178 &vmw_cmd_dx_define_shader, true, false, true),
3179 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADER,
3180 &vmw_cmd_dx_destroy_shader, true, false, true),
3181 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_SHADER,
3182 &vmw_cmd_dx_bind_shader, true, false, true),
3183 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_STREAMOUTPUT,
3184 &vmw_cmd_dx_so_define, true, false, true),
3185 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_STREAMOUTPUT,
3186 &vmw_cmd_dx_cid_check, true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003187 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_STREAMOUTPUT, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003188 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003189 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SOTARGETS,
3190 &vmw_cmd_dx_set_so_targets, true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003191 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INPUT_LAYOUT,
3192 &vmw_cmd_dx_cid_check, true, false, true),
3193 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_TOPOLOGY,
3194 &vmw_cmd_dx_cid_check, true, false, true),
Neha Bhende0fca749e2015-08-10 10:51:07 -07003195 VMW_CMD_DEF(SVGA_3D_CMD_DX_BUFFER_COPY,
3196 &vmw_cmd_buffer_copy_check, true, false, true),
3197 VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY_REGION,
3198 &vmw_cmd_pred_copy_check, true, false, true),
Charmaine Lee1f982e42016-10-10 10:37:03 -07003199 VMW_CMD_DEF(SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER,
3200 &vmw_cmd_dx_transfer_from_buffer,
3201 true, false, true),
Neha Bhende0d81d342018-06-18 17:14:56 -07003202 VMW_CMD_DEF(SVGA_3D_CMD_INTRA_SURFACE_COPY, &vmw_cmd_intra_surface_copy,
3203 true, false, true),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003204};
3205
Thomas Hellstrom65b97a22017-08-24 08:06:29 +02003206bool vmw_cmd_describe(const void *buf, u32 *size, char const **cmd)
3207{
3208 u32 cmd_id = ((u32 *) buf)[0];
3209
3210 if (cmd_id >= SVGA_CMD_MAX) {
3211 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3212 const struct vmw_cmd_entry *entry;
3213
3214 *size = header->size + sizeof(SVGA3dCmdHeader);
3215 cmd_id = header->id;
3216 if (cmd_id >= SVGA_3D_CMD_MAX)
3217 return false;
3218
3219 cmd_id -= SVGA_3D_CMD_BASE;
3220 entry = &vmw_cmd_entries[cmd_id];
3221 *cmd = entry->cmd_name;
3222 return true;
3223 }
3224
3225 switch (cmd_id) {
3226 case SVGA_CMD_UPDATE:
3227 *cmd = "SVGA_CMD_UPDATE";
3228 *size = sizeof(u32) + sizeof(SVGAFifoCmdUpdate);
3229 break;
3230 case SVGA_CMD_DEFINE_GMRFB:
3231 *cmd = "SVGA_CMD_DEFINE_GMRFB";
3232 *size = sizeof(u32) + sizeof(SVGAFifoCmdDefineGMRFB);
3233 break;
3234 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3235 *cmd = "SVGA_CMD_BLIT_GMRFB_TO_SCREEN";
3236 *size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3237 break;
3238 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3239 *cmd = "SVGA_CMD_BLIT_SCREEN_TO_GMRFB";
3240 *size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3241 break;
3242 default:
3243 *cmd = "UNKNOWN";
3244 *size = 0;
3245 return false;
3246 }
3247
3248 return true;
3249}
3250
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003251static int vmw_cmd_check(struct vmw_private *dev_priv,
3252 struct vmw_sw_context *sw_context,
3253 void *buf, uint32_t *size)
3254{
3255 uint32_t cmd_id;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003256 uint32_t size_remaining = *size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003257 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3258 int ret;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003259 const struct vmw_cmd_entry *entry;
3260 bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003261
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003262 cmd_id = ((uint32_t *)buf)[0];
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003263 /* Handle any none 3D commands */
3264 if (unlikely(cmd_id < SVGA_CMD_MAX))
3265 return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
3266
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003267
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003268 cmd_id = header->id;
3269 *size = header->size + sizeof(SVGA3dCmdHeader);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003270
3271 cmd_id -= SVGA_3D_CMD_BASE;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003272 if (unlikely(*size > size_remaining))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003273 goto out_invalid;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003274
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003275 if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003276 goto out_invalid;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003277
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003278 entry = &vmw_cmd_entries[cmd_id];
Thomas Hellstrom36e952c2014-02-12 13:19:36 +01003279 if (unlikely(!entry->func))
3280 goto out_invalid;
3281
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003282 if (unlikely(!entry->user_allow && !sw_context->kernel))
3283 goto out_privileged;
3284
3285 if (unlikely(entry->gb_disable && gb))
3286 goto out_old;
3287
3288 if (unlikely(entry->gb_enable && !gb))
3289 goto out_new;
3290
3291 ret = entry->func(dev_priv, sw_context, header);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003292 if (unlikely(ret != 0))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003293 goto out_invalid;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003294
3295 return 0;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003296out_invalid:
3297 DRM_ERROR("Invalid SVGA3D command: %d\n",
3298 cmd_id + SVGA_3D_CMD_BASE);
3299 return -EINVAL;
3300out_privileged:
3301 DRM_ERROR("Privileged SVGA3D command: %d\n",
3302 cmd_id + SVGA_3D_CMD_BASE);
3303 return -EPERM;
3304out_old:
3305 DRM_ERROR("Deprecated (disallowed) SVGA3D command: %d\n",
3306 cmd_id + SVGA_3D_CMD_BASE);
3307 return -EINVAL;
3308out_new:
3309 DRM_ERROR("SVGA3D command: %d not supported by virtual hardware.\n",
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003310 cmd_id + SVGA_3D_CMD_BASE);
3311 return -EINVAL;
3312}
3313
3314static int vmw_cmd_check_all(struct vmw_private *dev_priv,
3315 struct vmw_sw_context *sw_context,
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003316 void *buf,
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003317 uint32_t size)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003318{
3319 int32_t cur_size = size;
3320 int ret;
3321
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003322 sw_context->buf_start = buf;
3323
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003324 while (cur_size > 0) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003325 size = cur_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003326 ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
3327 if (unlikely(ret != 0))
3328 return ret;
3329 buf = (void *)((unsigned long) buf + size);
3330 cur_size -= size;
3331 }
3332
3333 if (unlikely(cur_size != 0)) {
3334 DRM_ERROR("Command verifier out of sync.\n");
3335 return -EINVAL;
3336 }
3337
3338 return 0;
3339}
3340
3341static void vmw_free_relocations(struct vmw_sw_context *sw_context)
3342{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003343 /* Memory is validation context memory, so no need to free it */
3344
3345 INIT_LIST_HEAD(&sw_context->bo_relocations);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003346}
3347
3348static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
3349{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003350 struct vmw_relocation *reloc;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003351 struct ttm_buffer_object *bo;
3352
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003353 list_for_each_entry(reloc, &sw_context->bo_relocations, head) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003354 bo = &reloc->vbo->base;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003355 switch (bo->mem.mem_type) {
3356 case TTM_PL_VRAM:
Thomas Hellstrom135cba02010-10-26 21:21:47 +02003357 reloc->location->offset += bo->offset;
3358 reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003359 break;
3360 case VMW_PL_GMR:
Thomas Hellstrom135cba02010-10-26 21:21:47 +02003361 reloc->location->gmrId = bo->mem.start;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003362 break;
Thomas Hellstromddcda242012-11-21 11:26:55 +01003363 case VMW_PL_MOB:
3364 *reloc->mob_loc = bo->mem.start;
3365 break;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003366 default:
3367 BUG();
3368 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003369 }
3370 vmw_free_relocations(sw_context);
3371}
3372
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003373static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
3374 uint32_t size)
3375{
3376 if (likely(sw_context->cmd_bounce_size >= size))
3377 return 0;
3378
3379 if (sw_context->cmd_bounce_size == 0)
3380 sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
3381
3382 while (sw_context->cmd_bounce_size < size) {
3383 sw_context->cmd_bounce_size =
3384 PAGE_ALIGN(sw_context->cmd_bounce_size +
3385 (sw_context->cmd_bounce_size >> 1));
3386 }
3387
Markus Elfring0bc32992016-07-22 13:31:00 +02003388 vfree(sw_context->cmd_bounce);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003389 sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
3390
3391 if (sw_context->cmd_bounce == NULL) {
3392 DRM_ERROR("Failed to allocate command bounce buffer.\n");
3393 sw_context->cmd_bounce_size = 0;
3394 return -ENOMEM;
3395 }
3396
3397 return 0;
3398}
3399
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003400/**
3401 * vmw_execbuf_fence_commands - create and submit a command stream fence
3402 *
3403 * Creates a fence object and submits a command stream marker.
3404 * If this fails for some reason, We sync the fifo and return NULL.
3405 * It is then safe to fence buffers with a NULL pointer.
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +02003406 *
3407 * If @p_handle is not NULL @file_priv must also not be NULL. Creates
3408 * a userspace handle if @p_handle is not NULL, otherwise not.
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003409 */
3410
3411int vmw_execbuf_fence_commands(struct drm_file *file_priv,
3412 struct vmw_private *dev_priv,
3413 struct vmw_fence_obj **p_fence,
3414 uint32_t *p_handle)
3415{
3416 uint32_t sequence;
3417 int ret;
3418 bool synced = false;
3419
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +02003420 /* p_handle implies file_priv. */
3421 BUG_ON(p_handle != NULL && file_priv == NULL);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003422
3423 ret = vmw_fifo_send_fence(dev_priv, &sequence);
3424 if (unlikely(ret != 0)) {
3425 DRM_ERROR("Fence submission error. Syncing.\n");
3426 synced = true;
3427 }
3428
3429 if (p_handle != NULL)
3430 ret = vmw_user_fence_create(file_priv, dev_priv->fman,
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003431 sequence, p_fence, p_handle);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003432 else
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003433 ret = vmw_fence_create(dev_priv->fman, sequence, p_fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003434
3435 if (unlikely(ret != 0 && !synced)) {
3436 (void) vmw_fallback_wait(dev_priv, false, false,
3437 sequence, false,
3438 VMW_FENCE_WAIT_TIMEOUT);
3439 *p_fence = NULL;
3440 }
3441
Thomas Hellstrom728354c2019-01-31 10:55:37 +01003442 return ret;
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003443}
3444
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003445/**
3446 * vmw_execbuf_copy_fence_user - copy fence object information to
3447 * user-space.
3448 *
3449 * @dev_priv: Pointer to a vmw_private struct.
3450 * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
3451 * @ret: Return value from fence object creation.
3452 * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
3453 * which the information should be copied.
3454 * @fence: Pointer to the fenc object.
3455 * @fence_handle: User-space fence handle.
Sinclair Yehc906965d2017-07-05 01:49:32 -07003456 * @out_fence_fd: exported file descriptor for the fence. -1 if not used
3457 * @sync_file: Only used to clean up in case of an error in this function.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003458 *
3459 * This function copies fence information to user-space. If copying fails,
3460 * The user-space struct drm_vmw_fence_rep::error member is hopefully
3461 * left untouched, and if it's preloaded with an -EFAULT by user-space,
3462 * the error will hopefully be detected.
3463 * Also if copying fails, user-space will be unable to signal the fence
3464 * object so we wait for it immediately, and then unreference the
3465 * user-space reference.
3466 */
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02003467void
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003468vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
3469 struct vmw_fpriv *vmw_fp,
3470 int ret,
3471 struct drm_vmw_fence_rep __user *user_fence_rep,
3472 struct vmw_fence_obj *fence,
Sinclair Yehc906965d2017-07-05 01:49:32 -07003473 uint32_t fence_handle,
3474 int32_t out_fence_fd,
3475 struct sync_file *sync_file)
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003476{
3477 struct drm_vmw_fence_rep fence_rep;
3478
3479 if (user_fence_rep == NULL)
3480 return;
3481
Dan Carpenter80d9b242011-10-18 09:10:12 +03003482 memset(&fence_rep, 0, sizeof(fence_rep));
3483
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003484 fence_rep.error = ret;
Sinclair Yehc906965d2017-07-05 01:49:32 -07003485 fence_rep.fd = out_fence_fd;
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003486 if (ret == 0) {
3487 BUG_ON(fence == NULL);
3488
3489 fence_rep.handle = fence_handle;
Maarten Lankhorst2298e802014-03-26 14:07:44 +01003490 fence_rep.seqno = fence->base.seqno;
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003491 vmw_update_seqno(dev_priv, &dev_priv->fifo);
3492 fence_rep.passed_seqno = dev_priv->last_read_seqno;
3493 }
3494
3495 /*
3496 * copy_to_user errors will be detected by user space not
3497 * seeing fence_rep::error filled in. Typically
3498 * user-space would have pre-set that member to -EFAULT.
3499 */
3500 ret = copy_to_user(user_fence_rep, &fence_rep,
3501 sizeof(fence_rep));
3502
3503 /*
3504 * User-space lost the fence object. We need to sync
3505 * and unreference the handle.
3506 */
3507 if (unlikely(ret != 0) && (fence_rep.error == 0)) {
Sinclair Yehc906965d2017-07-05 01:49:32 -07003508 if (sync_file)
3509 fput(sync_file->file);
3510
3511 if (fence_rep.fd != -1) {
3512 put_unused_fd(fence_rep.fd);
3513 fence_rep.fd = -1;
3514 }
3515
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003516 ttm_ref_object_base_unref(vmw_fp->tfile,
3517 fence_handle, TTM_REF_USAGE);
3518 DRM_ERROR("Fence copy error. Syncing.\n");
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003519 (void) vmw_fence_obj_wait(fence, false, false,
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003520 VMW_FENCE_WAIT_TIMEOUT);
3521 }
3522}
3523
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003524/**
3525 * vmw_execbuf_submit_fifo - Patch a command batch and submit it using
3526 * the fifo.
3527 *
3528 * @dev_priv: Pointer to a device private structure.
3529 * @kernel_commands: Pointer to the unpatched command batch.
3530 * @command_size: Size of the unpatched command batch.
3531 * @sw_context: Structure holding the relocation lists.
3532 *
3533 * Side effects: If this function returns 0, then the command batch
3534 * pointed to by @kernel_commands will have been modified.
3535 */
3536static int vmw_execbuf_submit_fifo(struct vmw_private *dev_priv,
3537 void *kernel_commands,
3538 u32 command_size,
3539 struct vmw_sw_context *sw_context)
3540{
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003541 void *cmd;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003542
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003543 if (sw_context->dx_ctx_node)
3544 cmd = vmw_fifo_reserve_dx(dev_priv, command_size,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003545 sw_context->dx_ctx_node->ctx->id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003546 else
3547 cmd = vmw_fifo_reserve(dev_priv, command_size);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003548 if (!cmd) {
3549 DRM_ERROR("Failed reserving fifo space for commands.\n");
3550 return -ENOMEM;
3551 }
3552
3553 vmw_apply_relocations(sw_context);
3554 memcpy(cmd, kernel_commands, command_size);
3555 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
3556 vmw_resource_relocations_free(&sw_context->res_relocations);
3557 vmw_fifo_commit(dev_priv, command_size);
3558
3559 return 0;
3560}
3561
3562/**
3563 * vmw_execbuf_submit_cmdbuf - Patch a command batch and submit it using
3564 * the command buffer manager.
3565 *
3566 * @dev_priv: Pointer to a device private structure.
3567 * @header: Opaque handle to the command buffer allocation.
3568 * @command_size: Size of the unpatched command batch.
3569 * @sw_context: Structure holding the relocation lists.
3570 *
3571 * Side effects: If this function returns 0, then the command buffer
3572 * represented by @header will have been modified.
3573 */
3574static int vmw_execbuf_submit_cmdbuf(struct vmw_private *dev_priv,
3575 struct vmw_cmdbuf_header *header,
3576 u32 command_size,
3577 struct vmw_sw_context *sw_context)
3578{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003579 u32 id = ((sw_context->dx_ctx_node) ? sw_context->dx_ctx_node->ctx->id :
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003580 SVGA3D_INVALID_ID);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003581 void *cmd = vmw_cmdbuf_reserve(dev_priv->cman, command_size,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003582 id, false, header);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003583
3584 vmw_apply_relocations(sw_context);
3585 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
3586 vmw_resource_relocations_free(&sw_context->res_relocations);
3587 vmw_cmdbuf_commit(dev_priv->cman, command_size, header, false);
3588
3589 return 0;
3590}
3591
3592/**
3593 * vmw_execbuf_cmdbuf - Prepare, if possible, a user-space command batch for
3594 * submission using a command buffer.
3595 *
3596 * @dev_priv: Pointer to a device private structure.
3597 * @user_commands: User-space pointer to the commands to be submitted.
3598 * @command_size: Size of the unpatched command batch.
3599 * @header: Out parameter returning the opaque pointer to the command buffer.
3600 *
3601 * This function checks whether we can use the command buffer manager for
3602 * submission and if so, creates a command buffer of suitable size and
3603 * copies the user data into that buffer.
3604 *
3605 * On successful return, the function returns a pointer to the data in the
3606 * command buffer and *@header is set to non-NULL.
3607 * If command buffers could not be used, the function will return the value
3608 * of @kernel_commands on function call. That value may be NULL. In that case,
3609 * the value of *@header will be set to NULL.
3610 * If an error is encountered, the function will return a pointer error value.
3611 * If the function is interrupted by a signal while sleeping, it will return
3612 * -ERESTARTSYS casted to a pointer error value.
3613 */
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003614static void *vmw_execbuf_cmdbuf(struct vmw_private *dev_priv,
3615 void __user *user_commands,
3616 void *kernel_commands,
3617 u32 command_size,
3618 struct vmw_cmdbuf_header **header)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003619{
3620 size_t cmdbuf_size;
3621 int ret;
3622
3623 *header = NULL;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003624 if (command_size > SVGA_CB_MAX_SIZE) {
3625 DRM_ERROR("Command buffer is too large.\n");
3626 return ERR_PTR(-EINVAL);
3627 }
3628
Thomas Hellstrom51ab70b2016-10-10 10:51:24 -07003629 if (!dev_priv->cman || kernel_commands)
3630 return kernel_commands;
3631
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003632 /* If possible, add a little space for fencing. */
3633 cmdbuf_size = command_size + 512;
3634 cmdbuf_size = min_t(size_t, cmdbuf_size, SVGA_CB_MAX_SIZE);
3635 kernel_commands = vmw_cmdbuf_alloc(dev_priv->cman, cmdbuf_size,
3636 true, header);
3637 if (IS_ERR(kernel_commands))
3638 return kernel_commands;
3639
3640 ret = copy_from_user(kernel_commands, user_commands,
3641 command_size);
3642 if (ret) {
3643 DRM_ERROR("Failed copying commands.\n");
3644 vmw_cmdbuf_header_free(*header);
3645 *header = NULL;
3646 return ERR_PTR(-EFAULT);
3647 }
3648
3649 return kernel_commands;
3650}
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003651
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003652static int vmw_execbuf_tie_context(struct vmw_private *dev_priv,
3653 struct vmw_sw_context *sw_context,
3654 uint32_t handle)
3655{
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003656 struct vmw_resource *res;
3657 int ret;
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003658 unsigned int size;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003659
3660 if (handle == SVGA3D_INVALID_ID)
3661 return 0;
3662
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003663 size = vmw_execbuf_res_size(dev_priv, vmw_res_dx_context);
3664 ret = vmw_validation_preload_res(sw_context->ctx, size);
3665 if (ret)
3666 return ret;
3667
3668 res = vmw_user_resource_noref_lookup_handle
3669 (dev_priv, sw_context->fp->tfile, handle,
3670 user_context_converter);
Chengguang Xu4efa6662019-03-01 10:14:06 -08003671 if (IS_ERR(res)) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003672 DRM_ERROR("Could not find or user DX context 0x%08x.\n",
3673 (unsigned) handle);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003674 return PTR_ERR(res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003675 }
3676
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01003677 ret = vmw_execbuf_res_noref_val_add(sw_context, res, VMW_RES_DIRTY_SET);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003678 if (unlikely(ret != 0))
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003679 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003680
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003681 sw_context->dx_ctx_node = vmw_execbuf_info_from_res(sw_context, res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003682 sw_context->man = vmw_context_res_man(res);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003683
3684 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003685}
3686
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003687int vmw_execbuf_process(struct drm_file *file_priv,
3688 struct vmw_private *dev_priv,
3689 void __user *user_commands,
3690 void *kernel_commands,
3691 uint32_t command_size,
3692 uint64_t throttle_us,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003693 uint32_t dx_context_handle,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01003694 struct drm_vmw_fence_rep __user *user_fence_rep,
Sinclair Yehc906965d2017-07-05 01:49:32 -07003695 struct vmw_fence_obj **out_fence,
3696 uint32_t flags)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003697{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003698 struct vmw_sw_context *sw_context = &dev_priv->ctx;
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01003699 struct vmw_fence_obj *fence = NULL;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003700 struct vmw_cmdbuf_header *header;
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003701 uint32_t handle;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003702 int ret;
Sinclair Yehc906965d2017-07-05 01:49:32 -07003703 int32_t out_fence_fd = -1;
3704 struct sync_file *sync_file = NULL;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003705 DECLARE_VAL_CONTEXT(val_ctx, &sw_context->res_ht, 1);
Sinclair Yehc906965d2017-07-05 01:49:32 -07003706
Thomas Hellstromfd567462018-12-12 11:52:08 +01003707 vmw_validation_set_val_mem(&val_ctx, &dev_priv->vvm);
3708
Sinclair Yehc906965d2017-07-05 01:49:32 -07003709 if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
3710 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
3711 if (out_fence_fd < 0) {
3712 DRM_ERROR("Failed to get a fence file descriptor.\n");
3713 return out_fence_fd;
3714 }
3715 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003716
Charmaine Lee2f633e52015-08-10 10:45:11 -07003717 if (throttle_us) {
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003718 ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
3719 throttle_us);
Charmaine Lee2f633e52015-08-10 10:45:11 -07003720
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003721 if (ret)
Sinclair Yehc906965d2017-07-05 01:49:32 -07003722 goto out_free_fence_fd;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003723 }
Charmaine Lee2f633e52015-08-10 10:45:11 -07003724
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003725 kernel_commands = vmw_execbuf_cmdbuf(dev_priv, user_commands,
3726 kernel_commands, command_size,
3727 &header);
Sinclair Yehc906965d2017-07-05 01:49:32 -07003728 if (IS_ERR(kernel_commands)) {
3729 ret = PTR_ERR(kernel_commands);
3730 goto out_free_fence_fd;
3731 }
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003732
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003733 ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003734 if (ret) {
3735 ret = -ERESTARTSYS;
3736 goto out_free_header;
3737 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003738
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003739 sw_context->kernel = false;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003740 if (kernel_commands == NULL) {
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003741 ret = vmw_resize_cmd_bounce(sw_context, command_size);
3742 if (unlikely(ret != 0))
3743 goto out_unlock;
3744
3745
3746 ret = copy_from_user(sw_context->cmd_bounce,
3747 user_commands, command_size);
3748
3749 if (unlikely(ret != 0)) {
3750 ret = -EFAULT;
3751 DRM_ERROR("Failed copying commands.\n");
3752 goto out_unlock;
3753 }
3754 kernel_commands = sw_context->cmd_bounce;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003755 } else if (!header)
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003756 sw_context->kernel = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003757
Thomas Hellstromd5bde952014-01-31 10:12:10 +01003758 sw_context->fp = vmw_fpriv(file_priv);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003759 INIT_LIST_HEAD(&sw_context->ctx_list);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003760 sw_context->cur_query_bo = dev_priv->pinned_bo;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003761 sw_context->last_query_ctx = NULL;
3762 sw_context->needs_post_query_barrier = false;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003763 sw_context->dx_ctx_node = NULL;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003764 sw_context->dx_query_mob = NULL;
3765 sw_context->dx_query_ctx = NULL;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003766 memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003767 INIT_LIST_HEAD(&sw_context->res_relocations);
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003768 INIT_LIST_HEAD(&sw_context->bo_relocations);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003769 if (sw_context->staged_bindings)
3770 vmw_binding_state_reset(sw_context->staged_bindings);
3771
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003772 if (!sw_context->res_ht_initialized) {
3773 ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
3774 if (unlikely(ret != 0))
3775 goto out_unlock;
3776 sw_context->res_ht_initialized = true;
3777 }
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003778 INIT_LIST_HEAD(&sw_context->staged_cmd_res);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003779 sw_context->ctx = &val_ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003780 ret = vmw_execbuf_tie_context(dev_priv, sw_context, dx_context_handle);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003781 if (unlikely(ret != 0))
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003782 goto out_err_nores;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003783
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003784 ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
3785 command_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003786 if (unlikely(ret != 0))
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01003787 goto out_err_nores;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003788
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003789 ret = vmw_resources_reserve(sw_context);
3790 if (unlikely(ret != 0))
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01003791 goto out_err_nores;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003792
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003793 ret = vmw_validation_bo_reserve(&val_ctx, true);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003794 if (unlikely(ret != 0))
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003795 goto out_err_nores;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003796
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003797 ret = vmw_validation_bo_validate(&val_ctx, true);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003798 if (unlikely(ret != 0))
3799 goto out_err;
3800
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003801 ret = vmw_validation_res_validate(&val_ctx, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003802 if (unlikely(ret != 0))
3803 goto out_err;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003804 vmw_validation_drop_ht(&val_ctx);
Thomas Hellstrom1925d452010-05-28 11:21:57 +02003805
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07003806 ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
3807 if (unlikely(ret != 0)) {
3808 ret = -ERESTARTSYS;
3809 goto out_err;
3810 }
3811
Thomas Hellstrom30f82d812014-02-05 08:13:56 +01003812 if (dev_priv->has_mob) {
3813 ret = vmw_rebind_contexts(sw_context);
3814 if (unlikely(ret != 0))
Dan Carpenterb2ad9882014-02-11 19:03:47 +03003815 goto out_unlock_binding;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +01003816 }
3817
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003818 if (!header) {
3819 ret = vmw_execbuf_submit_fifo(dev_priv, kernel_commands,
3820 command_size, sw_context);
3821 } else {
3822 ret = vmw_execbuf_submit_cmdbuf(dev_priv, header, command_size,
3823 sw_context);
3824 header = NULL;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003825 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003826 mutex_unlock(&dev_priv->binding_mutex);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003827 if (ret)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003828 goto out_err;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003829
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003830 vmw_query_bo_switch_commit(dev_priv, sw_context);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003831 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
3832 &fence,
3833 (user_fence_rep) ? &handle : NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003834 /*
3835 * This error is harmless, because if fence submission fails,
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003836 * vmw_fifo_send_fence will sync. The error will be propagated to
3837 * user-space in @fence_rep
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003838 */
3839
3840 if (ret != 0)
3841 DRM_ERROR("Fence submission error. Syncing.\n");
3842
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003843 vmw_execbuf_bindings_commit(sw_context, false);
3844 vmw_bind_dx_query_mob(sw_context);
3845 vmw_validation_res_unreserve(&val_ctx, false);
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07003846
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003847 vmw_validation_bo_fence(sw_context->ctx, fence);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003848
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003849 if (unlikely(dev_priv->pinned_bo != NULL &&
3850 !dev_priv->query_cid_valid))
3851 __vmw_execbuf_release_pinned_bo(dev_priv, fence);
3852
Sinclair Yehc906965d2017-07-05 01:49:32 -07003853 /*
3854 * If anything fails here, give up trying to export the fence
3855 * and do a sync since the user mode will not be able to sync
3856 * the fence itself. This ensures we are still functionally
3857 * correct.
3858 */
3859 if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
3860
3861 sync_file = sync_file_create(&fence->base);
3862 if (!sync_file) {
3863 DRM_ERROR("Unable to create sync file for fence\n");
3864 put_unused_fd(out_fence_fd);
3865 out_fence_fd = -1;
3866
3867 (void) vmw_fence_obj_wait(fence, false, false,
3868 VMW_FENCE_WAIT_TIMEOUT);
3869 } else {
3870 /* Link the fence with the FD created earlier */
3871 fd_install(out_fence_fd, sync_file->file);
3872 }
3873 }
3874
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003875 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
Sinclair Yehc906965d2017-07-05 01:49:32 -07003876 user_fence_rep, fence, handle,
3877 out_fence_fd, sync_file);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003878
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01003879 /* Don't unreference when handing fence out */
3880 if (unlikely(out_fence != NULL)) {
3881 *out_fence = fence;
3882 fence = NULL;
3883 } else if (likely(fence != NULL)) {
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003884 vmw_fence_obj_unreference(&fence);
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01003885 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003886
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003887 vmw_cmdbuf_res_commit(&sw_context->staged_cmd_res);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003888 mutex_unlock(&dev_priv->cmdbuf_mutex);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003889
3890 /*
3891 * Unreference resources outside of the cmdbuf_mutex to
3892 * avoid deadlocks in resource destruction paths.
3893 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003894 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003895
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003896 return 0;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003897
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07003898out_unlock_binding:
3899 mutex_unlock(&dev_priv->binding_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003900out_err:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003901 vmw_validation_bo_backoff(&val_ctx);
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01003902out_err_nores:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003903 vmw_execbuf_bindings_commit(sw_context, true);
3904 vmw_validation_res_unreserve(&val_ctx, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003905 vmw_resource_relocations_free(&sw_context->res_relocations);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003906 vmw_free_relocations(sw_context);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003907 if (unlikely(dev_priv->pinned_bo != NULL &&
3908 !dev_priv->query_cid_valid))
3909 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003910out_unlock:
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003911 vmw_cmdbuf_res_revert(&sw_context->staged_cmd_res);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003912 vmw_validation_drop_ht(&val_ctx);
3913 WARN_ON(!list_empty(&sw_context->ctx_list));
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003914 mutex_unlock(&dev_priv->cmdbuf_mutex);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003915
3916 /*
3917 * Unreference resources outside of the cmdbuf_mutex to
3918 * avoid deadlocks in resource destruction paths.
3919 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003920 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003921out_free_header:
3922 if (header)
3923 vmw_cmdbuf_header_free(header);
Sinclair Yehc906965d2017-07-05 01:49:32 -07003924out_free_fence_fd:
3925 if (out_fence_fd >= 0)
3926 put_unused_fd(out_fence_fd);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003927
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003928 return ret;
3929}
3930
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003931/**
3932 * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
3933 *
3934 * @dev_priv: The device private structure.
3935 *
3936 * This function is called to idle the fifo and unpin the query buffer
3937 * if the normal way to do this hits an error, which should typically be
3938 * extremely rare.
3939 */
3940static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
3941{
3942 DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
3943
3944 (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07003945 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
3946 if (dev_priv->dummy_query_bo_pinned) {
3947 vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
3948 dev_priv->dummy_query_bo_pinned = false;
3949 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003950}
3951
3952
3953/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003954 * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003955 * query bo.
3956 *
3957 * @dev_priv: The device private structure.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003958 * @fence: If non-NULL should point to a struct vmw_fence_obj issued
3959 * _after_ a query barrier that flushes all queries touching the current
3960 * buffer pointed to by @dev_priv->pinned_bo
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003961 *
3962 * This function should be used to unpin the pinned query bo, or
3963 * as a query barrier when we need to make sure that all queries have
3964 * finished before the next fifo command. (For example on hardware
3965 * context destructions where the hardware may otherwise leak unfinished
3966 * queries).
3967 *
3968 * This function does not return any failure codes, but make attempts
3969 * to do safe unpinning in case of errors.
3970 *
3971 * The function will synchronize on the previous query barrier, and will
3972 * thus not finish until that barrier has executed.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003973 *
3974 * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
3975 * before calling this function.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003976 */
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003977void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
3978 struct vmw_fence_obj *fence)
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003979{
3980 int ret = 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003981 struct vmw_fence_obj *lfence = NULL;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003982 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003983
3984 if (dev_priv->pinned_bo == NULL)
3985 goto out_unlock;
3986
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003987 ret = vmw_validation_add_bo(&val_ctx, dev_priv->pinned_bo, false,
3988 false);
3989 if (ret)
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003990 goto out_no_reserve;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003991
3992 ret = vmw_validation_add_bo(&val_ctx, dev_priv->dummy_query_bo, false,
3993 false);
3994 if (ret)
3995 goto out_no_reserve;
3996
3997 ret = vmw_validation_bo_reserve(&val_ctx, false);
3998 if (ret)
3999 goto out_no_reserve;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004000
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004001 if (dev_priv->query_cid_valid) {
4002 BUG_ON(fence != NULL);
4003 ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004004 if (ret)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004005 goto out_no_emit;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004006 dev_priv->query_cid_valid = false;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004007 }
4008
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07004009 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
4010 if (dev_priv->dummy_query_bo_pinned) {
4011 vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
4012 dev_priv->dummy_query_bo_pinned = false;
4013 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004014 if (fence == NULL) {
4015 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
4016 NULL);
4017 fence = lfence;
4018 }
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004019 vmw_validation_bo_fence(&val_ctx, fence);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004020 if (lfence != NULL)
4021 vmw_fence_obj_unreference(&lfence);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004022
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004023 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02004024 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004025out_unlock:
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004026 return;
4027
4028out_no_emit:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004029 vmw_validation_bo_backoff(&val_ctx);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004030out_no_reserve:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004031 vmw_validation_unref_lists(&val_ctx);
4032 vmw_execbuf_unpin_panic(dev_priv);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02004033 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004034
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004035}
4036
4037/**
4038 * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
4039 * query bo.
4040 *
4041 * @dev_priv: The device private structure.
4042 *
4043 * This function should be used to unpin the pinned query bo, or
4044 * as a query barrier when we need to make sure that all queries have
4045 * finished before the next fifo command. (For example on hardware
4046 * context destructions where the hardware may otherwise leak unfinished
4047 * queries).
4048 *
4049 * This function does not return any failure codes, but make attempts
4050 * to do safe unpinning in case of errors.
4051 *
4052 * The function will synchronize on the previous query barrier, and will
4053 * thus not finish until that barrier has executed.
4054 */
4055void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
4056{
4057 mutex_lock(&dev_priv->cmdbuf_mutex);
4058 if (dev_priv->query_cid_valid)
4059 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004060 mutex_unlock(&dev_priv->cmdbuf_mutex);
4061}
4062
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004063int vmw_execbuf_ioctl(struct drm_device *dev, unsigned long data,
4064 struct drm_file *file_priv, size_t size)
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004065{
4066 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004067 struct drm_vmw_execbuf_arg arg;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004068 int ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004069 static const size_t copy_offset[] = {
4070 offsetof(struct drm_vmw_execbuf_arg, context_handle),
4071 sizeof(struct drm_vmw_execbuf_arg)};
Sinclair Yeh585851162017-07-05 01:45:40 -07004072 struct dma_fence *in_fence = NULL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004073
4074 if (unlikely(size < copy_offset[0])) {
4075 DRM_ERROR("Invalid command size, ioctl %d\n",
4076 DRM_VMW_EXECBUF);
4077 return -EINVAL;
4078 }
4079
4080 if (copy_from_user(&arg, (void __user *) data, copy_offset[0]) != 0)
4081 return -EFAULT;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004082
4083 /*
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004084 * Extend the ioctl argument while
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004085 * maintaining backwards compatibility:
4086 * We take different code paths depending on the value of
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004087 * arg.version.
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004088 */
4089
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004090 if (unlikely(arg.version > DRM_VMW_EXECBUF_VERSION ||
4091 arg.version == 0)) {
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004092 DRM_ERROR("Incorrect execbuf version.\n");
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004093 return -EINVAL;
4094 }
4095
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004096 if (arg.version > 1 &&
4097 copy_from_user(&arg.context_handle,
4098 (void __user *) (data + copy_offset[0]),
4099 copy_offset[arg.version - 1] -
4100 copy_offset[0]) != 0)
4101 return -EFAULT;
4102
4103 switch (arg.version) {
4104 case 1:
4105 arg.context_handle = (uint32_t) -1;
4106 break;
4107 case 2:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004108 default:
4109 break;
4110 }
4111
Sinclair Yeh585851162017-07-05 01:45:40 -07004112
4113 /* If imported a fence FD from elsewhere, then wait on it */
4114 if (arg.flags & DRM_VMW_EXECBUF_FLAG_IMPORT_FENCE_FD) {
4115 in_fence = sync_file_get_fence(arg.imported_fence_fd);
4116
4117 if (!in_fence) {
4118 DRM_ERROR("Cannot get imported fence\n");
4119 return -EINVAL;
4120 }
4121
4122 ret = vmw_wait_dma_fence(dev_priv->fman, in_fence);
4123 if (ret)
4124 goto out;
4125 }
4126
Thomas Hellstrom294adf72014-02-27 12:34:51 +01004127 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004128 if (unlikely(ret != 0))
4129 return ret;
4130
4131 ret = vmw_execbuf_process(file_priv, dev_priv,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004132 (void __user *)(unsigned long)arg.commands,
4133 NULL, arg.command_size, arg.throttle_us,
4134 arg.context_handle,
4135 (void __user *)(unsigned long)arg.fence_rep,
Sinclair Yehc906965d2017-07-05 01:49:32 -07004136 NULL,
4137 arg.flags);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07004138 ttm_read_unlock(&dev_priv->reservation_sem);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004139 if (unlikely(ret != 0))
Sinclair Yeh585851162017-07-05 01:45:40 -07004140 goto out;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004141
4142 vmw_kms_cursor_post_execbuf(dev_priv);
4143
Sinclair Yeh585851162017-07-05 01:45:40 -07004144out:
4145 if (in_fence)
4146 dma_fence_put(in_fence);
4147 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004148}