• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "v3d_screen.h"
32 
33 struct v3d_query_perfcnt
34 {
35         struct v3d_query base;
36 
37         unsigned num_queries;
38         struct v3d_perfmon_state *perfmon;
39 };
40 
41 static void
kperfmon_destroy(struct v3d_context * v3d,struct v3d_perfmon_state * perfmon)42 kperfmon_destroy(struct v3d_context *v3d, struct v3d_perfmon_state *perfmon)
43 {
44         struct drm_v3d_perfmon_destroy destroyreq;
45 
46         destroyreq.id = perfmon->kperfmon_id;
47         int ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_DESTROY, &destroyreq);
48         if (ret != 0)
49                 fprintf(stderr, "failed to destroy perfmon %d: %s\n",
50                         perfmon->kperfmon_id, strerror(errno));
51 }
52 
53 static void
v3d_destroy_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)54 v3d_destroy_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
55 {
56         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
57 
58         assert(pquery->perfmon);
59 
60         if (v3d->active_perfmon == pquery->perfmon) {
61                 fprintf(stderr, "Query is active; end query before destroying\n");
62                 return;
63         }
64         if (pquery->perfmon->kperfmon_id)
65                 kperfmon_destroy(v3d, pquery->perfmon);
66 
67         v3d_fence_unreference(&pquery->perfmon->last_job_fence);
68         free(pquery->perfmon);
69         free(query);
70 }
71 
72 static bool
v3d_begin_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)73 v3d_begin_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
74 {
75         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
76         struct drm_v3d_perfmon_create createreq = { 0 };
77         int i, ret;
78 
79         /* Only one perfmon can be activated per context */
80         if (v3d->active_perfmon) {
81                 fprintf(stderr,
82                         "Another query is already active; "
83                         "finish it before starting a new one\n");
84                 return false;
85         }
86 
87         assert(pquery->perfmon);
88 
89         /* Reset the counters by destroying the previously allocated perfmon */
90         if (pquery->perfmon->kperfmon_id)
91                 kperfmon_destroy(v3d, pquery->perfmon);
92 
93         for (i = 0; i < pquery->num_queries; i++)
94                 createreq.counters[i] = pquery->perfmon->counters[i];
95 
96         createreq.ncounters = pquery->num_queries;
97         ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_CREATE, &createreq);
98         if (ret != 0)
99                 return false;
100 
101         pquery->perfmon->kperfmon_id = createreq.id;
102         pquery->perfmon->job_submitted = false;
103         v3d_fence_unreference(&pquery->perfmon->last_job_fence);
104 
105         /* Ensure all pending jobs are flushed before activating the
106          * perfmon
107          */
108         v3d_flush((struct pipe_context *)v3d);
109         v3d->active_perfmon = pquery->perfmon;
110 
111         return true;
112 }
113 
114 static bool
v3d_end_query_perfcnt(struct v3d_context * v3d,struct v3d_query * query)115 v3d_end_query_perfcnt(struct v3d_context *v3d, struct v3d_query *query)
116 {
117         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
118 
119         assert(pquery->perfmon);
120 
121         if (v3d->active_perfmon != pquery->perfmon) {
122                 fprintf(stderr, "This query is not active\n");
123                 return false;
124         }
125 
126         /* Ensure all pending jobs are flushed before deactivating the
127          * perfmon
128          */
129         v3d_flush((struct pipe_context *)v3d);
130 
131         /* Get a copy of latest submitted job's fence to wait for its
132          * completion
133          */
134         if (v3d->active_perfmon->job_submitted) {
135                 int fd = -1;
136                 drmSyncobjExportSyncFile(v3d->fd, v3d->out_sync, &fd);
137                 if (fd == -1) {
138                         fprintf(stderr, "export failed\n");
139                         v3d->active_perfmon->last_job_fence = NULL;
140                 } else {
141                         v3d->active_perfmon->last_job_fence =
142                                 v3d_fence_create(v3d, fd);
143                 }
144         }
145 
146         v3d->active_perfmon = NULL;
147 
148         return true;
149 }
150 
151 static bool
v3d_get_query_result_perfcnt(struct v3d_context * v3d,struct v3d_query * query,bool wait,union pipe_query_result * vresult)152 v3d_get_query_result_perfcnt(struct v3d_context *v3d, struct v3d_query *query,
153                              bool wait, union pipe_query_result *vresult)
154 {
155         struct v3d_query_perfcnt *pquery = (struct v3d_query_perfcnt *)query;
156         struct drm_v3d_perfmon_get_values req = { 0 };
157         int i, ret;
158 
159         assert(pquery->perfmon);
160 
161         if (pquery->perfmon->job_submitted) {
162                 if (!v3d_fence_wait(v3d->screen,
163                                     pquery->perfmon->last_job_fence,
164                                     wait ? OS_TIMEOUT_INFINITE : 0))
165                         return false;
166 
167                 req.id = pquery->perfmon->kperfmon_id;
168                 req.values_ptr = (uintptr_t)pquery->perfmon->values;
169                 ret = v3d_ioctl(v3d->fd, DRM_IOCTL_V3D_PERFMON_GET_VALUES, &req);
170                 if (ret != 0) {
171                         fprintf(stderr, "Can't request perfmon counters values\n");
172                         return false;
173                 }
174         }
175 
176         for (i = 0; i < pquery->num_queries; i++)
177                 vresult->batch[i].u64 = pquery->perfmon->values[i];
178 
179         return true;
180 }
181 
182 static const struct v3d_query_funcs perfcnt_query_funcs = {
183         .destroy_query = v3d_destroy_query_perfcnt,
184         .begin_query = v3d_begin_query_perfcnt,
185         .end_query = v3d_end_query_perfcnt,
186         .get_query_result = v3d_get_query_result_perfcnt,
187 };
188 
189 struct pipe_query *
v3d_create_batch_query_pipe(struct v3d_context * v3d,unsigned num_queries,unsigned * query_types)190 v3d_create_batch_query_pipe(struct v3d_context *v3d, unsigned num_queries,
191                             unsigned *query_types)
192 {
193         struct v3d_screen *screen = v3d->screen;
194         struct v3d_query_perfcnt *pquery = NULL;
195         struct v3d_query *query;
196         struct v3d_perfmon_state *perfmon = NULL;
197         unsigned max_perfcnt = screen->perfcnt->max_perfcnt;
198         int i;
199 
200         /* Validate queries */
201         for (i = 0; i < num_queries; i++) {
202                 if (query_types[i] < PIPE_QUERY_DRIVER_SPECIFIC ||
203                     query_types[i] >= PIPE_QUERY_DRIVER_SPECIFIC + max_perfcnt) {
204                         fprintf(stderr, "Invalid query type\n");
205                         return NULL;
206                 }
207         }
208 
209         pquery = calloc(1, sizeof(*pquery));
210         if (!pquery)
211                 return NULL;
212 
213         perfmon = calloc(1, sizeof(*perfmon));
214         if (!perfmon) {
215                 free(pquery);
216                 return NULL;
217         }
218 
219         for (i = 0; i < num_queries; i++)
220                 perfmon->counters[i] = query_types[i] - PIPE_QUERY_DRIVER_SPECIFIC;
221 
222         pquery->perfmon = perfmon;
223         pquery->num_queries = num_queries;
224 
225         query = &pquery->base;
226         query->funcs = &perfcnt_query_funcs;
227 
228         /* Note that struct pipe_query isn't actually defined anywhere. */
229          return (struct pipe_query *)query;
230 }
231