1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright 2023 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 */ 24 25 #include <drm/drm_auth.h> 26 #include <drm/drm_exec.h> 27 #include <linux/pm_runtime.h> 28 29 #include "amdgpu.h" 30 #include "amdgpu_vm.h" 31 #include "amdgpu_userq.h" 32 #include "amdgpu_userq_fence.h" 33 34 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev) 35 { 36 int i; 37 u32 userq_ip_mask = 0; 38 39 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) { 40 if (adev->userq_funcs[i]) 41 userq_ip_mask |= (1 << i); 42 } 43 44 return userq_ip_mask; 45 } 46 47 static int 48 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr, 49 struct amdgpu_usermode_queue *queue) 50 { 51 struct amdgpu_device *adev = uq_mgr->adev; 52 const struct amdgpu_userq_funcs *userq_funcs = 53 adev->userq_funcs[queue->queue_type]; 54 int r = 0; 55 56 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) { 57 r = userq_funcs->unmap(uq_mgr, queue); 58 if (r) 59 queue->state = AMDGPU_USERQ_STATE_HUNG; 60 else 61 queue->state = AMDGPU_USERQ_STATE_UNMAPPED; 62 } 63 return r; 64 } 65 66 static int 67 amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr, 68 struct amdgpu_usermode_queue *queue) 69 { 70 struct amdgpu_device *adev = uq_mgr->adev; 71 const struct amdgpu_userq_funcs *userq_funcs = 72 adev->userq_funcs[queue->queue_type]; 73 int r = 0; 74 75 if (queue->state == AMDGPU_USERQ_STATE_UNMAPPED) { 76 r = userq_funcs->map(uq_mgr, queue); 77 if (r) { 78 queue->state = AMDGPU_USERQ_STATE_HUNG; 79 } else { 80 queue->state = AMDGPU_USERQ_STATE_MAPPED; 81 } 82 } 83 return r; 84 } 85 86 static void 87 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr, 88 struct amdgpu_usermode_queue *queue) 89 { 90 struct dma_fence *f = queue->last_fence; 91 int ret; 92 93 if (f && !dma_fence_is_signaled(f)) { 94 ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100)); 95 if (ret <= 0) 96 drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n", 97 f->context, f->seqno); 98 } 99 } 100 101 static void 102 amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr, 103 struct amdgpu_usermode_queue *queue, 104 int queue_id) 105 { 106 struct amdgpu_device *adev = uq_mgr->adev; 107 const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; 108 109 uq_funcs->mqd_destroy(uq_mgr, queue); 110 amdgpu_userq_fence_driver_free(queue); 111 idr_remove(&uq_mgr->userq_idr, queue_id); 112 kfree(queue); 113 } 114 115 int 116 amdgpu_userq_active(struct amdgpu_userq_mgr *uq_mgr) 117 { 118 struct amdgpu_usermode_queue *queue; 119 int queue_id; 120 int ret = 0; 121 122 mutex_lock(&uq_mgr->userq_mutex); 123 /* Resume all the queues for this process */ 124 idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) 125 ret += queue->state == AMDGPU_USERQ_STATE_MAPPED; 126 127 mutex_unlock(&uq_mgr->userq_mutex); 128 return ret; 129 } 130 131 static struct amdgpu_usermode_queue * 132 amdgpu_userq_find(struct amdgpu_userq_mgr *uq_mgr, int qid) 133 { 134 return idr_find(&uq_mgr->userq_idr, qid); 135 } 136 137 void 138 amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr, 139 struct amdgpu_eviction_fence_mgr *evf_mgr) 140 { 141 struct amdgpu_eviction_fence *ev_fence; 142 143 retry: 144 /* Flush any pending resume work to create ev_fence */ 145 flush_delayed_work(&uq_mgr->resume_work); 146 147 mutex_lock(&uq_mgr->userq_mutex); 148 spin_lock(&evf_mgr->ev_fence_lock); 149 ev_fence = evf_mgr->ev_fence; 150 spin_unlock(&evf_mgr->ev_fence_lock); 151 if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) { 152 mutex_unlock(&uq_mgr->userq_mutex); 153 /* 154 * Looks like there was no pending resume work, 155 * add one now to create a valid eviction fence 156 */ 157 schedule_delayed_work(&uq_mgr->resume_work, 0); 158 goto retry; 159 } 160 } 161 162 int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr, 163 struct amdgpu_userq_obj *userq_obj, 164 int size) 165 { 166 struct amdgpu_device *adev = uq_mgr->adev; 167 struct amdgpu_bo_param bp; 168 int r; 169 170 memset(&bp, 0, sizeof(bp)); 171 bp.byte_align = PAGE_SIZE; 172 bp.domain = AMDGPU_GEM_DOMAIN_GTT; 173 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | 174 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 175 bp.type = ttm_bo_type_kernel; 176 bp.size = size; 177 bp.resv = NULL; 178 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 179 180 r = amdgpu_bo_create(adev, &bp, &userq_obj->obj); 181 if (r) { 182 drm_file_err(uq_mgr->file, "Failed to allocate BO for userqueue (%d)", r); 183 return r; 184 } 185 186 r = amdgpu_bo_reserve(userq_obj->obj, true); 187 if (r) { 188 drm_file_err(uq_mgr->file, "Failed to reserve BO to map (%d)", r); 189 goto free_obj; 190 } 191 192 r = amdgpu_ttm_alloc_gart(&(userq_obj->obj)->tbo); 193 if (r) { 194 drm_file_err(uq_mgr->file, "Failed to alloc GART for userqueue object (%d)", r); 195 goto unresv; 196 } 197 198 r = amdgpu_bo_kmap(userq_obj->obj, &userq_obj->cpu_ptr); 199 if (r) { 200 drm_file_err(uq_mgr->file, "Failed to map BO for userqueue (%d)", r); 201 goto unresv; 202 } 203 204 userq_obj->gpu_addr = amdgpu_bo_gpu_offset(userq_obj->obj); 205 amdgpu_bo_unreserve(userq_obj->obj); 206 memset(userq_obj->cpu_ptr, 0, size); 207 return 0; 208 209 unresv: 210 amdgpu_bo_unreserve(userq_obj->obj); 211 212 free_obj: 213 amdgpu_bo_unref(&userq_obj->obj); 214 return r; 215 } 216 217 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr, 218 struct amdgpu_userq_obj *userq_obj) 219 { 220 amdgpu_bo_kunmap(userq_obj->obj); 221 amdgpu_bo_unref(&userq_obj->obj); 222 } 223 224 uint64_t 225 amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, 226 struct amdgpu_db_info *db_info, 227 struct drm_file *filp) 228 { 229 uint64_t index; 230 struct drm_gem_object *gobj; 231 struct amdgpu_userq_obj *db_obj = db_info->db_obj; 232 int r, db_size; 233 234 gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle); 235 if (gobj == NULL) { 236 drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n"); 237 return -EINVAL; 238 } 239 240 db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj)); 241 drm_gem_object_put(gobj); 242 243 /* Pin the BO before generating the index, unpin in queue destroy */ 244 r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL); 245 if (r) { 246 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 247 goto unref_bo; 248 } 249 250 r = amdgpu_bo_reserve(db_obj->obj, true); 251 if (r) { 252 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 253 goto unpin_bo; 254 } 255 256 switch (db_info->queue_type) { 257 case AMDGPU_HW_IP_GFX: 258 case AMDGPU_HW_IP_COMPUTE: 259 case AMDGPU_HW_IP_DMA: 260 db_size = sizeof(u64); 261 break; 262 263 case AMDGPU_HW_IP_VCN_ENC: 264 db_size = sizeof(u32); 265 db_info->doorbell_offset += AMDGPU_NAVI10_DOORBELL64_VCN0_1 << 1; 266 break; 267 268 case AMDGPU_HW_IP_VPE: 269 db_size = sizeof(u32); 270 db_info->doorbell_offset += AMDGPU_NAVI10_DOORBELL64_VPE << 1; 271 break; 272 273 default: 274 drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n", 275 db_info->queue_type); 276 r = -EINVAL; 277 goto unpin_bo; 278 } 279 280 index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj, 281 db_info->doorbell_offset, db_size); 282 drm_dbg_driver(adev_to_drm(uq_mgr->adev), 283 "[Usermode queues] doorbell index=%lld\n", index); 284 amdgpu_bo_unreserve(db_obj->obj); 285 return index; 286 287 unpin_bo: 288 amdgpu_bo_unpin(db_obj->obj); 289 290 unref_bo: 291 amdgpu_bo_unref(&db_obj->obj); 292 return r; 293 } 294 295 static int 296 amdgpu_userq_destroy(struct drm_file *filp, int queue_id) 297 { 298 struct amdgpu_fpriv *fpriv = filp->driver_priv; 299 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 300 struct amdgpu_device *adev = uq_mgr->adev; 301 struct amdgpu_usermode_queue *queue; 302 int r = 0; 303 304 cancel_delayed_work_sync(&uq_mgr->resume_work); 305 mutex_lock(&uq_mgr->userq_mutex); 306 307 queue = amdgpu_userq_find(uq_mgr, queue_id); 308 if (!queue) { 309 drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n"); 310 mutex_unlock(&uq_mgr->userq_mutex); 311 return -EINVAL; 312 } 313 amdgpu_userq_wait_for_last_fence(uq_mgr, queue); 314 r = amdgpu_userq_unmap_helper(uq_mgr, queue); 315 amdgpu_bo_unpin(queue->db_obj.obj); 316 amdgpu_bo_unref(&queue->db_obj.obj); 317 amdgpu_userq_cleanup(uq_mgr, queue, queue_id); 318 mutex_unlock(&uq_mgr->userq_mutex); 319 320 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 321 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 322 323 return r; 324 } 325 326 static int amdgpu_userq_priority_permit(struct drm_file *filp, 327 int priority) 328 { 329 if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH) 330 return 0; 331 332 if (capable(CAP_SYS_NICE)) 333 return 0; 334 335 if (drm_is_current_master(filp)) 336 return 0; 337 338 return -EACCES; 339 } 340 341 static int 342 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) 343 { 344 struct amdgpu_fpriv *fpriv = filp->driver_priv; 345 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 346 struct amdgpu_device *adev = uq_mgr->adev; 347 const struct amdgpu_userq_funcs *uq_funcs; 348 struct amdgpu_usermode_queue *queue; 349 struct amdgpu_db_info db_info; 350 bool skip_map_queue; 351 uint64_t index; 352 int qid, r = 0; 353 int priority = 354 (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >> 355 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT; 356 357 /* Usermode queues are only supported for GFX IP as of now */ 358 if (args->in.ip_type != AMDGPU_HW_IP_GFX && 359 args->in.ip_type != AMDGPU_HW_IP_DMA && 360 args->in.ip_type != AMDGPU_HW_IP_COMPUTE) { 361 drm_file_err(uq_mgr->file, "Usermode queue doesn't support IP type %u\n", 362 args->in.ip_type); 363 return -EINVAL; 364 } 365 366 r = amdgpu_userq_priority_permit(filp, priority); 367 if (r) 368 return r; 369 370 if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) && 371 (args->in.ip_type != AMDGPU_HW_IP_GFX) && 372 (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) && 373 !amdgpu_is_tmz(adev)) { 374 drm_file_err(uq_mgr->file, "Secure only supported on GFX/Compute queues\n"); 375 return -EINVAL; 376 } 377 378 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 379 if (r < 0) { 380 drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n"); 381 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 382 return r; 383 } 384 385 /* 386 * There could be a situation that we are creating a new queue while 387 * the other queues under this UQ_mgr are suspended. So if there is any 388 * resume work pending, wait for it to get done. 389 * 390 * This will also make sure we have a valid eviction fence ready to be used. 391 */ 392 amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr); 393 394 uq_funcs = adev->userq_funcs[args->in.ip_type]; 395 if (!uq_funcs) { 396 drm_file_err(uq_mgr->file, "Usermode queue is not supported for this IP (%u)\n", 397 args->in.ip_type); 398 r = -EINVAL; 399 goto unlock; 400 } 401 402 queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL); 403 if (!queue) { 404 drm_file_err(uq_mgr->file, "Failed to allocate memory for queue\n"); 405 r = -ENOMEM; 406 goto unlock; 407 } 408 queue->doorbell_handle = args->in.doorbell_handle; 409 queue->queue_type = args->in.ip_type; 410 queue->vm = &fpriv->vm; 411 queue->priority = priority; 412 413 db_info.queue_type = queue->queue_type; 414 db_info.doorbell_handle = queue->doorbell_handle; 415 db_info.db_obj = &queue->db_obj; 416 db_info.doorbell_offset = args->in.doorbell_offset; 417 418 /* Convert relative doorbell offset into absolute doorbell index */ 419 index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp); 420 if (index == (uint64_t)-EINVAL) { 421 drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n"); 422 kfree(queue); 423 goto unlock; 424 } 425 426 queue->doorbell_index = index; 427 xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC); 428 r = amdgpu_userq_fence_driver_alloc(adev, queue); 429 if (r) { 430 drm_file_err(uq_mgr->file, "Failed to alloc fence driver\n"); 431 goto unlock; 432 } 433 434 r = uq_funcs->mqd_create(uq_mgr, &args->in, queue); 435 if (r) { 436 drm_file_err(uq_mgr->file, "Failed to create Queue\n"); 437 amdgpu_userq_fence_driver_free(queue); 438 kfree(queue); 439 goto unlock; 440 } 441 442 443 qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL); 444 if (qid < 0) { 445 drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n"); 446 amdgpu_userq_fence_driver_free(queue); 447 uq_funcs->mqd_destroy(uq_mgr, queue); 448 kfree(queue); 449 r = -ENOMEM; 450 goto unlock; 451 } 452 453 /* don't map the queue if scheduling is halted */ 454 mutex_lock(&adev->userq_mutex); 455 if (adev->userq_halt_for_enforce_isolation && 456 ((queue->queue_type == AMDGPU_HW_IP_GFX) || 457 (queue->queue_type == AMDGPU_HW_IP_COMPUTE))) 458 skip_map_queue = true; 459 else 460 skip_map_queue = false; 461 if (!skip_map_queue) { 462 r = amdgpu_userq_map_helper(uq_mgr, queue); 463 if (r) { 464 mutex_unlock(&adev->userq_mutex); 465 drm_file_err(uq_mgr->file, "Failed to map Queue\n"); 466 idr_remove(&uq_mgr->userq_idr, qid); 467 amdgpu_userq_fence_driver_free(queue); 468 uq_funcs->mqd_destroy(uq_mgr, queue); 469 kfree(queue); 470 goto unlock; 471 } 472 } 473 mutex_unlock(&adev->userq_mutex); 474 475 476 args->out.queue_id = qid; 477 478 unlock: 479 mutex_unlock(&uq_mgr->userq_mutex); 480 481 return r; 482 } 483 484 int amdgpu_userq_ioctl(struct drm_device *dev, void *data, 485 struct drm_file *filp) 486 { 487 union drm_amdgpu_userq *args = data; 488 int r; 489 490 switch (args->in.op) { 491 case AMDGPU_USERQ_OP_CREATE: 492 if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK | 493 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE)) 494 return -EINVAL; 495 r = amdgpu_userq_create(filp, args); 496 if (r) 497 drm_file_err(filp, "Failed to create usermode queue\n"); 498 break; 499 500 case AMDGPU_USERQ_OP_FREE: 501 if (args->in.ip_type || 502 args->in.doorbell_handle || 503 args->in.doorbell_offset || 504 args->in.flags || 505 args->in.queue_va || 506 args->in.queue_size || 507 args->in.rptr_va || 508 args->in.wptr_va || 509 args->in.wptr_va || 510 args->in.mqd || 511 args->in.mqd_size) 512 return -EINVAL; 513 r = amdgpu_userq_destroy(filp, args->in.queue_id); 514 if (r) 515 drm_file_err(filp, "Failed to destroy usermode queue\n"); 516 break; 517 518 default: 519 drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op); 520 return -EINVAL; 521 } 522 523 return r; 524 } 525 526 static int 527 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) 528 { 529 struct amdgpu_usermode_queue *queue; 530 int queue_id; 531 int ret = 0, r; 532 533 /* Resume all the queues for this process */ 534 idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) { 535 r = amdgpu_userq_map_helper(uq_mgr, queue); 536 if (r) 537 ret = r; 538 } 539 540 if (ret) 541 drm_file_err(uq_mgr->file, "Failed to map all the queues\n"); 542 return ret; 543 } 544 545 static int 546 amdgpu_userq_validate_vm_bo(void *_unused, struct amdgpu_bo *bo) 547 { 548 struct ttm_operation_ctx ctx = { false, false }; 549 int ret; 550 551 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 552 553 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 554 if (ret) 555 DRM_ERROR("Fail to validate\n"); 556 557 return ret; 558 } 559 560 static int 561 amdgpu_userq_validate_bos(struct amdgpu_userq_mgr *uq_mgr) 562 { 563 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 564 struct amdgpu_vm *vm = &fpriv->vm; 565 struct amdgpu_device *adev = uq_mgr->adev; 566 struct amdgpu_bo_va *bo_va; 567 struct ww_acquire_ctx *ticket; 568 struct drm_exec exec; 569 struct amdgpu_bo *bo; 570 struct dma_resv *resv; 571 bool clear, unlock; 572 int ret = 0; 573 574 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 575 drm_exec_until_all_locked(&exec) { 576 ret = amdgpu_vm_lock_pd(vm, &exec, 2); 577 drm_exec_retry_on_contention(&exec); 578 if (unlikely(ret)) { 579 drm_file_err(uq_mgr->file, "Failed to lock PD\n"); 580 goto unlock_all; 581 } 582 583 /* Lock the done list */ 584 list_for_each_entry(bo_va, &vm->done, base.vm_status) { 585 bo = bo_va->base.bo; 586 if (!bo) 587 continue; 588 589 ret = drm_exec_lock_obj(&exec, &bo->tbo.base); 590 drm_exec_retry_on_contention(&exec); 591 if (unlikely(ret)) 592 goto unlock_all; 593 } 594 } 595 596 spin_lock(&vm->status_lock); 597 while (!list_empty(&vm->moved)) { 598 bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va, 599 base.vm_status); 600 spin_unlock(&vm->status_lock); 601 602 /* Per VM BOs never need to bo cleared in the page tables */ 603 ret = amdgpu_vm_bo_update(adev, bo_va, false); 604 if (ret) 605 goto unlock_all; 606 spin_lock(&vm->status_lock); 607 } 608 609 ticket = &exec.ticket; 610 while (!list_empty(&vm->invalidated)) { 611 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, 612 base.vm_status); 613 resv = bo_va->base.bo->tbo.base.resv; 614 spin_unlock(&vm->status_lock); 615 616 bo = bo_va->base.bo; 617 ret = amdgpu_userq_validate_vm_bo(NULL, bo); 618 if (ret) { 619 drm_file_err(uq_mgr->file, "Failed to validate BO\n"); 620 goto unlock_all; 621 } 622 623 /* Try to reserve the BO to avoid clearing its ptes */ 624 if (!adev->debug_vm && dma_resv_trylock(resv)) { 625 clear = false; 626 unlock = true; 627 /* The caller is already holding the reservation lock */ 628 } else if (dma_resv_locking_ctx(resv) == ticket) { 629 clear = false; 630 unlock = false; 631 /* Somebody else is using the BO right now */ 632 } else { 633 clear = true; 634 unlock = false; 635 } 636 637 ret = amdgpu_vm_bo_update(adev, bo_va, clear); 638 639 if (unlock) 640 dma_resv_unlock(resv); 641 if (ret) 642 goto unlock_all; 643 644 spin_lock(&vm->status_lock); 645 } 646 spin_unlock(&vm->status_lock); 647 648 ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec); 649 if (ret) 650 drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n"); 651 652 unlock_all: 653 drm_exec_fini(&exec); 654 return ret; 655 } 656 657 static void amdgpu_userq_restore_worker(struct work_struct *work) 658 { 659 struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work); 660 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 661 int ret; 662 663 flush_work(&fpriv->evf_mgr.suspend_work.work); 664 665 mutex_lock(&uq_mgr->userq_mutex); 666 667 ret = amdgpu_userq_validate_bos(uq_mgr); 668 if (ret) { 669 drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n"); 670 goto unlock; 671 } 672 673 ret = amdgpu_userq_restore_all(uq_mgr); 674 if (ret) { 675 drm_file_err(uq_mgr->file, "Failed to restore all queues\n"); 676 goto unlock; 677 } 678 679 unlock: 680 mutex_unlock(&uq_mgr->userq_mutex); 681 } 682 683 static int 684 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) 685 { 686 struct amdgpu_usermode_queue *queue; 687 int queue_id; 688 int ret = 0, r; 689 690 /* Try to unmap all the queues in this process ctx */ 691 idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) { 692 r = amdgpu_userq_unmap_helper(uq_mgr, queue); 693 if (r) 694 ret = r; 695 } 696 697 if (ret) 698 drm_file_err(uq_mgr->file, "Couldn't unmap all the queues\n"); 699 return ret; 700 } 701 702 static int 703 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) 704 { 705 struct amdgpu_usermode_queue *queue; 706 int queue_id, ret; 707 708 idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) { 709 struct dma_fence *f = queue->last_fence; 710 711 if (!f || dma_fence_is_signaled(f)) 712 continue; 713 ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100)); 714 if (ret <= 0) { 715 drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n", 716 f->context, f->seqno); 717 return -ETIMEDOUT; 718 } 719 } 720 721 return 0; 722 } 723 724 void 725 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr, 726 struct amdgpu_eviction_fence *ev_fence) 727 { 728 int ret; 729 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 730 struct amdgpu_eviction_fence_mgr *evf_mgr = &fpriv->evf_mgr; 731 732 /* Wait for any pending userqueue fence work to finish */ 733 ret = amdgpu_userq_wait_for_signal(uq_mgr); 734 if (ret) { 735 drm_file_err(uq_mgr->file, "Not evicting userqueue, timeout waiting for work\n"); 736 return; 737 } 738 739 ret = amdgpu_userq_evict_all(uq_mgr); 740 if (ret) { 741 drm_file_err(uq_mgr->file, "Failed to evict userqueue\n"); 742 return; 743 } 744 745 /* Signal current eviction fence */ 746 amdgpu_eviction_fence_signal(evf_mgr, ev_fence); 747 748 if (evf_mgr->fd_closing) { 749 cancel_delayed_work_sync(&uq_mgr->resume_work); 750 return; 751 } 752 753 /* Schedule a resume work */ 754 schedule_delayed_work(&uq_mgr->resume_work, 0); 755 } 756 757 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv, 758 struct amdgpu_device *adev) 759 { 760 mutex_init(&userq_mgr->userq_mutex); 761 idr_init_base(&userq_mgr->userq_idr, 1); 762 userq_mgr->adev = adev; 763 userq_mgr->file = file_priv; 764 765 mutex_lock(&adev->userq_mutex); 766 list_add(&userq_mgr->list, &adev->userq_mgr_list); 767 mutex_unlock(&adev->userq_mutex); 768 769 INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker); 770 return 0; 771 } 772 773 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr) 774 { 775 struct amdgpu_device *adev = userq_mgr->adev; 776 struct amdgpu_usermode_queue *queue; 777 struct amdgpu_userq_mgr *uqm, *tmp; 778 uint32_t queue_id; 779 780 cancel_delayed_work_sync(&userq_mgr->resume_work); 781 782 mutex_lock(&adev->userq_mutex); 783 mutex_lock(&userq_mgr->userq_mutex); 784 idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) { 785 amdgpu_userq_wait_for_last_fence(userq_mgr, queue); 786 amdgpu_userq_unmap_helper(userq_mgr, queue); 787 amdgpu_userq_cleanup(userq_mgr, queue, queue_id); 788 } 789 790 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 791 if (uqm == userq_mgr) { 792 list_del(&uqm->list); 793 break; 794 } 795 } 796 idr_destroy(&userq_mgr->userq_idr); 797 mutex_unlock(&userq_mgr->userq_mutex); 798 mutex_unlock(&adev->userq_mutex); 799 mutex_destroy(&userq_mgr->userq_mutex); 800 } 801 802 int amdgpu_userq_suspend(struct amdgpu_device *adev) 803 { 804 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 805 struct amdgpu_usermode_queue *queue; 806 struct amdgpu_userq_mgr *uqm, *tmp; 807 int queue_id; 808 int ret = 0, r; 809 810 if (!ip_mask) 811 return 0; 812 813 mutex_lock(&adev->userq_mutex); 814 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 815 cancel_delayed_work_sync(&uqm->resume_work); 816 mutex_lock(&uqm->userq_mutex); 817 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 818 r = amdgpu_userq_unmap_helper(uqm, queue); 819 if (r) 820 ret = r; 821 } 822 mutex_unlock(&uqm->userq_mutex); 823 } 824 mutex_unlock(&adev->userq_mutex); 825 return ret; 826 } 827 828 int amdgpu_userq_resume(struct amdgpu_device *adev) 829 { 830 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 831 struct amdgpu_usermode_queue *queue; 832 struct amdgpu_userq_mgr *uqm, *tmp; 833 int queue_id; 834 int ret = 0, r; 835 836 if (!ip_mask) 837 return 0; 838 839 mutex_lock(&adev->userq_mutex); 840 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 841 mutex_lock(&uqm->userq_mutex); 842 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 843 r = amdgpu_userq_map_helper(uqm, queue); 844 if (r) 845 ret = r; 846 } 847 mutex_unlock(&uqm->userq_mutex); 848 } 849 mutex_unlock(&adev->userq_mutex); 850 return ret; 851 } 852 853 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, 854 u32 idx) 855 { 856 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 857 struct amdgpu_usermode_queue *queue; 858 struct amdgpu_userq_mgr *uqm, *tmp; 859 int queue_id; 860 int ret = 0, r; 861 862 /* only need to stop gfx/compute */ 863 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 864 return 0; 865 866 mutex_lock(&adev->userq_mutex); 867 if (adev->userq_halt_for_enforce_isolation) 868 dev_warn(adev->dev, "userq scheduling already stopped!\n"); 869 adev->userq_halt_for_enforce_isolation = true; 870 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 871 cancel_delayed_work_sync(&uqm->resume_work); 872 mutex_lock(&uqm->userq_mutex); 873 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 874 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 875 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 876 (queue->xcp_id == idx)) { 877 r = amdgpu_userq_unmap_helper(uqm, queue); 878 if (r) 879 ret = r; 880 } 881 } 882 mutex_unlock(&uqm->userq_mutex); 883 } 884 mutex_unlock(&adev->userq_mutex); 885 return ret; 886 } 887 888 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, 889 u32 idx) 890 { 891 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 892 struct amdgpu_usermode_queue *queue; 893 struct amdgpu_userq_mgr *uqm, *tmp; 894 int queue_id; 895 int ret = 0, r; 896 897 /* only need to stop gfx/compute */ 898 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 899 return 0; 900 901 mutex_lock(&adev->userq_mutex); 902 if (!adev->userq_halt_for_enforce_isolation) 903 dev_warn(adev->dev, "userq scheduling already started!\n"); 904 adev->userq_halt_for_enforce_isolation = false; 905 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 906 mutex_lock(&uqm->userq_mutex); 907 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 908 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 909 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 910 (queue->xcp_id == idx)) { 911 r = amdgpu_userq_map_helper(uqm, queue); 912 if (r) 913 ret = r; 914 } 915 } 916 mutex_unlock(&uqm->userq_mutex); 917 } 918 mutex_unlock(&adev->userq_mutex); 919 return ret; 920 } 921