1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* 3 * Copyright 2014-2022 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 <linux/slab.h> 26 #include <linux/list.h> 27 #include "kfd_device_queue_manager.h" 28 #include "kfd_priv.h" 29 #include "kfd_topology.h" 30 #include "kfd_kernel_queue.h" 31 #include "amdgpu_amdkfd.h" 32 #include "amdgpu_reset.h" 33 34 static inline struct process_queue_node *get_queue_by_qid( 35 struct process_queue_manager *pqm, unsigned int qid) 36 { 37 struct process_queue_node *pqn; 38 39 list_for_each_entry(pqn, &pqm->queues, process_queue_list) { 40 if ((pqn->q && pqn->q->properties.queue_id == qid) || 41 (pqn->kq && pqn->kq->queue->properties.queue_id == qid)) 42 return pqn; 43 } 44 45 return NULL; 46 } 47 48 static int assign_queue_slot_by_qid(struct process_queue_manager *pqm, 49 unsigned int qid) 50 { 51 if (qid >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) 52 return -EINVAL; 53 54 if (__test_and_set_bit(qid, pqm->queue_slot_bitmap)) { 55 pr_err("Cannot create new queue because requested qid(%u) is in use\n", qid); 56 return -ENOSPC; 57 } 58 59 return 0; 60 } 61 62 static int find_available_queue_slot(struct process_queue_manager *pqm, 63 unsigned int *qid) 64 { 65 unsigned long found; 66 67 found = find_first_zero_bit(pqm->queue_slot_bitmap, 68 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS); 69 70 pr_debug("The new slot id %lu\n", found); 71 72 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) { 73 pr_info("Cannot open more queues for process with pid %d\n", 74 pqm->process->lead_thread->pid); 75 return -ENOMEM; 76 } 77 78 set_bit(found, pqm->queue_slot_bitmap); 79 *qid = found; 80 81 return 0; 82 } 83 84 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd) 85 { 86 struct kfd_node *dev = pdd->dev; 87 88 if (pdd->already_dequeued) 89 return; 90 /* The MES context flush needs to filter out the case which the 91 * KFD process is created without setting up the MES context and 92 * queue for creating a compute queue. 93 */ 94 dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd); 95 if (dev->kfd->shared_resources.enable_mes && !!pdd->proc_ctx_gpu_addr && 96 down_read_trylock(&dev->adev->reset_domain->sem)) { 97 amdgpu_mes_flush_shader_debugger(dev->adev, 98 pdd->proc_ctx_gpu_addr, 99 ffs(pdd->dev->xcc_mask) - 1); 100 up_read(&dev->adev->reset_domain->sem); 101 } 102 pdd->already_dequeued = true; 103 } 104 105 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid, 106 void *gws) 107 { 108 struct mqd_update_info minfo = {0}; 109 struct kfd_node *dev = NULL; 110 struct process_queue_node *pqn; 111 struct kfd_process_device *pdd; 112 struct kgd_mem *mem = NULL; 113 int ret; 114 115 pqn = get_queue_by_qid(pqm, qid); 116 if (!pqn) { 117 pr_err("Queue id does not match any known queue\n"); 118 return -EINVAL; 119 } 120 121 if (pqn->q) 122 dev = pqn->q->device; 123 if (WARN_ON(!dev)) 124 return -ENODEV; 125 126 pdd = kfd_get_process_device_data(dev, pqm->process); 127 if (!pdd) { 128 pr_err("Process device data doesn't exist\n"); 129 return -EINVAL; 130 } 131 132 /* Only allow one queue per process can have GWS assigned */ 133 if (gws && pdd->qpd.num_gws) 134 return -EBUSY; 135 136 if (!gws && pdd->qpd.num_gws == 0) 137 return -EINVAL; 138 139 if ((KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 3) && 140 KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 4) && 141 KFD_GC_VERSION(dev) != IP_VERSION(9, 5, 0)) && 142 !dev->kfd->shared_resources.enable_mes) { 143 if (gws) 144 ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info, 145 gws, &mem); 146 else 147 ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info, 148 pqn->q->gws); 149 if (unlikely(ret)) 150 return ret; 151 pqn->q->gws = mem; 152 } else { 153 /* 154 * Intentionally set GWS to a non-NULL value 155 * for devices that do not use GWS for global wave 156 * synchronization but require the formality 157 * of setting GWS for cooperative groups. 158 */ 159 pqn->q->gws = gws ? ERR_PTR(-ENOMEM) : NULL; 160 } 161 162 pdd->qpd.num_gws = gws ? dev->adev->gds.gws_size : 0; 163 minfo.update_flag = gws ? UPDATE_FLAG_IS_GWS : 0; 164 165 return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm, 166 pqn->q, &minfo); 167 } 168 169 void kfd_process_dequeue_from_all_devices(struct kfd_process *p) 170 { 171 int i; 172 173 for (i = 0; i < p->n_pdds; i++) 174 kfd_process_dequeue_from_device(p->pdds[i]); 175 } 176 177 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p) 178 { 179 INIT_LIST_HEAD(&pqm->queues); 180 pqm->queue_slot_bitmap = bitmap_zalloc(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, 181 GFP_KERNEL); 182 if (!pqm->queue_slot_bitmap) 183 return -ENOMEM; 184 pqm->process = p; 185 186 return 0; 187 } 188 189 static void pqm_clean_queue_resource(struct process_queue_manager *pqm, 190 struct process_queue_node *pqn) 191 { 192 struct kfd_node *dev; 193 struct kfd_process_device *pdd; 194 195 dev = pqn->q->device; 196 197 pdd = kfd_get_process_device_data(dev, pqm->process); 198 if (!pdd) { 199 pr_err("Process device data doesn't exist\n"); 200 return; 201 } 202 203 if (pqn->q->gws) { 204 if (KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 3) && 205 KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 4, 4) && 206 KFD_GC_VERSION(pqn->q->device) != IP_VERSION(9, 5, 0) && 207 !dev->kfd->shared_resources.enable_mes) 208 amdgpu_amdkfd_remove_gws_from_process( 209 pqm->process->kgd_process_info, pqn->q->gws); 210 pdd->qpd.num_gws = 0; 211 } 212 213 if (dev->kfd->shared_resources.enable_mes) { 214 amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, pqn->q->gang_ctx_array_index); 215 amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo); 216 amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart); 217 } 218 } 219 220 void pqm_uninit(struct process_queue_manager *pqm) 221 { 222 struct process_queue_node *pqn, *next; 223 224 list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) { 225 if (pqn->q) { 226 struct kfd_process_device *pdd = kfd_get_process_device_data(pqn->q->device, 227 pqm->process); 228 if (pdd) { 229 kfd_queue_unref_bo_vas(pdd, &pqn->q->properties); 230 kfd_queue_release_buffers(pdd, &pqn->q->properties, false); 231 } else { 232 WARN_ON(!pdd); 233 } 234 pqm_clean_queue_resource(pqm, pqn); 235 } 236 237 kfd_procfs_del_queue(pqn->q); 238 uninit_queue(pqn->q); 239 list_del(&pqn->process_queue_list); 240 kfree(pqn); 241 } 242 243 bitmap_free(pqm->queue_slot_bitmap); 244 pqm->queue_slot_bitmap = NULL; 245 } 246 247 static int init_user_queue(struct process_queue_manager *pqm, 248 struct kfd_node *dev, struct queue **q, 249 struct queue_properties *q_properties, 250 unsigned int qid) 251 { 252 int retval; 253 254 /* Doorbell initialized in user space*/ 255 q_properties->doorbell_ptr = NULL; 256 q_properties->exception_status = KFD_EC_MASK(EC_QUEUE_NEW); 257 258 /* let DQM handle it*/ 259 q_properties->vmid = 0; 260 q_properties->queue_id = qid; 261 262 retval = init_queue(q, q_properties); 263 if (retval != 0) 264 return retval; 265 266 (*q)->device = dev; 267 (*q)->process = pqm->process; 268 269 if (dev->kfd->shared_resources.enable_mes) { 270 if (!q_properties->wptr_bo) { 271 pr_debug("Queue initialization with shared MES requires queue buffers to be initialized\n"); 272 return -EINVAL; 273 } 274 275 retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev, 276 AMDGPU_MES_GANG_CTX_SIZE, 277 AMDGPU_GEM_DOMAIN_GTT, 278 &(*q)->gang_ctx_bo, 279 &(*q)->gang_ctx_gpu_addr, 280 &(*q)->gang_ctx_cpu_ptr, 281 false); 282 if (retval) { 283 pr_err("failed to allocate gang context bo\n"); 284 goto cleanup; 285 } 286 memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE); 287 /* Bind one MES gang context slot per queue (gang). */ 288 if (dev->adev->mes.use_rs64mem) { 289 retval = amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes, 290 &(*q)->gang_ctx_array_index); 291 if (retval) { 292 pr_err("failed to allocate gang context index slot\n"); 293 goto cleanup; 294 } 295 } 296 /* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work 297 * on unmapped queues for usermode queue oversubscription (no aggregated doorbell) 298 */ 299 if (dev->adev != amdgpu_ttm_adev(q_properties->wptr_bo->tbo.bdev)) { 300 pr_err("Queue memory allocated to wrong device\n"); 301 retval = -EINVAL; 302 goto free_gang_ctx_bo; 303 } 304 305 retval = amdgpu_amdkfd_map_gtt_bo_to_gart(q_properties->wptr_bo, 306 &(*q)->wptr_bo_gart, 307 pqm->process->kgd_process_info); 308 if (retval) { 309 pr_err("Failed to map wptr bo to GART\n"); 310 goto free_gang_ctx_bo; 311 } 312 } 313 314 pr_debug("PQM After init queue"); 315 return 0; 316 317 free_gang_ctx_bo: 318 amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index); 319 amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo); 320 cleanup: 321 uninit_queue(*q); 322 *q = NULL; 323 return retval; 324 } 325 326 int pqm_create_queue(struct process_queue_manager *pqm, 327 struct kfd_node *dev, 328 struct queue_properties *properties, 329 unsigned int *qid, 330 const struct kfd_criu_queue_priv_data *q_data, 331 const void *restore_mqd, 332 const void *restore_ctl_stack, 333 uint32_t *p_doorbell_offset_in_process) 334 { 335 int retval; 336 struct kfd_process_device *pdd; 337 struct queue *q; 338 struct process_queue_node *pqn; 339 struct kernel_queue *kq; 340 enum kfd_queue_type type = properties->type; 341 unsigned int max_queues = 127; /* HWS limit */ 342 343 /* 344 * On GFX 9.4.3/9.5.0/12.1.0, increase the number of queues that 345 * can be created to 255. No HWS limit on GFX 9.4.3/9.5.0/12.1.0. 346 */ 347 if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3) || 348 KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4) || 349 KFD_GC_VERSION(dev) == IP_VERSION(9, 5, 0) || 350 KFD_GC_VERSION(dev) == IP_VERSION(12, 1, 0)) 351 max_queues = 255; 352 353 q = NULL; 354 kq = NULL; 355 356 pdd = kfd_get_process_device_data(dev, pqm->process); 357 if (!pdd) { 358 pr_err("Process device data doesn't exist\n"); 359 return -1; 360 } 361 362 /* 363 * for debug process, verify that it is within the static queues limit 364 * currently limit is set to half of the total avail HQD slots 365 * If we are just about to create DIQ, the is_debug flag is not set yet 366 * Hence we also check the type as well 367 */ 368 if (pdd->qpd.is_debug) 369 max_queues = dev->kfd->device_info.max_no_of_hqd/2; 370 371 if (pdd->qpd.queue_count >= max_queues) 372 return -ENOSPC; 373 374 if (q_data) { 375 retval = assign_queue_slot_by_qid(pqm, q_data->q_id); 376 *qid = q_data->q_id; 377 } else 378 retval = find_available_queue_slot(pqm, qid); 379 380 if (retval != 0) 381 return retval; 382 383 /* Register process if this is the first queue */ 384 if (list_empty(&pdd->qpd.queues_list) && 385 list_empty(&pdd->qpd.priv_queue_list)) 386 dev->dqm->ops.register_process(dev->dqm, &pdd->qpd); 387 388 /* Allocate proc_ctx_bo only if MES is enabled and this is the first queue */ 389 if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) { 390 retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev, 391 AMDGPU_MES_PROC_CTX_SIZE, 392 AMDGPU_GEM_DOMAIN_GTT, 393 &pdd->proc_ctx_bo, 394 &pdd->proc_ctx_gpu_addr, 395 &pdd->proc_ctx_cpu_ptr, 396 false); 397 if (retval) { 398 dev_err(dev->adev->dev, "failed to allocate process context bo\n"); 399 goto err_allocate_pqn; 400 } 401 memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE); 402 /* Bind one MES process context slot to the whole process 403 * (per device); every queue of this process reuses it. 404 */ 405 if (dev->adev->mes.use_rs64mem) { 406 retval = amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes, 407 &pdd->proc_ctx_array_index); 408 if (retval) { 409 dev_err(dev->adev->dev, "failed to allocate process context index\n"); 410 goto err_allocate_pqn; 411 } 412 } 413 } 414 415 pqn = kzalloc_obj(*pqn); 416 if (!pqn) { 417 retval = -ENOMEM; 418 goto err_allocate_pqn; 419 } 420 421 switch (type) { 422 case KFD_QUEUE_TYPE_SDMA: 423 case KFD_QUEUE_TYPE_SDMA_XGMI: 424 case KFD_QUEUE_TYPE_SDMA_BY_ENG_ID: 425 /* SDMA queues are always allocated statically no matter 426 * which scheduler mode is used. We also do not need to 427 * check whether a SDMA queue can be allocated here, because 428 * allocate_sdma_queue() in create_queue() has the 429 * corresponding check logic. 430 */ 431 retval = init_user_queue(pqm, dev, &q, properties, *qid); 432 if (retval != 0) 433 goto err_create_queue; 434 pqn->q = q; 435 pqn->kq = NULL; 436 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data, 437 restore_mqd, restore_ctl_stack); 438 print_queue(q); 439 break; 440 441 case KFD_QUEUE_TYPE_COMPUTE: 442 /* check if there is over subscription */ 443 if ((dev->dqm->sched_policy == 444 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) && 445 ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) || 446 (dev->dqm->active_queue_count >= get_cp_queues_num(dev->dqm)))) { 447 pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n"); 448 retval = -EPERM; 449 goto err_create_queue; 450 } 451 452 retval = init_user_queue(pqm, dev, &q, properties, *qid); 453 if (retval != 0) 454 goto err_create_queue; 455 pqn->q = q; 456 pqn->kq = NULL; 457 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd, q_data, 458 restore_mqd, restore_ctl_stack); 459 print_queue(q); 460 break; 461 default: 462 WARN(1, "Invalid queue type %d", type); 463 retval = -EINVAL; 464 } 465 466 if (retval != 0) { 467 if ((type == KFD_QUEUE_TYPE_SDMA || 468 type == KFD_QUEUE_TYPE_SDMA_XGMI || 469 type == KFD_QUEUE_TYPE_SDMA_BY_ENG_ID) && 470 retval == -ENOMEM) 471 pr_warn("process pid %d DQM create queue type %d failed. ret %d\n", 472 pqm->process->lead_thread->pid, type, retval); 473 else 474 pr_err("process pid %d DQM create queue type %d failed. ret %d\n", 475 pqm->process->lead_thread->pid, type, retval); 476 goto err_create_queue; 477 } 478 479 if (q && p_doorbell_offset_in_process) { 480 /* Return the doorbell offset within the doorbell page 481 * to the caller so it can be passed up to user mode 482 * (in bytes). 483 * relative doorbell index = Absolute doorbell index - 484 * absolute index of first doorbell in the page. 485 */ 486 uint32_t first_db_index = amdgpu_doorbell_index_on_bar(pdd->dev->adev, 487 pdd->qpd.proc_doorbells, 488 0, 489 pdd->dev->kfd->device_info.doorbell_size); 490 491 *p_doorbell_offset_in_process = (q->properties.doorbell_off 492 - first_db_index) * sizeof(uint32_t); 493 } 494 495 pr_debug("PQM After DQM create queue\n"); 496 497 list_add(&pqn->process_queue_list, &pqm->queues); 498 499 if (q) { 500 pr_debug("PQM done creating queue\n"); 501 kfd_procfs_add_queue(q); 502 print_queue_properties(&q->properties); 503 } 504 505 return retval; 506 507 err_create_queue: 508 uninit_queue(q); 509 if (kq) 510 kernel_queue_uninit(kq); 511 kfree(pqn); 512 err_allocate_pqn: 513 /* check if queues list is empty unregister process from device */ 514 clear_bit(*qid, pqm->queue_slot_bitmap); 515 if (list_empty(&pdd->qpd.queues_list) && 516 list_empty(&pdd->qpd.priv_queue_list)) 517 dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd); 518 return retval; 519 } 520 521 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid) 522 { 523 struct process_queue_node *pqn; 524 struct kfd_process_device *pdd; 525 struct device_queue_manager *dqm; 526 struct kfd_node *dev; 527 int retval; 528 529 dqm = NULL; 530 531 retval = 0; 532 533 pqn = get_queue_by_qid(pqm, qid); 534 if (!pqn) { 535 pr_err("Queue id does not match any known queue\n"); 536 return -EINVAL; 537 } 538 539 dev = NULL; 540 if (pqn->kq) 541 dev = pqn->kq->dev; 542 if (pqn->q) 543 dev = pqn->q->device; 544 if (WARN_ON(!dev)) 545 return -ENODEV; 546 547 pdd = kfd_get_process_device_data(dev, pqm->process); 548 if (!pdd) { 549 pr_err("Process device data doesn't exist\n"); 550 return -1; 551 } 552 553 if (pqn->kq) { 554 /* destroy kernel queue (DIQ) */ 555 dqm = pqn->kq->dev->dqm; 556 dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd); 557 kernel_queue_uninit(pqn->kq); 558 } 559 560 if (pqn->q) { 561 retval = kfd_queue_unref_bo_vas(pdd, &pqn->q->properties); 562 if (retval) 563 goto err_destroy_queue; 564 565 dqm = pqn->q->device->dqm; 566 retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q); 567 if (retval) { 568 pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n", 569 pdd->pasid, 570 pqn->q->properties.queue_id, retval); 571 if (retval != -ETIME && retval != -EIO) 572 goto err_destroy_queue; 573 } 574 kfd_procfs_del_queue(pqn->q); 575 kfd_queue_release_buffers(pdd, &pqn->q->properties, false); 576 pqm_clean_queue_resource(pqm, pqn); 577 uninit_queue(pqn->q); 578 } 579 580 list_del(&pqn->process_queue_list); 581 kfree(pqn); 582 clear_bit(qid, pqm->queue_slot_bitmap); 583 584 if (list_empty(&pdd->qpd.queues_list) && 585 list_empty(&pdd->qpd.priv_queue_list)) 586 dqm->ops.unregister_process(dqm, &pdd->qpd); 587 588 err_destroy_queue: 589 return retval; 590 } 591 592 int pqm_update_queue_properties(struct process_queue_manager *pqm, 593 unsigned int qid, struct queue_properties *p) 594 { 595 int retval; 596 struct process_queue_node *pqn; 597 598 pqn = get_queue_by_qid(pqm, qid); 599 if (!pqn || !pqn->q) { 600 pr_debug("No queue %d exists for update operation\n", qid); 601 return -EFAULT; 602 } 603 604 /* 605 * Update with NULL ring address is used to disable the queue 606 */ 607 if (p->queue_address && p->queue_size) { 608 struct kfd_process_device *pdd; 609 struct amdgpu_vm *vm; 610 struct queue *q = pqn->q; 611 int err; 612 613 pdd = kfd_get_process_device_data(q->device, q->process); 614 if (!pdd) 615 return -ENODEV; 616 vm = drm_priv_to_vm(pdd->drm_priv); 617 err = amdgpu_bo_reserve(vm->root.bo, false); 618 if (err) 619 return err; 620 621 if (kfd_queue_buffer_get(vm, (void *)p->queue_address, &p->ring_bo, 622 p->queue_size + 623 pqn->q->properties.metadata_queue_size)) { 624 pr_debug("ring buf 0x%llx size 0x%llx not mapped on GPU\n", 625 p->queue_address, p->queue_size); 626 amdgpu_bo_unreserve(vm->root.bo); 627 return -EFAULT; 628 } 629 630 kfd_queue_unref_bo_va(vm, &pqn->q->properties.ring_bo); 631 kfd_queue_buffer_put(&pqn->q->properties.ring_bo); 632 amdgpu_bo_unreserve(vm->root.bo); 633 634 pqn->q->properties.ring_bo = p->ring_bo; 635 } 636 637 pqn->q->properties.queue_address = p->queue_address; 638 pqn->q->properties.queue_size = p->queue_size; 639 pqn->q->properties.queue_percent = p->queue_percent; 640 pqn->q->properties.priority = p->priority; 641 pqn->q->properties.pm4_target_xcc = p->pm4_target_xcc; 642 643 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm, 644 pqn->q, NULL); 645 if (retval != 0) 646 return retval; 647 648 return 0; 649 } 650 651 int pqm_update_mqd(struct process_queue_manager *pqm, 652 unsigned int qid, struct mqd_update_info *minfo) 653 { 654 int retval; 655 struct process_queue_node *pqn; 656 657 pqn = get_queue_by_qid(pqm, qid); 658 if (!pqn) { 659 pr_debug("No queue %d exists for update operation\n", qid); 660 return -EFAULT; 661 } 662 663 /* CUs are masked for debugger requirements so deny user mask */ 664 if (pqn->q->properties.is_dbg_wa && minfo && minfo->cu_mask.ptr) 665 return -EBUSY; 666 667 /* ASICs that have WGPs must enforce pairwise enabled mask checks. */ 668 if (minfo && minfo->cu_mask.ptr && 669 KFD_GC_VERSION(pqn->q->device) >= IP_VERSION(10, 0, 0) && 670 KFD_GC_VERSION(pqn->q->device) < IP_VERSION(12, 1, 0)) { 671 int i; 672 673 for (i = 0; i < minfo->cu_mask.count; i += 2) { 674 uint32_t cu_pair = (minfo->cu_mask.ptr[i / 32] >> (i % 32)) & 0x3; 675 676 if (cu_pair && cu_pair != 0x3) { 677 pr_debug("CUs must be adjacent pairwise enabled.\n"); 678 return -EINVAL; 679 } 680 } 681 } 682 683 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm, 684 pqn->q, minfo); 685 if (retval != 0) 686 return retval; 687 688 if (minfo && minfo->cu_mask.ptr) 689 pqn->q->properties.is_user_cu_masked = true; 690 691 return 0; 692 } 693 694 struct queue *pqm_get_user_queue(struct process_queue_manager *pqm, 695 unsigned int qid) 696 { 697 struct process_queue_node *pqn; 698 699 pqn = get_queue_by_qid(pqm, qid); 700 return pqn ? pqn->q : NULL; 701 } 702 703 int pqm_get_wave_state(struct process_queue_manager *pqm, 704 unsigned int qid, 705 void __user *ctl_stack, 706 u32 *ctl_stack_used_size, 707 u32 *save_area_used_size) 708 { 709 struct process_queue_node *pqn; 710 711 pqn = get_queue_by_qid(pqm, qid); 712 if (!pqn) { 713 pr_debug("amdkfd: No queue %d exists for operation\n", 714 qid); 715 return -EFAULT; 716 } 717 718 return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm, 719 pqn->q, 720 ctl_stack, 721 ctl_stack_used_size, 722 save_area_used_size); 723 } 724 725 int pqm_get_queue_snapshot(struct process_queue_manager *pqm, 726 uint64_t exception_clear_mask, 727 void __user *buf, 728 int *num_qss_entries, 729 uint32_t *entry_size) 730 { 731 struct process_queue_node *pqn; 732 struct kfd_queue_snapshot_entry src; 733 uint32_t tmp_entry_size = *entry_size, tmp_qss_entries = *num_qss_entries; 734 int r = 0; 735 736 *num_qss_entries = 0; 737 if (!(*entry_size)) 738 return -EINVAL; 739 740 *entry_size = min_t(size_t, *entry_size, sizeof(struct kfd_queue_snapshot_entry)); 741 mutex_lock(&pqm->process->event_mutex); 742 743 memset(&src, 0, sizeof(src)); 744 745 list_for_each_entry(pqn, &pqm->queues, process_queue_list) { 746 if (!pqn->q) 747 continue; 748 749 if (*num_qss_entries < tmp_qss_entries) { 750 set_queue_snapshot_entry(pqn->q, exception_clear_mask, &src); 751 752 if (copy_to_user(buf, &src, *entry_size)) { 753 r = -EFAULT; 754 break; 755 } 756 buf += tmp_entry_size; 757 } 758 *num_qss_entries += 1; 759 } 760 761 mutex_unlock(&pqm->process->event_mutex); 762 return r; 763 } 764 765 static int get_queue_data_sizes(struct kfd_process_device *pdd, 766 struct queue *q, 767 uint32_t *mqd_size, 768 uint32_t *ctl_stack_size) 769 { 770 int ret; 771 772 ret = pqm_get_queue_checkpoint_info(&pdd->process->pqm, 773 q->properties.queue_id, 774 mqd_size, 775 ctl_stack_size); 776 if (ret) 777 pr_err("Failed to get queue dump info (%d)\n", ret); 778 779 return ret; 780 } 781 782 int kfd_process_get_queue_info(struct kfd_process *p, 783 uint32_t *num_queues, 784 uint64_t *priv_data_sizes) 785 { 786 uint32_t extra_data_sizes = 0; 787 struct queue *q; 788 int i; 789 int ret; 790 791 *num_queues = 0; 792 793 /* Run over all PDDs of the process */ 794 for (i = 0; i < p->n_pdds; i++) { 795 struct kfd_process_device *pdd = p->pdds[i]; 796 797 list_for_each_entry(q, &pdd->qpd.queues_list, list) { 798 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE || 799 q->properties.type == KFD_QUEUE_TYPE_SDMA || 800 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 801 uint32_t mqd_size, ctl_stack_size; 802 803 *num_queues = *num_queues + 1; 804 805 ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size); 806 if (ret) 807 return ret; 808 809 extra_data_sizes += mqd_size + ctl_stack_size; 810 } else { 811 pr_err("Unsupported queue type (%d)\n", q->properties.type); 812 return -EOPNOTSUPP; 813 } 814 } 815 } 816 *priv_data_sizes = extra_data_sizes + 817 (*num_queues * sizeof(struct kfd_criu_queue_priv_data)); 818 819 return 0; 820 } 821 822 static int pqm_checkpoint_mqd(struct process_queue_manager *pqm, 823 unsigned int qid, 824 void *mqd, 825 void *ctl_stack) 826 { 827 struct process_queue_node *pqn; 828 829 pqn = get_queue_by_qid(pqm, qid); 830 if (!pqn) { 831 pr_debug("amdkfd: No queue %d exists for operation\n", qid); 832 return -EFAULT; 833 } 834 835 if (!pqn->q->device->dqm->ops.checkpoint_mqd) { 836 pr_err("amdkfd: queue dumping not supported on this device\n"); 837 return -EOPNOTSUPP; 838 } 839 840 return pqn->q->device->dqm->ops.checkpoint_mqd(pqn->q->device->dqm, 841 pqn->q, mqd, ctl_stack); 842 } 843 844 static int criu_checkpoint_queue(struct kfd_process_device *pdd, 845 struct queue *q, 846 struct kfd_criu_queue_priv_data *q_data) 847 { 848 uint8_t *mqd, *ctl_stack; 849 int ret; 850 851 mqd = (void *)(q_data + 1); 852 ctl_stack = mqd + q_data->mqd_size; 853 854 q_data->gpu_id = pdd->user_gpu_id; 855 q_data->type = q->properties.type; 856 q_data->format = q->properties.format; 857 q_data->q_id = q->properties.queue_id; 858 q_data->q_address = q->properties.queue_address; 859 q_data->q_size = q->properties.queue_size; 860 q_data->priority = q->properties.priority; 861 q_data->q_percent = q->properties.queue_percent; 862 q_data->read_ptr_addr = (uint64_t)q->properties.read_ptr; 863 q_data->write_ptr_addr = (uint64_t)q->properties.write_ptr; 864 q_data->doorbell_id = q->doorbell_id; 865 866 q_data->sdma_id = q->sdma_id; 867 868 q_data->eop_ring_buffer_address = 869 q->properties.eop_ring_buffer_address; 870 871 q_data->eop_ring_buffer_size = q->properties.eop_ring_buffer_size; 872 873 q_data->ctx_save_restore_area_address = 874 q->properties.ctx_save_restore_area_address; 875 876 q_data->ctx_save_restore_area_size = 877 q->properties.ctx_save_restore_area_size; 878 879 q_data->gws = !!q->gws; 880 881 ret = pqm_checkpoint_mqd(&pdd->process->pqm, q->properties.queue_id, mqd, ctl_stack); 882 if (ret) { 883 pr_err("Failed checkpoint queue_mqd (%d)\n", ret); 884 return ret; 885 } 886 887 pr_debug("Dumping Queue: gpu_id:%x queue_id:%u\n", q_data->gpu_id, q_data->q_id); 888 return ret; 889 } 890 891 static int criu_checkpoint_queues_device(struct kfd_process_device *pdd, 892 uint8_t __user *user_priv, 893 unsigned int *q_index, 894 uint64_t *queues_priv_data_offset) 895 { 896 unsigned int q_private_data_size = 0; 897 uint8_t *q_private_data = NULL; /* Local buffer to store individual queue private data */ 898 struct queue *q; 899 int ret = 0; 900 901 list_for_each_entry(q, &pdd->qpd.queues_list, list) { 902 struct kfd_criu_queue_priv_data *q_data; 903 uint64_t q_data_size; 904 uint32_t mqd_size; 905 uint32_t ctl_stack_size; 906 907 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE && 908 q->properties.type != KFD_QUEUE_TYPE_SDMA && 909 q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI) { 910 911 pr_err("Unsupported queue type (%d)\n", q->properties.type); 912 ret = -EOPNOTSUPP; 913 break; 914 } 915 916 ret = get_queue_data_sizes(pdd, q, &mqd_size, &ctl_stack_size); 917 if (ret) 918 break; 919 920 q_data_size = sizeof(*q_data) + mqd_size + ctl_stack_size; 921 922 /* Increase local buffer space if needed */ 923 if (q_private_data_size < q_data_size) { 924 kfree(q_private_data); 925 926 q_private_data = kzalloc(q_data_size, GFP_KERNEL); 927 if (!q_private_data) { 928 ret = -ENOMEM; 929 break; 930 } 931 q_private_data_size = q_data_size; 932 } 933 934 q_data = (struct kfd_criu_queue_priv_data *)q_private_data; 935 936 /* 937 * data stored in this order: 938 * priv_data, mqd[xcc0], mqd[xcc1],..., ctl_stack[xcc0], ctl_stack[xcc1]... 939 */ 940 q_data->mqd_size = mqd_size; 941 q_data->ctl_stack_size = ctl_stack_size; 942 943 ret = criu_checkpoint_queue(pdd, q, q_data); 944 if (ret) 945 break; 946 947 q_data->object_type = KFD_CRIU_OBJECT_TYPE_QUEUE; 948 949 ret = copy_to_user(user_priv + *queues_priv_data_offset, 950 q_data, q_data_size); 951 if (ret) { 952 ret = -EFAULT; 953 break; 954 } 955 *queues_priv_data_offset += q_data_size; 956 *q_index = *q_index + 1; 957 } 958 959 kfree(q_private_data); 960 961 return ret; 962 } 963 964 int kfd_criu_checkpoint_queues(struct kfd_process *p, 965 uint8_t __user *user_priv_data, 966 uint64_t *priv_data_offset) 967 { 968 int ret = 0, pdd_index, q_index = 0; 969 970 for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) { 971 struct kfd_process_device *pdd = p->pdds[pdd_index]; 972 973 /* 974 * criu_checkpoint_queues_device will copy data to user and update q_index and 975 * queues_priv_data_offset 976 */ 977 ret = criu_checkpoint_queues_device(pdd, user_priv_data, &q_index, 978 priv_data_offset); 979 980 if (ret) 981 break; 982 } 983 984 return ret; 985 } 986 987 static void set_queue_properties_from_criu(struct queue_properties *qp, 988 struct kfd_criu_queue_priv_data *q_data) 989 { 990 qp->is_interop = false; 991 qp->queue_percent = q_data->q_percent; 992 qp->priority = q_data->priority; 993 qp->queue_address = q_data->q_address; 994 qp->queue_size = q_data->q_size; 995 qp->read_ptr = (void __user *)q_data->read_ptr_addr; 996 qp->write_ptr = (void __user *)q_data->write_ptr_addr; 997 qp->eop_ring_buffer_address = q_data->eop_ring_buffer_address; 998 qp->eop_ring_buffer_size = q_data->eop_ring_buffer_size; 999 qp->ctx_save_restore_area_address = q_data->ctx_save_restore_area_address; 1000 qp->ctx_save_restore_area_size = q_data->ctx_save_restore_area_size; 1001 qp->type = q_data->type; 1002 qp->format = q_data->format; 1003 } 1004 1005 int kfd_criu_restore_queue(struct kfd_process *p, 1006 uint8_t __user *user_priv_ptr, 1007 uint64_t *priv_data_offset, 1008 uint64_t max_priv_data_size) 1009 { 1010 uint8_t *mqd, *ctl_stack, *q_extra_data = NULL; 1011 struct kfd_criu_queue_priv_data *q_data; 1012 struct kfd_topology_device *topo_dev; 1013 struct kfd_process_device *pdd; 1014 uint64_t q_extra_data_size; 1015 struct queue_properties qp; 1016 unsigned int queue_id; 1017 int ret = 0; 1018 1019 if (*priv_data_offset + sizeof(*q_data) > max_priv_data_size) 1020 return -EINVAL; 1021 1022 q_data = kmalloc_obj(*q_data); 1023 if (!q_data) 1024 return -ENOMEM; 1025 1026 ret = copy_from_user(q_data, user_priv_ptr + *priv_data_offset, sizeof(*q_data)); 1027 if (ret) { 1028 ret = -EFAULT; 1029 goto exit; 1030 } 1031 1032 pdd = kfd_process_device_data_by_id(p, q_data->gpu_id); 1033 if (!pdd) { 1034 pr_err("Failed to get pdd\n"); 1035 ret = -EINVAL; 1036 goto exit; 1037 } 1038 1039 if (q_data->type >= KFD_QUEUE_TYPE_MAX) { 1040 ret = -EINVAL; 1041 goto exit; 1042 } 1043 1044 if (q_data->mqd_size != mqd_size_from_queue_type(pdd->dev->dqm, q_data->type)) { 1045 ret = -EINVAL; 1046 goto exit; 1047 } 1048 1049 memset(&qp, 0, sizeof(qp)); 1050 set_queue_properties_from_criu(&qp, q_data); 1051 1052 /* Per-XCC control stack size from topology, as create-queue requires */ 1053 if (qp.type == KFD_QUEUE_TYPE_COMPUTE && pdd->dev->kfd->cwsr_enabled) { 1054 topo_dev = kfd_topology_device_by_id(pdd->dev->id); 1055 if (!topo_dev) { 1056 ret = -EINVAL; 1057 goto exit; 1058 } 1059 qp.ctl_stack_size = topo_dev->node_props.ctl_stack_size; 1060 } 1061 1062 if (q_data->ctl_stack_size > (u64)qp.ctl_stack_size * NUM_XCC(pdd->dev->xcc_mask)) { 1063 ret = -EINVAL; 1064 goto exit; 1065 } 1066 1067 *priv_data_offset += sizeof(*q_data); 1068 q_extra_data_size = (uint64_t)q_data->ctl_stack_size + q_data->mqd_size; 1069 1070 if (*priv_data_offset + q_extra_data_size > max_priv_data_size) { 1071 ret = -EINVAL; 1072 goto exit; 1073 } 1074 1075 q_extra_data = kmalloc(q_extra_data_size, GFP_KERNEL); 1076 if (!q_extra_data) { 1077 ret = -ENOMEM; 1078 goto exit; 1079 } 1080 1081 ret = copy_from_user(q_extra_data, user_priv_ptr + *priv_data_offset, q_extra_data_size); 1082 if (ret) { 1083 ret = -EFAULT; 1084 goto exit; 1085 } 1086 1087 *priv_data_offset += q_extra_data_size; 1088 1089 /* 1090 * data stored in this order: 1091 * mqd[xcc0], mqd[xcc1],..., ctl_stack[xcc0], ctl_stack[xcc1]... 1092 */ 1093 mqd = q_extra_data; 1094 ctl_stack = mqd + q_data->mqd_size; 1095 1096 print_queue_properties(&qp); 1097 1098 ret = kfd_queue_acquire_buffers(pdd, &qp, true); 1099 if (ret) { 1100 pr_err("Failed to acquire user queue buffers err:%d\n", ret); 1101 goto exit; 1102 } 1103 1104 ret = pqm_create_queue(&p->pqm, pdd->dev, &qp, &queue_id, q_data, mqd, ctl_stack, NULL); 1105 if (ret) { 1106 pr_err("Failed to create new queue err:%d\n", ret); 1107 kfd_queue_unref_bo_vas(pdd, &qp); 1108 kfd_queue_release_buffers(pdd, &qp, true); 1109 goto exit; 1110 } 1111 1112 if (q_data->gws) 1113 ret = pqm_set_gws(&p->pqm, q_data->q_id, pdd->dev->gws); 1114 1115 exit: 1116 if (ret) 1117 pr_err("Failed to restore queue (%d)\n", ret); 1118 else 1119 pr_debug("Queue id %d was restored successfully\n", queue_id); 1120 1121 kfree(q_data); 1122 kfree(q_extra_data); 1123 1124 return ret; 1125 } 1126 1127 int kfd_criu_resume_queues(struct kfd_process *p) 1128 { 1129 int ret, i; 1130 u64 total_cwsr_size; 1131 1132 for (i = 0; i < p->n_pdds; i++) { 1133 struct kfd_process_device *pdd = p->pdds[i]; 1134 struct kfd_topology_device *topo_dev; 1135 struct queue *q; 1136 1137 topo_dev = kfd_topology_device_by_id(pdd->dev->id); 1138 1139 list_for_each_entry(q, &pdd->qpd.queues_list, list) { 1140 total_cwsr_size = q->properties.ctx_save_restore_area_size + 1141 topo_dev->node_props.debug_memory_size; 1142 if (check_mul_overflow(total_cwsr_size, 1143 NUM_XCC(pdd->dev->xcc_mask), 1144 &total_cwsr_size)) 1145 return -EINVAL; 1146 total_cwsr_size = ALIGN(total_cwsr_size, PAGE_SIZE); 1147 1148 ret = kfd_queue_buffer_svm_get(pdd, q->properties.ctx_save_restore_area_address, 1149 total_cwsr_size); 1150 if (ret) 1151 return ret; 1152 } 1153 1154 } 1155 1156 return 0; 1157 } 1158 1159 int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm, 1160 unsigned int qid, 1161 uint32_t *mqd_size, 1162 uint32_t *ctl_stack_size) 1163 { 1164 struct process_queue_node *pqn; 1165 int ret; 1166 1167 pqn = get_queue_by_qid(pqm, qid); 1168 if (!pqn) { 1169 pr_debug("amdkfd: No queue %d exists for operation\n", qid); 1170 return -EFAULT; 1171 } 1172 1173 if (!pqn->q->device->dqm->ops.get_queue_checkpoint_info) { 1174 pr_err("amdkfd: queue dumping not supported on this device\n"); 1175 return -EOPNOTSUPP; 1176 } 1177 1178 ret = pqn->q->device->dqm->ops.get_queue_checkpoint_info(pqn->q->device->dqm, 1179 pqn->q, mqd_size, 1180 ctl_stack_size); 1181 if (ret) { 1182 pr_debug("amdkfd: Overflow while computing stack size for queue %d\n", qid); 1183 return ret; 1184 } 1185 1186 return 0; 1187 } 1188 1189 #if defined(CONFIG_DEBUG_FS) 1190 1191 int pqm_debugfs_mqds(struct seq_file *m, void *data) 1192 { 1193 struct process_queue_manager *pqm = data; 1194 struct process_queue_node *pqn; 1195 struct queue *q; 1196 enum KFD_MQD_TYPE mqd_type; 1197 struct mqd_manager *mqd_mgr; 1198 int r = 0, xcc, num_xccs = 1; 1199 void *mqd; 1200 uint64_t size = 0; 1201 1202 list_for_each_entry(pqn, &pqm->queues, process_queue_list) { 1203 if (pqn->q) { 1204 q = pqn->q; 1205 switch (q->properties.type) { 1206 case KFD_QUEUE_TYPE_SDMA: 1207 case KFD_QUEUE_TYPE_SDMA_XGMI: 1208 seq_printf(m, " SDMA queue on device %x\n", 1209 q->device->id); 1210 mqd_type = KFD_MQD_TYPE_SDMA; 1211 break; 1212 case KFD_QUEUE_TYPE_COMPUTE: 1213 seq_printf(m, " Compute queue on device %x\n", 1214 q->device->id); 1215 mqd_type = KFD_MQD_TYPE_CP; 1216 num_xccs = NUM_XCC(q->device->xcc_mask); 1217 break; 1218 default: 1219 seq_printf(m, 1220 " Queue node with bad user queue type %d on device %x\n", 1221 q->properties.type, q->device->id); 1222 continue; 1223 } 1224 mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type]; 1225 size = mqd_mgr->mqd_stride(mqd_mgr, 1226 &q->properties); 1227 1228 for (xcc = 0; xcc < num_xccs; xcc++) { 1229 mqd = q->mqd + size * xcc; 1230 r = mqd_mgr->debugfs_show_mqd(m, mqd); 1231 if (r != 0) 1232 break; 1233 } 1234 } 1235 } 1236 1237 return r; 1238 } 1239 1240 #endif 1241