blob: e5775a0db495914a683f0b09a89fe210c3d2c5d5 [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
2 *
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
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 **************************************************************************/
27
28#include "vmwgfx_drv.h"
29#include "vmwgfx_reg.h"
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/ttm/ttm_bo_api.h>
31#include <drm/ttm/ttm_placement.h>
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000032
33static int vmw_cmd_invalid(struct vmw_private *dev_priv,
34 struct vmw_sw_context *sw_context,
35 SVGA3dCmdHeader *header)
36{
37 return capable(CAP_SYS_ADMIN) ? : -EINVAL;
38}
39
40static int vmw_cmd_ok(struct vmw_private *dev_priv,
41 struct vmw_sw_context *sw_context,
42 SVGA3dCmdHeader *header)
43{
44 return 0;
45}
46
Thomas Hellstromf18c8842011-10-04 20:13:31 +020047static void vmw_resource_to_validate_list(struct vmw_sw_context *sw_context,
48 struct vmw_resource **p_res)
Thomas Hellstrombe38ab62011-08-31 07:42:54 +000049{
Thomas Hellstrombe38ab62011-08-31 07:42:54 +000050 struct vmw_resource *res = *p_res;
51
Thomas Hellstromf18c8842011-10-04 20:13:31 +020052 if (list_empty(&res->validate_head)) {
53 list_add_tail(&res->validate_head, &sw_context->resource_list);
54 *p_res = NULL;
55 } else
56 vmw_resource_unreference(p_res);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +000057}
58
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +020059/**
60 * vmw_bo_to_validate_list - add a bo to a validate list
61 *
62 * @sw_context: The software context used for this command submission batch.
63 * @bo: The buffer object to add.
64 * @fence_flags: Fence flags to be or'ed with any other fence flags for
65 * this buffer on this submission batch.
66 * @p_val_node: If non-NULL Will be updated with the validate node number
67 * on return.
68 *
69 * Returns -EINVAL if the limit of number of buffer objects per command
70 * submission is reached.
71 */
72static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
73 struct ttm_buffer_object *bo,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +020074 uint32_t *p_val_node)
75{
76 uint32_t val_node;
77 struct ttm_validate_buffer *val_buf;
78
79 val_node = vmw_dmabuf_validate_node(bo, sw_context->cur_val_buf);
80
81 if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
82 DRM_ERROR("Max number of DMA buffers per submission"
83 " exceeded.\n");
84 return -EINVAL;
85 }
86
87 val_buf = &sw_context->val_bufs[val_node];
88 if (unlikely(val_node == sw_context->cur_val_buf)) {
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +020089 val_buf->bo = ttm_bo_reference(bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +020090 list_add_tail(&val_buf->head, &sw_context->validate_nodes);
91 ++sw_context->cur_val_buf;
92 }
93
Maarten Lankhorstbe013362012-10-12 15:01:43 +000094 sw_context->fence_flags |= DRM_VMW_FENCE_FLAG_EXEC;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +020095
96 if (p_val_node)
97 *p_val_node = val_node;
98
99 return 0;
100}
101
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000102static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
103 struct vmw_sw_context *sw_context,
104 SVGA3dCmdHeader *header)
105{
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000106 struct vmw_resource *ctx;
107
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000108 struct vmw_cid_cmd {
109 SVGA3dCmdHeader header;
110 __le32 cid;
111 } *cmd;
112 int ret;
113
114 cmd = container_of(header, struct vmw_cid_cmd, header);
115 if (likely(sw_context->cid_valid && cmd->cid == sw_context->last_cid))
116 return 0;
117
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000118 ret = vmw_context_check(dev_priv, sw_context->tfile, cmd->cid,
119 &ctx);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000120 if (unlikely(ret != 0)) {
121 DRM_ERROR("Could not find or use context %u\n",
122 (unsigned) cmd->cid);
123 return ret;
124 }
125
126 sw_context->last_cid = cmd->cid;
127 sw_context->cid_valid = true;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200128 sw_context->cur_ctx = ctx;
Thomas Hellstromf18c8842011-10-04 20:13:31 +0200129 vmw_resource_to_validate_list(sw_context, &ctx);
130
131 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000132}
133
134static int vmw_cmd_sid_check(struct vmw_private *dev_priv,
135 struct vmw_sw_context *sw_context,
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100136 uint32_t *sid)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000137{
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000138 struct vmw_surface *srf;
139 int ret;
140 struct vmw_resource *res;
141
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100142 if (*sid == SVGA3D_INVALID_ID)
143 return 0;
144
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000145 if (likely((sw_context->sid_valid &&
146 *sid == sw_context->last_sid))) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100147 *sid = sw_context->sid_translation;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000148 return 0;
149 }
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100150
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200151 ret = vmw_user_surface_lookup_handle(dev_priv,
152 sw_context->tfile,
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000153 *sid, &srf);
154 if (unlikely(ret != 0)) {
155 DRM_ERROR("Could ot find or use surface 0x%08x "
156 "address 0x%08lx\n",
157 (unsigned int) *sid,
158 (unsigned long) sid);
159 return ret;
160 }
161
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200162 ret = vmw_surface_validate(dev_priv, srf);
163 if (unlikely(ret != 0)) {
164 if (ret != -ERESTARTSYS)
165 DRM_ERROR("Could not validate surface.\n");
166 vmw_surface_unreference(&srf);
167 return ret;
168 }
169
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000170 sw_context->last_sid = *sid;
171 sw_context->sid_valid = true;
172 sw_context->sid_translation = srf->res.id;
173 *sid = sw_context->sid_translation;
174
175 res = &srf->res;
Thomas Hellstromf18c8842011-10-04 20:13:31 +0200176 vmw_resource_to_validate_list(sw_context, &res);
177
178 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000179}
180
181
182static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
183 struct vmw_sw_context *sw_context,
184 SVGA3dCmdHeader *header)
185{
186 struct vmw_sid_cmd {
187 SVGA3dCmdHeader header;
188 SVGA3dCmdSetRenderTarget body;
189 } *cmd;
190 int ret;
191
192 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
193 if (unlikely(ret != 0))
194 return ret;
195
196 cmd = container_of(header, struct vmw_sid_cmd, header);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100197 ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.target.sid);
198 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000199}
200
201static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
202 struct vmw_sw_context *sw_context,
203 SVGA3dCmdHeader *header)
204{
205 struct vmw_sid_cmd {
206 SVGA3dCmdHeader header;
207 SVGA3dCmdSurfaceCopy body;
208 } *cmd;
209 int ret;
210
211 cmd = container_of(header, struct vmw_sid_cmd, header);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100212 ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000213 if (unlikely(ret != 0))
214 return ret;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100215 return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000216}
217
218static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
219 struct vmw_sw_context *sw_context,
220 SVGA3dCmdHeader *header)
221{
222 struct vmw_sid_cmd {
223 SVGA3dCmdHeader header;
224 SVGA3dCmdSurfaceStretchBlt body;
225 } *cmd;
226 int ret;
227
228 cmd = container_of(header, struct vmw_sid_cmd, header);
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100229 ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000230 if (unlikely(ret != 0))
231 return ret;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100232 return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000233}
234
235static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
236 struct vmw_sw_context *sw_context,
237 SVGA3dCmdHeader *header)
238{
239 struct vmw_sid_cmd {
240 SVGA3dCmdHeader header;
241 SVGA3dCmdBlitSurfaceToScreen body;
242 } *cmd;
243
244 cmd = container_of(header, struct vmw_sid_cmd, header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +0200245
246 if (unlikely(!sw_context->kernel)) {
247 DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
248 return -EPERM;
249 }
250
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100251 return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.srcImage.sid);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000252}
253
254static int vmw_cmd_present_check(struct vmw_private *dev_priv,
255 struct vmw_sw_context *sw_context,
256 SVGA3dCmdHeader *header)
257{
258 struct vmw_sid_cmd {
259 SVGA3dCmdHeader header;
260 SVGA3dCmdPresent body;
261 } *cmd;
262
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200263
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000264 cmd = container_of(header, struct vmw_sid_cmd, header);
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +0200265
266 if (unlikely(!sw_context->kernel)) {
267 DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
268 return -EPERM;
269 }
270
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100271 return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.sid);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000272}
273
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200274/**
275 * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
276 *
277 * @dev_priv: The device private structure.
278 * @cid: The hardware context for the next query.
279 * @new_query_bo: The new buffer holding query results.
280 * @sw_context: The software context used for this command submission.
281 *
282 * This function checks whether @new_query_bo is suitable for holding
283 * query results, and if another buffer currently is pinned for query
284 * results. If so, the function prepares the state of @sw_context for
285 * switching pinned buffers after successful submission of the current
286 * command batch. It also checks whether we're using a new query context.
287 * In that case, it makes sure we emit a query barrier for the old
288 * context before the current query buffer is fenced.
289 */
290static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
291 uint32_t cid,
292 struct ttm_buffer_object *new_query_bo,
293 struct vmw_sw_context *sw_context)
294{
295 int ret;
296 bool add_cid = false;
297 uint32_t cid_to_add;
298
299 if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
300
301 if (unlikely(new_query_bo->num_pages > 4)) {
302 DRM_ERROR("Query buffer too large.\n");
303 return -EINVAL;
304 }
305
306 if (unlikely(sw_context->cur_query_bo != NULL)) {
307 BUG_ON(!sw_context->query_cid_valid);
308 add_cid = true;
309 cid_to_add = sw_context->cur_query_cid;
310 ret = vmw_bo_to_validate_list(sw_context,
311 sw_context->cur_query_bo,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200312 NULL);
313 if (unlikely(ret != 0))
314 return ret;
315 }
316 sw_context->cur_query_bo = new_query_bo;
317
318 ret = vmw_bo_to_validate_list(sw_context,
319 dev_priv->dummy_query_bo,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200320 NULL);
321 if (unlikely(ret != 0))
322 return ret;
323
324 }
325
326 if (unlikely(cid != sw_context->cur_query_cid &&
327 sw_context->query_cid_valid)) {
328 add_cid = true;
329 cid_to_add = sw_context->cur_query_cid;
330 }
331
332 sw_context->cur_query_cid = cid;
333 sw_context->query_cid_valid = true;
334
335 if (add_cid) {
336 struct vmw_resource *ctx = sw_context->cur_ctx;
337
338 if (list_empty(&ctx->query_head))
339 list_add_tail(&ctx->query_head,
340 &sw_context->query_list);
341 ret = vmw_bo_to_validate_list(sw_context,
342 dev_priv->dummy_query_bo,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200343 NULL);
344 if (unlikely(ret != 0))
345 return ret;
346 }
347 return 0;
348}
349
350
351/**
352 * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
353 *
354 * @dev_priv: The device private structure.
355 * @sw_context: The software context used for this command submission batch.
356 *
357 * This function will check if we're switching query buffers, and will then,
358 * if no other query waits are issued this command submission batch,
359 * issue a dummy occlusion query wait used as a query barrier. When the fence
360 * object following that query wait has signaled, we are sure that all
361 * preseding queries have finished, and the old query buffer can be unpinned.
362 * However, since both the new query buffer and the old one are fenced with
363 * that fence, we can do an asynchronus unpin now, and be sure that the
364 * old query buffer won't be moved until the fence has signaled.
365 *
366 * As mentioned above, both the new - and old query buffers need to be fenced
367 * using a sequence emitted *after* calling this function.
368 */
369static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
370 struct vmw_sw_context *sw_context)
371{
372
373 struct vmw_resource *ctx, *next_ctx;
374 int ret;
375
376 /*
377 * The validate list should still hold references to all
378 * contexts here.
379 */
380
381 list_for_each_entry_safe(ctx, next_ctx, &sw_context->query_list,
382 query_head) {
383 list_del_init(&ctx->query_head);
384
Thomas Hellstromf18c8842011-10-04 20:13:31 +0200385 BUG_ON(list_empty(&ctx->validate_head));
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200386
387 ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
388
389 if (unlikely(ret != 0))
390 DRM_ERROR("Out of fifo space for dummy query.\n");
391 }
392
393 if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
394 if (dev_priv->pinned_bo) {
395 vmw_bo_pin(dev_priv->pinned_bo, false);
396 ttm_bo_unref(&dev_priv->pinned_bo);
397 }
398
399 vmw_bo_pin(sw_context->cur_query_bo, true);
400
401 /*
402 * We pin also the dummy_query_bo buffer so that we
403 * don't need to validate it when emitting
404 * dummy queries in context destroy paths.
405 */
406
407 vmw_bo_pin(dev_priv->dummy_query_bo, true);
408 dev_priv->dummy_query_bo_pinned = true;
409
410 dev_priv->query_cid = sw_context->cur_query_cid;
411 dev_priv->pinned_bo =
412 ttm_bo_reference(sw_context->cur_query_bo);
413 }
414}
415
416/**
417 * vmw_query_switch_backoff - clear query barrier list
418 * @sw_context: The sw context used for this submission batch.
419 *
420 * This function is used as part of an error path, where a previously
421 * set up list of query barriers needs to be cleared.
422 *
423 */
424static void vmw_query_switch_backoff(struct vmw_sw_context *sw_context)
425{
426 struct list_head *list, *next;
427
428 list_for_each_safe(list, next, &sw_context->query_list) {
429 list_del_init(list);
430 }
431}
432
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000433static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
434 struct vmw_sw_context *sw_context,
435 SVGAGuestPtr *ptr,
436 struct vmw_dma_buffer **vmw_bo_p)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000437{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000438 struct vmw_dma_buffer *vmw_bo = NULL;
439 struct ttm_buffer_object *bo;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000440 uint32_t handle = ptr->gmrId;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000441 struct vmw_relocation *reloc;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000442 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000443
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000444 ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
445 if (unlikely(ret != 0)) {
446 DRM_ERROR("Could not find or use GMR region.\n");
447 return -EINVAL;
448 }
449 bo = &vmw_bo->base;
450
451 if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000452 DRM_ERROR("Max number relocations per submission"
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000453 " exceeded\n");
454 ret = -EINVAL;
455 goto out_no_reloc;
456 }
457
458 reloc = &sw_context->relocs[sw_context->cur_reloc++];
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000459 reloc->location = ptr;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000460
Maarten Lankhorstbe013362012-10-12 15:01:43 +0000461 ret = vmw_bo_to_validate_list(sw_context, bo, &reloc->index);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200462 if (unlikely(ret != 0))
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000463 goto out_no_reloc;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000464
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000465 *vmw_bo_p = vmw_bo;
466 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000467
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000468out_no_reloc:
469 vmw_dmabuf_unreference(&vmw_bo);
470 vmw_bo_p = NULL;
471 return ret;
472}
473
474static int vmw_cmd_end_query(struct vmw_private *dev_priv,
475 struct vmw_sw_context *sw_context,
476 SVGA3dCmdHeader *header)
477{
478 struct vmw_dma_buffer *vmw_bo;
479 struct vmw_query_cmd {
480 SVGA3dCmdHeader header;
481 SVGA3dCmdEndQuery q;
482 } *cmd;
483 int ret;
484
485 cmd = container_of(header, struct vmw_query_cmd, header);
486 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
487 if (unlikely(ret != 0))
488 return ret;
489
490 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
491 &cmd->q.guestResult,
492 &vmw_bo);
493 if (unlikely(ret != 0))
494 return ret;
495
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200496 ret = vmw_query_bo_switch_prepare(dev_priv, cmd->q.cid,
497 &vmw_bo->base, sw_context);
498
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000499 vmw_dmabuf_unreference(&vmw_bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200500 return ret;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000501}
502
503static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
504 struct vmw_sw_context *sw_context,
505 SVGA3dCmdHeader *header)
506{
507 struct vmw_dma_buffer *vmw_bo;
508 struct vmw_query_cmd {
509 SVGA3dCmdHeader header;
510 SVGA3dCmdWaitForQuery q;
511 } *cmd;
512 int ret;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200513 struct vmw_resource *ctx;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000514
515 cmd = container_of(header, struct vmw_query_cmd, header);
516 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
517 if (unlikely(ret != 0))
518 return ret;
519
520 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
521 &cmd->q.guestResult,
522 &vmw_bo);
523 if (unlikely(ret != 0))
524 return ret;
525
526 vmw_dmabuf_unreference(&vmw_bo);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200527
528 /*
529 * This wait will act as a barrier for previous waits for this
530 * context.
531 */
532
533 ctx = sw_context->cur_ctx;
534 if (!list_empty(&ctx->query_head))
535 list_del_init(&ctx->query_head);
536
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000537 return 0;
538}
539
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000540static int vmw_cmd_dma(struct vmw_private *dev_priv,
541 struct vmw_sw_context *sw_context,
542 SVGA3dCmdHeader *header)
543{
544 struct vmw_dma_buffer *vmw_bo = NULL;
545 struct ttm_buffer_object *bo;
546 struct vmw_surface *srf = NULL;
547 struct vmw_dma_cmd {
548 SVGA3dCmdHeader header;
549 SVGA3dCmdSurfaceDMA dma;
550 } *cmd;
551 int ret;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000552 struct vmw_resource *res;
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000553
554 cmd = container_of(header, struct vmw_dma_cmd, header);
555 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
556 &cmd->dma.guest.ptr,
557 &vmw_bo);
558 if (unlikely(ret != 0))
559 return ret;
560
561 bo = &vmw_bo->base;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100562 ret = vmw_user_surface_lookup_handle(dev_priv, sw_context->tfile,
563 cmd->dma.host.sid, &srf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000564 if (ret) {
565 DRM_ERROR("could not find surface\n");
566 goto out_no_reloc;
567 }
568
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200569 ret = vmw_surface_validate(dev_priv, srf);
570 if (unlikely(ret != 0)) {
571 if (ret != -ERESTARTSYS)
572 DRM_ERROR("Culd not validate surface.\n");
573 goto out_no_validate;
574 }
575
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000576 /*
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100577 * Patch command stream with device SID.
578 */
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100579 cmd->dma.host.sid = srf->res.id;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000580 vmw_kms_cursor_snoop(srf, sw_context->tfile, bo, header);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000581
582 vmw_dmabuf_unreference(&vmw_bo);
583
584 res = &srf->res;
Thomas Hellstromf18c8842011-10-04 20:13:31 +0200585 vmw_resource_to_validate_list(sw_context, &res);
586
587 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000588
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200589out_no_validate:
590 vmw_surface_unreference(&srf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000591out_no_reloc:
592 vmw_dmabuf_unreference(&vmw_bo);
593 return ret;
594}
595
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100596static int vmw_cmd_draw(struct vmw_private *dev_priv,
597 struct vmw_sw_context *sw_context,
598 SVGA3dCmdHeader *header)
599{
600 struct vmw_draw_cmd {
601 SVGA3dCmdHeader header;
602 SVGA3dCmdDrawPrimitives body;
603 } *cmd;
604 SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
605 (unsigned long)header + sizeof(*cmd));
606 SVGA3dPrimitiveRange *range;
607 uint32_t i;
608 uint32_t maxnum;
609 int ret;
610
611 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
612 if (unlikely(ret != 0))
613 return ret;
614
615 cmd = container_of(header, struct vmw_draw_cmd, header);
616 maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
617
618 if (unlikely(cmd->body.numVertexDecls > maxnum)) {
619 DRM_ERROR("Illegal number of vertex declarations.\n");
620 return -EINVAL;
621 }
622
623 for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
624 ret = vmw_cmd_sid_check(dev_priv, sw_context,
625 &decl->array.surfaceId);
626 if (unlikely(ret != 0))
627 return ret;
628 }
629
630 maxnum = (header->size - sizeof(cmd->body) -
631 cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
632 if (unlikely(cmd->body.numRanges > maxnum)) {
633 DRM_ERROR("Illegal number of index ranges.\n");
634 return -EINVAL;
635 }
636
637 range = (SVGA3dPrimitiveRange *) decl;
638 for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
639 ret = vmw_cmd_sid_check(dev_priv, sw_context,
640 &range->indexArray.surfaceId);
641 if (unlikely(ret != 0))
642 return ret;
643 }
644 return 0;
645}
646
647
648static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
649 struct vmw_sw_context *sw_context,
650 SVGA3dCmdHeader *header)
651{
652 struct vmw_tex_state_cmd {
653 SVGA3dCmdHeader header;
654 SVGA3dCmdSetTextureState state;
655 };
656
657 SVGA3dTextureState *last_state = (SVGA3dTextureState *)
658 ((unsigned long) header + header->size + sizeof(header));
659 SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
660 ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
661 int ret;
662
663 ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
664 if (unlikely(ret != 0))
665 return ret;
666
667 for (; cur_state < last_state; ++cur_state) {
668 if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
669 continue;
670
671 ret = vmw_cmd_sid_check(dev_priv, sw_context,
672 &cur_state->value);
673 if (unlikely(ret != 0))
674 return ret;
675 }
676
677 return 0;
678}
679
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +0200680static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
681 struct vmw_sw_context *sw_context,
682 void *buf)
683{
684 struct vmw_dma_buffer *vmw_bo;
685 int ret;
686
687 struct {
688 uint32_t header;
689 SVGAFifoCmdDefineGMRFB body;
690 } *cmd = buf;
691
692 ret = vmw_translate_guest_ptr(dev_priv, sw_context,
693 &cmd->body.ptr,
694 &vmw_bo);
695 if (unlikely(ret != 0))
696 return ret;
697
698 vmw_dmabuf_unreference(&vmw_bo);
699
700 return ret;
701}
702
703static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
704 struct vmw_sw_context *sw_context,
705 void *buf, uint32_t *size)
706{
707 uint32_t size_remaining = *size;
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +0200708 uint32_t cmd_id;
709
710 cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
711 switch (cmd_id) {
712 case SVGA_CMD_UPDATE:
713 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +0200714 break;
715 case SVGA_CMD_DEFINE_GMRFB:
716 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
717 break;
718 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
719 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
720 break;
721 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
722 *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
723 break;
724 default:
725 DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
726 return -EINVAL;
727 }
728
729 if (*size > size_remaining) {
730 DRM_ERROR("Invalid SVGA command (size mismatch):"
731 " %u.\n", cmd_id);
732 return -EINVAL;
733 }
734
Jakob Bornecrantz0cff60c2011-10-04 20:13:27 +0200735 if (unlikely(!sw_context->kernel)) {
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +0200736 DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
737 return -EPERM;
738 }
739
740 if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
741 return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
742
743 return 0;
744}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000745
746typedef int (*vmw_cmd_func) (struct vmw_private *,
747 struct vmw_sw_context *,
748 SVGA3dCmdHeader *);
749
750#define VMW_CMD_DEF(cmd, func) \
751 [cmd - SVGA_3D_CMD_BASE] = func
752
753static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
754 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
755 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
756 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
757 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
758 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
759 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
760 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
761 VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
762 VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
763 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
764 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
765 &vmw_cmd_set_render_target_check),
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100766 VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000767 VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
768 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
769 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
770 VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
771 VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
772 VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
773 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
774 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
775 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
776 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_cid_check),
777 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100778 VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000779 VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
780 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_cid_check),
Thomas Hellstrom4e4ddd42010-02-21 14:54:55 +0000781 VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query),
782 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000783 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
784 VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
785 &vmw_cmd_blt_surf_screen_check)
786};
787
788static int vmw_cmd_check(struct vmw_private *dev_priv,
789 struct vmw_sw_context *sw_context,
790 void *buf, uint32_t *size)
791{
792 uint32_t cmd_id;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100793 uint32_t size_remaining = *size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000794 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
795 int ret;
796
Jakob Bornecrantz4084fb82011-10-04 20:13:19 +0200797 cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
798 /* Handle any none 3D commands */
799 if (unlikely(cmd_id < SVGA_CMD_MAX))
800 return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
801
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000802
803 cmd_id = le32_to_cpu(header->id);
804 *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
805
806 cmd_id -= SVGA_3D_CMD_BASE;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100807 if (unlikely(*size > size_remaining))
808 goto out_err;
809
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000810 if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
811 goto out_err;
812
813 ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
814 if (unlikely(ret != 0))
815 goto out_err;
816
817 return 0;
818out_err:
819 DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
820 cmd_id + SVGA_3D_CMD_BASE);
821 return -EINVAL;
822}
823
824static int vmw_cmd_check_all(struct vmw_private *dev_priv,
825 struct vmw_sw_context *sw_context,
Thomas Hellstrom922ade02011-10-04 20:13:17 +0200826 void *buf,
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000827 uint32_t size)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000828{
829 int32_t cur_size = size;
830 int ret;
831
832 while (cur_size > 0) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100833 size = cur_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000834 ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
835 if (unlikely(ret != 0))
836 return ret;
837 buf = (void *)((unsigned long) buf + size);
838 cur_size -= size;
839 }
840
841 if (unlikely(cur_size != 0)) {
842 DRM_ERROR("Command verifier out of sync.\n");
843 return -EINVAL;
844 }
845
846 return 0;
847}
848
849static void vmw_free_relocations(struct vmw_sw_context *sw_context)
850{
851 sw_context->cur_reloc = 0;
852}
853
854static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
855{
856 uint32_t i;
857 struct vmw_relocation *reloc;
858 struct ttm_validate_buffer *validate;
859 struct ttm_buffer_object *bo;
860
861 for (i = 0; i < sw_context->cur_reloc; ++i) {
862 reloc = &sw_context->relocs[i];
863 validate = &sw_context->val_bufs[reloc->index];
864 bo = validate->bo;
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200865 if (bo->mem.mem_type == TTM_PL_VRAM) {
866 reloc->location->offset += bo->offset;
867 reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
868 } else
869 reloc->location->gmrId = bo->mem.start;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000870 }
871 vmw_free_relocations(sw_context);
872}
873
874static void vmw_clear_validations(struct vmw_sw_context *sw_context)
875{
876 struct ttm_validate_buffer *entry, *next;
Thomas Hellstromf18c8842011-10-04 20:13:31 +0200877 struct vmw_resource *res, *res_next;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000878
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000879 /*
880 * Drop references to DMA buffers held during command submission.
881 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000882 list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
883 head) {
884 list_del(&entry->head);
885 vmw_dmabuf_validate_clear(entry->bo);
886 ttm_bo_unref(&entry->bo);
887 sw_context->cur_val_buf--;
888 }
889 BUG_ON(sw_context->cur_val_buf != 0);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000890
891 /*
892 * Drop references to resources held during command submission.
893 */
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200894 vmw_resource_unreserve(&sw_context->resource_list);
Thomas Hellstromf18c8842011-10-04 20:13:31 +0200895 list_for_each_entry_safe(res, res_next, &sw_context->resource_list,
896 validate_head) {
897 list_del_init(&res->validate_head);
898 vmw_resource_unreference(&res);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000899 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000900}
901
902static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
903 struct ttm_buffer_object *bo)
904{
905 int ret;
906
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200907
908 /*
909 * Don't validate pinned buffers.
910 */
911
912 if (bo == dev_priv->pinned_bo ||
913 (bo == dev_priv->dummy_query_bo &&
914 dev_priv->dummy_query_bo_pinned))
915 return 0;
916
Thomas Hellstrom8ba51522010-01-16 16:05:05 +0100917 /**
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200918 * Put BO in VRAM if there is space, otherwise as a GMR.
919 * If there is no space in VRAM and GMR ids are all used up,
920 * start evicting GMRs to make room. If the DMA buffer can't be
921 * used as a GMR, this will return -ENOMEM.
Thomas Hellstrom8ba51522010-01-16 16:05:05 +0100922 */
923
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200924 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false, false);
Thomas Hellstrom3d3a5b32009-12-08 12:59:34 +0100925 if (likely(ret == 0 || ret == -ERESTARTSYS))
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000926 return ret;
927
Thomas Hellstrom8ba51522010-01-16 16:05:05 +0100928 /**
929 * If that failed, try VRAM again, this time evicting
930 * previous contents.
931 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000932
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200933 DRM_INFO("Falling through to VRAM.\n");
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000934 ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000935 return ret;
936}
937
938
939static int vmw_validate_buffers(struct vmw_private *dev_priv,
940 struct vmw_sw_context *sw_context)
941{
942 struct ttm_validate_buffer *entry;
943 int ret;
944
945 list_for_each_entry(entry, &sw_context->validate_nodes, head) {
946 ret = vmw_validate_single_buffer(dev_priv, entry->bo);
947 if (unlikely(ret != 0))
948 return ret;
949 }
950 return 0;
951}
952
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000953static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
954 uint32_t size)
955{
956 if (likely(sw_context->cmd_bounce_size >= size))
957 return 0;
958
959 if (sw_context->cmd_bounce_size == 0)
960 sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
961
962 while (sw_context->cmd_bounce_size < size) {
963 sw_context->cmd_bounce_size =
964 PAGE_ALIGN(sw_context->cmd_bounce_size +
965 (sw_context->cmd_bounce_size >> 1));
966 }
967
968 if (sw_context->cmd_bounce != NULL)
969 vfree(sw_context->cmd_bounce);
970
971 sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
972
973 if (sw_context->cmd_bounce == NULL) {
974 DRM_ERROR("Failed to allocate command bounce buffer.\n");
975 sw_context->cmd_bounce_size = 0;
976 return -ENOMEM;
977 }
978
979 return 0;
980}
981
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000982/**
983 * vmw_execbuf_fence_commands - create and submit a command stream fence
984 *
985 * Creates a fence object and submits a command stream marker.
986 * If this fails for some reason, We sync the fifo and return NULL.
987 * It is then safe to fence buffers with a NULL pointer.
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +0200988 *
989 * If @p_handle is not NULL @file_priv must also not be NULL. Creates
990 * a userspace handle if @p_handle is not NULL, otherwise not.
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000991 */
992
993int vmw_execbuf_fence_commands(struct drm_file *file_priv,
994 struct vmw_private *dev_priv,
995 struct vmw_fence_obj **p_fence,
996 uint32_t *p_handle)
997{
998 uint32_t sequence;
999 int ret;
1000 bool synced = false;
1001
Jakob Bornecrantz6070e9f2011-10-04 20:13:16 +02001002 /* p_handle implies file_priv. */
1003 BUG_ON(p_handle != NULL && file_priv == NULL);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001004
1005 ret = vmw_fifo_send_fence(dev_priv, &sequence);
1006 if (unlikely(ret != 0)) {
1007 DRM_ERROR("Fence submission error. Syncing.\n");
1008 synced = true;
1009 }
1010
1011 if (p_handle != NULL)
1012 ret = vmw_user_fence_create(file_priv, dev_priv->fman,
1013 sequence,
1014 DRM_VMW_FENCE_FLAG_EXEC,
1015 p_fence, p_handle);
1016 else
1017 ret = vmw_fence_create(dev_priv->fman, sequence,
1018 DRM_VMW_FENCE_FLAG_EXEC,
1019 p_fence);
1020
1021 if (unlikely(ret != 0 && !synced)) {
1022 (void) vmw_fallback_wait(dev_priv, false, false,
1023 sequence, false,
1024 VMW_FENCE_WAIT_TIMEOUT);
1025 *p_fence = NULL;
1026 }
1027
1028 return 0;
1029}
1030
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02001031/**
1032 * vmw_execbuf_copy_fence_user - copy fence object information to
1033 * user-space.
1034 *
1035 * @dev_priv: Pointer to a vmw_private struct.
1036 * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
1037 * @ret: Return value from fence object creation.
1038 * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
1039 * which the information should be copied.
1040 * @fence: Pointer to the fenc object.
1041 * @fence_handle: User-space fence handle.
1042 *
1043 * This function copies fence information to user-space. If copying fails,
1044 * The user-space struct drm_vmw_fence_rep::error member is hopefully
1045 * left untouched, and if it's preloaded with an -EFAULT by user-space,
1046 * the error will hopefully be detected.
1047 * Also if copying fails, user-space will be unable to signal the fence
1048 * object so we wait for it immediately, and then unreference the
1049 * user-space reference.
1050 */
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +02001051void
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02001052vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
1053 struct vmw_fpriv *vmw_fp,
1054 int ret,
1055 struct drm_vmw_fence_rep __user *user_fence_rep,
1056 struct vmw_fence_obj *fence,
1057 uint32_t fence_handle)
1058{
1059 struct drm_vmw_fence_rep fence_rep;
1060
1061 if (user_fence_rep == NULL)
1062 return;
1063
Dan Carpenter80d9b242011-10-18 09:10:12 +03001064 memset(&fence_rep, 0, sizeof(fence_rep));
1065
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02001066 fence_rep.error = ret;
1067 if (ret == 0) {
1068 BUG_ON(fence == NULL);
1069
1070 fence_rep.handle = fence_handle;
1071 fence_rep.seqno = fence->seqno;
1072 vmw_update_seqno(dev_priv, &dev_priv->fifo);
1073 fence_rep.passed_seqno = dev_priv->last_read_seqno;
1074 }
1075
1076 /*
1077 * copy_to_user errors will be detected by user space not
1078 * seeing fence_rep::error filled in. Typically
1079 * user-space would have pre-set that member to -EFAULT.
1080 */
1081 ret = copy_to_user(user_fence_rep, &fence_rep,
1082 sizeof(fence_rep));
1083
1084 /*
1085 * User-space lost the fence object. We need to sync
1086 * and unreference the handle.
1087 */
1088 if (unlikely(ret != 0) && (fence_rep.error == 0)) {
1089 ttm_ref_object_base_unref(vmw_fp->tfile,
1090 fence_handle, TTM_REF_USAGE);
1091 DRM_ERROR("Fence copy error. Syncing.\n");
1092 (void) vmw_fence_obj_wait(fence, fence->signal_mask,
1093 false, false,
1094 VMW_FENCE_WAIT_TIMEOUT);
1095 }
1096}
1097
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001098int vmw_execbuf_process(struct drm_file *file_priv,
1099 struct vmw_private *dev_priv,
1100 void __user *user_commands,
1101 void *kernel_commands,
1102 uint32_t command_size,
1103 uint64_t throttle_us,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001104 struct drm_vmw_fence_rep __user *user_fence_rep,
1105 struct vmw_fence_obj **out_fence)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001106{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001107 struct vmw_sw_context *sw_context = &dev_priv->ctx;
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001108 struct vmw_fence_obj *fence = NULL;
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001109 uint32_t handle;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001110 void *cmd;
1111 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001112
1113 ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001114 if (unlikely(ret != 0))
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001115 return -ERESTARTSYS;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001116
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001117 if (kernel_commands == NULL) {
1118 sw_context->kernel = false;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001119
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001120 ret = vmw_resize_cmd_bounce(sw_context, command_size);
1121 if (unlikely(ret != 0))
1122 goto out_unlock;
1123
1124
1125 ret = copy_from_user(sw_context->cmd_bounce,
1126 user_commands, command_size);
1127
1128 if (unlikely(ret != 0)) {
1129 ret = -EFAULT;
1130 DRM_ERROR("Failed copying commands.\n");
1131 goto out_unlock;
1132 }
1133 kernel_commands = sw_context->cmd_bounce;
1134 } else
1135 sw_context->kernel = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001136
1137 sw_context->tfile = vmw_fpriv(file_priv)->tfile;
1138 sw_context->cid_valid = false;
1139 sw_context->sid_valid = false;
1140 sw_context->cur_reloc = 0;
1141 sw_context->cur_val_buf = 0;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001142 sw_context->fence_flags = 0;
1143 INIT_LIST_HEAD(&sw_context->query_list);
Thomas Hellstromf18c8842011-10-04 20:13:31 +02001144 INIT_LIST_HEAD(&sw_context->resource_list);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001145 sw_context->cur_query_bo = dev_priv->pinned_bo;
1146 sw_context->cur_query_cid = dev_priv->query_cid;
1147 sw_context->query_cid_valid = (dev_priv->pinned_bo != NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001148
1149 INIT_LIST_HEAD(&sw_context->validate_nodes);
1150
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001151 ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
1152 command_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001153 if (unlikely(ret != 0))
1154 goto out_err;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001155
Thomas Hellstrom65705962010-11-17 12:28:31 +00001156 ret = ttm_eu_reserve_buffers(&sw_context->validate_nodes);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001157 if (unlikely(ret != 0))
1158 goto out_err;
1159
1160 ret = vmw_validate_buffers(dev_priv, sw_context);
1161 if (unlikely(ret != 0))
1162 goto out_err;
1163
1164 vmw_apply_relocations(sw_context);
Thomas Hellstrom1925d452010-05-28 11:21:57 +02001165
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001166 if (throttle_us) {
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +00001167 ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001168 throttle_us);
Thomas Hellstrom1925d452010-05-28 11:21:57 +02001169
1170 if (unlikely(ret != 0))
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001171 goto out_throttle;
Thomas Hellstrom1925d452010-05-28 11:21:57 +02001172 }
1173
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001174 cmd = vmw_fifo_reserve(dev_priv, command_size);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001175 if (unlikely(cmd == NULL)) {
1176 DRM_ERROR("Failed reserving fifo space for commands.\n");
1177 ret = -ENOMEM;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001178 goto out_throttle;
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001179 }
1180
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001181 memcpy(cmd, kernel_commands, command_size);
1182 vmw_fifo_commit(dev_priv, command_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001183
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001184 vmw_query_bo_switch_commit(dev_priv, sw_context);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001185 ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
1186 &fence,
1187 (user_fence_rep) ? &handle : NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001188 /*
1189 * This error is harmless, because if fence submission fails,
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001190 * vmw_fifo_send_fence will sync. The error will be propagated to
1191 * user-space in @fence_rep
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001192 */
1193
1194 if (ret != 0)
1195 DRM_ERROR("Fence submission error. Syncing.\n");
1196
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001197 ttm_eu_fence_buffer_objects(&sw_context->validate_nodes,
1198 (void *) fence);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001199
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001200 vmw_clear_validations(sw_context);
Thomas Hellstrom8bf445c2011-10-10 12:23:25 +02001201 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
1202 user_fence_rep, fence, handle);
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001203
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001204 /* Don't unreference when handing fence out */
1205 if (unlikely(out_fence != NULL)) {
1206 *out_fence = fence;
1207 fence = NULL;
1208 } else if (likely(fence != NULL)) {
Thomas Hellstromae2a1042011-09-01 20:18:44 +00001209 vmw_fence_obj_unreference(&fence);
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001210 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001211
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001212 mutex_unlock(&dev_priv->cmdbuf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001213 return 0;
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001214
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001215out_err:
1216 vmw_free_relocations(sw_context);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +00001217out_throttle:
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001218 vmw_query_switch_backoff(sw_context);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001219 ttm_eu_backoff_reservation(&sw_context->validate_nodes);
1220 vmw_clear_validations(sw_context);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001221out_unlock:
1222 mutex_unlock(&dev_priv->cmdbuf_mutex);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001223 return ret;
1224}
1225
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001226/**
1227 * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
1228 *
1229 * @dev_priv: The device private structure.
1230 *
1231 * This function is called to idle the fifo and unpin the query buffer
1232 * if the normal way to do this hits an error, which should typically be
1233 * extremely rare.
1234 */
1235static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
1236{
1237 DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
1238
1239 (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
1240 vmw_bo_pin(dev_priv->pinned_bo, false);
1241 vmw_bo_pin(dev_priv->dummy_query_bo, false);
1242 dev_priv->dummy_query_bo_pinned = false;
1243}
1244
1245
1246/**
1247 * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
1248 * query bo.
1249 *
1250 * @dev_priv: The device private structure.
1251 * @only_on_cid_match: Only flush and unpin if the current active query cid
1252 * matches @cid.
1253 * @cid: Optional context id to match.
1254 *
1255 * This function should be used to unpin the pinned query bo, or
1256 * as a query barrier when we need to make sure that all queries have
1257 * finished before the next fifo command. (For example on hardware
1258 * context destructions where the hardware may otherwise leak unfinished
1259 * queries).
1260 *
1261 * This function does not return any failure codes, but make attempts
1262 * to do safe unpinning in case of errors.
1263 *
1264 * The function will synchronize on the previous query barrier, and will
1265 * thus not finish until that barrier has executed.
1266 */
1267void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
1268 bool only_on_cid_match, uint32_t cid)
1269{
1270 int ret = 0;
1271 struct list_head validate_list;
1272 struct ttm_validate_buffer pinned_val, query_val;
1273 struct vmw_fence_obj *fence;
1274
1275 mutex_lock(&dev_priv->cmdbuf_mutex);
1276
1277 if (dev_priv->pinned_bo == NULL)
1278 goto out_unlock;
1279
1280 if (only_on_cid_match && cid != dev_priv->query_cid)
1281 goto out_unlock;
1282
1283 INIT_LIST_HEAD(&validate_list);
1284
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001285 pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
1286 list_add_tail(&pinned_val.head, &validate_list);
1287
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +02001288 query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
1289 list_add_tail(&query_val.head, &validate_list);
1290
1291 do {
1292 ret = ttm_eu_reserve_buffers(&validate_list);
1293 } while (ret == -ERESTARTSYS);
1294
1295 if (unlikely(ret != 0)) {
1296 vmw_execbuf_unpin_panic(dev_priv);
1297 goto out_no_reserve;
1298 }
1299
1300 ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
1301 if (unlikely(ret != 0)) {
1302 vmw_execbuf_unpin_panic(dev_priv);
1303 goto out_no_emit;
1304 }
1305
1306 vmw_bo_pin(dev_priv->pinned_bo, false);
1307 vmw_bo_pin(dev_priv->dummy_query_bo, false);
1308 dev_priv->dummy_query_bo_pinned = false;
1309
1310 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1311 ttm_eu_fence_buffer_objects(&validate_list, (void *) fence);
1312
1313 ttm_bo_unref(&query_val.bo);
1314 ttm_bo_unref(&pinned_val.bo);
1315 ttm_bo_unref(&dev_priv->pinned_bo);
1316
1317out_unlock:
1318 mutex_unlock(&dev_priv->cmdbuf_mutex);
1319 return;
1320
1321out_no_emit:
1322 ttm_eu_backoff_reservation(&validate_list);
1323out_no_reserve:
1324 ttm_bo_unref(&query_val.bo);
1325 ttm_bo_unref(&pinned_val.bo);
1326 ttm_bo_unref(&dev_priv->pinned_bo);
1327 mutex_unlock(&dev_priv->cmdbuf_mutex);
1328}
1329
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001330
1331int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
1332 struct drm_file *file_priv)
1333{
1334 struct vmw_private *dev_priv = vmw_priv(dev);
1335 struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
1336 struct vmw_master *vmaster = vmw_master(file_priv->master);
1337 int ret;
1338
1339 /*
1340 * This will allow us to extend the ioctl argument while
1341 * maintaining backwards compatibility:
1342 * We take different code paths depending on the value of
1343 * arg->version.
1344 */
1345
1346 if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
1347 DRM_ERROR("Incorrect execbuf version.\n");
1348 DRM_ERROR("You're running outdated experimental "
1349 "vmwgfx user-space drivers.");
1350 return -EINVAL;
1351 }
1352
1353 ret = ttm_read_lock(&vmaster->lock, true);
1354 if (unlikely(ret != 0))
1355 return ret;
1356
1357 ret = vmw_execbuf_process(file_priv, dev_priv,
1358 (void __user *)(unsigned long)arg->commands,
1359 NULL, arg->command_size, arg->throttle_us,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001360 (void __user *)(unsigned long)arg->fence_rep,
1361 NULL);
Thomas Hellstrom922ade02011-10-04 20:13:17 +02001362
1363 if (unlikely(ret != 0))
1364 goto out_unlock;
1365
1366 vmw_kms_cursor_post_execbuf(dev_priv);
1367
1368out_unlock:
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001369 ttm_read_unlock(&vmaster->lock);
1370 return ret;
1371}