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