1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 The Linux Foundation. All rights reserved.
5 #include <linux/kref.h>
6 #include <linux/uaccess.h>
10 void msm_submitqueue_destroy(struct kref *kref)
12 struct msm_gpu_submitqueue *queue = container_of(kref,
13 struct msm_gpu_submitqueue, ref);
15 idr_destroy(&queue->fence_idr);
17 drm_sched_entity_destroy(&queue->entity);
19 msm_file_private_put(queue->ctx);
24 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
27 struct msm_gpu_submitqueue *entry;
32 read_lock(&ctx->queuelock);
34 list_for_each_entry(entry, &ctx->submitqueues, node) {
35 if (entry->id == id) {
36 kref_get(&entry->ref);
37 read_unlock(&ctx->queuelock);
43 read_unlock(&ctx->queuelock);
47 void msm_submitqueue_close(struct msm_file_private *ctx)
49 struct msm_gpu_submitqueue *entry, *tmp;
55 * No lock needed in close and there won't
56 * be any more user ioctls coming our way
58 list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node) {
59 list_del(&entry->node);
60 msm_submitqueue_put(entry);
64 int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
65 u32 prio, u32 flags, u32 *id)
67 struct msm_drm_private *priv = drm->dev_private;
68 struct msm_gpu_submitqueue *queue;
69 struct msm_ringbuffer *ring;
70 struct drm_gpu_scheduler *sched;
71 enum drm_sched_priority sched_prio;
81 ret = msm_gpu_convert_priority(priv->gpu, prio, &ring_nr, &sched_prio);
85 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
90 kref_init(&queue->ref);
92 queue->ring_nr = ring_nr;
94 ring = priv->gpu->rb[ring_nr];
97 ret = drm_sched_entity_init(&queue->entity,
98 sched_prio, &sched, 1, NULL);
104 write_lock(&ctx->queuelock);
106 queue->ctx = msm_file_private_get(ctx);
107 queue->id = ctx->queueid++;
112 idr_init(&queue->fence_idr);
113 mutex_init(&queue->lock);
115 list_add_tail(&queue->node, &ctx->submitqueues);
117 write_unlock(&ctx->queuelock);
123 * Create the default submit-queue (id==0), used for backwards compatibility
124 * for userspace that pre-dates the introduction of submitqueues.
126 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
128 struct msm_drm_private *priv = drm->dev_private;
129 int default_prio, max_priority;
134 max_priority = (priv->gpu->nr_rings * NR_SCHED_PRIORITIES) - 1;
137 * Pick a medium priority level as default. Lower numeric value is
138 * higher priority, so round-up to pick a priority that is not higher
139 * than the middle priority level.
141 default_prio = DIV_ROUND_UP(max_priority, 2);
143 INIT_LIST_HEAD(&ctx->submitqueues);
145 rwlock_init(&ctx->queuelock);
147 return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
150 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
151 struct drm_msm_submitqueue_query *args)
153 size_t size = min_t(size_t, args->len, sizeof(queue->faults));
156 /* If a zero length was passed in, return the data size we expect */
158 args->len = sizeof(queue->faults);
162 /* Set the length to the actual size of the data */
165 ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
167 return ret ? -EFAULT : 0;
170 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
171 struct drm_msm_submitqueue_query *args)
173 struct msm_gpu_submitqueue *queue;
179 queue = msm_submitqueue_get(ctx, args->id);
183 if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
184 ret = msm_submitqueue_query_faults(queue, args);
186 msm_submitqueue_put(queue);
191 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
193 struct msm_gpu_submitqueue *entry;
199 * id 0 is the "default" queue and can't be destroyed
205 write_lock(&ctx->queuelock);
207 list_for_each_entry(entry, &ctx->submitqueues, node) {
208 if (entry->id == id) {
209 list_del(&entry->node);
210 write_unlock(&ctx->queuelock);
212 msm_submitqueue_put(entry);
217 write_unlock(&ctx->queuelock);