1 /*
2 * Copyright © 2021 Raspberry Pi Ltd
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * Gallium query object support for performance counters
26 *
27 * This contains the performance V3D counters queries.
28 */
29
30 #include "v3d_query.h"
31
32 #include "common/v3d_performance_counters.h"
33
34 struct v3d_query_perfcnt
35 {
36 struct v3d_query base;
37
38 unsigned num_queries;
39 struct v3d_perfmon_state *perfmon;
40 };
41
42 static void
kperfmon_destroy(struct v3d_context * v3d,struct v3d_perfmon_state * perfmon)43 kperfmon_destroy(struct v3d_context *v3d, struct v3d_perfmon_state *perfmon)
44 {
45 struct drm_v3d_perfmon_destroy destroyreq;
46
47 destroyreq.id = perfmon->kperfmon_id;
48 int ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_DESTROY, &destroyreq);
49 if (ret != 0)
50 fprintf(stderr, "failed to destroy perfmon %d: %s\n",
51 perfmon->kperfmon_id, strerror(errno));
52 }
53
54 int
v3dX(get_driver_query_group_info_perfcnt)55 v3dX(get_driver_query_group_info_perfcnt)(struct v3d_screen *screen, unsigned index,
56 struct pipe_driver_query_group_info *info)
57 {
58 struct v3d_device_info *devinfo = &screen->devinfo;
59
60 if (!screen->has_perfmon)
61 return 0;
62
63 if (!info)
64 return 1;
65
66 if (index > 0)
67 return 0;
68
69 info->name = "V3D counters";
70 info->max_active_queries = DRM_V3D_MAX_PERF_COUNTERS;
71 info->num_queries = devinfo->max_perfcnt ? devinfo->max_perfcnt
72 : ARRAY_SIZE(v3d_performance_counters);
73
74 return 1;
75 }
76
77 int
v3dX(get_driver_query_info_perfcnt)78 v3dX(get_driver_query_info_perfcnt)(struct v3d_screen *screen, unsigned index,
79 struct pipe_driver_query_info *info)
80 {
81 struct v3d_device_info *devinfo = &screen->devinfo;
82 unsigned max_perfcnt = devinfo->max_perfcnt ? devinfo->max_perfcnt
83 : ARRAY_SIZE(v3d_performance_counters);
84
85 if (!screen->has_perfmon)
86 return 0;
87
88 if (!info)
89 return max_perfcnt;
90
91 if (index >= max_perfcnt)
92 return 0;
93
94 if (screen->perfcnt_names[index]) {
95 info->name = screen->perfcnt_names[index];
96 } else if (devinfo->max_perfcnt) {
97 struct drm_v3d_perfmon_get_counter counter = {
98 .counter = index,
99 };
100 int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_PERFMON_GET_COUNTER, &counter);
101 if (ret != 0) {
102 fprintf(stderr, "Failed to get performance counter %d: %s\n",
103 index, strerror(errno));
104 return 0;
105 }
106
107 screen->perfcnt_names[index] = ralloc_strdup(screen->perfcnt_names,
108 (const char *) counter.name);
109 info->name = screen->perfcnt_names[index];
110 } else {
111 info->name = v3d_performance_counters[index][V3D_PERFCNT_NAME];
112 }
113
114 info->group_id = 0;
115 info->query_type = PIPE_QUERY_DRIVER_SPECIFIC + index;
116 info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE;
117 info->type = PIPE_DRIVER_QUERY_TYPE_UINT64;
118 info->flags = PIPE_DRIVER_QUERY_FLAG_BATCH;
119
120 return 1;
121 }
122
123 static void
v3d_destroy_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)124 v3d_destroy_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
125 {
126 struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
127
128 assert(pquery->perfmon);
129
130 if (v3d->active_perfmon == pquery->perfmon) {
131 fprintf(stderr, "Query is active; end query before destroying\n");
132 return;
133 }
134 if (pquery->perfmon->kperfmon_id)
135 kperfmon_destroy(v3d, pquery->perfmon);
136
137 v3d_fence_unreference(&pquery->perfmon->last_job_fence);
138 free(pquery->perfmon);
139 free(query);
140 }
141
142 static bool
v3d_begin_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)143 v3d_begin_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
144 {
145 struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
146 struct drm_v3d_perfmon_create createreq = { 0 };
147 int i, ret;
148
149 /* Only one perfmon can be activated per context */
150 if (v3d->active_perfmon) {
151 fprintf(stderr,
152 "Another query is already active; "
153 "finish it before starting a new one\n");
154 return false;
155 }
156
157 assert(pquery->perfmon);
158
159 /* Reset the counters by destroying the previously allocated perfmon */
160 if (pquery->perfmon->kperfmon_id)
161 kperfmon_destroy(v3d, pquery->perfmon);
162
163 for (i = 0; i < pquery->num_queries; i++)
164 createreq.counters[i] = pquery->perfmon->counters[i];
165
166 createreq.ncounters = pquery->num_queries;
167 ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_CREATE, &createreq);
168 if (ret != 0)
169 return false;
170
171 pquery->perfmon->kperfmon_id = createreq.id;
172 pquery->perfmon->job_submitted = false;
173 v3d_fence_unreference(&pquery->perfmon->last_job_fence);
174
175 /* Ensure all pending jobs are flushed before activating the
176 * perfmon
177 */
178 v3d_flush((struct pipe_context *)v3d);
179 v3d->active_perfmon = pquery->perfmon;
180
181 return true;
182 }
183
184 static bool
v3d_end_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)185 v3d_end_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
186 {
187 struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
188
189 assert(pquery->perfmon);
190
191 if (v3d->active_perfmon != pquery->perfmon) {
192 fprintf(stderr, "This query is not active\n");
193 return false;
194 }
195
196 /* Ensure all pending jobs are flushed before deactivating the
197 * perfmon
198 */
199 v3d_flush((struct pipe_context *)v3d);
200
201 /* Get a copy of latest submitted job's fence to wait for its
202 * completion
203 */
204 if (v3d->active_perfmon->job_submitted) {
205 int fd = -1;
206 drmSyncobjExportSyncFile(v3d->fd, v3d->out_sync, &fd);
207 if (fd == -1) {
208 fprintf(stderr, "export failed\n");
209 v3d->active_perfmon->last_job_fence = NULL;
210 } else {
211 v3d->active_perfmon->last_job_fence =
212 v3d_fence_create(v3d, fd);
213 }
214 }
215
216 v3d->active_perfmon = NULL;
217
218 return true;
219 }
220
221 static bool
v3d_get_query_result_perfcnt(struct v3d_context * v3d,struct v3d_query * query,bool wait,union pipe_query_result * vresult)222 v3d_get_query_result_perfcnt(struct v3d_context *v3d, struct v3d_query *query,
223 bool wait, union pipe_query_result *vresult)
224 {
225 struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
226 struct drm_v3d_perfmon_get_values req = { 0 };
227 int i, ret;
228
229 assert(pquery->perfmon);
230
231 if (pquery->perfmon->job_submitted) {
232 if (!v3d_fence_wait(v3d->screen,
233 pquery->perfmon->last_job_fence,
234 wait ? OS_TIMEOUT_INFINITE : 0))
235 return false;
236
237 req.id = pquery->perfmon->kperfmon_id;
238 req.values_ptr = (uintptr_t)pquery->perfmon->values;
239 ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_GET_VALUES, &req);
240 if (ret != 0) {
241 fprintf(stderr, "Can't request perfmon counters values\n");
242 return false;
243 }
244 }
245
246 for (i = 0; i < pquery->num_queries; i++)
247 vresult->batch[i].u64 = pquery->perfmon->values[i];
248
249 return true;
250 }
251
252 static const struct v3d_query_funcs perfcnt_query_funcs = {
253 .destroy_query = v3d_destroy_query_perfcnt,
254 .begin_query = v3d_begin_query_perfcnt,
255 .end_query = v3d_end_query_perfcnt,
256 .get_query_result = v3d_get_query_result_perfcnt,
257 };
258
259 struct pipe_query *
v3dX(create_batch_query_perfcnt)260 v3dX(create_batch_query_perfcnt)(struct v3d_context *v3d, unsigned num_queries,
261 unsigned *query_types)
262 {
263 struct v3d_query_perfcnt *pquery = NULL;
264 struct v3d_query *query;
265 struct v3d_perfmon_state *perfmon = NULL;
266 struct v3d_device_info *devinfo = &v3d->screen->devinfo;
267 unsigned max_perfcnt = devinfo->max_perfcnt ? devinfo->max_perfcnt
268 : ARRAY_SIZE(v3d_performance_counters);
269 int i;
270
271 /* Validate queries */
272 for (i = 0; i < num_queries; i++) {
273 if (query_types[i] < PIPE_QUERY_DRIVER_SPECIFIC ||
274 query_types[i] >= PIPE_QUERY_DRIVER_SPECIFIC + max_perfcnt) {
275 fprintf(stderr, "Invalid query type\n");
276 return NULL;
277 }
278 }
279
280 pquery = calloc(1, sizeof(*pquery));
281 if (!pquery)
282 return NULL;
283
284 perfmon = calloc(1, sizeof(*perfmon));
285 if (!perfmon) {
286 free(pquery);
287 return NULL;
288 }
289
290 for (i = 0; i < num_queries; i++)
291 perfmon->counters[i] = query_types[i] - PIPE_QUERY_DRIVER_SPECIFIC;
292
293 pquery->perfmon = perfmon;
294 pquery->num_queries = num_queries;
295
296 query = &pquery->base;
297 query->funcs = &perfcnt_query_funcs;
298
299 /* Note that struct pipe_query isn't actually defined anywhere. */
300 return (struct pipe_query *)query;
301 }
302