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 	int ret;
72 
73 	if (!ctx)
74 		return -ENODEV;
75 
76 	if (!priv->gpu)
77 		return -ENODEV;
78 
79 	if (prio >= priv->gpu->nr_rings)
80 		return -EINVAL;
81 
82 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
83 
84 	if (!queue)
85 		return -ENOMEM;
86 
87 	kref_init(&queue->ref);
88 	queue->flags = flags;
89 	queue->prio = prio;
90 
91 	ring = priv->gpu->rb[prio];
92 	sched = &ring->sched;
93 
94 	/*
95 	 * TODO we can allow more priorities than we have ringbuffers by
96 	 * mapping:
97 	 *
98 	 *    ring = prio / 3;
99 	 *    ent_prio = DRM_SCHED_PRIORITY_MIN + (prio % 3);
100 	 *
101 	 * Probably avoid using DRM_SCHED_PRIORITY_KERNEL as that is
102 	 * treated specially in places.
103 	 */
104 	ret = drm_sched_entity_init(&queue->entity,
105 			DRM_SCHED_PRIORITY_NORMAL,
106 			&sched, 1, NULL);
107 	if (ret) {
108 		kfree(queue);
109 		return ret;
110 	}
111 
112 	write_lock(&ctx->queuelock);
113 
114 	queue->ctx = msm_file_private_get(ctx);
115 	queue->id = ctx->queueid++;
116 
117 	if (id)
118 		*id = queue->id;
119 
120 	idr_init(&queue->fence_idr);
121 	mutex_init(&queue->lock);
122 
123 	list_add_tail(&queue->node, &ctx->submitqueues);
124 
125 	write_unlock(&ctx->queuelock);
126 
127 	return 0;
128 }
129 
130 /*
131  * Create the default submit-queue (id==0), used for backwards compatibility
132  * for userspace that pre-dates the introduction of submitqueues.
133  */
134 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
135 {
136 	struct msm_drm_private *priv = drm->dev_private;
137 	int default_prio;
138 
139 	if (!priv->gpu)
140 		return -ENODEV;
141 
142 	/*
143 	 * Select priority 2 as the "default priority" unless nr_rings is less
144 	 * than 2 and then pick the lowest priority
145 	 */
146 	default_prio = clamp_t(uint32_t, 2, 0, priv->gpu->nr_rings - 1);
147 
148 	INIT_LIST_HEAD(&ctx->submitqueues);
149 
150 	rwlock_init(&ctx->queuelock);
151 
152 	return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
153 }
154 
155 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
156 		struct drm_msm_submitqueue_query *args)
157 {
158 	size_t size = min_t(size_t, args->len, sizeof(queue->faults));
159 	int ret;
160 
161 	/* If a zero length was passed in, return the data size we expect */
162 	if (!args->len) {
163 		args->len = sizeof(queue->faults);
164 		return 0;
165 	}
166 
167 	/* Set the length to the actual size of the data */
168 	args->len = size;
169 
170 	ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
171 
172 	return ret ? -EFAULT : 0;
173 }
174 
175 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
176 		struct drm_msm_submitqueue_query *args)
177 {
178 	struct msm_gpu_submitqueue *queue;
179 	int ret = -EINVAL;
180 
181 	if (args->pad)
182 		return -EINVAL;
183 
184 	queue = msm_submitqueue_get(ctx, args->id);
185 	if (!queue)
186 		return -ENOENT;
187 
188 	if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
189 		ret = msm_submitqueue_query_faults(queue, args);
190 
191 	msm_submitqueue_put(queue);
192 
193 	return ret;
194 }
195 
196 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
197 {
198 	struct msm_gpu_submitqueue *entry;
199 
200 	if (!ctx)
201 		return 0;
202 
203 	/*
204 	 * id 0 is the "default" queue and can't be destroyed
205 	 * by the user
206 	 */
207 	if (!id)
208 		return -ENOENT;
209 
210 	write_lock(&ctx->queuelock);
211 
212 	list_for_each_entry(entry, &ctx->submitqueues, node) {
213 		if (entry->id == id) {
214 			list_del(&entry->node);
215 			write_unlock(&ctx->queuelock);
216 
217 			msm_submitqueue_put(entry);
218 			return 0;
219 		}
220 	}
221 
222 	write_unlock(&ctx->queuelock);
223 	return -ENOENT;
224 }
225 
226