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_validate_buffer *buf;
60 struct ttm_buffer_object *bo;
61 struct virtio_gpu_object *qobj;
64 ret = ttm_eu_reserve_buffers(ticket, head, true, NULL);
68 list_for_each_entry(buf, head, head) {
70 qobj = container_of(bo, struct virtio_gpu_object, tbo);
71 ret = ttm_bo_validate(bo, &qobj->placement, false, false);
73 ttm_eu_backoff_reservation(ticket, head);
80 static void virtio_gpu_unref_list(struct list_head *head)
82 struct ttm_validate_buffer *buf;
83 struct ttm_buffer_object *bo;
84 struct virtio_gpu_object *qobj;
85 list_for_each_entry(buf, head, head) {
87 qobj = container_of(bo, struct virtio_gpu_object, tbo);
89 drm_gem_object_unreference_unlocked(&qobj->gem_base);
94 * Usage of execbuffer:
95 * Relocations need to take into account the full VIRTIO_GPUDrawable size.
96 * However, the command as passed from user space must *not* contain the initial
97 * VIRTIO_GPUReleaseInfo struct (first XXX bytes)
99 static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
100 struct drm_file *drm_file)
102 struct drm_virtgpu_execbuffer *exbuf = data;
103 struct virtio_gpu_device *vgdev = dev->dev_private;
104 struct virtio_gpu_fpriv *vfpriv = drm_file->driver_priv;
105 struct drm_gem_object *gobj;
106 struct virtio_gpu_fence *fence;
107 struct virtio_gpu_object *qobj;
109 uint32_t *bo_handles = NULL;
110 void __user *user_bo_handles = NULL;
111 struct list_head validate_list;
112 struct ttm_validate_buffer *buflist = NULL;
114 struct ww_acquire_ctx ticket;
117 if (vgdev->has_virgl_3d == false)
120 INIT_LIST_HEAD(&validate_list);
121 if (exbuf->num_bo_handles) {
123 bo_handles = kvmalloc_array(exbuf->num_bo_handles,
124 sizeof(uint32_t), GFP_KERNEL);
125 buflist = kvmalloc_array(exbuf->num_bo_handles,
126 sizeof(struct ttm_validate_buffer),
127 GFP_KERNEL | __GFP_ZERO);
128 if (!bo_handles || !buflist) {
134 user_bo_handles = (void __user *)(uintptr_t)exbuf->bo_handles;
135 if (copy_from_user(bo_handles, user_bo_handles,
136 exbuf->num_bo_handles * sizeof(uint32_t))) {
143 for (i = 0; i < exbuf->num_bo_handles; i++) {
144 gobj = drm_gem_object_lookup(drm_file, bo_handles[i]);
151 qobj = gem_to_virtio_gpu_obj(gobj);
152 buflist[i].bo = &qobj->tbo;
154 list_add(&buflist[i].head, &validate_list);
159 ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
163 buf = memdup_user((void __user *)(uintptr_t)exbuf->command,
169 virtio_gpu_cmd_submit(vgdev, buf, exbuf->size,
170 vfpriv->ctx_id, &fence);
172 ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
174 /* fence the command bo */
175 virtio_gpu_unref_list(&validate_list);
177 dma_fence_put(&fence->f);
181 ttm_eu_backoff_reservation(&ticket, &validate_list);
183 virtio_gpu_unref_list(&validate_list);
188 static int virtio_gpu_getparam_ioctl(struct drm_device *dev, void *data,
189 struct drm_file *file_priv)
191 struct virtio_gpu_device *vgdev = dev->dev_private;
192 struct drm_virtgpu_getparam *param = data;
195 switch (param->param) {
196 case VIRTGPU_PARAM_3D_FEATURES:
197 value = vgdev->has_virgl_3d == true ? 1 : 0;
202 if (copy_to_user((void __user *)(unsigned long)param->value,
203 &value, sizeof(int))) {
209 static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data,
210 struct drm_file *file_priv)
212 struct virtio_gpu_device *vgdev = dev->dev_private;
213 struct drm_virtgpu_resource_create *rc = data;
216 struct virtio_gpu_object *qobj;
217 struct drm_gem_object *obj;
220 struct list_head validate_list;
221 struct ttm_validate_buffer mainbuf;
222 struct virtio_gpu_fence *fence = NULL;
223 struct ww_acquire_ctx ticket;
224 struct virtio_gpu_resource_create_3d rc_3d;
226 if (vgdev->has_virgl_3d == false) {
229 if (rc->nr_samples > 1)
231 if (rc->last_level > 1)
235 if (rc->array_size > 1)
239 INIT_LIST_HEAD(&validate_list);
240 memset(&mainbuf, 0, sizeof(struct ttm_validate_buffer));
242 virtio_gpu_resource_id_get(vgdev, &res_id);
246 /* allocate a single page size object */
250 qobj = virtio_gpu_alloc_object(dev, size, false, false);
255 obj = &qobj->gem_base;
257 if (!vgdev->has_virgl_3d) {
258 virtio_gpu_cmd_create_resource(vgdev, res_id, rc->format,
259 rc->width, rc->height);
261 ret = virtio_gpu_object_attach(vgdev, qobj, res_id, NULL);
263 /* use a gem reference since unref list undoes them */
264 drm_gem_object_reference(&qobj->gem_base);
265 mainbuf.bo = &qobj->tbo;
266 list_add(&mainbuf.head, &validate_list);
268 ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
270 DRM_DEBUG("failed to validate\n");
274 rc_3d.resource_id = cpu_to_le32(res_id);
275 rc_3d.target = cpu_to_le32(rc->target);
276 rc_3d.format = cpu_to_le32(rc->format);
277 rc_3d.bind = cpu_to_le32(rc->bind);
278 rc_3d.width = cpu_to_le32(rc->width);
279 rc_3d.height = cpu_to_le32(rc->height);
280 rc_3d.depth = cpu_to_le32(rc->depth);
281 rc_3d.array_size = cpu_to_le32(rc->array_size);
282 rc_3d.last_level = cpu_to_le32(rc->last_level);
283 rc_3d.nr_samples = cpu_to_le32(rc->nr_samples);
284 rc_3d.flags = cpu_to_le32(rc->flags);
286 virtio_gpu_cmd_resource_create_3d(vgdev, &rc_3d, NULL);
287 ret = virtio_gpu_object_attach(vgdev, qobj, res_id, &fence);
289 ttm_eu_backoff_reservation(&ticket, &validate_list);
292 ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
295 qobj->hw_res_handle = res_id;
297 ret = drm_gem_handle_create(file_priv, obj, &handle);
300 drm_gem_object_release(obj);
301 if (vgdev->has_virgl_3d) {
302 virtio_gpu_unref_list(&validate_list);
303 dma_fence_put(&fence->f);
307 drm_gem_object_unreference_unlocked(obj);
309 rc->res_handle = res_id; /* similiar to a VM address */
310 rc->bo_handle = handle;
312 if (vgdev->has_virgl_3d) {
313 virtio_gpu_unref_list(&validate_list);
314 dma_fence_put(&fence->f);
318 if (vgdev->has_virgl_3d) {
319 virtio_gpu_unref_list(&validate_list);
320 dma_fence_put(&fence->f);
323 // drm_gem_object_handle_unreference_unlocked(obj);
325 virtio_gpu_resource_id_put(vgdev, res_id);
329 static int virtio_gpu_resource_info_ioctl(struct drm_device *dev, void *data,
330 struct drm_file *file_priv)
332 struct drm_virtgpu_resource_info *ri = data;
333 struct drm_gem_object *gobj = NULL;
334 struct virtio_gpu_object *qobj = NULL;
336 gobj = drm_gem_object_lookup(file_priv, ri->bo_handle);
340 qobj = gem_to_virtio_gpu_obj(gobj);
342 ri->size = qobj->gem_base.size;
343 ri->res_handle = qobj->hw_res_handle;
344 drm_gem_object_unreference_unlocked(gobj);
348 static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev,
350 struct drm_file *file)
352 struct virtio_gpu_device *vgdev = dev->dev_private;
353 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
354 struct drm_virtgpu_3d_transfer_from_host *args = data;
355 struct drm_gem_object *gobj = NULL;
356 struct virtio_gpu_object *qobj = NULL;
357 struct virtio_gpu_fence *fence;
359 u32 offset = args->offset;
360 struct virtio_gpu_box box;
362 if (vgdev->has_virgl_3d == false)
365 gobj = drm_gem_object_lookup(file, args->bo_handle);
369 qobj = gem_to_virtio_gpu_obj(gobj);
371 ret = virtio_gpu_object_reserve(qobj, false);
375 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
380 convert_to_hw_box(&box, &args->box);
381 virtio_gpu_cmd_transfer_from_host_3d
382 (vgdev, qobj->hw_res_handle,
383 vfpriv->ctx_id, offset, args->level,
385 reservation_object_add_excl_fence(qobj->tbo.resv,
388 dma_fence_put(&fence->f);
390 virtio_gpu_object_unreserve(qobj);
392 drm_gem_object_unreference_unlocked(gobj);
396 static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data,
397 struct drm_file *file)
399 struct virtio_gpu_device *vgdev = dev->dev_private;
400 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
401 struct drm_virtgpu_3d_transfer_to_host *args = data;
402 struct drm_gem_object *gobj = NULL;
403 struct virtio_gpu_object *qobj = NULL;
404 struct virtio_gpu_fence *fence;
405 struct virtio_gpu_box box;
407 u32 offset = args->offset;
409 gobj = drm_gem_object_lookup(file, args->bo_handle);
413 qobj = gem_to_virtio_gpu_obj(gobj);
415 ret = virtio_gpu_object_reserve(qobj, false);
419 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
424 convert_to_hw_box(&box, &args->box);
425 if (!vgdev->has_virgl_3d) {
426 virtio_gpu_cmd_transfer_to_host_2d
427 (vgdev, qobj->hw_res_handle, offset,
428 box.w, box.h, box.x, box.y, NULL);
430 virtio_gpu_cmd_transfer_to_host_3d
431 (vgdev, qobj->hw_res_handle,
432 vfpriv ? vfpriv->ctx_id : 0, offset,
433 args->level, &box, &fence);
434 reservation_object_add_excl_fence(qobj->tbo.resv,
436 dma_fence_put(&fence->f);
440 virtio_gpu_object_unreserve(qobj);
442 drm_gem_object_unreference_unlocked(gobj);
446 static int virtio_gpu_wait_ioctl(struct drm_device *dev, void *data,
447 struct drm_file *file)
449 struct drm_virtgpu_3d_wait *args = data;
450 struct drm_gem_object *gobj = NULL;
451 struct virtio_gpu_object *qobj = NULL;
455 gobj = drm_gem_object_lookup(file, args->handle);
459 qobj = gem_to_virtio_gpu_obj(gobj);
461 if (args->flags & VIRTGPU_WAIT_NOWAIT)
463 ret = virtio_gpu_object_wait(qobj, nowait);
465 drm_gem_object_unreference_unlocked(gobj);
469 static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,
470 void *data, struct drm_file *file)
472 struct virtio_gpu_device *vgdev = dev->dev_private;
473 struct drm_virtgpu_get_caps *args = data;
476 int found_valid = -1;
478 struct virtio_gpu_drv_cap_cache *cache_ent;
480 if (vgdev->num_capsets == 0)
483 spin_lock(&vgdev->display_info_lock);
484 for (i = 0; i < vgdev->num_capsets; i++) {
485 if (vgdev->capsets[i].id == args->cap_set_id) {
486 if (vgdev->capsets[i].max_version >= args->cap_set_ver) {
493 if (found_valid == -1) {
494 spin_unlock(&vgdev->display_info_lock);
498 size = vgdev->capsets[found_valid].max_size;
499 if (args->size > size) {
500 spin_unlock(&vgdev->display_info_lock);
504 list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
505 if (cache_ent->id == args->cap_set_id &&
506 cache_ent->version == args->cap_set_ver) {
507 ptr = cache_ent->caps_cache;
508 spin_unlock(&vgdev->display_info_lock);
512 spin_unlock(&vgdev->display_info_lock);
514 /* not in cache - need to talk to hw */
515 virtio_gpu_cmd_get_capset(vgdev, found_valid, args->cap_set_ver,
518 ret = wait_event_timeout(vgdev->resp_wq,
519 atomic_read(&cache_ent->is_valid), 5 * HZ);
521 ptr = cache_ent->caps_cache;
524 if (copy_to_user((void __user *)(unsigned long)args->addr, ptr, size))
530 struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {
531 DRM_IOCTL_DEF_DRV(VIRTGPU_MAP, virtio_gpu_map_ioctl,
532 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
534 DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER, virtio_gpu_execbuffer_ioctl,
535 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
537 DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM, virtio_gpu_getparam_ioctl,
538 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
540 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE,
541 virtio_gpu_resource_create_ioctl,
542 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
544 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO, virtio_gpu_resource_info_ioctl,
545 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
547 /* make transfer async to the main ring? - no sure, can we
548 thread these in the underlying GL */
549 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST,
550 virtio_gpu_transfer_from_host_ioctl,
551 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
552 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST,
553 virtio_gpu_transfer_to_host_ioctl,
554 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
556 DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT, virtio_gpu_wait_ioctl,
557 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
559 DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl,
560 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),