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