1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* 3 * Copyright 2016-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/printk.h> 26 #include <linux/slab.h> 27 #include <linux/uaccess.h> 28 #include "kfd_priv.h" 29 #include "kfd_mqd_manager.h" 30 #include "v9_structs.h" 31 #include "gc/gc_9_0_offset.h" 32 #include "gc/gc_9_0_sh_mask.h" 33 #include "sdma0/sdma0_4_0_sh_mask.h" 34 #include "amdgpu_amdkfd.h" 35 #include "kfd_device_queue_manager.h" 36 37 static void update_mqd(struct mqd_manager *mm, void *mqd, 38 struct queue_properties *q, 39 struct mqd_update_info *minfo); 40 41 static uint64_t mqd_stride_v9(struct mqd_manager *mm, 42 struct queue_properties *q) 43 { 44 if (mm->dev->kfd->cwsr_enabled && 45 q->type == KFD_QUEUE_TYPE_COMPUTE) 46 return ALIGN(q->ctl_stack_size, PAGE_SIZE) + 47 ALIGN(sizeof(struct v9_mqd), PAGE_SIZE); 48 49 return mm->mqd_size; 50 } 51 52 static inline struct v9_mqd *get_mqd(void *mqd) 53 { 54 return (struct v9_mqd *)mqd; 55 } 56 57 static inline struct v9_sdma_mqd *get_sdma_mqd(void *mqd) 58 { 59 return (struct v9_sdma_mqd *)mqd; 60 } 61 62 static void update_cu_mask(struct mqd_manager *mm, void *mqd, 63 struct mqd_update_info *minfo, uint32_t inst) 64 { 65 struct v9_mqd *m; 66 uint32_t se_mask[KFD_MAX_NUM_SE] = {0}; 67 68 if (!minfo || !minfo->cu_mask.ptr) 69 return; 70 71 mqd_symmetrically_map_cu_mask(mm, 72 minfo->cu_mask.ptr, minfo->cu_mask.count, se_mask, inst); 73 74 m = get_mqd(mqd); 75 76 m->compute_static_thread_mgmt_se0 = se_mask[0]; 77 m->compute_static_thread_mgmt_se1 = se_mask[1]; 78 m->compute_static_thread_mgmt_se2 = se_mask[2]; 79 m->compute_static_thread_mgmt_se3 = se_mask[3]; 80 if (KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 4, 3) && 81 KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 4, 4)) { 82 m->compute_static_thread_mgmt_se4 = se_mask[4]; 83 m->compute_static_thread_mgmt_se5 = se_mask[5]; 84 m->compute_static_thread_mgmt_se6 = se_mask[6]; 85 m->compute_static_thread_mgmt_se7 = se_mask[7]; 86 87 pr_debug("update cu mask to %#x %#x %#x %#x %#x %#x %#x %#x\n", 88 m->compute_static_thread_mgmt_se0, 89 m->compute_static_thread_mgmt_se1, 90 m->compute_static_thread_mgmt_se2, 91 m->compute_static_thread_mgmt_se3, 92 m->compute_static_thread_mgmt_se4, 93 m->compute_static_thread_mgmt_se5, 94 m->compute_static_thread_mgmt_se6, 95 m->compute_static_thread_mgmt_se7); 96 } else { 97 pr_debug("inst: %u, update cu mask to %#x %#x %#x %#x\n", 98 inst, m->compute_static_thread_mgmt_se0, 99 m->compute_static_thread_mgmt_se1, 100 m->compute_static_thread_mgmt_se2, 101 m->compute_static_thread_mgmt_se3); 102 } 103 } 104 105 static void set_priority(struct v9_mqd *m, struct queue_properties *q) 106 { 107 m->cp_hqd_pipe_priority = pipe_priority_map[q->priority]; 108 m->cp_hqd_queue_priority = q->priority; 109 } 110 111 static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node, 112 struct queue_properties *q) 113 { 114 int retval; 115 struct kfd_mem_obj *mqd_mem_obj = NULL; 116 117 /* For V9 only, due to a HW bug, the control stack of a user mode 118 * compute queue needs to be allocated just behind the page boundary 119 * of its regular MQD buffer. So we allocate an enlarged MQD buffer: 120 * the first page of the buffer serves as the regular MQD buffer 121 * purpose and the remaining is for control stack. Although the two 122 * parts are in the same buffer object, they need different memory 123 * types: MQD part needs UC (uncached) as usual, while control stack 124 * needs NC (non coherent), which is different from the UC type which 125 * is used when control stack is allocated in user space. 126 * 127 * Because of all those, we use the gtt allocation function instead 128 * of sub-allocation function for this enlarged MQD buffer. Moreover, 129 * in order to achieve two memory types in a single buffer object, we 130 * pass a special bo flag AMDGPU_GEM_CREATE_CP_MQD_GFX9 to instruct 131 * amdgpu memory functions to do so. 132 */ 133 if (node->kfd->cwsr_enabled && (q->type == KFD_QUEUE_TYPE_COMPUTE)) { 134 mqd_mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); 135 if (!mqd_mem_obj) 136 return NULL; 137 retval = amdgpu_amdkfd_alloc_gtt_mem(node->adev, 138 (ALIGN(q->ctl_stack_size, PAGE_SIZE) + 139 ALIGN(sizeof(struct v9_mqd), PAGE_SIZE)) * 140 NUM_XCC(node->xcc_mask), 141 &(mqd_mem_obj->gtt_mem), 142 &(mqd_mem_obj->gpu_addr), 143 (void *)&(mqd_mem_obj->cpu_ptr), true); 144 145 if (retval) { 146 kfree(mqd_mem_obj); 147 return NULL; 148 } 149 } else { 150 retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd), 151 &mqd_mem_obj); 152 if (retval) 153 return NULL; 154 } 155 156 return mqd_mem_obj; 157 } 158 159 static void init_mqd(struct mqd_manager *mm, void **mqd, 160 struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr, 161 struct queue_properties *q) 162 { 163 uint64_t addr; 164 struct v9_mqd *m; 165 166 m = (struct v9_mqd *) mqd_mem_obj->cpu_ptr; 167 addr = mqd_mem_obj->gpu_addr; 168 169 memset(m, 0, sizeof(struct v9_mqd)); 170 171 m->header = 0xC0310800; 172 m->compute_pipelinestat_enable = 1; 173 m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF; 174 m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF; 175 m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF; 176 m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF; 177 m->compute_static_thread_mgmt_se4 = 0xFFFFFFFF; 178 m->compute_static_thread_mgmt_se5 = 0xFFFFFFFF; 179 m->compute_static_thread_mgmt_se6 = 0xFFFFFFFF; 180 m->compute_static_thread_mgmt_se7 = 0xFFFFFFFF; 181 182 m->cp_hqd_persistent_state = CP_HQD_PERSISTENT_STATE__PRELOAD_REQ_MASK | 183 0x53 << CP_HQD_PERSISTENT_STATE__PRELOAD_SIZE__SHIFT; 184 185 m->cp_mqd_control = 1 << CP_MQD_CONTROL__PRIV_STATE__SHIFT; 186 187 m->cp_mqd_base_addr_lo = lower_32_bits(addr); 188 m->cp_mqd_base_addr_hi = upper_32_bits(addr); 189 190 m->cp_hqd_quantum = 1 << CP_HQD_QUANTUM__QUANTUM_EN__SHIFT | 191 1 << CP_HQD_QUANTUM__QUANTUM_SCALE__SHIFT | 192 1 << CP_HQD_QUANTUM__QUANTUM_DURATION__SHIFT; 193 194 /* Set cp_hqd_hq_scheduler0 bit 14 to 1 to have the CP set up the 195 * DISPATCH_PTR. This is required for the kfd debugger 196 */ 197 m->cp_hqd_hq_status0 = 1 << 14; 198 199 if (q->format == KFD_QUEUE_FORMAT_AQL) 200 m->cp_hqd_aql_control = 201 1 << CP_HQD_AQL_CONTROL__CONTROL0__SHIFT; 202 203 if (q->tba_addr) { 204 m->compute_pgm_rsrc2 |= 205 (1 << COMPUTE_PGM_RSRC2__TRAP_PRESENT__SHIFT); 206 } 207 208 if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) { 209 m->cp_hqd_persistent_state |= 210 (1 << CP_HQD_PERSISTENT_STATE__QSWITCH_MODE__SHIFT); 211 m->cp_hqd_ctx_save_base_addr_lo = 212 lower_32_bits(q->ctx_save_restore_area_address); 213 m->cp_hqd_ctx_save_base_addr_hi = 214 upper_32_bits(q->ctx_save_restore_area_address); 215 m->cp_hqd_ctx_save_size = q->ctx_save_restore_area_size; 216 m->cp_hqd_cntl_stack_size = q->ctl_stack_size; 217 m->cp_hqd_cntl_stack_offset = q->ctl_stack_size; 218 m->cp_hqd_wg_state_offset = q->ctl_stack_size; 219 } 220 221 *mqd = m; 222 if (gart_addr) 223 *gart_addr = addr; 224 update_mqd(mm, m, q, NULL); 225 } 226 227 static int load_mqd(struct mqd_manager *mm, void *mqd, 228 uint32_t pipe_id, uint32_t queue_id, 229 struct queue_properties *p, struct mm_struct *mms) 230 { 231 /* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */ 232 uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0); 233 234 return mm->dev->kfd2kgd->hqd_load(mm->dev->adev, mqd, pipe_id, queue_id, 235 (uint32_t __user *)p->write_ptr, 236 wptr_shift, 0, mms, 0); 237 } 238 239 static void update_mqd(struct mqd_manager *mm, void *mqd, 240 struct queue_properties *q, 241 struct mqd_update_info *minfo) 242 { 243 struct v9_mqd *m; 244 245 m = get_mqd(mqd); 246 247 m->cp_hqd_pq_control = 5 << CP_HQD_PQ_CONTROL__RPTR_BLOCK_SIZE__SHIFT; 248 m->cp_hqd_pq_control |= order_base_2(q->queue_size / 4) - 1; 249 pr_debug("cp_hqd_pq_control 0x%x\n", m->cp_hqd_pq_control); 250 251 m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8); 252 m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8); 253 254 m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr); 255 m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr); 256 m->cp_hqd_pq_wptr_poll_addr_lo = lower_32_bits((uint64_t)q->write_ptr); 257 m->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits((uint64_t)q->write_ptr); 258 259 m->cp_hqd_pq_doorbell_control = 260 q->doorbell_off << 261 CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT; 262 pr_debug("cp_hqd_pq_doorbell_control 0x%x\n", 263 m->cp_hqd_pq_doorbell_control); 264 265 m->cp_hqd_ib_control = 266 3 << CP_HQD_IB_CONTROL__MIN_IB_AVAIL_SIZE__SHIFT | 267 1 << CP_HQD_IB_CONTROL__IB_EXE_DISABLE__SHIFT; 268 269 /* 270 * HW does not clamp this field correctly. Maximum EOP queue size 271 * is constrained by per-SE EOP done signal count, which is 8-bit. 272 * Limit is 0xFF EOP entries (= 0x7F8 dwords). CP will not submit 273 * more than (EOP entry count - 1) so a queue size of 0x800 dwords 274 * is safe, giving a maximum field value of 0xA. 275 * 276 * Also, do calculation only if EOP is used (size > 0), otherwise 277 * the order_base_2 calculation provides incorrect result. 278 * 279 */ 280 m->cp_hqd_eop_control = q->eop_ring_buffer_size ? 281 min(0xA, order_base_2(q->eop_ring_buffer_size / 4) - 1) : 0; 282 283 m->cp_hqd_eop_base_addr_lo = 284 lower_32_bits(q->eop_ring_buffer_address >> 8); 285 m->cp_hqd_eop_base_addr_hi = 286 upper_32_bits(q->eop_ring_buffer_address >> 8); 287 288 m->cp_hqd_iq_timer = 0; 289 290 m->cp_hqd_vmid = q->vmid; 291 292 if (q->format == KFD_QUEUE_FORMAT_AQL) { 293 m->cp_hqd_pq_control |= CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK | 294 2 << CP_HQD_PQ_CONTROL__SLOT_BASED_WPTR__SHIFT | 295 1 << CP_HQD_PQ_CONTROL__QUEUE_FULL_EN__SHIFT | 296 1 << CP_HQD_PQ_CONTROL__WPP_CLAMP_EN__SHIFT; 297 m->cp_hqd_pq_doorbell_control |= 1 << 298 CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_BIF_DROP__SHIFT; 299 } 300 if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) 301 m->cp_hqd_ctx_save_control = 0; 302 303 if (KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 4, 3) && 304 KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 4, 4)) 305 update_cu_mask(mm, mqd, minfo, 0); 306 set_priority(m, q); 307 308 if (minfo && KFD_GC_VERSION(mm->dev) >= IP_VERSION(9, 4, 2)) { 309 if (minfo->update_flag & UPDATE_FLAG_IS_GWS) 310 m->compute_resource_limits |= 311 COMPUTE_RESOURCE_LIMITS__FORCE_SIMD_DIST_MASK; 312 else 313 m->compute_resource_limits &= 314 ~COMPUTE_RESOURCE_LIMITS__FORCE_SIMD_DIST_MASK; 315 } 316 317 q->is_active = QUEUE_IS_ACTIVE(*q); 318 } 319 320 321 static bool check_preemption_failed(struct mqd_manager *mm, void *mqd) 322 { 323 struct v9_mqd *m = (struct v9_mqd *)mqd; 324 325 return kfd_check_hiq_mqd_doorbell_id(mm->dev, m->queue_doorbell_id0, 0); 326 } 327 328 static int get_wave_state(struct mqd_manager *mm, void *mqd, 329 struct queue_properties *q, 330 void __user *ctl_stack, 331 u32 *ctl_stack_used_size, 332 u32 *save_area_used_size) 333 { 334 struct v9_mqd *m; 335 struct kfd_context_save_area_header header; 336 337 /* Control stack is located one page after MQD. */ 338 void *mqd_ctl_stack = (void *)((uintptr_t)mqd + PAGE_SIZE); 339 340 m = get_mqd(mqd); 341 342 *ctl_stack_used_size = m->cp_hqd_cntl_stack_size - 343 m->cp_hqd_cntl_stack_offset; 344 *save_area_used_size = m->cp_hqd_wg_state_offset - 345 m->cp_hqd_cntl_stack_size; 346 347 header.wave_state.control_stack_size = *ctl_stack_used_size; 348 header.wave_state.wave_state_size = *save_area_used_size; 349 350 header.wave_state.wave_state_offset = m->cp_hqd_wg_state_offset; 351 header.wave_state.control_stack_offset = m->cp_hqd_cntl_stack_offset; 352 353 if (copy_to_user(ctl_stack, &header, sizeof(header.wave_state))) 354 return -EFAULT; 355 356 if (copy_to_user(ctl_stack + m->cp_hqd_cntl_stack_offset, 357 mqd_ctl_stack + m->cp_hqd_cntl_stack_offset, 358 *ctl_stack_used_size)) 359 return -EFAULT; 360 361 return 0; 362 } 363 364 static void get_checkpoint_info(struct mqd_manager *mm, void *mqd, u32 *ctl_stack_size) 365 { 366 struct v9_mqd *m = get_mqd(mqd); 367 368 *ctl_stack_size = m->cp_hqd_cntl_stack_size; 369 } 370 371 static void checkpoint_mqd(struct mqd_manager *mm, void *mqd, void *mqd_dst, void *ctl_stack_dst) 372 { 373 struct v9_mqd *m; 374 /* Control stack is located one page after MQD. */ 375 void *ctl_stack = (void *)((uintptr_t)mqd + PAGE_SIZE); 376 377 m = get_mqd(mqd); 378 379 memcpy(mqd_dst, m, sizeof(struct v9_mqd)); 380 memcpy(ctl_stack_dst, ctl_stack, m->cp_hqd_cntl_stack_size); 381 } 382 383 static void restore_mqd(struct mqd_manager *mm, void **mqd, 384 struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr, 385 struct queue_properties *qp, 386 const void *mqd_src, 387 const void *ctl_stack_src, u32 ctl_stack_size) 388 { 389 uint64_t addr; 390 struct v9_mqd *m; 391 void *ctl_stack; 392 393 m = (struct v9_mqd *) mqd_mem_obj->cpu_ptr; 394 addr = mqd_mem_obj->gpu_addr; 395 396 memcpy(m, mqd_src, sizeof(*m)); 397 398 *mqd = m; 399 if (gart_addr) 400 *gart_addr = addr; 401 402 /* Control stack is located one page after MQD. */ 403 ctl_stack = (void *)((uintptr_t)*mqd + PAGE_SIZE); 404 memcpy(ctl_stack, ctl_stack_src, ctl_stack_size); 405 406 m->cp_hqd_pq_doorbell_control = 407 qp->doorbell_off << 408 CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT; 409 pr_debug("cp_hqd_pq_doorbell_control 0x%x\n", 410 m->cp_hqd_pq_doorbell_control); 411 412 qp->is_active = 0; 413 } 414 415 static void init_mqd_hiq(struct mqd_manager *mm, void **mqd, 416 struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr, 417 struct queue_properties *q) 418 { 419 struct v9_mqd *m; 420 421 init_mqd(mm, mqd, mqd_mem_obj, gart_addr, q); 422 423 m = get_mqd(*mqd); 424 425 m->cp_hqd_pq_control |= 1 << CP_HQD_PQ_CONTROL__PRIV_STATE__SHIFT | 426 1 << CP_HQD_PQ_CONTROL__KMD_QUEUE__SHIFT; 427 } 428 429 static int destroy_hiq_mqd(struct mqd_manager *mm, void *mqd, 430 enum kfd_preempt_type type, unsigned int timeout, 431 uint32_t pipe_id, uint32_t queue_id) 432 { 433 int err; 434 struct v9_mqd *m; 435 u32 doorbell_off; 436 437 m = get_mqd(mqd); 438 439 doorbell_off = m->cp_hqd_pq_doorbell_control >> 440 CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT; 441 err = amdgpu_amdkfd_unmap_hiq(mm->dev->adev, doorbell_off, 0); 442 if (err) 443 pr_debug("Destroy HIQ MQD failed: %d\n", err); 444 445 return err; 446 } 447 448 static void init_mqd_sdma(struct mqd_manager *mm, void **mqd, 449 struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr, 450 struct queue_properties *q) 451 { 452 struct v9_sdma_mqd *m; 453 454 m = (struct v9_sdma_mqd *) mqd_mem_obj->cpu_ptr; 455 456 memset(m, 0, sizeof(struct v9_sdma_mqd)); 457 458 *mqd = m; 459 if (gart_addr) 460 *gart_addr = mqd_mem_obj->gpu_addr; 461 462 mm->update_mqd(mm, m, q, NULL); 463 } 464 465 #define SDMA_RLC_DUMMY_DEFAULT 0xf 466 467 static void update_mqd_sdma(struct mqd_manager *mm, void *mqd, 468 struct queue_properties *q, 469 struct mqd_update_info *minfo) 470 { 471 struct v9_sdma_mqd *m; 472 473 m = get_sdma_mqd(mqd); 474 m->sdmax_rlcx_rb_cntl = order_base_2(q->queue_size / 4) 475 << SDMA0_RLC0_RB_CNTL__RB_SIZE__SHIFT | 476 q->vmid << SDMA0_RLC0_RB_CNTL__RB_VMID__SHIFT | 477 1 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT | 478 6 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT; 479 480 m->sdmax_rlcx_rb_base = lower_32_bits(q->queue_address >> 8); 481 m->sdmax_rlcx_rb_base_hi = upper_32_bits(q->queue_address >> 8); 482 m->sdmax_rlcx_rb_rptr_addr_lo = lower_32_bits((uint64_t)q->read_ptr); 483 m->sdmax_rlcx_rb_rptr_addr_hi = upper_32_bits((uint64_t)q->read_ptr); 484 m->sdmax_rlcx_doorbell_offset = 485 q->doorbell_off << SDMA0_RLC0_DOORBELL_OFFSET__OFFSET__SHIFT; 486 487 m->sdma_engine_id = q->sdma_engine_id; 488 m->sdma_queue_id = q->sdma_queue_id; 489 m->sdmax_rlcx_dummy_reg = SDMA_RLC_DUMMY_DEFAULT; 490 491 q->is_active = QUEUE_IS_ACTIVE(*q); 492 } 493 494 static void checkpoint_mqd_sdma(struct mqd_manager *mm, 495 void *mqd, 496 void *mqd_dst, 497 void *ctl_stack_dst) 498 { 499 struct v9_sdma_mqd *m; 500 501 m = get_sdma_mqd(mqd); 502 503 memcpy(mqd_dst, m, sizeof(struct v9_sdma_mqd)); 504 } 505 506 static void restore_mqd_sdma(struct mqd_manager *mm, void **mqd, 507 struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr, 508 struct queue_properties *qp, 509 const void *mqd_src, 510 const void *ctl_stack_src, const u32 ctl_stack_size) 511 { 512 uint64_t addr; 513 struct v9_sdma_mqd *m; 514 515 m = (struct v9_sdma_mqd *) mqd_mem_obj->cpu_ptr; 516 addr = mqd_mem_obj->gpu_addr; 517 518 memcpy(m, mqd_src, sizeof(*m)); 519 520 m->sdmax_rlcx_doorbell_offset = 521 qp->doorbell_off << SDMA0_RLC0_DOORBELL_OFFSET__OFFSET__SHIFT; 522 523 *mqd = m; 524 if (gart_addr) 525 *gart_addr = addr; 526 527 qp->is_active = 0; 528 } 529 530 static void init_mqd_hiq_v9_4_3(struct mqd_manager *mm, void **mqd, 531 struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr, 532 struct queue_properties *q) 533 { 534 struct v9_mqd *m; 535 int xcc = 0; 536 struct kfd_mem_obj xcc_mqd_mem_obj; 537 uint64_t xcc_gart_addr = 0; 538 539 memset(&xcc_mqd_mem_obj, 0x0, sizeof(struct kfd_mem_obj)); 540 541 for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) { 542 kfd_get_hiq_xcc_mqd(mm->dev, &xcc_mqd_mem_obj, xcc); 543 544 init_mqd(mm, (void **)&m, &xcc_mqd_mem_obj, &xcc_gart_addr, q); 545 546 m->cp_hqd_pq_control |= CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK | 547 1 << CP_HQD_PQ_CONTROL__PRIV_STATE__SHIFT | 548 1 << CP_HQD_PQ_CONTROL__KMD_QUEUE__SHIFT; 549 m->cp_mqd_stride_size = kfd_hiq_mqd_stride(mm->dev); 550 if (xcc == 0) { 551 /* Set no_update_rptr = 0 in Master XCC */ 552 m->cp_hqd_pq_control &= ~CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK; 553 554 /* Set the MQD pointer and gart address to XCC0 MQD */ 555 *mqd = m; 556 *gart_addr = xcc_gart_addr; 557 } 558 } 559 } 560 561 static int hiq_load_mqd_kiq_v9_4_3(struct mqd_manager *mm, void *mqd, 562 uint32_t pipe_id, uint32_t queue_id, 563 struct queue_properties *p, struct mm_struct *mms) 564 { 565 uint32_t xcc_mask = mm->dev->xcc_mask; 566 int xcc_id, err, inst = 0; 567 void *xcc_mqd; 568 uint64_t hiq_mqd_size = kfd_hiq_mqd_stride(mm->dev); 569 570 for_each_inst(xcc_id, xcc_mask) { 571 xcc_mqd = mqd + hiq_mqd_size * inst; 572 err = mm->dev->kfd2kgd->hiq_mqd_load(mm->dev->adev, xcc_mqd, 573 pipe_id, queue_id, 574 p->doorbell_off, xcc_id); 575 if (err) { 576 pr_debug("Failed to load HIQ MQD for XCC: %d\n", inst); 577 break; 578 } 579 ++inst; 580 } 581 582 return err; 583 } 584 585 static int destroy_hiq_mqd_v9_4_3(struct mqd_manager *mm, void *mqd, 586 enum kfd_preempt_type type, unsigned int timeout, 587 uint32_t pipe_id, uint32_t queue_id) 588 { 589 uint32_t xcc_mask = mm->dev->xcc_mask; 590 int xcc_id, err, inst = 0; 591 uint64_t hiq_mqd_size = kfd_hiq_mqd_stride(mm->dev); 592 struct v9_mqd *m; 593 u32 doorbell_off; 594 595 for_each_inst(xcc_id, xcc_mask) { 596 m = get_mqd(mqd + hiq_mqd_size * inst); 597 598 doorbell_off = m->cp_hqd_pq_doorbell_control >> 599 CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT; 600 601 err = amdgpu_amdkfd_unmap_hiq(mm->dev->adev, doorbell_off, xcc_id); 602 if (err) { 603 pr_debug("Destroy HIQ MQD failed for xcc: %d\n", inst); 604 break; 605 } 606 ++inst; 607 } 608 609 return err; 610 } 611 612 static bool check_preemption_failed_v9_4_3(struct mqd_manager *mm, void *mqd) 613 { 614 uint64_t hiq_mqd_size = kfd_hiq_mqd_stride(mm->dev); 615 uint32_t xcc_mask = mm->dev->xcc_mask; 616 int inst = 0, xcc_id; 617 struct v9_mqd *m; 618 bool ret = false; 619 620 for_each_inst(xcc_id, xcc_mask) { 621 m = get_mqd(mqd + hiq_mqd_size * inst); 622 ret |= kfd_check_hiq_mqd_doorbell_id(mm->dev, 623 m->queue_doorbell_id0, inst); 624 ++inst; 625 } 626 627 return ret; 628 } 629 630 static void get_xcc_mqd(struct kfd_mem_obj *mqd_mem_obj, 631 struct kfd_mem_obj *xcc_mqd_mem_obj, 632 uint64_t offset) 633 { 634 xcc_mqd_mem_obj->gtt_mem = (offset == 0) ? 635 mqd_mem_obj->gtt_mem : NULL; 636 xcc_mqd_mem_obj->gpu_addr = mqd_mem_obj->gpu_addr + offset; 637 xcc_mqd_mem_obj->cpu_ptr = (uint32_t *)((uintptr_t)mqd_mem_obj->cpu_ptr 638 + offset); 639 } 640 641 static void init_mqd_v9_4_3(struct mqd_manager *mm, void **mqd, 642 struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr, 643 struct queue_properties *q) 644 { 645 struct v9_mqd *m; 646 int xcc = 0; 647 struct kfd_mem_obj xcc_mqd_mem_obj; 648 uint64_t xcc_gart_addr = 0; 649 uint64_t xcc_ctx_save_restore_area_address; 650 uint64_t offset = mm->mqd_stride(mm, q); 651 uint32_t local_xcc_start = mm->dev->dqm->current_logical_xcc_start++; 652 653 memset(&xcc_mqd_mem_obj, 0x0, sizeof(struct kfd_mem_obj)); 654 for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) { 655 get_xcc_mqd(mqd_mem_obj, &xcc_mqd_mem_obj, offset*xcc); 656 657 init_mqd(mm, (void **)&m, &xcc_mqd_mem_obj, &xcc_gart_addr, q); 658 659 m->cp_mqd_stride_size = offset; 660 661 /* 662 * Update the CWSR address for each XCC if CWSR is enabled 663 * and CWSR area is allocated in thunk 664 */ 665 if (mm->dev->kfd->cwsr_enabled && 666 q->ctx_save_restore_area_address) { 667 xcc_ctx_save_restore_area_address = 668 q->ctx_save_restore_area_address + 669 (xcc * q->ctx_save_restore_area_size); 670 671 m->cp_hqd_ctx_save_base_addr_lo = 672 lower_32_bits(xcc_ctx_save_restore_area_address); 673 m->cp_hqd_ctx_save_base_addr_hi = 674 upper_32_bits(xcc_ctx_save_restore_area_address); 675 } 676 677 if (q->format == KFD_QUEUE_FORMAT_AQL) { 678 m->compute_tg_chunk_size = 1; 679 m->compute_current_logic_xcc_id = 680 (local_xcc_start + xcc) % 681 NUM_XCC(mm->dev->xcc_mask); 682 683 switch (xcc) { 684 case 0: 685 /* Master XCC */ 686 m->cp_hqd_pq_control &= 687 ~CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK; 688 break; 689 default: 690 break; 691 } 692 } else { 693 /* PM4 Queue */ 694 m->compute_current_logic_xcc_id = 0; 695 m->compute_tg_chunk_size = 0; 696 m->pm4_target_xcc_in_xcp = q->pm4_target_xcc; 697 } 698 699 if (xcc == 0) { 700 /* Set the MQD pointer and gart address to XCC0 MQD */ 701 *mqd = m; 702 *gart_addr = xcc_gart_addr; 703 } 704 } 705 } 706 707 static void update_mqd_v9_4_3(struct mqd_manager *mm, void *mqd, 708 struct queue_properties *q, struct mqd_update_info *minfo) 709 { 710 struct v9_mqd *m; 711 int xcc = 0; 712 uint64_t size = mm->mqd_stride(mm, q); 713 714 for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) { 715 m = get_mqd(mqd + size * xcc); 716 update_mqd(mm, m, q, minfo); 717 718 update_cu_mask(mm, m, minfo, xcc); 719 720 if (q->format == KFD_QUEUE_FORMAT_AQL) { 721 switch (xcc) { 722 case 0: 723 /* Master XCC */ 724 m->cp_hqd_pq_control &= 725 ~CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK; 726 break; 727 default: 728 break; 729 } 730 m->compute_tg_chunk_size = 1; 731 } else { 732 /* PM4 Queue */ 733 m->compute_current_logic_xcc_id = 0; 734 m->compute_tg_chunk_size = 0; 735 m->pm4_target_xcc_in_xcp = q->pm4_target_xcc; 736 } 737 } 738 } 739 740 static int destroy_mqd_v9_4_3(struct mqd_manager *mm, void *mqd, 741 enum kfd_preempt_type type, unsigned int timeout, 742 uint32_t pipe_id, uint32_t queue_id) 743 { 744 uint32_t xcc_mask = mm->dev->xcc_mask; 745 int xcc_id, err, inst = 0; 746 void *xcc_mqd; 747 struct v9_mqd *m; 748 uint64_t mqd_offset; 749 750 m = get_mqd(mqd); 751 mqd_offset = m->cp_mqd_stride_size; 752 753 for_each_inst(xcc_id, xcc_mask) { 754 xcc_mqd = mqd + mqd_offset * inst; 755 err = mm->dev->kfd2kgd->hqd_destroy(mm->dev->adev, xcc_mqd, 756 type, timeout, pipe_id, 757 queue_id, xcc_id); 758 if (err) { 759 pr_debug("Destroy MQD failed for xcc: %d\n", inst); 760 break; 761 } 762 ++inst; 763 } 764 765 return err; 766 } 767 768 static int load_mqd_v9_4_3(struct mqd_manager *mm, void *mqd, 769 uint32_t pipe_id, uint32_t queue_id, 770 struct queue_properties *p, struct mm_struct *mms) 771 { 772 /* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */ 773 uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0); 774 uint32_t xcc_mask = mm->dev->xcc_mask; 775 int xcc_id, err, inst = 0; 776 void *xcc_mqd; 777 uint64_t mqd_stride_size = mm->mqd_stride(mm, p); 778 779 for_each_inst(xcc_id, xcc_mask) { 780 xcc_mqd = mqd + mqd_stride_size * inst; 781 err = mm->dev->kfd2kgd->hqd_load( 782 mm->dev->adev, xcc_mqd, pipe_id, queue_id, 783 (uint32_t __user *)p->write_ptr, wptr_shift, 0, mms, 784 xcc_id); 785 if (err) { 786 pr_debug("Load MQD failed for xcc: %d\n", inst); 787 break; 788 } 789 ++inst; 790 } 791 792 return err; 793 } 794 795 static int get_wave_state_v9_4_3(struct mqd_manager *mm, void *mqd, 796 struct queue_properties *q, 797 void __user *ctl_stack, 798 u32 *ctl_stack_used_size, 799 u32 *save_area_used_size) 800 { 801 int xcc, err = 0; 802 void *xcc_mqd; 803 void __user *xcc_ctl_stack; 804 uint64_t mqd_stride_size = mm->mqd_stride(mm, q); 805 u32 tmp_ctl_stack_used_size = 0, tmp_save_area_used_size = 0; 806 807 for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) { 808 xcc_mqd = mqd + mqd_stride_size * xcc; 809 xcc_ctl_stack = (void __user *)((uintptr_t)ctl_stack + 810 q->ctx_save_restore_area_size * xcc); 811 812 err = get_wave_state(mm, xcc_mqd, q, xcc_ctl_stack, 813 &tmp_ctl_stack_used_size, 814 &tmp_save_area_used_size); 815 if (err) 816 break; 817 818 /* 819 * Set the ctl_stack_used_size and save_area_used_size to 820 * ctl_stack_used_size and save_area_used_size of XCC 0 when 821 * passing the info the user-space. 822 * For multi XCC, user-space would have to look at the header 823 * info of each Control stack area to determine the control 824 * stack size and save area used. 825 */ 826 if (xcc == 0) { 827 *ctl_stack_used_size = tmp_ctl_stack_used_size; 828 *save_area_used_size = tmp_save_area_used_size; 829 } 830 } 831 832 return err; 833 } 834 835 #if defined(CONFIG_DEBUG_FS) 836 837 static int debugfs_show_mqd(struct seq_file *m, void *data) 838 { 839 seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 32, 4, 840 data, sizeof(struct v9_mqd), false); 841 return 0; 842 } 843 844 static int debugfs_show_mqd_sdma(struct seq_file *m, void *data) 845 { 846 seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 32, 4, 847 data, sizeof(struct v9_sdma_mqd), false); 848 return 0; 849 } 850 851 #endif 852 853 struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type, 854 struct kfd_node *dev) 855 { 856 struct mqd_manager *mqd; 857 858 if (WARN_ON(type >= KFD_MQD_TYPE_MAX)) 859 return NULL; 860 861 mqd = kzalloc(sizeof(*mqd), GFP_KERNEL); 862 if (!mqd) 863 return NULL; 864 865 mqd->dev = dev; 866 867 switch (type) { 868 case KFD_MQD_TYPE_CP: 869 mqd->allocate_mqd = allocate_mqd; 870 mqd->free_mqd = kfd_free_mqd_cp; 871 mqd->is_occupied = kfd_is_occupied_cp; 872 mqd->get_checkpoint_info = get_checkpoint_info; 873 mqd->checkpoint_mqd = checkpoint_mqd; 874 mqd->restore_mqd = restore_mqd; 875 mqd->mqd_size = sizeof(struct v9_mqd); 876 mqd->mqd_stride = mqd_stride_v9; 877 #if defined(CONFIG_DEBUG_FS) 878 mqd->debugfs_show_mqd = debugfs_show_mqd; 879 #endif 880 if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3) || 881 KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4)) { 882 mqd->init_mqd = init_mqd_v9_4_3; 883 mqd->load_mqd = load_mqd_v9_4_3; 884 mqd->update_mqd = update_mqd_v9_4_3; 885 mqd->destroy_mqd = destroy_mqd_v9_4_3; 886 mqd->get_wave_state = get_wave_state_v9_4_3; 887 } else { 888 mqd->init_mqd = init_mqd; 889 mqd->load_mqd = load_mqd; 890 mqd->update_mqd = update_mqd; 891 mqd->destroy_mqd = kfd_destroy_mqd_cp; 892 mqd->get_wave_state = get_wave_state; 893 } 894 break; 895 case KFD_MQD_TYPE_HIQ: 896 mqd->allocate_mqd = allocate_hiq_mqd; 897 mqd->free_mqd = free_mqd_hiq_sdma; 898 mqd->update_mqd = update_mqd; 899 mqd->is_occupied = kfd_is_occupied_cp; 900 mqd->mqd_size = sizeof(struct v9_mqd); 901 mqd->mqd_stride = kfd_mqd_stride; 902 #if defined(CONFIG_DEBUG_FS) 903 mqd->debugfs_show_mqd = debugfs_show_mqd; 904 #endif 905 if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3) || 906 KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4)) { 907 mqd->init_mqd = init_mqd_hiq_v9_4_3; 908 mqd->load_mqd = hiq_load_mqd_kiq_v9_4_3; 909 mqd->destroy_mqd = destroy_hiq_mqd_v9_4_3; 910 mqd->check_preemption_failed = check_preemption_failed_v9_4_3; 911 } else { 912 mqd->init_mqd = init_mqd_hiq; 913 mqd->load_mqd = kfd_hiq_load_mqd_kiq; 914 mqd->destroy_mqd = destroy_hiq_mqd; 915 mqd->check_preemption_failed = check_preemption_failed; 916 } 917 break; 918 case KFD_MQD_TYPE_DIQ: 919 mqd->allocate_mqd = allocate_mqd; 920 mqd->init_mqd = init_mqd_hiq; 921 mqd->free_mqd = kfd_free_mqd_cp; 922 mqd->load_mqd = load_mqd; 923 mqd->update_mqd = update_mqd; 924 mqd->destroy_mqd = kfd_destroy_mqd_cp; 925 mqd->is_occupied = kfd_is_occupied_cp; 926 mqd->mqd_size = sizeof(struct v9_mqd); 927 #if defined(CONFIG_DEBUG_FS) 928 mqd->debugfs_show_mqd = debugfs_show_mqd; 929 #endif 930 break; 931 case KFD_MQD_TYPE_SDMA: 932 mqd->allocate_mqd = allocate_sdma_mqd; 933 mqd->init_mqd = init_mqd_sdma; 934 mqd->free_mqd = free_mqd_hiq_sdma; 935 mqd->load_mqd = kfd_load_mqd_sdma; 936 mqd->update_mqd = update_mqd_sdma; 937 mqd->destroy_mqd = kfd_destroy_mqd_sdma; 938 mqd->is_occupied = kfd_is_occupied_sdma; 939 mqd->checkpoint_mqd = checkpoint_mqd_sdma; 940 mqd->restore_mqd = restore_mqd_sdma; 941 mqd->mqd_size = sizeof(struct v9_sdma_mqd); 942 mqd->mqd_stride = kfd_mqd_stride; 943 #if defined(CONFIG_DEBUG_FS) 944 mqd->debugfs_show_mqd = debugfs_show_mqd_sdma; 945 #endif 946 break; 947 default: 948 kfree(mqd); 949 return NULL; 950 } 951 952 return mqd; 953 } 954