2 * Copyright (C) 2015 Red Hat, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
29 #include <drm/virtgpu_drm.h>
30 #include <drm/ttm/ttm_execbuf_util.h>
32 #include "virtgpu_drv.h"
34 static void convert_to_hw_box(struct virtio_gpu_box
*dst
,
35 const struct drm_virtgpu_3d_box
*src
)
37 dst
->x
= cpu_to_le32(src
->x
);
38 dst
->y
= cpu_to_le32(src
->y
);
39 dst
->z
= cpu_to_le32(src
->z
);
40 dst
->w
= cpu_to_le32(src
->w
);
41 dst
->h
= cpu_to_le32(src
->h
);
42 dst
->d
= cpu_to_le32(src
->d
);
45 static int virtio_gpu_map_ioctl(struct drm_device
*dev
, void *data
,
46 struct drm_file
*file_priv
)
48 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
49 struct drm_virtgpu_map
*virtio_gpu_map
= data
;
51 return virtio_gpu_mode_dumb_mmap(file_priv
, vgdev
->ddev
,
52 virtio_gpu_map
->handle
,
53 &virtio_gpu_map
->offset
);
56 static int virtio_gpu_object_list_validate(struct ww_acquire_ctx
*ticket
,
57 struct list_head
*head
)
59 struct ttm_operation_ctx ctx
= { false, false };
60 struct ttm_validate_buffer
*buf
;
61 struct ttm_buffer_object
*bo
;
62 struct virtio_gpu_object
*qobj
;
65 ret
= ttm_eu_reserve_buffers(ticket
, head
, true, NULL
);
69 list_for_each_entry(buf
, head
, head
) {
71 qobj
= container_of(bo
, struct virtio_gpu_object
, tbo
);
72 ret
= ttm_bo_validate(bo
, &qobj
->placement
, &ctx
);
74 ttm_eu_backoff_reservation(ticket
, head
);
81 static void virtio_gpu_unref_list(struct list_head
*head
)
83 struct ttm_validate_buffer
*buf
;
84 struct ttm_buffer_object
*bo
;
85 struct virtio_gpu_object
*qobj
;
86 list_for_each_entry(buf
, head
, head
) {
88 qobj
= container_of(bo
, struct virtio_gpu_object
, tbo
);
90 drm_gem_object_put_unlocked(&qobj
->gem_base
);
95 * Usage of execbuffer:
96 * Relocations need to take into account the full VIRTIO_GPUDrawable size.
97 * However, the command as passed from user space must *not* contain the initial
98 * VIRTIO_GPUReleaseInfo struct (first XXX bytes)
100 static int virtio_gpu_execbuffer_ioctl(struct drm_device
*dev
, void *data
,
101 struct drm_file
*drm_file
)
103 struct drm_virtgpu_execbuffer
*exbuf
= data
;
104 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
105 struct virtio_gpu_fpriv
*vfpriv
= drm_file
->driver_priv
;
106 struct drm_gem_object
*gobj
;
107 struct virtio_gpu_fence
*fence
;
108 struct virtio_gpu_object
*qobj
;
110 uint32_t *bo_handles
= NULL
;
111 void __user
*user_bo_handles
= NULL
;
112 struct list_head validate_list
;
113 struct ttm_validate_buffer
*buflist
= NULL
;
115 struct ww_acquire_ctx ticket
;
118 if (vgdev
->has_virgl_3d
== false)
121 INIT_LIST_HEAD(&validate_list
);
122 if (exbuf
->num_bo_handles
) {
124 bo_handles
= kvmalloc_array(exbuf
->num_bo_handles
,
125 sizeof(uint32_t), GFP_KERNEL
);
126 buflist
= kvmalloc_array(exbuf
->num_bo_handles
,
127 sizeof(struct ttm_validate_buffer
),
128 GFP_KERNEL
| __GFP_ZERO
);
129 if (!bo_handles
|| !buflist
) {
135 user_bo_handles
= (void __user
*)(uintptr_t)exbuf
->bo_handles
;
136 if (copy_from_user(bo_handles
, user_bo_handles
,
137 exbuf
->num_bo_handles
* sizeof(uint32_t))) {
144 for (i
= 0; i
< exbuf
->num_bo_handles
; i
++) {
145 gobj
= drm_gem_object_lookup(drm_file
, bo_handles
[i
]);
152 qobj
= gem_to_virtio_gpu_obj(gobj
);
153 buflist
[i
].bo
= &qobj
->tbo
;
155 list_add(&buflist
[i
].head
, &validate_list
);
160 ret
= virtio_gpu_object_list_validate(&ticket
, &validate_list
);
164 buf
= memdup_user((void __user
*)(uintptr_t)exbuf
->command
,
170 virtio_gpu_cmd_submit(vgdev
, buf
, exbuf
->size
,
171 vfpriv
->ctx_id
, &fence
);
173 ttm_eu_fence_buffer_objects(&ticket
, &validate_list
, &fence
->f
);
175 /* fence the command bo */
176 virtio_gpu_unref_list(&validate_list
);
178 dma_fence_put(&fence
->f
);
182 ttm_eu_backoff_reservation(&ticket
, &validate_list
);
184 virtio_gpu_unref_list(&validate_list
);
189 static int virtio_gpu_getparam_ioctl(struct drm_device
*dev
, void *data
,
190 struct drm_file
*file_priv
)
192 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
193 struct drm_virtgpu_getparam
*param
= data
;
196 switch (param
->param
) {
197 case VIRTGPU_PARAM_3D_FEATURES
:
198 value
= vgdev
->has_virgl_3d
== true ? 1 : 0;
203 if (copy_to_user((void __user
*)(unsigned long)param
->value
,
204 &value
, sizeof(int))) {
210 static int virtio_gpu_resource_create_ioctl(struct drm_device
*dev
, void *data
,
211 struct drm_file
*file_priv
)
213 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
214 struct drm_virtgpu_resource_create
*rc
= data
;
217 struct virtio_gpu_object
*qobj
;
218 struct drm_gem_object
*obj
;
221 struct list_head validate_list
;
222 struct ttm_validate_buffer mainbuf
;
223 struct virtio_gpu_fence
*fence
= NULL
;
224 struct ww_acquire_ctx ticket
;
225 struct virtio_gpu_resource_create_3d rc_3d
;
227 if (vgdev
->has_virgl_3d
== false) {
230 if (rc
->nr_samples
> 1)
232 if (rc
->last_level
> 1)
236 if (rc
->array_size
> 1)
240 INIT_LIST_HEAD(&validate_list
);
241 memset(&mainbuf
, 0, sizeof(struct ttm_validate_buffer
));
243 virtio_gpu_resource_id_get(vgdev
, &res_id
);
247 /* allocate a single page size object */
251 qobj
= virtio_gpu_alloc_object(dev
, size
, false, false);
256 obj
= &qobj
->gem_base
;
258 if (!vgdev
->has_virgl_3d
) {
259 virtio_gpu_cmd_create_resource(vgdev
, res_id
, rc
->format
,
260 rc
->width
, rc
->height
);
262 ret
= virtio_gpu_object_attach(vgdev
, qobj
, res_id
, NULL
);
264 /* use a gem reference since unref list undoes them */
265 drm_gem_object_get(&qobj
->gem_base
);
266 mainbuf
.bo
= &qobj
->tbo
;
267 list_add(&mainbuf
.head
, &validate_list
);
269 ret
= virtio_gpu_object_list_validate(&ticket
, &validate_list
);
271 DRM_DEBUG("failed to validate\n");
275 rc_3d
.resource_id
= cpu_to_le32(res_id
);
276 rc_3d
.target
= cpu_to_le32(rc
->target
);
277 rc_3d
.format
= cpu_to_le32(rc
->format
);
278 rc_3d
.bind
= cpu_to_le32(rc
->bind
);
279 rc_3d
.width
= cpu_to_le32(rc
->width
);
280 rc_3d
.height
= cpu_to_le32(rc
->height
);
281 rc_3d
.depth
= cpu_to_le32(rc
->depth
);
282 rc_3d
.array_size
= cpu_to_le32(rc
->array_size
);
283 rc_3d
.last_level
= cpu_to_le32(rc
->last_level
);
284 rc_3d
.nr_samples
= cpu_to_le32(rc
->nr_samples
);
285 rc_3d
.flags
= cpu_to_le32(rc
->flags
);
287 virtio_gpu_cmd_resource_create_3d(vgdev
, &rc_3d
, NULL
);
288 ret
= virtio_gpu_object_attach(vgdev
, qobj
, res_id
, &fence
);
290 ttm_eu_backoff_reservation(&ticket
, &validate_list
);
293 ttm_eu_fence_buffer_objects(&ticket
, &validate_list
, &fence
->f
);
296 qobj
->hw_res_handle
= res_id
;
298 ret
= drm_gem_handle_create(file_priv
, obj
, &handle
);
301 drm_gem_object_release(obj
);
302 if (vgdev
->has_virgl_3d
) {
303 virtio_gpu_unref_list(&validate_list
);
304 dma_fence_put(&fence
->f
);
308 drm_gem_object_put_unlocked(obj
);
310 rc
->res_handle
= res_id
; /* similiar to a VM address */
311 rc
->bo_handle
= handle
;
313 if (vgdev
->has_virgl_3d
) {
314 virtio_gpu_unref_list(&validate_list
);
315 dma_fence_put(&fence
->f
);
319 if (vgdev
->has_virgl_3d
) {
320 virtio_gpu_unref_list(&validate_list
);
321 dma_fence_put(&fence
->f
);
324 // drm_gem_object_handle_unreference_unlocked(obj);
326 virtio_gpu_resource_id_put(vgdev
, res_id
);
330 static int virtio_gpu_resource_info_ioctl(struct drm_device
*dev
, void *data
,
331 struct drm_file
*file_priv
)
333 struct drm_virtgpu_resource_info
*ri
= data
;
334 struct drm_gem_object
*gobj
= NULL
;
335 struct virtio_gpu_object
*qobj
= NULL
;
337 gobj
= drm_gem_object_lookup(file_priv
, ri
->bo_handle
);
341 qobj
= gem_to_virtio_gpu_obj(gobj
);
343 ri
->size
= qobj
->gem_base
.size
;
344 ri
->res_handle
= qobj
->hw_res_handle
;
345 drm_gem_object_put_unlocked(gobj
);
349 static int virtio_gpu_transfer_from_host_ioctl(struct drm_device
*dev
,
351 struct drm_file
*file
)
353 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
354 struct virtio_gpu_fpriv
*vfpriv
= file
->driver_priv
;
355 struct drm_virtgpu_3d_transfer_from_host
*args
= data
;
356 struct ttm_operation_ctx ctx
= { true, false };
357 struct drm_gem_object
*gobj
= NULL
;
358 struct virtio_gpu_object
*qobj
= NULL
;
359 struct virtio_gpu_fence
*fence
;
361 u32 offset
= args
->offset
;
362 struct virtio_gpu_box box
;
364 if (vgdev
->has_virgl_3d
== false)
367 gobj
= drm_gem_object_lookup(file
, args
->bo_handle
);
371 qobj
= gem_to_virtio_gpu_obj(gobj
);
373 ret
= virtio_gpu_object_reserve(qobj
, false);
377 ret
= ttm_bo_validate(&qobj
->tbo
, &qobj
->placement
, &ctx
);
381 convert_to_hw_box(&box
, &args
->box
);
382 virtio_gpu_cmd_transfer_from_host_3d
383 (vgdev
, qobj
->hw_res_handle
,
384 vfpriv
->ctx_id
, offset
, args
->level
,
386 reservation_object_add_excl_fence(qobj
->tbo
.resv
,
389 dma_fence_put(&fence
->f
);
391 virtio_gpu_object_unreserve(qobj
);
393 drm_gem_object_put_unlocked(gobj
);
397 static int virtio_gpu_transfer_to_host_ioctl(struct drm_device
*dev
, void *data
,
398 struct drm_file
*file
)
400 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
401 struct virtio_gpu_fpriv
*vfpriv
= file
->driver_priv
;
402 struct drm_virtgpu_3d_transfer_to_host
*args
= data
;
403 struct ttm_operation_ctx ctx
= { true, false };
404 struct drm_gem_object
*gobj
= NULL
;
405 struct virtio_gpu_object
*qobj
= NULL
;
406 struct virtio_gpu_fence
*fence
;
407 struct virtio_gpu_box box
;
409 u32 offset
= args
->offset
;
411 gobj
= drm_gem_object_lookup(file
, args
->bo_handle
);
415 qobj
= gem_to_virtio_gpu_obj(gobj
);
417 ret
= virtio_gpu_object_reserve(qobj
, false);
421 ret
= ttm_bo_validate(&qobj
->tbo
, &qobj
->placement
, &ctx
);
425 convert_to_hw_box(&box
, &args
->box
);
426 if (!vgdev
->has_virgl_3d
) {
427 virtio_gpu_cmd_transfer_to_host_2d
428 (vgdev
, qobj
->hw_res_handle
, offset
,
429 box
.w
, box
.h
, box
.x
, box
.y
, NULL
);
431 virtio_gpu_cmd_transfer_to_host_3d
432 (vgdev
, qobj
->hw_res_handle
,
433 vfpriv
? vfpriv
->ctx_id
: 0, offset
,
434 args
->level
, &box
, &fence
);
435 reservation_object_add_excl_fence(qobj
->tbo
.resv
,
437 dma_fence_put(&fence
->f
);
441 virtio_gpu_object_unreserve(qobj
);
443 drm_gem_object_put_unlocked(gobj
);
447 static int virtio_gpu_wait_ioctl(struct drm_device
*dev
, void *data
,
448 struct drm_file
*file
)
450 struct drm_virtgpu_3d_wait
*args
= data
;
451 struct drm_gem_object
*gobj
= NULL
;
452 struct virtio_gpu_object
*qobj
= NULL
;
456 gobj
= drm_gem_object_lookup(file
, args
->handle
);
460 qobj
= gem_to_virtio_gpu_obj(gobj
);
462 if (args
->flags
& VIRTGPU_WAIT_NOWAIT
)
464 ret
= virtio_gpu_object_wait(qobj
, nowait
);
466 drm_gem_object_put_unlocked(gobj
);
470 static int virtio_gpu_get_caps_ioctl(struct drm_device
*dev
,
471 void *data
, struct drm_file
*file
)
473 struct virtio_gpu_device
*vgdev
= dev
->dev_private
;
474 struct drm_virtgpu_get_caps
*args
= data
;
477 int found_valid
= -1;
479 struct virtio_gpu_drv_cap_cache
*cache_ent
;
481 if (vgdev
->num_capsets
== 0)
484 spin_lock(&vgdev
->display_info_lock
);
485 for (i
= 0; i
< vgdev
->num_capsets
; i
++) {
486 if (vgdev
->capsets
[i
].id
== args
->cap_set_id
) {
487 if (vgdev
->capsets
[i
].max_version
>= args
->cap_set_ver
) {
494 if (found_valid
== -1) {
495 spin_unlock(&vgdev
->display_info_lock
);
499 size
= vgdev
->capsets
[found_valid
].max_size
;
500 if (args
->size
> size
) {
501 spin_unlock(&vgdev
->display_info_lock
);
505 list_for_each_entry(cache_ent
, &vgdev
->cap_cache
, head
) {
506 if (cache_ent
->id
== args
->cap_set_id
&&
507 cache_ent
->version
== args
->cap_set_ver
) {
508 ptr
= cache_ent
->caps_cache
;
509 spin_unlock(&vgdev
->display_info_lock
);
513 spin_unlock(&vgdev
->display_info_lock
);
515 /* not in cache - need to talk to hw */
516 virtio_gpu_cmd_get_capset(vgdev
, found_valid
, args
->cap_set_ver
,
519 ret
= wait_event_timeout(vgdev
->resp_wq
,
520 atomic_read(&cache_ent
->is_valid
), 5 * HZ
);
522 ptr
= cache_ent
->caps_cache
;
525 if (copy_to_user((void __user
*)(unsigned long)args
->addr
, ptr
, size
))
531 struct drm_ioctl_desc virtio_gpu_ioctls
[DRM_VIRTIO_NUM_IOCTLS
] = {
532 DRM_IOCTL_DEF_DRV(VIRTGPU_MAP
, virtio_gpu_map_ioctl
,
533 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
535 DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER
, virtio_gpu_execbuffer_ioctl
,
536 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
538 DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM
, virtio_gpu_getparam_ioctl
,
539 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
541 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE
,
542 virtio_gpu_resource_create_ioctl
,
543 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
545 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO
, virtio_gpu_resource_info_ioctl
,
546 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
548 /* make transfer async to the main ring? - no sure, can we
549 thread these in the underlying GL */
550 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST
,
551 virtio_gpu_transfer_from_host_ioctl
,
552 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
553 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST
,
554 virtio_gpu_transfer_to_host_ioctl
,
555 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
557 DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT
, virtio_gpu_wait_ioctl
,
558 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),
560 DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS
, virtio_gpu_get_caps_ioctl
,
561 DRM_AUTH
|DRM_UNLOCKED
|DRM_RENDER_ALLOW
),