intel/perf: make perf context private
[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 brw_context *ctx = brw_void;
115 struct gen_perf_context *perf_ctx = ctx->perf_ctx;
116 struct gl_perf_query_object *o = query_void;
117 struct brw_perf_query_object * brw_query = brw_perf_query(o);
118 struct gen_perf_query_object *obj = brw_query->query;
119
120 DBG("%4d: %-6s %-8s ",
121 id,
122 o->Used ? "Dirty," : "New,",
123 o->Active ? "Active," : (o->Ready ? "Ready," : "Pending,"));
124 gen_perf_dump_query(perf_ctx, obj, &ctx->batch);
125 }
126
127 static void
128 dump_perf_queries(struct brw_context *brw)
129 {
130 struct gl_context *ctx = &brw->ctx;
131 gen_perf_dump_query_count(brw->perf_ctx);
132 _mesa_HashWalk(ctx->PerfQuery.Objects, dump_perf_query_callback, brw);
133 }
134
135 /**
136 * Driver hook for glGetPerfQueryInfoINTEL().
137 */
138 static void
139 brw_get_perf_query_info(struct gl_context *ctx,
140 unsigned query_index,
141 const char **name,
142 GLuint *data_size,
143 GLuint *n_counters,
144 GLuint *n_active)
145 {
146 struct brw_context *brw = brw_context(ctx);
147 struct gen_perf_context *perf_ctx = brw->perf_ctx;
148 struct gen_perf_config *perf_cfg = gen_perf_config(perf_ctx);
149 const struct gen_perf_query_info *query = &perf_cfg->queries[query_index];
150
151 *name = query->name;
152 *data_size = query->data_size;
153 *n_counters = query->n_counters;
154 *n_active = gen_perf_active_queries(perf_ctx, query);
155 }
156
157 static GLuint
158 gen_counter_type_enum_to_gl_type(enum gen_perf_counter_type type)
159 {
160 switch (type) {
161 case GEN_PERF_COUNTER_TYPE_EVENT: return GL_PERFQUERY_COUNTER_EVENT_INTEL;
162 case GEN_PERF_COUNTER_TYPE_DURATION_NORM: return GL_PERFQUERY_COUNTER_DURATION_NORM_INTEL;
163 case GEN_PERF_COUNTER_TYPE_DURATION_RAW: return GL_PERFQUERY_COUNTER_DURATION_RAW_INTEL;
164 case GEN_PERF_COUNTER_TYPE_THROUGHPUT: return GL_PERFQUERY_COUNTER_THROUGHPUT_INTEL;
165 case GEN_PERF_COUNTER_TYPE_RAW: return GL_PERFQUERY_COUNTER_RAW_INTEL;
166 case GEN_PERF_COUNTER_TYPE_TIMESTAMP: return GL_PERFQUERY_COUNTER_TIMESTAMP_INTEL;
167 default:
168 unreachable("Unknown counter type");
169 }
170 }
171
172 static GLuint
173 gen_counter_data_type_to_gl_type(enum gen_perf_counter_data_type type)
174 {
175 switch (type) {
176 case GEN_PERF_COUNTER_DATA_TYPE_BOOL32: return GL_PERFQUERY_COUNTER_DATA_BOOL32_INTEL;
177 case GEN_PERF_COUNTER_DATA_TYPE_UINT32: return GL_PERFQUERY_COUNTER_DATA_UINT32_INTEL;
178 case GEN_PERF_COUNTER_DATA_TYPE_UINT64: return GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL;
179 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT: return GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL;
180 case GEN_PERF_COUNTER_DATA_TYPE_DOUBLE: return GL_PERFQUERY_COUNTER_DATA_DOUBLE_INTEL;
181 default:
182 unreachable("Unknown counter data type");
183 }
184 }
185
186 /**
187 * Driver hook for glGetPerfCounterInfoINTEL().
188 */
189 static void
190 brw_get_perf_counter_info(struct gl_context *ctx,
191 unsigned query_index,
192 unsigned counter_index,
193 const char **name,
194 const char **desc,
195 GLuint *offset,
196 GLuint *data_size,
197 GLuint *type_enum,
198 GLuint *data_type_enum,
199 GLuint64 *raw_max)
200 {
201 struct brw_context *brw = brw_context(ctx);
202 struct gen_perf_config *perf_cfg = gen_perf_config(brw->perf_ctx);
203 const struct gen_perf_query_info *query =
204 &perf_cfg->queries[query_index];
205 const struct gen_perf_query_counter *counter =
206 &query->counters[counter_index];
207
208 *name = counter->name;
209 *desc = counter->desc;
210 *offset = counter->offset;
211 *data_size = gen_perf_query_counter_get_size(counter);
212 *type_enum = gen_counter_type_enum_to_gl_type(counter->type);
213 *data_type_enum = gen_counter_data_type_to_gl_type(counter->data_type);
214 *raw_max = counter->raw_max;
215 }
216
217 enum OaReadStatus {
218 OA_READ_STATUS_ERROR,
219 OA_READ_STATUS_UNFINISHED,
220 OA_READ_STATUS_FINISHED,
221 };
222
223 /******************************************************************************/
224
225 static void
226 capture_frequency_stat_register(struct brw_context *brw,
227 struct brw_bo *bo,
228 uint32_t bo_offset)
229 {
230 const struct gen_device_info *devinfo = &brw->screen->devinfo;
231
232 if (devinfo->gen >= 7 && devinfo->gen <= 8 &&
233 !devinfo->is_baytrail && !devinfo->is_cherryview) {
234 brw_store_register_mem32(brw, bo, GEN7_RPSTAT1, bo_offset);
235 } else if (devinfo->gen >= 9) {
236 brw_store_register_mem32(brw, bo, GEN9_RPSTAT0, bo_offset);
237 }
238 }
239
240 /**
241 * Driver hook for glBeginPerfQueryINTEL().
242 */
243 static bool
244 brw_begin_perf_query(struct gl_context *ctx,
245 struct gl_perf_query_object *o)
246 {
247 struct brw_context *brw = brw_context(ctx);
248 struct brw_perf_query_object *brw_query = brw_perf_query(o);
249 struct gen_perf_query_object *obj = brw_query->query;
250 struct gen_perf_context *perf_ctx = brw->perf_ctx;
251
252 /* We can assume the frontend hides mistaken attempts to Begin a
253 * query object multiple times before its End. Similarly if an
254 * application reuses a query object before results have arrived
255 * the frontend will wait for prior results so we don't need
256 * to support abandoning in-flight results.
257 */
258 assert(!o->Active);
259 assert(!o->Used || o->Ready); /* no in-flight query to worry about */
260
261 DBG("Begin(%d)\n", o->Id);
262
263 gen_perf_begin_query(perf_ctx, obj);
264
265 if (INTEL_DEBUG & DEBUG_PERFMON)
266 dump_perf_queries(brw);
267
268 return true;
269 }
270
271 /**
272 * Driver hook for glEndPerfQueryINTEL().
273 */
274 static void
275 brw_end_perf_query(struct gl_context *ctx,
276 struct gl_perf_query_object *o)
277 {
278 struct brw_context *brw = brw_context(ctx);
279 struct brw_perf_query_object *brw_query = brw_perf_query(o);
280 struct gen_perf_query_object *obj = brw_query->query;
281 struct gen_perf_context *perf_ctx = brw->perf_ctx;
282
283 DBG("End(%d)\n", o->Id);
284 gen_perf_end_query(perf_ctx, obj);
285 }
286
287 static void
288 brw_wait_perf_query(struct gl_context *ctx, struct gl_perf_query_object *o)
289 {
290 struct brw_context *brw = brw_context(ctx);
291 struct brw_perf_query_object *brw_query = brw_perf_query(o);
292 struct gen_perf_query_object *obj = brw_query->query;
293
294 assert(!o->Ready);
295
296 gen_perf_wait_query(brw->perf_ctx, obj, &brw->batch);
297 }
298
299 static bool
300 brw_is_perf_query_ready(struct gl_context *ctx,
301 struct gl_perf_query_object *o)
302 {
303 struct brw_context *brw = brw_context(ctx);
304 struct brw_perf_query_object *brw_query = brw_perf_query(o);
305 struct gen_perf_query_object *obj = brw_query->query;
306
307 if (o->Ready)
308 return true;
309
310 return gen_perf_is_query_ready(brw->perf_ctx, obj, &brw->batch);
311 }
312
313 /**
314 * Driver hook for glGetPerfQueryDataINTEL().
315 */
316 static void
317 brw_get_perf_query_data(struct gl_context *ctx,
318 struct gl_perf_query_object *o,
319 GLsizei data_size,
320 GLuint *data,
321 GLuint *bytes_written)
322 {
323 struct brw_context *brw = brw_context(ctx);
324 struct brw_perf_query_object *brw_query = brw_perf_query(o);
325 struct gen_perf_query_object *obj = brw_query->query;
326
327 assert(brw_is_perf_query_ready(ctx, o));
328
329 DBG("GetData(%d)\n", o->Id);
330
331 if (INTEL_DEBUG & DEBUG_PERFMON)
332 dump_perf_queries(brw);
333
334 /* We expect that the frontend only calls this hook when it knows
335 * that results are available.
336 */
337 assert(o->Ready);
338
339 gen_perf_get_query_data(brw->perf_ctx, obj,
340 data_size, data, bytes_written);
341 }
342
343 static struct gl_perf_query_object *
344 brw_new_perf_query_object(struct gl_context *ctx, unsigned query_index)
345 {
346 struct brw_context *brw = brw_context(ctx);
347 struct gen_perf_context *perf_ctx = brw->perf_ctx;
348 struct gen_perf_query_object * obj = gen_perf_new_query(perf_ctx, query_index);
349 if (unlikely(!obj))
350 return NULL;
351
352 struct brw_perf_query_object *brw_query = calloc(1, sizeof(struct brw_perf_query_object));
353 if (unlikely(!brw_query))
354 return NULL;
355
356 brw_query->query = obj;
357 return &brw_query->base;
358 }
359
360 /**
361 * Driver hook for glDeletePerfQueryINTEL().
362 */
363 static void
364 brw_delete_perf_query(struct gl_context *ctx,
365 struct gl_perf_query_object *o)
366 {
367 struct brw_context *brw = brw_context(ctx);
368 struct brw_perf_query_object *brw_query = brw_perf_query(o);
369 struct gen_perf_query_object *obj = brw_query->query;
370 struct gen_perf_context *perf_ctx = brw->perf_ctx;
371
372 /* We can assume that the frontend waits for a query to complete
373 * before ever calling into here, so we don't have to worry about
374 * deleting an in-flight query object.
375 */
376 assert(!o->Active);
377 assert(!o->Used || o->Ready);
378
379 DBG("Delete(%d)\n", o->Id);
380
381 gen_perf_delete_query(perf_ctx, obj);
382 free(brw_query);
383 }
384
385 /******************************************************************************/
386 /* gen_device_info will have incorrect default topology values for unsupported kernels.
387 * verify kernel support to ensure OA metrics are accurate.
388 */
389 static bool
390 oa_metrics_kernel_support(int fd, const struct gen_device_info *devinfo)
391 {
392 if (devinfo->gen >= 10) {
393 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
394 * to verify support
395 */
396 struct drm_i915_query_item item = {
397 .query_id = DRM_I915_QUERY_TOPOLOGY_INFO,
398 };
399 struct drm_i915_query query = {
400 .num_items = 1,
401 .items_ptr = (uintptr_t) &item,
402 };
403
404 /* kernel 4.17+ supports the query */
405 return drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query) == 0;
406 }
407
408 if (devinfo->gen >= 8) {
409 /* 4.13+ api required for gen8 - gen9 */
410 int mask;
411 struct drm_i915_getparam gp = {
412 .param = I915_PARAM_SLICE_MASK,
413 .value = &mask,
414 };
415 /* kernel 4.13+ supports this parameter */
416 return drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == 0;
417 }
418
419 if (devinfo->gen == 7)
420 /* default topology values are correct for HSW */
421 return true;
422
423 /* oa not supported before gen 7*/
424 return false;
425 }
426
427 static void *
428 brw_oa_bo_alloc(void *bufmgr, const char *name, uint64_t size)
429 {
430 return brw_bo_alloc(bufmgr, name, size, BRW_MEMZONE_OTHER);
431 }
432
433 static void
434 brw_oa_emit_mi_report_perf_count(void *c,
435 void *bo,
436 uint32_t offset_in_bytes,
437 uint32_t report_id)
438 {
439 struct brw_context *ctx = c;
440 ctx->vtbl.emit_mi_report_perf_count(ctx,
441 bo,
442 offset_in_bytes,
443 report_id);
444 }
445
446 typedef void (*bo_unreference_t)(void *);
447 typedef void *(*bo_map_t)(void *, void *, unsigned flags);
448 typedef void (*bo_unmap_t)(void *);
449 typedef void (* emit_mi_report_t)(void *, void *, uint32_t, uint32_t);
450 typedef void (*emit_mi_flush_t)(void *);
451
452 static void
453 brw_oa_batchbuffer_flush(void *c, const char *file, int line)
454 {
455 struct brw_context *ctx = c;
456 _intel_batchbuffer_flush_fence(ctx, -1, NULL, file, line);
457 }
458
459 typedef void (*capture_frequency_stat_register_t)(void *, void *, uint32_t );
460 typedef void (*store_register_mem64_t)(void *ctx, void *bo,
461 uint32_t reg, uint32_t offset);
462 typedef bool (*batch_references_t)(void *batch, void *bo);
463 typedef void (*bo_wait_rendering_t)(void *bo);
464 typedef int (*bo_busy_t)(void *bo);
465
466 static unsigned
467 brw_init_perf_query_info(struct gl_context *ctx)
468 {
469 struct brw_context *brw = brw_context(ctx);
470 const struct gen_device_info *devinfo = &brw->screen->devinfo;
471
472 struct gen_perf_context *perf_ctx = brw->perf_ctx;
473 struct gen_perf_config *perf_cfg = gen_perf_config(perf_ctx);
474
475 if (perf_cfg)
476 return perf_cfg->n_queries;
477
478 if (!oa_metrics_kernel_support(brw->screen->driScrnPriv->fd, devinfo))
479 return 0;
480
481 perf_cfg = gen_perf_new(ctx);
482
483 perf_cfg->vtbl.bo_alloc = brw_oa_bo_alloc;
484 perf_cfg->vtbl.bo_unreference = (bo_unreference_t)brw_bo_unreference;
485 perf_cfg->vtbl.bo_map = (bo_map_t)brw_bo_map;
486 perf_cfg->vtbl.bo_unmap = (bo_unmap_t)brw_bo_unmap;
487 perf_cfg->vtbl.emit_mi_flush = (emit_mi_flush_t)brw_emit_mi_flush;
488 perf_cfg->vtbl.emit_mi_report_perf_count =
489 (emit_mi_report_t)brw_oa_emit_mi_report_perf_count;
490 perf_cfg->vtbl.batchbuffer_flush = brw_oa_batchbuffer_flush;
491 perf_cfg->vtbl.capture_frequency_stat_register =
492 (capture_frequency_stat_register_t) capture_frequency_stat_register;
493 perf_cfg->vtbl.store_register_mem64 =
494 (store_register_mem64_t) brw_store_register_mem64;
495 perf_cfg->vtbl.batch_references = (batch_references_t)brw_batch_references;
496 perf_cfg->vtbl.bo_wait_rendering = (bo_wait_rendering_t)brw_bo_wait_rendering;
497 perf_cfg->vtbl.bo_busy = (bo_busy_t)brw_bo_busy;
498
499 gen_perf_init_context(perf_ctx, perf_cfg, brw, brw->bufmgr, devinfo,
500 brw->hw_ctx, brw->screen->driScrnPriv->fd);
501 gen_perf_init_metrics(perf_cfg, devinfo, brw->screen->driScrnPriv->fd);
502
503 return perf_cfg->n_queries;
504 }
505
506 void
507 brw_init_performance_queries(struct brw_context *brw)
508 {
509 struct gl_context *ctx = &brw->ctx;
510
511 ctx->Driver.InitPerfQueryInfo = brw_init_perf_query_info;
512 ctx->Driver.GetPerfQueryInfo = brw_get_perf_query_info;
513 ctx->Driver.GetPerfCounterInfo = brw_get_perf_counter_info;
514 ctx->Driver.NewPerfQueryObject = brw_new_perf_query_object;
515 ctx->Driver.DeletePerfQuery = brw_delete_perf_query;
516 ctx->Driver.BeginPerfQuery = brw_begin_perf_query;
517 ctx->Driver.EndPerfQuery = brw_end_perf_query;
518 ctx->Driver.WaitPerfQuery = brw_wait_perf_query;
519 ctx->Driver.IsPerfQueryReady = brw_is_perf_query_ready;
520 ctx->Driver.GetPerfQueryData = brw_get_perf_query_data;
521 }