summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/etnaviv/etnaviv_query_pm.c
blob: eaad925e608e07b0459d7d57d892513f07d24057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (c) 2017 Etnaviv Project
 * Copyright (C) 2017 Zodiac Inflight Innovations
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Christian Gmeiner <christian.gmeiner@gmail.com>
 */

#include "util/u_inlines.h"
#include "util/u_memory.h"

#include "etnaviv_context.h"
#include "etnaviv_query_pm.h"
#include "etnaviv_screen.h"

struct etna_perfmon_source
{
   const char *domain;
   const char *signal;
};

struct etna_perfmon_config
{
   const char *name;
   unsigned type;
   const struct etna_perfmon_source *source;
};

static const struct etna_perfmon_config query_config[] = {
   {
      .name = "hi-total-cyles",
      .type = ETNA_QUERY_HI_TOTAL_CYCLES,
      .source = (const struct etna_perfmon_source[]) {
         { "HI", "TOTAL_CYCLES" }
      }
   },
   {
      .name = "hi-idle-cyles",
      .type = ETNA_QUERY_HI_IDLE_CYCLES,
      .source = (const struct etna_perfmon_source[]) {
         { "HI", "IDLE_CYCLES" }
      }
   },
   {
      .name = "hi-axi-cycles-read-request-stalled",
      .type = ETNA_QUERY_HI_AXI_CYCLES_READ_REQUEST_STALLED,
      .source = (const struct etna_perfmon_source[]) {
         { "HI", "AXI_CYCLES_READ_REQUEST_STALLED" }
      }
   },
   {
      .name = "hi-axi-cycles-write-request-stalled",
      .type = ETNA_QUERY_HI_AXI_CYCLES_WRITE_REQUEST_STALLED,
      .source = (const struct etna_perfmon_source[]) {
         { "HI", "AXI_CYCLES_WRITE_REQUEST_STALLED" }
      }
   },
   {
      .name = "hi-axi-cycles-write-data-stalled",
      .type = ETNA_QUERY_HI_AXI_CYCLES_WRITE_DATA_STALLED,
      .source = (const struct etna_perfmon_source[]) {
         { "HI", "AXI_CYCLES_WRITE_DATA_STALLED" }
      }
   },
   {
      .name = "pe-pixel-count-killed-by-color-pipe",
      .type = ETNA_QUERY_PE_PIXEL_COUNT_KILLED_BY_COLOR_PIPE,
      .source = (const struct etna_perfmon_source[]) {
         { "PE", "PIXEL_COUNT_KILLED_BY_COLOR_PIPE" }
      }
   },
   {
      .name = "pe-pixel-count-killed-by-depth-pipe",
      .type = ETNA_QUERY_PE_PIXEL_COUNT_KILLED_BY_DEPTH_PIPE,
      .source = (const struct etna_perfmon_source[]) {
         { "PE", "PIXEL_COUNT_KILLED_BY_DEPTH_PIPE" }
      }
   },
   {
      .name = "pe-pixel-count-drawn-by-color-pipe",
      .type = ETNA_QUERY_PE_PIXEL_COUNT_DRAWN_BY_COLOR_PIPE,
      .source = (const struct etna_perfmon_source[]) {
         { "PE", "PIXEL_COUNT_DRAWN_BY_COLOR_PIPE" }
      }
   },
   {
      .name = "pe-pixel-count-drawn-by-depth-pipe",
      .type = ETNA_QUERY_PE_PIXEL_COUNT_DRAWN_BY_DEPTH_PIPE,
      .source = (const struct etna_perfmon_source[]) {
         { "PE", "PIXEL_COUNT_DRAWN_BY_DEPTH_PIPE" }
      }
   },
   {
      .name = "sh-shader-cycles",
      .type = ETNA_QUERY_SH_SHADER_CYCLES,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "SHADER_CYCLES" }
      }
   },
   {
      .name = "sh-ps-inst-counter",
      .type = ETNA_QUERY_SH_PS_INST_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "PS_INST_COUNTER" }
      }
   },
   {
      .name = "sh-rendered-pixel-counter",
      .type = ETNA_QUERY_SH_RENDERED_PIXEL_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "RENDERED_PIXEL_COUNTER" }
      }
   },
   {
      .name = "sh-vs-inst-counter",
      .type = ETNA_QUERY_SH_VS_INST_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "VS_INST_COUNTER" }
      }
   },
   {
      .name = "sh-rendered-vertice-counter",
      .type = ETNA_QUERY_SH_RENDERED_VERTICE_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "RENDERED_VERTICE_COUNTER" }
      }
   },
   {
      .name = "sh-vtx-branch-inst-counter",
      .type = ETNA_QUERY_SH_RENDERED_VERTICE_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "VTX_BRANCH_INST_COUNTER" }
      }
   },
   {
      .name = "sh-vtx-texld-inst-counter",
      .type = ETNA_QUERY_SH_RENDERED_VERTICE_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "VTX_TEXLD_INST_COUNTER" }
      }
   },
   {
      .name = "sh-plx-branch-inst-counter",
      .type = ETNA_QUERY_SH_RENDERED_VERTICE_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "PXL_BRANCH_INST_COUNTER" }
      }
   },
   {
      .name = "sh-plx-texld-inst-counter",
      .type = ETNA_QUERY_SH_RENDERED_VERTICE_COUNTER,
      .source = (const struct etna_perfmon_source[]) {
         { "SH", "PXL_TEXLD_INST_COUNTER" }
      }
   }
};

static const struct etna_perfmon_config *
etna_pm_query_config(unsigned type)
{
   for (unsigned i = 0; i < ARRAY_SIZE(query_config); i++)
      if (query_config[i].type == type)
         return &query_config[i];

   return NULL;
}

static struct etna_perfmon_signal *
etna_pm_query_signal(struct etna_perfmon *perfmon,
                     const struct etna_perfmon_source *source)
{
   struct etna_perfmon_domain *domain;

   domain = etna_perfmon_get_dom_by_name(perfmon, source->domain);
   if (!domain)
      return NULL;

   return etna_perfmon_get_sig_by_name(domain, source->signal);
}

static inline bool
etna_pm_cfg_supported(struct etna_perfmon *perfmon,
                      const struct etna_perfmon_config *cfg)
{
   struct etna_perfmon_signal *signal = etna_pm_query_signal(perfmon, cfg->source);

   return !!signal;
}

static inline void
etna_pm_add_signal(struct etna_pm_query *pq, struct etna_perfmon *perfmon,
                   const struct etna_perfmon_config *cfg)
{
   struct etna_perfmon_signal *signal = etna_pm_query_signal(perfmon, cfg->source);

   pq->signal = signal;
}

static bool
realloc_query_bo(struct etna_context *ctx, struct etna_pm_query *pq)
{
   if (pq->bo)
      etna_bo_del(pq->bo);

   pq->bo = etna_bo_new(ctx->screen->dev, 64, DRM_ETNA_GEM_CACHE_WC);
   if (unlikely(!pq->bo))
      return false;

   pq->data = etna_bo_map(pq->bo);

   return true;
}

static void
etna_pm_query_get(struct etna_cmd_stream *stream, struct etna_query *q,
                  unsigned flags)
{
   struct etna_pm_query *pq = etna_pm_query(q);
   unsigned offset;
   assert(flags);

   if (flags == ETNA_PM_PROCESS_PRE)
      offset = 2;
   else
      offset = 3;

   struct etna_perf p = {
      .flags = flags,
      .sequence = pq->sequence,
      .bo = pq->bo,
      .signal = pq->signal,
      .offset = offset
   };

   etna_cmd_stream_perf(stream, &p);
}

static inline void
etna_pm_query_update(struct etna_query *q)
{
   struct etna_pm_query *pq = etna_pm_query(q);

   if (pq->data[0] == pq->sequence)
      pq->ready = true;
}

static void
etna_pm_destroy_query(struct etna_context *ctx, struct etna_query *q)
{
   struct etna_pm_query *pq = etna_pm_query(q);

   etna_bo_del(pq->bo);
   FREE(pq);
}

static boolean
etna_pm_begin_query(struct etna_context *ctx, struct etna_query *q)
{
   struct etna_pm_query *pq = etna_pm_query(q);

   pq->ready = false;
   pq->sequence++;

   etna_pm_query_get(ctx->stream, q, ETNA_PM_PROCESS_PRE);

   return true;
}

static void
etna_pm_end_query(struct etna_context *ctx, struct etna_query *q)
{
   etna_pm_query_get(ctx->stream, q, ETNA_PM_PROCESS_POST);
}

static boolean
etna_pm_get_query_result(struct etna_context *ctx, struct etna_query *q,
                         boolean wait, union pipe_query_result *result)
{
   struct etna_pm_query *pq = etna_pm_query(q);

   etna_pm_query_update(q);

   if (!pq->ready) {
      if (!wait)
         return false;

      if (!etna_bo_cpu_prep(pq->bo, DRM_ETNA_PREP_READ))
         return false;

      pq->ready = true;
      etna_bo_cpu_fini(pq->bo);
   }

   result->u32 = pq->data[2] - pq->data[1];

   return true;
}

static const struct etna_query_funcs hw_query_funcs = {
   .destroy_query = etna_pm_destroy_query,
   .begin_query = etna_pm_begin_query,
   .end_query = etna_pm_end_query,
   .get_query_result = etna_pm_get_query_result,
};

struct etna_query *
etna_pm_create_query(struct etna_context *ctx, unsigned query_type)
{
   struct etna_perfmon *perfmon = ctx->screen->perfmon;
   const struct etna_perfmon_config *cfg;
   struct etna_pm_query *pq;
   struct etna_query *q;

   cfg = etna_pm_query_config(query_type);
   if (!cfg)
      return NULL;

   if (!etna_pm_cfg_supported(perfmon, cfg))
      return NULL;

   pq = CALLOC_STRUCT(etna_pm_query);
   if (!pq)
      return NULL;

   if (!realloc_query_bo(ctx, pq)) {
      FREE(pq);
      return NULL;
   }

   q = &pq->base;
   q->funcs = &hw_query_funcs;
   q->type = query_type;

   etna_pm_add_signal(pq, perfmon, cfg);

   return q;
}

void
etna_pm_query_setup(struct etna_screen *screen)
{
   screen->perfmon = etna_perfmon_create(screen->pipe);

   if (!screen->perfmon)
      return;

   for (unsigned i = 0; i < ARRAY_SIZE(query_config); i++) {
      const struct etna_perfmon_config *cfg = &query_config[i];

      if (!etna_pm_cfg_supported(screen->perfmon, cfg))
         continue;

      util_dynarray_append(&screen->supported_pm_queries, unsigned, i);
   }
}

int
etna_pm_get_driver_query_info(struct pipe_screen *pscreen, unsigned index,
                              struct pipe_driver_query_info *info)
{
   const struct etna_screen *screen = etna_screen(pscreen);
   const unsigned num = screen->supported_pm_queries.size / sizeof(unsigned);
   unsigned i;

   if (!info)
      return num;

   if (index >= num)
      return 0;

   i = *util_dynarray_element(&screen->supported_pm_queries, unsigned, index);
   assert(i < ARRAY_SIZE(query_config));

   info->name = query_config[i].name;
   info->query_type = query_config[i].type;
   info->group_id = 0;

   return 1;
}