1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* 3 * Copyright 2014-2022 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 */ 24 25 #include <linux/slab.h> 26 #include "kfd_priv.h" 27 #include "kfd_topology.h" 28 #include "kfd_svm.h" 29 30 void print_queue_properties(struct queue_properties *q) 31 { 32 if (!q) 33 return; 34 35 pr_debug("Printing queue properties:\n"); 36 pr_debug("Queue Type: %u\n", q->type); 37 pr_debug("Queue Size: %llu\n", q->queue_size); 38 pr_debug("Queue percent: %u\n", q->queue_percent); 39 pr_debug("Queue Address: 0x%llX\n", q->queue_address); 40 pr_debug("Queue Id: %u\n", q->queue_id); 41 pr_debug("Queue Process Vmid: %u\n", q->vmid); 42 pr_debug("Queue Read Pointer: 0x%px\n", q->read_ptr); 43 pr_debug("Queue Write Pointer: 0x%px\n", q->write_ptr); 44 pr_debug("Queue Doorbell Pointer: 0x%p\n", q->doorbell_ptr); 45 pr_debug("Queue Doorbell Offset: %u\n", q->doorbell_off); 46 } 47 48 void print_queue(struct queue *q) 49 { 50 if (!q) 51 return; 52 pr_debug("Printing queue:\n"); 53 pr_debug("Queue Type: %u\n", q->properties.type); 54 pr_debug("Queue Size: %llu\n", q->properties.queue_size); 55 pr_debug("Queue percent: %u\n", q->properties.queue_percent); 56 pr_debug("Queue Address: 0x%llX\n", q->properties.queue_address); 57 pr_debug("Queue Id: %u\n", q->properties.queue_id); 58 pr_debug("Queue Process Vmid: %u\n", q->properties.vmid); 59 pr_debug("Queue Read Pointer: 0x%px\n", q->properties.read_ptr); 60 pr_debug("Queue Write Pointer: 0x%px\n", q->properties.write_ptr); 61 pr_debug("Queue Doorbell Pointer: 0x%p\n", q->properties.doorbell_ptr); 62 pr_debug("Queue Doorbell Offset: %u\n", q->properties.doorbell_off); 63 pr_debug("Queue MQD Address: 0x%p\n", q->mqd); 64 pr_debug("Queue MQD Gart: 0x%llX\n", q->gart_mqd_addr); 65 pr_debug("Queue Process Address: 0x%p\n", q->process); 66 pr_debug("Queue Device Address: 0x%p\n", q->device); 67 } 68 69 int init_queue(struct queue **q, const struct queue_properties *properties) 70 { 71 struct queue *tmp_q; 72 73 tmp_q = kzalloc(sizeof(*tmp_q), GFP_KERNEL); 74 if (!tmp_q) 75 return -ENOMEM; 76 77 memcpy(&tmp_q->properties, properties, sizeof(*properties)); 78 79 *q = tmp_q; 80 return 0; 81 } 82 83 void uninit_queue(struct queue *q) 84 { 85 kfree(q); 86 } 87 88 #if IS_ENABLED(CONFIG_HSA_AMD_SVM) 89 90 static int kfd_queue_buffer_svm_get(struct kfd_process_device *pdd, u64 addr, u64 size) 91 { 92 struct kfd_process *p = pdd->process; 93 struct list_head update_list; 94 struct svm_range *prange; 95 int ret = -EINVAL; 96 97 INIT_LIST_HEAD(&update_list); 98 addr >>= PAGE_SHIFT; 99 size >>= PAGE_SHIFT; 100 101 mutex_lock(&p->svms.lock); 102 103 /* 104 * range may split to multiple svm pranges aligned to granularity boundaery. 105 */ 106 while (size) { 107 uint32_t gpuid, gpuidx; 108 int r; 109 110 prange = svm_range_from_addr(&p->svms, addr, NULL); 111 if (!prange) 112 break; 113 114 if (!prange->mapped_to_gpu) 115 break; 116 117 r = kfd_process_gpuid_from_node(p, pdd->dev, &gpuid, &gpuidx); 118 if (r < 0) 119 break; 120 if (!test_bit(gpuidx, prange->bitmap_access) && 121 !test_bit(gpuidx, prange->bitmap_aip)) 122 break; 123 124 if (!(prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) 125 break; 126 127 list_add(&prange->update_list, &update_list); 128 129 if (prange->last - prange->start + 1 >= size) { 130 size = 0; 131 break; 132 } 133 134 size -= prange->last - prange->start + 1; 135 addr += prange->last - prange->start + 1; 136 } 137 if (size) { 138 pr_debug("[0x%llx 0x%llx] not registered\n", addr, addr + size - 1); 139 goto out_unlock; 140 } 141 142 list_for_each_entry(prange, &update_list, update_list) 143 atomic_inc(&prange->queue_refcount); 144 ret = 0; 145 146 out_unlock: 147 mutex_unlock(&p->svms.lock); 148 return ret; 149 } 150 151 static void kfd_queue_buffer_svm_put(struct kfd_process_device *pdd, u64 addr, u64 size) 152 { 153 struct kfd_process *p = pdd->process; 154 struct svm_range *prange, *pchild; 155 struct interval_tree_node *node; 156 unsigned long last; 157 158 addr >>= PAGE_SHIFT; 159 last = addr + (size >> PAGE_SHIFT) - 1; 160 161 mutex_lock(&p->svms.lock); 162 163 node = interval_tree_iter_first(&p->svms.objects, addr, last); 164 while (node) { 165 struct interval_tree_node *next_node; 166 unsigned long next_start; 167 168 prange = container_of(node, struct svm_range, it_node); 169 next_node = interval_tree_iter_next(node, addr, last); 170 next_start = min(node->last, last) + 1; 171 172 if (atomic_add_unless(&prange->queue_refcount, -1, 0)) { 173 list_for_each_entry(pchild, &prange->child_list, child_list) 174 atomic_add_unless(&pchild->queue_refcount, -1, 0); 175 } 176 177 node = next_node; 178 addr = next_start; 179 } 180 181 mutex_unlock(&p->svms.lock); 182 } 183 #else 184 185 static int kfd_queue_buffer_svm_get(struct kfd_process_device *pdd, u64 addr, u64 size) 186 { 187 return -EINVAL; 188 } 189 190 static void kfd_queue_buffer_svm_put(struct kfd_process_device *pdd, u64 addr, u64 size) 191 { 192 } 193 194 #endif 195 196 int kfd_queue_buffer_get(struct amdgpu_vm *vm, void __user *addr, struct amdgpu_bo **pbo, 197 u64 expected_size) 198 { 199 struct amdgpu_bo_va_mapping *mapping; 200 u64 user_addr; 201 u64 size; 202 203 user_addr = (u64)addr >> AMDGPU_GPU_PAGE_SHIFT; 204 size = expected_size >> AMDGPU_GPU_PAGE_SHIFT; 205 206 mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr); 207 if (!mapping) 208 goto out_err; 209 210 if (user_addr != mapping->start || 211 (size != 0 && user_addr + size - 1 != mapping->last)) { 212 pr_debug("expected size 0x%llx not equal to mapping addr 0x%llx size 0x%llx\n", 213 expected_size, mapping->start << AMDGPU_GPU_PAGE_SHIFT, 214 (mapping->last - mapping->start + 1) << AMDGPU_GPU_PAGE_SHIFT); 215 goto out_err; 216 } 217 218 *pbo = amdgpu_bo_ref(mapping->bo_va->base.bo); 219 mapping->bo_va->queue_refcount++; 220 return 0; 221 222 out_err: 223 *pbo = NULL; 224 return -EINVAL; 225 } 226 227 void kfd_queue_buffer_put(struct amdgpu_vm *vm, struct amdgpu_bo **bo) 228 { 229 if (*bo) { 230 struct amdgpu_bo_va *bo_va; 231 232 bo_va = amdgpu_vm_bo_find(vm, *bo); 233 if (bo_va) 234 bo_va->queue_refcount--; 235 } 236 237 amdgpu_bo_unref(bo); 238 } 239 240 int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_properties *properties) 241 { 242 struct kfd_topology_device *topo_dev; 243 struct amdgpu_vm *vm; 244 u32 total_cwsr_size; 245 int err; 246 247 topo_dev = kfd_topology_device_by_id(pdd->dev->id); 248 if (!topo_dev) 249 return -EINVAL; 250 251 vm = drm_priv_to_vm(pdd->drm_priv); 252 err = amdgpu_bo_reserve(vm->root.bo, false); 253 if (err) 254 return err; 255 256 err = kfd_queue_buffer_get(vm, properties->write_ptr, &properties->wptr_bo, PAGE_SIZE); 257 if (err) 258 goto out_err_unreserve; 259 260 err = kfd_queue_buffer_get(vm, properties->read_ptr, &properties->rptr_bo, PAGE_SIZE); 261 if (err) 262 goto out_err_unreserve; 263 264 err = kfd_queue_buffer_get(vm, (void *)properties->queue_address, 265 &properties->ring_bo, properties->queue_size); 266 if (err) 267 goto out_err_unreserve; 268 269 /* only compute queue requires EOP buffer and CWSR area */ 270 if (properties->type != KFD_QUEUE_TYPE_COMPUTE) 271 goto out_unreserve; 272 273 /* EOP buffer is not required for all ASICs */ 274 if (properties->eop_ring_buffer_address) { 275 if (properties->eop_ring_buffer_size != topo_dev->node_props.eop_buffer_size) { 276 pr_debug("queue eop bo size 0x%lx not equal to node eop buf size 0x%x\n", 277 properties->eop_buf_bo->tbo.base.size, 278 topo_dev->node_props.eop_buffer_size); 279 err = -EINVAL; 280 goto out_err_unreserve; 281 } 282 err = kfd_queue_buffer_get(vm, (void *)properties->eop_ring_buffer_address, 283 &properties->eop_buf_bo, 284 properties->eop_ring_buffer_size); 285 if (err) 286 goto out_err_unreserve; 287 } 288 289 if (properties->ctl_stack_size != topo_dev->node_props.ctl_stack_size) { 290 pr_debug("queue ctl stack size 0x%x not equal to node ctl stack size 0x%x\n", 291 properties->ctl_stack_size, 292 topo_dev->node_props.ctl_stack_size); 293 err = -EINVAL; 294 goto out_err_unreserve; 295 } 296 297 if (properties->ctx_save_restore_area_size != topo_dev->node_props.cwsr_size) { 298 pr_debug("queue cwsr size 0x%x not equal to node cwsr size 0x%x\n", 299 properties->ctx_save_restore_area_size, 300 topo_dev->node_props.cwsr_size); 301 err = -EINVAL; 302 goto out_err_unreserve; 303 } 304 305 total_cwsr_size = (topo_dev->node_props.cwsr_size + topo_dev->node_props.debug_memory_size) 306 * NUM_XCC(pdd->dev->xcc_mask); 307 total_cwsr_size = ALIGN(total_cwsr_size, PAGE_SIZE); 308 309 err = kfd_queue_buffer_get(vm, (void *)properties->ctx_save_restore_area_address, 310 &properties->cwsr_bo, total_cwsr_size); 311 if (!err) 312 goto out_unreserve; 313 314 amdgpu_bo_unreserve(vm->root.bo); 315 316 err = kfd_queue_buffer_svm_get(pdd, properties->ctx_save_restore_area_address, 317 total_cwsr_size); 318 if (err) 319 goto out_err_release; 320 321 return 0; 322 323 out_unreserve: 324 amdgpu_bo_unreserve(vm->root.bo); 325 return 0; 326 327 out_err_unreserve: 328 amdgpu_bo_unreserve(vm->root.bo); 329 out_err_release: 330 kfd_queue_release_buffers(pdd, properties); 331 return err; 332 } 333 334 int kfd_queue_release_buffers(struct kfd_process_device *pdd, struct queue_properties *properties) 335 { 336 struct kfd_topology_device *topo_dev; 337 struct amdgpu_vm *vm; 338 u32 total_cwsr_size; 339 int err; 340 341 vm = drm_priv_to_vm(pdd->drm_priv); 342 err = amdgpu_bo_reserve(vm->root.bo, false); 343 if (err) 344 return err; 345 346 kfd_queue_buffer_put(vm, &properties->wptr_bo); 347 kfd_queue_buffer_put(vm, &properties->rptr_bo); 348 kfd_queue_buffer_put(vm, &properties->ring_bo); 349 kfd_queue_buffer_put(vm, &properties->eop_buf_bo); 350 kfd_queue_buffer_put(vm, &properties->cwsr_bo); 351 352 amdgpu_bo_unreserve(vm->root.bo); 353 354 topo_dev = kfd_topology_device_by_id(pdd->dev->id); 355 if (!topo_dev) 356 return -EINVAL; 357 total_cwsr_size = (topo_dev->node_props.cwsr_size + topo_dev->node_props.debug_memory_size) 358 * NUM_XCC(pdd->dev->xcc_mask); 359 total_cwsr_size = ALIGN(total_cwsr_size, PAGE_SIZE); 360 361 kfd_queue_buffer_svm_put(pdd, properties->ctx_save_restore_area_address, total_cwsr_size); 362 return 0; 363 } 364 365 #define SGPR_SIZE_PER_CU 0x4000 366 #define LDS_SIZE_PER_CU 0x10000 367 #define HWREG_SIZE_PER_CU 0x1000 368 #define DEBUGGER_BYTES_ALIGN 64 369 #define DEBUGGER_BYTES_PER_WAVE 32 370 371 static u32 kfd_get_vgpr_size_per_cu(u32 gfxv) 372 { 373 u32 vgpr_size = 0x40000; 374 375 if ((gfxv / 100 * 100) == 90400 || /* GFX_VERSION_AQUA_VANJARAM */ 376 gfxv == 90010 || /* GFX_VERSION_ALDEBARAN */ 377 gfxv == 90008) /* GFX_VERSION_ARCTURUS */ 378 vgpr_size = 0x80000; 379 else if (gfxv == 110000 || /* GFX_VERSION_PLUM_BONITO */ 380 gfxv == 110001 || /* GFX_VERSION_WHEAT_NAS */ 381 gfxv == 120000 || /* GFX_VERSION_GFX1200 */ 382 gfxv == 120001) /* GFX_VERSION_GFX1201 */ 383 vgpr_size = 0x60000; 384 385 return vgpr_size; 386 } 387 388 #define WG_CONTEXT_DATA_SIZE_PER_CU(gfxv) \ 389 (kfd_get_vgpr_size_per_cu(gfxv) + SGPR_SIZE_PER_CU +\ 390 LDS_SIZE_PER_CU + HWREG_SIZE_PER_CU) 391 392 #define CNTL_STACK_BYTES_PER_WAVE(gfxv) \ 393 ((gfxv) >= 100100 ? 12 : 8) /* GFX_VERSION_NAVI10*/ 394 395 #define SIZEOF_HSA_USER_CONTEXT_SAVE_AREA_HEADER 40 396 397 void kfd_queue_ctx_save_restore_size(struct kfd_topology_device *dev) 398 { 399 struct kfd_node_properties *props = &dev->node_props; 400 u32 gfxv = props->gfx_target_version; 401 u32 ctl_stack_size; 402 u32 wg_data_size; 403 u32 wave_num; 404 u32 cu_num; 405 406 if (gfxv < 80001) /* GFX_VERSION_CARRIZO */ 407 return; 408 409 cu_num = props->simd_count / props->simd_per_cu / NUM_XCC(dev->gpu->xcc_mask); 410 wave_num = (gfxv < 100100) ? /* GFX_VERSION_NAVI10 */ 411 min(cu_num * 40, props->array_count / props->simd_arrays_per_engine * 512) 412 : cu_num * 32; 413 414 wg_data_size = ALIGN(cu_num * WG_CONTEXT_DATA_SIZE_PER_CU(gfxv), PAGE_SIZE); 415 ctl_stack_size = wave_num * CNTL_STACK_BYTES_PER_WAVE(gfxv) + 8; 416 ctl_stack_size = ALIGN(SIZEOF_HSA_USER_CONTEXT_SAVE_AREA_HEADER + ctl_stack_size, 417 PAGE_SIZE); 418 419 if ((gfxv / 10000 * 10000) == 100000) { 420 /* HW design limits control stack size to 0x7000. 421 * This is insufficient for theoretical PM4 cases 422 * but sufficient for AQL, limited by SPI events. 423 */ 424 ctl_stack_size = min(ctl_stack_size, 0x7000); 425 } 426 427 props->ctl_stack_size = ctl_stack_size; 428 props->debug_memory_size = ALIGN(wave_num * DEBUGGER_BYTES_PER_WAVE, DEBUGGER_BYTES_ALIGN); 429 props->cwsr_size = ctl_stack_size + wg_data_size; 430 431 if (gfxv == 80002) /* GFX_VERSION_TONGA */ 432 props->eop_buffer_size = 0x8000; 433 else if ((gfxv / 100 * 100) == 90400) /* GFX_VERSION_AQUA_VANJARAM */ 434 props->eop_buffer_size = 4096; 435 else if (gfxv >= 80000) 436 props->eop_buffer_size = 4096; 437 } 438