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 	return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
144 }
145 
146 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
147 		struct drm_msm_submitqueue_query *args)
148 {
149 	size_t size = min_t(size_t, args->len, sizeof(queue->faults));
150 	int ret;
151 
152 	/* If a zero length was passed in, return the data size we expect */
153 	if (!args->len) {
154 		args->len = sizeof(queue->faults);
155 		return 0;
156 	}
157 
158 	/* Set the length to the actual size of the data */
159 	args->len = size;
160 
161 	ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
162 
163 	return ret ? -EFAULT : 0;
164 }
165 
166 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
167 		struct drm_msm_submitqueue_query *args)
168 {
169 	struct msm_gpu_submitqueue *queue;
170 	int ret = -EINVAL;
171 
172 	if (args->pad)
173 		return -EINVAL;
174 
175 	queue = msm_submitqueue_get(ctx, args->id);
176 	if (!queue)
177 		return -ENOENT;
178 
179 	if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
180 		ret = msm_submitqueue_query_faults(queue, args);
181 
182 	msm_submitqueue_put(queue);
183 
184 	return ret;
185 }
186 
187 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
188 {
189 	struct msm_gpu_submitqueue *entry;
190 
191 	if (!ctx)
192 		return 0;
193 
194 	/*
195 	 * id 0 is the "default" queue and can't be destroyed
196 	 * by the user
197 	 */
198 	if (!id)
199 		return -ENOENT;
200 
201 	write_lock(&ctx->queuelock);
202 
203 	list_for_each_entry(entry, &ctx->submitqueues, node) {
204 		if (entry->id == id) {
205 			list_del(&entry->node);
206 			write_unlock(&ctx->queuelock);
207 
208 			msm_submitqueue_put(entry);
209 			return 0;
210 		}
211 	}
212 
213 	write_unlock(&ctx->queuelock);
214 	return -ENOENT;
215 }
216 
217