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