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