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 struct virtio_device *virtio_dev = to_virtio_device(pipe->dev);
80
81 switch (param) {
82 case FD_DEVICE_ID: // XXX probably get rid of this..
83 case FD_GPU_ID:
84 *value = virtio_pipe->gpu_id;
85 return 0;
86 case FD_GMEM_SIZE:
87 *value = virtio_pipe->gmem;
88 return 0;
89 case FD_GMEM_BASE:
90 *value = virtio_pipe->gmem_base;
91 return 0;
92 case FD_CHIP_ID:
93 *value = virtio_pipe->chip_id;
94 return 0;
95 case FD_MAX_FREQ:
96 *value = virtio_dev->caps.u.msm.max_freq;
97 return 0;
98 case FD_TIMESTAMP:
99 return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
100 case FD_NR_RINGS:
101 *value = virtio_dev->caps.u.msm.priorities;
102 return 0;
103 case FD_CTX_FAULTS:
104 return query_queue_param(pipe, MSM_SUBMITQUEUE_PARAM_FAULTS, value);
105 case FD_GLOBAL_FAULTS:
106 return query_param(pipe, MSM_PARAM_FAULTS, value);
107 case FD_SUSPEND_COUNT:
108 return query_param(pipe, MSM_PARAM_SUSPENDS, value);
109 default:
110 ERROR_MSG("invalid param id: %d", param);
111 return -1;
112 }
113 }
114
115 static int
virtio_pipe_wait(struct fd_pipe * pipe,const struct fd_fence * fence,uint64_t timeout)116 virtio_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence, uint64_t timeout)
117 {
118 struct msm_ccmd_wait_fence_req req = {
119 .hdr = MSM_CCMD(WAIT_FENCE, sizeof(req)),
120 .queue_id = to_virtio_pipe(pipe)->queue_id,
121 .fence = fence->kfence,
122 };
123 struct msm_ccmd_submitqueue_query_rsp *rsp;
124 int64_t end_time = os_time_get_nano() + timeout;
125 int ret;
126
127 do {
128 rsp = virtio_alloc_rsp(pipe->dev, &req.hdr, sizeof(*rsp));
129
130 ret = virtio_execbuf(pipe->dev, &req.hdr, true);
131 if (ret)
132 goto out;
133
134 if ((timeout != PIPE_TIMEOUT_INFINITE) &&
135 (os_time_get_nano() >= end_time))
136 break;
137
138 ret = rsp->ret;
139 } while (ret == -ETIMEDOUT);
140
141 out:
142 return ret;
143 }
144
145 static int
open_submitqueue(struct fd_pipe * pipe,uint32_t prio)146 open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
147 {
148 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
149
150 struct drm_msm_submitqueue req = {
151 .flags = 0,
152 .prio = prio,
153 };
154 uint64_t nr_rings = 1;
155 int ret;
156
157 virtio_pipe_get_param(pipe, FD_NR_RINGS, &nr_rings);
158
159 req.prio = MIN2(req.prio, MAX2(nr_rings, 1) - 1);
160
161 ret = virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_SUBMITQUEUE_NEW, &req);
162 if (ret) {
163 ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
164 return ret;
165 }
166
167 virtio_pipe->queue_id = req.id;
168 virtio_pipe->ring_idx = req.prio + 1;
169
170 return 0;
171 }
172
173 static void
close_submitqueue(struct fd_pipe * pipe,uint32_t queue_id)174 close_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
175 {
176 virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE, &queue_id);
177 }
178
179 static void
virtio_pipe_destroy(struct fd_pipe * pipe)180 virtio_pipe_destroy(struct fd_pipe *pipe)
181 {
182 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
183
184 if (util_queue_is_initialized(&virtio_pipe->retire_queue))
185 util_queue_destroy(&virtio_pipe->retire_queue);
186
187 close_submitqueue(pipe, virtio_pipe->queue_id);
188 fd_pipe_sp_ringpool_fini(pipe);
189 free(virtio_pipe);
190 }
191
192 static const struct fd_pipe_funcs funcs = {
193 .ringbuffer_new_object = fd_ringbuffer_sp_new_object,
194 .submit_new = virtio_submit_new,
195 .flush = fd_pipe_sp_flush,
196 .get_param = virtio_pipe_get_param,
197 .wait = virtio_pipe_wait,
198 .destroy = virtio_pipe_destroy,
199 };
200
201 static void
init_shmem(struct fd_device * dev)202 init_shmem(struct fd_device *dev)
203 {
204 struct virtio_device *virtio_dev = to_virtio_device(dev);
205
206 simple_mtx_lock(&virtio_dev->rsp_lock);
207
208 /* One would like to do this in virtio_device_new(), but we'd
209 * have to bypass/reinvent fd_bo_new()..
210 */
211 if (unlikely(!virtio_dev->shmem)) {
212 virtio_dev->shmem_bo = fd_bo_new(dev, 0x4000,
213 _FD_BO_VIRTIO_SHM, "shmem");
214 virtio_dev->shmem = fd_bo_map(virtio_dev->shmem_bo);
215 virtio_dev->shmem_bo->bo_reuse = NO_CACHE;
216
217 uint32_t offset = virtio_dev->shmem->rsp_mem_offset;
218 virtio_dev->rsp_mem_len = fd_bo_size(virtio_dev->shmem_bo) - offset;
219 virtio_dev->rsp_mem = &((uint8_t *)virtio_dev->shmem)[offset];
220 }
221
222 simple_mtx_unlock(&virtio_dev->rsp_lock);
223 }
224
225 struct fd_pipe *
virtio_pipe_new(struct fd_device * dev,enum fd_pipe_id id,uint32_t prio)226 virtio_pipe_new(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
227 {
228 static const uint32_t pipe_id[] = {
229 [FD_PIPE_3D] = MSM_PIPE_3D0,
230 [FD_PIPE_2D] = MSM_PIPE_2D0,
231 };
232 struct virtio_device *virtio_dev = to_virtio_device(dev);
233 struct virtio_pipe *virtio_pipe = NULL;
234 struct fd_pipe *pipe = NULL;
235
236 init_shmem(dev);
237
238 virtio_pipe = calloc(1, sizeof(*virtio_pipe));
239 if (!virtio_pipe) {
240 ERROR_MSG("allocation failed");
241 goto fail;
242 }
243
244 pipe = &virtio_pipe->base;
245
246 pipe->funcs = &funcs;
247
248 /* initialize before get_param(): */
249 pipe->dev = dev;
250 virtio_pipe->pipe = pipe_id[id];
251
252 virtio_pipe->gpu_id = virtio_dev->caps.u.msm.gpu_id;
253 virtio_pipe->gmem = virtio_dev->caps.u.msm.gmem_size;
254 virtio_pipe->gmem_base = virtio_dev->caps.u.msm.gmem_base;
255 virtio_pipe->chip_id = virtio_dev->caps.u.msm.chip_id;
256
257
258 if (!(virtio_pipe->gpu_id || virtio_pipe->chip_id))
259 goto fail;
260
261 util_queue_init(&virtio_pipe->retire_queue, "rq", 8, 1,
262 UTIL_QUEUE_INIT_RESIZE_IF_FULL, NULL);
263
264 INFO_MSG("Pipe Info:");
265 INFO_MSG(" GPU-id: %d", virtio_pipe->gpu_id);
266 INFO_MSG(" Chip-id: 0x%016"PRIx64, virtio_pipe->chip_id);
267 INFO_MSG(" GMEM size: 0x%08x", virtio_pipe->gmem);
268
269 if (open_submitqueue(pipe, prio))
270 goto fail;
271
272 fd_pipe_sp_ringpool_init(pipe);
273
274 return pipe;
275 fail:
276 if (pipe)
277 fd_pipe_del(pipe);
278 return NULL;
279 }
280