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