intel/perf: fix debug typo
[mesa.git] / src / intel / perf / gen_perf.c
1 /*
2 * Copyright © 2018 Intel Corporation
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 #include <dirent.h>
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <errno.h>
31
32 #include <drm-uapi/i915_drm.h>
33
34 #include "common/gen_gem.h"
35 #include "gen_perf.h"
36 #include "perf/gen_perf_mdapi.h"
37 #include "perf/gen_perf_metrics.h"
38
39 #include "dev/gen_debug.h"
40 #include "dev/gen_device_info.h"
41 #include "util/bitscan.h"
42 #include "util/u_math.h"
43
44 #define FILE_DEBUG_FLAG DEBUG_PERFMON
45 #define MI_RPC_BO_SIZE 4096
46 #define MI_FREQ_START_OFFSET_BYTES (3072)
47 #define MI_RPC_BO_END_OFFSET_BYTES (MI_RPC_BO_SIZE / 2)
48 #define MI_FREQ_END_OFFSET_BYTES (3076)
49
50 #define INTEL_MASK(high, low) (((1u<<((high)-(low)+1))-1)<<(low))
51
52 #define GEN7_RPSTAT1 0xA01C
53 #define GEN7_RPSTAT1_CURR_GT_FREQ_SHIFT 7
54 #define GEN7_RPSTAT1_CURR_GT_FREQ_MASK INTEL_MASK(13, 7)
55 #define GEN7_RPSTAT1_PREV_GT_FREQ_SHIFT 0
56 #define GEN7_RPSTAT1_PREV_GT_FREQ_MASK INTEL_MASK(6, 0)
57
58 #define GEN9_RPSTAT0 0xA01C
59 #define GEN9_RPSTAT0_CURR_GT_FREQ_SHIFT 23
60 #define GEN9_RPSTAT0_CURR_GT_FREQ_MASK INTEL_MASK(31, 23)
61 #define GEN9_RPSTAT0_PREV_GT_FREQ_SHIFT 0
62 #define GEN9_RPSTAT0_PREV_GT_FREQ_MASK INTEL_MASK(8, 0)
63
64 #define GEN6_SO_PRIM_STORAGE_NEEDED 0x2280
65 #define GEN7_SO_PRIM_STORAGE_NEEDED(n) (0x5240 + (n) * 8)
66 #define GEN6_SO_NUM_PRIMS_WRITTEN 0x2288
67 #define GEN7_SO_NUM_PRIMS_WRITTEN(n) (0x5200 + (n) * 8)
68
69 #define MAP_READ (1 << 0)
70 #define MAP_WRITE (1 << 1)
71
72 /**
73 * Periodic OA samples are read() into these buffer structures via the
74 * i915 perf kernel interface and appended to the
75 * perf_ctx->sample_buffers linked list. When we process the
76 * results of an OA metrics query we need to consider all the periodic
77 * samples between the Begin and End MI_REPORT_PERF_COUNT command
78 * markers.
79 *
80 * 'Periodic' is a simplification as there are other automatic reports
81 * written by the hardware also buffered here.
82 *
83 * Considering three queries, A, B and C:
84 *
85 * Time ---->
86 * ________________A_________________
87 * | |
88 * | ________B_________ _____C___________
89 * | | | | | |
90 *
91 * And an illustration of sample buffers read over this time frame:
92 * [HEAD ][ ][ ][ ][ ][ ][ ][ ][TAIL ]
93 *
94 * These nodes may hold samples for query A:
95 * [ ][ ][ A ][ A ][ A ][ A ][ A ][ ][ ]
96 *
97 * These nodes may hold samples for query B:
98 * [ ][ ][ B ][ B ][ B ][ ][ ][ ][ ]
99 *
100 * These nodes may hold samples for query C:
101 * [ ][ ][ ][ ][ ][ C ][ C ][ C ][ ]
102 *
103 * The illustration assumes we have an even distribution of periodic
104 * samples so all nodes have the same size plotted against time:
105 *
106 * Note, to simplify code, the list is never empty.
107 *
108 * With overlapping queries we can see that periodic OA reports may
109 * relate to multiple queries and care needs to be take to keep
110 * track of sample buffers until there are no queries that might
111 * depend on their contents.
112 *
113 * We use a node ref counting system where a reference ensures that a
114 * node and all following nodes can't be freed/recycled until the
115 * reference drops to zero.
116 *
117 * E.g. with a ref of one here:
118 * [ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ]
119 *
120 * These nodes could be freed or recycled ("reaped"):
121 * [ 0 ][ 0 ]
122 *
123 * These must be preserved until the leading ref drops to zero:
124 * [ 1 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ]
125 *
126 * When a query starts we take a reference on the current tail of
127 * the list, knowing that no already-buffered samples can possibly
128 * relate to the newly-started query. A pointer to this node is
129 * also saved in the query object's ->oa.samples_head.
130 *
131 * E.g. starting query A while there are two nodes in .sample_buffers:
132 * ________________A________
133 * |
134 *
135 * [ 0 ][ 1 ]
136 * ^_______ Add a reference and store pointer to node in
137 * A->oa.samples_head
138 *
139 * Moving forward to when the B query starts with no new buffer nodes:
140 * (for reference, i915 perf reads() are only done when queries finish)
141 * ________________A_______
142 * | ________B___
143 * | |
144 *
145 * [ 0 ][ 2 ]
146 * ^_______ Add a reference and store pointer to
147 * node in B->oa.samples_head
148 *
149 * Once a query is finished, after an OA query has become 'Ready',
150 * once the End OA report has landed and after we we have processed
151 * all the intermediate periodic samples then we drop the
152 * ->oa.samples_head reference we took at the start.
153 *
154 * So when the B query has finished we have:
155 * ________________A________
156 * | ______B___________
157 * | | |
158 * [ 0 ][ 1 ][ 0 ][ 0 ][ 0 ]
159 * ^_______ Drop B->oa.samples_head reference
160 *
161 * We still can't free these due to the A->oa.samples_head ref:
162 * [ 1 ][ 0 ][ 0 ][ 0 ]
163 *
164 * When the A query finishes: (note there's a new ref for C's samples_head)
165 * ________________A_________________
166 * | |
167 * | _____C_________
168 * | | |
169 * [ 0 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 0 ]
170 * ^_______ Drop A->oa.samples_head reference
171 *
172 * And we can now reap these nodes up to the C->oa.samples_head:
173 * [ X ][ X ][ X ][ X ]
174 * keeping -> [ 1 ][ 0 ][ 0 ]
175 *
176 * We reap old sample buffers each time we finish processing an OA
177 * query by iterating the sample_buffers list from the head until we
178 * find a referenced node and stop.
179 *
180 * Reaped buffers move to a perfquery.free_sample_buffers list and
181 * when we come to read() we first look to recycle a buffer from the
182 * free_sample_buffers list before allocating a new buffer.
183 */
184 struct oa_sample_buf {
185 struct exec_node link;
186 int refcount;
187 int len;
188 uint8_t buf[I915_PERF_OA_SAMPLE_SIZE * 10];
189 uint32_t last_timestamp;
190 };
191
192 /**
193 * gen representation of a performance query object.
194 *
195 * NB: We want to keep this structure relatively lean considering that
196 * applications may expect to allocate enough objects to be able to
197 * query around all draw calls in a frame.
198 */
199 struct gen_perf_query_object
200 {
201 const struct gen_perf_query_info *queryinfo;
202
203 /* See query->kind to know which state below is in use... */
204 union {
205 struct {
206
207 /**
208 * BO containing OA counter snapshots at query Begin/End time.
209 */
210 void *bo;
211
212 /**
213 * Address of mapped of @bo
214 */
215 void *map;
216
217 /**
218 * The MI_REPORT_PERF_COUNT command lets us specify a unique
219 * ID that will be reflected in the resulting OA report
220 * that's written by the GPU. This is the ID we're expecting
221 * in the begin report and the the end report should be
222 * @begin_report_id + 1.
223 */
224 int begin_report_id;
225
226 /**
227 * Reference the head of the brw->perfquery.sample_buffers
228 * list at the time that the query started (so we only need
229 * to look at nodes after this point when looking for samples
230 * related to this query)
231 *
232 * (See struct brw_oa_sample_buf description for more details)
233 */
234 struct exec_node *samples_head;
235
236 /**
237 * false while in the unaccumulated_elements list, and set to
238 * true when the final, end MI_RPC snapshot has been
239 * accumulated.
240 */
241 bool results_accumulated;
242
243 /**
244 * Frequency of the GT at begin and end of the query.
245 */
246 uint64_t gt_frequency[2];
247
248 /**
249 * Accumulated OA results between begin and end of the query.
250 */
251 struct gen_perf_query_result result;
252 } oa;
253
254 struct {
255 /**
256 * BO containing starting and ending snapshots for the
257 * statistics counters.
258 */
259 void *bo;
260 } pipeline_stats;
261 };
262 };
263
264 struct gen_perf_context {
265 struct gen_perf_config *perf;
266
267 void * ctx; /* driver context (eg, brw_context) */
268 void * bufmgr;
269 const struct gen_device_info *devinfo;
270
271 uint32_t hw_ctx;
272 int drm_fd;
273
274 /* The i915 perf stream we open to setup + enable the OA counters */
275 int oa_stream_fd;
276
277 /* An i915 perf stream fd gives exclusive access to the OA unit that will
278 * report counter snapshots for a specific counter set/profile in a
279 * specific layout/format so we can only start OA queries that are
280 * compatible with the currently open fd...
281 */
282 int current_oa_metrics_set_id;
283 int current_oa_format;
284
285 /* List of buffers containing OA reports */
286 struct exec_list sample_buffers;
287
288 /* Cached list of empty sample buffers */
289 struct exec_list free_sample_buffers;
290
291 int n_active_oa_queries;
292 int n_active_pipeline_stats_queries;
293
294 /* The number of queries depending on running OA counters which
295 * extends beyond brw_end_perf_query() since we need to wait until
296 * the last MI_RPC command has parsed by the GPU.
297 *
298 * Accurate accounting is important here as emitting an
299 * MI_REPORT_PERF_COUNT command while the OA unit is disabled will
300 * effectively hang the gpu.
301 */
302 int n_oa_users;
303
304 /* To help catch an spurious problem with the hardware or perf
305 * forwarding samples, we emit each MI_REPORT_PERF_COUNT command
306 * with a unique ID that we can explicitly check for...
307 */
308 int next_query_start_report_id;
309
310 /**
311 * An array of queries whose results haven't yet been assembled
312 * based on the data in buffer objects.
313 *
314 * These may be active, or have already ended. However, the
315 * results have not been requested.
316 */
317 struct gen_perf_query_object **unaccumulated;
318 int unaccumulated_elements;
319 int unaccumulated_array_size;
320
321 /* The total number of query objects so we can relinquish
322 * our exclusive access to perf if the application deletes
323 * all of its objects. (NB: We only disable perf while
324 * there are no active queries)
325 */
326 int n_query_instances;
327 };
328
329 const struct gen_perf_query_info*
330 gen_perf_query_info(const struct gen_perf_query_object *query)
331 {
332 return query->queryinfo;
333 }
334
335 struct gen_perf_context *
336 gen_perf_new_context(void *parent)
337 {
338 struct gen_perf_context *ctx = rzalloc(parent, struct gen_perf_context);
339 if (! ctx)
340 fprintf(stderr, "%s: failed to alloc context\n", __func__);
341 return ctx;
342 }
343
344 struct gen_perf_config *
345 gen_perf_config(struct gen_perf_context *ctx)
346 {
347 return ctx->perf;
348 }
349
350 struct gen_perf_query_object *
351 gen_perf_new_query(struct gen_perf_context *perf_ctx, unsigned query_index)
352 {
353 const struct gen_perf_query_info *query =
354 &perf_ctx->perf->queries[query_index];
355 struct gen_perf_query_object *obj =
356 calloc(1, sizeof(struct gen_perf_query_object));
357
358 if (!obj)
359 return NULL;
360
361 obj->queryinfo = query;
362
363 perf_ctx->n_query_instances++;
364 return obj;
365 }
366
367 int
368 gen_perf_active_queries(struct gen_perf_context *perf_ctx,
369 const struct gen_perf_query_info *query)
370 {
371 assert(perf_ctx->n_active_oa_queries == 0 || perf_ctx->n_active_pipeline_stats_queries == 0);
372
373 switch (query->kind) {
374 case GEN_PERF_QUERY_TYPE_OA:
375 case GEN_PERF_QUERY_TYPE_RAW:
376 return perf_ctx->n_active_oa_queries;
377 break;
378
379 case GEN_PERF_QUERY_TYPE_PIPELINE:
380 return perf_ctx->n_active_pipeline_stats_queries;
381 break;
382
383 default:
384 unreachable("Unknown query type");
385 break;
386 }
387 }
388
389 static bool
390 get_sysfs_dev_dir(struct gen_perf_config *perf, int fd)
391 {
392 struct stat sb;
393 int min, maj;
394 DIR *drmdir;
395 struct dirent *drm_entry;
396 int len;
397
398 perf->sysfs_dev_dir[0] = '\0';
399
400 if (fstat(fd, &sb)) {
401 DBG("Failed to stat DRM fd\n");
402 return false;
403 }
404
405 maj = major(sb.st_rdev);
406 min = minor(sb.st_rdev);
407
408 if (!S_ISCHR(sb.st_mode)) {
409 DBG("DRM fd is not a character device as expected\n");
410 return false;
411 }
412
413 len = snprintf(perf->sysfs_dev_dir,
414 sizeof(perf->sysfs_dev_dir),
415 "/sys/dev/char/%d:%d/device/drm", maj, min);
416 if (len < 0 || len >= sizeof(perf->sysfs_dev_dir)) {
417 DBG("Failed to concatenate sysfs path to drm device\n");
418 return false;
419 }
420
421 drmdir = opendir(perf->sysfs_dev_dir);
422 if (!drmdir) {
423 DBG("Failed to open %s: %m\n", perf->sysfs_dev_dir);
424 return false;
425 }
426
427 while ((drm_entry = readdir(drmdir))) {
428 if ((drm_entry->d_type == DT_DIR ||
429 drm_entry->d_type == DT_LNK) &&
430 strncmp(drm_entry->d_name, "card", 4) == 0)
431 {
432 len = snprintf(perf->sysfs_dev_dir,
433 sizeof(perf->sysfs_dev_dir),
434 "/sys/dev/char/%d:%d/device/drm/%s",
435 maj, min, drm_entry->d_name);
436 closedir(drmdir);
437 if (len < 0 || len >= sizeof(perf->sysfs_dev_dir))
438 return false;
439 else
440 return true;
441 }
442 }
443
444 closedir(drmdir);
445
446 DBG("Failed to find cardX directory under /sys/dev/char/%d:%d/device/drm\n",
447 maj, min);
448
449 return false;
450 }
451
452 static bool
453 read_file_uint64(const char *file, uint64_t *val)
454 {
455 char buf[32];
456 int fd, n;
457
458 fd = open(file, 0);
459 if (fd < 0)
460 return false;
461 while ((n = read(fd, buf, sizeof (buf) - 1)) < 0 &&
462 errno == EINTR);
463 close(fd);
464 if (n < 0)
465 return false;
466
467 buf[n] = '\0';
468 *val = strtoull(buf, NULL, 0);
469
470 return true;
471 }
472
473 static bool
474 read_sysfs_drm_device_file_uint64(struct gen_perf_config *perf,
475 const char *file,
476 uint64_t *value)
477 {
478 char buf[512];
479 int len;
480
481 len = snprintf(buf, sizeof(buf), "%s/%s", perf->sysfs_dev_dir, file);
482 if (len < 0 || len >= sizeof(buf)) {
483 DBG("Failed to concatenate sys filename to read u64 from\n");
484 return false;
485 }
486
487 return read_file_uint64(buf, value);
488 }
489
490 static inline struct gen_perf_query_info *
491 append_query_info(struct gen_perf_config *perf, int max_counters)
492 {
493 struct gen_perf_query_info *query;
494
495 perf->queries = reralloc(perf, perf->queries,
496 struct gen_perf_query_info,
497 ++perf->n_queries);
498 query = &perf->queries[perf->n_queries - 1];
499 memset(query, 0, sizeof(*query));
500
501 if (max_counters > 0) {
502 query->max_counters = max_counters;
503 query->counters =
504 rzalloc_array(perf, struct gen_perf_query_counter, max_counters);
505 }
506
507 return query;
508 }
509
510 static void
511 register_oa_config(struct gen_perf_config *perf,
512 const struct gen_perf_query_info *query,
513 uint64_t config_id)
514 {
515 struct gen_perf_query_info *registered_query = append_query_info(perf, 0);
516
517 *registered_query = *query;
518 registered_query->oa_metrics_set_id = config_id;
519 DBG("metric set registered: id = %" PRIu64", guid = %s\n",
520 registered_query->oa_metrics_set_id, query->guid);
521 }
522
523 static void
524 enumerate_sysfs_metrics(struct gen_perf_config *perf)
525 {
526 DIR *metricsdir = NULL;
527 struct dirent *metric_entry;
528 char buf[256];
529 int len;
530
531 len = snprintf(buf, sizeof(buf), "%s/metrics", perf->sysfs_dev_dir);
532 if (len < 0 || len >= sizeof(buf)) {
533 DBG("Failed to concatenate path to sysfs metrics/ directory\n");
534 return;
535 }
536
537 metricsdir = opendir(buf);
538 if (!metricsdir) {
539 DBG("Failed to open %s: %m\n", buf);
540 return;
541 }
542
543 while ((metric_entry = readdir(metricsdir))) {
544 struct hash_entry *entry;
545
546 if ((metric_entry->d_type != DT_DIR &&
547 metric_entry->d_type != DT_LNK) ||
548 metric_entry->d_name[0] == '.')
549 continue;
550
551 DBG("metric set: %s\n", metric_entry->d_name);
552 entry = _mesa_hash_table_search(perf->oa_metrics_table,
553 metric_entry->d_name);
554 if (entry) {
555 uint64_t id;
556
557 len = snprintf(buf, sizeof(buf), "%s/metrics/%s/id",
558 perf->sysfs_dev_dir, metric_entry->d_name);
559 if (len < 0 || len >= sizeof(buf)) {
560 DBG("Failed to concatenate path to sysfs metric id file\n");
561 continue;
562 }
563
564 if (!read_file_uint64(buf, &id)) {
565 DBG("Failed to read metric set id from %s: %m", buf);
566 continue;
567 }
568
569 register_oa_config(perf, (const struct gen_perf_query_info *)entry->data, id);
570 } else
571 DBG("metric set not known by mesa (skipping)\n");
572 }
573
574 closedir(metricsdir);
575 }
576
577 static bool
578 kernel_has_dynamic_config_support(struct gen_perf_config *perf, int fd)
579 {
580 uint64_t invalid_config_id = UINT64_MAX;
581
582 return gen_ioctl(fd, DRM_IOCTL_I915_PERF_REMOVE_CONFIG,
583 &invalid_config_id) < 0 && errno == ENOENT;
584 }
585
586 static bool
587 load_metric_id(struct gen_perf_config *perf, const char *guid,
588 uint64_t *metric_id)
589 {
590 char config_path[280];
591
592 snprintf(config_path, sizeof(config_path), "%s/metrics/%s/id",
593 perf->sysfs_dev_dir, guid);
594
595 /* Don't recreate already loaded configs. */
596 return read_file_uint64(config_path, metric_id);
597 }
598
599 static void
600 init_oa_configs(struct gen_perf_config *perf, int fd)
601 {
602 hash_table_foreach(perf->oa_metrics_table, entry) {
603 const struct gen_perf_query_info *query = entry->data;
604 struct drm_i915_perf_oa_config config;
605 uint64_t config_id;
606 int ret;
607
608 if (load_metric_id(perf, query->guid, &config_id)) {
609 DBG("metric set: %s (already loaded)\n", query->guid);
610 register_oa_config(perf, query, config_id);
611 continue;
612 }
613
614 memset(&config, 0, sizeof(config));
615
616 memcpy(config.uuid, query->guid, sizeof(config.uuid));
617
618 config.n_mux_regs = query->n_mux_regs;
619 config.mux_regs_ptr = (uintptr_t) query->mux_regs;
620
621 config.n_boolean_regs = query->n_b_counter_regs;
622 config.boolean_regs_ptr = (uintptr_t) query->b_counter_regs;
623
624 config.n_flex_regs = query->n_flex_regs;
625 config.flex_regs_ptr = (uintptr_t) query->flex_regs;
626
627 ret = gen_ioctl(fd, DRM_IOCTL_I915_PERF_ADD_CONFIG, &config);
628 if (ret < 0) {
629 DBG("Failed to load \"%s\" (%s) metrics set in kernel: %s\n",
630 query->name, query->guid, strerror(errno));
631 continue;
632 }
633
634 register_oa_config(perf, query, ret);
635 DBG("metric set: %s (added)\n", query->guid);
636 }
637 }
638
639 static void
640 compute_topology_builtins(struct gen_perf_config *perf,
641 const struct gen_device_info *devinfo)
642 {
643 perf->sys_vars.slice_mask = devinfo->slice_masks;
644 perf->sys_vars.n_eu_slices = devinfo->num_slices;
645
646 for (int i = 0; i < sizeof(devinfo->subslice_masks[i]); i++) {
647 perf->sys_vars.n_eu_sub_slices +=
648 __builtin_popcount(devinfo->subslice_masks[i]);
649 }
650
651 for (int i = 0; i < sizeof(devinfo->eu_masks); i++)
652 perf->sys_vars.n_eus += __builtin_popcount(devinfo->eu_masks[i]);
653
654 perf->sys_vars.eu_threads_count = devinfo->num_thread_per_eu;
655
656 /* The subslice mask builtin contains bits for all slices. Prior to Gen11
657 * it had groups of 3bits for each slice, on Gen11 it's 8bits for each
658 * slice.
659 *
660 * Ideally equations would be updated to have a slice/subslice query
661 * function/operator.
662 */
663 perf->sys_vars.subslice_mask = 0;
664
665 int bits_per_subslice = devinfo->gen == 11 ? 8 : 3;
666
667 for (int s = 0; s < util_last_bit(devinfo->slice_masks); s++) {
668 for (int ss = 0; ss < (devinfo->subslice_slice_stride * 8); ss++) {
669 if (gen_device_info_subslice_available(devinfo, s, ss))
670 perf->sys_vars.subslice_mask |= 1ULL << (s * bits_per_subslice + ss);
671 }
672 }
673 }
674
675 static bool
676 init_oa_sys_vars(struct gen_perf_config *perf, const struct gen_device_info *devinfo)
677 {
678 uint64_t min_freq_mhz = 0, max_freq_mhz = 0;
679
680 if (!read_sysfs_drm_device_file_uint64(perf, "gt_min_freq_mhz", &min_freq_mhz))
681 return false;
682
683 if (!read_sysfs_drm_device_file_uint64(perf, "gt_max_freq_mhz", &max_freq_mhz))
684 return false;
685
686 memset(&perf->sys_vars, 0, sizeof(perf->sys_vars));
687 perf->sys_vars.gt_min_freq = min_freq_mhz * 1000000;
688 perf->sys_vars.gt_max_freq = max_freq_mhz * 1000000;
689 perf->sys_vars.timestamp_frequency = devinfo->timestamp_frequency;
690 perf->sys_vars.revision = devinfo->revision;
691 compute_topology_builtins(perf, devinfo);
692
693 return true;
694 }
695
696 typedef void (*perf_register_oa_queries_t)(struct gen_perf_config *);
697
698 static perf_register_oa_queries_t
699 get_register_queries_function(const struct gen_device_info *devinfo)
700 {
701 if (devinfo->is_haswell)
702 return gen_oa_register_queries_hsw;
703 if (devinfo->is_cherryview)
704 return gen_oa_register_queries_chv;
705 if (devinfo->is_broadwell)
706 return gen_oa_register_queries_bdw;
707 if (devinfo->is_broxton)
708 return gen_oa_register_queries_bxt;
709 if (devinfo->is_skylake) {
710 if (devinfo->gt == 2)
711 return gen_oa_register_queries_sklgt2;
712 if (devinfo->gt == 3)
713 return gen_oa_register_queries_sklgt3;
714 if (devinfo->gt == 4)
715 return gen_oa_register_queries_sklgt4;
716 }
717 if (devinfo->is_kabylake) {
718 if (devinfo->gt == 2)
719 return gen_oa_register_queries_kblgt2;
720 if (devinfo->gt == 3)
721 return gen_oa_register_queries_kblgt3;
722 }
723 if (devinfo->is_geminilake)
724 return gen_oa_register_queries_glk;
725 if (devinfo->is_coffeelake) {
726 if (devinfo->gt == 2)
727 return gen_oa_register_queries_cflgt2;
728 if (devinfo->gt == 3)
729 return gen_oa_register_queries_cflgt3;
730 }
731 if (devinfo->is_cannonlake)
732 return gen_oa_register_queries_cnl;
733 if (devinfo->gen == 11)
734 return gen_oa_register_queries_icl;
735
736 return NULL;
737 }
738
739 static inline void
740 add_stat_reg(struct gen_perf_query_info *query, uint32_t reg,
741 uint32_t numerator, uint32_t denominator,
742 const char *name, const char *description)
743 {
744 struct gen_perf_query_counter *counter;
745
746 assert(query->n_counters < query->max_counters);
747
748 counter = &query->counters[query->n_counters];
749 counter->name = name;
750 counter->desc = description;
751 counter->type = GEN_PERF_COUNTER_TYPE_RAW;
752 counter->data_type = GEN_PERF_COUNTER_DATA_TYPE_UINT64;
753 counter->offset = sizeof(uint64_t) * query->n_counters;
754 counter->pipeline_stat.reg = reg;
755 counter->pipeline_stat.numerator = numerator;
756 counter->pipeline_stat.denominator = denominator;
757
758 query->n_counters++;
759 }
760
761 static inline void
762 add_basic_stat_reg(struct gen_perf_query_info *query,
763 uint32_t reg, const char *name)
764 {
765 add_stat_reg(query, reg, 1, 1, name, name);
766 }
767
768 static void
769 load_pipeline_statistic_metrics(struct gen_perf_config *perf_cfg,
770 const struct gen_device_info *devinfo)
771 {
772 struct gen_perf_query_info *query =
773 append_query_info(perf_cfg, MAX_STAT_COUNTERS);
774
775 query->kind = GEN_PERF_QUERY_TYPE_PIPELINE;
776 query->name = "Pipeline Statistics Registers";
777
778 add_basic_stat_reg(query, IA_VERTICES_COUNT,
779 "N vertices submitted");
780 add_basic_stat_reg(query, IA_PRIMITIVES_COUNT,
781 "N primitives submitted");
782 add_basic_stat_reg(query, VS_INVOCATION_COUNT,
783 "N vertex shader invocations");
784
785 if (devinfo->gen == 6) {
786 add_stat_reg(query, GEN6_SO_PRIM_STORAGE_NEEDED, 1, 1,
787 "SO_PRIM_STORAGE_NEEDED",
788 "N geometry shader stream-out primitives (total)");
789 add_stat_reg(query, GEN6_SO_NUM_PRIMS_WRITTEN, 1, 1,
790 "SO_NUM_PRIMS_WRITTEN",
791 "N geometry shader stream-out primitives (written)");
792 } else {
793 add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
794 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
795 "N stream-out (stream 0) primitives (total)");
796 add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
797 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
798 "N stream-out (stream 1) primitives (total)");
799 add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
800 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
801 "N stream-out (stream 2) primitives (total)");
802 add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
803 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
804 "N stream-out (stream 3) primitives (total)");
805 add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
806 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
807 "N stream-out (stream 0) primitives (written)");
808 add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
809 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
810 "N stream-out (stream 1) primitives (written)");
811 add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
812 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
813 "N stream-out (stream 2) primitives (written)");
814 add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
815 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
816 "N stream-out (stream 3) primitives (written)");
817 }
818
819 add_basic_stat_reg(query, HS_INVOCATION_COUNT,
820 "N TCS shader invocations");
821 add_basic_stat_reg(query, DS_INVOCATION_COUNT,
822 "N TES shader invocations");
823
824 add_basic_stat_reg(query, GS_INVOCATION_COUNT,
825 "N geometry shader invocations");
826 add_basic_stat_reg(query, GS_PRIMITIVES_COUNT,
827 "N geometry shader primitives emitted");
828
829 add_basic_stat_reg(query, CL_INVOCATION_COUNT,
830 "N primitives entering clipping");
831 add_basic_stat_reg(query, CL_PRIMITIVES_COUNT,
832 "N primitives leaving clipping");
833
834 if (devinfo->is_haswell || devinfo->gen == 8) {
835 add_stat_reg(query, PS_INVOCATION_COUNT, 1, 4,
836 "N fragment shader invocations",
837 "N fragment shader invocations");
838 } else {
839 add_basic_stat_reg(query, PS_INVOCATION_COUNT,
840 "N fragment shader invocations");
841 }
842
843 add_basic_stat_reg(query, PS_DEPTH_COUNT,
844 "N z-pass fragments");
845
846 if (devinfo->gen >= 7) {
847 add_basic_stat_reg(query, CS_INVOCATION_COUNT,
848 "N compute shader invocations");
849 }
850
851 query->data_size = sizeof(uint64_t) * query->n_counters;
852 }
853
854 static bool
855 load_oa_metrics(struct gen_perf_config *perf, int fd,
856 const struct gen_device_info *devinfo)
857 {
858 perf_register_oa_queries_t oa_register = get_register_queries_function(devinfo);
859 bool i915_perf_oa_available = false;
860 struct stat sb;
861
862 /* The existence of this sysctl parameter implies the kernel supports
863 * the i915 perf interface.
864 */
865 if (stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb) == 0) {
866
867 /* If _paranoid == 1 then on Gen8+ we won't be able to access OA
868 * metrics unless running as root.
869 */
870 if (devinfo->is_haswell)
871 i915_perf_oa_available = true;
872 else {
873 uint64_t paranoid = 1;
874
875 read_file_uint64("/proc/sys/dev/i915/perf_stream_paranoid", &paranoid);
876
877 if (paranoid == 0 || geteuid() == 0)
878 i915_perf_oa_available = true;
879 }
880 }
881
882 if (!i915_perf_oa_available ||
883 !oa_register ||
884 !get_sysfs_dev_dir(perf, fd) ||
885 !init_oa_sys_vars(perf, devinfo))
886 return false;
887
888 perf->oa_metrics_table =
889 _mesa_hash_table_create(perf, _mesa_key_hash_string,
890 _mesa_key_string_equal);
891
892 /* Index all the metric sets mesa knows about before looking to see what
893 * the kernel is advertising.
894 */
895 oa_register(perf);
896
897 if (likely((INTEL_DEBUG & DEBUG_NO_OACONFIG) == 0) &&
898 kernel_has_dynamic_config_support(perf, fd))
899 init_oa_configs(perf, fd);
900 else
901 enumerate_sysfs_metrics(perf);
902
903 return true;
904 }
905
906 /* Accumulate 32bits OA counters */
907 static inline void
908 accumulate_uint32(const uint32_t *report0,
909 const uint32_t *report1,
910 uint64_t *accumulator)
911 {
912 *accumulator += (uint32_t)(*report1 - *report0);
913 }
914
915 /* Accumulate 40bits OA counters */
916 static inline void
917 accumulate_uint40(int a_index,
918 const uint32_t *report0,
919 const uint32_t *report1,
920 uint64_t *accumulator)
921 {
922 const uint8_t *high_bytes0 = (uint8_t *)(report0 + 40);
923 const uint8_t *high_bytes1 = (uint8_t *)(report1 + 40);
924 uint64_t high0 = (uint64_t)(high_bytes0[a_index]) << 32;
925 uint64_t high1 = (uint64_t)(high_bytes1[a_index]) << 32;
926 uint64_t value0 = report0[a_index + 4] | high0;
927 uint64_t value1 = report1[a_index + 4] | high1;
928 uint64_t delta;
929
930 if (value0 > value1)
931 delta = (1ULL << 40) + value1 - value0;
932 else
933 delta = value1 - value0;
934
935 *accumulator += delta;
936 }
937
938 static void
939 gen8_read_report_clock_ratios(const uint32_t *report,
940 uint64_t *slice_freq_hz,
941 uint64_t *unslice_freq_hz)
942 {
943 /* The lower 16bits of the RPT_ID field of the OA reports contains a
944 * snapshot of the bits coming from the RP_FREQ_NORMAL register and is
945 * divided this way :
946 *
947 * RPT_ID[31:25]: RP_FREQ_NORMAL[20:14] (low squashed_slice_clock_frequency)
948 * RPT_ID[10:9]: RP_FREQ_NORMAL[22:21] (high squashed_slice_clock_frequency)
949 * RPT_ID[8:0]: RP_FREQ_NORMAL[31:23] (squashed_unslice_clock_frequency)
950 *
951 * RP_FREQ_NORMAL[31:23]: Software Unslice Ratio Request
952 * Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
953 *
954 * RP_FREQ_NORMAL[22:14]: Software Slice Ratio Request
955 * Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
956 */
957
958 uint32_t unslice_freq = report[0] & 0x1ff;
959 uint32_t slice_freq_low = (report[0] >> 25) & 0x7f;
960 uint32_t slice_freq_high = (report[0] >> 9) & 0x3;
961 uint32_t slice_freq = slice_freq_low | (slice_freq_high << 7);
962
963 *slice_freq_hz = slice_freq * 16666667ULL;
964 *unslice_freq_hz = unslice_freq * 16666667ULL;
965 }
966
967 static void
968 query_result_read_frequencies(struct gen_perf_query_result *result,
969 const struct gen_device_info *devinfo,
970 const uint32_t *start,
971 const uint32_t *end)
972 {
973 /* Slice/Unslice frequency is only available in the OA reports when the
974 * "Disable OA reports due to clock ratio change" field in
975 * OA_DEBUG_REGISTER is set to 1. This is how the kernel programs this
976 * global register (see drivers/gpu/drm/i915/i915_perf.c)
977 *
978 * Documentation says this should be available on Gen9+ but experimentation
979 * shows that Gen8 reports similar values, so we enable it there too.
980 */
981 if (devinfo->gen < 8)
982 return;
983
984 gen8_read_report_clock_ratios(start,
985 &result->slice_frequency[0],
986 &result->unslice_frequency[0]);
987 gen8_read_report_clock_ratios(end,
988 &result->slice_frequency[1],
989 &result->unslice_frequency[1]);
990 }
991
992 static void
993 query_result_accumulate(struct gen_perf_query_result *result,
994 const struct gen_perf_query_info *query,
995 const uint32_t *start,
996 const uint32_t *end)
997 {
998 int i, idx = 0;
999
1000 result->hw_id = start[2];
1001 result->reports_accumulated++;
1002
1003 switch (query->oa_format) {
1004 case I915_OA_FORMAT_A32u40_A4u32_B8_C8:
1005 accumulate_uint32(start + 1, end + 1, result->accumulator + idx++); /* timestamp */
1006 accumulate_uint32(start + 3, end + 3, result->accumulator + idx++); /* clock */
1007
1008 /* 32x 40bit A counters... */
1009 for (i = 0; i < 32; i++)
1010 accumulate_uint40(i, start, end, result->accumulator + idx++);
1011
1012 /* 4x 32bit A counters... */
1013 for (i = 0; i < 4; i++)
1014 accumulate_uint32(start + 36 + i, end + 36 + i, result->accumulator + idx++);
1015
1016 /* 8x 32bit B counters + 8x 32bit C counters... */
1017 for (i = 0; i < 16; i++)
1018 accumulate_uint32(start + 48 + i, end + 48 + i, result->accumulator + idx++);
1019 break;
1020
1021 case I915_OA_FORMAT_A45_B8_C8:
1022 accumulate_uint32(start + 1, end + 1, result->accumulator); /* timestamp */
1023
1024 for (i = 0; i < 61; i++)
1025 accumulate_uint32(start + 3 + i, end + 3 + i, result->accumulator + 1 + i);
1026 break;
1027
1028 default:
1029 unreachable("Can't accumulate OA counters in unknown format");
1030 }
1031
1032 }
1033
1034 static void
1035 query_result_clear(struct gen_perf_query_result *result)
1036 {
1037 memset(result, 0, sizeof(*result));
1038 result->hw_id = 0xffffffff; /* invalid */
1039 }
1040
1041 static void
1042 register_mdapi_statistic_query(struct gen_perf_config *perf_cfg,
1043 const struct gen_device_info *devinfo)
1044 {
1045 if (!(devinfo->gen >= 7 && devinfo->gen <= 11))
1046 return;
1047
1048 struct gen_perf_query_info *query =
1049 append_query_info(perf_cfg, MAX_STAT_COUNTERS);
1050
1051 query->kind = GEN_PERF_QUERY_TYPE_PIPELINE;
1052 query->name = "Intel_Raw_Pipeline_Statistics_Query";
1053
1054 /* The order has to match mdapi_pipeline_metrics. */
1055 add_basic_stat_reg(query, IA_VERTICES_COUNT,
1056 "N vertices submitted");
1057 add_basic_stat_reg(query, IA_PRIMITIVES_COUNT,
1058 "N primitives submitted");
1059 add_basic_stat_reg(query, VS_INVOCATION_COUNT,
1060 "N vertex shader invocations");
1061 add_basic_stat_reg(query, GS_INVOCATION_COUNT,
1062 "N geometry shader invocations");
1063 add_basic_stat_reg(query, GS_PRIMITIVES_COUNT,
1064 "N geometry shader primitives emitted");
1065 add_basic_stat_reg(query, CL_INVOCATION_COUNT,
1066 "N primitives entering clipping");
1067 add_basic_stat_reg(query, CL_PRIMITIVES_COUNT,
1068 "N primitives leaving clipping");
1069 if (devinfo->is_haswell || devinfo->gen == 8) {
1070 add_stat_reg(query, PS_INVOCATION_COUNT, 1, 4,
1071 "N fragment shader invocations",
1072 "N fragment shader invocations");
1073 } else {
1074 add_basic_stat_reg(query, PS_INVOCATION_COUNT,
1075 "N fragment shader invocations");
1076 }
1077 add_basic_stat_reg(query, HS_INVOCATION_COUNT,
1078 "N TCS shader invocations");
1079 add_basic_stat_reg(query, DS_INVOCATION_COUNT,
1080 "N TES shader invocations");
1081 if (devinfo->gen >= 7) {
1082 add_basic_stat_reg(query, CS_INVOCATION_COUNT,
1083 "N compute shader invocations");
1084 }
1085
1086 if (devinfo->gen >= 10) {
1087 /* Reuse existing CS invocation register until we can expose this new
1088 * one.
1089 */
1090 add_basic_stat_reg(query, CS_INVOCATION_COUNT,
1091 "Reserved1");
1092 }
1093
1094 query->data_size = sizeof(uint64_t) * query->n_counters;
1095 }
1096
1097 static void
1098 fill_mdapi_perf_query_counter(struct gen_perf_query_info *query,
1099 const char *name,
1100 uint32_t data_offset,
1101 uint32_t data_size,
1102 enum gen_perf_counter_data_type data_type)
1103 {
1104 struct gen_perf_query_counter *counter = &query->counters[query->n_counters];
1105
1106 assert(query->n_counters <= query->max_counters);
1107
1108 counter->name = name;
1109 counter->desc = "Raw counter value";
1110 counter->type = GEN_PERF_COUNTER_TYPE_RAW;
1111 counter->data_type = data_type;
1112 counter->offset = data_offset;
1113
1114 query->n_counters++;
1115
1116 assert(counter->offset + gen_perf_query_counter_get_size(counter) <= query->data_size);
1117 }
1118
1119 #define MDAPI_QUERY_ADD_COUNTER(query, struct_name, field_name, type_name) \
1120 fill_mdapi_perf_query_counter(query, #field_name, \
1121 (uint8_t *) &struct_name.field_name - \
1122 (uint8_t *) &struct_name, \
1123 sizeof(struct_name.field_name), \
1124 GEN_PERF_COUNTER_DATA_TYPE_##type_name)
1125 #define MDAPI_QUERY_ADD_ARRAY_COUNTER(ctx, query, struct_name, field_name, idx, type_name) \
1126 fill_mdapi_perf_query_counter(query, \
1127 ralloc_asprintf(ctx, "%s%i", #field_name, idx), \
1128 (uint8_t *) &struct_name.field_name[idx] - \
1129 (uint8_t *) &struct_name, \
1130 sizeof(struct_name.field_name[0]), \
1131 GEN_PERF_COUNTER_DATA_TYPE_##type_name)
1132
1133 static void
1134 register_mdapi_oa_query(const struct gen_device_info *devinfo,
1135 struct gen_perf_config *perf)
1136 {
1137 struct gen_perf_query_info *query = NULL;
1138
1139 /* MDAPI requires different structures for pretty much every generation
1140 * (right now we have definitions for gen 7 to 11).
1141 */
1142 if (!(devinfo->gen >= 7 && devinfo->gen <= 11))
1143 return;
1144
1145 switch (devinfo->gen) {
1146 case 7: {
1147 query = append_query_info(perf, 1 + 45 + 16 + 7);
1148 query->oa_format = I915_OA_FORMAT_A45_B8_C8;
1149
1150 struct gen7_mdapi_metrics metric_data;
1151 query->data_size = sizeof(metric_data);
1152
1153 MDAPI_QUERY_ADD_COUNTER(query, metric_data, TotalTime, UINT64);
1154 for (int i = 0; i < ARRAY_SIZE(metric_data.ACounters); i++) {
1155 MDAPI_QUERY_ADD_ARRAY_COUNTER(perf->queries, query,
1156 metric_data, ACounters, i, UINT64);
1157 }
1158 for (int i = 0; i < ARRAY_SIZE(metric_data.NOACounters); i++) {
1159 MDAPI_QUERY_ADD_ARRAY_COUNTER(perf->queries, query,
1160 metric_data, NOACounters, i, UINT64);
1161 }
1162 MDAPI_QUERY_ADD_COUNTER(query, metric_data, PerfCounter1, UINT64);
1163 MDAPI_QUERY_ADD_COUNTER(query, metric_data, PerfCounter2, UINT64);
1164 MDAPI_QUERY_ADD_COUNTER(query, metric_data, SplitOccured, BOOL32);
1165 MDAPI_QUERY_ADD_COUNTER(query, metric_data, CoreFrequencyChanged, BOOL32);
1166 MDAPI_QUERY_ADD_COUNTER(query, metric_data, CoreFrequency, UINT64);
1167 MDAPI_QUERY_ADD_COUNTER(query, metric_data, ReportId, UINT32);
1168 MDAPI_QUERY_ADD_COUNTER(query, metric_data, ReportsCount, UINT32);
1169 break;
1170 }
1171 case 8: {
1172 query = append_query_info(perf, 2 + 36 + 16 + 16);
1173 query->oa_format = I915_OA_FORMAT_A32u40_A4u32_B8_C8;
1174
1175 struct gen8_mdapi_metrics metric_data;
1176 query->data_size = sizeof(metric_data);
1177
1178 MDAPI_QUERY_ADD_COUNTER(query, metric_data, TotalTime, UINT64);
1179 MDAPI_QUERY_ADD_COUNTER(query, metric_data, GPUTicks, UINT64);
1180 for (int i = 0; i < ARRAY_SIZE(metric_data.OaCntr); i++) {
1181 MDAPI_QUERY_ADD_ARRAY_COUNTER(perf->queries, query,
1182 metric_data, OaCntr, i, UINT64);
1183 }
1184 for (int i = 0; i < ARRAY_SIZE(metric_data.NoaCntr); i++) {
1185 MDAPI_QUERY_ADD_ARRAY_COUNTER(perf->queries, query,
1186 metric_data, NoaCntr, i, UINT64);
1187 }
1188 MDAPI_QUERY_ADD_COUNTER(query, metric_data, BeginTimestamp, UINT64);
1189 MDAPI_QUERY_ADD_COUNTER(query, metric_data, Reserved1, UINT64);
1190 MDAPI_QUERY_ADD_COUNTER(query, metric_data, Reserved2, UINT64);
1191 MDAPI_QUERY_ADD_COUNTER(query, metric_data, Reserved3, UINT32);
1192 MDAPI_QUERY_ADD_COUNTER(query, metric_data, OverrunOccured, BOOL32);
1193 MDAPI_QUERY_ADD_COUNTER(query, metric_data, MarkerUser, UINT64);
1194 MDAPI_QUERY_ADD_COUNTER(query, metric_data, MarkerDriver, UINT64);
1195 MDAPI_QUERY_ADD_COUNTER(query, metric_data, SliceFrequency, UINT64);
1196 MDAPI_QUERY_ADD_COUNTER(query, metric_data, UnsliceFrequency, UINT64);
1197 MDAPI_QUERY_ADD_COUNTER(query, metric_data, PerfCounter1, UINT64);
1198 MDAPI_QUERY_ADD_COUNTER(query, metric_data, PerfCounter2, UINT64);
1199 MDAPI_QUERY_ADD_COUNTER(query, metric_data, SplitOccured, BOOL32);
1200 MDAPI_QUERY_ADD_COUNTER(query, metric_data, CoreFrequencyChanged, BOOL32);
1201 MDAPI_QUERY_ADD_COUNTER(query, metric_data, CoreFrequency, UINT64);
1202 MDAPI_QUERY_ADD_COUNTER(query, metric_data, ReportId, UINT32);
1203 MDAPI_QUERY_ADD_COUNTER(query, metric_data, ReportsCount, UINT32);
1204 break;
1205 }
1206 case 9:
1207 case 10:
1208 case 11: {
1209 query = append_query_info(perf, 2 + 36 + 16 + 16 + 16 + 2);
1210 query->oa_format = I915_OA_FORMAT_A32u40_A4u32_B8_C8;
1211
1212 struct gen9_mdapi_metrics metric_data;
1213 query->data_size = sizeof(metric_data);
1214
1215 MDAPI_QUERY_ADD_COUNTER(query, metric_data, TotalTime, UINT64);
1216 MDAPI_QUERY_ADD_COUNTER(query, metric_data, GPUTicks, UINT64);
1217 for (int i = 0; i < ARRAY_SIZE(metric_data.OaCntr); i++) {
1218 MDAPI_QUERY_ADD_ARRAY_COUNTER(perf->queries, query,
1219 metric_data, OaCntr, i, UINT64);
1220 }
1221 for (int i = 0; i < ARRAY_SIZE(metric_data.NoaCntr); i++) {
1222 MDAPI_QUERY_ADD_ARRAY_COUNTER(perf->queries, query,
1223 metric_data, NoaCntr, i, UINT64);
1224 }
1225 MDAPI_QUERY_ADD_COUNTER(query, metric_data, BeginTimestamp, UINT64);
1226 MDAPI_QUERY_ADD_COUNTER(query, metric_data, Reserved1, UINT64);
1227 MDAPI_QUERY_ADD_COUNTER(query, metric_data, Reserved2, UINT64);
1228 MDAPI_QUERY_ADD_COUNTER(query, metric_data, Reserved3, UINT32);
1229 MDAPI_QUERY_ADD_COUNTER(query, metric_data, OverrunOccured, BOOL32);
1230 MDAPI_QUERY_ADD_COUNTER(query, metric_data, MarkerUser, UINT64);
1231 MDAPI_QUERY_ADD_COUNTER(query, metric_data, MarkerDriver, UINT64);
1232 MDAPI_QUERY_ADD_COUNTER(query, metric_data, SliceFrequency, UINT64);
1233 MDAPI_QUERY_ADD_COUNTER(query, metric_data, UnsliceFrequency, UINT64);
1234 MDAPI_QUERY_ADD_COUNTER(query, metric_data, PerfCounter1, UINT64);
1235 MDAPI_QUERY_ADD_COUNTER(query, metric_data, PerfCounter2, UINT64);
1236 MDAPI_QUERY_ADD_COUNTER(query, metric_data, SplitOccured, BOOL32);
1237 MDAPI_QUERY_ADD_COUNTER(query, metric_data, CoreFrequencyChanged, BOOL32);
1238 MDAPI_QUERY_ADD_COUNTER(query, metric_data, CoreFrequency, UINT64);
1239 MDAPI_QUERY_ADD_COUNTER(query, metric_data, ReportId, UINT32);
1240 MDAPI_QUERY_ADD_COUNTER(query, metric_data, ReportsCount, UINT32);
1241 for (int i = 0; i < ARRAY_SIZE(metric_data.UserCntr); i++) {
1242 MDAPI_QUERY_ADD_ARRAY_COUNTER(perf->queries, query,
1243 metric_data, UserCntr, i, UINT64);
1244 }
1245 MDAPI_QUERY_ADD_COUNTER(query, metric_data, UserCntrCfgId, UINT32);
1246 MDAPI_QUERY_ADD_COUNTER(query, metric_data, Reserved4, UINT32);
1247 break;
1248 }
1249 default:
1250 unreachable("Unsupported gen");
1251 break;
1252 }
1253
1254 query->kind = GEN_PERF_QUERY_TYPE_RAW;
1255 query->name = "Intel_Raw_Hardware_Counters_Set_0_Query";
1256 query->guid = GEN_PERF_QUERY_GUID_MDAPI;
1257
1258 {
1259 /* Accumulation buffer offsets copied from an actual query... */
1260 const struct gen_perf_query_info *copy_query =
1261 &perf->queries[0];
1262
1263 query->gpu_time_offset = copy_query->gpu_time_offset;
1264 query->gpu_clock_offset = copy_query->gpu_clock_offset;
1265 query->a_offset = copy_query->a_offset;
1266 query->b_offset = copy_query->b_offset;
1267 query->c_offset = copy_query->c_offset;
1268 }
1269 }
1270
1271 static uint64_t
1272 get_metric_id(struct gen_perf_config *perf,
1273 const struct gen_perf_query_info *query)
1274 {
1275 /* These queries are know not to ever change, their config ID has been
1276 * loaded upon the first query creation. No need to look them up again.
1277 */
1278 if (query->kind == GEN_PERF_QUERY_TYPE_OA)
1279 return query->oa_metrics_set_id;
1280
1281 assert(query->kind == GEN_PERF_QUERY_TYPE_RAW);
1282
1283 /* Raw queries can be reprogrammed up by an external application/library.
1284 * When a raw query is used for the first time it's id is set to a value !=
1285 * 0. When it stops being used the id returns to 0. No need to reload the
1286 * ID when it's already loaded.
1287 */
1288 if (query->oa_metrics_set_id != 0) {
1289 DBG("Raw query '%s' guid=%s using cached ID: %"PRIu64"\n",
1290 query->name, query->guid, query->oa_metrics_set_id);
1291 return query->oa_metrics_set_id;
1292 }
1293
1294 struct gen_perf_query_info *raw_query = (struct gen_perf_query_info *)query;
1295 if (!load_metric_id(perf, query->guid,
1296 &raw_query->oa_metrics_set_id)) {
1297 DBG("Unable to read query guid=%s ID, falling back to test config\n", query->guid);
1298 raw_query->oa_metrics_set_id = 1ULL;
1299 } else {
1300 DBG("Raw query '%s'guid=%s loaded ID: %"PRIu64"\n",
1301 query->name, query->guid, query->oa_metrics_set_id);
1302 }
1303 return query->oa_metrics_set_id;
1304 }
1305
1306 static struct oa_sample_buf *
1307 get_free_sample_buf(struct gen_perf_context *perf_ctx)
1308 {
1309 struct exec_node *node = exec_list_pop_head(&perf_ctx->free_sample_buffers);
1310 struct oa_sample_buf *buf;
1311
1312 if (node)
1313 buf = exec_node_data(struct oa_sample_buf, node, link);
1314 else {
1315 buf = ralloc_size(perf_ctx->perf, sizeof(*buf));
1316
1317 exec_node_init(&buf->link);
1318 buf->refcount = 0;
1319 buf->len = 0;
1320 }
1321
1322 return buf;
1323 }
1324
1325 static void
1326 reap_old_sample_buffers(struct gen_perf_context *perf_ctx)
1327 {
1328 struct exec_node *tail_node =
1329 exec_list_get_tail(&perf_ctx->sample_buffers);
1330 struct oa_sample_buf *tail_buf =
1331 exec_node_data(struct oa_sample_buf, tail_node, link);
1332
1333 /* Remove all old, unreferenced sample buffers walking forward from
1334 * the head of the list, except always leave at least one node in
1335 * the list so we always have a node to reference when we Begin
1336 * a new query.
1337 */
1338 foreach_list_typed_safe(struct oa_sample_buf, buf, link,
1339 &perf_ctx->sample_buffers)
1340 {
1341 if (buf->refcount == 0 && buf != tail_buf) {
1342 exec_node_remove(&buf->link);
1343 exec_list_push_head(&perf_ctx->free_sample_buffers, &buf->link);
1344 } else
1345 return;
1346 }
1347 }
1348
1349 static void
1350 free_sample_bufs(struct gen_perf_context *perf_ctx)
1351 {
1352 foreach_list_typed_safe(struct oa_sample_buf, buf, link,
1353 &perf_ctx->free_sample_buffers)
1354 ralloc_free(buf);
1355
1356 exec_list_make_empty(&perf_ctx->free_sample_buffers);
1357 }
1358
1359 /******************************************************************************/
1360
1361 /**
1362 * Emit MI_STORE_REGISTER_MEM commands to capture all of the
1363 * pipeline statistics for the performance query object.
1364 */
1365 static void
1366 snapshot_statistics_registers(void *context,
1367 struct gen_perf_config *perf,
1368 struct gen_perf_query_object *obj,
1369 uint32_t offset_in_bytes)
1370 {
1371 const struct gen_perf_query_info *query = obj->queryinfo;
1372 const int n_counters = query->n_counters;
1373
1374 for (int i = 0; i < n_counters; i++) {
1375 const struct gen_perf_query_counter *counter = &query->counters[i];
1376
1377 assert(counter->data_type == GEN_PERF_COUNTER_DATA_TYPE_UINT64);
1378
1379 perf->vtbl.store_register_mem64(context, obj->pipeline_stats.bo,
1380 counter->pipeline_stat.reg,
1381 offset_in_bytes + i * sizeof(uint64_t));
1382 }
1383 }
1384
1385 static void
1386 gen_perf_close(struct gen_perf_context *perfquery,
1387 const struct gen_perf_query_info *query)
1388 {
1389 if (perfquery->oa_stream_fd != -1) {
1390 close(perfquery->oa_stream_fd);
1391 perfquery->oa_stream_fd = -1;
1392 }
1393 if (query->kind == GEN_PERF_QUERY_TYPE_RAW) {
1394 struct gen_perf_query_info *raw_query =
1395 (struct gen_perf_query_info *) query;
1396 raw_query->oa_metrics_set_id = 0;
1397 }
1398 }
1399
1400 static bool
1401 gen_perf_open(struct gen_perf_context *perf_ctx,
1402 int metrics_set_id,
1403 int report_format,
1404 int period_exponent,
1405 int drm_fd,
1406 uint32_t ctx_id)
1407 {
1408 uint64_t properties[] = {
1409 /* Single context sampling */
1410 DRM_I915_PERF_PROP_CTX_HANDLE, ctx_id,
1411
1412 /* Include OA reports in samples */
1413 DRM_I915_PERF_PROP_SAMPLE_OA, true,
1414
1415 /* OA unit configuration */
1416 DRM_I915_PERF_PROP_OA_METRICS_SET, metrics_set_id,
1417 DRM_I915_PERF_PROP_OA_FORMAT, report_format,
1418 DRM_I915_PERF_PROP_OA_EXPONENT, period_exponent,
1419 };
1420 struct drm_i915_perf_open_param param = {
1421 .flags = I915_PERF_FLAG_FD_CLOEXEC |
1422 I915_PERF_FLAG_FD_NONBLOCK |
1423 I915_PERF_FLAG_DISABLED,
1424 .num_properties = ARRAY_SIZE(properties) / 2,
1425 .properties_ptr = (uintptr_t) properties,
1426 };
1427 int fd = gen_ioctl(drm_fd, DRM_IOCTL_I915_PERF_OPEN, &param);
1428 if (fd == -1) {
1429 DBG("Error opening gen perf OA stream: %m\n");
1430 return false;
1431 }
1432
1433 perf_ctx->oa_stream_fd = fd;
1434
1435 perf_ctx->current_oa_metrics_set_id = metrics_set_id;
1436 perf_ctx->current_oa_format = report_format;
1437
1438 return true;
1439 }
1440
1441 static bool
1442 inc_n_users(struct gen_perf_context *perf_ctx)
1443 {
1444 if (perf_ctx->n_oa_users == 0 &&
1445 gen_ioctl(perf_ctx->oa_stream_fd, I915_PERF_IOCTL_ENABLE, 0) < 0)
1446 {
1447 return false;
1448 }
1449 ++perf_ctx->n_oa_users;
1450
1451 return true;
1452 }
1453
1454 static void
1455 dec_n_users(struct gen_perf_context *perf_ctx)
1456 {
1457 /* Disabling the i915 perf stream will effectively disable the OA
1458 * counters. Note it's important to be sure there are no outstanding
1459 * MI_RPC commands at this point since they could stall the CS
1460 * indefinitely once OACONTROL is disabled.
1461 */
1462 --perf_ctx->n_oa_users;
1463 if (perf_ctx->n_oa_users == 0 &&
1464 gen_ioctl(perf_ctx->oa_stream_fd, I915_PERF_IOCTL_DISABLE, 0) < 0)
1465 {
1466 DBG("WARNING: Error disabling gen perf stream: %m\n");
1467 }
1468 }
1469
1470 void
1471 gen_perf_init_metrics(struct gen_perf_config *perf_cfg,
1472 const struct gen_device_info *devinfo,
1473 int drm_fd)
1474 {
1475 load_pipeline_statistic_metrics(perf_cfg, devinfo);
1476 register_mdapi_statistic_query(perf_cfg, devinfo);
1477 if (load_oa_metrics(perf_cfg, drm_fd, devinfo))
1478 register_mdapi_oa_query(devinfo, perf_cfg);
1479 }
1480
1481 void
1482 gen_perf_init_context(struct gen_perf_context *perf_ctx,
1483 struct gen_perf_config *perf_cfg,
1484 void * ctx, /* driver context (eg, brw_context) */
1485 void * bufmgr, /* eg brw_bufmgr */
1486 const struct gen_device_info *devinfo,
1487 uint32_t hw_ctx,
1488 int drm_fd)
1489 {
1490 perf_ctx->perf = perf_cfg;
1491 perf_ctx->ctx = ctx;
1492 perf_ctx->bufmgr = bufmgr;
1493 perf_ctx->drm_fd = drm_fd;
1494 perf_ctx->hw_ctx = hw_ctx;
1495 perf_ctx->devinfo = devinfo;
1496
1497 perf_ctx->unaccumulated =
1498 ralloc_array(ctx, struct gen_perf_query_object *, 2);
1499 perf_ctx->unaccumulated_elements = 0;
1500 perf_ctx->unaccumulated_array_size = 2;
1501
1502 exec_list_make_empty(&perf_ctx->sample_buffers);
1503 exec_list_make_empty(&perf_ctx->free_sample_buffers);
1504
1505 /* It's convenient to guarantee that this linked list of sample
1506 * buffers is never empty so we add an empty head so when we
1507 * Begin an OA query we can always take a reference on a buffer
1508 * in this list.
1509 */
1510 struct oa_sample_buf *buf = get_free_sample_buf(perf_ctx);
1511 exec_list_push_head(&perf_ctx->sample_buffers, &buf->link);
1512
1513 perf_ctx->oa_stream_fd = -1;
1514 perf_ctx->next_query_start_report_id = 1000;
1515 }
1516
1517 /**
1518 * Add a query to the global list of "unaccumulated queries."
1519 *
1520 * Queries are tracked here until all the associated OA reports have
1521 * been accumulated via accumulate_oa_reports() after the end
1522 * MI_REPORT_PERF_COUNT has landed in query->oa.bo.
1523 */
1524 static void
1525 add_to_unaccumulated_query_list(struct gen_perf_context *perf_ctx,
1526 struct gen_perf_query_object *obj)
1527 {
1528 if (perf_ctx->unaccumulated_elements >=
1529 perf_ctx->unaccumulated_array_size)
1530 {
1531 perf_ctx->unaccumulated_array_size *= 1.5;
1532 perf_ctx->unaccumulated =
1533 reralloc(perf_ctx->ctx, perf_ctx->unaccumulated,
1534 struct gen_perf_query_object *,
1535 perf_ctx->unaccumulated_array_size);
1536 }
1537
1538 perf_ctx->unaccumulated[perf_ctx->unaccumulated_elements++] = obj;
1539 }
1540
1541 bool
1542 gen_perf_begin_query(struct gen_perf_context *perf_ctx,
1543 struct gen_perf_query_object *query)
1544 {
1545 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1546 const struct gen_perf_query_info *queryinfo = query->queryinfo;
1547
1548 /* XXX: We have to consider that the command parser unit that parses batch
1549 * buffer commands and is used to capture begin/end counter snapshots isn't
1550 * implicitly synchronized with what's currently running across other GPU
1551 * units (such as the EUs running shaders) that the performance counters are
1552 * associated with.
1553 *
1554 * The intention of performance queries is to measure the work associated
1555 * with commands between the begin/end delimiters and so for that to be the
1556 * case we need to explicitly synchronize the parsing of commands to capture
1557 * Begin/End counter snapshots with what's running across other parts of the
1558 * GPU.
1559 *
1560 * When the command parser reaches a Begin marker it effectively needs to
1561 * drain everything currently running on the GPU until the hardware is idle
1562 * before capturing the first snapshot of counters - otherwise the results
1563 * would also be measuring the effects of earlier commands.
1564 *
1565 * When the command parser reaches an End marker it needs to stall until
1566 * everything currently running on the GPU has finished before capturing the
1567 * end snapshot - otherwise the results won't be a complete representation
1568 * of the work.
1569 *
1570 * Theoretically there could be opportunities to minimize how much of the
1571 * GPU pipeline is drained, or that we stall for, when we know what specific
1572 * units the performance counters being queried relate to but we don't
1573 * currently attempt to be clever here.
1574 *
1575 * Note: with our current simple approach here then for back-to-back queries
1576 * we will redundantly emit duplicate commands to synchronize the command
1577 * streamer with the rest of the GPU pipeline, but we assume that in HW the
1578 * second synchronization is effectively a NOOP.
1579 *
1580 * N.B. The final results are based on deltas of counters between (inside)
1581 * Begin/End markers so even though the total wall clock time of the
1582 * workload is stretched by larger pipeline bubbles the bubbles themselves
1583 * are generally invisible to the query results. Whether that's a good or a
1584 * bad thing depends on the use case. For a lower real-time impact while
1585 * capturing metrics then periodic sampling may be a better choice than
1586 * INTEL_performance_query.
1587 *
1588 *
1589 * This is our Begin synchronization point to drain current work on the
1590 * GPU before we capture our first counter snapshot...
1591 */
1592 perf_cfg->vtbl.emit_mi_flush(perf_ctx->ctx);
1593
1594 switch (queryinfo->kind) {
1595 case GEN_PERF_QUERY_TYPE_OA:
1596 case GEN_PERF_QUERY_TYPE_RAW: {
1597
1598 /* Opening an i915 perf stream implies exclusive access to the OA unit
1599 * which will generate counter reports for a specific counter set with a
1600 * specific layout/format so we can't begin any OA based queries that
1601 * require a different counter set or format unless we get an opportunity
1602 * to close the stream and open a new one...
1603 */
1604 uint64_t metric_id = get_metric_id(perf_ctx->perf, queryinfo);
1605
1606 if (perf_ctx->oa_stream_fd != -1 &&
1607 perf_ctx->current_oa_metrics_set_id != metric_id) {
1608
1609 if (perf_ctx->n_oa_users != 0) {
1610 DBG("WARNING: Begin failed already using perf config=%i/%"PRIu64"\n",
1611 perf_ctx->current_oa_metrics_set_id, metric_id);
1612 return false;
1613 } else
1614 gen_perf_close(perf_ctx, queryinfo);
1615 }
1616
1617 /* If the OA counters aren't already on, enable them. */
1618 if (perf_ctx->oa_stream_fd == -1) {
1619 const struct gen_device_info *devinfo = perf_ctx->devinfo;
1620
1621 /* The period_exponent gives a sampling period as follows:
1622 * sample_period = timestamp_period * 2^(period_exponent + 1)
1623 *
1624 * The timestamps increments every 80ns (HSW), ~52ns (GEN9LP) or
1625 * ~83ns (GEN8/9).
1626 *
1627 * The counter overflow period is derived from the EuActive counter
1628 * which reads a counter that increments by the number of clock
1629 * cycles multiplied by the number of EUs. It can be calculated as:
1630 *
1631 * 2^(number of bits in A counter) / (n_eus * max_gen_freq * 2)
1632 *
1633 * (E.g. 40 EUs @ 1GHz = ~53ms)
1634 *
1635 * We select a sampling period inferior to that overflow period to
1636 * ensure we cannot see more than 1 counter overflow, otherwise we
1637 * could loose information.
1638 */
1639
1640 int a_counter_in_bits = 32;
1641 if (devinfo->gen >= 8)
1642 a_counter_in_bits = 40;
1643
1644 uint64_t overflow_period = pow(2, a_counter_in_bits) / (perf_cfg->sys_vars.n_eus *
1645 /* drop 1GHz freq to have units in nanoseconds */
1646 2);
1647
1648 DBG("A counter overflow period: %"PRIu64"ns, %"PRIu64"ms (n_eus=%"PRIu64")\n",
1649 overflow_period, overflow_period / 1000000ul, perf_cfg->sys_vars.n_eus);
1650
1651 int period_exponent = 0;
1652 uint64_t prev_sample_period, next_sample_period;
1653 for (int e = 0; e < 30; e++) {
1654 prev_sample_period = 1000000000ull * pow(2, e + 1) / devinfo->timestamp_frequency;
1655 next_sample_period = 1000000000ull * pow(2, e + 2) / devinfo->timestamp_frequency;
1656
1657 /* Take the previous sampling period, lower than the overflow
1658 * period.
1659 */
1660 if (prev_sample_period < overflow_period &&
1661 next_sample_period > overflow_period)
1662 period_exponent = e + 1;
1663 }
1664
1665 if (period_exponent == 0) {
1666 DBG("WARNING: enable to find a sampling exponent\n");
1667 return false;
1668 }
1669
1670 DBG("OA sampling exponent: %i ~= %"PRIu64"ms\n", period_exponent,
1671 prev_sample_period / 1000000ul);
1672
1673 if (!gen_perf_open(perf_ctx, metric_id, queryinfo->oa_format,
1674 period_exponent, perf_ctx->drm_fd,
1675 perf_ctx->hw_ctx))
1676 return false;
1677 } else {
1678 assert(perf_ctx->current_oa_metrics_set_id == metric_id &&
1679 perf_ctx->current_oa_format == queryinfo->oa_format);
1680 }
1681
1682 if (!inc_n_users(perf_ctx)) {
1683 DBG("WARNING: Error enabling i915 perf stream: %m\n");
1684 return false;
1685 }
1686
1687 if (query->oa.bo) {
1688 perf_cfg->vtbl.bo_unreference(query->oa.bo);
1689 query->oa.bo = NULL;
1690 }
1691
1692 query->oa.bo = perf_cfg->vtbl.bo_alloc(perf_ctx->bufmgr,
1693 "perf. query OA MI_RPC bo",
1694 MI_RPC_BO_SIZE);
1695 #ifdef DEBUG
1696 /* Pre-filling the BO helps debug whether writes landed. */
1697 void *map = perf_cfg->vtbl.bo_map(perf_ctx->ctx, query->oa.bo, MAP_WRITE);
1698 memset(map, 0x80, MI_RPC_BO_SIZE);
1699 perf_cfg->vtbl.bo_unmap(query->oa.bo);
1700 #endif
1701
1702 query->oa.begin_report_id = perf_ctx->next_query_start_report_id;
1703 perf_ctx->next_query_start_report_id += 2;
1704
1705 /* We flush the batchbuffer here to minimize the chances that MI_RPC
1706 * delimiting commands end up in different batchbuffers. If that's the
1707 * case, the measurement will include the time it takes for the kernel
1708 * scheduler to load a new request into the hardware. This is manifested in
1709 * tools like frameretrace by spikes in the "GPU Core Clocks" counter.
1710 */
1711 perf_cfg->vtbl.batchbuffer_flush(perf_ctx->ctx, __FILE__, __LINE__);
1712
1713 /* Take a starting OA counter snapshot. */
1714 perf_cfg->vtbl.emit_mi_report_perf_count(perf_ctx->ctx, query->oa.bo, 0,
1715 query->oa.begin_report_id);
1716 perf_cfg->vtbl.capture_frequency_stat_register(perf_ctx->ctx, query->oa.bo,
1717 MI_FREQ_START_OFFSET_BYTES);
1718
1719 ++perf_ctx->n_active_oa_queries;
1720
1721 /* No already-buffered samples can possibly be associated with this query
1722 * so create a marker within the list of sample buffers enabling us to
1723 * easily ignore earlier samples when processing this query after
1724 * completion.
1725 */
1726 assert(!exec_list_is_empty(&perf_ctx->sample_buffers));
1727 query->oa.samples_head = exec_list_get_tail(&perf_ctx->sample_buffers);
1728
1729 struct oa_sample_buf *buf =
1730 exec_node_data(struct oa_sample_buf, query->oa.samples_head, link);
1731
1732 /* This reference will ensure that future/following sample
1733 * buffers (that may relate to this query) can't be freed until
1734 * this drops to zero.
1735 */
1736 buf->refcount++;
1737
1738 query_result_clear(&query->oa.result);
1739 query->oa.results_accumulated = false;
1740
1741 add_to_unaccumulated_query_list(perf_ctx, query);
1742 break;
1743 }
1744
1745 case GEN_PERF_QUERY_TYPE_PIPELINE:
1746 if (query->pipeline_stats.bo) {
1747 perf_cfg->vtbl.bo_unreference(query->pipeline_stats.bo);
1748 query->pipeline_stats.bo = NULL;
1749 }
1750
1751 query->pipeline_stats.bo =
1752 perf_cfg->vtbl.bo_alloc(perf_ctx->bufmgr,
1753 "perf. query pipeline stats bo",
1754 STATS_BO_SIZE);
1755
1756 /* Take starting snapshots. */
1757 snapshot_statistics_registers(perf_ctx->ctx , perf_cfg, query, 0);
1758
1759 ++perf_ctx->n_active_pipeline_stats_queries;
1760 break;
1761
1762 default:
1763 unreachable("Unknown query type");
1764 break;
1765 }
1766
1767 return true;
1768 }
1769
1770 void
1771 gen_perf_end_query(struct gen_perf_context *perf_ctx,
1772 struct gen_perf_query_object *query)
1773 {
1774 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1775
1776 /* Ensure that the work associated with the queried commands will have
1777 * finished before taking our query end counter readings.
1778 *
1779 * For more details see comment in brw_begin_perf_query for
1780 * corresponding flush.
1781 */
1782 perf_cfg->vtbl.emit_mi_flush(perf_ctx->ctx);
1783
1784 switch (query->queryinfo->kind) {
1785 case GEN_PERF_QUERY_TYPE_OA:
1786 case GEN_PERF_QUERY_TYPE_RAW:
1787
1788 /* NB: It's possible that the query will have already been marked
1789 * as 'accumulated' if an error was seen while reading samples
1790 * from perf. In this case we mustn't try and emit a closing
1791 * MI_RPC command in case the OA unit has already been disabled
1792 */
1793 if (!query->oa.results_accumulated) {
1794 /* Take an ending OA counter snapshot. */
1795 perf_cfg->vtbl.capture_frequency_stat_register(perf_ctx->ctx, query->oa.bo,
1796 MI_FREQ_END_OFFSET_BYTES);
1797 perf_cfg->vtbl.emit_mi_report_perf_count(perf_ctx->ctx, query->oa.bo,
1798 MI_RPC_BO_END_OFFSET_BYTES,
1799 query->oa.begin_report_id + 1);
1800 }
1801
1802 --perf_ctx->n_active_oa_queries;
1803
1804 /* NB: even though the query has now ended, it can't be accumulated
1805 * until the end MI_REPORT_PERF_COUNT snapshot has been written
1806 * to query->oa.bo
1807 */
1808 break;
1809
1810 case GEN_PERF_QUERY_TYPE_PIPELINE:
1811 snapshot_statistics_registers(perf_ctx->ctx, perf_cfg, query,
1812 STATS_BO_END_OFFSET_BYTES);
1813 --perf_ctx->n_active_pipeline_stats_queries;
1814 break;
1815
1816 default:
1817 unreachable("Unknown query type");
1818 break;
1819 }
1820 }
1821
1822 enum OaReadStatus {
1823 OA_READ_STATUS_ERROR,
1824 OA_READ_STATUS_UNFINISHED,
1825 OA_READ_STATUS_FINISHED,
1826 };
1827
1828 static enum OaReadStatus
1829 read_oa_samples_until(struct gen_perf_context *perf_ctx,
1830 uint32_t start_timestamp,
1831 uint32_t end_timestamp)
1832 {
1833 struct exec_node *tail_node =
1834 exec_list_get_tail(&perf_ctx->sample_buffers);
1835 struct oa_sample_buf *tail_buf =
1836 exec_node_data(struct oa_sample_buf, tail_node, link);
1837 uint32_t last_timestamp = tail_buf->last_timestamp;
1838
1839 while (1) {
1840 struct oa_sample_buf *buf = get_free_sample_buf(perf_ctx);
1841 uint32_t offset;
1842 int len;
1843
1844 while ((len = read(perf_ctx->oa_stream_fd, buf->buf,
1845 sizeof(buf->buf))) < 0 && errno == EINTR)
1846 ;
1847
1848 if (len <= 0) {
1849 exec_list_push_tail(&perf_ctx->free_sample_buffers, &buf->link);
1850
1851 if (len < 0) {
1852 if (errno == EAGAIN)
1853 return ((last_timestamp - start_timestamp) >=
1854 (end_timestamp - start_timestamp)) ?
1855 OA_READ_STATUS_FINISHED :
1856 OA_READ_STATUS_UNFINISHED;
1857 else {
1858 DBG("Error reading i915 perf samples: %m\n");
1859 }
1860 } else
1861 DBG("Spurious EOF reading i915 perf samples\n");
1862
1863 return OA_READ_STATUS_ERROR;
1864 }
1865
1866 buf->len = len;
1867 exec_list_push_tail(&perf_ctx->sample_buffers, &buf->link);
1868
1869 /* Go through the reports and update the last timestamp. */
1870 offset = 0;
1871 while (offset < buf->len) {
1872 const struct drm_i915_perf_record_header *header =
1873 (const struct drm_i915_perf_record_header *) &buf->buf[offset];
1874 uint32_t *report = (uint32_t *) (header + 1);
1875
1876 if (header->type == DRM_I915_PERF_RECORD_SAMPLE)
1877 last_timestamp = report[1];
1878
1879 offset += header->size;
1880 }
1881
1882 buf->last_timestamp = last_timestamp;
1883 }
1884
1885 unreachable("not reached");
1886 return OA_READ_STATUS_ERROR;
1887 }
1888
1889 /**
1890 * Try to read all the reports until either the delimiting timestamp
1891 * or an error arises.
1892 */
1893 static bool
1894 read_oa_samples_for_query(struct gen_perf_context *perf_ctx,
1895 struct gen_perf_query_object *query,
1896 void *current_batch)
1897 {
1898 uint32_t *start;
1899 uint32_t *last;
1900 uint32_t *end;
1901 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1902
1903 /* We need the MI_REPORT_PERF_COUNT to land before we can start
1904 * accumulate. */
1905 assert(!perf_cfg->vtbl.batch_references(current_batch, query->oa.bo) &&
1906 !perf_cfg->vtbl.bo_busy(query->oa.bo));
1907
1908 /* Map the BO once here and let accumulate_oa_reports() unmap
1909 * it. */
1910 if (query->oa.map == NULL)
1911 query->oa.map = perf_cfg->vtbl.bo_map(perf_ctx->ctx, query->oa.bo, MAP_READ);
1912
1913 start = last = query->oa.map;
1914 end = query->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
1915
1916 if (start[0] != query->oa.begin_report_id) {
1917 DBG("Spurious start report id=%"PRIu32"\n", start[0]);
1918 return true;
1919 }
1920 if (end[0] != (query->oa.begin_report_id + 1)) {
1921 DBG("Spurious end report id=%"PRIu32"\n", end[0]);
1922 return true;
1923 }
1924
1925 /* Read the reports until the end timestamp. */
1926 switch (read_oa_samples_until(perf_ctx, start[1], end[1])) {
1927 case OA_READ_STATUS_ERROR:
1928 /* Fallthrough and let accumulate_oa_reports() deal with the
1929 * error. */
1930 case OA_READ_STATUS_FINISHED:
1931 return true;
1932 case OA_READ_STATUS_UNFINISHED:
1933 return false;
1934 }
1935
1936 unreachable("invalid read status");
1937 return false;
1938 }
1939
1940 void
1941 gen_perf_wait_query(struct gen_perf_context *perf_ctx,
1942 struct gen_perf_query_object *query,
1943 void *current_batch)
1944 {
1945 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1946 struct brw_bo *bo = NULL;
1947
1948 switch (query->queryinfo->kind) {
1949 case GEN_PERF_QUERY_TYPE_OA:
1950 case GEN_PERF_QUERY_TYPE_RAW:
1951 bo = query->oa.bo;
1952 break;
1953
1954 case GEN_PERF_QUERY_TYPE_PIPELINE:
1955 bo = query->pipeline_stats.bo;
1956 break;
1957
1958 default:
1959 unreachable("Unknown query type");
1960 break;
1961 }
1962
1963 if (bo == NULL)
1964 return;
1965
1966 /* If the current batch references our results bo then we need to
1967 * flush first...
1968 */
1969 if (perf_cfg->vtbl.batch_references(current_batch, bo))
1970 perf_cfg->vtbl.batchbuffer_flush(perf_ctx->ctx, __FILE__, __LINE__);
1971
1972 perf_cfg->vtbl.bo_wait_rendering(bo);
1973
1974 /* Due to a race condition between the OA unit signaling report
1975 * availability and the report actually being written into memory,
1976 * we need to wait for all the reports to come in before we can
1977 * read them.
1978 */
1979 if (query->queryinfo->kind == GEN_PERF_QUERY_TYPE_OA ||
1980 query->queryinfo->kind == GEN_PERF_QUERY_TYPE_RAW) {
1981 while (!read_oa_samples_for_query(perf_ctx, query, current_batch))
1982 ;
1983 }
1984 }
1985
1986 bool
1987 gen_perf_is_query_ready(struct gen_perf_context *perf_ctx,
1988 struct gen_perf_query_object *query,
1989 void *current_batch)
1990 {
1991 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1992
1993 switch (query->queryinfo->kind) {
1994 case GEN_PERF_QUERY_TYPE_OA:
1995 case GEN_PERF_QUERY_TYPE_RAW:
1996 return (query->oa.results_accumulated ||
1997 (query->oa.bo &&
1998 !perf_cfg->vtbl.batch_references(current_batch, query->oa.bo) &&
1999 !perf_cfg->vtbl.bo_busy(query->oa.bo) &&
2000 read_oa_samples_for_query(perf_ctx, query, current_batch)));
2001 case GEN_PERF_QUERY_TYPE_PIPELINE:
2002 return (query->pipeline_stats.bo &&
2003 !perf_cfg->vtbl.batch_references(current_batch, query->pipeline_stats.bo) &&
2004 !perf_cfg->vtbl.bo_busy(query->pipeline_stats.bo));
2005
2006 default:
2007 unreachable("Unknown query type");
2008 break;
2009 }
2010
2011 return false;
2012 }
2013
2014 /**
2015 * Remove a query from the global list of unaccumulated queries once
2016 * after successfully accumulating the OA reports associated with the
2017 * query in accumulate_oa_reports() or when discarding unwanted query
2018 * results.
2019 */
2020 static void
2021 drop_from_unaccumulated_query_list(struct gen_perf_context *perf_ctx,
2022 struct gen_perf_query_object *query)
2023 {
2024 for (int i = 0; i < perf_ctx->unaccumulated_elements; i++) {
2025 if (perf_ctx->unaccumulated[i] == query) {
2026 int last_elt = --perf_ctx->unaccumulated_elements;
2027
2028 if (i == last_elt)
2029 perf_ctx->unaccumulated[i] = NULL;
2030 else {
2031 perf_ctx->unaccumulated[i] =
2032 perf_ctx->unaccumulated[last_elt];
2033 }
2034
2035 break;
2036 }
2037 }
2038
2039 /* Drop our samples_head reference so that associated periodic
2040 * sample data buffers can potentially be reaped if they aren't
2041 * referenced by any other queries...
2042 */
2043
2044 struct oa_sample_buf *buf =
2045 exec_node_data(struct oa_sample_buf, query->oa.samples_head, link);
2046
2047 assert(buf->refcount > 0);
2048 buf->refcount--;
2049
2050 query->oa.samples_head = NULL;
2051
2052 reap_old_sample_buffers(perf_ctx);
2053 }
2054
2055 /* In general if we see anything spurious while accumulating results,
2056 * we don't try and continue accumulating the current query, hoping
2057 * for the best, we scrap anything outstanding, and then hope for the
2058 * best with new queries.
2059 */
2060 static void
2061 discard_all_queries(struct gen_perf_context *perf_ctx)
2062 {
2063 while (perf_ctx->unaccumulated_elements) {
2064 struct gen_perf_query_object *query = perf_ctx->unaccumulated[0];
2065
2066 query->oa.results_accumulated = true;
2067 drop_from_unaccumulated_query_list(perf_ctx, query);
2068
2069 dec_n_users(perf_ctx);
2070 }
2071 }
2072
2073 /**
2074 * Accumulate raw OA counter values based on deltas between pairs of
2075 * OA reports.
2076 *
2077 * Accumulation starts from the first report captured via
2078 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
2079 * last MI_RPC report requested by brw_end_perf_query(). Between these
2080 * two reports there may also some number of periodically sampled OA
2081 * reports collected via the i915 perf interface - depending on the
2082 * duration of the query.
2083 *
2084 * These periodic snapshots help to ensure we handle counter overflow
2085 * correctly by being frequent enough to ensure we don't miss multiple
2086 * overflows of a counter between snapshots. For Gen8+ the i915 perf
2087 * snapshots provide the extra context-switch reports that let us
2088 * subtract out the progress of counters associated with other
2089 * contexts running on the system.
2090 */
2091 static void
2092 accumulate_oa_reports(struct gen_perf_context *perf_ctx,
2093 struct gen_perf_query_object *query)
2094 {
2095 const struct gen_device_info *devinfo = perf_ctx->devinfo;
2096 uint32_t *start;
2097 uint32_t *last;
2098 uint32_t *end;
2099 struct exec_node *first_samples_node;
2100 bool in_ctx = true;
2101 int out_duration = 0;
2102
2103 assert(query->oa.map != NULL);
2104
2105 start = last = query->oa.map;
2106 end = query->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
2107
2108 if (start[0] != query->oa.begin_report_id) {
2109 DBG("Spurious start report id=%"PRIu32"\n", start[0]);
2110 goto error;
2111 }
2112 if (end[0] != (query->oa.begin_report_id + 1)) {
2113 DBG("Spurious end report id=%"PRIu32"\n", end[0]);
2114 goto error;
2115 }
2116
2117 /* See if we have any periodic reports to accumulate too... */
2118
2119 /* N.B. The oa.samples_head was set when the query began and
2120 * pointed to the tail of the perf_ctx->sample_buffers list at
2121 * the time the query started. Since the buffer existed before the
2122 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
2123 * that no data in this particular node's buffer can possibly be
2124 * associated with the query - so skip ahead one...
2125 */
2126 first_samples_node = query->oa.samples_head->next;
2127
2128 foreach_list_typed_from(struct oa_sample_buf, buf, link,
2129 &perf_ctx.sample_buffers,
2130 first_samples_node)
2131 {
2132 int offset = 0;
2133
2134 while (offset < buf->len) {
2135 const struct drm_i915_perf_record_header *header =
2136 (const struct drm_i915_perf_record_header *)(buf->buf + offset);
2137
2138 assert(header->size != 0);
2139 assert(header->size <= buf->len);
2140
2141 offset += header->size;
2142
2143 switch (header->type) {
2144 case DRM_I915_PERF_RECORD_SAMPLE: {
2145 uint32_t *report = (uint32_t *)(header + 1);
2146 bool add = true;
2147
2148 /* Ignore reports that come before the start marker.
2149 * (Note: takes care to allow overflow of 32bit timestamps)
2150 */
2151 if (gen_device_info_timebase_scale(devinfo,
2152 report[1] - start[1]) > 5000000000) {
2153 continue;
2154 }
2155
2156 /* Ignore reports that come after the end marker.
2157 * (Note: takes care to allow overflow of 32bit timestamps)
2158 */
2159 if (gen_device_info_timebase_scale(devinfo,
2160 report[1] - end[1]) <= 5000000000) {
2161 goto end;
2162 }
2163
2164 /* For Gen8+ since the counters continue while other
2165 * contexts are running we need to discount any unrelated
2166 * deltas. The hardware automatically generates a report
2167 * on context switch which gives us a new reference point
2168 * to continuing adding deltas from.
2169 *
2170 * For Haswell we can rely on the HW to stop the progress
2171 * of OA counters while any other context is acctive.
2172 */
2173 if (devinfo->gen >= 8) {
2174 if (in_ctx && report[2] != query->oa.result.hw_id) {
2175 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
2176 in_ctx = false;
2177 out_duration = 0;
2178 } else if (in_ctx == false && report[2] == query->oa.result.hw_id) {
2179 DBG("i915 perf: Switch TO\n");
2180 in_ctx = true;
2181
2182 /* From experimentation in IGT, we found that the OA unit
2183 * might label some report as "idle" (using an invalid
2184 * context ID), right after a report for a given context.
2185 * Deltas generated by those reports actually belong to the
2186 * previous context, even though they're not labelled as
2187 * such.
2188 *
2189 * We didn't *really* Switch AWAY in the case that we e.g.
2190 * saw a single periodic report while idle...
2191 */
2192 if (out_duration >= 1)
2193 add = false;
2194 } else if (in_ctx) {
2195 assert(report[2] == query->oa.result.hw_id);
2196 DBG("i915 perf: Continuation IN\n");
2197 } else {
2198 assert(report[2] != query->oa.result.hw_id);
2199 DBG("i915 perf: Continuation OUT\n");
2200 add = false;
2201 out_duration++;
2202 }
2203 }
2204
2205 if (add) {
2206 query_result_accumulate(&query->oa.result, query->queryinfo,
2207 last, report);
2208 }
2209
2210 last = report;
2211
2212 break;
2213 }
2214
2215 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST:
2216 DBG("i915 perf: OA error: all reports lost\n");
2217 goto error;
2218 case DRM_I915_PERF_RECORD_OA_REPORT_LOST:
2219 DBG("i915 perf: OA report lost\n");
2220 break;
2221 }
2222 }
2223 }
2224
2225 end:
2226
2227 query_result_accumulate(&query->oa.result, query->queryinfo,
2228 last, end);
2229
2230 query->oa.results_accumulated = true;
2231 drop_from_unaccumulated_query_list(perf_ctx, query);
2232 dec_n_users(perf_ctx);
2233
2234 return;
2235
2236 error:
2237
2238 discard_all_queries(perf_ctx);
2239 }
2240
2241 void
2242 gen_perf_delete_query(struct gen_perf_context *perf_ctx,
2243 struct gen_perf_query_object *query)
2244 {
2245 struct gen_perf_config *perf_cfg = perf_ctx->perf;
2246
2247 /* We can assume that the frontend waits for a query to complete
2248 * before ever calling into here, so we don't have to worry about
2249 * deleting an in-flight query object.
2250 */
2251 switch (query->queryinfo->kind) {
2252 case GEN_PERF_QUERY_TYPE_OA:
2253 case GEN_PERF_QUERY_TYPE_RAW:
2254 if (query->oa.bo) {
2255 if (!query->oa.results_accumulated) {
2256 drop_from_unaccumulated_query_list(perf_ctx, query);
2257 dec_n_users(perf_ctx);
2258 }
2259
2260 perf_cfg->vtbl.bo_unreference(query->oa.bo);
2261 query->oa.bo = NULL;
2262 }
2263
2264 query->oa.results_accumulated = false;
2265 break;
2266
2267 case GEN_PERF_QUERY_TYPE_PIPELINE:
2268 if (query->pipeline_stats.bo) {
2269 perf_cfg->vtbl.bo_unreference(query->pipeline_stats.bo);
2270 query->pipeline_stats.bo = NULL;
2271 }
2272 break;
2273
2274 default:
2275 unreachable("Unknown query type");
2276 break;
2277 }
2278
2279 /* As an indication that the INTEL_performance_query extension is no
2280 * longer in use, it's a good time to free our cache of sample
2281 * buffers and close any current i915-perf stream.
2282 */
2283 if (--perf_ctx->n_query_instances == 0) {
2284 free_sample_bufs(perf_ctx);
2285 gen_perf_close(perf_ctx, query->queryinfo);
2286 }
2287
2288 free(query);
2289 }
2290
2291 #define GET_FIELD(word, field) (((word) & field ## _MASK) >> field ## _SHIFT)
2292
2293 static void
2294 read_gt_frequency(struct gen_perf_context *perf_ctx,
2295 struct gen_perf_query_object *obj)
2296 {
2297 const struct gen_device_info *devinfo = perf_ctx->devinfo;
2298 uint32_t start = *((uint32_t *)(obj->oa.map + MI_FREQ_START_OFFSET_BYTES)),
2299 end = *((uint32_t *)(obj->oa.map + MI_FREQ_END_OFFSET_BYTES));
2300
2301 switch (devinfo->gen) {
2302 case 7:
2303 case 8:
2304 obj->oa.gt_frequency[0] = GET_FIELD(start, GEN7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
2305 obj->oa.gt_frequency[1] = GET_FIELD(end, GEN7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
2306 break;
2307 case 9:
2308 case 10:
2309 case 11:
2310 obj->oa.gt_frequency[0] = GET_FIELD(start, GEN9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
2311 obj->oa.gt_frequency[1] = GET_FIELD(end, GEN9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
2312 break;
2313 default:
2314 unreachable("unexpected gen");
2315 }
2316
2317 /* Put the numbers into Hz. */
2318 obj->oa.gt_frequency[0] *= 1000000ULL;
2319 obj->oa.gt_frequency[1] *= 1000000ULL;
2320 }
2321
2322 static int
2323 get_oa_counter_data(struct gen_perf_context *perf_ctx,
2324 struct gen_perf_query_object *query,
2325 size_t data_size,
2326 uint8_t *data)
2327 {
2328 struct gen_perf_config *perf_cfg = perf_ctx->perf;
2329 const struct gen_perf_query_info *queryinfo = query->queryinfo;
2330 int n_counters = queryinfo->n_counters;
2331 int written = 0;
2332
2333 for (int i = 0; i < n_counters; i++) {
2334 const struct gen_perf_query_counter *counter = &queryinfo->counters[i];
2335 uint64_t *out_uint64;
2336 float *out_float;
2337 size_t counter_size = gen_perf_query_counter_get_size(counter);
2338
2339 if (counter_size) {
2340 switch (counter->data_type) {
2341 case GEN_PERF_COUNTER_DATA_TYPE_UINT64:
2342 out_uint64 = (uint64_t *)(data + counter->offset);
2343 *out_uint64 =
2344 counter->oa_counter_read_uint64(perf_cfg, queryinfo,
2345 query->oa.result.accumulator);
2346 break;
2347 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT:
2348 out_float = (float *)(data + counter->offset);
2349 *out_float =
2350 counter->oa_counter_read_float(perf_cfg, queryinfo,
2351 query->oa.result.accumulator);
2352 break;
2353 default:
2354 /* So far we aren't using uint32, double or bool32... */
2355 unreachable("unexpected counter data type");
2356 }
2357 written = counter->offset + counter_size;
2358 }
2359 }
2360
2361 return written;
2362 }
2363
2364 static int
2365 get_pipeline_stats_data(struct gen_perf_context *perf_ctx,
2366 struct gen_perf_query_object *query,
2367 size_t data_size,
2368 uint8_t *data)
2369
2370 {
2371 struct gen_perf_config *perf_cfg = perf_ctx->perf;
2372 const struct gen_perf_query_info *queryinfo = query->queryinfo;
2373 int n_counters = queryinfo->n_counters;
2374 uint8_t *p = data;
2375
2376 uint64_t *start = perf_cfg->vtbl.bo_map(perf_ctx->ctx, query->pipeline_stats.bo, MAP_READ);
2377 uint64_t *end = start + (STATS_BO_END_OFFSET_BYTES / sizeof(uint64_t));
2378
2379 for (int i = 0; i < n_counters; i++) {
2380 const struct gen_perf_query_counter *counter = &queryinfo->counters[i];
2381 uint64_t value = end[i] - start[i];
2382
2383 if (counter->pipeline_stat.numerator !=
2384 counter->pipeline_stat.denominator) {
2385 value *= counter->pipeline_stat.numerator;
2386 value /= counter->pipeline_stat.denominator;
2387 }
2388
2389 *((uint64_t *)p) = value;
2390 p += 8;
2391 }
2392
2393 perf_cfg->vtbl.bo_unmap(query->pipeline_stats.bo);
2394
2395 return p - data;
2396 }
2397
2398 void
2399 gen_perf_get_query_data(struct gen_perf_context *perf_ctx,
2400 struct gen_perf_query_object *query,
2401 int data_size,
2402 unsigned *data,
2403 unsigned *bytes_written)
2404 {
2405 struct gen_perf_config *perf_cfg = perf_ctx->perf;
2406 int written = 0;
2407
2408 switch (query->queryinfo->kind) {
2409 case GEN_PERF_QUERY_TYPE_OA:
2410 case GEN_PERF_QUERY_TYPE_RAW:
2411 if (!query->oa.results_accumulated) {
2412 read_gt_frequency(perf_ctx, query);
2413 uint32_t *begin_report = query->oa.map;
2414 uint32_t *end_report = query->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
2415 query_result_read_frequencies(&query->oa.result,
2416 perf_ctx->devinfo,
2417 begin_report,
2418 end_report);
2419 accumulate_oa_reports(perf_ctx, query);
2420 assert(query->oa.results_accumulated);
2421
2422 perf_cfg->vtbl.bo_unmap(query->oa.bo);
2423 query->oa.map = NULL;
2424 }
2425 if (query->queryinfo->kind == GEN_PERF_QUERY_TYPE_OA) {
2426 written = get_oa_counter_data(perf_ctx, query, data_size, (uint8_t *)data);
2427 } else {
2428 const struct gen_device_info *devinfo = perf_ctx->devinfo;
2429
2430 written = gen_perf_query_result_write_mdapi((uint8_t *)data, data_size,
2431 devinfo, &query->oa.result,
2432 query->oa.gt_frequency[0],
2433 query->oa.gt_frequency[1]);
2434 }
2435 break;
2436
2437 case GEN_PERF_QUERY_TYPE_PIPELINE:
2438 written = get_pipeline_stats_data(perf_ctx, query, data_size, (uint8_t *)data);
2439 break;
2440
2441 default:
2442 unreachable("Unknown query type");
2443 break;
2444 }
2445
2446 if (bytes_written)
2447 *bytes_written = written;
2448 }
2449
2450 void
2451 gen_perf_dump_query_count(struct gen_perf_context *perf_ctx)
2452 {
2453 DBG("Queries: (Open queries = %d, OA users = %d)\n",
2454 perf_ctx->n_active_oa_queries, perf_ctx->n_oa_users);
2455 }
2456
2457 void
2458 gen_perf_dump_query(struct gen_perf_context *ctx,
2459 struct gen_perf_query_object *obj,
2460 void *current_batch)
2461 {
2462 switch (obj->queryinfo->kind) {
2463 case GEN_PERF_QUERY_TYPE_OA:
2464 case GEN_PERF_QUERY_TYPE_RAW:
2465 DBG("BO: %-4s OA data: %-10s %-15s\n",
2466 obj->oa.bo ? "yes," : "no,",
2467 gen_perf_is_query_ready(ctx, obj, current_batch) ? "ready," : "not ready,",
2468 obj->oa.results_accumulated ? "accumulated" : "not accumulated");
2469 break;
2470 case GEN_PERF_QUERY_TYPE_PIPELINE:
2471 DBG("BO: %-4s\n",
2472 obj->pipeline_stats.bo ? "yes" : "no");
2473 break;
2474 default:
2475 unreachable("Unknown query type");
2476 break;
2477 }
2478 }