blob: e0613a9c283325a1a3979a599f9fb0022f488390 [file] [log] [blame]
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +01001/*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * All Rights Reserved.
4 *
5 * Authors:
6 * Dave Airlie
7 * Alon Levy
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
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.
26 */
27
28#include <drm/drmP.h>
29#include "virtgpu_drv.h"
30#include <drm/virtgpu_drm.h>
31#include "ttm/ttm_execbuf_util.h"
32
33static void convert_to_hw_box(struct virtio_gpu_box *dst,
34 const struct drm_virtgpu_3d_box *src)
35{
36 dst->x = cpu_to_le32(src->x);
37 dst->y = cpu_to_le32(src->y);
38 dst->z = cpu_to_le32(src->z);
39 dst->w = cpu_to_le32(src->w);
40 dst->h = cpu_to_le32(src->h);
41 dst->d = cpu_to_le32(src->d);
42}
43
44static int virtio_gpu_map_ioctl(struct drm_device *dev, void *data,
45 struct drm_file *file_priv)
46{
47 struct virtio_gpu_device *vgdev = dev->dev_private;
48 struct drm_virtgpu_map *virtio_gpu_map = data;
49
50 return virtio_gpu_mode_dumb_mmap(file_priv, vgdev->ddev,
51 virtio_gpu_map->handle,
52 &virtio_gpu_map->offset);
53}
54
55static int virtio_gpu_object_list_validate(struct ww_acquire_ctx *ticket,
56 struct list_head *head)
57{
58 struct ttm_validate_buffer *buf;
59 struct ttm_buffer_object *bo;
60 struct virtio_gpu_object *qobj;
61 int ret;
62
63 ret = ttm_eu_reserve_buffers(ticket, head, true, NULL);
64 if (ret != 0)
65 return ret;
66
67 list_for_each_entry(buf, head, head) {
68 bo = buf->bo;
69 qobj = container_of(bo, struct virtio_gpu_object, tbo);
70 ret = ttm_bo_validate(bo, &qobj->placement, false, false);
71 if (ret) {
72 ttm_eu_backoff_reservation(ticket, head);
73 return ret;
74 }
75 }
76 return 0;
77}
78
79static void virtio_gpu_unref_list(struct list_head *head)
80{
81 struct ttm_validate_buffer *buf;
82 struct ttm_buffer_object *bo;
83 struct virtio_gpu_object *qobj;
84 list_for_each_entry(buf, head, head) {
85 bo = buf->bo;
86 qobj = container_of(bo, struct virtio_gpu_object, tbo);
87
88 drm_gem_object_unreference_unlocked(&qobj->gem_base);
89 }
90}
91
Gustavo Padovan5c32c3d2016-08-31 12:26:52 -040092/*
93 * Usage of execbuffer:
94 * Relocations need to take into account the full VIRTIO_GPUDrawable size.
95 * However, the command as passed from user space must *not* contain the initial
96 * VIRTIO_GPUReleaseInfo struct (first XXX bytes)
97 */
98static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +010099 struct drm_file *drm_file)
100{
Gustavo Padovan5c32c3d2016-08-31 12:26:52 -0400101 struct drm_virtgpu_execbuffer *exbuf = data;
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100102 struct virtio_gpu_device *vgdev = dev->dev_private;
103 struct virtio_gpu_fpriv *vfpriv = drm_file->driver_priv;
104 struct drm_gem_object *gobj;
105 struct virtio_gpu_fence *fence;
106 struct virtio_gpu_object *qobj;
107 int ret;
108 uint32_t *bo_handles = NULL;
109 void __user *user_bo_handles = NULL;
110 struct list_head validate_list;
111 struct ttm_validate_buffer *buflist = NULL;
112 int i;
113 struct ww_acquire_ctx ticket;
114 void *buf;
115
116 if (vgdev->has_virgl_3d == false)
117 return -ENOSYS;
118
119 INIT_LIST_HEAD(&validate_list);
120 if (exbuf->num_bo_handles) {
121
122 bo_handles = drm_malloc_ab(exbuf->num_bo_handles,
123 sizeof(uint32_t));
124 buflist = drm_calloc_large(exbuf->num_bo_handles,
125 sizeof(struct ttm_validate_buffer));
126 if (!bo_handles || !buflist) {
127 drm_free_large(bo_handles);
128 drm_free_large(buflist);
129 return -ENOMEM;
130 }
131
132 user_bo_handles = (void __user *)(uintptr_t)exbuf->bo_handles;
133 if (copy_from_user(bo_handles, user_bo_handles,
134 exbuf->num_bo_handles * sizeof(uint32_t))) {
135 ret = -EFAULT;
136 drm_free_large(bo_handles);
137 drm_free_large(buflist);
138 return ret;
139 }
140
141 for (i = 0; i < exbuf->num_bo_handles; i++) {
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100142 gobj = drm_gem_object_lookup(drm_file, bo_handles[i]);
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100143 if (!gobj) {
144 drm_free_large(bo_handles);
145 drm_free_large(buflist);
146 return -ENOENT;
147 }
148
149 qobj = gem_to_virtio_gpu_obj(gobj);
150 buflist[i].bo = &qobj->tbo;
151
152 list_add(&buflist[i].head, &validate_list);
153 }
154 drm_free_large(bo_handles);
155 }
156
157 ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
158 if (ret)
159 goto out_free;
160
161 buf = kmalloc(exbuf->size, GFP_KERNEL);
162 if (!buf) {
163 ret = -ENOMEM;
164 goto out_unresv;
165 }
166 if (copy_from_user(buf, (void __user *)(uintptr_t)exbuf->command,
167 exbuf->size)) {
168 kfree(buf);
169 ret = -EFAULT;
170 goto out_unresv;
171 }
172 virtio_gpu_cmd_submit(vgdev, buf, exbuf->size,
173 vfpriv->ctx_id, &fence);
174
175 ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
176
177 /* fence the command bo */
178 virtio_gpu_unref_list(&validate_list);
179 drm_free_large(buflist);
180 fence_put(&fence->f);
181 return 0;
182
183out_unresv:
184 ttm_eu_backoff_reservation(&ticket, &validate_list);
185out_free:
186 virtio_gpu_unref_list(&validate_list);
187 drm_free_large(buflist);
188 return ret;
189}
190
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100191static int virtio_gpu_getparam_ioctl(struct drm_device *dev, void *data,
192 struct drm_file *file_priv)
193{
194 struct virtio_gpu_device *vgdev = dev->dev_private;
195 struct drm_virtgpu_getparam *param = data;
196 int value;
197
198 switch (param->param) {
199 case VIRTGPU_PARAM_3D_FEATURES:
200 value = vgdev->has_virgl_3d == true ? 1 : 0;
201 break;
202 default:
203 return -EINVAL;
204 }
205 if (copy_to_user((void __user *)(unsigned long)param->value,
206 &value, sizeof(int))) {
207 return -EFAULT;
208 }
209 return 0;
210}
211
212static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data,
213 struct drm_file *file_priv)
214{
215 struct virtio_gpu_device *vgdev = dev->dev_private;
216 struct drm_virtgpu_resource_create *rc = data;
217 int ret;
218 uint32_t res_id;
219 struct virtio_gpu_object *qobj;
220 struct drm_gem_object *obj;
221 uint32_t handle = 0;
222 uint32_t size;
223 struct list_head validate_list;
224 struct ttm_validate_buffer mainbuf;
225 struct virtio_gpu_fence *fence = NULL;
226 struct ww_acquire_ctx ticket;
227 struct virtio_gpu_resource_create_3d rc_3d;
228
229 if (vgdev->has_virgl_3d == false) {
230 if (rc->depth > 1)
231 return -EINVAL;
232 if (rc->nr_samples > 1)
233 return -EINVAL;
234 if (rc->last_level > 1)
235 return -EINVAL;
236 if (rc->target != 2)
237 return -EINVAL;
238 if (rc->array_size > 1)
239 return -EINVAL;
240 }
241
242 INIT_LIST_HEAD(&validate_list);
243 memset(&mainbuf, 0, sizeof(struct ttm_validate_buffer));
244
245 virtio_gpu_resource_id_get(vgdev, &res_id);
246
247 size = rc->size;
248
249 /* allocate a single page size object */
250 if (size == 0)
251 size = PAGE_SIZE;
252
253 qobj = virtio_gpu_alloc_object(dev, size, false, false);
254 if (IS_ERR(qobj)) {
255 ret = PTR_ERR(qobj);
256 goto fail_id;
257 }
258 obj = &qobj->gem_base;
259
260 if (!vgdev->has_virgl_3d) {
261 virtio_gpu_cmd_create_resource(vgdev, res_id, rc->format,
262 rc->width, rc->height);
263
264 ret = virtio_gpu_object_attach(vgdev, qobj, res_id, NULL);
265 } else {
266 /* use a gem reference since unref list undoes them */
267 drm_gem_object_reference(&qobj->gem_base);
268 mainbuf.bo = &qobj->tbo;
269 list_add(&mainbuf.head, &validate_list);
270
271 ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
272 if (ret) {
273 DRM_DEBUG("failed to validate\n");
274 goto fail_unref;
275 }
276
277 rc_3d.resource_id = cpu_to_le32(res_id);
278 rc_3d.target = cpu_to_le32(rc->target);
279 rc_3d.format = cpu_to_le32(rc->format);
280 rc_3d.bind = cpu_to_le32(rc->bind);
281 rc_3d.width = cpu_to_le32(rc->width);
282 rc_3d.height = cpu_to_le32(rc->height);
283 rc_3d.depth = cpu_to_le32(rc->depth);
284 rc_3d.array_size = cpu_to_le32(rc->array_size);
285 rc_3d.last_level = cpu_to_le32(rc->last_level);
286 rc_3d.nr_samples = cpu_to_le32(rc->nr_samples);
287 rc_3d.flags = cpu_to_le32(rc->flags);
288
289 virtio_gpu_cmd_resource_create_3d(vgdev, &rc_3d, NULL);
290 ret = virtio_gpu_object_attach(vgdev, qobj, res_id, &fence);
291 if (ret) {
292 ttm_eu_backoff_reservation(&ticket, &validate_list);
293 goto fail_unref;
294 }
295 ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
296 }
297
298 qobj->hw_res_handle = res_id;
299
300 ret = drm_gem_handle_create(file_priv, obj, &handle);
301 if (ret) {
302
303 drm_gem_object_release(obj);
304 if (vgdev->has_virgl_3d) {
305 virtio_gpu_unref_list(&validate_list);
306 fence_put(&fence->f);
307 }
308 return ret;
309 }
310 drm_gem_object_unreference_unlocked(obj);
311
312 rc->res_handle = res_id; /* similiar to a VM address */
313 rc->bo_handle = handle;
314
315 if (vgdev->has_virgl_3d) {
316 virtio_gpu_unref_list(&validate_list);
317 fence_put(&fence->f);
318 }
319 return 0;
320fail_unref:
321 if (vgdev->has_virgl_3d) {
322 virtio_gpu_unref_list(&validate_list);
323 fence_put(&fence->f);
324 }
325//fail_obj:
326// drm_gem_object_handle_unreference_unlocked(obj);
327fail_id:
328 virtio_gpu_resource_id_put(vgdev, res_id);
329 return ret;
330}
331
332static int virtio_gpu_resource_info_ioctl(struct drm_device *dev, void *data,
333 struct drm_file *file_priv)
334{
335 struct drm_virtgpu_resource_info *ri = data;
336 struct drm_gem_object *gobj = NULL;
337 struct virtio_gpu_object *qobj = NULL;
338
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100339 gobj = drm_gem_object_lookup(file_priv, ri->bo_handle);
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100340 if (gobj == NULL)
341 return -ENOENT;
342
343 qobj = gem_to_virtio_gpu_obj(gobj);
344
345 ri->size = qobj->gem_base.size;
346 ri->res_handle = qobj->hw_res_handle;
347 drm_gem_object_unreference_unlocked(gobj);
348 return 0;
349}
350
351static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev,
352 void *data,
353 struct drm_file *file)
354{
355 struct virtio_gpu_device *vgdev = dev->dev_private;
356 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
357 struct drm_virtgpu_3d_transfer_from_host *args = data;
358 struct drm_gem_object *gobj = NULL;
359 struct virtio_gpu_object *qobj = NULL;
360 struct virtio_gpu_fence *fence;
361 int ret;
362 u32 offset = args->offset;
363 struct virtio_gpu_box box;
364
365 if (vgdev->has_virgl_3d == false)
366 return -ENOSYS;
367
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100368 gobj = drm_gem_object_lookup(file, args->bo_handle);
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100369 if (gobj == NULL)
370 return -ENOENT;
371
372 qobj = gem_to_virtio_gpu_obj(gobj);
373
374 ret = virtio_gpu_object_reserve(qobj, false);
375 if (ret)
376 goto out;
377
378 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
379 true, false);
380 if (unlikely(ret))
381 goto out_unres;
382
383 convert_to_hw_box(&box, &args->box);
384 virtio_gpu_cmd_transfer_from_host_3d
385 (vgdev, qobj->hw_res_handle,
386 vfpriv->ctx_id, offset, args->level,
387 &box, &fence);
388 reservation_object_add_excl_fence(qobj->tbo.resv,
389 &fence->f);
390
391 fence_put(&fence->f);
392out_unres:
393 virtio_gpu_object_unreserve(qobj);
394out:
395 drm_gem_object_unreference_unlocked(gobj);
396 return ret;
397}
398
399static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data,
400 struct drm_file *file)
401{
402 struct virtio_gpu_device *vgdev = dev->dev_private;
403 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
404 struct drm_virtgpu_3d_transfer_to_host *args = data;
405 struct drm_gem_object *gobj = NULL;
406 struct virtio_gpu_object *qobj = NULL;
407 struct virtio_gpu_fence *fence;
408 struct virtio_gpu_box box;
409 int ret;
410 u32 offset = args->offset;
411
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100412 gobj = drm_gem_object_lookup(file, args->bo_handle);
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100413 if (gobj == NULL)
414 return -ENOENT;
415
416 qobj = gem_to_virtio_gpu_obj(gobj);
417
418 ret = virtio_gpu_object_reserve(qobj, false);
419 if (ret)
420 goto out;
421
422 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
423 true, false);
424 if (unlikely(ret))
425 goto out_unres;
426
427 convert_to_hw_box(&box, &args->box);
428 if (!vgdev->has_virgl_3d) {
429 virtio_gpu_cmd_transfer_to_host_2d
430 (vgdev, qobj->hw_res_handle, offset,
431 box.w, box.h, box.x, box.y, NULL);
432 } else {
433 virtio_gpu_cmd_transfer_to_host_3d
434 (vgdev, qobj->hw_res_handle,
435 vfpriv ? vfpriv->ctx_id : 0, offset,
436 args->level, &box, &fence);
437 reservation_object_add_excl_fence(qobj->tbo.resv,
438 &fence->f);
439 fence_put(&fence->f);
440 }
441
442out_unres:
443 virtio_gpu_object_unreserve(qobj);
444out:
445 drm_gem_object_unreference_unlocked(gobj);
446 return ret;
447}
448
449static int virtio_gpu_wait_ioctl(struct drm_device *dev, void *data,
450 struct drm_file *file)
451{
452 struct drm_virtgpu_3d_wait *args = data;
453 struct drm_gem_object *gobj = NULL;
454 struct virtio_gpu_object *qobj = NULL;
455 int ret;
456 bool nowait = false;
457
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100458 gobj = drm_gem_object_lookup(file, args->handle);
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100459 if (gobj == NULL)
460 return -ENOENT;
461
462 qobj = gem_to_virtio_gpu_obj(gobj);
463
464 if (args->flags & VIRTGPU_WAIT_NOWAIT)
465 nowait = true;
466 ret = virtio_gpu_object_wait(qobj, nowait);
467
468 drm_gem_object_unreference_unlocked(gobj);
469 return ret;
470}
471
472static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,
473 void *data, struct drm_file *file)
474{
475 struct virtio_gpu_device *vgdev = dev->dev_private;
476 struct drm_virtgpu_get_caps *args = data;
477 int size;
478 int i;
479 int found_valid = -1;
480 int ret;
481 struct virtio_gpu_drv_cap_cache *cache_ent;
482 void *ptr;
483 if (vgdev->num_capsets == 0)
484 return -ENOSYS;
485
486 spin_lock(&vgdev->display_info_lock);
487 for (i = 0; i < vgdev->num_capsets; i++) {
488 if (vgdev->capsets[i].id == args->cap_set_id) {
489 if (vgdev->capsets[i].max_version >= args->cap_set_ver) {
490 found_valid = i;
491 break;
492 }
493 }
494 }
495
496 if (found_valid == -1) {
497 spin_unlock(&vgdev->display_info_lock);
498 return -EINVAL;
499 }
500
501 size = vgdev->capsets[found_valid].max_size;
502 if (args->size > size) {
503 spin_unlock(&vgdev->display_info_lock);
504 return -EINVAL;
505 }
506
507 list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
508 if (cache_ent->id == args->cap_set_id &&
509 cache_ent->version == args->cap_set_ver) {
510 ptr = cache_ent->caps_cache;
511 spin_unlock(&vgdev->display_info_lock);
512 goto copy_exit;
513 }
514 }
515 spin_unlock(&vgdev->display_info_lock);
516
517 /* not in cache - need to talk to hw */
518 virtio_gpu_cmd_get_capset(vgdev, found_valid, args->cap_set_ver,
519 &cache_ent);
520
521 ret = wait_event_timeout(vgdev->resp_wq,
522 atomic_read(&cache_ent->is_valid), 5 * HZ);
523
524 ptr = cache_ent->caps_cache;
525
526copy_exit:
527 if (copy_to_user((void __user *)(unsigned long)args->addr, ptr, size))
528 return -EFAULT;
529
530 return 0;
531}
532
533struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {
534 DRM_IOCTL_DEF_DRV(VIRTGPU_MAP, virtio_gpu_map_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000535 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100536
537 DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER, virtio_gpu_execbuffer_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000538 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100539
540 DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM, virtio_gpu_getparam_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000541 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100542
543 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE,
544 virtio_gpu_resource_create_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000545 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100546
547 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO, virtio_gpu_resource_info_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000548 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100549
550 /* make transfer async to the main ring? - no sure, can we
551 thread these in the underlying GL */
552 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST,
553 virtio_gpu_transfer_from_host_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000554 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100555 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST,
556 virtio_gpu_transfer_to_host_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000557 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100558
559 DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT, virtio_gpu_wait_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000560 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100561
562 DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl,
Dave Airlief3380a32015-06-16 15:41:56 +1000563 DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Gerd Hoffmann62fb7a52014-10-28 12:48:00 +0100564};