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/ratelimit.h> 26 #include <linux/printk.h> 27 #include <linux/slab.h> 28 #include <linux/list.h> 29 #include <linux/types.h> 30 #include <linux/bitops.h> 31 #include <linux/sched.h> 32 #include "kfd_priv.h" 33 #include "kfd_device_queue_manager.h" 34 #include "kfd_mqd_manager.h" 35 #include "cik_regs.h" 36 #include "kfd_kernel_queue.h" 37 #include "amdgpu_amdkfd.h" 38 #include "amdgpu_reset.h" 39 #include "amdgpu_sdma.h" 40 #include "mes_v11_api_def.h" 41 #include "kfd_debug.h" 42 43 /* Size of the per-pipe EOP queue */ 44 #define CIK_HPD_EOP_BYTES_LOG2 11 45 #define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2) 46 /* See unmap_queues_cpsch() */ 47 #define USE_DEFAULT_GRACE_PERIOD 0xffffffff 48 49 static int set_pasid_vmid_mapping(struct device_queue_manager *dqm, 50 u32 pasid, unsigned int vmid); 51 52 static int execute_queues_cpsch(struct device_queue_manager *dqm, 53 enum kfd_unmap_queues_filter filter, 54 uint32_t filter_param, 55 uint32_t grace_period); 56 static int unmap_queues_cpsch(struct device_queue_manager *dqm, 57 enum kfd_unmap_queues_filter filter, 58 uint32_t filter_param, 59 uint32_t grace_period, 60 bool reset); 61 62 static int map_queues_cpsch(struct device_queue_manager *dqm); 63 64 static void deallocate_sdma_queue(struct device_queue_manager *dqm, 65 struct queue *q); 66 67 static inline void deallocate_hqd(struct device_queue_manager *dqm, 68 struct queue *q); 69 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q); 70 static int allocate_sdma_queue(struct device_queue_manager *dqm, 71 struct queue *q, const uint32_t *restore_sdma_id); 72 73 static int reset_queues_on_hws_hang(struct device_queue_manager *dqm, bool is_sdma); 74 75 static inline 76 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type) 77 { 78 if (type == KFD_QUEUE_TYPE_SDMA || type == KFD_QUEUE_TYPE_SDMA_XGMI) 79 return KFD_MQD_TYPE_SDMA; 80 return KFD_MQD_TYPE_CP; 81 } 82 83 static bool is_pipe_enabled(struct device_queue_manager *dqm, int mec, int pipe) 84 { 85 int i; 86 int pipe_offset = (mec * dqm->dev->kfd->shared_resources.num_pipe_per_mec 87 + pipe) * dqm->dev->kfd->shared_resources.num_queue_per_pipe; 88 89 /* queue is available for KFD usage if bit is 1 */ 90 for (i = 0; i < dqm->dev->kfd->shared_resources.num_queue_per_pipe; ++i) 91 if (test_bit(pipe_offset + i, 92 dqm->dev->kfd->shared_resources.cp_queue_bitmap)) 93 return true; 94 return false; 95 } 96 97 unsigned int get_cp_queues_num(struct device_queue_manager *dqm) 98 { 99 return bitmap_weight(dqm->dev->kfd->shared_resources.cp_queue_bitmap, 100 AMDGPU_MAX_QUEUES); 101 } 102 103 unsigned int get_queues_per_pipe(struct device_queue_manager *dqm) 104 { 105 return dqm->dev->kfd->shared_resources.num_queue_per_pipe; 106 } 107 108 unsigned int get_pipes_per_mec(struct device_queue_manager *dqm) 109 { 110 return dqm->dev->kfd->shared_resources.num_pipe_per_mec; 111 } 112 113 static unsigned int get_num_all_sdma_engines(struct device_queue_manager *dqm) 114 { 115 return kfd_get_num_sdma_engines(dqm->dev) + 116 kfd_get_num_xgmi_sdma_engines(dqm->dev); 117 } 118 119 unsigned int get_num_sdma_queues(struct device_queue_manager *dqm) 120 { 121 return kfd_get_num_sdma_engines(dqm->dev) * 122 dqm->dev->kfd->device_info.num_sdma_queues_per_engine; 123 } 124 125 unsigned int get_num_xgmi_sdma_queues(struct device_queue_manager *dqm) 126 { 127 return kfd_get_num_xgmi_sdma_engines(dqm->dev) * 128 dqm->dev->kfd->device_info.num_sdma_queues_per_engine; 129 } 130 131 static void init_sdma_bitmaps(struct device_queue_manager *dqm) 132 { 133 bitmap_zero(dqm->sdma_bitmap, KFD_MAX_SDMA_QUEUES); 134 bitmap_set(dqm->sdma_bitmap, 0, get_num_sdma_queues(dqm)); 135 136 bitmap_zero(dqm->xgmi_sdma_bitmap, KFD_MAX_SDMA_QUEUES); 137 bitmap_set(dqm->xgmi_sdma_bitmap, 0, get_num_xgmi_sdma_queues(dqm)); 138 139 /* Mask out the reserved queues */ 140 bitmap_clear(dqm->sdma_bitmap, 0, kfd_get_num_sdma_engines(dqm->dev) * 141 dqm->dev->kfd->device_info.num_reserved_sdma_queues_per_engine); 142 bitmap_clear(dqm->xgmi_sdma_bitmap, 0, kfd_get_num_xgmi_sdma_engines(dqm->dev) * 143 dqm->dev->kfd->device_info.num_reserved_sdma_queues_per_engine); 144 } 145 146 void program_sh_mem_settings(struct device_queue_manager *dqm, 147 struct qcm_process_device *qpd) 148 { 149 uint32_t xcc_mask = dqm->dev->xcc_mask; 150 int xcc_id; 151 152 for_each_inst(xcc_id, xcc_mask) 153 dqm->dev->kfd2kgd->program_sh_mem_settings( 154 dqm->dev->adev, qpd->vmid, qpd->sh_mem_config, 155 qpd->sh_mem_ape1_base, qpd->sh_mem_ape1_limit, 156 qpd->sh_mem_bases, xcc_id); 157 } 158 159 static void kfd_hws_hang(struct device_queue_manager *dqm) 160 { 161 struct device_process_node *cur; 162 struct qcm_process_device *qpd; 163 struct queue *q; 164 165 /* Mark all device queues as reset. */ 166 list_for_each_entry(cur, &dqm->queues, list) { 167 qpd = cur->qpd; 168 list_for_each_entry(q, &qpd->queues_list, list) { 169 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 170 171 pdd->has_reset_queue = true; 172 } 173 } 174 175 /* 176 * Issue a GPU reset if HWS is unresponsive 177 */ 178 amdgpu_amdkfd_gpu_reset(dqm->dev->adev); 179 } 180 181 static int convert_to_mes_queue_type(int queue_type) 182 { 183 int mes_queue_type; 184 185 switch (queue_type) { 186 case KFD_QUEUE_TYPE_COMPUTE: 187 mes_queue_type = MES_QUEUE_TYPE_COMPUTE; 188 break; 189 case KFD_QUEUE_TYPE_SDMA: 190 mes_queue_type = MES_QUEUE_TYPE_SDMA; 191 break; 192 default: 193 WARN(1, "Invalid queue type %d", queue_type); 194 mes_queue_type = -EINVAL; 195 break; 196 } 197 198 return mes_queue_type; 199 } 200 201 static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q, 202 struct qcm_process_device *qpd) 203 { 204 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev; 205 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 206 struct mes_add_queue_input queue_input; 207 int r, queue_type; 208 uint64_t wptr_addr_off; 209 210 if (!dqm->sched_running || dqm->sched_halt) 211 return 0; 212 if (!down_read_trylock(&adev->reset_domain->sem)) 213 return -EIO; 214 215 memset(&queue_input, 0x0, sizeof(struct mes_add_queue_input)); 216 queue_input.process_id = pdd->pasid; 217 queue_input.page_table_base_addr = qpd->page_table_base; 218 queue_input.process_va_start = 0; 219 queue_input.process_va_end = adev->vm_manager.max_pfn - 1; 220 /* MES unit for quantum is 100ns */ 221 queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM; /* Equivalent to 10ms. */ 222 queue_input.process_context_addr = pdd->proc_ctx_gpu_addr; 223 queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */ 224 queue_input.gang_context_addr = q->gang_ctx_gpu_addr; 225 queue_input.inprocess_gang_priority = q->properties.priority; 226 queue_input.gang_global_priority_level = 227 AMDGPU_MES_PRIORITY_LEVEL_NORMAL; 228 queue_input.doorbell_offset = q->properties.doorbell_off; 229 queue_input.mqd_addr = q->gart_mqd_addr; 230 queue_input.wptr_addr = (uint64_t)q->properties.write_ptr; 231 232 wptr_addr_off = (uint64_t)q->properties.write_ptr & (PAGE_SIZE - 1); 233 queue_input.wptr_mc_addr = amdgpu_bo_gpu_offset(q->properties.wptr_bo) + wptr_addr_off; 234 235 queue_input.is_kfd_process = 1; 236 queue_input.is_aql_queue = (q->properties.format == KFD_QUEUE_FORMAT_AQL); 237 queue_input.queue_size = q->properties.queue_size >> 2; 238 239 queue_input.paging = false; 240 queue_input.tba_addr = qpd->tba_addr; 241 queue_input.tma_addr = qpd->tma_addr; 242 queue_input.trap_en = !kfd_dbg_has_cwsr_workaround(q->device); 243 queue_input.skip_process_ctx_clear = 244 qpd->pqm->process->runtime_info.runtime_state == DEBUG_RUNTIME_STATE_ENABLED && 245 (qpd->pqm->process->debug_trap_enabled || 246 kfd_dbg_has_ttmps_always_setup(q->device)); 247 248 queue_type = convert_to_mes_queue_type(q->properties.type); 249 if (queue_type < 0) { 250 dev_err(adev->dev, "Queue type not supported with MES, queue:%d\n", 251 q->properties.type); 252 up_read(&adev->reset_domain->sem); 253 return -EINVAL; 254 } 255 queue_input.queue_type = (uint32_t)queue_type; 256 257 queue_input.exclusively_scheduled = q->properties.is_gws; 258 259 amdgpu_mes_lock(&adev->mes); 260 r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input); 261 amdgpu_mes_unlock(&adev->mes); 262 up_read(&adev->reset_domain->sem); 263 if (r) { 264 dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n", 265 q->properties.doorbell_off); 266 dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n"); 267 kfd_hws_hang(dqm); 268 } 269 270 return r; 271 } 272 273 static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q, 274 struct qcm_process_device *qpd) 275 { 276 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev; 277 int r; 278 struct mes_remove_queue_input queue_input; 279 280 if (!dqm->sched_running || dqm->sched_halt) 281 return 0; 282 if (!down_read_trylock(&adev->reset_domain->sem)) 283 return -EIO; 284 285 memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input)); 286 queue_input.doorbell_offset = q->properties.doorbell_off; 287 queue_input.gang_context_addr = q->gang_ctx_gpu_addr; 288 289 amdgpu_mes_lock(&adev->mes); 290 r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input); 291 amdgpu_mes_unlock(&adev->mes); 292 up_read(&adev->reset_domain->sem); 293 294 if (r) { 295 dev_err(adev->dev, "failed to remove hardware queue from MES, doorbell=0x%x\n", 296 q->properties.doorbell_off); 297 dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n"); 298 kfd_hws_hang(dqm); 299 } 300 301 return r; 302 } 303 304 static int remove_all_kfd_queues_mes(struct device_queue_manager *dqm) 305 { 306 struct device_process_node *cur; 307 struct device *dev = dqm->dev->adev->dev; 308 struct qcm_process_device *qpd; 309 struct queue *q; 310 int retval = 0; 311 312 list_for_each_entry(cur, &dqm->queues, list) { 313 qpd = cur->qpd; 314 list_for_each_entry(q, &qpd->queues_list, list) { 315 if (q->properties.is_active) { 316 retval = remove_queue_mes(dqm, q, qpd); 317 if (retval) { 318 dev_err(dev, "%s: Failed to remove queue %d for dev %d", 319 __func__, 320 q->properties.queue_id, 321 dqm->dev->id); 322 return retval; 323 } 324 } 325 } 326 } 327 328 return retval; 329 } 330 331 static int add_all_kfd_queues_mes(struct device_queue_manager *dqm) 332 { 333 struct device_process_node *cur; 334 struct device *dev = dqm->dev->adev->dev; 335 struct qcm_process_device *qpd; 336 struct queue *q; 337 int retval = 0; 338 339 list_for_each_entry(cur, &dqm->queues, list) { 340 qpd = cur->qpd; 341 list_for_each_entry(q, &qpd->queues_list, list) { 342 if (!q->properties.is_active) 343 continue; 344 retval = add_queue_mes(dqm, q, qpd); 345 if (retval) { 346 dev_err(dev, "%s: Failed to add queue %d for dev %d", 347 __func__, 348 q->properties.queue_id, 349 dqm->dev->id); 350 return retval; 351 } 352 } 353 } 354 355 return retval; 356 } 357 358 static int suspend_all_queues_mes(struct device_queue_manager *dqm) 359 { 360 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev; 361 int r = 0; 362 363 if (!down_read_trylock(&adev->reset_domain->sem)) 364 return -EIO; 365 366 r = amdgpu_mes_suspend(adev); 367 up_read(&adev->reset_domain->sem); 368 369 if (r) { 370 dev_err(adev->dev, "failed to suspend gangs from MES\n"); 371 dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n"); 372 kfd_hws_hang(dqm); 373 } 374 375 return r; 376 } 377 378 static int resume_all_queues_mes(struct device_queue_manager *dqm) 379 { 380 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev; 381 int r = 0; 382 383 if (!down_read_trylock(&adev->reset_domain->sem)) 384 return -EIO; 385 386 r = amdgpu_mes_resume(adev); 387 up_read(&adev->reset_domain->sem); 388 389 if (r) { 390 dev_err(adev->dev, "failed to resume gangs from MES\n"); 391 dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n"); 392 kfd_hws_hang(dqm); 393 } 394 395 return r; 396 } 397 398 static void increment_queue_count(struct device_queue_manager *dqm, 399 struct qcm_process_device *qpd, 400 struct queue *q) 401 { 402 dqm->active_queue_count++; 403 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE || 404 q->properties.type == KFD_QUEUE_TYPE_DIQ) 405 dqm->active_cp_queue_count++; 406 407 if (q->properties.is_gws) { 408 dqm->gws_queue_count++; 409 qpd->mapped_gws_queue = true; 410 } 411 } 412 413 static void decrement_queue_count(struct device_queue_manager *dqm, 414 struct qcm_process_device *qpd, 415 struct queue *q) 416 { 417 dqm->active_queue_count--; 418 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE || 419 q->properties.type == KFD_QUEUE_TYPE_DIQ) 420 dqm->active_cp_queue_count--; 421 422 if (q->properties.is_gws) { 423 dqm->gws_queue_count--; 424 qpd->mapped_gws_queue = false; 425 } 426 } 427 428 /* 429 * Allocate a doorbell ID to this queue. 430 * If doorbell_id is passed in, make sure requested ID is valid then allocate it. 431 */ 432 static int allocate_doorbell(struct qcm_process_device *qpd, 433 struct queue *q, 434 uint32_t const *restore_id) 435 { 436 struct kfd_node *dev = qpd->dqm->dev; 437 438 if (!KFD_IS_SOC15(dev)) { 439 /* On pre-SOC15 chips we need to use the queue ID to 440 * preserve the user mode ABI. 441 */ 442 443 if (restore_id && *restore_id != q->properties.queue_id) 444 return -EINVAL; 445 446 q->doorbell_id = q->properties.queue_id; 447 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 448 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 449 /* For SDMA queues on SOC15 with 8-byte doorbell, use static 450 * doorbell assignments based on the engine and queue id. 451 * The doobell index distance between RLC (2*i) and (2*i+1) 452 * for a SDMA engine is 512. 453 */ 454 455 uint32_t *idx_offset = dev->kfd->shared_resources.sdma_doorbell_idx; 456 457 /* 458 * q->properties.sdma_engine_id corresponds to the virtual 459 * sdma engine number. However, for doorbell allocation, 460 * we need the physical sdma engine id in order to get the 461 * correct doorbell offset. 462 */ 463 uint32_t valid_id = idx_offset[qpd->dqm->dev->node_id * 464 get_num_all_sdma_engines(qpd->dqm) + 465 q->properties.sdma_engine_id] 466 + (q->properties.sdma_queue_id & 1) 467 * KFD_QUEUE_DOORBELL_MIRROR_OFFSET 468 + (q->properties.sdma_queue_id >> 1); 469 470 if (restore_id && *restore_id != valid_id) 471 return -EINVAL; 472 q->doorbell_id = valid_id; 473 } else { 474 /* For CP queues on SOC15 */ 475 if (restore_id) { 476 /* make sure that ID is free */ 477 if (__test_and_set_bit(*restore_id, qpd->doorbell_bitmap)) 478 return -EINVAL; 479 480 q->doorbell_id = *restore_id; 481 } else { 482 /* or reserve a free doorbell ID */ 483 unsigned int found; 484 485 found = find_first_zero_bit(qpd->doorbell_bitmap, 486 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS); 487 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) { 488 pr_debug("No doorbells available"); 489 return -EBUSY; 490 } 491 set_bit(found, qpd->doorbell_bitmap); 492 q->doorbell_id = found; 493 } 494 } 495 496 q->properties.doorbell_off = amdgpu_doorbell_index_on_bar(dev->adev, 497 qpd->proc_doorbells, 498 q->doorbell_id, 499 dev->kfd->device_info.doorbell_size); 500 return 0; 501 } 502 503 static void deallocate_doorbell(struct qcm_process_device *qpd, 504 struct queue *q) 505 { 506 unsigned int old; 507 struct kfd_node *dev = qpd->dqm->dev; 508 509 if (!KFD_IS_SOC15(dev) || 510 q->properties.type == KFD_QUEUE_TYPE_SDMA || 511 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 512 return; 513 514 old = test_and_clear_bit(q->doorbell_id, qpd->doorbell_bitmap); 515 WARN_ON(!old); 516 } 517 518 static void program_trap_handler_settings(struct device_queue_manager *dqm, 519 struct qcm_process_device *qpd) 520 { 521 uint32_t xcc_mask = dqm->dev->xcc_mask; 522 int xcc_id; 523 524 if (dqm->dev->kfd2kgd->program_trap_handler_settings) 525 for_each_inst(xcc_id, xcc_mask) 526 dqm->dev->kfd2kgd->program_trap_handler_settings( 527 dqm->dev->adev, qpd->vmid, qpd->tba_addr, 528 qpd->tma_addr, xcc_id); 529 } 530 531 static int allocate_vmid(struct device_queue_manager *dqm, 532 struct qcm_process_device *qpd, 533 struct queue *q) 534 { 535 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 536 struct device *dev = dqm->dev->adev->dev; 537 int allocated_vmid = -1, i; 538 539 for (i = dqm->dev->vm_info.first_vmid_kfd; 540 i <= dqm->dev->vm_info.last_vmid_kfd; i++) { 541 if (!dqm->vmid_pasid[i]) { 542 allocated_vmid = i; 543 break; 544 } 545 } 546 547 if (allocated_vmid < 0) { 548 dev_err(dev, "no more vmid to allocate\n"); 549 return -ENOSPC; 550 } 551 552 pr_debug("vmid allocated: %d\n", allocated_vmid); 553 554 dqm->vmid_pasid[allocated_vmid] = pdd->pasid; 555 556 set_pasid_vmid_mapping(dqm, pdd->pasid, allocated_vmid); 557 558 qpd->vmid = allocated_vmid; 559 q->properties.vmid = allocated_vmid; 560 561 program_sh_mem_settings(dqm, qpd); 562 563 if (KFD_IS_SOC15(dqm->dev) && dqm->dev->kfd->cwsr_enabled) 564 program_trap_handler_settings(dqm, qpd); 565 566 /* qpd->page_table_base is set earlier when register_process() 567 * is called, i.e. when the first queue is created. 568 */ 569 dqm->dev->kfd2kgd->set_vm_context_page_table_base(dqm->dev->adev, 570 qpd->vmid, 571 qpd->page_table_base); 572 /* invalidate the VM context after pasid and vmid mapping is set up */ 573 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY); 574 575 if (dqm->dev->kfd2kgd->set_scratch_backing_va) 576 dqm->dev->kfd2kgd->set_scratch_backing_va(dqm->dev->adev, 577 qpd->sh_hidden_private_base, qpd->vmid); 578 579 return 0; 580 } 581 582 static int flush_texture_cache_nocpsch(struct kfd_node *kdev, 583 struct qcm_process_device *qpd) 584 { 585 const struct packet_manager_funcs *pmf = qpd->dqm->packet_mgr.pmf; 586 int ret; 587 588 if (!qpd->ib_kaddr) 589 return -ENOMEM; 590 591 ret = pmf->release_mem(qpd->ib_base, (uint32_t *)qpd->ib_kaddr); 592 if (ret) 593 return ret; 594 595 return amdgpu_amdkfd_submit_ib(kdev->adev, KGD_ENGINE_MEC1, qpd->vmid, 596 qpd->ib_base, (uint32_t *)qpd->ib_kaddr, 597 pmf->release_mem_size / sizeof(uint32_t)); 598 } 599 600 static void deallocate_vmid(struct device_queue_manager *dqm, 601 struct qcm_process_device *qpd, 602 struct queue *q) 603 { 604 struct device *dev = dqm->dev->adev->dev; 605 606 /* On GFX v7, CP doesn't flush TC at dequeue */ 607 if (q->device->adev->asic_type == CHIP_HAWAII) 608 if (flush_texture_cache_nocpsch(q->device, qpd)) 609 dev_err(dev, "Failed to flush TC\n"); 610 611 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY); 612 613 /* Release the vmid mapping */ 614 set_pasid_vmid_mapping(dqm, 0, qpd->vmid); 615 dqm->vmid_pasid[qpd->vmid] = 0; 616 617 qpd->vmid = 0; 618 q->properties.vmid = 0; 619 } 620 621 static int create_queue_nocpsch(struct device_queue_manager *dqm, 622 struct queue *q, 623 struct qcm_process_device *qpd, 624 const struct kfd_criu_queue_priv_data *qd, 625 const void *restore_mqd, const void *restore_ctl_stack) 626 { 627 struct mqd_manager *mqd_mgr; 628 int retval; 629 630 dqm_lock(dqm); 631 632 if (dqm->total_queue_count >= max_num_of_queues_per_device) { 633 pr_warn("Can't create new usermode queue because %d queues were already created\n", 634 dqm->total_queue_count); 635 retval = -EPERM; 636 goto out_unlock; 637 } 638 639 if (list_empty(&qpd->queues_list)) { 640 retval = allocate_vmid(dqm, qpd, q); 641 if (retval) 642 goto out_unlock; 643 } 644 q->properties.vmid = qpd->vmid; 645 /* 646 * Eviction state logic: mark all queues as evicted, even ones 647 * not currently active. Restoring inactive queues later only 648 * updates the is_evicted flag but is a no-op otherwise. 649 */ 650 q->properties.is_evicted = !!qpd->evicted; 651 652 q->properties.tba_addr = qpd->tba_addr; 653 q->properties.tma_addr = qpd->tma_addr; 654 655 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 656 q->properties.type)]; 657 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) { 658 retval = allocate_hqd(dqm, q); 659 if (retval) 660 goto deallocate_vmid; 661 pr_debug("Loading mqd to hqd on pipe %d, queue %d\n", 662 q->pipe, q->queue); 663 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 664 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 665 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL); 666 if (retval) 667 goto deallocate_vmid; 668 dqm->asic_ops.init_sdma_vm(dqm, q, qpd); 669 } 670 671 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL); 672 if (retval) 673 goto out_deallocate_hqd; 674 675 /* Temporarily release dqm lock to avoid a circular lock dependency */ 676 dqm_unlock(dqm); 677 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties); 678 dqm_lock(dqm); 679 680 if (!q->mqd_mem_obj) { 681 retval = -ENOMEM; 682 goto out_deallocate_doorbell; 683 } 684 685 if (qd) 686 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr, 687 &q->properties, restore_mqd, restore_ctl_stack, 688 qd->ctl_stack_size); 689 else 690 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, 691 &q->gart_mqd_addr, &q->properties); 692 693 if (q->properties.is_active) { 694 if (!dqm->sched_running) { 695 WARN_ONCE(1, "Load non-HWS mqd while stopped\n"); 696 goto add_queue_to_list; 697 } 698 699 if (WARN(q->process->mm != current->mm, 700 "should only run in user thread")) 701 retval = -EFAULT; 702 else 703 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe, 704 q->queue, &q->properties, current->mm); 705 if (retval) 706 goto out_free_mqd; 707 } 708 709 add_queue_to_list: 710 list_add(&q->list, &qpd->queues_list); 711 qpd->queue_count++; 712 if (q->properties.is_active) 713 increment_queue_count(dqm, qpd, q); 714 715 /* 716 * Unconditionally increment this counter, regardless of the queue's 717 * type or whether the queue is active. 718 */ 719 dqm->total_queue_count++; 720 pr_debug("Total of %d queues are accountable so far\n", 721 dqm->total_queue_count); 722 goto out_unlock; 723 724 out_free_mqd: 725 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 726 out_deallocate_doorbell: 727 deallocate_doorbell(qpd, q); 728 out_deallocate_hqd: 729 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) 730 deallocate_hqd(dqm, q); 731 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 732 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 733 deallocate_sdma_queue(dqm, q); 734 deallocate_vmid: 735 if (list_empty(&qpd->queues_list)) 736 deallocate_vmid(dqm, qpd, q); 737 out_unlock: 738 dqm_unlock(dqm); 739 return retval; 740 } 741 742 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q) 743 { 744 bool set; 745 int pipe, bit, i; 746 747 set = false; 748 749 for (pipe = dqm->next_pipe_to_allocate, i = 0; 750 i < get_pipes_per_mec(dqm); 751 pipe = ((pipe + 1) % get_pipes_per_mec(dqm)), ++i) { 752 753 if (!is_pipe_enabled(dqm, 0, pipe)) 754 continue; 755 756 if (dqm->allocated_queues[pipe] != 0) { 757 bit = ffs(dqm->allocated_queues[pipe]) - 1; 758 dqm->allocated_queues[pipe] &= ~(1 << bit); 759 q->pipe = pipe; 760 q->queue = bit; 761 set = true; 762 break; 763 } 764 } 765 766 if (!set) 767 return -EBUSY; 768 769 pr_debug("hqd slot - pipe %d, queue %d\n", q->pipe, q->queue); 770 /* horizontal hqd allocation */ 771 dqm->next_pipe_to_allocate = (pipe + 1) % get_pipes_per_mec(dqm); 772 773 return 0; 774 } 775 776 static inline void deallocate_hqd(struct device_queue_manager *dqm, 777 struct queue *q) 778 { 779 dqm->allocated_queues[q->pipe] |= (1 << q->queue); 780 } 781 782 #define SQ_IND_CMD_CMD_KILL 0x00000003 783 #define SQ_IND_CMD_MODE_BROADCAST 0x00000001 784 785 static int dbgdev_wave_reset_wavefronts(struct kfd_node *dev, struct kfd_process *p) 786 { 787 int status = 0; 788 unsigned int vmid; 789 uint16_t queried_pasid; 790 union SQ_CMD_BITS reg_sq_cmd; 791 union GRBM_GFX_INDEX_BITS reg_gfx_index; 792 struct kfd_process_device *pdd; 793 int first_vmid_to_scan = dev->vm_info.first_vmid_kfd; 794 int last_vmid_to_scan = dev->vm_info.last_vmid_kfd; 795 uint32_t xcc_mask = dev->xcc_mask; 796 int xcc_id; 797 798 reg_sq_cmd.u32All = 0; 799 reg_gfx_index.u32All = 0; 800 801 pr_debug("Killing all process wavefronts\n"); 802 803 if (!dev->kfd2kgd->get_atc_vmid_pasid_mapping_info) { 804 dev_err(dev->adev->dev, "no vmid pasid mapping supported\n"); 805 return -EOPNOTSUPP; 806 } 807 808 /* taking the VMID for that process on the safe way using PDD */ 809 pdd = kfd_get_process_device_data(dev, p); 810 if (!pdd) 811 return -EFAULT; 812 813 /* Scan all registers in the range ATC_VMID8_PASID_MAPPING .. 814 * ATC_VMID15_PASID_MAPPING 815 * to check which VMID the current process is mapped to. 816 */ 817 818 for (vmid = first_vmid_to_scan; vmid <= last_vmid_to_scan; vmid++) { 819 status = dev->kfd2kgd->get_atc_vmid_pasid_mapping_info 820 (dev->adev, vmid, &queried_pasid); 821 822 if (status && queried_pasid == pdd->pasid) { 823 pr_debug("Killing wave fronts of vmid %d and process pid %d\n", 824 vmid, p->lead_thread->pid); 825 break; 826 } 827 } 828 829 if (vmid > last_vmid_to_scan) { 830 dev_err(dev->adev->dev, "Didn't find vmid for process pid %d\n", 831 p->lead_thread->pid); 832 return -EFAULT; 833 } 834 835 reg_gfx_index.bits.sh_broadcast_writes = 1; 836 reg_gfx_index.bits.se_broadcast_writes = 1; 837 reg_gfx_index.bits.instance_broadcast_writes = 1; 838 reg_sq_cmd.bits.mode = SQ_IND_CMD_MODE_BROADCAST; 839 reg_sq_cmd.bits.cmd = SQ_IND_CMD_CMD_KILL; 840 reg_sq_cmd.bits.vm_id = vmid; 841 842 for_each_inst(xcc_id, xcc_mask) 843 dev->kfd2kgd->wave_control_execute( 844 dev->adev, reg_gfx_index.u32All, 845 reg_sq_cmd.u32All, xcc_id); 846 847 return 0; 848 } 849 850 /* Access to DQM has to be locked before calling destroy_queue_nocpsch_locked 851 * to avoid asynchronized access 852 */ 853 static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, 854 struct qcm_process_device *qpd, 855 struct queue *q) 856 { 857 int retval; 858 struct mqd_manager *mqd_mgr; 859 860 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)]; 861 862 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) 863 deallocate_hqd(dqm, q); 864 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) 865 deallocate_sdma_queue(dqm, q); 866 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 867 deallocate_sdma_queue(dqm, q); 868 else { 869 pr_debug("q->properties.type %d is invalid\n", 870 q->properties.type); 871 return -EINVAL; 872 } 873 dqm->total_queue_count--; 874 875 deallocate_doorbell(qpd, q); 876 877 if (!dqm->sched_running) { 878 WARN_ONCE(1, "Destroy non-HWS queue while stopped\n"); 879 return 0; 880 } 881 882 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd, 883 KFD_PREEMPT_TYPE_WAVEFRONT_RESET, 884 KFD_UNMAP_LATENCY_MS, 885 q->pipe, q->queue); 886 if (retval == -ETIME) 887 qpd->reset_wavefronts = true; 888 889 list_del(&q->list); 890 if (list_empty(&qpd->queues_list)) { 891 if (qpd->reset_wavefronts) { 892 pr_warn("Resetting wave fronts (nocpsch) on dev %p\n", 893 dqm->dev); 894 /* dbgdev_wave_reset_wavefronts has to be called before 895 * deallocate_vmid(), i.e. when vmid is still in use. 896 */ 897 dbgdev_wave_reset_wavefronts(dqm->dev, 898 qpd->pqm->process); 899 qpd->reset_wavefronts = false; 900 } 901 902 deallocate_vmid(dqm, qpd, q); 903 } 904 qpd->queue_count--; 905 if (q->properties.is_active) 906 decrement_queue_count(dqm, qpd, q); 907 908 return retval; 909 } 910 911 static int destroy_queue_nocpsch(struct device_queue_manager *dqm, 912 struct qcm_process_device *qpd, 913 struct queue *q) 914 { 915 int retval; 916 uint64_t sdma_val = 0; 917 struct device *dev = dqm->dev->adev->dev; 918 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 919 struct mqd_manager *mqd_mgr = 920 dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)]; 921 922 /* Get the SDMA queue stats */ 923 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || 924 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 925 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr, 926 &sdma_val); 927 if (retval) 928 dev_err(dev, "Failed to read SDMA queue counter for queue: %d\n", 929 q->properties.queue_id); 930 } 931 932 dqm_lock(dqm); 933 retval = destroy_queue_nocpsch_locked(dqm, qpd, q); 934 if (!retval) 935 pdd->sdma_past_activity_counter += sdma_val; 936 dqm_unlock(dqm); 937 938 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 939 940 return retval; 941 } 942 943 static int update_queue(struct device_queue_manager *dqm, struct queue *q, 944 struct mqd_update_info *minfo) 945 { 946 int retval = 0; 947 struct device *dev = dqm->dev->adev->dev; 948 struct mqd_manager *mqd_mgr; 949 struct kfd_process_device *pdd; 950 bool prev_active = false; 951 952 dqm_lock(dqm); 953 pdd = kfd_get_process_device_data(q->device, q->process); 954 if (!pdd) { 955 retval = -ENODEV; 956 goto out_unlock; 957 } 958 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 959 q->properties.type)]; 960 961 /* Save previous activity state for counters */ 962 prev_active = q->properties.is_active; 963 964 /* Make sure the queue is unmapped before updating the MQD */ 965 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) { 966 if (!dqm->dev->kfd->shared_resources.enable_mes) 967 retval = unmap_queues_cpsch(dqm, 968 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD, false); 969 else if (prev_active) 970 retval = remove_queue_mes(dqm, q, &pdd->qpd); 971 972 /* queue is reset so inaccessable */ 973 if (pdd->has_reset_queue) { 974 retval = -EACCES; 975 goto out_unlock; 976 } 977 978 if (retval) { 979 dev_err(dev, "unmap queue failed\n"); 980 goto out_unlock; 981 } 982 } else if (prev_active && 983 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE || 984 q->properties.type == KFD_QUEUE_TYPE_SDMA || 985 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 986 987 if (!dqm->sched_running) { 988 WARN_ONCE(1, "Update non-HWS queue while stopped\n"); 989 goto out_unlock; 990 } 991 992 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd, 993 (dqm->dev->kfd->cwsr_enabled ? 994 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE : 995 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN), 996 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue); 997 if (retval) { 998 dev_err(dev, "destroy mqd failed\n"); 999 goto out_unlock; 1000 } 1001 } 1002 1003 mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo); 1004 1005 /* 1006 * check active state vs. the previous state and modify 1007 * counter accordingly. map_queues_cpsch uses the 1008 * dqm->active_queue_count to determine whether a new runlist must be 1009 * uploaded. 1010 */ 1011 if (q->properties.is_active && !prev_active) { 1012 increment_queue_count(dqm, &pdd->qpd, q); 1013 } else if (!q->properties.is_active && prev_active) { 1014 decrement_queue_count(dqm, &pdd->qpd, q); 1015 } else if (q->gws && !q->properties.is_gws) { 1016 if (q->properties.is_active) { 1017 dqm->gws_queue_count++; 1018 pdd->qpd.mapped_gws_queue = true; 1019 } 1020 q->properties.is_gws = true; 1021 } else if (!q->gws && q->properties.is_gws) { 1022 if (q->properties.is_active) { 1023 dqm->gws_queue_count--; 1024 pdd->qpd.mapped_gws_queue = false; 1025 } 1026 q->properties.is_gws = false; 1027 } 1028 1029 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) { 1030 if (!dqm->dev->kfd->shared_resources.enable_mes) 1031 retval = map_queues_cpsch(dqm); 1032 else if (q->properties.is_active) 1033 retval = add_queue_mes(dqm, q, &pdd->qpd); 1034 } else if (q->properties.is_active && 1035 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE || 1036 q->properties.type == KFD_QUEUE_TYPE_SDMA || 1037 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 1038 if (WARN(q->process->mm != current->mm, 1039 "should only run in user thread")) 1040 retval = -EFAULT; 1041 else 1042 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, 1043 q->pipe, q->queue, 1044 &q->properties, current->mm); 1045 } 1046 1047 out_unlock: 1048 dqm_unlock(dqm); 1049 return retval; 1050 } 1051 1052 /* suspend_single_queue does not lock the dqm like the 1053 * evict_process_queues_cpsch or evict_process_queues_nocpsch. You should 1054 * lock the dqm before calling, and unlock after calling. 1055 * 1056 * The reason we don't lock the dqm is because this function may be 1057 * called on multiple queues in a loop, so rather than locking/unlocking 1058 * multiple times, we will just keep the dqm locked for all of the calls. 1059 */ 1060 static int suspend_single_queue(struct device_queue_manager *dqm, 1061 struct kfd_process_device *pdd, 1062 struct queue *q) 1063 { 1064 bool is_new; 1065 1066 if (q->properties.is_suspended) 1067 return 0; 1068 1069 pr_debug("Suspending process pid %d queue [%i]\n", 1070 pdd->process->lead_thread->pid, 1071 q->properties.queue_id); 1072 1073 is_new = q->properties.exception_status & KFD_EC_MASK(EC_QUEUE_NEW); 1074 1075 if (is_new || q->properties.is_being_destroyed) { 1076 pr_debug("Suspend: skip %s queue id %i\n", 1077 is_new ? "new" : "destroyed", 1078 q->properties.queue_id); 1079 return -EBUSY; 1080 } 1081 1082 q->properties.is_suspended = true; 1083 if (q->properties.is_active) { 1084 if (dqm->dev->kfd->shared_resources.enable_mes) { 1085 int r = remove_queue_mes(dqm, q, &pdd->qpd); 1086 1087 if (r) 1088 return r; 1089 } 1090 1091 decrement_queue_count(dqm, &pdd->qpd, q); 1092 q->properties.is_active = false; 1093 } 1094 1095 return 0; 1096 } 1097 1098 /* resume_single_queue does not lock the dqm like the functions 1099 * restore_process_queues_cpsch or restore_process_queues_nocpsch. You should 1100 * lock the dqm before calling, and unlock after calling. 1101 * 1102 * The reason we don't lock the dqm is because this function may be 1103 * called on multiple queues in a loop, so rather than locking/unlocking 1104 * multiple times, we will just keep the dqm locked for all of the calls. 1105 */ 1106 static int resume_single_queue(struct device_queue_manager *dqm, 1107 struct qcm_process_device *qpd, 1108 struct queue *q) 1109 { 1110 struct kfd_process_device *pdd; 1111 1112 if (!q->properties.is_suspended) 1113 return 0; 1114 1115 pdd = qpd_to_pdd(qpd); 1116 1117 pr_debug("Restoring from suspend process pid %d queue [%i]\n", 1118 pdd->process->lead_thread->pid, 1119 q->properties.queue_id); 1120 1121 q->properties.is_suspended = false; 1122 1123 if (QUEUE_IS_ACTIVE(q->properties)) { 1124 if (dqm->dev->kfd->shared_resources.enable_mes) { 1125 int r = add_queue_mes(dqm, q, &pdd->qpd); 1126 1127 if (r) 1128 return r; 1129 } 1130 1131 q->properties.is_active = true; 1132 increment_queue_count(dqm, qpd, q); 1133 } 1134 1135 return 0; 1136 } 1137 1138 static int evict_process_queues_nocpsch(struct device_queue_manager *dqm, 1139 struct qcm_process_device *qpd) 1140 { 1141 struct queue *q; 1142 struct mqd_manager *mqd_mgr; 1143 struct kfd_process_device *pdd; 1144 int retval, ret = 0; 1145 1146 dqm_lock(dqm); 1147 if (qpd->evicted++ > 0) /* already evicted, do nothing */ 1148 goto out; 1149 1150 pdd = qpd_to_pdd(qpd); 1151 pr_debug_ratelimited("Evicting process pid %d queues\n", 1152 pdd->process->lead_thread->pid); 1153 1154 pdd->last_evict_timestamp = get_jiffies_64(); 1155 /* Mark all queues as evicted. Deactivate all active queues on 1156 * the qpd. 1157 */ 1158 list_for_each_entry(q, &qpd->queues_list, list) { 1159 q->properties.is_evicted = true; 1160 if (!q->properties.is_active) 1161 continue; 1162 1163 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 1164 q->properties.type)]; 1165 q->properties.is_active = false; 1166 decrement_queue_count(dqm, qpd, q); 1167 1168 if (WARN_ONCE(!dqm->sched_running, "Evict when stopped\n")) 1169 continue; 1170 1171 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd, 1172 (dqm->dev->kfd->cwsr_enabled ? 1173 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE : 1174 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN), 1175 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue); 1176 if (retval && !ret) 1177 /* Return the first error, but keep going to 1178 * maintain a consistent eviction state 1179 */ 1180 ret = retval; 1181 } 1182 1183 out: 1184 dqm_unlock(dqm); 1185 return ret; 1186 } 1187 1188 static int evict_process_queues_cpsch(struct device_queue_manager *dqm, 1189 struct qcm_process_device *qpd) 1190 { 1191 struct queue *q; 1192 struct device *dev = dqm->dev->adev->dev; 1193 struct kfd_process_device *pdd; 1194 int retval = 0; 1195 1196 dqm_lock(dqm); 1197 if (qpd->evicted++ > 0) /* already evicted, do nothing */ 1198 goto out; 1199 1200 pdd = qpd_to_pdd(qpd); 1201 1202 /* The debugger creates processes that temporarily have not acquired 1203 * all VMs for all devices and has no VMs itself. 1204 * Skip queue eviction on process eviction. 1205 */ 1206 if (!pdd->drm_priv) 1207 goto out; 1208 1209 pr_debug_ratelimited("Evicting process pid %d queues\n", 1210 pdd->process->lead_thread->pid); 1211 1212 if (dqm->dev->kfd->shared_resources.enable_mes) { 1213 pdd->last_evict_timestamp = get_jiffies_64(); 1214 retval = suspend_all_queues_mes(dqm); 1215 if (retval) { 1216 dev_err(dev, "Suspending all queues failed"); 1217 goto out; 1218 } 1219 } 1220 1221 /* Mark all queues as evicted. Deactivate all active queues on 1222 * the qpd. 1223 */ 1224 list_for_each_entry(q, &qpd->queues_list, list) { 1225 q->properties.is_evicted = true; 1226 if (!q->properties.is_active) 1227 continue; 1228 1229 q->properties.is_active = false; 1230 decrement_queue_count(dqm, qpd, q); 1231 1232 if (dqm->dev->kfd->shared_resources.enable_mes) { 1233 retval = remove_queue_mes(dqm, q, qpd); 1234 if (retval) { 1235 dev_err(dev, "Failed to evict queue %d\n", 1236 q->properties.queue_id); 1237 goto out; 1238 } 1239 } 1240 } 1241 1242 if (!dqm->dev->kfd->shared_resources.enable_mes) { 1243 pdd->last_evict_timestamp = get_jiffies_64(); 1244 retval = execute_queues_cpsch(dqm, 1245 qpd->is_debug ? 1246 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES : 1247 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, 1248 USE_DEFAULT_GRACE_PERIOD); 1249 } else { 1250 retval = resume_all_queues_mes(dqm); 1251 if (retval) 1252 dev_err(dev, "Resuming all queues failed"); 1253 } 1254 1255 out: 1256 dqm_unlock(dqm); 1257 return retval; 1258 } 1259 1260 static int restore_process_queues_nocpsch(struct device_queue_manager *dqm, 1261 struct qcm_process_device *qpd) 1262 { 1263 struct mm_struct *mm = NULL; 1264 struct queue *q; 1265 struct mqd_manager *mqd_mgr; 1266 struct kfd_process_device *pdd; 1267 uint64_t pd_base; 1268 uint64_t eviction_duration; 1269 int retval, ret = 0; 1270 1271 pdd = qpd_to_pdd(qpd); 1272 /* Retrieve PD base */ 1273 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv); 1274 1275 dqm_lock(dqm); 1276 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */ 1277 goto out; 1278 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */ 1279 qpd->evicted--; 1280 goto out; 1281 } 1282 1283 pr_debug_ratelimited("Restoring process pid %d queues\n", 1284 pdd->process->lead_thread->pid); 1285 1286 /* Update PD Base in QPD */ 1287 qpd->page_table_base = pd_base; 1288 pr_debug("Updated PD address to 0x%llx\n", pd_base); 1289 1290 if (!list_empty(&qpd->queues_list)) { 1291 dqm->dev->kfd2kgd->set_vm_context_page_table_base( 1292 dqm->dev->adev, 1293 qpd->vmid, 1294 qpd->page_table_base); 1295 kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY); 1296 } 1297 1298 /* Take a safe reference to the mm_struct, which may otherwise 1299 * disappear even while the kfd_process is still referenced. 1300 */ 1301 mm = get_task_mm(pdd->process->lead_thread); 1302 if (!mm) { 1303 ret = -EFAULT; 1304 goto out; 1305 } 1306 1307 /* Remove the eviction flags. Activate queues that are not 1308 * inactive for other reasons. 1309 */ 1310 list_for_each_entry(q, &qpd->queues_list, list) { 1311 q->properties.is_evicted = false; 1312 if (!QUEUE_IS_ACTIVE(q->properties)) 1313 continue; 1314 1315 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 1316 q->properties.type)]; 1317 q->properties.is_active = true; 1318 increment_queue_count(dqm, qpd, q); 1319 1320 if (WARN_ONCE(!dqm->sched_running, "Restore when stopped\n")) 1321 continue; 1322 1323 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe, 1324 q->queue, &q->properties, mm); 1325 if (retval && !ret) 1326 /* Return the first error, but keep going to 1327 * maintain a consistent eviction state 1328 */ 1329 ret = retval; 1330 } 1331 qpd->evicted = 0; 1332 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp; 1333 atomic64_add(eviction_duration, &pdd->evict_duration_counter); 1334 out: 1335 if (mm) 1336 mmput(mm); 1337 dqm_unlock(dqm); 1338 return ret; 1339 } 1340 1341 static int restore_process_queues_cpsch(struct device_queue_manager *dqm, 1342 struct qcm_process_device *qpd) 1343 { 1344 struct queue *q; 1345 struct device *dev = dqm->dev->adev->dev; 1346 struct kfd_process_device *pdd; 1347 uint64_t eviction_duration; 1348 int retval = 0; 1349 1350 pdd = qpd_to_pdd(qpd); 1351 1352 dqm_lock(dqm); 1353 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */ 1354 goto out; 1355 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */ 1356 qpd->evicted--; 1357 goto out; 1358 } 1359 1360 /* The debugger creates processes that temporarily have not acquired 1361 * all VMs for all devices and has no VMs itself. 1362 * Skip queue restore on process restore. 1363 */ 1364 if (!pdd->drm_priv) 1365 goto vm_not_acquired; 1366 1367 pr_debug_ratelimited("Restoring process pid %d queues\n", 1368 pdd->process->lead_thread->pid); 1369 1370 /* Update PD Base in QPD */ 1371 qpd->page_table_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv); 1372 pr_debug("Updated PD address to 0x%llx\n", qpd->page_table_base); 1373 1374 /* activate all active queues on the qpd */ 1375 list_for_each_entry(q, &qpd->queues_list, list) { 1376 q->properties.is_evicted = false; 1377 if (!QUEUE_IS_ACTIVE(q->properties)) 1378 continue; 1379 1380 q->properties.is_active = true; 1381 increment_queue_count(dqm, &pdd->qpd, q); 1382 1383 if (dqm->dev->kfd->shared_resources.enable_mes) { 1384 retval = add_queue_mes(dqm, q, qpd); 1385 if (retval) { 1386 dev_err(dev, "Failed to restore queue %d\n", 1387 q->properties.queue_id); 1388 goto out; 1389 } 1390 } 1391 } 1392 if (!dqm->dev->kfd->shared_resources.enable_mes) 1393 retval = execute_queues_cpsch(dqm, 1394 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD); 1395 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp; 1396 atomic64_add(eviction_duration, &pdd->evict_duration_counter); 1397 vm_not_acquired: 1398 qpd->evicted = 0; 1399 out: 1400 dqm_unlock(dqm); 1401 return retval; 1402 } 1403 1404 static int register_process(struct device_queue_manager *dqm, 1405 struct qcm_process_device *qpd) 1406 { 1407 struct device_process_node *n; 1408 struct kfd_process_device *pdd; 1409 uint64_t pd_base; 1410 int retval; 1411 1412 n = kzalloc(sizeof(*n), GFP_KERNEL); 1413 if (!n) 1414 return -ENOMEM; 1415 1416 n->qpd = qpd; 1417 1418 pdd = qpd_to_pdd(qpd); 1419 /* Retrieve PD base */ 1420 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv); 1421 1422 dqm_lock(dqm); 1423 list_add(&n->list, &dqm->queues); 1424 1425 /* Update PD Base in QPD */ 1426 qpd->page_table_base = pd_base; 1427 pr_debug("Updated PD address to 0x%llx\n", pd_base); 1428 1429 retval = dqm->asic_ops.update_qpd(dqm, qpd); 1430 1431 dqm->processes_count++; 1432 1433 dqm_unlock(dqm); 1434 1435 /* Outside the DQM lock because under the DQM lock we can't do 1436 * reclaim or take other locks that others hold while reclaiming. 1437 */ 1438 kfd_inc_compute_active(dqm->dev); 1439 1440 return retval; 1441 } 1442 1443 static int unregister_process(struct device_queue_manager *dqm, 1444 struct qcm_process_device *qpd) 1445 { 1446 int retval = 0; 1447 struct device_process_node *cur, *next; 1448 1449 pr_debug("qpd->queues_list is %s\n", 1450 list_empty(&qpd->queues_list) ? "empty" : "not empty"); 1451 1452 dqm_lock(dqm); 1453 1454 list_for_each_entry_safe(cur, next, &dqm->queues, list) { 1455 if (qpd == cur->qpd) { 1456 list_del(&cur->list); 1457 kfree(cur); 1458 dqm->processes_count--; 1459 goto out; 1460 } 1461 } 1462 /* qpd not found in dqm list */ 1463 retval = 1; 1464 out: 1465 dqm_unlock(dqm); 1466 1467 /* Outside the DQM lock because under the DQM lock we can't do 1468 * reclaim or take other locks that others hold while reclaiming. 1469 */ 1470 if (!retval) 1471 kfd_dec_compute_active(dqm->dev); 1472 1473 return retval; 1474 } 1475 1476 static int 1477 set_pasid_vmid_mapping(struct device_queue_manager *dqm, u32 pasid, 1478 unsigned int vmid) 1479 { 1480 uint32_t xcc_mask = dqm->dev->xcc_mask; 1481 int xcc_id, ret = 0; 1482 1483 for_each_inst(xcc_id, xcc_mask) { 1484 ret = dqm->dev->kfd2kgd->set_pasid_vmid_mapping( 1485 dqm->dev->adev, pasid, vmid, xcc_id); 1486 if (ret) 1487 break; 1488 } 1489 1490 return ret; 1491 } 1492 1493 static void init_interrupts(struct device_queue_manager *dqm) 1494 { 1495 uint32_t xcc_mask = dqm->dev->xcc_mask; 1496 unsigned int i, xcc_id; 1497 1498 for_each_inst(xcc_id, xcc_mask) { 1499 for (i = 0 ; i < get_pipes_per_mec(dqm) ; i++) { 1500 if (is_pipe_enabled(dqm, 0, i)) { 1501 dqm->dev->kfd2kgd->init_interrupts( 1502 dqm->dev->adev, i, xcc_id); 1503 } 1504 } 1505 } 1506 } 1507 1508 static int initialize_nocpsch(struct device_queue_manager *dqm) 1509 { 1510 int pipe, queue; 1511 1512 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm)); 1513 1514 dqm->allocated_queues = kcalloc(get_pipes_per_mec(dqm), 1515 sizeof(unsigned int), GFP_KERNEL); 1516 if (!dqm->allocated_queues) 1517 return -ENOMEM; 1518 1519 mutex_init(&dqm->lock_hidden); 1520 INIT_LIST_HEAD(&dqm->queues); 1521 dqm->active_queue_count = dqm->next_pipe_to_allocate = 0; 1522 dqm->active_cp_queue_count = 0; 1523 dqm->gws_queue_count = 0; 1524 1525 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) { 1526 int pipe_offset = pipe * get_queues_per_pipe(dqm); 1527 1528 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) 1529 if (test_bit(pipe_offset + queue, 1530 dqm->dev->kfd->shared_resources.cp_queue_bitmap)) 1531 dqm->allocated_queues[pipe] |= 1 << queue; 1532 } 1533 1534 memset(dqm->vmid_pasid, 0, sizeof(dqm->vmid_pasid)); 1535 1536 init_sdma_bitmaps(dqm); 1537 1538 return 0; 1539 } 1540 1541 static void uninitialize(struct device_queue_manager *dqm) 1542 { 1543 int i; 1544 1545 WARN_ON(dqm->active_queue_count > 0 || dqm->processes_count > 0); 1546 1547 kfree(dqm->allocated_queues); 1548 for (i = 0 ; i < KFD_MQD_TYPE_MAX ; i++) 1549 kfree(dqm->mqd_mgrs[i]); 1550 mutex_destroy(&dqm->lock_hidden); 1551 } 1552 1553 static int start_nocpsch(struct device_queue_manager *dqm) 1554 { 1555 int r = 0; 1556 1557 pr_info("SW scheduler is used"); 1558 init_interrupts(dqm); 1559 1560 if (dqm->dev->adev->asic_type == CHIP_HAWAII) 1561 r = pm_init(&dqm->packet_mgr, dqm); 1562 if (!r) 1563 dqm->sched_running = true; 1564 1565 return r; 1566 } 1567 1568 static int stop_nocpsch(struct device_queue_manager *dqm) 1569 { 1570 dqm_lock(dqm); 1571 if (!dqm->sched_running) { 1572 dqm_unlock(dqm); 1573 return 0; 1574 } 1575 1576 if (dqm->dev->adev->asic_type == CHIP_HAWAII) 1577 pm_uninit(&dqm->packet_mgr); 1578 dqm->sched_running = false; 1579 dqm_unlock(dqm); 1580 1581 return 0; 1582 } 1583 1584 static int allocate_sdma_queue(struct device_queue_manager *dqm, 1585 struct queue *q, const uint32_t *restore_sdma_id) 1586 { 1587 struct device *dev = dqm->dev->adev->dev; 1588 int bit; 1589 1590 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { 1591 if (bitmap_empty(dqm->sdma_bitmap, get_num_sdma_queues(dqm))) { 1592 dev_warn(dev, "No more SDMA queue to allocate (%d total queues)\n", 1593 get_num_sdma_queues(dqm)); 1594 return -ENOMEM; 1595 } 1596 1597 if (restore_sdma_id) { 1598 /* Re-use existing sdma_id */ 1599 if (!test_bit(*restore_sdma_id, dqm->sdma_bitmap)) { 1600 dev_err(dev, "SDMA queue already in use\n"); 1601 return -EBUSY; 1602 } 1603 clear_bit(*restore_sdma_id, dqm->sdma_bitmap); 1604 q->sdma_id = *restore_sdma_id; 1605 } else { 1606 /* Find first available sdma_id */ 1607 bit = find_first_bit(dqm->sdma_bitmap, 1608 get_num_sdma_queues(dqm)); 1609 clear_bit(bit, dqm->sdma_bitmap); 1610 q->sdma_id = bit; 1611 } 1612 1613 q->properties.sdma_engine_id = 1614 q->sdma_id % kfd_get_num_sdma_engines(dqm->dev); 1615 q->properties.sdma_queue_id = q->sdma_id / 1616 kfd_get_num_sdma_engines(dqm->dev); 1617 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 1618 if (bitmap_empty(dqm->xgmi_sdma_bitmap, get_num_xgmi_sdma_queues(dqm))) { 1619 dev_warn(dev, "No more XGMI SDMA queue to allocate (%d total queues)\n", 1620 get_num_xgmi_sdma_queues(dqm)); 1621 return -ENOMEM; 1622 } 1623 if (restore_sdma_id) { 1624 /* Re-use existing sdma_id */ 1625 if (!test_bit(*restore_sdma_id, dqm->xgmi_sdma_bitmap)) { 1626 dev_err(dev, "SDMA queue already in use\n"); 1627 return -EBUSY; 1628 } 1629 clear_bit(*restore_sdma_id, dqm->xgmi_sdma_bitmap); 1630 q->sdma_id = *restore_sdma_id; 1631 } else { 1632 bit = find_first_bit(dqm->xgmi_sdma_bitmap, 1633 get_num_xgmi_sdma_queues(dqm)); 1634 clear_bit(bit, dqm->xgmi_sdma_bitmap); 1635 q->sdma_id = bit; 1636 } 1637 /* sdma_engine_id is sdma id including 1638 * both PCIe-optimized SDMAs and XGMI- 1639 * optimized SDMAs. The calculation below 1640 * assumes the first N engines are always 1641 * PCIe-optimized ones 1642 */ 1643 q->properties.sdma_engine_id = 1644 kfd_get_num_sdma_engines(dqm->dev) + 1645 q->sdma_id % kfd_get_num_xgmi_sdma_engines(dqm->dev); 1646 q->properties.sdma_queue_id = q->sdma_id / 1647 kfd_get_num_xgmi_sdma_engines(dqm->dev); 1648 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_BY_ENG_ID) { 1649 int i, num_queues, num_engines, eng_offset = 0, start_engine; 1650 bool free_bit_found = false, is_xgmi = false; 1651 1652 if (q->properties.sdma_engine_id < kfd_get_num_sdma_engines(dqm->dev)) { 1653 num_queues = get_num_sdma_queues(dqm); 1654 num_engines = kfd_get_num_sdma_engines(dqm->dev); 1655 q->properties.type = KFD_QUEUE_TYPE_SDMA; 1656 } else { 1657 num_queues = get_num_xgmi_sdma_queues(dqm); 1658 num_engines = kfd_get_num_xgmi_sdma_engines(dqm->dev); 1659 eng_offset = kfd_get_num_sdma_engines(dqm->dev); 1660 q->properties.type = KFD_QUEUE_TYPE_SDMA_XGMI; 1661 is_xgmi = true; 1662 } 1663 1664 /* Scan available bit based on target engine ID. */ 1665 start_engine = q->properties.sdma_engine_id - eng_offset; 1666 for (i = start_engine; i < num_queues; i += num_engines) { 1667 1668 if (!test_bit(i, is_xgmi ? dqm->xgmi_sdma_bitmap : dqm->sdma_bitmap)) 1669 continue; 1670 1671 clear_bit(i, is_xgmi ? dqm->xgmi_sdma_bitmap : dqm->sdma_bitmap); 1672 q->sdma_id = i; 1673 q->properties.sdma_queue_id = q->sdma_id / num_engines; 1674 free_bit_found = true; 1675 break; 1676 } 1677 1678 if (!free_bit_found) { 1679 dev_warn(dev, "No more SDMA queue to allocate for target ID %i (%d total queues)\n", 1680 q->properties.sdma_engine_id, num_queues); 1681 return -ENOMEM; 1682 } 1683 } 1684 1685 pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id); 1686 pr_debug("SDMA queue id: %d\n", q->properties.sdma_queue_id); 1687 1688 return 0; 1689 } 1690 1691 static void deallocate_sdma_queue(struct device_queue_manager *dqm, 1692 struct queue *q) 1693 { 1694 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { 1695 if (q->sdma_id >= get_num_sdma_queues(dqm)) 1696 return; 1697 set_bit(q->sdma_id, dqm->sdma_bitmap); 1698 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 1699 if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) 1700 return; 1701 set_bit(q->sdma_id, dqm->xgmi_sdma_bitmap); 1702 } 1703 } 1704 1705 /* 1706 * Device Queue Manager implementation for cp scheduler 1707 */ 1708 1709 static int set_sched_resources(struct device_queue_manager *dqm) 1710 { 1711 int i, mec; 1712 struct scheduling_resources res; 1713 struct device *dev = dqm->dev->adev->dev; 1714 1715 res.vmid_mask = dqm->dev->compute_vmid_bitmap; 1716 1717 res.queue_mask = 0; 1718 for (i = 0; i < AMDGPU_MAX_QUEUES; ++i) { 1719 mec = (i / dqm->dev->kfd->shared_resources.num_queue_per_pipe) 1720 / dqm->dev->kfd->shared_resources.num_pipe_per_mec; 1721 1722 if (!test_bit(i, dqm->dev->kfd->shared_resources.cp_queue_bitmap)) 1723 continue; 1724 1725 /* only acquire queues from the first MEC */ 1726 if (mec > 0) 1727 continue; 1728 1729 /* This situation may be hit in the future if a new HW 1730 * generation exposes more than 64 queues. If so, the 1731 * definition of res.queue_mask needs updating 1732 */ 1733 if (WARN_ON(i >= (sizeof(res.queue_mask)*8))) { 1734 dev_err(dev, "Invalid queue enabled by amdgpu: %d\n", i); 1735 break; 1736 } 1737 1738 res.queue_mask |= 1ull 1739 << amdgpu_queue_mask_bit_to_set_resource_bit( 1740 dqm->dev->adev, i); 1741 } 1742 res.gws_mask = ~0ull; 1743 res.oac_mask = res.gds_heap_base = res.gds_heap_size = 0; 1744 1745 pr_debug("Scheduling resources:\n" 1746 "vmid mask: 0x%8X\n" 1747 "queue mask: 0x%8llX\n", 1748 res.vmid_mask, res.queue_mask); 1749 1750 return pm_send_set_resources(&dqm->packet_mgr, &res); 1751 } 1752 1753 static int initialize_cpsch(struct device_queue_manager *dqm) 1754 { 1755 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm)); 1756 1757 mutex_init(&dqm->lock_hidden); 1758 INIT_LIST_HEAD(&dqm->queues); 1759 dqm->active_queue_count = dqm->processes_count = 0; 1760 dqm->active_cp_queue_count = 0; 1761 dqm->gws_queue_count = 0; 1762 dqm->active_runlist = false; 1763 dqm->trap_debug_vmid = 0; 1764 1765 init_sdma_bitmaps(dqm); 1766 1767 update_dqm_wait_times(dqm); 1768 return 0; 1769 } 1770 1771 /* halt_cpsch: 1772 * Unmap queues so the schedule doesn't continue remaining jobs in the queue. 1773 * Then set dqm->sched_halt so queues don't map to runlist until unhalt_cpsch 1774 * is called. 1775 */ 1776 static int halt_cpsch(struct device_queue_manager *dqm) 1777 { 1778 int ret = 0; 1779 1780 dqm_lock(dqm); 1781 if (!dqm->sched_running) { 1782 dqm_unlock(dqm); 1783 return 0; 1784 } 1785 1786 WARN_ONCE(dqm->sched_halt, "Scheduling is already on halt\n"); 1787 1788 if (!dqm->is_hws_hang) { 1789 if (!dqm->dev->kfd->shared_resources.enable_mes) 1790 ret = unmap_queues_cpsch(dqm, 1791 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, 1792 USE_DEFAULT_GRACE_PERIOD, false); 1793 else 1794 ret = remove_all_kfd_queues_mes(dqm); 1795 } 1796 dqm->sched_halt = true; 1797 dqm_unlock(dqm); 1798 1799 return ret; 1800 } 1801 1802 /* unhalt_cpsch 1803 * Unset dqm->sched_halt and map queues back to runlist 1804 */ 1805 static int unhalt_cpsch(struct device_queue_manager *dqm) 1806 { 1807 int ret = 0; 1808 1809 dqm_lock(dqm); 1810 if (!dqm->sched_running || !dqm->sched_halt) { 1811 WARN_ONCE(!dqm->sched_halt, "Scheduling is not on halt.\n"); 1812 dqm_unlock(dqm); 1813 return 0; 1814 } 1815 dqm->sched_halt = false; 1816 if (!dqm->dev->kfd->shared_resources.enable_mes) 1817 ret = execute_queues_cpsch(dqm, 1818 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 1819 0, USE_DEFAULT_GRACE_PERIOD); 1820 else 1821 ret = add_all_kfd_queues_mes(dqm); 1822 1823 dqm_unlock(dqm); 1824 1825 return ret; 1826 } 1827 1828 static int start_cpsch(struct device_queue_manager *dqm) 1829 { 1830 struct device *dev = dqm->dev->adev->dev; 1831 int retval, num_hw_queue_slots; 1832 1833 dqm_lock(dqm); 1834 1835 if (!dqm->dev->kfd->shared_resources.enable_mes) { 1836 retval = pm_init(&dqm->packet_mgr, dqm); 1837 if (retval) 1838 goto fail_packet_manager_init; 1839 1840 retval = set_sched_resources(dqm); 1841 if (retval) 1842 goto fail_set_sched_resources; 1843 } 1844 pr_debug("Allocating fence memory\n"); 1845 1846 /* allocate fence memory on the gart */ 1847 retval = kfd_gtt_sa_allocate(dqm->dev, sizeof(*dqm->fence_addr), 1848 &dqm->fence_mem); 1849 1850 if (retval) 1851 goto fail_allocate_vidmem; 1852 1853 dqm->fence_addr = (uint64_t *)dqm->fence_mem->cpu_ptr; 1854 dqm->fence_gpu_addr = dqm->fence_mem->gpu_addr; 1855 1856 init_interrupts(dqm); 1857 1858 /* clear hang status when driver try to start the hw scheduler */ 1859 dqm->sched_running = true; 1860 1861 if (!dqm->dev->kfd->shared_resources.enable_mes) { 1862 if (pm_config_dequeue_wait_counts(&dqm->packet_mgr, 1863 KFD_DEQUEUE_WAIT_INIT, 0 /* unused */)) 1864 dev_err(dev, "Setting optimized dequeue wait failed. Using default values\n"); 1865 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD); 1866 } 1867 1868 /* setup per-queue reset detection buffer */ 1869 num_hw_queue_slots = dqm->dev->kfd->shared_resources.num_queue_per_pipe * 1870 dqm->dev->kfd->shared_resources.num_pipe_per_mec * 1871 NUM_XCC(dqm->dev->xcc_mask); 1872 1873 dqm->detect_hang_info_size = num_hw_queue_slots * sizeof(struct dqm_detect_hang_info); 1874 dqm->detect_hang_info = kzalloc(dqm->detect_hang_info_size, GFP_KERNEL); 1875 1876 if (!dqm->detect_hang_info) { 1877 retval = -ENOMEM; 1878 goto fail_detect_hang_buffer; 1879 } 1880 1881 dqm_unlock(dqm); 1882 1883 return 0; 1884 fail_detect_hang_buffer: 1885 kfd_gtt_sa_free(dqm->dev, dqm->fence_mem); 1886 fail_allocate_vidmem: 1887 fail_set_sched_resources: 1888 if (!dqm->dev->kfd->shared_resources.enable_mes) 1889 pm_uninit(&dqm->packet_mgr); 1890 fail_packet_manager_init: 1891 dqm_unlock(dqm); 1892 return retval; 1893 } 1894 1895 static int stop_cpsch(struct device_queue_manager *dqm) 1896 { 1897 int ret = 0; 1898 1899 dqm_lock(dqm); 1900 if (!dqm->sched_running) { 1901 dqm_unlock(dqm); 1902 return 0; 1903 } 1904 1905 if (!dqm->dev->kfd->shared_resources.enable_mes) 1906 ret = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 1907 0, USE_DEFAULT_GRACE_PERIOD, false); 1908 else 1909 ret = remove_all_kfd_queues_mes(dqm); 1910 1911 dqm->sched_running = false; 1912 1913 if (!dqm->dev->kfd->shared_resources.enable_mes) 1914 pm_release_ib(&dqm->packet_mgr); 1915 1916 kfd_gtt_sa_free(dqm->dev, dqm->fence_mem); 1917 if (!dqm->dev->kfd->shared_resources.enable_mes) 1918 pm_uninit(&dqm->packet_mgr); 1919 kfree(dqm->detect_hang_info); 1920 dqm->detect_hang_info = NULL; 1921 dqm_unlock(dqm); 1922 1923 return ret; 1924 } 1925 1926 static int create_kernel_queue_cpsch(struct device_queue_manager *dqm, 1927 struct kernel_queue *kq, 1928 struct qcm_process_device *qpd) 1929 { 1930 dqm_lock(dqm); 1931 if (dqm->total_queue_count >= max_num_of_queues_per_device) { 1932 pr_warn("Can't create new kernel queue because %d queues were already created\n", 1933 dqm->total_queue_count); 1934 dqm_unlock(dqm); 1935 return -EPERM; 1936 } 1937 1938 /* 1939 * Unconditionally increment this counter, regardless of the queue's 1940 * type or whether the queue is active. 1941 */ 1942 dqm->total_queue_count++; 1943 pr_debug("Total of %d queues are accountable so far\n", 1944 dqm->total_queue_count); 1945 1946 list_add(&kq->list, &qpd->priv_queue_list); 1947 increment_queue_count(dqm, qpd, kq->queue); 1948 qpd->is_debug = true; 1949 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, 1950 USE_DEFAULT_GRACE_PERIOD); 1951 dqm_unlock(dqm); 1952 1953 return 0; 1954 } 1955 1956 static void destroy_kernel_queue_cpsch(struct device_queue_manager *dqm, 1957 struct kernel_queue *kq, 1958 struct qcm_process_device *qpd) 1959 { 1960 dqm_lock(dqm); 1961 list_del(&kq->list); 1962 decrement_queue_count(dqm, qpd, kq->queue); 1963 qpd->is_debug = false; 1964 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, 1965 USE_DEFAULT_GRACE_PERIOD); 1966 /* 1967 * Unconditionally decrement this counter, regardless of the queue's 1968 * type. 1969 */ 1970 dqm->total_queue_count--; 1971 pr_debug("Total of %d queues are accountable so far\n", 1972 dqm->total_queue_count); 1973 dqm_unlock(dqm); 1974 } 1975 1976 static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, 1977 struct qcm_process_device *qpd, 1978 const struct kfd_criu_queue_priv_data *qd, 1979 const void *restore_mqd, const void *restore_ctl_stack) 1980 { 1981 int retval; 1982 struct mqd_manager *mqd_mgr; 1983 1984 if (dqm->total_queue_count >= max_num_of_queues_per_device) { 1985 pr_warn("Can't create new usermode queue because %d queues were already created\n", 1986 dqm->total_queue_count); 1987 retval = -EPERM; 1988 goto out; 1989 } 1990 1991 if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 1992 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI || 1993 q->properties.type == KFD_QUEUE_TYPE_SDMA_BY_ENG_ID) { 1994 dqm_lock(dqm); 1995 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL); 1996 dqm_unlock(dqm); 1997 if (retval) 1998 goto out; 1999 } 2000 2001 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL); 2002 if (retval) 2003 goto out_deallocate_sdma_queue; 2004 2005 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 2006 q->properties.type)]; 2007 2008 if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 2009 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 2010 dqm->asic_ops.init_sdma_vm(dqm, q, qpd); 2011 q->properties.tba_addr = qpd->tba_addr; 2012 q->properties.tma_addr = qpd->tma_addr; 2013 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties); 2014 if (!q->mqd_mem_obj) { 2015 retval = -ENOMEM; 2016 goto out_deallocate_doorbell; 2017 } 2018 2019 dqm_lock(dqm); 2020 /* 2021 * Eviction state logic: mark all queues as evicted, even ones 2022 * not currently active. Restoring inactive queues later only 2023 * updates the is_evicted flag but is a no-op otherwise. 2024 */ 2025 q->properties.is_evicted = !!qpd->evicted; 2026 q->properties.is_dbg_wa = qpd->pqm->process->debug_trap_enabled && 2027 kfd_dbg_has_cwsr_workaround(q->device); 2028 2029 if (qd) 2030 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr, 2031 &q->properties, restore_mqd, restore_ctl_stack, 2032 qd->ctl_stack_size); 2033 else 2034 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, 2035 &q->gart_mqd_addr, &q->properties); 2036 2037 list_add(&q->list, &qpd->queues_list); 2038 qpd->queue_count++; 2039 2040 if (q->properties.is_active) { 2041 increment_queue_count(dqm, qpd, q); 2042 2043 if (!dqm->dev->kfd->shared_resources.enable_mes) 2044 retval = execute_queues_cpsch(dqm, 2045 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD); 2046 else 2047 retval = add_queue_mes(dqm, q, qpd); 2048 if (retval) 2049 goto cleanup_queue; 2050 } 2051 2052 /* 2053 * Unconditionally increment this counter, regardless of the queue's 2054 * type or whether the queue is active. 2055 */ 2056 dqm->total_queue_count++; 2057 2058 pr_debug("Total of %d queues are accountable so far\n", 2059 dqm->total_queue_count); 2060 2061 dqm_unlock(dqm); 2062 return retval; 2063 2064 cleanup_queue: 2065 qpd->queue_count--; 2066 list_del(&q->list); 2067 if (q->properties.is_active) 2068 decrement_queue_count(dqm, qpd, q); 2069 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 2070 dqm_unlock(dqm); 2071 out_deallocate_doorbell: 2072 deallocate_doorbell(qpd, q); 2073 out_deallocate_sdma_queue: 2074 if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 2075 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 2076 dqm_lock(dqm); 2077 deallocate_sdma_queue(dqm, q); 2078 dqm_unlock(dqm); 2079 } 2080 out: 2081 return retval; 2082 } 2083 2084 int amdkfd_fence_wait_timeout(struct device_queue_manager *dqm, 2085 uint64_t fence_value, 2086 unsigned int timeout_ms) 2087 { 2088 unsigned long end_jiffies = msecs_to_jiffies(timeout_ms) + jiffies; 2089 struct device *dev = dqm->dev->adev->dev; 2090 uint64_t *fence_addr = dqm->fence_addr; 2091 2092 while (*fence_addr != fence_value) { 2093 /* Fatal err detected, this response won't come */ 2094 if (amdgpu_amdkfd_is_fed(dqm->dev->adev) || 2095 amdgpu_in_reset(dqm->dev->adev)) 2096 return -EIO; 2097 2098 if (time_after(jiffies, end_jiffies)) { 2099 dev_err(dev, "qcm fence wait loop timeout expired\n"); 2100 /* In HWS case, this is used to halt the driver thread 2101 * in order not to mess up CP states before doing 2102 * scandumps for FW debugging. 2103 */ 2104 while (halt_if_hws_hang) 2105 schedule(); 2106 2107 return -ETIME; 2108 } 2109 schedule(); 2110 } 2111 2112 return 0; 2113 } 2114 2115 /* dqm->lock mutex has to be locked before calling this function */ 2116 static int map_queues_cpsch(struct device_queue_manager *dqm) 2117 { 2118 struct device *dev = dqm->dev->adev->dev; 2119 int retval; 2120 2121 if (!dqm->sched_running || dqm->sched_halt) 2122 return 0; 2123 if (dqm->active_queue_count <= 0 || dqm->processes_count <= 0) 2124 return 0; 2125 if (dqm->active_runlist) 2126 return 0; 2127 2128 retval = pm_send_runlist(&dqm->packet_mgr, &dqm->queues); 2129 pr_debug("%s sent runlist\n", __func__); 2130 if (retval) { 2131 dev_err(dev, "failed to execute runlist\n"); 2132 return retval; 2133 } 2134 dqm->active_runlist = true; 2135 2136 return retval; 2137 } 2138 2139 static void set_queue_as_reset(struct device_queue_manager *dqm, struct queue *q, 2140 struct qcm_process_device *qpd) 2141 { 2142 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 2143 2144 dev_err(dqm->dev->adev->dev, "queue id 0x%0x at pasid %d is reset\n", 2145 q->properties.queue_id, pdd->process->lead_thread->pid); 2146 2147 pdd->has_reset_queue = true; 2148 if (q->properties.is_active) { 2149 q->properties.is_active = false; 2150 decrement_queue_count(dqm, qpd, q); 2151 } 2152 } 2153 2154 static int detect_queue_hang(struct device_queue_manager *dqm) 2155 { 2156 int i; 2157 2158 /* detect should be used only in dqm locked queue reset */ 2159 if (WARN_ON(dqm->detect_hang_count > 0)) 2160 return 0; 2161 2162 memset(dqm->detect_hang_info, 0, dqm->detect_hang_info_size); 2163 2164 for (i = 0; i < AMDGPU_MAX_QUEUES; ++i) { 2165 uint32_t mec, pipe, queue; 2166 int xcc_id; 2167 2168 mec = (i / dqm->dev->kfd->shared_resources.num_queue_per_pipe) 2169 / dqm->dev->kfd->shared_resources.num_pipe_per_mec; 2170 2171 if (mec || !test_bit(i, dqm->dev->kfd->shared_resources.cp_queue_bitmap)) 2172 continue; 2173 2174 amdgpu_queue_mask_bit_to_mec_queue(dqm->dev->adev, i, &mec, &pipe, &queue); 2175 2176 for_each_inst(xcc_id, dqm->dev->xcc_mask) { 2177 uint64_t queue_addr = dqm->dev->kfd2kgd->hqd_get_pq_addr( 2178 dqm->dev->adev, pipe, queue, xcc_id); 2179 struct dqm_detect_hang_info hang_info; 2180 2181 if (!queue_addr) 2182 continue; 2183 2184 hang_info.pipe_id = pipe; 2185 hang_info.queue_id = queue; 2186 hang_info.xcc_id = xcc_id; 2187 hang_info.queue_address = queue_addr; 2188 2189 dqm->detect_hang_info[dqm->detect_hang_count] = hang_info; 2190 dqm->detect_hang_count++; 2191 } 2192 } 2193 2194 return dqm->detect_hang_count; 2195 } 2196 2197 static struct queue *find_queue_by_address(struct device_queue_manager *dqm, uint64_t queue_address) 2198 { 2199 struct device_process_node *cur; 2200 struct qcm_process_device *qpd; 2201 struct queue *q; 2202 2203 list_for_each_entry(cur, &dqm->queues, list) { 2204 qpd = cur->qpd; 2205 list_for_each_entry(q, &qpd->queues_list, list) { 2206 if (queue_address == q->properties.queue_address) 2207 return q; 2208 } 2209 } 2210 2211 return NULL; 2212 } 2213 2214 static int reset_hung_queues(struct device_queue_manager *dqm) 2215 { 2216 int r = 0, reset_count = 0, i; 2217 2218 if (!dqm->detect_hang_info || dqm->is_hws_hang) 2219 return -EIO; 2220 2221 /* assume dqm locked. */ 2222 if (!detect_queue_hang(dqm)) 2223 return -ENOTRECOVERABLE; 2224 2225 for (i = 0; i < dqm->detect_hang_count; i++) { 2226 struct dqm_detect_hang_info hang_info = dqm->detect_hang_info[i]; 2227 struct queue *q = find_queue_by_address(dqm, hang_info.queue_address); 2228 struct kfd_process_device *pdd; 2229 uint64_t queue_addr = 0; 2230 2231 if (!q) { 2232 r = -ENOTRECOVERABLE; 2233 goto reset_fail; 2234 } 2235 2236 pdd = kfd_get_process_device_data(dqm->dev, q->process); 2237 if (!pdd) { 2238 r = -ENOTRECOVERABLE; 2239 goto reset_fail; 2240 } 2241 2242 queue_addr = dqm->dev->kfd2kgd->hqd_reset(dqm->dev->adev, 2243 hang_info.pipe_id, hang_info.queue_id, hang_info.xcc_id, 2244 KFD_UNMAP_LATENCY_MS); 2245 2246 /* either reset failed or we reset an unexpected queue. */ 2247 if (queue_addr != q->properties.queue_address) { 2248 r = -ENOTRECOVERABLE; 2249 goto reset_fail; 2250 } 2251 2252 set_queue_as_reset(dqm, q, &pdd->qpd); 2253 reset_count++; 2254 } 2255 2256 if (reset_count == dqm->detect_hang_count) 2257 kfd_signal_reset_event(dqm->dev); 2258 else 2259 r = -ENOTRECOVERABLE; 2260 2261 reset_fail: 2262 dqm->detect_hang_count = 0; 2263 2264 return r; 2265 } 2266 2267 static bool sdma_has_hang(struct device_queue_manager *dqm) 2268 { 2269 int engine_start = dqm->dev->node_id * get_num_all_sdma_engines(dqm); 2270 int engine_end = engine_start + get_num_all_sdma_engines(dqm); 2271 int num_queues_per_eng = dqm->dev->kfd->device_info.num_sdma_queues_per_engine; 2272 int i, j; 2273 2274 for (i = engine_start; i < engine_end; i++) { 2275 for (j = 0; j < num_queues_per_eng; j++) { 2276 if (!dqm->dev->kfd2kgd->hqd_sdma_get_doorbell(dqm->dev->adev, i, j)) 2277 continue; 2278 2279 return true; 2280 } 2281 } 2282 2283 return false; 2284 } 2285 2286 static bool set_sdma_queue_as_reset(struct device_queue_manager *dqm, 2287 uint32_t doorbell_off) 2288 { 2289 struct device_process_node *cur; 2290 struct qcm_process_device *qpd; 2291 struct queue *q; 2292 2293 list_for_each_entry(cur, &dqm->queues, list) { 2294 qpd = cur->qpd; 2295 list_for_each_entry(q, &qpd->queues_list, list) { 2296 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA || 2297 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) && 2298 q->properties.doorbell_off == doorbell_off) { 2299 set_queue_as_reset(dqm, q, qpd); 2300 return true; 2301 } 2302 } 2303 } 2304 2305 return false; 2306 } 2307 2308 static int reset_hung_queues_sdma(struct device_queue_manager *dqm) 2309 { 2310 int engine_start = dqm->dev->node_id * get_num_all_sdma_engines(dqm); 2311 int engine_end = engine_start + get_num_all_sdma_engines(dqm); 2312 int num_queues_per_eng = dqm->dev->kfd->device_info.num_sdma_queues_per_engine; 2313 int r = 0, i, j; 2314 2315 if (dqm->is_hws_hang) 2316 return -EIO; 2317 2318 /* Scan for hung HW queues and reset engine. */ 2319 dqm->detect_hang_count = 0; 2320 for (i = engine_start; i < engine_end; i++) { 2321 for (j = 0; j < num_queues_per_eng; j++) { 2322 uint32_t doorbell_off = 2323 dqm->dev->kfd2kgd->hqd_sdma_get_doorbell(dqm->dev->adev, i, j); 2324 2325 if (!doorbell_off) 2326 continue; 2327 2328 /* Reset engine and check. */ 2329 if (amdgpu_sdma_reset_engine(dqm->dev->adev, i, false) || 2330 dqm->dev->kfd2kgd->hqd_sdma_get_doorbell(dqm->dev->adev, i, j) || 2331 !set_sdma_queue_as_reset(dqm, doorbell_off)) { 2332 r = -ENOTRECOVERABLE; 2333 goto reset_fail; 2334 } 2335 2336 /* Should only expect one queue active per engine */ 2337 dqm->detect_hang_count++; 2338 break; 2339 } 2340 } 2341 2342 /* Signal process reset */ 2343 if (dqm->detect_hang_count) 2344 kfd_signal_reset_event(dqm->dev); 2345 else 2346 r = -ENOTRECOVERABLE; 2347 2348 reset_fail: 2349 dqm->detect_hang_count = 0; 2350 2351 return r; 2352 } 2353 2354 static int reset_queues_on_hws_hang(struct device_queue_manager *dqm, bool is_sdma) 2355 { 2356 struct amdgpu_device *adev = dqm->dev->adev; 2357 2358 while (halt_if_hws_hang) 2359 schedule(); 2360 2361 if (adev->debug_disable_gpu_ring_reset) { 2362 dev_info_once(adev->dev, 2363 "%s queue hung, but ring reset disabled", 2364 is_sdma ? "sdma" : "compute"); 2365 2366 return -EPERM; 2367 } 2368 if (!amdgpu_gpu_recovery) 2369 return -ENOTRECOVERABLE; 2370 2371 return is_sdma ? reset_hung_queues_sdma(dqm) : reset_hung_queues(dqm); 2372 } 2373 2374 /* dqm->lock mutex has to be locked before calling this function 2375 * 2376 * @grace_period: If USE_DEFAULT_GRACE_PERIOD then default wait time 2377 * for context switch latency. Lower values are used by debugger 2378 * since context switching are triggered at high frequency. 2379 * This is configured by setting CP_IQ_WAIT_TIME2.SCH_WAVE 2380 * 2381 */ 2382 static int unmap_queues_cpsch(struct device_queue_manager *dqm, 2383 enum kfd_unmap_queues_filter filter, 2384 uint32_t filter_param, 2385 uint32_t grace_period, 2386 bool reset) 2387 { 2388 struct device *dev = dqm->dev->adev->dev; 2389 struct mqd_manager *mqd_mgr; 2390 int retval; 2391 2392 if (!dqm->sched_running) 2393 return 0; 2394 if (!dqm->active_runlist) 2395 return 0; 2396 if (!down_read_trylock(&dqm->dev->adev->reset_domain->sem)) 2397 return -EIO; 2398 2399 if (grace_period != USE_DEFAULT_GRACE_PERIOD) { 2400 retval = pm_config_dequeue_wait_counts(&dqm->packet_mgr, 2401 KFD_DEQUEUE_WAIT_SET_SCH_WAVE, grace_period); 2402 if (retval) 2403 goto out; 2404 } 2405 2406 retval = pm_send_unmap_queue(&dqm->packet_mgr, filter, filter_param, reset); 2407 if (retval) 2408 goto out; 2409 2410 *dqm->fence_addr = KFD_FENCE_INIT; 2411 mb(); 2412 pm_send_query_status(&dqm->packet_mgr, dqm->fence_gpu_addr, 2413 KFD_FENCE_COMPLETED); 2414 /* should be timed out */ 2415 retval = amdkfd_fence_wait_timeout(dqm, KFD_FENCE_COMPLETED, 2416 queue_preemption_timeout_ms); 2417 if (retval) { 2418 dev_err(dev, "The cp might be in an unrecoverable state due to an unsuccessful queues preemption\n"); 2419 kfd_hws_hang(dqm); 2420 goto out; 2421 } 2422 2423 /* In the current MEC firmware implementation, if compute queue 2424 * doesn't response to the preemption request in time, HIQ will 2425 * abandon the unmap request without returning any timeout error 2426 * to driver. Instead, MEC firmware will log the doorbell of the 2427 * unresponding compute queue to HIQ.MQD.queue_doorbell_id fields. 2428 * To make sure the queue unmap was successful, driver need to 2429 * check those fields 2430 */ 2431 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]; 2432 if (mqd_mgr->check_preemption_failed(mqd_mgr, dqm->packet_mgr.priv_queue->queue->mqd) && 2433 reset_queues_on_hws_hang(dqm, false)) 2434 goto reset_fail; 2435 2436 /* Check for SDMA hang and attempt SDMA reset */ 2437 if (sdma_has_hang(dqm) && reset_queues_on_hws_hang(dqm, true)) 2438 goto reset_fail; 2439 2440 /* We need to reset the grace period value for this device */ 2441 if (grace_period != USE_DEFAULT_GRACE_PERIOD) { 2442 if (pm_config_dequeue_wait_counts(&dqm->packet_mgr, 2443 KFD_DEQUEUE_WAIT_RESET, 0 /* unused */)) 2444 dev_err(dev, "Failed to reset grace period\n"); 2445 } 2446 2447 pm_release_ib(&dqm->packet_mgr); 2448 dqm->active_runlist = false; 2449 out: 2450 up_read(&dqm->dev->adev->reset_domain->sem); 2451 return retval; 2452 2453 reset_fail: 2454 dqm->is_hws_hang = true; 2455 kfd_hws_hang(dqm); 2456 up_read(&dqm->dev->adev->reset_domain->sem); 2457 return -ETIME; 2458 } 2459 2460 /* only for compute queue */ 2461 static int reset_queues_cpsch(struct device_queue_manager *dqm, uint16_t pasid) 2462 { 2463 int retval; 2464 2465 dqm_lock(dqm); 2466 2467 retval = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_BY_PASID, 2468 pasid, USE_DEFAULT_GRACE_PERIOD, true); 2469 2470 dqm_unlock(dqm); 2471 return retval; 2472 } 2473 2474 /* dqm->lock mutex has to be locked before calling this function */ 2475 static int execute_queues_cpsch(struct device_queue_manager *dqm, 2476 enum kfd_unmap_queues_filter filter, 2477 uint32_t filter_param, 2478 uint32_t grace_period) 2479 { 2480 int retval; 2481 2482 if (!down_read_trylock(&dqm->dev->adev->reset_domain->sem)) 2483 return -EIO; 2484 retval = unmap_queues_cpsch(dqm, filter, filter_param, grace_period, false); 2485 if (!retval) 2486 retval = map_queues_cpsch(dqm); 2487 up_read(&dqm->dev->adev->reset_domain->sem); 2488 return retval; 2489 } 2490 2491 static int wait_on_destroy_queue(struct device_queue_manager *dqm, 2492 struct queue *q) 2493 { 2494 struct kfd_process_device *pdd = kfd_get_process_device_data(q->device, 2495 q->process); 2496 int ret = 0; 2497 2498 if (WARN_ON(!pdd)) 2499 return ret; 2500 2501 if (pdd->qpd.is_debug) 2502 return ret; 2503 2504 q->properties.is_being_destroyed = true; 2505 2506 if (pdd->process->debug_trap_enabled && q->properties.is_suspended) { 2507 dqm_unlock(dqm); 2508 mutex_unlock(&q->process->mutex); 2509 ret = wait_event_interruptible(dqm->destroy_wait, 2510 !q->properties.is_suspended); 2511 2512 mutex_lock(&q->process->mutex); 2513 dqm_lock(dqm); 2514 } 2515 2516 return ret; 2517 } 2518 2519 static int destroy_queue_cpsch(struct device_queue_manager *dqm, 2520 struct qcm_process_device *qpd, 2521 struct queue *q) 2522 { 2523 int retval; 2524 struct mqd_manager *mqd_mgr; 2525 uint64_t sdma_val = 0; 2526 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 2527 struct device *dev = dqm->dev->adev->dev; 2528 2529 /* Get the SDMA queue stats */ 2530 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || 2531 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 2532 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr, 2533 &sdma_val); 2534 if (retval) 2535 dev_err(dev, "Failed to read SDMA queue counter for queue: %d\n", 2536 q->properties.queue_id); 2537 } 2538 2539 /* remove queue from list to prevent rescheduling after preemption */ 2540 dqm_lock(dqm); 2541 2542 retval = wait_on_destroy_queue(dqm, q); 2543 2544 if (retval) { 2545 dqm_unlock(dqm); 2546 return retval; 2547 } 2548 2549 if (qpd->is_debug) { 2550 /* 2551 * error, currently we do not allow to destroy a queue 2552 * of a currently debugged process 2553 */ 2554 retval = -EBUSY; 2555 goto failed_try_destroy_debugged_queue; 2556 2557 } 2558 2559 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 2560 q->properties.type)]; 2561 2562 deallocate_doorbell(qpd, q); 2563 2564 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || 2565 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 2566 deallocate_sdma_queue(dqm, q); 2567 pdd->sdma_past_activity_counter += sdma_val; 2568 } 2569 2570 if (q->properties.is_active) { 2571 decrement_queue_count(dqm, qpd, q); 2572 q->properties.is_active = false; 2573 if (!dqm->dev->kfd->shared_resources.enable_mes) { 2574 retval = execute_queues_cpsch(dqm, 2575 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, 2576 USE_DEFAULT_GRACE_PERIOD); 2577 if (retval == -ETIME) 2578 qpd->reset_wavefronts = true; 2579 } else { 2580 retval = remove_queue_mes(dqm, q, qpd); 2581 } 2582 } 2583 list_del(&q->list); 2584 qpd->queue_count--; 2585 2586 /* 2587 * Unconditionally decrement this counter, regardless of the queue's 2588 * type 2589 */ 2590 dqm->total_queue_count--; 2591 pr_debug("Total of %d queues are accountable so far\n", 2592 dqm->total_queue_count); 2593 2594 dqm_unlock(dqm); 2595 2596 /* 2597 * Do free_mqd and raise delete event after dqm_unlock(dqm) to avoid 2598 * circular locking 2599 */ 2600 kfd_dbg_ev_raise(KFD_EC_MASK(EC_DEVICE_QUEUE_DELETE), 2601 qpd->pqm->process, q->device, 2602 -1, false, NULL, 0); 2603 2604 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 2605 2606 return retval; 2607 2608 failed_try_destroy_debugged_queue: 2609 2610 dqm_unlock(dqm); 2611 return retval; 2612 } 2613 2614 static bool set_cache_memory_policy(struct device_queue_manager *dqm, 2615 struct qcm_process_device *qpd, 2616 enum cache_policy default_policy, 2617 enum cache_policy alternate_policy, 2618 void __user *alternate_aperture_base, 2619 uint64_t alternate_aperture_size, 2620 u32 misc_process_properties) 2621 { 2622 bool retval = true; 2623 2624 if (!dqm->asic_ops.set_cache_memory_policy) 2625 return retval; 2626 2627 dqm_lock(dqm); 2628 2629 retval = dqm->asic_ops.set_cache_memory_policy( 2630 dqm, 2631 qpd, 2632 default_policy, 2633 alternate_policy, 2634 alternate_aperture_base, 2635 alternate_aperture_size, 2636 misc_process_properties); 2637 2638 if (retval) 2639 goto out; 2640 2641 if ((dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) && (qpd->vmid != 0)) 2642 program_sh_mem_settings(dqm, qpd); 2643 2644 pr_debug("sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n", 2645 qpd->sh_mem_config, qpd->sh_mem_ape1_base, 2646 qpd->sh_mem_ape1_limit); 2647 2648 out: 2649 dqm_unlock(dqm); 2650 return retval; 2651 } 2652 2653 static int process_termination_nocpsch(struct device_queue_manager *dqm, 2654 struct qcm_process_device *qpd) 2655 { 2656 struct queue *q; 2657 struct device_process_node *cur, *next_dpn; 2658 int retval = 0; 2659 bool found = false; 2660 2661 dqm_lock(dqm); 2662 2663 /* Clear all user mode queues */ 2664 while (!list_empty(&qpd->queues_list)) { 2665 struct mqd_manager *mqd_mgr; 2666 int ret; 2667 2668 q = list_first_entry(&qpd->queues_list, struct queue, list); 2669 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 2670 q->properties.type)]; 2671 ret = destroy_queue_nocpsch_locked(dqm, qpd, q); 2672 if (ret) 2673 retval = ret; 2674 dqm_unlock(dqm); 2675 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 2676 dqm_lock(dqm); 2677 } 2678 2679 /* Unregister process */ 2680 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) { 2681 if (qpd == cur->qpd) { 2682 list_del(&cur->list); 2683 kfree(cur); 2684 dqm->processes_count--; 2685 found = true; 2686 break; 2687 } 2688 } 2689 2690 dqm_unlock(dqm); 2691 2692 /* Outside the DQM lock because under the DQM lock we can't do 2693 * reclaim or take other locks that others hold while reclaiming. 2694 */ 2695 if (found) 2696 kfd_dec_compute_active(dqm->dev); 2697 2698 return retval; 2699 } 2700 2701 static int get_wave_state(struct device_queue_manager *dqm, 2702 struct queue *q, 2703 void __user *ctl_stack, 2704 u32 *ctl_stack_used_size, 2705 u32 *save_area_used_size) 2706 { 2707 struct mqd_manager *mqd_mgr; 2708 2709 dqm_lock(dqm); 2710 2711 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP]; 2712 2713 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE || 2714 q->properties.is_active || !q->device->kfd->cwsr_enabled || 2715 !mqd_mgr->get_wave_state) { 2716 dqm_unlock(dqm); 2717 return -EINVAL; 2718 } 2719 2720 dqm_unlock(dqm); 2721 2722 /* 2723 * get_wave_state is outside the dqm lock to prevent circular locking 2724 * and the queue should be protected against destruction by the process 2725 * lock. 2726 */ 2727 return mqd_mgr->get_wave_state(mqd_mgr, q->mqd, &q->properties, 2728 ctl_stack, ctl_stack_used_size, save_area_used_size); 2729 } 2730 2731 static void get_queue_checkpoint_info(struct device_queue_manager *dqm, 2732 const struct queue *q, 2733 u32 *mqd_size, 2734 u32 *ctl_stack_size) 2735 { 2736 struct mqd_manager *mqd_mgr; 2737 enum KFD_MQD_TYPE mqd_type = 2738 get_mqd_type_from_queue_type(q->properties.type); 2739 2740 dqm_lock(dqm); 2741 mqd_mgr = dqm->mqd_mgrs[mqd_type]; 2742 *mqd_size = mqd_mgr->mqd_size * NUM_XCC(mqd_mgr->dev->xcc_mask); 2743 *ctl_stack_size = 0; 2744 2745 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE && mqd_mgr->get_checkpoint_info) 2746 mqd_mgr->get_checkpoint_info(mqd_mgr, q->mqd, ctl_stack_size); 2747 2748 dqm_unlock(dqm); 2749 } 2750 2751 static int checkpoint_mqd(struct device_queue_manager *dqm, 2752 const struct queue *q, 2753 void *mqd, 2754 void *ctl_stack) 2755 { 2756 struct mqd_manager *mqd_mgr; 2757 int r = 0; 2758 enum KFD_MQD_TYPE mqd_type = 2759 get_mqd_type_from_queue_type(q->properties.type); 2760 2761 dqm_lock(dqm); 2762 2763 if (q->properties.is_active || !q->device->kfd->cwsr_enabled) { 2764 r = -EINVAL; 2765 goto dqm_unlock; 2766 } 2767 2768 mqd_mgr = dqm->mqd_mgrs[mqd_type]; 2769 if (!mqd_mgr->checkpoint_mqd) { 2770 r = -EOPNOTSUPP; 2771 goto dqm_unlock; 2772 } 2773 2774 mqd_mgr->checkpoint_mqd(mqd_mgr, q->mqd, mqd, ctl_stack); 2775 2776 dqm_unlock: 2777 dqm_unlock(dqm); 2778 return r; 2779 } 2780 2781 static int process_termination_cpsch(struct device_queue_manager *dqm, 2782 struct qcm_process_device *qpd) 2783 { 2784 int retval = 0; 2785 struct queue *q; 2786 struct device *dev = dqm->dev->adev->dev; 2787 struct kernel_queue *kq, *kq_next; 2788 struct mqd_manager *mqd_mgr; 2789 struct device_process_node *cur, *next_dpn; 2790 enum kfd_unmap_queues_filter filter = 2791 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES; 2792 bool found = false; 2793 2794 dqm_lock(dqm); 2795 2796 /* Clean all kernel queues */ 2797 list_for_each_entry_safe(kq, kq_next, &qpd->priv_queue_list, list) { 2798 list_del(&kq->list); 2799 decrement_queue_count(dqm, qpd, kq->queue); 2800 qpd->is_debug = false; 2801 dqm->total_queue_count--; 2802 filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES; 2803 } 2804 2805 /* Clear all user mode queues */ 2806 list_for_each_entry(q, &qpd->queues_list, list) { 2807 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) 2808 deallocate_sdma_queue(dqm, q); 2809 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 2810 deallocate_sdma_queue(dqm, q); 2811 2812 if (q->properties.is_active) { 2813 decrement_queue_count(dqm, qpd, q); 2814 2815 if (dqm->dev->kfd->shared_resources.enable_mes) { 2816 retval = remove_queue_mes(dqm, q, qpd); 2817 if (retval) 2818 dev_err(dev, "Failed to remove queue %d\n", 2819 q->properties.queue_id); 2820 } 2821 } 2822 2823 dqm->total_queue_count--; 2824 } 2825 2826 /* Unregister process */ 2827 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) { 2828 if (qpd == cur->qpd) { 2829 list_del(&cur->list); 2830 kfree(cur); 2831 dqm->processes_count--; 2832 found = true; 2833 break; 2834 } 2835 } 2836 2837 if (!dqm->dev->kfd->shared_resources.enable_mes) 2838 retval = execute_queues_cpsch(dqm, filter, 0, USE_DEFAULT_GRACE_PERIOD); 2839 2840 if ((retval || qpd->reset_wavefronts) && 2841 down_read_trylock(&dqm->dev->adev->reset_domain->sem)) { 2842 pr_warn("Resetting wave fronts (cpsch) on dev %p\n", dqm->dev); 2843 dbgdev_wave_reset_wavefronts(dqm->dev, qpd->pqm->process); 2844 qpd->reset_wavefronts = false; 2845 up_read(&dqm->dev->adev->reset_domain->sem); 2846 } 2847 2848 /* Lastly, free mqd resources. 2849 * Do free_mqd() after dqm_unlock to avoid circular locking. 2850 */ 2851 while (!list_empty(&qpd->queues_list)) { 2852 q = list_first_entry(&qpd->queues_list, struct queue, list); 2853 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 2854 q->properties.type)]; 2855 list_del(&q->list); 2856 qpd->queue_count--; 2857 dqm_unlock(dqm); 2858 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 2859 dqm_lock(dqm); 2860 } 2861 dqm_unlock(dqm); 2862 2863 /* Outside the DQM lock because under the DQM lock we can't do 2864 * reclaim or take other locks that others hold while reclaiming. 2865 */ 2866 if (found) 2867 kfd_dec_compute_active(dqm->dev); 2868 2869 return retval; 2870 } 2871 2872 static int init_mqd_managers(struct device_queue_manager *dqm) 2873 { 2874 int i, j; 2875 struct device *dev = dqm->dev->adev->dev; 2876 struct mqd_manager *mqd_mgr; 2877 2878 for (i = 0; i < KFD_MQD_TYPE_MAX; i++) { 2879 mqd_mgr = dqm->asic_ops.mqd_manager_init(i, dqm->dev); 2880 if (!mqd_mgr) { 2881 dev_err(dev, "mqd manager [%d] initialization failed\n", i); 2882 goto out_free; 2883 } 2884 dqm->mqd_mgrs[i] = mqd_mgr; 2885 } 2886 2887 return 0; 2888 2889 out_free: 2890 for (j = 0; j < i; j++) { 2891 kfree(dqm->mqd_mgrs[j]); 2892 dqm->mqd_mgrs[j] = NULL; 2893 } 2894 2895 return -ENOMEM; 2896 } 2897 2898 /* Allocate one hiq mqd (HWS) and all SDMA mqd in a continuous trunk*/ 2899 static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm) 2900 { 2901 int retval; 2902 struct kfd_node *dev = dqm->dev; 2903 struct kfd_mem_obj *mem_obj = &dqm->hiq_sdma_mqd; 2904 uint32_t size = dqm->mqd_mgrs[KFD_MQD_TYPE_SDMA]->mqd_size * 2905 get_num_all_sdma_engines(dqm) * 2906 dev->kfd->device_info.num_sdma_queues_per_engine + 2907 (dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size * 2908 NUM_XCC(dqm->dev->xcc_mask)); 2909 2910 retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size, 2911 &(mem_obj->gtt_mem), &(mem_obj->gpu_addr), 2912 (void *)&(mem_obj->cpu_ptr), false); 2913 2914 return retval; 2915 } 2916 2917 struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) 2918 { 2919 struct device_queue_manager *dqm; 2920 2921 pr_debug("Loading device queue manager\n"); 2922 2923 dqm = kzalloc(sizeof(*dqm), GFP_KERNEL); 2924 if (!dqm) 2925 return NULL; 2926 2927 switch (dev->adev->asic_type) { 2928 /* HWS is not available on Hawaii. */ 2929 case CHIP_HAWAII: 2930 /* HWS depends on CWSR for timely dequeue. CWSR is not 2931 * available on Tonga. 2932 * 2933 * FIXME: This argument also applies to Kaveri. 2934 */ 2935 case CHIP_TONGA: 2936 dqm->sched_policy = KFD_SCHED_POLICY_NO_HWS; 2937 break; 2938 default: 2939 dqm->sched_policy = sched_policy; 2940 break; 2941 } 2942 2943 dqm->dev = dev; 2944 switch (dqm->sched_policy) { 2945 case KFD_SCHED_POLICY_HWS: 2946 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: 2947 /* initialize dqm for cp scheduling */ 2948 dqm->ops.create_queue = create_queue_cpsch; 2949 dqm->ops.initialize = initialize_cpsch; 2950 dqm->ops.start = start_cpsch; 2951 dqm->ops.stop = stop_cpsch; 2952 dqm->ops.halt = halt_cpsch; 2953 dqm->ops.unhalt = unhalt_cpsch; 2954 dqm->ops.destroy_queue = destroy_queue_cpsch; 2955 dqm->ops.update_queue = update_queue; 2956 dqm->ops.register_process = register_process; 2957 dqm->ops.unregister_process = unregister_process; 2958 dqm->ops.uninitialize = uninitialize; 2959 dqm->ops.create_kernel_queue = create_kernel_queue_cpsch; 2960 dqm->ops.destroy_kernel_queue = destroy_kernel_queue_cpsch; 2961 dqm->ops.set_cache_memory_policy = set_cache_memory_policy; 2962 dqm->ops.process_termination = process_termination_cpsch; 2963 dqm->ops.evict_process_queues = evict_process_queues_cpsch; 2964 dqm->ops.restore_process_queues = restore_process_queues_cpsch; 2965 dqm->ops.get_wave_state = get_wave_state; 2966 dqm->ops.reset_queues = reset_queues_cpsch; 2967 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; 2968 dqm->ops.checkpoint_mqd = checkpoint_mqd; 2969 break; 2970 case KFD_SCHED_POLICY_NO_HWS: 2971 /* initialize dqm for no cp scheduling */ 2972 dqm->ops.start = start_nocpsch; 2973 dqm->ops.stop = stop_nocpsch; 2974 dqm->ops.create_queue = create_queue_nocpsch; 2975 dqm->ops.destroy_queue = destroy_queue_nocpsch; 2976 dqm->ops.update_queue = update_queue; 2977 dqm->ops.register_process = register_process; 2978 dqm->ops.unregister_process = unregister_process; 2979 dqm->ops.initialize = initialize_nocpsch; 2980 dqm->ops.uninitialize = uninitialize; 2981 dqm->ops.set_cache_memory_policy = set_cache_memory_policy; 2982 dqm->ops.process_termination = process_termination_nocpsch; 2983 dqm->ops.evict_process_queues = evict_process_queues_nocpsch; 2984 dqm->ops.restore_process_queues = 2985 restore_process_queues_nocpsch; 2986 dqm->ops.get_wave_state = get_wave_state; 2987 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; 2988 dqm->ops.checkpoint_mqd = checkpoint_mqd; 2989 break; 2990 default: 2991 dev_err(dev->adev->dev, "Invalid scheduling policy %d\n", dqm->sched_policy); 2992 goto out_free; 2993 } 2994 2995 switch (dev->adev->asic_type) { 2996 case CHIP_KAVERI: 2997 case CHIP_HAWAII: 2998 device_queue_manager_init_cik(&dqm->asic_ops); 2999 break; 3000 3001 case CHIP_CARRIZO: 3002 case CHIP_TONGA: 3003 case CHIP_FIJI: 3004 case CHIP_POLARIS10: 3005 case CHIP_POLARIS11: 3006 case CHIP_POLARIS12: 3007 case CHIP_VEGAM: 3008 device_queue_manager_init_vi(&dqm->asic_ops); 3009 break; 3010 3011 default: 3012 if (KFD_GC_VERSION(dev) >= IP_VERSION(12, 0, 0)) 3013 device_queue_manager_init_v12(&dqm->asic_ops); 3014 else if (KFD_GC_VERSION(dev) >= IP_VERSION(11, 0, 0)) 3015 device_queue_manager_init_v11(&dqm->asic_ops); 3016 else if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1)) 3017 device_queue_manager_init_v10(&dqm->asic_ops); 3018 else if (KFD_GC_VERSION(dev) >= IP_VERSION(9, 0, 1)) 3019 device_queue_manager_init_v9(&dqm->asic_ops); 3020 else { 3021 WARN(1, "Unexpected ASIC family %u", 3022 dev->adev->asic_type); 3023 goto out_free; 3024 } 3025 } 3026 3027 if (init_mqd_managers(dqm)) 3028 goto out_free; 3029 3030 if (!dev->kfd->shared_resources.enable_mes && allocate_hiq_sdma_mqd(dqm)) { 3031 dev_err(dev->adev->dev, "Failed to allocate hiq sdma mqd trunk buffer\n"); 3032 goto out_free; 3033 } 3034 3035 if (!dqm->ops.initialize(dqm)) { 3036 init_waitqueue_head(&dqm->destroy_wait); 3037 return dqm; 3038 } 3039 3040 out_free: 3041 kfree(dqm); 3042 return NULL; 3043 } 3044 3045 static void deallocate_hiq_sdma_mqd(struct kfd_node *dev, 3046 struct kfd_mem_obj *mqd) 3047 { 3048 WARN(!mqd, "No hiq sdma mqd trunk to free"); 3049 3050 amdgpu_amdkfd_free_gtt_mem(dev->adev, &mqd->gtt_mem); 3051 } 3052 3053 void device_queue_manager_uninit(struct device_queue_manager *dqm) 3054 { 3055 dqm->ops.stop(dqm); 3056 dqm->ops.uninitialize(dqm); 3057 if (!dqm->dev->kfd->shared_resources.enable_mes) 3058 deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd); 3059 kfree(dqm); 3060 } 3061 3062 int kfd_dqm_suspend_bad_queue_mes(struct kfd_node *knode, u32 pasid, u32 doorbell_id) 3063 { 3064 struct kfd_process_device *pdd = NULL; 3065 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, &pdd); 3066 struct device_queue_manager *dqm = knode->dqm; 3067 struct device *dev = dqm->dev->adev->dev; 3068 struct qcm_process_device *qpd; 3069 struct queue *q = NULL; 3070 int ret = 0; 3071 3072 if (!pdd) 3073 return -EINVAL; 3074 3075 dqm_lock(dqm); 3076 3077 if (pdd) { 3078 qpd = &pdd->qpd; 3079 3080 list_for_each_entry(q, &qpd->queues_list, list) { 3081 if (q->doorbell_id == doorbell_id && q->properties.is_active) { 3082 ret = suspend_all_queues_mes(dqm); 3083 if (ret) { 3084 dev_err(dev, "Suspending all queues failed"); 3085 goto out; 3086 } 3087 3088 q->properties.is_evicted = true; 3089 q->properties.is_active = false; 3090 decrement_queue_count(dqm, qpd, q); 3091 3092 ret = remove_queue_mes(dqm, q, qpd); 3093 if (ret) { 3094 dev_err(dev, "Removing bad queue failed"); 3095 goto out; 3096 } 3097 3098 ret = resume_all_queues_mes(dqm); 3099 if (ret) 3100 dev_err(dev, "Resuming all queues failed"); 3101 3102 break; 3103 } 3104 } 3105 } 3106 3107 out: 3108 dqm_unlock(dqm); 3109 kfd_unref_process(p); 3110 return ret; 3111 } 3112 3113 int kfd_evict_process_device(struct kfd_process_device *pdd) 3114 { 3115 struct device_queue_manager *dqm; 3116 struct kfd_process *p; 3117 3118 p = pdd->process; 3119 dqm = pdd->dev->dqm; 3120 3121 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid); 3122 3123 return dqm->ops.evict_process_queues(dqm, &pdd->qpd); 3124 } 3125 3126 int reserve_debug_trap_vmid(struct device_queue_manager *dqm, 3127 struct qcm_process_device *qpd) 3128 { 3129 int r; 3130 struct device *dev = dqm->dev->adev->dev; 3131 int updated_vmid_mask; 3132 3133 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) { 3134 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy); 3135 return -EINVAL; 3136 } 3137 3138 dqm_lock(dqm); 3139 3140 if (dqm->trap_debug_vmid != 0) { 3141 dev_err(dev, "Trap debug id already reserved\n"); 3142 r = -EBUSY; 3143 goto out_unlock; 3144 } 3145 3146 r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, 3147 USE_DEFAULT_GRACE_PERIOD, false); 3148 if (r) 3149 goto out_unlock; 3150 3151 updated_vmid_mask = dqm->dev->kfd->shared_resources.compute_vmid_bitmap; 3152 updated_vmid_mask &= ~(1 << dqm->dev->vm_info.last_vmid_kfd); 3153 3154 dqm->dev->kfd->shared_resources.compute_vmid_bitmap = updated_vmid_mask; 3155 dqm->trap_debug_vmid = dqm->dev->vm_info.last_vmid_kfd; 3156 r = set_sched_resources(dqm); 3157 if (r) 3158 goto out_unlock; 3159 3160 r = map_queues_cpsch(dqm); 3161 if (r) 3162 goto out_unlock; 3163 3164 pr_debug("Reserved VMID for trap debug: %i\n", dqm->trap_debug_vmid); 3165 3166 out_unlock: 3167 dqm_unlock(dqm); 3168 return r; 3169 } 3170 3171 /* 3172 * Releases vmid for the trap debugger 3173 */ 3174 int release_debug_trap_vmid(struct device_queue_manager *dqm, 3175 struct qcm_process_device *qpd) 3176 { 3177 struct device *dev = dqm->dev->adev->dev; 3178 int r; 3179 int updated_vmid_mask; 3180 uint32_t trap_debug_vmid; 3181 3182 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) { 3183 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy); 3184 return -EINVAL; 3185 } 3186 3187 dqm_lock(dqm); 3188 trap_debug_vmid = dqm->trap_debug_vmid; 3189 if (dqm->trap_debug_vmid == 0) { 3190 dev_err(dev, "Trap debug id is not reserved\n"); 3191 r = -EINVAL; 3192 goto out_unlock; 3193 } 3194 3195 r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, 3196 USE_DEFAULT_GRACE_PERIOD, false); 3197 if (r) 3198 goto out_unlock; 3199 3200 updated_vmid_mask = dqm->dev->kfd->shared_resources.compute_vmid_bitmap; 3201 updated_vmid_mask |= (1 << dqm->dev->vm_info.last_vmid_kfd); 3202 3203 dqm->dev->kfd->shared_resources.compute_vmid_bitmap = updated_vmid_mask; 3204 dqm->trap_debug_vmid = 0; 3205 r = set_sched_resources(dqm); 3206 if (r) 3207 goto out_unlock; 3208 3209 r = map_queues_cpsch(dqm); 3210 if (r) 3211 goto out_unlock; 3212 3213 pr_debug("Released VMID for trap debug: %i\n", trap_debug_vmid); 3214 3215 out_unlock: 3216 dqm_unlock(dqm); 3217 return r; 3218 } 3219 3220 #define QUEUE_NOT_FOUND -1 3221 /* invalidate queue operation in array */ 3222 static void q_array_invalidate(uint32_t num_queues, uint32_t *queue_ids) 3223 { 3224 int i; 3225 3226 for (i = 0; i < num_queues; i++) 3227 queue_ids[i] |= KFD_DBG_QUEUE_INVALID_MASK; 3228 } 3229 3230 /* find queue index in array */ 3231 static int q_array_get_index(unsigned int queue_id, 3232 uint32_t num_queues, 3233 uint32_t *queue_ids) 3234 { 3235 int i; 3236 3237 for (i = 0; i < num_queues; i++) 3238 if (queue_id == (queue_ids[i] & ~KFD_DBG_QUEUE_INVALID_MASK)) 3239 return i; 3240 3241 return QUEUE_NOT_FOUND; 3242 } 3243 3244 struct copy_context_work_handler_workarea { 3245 struct work_struct copy_context_work; 3246 struct kfd_process *p; 3247 }; 3248 3249 static void copy_context_work_handler(struct work_struct *work) 3250 { 3251 struct copy_context_work_handler_workarea *workarea; 3252 struct mqd_manager *mqd_mgr; 3253 struct queue *q; 3254 struct mm_struct *mm; 3255 struct kfd_process *p; 3256 uint32_t tmp_ctl_stack_used_size, tmp_save_area_used_size; 3257 int i; 3258 3259 workarea = container_of(work, 3260 struct copy_context_work_handler_workarea, 3261 copy_context_work); 3262 3263 p = workarea->p; 3264 mm = get_task_mm(p->lead_thread); 3265 3266 if (!mm) 3267 return; 3268 3269 kthread_use_mm(mm); 3270 for (i = 0; i < p->n_pdds; i++) { 3271 struct kfd_process_device *pdd = p->pdds[i]; 3272 struct device_queue_manager *dqm = pdd->dev->dqm; 3273 struct qcm_process_device *qpd = &pdd->qpd; 3274 3275 list_for_each_entry(q, &qpd->queues_list, list) { 3276 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE) 3277 continue; 3278 3279 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP]; 3280 3281 /* We ignore the return value from get_wave_state 3282 * because 3283 * i) right now, it always returns 0, and 3284 * ii) if we hit an error, we would continue to the 3285 * next queue anyway. 3286 */ 3287 mqd_mgr->get_wave_state(mqd_mgr, 3288 q->mqd, 3289 &q->properties, 3290 (void __user *) q->properties.ctx_save_restore_area_address, 3291 &tmp_ctl_stack_used_size, 3292 &tmp_save_area_used_size); 3293 } 3294 } 3295 kthread_unuse_mm(mm); 3296 mmput(mm); 3297 } 3298 3299 static uint32_t *get_queue_ids(uint32_t num_queues, uint32_t *usr_queue_id_array) 3300 { 3301 size_t array_size = num_queues * sizeof(uint32_t); 3302 3303 if (!usr_queue_id_array) 3304 return NULL; 3305 3306 return memdup_user(usr_queue_id_array, array_size); 3307 } 3308 3309 int resume_queues(struct kfd_process *p, 3310 uint32_t num_queues, 3311 uint32_t *usr_queue_id_array) 3312 { 3313 uint32_t *queue_ids = NULL; 3314 int total_resumed = 0; 3315 int i; 3316 3317 if (usr_queue_id_array) { 3318 queue_ids = get_queue_ids(num_queues, usr_queue_id_array); 3319 3320 if (IS_ERR(queue_ids)) 3321 return PTR_ERR(queue_ids); 3322 3323 /* mask all queues as invalid. unmask per successful request */ 3324 q_array_invalidate(num_queues, queue_ids); 3325 } 3326 3327 for (i = 0; i < p->n_pdds; i++) { 3328 struct kfd_process_device *pdd = p->pdds[i]; 3329 struct device_queue_manager *dqm = pdd->dev->dqm; 3330 struct device *dev = dqm->dev->adev->dev; 3331 struct qcm_process_device *qpd = &pdd->qpd; 3332 struct queue *q; 3333 int r, per_device_resumed = 0; 3334 3335 dqm_lock(dqm); 3336 3337 /* unmask queues that resume or already resumed as valid */ 3338 list_for_each_entry(q, &qpd->queues_list, list) { 3339 int q_idx = QUEUE_NOT_FOUND; 3340 3341 if (queue_ids) 3342 q_idx = q_array_get_index( 3343 q->properties.queue_id, 3344 num_queues, 3345 queue_ids); 3346 3347 if (!queue_ids || q_idx != QUEUE_NOT_FOUND) { 3348 int err = resume_single_queue(dqm, &pdd->qpd, q); 3349 3350 if (queue_ids) { 3351 if (!err) { 3352 queue_ids[q_idx] &= 3353 ~KFD_DBG_QUEUE_INVALID_MASK; 3354 } else { 3355 queue_ids[q_idx] |= 3356 KFD_DBG_QUEUE_ERROR_MASK; 3357 break; 3358 } 3359 } 3360 3361 if (dqm->dev->kfd->shared_resources.enable_mes) { 3362 wake_up_all(&dqm->destroy_wait); 3363 if (!err) 3364 total_resumed++; 3365 } else { 3366 per_device_resumed++; 3367 } 3368 } 3369 } 3370 3371 if (!per_device_resumed) { 3372 dqm_unlock(dqm); 3373 continue; 3374 } 3375 3376 r = execute_queues_cpsch(dqm, 3377 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 3378 0, 3379 USE_DEFAULT_GRACE_PERIOD); 3380 if (r) { 3381 dev_err(dev, "Failed to resume process queues\n"); 3382 if (queue_ids) { 3383 list_for_each_entry(q, &qpd->queues_list, list) { 3384 int q_idx = q_array_get_index( 3385 q->properties.queue_id, 3386 num_queues, 3387 queue_ids); 3388 3389 /* mask queue as error on resume fail */ 3390 if (q_idx != QUEUE_NOT_FOUND) 3391 queue_ids[q_idx] |= 3392 KFD_DBG_QUEUE_ERROR_MASK; 3393 } 3394 } 3395 } else { 3396 wake_up_all(&dqm->destroy_wait); 3397 total_resumed += per_device_resumed; 3398 } 3399 3400 dqm_unlock(dqm); 3401 } 3402 3403 if (queue_ids) { 3404 if (copy_to_user((void __user *)usr_queue_id_array, queue_ids, 3405 num_queues * sizeof(uint32_t))) 3406 pr_err("copy_to_user failed on queue resume\n"); 3407 3408 kfree(queue_ids); 3409 } 3410 3411 return total_resumed; 3412 } 3413 3414 int suspend_queues(struct kfd_process *p, 3415 uint32_t num_queues, 3416 uint32_t grace_period, 3417 uint64_t exception_clear_mask, 3418 uint32_t *usr_queue_id_array) 3419 { 3420 uint32_t *queue_ids = get_queue_ids(num_queues, usr_queue_id_array); 3421 int total_suspended = 0; 3422 int i; 3423 3424 if (IS_ERR(queue_ids)) 3425 return PTR_ERR(queue_ids); 3426 3427 /* mask all queues as invalid. umask on successful request */ 3428 q_array_invalidate(num_queues, queue_ids); 3429 3430 for (i = 0; i < p->n_pdds; i++) { 3431 struct kfd_process_device *pdd = p->pdds[i]; 3432 struct device_queue_manager *dqm = pdd->dev->dqm; 3433 struct device *dev = dqm->dev->adev->dev; 3434 struct qcm_process_device *qpd = &pdd->qpd; 3435 struct queue *q; 3436 int r, per_device_suspended = 0; 3437 3438 mutex_lock(&p->event_mutex); 3439 dqm_lock(dqm); 3440 3441 /* unmask queues that suspend or already suspended */ 3442 list_for_each_entry(q, &qpd->queues_list, list) { 3443 int q_idx = q_array_get_index(q->properties.queue_id, 3444 num_queues, 3445 queue_ids); 3446 3447 if (q_idx != QUEUE_NOT_FOUND) { 3448 int err = suspend_single_queue(dqm, pdd, q); 3449 bool is_mes = dqm->dev->kfd->shared_resources.enable_mes; 3450 3451 if (!err) { 3452 queue_ids[q_idx] &= ~KFD_DBG_QUEUE_INVALID_MASK; 3453 if (exception_clear_mask && is_mes) 3454 q->properties.exception_status &= 3455 ~exception_clear_mask; 3456 3457 if (is_mes) 3458 total_suspended++; 3459 else 3460 per_device_suspended++; 3461 } else if (err != -EBUSY) { 3462 queue_ids[q_idx] |= KFD_DBG_QUEUE_ERROR_MASK; 3463 break; 3464 } 3465 } 3466 } 3467 3468 if (!per_device_suspended) { 3469 dqm_unlock(dqm); 3470 mutex_unlock(&p->event_mutex); 3471 if (total_suspended) 3472 amdgpu_amdkfd_debug_mem_fence(dqm->dev->adev); 3473 continue; 3474 } 3475 3476 r = execute_queues_cpsch(dqm, 3477 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, 3478 grace_period); 3479 3480 if (r) 3481 dev_err(dev, "Failed to suspend process queues.\n"); 3482 else 3483 total_suspended += per_device_suspended; 3484 3485 list_for_each_entry(q, &qpd->queues_list, list) { 3486 int q_idx = q_array_get_index(q->properties.queue_id, 3487 num_queues, queue_ids); 3488 3489 if (q_idx == QUEUE_NOT_FOUND) 3490 continue; 3491 3492 /* mask queue as error on suspend fail */ 3493 if (r) 3494 queue_ids[q_idx] |= KFD_DBG_QUEUE_ERROR_MASK; 3495 else if (exception_clear_mask) 3496 q->properties.exception_status &= 3497 ~exception_clear_mask; 3498 } 3499 3500 dqm_unlock(dqm); 3501 mutex_unlock(&p->event_mutex); 3502 amdgpu_device_flush_hdp(dqm->dev->adev, NULL); 3503 } 3504 3505 if (total_suspended) { 3506 struct copy_context_work_handler_workarea copy_context_worker; 3507 3508 INIT_WORK_ONSTACK( 3509 ©_context_worker.copy_context_work, 3510 copy_context_work_handler); 3511 3512 copy_context_worker.p = p; 3513 3514 schedule_work(©_context_worker.copy_context_work); 3515 3516 3517 flush_work(©_context_worker.copy_context_work); 3518 destroy_work_on_stack(©_context_worker.copy_context_work); 3519 } 3520 3521 if (copy_to_user((void __user *)usr_queue_id_array, queue_ids, 3522 num_queues * sizeof(uint32_t))) 3523 pr_err("copy_to_user failed on queue suspend\n"); 3524 3525 kfree(queue_ids); 3526 3527 return total_suspended; 3528 } 3529 3530 static uint32_t set_queue_type_for_user(struct queue_properties *q_props) 3531 { 3532 switch (q_props->type) { 3533 case KFD_QUEUE_TYPE_COMPUTE: 3534 return q_props->format == KFD_QUEUE_FORMAT_PM4 3535 ? KFD_IOC_QUEUE_TYPE_COMPUTE 3536 : KFD_IOC_QUEUE_TYPE_COMPUTE_AQL; 3537 case KFD_QUEUE_TYPE_SDMA: 3538 return KFD_IOC_QUEUE_TYPE_SDMA; 3539 case KFD_QUEUE_TYPE_SDMA_XGMI: 3540 return KFD_IOC_QUEUE_TYPE_SDMA_XGMI; 3541 default: 3542 WARN_ONCE(true, "queue type not recognized!"); 3543 return 0xffffffff; 3544 }; 3545 } 3546 3547 void set_queue_snapshot_entry(struct queue *q, 3548 uint64_t exception_clear_mask, 3549 struct kfd_queue_snapshot_entry *qss_entry) 3550 { 3551 qss_entry->ring_base_address = q->properties.queue_address; 3552 qss_entry->write_pointer_address = (uint64_t)q->properties.write_ptr; 3553 qss_entry->read_pointer_address = (uint64_t)q->properties.read_ptr; 3554 qss_entry->ctx_save_restore_address = 3555 q->properties.ctx_save_restore_area_address; 3556 qss_entry->ctx_save_restore_area_size = 3557 q->properties.ctx_save_restore_area_size; 3558 qss_entry->exception_status = q->properties.exception_status; 3559 qss_entry->queue_id = q->properties.queue_id; 3560 qss_entry->gpu_id = q->device->id; 3561 qss_entry->ring_size = (uint32_t)q->properties.queue_size; 3562 qss_entry->queue_type = set_queue_type_for_user(&q->properties); 3563 q->properties.exception_status &= ~exception_clear_mask; 3564 } 3565 3566 int debug_lock_and_unmap(struct device_queue_manager *dqm) 3567 { 3568 struct device *dev = dqm->dev->adev->dev; 3569 int r; 3570 3571 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) { 3572 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy); 3573 return -EINVAL; 3574 } 3575 3576 if (!kfd_dbg_is_per_vmid_supported(dqm->dev)) 3577 return 0; 3578 3579 dqm_lock(dqm); 3580 3581 r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, 0, false); 3582 if (r) 3583 dqm_unlock(dqm); 3584 3585 return r; 3586 } 3587 3588 int debug_map_and_unlock(struct device_queue_manager *dqm) 3589 { 3590 struct device *dev = dqm->dev->adev->dev; 3591 int r; 3592 3593 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) { 3594 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy); 3595 return -EINVAL; 3596 } 3597 3598 if (!kfd_dbg_is_per_vmid_supported(dqm->dev)) 3599 return 0; 3600 3601 r = map_queues_cpsch(dqm); 3602 3603 dqm_unlock(dqm); 3604 3605 return r; 3606 } 3607 3608 int debug_refresh_runlist(struct device_queue_manager *dqm) 3609 { 3610 int r = debug_lock_and_unmap(dqm); 3611 3612 if (r) 3613 return r; 3614 3615 return debug_map_and_unlock(dqm); 3616 } 3617 3618 bool kfd_dqm_is_queue_in_process(struct device_queue_manager *dqm, 3619 struct qcm_process_device *qpd, 3620 int doorbell_off, u32 *queue_format) 3621 { 3622 struct queue *q; 3623 bool r = false; 3624 3625 if (!queue_format) 3626 return r; 3627 3628 dqm_lock(dqm); 3629 3630 list_for_each_entry(q, &qpd->queues_list, list) { 3631 if (q->properties.doorbell_off == doorbell_off) { 3632 *queue_format = q->properties.format; 3633 r = true; 3634 goto out; 3635 } 3636 } 3637 3638 out: 3639 dqm_unlock(dqm); 3640 return r; 3641 } 3642 #if defined(CONFIG_DEBUG_FS) 3643 3644 static void seq_reg_dump(struct seq_file *m, 3645 uint32_t (*dump)[2], uint32_t n_regs) 3646 { 3647 uint32_t i, count; 3648 3649 for (i = 0, count = 0; i < n_regs; i++) { 3650 if (count == 0 || 3651 dump[i-1][0] + sizeof(uint32_t) != dump[i][0]) { 3652 seq_printf(m, "%s %08x: %08x", 3653 i ? "\n" : "", 3654 dump[i][0], dump[i][1]); 3655 count = 7; 3656 } else { 3657 seq_printf(m, " %08x", dump[i][1]); 3658 count--; 3659 } 3660 } 3661 3662 seq_puts(m, "\n"); 3663 } 3664 3665 int dqm_debugfs_hqds(struct seq_file *m, void *data) 3666 { 3667 struct device_queue_manager *dqm = data; 3668 uint32_t xcc_mask = dqm->dev->xcc_mask; 3669 uint32_t (*dump)[2], n_regs; 3670 int pipe, queue; 3671 int r = 0, xcc_id; 3672 uint32_t sdma_engine_start; 3673 3674 if (!dqm->sched_running) { 3675 seq_puts(m, " Device is stopped\n"); 3676 return 0; 3677 } 3678 3679 for_each_inst(xcc_id, xcc_mask) { 3680 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev, 3681 KFD_CIK_HIQ_PIPE, 3682 KFD_CIK_HIQ_QUEUE, &dump, 3683 &n_regs, xcc_id); 3684 if (!r) { 3685 seq_printf( 3686 m, 3687 " Inst %d, HIQ on MEC %d Pipe %d Queue %d\n", 3688 xcc_id, 3689 KFD_CIK_HIQ_PIPE / get_pipes_per_mec(dqm) + 1, 3690 KFD_CIK_HIQ_PIPE % get_pipes_per_mec(dqm), 3691 KFD_CIK_HIQ_QUEUE); 3692 seq_reg_dump(m, dump, n_regs); 3693 3694 kfree(dump); 3695 } 3696 3697 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) { 3698 int pipe_offset = pipe * get_queues_per_pipe(dqm); 3699 3700 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) { 3701 if (!test_bit(pipe_offset + queue, 3702 dqm->dev->kfd->shared_resources.cp_queue_bitmap)) 3703 continue; 3704 3705 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev, 3706 pipe, queue, 3707 &dump, &n_regs, 3708 xcc_id); 3709 if (r) 3710 break; 3711 3712 seq_printf(m, 3713 " Inst %d, CP Pipe %d, Queue %d\n", 3714 xcc_id, pipe, queue); 3715 seq_reg_dump(m, dump, n_regs); 3716 3717 kfree(dump); 3718 } 3719 } 3720 } 3721 3722 sdma_engine_start = dqm->dev->node_id * get_num_all_sdma_engines(dqm); 3723 for (pipe = sdma_engine_start; 3724 pipe < (sdma_engine_start + get_num_all_sdma_engines(dqm)); 3725 pipe++) { 3726 for (queue = 0; 3727 queue < dqm->dev->kfd->device_info.num_sdma_queues_per_engine; 3728 queue++) { 3729 r = dqm->dev->kfd2kgd->hqd_sdma_dump( 3730 dqm->dev->adev, pipe, queue, &dump, &n_regs); 3731 if (r) 3732 break; 3733 3734 seq_printf(m, " SDMA Engine %d, RLC %d\n", 3735 pipe, queue); 3736 seq_reg_dump(m, dump, n_regs); 3737 3738 kfree(dump); 3739 } 3740 } 3741 3742 return r; 3743 } 3744 3745 int dqm_debugfs_hang_hws(struct device_queue_manager *dqm) 3746 { 3747 int r = 0; 3748 3749 dqm_lock(dqm); 3750 r = pm_debugfs_hang_hws(&dqm->packet_mgr); 3751 if (r) { 3752 dqm_unlock(dqm); 3753 return r; 3754 } 3755 dqm->active_runlist = true; 3756 r = execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 3757 0, USE_DEFAULT_GRACE_PERIOD); 3758 dqm_unlock(dqm); 3759 3760 return r; 3761 } 3762 3763 #endif 3764