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 || !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(sizeof(*va_cursor), GFP_KERNEL); 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 int amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue *queue) 431 { 432 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 433 struct dma_fence *f = queue->last_fence; 434 int ret = 0; 435 436 if (f && !dma_fence_is_signaled(f)) { 437 ret = dma_fence_wait_timeout(f, true, MAX_SCHEDULE_TIMEOUT); 438 if (ret <= 0) { 439 drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n", 440 f->context, f->seqno); 441 queue->state = AMDGPU_USERQ_STATE_HUNG; 442 return -ETIME; 443 } 444 } 445 446 return ret; 447 } 448 449 static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue, 450 int queue_id) 451 { 452 struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; 453 struct amdgpu_device *adev = uq_mgr->adev; 454 const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; 455 456 /* Wait for mode-1 reset to complete */ 457 down_read(&adev->reset_domain->sem); 458 459 /* Drop the userq reference. */ 460 amdgpu_userq_buffer_vas_list_cleanup(adev, queue); 461 uq_funcs->mqd_destroy(queue); 462 amdgpu_userq_fence_driver_free(queue); 463 /* Use interrupt-safe locking since IRQ handlers may access these XArrays */ 464 xa_erase_irq(&uq_mgr->userq_xa, (unsigned long)queue_id); 465 xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index); 466 queue->userq_mgr = NULL; 467 list_del(&queue->userq_va_list); 468 kfree(queue); 469 470 up_read(&adev->reset_domain->sem); 471 } 472 473 static struct amdgpu_usermode_queue * 474 amdgpu_userq_find(struct amdgpu_userq_mgr *uq_mgr, int qid) 475 { 476 return xa_load(&uq_mgr->userq_xa, qid); 477 } 478 479 void 480 amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr, 481 struct amdgpu_eviction_fence_mgr *evf_mgr) 482 { 483 struct amdgpu_eviction_fence *ev_fence; 484 485 retry: 486 /* Flush any pending resume work to create ev_fence */ 487 flush_delayed_work(&uq_mgr->resume_work); 488 489 mutex_lock(&uq_mgr->userq_mutex); 490 spin_lock(&evf_mgr->ev_fence_lock); 491 ev_fence = evf_mgr->ev_fence; 492 spin_unlock(&evf_mgr->ev_fence_lock); 493 if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) { 494 mutex_unlock(&uq_mgr->userq_mutex); 495 /* 496 * Looks like there was no pending resume work, 497 * add one now to create a valid eviction fence 498 */ 499 schedule_delayed_work(&uq_mgr->resume_work, 0); 500 goto retry; 501 } 502 } 503 504 int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr, 505 struct amdgpu_userq_obj *userq_obj, 506 int size) 507 { 508 struct amdgpu_device *adev = uq_mgr->adev; 509 struct amdgpu_bo_param bp; 510 int r; 511 512 memset(&bp, 0, sizeof(bp)); 513 bp.byte_align = PAGE_SIZE; 514 bp.domain = AMDGPU_GEM_DOMAIN_GTT; 515 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | 516 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 517 bp.type = ttm_bo_type_kernel; 518 bp.size = size; 519 bp.resv = NULL; 520 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 521 522 r = amdgpu_bo_create(adev, &bp, &userq_obj->obj); 523 if (r) { 524 drm_file_err(uq_mgr->file, "Failed to allocate BO for userqueue (%d)", r); 525 return r; 526 } 527 528 r = amdgpu_bo_reserve(userq_obj->obj, true); 529 if (r) { 530 drm_file_err(uq_mgr->file, "Failed to reserve BO to map (%d)", r); 531 goto free_obj; 532 } 533 534 r = amdgpu_ttm_alloc_gart(&(userq_obj->obj)->tbo); 535 if (r) { 536 drm_file_err(uq_mgr->file, "Failed to alloc GART for userqueue object (%d)", r); 537 goto unresv; 538 } 539 540 r = amdgpu_bo_kmap(userq_obj->obj, &userq_obj->cpu_ptr); 541 if (r) { 542 drm_file_err(uq_mgr->file, "Failed to map BO for userqueue (%d)", r); 543 goto unresv; 544 } 545 546 userq_obj->gpu_addr = amdgpu_bo_gpu_offset(userq_obj->obj); 547 amdgpu_bo_unreserve(userq_obj->obj); 548 memset(userq_obj->cpu_ptr, 0, size); 549 return 0; 550 551 unresv: 552 amdgpu_bo_unreserve(userq_obj->obj); 553 554 free_obj: 555 amdgpu_bo_unref(&userq_obj->obj); 556 return r; 557 } 558 559 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr, 560 struct amdgpu_userq_obj *userq_obj) 561 { 562 amdgpu_bo_kunmap(userq_obj->obj); 563 amdgpu_bo_unref(&userq_obj->obj); 564 } 565 566 uint64_t 567 amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, 568 struct amdgpu_db_info *db_info, 569 struct drm_file *filp) 570 { 571 uint64_t index; 572 struct drm_gem_object *gobj; 573 struct amdgpu_userq_obj *db_obj = db_info->db_obj; 574 int r, db_size; 575 576 gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle); 577 if (gobj == NULL) { 578 drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n"); 579 return -EINVAL; 580 } 581 582 db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj)); 583 drm_gem_object_put(gobj); 584 585 r = amdgpu_bo_reserve(db_obj->obj, true); 586 if (r) { 587 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 588 goto unref_bo; 589 } 590 591 /* Pin the BO before generating the index, unpin in queue destroy */ 592 r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL); 593 if (r) { 594 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 595 goto unresv_bo; 596 } 597 598 switch (db_info->queue_type) { 599 case AMDGPU_HW_IP_GFX: 600 case AMDGPU_HW_IP_COMPUTE: 601 case AMDGPU_HW_IP_DMA: 602 db_size = sizeof(u64); 603 break; 604 default: 605 drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n", 606 db_info->queue_type); 607 r = -EINVAL; 608 goto unpin_bo; 609 } 610 611 index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj, 612 db_info->doorbell_offset, db_size); 613 drm_dbg_driver(adev_to_drm(uq_mgr->adev), 614 "[Usermode queues] doorbell index=%lld\n", index); 615 amdgpu_bo_unreserve(db_obj->obj); 616 return index; 617 618 unpin_bo: 619 amdgpu_bo_unpin(db_obj->obj); 620 unresv_bo: 621 amdgpu_bo_unreserve(db_obj->obj); 622 unref_bo: 623 amdgpu_bo_unref(&db_obj->obj); 624 return r; 625 } 626 627 static int 628 amdgpu_userq_destroy(struct drm_file *filp, int queue_id) 629 { 630 struct amdgpu_fpriv *fpriv = filp->driver_priv; 631 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 632 struct amdgpu_device *adev = uq_mgr->adev; 633 struct amdgpu_usermode_queue *queue; 634 int r = 0; 635 636 cancel_delayed_work_sync(&uq_mgr->resume_work); 637 mutex_lock(&uq_mgr->userq_mutex); 638 queue = amdgpu_userq_find(uq_mgr, queue_id); 639 if (!queue) { 640 drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n"); 641 mutex_unlock(&uq_mgr->userq_mutex); 642 return -EINVAL; 643 } 644 amdgpu_userq_wait_for_last_fence(queue); 645 /* Cancel any pending hang detection work and cleanup */ 646 if (queue->hang_detect_fence) { 647 cancel_delayed_work_sync(&queue->hang_detect_work); 648 queue->hang_detect_fence = NULL; 649 } 650 r = amdgpu_bo_reserve(queue->db_obj.obj, true); 651 if (!r) { 652 amdgpu_bo_unpin(queue->db_obj.obj); 653 amdgpu_bo_unreserve(queue->db_obj.obj); 654 } 655 amdgpu_bo_unref(&queue->db_obj.obj); 656 657 r = amdgpu_bo_reserve(queue->wptr_obj.obj, true); 658 if (!r) { 659 amdgpu_bo_unpin(queue->wptr_obj.obj); 660 amdgpu_bo_unreserve(queue->wptr_obj.obj); 661 } 662 amdgpu_bo_unref(&queue->wptr_obj.obj); 663 664 atomic_dec(&uq_mgr->userq_count[queue->queue_type]); 665 #if defined(CONFIG_DEBUG_FS) 666 debugfs_remove_recursive(queue->debugfs_queue); 667 #endif 668 amdgpu_userq_detect_and_reset_queues(uq_mgr); 669 r = amdgpu_userq_unmap_helper(queue); 670 /*TODO: It requires a reset for userq hw unmap error*/ 671 if (unlikely(r != AMDGPU_USERQ_STATE_UNMAPPED)) { 672 drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n"); 673 queue->state = AMDGPU_USERQ_STATE_HUNG; 674 } 675 amdgpu_userq_cleanup(queue, queue_id); 676 mutex_unlock(&uq_mgr->userq_mutex); 677 678 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 679 680 return r; 681 } 682 683 static int amdgpu_userq_priority_permit(struct drm_file *filp, 684 int priority) 685 { 686 if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH) 687 return 0; 688 689 if (capable(CAP_SYS_NICE)) 690 return 0; 691 692 if (drm_is_current_master(filp)) 693 return 0; 694 695 return -EACCES; 696 } 697 698 #if defined(CONFIG_DEBUG_FS) 699 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused) 700 { 701 struct amdgpu_usermode_queue *queue = m->private; 702 struct amdgpu_bo *bo; 703 int r; 704 705 if (!queue || !queue->mqd.obj) 706 return -EINVAL; 707 708 bo = amdgpu_bo_ref(queue->mqd.obj); 709 r = amdgpu_bo_reserve(bo, true); 710 if (r) { 711 amdgpu_bo_unref(&bo); 712 return -EINVAL; 713 } 714 715 seq_printf(m, "queue_type: %d\n", queue->queue_type); 716 seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj)); 717 718 amdgpu_bo_unreserve(bo); 719 amdgpu_bo_unref(&bo); 720 721 return 0; 722 } 723 724 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file) 725 { 726 return single_open(file, amdgpu_mqd_info_read, inode->i_private); 727 } 728 729 static const struct file_operations amdgpu_mqd_info_fops = { 730 .owner = THIS_MODULE, 731 .open = amdgpu_mqd_info_open, 732 .read = seq_read, 733 .llseek = seq_lseek, 734 .release = single_release, 735 }; 736 #endif 737 738 static int 739 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) 740 { 741 struct amdgpu_fpriv *fpriv = filp->driver_priv; 742 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 743 struct amdgpu_device *adev = uq_mgr->adev; 744 const struct amdgpu_userq_funcs *uq_funcs; 745 struct amdgpu_usermode_queue *queue; 746 struct amdgpu_db_info db_info; 747 char *queue_name; 748 bool skip_map_queue; 749 u32 qid; 750 uint64_t index; 751 int r = 0; 752 int priority = 753 (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >> 754 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT; 755 756 r = amdgpu_userq_priority_permit(filp, priority); 757 if (r) 758 return r; 759 760 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 761 if (r < 0) { 762 drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n"); 763 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 764 return r; 765 } 766 767 /* 768 * There could be a situation that we are creating a new queue while 769 * the other queues under this UQ_mgr are suspended. So if there is any 770 * resume work pending, wait for it to get done. 771 * 772 * This will also make sure we have a valid eviction fence ready to be used. 773 */ 774 amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr); 775 776 uq_funcs = adev->userq_funcs[args->in.ip_type]; 777 if (!uq_funcs) { 778 drm_file_err(uq_mgr->file, "Usermode queue is not supported for this IP (%u)\n", 779 args->in.ip_type); 780 r = -EINVAL; 781 goto unlock; 782 } 783 784 queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL); 785 if (!queue) { 786 drm_file_err(uq_mgr->file, "Failed to allocate memory for queue\n"); 787 r = -ENOMEM; 788 goto unlock; 789 } 790 791 INIT_LIST_HEAD(&queue->userq_va_list); 792 queue->doorbell_handle = args->in.doorbell_handle; 793 queue->queue_type = args->in.ip_type; 794 queue->vm = &fpriv->vm; 795 queue->priority = priority; 796 797 db_info.queue_type = queue->queue_type; 798 db_info.doorbell_handle = queue->doorbell_handle; 799 db_info.db_obj = &queue->db_obj; 800 db_info.doorbell_offset = args->in.doorbell_offset; 801 802 queue->userq_mgr = uq_mgr; 803 /* Validate the userq virtual address.*/ 804 if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va, args->in.queue_size) || 805 amdgpu_userq_input_va_validate(adev, queue, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE) || 806 amdgpu_userq_input_va_validate(adev, queue, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) { 807 r = -EINVAL; 808 kfree(queue); 809 goto unlock; 810 } 811 812 /* Convert relative doorbell offset into absolute doorbell index */ 813 index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp); 814 if (index == (uint64_t)-EINVAL) { 815 drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n"); 816 kfree(queue); 817 r = -EINVAL; 818 goto unlock; 819 } 820 821 queue->doorbell_index = index; 822 xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC); 823 r = amdgpu_userq_fence_driver_alloc(adev, queue); 824 if (r) { 825 drm_file_err(uq_mgr->file, "Failed to alloc fence driver\n"); 826 goto unlock; 827 } 828 829 r = uq_funcs->mqd_create(queue, &args->in); 830 if (r) { 831 drm_file_err(uq_mgr->file, "Failed to create Queue\n"); 832 amdgpu_userq_fence_driver_free(queue); 833 kfree(queue); 834 goto unlock; 835 } 836 837 /* Wait for mode-1 reset to complete */ 838 down_read(&adev->reset_domain->sem); 839 r = xa_err(xa_store_irq(&adev->userq_doorbell_xa, index, queue, GFP_KERNEL)); 840 if (r) { 841 kfree(queue); 842 up_read(&adev->reset_domain->sem); 843 goto unlock; 844 } 845 846 r = xa_alloc(&uq_mgr->userq_xa, &qid, queue, 847 XA_LIMIT(1, AMDGPU_MAX_USERQ_COUNT), GFP_KERNEL); 848 if (r) { 849 drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n"); 850 amdgpu_userq_fence_driver_free(queue); 851 uq_funcs->mqd_destroy(queue); 852 kfree(queue); 853 r = -ENOMEM; 854 up_read(&adev->reset_domain->sem); 855 goto unlock; 856 } 857 up_read(&adev->reset_domain->sem); 858 859 /* don't map the queue if scheduling is halted */ 860 if (adev->userq_halt_for_enforce_isolation && 861 ((queue->queue_type == AMDGPU_HW_IP_GFX) || 862 (queue->queue_type == AMDGPU_HW_IP_COMPUTE))) 863 skip_map_queue = true; 864 else 865 skip_map_queue = false; 866 if (!skip_map_queue) { 867 r = amdgpu_userq_map_helper(queue); 868 if (r) { 869 drm_file_err(uq_mgr->file, "Failed to map Queue\n"); 870 xa_erase(&uq_mgr->userq_xa, qid); 871 amdgpu_userq_fence_driver_free(queue); 872 uq_funcs->mqd_destroy(queue); 873 kfree(queue); 874 goto unlock; 875 } 876 } 877 878 queue_name = kasprintf(GFP_KERNEL, "queue-%d", qid); 879 if (!queue_name) { 880 r = -ENOMEM; 881 goto unlock; 882 } 883 884 #if defined(CONFIG_DEBUG_FS) 885 /* Queue dentry per client to hold MQD information */ 886 queue->debugfs_queue = debugfs_create_dir(queue_name, filp->debugfs_client); 887 debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); 888 #endif 889 amdgpu_userq_init_hang_detect_work(queue); 890 kfree(queue_name); 891 892 args->out.queue_id = qid; 893 atomic_inc(&uq_mgr->userq_count[queue->queue_type]); 894 895 unlock: 896 mutex_unlock(&uq_mgr->userq_mutex); 897 898 return r; 899 } 900 901 static int amdgpu_userq_input_args_validate(struct drm_device *dev, 902 union drm_amdgpu_userq *args, 903 struct drm_file *filp) 904 { 905 struct amdgpu_device *adev = drm_to_adev(dev); 906 907 switch (args->in.op) { 908 case AMDGPU_USERQ_OP_CREATE: 909 if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK | 910 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE)) 911 return -EINVAL; 912 /* Usermode queues are only supported for GFX IP as of now */ 913 if (args->in.ip_type != AMDGPU_HW_IP_GFX && 914 args->in.ip_type != AMDGPU_HW_IP_DMA && 915 args->in.ip_type != AMDGPU_HW_IP_COMPUTE) { 916 drm_file_err(filp, "Usermode queue doesn't support IP type %u\n", 917 args->in.ip_type); 918 return -EINVAL; 919 } 920 921 if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) && 922 (args->in.ip_type != AMDGPU_HW_IP_GFX) && 923 (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) && 924 !amdgpu_is_tmz(adev)) { 925 drm_file_err(filp, "Secure only supported on GFX/Compute queues\n"); 926 return -EINVAL; 927 } 928 929 if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET || 930 args->in.queue_va == 0 || 931 args->in.queue_size == 0) { 932 drm_file_err(filp, "invalidate userq queue va or size\n"); 933 return -EINVAL; 934 } 935 if (!args->in.wptr_va || !args->in.rptr_va) { 936 drm_file_err(filp, "invalidate userq queue rptr or wptr\n"); 937 return -EINVAL; 938 } 939 break; 940 case AMDGPU_USERQ_OP_FREE: 941 if (args->in.ip_type || 942 args->in.doorbell_handle || 943 args->in.doorbell_offset || 944 args->in.flags || 945 args->in.queue_va || 946 args->in.queue_size || 947 args->in.rptr_va || 948 args->in.wptr_va || 949 args->in.mqd || 950 args->in.mqd_size) 951 return -EINVAL; 952 break; 953 default: 954 return -EINVAL; 955 } 956 957 return 0; 958 } 959 960 bool amdgpu_userq_enabled(struct drm_device *dev) 961 { 962 struct amdgpu_device *adev = drm_to_adev(dev); 963 int i; 964 965 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) { 966 if (adev->userq_funcs[i]) 967 return true; 968 } 969 970 return false; 971 } 972 973 int amdgpu_userq_ioctl(struct drm_device *dev, void *data, 974 struct drm_file *filp) 975 { 976 union drm_amdgpu_userq *args = data; 977 int r; 978 979 if (!amdgpu_userq_enabled(dev)) 980 return -ENOTSUPP; 981 982 if (amdgpu_userq_input_args_validate(dev, args, filp) < 0) 983 return -EINVAL; 984 985 switch (args->in.op) { 986 case AMDGPU_USERQ_OP_CREATE: 987 r = amdgpu_userq_create(filp, args); 988 if (r) 989 drm_file_err(filp, "Failed to create usermode queue\n"); 990 break; 991 992 case AMDGPU_USERQ_OP_FREE: 993 r = amdgpu_userq_destroy(filp, args->in.queue_id); 994 if (r) 995 drm_file_err(filp, "Failed to destroy usermode queue\n"); 996 break; 997 998 default: 999 drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op); 1000 return -EINVAL; 1001 } 1002 1003 return r; 1004 } 1005 1006 static int 1007 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) 1008 { 1009 struct amdgpu_usermode_queue *queue; 1010 unsigned long queue_id; 1011 int ret = 0, r; 1012 1013 /* Resume all the queues for this process */ 1014 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 1015 if (!amdgpu_userq_buffer_vas_mapped(queue)) { 1016 drm_file_err(uq_mgr->file, 1017 "trying restore queue without va mapping\n"); 1018 queue->state = AMDGPU_USERQ_STATE_INVALID_VA; 1019 continue; 1020 } 1021 1022 r = amdgpu_userq_restore_helper(queue); 1023 if (r) 1024 ret = r; 1025 } 1026 1027 if (ret) 1028 drm_file_err(uq_mgr->file, "Failed to map all the queues\n"); 1029 return ret; 1030 } 1031 1032 static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo) 1033 { 1034 struct ttm_operation_ctx ctx = { false, false }; 1035 1036 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 1037 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1038 } 1039 1040 /* Handle all BOs on the invalidated list, validate them and update the PTs */ 1041 static int 1042 amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec, 1043 struct amdgpu_vm *vm) 1044 { 1045 struct ttm_operation_ctx ctx = { false, false }; 1046 struct amdgpu_bo_va *bo_va; 1047 struct amdgpu_bo *bo; 1048 int ret; 1049 1050 spin_lock(&vm->status_lock); 1051 while (!list_empty(&vm->invalidated)) { 1052 bo_va = list_first_entry(&vm->invalidated, 1053 struct amdgpu_bo_va, 1054 base.vm_status); 1055 spin_unlock(&vm->status_lock); 1056 1057 bo = bo_va->base.bo; 1058 ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 2); 1059 if (unlikely(ret)) 1060 return ret; 1061 1062 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 1063 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1064 if (ret) 1065 return ret; 1066 1067 /* This moves the bo_va to the done list */ 1068 ret = amdgpu_vm_bo_update(adev, bo_va, false); 1069 if (ret) 1070 return ret; 1071 1072 spin_lock(&vm->status_lock); 1073 } 1074 spin_unlock(&vm->status_lock); 1075 1076 return 0; 1077 } 1078 1079 /* Make sure the whole VM is ready to be used */ 1080 static int 1081 amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) 1082 { 1083 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 1084 bool invalidated = false, new_addition = false; 1085 struct ttm_operation_ctx ctx = { true, false }; 1086 struct amdgpu_device *adev = uq_mgr->adev; 1087 struct amdgpu_hmm_range *range; 1088 struct amdgpu_vm *vm = &fpriv->vm; 1089 unsigned long key, tmp_key; 1090 struct amdgpu_bo_va *bo_va; 1091 struct amdgpu_bo *bo; 1092 struct drm_exec exec; 1093 struct xarray xa; 1094 int ret; 1095 1096 xa_init(&xa); 1097 1098 retry_lock: 1099 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 1100 drm_exec_until_all_locked(&exec) { 1101 ret = amdgpu_vm_lock_pd(vm, &exec, 1); 1102 drm_exec_retry_on_contention(&exec); 1103 if (unlikely(ret)) 1104 goto unlock_all; 1105 1106 ret = amdgpu_vm_lock_done_list(vm, &exec, 1); 1107 drm_exec_retry_on_contention(&exec); 1108 if (unlikely(ret)) 1109 goto unlock_all; 1110 1111 /* This validates PDs, PTs and per VM BOs */ 1112 ret = amdgpu_vm_validate(adev, vm, NULL, 1113 amdgpu_userq_validate_vm, 1114 NULL); 1115 if (unlikely(ret)) 1116 goto unlock_all; 1117 1118 /* This locks and validates the remaining evicted BOs */ 1119 ret = amdgpu_userq_bo_validate(adev, &exec, vm); 1120 drm_exec_retry_on_contention(&exec); 1121 if (unlikely(ret)) 1122 goto unlock_all; 1123 } 1124 1125 if (invalidated) { 1126 xa_for_each(&xa, tmp_key, range) { 1127 bo = range->bo; 1128 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU); 1129 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1130 if (ret) 1131 goto unlock_all; 1132 1133 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range); 1134 1135 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); 1136 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1137 if (ret) 1138 goto unlock_all; 1139 } 1140 invalidated = false; 1141 } 1142 1143 ret = amdgpu_vm_handle_moved(adev, vm, NULL); 1144 if (ret) 1145 goto unlock_all; 1146 1147 key = 0; 1148 /* Validate User Ptr BOs */ 1149 list_for_each_entry(bo_va, &vm->done, base.vm_status) { 1150 bo = bo_va->base.bo; 1151 if (!bo) 1152 continue; 1153 1154 if (!amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) 1155 continue; 1156 1157 range = xa_load(&xa, key); 1158 if (range && range->bo != bo) { 1159 xa_erase(&xa, key); 1160 amdgpu_hmm_range_free(range); 1161 range = NULL; 1162 } 1163 1164 if (!range) { 1165 range = amdgpu_hmm_range_alloc(bo); 1166 if (!range) { 1167 ret = -ENOMEM; 1168 goto unlock_all; 1169 } 1170 1171 xa_store(&xa, key, range, GFP_KERNEL); 1172 new_addition = true; 1173 } 1174 key++; 1175 } 1176 1177 if (new_addition) { 1178 drm_exec_fini(&exec); 1179 xa_for_each(&xa, tmp_key, range) { 1180 if (!range) 1181 continue; 1182 bo = range->bo; 1183 ret = amdgpu_ttm_tt_get_user_pages(bo, range); 1184 if (ret) 1185 goto unlock_all; 1186 } 1187 1188 invalidated = true; 1189 new_addition = false; 1190 goto retry_lock; 1191 } 1192 1193 ret = amdgpu_vm_update_pdes(adev, vm, false); 1194 if (ret) 1195 goto unlock_all; 1196 1197 /* 1198 * We need to wait for all VM updates to finish before restarting the 1199 * queues. Using the done list like that is now ok since everything is 1200 * locked in place. 1201 */ 1202 list_for_each_entry(bo_va, &vm->done, base.vm_status) 1203 dma_fence_wait(bo_va->last_pt_update, false); 1204 dma_fence_wait(vm->last_update, false); 1205 1206 ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec); 1207 if (ret) 1208 drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n"); 1209 1210 unlock_all: 1211 drm_exec_fini(&exec); 1212 xa_for_each(&xa, tmp_key, range) { 1213 if (!range) 1214 continue; 1215 bo = range->bo; 1216 amdgpu_hmm_range_free(range); 1217 } 1218 xa_destroy(&xa); 1219 return ret; 1220 } 1221 1222 static void amdgpu_userq_restore_worker(struct work_struct *work) 1223 { 1224 struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work); 1225 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 1226 int ret; 1227 1228 flush_delayed_work(&fpriv->evf_mgr.suspend_work); 1229 1230 mutex_lock(&uq_mgr->userq_mutex); 1231 1232 ret = amdgpu_userq_vm_validate(uq_mgr); 1233 if (ret) { 1234 drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n"); 1235 goto unlock; 1236 } 1237 1238 ret = amdgpu_userq_restore_all(uq_mgr); 1239 if (ret) { 1240 drm_file_err(uq_mgr->file, "Failed to restore all queues\n"); 1241 goto unlock; 1242 } 1243 1244 unlock: 1245 mutex_unlock(&uq_mgr->userq_mutex); 1246 } 1247 1248 static int 1249 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) 1250 { 1251 struct amdgpu_usermode_queue *queue; 1252 unsigned long queue_id; 1253 int ret = 0, r; 1254 1255 amdgpu_userq_detect_and_reset_queues(uq_mgr); 1256 /* Try to unmap all the queues in this process ctx */ 1257 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 1258 r = amdgpu_userq_preempt_helper(queue); 1259 if (r) 1260 ret = r; 1261 } 1262 1263 if (ret) 1264 drm_file_err(uq_mgr->file, "Couldn't unmap all the queues\n"); 1265 return ret; 1266 } 1267 1268 void amdgpu_userq_reset_work(struct work_struct *work) 1269 { 1270 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 1271 userq_reset_work); 1272 struct amdgpu_reset_context reset_context; 1273 1274 memset(&reset_context, 0, sizeof(reset_context)); 1275 1276 reset_context.method = AMD_RESET_METHOD_NONE; 1277 reset_context.reset_req_dev = adev; 1278 reset_context.src = AMDGPU_RESET_SRC_USERQ; 1279 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 1280 /*set_bit(AMDGPU_SKIP_COREDUMP, &reset_context.flags);*/ 1281 1282 amdgpu_device_gpu_recover(adev, NULL, &reset_context); 1283 } 1284 1285 static int 1286 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) 1287 { 1288 struct amdgpu_usermode_queue *queue; 1289 unsigned long queue_id; 1290 int ret; 1291 1292 xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { 1293 struct dma_fence *f = queue->last_fence; 1294 1295 if (!f || dma_fence_is_signaled(f)) 1296 continue; 1297 ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100)); 1298 if (ret <= 0) { 1299 drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n", 1300 f->context, f->seqno); 1301 return -ETIMEDOUT; 1302 } 1303 } 1304 1305 return 0; 1306 } 1307 1308 void 1309 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr, 1310 struct amdgpu_eviction_fence *ev_fence) 1311 { 1312 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 1313 struct amdgpu_eviction_fence_mgr *evf_mgr = &fpriv->evf_mgr; 1314 struct amdgpu_device *adev = uq_mgr->adev; 1315 int ret; 1316 1317 /* Wait for any pending userqueue fence work to finish */ 1318 ret = amdgpu_userq_wait_for_signal(uq_mgr); 1319 if (ret) 1320 dev_err(adev->dev, "Not evicting userqueue, timeout waiting for work\n"); 1321 1322 ret = amdgpu_userq_evict_all(uq_mgr); 1323 if (ret) 1324 dev_err(adev->dev, "Failed to evict userqueue\n"); 1325 1326 /* Signal current eviction fence */ 1327 amdgpu_eviction_fence_signal(evf_mgr, ev_fence); 1328 1329 if (evf_mgr->fd_closing) { 1330 cancel_delayed_work_sync(&uq_mgr->resume_work); 1331 return; 1332 } 1333 1334 /* Schedule a resume work */ 1335 schedule_delayed_work(&uq_mgr->resume_work, 0); 1336 } 1337 1338 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv, 1339 struct amdgpu_device *adev) 1340 { 1341 mutex_init(&userq_mgr->userq_mutex); 1342 xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC); 1343 userq_mgr->adev = adev; 1344 userq_mgr->file = file_priv; 1345 1346 INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker); 1347 return 0; 1348 } 1349 1350 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr) 1351 { 1352 struct amdgpu_usermode_queue *queue; 1353 unsigned long queue_id; 1354 1355 cancel_delayed_work_sync(&userq_mgr->resume_work); 1356 1357 mutex_lock(&userq_mgr->userq_mutex); 1358 amdgpu_userq_detect_and_reset_queues(userq_mgr); 1359 xa_for_each(&userq_mgr->userq_xa, queue_id, queue) { 1360 amdgpu_userq_wait_for_last_fence(queue); 1361 amdgpu_userq_unmap_helper(queue); 1362 amdgpu_userq_cleanup(queue, queue_id); 1363 } 1364 1365 xa_destroy(&userq_mgr->userq_xa); 1366 mutex_unlock(&userq_mgr->userq_mutex); 1367 mutex_destroy(&userq_mgr->userq_mutex); 1368 } 1369 1370 int amdgpu_userq_suspend(struct amdgpu_device *adev) 1371 { 1372 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1373 struct amdgpu_usermode_queue *queue; 1374 struct amdgpu_userq_mgr *uqm; 1375 unsigned long queue_id; 1376 int r; 1377 1378 if (!ip_mask) 1379 return 0; 1380 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 guard(mutex)(&uqm->userq_mutex); 1385 amdgpu_userq_detect_and_reset_queues(uqm); 1386 if (adev->in_s0ix) 1387 r = amdgpu_userq_preempt_helper(queue); 1388 else 1389 r = amdgpu_userq_unmap_helper(queue); 1390 if (r) 1391 return r; 1392 } 1393 return 0; 1394 } 1395 1396 int amdgpu_userq_resume(struct amdgpu_device *adev) 1397 { 1398 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1399 struct amdgpu_usermode_queue *queue; 1400 struct amdgpu_userq_mgr *uqm; 1401 unsigned long queue_id; 1402 int r; 1403 1404 if (!ip_mask) 1405 return 0; 1406 1407 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1408 uqm = queue->userq_mgr; 1409 guard(mutex)(&uqm->userq_mutex); 1410 if (adev->in_s0ix) 1411 r = amdgpu_userq_restore_helper(queue); 1412 else 1413 r = amdgpu_userq_map_helper(queue); 1414 if (r) 1415 return r; 1416 } 1417 1418 return 0; 1419 } 1420 1421 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, 1422 u32 idx) 1423 { 1424 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1425 struct amdgpu_usermode_queue *queue; 1426 struct amdgpu_userq_mgr *uqm; 1427 unsigned long queue_id; 1428 int ret = 0, r; 1429 1430 /* only need to stop gfx/compute */ 1431 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 1432 return 0; 1433 1434 if (adev->userq_halt_for_enforce_isolation) 1435 dev_warn(adev->dev, "userq scheduling already stopped!\n"); 1436 adev->userq_halt_for_enforce_isolation = true; 1437 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1438 uqm = queue->userq_mgr; 1439 cancel_delayed_work_sync(&uqm->resume_work); 1440 mutex_lock(&uqm->userq_mutex); 1441 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 1442 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 1443 (queue->xcp_id == idx)) { 1444 amdgpu_userq_detect_and_reset_queues(uqm); 1445 r = amdgpu_userq_preempt_helper(queue); 1446 if (r) 1447 ret = r; 1448 } 1449 mutex_unlock(&uqm->userq_mutex); 1450 } 1451 1452 return ret; 1453 } 1454 1455 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, 1456 u32 idx) 1457 { 1458 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1459 struct amdgpu_usermode_queue *queue; 1460 struct amdgpu_userq_mgr *uqm; 1461 unsigned long queue_id; 1462 int ret = 0, r; 1463 1464 /* only need to stop gfx/compute */ 1465 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 1466 return 0; 1467 1468 if (!adev->userq_halt_for_enforce_isolation) 1469 dev_warn(adev->dev, "userq scheduling already started!\n"); 1470 adev->userq_halt_for_enforce_isolation = false; 1471 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1472 uqm = queue->userq_mgr; 1473 mutex_lock(&uqm->userq_mutex); 1474 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 1475 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 1476 (queue->xcp_id == idx)) { 1477 r = amdgpu_userq_restore_helper(queue); 1478 if (r) 1479 ret = r; 1480 } 1481 mutex_unlock(&uqm->userq_mutex); 1482 } 1483 1484 return ret; 1485 } 1486 1487 int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev, 1488 struct amdgpu_bo_va_mapping *mapping, 1489 uint64_t saddr) 1490 { 1491 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1492 struct amdgpu_bo_va *bo_va = mapping->bo_va; 1493 struct dma_resv *resv = bo_va->base.bo->tbo.base.resv; 1494 int ret = 0; 1495 1496 if (!ip_mask) 1497 return 0; 1498 1499 dev_warn_once(adev->dev, "now unmapping a vital queue va:%llx\n", saddr); 1500 /** 1501 * The userq VA mapping reservation should include the eviction fence, 1502 * if the eviction fence can't signal successfully during unmapping, 1503 * then driver will warn to flag this improper unmap of the userq VA. 1504 * Note: The eviction fence may be attached to different BOs, and this 1505 * unmap is only for one kind of userq VAs, so at this point suppose 1506 * the eviction fence is always unsignaled. 1507 */ 1508 if (!dma_resv_test_signaled(resv, DMA_RESV_USAGE_BOOKKEEP)) { 1509 ret = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP, true, 1510 MAX_SCHEDULE_TIMEOUT); 1511 if (ret <= 0) 1512 return -EBUSY; 1513 } 1514 1515 return 0; 1516 } 1517 1518 void amdgpu_userq_pre_reset(struct amdgpu_device *adev) 1519 { 1520 const struct amdgpu_userq_funcs *userq_funcs; 1521 struct amdgpu_usermode_queue *queue; 1522 struct amdgpu_userq_mgr *uqm; 1523 unsigned long queue_id; 1524 1525 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1526 uqm = queue->userq_mgr; 1527 cancel_delayed_work_sync(&uqm->resume_work); 1528 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) { 1529 amdgpu_userq_wait_for_last_fence(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 queue->state = AMDGPU_USERQ_STATE_HUNG; 1537 amdgpu_userq_fence_driver_force_completion(queue); 1538 } 1539 } 1540 } 1541 1542 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost) 1543 { 1544 /* if any queue state is AMDGPU_USERQ_STATE_UNMAPPED 1545 * at this point, we should be able to map it again 1546 * and continue if vram is not lost. 1547 */ 1548 struct amdgpu_usermode_queue *queue; 1549 const struct amdgpu_userq_funcs *userq_funcs; 1550 unsigned long queue_id; 1551 int r = 0; 1552 1553 xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { 1554 if (queue->state == AMDGPU_USERQ_STATE_HUNG && !vram_lost) { 1555 userq_funcs = adev->userq_funcs[queue->queue_type]; 1556 /* Re-map queue */ 1557 r = userq_funcs->map(queue); 1558 if (r) { 1559 dev_err(adev->dev, "Failed to remap queue %ld\n", queue_id); 1560 continue; 1561 } 1562 queue->state = AMDGPU_USERQ_STATE_MAPPED; 1563 } 1564 } 1565 1566 return r; 1567 } 1568