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