intel/perf: move initialization of pipeline statistics metrics to gen_perf
[mesa.git] / src / mesa / drivers / dri / i965 / brw_performance_query.c
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_performance_query.c
26 *
27 * Implementation of the GL_INTEL_performance_query extension.
28 *
29 * Currently there are two possible counter sources exposed here:
30 *
31 * On Gen6+ hardware we have numerous 64bit Pipeline Statistics Registers
32 * that we can snapshot at the beginning and end of a query.
33 *
34 * On Gen7.5+ we have Observability Architecture counters which are
35 * covered in separate document from the rest of the PRMs. It is available at:
36 * https://01.org/linuxgraphics/documentation/driver-documentation-prms
37 * => 2013 Intel Core Processor Family => Observability Performance Counters
38 * (This one volume covers Sandybridge, Ivybridge, Baytrail, and Haswell,
39 * though notably we currently only support OA counters for Haswell+)
40 */
41
42 #include <limits.h>
43
44 /* put before sys/types.h to silence glibc warnings */
45 #ifdef MAJOR_IN_MKDEV
46 #include <sys/mkdev.h>
47 #endif
48 #ifdef MAJOR_IN_SYSMACROS
49 #include <sys/sysmacros.h>
50 #endif
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <sys/mman.h>
55 #include <sys/ioctl.h>
56
57 #include <xf86drm.h>
58 #include "drm-uapi/i915_drm.h"
59
60 #include "main/hash.h"
61 #include "main/macros.h"
62 #include "main/mtypes.h"
63 #include "main/performance_query.h"
64
65 #include "util/bitset.h"
66 #include "util/ralloc.h"
67 #include "util/hash_table.h"
68 #include "util/list.h"
69 #include "util/u_math.h"
70
71 #include "brw_context.h"
72 #include "brw_defines.h"
73 #include "intel_batchbuffer.h"
74
75 #include "perf/gen_perf.h"
76 #include "perf/gen_perf_mdapi.h"
77
78 #define FILE_DEBUG_FLAG DEBUG_PERFMON
79
80 #define OAREPORT_REASON_MASK 0x3f
81 #define OAREPORT_REASON_SHIFT 19
82 #define OAREPORT_REASON_TIMER (1<<0)
83 #define OAREPORT_REASON_TRIGGER1 (1<<1)
84 #define OAREPORT_REASON_TRIGGER2 (1<<2)
85 #define OAREPORT_REASON_CTX_SWITCH (1<<3)
86 #define OAREPORT_REASON_GO_TRANSITION (1<<4)
87
88 struct brw_perf_query_object {
89 struct gl_perf_query_object base;
90 struct gen_perf_query_object *query;
91 };
92
93 /** Downcasting convenience macro. */
94 static inline struct brw_perf_query_object *
95 brw_perf_query(struct gl_perf_query_object *o)
96 {
97 return (struct brw_perf_query_object *) o;
98 }
99
100 #define MI_RPC_BO_SIZE 4096
101 #define MI_RPC_BO_END_OFFSET_BYTES (MI_RPC_BO_SIZE / 2)
102 #define MI_FREQ_START_OFFSET_BYTES (3072)
103 #define MI_FREQ_END_OFFSET_BYTES (3076)
104
105 /******************************************************************************/
106
107 static bool
108 brw_is_perf_query_ready(struct gl_context *ctx,
109 struct gl_perf_query_object *o);
110
111 static void
112 dump_perf_query_callback(GLuint id, void *query_void, void *brw_void)
113 {
114 struct gl_context *ctx = brw_void;
115 struct gl_perf_query_object *o = query_void;
116 struct brw_perf_query_object * brw_query = brw_perf_query(o);
117 struct gen_perf_query_object *obj = brw_query->query;
118
119 switch (obj->queryinfo->kind) {
120 case GEN_PERF_QUERY_TYPE_OA:
121 case GEN_PERF_QUERY_TYPE_RAW:
122 DBG("%4d: %-6s %-8s BO: %-4s OA data: %-10s %-15s\n",
123 id,
124 o->Used ? "Dirty," : "New,",
125 o->Active ? "Active," : (o->Ready ? "Ready," : "Pending,"),
126 obj->oa.bo ? "yes," : "no,",
127 brw_is_perf_query_ready(ctx, o) ? "ready," : "not ready,",
128 obj->oa.results_accumulated ? "accumulated" : "not accumulated");
129 break;
130 case GEN_PERF_QUERY_TYPE_PIPELINE:
131 DBG("%4d: %-6s %-8s BO: %-4s\n",
132 id,
133 o->Used ? "Dirty," : "New,",
134 o->Active ? "Active," : (o->Ready ? "Ready," : "Pending,"),
135 obj->pipeline_stats.bo ? "yes" : "no");
136 break;
137 default:
138 unreachable("Unknown query type");
139 break;
140 }
141 }
142
143 static void
144 dump_perf_queries(struct brw_context *brw)
145 {
146 struct gl_context *ctx = &brw->ctx;
147 DBG("Queries: (Open queries = %d, OA users = %d)\n",
148 brw->perf_ctx.n_active_oa_queries, brw->perf_ctx.n_oa_users);
149 _mesa_HashWalk(ctx->PerfQuery.Objects, dump_perf_query_callback, brw);
150 }
151
152 /**
153 * Driver hook for glGetPerfQueryInfoINTEL().
154 */
155 static void
156 brw_get_perf_query_info(struct gl_context *ctx,
157 unsigned query_index,
158 const char **name,
159 GLuint *data_size,
160 GLuint *n_counters,
161 GLuint *n_active)
162 {
163 struct brw_context *brw = brw_context(ctx);
164 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
165 const struct gen_perf_query_info *query =
166 &perf_ctx->perf->queries[query_index];
167
168 *name = query->name;
169 *data_size = query->data_size;
170 *n_counters = query->n_counters;
171
172 switch (query->kind) {
173 case GEN_PERF_QUERY_TYPE_OA:
174 case GEN_PERF_QUERY_TYPE_RAW:
175 *n_active = perf_ctx->n_active_oa_queries;
176 break;
177
178 case GEN_PERF_QUERY_TYPE_PIPELINE:
179 *n_active = perf_ctx->n_active_pipeline_stats_queries;
180 break;
181
182 default:
183 unreachable("Unknown query type");
184 break;
185 }
186 }
187
188 static GLuint
189 gen_counter_type_enum_to_gl_type(enum gen_perf_counter_type type)
190 {
191 switch (type) {
192 case GEN_PERF_COUNTER_TYPE_EVENT: return GL_PERFQUERY_COUNTER_EVENT_INTEL;
193 case GEN_PERF_COUNTER_TYPE_DURATION_NORM: return GL_PERFQUERY_COUNTER_DURATION_NORM_INTEL;
194 case GEN_PERF_COUNTER_TYPE_DURATION_RAW: return GL_PERFQUERY_COUNTER_DURATION_RAW_INTEL;
195 case GEN_PERF_COUNTER_TYPE_THROUGHPUT: return GL_PERFQUERY_COUNTER_THROUGHPUT_INTEL;
196 case GEN_PERF_COUNTER_TYPE_RAW: return GL_PERFQUERY_COUNTER_RAW_INTEL;
197 case GEN_PERF_COUNTER_TYPE_TIMESTAMP: return GL_PERFQUERY_COUNTER_TIMESTAMP_INTEL;
198 default:
199 unreachable("Unknown counter type");
200 }
201 }
202
203 static GLuint
204 gen_counter_data_type_to_gl_type(enum gen_perf_counter_data_type type)
205 {
206 switch (type) {
207 case GEN_PERF_COUNTER_DATA_TYPE_BOOL32: return GL_PERFQUERY_COUNTER_DATA_BOOL32_INTEL;
208 case GEN_PERF_COUNTER_DATA_TYPE_UINT32: return GL_PERFQUERY_COUNTER_DATA_UINT32_INTEL;
209 case GEN_PERF_COUNTER_DATA_TYPE_UINT64: return GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL;
210 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT: return GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL;
211 case GEN_PERF_COUNTER_DATA_TYPE_DOUBLE: return GL_PERFQUERY_COUNTER_DATA_DOUBLE_INTEL;
212 default:
213 unreachable("Unknown counter data type");
214 }
215 }
216
217 /**
218 * Driver hook for glGetPerfCounterInfoINTEL().
219 */
220 static void
221 brw_get_perf_counter_info(struct gl_context *ctx,
222 unsigned query_index,
223 unsigned counter_index,
224 const char **name,
225 const char **desc,
226 GLuint *offset,
227 GLuint *data_size,
228 GLuint *type_enum,
229 GLuint *data_type_enum,
230 GLuint64 *raw_max)
231 {
232 struct brw_context *brw = brw_context(ctx);
233 const struct gen_perf_query_info *query =
234 &brw->perf_ctx.perf->queries[query_index];
235 const struct gen_perf_query_counter *counter =
236 &query->counters[counter_index];
237
238 *name = counter->name;
239 *desc = counter->desc;
240 *offset = counter->offset;
241 *data_size = gen_perf_query_counter_get_size(counter);
242 *type_enum = gen_counter_type_enum_to_gl_type(counter->type);
243 *data_type_enum = gen_counter_data_type_to_gl_type(counter->data_type);
244 *raw_max = counter->raw_max;
245 }
246
247 enum OaReadStatus {
248 OA_READ_STATUS_ERROR,
249 OA_READ_STATUS_UNFINISHED,
250 OA_READ_STATUS_FINISHED,
251 };
252
253 /******************************************************************************/
254
255 static void
256 capture_frequency_stat_register(struct brw_context *brw,
257 struct brw_bo *bo,
258 uint32_t bo_offset)
259 {
260 const struct gen_device_info *devinfo = &brw->screen->devinfo;
261
262 if (devinfo->gen >= 7 && devinfo->gen <= 8 &&
263 !devinfo->is_baytrail && !devinfo->is_cherryview) {
264 brw_store_register_mem32(brw, bo, GEN7_RPSTAT1, bo_offset);
265 } else if (devinfo->gen >= 9) {
266 brw_store_register_mem32(brw, bo, GEN9_RPSTAT0, bo_offset);
267 }
268 }
269
270 /**
271 * Driver hook for glBeginPerfQueryINTEL().
272 */
273 static bool
274 brw_begin_perf_query(struct gl_context *ctx,
275 struct gl_perf_query_object *o)
276 {
277 struct brw_context *brw = brw_context(ctx);
278 struct brw_perf_query_object *brw_query = brw_perf_query(o);
279 struct gen_perf_query_object *obj = brw_query->query;
280 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
281
282 /* We can assume the frontend hides mistaken attempts to Begin a
283 * query object multiple times before its End. Similarly if an
284 * application reuses a query object before results have arrived
285 * the frontend will wait for prior results so we don't need
286 * to support abandoning in-flight results.
287 */
288 assert(!o->Active);
289 assert(!o->Used || o->Ready); /* no in-flight query to worry about */
290
291 DBG("Begin(%d)\n", o->Id);
292
293 gen_perf_begin_query(perf_ctx, obj);
294
295 if (INTEL_DEBUG & DEBUG_PERFMON)
296 dump_perf_queries(brw);
297
298 return true;
299 }
300
301 /**
302 * Driver hook for glEndPerfQueryINTEL().
303 */
304 static void
305 brw_end_perf_query(struct gl_context *ctx,
306 struct gl_perf_query_object *o)
307 {
308 struct brw_context *brw = brw_context(ctx);
309 struct brw_perf_query_object *brw_query = brw_perf_query(o);
310 struct gen_perf_query_object *obj = brw_query->query;
311 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
312
313 DBG("End(%d)\n", o->Id);
314 gen_perf_end_query(perf_ctx, obj);
315 }
316
317 static void
318 brw_wait_perf_query(struct gl_context *ctx, struct gl_perf_query_object *o)
319 {
320 struct brw_context *brw = brw_context(ctx);
321 struct brw_perf_query_object *brw_query = brw_perf_query(o);
322 struct gen_perf_query_object *obj = brw_query->query;
323
324 assert(!o->Ready);
325
326 gen_perf_wait_query(&brw->perf_ctx, obj, &brw->batch);
327 }
328
329 static bool
330 brw_is_perf_query_ready(struct gl_context *ctx,
331 struct gl_perf_query_object *o)
332 {
333 struct brw_context *brw = brw_context(ctx);
334 struct brw_perf_query_object *brw_query = brw_perf_query(o);
335 struct gen_perf_query_object *obj = brw_query->query;
336
337 if (o->Ready)
338 return true;
339
340 return gen_perf_is_query_ready(&brw->perf_ctx, obj, &brw->batch);
341 }
342
343 /**
344 * Driver hook for glGetPerfQueryDataINTEL().
345 */
346 static void
347 brw_get_perf_query_data(struct gl_context *ctx,
348 struct gl_perf_query_object *o,
349 GLsizei data_size,
350 GLuint *data,
351 GLuint *bytes_written)
352 {
353 struct brw_context *brw = brw_context(ctx);
354 struct brw_perf_query_object *brw_query = brw_perf_query(o);
355 struct gen_perf_query_object *obj = brw_query->query;
356
357 assert(brw_is_perf_query_ready(ctx, o));
358
359 DBG("GetData(%d)\n", o->Id);
360
361 if (INTEL_DEBUG & DEBUG_PERFMON)
362 dump_perf_queries(brw);
363
364 /* We expect that the frontend only calls this hook when it knows
365 * that results are available.
366 */
367 assert(o->Ready);
368
369 gen_perf_get_query_data(&brw->perf_ctx, obj,
370 data_size, data, bytes_written);
371 }
372
373 static struct gl_perf_query_object *
374 brw_new_perf_query_object(struct gl_context *ctx, unsigned query_index)
375 {
376 struct brw_context *brw = brw_context(ctx);
377 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
378 const struct gen_perf_query_info *queryinfo =
379 &perf_ctx->perf->queries[query_index];
380 struct gen_perf_query_object *obj =
381 calloc(1, sizeof(struct gen_perf_query_object));
382
383 if (!obj)
384 return NULL;
385
386 obj->queryinfo = queryinfo;
387
388 perf_ctx->n_query_instances++;
389
390 struct brw_perf_query_object *brw_query = calloc(1, sizeof(struct brw_perf_query_object));
391 if (unlikely(!brw_query))
392 return NULL;
393 brw_query->query = obj;
394 return &brw_query->base;
395 }
396
397 /**
398 * Driver hook for glDeletePerfQueryINTEL().
399 */
400 static void
401 brw_delete_perf_query(struct gl_context *ctx,
402 struct gl_perf_query_object *o)
403 {
404 struct brw_context *brw = brw_context(ctx);
405 struct brw_perf_query_object *brw_query = brw_perf_query(o);
406 struct gen_perf_query_object *obj = brw_query->query;
407 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
408
409 /* We can assume that the frontend waits for a query to complete
410 * before ever calling into here, so we don't have to worry about
411 * deleting an in-flight query object.
412 */
413 assert(!o->Active);
414 assert(!o->Used || o->Ready);
415
416 DBG("Delete(%d)\n", o->Id);
417
418 gen_perf_delete_query(perf_ctx, obj);
419 free(brw_query);
420 }
421
422 /******************************************************************************/
423 /* gen_device_info will have incorrect default topology values for unsupported kernels.
424 * verify kernel support to ensure OA metrics are accurate.
425 */
426 static bool
427 oa_metrics_kernel_support(int fd, const struct gen_device_info *devinfo)
428 {
429 if (devinfo->gen >= 10) {
430 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
431 * to verify support
432 */
433 struct drm_i915_query_item item = {
434 .query_id = DRM_I915_QUERY_TOPOLOGY_INFO,
435 };
436 struct drm_i915_query query = {
437 .num_items = 1,
438 .items_ptr = (uintptr_t) &item,
439 };
440
441 /* kernel 4.17+ supports the query */
442 return drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query) == 0;
443 }
444
445 if (devinfo->gen >= 8) {
446 /* 4.13+ api required for gen8 - gen9 */
447 int mask;
448 struct drm_i915_getparam gp = {
449 .param = I915_PARAM_SLICE_MASK,
450 .value = &mask,
451 };
452 /* kernel 4.13+ supports this parameter */
453 return drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == 0;
454 }
455
456 if (devinfo->gen == 7)
457 /* default topology values are correct for HSW */
458 return true;
459
460 /* oa not supported before gen 7*/
461 return false;
462 }
463
464 static void *
465 brw_oa_bo_alloc(void *bufmgr, const char *name, uint64_t size)
466 {
467 return brw_bo_alloc(bufmgr, name, size, BRW_MEMZONE_OTHER);
468 }
469
470 static void
471 brw_oa_emit_mi_report_perf_count(void *c,
472 void *bo,
473 uint32_t offset_in_bytes,
474 uint32_t report_id)
475 {
476 struct brw_context *ctx = c;
477 ctx->vtbl.emit_mi_report_perf_count(ctx,
478 bo,
479 offset_in_bytes,
480 report_id);
481 }
482
483 typedef void (*bo_unreference_t)(void *);
484 typedef void *(*bo_map_t)(void *, void *, unsigned flags);
485 typedef void (*bo_unmap_t)(void *);
486 typedef void (* emit_mi_report_t)(void *, void *, uint32_t, uint32_t);
487 typedef void (*emit_mi_flush_t)(void *);
488
489 static void
490 brw_oa_batchbuffer_flush(void *c, const char *file, int line)
491 {
492 struct brw_context *ctx = c;
493 _intel_batchbuffer_flush_fence(ctx, -1, NULL, file, line);
494 }
495
496 typedef void (*capture_frequency_stat_register_t)(void *, void *, uint32_t );
497 typedef void (*store_register_mem64_t)(void *ctx, void *bo,
498 uint32_t reg, uint32_t offset);
499 typedef bool (*batch_references_t)(void *batch, void *bo);
500 typedef void (*bo_wait_rendering_t)(void *bo);
501 typedef int (*bo_busy_t)(void *bo);
502
503 static unsigned
504 brw_init_perf_query_info(struct gl_context *ctx)
505 {
506 struct brw_context *brw = brw_context(ctx);
507 const struct gen_device_info *devinfo = &brw->screen->devinfo;
508
509 struct gen_perf_context *perf_ctx = &brw->perf_ctx;
510 if (perf_ctx->perf)
511 return perf_ctx->perf->n_queries;
512
513 perf_ctx->perf = gen_perf_new(brw);
514 struct gen_perf_config *perf_cfg = perf_ctx->perf;
515
516 perf_cfg->vtbl.bo_alloc = brw_oa_bo_alloc;
517 perf_cfg->vtbl.bo_unreference = (bo_unreference_t)brw_bo_unreference;
518 perf_cfg->vtbl.bo_map = (bo_map_t)brw_bo_map;
519 perf_cfg->vtbl.bo_unmap = (bo_unmap_t)brw_bo_unmap;
520 perf_cfg->vtbl.emit_mi_flush = (emit_mi_flush_t)brw_emit_mi_flush;
521 perf_cfg->vtbl.emit_mi_report_perf_count =
522 (emit_mi_report_t)brw_oa_emit_mi_report_perf_count;
523 perf_cfg->vtbl.batchbuffer_flush = brw_oa_batchbuffer_flush;
524 perf_cfg->vtbl.capture_frequency_stat_register =
525 (capture_frequency_stat_register_t) capture_frequency_stat_register;
526 perf_cfg->vtbl.store_register_mem64 =
527 (store_register_mem64_t) brw_store_register_mem64;
528 perf_cfg->vtbl.batch_references = (batch_references_t)brw_batch_references;
529 perf_cfg->vtbl.bo_wait_rendering = (bo_wait_rendering_t)brw_bo_wait_rendering;
530 perf_cfg->vtbl.bo_busy = (bo_busy_t)brw_bo_busy;
531
532 gen_perf_init_context(perf_ctx, perf_cfg, brw, brw->bufmgr, devinfo,
533 brw->hw_ctx, brw->screen->driScrnPriv->fd);
534
535 if (!oa_metrics_kernel_support(perf_ctx->drm_fd, devinfo))
536 return 0;
537
538 gen_perf_init_metrics(perf_cfg, devinfo, perf_ctx->drm_fd);
539
540 return perf_cfg->n_queries;
541 }
542
543 void
544 brw_init_performance_queries(struct brw_context *brw)
545 {
546 struct gl_context *ctx = &brw->ctx;
547
548 ctx->Driver.InitPerfQueryInfo = brw_init_perf_query_info;
549 ctx->Driver.GetPerfQueryInfo = brw_get_perf_query_info;
550 ctx->Driver.GetPerfCounterInfo = brw_get_perf_counter_info;
551 ctx->Driver.NewPerfQueryObject = brw_new_perf_query_object;
552 ctx->Driver.DeletePerfQuery = brw_delete_perf_query;
553 ctx->Driver.BeginPerfQuery = brw_begin_perf_query;
554 ctx->Driver.EndPerfQuery = brw_end_perf_query;
555 ctx->Driver.WaitPerfQuery = brw_wait_perf_query;
556 ctx->Driver.IsPerfQueryReady = brw_is_perf_query_ready;
557 ctx->Driver.GetPerfQueryData = brw_get_perf_query_data;
558 }