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 	msm_file_private_put(queue->ctx);
16 
17 	kfree(queue);
18 }
19 
20 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
21 		u32 id)
22 {
23 	struct msm_gpu_submitqueue *entry;
24 
25 	if (!ctx)
26 		return NULL;
27 
28 	read_lock(&ctx->queuelock);
29 
30 	list_for_each_entry(entry, &ctx->submitqueues, node) {
31 		if (entry->id == id) {
32 			kref_get(&entry->ref);
33 			read_unlock(&ctx->queuelock);
34 
35 			return entry;
36 		}
37 	}
38 
39 	read_unlock(&ctx->queuelock);
40 	return NULL;
41 }
42 
43 void msm_submitqueue_close(struct msm_file_private *ctx)
44 {
45 	struct msm_gpu_submitqueue *entry, *tmp;
46 
47 	if (!ctx)
48 		return;
49 
50 	/*
51 	 * No lock needed in close and there won't
52 	 * be any more user ioctls coming our way
53 	 */
54 	list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node) {
55 		list_del(&entry->node);
56 		msm_submitqueue_put(entry);
57 	}
58 }
59 
60 int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
61 		u32 prio, u32 flags, u32 *id)
62 {
63 	struct msm_drm_private *priv = drm->dev_private;
64 	struct msm_gpu_submitqueue *queue;
65 
66 	if (!ctx)
67 		return -ENODEV;
68 
69 	if (!priv->gpu)
70 		return -ENODEV;
71 
72 	if (prio >= priv->gpu->nr_rings)
73 		return -EINVAL;
74 
75 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
76 
77 	if (!queue)
78 		return -ENOMEM;
79 
80 	kref_init(&queue->ref);
81 	queue->flags = flags;
82 	queue->prio = prio;
83 
84 	write_lock(&ctx->queuelock);
85 
86 	queue->ctx = msm_file_private_get(ctx);
87 	queue->id = ctx->queueid++;
88 
89 	if (id)
90 		*id = queue->id;
91 
92 	list_add_tail(&queue->node, &ctx->submitqueues);
93 
94 	write_unlock(&ctx->queuelock);
95 
96 	return 0;
97 }
98 
99 /*
100  * Create the default submit-queue (id==0), used for backwards compatibility
101  * for userspace that pre-dates the introduction of submitqueues.
102  */
103 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
104 {
105 	struct msm_drm_private *priv = drm->dev_private;
106 	int default_prio;
107 
108 	if (!priv->gpu)
109 		return -ENODEV;
110 
111 	/*
112 	 * Select priority 2 as the "default priority" unless nr_rings is less
113 	 * than 2 and then pick the lowest priority
114 	 */
115 	default_prio = clamp_t(uint32_t, 2, 0, priv->gpu->nr_rings - 1);
116 
117 	INIT_LIST_HEAD(&ctx->submitqueues);
118 
119 	rwlock_init(&ctx->queuelock);
120 
121 	return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
122 }
123 
124 static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
125 		struct drm_msm_submitqueue_query *args)
126 {
127 	size_t size = min_t(size_t, args->len, sizeof(queue->faults));
128 	int ret;
129 
130 	/* If a zero length was passed in, return the data size we expect */
131 	if (!args->len) {
132 		args->len = sizeof(queue->faults);
133 		return 0;
134 	}
135 
136 	/* Set the length to the actual size of the data */
137 	args->len = size;
138 
139 	ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
140 
141 	return ret ? -EFAULT : 0;
142 }
143 
144 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
145 		struct drm_msm_submitqueue_query *args)
146 {
147 	struct msm_gpu_submitqueue *queue;
148 	int ret = -EINVAL;
149 
150 	if (args->pad)
151 		return -EINVAL;
152 
153 	queue = msm_submitqueue_get(ctx, args->id);
154 	if (!queue)
155 		return -ENOENT;
156 
157 	if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
158 		ret = msm_submitqueue_query_faults(queue, args);
159 
160 	msm_submitqueue_put(queue);
161 
162 	return ret;
163 }
164 
165 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
166 {
167 	struct msm_gpu_submitqueue *entry;
168 
169 	if (!ctx)
170 		return 0;
171 
172 	/*
173 	 * id 0 is the "default" queue and can't be destroyed
174 	 * by the user
175 	 */
176 	if (!id)
177 		return -ENOENT;
178 
179 	write_lock(&ctx->queuelock);
180 
181 	list_for_each_entry(entry, &ctx->submitqueues, node) {
182 		if (entry->id == id) {
183 			list_del(&entry->node);
184 			write_unlock(&ctx->queuelock);
185 
186 			msm_submitqueue_put(entry);
187 			return 0;
188 		}
189 	}
190 
191 	write_unlock(&ctx->queuelock);
192 	return -ENOENT;
193 }
194 
195