drm/amdkfd: Encapsulate KQ functions in ops structure
[deliverable/linux.git] / drivers / gpu / drm / amd / amdkfd / kfd_kernel_queue.c
CommitLineData
ed6e6a34
BG
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>
9a5634a7 28#include <linux/sched.h>
ed6e6a34
BG
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 BUG_ON(!kq || !dev);
45 BUG_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ);
46
47 pr_debug("kfd: In func %s initializing queue type %d size %d\n",
48 __func__, KFD_QUEUE_TYPE_HIQ, queue_size);
49
50 nop.opcode = IT_NOP;
51 nop.type = PM4_TYPE_3;
52 nop.u32all |= PM4_COUNT_ZERO;
53
54 kq->dev = dev;
55 kq->nop_packet = nop.u32all;
56 switch (type) {
57 case KFD_QUEUE_TYPE_DIQ:
58 case KFD_QUEUE_TYPE_HIQ:
45c9a5e4 59 kq->mqd = dev->dqm->ops.get_mqd_manager(dev->dqm,
85d258f9 60 KFD_MQD_TYPE_HIQ);
ed6e6a34
BG
61 break;
62 default:
63 BUG();
64 break;
65 }
66
67 if (kq->mqd == NULL)
68 return false;
69
5cd78de5 70 prop.doorbell_ptr = kfd_get_kernel_doorbell(dev, &prop.doorbell_off);
ed6e6a34
BG
71
72 if (prop.doorbell_ptr == NULL)
73 goto err_get_kernel_doorbell;
74
a86aa3ca 75 retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq);
ed6e6a34
BG
76
77 if (retval != 0)
78 goto err_pq_allocate_vidmem;
79
80 kq->pq_kernel_addr = kq->pq->cpu_ptr;
81 kq->pq_gpu_addr = kq->pq->gpu_addr;
82
a86aa3ca
OG
83 retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel),
84 &kq->rptr_mem);
ed6e6a34
BG
85
86 if (retval != 0)
87 goto err_rptr_allocate_vidmem;
88
89 kq->rptr_kernel = kq->rptr_mem->cpu_ptr;
90 kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr;
91
a86aa3ca
OG
92 retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->wptr_kernel),
93 &kq->wptr_mem);
ed6e6a34
BG
94
95 if (retval != 0)
96 goto err_wptr_allocate_vidmem;
97
98 kq->wptr_kernel = kq->wptr_mem->cpu_ptr;
99 kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr;
100
101 memset(kq->pq_kernel_addr, 0, queue_size);
102 memset(kq->rptr_kernel, 0, sizeof(*kq->rptr_kernel));
103 memset(kq->wptr_kernel, 0, sizeof(*kq->wptr_kernel));
104
105 prop.queue_size = queue_size;
106 prop.is_interop = false;
107 prop.priority = 1;
108 prop.queue_percent = 100;
109 prop.type = type;
110 prop.vmid = 0;
111 prop.queue_address = kq->pq_gpu_addr;
112 prop.read_ptr = (uint32_t *) kq->rptr_gpu_addr;
113 prop.write_ptr = (uint32_t *) kq->wptr_gpu_addr;
114
115 if (init_queue(&kq->queue, prop) != 0)
116 goto err_init_queue;
117
118 kq->queue->device = dev;
119 kq->queue->process = kfd_get_process(current);
120
121 retval = kq->mqd->init_mqd(kq->mqd, &kq->queue->mqd,
122 &kq->queue->mqd_mem_obj,
123 &kq->queue->gart_mqd_addr,
124 &kq->queue->properties);
125 if (retval != 0)
126 goto err_init_mqd;
127
128 /* assign HIQ to HQD */
129 if (type == KFD_QUEUE_TYPE_HIQ) {
130 pr_debug("assigning hiq to hqd\n");
131 kq->queue->pipe = KFD_CIK_HIQ_PIPE;
132 kq->queue->queue = KFD_CIK_HIQ_QUEUE;
133 kq->mqd->load_mqd(kq->mqd, kq->queue->mqd, kq->queue->pipe,
134 kq->queue->queue, NULL);
135 } else {
136 /* allocate fence for DIQ */
137
a86aa3ca
OG
138 retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t),
139 &kq->fence_mem_obj);
ed6e6a34
BG
140
141 if (retval != 0)
142 goto err_alloc_fence;
143
144 kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr;
145 kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr;
146 }
147
148 print_queue(kq->queue);
149
150 return true;
151err_alloc_fence:
152err_init_mqd:
153 uninit_queue(kq->queue);
154err_init_queue:
a86aa3ca 155 kfd_gtt_sa_free(dev, kq->wptr_mem);
ed6e6a34 156err_wptr_allocate_vidmem:
a86aa3ca 157 kfd_gtt_sa_free(dev, kq->rptr_mem);
ed6e6a34 158err_rptr_allocate_vidmem:
a86aa3ca 159 kfd_gtt_sa_free(dev, kq->pq);
ed6e6a34
BG
160err_pq_allocate_vidmem:
161 pr_err("kfd: error init pq\n");
5cd78de5 162 kfd_release_kernel_doorbell(dev, prop.doorbell_ptr);
ed6e6a34
BG
163err_get_kernel_doorbell:
164 pr_err("kfd: error init doorbell");
165 return false;
166
167}
168
169static void uninitialize(struct kernel_queue *kq)
170{
171 BUG_ON(!kq);
172
173 if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ)
174 kq->mqd->destroy_mqd(kq->mqd,
175 NULL,
176 false,
177 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS,
178 kq->queue->pipe,
179 kq->queue->queue);
a86aa3ca
OG
180 else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ)
181 kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj);
ed6e6a34 182
a86aa3ca
OG
183 kfd_gtt_sa_free(kq->dev, kq->rptr_mem);
184 kfd_gtt_sa_free(kq->dev, kq->wptr_mem);
185 kfd_gtt_sa_free(kq->dev, kq->pq);
ed6e6a34 186 kfd_release_kernel_doorbell(kq->dev,
5cd78de5 187 kq->queue->properties.doorbell_ptr);
ed6e6a34
BG
188 uninit_queue(kq->queue);
189}
190
191static int acquire_packet_buffer(struct kernel_queue *kq,
192 size_t packet_size_in_dwords, unsigned int **buffer_ptr)
193{
194 size_t available_size;
195 size_t queue_size_dwords;
196 uint32_t wptr, rptr;
197 unsigned int *queue_address;
198
199 BUG_ON(!kq || !buffer_ptr);
200
201 rptr = *kq->rptr_kernel;
202 wptr = *kq->wptr_kernel;
203 queue_address = (unsigned int *)kq->pq_kernel_addr;
204 queue_size_dwords = kq->queue->properties.queue_size / sizeof(uint32_t);
205
206 pr_debug("kfd: In func %s\nrptr: %d\nwptr: %d\nqueue_address 0x%p\n",
207 __func__, rptr, wptr, queue_address);
208
209 available_size = (rptr - 1 - wptr + queue_size_dwords) %
210 queue_size_dwords;
211
212 if (packet_size_in_dwords >= queue_size_dwords ||
a550bb3d
OG
213 packet_size_in_dwords >= available_size) {
214 /*
215 * make sure calling functions know
216 * acquire_packet_buffer() failed
217 */
218 *buffer_ptr = NULL;
ed6e6a34 219 return -ENOMEM;
a550bb3d 220 }
ed6e6a34
BG
221
222 if (wptr + packet_size_in_dwords >= queue_size_dwords) {
223 while (wptr > 0) {
224 queue_address[wptr] = kq->nop_packet;
225 wptr = (wptr + 1) % queue_size_dwords;
226 }
227 }
228
229 *buffer_ptr = &queue_address[wptr];
230 kq->pending_wptr = wptr + packet_size_in_dwords;
231
232 return 0;
233}
234
235static void submit_packet(struct kernel_queue *kq)
236{
237#ifdef DEBUG
238 int i;
239#endif
240
241 BUG_ON(!kq);
242
243#ifdef DEBUG
244 for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) {
245 pr_debug("0x%2X ", kq->pq_kernel_addr[i]);
246 if (i % 15 == 0)
247 pr_debug("\n");
248 }
249 pr_debug("\n");
250#endif
251
252 *kq->wptr_kernel = kq->pending_wptr;
5cd78de5 253 write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
ed6e6a34
BG
254 kq->pending_wptr);
255}
256
257static int sync_with_hw(struct kernel_queue *kq, unsigned long timeout_ms)
258{
259 unsigned long org_timeout_ms;
260
261 BUG_ON(!kq);
262
263 org_timeout_ms = timeout_ms;
264 timeout_ms += jiffies * 1000 / HZ;
265 while (*kq->wptr_kernel != *kq->rptr_kernel) {
266 if (time_after(jiffies * 1000 / HZ, timeout_ms)) {
267 pr_err("kfd: kernel_queue %s timeout expired %lu\n",
268 __func__, org_timeout_ms);
269 pr_err("kfd: wptr: %d rptr: %d\n",
270 *kq->wptr_kernel, *kq->rptr_kernel);
271 return -ETIME;
272 }
9a5634a7 273 schedule();
ed6e6a34
BG
274 }
275
276 return 0;
277}
278
279static void rollback_packet(struct kernel_queue *kq)
280{
281 BUG_ON(!kq);
282 kq->pending_wptr = *kq->queue->properties.write_ptr;
283}
284
285struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
286 enum kfd_queue_type type)
287{
288 struct kernel_queue *kq;
289
290 BUG_ON(!dev);
291
292 kq = kzalloc(sizeof(struct kernel_queue), GFP_KERNEL);
293 if (!kq)
294 return NULL;
295
443fbd5f
OG
296 kq->ops.initialize = initialize;
297 kq->ops.uninitialize = uninitialize;
298 kq->ops.acquire_packet_buffer = acquire_packet_buffer;
299 kq->ops.submit_packet = submit_packet;
300 kq->ops.sync_with_hw = sync_with_hw;
301 kq->ops.rollback_packet = rollback_packet;
302
303 if (kq->ops.initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE) == false) {
ed6e6a34
BG
304 pr_err("kfd: failed to init kernel queue\n");
305 kfree(kq);
306 return NULL;
307 }
308 return kq;
309}
310
311void kernel_queue_uninit(struct kernel_queue *kq)
312{
313 BUG_ON(!kq);
314
443fbd5f 315 kq->ops.uninitialize(kq);
ed6e6a34
BG
316 kfree(kq);
317}
318
5ef360ea 319static __attribute__((unused)) void test_kq(struct kfd_dev *dev)
ed6e6a34
BG
320{
321 struct kernel_queue *kq;
322 uint32_t *buffer, i;
323 int retval;
324
325 BUG_ON(!dev);
326
327 pr_debug("kfd: starting kernel queue test\n");
328
329 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ);
330 BUG_ON(!kq);
331
443fbd5f 332 retval = kq->ops.acquire_packet_buffer(kq, 5, &buffer);
ed6e6a34
BG
333 BUG_ON(retval != 0);
334 for (i = 0; i < 5; i++)
335 buffer[i] = kq->nop_packet;
443fbd5f
OG
336 kq->ops.submit_packet(kq);
337 kq->ops.sync_with_hw(kq, 1000);
ed6e6a34
BG
338
339 pr_debug("kfd: ending kernel queue test\n");
340}
341
342
This page took 0.04676 seconds and 5 git commands to generate.