CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / amd-kfd-launch / reference / queues-rocm-6.2.0.c
| 1 | /* |
| 2 | * Copyright © 2014 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person |
| 5 | * obtaining a copy of this software and associated documentation |
| 6 | * files (the "Software"), to deal in the Software without |
| 7 | * restriction, including without limitation the rights to use, copy, |
| 8 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | * of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including |
| 13 | * the next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | * DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "libhsakmt.h" |
| 27 | #include "fmm.h" |
| 28 | #include "hsakmt/linux/kfd_ioctl.h" |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <math.h> |
| 33 | #include <stdio.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <errno.h> |
| 38 | |
| 39 | /* 1024 doorbells, 4 or 8 bytes each doorbell depending on ASIC generation */ |
| 40 | #define DOORBELL_SIZE(gfxv) (((gfxv) >= 0x90000) ? 8 : 4) |
| 41 | #define DOORBELLS_PAGE_SIZE(ds) (1024 * (ds)) |
| 42 | |
| 43 | #define WG_CONTEXT_DATA_SIZE_PER_CU(gfxv) \ |
| 44 | (get_vgpr_size_per_cu(gfxv) + SGPR_SIZE_PER_CU + \ |
| 45 | LDS_SIZE_PER_CU + HWREG_SIZE_PER_CU) |
| 46 | |
| 47 | #define CNTL_STACK_BYTES_PER_WAVE(gfxv) \ |
| 48 | ((gfxv) >= GFX_VERSION_NAVI10 ? 12 : 8) |
| 49 | |
| 50 | #define LDS_SIZE_PER_CU 0x10000 |
| 51 | #define HWREG_SIZE_PER_CU 0x1000 |
| 52 | #define DEBUGGER_BYTES_ALIGN 64 |
| 53 | #define DEBUGGER_BYTES_PER_WAVE 32 |
| 54 | |
| 55 | struct queue { |
| 56 | uint32_t queue_id; |
| 57 | uint64_t wptr; |
| 58 | uint64_t rptr; |
| 59 | void *eop_buffer; |
| 60 | void *ctx_save_restore; |
| 61 | uint32_t ctx_save_restore_size; |
| 62 | uint32_t ctl_stack_size; |
| 63 | uint32_t debug_memory_size; |
| 64 | uint32_t eop_buffer_size; |
| 65 | uint32_t total_mem_alloc_size; |
| 66 | uint32_t gfxv; |
| 67 | bool use_ats; |
| 68 | bool unified_ctx_save_restore; |
| 69 | /* This queue structure is allocated from GPU with page aligned size |
| 70 | * but only small bytes are used. We use the extra space in the end for |
| 71 | * cu_mask bits array. |
| 72 | */ |
| 73 | uint32_t cu_mask_count; /* in bits */ |
| 74 | uint32_t cu_mask[0]; |
| 75 | }; |
| 76 | |
| 77 | struct process_doorbells { |
| 78 | bool use_gpuvm; |
| 79 | uint32_t size; |
| 80 | void *mapping; |
| 81 | pthread_mutex_t mutex; |
| 82 | }; |
| 83 | |
| 84 | static unsigned int num_doorbells; |
| 85 | static struct process_doorbells *doorbells; |
| 86 | |
| 87 | uint32_t get_vgpr_size_per_cu(uint32_t gfxv) |
| 88 | { |
| 89 | uint32_t vgpr_size = 0x40000; |
| 90 | |
| 91 | if ((gfxv & ~(0xff)) == GFX_VERSION_AQUA_VANJARAM || |
| 92 | gfxv == GFX_VERSION_ALDEBARAN || |
| 93 | gfxv == GFX_VERSION_ARCTURUS) |
| 94 | vgpr_size = 0x80000; |
| 95 | |
| 96 | else if (gfxv == GFX_VERSION_PLUM_BONITO || |
| 97 | gfxv == GFX_VERSION_WHEAT_NAS || |
| 98 | gfxv == GFX_VERSION_GFX1200 || |
| 99 | gfxv == GFX_VERSION_GFX1201) |
| 100 | vgpr_size = 0x60000; |
| 101 | |
| 102 | return vgpr_size; |
| 103 | } |
| 104 | |
| 105 | HSAKMT_STATUS init_process_doorbells(unsigned int NumNodes) |
| 106 | { |
| 107 | unsigned int i; |
| 108 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 109 | |
| 110 | /* doorbells[] is accessed using Topology NodeId. This means doorbells[0], |
| 111 | * which corresponds to CPU only Node, might not be used |
| 112 | */ |
| 113 | doorbells = malloc(NumNodes * sizeof(struct process_doorbells)); |
| 114 | if (!doorbells) |
| 115 | return HSAKMT_STATUS_NO_MEMORY; |
| 116 | |
| 117 | for (i = 0; i < NumNodes; i++) { |
| 118 | doorbells[i].use_gpuvm = false; |
| 119 | doorbells[i].size = 0; |
| 120 | doorbells[i].mapping = NULL; |
| 121 | pthread_mutex_init(&doorbells[i].mutex, NULL); |
| 122 | } |
| 123 | |
| 124 | num_doorbells = NumNodes; |
| 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | static void get_doorbell_map_info(uint32_t node_id, |
| 130 | struct process_doorbells *doorbell) |
| 131 | { |
| 132 | /* |
| 133 | * GPUVM doorbell on Tonga requires a workaround for VM TLB ACTIVE bit |
| 134 | * lookup bug. Remove ASIC check when this is implemented in amdgpu. |
| 135 | */ |
| 136 | uint32_t gfxv = get_gfxv_by_node_id(node_id); |
| 137 | doorbell->use_gpuvm = (is_dgpu && gfxv != GFX_VERSION_TONGA); |
| 138 | doorbell->size = DOORBELLS_PAGE_SIZE(DOORBELL_SIZE(gfxv)); |
| 139 | |
| 140 | if (doorbell->size < (uint32_t) PAGE_SIZE) { |
| 141 | doorbell->size = PAGE_SIZE; |
| 142 | } |
| 143 | |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | void destroy_process_doorbells(void) |
| 148 | { |
| 149 | unsigned int i; |
| 150 | |
| 151 | if (!doorbells) |
| 152 | return; |
| 153 | |
| 154 | for (i = 0; i < num_doorbells; i++) { |
| 155 | if (!doorbells[i].size) |
| 156 | continue; |
| 157 | |
| 158 | if (doorbells[i].use_gpuvm) { |
| 159 | fmm_unmap_from_gpu(doorbells[i].mapping); |
| 160 | fmm_release(doorbells[i].mapping); |
| 161 | } else |
| 162 | munmap(doorbells[i].mapping, doorbells[i].size); |
| 163 | } |
| 164 | |
| 165 | free(doorbells); |
| 166 | doorbells = NULL; |
| 167 | num_doorbells = 0; |
| 168 | } |
| 169 | |
| 170 | /* This is a special funcion that should be called only from the child process |
| 171 | * after a fork(). This will clear doorbells duplicated from the parent. |
| 172 | */ |
| 173 | void clear_process_doorbells(void) |
| 174 | { |
| 175 | unsigned int i; |
| 176 | |
| 177 | if (!doorbells) |
| 178 | return; |
| 179 | |
| 180 | for (i = 0; i < num_doorbells; i++) { |
| 181 | if (!doorbells[i].size) |
| 182 | continue; |
| 183 | |
| 184 | if (!doorbells[i].use_gpuvm) |
| 185 | munmap(doorbells[i].mapping, doorbells[i].size); |
| 186 | } |
| 187 | |
| 188 | free(doorbells); |
| 189 | doorbells = NULL; |
| 190 | num_doorbells = 0; |
| 191 | } |
| 192 | |
| 193 | static HSAKMT_STATUS map_doorbell_apu(HSAuint32 NodeId, HSAuint32 gpu_id, |
| 194 | HSAuint64 doorbell_mmap_offset) |
| 195 | { |
| 196 | void *ptr; |
| 197 | |
| 198 | ptr = mmap(0, doorbells[NodeId].size, PROT_READ|PROT_WRITE, |
| 199 | MAP_SHARED, kfd_fd, doorbell_mmap_offset); |
| 200 | |
| 201 | if (ptr == MAP_FAILED) |
| 202 | return HSAKMT_STATUS_ERROR; |
| 203 | |
| 204 | doorbells[NodeId].mapping = ptr; |
| 205 | |
| 206 | return HSAKMT_STATUS_SUCCESS; |
| 207 | } |
| 208 | |
| 209 | static HSAKMT_STATUS map_doorbell_dgpu(HSAuint32 NodeId, HSAuint32 gpu_id, |
| 210 | HSAuint64 doorbell_mmap_offset) |
| 211 | { |
| 212 | void *ptr; |
| 213 | |
| 214 | ptr = fmm_allocate_doorbell(gpu_id, doorbells[NodeId].size, |
| 215 | doorbell_mmap_offset); |
| 216 | |
| 217 | if (!ptr) |
| 218 | return HSAKMT_STATUS_ERROR; |
| 219 | |
| 220 | /* map for GPU access */ |
| 221 | if (fmm_map_to_gpu(ptr, doorbells[NodeId].size, NULL)) { |
| 222 | fmm_release(ptr); |
| 223 | return HSAKMT_STATUS_ERROR; |
| 224 | } |
| 225 | |
| 226 | doorbells[NodeId].mapping = ptr; |
| 227 | |
| 228 | return HSAKMT_STATUS_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | static HSAKMT_STATUS map_doorbell(HSAuint32 NodeId, HSAuint32 gpu_id, |
| 232 | HSAuint64 doorbell_mmap_offset) |
| 233 | { |
| 234 | HSAKMT_STATUS status = HSAKMT_STATUS_SUCCESS; |
| 235 | |
| 236 | pthread_mutex_lock(&doorbells[NodeId].mutex); |
| 237 | if (doorbells[NodeId].size) { |
| 238 | pthread_mutex_unlock(&doorbells[NodeId].mutex); |
| 239 | return HSAKMT_STATUS_SUCCESS; |
| 240 | } |
| 241 | |
| 242 | get_doorbell_map_info(NodeId, &doorbells[NodeId]); |
| 243 | |
| 244 | if (doorbells[NodeId].use_gpuvm) { |
| 245 | status = map_doorbell_dgpu(NodeId, gpu_id, doorbell_mmap_offset); |
| 246 | if (status != HSAKMT_STATUS_SUCCESS) { |
| 247 | /* Fall back to the old method if KFD doesn't |
| 248 | * support doorbells in GPUVM |
| 249 | */ |
| 250 | doorbells[NodeId].use_gpuvm = false; |
| 251 | status = map_doorbell_apu(NodeId, gpu_id, doorbell_mmap_offset); |
| 252 | } |
| 253 | } else |
| 254 | status = map_doorbell_apu(NodeId, gpu_id, doorbell_mmap_offset); |
| 255 | |
| 256 | if (status != HSAKMT_STATUS_SUCCESS) |
| 257 | doorbells[NodeId].size = 0; |
| 258 | |
| 259 | pthread_mutex_unlock(&doorbells[NodeId].mutex); |
| 260 | |
| 261 | return status; |
| 262 | } |
| 263 | |
| 264 | static void *allocate_exec_aligned_memory_cpu(uint32_t size) |
| 265 | { |
| 266 | void *ptr; |
| 267 | |
| 268 | /* mmap will return a pointer with alignment equal to |
| 269 | * sysconf(_SC_PAGESIZE). |
| 270 | * |
| 271 | * MAP_ANONYMOUS initializes the memory to zero. |
| 272 | */ |
| 273 | ptr = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 274 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 275 | |
| 276 | if (ptr == MAP_FAILED) |
| 277 | return NULL; |
| 278 | return ptr; |
| 279 | } |
| 280 | |
| 281 | /* The bool return indicate whether the queue needs a context-save-restore area*/ |
| 282 | static bool update_ctx_save_restore_size(uint32_t nodeid, struct queue *q) |
| 283 | { |
| 284 | HsaNodeProperties node; |
| 285 | |
| 286 | if (q->gfxv < GFX_VERSION_CARRIZO) |
| 287 | return false; |
| 288 | if (hsaKmtGetNodeProperties(nodeid, &node)) |
| 289 | return false; |
| 290 | if (node.NumFComputeCores && node.NumSIMDPerCU) { |
| 291 | uint32_t ctl_stack_size, wg_data_size; |
| 292 | uint32_t cu_num = node.NumFComputeCores / node.NumSIMDPerCU / node.NumXcc; |
| 293 | uint32_t wave_num = (q->gfxv < GFX_VERSION_NAVI10) |
| 294 | ? MIN(cu_num * 40, node.NumShaderBanks / node.NumArrays * 512) |
| 295 | : cu_num * 32; |
| 296 | |
| 297 | ctl_stack_size = wave_num * CNTL_STACK_BYTES_PER_WAVE(q->gfxv) + 8; |
| 298 | wg_data_size = cu_num * WG_CONTEXT_DATA_SIZE_PER_CU(q->gfxv); |
| 299 | q->ctl_stack_size = PAGE_ALIGN_UP(sizeof(HsaUserContextSaveAreaHeader) |
| 300 | + ctl_stack_size); |
| 301 | if ((q->gfxv & 0x3f0000) == 0xA0000) { |
| 302 | /* HW design limits control stack size to 0x7000. |
| 303 | * This is insufficient for theoretical PM4 cases |
| 304 | * but sufficient for AQL, limited by SPI events. |
| 305 | */ |
| 306 | q->ctl_stack_size = MIN(q->ctl_stack_size, 0x7000); |
| 307 | } |
| 308 | |
| 309 | q->debug_memory_size = |
| 310 | ALIGN_UP(wave_num * DEBUGGER_BYTES_PER_WAVE, DEBUGGER_BYTES_ALIGN); |
| 311 | |
| 312 | q->ctx_save_restore_size = q->ctl_stack_size |
| 313 | + PAGE_ALIGN_UP(wg_data_size); |
| 314 | return true; |
| 315 | } |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | void *allocate_exec_aligned_memory_gpu(uint32_t size, uint32_t align, uint32_t gpu_id, |
| 320 | uint32_t NodeId, bool nonPaged, |
| 321 | bool DeviceLocal, |
| 322 | bool Uncached) |
| 323 | { |
| 324 | void *mem = NULL; |
| 325 | HSAuint64 gpu_va; |
| 326 | HsaMemFlags flags; |
| 327 | HSAuint32 cpu_id = 0; |
| 328 | |
| 329 | flags.Value = 0; |
| 330 | flags.ui32.HostAccess = !DeviceLocal; |
| 331 | flags.ui32.ExecuteAccess = 1; |
| 332 | flags.ui32.NonPaged = nonPaged; |
| 333 | flags.ui32.PageSize = HSA_PAGE_SIZE_4KB; |
| 334 | flags.ui32.CoarseGrain = DeviceLocal; |
| 335 | flags.ui32.Uncached = Uncached; |
| 336 | |
| 337 | size = ALIGN_UP(size, align); |
| 338 | |
| 339 | if (DeviceLocal && !zfb_support) |
| 340 | mem = fmm_allocate_device(gpu_id, NodeId, mem, size, 0, flags); |
| 341 | else { |
| 342 | /* VRAM under ZFB mode should be supported here without any |
| 343 | * additional code |
| 344 | */ |
| 345 | /* Get the closest cpu_id to GPU NodeId for system memory allocation |
| 346 | * nonPaged=0 system memory allocation uses GTT path |
| 347 | */ |
| 348 | if (!nonPaged) { |
| 349 | cpu_id = get_direct_link_cpu(NodeId); |
| 350 | if (cpu_id == INVALID_NODEID) { |
| 351 | flags.ui32.NoNUMABind = 1; |
| 352 | cpu_id = 0; |
| 353 | } |
| 354 | } |
| 355 | mem = fmm_allocate_host(gpu_id, cpu_id, mem, size, 0, flags); |
| 356 | } |
| 357 | |
| 358 | if (!mem) { |
| 359 | pr_err("Alloc %s memory failed size %d\n", |
| 360 | DeviceLocal ? "VRAM" : "GTT", size); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | if (NodeId != 0) { |
| 365 | uint32_t nodes_array[1] = {NodeId}; |
| 366 | HsaMemMapFlags map_flags = {0}; |
| 367 | HSAKMT_STATUS result; |
| 368 | |
| 369 | result = hsaKmtMapMemoryToGPUNodes(mem, size, &gpu_va, map_flags, 1, nodes_array); |
| 370 | if (result != HSAKMT_STATUS_SUCCESS) { |
| 371 | hsaKmtFreeMemory(mem, size); |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | return mem; |
| 376 | } |
| 377 | |
| 378 | if (hsaKmtMapMemoryToGPU(mem, size, &gpu_va) != HSAKMT_STATUS_SUCCESS) { |
| 379 | hsaKmtFreeMemory(mem, size); |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | return mem; |
| 384 | } |
| 385 | |
| 386 | void free_exec_aligned_memory_gpu(void *addr, uint32_t size, uint32_t align) |
| 387 | { |
| 388 | size = ALIGN_UP(size, align); |
| 389 | |
| 390 | if (hsaKmtUnmapMemoryToGPU(addr) == HSAKMT_STATUS_SUCCESS) |
| 391 | hsaKmtFreeMemory(addr, size); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Allocates memory aligned to sysconf(_SC_PAGESIZE) |
| 396 | */ |
| 397 | static void *allocate_exec_aligned_memory(uint32_t size, |
| 398 | bool use_ats, |
| 399 | uint32_t gpu_id, |
| 400 | uint32_t NodeId, |
| 401 | bool nonPaged, |
| 402 | bool DeviceLocal, |
| 403 | bool Uncached) |
| 404 | { |
| 405 | if (!use_ats) |
| 406 | return allocate_exec_aligned_memory_gpu(size, PAGE_SIZE, gpu_id, NodeId, |
| 407 | nonPaged, DeviceLocal, |
| 408 | Uncached); |
| 409 | return allocate_exec_aligned_memory_cpu(size); |
| 410 | } |
| 411 | |
| 412 | static void free_exec_aligned_memory(void *addr, uint32_t size, uint32_t align, |
| 413 | bool use_ats) |
| 414 | { |
| 415 | if (!use_ats) |
| 416 | free_exec_aligned_memory_gpu(addr, size, align); |
| 417 | else |
| 418 | munmap(addr, size); |
| 419 | } |
| 420 | |
| 421 | static HSAKMT_STATUS register_svm_range(void *mem, uint32_t size, |
| 422 | uint32_t gpuNode, uint32_t prefetchNode, |
| 423 | uint32_t preferredNode, bool alwaysMapped) |
| 424 | { |
| 425 | HSA_SVM_ATTRIBUTE *attrs; |
| 426 | HSAuint64 s_attr; |
| 427 | HSAuint32 nattr; |
| 428 | HSAuint32 flags; |
| 429 | |
| 430 | flags = HSA_SVM_FLAG_HOST_ACCESS | HSA_SVM_FLAG_GPU_EXEC; |
| 431 | |
| 432 | if (alwaysMapped) { |
| 433 | CHECK_KFD_MINOR_VERSION(11); |
| 434 | flags |= HSA_SVM_FLAG_GPU_ALWAYS_MAPPED; |
| 435 | } |
| 436 | |
| 437 | nattr = 6; |
| 438 | s_attr = sizeof(*attrs) * nattr; |
| 439 | attrs = (HSA_SVM_ATTRIBUTE *)alloca(s_attr); |
| 440 | |
| 441 | attrs[0].type = HSA_SVM_ATTR_PREFETCH_LOC; |
| 442 | attrs[0].value = prefetchNode; |
| 443 | attrs[1].type = HSA_SVM_ATTR_PREFERRED_LOC; |
| 444 | attrs[1].value = preferredNode; |
| 445 | attrs[2].type = HSA_SVM_ATTR_CLR_FLAGS; |
| 446 | attrs[2].value = ~flags; |
| 447 | attrs[3].type = HSA_SVM_ATTR_SET_FLAGS; |
| 448 | attrs[3].value = flags; |
| 449 | attrs[4].type = HSA_SVM_ATTR_ACCESS; |
| 450 | attrs[4].value = gpuNode; |
| 451 | attrs[5].type = HSA_SVM_ATTR_GRANULARITY; |
| 452 | attrs[5].value = 0xFF; |
| 453 | |
| 454 | return hsaKmtSVMSetAttr(mem, size, nattr, attrs); |
| 455 | } |
| 456 | |
| 457 | static void free_queue(struct queue *q) |
| 458 | { |
| 459 | if (q->eop_buffer) |
| 460 | free_exec_aligned_memory(q->eop_buffer, |
| 461 | q->eop_buffer_size, |
| 462 | PAGE_SIZE, q->use_ats); |
| 463 | if (q->unified_ctx_save_restore) |
| 464 | munmap(q->ctx_save_restore, q->total_mem_alloc_size); |
| 465 | else if (q->ctx_save_restore) |
| 466 | free_exec_aligned_memory(q->ctx_save_restore, |
| 467 | q->total_mem_alloc_size, |
| 468 | PAGE_SIZE, q->use_ats); |
| 469 | |
| 470 | free_exec_aligned_memory((void *)q, sizeof(*q), PAGE_SIZE, q->use_ats); |
| 471 | } |
| 472 | |
| 473 | static inline void fill_cwsr_header(struct queue *q, void *addr, |
| 474 | HsaEvent *Event, volatile HSAint64 *ErrPayload, HSAuint32 NumXcc) |
| 475 | { |
| 476 | uint32_t i; |
| 477 | HsaUserContextSaveAreaHeader *header; |
| 478 | |
| 479 | for (i = 0; i < NumXcc; i++) { |
| 480 | header = (HsaUserContextSaveAreaHeader *) |
| 481 | ((uintptr_t)addr + (i * q->ctx_save_restore_size)); |
| 482 | header->ErrorEventId = 0; |
| 483 | if (Event) |
| 484 | header->ErrorEventId = Event->EventId; |
| 485 | header->ErrorReason = ErrPayload; |
| 486 | header->DebugOffset = (NumXcc - i) * q->ctx_save_restore_size; |
| 487 | header->DebugSize = q->debug_memory_size * NumXcc; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static int handle_concrete_asic(struct queue *q, |
| 492 | struct kfd_ioctl_create_queue_args *args, |
| 493 | uint32_t gpu_id, |
| 494 | uint32_t NodeId, |
| 495 | HsaEvent *Event, |
| 496 | volatile HSAint64 *ErrPayload) |
| 497 | { |
| 498 | bool ret; |
| 499 | |
| 500 | if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA || |
| 501 | args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA_XGMI) |
| 502 | return HSAKMT_STATUS_SUCCESS; |
| 503 | |
| 504 | if (q->eop_buffer_size > 0) { |
| 505 | pr_info("Allocating VRAM for EOP\n"); |
| 506 | q->eop_buffer = allocate_exec_aligned_memory(q->eop_buffer_size, |
| 507 | q->use_ats, gpu_id, |
| 508 | NodeId, true, true, /* Unused for VRAM */false); |
| 509 | if (!q->eop_buffer) |
| 510 | return HSAKMT_STATUS_NO_MEMORY; |
| 511 | |
| 512 | args->eop_buffer_address = (uintptr_t)q->eop_buffer; |
| 513 | args->eop_buffer_size = q->eop_buffer_size; |
| 514 | } |
| 515 | |
| 516 | ret = update_ctx_save_restore_size(NodeId, q); |
| 517 | |
| 518 | if (ret) { |
| 519 | HsaNodeProperties node; |
| 520 | |
| 521 | if (hsaKmtGetNodeProperties(NodeId, &node)) |
| 522 | return HSAKMT_STATUS_ERROR; |
| 523 | |
| 524 | args->ctx_save_restore_size = q->ctx_save_restore_size; |
| 525 | args->ctl_stack_size = q->ctl_stack_size; |
| 526 | |
| 527 | /* Total memory to be allocated is = |
| 528 | * (Control Stack size + WG size + |
| 529 | * Debug memory area size) * num_xcc |
| 530 | */ |
| 531 | q->total_mem_alloc_size = (q->ctx_save_restore_size + |
| 532 | q->debug_memory_size) * node.NumXcc; |
| 533 | |
| 534 | /* Allocate unified memory for context save restore |
| 535 | * area on dGPU. |
| 536 | */ |
| 537 | if (!q->use_ats && is_svm_api_supported) { |
| 538 | uint32_t size = PAGE_ALIGN_UP(q->total_mem_alloc_size); |
| 539 | void *addr; |
| 540 | HSAKMT_STATUS r = HSAKMT_STATUS_ERROR; |
| 541 | |
| 542 | pr_info("Allocating GTT for CWSR\n"); |
| 543 | addr = mmap_allocate_aligned(PROT_READ | PROT_WRITE, |
| 544 | MAP_ANONYMOUS | MAP_PRIVATE, |
| 545 | size, GPU_HUGE_PAGE_SIZE, 0, |
| 546 | 0, (void *)LONG_MAX); |
| 547 | if (!addr) { |
| 548 | pr_err("mmap failed to alloc ctx area size 0x%x: %s\n", |
| 549 | size, strerror(errno)); |
| 550 | } else { |
| 551 | /* |
| 552 | * To avoid fork child process COW MMU notifier |
| 553 | * callback evict parent process queues. |
| 554 | */ |
| 555 | if (madvise(addr, size, MADV_DONTFORK)) |
| 556 | pr_err("madvise failed -%d\n", errno); |
| 557 | |
| 558 | fill_cwsr_header(q, addr, Event, ErrPayload, node.NumXcc); |
| 559 | |
| 560 | r = register_svm_range(addr, size, |
| 561 | NodeId, NodeId, 0, true); |
| 562 | |
| 563 | if (r == HSAKMT_STATUS_SUCCESS) { |
| 564 | q->ctx_save_restore = addr; |
| 565 | q->unified_ctx_save_restore = true; |
| 566 | } else { |
| 567 | munmap(addr, size); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (!q->unified_ctx_save_restore) { |
| 573 | q->ctx_save_restore = allocate_exec_aligned_memory( |
| 574 | q->total_mem_alloc_size, |
| 575 | q->use_ats, gpu_id, NodeId, |
| 576 | false, false, false); |
| 577 | |
| 578 | if (!q->ctx_save_restore) |
| 579 | return HSAKMT_STATUS_NO_MEMORY; |
| 580 | |
| 581 | fill_cwsr_header(q, q->ctx_save_restore, Event, ErrPayload, node.NumXcc); |
| 582 | } |
| 583 | |
| 584 | args->ctx_save_restore_address = (uintptr_t)q->ctx_save_restore; |
| 585 | } |
| 586 | |
| 587 | return HSAKMT_STATUS_SUCCESS; |
| 588 | } |
| 589 | |
| 590 | /* A map to translate thunk queue priority (-3 to +3) |
| 591 | * to KFD queue priority (0 to 15) |
| 592 | * Indexed by thunk_queue_priority+3 |
| 593 | */ |
| 594 | static uint32_t priority_map[] = {0, 3, 5, 7, 9, 11, 15}; |
| 595 | |
| 596 | HSAKMT_STATUS HSAKMTAPI hsaKmtCreateQueue(HSAuint32 NodeId, |
| 597 | HSA_QUEUE_TYPE Type, |
| 598 | HSAuint32 QueuePercentage, |
| 599 | HSA_QUEUE_PRIORITY Priority, |
| 600 | void *QueueAddress, |
| 601 | HSAuint64 QueueSizeInBytes, |
| 602 | HsaEvent *Event, |
| 603 | HsaQueueResource *QueueResource) |
| 604 | { |
| 605 | HSAKMT_STATUS result; |
| 606 | uint32_t gpu_id; |
| 607 | uint64_t doorbell_mmap_offset; |
| 608 | unsigned int doorbell_offset; |
| 609 | int err; |
| 610 | HsaNodeProperties props; |
| 611 | uint32_t cu_num, i; |
| 612 | |
| 613 | CHECK_KFD_OPEN(); |
| 614 | |
| 615 | if (Priority < HSA_QUEUE_PRIORITY_MINIMUM || |
| 616 | Priority > HSA_QUEUE_PRIORITY_MAXIMUM) |
| 617 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 618 | |
| 619 | result = validate_nodeid(NodeId, &gpu_id); |
| 620 | if (result != HSAKMT_STATUS_SUCCESS) |
| 621 | return result; |
| 622 | |
| 623 | struct queue *q = allocate_exec_aligned_memory(sizeof(*q), |
| 624 | false, gpu_id, NodeId, true, false, true); |
| 625 | if (!q) |
| 626 | return HSAKMT_STATUS_NO_MEMORY; |
| 627 | |
| 628 | memset(q, 0, sizeof(*q)); |
| 629 | |
| 630 | q->gfxv = get_gfxv_by_node_id(NodeId); |
| 631 | q->use_ats = false; |
| 632 | |
| 633 | if (q->gfxv == GFX_VERSION_TONGA) |
| 634 | q->eop_buffer_size = TONGA_PAGE_SIZE; |
| 635 | else if ((q->gfxv & ~(0xff)) == GFX_VERSION_AQUA_VANJARAM) |
| 636 | q->eop_buffer_size = ((Type == HSA_QUEUE_COMPUTE) ? 4096 : 0); |
| 637 | else if (q->gfxv >= 0x80000) |
| 638 | q->eop_buffer_size = 4096; |
| 639 | |
| 640 | /* By default, CUs are all turned on. Initialize cu_mask to '1 |
| 641 | * for all CU bits. |
| 642 | */ |
| 643 | if (hsaKmtGetNodeProperties(NodeId, &props)) |
| 644 | q->cu_mask_count = 0; |
| 645 | else { |
| 646 | cu_num = props.NumFComputeCores / props.NumSIMDPerCU; |
| 647 | /* cu_mask_count counts bits. It must be multiple of 32 */ |
| 648 | q->cu_mask_count = ALIGN_UP_32(cu_num, 32); |
| 649 | for (i = 0; i < cu_num; i++) |
| 650 | q->cu_mask[i/32] |= (1 << (i % 32)); |
| 651 | } |
| 652 | |
| 653 | struct kfd_ioctl_create_queue_args args = {0}; |
| 654 | |
| 655 | args.gpu_id = gpu_id; |
| 656 | |
| 657 | switch (Type) { |
| 658 | case HSA_QUEUE_COMPUTE: |
| 659 | args.queue_type = KFD_IOC_QUEUE_TYPE_COMPUTE; |
| 660 | break; |
| 661 | case HSA_QUEUE_SDMA: |
| 662 | args.queue_type = KFD_IOC_QUEUE_TYPE_SDMA; |
| 663 | break; |
| 664 | case HSA_QUEUE_SDMA_XGMI: |
| 665 | args.queue_type = KFD_IOC_QUEUE_TYPE_SDMA_XGMI; |
| 666 | break; |
| 667 | case HSA_QUEUE_COMPUTE_AQL: |
| 668 | args.queue_type = KFD_IOC_QUEUE_TYPE_COMPUTE_AQL; |
| 669 | break; |
| 670 | default: |
| 671 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 672 | } |
| 673 | |
| 674 | if (Type != HSA_QUEUE_COMPUTE_AQL) { |
| 675 | QueueResource->QueueRptrValue = (uintptr_t)&q->rptr; |
| 676 | QueueResource->QueueWptrValue = (uintptr_t)&q->wptr; |
| 677 | } |
| 678 | |
| 679 | err = handle_concrete_asic(q, &args, gpu_id, NodeId, Event, QueueResource->ErrorReason); |
| 680 | if (err != HSAKMT_STATUS_SUCCESS) { |
| 681 | free_queue(q); |
| 682 | return err; |
| 683 | } |
| 684 | |
| 685 | args.read_pointer_address = QueueResource->QueueRptrValue; |
| 686 | args.write_pointer_address = QueueResource->QueueWptrValue; |
| 687 | args.ring_base_address = (uintptr_t)QueueAddress; |
| 688 | args.ring_size = QueueSizeInBytes; |
| 689 | args.queue_percentage = QueuePercentage; |
| 690 | args.queue_priority = priority_map[Priority+3]; |
| 691 | |
| 692 | err = kmtIoctl(kfd_fd, AMDKFD_IOC_CREATE_QUEUE, &args); |
| 693 | |
| 694 | if (err == -1) { |
| 695 | free_queue(q); |
| 696 | return HSAKMT_STATUS_ERROR; |
| 697 | } |
| 698 | |
| 699 | q->queue_id = args.queue_id; |
| 700 | |
| 701 | if (IS_SOC15(q->gfxv)) { |
| 702 | HSAuint64 mask = DOORBELLS_PAGE_SIZE(DOORBELL_SIZE(q->gfxv)) - 1; |
| 703 | |
| 704 | /* On SOC15 chips, the doorbell offset within the |
| 705 | * doorbell page is included in the doorbell offset |
| 706 | * returned by KFD. This allows CP queue doorbells to be |
| 707 | * allocated dynamically (while SDMA queue doorbells fixed) |
| 708 | * rather than based on the its process queue ID. |
| 709 | */ |
| 710 | doorbell_mmap_offset = args.doorbell_offset & ~mask; |
| 711 | doorbell_offset = args.doorbell_offset & mask; |
| 712 | } else { |
| 713 | /* On older chips, the doorbell offset within the |
| 714 | * doorbell page is based on the queue ID. |
| 715 | */ |
| 716 | doorbell_mmap_offset = args.doorbell_offset; |
| 717 | doorbell_offset = q->queue_id * DOORBELL_SIZE(q->gfxv); |
| 718 | } |
| 719 | |
| 720 | err = map_doorbell(NodeId, gpu_id, doorbell_mmap_offset); |
| 721 | if (err != HSAKMT_STATUS_SUCCESS) { |
| 722 | hsaKmtDestroyQueue(q->queue_id); |
| 723 | return HSAKMT_STATUS_ERROR; |
| 724 | } |
| 725 | |
| 726 | QueueResource->QueueId = PORT_VPTR_TO_UINT64(q); |
| 727 | QueueResource->Queue_DoorBell = VOID_PTR_ADD(doorbells[NodeId].mapping, |
| 728 | doorbell_offset); |
| 729 | |
| 730 | return HSAKMT_STATUS_SUCCESS; |
| 731 | } |
| 732 | |
| 733 | |
| 734 | HSAKMT_STATUS HSAKMTAPI hsaKmtUpdateQueue(HSA_QUEUEID QueueId, |
| 735 | HSAuint32 QueuePercentage, |
| 736 | HSA_QUEUE_PRIORITY Priority, |
| 737 | void *QueueAddress, |
| 738 | HSAuint64 QueueSize, |
| 739 | HsaEvent *Event) |
| 740 | { |
| 741 | struct kfd_ioctl_update_queue_args arg = {0}; |
| 742 | struct queue *q = PORT_UINT64_TO_VPTR(QueueId); |
| 743 | |
| 744 | CHECK_KFD_OPEN(); |
| 745 | |
| 746 | if (Priority < HSA_QUEUE_PRIORITY_MINIMUM || |
| 747 | Priority > HSA_QUEUE_PRIORITY_MAXIMUM) |
| 748 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 749 | |
| 750 | if (!q) |
| 751 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 752 | arg.queue_id = (HSAuint32)q->queue_id; |
| 753 | arg.ring_base_address = (uintptr_t)QueueAddress; |
| 754 | arg.ring_size = QueueSize; |
| 755 | arg.queue_percentage = QueuePercentage; |
| 756 | arg.queue_priority = priority_map[Priority+3]; |
| 757 | |
| 758 | int err = kmtIoctl(kfd_fd, AMDKFD_IOC_UPDATE_QUEUE, &arg); |
| 759 | |
| 760 | if (err == -1) |
| 761 | return HSAKMT_STATUS_ERROR; |
| 762 | |
| 763 | return HSAKMT_STATUS_SUCCESS; |
| 764 | } |
| 765 | |
| 766 | HSAKMT_STATUS HSAKMTAPI hsaKmtDestroyQueue(HSA_QUEUEID QueueId) |
| 767 | { |
| 768 | CHECK_KFD_OPEN(); |
| 769 | |
| 770 | struct queue *q = PORT_UINT64_TO_VPTR(QueueId); |
| 771 | struct kfd_ioctl_destroy_queue_args args = {0}; |
| 772 | |
| 773 | if (!q) |
| 774 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 775 | |
| 776 | args.queue_id = q->queue_id; |
| 777 | |
| 778 | int err = kmtIoctl(kfd_fd, AMDKFD_IOC_DESTROY_QUEUE, &args); |
| 779 | |
| 780 | if (err == -1) { |
| 781 | pr_err("Failed to destroy queue: %s\n", strerror(errno)); |
| 782 | return HSAKMT_STATUS_ERROR; |
| 783 | } |
| 784 | |
| 785 | free_queue(q); |
| 786 | return HSAKMT_STATUS_SUCCESS; |
| 787 | } |
| 788 | |
| 789 | HSAKMT_STATUS HSAKMTAPI hsaKmtSetQueueCUMask(HSA_QUEUEID QueueId, |
| 790 | HSAuint32 CUMaskCount, |
| 791 | HSAuint32 *QueueCUMask) |
| 792 | { |
| 793 | struct queue *q = PORT_UINT64_TO_VPTR(QueueId); |
| 794 | struct kfd_ioctl_set_cu_mask_args args = {0}; |
| 795 | |
| 796 | CHECK_KFD_OPEN(); |
| 797 | |
| 798 | if (CUMaskCount == 0 || !QueueCUMask || ((CUMaskCount % 32) != 0)) |
| 799 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 800 | |
| 801 | args.queue_id = q->queue_id; |
| 802 | args.num_cu_mask = CUMaskCount; |
| 803 | args.cu_mask_ptr = (uintptr_t)QueueCUMask; |
| 804 | |
| 805 | int err = kmtIoctl(kfd_fd, AMDKFD_IOC_SET_CU_MASK, &args); |
| 806 | |
| 807 | if (err == -1) |
| 808 | return HSAKMT_STATUS_ERROR; |
| 809 | |
| 810 | memcpy(q->cu_mask, QueueCUMask, CUMaskCount / 8); |
| 811 | q->cu_mask_count = CUMaskCount; |
| 812 | |
| 813 | return HSAKMT_STATUS_SUCCESS; |
| 814 | } |
| 815 | |
| 816 | HSAKMT_STATUS |
| 817 | HSAKMTAPI |
| 818 | hsaKmtGetQueueInfo( |
| 819 | HSA_QUEUEID QueueId, |
| 820 | HsaQueueInfo *QueueInfo |
| 821 | ) |
| 822 | { |
| 823 | struct queue *q = PORT_UINT64_TO_VPTR(QueueId); |
| 824 | struct kfd_ioctl_get_queue_wave_state_args args = {0}; |
| 825 | |
| 826 | CHECK_KFD_OPEN(); |
| 827 | |
| 828 | if (QueueInfo == NULL || q == NULL) |
| 829 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 830 | |
| 831 | if (q->ctx_save_restore == NULL) |
| 832 | return HSAKMT_STATUS_ERROR; |
| 833 | |
| 834 | args.queue_id = q->queue_id; |
| 835 | args.ctl_stack_address = (uintptr_t)q->ctx_save_restore; |
| 836 | |
| 837 | if (kmtIoctl(kfd_fd, AMDKFD_IOC_GET_QUEUE_WAVE_STATE, &args) < 0) |
| 838 | return HSAKMT_STATUS_ERROR; |
| 839 | |
| 840 | QueueInfo->ControlStackTop = (void *)(args.ctl_stack_address + |
| 841 | q->ctl_stack_size - args.ctl_stack_used_size); |
| 842 | QueueInfo->UserContextSaveArea = (void *) |
| 843 | (args.ctl_stack_address + q->ctl_stack_size); |
| 844 | QueueInfo->SaveAreaSizeInBytes = args.save_area_used_size; |
| 845 | QueueInfo->ControlStackUsedInBytes = args.ctl_stack_used_size; |
| 846 | QueueInfo->NumCUAssigned = q->cu_mask_count; |
| 847 | QueueInfo->CUMaskInfo = q->cu_mask; |
| 848 | QueueInfo->QueueDetailError = 0; |
| 849 | QueueInfo->QueueTypeExtended = 0; |
| 850 | QueueInfo->SaveAreaHeader = q->ctx_save_restore; |
| 851 | |
| 852 | return HSAKMT_STATUS_SUCCESS; |
| 853 | } |
| 854 | |
| 855 | HSAKMT_STATUS HSAKMTAPI hsaKmtSetTrapHandler(HSAuint32 Node, |
| 856 | void *TrapHandlerBaseAddress, |
| 857 | HSAuint64 TrapHandlerSizeInBytes, |
| 858 | void *TrapBufferBaseAddress, |
| 859 | HSAuint64 TrapBufferSizeInBytes) |
| 860 | { |
| 861 | struct kfd_ioctl_set_trap_handler_args args = {0}; |
| 862 | HSAKMT_STATUS result; |
| 863 | uint32_t gpu_id; |
| 864 | |
| 865 | CHECK_KFD_OPEN(); |
| 866 | |
| 867 | result = validate_nodeid(Node, &gpu_id); |
| 868 | if (result != HSAKMT_STATUS_SUCCESS) |
| 869 | return result; |
| 870 | |
| 871 | args.gpu_id = gpu_id; |
| 872 | args.tba_addr = (uintptr_t)TrapHandlerBaseAddress; |
| 873 | args.tma_addr = (uintptr_t)TrapBufferBaseAddress; |
| 874 | |
| 875 | int err = kmtIoctl(kfd_fd, AMDKFD_IOC_SET_TRAP_HANDLER, &args); |
| 876 | |
| 877 | return (err == -1) ? HSAKMT_STATUS_ERROR : HSAKMT_STATUS_SUCCESS; |
| 878 | } |
| 879 | |
| 880 | uint32_t *convert_queue_ids(HSAuint32 NumQueues, HSA_QUEUEID *Queues) |
| 881 | { |
| 882 | uint32_t *queue_ids_ptr; |
| 883 | unsigned int i; |
| 884 | |
| 885 | queue_ids_ptr = malloc(NumQueues * sizeof(uint32_t)); |
| 886 | if (!queue_ids_ptr) |
| 887 | return NULL; |
| 888 | |
| 889 | for (i = 0; i < NumQueues; i++) { |
| 890 | struct queue *q = PORT_UINT64_TO_VPTR(Queues[i]); |
| 891 | |
| 892 | queue_ids_ptr[i] = q->queue_id; |
| 893 | } |
| 894 | return queue_ids_ptr; |
| 895 | } |
| 896 | |
| 897 | HSAKMT_STATUS |
| 898 | HSAKMTAPI |
| 899 | hsaKmtAllocQueueGWS( |
| 900 | HSA_QUEUEID QueueId, |
| 901 | HSAuint32 nGWS, |
| 902 | HSAuint32 *firstGWS) |
| 903 | { |
| 904 | struct kfd_ioctl_alloc_queue_gws_args args = {0}; |
| 905 | struct queue *q = PORT_UINT64_TO_VPTR(QueueId); |
| 906 | |
| 907 | CHECK_KFD_OPEN(); |
| 908 | |
| 909 | args.queue_id = (HSAuint32)q->queue_id; |
| 910 | args.num_gws = nGWS; |
| 911 | |
| 912 | int err = kmtIoctl(kfd_fd, AMDKFD_IOC_ALLOC_QUEUE_GWS, &args); |
| 913 | |
| 914 | if (!err && firstGWS) |
| 915 | *firstGWS = args.first_gws; |
| 916 | |
| 917 | if (!err) |
| 918 | return HSAKMT_STATUS_SUCCESS; |
| 919 | else if (errno == EINVAL) |
| 920 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 921 | else if (errno == EBUSY) |
| 922 | return HSAKMT_STATUS_OUT_OF_RESOURCES; |
| 923 | else if (errno == ENODEV) |
| 924 | return HSAKMT_STATUS_NOT_SUPPORTED; |
| 925 | else |
| 926 | return HSAKMT_STATUS_ERROR; |
| 927 | } |