nir: support lowering clipdist to arrays
[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 gen_perf_delete_query(perf_ctx, obj);
355 return NULL;
356 }
357
358 brw_query->query = obj;
359 return &brw_query->base;
360 }
361
362 /**
363 * Driver hook for glDeletePerfQueryINTEL().
364 */
365 static void
366 brw_delete_perf_query(struct gl_context *ctx,
367 struct gl_perf_query_object *o)
368 {
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;
373
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.
377 */
378 assert(!o->Active);
379 assert(!o->Used || o->Ready);
380
381 DBG("Delete(%d)\n", o->Id);
382
383 gen_perf_delete_query(perf_ctx, obj);
384 free(brw_query);
385 }
386
387 /******************************************************************************/
388 /* gen_device_info will have incorrect default topology values for unsupported kernels.
389 * verify kernel support to ensure OA metrics are accurate.
390 */
391 static bool
392 oa_metrics_kernel_support(int fd, const struct gen_device_info *devinfo)
393 {
394 if (devinfo->gen >= 10) {
395 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
396 * to verify support
397 */
398 struct drm_i915_query_item item = {
399 .query_id = DRM_I915_QUERY_TOPOLOGY_INFO,
400 };
401 struct drm_i915_query query = {
402 .num_items = 1,
403 .items_ptr = (uintptr_t) &item,
404 };
405
406 /* kernel 4.17+ supports the query */
407 return drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query) == 0;
408 }
409
410 if (devinfo->gen >= 8) {
411 /* 4.13+ api required for gen8 - gen9 */
412 int mask;
413 struct drm_i915_getparam gp = {
414 .param = I915_PARAM_SLICE_MASK,
415 .value = &mask,
416 };
417 /* kernel 4.13+ supports this parameter */
418 return drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == 0;
419 }
420
421 if (devinfo->gen == 7)
422 /* default topology values are correct for HSW */
423 return true;
424
425 /* oa not supported before gen 7*/
426 return false;
427 }
428
429 static void *
430 brw_oa_bo_alloc(void *bufmgr, const char *name, uint64_t size)
431 {
432 return brw_bo_alloc(bufmgr, name, size, BRW_MEMZONE_OTHER);
433 }
434
435 static void
436 brw_oa_emit_mi_report_perf_count(void *c,
437 void *bo,
438 uint32_t offset_in_bytes,
439 uint32_t report_id)
440 {
441 struct brw_context *ctx = c;
442 ctx->vtbl.emit_mi_report_perf_count(ctx,
443 bo,
444 offset_in_bytes,
445 report_id);
446 }
447
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 *);
453
454 static void
455 brw_oa_batchbuffer_flush(void *c, const char *file, int line)
456 {
457 struct brw_context *ctx = c;
458 _intel_batchbuffer_flush_fence(ctx, -1, NULL, file, line);
459 }
460
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);
467
468 static unsigned
469 brw_init_perf_query_info(struct gl_context *ctx)
470 {
471 struct brw_context *brw = brw_context(ctx);
472 const struct gen_device_info *devinfo = &brw->screen->devinfo;
473
474 struct gen_perf_context *perf_ctx = brw->perf_ctx;
475 struct gen_perf_config *perf_cfg = gen_perf_config(perf_ctx);
476
477 if (perf_cfg)
478 return perf_cfg->n_queries;
479
480 if (!oa_metrics_kernel_support(brw->screen->driScrnPriv->fd, devinfo))
481 return 0;
482
483 perf_cfg = gen_perf_new(ctx);
484
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;
500
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);
504
505 return perf_cfg->n_queries;
506 }
507
508 void
509 brw_init_performance_queries(struct brw_context *brw)
510 {
511 struct gl_context *ctx = &brw->ctx;
512
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;
523 }