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.
28 #include <linux/file.h>
29 #include <linux/sync_file.h>
30 #include <linux/uaccess.h>
32 #include <drm/drm_file.h>
33 #include <drm/virtgpu_drm.h>
35 #include "virtgpu_drv.h"
37 #define VIRTGPU_BLOB_FLAG_USE_MASK (VIRTGPU_BLOB_FLAG_USE_MAPPABLE | \
38 VIRTGPU_BLOB_FLAG_USE_SHAREABLE | \
39 VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE)
41 /* Must be called with &virtio_gpu_fpriv.struct_mutex held. */
42 static void virtio_gpu_create_context_locked(struct virtio_gpu_device *vgdev,
43 struct virtio_gpu_fpriv *vfpriv)
45 char dbgname[TASK_COMM_LEN];
47 get_task_comm(dbgname, current);
48 virtio_gpu_cmd_context_create(vgdev, vfpriv->ctx_id,
49 vfpriv->context_init, strlen(dbgname),
52 vfpriv->context_created = true;
55 void virtio_gpu_create_context(struct drm_device *dev, struct drm_file *file)
57 struct virtio_gpu_device *vgdev = dev->dev_private;
58 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
60 mutex_lock(&vfpriv->context_lock);
61 if (vfpriv->context_created)
64 virtio_gpu_create_context_locked(vgdev, vfpriv);
67 mutex_unlock(&vfpriv->context_lock);
70 static int virtio_gpu_map_ioctl(struct drm_device *dev, void *data,
71 struct drm_file *file)
73 struct virtio_gpu_device *vgdev = dev->dev_private;
74 struct drm_virtgpu_map *virtio_gpu_map = data;
76 return virtio_gpu_mode_dumb_mmap(file, vgdev->ddev,
77 virtio_gpu_map->handle,
78 &virtio_gpu_map->offset);
81 static int virtio_gpu_getparam_ioctl(struct drm_device *dev, void *data,
82 struct drm_file *file)
84 struct virtio_gpu_device *vgdev = dev->dev_private;
85 struct drm_virtgpu_getparam *param = data;
88 switch (param->param) {
89 case VIRTGPU_PARAM_3D_FEATURES:
90 value = vgdev->has_virgl_3d ? 1 : 0;
92 case VIRTGPU_PARAM_CAPSET_QUERY_FIX:
95 case VIRTGPU_PARAM_RESOURCE_BLOB:
96 value = vgdev->has_resource_blob ? 1 : 0;
98 case VIRTGPU_PARAM_HOST_VISIBLE:
99 value = vgdev->has_host_visible ? 1 : 0;
101 case VIRTGPU_PARAM_CROSS_DEVICE:
102 value = vgdev->has_resource_assign_uuid ? 1 : 0;
104 case VIRTGPU_PARAM_CONTEXT_INIT:
105 value = vgdev->has_context_init ? 1 : 0;
107 case VIRTGPU_PARAM_SUPPORTED_CAPSET_IDs:
108 value = vgdev->capset_id_mask;
113 if (copy_to_user(u64_to_user_ptr(param->value), &value, sizeof(int)))
119 static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data,
120 struct drm_file *file)
122 struct virtio_gpu_device *vgdev = dev->dev_private;
123 struct drm_virtgpu_resource_create *rc = data;
124 struct virtio_gpu_fence *fence;
126 struct virtio_gpu_object *qobj;
127 struct drm_gem_object *obj;
129 struct virtio_gpu_object_params params = { 0 };
131 if (vgdev->has_virgl_3d) {
132 virtio_gpu_create_context(dev, file);
134 params.target = rc->target;
135 params.bind = rc->bind;
136 params.depth = rc->depth;
137 params.array_size = rc->array_size;
138 params.last_level = rc->last_level;
139 params.nr_samples = rc->nr_samples;
140 params.flags = rc->flags;
144 if (rc->nr_samples > 1)
146 if (rc->last_level > 1)
150 if (rc->array_size > 1)
154 params.format = rc->format;
155 params.width = rc->width;
156 params.height = rc->height;
157 params.size = rc->size;
158 /* allocate a single page size object */
159 if (params.size == 0)
160 params.size = PAGE_SIZE;
162 fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context, 0);
165 ret = virtio_gpu_object_create(vgdev, ¶ms, &qobj, fence);
166 dma_fence_put(&fence->f);
169 obj = &qobj->base.base;
171 ret = drm_gem_handle_create(file, obj, &handle);
173 drm_gem_object_release(obj);
177 rc->res_handle = qobj->hw_res_handle; /* similiar to a VM address */
178 rc->bo_handle = handle;
181 * The handle owns the reference now. But we must drop our
182 * remaining reference *after* we no longer need to dereference
183 * the obj. Otherwise userspace could guess the handle and
184 * race closing it from another thread.
186 drm_gem_object_put(obj);
191 static int virtio_gpu_resource_info_ioctl(struct drm_device *dev, void *data,
192 struct drm_file *file)
194 struct drm_virtgpu_resource_info *ri = data;
195 struct drm_gem_object *gobj = NULL;
196 struct virtio_gpu_object *qobj = NULL;
198 gobj = drm_gem_object_lookup(file, ri->bo_handle);
202 qobj = gem_to_virtio_gpu_obj(gobj);
204 ri->size = qobj->base.base.size;
205 ri->res_handle = qobj->hw_res_handle;
206 if (qobj->host3d_blob || qobj->guest_blob)
207 ri->blob_mem = qobj->blob_mem;
209 drm_gem_object_put(gobj);
213 static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev,
215 struct drm_file *file)
217 struct virtio_gpu_device *vgdev = dev->dev_private;
218 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
219 struct drm_virtgpu_3d_transfer_from_host *args = data;
220 struct virtio_gpu_object *bo;
221 struct virtio_gpu_object_array *objs;
222 struct virtio_gpu_fence *fence;
224 u32 offset = args->offset;
226 if (vgdev->has_virgl_3d == false)
229 virtio_gpu_create_context(dev, file);
230 objs = virtio_gpu_array_from_handles(file, &args->bo_handle, 1);
234 bo = gem_to_virtio_gpu_obj(objs->objs[0]);
235 if (bo->guest_blob && !bo->host3d_blob) {
240 if (!bo->host3d_blob && (args->stride || args->layer_stride)) {
245 ret = virtio_gpu_array_lock_resv(objs);
249 fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context, 0);
255 virtio_gpu_cmd_transfer_from_host_3d
256 (vgdev, vfpriv->ctx_id, offset, args->level, args->stride,
257 args->layer_stride, &args->box, objs, fence);
258 dma_fence_put(&fence->f);
259 virtio_gpu_notify(vgdev);
263 virtio_gpu_array_unlock_resv(objs);
265 virtio_gpu_array_put_free(objs);
269 static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data,
270 struct drm_file *file)
272 struct virtio_gpu_device *vgdev = dev->dev_private;
273 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
274 struct drm_virtgpu_3d_transfer_to_host *args = data;
275 struct virtio_gpu_object *bo;
276 struct virtio_gpu_object_array *objs;
277 struct virtio_gpu_fence *fence;
279 u32 offset = args->offset;
281 objs = virtio_gpu_array_from_handles(file, &args->bo_handle, 1);
285 bo = gem_to_virtio_gpu_obj(objs->objs[0]);
286 if (bo->guest_blob && !bo->host3d_blob) {
291 if (!vgdev->has_virgl_3d) {
292 virtio_gpu_cmd_transfer_to_host_2d
294 args->box.w, args->box.h, args->box.x, args->box.y,
297 virtio_gpu_create_context(dev, file);
299 if (!bo->host3d_blob && (args->stride || args->layer_stride)) {
304 ret = virtio_gpu_array_lock_resv(objs);
309 fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context,
314 virtio_gpu_cmd_transfer_to_host_3d
316 vfpriv ? vfpriv->ctx_id : 0, offset, args->level,
317 args->stride, args->layer_stride, &args->box, objs,
319 dma_fence_put(&fence->f);
321 virtio_gpu_notify(vgdev);
325 virtio_gpu_array_unlock_resv(objs);
327 virtio_gpu_array_put_free(objs);
331 static int virtio_gpu_wait_ioctl(struct drm_device *dev, void *data,
332 struct drm_file *file)
334 struct drm_virtgpu_3d_wait *args = data;
335 struct drm_gem_object *obj;
336 long timeout = 15 * HZ;
339 obj = drm_gem_object_lookup(file, args->handle);
343 if (args->flags & VIRTGPU_WAIT_NOWAIT) {
344 ret = dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ);
346 ret = dma_resv_wait_timeout(obj->resv, DMA_RESV_USAGE_READ,
354 drm_gem_object_put(obj);
358 static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,
359 void *data, struct drm_file *file)
361 struct virtio_gpu_device *vgdev = dev->dev_private;
362 struct drm_virtgpu_get_caps *args = data;
363 unsigned size, host_caps_size;
365 int found_valid = -1;
367 struct virtio_gpu_drv_cap_cache *cache_ent;
370 if (vgdev->num_capsets == 0)
373 /* don't allow userspace to pass 0 */
377 spin_lock(&vgdev->display_info_lock);
378 for (i = 0; i < vgdev->num_capsets; i++) {
379 if (vgdev->capsets[i].id == args->cap_set_id) {
380 if (vgdev->capsets[i].max_version >= args->cap_set_ver) {
387 if (found_valid == -1) {
388 spin_unlock(&vgdev->display_info_lock);
392 host_caps_size = vgdev->capsets[found_valid].max_size;
393 /* only copy to user the minimum of the host caps size or the guest caps size */
394 size = min(args->size, host_caps_size);
396 list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
397 if (cache_ent->id == args->cap_set_id &&
398 cache_ent->version == args->cap_set_ver) {
399 spin_unlock(&vgdev->display_info_lock);
403 spin_unlock(&vgdev->display_info_lock);
405 /* not in cache - need to talk to hw */
406 ret = virtio_gpu_cmd_get_capset(vgdev, found_valid, args->cap_set_ver,
410 virtio_gpu_notify(vgdev);
413 ret = wait_event_timeout(vgdev->resp_wq,
414 atomic_read(&cache_ent->is_valid), 5 * HZ);
418 /* is_valid check must proceed before copy of the cache entry. */
421 ptr = cache_ent->caps_cache;
423 if (copy_to_user(u64_to_user_ptr(args->addr), ptr, size))
429 static int verify_blob(struct virtio_gpu_device *vgdev,
430 struct virtio_gpu_fpriv *vfpriv,
431 struct virtio_gpu_object_params *params,
432 struct drm_virtgpu_resource_create_blob *rc_blob,
433 bool *guest_blob, bool *host3d_blob)
435 if (!vgdev->has_resource_blob)
438 if (rc_blob->blob_flags & ~VIRTGPU_BLOB_FLAG_USE_MASK)
441 if (rc_blob->blob_flags & VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE) {
442 if (!vgdev->has_resource_assign_uuid)
446 switch (rc_blob->blob_mem) {
447 case VIRTGPU_BLOB_MEM_GUEST:
450 case VIRTGPU_BLOB_MEM_HOST3D_GUEST:
453 case VIRTGPU_BLOB_MEM_HOST3D:
461 if (!vgdev->has_virgl_3d)
464 /* Must be dword aligned. */
465 if (rc_blob->cmd_size % 4 != 0)
468 params->ctx_id = vfpriv->ctx_id;
469 params->blob_id = rc_blob->blob_id;
471 if (rc_blob->blob_id != 0)
474 if (rc_blob->cmd_size != 0)
478 params->blob_mem = rc_blob->blob_mem;
479 params->size = rc_blob->size;
481 params->blob_flags = rc_blob->blob_flags;
485 static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev,
487 struct drm_file *file)
491 bool guest_blob = false;
492 bool host3d_blob = false;
493 struct drm_gem_object *obj;
494 struct virtio_gpu_object *bo;
495 struct virtio_gpu_object_params params = { 0 };
496 struct virtio_gpu_device *vgdev = dev->dev_private;
497 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
498 struct drm_virtgpu_resource_create_blob *rc_blob = data;
500 if (verify_blob(vgdev, vfpriv, ¶ms, rc_blob,
501 &guest_blob, &host3d_blob))
504 if (vgdev->has_virgl_3d)
505 virtio_gpu_create_context(dev, file);
507 if (rc_blob->cmd_size) {
510 buf = memdup_user(u64_to_user_ptr(rc_blob->cmd),
516 virtio_gpu_cmd_submit(vgdev, buf, rc_blob->cmd_size,
517 vfpriv->ctx_id, NULL, NULL);
521 ret = virtio_gpu_object_create(vgdev, ¶ms, &bo, NULL);
522 else if (!guest_blob && host3d_blob)
523 ret = virtio_gpu_vram_create(vgdev, ¶ms, &bo);
530 bo->guest_blob = guest_blob;
531 bo->host3d_blob = host3d_blob;
532 bo->blob_mem = rc_blob->blob_mem;
533 bo->blob_flags = rc_blob->blob_flags;
535 obj = &bo->base.base;
536 if (params.blob_flags & VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE) {
537 ret = virtio_gpu_resource_assign_uuid(vgdev, bo);
539 drm_gem_object_release(obj);
544 ret = drm_gem_handle_create(file, obj, &handle);
546 drm_gem_object_release(obj);
550 rc_blob->res_handle = bo->hw_res_handle;
551 rc_blob->bo_handle = handle;
554 * The handle owns the reference now. But we must drop our
555 * remaining reference *after* we no longer need to dereference
556 * the obj. Otherwise userspace could guess the handle and
557 * race closing it from another thread.
559 drm_gem_object_put(obj);
564 static int virtio_gpu_context_init_ioctl(struct drm_device *dev,
565 void *data, struct drm_file *file)
568 uint32_t num_params, i, param, value;
569 uint64_t valid_ring_mask;
571 struct drm_virtgpu_context_set_param *ctx_set_params = NULL;
572 struct virtio_gpu_device *vgdev = dev->dev_private;
573 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
574 struct drm_virtgpu_context_init *args = data;
576 num_params = args->num_params;
577 len = num_params * sizeof(struct drm_virtgpu_context_set_param);
579 if (!vgdev->has_context_init || !vgdev->has_virgl_3d)
582 /* Number of unique parameters supported at this time. */
586 ctx_set_params = memdup_user(u64_to_user_ptr(args->ctx_set_params),
589 if (IS_ERR(ctx_set_params))
590 return PTR_ERR(ctx_set_params);
592 mutex_lock(&vfpriv->context_lock);
593 if (vfpriv->context_created) {
598 for (i = 0; i < num_params; i++) {
599 param = ctx_set_params[i].param;
600 value = ctx_set_params[i].value;
603 case VIRTGPU_CONTEXT_PARAM_CAPSET_ID:
604 if (value > MAX_CAPSET_ID) {
609 if ((vgdev->capset_id_mask & (1ULL << value)) == 0) {
614 /* Context capset ID already set */
615 if (vfpriv->context_init &
616 VIRTIO_GPU_CONTEXT_INIT_CAPSET_ID_MASK) {
621 vfpriv->context_init |= value;
623 case VIRTGPU_CONTEXT_PARAM_NUM_RINGS:
624 if (vfpriv->base_fence_ctx) {
629 if (value > MAX_RINGS) {
634 vfpriv->base_fence_ctx = dma_fence_context_alloc(value);
635 vfpriv->num_rings = value;
637 case VIRTGPU_CONTEXT_PARAM_POLL_RINGS_MASK:
638 if (vfpriv->ring_idx_mask) {
643 vfpriv->ring_idx_mask = value;
651 if (vfpriv->ring_idx_mask) {
653 for (i = 0; i < vfpriv->num_rings; i++)
654 valid_ring_mask |= 1ULL << i;
656 if (~valid_ring_mask & vfpriv->ring_idx_mask) {
662 virtio_gpu_create_context_locked(vgdev, vfpriv);
663 virtio_gpu_notify(vgdev);
666 mutex_unlock(&vfpriv->context_lock);
667 kfree(ctx_set_params);
671 struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {
672 DRM_IOCTL_DEF_DRV(VIRTGPU_MAP, virtio_gpu_map_ioctl,
675 DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER, virtio_gpu_execbuffer_ioctl,
678 DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM, virtio_gpu_getparam_ioctl,
681 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE,
682 virtio_gpu_resource_create_ioctl,
685 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO, virtio_gpu_resource_info_ioctl,
688 /* make transfer async to the main ring? - no sure, can we
689 * thread these in the underlying GL
691 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST,
692 virtio_gpu_transfer_from_host_ioctl,
694 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST,
695 virtio_gpu_transfer_to_host_ioctl,
698 DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT, virtio_gpu_wait_ioctl,
701 DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl,
704 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE_BLOB,
705 virtio_gpu_resource_create_blob_ioctl,
708 DRM_IOCTL_DEF_DRV(VIRTGPU_CONTEXT_INIT, virtio_gpu_context_init_ioctl,