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