2 * Copyright © 2013 Intel Corporation
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:
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
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.
25 * \file brw_performance_query.c
27 * Implementation of the GL_INTEL_performance_query extension.
29 * Currently there are two possible counter sources exposed here:
31 * On Gen6+ hardware we have numerous 64bit Pipeline Statistics Registers
32 * that we can snapshot at the beginning and end of a query.
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+)
44 /* put before sys/types.h to silence glibc warnings */
46 #include <sys/mkdev.h>
48 #ifdef MAJOR_IN_SYSMACROS
49 #include <sys/sysmacros.h>
51 #include <sys/types.h>
55 #include <sys/ioctl.h>
58 #include "drm-uapi/i915_drm.h"
60 #include "main/hash.h"
61 #include "main/macros.h"
62 #include "main/mtypes.h"
63 #include "main/performance_query.h"
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"
71 #include "brw_context.h"
72 #include "brw_defines.h"
73 #include "intel_batchbuffer.h"
75 #include "perf/gen_perf.h"
76 #include "perf/gen_perf_mdapi.h"
78 #define FILE_DEBUG_FLAG DEBUG_PERFMON
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)
88 struct brw_perf_query_object
{
89 struct gl_perf_query_object base
;
90 struct gen_perf_query_object
*query
;
93 /** Downcasting convenience macro. */
94 static inline struct brw_perf_query_object
*
95 brw_perf_query(struct gl_perf_query_object
*o
)
97 return (struct brw_perf_query_object
*) o
;
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)
105 /******************************************************************************/
108 brw_is_perf_query_ready(struct gl_context
*ctx
,
109 struct gl_perf_query_object
*o
);
112 dump_perf_query_callback(GLuint id
, void *query_void
, void *brw_void
)
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
;
120 DBG("%4d: %-6s %-8s ",
122 o
->Used
? "Dirty," : "New,",
123 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"));
124 gen_perf_dump_query(perf_ctx
, obj
, &ctx
->batch
);
128 dump_perf_queries(struct brw_context
*brw
)
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
);
136 * Driver hook for glGetPerfQueryInfoINTEL().
139 brw_get_perf_query_info(struct gl_context
*ctx
,
140 unsigned query_index
,
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
];
152 *data_size
= query
->data_size
;
153 *n_counters
= query
->n_counters
;
154 *n_active
= gen_perf_active_queries(perf_ctx
, query
);
158 gen_counter_type_enum_to_gl_type(enum gen_perf_counter_type 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
;
168 unreachable("Unknown counter type");
173 gen_counter_data_type_to_gl_type(enum gen_perf_counter_data_type 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
;
182 unreachable("Unknown counter data type");
187 * Driver hook for glGetPerfCounterInfoINTEL().
190 brw_get_perf_counter_info(struct gl_context
*ctx
,
191 unsigned query_index
,
192 unsigned counter_index
,
198 GLuint
*data_type_enum
,
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
];
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
;
218 OA_READ_STATUS_ERROR
,
219 OA_READ_STATUS_UNFINISHED
,
220 OA_READ_STATUS_FINISHED
,
223 /******************************************************************************/
226 capture_frequency_stat_register(struct brw_context
*brw
,
230 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
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
);
241 * Driver hook for glBeginPerfQueryINTEL().
244 brw_begin_perf_query(struct gl_context
*ctx
,
245 struct gl_perf_query_object
*o
)
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
;
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.
259 assert(!o
->Used
|| o
->Ready
); /* no in-flight query to worry about */
261 DBG("Begin(%d)\n", o
->Id
);
263 gen_perf_begin_query(perf_ctx
, obj
);
265 if (INTEL_DEBUG
& DEBUG_PERFMON
)
266 dump_perf_queries(brw
);
272 * Driver hook for glEndPerfQueryINTEL().
275 brw_end_perf_query(struct gl_context
*ctx
,
276 struct gl_perf_query_object
*o
)
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
;
283 DBG("End(%d)\n", o
->Id
);
284 gen_perf_end_query(perf_ctx
, obj
);
288 brw_wait_perf_query(struct gl_context
*ctx
, struct gl_perf_query_object
*o
)
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
;
296 gen_perf_wait_query(brw
->perf_ctx
, obj
, &brw
->batch
);
300 brw_is_perf_query_ready(struct gl_context
*ctx
,
301 struct gl_perf_query_object
*o
)
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
;
310 return gen_perf_is_query_ready(brw
->perf_ctx
, obj
, &brw
->batch
);
314 * Driver hook for glGetPerfQueryDataINTEL().
317 brw_get_perf_query_data(struct gl_context
*ctx
,
318 struct gl_perf_query_object
*o
,
321 GLuint
*bytes_written
)
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
;
327 assert(brw_is_perf_query_ready(ctx
, o
));
329 DBG("GetData(%d)\n", o
->Id
);
331 if (INTEL_DEBUG
& DEBUG_PERFMON
)
332 dump_perf_queries(brw
);
334 /* We expect that the frontend only calls this hook when it knows
335 * that results are available.
339 gen_perf_get_query_data(brw
->perf_ctx
, obj
,
340 data_size
, data
, bytes_written
);
343 static struct gl_perf_query_object
*
344 brw_new_perf_query_object(struct gl_context
*ctx
, unsigned query_index
)
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
);
352 struct brw_perf_query_object
*brw_query
= calloc(1, sizeof(struct brw_perf_query_object
));
353 if (unlikely(!brw_query
)) {
354 gen_perf_delete_query(perf_ctx
, obj
);
358 brw_query
->query
= obj
;
359 return &brw_query
->base
;
363 * Driver hook for glDeletePerfQueryINTEL().
366 brw_delete_perf_query(struct gl_context
*ctx
,
367 struct gl_perf_query_object
*o
)
369 struct brw_context
*brw
= brw_context(ctx
);
370 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
371 struct gen_perf_query_object
*obj
= brw_query
->query
;
372 struct gen_perf_context
*perf_ctx
= brw
->perf_ctx
;
374 /* We can assume that the frontend waits for a query to complete
375 * before ever calling into here, so we don't have to worry about
376 * deleting an in-flight query object.
379 assert(!o
->Used
|| o
->Ready
);
381 DBG("Delete(%d)\n", o
->Id
);
383 gen_perf_delete_query(perf_ctx
, obj
);
387 /******************************************************************************/
388 /* gen_device_info will have incorrect default topology values for unsupported kernels.
389 * verify kernel support to ensure OA metrics are accurate.
392 oa_metrics_kernel_support(int fd
, const struct gen_device_info
*devinfo
)
394 if (devinfo
->gen
>= 10) {
395 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
398 struct drm_i915_query_item item
= {
399 .query_id
= DRM_I915_QUERY_TOPOLOGY_INFO
,
401 struct drm_i915_query query
= {
403 .items_ptr
= (uintptr_t) &item
,
406 /* kernel 4.17+ supports the query */
407 return drmIoctl(fd
, DRM_IOCTL_I915_QUERY
, &query
) == 0;
410 if (devinfo
->gen
>= 8) {
411 /* 4.13+ api required for gen8 - gen9 */
413 struct drm_i915_getparam gp
= {
414 .param
= I915_PARAM_SLICE_MASK
,
417 /* kernel 4.13+ supports this parameter */
418 return drmIoctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
) == 0;
421 if (devinfo
->gen
== 7)
422 /* default topology values are correct for HSW */
425 /* oa not supported before gen 7*/
430 brw_oa_bo_alloc(void *bufmgr
, const char *name
, uint64_t size
)
432 return brw_bo_alloc(bufmgr
, name
, size
, BRW_MEMZONE_OTHER
);
436 brw_oa_emit_mi_report_perf_count(void *c
,
438 uint32_t offset_in_bytes
,
441 struct brw_context
*ctx
= c
;
442 ctx
->vtbl
.emit_mi_report_perf_count(ctx
,
448 typedef void (*bo_unreference_t
)(void *);
449 typedef void *(*bo_map_t
)(void *, void *, unsigned flags
);
450 typedef void (*bo_unmap_t
)(void *);
451 typedef void (* emit_mi_report_t
)(void *, void *, uint32_t, uint32_t);
452 typedef void (*emit_mi_flush_t
)(void *);
455 brw_oa_batchbuffer_flush(void *c
, const char *file
, int line
)
457 struct brw_context
*ctx
= c
;
458 _intel_batchbuffer_flush_fence(ctx
, -1, NULL
, file
, line
);
461 typedef void (*capture_frequency_stat_register_t
)(void *, void *, uint32_t );
462 typedef void (*store_register_mem64_t
)(void *ctx
, void *bo
,
463 uint32_t reg
, uint32_t offset
);
464 typedef bool (*batch_references_t
)(void *batch
, void *bo
);
465 typedef void (*bo_wait_rendering_t
)(void *bo
);
466 typedef int (*bo_busy_t
)(void *bo
);
469 brw_init_perf_query_info(struct gl_context
*ctx
)
471 struct brw_context
*brw
= brw_context(ctx
);
472 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
474 struct gen_perf_context
*perf_ctx
= brw
->perf_ctx
;
475 struct gen_perf_config
*perf_cfg
= gen_perf_config(perf_ctx
);
478 return perf_cfg
->n_queries
;
480 if (!oa_metrics_kernel_support(brw
->screen
->driScrnPriv
->fd
, devinfo
))
483 perf_cfg
= gen_perf_new(ctx
);
485 perf_cfg
->vtbl
.bo_alloc
= brw_oa_bo_alloc
;
486 perf_cfg
->vtbl
.bo_unreference
= (bo_unreference_t
)brw_bo_unreference
;
487 perf_cfg
->vtbl
.bo_map
= (bo_map_t
)brw_bo_map
;
488 perf_cfg
->vtbl
.bo_unmap
= (bo_unmap_t
)brw_bo_unmap
;
489 perf_cfg
->vtbl
.emit_mi_flush
= (emit_mi_flush_t
)brw_emit_mi_flush
;
490 perf_cfg
->vtbl
.emit_mi_report_perf_count
=
491 (emit_mi_report_t
)brw_oa_emit_mi_report_perf_count
;
492 perf_cfg
->vtbl
.batchbuffer_flush
= brw_oa_batchbuffer_flush
;
493 perf_cfg
->vtbl
.capture_frequency_stat_register
=
494 (capture_frequency_stat_register_t
) capture_frequency_stat_register
;
495 perf_cfg
->vtbl
.store_register_mem64
=
496 (store_register_mem64_t
) brw_store_register_mem64
;
497 perf_cfg
->vtbl
.batch_references
= (batch_references_t
)brw_batch_references
;
498 perf_cfg
->vtbl
.bo_wait_rendering
= (bo_wait_rendering_t
)brw_bo_wait_rendering
;
499 perf_cfg
->vtbl
.bo_busy
= (bo_busy_t
)brw_bo_busy
;
501 gen_perf_init_context(perf_ctx
, perf_cfg
, brw
, brw
->bufmgr
, devinfo
,
502 brw
->hw_ctx
, brw
->screen
->driScrnPriv
->fd
);
503 gen_perf_init_metrics(perf_cfg
, devinfo
, brw
->screen
->driScrnPriv
->fd
);
505 return perf_cfg
->n_queries
;
509 brw_init_performance_queries(struct brw_context
*brw
)
511 struct gl_context
*ctx
= &brw
->ctx
;
513 ctx
->Driver
.InitPerfQueryInfo
= brw_init_perf_query_info
;
514 ctx
->Driver
.GetPerfQueryInfo
= brw_get_perf_query_info
;
515 ctx
->Driver
.GetPerfCounterInfo
= brw_get_perf_counter_info
;
516 ctx
->Driver
.NewPerfQueryObject
= brw_new_perf_query_object
;
517 ctx
->Driver
.DeletePerfQuery
= brw_delete_perf_query
;
518 ctx
->Driver
.BeginPerfQuery
= brw_begin_perf_query
;
519 ctx
->Driver
.EndPerfQuery
= brw_end_perf_query
;
520 ctx
->Driver
.WaitPerfQuery
= brw_wait_perf_query
;
521 ctx
->Driver
.IsPerfQueryReady
= brw_is_perf_query_ready
;
522 ctx
->Driver
.GetPerfQueryData
= brw_get_perf_query_data
;