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