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