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