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