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