1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include "vmwgfx_reg.h"
30 #include "ttm/ttm_bo_api.h"
31 #include "ttm/ttm_placement.h"
33 static int vmw_cmd_invalid(struct vmw_private
*dev_priv
,
34 struct vmw_sw_context
*sw_context
,
35 SVGA3dCmdHeader
*header
)
37 return capable(CAP_SYS_ADMIN
) ? : -EINVAL
;
40 static int vmw_cmd_ok(struct vmw_private
*dev_priv
,
41 struct vmw_sw_context
*sw_context
,
42 SVGA3dCmdHeader
*header
)
47 static int vmw_cmd_cid_check(struct vmw_private
*dev_priv
,
48 struct vmw_sw_context
*sw_context
,
49 SVGA3dCmdHeader
*header
)
52 SVGA3dCmdHeader header
;
57 cmd
= container_of(header
, struct vmw_cid_cmd
, header
);
58 if (likely(sw_context
->cid_valid
&& cmd
->cid
== sw_context
->last_cid
))
61 ret
= vmw_context_check(dev_priv
, sw_context
->tfile
, cmd
->cid
);
62 if (unlikely(ret
!= 0)) {
63 DRM_ERROR("Could not find or use context %u\n",
68 sw_context
->last_cid
= cmd
->cid
;
69 sw_context
->cid_valid
= true;
74 static int vmw_cmd_sid_check(struct vmw_private
*dev_priv
,
75 struct vmw_sw_context
*sw_context
,
78 if (*sid
== SVGA3D_INVALID_ID
)
81 if (unlikely((!sw_context
->sid_valid
||
82 *sid
!= sw_context
->last_sid
))) {
84 int ret
= vmw_surface_check(dev_priv
, sw_context
->tfile
,
87 if (unlikely(ret
!= 0)) {
88 DRM_ERROR("Could ot find or use surface 0x%08x "
95 sw_context
->last_sid
= *sid
;
96 sw_context
->sid_valid
= true;
98 sw_context
->sid_translation
= real_id
;
100 *sid
= sw_context
->sid_translation
;
106 static int vmw_cmd_set_render_target_check(struct vmw_private
*dev_priv
,
107 struct vmw_sw_context
*sw_context
,
108 SVGA3dCmdHeader
*header
)
111 SVGA3dCmdHeader header
;
112 SVGA3dCmdSetRenderTarget body
;
116 ret
= vmw_cmd_cid_check(dev_priv
, sw_context
, header
);
117 if (unlikely(ret
!= 0))
120 cmd
= container_of(header
, struct vmw_sid_cmd
, header
);
121 ret
= vmw_cmd_sid_check(dev_priv
, sw_context
, &cmd
->body
.target
.sid
);
125 static int vmw_cmd_surface_copy_check(struct vmw_private
*dev_priv
,
126 struct vmw_sw_context
*sw_context
,
127 SVGA3dCmdHeader
*header
)
130 SVGA3dCmdHeader header
;
131 SVGA3dCmdSurfaceCopy body
;
135 cmd
= container_of(header
, struct vmw_sid_cmd
, header
);
136 ret
= vmw_cmd_sid_check(dev_priv
, sw_context
, &cmd
->body
.src
.sid
);
137 if (unlikely(ret
!= 0))
139 return vmw_cmd_sid_check(dev_priv
, sw_context
, &cmd
->body
.dest
.sid
);
142 static int vmw_cmd_stretch_blt_check(struct vmw_private
*dev_priv
,
143 struct vmw_sw_context
*sw_context
,
144 SVGA3dCmdHeader
*header
)
147 SVGA3dCmdHeader header
;
148 SVGA3dCmdSurfaceStretchBlt body
;
152 cmd
= container_of(header
, struct vmw_sid_cmd
, header
);
153 ret
= vmw_cmd_sid_check(dev_priv
, sw_context
, &cmd
->body
.src
.sid
);
154 if (unlikely(ret
!= 0))
156 return vmw_cmd_sid_check(dev_priv
, sw_context
, &cmd
->body
.dest
.sid
);
159 static int vmw_cmd_blt_surf_screen_check(struct vmw_private
*dev_priv
,
160 struct vmw_sw_context
*sw_context
,
161 SVGA3dCmdHeader
*header
)
164 SVGA3dCmdHeader header
;
165 SVGA3dCmdBlitSurfaceToScreen body
;
168 cmd
= container_of(header
, struct vmw_sid_cmd
, header
);
169 return vmw_cmd_sid_check(dev_priv
, sw_context
, &cmd
->body
.srcImage
.sid
);
172 static int vmw_cmd_present_check(struct vmw_private
*dev_priv
,
173 struct vmw_sw_context
*sw_context
,
174 SVGA3dCmdHeader
*header
)
177 SVGA3dCmdHeader header
;
178 SVGA3dCmdPresent body
;
181 cmd
= container_of(header
, struct vmw_sid_cmd
, header
);
182 return vmw_cmd_sid_check(dev_priv
, sw_context
, &cmd
->body
.sid
);
185 static int vmw_cmd_dma(struct vmw_private
*dev_priv
,
186 struct vmw_sw_context
*sw_context
,
187 SVGA3dCmdHeader
*header
)
190 struct vmw_dma_buffer
*vmw_bo
= NULL
;
191 struct ttm_buffer_object
*bo
;
192 struct vmw_surface
*srf
= NULL
;
194 SVGA3dCmdHeader header
;
195 SVGA3dCmdSurfaceDMA dma
;
197 struct vmw_relocation
*reloc
;
199 uint32_t cur_validate_node
;
200 struct ttm_validate_buffer
*val_buf
;
202 cmd
= container_of(header
, struct vmw_dma_cmd
, header
);
203 handle
= cmd
->dma
.guest
.ptr
.gmrId
;
204 ret
= vmw_user_dmabuf_lookup(sw_context
->tfile
, handle
, &vmw_bo
);
205 if (unlikely(ret
!= 0)) {
206 DRM_ERROR("Could not find or use GMR region.\n");
211 if (unlikely(sw_context
->cur_reloc
>= VMWGFX_MAX_RELOCATIONS
)) {
212 DRM_ERROR("Max number of DMA commands per submission"
218 reloc
= &sw_context
->relocs
[sw_context
->cur_reloc
++];
219 reloc
->location
= &cmd
->dma
.guest
.ptr
;
221 cur_validate_node
= vmw_dmabuf_validate_node(bo
, sw_context
->cur_val_buf
);
222 if (unlikely(cur_validate_node
>= VMWGFX_MAX_GMRS
)) {
223 DRM_ERROR("Max number of DMA buffers per submission"
229 reloc
->index
= cur_validate_node
;
230 if (unlikely(cur_validate_node
== sw_context
->cur_val_buf
)) {
231 val_buf
= &sw_context
->val_bufs
[cur_validate_node
];
232 val_buf
->bo
= ttm_bo_reference(bo
);
233 val_buf
->new_sync_obj_arg
= (void *) dev_priv
;
234 list_add_tail(&val_buf
->head
, &sw_context
->validate_nodes
);
235 ++sw_context
->cur_val_buf
;
238 ret
= vmw_user_surface_lookup_handle(dev_priv
, sw_context
->tfile
,
239 cmd
->dma
.host
.sid
, &srf
);
241 DRM_ERROR("could not find surface\n");
246 * Patch command stream with device SID.
249 cmd
->dma
.host
.sid
= srf
->res
.id
;
250 vmw_kms_cursor_snoop(srf
, sw_context
->tfile
, bo
, header
);
252 * FIXME: May deadlock here when called from the
253 * command parsing code.
255 vmw_surface_unreference(&srf
);
258 vmw_dmabuf_unreference(&vmw_bo
);
262 static int vmw_cmd_draw(struct vmw_private
*dev_priv
,
263 struct vmw_sw_context
*sw_context
,
264 SVGA3dCmdHeader
*header
)
266 struct vmw_draw_cmd
{
267 SVGA3dCmdHeader header
;
268 SVGA3dCmdDrawPrimitives body
;
270 SVGA3dVertexDecl
*decl
= (SVGA3dVertexDecl
*)(
271 (unsigned long)header
+ sizeof(*cmd
));
272 SVGA3dPrimitiveRange
*range
;
277 ret
= vmw_cmd_cid_check(dev_priv
, sw_context
, header
);
278 if (unlikely(ret
!= 0))
281 cmd
= container_of(header
, struct vmw_draw_cmd
, header
);
282 maxnum
= (header
->size
- sizeof(cmd
->body
)) / sizeof(*decl
);
284 if (unlikely(cmd
->body
.numVertexDecls
> maxnum
)) {
285 DRM_ERROR("Illegal number of vertex declarations.\n");
289 for (i
= 0; i
< cmd
->body
.numVertexDecls
; ++i
, ++decl
) {
290 ret
= vmw_cmd_sid_check(dev_priv
, sw_context
,
291 &decl
->array
.surfaceId
);
292 if (unlikely(ret
!= 0))
296 maxnum
= (header
->size
- sizeof(cmd
->body
) -
297 cmd
->body
.numVertexDecls
* sizeof(*decl
)) / sizeof(*range
);
298 if (unlikely(cmd
->body
.numRanges
> maxnum
)) {
299 DRM_ERROR("Illegal number of index ranges.\n");
303 range
= (SVGA3dPrimitiveRange
*) decl
;
304 for (i
= 0; i
< cmd
->body
.numRanges
; ++i
, ++range
) {
305 ret
= vmw_cmd_sid_check(dev_priv
, sw_context
,
306 &range
->indexArray
.surfaceId
);
307 if (unlikely(ret
!= 0))
314 static int vmw_cmd_tex_state(struct vmw_private
*dev_priv
,
315 struct vmw_sw_context
*sw_context
,
316 SVGA3dCmdHeader
*header
)
318 struct vmw_tex_state_cmd
{
319 SVGA3dCmdHeader header
;
320 SVGA3dCmdSetTextureState state
;
323 SVGA3dTextureState
*last_state
= (SVGA3dTextureState
*)
324 ((unsigned long) header
+ header
->size
+ sizeof(header
));
325 SVGA3dTextureState
*cur_state
= (SVGA3dTextureState
*)
326 ((unsigned long) header
+ sizeof(struct vmw_tex_state_cmd
));
329 ret
= vmw_cmd_cid_check(dev_priv
, sw_context
, header
);
330 if (unlikely(ret
!= 0))
333 for (; cur_state
< last_state
; ++cur_state
) {
334 if (likely(cur_state
->name
!= SVGA3D_TS_BIND_TEXTURE
))
337 ret
= vmw_cmd_sid_check(dev_priv
, sw_context
,
339 if (unlikely(ret
!= 0))
347 typedef int (*vmw_cmd_func
) (struct vmw_private
*,
348 struct vmw_sw_context
*,
351 #define VMW_CMD_DEF(cmd, func) \
352 [cmd - SVGA_3D_CMD_BASE] = func
354 static vmw_cmd_func vmw_cmd_funcs
[SVGA_3D_CMD_MAX
] = {
355 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE
, &vmw_cmd_invalid
),
356 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY
, &vmw_cmd_invalid
),
357 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY
, &vmw_cmd_surface_copy_check
),
358 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT
, &vmw_cmd_stretch_blt_check
),
359 VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA
, &vmw_cmd_dma
),
360 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE
, &vmw_cmd_invalid
),
361 VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY
, &vmw_cmd_invalid
),
362 VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM
, &vmw_cmd_cid_check
),
363 VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE
, &vmw_cmd_cid_check
),
364 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE
, &vmw_cmd_cid_check
),
365 VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET
,
366 &vmw_cmd_set_render_target_check
),
367 VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE
, &vmw_cmd_tex_state
),
368 VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL
, &vmw_cmd_cid_check
),
369 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA
, &vmw_cmd_cid_check
),
370 VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED
, &vmw_cmd_cid_check
),
371 VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT
, &vmw_cmd_cid_check
),
372 VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE
, &vmw_cmd_cid_check
),
373 VMW_CMD_DEF(SVGA_3D_CMD_CLEAR
, &vmw_cmd_cid_check
),
374 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT
, &vmw_cmd_present_check
),
375 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE
, &vmw_cmd_cid_check
),
376 VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY
, &vmw_cmd_cid_check
),
377 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER
, &vmw_cmd_cid_check
),
378 VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST
, &vmw_cmd_cid_check
),
379 VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES
, &vmw_cmd_draw
),
380 VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT
, &vmw_cmd_cid_check
),
381 VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY
, &vmw_cmd_cid_check
),
382 VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY
, &vmw_cmd_cid_check
),
383 VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY
, &vmw_cmd_cid_check
),
384 VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK
, &vmw_cmd_ok
),
385 VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN
,
386 &vmw_cmd_blt_surf_screen_check
)
389 static int vmw_cmd_check(struct vmw_private
*dev_priv
,
390 struct vmw_sw_context
*sw_context
,
391 void *buf
, uint32_t *size
)
394 uint32_t size_remaining
= *size
;
395 SVGA3dCmdHeader
*header
= (SVGA3dCmdHeader
*) buf
;
398 cmd_id
= ((uint32_t *)buf
)[0];
399 if (cmd_id
== SVGA_CMD_UPDATE
) {
404 cmd_id
= le32_to_cpu(header
->id
);
405 *size
= le32_to_cpu(header
->size
) + sizeof(SVGA3dCmdHeader
);
407 cmd_id
-= SVGA_3D_CMD_BASE
;
408 if (unlikely(*size
> size_remaining
))
411 if (unlikely(cmd_id
>= SVGA_3D_CMD_MAX
- SVGA_3D_CMD_BASE
))
414 ret
= vmw_cmd_funcs
[cmd_id
](dev_priv
, sw_context
, header
);
415 if (unlikely(ret
!= 0))
420 DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
421 cmd_id
+ SVGA_3D_CMD_BASE
);
425 static int vmw_cmd_check_all(struct vmw_private
*dev_priv
,
426 struct vmw_sw_context
*sw_context
,
427 void *buf
, uint32_t size
)
429 int32_t cur_size
= size
;
432 while (cur_size
> 0) {
434 ret
= vmw_cmd_check(dev_priv
, sw_context
, buf
, &size
);
435 if (unlikely(ret
!= 0))
437 buf
= (void *)((unsigned long) buf
+ size
);
441 if (unlikely(cur_size
!= 0)) {
442 DRM_ERROR("Command verifier out of sync.\n");
449 static void vmw_free_relocations(struct vmw_sw_context
*sw_context
)
451 sw_context
->cur_reloc
= 0;
454 static void vmw_apply_relocations(struct vmw_sw_context
*sw_context
)
457 struct vmw_relocation
*reloc
;
458 struct ttm_validate_buffer
*validate
;
459 struct ttm_buffer_object
*bo
;
461 for (i
= 0; i
< sw_context
->cur_reloc
; ++i
) {
462 reloc
= &sw_context
->relocs
[i
];
463 validate
= &sw_context
->val_bufs
[reloc
->index
];
465 reloc
->location
->offset
+= bo
->offset
;
466 reloc
->location
->gmrId
= vmw_dmabuf_gmr(bo
);
468 vmw_free_relocations(sw_context
);
471 static void vmw_clear_validations(struct vmw_sw_context
*sw_context
)
473 struct ttm_validate_buffer
*entry
, *next
;
475 list_for_each_entry_safe(entry
, next
, &sw_context
->validate_nodes
,
477 list_del(&entry
->head
);
478 vmw_dmabuf_validate_clear(entry
->bo
);
479 ttm_bo_unref(&entry
->bo
);
480 sw_context
->cur_val_buf
--;
482 BUG_ON(sw_context
->cur_val_buf
!= 0);
485 static int vmw_validate_single_buffer(struct vmw_private
*dev_priv
,
486 struct ttm_buffer_object
*bo
)
490 if (vmw_dmabuf_gmr(bo
) != SVGA_GMR_NULL
)
494 * Put BO in VRAM, only if there is space.
497 ret
= ttm_bo_validate(bo
, &vmw_vram_sys_placement
, true, false);
498 if (unlikely(ret
== -ERESTARTSYS
))
502 * Otherwise, set it up as GMR.
505 if (vmw_dmabuf_gmr(bo
) != SVGA_GMR_NULL
)
508 ret
= vmw_gmr_bind(dev_priv
, bo
);
509 if (likely(ret
== 0 || ret
== -ERESTARTSYS
))
513 * If that failed, try VRAM again, this time evicting
517 ret
= ttm_bo_validate(bo
, &vmw_vram_placement
, true, false);
522 static int vmw_validate_buffers(struct vmw_private
*dev_priv
,
523 struct vmw_sw_context
*sw_context
)
525 struct ttm_validate_buffer
*entry
;
528 list_for_each_entry(entry
, &sw_context
->validate_nodes
, head
) {
529 ret
= vmw_validate_single_buffer(dev_priv
, entry
->bo
);
530 if (unlikely(ret
!= 0))
536 int vmw_execbuf_ioctl(struct drm_device
*dev
, void *data
,
537 struct drm_file
*file_priv
)
539 struct vmw_private
*dev_priv
= vmw_priv(dev
);
540 struct drm_vmw_execbuf_arg
*arg
= (struct drm_vmw_execbuf_arg
*)data
;
541 struct drm_vmw_fence_rep fence_rep
;
542 struct drm_vmw_fence_rep __user
*user_fence_rep
;
547 struct vmw_sw_context
*sw_context
= &dev_priv
->ctx
;
548 struct vmw_master
*vmaster
= vmw_master(file_priv
->master
);
550 ret
= ttm_read_lock(&vmaster
->lock
, true);
551 if (unlikely(ret
!= 0))
554 ret
= mutex_lock_interruptible(&dev_priv
->cmdbuf_mutex
);
555 if (unlikely(ret
!= 0)) {
557 goto out_no_cmd_mutex
;
560 cmd
= vmw_fifo_reserve(dev_priv
, arg
->command_size
);
561 if (unlikely(cmd
== NULL
)) {
562 DRM_ERROR("Failed reserving fifo space for commands.\n");
567 user_cmd
= (void __user
*)(unsigned long)arg
->commands
;
568 ret
= copy_from_user(cmd
, user_cmd
, arg
->command_size
);
570 if (unlikely(ret
!= 0)) {
571 DRM_ERROR("Failed copying commands.\n");
575 sw_context
->tfile
= vmw_fpriv(file_priv
)->tfile
;
576 sw_context
->cid_valid
= false;
577 sw_context
->sid_valid
= false;
578 sw_context
->cur_reloc
= 0;
579 sw_context
->cur_val_buf
= 0;
581 INIT_LIST_HEAD(&sw_context
->validate_nodes
);
583 ret
= vmw_cmd_check_all(dev_priv
, sw_context
, cmd
, arg
->command_size
);
584 if (unlikely(ret
!= 0))
586 ret
= ttm_eu_reserve_buffers(&sw_context
->validate_nodes
,
587 dev_priv
->val_seq
++);
588 if (unlikely(ret
!= 0))
591 ret
= vmw_validate_buffers(dev_priv
, sw_context
);
592 if (unlikely(ret
!= 0))
595 vmw_apply_relocations(sw_context
);
596 vmw_fifo_commit(dev_priv
, arg
->command_size
);
598 ret
= vmw_fifo_send_fence(dev_priv
, &sequence
);
600 ttm_eu_fence_buffer_objects(&sw_context
->validate_nodes
,
601 (void *)(unsigned long) sequence
);
602 vmw_clear_validations(sw_context
);
603 mutex_unlock(&dev_priv
->cmdbuf_mutex
);
606 * This error is harmless, because if fence submission fails,
607 * vmw_fifo_send_fence will sync.
611 DRM_ERROR("Fence submission error. Syncing.\n");
613 fence_rep
.error
= ret
;
614 fence_rep
.fence_seq
= (uint64_t) sequence
;
616 user_fence_rep
= (struct drm_vmw_fence_rep __user
*)
617 (unsigned long)arg
->fence_rep
;
620 * copy_to_user errors will be detected by user space not
621 * seeing fence_rep::error filled in.
624 ret
= copy_to_user(user_fence_rep
, &fence_rep
, sizeof(fence_rep
));
626 vmw_kms_cursor_post_execbuf(dev_priv
);
627 ttm_read_unlock(&vmaster
->lock
);
630 vmw_free_relocations(sw_context
);
631 ttm_eu_backoff_reservation(&sw_context
->validate_nodes
);
632 vmw_clear_validations(sw_context
);
634 vmw_fifo_commit(dev_priv
, 0);
636 mutex_unlock(&dev_priv
->cmdbuf_mutex
);
638 ttm_read_unlock(&vmaster
->lock
);