2019-05-29 22:17:58 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-10-21 01:06:55 +08:00
|
|
|
/* Copyright (c) 2017 The Linux Foundation. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kref.h>
|
2019-08-04 14:55:51 +08:00
|
|
|
#include <linux/uaccess.h>
|
|
|
|
|
2017-10-21 01:06:55 +08:00
|
|
|
#include "msm_gpu.h"
|
|
|
|
|
2022-03-04 08:52:16 +08:00
|
|
|
int msm_file_private_set_sysprof(struct msm_file_private *ctx,
|
|
|
|
struct msm_gpu *gpu, int sysprof)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Since pm_runtime and sysprof_active are both refcounts, we
|
|
|
|
* call apply the new value first, and then unwind the previous
|
|
|
|
* value
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (sysprof) {
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
case 2:
|
|
|
|
pm_runtime_get_sync(&gpu->pdev->dev);
|
|
|
|
fallthrough;
|
|
|
|
case 1:
|
|
|
|
refcount_inc(&gpu->sysprof_active);
|
|
|
|
fallthrough;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unwind old value: */
|
|
|
|
switch (ctx->sysprof) {
|
|
|
|
case 2:
|
|
|
|
pm_runtime_put_autosuspend(&gpu->pdev->dev);
|
|
|
|
fallthrough;
|
|
|
|
case 1:
|
|
|
|
refcount_dec(&gpu->sysprof_active);
|
|
|
|
fallthrough;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->sysprof = sysprof;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-02 00:42:05 +08:00
|
|
|
void __msm_file_private_destroy(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct msm_file_private *ctx = container_of(kref,
|
|
|
|
struct msm_file_private, ref);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(ctx->entities); i++) {
|
|
|
|
if (!ctx->entities[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
drm_sched_entity_destroy(ctx->entities[i]);
|
|
|
|
kfree(ctx->entities[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
msm_gem_address_space_put(ctx->aspace);
|
2022-03-18 00:51:40 +08:00
|
|
|
kfree(ctx->comm);
|
|
|
|
kfree(ctx->cmdline);
|
2021-10-02 00:42:05 +08:00
|
|
|
kfree(ctx);
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:06:55 +08:00
|
|
|
void msm_submitqueue_destroy(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct msm_gpu_submitqueue *queue = container_of(kref,
|
|
|
|
struct msm_gpu_submitqueue, ref);
|
|
|
|
|
2021-07-28 09:06:12 +08:00
|
|
|
idr_destroy(&queue->fence_idr);
|
|
|
|
|
2020-08-18 06:01:36 +08:00
|
|
|
msm_file_private_put(queue->ctx);
|
|
|
|
|
2017-10-21 01:06:55 +08:00
|
|
|
kfree(queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
|
|
|
|
u32 id)
|
|
|
|
{
|
|
|
|
struct msm_gpu_submitqueue *entry;
|
|
|
|
|
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
read_lock(&ctx->queuelock);
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &ctx->submitqueues, node) {
|
|
|
|
if (entry->id == id) {
|
|
|
|
kref_get(&entry->ref);
|
|
|
|
read_unlock(&ctx->queuelock);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
read_unlock(&ctx->queuelock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void msm_submitqueue_close(struct msm_file_private *ctx)
|
|
|
|
{
|
|
|
|
struct msm_gpu_submitqueue *entry, *tmp;
|
|
|
|
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No lock needed in close and there won't
|
|
|
|
* be any more user ioctls coming our way
|
|
|
|
*/
|
2020-08-18 06:01:26 +08:00
|
|
|
list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node) {
|
|
|
|
list_del(&entry->node);
|
2017-10-21 01:06:55 +08:00
|
|
|
msm_submitqueue_put(entry);
|
2020-08-18 06:01:26 +08:00
|
|
|
}
|
2017-10-21 01:06:55 +08:00
|
|
|
}
|
|
|
|
|
2021-10-02 00:42:05 +08:00
|
|
|
static struct drm_sched_entity *
|
|
|
|
get_sched_entity(struct msm_file_private *ctx, struct msm_ringbuffer *ring,
|
|
|
|
unsigned ring_nr, enum drm_sched_priority sched_prio)
|
|
|
|
{
|
|
|
|
static DEFINE_MUTEX(entity_lock);
|
|
|
|
unsigned idx = (ring_nr * NR_SCHED_PRIORITIES) + sched_prio;
|
|
|
|
|
|
|
|
/* We should have already validated that the requested priority is
|
|
|
|
* valid by the time we get here.
|
|
|
|
*/
|
|
|
|
if (WARN_ON(idx >= ARRAY_SIZE(ctx->entities)))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
mutex_lock(&entity_lock);
|
|
|
|
|
|
|
|
if (!ctx->entities[idx]) {
|
|
|
|
struct drm_sched_entity *entity;
|
|
|
|
struct drm_gpu_scheduler *sched = &ring->sched;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
entity = kzalloc(sizeof(*ctx->entities[idx]), GFP_KERNEL);
|
|
|
|
|
|
|
|
ret = drm_sched_entity_init(entity, sched_prio, &sched, 1, NULL);
|
|
|
|
if (ret) {
|
2021-10-11 20:40:05 +08:00
|
|
|
mutex_unlock(&entity_lock);
|
2021-10-02 00:42:05 +08:00
|
|
|
kfree(entity);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->entities[idx] = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&entity_lock);
|
|
|
|
|
|
|
|
return ctx->entities[idx];
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:06:57 +08:00
|
|
|
int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
|
|
|
|
u32 prio, u32 flags, u32 *id)
|
2017-10-21 01:06:55 +08:00
|
|
|
{
|
2017-10-21 01:06:57 +08:00
|
|
|
struct msm_drm_private *priv = drm->dev_private;
|
2017-10-21 01:06:55 +08:00
|
|
|
struct msm_gpu_submitqueue *queue;
|
2021-07-28 09:06:17 +08:00
|
|
|
enum drm_sched_priority sched_prio;
|
|
|
|
unsigned ring_nr;
|
2021-07-28 09:06:14 +08:00
|
|
|
int ret;
|
2017-10-21 01:06:55 +08:00
|
|
|
|
|
|
|
if (!ctx)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2021-07-28 09:06:07 +08:00
|
|
|
if (!priv->gpu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2021-07-28 09:06:17 +08:00
|
|
|
ret = msm_gpu_convert_priority(priv->gpu, prio, &ring_nr, &sched_prio);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2021-07-28 09:06:07 +08:00
|
|
|
|
2017-10-21 01:06:55 +08:00
|
|
|
queue = kzalloc(sizeof(*queue), GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!queue)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
kref_init(&queue->ref);
|
|
|
|
queue->flags = flags;
|
2021-07-28 09:06:17 +08:00
|
|
|
queue->ring_nr = ring_nr;
|
2017-10-21 01:06:55 +08:00
|
|
|
|
2021-10-02 00:42:05 +08:00
|
|
|
queue->entity = get_sched_entity(ctx, priv->gpu->rb[ring_nr],
|
|
|
|
ring_nr, sched_prio);
|
|
|
|
if (IS_ERR(queue->entity)) {
|
|
|
|
ret = PTR_ERR(queue->entity);
|
2021-07-28 09:06:14 +08:00
|
|
|
kfree(queue);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:06:55 +08:00
|
|
|
write_lock(&ctx->queuelock);
|
|
|
|
|
2020-08-18 06:01:36 +08:00
|
|
|
queue->ctx = msm_file_private_get(ctx);
|
2017-10-21 01:06:55 +08:00
|
|
|
queue->id = ctx->queueid++;
|
|
|
|
|
|
|
|
if (id)
|
|
|
|
*id = queue->id;
|
|
|
|
|
2021-07-28 09:06:12 +08:00
|
|
|
idr_init(&queue->fence_idr);
|
2022-08-02 23:51:36 +08:00
|
|
|
mutex_init(&queue->idr_lock);
|
2021-07-28 09:06:12 +08:00
|
|
|
mutex_init(&queue->lock);
|
|
|
|
|
2017-10-21 01:06:55 +08:00
|
|
|
list_add_tail(&queue->node, &ctx->submitqueues);
|
|
|
|
|
|
|
|
write_unlock(&ctx->queuelock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:06:06 +08:00
|
|
|
/*
|
|
|
|
* Create the default submit-queue (id==0), used for backwards compatibility
|
|
|
|
* for userspace that pre-dates the introduction of submitqueues.
|
|
|
|
*/
|
2017-10-21 01:06:57 +08:00
|
|
|
int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
|
2017-10-21 01:06:55 +08:00
|
|
|
{
|
2017-10-21 01:06:57 +08:00
|
|
|
struct msm_drm_private *priv = drm->dev_private;
|
2021-07-28 09:06:17 +08:00
|
|
|
int default_prio, max_priority;
|
2017-10-21 01:06:57 +08:00
|
|
|
|
2021-07-28 09:06:07 +08:00
|
|
|
if (!priv->gpu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2021-07-28 09:06:17 +08:00
|
|
|
max_priority = (priv->gpu->nr_rings * NR_SCHED_PRIORITIES) - 1;
|
|
|
|
|
2017-10-21 01:06:57 +08:00
|
|
|
/*
|
2021-07-28 09:06:17 +08:00
|
|
|
* Pick a medium priority level as default. Lower numeric value is
|
|
|
|
* higher priority, so round-up to pick a priority that is not higher
|
|
|
|
* than the middle priority level.
|
2017-10-21 01:06:57 +08:00
|
|
|
*/
|
2021-07-28 09:06:17 +08:00
|
|
|
default_prio = DIV_ROUND_UP(max_priority, 2);
|
2017-10-21 01:06:57 +08:00
|
|
|
|
|
|
|
return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
|
2017-10-21 01:06:55 +08:00
|
|
|
}
|
|
|
|
|
2019-03-23 04:21:22 +08:00
|
|
|
static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
|
|
|
|
struct drm_msm_submitqueue_query *args)
|
|
|
|
{
|
|
|
|
size_t size = min_t(size_t, args->len, sizeof(queue->faults));
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* If a zero length was passed in, return the data size we expect */
|
|
|
|
if (!args->len) {
|
|
|
|
args->len = sizeof(queue->faults);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the length to the actual size of the data */
|
|
|
|
args->len = size;
|
|
|
|
|
|
|
|
ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
|
|
|
|
|
|
|
|
return ret ? -EFAULT : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
|
|
|
|
struct drm_msm_submitqueue_query *args)
|
|
|
|
{
|
|
|
|
struct msm_gpu_submitqueue *queue;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
if (args->pad)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
queue = msm_submitqueue_get(ctx, args->id);
|
|
|
|
if (!queue)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
|
|
|
|
ret = msm_submitqueue_query_faults(queue, args);
|
|
|
|
|
|
|
|
msm_submitqueue_put(queue);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:06:55 +08:00
|
|
|
int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
|
|
|
|
{
|
|
|
|
struct msm_gpu_submitqueue *entry;
|
|
|
|
|
|
|
|
if (!ctx)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* id 0 is the "default" queue and can't be destroyed
|
|
|
|
* by the user
|
|
|
|
*/
|
|
|
|
if (!id)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
write_lock(&ctx->queuelock);
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &ctx->submitqueues, node) {
|
|
|
|
if (entry->id == id) {
|
|
|
|
list_del(&entry->node);
|
|
|
|
write_unlock(&ctx->queuelock);
|
|
|
|
|
|
|
|
msm_submitqueue_put(entry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write_unlock(&ctx->queuelock);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|