1 /*
2 * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/slab.h"
28
29 #include "freedreno_ringbuffer_sp.h"
30 #include "msm_priv.h"
31
32 static int
query_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)33 query_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
34 {
35 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
36 struct drm_msm_param req = {
37 .pipe = msm_pipe->pipe,
38 .param = param,
39 };
40 int ret;
41
42 ret =
43 drmCommandWriteRead(pipe->dev->fd, DRM_MSM_GET_PARAM, &req, sizeof(req));
44 if (ret)
45 return ret;
46
47 *value = req.value;
48
49 return 0;
50 }
51
52 static int
query_queue_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)53 query_queue_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
54 {
55 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
56 struct drm_msm_submitqueue_query req = {
57 .data = VOID2U64(value),
58 .id = msm_pipe->queue_id,
59 .param = param,
60 .len = sizeof(*value),
61 };
62 int ret;
63
64 ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_QUERY, &req,
65 sizeof(req));
66 if (ret)
67 return ret;
68
69 return 0;
70 }
71
72 static int
msm_pipe_get_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t * value)73 msm_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
74 uint64_t *value)
75 {
76 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
77 switch (param) {
78 case FD_DEVICE_ID: // XXX probably get rid of this..
79 case FD_GPU_ID:
80 *value = msm_pipe->gpu_id;
81 return 0;
82 case FD_GMEM_SIZE:
83 *value = msm_pipe->gmem;
84 return 0;
85 case FD_GMEM_BASE:
86 *value = msm_pipe->gmem_base;
87 return 0;
88 case FD_CHIP_ID:
89 *value = msm_pipe->chip_id;
90 return 0;
91 case FD_MAX_FREQ:
92 return query_param(pipe, MSM_PARAM_MAX_FREQ, value);
93 case FD_TIMESTAMP:
94 return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
95 case FD_NR_RINGS:
96 return query_param(pipe, MSM_PARAM_NR_RINGS, value);
97 case FD_CTX_FAULTS:
98 return query_queue_param(pipe, MSM_SUBMITQUEUE_PARAM_FAULTS, value);
99 case FD_GLOBAL_FAULTS:
100 return query_param(pipe, MSM_PARAM_FAULTS, value);
101 case FD_SUSPEND_COUNT:
102 return query_param(pipe, MSM_PARAM_SUSPENDS, value);
103 default:
104 ERROR_MSG("invalid param id: %d", param);
105 return -1;
106 }
107 }
108
109 static int
set_param(struct fd_pipe * pipe,uint32_t param,uint64_t value)110 set_param(struct fd_pipe *pipe, uint32_t param, uint64_t value)
111 {
112 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
113 struct drm_msm_param req = {
114 .pipe = msm_pipe->pipe,
115 .param = param,
116 .value = value,
117 };
118
119 return drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SET_PARAM,
120 &req, sizeof(req));
121 }
122
123 static int
msm_pipe_set_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t value)124 msm_pipe_set_param(struct fd_pipe *pipe, enum fd_param_id param, uint64_t value)
125 {
126 switch (param) {
127 case FD_SYSPROF:
128 return set_param(pipe, MSM_PARAM_SYSPROF, value);
129 default:
130 ERROR_MSG("invalid param id: %d", param);
131 return -1;
132 }
133 }
134
135 static int
msm_pipe_wait(struct fd_pipe * pipe,const struct fd_fence * fence,uint64_t timeout)136 msm_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence, uint64_t timeout)
137 {
138 struct fd_device *dev = pipe->dev;
139 struct drm_msm_wait_fence req = {
140 .fence = fence->kfence,
141 .queueid = to_msm_pipe(pipe)->queue_id,
142 };
143 int ret;
144
145 get_abs_timeout(&req.timeout, timeout);
146
147 ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
148 if (ret && (ret != -ETIMEDOUT)) {
149 ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
150 }
151
152 return ret;
153 }
154
155 static int
open_submitqueue(struct fd_pipe * pipe,uint32_t prio)156 open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
157 {
158 struct drm_msm_submitqueue req = {
159 .flags = 0,
160 .prio = prio,
161 };
162 uint64_t nr_rings = 1;
163 int ret;
164
165 if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES) {
166 to_msm_pipe(pipe)->queue_id = 0;
167 return 0;
168 }
169
170 msm_pipe_get_param(pipe, FD_NR_RINGS, &nr_rings);
171
172 req.prio = MIN2(req.prio, MAX2(nr_rings, 1) - 1);
173
174 ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_NEW, &req,
175 sizeof(req));
176 if (ret) {
177 ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
178 return ret;
179 }
180
181 to_msm_pipe(pipe)->queue_id = req.id;
182 return 0;
183 }
184
185 static void
close_submitqueue(struct fd_pipe * pipe,uint32_t queue_id)186 close_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
187 {
188 if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES)
189 return;
190
191 drmCommandWrite(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_CLOSE, &queue_id,
192 sizeof(queue_id));
193 }
194
195 static void
msm_pipe_destroy(struct fd_pipe * pipe)196 msm_pipe_destroy(struct fd_pipe *pipe)
197 {
198 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
199
200 close_submitqueue(pipe, msm_pipe->queue_id);
201 fd_pipe_sp_ringpool_fini(pipe);
202 free(msm_pipe);
203 }
204
205 static const struct fd_pipe_funcs sp_funcs = {
206 .ringbuffer_new_object = fd_ringbuffer_sp_new_object,
207 .submit_new = msm_submit_sp_new,
208 .flush = fd_pipe_sp_flush,
209 .get_param = msm_pipe_get_param,
210 .set_param = msm_pipe_set_param,
211 .wait = msm_pipe_wait,
212 .destroy = msm_pipe_destroy,
213 };
214
215 static const struct fd_pipe_funcs legacy_funcs = {
216 .ringbuffer_new_object = msm_ringbuffer_new_object,
217 .submit_new = msm_submit_new,
218 .get_param = msm_pipe_get_param,
219 .set_param = msm_pipe_set_param,
220 .wait = msm_pipe_wait,
221 .destroy = msm_pipe_destroy,
222 };
223
224 static uint64_t
get_param(struct fd_pipe * pipe,uint32_t param)225 get_param(struct fd_pipe *pipe, uint32_t param)
226 {
227 uint64_t value;
228 int ret = query_param(pipe, param, &value);
229 if (ret) {
230 ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
231 return 0;
232 }
233 return value;
234 }
235
236 struct fd_pipe *
msm_pipe_new(struct fd_device * dev,enum fd_pipe_id id,uint32_t prio)237 msm_pipe_new(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
238 {
239 static const uint32_t pipe_id[] = {
240 [FD_PIPE_3D] = MSM_PIPE_3D0,
241 [FD_PIPE_2D] = MSM_PIPE_2D0,
242 };
243 struct msm_pipe *msm_pipe = NULL;
244 struct fd_pipe *pipe = NULL;
245
246 msm_pipe = calloc(1, sizeof(*msm_pipe));
247 if (!msm_pipe) {
248 ERROR_MSG("allocation failed");
249 goto fail;
250 }
251
252 pipe = &msm_pipe->base;
253
254 if (fd_device_version(dev) >= FD_VERSION_SOFTPIN) {
255 pipe->funcs = &sp_funcs;
256 } else {
257 pipe->funcs = &legacy_funcs;
258 }
259
260 /* initialize before get_param(): */
261 pipe->dev = dev;
262 msm_pipe->pipe = pipe_id[id];
263
264 /* these params should be supported since the first version of drm/msm: */
265 msm_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
266 msm_pipe->gmem = get_param(pipe, MSM_PARAM_GMEM_SIZE);
267 msm_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
268
269 if (fd_device_version(pipe->dev) >= FD_VERSION_GMEM_BASE)
270 msm_pipe->gmem_base = get_param(pipe, MSM_PARAM_GMEM_BASE);
271
272 if (!(msm_pipe->gpu_id || msm_pipe->chip_id))
273 goto fail;
274
275 INFO_MSG("Pipe Info:");
276 INFO_MSG(" GPU-id: %d", msm_pipe->gpu_id);
277 INFO_MSG(" Chip-id: 0x%016"PRIx64, msm_pipe->chip_id);
278 INFO_MSG(" GMEM size: 0x%08x", msm_pipe->gmem);
279
280 if (open_submitqueue(pipe, prio))
281 goto fail;
282
283 fd_pipe_sp_ringpool_init(pipe);
284
285 return pipe;
286 fail:
287 if (pipe)
288 fd_pipe_del(pipe);
289 return NULL;
290 }
291