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"
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 /* Initialize a kernel queue, including allocations of GART memory
38 * needed for the queue.
40 static bool kq_initialize(struct kernel_queue
*kq
, struct kfd_dev
*dev
,
41 enum kfd_queue_type type
, unsigned int queue_size
)
43 struct queue_properties prop
;
45 union PM4_MES_TYPE_3_HEADER nop
;
47 if (WARN_ON(type
!= KFD_QUEUE_TYPE_DIQ
&& type
!= KFD_QUEUE_TYPE_HIQ
))
50 pr_debug("Initializing queue type %d size %d\n", KFD_QUEUE_TYPE_HIQ
,
53 memset(&prop
, 0, sizeof(prop
));
54 memset(&nop
, 0, sizeof(nop
));
57 nop
.type
= PM4_TYPE_3
;
58 nop
.u32all
|= PM4_COUNT_ZERO
;
61 kq
->nop_packet
= nop
.u32all
;
63 case KFD_QUEUE_TYPE_DIQ
:
64 kq
->mqd_mgr
= dev
->dqm
->mqd_mgrs
[KFD_MQD_TYPE_DIQ
];
66 case KFD_QUEUE_TYPE_HIQ
:
67 kq
->mqd_mgr
= dev
->dqm
->mqd_mgrs
[KFD_MQD_TYPE_HIQ
];
70 pr_err("Invalid queue type %d\n", type
);
77 prop
.doorbell_ptr
= kfd_get_kernel_doorbell(dev
, &prop
.doorbell_off
);
79 if (!prop
.doorbell_ptr
) {
80 pr_err("Failed to initialize doorbell");
81 goto err_get_kernel_doorbell
;
84 retval
= kfd_gtt_sa_allocate(dev
, queue_size
, &kq
->pq
);
86 pr_err("Failed to init pq queues size %d\n", queue_size
);
87 goto err_pq_allocate_vidmem
;
90 kq
->pq_kernel_addr
= kq
->pq
->cpu_ptr
;
91 kq
->pq_gpu_addr
= kq
->pq
->gpu_addr
;
93 /* For CIK family asics, kq->eop_mem is not needed */
94 if (dev
->device_info
->asic_family
> CHIP_MULLINS
) {
95 retval
= kfd_gtt_sa_allocate(dev
, PAGE_SIZE
, &kq
->eop_mem
);
97 goto err_eop_allocate_vidmem
;
99 kq
->eop_gpu_addr
= kq
->eop_mem
->gpu_addr
;
100 kq
->eop_kernel_addr
= kq
->eop_mem
->cpu_ptr
;
102 memset(kq
->eop_kernel_addr
, 0, PAGE_SIZE
);
105 retval
= kfd_gtt_sa_allocate(dev
, sizeof(*kq
->rptr_kernel
),
109 goto err_rptr_allocate_vidmem
;
111 kq
->rptr_kernel
= kq
->rptr_mem
->cpu_ptr
;
112 kq
->rptr_gpu_addr
= kq
->rptr_mem
->gpu_addr
;
114 retval
= kfd_gtt_sa_allocate(dev
, dev
->device_info
->doorbell_size
,
118 goto err_wptr_allocate_vidmem
;
120 kq
->wptr_kernel
= kq
->wptr_mem
->cpu_ptr
;
121 kq
->wptr_gpu_addr
= kq
->wptr_mem
->gpu_addr
;
123 memset(kq
->pq_kernel_addr
, 0, queue_size
);
124 memset(kq
->rptr_kernel
, 0, sizeof(*kq
->rptr_kernel
));
125 memset(kq
->wptr_kernel
, 0, sizeof(*kq
->wptr_kernel
));
127 prop
.queue_size
= queue_size
;
128 prop
.is_interop
= false;
130 prop
.queue_percent
= 100;
133 prop
.queue_address
= kq
->pq_gpu_addr
;
134 prop
.read_ptr
= (uint32_t *) kq
->rptr_gpu_addr
;
135 prop
.write_ptr
= (uint32_t *) kq
->wptr_gpu_addr
;
136 prop
.eop_ring_buffer_address
= kq
->eop_gpu_addr
;
137 prop
.eop_ring_buffer_size
= PAGE_SIZE
;
140 if (init_queue(&kq
->queue
, &prop
) != 0)
143 kq
->queue
->device
= dev
;
144 kq
->queue
->process
= kfd_get_process(current
);
146 kq
->queue
->mqd_mem_obj
= kq
->mqd_mgr
->allocate_mqd(kq
->mqd_mgr
->dev
,
147 &kq
->queue
->properties
);
148 if (!kq
->queue
->mqd_mem_obj
)
149 goto err_allocate_mqd
;
150 kq
->mqd_mgr
->init_mqd(kq
->mqd_mgr
, &kq
->queue
->mqd
,
151 kq
->queue
->mqd_mem_obj
,
152 &kq
->queue
->gart_mqd_addr
,
153 &kq
->queue
->properties
);
154 /* assign HIQ to HQD */
155 if (type
== KFD_QUEUE_TYPE_HIQ
) {
156 pr_debug("Assigning hiq to hqd\n");
157 kq
->queue
->pipe
= KFD_CIK_HIQ_PIPE
;
158 kq
->queue
->queue
= KFD_CIK_HIQ_QUEUE
;
159 kq
->mqd_mgr
->load_mqd(kq
->mqd_mgr
, kq
->queue
->mqd
,
160 kq
->queue
->pipe
, kq
->queue
->queue
,
161 &kq
->queue
->properties
, NULL
);
163 /* allocate fence for DIQ */
165 retval
= kfd_gtt_sa_allocate(dev
, sizeof(uint32_t),
169 goto err_alloc_fence
;
171 kq
->fence_kernel_address
= kq
->fence_mem_obj
->cpu_ptr
;
172 kq
->fence_gpu_addr
= kq
->fence_mem_obj
->gpu_addr
;
175 print_queue(kq
->queue
);
179 kq
->mqd_mgr
->free_mqd(kq
->mqd_mgr
, kq
->queue
->mqd
, kq
->queue
->mqd_mem_obj
);
181 uninit_queue(kq
->queue
);
183 kfd_gtt_sa_free(dev
, kq
->wptr_mem
);
184 err_wptr_allocate_vidmem
:
185 kfd_gtt_sa_free(dev
, kq
->rptr_mem
);
186 err_rptr_allocate_vidmem
:
187 kfd_gtt_sa_free(dev
, kq
->eop_mem
);
188 err_eop_allocate_vidmem
:
189 kfd_gtt_sa_free(dev
, kq
->pq
);
190 err_pq_allocate_vidmem
:
191 kfd_release_kernel_doorbell(dev
, prop
.doorbell_ptr
);
192 err_get_kernel_doorbell
:
197 /* Uninitialize a kernel queue and free all its memory usages. */
198 static void kq_uninitialize(struct kernel_queue
*kq
, bool hanging
)
200 if (kq
->queue
->properties
.type
== KFD_QUEUE_TYPE_HIQ
&& !hanging
)
201 kq
->mqd_mgr
->destroy_mqd(kq
->mqd_mgr
,
203 KFD_PREEMPT_TYPE_WAVEFRONT_RESET
,
204 KFD_UNMAP_LATENCY_MS
,
207 else if (kq
->queue
->properties
.type
== KFD_QUEUE_TYPE_DIQ
)
208 kfd_gtt_sa_free(kq
->dev
, kq
->fence_mem_obj
);
210 kq
->mqd_mgr
->free_mqd(kq
->mqd_mgr
, kq
->queue
->mqd
,
211 kq
->queue
->mqd_mem_obj
);
213 kfd_gtt_sa_free(kq
->dev
, kq
->rptr_mem
);
214 kfd_gtt_sa_free(kq
->dev
, kq
->wptr_mem
);
216 /* For CIK family asics, kq->eop_mem is Null, kfd_gtt_sa_free()
217 * is able to handle NULL properly.
219 kfd_gtt_sa_free(kq
->dev
, kq
->eop_mem
);
221 kfd_gtt_sa_free(kq
->dev
, kq
->pq
);
222 kfd_release_kernel_doorbell(kq
->dev
,
223 kq
->queue
->properties
.doorbell_ptr
);
224 uninit_queue(kq
->queue
);
227 int kq_acquire_packet_buffer(struct kernel_queue
*kq
,
228 size_t packet_size_in_dwords
, unsigned int **buffer_ptr
)
230 size_t available_size
;
231 size_t queue_size_dwords
;
234 unsigned int *queue_address
;
236 /* When rptr == wptr, the buffer is empty.
237 * When rptr == wptr + 1, the buffer is full.
238 * It is always rptr that advances to the position of wptr, rather than
239 * the opposite. So we can only use up to queue_size_dwords - 1 dwords.
241 rptr
= *kq
->rptr_kernel
;
242 wptr
= kq
->pending_wptr
;
243 wptr64
= kq
->pending_wptr64
;
244 queue_address
= (unsigned int *)kq
->pq_kernel_addr
;
245 queue_size_dwords
= kq
->queue
->properties
.queue_size
/ 4;
247 pr_debug("rptr: %d\n", rptr
);
248 pr_debug("wptr: %d\n", wptr
);
249 pr_debug("queue_address 0x%p\n", queue_address
);
251 available_size
= (rptr
+ queue_size_dwords
- 1 - wptr
) %
254 if (packet_size_in_dwords
> available_size
) {
256 * make sure calling functions know
257 * acquire_packet_buffer() failed
262 if (wptr
+ packet_size_in_dwords
>= queue_size_dwords
) {
263 /* make sure after rolling back to position 0, there is
264 * still enough space.
266 if (packet_size_in_dwords
>= rptr
)
269 /* fill nops, roll back and start at position 0 */
271 queue_address
[wptr
] = kq
->nop_packet
;
272 wptr
= (wptr
+ 1) % queue_size_dwords
;
277 *buffer_ptr
= &queue_address
[wptr
];
278 kq
->pending_wptr
= wptr
+ packet_size_in_dwords
;
279 kq
->pending_wptr64
= wptr64
+ packet_size_in_dwords
;
288 void kq_submit_packet(struct kernel_queue
*kq
)
293 for (i
= *kq
->wptr_kernel
; i
< kq
->pending_wptr
; i
++) {
294 pr_debug("0x%2X ", kq
->pq_kernel_addr
[i
]);
300 if (kq
->dev
->device_info
->doorbell_size
== 8) {
301 *kq
->wptr64_kernel
= kq
->pending_wptr64
;
302 write_kernel_doorbell64(kq
->queue
->properties
.doorbell_ptr
,
305 *kq
->wptr_kernel
= kq
->pending_wptr
;
306 write_kernel_doorbell(kq
->queue
->properties
.doorbell_ptr
,
311 void kq_rollback_packet(struct kernel_queue
*kq
)
313 if (kq
->dev
->device_info
->doorbell_size
== 8) {
314 kq
->pending_wptr64
= *kq
->wptr64_kernel
;
315 kq
->pending_wptr
= *kq
->wptr_kernel
%
316 (kq
->queue
->properties
.queue_size
/ 4);
318 kq
->pending_wptr
= *kq
->wptr_kernel
;
322 struct kernel_queue
*kernel_queue_init(struct kfd_dev
*dev
,
323 enum kfd_queue_type type
)
325 struct kernel_queue
*kq
;
327 kq
= kzalloc(sizeof(*kq
), GFP_KERNEL
);
331 if (kq_initialize(kq
, dev
, type
, KFD_KERNEL_QUEUE_SIZE
))
334 pr_err("Failed to init kernel queue\n");
340 void kernel_queue_uninit(struct kernel_queue
*kq
, bool hanging
)
342 kq_uninitialize(kq
, hanging
);
346 /* FIXME: Can this test be removed? */
347 static __attribute__((unused
)) void test_kq(struct kfd_dev
*dev
)
349 struct kernel_queue
*kq
;
353 pr_err("Starting kernel queue test\n");
355 kq
= kernel_queue_init(dev
, KFD_QUEUE_TYPE_HIQ
);
357 pr_err(" Failed to initialize HIQ\n");
358 pr_err("Kernel queue test failed\n");
362 retval
= kq_acquire_packet_buffer(kq
, 5, &buffer
);
363 if (unlikely(retval
!= 0)) {
364 pr_err(" Failed to acquire packet buffer\n");
365 pr_err("Kernel queue test failed\n");
368 for (i
= 0; i
< 5; i
++)
369 buffer
[i
] = kq
->nop_packet
;
370 kq_submit_packet(kq
);
372 pr_err("Ending kernel queue test\n");