2014-07-16 21:45:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/printk.h>
|
2014-11-17 11:18:32 +00:00
|
|
|
#include <linux/sched.h>
|
2014-07-16 21:45:35 +00:00
|
|
|
#include "kfd_kernel_queue.h"
|
|
|
|
#include "kfd_priv.h"
|
|
|
|
#include "kfd_device_queue_manager.h"
|
|
|
|
#include "kfd_pm4_headers.h"
|
|
|
|
#include "kfd_pm4_opcodes.h"
|
|
|
|
|
|
|
|
#define PM4_COUNT_ZERO (((1 << 15) - 1) << 16)
|
|
|
|
|
|
|
|
static bool initialize(struct kernel_queue *kq, struct kfd_dev *dev,
|
|
|
|
enum kfd_queue_type type, unsigned int queue_size)
|
|
|
|
{
|
|
|
|
struct queue_properties prop;
|
|
|
|
int retval;
|
|
|
|
union PM4_MES_TYPE_3_HEADER nop;
|
|
|
|
|
2017-08-16 03:00:12 +00:00
|
|
|
if (WARN_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ))
|
|
|
|
return false;
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2017-08-16 03:00:05 +00:00
|
|
|
pr_debug("Initializing queue type %d size %d\n", KFD_QUEUE_TYPE_HIQ,
|
|
|
|
queue_size);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2016-09-17 05:01:41 +00:00
|
|
|
memset(&prop, 0, sizeof(prop));
|
|
|
|
memset(&nop, 0, sizeof(nop));
|
|
|
|
|
2014-07-16 21:45:35 +00:00
|
|
|
nop.opcode = IT_NOP;
|
|
|
|
nop.type = PM4_TYPE_3;
|
|
|
|
nop.u32all |= PM4_COUNT_ZERO;
|
|
|
|
|
|
|
|
kq->dev = dev;
|
|
|
|
kq->nop_packet = nop.u32all;
|
|
|
|
switch (type) {
|
|
|
|
case KFD_QUEUE_TYPE_DIQ:
|
|
|
|
case KFD_QUEUE_TYPE_HIQ:
|
2015-01-12 12:26:10 +00:00
|
|
|
kq->mqd = dev->dqm->ops.get_mqd_manager(dev->dqm,
|
2015-01-04 08:36:30 +00:00
|
|
|
KFD_MQD_TYPE_HIQ);
|
2014-07-16 21:45:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-16 03:00:12 +00:00
|
|
|
pr_err("Invalid queue type %d\n", type);
|
|
|
|
return false;
|
2014-07-16 21:45:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 03:00:06 +00:00
|
|
|
if (!kq->mqd)
|
2014-07-16 21:45:35 +00:00
|
|
|
return false;
|
|
|
|
|
2014-11-20 14:14:56 +00:00
|
|
|
prop.doorbell_ptr = kfd_get_kernel_doorbell(dev, &prop.doorbell_off);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2017-08-16 03:00:06 +00:00
|
|
|
if (!prop.doorbell_ptr) {
|
2017-08-16 03:00:05 +00:00
|
|
|
pr_err("Failed to initialize doorbell");
|
2014-07-16 21:45:35 +00:00
|
|
|
goto err_get_kernel_doorbell;
|
2015-03-08 12:17:02 +00:00
|
|
|
}
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2014-10-26 20:00:31 +00:00
|
|
|
retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq);
|
2015-03-08 12:17:02 +00:00
|
|
|
if (retval != 0) {
|
2017-08-16 03:00:05 +00:00
|
|
|
pr_err("Failed to init pq queues size %d\n", queue_size);
|
2014-07-16 21:45:35 +00:00
|
|
|
goto err_pq_allocate_vidmem;
|
2015-03-08 12:17:02 +00:00
|
|
|
}
|
2014-07-16 21:45:35 +00:00
|
|
|
|
|
|
|
kq->pq_kernel_addr = kq->pq->cpu_ptr;
|
|
|
|
kq->pq_gpu_addr = kq->pq->gpu_addr;
|
|
|
|
|
2014-12-02 14:38:57 +00:00
|
|
|
retval = kq->ops_asic_specific.initialize(kq, dev, type, queue_size);
|
2017-08-16 03:00:06 +00:00
|
|
|
if (!retval)
|
2014-12-02 14:38:57 +00:00
|
|
|
goto err_eop_allocate_vidmem;
|
|
|
|
|
2014-10-26 20:00:31 +00:00
|
|
|
retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel),
|
|
|
|
&kq->rptr_mem);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
|
|
|
if (retval != 0)
|
|
|
|
goto err_rptr_allocate_vidmem;
|
|
|
|
|
|
|
|
kq->rptr_kernel = kq->rptr_mem->cpu_ptr;
|
|
|
|
kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr;
|
|
|
|
|
2014-10-26 20:00:31 +00:00
|
|
|
retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->wptr_kernel),
|
|
|
|
&kq->wptr_mem);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
|
|
|
if (retval != 0)
|
|
|
|
goto err_wptr_allocate_vidmem;
|
|
|
|
|
|
|
|
kq->wptr_kernel = kq->wptr_mem->cpu_ptr;
|
|
|
|
kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr;
|
|
|
|
|
|
|
|
memset(kq->pq_kernel_addr, 0, queue_size);
|
|
|
|
memset(kq->rptr_kernel, 0, sizeof(*kq->rptr_kernel));
|
|
|
|
memset(kq->wptr_kernel, 0, sizeof(*kq->wptr_kernel));
|
|
|
|
|
|
|
|
prop.queue_size = queue_size;
|
|
|
|
prop.is_interop = false;
|
|
|
|
prop.priority = 1;
|
|
|
|
prop.queue_percent = 100;
|
|
|
|
prop.type = type;
|
|
|
|
prop.vmid = 0;
|
|
|
|
prop.queue_address = kq->pq_gpu_addr;
|
|
|
|
prop.read_ptr = (uint32_t *) kq->rptr_gpu_addr;
|
|
|
|
prop.write_ptr = (uint32_t *) kq->wptr_gpu_addr;
|
2014-12-02 14:38:57 +00:00
|
|
|
prop.eop_ring_buffer_address = kq->eop_gpu_addr;
|
|
|
|
prop.eop_ring_buffer_size = PAGE_SIZE;
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2016-09-17 05:01:45 +00:00
|
|
|
if (init_queue(&kq->queue, &prop) != 0)
|
2014-07-16 21:45:35 +00:00
|
|
|
goto err_init_queue;
|
|
|
|
|
|
|
|
kq->queue->device = dev;
|
|
|
|
kq->queue->process = kfd_get_process(current);
|
|
|
|
|
|
|
|
retval = kq->mqd->init_mqd(kq->mqd, &kq->queue->mqd,
|
|
|
|
&kq->queue->mqd_mem_obj,
|
|
|
|
&kq->queue->gart_mqd_addr,
|
|
|
|
&kq->queue->properties);
|
|
|
|
if (retval != 0)
|
|
|
|
goto err_init_mqd;
|
|
|
|
|
|
|
|
/* assign HIQ to HQD */
|
|
|
|
if (type == KFD_QUEUE_TYPE_HIQ) {
|
2017-08-16 03:00:05 +00:00
|
|
|
pr_debug("Assigning hiq to hqd\n");
|
2014-07-16 21:45:35 +00:00
|
|
|
kq->queue->pipe = KFD_CIK_HIQ_PIPE;
|
|
|
|
kq->queue->queue = KFD_CIK_HIQ_QUEUE;
|
|
|
|
kq->mqd->load_mqd(kq->mqd, kq->queue->mqd, kq->queue->pipe,
|
2017-08-16 03:00:17 +00:00
|
|
|
kq->queue->queue, &kq->queue->properties,
|
|
|
|
NULL);
|
2014-07-16 21:45:35 +00:00
|
|
|
} else {
|
|
|
|
/* allocate fence for DIQ */
|
|
|
|
|
2014-10-26 20:00:31 +00:00
|
|
|
retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t),
|
|
|
|
&kq->fence_mem_obj);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
|
|
|
if (retval != 0)
|
|
|
|
goto err_alloc_fence;
|
|
|
|
|
|
|
|
kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr;
|
|
|
|
kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_queue(kq->queue);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
err_alloc_fence:
|
|
|
|
err_init_mqd:
|
|
|
|
uninit_queue(kq->queue);
|
|
|
|
err_init_queue:
|
2014-10-26 20:00:31 +00:00
|
|
|
kfd_gtt_sa_free(dev, kq->wptr_mem);
|
2014-07-16 21:45:35 +00:00
|
|
|
err_wptr_allocate_vidmem:
|
2014-10-26 20:00:31 +00:00
|
|
|
kfd_gtt_sa_free(dev, kq->rptr_mem);
|
2014-07-16 21:45:35 +00:00
|
|
|
err_rptr_allocate_vidmem:
|
2014-12-02 14:38:57 +00:00
|
|
|
kfd_gtt_sa_free(dev, kq->eop_mem);
|
|
|
|
err_eop_allocate_vidmem:
|
2014-10-26 20:00:31 +00:00
|
|
|
kfd_gtt_sa_free(dev, kq->pq);
|
2014-07-16 21:45:35 +00:00
|
|
|
err_pq_allocate_vidmem:
|
2014-11-20 14:14:56 +00:00
|
|
|
kfd_release_kernel_doorbell(dev, prop.doorbell_ptr);
|
2014-07-16 21:45:35 +00:00
|
|
|
err_get_kernel_doorbell:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninitialize(struct kernel_queue *kq)
|
|
|
|
{
|
|
|
|
if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ)
|
|
|
|
kq->mqd->destroy_mqd(kq->mqd,
|
2017-09-02 12:00:25 +00:00
|
|
|
kq->queue->mqd,
|
2017-09-20 22:10:17 +00:00
|
|
|
KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
|
2017-09-20 22:10:16 +00:00
|
|
|
KFD_UNMAP_LATENCY_MS,
|
2014-07-16 21:45:35 +00:00
|
|
|
kq->queue->pipe,
|
|
|
|
kq->queue->queue);
|
2014-10-26 20:00:31 +00:00
|
|
|
else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ)
|
|
|
|
kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2015-03-08 12:17:02 +00:00
|
|
|
kq->mqd->uninit_mqd(kq->mqd, kq->queue->mqd, kq->queue->mqd_mem_obj);
|
|
|
|
|
2014-10-26 20:00:31 +00:00
|
|
|
kfd_gtt_sa_free(kq->dev, kq->rptr_mem);
|
|
|
|
kfd_gtt_sa_free(kq->dev, kq->wptr_mem);
|
2014-12-02 14:38:57 +00:00
|
|
|
kq->ops_asic_specific.uninitialize(kq);
|
2014-10-26 20:00:31 +00:00
|
|
|
kfd_gtt_sa_free(kq->dev, kq->pq);
|
2014-07-16 21:45:35 +00:00
|
|
|
kfd_release_kernel_doorbell(kq->dev,
|
2014-11-20 14:14:56 +00:00
|
|
|
kq->queue->properties.doorbell_ptr);
|
2014-07-16 21:45:35 +00:00
|
|
|
uninit_queue(kq->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int acquire_packet_buffer(struct kernel_queue *kq,
|
|
|
|
size_t packet_size_in_dwords, unsigned int **buffer_ptr)
|
|
|
|
{
|
|
|
|
size_t available_size;
|
|
|
|
size_t queue_size_dwords;
|
|
|
|
uint32_t wptr, rptr;
|
|
|
|
unsigned int *queue_address;
|
|
|
|
|
2017-09-20 22:10:21 +00:00
|
|
|
/* When rptr == wptr, the buffer is empty.
|
|
|
|
* When rptr == wptr + 1, the buffer is full.
|
|
|
|
* It is always rptr that advances to the position of wptr, rather than
|
|
|
|
* the opposite. So we can only use up to queue_size_dwords - 1 dwords.
|
|
|
|
*/
|
2014-07-16 21:45:35 +00:00
|
|
|
rptr = *kq->rptr_kernel;
|
|
|
|
wptr = *kq->wptr_kernel;
|
|
|
|
queue_address = (unsigned int *)kq->pq_kernel_addr;
|
|
|
|
queue_size_dwords = kq->queue->properties.queue_size / sizeof(uint32_t);
|
|
|
|
|
2015-05-04 12:53:15 +00:00
|
|
|
pr_debug("rptr: %d\n", rptr);
|
|
|
|
pr_debug("wptr: %d\n", wptr);
|
|
|
|
pr_debug("queue_address 0x%p\n", queue_address);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2017-09-20 22:10:21 +00:00
|
|
|
available_size = (rptr + queue_size_dwords - 1 - wptr) %
|
2014-07-16 21:45:35 +00:00
|
|
|
queue_size_dwords;
|
|
|
|
|
2017-09-20 22:10:21 +00:00
|
|
|
if (packet_size_in_dwords > available_size) {
|
2014-12-04 12:09:02 +00:00
|
|
|
/*
|
|
|
|
* make sure calling functions know
|
|
|
|
* acquire_packet_buffer() failed
|
|
|
|
*/
|
|
|
|
*buffer_ptr = NULL;
|
2014-07-16 21:45:35 +00:00
|
|
|
return -ENOMEM;
|
2014-12-04 12:09:02 +00:00
|
|
|
}
|
2014-07-16 21:45:35 +00:00
|
|
|
|
|
|
|
if (wptr + packet_size_in_dwords >= queue_size_dwords) {
|
2017-09-20 22:10:21 +00:00
|
|
|
/* make sure after rolling back to position 0, there is
|
|
|
|
* still enough space.
|
|
|
|
*/
|
|
|
|
if (packet_size_in_dwords >= rptr) {
|
|
|
|
*buffer_ptr = NULL;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
/* fill nops, roll back and start at position 0 */
|
2014-07-16 21:45:35 +00:00
|
|
|
while (wptr > 0) {
|
|
|
|
queue_address[wptr] = kq->nop_packet;
|
|
|
|
wptr = (wptr + 1) % queue_size_dwords;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer_ptr = &queue_address[wptr];
|
|
|
|
kq->pending_wptr = wptr + packet_size_in_dwords;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void submit_packet(struct kernel_queue *kq)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) {
|
|
|
|
pr_debug("0x%2X ", kq->pq_kernel_addr[i]);
|
|
|
|
if (i % 15 == 0)
|
|
|
|
pr_debug("\n");
|
|
|
|
}
|
|
|
|
pr_debug("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*kq->wptr_kernel = kq->pending_wptr;
|
2014-11-20 14:14:56 +00:00
|
|
|
write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
|
2014-07-16 21:45:35 +00:00
|
|
|
kq->pending_wptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rollback_packet(struct kernel_queue *kq)
|
|
|
|
{
|
|
|
|
kq->pending_wptr = *kq->queue->properties.write_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
|
|
|
|
enum kfd_queue_type type)
|
|
|
|
{
|
|
|
|
struct kernel_queue *kq;
|
|
|
|
|
2017-08-16 03:00:08 +00:00
|
|
|
kq = kzalloc(sizeof(*kq), GFP_KERNEL);
|
2014-07-16 21:45:35 +00:00
|
|
|
if (!kq)
|
|
|
|
return NULL;
|
|
|
|
|
2015-01-12 13:53:44 +00:00
|
|
|
kq->ops.initialize = initialize;
|
|
|
|
kq->ops.uninitialize = uninitialize;
|
|
|
|
kq->ops.acquire_packet_buffer = acquire_packet_buffer;
|
|
|
|
kq->ops.submit_packet = submit_packet;
|
|
|
|
kq->ops.rollback_packet = rollback_packet;
|
|
|
|
|
2014-12-02 14:38:57 +00:00
|
|
|
switch (dev->device_info->asic_family) {
|
|
|
|
case CHIP_CARRIZO:
|
|
|
|
kernel_queue_init_vi(&kq->ops_asic_specific);
|
2015-01-22 09:15:51 +00:00
|
|
|
break;
|
|
|
|
|
2014-12-02 14:38:57 +00:00
|
|
|
case CHIP_KAVERI:
|
|
|
|
kernel_queue_init_cik(&kq->ops_asic_specific);
|
2015-01-22 09:15:51 +00:00
|
|
|
break;
|
2017-09-20 22:10:19 +00:00
|
|
|
default:
|
|
|
|
WARN(1, "Unexpected ASIC family %u",
|
|
|
|
dev->device_info->asic_family);
|
|
|
|
goto out_free;
|
2014-12-02 14:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 22:10:19 +00:00
|
|
|
if (kq->ops.initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE))
|
|
|
|
return kq;
|
|
|
|
|
|
|
|
pr_err("Failed to init kernel queue\n");
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(kq);
|
|
|
|
return NULL;
|
2014-07-16 21:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void kernel_queue_uninit(struct kernel_queue *kq)
|
|
|
|
{
|
2015-01-12 13:53:44 +00:00
|
|
|
kq->ops.uninitialize(kq);
|
2014-07-16 21:45:35 +00:00
|
|
|
kfree(kq);
|
|
|
|
}
|
|
|
|
|
2017-08-16 03:00:12 +00:00
|
|
|
/* FIXME: Can this test be removed? */
|
2014-11-20 08:08:14 +00:00
|
|
|
static __attribute__((unused)) void test_kq(struct kfd_dev *dev)
|
2014-07-16 21:45:35 +00:00
|
|
|
{
|
|
|
|
struct kernel_queue *kq;
|
|
|
|
uint32_t *buffer, i;
|
|
|
|
int retval;
|
|
|
|
|
2017-08-16 03:00:05 +00:00
|
|
|
pr_err("Starting kernel queue test\n");
|
2014-07-16 21:45:35 +00:00
|
|
|
|
|
|
|
kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ);
|
2017-08-16 03:00:12 +00:00
|
|
|
if (unlikely(!kq)) {
|
|
|
|
pr_err(" Failed to initialize HIQ\n");
|
|
|
|
pr_err("Kernel queue test failed\n");
|
|
|
|
return;
|
|
|
|
}
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2015-01-12 13:53:44 +00:00
|
|
|
retval = kq->ops.acquire_packet_buffer(kq, 5, &buffer);
|
2017-08-16 03:00:12 +00:00
|
|
|
if (unlikely(retval != 0)) {
|
|
|
|
pr_err(" Failed to acquire packet buffer\n");
|
|
|
|
pr_err("Kernel queue test failed\n");
|
|
|
|
return;
|
|
|
|
}
|
2014-07-16 21:45:35 +00:00
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
buffer[i] = kq->nop_packet;
|
2015-01-12 13:53:44 +00:00
|
|
|
kq->ops.submit_packet(kq);
|
2014-07-16 21:45:35 +00:00
|
|
|
|
2017-08-16 03:00:05 +00:00
|
|
|
pr_err("Ending kernel queue test\n");
|
2014-07-16 21:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|