60fd607c1ecbc0127f28923f1235824f7b9ce87a
[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 * Add a query to the global list of "unaccumulated queries."
249 *
250 * Queries are tracked here until all the associated OA reports have
251 * been accumulated via accumulate_oa_reports() after the end
252 * MI_REPORT_PERF_COUNT has landed in query->oa.bo.
253 */
254 static void
255 add_to_unaccumulated_query_list(struct brw_context *brw,
256 struct gen_perf_query_object *obj)
257 {
258 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
259 if (perf_ctx->unaccumulated_elements >=
260 perf_ctx->unaccumulated_array_size)
261 {
262 perf_ctx->unaccumulated_array_size *= 1.5;
263 perf_ctx->unaccumulated =
264 reralloc(brw, perf_ctx->unaccumulated,
265 struct gen_perf_query_object *,
266 perf_ctx->unaccumulated_array_size);
267 }
268
269 perf_ctx->unaccumulated[perf_ctx->unaccumulated_elements++] = obj;
270 }
271
272 /**
273 * Remove a query from the global list of unaccumulated queries once
274 * after successfully accumulating the OA reports associated with the
275 * query in accumulate_oa_reports() or when discarding unwanted query
276 * results.
277 */
278 static void
279 drop_from_unaccumulated_query_list(struct brw_context *brw,
280 struct gen_perf_query_object *obj)
281 {
282 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
283 for (int i = 0; i < perf_ctx->unaccumulated_elements; i++) {
284 if (perf_ctx->unaccumulated[i] == obj) {
285 int last_elt = --perf_ctx->unaccumulated_elements;
286
287 if (i == last_elt)
288 perf_ctx->unaccumulated[i] = NULL;
289 else {
290 perf_ctx->unaccumulated[i] =
291 perf_ctx->unaccumulated[last_elt];
292 }
293
294 break;
295 }
296 }
297
298 /* Drop our samples_head reference so that associated periodic
299 * sample data buffers can potentially be reaped if they aren't
300 * referenced by any other queries...
301 */
302
303 struct oa_sample_buf *buf =
304 exec_node_data(struct oa_sample_buf, obj->oa.samples_head, link);
305
306 assert(buf->refcount > 0);
307 buf->refcount--;
308
309 obj->oa.samples_head = NULL;
310
311 gen_perf_reap_old_sample_buffers(&brw->perf_ctx);
312 }
313
314 /* In general if we see anything spurious while accumulating results,
315 * we don't try and continue accumulating the current query, hoping
316 * for the best, we scrap anything outstanding, and then hope for the
317 * best with new queries.
318 */
319 static void
320 discard_all_queries(struct brw_context *brw)
321 {
322 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
323 while (perf_ctx->unaccumulated_elements) {
324 struct gen_perf_query_object *obj = perf_ctx->unaccumulated[0];
325
326 obj->oa.results_accumulated = true;
327 drop_from_unaccumulated_query_list(brw, perf_ctx->unaccumulated[0]);
328
329 gen_perf_dec_n_users(perf_ctx);
330 }
331 }
332
333 enum OaReadStatus {
334 OA_READ_STATUS_ERROR,
335 OA_READ_STATUS_UNFINISHED,
336 OA_READ_STATUS_FINISHED,
337 };
338
339 static enum OaReadStatus
340 read_oa_samples_until(struct brw_context *brw,
341 uint32_t start_timestamp,
342 uint32_t end_timestamp)
343 {
344 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
345 struct exec_node *tail_node =
346 exec_list_get_tail(&perf_ctx->sample_buffers);
347 struct oa_sample_buf *tail_buf =
348 exec_node_data(struct oa_sample_buf, tail_node, link);
349 uint32_t last_timestamp = tail_buf->last_timestamp;
350
351 while (1) {
352 struct oa_sample_buf *buf = gen_perf_get_free_sample_buf(perf_ctx);
353 uint32_t offset;
354 int len;
355
356 while ((len = read(perf_ctx->oa_stream_fd, buf->buf,
357 sizeof(buf->buf))) < 0 && errno == EINTR)
358 ;
359
360 if (len <= 0) {
361 exec_list_push_tail(&perf_ctx->free_sample_buffers, &buf->link);
362
363 if (len < 0) {
364 if (errno == EAGAIN)
365 return ((last_timestamp - start_timestamp) >=
366 (end_timestamp - start_timestamp)) ?
367 OA_READ_STATUS_FINISHED :
368 OA_READ_STATUS_UNFINISHED;
369 else {
370 DBG("Error reading i915 perf samples: %m\n");
371 }
372 } else
373 DBG("Spurious EOF reading i915 perf samples\n");
374
375 return OA_READ_STATUS_ERROR;
376 }
377
378 buf->len = len;
379 exec_list_push_tail(&perf_ctx->sample_buffers, &buf->link);
380
381 /* Go through the reports and update the last timestamp. */
382 offset = 0;
383 while (offset < buf->len) {
384 const struct drm_i915_perf_record_header *header =
385 (const struct drm_i915_perf_record_header *) &buf->buf[offset];
386 uint32_t *report = (uint32_t *) (header + 1);
387
388 if (header->type == DRM_I915_PERF_RECORD_SAMPLE)
389 last_timestamp = report[1];
390
391 offset += header->size;
392 }
393
394 buf->last_timestamp = last_timestamp;
395 }
396
397 unreachable("not reached");
398 return OA_READ_STATUS_ERROR;
399 }
400
401 /**
402 * Try to read all the reports until either the delimiting timestamp
403 * or an error arises.
404 */
405 static bool
406 read_oa_samples_for_query(struct brw_context *brw,
407 struct gen_perf_query_object *obj)
408 {
409 uint32_t *start;
410 uint32_t *last;
411 uint32_t *end;
412
413 /* We need the MI_REPORT_PERF_COUNT to land before we can start
414 * accumulate. */
415 assert(!brw_batch_references(&brw->batch, obj->oa.bo) &&
416 !brw_bo_busy(obj->oa.bo));
417
418 /* Map the BO once here and let accumulate_oa_reports() unmap
419 * it. */
420 if (obj->oa.map == NULL)
421 obj->oa.map = brw->perf_ctx.perf->vtbl.bo_map(brw, obj->oa.bo, MAP_READ);
422
423 start = last = obj->oa.map;
424 end = obj->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
425
426 if (start[0] != obj->oa.begin_report_id) {
427 DBG("Spurious start report id=%"PRIu32"\n", start[0]);
428 return true;
429 }
430 if (end[0] != (obj->oa.begin_report_id + 1)) {
431 DBG("Spurious end report id=%"PRIu32"\n", end[0]);
432 return true;
433 }
434
435 /* Read the reports until the end timestamp. */
436 switch (read_oa_samples_until(brw, start[1], end[1])) {
437 case OA_READ_STATUS_ERROR:
438 /* Fallthrough and let accumulate_oa_reports() deal with the
439 * error. */
440 case OA_READ_STATUS_FINISHED:
441 return true;
442 case OA_READ_STATUS_UNFINISHED:
443 return false;
444 }
445
446 unreachable("invalid read status");
447 return false;
448 }
449
450 /**
451 * Accumulate raw OA counter values based on deltas between pairs of
452 * OA reports.
453 *
454 * Accumulation starts from the first report captured via
455 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
456 * last MI_RPC report requested by brw_end_perf_query(). Between these
457 * two reports there may also some number of periodically sampled OA
458 * reports collected via the i915 perf interface - depending on the
459 * duration of the query.
460 *
461 * These periodic snapshots help to ensure we handle counter overflow
462 * correctly by being frequent enough to ensure we don't miss multiple
463 * overflows of a counter between snapshots. For Gen8+ the i915 perf
464 * snapshots provide the extra context-switch reports that let us
465 * subtract out the progress of counters associated with other
466 * contexts running on the system.
467 */
468 static void
469 accumulate_oa_reports(struct brw_context *brw,
470 struct brw_perf_query_object *brw_query)
471 {
472 const struct gen_device_info *devinfo = &brw->screen->devinfo;
473 struct gen_perf_query_object *obj = brw_query->query;
474 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
475 uint32_t *start;
476 uint32_t *last;
477 uint32_t *end;
478 struct exec_node *first_samples_node;
479 bool in_ctx = true;
480 int out_duration = 0;
481
482 assert(brw_query->base.Ready);
483 assert(obj->oa.map != NULL);
484
485 start = last = obj->oa.map;
486 end = obj->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
487
488 if (start[0] != obj->oa.begin_report_id) {
489 DBG("Spurious start report id=%"PRIu32"\n", start[0]);
490 goto error;
491 }
492 if (end[0] != (obj->oa.begin_report_id + 1)) {
493 DBG("Spurious end report id=%"PRIu32"\n", end[0]);
494 goto error;
495 }
496
497 /* See if we have any periodic reports to accumulate too... */
498
499 /* N.B. The oa.samples_head was set when the query began and
500 * pointed to the tail of the perf_ctx->sample_buffers list at
501 * the time the query started. Since the buffer existed before the
502 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
503 * that no data in this particular node's buffer can possibly be
504 * associated with the query - so skip ahead one...
505 */
506 first_samples_node = obj->oa.samples_head->next;
507
508 foreach_list_typed_from(struct oa_sample_buf, buf, link,
509 &brw->perf_ctx.sample_buffers,
510 first_samples_node)
511 {
512 int offset = 0;
513
514 while (offset < buf->len) {
515 const struct drm_i915_perf_record_header *header =
516 (const struct drm_i915_perf_record_header *)(buf->buf + offset);
517
518 assert(header->size != 0);
519 assert(header->size <= buf->len);
520
521 offset += header->size;
522
523 switch (header->type) {
524 case DRM_I915_PERF_RECORD_SAMPLE: {
525 uint32_t *report = (uint32_t *)(header + 1);
526 bool add = true;
527
528 /* Ignore reports that come before the start marker.
529 * (Note: takes care to allow overflow of 32bit timestamps)
530 */
531 if (gen_device_info_timebase_scale(devinfo,
532 report[1] - start[1]) > 5000000000) {
533 continue;
534 }
535
536 /* Ignore reports that come after the end marker.
537 * (Note: takes care to allow overflow of 32bit timestamps)
538 */
539 if (gen_device_info_timebase_scale(devinfo,
540 report[1] - end[1]) <= 5000000000) {
541 goto end;
542 }
543
544 /* For Gen8+ since the counters continue while other
545 * contexts are running we need to discount any unrelated
546 * deltas. The hardware automatically generates a report
547 * on context switch which gives us a new reference point
548 * to continuing adding deltas from.
549 *
550 * For Haswell we can rely on the HW to stop the progress
551 * of OA counters while any other context is acctive.
552 */
553 if (devinfo->gen >= 8) {
554 if (in_ctx && report[2] != obj->oa.result.hw_id) {
555 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
556 in_ctx = false;
557 out_duration = 0;
558 } else if (in_ctx == false && report[2] == obj->oa.result.hw_id) {
559 DBG("i915 perf: Switch TO\n");
560 in_ctx = true;
561
562 /* From experimentation in IGT, we found that the OA unit
563 * might label some report as "idle" (using an invalid
564 * context ID), right after a report for a given context.
565 * Deltas generated by those reports actually belong to the
566 * previous context, even though they're not labelled as
567 * such.
568 *
569 * We didn't *really* Switch AWAY in the case that we e.g.
570 * saw a single periodic report while idle...
571 */
572 if (out_duration >= 1)
573 add = false;
574 } else if (in_ctx) {
575 assert(report[2] == obj->oa.result.hw_id);
576 DBG("i915 perf: Continuation IN\n");
577 } else {
578 assert(report[2] != obj->oa.result.hw_id);
579 DBG("i915 perf: Continuation OUT\n");
580 add = false;
581 out_duration++;
582 }
583 }
584
585 if (add) {
586 gen_perf_query_result_accumulate(&obj->oa.result, obj->queryinfo,
587 last, report);
588 }
589
590 last = report;
591
592 break;
593 }
594
595 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST:
596 DBG("i915 perf: OA error: all reports lost\n");
597 goto error;
598 case DRM_I915_PERF_RECORD_OA_REPORT_LOST:
599 DBG("i915 perf: OA report lost\n");
600 break;
601 }
602 }
603 }
604
605 end:
606
607 gen_perf_query_result_accumulate(&obj->oa.result, obj->queryinfo,
608 last, end);
609
610 DBG("Marking %d accumulated - results gathered\n", brw_query->base.Id);
611
612 obj->oa.results_accumulated = true;
613 drop_from_unaccumulated_query_list(brw, obj);
614 gen_perf_dec_n_users(perf_ctx);
615
616 return;
617
618 error:
619
620 discard_all_queries(brw);
621 }
622
623 /******************************************************************************/
624
625 static void
626 capture_frequency_stat_register(struct brw_context *brw,
627 struct brw_bo *bo,
628 uint32_t bo_offset)
629 {
630 const struct gen_device_info *devinfo = &brw->screen->devinfo;
631
632 if (devinfo->gen >= 7 && devinfo->gen <= 8 &&
633 !devinfo->is_baytrail && !devinfo->is_cherryview) {
634 brw_store_register_mem32(brw, bo, GEN7_RPSTAT1, bo_offset);
635 } else if (devinfo->gen >= 9) {
636 brw_store_register_mem32(brw, bo, GEN9_RPSTAT0, bo_offset);
637 }
638 }
639
640 /**
641 * Driver hook for glBeginPerfQueryINTEL().
642 */
643 static bool
644 brw_begin_perf_query(struct gl_context *ctx,
645 struct gl_perf_query_object *o)
646 {
647 struct brw_context *brw = brw_context(ctx);
648 struct brw_perf_query_object *brw_query = brw_perf_query(o);
649 struct gen_perf_query_object *obj = brw_query->query;
650 const struct gen_perf_query_info *query = obj->queryinfo;
651 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
652 struct gen_perf_config *perf_cfg = perf_ctx->perf;
653
654 /* We can assume the frontend hides mistaken attempts to Begin a
655 * query object multiple times before its End. Similarly if an
656 * application reuses a query object before results have arrived
657 * the frontend will wait for prior results so we don't need
658 * to support abandoning in-flight results.
659 */
660 assert(!o->Active);
661 assert(!o->Used || o->Ready); /* no in-flight query to worry about */
662
663 DBG("Begin(%d)\n", o->Id);
664
665 /* XXX: We have to consider that the command parser unit that parses batch
666 * buffer commands and is used to capture begin/end counter snapshots isn't
667 * implicitly synchronized with what's currently running across other GPU
668 * units (such as the EUs running shaders) that the performance counters are
669 * associated with.
670 *
671 * The intention of performance queries is to measure the work associated
672 * with commands between the begin/end delimiters and so for that to be the
673 * case we need to explicitly synchronize the parsing of commands to capture
674 * Begin/End counter snapshots with what's running across other parts of the
675 * GPU.
676 *
677 * When the command parser reaches a Begin marker it effectively needs to
678 * drain everything currently running on the GPU until the hardware is idle
679 * before capturing the first snapshot of counters - otherwise the results
680 * would also be measuring the effects of earlier commands.
681 *
682 * When the command parser reaches an End marker it needs to stall until
683 * everything currently running on the GPU has finished before capturing the
684 * end snapshot - otherwise the results won't be a complete representation
685 * of the work.
686 *
687 * Theoretically there could be opportunities to minimize how much of the
688 * GPU pipeline is drained, or that we stall for, when we know what specific
689 * units the performance counters being queried relate to but we don't
690 * currently attempt to be clever here.
691 *
692 * Note: with our current simple approach here then for back-to-back queries
693 * we will redundantly emit duplicate commands to synchronize the command
694 * streamer with the rest of the GPU pipeline, but we assume that in HW the
695 * second synchronization is effectively a NOOP.
696 *
697 * N.B. The final results are based on deltas of counters between (inside)
698 * Begin/End markers so even though the total wall clock time of the
699 * workload is stretched by larger pipeline bubbles the bubbles themselves
700 * are generally invisible to the query results. Whether that's a good or a
701 * bad thing depends on the use case. For a lower real-time impact while
702 * capturing metrics then periodic sampling may be a better choice than
703 * INTEL_performance_query.
704 *
705 *
706 * This is our Begin synchronization point to drain current work on the
707 * GPU before we capture our first counter snapshot...
708 */
709 perf_cfg->vtbl.emit_mi_flush(brw);
710
711 switch (query->kind) {
712 case GEN_PERF_QUERY_TYPE_OA:
713 case GEN_PERF_QUERY_TYPE_RAW: {
714
715 /* Opening an i915 perf stream implies exclusive access to the OA unit
716 * which will generate counter reports for a specific counter set with a
717 * specific layout/format so we can't begin any OA based queries that
718 * require a different counter set or format unless we get an opportunity
719 * to close the stream and open a new one...
720 */
721 uint64_t metric_id = gen_perf_query_get_metric_id(perf_ctx->perf, query);
722
723 if (perf_ctx->oa_stream_fd != -1 &&
724 perf_ctx->current_oa_metrics_set_id != metric_id) {
725
726 if (perf_ctx->n_oa_users != 0) {
727 DBG("WARNING: Begin(%d) failed already using perf config=%i/%"PRIu64"\n",
728 o->Id, perf_ctx->current_oa_metrics_set_id, metric_id);
729 return false;
730 } else
731 gen_perf_close(perf_ctx, query);
732 }
733
734 /* If the OA counters aren't already on, enable them. */
735
736 if (perf_ctx->oa_stream_fd == -1) {
737 __DRIscreen *screen = brw->screen->driScrnPriv;
738 const struct gen_device_info *devinfo = &brw->screen->devinfo;
739
740 /* The period_exponent gives a sampling period as follows:
741 * sample_period = timestamp_period * 2^(period_exponent + 1)
742 *
743 * The timestamps increments every 80ns (HSW), ~52ns (GEN9LP) or
744 * ~83ns (GEN8/9).
745 *
746 * The counter overflow period is derived from the EuActive counter
747 * which reads a counter that increments by the number of clock
748 * cycles multiplied by the number of EUs. It can be calculated as:
749 *
750 * 2^(number of bits in A counter) / (n_eus * max_gen_freq * 2)
751 *
752 * (E.g. 40 EUs @ 1GHz = ~53ms)
753 *
754 * We select a sampling period inferior to that overflow period to
755 * ensure we cannot see more than 1 counter overflow, otherwise we
756 * could loose information.
757 */
758
759 int a_counter_in_bits = 32;
760 if (devinfo->gen >= 8)
761 a_counter_in_bits = 40;
762
763 uint64_t overflow_period = pow(2, a_counter_in_bits) /
764 (perf_cfg->sys_vars.n_eus *
765 /* drop 1GHz freq to have units in nanoseconds */
766 2);
767
768 DBG("A counter overflow period: %"PRIu64"ns, %"PRIu64"ms (n_eus=%"PRIu64")\n",
769 overflow_period, overflow_period / 1000000ul, perf_cfg->sys_vars.n_eus);
770
771 int period_exponent = 0;
772 uint64_t prev_sample_period, next_sample_period;
773 for (int e = 0; e < 30; e++) {
774 prev_sample_period = 1000000000ull * pow(2, e + 1) / devinfo->timestamp_frequency;
775 next_sample_period = 1000000000ull * pow(2, e + 2) / devinfo->timestamp_frequency;
776
777 /* Take the previous sampling period, lower than the overflow
778 * period.
779 */
780 if (prev_sample_period < overflow_period &&
781 next_sample_period > overflow_period)
782 period_exponent = e + 1;
783 }
784
785 if (period_exponent == 0) {
786 DBG("WARNING: enable to find a sampling exponent\n");
787 return false;
788 }
789
790 DBG("OA sampling exponent: %i ~= %"PRIu64"ms\n", period_exponent,
791 prev_sample_period / 1000000ul);
792
793 if (!gen_perf_open(perf_ctx, metric_id, query->oa_format,
794 period_exponent, screen->fd, brw->hw_ctx))
795 return false;
796 } else {
797 assert(perf_ctx->current_oa_metrics_set_id == metric_id &&
798 perf_ctx->current_oa_format == query->oa_format);
799 }
800
801 if (!gen_perf_inc_n_users(perf_ctx)) {
802 DBG("WARNING: Error enabling i915 perf stream: %m\n");
803 return false;
804 }
805
806 if (obj->oa.bo) {
807 perf_cfg->vtbl.bo_unreference(obj->oa.bo);
808 obj->oa.bo = NULL;
809 }
810
811 obj->oa.bo =
812 brw->perf_ctx.perf->vtbl.bo_alloc(brw->bufmgr,
813 "perf. query OA MI_RPC bo",
814 MI_RPC_BO_SIZE);
815 #ifdef DEBUG
816 /* Pre-filling the BO helps debug whether writes landed. */
817 void *map = brw->perf_ctx.perf->vtbl.bo_map(brw, obj->oa.bo, MAP_WRITE);
818 memset(map, 0x80, MI_RPC_BO_SIZE);
819 brw->perf_ctx.perf->vtbl.bo_unmap(obj->oa.bo);
820 #endif
821
822 obj->oa.begin_report_id = perf_ctx->next_query_start_report_id;
823 perf_ctx->next_query_start_report_id += 2;
824
825 /* We flush the batchbuffer here to minimize the chances that MI_RPC
826 * delimiting commands end up in different batchbuffers. If that's the
827 * case, the measurement will include the time it takes for the kernel
828 * scheduler to load a new request into the hardware. This is manifested in
829 * tools like frameretrace by spikes in the "GPU Core Clocks" counter.
830 */
831 perf_cfg->vtbl.batchbuffer_flush(brw, __FILE__, __LINE__);
832
833 /* Take a starting OA counter snapshot. */
834 perf_cfg->vtbl.emit_mi_report_perf_count(brw, obj->oa.bo, 0,
835 obj->oa.begin_report_id);
836 perf_cfg->vtbl.capture_frequency_stat_register(brw, obj->oa.bo,
837 MI_FREQ_START_OFFSET_BYTES);
838
839 ++perf_ctx->n_active_oa_queries;
840
841 /* No already-buffered samples can possibly be associated with this query
842 * so create a marker within the list of sample buffers enabling us to
843 * easily ignore earlier samples when processing this query after
844 * completion.
845 */
846 assert(!exec_list_is_empty(&perf_ctx->sample_buffers));
847 obj->oa.samples_head = exec_list_get_tail(&perf_ctx->sample_buffers);
848
849 struct oa_sample_buf *buf =
850 exec_node_data(struct oa_sample_buf, obj->oa.samples_head, link);
851
852 /* This reference will ensure that future/following sample
853 * buffers (that may relate to this query) can't be freed until
854 * this drops to zero.
855 */
856 buf->refcount++;
857
858 gen_perf_query_result_clear(&obj->oa.result);
859 obj->oa.results_accumulated = false;
860
861 add_to_unaccumulated_query_list(brw, obj);
862 break;
863 }
864
865 case GEN_PERF_QUERY_TYPE_PIPELINE:
866 if (obj->pipeline_stats.bo) {
867 brw->perf_ctx.perf->vtbl.bo_unreference(obj->pipeline_stats.bo);
868 obj->pipeline_stats.bo = NULL;
869 }
870
871 obj->pipeline_stats.bo =
872 brw->perf_ctx.perf->vtbl.bo_alloc(brw->bufmgr,
873 "perf. query pipeline stats bo",
874 STATS_BO_SIZE);
875
876 /* Take starting snapshots. */
877 gen_perf_snapshot_statistics_registers(brw, perf_cfg, obj, 0);
878
879 ++perf_ctx->n_active_pipeline_stats_queries;
880 break;
881
882 default:
883 unreachable("Unknown query type");
884 break;
885 }
886
887 if (INTEL_DEBUG & DEBUG_PERFMON)
888 dump_perf_queries(brw);
889
890 return true;
891 }
892
893 /**
894 * Driver hook for glEndPerfQueryINTEL().
895 */
896 static void
897 brw_end_perf_query(struct gl_context *ctx,
898 struct gl_perf_query_object *o)
899 {
900 struct brw_context *brw = brw_context(ctx);
901 struct brw_perf_query_object *brw_query = brw_perf_query(o);
902 struct gen_perf_query_object *obj = brw_query->query;
903 struct gen_perf_config *perf_cfg = brw->perf_ctx.perf;
904 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
905
906 DBG("End(%d)\n", o->Id);
907
908 /* Ensure that the work associated with the queried commands will have
909 * finished before taking our query end counter readings.
910 *
911 * For more details see comment in brw_begin_perf_query for
912 * corresponding flush.
913 */
914 perf_cfg->vtbl.emit_mi_flush(brw);
915
916 switch (obj->queryinfo->kind) {
917 case GEN_PERF_QUERY_TYPE_OA:
918 case GEN_PERF_QUERY_TYPE_RAW:
919
920 /* NB: It's possible that the query will have already been marked
921 * as 'accumulated' if an error was seen while reading samples
922 * from perf. In this case we mustn't try and emit a closing
923 * MI_RPC command in case the OA unit has already been disabled
924 */
925 if (!obj->oa.results_accumulated) {
926 /* Take an ending OA counter snapshot. */
927 perf_cfg->vtbl.capture_frequency_stat_register(brw, obj->oa.bo,
928 MI_FREQ_END_OFFSET_BYTES);
929 brw->vtbl.emit_mi_report_perf_count(brw, obj->oa.bo,
930 MI_RPC_BO_END_OFFSET_BYTES,
931 obj->oa.begin_report_id + 1);
932 }
933
934 --perf_ctx->n_active_oa_queries;
935
936 /* NB: even though the query has now ended, it can't be accumulated
937 * until the end MI_REPORT_PERF_COUNT snapshot has been written
938 * to query->oa.bo
939 */
940 break;
941
942 case GEN_PERF_QUERY_TYPE_PIPELINE:
943 gen_perf_snapshot_statistics_registers(brw, perf_cfg, obj,
944 STATS_BO_END_OFFSET_BYTES);
945 --perf_ctx->n_active_pipeline_stats_queries;
946 break;
947
948 default:
949 unreachable("Unknown query type");
950 break;
951 }
952 }
953
954 static void
955 brw_wait_perf_query(struct gl_context *ctx, struct gl_perf_query_object *o)
956 {
957 struct brw_context *brw = brw_context(ctx);
958 struct brw_perf_query_object *brw_query = brw_perf_query(o);
959 struct gen_perf_query_object *obj = brw_query->query;
960 struct brw_bo *bo = NULL;
961 struct gen_perf_config *perf_cfg = brw->perf_ctx.perf;
962
963 assert(!o->Ready);
964
965 switch (obj->queryinfo->kind) {
966 case GEN_PERF_QUERY_TYPE_OA:
967 case GEN_PERF_QUERY_TYPE_RAW:
968 bo = obj->oa.bo;
969 break;
970
971 case GEN_PERF_QUERY_TYPE_PIPELINE:
972 bo = obj->pipeline_stats.bo;
973 break;
974
975 default:
976 unreachable("Unknown query type");
977 break;
978 }
979
980 if (bo == NULL)
981 return;
982
983 /* If the current batch references our results bo then we need to
984 * flush first...
985 */
986 if (brw_batch_references(&brw->batch, bo))
987 perf_cfg->vtbl.batchbuffer_flush(brw, __FILE__, __LINE__);
988
989 brw_bo_wait_rendering(bo);
990
991 /* Due to a race condition between the OA unit signaling report
992 * availability and the report actually being written into memory,
993 * we need to wait for all the reports to come in before we can
994 * read them.
995 */
996 if (obj->queryinfo->kind == GEN_PERF_QUERY_TYPE_OA ||
997 obj->queryinfo->kind == GEN_PERF_QUERY_TYPE_RAW) {
998 while (!read_oa_samples_for_query(brw, obj))
999 ;
1000 }
1001 }
1002
1003 static bool
1004 brw_is_perf_query_ready(struct gl_context *ctx,
1005 struct gl_perf_query_object *o)
1006 {
1007 struct brw_context *brw = brw_context(ctx);
1008 struct brw_perf_query_object *brw_query = brw_perf_query(o);
1009 struct gen_perf_query_object *obj = brw_query->query;
1010
1011 if (o->Ready)
1012 return true;
1013
1014 switch (obj->queryinfo->kind) {
1015 case GEN_PERF_QUERY_TYPE_OA:
1016 case GEN_PERF_QUERY_TYPE_RAW:
1017 return (obj->oa.results_accumulated ||
1018 (obj->oa.bo &&
1019 !brw_batch_references(&brw->batch, obj->oa.bo) &&
1020 !brw_bo_busy(obj->oa.bo) &&
1021 read_oa_samples_for_query(brw, obj)));
1022 case GEN_PERF_QUERY_TYPE_PIPELINE:
1023 return (obj->pipeline_stats.bo &&
1024 !brw_batch_references(&brw->batch, obj->pipeline_stats.bo) &&
1025 !brw_bo_busy(obj->pipeline_stats.bo));
1026
1027 default:
1028 unreachable("Unknown query type");
1029 break;
1030 }
1031
1032 return false;
1033 }
1034
1035 static void
1036 read_slice_unslice_frequencies(struct brw_context *brw,
1037 struct gen_perf_query_object *obj)
1038 {
1039 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1040 uint32_t *begin_report = obj->oa.map, *end_report = obj->oa.map + MI_RPC_BO_END_OFFSET_BYTES;
1041
1042 gen_perf_query_result_read_frequencies(&obj->oa.result,
1043 devinfo, begin_report, end_report);
1044 }
1045
1046 static void
1047 read_gt_frequency(struct brw_context *brw,
1048 struct gen_perf_query_object *obj)
1049 {
1050 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1051 uint32_t start = *((uint32_t *)(obj->oa.map + MI_FREQ_START_OFFSET_BYTES)),
1052 end = *((uint32_t *)(obj->oa.map + MI_FREQ_END_OFFSET_BYTES));
1053
1054 switch (devinfo->gen) {
1055 case 7:
1056 case 8:
1057 obj->oa.gt_frequency[0] = GET_FIELD(start, GEN7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
1058 obj->oa.gt_frequency[1] = GET_FIELD(end, GEN7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
1059 break;
1060 case 9:
1061 case 10:
1062 case 11:
1063 obj->oa.gt_frequency[0] = GET_FIELD(start, GEN9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
1064 obj->oa.gt_frequency[1] = GET_FIELD(end, GEN9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
1065 break;
1066 default:
1067 unreachable("unexpected gen");
1068 }
1069
1070 /* Put the numbers into Hz. */
1071 obj->oa.gt_frequency[0] *= 1000000ULL;
1072 obj->oa.gt_frequency[1] *= 1000000ULL;
1073 }
1074
1075 static int
1076 get_oa_counter_data(struct brw_context *brw,
1077 struct gen_perf_query_object *obj,
1078 size_t data_size,
1079 uint8_t *data)
1080 {
1081 struct gen_perf_config *perf = brw->perf_ctx.perf;
1082 const struct gen_perf_query_info *query = obj->queryinfo;
1083 int n_counters = query->n_counters;
1084 int written = 0;
1085
1086 for (int i = 0; i < n_counters; i++) {
1087 const struct gen_perf_query_counter *counter = &query->counters[i];
1088 uint64_t *out_uint64;
1089 float *out_float;
1090 size_t counter_size = gen_perf_query_counter_get_size(counter);
1091
1092 if (counter_size) {
1093 switch (counter->data_type) {
1094 case GEN_PERF_COUNTER_DATA_TYPE_UINT64:
1095 out_uint64 = (uint64_t *)(data + counter->offset);
1096 *out_uint64 =
1097 counter->oa_counter_read_uint64(perf, query,
1098 obj->oa.result.accumulator);
1099 break;
1100 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT:
1101 out_float = (float *)(data + counter->offset);
1102 *out_float =
1103 counter->oa_counter_read_float(perf, query,
1104 obj->oa.result.accumulator);
1105 break;
1106 default:
1107 /* So far we aren't using uint32, double or bool32... */
1108 unreachable("unexpected counter data type");
1109 }
1110 written = counter->offset + counter_size;
1111 }
1112 }
1113
1114 return written;
1115 }
1116
1117 static int
1118 get_pipeline_stats_data(struct brw_context *brw,
1119 struct gen_perf_query_object *obj,
1120 size_t data_size,
1121 uint8_t *data)
1122
1123 {
1124 const struct gen_perf_query_info *query = obj->queryinfo;
1125 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
1126 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1127 int n_counters = obj->queryinfo->n_counters;
1128 uint8_t *p = data;
1129
1130 uint64_t *start = perf_cfg->vtbl.bo_map(brw, obj->pipeline_stats.bo, MAP_READ);
1131 uint64_t *end = start + (STATS_BO_END_OFFSET_BYTES / sizeof(uint64_t));
1132
1133 for (int i = 0; i < n_counters; i++) {
1134 const struct gen_perf_query_counter *counter = &query->counters[i];
1135 uint64_t value = end[i] - start[i];
1136
1137 if (counter->pipeline_stat.numerator !=
1138 counter->pipeline_stat.denominator) {
1139 value *= counter->pipeline_stat.numerator;
1140 value /= counter->pipeline_stat.denominator;
1141 }
1142
1143 *((uint64_t *)p) = value;
1144 p += 8;
1145 }
1146
1147 perf_cfg->vtbl.bo_unmap(obj->pipeline_stats.bo);
1148
1149 return p - data;
1150 }
1151
1152 /**
1153 * Driver hook for glGetPerfQueryDataINTEL().
1154 */
1155 static void
1156 brw_get_perf_query_data(struct gl_context *ctx,
1157 struct gl_perf_query_object *o,
1158 GLsizei data_size,
1159 GLuint *data,
1160 GLuint *bytes_written)
1161 {
1162 struct brw_context *brw = brw_context(ctx);
1163 struct brw_perf_query_object *brw_query = brw_perf_query(o);
1164 struct gen_perf_query_object *obj = brw_query->query;
1165 int written = 0;
1166
1167 assert(brw_is_perf_query_ready(ctx, o));
1168
1169 DBG("GetData(%d)\n", o->Id);
1170
1171 if (INTEL_DEBUG & DEBUG_PERFMON)
1172 dump_perf_queries(brw);
1173
1174 /* We expect that the frontend only calls this hook when it knows
1175 * that results are available.
1176 */
1177 assert(o->Ready);
1178
1179 switch (obj->queryinfo->kind) {
1180 case GEN_PERF_QUERY_TYPE_OA:
1181 case GEN_PERF_QUERY_TYPE_RAW:
1182 if (!obj->oa.results_accumulated) {
1183 read_gt_frequency(brw, obj);
1184 read_slice_unslice_frequencies(brw, obj);
1185 accumulate_oa_reports(brw, brw_query);
1186 assert(obj->oa.results_accumulated);
1187
1188 brw->perf_ctx.perf->vtbl.bo_unmap(obj->oa.bo);
1189 obj->oa.map = NULL;
1190 }
1191 if (obj->queryinfo->kind == GEN_PERF_QUERY_TYPE_OA) {
1192 written = get_oa_counter_data(brw, obj, data_size, (uint8_t *)data);
1193 } else {
1194 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1195
1196 written = gen_perf_query_result_write_mdapi((uint8_t *)data, data_size,
1197 devinfo, &obj->oa.result,
1198 obj->oa.gt_frequency[0],
1199 obj->oa.gt_frequency[1]);
1200 }
1201 break;
1202
1203 case GEN_PERF_QUERY_TYPE_PIPELINE:
1204 written = get_pipeline_stats_data(brw, obj, data_size, (uint8_t *)data);
1205 break;
1206
1207 default:
1208 unreachable("Unknown query type");
1209 break;
1210 }
1211
1212 if (bytes_written)
1213 *bytes_written = written;
1214 }
1215
1216 static struct gl_perf_query_object *
1217 brw_new_perf_query_object(struct gl_context *ctx, unsigned query_index)
1218 {
1219 struct brw_context *brw = brw_context(ctx);
1220 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
1221 const struct gen_perf_query_info *queryinfo =
1222 &perf_ctx->perf->queries[query_index];
1223 struct gen_perf_query_object *obj =
1224 calloc(1, sizeof(struct gen_perf_query_object));
1225
1226 if (!obj)
1227 return NULL;
1228
1229 obj->queryinfo = queryinfo;
1230
1231 perf_ctx->n_query_instances++;
1232
1233 struct brw_perf_query_object *brw_query = calloc(1, sizeof(struct brw_perf_query_object));
1234 if (unlikely(!brw_query))
1235 return NULL;
1236 brw_query->query = obj;
1237 return &brw_query->base;
1238 }
1239
1240 /**
1241 * Driver hook for glDeletePerfQueryINTEL().
1242 */
1243 static void
1244 brw_delete_perf_query(struct gl_context *ctx,
1245 struct gl_perf_query_object *o)
1246 {
1247 struct brw_context *brw = brw_context(ctx);
1248 struct gen_perf_config *perf_cfg = brw->perf_ctx.perf;
1249 struct brw_perf_query_object *brw_query = brw_perf_query(o);
1250 struct gen_perf_query_object *obj = brw_query->query;
1251 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
1252
1253 /* We can assume that the frontend waits for a query to complete
1254 * before ever calling into here, so we don't have to worry about
1255 * deleting an in-flight query object.
1256 */
1257 assert(!o->Active);
1258 assert(!o->Used || o->Ready);
1259
1260 DBG("Delete(%d)\n", o->Id);
1261
1262 switch (obj->queryinfo->kind) {
1263 case GEN_PERF_QUERY_TYPE_OA:
1264 case GEN_PERF_QUERY_TYPE_RAW:
1265 if (obj->oa.bo) {
1266 if (!obj->oa.results_accumulated) {
1267 drop_from_unaccumulated_query_list(brw, obj);
1268 gen_perf_dec_n_users(perf_ctx);
1269 }
1270
1271 perf_cfg->vtbl.bo_unreference(obj->oa.bo);
1272 obj->oa.bo = NULL;
1273 }
1274
1275 obj->oa.results_accumulated = false;
1276 break;
1277
1278 case GEN_PERF_QUERY_TYPE_PIPELINE:
1279 if (obj->pipeline_stats.bo) {
1280 perf_cfg->vtbl.bo_unreference(obj->pipeline_stats.bo);
1281 obj->pipeline_stats.bo = NULL;
1282 }
1283 break;
1284
1285 default:
1286 unreachable("Unknown query type");
1287 break;
1288 }
1289
1290 /* As an indication that the INTEL_performance_query extension is no
1291 * longer in use, it's a good time to free our cache of sample
1292 * buffers and close any current i915-perf stream.
1293 */
1294 if (--perf_ctx->n_query_instances == 0) {
1295 gen_perf_free_sample_bufs(perf_ctx);
1296 gen_perf_close(perf_ctx, obj->queryinfo);
1297 }
1298
1299 free(obj);
1300 free(brw_query);
1301 }
1302
1303 /******************************************************************************/
1304
1305 static void
1306 init_pipeline_statistic_query_registers(struct brw_context *brw)
1307 {
1308 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1309 struct gen_perf_config *perf = brw->perf_ctx.perf;
1310 struct gen_perf_query_info *query =
1311 gen_perf_query_append_query_info(perf, MAX_STAT_COUNTERS);
1312
1313 query->kind = GEN_PERF_QUERY_TYPE_PIPELINE;
1314 query->name = "Pipeline Statistics Registers";
1315
1316 gen_perf_query_info_add_basic_stat_reg(query, IA_VERTICES_COUNT,
1317 "N vertices submitted");
1318 gen_perf_query_info_add_basic_stat_reg(query, IA_PRIMITIVES_COUNT,
1319 "N primitives submitted");
1320 gen_perf_query_info_add_basic_stat_reg(query, VS_INVOCATION_COUNT,
1321 "N vertex shader invocations");
1322
1323 if (devinfo->gen == 6) {
1324 gen_perf_query_info_add_stat_reg(query, GEN6_SO_PRIM_STORAGE_NEEDED, 1, 1,
1325 "SO_PRIM_STORAGE_NEEDED",
1326 "N geometry shader stream-out primitives (total)");
1327 gen_perf_query_info_add_stat_reg(query, GEN6_SO_NUM_PRIMS_WRITTEN, 1, 1,
1328 "SO_NUM_PRIMS_WRITTEN",
1329 "N geometry shader stream-out primitives (written)");
1330 } else {
1331 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
1332 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
1333 "N stream-out (stream 0) primitives (total)");
1334 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
1335 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
1336 "N stream-out (stream 1) primitives (total)");
1337 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
1338 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
1339 "N stream-out (stream 2) primitives (total)");
1340 gen_perf_query_info_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
1341 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
1342 "N stream-out (stream 3) primitives (total)");
1343 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
1344 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
1345 "N stream-out (stream 0) primitives (written)");
1346 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
1347 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
1348 "N stream-out (stream 1) primitives (written)");
1349 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
1350 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
1351 "N stream-out (stream 2) primitives (written)");
1352 gen_perf_query_info_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
1353 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
1354 "N stream-out (stream 3) primitives (written)");
1355 }
1356
1357 gen_perf_query_info_add_basic_stat_reg(query, HS_INVOCATION_COUNT,
1358 "N TCS shader invocations");
1359 gen_perf_query_info_add_basic_stat_reg(query, DS_INVOCATION_COUNT,
1360 "N TES shader invocations");
1361
1362 gen_perf_query_info_add_basic_stat_reg(query, GS_INVOCATION_COUNT,
1363 "N geometry shader invocations");
1364 gen_perf_query_info_add_basic_stat_reg(query, GS_PRIMITIVES_COUNT,
1365 "N geometry shader primitives emitted");
1366
1367 gen_perf_query_info_add_basic_stat_reg(query, CL_INVOCATION_COUNT,
1368 "N primitives entering clipping");
1369 gen_perf_query_info_add_basic_stat_reg(query, CL_PRIMITIVES_COUNT,
1370 "N primitives leaving clipping");
1371
1372 if (devinfo->is_haswell || devinfo->gen == 8) {
1373 gen_perf_query_info_add_stat_reg(query, PS_INVOCATION_COUNT, 1, 4,
1374 "N fragment shader invocations",
1375 "N fragment shader invocations");
1376 } else {
1377 gen_perf_query_info_add_basic_stat_reg(query, PS_INVOCATION_COUNT,
1378 "N fragment shader invocations");
1379 }
1380
1381 gen_perf_query_info_add_basic_stat_reg(query, PS_DEPTH_COUNT,
1382 "N z-pass fragments");
1383
1384 if (devinfo->gen >= 7) {
1385 gen_perf_query_info_add_basic_stat_reg(query, CS_INVOCATION_COUNT,
1386 "N compute shader invocations");
1387 }
1388
1389 query->data_size = sizeof(uint64_t) * query->n_counters;
1390 }
1391
1392 /* gen_device_info will have incorrect default topology values for unsupported kernels.
1393 * verify kernel support to ensure OA metrics are accurate.
1394 */
1395 static bool
1396 oa_metrics_kernel_support(int fd, const struct gen_device_info *devinfo)
1397 {
1398 if (devinfo->gen >= 10) {
1399 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
1400 * to verify support
1401 */
1402 struct drm_i915_query_item item = {
1403 .query_id = DRM_I915_QUERY_TOPOLOGY_INFO,
1404 };
1405 struct drm_i915_query query = {
1406 .num_items = 1,
1407 .items_ptr = (uintptr_t) &item,
1408 };
1409
1410 /* kernel 4.17+ supports the query */
1411 return drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query) == 0;
1412 }
1413
1414 if (devinfo->gen >= 8) {
1415 /* 4.13+ api required for gen8 - gen9 */
1416 int mask;
1417 struct drm_i915_getparam gp = {
1418 .param = I915_PARAM_SLICE_MASK,
1419 .value = &mask,
1420 };
1421 /* kernel 4.13+ supports this parameter */
1422 return drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == 0;
1423 }
1424
1425 if (devinfo->gen == 7)
1426 /* default topology values are correct for HSW */
1427 return true;
1428
1429 /* oa not supported before gen 7*/
1430 return false;
1431 }
1432
1433 static void *
1434 brw_oa_bo_alloc(void *bufmgr, const char *name, uint64_t size)
1435 {
1436 return brw_bo_alloc(bufmgr, name, size, BRW_MEMZONE_OTHER);
1437 }
1438
1439 static void
1440 brw_oa_emit_mi_report_perf_count(void *c,
1441 void *bo,
1442 uint32_t offset_in_bytes,
1443 uint32_t report_id)
1444 {
1445 struct brw_context *ctx = c;
1446 ctx->vtbl.emit_mi_report_perf_count(ctx,
1447 bo,
1448 offset_in_bytes,
1449 report_id);
1450 }
1451
1452 typedef void (*bo_unreference_t)(void *);
1453 typedef void *(*bo_map_t)(void *, void *, unsigned flags);
1454 typedef void (*bo_unmap_t)(void *);
1455 typedef void (* emit_mi_report_t)(void *, void *, uint32_t, uint32_t);
1456 typedef void (*emit_mi_flush_t)(void *);
1457
1458 static void
1459 brw_oa_batchbuffer_flush(void *c, const char *file, int line)
1460 {
1461 struct brw_context *ctx = c;
1462 _intel_batchbuffer_flush_fence(ctx, -1, NULL, file, line);
1463 }
1464
1465 typedef void (*capture_frequency_stat_register_t)(void *, void *, uint32_t );
1466 typedef void (*store_register_mem64_t)(void *ctx, void *bo,
1467 uint32_t reg, uint32_t offset);
1468
1469 static unsigned
1470 brw_init_perf_query_info(struct gl_context *ctx)
1471 {
1472 struct brw_context *brw = brw_context(ctx);
1473 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1474 __DRIscreen *screen = brw->screen->driScrnPriv;
1475
1476 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
1477 if (perf_ctx->perf)
1478 return perf_ctx->perf->n_queries;
1479
1480 perf_ctx->perf = gen_perf_new(brw);
1481 struct gen_perf_config *perf_cfg = perf_ctx->perf;
1482
1483 perf_cfg->vtbl.bo_alloc = brw_oa_bo_alloc;
1484 perf_cfg->vtbl.bo_unreference = (bo_unreference_t)brw_bo_unreference;
1485 perf_cfg->vtbl.bo_map = (bo_map_t)brw_bo_map;
1486 perf_cfg->vtbl.bo_unmap = (bo_unmap_t)brw_bo_unmap;
1487 perf_cfg->vtbl.emit_mi_flush = (emit_mi_flush_t)brw_emit_mi_flush;
1488 perf_cfg->vtbl.emit_mi_report_perf_count =
1489 (emit_mi_report_t)brw_oa_emit_mi_report_perf_count;
1490 perf_cfg->vtbl.batchbuffer_flush = brw_oa_batchbuffer_flush;
1491 perf_cfg->vtbl.capture_frequency_stat_register =
1492 (capture_frequency_stat_register_t) capture_frequency_stat_register;
1493 perf_cfg->vtbl.store_register_mem64 =
1494 (store_register_mem64_t) brw_store_register_mem64;
1495
1496 init_pipeline_statistic_query_registers(brw);
1497 gen_perf_query_register_mdapi_statistic_query(&brw->screen->devinfo,
1498 brw->perf_ctx.perf);
1499
1500 if ((oa_metrics_kernel_support(screen->fd, devinfo)) &&
1501 (gen_perf_load_oa_metrics(perf_cfg, screen->fd, devinfo)))
1502 gen_perf_query_register_mdapi_oa_query(devinfo, perf_cfg);
1503
1504 perf_ctx->unaccumulated =
1505 ralloc_array(brw, struct gen_perf_query_object *, 2);
1506 perf_ctx->unaccumulated_elements = 0;
1507 perf_ctx->unaccumulated_array_size = 2;
1508
1509 exec_list_make_empty(&perf_ctx->sample_buffers);
1510 exec_list_make_empty(&perf_ctx->free_sample_buffers);
1511
1512 /* It's convenient to guarantee that this linked list of sample
1513 * buffers is never empty so we add an empty head so when we
1514 * Begin an OA query we can always take a reference on a buffer
1515 * in this list.
1516 */
1517 struct oa_sample_buf *buf = gen_perf_get_free_sample_buf(&brw->perf_ctx);
1518 exec_list_push_head(&perf_ctx->sample_buffers, &buf->link);
1519
1520 perf_ctx->oa_stream_fd = -1;
1521
1522 perf_ctx->next_query_start_report_id = 1000;
1523
1524 return perf_cfg->n_queries;
1525 }
1526
1527 void
1528 brw_init_performance_queries(struct brw_context *brw)
1529 {
1530 struct gl_context *ctx = &brw->ctx;
1531
1532 ctx->Driver.InitPerfQueryInfo = brw_init_perf_query_info;
1533 ctx->Driver.GetPerfQueryInfo = brw_get_perf_query_info;
1534 ctx->Driver.GetPerfCounterInfo = brw_get_perf_counter_info;
1535 ctx->Driver.NewPerfQueryObject = brw_new_perf_query_object;
1536 ctx->Driver.DeletePerfQuery = brw_delete_perf_query;
1537 ctx->Driver.BeginPerfQuery = brw_begin_perf_query;
1538 ctx->Driver.EndPerfQuery = brw_end_perf_query;
1539 ctx->Driver.WaitPerfQuery = brw_wait_perf_query;
1540 ctx->Driver.IsPerfQueryReady = brw_is_perf_query_ready;
1541 ctx->Driver.GetPerfQueryData = brw_get_perf_query_data;
1542 }