1 /* 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 <drm/virtgpu_drm.h> 30 #include <drm/ttm/ttm_execbuf_util.h> 31 32 #include "virtgpu_drv.h" 33 34 static void convert_to_hw_box(struct virtio_gpu_box *dst, 35 const struct drm_virtgpu_3d_box *src) 36 { 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); 43 } 44 45 static int virtio_gpu_map_ioctl(struct drm_device *dev, void *data, 46 struct drm_file *file_priv) 47 { 48 struct virtio_gpu_device *vgdev = dev->dev_private; 49 struct drm_virtgpu_map *virtio_gpu_map = data; 50 51 return virtio_gpu_mode_dumb_mmap(file_priv, vgdev->ddev, 52 virtio_gpu_map->handle, 53 &virtio_gpu_map->offset); 54 } 55 56 static int virtio_gpu_object_list_validate(struct ww_acquire_ctx *ticket, 57 struct list_head *head) 58 { 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; 63 int ret; 64 65 ret = ttm_eu_reserve_buffers(ticket, head, true, NULL); 66 if (ret != 0) 67 return ret; 68 69 list_for_each_entry(buf, head, head) { 70 bo = buf->bo; 71 qobj = container_of(bo, struct virtio_gpu_object, tbo); 72 ret = ttm_bo_validate(bo, &qobj->placement, &ctx); 73 if (ret) { 74 ttm_eu_backoff_reservation(ticket, head); 75 return ret; 76 } 77 } 78 return 0; 79 } 80 81 static void virtio_gpu_unref_list(struct list_head *head) 82 { 83 struct ttm_validate_buffer *buf; 84 struct ttm_buffer_object *bo; 85 struct virtio_gpu_object *qobj; 86 87 list_for_each_entry(buf, head, head) { 88 bo = buf->bo; 89 qobj = container_of(bo, struct virtio_gpu_object, tbo); 90 91 drm_gem_object_put_unlocked(&qobj->gem_base); 92 } 93 } 94 95 /* 96 * Usage of execbuffer: 97 * Relocations need to take into account the full VIRTIO_GPUDrawable size. 98 * However, the command as passed from user space must *not* contain the initial 99 * VIRTIO_GPUReleaseInfo struct (first XXX bytes) 100 */ 101 static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, 102 struct drm_file *drm_file) 103 { 104 struct drm_virtgpu_execbuffer *exbuf = data; 105 struct virtio_gpu_device *vgdev = dev->dev_private; 106 struct virtio_gpu_fpriv *vfpriv = drm_file->driver_priv; 107 struct drm_gem_object *gobj; 108 struct virtio_gpu_fence *fence; 109 struct virtio_gpu_object *qobj; 110 int ret; 111 uint32_t *bo_handles = NULL; 112 void __user *user_bo_handles = NULL; 113 struct list_head validate_list; 114 struct ttm_validate_buffer *buflist = NULL; 115 int i; 116 struct ww_acquire_ctx ticket; 117 void *buf; 118 119 if (vgdev->has_virgl_3d == false) 120 return -ENOSYS; 121 122 INIT_LIST_HEAD(&validate_list); 123 if (exbuf->num_bo_handles) { 124 125 bo_handles = kvmalloc_array(exbuf->num_bo_handles, 126 sizeof(uint32_t), GFP_KERNEL); 127 buflist = kvmalloc_array(exbuf->num_bo_handles, 128 sizeof(struct ttm_validate_buffer), 129 GFP_KERNEL | __GFP_ZERO); 130 if (!bo_handles || !buflist) { 131 kvfree(bo_handles); 132 kvfree(buflist); 133 return -ENOMEM; 134 } 135 136 user_bo_handles = (void __user *)(uintptr_t)exbuf->bo_handles; 137 if (copy_from_user(bo_handles, user_bo_handles, 138 exbuf->num_bo_handles * sizeof(uint32_t))) { 139 ret = -EFAULT; 140 kvfree(bo_handles); 141 kvfree(buflist); 142 return ret; 143 } 144 145 for (i = 0; i < exbuf->num_bo_handles; i++) { 146 gobj = drm_gem_object_lookup(drm_file, bo_handles[i]); 147 if (!gobj) { 148 kvfree(bo_handles); 149 kvfree(buflist); 150 return -ENOENT; 151 } 152 153 qobj = gem_to_virtio_gpu_obj(gobj); 154 buflist[i].bo = &qobj->tbo; 155 156 list_add(&buflist[i].head, &validate_list); 157 } 158 kvfree(bo_handles); 159 } 160 161 ret = virtio_gpu_object_list_validate(&ticket, &validate_list); 162 if (ret) 163 goto out_free; 164 165 buf = memdup_user((void __user *)(uintptr_t)exbuf->command, 166 exbuf->size); 167 if (IS_ERR(buf)) { 168 ret = PTR_ERR(buf); 169 goto out_unresv; 170 } 171 virtio_gpu_cmd_submit(vgdev, buf, exbuf->size, 172 vfpriv->ctx_id, &fence); 173 174 ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f); 175 176 /* fence the command bo */ 177 virtio_gpu_unref_list(&validate_list); 178 kvfree(buflist); 179 dma_fence_put(&fence->f); 180 return 0; 181 182 out_unresv: 183 ttm_eu_backoff_reservation(&ticket, &validate_list); 184 out_free: 185 virtio_gpu_unref_list(&validate_list); 186 kvfree(buflist); 187 return ret; 188 } 189 190 static int virtio_gpu_getparam_ioctl(struct drm_device *dev, void *data, 191 struct drm_file *file_priv) 192 { 193 struct virtio_gpu_device *vgdev = dev->dev_private; 194 struct drm_virtgpu_getparam *param = data; 195 int value; 196 197 switch (param->param) { 198 case VIRTGPU_PARAM_3D_FEATURES: 199 value = vgdev->has_virgl_3d == true ? 1 : 0; 200 break; 201 case VIRTGPU_PARAM_CAPSET_QUERY_FIX: 202 value = 1; 203 break; 204 default: 205 return -EINVAL; 206 } 207 if (copy_to_user((void __user *)(unsigned long)param->value, 208 &value, sizeof(int))) { 209 return -EFAULT; 210 } 211 return 0; 212 } 213 214 static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data, 215 struct drm_file *file_priv) 216 { 217 struct virtio_gpu_device *vgdev = dev->dev_private; 218 struct drm_virtgpu_resource_create *rc = data; 219 int ret; 220 struct virtio_gpu_object *qobj; 221 struct drm_gem_object *obj; 222 uint32_t handle = 0; 223 uint32_t size; 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; 229 230 if (vgdev->has_virgl_3d == false) { 231 if (rc->depth > 1) 232 return -EINVAL; 233 if (rc->nr_samples > 1) 234 return -EINVAL; 235 if (rc->last_level > 1) 236 return -EINVAL; 237 if (rc->target != 2) 238 return -EINVAL; 239 if (rc->array_size > 1) 240 return -EINVAL; 241 } 242 243 INIT_LIST_HEAD(&validate_list); 244 memset(&mainbuf, 0, sizeof(struct ttm_validate_buffer)); 245 246 size = rc->size; 247 248 /* allocate a single page size object */ 249 if (size == 0) 250 size = PAGE_SIZE; 251 252 qobj = virtio_gpu_alloc_object(dev, size, false, false); 253 if (IS_ERR(qobj)) 254 return PTR_ERR(qobj); 255 obj = &qobj->gem_base; 256 257 if (!vgdev->has_virgl_3d) { 258 virtio_gpu_cmd_create_resource(vgdev, qobj, rc->format, 259 rc->width, rc->height); 260 261 ret = virtio_gpu_object_attach(vgdev, qobj, NULL); 262 } else { 263 /* use a gem reference since unref list undoes them */ 264 drm_gem_object_get(&qobj->gem_base); 265 mainbuf.bo = &qobj->tbo; 266 list_add(&mainbuf.head, &validate_list); 267 268 ret = virtio_gpu_object_list_validate(&ticket, &validate_list); 269 if (ret) { 270 DRM_DEBUG("failed to validate\n"); 271 goto fail_unref; 272 } 273 274 rc_3d.resource_id = cpu_to_le32(qobj->hw_res_handle); 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); 285 286 virtio_gpu_cmd_resource_create_3d(vgdev, qobj, &rc_3d, NULL); 287 ret = virtio_gpu_object_attach(vgdev, qobj, &fence); 288 if (ret) { 289 ttm_eu_backoff_reservation(&ticket, &validate_list); 290 goto fail_unref; 291 } 292 ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f); 293 } 294 295 ret = drm_gem_handle_create(file_priv, obj, &handle); 296 if (ret) { 297 298 drm_gem_object_release(obj); 299 if (vgdev->has_virgl_3d) { 300 virtio_gpu_unref_list(&validate_list); 301 dma_fence_put(&fence->f); 302 } 303 return ret; 304 } 305 drm_gem_object_put_unlocked(obj); 306 307 rc->res_handle = qobj->hw_res_handle; /* similiar to a VM address */ 308 rc->bo_handle = handle; 309 310 if (vgdev->has_virgl_3d) { 311 virtio_gpu_unref_list(&validate_list); 312 dma_fence_put(&fence->f); 313 } 314 return 0; 315 fail_unref: 316 if (vgdev->has_virgl_3d) { 317 virtio_gpu_unref_list(&validate_list); 318 dma_fence_put(&fence->f); 319 } 320 //fail_obj: 321 // drm_gem_object_handle_unreference_unlocked(obj); 322 return ret; 323 } 324 325 static int virtio_gpu_resource_info_ioctl(struct drm_device *dev, void *data, 326 struct drm_file *file_priv) 327 { 328 struct drm_virtgpu_resource_info *ri = data; 329 struct drm_gem_object *gobj = NULL; 330 struct virtio_gpu_object *qobj = NULL; 331 332 gobj = drm_gem_object_lookup(file_priv, ri->bo_handle); 333 if (gobj == NULL) 334 return -ENOENT; 335 336 qobj = gem_to_virtio_gpu_obj(gobj); 337 338 ri->size = qobj->gem_base.size; 339 ri->res_handle = qobj->hw_res_handle; 340 drm_gem_object_put_unlocked(gobj); 341 return 0; 342 } 343 344 static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev, 345 void *data, 346 struct drm_file *file) 347 { 348 struct virtio_gpu_device *vgdev = dev->dev_private; 349 struct virtio_gpu_fpriv *vfpriv = file->driver_priv; 350 struct drm_virtgpu_3d_transfer_from_host *args = data; 351 struct ttm_operation_ctx ctx = { true, false }; 352 struct drm_gem_object *gobj = NULL; 353 struct virtio_gpu_object *qobj = NULL; 354 struct virtio_gpu_fence *fence; 355 int ret; 356 u32 offset = args->offset; 357 struct virtio_gpu_box box; 358 359 if (vgdev->has_virgl_3d == false) 360 return -ENOSYS; 361 362 gobj = drm_gem_object_lookup(file, args->bo_handle); 363 if (gobj == NULL) 364 return -ENOENT; 365 366 qobj = gem_to_virtio_gpu_obj(gobj); 367 368 ret = virtio_gpu_object_reserve(qobj, false); 369 if (ret) 370 goto out; 371 372 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx); 373 if (unlikely(ret)) 374 goto out_unres; 375 376 convert_to_hw_box(&box, &args->box); 377 virtio_gpu_cmd_transfer_from_host_3d 378 (vgdev, qobj->hw_res_handle, 379 vfpriv->ctx_id, offset, args->level, 380 &box, &fence); 381 reservation_object_add_excl_fence(qobj->tbo.resv, 382 &fence->f); 383 384 dma_fence_put(&fence->f); 385 out_unres: 386 virtio_gpu_object_unreserve(qobj); 387 out: 388 drm_gem_object_put_unlocked(gobj); 389 return ret; 390 } 391 392 static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data, 393 struct drm_file *file) 394 { 395 struct virtio_gpu_device *vgdev = dev->dev_private; 396 struct virtio_gpu_fpriv *vfpriv = file->driver_priv; 397 struct drm_virtgpu_3d_transfer_to_host *args = data; 398 struct ttm_operation_ctx ctx = { true, false }; 399 struct drm_gem_object *gobj = NULL; 400 struct virtio_gpu_object *qobj = NULL; 401 struct virtio_gpu_fence *fence; 402 struct virtio_gpu_box box; 403 int ret; 404 u32 offset = args->offset; 405 406 gobj = drm_gem_object_lookup(file, args->bo_handle); 407 if (gobj == NULL) 408 return -ENOENT; 409 410 qobj = gem_to_virtio_gpu_obj(gobj); 411 412 ret = virtio_gpu_object_reserve(qobj, false); 413 if (ret) 414 goto out; 415 416 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx); 417 if (unlikely(ret)) 418 goto out_unres; 419 420 convert_to_hw_box(&box, &args->box); 421 if (!vgdev->has_virgl_3d) { 422 virtio_gpu_cmd_transfer_to_host_2d 423 (vgdev, qobj, offset, 424 box.w, box.h, box.x, box.y, NULL); 425 } else { 426 virtio_gpu_cmd_transfer_to_host_3d 427 (vgdev, qobj, 428 vfpriv ? vfpriv->ctx_id : 0, offset, 429 args->level, &box, &fence); 430 reservation_object_add_excl_fence(qobj->tbo.resv, 431 &fence->f); 432 dma_fence_put(&fence->f); 433 } 434 435 out_unres: 436 virtio_gpu_object_unreserve(qobj); 437 out: 438 drm_gem_object_put_unlocked(gobj); 439 return ret; 440 } 441 442 static int virtio_gpu_wait_ioctl(struct drm_device *dev, void *data, 443 struct drm_file *file) 444 { 445 struct drm_virtgpu_3d_wait *args = data; 446 struct drm_gem_object *gobj = NULL; 447 struct virtio_gpu_object *qobj = NULL; 448 int ret; 449 bool nowait = false; 450 451 gobj = drm_gem_object_lookup(file, args->handle); 452 if (gobj == NULL) 453 return -ENOENT; 454 455 qobj = gem_to_virtio_gpu_obj(gobj); 456 457 if (args->flags & VIRTGPU_WAIT_NOWAIT) 458 nowait = true; 459 ret = virtio_gpu_object_wait(qobj, nowait); 460 461 drm_gem_object_put_unlocked(gobj); 462 return ret; 463 } 464 465 static int virtio_gpu_get_caps_ioctl(struct drm_device *dev, 466 void *data, struct drm_file *file) 467 { 468 struct virtio_gpu_device *vgdev = dev->dev_private; 469 struct drm_virtgpu_get_caps *args = data; 470 unsigned size, host_caps_size; 471 int i; 472 int found_valid = -1; 473 int ret; 474 struct virtio_gpu_drv_cap_cache *cache_ent; 475 void *ptr; 476 477 if (vgdev->num_capsets == 0) 478 return -ENOSYS; 479 480 /* don't allow userspace to pass 0 */ 481 if (args->size == 0) 482 return -EINVAL; 483 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) { 488 found_valid = i; 489 break; 490 } 491 } 492 } 493 494 if (found_valid == -1) { 495 spin_unlock(&vgdev->display_info_lock); 496 return -EINVAL; 497 } 498 499 host_caps_size = vgdev->capsets[found_valid].max_size; 500 /* only copy to user the minimum of the host caps size or the guest caps size */ 501 size = min(args->size, host_caps_size); 502 503 list_for_each_entry(cache_ent, &vgdev->cap_cache, head) { 504 if (cache_ent->id == args->cap_set_id && 505 cache_ent->version == args->cap_set_ver) { 506 ptr = cache_ent->caps_cache; 507 spin_unlock(&vgdev->display_info_lock); 508 goto copy_exit; 509 } 510 } 511 spin_unlock(&vgdev->display_info_lock); 512 513 /* not in cache - need to talk to hw */ 514 virtio_gpu_cmd_get_capset(vgdev, found_valid, args->cap_set_ver, 515 &cache_ent); 516 517 ret = wait_event_timeout(vgdev->resp_wq, 518 atomic_read(&cache_ent->is_valid), 5 * HZ); 519 if (!ret) 520 return -EBUSY; 521 522 ptr = cache_ent->caps_cache; 523 524 copy_exit: 525 if (copy_to_user((void __user *)(unsigned long)args->addr, ptr, size)) 526 return -EFAULT; 527 528 return 0; 529 } 530 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), 534 535 DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER, virtio_gpu_execbuffer_ioctl, 536 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 537 538 DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM, virtio_gpu_getparam_ioctl, 539 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 540 541 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE, 542 virtio_gpu_resource_create_ioctl, 543 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 544 545 DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO, virtio_gpu_resource_info_ioctl, 546 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 547 548 /* make transfer async to the main ring? - no sure, can we 549 * thread these in the underlying GL 550 */ 551 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST, 552 virtio_gpu_transfer_from_host_ioctl, 553 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 554 DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST, 555 virtio_gpu_transfer_to_host_ioctl, 556 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 557 558 DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT, virtio_gpu_wait_ioctl, 559 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 560 561 DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl, 562 DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW), 563 }; 564