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