intel/perf: refactor gen_perf_begin_query into gen_perf
[mesa.git] / src / mesa / drivers / dri / i965 / brw_performance_query.c
1 /*
2 * Copyright © 2013 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_performance_query.c
26 *
27 * Implementation of the GL_INTEL_performance_query extension.
28 *
29 * Currently there are two possible counter sources exposed here:
30 *
31 * On Gen6+ hardware we have numerous 64bit Pipeline Statistics Registers
32 * that we can snapshot at the beginning and end of a query.
33 *
34 * On Gen7.5+ we have Observability Architecture counters which are
35 * covered in separate document from the rest of the PRMs. It is available at:
36 * https://01.org/linuxgraphics/documentation/driver-documentation-prms
37 * => 2013 Intel Core Processor Family => Observability Performance Counters
38 * (This one volume covers Sandybridge, Ivybridge, Baytrail, and Haswell,
39 * though notably we currently only support OA counters for Haswell+)
40 */
41
42 #include <limits.h>
43
44 /* put before sys/types.h to silence glibc warnings */
45 #ifdef MAJOR_IN_MKDEV
46 #include <sys/mkdev.h>
47 #endif
48 #ifdef MAJOR_IN_SYSMACROS
49 #include <sys/sysmacros.h>
50 #endif
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <sys/mman.h>
55 #include <sys/ioctl.h>
56
57 #include <xf86drm.h>
58 #include "drm-uapi/i915_drm.h"
59
60 #include "main/hash.h"
61 #include "main/macros.h"
62 #include "main/mtypes.h"
63 #include "main/performance_query.h"
64
65 #include "util/bitset.h"
66 #include "util/ralloc.h"
67 #include "util/hash_table.h"
68 #include "util/list.h"
69 #include "util/u_math.h"
70
71 #include "brw_context.h"
72 #include "brw_defines.h"
73 #include "intel_batchbuffer.h"
74
75 #include "perf/gen_perf.h"
76 #include "perf/gen_perf_mdapi.h"
77
78 #define FILE_DEBUG_FLAG DEBUG_PERFMON
79
80 #define OAREPORT_REASON_MASK 0x3f
81 #define OAREPORT_REASON_SHIFT 19
82 #define OAREPORT_REASON_TIMER (1<<0)
83 #define OAREPORT_REASON_TRIGGER1 (1<<1)
84 #define OAREPORT_REASON_TRIGGER2 (1<<2)
85 #define OAREPORT_REASON_CTX_SWITCH (1<<3)
86 #define OAREPORT_REASON_GO_TRANSITION (1<<4)
87
88 struct brw_perf_query_object {
89 struct gl_perf_query_object base;
90 struct gen_perf_query_object *query;
91 };
92
93 /** Downcasting convenience macro. */
94 static inline struct brw_perf_query_object *
95 brw_perf_query(struct gl_perf_query_object *o)
96 {
97 return (struct brw_perf_query_object *) o;
98 }
99
100 #define MI_RPC_BO_SIZE 4096
101 #define MI_RPC_BO_END_OFFSET_BYTES (MI_RPC_BO_SIZE / 2)
102 #define MI_FREQ_START_OFFSET_BYTES (3072)
103 #define MI_FREQ_END_OFFSET_BYTES (3076)
104
105 /******************************************************************************/
106
107 static bool
108 brw_is_perf_query_ready(struct gl_context *ctx,
109 struct gl_perf_query_object *o);
110
111 static void
112 dump_perf_query_callback(GLuint id, void *query_void, void *brw_void)
113 {
114 struct gl_context *ctx = brw_void;
115 struct gl_perf_query_object *o = query_void;
116 struct brw_perf_query_object * brw_query = brw_perf_query(o);
117 struct gen_perf_query_object *obj = brw_query->query;
118
119 switch (obj->queryinfo->kind) {
120 case GEN_PERF_QUERY_TYPE_OA:
121 case GEN_PERF_QUERY_TYPE_RAW:
122 DBG("%4d: %-6s %-8s BO: %-4s OA data: %-10s %-15s\n",
123 id,
124 o->Used ? "Dirty," : "New,",
125 o->Active ? "Active," : (o->Ready ? "Ready," : "Pending,"),
126 obj->oa.bo ? "yes," : "no,",
127 brw_is_perf_query_ready(ctx, o) ? "ready," : "not ready,",
128 obj->oa.results_accumulated ? "accumulated" : "not accumulated");
129 break;
130 case GEN_PERF_QUERY_TYPE_PIPELINE:
131 DBG("%4d: %-6s %-8s BO: %-4s\n",
132 id,
133 o->Used ? "Dirty," : "New,",
134 o->Active ? "Active," : (o->Ready ? "Ready," : "Pending,"),
135 obj->pipeline_stats.bo ? "yes" : "no");
136 break;
137 default:
138 unreachable("Unknown query type");
139 break;
140 }
141 }
142
143 static void
144 dump_perf_queries(struct brw_context *brw)
145 {
146 struct gl_context *ctx = &brw->ctx;
147 DBG("Queries: (Open queries = %d, OA users = %d)\n",
148 brw->perf_ctx.n_active_oa_queries, brw->perf_ctx.n_oa_users);
149 _mesa_HashWalk(ctx->PerfQuery.Objects, dump_perf_query_callback, brw);
150 }
151
152 /**
153 * Driver hook for glGetPerfQueryInfoINTEL().
154 */
155 static void
156 brw_get_perf_query_info(struct gl_context *ctx,
157 unsigned query_index,
158 const char **name,
159 GLuint *data_size,
160 GLuint *n_counters,
161 GLuint *n_active)
162 {
163 struct brw_context *brw = brw_context(ctx);
164 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
165 const struct gen_perf_query_info *query =
166 &perf_ctx->perf->queries[query_index];
167
168 *name = query->name;
169 *data_size = query->data_size;
170 *n_counters = query->n_counters;
171
172 switch (query->kind) {
173 case GEN_PERF_QUERY_TYPE_OA:
174 case GEN_PERF_QUERY_TYPE_RAW:
175 *n_active = perf_ctx->n_active_oa_queries;
176 break;
177
178 case GEN_PERF_QUERY_TYPE_PIPELINE:
179 *n_active = perf_ctx->n_active_pipeline_stats_queries;
180 break;
181
182 default:
183 unreachable("Unknown query type");
184 break;
185 }
186 }
187
188 static GLuint
189 gen_counter_type_enum_to_gl_type(enum gen_perf_counter_type type)
190 {
191 switch (type) {
192 case GEN_PERF_COUNTER_TYPE_EVENT: return GL_PERFQUERY_COUNTER_EVENT_INTEL;
193 case GEN_PERF_COUNTER_TYPE_DURATION_NORM: return GL_PERFQUERY_COUNTER_DURATION_NORM_INTEL;
194 case GEN_PERF_COUNTER_TYPE_DURATION_RAW: return GL_PERFQUERY_COUNTER_DURATION_RAW_INTEL;
195 case GEN_PERF_COUNTER_TYPE_THROUGHPUT: return GL_PERFQUERY_COUNTER_THROUGHPUT_INTEL;
196 case GEN_PERF_COUNTER_TYPE_RAW: return GL_PERFQUERY_COUNTER_RAW_INTEL;
197 case GEN_PERF_COUNTER_TYPE_TIMESTAMP: return GL_PERFQUERY_COUNTER_TIMESTAMP_INTEL;
198 default:
199 unreachable("Unknown counter type");
200 }
201 }
202
203 static GLuint
204 gen_counter_data_type_to_gl_type(enum gen_perf_counter_data_type type)
205 {
206 switch (type) {
207 case GEN_PERF_COUNTER_DATA_TYPE_BOOL32: return GL_PERFQUERY_COUNTER_DATA_BOOL32_INTEL;
208 case GEN_PERF_COUNTER_DATA_TYPE_UINT32: return GL_PERFQUERY_COUNTER_DATA_UINT32_INTEL;
209 case GEN_PERF_COUNTER_DATA_TYPE_UINT64: return GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL;
210 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT: return GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL;
211 case GEN_PERF_COUNTER_DATA_TYPE_DOUBLE: return GL_PERFQUERY_COUNTER_DATA_DOUBLE_INTEL;
212 default:
213 unreachable("Unknown counter data type");
214 }
215 }
216
217 /**
218 * Driver hook for glGetPerfCounterInfoINTEL().
219 */
220 static void
221 brw_get_perf_counter_info(struct gl_context *ctx,
222 unsigned query_index,
223 unsigned counter_index,
224 const char **name,
225 const char **desc,
226 GLuint *offset,
227 GLuint *data_size,
228 GLuint *type_enum,
229 GLuint *data_type_enum,
230 GLuint64 *raw_max)
231 {
232 struct brw_context *brw = brw_context(ctx);
233 const struct gen_perf_query_info *query =
234 &brw->perf_ctx.perf->queries[query_index];
235 const struct gen_perf_query_counter *counter =
236 &query->counters[counter_index];
237
238 *name = counter->name;
239 *desc = counter->desc;
240 *offset = counter->offset;
241 *data_size = gen_perf_query_counter_get_size(counter);
242 *type_enum = gen_counter_type_enum_to_gl_type(counter->type);
243 *data_type_enum = gen_counter_data_type_to_gl_type(counter->data_type);
244 *raw_max = counter->raw_max;
245 }
246
247 /**
248 * Remove a query from the global list of unaccumulated queries once
249 * after successfully accumulating the OA reports associated with the
250 * query in accumulate_oa_reports() or when discarding unwanted query
251 * results.
252 */
253 static void
254 drop_from_unaccumulated_query_list(struct brw_context *brw,
255 struct gen_perf_query_object *obj)
256 {
257 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
258 for (int i = 0; i < perf_ctx->unaccumulated_elements; i++) {
259 if (perf_ctx->unaccumulated[i] == obj) {
260 int last_elt = --perf_ctx->unaccumulated_elements;
261
262 if (i == last_elt)
263 perf_ctx->unaccumulated[i] = NULL;
264 else {
265 perf_ctx->unaccumulated[i] =
266 perf_ctx->unaccumulated[last_elt];
267 }
268
269 break;
270 }
271 }
272
273 /* Drop our samples_head reference so that associated periodic
274 * sample data buffers can potentially be reaped if they aren't
275 * referenced by any other queries...
276 */
277
278 struct oa_sample_buf *buf =
279 exec_node_data(struct oa_sample_buf, obj->oa.samples_head, link);
280
281 assert(buf->refcount > 0);
282 buf->refcount--;
283
284 obj->oa.samples_head = NULL;
285
286 gen_perf_reap_old_sample_buffers(&brw->perf_ctx);
287 }
288
289 /* In general if we see anything spurious while accumulating results,
290 * we don't try and continue accumulating the current query, hoping
291 * for the best, we scrap anything outstanding, and then hope for the
292 * best with new queries.
293 */
294 static void
295 discard_all_queries(struct brw_context *brw)
296 {
297 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
298 while (perf_ctx->unaccumulated_elements) {
299 struct gen_perf_query_object *obj = perf_ctx->unaccumulated[0];
300
301 obj->oa.results_accumulated = true;
302 drop_from_unaccumulated_query_list(brw, perf_ctx->unaccumulated[0]);
303
304 gen_perf_dec_n_users(perf_ctx);
305 }
306 }
307
308 enum OaReadStatus {
309 OA_READ_STATUS_ERROR,
310 OA_READ_STATUS_UNFINISHED,
311 OA_READ_STATUS_FINISHED,
312 };
313
314 static enum OaReadStatus
315 read_oa_samples_until(struct brw_context *brw,
316 uint32_t start_timestamp,
317 uint32_t end_timestamp)
318 {
319 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
320 struct exec_node *tail_node =
321 exec_list_get_tail(&perf_ctx->sample_buffers);
322 struct oa_sample_buf *tail_buf =
323 exec_node_data(struct oa_sample_buf, tail_node, link);
324 uint32_t last_timestamp = tail_buf->last_timestamp;
325
326 while (1) {
327 struct oa_sample_buf *buf = gen_perf_get_free_sample_buf(perf_ctx);
328 uint32_t offset;
329 int len;
330
331 while ((len = read(perf_ctx->oa_stream_fd, buf->buf,
332 sizeof(buf->buf))) < 0 && errno == EINTR)
333 ;
334
335 if (len <= 0) {
336 exec_list_push_tail(&perf_ctx->free_sample_buffers, &buf->link);
337
338 if (len < 0) {
339 if (errno == EAGAIN)
340 return ((last_timestamp - start_timestamp) >=
341 (end_timestamp - start_timestamp)) ?
342 OA_READ_STATUS_FINISHED :
343 OA_READ_STATUS_UNFINISHED;
344 else {
345 DBG("Error reading i915 perf samples: %m\n");
346 }
347 } else
348 DBG("Spurious EOF reading i915 perf samples\n");
349
350 return OA_READ_STATUS_ERROR;
351 }
352
353 buf->len = len;
354 exec_list_push_tail(&perf_ctx->sample_buffers, &buf->link);
355
356 /* Go through the reports and update the last timestamp. */
357 offset = 0;
358 while (offset < buf->len) {
359 const struct drm_i915_perf_record_header *header =
360 (const struct drm_i915_perf_record_header *) &buf->buf[offset];
361 uint32_t *report = (uint32_t *) (header + 1);
362
363 if (header->type == DRM_I915_PERF_RECORD_SAMPLE)
364 last_timestamp = report[1];
365
366 offset += header->size;
367 }
368
369 buf->last_timestamp = last_timestamp;
370 }
371
372 unreachable("not reached");
373 return OA_READ_STATUS_ERROR;
374 }
375
376 /**
377 * Try to read all the reports until either the delimiting timestamp
378 * or an error arises.
379 */
380 static bool
381 read_oa_samples_for_query(struct brw_context *brw,
382 struct gen_perf_query_object *obj)
383 {
384 uint32_t *start;
385 uint32_t *last;
386 uint32_t *end;
387
388 /* We need the MI_REPORT_PERF_COUNT to land before we can start
389 * accumulate. */
390 assert(!brw_batch_references(&brw->batch, obj->oa.bo) &&
391 !brw_bo_busy(obj->oa.bo));
392
393 /* Map the BO once here and let accumulate_oa_reports() unmap
394 * it. */
395 if (obj->oa.map == NULL)
396 obj->oa.map = brw->perf_ctx.perf->vtbl.bo_map(brw, obj->oa.bo, MAP_READ);
397
398 start = last = obj->oa.map;
399 end = obj->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
400
401 if (start[0] != obj->oa.begin_report_id) {
402 DBG("Spurious start report id=%"PRIu32"\n", start[0]);
403 return true;
404 }
405 if (end[0] != (obj->oa.begin_report_id + 1)) {
406 DBG("Spurious end report id=%"PRIu32"\n", end[0]);
407 return true;
408 }
409
410 /* Read the reports until the end timestamp. */
411 switch (read_oa_samples_until(brw, start[1], end[1])) {
412 case OA_READ_STATUS_ERROR:
413 /* Fallthrough and let accumulate_oa_reports() deal with the
414 * error. */
415 case OA_READ_STATUS_FINISHED:
416 return true;
417 case OA_READ_STATUS_UNFINISHED:
418 return false;
419 }
420
421 unreachable("invalid read status");
422 return false;
423 }
424
425 /**
426 * Accumulate raw OA counter values based on deltas between pairs of
427 * OA reports.
428 *
429 * Accumulation starts from the first report captured via
430 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
431 * last MI_RPC report requested by brw_end_perf_query(). Between these
432 * two reports there may also some number of periodically sampled OA
433 * reports collected via the i915 perf interface - depending on the
434 * duration of the query.
435 *
436 * These periodic snapshots help to ensure we handle counter overflow
437 * correctly by being frequent enough to ensure we don't miss multiple
438 * overflows of a counter between snapshots. For Gen8+ the i915 perf
439 * snapshots provide the extra context-switch reports that let us
440 * subtract out the progress of counters associated with other
441 * contexts running on the system.
442 */
443 static void
444 accumulate_oa_reports(struct brw_context *brw,
445 struct brw_perf_query_object *brw_query)
446 {
447 const struct gen_device_info *devinfo = &brw->screen->devinfo;
448 struct gen_perf_query_object *obj = brw_query->query;
449 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
450 uint32_t *start;
451 uint32_t *last;
452 uint32_t *end;
453 struct exec_node *first_samples_node;
454 bool in_ctx = true;
455 int out_duration = 0;
456
457 assert(brw_query->base.Ready);
458 assert(obj->oa.map != NULL);
459
460 start = last = obj->oa.map;
461 end = obj->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
462
463 if (start[0] != obj->oa.begin_report_id) {
464 DBG("Spurious start report id=%"PRIu32"\n", start[0]);
465 goto error;
466 }
467 if (end[0] != (obj->oa.begin_report_id + 1)) {
468 DBG("Spurious end report id=%"PRIu32"\n", end[0]);
469 goto error;
470 }
471
472 /* See if we have any periodic reports to accumulate too... */
473
474 /* N.B. The oa.samples_head was set when the query began and
475 * pointed to the tail of the perf_ctx->sample_buffers list at
476 * the time the query started. Since the buffer existed before the
477 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
478 * that no data in this particular node's buffer can possibly be
479 * associated with the query - so skip ahead one...
480 */
481 first_samples_node = obj->oa.samples_head->next;
482
483 foreach_list_typed_from(struct oa_sample_buf, buf, link,
484 &brw->perf_ctx.sample_buffers,
485 first_samples_node)
486 {
487 int offset = 0;
488
489 while (offset < buf->len) {
490 const struct drm_i915_perf_record_header *header =
491 (const struct drm_i915_perf_record_header *)(buf->buf + offset);
492
493 assert(header->size != 0);
494 assert(header->size <= buf->len);
495
496 offset += header->size;
497
498 switch (header->type) {
499 case DRM_I915_PERF_RECORD_SAMPLE: {
500 uint32_t *report = (uint32_t *)(header + 1);
501 bool add = true;
502
503 /* Ignore reports that come before the start marker.
504 * (Note: takes care to allow overflow of 32bit timestamps)
505 */
506 if (gen_device_info_timebase_scale(devinfo,
507 report[1] - start[1]) > 5000000000) {
508 continue;
509 }
510
511 /* Ignore reports that come after the end marker.
512 * (Note: takes care to allow overflow of 32bit timestamps)
513 */
514 if (gen_device_info_timebase_scale(devinfo,
515 report[1] - end[1]) <= 5000000000) {
516 goto end;
517 }
518
519 /* For Gen8+ since the counters continue while other
520 * contexts are running we need to discount any unrelated
521 * deltas. The hardware automatically generates a report
522 * on context switch which gives us a new reference point
523 * to continuing adding deltas from.
524 *
525 * For Haswell we can rely on the HW to stop the progress
526 * of OA counters while any other context is acctive.
527 */
528 if (devinfo->gen >= 8) {
529 if (in_ctx && report[2] != obj->oa.result.hw_id) {
530 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
531 in_ctx = false;
532 out_duration = 0;
533 } else if (in_ctx == false && report[2] == obj->oa.result.hw_id) {
534 DBG("i915 perf: Switch TO\n");
535 in_ctx = true;
536
537 /* From experimentation in IGT, we found that the OA unit
538 * might label some report as "idle" (using an invalid
539 * context ID), right after a report for a given context.
540 * Deltas generated by those reports actually belong to the
541 * previous context, even though they're not labelled as
542 * such.
543 *
544 * We didn't *really* Switch AWAY in the case that we e.g.
545 * saw a single periodic report while idle...
546 */
547 if (out_duration >= 1)
548 add = false;
549 } else if (in_ctx) {
550 assert(report[2] == obj->oa.result.hw_id);
551 DBG("i915 perf: Continuation IN\n");
552 } else {
553 assert(report[2] != obj->oa.result.hw_id);
554 DBG("i915 perf: Continuation OUT\n");
555 add = false;
556 out_duration++;
557 }
558 }
559
560 if (add) {
561 gen_perf_query_result_accumulate(&obj->oa.result, obj->queryinfo,
562 last, report);
563 }
564
565 last = report;
566
567 break;
568 }
569
570 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST:
571 DBG("i915 perf: OA error: all reports lost\n");
572 goto error;
573 case DRM_I915_PERF_RECORD_OA_REPORT_LOST:
574 DBG("i915 perf: OA report lost\n");
575 break;
576 }
577 }
578 }
579
580 end:
581
582 gen_perf_query_result_accumulate(&obj->oa.result, obj->queryinfo,
583 last, end);
584
585 DBG("Marking %d accumulated - results gathered\n", brw_query->base.Id);
586
587 obj->oa.results_accumulated = true;
588 drop_from_unaccumulated_query_list(brw, obj);
589 gen_perf_dec_n_users(perf_ctx);
590
591 return;
592
593 error:
594
595 discard_all_queries(brw);
596 }
597
598 /******************************************************************************/
599
600 static void
601 capture_frequency_stat_register(struct brw_context *brw,
602 struct brw_bo *bo,
603 uint32_t bo_offset)
604 {
605 const struct gen_device_info *devinfo = &brw->screen->devinfo;
606
607 if (devinfo->gen >= 7 && devinfo->gen <= 8 &&
608 !devinfo->is_baytrail && !devinfo->is_cherryview) {
609 brw_store_register_mem32(brw, bo, GEN7_RPSTAT1, bo_offset);
610 } else if (devinfo->gen >= 9) {
611 brw_store_register_mem32(brw, bo, GEN9_RPSTAT0, bo_offset);
612 }
613 }
614
615 /**
616 * Driver hook for glBeginPerfQueryINTEL().
617 */
618 static bool
619 brw_begin_perf_query(struct gl_context *ctx,
620 struct gl_perf_query_object *o)
621 {
622 struct brw_context *brw = brw_context(ctx);
623 struct brw_perf_query_object *brw_query = brw_perf_query(o);
624 struct gen_perf_query_object *obj = brw_query->query;
625 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
626
627 /* We can assume the frontend hides mistaken attempts to Begin a
628 * query object multiple times before its End. Similarly if an
629 * application reuses a query object before results have arrived
630 * the frontend will wait for prior results so we don't need
631 * to support abandoning in-flight results.
632 */
633 assert(!o->Active);
634 assert(!o->Used || o->Ready); /* no in-flight query to worry about */
635
636 DBG("Begin(%d)\n", o->Id);
637
638 gen_perf_begin_query(perf_ctx, obj);
639
640 if (INTEL_DEBUG & DEBUG_PERFMON)
641 dump_perf_queries(brw);
642
643 return true;
644 }
645
646 /**
647 * Driver hook for glEndPerfQueryINTEL().
648 */
649 static void
650 brw_end_perf_query(struct gl_context *ctx,
651 struct gl_perf_query_object *o)
652 {
653 struct brw_context *brw = brw_context(ctx);
654 struct brw_perf_query_object *brw_query = brw_perf_query(o);
655 struct gen_perf_query_object *obj = brw_query->query;
656 struct gen_perf_config *perf_cfg = brw->perf_ctx.perf;
657 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
658
659 DBG("End(%d)\n", o->Id);
660
661 /* Ensure that the work associated with the queried commands will have
662 * finished before taking our query end counter readings.
663 *
664 * For more details see comment in brw_begin_perf_query for
665 * corresponding flush.
666 */
667 perf_cfg->vtbl.emit_mi_flush(perf_ctx->ctx);
668
669 switch (obj->queryinfo->kind) {
670 case GEN_PERF_QUERY_TYPE_OA:
671 case GEN_PERF_QUERY_TYPE_RAW:
672
673 /* NB: It's possible that the query will have already been marked
674 * as 'accumulated' if an error was seen while reading samples
675 * from perf. In this case we mustn't try and emit a closing
676 * MI_RPC command in case the OA unit has already been disabled
677 */
678 if (!obj->oa.results_accumulated) {
679 /* Take an ending OA counter snapshot. */
680 perf_cfg->vtbl.capture_frequency_stat_register(perf_ctx->ctx, obj->oa.bo,
681 MI_FREQ_END_OFFSET_BYTES);
682 brw->vtbl.emit_mi_report_perf_count(perf_ctx->ctx, obj->oa.bo,
683 MI_RPC_BO_END_OFFSET_BYTES,
684 obj->oa.begin_report_id + 1);
685 }
686
687 --perf_ctx->n_active_oa_queries;
688
689 /* NB: even though the query has now ended, it can't be accumulated
690 * until the end MI_REPORT_PERF_COUNT snapshot has been written
691 * to query->oa.bo
692 */
693 break;
694
695 case GEN_PERF_QUERY_TYPE_PIPELINE:
696 gen_perf_snapshot_statistics_registers(brw, perf_cfg, obj,
697 STATS_BO_END_OFFSET_BYTES);
698 --perf_ctx->n_active_pipeline_stats_queries;
699 break;
700
701 default:
702 unreachable("Unknown query type");
703 break;
704 }
705 }
706
707 static void
708 brw_wait_perf_query(struct gl_context *ctx, struct gl_perf_query_object *o)
709 {
710 struct brw_context *brw = brw_context(ctx);
711 struct brw_perf_query_object *brw_query = brw_perf_query(o);
712 struct gen_perf_query_object *obj = brw_query->query;
713 struct brw_bo *bo = NULL;
714 struct gen_perf_config *perf_cfg = brw->perf_ctx.perf;
715
716 assert(!o->Ready);
717
718 switch (obj->queryinfo->kind) {
719 case GEN_PERF_QUERY_TYPE_OA:
720 case GEN_PERF_QUERY_TYPE_RAW:
721 bo = obj->oa.bo;
722 break;
723
724 case GEN_PERF_QUERY_TYPE_PIPELINE:
725 bo = obj->pipeline_stats.bo;
726 break;
727
728 default:
729 unreachable("Unknown query type");
730 break;
731 }
732
733 if (bo == NULL)
734 return;
735
736 /* If the current batch references our results bo then we need to
737 * flush first...
738 */
739 if (brw_batch_references(&brw->batch, bo))
740 perf_cfg->vtbl.batchbuffer_flush(brw, __FILE__, __LINE__);
741
742 brw_bo_wait_rendering(bo);
743
744 /* Due to a race condition between the OA unit signaling report
745 * availability and the report actually being written into memory,
746 * we need to wait for all the reports to come in before we can
747 * read them.
748 */
749 if (obj->queryinfo->kind == GEN_PERF_QUERY_TYPE_OA ||
750 obj->queryinfo->kind == GEN_PERF_QUERY_TYPE_RAW) {
751 while (!read_oa_samples_for_query(brw, obj))
752 ;
753 }
754 }
755
756 static bool
757 brw_is_perf_query_ready(struct gl_context *ctx,
758 struct gl_perf_query_object *o)
759 {
760 struct brw_context *brw = brw_context(ctx);
761 struct brw_perf_query_object *brw_query = brw_perf_query(o);
762 struct gen_perf_query_object *obj = brw_query->query;
763
764 if (o->Ready)
765 return true;
766
767 switch (obj->queryinfo->kind) {
768 case GEN_PERF_QUERY_TYPE_OA:
769 case GEN_PERF_QUERY_TYPE_RAW:
770 return (obj->oa.results_accumulated ||
771 (obj->oa.bo &&
772 !brw_batch_references(&brw->batch, obj->oa.bo) &&
773 !brw_bo_busy(obj->oa.bo) &&
774 read_oa_samples_for_query(brw, obj)));
775 case GEN_PERF_QUERY_TYPE_PIPELINE:
776 return (obj->pipeline_stats.bo &&
777 !brw_batch_references(&brw->batch, obj->pipeline_stats.bo) &&
778 !brw_bo_busy(obj->pipeline_stats.bo));
779
780 default:
781 unreachable("Unknown query type");
782 break;
783 }
784
785 return false;
786 }
787
788 static void
789 read_slice_unslice_frequencies(struct brw_context *brw,
790 struct gen_perf_query_object *obj)
791 {
792 const struct gen_device_info *devinfo = &brw->screen->devinfo;
793 uint32_t *begin_report = obj->oa.map, *end_report = obj->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
794
795 gen_perf_query_result_read_frequencies(&obj->oa.result,
796 devinfo, begin_report, end_report);
797 }
798
799 static void
800 read_gt_frequency(struct brw_context *brw,
801 struct gen_perf_query_object *obj)
802 {
803 const struct gen_device_info *devinfo = &brw->screen->devinfo;
804 uint32_t start = *((uint32_t *)(obj->oa.map + MI_FREQ_START_OFFSET_BYTES)),
805 end = *((uint32_t *)(obj->oa.map + MI_FREQ_END_OFFSET_BYTES));
806
807 switch (devinfo->gen) {
808 case 7:
809 case 8:
810 obj->oa.gt_frequency[0] = GET_FIELD(start, GEN7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
811 obj->oa.gt_frequency[1] = GET_FIELD(end, GEN7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
812 break;
813 case 9:
814 case 10:
815 case 11:
816 obj->oa.gt_frequency[0] = GET_FIELD(start, GEN9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
817 obj->oa.gt_frequency[1] = GET_FIELD(end, GEN9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
818 break;
819 default:
820 unreachable("unexpected gen");
821 }
822
823 /* Put the numbers into Hz. */
824 obj->oa.gt_frequency[0] *= 1000000ULL;
825 obj->oa.gt_frequency[1] *= 1000000ULL;
826 }
827
828 static int
829 get_oa_counter_data(struct brw_context *brw,
830 struct gen_perf_query_object *obj,
831 size_t data_size,
832 uint8_t *data)
833 {
834 struct gen_perf_config *perf = brw->perf_ctx.perf;
835 const struct gen_perf_query_info *query = obj->queryinfo;
836 int n_counters = query->n_counters;
837 int written = 0;
838
839 for (int i = 0; i < n_counters; i++) {
840 const struct gen_perf_query_counter *counter = &query->counters[i];
841 uint64_t *out_uint64;
842 float *out_float;
843 size_t counter_size = gen_perf_query_counter_get_size(counter);
844
845 if (counter_size) {
846 switch (counter->data_type) {
847 case GEN_PERF_COUNTER_DATA_TYPE_UINT64:
848 out_uint64 = (uint64_t *)(data + counter->offset);
849 *out_uint64 =
850 counter->oa_counter_read_uint64(perf, query,
851 obj->oa.result.accumulator);
852 break;
853 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT:
854 out_float = (float *)(data + counter->offset);
855 *out_float =
856 counter->oa_counter_read_float(perf, query,
857 obj->oa.result.accumulator);
858 break;
859 default:
860 /* So far we aren't using uint32, double or bool32... */
861 unreachable("unexpected counter data type");
862 }
863 written = counter->offset + counter_size;
864 }
865 }
866
867 return written;
868 }
869
870 static int
871 get_pipeline_stats_data(struct brw_context *brw,
872 struct gen_perf_query_object *obj,
873 size_t data_size,
874 uint8_t *data)
875
876 {
877 const struct gen_perf_query_info *query = obj->queryinfo;
878 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
879 struct gen_perf_config *perf_cfg = perf_ctx->perf;
880 int n_counters = obj->queryinfo->n_counters;
881 uint8_t *p = data;
882
883 uint64_t *start = perf_cfg->vtbl.bo_map(perf_ctx->ctx, obj->pipeline_stats.bo, MAP_READ);
884 uint64_t *end = start + (STATS_BO_END_OFFSET_BYTES / sizeof(uint64_t));
885
886 for (int i = 0; i < n_counters; i++) {
887 const struct gen_perf_query_counter *counter = &query->counters[i];
888 uint64_t value = end[i] - start[i];
889
890 if (counter->pipeline_stat.numerator !=
891 counter->pipeline_stat.denominator) {
892 value *= counter->pipeline_stat.numerator;
893 value /= counter->pipeline_stat.denominator;
894 }
895
896 *((uint64_t *)p) = value;
897 p += 8;
898 }
899
900 perf_cfg->vtbl.bo_unmap(obj->pipeline_stats.bo);
901
902 return p - data;
903 }
904
905 /**
906 * Driver hook for glGetPerfQueryDataINTEL().
907 */
908 static void
909 brw_get_perf_query_data(struct gl_context *ctx,
910 struct gl_perf_query_object *o,
911 GLsizei data_size,
912 GLuint *data,
913 GLuint *bytes_written)
914 {
915 struct brw_context *brw = brw_context(ctx);
916 struct brw_perf_query_object *brw_query = brw_perf_query(o);
917 struct gen_perf_query_object *obj = brw_query->query;
918 int written = 0;
919
920 assert(brw_is_perf_query_ready(ctx, o));
921
922 DBG("GetData(%d)\n", o->Id);
923
924 if (INTEL_DEBUG & DEBUG_PERFMON)
925 dump_perf_queries(brw);
926
927 /* We expect that the frontend only calls this hook when it knows
928 * that results are available.
929 */
930 assert(o->Ready);
931
932 switch (obj->queryinfo->kind) {
933 case GEN_PERF_QUERY_TYPE_OA:
934 case GEN_PERF_QUERY_TYPE_RAW:
935 if (!obj->oa.results_accumulated) {
936 read_gt_frequency(brw, obj);
937 read_slice_unslice_frequencies(brw, obj);
938 accumulate_oa_reports(brw, brw_query);
939 assert(obj->oa.results_accumulated);
940
941 brw->perf_ctx.perf->vtbl.bo_unmap(obj->oa.bo);
942 obj->oa.map = NULL;
943 }
944 if (obj->queryinfo->kind == GEN_PERF_QUERY_TYPE_OA) {
945 written = get_oa_counter_data(brw, obj, data_size, (uint8_t *)data);
946 } else {
947 const struct gen_device_info *devinfo = &brw->screen->devinfo;
948
949 written = gen_perf_query_result_write_mdapi((uint8_t *)data, data_size,
950 devinfo, &obj->oa.result,
951 obj->oa.gt_frequency[0],
952 obj->oa.gt_frequency[1]);
953 }
954 break;
955
956 case GEN_PERF_QUERY_TYPE_PIPELINE:
957 written = get_pipeline_stats_data(brw, obj, data_size, (uint8_t *)data);
958 break;
959
960 default:
961 unreachable("Unknown query type");
962 break;
963 }
964
965 if (bytes_written)
966 *bytes_written = written;
967 }
968
969 static struct gl_perf_query_object *
970 brw_new_perf_query_object(struct gl_context *ctx, unsigned query_index)
971 {
972 struct brw_context *brw = brw_context(ctx);
973 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
974 const struct gen_perf_query_info *queryinfo =
975 &perf_ctx->perf->queries[query_index];
976 struct gen_perf_query_object *obj =
977 calloc(1, sizeof(struct gen_perf_query_object));
978
979 if (!obj)
980 return NULL;
981
982 obj->queryinfo = queryinfo;
983
984 perf_ctx->n_query_instances++;
985
986 struct brw_perf_query_object *brw_query = calloc(1, sizeof(struct brw_perf_query_object));
987 if (unlikely(!brw_query))
988 return NULL;
989 brw_query->query = obj;
990 return &brw_query->base;
991 }
992
993 /**
994 * Driver hook for glDeletePerfQueryINTEL().
995 */
996 static void
997 brw_delete_perf_query(struct gl_context *ctx,
998 struct gl_perf_query_object *o)
999 {
1000 struct brw_context *brw = brw_context(ctx);
1001 struct gen_perf_config *perf_cfg = brw->perf_ctx.perf;
1002 struct brw_perf_query_object *brw_query = brw_perf_query(o);
1003 struct gen_perf_query_object *obj = brw_query->query;
1004 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
1005
1006 /* We can assume that the frontend waits for a query to complete
1007 * before ever calling into here, so we don't have to worry about
1008 * deleting an in-flight query object.
1009 */
1010 assert(!o->Active);
1011 assert(!o->Used || o->Ready);
1012
1013 DBG("Delete(%d)\n", o->Id);
1014
1015 switch (obj->queryinfo->kind) {
1016 case GEN_PERF_QUERY_TYPE_OA:
1017 case GEN_PERF_QUERY_TYPE_RAW:
1018 if (obj->oa.bo) {
1019 if (!obj->oa.results_accumulated) {
1020 drop_from_unaccumulated_query_list(brw, obj);
1021 gen_perf_dec_n_users(perf_ctx);
1022 }
1023
1024 perf_cfg->vtbl.bo_unreference(obj->oa.bo);
1025 obj->oa.bo = NULL;
1026 }
1027
1028 obj->oa.results_accumulated = false;
1029 break;
1030
1031 case GEN_PERF_QUERY_TYPE_PIPELINE:
1032 if (obj->pipeline_stats.bo) {
1033 perf_cfg->vtbl.bo_unreference(obj->pipeline_stats.bo);
1034 obj->pipeline_stats.bo = NULL;
1035 }
1036 break;
1037
1038 default:
1039 unreachable("Unknown query type");
1040 break;
1041 }
1042
1043 /* As an indication that the INTEL_performance_query extension is no
1044 * longer in use, it's a good time to free our cache of sample
1045 * buffers and close any current i915-perf stream.
1046 */
1047 if (--perf_ctx->n_query_instances == 0) {
1048 gen_perf_free_sample_bufs(perf_ctx);
1049 gen_perf_close(perf_ctx, obj->queryinfo);
1050 }
1051
1052 free(obj);
1053 free(brw_query);
1054 }
1055
1056 /******************************************************************************/
1057
1058 static void
1059 init_pipeline_statistic_query_registers(struct brw_context *brw)
1060 {
1061 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1062 struct gen_perf_config *perf = brw->perf_ctx.perf;
1063 struct gen_perf_query_info *query =
1064 gen_perf_query_append_query_info(perf, MAX_STAT_COUNTERS);
1065
1066 query->kind = GEN_PERF_QUERY_TYPE_PIPELINE;
1067 query->name = "Pipeline Statistics Registers";
1068
1069 gen_perf_query_info_add_basic_stat_reg(query, IA_VERTICES_COUNT,
1070 "N vertices submitted");
1071 gen_perf_query_info_add_basic_stat_reg(query, IA_PRIMITIVES_COUNT,
1072 "N primitives submitted");
1073 gen_perf_query_info_add_basic_stat_reg(query, VS_INVOCATION_COUNT,
1074 "N vertex shader invocations");
1075
1076 if (devinfo->gen == 6) {
1077 gen_perf_query_info_add_stat_reg(query, GEN6_SO_PRIM_STORAGE_NEEDED, 1, 1,
1078 "SO_PRIM_STORAGE_NEEDED",
1079 "N geometry shader stream-out primitives (total)");
1080 gen_perf_query_info_add_stat_reg(query, GEN6_SO_NUM_PRIMS_WRITTEN, 1, 1,
1081 "SO_NUM_PRIMS_WRITTEN",
1082 "N geometry shader stream-out primitives (written)");
1083 } else {
1084 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
1085 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
1086 "N stream-out (stream 0) primitives (total)");
1087 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
1088 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
1089 "N stream-out (stream 1) primitives (total)");
1090 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
1091 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
1092 "N stream-out (stream 2) primitives (total)");
1093 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
1094 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
1095 "N stream-out (stream 3) primitives (total)");
1096 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
1097 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
1098 "N stream-out (stream 0) primitives (written)");
1099 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
1100 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
1101 "N stream-out (stream 1) primitives (written)");
1102 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
1103 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
1104 "N stream-out (stream 2) primitives (written)");
1105 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
1106 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
1107 "N stream-out (stream 3) primitives (written)");
1108 }
1109
1110 gen_perf_query_info_add_basic_stat_reg(query, HS_INVOCATION_COUNT,
1111 "N TCS shader invocations");
1112 gen_perf_query_info_add_basic_stat_reg(query, DS_INVOCATION_COUNT,
1113 "N TES shader invocations");
1114
1115 gen_perf_query_info_add_basic_stat_reg(query, GS_INVOCATION_COUNT,
1116 "N geometry shader invocations");
1117 gen_perf_query_info_add_basic_stat_reg(query, GS_PRIMITIVES_COUNT,
1118 "N geometry shader primitives emitted");
1119
1120 gen_perf_query_info_add_basic_stat_reg(query, CL_INVOCATION_COUNT,
1121 "N primitives entering clipping");
1122 gen_perf_query_info_add_basic_stat_reg(query, CL_PRIMITIVES_COUNT,
1123 "N primitives leaving clipping");
1124
1125 if (devinfo->is_haswell || devinfo->gen == 8) {
1126 gen_perf_query_info_add_stat_reg(query, PS_INVOCATION_COUNT, 1, 4,
1127 "N fragment shader invocations",
1128 "N fragment shader invocations");
1129 } else {
1130 gen_perf_query_info_add_basic_stat_reg(query, PS_INVOCATION_COUNT,
1131 "N fragment shader invocations");
1132 }
1133
1134 gen_perf_query_info_add_basic_stat_reg(query, PS_DEPTH_COUNT,
1135 "N z-pass fragments");
1136
1137 if (devinfo->gen >= 7) {
1138 gen_perf_query_info_add_basic_stat_reg(query, CS_INVOCATION_COUNT,
1139 "N compute shader invocations");
1140 }
1141
1142 query->data_size = sizeof(uint64_t) * query->n_counters;
1143 }
1144
1145 /* gen_device_info will have incorrect default topology values for unsupported kernels.
1146 * verify kernel support to ensure OA metrics are accurate.
1147 */
1148 static bool
1149 oa_metrics_kernel_support(int fd, const struct gen_device_info *devinfo)
1150 {
1151 if (devinfo->gen >= 10) {
1152 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
1153 * to verify support
1154 */
1155 struct drm_i915_query_item item = {
1156 .query_id = DRM_I915_QUERY_TOPOLOGY_INFO,
1157 };
1158 struct drm_i915_query query = {
1159 .num_items = 1,
1160 .items_ptr = (uintptr_t) &item,
1161 };
1162
1163 /* kernel 4.17+ supports the query */
1164 return drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query) == 0;
1165 }
1166
1167 if (devinfo->gen >= 8) {
1168 /* 4.13+ api required for gen8 - gen9 */
1169 int mask;
1170 struct drm_i915_getparam gp = {
1171 .param = I915_PARAM_SLICE_MASK,
1172 .value = &mask,
1173 };
1174 /* kernel 4.13+ supports this parameter */
1175 return drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == 0;
1176 }
1177
1178 if (devinfo->gen == 7)
1179 /* default topology values are correct for HSW */
1180 return true;
1181
1182 /* oa not supported before gen 7*/
1183 return false;
1184 }
1185
1186 static void *
1187 brw_oa_bo_alloc(void *bufmgr, const char *name, uint64_t size)
1188 {
1189 return brw_bo_alloc(bufmgr, name, size, BRW_MEMZONE_OTHER);
1190 }
1191
1192 static void
1193 brw_oa_emit_mi_report_perf_count(void *c,
1194 void *bo,
1195 uint32_t offset_in_bytes,
1196 uint32_t report_id)
1197 {
1198 struct brw_context *ctx = c;
1199 ctx->vtbl.emit_mi_report_perf_count(ctx,
1200 bo,
1201 offset_in_bytes,
1202 report_id);
1203 }
1204
1205 typedef void (*bo_unreference_t)(void *);
1206 typedef void *(*bo_map_t)(void *, void *, unsigned flags);
1207 typedef void (*bo_unmap_t)(void *);
1208 typedef void (* emit_mi_report_t)(void *, void *, uint32_t, uint32_t);
1209 typedef void (*emit_mi_flush_t)(void *);
1210
1211 static void
1212 brw_oa_batchbuffer_flush(void *c, const char *file, int line)
1213 {
1214 struct brw_context *ctx = c;
1215 _intel_batchbuffer_flush_fence(ctx, -1, NULL, file, line);
1216 }
1217
1218 typedef void (*capture_frequency_stat_register_t)(void *, void *, uint32_t );
1219 typedef void (*store_register_mem64_t)(void *ctx, void *bo,
1220 uint32_t reg, uint32_t offset);
1221
1222 static unsigned
1223 brw_init_perf_query_info(struct gl_context *ctx)
1224 {
1225 struct brw_context *brw = brw_context(ctx);
1226 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1227
1228 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
1229 if (perf_ctx->perf)
1230 return perf_ctx->perf->n_queries;
1231
1232 perf_ctx->perf = gen_perf_new(brw);
1233 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1234
1235 perf_cfg->vtbl.bo_alloc = brw_oa_bo_alloc;
1236 perf_cfg->vtbl.bo_unreference = (bo_unreference_t)brw_bo_unreference;
1237 perf_cfg->vtbl.bo_map = (bo_map_t)brw_bo_map;
1238 perf_cfg->vtbl.bo_unmap = (bo_unmap_t)brw_bo_unmap;
1239 perf_cfg->vtbl.emit_mi_flush = (emit_mi_flush_t)brw_emit_mi_flush;
1240 perf_cfg->vtbl.emit_mi_report_perf_count =
1241 (emit_mi_report_t)brw_oa_emit_mi_report_perf_count;
1242 perf_cfg->vtbl.batchbuffer_flush = brw_oa_batchbuffer_flush;
1243 perf_cfg->vtbl.capture_frequency_stat_register =
1244 (capture_frequency_stat_register_t) capture_frequency_stat_register;
1245 perf_cfg->vtbl.store_register_mem64 =
1246 (store_register_mem64_t) brw_store_register_mem64;
1247
1248 gen_perf_init_context(perf_ctx, perf_cfg, brw, brw->bufmgr, devinfo,
1249 brw->hw_ctx, brw->screen->driScrnPriv->fd);
1250
1251 init_pipeline_statistic_query_registers(brw);
1252 gen_perf_query_register_mdapi_statistic_query(devinfo, perf_cfg);
1253
1254 if ((oa_metrics_kernel_support(perf_ctx->drm_fd, devinfo)) &&
1255 (gen_perf_load_oa_metrics(perf_cfg, perf_ctx->drm_fd, devinfo)))
1256 gen_perf_query_register_mdapi_oa_query(devinfo, perf_cfg);
1257
1258 return perf_cfg->n_queries;
1259 }
1260
1261 void
1262 brw_init_performance_queries(struct brw_context *brw)
1263 {
1264 struct gl_context *ctx = &brw->ctx;
1265
1266 ctx->Driver.InitPerfQueryInfo = brw_init_perf_query_info;
1267 ctx->Driver.GetPerfQueryInfo = brw_get_perf_query_info;
1268 ctx->Driver.GetPerfCounterInfo = brw_get_perf_counter_info;
1269 ctx->Driver.NewPerfQueryObject = brw_new_perf_query_object;
1270 ctx->Driver.DeletePerfQuery = brw_delete_perf_query;
1271 ctx->Driver.BeginPerfQuery = brw_begin_perf_query;
1272 ctx->Driver.EndPerfQuery = brw_end_perf_query;
1273 ctx->Driver.WaitPerfQuery = brw_wait_perf_query;
1274 ctx->Driver.IsPerfQueryReady = brw_is_perf_query_ready;
1275 ctx->Driver.GetPerfQueryData = brw_get_perf_query_data;
1276 }