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_jiffies; 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_jiffies = adev->gfx_timeout; 194 break; 195 case AMDGPU_RING_TYPE_COMPUTE: 196 timeout_jiffies = adev->compute_timeout; 197 break; 198 case AMDGPU_RING_TYPE_SDMA: 199 timeout_jiffies = adev->sdma_timeout; 200 break; 201 default: 202 timeout_jiffies = adev->gfx_timeout; 203 break; 204 } 205 206 queue_delayed_work(adev->reset_domain->wq, &queue->hang_detect_work, 207 timeout_jiffies); 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 struct amdgpu_bo *abo; 476 int r, db_size; 477 478 gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle); 479 if (gobj == NULL) { 480 drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n"); 481 return -EINVAL; 482 } 483 484 /* 485 * Pinning a regular BO into the doorbell domain would discard its 486 * contents, possibly those of a buffer shared by another client. 487 */ 488 abo = gem_to_amdgpu_bo(gobj); 489 if (!(abo->preferred_domains & AMDGPU_GEM_DOMAIN_DOORBELL)) { 490 drm_gem_object_put(gobj); 491 return -EINVAL; 492 } 493 494 db_obj->obj = amdgpu_bo_ref(abo); 495 drm_gem_object_put(gobj); 496 497 r = amdgpu_bo_reserve(db_obj->obj, true); 498 if (r) { 499 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 500 goto unref_bo; 501 } 502 503 /* Pin the BO before generating the index, unpin in queue destroy */ 504 r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL); 505 if (r) { 506 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 507 goto unresv_bo; 508 } 509 510 switch (db_info->queue_type) { 511 case AMDGPU_HW_IP_GFX: 512 case AMDGPU_HW_IP_COMPUTE: 513 case AMDGPU_HW_IP_DMA: 514 db_size = sizeof(u64); 515 break; 516 default: 517 drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n", 518 db_info->queue_type); 519 r = -EINVAL; 520 goto unpin_bo; 521 } 522 523 /* Validate doorbell_offset is within the doorbell BO */ 524 if ((u64)db_info->doorbell_offset * db_size + db_size > 525 amdgpu_bo_size(db_obj->obj)) { 526 r = -EINVAL; 527 goto unpin_bo; 528 } 529 530 doorbell_index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj, 531 db_info->doorbell_offset, db_size); 532 drm_dbg_driver(adev_to_drm(uq_mgr->adev), 533 "[Usermode queues] doorbell index=%lld\n", doorbell_index); 534 amdgpu_bo_unreserve(db_obj->obj); 535 *index = doorbell_index; 536 return 0; 537 538 unpin_bo: 539 amdgpu_bo_unpin(db_obj->obj); 540 unresv_bo: 541 amdgpu_bo_unreserve(db_obj->obj); 542 unref_bo: 543 amdgpu_bo_unref(&db_obj->obj); 544 return r; 545 } 546 547 static int 548 amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) 549 { 550 struct amdgpu_device *adev = uq_mgr->adev; 551 const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; 552 int r = 0; 553 554 trace_amdgpu_userq_destroy_start(queue); 555 556 cancel_delayed_work_sync(&uq_mgr->resume_work); 557 558 mutex_lock(&uq_mgr->userq_mutex); 559 amdgpu_userq_wait_for_last_fence(queue); 560 561 amdgpu_userq_detach_doorbell(queue); 562 cancel_delayed_work_sync(&queue->hang_detect_work); 563 564 #if defined(CONFIG_DEBUG_FS) 565 debugfs_remove_recursive(queue->debugfs_queue); 566 #endif 567 r = amdgpu_userq_unmap_helper(queue); 568 atomic_dec(&uq_mgr->userq_count[queue->queue_type]); 569 amdgpu_userq_fence_driver_free(queue); 570 queue->fence_drv = NULL; 571 mutex_unlock(&uq_mgr->userq_mutex); 572 573 /* 574 * A failed unmap means MES could not remove the hung queue and is now 575 * unresponsive. Recover the GPU here so the wedged MES does not fail 576 * the next, unrelated queue submission and trigger a reset attributed 577 * to an innocent workload. 578 */ 579 if (r) 580 queue_work(adev->reset_domain->wq, &uq_mgr->reset_work); 581 582 uq_funcs->mqd_destroy(queue); 583 queue->userq_mgr = NULL; 584 585 amdgpu_bo_reserve(queue->db_obj.obj, true); 586 amdgpu_bo_unpin(queue->db_obj.obj); 587 amdgpu_bo_unreserve(queue->db_obj.obj); 588 amdgpu_bo_unref(&queue->db_obj.obj); 589 590 trace_amdgpu_userq_destroy_end(queue, r); 591 kfree(queue); 592 593 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 594 595 return r; 596 } 597 598 static void amdgpu_userq_kref_destroy(struct kref *kref) 599 { 600 int r; 601 struct amdgpu_usermode_queue *queue = 602 container_of(kref, struct amdgpu_usermode_queue, refcount); 603 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 604 605 r = amdgpu_userq_destroy(uq_mgr, queue); 606 if (r) 607 drm_file_err(uq_mgr->file, "Failed to destroy usermode queue %d\n", r); 608 } 609 610 struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr, u32 qid) 611 { 612 struct amdgpu_usermode_queue *queue; 613 614 xa_lock(&uq_mgr->userq_xa); 615 queue = xa_load(&uq_mgr->userq_xa, qid); 616 if (queue) 617 kref_get(&queue->refcount); 618 xa_unlock(&uq_mgr->userq_xa); 619 620 return queue; 621 } 622 623 void amdgpu_userq_put(struct amdgpu_usermode_queue *queue) 624 { 625 if (queue) 626 kref_put(&queue->refcount, amdgpu_userq_kref_destroy); 627 } 628 629 static int amdgpu_userq_priority_permit(struct drm_file *filp, 630 int priority) 631 { 632 if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH) 633 return 0; 634 635 if (capable(CAP_SYS_NICE)) 636 return 0; 637 638 if (drm_is_current_master(filp)) 639 return 0; 640 641 return -EACCES; 642 } 643 644 static int 645 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) 646 { 647 struct amdgpu_fpriv *fpriv = filp->driver_priv; 648 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 649 struct amdgpu_device *adev = uq_mgr->adev; 650 const struct amdgpu_userq_funcs *uq_funcs; 651 struct amdgpu_usermode_queue *queue; 652 struct amdgpu_db_info db_info; 653 uint64_t index; 654 int priority; 655 u32 qid; 656 int r; 657 658 priority = 659 (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) 660 >> AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT; 661 r = amdgpu_userq_priority_permit(filp, priority); 662 if (r) 663 return r; 664 665 r = pm_runtime_resume_and_get(adev_to_drm(adev)->dev); 666 if (r < 0) { 667 drm_file_err(uq_mgr->file, "pm_runtime_resume_and_get() failed for userqueue create\n"); 668 return r; 669 } 670 671 uq_funcs = adev->userq_funcs[args->in.ip_type]; 672 if (!uq_funcs) { 673 r = -EINVAL; 674 goto err_pm_runtime; 675 } 676 677 queue = kzalloc_obj(struct amdgpu_usermode_queue); 678 if (!queue) { 679 r = -ENOMEM; 680 goto err_pm_runtime; 681 } 682 683 kref_init(&queue->refcount); 684 queue->doorbell_handle = args->in.doorbell_handle; 685 queue->queue_type = args->in.ip_type; 686 queue->vm = &fpriv->vm; 687 queue->priority = priority; 688 queue->xcp_id = (fpriv->xcp_id != AMDGPU_XCP_NO_PARTITION) ? 689 fpriv->xcp_id : 0; 690 queue->userq_mgr = uq_mgr; 691 INIT_DELAYED_WORK(&queue->hang_detect_work, 692 amdgpu_userq_hang_detect_work); 693 694 r = amdgpu_userq_fence_driver_alloc(adev, &queue->fence_drv); 695 if (r) 696 goto free_queue; 697 698 xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC); 699 mutex_init(&queue->fence_drv_lock); 700 /* Make sure the queue can actually run with those virtual addresses. */ 701 r = amdgpu_bo_reserve(fpriv->vm.root.bo, false); 702 if (r) 703 goto free_fence_drv; 704 705 if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va, 706 args->in.queue_size, 707 &queue->userq_vas.va.queue_rb) || 708 amdgpu_userq_input_va_validate(adev, queue, args->in.rptr_va, 709 sizeof(u64), 710 &queue->userq_vas.va.rptr) || 711 amdgpu_userq_input_va_validate(adev, queue, args->in.wptr_va, 712 sizeof(u64), 713 &queue->userq_vas.va.wptr)) { 714 r = -EINVAL; 715 amdgpu_bo_unreserve(fpriv->vm.root.bo); 716 goto free_fence_drv; 717 } 718 amdgpu_bo_unreserve(fpriv->vm.root.bo); 719 720 /* Convert relative doorbell offset into absolute doorbell index */ 721 db_info.queue_type = queue->queue_type; 722 db_info.doorbell_handle = queue->doorbell_handle; 723 db_info.db_obj = &queue->db_obj; 724 db_info.doorbell_offset = args->in.doorbell_offset; 725 r = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp, &index); 726 if (r) { 727 drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n"); 728 goto free_fence_drv; 729 } 730 731 queue->doorbell_index = index; 732 queue->doorbell_offset = (u32)args->in.doorbell_offset; 733 trace_amdgpu_userq_create_start(queue); 734 r = uq_funcs->mqd_create(queue, &args->in); 735 if (r) { 736 drm_file_err(uq_mgr->file, "Failed to create Queue\n"); 737 goto clean_doorbell_bo; 738 } 739 740 /* Update VM owner at userq submit-time for page-fault attribution. */ 741 amdgpu_vm_set_task_info(&fpriv->vm); 742 743 r = xa_insert_irq(&adev->userq_doorbell_xa, index, queue, 744 GFP_KERNEL); 745 if (r) 746 goto clean_mqd; 747 748 amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr); 749 750 /* don't map the queue if scheduling is halted */ 751 if (!adev->userq_halt_for_enforce_isolation || 752 ((queue->queue_type != AMDGPU_HW_IP_GFX) && 753 (queue->queue_type != AMDGPU_HW_IP_COMPUTE))) { 754 /* Serialize the map against an in-progress GPU reset (MES is 755 * unresponsive during recovery), matching amdgpu_userq_detach_doorbell(). 756 */ 757 down_read(&adev->reset_domain->sem); 758 r = amdgpu_userq_map_helper(queue); 759 up_read(&adev->reset_domain->sem); 760 if (r) { 761 drm_file_err(uq_mgr->file, "Failed to map Queue\n"); 762 trace_amdgpu_userq_create_end(queue, r); 763 mutex_unlock(&uq_mgr->userq_mutex); 764 goto erase_doorbell; 765 } 766 } 767 768 atomic_inc(&uq_mgr->userq_count[queue->queue_type]); 769 mutex_unlock(&uq_mgr->userq_mutex); 770 771 r = xa_alloc(&uq_mgr->userq_xa, &qid, queue, 772 XA_LIMIT(1, AMDGPU_MAX_USERQ_COUNT), 773 GFP_KERNEL); 774 if (r) { 775 /* 776 * This drops the last reference which should take care of 777 * all cleanup. 778 */ 779 trace_amdgpu_userq_create_end(queue, r); 780 amdgpu_userq_put(queue); 781 return r; 782 } 783 784 amdgpu_debugfs_userq_init(filp, queue, qid); 785 trace_amdgpu_userq_create_end(queue, 0); 786 args->out.queue_id = qid; 787 return 0; 788 789 erase_doorbell: 790 xa_erase_irq(&adev->userq_doorbell_xa, index); 791 clean_mqd: 792 uq_funcs->mqd_destroy(queue); 793 clean_doorbell_bo: 794 amdgpu_bo_reserve(queue->db_obj.obj, true); 795 amdgpu_bo_unpin(queue->db_obj.obj); 796 amdgpu_bo_unreserve(queue->db_obj.obj); 797 amdgpu_bo_unref(&queue->db_obj.obj); 798 free_fence_drv: 799 amdgpu_userq_fence_driver_free(queue); 800 free_queue: 801 trace_amdgpu_userq_create_end(queue, r); 802 kfree(queue); 803 err_pm_runtime: 804 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 805 return r; 806 } 807 808 static int amdgpu_userq_input_args_validate(struct drm_device *dev, 809 union drm_amdgpu_userq *args, 810 struct drm_file *filp) 811 { 812 struct amdgpu_device *adev = drm_to_adev(dev); 813 814 switch (args->in.op) { 815 case AMDGPU_USERQ_OP_CREATE: 816 if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK | 817 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE)) 818 return -EINVAL; 819 /* Usermode queues are only supported for GFX IP as of now */ 820 if (args->in.ip_type != AMDGPU_HW_IP_GFX && 821 args->in.ip_type != AMDGPU_HW_IP_DMA && 822 args->in.ip_type != AMDGPU_HW_IP_COMPUTE) { 823 drm_file_err(filp, "Usermode queue doesn't support IP type %u\n", 824 args->in.ip_type); 825 return -EINVAL; 826 } 827 828 if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) && 829 (args->in.ip_type != AMDGPU_HW_IP_GFX) && 830 (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) && 831 !amdgpu_is_tmz(adev)) { 832 drm_file_err(filp, "Secure only supported on GFX/Compute queues\n"); 833 return -EINVAL; 834 } 835 836 if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET || 837 args->in.queue_va == 0 || 838 args->in.queue_size == 0) { 839 drm_file_err(filp, "invalidate userq queue va or size\n"); 840 return -EINVAL; 841 } 842 843 if (!is_power_of_2(args->in.queue_size)) { 844 drm_file_err(filp, "Queue size must be a power of 2\n"); 845 return -EINVAL; 846 } 847 848 if (args->in.queue_size < AMDGPU_GPU_PAGE_SIZE) { 849 drm_file_err(filp, "Queue size smaller than AMDGPU_GPU_PAGE_SIZE\n"); 850 return -EINVAL; 851 } 852 853 if (!args->in.wptr_va || !args->in.rptr_va) { 854 drm_file_err(filp, "invalidate userq queue rptr or wptr\n"); 855 return -EINVAL; 856 } 857 858 if (!IS_ALIGNED(args->in.wptr_va, sizeof(u64)) || 859 !IS_ALIGNED(args->in.rptr_va, sizeof(u64))) { 860 drm_file_err(filp, "user queue rptr or wptr is not 8-byte aligned\n"); 861 return -EINVAL; 862 } 863 break; 864 case AMDGPU_USERQ_OP_FREE: 865 if (args->in.ip_type || 866 args->in.doorbell_handle || 867 args->in.doorbell_offset || 868 args->in.flags || 869 args->in.queue_va || 870 args->in.queue_size || 871 args->in.rptr_va || 872 args->in.wptr_va || 873 args->in.mqd || 874 args->in.mqd_size) 875 return -EINVAL; 876 break; 877 default: 878 return -EINVAL; 879 } 880 881 return 0; 882 } 883 884 bool amdgpu_userq_enabled(struct drm_device *dev) 885 { 886 struct amdgpu_device *adev = drm_to_adev(dev); 887 int i; 888 889 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) { 890 if (adev->userq_funcs[i]) 891 return true; 892 } 893 894 return false; 895 } 896 897 int amdgpu_userq_ioctl(struct drm_device *dev, void *data, 898 struct drm_file *filp) 899 { 900 union drm_amdgpu_userq *args = data; 901 struct amdgpu_fpriv *fpriv = filp->driver_priv; 902 struct amdgpu_usermode_queue *queue; 903 int r = 0; 904 905 if (!amdgpu_userq_enabled(dev)) 906 return -ENOTSUPP; 907 908 if (amdgpu_userq_input_args_validate(dev, args, filp) < 0) 909 return -EINVAL; 910 911 switch (args->in.op) { 912 case AMDGPU_USERQ_OP_CREATE: 913 r = amdgpu_userq_create(filp, args); 914 if (r) 915 drm_file_err(filp, "Failed to create usermode queue\n"); 916 break; 917 918 case AMDGPU_USERQ_OP_FREE: { 919 xa_lock(&fpriv->userq_mgr.userq_xa); 920 queue = __xa_erase(&fpriv->userq_mgr.userq_xa, args->in.queue_id); 921 xa_unlock(&fpriv->userq_mgr.userq_xa); 922 if (!queue) 923 return -ENOENT; 924 925 amdgpu_userq_put(queue); 926 break; 927 } 928 929 default: 930 drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op); 931 return -EINVAL; 932 } 933 934 return r; 935 } 936 937 static int 938 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) 939 { 940 struct amdgpu_usermode_queue *queue; 941 unsigned long queue_id; 942 int ret = 0, r; 943 944 mutex_lock(&uq_mgr->userq_mutex); 945 /* Resume all the queues for this process */ 946 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 947 948 if (!amdgpu_userq_buffer_vas_mapped(queue)) { 949 drm_file_err(uq_mgr->file, 950 "trying restore queue without va mapping\n"); 951 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_INVALID_VA); 952 queue->state = AMDGPU_USERQ_STATE_INVALID_VA; 953 continue; 954 } 955 956 r = amdgpu_userq_map_helper(queue); 957 if (r) 958 ret = r; 959 } 960 mutex_unlock(&uq_mgr->userq_mutex); 961 962 if (ret) 963 drm_file_err(uq_mgr->file, 964 "Failed to map all the queues, restore failed ret=%d\n", ret); 965 return ret; 966 } 967 968 static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo) 969 { 970 struct ttm_operation_ctx ctx = { false, false }; 971 972 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 973 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 974 } 975 976 /* Handle all BOs on the invalidated list, validate them and update the PTs */ 977 static int 978 amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec, 979 struct amdgpu_vm *vm) 980 { 981 struct ttm_operation_ctx ctx = { false, false }; 982 struct amdgpu_bo_va *bo_va; 983 struct amdgpu_bo *bo; 984 int ret; 985 986 spin_lock(&vm->individual_lock); 987 while (!list_empty(&vm->always_valid.evicted)) { 988 bo_va = list_first_entry(&vm->always_valid.evicted, 989 struct amdgpu_bo_va, 990 base.vm_status); 991 spin_unlock(&vm->individual_lock); 992 993 bo = bo_va->base.bo; 994 ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 995 TTM_NUM_MOVE_FENCES + 1); 996 if (unlikely(ret)) 997 return ret; 998 999 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 1000 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1001 if (ret) 1002 return ret; 1003 1004 /* This moves the bo_va to the idle list */ 1005 ret = amdgpu_vm_bo_update(adev, bo_va, false); 1006 if (ret) 1007 return ret; 1008 1009 spin_lock(&vm->individual_lock); 1010 } 1011 spin_unlock(&vm->individual_lock); 1012 1013 return 0; 1014 } 1015 1016 /* Make sure the whole VM is ready to be used */ 1017 static int 1018 amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr) 1019 { 1020 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 1021 bool invalidated = false, new_addition = false; 1022 struct ttm_operation_ctx ctx = { true, false }; 1023 struct amdgpu_device *adev = uq_mgr->adev; 1024 struct amdgpu_hmm_range *range; 1025 struct amdgpu_vm *vm = &fpriv->vm; 1026 unsigned long key, tmp_key; 1027 struct amdgpu_bo_va *bo_va; 1028 struct amdgpu_usermode_queue *queue; 1029 struct amdgpu_bo *bo; 1030 struct drm_exec exec; 1031 struct xarray xa; 1032 int ret; 1033 1034 xa_init(&xa); 1035 1036 retry_lock: 1037 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 1038 drm_exec_until_all_locked(&exec) { 1039 ret = amdgpu_vm_lock_pd(vm, &exec, 1); 1040 drm_exec_retry_on_contention(&exec); 1041 if (unlikely(ret)) 1042 goto unlock_all; 1043 1044 ret = amdgpu_vm_lock_individual(vm, &exec, TTM_NUM_MOVE_FENCES + 1); 1045 drm_exec_retry_on_contention(&exec); 1046 if (unlikely(ret)) 1047 goto unlock_all; 1048 1049 /* This validates PDs, PTs and per VM BOs */ 1050 ret = amdgpu_vm_validate(adev, vm, NULL, 1051 amdgpu_userq_validate_vm, 1052 NULL); 1053 if (unlikely(ret)) 1054 goto unlock_all; 1055 1056 /* This locks and validates the remaining evicted BOs */ 1057 ret = amdgpu_userq_bo_validate(adev, &exec, vm); 1058 drm_exec_retry_on_contention(&exec); 1059 if (unlikely(ret)) 1060 goto unlock_all; 1061 1062 /* 1063 * WPTR BOs are VM-mapped, but each BO has its own reservation 1064 * object. Lock them into this drm_exec ww context so the later 1065 * amdgpu_bo_gpu_offset() reads are done with the BO resv locked. 1066 */ 1067 xa_for_each(&uq_mgr->userq_xa, tmp_key, queue) { 1068 struct ttm_operation_ctx wptr_ctx = { false, false }; 1069 1070 bo = queue->wptr_obj.obj; 1071 if (!bo) 1072 continue; 1073 1074 ret = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1075 TTM_NUM_MOVE_FENCES + 1); 1076 drm_exec_retry_on_contention(&exec); 1077 if (unlikely(ret)) 1078 goto unlock_all; 1079 1080 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 1081 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &wptr_ctx); 1082 if (unlikely(ret)) 1083 goto unlock_all; 1084 } 1085 } 1086 1087 if (invalidated) { 1088 xa_for_each(&xa, tmp_key, range) { 1089 bo = range->bo; 1090 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU); 1091 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1092 if (ret) 1093 goto unlock_all; 1094 1095 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range); 1096 1097 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); 1098 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1099 if (ret) 1100 goto unlock_all; 1101 } 1102 invalidated = false; 1103 } 1104 1105 ret = amdgpu_vm_handle_moved(adev, vm, NULL); 1106 if (ret) 1107 goto unlock_all; 1108 1109 /* 1110 * PRT/sparse mappings are kept off the vm_bo state lists, so 1111 * amdgpu_vm_handle_moved() does not touch them. Refresh their PTEs 1112 * explicitly here (as the CS path does) so sparse mappings survive a 1113 * VRAM-lost reset. 1114 */ 1115 ret = amdgpu_vm_bo_update(adev, fpriv->prt_va, false); 1116 if (ret) 1117 goto unlock_all; 1118 1119 key = 0; 1120 /* Validate User Ptr BOs */ 1121 list_for_each_entry(bo_va, &vm->always_valid.idle, base.vm_status) { 1122 bo = bo_va->base.bo; 1123 if (!bo) 1124 continue; 1125 1126 if (!amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) 1127 continue; 1128 1129 range = xa_load(&xa, key); 1130 if (range && range->bo != bo) { 1131 xa_erase(&xa, key); 1132 amdgpu_hmm_range_free(range); 1133 range = NULL; 1134 } 1135 1136 if (!range) { 1137 range = amdgpu_hmm_range_alloc(bo); 1138 if (!range) { 1139 ret = -ENOMEM; 1140 goto unlock_all; 1141 } 1142 1143 xa_store(&xa, key, range, GFP_KERNEL); 1144 new_addition = true; 1145 } 1146 key++; 1147 } 1148 1149 if (new_addition) { 1150 drm_exec_fini(&exec); 1151 xa_for_each(&xa, tmp_key, range) { 1152 if (!range) 1153 continue; 1154 bo = range->bo; 1155 ret = amdgpu_ttm_tt_get_user_pages(bo, range); 1156 if (ret) 1157 goto free_ranges; 1158 } 1159 1160 invalidated = true; 1161 new_addition = false; 1162 goto retry_lock; 1163 } 1164 1165 ret = amdgpu_vm_update_pdes(adev, vm, false); 1166 if (ret) 1167 goto unlock_all; 1168 1169 /* 1170 * We need to wait for all VM updates to finish before restarting the 1171 * queues. Using the idle list like that is now ok since everything is 1172 * locked in place. 1173 */ 1174 list_for_each_entry(bo_va, &vm->always_valid.idle, base.vm_status) 1175 dma_fence_wait(bo_va->last_pt_update, false); 1176 /* 1177 * The PRT bo_va is kept off the state lists, so its PTE update fence 1178 * lands in prt_va->last_pt_update rather than vm->last_update; wait on 1179 * it explicitly (as the CS path syncs it) before restarting queues. 1180 */ 1181 dma_fence_wait(fpriv->prt_va->last_pt_update, false); 1182 dma_fence_wait(vm->last_update, false); 1183 1184 xa_for_each(&uq_mgr->userq_xa, tmp_key, queue) { 1185 bo = queue->wptr_obj.obj; 1186 if (!bo) { 1187 ret = -EINVAL; 1188 goto unlock_all; 1189 } 1190 1191 ret = amdgpu_ttm_alloc_gart(&bo->tbo); 1192 if (unlikely(ret)) { 1193 drm_file_err(uq_mgr->file, 1194 "failed to bind wptr bo to gart on resume, qid=%lu ret=%d\n", 1195 tmp_key, ret); 1196 goto unlock_all; 1197 } 1198 1199 queue->wptr_obj.gpu_addr = amdgpu_bo_gpu_offset(bo); 1200 } 1201 1202 ret = amdgpu_evf_mgr_rearm(&fpriv->evf_mgr, &exec); 1203 if (ret) { 1204 drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n"); 1205 goto unlock_all; 1206 } 1207 1208 ret = amdgpu_userq_restore_all(uq_mgr); 1209 1210 unlock_all: 1211 drm_exec_fini(&exec); 1212 free_ranges: 1213 xa_for_each(&xa, tmp_key, range) { 1214 if (!range) 1215 continue; 1216 bo = range->bo; 1217 amdgpu_hmm_range_free(range); 1218 } 1219 xa_destroy(&xa); 1220 return ret; 1221 } 1222 1223 static void amdgpu_userq_restore_worker(struct work_struct *work) 1224 { 1225 struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work); 1226 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 1227 struct dma_fence *ev_fence; 1228 int ret; 1229 1230 ev_fence = amdgpu_evf_mgr_get_fence(&fpriv->evf_mgr); 1231 if (!dma_fence_is_signaled(ev_fence)) 1232 goto put_fence; 1233 1234 ret = amdgpu_userq_vm_validate_and_restore_queue(uq_mgr); 1235 if (ret) { 1236 drm_file_err(uq_mgr->file, "Failed to validate BOs to restore ret=%d\n", ret); 1237 goto put_fence; 1238 } 1239 1240 put_fence: 1241 dma_fence_put(ev_fence); 1242 } 1243 1244 void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev, 1245 u32 pasid, u32 doorbell_offset) 1246 { 1247 struct xarray *xa = &adev->userq_doorbell_xa; 1248 struct amdgpu_usermode_queue *queue; 1249 unsigned long flags, idx; 1250 1251 xa_lock_irqsave(xa, flags); 1252 xa_for_each(xa, idx, queue) { 1253 if (queue->vm && queue->vm->pasid == pasid && 1254 queue->doorbell_offset == doorbell_offset) { 1255 amdgpu_userq_start_hang_detect_work(queue); 1256 break; 1257 } 1258 } 1259 xa_unlock_irqrestore(xa, flags); 1260 } 1261 1262 static int 1263 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) 1264 { 1265 struct amdgpu_usermode_queue *queue; 1266 unsigned long queue_id; 1267 int ret = 0, r; 1268 1269 /* Try to unmap all the queues in this process ctx */ 1270 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 1271 r = amdgpu_userq_unmap_helper(queue); 1272 if (r) 1273 ret = r; 1274 } 1275 1276 if (ret) { 1277 drm_file_err(uq_mgr->file, 1278 "Couldn't unmap all the queues, eviction failed ret=%d\n", ret); 1279 amdgpu_reset_domain_schedule(uq_mgr->adev->reset_domain, 1280 &uq_mgr->reset_work); 1281 flush_work(&uq_mgr->reset_work); 1282 } 1283 return ret; 1284 } 1285 1286 void 1287 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) 1288 { 1289 struct amdgpu_usermode_queue *queue; 1290 unsigned long queue_id; 1291 1292 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 1293 struct dma_fence *f = queue->last_fence; 1294 1295 if (!f) 1296 continue; 1297 1298 dma_fence_wait(f, false); 1299 } 1300 } 1301 1302 void 1303 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr) 1304 { 1305 amdgpu_userq_evict_all(uq_mgr); 1306 } 1307 1308 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv, 1309 struct amdgpu_device *adev) 1310 { 1311 mutex_init(&userq_mgr->userq_mutex); 1312 xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC); 1313 userq_mgr->adev = adev; 1314 userq_mgr->file = file_priv; 1315 userq_mgr->proc_ctx_allocated = false; 1316 mutex_init(&userq_mgr->proc_ctx_lock); 1317 1318 INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker); 1319 INIT_WORK(&userq_mgr->reset_work, amdgpu_userq_mgr_reset_work); 1320 return 0; 1321 } 1322 1323 void amdgpu_userq_mgr_cancel_reset_work(struct amdgpu_device *adev) 1324 { 1325 struct xarray *xa = &adev->userq_doorbell_xa; 1326 struct amdgpu_usermode_queue *queue; 1327 unsigned long flags, queue_id; 1328 1329 xa_lock_irqsave(xa, flags); 1330 xa_for_each(xa, queue_id, queue) { 1331 cancel_delayed_work(&queue->hang_detect_work); 1332 cancel_work(&queue->userq_mgr->reset_work); 1333 } 1334 xa_unlock_irqrestore(xa, flags); 1335 } 1336 1337 void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr) 1338 { 1339 cancel_delayed_work_sync(&userq_mgr->resume_work); 1340 } 1341 1342 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr) 1343 { 1344 struct amdgpu_mes *mes = &userq_mgr->adev->mes; 1345 struct amdgpu_usermode_queue *queue; 1346 unsigned long queue_id = 0; 1347 1348 for (;;) { 1349 xa_lock(&userq_mgr->userq_xa); 1350 queue = xa_find(&userq_mgr->userq_xa, &queue_id, ULONG_MAX, 1351 XA_PRESENT); 1352 if (queue) 1353 __xa_erase(&userq_mgr->userq_xa, queue_id); 1354 xa_unlock(&userq_mgr->userq_xa); 1355 1356 if (!queue) 1357 break; 1358 1359 amdgpu_userq_put(queue); 1360 } 1361 1362 xa_destroy(&userq_mgr->userq_xa); 1363 1364 /* 1365 * Drain any in-flight reset_work. By this point all queues are freed 1366 * and userq_count is 0, so if reset_work starts now it exits early. 1367 * We still need to wait in case it was already executing gpu_recover. 1368 */ 1369 cancel_work_sync(&userq_mgr->reset_work); 1370 1371 if (userq_mgr->proc_ctx_allocated) { 1372 amdgpu_mes_free_proc_ctx_index(mes, userq_mgr->proc_ctx_array_index); 1373 userq_mgr->proc_ctx_allocated = false; 1374 } 1375 amdgpu_bo_free_kernel(&userq_mgr->proc_ctx_obj.obj, 1376 &userq_mgr->proc_ctx_obj.gpu_addr, 1377 &userq_mgr->proc_ctx_obj.cpu_ptr); 1378 1379 mutex_destroy(&userq_mgr->proc_ctx_lock); 1380 mutex_destroy(&userq_mgr->userq_mutex); 1381 } 1382 1383 int amdgpu_userq_suspend(struct amdgpu_device *adev) 1384 { 1385 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1386 struct amdgpu_usermode_queue *queue; 1387 struct amdgpu_userq_mgr *uqm; 1388 unsigned long queue_id; 1389 int r; 1390 1391 if (!ip_mask) 1392 return 0; 1393 1394 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1395 uqm = queue->userq_mgr; 1396 cancel_delayed_work_sync(&uqm->resume_work); 1397 guard(mutex)(&uqm->userq_mutex); 1398 if (adev->in_s0ix) 1399 r = amdgpu_userq_preempt_helper(queue); 1400 else 1401 r = amdgpu_userq_unmap_helper(queue); 1402 if (r) 1403 return r; 1404 } 1405 return 0; 1406 } 1407 1408 int amdgpu_userq_resume(struct amdgpu_device *adev) 1409 { 1410 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1411 struct amdgpu_usermode_queue *queue; 1412 struct amdgpu_userq_mgr *uqm; 1413 unsigned long queue_id; 1414 int r; 1415 1416 if (!ip_mask) 1417 return 0; 1418 1419 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1420 uqm = queue->userq_mgr; 1421 guard(mutex)(&uqm->userq_mutex); 1422 if (adev->in_s0ix) 1423 r = amdgpu_userq_restore_helper(queue); 1424 else 1425 r = amdgpu_userq_map_helper(queue); 1426 if (r) 1427 return r; 1428 } 1429 1430 return 0; 1431 } 1432 1433 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, 1434 u32 idx) 1435 { 1436 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1437 struct amdgpu_usermode_queue *queue; 1438 struct amdgpu_userq_mgr *uqm; 1439 unsigned long queue_id; 1440 int ret = 0, r; 1441 1442 /* only need to stop gfx/compute */ 1443 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 1444 return 0; 1445 1446 if (adev->userq_halt_for_enforce_isolation) 1447 dev_warn(adev->dev, "userq scheduling already stopped!\n"); 1448 adev->userq_halt_for_enforce_isolation = true; 1449 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1450 uqm = queue->userq_mgr; 1451 cancel_delayed_work_sync(&uqm->resume_work); 1452 mutex_lock(&uqm->userq_mutex); 1453 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 1454 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 1455 (queue->xcp_id == idx)) { 1456 r = amdgpu_userq_preempt_helper(queue); 1457 if (r) 1458 ret = r; 1459 } 1460 mutex_unlock(&uqm->userq_mutex); 1461 } 1462 1463 return ret; 1464 } 1465 1466 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, 1467 u32 idx) 1468 { 1469 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1470 struct amdgpu_usermode_queue *queue; 1471 struct amdgpu_userq_mgr *uqm; 1472 unsigned long queue_id; 1473 int ret = 0, r; 1474 1475 /* only need to stop gfx/compute */ 1476 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 1477 return 0; 1478 1479 if (!adev->userq_halt_for_enforce_isolation) 1480 dev_warn(adev->dev, "userq scheduling already started!\n"); 1481 1482 adev->userq_halt_for_enforce_isolation = false; 1483 1484 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1485 uqm = queue->userq_mgr; 1486 mutex_lock(&uqm->userq_mutex); 1487 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 1488 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 1489 (queue->xcp_id == idx)) { 1490 r = amdgpu_userq_restore_helper(queue); 1491 if (r) 1492 ret = r; 1493 } 1494 mutex_unlock(&uqm->userq_mutex); 1495 } 1496 1497 return ret; 1498 } 1499 1500 void amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev, 1501 struct amdgpu_bo_va_mapping *mapping) 1502 { 1503 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1504 struct amdgpu_bo_va *bo_va = mapping->bo_va; 1505 struct dma_resv *resv = bo_va->base.bo->tbo.base.resv; 1506 1507 if (!ip_mask) 1508 return; 1509 1510 /** 1511 * The userq VA mapping reservation should include the eviction fence. 1512 * Note: The eviction fence may be attached to different BOs and this 1513 * unmap is only for one kind of userq VAs, so at this point suppose 1514 * the eviction fence is always unsignaled. 1515 */ 1516 dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP, 1517 false, MAX_SCHEDULE_TIMEOUT); 1518 } 1519 1520 void amdgpu_userq_pre_reset(struct amdgpu_device *adev) 1521 { 1522 const struct amdgpu_userq_funcs *userq_funcs; 1523 struct amdgpu_usermode_queue *queue; 1524 unsigned long queue_id; 1525 1526 /* TODO: We probably need a new lock for the queue state */ 1527 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1528 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) { 1529 trace_amdgpu_userq_state_start(queue); 1530 userq_funcs = adev->userq_funcs[queue->queue_type]; 1531 userq_funcs->unmap(queue); 1532 /* just mark all queues as hung at this point. 1533 * if unmap succeeds, we could map again 1534 * in amdgpu_userq_post_reset() if vram is not lost 1535 */ 1536 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG); 1537 queue->state = AMDGPU_USERQ_STATE_HUNG; 1538 } 1539 /* Force-complete any pending fence regardless of queue state so 1540 * that eviction/suspend and queue teardown waiters don't block 1541 * forever on a fence that will never signal after the reset. 1542 */ 1543 amdgpu_userq_fence_driver_force_completion(queue); 1544 } 1545 } 1546 1547 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost) 1548 { 1549 /* if any queue state is AMDGPU_USERQ_STATE_UNMAPPED 1550 * at this point, we should be able to map it again 1551 * and continue if vram is not lost. 1552 */ 1553 struct amdgpu_usermode_queue *queue; 1554 const struct amdgpu_userq_funcs *userq_funcs; 1555 unsigned long queue_id; 1556 int ret = 0, r; 1557 1558 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1559 if (queue->state == AMDGPU_USERQ_STATE_HUNG && !vram_lost) { 1560 trace_amdgpu_userq_state_start(queue); 1561 1562 userq_funcs = adev->userq_funcs[queue->queue_type]; 1563 /* Re-map queue */ 1564 r = userq_funcs->map(queue); 1565 if (r) { 1566 dev_err(adev->dev, "Failed to remap queue %ld\n", queue_id); 1567 ret = r; 1568 continue; 1569 } 1570 trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_MAPPED); 1571 queue->state = AMDGPU_USERQ_STATE_MAPPED; 1572 } 1573 } 1574 1575 return ret; 1576 } 1577