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