blob: 4f5445c531118e4e8e5f879c6cc76f3bcf14755e [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
51/*
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020052 * struct vmw_relocation - Buffer object relocation
53 *
54 * @head: List head for the command submission context's relocation list
Thomas Hellstromcc1e3b72018-09-26 15:38:13 +020055 * @vbo: Non ref-counted pointer to buffer object
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020056 * @mob_loc: Pointer to location for mob id to be modified
57 * @location: Pointer to location for guest pointer to be modified
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020058 */
59struct vmw_relocation {
60 struct list_head head;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020061 struct vmw_buffer_object *vbo;
Thomas Hellstromcc1e3b72018-09-26 15:38:13 +020062 union {
63 SVGAMobId *mob_loc;
64 SVGAGuestPtr *location;
65 };
Thomas Hellstromfc18afc2018-09-26 15:36:52 +020066};
67
Thomas Hellstromc0951b72012-11-20 12:19:35 +000068/**
Thomas Hellstroma1944032016-10-10 11:06:45 -070069 * enum vmw_resource_relocation_type - Relocation type for resources
70 *
71 * @vmw_res_rel_normal: Traditional relocation. The resource id in the
72 * command stream is replaced with the actual id after validation.
73 * @vmw_res_rel_nop: NOP relocation. The command is unconditionally replaced
74 * with a NOP.
75 * @vmw_res_rel_cond_nop: Conditional NOP relocation. If the resource id
76 * after validation is -1, the command is replaced with a NOP. Otherwise no
77 * action.
78 */
79enum vmw_resource_relocation_type {
80 vmw_res_rel_normal,
81 vmw_res_rel_nop,
82 vmw_res_rel_cond_nop,
83 vmw_res_rel_max
84};
85
86/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +000087 * struct vmw_resource_relocation - Relocation info for resources
88 *
89 * @head: List head for the software context's relocation list.
90 * @res: Non-ref-counted pointer to the resource.
Thomas Hellstrome7a45282016-10-10 10:44:00 -070091 * @offset: Offset of single byte entries into the command buffer where the
Thomas Hellstromc0951b72012-11-20 12:19:35 +000092 * id that needs fixup is located.
Thomas Hellstroma1944032016-10-10 11:06:45 -070093 * @rel_type: Type of relocation.
Thomas Hellstromc0951b72012-11-20 12:19:35 +000094 */
95struct vmw_resource_relocation {
96 struct list_head head;
97 const struct vmw_resource *res;
Thomas Hellstroma1944032016-10-10 11:06:45 -070098 u32 offset:29;
99 enum vmw_resource_relocation_type rel_type:3;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000100};
101
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200102/*
103 * struct vmw_ctx_validation_info - Extra validation metadata for contexts
104 * @head: List head of context list
105 * @ctx: The context resource
106 * @cur: The context's persistent binding state
107 * @staged: The binding state changes of this command buffer
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000108 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200109struct vmw_ctx_validation_info {
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000110 struct list_head head;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200111 struct vmw_resource *ctx;
112 struct vmw_ctx_binding_state *cur;
113 struct vmw_ctx_binding_state *staged;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000114};
115
116/**
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100117 * struct vmw_cmd_entry - Describe a command for the verifier
118 *
119 * @user_allow: Whether allowed from the execbuf ioctl.
120 * @gb_disable: Whether disabled if guest-backed objects are available.
121 * @gb_enable: Whether enabled iff guest-backed objects are available.
122 */
123struct vmw_cmd_entry {
124 int (*func) (struct vmw_private *, struct vmw_sw_context *,
125 SVGA3dCmdHeader *);
126 bool user_allow;
127 bool gb_disable;
128 bool gb_enable;
Thomas Hellstrom65b97a22017-08-24 08:06:29 +0200129 const char *cmd_name;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100130};
131
132#define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable) \
133 [(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
Thomas Hellstrom65b97a22017-08-24 08:06:29 +0200134 (_gb_disable), (_gb_enable), #_cmd}
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100135
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700136static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
137 struct vmw_sw_context *sw_context,
138 struct vmw_resource *ctx);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700139static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
140 struct vmw_sw_context *sw_context,
141 SVGAMobId *id,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200142 struct vmw_buffer_object **vmw_bo_p);
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700143/**
144 * vmw_ptr_diff - Compute the offset from a to b in bytes
145 *
146 * @a: A starting pointer.
147 * @b: A pointer offset in the same address space.
148 *
149 * Returns: The offset in bytes between the two pointers.
150 */
151static size_t vmw_ptr_diff(void *a, void *b)
152{
153 return (unsigned long) b - (unsigned long) a;
154}
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700155
Thomas Hellstromc373d4e2012-11-21 12:22:35 +0100156/**
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200157 * vmw_execbuf_bindings_commit - Commit modified binding state
158 * @sw_context: The command submission context
159 * @backoff: Whether this is part of the error path and binding state
160 * changes should be ignored
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000161 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200162static void vmw_execbuf_bindings_commit(struct vmw_sw_context *sw_context,
163 bool backoff)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000164{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200165 struct vmw_ctx_validation_info *entry;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700166
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200167 list_for_each_entry(entry, &sw_context->ctx_list, head) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200168 if (!backoff)
169 vmw_binding_state_commit(entry->cur, entry->staged);
170 if (entry->staged != sw_context->staged_bindings)
171 vmw_binding_state_free(entry->staged);
172 else
173 sw_context->staged_bindings_inuse = false;
174 }
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200175
176 /* List entries are freed with the validation context */
177 INIT_LIST_HEAD(&sw_context->ctx_list);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200178}
179
180/**
181 * vmw_bind_dx_query_mob - Bind the DX query MOB if referenced
182 * @sw_context: The command submission context
183 */
184static void vmw_bind_dx_query_mob(struct vmw_sw_context *sw_context)
185{
186 if (sw_context->dx_query_mob)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700187 vmw_context_bind_dx_query(sw_context->dx_query_ctx,
188 sw_context->dx_query_mob);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000189}
190
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700191/**
192 * vmw_cmd_ctx_first_setup - Perform the setup needed when a context is
193 * added to the validate list.
194 *
195 * @dev_priv: Pointer to the device private:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200196 * @sw_context: The command submission context
197 * @node: The validation node holding the context resource metadata
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700198 */
199static int vmw_cmd_ctx_first_setup(struct vmw_private *dev_priv,
200 struct vmw_sw_context *sw_context,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200201 struct vmw_resource *res,
202 struct vmw_ctx_validation_info *node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700203{
204 int ret;
205
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200206 ret = vmw_resource_context_res_add(dev_priv, sw_context, res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700207 if (unlikely(ret != 0))
208 goto out_err;
209
210 if (!sw_context->staged_bindings) {
211 sw_context->staged_bindings =
212 vmw_binding_state_alloc(dev_priv);
213 if (IS_ERR(sw_context->staged_bindings)) {
214 DRM_ERROR("Failed to allocate context binding "
215 "information.\n");
216 ret = PTR_ERR(sw_context->staged_bindings);
217 sw_context->staged_bindings = NULL;
218 goto out_err;
219 }
220 }
221
222 if (sw_context->staged_bindings_inuse) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200223 node->staged = vmw_binding_state_alloc(dev_priv);
224 if (IS_ERR(node->staged)) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700225 DRM_ERROR("Failed to allocate context binding "
226 "information.\n");
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200227 ret = PTR_ERR(node->staged);
228 node->staged = NULL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700229 goto out_err;
230 }
231 } else {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200232 node->staged = sw_context->staged_bindings;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700233 sw_context->staged_bindings_inuse = true;
234 }
235
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200236 node->ctx = res;
237 node->cur = vmw_context_binding_state(res);
238 list_add_tail(&node->head, &sw_context->ctx_list);
239
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700240 return 0;
241out_err:
242 return ret;
243}
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000244
245/**
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200246 * vmw_execbuf_res_size - calculate extra size fore the resource validation
247 * node
248 * @dev_priv: Pointer to the device private struct.
249 * @res_type: The resource type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000250 *
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200251 * Guest-backed contexts and DX contexts require extra size to store
252 * execbuf private information in the validation node. Typically the
253 * binding manager associated data structures.
254 *
255 * Returns: The extra size requirement based on resource type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000256 */
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200257static unsigned int vmw_execbuf_res_size(struct vmw_private *dev_priv,
258 enum vmw_res_type res_type)
259{
260 return (res_type == vmw_res_dx_context ||
261 (res_type == vmw_res_context && dev_priv->has_mob)) ?
262 sizeof(struct vmw_ctx_validation_info) : 0;
263}
264
265/**
266 * vmw_execbuf_rcache_update - Update a resource-node cache entry
267 *
268 * @rcache: Pointer to the entry to update.
269 * @res: Pointer to the resource.
270 * @private: Pointer to the execbuf-private space in the resource
271 * validation node.
272 */
273static void vmw_execbuf_rcache_update(struct vmw_res_cache_entry *rcache,
274 struct vmw_resource *res,
275 void *private)
276{
277 rcache->res = res;
278 rcache->private = private;
279 rcache->valid = 1;
280 rcache->valid_handle = 0;
281}
282
283/**
284 * vmw_execbuf_res_noref_val_add - Add a resource described by an
285 * unreferenced rcu-protected pointer to the validation list.
286 * @sw_context: Pointer to the software context.
287 * @res: Unreferenced rcu-protected pointer to the resource.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100288 * @dirty: Whether to change dirty status.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200289 *
290 * Returns: 0 on success. Negative error code on failure. Typical error
291 * codes are %-EINVAL on inconsistency and %-ESRCH if the resource was
292 * doomed.
293 */
294static int vmw_execbuf_res_noref_val_add(struct vmw_sw_context *sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100295 struct vmw_resource *res,
296 u32 dirty)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000297{
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700298 struct vmw_private *dev_priv = res->dev_priv;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000299 int ret;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200300 enum vmw_res_type res_type = vmw_res_type(res);
301 struct vmw_res_cache_entry *rcache;
302 struct vmw_ctx_validation_info *ctx_info;
303 bool first_usage;
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200304 unsigned int priv_size;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000305
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200306 rcache = &sw_context->res_cache[res_type];
307 if (likely(rcache->valid && rcache->res == res)) {
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100308 if (dirty)
309 vmw_validation_res_set_dirty(sw_context->ctx,
310 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200311 vmw_user_resource_noref_release();
312 return 0;
313 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000314
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200315 priv_size = vmw_execbuf_res_size(dev_priv, res_type);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200316 ret = vmw_validation_add_resource(sw_context->ctx, res, priv_size,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100317 dirty, (void **)&ctx_info,
318 &first_usage);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200319 vmw_user_resource_noref_release();
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200320 if (ret)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000321 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000322
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200323 if (priv_size && first_usage) {
324 ret = vmw_cmd_ctx_first_setup(dev_priv, sw_context, res,
325 ctx_info);
326 if (ret)
327 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700328 }
329
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200330 vmw_execbuf_rcache_update(rcache, res, ctx_info);
331 return 0;
332}
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700333
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200334/**
335 * vmw_execbuf_res_noctx_val_add - Add a non-context resource to the resource
336 * validation list if it's not already on it
337 * @sw_context: Pointer to the software context.
338 * @res: Pointer to the resource.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100339 * @dirty: Whether to change dirty status.
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200340 *
341 * Returns: Zero on success. Negative error code on failure.
342 */
343static int vmw_execbuf_res_noctx_val_add(struct vmw_sw_context *sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100344 struct vmw_resource *res,
345 u32 dirty)
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200346{
347 struct vmw_res_cache_entry *rcache;
348 enum vmw_res_type res_type = vmw_res_type(res);
349 void *ptr;
350 int ret;
351
352 rcache = &sw_context->res_cache[res_type];
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100353 if (likely(rcache->valid && rcache->res == res)) {
354 if (dirty)
355 vmw_validation_res_set_dirty(sw_context->ctx,
356 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200357 return 0;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100358 }
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200359
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100360 ret = vmw_validation_add_resource(sw_context->ctx, res, 0, dirty,
361 &ptr, NULL);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200362 if (ret)
363 return ret;
364
365 vmw_execbuf_rcache_update(rcache, res, ptr);
366
367 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700368}
369
370/**
371 * vmw_view_res_val_add - Add a view and the surface it's pointing to
372 * to the validation list
373 *
374 * @sw_context: The software context holding the validation list.
375 * @view: Pointer to the view resource.
376 *
377 * Returns 0 if success, negative error code otherwise.
378 */
379static int vmw_view_res_val_add(struct vmw_sw_context *sw_context,
380 struct vmw_resource *view)
381{
382 int ret;
383
384 /*
385 * First add the resource the view is pointing to, otherwise
386 * it may be swapped out when the view is validated.
387 */
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100388 ret = vmw_execbuf_res_noctx_val_add(sw_context, vmw_view_srf(view),
389 vmw_view_dirtying(view));
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700390 if (ret)
391 return ret;
392
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100393 return vmw_execbuf_res_noctx_val_add(sw_context, view,
394 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700395}
396
397/**
398 * vmw_view_id_val_add - Look up a view and add it and the surface it's
399 * pointing to to the validation list.
400 *
401 * @sw_context: The software context holding the validation list.
402 * @view_type: The view type to look up.
403 * @id: view id of the view.
404 *
405 * The view is represented by a view id and the DX context it's created on,
406 * or scheduled for creation on. If there is no DX context set, the function
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200407 * will return an -EINVAL error pointer.
408 *
409 * Returns: Unreferenced pointer to the resource on success, negative error
410 * pointer on failure.
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700411 */
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200412static struct vmw_resource *
413vmw_view_id_val_add(struct vmw_sw_context *sw_context,
414 enum vmw_view_type view_type, u32 id)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700415{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200416 struct vmw_ctx_validation_info *ctx_node = sw_context->dx_ctx_node;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700417 struct vmw_resource *view;
418 int ret;
419
420 if (!ctx_node) {
421 DRM_ERROR("DX Context not set.\n");
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200422 return ERR_PTR(-EINVAL);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700423 }
424
425 view = vmw_view_lookup(sw_context->man, view_type, id);
426 if (IS_ERR(view))
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200427 return view;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700428
429 ret = vmw_view_res_val_add(sw_context, view);
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200430 if (ret)
431 return ERR_PTR(ret);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700432
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200433 return view;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000434}
435
436/**
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100437 * vmw_resource_context_res_add - Put resources previously bound to a context on
438 * the validation list
439 *
440 * @dev_priv: Pointer to a device private structure
441 * @sw_context: Pointer to a software context used for this command submission
442 * @ctx: Pointer to the context resource
443 *
444 * This function puts all resources that were previously bound to @ctx on
445 * the resource validation list. This is part of the context state reemission
446 */
447static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
448 struct vmw_sw_context *sw_context,
449 struct vmw_resource *ctx)
450{
451 struct list_head *binding_list;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700452 struct vmw_ctx_bindinfo *entry;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100453 int ret = 0;
454 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700455 u32 i;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100456
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700457 /* Add all cotables to the validation list. */
458 if (dev_priv->has_dx && vmw_res_type(ctx) == vmw_res_dx_context) {
459 for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) {
460 res = vmw_context_cotable(ctx, i);
461 if (IS_ERR(res))
462 continue;
463
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100464 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
465 VMW_RES_DIRTY_SET);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700466 if (unlikely(ret != 0))
467 return ret;
468 }
469 }
470
471
472 /* Add all resources bound to the context to the validation list */
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100473 mutex_lock(&dev_priv->binding_mutex);
474 binding_list = vmw_context_binding_list(ctx);
475
476 list_for_each_entry(entry, binding_list, ctx_list) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700477 if (vmw_res_type(entry->res) == vmw_res_view)
478 ret = vmw_view_res_val_add(sw_context, entry->res);
479 else
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100480 ret = vmw_execbuf_res_noctx_val_add
481 (sw_context, entry->res,
482 vmw_binding_dirtying(entry->bt));
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100483 if (unlikely(ret != 0))
484 break;
485 }
486
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700487 if (dev_priv->has_dx && vmw_res_type(ctx) == vmw_res_dx_context) {
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200488 struct vmw_buffer_object *dx_query_mob;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700489
490 dx_query_mob = vmw_context_get_dx_query_mob(ctx);
491 if (dx_query_mob)
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200492 ret = vmw_validation_add_bo(sw_context->ctx,
493 dx_query_mob, true, false);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700494 }
495
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100496 mutex_unlock(&dev_priv->binding_mutex);
497 return ret;
498}
499
500/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000501 * vmw_resource_relocation_add - Add a relocation to the relocation list
502 *
503 * @list: Pointer to head of relocation list.
504 * @res: The resource.
505 * @offset: Offset into the command buffer currently being parsed where the
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700506 * id that needs fixup is located. Granularity is one byte.
Thomas Hellstroma1944032016-10-10 11:06:45 -0700507 * @rel_type: Relocation type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000508 */
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200509static int vmw_resource_relocation_add(struct vmw_sw_context *sw_context,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000510 const struct vmw_resource *res,
Thomas Hellstroma1944032016-10-10 11:06:45 -0700511 unsigned long offset,
512 enum vmw_resource_relocation_type
513 rel_type)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000514{
515 struct vmw_resource_relocation *rel;
516
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200517 rel = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*rel));
Ravikant B Sharma1a4adb02016-11-08 17:30:31 +0530518 if (unlikely(!rel)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000519 DRM_ERROR("Failed to allocate a resource relocation.\n");
520 return -ENOMEM;
521 }
522
523 rel->res = res;
524 rel->offset = offset;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700525 rel->rel_type = rel_type;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200526 list_add_tail(&rel->head, &sw_context->res_relocations);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000527
528 return 0;
529}
530
531/**
532 * vmw_resource_relocations_free - Free all relocations on a list
533 *
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200534 * @list: Pointer to the head of the relocation list
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000535 */
536static void vmw_resource_relocations_free(struct list_head *list)
537{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200538 /* Memory is validation context memory, so no need to free it */
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000539
Thomas Hellstromfc18afc2018-09-26 15:36:52 +0200540 INIT_LIST_HEAD(list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000541}
542
543/**
544 * vmw_resource_relocations_apply - Apply all relocations on a list
545 *
546 * @cb: Pointer to the start of the command buffer bein patch. This need
547 * not be the same buffer as the one being parsed when the relocation
548 * list was built, but the contents must be the same modulo the
549 * resource ids.
550 * @list: Pointer to the head of the relocation list.
551 */
552static void vmw_resource_relocations_apply(uint32_t *cb,
553 struct list_head *list)
554{
555 struct vmw_resource_relocation *rel;
556
Thomas Hellstroma1944032016-10-10 11:06:45 -0700557 /* Validate the struct vmw_resource_relocation member size */
558 BUILD_BUG_ON(SVGA_CB_MAX_SIZE >= (1 << 29));
559 BUILD_BUG_ON(vmw_res_rel_max >= (1 << 3));
560
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100561 list_for_each_entry(rel, list, head) {
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700562 u32 *addr = (u32 *)((unsigned long) cb + rel->offset);
Thomas Hellstroma1944032016-10-10 11:06:45 -0700563 switch (rel->rel_type) {
564 case vmw_res_rel_normal:
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700565 *addr = rel->res->id;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700566 break;
567 case vmw_res_rel_nop:
Thomas Hellstrome7a45282016-10-10 10:44:00 -0700568 *addr = SVGA_3D_CMD_NOP;
Thomas Hellstroma1944032016-10-10 11:06:45 -0700569 break;
570 default:
571 if (rel->res->id == -1)
572 *addr = SVGA_3D_CMD_NOP;
573 break;
574 }
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100575 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000576}
577
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000578static int vmw_cmd_invalid(struct vmw_private *dev_priv,
579 struct vmw_sw_context *sw_context,
580 SVGA3dCmdHeader *header)
581{
Sinclair Yehfcfffdd2017-07-17 23:28:36 -0700582 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000583}
584
585static int vmw_cmd_ok(struct vmw_private *dev_priv,
586 struct vmw_sw_context *sw_context,
587 SVGA3dCmdHeader *header)
588{
589 return 0;
590}
591
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200592/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000593 * vmw_resources_reserve - Reserve all resources on the sw_context's
594 * resource list.
595 *
596 * @sw_context: Pointer to the software context.
597 *
598 * Note that since vmware's command submission currently is protected by
599 * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
600 * since only a single thread at once will attempt this.
601 */
602static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
603{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200604 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000605
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200606 ret = vmw_validation_res_reserve(sw_context->ctx, true);
607 if (ret)
608 return ret;
Charmaine Lee2f633e52015-08-10 10:45:11 -0700609
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700610 if (sw_context->dx_query_mob) {
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200611 struct vmw_buffer_object *expected_dx_query_mob;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700612
613 expected_dx_query_mob =
614 vmw_context_get_dx_query_mob(sw_context->dx_query_ctx);
615 if (expected_dx_query_mob &&
616 expected_dx_query_mob != sw_context->dx_query_mob) {
617 ret = -EINVAL;
618 }
619 }
620
621 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000622}
623
624/**
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100625 * vmw_cmd_res_check - Check that a resource is present and if so, put it
626 * on the resource validate list unless it's already there.
627 *
628 * @dev_priv: Pointer to a device private structure.
629 * @sw_context: Pointer to the software context.
630 * @res_type: Resource type.
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100631 * @dirty: Whether to change dirty status.
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100632 * @converter: User-space visisble type specific information.
633 * @id_loc: Pointer to the location in the command buffer currently being
634 * parsed from where the user-space resource id handle is located.
635 * @p_val: Pointer to pointer to resource validalidation node. Populated
636 * on exit.
637 */
638static int
639vmw_cmd_res_check(struct vmw_private *dev_priv,
640 struct vmw_sw_context *sw_context,
641 enum vmw_res_type res_type,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100642 u32 dirty,
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100643 const struct vmw_user_resource_conv *converter,
644 uint32_t *id_loc,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200645 struct vmw_resource **p_res)
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100646{
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200647 struct vmw_res_cache_entry *rcache = &sw_context->res_cache[res_type];
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200648 struct vmw_resource *res;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200649 int ret;
650
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200651 if (p_res)
652 *p_res = NULL;
653
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200654 if (*id_loc == SVGA3D_INVALID_ID) {
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200655 if (res_type == vmw_res_context) {
656 DRM_ERROR("Illegal context invalid id.\n");
657 return -EINVAL;
658 }
659 return 0;
660 }
661
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200662 if (likely(rcache->valid_handle && *id_loc == rcache->handle)) {
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200663 res = rcache->res;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100664 if (dirty)
665 vmw_validation_res_set_dirty(sw_context->ctx,
666 rcache->private, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200667 } else {
668 unsigned int size = vmw_execbuf_res_size(dev_priv, res_type);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200669
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200670 ret = vmw_validation_preload_res(sw_context->ctx, size);
671 if (ret)
672 return ret;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200673
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200674 res = vmw_user_resource_noref_lookup_handle
675 (dev_priv, sw_context->fp->tfile, *id_loc, converter);
Chengguang Xu4efa6662019-03-01 10:14:06 -0800676 if (IS_ERR(res)) {
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200677 DRM_ERROR("Could not find or use resource 0x%08x.\n",
678 (unsigned int) *id_loc);
679 return PTR_ERR(res);
680 }
681
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100682 ret = vmw_execbuf_res_noref_val_add(sw_context, res, dirty);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200683 if (unlikely(ret != 0))
684 return ret;
685
686 if (rcache->valid && rcache->res == res) {
687 rcache->valid_handle = true;
688 rcache->handle = *id_loc;
689 }
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200690 }
691
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200692 ret = vmw_resource_relocation_add(sw_context, res,
693 vmw_ptr_diff(sw_context->buf_start,
694 id_loc),
695 vmw_res_rel_normal);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200696 if (p_res)
697 *p_res = res;
698
Thomas Hellstrom18e4a462014-06-09 12:39:22 +0200699 return 0;
Thomas Hellstromd5bde952014-01-31 10:12:10 +0100700}
701
702/**
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700703 * vmw_rebind_dx_query - Rebind DX query associated with the context
704 *
705 * @ctx_res: context the query belongs to
706 *
707 * This function assumes binding_mutex is held.
708 */
709static int vmw_rebind_all_dx_query(struct vmw_resource *ctx_res)
710{
711 struct vmw_private *dev_priv = ctx_res->dev_priv;
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200712 struct vmw_buffer_object *dx_query_mob;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700713 struct {
714 SVGA3dCmdHeader header;
715 SVGA3dCmdDXBindAllQuery body;
716 } *cmd;
717
718
719 dx_query_mob = vmw_context_get_dx_query_mob(ctx_res);
720
721 if (!dx_query_mob || dx_query_mob->dx_query_ctx)
722 return 0;
723
724 cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), ctx_res->id);
725
726 if (cmd == NULL) {
727 DRM_ERROR("Failed to rebind queries.\n");
728 return -ENOMEM;
729 }
730
731 cmd->header.id = SVGA_3D_CMD_DX_BIND_ALL_QUERY;
732 cmd->header.size = sizeof(cmd->body);
733 cmd->body.cid = ctx_res->id;
734 cmd->body.mobid = dx_query_mob->base.mem.start;
735 vmw_fifo_commit(dev_priv, sizeof(*cmd));
736
737 vmw_context_bind_dx_query(ctx_res, dx_query_mob);
738
739 return 0;
740}
741
742/**
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100743 * vmw_rebind_contexts - Rebind all resources previously bound to
744 * referenced contexts.
745 *
746 * @sw_context: Pointer to the software context.
747 *
748 * Rebind context binding points that have been scrubbed because of eviction.
749 */
750static int vmw_rebind_contexts(struct vmw_sw_context *sw_context)
751{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200752 struct vmw_ctx_validation_info *val;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100753 int ret;
754
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200755 list_for_each_entry(val, &sw_context->ctx_list, head) {
756 ret = vmw_binding_rebind_all(val->cur);
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100757 if (unlikely(ret != 0)) {
758 if (ret != -ERESTARTSYS)
759 DRM_ERROR("Failed to rebind context.\n");
760 return ret;
761 }
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700762
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200763 ret = vmw_rebind_all_dx_query(val->ctx);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -0700764 if (ret != 0)
765 return ret;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +0100766 }
767
768 return 0;
769}
770
771/**
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700772 * vmw_view_bindings_add - Add an array of view bindings to a context
773 * binding state tracker.
774 *
775 * @sw_context: The execbuf state used for this command.
776 * @view_type: View type for the bindings.
777 * @binding_type: Binding type for the bindings.
778 * @shader_slot: The shader slot to user for the bindings.
779 * @view_ids: Array of view ids to be bound.
780 * @num_views: Number of view ids in @view_ids.
781 * @first_slot: The binding slot to be used for the first view id in @view_ids.
782 */
783static int vmw_view_bindings_add(struct vmw_sw_context *sw_context,
784 enum vmw_view_type view_type,
785 enum vmw_ctx_binding_type binding_type,
786 uint32 shader_slot,
787 uint32 view_ids[], u32 num_views,
788 u32 first_slot)
789{
Deepak Rawat6f74fd92019-02-08 12:53:57 -0800790 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700791 u32 i;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700792
Deepak Rawat6f74fd92019-02-08 12:53:57 -0800793 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700794 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700795
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700796 for (i = 0; i < num_views; ++i) {
797 struct vmw_ctx_bindinfo_view binding;
798 struct vmw_resource *view = NULL;
799
800 if (view_ids[i] != SVGA3D_INVALID_ID) {
Thomas Hellstrom508108e2018-09-26 16:28:45 +0200801 view = vmw_view_id_val_add(sw_context, view_type,
802 view_ids[i]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700803 if (IS_ERR(view)) {
804 DRM_ERROR("View not found.\n");
805 return PTR_ERR(view);
806 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700807 }
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200808 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700809 binding.bi.res = view;
810 binding.bi.bt = binding_type;
811 binding.shader_slot = shader_slot;
812 binding.slot = first_slot + i;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200813 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700814 shader_slot, binding.slot);
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700815 }
816
817 return 0;
818}
819
820/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000821 * vmw_cmd_cid_check - Check a command header for valid context information.
822 *
823 * @dev_priv: Pointer to a device private structure.
824 * @sw_context: Pointer to the software context.
825 * @header: A command header with an embedded user-space context handle.
826 *
827 * Convenience function: Call vmw_cmd_res_check with the user-space context
828 * handle embedded in @header.
829 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000830static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
831 struct vmw_sw_context *sw_context,
832 SVGA3dCmdHeader *header)
833{
834 struct vmw_cid_cmd {
835 SVGA3dCmdHeader header;
Thomas Hellstrom8e67bbb2014-02-06 12:35:05 +0100836 uint32_t cid;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000837 } *cmd;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000838
839 cmd = container_of(header, struct vmw_cid_cmd, header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000840 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100841 VMW_RES_DIRTY_SET, user_context_converter,
842 &cmd->cid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000843}
844
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200845/**
846 * vmw_execbuf_info_from_res - Get the private validation metadata for a
847 * recently validated resource
848 * @sw_context: Pointer to the command submission context
849 * @res: The resource
850 *
851 * The resource pointed to by @res needs to be present in the command submission
852 * context's resource cache and hence the last resource of that type to be
853 * processed by the validation code.
854 *
855 * Return: a pointer to the private metadata of the resource, or NULL
856 * if it wasn't found
857 */
858static struct vmw_ctx_validation_info *
859vmw_execbuf_info_from_res(struct vmw_sw_context *sw_context,
860 struct vmw_resource *res)
861{
862 struct vmw_res_cache_entry *rcache =
863 &sw_context->res_cache[vmw_res_type(res)];
864
865 if (rcache->valid && rcache->res == res)
866 return rcache->private;
867
868 WARN_ON_ONCE(true);
869 return NULL;
870}
871
872
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000873static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
874 struct vmw_sw_context *sw_context,
875 SVGA3dCmdHeader *header)
876{
877 struct vmw_sid_cmd {
878 SVGA3dCmdHeader header;
879 SVGA3dCmdSetRenderTarget body;
880 } *cmd;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200881 struct vmw_resource *ctx;
882 struct vmw_resource *res;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000883 int ret;
884
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700885 cmd = container_of(header, struct vmw_sid_cmd, header);
886
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700887 if (cmd->body.type >= SVGA3D_RT_MAX) {
888 DRM_ERROR("Illegal render target type %u.\n",
889 (unsigned) cmd->body.type);
890 return -EINVAL;
891 }
892
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700893 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100894 VMW_RES_DIRTY_SET, user_context_converter,
895 &cmd->body.cid, &ctx);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000896 if (unlikely(ret != 0))
897 return ret;
898
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000899 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100900 VMW_RES_DIRTY_SET, user_surface_converter,
901 &cmd->body.target.sid, &res);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +0200902 if (unlikely(ret))
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700903 return ret;
904
905 if (dev_priv->has_mob) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700906 struct vmw_ctx_bindinfo_view binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200907 struct vmw_ctx_validation_info *node;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700908
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200909 node = vmw_execbuf_info_from_res(sw_context, ctx);
910 if (!node)
911 return -EINVAL;
912
913 binding.bi.ctx = ctx;
914 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700915 binding.bi.bt = vmw_ctx_binding_rt;
916 binding.slot = cmd->body.type;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +0200917 vmw_binding_add(node->staged, &binding.bi, 0, binding.slot);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -0700918 }
919
920 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000921}
922
923static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
924 struct vmw_sw_context *sw_context,
925 SVGA3dCmdHeader *header)
926{
927 struct vmw_sid_cmd {
928 SVGA3dCmdHeader header;
929 SVGA3dCmdSurfaceCopy body;
930 } *cmd;
931 int ret;
932
933 cmd = container_of(header, struct vmw_sid_cmd, header);
Thomas Hellstromc9146cd2015-03-02 23:45:04 -0800934
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700935 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100936 VMW_RES_DIRTY_NONE, user_surface_converter,
937 &cmd->body.src.sid, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700938 if (ret)
939 return ret;
Thomas Hellstromc9146cd2015-03-02 23:45:04 -0800940
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000941 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100942 VMW_RES_DIRTY_SET, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000943 &cmd->body.dest.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000944}
945
Neha Bhende0fca749e2015-08-10 10:51:07 -0700946static int vmw_cmd_buffer_copy_check(struct vmw_private *dev_priv,
947 struct vmw_sw_context *sw_context,
948 SVGA3dCmdHeader *header)
949{
950 struct {
951 SVGA3dCmdHeader header;
952 SVGA3dCmdDXBufferCopy body;
953 } *cmd;
954 int ret;
955
956 cmd = container_of(header, typeof(*cmd), header);
957 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100958 VMW_RES_DIRTY_NONE, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700959 &cmd->body.src, NULL);
960 if (ret != 0)
961 return ret;
962
963 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100964 VMW_RES_DIRTY_SET, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700965 &cmd->body.dest, NULL);
966}
967
968static int vmw_cmd_pred_copy_check(struct vmw_private *dev_priv,
969 struct vmw_sw_context *sw_context,
970 SVGA3dCmdHeader *header)
971{
972 struct {
973 SVGA3dCmdHeader header;
974 SVGA3dCmdDXPredCopyRegion body;
975 } *cmd;
976 int ret;
977
978 cmd = container_of(header, typeof(*cmd), header);
979 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100980 VMW_RES_DIRTY_NONE, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700981 &cmd->body.srcSid, NULL);
982 if (ret != 0)
983 return ret;
984
985 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +0100986 VMW_RES_DIRTY_SET, user_surface_converter,
Neha Bhende0fca749e2015-08-10 10:51:07 -0700987 &cmd->body.dstSid, NULL);
988}
989
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000990static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
991 struct vmw_sw_context *sw_context,
992 SVGA3dCmdHeader *header)
993{
994 struct vmw_sid_cmd {
995 SVGA3dCmdHeader header;
996 SVGA3dCmdSurfaceStretchBlt body;
997 } *cmd;
998 int ret;
999
1000 cmd = container_of(header, struct vmw_sid_cmd, header);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001001 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001002 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001003 &cmd->body.src.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001004 if (unlikely(ret != 0))
1005 return ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001006 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001007 VMW_RES_DIRTY_SET, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001008 &cmd->body.dest.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001009}
1010
1011static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
1012 struct vmw_sw_context *sw_context,
1013 SVGA3dCmdHeader *header)
1014{
1015 struct vmw_sid_cmd {
1016 SVGA3dCmdHeader header;
1017 SVGA3dCmdBlitSurfaceToScreen body;
1018 } *cmd;
1019
1020 cmd = container_of(header, struct vmw_sid_cmd, header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02001021
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001022 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001023 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001024 &cmd->body.srcImage.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001025}
1026
1027static int vmw_cmd_present_check(struct vmw_private *dev_priv,
1028 struct vmw_sw_context *sw_context,
1029 SVGA3dCmdHeader *header)
1030{
1031 struct vmw_sid_cmd {
1032 SVGA3dCmdHeader header;
1033 SVGA3dCmdPresent body;
1034 } *cmd;
1035
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +02001036
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001037 cmd = container_of(header, struct vmw_sid_cmd, header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02001038
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001039 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001040 VMW_RES_DIRTY_NONE, user_surface_converter,
1041 &cmd->body.sid, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001042}
1043
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001044/**
1045 * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
1046 *
1047 * @dev_priv: The device private structure.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001048 * @new_query_bo: The new buffer holding query results.
1049 * @sw_context: The software context used for this command submission.
1050 *
1051 * This function checks whether @new_query_bo is suitable for holding
1052 * query results, and if another buffer currently is pinned for query
1053 * results. If so, the function prepares the state of @sw_context for
1054 * switching pinned buffers after successful submission of the current
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001055 * command batch.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001056 */
1057static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001058 struct vmw_buffer_object *new_query_bo,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001059 struct vmw_sw_context *sw_context)
1060{
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001061 struct vmw_res_cache_entry *ctx_entry =
1062 &sw_context->res_cache[vmw_res_context];
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001063 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001064
1065 BUG_ON(!ctx_entry->valid);
1066 sw_context->last_query_ctx = ctx_entry->res;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001067
1068 if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
1069
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001070 if (unlikely(new_query_bo->base.num_pages > 4)) {
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001071 DRM_ERROR("Query buffer too large.\n");
1072 return -EINVAL;
1073 }
1074
1075 if (unlikely(sw_context->cur_query_bo != NULL)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001076 sw_context->needs_post_query_barrier = true;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001077 ret = vmw_validation_add_bo(sw_context->ctx,
1078 sw_context->cur_query_bo,
1079 dev_priv->has_mob, false);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001080 if (unlikely(ret != 0))
1081 return ret;
1082 }
1083 sw_context->cur_query_bo = new_query_bo;
1084
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001085 ret = vmw_validation_add_bo(sw_context->ctx,
1086 dev_priv->dummy_query_bo,
1087 dev_priv->has_mob, false);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001088 if (unlikely(ret != 0))
1089 return ret;
1090
1091 }
1092
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001093 return 0;
1094}
1095
1096
1097/**
1098 * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
1099 *
1100 * @dev_priv: The device private structure.
1101 * @sw_context: The software context used for this command submission batch.
1102 *
1103 * This function will check if we're switching query buffers, and will then,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001104 * issue a dummy occlusion query wait used as a query barrier. When the fence
1105 * object following that query wait has signaled, we are sure that all
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001106 * preceding queries have finished, and the old query buffer can be unpinned.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001107 * However, since both the new query buffer and the old one are fenced with
1108 * that fence, we can do an asynchronus unpin now, and be sure that the
1109 * old query buffer won't be moved until the fence has signaled.
1110 *
1111 * As mentioned above, both the new - and old query buffers need to be fenced
1112 * using a sequence emitted *after* calling this function.
1113 */
1114static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
1115 struct vmw_sw_context *sw_context)
1116{
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001117 /*
1118 * The validate list should still hold references to all
1119 * contexts here.
1120 */
1121
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001122 if (sw_context->needs_post_query_barrier) {
1123 struct vmw_res_cache_entry *ctx_entry =
1124 &sw_context->res_cache[vmw_res_context];
1125 struct vmw_resource *ctx;
1126 int ret;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001127
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001128 BUG_ON(!ctx_entry->valid);
1129 ctx = ctx_entry->res;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001130
1131 ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
1132
1133 if (unlikely(ret != 0))
1134 DRM_ERROR("Out of fifo space for dummy query.\n");
1135 }
1136
1137 if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
1138 if (dev_priv->pinned_bo) {
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001139 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001140 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001141 }
1142
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001143 if (!sw_context->needs_post_query_barrier) {
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001144 vmw_bo_pin_reserved(sw_context->cur_query_bo, true);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001145
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001146 /*
1147 * We pin also the dummy_query_bo buffer so that we
1148 * don't need to validate it when emitting
1149 * dummy queries in context destroy paths.
1150 */
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001151
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001152 if (!dev_priv->dummy_query_bo_pinned) {
1153 vmw_bo_pin_reserved(dev_priv->dummy_query_bo,
1154 true);
1155 dev_priv->dummy_query_bo_pinned = true;
1156 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001157
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001158 BUG_ON(sw_context->last_query_ctx == NULL);
1159 dev_priv->query_cid = sw_context->last_query_ctx->id;
1160 dev_priv->query_cid_valid = true;
1161 dev_priv->pinned_bo =
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001162 vmw_bo_reference(sw_context->cur_query_bo);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001163 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001164 }
1165}
1166
1167/**
Thomas Hellstromddcda242012-11-21 11:26:55 +01001168 * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
1169 * handle to a MOB id.
1170 *
1171 * @dev_priv: Pointer to a device private structure.
1172 * @sw_context: The software context used for this command batch validation.
1173 * @id: Pointer to the user-space handle to be translated.
1174 * @vmw_bo_p: Points to a location that, on successful return will carry
Thomas Hellstromb139d432018-09-26 16:27:54 +02001175 * a non-reference-counted pointer to the buffer object identified by the
Thomas Hellstromddcda242012-11-21 11:26:55 +01001176 * user-space handle in @id.
1177 *
1178 * This function saves information needed to translate a user-space buffer
1179 * handle to a MOB id. The translation does not take place immediately, but
1180 * during a call to vmw_apply_relocations(). This function builds a relocation
1181 * list and a list of buffers to validate. The former needs to be freed using
1182 * either vmw_apply_relocations() or vmw_free_relocations(). The latter
1183 * needs to be freed using vmw_clear_validations.
1184 */
1185static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
1186 struct vmw_sw_context *sw_context,
1187 SVGAMobId *id,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001188 struct vmw_buffer_object **vmw_bo_p)
Thomas Hellstromddcda242012-11-21 11:26:55 +01001189{
Thomas Hellstromb139d432018-09-26 16:27:54 +02001190 struct vmw_buffer_object *vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001191 uint32_t handle = *id;
1192 struct vmw_relocation *reloc;
1193 int ret;
1194
Thomas Hellstromb139d432018-09-26 16:27:54 +02001195 vmw_validation_preload_bo(sw_context->ctx);
1196 vmw_bo = vmw_user_bo_noref_lookup(sw_context->fp->tfile, handle);
1197 if (IS_ERR(vmw_bo)) {
Thomas Hellstromddcda242012-11-21 11:26:55 +01001198 DRM_ERROR("Could not find or use MOB buffer.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001199 return PTR_ERR(vmw_bo);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001200 }
Thomas Hellstromddcda242012-11-21 11:26:55 +01001201
Thomas Hellstromb139d432018-09-26 16:27:54 +02001202 ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo, true, false);
1203 vmw_user_bo_noref_release();
1204 if (unlikely(ret != 0))
1205 return ret;
1206
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001207 reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1208 if (!reloc)
Thomas Hellstromb139d432018-09-26 16:27:54 +02001209 return -ENOMEM;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001210
Thomas Hellstromddcda242012-11-21 11:26:55 +01001211 reloc->mob_loc = id;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001212 reloc->vbo = vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001213
Thomas Hellstromddcda242012-11-21 11:26:55 +01001214 *vmw_bo_p = vmw_bo;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001215 list_add_tail(&reloc->head, &sw_context->bo_relocations);
1216
Thomas Hellstromddcda242012-11-21 11:26:55 +01001217 return 0;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001218}
1219
1220/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001221 * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
1222 * handle to a valid SVGAGuestPtr
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001223 *
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001224 * @dev_priv: Pointer to a device private structure.
1225 * @sw_context: The software context used for this command batch validation.
1226 * @ptr: Pointer to the user-space handle to be translated.
1227 * @vmw_bo_p: Points to a location that, on successful return will carry
Thomas Hellstromb139d432018-09-26 16:27:54 +02001228 * a non-reference-counted pointer to the DMA buffer identified by the
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001229 * user-space handle in @id.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001230 *
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001231 * This function saves information needed to translate a user-space buffer
1232 * handle to a valid SVGAGuestPtr. The translation does not take place
1233 * immediately, but during a call to vmw_apply_relocations().
1234 * This function builds a relocation list and a list of buffers to validate.
1235 * The former needs to be freed using either vmw_apply_relocations() or
1236 * vmw_free_relocations(). The latter needs to be freed using
1237 * vmw_clear_validations.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001238 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001239static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
1240 struct vmw_sw_context *sw_context,
1241 SVGAGuestPtr *ptr,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001242 struct vmw_buffer_object **vmw_bo_p)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001243{
Thomas Hellstromb139d432018-09-26 16:27:54 +02001244 struct vmw_buffer_object *vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001245 uint32_t handle = ptr->gmrId;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001246 struct vmw_relocation *reloc;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001247 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001248
Thomas Hellstromb139d432018-09-26 16:27:54 +02001249 vmw_validation_preload_bo(sw_context->ctx);
1250 vmw_bo = vmw_user_bo_noref_lookup(sw_context->fp->tfile, handle);
1251 if (IS_ERR(vmw_bo)) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001252 DRM_ERROR("Could not find or use GMR region.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001253 return PTR_ERR(vmw_bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001254 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001255
Thomas Hellstromb139d432018-09-26 16:27:54 +02001256 ret = vmw_validation_add_bo(sw_context->ctx, vmw_bo, false, false);
1257 vmw_user_bo_noref_release();
1258 if (unlikely(ret != 0))
1259 return ret;
1260
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001261 reloc = vmw_validation_mem_alloc(sw_context->ctx, sizeof(*reloc));
1262 if (!reloc)
Thomas Hellstromb139d432018-09-26 16:27:54 +02001263 return -ENOMEM;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001264
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001265 reloc->location = ptr;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001266 reloc->vbo = vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001267 *vmw_bo_p = vmw_bo;
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02001268 list_add_tail(&reloc->head, &sw_context->bo_relocations);
1269
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001270 return 0;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001271}
1272
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001273
1274
1275/**
1276 * vmw_cmd_dx_define_query - validate a SVGA_3D_CMD_DX_DEFINE_QUERY command.
1277 *
1278 * @dev_priv: Pointer to a device private struct.
1279 * @sw_context: The software context used for this command submission.
1280 * @header: Pointer to the command header in the command stream.
1281 *
1282 * This function adds the new query into the query COTABLE
1283 */
1284static int vmw_cmd_dx_define_query(struct vmw_private *dev_priv,
1285 struct vmw_sw_context *sw_context,
1286 SVGA3dCmdHeader *header)
1287{
1288 struct vmw_dx_define_query_cmd {
1289 SVGA3dCmdHeader header;
1290 SVGA3dCmdDXDefineQuery q;
1291 } *cmd;
1292
1293 int ret;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08001294 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001295 struct vmw_resource *cotable_res;
1296
Deepak Rawat6f74fd92019-02-08 12:53:57 -08001297 if (!ctx_node)
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001298 return -EINVAL;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001299
1300 cmd = container_of(header, struct vmw_dx_define_query_cmd, header);
1301
1302 if (cmd->q.type < SVGA3D_QUERYTYPE_MIN ||
1303 cmd->q.type >= SVGA3D_QUERYTYPE_MAX)
1304 return -EINVAL;
1305
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001306 cotable_res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXQUERY);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001307 ret = vmw_cotable_notify(cotable_res, cmd->q.queryId);
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001308
1309 return ret;
1310}
1311
1312
1313
1314/**
1315 * vmw_cmd_dx_bind_query - validate a SVGA_3D_CMD_DX_BIND_QUERY command.
1316 *
1317 * @dev_priv: Pointer to a device private struct.
1318 * @sw_context: The software context used for this command submission.
1319 * @header: Pointer to the command header in the command stream.
1320 *
1321 * The query bind operation will eventually associate the query ID
1322 * with its backing MOB. In this function, we take the user mode
1323 * MOB ID and use vmw_translate_mob_ptr() to translate it to its
1324 * kernel mode equivalent.
1325 */
1326static int vmw_cmd_dx_bind_query(struct vmw_private *dev_priv,
1327 struct vmw_sw_context *sw_context,
1328 SVGA3dCmdHeader *header)
1329{
1330 struct vmw_dx_bind_query_cmd {
1331 SVGA3dCmdHeader header;
1332 SVGA3dCmdDXBindQuery q;
1333 } *cmd;
1334
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001335 struct vmw_buffer_object *vmw_bo;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001336 int ret;
1337
1338
1339 cmd = container_of(header, struct vmw_dx_bind_query_cmd, header);
1340
1341 /*
1342 * Look up the buffer pointed to by q.mobid, put it on the relocation
1343 * list so its kernel mode MOB ID can be filled in later
1344 */
1345 ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->q.mobid,
1346 &vmw_bo);
1347
1348 if (ret != 0)
1349 return ret;
1350
1351 sw_context->dx_query_mob = vmw_bo;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001352 sw_context->dx_query_ctx = sw_context->dx_ctx_node->ctx;
Thomas Hellstromb139d432018-09-26 16:27:54 +02001353 return 0;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07001354}
1355
1356
1357
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001358/**
Thomas Hellstromddcda242012-11-21 11:26:55 +01001359 * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_QUERY command.
1360 *
1361 * @dev_priv: Pointer to a device private struct.
1362 * @sw_context: The software context used for this command submission.
1363 * @header: Pointer to the command header in the command stream.
1364 */
1365static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
1366 struct vmw_sw_context *sw_context,
1367 SVGA3dCmdHeader *header)
1368{
1369 struct vmw_begin_gb_query_cmd {
1370 SVGA3dCmdHeader header;
1371 SVGA3dCmdBeginGBQuery q;
1372 } *cmd;
1373
1374 cmd = container_of(header, struct vmw_begin_gb_query_cmd,
1375 header);
1376
1377 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001378 VMW_RES_DIRTY_SET, user_context_converter,
1379 &cmd->q.cid, NULL);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001380}
1381
1382/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001383 * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
1384 *
1385 * @dev_priv: Pointer to a device private struct.
1386 * @sw_context: The software context used for this command submission.
1387 * @header: Pointer to the command header in the command stream.
1388 */
1389static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
1390 struct vmw_sw_context *sw_context,
1391 SVGA3dCmdHeader *header)
1392{
1393 struct vmw_begin_query_cmd {
1394 SVGA3dCmdHeader header;
1395 SVGA3dCmdBeginQuery q;
1396 } *cmd;
1397
1398 cmd = container_of(header, struct vmw_begin_query_cmd,
1399 header);
1400
Thomas Hellstromddcda242012-11-21 11:26:55 +01001401 if (unlikely(dev_priv->has_mob)) {
1402 struct {
1403 SVGA3dCmdHeader header;
1404 SVGA3dCmdBeginGBQuery q;
1405 } gb_cmd;
1406
1407 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1408
1409 gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
1410 gb_cmd.header.size = cmd->header.size;
1411 gb_cmd.q.cid = cmd->q.cid;
1412 gb_cmd.q.type = cmd->q.type;
1413
1414 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1415 return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
1416 }
1417
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001418 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001419 VMW_RES_DIRTY_SET, user_context_converter,
1420 &cmd->q.cid, NULL);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001421}
1422
1423/**
Thomas Hellstromddcda242012-11-21 11:26:55 +01001424 * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
1425 *
1426 * @dev_priv: Pointer to a device private struct.
1427 * @sw_context: The software context used for this command submission.
1428 * @header: Pointer to the command header in the command stream.
1429 */
1430static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
1431 struct vmw_sw_context *sw_context,
1432 SVGA3dCmdHeader *header)
1433{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001434 struct vmw_buffer_object *vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001435 struct vmw_query_cmd {
1436 SVGA3dCmdHeader header;
1437 SVGA3dCmdEndGBQuery q;
1438 } *cmd;
1439 int ret;
1440
1441 cmd = container_of(header, struct vmw_query_cmd, header);
1442 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1443 if (unlikely(ret != 0))
1444 return ret;
1445
1446 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
1447 &cmd->q.mobid,
1448 &vmw_bo);
1449 if (unlikely(ret != 0))
1450 return ret;
1451
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001452 ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001453
Thomas Hellstromddcda242012-11-21 11:26:55 +01001454 return ret;
1455}
1456
1457/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001458 * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
1459 *
1460 * @dev_priv: Pointer to a device private struct.
1461 * @sw_context: The software context used for this command submission.
1462 * @header: Pointer to the command header in the command stream.
1463 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001464static int vmw_cmd_end_query(struct vmw_private *dev_priv,
1465 struct vmw_sw_context *sw_context,
1466 SVGA3dCmdHeader *header)
1467{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001468 struct vmw_buffer_object *vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001469 struct vmw_query_cmd {
1470 SVGA3dCmdHeader header;
1471 SVGA3dCmdEndQuery q;
1472 } *cmd;
1473 int ret;
1474
1475 cmd = container_of(header, struct vmw_query_cmd, header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001476 if (dev_priv->has_mob) {
1477 struct {
1478 SVGA3dCmdHeader header;
1479 SVGA3dCmdEndGBQuery q;
1480 } gb_cmd;
1481
1482 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1483
1484 gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
1485 gb_cmd.header.size = cmd->header.size;
1486 gb_cmd.q.cid = cmd->q.cid;
1487 gb_cmd.q.type = cmd->q.type;
1488 gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
1489 gb_cmd.q.offset = cmd->q.guestResult.offset;
1490
1491 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1492 return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
1493 }
1494
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001495 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1496 if (unlikely(ret != 0))
1497 return ret;
1498
1499 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1500 &cmd->q.guestResult,
1501 &vmw_bo);
1502 if (unlikely(ret != 0))
1503 return ret;
1504
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07001505 ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001506
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001507 return ret;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001508}
1509
Thomas Hellstromddcda242012-11-21 11:26:55 +01001510/**
1511 * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
1512 *
1513 * @dev_priv: Pointer to a device private struct.
1514 * @sw_context: The software context used for this command submission.
1515 * @header: Pointer to the command header in the command stream.
1516 */
1517static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
1518 struct vmw_sw_context *sw_context,
1519 SVGA3dCmdHeader *header)
1520{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001521 struct vmw_buffer_object *vmw_bo;
Thomas Hellstromddcda242012-11-21 11:26:55 +01001522 struct vmw_query_cmd {
1523 SVGA3dCmdHeader header;
1524 SVGA3dCmdWaitForGBQuery q;
1525 } *cmd;
1526 int ret;
1527
1528 cmd = container_of(header, struct vmw_query_cmd, header);
1529 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1530 if (unlikely(ret != 0))
1531 return ret;
1532
1533 ret = vmw_translate_mob_ptr(dev_priv, sw_context,
1534 &cmd->q.mobid,
1535 &vmw_bo);
1536 if (unlikely(ret != 0))
1537 return ret;
1538
Thomas Hellstromddcda242012-11-21 11:26:55 +01001539 return 0;
1540}
1541
1542/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001543 * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
1544 *
1545 * @dev_priv: Pointer to a device private struct.
1546 * @sw_context: The software context used for this command submission.
1547 * @header: Pointer to the command header in the command stream.
1548 */
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001549static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
1550 struct vmw_sw_context *sw_context,
1551 SVGA3dCmdHeader *header)
1552{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001553 struct vmw_buffer_object *vmw_bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001554 struct vmw_query_cmd {
1555 SVGA3dCmdHeader header;
1556 SVGA3dCmdWaitForQuery q;
1557 } *cmd;
1558 int ret;
1559
1560 cmd = container_of(header, struct vmw_query_cmd, header);
Thomas Hellstromddcda242012-11-21 11:26:55 +01001561 if (dev_priv->has_mob) {
1562 struct {
1563 SVGA3dCmdHeader header;
1564 SVGA3dCmdWaitForGBQuery q;
1565 } gb_cmd;
1566
1567 BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
1568
1569 gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
1570 gb_cmd.header.size = cmd->header.size;
1571 gb_cmd.q.cid = cmd->q.cid;
1572 gb_cmd.q.type = cmd->q.type;
1573 gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
1574 gb_cmd.q.offset = cmd->q.guestResult.offset;
1575
1576 memcpy(cmd, &gb_cmd, sizeof(*cmd));
1577 return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
1578 }
1579
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001580 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1581 if (unlikely(ret != 0))
1582 return ret;
1583
1584 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1585 &cmd->q.guestResult,
1586 &vmw_bo);
1587 if (unlikely(ret != 0))
1588 return ret;
1589
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001590 return 0;
1591}
1592
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001593static int vmw_cmd_dma(struct vmw_private *dev_priv,
1594 struct vmw_sw_context *sw_context,
1595 SVGA3dCmdHeader *header)
1596{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001597 struct vmw_buffer_object *vmw_bo = NULL;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001598 struct vmw_surface *srf = NULL;
1599 struct vmw_dma_cmd {
1600 SVGA3dCmdHeader header;
1601 SVGA3dCmdSurfaceDMA dma;
1602 } *cmd;
1603 int ret;
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001604 SVGA3dCmdSurfaceDMASuffix *suffix;
1605 uint32_t bo_size;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001606 bool dirty;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001607
1608 cmd = container_of(header, struct vmw_dma_cmd, header);
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001609 suffix = (SVGA3dCmdSurfaceDMASuffix *)((unsigned long) &cmd->dma +
1610 header->size - sizeof(*suffix));
1611
1612 /* Make sure device and verifier stays in sync. */
1613 if (unlikely(suffix->suffixSize != sizeof(*suffix))) {
1614 DRM_ERROR("Invalid DMA suffix size.\n");
1615 return -EINVAL;
1616 }
1617
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +00001618 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
1619 &cmd->dma.guest.ptr,
1620 &vmw_bo);
1621 if (unlikely(ret != 0))
1622 return ret;
1623
Thomas Hellstromcbd75e92014-04-15 18:25:48 +02001624 /* Make sure DMA doesn't cross BO boundaries. */
1625 bo_size = vmw_bo->base.num_pages * PAGE_SIZE;
1626 if (unlikely(cmd->dma.guest.ptr.offset > bo_size)) {
1627 DRM_ERROR("Invalid DMA offset.\n");
1628 return -EINVAL;
1629 }
1630
1631 bo_size -= cmd->dma.guest.ptr.offset;
1632 if (unlikely(suffix->maximumOffset > bo_size))
1633 suffix->maximumOffset = bo_size;
1634
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001635 dirty = (cmd->dma.transfer == SVGA3D_WRITE_HOST_VRAM) ?
1636 VMW_RES_DIRTY_SET : 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001637 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001638 dirty, user_surface_converter,
1639 &cmd->dma.host.sid, NULL);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +02001640 if (unlikely(ret != 0)) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001641 if (unlikely(ret != -ERESTARTSYS))
1642 DRM_ERROR("could not find surface for DMA.\n");
Thomas Hellstromb139d432018-09-26 16:27:54 +02001643 return ret;
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +02001644 }
1645
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001646 srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001647
Thomas Hellstromd5bde952014-01-31 10:12:10 +01001648 vmw_kms_cursor_snoop(srf, sw_context->fp->tfile, &vmw_bo->base,
1649 header);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001650
Thomas Hellstromb139d432018-09-26 16:27:54 +02001651 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001652}
1653
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001654static int vmw_cmd_draw(struct vmw_private *dev_priv,
1655 struct vmw_sw_context *sw_context,
1656 SVGA3dCmdHeader *header)
1657{
1658 struct vmw_draw_cmd {
1659 SVGA3dCmdHeader header;
1660 SVGA3dCmdDrawPrimitives body;
1661 } *cmd;
1662 SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
1663 (unsigned long)header + sizeof(*cmd));
1664 SVGA3dPrimitiveRange *range;
1665 uint32_t i;
1666 uint32_t maxnum;
1667 int ret;
1668
1669 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
1670 if (unlikely(ret != 0))
1671 return ret;
1672
1673 cmd = container_of(header, struct vmw_draw_cmd, header);
1674 maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
1675
1676 if (unlikely(cmd->body.numVertexDecls > maxnum)) {
1677 DRM_ERROR("Illegal number of vertex declarations.\n");
1678 return -EINVAL;
1679 }
1680
1681 for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001682 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001683 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001684 user_surface_converter,
1685 &decl->array.surfaceId, NULL);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001686 if (unlikely(ret != 0))
1687 return ret;
1688 }
1689
1690 maxnum = (header->size - sizeof(cmd->body) -
1691 cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
1692 if (unlikely(cmd->body.numRanges > maxnum)) {
1693 DRM_ERROR("Illegal number of index ranges.\n");
1694 return -EINVAL;
1695 }
1696
1697 range = (SVGA3dPrimitiveRange *) decl;
1698 for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001699 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001700 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001701 user_surface_converter,
1702 &range->indexArray.surfaceId, NULL);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001703 if (unlikely(ret != 0))
1704 return ret;
1705 }
1706 return 0;
1707}
1708
1709
1710static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
1711 struct vmw_sw_context *sw_context,
1712 SVGA3dCmdHeader *header)
1713{
1714 struct vmw_tex_state_cmd {
1715 SVGA3dCmdHeader header;
1716 SVGA3dCmdSetTextureState state;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001717 } *cmd;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001718
1719 SVGA3dTextureState *last_state = (SVGA3dTextureState *)
1720 ((unsigned long) header + header->size + sizeof(header));
1721 SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
1722 ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001723 struct vmw_resource *ctx;
1724 struct vmw_resource *res;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001725 int ret;
1726
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001727 cmd = container_of(header, struct vmw_tex_state_cmd,
1728 header);
1729
1730 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001731 VMW_RES_DIRTY_SET, user_context_converter,
1732 &cmd->state.cid,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001733 &ctx);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001734 if (unlikely(ret != 0))
1735 return ret;
1736
1737 for (; cur_state < last_state; ++cur_state) {
1738 if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
1739 continue;
1740
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001741 if (cur_state->stage >= SVGA3D_NUM_TEXTURE_UNITS) {
1742 DRM_ERROR("Illegal texture/sampler unit %u.\n",
1743 (unsigned) cur_state->stage);
1744 return -EINVAL;
1745 }
1746
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001747 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001748 VMW_RES_DIRTY_NONE,
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001749 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001750 &cur_state->value, &res);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001751 if (unlikely(ret != 0))
1752 return ret;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001753
1754 if (dev_priv->has_mob) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001755 struct vmw_ctx_bindinfo_tex binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001756 struct vmw_ctx_validation_info *node;
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001757
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001758 node = vmw_execbuf_info_from_res(sw_context, ctx);
1759 if (!node)
1760 return -EINVAL;
1761
1762 binding.bi.ctx = ctx;
1763 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001764 binding.bi.bt = vmw_ctx_binding_tex;
1765 binding.texture_stage = cur_state->stage;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001766 vmw_binding_add(node->staged, &binding.bi, 0,
1767 binding.texture_stage);
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07001768 }
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001769 }
1770
1771 return 0;
1772}
1773
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001774static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
1775 struct vmw_sw_context *sw_context,
1776 void *buf)
1777{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001778 struct vmw_buffer_object *vmw_bo;
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001779
1780 struct {
1781 uint32_t header;
1782 SVGAFifoCmdDefineGMRFB body;
1783 } *cmd = buf;
1784
Thomas Hellstromb139d432018-09-26 16:27:54 +02001785 return vmw_translate_guest_ptr(dev_priv, sw_context,
1786 &cmd->body.ptr,
1787 &vmw_bo);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02001788}
1789
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001790
1791/**
1792 * vmw_cmd_res_switch_backup - Utility function to handle backup buffer
1793 * switching
1794 *
1795 * @dev_priv: Pointer to a device private struct.
1796 * @sw_context: The software context being used for this batch.
1797 * @val_node: The validation node representing the resource.
1798 * @buf_id: Pointer to the user-space backup buffer handle in the command
1799 * stream.
1800 * @backup_offset: Offset of backup into MOB.
1801 *
1802 * This function prepares for registering a switch of backup buffers
1803 * in the resource metadata just prior to unreserving. It's basically a wrapper
1804 * around vmw_cmd_res_switch_backup with a different interface.
1805 */
1806static int vmw_cmd_res_switch_backup(struct vmw_private *dev_priv,
1807 struct vmw_sw_context *sw_context,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001808 struct vmw_resource *res,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001809 uint32_t *buf_id,
1810 unsigned long backup_offset)
1811{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001812 struct vmw_buffer_object *vbo;
1813 void *info;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001814 int ret;
1815
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001816 info = vmw_execbuf_info_from_res(sw_context, res);
1817 if (!info)
1818 return -EINVAL;
1819
1820 ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &vbo);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001821 if (ret)
1822 return ret;
1823
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001824 vmw_validation_res_switch_backup(sw_context->ctx, info, vbo,
1825 backup_offset);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001826 return 0;
1827}
1828
1829
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001830/**
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001831 * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
1832 *
1833 * @dev_priv: Pointer to a device private struct.
1834 * @sw_context: The software context being used for this batch.
1835 * @res_type: The resource type.
1836 * @converter: Information about user-space binding for this resource type.
1837 * @res_id: Pointer to the user-space resource handle in the command stream.
1838 * @buf_id: Pointer to the user-space backup buffer handle in the command
1839 * stream.
1840 * @backup_offset: Offset of backup into MOB.
1841 *
1842 * This function prepares for registering a switch of backup buffers
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001843 * in the resource metadata just prior to unreserving. It's basically a wrapper
1844 * around vmw_cmd_res_switch_backup with a different interface.
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001845 */
1846static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
1847 struct vmw_sw_context *sw_context,
1848 enum vmw_res_type res_type,
1849 const struct vmw_user_resource_conv
1850 *converter,
1851 uint32_t *res_id,
1852 uint32_t *buf_id,
1853 unsigned long backup_offset)
1854{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001855 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001856 int ret;
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001857
1858 ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001859 VMW_RES_DIRTY_NONE, converter, res_id, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001860 if (ret)
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001861 return ret;
1862
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02001863 return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001864 buf_id, backup_offset);
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001865}
1866
1867/**
1868 * vmw_cmd_bind_gb_surface - Validate an SVGA_3D_CMD_BIND_GB_SURFACE
1869 * command
1870 *
1871 * @dev_priv: Pointer to a device private struct.
1872 * @sw_context: The software context being used for this batch.
1873 * @header: Pointer to the command header in the command stream.
1874 */
1875static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
1876 struct vmw_sw_context *sw_context,
1877 SVGA3dCmdHeader *header)
1878{
1879 struct vmw_bind_gb_surface_cmd {
1880 SVGA3dCmdHeader header;
1881 SVGA3dCmdBindGBSurface body;
1882 } *cmd;
1883
1884 cmd = container_of(header, struct vmw_bind_gb_surface_cmd, header);
1885
1886 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
1887 user_surface_converter,
1888 &cmd->body.sid, &cmd->body.mobid,
1889 0);
1890}
1891
1892/**
1893 * vmw_cmd_update_gb_image - Validate an SVGA_3D_CMD_UPDATE_GB_IMAGE
1894 * command
1895 *
1896 * @dev_priv: Pointer to a device private struct.
1897 * @sw_context: The software context being used for this batch.
1898 * @header: Pointer to the command header in the command stream.
1899 */
1900static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
1901 struct vmw_sw_context *sw_context,
1902 SVGA3dCmdHeader *header)
1903{
1904 struct vmw_gb_surface_cmd {
1905 SVGA3dCmdHeader header;
1906 SVGA3dCmdUpdateGBImage body;
1907 } *cmd;
1908
1909 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1910
1911 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001912 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001913 &cmd->body.image.sid, NULL);
1914}
1915
1916/**
1917 * vmw_cmd_update_gb_surface - Validate an SVGA_3D_CMD_UPDATE_GB_SURFACE
1918 * command
1919 *
1920 * @dev_priv: Pointer to a device private struct.
1921 * @sw_context: The software context being used for this batch.
1922 * @header: Pointer to the command header in the command stream.
1923 */
1924static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
1925 struct vmw_sw_context *sw_context,
1926 SVGA3dCmdHeader *header)
1927{
1928 struct vmw_gb_surface_cmd {
1929 SVGA3dCmdHeader header;
1930 SVGA3dCmdUpdateGBSurface body;
1931 } *cmd;
1932
1933 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1934
1935 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001936 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001937 &cmd->body.sid, NULL);
1938}
1939
1940/**
1941 * vmw_cmd_readback_gb_image - Validate an SVGA_3D_CMD_READBACK_GB_IMAGE
1942 * command
1943 *
1944 * @dev_priv: Pointer to a device private struct.
1945 * @sw_context: The software context being used for this batch.
1946 * @header: Pointer to the command header in the command stream.
1947 */
1948static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
1949 struct vmw_sw_context *sw_context,
1950 SVGA3dCmdHeader *header)
1951{
1952 struct vmw_gb_surface_cmd {
1953 SVGA3dCmdHeader header;
1954 SVGA3dCmdReadbackGBImage body;
1955 } *cmd;
1956
1957 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1958
1959 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001960 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001961 &cmd->body.image.sid, NULL);
1962}
1963
1964/**
1965 * vmw_cmd_readback_gb_surface - Validate an SVGA_3D_CMD_READBACK_GB_SURFACE
1966 * command
1967 *
1968 * @dev_priv: Pointer to a device private struct.
1969 * @sw_context: The software context being used for this batch.
1970 * @header: Pointer to the command header in the command stream.
1971 */
1972static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
1973 struct vmw_sw_context *sw_context,
1974 SVGA3dCmdHeader *header)
1975{
1976 struct vmw_gb_surface_cmd {
1977 SVGA3dCmdHeader header;
1978 SVGA3dCmdReadbackGBSurface body;
1979 } *cmd;
1980
1981 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
1982
1983 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001984 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01001985 &cmd->body.sid, NULL);
1986}
1987
1988/**
1989 * vmw_cmd_invalidate_gb_image - Validate an SVGA_3D_CMD_INVALIDATE_GB_IMAGE
1990 * command
1991 *
1992 * @dev_priv: Pointer to a device private struct.
1993 * @sw_context: The software context being used for this batch.
1994 * @header: Pointer to the command header in the command stream.
1995 */
1996static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
1997 struct vmw_sw_context *sw_context,
1998 SVGA3dCmdHeader *header)
1999{
2000 struct vmw_gb_surface_cmd {
2001 SVGA3dCmdHeader header;
2002 SVGA3dCmdInvalidateGBImage body;
2003 } *cmd;
2004
2005 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
2006
2007 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002008 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01002009 &cmd->body.image.sid, NULL);
2010}
2011
2012/**
2013 * vmw_cmd_invalidate_gb_surface - Validate an
2014 * SVGA_3D_CMD_INVALIDATE_GB_SURFACE command
2015 *
2016 * @dev_priv: Pointer to a device private struct.
2017 * @sw_context: The software context being used for this batch.
2018 * @header: Pointer to the command header in the command stream.
2019 */
2020static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
2021 struct vmw_sw_context *sw_context,
2022 SVGA3dCmdHeader *header)
2023{
2024 struct vmw_gb_surface_cmd {
2025 SVGA3dCmdHeader header;
2026 SVGA3dCmdInvalidateGBSurface body;
2027 } *cmd;
2028
2029 cmd = container_of(header, struct vmw_gb_surface_cmd, header);
2030
2031 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002032 VMW_RES_DIRTY_CLEAR, user_surface_converter,
Thomas Hellstroma97e2192012-11-21 11:45:13 +01002033 &cmd->body.sid, NULL);
2034}
2035
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002036
2037/**
2038 * vmw_cmd_shader_define - Validate an SVGA_3D_CMD_SHADER_DEFINE
2039 * command
2040 *
2041 * @dev_priv: Pointer to a device private struct.
2042 * @sw_context: The software context being used for this batch.
2043 * @header: Pointer to the command header in the command stream.
2044 */
2045static int vmw_cmd_shader_define(struct vmw_private *dev_priv,
2046 struct vmw_sw_context *sw_context,
2047 SVGA3dCmdHeader *header)
2048{
2049 struct vmw_shader_define_cmd {
2050 SVGA3dCmdHeader header;
2051 SVGA3dCmdDefineShader body;
2052 } *cmd;
2053 int ret;
2054 size_t size;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002055 struct vmw_resource *ctx;
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002056
2057 cmd = container_of(header, struct vmw_shader_define_cmd,
2058 header);
2059
2060 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002061 VMW_RES_DIRTY_SET, user_context_converter,
2062 &cmd->body.cid, &ctx);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002063 if (unlikely(ret != 0))
2064 return ret;
2065
2066 if (unlikely(!dev_priv->has_mob))
2067 return 0;
2068
2069 size = cmd->header.size - sizeof(cmd->body);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002070 ret = vmw_compat_shader_add(dev_priv,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002071 vmw_context_res_man(ctx),
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002072 cmd->body.shid, cmd + 1,
2073 cmd->body.type, size,
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002074 &sw_context->staged_cmd_res);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002075 if (unlikely(ret != 0))
2076 return ret;
2077
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02002078 return vmw_resource_relocation_add(sw_context,
Thomas Hellstrome7a45282016-10-10 10:44:00 -07002079 NULL,
2080 vmw_ptr_diff(sw_context->buf_start,
Thomas Hellstroma1944032016-10-10 11:06:45 -07002081 &cmd->header.id),
2082 vmw_res_rel_nop);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002083}
2084
2085/**
2086 * vmw_cmd_shader_destroy - Validate an SVGA_3D_CMD_SHADER_DESTROY
2087 * command
2088 *
2089 * @dev_priv: Pointer to a device private struct.
2090 * @sw_context: The software context being used for this batch.
2091 * @header: Pointer to the command header in the command stream.
2092 */
2093static int vmw_cmd_shader_destroy(struct vmw_private *dev_priv,
2094 struct vmw_sw_context *sw_context,
2095 SVGA3dCmdHeader *header)
2096{
2097 struct vmw_shader_destroy_cmd {
2098 SVGA3dCmdHeader header;
2099 SVGA3dCmdDestroyShader body;
2100 } *cmd;
2101 int ret;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002102 struct vmw_resource *ctx;
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002103
2104 cmd = container_of(header, struct vmw_shader_destroy_cmd,
2105 header);
2106
2107 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002108 VMW_RES_DIRTY_SET, user_context_converter,
2109 &cmd->body.cid, &ctx);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002110 if (unlikely(ret != 0))
2111 return ret;
2112
2113 if (unlikely(!dev_priv->has_mob))
2114 return 0;
2115
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002116 ret = vmw_shader_remove(vmw_context_res_man(ctx),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002117 cmd->body.shid,
2118 cmd->body.type,
2119 &sw_context->staged_cmd_res);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002120 if (unlikely(ret != 0))
2121 return ret;
2122
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02002123 return vmw_resource_relocation_add(sw_context,
Thomas Hellstrome7a45282016-10-10 10:44:00 -07002124 NULL,
2125 vmw_ptr_diff(sw_context->buf_start,
Thomas Hellstroma1944032016-10-10 11:06:45 -07002126 &cmd->header.id),
2127 vmw_res_rel_nop);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002128}
2129
Thomas Hellstroma97e2192012-11-21 11:45:13 +01002130/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002131 * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
2132 * command
2133 *
2134 * @dev_priv: Pointer to a device private struct.
2135 * @sw_context: The software context being used for this batch.
2136 * @header: Pointer to the command header in the command stream.
2137 */
2138static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
2139 struct vmw_sw_context *sw_context,
2140 SVGA3dCmdHeader *header)
2141{
2142 struct vmw_set_shader_cmd {
2143 SVGA3dCmdHeader header;
2144 SVGA3dCmdSetShader body;
2145 } *cmd;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002146 struct vmw_ctx_bindinfo_shader binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002147 struct vmw_resource *ctx, *res = NULL;
2148 struct vmw_ctx_validation_info *ctx_info;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002149 int ret;
2150
2151 cmd = container_of(header, struct vmw_set_shader_cmd,
2152 header);
2153
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002154 if (cmd->body.type >= SVGA3D_SHADERTYPE_PREDX_MAX) {
2155 DRM_ERROR("Illegal shader type %u.\n",
2156 (unsigned) cmd->body.type);
2157 return -EINVAL;
2158 }
2159
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07002160 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002161 VMW_RES_DIRTY_SET, user_context_converter,
2162 &cmd->body.cid, &ctx);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002163 if (unlikely(ret != 0))
2164 return ret;
2165
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002166 if (!dev_priv->has_mob)
2167 return 0;
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002168
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002169 if (cmd->body.shid != SVGA3D_INVALID_ID) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002170 res = vmw_shader_lookup(vmw_context_res_man(ctx),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002171 cmd->body.shid,
2172 cmd->body.type);
Thomas Hellstromd5bde952014-01-31 10:12:10 +01002173
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002174 if (!IS_ERR(res)) {
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002175 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2176 VMW_RES_DIRTY_NONE);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002177 if (unlikely(ret != 0))
2178 return ret;
2179 }
Thomas Hellstromb5c3b1a62013-10-08 02:27:17 -07002180 }
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002181
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002182 if (IS_ERR_OR_NULL(res)) {
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002183 ret = vmw_cmd_res_check(dev_priv, sw_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002184 vmw_res_shader, VMW_RES_DIRTY_NONE,
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002185 user_shader_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002186 &cmd->body.shid, &res);
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02002187 if (unlikely(ret != 0))
2188 return ret;
2189 }
2190
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002191 ctx_info = vmw_execbuf_info_from_res(sw_context, ctx);
2192 if (!ctx_info)
2193 return -EINVAL;
2194
2195 binding.bi.ctx = ctx;
2196 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002197 binding.bi.bt = vmw_ctx_binding_shader;
2198 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002199 vmw_binding_add(ctx_info->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002200 binding.shader_slot, 0);
2201 return 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00002202}
2203
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002204/**
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002205 * vmw_cmd_set_shader_const - Validate an SVGA_3D_CMD_SET_SHADER_CONST
2206 * command
2207 *
2208 * @dev_priv: Pointer to a device private struct.
2209 * @sw_context: The software context being used for this batch.
2210 * @header: Pointer to the command header in the command stream.
2211 */
2212static int vmw_cmd_set_shader_const(struct vmw_private *dev_priv,
2213 struct vmw_sw_context *sw_context,
2214 SVGA3dCmdHeader *header)
2215{
2216 struct vmw_set_shader_const_cmd {
2217 SVGA3dCmdHeader header;
2218 SVGA3dCmdSetShaderConst body;
2219 } *cmd;
2220 int ret;
2221
2222 cmd = container_of(header, struct vmw_set_shader_const_cmd,
2223 header);
2224
2225 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002226 VMW_RES_DIRTY_SET, user_context_converter,
2227 &cmd->body.cid, NULL);
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01002228 if (unlikely(ret != 0))
2229 return ret;
2230
2231 if (dev_priv->has_mob)
2232 header->id = SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE;
2233
2234 return 0;
2235}
2236
2237/**
Thomas Hellstromc74c1622012-11-21 12:10:26 +01002238 * vmw_cmd_bind_gb_shader - Validate an SVGA_3D_CMD_BIND_GB_SHADER
2239 * command
2240 *
2241 * @dev_priv: Pointer to a device private struct.
2242 * @sw_context: The software context being used for this batch.
2243 * @header: Pointer to the command header in the command stream.
2244 */
2245static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
2246 struct vmw_sw_context *sw_context,
2247 SVGA3dCmdHeader *header)
2248{
2249 struct vmw_bind_gb_shader_cmd {
2250 SVGA3dCmdHeader header;
2251 SVGA3dCmdBindGBShader body;
2252 } *cmd;
2253
2254 cmd = container_of(header, struct vmw_bind_gb_shader_cmd,
2255 header);
2256
2257 return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
2258 user_shader_converter,
2259 &cmd->body.shid, &cmd->body.mobid,
2260 cmd->body.offsetInBytes);
2261}
2262
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002263/**
2264 * vmw_cmd_dx_set_single_constant_buffer - Validate an
2265 * SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER command.
2266 *
2267 * @dev_priv: Pointer to a device private struct.
2268 * @sw_context: The software context being used for this batch.
2269 * @header: Pointer to the command header in the command stream.
2270 */
2271static int
2272vmw_cmd_dx_set_single_constant_buffer(struct vmw_private *dev_priv,
2273 struct vmw_sw_context *sw_context,
2274 SVGA3dCmdHeader *header)
2275{
2276 struct {
2277 SVGA3dCmdHeader header;
2278 SVGA3dCmdDXSetSingleConstantBuffer body;
2279 } *cmd;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002280 struct vmw_resource *res = NULL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002281 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002282 struct vmw_ctx_bindinfo_cb binding;
2283 int ret;
2284
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002285 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002286 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002287
2288 cmd = container_of(header, typeof(*cmd), header);
2289 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002290 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002291 &cmd->body.sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002292 if (unlikely(ret != 0))
2293 return ret;
2294
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002295 binding.bi.ctx = ctx_node->ctx;
2296 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002297 binding.bi.bt = vmw_ctx_binding_cb;
2298 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2299 binding.offset = cmd->body.offsetInBytes;
2300 binding.size = cmd->body.sizeInBytes;
2301 binding.slot = cmd->body.slot;
2302
2303 if (binding.shader_slot >= SVGA3D_NUM_SHADERTYPE_DX10 ||
2304 binding.slot >= SVGA3D_DX_MAX_CONSTBUFFERS) {
2305 DRM_ERROR("Illegal const buffer shader %u slot %u.\n",
2306 (unsigned) cmd->body.type,
2307 (unsigned) binding.slot);
2308 return -EINVAL;
2309 }
2310
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002311 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002312 binding.shader_slot, binding.slot);
2313
2314 return 0;
2315}
2316
2317/**
2318 * vmw_cmd_dx_set_shader_res - Validate an
2319 * SVGA_3D_CMD_DX_SET_SHADER_RESOURCES command
2320 *
2321 * @dev_priv: Pointer to a device private struct.
2322 * @sw_context: The software context being used for this batch.
2323 * @header: Pointer to the command header in the command stream.
2324 */
2325static int vmw_cmd_dx_set_shader_res(struct vmw_private *dev_priv,
2326 struct vmw_sw_context *sw_context,
2327 SVGA3dCmdHeader *header)
2328{
2329 struct {
2330 SVGA3dCmdHeader header;
2331 SVGA3dCmdDXSetShaderResources body;
2332 } *cmd = container_of(header, typeof(*cmd), header);
2333 u32 num_sr_view = (cmd->header.size - sizeof(cmd->body)) /
2334 sizeof(SVGA3dShaderResourceViewId);
2335
2336 if ((u64) cmd->body.startView + (u64) num_sr_view >
2337 (u64) SVGA3D_DX_MAX_SRVIEWS ||
2338 cmd->body.type >= SVGA3D_SHADERTYPE_DX10_MAX) {
2339 DRM_ERROR("Invalid shader binding.\n");
2340 return -EINVAL;
2341 }
2342
2343 return vmw_view_bindings_add(sw_context, vmw_view_sr,
2344 vmw_ctx_binding_sr,
2345 cmd->body.type - SVGA3D_SHADERTYPE_MIN,
2346 (void *) &cmd[1], num_sr_view,
2347 cmd->body.startView);
2348}
2349
2350/**
2351 * vmw_cmd_dx_set_shader - Validate an SVGA_3D_CMD_DX_SET_SHADER
2352 * command
2353 *
2354 * @dev_priv: Pointer to a device private struct.
2355 * @sw_context: The software context being used for this batch.
2356 * @header: Pointer to the command header in the command stream.
2357 */
2358static int vmw_cmd_dx_set_shader(struct vmw_private *dev_priv,
2359 struct vmw_sw_context *sw_context,
2360 SVGA3dCmdHeader *header)
2361{
2362 struct {
2363 SVGA3dCmdHeader header;
2364 SVGA3dCmdDXSetShader body;
2365 } *cmd;
2366 struct vmw_resource *res = NULL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002367 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002368 struct vmw_ctx_bindinfo_shader binding;
2369 int ret = 0;
2370
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002371 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002372 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002373
2374 cmd = container_of(header, typeof(*cmd), header);
2375
2376 if (cmd->body.type >= SVGA3D_SHADERTYPE_DX10_MAX) {
2377 DRM_ERROR("Illegal shader type %u.\n",
2378 (unsigned) cmd->body.type);
2379 return -EINVAL;
2380 }
2381
2382 if (cmd->body.shaderId != SVGA3D_INVALID_ID) {
2383 res = vmw_shader_lookup(sw_context->man, cmd->body.shaderId, 0);
2384 if (IS_ERR(res)) {
2385 DRM_ERROR("Could not find shader for binding.\n");
2386 return PTR_ERR(res);
2387 }
2388
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002389 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2390 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002391 if (ret)
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002392 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002393 }
2394
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002395 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002396 binding.bi.res = res;
2397 binding.bi.bt = vmw_ctx_binding_dx_shader;
2398 binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
2399
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002400 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002401 binding.shader_slot, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002402
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002403 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002404}
2405
2406/**
2407 * vmw_cmd_dx_set_vertex_buffers - Validates an
2408 * SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS command
2409 *
2410 * @dev_priv: Pointer to a device private struct.
2411 * @sw_context: The software context being used for this batch.
2412 * @header: Pointer to the command header in the command stream.
2413 */
2414static int vmw_cmd_dx_set_vertex_buffers(struct vmw_private *dev_priv,
2415 struct vmw_sw_context *sw_context,
2416 SVGA3dCmdHeader *header)
2417{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002418 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002419 struct vmw_ctx_bindinfo_vb binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002420 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002421 struct {
2422 SVGA3dCmdHeader header;
2423 SVGA3dCmdDXSetVertexBuffers body;
2424 SVGA3dVertexBuffer buf[];
2425 } *cmd;
2426 int i, ret, num;
2427
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002428 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002429 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002430
2431 cmd = container_of(header, typeof(*cmd), header);
2432 num = (cmd->header.size - sizeof(cmd->body)) /
2433 sizeof(SVGA3dVertexBuffer);
2434 if ((u64)num + (u64)cmd->body.startBuffer >
2435 (u64)SVGA3D_DX_MAX_VERTEXBUFFERS) {
2436 DRM_ERROR("Invalid number of vertex buffers.\n");
2437 return -EINVAL;
2438 }
2439
2440 for (i = 0; i < num; i++) {
2441 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002442 VMW_RES_DIRTY_NONE,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002443 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002444 &cmd->buf[i].sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002445 if (unlikely(ret != 0))
2446 return ret;
2447
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002448 binding.bi.ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002449 binding.bi.bt = vmw_ctx_binding_vb;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002450 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002451 binding.offset = cmd->buf[i].offset;
2452 binding.stride = cmd->buf[i].stride;
2453 binding.slot = i + cmd->body.startBuffer;
2454
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002455 vmw_binding_add(ctx_node->staged, &binding.bi,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002456 0, binding.slot);
2457 }
2458
2459 return 0;
2460}
2461
2462/**
2463 * vmw_cmd_dx_ia_set_vertex_buffers - Validate an
Brian Paul8bd62872017-07-17 07:36:10 -07002464 * SVGA_3D_CMD_DX_IA_SET_INDEX_BUFFER command.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002465 *
2466 * @dev_priv: Pointer to a device private struct.
2467 * @sw_context: The software context being used for this batch.
2468 * @header: Pointer to the command header in the command stream.
2469 */
2470static int vmw_cmd_dx_set_index_buffer(struct vmw_private *dev_priv,
2471 struct vmw_sw_context *sw_context,
2472 SVGA3dCmdHeader *header)
2473{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002474 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002475 struct vmw_ctx_bindinfo_ib binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002476 struct vmw_resource *res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002477 struct {
2478 SVGA3dCmdHeader header;
2479 SVGA3dCmdDXSetIndexBuffer body;
2480 } *cmd;
2481 int ret;
2482
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002483 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002484 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002485
2486 cmd = container_of(header, typeof(*cmd), header);
2487 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002488 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002489 &cmd->body.sid, &res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002490 if (unlikely(ret != 0))
2491 return ret;
2492
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002493 binding.bi.ctx = ctx_node->ctx;
2494 binding.bi.res = res;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002495 binding.bi.bt = vmw_ctx_binding_ib;
2496 binding.offset = cmd->body.offset;
2497 binding.format = cmd->body.format;
2498
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002499 vmw_binding_add(ctx_node->staged, &binding.bi, 0, 0);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002500
2501 return 0;
2502}
2503
2504/**
2505 * vmw_cmd_dx_set_rendertarget - Validate an
2506 * SVGA_3D_CMD_DX_SET_RENDERTARGETS command
2507 *
2508 * @dev_priv: Pointer to a device private struct.
2509 * @sw_context: The software context being used for this batch.
2510 * @header: Pointer to the command header in the command stream.
2511 */
2512static int vmw_cmd_dx_set_rendertargets(struct vmw_private *dev_priv,
2513 struct vmw_sw_context *sw_context,
2514 SVGA3dCmdHeader *header)
2515{
2516 struct {
2517 SVGA3dCmdHeader header;
2518 SVGA3dCmdDXSetRenderTargets body;
2519 } *cmd = container_of(header, typeof(*cmd), header);
2520 int ret;
2521 u32 num_rt_view = (cmd->header.size - sizeof(cmd->body)) /
2522 sizeof(SVGA3dRenderTargetViewId);
2523
2524 if (num_rt_view > SVGA3D_MAX_SIMULTANEOUS_RENDER_TARGETS) {
2525 DRM_ERROR("Invalid DX Rendertarget binding.\n");
2526 return -EINVAL;
2527 }
2528
2529 ret = vmw_view_bindings_add(sw_context, vmw_view_ds,
2530 vmw_ctx_binding_ds, 0,
2531 &cmd->body.depthStencilViewId, 1, 0);
2532 if (ret)
2533 return ret;
2534
2535 return vmw_view_bindings_add(sw_context, vmw_view_rt,
2536 vmw_ctx_binding_dx_rt, 0,
2537 (void *)&cmd[1], num_rt_view, 0);
2538}
2539
2540/**
2541 * vmw_cmd_dx_clear_rendertarget_view - Validate an
2542 * SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW command
2543 *
2544 * @dev_priv: Pointer to a device private struct.
2545 * @sw_context: The software context being used for this batch.
2546 * @header: Pointer to the command header in the command stream.
2547 */
2548static int vmw_cmd_dx_clear_rendertarget_view(struct vmw_private *dev_priv,
2549 struct vmw_sw_context *sw_context,
2550 SVGA3dCmdHeader *header)
2551{
2552 struct {
2553 SVGA3dCmdHeader header;
2554 SVGA3dCmdDXClearRenderTargetView body;
2555 } *cmd = container_of(header, typeof(*cmd), header);
2556
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002557 return PTR_RET(vmw_view_id_val_add(sw_context, vmw_view_rt,
2558 cmd->body.renderTargetViewId));
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002559}
2560
2561/**
2562 * vmw_cmd_dx_clear_rendertarget_view - Validate an
2563 * SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW command
2564 *
2565 * @dev_priv: Pointer to a device private struct.
2566 * @sw_context: The software context being used for this batch.
2567 * @header: Pointer to the command header in the command stream.
2568 */
2569static int vmw_cmd_dx_clear_depthstencil_view(struct vmw_private *dev_priv,
2570 struct vmw_sw_context *sw_context,
2571 SVGA3dCmdHeader *header)
2572{
2573 struct {
2574 SVGA3dCmdHeader header;
2575 SVGA3dCmdDXClearDepthStencilView body;
2576 } *cmd = container_of(header, typeof(*cmd), header);
2577
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002578 return PTR_RET(vmw_view_id_val_add(sw_context, vmw_view_ds,
2579 cmd->body.depthStencilViewId));
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002580}
2581
2582static int vmw_cmd_dx_view_define(struct vmw_private *dev_priv,
2583 struct vmw_sw_context *sw_context,
2584 SVGA3dCmdHeader *header)
2585{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002586 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002587 struct vmw_resource *srf;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002588 struct vmw_resource *res;
2589 enum vmw_view_type view_type;
2590 int ret;
2591 /*
2592 * This is based on the fact that all affected define commands have
2593 * the same initial command body layout.
2594 */
2595 struct {
2596 SVGA3dCmdHeader header;
2597 uint32 defined_id;
2598 uint32 sid;
2599 } *cmd;
2600
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002601 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002602 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002603
2604 view_type = vmw_view_cmd_to_type(header->id);
Dan Carpenter0d9cac02018-01-10 12:40:04 +03002605 if (view_type == vmw_view_max)
2606 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002607 cmd = container_of(header, typeof(*cmd), header);
2608 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002609 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002610 &cmd->sid, &srf);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002611 if (unlikely(ret != 0))
2612 return ret;
2613
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002614 res = vmw_context_cotable(ctx_node->ctx, vmw_view_cotables[view_type]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002615 ret = vmw_cotable_notify(res, cmd->defined_id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002616 if (unlikely(ret != 0))
2617 return ret;
2618
2619 return vmw_view_add(sw_context->man,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002620 ctx_node->ctx,
2621 srf,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002622 view_type,
2623 cmd->defined_id,
2624 header,
2625 header->size + sizeof(*header),
2626 &sw_context->staged_cmd_res);
2627}
2628
Charmaine Lee2f633e52015-08-10 10:45:11 -07002629/**
2630 * vmw_cmd_dx_set_so_targets - Validate an
2631 * SVGA_3D_CMD_DX_SET_SOTARGETS command.
2632 *
2633 * @dev_priv: Pointer to a device private struct.
2634 * @sw_context: The software context being used for this batch.
2635 * @header: Pointer to the command header in the command stream.
2636 */
2637static int vmw_cmd_dx_set_so_targets(struct vmw_private *dev_priv,
2638 struct vmw_sw_context *sw_context,
2639 SVGA3dCmdHeader *header)
2640{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002641 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Charmaine Lee2f633e52015-08-10 10:45:11 -07002642 struct vmw_ctx_bindinfo_so binding;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002643 struct vmw_resource *res;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002644 struct {
2645 SVGA3dCmdHeader header;
2646 SVGA3dCmdDXSetSOTargets body;
2647 SVGA3dSoTarget targets[];
2648 } *cmd;
2649 int i, ret, num;
2650
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002651 if (!ctx_node)
Charmaine Lee2f633e52015-08-10 10:45:11 -07002652 return -EINVAL;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002653
2654 cmd = container_of(header, typeof(*cmd), header);
2655 num = (cmd->header.size - sizeof(cmd->body)) /
2656 sizeof(SVGA3dSoTarget);
2657
2658 if (num > SVGA3D_DX_MAX_SOTARGETS) {
2659 DRM_ERROR("Invalid DX SO binding.\n");
2660 return -EINVAL;
2661 }
2662
2663 for (i = 0; i < num; i++) {
2664 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002665 VMW_RES_DIRTY_SET,
Charmaine Lee2f633e52015-08-10 10:45:11 -07002666 user_surface_converter,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002667 &cmd->targets[i].sid, &res);
Charmaine Lee2f633e52015-08-10 10:45:11 -07002668 if (unlikely(ret != 0))
2669 return ret;
2670
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002671 binding.bi.ctx = ctx_node->ctx;
2672 binding.bi.res = res;
Charmaine Lee2f633e52015-08-10 10:45:11 -07002673 binding.bi.bt = vmw_ctx_binding_so,
2674 binding.offset = cmd->targets[i].offset;
2675 binding.size = cmd->targets[i].sizeInBytes;
2676 binding.slot = i;
2677
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002678 vmw_binding_add(ctx_node->staged, &binding.bi,
Charmaine Lee2f633e52015-08-10 10:45:11 -07002679 0, binding.slot);
2680 }
2681
2682 return 0;
2683}
2684
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002685static int vmw_cmd_dx_so_define(struct vmw_private *dev_priv,
2686 struct vmw_sw_context *sw_context,
2687 SVGA3dCmdHeader *header)
2688{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002689 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002690 struct vmw_resource *res;
2691 /*
2692 * This is based on the fact that all affected define commands have
2693 * the same initial command body layout.
2694 */
2695 struct {
2696 SVGA3dCmdHeader header;
2697 uint32 defined_id;
2698 } *cmd;
2699 enum vmw_so_type so_type;
2700 int ret;
2701
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002702 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002703 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002704
2705 so_type = vmw_so_cmd_to_type(header->id);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002706 res = vmw_context_cotable(ctx_node->ctx, vmw_so_cotables[so_type]);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002707 cmd = container_of(header, typeof(*cmd), header);
2708 ret = vmw_cotable_notify(res, cmd->defined_id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002709
2710 return ret;
2711}
2712
2713/**
2714 * vmw_cmd_dx_check_subresource - Validate an
2715 * SVGA_3D_CMD_DX_[X]_SUBRESOURCE command
2716 *
2717 * @dev_priv: Pointer to a device private struct.
2718 * @sw_context: The software context being used for this batch.
2719 * @header: Pointer to the command header in the command stream.
2720 */
2721static int vmw_cmd_dx_check_subresource(struct vmw_private *dev_priv,
2722 struct vmw_sw_context *sw_context,
2723 SVGA3dCmdHeader *header)
2724{
2725 struct {
2726 SVGA3dCmdHeader header;
2727 union {
2728 SVGA3dCmdDXReadbackSubResource r_body;
2729 SVGA3dCmdDXInvalidateSubResource i_body;
2730 SVGA3dCmdDXUpdateSubResource u_body;
2731 SVGA3dSurfaceId sid;
2732 };
2733 } *cmd;
2734
2735 BUILD_BUG_ON(offsetof(typeof(*cmd), r_body.sid) !=
2736 offsetof(typeof(*cmd), sid));
2737 BUILD_BUG_ON(offsetof(typeof(*cmd), i_body.sid) !=
2738 offsetof(typeof(*cmd), sid));
2739 BUILD_BUG_ON(offsetof(typeof(*cmd), u_body.sid) !=
2740 offsetof(typeof(*cmd), sid));
2741
2742 cmd = container_of(header, typeof(*cmd), header);
2743
2744 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002745 VMW_RES_DIRTY_NONE, user_surface_converter,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002746 &cmd->sid, NULL);
2747}
2748
2749static int vmw_cmd_dx_cid_check(struct vmw_private *dev_priv,
2750 struct vmw_sw_context *sw_context,
2751 SVGA3dCmdHeader *header)
2752{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002753 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002754
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002755 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002756 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002757
2758 return 0;
2759}
2760
2761/**
2762 * vmw_cmd_dx_view_remove - validate a view remove command and
2763 * schedule the view resource for removal.
2764 *
2765 * @dev_priv: Pointer to a device private struct.
2766 * @sw_context: The software context being used for this batch.
2767 * @header: Pointer to the command header in the command stream.
2768 *
2769 * Check that the view exists, and if it was not created using this
Thomas Hellstroma1944032016-10-10 11:06:45 -07002770 * command batch, conditionally make this command a NOP.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002771 */
2772static int vmw_cmd_dx_view_remove(struct vmw_private *dev_priv,
2773 struct vmw_sw_context *sw_context,
2774 SVGA3dCmdHeader *header)
2775{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002776 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002777 struct {
2778 SVGA3dCmdHeader header;
2779 union vmw_view_destroy body;
2780 } *cmd = container_of(header, typeof(*cmd), header);
2781 enum vmw_view_type view_type = vmw_view_cmd_to_type(header->id);
2782 struct vmw_resource *view;
2783 int ret;
2784
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002785 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002786 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002787
2788 ret = vmw_view_remove(sw_context->man,
2789 cmd->body.view_id, view_type,
2790 &sw_context->staged_cmd_res,
2791 &view);
2792 if (ret || !view)
2793 return ret;
2794
2795 /*
Thomas Hellstroma1944032016-10-10 11:06:45 -07002796 * If the view wasn't created during this command batch, it might
2797 * have been removed due to a context swapout, so add a
2798 * relocation to conditionally make this command a NOP to avoid
2799 * device errors.
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002800 */
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02002801 return vmw_resource_relocation_add(sw_context,
Thomas Hellstroma1944032016-10-10 11:06:45 -07002802 view,
2803 vmw_ptr_diff(sw_context->buf_start,
2804 &cmd->header.id),
2805 vmw_res_rel_cond_nop);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002806}
2807
2808/**
2809 * vmw_cmd_dx_define_shader - Validate an SVGA_3D_CMD_DX_DEFINE_SHADER
2810 * command
2811 *
2812 * @dev_priv: Pointer to a device private struct.
2813 * @sw_context: The software context being used for this batch.
2814 * @header: Pointer to the command header in the command stream.
2815 */
2816static int vmw_cmd_dx_define_shader(struct vmw_private *dev_priv,
2817 struct vmw_sw_context *sw_context,
2818 SVGA3dCmdHeader *header)
2819{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002820 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002821 struct vmw_resource *res;
2822 struct {
2823 SVGA3dCmdHeader header;
2824 SVGA3dCmdDXDefineShader body;
2825 } *cmd = container_of(header, typeof(*cmd), header);
2826 int ret;
2827
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002828 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002829 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002830
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002831 res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXSHADER);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002832 ret = vmw_cotable_notify(res, cmd->body.shaderId);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002833 if (ret)
2834 return ret;
2835
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002836 return vmw_dx_shader_add(sw_context->man, ctx_node->ctx,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002837 cmd->body.shaderId, cmd->body.type,
2838 &sw_context->staged_cmd_res);
2839}
2840
2841/**
2842 * vmw_cmd_dx_destroy_shader - Validate an SVGA_3D_CMD_DX_DESTROY_SHADER
2843 * command
2844 *
2845 * @dev_priv: Pointer to a device private struct.
2846 * @sw_context: The software context being used for this batch.
2847 * @header: Pointer to the command header in the command stream.
2848 */
2849static int vmw_cmd_dx_destroy_shader(struct vmw_private *dev_priv,
2850 struct vmw_sw_context *sw_context,
2851 SVGA3dCmdHeader *header)
2852{
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002853 struct vmw_ctx_validation_info *ctx_node = VMW_GET_CTX_NODE(sw_context);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002854 struct {
2855 SVGA3dCmdHeader header;
2856 SVGA3dCmdDXDestroyShader body;
2857 } *cmd = container_of(header, typeof(*cmd), header);
2858 int ret;
2859
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002860 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002861 return -EINVAL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002862
2863 ret = vmw_shader_remove(sw_context->man, cmd->body.shaderId, 0,
2864 &sw_context->staged_cmd_res);
2865 if (ret)
2866 DRM_ERROR("Could not find shader to remove.\n");
2867
2868 return ret;
2869}
2870
2871/**
2872 * vmw_cmd_dx_bind_shader - Validate an SVGA_3D_CMD_DX_BIND_SHADER
2873 * command
2874 *
2875 * @dev_priv: Pointer to a device private struct.
2876 * @sw_context: The software context being used for this batch.
2877 * @header: Pointer to the command header in the command stream.
2878 */
2879static int vmw_cmd_dx_bind_shader(struct vmw_private *dev_priv,
2880 struct vmw_sw_context *sw_context,
2881 SVGA3dCmdHeader *header)
2882{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002883 struct vmw_resource *ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002884 struct vmw_resource *res;
2885 struct {
2886 SVGA3dCmdHeader header;
2887 SVGA3dCmdDXBindShader body;
2888 } *cmd = container_of(header, typeof(*cmd), header);
2889 int ret;
2890
2891 if (cmd->body.cid != SVGA3D_INVALID_ID) {
2892 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002893 VMW_RES_DIRTY_SET,
2894 user_context_converter, &cmd->body.cid,
2895 &ctx);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002896 if (ret)
2897 return ret;
2898 } else {
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002899 struct vmw_ctx_validation_info *ctx_node =
2900 VMW_GET_CTX_NODE(sw_context);
2901
2902 if (!ctx_node)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002903 return -EINVAL;
Deepak Rawat6f74fd92019-02-08 12:53:57 -08002904
2905 ctx = ctx_node->ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002906 }
2907
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02002908 res = vmw_shader_lookup(vmw_context_res_man(ctx),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002909 cmd->body.shid, 0);
2910 if (IS_ERR(res)) {
2911 DRM_ERROR("Could not find shader to bind.\n");
2912 return PTR_ERR(res);
2913 }
2914
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002915 ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
2916 VMW_RES_DIRTY_NONE);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002917 if (ret) {
2918 DRM_ERROR("Error creating resource validation node.\n");
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002919 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002920 }
2921
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002922 return vmw_cmd_res_switch_backup(dev_priv, sw_context, res,
2923 &cmd->body.mobid,
2924 cmd->body.offsetInBytes);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002925}
2926
Charmaine Leef3b335502016-02-12 08:11:56 +01002927/**
2928 * vmw_cmd_dx_genmips - Validate an SVGA_3D_CMD_DX_GENMIPS command
2929 *
2930 * @dev_priv: Pointer to a device private struct.
2931 * @sw_context: The software context being used for this batch.
2932 * @header: Pointer to the command header in the command stream.
2933 */
2934static int vmw_cmd_dx_genmips(struct vmw_private *dev_priv,
2935 struct vmw_sw_context *sw_context,
2936 SVGA3dCmdHeader *header)
2937{
2938 struct {
2939 SVGA3dCmdHeader header;
2940 SVGA3dCmdDXGenMips body;
2941 } *cmd = container_of(header, typeof(*cmd), header);
2942
Thomas Hellstrom508108e2018-09-26 16:28:45 +02002943 return PTR_RET(vmw_view_id_val_add(sw_context, vmw_view_sr,
2944 cmd->body.shaderResourceViewId));
Charmaine Leef3b335502016-02-12 08:11:56 +01002945}
2946
Charmaine Lee1f982e42016-10-10 10:37:03 -07002947/**
2948 * vmw_cmd_dx_transfer_from_buffer -
2949 * Validate an SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER command
2950 *
2951 * @dev_priv: Pointer to a device private struct.
2952 * @sw_context: The software context being used for this batch.
2953 * @header: Pointer to the command header in the command stream.
2954 */
2955static int vmw_cmd_dx_transfer_from_buffer(struct vmw_private *dev_priv,
2956 struct vmw_sw_context *sw_context,
2957 SVGA3dCmdHeader *header)
2958{
2959 struct {
2960 SVGA3dCmdHeader header;
2961 SVGA3dCmdDXTransferFromBuffer body;
2962 } *cmd = container_of(header, typeof(*cmd), header);
2963 int ret;
2964
2965 ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002966 VMW_RES_DIRTY_NONE, user_surface_converter,
Charmaine Lee1f982e42016-10-10 10:37:03 -07002967 &cmd->body.srcSid, NULL);
2968 if (ret != 0)
2969 return ret;
2970
2971 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002972 VMW_RES_DIRTY_SET, user_surface_converter,
Charmaine Lee1f982e42016-10-10 10:37:03 -07002973 &cmd->body.destSid, NULL);
2974}
2975
Neha Bhende0d81d342018-06-18 17:14:56 -07002976/**
2977 * vmw_cmd_intra_surface_copy -
2978 * Validate an SVGA_3D_CMD_INTRA_SURFACE_COPY command
2979 *
2980 * @dev_priv: Pointer to a device private struct.
2981 * @sw_context: The software context being used for this batch.
2982 * @header: Pointer to the command header in the command stream.
2983 */
2984static int vmw_cmd_intra_surface_copy(struct vmw_private *dev_priv,
2985 struct vmw_sw_context *sw_context,
2986 SVGA3dCmdHeader *header)
2987{
2988 struct {
2989 SVGA3dCmdHeader header;
2990 SVGA3dCmdIntraSurfaceCopy body;
2991 } *cmd = container_of(header, typeof(*cmd), header);
2992
2993 if (!(dev_priv->capabilities2 & SVGA_CAP2_INTRA_SURFACE_COPY))
2994 return -EINVAL;
2995
2996 return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002997 VMW_RES_DIRTY_SET, user_surface_converter,
2998 &cmd->body.surface.sid, NULL);
Neha Bhende0d81d342018-06-18 17:14:56 -07002999}
3000
3001
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003002static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
3003 struct vmw_sw_context *sw_context,
3004 void *buf, uint32_t *size)
3005{
3006 uint32_t size_remaining = *size;
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003007 uint32_t cmd_id;
3008
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003009 cmd_id = ((uint32_t *)buf)[0];
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003010 switch (cmd_id) {
3011 case SVGA_CMD_UPDATE:
3012 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003013 break;
3014 case SVGA_CMD_DEFINE_GMRFB:
3015 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
3016 break;
3017 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3018 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3019 break;
3020 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3021 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3022 break;
3023 default:
3024 DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
3025 return -EINVAL;
3026 }
3027
3028 if (*size > size_remaining) {
3029 DRM_ERROR("Invalid SVGA command (size mismatch):"
3030 " %u.\n", cmd_id);
3031 return -EINVAL;
3032 }
3033
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +02003034 if (unlikely(!sw_context->kernel)) {
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003035 DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
3036 return -EPERM;
3037 }
3038
3039 if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
3040 return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
3041
3042 return 0;
3043}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003044
Thomas Hellstrom4fbd9d22014-02-12 12:37:01 +01003045static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003046 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
3047 false, false, false),
3048 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
3049 false, false, false),
3050 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
3051 true, false, false),
3052 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
3053 true, false, false),
3054 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
3055 true, false, false),
3056 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
3057 false, false, false),
3058 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
3059 false, false, false),
3060 VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
3061 true, false, false),
3062 VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
3063 true, false, false),
3064 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
3065 true, false, false),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003066 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003067 &vmw_cmd_set_render_target_check, true, false, false),
3068 VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
3069 true, false, false),
3070 VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
3071 true, false, false),
3072 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
3073 true, false, false),
3074 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
3075 true, false, false),
3076 VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
3077 true, false, false),
3078 VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
3079 true, false, false),
3080 VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
3081 true, false, false),
3082 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
3083 false, false, false),
Thomas Hellstromd5bde952014-01-31 10:12:10 +01003084 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_shader_define,
3085 true, false, false),
3086 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_shader_destroy,
3087 true, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003088 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
3089 true, false, false),
Thomas Hellstrom0ccbbae2014-01-30 11:13:43 +01003090 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_set_shader_const,
3091 true, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003092 VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
3093 true, false, false),
3094 VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
3095 true, false, false),
3096 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
3097 true, false, false),
3098 VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
3099 true, false, false),
3100 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
3101 true, false, false),
3102 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
3103 true, false, false),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003104 VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003105 &vmw_cmd_blt_surf_screen_check, false, false, false),
3106 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
3107 false, false, false),
3108 VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
3109 false, false, false),
3110 VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
3111 false, false, false),
3112 VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
3113 false, false, false),
3114 VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
3115 false, false, false),
Deepak Rawatdc75e732018-06-13 13:53:28 -07003116 VMW_CMD_DEF(SVGA_3D_CMD_DEAD1, &vmw_cmd_invalid,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003117 false, false, false),
Deepak Rawatdc75e732018-06-13 13:53:28 -07003118 VMW_CMD_DEF(SVGA_3D_CMD_DEAD2, &vmw_cmd_invalid,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003119 false, false, false),
3120 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_BITBLT, &vmw_cmd_invalid,
3121 false, false, false),
3122 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_TRANSBLT, &vmw_cmd_invalid,
3123 false, false, false),
3124 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_STRETCHBLT, &vmw_cmd_invalid,
3125 false, false, false),
3126 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_COLORFILL, &vmw_cmd_invalid,
3127 false, false, false),
3128 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_ALPHABLEND, &vmw_cmd_invalid,
3129 false, false, false),
3130 VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_CLEARTYPEBLEND, &vmw_cmd_invalid,
3131 false, false, false),
3132 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
3133 false, false, true),
3134 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
3135 false, false, true),
3136 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
3137 false, false, true),
3138 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
3139 false, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003140 VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB64, &vmw_cmd_invalid,
3141 false, false, true),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003142 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
3143 false, false, true),
3144 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
3145 false, false, true),
3146 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
3147 false, false, true),
3148 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
3149 true, false, true),
3150 VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
3151 false, false, true),
3152 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
3153 true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003154 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003155 &vmw_cmd_update_gb_surface, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003156 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003157 &vmw_cmd_readback_gb_image, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003158 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003159 &vmw_cmd_readback_gb_surface, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003160 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003161 &vmw_cmd_invalidate_gb_image, true, false, true),
Thomas Hellstroma97e2192012-11-21 11:45:13 +01003162 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003163 &vmw_cmd_invalidate_gb_surface, true, false, true),
3164 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
3165 false, false, true),
3166 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
3167 false, false, true),
3168 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
3169 false, false, true),
3170 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
3171 false, false, true),
3172 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
3173 false, false, true),
3174 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
3175 false, false, true),
3176 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
3177 true, false, true),
3178 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
3179 false, false, true),
Thomas Hellstromf2a0dcb2014-01-15 10:04:07 +01003180 VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
Thomas Hellstrom8ba07312013-10-08 02:25:35 -07003181 false, false, false),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003182 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
3183 true, false, true),
3184 VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
3185 true, false, true),
3186 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
3187 true, false, true),
3188 VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
3189 true, false, true),
Thomas Hellstrom5f55be5f2017-08-24 08:06:30 +02003190 VMW_CMD_DEF(SVGA_3D_CMD_NOP_ERROR, &vmw_cmd_ok,
3191 true, false, true),
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003192 VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
3193 false, false, true),
3194 VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
3195 false, false, true),
3196 VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
3197 false, false, true),
3198 VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
3199 false, false, true),
3200 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
3201 false, false, true),
3202 VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
3203 false, false, true),
3204 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
3205 false, false, true),
3206 VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
3207 false, false, true),
3208 VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3209 false, false, true),
3210 VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
3211 false, false, true),
3212 VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003213 true, false, true),
3214 VMW_CMD_DEF(SVGA_3D_CMD_GB_SCREEN_DMA, &vmw_cmd_invalid,
3215 false, false, true),
3216 VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE_WITH_PITCH, &vmw_cmd_invalid,
3217 false, false, true),
3218 VMW_CMD_DEF(SVGA_3D_CMD_GB_MOB_FENCE, &vmw_cmd_invalid,
3219 false, false, true),
3220 VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V2, &vmw_cmd_invalid,
3221 false, false, true),
3222
3223 /*
3224 * DX commands
3225 */
3226 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_CONTEXT, &vmw_cmd_invalid,
3227 false, false, true),
3228 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_CONTEXT, &vmw_cmd_invalid,
3229 false, false, true),
3230 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_CONTEXT, &vmw_cmd_invalid,
3231 false, false, true),
3232 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_CONTEXT, &vmw_cmd_invalid,
3233 false, false, true),
3234 VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_CONTEXT, &vmw_cmd_invalid,
3235 false, false, true),
3236 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER,
3237 &vmw_cmd_dx_set_single_constant_buffer, true, false, true),
3238 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER_RESOURCES,
3239 &vmw_cmd_dx_set_shader_res, true, false, true),
3240 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER, &vmw_cmd_dx_set_shader,
3241 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003242 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SAMPLERS, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003243 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003244 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003245 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003246 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED, &vmw_cmd_dx_cid_check,
3247 true, false, true),
3248 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INSTANCED, &vmw_cmd_dx_cid_check,
3249 true, false, true),
3250 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED_INSTANCED,
3251 &vmw_cmd_dx_cid_check, true, false, true),
3252 VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_AUTO, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003253 true, false, true),
3254 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS,
3255 &vmw_cmd_dx_set_vertex_buffers, true, false, true),
3256 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INDEX_BUFFER,
3257 &vmw_cmd_dx_set_index_buffer, true, false, true),
3258 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RENDERTARGETS,
3259 &vmw_cmd_dx_set_rendertargets, true, false, true),
3260 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_BLEND_STATE, &vmw_cmd_dx_cid_check,
3261 true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003262 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_DEPTHSTENCIL_STATE,
Charmaine Lee2f633e52015-08-10 10:45:11 -07003263 &vmw_cmd_dx_cid_check, true, false, true),
3264 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RASTERIZER_STATE,
3265 &vmw_cmd_dx_cid_check, true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003266 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_QUERY, &vmw_cmd_dx_define_query,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003267 true, false, true),
Charmaine Leee02e5882016-04-12 08:19:08 -07003268 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003269 true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003270 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_QUERY, &vmw_cmd_dx_bind_query,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003271 true, false, true),
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003272 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_QUERY_OFFSET,
Charmaine Leee02e5882016-04-12 08:19:08 -07003273 &vmw_cmd_dx_cid_check, true, false, true),
3274 VMW_CMD_DEF(SVGA_3D_CMD_DX_BEGIN_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003275 true, false, true),
Charmaine Leee02e5882016-04-12 08:19:08 -07003276 VMW_CMD_DEF(SVGA_3D_CMD_DX_END_QUERY, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003277 true, false, true),
3278 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_QUERY, &vmw_cmd_invalid,
3279 true, false, true),
Charmaine Lee18835982016-04-12 08:14:23 -07003280 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_PREDICATION, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003281 true, false, true),
3282 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VIEWPORTS, &vmw_cmd_dx_cid_check,
3283 true, false, true),
3284 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SCISSORRECTS, &vmw_cmd_dx_cid_check,
3285 true, false, true),
3286 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW,
3287 &vmw_cmd_dx_clear_rendertarget_view, true, false, true),
3288 VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW,
3289 &vmw_cmd_dx_clear_depthstencil_view, true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003290 VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY, &vmw_cmd_invalid,
3291 true, false, true),
Charmaine Leef3b335502016-02-12 08:11:56 +01003292 VMW_CMD_DEF(SVGA_3D_CMD_DX_GENMIPS, &vmw_cmd_dx_genmips,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003293 true, false, true),
3294 VMW_CMD_DEF(SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE,
3295 &vmw_cmd_dx_check_subresource, true, false, true),
3296 VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_SUBRESOURCE,
3297 &vmw_cmd_dx_check_subresource, true, false, true),
3298 VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_SUBRESOURCE,
3299 &vmw_cmd_dx_check_subresource, true, false, true),
3300 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW,
3301 &vmw_cmd_dx_view_define, true, false, true),
3302 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
3303 &vmw_cmd_dx_view_remove, true, false, true),
3304 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW,
3305 &vmw_cmd_dx_view_define, true, false, true),
3306 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
3307 &vmw_cmd_dx_view_remove, true, false, true),
3308 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW,
3309 &vmw_cmd_dx_view_define, true, false, true),
3310 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
3311 &vmw_cmd_dx_view_remove, true, false, true),
3312 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_ELEMENTLAYOUT,
3313 &vmw_cmd_dx_so_define, true, false, true),
3314 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_ELEMENTLAYOUT,
3315 &vmw_cmd_dx_cid_check, true, false, true),
3316 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_BLEND_STATE,
3317 &vmw_cmd_dx_so_define, true, false, true),
3318 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_BLEND_STATE,
3319 &vmw_cmd_dx_cid_check, true, false, true),
3320 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_STATE,
3321 &vmw_cmd_dx_so_define, true, false, true),
3322 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_STATE,
3323 &vmw_cmd_dx_cid_check, true, false, true),
3324 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RASTERIZER_STATE,
3325 &vmw_cmd_dx_so_define, true, false, true),
3326 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RASTERIZER_STATE,
3327 &vmw_cmd_dx_cid_check, true, false, true),
3328 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SAMPLER_STATE,
3329 &vmw_cmd_dx_so_define, true, false, true),
3330 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SAMPLER_STATE,
3331 &vmw_cmd_dx_cid_check, true, false, true),
3332 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADER,
3333 &vmw_cmd_dx_define_shader, true, false, true),
3334 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADER,
3335 &vmw_cmd_dx_destroy_shader, true, false, true),
3336 VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_SHADER,
3337 &vmw_cmd_dx_bind_shader, true, false, true),
3338 VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_STREAMOUTPUT,
3339 &vmw_cmd_dx_so_define, true, false, true),
3340 VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_STREAMOUTPUT,
3341 &vmw_cmd_dx_cid_check, true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003342 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_STREAMOUTPUT, &vmw_cmd_dx_cid_check,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003343 true, false, true),
Charmaine Lee2f633e52015-08-10 10:45:11 -07003344 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SOTARGETS,
3345 &vmw_cmd_dx_set_so_targets, true, false, true),
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003346 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INPUT_LAYOUT,
3347 &vmw_cmd_dx_cid_check, true, false, true),
3348 VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_TOPOLOGY,
3349 &vmw_cmd_dx_cid_check, true, false, true),
Neha Bhende0fca749e2015-08-10 10:51:07 -07003350 VMW_CMD_DEF(SVGA_3D_CMD_DX_BUFFER_COPY,
3351 &vmw_cmd_buffer_copy_check, true, false, true),
3352 VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY_REGION,
3353 &vmw_cmd_pred_copy_check, true, false, true),
Charmaine Lee1f982e42016-10-10 10:37:03 -07003354 VMW_CMD_DEF(SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER,
3355 &vmw_cmd_dx_transfer_from_buffer,
3356 true, false, true),
Neha Bhende0d81d342018-06-18 17:14:56 -07003357 VMW_CMD_DEF(SVGA_3D_CMD_INTRA_SURFACE_COPY, &vmw_cmd_intra_surface_copy,
3358 true, false, true),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003359};
3360
Thomas Hellstrom65b97a22017-08-24 08:06:29 +02003361bool vmw_cmd_describe(const void *buf, u32 *size, char const **cmd)
3362{
3363 u32 cmd_id = ((u32 *) buf)[0];
3364
3365 if (cmd_id >= SVGA_CMD_MAX) {
3366 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3367 const struct vmw_cmd_entry *entry;
3368
3369 *size = header->size + sizeof(SVGA3dCmdHeader);
3370 cmd_id = header->id;
3371 if (cmd_id >= SVGA_3D_CMD_MAX)
3372 return false;
3373
3374 cmd_id -= SVGA_3D_CMD_BASE;
3375 entry = &vmw_cmd_entries[cmd_id];
3376 *cmd = entry->cmd_name;
3377 return true;
3378 }
3379
3380 switch (cmd_id) {
3381 case SVGA_CMD_UPDATE:
3382 *cmd = "SVGA_CMD_UPDATE";
3383 *size = sizeof(u32) + sizeof(SVGAFifoCmdUpdate);
3384 break;
3385 case SVGA_CMD_DEFINE_GMRFB:
3386 *cmd = "SVGA_CMD_DEFINE_GMRFB";
3387 *size = sizeof(u32) + sizeof(SVGAFifoCmdDefineGMRFB);
3388 break;
3389 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3390 *cmd = "SVGA_CMD_BLIT_GMRFB_TO_SCREEN";
3391 *size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3392 break;
3393 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3394 *cmd = "SVGA_CMD_BLIT_SCREEN_TO_GMRFB";
3395 *size = sizeof(u32) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
3396 break;
3397 default:
3398 *cmd = "UNKNOWN";
3399 *size = 0;
3400 return false;
3401 }
3402
3403 return true;
3404}
3405
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003406static int vmw_cmd_check(struct vmw_private *dev_priv,
3407 struct vmw_sw_context *sw_context,
3408 void *buf, uint32_t *size)
3409{
3410 uint32_t cmd_id;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003411 uint32_t size_remaining = *size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003412 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
3413 int ret;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003414 const struct vmw_cmd_entry *entry;
3415 bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003416
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003417 cmd_id = ((uint32_t *)buf)[0];
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +02003418 /* Handle any none 3D commands */
3419 if (unlikely(cmd_id < SVGA_CMD_MAX))
3420 return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
3421
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003422
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003423 cmd_id = header->id;
3424 *size = header->size + sizeof(SVGA3dCmdHeader);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003425
3426 cmd_id -= SVGA_3D_CMD_BASE;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003427 if (unlikely(*size > size_remaining))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003428 goto out_invalid;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003429
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003430 if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003431 goto out_invalid;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003432
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003433 entry = &vmw_cmd_entries[cmd_id];
Thomas Hellstrom36e952c2014-02-12 13:19:36 +01003434 if (unlikely(!entry->func))
3435 goto out_invalid;
3436
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003437 if (unlikely(!entry->user_allow && !sw_context->kernel))
3438 goto out_privileged;
3439
3440 if (unlikely(entry->gb_disable && gb))
3441 goto out_old;
3442
3443 if (unlikely(entry->gb_enable && !gb))
3444 goto out_new;
3445
3446 ret = entry->func(dev_priv, sw_context, header);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003447 if (unlikely(ret != 0))
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003448 goto out_invalid;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003449
3450 return 0;
Thomas Hellstromc373d4e2012-11-21 12:22:35 +01003451out_invalid:
3452 DRM_ERROR("Invalid SVGA3D command: %d\n",
3453 cmd_id + SVGA_3D_CMD_BASE);
3454 return -EINVAL;
3455out_privileged:
3456 DRM_ERROR("Privileged SVGA3D command: %d\n",
3457 cmd_id + SVGA_3D_CMD_BASE);
3458 return -EPERM;
3459out_old:
3460 DRM_ERROR("Deprecated (disallowed) SVGA3D command: %d\n",
3461 cmd_id + SVGA_3D_CMD_BASE);
3462 return -EINVAL;
3463out_new:
3464 DRM_ERROR("SVGA3D command: %d not supported by virtual hardware.\n",
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003465 cmd_id + SVGA_3D_CMD_BASE);
3466 return -EINVAL;
3467}
3468
3469static int vmw_cmd_check_all(struct vmw_private *dev_priv,
3470 struct vmw_sw_context *sw_context,
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003471 void *buf,
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003472 uint32_t size)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003473{
3474 int32_t cur_size = size;
3475 int ret;
3476
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003477 sw_context->buf_start = buf;
3478
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003479 while (cur_size > 0) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01003480 size = cur_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003481 ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
3482 if (unlikely(ret != 0))
3483 return ret;
3484 buf = (void *)((unsigned long) buf + size);
3485 cur_size -= size;
3486 }
3487
3488 if (unlikely(cur_size != 0)) {
3489 DRM_ERROR("Command verifier out of sync.\n");
3490 return -EINVAL;
3491 }
3492
3493 return 0;
3494}
3495
3496static void vmw_free_relocations(struct vmw_sw_context *sw_context)
3497{
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003498 /* Memory is validation context memory, so no need to free it */
3499
3500 INIT_LIST_HEAD(&sw_context->bo_relocations);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003501}
3502
3503static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
3504{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003505 struct vmw_relocation *reloc;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003506 struct ttm_buffer_object *bo;
3507
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003508 list_for_each_entry(reloc, &sw_context->bo_relocations, head) {
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003509 bo = &reloc->vbo->base;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003510 switch (bo->mem.mem_type) {
3511 case TTM_PL_VRAM:
Thomas Hellstrom135cba02010-10-26 21:21:47 +02003512 reloc->location->offset += bo->offset;
3513 reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003514 break;
3515 case VMW_PL_GMR:
Thomas Hellstrom135cba02010-10-26 21:21:47 +02003516 reloc->location->gmrId = bo->mem.start;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003517 break;
Thomas Hellstromddcda242012-11-21 11:26:55 +01003518 case VMW_PL_MOB:
3519 *reloc->mob_loc = bo->mem.start;
3520 break;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003521 default:
3522 BUG();
3523 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003524 }
3525 vmw_free_relocations(sw_context);
3526}
3527
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003528static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
3529 uint32_t size)
3530{
3531 if (likely(sw_context->cmd_bounce_size >= size))
3532 return 0;
3533
3534 if (sw_context->cmd_bounce_size == 0)
3535 sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
3536
3537 while (sw_context->cmd_bounce_size < size) {
3538 sw_context->cmd_bounce_size =
3539 PAGE_ALIGN(sw_context->cmd_bounce_size +
3540 (sw_context->cmd_bounce_size >> 1));
3541 }
3542
Markus Elfring0bc32992016-07-22 13:31:00 +02003543 vfree(sw_context->cmd_bounce);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003544 sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
3545
3546 if (sw_context->cmd_bounce == NULL) {
3547 DRM_ERROR("Failed to allocate command bounce buffer.\n");
3548 sw_context->cmd_bounce_size = 0;
3549 return -ENOMEM;
3550 }
3551
3552 return 0;
3553}
3554
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003555/**
3556 * vmw_execbuf_fence_commands - create and submit a command stream fence
3557 *
3558 * Creates a fence object and submits a command stream marker.
3559 * If this fails for some reason, We sync the fifo and return NULL.
3560 * It is then safe to fence buffers with a NULL pointer.
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +02003561 *
3562 * If @p_handle is not NULL @file_priv must also not be NULL. Creates
3563 * a userspace handle if @p_handle is not NULL, otherwise not.
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003564 */
3565
3566int vmw_execbuf_fence_commands(struct drm_file *file_priv,
3567 struct vmw_private *dev_priv,
3568 struct vmw_fence_obj **p_fence,
3569 uint32_t *p_handle)
3570{
3571 uint32_t sequence;
3572 int ret;
3573 bool synced = false;
3574
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +02003575 /* p_handle implies file_priv. */
3576 BUG_ON(p_handle != NULL && file_priv == NULL);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003577
3578 ret = vmw_fifo_send_fence(dev_priv, &sequence);
3579 if (unlikely(ret != 0)) {
3580 DRM_ERROR("Fence submission error. Syncing.\n");
3581 synced = true;
3582 }
3583
3584 if (p_handle != NULL)
3585 ret = vmw_user_fence_create(file_priv, dev_priv->fman,
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003586 sequence, p_fence, p_handle);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003587 else
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003588 ret = vmw_fence_create(dev_priv->fman, sequence, p_fence);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003589
3590 if (unlikely(ret != 0 && !synced)) {
3591 (void) vmw_fallback_wait(dev_priv, false, false,
3592 sequence, false,
3593 VMW_FENCE_WAIT_TIMEOUT);
3594 *p_fence = NULL;
3595 }
3596
Thomas Hellstrom728354c2019-01-31 10:55:37 +01003597 return ret;
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003598}
3599
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003600/**
3601 * vmw_execbuf_copy_fence_user - copy fence object information to
3602 * user-space.
3603 *
3604 * @dev_priv: Pointer to a vmw_private struct.
3605 * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
3606 * @ret: Return value from fence object creation.
3607 * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
3608 * which the information should be copied.
3609 * @fence: Pointer to the fenc object.
3610 * @fence_handle: User-space fence handle.
Sinclair Yehc906965d2017-07-05 01:49:32 -07003611 * @out_fence_fd: exported file descriptor for the fence. -1 if not used
3612 * @sync_file: Only used to clean up in case of an error in this function.
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003613 *
3614 * This function copies fence information to user-space. If copying fails,
3615 * The user-space struct drm_vmw_fence_rep::error member is hopefully
3616 * left untouched, and if it's preloaded with an -EFAULT by user-space,
3617 * the error will hopefully be detected.
3618 * Also if copying fails, user-space will be unable to signal the fence
3619 * object so we wait for it immediately, and then unreference the
3620 * user-space reference.
3621 */
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02003622void
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003623vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
3624 struct vmw_fpriv *vmw_fp,
3625 int ret,
3626 struct drm_vmw_fence_rep __user *user_fence_rep,
3627 struct vmw_fence_obj *fence,
Sinclair Yehc906965d2017-07-05 01:49:32 -07003628 uint32_t fence_handle,
3629 int32_t out_fence_fd,
3630 struct sync_file *sync_file)
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003631{
3632 struct drm_vmw_fence_rep fence_rep;
3633
3634 if (user_fence_rep == NULL)
3635 return;
3636
Dan Carpenter80d9b242011-10-18 09:10:12 +03003637 memset(&fence_rep, 0, sizeof(fence_rep));
3638
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003639 fence_rep.error = ret;
Sinclair Yehc906965d2017-07-05 01:49:32 -07003640 fence_rep.fd = out_fence_fd;
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003641 if (ret == 0) {
3642 BUG_ON(fence == NULL);
3643
3644 fence_rep.handle = fence_handle;
Maarten Lankhorst2298e802014-03-26 14:07:44 +01003645 fence_rep.seqno = fence->base.seqno;
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003646 vmw_update_seqno(dev_priv, &dev_priv->fifo);
3647 fence_rep.passed_seqno = dev_priv->last_read_seqno;
3648 }
3649
3650 /*
3651 * copy_to_user errors will be detected by user space not
3652 * seeing fence_rep::error filled in. Typically
3653 * user-space would have pre-set that member to -EFAULT.
3654 */
3655 ret = copy_to_user(user_fence_rep, &fence_rep,
3656 sizeof(fence_rep));
3657
3658 /*
3659 * User-space lost the fence object. We need to sync
3660 * and unreference the handle.
3661 */
3662 if (unlikely(ret != 0) && (fence_rep.error == 0)) {
Sinclair Yehc906965d2017-07-05 01:49:32 -07003663 if (sync_file)
3664 fput(sync_file->file);
3665
3666 if (fence_rep.fd != -1) {
3667 put_unused_fd(fence_rep.fd);
3668 fence_rep.fd = -1;
3669 }
3670
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003671 ttm_ref_object_base_unref(vmw_fp->tfile,
3672 fence_handle, TTM_REF_USAGE);
3673 DRM_ERROR("Fence copy error. Syncing.\n");
Maarten Lankhorstc060a4e2014-03-26 13:06:24 +01003674 (void) vmw_fence_obj_wait(fence, false, false,
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02003675 VMW_FENCE_WAIT_TIMEOUT);
3676 }
3677}
3678
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003679/**
3680 * vmw_execbuf_submit_fifo - Patch a command batch and submit it using
3681 * the fifo.
3682 *
3683 * @dev_priv: Pointer to a device private structure.
3684 * @kernel_commands: Pointer to the unpatched command batch.
3685 * @command_size: Size of the unpatched command batch.
3686 * @sw_context: Structure holding the relocation lists.
3687 *
3688 * Side effects: If this function returns 0, then the command batch
3689 * pointed to by @kernel_commands will have been modified.
3690 */
3691static int vmw_execbuf_submit_fifo(struct vmw_private *dev_priv,
3692 void *kernel_commands,
3693 u32 command_size,
3694 struct vmw_sw_context *sw_context)
3695{
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003696 void *cmd;
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003697
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003698 if (sw_context->dx_ctx_node)
3699 cmd = vmw_fifo_reserve_dx(dev_priv, command_size,
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003700 sw_context->dx_ctx_node->ctx->id);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003701 else
3702 cmd = vmw_fifo_reserve(dev_priv, command_size);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003703 if (!cmd) {
3704 DRM_ERROR("Failed reserving fifo space for commands.\n");
3705 return -ENOMEM;
3706 }
3707
3708 vmw_apply_relocations(sw_context);
3709 memcpy(cmd, kernel_commands, command_size);
3710 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
3711 vmw_resource_relocations_free(&sw_context->res_relocations);
3712 vmw_fifo_commit(dev_priv, command_size);
3713
3714 return 0;
3715}
3716
3717/**
3718 * vmw_execbuf_submit_cmdbuf - Patch a command batch and submit it using
3719 * the command buffer manager.
3720 *
3721 * @dev_priv: Pointer to a device private structure.
3722 * @header: Opaque handle to the command buffer allocation.
3723 * @command_size: Size of the unpatched command batch.
3724 * @sw_context: Structure holding the relocation lists.
3725 *
3726 * Side effects: If this function returns 0, then the command buffer
3727 * represented by @header will have been modified.
3728 */
3729static int vmw_execbuf_submit_cmdbuf(struct vmw_private *dev_priv,
3730 struct vmw_cmdbuf_header *header,
3731 u32 command_size,
3732 struct vmw_sw_context *sw_context)
3733{
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003734 u32 id = ((sw_context->dx_ctx_node) ? sw_context->dx_ctx_node->ctx->id :
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003735 SVGA3D_INVALID_ID);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003736 void *cmd = vmw_cmdbuf_reserve(dev_priv->cman, command_size,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003737 id, false, header);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003738
3739 vmw_apply_relocations(sw_context);
3740 vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
3741 vmw_resource_relocations_free(&sw_context->res_relocations);
3742 vmw_cmdbuf_commit(dev_priv->cman, command_size, header, false);
3743
3744 return 0;
3745}
3746
3747/**
3748 * vmw_execbuf_cmdbuf - Prepare, if possible, a user-space command batch for
3749 * submission using a command buffer.
3750 *
3751 * @dev_priv: Pointer to a device private structure.
3752 * @user_commands: User-space pointer to the commands to be submitted.
3753 * @command_size: Size of the unpatched command batch.
3754 * @header: Out parameter returning the opaque pointer to the command buffer.
3755 *
3756 * This function checks whether we can use the command buffer manager for
3757 * submission and if so, creates a command buffer of suitable size and
3758 * copies the user data into that buffer.
3759 *
3760 * On successful return, the function returns a pointer to the data in the
3761 * command buffer and *@header is set to non-NULL.
3762 * If command buffers could not be used, the function will return the value
3763 * of @kernel_commands on function call. That value may be NULL. In that case,
3764 * the value of *@header will be set to NULL.
3765 * If an error is encountered, the function will return a pointer error value.
3766 * If the function is interrupted by a signal while sleeping, it will return
3767 * -ERESTARTSYS casted to a pointer error value.
3768 */
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07003769static void *vmw_execbuf_cmdbuf(struct vmw_private *dev_priv,
3770 void __user *user_commands,
3771 void *kernel_commands,
3772 u32 command_size,
3773 struct vmw_cmdbuf_header **header)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003774{
3775 size_t cmdbuf_size;
3776 int ret;
3777
3778 *header = NULL;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003779 if (command_size > SVGA_CB_MAX_SIZE) {
3780 DRM_ERROR("Command buffer is too large.\n");
3781 return ERR_PTR(-EINVAL);
3782 }
3783
Thomas Hellstrom51ab70b2016-10-10 10:51:24 -07003784 if (!dev_priv->cman || kernel_commands)
3785 return kernel_commands;
3786
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003787 /* If possible, add a little space for fencing. */
3788 cmdbuf_size = command_size + 512;
3789 cmdbuf_size = min_t(size_t, cmdbuf_size, SVGA_CB_MAX_SIZE);
3790 kernel_commands = vmw_cmdbuf_alloc(dev_priv->cman, cmdbuf_size,
3791 true, header);
3792 if (IS_ERR(kernel_commands))
3793 return kernel_commands;
3794
3795 ret = copy_from_user(kernel_commands, user_commands,
3796 command_size);
3797 if (ret) {
3798 DRM_ERROR("Failed copying commands.\n");
3799 vmw_cmdbuf_header_free(*header);
3800 *header = NULL;
3801 return ERR_PTR(-EFAULT);
3802 }
3803
3804 return kernel_commands;
3805}
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003806
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003807static int vmw_execbuf_tie_context(struct vmw_private *dev_priv,
3808 struct vmw_sw_context *sw_context,
3809 uint32_t handle)
3810{
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003811 struct vmw_resource *res;
3812 int ret;
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003813 unsigned int size;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003814
3815 if (handle == SVGA3D_INVALID_ID)
3816 return 0;
3817
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003818 size = vmw_execbuf_res_size(dev_priv, vmw_res_dx_context);
3819 ret = vmw_validation_preload_res(sw_context->ctx, size);
3820 if (ret)
3821 return ret;
3822
3823 res = vmw_user_resource_noref_lookup_handle
3824 (dev_priv, sw_context->fp->tfile, handle,
3825 user_context_converter);
Chengguang Xu4efa6662019-03-01 10:14:06 -08003826 if (IS_ERR(res)) {
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003827 DRM_ERROR("Could not find or user DX context 0x%08x.\n",
3828 (unsigned) handle);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003829 return PTR_ERR(res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003830 }
3831
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01003832 ret = vmw_execbuf_res_noref_val_add(sw_context, res, VMW_RES_DIRTY_SET);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003833 if (unlikely(ret != 0))
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003834 return ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003835
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003836 sw_context->dx_ctx_node = vmw_execbuf_info_from_res(sw_context, res);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003837 sw_context->man = vmw_context_res_man(res);
Thomas Hellstrome8c66ef2018-09-26 16:32:40 +02003838
3839 return 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003840}
3841
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003842int vmw_execbuf_process(struct drm_file *file_priv,
3843 struct vmw_private *dev_priv,
3844 void __user *user_commands,
3845 void *kernel_commands,
3846 uint32_t command_size,
3847 uint64_t throttle_us,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003848 uint32_t dx_context_handle,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01003849 struct drm_vmw_fence_rep __user *user_fence_rep,
Sinclair Yehc906965d2017-07-05 01:49:32 -07003850 struct vmw_fence_obj **out_fence,
3851 uint32_t flags)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003852{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003853 struct vmw_sw_context *sw_context = &dev_priv->ctx;
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01003854 struct vmw_fence_obj *fence = NULL;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003855 struct vmw_cmdbuf_header *header;
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003856 uint32_t handle;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003857 int ret;
Sinclair Yehc906965d2017-07-05 01:49:32 -07003858 int32_t out_fence_fd = -1;
3859 struct sync_file *sync_file = NULL;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003860 DECLARE_VAL_CONTEXT(val_ctx, &sw_context->res_ht, 1);
Sinclair Yehc906965d2017-07-05 01:49:32 -07003861
Thomas Hellstromfd567462018-12-12 11:52:08 +01003862 vmw_validation_set_val_mem(&val_ctx, &dev_priv->vvm);
3863
Sinclair Yehc906965d2017-07-05 01:49:32 -07003864 if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
3865 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
3866 if (out_fence_fd < 0) {
3867 DRM_ERROR("Failed to get a fence file descriptor.\n");
3868 return out_fence_fd;
3869 }
3870 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003871
Charmaine Lee2f633e52015-08-10 10:45:11 -07003872 if (throttle_us) {
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003873 ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
3874 throttle_us);
Charmaine Lee2f633e52015-08-10 10:45:11 -07003875
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003876 if (ret)
Sinclair Yehc906965d2017-07-05 01:49:32 -07003877 goto out_free_fence_fd;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003878 }
Charmaine Lee2f633e52015-08-10 10:45:11 -07003879
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003880 kernel_commands = vmw_execbuf_cmdbuf(dev_priv, user_commands,
3881 kernel_commands, command_size,
3882 &header);
Sinclair Yehc906965d2017-07-05 01:49:32 -07003883 if (IS_ERR(kernel_commands)) {
3884 ret = PTR_ERR(kernel_commands);
3885 goto out_free_fence_fd;
3886 }
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003887
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003888 ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003889 if (ret) {
3890 ret = -ERESTARTSYS;
3891 goto out_free_header;
3892 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003893
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003894 sw_context->kernel = false;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003895 if (kernel_commands == NULL) {
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003896 ret = vmw_resize_cmd_bounce(sw_context, command_size);
3897 if (unlikely(ret != 0))
3898 goto out_unlock;
3899
3900
3901 ret = copy_from_user(sw_context->cmd_bounce,
3902 user_commands, command_size);
3903
3904 if (unlikely(ret != 0)) {
3905 ret = -EFAULT;
3906 DRM_ERROR("Failed copying commands.\n");
3907 goto out_unlock;
3908 }
3909 kernel_commands = sw_context->cmd_bounce;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003910 } else if (!header)
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003911 sw_context->kernel = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003912
Thomas Hellstromd5bde952014-01-31 10:12:10 +01003913 sw_context->fp = vmw_fpriv(file_priv);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003914 INIT_LIST_HEAD(&sw_context->ctx_list);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003915 sw_context->cur_query_bo = dev_priv->pinned_bo;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003916 sw_context->last_query_ctx = NULL;
3917 sw_context->needs_post_query_barrier = false;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003918 sw_context->dx_ctx_node = NULL;
Sinclair Yehfd11a3c2015-08-10 10:56:15 -07003919 sw_context->dx_query_mob = NULL;
3920 sw_context->dx_query_ctx = NULL;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003921 memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003922 INIT_LIST_HEAD(&sw_context->res_relocations);
Thomas Hellstromfc18afc2018-09-26 15:36:52 +02003923 INIT_LIST_HEAD(&sw_context->bo_relocations);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003924 if (sw_context->staged_bindings)
3925 vmw_binding_state_reset(sw_context->staged_bindings);
3926
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003927 if (!sw_context->res_ht_initialized) {
3928 ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
3929 if (unlikely(ret != 0))
3930 goto out_unlock;
3931 sw_context->res_ht_initialized = true;
3932 }
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02003933 INIT_LIST_HEAD(&sw_context->staged_cmd_res);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003934 sw_context->ctx = &val_ctx;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003935 ret = vmw_execbuf_tie_context(dev_priv, sw_context, dx_context_handle);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003936 if (unlikely(ret != 0))
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003937 goto out_err_nores;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003938
Thomas Hellstrom922ade02011-10-04 20:13:17 +02003939 ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
3940 command_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003941 if (unlikely(ret != 0))
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01003942 goto out_err_nores;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003943
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003944 ret = vmw_resources_reserve(sw_context);
3945 if (unlikely(ret != 0))
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01003946 goto out_err_nores;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003947
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003948 ret = vmw_validation_bo_reserve(&val_ctx, true);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003949 if (unlikely(ret != 0))
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003950 goto out_err_nores;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003951
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003952 ret = vmw_validation_bo_validate(&val_ctx, true);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003953 if (unlikely(ret != 0))
3954 goto out_err;
3955
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003956 ret = vmw_validation_res_validate(&val_ctx, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00003957 if (unlikely(ret != 0))
3958 goto out_err;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003959 vmw_validation_drop_ht(&val_ctx);
Thomas Hellstrom1925d452010-05-28 11:21:57 +02003960
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07003961 ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
3962 if (unlikely(ret != 0)) {
3963 ret = -ERESTARTSYS;
3964 goto out_err;
3965 }
3966
Thomas Hellstrom30f82d812014-02-05 08:13:56 +01003967 if (dev_priv->has_mob) {
3968 ret = vmw_rebind_contexts(sw_context);
3969 if (unlikely(ret != 0))
Dan Carpenterb2ad9882014-02-11 19:03:47 +03003970 goto out_unlock_binding;
Thomas Hellstrom30f82d812014-02-05 08:13:56 +01003971 }
3972
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003973 if (!header) {
3974 ret = vmw_execbuf_submit_fifo(dev_priv, kernel_commands,
3975 command_size, sw_context);
3976 } else {
3977 ret = vmw_execbuf_submit_cmdbuf(dev_priv, header, command_size,
3978 sw_context);
3979 header = NULL;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00003980 }
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003981 mutex_unlock(&dev_priv->binding_mutex);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07003982 if (ret)
Thomas Hellstromd80efd52015-08-10 10:39:35 -07003983 goto out_err;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003984
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02003985 vmw_query_bo_switch_commit(dev_priv, sw_context);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003986 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
3987 &fence,
3988 (user_fence_rep) ? &handle : NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003989 /*
3990 * This error is harmless, because if fence submission fails,
Thomas Hellstromae2a1042011-09-01 20:18:44 +00003991 * vmw_fifo_send_fence will sync. The error will be propagated to
3992 * user-space in @fence_rep
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00003993 */
3994
3995 if (ret != 0)
3996 DRM_ERROR("Fence submission error. Syncing.\n");
3997
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02003998 vmw_execbuf_bindings_commit(sw_context, false);
3999 vmw_bind_dx_query_mob(sw_context);
4000 vmw_validation_res_unreserve(&val_ctx, false);
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07004001
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004002 vmw_validation_bo_fence(sw_context->ctx, fence);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004003
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004004 if (unlikely(dev_priv->pinned_bo != NULL &&
4005 !dev_priv->query_cid_valid))
4006 __vmw_execbuf_release_pinned_bo(dev_priv, fence);
4007
Sinclair Yehc906965d2017-07-05 01:49:32 -07004008 /*
4009 * If anything fails here, give up trying to export the fence
4010 * and do a sync since the user mode will not be able to sync
4011 * the fence itself. This ensures we are still functionally
4012 * correct.
4013 */
4014 if (flags & DRM_VMW_EXECBUF_FLAG_EXPORT_FENCE_FD) {
4015
4016 sync_file = sync_file_create(&fence->base);
4017 if (!sync_file) {
4018 DRM_ERROR("Unable to create sync file for fence\n");
4019 put_unused_fd(out_fence_fd);
4020 out_fence_fd = -1;
4021
4022 (void) vmw_fence_obj_wait(fence, false, false,
4023 VMW_FENCE_WAIT_TIMEOUT);
4024 } else {
4025 /* Link the fence with the FD created earlier */
4026 fd_install(out_fence_fd, sync_file->file);
4027 }
4028 }
4029
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02004030 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
Sinclair Yehc906965d2017-07-05 01:49:32 -07004031 user_fence_rep, fence, handle,
4032 out_fence_fd, sync_file);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00004033
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01004034 /* Don't unreference when handing fence out */
4035 if (unlikely(out_fence != NULL)) {
4036 *out_fence = fence;
4037 fence = NULL;
4038 } else if (likely(fence != NULL)) {
Thomas Hellstromae2a1042011-09-01 20:18:44 +00004039 vmw_fence_obj_unreference(&fence);
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01004040 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004041
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02004042 vmw_cmdbuf_res_commit(&sw_context->staged_cmd_res);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004043 mutex_unlock(&dev_priv->cmdbuf_mutex);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004044
4045 /*
4046 * Unreference resources outside of the cmdbuf_mutex to
4047 * avoid deadlocks in resource destruction paths.
4048 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004049 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004050
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004051 return 0;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004052
Thomas Hellstrom173fb7d2013-10-08 02:32:36 -07004053out_unlock_binding:
4054 mutex_unlock(&dev_priv->binding_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004055out_err:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004056 vmw_validation_bo_backoff(&val_ctx);
Thomas Hellstromcf5e3412014-01-30 10:58:19 +01004057out_err_nores:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004058 vmw_execbuf_bindings_commit(sw_context, true);
4059 vmw_validation_res_unreserve(&val_ctx, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004060 vmw_resource_relocations_free(&sw_context->res_relocations);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004061 vmw_free_relocations(sw_context);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004062 if (unlikely(dev_priv->pinned_bo != NULL &&
4063 !dev_priv->query_cid_valid))
4064 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004065out_unlock:
Thomas Hellstrom18e4a462014-06-09 12:39:22 +02004066 vmw_cmdbuf_res_revert(&sw_context->staged_cmd_res);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004067 vmw_validation_drop_ht(&val_ctx);
4068 WARN_ON(!list_empty(&sw_context->ctx_list));
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004069 mutex_unlock(&dev_priv->cmdbuf_mutex);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004070
4071 /*
4072 * Unreference resources outside of the cmdbuf_mutex to
4073 * avoid deadlocks in resource destruction paths.
4074 */
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004075 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07004076out_free_header:
4077 if (header)
4078 vmw_cmdbuf_header_free(header);
Sinclair Yehc906965d2017-07-05 01:49:32 -07004079out_free_fence_fd:
4080 if (out_fence_fd >= 0)
4081 put_unused_fd(out_fence_fd);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004082
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004083 return ret;
4084}
4085
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004086/**
4087 * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
4088 *
4089 * @dev_priv: The device private structure.
4090 *
4091 * This function is called to idle the fifo and unpin the query buffer
4092 * if the normal way to do this hits an error, which should typically be
4093 * extremely rare.
4094 */
4095static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
4096{
4097 DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
4098
4099 (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07004100 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
4101 if (dev_priv->dummy_query_bo_pinned) {
4102 vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
4103 dev_priv->dummy_query_bo_pinned = false;
4104 }
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004105}
4106
4107
4108/**
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004109 * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004110 * query bo.
4111 *
4112 * @dev_priv: The device private structure.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004113 * @fence: If non-NULL should point to a struct vmw_fence_obj issued
4114 * _after_ a query barrier that flushes all queries touching the current
4115 * buffer pointed to by @dev_priv->pinned_bo
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004116 *
4117 * This function should be used to unpin the pinned query bo, or
4118 * as a query barrier when we need to make sure that all queries have
4119 * finished before the next fifo command. (For example on hardware
4120 * context destructions where the hardware may otherwise leak unfinished
4121 * queries).
4122 *
4123 * This function does not return any failure codes, but make attempts
4124 * to do safe unpinning in case of errors.
4125 *
4126 * The function will synchronize on the previous query barrier, and will
4127 * thus not finish until that barrier has executed.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004128 *
4129 * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
4130 * before calling this function.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004131 */
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004132void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
4133 struct vmw_fence_obj *fence)
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004134{
4135 int ret = 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004136 struct vmw_fence_obj *lfence = NULL;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004137 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004138
4139 if (dev_priv->pinned_bo == NULL)
4140 goto out_unlock;
4141
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004142 ret = vmw_validation_add_bo(&val_ctx, dev_priv->pinned_bo, false,
4143 false);
4144 if (ret)
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004145 goto out_no_reserve;
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004146
4147 ret = vmw_validation_add_bo(&val_ctx, dev_priv->dummy_query_bo, false,
4148 false);
4149 if (ret)
4150 goto out_no_reserve;
4151
4152 ret = vmw_validation_bo_reserve(&val_ctx, false);
4153 if (ret)
4154 goto out_no_reserve;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004155
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004156 if (dev_priv->query_cid_valid) {
4157 BUG_ON(fence != NULL);
4158 ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004159 if (ret)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004160 goto out_no_emit;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004161 dev_priv->query_cid_valid = false;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004162 }
4163
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -07004164 vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
4165 if (dev_priv->dummy_query_bo_pinned) {
4166 vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
4167 dev_priv->dummy_query_bo_pinned = false;
4168 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004169 if (fence == NULL) {
4170 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
4171 NULL);
4172 fence = lfence;
4173 }
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004174 vmw_validation_bo_fence(&val_ctx, fence);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004175 if (lfence != NULL)
4176 vmw_fence_obj_unreference(&lfence);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004177
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004178 vmw_validation_unref_lists(&val_ctx);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02004179 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004180out_unlock:
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004181 return;
4182
4183out_no_emit:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004184 vmw_validation_bo_backoff(&val_ctx);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004185out_no_reserve:
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004186 vmw_validation_unref_lists(&val_ctx);
4187 vmw_execbuf_unpin_panic(dev_priv);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02004188 vmw_bo_unreference(&dev_priv->pinned_bo);
Thomas Hellstrom9c079b82018-09-26 15:28:55 +02004189
Thomas Hellstromc0951b72012-11-20 12:19:35 +00004190}
4191
4192/**
4193 * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
4194 * query bo.
4195 *
4196 * @dev_priv: The device private structure.
4197 *
4198 * This function should be used to unpin the pinned query bo, or
4199 * as a query barrier when we need to make sure that all queries have
4200 * finished before the next fifo command. (For example on hardware
4201 * context destructions where the hardware may otherwise leak unfinished
4202 * queries).
4203 *
4204 * This function does not return any failure codes, but make attempts
4205 * to do safe unpinning in case of errors.
4206 *
4207 * The function will synchronize on the previous query barrier, and will
4208 * thus not finish until that barrier has executed.
4209 */
4210void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
4211{
4212 mutex_lock(&dev_priv->cmdbuf_mutex);
4213 if (dev_priv->query_cid_valid)
4214 __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02004215 mutex_unlock(&dev_priv->cmdbuf_mutex);
4216}
4217
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004218int vmw_execbuf_ioctl(struct drm_device *dev, unsigned long data,
4219 struct drm_file *file_priv, size_t size)
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004220{
4221 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004222 struct drm_vmw_execbuf_arg arg;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004223 int ret;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004224 static const size_t copy_offset[] = {
4225 offsetof(struct drm_vmw_execbuf_arg, context_handle),
4226 sizeof(struct drm_vmw_execbuf_arg)};
Sinclair Yeh585851162017-07-05 01:45:40 -07004227 struct dma_fence *in_fence = NULL;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004228
4229 if (unlikely(size < copy_offset[0])) {
4230 DRM_ERROR("Invalid command size, ioctl %d\n",
4231 DRM_VMW_EXECBUF);
4232 return -EINVAL;
4233 }
4234
4235 if (copy_from_user(&arg, (void __user *) data, copy_offset[0]) != 0)
4236 return -EFAULT;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004237
4238 /*
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004239 * Extend the ioctl argument while
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004240 * maintaining backwards compatibility:
4241 * We take different code paths depending on the value of
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004242 * arg.version.
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004243 */
4244
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004245 if (unlikely(arg.version > DRM_VMW_EXECBUF_VERSION ||
4246 arg.version == 0)) {
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004247 DRM_ERROR("Incorrect execbuf version.\n");
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004248 return -EINVAL;
4249 }
4250
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004251 if (arg.version > 1 &&
4252 copy_from_user(&arg.context_handle,
4253 (void __user *) (data + copy_offset[0]),
4254 copy_offset[arg.version - 1] -
4255 copy_offset[0]) != 0)
4256 return -EFAULT;
4257
4258 switch (arg.version) {
4259 case 1:
4260 arg.context_handle = (uint32_t) -1;
4261 break;
4262 case 2:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004263 default:
4264 break;
4265 }
4266
Sinclair Yeh585851162017-07-05 01:45:40 -07004267
4268 /* If imported a fence FD from elsewhere, then wait on it */
4269 if (arg.flags & DRM_VMW_EXECBUF_FLAG_IMPORT_FENCE_FD) {
4270 in_fence = sync_file_get_fence(arg.imported_fence_fd);
4271
4272 if (!in_fence) {
4273 DRM_ERROR("Cannot get imported fence\n");
4274 return -EINVAL;
4275 }
4276
4277 ret = vmw_wait_dma_fence(dev_priv->fman, in_fence);
4278 if (ret)
4279 goto out;
4280 }
4281
Thomas Hellstrom294adf72014-02-27 12:34:51 +01004282 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004283 if (unlikely(ret != 0))
4284 return ret;
4285
4286 ret = vmw_execbuf_process(file_priv, dev_priv,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07004287 (void __user *)(unsigned long)arg.commands,
4288 NULL, arg.command_size, arg.throttle_us,
4289 arg.context_handle,
4290 (void __user *)(unsigned long)arg.fence_rep,
Sinclair Yehc906965d2017-07-05 01:49:32 -07004291 NULL,
4292 arg.flags);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07004293 ttm_read_unlock(&dev_priv->reservation_sem);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004294 if (unlikely(ret != 0))
Sinclair Yeh585851162017-07-05 01:45:40 -07004295 goto out;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02004296
4297 vmw_kms_cursor_post_execbuf(dev_priv);
4298
Sinclair Yeh585851162017-07-05 01:45:40 -07004299out:
4300 if (in_fence)
4301 dma_fence_put(in_fence);
4302 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004303}