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