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/types.h> 25 #include <linux/mutex.h> 26 #include <linux/slab.h> 27 #include <linux/printk.h> 28 #include <linux/sched.h> 29 #include "kfd_kernel_queue.h" 30 #include "kfd_priv.h" 31 #include "kfd_device_queue_manager.h" 32 #include "kfd_pm4_headers.h" 33 #include "kfd_pm4_opcodes.h" 34 35 #define PM4_COUNT_ZERO (((1 << 15) - 1) << 16) 36 37 static bool initialize(struct kernel_queue *kq, struct kfd_dev *dev, 38 enum kfd_queue_type type, unsigned int queue_size) 39 { 40 struct queue_properties prop; 41 int retval; 42 union PM4_MES_TYPE_3_HEADER nop; 43 44 BUG_ON(!kq || !dev); 45 BUG_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ); 46 47 pr_debug("amdkfd: In func %s initializing queue type %d size %d\n", 48 __func__, KFD_QUEUE_TYPE_HIQ, queue_size); 49 50 nop.opcode = IT_NOP; 51 nop.type = PM4_TYPE_3; 52 nop.u32all |= PM4_COUNT_ZERO; 53 54 kq->dev = dev; 55 kq->nop_packet = nop.u32all; 56 switch (type) { 57 case KFD_QUEUE_TYPE_DIQ: 58 case KFD_QUEUE_TYPE_HIQ: 59 kq->mqd = dev->dqm->ops.get_mqd_manager(dev->dqm, 60 KFD_MQD_TYPE_HIQ); 61 break; 62 default: 63 BUG(); 64 break; 65 } 66 67 if (kq->mqd == NULL) 68 return false; 69 70 prop.doorbell_ptr = kfd_get_kernel_doorbell(dev, &prop.doorbell_off); 71 72 if (prop.doorbell_ptr == NULL) { 73 pr_err("amdkfd: error init doorbell"); 74 goto err_get_kernel_doorbell; 75 } 76 77 retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq); 78 if (retval != 0) { 79 pr_err("amdkfd: error init pq queues size (%d)\n", queue_size); 80 goto err_pq_allocate_vidmem; 81 } 82 83 kq->pq_kernel_addr = kq->pq->cpu_ptr; 84 kq->pq_gpu_addr = kq->pq->gpu_addr; 85 86 retval = kq->ops_asic_specific.initialize(kq, dev, type, queue_size); 87 if (retval == false) 88 goto err_eop_allocate_vidmem; 89 90 retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel), 91 &kq->rptr_mem); 92 93 if (retval != 0) 94 goto err_rptr_allocate_vidmem; 95 96 kq->rptr_kernel = kq->rptr_mem->cpu_ptr; 97 kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr; 98 99 retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->wptr_kernel), 100 &kq->wptr_mem); 101 102 if (retval != 0) 103 goto err_wptr_allocate_vidmem; 104 105 kq->wptr_kernel = kq->wptr_mem->cpu_ptr; 106 kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr; 107 108 memset(kq->pq_kernel_addr, 0, queue_size); 109 memset(kq->rptr_kernel, 0, sizeof(*kq->rptr_kernel)); 110 memset(kq->wptr_kernel, 0, sizeof(*kq->wptr_kernel)); 111 112 prop.queue_size = queue_size; 113 prop.is_interop = false; 114 prop.priority = 1; 115 prop.queue_percent = 100; 116 prop.type = type; 117 prop.vmid = 0; 118 prop.queue_address = kq->pq_gpu_addr; 119 prop.read_ptr = (uint32_t *) kq->rptr_gpu_addr; 120 prop.write_ptr = (uint32_t *) kq->wptr_gpu_addr; 121 prop.eop_ring_buffer_address = kq->eop_gpu_addr; 122 prop.eop_ring_buffer_size = PAGE_SIZE; 123 124 if (init_queue(&kq->queue, prop) != 0) 125 goto err_init_queue; 126 127 kq->queue->device = dev; 128 kq->queue->process = kfd_get_process(current); 129 130 retval = kq->mqd->init_mqd(kq->mqd, &kq->queue->mqd, 131 &kq->queue->mqd_mem_obj, 132 &kq->queue->gart_mqd_addr, 133 &kq->queue->properties); 134 if (retval != 0) 135 goto err_init_mqd; 136 137 /* assign HIQ to HQD */ 138 if (type == KFD_QUEUE_TYPE_HIQ) { 139 pr_debug("assigning hiq to hqd\n"); 140 kq->queue->pipe = KFD_CIK_HIQ_PIPE; 141 kq->queue->queue = KFD_CIK_HIQ_QUEUE; 142 kq->mqd->load_mqd(kq->mqd, kq->queue->mqd, kq->queue->pipe, 143 kq->queue->queue, NULL); 144 } else { 145 /* allocate fence for DIQ */ 146 147 retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t), 148 &kq->fence_mem_obj); 149 150 if (retval != 0) 151 goto err_alloc_fence; 152 153 kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr; 154 kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr; 155 } 156 157 print_queue(kq->queue); 158 159 return true; 160 err_alloc_fence: 161 err_init_mqd: 162 uninit_queue(kq->queue); 163 err_init_queue: 164 kfd_gtt_sa_free(dev, kq->wptr_mem); 165 err_wptr_allocate_vidmem: 166 kfd_gtt_sa_free(dev, kq->rptr_mem); 167 err_rptr_allocate_vidmem: 168 kfd_gtt_sa_free(dev, kq->eop_mem); 169 err_eop_allocate_vidmem: 170 kfd_gtt_sa_free(dev, kq->pq); 171 err_pq_allocate_vidmem: 172 kfd_release_kernel_doorbell(dev, prop.doorbell_ptr); 173 err_get_kernel_doorbell: 174 return false; 175 176 } 177 178 static void uninitialize(struct kernel_queue *kq) 179 { 180 BUG_ON(!kq); 181 182 if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ) 183 kq->mqd->destroy_mqd(kq->mqd, 184 NULL, 185 false, 186 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS, 187 kq->queue->pipe, 188 kq->queue->queue); 189 else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ) 190 kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj); 191 192 kq->mqd->uninit_mqd(kq->mqd, kq->queue->mqd, kq->queue->mqd_mem_obj); 193 194 kfd_gtt_sa_free(kq->dev, kq->rptr_mem); 195 kfd_gtt_sa_free(kq->dev, kq->wptr_mem); 196 kq->ops_asic_specific.uninitialize(kq); 197 kfd_gtt_sa_free(kq->dev, kq->pq); 198 kfd_release_kernel_doorbell(kq->dev, 199 kq->queue->properties.doorbell_ptr); 200 uninit_queue(kq->queue); 201 } 202 203 static int acquire_packet_buffer(struct kernel_queue *kq, 204 size_t packet_size_in_dwords, unsigned int **buffer_ptr) 205 { 206 size_t available_size; 207 size_t queue_size_dwords; 208 uint32_t wptr, rptr; 209 unsigned int *queue_address; 210 211 BUG_ON(!kq || !buffer_ptr); 212 213 rptr = *kq->rptr_kernel; 214 wptr = *kq->wptr_kernel; 215 queue_address = (unsigned int *)kq->pq_kernel_addr; 216 queue_size_dwords = kq->queue->properties.queue_size / sizeof(uint32_t); 217 218 pr_debug("amdkfd: In func %s\nrptr: %d\nwptr: %d\nqueue_address 0x%p\n", 219 __func__, rptr, wptr, queue_address); 220 221 available_size = (rptr - 1 - wptr + queue_size_dwords) % 222 queue_size_dwords; 223 224 if (packet_size_in_dwords >= queue_size_dwords || 225 packet_size_in_dwords >= available_size) { 226 /* 227 * make sure calling functions know 228 * acquire_packet_buffer() failed 229 */ 230 *buffer_ptr = NULL; 231 return -ENOMEM; 232 } 233 234 if (wptr + packet_size_in_dwords >= queue_size_dwords) { 235 while (wptr > 0) { 236 queue_address[wptr] = kq->nop_packet; 237 wptr = (wptr + 1) % queue_size_dwords; 238 } 239 } 240 241 *buffer_ptr = &queue_address[wptr]; 242 kq->pending_wptr = wptr + packet_size_in_dwords; 243 244 return 0; 245 } 246 247 static void submit_packet(struct kernel_queue *kq) 248 { 249 #ifdef DEBUG 250 int i; 251 #endif 252 253 BUG_ON(!kq); 254 255 #ifdef DEBUG 256 for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) { 257 pr_debug("0x%2X ", kq->pq_kernel_addr[i]); 258 if (i % 15 == 0) 259 pr_debug("\n"); 260 } 261 pr_debug("\n"); 262 #endif 263 264 *kq->wptr_kernel = kq->pending_wptr; 265 write_kernel_doorbell(kq->queue->properties.doorbell_ptr, 266 kq->pending_wptr); 267 } 268 269 static void rollback_packet(struct kernel_queue *kq) 270 { 271 BUG_ON(!kq); 272 kq->pending_wptr = *kq->queue->properties.write_ptr; 273 } 274 275 struct kernel_queue *kernel_queue_init(struct kfd_dev *dev, 276 enum kfd_queue_type type) 277 { 278 struct kernel_queue *kq; 279 280 BUG_ON(!dev); 281 282 kq = kzalloc(sizeof(struct kernel_queue), GFP_KERNEL); 283 if (!kq) 284 return NULL; 285 286 kq->ops.initialize = initialize; 287 kq->ops.uninitialize = uninitialize; 288 kq->ops.acquire_packet_buffer = acquire_packet_buffer; 289 kq->ops.submit_packet = submit_packet; 290 kq->ops.rollback_packet = rollback_packet; 291 292 switch (dev->device_info->asic_family) { 293 case CHIP_CARRIZO: 294 kernel_queue_init_vi(&kq->ops_asic_specific); 295 break; 296 297 case CHIP_KAVERI: 298 kernel_queue_init_cik(&kq->ops_asic_specific); 299 break; 300 } 301 302 if (kq->ops.initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE) == false) { 303 pr_err("amdkfd: failed to init kernel queue\n"); 304 kfree(kq); 305 return NULL; 306 } 307 return kq; 308 } 309 310 void kernel_queue_uninit(struct kernel_queue *kq) 311 { 312 BUG_ON(!kq); 313 314 kq->ops.uninitialize(kq); 315 kfree(kq); 316 } 317 318 static __attribute__((unused)) void test_kq(struct kfd_dev *dev) 319 { 320 struct kernel_queue *kq; 321 uint32_t *buffer, i; 322 int retval; 323 324 BUG_ON(!dev); 325 326 pr_err("amdkfd: starting kernel queue test\n"); 327 328 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ); 329 BUG_ON(!kq); 330 331 retval = kq->ops.acquire_packet_buffer(kq, 5, &buffer); 332 BUG_ON(retval != 0); 333 for (i = 0; i < 5; i++) 334 buffer[i] = kq->nop_packet; 335 kq->ops.submit_packet(kq); 336 337 pr_err("amdkfd: ending kernel queue test\n"); 338 } 339 340 341