1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 The Linux Foundation. All rights reserved.
5 #include <linux/kref.h>
8 void msm_submitqueue_destroy(struct kref *kref)
10 struct msm_gpu_submitqueue *queue = container_of(kref,
11 struct msm_gpu_submitqueue, ref);
16 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
19 struct msm_gpu_submitqueue *entry;
24 read_lock(&ctx->queuelock);
26 list_for_each_entry(entry, &ctx->submitqueues, node) {
27 if (entry->id == id) {
28 kref_get(&entry->ref);
29 read_unlock(&ctx->queuelock);
35 read_unlock(&ctx->queuelock);
39 void msm_submitqueue_close(struct msm_file_private *ctx)
41 struct msm_gpu_submitqueue *entry, *tmp;
47 * No lock needed in close and there won't
48 * be any more user ioctls coming our way
50 list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node)
51 msm_submitqueue_put(entry);
54 int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
55 u32 prio, u32 flags, u32 *id)
57 struct msm_drm_private *priv = drm->dev_private;
58 struct msm_gpu_submitqueue *queue;
63 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
68 kref_init(&queue->ref);
72 if (prio >= priv->gpu->nr_rings)
78 write_lock(&ctx->queuelock);
80 queue->id = ctx->queueid++;
85 list_add_tail(&queue->node, &ctx->submitqueues);
87 write_unlock(&ctx->queuelock);
92 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
94 struct msm_drm_private *priv = drm->dev_private;
101 * Select priority 2 as the "default priority" unless nr_rings is less
102 * than 2 and then pick the lowest pirority
104 default_prio = priv->gpu ?
105 clamp_t(uint32_t, 2, 0, priv->gpu->nr_rings - 1) : 0;
107 INIT_LIST_HEAD(&ctx->submitqueues);
109 rwlock_init(&ctx->queuelock);
111 return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
114 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
115 struct drm_msm_submitqueue_query *args)
117 size_t size = min_t(size_t, args->len, sizeof(queue->faults));
120 /* If a zero length was passed in, return the data size we expect */
122 args->len = sizeof(queue->faults);
126 /* Set the length to the actual size of the data */
129 ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
131 return ret ? -EFAULT : 0;
134 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
135 struct drm_msm_submitqueue_query *args)
137 struct msm_gpu_submitqueue *queue;
143 queue = msm_submitqueue_get(ctx, args->id);
147 if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
148 ret = msm_submitqueue_query_faults(queue, args);
150 msm_submitqueue_put(queue);
155 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
157 struct msm_gpu_submitqueue *entry;
163 * id 0 is the "default" queue and can't be destroyed
169 write_lock(&ctx->queuelock);
171 list_for_each_entry(entry, &ctx->submitqueues, node) {
172 if (entry->id == id) {
173 list_del(&entry->node);
174 write_unlock(&ctx->queuelock);
176 msm_submitqueue_put(entry);
181 write_unlock(&ctx->queuelock);