1 /* 2 * Copyright 2014 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 */ 23 24 #include <linux/ratelimit.h> 25 #include <linux/printk.h> 26 #include <linux/slab.h> 27 #include <linux/list.h> 28 #include <linux/types.h> 29 #include <linux/bitops.h> 30 #include <linux/sched.h> 31 #include "kfd_priv.h" 32 #include "kfd_device_queue_manager.h" 33 #include "kfd_mqd_manager.h" 34 #include "cik_regs.h" 35 #include "kfd_kernel_queue.h" 36 #include "amdgpu_amdkfd.h" 37 38 /* Size of the per-pipe EOP queue */ 39 #define CIK_HPD_EOP_BYTES_LOG2 11 40 #define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2) 41 42 static int set_pasid_vmid_mapping(struct device_queue_manager *dqm, 43 u32 pasid, unsigned int vmid); 44 45 static int execute_queues_cpsch(struct device_queue_manager *dqm, 46 enum kfd_unmap_queues_filter filter, 47 uint32_t filter_param); 48 static int unmap_queues_cpsch(struct device_queue_manager *dqm, 49 enum kfd_unmap_queues_filter filter, 50 uint32_t filter_param); 51 52 static int map_queues_cpsch(struct device_queue_manager *dqm); 53 54 static void deallocate_sdma_queue(struct device_queue_manager *dqm, 55 struct queue *q); 56 57 static inline void deallocate_hqd(struct device_queue_manager *dqm, 58 struct queue *q); 59 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q); 60 static int allocate_sdma_queue(struct device_queue_manager *dqm, 61 struct queue *q); 62 static void kfd_process_hw_exception(struct work_struct *work); 63 64 static inline 65 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type) 66 { 67 if (type == KFD_QUEUE_TYPE_SDMA || type == KFD_QUEUE_TYPE_SDMA_XGMI) 68 return KFD_MQD_TYPE_SDMA; 69 return KFD_MQD_TYPE_CP; 70 } 71 72 static bool is_pipe_enabled(struct device_queue_manager *dqm, int mec, int pipe) 73 { 74 int i; 75 int pipe_offset = (mec * dqm->dev->shared_resources.num_pipe_per_mec 76 + pipe) * dqm->dev->shared_resources.num_queue_per_pipe; 77 78 /* queue is available for KFD usage if bit is 1 */ 79 for (i = 0; i < dqm->dev->shared_resources.num_queue_per_pipe; ++i) 80 if (test_bit(pipe_offset + i, 81 dqm->dev->shared_resources.cp_queue_bitmap)) 82 return true; 83 return false; 84 } 85 86 unsigned int get_cp_queues_num(struct device_queue_manager *dqm) 87 { 88 return bitmap_weight(dqm->dev->shared_resources.cp_queue_bitmap, 89 KGD_MAX_QUEUES); 90 } 91 92 unsigned int get_queues_per_pipe(struct device_queue_manager *dqm) 93 { 94 return dqm->dev->shared_resources.num_queue_per_pipe; 95 } 96 97 unsigned int get_pipes_per_mec(struct device_queue_manager *dqm) 98 { 99 return dqm->dev->shared_resources.num_pipe_per_mec; 100 } 101 102 static unsigned int get_num_sdma_engines(struct device_queue_manager *dqm) 103 { 104 return dqm->dev->device_info->num_sdma_engines; 105 } 106 107 static unsigned int get_num_xgmi_sdma_engines(struct device_queue_manager *dqm) 108 { 109 return dqm->dev->device_info->num_xgmi_sdma_engines; 110 } 111 112 static unsigned int get_num_all_sdma_engines(struct device_queue_manager *dqm) 113 { 114 return get_num_sdma_engines(dqm) + get_num_xgmi_sdma_engines(dqm); 115 } 116 117 unsigned int get_num_sdma_queues(struct device_queue_manager *dqm) 118 { 119 return dqm->dev->device_info->num_sdma_engines 120 * dqm->dev->device_info->num_sdma_queues_per_engine; 121 } 122 123 unsigned int get_num_xgmi_sdma_queues(struct device_queue_manager *dqm) 124 { 125 return dqm->dev->device_info->num_xgmi_sdma_engines 126 * dqm->dev->device_info->num_sdma_queues_per_engine; 127 } 128 129 void program_sh_mem_settings(struct device_queue_manager *dqm, 130 struct qcm_process_device *qpd) 131 { 132 return dqm->dev->kfd2kgd->program_sh_mem_settings( 133 dqm->dev->kgd, qpd->vmid, 134 qpd->sh_mem_config, 135 qpd->sh_mem_ape1_base, 136 qpd->sh_mem_ape1_limit, 137 qpd->sh_mem_bases); 138 } 139 140 static void increment_queue_count(struct device_queue_manager *dqm, 141 enum kfd_queue_type type) 142 { 143 dqm->active_queue_count++; 144 if (type == KFD_QUEUE_TYPE_COMPUTE || type == KFD_QUEUE_TYPE_DIQ) 145 dqm->active_cp_queue_count++; 146 } 147 148 static void decrement_queue_count(struct device_queue_manager *dqm, 149 enum kfd_queue_type type) 150 { 151 dqm->active_queue_count--; 152 if (type == KFD_QUEUE_TYPE_COMPUTE || type == KFD_QUEUE_TYPE_DIQ) 153 dqm->active_cp_queue_count--; 154 } 155 156 static int allocate_doorbell(struct qcm_process_device *qpd, struct queue *q) 157 { 158 struct kfd_dev *dev = qpd->dqm->dev; 159 160 if (!KFD_IS_SOC15(dev->device_info->asic_family)) { 161 /* On pre-SOC15 chips we need to use the queue ID to 162 * preserve the user mode ABI. 163 */ 164 q->doorbell_id = q->properties.queue_id; 165 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 166 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 167 /* For SDMA queues on SOC15 with 8-byte doorbell, use static 168 * doorbell assignments based on the engine and queue id. 169 * The doobell index distance between RLC (2*i) and (2*i+1) 170 * for a SDMA engine is 512. 171 */ 172 uint32_t *idx_offset = 173 dev->shared_resources.sdma_doorbell_idx; 174 175 q->doorbell_id = idx_offset[q->properties.sdma_engine_id] 176 + (q->properties.sdma_queue_id & 1) 177 * KFD_QUEUE_DOORBELL_MIRROR_OFFSET 178 + (q->properties.sdma_queue_id >> 1); 179 } else { 180 /* For CP queues on SOC15 reserve a free doorbell ID */ 181 unsigned int found; 182 183 found = find_first_zero_bit(qpd->doorbell_bitmap, 184 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS); 185 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) { 186 pr_debug("No doorbells available"); 187 return -EBUSY; 188 } 189 set_bit(found, qpd->doorbell_bitmap); 190 q->doorbell_id = found; 191 } 192 193 q->properties.doorbell_off = 194 kfd_get_doorbell_dw_offset_in_bar(dev, qpd_to_pdd(qpd), 195 q->doorbell_id); 196 return 0; 197 } 198 199 static void deallocate_doorbell(struct qcm_process_device *qpd, 200 struct queue *q) 201 { 202 unsigned int old; 203 struct kfd_dev *dev = qpd->dqm->dev; 204 205 if (!KFD_IS_SOC15(dev->device_info->asic_family) || 206 q->properties.type == KFD_QUEUE_TYPE_SDMA || 207 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 208 return; 209 210 old = test_and_clear_bit(q->doorbell_id, qpd->doorbell_bitmap); 211 WARN_ON(!old); 212 } 213 214 static void program_trap_handler_settings(struct device_queue_manager *dqm, 215 struct qcm_process_device *qpd) 216 { 217 if (dqm->dev->kfd2kgd->program_trap_handler_settings) 218 dqm->dev->kfd2kgd->program_trap_handler_settings( 219 dqm->dev->kgd, qpd->vmid, 220 qpd->tba_addr, qpd->tma_addr); 221 } 222 223 static int allocate_vmid(struct device_queue_manager *dqm, 224 struct qcm_process_device *qpd, 225 struct queue *q) 226 { 227 int allocated_vmid = -1, i; 228 229 for (i = dqm->dev->vm_info.first_vmid_kfd; 230 i <= dqm->dev->vm_info.last_vmid_kfd; i++) { 231 if (!dqm->vmid_pasid[i]) { 232 allocated_vmid = i; 233 break; 234 } 235 } 236 237 if (allocated_vmid < 0) { 238 pr_err("no more vmid to allocate\n"); 239 return -ENOSPC; 240 } 241 242 pr_debug("vmid allocated: %d\n", allocated_vmid); 243 244 dqm->vmid_pasid[allocated_vmid] = q->process->pasid; 245 246 set_pasid_vmid_mapping(dqm, q->process->pasid, allocated_vmid); 247 248 qpd->vmid = allocated_vmid; 249 q->properties.vmid = allocated_vmid; 250 251 program_sh_mem_settings(dqm, qpd); 252 253 if (dqm->dev->device_info->asic_family >= CHIP_VEGA10 && 254 dqm->dev->cwsr_enabled) 255 program_trap_handler_settings(dqm, qpd); 256 257 /* qpd->page_table_base is set earlier when register_process() 258 * is called, i.e. when the first queue is created. 259 */ 260 dqm->dev->kfd2kgd->set_vm_context_page_table_base(dqm->dev->kgd, 261 qpd->vmid, 262 qpd->page_table_base); 263 /* invalidate the VM context after pasid and vmid mapping is set up */ 264 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY); 265 266 if (dqm->dev->kfd2kgd->set_scratch_backing_va) 267 dqm->dev->kfd2kgd->set_scratch_backing_va(dqm->dev->kgd, 268 qpd->sh_hidden_private_base, qpd->vmid); 269 270 return 0; 271 } 272 273 static int flush_texture_cache_nocpsch(struct kfd_dev *kdev, 274 struct qcm_process_device *qpd) 275 { 276 const struct packet_manager_funcs *pmf = qpd->dqm->packet_mgr.pmf; 277 int ret; 278 279 if (!qpd->ib_kaddr) 280 return -ENOMEM; 281 282 ret = pmf->release_mem(qpd->ib_base, (uint32_t *)qpd->ib_kaddr); 283 if (ret) 284 return ret; 285 286 return amdgpu_amdkfd_submit_ib(kdev->kgd, KGD_ENGINE_MEC1, qpd->vmid, 287 qpd->ib_base, (uint32_t *)qpd->ib_kaddr, 288 pmf->release_mem_size / sizeof(uint32_t)); 289 } 290 291 static void deallocate_vmid(struct device_queue_manager *dqm, 292 struct qcm_process_device *qpd, 293 struct queue *q) 294 { 295 /* On GFX v7, CP doesn't flush TC at dequeue */ 296 if (q->device->device_info->asic_family == CHIP_HAWAII) 297 if (flush_texture_cache_nocpsch(q->device, qpd)) 298 pr_err("Failed to flush TC\n"); 299 300 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY); 301 302 /* Release the vmid mapping */ 303 set_pasid_vmid_mapping(dqm, 0, qpd->vmid); 304 dqm->vmid_pasid[qpd->vmid] = 0; 305 306 qpd->vmid = 0; 307 q->properties.vmid = 0; 308 } 309 310 static int create_queue_nocpsch(struct device_queue_manager *dqm, 311 struct queue *q, 312 struct qcm_process_device *qpd) 313 { 314 struct mqd_manager *mqd_mgr; 315 int retval; 316 317 dqm_lock(dqm); 318 319 if (dqm->total_queue_count >= max_num_of_queues_per_device) { 320 pr_warn("Can't create new usermode queue because %d queues were already created\n", 321 dqm->total_queue_count); 322 retval = -EPERM; 323 goto out_unlock; 324 } 325 326 if (list_empty(&qpd->queues_list)) { 327 retval = allocate_vmid(dqm, qpd, q); 328 if (retval) 329 goto out_unlock; 330 } 331 q->properties.vmid = qpd->vmid; 332 /* 333 * Eviction state logic: mark all queues as evicted, even ones 334 * not currently active. Restoring inactive queues later only 335 * updates the is_evicted flag but is a no-op otherwise. 336 */ 337 q->properties.is_evicted = !!qpd->evicted; 338 339 q->properties.tba_addr = qpd->tba_addr; 340 q->properties.tma_addr = qpd->tma_addr; 341 342 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 343 q->properties.type)]; 344 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) { 345 retval = allocate_hqd(dqm, q); 346 if (retval) 347 goto deallocate_vmid; 348 pr_debug("Loading mqd to hqd on pipe %d, queue %d\n", 349 q->pipe, q->queue); 350 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 351 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 352 retval = allocate_sdma_queue(dqm, q); 353 if (retval) 354 goto deallocate_vmid; 355 dqm->asic_ops.init_sdma_vm(dqm, q, qpd); 356 } 357 358 retval = allocate_doorbell(qpd, q); 359 if (retval) 360 goto out_deallocate_hqd; 361 362 /* Temporarily release dqm lock to avoid a circular lock dependency */ 363 dqm_unlock(dqm); 364 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties); 365 dqm_lock(dqm); 366 367 if (!q->mqd_mem_obj) { 368 retval = -ENOMEM; 369 goto out_deallocate_doorbell; 370 } 371 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, 372 &q->gart_mqd_addr, &q->properties); 373 if (q->properties.is_active) { 374 if (!dqm->sched_running) { 375 WARN_ONCE(1, "Load non-HWS mqd while stopped\n"); 376 goto add_queue_to_list; 377 } 378 379 if (WARN(q->process->mm != current->mm, 380 "should only run in user thread")) 381 retval = -EFAULT; 382 else 383 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe, 384 q->queue, &q->properties, current->mm); 385 if (retval) 386 goto out_free_mqd; 387 } 388 389 add_queue_to_list: 390 list_add(&q->list, &qpd->queues_list); 391 qpd->queue_count++; 392 if (q->properties.is_active) 393 increment_queue_count(dqm, q->properties.type); 394 395 /* 396 * Unconditionally increment this counter, regardless of the queue's 397 * type or whether the queue is active. 398 */ 399 dqm->total_queue_count++; 400 pr_debug("Total of %d queues are accountable so far\n", 401 dqm->total_queue_count); 402 goto out_unlock; 403 404 out_free_mqd: 405 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 406 out_deallocate_doorbell: 407 deallocate_doorbell(qpd, q); 408 out_deallocate_hqd: 409 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) 410 deallocate_hqd(dqm, q); 411 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 412 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 413 deallocate_sdma_queue(dqm, q); 414 deallocate_vmid: 415 if (list_empty(&qpd->queues_list)) 416 deallocate_vmid(dqm, qpd, q); 417 out_unlock: 418 dqm_unlock(dqm); 419 return retval; 420 } 421 422 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q) 423 { 424 bool set; 425 int pipe, bit, i; 426 427 set = false; 428 429 for (pipe = dqm->next_pipe_to_allocate, i = 0; 430 i < get_pipes_per_mec(dqm); 431 pipe = ((pipe + 1) % get_pipes_per_mec(dqm)), ++i) { 432 433 if (!is_pipe_enabled(dqm, 0, pipe)) 434 continue; 435 436 if (dqm->allocated_queues[pipe] != 0) { 437 bit = ffs(dqm->allocated_queues[pipe]) - 1; 438 dqm->allocated_queues[pipe] &= ~(1 << bit); 439 q->pipe = pipe; 440 q->queue = bit; 441 set = true; 442 break; 443 } 444 } 445 446 if (!set) 447 return -EBUSY; 448 449 pr_debug("hqd slot - pipe %d, queue %d\n", q->pipe, q->queue); 450 /* horizontal hqd allocation */ 451 dqm->next_pipe_to_allocate = (pipe + 1) % get_pipes_per_mec(dqm); 452 453 return 0; 454 } 455 456 static inline void deallocate_hqd(struct device_queue_manager *dqm, 457 struct queue *q) 458 { 459 dqm->allocated_queues[q->pipe] |= (1 << q->queue); 460 } 461 462 /* Access to DQM has to be locked before calling destroy_queue_nocpsch_locked 463 * to avoid asynchronized access 464 */ 465 static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, 466 struct qcm_process_device *qpd, 467 struct queue *q) 468 { 469 int retval; 470 struct mqd_manager *mqd_mgr; 471 472 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 473 q->properties.type)]; 474 475 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) 476 deallocate_hqd(dqm, q); 477 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) 478 deallocate_sdma_queue(dqm, q); 479 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 480 deallocate_sdma_queue(dqm, q); 481 else { 482 pr_debug("q->properties.type %d is invalid\n", 483 q->properties.type); 484 return -EINVAL; 485 } 486 dqm->total_queue_count--; 487 488 deallocate_doorbell(qpd, q); 489 490 if (!dqm->sched_running) { 491 WARN_ONCE(1, "Destroy non-HWS queue while stopped\n"); 492 return 0; 493 } 494 495 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd, 496 KFD_PREEMPT_TYPE_WAVEFRONT_RESET, 497 KFD_UNMAP_LATENCY_MS, 498 q->pipe, q->queue); 499 if (retval == -ETIME) 500 qpd->reset_wavefronts = true; 501 502 list_del(&q->list); 503 if (list_empty(&qpd->queues_list)) { 504 if (qpd->reset_wavefronts) { 505 pr_warn("Resetting wave fronts (nocpsch) on dev %p\n", 506 dqm->dev); 507 /* dbgdev_wave_reset_wavefronts has to be called before 508 * deallocate_vmid(), i.e. when vmid is still in use. 509 */ 510 dbgdev_wave_reset_wavefronts(dqm->dev, 511 qpd->pqm->process); 512 qpd->reset_wavefronts = false; 513 } 514 515 deallocate_vmid(dqm, qpd, q); 516 } 517 qpd->queue_count--; 518 if (q->properties.is_active) { 519 decrement_queue_count(dqm, q->properties.type); 520 if (q->properties.is_gws) { 521 dqm->gws_queue_count--; 522 qpd->mapped_gws_queue = false; 523 } 524 } 525 526 return retval; 527 } 528 529 static int destroy_queue_nocpsch(struct device_queue_manager *dqm, 530 struct qcm_process_device *qpd, 531 struct queue *q) 532 { 533 int retval; 534 uint64_t sdma_val = 0; 535 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 536 struct mqd_manager *mqd_mgr = 537 dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)]; 538 539 /* Get the SDMA queue stats */ 540 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || 541 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 542 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr, 543 &sdma_val); 544 if (retval) 545 pr_err("Failed to read SDMA queue counter for queue: %d\n", 546 q->properties.queue_id); 547 } 548 549 dqm_lock(dqm); 550 retval = destroy_queue_nocpsch_locked(dqm, qpd, q); 551 if (!retval) 552 pdd->sdma_past_activity_counter += sdma_val; 553 dqm_unlock(dqm); 554 555 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 556 557 return retval; 558 } 559 560 static int update_queue(struct device_queue_manager *dqm, struct queue *q, 561 struct mqd_update_info *minfo) 562 { 563 int retval = 0; 564 struct mqd_manager *mqd_mgr; 565 struct kfd_process_device *pdd; 566 bool prev_active = false; 567 568 dqm_lock(dqm); 569 pdd = kfd_get_process_device_data(q->device, q->process); 570 if (!pdd) { 571 retval = -ENODEV; 572 goto out_unlock; 573 } 574 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 575 q->properties.type)]; 576 577 /* Save previous activity state for counters */ 578 prev_active = q->properties.is_active; 579 580 /* Make sure the queue is unmapped before updating the MQD */ 581 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) { 582 retval = unmap_queues_cpsch(dqm, 583 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0); 584 if (retval) { 585 pr_err("unmap queue failed\n"); 586 goto out_unlock; 587 } 588 } else if (prev_active && 589 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE || 590 q->properties.type == KFD_QUEUE_TYPE_SDMA || 591 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 592 593 if (!dqm->sched_running) { 594 WARN_ONCE(1, "Update non-HWS queue while stopped\n"); 595 goto out_unlock; 596 } 597 598 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd, 599 (dqm->dev->cwsr_enabled? 600 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE: 601 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN), 602 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue); 603 if (retval) { 604 pr_err("destroy mqd failed\n"); 605 goto out_unlock; 606 } 607 } 608 609 mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo); 610 611 /* 612 * check active state vs. the previous state and modify 613 * counter accordingly. map_queues_cpsch uses the 614 * dqm->active_queue_count to determine whether a new runlist must be 615 * uploaded. 616 */ 617 if (q->properties.is_active && !prev_active) 618 increment_queue_count(dqm, q->properties.type); 619 else if (!q->properties.is_active && prev_active) 620 decrement_queue_count(dqm, q->properties.type); 621 622 if (q->gws && !q->properties.is_gws) { 623 if (q->properties.is_active) { 624 dqm->gws_queue_count++; 625 pdd->qpd.mapped_gws_queue = true; 626 } 627 q->properties.is_gws = true; 628 } else if (!q->gws && q->properties.is_gws) { 629 if (q->properties.is_active) { 630 dqm->gws_queue_count--; 631 pdd->qpd.mapped_gws_queue = false; 632 } 633 q->properties.is_gws = false; 634 } 635 636 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) 637 retval = map_queues_cpsch(dqm); 638 else if (q->properties.is_active && 639 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE || 640 q->properties.type == KFD_QUEUE_TYPE_SDMA || 641 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 642 if (WARN(q->process->mm != current->mm, 643 "should only run in user thread")) 644 retval = -EFAULT; 645 else 646 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, 647 q->pipe, q->queue, 648 &q->properties, current->mm); 649 } 650 651 out_unlock: 652 dqm_unlock(dqm); 653 return retval; 654 } 655 656 static int evict_process_queues_nocpsch(struct device_queue_manager *dqm, 657 struct qcm_process_device *qpd) 658 { 659 struct queue *q; 660 struct mqd_manager *mqd_mgr; 661 struct kfd_process_device *pdd; 662 int retval, ret = 0; 663 664 dqm_lock(dqm); 665 if (qpd->evicted++ > 0) /* already evicted, do nothing */ 666 goto out; 667 668 pdd = qpd_to_pdd(qpd); 669 pr_debug_ratelimited("Evicting PASID 0x%x queues\n", 670 pdd->process->pasid); 671 672 pdd->last_evict_timestamp = get_jiffies_64(); 673 /* Mark all queues as evicted. Deactivate all active queues on 674 * the qpd. 675 */ 676 list_for_each_entry(q, &qpd->queues_list, list) { 677 q->properties.is_evicted = true; 678 if (!q->properties.is_active) 679 continue; 680 681 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 682 q->properties.type)]; 683 q->properties.is_active = false; 684 decrement_queue_count(dqm, q->properties.type); 685 if (q->properties.is_gws) { 686 dqm->gws_queue_count--; 687 qpd->mapped_gws_queue = false; 688 } 689 690 if (WARN_ONCE(!dqm->sched_running, "Evict when stopped\n")) 691 continue; 692 693 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd, 694 (dqm->dev->cwsr_enabled? 695 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE: 696 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN), 697 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue); 698 if (retval && !ret) 699 /* Return the first error, but keep going to 700 * maintain a consistent eviction state 701 */ 702 ret = retval; 703 } 704 705 out: 706 dqm_unlock(dqm); 707 return ret; 708 } 709 710 static int evict_process_queues_cpsch(struct device_queue_manager *dqm, 711 struct qcm_process_device *qpd) 712 { 713 struct queue *q; 714 struct kfd_process_device *pdd; 715 int retval = 0; 716 717 dqm_lock(dqm); 718 if (qpd->evicted++ > 0) /* already evicted, do nothing */ 719 goto out; 720 721 pdd = qpd_to_pdd(qpd); 722 pr_debug_ratelimited("Evicting PASID 0x%x queues\n", 723 pdd->process->pasid); 724 725 /* Mark all queues as evicted. Deactivate all active queues on 726 * the qpd. 727 */ 728 list_for_each_entry(q, &qpd->queues_list, list) { 729 q->properties.is_evicted = true; 730 if (!q->properties.is_active) 731 continue; 732 733 q->properties.is_active = false; 734 decrement_queue_count(dqm, q->properties.type); 735 } 736 pdd->last_evict_timestamp = get_jiffies_64(); 737 retval = execute_queues_cpsch(dqm, 738 qpd->is_debug ? 739 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES : 740 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0); 741 742 out: 743 dqm_unlock(dqm); 744 return retval; 745 } 746 747 static int restore_process_queues_nocpsch(struct device_queue_manager *dqm, 748 struct qcm_process_device *qpd) 749 { 750 struct mm_struct *mm = NULL; 751 struct queue *q; 752 struct mqd_manager *mqd_mgr; 753 struct kfd_process_device *pdd; 754 uint64_t pd_base; 755 uint64_t eviction_duration; 756 int retval, ret = 0; 757 758 pdd = qpd_to_pdd(qpd); 759 /* Retrieve PD base */ 760 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv); 761 762 dqm_lock(dqm); 763 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */ 764 goto out; 765 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */ 766 qpd->evicted--; 767 goto out; 768 } 769 770 pr_debug_ratelimited("Restoring PASID 0x%x queues\n", 771 pdd->process->pasid); 772 773 /* Update PD Base in QPD */ 774 qpd->page_table_base = pd_base; 775 pr_debug("Updated PD address to 0x%llx\n", pd_base); 776 777 if (!list_empty(&qpd->queues_list)) { 778 dqm->dev->kfd2kgd->set_vm_context_page_table_base( 779 dqm->dev->kgd, 780 qpd->vmid, 781 qpd->page_table_base); 782 kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY); 783 } 784 785 /* Take a safe reference to the mm_struct, which may otherwise 786 * disappear even while the kfd_process is still referenced. 787 */ 788 mm = get_task_mm(pdd->process->lead_thread); 789 if (!mm) { 790 ret = -EFAULT; 791 goto out; 792 } 793 794 /* Remove the eviction flags. Activate queues that are not 795 * inactive for other reasons. 796 */ 797 list_for_each_entry(q, &qpd->queues_list, list) { 798 q->properties.is_evicted = false; 799 if (!QUEUE_IS_ACTIVE(q->properties)) 800 continue; 801 802 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 803 q->properties.type)]; 804 q->properties.is_active = true; 805 increment_queue_count(dqm, q->properties.type); 806 if (q->properties.is_gws) { 807 dqm->gws_queue_count++; 808 qpd->mapped_gws_queue = true; 809 } 810 811 if (WARN_ONCE(!dqm->sched_running, "Restore when stopped\n")) 812 continue; 813 814 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe, 815 q->queue, &q->properties, mm); 816 if (retval && !ret) 817 /* Return the first error, but keep going to 818 * maintain a consistent eviction state 819 */ 820 ret = retval; 821 } 822 qpd->evicted = 0; 823 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp; 824 atomic64_add(eviction_duration, &pdd->evict_duration_counter); 825 out: 826 if (mm) 827 mmput(mm); 828 dqm_unlock(dqm); 829 return ret; 830 } 831 832 static int restore_process_queues_cpsch(struct device_queue_manager *dqm, 833 struct qcm_process_device *qpd) 834 { 835 struct queue *q; 836 struct kfd_process_device *pdd; 837 uint64_t pd_base; 838 uint64_t eviction_duration; 839 int retval = 0; 840 841 pdd = qpd_to_pdd(qpd); 842 /* Retrieve PD base */ 843 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv); 844 845 dqm_lock(dqm); 846 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */ 847 goto out; 848 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */ 849 qpd->evicted--; 850 goto out; 851 } 852 853 pr_debug_ratelimited("Restoring PASID 0x%x queues\n", 854 pdd->process->pasid); 855 856 /* Update PD Base in QPD */ 857 qpd->page_table_base = pd_base; 858 pr_debug("Updated PD address to 0x%llx\n", pd_base); 859 860 /* activate all active queues on the qpd */ 861 list_for_each_entry(q, &qpd->queues_list, list) { 862 q->properties.is_evicted = false; 863 if (!QUEUE_IS_ACTIVE(q->properties)) 864 continue; 865 866 q->properties.is_active = true; 867 increment_queue_count(dqm, q->properties.type); 868 } 869 retval = execute_queues_cpsch(dqm, 870 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0); 871 qpd->evicted = 0; 872 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp; 873 atomic64_add(eviction_duration, &pdd->evict_duration_counter); 874 out: 875 dqm_unlock(dqm); 876 return retval; 877 } 878 879 static int register_process(struct device_queue_manager *dqm, 880 struct qcm_process_device *qpd) 881 { 882 struct device_process_node *n; 883 struct kfd_process_device *pdd; 884 uint64_t pd_base; 885 int retval; 886 887 n = kzalloc(sizeof(*n), GFP_KERNEL); 888 if (!n) 889 return -ENOMEM; 890 891 n->qpd = qpd; 892 893 pdd = qpd_to_pdd(qpd); 894 /* Retrieve PD base */ 895 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv); 896 897 dqm_lock(dqm); 898 list_add(&n->list, &dqm->queues); 899 900 /* Update PD Base in QPD */ 901 qpd->page_table_base = pd_base; 902 pr_debug("Updated PD address to 0x%llx\n", pd_base); 903 904 retval = dqm->asic_ops.update_qpd(dqm, qpd); 905 906 dqm->processes_count++; 907 908 dqm_unlock(dqm); 909 910 /* Outside the DQM lock because under the DQM lock we can't do 911 * reclaim or take other locks that others hold while reclaiming. 912 */ 913 kfd_inc_compute_active(dqm->dev); 914 915 return retval; 916 } 917 918 static int unregister_process(struct device_queue_manager *dqm, 919 struct qcm_process_device *qpd) 920 { 921 int retval; 922 struct device_process_node *cur, *next; 923 924 pr_debug("qpd->queues_list is %s\n", 925 list_empty(&qpd->queues_list) ? "empty" : "not empty"); 926 927 retval = 0; 928 dqm_lock(dqm); 929 930 list_for_each_entry_safe(cur, next, &dqm->queues, list) { 931 if (qpd == cur->qpd) { 932 list_del(&cur->list); 933 kfree(cur); 934 dqm->processes_count--; 935 goto out; 936 } 937 } 938 /* qpd not found in dqm list */ 939 retval = 1; 940 out: 941 dqm_unlock(dqm); 942 943 /* Outside the DQM lock because under the DQM lock we can't do 944 * reclaim or take other locks that others hold while reclaiming. 945 */ 946 if (!retval) 947 kfd_dec_compute_active(dqm->dev); 948 949 return retval; 950 } 951 952 static int 953 set_pasid_vmid_mapping(struct device_queue_manager *dqm, u32 pasid, 954 unsigned int vmid) 955 { 956 return dqm->dev->kfd2kgd->set_pasid_vmid_mapping( 957 dqm->dev->kgd, pasid, vmid); 958 } 959 960 static void init_interrupts(struct device_queue_manager *dqm) 961 { 962 unsigned int i; 963 964 for (i = 0 ; i < get_pipes_per_mec(dqm) ; i++) 965 if (is_pipe_enabled(dqm, 0, i)) 966 dqm->dev->kfd2kgd->init_interrupts(dqm->dev->kgd, i); 967 } 968 969 static int initialize_nocpsch(struct device_queue_manager *dqm) 970 { 971 int pipe, queue; 972 973 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm)); 974 975 dqm->allocated_queues = kcalloc(get_pipes_per_mec(dqm), 976 sizeof(unsigned int), GFP_KERNEL); 977 if (!dqm->allocated_queues) 978 return -ENOMEM; 979 980 mutex_init(&dqm->lock_hidden); 981 INIT_LIST_HEAD(&dqm->queues); 982 dqm->active_queue_count = dqm->next_pipe_to_allocate = 0; 983 dqm->active_cp_queue_count = 0; 984 dqm->gws_queue_count = 0; 985 986 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) { 987 int pipe_offset = pipe * get_queues_per_pipe(dqm); 988 989 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) 990 if (test_bit(pipe_offset + queue, 991 dqm->dev->shared_resources.cp_queue_bitmap)) 992 dqm->allocated_queues[pipe] |= 1 << queue; 993 } 994 995 memset(dqm->vmid_pasid, 0, sizeof(dqm->vmid_pasid)); 996 997 dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm)); 998 dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm)); 999 1000 return 0; 1001 } 1002 1003 static void uninitialize(struct device_queue_manager *dqm) 1004 { 1005 int i; 1006 1007 WARN_ON(dqm->active_queue_count > 0 || dqm->processes_count > 0); 1008 1009 kfree(dqm->allocated_queues); 1010 for (i = 0 ; i < KFD_MQD_TYPE_MAX ; i++) 1011 kfree(dqm->mqd_mgrs[i]); 1012 mutex_destroy(&dqm->lock_hidden); 1013 } 1014 1015 static int start_nocpsch(struct device_queue_manager *dqm) 1016 { 1017 pr_info("SW scheduler is used"); 1018 init_interrupts(dqm); 1019 1020 if (dqm->dev->device_info->asic_family == CHIP_HAWAII) 1021 return pm_init(&dqm->packet_mgr, dqm); 1022 dqm->sched_running = true; 1023 1024 return 0; 1025 } 1026 1027 static int stop_nocpsch(struct device_queue_manager *dqm) 1028 { 1029 if (dqm->dev->device_info->asic_family == CHIP_HAWAII) 1030 pm_uninit(&dqm->packet_mgr, false); 1031 dqm->sched_running = false; 1032 1033 return 0; 1034 } 1035 1036 static void pre_reset(struct device_queue_manager *dqm) 1037 { 1038 dqm_lock(dqm); 1039 dqm->is_resetting = true; 1040 dqm_unlock(dqm); 1041 } 1042 1043 static int allocate_sdma_queue(struct device_queue_manager *dqm, 1044 struct queue *q) 1045 { 1046 int bit; 1047 1048 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { 1049 if (dqm->sdma_bitmap == 0) { 1050 pr_err("No more SDMA queue to allocate\n"); 1051 return -ENOMEM; 1052 } 1053 1054 bit = __ffs64(dqm->sdma_bitmap); 1055 dqm->sdma_bitmap &= ~(1ULL << bit); 1056 q->sdma_id = bit; 1057 q->properties.sdma_engine_id = q->sdma_id % 1058 get_num_sdma_engines(dqm); 1059 q->properties.sdma_queue_id = q->sdma_id / 1060 get_num_sdma_engines(dqm); 1061 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 1062 if (dqm->xgmi_sdma_bitmap == 0) { 1063 pr_err("No more XGMI SDMA queue to allocate\n"); 1064 return -ENOMEM; 1065 } 1066 bit = __ffs64(dqm->xgmi_sdma_bitmap); 1067 dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); 1068 q->sdma_id = bit; 1069 /* sdma_engine_id is sdma id including 1070 * both PCIe-optimized SDMAs and XGMI- 1071 * optimized SDMAs. The calculation below 1072 * assumes the first N engines are always 1073 * PCIe-optimized ones 1074 */ 1075 q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + 1076 q->sdma_id % get_num_xgmi_sdma_engines(dqm); 1077 q->properties.sdma_queue_id = q->sdma_id / 1078 get_num_xgmi_sdma_engines(dqm); 1079 } 1080 1081 pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id); 1082 pr_debug("SDMA queue id: %d\n", q->properties.sdma_queue_id); 1083 1084 return 0; 1085 } 1086 1087 static void deallocate_sdma_queue(struct device_queue_manager *dqm, 1088 struct queue *q) 1089 { 1090 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { 1091 if (q->sdma_id >= get_num_sdma_queues(dqm)) 1092 return; 1093 dqm->sdma_bitmap |= (1ULL << q->sdma_id); 1094 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 1095 if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) 1096 return; 1097 dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); 1098 } 1099 } 1100 1101 /* 1102 * Device Queue Manager implementation for cp scheduler 1103 */ 1104 1105 static int set_sched_resources(struct device_queue_manager *dqm) 1106 { 1107 int i, mec; 1108 struct scheduling_resources res; 1109 1110 res.vmid_mask = dqm->dev->shared_resources.compute_vmid_bitmap; 1111 1112 res.queue_mask = 0; 1113 for (i = 0; i < KGD_MAX_QUEUES; ++i) { 1114 mec = (i / dqm->dev->shared_resources.num_queue_per_pipe) 1115 / dqm->dev->shared_resources.num_pipe_per_mec; 1116 1117 if (!test_bit(i, dqm->dev->shared_resources.cp_queue_bitmap)) 1118 continue; 1119 1120 /* only acquire queues from the first MEC */ 1121 if (mec > 0) 1122 continue; 1123 1124 /* This situation may be hit in the future if a new HW 1125 * generation exposes more than 64 queues. If so, the 1126 * definition of res.queue_mask needs updating 1127 */ 1128 if (WARN_ON(i >= (sizeof(res.queue_mask)*8))) { 1129 pr_err("Invalid queue enabled by amdgpu: %d\n", i); 1130 break; 1131 } 1132 1133 res.queue_mask |= 1ull 1134 << amdgpu_queue_mask_bit_to_set_resource_bit( 1135 (struct amdgpu_device *)dqm->dev->kgd, i); 1136 } 1137 res.gws_mask = ~0ull; 1138 res.oac_mask = res.gds_heap_base = res.gds_heap_size = 0; 1139 1140 pr_debug("Scheduling resources:\n" 1141 "vmid mask: 0x%8X\n" 1142 "queue mask: 0x%8llX\n", 1143 res.vmid_mask, res.queue_mask); 1144 1145 return pm_send_set_resources(&dqm->packet_mgr, &res); 1146 } 1147 1148 static int initialize_cpsch(struct device_queue_manager *dqm) 1149 { 1150 uint64_t num_sdma_queues; 1151 uint64_t num_xgmi_sdma_queues; 1152 1153 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm)); 1154 1155 mutex_init(&dqm->lock_hidden); 1156 INIT_LIST_HEAD(&dqm->queues); 1157 dqm->active_queue_count = dqm->processes_count = 0; 1158 dqm->active_cp_queue_count = 0; 1159 dqm->gws_queue_count = 0; 1160 dqm->active_runlist = false; 1161 1162 num_sdma_queues = get_num_sdma_queues(dqm); 1163 if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap)) 1164 dqm->sdma_bitmap = ULLONG_MAX; 1165 else 1166 dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1); 1167 1168 num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm); 1169 if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap)) 1170 dqm->xgmi_sdma_bitmap = ULLONG_MAX; 1171 else 1172 dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1); 1173 1174 INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception); 1175 1176 return 0; 1177 } 1178 1179 static int start_cpsch(struct device_queue_manager *dqm) 1180 { 1181 int retval; 1182 1183 retval = 0; 1184 1185 dqm_lock(dqm); 1186 retval = pm_init(&dqm->packet_mgr, dqm); 1187 if (retval) 1188 goto fail_packet_manager_init; 1189 1190 retval = set_sched_resources(dqm); 1191 if (retval) 1192 goto fail_set_sched_resources; 1193 1194 pr_debug("Allocating fence memory\n"); 1195 1196 /* allocate fence memory on the gart */ 1197 retval = kfd_gtt_sa_allocate(dqm->dev, sizeof(*dqm->fence_addr), 1198 &dqm->fence_mem); 1199 1200 if (retval) 1201 goto fail_allocate_vidmem; 1202 1203 dqm->fence_addr = (uint64_t *)dqm->fence_mem->cpu_ptr; 1204 dqm->fence_gpu_addr = dqm->fence_mem->gpu_addr; 1205 1206 init_interrupts(dqm); 1207 1208 /* clear hang status when driver try to start the hw scheduler */ 1209 dqm->is_hws_hang = false; 1210 dqm->is_resetting = false; 1211 dqm->sched_running = true; 1212 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0); 1213 dqm_unlock(dqm); 1214 1215 return 0; 1216 fail_allocate_vidmem: 1217 fail_set_sched_resources: 1218 pm_uninit(&dqm->packet_mgr, false); 1219 fail_packet_manager_init: 1220 dqm_unlock(dqm); 1221 return retval; 1222 } 1223 1224 static int stop_cpsch(struct device_queue_manager *dqm) 1225 { 1226 bool hanging; 1227 1228 dqm_lock(dqm); 1229 if (!dqm->sched_running) { 1230 dqm_unlock(dqm); 1231 return 0; 1232 } 1233 1234 if (!dqm->is_hws_hang) 1235 unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0); 1236 hanging = dqm->is_hws_hang || dqm->is_resetting; 1237 dqm->sched_running = false; 1238 1239 pm_release_ib(&dqm->packet_mgr); 1240 1241 kfd_gtt_sa_free(dqm->dev, dqm->fence_mem); 1242 pm_uninit(&dqm->packet_mgr, hanging); 1243 dqm_unlock(dqm); 1244 1245 return 0; 1246 } 1247 1248 static int create_kernel_queue_cpsch(struct device_queue_manager *dqm, 1249 struct kernel_queue *kq, 1250 struct qcm_process_device *qpd) 1251 { 1252 dqm_lock(dqm); 1253 if (dqm->total_queue_count >= max_num_of_queues_per_device) { 1254 pr_warn("Can't create new kernel queue because %d queues were already created\n", 1255 dqm->total_queue_count); 1256 dqm_unlock(dqm); 1257 return -EPERM; 1258 } 1259 1260 /* 1261 * Unconditionally increment this counter, regardless of the queue's 1262 * type or whether the queue is active. 1263 */ 1264 dqm->total_queue_count++; 1265 pr_debug("Total of %d queues are accountable so far\n", 1266 dqm->total_queue_count); 1267 1268 list_add(&kq->list, &qpd->priv_queue_list); 1269 increment_queue_count(dqm, kq->queue->properties.type); 1270 qpd->is_debug = true; 1271 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0); 1272 dqm_unlock(dqm); 1273 1274 return 0; 1275 } 1276 1277 static void destroy_kernel_queue_cpsch(struct device_queue_manager *dqm, 1278 struct kernel_queue *kq, 1279 struct qcm_process_device *qpd) 1280 { 1281 dqm_lock(dqm); 1282 list_del(&kq->list); 1283 decrement_queue_count(dqm, kq->queue->properties.type); 1284 qpd->is_debug = false; 1285 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0); 1286 /* 1287 * Unconditionally decrement this counter, regardless of the queue's 1288 * type. 1289 */ 1290 dqm->total_queue_count--; 1291 pr_debug("Total of %d queues are accountable so far\n", 1292 dqm->total_queue_count); 1293 dqm_unlock(dqm); 1294 } 1295 1296 static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, 1297 struct qcm_process_device *qpd) 1298 { 1299 int retval; 1300 struct mqd_manager *mqd_mgr; 1301 1302 if (dqm->total_queue_count >= max_num_of_queues_per_device) { 1303 pr_warn("Can't create new usermode queue because %d queues were already created\n", 1304 dqm->total_queue_count); 1305 retval = -EPERM; 1306 goto out; 1307 } 1308 1309 if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 1310 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 1311 dqm_lock(dqm); 1312 retval = allocate_sdma_queue(dqm, q); 1313 dqm_unlock(dqm); 1314 if (retval) 1315 goto out; 1316 } 1317 1318 retval = allocate_doorbell(qpd, q); 1319 if (retval) 1320 goto out_deallocate_sdma_queue; 1321 1322 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 1323 q->properties.type)]; 1324 1325 if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 1326 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 1327 dqm->asic_ops.init_sdma_vm(dqm, q, qpd); 1328 q->properties.tba_addr = qpd->tba_addr; 1329 q->properties.tma_addr = qpd->tma_addr; 1330 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties); 1331 if (!q->mqd_mem_obj) { 1332 retval = -ENOMEM; 1333 goto out_deallocate_doorbell; 1334 } 1335 1336 dqm_lock(dqm); 1337 /* 1338 * Eviction state logic: mark all queues as evicted, even ones 1339 * not currently active. Restoring inactive queues later only 1340 * updates the is_evicted flag but is a no-op otherwise. 1341 */ 1342 q->properties.is_evicted = !!qpd->evicted; 1343 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, 1344 &q->gart_mqd_addr, &q->properties); 1345 1346 list_add(&q->list, &qpd->queues_list); 1347 qpd->queue_count++; 1348 1349 if (q->properties.is_active) { 1350 increment_queue_count(dqm, q->properties.type); 1351 1352 execute_queues_cpsch(dqm, 1353 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0); 1354 } 1355 1356 /* 1357 * Unconditionally increment this counter, regardless of the queue's 1358 * type or whether the queue is active. 1359 */ 1360 dqm->total_queue_count++; 1361 1362 pr_debug("Total of %d queues are accountable so far\n", 1363 dqm->total_queue_count); 1364 1365 dqm_unlock(dqm); 1366 return retval; 1367 1368 out_deallocate_doorbell: 1369 deallocate_doorbell(qpd, q); 1370 out_deallocate_sdma_queue: 1371 if (q->properties.type == KFD_QUEUE_TYPE_SDMA || 1372 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { 1373 dqm_lock(dqm); 1374 deallocate_sdma_queue(dqm, q); 1375 dqm_unlock(dqm); 1376 } 1377 out: 1378 return retval; 1379 } 1380 1381 int amdkfd_fence_wait_timeout(uint64_t *fence_addr, 1382 uint64_t fence_value, 1383 unsigned int timeout_ms) 1384 { 1385 unsigned long end_jiffies = msecs_to_jiffies(timeout_ms) + jiffies; 1386 1387 while (*fence_addr != fence_value) { 1388 if (time_after(jiffies, end_jiffies)) { 1389 pr_err("qcm fence wait loop timeout expired\n"); 1390 /* In HWS case, this is used to halt the driver thread 1391 * in order not to mess up CP states before doing 1392 * scandumps for FW debugging. 1393 */ 1394 while (halt_if_hws_hang) 1395 schedule(); 1396 1397 return -ETIME; 1398 } 1399 schedule(); 1400 } 1401 1402 return 0; 1403 } 1404 1405 /* dqm->lock mutex has to be locked before calling this function */ 1406 static int map_queues_cpsch(struct device_queue_manager *dqm) 1407 { 1408 int retval; 1409 1410 if (!dqm->sched_running) 1411 return 0; 1412 if (dqm->active_queue_count <= 0 || dqm->processes_count <= 0) 1413 return 0; 1414 if (dqm->active_runlist) 1415 return 0; 1416 1417 retval = pm_send_runlist(&dqm->packet_mgr, &dqm->queues); 1418 pr_debug("%s sent runlist\n", __func__); 1419 if (retval) { 1420 pr_err("failed to execute runlist\n"); 1421 return retval; 1422 } 1423 dqm->active_runlist = true; 1424 1425 return retval; 1426 } 1427 1428 /* dqm->lock mutex has to be locked before calling this function */ 1429 static int unmap_queues_cpsch(struct device_queue_manager *dqm, 1430 enum kfd_unmap_queues_filter filter, 1431 uint32_t filter_param) 1432 { 1433 int retval = 0; 1434 struct mqd_manager *mqd_mgr; 1435 1436 if (!dqm->sched_running) 1437 return 0; 1438 if (dqm->is_hws_hang || dqm->is_resetting) 1439 return -EIO; 1440 if (!dqm->active_runlist) 1441 return retval; 1442 1443 retval = pm_send_unmap_queue(&dqm->packet_mgr, KFD_QUEUE_TYPE_COMPUTE, 1444 filter, filter_param, false, 0); 1445 if (retval) 1446 return retval; 1447 1448 *dqm->fence_addr = KFD_FENCE_INIT; 1449 pm_send_query_status(&dqm->packet_mgr, dqm->fence_gpu_addr, 1450 KFD_FENCE_COMPLETED); 1451 /* should be timed out */ 1452 retval = amdkfd_fence_wait_timeout(dqm->fence_addr, KFD_FENCE_COMPLETED, 1453 queue_preemption_timeout_ms); 1454 if (retval) { 1455 pr_err("The cp might be in an unrecoverable state due to an unsuccessful queues preemption\n"); 1456 dqm->is_hws_hang = true; 1457 /* It's possible we're detecting a HWS hang in the 1458 * middle of a GPU reset. No need to schedule another 1459 * reset in this case. 1460 */ 1461 if (!dqm->is_resetting) 1462 schedule_work(&dqm->hw_exception_work); 1463 return retval; 1464 } 1465 1466 /* In the current MEC firmware implementation, if compute queue 1467 * doesn't response to the preemption request in time, HIQ will 1468 * abandon the unmap request without returning any timeout error 1469 * to driver. Instead, MEC firmware will log the doorbell of the 1470 * unresponding compute queue to HIQ.MQD.queue_doorbell_id fields. 1471 * To make sure the queue unmap was successful, driver need to 1472 * check those fields 1473 */ 1474 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]; 1475 if (mqd_mgr->read_doorbell_id(dqm->packet_mgr.priv_queue->queue->mqd)) { 1476 pr_err("HIQ MQD's queue_doorbell_id0 is not 0, Queue preemption time out\n"); 1477 while (halt_if_hws_hang) 1478 schedule(); 1479 return -ETIME; 1480 } 1481 1482 pm_release_ib(&dqm->packet_mgr); 1483 dqm->active_runlist = false; 1484 1485 return retval; 1486 } 1487 1488 /* dqm->lock mutex has to be locked before calling this function */ 1489 static int execute_queues_cpsch(struct device_queue_manager *dqm, 1490 enum kfd_unmap_queues_filter filter, 1491 uint32_t filter_param) 1492 { 1493 int retval; 1494 1495 if (dqm->is_hws_hang) 1496 return -EIO; 1497 retval = unmap_queues_cpsch(dqm, filter, filter_param); 1498 if (retval) 1499 return retval; 1500 1501 return map_queues_cpsch(dqm); 1502 } 1503 1504 static int destroy_queue_cpsch(struct device_queue_manager *dqm, 1505 struct qcm_process_device *qpd, 1506 struct queue *q) 1507 { 1508 int retval; 1509 struct mqd_manager *mqd_mgr; 1510 uint64_t sdma_val = 0; 1511 struct kfd_process_device *pdd = qpd_to_pdd(qpd); 1512 1513 /* Get the SDMA queue stats */ 1514 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || 1515 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 1516 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr, 1517 &sdma_val); 1518 if (retval) 1519 pr_err("Failed to read SDMA queue counter for queue: %d\n", 1520 q->properties.queue_id); 1521 } 1522 1523 retval = 0; 1524 1525 /* remove queue from list to prevent rescheduling after preemption */ 1526 dqm_lock(dqm); 1527 1528 if (qpd->is_debug) { 1529 /* 1530 * error, currently we do not allow to destroy a queue 1531 * of a currently debugged process 1532 */ 1533 retval = -EBUSY; 1534 goto failed_try_destroy_debugged_queue; 1535 1536 } 1537 1538 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 1539 q->properties.type)]; 1540 1541 deallocate_doorbell(qpd, q); 1542 1543 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || 1544 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { 1545 deallocate_sdma_queue(dqm, q); 1546 pdd->sdma_past_activity_counter += sdma_val; 1547 } 1548 1549 list_del(&q->list); 1550 qpd->queue_count--; 1551 if (q->properties.is_active) { 1552 decrement_queue_count(dqm, q->properties.type); 1553 retval = execute_queues_cpsch(dqm, 1554 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0); 1555 if (retval == -ETIME) 1556 qpd->reset_wavefronts = true; 1557 if (q->properties.is_gws) { 1558 dqm->gws_queue_count--; 1559 qpd->mapped_gws_queue = false; 1560 } 1561 } 1562 1563 /* 1564 * Unconditionally decrement this counter, regardless of the queue's 1565 * type 1566 */ 1567 dqm->total_queue_count--; 1568 pr_debug("Total of %d queues are accountable so far\n", 1569 dqm->total_queue_count); 1570 1571 dqm_unlock(dqm); 1572 1573 /* Do free_mqd after dqm_unlock(dqm) to avoid circular locking */ 1574 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 1575 1576 return retval; 1577 1578 failed_try_destroy_debugged_queue: 1579 1580 dqm_unlock(dqm); 1581 return retval; 1582 } 1583 1584 /* 1585 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to 1586 * stay in user mode. 1587 */ 1588 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL 1589 /* APE1 limit is inclusive and 64K aligned. */ 1590 #define APE1_LIMIT_ALIGNMENT 0xFFFF 1591 1592 static bool set_cache_memory_policy(struct device_queue_manager *dqm, 1593 struct qcm_process_device *qpd, 1594 enum cache_policy default_policy, 1595 enum cache_policy alternate_policy, 1596 void __user *alternate_aperture_base, 1597 uint64_t alternate_aperture_size) 1598 { 1599 bool retval = true; 1600 1601 if (!dqm->asic_ops.set_cache_memory_policy) 1602 return retval; 1603 1604 dqm_lock(dqm); 1605 1606 if (alternate_aperture_size == 0) { 1607 /* base > limit disables APE1 */ 1608 qpd->sh_mem_ape1_base = 1; 1609 qpd->sh_mem_ape1_limit = 0; 1610 } else { 1611 /* 1612 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]}, 1613 * SH_MEM_APE1_BASE[31:0], 0x0000 } 1614 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]}, 1615 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF } 1616 * Verify that the base and size parameters can be 1617 * represented in this format and convert them. 1618 * Additionally restrict APE1 to user-mode addresses. 1619 */ 1620 1621 uint64_t base = (uintptr_t)alternate_aperture_base; 1622 uint64_t limit = base + alternate_aperture_size - 1; 1623 1624 if (limit <= base || (base & APE1_FIXED_BITS_MASK) != 0 || 1625 (limit & APE1_FIXED_BITS_MASK) != APE1_LIMIT_ALIGNMENT) { 1626 retval = false; 1627 goto out; 1628 } 1629 1630 qpd->sh_mem_ape1_base = base >> 16; 1631 qpd->sh_mem_ape1_limit = limit >> 16; 1632 } 1633 1634 retval = dqm->asic_ops.set_cache_memory_policy( 1635 dqm, 1636 qpd, 1637 default_policy, 1638 alternate_policy, 1639 alternate_aperture_base, 1640 alternate_aperture_size); 1641 1642 if ((dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) && (qpd->vmid != 0)) 1643 program_sh_mem_settings(dqm, qpd); 1644 1645 pr_debug("sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n", 1646 qpd->sh_mem_config, qpd->sh_mem_ape1_base, 1647 qpd->sh_mem_ape1_limit); 1648 1649 out: 1650 dqm_unlock(dqm); 1651 return retval; 1652 } 1653 1654 static int process_termination_nocpsch(struct device_queue_manager *dqm, 1655 struct qcm_process_device *qpd) 1656 { 1657 struct queue *q; 1658 struct device_process_node *cur, *next_dpn; 1659 int retval = 0; 1660 bool found = false; 1661 1662 dqm_lock(dqm); 1663 1664 /* Clear all user mode queues */ 1665 while (!list_empty(&qpd->queues_list)) { 1666 struct mqd_manager *mqd_mgr; 1667 int ret; 1668 1669 q = list_first_entry(&qpd->queues_list, struct queue, list); 1670 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 1671 q->properties.type)]; 1672 ret = destroy_queue_nocpsch_locked(dqm, qpd, q); 1673 if (ret) 1674 retval = ret; 1675 dqm_unlock(dqm); 1676 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 1677 dqm_lock(dqm); 1678 } 1679 1680 /* Unregister process */ 1681 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) { 1682 if (qpd == cur->qpd) { 1683 list_del(&cur->list); 1684 kfree(cur); 1685 dqm->processes_count--; 1686 found = true; 1687 break; 1688 } 1689 } 1690 1691 dqm_unlock(dqm); 1692 1693 /* Outside the DQM lock because under the DQM lock we can't do 1694 * reclaim or take other locks that others hold while reclaiming. 1695 */ 1696 if (found) 1697 kfd_dec_compute_active(dqm->dev); 1698 1699 return retval; 1700 } 1701 1702 static int get_wave_state(struct device_queue_manager *dqm, 1703 struct queue *q, 1704 void __user *ctl_stack, 1705 u32 *ctl_stack_used_size, 1706 u32 *save_area_used_size) 1707 { 1708 struct mqd_manager *mqd_mgr; 1709 1710 dqm_lock(dqm); 1711 1712 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP]; 1713 1714 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE || 1715 q->properties.is_active || !q->device->cwsr_enabled || 1716 !mqd_mgr->get_wave_state) { 1717 dqm_unlock(dqm); 1718 return -EINVAL; 1719 } 1720 1721 dqm_unlock(dqm); 1722 1723 /* 1724 * get_wave_state is outside the dqm lock to prevent circular locking 1725 * and the queue should be protected against destruction by the process 1726 * lock. 1727 */ 1728 return mqd_mgr->get_wave_state(mqd_mgr, q->mqd, ctl_stack, 1729 ctl_stack_used_size, save_area_used_size); 1730 } 1731 1732 static int process_termination_cpsch(struct device_queue_manager *dqm, 1733 struct qcm_process_device *qpd) 1734 { 1735 int retval; 1736 struct queue *q; 1737 struct kernel_queue *kq, *kq_next; 1738 struct mqd_manager *mqd_mgr; 1739 struct device_process_node *cur, *next_dpn; 1740 enum kfd_unmap_queues_filter filter = 1741 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES; 1742 bool found = false; 1743 1744 retval = 0; 1745 1746 dqm_lock(dqm); 1747 1748 /* Clean all kernel queues */ 1749 list_for_each_entry_safe(kq, kq_next, &qpd->priv_queue_list, list) { 1750 list_del(&kq->list); 1751 decrement_queue_count(dqm, kq->queue->properties.type); 1752 qpd->is_debug = false; 1753 dqm->total_queue_count--; 1754 filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES; 1755 } 1756 1757 /* Clear all user mode queues */ 1758 list_for_each_entry(q, &qpd->queues_list, list) { 1759 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) 1760 deallocate_sdma_queue(dqm, q); 1761 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) 1762 deallocate_sdma_queue(dqm, q); 1763 1764 if (q->properties.is_active) { 1765 decrement_queue_count(dqm, q->properties.type); 1766 if (q->properties.is_gws) { 1767 dqm->gws_queue_count--; 1768 qpd->mapped_gws_queue = false; 1769 } 1770 } 1771 1772 dqm->total_queue_count--; 1773 } 1774 1775 /* Unregister process */ 1776 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) { 1777 if (qpd == cur->qpd) { 1778 list_del(&cur->list); 1779 kfree(cur); 1780 dqm->processes_count--; 1781 found = true; 1782 break; 1783 } 1784 } 1785 1786 retval = execute_queues_cpsch(dqm, filter, 0); 1787 if ((!dqm->is_hws_hang) && (retval || qpd->reset_wavefronts)) { 1788 pr_warn("Resetting wave fronts (cpsch) on dev %p\n", dqm->dev); 1789 dbgdev_wave_reset_wavefronts(dqm->dev, qpd->pqm->process); 1790 qpd->reset_wavefronts = false; 1791 } 1792 1793 /* Lastly, free mqd resources. 1794 * Do free_mqd() after dqm_unlock to avoid circular locking. 1795 */ 1796 while (!list_empty(&qpd->queues_list)) { 1797 q = list_first_entry(&qpd->queues_list, struct queue, list); 1798 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( 1799 q->properties.type)]; 1800 list_del(&q->list); 1801 qpd->queue_count--; 1802 dqm_unlock(dqm); 1803 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); 1804 dqm_lock(dqm); 1805 } 1806 dqm_unlock(dqm); 1807 1808 /* Outside the DQM lock because under the DQM lock we can't do 1809 * reclaim or take other locks that others hold while reclaiming. 1810 */ 1811 if (found) 1812 kfd_dec_compute_active(dqm->dev); 1813 1814 return retval; 1815 } 1816 1817 static int init_mqd_managers(struct device_queue_manager *dqm) 1818 { 1819 int i, j; 1820 struct mqd_manager *mqd_mgr; 1821 1822 for (i = 0; i < KFD_MQD_TYPE_MAX; i++) { 1823 mqd_mgr = dqm->asic_ops.mqd_manager_init(i, dqm->dev); 1824 if (!mqd_mgr) { 1825 pr_err("mqd manager [%d] initialization failed\n", i); 1826 goto out_free; 1827 } 1828 dqm->mqd_mgrs[i] = mqd_mgr; 1829 } 1830 1831 return 0; 1832 1833 out_free: 1834 for (j = 0; j < i; j++) { 1835 kfree(dqm->mqd_mgrs[j]); 1836 dqm->mqd_mgrs[j] = NULL; 1837 } 1838 1839 return -ENOMEM; 1840 } 1841 1842 /* Allocate one hiq mqd (HWS) and all SDMA mqd in a continuous trunk*/ 1843 static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm) 1844 { 1845 int retval; 1846 struct kfd_dev *dev = dqm->dev; 1847 struct kfd_mem_obj *mem_obj = &dqm->hiq_sdma_mqd; 1848 uint32_t size = dqm->mqd_mgrs[KFD_MQD_TYPE_SDMA]->mqd_size * 1849 get_num_all_sdma_engines(dqm) * 1850 dev->device_info->num_sdma_queues_per_engine + 1851 dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size; 1852 1853 retval = amdgpu_amdkfd_alloc_gtt_mem(dev->kgd, size, 1854 &(mem_obj->gtt_mem), &(mem_obj->gpu_addr), 1855 (void *)&(mem_obj->cpu_ptr), false); 1856 1857 return retval; 1858 } 1859 1860 struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev) 1861 { 1862 struct device_queue_manager *dqm; 1863 1864 pr_debug("Loading device queue manager\n"); 1865 1866 dqm = kzalloc(sizeof(*dqm), GFP_KERNEL); 1867 if (!dqm) 1868 return NULL; 1869 1870 switch (dev->device_info->asic_family) { 1871 /* HWS is not available on Hawaii. */ 1872 case CHIP_HAWAII: 1873 /* HWS depends on CWSR for timely dequeue. CWSR is not 1874 * available on Tonga. 1875 * 1876 * FIXME: This argument also applies to Kaveri. 1877 */ 1878 case CHIP_TONGA: 1879 dqm->sched_policy = KFD_SCHED_POLICY_NO_HWS; 1880 break; 1881 default: 1882 dqm->sched_policy = sched_policy; 1883 break; 1884 } 1885 1886 dqm->dev = dev; 1887 switch (dqm->sched_policy) { 1888 case KFD_SCHED_POLICY_HWS: 1889 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: 1890 /* initialize dqm for cp scheduling */ 1891 dqm->ops.create_queue = create_queue_cpsch; 1892 dqm->ops.initialize = initialize_cpsch; 1893 dqm->ops.start = start_cpsch; 1894 dqm->ops.stop = stop_cpsch; 1895 dqm->ops.pre_reset = pre_reset; 1896 dqm->ops.destroy_queue = destroy_queue_cpsch; 1897 dqm->ops.update_queue = update_queue; 1898 dqm->ops.register_process = register_process; 1899 dqm->ops.unregister_process = unregister_process; 1900 dqm->ops.uninitialize = uninitialize; 1901 dqm->ops.create_kernel_queue = create_kernel_queue_cpsch; 1902 dqm->ops.destroy_kernel_queue = destroy_kernel_queue_cpsch; 1903 dqm->ops.set_cache_memory_policy = set_cache_memory_policy; 1904 dqm->ops.process_termination = process_termination_cpsch; 1905 dqm->ops.evict_process_queues = evict_process_queues_cpsch; 1906 dqm->ops.restore_process_queues = restore_process_queues_cpsch; 1907 dqm->ops.get_wave_state = get_wave_state; 1908 break; 1909 case KFD_SCHED_POLICY_NO_HWS: 1910 /* initialize dqm for no cp scheduling */ 1911 dqm->ops.start = start_nocpsch; 1912 dqm->ops.stop = stop_nocpsch; 1913 dqm->ops.pre_reset = pre_reset; 1914 dqm->ops.create_queue = create_queue_nocpsch; 1915 dqm->ops.destroy_queue = destroy_queue_nocpsch; 1916 dqm->ops.update_queue = update_queue; 1917 dqm->ops.register_process = register_process; 1918 dqm->ops.unregister_process = unregister_process; 1919 dqm->ops.initialize = initialize_nocpsch; 1920 dqm->ops.uninitialize = uninitialize; 1921 dqm->ops.set_cache_memory_policy = set_cache_memory_policy; 1922 dqm->ops.process_termination = process_termination_nocpsch; 1923 dqm->ops.evict_process_queues = evict_process_queues_nocpsch; 1924 dqm->ops.restore_process_queues = 1925 restore_process_queues_nocpsch; 1926 dqm->ops.get_wave_state = get_wave_state; 1927 break; 1928 default: 1929 pr_err("Invalid scheduling policy %d\n", dqm->sched_policy); 1930 goto out_free; 1931 } 1932 1933 switch (dev->device_info->asic_family) { 1934 case CHIP_CARRIZO: 1935 device_queue_manager_init_vi(&dqm->asic_ops); 1936 break; 1937 1938 case CHIP_KAVERI: 1939 device_queue_manager_init_cik(&dqm->asic_ops); 1940 break; 1941 1942 case CHIP_HAWAII: 1943 device_queue_manager_init_cik_hawaii(&dqm->asic_ops); 1944 break; 1945 1946 case CHIP_TONGA: 1947 case CHIP_FIJI: 1948 case CHIP_POLARIS10: 1949 case CHIP_POLARIS11: 1950 case CHIP_POLARIS12: 1951 case CHIP_VEGAM: 1952 device_queue_manager_init_vi_tonga(&dqm->asic_ops); 1953 break; 1954 1955 case CHIP_VEGA10: 1956 case CHIP_VEGA12: 1957 case CHIP_VEGA20: 1958 case CHIP_RAVEN: 1959 case CHIP_RENOIR: 1960 case CHIP_ARCTURUS: 1961 case CHIP_ALDEBARAN: 1962 device_queue_manager_init_v9(&dqm->asic_ops); 1963 break; 1964 case CHIP_NAVI10: 1965 case CHIP_NAVI12: 1966 case CHIP_NAVI14: 1967 case CHIP_SIENNA_CICHLID: 1968 case CHIP_NAVY_FLOUNDER: 1969 case CHIP_VANGOGH: 1970 case CHIP_DIMGREY_CAVEFISH: 1971 case CHIP_BEIGE_GOBY: 1972 case CHIP_YELLOW_CARP: 1973 case CHIP_CYAN_SKILLFISH: 1974 device_queue_manager_init_v10_navi10(&dqm->asic_ops); 1975 break; 1976 default: 1977 WARN(1, "Unexpected ASIC family %u", 1978 dev->device_info->asic_family); 1979 goto out_free; 1980 } 1981 1982 if (init_mqd_managers(dqm)) 1983 goto out_free; 1984 1985 if (allocate_hiq_sdma_mqd(dqm)) { 1986 pr_err("Failed to allocate hiq sdma mqd trunk buffer\n"); 1987 goto out_free; 1988 } 1989 1990 if (!dqm->ops.initialize(dqm)) 1991 return dqm; 1992 1993 out_free: 1994 kfree(dqm); 1995 return NULL; 1996 } 1997 1998 static void deallocate_hiq_sdma_mqd(struct kfd_dev *dev, 1999 struct kfd_mem_obj *mqd) 2000 { 2001 WARN(!mqd, "No hiq sdma mqd trunk to free"); 2002 2003 amdgpu_amdkfd_free_gtt_mem(dev->kgd, mqd->gtt_mem); 2004 } 2005 2006 void device_queue_manager_uninit(struct device_queue_manager *dqm) 2007 { 2008 dqm->ops.uninitialize(dqm); 2009 deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd); 2010 kfree(dqm); 2011 } 2012 2013 int kfd_process_vm_fault(struct device_queue_manager *dqm, u32 pasid) 2014 { 2015 struct kfd_process_device *pdd; 2016 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid); 2017 int ret = 0; 2018 2019 if (!p) 2020 return -EINVAL; 2021 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid); 2022 pdd = kfd_get_process_device_data(dqm->dev, p); 2023 if (pdd) 2024 ret = dqm->ops.evict_process_queues(dqm, &pdd->qpd); 2025 kfd_unref_process(p); 2026 2027 return ret; 2028 } 2029 2030 static void kfd_process_hw_exception(struct work_struct *work) 2031 { 2032 struct device_queue_manager *dqm = container_of(work, 2033 struct device_queue_manager, hw_exception_work); 2034 amdgpu_amdkfd_gpu_reset(dqm->dev->kgd); 2035 } 2036 2037 #if defined(CONFIG_DEBUG_FS) 2038 2039 static void seq_reg_dump(struct seq_file *m, 2040 uint32_t (*dump)[2], uint32_t n_regs) 2041 { 2042 uint32_t i, count; 2043 2044 for (i = 0, count = 0; i < n_regs; i++) { 2045 if (count == 0 || 2046 dump[i-1][0] + sizeof(uint32_t) != dump[i][0]) { 2047 seq_printf(m, "%s %08x: %08x", 2048 i ? "\n" : "", 2049 dump[i][0], dump[i][1]); 2050 count = 7; 2051 } else { 2052 seq_printf(m, " %08x", dump[i][1]); 2053 count--; 2054 } 2055 } 2056 2057 seq_puts(m, "\n"); 2058 } 2059 2060 int dqm_debugfs_hqds(struct seq_file *m, void *data) 2061 { 2062 struct device_queue_manager *dqm = data; 2063 uint32_t (*dump)[2], n_regs; 2064 int pipe, queue; 2065 int r = 0; 2066 2067 if (!dqm->sched_running) { 2068 seq_printf(m, " Device is stopped\n"); 2069 2070 return 0; 2071 } 2072 2073 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->kgd, 2074 KFD_CIK_HIQ_PIPE, KFD_CIK_HIQ_QUEUE, 2075 &dump, &n_regs); 2076 if (!r) { 2077 seq_printf(m, " HIQ on MEC %d Pipe %d Queue %d\n", 2078 KFD_CIK_HIQ_PIPE/get_pipes_per_mec(dqm)+1, 2079 KFD_CIK_HIQ_PIPE%get_pipes_per_mec(dqm), 2080 KFD_CIK_HIQ_QUEUE); 2081 seq_reg_dump(m, dump, n_regs); 2082 2083 kfree(dump); 2084 } 2085 2086 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) { 2087 int pipe_offset = pipe * get_queues_per_pipe(dqm); 2088 2089 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) { 2090 if (!test_bit(pipe_offset + queue, 2091 dqm->dev->shared_resources.cp_queue_bitmap)) 2092 continue; 2093 2094 r = dqm->dev->kfd2kgd->hqd_dump( 2095 dqm->dev->kgd, pipe, queue, &dump, &n_regs); 2096 if (r) 2097 break; 2098 2099 seq_printf(m, " CP Pipe %d, Queue %d\n", 2100 pipe, queue); 2101 seq_reg_dump(m, dump, n_regs); 2102 2103 kfree(dump); 2104 } 2105 } 2106 2107 for (pipe = 0; pipe < get_num_all_sdma_engines(dqm); pipe++) { 2108 for (queue = 0; 2109 queue < dqm->dev->device_info->num_sdma_queues_per_engine; 2110 queue++) { 2111 r = dqm->dev->kfd2kgd->hqd_sdma_dump( 2112 dqm->dev->kgd, pipe, queue, &dump, &n_regs); 2113 if (r) 2114 break; 2115 2116 seq_printf(m, " SDMA Engine %d, RLC %d\n", 2117 pipe, queue); 2118 seq_reg_dump(m, dump, n_regs); 2119 2120 kfree(dump); 2121 } 2122 } 2123 2124 return r; 2125 } 2126 2127 int dqm_debugfs_hang_hws(struct device_queue_manager *dqm) 2128 { 2129 int r = 0; 2130 2131 dqm_lock(dqm); 2132 r = pm_debugfs_hang_hws(&dqm->packet_mgr); 2133 if (r) { 2134 dqm_unlock(dqm); 2135 return r; 2136 } 2137 dqm->active_runlist = true; 2138 r = execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0); 2139 dqm_unlock(dqm); 2140 2141 return r; 2142 } 2143 2144 #endif 2145