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