xref: /linux/drivers/gpu/drm/msm/msm_submitqueue.c (revision fdc290ff4ab19c7e0dde36c4cd1e2771b61f6bf5)
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 int msm_context_set_sysprof(struct msm_context *ctx, struct msm_gpu *gpu, int sysprof)
11 {
12 	guard(rwsem_write)(&ctx->ctxlock);
13 
14 	/*
15 	 * Since pm_runtime and sysprof_active are both refcounts, we
16 	 * call apply the new value first, and then unwind the previous
17 	 * value
18 	 */
19 
20 	switch (sysprof) {
21 	default:
22 		return UERR(EINVAL, gpu->dev, "Invalid sysprof: %d", sysprof);
23 	case 2:
24 		pm_runtime_get_sync(&gpu->pdev->dev);
25 		fallthrough;
26 	case 1:
27 		refcount_inc(&gpu->sysprof_active);
28 		fallthrough;
29 	case 0:
30 		break;
31 	}
32 
33 	/* unwind old value: */
34 	switch (ctx->sysprof) {
35 	case 2:
36 		pm_runtime_put_autosuspend(&gpu->pdev->dev);
37 		fallthrough;
38 	case 1:
39 		refcount_dec(&gpu->sysprof_active);
40 		fallthrough;
41 	case 0:
42 		break;
43 	}
44 
45 	/* Some gpu families require additional setup for sysprof */
46 	if (gpu->funcs->sysprof_setup)
47 		gpu->funcs->sysprof_setup(gpu, false);
48 
49 	ctx->sysprof = sysprof;
50 
51 	return 0;
52 }
53 
54 void __msm_context_destroy(struct kref *kref)
55 {
56 	struct msm_context *ctx = container_of(kref,
57 		struct msm_context, ref);
58 	int i;
59 
60 	for (i = 0; i < ARRAY_SIZE(ctx->entities); i++) {
61 		if (!ctx->entities[i])
62 			continue;
63 
64 		drm_sched_entity_destroy(ctx->entities[i]);
65 		kfree(ctx->entities[i]);
66 	}
67 
68 	drm_gpuvm_put(ctx->vm);
69 	kfree(ctx->comm);
70 	kfree(ctx->cmdline);
71 	kfree(ctx->perfctx);
72 	kfree(ctx);
73 }
74 
75 void msm_submitqueue_destroy(struct kref *kref)
76 {
77 	struct msm_gpu_submitqueue *queue = container_of(kref,
78 		struct msm_gpu_submitqueue, ref);
79 
80 	idr_destroy(&queue->fence_idr);
81 
82 	if (queue->entity == &queue->_vm_bind_entity[0])
83 		drm_sched_entity_destroy(queue->entity);
84 
85 	msm_context_put(queue->ctx);
86 
87 	kfree(queue);
88 }
89 
90 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_context *ctx,
91 		u32 id)
92 {
93 	struct msm_gpu_submitqueue *entry;
94 
95 	if (!ctx)
96 		return NULL;
97 
98 	guard(rwsem_read)(&ctx->ctxlock);
99 
100 	list_for_each_entry(entry, &ctx->submitqueues, node) {
101 		if (entry->id == id) {
102 			kref_get(&entry->ref);
103 			return entry;
104 		}
105 	}
106 
107 	return NULL;
108 }
109 
110 void msm_submitqueue_close(struct msm_context *ctx)
111 {
112 	struct msm_gpu_submitqueue *queue, *tmp;
113 
114 	if (!ctx)
115 		return;
116 
117 	/*
118 	 * No lock needed in close and there won't
119 	 * be any more user ioctls coming our way
120 	 */
121 	list_for_each_entry_safe(queue, tmp, &ctx->submitqueues, node) {
122 		if (queue->entity == &queue->_vm_bind_entity[0])
123 			drm_sched_entity_flush(queue->entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY);
124 		list_del(&queue->node);
125 		msm_submitqueue_put(queue);
126 	}
127 
128 	if (!ctx->vm)
129 		return;
130 
131 	msm_gem_vm_close(ctx->vm);
132 }
133 
134 static struct drm_sched_entity *
135 get_sched_entity(struct msm_context *ctx, struct msm_ringbuffer *ring,
136 		 unsigned ring_nr, enum drm_sched_priority sched_prio)
137 {
138 	static DEFINE_MUTEX(entity_lock);
139 	unsigned idx = (ring_nr * NR_SCHED_PRIORITIES) + sched_prio;
140 
141 	/* We should have already validated that the requested priority is
142 	 * valid by the time we get here.
143 	 */
144 	if (WARN_ON(idx >= ARRAY_SIZE(ctx->entities)))
145 		return ERR_PTR(-EINVAL);
146 
147 	mutex_lock(&entity_lock);
148 
149 	if (!ctx->entities[idx]) {
150 		struct drm_sched_entity *entity;
151 		struct drm_gpu_scheduler *sched = &ring->sched;
152 		int ret;
153 
154 		entity = kzalloc_obj(*ctx->entities[idx]);
155 
156 		ret = drm_sched_entity_init(entity, sched_prio, &sched, 1, NULL);
157 		if (ret) {
158 			mutex_unlock(&entity_lock);
159 			kfree(entity);
160 			return ERR_PTR(ret);
161 		}
162 
163 		ctx->entities[idx] = entity;
164 	}
165 
166 	mutex_unlock(&entity_lock);
167 
168 	return ctx->entities[idx];
169 }
170 
171 int msm_submitqueue_create(struct drm_device *drm, struct msm_context *ctx,
172 		u32 prio, u32 flags, u32 *id)
173 {
174 	struct msm_drm_private *priv = drm->dev_private;
175 	struct msm_gpu_submitqueue *queue;
176 	enum drm_sched_priority sched_prio;
177 	struct drm_gpuvm *vm = NULL;
178 	unsigned ring_nr;
179 	int ret;
180 
181 	if (!ctx)
182 		return -ENODEV;
183 
184 	if (!priv->gpu)
185 		return -ENODEV;
186 
187 	if (flags & MSM_SUBMITQUEUE_VM_BIND) {
188 		unsigned sz;
189 
190 		vm = msm_context_vm(drm, ctx);
191 
192 		if (!vm)
193 			return UERR(ENOMEM, drm, "no VM");
194 
195 		/* Not allowed for kernel managed VMs (ie. kernel allocs VA) */
196 		if (!msm_context_is_vmbind(ctx))
197 			return -EINVAL;
198 
199 		if (prio)
200 			return -EINVAL;
201 
202 		sz = struct_size(queue, _vm_bind_entity, 1);
203 		queue = kzalloc(sz, GFP_KERNEL);
204 	} else {
205 		extern int enable_preemption;
206 		bool preemption_supported =
207 			priv->gpu->nr_rings == 1 && enable_preemption != 0;
208 
209 		if (flags & MSM_SUBMITQUEUE_ALLOW_PREEMPT && preemption_supported)
210 			return -EINVAL;
211 
212 		ret = msm_gpu_convert_priority(priv->gpu, prio, &ring_nr, &sched_prio);
213 		if (ret)
214 			return ret;
215 
216 		queue = kzalloc_obj(*queue);
217 	}
218 
219 	if (!queue)
220 		return -ENOMEM;
221 
222 	kref_init(&queue->ref);
223 	queue->flags = flags;
224 
225 	if (flags & MSM_SUBMITQUEUE_VM_BIND) {
226 		struct drm_gpu_scheduler *sched = &to_msm_vm(vm)->sched;
227 
228 		queue->entity = &queue->_vm_bind_entity[0];
229 
230 		drm_sched_entity_init(queue->entity, DRM_SCHED_PRIORITY_KERNEL,
231 				      &sched, 1, NULL);
232 	} else {
233 		queue->ring_nr = ring_nr;
234 
235 		queue->entity = get_sched_entity(ctx, priv->gpu->rb[ring_nr],
236 						 ring_nr, sched_prio);
237 	}
238 
239 	if (IS_ERR(queue->entity)) {
240 		ret = PTR_ERR(queue->entity);
241 		kfree(queue);
242 		return ret;
243 	}
244 
245 	guard(rwsem_write)(&ctx->ctxlock);
246 
247 	queue->ctx = msm_context_get(ctx);
248 	queue->id = ctx->queueid++;
249 
250 	if (id)
251 		*id = queue->id;
252 
253 	idr_init(&queue->fence_idr);
254 	spin_lock_init(&queue->idr_lock);
255 	mutex_init(&queue->lock);
256 
257 	list_add_tail(&queue->node, &ctx->submitqueues);
258 
259 	return 0;
260 }
261 
262 /*
263  * Create the default submit-queue (id==0), used for backwards compatibility
264  * for userspace that pre-dates the introduction of submitqueues.
265  */
266 int msm_submitqueue_init(struct drm_device *drm, struct msm_context *ctx)
267 {
268 	struct msm_drm_private *priv = drm->dev_private;
269 	int default_prio, max_priority;
270 
271 	if (!priv->gpu)
272 		return -ENODEV;
273 
274 	max_priority = (priv->gpu->nr_rings * NR_SCHED_PRIORITIES) - 1;
275 
276 	/*
277 	 * Pick a medium priority level as default.  Lower numeric value is
278 	 * higher priority, so round-up to pick a priority that is not higher
279 	 * than the middle priority level.
280 	 */
281 	default_prio = DIV_ROUND_UP(max_priority, 2);
282 
283 	return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
284 }
285 
286 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
287 		struct drm_msm_submitqueue_query *args)
288 {
289 	size_t size = min_t(size_t, args->len, sizeof(queue->faults));
290 	int ret;
291 
292 	/* If a zero length was passed in, return the data size we expect */
293 	if (!args->len) {
294 		args->len = sizeof(queue->faults);
295 		return 0;
296 	}
297 
298 	/* Set the length to the actual size of the data */
299 	args->len = size;
300 
301 	ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
302 
303 	return ret ? -EFAULT : 0;
304 }
305 
306 int msm_submitqueue_query(struct drm_device *drm, struct msm_context *ctx,
307 		struct drm_msm_submitqueue_query *args)
308 {
309 	struct msm_gpu_submitqueue *queue;
310 	int ret = -EINVAL;
311 
312 	if (args->pad)
313 		return -EINVAL;
314 
315 	queue = msm_submitqueue_get(ctx, args->id);
316 	if (!queue)
317 		return -ENOENT;
318 
319 	if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
320 		ret = msm_submitqueue_query_faults(queue, args);
321 
322 	msm_submitqueue_put(queue);
323 
324 	return ret;
325 }
326 
327 int msm_submitqueue_remove(struct msm_context *ctx, u32 id)
328 {
329 	struct msm_gpu_submitqueue *entry;
330 
331 	if (!ctx)
332 		return 0;
333 
334 	/*
335 	 * id 0 is the "default" queue and can't be destroyed
336 	 * by the user
337 	 */
338 	if (!id)
339 		return -ENOENT;
340 
341 	guard(rwsem_write)(&ctx->ctxlock);
342 
343 	list_for_each_entry(entry, &ctx->submitqueues, node) {
344 		if (entry->id == id) {
345 			list_del(&entry->node);
346 			msm_submitqueue_put(entry);
347 			return 0;
348 		}
349 	}
350 
351 	return -ENOENT;
352 }
353 
354