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/types.h> 26 #include <linux/mutex.h> 27 #include <linux/slab.h> 28 #include <linux/printk.h> 29 #include <linux/sched.h> 30 #include "kfd_kernel_queue.h" 31 #include "kfd_priv.h" 32 #include "kfd_device_queue_manager.h" 33 #include "kfd_pm4_headers.h" 34 #include "kfd_pm4_opcodes.h" 35 36 #define PM4_COUNT_ZERO (((1 << 15) - 1) << 16) 37 38 /* Initialize a kernel queue, including allocations of GART memory 39 * needed for the queue. 40 */ 41 static bool kq_initialize(struct kernel_queue *kq, struct kfd_node *dev, 42 enum kfd_queue_type type, unsigned int queue_size) 43 { 44 struct queue_properties prop; 45 int retval; 46 union PM4_MES_TYPE_3_HEADER nop; 47 48 if (WARN_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ)) 49 return false; 50 51 pr_debug("Initializing queue type %d size %d\n", KFD_QUEUE_TYPE_HIQ, 52 queue_size); 53 54 memset(&prop, 0, sizeof(prop)); 55 memset(&nop, 0, sizeof(nop)); 56 57 nop.opcode = IT_NOP; 58 nop.type = PM4_TYPE_3; 59 nop.u32all |= PM4_COUNT_ZERO; 60 61 kq->dev = dev; 62 kq->nop_packet = nop.u32all; 63 switch (type) { 64 case KFD_QUEUE_TYPE_DIQ: 65 kq->mqd_mgr = dev->dqm->mqd_mgrs[KFD_MQD_TYPE_DIQ]; 66 break; 67 case KFD_QUEUE_TYPE_HIQ: 68 kq->mqd_mgr = dev->dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]; 69 break; 70 default: 71 pr_err("Invalid queue type %d\n", type); 72 return false; 73 } 74 75 if (!kq->mqd_mgr) 76 return false; 77 78 prop.doorbell_ptr = kfd_get_kernel_doorbell(dev->kfd, &prop.doorbell_off); 79 80 if (!prop.doorbell_ptr) { 81 pr_err("Failed to initialize doorbell"); 82 goto err_get_kernel_doorbell; 83 } 84 85 retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq); 86 if (retval != 0) { 87 pr_err("Failed to init pq queues size %d\n", queue_size); 88 goto err_pq_allocate_vidmem; 89 } 90 91 kq->pq_kernel_addr = kq->pq->cpu_ptr; 92 kq->pq_gpu_addr = kq->pq->gpu_addr; 93 94 /* For CIK family asics, kq->eop_mem is not needed */ 95 if (dev->adev->asic_type > CHIP_MULLINS) { 96 retval = kfd_gtt_sa_allocate(dev, PAGE_SIZE, &kq->eop_mem); 97 if (retval != 0) 98 goto err_eop_allocate_vidmem; 99 100 kq->eop_gpu_addr = kq->eop_mem->gpu_addr; 101 kq->eop_kernel_addr = kq->eop_mem->cpu_ptr; 102 103 memset(kq->eop_kernel_addr, 0, PAGE_SIZE); 104 } 105 106 retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel), 107 &kq->rptr_mem); 108 109 if (retval != 0) 110 goto err_rptr_allocate_vidmem; 111 112 kq->rptr_kernel = kq->rptr_mem->cpu_ptr; 113 kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr; 114 115 retval = kfd_gtt_sa_allocate(dev, dev->kfd->device_info.doorbell_size, 116 &kq->wptr_mem); 117 118 if (retval != 0) 119 goto err_wptr_allocate_vidmem; 120 121 kq->wptr_kernel = kq->wptr_mem->cpu_ptr; 122 kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr; 123 124 memset(kq->pq_kernel_addr, 0, queue_size); 125 memset(kq->rptr_kernel, 0, sizeof(*kq->rptr_kernel)); 126 memset(kq->wptr_kernel, 0, sizeof(*kq->wptr_kernel)); 127 128 prop.queue_size = queue_size; 129 prop.is_interop = false; 130 prop.is_gws = false; 131 prop.priority = 1; 132 prop.queue_percent = 100; 133 prop.type = type; 134 prop.vmid = 0; 135 prop.queue_address = kq->pq_gpu_addr; 136 prop.read_ptr = (uint32_t *) kq->rptr_gpu_addr; 137 prop.write_ptr = (uint32_t *) kq->wptr_gpu_addr; 138 prop.eop_ring_buffer_address = kq->eop_gpu_addr; 139 prop.eop_ring_buffer_size = PAGE_SIZE; 140 141 if (init_queue(&kq->queue, &prop) != 0) 142 goto err_init_queue; 143 144 kq->queue->device = dev; 145 kq->queue->process = kfd_get_process(current); 146 147 kq->queue->mqd_mem_obj = kq->mqd_mgr->allocate_mqd(kq->mqd_mgr->dev, 148 &kq->queue->properties); 149 if (!kq->queue->mqd_mem_obj) 150 goto err_allocate_mqd; 151 kq->mqd_mgr->init_mqd(kq->mqd_mgr, &kq->queue->mqd, 152 kq->queue->mqd_mem_obj, 153 &kq->queue->gart_mqd_addr, 154 &kq->queue->properties); 155 /* assign HIQ to HQD */ 156 if (type == KFD_QUEUE_TYPE_HIQ) { 157 pr_debug("Assigning hiq to hqd\n"); 158 kq->queue->pipe = KFD_CIK_HIQ_PIPE; 159 kq->queue->queue = KFD_CIK_HIQ_QUEUE; 160 kq->mqd_mgr->load_mqd(kq->mqd_mgr, kq->queue->mqd, 161 kq->queue->pipe, kq->queue->queue, 162 &kq->queue->properties, NULL); 163 } else { 164 /* allocate fence for DIQ */ 165 166 retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t), 167 &kq->fence_mem_obj); 168 169 if (retval != 0) 170 goto err_alloc_fence; 171 172 kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr; 173 kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr; 174 } 175 176 print_queue(kq->queue); 177 178 return true; 179 err_alloc_fence: 180 kq->mqd_mgr->free_mqd(kq->mqd_mgr, kq->queue->mqd, kq->queue->mqd_mem_obj); 181 err_allocate_mqd: 182 uninit_queue(kq->queue); 183 err_init_queue: 184 kfd_gtt_sa_free(dev, kq->wptr_mem); 185 err_wptr_allocate_vidmem: 186 kfd_gtt_sa_free(dev, kq->rptr_mem); 187 err_rptr_allocate_vidmem: 188 kfd_gtt_sa_free(dev, kq->eop_mem); 189 err_eop_allocate_vidmem: 190 kfd_gtt_sa_free(dev, kq->pq); 191 err_pq_allocate_vidmem: 192 kfd_release_kernel_doorbell(dev->kfd, prop.doorbell_ptr); 193 err_get_kernel_doorbell: 194 return false; 195 196 } 197 198 /* Uninitialize a kernel queue and free all its memory usages. */ 199 static void kq_uninitialize(struct kernel_queue *kq, bool hanging) 200 { 201 if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ && !hanging) 202 kq->mqd_mgr->destroy_mqd(kq->mqd_mgr, 203 kq->queue->mqd, 204 KFD_PREEMPT_TYPE_WAVEFRONT_RESET, 205 KFD_UNMAP_LATENCY_MS, 206 kq->queue->pipe, 207 kq->queue->queue); 208 else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ) 209 kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj); 210 211 kq->mqd_mgr->free_mqd(kq->mqd_mgr, kq->queue->mqd, 212 kq->queue->mqd_mem_obj); 213 214 kfd_gtt_sa_free(kq->dev, kq->rptr_mem); 215 kfd_gtt_sa_free(kq->dev, kq->wptr_mem); 216 217 /* For CIK family asics, kq->eop_mem is Null, kfd_gtt_sa_free() 218 * is able to handle NULL properly. 219 */ 220 kfd_gtt_sa_free(kq->dev, kq->eop_mem); 221 222 kfd_gtt_sa_free(kq->dev, kq->pq); 223 kfd_release_kernel_doorbell(kq->dev->kfd, 224 kq->queue->properties.doorbell_ptr); 225 uninit_queue(kq->queue); 226 } 227 228 int kq_acquire_packet_buffer(struct kernel_queue *kq, 229 size_t packet_size_in_dwords, unsigned int **buffer_ptr) 230 { 231 size_t available_size; 232 size_t queue_size_dwords; 233 uint32_t wptr, rptr; 234 uint64_t wptr64; 235 unsigned int *queue_address; 236 237 /* When rptr == wptr, the buffer is empty. 238 * When rptr == wptr + 1, the buffer is full. 239 * It is always rptr that advances to the position of wptr, rather than 240 * the opposite. So we can only use up to queue_size_dwords - 1 dwords. 241 */ 242 rptr = *kq->rptr_kernel; 243 wptr = kq->pending_wptr; 244 wptr64 = kq->pending_wptr64; 245 queue_address = (unsigned int *)kq->pq_kernel_addr; 246 queue_size_dwords = kq->queue->properties.queue_size / 4; 247 248 pr_debug("rptr: %d\n", rptr); 249 pr_debug("wptr: %d\n", wptr); 250 pr_debug("queue_address 0x%p\n", queue_address); 251 252 available_size = (rptr + queue_size_dwords - 1 - wptr) % 253 queue_size_dwords; 254 255 if (packet_size_in_dwords > available_size) { 256 /* 257 * make sure calling functions know 258 * acquire_packet_buffer() failed 259 */ 260 goto err_no_space; 261 } 262 263 if (wptr + packet_size_in_dwords >= queue_size_dwords) { 264 /* make sure after rolling back to position 0, there is 265 * still enough space. 266 */ 267 if (packet_size_in_dwords >= rptr) 268 goto err_no_space; 269 270 /* fill nops, roll back and start at position 0 */ 271 while (wptr > 0) { 272 queue_address[wptr] = kq->nop_packet; 273 wptr = (wptr + 1) % queue_size_dwords; 274 wptr64++; 275 } 276 } 277 278 *buffer_ptr = &queue_address[wptr]; 279 kq->pending_wptr = wptr + packet_size_in_dwords; 280 kq->pending_wptr64 = wptr64 + packet_size_in_dwords; 281 282 return 0; 283 284 err_no_space: 285 *buffer_ptr = NULL; 286 return -ENOMEM; 287 } 288 289 int kq_submit_packet(struct kernel_queue *kq) 290 { 291 #ifdef DEBUG 292 int i; 293 294 for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) { 295 pr_debug("0x%2X ", kq->pq_kernel_addr[i]); 296 if (i % 15 == 0) 297 pr_debug("\n"); 298 } 299 pr_debug("\n"); 300 #endif 301 /* Fatal err detected, packet submission won't go through */ 302 if (amdgpu_amdkfd_is_fed(kq->dev->adev)) 303 return -EIO; 304 305 if (kq->dev->kfd->device_info.doorbell_size == 8) { 306 *kq->wptr64_kernel = kq->pending_wptr64; 307 write_kernel_doorbell64(kq->queue->properties.doorbell_ptr, 308 kq->pending_wptr64); 309 } else { 310 *kq->wptr_kernel = kq->pending_wptr; 311 write_kernel_doorbell(kq->queue->properties.doorbell_ptr, 312 kq->pending_wptr); 313 } 314 315 return 0; 316 } 317 318 void kq_rollback_packet(struct kernel_queue *kq) 319 { 320 if (kq->dev->kfd->device_info.doorbell_size == 8) { 321 kq->pending_wptr64 = *kq->wptr64_kernel; 322 kq->pending_wptr = *kq->wptr_kernel % 323 (kq->queue->properties.queue_size / 4); 324 } else { 325 kq->pending_wptr = *kq->wptr_kernel; 326 } 327 } 328 329 struct kernel_queue *kernel_queue_init(struct kfd_node *dev, 330 enum kfd_queue_type type) 331 { 332 struct kernel_queue *kq; 333 334 kq = kzalloc(sizeof(*kq), GFP_KERNEL); 335 if (!kq) 336 return NULL; 337 338 if (kq_initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE)) 339 return kq; 340 341 pr_err("Failed to init kernel queue\n"); 342 343 kfree(kq); 344 return NULL; 345 } 346 347 void kernel_queue_uninit(struct kernel_queue *kq, bool hanging) 348 { 349 kq_uninitialize(kq, hanging); 350 kfree(kq); 351 } 352 353 /* FIXME: Can this test be removed? */ 354 static __attribute__((unused)) void test_kq(struct kfd_node *dev) 355 { 356 struct kernel_queue *kq; 357 uint32_t *buffer, i; 358 int retval; 359 360 pr_err("Starting kernel queue test\n"); 361 362 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ); 363 if (unlikely(!kq)) { 364 pr_err(" Failed to initialize HIQ\n"); 365 pr_err("Kernel queue test failed\n"); 366 return; 367 } 368 369 retval = kq_acquire_packet_buffer(kq, 5, &buffer); 370 if (unlikely(retval != 0)) { 371 pr_err(" Failed to acquire packet buffer\n"); 372 pr_err("Kernel queue test failed\n"); 373 return; 374 } 375 for (i = 0; i < 5; i++) 376 buffer[i] = kq->nop_packet; 377 kq_submit_packet(kq); 378 379 pr_err("Ending kernel queue test\n"); 380 } 381 382 383