1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
37static 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 kq->queue->mqd_mem_obj = kq->mqd_mgr->allocate_mqd(kq->mqd_mgr->dev,
136 &kq->queue->properties);
137 if (!kq->queue->mqd_mem_obj)
138 goto err_allocate_mqd;
139 kq->mqd_mgr->init_mqd(kq->mqd_mgr, &kq->queue->mqd,
140 kq->queue->mqd_mem_obj,
141 &kq->queue->gart_mqd_addr,
142 &kq->queue->properties);
143
144 if (type == KFD_QUEUE_TYPE_HIQ) {
145 pr_debug("Assigning hiq to hqd\n");
146 kq->queue->pipe = KFD_CIK_HIQ_PIPE;
147 kq->queue->queue = KFD_CIK_HIQ_QUEUE;
148 kq->mqd_mgr->load_mqd(kq->mqd_mgr, kq->queue->mqd,
149 kq->queue->pipe, kq->queue->queue,
150 &kq->queue->properties, NULL);
151 } else {
152
153
154 retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t),
155 &kq->fence_mem_obj);
156
157 if (retval != 0)
158 goto err_alloc_fence;
159
160 kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr;
161 kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr;
162 }
163
164 print_queue(kq->queue);
165
166 return true;
167err_alloc_fence:
168 kq->mqd_mgr->free_mqd(kq->mqd_mgr, kq->queue->mqd, kq->queue->mqd_mem_obj);
169err_allocate_mqd:
170 uninit_queue(kq->queue);
171err_init_queue:
172 kfd_gtt_sa_free(dev, kq->wptr_mem);
173err_wptr_allocate_vidmem:
174 kfd_gtt_sa_free(dev, kq->rptr_mem);
175err_rptr_allocate_vidmem:
176 kfd_gtt_sa_free(dev, kq->eop_mem);
177err_eop_allocate_vidmem:
178 kfd_gtt_sa_free(dev, kq->pq);
179err_pq_allocate_vidmem:
180 kfd_release_kernel_doorbell(dev, prop.doorbell_ptr);
181err_get_kernel_doorbell:
182 return false;
183
184}
185
186static void uninitialize(struct kernel_queue *kq)
187{
188 if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ)
189 kq->mqd_mgr->destroy_mqd(kq->mqd_mgr,
190 kq->queue->mqd,
191 KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
192 KFD_UNMAP_LATENCY_MS,
193 kq->queue->pipe,
194 kq->queue->queue);
195 else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ)
196 kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj);
197
198 kq->mqd_mgr->free_mqd(kq->mqd_mgr, kq->queue->mqd,
199 kq->queue->mqd_mem_obj);
200
201 kfd_gtt_sa_free(kq->dev, kq->rptr_mem);
202 kfd_gtt_sa_free(kq->dev, kq->wptr_mem);
203 kq->ops_asic_specific.uninitialize(kq);
204 kfd_gtt_sa_free(kq->dev, kq->pq);
205 kfd_release_kernel_doorbell(kq->dev,
206 kq->queue->properties.doorbell_ptr);
207 uninit_queue(kq->queue);
208}
209
210static int acquire_packet_buffer(struct kernel_queue *kq,
211 size_t packet_size_in_dwords, unsigned int **buffer_ptr)
212{
213 size_t available_size;
214 size_t queue_size_dwords;
215 uint32_t wptr, rptr;
216 uint64_t wptr64;
217 unsigned int *queue_address;
218
219
220
221
222
223
224 rptr = *kq->rptr_kernel;
225 wptr = kq->pending_wptr;
226 wptr64 = kq->pending_wptr64;
227 queue_address = (unsigned int *)kq->pq_kernel_addr;
228 queue_size_dwords = kq->queue->properties.queue_size / 4;
229
230 pr_debug("rptr: %d\n", rptr);
231 pr_debug("wptr: %d\n", wptr);
232 pr_debug("queue_address 0x%p\n", queue_address);
233
234 available_size = (rptr + queue_size_dwords - 1 - wptr) %
235 queue_size_dwords;
236
237 if (packet_size_in_dwords > available_size) {
238
239
240
241
242 goto err_no_space;
243 }
244
245 if (wptr + packet_size_in_dwords >= queue_size_dwords) {
246
247
248
249 if (packet_size_in_dwords >= rptr)
250 goto err_no_space;
251
252
253 while (wptr > 0) {
254 queue_address[wptr] = kq->nop_packet;
255 wptr = (wptr + 1) % queue_size_dwords;
256 wptr64++;
257 }
258 }
259
260 *buffer_ptr = &queue_address[wptr];
261 kq->pending_wptr = wptr + packet_size_in_dwords;
262 kq->pending_wptr64 = wptr64 + packet_size_in_dwords;
263
264 return 0;
265
266err_no_space:
267 *buffer_ptr = NULL;
268 return -ENOMEM;
269}
270
271static void submit_packet(struct kernel_queue *kq)
272{
273#ifdef DEBUG
274 int i;
275
276 for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) {
277 pr_debug("0x%2X ", kq->pq_kernel_addr[i]);
278 if (i % 15 == 0)
279 pr_debug("\n");
280 }
281 pr_debug("\n");
282#endif
283
284 kq->ops_asic_specific.submit_packet(kq);
285}
286
287static void rollback_packet(struct kernel_queue *kq)
288{
289 if (kq->dev->device_info->doorbell_size == 8) {
290 kq->pending_wptr64 = *kq->wptr64_kernel;
291 kq->pending_wptr = *kq->wptr_kernel %
292 (kq->queue->properties.queue_size / 4);
293 } else {
294 kq->pending_wptr = *kq->wptr_kernel;
295 }
296}
297
298struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
299 enum kfd_queue_type type)
300{
301 struct kernel_queue *kq;
302
303 kq = kzalloc(sizeof(*kq), GFP_KERNEL);
304 if (!kq)
305 return NULL;
306
307 kq->ops.initialize = initialize;
308 kq->ops.uninitialize = uninitialize;
309 kq->ops.acquire_packet_buffer = acquire_packet_buffer;
310 kq->ops.submit_packet = submit_packet;
311 kq->ops.rollback_packet = rollback_packet;
312
313 switch (dev->device_info->asic_family) {
314 case CHIP_CARRIZO:
315 case CHIP_TONGA:
316 case CHIP_FIJI:
317 case CHIP_POLARIS10:
318 case CHIP_POLARIS11:
319 case CHIP_POLARIS12:
320 case CHIP_VEGAM:
321 kernel_queue_init_vi(&kq->ops_asic_specific);
322 break;
323
324 case CHIP_KAVERI:
325 case CHIP_HAWAII:
326 kernel_queue_init_cik(&kq->ops_asic_specific);
327 break;
328
329 case CHIP_VEGA10:
330 case CHIP_VEGA12:
331 case CHIP_VEGA20:
332 case CHIP_RAVEN:
333 kernel_queue_init_v9(&kq->ops_asic_specific);
334 break;
335 case CHIP_NAVI10:
336 kernel_queue_init_v10(&kq->ops_asic_specific);
337 break;
338 default:
339 WARN(1, "Unexpected ASIC family %u",
340 dev->device_info->asic_family);
341 goto out_free;
342 }
343
344 if (kq->ops.initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE))
345 return kq;
346
347 pr_err("Failed to init kernel queue\n");
348
349out_free:
350 kfree(kq);
351 return NULL;
352}
353
354void kernel_queue_uninit(struct kernel_queue *kq)
355{
356 kq->ops.uninitialize(kq);
357 kfree(kq);
358}
359
360
361static __attribute__((unused)) void test_kq(struct kfd_dev *dev)
362{
363 struct kernel_queue *kq;
364 uint32_t *buffer, i;
365 int retval;
366
367 pr_err("Starting kernel queue test\n");
368
369 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ);
370 if (unlikely(!kq)) {
371 pr_err(" Failed to initialize HIQ\n");
372 pr_err("Kernel queue test failed\n");
373 return;
374 }
375
376 retval = kq->ops.acquire_packet_buffer(kq, 5, &buffer);
377 if (unlikely(retval != 0)) {
378 pr_err(" Failed to acquire packet buffer\n");
379 pr_err("Kernel queue test failed\n");
380 return;
381 }
382 for (i = 0; i < 5; i++)
383 buffer[i] = kq->nop_packet;
384 kq->ops.submit_packet(kq);
385
386 pr_err("Ending kernel queue test\n");
387}
388
389
390