Merge tag 'powerpc-5.15-1' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux-2.6-microblaze.git] / drivers / gpu / drm / msm / msm_submitqueue.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 The Linux Foundation. All rights reserved.
3  */
4
5 #include <linux/kref.h>
6 #include <linux/uaccess.h>
7
8 #include "msm_gpu.h"
9
10 void msm_submitqueue_destroy(struct kref *kref)
11 {
12         struct msm_gpu_submitqueue *queue = container_of(kref,
13                 struct msm_gpu_submitqueue, ref);
14
15         idr_destroy(&queue->fence_idr);
16
17         drm_sched_entity_destroy(&queue->entity);
18
19         msm_file_private_put(queue->ctx);
20
21         kfree(queue);
22 }
23
24 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
25                 u32 id)
26 {
27         struct msm_gpu_submitqueue *entry;
28
29         if (!ctx)
30                 return NULL;
31
32         read_lock(&ctx->queuelock);
33
34         list_for_each_entry(entry, &ctx->submitqueues, node) {
35                 if (entry->id == id) {
36                         kref_get(&entry->ref);
37                         read_unlock(&ctx->queuelock);
38
39                         return entry;
40                 }
41         }
42
43         read_unlock(&ctx->queuelock);
44         return NULL;
45 }
46
47 void msm_submitqueue_close(struct msm_file_private *ctx)
48 {
49         struct msm_gpu_submitqueue *entry, *tmp;
50
51         if (!ctx)
52                 return;
53
54         /*
55          * No lock needed in close and there won't
56          * be any more user ioctls coming our way
57          */
58         list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node) {
59                 list_del(&entry->node);
60                 msm_submitqueue_put(entry);
61         }
62 }
63
64 int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
65                 u32 prio, u32 flags, u32 *id)
66 {
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;
72         unsigned ring_nr;
73         int ret;
74
75         if (!ctx)
76                 return -ENODEV;
77
78         if (!priv->gpu)
79                 return -ENODEV;
80
81         ret = msm_gpu_convert_priority(priv->gpu, prio, &ring_nr, &sched_prio);
82         if (ret)
83                 return ret;
84
85         queue = kzalloc(sizeof(*queue), GFP_KERNEL);
86
87         if (!queue)
88                 return -ENOMEM;
89
90         kref_init(&queue->ref);
91         queue->flags = flags;
92         queue->ring_nr = ring_nr;
93
94         ring = priv->gpu->rb[ring_nr];
95         sched = &ring->sched;
96
97         ret = drm_sched_entity_init(&queue->entity,
98                         sched_prio, &sched, 1, NULL);
99         if (ret) {
100                 kfree(queue);
101                 return ret;
102         }
103
104         write_lock(&ctx->queuelock);
105
106         queue->ctx = msm_file_private_get(ctx);
107         queue->id = ctx->queueid++;
108
109         if (id)
110                 *id = queue->id;
111
112         idr_init(&queue->fence_idr);
113         mutex_init(&queue->lock);
114
115         list_add_tail(&queue->node, &ctx->submitqueues);
116
117         write_unlock(&ctx->queuelock);
118
119         return 0;
120 }
121
122 /*
123  * Create the default submit-queue (id==0), used for backwards compatibility
124  * for userspace that pre-dates the introduction of submitqueues.
125  */
126 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
127 {
128         struct msm_drm_private *priv = drm->dev_private;
129         int default_prio, max_priority;
130
131         if (!priv->gpu)
132                 return -ENODEV;
133
134         max_priority = (priv->gpu->nr_rings * NR_SCHED_PRIORITIES) - 1;
135
136         /*
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.
140          */
141         default_prio = DIV_ROUND_UP(max_priority, 2);
142
143         INIT_LIST_HEAD(&ctx->submitqueues);
144
145         rwlock_init(&ctx->queuelock);
146
147         return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
148 }
149
150 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
151                 struct drm_msm_submitqueue_query *args)
152 {
153         size_t size = min_t(size_t, args->len, sizeof(queue->faults));
154         int ret;
155
156         /* If a zero length was passed in, return the data size we expect */
157         if (!args->len) {
158                 args->len = sizeof(queue->faults);
159                 return 0;
160         }
161
162         /* Set the length to the actual size of the data */
163         args->len = size;
164
165         ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
166
167         return ret ? -EFAULT : 0;
168 }
169
170 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
171                 struct drm_msm_submitqueue_query *args)
172 {
173         struct msm_gpu_submitqueue *queue;
174         int ret = -EINVAL;
175
176         if (args->pad)
177                 return -EINVAL;
178
179         queue = msm_submitqueue_get(ctx, args->id);
180         if (!queue)
181                 return -ENOENT;
182
183         if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
184                 ret = msm_submitqueue_query_faults(queue, args);
185
186         msm_submitqueue_put(queue);
187
188         return ret;
189 }
190
191 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
192 {
193         struct msm_gpu_submitqueue *entry;
194
195         if (!ctx)
196                 return 0;
197
198         /*
199          * id 0 is the "default" queue and can't be destroyed
200          * by the user
201          */
202         if (!id)
203                 return -ENOENT;
204
205         write_lock(&ctx->queuelock);
206
207         list_for_each_entry(entry, &ctx->submitqueues, node) {
208                 if (entry->id == id) {
209                         list_del(&entry->node);
210                         write_unlock(&ctx->queuelock);
211
212                         msm_submitqueue_put(entry);
213                         return 0;
214                 }
215         }
216
217         write_unlock(&ctx->queuelock);
218         return -ENOENT;
219 }
220