2015-04-20 10:04:22 -06: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 <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "xf86drm.h"
|
|
|
|
#include "amdgpu_drm.h"
|
|
|
|
#include "amdgpu_internal.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create command submission context
|
|
|
|
*
|
|
|
|
* \param dev - \c [in] amdgpu device handle
|
|
|
|
* \param context - \c [out] amdgpu context handle
|
|
|
|
*
|
|
|
|
* \return 0 on success otherwise POSIX Error code
|
|
|
|
*/
|
|
|
|
int amdgpu_cs_ctx_create(amdgpu_device_handle dev,
|
|
|
|
amdgpu_context_handle *context)
|
|
|
|
{
|
2015-05-29 09:13:12 -06:00
|
|
|
struct amdgpu_bo_alloc_request alloc_buffer = {};
|
|
|
|
struct amdgpu_bo_alloc_result info = {};
|
2015-04-20 10:04:22 -06:00
|
|
|
struct amdgpu_context *gpu_context;
|
|
|
|
union drm_amdgpu_ctx args;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (NULL == dev)
|
|
|
|
return -EINVAL;
|
|
|
|
if (NULL == context)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
gpu_context = calloc(1, sizeof(struct amdgpu_context));
|
|
|
|
if (NULL == gpu_context)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-04-22 04:21:13 -06:00
|
|
|
gpu_context->dev = dev;
|
|
|
|
|
2015-04-20 10:04:22 -06:00
|
|
|
r = pthread_mutex_init(&gpu_context->sequence_mutex, NULL);
|
|
|
|
if (r)
|
|
|
|
goto error_mutex;
|
|
|
|
|
2015-05-29 09:13:12 -06:00
|
|
|
/* Create the fence BO */
|
|
|
|
alloc_buffer.alloc_size = 4 * 1024;
|
|
|
|
alloc_buffer.phys_alignment = 4 * 1024;
|
|
|
|
alloc_buffer.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
|
|
|
|
|
|
|
|
r = amdgpu_bo_alloc(dev, &alloc_buffer, &info);
|
2015-04-20 10:04:22 -06:00
|
|
|
if (r)
|
2015-05-29 09:13:12 -06:00
|
|
|
goto error_fence_alloc;
|
|
|
|
gpu_context->fence_bo = info.buf_handle;
|
2015-04-20 10:04:22 -06:00
|
|
|
|
2015-05-29 09:13:12 -06:00
|
|
|
r = amdgpu_bo_cpu_map(gpu_context->fence_bo, &gpu_context->fence_cpu);
|
|
|
|
if (r)
|
|
|
|
goto error_fence_map;
|
2015-04-20 10:04:22 -06:00
|
|
|
|
2015-05-29 09:13:12 -06:00
|
|
|
/* Create the context */
|
2015-04-20 10:04:22 -06:00
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
|
|
|
|
r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_CTX, &args, sizeof(args));
|
|
|
|
if (r)
|
|
|
|
goto error_kernel;
|
|
|
|
|
|
|
|
gpu_context->id = args.out.alloc.ctx_id;
|
|
|
|
*context = (amdgpu_context_handle)gpu_context;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_kernel:
|
2015-05-29 09:13:12 -06:00
|
|
|
amdgpu_bo_cpu_unmap(gpu_context->fence_bo);
|
2015-04-20 10:04:22 -06:00
|
|
|
|
2015-05-29 09:13:12 -06:00
|
|
|
error_fence_map:
|
|
|
|
amdgpu_bo_free(gpu_context->fence_bo);
|
|
|
|
|
|
|
|
error_fence_alloc:
|
2015-04-20 10:04:22 -06:00
|
|
|
pthread_mutex_destroy(&gpu_context->sequence_mutex);
|
|
|
|
|
|
|
|
error_mutex:
|
|
|
|
free(gpu_context);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release command submission context
|
|
|
|
*
|
|
|
|
* \param dev - \c [in] amdgpu device handle
|
|
|
|
* \param context - \c [in] amdgpu context handle
|
|
|
|
*
|
|
|
|
* \return 0 on success otherwise POSIX Error code
|
|
|
|
*/
|
2015-04-22 04:21:13 -06:00
|
|
|
int amdgpu_cs_ctx_free(amdgpu_context_handle context)
|
2015-04-20 10:04:22 -06:00
|
|
|
{
|
|
|
|
union drm_amdgpu_ctx args;
|
2015-04-22 04:21:13 -06:00
|
|
|
int r;
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
if (NULL == context)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-05-29 09:13:12 -06:00
|
|
|
r = amdgpu_bo_cpu_unmap(context->fence_bo);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = amdgpu_bo_free(context->fence_bo);
|
2015-04-20 10:04:22 -06:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
pthread_mutex_destroy(&context->sequence_mutex);
|
|
|
|
|
|
|
|
/* now deal with kernel side */
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.in.op = AMDGPU_CTX_OP_FREE_CTX;
|
|
|
|
args.in.ctx_id = context->id;
|
2015-04-22 04:21:13 -06:00
|
|
|
r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
|
|
|
|
&args, sizeof(args));
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
free(context);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-05-05 13:23:02 -06:00
|
|
|
int amdgpu_cs_query_reset_state(amdgpu_context_handle context,
|
|
|
|
uint32_t *state, uint32_t *hangs)
|
|
|
|
{
|
|
|
|
union drm_amdgpu_ctx args;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.in.op = AMDGPU_CTX_OP_QUERY_STATE;
|
|
|
|
args.in.ctx_id = context->id;
|
|
|
|
r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
|
|
|
|
&args, sizeof(args));
|
|
|
|
if (!r) {
|
|
|
|
*state = args.out.state.reset_status;
|
|
|
|
*hangs = args.out.state.hangs;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-04-20 10:04:22 -06:00
|
|
|
static uint32_t amdgpu_cs_fence_index(unsigned ip, unsigned ring)
|
|
|
|
{
|
|
|
|
return ip * AMDGPU_CS_MAX_RINGS + ring;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit command to kernel DRM
|
|
|
|
* \param dev - \c [in] Device handle
|
|
|
|
* \param context - \c [in] GPU Context
|
|
|
|
* \param ibs_request - \c [in] Pointer to submission requests
|
|
|
|
* \param fence - \c [out] return fence for this submission
|
|
|
|
*
|
|
|
|
* \return 0 on success otherwise POSIX Error code
|
|
|
|
* \sa amdgpu_cs_submit()
|
|
|
|
*/
|
2015-04-22 04:21:13 -06:00
|
|
|
static int amdgpu_cs_submit_one(amdgpu_context_handle context,
|
2015-04-20 10:04:22 -06:00
|
|
|
struct amdgpu_cs_request *ibs_request,
|
|
|
|
uint64_t *fence)
|
|
|
|
{
|
2015-05-29 04:59:59 -06:00
|
|
|
int r = 0;
|
2015-04-20 10:04:22 -06:00
|
|
|
uint32_t i, size;
|
|
|
|
union drm_amdgpu_cs cs;
|
|
|
|
uint64_t *chunk_array;
|
|
|
|
struct drm_amdgpu_cs_chunk *chunks;
|
|
|
|
struct drm_amdgpu_cs_chunk_data *chunk_data;
|
|
|
|
|
|
|
|
if (ibs_request->ip_type >= AMDGPU_HW_IP_NUM)
|
|
|
|
return -EINVAL;
|
|
|
|
if (ibs_request->ring >= AMDGPU_CS_MAX_RINGS)
|
|
|
|
return -EINVAL;
|
|
|
|
if (ibs_request->number_of_ibs > AMDGPU_CS_MAX_IBS_PER_SUBMIT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-04-22 06:52:34 -06:00
|
|
|
size = (ibs_request->number_of_ibs + 1) * (
|
|
|
|
sizeof(uint64_t) +
|
2015-04-20 10:04:22 -06:00
|
|
|
sizeof(struct drm_amdgpu_cs_chunk) +
|
2015-04-22 06:52:34 -06:00
|
|
|
sizeof(struct drm_amdgpu_cs_chunk_data));
|
2015-05-29 04:59:59 -06:00
|
|
|
|
|
|
|
chunk_array = calloc(1, size);
|
2015-04-20 10:04:22 -06:00
|
|
|
if (NULL == chunk_array)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
chunks = (struct drm_amdgpu_cs_chunk *)(chunk_array + ibs_request->number_of_ibs + 1);
|
|
|
|
chunk_data = (struct drm_amdgpu_cs_chunk_data *)(chunks + ibs_request->number_of_ibs + 1);
|
|
|
|
|
|
|
|
memset(&cs, 0, sizeof(cs));
|
|
|
|
cs.in.chunks = (uint64_t)(uintptr_t)chunk_array;
|
|
|
|
cs.in.ctx_id = context->id;
|
2015-05-05 01:15:15 -06:00
|
|
|
if (ibs_request->resources)
|
|
|
|
cs.in.bo_list_handle = ibs_request->resources->handle;
|
2015-04-20 10:04:22 -06:00
|
|
|
cs.in.num_chunks = ibs_request->number_of_ibs;
|
|
|
|
/* IB chunks */
|
|
|
|
for (i = 0; i < ibs_request->number_of_ibs; i++) {
|
|
|
|
struct amdgpu_cs_ib_info *ib;
|
|
|
|
chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
|
|
|
|
chunks[i].chunk_id = AMDGPU_CHUNK_ID_IB;
|
|
|
|
chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
|
|
|
|
chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
|
|
|
|
|
|
|
|
ib = &ibs_request->ibs[i];
|
|
|
|
|
2015-05-29 11:13:41 -06:00
|
|
|
chunk_data[i].ib_data.handle = ib->bo_handle->handle;
|
|
|
|
chunk_data[i].ib_data.va_start = ib->bo_handle->virtual_mc_base_address
|
2015-05-20 14:17:48 -06:00
|
|
|
+ ib->offset_dw * 4;
|
2015-04-20 10:04:22 -06:00
|
|
|
chunk_data[i].ib_data.ib_bytes = ib->size * 4;
|
|
|
|
chunk_data[i].ib_data.ip_type = ibs_request->ip_type;
|
|
|
|
chunk_data[i].ib_data.ip_instance = ibs_request->ip_instance;
|
|
|
|
chunk_data[i].ib_data.ring = ibs_request->ring;
|
2015-05-11 09:02:09 -06:00
|
|
|
chunk_data[i].ib_data.flags = ib->flags;
|
2015-04-20 10:04:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&context->sequence_mutex);
|
|
|
|
|
|
|
|
if (ibs_request->ip_type != AMDGPU_HW_IP_UVD &&
|
|
|
|
ibs_request->ip_type != AMDGPU_HW_IP_VCE) {
|
|
|
|
i = cs.in.num_chunks++;
|
|
|
|
|
|
|
|
/* fence chunk */
|
|
|
|
chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
|
|
|
|
chunks[i].chunk_id = AMDGPU_CHUNK_ID_FENCE;
|
|
|
|
chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_fence) / 4;
|
|
|
|
chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
|
|
|
|
|
|
|
|
/* fence bo handle */
|
2015-05-29 09:13:12 -06:00
|
|
|
chunk_data[i].fence_data.handle = context->fence_bo->handle;
|
2015-04-20 10:04:22 -06:00
|
|
|
/* offset */
|
|
|
|
chunk_data[i].fence_data.offset = amdgpu_cs_fence_index(
|
|
|
|
ibs_request->ip_type, ibs_request->ring);
|
|
|
|
chunk_data[i].fence_data.offset *= sizeof(uint64_t);
|
|
|
|
}
|
|
|
|
|
2015-04-22 04:21:13 -06:00
|
|
|
r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CS,
|
2015-04-20 10:04:22 -06:00
|
|
|
&cs, sizeof(cs));
|
|
|
|
if (r)
|
|
|
|
goto error_unlock;
|
|
|
|
|
|
|
|
*fence = cs.out.handle;
|
|
|
|
|
|
|
|
error_unlock:
|
|
|
|
pthread_mutex_unlock(&context->sequence_mutex);
|
|
|
|
free(chunk_array);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-04-22 04:21:13 -06:00
|
|
|
int amdgpu_cs_submit(amdgpu_context_handle context,
|
2015-04-20 10:04:22 -06:00
|
|
|
uint64_t flags,
|
|
|
|
struct amdgpu_cs_request *ibs_request,
|
|
|
|
uint32_t number_of_requests,
|
|
|
|
uint64_t *fences)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
2015-04-22 04:21:13 -06:00
|
|
|
int r;
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
if (NULL == context)
|
|
|
|
return -EINVAL;
|
|
|
|
if (NULL == ibs_request)
|
|
|
|
return -EINVAL;
|
|
|
|
if (NULL == fences)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
for (i = 0; i < number_of_requests; i++) {
|
2015-04-22 04:21:13 -06:00
|
|
|
r = amdgpu_cs_submit_one(context, ibs_request, fences);
|
2015-04-20 10:04:22 -06:00
|
|
|
if (r)
|
|
|
|
break;
|
|
|
|
fences++;
|
|
|
|
ibs_request++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate absolute timeout.
|
|
|
|
*
|
|
|
|
* \param timeout - \c [in] timeout in nanoseconds.
|
|
|
|
*
|
|
|
|
* \return absolute timeout in nanoseconds
|
|
|
|
*/
|
|
|
|
uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (timeout != AMDGPU_TIMEOUT_INFINITE) {
|
|
|
|
struct timespec current;
|
|
|
|
r = clock_gettime(CLOCK_MONOTONIC, ¤t);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
timeout += ((uint64_t)current.tv_sec) * 1000000000ull;
|
|
|
|
timeout += current.tv_nsec;
|
|
|
|
}
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2015-05-08 04:34:20 -06:00
|
|
|
static int amdgpu_ioctl_wait_cs(amdgpu_context_handle context,
|
2015-04-20 10:04:22 -06:00
|
|
|
unsigned ip,
|
|
|
|
unsigned ip_instance,
|
|
|
|
uint32_t ring,
|
|
|
|
uint64_t handle,
|
|
|
|
uint64_t timeout_ns,
|
|
|
|
bool *busy)
|
|
|
|
{
|
2015-05-08 04:34:20 -06:00
|
|
|
amdgpu_device_handle dev = context->dev;
|
2015-04-20 10:04:22 -06:00
|
|
|
union drm_amdgpu_wait_cs args;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.in.handle = handle;
|
|
|
|
args.in.ip_type = ip;
|
|
|
|
args.in.ip_instance = ip_instance;
|
|
|
|
args.in.ring = ring;
|
|
|
|
args.in.timeout = amdgpu_cs_calculate_timeout(timeout_ns);
|
2015-05-08 04:34:20 -06:00
|
|
|
args.in.ctx_id = context->id;
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
/* Handle errors manually here because of timeout */
|
|
|
|
r = ioctl(dev->fd, DRM_IOCTL_AMDGPU_WAIT_CS, &args);
|
|
|
|
if (r == -1 && (errno == EINTR || errno == EAGAIN)) {
|
|
|
|
*busy = true;
|
|
|
|
return 0;
|
|
|
|
} else if (r)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
*busy = args.out.status;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-22 04:21:13 -06:00
|
|
|
int amdgpu_cs_query_fence_status(struct amdgpu_cs_query_fence *fence,
|
2015-04-20 10:04:22 -06:00
|
|
|
uint32_t *expired)
|
|
|
|
{
|
|
|
|
amdgpu_context_handle context;
|
|
|
|
uint64_t *signaled_fence;
|
|
|
|
uint64_t *expired_fence;
|
|
|
|
unsigned ip_type, ip_instance;
|
|
|
|
uint32_t ring;
|
|
|
|
bool busy = true;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (NULL == fence)
|
|
|
|
return -EINVAL;
|
|
|
|
if (NULL == expired)
|
|
|
|
return -EINVAL;
|
|
|
|
if (NULL == fence->context)
|
|
|
|
return -EINVAL;
|
|
|
|
if (fence->ip_type >= AMDGPU_HW_IP_NUM)
|
|
|
|
return -EINVAL;
|
|
|
|
if (fence->ring >= AMDGPU_CS_MAX_RINGS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
context = fence->context;
|
|
|
|
ip_type = fence->ip_type;
|
|
|
|
ip_instance = fence->ip_instance;
|
|
|
|
ring = fence->ring;
|
2015-05-29 09:13:12 -06:00
|
|
|
signaled_fence = context->fence_cpu;
|
2015-04-20 10:04:22 -06:00
|
|
|
signaled_fence += amdgpu_cs_fence_index(ip_type, ring);
|
|
|
|
expired_fence = &context->expired_fences[ip_type][ip_instance][ring];
|
|
|
|
*expired = false;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&context->sequence_mutex);
|
|
|
|
if (fence->fence <= *expired_fence) {
|
|
|
|
/* This fence value is expired already. */
|
|
|
|
pthread_mutex_unlock(&context->sequence_mutex);
|
|
|
|
*expired = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fence->fence <= *signaled_fence) {
|
|
|
|
/* This fence value is signaled already. */
|
|
|
|
*expired_fence = *signaled_fence;
|
|
|
|
pthread_mutex_unlock(&context->sequence_mutex);
|
|
|
|
*expired = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&context->sequence_mutex);
|
|
|
|
|
2015-05-08 04:34:20 -06:00
|
|
|
r = amdgpu_ioctl_wait_cs(context, ip_type, ip_instance, ring,
|
2015-04-20 10:04:22 -06:00
|
|
|
fence->fence, fence->timeout_ns, &busy);
|
|
|
|
if (!r && !busy) {
|
|
|
|
*expired = true;
|
|
|
|
pthread_mutex_lock(&context->sequence_mutex);
|
|
|
|
/* The thread doesn't hold sequence_mutex. Other thread could
|
|
|
|
update *expired_fence already. Check whether there is a
|
|
|
|
newerly expired fence. */
|
2015-05-29 04:59:59 -06:00
|
|
|
if (fence->fence > *expired_fence)
|
2015-04-20 10:04:22 -06:00
|
|
|
*expired_fence = fence->fence;
|
2015-05-29 04:59:59 -06:00
|
|
|
pthread_mutex_unlock(&context->sequence_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|