Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/intel/perf/intel_perf.c
4547 views
1
/*
2
* Copyright © 2018 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 DEALINGS
21
* IN THE SOFTWARE.
22
*/
23
24
#include <dirent.h>
25
26
#include <sys/types.h>
27
#include <sys/stat.h>
28
#include <fcntl.h>
29
#include <unistd.h>
30
#include <errno.h>
31
32
#ifndef HAVE_DIRENT_D_TYPE
33
#include <limits.h> // PATH_MAX
34
#endif
35
36
#include <drm-uapi/i915_drm.h>
37
38
#include "common/intel_gem.h"
39
40
#include "dev/intel_debug.h"
41
#include "dev/intel_device_info.h"
42
43
#include "perf/intel_perf.h"
44
#include "perf/intel_perf_regs.h"
45
#include "perf/intel_perf_mdapi.h"
46
#include "perf/intel_perf_metrics.h"
47
#include "perf/intel_perf_private.h"
48
49
#include "util/bitscan.h"
50
#include "util/macros.h"
51
#include "util/mesa-sha1.h"
52
#include "util/u_math.h"
53
54
#define FILE_DEBUG_FLAG DEBUG_PERFMON
55
56
static bool
57
is_dir_or_link(const struct dirent *entry, const char *parent_dir)
58
{
59
#ifdef HAVE_DIRENT_D_TYPE
60
return entry->d_type == DT_DIR || entry->d_type == DT_LNK;
61
#else
62
struct stat st;
63
char path[PATH_MAX + 1];
64
snprintf(path, sizeof(path), "%s/%s", parent_dir, entry->d_name);
65
lstat(path, &st);
66
return S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode);
67
#endif
68
}
69
70
static bool
71
get_sysfs_dev_dir(struct intel_perf_config *perf, int fd)
72
{
73
struct stat sb;
74
int min, maj;
75
DIR *drmdir;
76
struct dirent *drm_entry;
77
int len;
78
79
perf->sysfs_dev_dir[0] = '\0';
80
81
if (INTEL_DEBUG & DEBUG_NO_OACONFIG)
82
return true;
83
84
if (fstat(fd, &sb)) {
85
DBG("Failed to stat DRM fd\n");
86
return false;
87
}
88
89
maj = major(sb.st_rdev);
90
min = minor(sb.st_rdev);
91
92
if (!S_ISCHR(sb.st_mode)) {
93
DBG("DRM fd is not a character device as expected\n");
94
return false;
95
}
96
97
len = snprintf(perf->sysfs_dev_dir,
98
sizeof(perf->sysfs_dev_dir),
99
"/sys/dev/char/%d:%d/device/drm", maj, min);
100
if (len < 0 || len >= sizeof(perf->sysfs_dev_dir)) {
101
DBG("Failed to concatenate sysfs path to drm device\n");
102
return false;
103
}
104
105
drmdir = opendir(perf->sysfs_dev_dir);
106
if (!drmdir) {
107
DBG("Failed to open %s: %m\n", perf->sysfs_dev_dir);
108
return false;
109
}
110
111
while ((drm_entry = readdir(drmdir))) {
112
if (is_dir_or_link(drm_entry, perf->sysfs_dev_dir) &&
113
strncmp(drm_entry->d_name, "card", 4) == 0)
114
{
115
len = snprintf(perf->sysfs_dev_dir,
116
sizeof(perf->sysfs_dev_dir),
117
"/sys/dev/char/%d:%d/device/drm/%s",
118
maj, min, drm_entry->d_name);
119
closedir(drmdir);
120
if (len < 0 || len >= sizeof(perf->sysfs_dev_dir))
121
return false;
122
else
123
return true;
124
}
125
}
126
127
closedir(drmdir);
128
129
DBG("Failed to find cardX directory under /sys/dev/char/%d:%d/device/drm\n",
130
maj, min);
131
132
return false;
133
}
134
135
static bool
136
read_file_uint64(const char *file, uint64_t *val)
137
{
138
char buf[32];
139
int fd, n;
140
141
fd = open(file, 0);
142
if (fd < 0)
143
return false;
144
while ((n = read(fd, buf, sizeof (buf) - 1)) < 0 &&
145
errno == EINTR);
146
close(fd);
147
if (n < 0)
148
return false;
149
150
buf[n] = '\0';
151
*val = strtoull(buf, NULL, 0);
152
153
return true;
154
}
155
156
static bool
157
read_sysfs_drm_device_file_uint64(struct intel_perf_config *perf,
158
const char *file,
159
uint64_t *value)
160
{
161
char buf[512];
162
int len;
163
164
len = snprintf(buf, sizeof(buf), "%s/%s", perf->sysfs_dev_dir, file);
165
if (len < 0 || len >= sizeof(buf)) {
166
DBG("Failed to concatenate sys filename to read u64 from\n");
167
return false;
168
}
169
170
return read_file_uint64(buf, value);
171
}
172
173
static void
174
register_oa_config(struct intel_perf_config *perf,
175
const struct intel_device_info *devinfo,
176
const struct intel_perf_query_info *query,
177
uint64_t config_id)
178
{
179
struct intel_perf_query_info *registered_query =
180
intel_perf_append_query_info(perf, 0);
181
182
*registered_query = *query;
183
registered_query->oa_format = devinfo->ver >= 8 ?
184
I915_OA_FORMAT_A32u40_A4u32_B8_C8 : I915_OA_FORMAT_A45_B8_C8;
185
registered_query->oa_metrics_set_id = config_id;
186
DBG("metric set registered: id = %" PRIu64", guid = %s\n",
187
registered_query->oa_metrics_set_id, query->guid);
188
}
189
190
static void
191
enumerate_sysfs_metrics(struct intel_perf_config *perf,
192
const struct intel_device_info *devinfo)
193
{
194
DIR *metricsdir = NULL;
195
struct dirent *metric_entry;
196
char buf[256];
197
int len;
198
199
len = snprintf(buf, sizeof(buf), "%s/metrics", perf->sysfs_dev_dir);
200
if (len < 0 || len >= sizeof(buf)) {
201
DBG("Failed to concatenate path to sysfs metrics/ directory\n");
202
return;
203
}
204
205
metricsdir = opendir(buf);
206
if (!metricsdir) {
207
DBG("Failed to open %s: %m\n", buf);
208
return;
209
}
210
211
while ((metric_entry = readdir(metricsdir))) {
212
struct hash_entry *entry;
213
if (!is_dir_or_link(metric_entry, buf) ||
214
metric_entry->d_name[0] == '.')
215
continue;
216
217
DBG("metric set: %s\n", metric_entry->d_name);
218
entry = _mesa_hash_table_search(perf->oa_metrics_table,
219
metric_entry->d_name);
220
if (entry) {
221
uint64_t id;
222
if (!intel_perf_load_metric_id(perf, metric_entry->d_name, &id)) {
223
DBG("Failed to read metric set id from %s: %m", buf);
224
continue;
225
}
226
227
register_oa_config(perf, devinfo,
228
(const struct intel_perf_query_info *)entry->data, id);
229
} else
230
DBG("metric set not known by mesa (skipping)\n");
231
}
232
233
closedir(metricsdir);
234
}
235
236
static void
237
add_all_metrics(struct intel_perf_config *perf,
238
const struct intel_device_info *devinfo)
239
{
240
hash_table_foreach(perf->oa_metrics_table, entry) {
241
const struct intel_perf_query_info *query = entry->data;
242
register_oa_config(perf, devinfo, query, 0);
243
}
244
}
245
246
static bool
247
kernel_has_dynamic_config_support(struct intel_perf_config *perf, int fd)
248
{
249
uint64_t invalid_config_id = UINT64_MAX;
250
251
return intel_ioctl(fd, DRM_IOCTL_I915_PERF_REMOVE_CONFIG,
252
&invalid_config_id) < 0 && errno == ENOENT;
253
}
254
255
static int
256
i915_query_items(struct intel_perf_config *perf, int fd,
257
struct drm_i915_query_item *items, uint32_t n_items)
258
{
259
struct drm_i915_query q = {
260
.num_items = n_items,
261
.items_ptr = to_user_pointer(items),
262
};
263
return intel_ioctl(fd, DRM_IOCTL_I915_QUERY, &q);
264
}
265
266
static bool
267
i915_query_perf_config_supported(struct intel_perf_config *perf, int fd)
268
{
269
struct drm_i915_query_item item = {
270
.query_id = DRM_I915_QUERY_PERF_CONFIG,
271
.flags = DRM_I915_QUERY_PERF_CONFIG_LIST,
272
};
273
274
return i915_query_items(perf, fd, &item, 1) == 0 && item.length > 0;
275
}
276
277
static bool
278
i915_query_perf_config_data(struct intel_perf_config *perf,
279
int fd, const char *guid,
280
struct drm_i915_perf_oa_config *config)
281
{
282
struct {
283
struct drm_i915_query_perf_config query;
284
struct drm_i915_perf_oa_config config;
285
} item_data;
286
struct drm_i915_query_item item = {
287
.query_id = DRM_I915_QUERY_PERF_CONFIG,
288
.flags = DRM_I915_QUERY_PERF_CONFIG_DATA_FOR_UUID,
289
.data_ptr = to_user_pointer(&item_data),
290
.length = sizeof(item_data),
291
};
292
293
memset(&item_data, 0, sizeof(item_data));
294
memcpy(item_data.query.uuid, guid, sizeof(item_data.query.uuid));
295
memcpy(&item_data.config, config, sizeof(item_data.config));
296
297
if (!(i915_query_items(perf, fd, &item, 1) == 0 && item.length > 0))
298
return false;
299
300
memcpy(config, &item_data.config, sizeof(item_data.config));
301
302
return true;
303
}
304
305
bool
306
intel_perf_load_metric_id(struct intel_perf_config *perf_cfg,
307
const char *guid,
308
uint64_t *metric_id)
309
{
310
char config_path[280];
311
312
snprintf(config_path, sizeof(config_path), "%s/metrics/%s/id",
313
perf_cfg->sysfs_dev_dir, guid);
314
315
/* Don't recreate already loaded configs. */
316
return read_file_uint64(config_path, metric_id);
317
}
318
319
static uint64_t
320
i915_add_config(struct intel_perf_config *perf, int fd,
321
const struct intel_perf_registers *config,
322
const char *guid)
323
{
324
struct drm_i915_perf_oa_config i915_config = { 0, };
325
326
memcpy(i915_config.uuid, guid, sizeof(i915_config.uuid));
327
328
i915_config.n_mux_regs = config->n_mux_regs;
329
i915_config.mux_regs_ptr = to_const_user_pointer(config->mux_regs);
330
331
i915_config.n_boolean_regs = config->n_b_counter_regs;
332
i915_config.boolean_regs_ptr = to_const_user_pointer(config->b_counter_regs);
333
334
i915_config.n_flex_regs = config->n_flex_regs;
335
i915_config.flex_regs_ptr = to_const_user_pointer(config->flex_regs);
336
337
int ret = intel_ioctl(fd, DRM_IOCTL_I915_PERF_ADD_CONFIG, &i915_config);
338
return ret > 0 ? ret : 0;
339
}
340
341
static void
342
init_oa_configs(struct intel_perf_config *perf, int fd,
343
const struct intel_device_info *devinfo)
344
{
345
hash_table_foreach(perf->oa_metrics_table, entry) {
346
const struct intel_perf_query_info *query = entry->data;
347
uint64_t config_id;
348
349
if (intel_perf_load_metric_id(perf, query->guid, &config_id)) {
350
DBG("metric set: %s (already loaded)\n", query->guid);
351
register_oa_config(perf, devinfo, query, config_id);
352
continue;
353
}
354
355
int ret = i915_add_config(perf, fd, &query->config, query->guid);
356
if (ret < 0) {
357
DBG("Failed to load \"%s\" (%s) metrics set in kernel: %s\n",
358
query->name, query->guid, strerror(errno));
359
continue;
360
}
361
362
register_oa_config(perf, devinfo, query, ret);
363
DBG("metric set: %s (added)\n", query->guid);
364
}
365
}
366
367
static void
368
compute_topology_builtins(struct intel_perf_config *perf,
369
const struct intel_device_info *devinfo)
370
{
371
perf->sys_vars.slice_mask = devinfo->slice_masks;
372
perf->sys_vars.n_eu_slices = devinfo->num_slices;
373
374
for (int i = 0; i < sizeof(devinfo->subslice_masks[i]); i++) {
375
perf->sys_vars.n_eu_sub_slices +=
376
util_bitcount(devinfo->subslice_masks[i]);
377
}
378
379
for (int i = 0; i < sizeof(devinfo->eu_masks); i++)
380
perf->sys_vars.n_eus += util_bitcount(devinfo->eu_masks[i]);
381
382
perf->sys_vars.eu_threads_count = devinfo->num_thread_per_eu;
383
384
/* The subslice mask builtin contains bits for all slices. Prior to Gfx11
385
* it had groups of 3bits for each slice, on Gfx11 it's 8bits for each
386
* slice.
387
*
388
* Ideally equations would be updated to have a slice/subslice query
389
* function/operator.
390
*/
391
perf->sys_vars.subslice_mask = 0;
392
393
int bits_per_subslice = devinfo->ver == 11 ? 8 : 3;
394
395
for (int s = 0; s < util_last_bit(devinfo->slice_masks); s++) {
396
for (int ss = 0; ss < (devinfo->subslice_slice_stride * 8); ss++) {
397
if (intel_device_info_subslice_available(devinfo, s, ss))
398
perf->sys_vars.subslice_mask |= 1ULL << (s * bits_per_subslice + ss);
399
}
400
}
401
}
402
403
static bool
404
init_oa_sys_vars(struct intel_perf_config *perf,
405
const struct intel_device_info *devinfo,
406
bool use_register_snapshots)
407
{
408
uint64_t min_freq_mhz = 0, max_freq_mhz = 0;
409
410
if (!(INTEL_DEBUG & DEBUG_NO_OACONFIG)) {
411
if (!read_sysfs_drm_device_file_uint64(perf, "gt_min_freq_mhz", &min_freq_mhz))
412
return false;
413
414
if (!read_sysfs_drm_device_file_uint64(perf, "gt_max_freq_mhz", &max_freq_mhz))
415
return false;
416
} else {
417
min_freq_mhz = 300;
418
max_freq_mhz = 1000;
419
}
420
421
memset(&perf->sys_vars, 0, sizeof(perf->sys_vars));
422
perf->sys_vars.gt_min_freq = min_freq_mhz * 1000000;
423
perf->sys_vars.gt_max_freq = max_freq_mhz * 1000000;
424
perf->sys_vars.timestamp_frequency = devinfo->timestamp_frequency;
425
perf->sys_vars.revision = devinfo->revision;
426
perf->sys_vars.query_mode = use_register_snapshots;
427
compute_topology_builtins(perf, devinfo);
428
429
return true;
430
}
431
432
typedef void (*perf_register_oa_queries_t)(struct intel_perf_config *);
433
434
static perf_register_oa_queries_t
435
get_register_queries_function(const struct intel_device_info *devinfo)
436
{
437
if (devinfo->is_haswell)
438
return intel_oa_register_queries_hsw;
439
if (devinfo->is_cherryview)
440
return intel_oa_register_queries_chv;
441
if (devinfo->is_broadwell)
442
return intel_oa_register_queries_bdw;
443
if (devinfo->is_broxton)
444
return intel_oa_register_queries_bxt;
445
if (devinfo->is_skylake) {
446
if (devinfo->gt == 2)
447
return intel_oa_register_queries_sklgt2;
448
if (devinfo->gt == 3)
449
return intel_oa_register_queries_sklgt3;
450
if (devinfo->gt == 4)
451
return intel_oa_register_queries_sklgt4;
452
}
453
if (devinfo->is_kabylake) {
454
if (devinfo->gt == 2)
455
return intel_oa_register_queries_kblgt2;
456
if (devinfo->gt == 3)
457
return intel_oa_register_queries_kblgt3;
458
}
459
if (devinfo->is_geminilake)
460
return intel_oa_register_queries_glk;
461
if (devinfo->is_coffeelake) {
462
if (devinfo->gt == 2)
463
return intel_oa_register_queries_cflgt2;
464
if (devinfo->gt == 3)
465
return intel_oa_register_queries_cflgt3;
466
}
467
if (devinfo->ver == 11) {
468
if (devinfo->is_elkhartlake)
469
return intel_oa_register_queries_ehl;
470
return intel_oa_register_queries_icl;
471
}
472
if (devinfo->is_tigerlake) {
473
if (devinfo->gt == 1)
474
return intel_oa_register_queries_tglgt1;
475
if (devinfo->gt == 2)
476
return intel_oa_register_queries_tglgt2;
477
}
478
if (devinfo->is_rocketlake)
479
return intel_oa_register_queries_rkl;
480
if (devinfo->is_dg1)
481
return intel_oa_register_queries_dg1;
482
if (devinfo->is_alderlake)
483
return intel_oa_register_queries_adl;
484
485
return NULL;
486
}
487
488
static int
489
intel_perf_compare_counter_names(const void *v1, const void *v2)
490
{
491
const struct intel_perf_query_counter *c1 = v1;
492
const struct intel_perf_query_counter *c2 = v2;
493
494
return strcmp(c1->name, c2->name);
495
}
496
497
static void
498
sort_query(struct intel_perf_query_info *q)
499
{
500
qsort(q->counters, q->n_counters, sizeof(q->counters[0]),
501
intel_perf_compare_counter_names);
502
}
503
504
static void
505
load_pipeline_statistic_metrics(struct intel_perf_config *perf_cfg,
506
const struct intel_device_info *devinfo)
507
{
508
struct intel_perf_query_info *query =
509
intel_perf_append_query_info(perf_cfg, MAX_STAT_COUNTERS);
510
511
query->kind = INTEL_PERF_QUERY_TYPE_PIPELINE;
512
query->name = "Pipeline Statistics Registers";
513
514
intel_perf_query_add_basic_stat_reg(query, IA_VERTICES_COUNT,
515
"N vertices submitted");
516
intel_perf_query_add_basic_stat_reg(query, IA_PRIMITIVES_COUNT,
517
"N primitives submitted");
518
intel_perf_query_add_basic_stat_reg(query, VS_INVOCATION_COUNT,
519
"N vertex shader invocations");
520
521
if (devinfo->ver == 6) {
522
intel_perf_query_add_stat_reg(query, GFX6_SO_PRIM_STORAGE_NEEDED, 1, 1,
523
"SO_PRIM_STORAGE_NEEDED",
524
"N geometry shader stream-out primitives (total)");
525
intel_perf_query_add_stat_reg(query, GFX6_SO_NUM_PRIMS_WRITTEN, 1, 1,
526
"SO_NUM_PRIMS_WRITTEN",
527
"N geometry shader stream-out primitives (written)");
528
} else {
529
intel_perf_query_add_stat_reg(query, GFX7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
530
"SO_PRIM_STORAGE_NEEDED (Stream 0)",
531
"N stream-out (stream 0) primitives (total)");
532
intel_perf_query_add_stat_reg(query, GFX7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
533
"SO_PRIM_STORAGE_NEEDED (Stream 1)",
534
"N stream-out (stream 1) primitives (total)");
535
intel_perf_query_add_stat_reg(query, GFX7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
536
"SO_PRIM_STORAGE_NEEDED (Stream 2)",
537
"N stream-out (stream 2) primitives (total)");
538
intel_perf_query_add_stat_reg(query, GFX7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
539
"SO_PRIM_STORAGE_NEEDED (Stream 3)",
540
"N stream-out (stream 3) primitives (total)");
541
intel_perf_query_add_stat_reg(query, GFX7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
542
"SO_NUM_PRIMS_WRITTEN (Stream 0)",
543
"N stream-out (stream 0) primitives (written)");
544
intel_perf_query_add_stat_reg(query, GFX7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
545
"SO_NUM_PRIMS_WRITTEN (Stream 1)",
546
"N stream-out (stream 1) primitives (written)");
547
intel_perf_query_add_stat_reg(query, GFX7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
548
"SO_NUM_PRIMS_WRITTEN (Stream 2)",
549
"N stream-out (stream 2) primitives (written)");
550
intel_perf_query_add_stat_reg(query, GFX7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
551
"SO_NUM_PRIMS_WRITTEN (Stream 3)",
552
"N stream-out (stream 3) primitives (written)");
553
}
554
555
intel_perf_query_add_basic_stat_reg(query, HS_INVOCATION_COUNT,
556
"N TCS shader invocations");
557
intel_perf_query_add_basic_stat_reg(query, DS_INVOCATION_COUNT,
558
"N TES shader invocations");
559
560
intel_perf_query_add_basic_stat_reg(query, GS_INVOCATION_COUNT,
561
"N geometry shader invocations");
562
intel_perf_query_add_basic_stat_reg(query, GS_PRIMITIVES_COUNT,
563
"N geometry shader primitives emitted");
564
565
intel_perf_query_add_basic_stat_reg(query, CL_INVOCATION_COUNT,
566
"N primitives entering clipping");
567
intel_perf_query_add_basic_stat_reg(query, CL_PRIMITIVES_COUNT,
568
"N primitives leaving clipping");
569
570
if (devinfo->is_haswell || devinfo->ver == 8) {
571
intel_perf_query_add_stat_reg(query, PS_INVOCATION_COUNT, 1, 4,
572
"N fragment shader invocations",
573
"N fragment shader invocations");
574
} else {
575
intel_perf_query_add_basic_stat_reg(query, PS_INVOCATION_COUNT,
576
"N fragment shader invocations");
577
}
578
579
intel_perf_query_add_basic_stat_reg(query, PS_DEPTH_COUNT,
580
"N z-pass fragments");
581
582
if (devinfo->ver >= 7) {
583
intel_perf_query_add_basic_stat_reg(query, CS_INVOCATION_COUNT,
584
"N compute shader invocations");
585
}
586
587
query->data_size = sizeof(uint64_t) * query->n_counters;
588
589
sort_query(query);
590
}
591
592
static int
593
i915_perf_version(int drm_fd)
594
{
595
int tmp;
596
drm_i915_getparam_t gp = {
597
.param = I915_PARAM_PERF_REVISION,
598
.value = &tmp,
599
};
600
601
int ret = intel_ioctl(drm_fd, DRM_IOCTL_I915_GETPARAM, &gp);
602
603
/* Return 0 if this getparam is not supported, the first version supported
604
* is 1.
605
*/
606
return ret < 0 ? 0 : tmp;
607
}
608
609
static void
610
i915_get_sseu(int drm_fd, struct drm_i915_gem_context_param_sseu *sseu)
611
{
612
struct drm_i915_gem_context_param arg = {
613
.param = I915_CONTEXT_PARAM_SSEU,
614
.size = sizeof(*sseu),
615
.value = to_user_pointer(sseu)
616
};
617
618
intel_ioctl(drm_fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &arg);
619
}
620
621
static inline int
622
compare_str_or_null(const char *s1, const char *s2)
623
{
624
if (s1 == NULL && s2 == NULL)
625
return 0;
626
if (s1 == NULL)
627
return -1;
628
if (s2 == NULL)
629
return 1;
630
631
return strcmp(s1, s2);
632
}
633
634
static int
635
compare_counter_categories_and_names(const void *_c1, const void *_c2)
636
{
637
const struct intel_perf_query_counter_info *c1 = (const struct intel_perf_query_counter_info *)_c1;
638
const struct intel_perf_query_counter_info *c2 = (const struct intel_perf_query_counter_info *)_c2;
639
640
/* pipeline counters don't have an assigned category */
641
int r = compare_str_or_null(c1->counter->category, c2->counter->category);
642
if (r)
643
return r;
644
645
return strcmp(c1->counter->name, c2->counter->name);
646
}
647
648
static void
649
build_unique_counter_list(struct intel_perf_config *perf)
650
{
651
assert(perf->n_queries < 64);
652
653
size_t max_counters = 0;
654
655
for (int q = 0; q < perf->n_queries; q++)
656
max_counters += perf->queries[q].n_counters;
657
658
/*
659
* Allocate big enough array to hold maximum possible number of counters.
660
* We can't alloc it small and realloc when needed because the hash table
661
* below contains pointers to this array.
662
*/
663
struct intel_perf_query_counter_info *counter_infos =
664
ralloc_array_size(perf, sizeof(counter_infos[0]), max_counters);
665
666
perf->n_counters = 0;
667
668
struct hash_table *counters_table =
669
_mesa_hash_table_create(perf,
670
_mesa_hash_string,
671
_mesa_key_string_equal);
672
struct hash_entry *entry;
673
for (int q = 0; q < perf->n_queries ; q++) {
674
struct intel_perf_query_info *query = &perf->queries[q];
675
676
for (int c = 0; c < query->n_counters; c++) {
677
struct intel_perf_query_counter *counter;
678
struct intel_perf_query_counter_info *counter_info;
679
680
counter = &query->counters[c];
681
entry = _mesa_hash_table_search(counters_table, counter->symbol_name);
682
683
if (entry) {
684
counter_info = entry->data;
685
counter_info->query_mask |= BITFIELD64_BIT(q);
686
continue;
687
}
688
assert(perf->n_counters < max_counters);
689
690
counter_info = &counter_infos[perf->n_counters++];
691
counter_info->counter = counter;
692
counter_info->query_mask = BITFIELD64_BIT(q);
693
694
counter_info->location.group_idx = q;
695
counter_info->location.counter_idx = c;
696
697
_mesa_hash_table_insert(counters_table, counter->symbol_name, counter_info);
698
}
699
}
700
701
_mesa_hash_table_destroy(counters_table, NULL);
702
703
/* Now we can realloc counter_infos array because hash table doesn't exist. */
704
perf->counter_infos = reralloc_array_size(perf, counter_infos,
705
sizeof(counter_infos[0]), perf->n_counters);
706
707
qsort(perf->counter_infos, perf->n_counters, sizeof(perf->counter_infos[0]),
708
compare_counter_categories_and_names);
709
}
710
711
static bool
712
oa_metrics_available(struct intel_perf_config *perf, int fd,
713
const struct intel_device_info *devinfo,
714
bool use_register_snapshots)
715
{
716
perf_register_oa_queries_t oa_register = get_register_queries_function(devinfo);
717
bool i915_perf_oa_available = false;
718
struct stat sb;
719
720
perf->i915_query_supported = i915_query_perf_config_supported(perf, fd);
721
perf->i915_perf_version = i915_perf_version(fd);
722
723
/* Record the default SSEU configuration. */
724
i915_get_sseu(fd, &perf->sseu);
725
726
/* The existence of this sysctl parameter implies the kernel supports
727
* the i915 perf interface.
728
*/
729
if (stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb) == 0) {
730
731
/* If _paranoid == 1 then on Gfx8+ we won't be able to access OA
732
* metrics unless running as root.
733
*/
734
if (devinfo->is_haswell)
735
i915_perf_oa_available = true;
736
else {
737
uint64_t paranoid = 1;
738
739
read_file_uint64("/proc/sys/dev/i915/perf_stream_paranoid", &paranoid);
740
741
if (paranoid == 0 || geteuid() == 0)
742
i915_perf_oa_available = true;
743
}
744
745
perf->platform_supported = oa_register != NULL;
746
}
747
748
return i915_perf_oa_available &&
749
oa_register &&
750
get_sysfs_dev_dir(perf, fd) &&
751
init_oa_sys_vars(perf, devinfo, use_register_snapshots);
752
}
753
754
static void
755
load_oa_metrics(struct intel_perf_config *perf, int fd,
756
const struct intel_device_info *devinfo)
757
{
758
int existing_queries = perf->n_queries;
759
760
perf_register_oa_queries_t oa_register = get_register_queries_function(devinfo);
761
762
perf->oa_metrics_table =
763
_mesa_hash_table_create(perf, _mesa_hash_string,
764
_mesa_key_string_equal);
765
766
/* Index all the metric sets mesa knows about before looking to see what
767
* the kernel is advertising.
768
*/
769
oa_register(perf);
770
771
if (!(INTEL_DEBUG & DEBUG_NO_OACONFIG)) {
772
if (kernel_has_dynamic_config_support(perf, fd))
773
init_oa_configs(perf, fd, devinfo);
774
else
775
enumerate_sysfs_metrics(perf, devinfo);
776
} else {
777
add_all_metrics(perf, devinfo);
778
}
779
780
/* sort counters in each individual group created by this function by name */
781
for (int i = existing_queries; i < perf->n_queries; ++i)
782
sort_query(&perf->queries[i]);
783
784
/* Select a fallback OA metric. Look for the TestOa metric or use the last
785
* one if no present (on HSW).
786
*/
787
for (int i = existing_queries; i < perf->n_queries; i++) {
788
if (perf->queries[i].symbol_name &&
789
strcmp(perf->queries[i].symbol_name, "TestOa") == 0) {
790
perf->fallback_raw_oa_metric = perf->queries[i].oa_metrics_set_id;
791
break;
792
}
793
}
794
if (perf->fallback_raw_oa_metric == 0 && perf->n_queries > 0)
795
perf->fallback_raw_oa_metric = perf->queries[perf->n_queries - 1].oa_metrics_set_id;
796
}
797
798
struct intel_perf_registers *
799
intel_perf_load_configuration(struct intel_perf_config *perf_cfg, int fd, const char *guid)
800
{
801
if (!perf_cfg->i915_query_supported)
802
return NULL;
803
804
struct drm_i915_perf_oa_config i915_config = { 0, };
805
if (!i915_query_perf_config_data(perf_cfg, fd, guid, &i915_config))
806
return NULL;
807
808
struct intel_perf_registers *config = rzalloc(NULL, struct intel_perf_registers);
809
config->n_flex_regs = i915_config.n_flex_regs;
810
config->flex_regs = rzalloc_array(config, struct intel_perf_query_register_prog, config->n_flex_regs);
811
config->n_mux_regs = i915_config.n_mux_regs;
812
config->mux_regs = rzalloc_array(config, struct intel_perf_query_register_prog, config->n_mux_regs);
813
config->n_b_counter_regs = i915_config.n_boolean_regs;
814
config->b_counter_regs = rzalloc_array(config, struct intel_perf_query_register_prog, config->n_b_counter_regs);
815
816
/*
817
* struct intel_perf_query_register_prog maps exactly to the tuple of
818
* (register offset, register value) returned by the i915.
819
*/
820
i915_config.flex_regs_ptr = to_const_user_pointer(config->flex_regs);
821
i915_config.mux_regs_ptr = to_const_user_pointer(config->mux_regs);
822
i915_config.boolean_regs_ptr = to_const_user_pointer(config->b_counter_regs);
823
if (!i915_query_perf_config_data(perf_cfg, fd, guid, &i915_config)) {
824
ralloc_free(config);
825
return NULL;
826
}
827
828
return config;
829
}
830
831
uint64_t
832
intel_perf_store_configuration(struct intel_perf_config *perf_cfg, int fd,
833
const struct intel_perf_registers *config,
834
const char *guid)
835
{
836
if (guid)
837
return i915_add_config(perf_cfg, fd, config, guid);
838
839
struct mesa_sha1 sha1_ctx;
840
_mesa_sha1_init(&sha1_ctx);
841
842
if (config->flex_regs) {
843
_mesa_sha1_update(&sha1_ctx, config->flex_regs,
844
sizeof(config->flex_regs[0]) *
845
config->n_flex_regs);
846
}
847
if (config->mux_regs) {
848
_mesa_sha1_update(&sha1_ctx, config->mux_regs,
849
sizeof(config->mux_regs[0]) *
850
config->n_mux_regs);
851
}
852
if (config->b_counter_regs) {
853
_mesa_sha1_update(&sha1_ctx, config->b_counter_regs,
854
sizeof(config->b_counter_regs[0]) *
855
config->n_b_counter_regs);
856
}
857
858
uint8_t hash[20];
859
_mesa_sha1_final(&sha1_ctx, hash);
860
861
char formatted_hash[41];
862
_mesa_sha1_format(formatted_hash, hash);
863
864
char generated_guid[37];
865
snprintf(generated_guid, sizeof(generated_guid),
866
"%.8s-%.4s-%.4s-%.4s-%.12s",
867
&formatted_hash[0], &formatted_hash[8],
868
&formatted_hash[8 + 4], &formatted_hash[8 + 4 + 4],
869
&formatted_hash[8 + 4 + 4 + 4]);
870
871
/* Check if already present. */
872
uint64_t id;
873
if (intel_perf_load_metric_id(perf_cfg, generated_guid, &id))
874
return id;
875
876
return i915_add_config(perf_cfg, fd, config, generated_guid);
877
}
878
879
static uint64_t
880
get_passes_mask(struct intel_perf_config *perf,
881
const uint32_t *counter_indices,
882
uint32_t counter_indices_count)
883
{
884
uint64_t queries_mask = 0;
885
886
assert(perf->n_queries < 64);
887
888
/* Compute the number of passes by going through all counters N times (with
889
* N the number of queries) to make sure we select the most constraining
890
* counters first and look at the more flexible ones (that could be
891
* obtained from multiple queries) later. That way we minimize the number
892
* of passes required.
893
*/
894
for (uint32_t q = 0; q < perf->n_queries; q++) {
895
for (uint32_t i = 0; i < counter_indices_count; i++) {
896
assert(counter_indices[i] < perf->n_counters);
897
898
uint32_t idx = counter_indices[i];
899
if (util_bitcount64(perf->counter_infos[idx].query_mask) != (q + 1))
900
continue;
901
902
if (queries_mask & perf->counter_infos[idx].query_mask)
903
continue;
904
905
queries_mask |= BITFIELD64_BIT(ffsll(perf->counter_infos[idx].query_mask) - 1);
906
}
907
}
908
909
return queries_mask;
910
}
911
912
uint32_t
913
intel_perf_get_n_passes(struct intel_perf_config *perf,
914
const uint32_t *counter_indices,
915
uint32_t counter_indices_count,
916
struct intel_perf_query_info **pass_queries)
917
{
918
uint64_t queries_mask = get_passes_mask(perf, counter_indices, counter_indices_count);
919
920
if (pass_queries) {
921
uint32_t pass = 0;
922
for (uint32_t q = 0; q < perf->n_queries; q++) {
923
if ((1ULL << q) & queries_mask)
924
pass_queries[pass++] = &perf->queries[q];
925
}
926
}
927
928
return util_bitcount64(queries_mask);
929
}
930
931
void
932
intel_perf_get_counters_passes(struct intel_perf_config *perf,
933
const uint32_t *counter_indices,
934
uint32_t counter_indices_count,
935
struct intel_perf_counter_pass *counter_pass)
936
{
937
uint64_t queries_mask = get_passes_mask(perf, counter_indices, counter_indices_count);
938
ASSERTED uint32_t n_passes = util_bitcount64(queries_mask);
939
940
for (uint32_t i = 0; i < counter_indices_count; i++) {
941
assert(counter_indices[i] < perf->n_counters);
942
943
uint32_t idx = counter_indices[i];
944
counter_pass[i].counter = perf->counter_infos[idx].counter;
945
946
uint32_t query_idx = ffsll(perf->counter_infos[idx].query_mask & queries_mask) - 1;
947
counter_pass[i].query = &perf->queries[query_idx];
948
949
uint32_t clear_bits = 63 - query_idx;
950
counter_pass[i].pass = util_bitcount64((queries_mask << clear_bits) >> clear_bits) - 1;
951
assert(counter_pass[i].pass < n_passes);
952
}
953
}
954
955
/* Accumulate 32bits OA counters */
956
static inline void
957
accumulate_uint32(const uint32_t *report0,
958
const uint32_t *report1,
959
uint64_t *accumulator)
960
{
961
*accumulator += (uint32_t)(*report1 - *report0);
962
}
963
964
/* Accumulate 40bits OA counters */
965
static inline void
966
accumulate_uint40(int a_index,
967
const uint32_t *report0,
968
const uint32_t *report1,
969
uint64_t *accumulator)
970
{
971
const uint8_t *high_bytes0 = (uint8_t *)(report0 + 40);
972
const uint8_t *high_bytes1 = (uint8_t *)(report1 + 40);
973
uint64_t high0 = (uint64_t)(high_bytes0[a_index]) << 32;
974
uint64_t high1 = (uint64_t)(high_bytes1[a_index]) << 32;
975
uint64_t value0 = report0[a_index + 4] | high0;
976
uint64_t value1 = report1[a_index + 4] | high1;
977
uint64_t delta;
978
979
if (value0 > value1)
980
delta = (1ULL << 40) + value1 - value0;
981
else
982
delta = value1 - value0;
983
984
*accumulator += delta;
985
}
986
987
static void
988
gfx8_read_report_clock_ratios(const uint32_t *report,
989
uint64_t *slice_freq_hz,
990
uint64_t *unslice_freq_hz)
991
{
992
/* The lower 16bits of the RPT_ID field of the OA reports contains a
993
* snapshot of the bits coming from the RP_FREQ_NORMAL register and is
994
* divided this way :
995
*
996
* RPT_ID[31:25]: RP_FREQ_NORMAL[20:14] (low squashed_slice_clock_frequency)
997
* RPT_ID[10:9]: RP_FREQ_NORMAL[22:21] (high squashed_slice_clock_frequency)
998
* RPT_ID[8:0]: RP_FREQ_NORMAL[31:23] (squashed_unslice_clock_frequency)
999
*
1000
* RP_FREQ_NORMAL[31:23]: Software Unslice Ratio Request
1001
* Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
1002
*
1003
* RP_FREQ_NORMAL[22:14]: Software Slice Ratio Request
1004
* Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
1005
*/
1006
1007
uint32_t unslice_freq = report[0] & 0x1ff;
1008
uint32_t slice_freq_low = (report[0] >> 25) & 0x7f;
1009
uint32_t slice_freq_high = (report[0] >> 9) & 0x3;
1010
uint32_t slice_freq = slice_freq_low | (slice_freq_high << 7);
1011
1012
*slice_freq_hz = slice_freq * 16666667ULL;
1013
*unslice_freq_hz = unslice_freq * 16666667ULL;
1014
}
1015
1016
void
1017
intel_perf_query_result_read_frequencies(struct intel_perf_query_result *result,
1018
const struct intel_device_info *devinfo,
1019
const uint32_t *start,
1020
const uint32_t *end)
1021
{
1022
/* Slice/Unslice frequency is only available in the OA reports when the
1023
* "Disable OA reports due to clock ratio change" field in
1024
* OA_DEBUG_REGISTER is set to 1. This is how the kernel programs this
1025
* global register (see drivers/gpu/drm/i915/i915_perf.c)
1026
*
1027
* Documentation says this should be available on Gfx9+ but experimentation
1028
* shows that Gfx8 reports similar values, so we enable it there too.
1029
*/
1030
if (devinfo->ver < 8)
1031
return;
1032
1033
gfx8_read_report_clock_ratios(start,
1034
&result->slice_frequency[0],
1035
&result->unslice_frequency[0]);
1036
gfx8_read_report_clock_ratios(end,
1037
&result->slice_frequency[1],
1038
&result->unslice_frequency[1]);
1039
}
1040
1041
static inline bool
1042
can_use_mi_rpc_bc_counters(const struct intel_device_info *devinfo)
1043
{
1044
return devinfo->ver <= 11;
1045
}
1046
1047
void
1048
intel_perf_query_result_accumulate(struct intel_perf_query_result *result,
1049
const struct intel_perf_query_info *query,
1050
const struct intel_device_info *devinfo,
1051
const uint32_t *start,
1052
const uint32_t *end)
1053
{
1054
int i;
1055
1056
if (result->hw_id == INTEL_PERF_INVALID_CTX_ID &&
1057
start[2] != INTEL_PERF_INVALID_CTX_ID)
1058
result->hw_id = start[2];
1059
if (result->reports_accumulated == 0)
1060
result->begin_timestamp = start[1];
1061
result->reports_accumulated++;
1062
1063
switch (query->oa_format) {
1064
case I915_OA_FORMAT_A32u40_A4u32_B8_C8:
1065
accumulate_uint32(start + 1, end + 1,
1066
result->accumulator + query->gpu_time_offset); /* timestamp */
1067
accumulate_uint32(start + 3, end + 3,
1068
result->accumulator + query->gpu_clock_offset); /* clock */
1069
1070
/* 32x 40bit A counters... */
1071
for (i = 0; i < 32; i++) {
1072
accumulate_uint40(i, start, end,
1073
result->accumulator + query->a_offset + i);
1074
}
1075
1076
/* 4x 32bit A counters... */
1077
for (i = 0; i < 4; i++) {
1078
accumulate_uint32(start + 36 + i, end + 36 + i,
1079
result->accumulator + query->a_offset + 32 + i);
1080
}
1081
1082
if (can_use_mi_rpc_bc_counters(devinfo)) {
1083
/* 8x 32bit B counters */
1084
for (i = 0; i < 8; i++) {
1085
accumulate_uint32(start + 48 + i, end + 48 + i,
1086
result->accumulator + query->b_offset + i);
1087
}
1088
1089
/* 8x 32bit C counters... */
1090
for (i = 0; i < 8; i++) {
1091
accumulate_uint32(start + 56 + i, end + 56 + i,
1092
result->accumulator + query->c_offset + i);
1093
}
1094
}
1095
break;
1096
1097
case I915_OA_FORMAT_A45_B8_C8:
1098
accumulate_uint32(start + 1, end + 1, result->accumulator); /* timestamp */
1099
1100
for (i = 0; i < 61; i++) {
1101
accumulate_uint32(start + 3 + i, end + 3 + i,
1102
result->accumulator + query->a_offset + i);
1103
}
1104
break;
1105
1106
default:
1107
unreachable("Can't accumulate OA counters in unknown format");
1108
}
1109
1110
}
1111
1112
#define GET_FIELD(word, field) (((word) & field ## _MASK) >> field ## _SHIFT)
1113
1114
void
1115
intel_perf_query_result_read_gt_frequency(struct intel_perf_query_result *result,
1116
const struct intel_device_info *devinfo,
1117
const uint32_t start,
1118
const uint32_t end)
1119
{
1120
switch (devinfo->ver) {
1121
case 7:
1122
case 8:
1123
result->gt_frequency[0] = GET_FIELD(start, GFX7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
1124
result->gt_frequency[1] = GET_FIELD(end, GFX7_RPSTAT1_CURR_GT_FREQ) * 50ULL;
1125
break;
1126
case 9:
1127
case 11:
1128
case 12:
1129
result->gt_frequency[0] = GET_FIELD(start, GFX9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
1130
result->gt_frequency[1] = GET_FIELD(end, GFX9_RPSTAT0_CURR_GT_FREQ) * 50ULL / 3ULL;
1131
break;
1132
default:
1133
unreachable("unexpected gen");
1134
}
1135
1136
/* Put the numbers into Hz. */
1137
result->gt_frequency[0] *= 1000000ULL;
1138
result->gt_frequency[1] *= 1000000ULL;
1139
}
1140
1141
void
1142
intel_perf_query_result_read_perfcnts(struct intel_perf_query_result *result,
1143
const struct intel_perf_query_info *query,
1144
const uint64_t *start,
1145
const uint64_t *end)
1146
{
1147
for (uint32_t i = 0; i < 2; i++) {
1148
uint64_t v0 = start[i] & PERF_CNT_VALUE_MASK;
1149
uint64_t v1 = end[i] & PERF_CNT_VALUE_MASK;
1150
1151
result->accumulator[query->perfcnt_offset + i] = v0 > v1 ?
1152
(PERF_CNT_VALUE_MASK + 1 + v1 - v0) :
1153
(v1 - v0);
1154
}
1155
}
1156
1157
static uint32_t
1158
query_accumulator_offset(const struct intel_perf_query_info *query,
1159
enum intel_perf_query_field_type type,
1160
uint8_t index)
1161
{
1162
switch (type) {
1163
case INTEL_PERF_QUERY_FIELD_TYPE_SRM_PERFCNT:
1164
return query->perfcnt_offset + index;
1165
case INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_B:
1166
return query->b_offset + index;
1167
case INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_C:
1168
return query->c_offset + index;
1169
default:
1170
unreachable("Invalid register type");
1171
return 0;
1172
}
1173
}
1174
1175
void
1176
intel_perf_query_result_accumulate_fields(struct intel_perf_query_result *result,
1177
const struct intel_perf_query_info *query,
1178
const struct intel_device_info *devinfo,
1179
const void *start,
1180
const void *end,
1181
bool no_oa_accumulate)
1182
{
1183
struct intel_perf_query_field_layout *layout = &query->perf->query_layout;
1184
1185
for (uint32_t r = 0; r < layout->n_fields; r++) {
1186
struct intel_perf_query_field *field = &layout->fields[r];
1187
1188
if (field->type == INTEL_PERF_QUERY_FIELD_TYPE_MI_RPC) {
1189
intel_perf_query_result_read_frequencies(result, devinfo,
1190
start + field->location,
1191
end + field->location);
1192
/* no_oa_accumulate=true is used when doing GL perf queries, we
1193
* manually parse the OA reports from the OA buffer and substract
1194
* unrelated deltas, so don't accumulate the begin/end reports here.
1195
*/
1196
if (!no_oa_accumulate) {
1197
intel_perf_query_result_accumulate(result, query, devinfo,
1198
start + field->location,
1199
end + field->location);
1200
}
1201
} else {
1202
uint64_t v0, v1;
1203
1204
if (field->size == 4) {
1205
v0 = *(const uint32_t *)(start + field->location);
1206
v1 = *(const uint32_t *)(end + field->location);
1207
} else {
1208
assert(field->size == 8);
1209
v0 = *(const uint64_t *)(start + field->location);
1210
v1 = *(const uint64_t *)(end + field->location);
1211
}
1212
1213
if (field->mask) {
1214
v0 = field->mask & v0;
1215
v1 = field->mask & v1;
1216
}
1217
1218
/* RPSTAT is a bit of a special case because its begin/end values
1219
* represent frequencies. We store it in a separate location.
1220
*/
1221
if (field->type == INTEL_PERF_QUERY_FIELD_TYPE_SRM_RPSTAT)
1222
intel_perf_query_result_read_gt_frequency(result, devinfo, v0, v1);
1223
else
1224
result->accumulator[query_accumulator_offset(query, field->type, field->index)] = v1 - v0;
1225
}
1226
}
1227
}
1228
1229
void
1230
intel_perf_query_result_clear(struct intel_perf_query_result *result)
1231
{
1232
memset(result, 0, sizeof(*result));
1233
result->hw_id = INTEL_PERF_INVALID_CTX_ID;
1234
}
1235
1236
void
1237
intel_perf_query_result_print_fields(const struct intel_perf_query_info *query,
1238
const struct intel_device_info *devinfo,
1239
const void *data)
1240
{
1241
const struct intel_perf_query_field_layout *layout = &query->perf->query_layout;
1242
1243
for (uint32_t r = 0; r < layout->n_fields; r++) {
1244
const struct intel_perf_query_field *field = &layout->fields[r];
1245
const uint32_t *value32 = data + field->location;
1246
1247
switch (field->type) {
1248
case INTEL_PERF_QUERY_FIELD_TYPE_MI_RPC:
1249
fprintf(stderr, "MI_RPC:\n");
1250
fprintf(stderr, " TS: 0x%08x\n", *(value32 + 1));
1251
fprintf(stderr, " CLK: 0x%08x\n", *(value32 + 3));
1252
break;
1253
case INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_B:
1254
fprintf(stderr, "B%u: 0x%08x\n", field->index, *value32);
1255
break;
1256
case INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_C:
1257
fprintf(stderr, "C%u: 0x%08x\n", field->index, *value32);
1258
break;
1259
default:
1260
break;
1261
}
1262
}
1263
}
1264
1265
static int
1266
intel_perf_compare_query_names(const void *v1, const void *v2)
1267
{
1268
const struct intel_perf_query_info *q1 = v1;
1269
const struct intel_perf_query_info *q2 = v2;
1270
1271
return strcmp(q1->name, q2->name);
1272
}
1273
1274
static inline struct intel_perf_query_field *
1275
add_query_register(struct intel_perf_query_field_layout *layout,
1276
enum intel_perf_query_field_type type,
1277
uint16_t offset,
1278
uint16_t size,
1279
uint8_t index)
1280
{
1281
/* Align MI_RPC to 64bytes (HW requirement) & 64bit registers to 8bytes
1282
* (shows up nicely in the debugger).
1283
*/
1284
if (type == INTEL_PERF_QUERY_FIELD_TYPE_MI_RPC)
1285
layout->size = align(layout->size, 64);
1286
else if (size % 8 == 0)
1287
layout->size = align(layout->size, 8);
1288
1289
layout->fields[layout->n_fields++] = (struct intel_perf_query_field) {
1290
.mmio_offset = offset,
1291
.location = layout->size,
1292
.type = type,
1293
.index = index,
1294
.size = size,
1295
};
1296
layout->size += size;
1297
1298
return &layout->fields[layout->n_fields - 1];
1299
}
1300
1301
static void
1302
intel_perf_init_query_fields(struct intel_perf_config *perf_cfg,
1303
const struct intel_device_info *devinfo,
1304
bool use_register_snapshots)
1305
{
1306
struct intel_perf_query_field_layout *layout = &perf_cfg->query_layout;
1307
1308
layout->n_fields = 0;
1309
1310
/* MI_RPC requires a 64byte alignment. */
1311
layout->alignment = 64;
1312
1313
layout->fields = rzalloc_array(perf_cfg, struct intel_perf_query_field, 5 + 16);
1314
1315
add_query_register(layout, INTEL_PERF_QUERY_FIELD_TYPE_MI_RPC,
1316
0, 256, 0);
1317
1318
if (use_register_snapshots) {
1319
if (devinfo->ver <= 11) {
1320
struct intel_perf_query_field *field =
1321
add_query_register(layout,
1322
INTEL_PERF_QUERY_FIELD_TYPE_SRM_PERFCNT,
1323
PERF_CNT_1_DW0, 8, 0);
1324
field->mask = PERF_CNT_VALUE_MASK;
1325
1326
field = add_query_register(layout,
1327
INTEL_PERF_QUERY_FIELD_TYPE_SRM_PERFCNT,
1328
PERF_CNT_2_DW0, 8, 1);
1329
field->mask = PERF_CNT_VALUE_MASK;
1330
}
1331
1332
if (devinfo->ver == 8 && !devinfo->is_cherryview) {
1333
add_query_register(layout,
1334
INTEL_PERF_QUERY_FIELD_TYPE_SRM_RPSTAT,
1335
GFX7_RPSTAT1, 4, 0);
1336
}
1337
1338
if (devinfo->ver >= 9) {
1339
add_query_register(layout,
1340
INTEL_PERF_QUERY_FIELD_TYPE_SRM_RPSTAT,
1341
GFX9_RPSTAT0, 4, 0);
1342
}
1343
1344
if (!can_use_mi_rpc_bc_counters(devinfo)) {
1345
if (devinfo->ver >= 8 && devinfo->ver <= 11) {
1346
for (uint32_t i = 0; i < GFX8_N_OA_PERF_B32; i++) {
1347
add_query_register(layout, INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_B,
1348
GFX8_OA_PERF_B32(i), 4, i);
1349
}
1350
for (uint32_t i = 0; i < GFX8_N_OA_PERF_C32; i++) {
1351
add_query_register(layout, INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_C,
1352
GFX8_OA_PERF_C32(i), 4, i);
1353
}
1354
} else if (devinfo->ver == 12) {
1355
for (uint32_t i = 0; i < GFX12_N_OAG_PERF_B32; i++) {
1356
add_query_register(layout, INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_B,
1357
GFX12_OAG_PERF_B32(i), 4, i);
1358
}
1359
for (uint32_t i = 0; i < GFX12_N_OAG_PERF_C32; i++) {
1360
add_query_register(layout, INTEL_PERF_QUERY_FIELD_TYPE_SRM_OA_C,
1361
GFX12_OAG_PERF_C32(i), 4, i);
1362
}
1363
}
1364
}
1365
}
1366
1367
/* Align the whole package to 64bytes so that 2 snapshots can be put
1368
* together without extract alignment for the user.
1369
*/
1370
layout->size = align(layout->size, 64);
1371
}
1372
1373
void
1374
intel_perf_init_metrics(struct intel_perf_config *perf_cfg,
1375
const struct intel_device_info *devinfo,
1376
int drm_fd,
1377
bool include_pipeline_statistics,
1378
bool use_register_snapshots)
1379
{
1380
intel_perf_init_query_fields(perf_cfg, devinfo, use_register_snapshots);
1381
1382
if (include_pipeline_statistics) {
1383
load_pipeline_statistic_metrics(perf_cfg, devinfo);
1384
intel_perf_register_mdapi_statistic_query(perf_cfg, devinfo);
1385
}
1386
1387
bool oa_metrics = oa_metrics_available(perf_cfg, drm_fd, devinfo,
1388
use_register_snapshots);
1389
if (oa_metrics)
1390
load_oa_metrics(perf_cfg, drm_fd, devinfo);
1391
1392
/* sort query groups by name */
1393
qsort(perf_cfg->queries, perf_cfg->n_queries,
1394
sizeof(perf_cfg->queries[0]), intel_perf_compare_query_names);
1395
1396
build_unique_counter_list(perf_cfg);
1397
1398
if (oa_metrics)
1399
intel_perf_register_mdapi_oa_query(perf_cfg, devinfo);
1400
}
1401
1402