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 int msm_file_private_set_sysprof(struct msm_file_private *ctx,
11 struct msm_gpu *gpu, int sysprof)
14 * Since pm_runtime and sysprof_active are both refcounts, we
15 * call apply the new value first, and then unwind the previous
23 pm_runtime_get_sync(&gpu->pdev->dev);
26 refcount_inc(&gpu->sysprof_active);
32 /* unwind old value: */
33 switch (ctx->sysprof) {
35 pm_runtime_put_autosuspend(&gpu->pdev->dev);
38 refcount_dec(&gpu->sysprof_active);
44 ctx->sysprof = sysprof;
49 void __msm_file_private_destroy(struct kref *kref)
51 struct msm_file_private *ctx = container_of(kref,
52 struct msm_file_private, ref);
55 for (i = 0; i < ARRAY_SIZE(ctx->entities); i++) {
56 if (!ctx->entities[i])
59 drm_sched_entity_destroy(ctx->entities[i]);
60 kfree(ctx->entities[i]);
63 msm_gem_address_space_put(ctx->aspace);
69 void msm_submitqueue_destroy(struct kref *kref)
71 struct msm_gpu_submitqueue *queue = container_of(kref,
72 struct msm_gpu_submitqueue, ref);
74 idr_destroy(&queue->fence_idr);
76 msm_file_private_put(queue->ctx);
81 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
84 struct msm_gpu_submitqueue *entry;
89 read_lock(&ctx->queuelock);
91 list_for_each_entry(entry, &ctx->submitqueues, node) {
92 if (entry->id == id) {
93 kref_get(&entry->ref);
94 read_unlock(&ctx->queuelock);
100 read_unlock(&ctx->queuelock);
104 void msm_submitqueue_close(struct msm_file_private *ctx)
106 struct msm_gpu_submitqueue *entry, *tmp;
112 * No lock needed in close and there won't
113 * be any more user ioctls coming our way
115 list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node) {
116 list_del(&entry->node);
117 msm_submitqueue_put(entry);
121 static struct drm_sched_entity *
122 get_sched_entity(struct msm_file_private *ctx, struct msm_ringbuffer *ring,
123 unsigned ring_nr, enum drm_sched_priority sched_prio)
125 static DEFINE_MUTEX(entity_lock);
126 unsigned idx = (ring_nr * NR_SCHED_PRIORITIES) + sched_prio;
128 /* We should have already validated that the requested priority is
129 * valid by the time we get here.
131 if (WARN_ON(idx >= ARRAY_SIZE(ctx->entities)))
132 return ERR_PTR(-EINVAL);
134 mutex_lock(&entity_lock);
136 if (!ctx->entities[idx]) {
137 struct drm_sched_entity *entity;
138 struct drm_gpu_scheduler *sched = &ring->sched;
141 entity = kzalloc(sizeof(*ctx->entities[idx]), GFP_KERNEL);
143 ret = drm_sched_entity_init(entity, sched_prio, &sched, 1, NULL);
145 mutex_unlock(&entity_lock);
150 ctx->entities[idx] = entity;
153 mutex_unlock(&entity_lock);
155 return ctx->entities[idx];
158 int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
159 u32 prio, u32 flags, u32 *id)
161 struct msm_drm_private *priv = drm->dev_private;
162 struct msm_gpu_submitqueue *queue;
163 enum drm_sched_priority sched_prio;
173 ret = msm_gpu_convert_priority(priv->gpu, prio, &ring_nr, &sched_prio);
177 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
182 kref_init(&queue->ref);
183 queue->flags = flags;
184 queue->ring_nr = ring_nr;
186 queue->entity = get_sched_entity(ctx, priv->gpu->rb[ring_nr],
187 ring_nr, sched_prio);
188 if (IS_ERR(queue->entity)) {
189 ret = PTR_ERR(queue->entity);
194 write_lock(&ctx->queuelock);
196 queue->ctx = msm_file_private_get(ctx);
197 queue->id = ctx->queueid++;
202 idr_init(&queue->fence_idr);
203 mutex_init(&queue->idr_lock);
204 mutex_init(&queue->lock);
206 list_add_tail(&queue->node, &ctx->submitqueues);
208 write_unlock(&ctx->queuelock);
214 * Create the default submit-queue (id==0), used for backwards compatibility
215 * for userspace that pre-dates the introduction of submitqueues.
217 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
219 struct msm_drm_private *priv = drm->dev_private;
220 int default_prio, max_priority;
225 max_priority = (priv->gpu->nr_rings * NR_SCHED_PRIORITIES) - 1;
228 * Pick a medium priority level as default. Lower numeric value is
229 * higher priority, so round-up to pick a priority that is not higher
230 * than the middle priority level.
232 default_prio = DIV_ROUND_UP(max_priority, 2);
234 return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
237 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
238 struct drm_msm_submitqueue_query *args)
240 size_t size = min_t(size_t, args->len, sizeof(queue->faults));
243 /* If a zero length was passed in, return the data size we expect */
245 args->len = sizeof(queue->faults);
249 /* Set the length to the actual size of the data */
252 ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
254 return ret ? -EFAULT : 0;
257 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
258 struct drm_msm_submitqueue_query *args)
260 struct msm_gpu_submitqueue *queue;
266 queue = msm_submitqueue_get(ctx, args->id);
270 if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
271 ret = msm_submitqueue_query_faults(queue, args);
273 msm_submitqueue_put(queue);
278 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
280 struct msm_gpu_submitqueue *entry;
286 * id 0 is the "default" queue and can't be destroyed
292 write_lock(&ctx->queuelock);
294 list_for_each_entry(entry, &ctx->submitqueues, node) {
295 if (entry->id == id) {
296 list_del(&entry->node);
297 write_unlock(&ctx->queuelock);
299 msm_submitqueue_put(entry);
304 write_unlock(&ctx->queuelock);