2015-04-21 04:55:21 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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.
|
|
|
|
*
|
|
|
|
* Authors: monk liu <monk.liu@amd.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <drm/drmP.h>
|
2016-12-23 06:06:50 +08:00
|
|
|
#include <drm/drm_auth.h>
|
2015-04-21 04:55:21 +08:00
|
|
|
#include "amdgpu.h"
|
2017-06-27 04:17:13 +08:00
|
|
|
#include "amdgpu_sched.h"
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2016-12-23 06:06:50 +08:00
|
|
|
static int amdgpu_ctx_priority_permit(struct drm_file *filp,
|
2017-12-07 00:49:39 +08:00
|
|
|
enum drm_sched_priority priority)
|
2016-12-23 06:06:50 +08:00
|
|
|
{
|
|
|
|
/* NORMAL and below are accessible by everyone */
|
2017-12-07 00:49:39 +08:00
|
|
|
if (priority <= DRM_SCHED_PRIORITY_NORMAL)
|
2016-12-23 06:06:50 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (capable(CAP_SYS_NICE))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (drm_is_current_master(filp))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int amdgpu_ctx_init(struct amdgpu_device *adev,
|
2017-12-07 00:49:39 +08:00
|
|
|
enum drm_sched_priority priority,
|
2016-12-23 06:06:50 +08:00
|
|
|
struct drm_file *filp,
|
|
|
|
struct amdgpu_ctx *ctx)
|
2015-04-21 04:55:21 +08:00
|
|
|
{
|
2018-07-13 15:12:44 +08:00
|
|
|
struct drm_sched_rq *sdma_rqs[AMDGPU_MAX_RINGS];
|
2018-07-16 20:59:26 +08:00
|
|
|
struct drm_sched_rq *comp_rqs[AMDGPU_MAX_RINGS];
|
|
|
|
unsigned i, j, num_sdma_rqs, num_comp_rqs;
|
2015-08-04 23:51:05 +08:00
|
|
|
int r;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2017-12-07 00:49:39 +08:00
|
|
|
if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
|
2016-12-23 06:06:50 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
r = amdgpu_ctx_priority_permit(filp, priority);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
2015-07-06 13:42:58 +08:00
|
|
|
memset(ctx, 0, sizeof(*ctx));
|
|
|
|
ctx->adev = adev;
|
|
|
|
kref_init(&ctx->refcount);
|
|
|
|
spin_lock_init(&ctx->ring_lock);
|
2016-02-11 17:20:53 +08:00
|
|
|
ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
|
2016-10-25 20:00:45 +08:00
|
|
|
sizeof(struct dma_fence*), GFP_KERNEL);
|
2015-12-10 15:45:11 +08:00
|
|
|
if (!ctx->fences)
|
|
|
|
return -ENOMEM;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2017-10-11 04:50:17 +08:00
|
|
|
mutex_init(&ctx->lock);
|
|
|
|
|
2015-12-10 15:45:11 +08:00
|
|
|
for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
|
|
|
|
ctx->rings[i].sequence = 1;
|
2016-02-11 17:20:53 +08:00
|
|
|
ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
|
2015-12-10 15:45:11 +08:00
|
|
|
}
|
2016-10-04 15:43:30 +08:00
|
|
|
|
|
|
|
ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
|
2017-10-17 14:39:23 +08:00
|
|
|
ctx->reset_counter_query = ctx->reset_counter;
|
2017-10-09 21:18:43 +08:00
|
|
|
ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
|
2017-06-07 08:20:38 +08:00
|
|
|
ctx->init_priority = priority;
|
2017-12-07 00:49:39 +08:00
|
|
|
ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
|
2016-10-04 15:43:30 +08:00
|
|
|
|
2018-07-13 15:12:44 +08:00
|
|
|
num_sdma_rqs = 0;
|
2018-07-16 20:59:26 +08:00
|
|
|
num_comp_rqs = 0;
|
2016-01-15 11:25:00 +08:00
|
|
|
for (i = 0; i < adev->num_rings; i++) {
|
2016-02-11 16:56:44 +08:00
|
|
|
struct amdgpu_ring *ring = adev->rings[i];
|
2017-12-07 00:49:39 +08:00
|
|
|
struct drm_sched_rq *rq;
|
2016-02-11 16:56:44 +08:00
|
|
|
|
2016-12-23 06:06:50 +08:00
|
|
|
rq = &ring->sched.sched_rq[priority];
|
2018-07-13 15:12:44 +08:00
|
|
|
if (ring->funcs->type == AMDGPU_RING_TYPE_SDMA)
|
|
|
|
sdma_rqs[num_sdma_rqs++] = rq;
|
2018-07-16 20:59:26 +08:00
|
|
|
else if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
|
|
|
|
comp_rqs[num_comp_rqs++] = rq;
|
2018-07-13 15:12:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create context entity for each ring */
|
|
|
|
for (i = 0; i < adev->num_rings; i++) {
|
|
|
|
struct amdgpu_ring *ring = adev->rings[i];
|
2017-05-11 13:36:33 +08:00
|
|
|
|
|
|
|
if (ring == &adev->gfx.kiq.ring)
|
|
|
|
continue;
|
|
|
|
|
2018-07-13 15:12:44 +08:00
|
|
|
if (ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
|
|
|
|
r = drm_sched_entity_init(&ctx->rings[i].entity,
|
|
|
|
sdma_rqs, num_sdma_rqs,
|
|
|
|
&ctx->guilty);
|
2018-07-16 20:59:26 +08:00
|
|
|
} else if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) {
|
|
|
|
r = drm_sched_entity_init(&ctx->rings[i].entity,
|
|
|
|
comp_rqs, num_comp_rqs,
|
|
|
|
&ctx->guilty);
|
2018-07-13 15:12:44 +08:00
|
|
|
} else {
|
|
|
|
struct drm_sched_rq *rq;
|
|
|
|
|
|
|
|
rq = &ring->sched.sched_rq[priority];
|
|
|
|
r = drm_sched_entity_init(&ctx->rings[i].entity,
|
|
|
|
&rq, 1, &ctx->guilty);
|
|
|
|
}
|
2016-01-15 11:25:00 +08:00
|
|
|
if (r)
|
2016-10-26 17:07:03 +08:00
|
|
|
goto failed;
|
2016-01-15 11:25:00 +08:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:55:21 +08:00
|
|
|
return 0;
|
2016-10-26 17:07:03 +08:00
|
|
|
|
|
|
|
failed:
|
|
|
|
for (j = 0; j < i; j++)
|
2018-07-20 20:21:05 +08:00
|
|
|
drm_sched_entity_destroy(&ctx->rings[j].entity);
|
2016-10-26 17:07:03 +08:00
|
|
|
kfree(ctx->fences);
|
|
|
|
ctx->fences = NULL;
|
|
|
|
return r;
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
|
|
|
|
2018-04-16 10:07:02 +08:00
|
|
|
static void amdgpu_ctx_fini(struct kref *ref)
|
2015-04-21 04:55:21 +08:00
|
|
|
{
|
2018-04-16 10:07:02 +08:00
|
|
|
struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
|
2015-08-04 23:51:05 +08:00
|
|
|
struct amdgpu_device *adev = ctx->adev;
|
|
|
|
unsigned i, j;
|
|
|
|
|
2015-11-04 00:07:11 +08:00
|
|
|
if (!adev)
|
|
|
|
return;
|
|
|
|
|
2015-08-04 23:51:05 +08:00
|
|
|
for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
|
2015-12-10 15:45:11 +08:00
|
|
|
for (j = 0; j < amdgpu_sched_jobs; ++j)
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_put(ctx->rings[i].fences[j]);
|
2015-12-10 15:45:11 +08:00
|
|
|
kfree(ctx->fences);
|
2016-09-26 04:34:46 +08:00
|
|
|
ctx->fences = NULL;
|
2015-08-04 23:51:05 +08:00
|
|
|
|
2017-10-11 04:50:17 +08:00
|
|
|
mutex_destroy(&ctx->lock);
|
2018-04-16 10:07:02 +08:00
|
|
|
|
|
|
|
kfree(ctx);
|
2015-08-04 23:51:05 +08:00
|
|
|
}
|
|
|
|
|
2018-07-16 21:19:20 +08:00
|
|
|
int amdgpu_ctx_get_ring(struct amdgpu_ctx *ctx,
|
|
|
|
u32 hw_ip, u32 instance, u32 ring,
|
|
|
|
struct amdgpu_ring **out_ring)
|
|
|
|
{
|
|
|
|
struct amdgpu_device *adev = ctx->adev;
|
|
|
|
unsigned num_rings = 0;
|
|
|
|
|
|
|
|
/* Right now all IPs have only one instance - multiple rings. */
|
|
|
|
if (instance != 0) {
|
|
|
|
DRM_DEBUG("invalid ip instance: %d\n", instance);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (hw_ip) {
|
|
|
|
case AMDGPU_HW_IP_GFX:
|
|
|
|
*out_ring = &adev->gfx.gfx_ring[ring];
|
|
|
|
num_rings = adev->gfx.num_gfx_rings;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_COMPUTE:
|
|
|
|
*out_ring = &adev->gfx.compute_ring[ring];
|
|
|
|
num_rings = adev->gfx.num_compute_rings;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_DMA:
|
|
|
|
*out_ring = &adev->sdma.instance[ring].ring;
|
|
|
|
num_rings = adev->sdma.num_instances;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_UVD:
|
|
|
|
*out_ring = &adev->uvd.inst[0].ring;
|
|
|
|
num_rings = adev->uvd.num_uvd_inst;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_VCE:
|
|
|
|
*out_ring = &adev->vce.ring[ring];
|
|
|
|
num_rings = adev->vce.num_rings;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_UVD_ENC:
|
|
|
|
*out_ring = &adev->uvd.inst[0].ring_enc[ring];
|
|
|
|
num_rings = adev->uvd.num_enc_rings;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_VCN_DEC:
|
|
|
|
*out_ring = &adev->vcn.ring_dec;
|
|
|
|
num_rings = 1;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_VCN_ENC:
|
|
|
|
*out_ring = &adev->vcn.ring_enc[ring];
|
|
|
|
num_rings = adev->vcn.num_enc_rings;
|
|
|
|
break;
|
|
|
|
case AMDGPU_HW_IP_VCN_JPEG:
|
|
|
|
*out_ring = &adev->vcn.ring_jpeg;
|
|
|
|
num_rings = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ring > num_rings)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:51:05 +08:00
|
|
|
static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
|
|
|
|
struct amdgpu_fpriv *fpriv,
|
2016-12-23 06:06:50 +08:00
|
|
|
struct drm_file *filp,
|
2017-12-07 00:49:39 +08:00
|
|
|
enum drm_sched_priority priority,
|
2015-08-04 23:51:05 +08:00
|
|
|
uint32_t *id)
|
|
|
|
{
|
|
|
|
struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
|
2015-04-21 04:55:21 +08:00
|
|
|
struct amdgpu_ctx *ctx;
|
2015-08-04 23:51:05 +08:00
|
|
|
int r;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2015-08-04 23:51:05 +08:00
|
|
|
ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
|
|
|
|
if (!ctx)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_lock(&mgr->lock);
|
|
|
|
r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
|
|
|
|
if (r < 0) {
|
2015-05-06 02:52:00 +08:00
|
|
|
mutex_unlock(&mgr->lock);
|
2015-08-04 23:51:05 +08:00
|
|
|
kfree(ctx);
|
|
|
|
return r;
|
|
|
|
}
|
2016-12-23 06:06:50 +08:00
|
|
|
|
2015-08-04 23:51:05 +08:00
|
|
|
*id = (uint32_t)r;
|
2016-12-23 06:06:50 +08:00
|
|
|
r = amdgpu_ctx_init(adev, priority, filp, ctx);
|
2015-12-10 15:50:02 +08:00
|
|
|
if (r) {
|
|
|
|
idr_remove(&mgr->ctx_handles, *id);
|
|
|
|
*id = 0;
|
|
|
|
kfree(ctx);
|
|
|
|
}
|
2015-08-04 23:51:05 +08:00
|
|
|
mutex_unlock(&mgr->lock);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void amdgpu_ctx_do_release(struct kref *ref)
|
|
|
|
{
|
|
|
|
struct amdgpu_ctx *ctx;
|
2018-04-16 10:07:02 +08:00
|
|
|
u32 i;
|
2015-08-04 23:51:05 +08:00
|
|
|
|
|
|
|
ctx = container_of(ref, struct amdgpu_ctx, refcount);
|
|
|
|
|
2018-05-16 02:12:21 +08:00
|
|
|
for (i = 0; i < ctx->adev->num_rings; i++) {
|
|
|
|
|
|
|
|
if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
|
|
|
|
continue;
|
|
|
|
|
2018-07-20 20:21:05 +08:00
|
|
|
drm_sched_entity_destroy(&ctx->rings[i].entity);
|
2018-05-16 02:12:21 +08:00
|
|
|
}
|
2015-08-04 23:51:05 +08:00
|
|
|
|
2018-04-16 10:07:02 +08:00
|
|
|
amdgpu_ctx_fini(ref);
|
2015-08-04 23:51:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
|
|
|
|
{
|
|
|
|
struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
|
|
|
|
struct amdgpu_ctx *ctx;
|
|
|
|
|
|
|
|
mutex_lock(&mgr->lock);
|
2016-12-23 02:30:22 +08:00
|
|
|
ctx = idr_remove(&mgr->ctx_handles, id);
|
|
|
|
if (ctx)
|
2015-07-06 13:42:58 +08:00
|
|
|
kref_put(&ctx->refcount, amdgpu_ctx_do_release);
|
2015-08-04 23:51:05 +08:00
|
|
|
mutex_unlock(&mgr->lock);
|
2016-12-23 02:30:22 +08:00
|
|
|
return ctx ? 0 : -EINVAL;
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
|
|
|
|
2015-05-06 03:13:49 +08:00
|
|
|
static int amdgpu_ctx_query(struct amdgpu_device *adev,
|
|
|
|
struct amdgpu_fpriv *fpriv, uint32_t id,
|
|
|
|
union drm_amdgpu_ctx_out *out)
|
2015-04-21 04:55:21 +08:00
|
|
|
{
|
|
|
|
struct amdgpu_ctx *ctx;
|
2015-07-06 13:42:58 +08:00
|
|
|
struct amdgpu_ctx_mgr *mgr;
|
2015-05-06 03:13:49 +08:00
|
|
|
unsigned reset_counter;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
2015-07-06 13:42:58 +08:00
|
|
|
if (!fpriv)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mgr = &fpriv->ctx_mgr;
|
2015-05-06 02:52:00 +08:00
|
|
|
mutex_lock(&mgr->lock);
|
2015-04-21 04:55:21 +08:00
|
|
|
ctx = idr_find(&mgr->ctx_handles, id);
|
2015-05-06 03:13:49 +08:00
|
|
|
if (!ctx) {
|
2015-05-06 02:52:00 +08:00
|
|
|
mutex_unlock(&mgr->lock);
|
2015-05-06 03:13:49 +08:00
|
|
|
return -EINVAL;
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
2015-05-06 03:13:49 +08:00
|
|
|
|
|
|
|
/* TODO: these two are always zero */
|
2015-08-17 10:48:26 +08:00
|
|
|
out->state.flags = 0x0;
|
|
|
|
out->state.hangs = 0x0;
|
2015-05-06 03:13:49 +08:00
|
|
|
|
|
|
|
/* determine if a GPU reset has occured since the last call */
|
|
|
|
reset_counter = atomic_read(&adev->gpu_reset_counter);
|
|
|
|
/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
|
2017-10-17 14:39:23 +08:00
|
|
|
if (ctx->reset_counter_query == reset_counter)
|
2015-05-06 03:13:49 +08:00
|
|
|
out->state.reset_status = AMDGPU_CTX_NO_RESET;
|
|
|
|
else
|
|
|
|
out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
|
2017-10-17 14:39:23 +08:00
|
|
|
ctx->reset_counter_query = reset_counter;
|
2015-05-06 03:13:49 +08:00
|
|
|
|
2015-05-06 02:52:00 +08:00
|
|
|
mutex_unlock(&mgr->lock);
|
2015-05-06 03:13:49 +08:00
|
|
|
return 0;
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 14:58:01 +08:00
|
|
|
static int amdgpu_ctx_query2(struct amdgpu_device *adev,
|
|
|
|
struct amdgpu_fpriv *fpriv, uint32_t id,
|
|
|
|
union drm_amdgpu_ctx_out *out)
|
|
|
|
{
|
|
|
|
struct amdgpu_ctx *ctx;
|
|
|
|
struct amdgpu_ctx_mgr *mgr;
|
|
|
|
|
|
|
|
if (!fpriv)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mgr = &fpriv->ctx_mgr;
|
|
|
|
mutex_lock(&mgr->lock);
|
|
|
|
ctx = idr_find(&mgr->ctx_handles, id);
|
|
|
|
if (!ctx) {
|
|
|
|
mutex_unlock(&mgr->lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->state.flags = 0x0;
|
|
|
|
out->state.hangs = 0x0;
|
|
|
|
|
|
|
|
if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
|
|
|
|
out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
|
|
|
|
|
|
|
|
if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
|
|
|
|
out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
|
|
|
|
|
|
|
|
if (atomic_read(&ctx->guilty))
|
|
|
|
out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
|
|
|
|
|
|
|
|
mutex_unlock(&mgr->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:55:21 +08:00
|
|
|
int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
|
2015-05-06 03:13:49 +08:00
|
|
|
struct drm_file *filp)
|
2015-04-21 04:55:21 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t id;
|
2017-12-07 00:49:39 +08:00
|
|
|
enum drm_sched_priority priority;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
|
|
|
union drm_amdgpu_ctx *args = data;
|
|
|
|
struct amdgpu_device *adev = dev->dev_private;
|
|
|
|
struct amdgpu_fpriv *fpriv = filp->driver_priv;
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
id = args->in.ctx_id;
|
2016-12-23 06:06:50 +08:00
|
|
|
priority = amdgpu_to_sched_priority(args->in.priority);
|
|
|
|
|
2017-05-25 05:00:10 +08:00
|
|
|
/* For backwards compatibility reasons, we need to accept
|
|
|
|
* ioctls with garbage in the priority field */
|
2017-12-07 00:49:39 +08:00
|
|
|
if (priority == DRM_SCHED_PRIORITY_INVALID)
|
|
|
|
priority = DRM_SCHED_PRIORITY_NORMAL;
|
2015-04-21 04:55:21 +08:00
|
|
|
|
|
|
|
switch (args->in.op) {
|
2016-02-11 17:20:53 +08:00
|
|
|
case AMDGPU_CTX_OP_ALLOC_CTX:
|
2016-12-23 06:06:50 +08:00
|
|
|
r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
|
2016-02-11 17:20:53 +08:00
|
|
|
args->out.alloc.ctx_id = id;
|
|
|
|
break;
|
|
|
|
case AMDGPU_CTX_OP_FREE_CTX:
|
|
|
|
r = amdgpu_ctx_free(fpriv, id);
|
|
|
|
break;
|
|
|
|
case AMDGPU_CTX_OP_QUERY_STATE:
|
|
|
|
r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
|
|
|
|
break;
|
2017-10-17 14:58:01 +08:00
|
|
|
case AMDGPU_CTX_OP_QUERY_STATE2:
|
|
|
|
r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
|
|
|
|
break;
|
2016-02-11 17:20:53 +08:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2015-04-21 04:55:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2015-05-08 17:29:40 +08:00
|
|
|
|
|
|
|
struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
|
|
|
|
{
|
|
|
|
struct amdgpu_ctx *ctx;
|
2015-07-06 13:42:58 +08:00
|
|
|
struct amdgpu_ctx_mgr *mgr;
|
|
|
|
|
|
|
|
if (!fpriv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mgr = &fpriv->ctx_mgr;
|
2015-05-08 17:29:40 +08:00
|
|
|
|
|
|
|
mutex_lock(&mgr->lock);
|
|
|
|
ctx = idr_find(&mgr->ctx_handles, id);
|
|
|
|
if (ctx)
|
|
|
|
kref_get(&ctx->refcount);
|
|
|
|
mutex_unlock(&mgr->lock);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
kref_put(&ctx->refcount, amdgpu_ctx_do_release);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-07 23:24:49 +08:00
|
|
|
|
2017-09-15 13:40:31 +08:00
|
|
|
int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
|
|
|
|
struct dma_fence *fence, uint64_t* handler)
|
2015-07-07 23:24:49 +08:00
|
|
|
{
|
|
|
|
struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
|
2015-08-19 21:00:55 +08:00
|
|
|
uint64_t seq = cring->sequence;
|
2015-07-21 15:13:53 +08:00
|
|
|
unsigned idx = 0;
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence *other = NULL;
|
2015-07-07 23:24:49 +08:00
|
|
|
|
2015-12-10 17:34:33 +08:00
|
|
|
idx = seq & (amdgpu_sched_jobs - 1);
|
2015-07-21 15:13:53 +08:00
|
|
|
other = cring->fences[idx];
|
2017-10-11 04:50:17 +08:00
|
|
|
if (other)
|
|
|
|
BUG_ON(!dma_fence_is_signaled(other));
|
2015-07-07 23:24:49 +08:00
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_get(fence);
|
2015-07-07 23:24:49 +08:00
|
|
|
|
|
|
|
spin_lock(&ctx->ring_lock);
|
|
|
|
cring->fences[idx] = fence;
|
2015-08-19 21:00:55 +08:00
|
|
|
cring->sequence++;
|
2015-07-07 23:24:49 +08:00
|
|
|
spin_unlock(&ctx->ring_lock);
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_put(other);
|
2017-09-15 13:40:31 +08:00
|
|
|
if (handler)
|
|
|
|
*handler = seq;
|
2015-07-07 23:24:49 +08:00
|
|
|
|
2017-09-15 13:40:31 +08:00
|
|
|
return 0;
|
2015-07-07 23:24:49 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
|
|
|
|
struct amdgpu_ring *ring, uint64_t seq)
|
2015-07-07 23:24:49 +08:00
|
|
|
{
|
|
|
|
struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence *fence;
|
2015-07-07 23:24:49 +08:00
|
|
|
|
|
|
|
spin_lock(&ctx->ring_lock);
|
2015-07-21 15:13:53 +08:00
|
|
|
|
2017-04-07 18:39:07 +08:00
|
|
|
if (seq == ~0ull)
|
|
|
|
seq = ctx->rings[ring->idx].sequence - 1;
|
|
|
|
|
2015-08-19 21:00:55 +08:00
|
|
|
if (seq >= cring->sequence) {
|
2015-07-07 23:24:49 +08:00
|
|
|
spin_unlock(&ctx->ring_lock);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:13:53 +08:00
|
|
|
|
2015-12-10 15:45:11 +08:00
|
|
|
if (seq + amdgpu_sched_jobs < cring->sequence) {
|
2015-07-07 23:24:49 +08:00
|
|
|
spin_unlock(&ctx->ring_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
|
2015-07-07 23:24:49 +08:00
|
|
|
spin_unlock(&ctx->ring_lock);
|
|
|
|
|
|
|
|
return fence;
|
|
|
|
}
|
2015-08-04 22:20:31 +08:00
|
|
|
|
2017-06-07 08:20:38 +08:00
|
|
|
void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
|
2017-12-07 00:49:39 +08:00
|
|
|
enum drm_sched_priority priority)
|
2017-06-07 08:20:38 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct amdgpu_device *adev = ctx->adev;
|
2017-12-07 00:49:39 +08:00
|
|
|
struct drm_sched_entity *entity;
|
2017-06-07 08:20:38 +08:00
|
|
|
struct amdgpu_ring *ring;
|
2017-12-07 00:49:39 +08:00
|
|
|
enum drm_sched_priority ctx_prio;
|
2017-06-07 08:20:38 +08:00
|
|
|
|
|
|
|
ctx->override_priority = priority;
|
|
|
|
|
2017-12-07 00:49:39 +08:00
|
|
|
ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
|
2017-06-07 08:20:38 +08:00
|
|
|
ctx->init_priority : ctx->override_priority;
|
|
|
|
|
|
|
|
for (i = 0; i < adev->num_rings; i++) {
|
|
|
|
ring = adev->rings[i];
|
|
|
|
entity = &ctx->rings[i].entity;
|
|
|
|
|
|
|
|
if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
|
|
|
|
continue;
|
|
|
|
|
2018-08-01 22:22:39 +08:00
|
|
|
drm_sched_entity_set_priority(entity, ctx_prio);
|
2017-06-07 08:20:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 04:50:17 +08:00
|
|
|
int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx, unsigned ring_id)
|
|
|
|
{
|
|
|
|
struct amdgpu_ctx_ring *cring = &ctx->rings[ring_id];
|
|
|
|
unsigned idx = cring->sequence & (amdgpu_sched_jobs - 1);
|
|
|
|
struct dma_fence *other = cring->fences[idx];
|
|
|
|
|
|
|
|
if (other) {
|
|
|
|
signed long r;
|
2018-04-30 22:04:42 +08:00
|
|
|
r = dma_fence_wait(other, true);
|
2017-10-11 04:50:17 +08:00
|
|
|
if (r < 0) {
|
2018-04-30 22:04:42 +08:00
|
|
|
if (r != -ERESTARTSYS)
|
|
|
|
DRM_ERROR("Error (%ld) waiting for fence!\n", r);
|
|
|
|
|
2017-10-11 04:50:17 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 22:20:31 +08:00
|
|
|
void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
|
|
|
|
{
|
|
|
|
mutex_init(&mgr->lock);
|
|
|
|
idr_init(&mgr->ctx_handles);
|
|
|
|
}
|
|
|
|
|
2018-06-06 00:56:26 +08:00
|
|
|
void amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr)
|
2018-04-16 10:07:02 +08:00
|
|
|
{
|
|
|
|
struct amdgpu_ctx *ctx;
|
|
|
|
struct idr *idp;
|
|
|
|
uint32_t id, i;
|
2018-05-31 03:28:52 +08:00
|
|
|
long max_wait = MAX_WAIT_SCHED_ENTITY_Q_EMPTY;
|
2018-04-16 10:07:02 +08:00
|
|
|
|
|
|
|
idp = &mgr->ctx_handles;
|
|
|
|
|
2018-05-31 03:28:52 +08:00
|
|
|
mutex_lock(&mgr->lock);
|
2018-04-16 10:07:02 +08:00
|
|
|
idr_for_each_entry(idp, ctx, id) {
|
|
|
|
|
2018-05-31 03:28:52 +08:00
|
|
|
if (!ctx->adev) {
|
|
|
|
mutex_unlock(&mgr->lock);
|
2018-04-16 10:07:02 +08:00
|
|
|
return;
|
2018-05-31 03:28:52 +08:00
|
|
|
}
|
2018-04-16 10:07:02 +08:00
|
|
|
|
2018-05-16 02:12:21 +08:00
|
|
|
for (i = 0; i < ctx->adev->num_rings; i++) {
|
|
|
|
|
|
|
|
if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
|
|
|
|
continue;
|
|
|
|
|
2018-07-20 20:21:05 +08:00
|
|
|
max_wait = drm_sched_entity_flush(&ctx->rings[i].entity,
|
|
|
|
max_wait);
|
2018-05-16 02:12:21 +08:00
|
|
|
}
|
2018-04-16 10:07:02 +08:00
|
|
|
}
|
2018-05-31 03:28:52 +08:00
|
|
|
mutex_unlock(&mgr->lock);
|
2018-04-16 10:07:02 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 00:56:26 +08:00
|
|
|
void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
|
2018-04-16 10:07:02 +08:00
|
|
|
{
|
|
|
|
struct amdgpu_ctx *ctx;
|
|
|
|
struct idr *idp;
|
|
|
|
uint32_t id, i;
|
|
|
|
|
|
|
|
idp = &mgr->ctx_handles;
|
|
|
|
|
|
|
|
idr_for_each_entry(idp, ctx, id) {
|
|
|
|
|
|
|
|
if (!ctx->adev)
|
|
|
|
return;
|
|
|
|
|
2018-05-16 02:12:21 +08:00
|
|
|
for (i = 0; i < ctx->adev->num_rings; i++) {
|
|
|
|
|
|
|
|
if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
|
|
|
|
continue;
|
|
|
|
|
2018-04-16 10:07:02 +08:00
|
|
|
if (kref_read(&ctx->refcount) == 1)
|
2018-07-20 20:21:05 +08:00
|
|
|
drm_sched_entity_fini(&ctx->rings[i].entity);
|
2018-04-16 10:07:02 +08:00
|
|
|
else
|
|
|
|
DRM_ERROR("ctx %p is still alive\n", ctx);
|
2018-05-16 02:12:21 +08:00
|
|
|
}
|
2018-04-16 10:07:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 22:20:31 +08:00
|
|
|
void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
|
|
|
|
{
|
|
|
|
struct amdgpu_ctx *ctx;
|
|
|
|
struct idr *idp;
|
|
|
|
uint32_t id;
|
|
|
|
|
2018-06-06 00:56:26 +08:00
|
|
|
amdgpu_ctx_mgr_entity_fini(mgr);
|
2018-04-16 10:07:02 +08:00
|
|
|
|
2015-08-04 22:20:31 +08:00
|
|
|
idp = &mgr->ctx_handles;
|
|
|
|
|
|
|
|
idr_for_each_entry(idp, ctx, id) {
|
2018-04-16 10:07:02 +08:00
|
|
|
if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
|
2015-08-04 22:20:31 +08:00
|
|
|
DRM_ERROR("ctx %p is still alive\n", ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
idr_destroy(&mgr->ctx_handles);
|
|
|
|
mutex_destroy(&mgr->lock);
|
|
|
|
}
|