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