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