1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28 #include <linux/ktime.h> 29 #include <linux/module.h> 30 #include <linux/overflow.h> 31 #include <linux/pagemap.h> 32 #include <linux/pci.h> 33 #include <linux/dma-buf.h> 34 #include <linux/dma-fence-unwrap.h> 35 #include <linux/uaccess.h> 36 37 #include <drm/amdgpu_drm.h> 38 #include <drm/drm_drv.h> 39 #include <drm/drm_exec.h> 40 #include <drm/drm_gem_ttm_helper.h> 41 #include <drm/ttm/ttm_tt.h> 42 #include <drm/drm_syncobj.h> 43 44 #include "amdgpu.h" 45 #include "amdgpu_display.h" 46 #include "amdgpu_dma_buf.h" 47 #include "amdgpu_hmm.h" 48 #include "amdgpu_xgmi.h" 49 #include "amdgpu_vm.h" 50 51 static int 52 amdgpu_gem_add_input_fence(struct drm_file *filp, 53 uint64_t syncobj_handles_array, 54 uint32_t num_syncobj_handles) 55 { 56 struct dma_fence *fence; 57 uint32_t *syncobj_handles; 58 int ret, i; 59 60 if (!num_syncobj_handles) 61 return 0; 62 63 syncobj_handles = memdup_user(u64_to_user_ptr(syncobj_handles_array), 64 size_mul(sizeof(uint32_t), num_syncobj_handles)); 65 if (IS_ERR(syncobj_handles)) 66 return PTR_ERR(syncobj_handles); 67 68 for (i = 0; i < num_syncobj_handles; i++) { 69 70 if (!syncobj_handles[i]) { 71 ret = -EINVAL; 72 goto free_memdup; 73 } 74 75 ret = drm_syncobj_find_fence(filp, syncobj_handles[i], 0, 0, &fence); 76 if (ret) 77 goto free_memdup; 78 79 dma_fence_wait(fence, false); 80 81 /* TODO: optimize async handling */ 82 dma_fence_put(fence); 83 } 84 85 free_memdup: 86 kfree(syncobj_handles); 87 return ret; 88 } 89 90 static int 91 amdgpu_gem_update_timeline_node(struct drm_file *filp, 92 uint32_t syncobj_handle, 93 uint64_t point, 94 struct drm_syncobj **syncobj, 95 struct dma_fence_chain **chain) 96 { 97 if (!syncobj_handle) 98 return 0; 99 100 /* Find the sync object */ 101 *syncobj = drm_syncobj_find(filp, syncobj_handle); 102 if (!*syncobj) 103 return -ENOENT; 104 105 if (!point) 106 return 0; 107 108 /* Allocate the chain node */ 109 *chain = dma_fence_chain_alloc(); 110 if (!*chain) { 111 drm_syncobj_put(*syncobj); 112 *syncobj = NULL; 113 return -ENOMEM; 114 } 115 116 return 0; 117 } 118 119 static vm_fault_t amdgpu_gem_fault(struct vm_fault *vmf) 120 { 121 struct ttm_buffer_object *bo = vmf->vma->vm_private_data; 122 struct drm_device *ddev = bo->base.dev; 123 vm_fault_t ret; 124 int idx; 125 126 ret = ttm_bo_vm_reserve(bo, vmf); 127 if (ret) 128 return ret; 129 130 if (drm_dev_enter(ddev, &idx)) { 131 ret = amdgpu_bo_fault_reserve_notify(bo); 132 if (ret) { 133 drm_dev_exit(idx); 134 goto unlock; 135 } 136 137 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot, 138 TTM_BO_VM_NUM_PREFAULT); 139 140 drm_dev_exit(idx); 141 } else { 142 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot); 143 } 144 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) 145 return ret; 146 147 unlock: 148 dma_resv_unlock(bo->base.resv); 149 return ret; 150 } 151 152 static const struct vm_operations_struct amdgpu_gem_vm_ops = { 153 .fault = amdgpu_gem_fault, 154 .open = ttm_bo_vm_open, 155 .close = ttm_bo_vm_close, 156 .access = ttm_bo_vm_access 157 }; 158 159 static void amdgpu_gem_object_free(struct drm_gem_object *gobj) 160 { 161 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(gobj); 162 163 amdgpu_hmm_unregister(aobj); 164 ttm_bo_fini(&aobj->tbo); 165 } 166 167 int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size, 168 int alignment, u32 initial_domain, 169 u64 flags, enum ttm_bo_type type, 170 struct dma_resv *resv, 171 struct drm_gem_object **obj, int8_t xcp_id_plus1) 172 { 173 struct amdgpu_bo *bo; 174 struct amdgpu_bo_user *ubo; 175 struct amdgpu_bo_param bp; 176 int r; 177 178 memset(&bp, 0, sizeof(bp)); 179 *obj = NULL; 180 flags |= AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE; 181 182 bp.size = size; 183 bp.byte_align = alignment; 184 bp.type = type; 185 bp.resv = resv; 186 bp.preferred_domain = initial_domain; 187 bp.flags = flags; 188 bp.domain = initial_domain; 189 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 190 bp.xcp_id_plus1 = xcp_id_plus1; 191 192 r = amdgpu_bo_create_user(adev, &bp, &ubo); 193 if (r) 194 return r; 195 196 bo = &ubo->bo; 197 *obj = &bo->tbo.base; 198 199 return 0; 200 } 201 202 void amdgpu_gem_force_release(struct amdgpu_device *adev) 203 { 204 struct drm_device *ddev = adev_to_drm(adev); 205 struct drm_file *file; 206 207 mutex_lock(&ddev->filelist_mutex); 208 209 list_for_each_entry(file, &ddev->filelist, lhead) { 210 struct drm_gem_object *gobj; 211 int handle; 212 213 WARN_ONCE(1, "Still active user space clients!\n"); 214 spin_lock(&file->table_lock); 215 idr_for_each_entry(&file->object_idr, gobj, handle) { 216 WARN_ONCE(1, "And also active allocations!\n"); 217 drm_gem_object_put(gobj); 218 } 219 idr_destroy(&file->object_idr); 220 spin_unlock(&file->table_lock); 221 } 222 223 mutex_unlock(&ddev->filelist_mutex); 224 } 225 226 /* 227 * Call from drm_gem_handle_create which appear in both new and open ioctl 228 * case. 229 */ 230 static int amdgpu_gem_object_open(struct drm_gem_object *obj, 231 struct drm_file *file_priv) 232 { 233 struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj); 234 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev); 235 struct amdgpu_fpriv *fpriv = file_priv->driver_priv; 236 struct amdgpu_vm *vm = &fpriv->vm; 237 struct amdgpu_bo_va *bo_va; 238 struct mm_struct *mm; 239 struct drm_exec exec; 240 int r; 241 242 mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm); 243 if (mm && mm != current->mm) 244 return -EPERM; 245 246 if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID && 247 !amdgpu_vm_is_bo_always_valid(vm, abo)) 248 return -EPERM; 249 250 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 251 drm_exec_until_all_locked(&exec) { 252 r = drm_exec_prepare_obj(&exec, &abo->tbo.base, TTM_NUM_MOVE_FENCES + 1); 253 drm_exec_retry_on_contention(&exec); 254 if (unlikely(r)) 255 goto out_unlock; 256 257 r = amdgpu_vm_lock_pd(vm, &exec, 0); 258 drm_exec_retry_on_contention(&exec); 259 if (unlikely(r)) 260 goto out_unlock; 261 } 262 263 amdgpu_vm_bo_update_shared(abo); 264 bo_va = amdgpu_vm_bo_find(vm, abo); 265 if (!bo_va) { 266 bo_va = amdgpu_vm_bo_add(adev, vm, abo); 267 r = amdgpu_evf_mgr_attach_fence(&fpriv->evf_mgr, abo); 268 if (r) 269 goto out_unlock; 270 } else { 271 ++bo_va->ref_count; 272 } 273 274 drm_exec_fini(&exec); 275 276 /* Validate and add eviction fence to DMABuf imports with dynamic 277 * attachment in compute VMs. Re-validation will be done by 278 * amdgpu_vm_validate. Fences are on the reservation shared with the 279 * export, which is currently required to be validated and fenced 280 * already by amdgpu_amdkfd_gpuvm_restore_process_bos. 281 * 282 * Nested locking below for the case that a GEM object is opened in 283 * kfd_mem_export_dmabuf. Since the lock below is only taken for imports, 284 * but not for export, this is a different lock class that cannot lead to 285 * circular lock dependencies. 286 */ 287 if (!vm->is_compute_context || !vm->process_info) 288 return 0; 289 if (!drm_gem_is_imported(obj) || 290 !dma_buf_is_dynamic(obj->import_attach->dmabuf)) 291 return 0; 292 mutex_lock_nested(&vm->process_info->lock, 1); 293 if (!WARN_ON(!vm->process_info->eviction_fence)) { 294 r = amdgpu_amdkfd_bo_validate_and_fence(abo, AMDGPU_GEM_DOMAIN_GTT, 295 &vm->process_info->eviction_fence->base); 296 if (r) { 297 struct amdgpu_task_info *ti = amdgpu_vm_get_task_info_vm(vm); 298 299 dev_warn(adev->dev, "validate_and_fence failed: %d\n", r); 300 if (ti) { 301 dev_warn(adev->dev, "pid %d\n", ti->task.pid); 302 amdgpu_vm_put_task_info(ti); 303 } 304 } 305 } 306 mutex_unlock(&vm->process_info->lock); 307 return r; 308 309 out_unlock: 310 drm_exec_fini(&exec); 311 return r; 312 } 313 314 static void amdgpu_gem_object_close(struct drm_gem_object *obj, 315 struct drm_file *file_priv) 316 { 317 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); 318 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 319 struct amdgpu_fpriv *fpriv = file_priv->driver_priv; 320 struct amdgpu_vm *vm = &fpriv->vm; 321 322 struct dma_fence *fence = NULL; 323 struct amdgpu_bo_va *bo_va; 324 struct drm_exec exec; 325 long r; 326 327 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 328 drm_exec_until_all_locked(&exec) { 329 r = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1); 330 drm_exec_retry_on_contention(&exec); 331 if (unlikely(r)) 332 goto out_unlock; 333 334 r = amdgpu_vm_lock_pd(vm, &exec, 0); 335 drm_exec_retry_on_contention(&exec); 336 if (unlikely(r)) 337 goto out_unlock; 338 } 339 340 if (!amdgpu_vm_is_bo_always_valid(vm, bo)) 341 amdgpu_evf_mgr_detach_fence(&fpriv->evf_mgr, bo); 342 343 bo_va = amdgpu_vm_bo_find(vm, bo); 344 if (!bo_va || --bo_va->ref_count) 345 goto out_unlock; 346 347 amdgpu_vm_bo_del(adev, bo_va); 348 amdgpu_vm_bo_update_shared(bo); 349 if (!amdgpu_vm_ready(vm)) 350 goto out_unlock; 351 352 r = amdgpu_vm_clear_freed(adev, vm, &fence); 353 if (unlikely(r < 0) && !drm_dev_is_unplugged(adev_to_drm(adev))) 354 dev_err(adev->dev, "failed to clear page " 355 "tables on GEM object close (%ld)\n", r); 356 if (r || !fence) 357 goto out_unlock; 358 359 amdgpu_bo_fence(bo, fence, true); 360 dma_fence_put(fence); 361 362 out_unlock: 363 if (r && !drm_dev_is_unplugged(adev_to_drm(adev))) 364 dev_err(adev->dev, "leaking bo va (%ld)\n", r); 365 drm_exec_fini(&exec); 366 } 367 368 static int amdgpu_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 369 { 370 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); 371 372 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) 373 return -EPERM; 374 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) 375 return -EPERM; 376 377 /* Workaround for Thunk bug creating PROT_NONE,MAP_PRIVATE mappings 378 * for debugger access to invisible VRAM. Should have used MAP_SHARED 379 * instead. Clearing VM_MAYWRITE prevents the mapping from ever 380 * becoming writable and makes is_cow_mapping(vm_flags) false. 381 */ 382 if (is_cow_mapping(vma->vm_flags) && 383 !(vma->vm_flags & VM_ACCESS_FLAGS)) 384 vm_flags_clear(vma, VM_MAYWRITE); 385 386 return drm_gem_ttm_mmap(obj, vma); 387 } 388 389 const struct drm_gem_object_funcs amdgpu_gem_object_funcs = { 390 .free = amdgpu_gem_object_free, 391 .open = amdgpu_gem_object_open, 392 .close = amdgpu_gem_object_close, 393 .export = amdgpu_gem_prime_export, 394 .vmap = drm_gem_ttm_vmap, 395 .vunmap = drm_gem_ttm_vunmap, 396 .mmap = amdgpu_gem_object_mmap, 397 .vm_ops = &amdgpu_gem_vm_ops, 398 }; 399 400 /* 401 * GEM ioctls. 402 */ 403 int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data, 404 struct drm_file *filp) 405 { 406 struct amdgpu_device *adev = drm_to_adev(dev); 407 struct amdgpu_fpriv *fpriv = filp->driver_priv; 408 struct amdgpu_vm *vm = &fpriv->vm; 409 union drm_amdgpu_gem_create *args = data; 410 uint64_t flags = args->in.domain_flags; 411 uint64_t size = args->in.bo_size; 412 struct dma_resv *resv = NULL; 413 struct drm_gem_object *gobj; 414 uint32_t handle, initial_domain; 415 int r; 416 417 /* reject invalid gem flags */ 418 if (flags & ~AMDGPU_GEM_CREATE_SETTABLE_MASK) 419 return -EINVAL; 420 421 /* reject invalid gem domains */ 422 if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK) 423 return -EINVAL; 424 425 if (!amdgpu_is_tmz(adev) && (flags & AMDGPU_GEM_CREATE_ENCRYPTED)) { 426 DRM_NOTE_ONCE("Cannot allocate secure buffer since TMZ is disabled\n"); 427 return -EINVAL; 428 } 429 430 /* always clear VRAM */ 431 flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED; 432 433 /* create a gem object to contain this object in */ 434 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS | 435 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) { 436 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) { 437 /* if gds bo is created from user space, it must be 438 * passed to bo list 439 */ 440 DRM_ERROR("GDS bo cannot be per-vm-bo\n"); 441 return -EINVAL; 442 } 443 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS; 444 } 445 446 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) { 447 r = amdgpu_bo_reserve(vm->root.bo, false); 448 if (r) 449 return r; 450 451 resv = vm->root.bo->tbo.base.resv; 452 } 453 454 initial_domain = (u32)(0xffffffff & args->in.domains); 455 retry: 456 r = amdgpu_gem_object_create(adev, size, args->in.alignment, 457 initial_domain, 458 flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1); 459 if (r && r != -ERESTARTSYS) { 460 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) { 461 flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 462 goto retry; 463 } 464 465 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) { 466 initial_domain |= AMDGPU_GEM_DOMAIN_GTT; 467 goto retry; 468 } 469 DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n", 470 size, initial_domain, args->in.alignment, r); 471 } 472 473 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) { 474 if (!r) { 475 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj); 476 477 abo->parent = amdgpu_bo_ref(vm->root.bo); 478 } 479 amdgpu_bo_unreserve(vm->root.bo); 480 } 481 if (r) 482 return r; 483 484 r = drm_gem_handle_create(filp, gobj, &handle); 485 /* drop reference from allocate - handle holds it now */ 486 drm_gem_object_put(gobj); 487 if (r) 488 return r; 489 490 memset(args, 0, sizeof(*args)); 491 args->out.handle = handle; 492 return 0; 493 } 494 495 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, 496 struct drm_file *filp) 497 { 498 struct ttm_operation_ctx ctx = { true, false }; 499 struct amdgpu_device *adev = drm_to_adev(dev); 500 struct drm_amdgpu_gem_userptr *args = data; 501 struct amdgpu_fpriv *fpriv = filp->driver_priv; 502 struct drm_gem_object *gobj; 503 struct amdgpu_hmm_range *range; 504 struct amdgpu_bo *bo; 505 uint32_t handle; 506 int r; 507 508 args->addr = untagged_addr(args->addr); 509 510 if (offset_in_page(args->addr | args->size)) 511 return -EINVAL; 512 513 if (!access_ok((void __user *)(uintptr_t)args->addr, args->size)) 514 return -EFAULT; 515 516 /* reject unknown flag values */ 517 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | 518 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE | 519 AMDGPU_GEM_USERPTR_REGISTER)) 520 return -EINVAL; 521 522 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) && 523 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) { 524 525 /* if we want to write to it we must install a MMU notifier */ 526 return -EACCES; 527 } 528 529 /* create a gem object to contain this object in */ 530 r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU, 531 0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1); 532 if (r) 533 return r; 534 535 bo = gem_to_amdgpu_bo(gobj); 536 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT; 537 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT; 538 r = amdgpu_ttm_tt_set_userptr(&bo->tbo, args->addr, args->flags); 539 if (r) 540 goto release_object; 541 542 r = amdgpu_hmm_register(bo, args->addr); 543 if (r) 544 goto release_object; 545 546 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) { 547 range = amdgpu_hmm_range_alloc(NULL); 548 if (unlikely(!range)) 549 return -ENOMEM; 550 r = amdgpu_ttm_tt_get_user_pages(bo, range); 551 if (r) { 552 amdgpu_hmm_range_free(range); 553 goto release_object; 554 } 555 r = amdgpu_bo_reserve(bo, true); 556 if (r) 557 goto user_pages_done; 558 559 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range); 560 561 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); 562 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 563 amdgpu_bo_unreserve(bo); 564 if (r) 565 goto user_pages_done; 566 } 567 568 r = drm_gem_handle_create(filp, gobj, &handle); 569 if (r) 570 goto user_pages_done; 571 572 args->handle = handle; 573 574 user_pages_done: 575 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) 576 amdgpu_hmm_range_free(range); 577 release_object: 578 drm_gem_object_put(gobj); 579 580 return r; 581 } 582 583 int amdgpu_mode_dumb_mmap(struct drm_file *filp, 584 struct drm_device *dev, 585 uint32_t handle, uint64_t *offset_p) 586 { 587 struct drm_gem_object *gobj; 588 struct amdgpu_bo *robj; 589 590 gobj = drm_gem_object_lookup(filp, handle); 591 if (!gobj) 592 return -ENOENT; 593 594 robj = gem_to_amdgpu_bo(gobj); 595 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) || 596 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) { 597 drm_gem_object_put(gobj); 598 return -EPERM; 599 } 600 *offset_p = amdgpu_bo_mmap_offset(robj); 601 drm_gem_object_put(gobj); 602 return 0; 603 } 604 605 int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data, 606 struct drm_file *filp) 607 { 608 union drm_amdgpu_gem_mmap *args = data; 609 uint32_t handle = args->in.handle; 610 611 memset(args, 0, sizeof(*args)); 612 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr); 613 } 614 615 /** 616 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value 617 * 618 * @timeout_ns: timeout in ns 619 * 620 * Calculate the timeout in jiffies from an absolute timeout in ns. 621 */ 622 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns) 623 { 624 unsigned long timeout_jiffies; 625 ktime_t timeout; 626 627 /* clamp timeout if it's to large */ 628 if (((int64_t)timeout_ns) < 0) 629 return MAX_SCHEDULE_TIMEOUT; 630 631 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get()); 632 if (ktime_to_ns(timeout) < 0) 633 return 0; 634 635 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout)); 636 /* clamp timeout to avoid unsigned-> signed overflow */ 637 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT) 638 return MAX_SCHEDULE_TIMEOUT - 1; 639 640 return timeout_jiffies; 641 } 642 643 int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data, 644 struct drm_file *filp) 645 { 646 union drm_amdgpu_gem_wait_idle *args = data; 647 struct drm_gem_object *gobj; 648 struct amdgpu_bo *robj; 649 uint32_t handle = args->in.handle; 650 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout); 651 int r = 0; 652 long ret; 653 654 gobj = drm_gem_object_lookup(filp, handle); 655 if (!gobj) 656 return -ENOENT; 657 658 robj = gem_to_amdgpu_bo(gobj); 659 ret = dma_resv_wait_timeout(robj->tbo.base.resv, DMA_RESV_USAGE_READ, 660 true, timeout); 661 662 /* ret == 0 means not signaled, 663 * ret > 0 means signaled 664 * ret < 0 means interrupted before timeout 665 */ 666 if (ret >= 0) { 667 memset(args, 0, sizeof(*args)); 668 args->out.status = (ret == 0); 669 } else 670 r = ret; 671 672 drm_gem_object_put(gobj); 673 return r; 674 } 675 676 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data, 677 struct drm_file *filp) 678 { 679 struct drm_amdgpu_gem_metadata *args = data; 680 struct drm_gem_object *gobj; 681 struct amdgpu_bo *robj; 682 int r = -1; 683 684 DRM_DEBUG("%d\n", args->handle); 685 gobj = drm_gem_object_lookup(filp, args->handle); 686 if (gobj == NULL) 687 return -ENOENT; 688 robj = gem_to_amdgpu_bo(gobj); 689 690 r = amdgpu_bo_reserve(robj, false); 691 if (unlikely(r != 0)) 692 goto out; 693 694 /* Reject MMIO_REMAP BOs at IOCTL level: metadata/tiling does not apply. */ 695 if (robj->tbo.resource && 696 robj->tbo.resource->mem_type == AMDGPU_PL_MMIO_REMAP) { 697 DRM_WARN("metadata ioctl on MMIO_REMAP BO (handle %d)\n", 698 args->handle); 699 r = -EINVAL; 700 goto unreserve; 701 } 702 703 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) { 704 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info); 705 r = amdgpu_bo_get_metadata(robj, args->data.data, 706 sizeof(args->data.data), 707 &args->data.data_size_bytes, 708 &args->data.flags); 709 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) { 710 if (args->data.data_size_bytes > sizeof(args->data.data)) { 711 r = -EINVAL; 712 goto unreserve; 713 } 714 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info); 715 if (!r) 716 r = amdgpu_bo_set_metadata(robj, args->data.data, 717 args->data.data_size_bytes, 718 args->data.flags); 719 } 720 721 unreserve: 722 amdgpu_bo_unreserve(robj); 723 out: 724 drm_gem_object_put(gobj); 725 return r; 726 } 727 728 /** 729 * amdgpu_gem_va_update_vm -update the bo_va in its VM 730 * 731 * @adev: amdgpu_device pointer 732 * @vm: vm to update 733 * @bo_va: bo_va to update 734 * @operation: map, unmap or clear 735 * 736 * Update the bo_va directly after setting its address. Errors are not 737 * vital here, so they are not reported back to userspace. 738 * 739 * Returns resulting fence if freed BO(s) got cleared from the PT. 740 * otherwise stub fence in case of error. 741 */ 742 static struct dma_fence * 743 amdgpu_gem_va_update_vm(struct amdgpu_device *adev, 744 struct amdgpu_vm *vm, 745 struct amdgpu_bo_va *bo_va, 746 uint32_t operation) 747 { 748 struct dma_fence *fence; 749 int r = 0; 750 751 /* If the VM is not ready return only a stub. */ 752 if (!amdgpu_vm_ready(vm)) 753 return dma_fence_get_stub(); 754 755 756 /* 757 * First clean up any freed mappings in the VM. 758 * 759 * amdgpu_vm_clear_freed() may replace @fence with a new fence if it 760 * schedules GPU work. If nothing needs clearing, @fence can remain as 761 * the original vm->last_update. 762 */ 763 r = amdgpu_vm_clear_freed(adev, vm, &vm->last_update); 764 if (r) 765 goto error; 766 767 /* For MAP/REPLACE we also need to update the BO mappings. */ 768 if (operation == AMDGPU_VA_OP_MAP || 769 operation == AMDGPU_VA_OP_REPLACE) { 770 r = amdgpu_vm_bo_update(adev, bo_va, false); 771 if (r) 772 goto error; 773 } 774 775 /* Always update PDEs after we touched the mappings. */ 776 r = amdgpu_vm_update_pdes(adev, vm, false); 777 if (r) 778 goto error; 779 780 if ((operation == AMDGPU_VA_OP_MAP || 781 operation == AMDGPU_VA_OP_REPLACE) && 782 !amdgpu_vm_is_bo_always_valid(vm, bo_va->base.bo)) { 783 784 /* 785 * For MAP/REPLACE of non per-VM BOs we need to sync to both the 786 * bo_va->last_pt_update and vm->last_update or otherwise we 787 * potentially miss the PDE updates. 788 */ 789 fence = dma_fence_unwrap_merge(vm->last_update, 790 bo_va->last_pt_update); 791 if (!fence) { 792 /* As fallback in OOM situations */ 793 dma_fence_wait(vm->last_update, false); 794 dma_fence_wait(bo_va->last_pt_update, false); 795 fence = dma_fence_get_stub(); 796 } 797 } else { 798 fence = dma_fence_get(vm->last_update); 799 } 800 801 return fence; 802 803 error: 804 if (r && r != -ERESTARTSYS) 805 DRM_ERROR("Couldn't update BO_VA (%d)\n", r); 806 807 return dma_fence_get(vm->last_update); 808 } 809 810 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, 811 struct drm_file *filp) 812 { 813 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE | 814 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE | 815 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK | 816 AMDGPU_VM_PAGE_NOALLOC; 817 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE | 818 AMDGPU_VM_PAGE_PRT; 819 820 struct drm_amdgpu_gem_va *args = data; 821 struct drm_gem_object *gobj; 822 struct amdgpu_device *adev = drm_to_adev(dev); 823 struct amdgpu_fpriv *fpriv = filp->driver_priv; 824 struct amdgpu_bo *abo; 825 struct amdgpu_bo_va *bo_va; 826 struct drm_syncobj *timeline_syncobj = NULL; 827 struct dma_fence_chain *timeline_chain = NULL; 828 struct drm_exec exec; 829 uint64_t vm_size, tmp; 830 int r = 0; 831 832 /* Validate virtual address range against reserved regions. */ 833 if (args->va_address < AMDGPU_VA_RESERVED_BOTTOM) { 834 dev_dbg(dev->dev, 835 "va_address 0x%llx is in reserved area 0x%llx\n", 836 args->va_address, AMDGPU_VA_RESERVED_BOTTOM); 837 return -EINVAL; 838 } 839 840 if (args->va_address >= AMDGPU_GMC_HOLE_START && 841 args->va_address < AMDGPU_GMC_HOLE_END) { 842 dev_dbg(dev->dev, 843 "va_address 0x%llx is in VA hole 0x%llx-0x%llx\n", 844 args->va_address, AMDGPU_GMC_HOLE_START, 845 AMDGPU_GMC_HOLE_END); 846 return -EINVAL; 847 } 848 849 args->va_address &= AMDGPU_GMC_HOLE_MASK; 850 851 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE; 852 vm_size -= AMDGPU_VA_RESERVED_TOP; 853 if (check_add_overflow(args->va_address, args->map_size, &tmp) || tmp > vm_size) { 854 dev_dbg(dev->dev, 855 "va_address 0x%llx is in top reserved area 0x%llx\n", 856 args->va_address + args->map_size, vm_size); 857 return -EINVAL; 858 } 859 860 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) { 861 dev_dbg(dev->dev, "invalid flags combination 0x%08X\n", 862 args->flags); 863 return -EINVAL; 864 } 865 866 /* Validate operation type. */ 867 switch (args->operation) { 868 case AMDGPU_VA_OP_MAP: 869 case AMDGPU_VA_OP_UNMAP: 870 case AMDGPU_VA_OP_CLEAR: 871 case AMDGPU_VA_OP_REPLACE: 872 break; 873 default: 874 dev_dbg(dev->dev, "unsupported operation %d\n", 875 args->operation); 876 return -EINVAL; 877 } 878 879 if (args->flags & AMDGPU_VM_DELAY_UPDATE && 880 args->vm_timeline_syncobj_out) 881 return -EINVAL; 882 883 if ((args->operation != AMDGPU_VA_OP_CLEAR) && 884 !(args->flags & AMDGPU_VM_PAGE_PRT)) { 885 gobj = drm_gem_object_lookup(filp, args->handle); 886 if (gobj == NULL) 887 return -ENOENT; 888 abo = gem_to_amdgpu_bo(gobj); 889 } else { 890 gobj = NULL; 891 abo = NULL; 892 } 893 894 /* Add input syncobj fences (if any) for synchronization. */ 895 r = amdgpu_gem_add_input_fence(filp, 896 args->input_fence_syncobj_handles, 897 args->num_syncobj_handles); 898 if (r) 899 goto error_put_gobj; 900 901 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT | 902 DRM_EXEC_IGNORE_DUPLICATES, 0); 903 drm_exec_until_all_locked(&exec) { 904 if (gobj) { 905 r = drm_exec_lock_obj(&exec, gobj); 906 drm_exec_retry_on_contention(&exec); 907 if (unlikely(r)) 908 goto error; 909 } 910 911 r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2); 912 drm_exec_retry_on_contention(&exec); 913 if (unlikely(r)) 914 goto error; 915 } 916 917 /* Resolve the BO-VA mapping for this VM/BO combination. */ 918 if (abo) { 919 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo); 920 if (!bo_va) { 921 r = -ENOENT; 922 goto error; 923 } 924 } else if (args->operation != AMDGPU_VA_OP_CLEAR) { 925 bo_va = fpriv->prt_va; 926 } else { 927 bo_va = NULL; 928 } 929 930 /* 931 * Prepare the timeline syncobj node if the user requested a VM 932 * timeline update. This only allocates/looks up the syncobj and 933 * chain node; the actual fence is attached later. 934 */ 935 r = amdgpu_gem_update_timeline_node(filp, 936 args->vm_timeline_syncobj_out, 937 args->vm_timeline_point, 938 &timeline_syncobj, 939 &timeline_chain); 940 if (r) 941 goto error; 942 943 switch (args->operation) { 944 case AMDGPU_VA_OP_MAP: 945 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address, 946 args->offset_in_bo, args->map_size, 947 args->flags); 948 break; 949 case AMDGPU_VA_OP_UNMAP: 950 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address); 951 break; 952 953 case AMDGPU_VA_OP_CLEAR: 954 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm, 955 args->va_address, 956 args->map_size); 957 break; 958 case AMDGPU_VA_OP_REPLACE: 959 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address, 960 args->offset_in_bo, args->map_size, 961 args->flags); 962 break; 963 default: 964 break; 965 } 966 967 /* 968 * Once the VA operation is done, update the VM and obtain the fence 969 * that represents the last relevant update for this mapping. This 970 * fence can then be exported to the user-visible VM timeline. 971 */ 972 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && 973 (!adev->debug_vm || timeline_syncobj)) { 974 struct dma_fence *fence; 975 976 fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, 977 args->operation); 978 if (timeline_syncobj) { 979 if (!args->vm_timeline_point) { 980 /* Replace the existing fence when no point is given. */ 981 drm_syncobj_replace_fence(timeline_syncobj, 982 fence); 983 } else { 984 /* Attach the last-update fence at a specific point. */ 985 drm_syncobj_add_point(timeline_syncobj, 986 timeline_chain, 987 fence, 988 args->vm_timeline_point); 989 timeline_chain = NULL; 990 } 991 } 992 dma_fence_put(fence); 993 994 } 995 996 error: 997 dma_fence_chain_free(timeline_chain); 998 if (timeline_syncobj) 999 drm_syncobj_put(timeline_syncobj); 1000 drm_exec_fini(&exec); 1001 error_put_gobj: 1002 drm_gem_object_put(gobj); 1003 return r; 1004 } 1005 1006 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data, 1007 struct drm_file *filp) 1008 { 1009 struct drm_amdgpu_gem_op *args = data; 1010 struct drm_gem_object *gobj; 1011 struct amdgpu_vm_bo_base *base; 1012 struct amdgpu_bo *robj; 1013 struct drm_exec exec; 1014 struct amdgpu_fpriv *fpriv = filp->driver_priv; 1015 int r; 1016 1017 if (args->padding) 1018 return -EINVAL; 1019 1020 gobj = drm_gem_object_lookup(filp, args->handle); 1021 if (!gobj) 1022 return -ENOENT; 1023 1024 robj = gem_to_amdgpu_bo(gobj); 1025 1026 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT | 1027 DRM_EXEC_IGNORE_DUPLICATES, 0); 1028 drm_exec_until_all_locked(&exec) { 1029 r = drm_exec_lock_obj(&exec, gobj); 1030 drm_exec_retry_on_contention(&exec); 1031 if (r) 1032 goto out_exec; 1033 1034 if (args->op == AMDGPU_GEM_OP_GET_MAPPING_INFO) { 1035 r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 0); 1036 drm_exec_retry_on_contention(&exec); 1037 if (r) 1038 goto out_exec; 1039 } 1040 } 1041 1042 switch (args->op) { 1043 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: { 1044 struct drm_amdgpu_gem_create_in info; 1045 void __user *out = u64_to_user_ptr(args->value); 1046 1047 info.bo_size = robj->tbo.base.size; 1048 info.alignment = robj->tbo.page_alignment << PAGE_SHIFT; 1049 info.domains = robj->preferred_domains; 1050 info.domain_flags = robj->flags; 1051 drm_exec_fini(&exec); 1052 if (copy_to_user(out, &info, sizeof(info))) 1053 r = -EFAULT; 1054 break; 1055 } 1056 case AMDGPU_GEM_OP_SET_PLACEMENT: 1057 if (drm_gem_is_imported(&robj->tbo.base) && 1058 args->value & AMDGPU_GEM_DOMAIN_VRAM) { 1059 r = -EINVAL; 1060 goto out_exec; 1061 } 1062 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) { 1063 r = -EPERM; 1064 goto out_exec; 1065 } 1066 for (base = robj->vm_bo; base; base = base->next) 1067 if (amdgpu_xgmi_same_hive(amdgpu_ttm_adev(robj->tbo.bdev), 1068 amdgpu_ttm_adev(base->vm->root.bo->tbo.bdev))) { 1069 r = -EINVAL; 1070 goto out_exec; 1071 } 1072 1073 1074 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM | 1075 AMDGPU_GEM_DOMAIN_GTT | 1076 AMDGPU_GEM_DOMAIN_CPU); 1077 robj->allowed_domains = robj->preferred_domains; 1078 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM) 1079 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT; 1080 1081 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) 1082 amdgpu_vm_bo_invalidate(robj, true); 1083 drm_exec_fini(&exec); 1084 break; 1085 case AMDGPU_GEM_OP_GET_MAPPING_INFO: { 1086 struct amdgpu_bo_va *bo_va = amdgpu_vm_bo_find(&fpriv->vm, robj); 1087 struct drm_amdgpu_gem_vm_entry *vm_entries; 1088 struct amdgpu_bo_va_mapping *mapping; 1089 int num_mappings = 0; 1090 /* 1091 * num_entries is set as an input to the size of the user-allocated array of 1092 * drm_amdgpu_gem_vm_entry stored at args->value. 1093 * num_entries is sent back as output as the number of mappings the bo has. 1094 * If that number is larger than the size of the array, the ioctl must 1095 * be retried. 1096 */ 1097 if (args->num_entries > INT_MAX / sizeof(*vm_entries)) { 1098 r = -EINVAL; 1099 goto out_exec; 1100 } 1101 1102 vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL); 1103 if (!vm_entries) { 1104 r = -ENOMEM; 1105 goto out_exec; 1106 } 1107 1108 amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) { 1109 if (num_mappings < args->num_entries) { 1110 vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE; 1111 vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE; 1112 vm_entries[num_mappings].offset = mapping->offset; 1113 vm_entries[num_mappings].flags = mapping->flags; 1114 } 1115 num_mappings += 1; 1116 } 1117 1118 amdgpu_vm_bo_va_for_each_invalid_mapping(bo_va, mapping) { 1119 if (num_mappings < args->num_entries) { 1120 vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE; 1121 vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE; 1122 vm_entries[num_mappings].offset = mapping->offset; 1123 vm_entries[num_mappings].flags = mapping->flags; 1124 } 1125 num_mappings += 1; 1126 } 1127 1128 drm_exec_fini(&exec); 1129 1130 if (num_mappings > 0 && num_mappings <= args->num_entries) 1131 if (copy_to_user(u64_to_user_ptr(args->value), vm_entries, num_mappings * sizeof(*vm_entries))) 1132 r = -EFAULT; 1133 1134 args->num_entries = num_mappings; 1135 1136 kvfree(vm_entries); 1137 break; 1138 } 1139 default: 1140 drm_exec_fini(&exec); 1141 r = -EINVAL; 1142 } 1143 1144 drm_gem_object_put(gobj); 1145 return r; 1146 out_exec: 1147 drm_exec_fini(&exec); 1148 drm_gem_object_put(gobj); 1149 return r; 1150 } 1151 1152 /** 1153 * amdgpu_gem_list_handles_ioctl - get information about a process' buffer objects 1154 * 1155 * @dev: drm device pointer 1156 * @data: drm_amdgpu_gem_list_handles 1157 * @filp: drm file pointer 1158 * 1159 * num_entries is set as an input to the size of the entries array. 1160 * num_entries is sent back as output as the number of bos in the process. 1161 * If that number is larger than the size of the array, the ioctl must 1162 * be retried. 1163 * 1164 * Returns: 1165 * 0 for success, -errno for errors. 1166 */ 1167 int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, 1168 struct drm_file *filp) 1169 { 1170 struct drm_amdgpu_gem_list_handles *args = data; 1171 struct drm_amdgpu_gem_list_handles_entry *bo_entries; 1172 struct drm_gem_object *gobj; 1173 int id, ret = 0; 1174 int bo_index = 0; 1175 int num_bos = 0; 1176 1177 spin_lock(&filp->table_lock); 1178 idr_for_each_entry(&filp->object_idr, gobj, id) 1179 num_bos += 1; 1180 spin_unlock(&filp->table_lock); 1181 1182 if (args->num_entries < num_bos) { 1183 args->num_entries = num_bos; 1184 return 0; 1185 } 1186 1187 if (num_bos == 0) { 1188 args->num_entries = 0; 1189 return 0; 1190 } 1191 1192 bo_entries = kvzalloc_objs(*bo_entries, num_bos); 1193 if (!bo_entries) 1194 return -ENOMEM; 1195 1196 spin_lock(&filp->table_lock); 1197 idr_for_each_entry(&filp->object_idr, gobj, id) { 1198 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj); 1199 struct drm_amdgpu_gem_list_handles_entry *bo_entry; 1200 1201 if (bo_index >= num_bos) { 1202 ret = -EAGAIN; 1203 break; 1204 } 1205 1206 bo_entry = &bo_entries[bo_index]; 1207 1208 bo_entry->size = amdgpu_bo_size(bo); 1209 bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK; 1210 bo_entry->preferred_domains = bo->preferred_domains; 1211 bo_entry->gem_handle = id; 1212 bo_entry->alignment = bo->tbo.page_alignment; 1213 1214 if (bo->tbo.base.import_attach) 1215 bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT; 1216 1217 bo_index += 1; 1218 } 1219 spin_unlock(&filp->table_lock); 1220 1221 args->num_entries = bo_index; 1222 1223 if (!ret) 1224 if (copy_to_user(u64_to_user_ptr(args->entries), bo_entries, num_bos * sizeof(*bo_entries))) 1225 ret = -EFAULT; 1226 1227 kvfree(bo_entries); 1228 1229 return ret; 1230 } 1231 1232 static unsigned int amdgpu_gem_align_pitch(struct amdgpu_device *adev, 1233 unsigned int width, 1234 unsigned int cpp, 1235 bool tiled) 1236 { 1237 unsigned int aligned = width; 1238 unsigned int pitch_mask = 0; 1239 unsigned int pitch; 1240 1241 switch (cpp) { 1242 case 1: 1243 pitch_mask = 255; 1244 break; 1245 case 2: 1246 pitch_mask = 127; 1247 break; 1248 case 3: 1249 case 4: 1250 pitch_mask = 63; 1251 break; 1252 } 1253 1254 if (check_add_overflow(aligned, pitch_mask, &aligned)) 1255 return 0; 1256 aligned &= ~pitch_mask; 1257 if (check_mul_overflow(aligned, cpp, &pitch)) 1258 return 0; 1259 return pitch; 1260 } 1261 1262 int amdgpu_mode_dumb_create(struct drm_file *file_priv, 1263 struct drm_device *dev, 1264 struct drm_mode_create_dumb *args) 1265 { 1266 struct amdgpu_device *adev = drm_to_adev(dev); 1267 struct amdgpu_fpriv *fpriv = file_priv->driver_priv; 1268 struct drm_gem_object *gobj; 1269 uint32_t handle; 1270 u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED | 1271 AMDGPU_GEM_CREATE_CPU_GTT_USWC | 1272 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; 1273 u32 domain; 1274 int r; 1275 1276 /* 1277 * The buffer returned from this function should be cleared, but 1278 * it can only be done if the ring is enabled or we'll fail to 1279 * create the buffer. 1280 */ 1281 if (adev->mman.buffer_funcs_enabled) 1282 flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED; 1283 1284 args->pitch = amdgpu_gem_align_pitch(adev, args->width, 1285 DIV_ROUND_UP(args->bpp, 8), 0); 1286 if (!args->pitch) 1287 return -EINVAL; 1288 args->size = (u64)args->pitch * args->height; 1289 args->size = ALIGN(args->size, PAGE_SIZE); 1290 if (!args->size) 1291 return -EINVAL; 1292 domain = amdgpu_bo_get_preferred_domain(adev, 1293 amdgpu_display_supported_domains(adev, flags)); 1294 r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags, 1295 ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1); 1296 if (r) 1297 return -ENOMEM; 1298 1299 r = drm_gem_handle_create(file_priv, gobj, &handle); 1300 /* drop reference from allocate - handle holds it now */ 1301 drm_gem_object_put(gobj); 1302 if (r) 1303 return r; 1304 1305 args->handle = handle; 1306 return 0; 1307 } 1308 1309 #if defined(CONFIG_DEBUG_FS) 1310 static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused) 1311 { 1312 struct amdgpu_device *adev = m->private; 1313 struct drm_device *dev = adev_to_drm(adev); 1314 struct drm_file *file; 1315 int r; 1316 1317 r = mutex_lock_interruptible(&dev->filelist_mutex); 1318 if (r) 1319 return r; 1320 1321 list_for_each_entry(file, &dev->filelist, lhead) { 1322 struct task_struct *task; 1323 struct drm_gem_object *gobj; 1324 struct pid *pid; 1325 int id; 1326 1327 /* 1328 * Although we have a valid reference on file->pid, that does 1329 * not guarantee that the task_struct who called get_pid() is 1330 * still alive (e.g. get_pid(current) => fork() => exit()). 1331 * Therefore, we need to protect this ->comm access using RCU. 1332 */ 1333 rcu_read_lock(); 1334 pid = rcu_dereference(file->pid); 1335 task = pid_task(pid, PIDTYPE_TGID); 1336 seq_printf(m, "pid %8d command %s:\n", pid_nr(pid), 1337 task ? task->comm : "<unknown>"); 1338 rcu_read_unlock(); 1339 1340 spin_lock(&file->table_lock); 1341 idr_for_each_entry(&file->object_idr, gobj, id) { 1342 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj); 1343 1344 amdgpu_bo_print_info(id, bo, m); 1345 } 1346 spin_unlock(&file->table_lock); 1347 } 1348 1349 mutex_unlock(&dev->filelist_mutex); 1350 return 0; 1351 } 1352 1353 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_gem_info); 1354 1355 #endif 1356 1357 void amdgpu_debugfs_gem_init(struct amdgpu_device *adev) 1358 { 1359 #if defined(CONFIG_DEBUG_FS) 1360 struct drm_minor *minor = adev_to_drm(adev)->primary; 1361 struct dentry *root = minor->debugfs_root; 1362 1363 debugfs_create_file("amdgpu_gem_info", 0444, root, adev, 1364 &amdgpu_debugfs_gem_info_fops); 1365 #endif 1366 } 1367