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/libsync.h"
25 #include "util/slab.h"
26
27 #include "freedreno_ringbuffer_sp.h"
28 #include "virtio_priv.h"
29
30 static int
query_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)31 query_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
32 {
33 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
34 struct drm_msm_param req = {
35 .pipe = virtio_pipe->pipe,
36 .param = param,
37 };
38 int ret;
39
40 ret = virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_GET_PARAM, &req);
41 if (ret)
42 return ret;
43
44 *value = req.value;
45
46 return 0;
47 }
48
49 static int
query_faults(struct fd_pipe * pipe,uint64_t * value)50 query_faults(struct fd_pipe *pipe, uint64_t *value)
51 {
52 struct virtio_device *virtio_dev = to_virtio_device(pipe->dev);
53 uint32_t async_error = 0;
54 uint64_t global_faults;
55
56 if (vdrm_shmem_has_field(virtio_dev->shmem, async_error))
57 async_error = virtio_dev->shmem->async_error;
58
59 if (vdrm_shmem_has_field(virtio_dev->shmem, global_faults)) {
60 global_faults = virtio_dev->shmem->global_faults;
61 } else {
62 int ret = query_param(pipe, MSM_PARAM_FAULTS, &global_faults);
63 if (ret)
64 return ret;
65 }
66
67 *value = global_faults + async_error;
68
69 return 0;
70 }
71
72 static int
virtio_pipe_get_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t * value)73 virtio_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
74 uint64_t *value)
75 {
76 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
77 struct virtio_device *virtio_dev = to_virtio_device(pipe->dev);
78
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 *value = virtio_dev->vdrm->caps.u.msm.max_freq;
95 return 0;
96 case FD_TIMESTAMP:
97 return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
98 case FD_NR_PRIORITIES:
99 *value = virtio_dev->vdrm->caps.u.msm.priorities;
100 return 0;
101 case FD_CTX_FAULTS:
102 case FD_GLOBAL_FAULTS:
103 return query_faults(pipe, value);
104 case FD_SUSPEND_COUNT:
105 return query_param(pipe, MSM_PARAM_SUSPENDS, value);
106 case FD_VA_SIZE:
107 *value = virtio_dev->vdrm->caps.u.msm.va_size;
108 return 0;
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 MESA_TRACE_FUNC();
119 struct vdrm_device *vdrm = to_virtio_device(pipe->dev)->vdrm;
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 };
125 struct msm_ccmd_submitqueue_query_rsp *rsp;
126 int64_t end_time = os_time_get_nano() + timeout;
127 int ret;
128
129 /* Do a non-blocking wait to trigger host-side wait-boost,
130 * if the host kernel is new enough
131 */
132 rsp = vdrm_alloc_rsp(vdrm, &req.hdr, sizeof(*rsp));
133 ret = vdrm_send_req(vdrm, &req.hdr, false);
134 if (ret)
135 goto out;
136
137 vdrm_flush(vdrm);
138
139 if (fence->use_fence_fd)
140 return sync_wait(fence->fence_fd, timeout / 1000000);
141
142 do {
143 rsp = vdrm_alloc_rsp(vdrm, &req.hdr, sizeof(*rsp));
144
145 ret = vdrm_send_req(vdrm, &req.hdr, true);
146 if (ret)
147 goto out;
148
149 if ((timeout != OS_TIMEOUT_INFINITE) &&
150 (os_time_get_nano() >= end_time))
151 break;
152
153 ret = rsp->ret;
154 } while (ret == -ETIMEDOUT);
155
156 out:
157 return ret;
158 }
159
160 static int
open_submitqueue(struct fd_pipe * pipe,uint32_t prio)161 open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
162 {
163 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
164
165 struct drm_msm_submitqueue req = {
166 .flags = 0,
167 .prio = prio,
168 };
169 uint64_t nr_prio = 1;
170 int ret;
171
172 virtio_pipe_get_param(pipe, FD_NR_PRIORITIES, &nr_prio);
173
174 req.prio = MIN2(req.prio, MAX2(nr_prio, 1) - 1);
175
176 ret = virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_SUBMITQUEUE_NEW, &req);
177 if (ret) {
178 ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
179 return ret;
180 }
181
182 virtio_pipe->queue_id = req.id;
183 virtio_pipe->ring_idx = req.prio + 1;
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 virtio_simple_ioctl(pipe->dev, DRM_IOCTL_MSM_SUBMITQUEUE_CLOSE, &queue_id);
192 }
193
194 static void
virtio_pipe_destroy(struct fd_pipe * pipe)195 virtio_pipe_destroy(struct fd_pipe *pipe)
196 {
197 struct virtio_pipe *virtio_pipe = to_virtio_pipe(pipe);
198
199 if (util_queue_is_initialized(&virtio_pipe->retire_queue))
200 util_queue_destroy(&virtio_pipe->retire_queue);
201
202 close_submitqueue(pipe, virtio_pipe->queue_id);
203 fd_pipe_sp_ringpool_fini(pipe);
204 free(virtio_pipe);
205 }
206
207 static const struct fd_pipe_funcs funcs = {
208 .ringbuffer_new_object = fd_ringbuffer_sp_new_object,
209 .submit_new = virtio_submit_new,
210 .flush = fd_pipe_sp_flush,
211 .get_param = virtio_pipe_get_param,
212 .wait = virtio_pipe_wait,
213 .destroy = virtio_pipe_destroy,
214 };
215
216 struct fd_pipe *
virtio_pipe_new(struct fd_device * dev,enum fd_pipe_id id,uint32_t prio)217 virtio_pipe_new(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
218 {
219 static const uint32_t pipe_id[] = {
220 [FD_PIPE_3D] = MSM_PIPE_3D0,
221 [FD_PIPE_2D] = MSM_PIPE_2D0,
222 };
223 struct virtio_device *virtio_dev = to_virtio_device(dev);
224 struct vdrm_device *vdrm = virtio_dev->vdrm;
225 struct virtio_pipe *virtio_pipe = NULL;
226 struct fd_pipe *pipe = NULL;
227
228 virtio_pipe = calloc(1, sizeof(*virtio_pipe));
229 if (!virtio_pipe) {
230 ERROR_MSG("allocation failed");
231 goto fail;
232 }
233
234 pipe = &virtio_pipe->base;
235
236 pipe->funcs = &funcs;
237
238 /* initialize before get_param(): */
239 pipe->dev = dev;
240 virtio_pipe->pipe = pipe_id[id];
241
242 virtio_pipe->gpu_id = vdrm->caps.u.msm.gpu_id;
243 virtio_pipe->gmem = vdrm->caps.u.msm.gmem_size;
244 virtio_pipe->gmem_base = vdrm->caps.u.msm.gmem_base;
245 virtio_pipe->chip_id = vdrm->caps.u.msm.chip_id;
246
247
248 if (!(virtio_pipe->gpu_id || virtio_pipe->chip_id))
249 goto fail;
250
251 util_queue_init(&virtio_pipe->retire_queue, "rq", 8, 1,
252 UTIL_QUEUE_INIT_RESIZE_IF_FULL, NULL);
253
254 INFO_MSG("Pipe Info:");
255 INFO_MSG(" GPU-id: %d", virtio_pipe->gpu_id);
256 INFO_MSG(" Chip-id: 0x%016"PRIx64, virtio_pipe->chip_id);
257 INFO_MSG(" GMEM size: 0x%08x", virtio_pipe->gmem);
258
259 if (open_submitqueue(pipe, prio))
260 goto fail;
261
262 fd_pipe_sp_ringpool_init(pipe);
263
264 return pipe;
265 fail:
266 if (pipe)
267 fd_pipe_del(pipe);
268 return NULL;
269 }
270