1 /*
2 * Copyright © 2022 Google, Inc.
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
24 #include "util/slab.h"
25
26 #include "freedreno_ringbuffer_sp.h"
27 #include "virtio_priv.h"
28
29 static int
query_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)30 query_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
31 {
32 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
33 struct drm_msm_param req = {
34 .pipe = virtio_pipe->pipe,
35 .param = param,
36 };
37 int ret;
38
39 ret = virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_GET_PARAM, &req);
40 if (ret)
41 return ret;
42
43 *value = req.value;
44
45 return 0;
46 }
47
48 static int
query_queue_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)49 query_queue_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
50 {
51 struct msm_ccmd_submitqueue_query_req req = {
52 .hdr = MSM_CCMD(SUBMITQUEUE_QUERY, sizeof(req)),
53 .queue_id = to_virtio_pipe(pipe)->queue_id,
54 .param = param,
55 .len = sizeof(*value),
56 };
57 struct msm_ccmd_submitqueue_query_rsp *rsp;
58 unsigned rsp_len = sizeof(*rsp) + req.len;
59
60 rsp = virtio_alloc_rsp(pipe->dev, &req.hdr, rsp_len);
61
62 int ret = virtio_execbuf(pipe->dev, &req.hdr, true);
63 if (ret)
64 goto out;
65
66 memcpy(value, rsp->payload, req.len);
67
68 ret = rsp->ret;
69
70 out:
71 return ret;
72 }
73
74 static int
virtio_pipe_get_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t * value)75 virtio_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
76 uint64_t *value)
77 {
78 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
79 switch (param) {
80 case FD_DEVICE_ID: // XXX probably get rid of this..
81 case FD_GPU_ID:
82 *value = virtio_pipe->gpu_id;
83 return 0;
84 case FD_GMEM_SIZE:
85 *value = virtio_pipe->gmem;
86 return 0;
87 case FD_GMEM_BASE:
88 *value = virtio_pipe->gmem_base;
89 return 0;
90 case FD_CHIP_ID:
91 *value = virtio_pipe->chip_id;
92 return 0;
93 case FD_MAX_FREQ:
94 return query_param(pipe, MSM_PARAM_MAX_FREQ, value);
95 case FD_TIMESTAMP:
96 return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
97 case FD_NR_RINGS:
98 /* TODO need to not rely on host egl ctx for fence if
99 * we want to support multiple priority levels
100 */
101 return 1;
102 // return query_param(pipe, MSM_PARAM_NR_RINGS, value);
103 case FD_PP_PGTABLE:
104 return query_param(pipe, MSM_PARAM_PP_PGTABLE, value);
105 case FD_CTX_FAULTS:
106 return query_queue_param(pipe, MSM_SUBMITQUEUE_PARAM_FAULTS, value);
107 case FD_GLOBAL_FAULTS:
108 return query_param(pipe, MSM_PARAM_FAULTS, value);
109 case FD_SUSPEND_COUNT:
110 return query_param(pipe, MSM_PARAM_SUSPENDS, value);
111 default:
112 ERROR_MSG("invalid param id: %d", param);
113 return -1;
114 }
115 }
116
117 static int
virtio_pipe_wait(struct fd_pipe * pipe,const struct fd_fence * fence,uint64_t timeout)118 virtio_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence, uint64_t timeout)
119 {
120 struct msm_ccmd_wait_fence_req req = {
121 .hdr = MSM_CCMD(WAIT_FENCE, sizeof(req)),
122 .queue_id = to_virtio_pipe(pipe)->queue_id,
123 .fence = fence->kfence,
124 .timeout = timeout,
125 };
126 struct msm_ccmd_submitqueue_query_rsp *rsp;
127
128 rsp = virtio_alloc_rsp(pipe->dev, &req.hdr, sizeof(*rsp));
129
130 int ret = virtio_execbuf(pipe->dev, &req.hdr, true);
131 if (ret)
132 goto out;
133
134 ret = rsp->ret;
135
136 out:
137 return ret;
138 }
139
140 static int
open_submitqueue(struct fd_pipe * pipe,uint32_t prio)141 open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
142 {
143 struct drm_msm_submitqueue req = {
144 .flags = 0,
145 .prio = prio,
146 };
147 uint64_t nr_rings = 1;
148 int ret;
149
150 virtio_pipe_get_param(pipe, FD_NR_RINGS, &nr_rings);
151
152 req.prio = MIN2(req.prio, MAX2(nr_rings, 1) - 1);
153
154 ret = virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_SUBMITQUEUE_NEW, &req);
155 if (ret) {
156 ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
157 return ret;
158 }
159
160 to_virtio_pipe(pipe)->queue_id = req.id;
161
162 return 0;
163 }
164
165 static void
close_submitqueue(struct fd_pipe * pipe,uint32_t queue_id)166 close_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
167 {
168 virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE, &queue_id);
169 }
170
171 static void
virtio_pipe_destroy(struct fd_pipe * pipe)172 virtio_pipe_destroy(struct fd_pipe *pipe)
173 {
174 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
175
176 close_submitqueue(pipe, virtio_pipe->queue_id);
177 fd_pipe_sp_ringpool_fini(pipe);
178 free(virtio_pipe);
179 }
180
181 static const struct fd_pipe_funcs funcs = {
182 .ringbuffer_new_object = fd_ringbuffer_sp_new_object,
183 .submit_new = virtio_submit_new,
184 .flush = fd_pipe_sp_flush,
185 .get_param = virtio_pipe_get_param,
186 .wait = virtio_pipe_wait,
187 .destroy = virtio_pipe_destroy,
188 };
189
190 static uint64_t
get_param(struct fd_pipe * pipe,uint32_t param)191 get_param(struct fd_pipe *pipe, uint32_t param)
192 {
193 uint64_t value;
194 int ret = query_param(pipe, param, &value);
195 if (ret) {
196 ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
197 return 0;
198 }
199 return value;
200 }
201
202 static void
init_shmem(struct fd_device * dev)203 init_shmem(struct fd_device *dev)
204 {
205 struct virtio_device *virtio_dev = to_virtio_device(dev);
206
207 simple_mtx_lock(&virtio_dev->rsp_lock);
208
209 /* One would like to do this in virtio_device_new(), but we'd
210 * have to bypass/reinvent fd_bo_new()..
211 */
212 if (unlikely(!virtio_dev->shmem)) {
213 virtio_dev->shmem_bo = fd_bo_new(dev, sizeof(*virtio_dev->shmem),
214 _FD_BO_VIRTIO_SHM, "shmem");
215 virtio_dev->shmem = fd_bo_map(virtio_dev->shmem_bo);
216
217 virtio_dev->shmem_bo->bo_reuse = NO_CACHE;
218 }
219
220 simple_mtx_unlock(&virtio_dev->rsp_lock);
221 }
222
223 struct fd_pipe *
virtio_pipe_new(struct fd_device * dev,enum fd_pipe_id id,uint32_t prio)224 virtio_pipe_new(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
225 {
226 static const uint32_t pipe_id[] = {
227 [FD_PIPE_3D] = MSM_PIPE_3D0,
228 [FD_PIPE_2D] = MSM_PIPE_2D0,
229 };
230 struct virtio_pipe *virtio_pipe = NULL;
231 struct fd_pipe *pipe = NULL;
232
233 init_shmem(dev);
234
235 virtio_pipe = calloc(1, sizeof(*virtio_pipe));
236 if (!virtio_pipe) {
237 ERROR_MSG("allocation failed");
238 goto fail;
239 }
240
241 pipe = &virtio_pipe->base;
242
243 pipe->funcs = &funcs;
244
245 /* initialize before get_param(): */
246 pipe->dev = dev;
247 virtio_pipe->pipe = pipe_id[id];
248
249 /* these params should be supported since the first version of drm/msm: */
250 virtio_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
251 virtio_pipe->gmem = get_param(pipe, MSM_PARAM_GMEM_SIZE);
252 virtio_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
253
254 if (fd_device_version(pipe->dev) >= FD_VERSION_GMEM_BASE)
255 virtio_pipe->gmem_base = get_param(pipe, MSM_PARAM_GMEM_BASE);
256
257 if (!(virtio_pipe->gpu_id || virtio_pipe->chip_id))
258 goto fail;
259
260 INFO_MSG("Pipe Info:");
261 INFO_MSG(" GPU-id: %d", virtio_pipe->gpu_id);
262 INFO_MSG(" Chip-id: 0x%016"PRIx64, virtio_pipe->chip_id);
263 INFO_MSG(" GMEM size: 0x%08x", virtio_pipe->gmem);
264
265 if (open_submitqueue(pipe, prio))
266 goto fail;
267
268 fd_pipe_sp_ringpool_init(pipe);
269
270 return pipe;
271 fail:
272 if (pipe)
273 fd_pipe_del(pipe);
274 return NULL;
275 }
276