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