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;
200 case VIRTGPU_PARAM_CAPSET_QUERY_FIX:
206 if (copy_to_user((void __user *)(unsigned long)param->value,
207 &value, sizeof(int))) {
213 static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data,
214 struct drm_file *file_priv)
216 struct virtio_gpu_device *vgdev = dev->dev_private;
217 struct drm_virtgpu_resource_create *rc = data;
220 struct virtio_gpu_object *qobj;
221 struct drm_gem_object *obj;
224 struct list_head validate_list;
225 struct ttm_validate_buffer mainbuf;
226 struct virtio_gpu_fence *fence = NULL;
227 struct ww_acquire_ctx ticket;
228 struct virtio_gpu_resource_create_3d rc_3d;
230 if (vgdev->has_virgl_3d == false) {
233 if (rc->nr_samples > 1)
235 if (rc->last_level > 1)
239 if (rc->array_size > 1)
243 INIT_LIST_HEAD(&validate_list);
244 memset(&mainbuf, 0, sizeof(struct ttm_validate_buffer));
246 virtio_gpu_resource_id_get(vgdev, &res_id);
250 /* allocate a single page size object */
254 qobj = virtio_gpu_alloc_object(dev, size, false, false);
259 obj = &qobj->gem_base;
261 if (!vgdev->has_virgl_3d) {
262 virtio_gpu_cmd_create_resource(vgdev, res_id, rc->format,
263 rc->width, rc->height);
265 ret = virtio_gpu_object_attach(vgdev, qobj, res_id, NULL);
267 /* use a gem reference since unref list undoes them */
268 drm_gem_object_get(&qobj->gem_base);
269 mainbuf.bo = &qobj->tbo;
270 list_add(&mainbuf.head, &validate_list);
272 ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
274 DRM_DEBUG("failed to validate\n");
278 rc_3d.resource_id = cpu_to_le32(res_id);
279 rc_3d.target = cpu_to_le32(rc->target);
280 rc_3d.format = cpu_to_le32(rc->format);
281 rc_3d.bind = cpu_to_le32(rc->bind);
282 rc_3d.width = cpu_to_le32(rc->width);
283 rc_3d.height = cpu_to_le32(rc->height);
284 rc_3d.depth = cpu_to_le32(rc->depth);
285 rc_3d.array_size = cpu_to_le32(rc->array_size);
286 rc_3d.last_level = cpu_to_le32(rc->last_level);
287 rc_3d.nr_samples = cpu_to_le32(rc->nr_samples);
288 rc_3d.flags = cpu_to_le32(rc->flags);
290 virtio_gpu_cmd_resource_create_3d(vgdev, &rc_3d, NULL);
291 ret = virtio_gpu_object_attach(vgdev, qobj, res_id, &fence);
293 ttm_eu_backoff_reservation(&ticket, &validate_list);
296 ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
299 qobj->hw_res_handle = res_id;
301 ret = drm_gem_handle_create(file_priv, obj, &handle);
304 drm_gem_object_release(obj);
305 if (vgdev->has_virgl_3d) {
306 virtio_gpu_unref_list(&validate_list);
307 dma_fence_put(&fence->f);
311 drm_gem_object_put_unlocked(obj);
313 rc->res_handle = res_id; /* similiar to a VM address */
314 rc->bo_handle = handle;
316 if (vgdev->has_virgl_3d) {
317 virtio_gpu_unref_list(&validate_list);
318 dma_fence_put(&fence->f);
322 if (vgdev->has_virgl_3d) {
323 virtio_gpu_unref_list(&validate_list);
324 dma_fence_put(&fence->f);
327 // drm_gem_object_handle_unreference_unlocked(obj);
329 virtio_gpu_resource_id_put(vgdev, res_id);
333 static int virtio_gpu_resource_info_ioctl(struct drm_device *dev, void *data,
334 struct drm_file *file_priv)
336 struct drm_virtgpu_resource_info *ri = data;
337 struct drm_gem_object *gobj = NULL;
338 struct virtio_gpu_object *qobj = NULL;
340 gobj = drm_gem_object_lookup(file_priv, ri->bo_handle);
344 qobj = gem_to_virtio_gpu_obj(gobj);
346 ri->size = qobj->gem_base.size;
347 ri->res_handle = qobj->hw_res_handle;
348 drm_gem_object_put_unlocked(gobj);
352 static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev,
354 struct drm_file *file)
356 struct virtio_gpu_device *vgdev = dev->dev_private;
357 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
358 struct drm_virtgpu_3d_transfer_from_host *args = data;
359 struct ttm_operation_ctx ctx = { true, false };
360 struct drm_gem_object *gobj = NULL;
361 struct virtio_gpu_object *qobj = NULL;
362 struct virtio_gpu_fence *fence;
364 u32 offset = args->offset;
365 struct virtio_gpu_box box;
367 if (vgdev->has_virgl_3d == false)
370 gobj = drm_gem_object_lookup(file, args->bo_handle);
374 qobj = gem_to_virtio_gpu_obj(gobj);
376 ret = virtio_gpu_object_reserve(qobj, false);
380 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
384 convert_to_hw_box(&box, &args->box);
385 virtio_gpu_cmd_transfer_from_host_3d
386 (vgdev, qobj->hw_res_handle,
387 vfpriv->ctx_id, offset, args->level,
389 reservation_object_add_excl_fence(qobj->tbo.resv,
392 dma_fence_put(&fence->f);
394 virtio_gpu_object_unreserve(qobj);
396 drm_gem_object_put_unlocked(gobj);
400 static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data,
401 struct drm_file *file)
403 struct virtio_gpu_device *vgdev = dev->dev_private;
404 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
405 struct drm_virtgpu_3d_transfer_to_host *args = data;
406 struct ttm_operation_ctx ctx = { true, false };
407 struct drm_gem_object *gobj = NULL;
408 struct virtio_gpu_object *qobj = NULL;
409 struct virtio_gpu_fence *fence;
410 struct virtio_gpu_box box;
412 u32 offset = args->offset;
414 gobj = drm_gem_object_lookup(file, args->bo_handle);
418 qobj = gem_to_virtio_gpu_obj(gobj);
420 ret = virtio_gpu_object_reserve(qobj, false);
424 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
428 convert_to_hw_box(&box, &args->box);
429 if (!vgdev->has_virgl_3d) {
430 virtio_gpu_cmd_transfer_to_host_2d
431 (vgdev, qobj->hw_res_handle, offset,
432 box.w, box.h, box.x, box.y, NULL);
434 virtio_gpu_cmd_transfer_to_host_3d
435 (vgdev, qobj->hw_res_handle,
436 vfpriv ? vfpriv->ctx_id : 0, offset,
437 args->level, &box, &fence);
438 reservation_object_add_excl_fence(qobj->tbo.resv,
440 dma_fence_put(&fence->f);
444 virtio_gpu_object_unreserve(qobj);
446 drm_gem_object_put_unlocked(gobj);
450 static int virtio_gpu_wait_ioctl(struct drm_device *dev, void *data,
451 struct drm_file *file)
453 struct drm_virtgpu_3d_wait *args = data;
454 struct drm_gem_object *gobj = NULL;
455 struct virtio_gpu_object *qobj = NULL;
459 gobj = drm_gem_object_lookup(file, args->handle);
463 qobj = gem_to_virtio_gpu_obj(gobj);
465 if (args->flags & VIRTGPU_WAIT_NOWAIT)
467 ret = virtio_gpu_object_wait(qobj, nowait);
469 drm_gem_object_put_unlocked(gobj);
473 static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,
474 void *data, struct drm_file *file)
476 struct virtio_gpu_device *vgdev = dev->dev_private;
477 struct drm_virtgpu_get_caps *args = data;
478 unsigned size, host_caps_size;
480 int found_valid = -1;
482 struct virtio_gpu_drv_cap_cache *cache_ent;
484 if (vgdev->num_capsets == 0)
487 /* don't allow userspace to pass 0 */
491 spin_lock(&vgdev->display_info_lock);
492 for (i = 0; i < vgdev->num_capsets; i++) {
493 if (vgdev->capsets[i].id == args->cap_set_id) {
494 if (vgdev->capsets[i].max_version >= args->cap_set_ver) {
501 if (found_valid == -1) {
502 spin_unlock(&vgdev->display_info_lock);
506 host_caps_size = vgdev->capsets[found_valid].max_size;
507 /* only copy to user the minimum of the host caps size or the guest caps size */
508 size = min(args->size, host_caps_size);
510 list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
511 if (cache_ent->id == args->cap_set_id &&
512 cache_ent->version == args->cap_set_ver) {
513 ptr = cache_ent->caps_cache;
514 spin_unlock(&vgdev->display_info_lock);
518 spin_unlock(&vgdev->display_info_lock);
520 /* not in cache - need to talk to hw */
521 virtio_gpu_cmd_get_capset(vgdev, found_valid, args->cap_set_ver,
524 ret = wait_event_timeout(vgdev->resp_wq,
525 atomic_read(&cache_ent->is_valid), 5 * HZ);
527 ptr = cache_ent->caps_cache;
530 if (copy_to_user((void __user *)(unsigned long)args->addr, ptr, size))
536 struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {
537 DRM_IOCTL_DEF_DRV(VIRTGPU_MAP, virtio_gpu_map_ioctl,
538 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
540 DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER, virtio_gpu_execbuffer_ioctl,
541 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
543 DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM, virtio_gpu_getparam_ioctl,
544 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
546 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE,
547 virtio_gpu_resource_create_ioctl,
548 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
550 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO, virtio_gpu_resource_info_ioctl,
551 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
553 /* make transfer async to the main ring? - no sure, can we
554 thread these in the underlying GL */
555 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST,
556 virtio_gpu_transfer_from_host_ioctl,
557 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
558 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST,
559 virtio_gpu_transfer_to_host_ioctl,
560 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
562 DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT, virtio_gpu_wait_ioctl,
563 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
565 DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl,
566 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),