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