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