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