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 #include <linux/overflow.h> 29 #include <drm/drm_drv.h> 30 31 #include "amdgpu.h" 32 #include "amdgpu_reset.h" 33 #include "amdgpu_vm.h" 34 #include "amdgpu_userq.h" 35 #include "amdgpu_hmm.h" 36 #include "amdgpu_userq_fence.h" 37 #include "amdgpu_trace.h" 38 39 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev) 40 { 41 int i; 42 u32 userq_ip_mask = 0; 43 44 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) { 45 if (adev->userq_funcs[i]) 46 userq_ip_mask |= (1 << i); 47 } 48 49 return userq_ip_mask; 50 } 51 52 static bool amdgpu_userq_is_reset_type_supported(struct amdgpu_device *adev, 53 enum amdgpu_ring_type ring_type, int reset_type) 54 { 55 56 if (ring_type < 0 || ring_type >= AMDGPU_RING_TYPE_MAX) 57 return false; 58 59 switch (ring_type) { 60 case AMDGPU_RING_TYPE_GFX: 61 if (adev->gfx.gfx_supported_reset & reset_type) 62 return true; 63 break; 64 case AMDGPU_RING_TYPE_COMPUTE: 65 if (adev->gfx.compute_supported_reset & reset_type) 66 return true; 67 break; 68 case AMDGPU_RING_TYPE_SDMA: 69 if (adev->sdma.supported_reset & reset_type) 70 return true; 71 break; 72 case AMDGPU_RING_TYPE_VCN_DEC: 73 case AMDGPU_RING_TYPE_VCN_ENC: 74 if (adev->vcn.supported_reset & reset_type) 75 return true; 76 break; 77 case AMDGPU_RING_TYPE_VCN_JPEG: 78 if (adev->jpeg.supported_reset & reset_type) 79 return true; 80 break; 81 default: 82 break; 83 } 84 return false; 85 } 86 87 static void amdgpu_userq_mgr_reset_work(struct work_struct *work) 88 { 89 struct amdgpu_userq_mgr *uq_mgr = 90 container_of(work, struct amdgpu_userq_mgr, 91 reset_work); 92 struct amdgpu_device *adev = uq_mgr->adev; 93 struct amdgpu_reset_context reset_context; 94 95 if (unlikely(adev->debug_disable_gpu_ring_reset)) { 96 dev_err(adev->dev, "userq reset disabled by debug mask\n"); 97 return; 98 } 99 100 /* 101 * If GPU recovery feature is disabled system-wide, 102 * skip all reset detection logic 103 */ 104 if (!amdgpu_gpu_recovery) 105 return; 106 107 memset(&reset_context, 0, sizeof(reset_context)); 108 109 reset_context.method = AMD_RESET_METHOD_NONE; 110 reset_context.reset_req_dev = adev; 111 reset_context.src = AMDGPU_RESET_SRC_USERQ; 112 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 113 /*set_bit(AMDGPU_SKIP_COREDUMP, &reset_context.flags);*/ 114 115 amdgpu_device_gpu_recover(adev, NULL, &reset_context); 116 } 117 118 static void amdgpu_userq_hang_detect_work(struct work_struct *work) 119 { 120 struct amdgpu_usermode_queue *queue = 121 container_of(work, struct amdgpu_usermode_queue, 122 hang_detect_work.work); 123 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 124 struct amdgpu_device *adev = uq_mgr->adev; 125 const struct amdgpu_userq_funcs *userq_funcs = 126 adev->userq_funcs[queue->queue_type]; 127 struct drm_wedge_task_info *info = NULL; 128 struct amdgpu_task_info *ti = NULL; 129 bool gpu_reset = false; 130 131 if (unlikely(adev->debug_disable_gpu_ring_reset)) { 132 dev_err(adev->dev, "userq reset disabled by debug mask\n"); 133 return; 134 } 135 136 /* 137 * If GPU recovery feature is disabled system-wide, 138 * skip all reset detection logic 139 */ 140 if (!amdgpu_gpu_recovery) 141 return; 142 143 if (queue->vm && queue->vm->pasid) { 144 ti = amdgpu_vm_get_task_info_pasid(adev, queue->vm->pasid); 145 if (ti) { 146 amdgpu_vm_print_task_info(adev, ti); 147 info = &ti->task; 148 } 149 } 150 151 if (amdgpu_userq_is_reset_type_supported(adev, queue->queue_type, 152 AMDGPU_RESET_TYPE_PER_QUEUE)) { 153 int r; 154 155 if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) 156 r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL, 157 queue, NULL, NULL); 158 else 159 r = userq_funcs->reset(queue); 160 if (r) { 161 gpu_reset = true; 162 } else { 163 atomic_inc(&adev->gpu_reset_counter); 164 amdgpu_userq_fence_driver_force_completion(queue); 165 drm_dev_wedged_event(adev_to_drm(adev), DRM_WEDGE_RECOVERY_NONE, info); 166 } 167 } else { 168 gpu_reset = true; 169 } 170 amdgpu_vm_put_task_info(ti); 171 172 /* 173 * Don't schedule the work here! Scheduling or queue work from one reset 174 * handler to another is illegal if you don't take extra precautions! 175 */ 176 if (gpu_reset) 177 amdgpu_userq_mgr_reset_work(&queue->userq_mgr->reset_work); 178 } 179 180 /* 181 * Start hang detection for a user queue fence. A delayed work will be scheduled 182 * to reset the queues when the fence doesn't signal in time. 183 */ 184 void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue) 185 { 186 struct amdgpu_device *adev; 187 unsigned long timeout_ms; 188 189 adev = queue->userq_mgr->adev; 190 /* Determine timeout based on queue type */ 191 switch (queue->queue_type) { 192 case AMDGPU_RING_TYPE_GFX: 193 timeout_ms = adev->gfx_timeout; 194 break; 195 case AMDGPU_RING_TYPE_COMPUTE: 196 timeout_ms = adev->compute_timeout; 197 break; 198 case AMDGPU_RING_TYPE_SDMA: 199 timeout_ms = adev->sdma_timeout; 200 break; 201 default: 202 timeout_ms = adev->gfx_timeout; 203 break; 204 } 205 206 queue_delayed_work(adev->reset_domain->wq, &queue->hang_detect_work, 207 msecs_to_jiffies(timeout_ms)); 208 } 209 210 void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell) 211 { 212 struct xarray *xa = &adev->userq_doorbell_xa; 213 struct amdgpu_usermode_queue *queue; 214 unsigned long flags; 215 int r; 216 217 xa_lock_irqsave(xa, flags); 218 queue = xa_load(xa, doorbell); 219 if (queue) { 220 r = amdgpu_userq_fence_driver_process(queue->fence_drv); 221 /* 222 * We are in interrupt context here, this *can't* wait for 223 * reset work to finish. 224 */ 225 if (r >= 0) 226 cancel_delayed_work(&queue->hang_detect_work); 227 228 /* Restart the timer when there are still fences pending */ 229 if (r == 1) 230 amdgpu_userq_start_hang_detect_work(queue); 231 } 232 xa_unlock_irqrestore(xa, flags); 233 } 234 235 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev, 236 struct amdgpu_usermode_queue *queue, 237 u64 addr, u64 expected_size, 238 u64 *va_out) 239 { 240 struct amdgpu_bo_va_mapping *va_map; 241 struct amdgpu_vm *vm = queue->vm; 242 u64 start_addr; 243 u64 end_addr; 244 u64 start_page; 245 246 /* Caller must hold vm->root.bo reservation */ 247 dma_resv_assert_held(queue->vm->root.bo->tbo.base.resv); 248 249 if (!expected_size) 250 return -EINVAL; 251 252 start_addr = addr & AMDGPU_GMC_HOLE_MASK; 253 if (check_add_overflow(start_addr, expected_size - 1, &end_addr)) 254 return -EINVAL; 255 256 start_page = start_addr >> AMDGPU_GPU_PAGE_SHIFT; 257 258 va_map = amdgpu_vm_bo_lookup_mapping(vm, start_page); 259 if (!va_map) 260 return -EINVAL; 261 262 /* Lookup guarantees start_page is mapped; ensure full span is covered. */ 263 if ((end_addr >> AMDGPU_GPU_PAGE_SHIFT) <= va_map->last) { 264 va_map->bo_va->userq_va_mapped = true; 265 *va_out = start_page; 266 return 0; 267 } 268 269 return -EINVAL; 270 } 271 272 static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr) 273 { 274 struct amdgpu_bo_va_mapping *mapping; 275 bool r; 276 277 dma_resv_assert_held(vm->root.bo->tbo.base.resv); 278 279 mapping = amdgpu_vm_bo_lookup_mapping(vm, addr); 280 if (!IS_ERR_OR_NULL(mapping) && mapping->bo_va->userq_va_mapped) 281 r = true; 282 else 283 r = false; 284 285 return r; 286 } 287 288 static bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue) 289 { 290 int i; 291 bool mapped; 292 293 for (i = 0; i < ARRAY_SIZE(queue->userq_vas.va_array); i++) { 294 if (!queue->userq_vas.va_array[i]) 295 continue; 296 297 mapped = amdgpu_userq_buffer_va_mapped(queue->vm, 298 queue->userq_vas.va_array[i]); 299 dev_dbg(queue->userq_mgr->adev->dev, 300 "validate the userq mapping:%p va:%llx r:%d\n", 301 queue, queue->userq_vas.va_array[i], mapped); 302 303 if (!mapped) 304 return false; 305 } 306 307 return true; 308 } 309 310 311 312 static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue) 313 { 314 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 315 struct amdgpu_device *adev = uq_mgr->adev; 316 const struct amdgpu_userq_funcs *userq_funcs = 317 adev->userq_funcs[queue->queue_type]; 318 int r; 319 320 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) { 321 trace_amdgpu_userq_state_start(queue); 322 323 r = userq_funcs->preempt(queue); 324 if (r) { 325 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG); 326 queue->state = AMDGPU_USERQ_STATE_HUNG; 327 return r; 328 } else { 329 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_PREEMPTED); 330 queue->state = AMDGPU_USERQ_STATE_PREEMPTED; 331 } 332 } 333 return 0; 334 } 335 336 static int amdgpu_userq_restore_helper(struct amdgpu_usermode_queue *queue) 337 { 338 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 339 struct amdgpu_device *adev = uq_mgr->adev; 340 const struct amdgpu_userq_funcs *userq_funcs = 341 adev->userq_funcs[queue->queue_type]; 342 int r = 0; 343 344 if (queue->state == AMDGPU_USERQ_STATE_PREEMPTED) { 345 trace_amdgpu_userq_state_start(queue); 346 347 r = userq_funcs->restore(queue); 348 if (r) { 349 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG); 350 queue->state = AMDGPU_USERQ_STATE_HUNG; 351 } else { 352 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_MAPPED); 353 queue->state = AMDGPU_USERQ_STATE_MAPPED; 354 } 355 } 356 357 return r; 358 } 359 360 static int amdgpu_userq_unmap_helper(struct amdgpu_usermode_queue *queue) 361 { 362 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 363 struct amdgpu_device *adev = uq_mgr->adev; 364 const struct amdgpu_userq_funcs *userq_funcs = 365 adev->userq_funcs[queue->queue_type]; 366 int r; 367 368 if ((queue->state == AMDGPU_USERQ_STATE_MAPPED) || 369 (queue->state == AMDGPU_USERQ_STATE_PREEMPTED)) { 370 trace_amdgpu_userq_state_start(queue); 371 372 r = userq_funcs->unmap(queue); 373 if (r) { 374 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG); 375 queue->state = AMDGPU_USERQ_STATE_HUNG; 376 return r; 377 } else { 378 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_UNMAPPED); 379 queue->state = AMDGPU_USERQ_STATE_UNMAPPED; 380 } 381 } 382 383 return 0; 384 } 385 386 static int amdgpu_userq_map_helper(struct amdgpu_usermode_queue *queue) 387 { 388 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 389 struct amdgpu_device *adev = uq_mgr->adev; 390 const struct amdgpu_userq_funcs *userq_funcs = 391 adev->userq_funcs[queue->queue_type]; 392 int r; 393 394 if (queue->state == AMDGPU_USERQ_STATE_UNMAPPED) { 395 trace_amdgpu_userq_state_start(queue); 396 397 r = userq_funcs->map(queue); 398 if (r) { 399 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG); 400 queue->state = AMDGPU_USERQ_STATE_HUNG; 401 return r; 402 } else { 403 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_MAPPED); 404 queue->state = AMDGPU_USERQ_STATE_MAPPED; 405 } 406 } 407 408 return 0; 409 } 410 411 static void amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue *queue) 412 { 413 struct dma_fence *f = queue->last_fence; 414 415 if (!f) 416 return; 417 418 dma_fence_wait(f, false); 419 } 420 421 static void amdgpu_userq_detach_doorbell(struct amdgpu_usermode_queue *queue) 422 { 423 struct amdgpu_device *adev = queue->userq_mgr->adev; 424 425 down_read(&adev->reset_domain->sem); 426 xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index); 427 up_read(&adev->reset_domain->sem); 428 } 429 430 /** 431 * amdgpu_userq_ensure_ev_fence - ensure a valid, unsignaled eviction fence exists 432 * @uq_mgr: the usermode queue manager for this process 433 * @evf_mgr: the eviction fence manager to check and rearm 434 * 435 * Ensures that a valid and not yet signaled eviction fence is attached to the 436 * usermode queue before any queue operations proceed. If it is signalled, then 437 * rearm a new eviction fence. 438 */ 439 void 440 amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr, 441 struct amdgpu_eviction_fence_mgr *evf_mgr) 442 { 443 struct dma_fence *ev_fence; 444 445 retry: 446 /* Flush any pending resume work to create ev_fence */ 447 flush_delayed_work(&uq_mgr->resume_work); 448 449 mutex_lock(&uq_mgr->userq_mutex); 450 ev_fence = amdgpu_evf_mgr_get_fence(evf_mgr); 451 if (dma_fence_is_signaled(ev_fence)) { 452 dma_fence_put(ev_fence); 453 mutex_unlock(&uq_mgr->userq_mutex); 454 /* 455 * Looks like there was no pending resume work, 456 * add one now to create a valid eviction fence 457 */ 458 schedule_delayed_work(&uq_mgr->resume_work, 0); 459 goto retry; 460 } 461 dma_fence_put(ev_fence); 462 } 463 464 465 466 static int 467 amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, 468 struct amdgpu_db_info *db_info, 469 struct drm_file *filp, 470 u64 *index) 471 { 472 u64 doorbell_index; 473 struct drm_gem_object *gobj; 474 struct amdgpu_userq_obj *db_obj = db_info->db_obj; 475 int r, db_size; 476 477 gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle); 478 if (gobj == NULL) { 479 drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n"); 480 return -EINVAL; 481 } 482 483 db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj)); 484 drm_gem_object_put(gobj); 485 486 r = amdgpu_bo_reserve(db_obj->obj, true); 487 if (r) { 488 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 489 goto unref_bo; 490 } 491 492 /* Pin the BO before generating the index, unpin in queue destroy */ 493 r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL); 494 if (r) { 495 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 496 goto unresv_bo; 497 } 498 499 switch (db_info->queue_type) { 500 case AMDGPU_HW_IP_GFX: 501 case AMDGPU_HW_IP_COMPUTE: 502 case AMDGPU_HW_IP_DMA: 503 db_size = sizeof(u64); 504 break; 505 default: 506 drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n", 507 db_info->queue_type); 508 r = -EINVAL; 509 goto unpin_bo; 510 } 511 512 /* Validate doorbell_offset is within the doorbell BO */ 513 if ((u64)db_info->doorbell_offset * db_size + db_size > 514 amdgpu_bo_size(db_obj->obj)) { 515 r = -EINVAL; 516 goto unpin_bo; 517 } 518 519 doorbell_index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj, 520 db_info->doorbell_offset, db_size); 521 drm_dbg_driver(adev_to_drm(uq_mgr->adev), 522 "[Usermode queues] doorbell index=%lld\n", doorbell_index); 523 amdgpu_bo_unreserve(db_obj->obj); 524 *index = doorbell_index; 525 return 0; 526 527 unpin_bo: 528 amdgpu_bo_unpin(db_obj->obj); 529 unresv_bo: 530 amdgpu_bo_unreserve(db_obj->obj); 531 unref_bo: 532 amdgpu_bo_unref(&db_obj->obj); 533 return r; 534 } 535 536 static int 537 amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) 538 { 539 struct amdgpu_device *adev = uq_mgr->adev; 540 const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; 541 int r = 0; 542 543 trace_amdgpu_userq_destroy_start(queue); 544 545 cancel_delayed_work_sync(&uq_mgr->resume_work); 546 547 mutex_lock(&uq_mgr->userq_mutex); 548 amdgpu_userq_wait_for_last_fence(queue); 549 550 amdgpu_userq_detach_doorbell(queue); 551 cancel_delayed_work_sync(&queue->hang_detect_work); 552 553 #if defined(CONFIG_DEBUG_FS) 554 debugfs_remove_recursive(queue->debugfs_queue); 555 #endif 556 r = amdgpu_userq_unmap_helper(queue); 557 atomic_dec(&uq_mgr->userq_count[queue->queue_type]); 558 amdgpu_userq_fence_driver_free(queue); 559 queue->fence_drv = NULL; 560 mutex_unlock(&uq_mgr->userq_mutex); 561 562 /* 563 * A failed unmap means MES could not remove the hung queue and is now 564 * unresponsive. Recover the GPU here so the wedged MES does not fail 565 * the next, unrelated queue submission and trigger a reset attributed 566 * to an innocent workload. 567 */ 568 if (r) 569 queue_work(adev->reset_domain->wq, &uq_mgr->reset_work); 570 571 uq_funcs->mqd_destroy(queue); 572 queue->userq_mgr = NULL; 573 574 amdgpu_bo_reserve(queue->db_obj.obj, true); 575 amdgpu_bo_unpin(queue->db_obj.obj); 576 amdgpu_bo_unreserve(queue->db_obj.obj); 577 amdgpu_bo_unref(&queue->db_obj.obj); 578 579 trace_amdgpu_userq_destroy_end(queue, r); 580 kfree(queue); 581 582 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 583 584 return r; 585 } 586 587 static void amdgpu_userq_kref_destroy(struct kref *kref) 588 { 589 int r; 590 struct amdgpu_usermode_queue *queue = 591 container_of(kref, struct amdgpu_usermode_queue, refcount); 592 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 593 594 r = amdgpu_userq_destroy(uq_mgr, queue); 595 if (r) 596 drm_file_err(uq_mgr->file, "Failed to destroy usermode queue %d\n", r); 597 } 598 599 struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr, u32 qid) 600 { 601 struct amdgpu_usermode_queue *queue; 602 603 xa_lock(&uq_mgr->userq_xa); 604 queue = xa_load(&uq_mgr->userq_xa, qid); 605 if (queue) 606 kref_get(&queue->refcount); 607 xa_unlock(&uq_mgr->userq_xa); 608 609 return queue; 610 } 611 612 void amdgpu_userq_put(struct amdgpu_usermode_queue *queue) 613 { 614 if (queue) 615 kref_put(&queue->refcount, amdgpu_userq_kref_destroy); 616 } 617 618 static int amdgpu_userq_priority_permit(struct drm_file *filp, 619 int priority) 620 { 621 if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH) 622 return 0; 623 624 if (capable(CAP_SYS_NICE)) 625 return 0; 626 627 if (drm_is_current_master(filp)) 628 return 0; 629 630 return -EACCES; 631 } 632 633 static int 634 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) 635 { 636 struct amdgpu_fpriv *fpriv = filp->driver_priv; 637 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 638 struct amdgpu_device *adev = uq_mgr->adev; 639 const struct amdgpu_userq_funcs *uq_funcs; 640 struct amdgpu_usermode_queue *queue; 641 struct amdgpu_db_info db_info; 642 uint64_t index; 643 int priority; 644 u32 qid; 645 int r; 646 647 priority = 648 (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) 649 >> AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT; 650 r = amdgpu_userq_priority_permit(filp, priority); 651 if (r) 652 return r; 653 654 r = pm_runtime_resume_and_get(adev_to_drm(adev)->dev); 655 if (r < 0) { 656 drm_file_err(uq_mgr->file, "pm_runtime_resume_and_get() failed for userqueue create\n"); 657 return r; 658 } 659 660 uq_funcs = adev->userq_funcs[args->in.ip_type]; 661 if (!uq_funcs) { 662 r = -EINVAL; 663 goto err_pm_runtime; 664 } 665 666 queue = kzalloc_obj(struct amdgpu_usermode_queue); 667 if (!queue) { 668 r = -ENOMEM; 669 goto err_pm_runtime; 670 } 671 672 kref_init(&queue->refcount); 673 queue->doorbell_handle = args->in.doorbell_handle; 674 queue->queue_type = args->in.ip_type; 675 queue->vm = &fpriv->vm; 676 queue->priority = priority; 677 queue->xcp_id = (fpriv->xcp_id != AMDGPU_XCP_NO_PARTITION) ? 678 fpriv->xcp_id : 0; 679 queue->userq_mgr = uq_mgr; 680 INIT_DELAYED_WORK(&queue->hang_detect_work, 681 amdgpu_userq_hang_detect_work); 682 683 r = amdgpu_userq_fence_driver_alloc(adev, &queue->fence_drv); 684 if (r) 685 goto free_queue; 686 687 xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC); 688 mutex_init(&queue->fence_drv_lock); 689 /* Make sure the queue can actually run with those virtual addresses. */ 690 r = amdgpu_bo_reserve(fpriv->vm.root.bo, false); 691 if (r) 692 goto free_fence_drv; 693 694 if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va, 695 args->in.queue_size, 696 &queue->userq_vas.va.queue_rb) || 697 amdgpu_userq_input_va_validate(adev, queue, args->in.rptr_va, 698 sizeof(u64), 699 &queue->userq_vas.va.rptr) || 700 amdgpu_userq_input_va_validate(adev, queue, args->in.wptr_va, 701 sizeof(u64), 702 &queue->userq_vas.va.wptr)) { 703 r = -EINVAL; 704 amdgpu_bo_unreserve(fpriv->vm.root.bo); 705 goto free_fence_drv; 706 } 707 amdgpu_bo_unreserve(fpriv->vm.root.bo); 708 709 /* Convert relative doorbell offset into absolute doorbell index */ 710 db_info.queue_type = queue->queue_type; 711 db_info.doorbell_handle = queue->doorbell_handle; 712 db_info.db_obj = &queue->db_obj; 713 db_info.doorbell_offset = args->in.doorbell_offset; 714 r = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp, &index); 715 if (r) { 716 drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n"); 717 goto free_fence_drv; 718 } 719 720 queue->doorbell_index = index; 721 queue->doorbell_offset = (u32)args->in.doorbell_offset; 722 trace_amdgpu_userq_create_start(queue); 723 r = uq_funcs->mqd_create(queue, &args->in); 724 if (r) { 725 drm_file_err(uq_mgr->file, "Failed to create Queue\n"); 726 goto clean_doorbell_bo; 727 } 728 729 /* Update VM owner at userq submit-time for page-fault attribution. */ 730 amdgpu_vm_set_task_info(&fpriv->vm); 731 732 r = xa_insert_irq(&adev->userq_doorbell_xa, index, queue, 733 GFP_KERNEL); 734 if (r) 735 goto clean_mqd; 736 737 amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr); 738 739 /* don't map the queue if scheduling is halted */ 740 if (!adev->userq_halt_for_enforce_isolation || 741 ((queue->queue_type != AMDGPU_HW_IP_GFX) && 742 (queue->queue_type != AMDGPU_HW_IP_COMPUTE))) { 743 /* Serialize the map against an in-progress GPU reset (MES is 744 * unresponsive during recovery), matching amdgpu_userq_detach_doorbell(). 745 */ 746 down_read(&adev->reset_domain->sem); 747 r = amdgpu_userq_map_helper(queue); 748 up_read(&adev->reset_domain->sem); 749 if (r) { 750 drm_file_err(uq_mgr->file, "Failed to map Queue\n"); 751 trace_amdgpu_userq_create_end(queue, r); 752 mutex_unlock(&uq_mgr->userq_mutex); 753 goto erase_doorbell; 754 } 755 } 756 757 atomic_inc(&uq_mgr->userq_count[queue->queue_type]); 758 mutex_unlock(&uq_mgr->userq_mutex); 759 760 r = xa_alloc(&uq_mgr->userq_xa, &qid, queue, 761 XA_LIMIT(1, AMDGPU_MAX_USERQ_COUNT), 762 GFP_KERNEL); 763 if (r) { 764 /* 765 * This drops the last reference which should take care of 766 * all cleanup. 767 */ 768 trace_amdgpu_userq_create_end(queue, r); 769 amdgpu_userq_put(queue); 770 return r; 771 } 772 773 amdgpu_debugfs_userq_init(filp, queue, qid); 774 trace_amdgpu_userq_create_end(queue, 0); 775 args->out.queue_id = qid; 776 return 0; 777 778 erase_doorbell: 779 xa_erase_irq(&adev->userq_doorbell_xa, index); 780 clean_mqd: 781 uq_funcs->mqd_destroy(queue); 782 clean_doorbell_bo: 783 amdgpu_bo_reserve(queue->db_obj.obj, true); 784 amdgpu_bo_unpin(queue->db_obj.obj); 785 amdgpu_bo_unreserve(queue->db_obj.obj); 786 amdgpu_bo_unref(&queue->db_obj.obj); 787 free_fence_drv: 788 amdgpu_userq_fence_driver_free(queue); 789 free_queue: 790 trace_amdgpu_userq_create_end(queue, r); 791 kfree(queue); 792 err_pm_runtime: 793 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 794 return r; 795 } 796 797 static int amdgpu_userq_input_args_validate(struct drm_device *dev, 798 union drm_amdgpu_userq *args, 799 struct drm_file *filp) 800 { 801 struct amdgpu_device *adev = drm_to_adev(dev); 802 803 switch (args->in.op) { 804 case AMDGPU_USERQ_OP_CREATE: 805 if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK | 806 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE)) 807 return -EINVAL; 808 /* Usermode queues are only supported for GFX IP as of now */ 809 if (args->in.ip_type != AMDGPU_HW_IP_GFX && 810 args->in.ip_type != AMDGPU_HW_IP_DMA && 811 args->in.ip_type != AMDGPU_HW_IP_COMPUTE) { 812 drm_file_err(filp, "Usermode queue doesn't support IP type %u\n", 813 args->in.ip_type); 814 return -EINVAL; 815 } 816 817 if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) && 818 (args->in.ip_type != AMDGPU_HW_IP_GFX) && 819 (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) && 820 !amdgpu_is_tmz(adev)) { 821 drm_file_err(filp, "Secure only supported on GFX/Compute queues\n"); 822 return -EINVAL; 823 } 824 825 if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET || 826 args->in.queue_va == 0 || 827 args->in.queue_size == 0) { 828 drm_file_err(filp, "invalidate userq queue va or size\n"); 829 return -EINVAL; 830 } 831 832 if (!is_power_of_2(args->in.queue_size)) { 833 drm_file_err(filp, "Queue size must be a power of 2\n"); 834 return -EINVAL; 835 } 836 837 if (args->in.queue_size < AMDGPU_GPU_PAGE_SIZE) { 838 drm_file_err(filp, "Queue size smaller than AMDGPU_GPU_PAGE_SIZE\n"); 839 return -EINVAL; 840 } 841 842 if (!args->in.wptr_va || !args->in.rptr_va) { 843 drm_file_err(filp, "invalidate userq queue rptr or wptr\n"); 844 return -EINVAL; 845 } 846 847 if (!IS_ALIGNED(args->in.wptr_va, sizeof(u64)) || 848 !IS_ALIGNED(args->in.rptr_va, sizeof(u64))) { 849 drm_file_err(filp, "user queue rptr or wptr is not 8-byte aligned\n"); 850 return -EINVAL; 851 } 852 break; 853 case AMDGPU_USERQ_OP_FREE: 854 if (args->in.ip_type || 855 args->in.doorbell_handle || 856 args->in.doorbell_offset || 857 args->in.flags || 858 args->in.queue_va || 859 args->in.queue_size || 860 args->in.rptr_va || 861 args->in.wptr_va || 862 args->in.mqd || 863 args->in.mqd_size) 864 return -EINVAL; 865 break; 866 default: 867 return -EINVAL; 868 } 869 870 return 0; 871 } 872 873 bool amdgpu_userq_enabled(struct drm_device *dev) 874 { 875 struct amdgpu_device *adev = drm_to_adev(dev); 876 int i; 877 878 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) { 879 if (adev->userq_funcs[i]) 880 return true; 881 } 882 883 return false; 884 } 885 886 int amdgpu_userq_ioctl(struct drm_device *dev, void *data, 887 struct drm_file *filp) 888 { 889 union drm_amdgpu_userq *args = data; 890 struct amdgpu_fpriv *fpriv = filp->driver_priv; 891 struct amdgpu_usermode_queue *queue; 892 int r = 0; 893 894 if (!amdgpu_userq_enabled(dev)) 895 return -ENOTSUPP; 896 897 if (amdgpu_userq_input_args_validate(dev, args, filp) < 0) 898 return -EINVAL; 899 900 switch (args->in.op) { 901 case AMDGPU_USERQ_OP_CREATE: 902 r = amdgpu_userq_create(filp, args); 903 if (r) 904 drm_file_err(filp, "Failed to create usermode queue\n"); 905 break; 906 907 case AMDGPU_USERQ_OP_FREE: { 908 xa_lock(&fpriv->userq_mgr.userq_xa); 909 queue = __xa_erase(&fpriv->userq_mgr.userq_xa, args->in.queue_id); 910 xa_unlock(&fpriv->userq_mgr.userq_xa); 911 if (!queue) 912 return -ENOENT; 913 914 amdgpu_userq_put(queue); 915 break; 916 } 917 918 default: 919 drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op); 920 return -EINVAL; 921 } 922 923 return r; 924 } 925 926 static int 927 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) 928 { 929 struct amdgpu_usermode_queue *queue; 930 unsigned long queue_id; 931 int ret = 0, r; 932 933 mutex_lock(&uq_mgr->userq_mutex); 934 /* Resume all the queues for this process */ 935 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 936 937 if (!amdgpu_userq_buffer_vas_mapped(queue)) { 938 drm_file_err(uq_mgr->file, 939 "trying restore queue without va mapping\n"); 940 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_INVALID_VA); 941 queue->state = AMDGPU_USERQ_STATE_INVALID_VA; 942 continue; 943 } 944 945 r = amdgpu_userq_map_helper(queue); 946 if (r) 947 ret = r; 948 } 949 mutex_unlock(&uq_mgr->userq_mutex); 950 951 if (ret) 952 drm_file_err(uq_mgr->file, 953 "Failed to map all the queues, restore failed ret=%d\n", ret); 954 return ret; 955 } 956 957 static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo) 958 { 959 struct ttm_operation_ctx ctx = { false, false }; 960 961 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 962 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 963 } 964 965 /* Handle all BOs on the invalidated list, validate them and update the PTs */ 966 static int 967 amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec, 968 struct amdgpu_vm *vm) 969 { 970 struct ttm_operation_ctx ctx = { false, false }; 971 struct amdgpu_bo_va *bo_va; 972 struct amdgpu_bo *bo; 973 int ret; 974 975 spin_lock(&vm->individual_lock); 976 while (!list_empty(&vm->always_valid.evicted)) { 977 bo_va = list_first_entry(&vm->always_valid.evicted, 978 struct amdgpu_bo_va, 979 base.vm_status); 980 spin_unlock(&vm->individual_lock); 981 982 bo = bo_va->base.bo; 983 ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 984 TTM_NUM_MOVE_FENCES + 1); 985 if (unlikely(ret)) 986 return ret; 987 988 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 989 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 990 if (ret) 991 return ret; 992 993 /* This moves the bo_va to the idle list */ 994 ret = amdgpu_vm_bo_update(adev, bo_va, false); 995 if (ret) 996 return ret; 997 998 spin_lock(&vm->individual_lock); 999 } 1000 spin_unlock(&vm->individual_lock); 1001 1002 return 0; 1003 } 1004 1005 /* Make sure the whole VM is ready to be used */ 1006 static int 1007 amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr) 1008 { 1009 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 1010 bool invalidated = false, new_addition = false; 1011 struct ttm_operation_ctx ctx = { true, false }; 1012 struct amdgpu_device *adev = uq_mgr->adev; 1013 struct amdgpu_hmm_range *range; 1014 struct amdgpu_vm *vm = &fpriv->vm; 1015 unsigned long key, tmp_key; 1016 struct amdgpu_bo_va *bo_va; 1017 struct amdgpu_usermode_queue *queue; 1018 struct amdgpu_bo *bo; 1019 struct drm_exec exec; 1020 struct xarray xa; 1021 int ret; 1022 1023 xa_init(&xa); 1024 1025 retry_lock: 1026 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 1027 drm_exec_until_all_locked(&exec) { 1028 ret = amdgpu_vm_lock_pd(vm, &exec, 1); 1029 drm_exec_retry_on_contention(&exec); 1030 if (unlikely(ret)) 1031 goto unlock_all; 1032 1033 ret = amdgpu_vm_lock_individual(vm, &exec, TTM_NUM_MOVE_FENCES + 1); 1034 drm_exec_retry_on_contention(&exec); 1035 if (unlikely(ret)) 1036 goto unlock_all; 1037 1038 /* This validates PDs, PTs and per VM BOs */ 1039 ret = amdgpu_vm_validate(adev, vm, NULL, 1040 amdgpu_userq_validate_vm, 1041 NULL); 1042 if (unlikely(ret)) 1043 goto unlock_all; 1044 1045 /* This locks and validates the remaining evicted BOs */ 1046 ret = amdgpu_userq_bo_validate(adev, &exec, vm); 1047 drm_exec_retry_on_contention(&exec); 1048 if (unlikely(ret)) 1049 goto unlock_all; 1050 1051 /* 1052 * WPTR BOs are VM-mapped, but each BO has its own reservation 1053 * object. Lock them into this drm_exec ww context so the later 1054 * amdgpu_bo_gpu_offset() reads are done with the BO resv locked. 1055 */ 1056 xa_for_each(&uq_mgr->userq_xa, tmp_key, queue) { 1057 struct ttm_operation_ctx wptr_ctx = { false, false }; 1058 1059 bo = queue->wptr_obj.obj; 1060 if (!bo) 1061 continue; 1062 1063 ret = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1064 TTM_NUM_MOVE_FENCES + 1); 1065 drm_exec_retry_on_contention(&exec); 1066 if (unlikely(ret)) 1067 goto unlock_all; 1068 1069 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 1070 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &wptr_ctx); 1071 if (unlikely(ret)) 1072 goto unlock_all; 1073 } 1074 } 1075 1076 if (invalidated) { 1077 xa_for_each(&xa, tmp_key, range) { 1078 bo = range->bo; 1079 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU); 1080 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1081 if (ret) 1082 goto unlock_all; 1083 1084 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range); 1085 1086 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); 1087 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1088 if (ret) 1089 goto unlock_all; 1090 } 1091 invalidated = false; 1092 } 1093 1094 ret = amdgpu_vm_handle_moved(adev, vm, NULL); 1095 if (ret) 1096 goto unlock_all; 1097 1098 /* 1099 * PRT/sparse mappings are kept off the vm_bo state lists, so 1100 * amdgpu_vm_handle_moved() does not touch them. Refresh their PTEs 1101 * explicitly here (as the CS path does) so sparse mappings survive a 1102 * VRAM-lost reset. 1103 */ 1104 ret = amdgpu_vm_bo_update(adev, fpriv->prt_va, false); 1105 if (ret) 1106 goto unlock_all; 1107 1108 key = 0; 1109 /* Validate User Ptr BOs */ 1110 list_for_each_entry(bo_va, &vm->always_valid.idle, base.vm_status) { 1111 bo = bo_va->base.bo; 1112 if (!bo) 1113 continue; 1114 1115 if (!amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) 1116 continue; 1117 1118 range = xa_load(&xa, key); 1119 if (range && range->bo != bo) { 1120 xa_erase(&xa, key); 1121 amdgpu_hmm_range_free(range); 1122 range = NULL; 1123 } 1124 1125 if (!range) { 1126 range = amdgpu_hmm_range_alloc(bo); 1127 if (!range) { 1128 ret = -ENOMEM; 1129 goto unlock_all; 1130 } 1131 1132 xa_store(&xa, key, range, GFP_KERNEL); 1133 new_addition = true; 1134 } 1135 key++; 1136 } 1137 1138 if (new_addition) { 1139 drm_exec_fini(&exec); 1140 xa_for_each(&xa, tmp_key, range) { 1141 if (!range) 1142 continue; 1143 bo = range->bo; 1144 ret = amdgpu_ttm_tt_get_user_pages(bo, range); 1145 if (ret) 1146 goto free_ranges; 1147 } 1148 1149 invalidated = true; 1150 new_addition = false; 1151 goto retry_lock; 1152 } 1153 1154 ret = amdgpu_vm_update_pdes(adev, vm, false); 1155 if (ret) 1156 goto unlock_all; 1157 1158 /* 1159 * We need to wait for all VM updates to finish before restarting the 1160 * queues. Using the idle list like that is now ok since everything is 1161 * locked in place. 1162 */ 1163 list_for_each_entry(bo_va, &vm->always_valid.idle, base.vm_status) 1164 dma_fence_wait(bo_va->last_pt_update, false); 1165 /* 1166 * The PRT bo_va is kept off the state lists, so its PTE update fence 1167 * lands in prt_va->last_pt_update rather than vm->last_update; wait on 1168 * it explicitly (as the CS path syncs it) before restarting queues. 1169 */ 1170 dma_fence_wait(fpriv->prt_va->last_pt_update, false); 1171 dma_fence_wait(vm->last_update, false); 1172 1173 xa_for_each(&uq_mgr->userq_xa, tmp_key, queue) { 1174 bo = queue->wptr_obj.obj; 1175 if (!bo) { 1176 ret = -EINVAL; 1177 goto unlock_all; 1178 } 1179 1180 ret = amdgpu_ttm_alloc_gart(&bo->tbo); 1181 if (unlikely(ret)) { 1182 drm_file_err(uq_mgr->file, 1183 "failed to bind wptr bo to gart on resume, qid=%lu ret=%d\n", 1184 tmp_key, ret); 1185 goto unlock_all; 1186 } 1187 1188 queue->wptr_obj.gpu_addr = amdgpu_bo_gpu_offset(bo); 1189 } 1190 1191 ret = amdgpu_evf_mgr_rearm(&fpriv->evf_mgr, &exec); 1192 if (ret) { 1193 drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n"); 1194 goto unlock_all; 1195 } 1196 1197 ret = amdgpu_userq_restore_all(uq_mgr); 1198 1199 unlock_all: 1200 drm_exec_fini(&exec); 1201 free_ranges: 1202 xa_for_each(&xa, tmp_key, range) { 1203 if (!range) 1204 continue; 1205 bo = range->bo; 1206 amdgpu_hmm_range_free(range); 1207 } 1208 xa_destroy(&xa); 1209 return ret; 1210 } 1211 1212 static void amdgpu_userq_restore_worker(struct work_struct *work) 1213 { 1214 struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work); 1215 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 1216 struct dma_fence *ev_fence; 1217 int ret; 1218 1219 ev_fence = amdgpu_evf_mgr_get_fence(&fpriv->evf_mgr); 1220 if (!dma_fence_is_signaled(ev_fence)) 1221 goto put_fence; 1222 1223 ret = amdgpu_userq_vm_validate_and_restore_queue(uq_mgr); 1224 if (ret) { 1225 drm_file_err(uq_mgr->file, "Failed to validate BOs to restore ret=%d\n", ret); 1226 goto put_fence; 1227 } 1228 1229 put_fence: 1230 dma_fence_put(ev_fence); 1231 } 1232 1233 void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev, 1234 u32 pasid, u32 doorbell_offset) 1235 { 1236 struct xarray *xa = &adev->userq_doorbell_xa; 1237 struct amdgpu_usermode_queue *queue; 1238 unsigned long flags, idx; 1239 1240 xa_lock_irqsave(xa, flags); 1241 xa_for_each(xa, idx, queue) { 1242 if (queue->vm && queue->vm->pasid == pasid && 1243 queue->doorbell_offset == doorbell_offset) { 1244 amdgpu_userq_start_hang_detect_work(queue); 1245 break; 1246 } 1247 } 1248 xa_unlock_irqrestore(xa, flags); 1249 } 1250 1251 static int 1252 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) 1253 { 1254 struct amdgpu_usermode_queue *queue; 1255 unsigned long queue_id; 1256 int ret = 0, r; 1257 1258 /* Try to unmap all the queues in this process ctx */ 1259 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 1260 r = amdgpu_userq_unmap_helper(queue); 1261 if (r) 1262 ret = r; 1263 } 1264 1265 if (ret) { 1266 drm_file_err(uq_mgr->file, 1267 "Couldn't unmap all the queues, eviction failed ret=%d\n", ret); 1268 amdgpu_reset_domain_schedule(uq_mgr->adev->reset_domain, 1269 &uq_mgr->reset_work); 1270 flush_work(&uq_mgr->reset_work); 1271 } 1272 return ret; 1273 } 1274 1275 static void 1276 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) 1277 { 1278 struct amdgpu_usermode_queue *queue; 1279 unsigned long queue_id; 1280 1281 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 1282 struct dma_fence *f = queue->last_fence; 1283 1284 if (!f) 1285 continue; 1286 1287 dma_fence_wait(f, false); 1288 } 1289 } 1290 1291 void 1292 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr) 1293 { 1294 /* Wait for any pending userqueue fence work to finish */ 1295 amdgpu_userq_wait_for_signal(uq_mgr); 1296 amdgpu_userq_evict_all(uq_mgr); 1297 } 1298 1299 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv, 1300 struct amdgpu_device *adev) 1301 { 1302 mutex_init(&userq_mgr->userq_mutex); 1303 xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC); 1304 userq_mgr->adev = adev; 1305 userq_mgr->file = file_priv; 1306 userq_mgr->proc_ctx_allocated = false; 1307 mutex_init(&userq_mgr->proc_ctx_lock); 1308 1309 INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker); 1310 INIT_WORK(&userq_mgr->reset_work, amdgpu_userq_mgr_reset_work); 1311 return 0; 1312 } 1313 1314 void amdgpu_userq_mgr_cancel_reset_work(struct amdgpu_device *adev) 1315 { 1316 struct xarray *xa = &adev->userq_doorbell_xa; 1317 struct amdgpu_usermode_queue *queue; 1318 unsigned long flags, queue_id; 1319 1320 xa_lock_irqsave(xa, flags); 1321 xa_for_each(xa, queue_id, queue) { 1322 cancel_delayed_work(&queue->hang_detect_work); 1323 cancel_work(&queue->userq_mgr->reset_work); 1324 } 1325 xa_unlock_irqrestore(xa, flags); 1326 } 1327 1328 void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr) 1329 { 1330 cancel_delayed_work_sync(&userq_mgr->resume_work); 1331 } 1332 1333 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr) 1334 { 1335 struct amdgpu_mes *mes = &userq_mgr->adev->mes; 1336 struct amdgpu_usermode_queue *queue; 1337 unsigned long queue_id = 0; 1338 1339 for (;;) { 1340 xa_lock(&userq_mgr->userq_xa); 1341 queue = xa_find(&userq_mgr->userq_xa, &queue_id, ULONG_MAX, 1342 XA_PRESENT); 1343 if (queue) 1344 __xa_erase(&userq_mgr->userq_xa, queue_id); 1345 xa_unlock(&userq_mgr->userq_xa); 1346 1347 if (!queue) 1348 break; 1349 1350 amdgpu_userq_put(queue); 1351 } 1352 1353 xa_destroy(&userq_mgr->userq_xa); 1354 1355 /* 1356 * Drain any in-flight reset_work. By this point all queues are freed 1357 * and userq_count is 0, so if reset_work starts now it exits early. 1358 * We still need to wait in case it was already executing gpu_recover. 1359 */ 1360 cancel_work_sync(&userq_mgr->reset_work); 1361 1362 if (userq_mgr->proc_ctx_allocated) { 1363 amdgpu_mes_free_proc_ctx_index(mes, userq_mgr->proc_ctx_array_index); 1364 userq_mgr->proc_ctx_allocated = false; 1365 } 1366 amdgpu_bo_free_kernel(&userq_mgr->proc_ctx_obj.obj, 1367 &userq_mgr->proc_ctx_obj.gpu_addr, 1368 &userq_mgr->proc_ctx_obj.cpu_ptr); 1369 1370 mutex_destroy(&userq_mgr->proc_ctx_lock); 1371 mutex_destroy(&userq_mgr->userq_mutex); 1372 } 1373 1374 int amdgpu_userq_suspend(struct amdgpu_device *adev) 1375 { 1376 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1377 struct amdgpu_usermode_queue *queue; 1378 struct amdgpu_userq_mgr *uqm; 1379 unsigned long queue_id; 1380 int r; 1381 1382 if (!ip_mask) 1383 return 0; 1384 1385 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1386 uqm = queue->userq_mgr; 1387 cancel_delayed_work_sync(&uqm->resume_work); 1388 guard(mutex)(&uqm->userq_mutex); 1389 if (adev->in_s0ix) 1390 r = amdgpu_userq_preempt_helper(queue); 1391 else 1392 r = amdgpu_userq_unmap_helper(queue); 1393 if (r) 1394 return r; 1395 } 1396 return 0; 1397 } 1398 1399 int amdgpu_userq_resume(struct amdgpu_device *adev) 1400 { 1401 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1402 struct amdgpu_usermode_queue *queue; 1403 struct amdgpu_userq_mgr *uqm; 1404 unsigned long queue_id; 1405 int r; 1406 1407 if (!ip_mask) 1408 return 0; 1409 1410 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1411 uqm = queue->userq_mgr; 1412 guard(mutex)(&uqm->userq_mutex); 1413 if (adev->in_s0ix) 1414 r = amdgpu_userq_restore_helper(queue); 1415 else 1416 r = amdgpu_userq_map_helper(queue); 1417 if (r) 1418 return r; 1419 } 1420 1421 return 0; 1422 } 1423 1424 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, 1425 u32 idx) 1426 { 1427 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1428 struct amdgpu_usermode_queue *queue; 1429 struct amdgpu_userq_mgr *uqm; 1430 unsigned long queue_id; 1431 int ret = 0, r; 1432 1433 /* only need to stop gfx/compute */ 1434 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 1435 return 0; 1436 1437 if (adev->userq_halt_for_enforce_isolation) 1438 dev_warn(adev->dev, "userq scheduling already stopped!\n"); 1439 adev->userq_halt_for_enforce_isolation = true; 1440 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1441 uqm = queue->userq_mgr; 1442 cancel_delayed_work_sync(&uqm->resume_work); 1443 mutex_lock(&uqm->userq_mutex); 1444 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 1445 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 1446 (queue->xcp_id == idx)) { 1447 r = amdgpu_userq_preempt_helper(queue); 1448 if (r) 1449 ret = r; 1450 } 1451 mutex_unlock(&uqm->userq_mutex); 1452 } 1453 1454 return ret; 1455 } 1456 1457 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, 1458 u32 idx) 1459 { 1460 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1461 struct amdgpu_usermode_queue *queue; 1462 struct amdgpu_userq_mgr *uqm; 1463 unsigned long queue_id; 1464 int ret = 0, r; 1465 1466 /* only need to stop gfx/compute */ 1467 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 1468 return 0; 1469 1470 if (!adev->userq_halt_for_enforce_isolation) 1471 dev_warn(adev->dev, "userq scheduling already started!\n"); 1472 1473 adev->userq_halt_for_enforce_isolation = false; 1474 1475 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1476 uqm = queue->userq_mgr; 1477 mutex_lock(&uqm->userq_mutex); 1478 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 1479 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 1480 (queue->xcp_id == idx)) { 1481 r = amdgpu_userq_restore_helper(queue); 1482 if (r) 1483 ret = r; 1484 } 1485 mutex_unlock(&uqm->userq_mutex); 1486 } 1487 1488 return ret; 1489 } 1490 1491 void amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev, 1492 struct amdgpu_bo_va_mapping *mapping) 1493 { 1494 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1495 struct amdgpu_bo_va *bo_va = mapping->bo_va; 1496 struct dma_resv *resv = bo_va->base.bo->tbo.base.resv; 1497 1498 if (!ip_mask) 1499 return; 1500 1501 /** 1502 * The userq VA mapping reservation should include the eviction fence. 1503 * Note: The eviction fence may be attached to different BOs and this 1504 * unmap is only for one kind of userq VAs, so at this point suppose 1505 * the eviction fence is always unsignaled. 1506 */ 1507 dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP, 1508 false, MAX_SCHEDULE_TIMEOUT); 1509 } 1510 1511 void amdgpu_userq_pre_reset(struct amdgpu_device *adev) 1512 { 1513 const struct amdgpu_userq_funcs *userq_funcs; 1514 struct amdgpu_usermode_queue *queue; 1515 unsigned long queue_id; 1516 1517 /* TODO: We probably need a new lock for the queue state */ 1518 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1519 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) { 1520 trace_amdgpu_userq_state_start(queue); 1521 userq_funcs = adev->userq_funcs[queue->queue_type]; 1522 userq_funcs->unmap(queue); 1523 /* just mark all queues as hung at this point. 1524 * if unmap succeeds, we could map again 1525 * in amdgpu_userq_post_reset() if vram is not lost 1526 */ 1527 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG); 1528 queue->state = AMDGPU_USERQ_STATE_HUNG; 1529 } 1530 /* Force-complete any pending fence regardless of queue state so 1531 * that eviction/suspend and queue teardown waiters don't block 1532 * forever on a fence that will never signal after the reset. 1533 */ 1534 amdgpu_userq_fence_driver_force_completion(queue); 1535 } 1536 } 1537 1538 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost) 1539 { 1540 /* if any queue state is AMDGPU_USERQ_STATE_UNMAPPED 1541 * at this point, we should be able to map it again 1542 * and continue if vram is not lost. 1543 */ 1544 struct amdgpu_usermode_queue *queue; 1545 const struct amdgpu_userq_funcs *userq_funcs; 1546 unsigned long queue_id; 1547 int r = 0; 1548 1549 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1550 if (queue->state == AMDGPU_USERQ_STATE_HUNG && !vram_lost) { 1551 trace_amdgpu_userq_state_start(queue); 1552 1553 userq_funcs = adev->userq_funcs[queue->queue_type]; 1554 /* Re-map queue */ 1555 r = userq_funcs->map(queue); 1556 if (r) { 1557 dev_err(adev->dev, "Failed to remap queue %ld\n", queue_id); 1558 continue; 1559 } 1560 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_MAPPED); 1561 queue->state = AMDGPU_USERQ_STATE_MAPPED; 1562 } 1563 } 1564 1565 return r; 1566 } 1567