1 /*
2 * Copyright © 2012-2018 Rob Clark <robclark@freedesktop.org>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <robclark@freedesktop.org>
7 */
8
9 #include "util/slab.h"
10
11 #include "freedreno_ringbuffer_sp.h"
12 #include "msm_priv.h"
13
14 static int
query_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)15 query_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
16 {
17 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
18 struct drm_msm_param req = {
19 .pipe = msm_pipe->pipe,
20 .param = param,
21 };
22 int ret;
23
24 ret =
25 drmCommandWriteRead(pipe->dev->fd, DRM_MSM_GET_PARAM, &req, sizeof(req));
26 if (ret)
27 return ret;
28
29 *value = req.value;
30
31 return 0;
32 }
33
34 static int
query_queue_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)35 query_queue_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
36 {
37 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
38 struct drm_msm_submitqueue_query req = {
39 .data = VOID2U64(value),
40 .id = msm_pipe->queue_id,
41 .param = param,
42 .len = sizeof(*value),
43 };
44 int ret;
45
46 ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_QUERY, &req,
47 sizeof(req));
48 if (ret)
49 return ret;
50
51 return 0;
52 }
53
54 static int
msm_pipe_get_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t * value)55 msm_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
56 uint64_t *value)
57 {
58 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
59 switch (param) {
60 case FD_DEVICE_ID: // XXX probably get rid of this..
61 case FD_GPU_ID:
62 *value = msm_pipe->gpu_id;
63 return 0;
64 case FD_GMEM_SIZE:
65 *value = msm_pipe->gmem;
66 return 0;
67 case FD_GMEM_BASE:
68 *value = msm_pipe->gmem_base;
69 return 0;
70 case FD_CHIP_ID:
71 *value = msm_pipe->chip_id;
72 return 0;
73 case FD_MAX_FREQ:
74 return query_param(pipe, MSM_PARAM_MAX_FREQ, value);
75 case FD_TIMESTAMP:
76 return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
77 case FD_NR_PRIORITIES:
78 return query_param(pipe, MSM_PARAM_PRIORITIES, value);
79 case FD_CTX_FAULTS:
80 return query_queue_param(pipe, MSM_SUBMITQUEUE_PARAM_FAULTS, value);
81 case FD_GLOBAL_FAULTS:
82 return query_param(pipe, MSM_PARAM_FAULTS, value);
83 case FD_SUSPEND_COUNT:
84 return query_param(pipe, MSM_PARAM_SUSPENDS, value);
85 case FD_VA_SIZE:
86 return query_param(pipe, MSM_PARAM_VA_SIZE, value);
87 default:
88 ERROR_MSG("invalid param id: %d", param);
89 return -1;
90 }
91 }
92
93 static int
set_param(struct fd_pipe * pipe,uint32_t param,uint64_t value)94 set_param(struct fd_pipe *pipe, uint32_t param, uint64_t value)
95 {
96 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
97 struct drm_msm_param req = {
98 .pipe = msm_pipe->pipe,
99 .param = param,
100 .value = value,
101 };
102
103 return drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SET_PARAM,
104 &req, sizeof(req));
105 }
106
107 static int
msm_pipe_set_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t value)108 msm_pipe_set_param(struct fd_pipe *pipe, enum fd_param_id param, uint64_t value)
109 {
110 switch (param) {
111 case FD_SYSPROF:
112 return set_param(pipe, MSM_PARAM_SYSPROF, value);
113 default:
114 ERROR_MSG("invalid param id: %d", param);
115 return -1;
116 }
117 }
118
119 static int
msm_pipe_wait(struct fd_pipe * pipe,const struct fd_fence * fence,uint64_t timeout)120 msm_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence, uint64_t timeout)
121 {
122 struct fd_device *dev = pipe->dev;
123 struct drm_msm_wait_fence req = {
124 .fence = fence->kfence,
125 .queueid = to_msm_pipe(pipe)->queue_id,
126 };
127 int ret;
128
129 get_abs_timeout(&req.timeout, timeout);
130
131 ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
132 if (ret && (ret != -ETIMEDOUT)) {
133 ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
134 }
135
136 return ret;
137 }
138
139 static int
__open_submitqueue(struct fd_pipe * pipe,uint32_t prio,uint32_t flags)140 __open_submitqueue(struct fd_pipe *pipe, uint32_t prio, uint32_t flags)
141 {
142 struct drm_msm_submitqueue req = {
143 .flags = flags,
144 .prio = prio,
145 };
146 uint64_t nr_prio = 1;
147 int ret;
148
149 if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES) {
150 to_msm_pipe(pipe)->queue_id = 0;
151 return 0;
152 }
153
154 msm_pipe_get_param(pipe, FD_NR_PRIORITIES, &nr_prio);
155
156 req.prio = MIN2(req.prio, MAX2(nr_prio, 1) - 1);
157
158 ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_NEW, &req,
159 sizeof(req));
160 if (ret)
161 return ret;
162
163 to_msm_pipe(pipe)->queue_id = req.id;
164 return 0;
165 }
166
167 static int
open_submitqueue(struct fd_pipe * pipe,uint32_t prio)168 open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
169 {
170 const struct fd_dev_info *info = fd_dev_info_raw(&pipe->dev_id);
171 int ret = -1;
172
173 if (info && info->chip >= A7XX)
174 ret = __open_submitqueue(pipe, prio, MSM_SUBMITQUEUE_ALLOW_PREEMPT);
175
176 /* If kernel doesn't support preemption, try again without: */
177 if (ret)
178 ret = __open_submitqueue(pipe, prio, 0);
179
180 if (ret) {
181 ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
182 return ret;
183 }
184
185 return 0;
186 }
187
188 static void
close_submitqueue(struct fd_pipe * pipe,uint32_t queue_id)189 close_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
190 {
191 if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES)
192 return;
193
194 drmCommandWrite(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_CLOSE, &queue_id,
195 sizeof(queue_id));
196 }
197
198 static void
msm_pipe_destroy(struct fd_pipe * pipe)199 msm_pipe_destroy(struct fd_pipe *pipe)
200 {
201 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
202
203 close_submitqueue(pipe, msm_pipe->queue_id);
204 fd_pipe_sp_ringpool_fini(pipe);
205 free(msm_pipe);
206 }
207
208 static const struct fd_pipe_funcs sp_funcs = {
209 .ringbuffer_new_object = fd_ringbuffer_sp_new_object,
210 .submit_new = msm_submit_sp_new,
211 .flush = fd_pipe_sp_flush,
212 .get_param = msm_pipe_get_param,
213 .set_param = msm_pipe_set_param,
214 .wait = msm_pipe_wait,
215 .destroy = msm_pipe_destroy,
216 };
217
218 static const struct fd_pipe_funcs legacy_funcs = {
219 .ringbuffer_new_object = msm_ringbuffer_new_object,
220 .submit_new = msm_submit_new,
221 .get_param = msm_pipe_get_param,
222 .set_param = msm_pipe_set_param,
223 .wait = msm_pipe_wait,
224 .destroy = msm_pipe_destroy,
225 };
226
227 static uint64_t
get_param(struct fd_pipe * pipe,uint32_t param)228 get_param(struct fd_pipe *pipe, uint32_t param)
229 {
230 uint64_t value;
231 int ret = query_param(pipe, param, &value);
232 if (ret) {
233 ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
234 return 0;
235 }
236 return value;
237 }
238
239 struct fd_pipe *
msm_pipe_new(struct fd_device * dev,enum fd_pipe_id id,uint32_t prio)240 msm_pipe_new(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
241 {
242 static const uint32_t pipe_id[] = {
243 [FD_PIPE_3D] = MSM_PIPE_3D0,
244 [FD_PIPE_2D] = MSM_PIPE_2D0,
245 };
246 struct msm_pipe *msm_pipe = NULL;
247 struct fd_pipe *pipe = NULL;
248
249 msm_pipe = calloc(1, sizeof(*msm_pipe));
250 if (!msm_pipe) {
251 ERROR_MSG("allocation failed");
252 goto fail;
253 }
254
255 pipe = &msm_pipe->base;
256
257 if (fd_device_version(dev) >= FD_VERSION_SOFTPIN) {
258 pipe->funcs = &sp_funcs;
259 } else {
260 pipe->funcs = &legacy_funcs;
261 }
262
263 /* initialize before get_param(): */
264 pipe->dev = dev;
265 msm_pipe->pipe = pipe_id[id];
266
267 /* these params should be supported since the first version of drm/msm: */
268 msm_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
269 msm_pipe->gmem = get_param(pipe, MSM_PARAM_GMEM_SIZE);
270 msm_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
271
272 if (fd_device_version(pipe->dev) >= FD_VERSION_GMEM_BASE)
273 msm_pipe->gmem_base = get_param(pipe, MSM_PARAM_GMEM_BASE);
274
275 if (!(msm_pipe->gpu_id || msm_pipe->chip_id))
276 goto fail;
277
278 INFO_MSG("Pipe Info:");
279 INFO_MSG(" GPU-id: %d", msm_pipe->gpu_id);
280 INFO_MSG(" Chip-id: 0x%016"PRIx64, msm_pipe->chip_id);
281 INFO_MSG(" GMEM size: 0x%08x", msm_pipe->gmem);
282
283 if (open_submitqueue(pipe, prio))
284 goto fail;
285
286 fd_pipe_sp_ringpool_init(pipe);
287
288 return pipe;
289 fail:
290 if (pipe)
291 fd_pipe_del(pipe);
292 return NULL;
293 }
294