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_userq_fence.h" 33 34 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev) 35 { 36 int i; 37 u32 userq_ip_mask = 0; 38 39 for (i = 0; i < AMDGPU_HW_IP_NUM; i++) { 40 if (adev->userq_funcs[i]) 41 userq_ip_mask |= (1 << i); 42 } 43 44 return userq_ip_mask; 45 } 46 47 int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr, 48 u64 expected_size) 49 { 50 struct amdgpu_bo_va_mapping *va_map; 51 u64 user_addr; 52 u64 size; 53 int r = 0; 54 55 user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT; 56 size = expected_size >> AMDGPU_GPU_PAGE_SHIFT; 57 58 r = amdgpu_bo_reserve(vm->root.bo, false); 59 if (r) 60 return r; 61 62 va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr); 63 if (!va_map) { 64 r = -EINVAL; 65 goto out_err; 66 } 67 /* Only validate the userq whether resident in the VM mapping range */ 68 if (user_addr >= va_map->start && 69 va_map->last - user_addr + 1 >= size) { 70 amdgpu_bo_unreserve(vm->root.bo); 71 return 0; 72 } 73 74 out_err: 75 amdgpu_bo_unreserve(vm->root.bo); 76 return r; 77 } 78 79 static int 80 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr, 81 struct amdgpu_usermode_queue *queue) 82 { 83 struct amdgpu_device *adev = uq_mgr->adev; 84 const struct amdgpu_userq_funcs *userq_funcs = 85 adev->userq_funcs[queue->queue_type]; 86 int r = 0; 87 88 if (queue->state == AMDGPU_USERQ_STATE_MAPPED) { 89 r = userq_funcs->unmap(uq_mgr, queue); 90 if (r) 91 queue->state = AMDGPU_USERQ_STATE_HUNG; 92 else 93 queue->state = AMDGPU_USERQ_STATE_UNMAPPED; 94 } 95 return r; 96 } 97 98 static int 99 amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr, 100 struct amdgpu_usermode_queue *queue) 101 { 102 struct amdgpu_device *adev = uq_mgr->adev; 103 const struct amdgpu_userq_funcs *userq_funcs = 104 adev->userq_funcs[queue->queue_type]; 105 int r = 0; 106 107 if (queue->state == AMDGPU_USERQ_STATE_UNMAPPED) { 108 r = userq_funcs->map(uq_mgr, queue); 109 if (r) { 110 queue->state = AMDGPU_USERQ_STATE_HUNG; 111 } else { 112 queue->state = AMDGPU_USERQ_STATE_MAPPED; 113 } 114 } 115 return r; 116 } 117 118 static void 119 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr, 120 struct amdgpu_usermode_queue *queue) 121 { 122 struct dma_fence *f = queue->last_fence; 123 int ret; 124 125 if (f && !dma_fence_is_signaled(f)) { 126 ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100)); 127 if (ret <= 0) 128 drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n", 129 f->context, f->seqno); 130 } 131 } 132 133 static void 134 amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr, 135 struct amdgpu_usermode_queue *queue, 136 int queue_id) 137 { 138 struct amdgpu_device *adev = uq_mgr->adev; 139 const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; 140 141 uq_funcs->mqd_destroy(uq_mgr, queue); 142 amdgpu_userq_fence_driver_free(queue); 143 idr_remove(&uq_mgr->userq_idr, queue_id); 144 kfree(queue); 145 } 146 147 static struct amdgpu_usermode_queue * 148 amdgpu_userq_find(struct amdgpu_userq_mgr *uq_mgr, int qid) 149 { 150 return idr_find(&uq_mgr->userq_idr, qid); 151 } 152 153 void 154 amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr, 155 struct amdgpu_eviction_fence_mgr *evf_mgr) 156 { 157 struct amdgpu_eviction_fence *ev_fence; 158 159 retry: 160 /* Flush any pending resume work to create ev_fence */ 161 flush_delayed_work(&uq_mgr->resume_work); 162 163 mutex_lock(&uq_mgr->userq_mutex); 164 spin_lock(&evf_mgr->ev_fence_lock); 165 ev_fence = evf_mgr->ev_fence; 166 spin_unlock(&evf_mgr->ev_fence_lock); 167 if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) { 168 mutex_unlock(&uq_mgr->userq_mutex); 169 /* 170 * Looks like there was no pending resume work, 171 * add one now to create a valid eviction fence 172 */ 173 schedule_delayed_work(&uq_mgr->resume_work, 0); 174 goto retry; 175 } 176 } 177 178 int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr, 179 struct amdgpu_userq_obj *userq_obj, 180 int size) 181 { 182 struct amdgpu_device *adev = uq_mgr->adev; 183 struct amdgpu_bo_param bp; 184 int r; 185 186 memset(&bp, 0, sizeof(bp)); 187 bp.byte_align = PAGE_SIZE; 188 bp.domain = AMDGPU_GEM_DOMAIN_GTT; 189 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | 190 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 191 bp.type = ttm_bo_type_kernel; 192 bp.size = size; 193 bp.resv = NULL; 194 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 195 196 r = amdgpu_bo_create(adev, &bp, &userq_obj->obj); 197 if (r) { 198 drm_file_err(uq_mgr->file, "Failed to allocate BO for userqueue (%d)", r); 199 return r; 200 } 201 202 r = amdgpu_bo_reserve(userq_obj->obj, true); 203 if (r) { 204 drm_file_err(uq_mgr->file, "Failed to reserve BO to map (%d)", r); 205 goto free_obj; 206 } 207 208 r = amdgpu_ttm_alloc_gart(&(userq_obj->obj)->tbo); 209 if (r) { 210 drm_file_err(uq_mgr->file, "Failed to alloc GART for userqueue object (%d)", r); 211 goto unresv; 212 } 213 214 r = amdgpu_bo_kmap(userq_obj->obj, &userq_obj->cpu_ptr); 215 if (r) { 216 drm_file_err(uq_mgr->file, "Failed to map BO for userqueue (%d)", r); 217 goto unresv; 218 } 219 220 userq_obj->gpu_addr = amdgpu_bo_gpu_offset(userq_obj->obj); 221 amdgpu_bo_unreserve(userq_obj->obj); 222 memset(userq_obj->cpu_ptr, 0, size); 223 return 0; 224 225 unresv: 226 amdgpu_bo_unreserve(userq_obj->obj); 227 228 free_obj: 229 amdgpu_bo_unref(&userq_obj->obj); 230 return r; 231 } 232 233 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr, 234 struct amdgpu_userq_obj *userq_obj) 235 { 236 amdgpu_bo_kunmap(userq_obj->obj); 237 amdgpu_bo_unref(&userq_obj->obj); 238 } 239 240 uint64_t 241 amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, 242 struct amdgpu_db_info *db_info, 243 struct drm_file *filp) 244 { 245 uint64_t index; 246 struct drm_gem_object *gobj; 247 struct amdgpu_userq_obj *db_obj = db_info->db_obj; 248 int r, db_size; 249 250 gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle); 251 if (gobj == NULL) { 252 drm_file_err(uq_mgr->file, "Can't find GEM object for doorbell\n"); 253 return -EINVAL; 254 } 255 256 db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj)); 257 drm_gem_object_put(gobj); 258 259 r = amdgpu_bo_reserve(db_obj->obj, true); 260 if (r) { 261 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 262 goto unref_bo; 263 } 264 265 /* Pin the BO before generating the index, unpin in queue destroy */ 266 r = amdgpu_bo_pin(db_obj->obj, AMDGPU_GEM_DOMAIN_DOORBELL); 267 if (r) { 268 drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin doorbell object\n"); 269 goto unresv_bo; 270 } 271 272 switch (db_info->queue_type) { 273 case AMDGPU_HW_IP_GFX: 274 case AMDGPU_HW_IP_COMPUTE: 275 case AMDGPU_HW_IP_DMA: 276 db_size = sizeof(u64); 277 break; 278 279 case AMDGPU_HW_IP_VCN_ENC: 280 db_size = sizeof(u32); 281 db_info->doorbell_offset += AMDGPU_NAVI10_DOORBELL64_VCN0_1 << 1; 282 break; 283 284 case AMDGPU_HW_IP_VPE: 285 db_size = sizeof(u32); 286 db_info->doorbell_offset += AMDGPU_NAVI10_DOORBELL64_VPE << 1; 287 break; 288 289 default: 290 drm_file_err(uq_mgr->file, "[Usermode queues] IP %d not support\n", 291 db_info->queue_type); 292 r = -EINVAL; 293 goto unpin_bo; 294 } 295 296 index = amdgpu_doorbell_index_on_bar(uq_mgr->adev, db_obj->obj, 297 db_info->doorbell_offset, db_size); 298 drm_dbg_driver(adev_to_drm(uq_mgr->adev), 299 "[Usermode queues] doorbell index=%lld\n", index); 300 amdgpu_bo_unreserve(db_obj->obj); 301 return index; 302 303 unpin_bo: 304 amdgpu_bo_unpin(db_obj->obj); 305 unresv_bo: 306 amdgpu_bo_unreserve(db_obj->obj); 307 unref_bo: 308 amdgpu_bo_unref(&db_obj->obj); 309 return r; 310 } 311 312 static int 313 amdgpu_userq_destroy(struct drm_file *filp, int queue_id) 314 { 315 struct amdgpu_fpriv *fpriv = filp->driver_priv; 316 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 317 struct amdgpu_device *adev = uq_mgr->adev; 318 struct amdgpu_usermode_queue *queue; 319 int r = 0; 320 321 cancel_delayed_work_sync(&uq_mgr->resume_work); 322 mutex_lock(&uq_mgr->userq_mutex); 323 324 queue = amdgpu_userq_find(uq_mgr, queue_id); 325 if (!queue) { 326 drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n"); 327 mutex_unlock(&uq_mgr->userq_mutex); 328 return -EINVAL; 329 } 330 amdgpu_userq_wait_for_last_fence(uq_mgr, queue); 331 r = amdgpu_bo_reserve(queue->db_obj.obj, true); 332 if (!r) { 333 amdgpu_bo_unpin(queue->db_obj.obj); 334 amdgpu_bo_unreserve(queue->db_obj.obj); 335 } 336 amdgpu_bo_unref(&queue->db_obj.obj); 337 338 #if defined(CONFIG_DEBUG_FS) 339 debugfs_remove_recursive(queue->debugfs_queue); 340 #endif 341 r = amdgpu_userq_unmap_helper(uq_mgr, queue); 342 /*TODO: It requires a reset for userq hw unmap error*/ 343 if (unlikely(r != AMDGPU_USERQ_STATE_UNMAPPED)) { 344 drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n"); 345 queue->state = AMDGPU_USERQ_STATE_HUNG; 346 } 347 amdgpu_userq_cleanup(uq_mgr, queue, queue_id); 348 mutex_unlock(&uq_mgr->userq_mutex); 349 350 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 351 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 352 353 return r; 354 } 355 356 static int amdgpu_userq_priority_permit(struct drm_file *filp, 357 int priority) 358 { 359 if (priority < AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_HIGH) 360 return 0; 361 362 if (capable(CAP_SYS_NICE)) 363 return 0; 364 365 if (drm_is_current_master(filp)) 366 return 0; 367 368 return -EACCES; 369 } 370 371 #if defined(CONFIG_DEBUG_FS) 372 static int amdgpu_mqd_info_read(struct seq_file *m, void *unused) 373 { 374 struct amdgpu_usermode_queue *queue = m->private; 375 struct amdgpu_bo *bo; 376 int r; 377 378 if (!queue || !queue->mqd.obj) 379 return -EINVAL; 380 381 bo = amdgpu_bo_ref(queue->mqd.obj); 382 r = amdgpu_bo_reserve(bo, true); 383 if (r) { 384 amdgpu_bo_unref(&bo); 385 return -EINVAL; 386 } 387 388 seq_printf(m, "queue_type: %d\n", queue->queue_type); 389 seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj)); 390 391 amdgpu_bo_unreserve(bo); 392 amdgpu_bo_unref(&bo); 393 394 return 0; 395 } 396 397 static int amdgpu_mqd_info_open(struct inode *inode, struct file *file) 398 { 399 return single_open(file, amdgpu_mqd_info_read, inode->i_private); 400 } 401 402 static const struct file_operations amdgpu_mqd_info_fops = { 403 .owner = THIS_MODULE, 404 .open = amdgpu_mqd_info_open, 405 .read = seq_read, 406 .llseek = seq_lseek, 407 .release = single_release, 408 }; 409 #endif 410 411 static int 412 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) 413 { 414 struct amdgpu_fpriv *fpriv = filp->driver_priv; 415 struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; 416 struct amdgpu_device *adev = uq_mgr->adev; 417 const struct amdgpu_userq_funcs *uq_funcs; 418 struct amdgpu_usermode_queue *queue; 419 struct amdgpu_db_info db_info; 420 char *queue_name; 421 bool skip_map_queue; 422 uint64_t index; 423 int qid, r = 0; 424 int priority = 425 (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >> 426 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT; 427 428 r = amdgpu_userq_priority_permit(filp, priority); 429 if (r) 430 return r; 431 432 r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 433 if (r < 0) { 434 drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n"); 435 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 436 return r; 437 } 438 439 /* 440 * There could be a situation that we are creating a new queue while 441 * the other queues under this UQ_mgr are suspended. So if there is any 442 * resume work pending, wait for it to get done. 443 * 444 * This will also make sure we have a valid eviction fence ready to be used. 445 */ 446 mutex_lock(&adev->userq_mutex); 447 amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr); 448 449 uq_funcs = adev->userq_funcs[args->in.ip_type]; 450 if (!uq_funcs) { 451 drm_file_err(uq_mgr->file, "Usermode queue is not supported for this IP (%u)\n", 452 args->in.ip_type); 453 r = -EINVAL; 454 goto unlock; 455 } 456 457 queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL); 458 if (!queue) { 459 drm_file_err(uq_mgr->file, "Failed to allocate memory for queue\n"); 460 r = -ENOMEM; 461 goto unlock; 462 } 463 464 /* Validate the userq virtual address.*/ 465 if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, args->in.queue_size) || 466 amdgpu_userq_input_va_validate(&fpriv->vm, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE) || 467 amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) { 468 kfree(queue); 469 goto unlock; 470 } 471 queue->doorbell_handle = args->in.doorbell_handle; 472 queue->queue_type = args->in.ip_type; 473 queue->vm = &fpriv->vm; 474 queue->priority = priority; 475 476 db_info.queue_type = queue->queue_type; 477 db_info.doorbell_handle = queue->doorbell_handle; 478 db_info.db_obj = &queue->db_obj; 479 db_info.doorbell_offset = args->in.doorbell_offset; 480 481 /* Convert relative doorbell offset into absolute doorbell index */ 482 index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp); 483 if (index == (uint64_t)-EINVAL) { 484 drm_file_err(uq_mgr->file, "Failed to get doorbell for queue\n"); 485 kfree(queue); 486 r = -EINVAL; 487 goto unlock; 488 } 489 490 queue->doorbell_index = index; 491 xa_init_flags(&queue->fence_drv_xa, XA_FLAGS_ALLOC); 492 r = amdgpu_userq_fence_driver_alloc(adev, queue); 493 if (r) { 494 drm_file_err(uq_mgr->file, "Failed to alloc fence driver\n"); 495 goto unlock; 496 } 497 498 r = uq_funcs->mqd_create(uq_mgr, &args->in, queue); 499 if (r) { 500 drm_file_err(uq_mgr->file, "Failed to create Queue\n"); 501 amdgpu_userq_fence_driver_free(queue); 502 kfree(queue); 503 goto unlock; 504 } 505 506 507 qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL); 508 if (qid < 0) { 509 drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n"); 510 amdgpu_userq_fence_driver_free(queue); 511 uq_funcs->mqd_destroy(uq_mgr, queue); 512 kfree(queue); 513 r = -ENOMEM; 514 goto unlock; 515 } 516 517 /* don't map the queue if scheduling is halted */ 518 if (adev->userq_halt_for_enforce_isolation && 519 ((queue->queue_type == AMDGPU_HW_IP_GFX) || 520 (queue->queue_type == AMDGPU_HW_IP_COMPUTE))) 521 skip_map_queue = true; 522 else 523 skip_map_queue = false; 524 if (!skip_map_queue) { 525 r = amdgpu_userq_map_helper(uq_mgr, queue); 526 if (r) { 527 drm_file_err(uq_mgr->file, "Failed to map Queue\n"); 528 idr_remove(&uq_mgr->userq_idr, qid); 529 amdgpu_userq_fence_driver_free(queue); 530 uq_funcs->mqd_destroy(uq_mgr, queue); 531 kfree(queue); 532 goto unlock; 533 } 534 } 535 536 queue_name = kasprintf(GFP_KERNEL, "queue-%d", qid); 537 if (!queue_name) { 538 r = -ENOMEM; 539 goto unlock; 540 } 541 542 #if defined(CONFIG_DEBUG_FS) 543 /* Queue dentry per client to hold MQD information */ 544 queue->debugfs_queue = debugfs_create_dir(queue_name, filp->debugfs_client); 545 debugfs_create_file("mqd_info", 0444, queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); 546 #endif 547 kfree(queue_name); 548 549 args->out.queue_id = qid; 550 551 unlock: 552 mutex_unlock(&uq_mgr->userq_mutex); 553 mutex_unlock(&adev->userq_mutex); 554 555 return r; 556 } 557 558 static int amdgpu_userq_input_args_validate(struct drm_device *dev, 559 union drm_amdgpu_userq *args, 560 struct drm_file *filp) 561 { 562 struct amdgpu_device *adev = drm_to_adev(dev); 563 564 switch (args->in.op) { 565 case AMDGPU_USERQ_OP_CREATE: 566 if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK | 567 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE)) 568 return -EINVAL; 569 /* Usermode queues are only supported for GFX IP as of now */ 570 if (args->in.ip_type != AMDGPU_HW_IP_GFX && 571 args->in.ip_type != AMDGPU_HW_IP_DMA && 572 args->in.ip_type != AMDGPU_HW_IP_COMPUTE) { 573 drm_file_err(filp, "Usermode queue doesn't support IP type %u\n", 574 args->in.ip_type); 575 return -EINVAL; 576 } 577 578 if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) && 579 (args->in.ip_type != AMDGPU_HW_IP_GFX) && 580 (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) && 581 !amdgpu_is_tmz(adev)) { 582 drm_file_err(filp, "Secure only supported on GFX/Compute queues\n"); 583 return -EINVAL; 584 } 585 586 if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET || 587 args->in.queue_va == 0 || 588 args->in.queue_size == 0) { 589 drm_file_err(filp, "invalidate userq queue va or size\n"); 590 return -EINVAL; 591 } 592 if (!args->in.wptr_va || !args->in.rptr_va) { 593 drm_file_err(filp, "invalidate userq queue rptr or wptr\n"); 594 return -EINVAL; 595 } 596 break; 597 case AMDGPU_USERQ_OP_FREE: 598 if (args->in.ip_type || 599 args->in.doorbell_handle || 600 args->in.doorbell_offset || 601 args->in.flags || 602 args->in.queue_va || 603 args->in.queue_size || 604 args->in.rptr_va || 605 args->in.wptr_va || 606 args->in.mqd || 607 args->in.mqd_size) 608 return -EINVAL; 609 break; 610 default: 611 return -EINVAL; 612 } 613 614 return 0; 615 } 616 617 int amdgpu_userq_ioctl(struct drm_device *dev, void *data, 618 struct drm_file *filp) 619 { 620 union drm_amdgpu_userq *args = data; 621 int r; 622 623 if (amdgpu_userq_input_args_validate(dev, args, filp) < 0) 624 return -EINVAL; 625 626 switch (args->in.op) { 627 case AMDGPU_USERQ_OP_CREATE: 628 r = amdgpu_userq_create(filp, args); 629 if (r) 630 drm_file_err(filp, "Failed to create usermode queue\n"); 631 break; 632 633 case AMDGPU_USERQ_OP_FREE: 634 r = amdgpu_userq_destroy(filp, args->in.queue_id); 635 if (r) 636 drm_file_err(filp, "Failed to destroy usermode queue\n"); 637 break; 638 639 default: 640 drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op); 641 return -EINVAL; 642 } 643 644 return r; 645 } 646 647 static int 648 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) 649 { 650 struct amdgpu_usermode_queue *queue; 651 int queue_id; 652 int ret = 0, r; 653 654 /* Resume all the queues for this process */ 655 idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) { 656 r = amdgpu_userq_map_helper(uq_mgr, queue); 657 if (r) 658 ret = r; 659 } 660 661 if (ret) 662 drm_file_err(uq_mgr->file, "Failed to map all the queues\n"); 663 return ret; 664 } 665 666 static int 667 amdgpu_userq_validate_vm_bo(void *_unused, struct amdgpu_bo *bo) 668 { 669 struct ttm_operation_ctx ctx = { false, false }; 670 int ret; 671 672 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 673 674 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 675 if (ret) 676 DRM_ERROR("Fail to validate\n"); 677 678 return ret; 679 } 680 681 static int 682 amdgpu_userq_validate_bos(struct amdgpu_userq_mgr *uq_mgr) 683 { 684 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 685 struct amdgpu_vm *vm = &fpriv->vm; 686 struct amdgpu_device *adev = uq_mgr->adev; 687 struct amdgpu_bo_va *bo_va; 688 struct ww_acquire_ctx *ticket; 689 struct drm_exec exec; 690 struct amdgpu_bo *bo; 691 struct dma_resv *resv; 692 bool clear, unlock; 693 int ret = 0; 694 695 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0); 696 drm_exec_until_all_locked(&exec) { 697 ret = amdgpu_vm_lock_pd(vm, &exec, 2); 698 drm_exec_retry_on_contention(&exec); 699 if (unlikely(ret)) { 700 drm_file_err(uq_mgr->file, "Failed to lock PD\n"); 701 goto unlock_all; 702 } 703 704 /* Lock the done list */ 705 list_for_each_entry(bo_va, &vm->done, base.vm_status) { 706 bo = bo_va->base.bo; 707 if (!bo) 708 continue; 709 710 ret = drm_exec_lock_obj(&exec, &bo->tbo.base); 711 drm_exec_retry_on_contention(&exec); 712 if (unlikely(ret)) 713 goto unlock_all; 714 } 715 } 716 717 spin_lock(&vm->status_lock); 718 while (!list_empty(&vm->moved)) { 719 bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va, 720 base.vm_status); 721 spin_unlock(&vm->status_lock); 722 723 /* Per VM BOs never need to bo cleared in the page tables */ 724 ret = amdgpu_vm_bo_update(adev, bo_va, false); 725 if (ret) 726 goto unlock_all; 727 spin_lock(&vm->status_lock); 728 } 729 730 ticket = &exec.ticket; 731 while (!list_empty(&vm->invalidated)) { 732 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, 733 base.vm_status); 734 resv = bo_va->base.bo->tbo.base.resv; 735 spin_unlock(&vm->status_lock); 736 737 bo = bo_va->base.bo; 738 ret = amdgpu_userq_validate_vm_bo(NULL, bo); 739 if (ret) { 740 drm_file_err(uq_mgr->file, "Failed to validate BO\n"); 741 goto unlock_all; 742 } 743 744 /* Try to reserve the BO to avoid clearing its ptes */ 745 if (!adev->debug_vm && dma_resv_trylock(resv)) { 746 clear = false; 747 unlock = true; 748 /* The caller is already holding the reservation lock */ 749 } else if (dma_resv_locking_ctx(resv) == ticket) { 750 clear = false; 751 unlock = false; 752 /* Somebody else is using the BO right now */ 753 } else { 754 clear = true; 755 unlock = false; 756 } 757 758 ret = amdgpu_vm_bo_update(adev, bo_va, clear); 759 760 if (unlock) 761 dma_resv_unlock(resv); 762 if (ret) 763 goto unlock_all; 764 765 spin_lock(&vm->status_lock); 766 } 767 spin_unlock(&vm->status_lock); 768 769 ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec); 770 if (ret) 771 drm_file_err(uq_mgr->file, "Failed to replace eviction fence\n"); 772 773 unlock_all: 774 drm_exec_fini(&exec); 775 return ret; 776 } 777 778 static void amdgpu_userq_restore_worker(struct work_struct *work) 779 { 780 struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work); 781 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 782 int ret; 783 784 flush_delayed_work(&fpriv->evf_mgr.suspend_work); 785 786 mutex_lock(&uq_mgr->userq_mutex); 787 788 ret = amdgpu_userq_validate_bos(uq_mgr); 789 if (ret) { 790 drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n"); 791 goto unlock; 792 } 793 794 ret = amdgpu_userq_restore_all(uq_mgr); 795 if (ret) { 796 drm_file_err(uq_mgr->file, "Failed to restore all queues\n"); 797 goto unlock; 798 } 799 800 unlock: 801 mutex_unlock(&uq_mgr->userq_mutex); 802 } 803 804 static int 805 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) 806 { 807 struct amdgpu_usermode_queue *queue; 808 int queue_id; 809 int ret = 0, r; 810 811 /* Try to unmap all the queues in this process ctx */ 812 idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) { 813 r = amdgpu_userq_unmap_helper(uq_mgr, queue); 814 if (r) 815 ret = r; 816 } 817 818 if (ret) 819 drm_file_err(uq_mgr->file, "Couldn't unmap all the queues\n"); 820 return ret; 821 } 822 823 static int 824 amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) 825 { 826 struct amdgpu_usermode_queue *queue; 827 int queue_id, ret; 828 829 idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) { 830 struct dma_fence *f = queue->last_fence; 831 832 if (!f || dma_fence_is_signaled(f)) 833 continue; 834 ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100)); 835 if (ret <= 0) { 836 drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n", 837 f->context, f->seqno); 838 return -ETIMEDOUT; 839 } 840 } 841 842 return 0; 843 } 844 845 void 846 amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr, 847 struct amdgpu_eviction_fence *ev_fence) 848 { 849 int ret; 850 struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr); 851 struct amdgpu_eviction_fence_mgr *evf_mgr = &fpriv->evf_mgr; 852 853 /* Wait for any pending userqueue fence work to finish */ 854 ret = amdgpu_userq_wait_for_signal(uq_mgr); 855 if (ret) { 856 drm_file_err(uq_mgr->file, "Not evicting userqueue, timeout waiting for work\n"); 857 return; 858 } 859 860 ret = amdgpu_userq_evict_all(uq_mgr); 861 if (ret) { 862 drm_file_err(uq_mgr->file, "Failed to evict userqueue\n"); 863 return; 864 } 865 866 /* Signal current eviction fence */ 867 amdgpu_eviction_fence_signal(evf_mgr, ev_fence); 868 869 if (evf_mgr->fd_closing) { 870 cancel_delayed_work_sync(&uq_mgr->resume_work); 871 return; 872 } 873 874 /* Schedule a resume work */ 875 schedule_delayed_work(&uq_mgr->resume_work, 0); 876 } 877 878 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv, 879 struct amdgpu_device *adev) 880 { 881 mutex_init(&userq_mgr->userq_mutex); 882 idr_init_base(&userq_mgr->userq_idr, 1); 883 userq_mgr->adev = adev; 884 userq_mgr->file = file_priv; 885 886 mutex_lock(&adev->userq_mutex); 887 list_add(&userq_mgr->list, &adev->userq_mgr_list); 888 mutex_unlock(&adev->userq_mutex); 889 890 INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker); 891 return 0; 892 } 893 894 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr) 895 { 896 struct amdgpu_device *adev = userq_mgr->adev; 897 struct amdgpu_usermode_queue *queue; 898 struct amdgpu_userq_mgr *uqm, *tmp; 899 uint32_t queue_id; 900 901 cancel_delayed_work_sync(&userq_mgr->resume_work); 902 903 mutex_lock(&adev->userq_mutex); 904 mutex_lock(&userq_mgr->userq_mutex); 905 idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) { 906 amdgpu_userq_wait_for_last_fence(userq_mgr, queue); 907 amdgpu_userq_unmap_helper(userq_mgr, queue); 908 amdgpu_userq_cleanup(userq_mgr, queue, queue_id); 909 } 910 911 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 912 if (uqm == userq_mgr) { 913 list_del(&uqm->list); 914 break; 915 } 916 } 917 idr_destroy(&userq_mgr->userq_idr); 918 mutex_unlock(&userq_mgr->userq_mutex); 919 mutex_unlock(&adev->userq_mutex); 920 mutex_destroy(&userq_mgr->userq_mutex); 921 } 922 923 int amdgpu_userq_suspend(struct amdgpu_device *adev) 924 { 925 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 926 struct amdgpu_usermode_queue *queue; 927 struct amdgpu_userq_mgr *uqm, *tmp; 928 int queue_id; 929 int ret = 0, r; 930 931 if (!ip_mask) 932 return 0; 933 934 mutex_lock(&adev->userq_mutex); 935 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 936 cancel_delayed_work_sync(&uqm->resume_work); 937 mutex_lock(&uqm->userq_mutex); 938 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 939 r = amdgpu_userq_unmap_helper(uqm, queue); 940 if (r) 941 ret = r; 942 } 943 mutex_unlock(&uqm->userq_mutex); 944 } 945 mutex_unlock(&adev->userq_mutex); 946 return ret; 947 } 948 949 int amdgpu_userq_resume(struct amdgpu_device *adev) 950 { 951 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 952 struct amdgpu_usermode_queue *queue; 953 struct amdgpu_userq_mgr *uqm, *tmp; 954 int queue_id; 955 int ret = 0, r; 956 957 if (!ip_mask) 958 return 0; 959 960 mutex_lock(&adev->userq_mutex); 961 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 962 mutex_lock(&uqm->userq_mutex); 963 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 964 r = amdgpu_userq_map_helper(uqm, queue); 965 if (r) 966 ret = r; 967 } 968 mutex_unlock(&uqm->userq_mutex); 969 } 970 mutex_unlock(&adev->userq_mutex); 971 return ret; 972 } 973 974 int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, 975 u32 idx) 976 { 977 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 978 struct amdgpu_usermode_queue *queue; 979 struct amdgpu_userq_mgr *uqm, *tmp; 980 int queue_id; 981 int ret = 0, r; 982 983 /* only need to stop gfx/compute */ 984 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 985 return 0; 986 987 mutex_lock(&adev->userq_mutex); 988 if (adev->userq_halt_for_enforce_isolation) 989 dev_warn(adev->dev, "userq scheduling already stopped!\n"); 990 adev->userq_halt_for_enforce_isolation = true; 991 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 992 cancel_delayed_work_sync(&uqm->resume_work); 993 mutex_lock(&uqm->userq_mutex); 994 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 995 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 996 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 997 (queue->xcp_id == idx)) { 998 r = amdgpu_userq_unmap_helper(uqm, queue); 999 if (r) 1000 ret = r; 1001 } 1002 } 1003 mutex_unlock(&uqm->userq_mutex); 1004 } 1005 mutex_unlock(&adev->userq_mutex); 1006 return ret; 1007 } 1008 1009 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, 1010 u32 idx) 1011 { 1012 u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); 1013 struct amdgpu_usermode_queue *queue; 1014 struct amdgpu_userq_mgr *uqm, *tmp; 1015 int queue_id; 1016 int ret = 0, r; 1017 1018 /* only need to stop gfx/compute */ 1019 if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 << AMDGPU_HW_IP_COMPUTE)))) 1020 return 0; 1021 1022 mutex_lock(&adev->userq_mutex); 1023 if (!adev->userq_halt_for_enforce_isolation) 1024 dev_warn(adev->dev, "userq scheduling already started!\n"); 1025 adev->userq_halt_for_enforce_isolation = false; 1026 list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) { 1027 mutex_lock(&uqm->userq_mutex); 1028 idr_for_each_entry(&uqm->userq_idr, queue, queue_id) { 1029 if (((queue->queue_type == AMDGPU_HW_IP_GFX) || 1030 (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) && 1031 (queue->xcp_id == idx)) { 1032 r = amdgpu_userq_map_helper(uqm, queue); 1033 if (r) 1034 ret = r; 1035 } 1036 } 1037 mutex_unlock(&uqm->userq_mutex); 1038 } 1039 mutex_unlock(&adev->userq_mutex); 1040 return ret; 1041 } 1042