dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / amd / amdkfd / kfd_kernel_queue.c
blobf1596881f20a473384b6d9f01684e5d1a71bec3a
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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"
35 #define PM4_COUNT_ZERO (((1 << 15) - 1) << 16)
37 static bool initialize(struct kernel_queue *kq, struct kfd_dev *dev,
38 enum kfd_queue_type type, unsigned int queue_size)
40 struct queue_properties prop;
41 int retval;
42 union PM4_MES_TYPE_3_HEADER nop;
44 if (WARN_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ))
45 return false;
47 pr_debug("Initializing queue type %d size %d\n", KFD_QUEUE_TYPE_HIQ,
48 queue_size);
50 memset(&prop, 0, sizeof(prop));
51 memset(&nop, 0, sizeof(nop));
53 nop.opcode = IT_NOP;
54 nop.type = PM4_TYPE_3;
55 nop.u32all |= PM4_COUNT_ZERO;
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_mgr = dev->dqm->ops.get_mqd_manager(dev->dqm,
63 KFD_MQD_TYPE_HIQ);
64 break;
65 default:
66 pr_err("Invalid queue type %d\n", type);
67 return false;
70 if (!kq->mqd_mgr)
71 return false;
73 prop.doorbell_ptr = kfd_get_kernel_doorbell(dev, &prop.doorbell_off);
75 if (!prop.doorbell_ptr) {
76 pr_err("Failed to initialize doorbell");
77 goto err_get_kernel_doorbell;
80 retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq);
81 if (retval != 0) {
82 pr_err("Failed to init pq queues size %d\n", queue_size);
83 goto err_pq_allocate_vidmem;
86 kq->pq_kernel_addr = kq->pq->cpu_ptr;
87 kq->pq_gpu_addr = kq->pq->gpu_addr;
89 retval = kq->ops_asic_specific.initialize(kq, dev, type, queue_size);
90 if (!retval)
91 goto err_eop_allocate_vidmem;
93 retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel),
94 &kq->rptr_mem);
96 if (retval != 0)
97 goto err_rptr_allocate_vidmem;
99 kq->rptr_kernel = kq->rptr_mem->cpu_ptr;
100 kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr;
102 retval = kfd_gtt_sa_allocate(dev, dev->device_info->doorbell_size,
103 &kq->wptr_mem);
105 if (retval != 0)
106 goto err_wptr_allocate_vidmem;
108 kq->wptr_kernel = kq->wptr_mem->cpu_ptr;
109 kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr;
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));
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 prop.cu_mask = NULL;
128 if (init_queue(&kq->queue, &prop) != 0)
129 goto err_init_queue;
131 kq->queue->device = dev;
132 kq->queue->process = kfd_get_process(current);
134 retval = kq->mqd_mgr->init_mqd(kq->mqd_mgr, &kq->queue->mqd,
135 &kq->queue->mqd_mem_obj,
136 &kq->queue->gart_mqd_addr,
137 &kq->queue->properties);
138 if (retval != 0)
139 goto err_init_mqd;
141 /* assign HIQ to HQD */
142 if (type == KFD_QUEUE_TYPE_HIQ) {
143 pr_debug("Assigning hiq to hqd\n");
144 kq->queue->pipe = KFD_CIK_HIQ_PIPE;
145 kq->queue->queue = KFD_CIK_HIQ_QUEUE;
146 kq->mqd_mgr->load_mqd(kq->mqd_mgr, kq->queue->mqd,
147 kq->queue->pipe, kq->queue->queue,
148 &kq->queue->properties, NULL);
149 } else {
150 /* allocate fence for DIQ */
152 retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t),
153 &kq->fence_mem_obj);
155 if (retval != 0)
156 goto err_alloc_fence;
158 kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr;
159 kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr;
162 print_queue(kq->queue);
164 return true;
165 err_alloc_fence:
166 err_init_mqd:
167 uninit_queue(kq->queue);
168 err_init_queue:
169 kfd_gtt_sa_free(dev, kq->wptr_mem);
170 err_wptr_allocate_vidmem:
171 kfd_gtt_sa_free(dev, kq->rptr_mem);
172 err_rptr_allocate_vidmem:
173 kfd_gtt_sa_free(dev, kq->eop_mem);
174 err_eop_allocate_vidmem:
175 kfd_gtt_sa_free(dev, kq->pq);
176 err_pq_allocate_vidmem:
177 kfd_release_kernel_doorbell(dev, prop.doorbell_ptr);
178 err_get_kernel_doorbell:
179 return false;
183 static void uninitialize(struct kernel_queue *kq)
185 if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ)
186 kq->mqd_mgr->destroy_mqd(kq->mqd_mgr,
187 kq->queue->mqd,
188 KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
189 KFD_UNMAP_LATENCY_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);
195 kq->mqd_mgr->uninit_mqd(kq->mqd_mgr, kq->queue->mqd,
196 kq->queue->mqd_mem_obj);
198 kfd_gtt_sa_free(kq->dev, kq->rptr_mem);
199 kfd_gtt_sa_free(kq->dev, kq->wptr_mem);
200 kq->ops_asic_specific.uninitialize(kq);
201 kfd_gtt_sa_free(kq->dev, kq->pq);
202 kfd_release_kernel_doorbell(kq->dev,
203 kq->queue->properties.doorbell_ptr);
204 uninit_queue(kq->queue);
207 static int acquire_packet_buffer(struct kernel_queue *kq,
208 size_t packet_size_in_dwords, unsigned int **buffer_ptr)
210 size_t available_size;
211 size_t queue_size_dwords;
212 uint32_t wptr, rptr;
213 uint64_t wptr64;
214 unsigned int *queue_address;
216 /* When rptr == wptr, the buffer is empty.
217 * When rptr == wptr + 1, the buffer is full.
218 * It is always rptr that advances to the position of wptr, rather than
219 * the opposite. So we can only use up to queue_size_dwords - 1 dwords.
221 rptr = *kq->rptr_kernel;
222 wptr = kq->pending_wptr;
223 wptr64 = kq->pending_wptr64;
224 queue_address = (unsigned int *)kq->pq_kernel_addr;
225 queue_size_dwords = kq->queue->properties.queue_size / 4;
227 pr_debug("rptr: %d\n", rptr);
228 pr_debug("wptr: %d\n", wptr);
229 pr_debug("queue_address 0x%p\n", queue_address);
231 available_size = (rptr + queue_size_dwords - 1 - wptr) %
232 queue_size_dwords;
234 if (packet_size_in_dwords > available_size) {
236 * make sure calling functions know
237 * acquire_packet_buffer() failed
239 goto err_no_space;
242 if (wptr + packet_size_in_dwords >= queue_size_dwords) {
243 /* make sure after rolling back to position 0, there is
244 * still enough space.
246 if (packet_size_in_dwords >= rptr)
247 goto err_no_space;
249 /* fill nops, roll back and start at position 0 */
250 while (wptr > 0) {
251 queue_address[wptr] = kq->nop_packet;
252 wptr = (wptr + 1) % queue_size_dwords;
253 wptr64++;
257 *buffer_ptr = &queue_address[wptr];
258 kq->pending_wptr = wptr + packet_size_in_dwords;
259 kq->pending_wptr64 = wptr64 + packet_size_in_dwords;
261 return 0;
263 err_no_space:
264 *buffer_ptr = NULL;
265 return -ENOMEM;
268 static void submit_packet(struct kernel_queue *kq)
270 #ifdef DEBUG
271 int i;
273 for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) {
274 pr_debug("0x%2X ", kq->pq_kernel_addr[i]);
275 if (i % 15 == 0)
276 pr_debug("\n");
278 pr_debug("\n");
279 #endif
281 kq->ops_asic_specific.submit_packet(kq);
284 static void rollback_packet(struct kernel_queue *kq)
286 if (kq->dev->device_info->doorbell_size == 8) {
287 kq->pending_wptr64 = *kq->wptr64_kernel;
288 kq->pending_wptr = *kq->wptr_kernel %
289 (kq->queue->properties.queue_size / 4);
290 } else {
291 kq->pending_wptr = *kq->wptr_kernel;
295 struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
296 enum kfd_queue_type type)
298 struct kernel_queue *kq;
300 kq = kzalloc(sizeof(*kq), GFP_KERNEL);
301 if (!kq)
302 return NULL;
304 kq->ops.initialize = initialize;
305 kq->ops.uninitialize = uninitialize;
306 kq->ops.acquire_packet_buffer = acquire_packet_buffer;
307 kq->ops.submit_packet = submit_packet;
308 kq->ops.rollback_packet = rollback_packet;
310 switch (dev->device_info->asic_family) {
311 case CHIP_CARRIZO:
312 case CHIP_TONGA:
313 case CHIP_FIJI:
314 case CHIP_POLARIS10:
315 case CHIP_POLARIS11:
316 case CHIP_POLARIS12:
317 kernel_queue_init_vi(&kq->ops_asic_specific);
318 break;
320 case CHIP_KAVERI:
321 case CHIP_HAWAII:
322 kernel_queue_init_cik(&kq->ops_asic_specific);
323 break;
325 case CHIP_VEGA10:
326 case CHIP_VEGA12:
327 case CHIP_VEGA20:
328 case CHIP_RAVEN:
329 kernel_queue_init_v9(&kq->ops_asic_specific);
330 break;
331 default:
332 WARN(1, "Unexpected ASIC family %u",
333 dev->device_info->asic_family);
334 goto out_free;
337 if (kq->ops.initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE))
338 return kq;
340 pr_err("Failed to init kernel queue\n");
342 out_free:
343 kfree(kq);
344 return NULL;
347 void kernel_queue_uninit(struct kernel_queue *kq)
349 kq->ops.uninitialize(kq);
350 kfree(kq);
353 /* FIXME: Can this test be removed? */
354 static __attribute__((unused)) void test_kq(struct kfd_dev *dev)
356 struct kernel_queue *kq;
357 uint32_t *buffer, i;
358 int retval;
360 pr_err("Starting kernel queue test\n");
362 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ);
363 if (unlikely(!kq)) {
364 pr_err(" Failed to initialize HIQ\n");
365 pr_err("Kernel queue test failed\n");
366 return;
369 retval = kq->ops.acquire_packet_buffer(kq, 5, &buffer);
370 if (unlikely(retval != 0)) {
371 pr_err(" Failed to acquire packet buffer\n");
372 pr_err("Kernel queue test failed\n");
373 return;
375 for (i = 0; i < 5; i++)
376 buffer[i] = kq->nop_packet;
377 kq->ops.submit_packet(kq);
379 pr_err("Ending kernel queue test\n");