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/gen_gem.h"
39
40 #include "dev/gen_debug.h"
41 #include "dev/gen_device_info.h"
42
43 #include "perf/gen_perf.h"
44 #include "perf/gen_perf_regs.h"
45 #include "perf/gen_perf_mdapi.h"
46 #include "perf/gen_perf_metrics.h"
47 #include "perf/gen_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 #define OA_REPORT_INVALID_CTX_ID (0xffffffff)
57
58 static bool
is_dir_or_link(const struct dirent * entry,const char * parent_dir)59 is_dir_or_link(const struct dirent *entry, const char *parent_dir)
60 {
61 #ifdef HAVE_DIRENT_D_TYPE
62 return entry->d_type == DT_DIR || entry->d_type == DT_LNK;
63 #else
64 struct stat st;
65 char path[PATH_MAX + 1];
66 snprintf(path, sizeof(path), "%s/%s", parent_dir, entry->d_name);
67 lstat(path, &st);
68 return S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode);
69 #endif
70 }
71
72 static bool
get_sysfs_dev_dir(struct gen_perf_config * perf,int fd)73 get_sysfs_dev_dir(struct gen_perf_config *perf, int fd)
74 {
75 struct stat sb;
76 int min, maj;
77 DIR *drmdir;
78 struct dirent *drm_entry;
79 int len;
80
81 perf->sysfs_dev_dir[0] = '\0';
82
83 if (INTEL_DEBUG & DEBUG_NO_OACONFIG)
84 return true;
85
86 if (fstat(fd, &sb)) {
87 DBG("Failed to stat DRM fd\n");
88 return false;
89 }
90
91 maj = major(sb.st_rdev);
92 min = minor(sb.st_rdev);
93
94 if (!S_ISCHR(sb.st_mode)) {
95 DBG("DRM fd is not a character device as expected\n");
96 return false;
97 }
98
99 len = snprintf(perf->sysfs_dev_dir,
100 sizeof(perf->sysfs_dev_dir),
101 "/sys/dev/char/%d:%d/device/drm", maj, min);
102 if (len < 0 || len >= sizeof(perf->sysfs_dev_dir)) {
103 DBG("Failed to concatenate sysfs path to drm device\n");
104 return false;
105 }
106
107 drmdir = opendir(perf->sysfs_dev_dir);
108 if (!drmdir) {
109 DBG("Failed to open %s: %m\n", perf->sysfs_dev_dir);
110 return false;
111 }
112
113 while ((drm_entry = readdir(drmdir))) {
114 if (is_dir_or_link(drm_entry, perf->sysfs_dev_dir) &&
115 strncmp(drm_entry->d_name, "card", 4) == 0)
116 {
117 len = snprintf(perf->sysfs_dev_dir,
118 sizeof(perf->sysfs_dev_dir),
119 "/sys/dev/char/%d:%d/device/drm/%s",
120 maj, min, drm_entry->d_name);
121 closedir(drmdir);
122 if (len < 0 || len >= sizeof(perf->sysfs_dev_dir))
123 return false;
124 else
125 return true;
126 }
127 }
128
129 closedir(drmdir);
130
131 DBG("Failed to find cardX directory under /sys/dev/char/%d:%d/device/drm\n",
132 maj, min);
133
134 return false;
135 }
136
137 static bool
read_file_uint64(const char * file,uint64_t * val)138 read_file_uint64(const char *file, uint64_t *val)
139 {
140 char buf[32];
141 int fd, n;
142
143 fd = open(file, 0);
144 if (fd < 0)
145 return false;
146 while ((n = read(fd, buf, sizeof (buf) - 1)) < 0 &&
147 errno == EINTR);
148 close(fd);
149 if (n < 0)
150 return false;
151
152 buf[n] = '\0';
153 *val = strtoull(buf, NULL, 0);
154
155 return true;
156 }
157
158 static bool
read_sysfs_drm_device_file_uint64(struct gen_perf_config * perf,const char * file,uint64_t * value)159 read_sysfs_drm_device_file_uint64(struct gen_perf_config *perf,
160 const char *file,
161 uint64_t *value)
162 {
163 char buf[512];
164 int len;
165
166 len = snprintf(buf, sizeof(buf), "%s/%s", perf->sysfs_dev_dir, file);
167 if (len < 0 || len >= sizeof(buf)) {
168 DBG("Failed to concatenate sys filename to read u64 from\n");
169 return false;
170 }
171
172 return read_file_uint64(buf, value);
173 }
174
175 static void
register_oa_config(struct gen_perf_config * perf,const struct gen_device_info * devinfo,const struct gen_perf_query_info * query,uint64_t config_id)176 register_oa_config(struct gen_perf_config *perf,
177 const struct gen_device_info *devinfo,
178 const struct gen_perf_query_info *query,
179 uint64_t config_id)
180 {
181 struct gen_perf_query_info *registered_query =
182 gen_perf_append_query_info(perf, 0);
183
184 *registered_query = *query;
185 registered_query->oa_format = devinfo->gen >= 8 ?
186 I915_OA_FORMAT_A32u40_A4u32_B8_C8 : I915_OA_FORMAT_A45_B8_C8;
187 registered_query->oa_metrics_set_id = config_id;
188 DBG("metric set registered: id = %" PRIu64", guid = %s\n",
189 registered_query->oa_metrics_set_id, query->guid);
190 }
191
192 static void
enumerate_sysfs_metrics(struct gen_perf_config * perf,const struct gen_device_info * devinfo)193 enumerate_sysfs_metrics(struct gen_perf_config *perf,
194 const struct gen_device_info *devinfo)
195 {
196 DIR *metricsdir = NULL;
197 struct dirent *metric_entry;
198 char buf[256];
199 int len;
200
201 len = snprintf(buf, sizeof(buf), "%s/metrics", perf->sysfs_dev_dir);
202 if (len < 0 || len >= sizeof(buf)) {
203 DBG("Failed to concatenate path to sysfs metrics/ directory\n");
204 return;
205 }
206
207 metricsdir = opendir(buf);
208 if (!metricsdir) {
209 DBG("Failed to open %s: %m\n", buf);
210 return;
211 }
212
213 while ((metric_entry = readdir(metricsdir))) {
214 struct hash_entry *entry;
215 if (!is_dir_or_link(metric_entry, buf) ||
216 metric_entry->d_name[0] == '.')
217 continue;
218
219 DBG("metric set: %s\n", metric_entry->d_name);
220 entry = _mesa_hash_table_search(perf->oa_metrics_table,
221 metric_entry->d_name);
222 if (entry) {
223 uint64_t id;
224 if (!gen_perf_load_metric_id(perf, metric_entry->d_name, &id)) {
225 DBG("Failed to read metric set id from %s: %m", buf);
226 continue;
227 }
228
229 register_oa_config(perf, devinfo,
230 (const struct gen_perf_query_info *)entry->data, id);
231 } else
232 DBG("metric set not known by mesa (skipping)\n");
233 }
234
235 closedir(metricsdir);
236 }
237
238 static void
add_all_metrics(struct gen_perf_config * perf,const struct gen_device_info * devinfo)239 add_all_metrics(struct gen_perf_config *perf,
240 const struct gen_device_info *devinfo)
241 {
242 hash_table_foreach(perf->oa_metrics_table, entry) {
243 const struct gen_perf_query_info *query = entry->data;
244 register_oa_config(perf, devinfo, query, 0);
245 }
246 }
247
248 static bool
kernel_has_dynamic_config_support(struct gen_perf_config * perf,int fd)249 kernel_has_dynamic_config_support(struct gen_perf_config *perf, int fd)
250 {
251 uint64_t invalid_config_id = UINT64_MAX;
252
253 return gen_ioctl(fd, DRM_IOCTL_I915_PERF_REMOVE_CONFIG,
254 &invalid_config_id) < 0 && errno == ENOENT;
255 }
256
257 static int
i915_query_items(struct gen_perf_config * perf,int fd,struct drm_i915_query_item * items,uint32_t n_items)258 i915_query_items(struct gen_perf_config *perf, int fd,
259 struct drm_i915_query_item *items, uint32_t n_items)
260 {
261 struct drm_i915_query q = {
262 .num_items = n_items,
263 .items_ptr = to_user_pointer(items),
264 };
265 return gen_ioctl(fd, DRM_IOCTL_I915_QUERY, &q);
266 }
267
268 static bool
i915_query_perf_config_supported(struct gen_perf_config * perf,int fd)269 i915_query_perf_config_supported(struct gen_perf_config *perf, int fd)
270 {
271 struct drm_i915_query_item item = {
272 .query_id = DRM_I915_QUERY_PERF_CONFIG,
273 .flags = DRM_I915_QUERY_PERF_CONFIG_LIST,
274 };
275
276 return i915_query_items(perf, fd, &item, 1) == 0 && item.length > 0;
277 }
278
279 static bool
i915_query_perf_config_data(struct gen_perf_config * perf,int fd,const char * guid,struct drm_i915_perf_oa_config * config)280 i915_query_perf_config_data(struct gen_perf_config *perf,
281 int fd, const char *guid,
282 struct drm_i915_perf_oa_config *config)
283 {
284 struct {
285 struct drm_i915_query_perf_config query;
286 struct drm_i915_perf_oa_config config;
287 } item_data;
288 struct drm_i915_query_item item = {
289 .query_id = DRM_I915_QUERY_PERF_CONFIG,
290 .flags = DRM_I915_QUERY_PERF_CONFIG_DATA_FOR_UUID,
291 .data_ptr = to_user_pointer(&item_data),
292 .length = sizeof(item_data),
293 };
294
295 memset(&item_data, 0, sizeof(item_data));
296 memcpy(item_data.query.uuid, guid, sizeof(item_data.query.uuid));
297 memcpy(&item_data.config, config, sizeof(item_data.config));
298
299 if (!(i915_query_items(perf, fd, &item, 1) == 0 && item.length > 0))
300 return false;
301
302 memcpy(config, &item_data.config, sizeof(item_data.config));
303
304 return true;
305 }
306
307 bool
gen_perf_load_metric_id(struct gen_perf_config * perf_cfg,const char * guid,uint64_t * metric_id)308 gen_perf_load_metric_id(struct gen_perf_config *perf_cfg,
309 const char *guid,
310 uint64_t *metric_id)
311 {
312 char config_path[280];
313
314 snprintf(config_path, sizeof(config_path), "%s/metrics/%s/id",
315 perf_cfg->sysfs_dev_dir, guid);
316
317 /* Don't recreate already loaded configs. */
318 return read_file_uint64(config_path, metric_id);
319 }
320
321 static uint64_t
i915_add_config(struct gen_perf_config * perf,int fd,const struct gen_perf_registers * config,const char * guid)322 i915_add_config(struct gen_perf_config *perf, int fd,
323 const struct gen_perf_registers *config,
324 const char *guid)
325 {
326 struct drm_i915_perf_oa_config i915_config = { 0, };
327
328 memcpy(i915_config.uuid, guid, sizeof(i915_config.uuid));
329
330 i915_config.n_mux_regs = config->n_mux_regs;
331 i915_config.mux_regs_ptr = to_const_user_pointer(config->mux_regs);
332
333 i915_config.n_boolean_regs = config->n_b_counter_regs;
334 i915_config.boolean_regs_ptr = to_const_user_pointer(config->b_counter_regs);
335
336 i915_config.n_flex_regs = config->n_flex_regs;
337 i915_config.flex_regs_ptr = to_const_user_pointer(config->flex_regs);
338
339 int ret = gen_ioctl(fd, DRM_IOCTL_I915_PERF_ADD_CONFIG, &i915_config);
340 return ret > 0 ? ret : 0;
341 }
342
343 static void
init_oa_configs(struct gen_perf_config * perf,int fd,const struct gen_device_info * devinfo)344 init_oa_configs(struct gen_perf_config *perf, int fd,
345 const struct gen_device_info *devinfo)
346 {
347 hash_table_foreach(perf->oa_metrics_table, entry) {
348 const struct gen_perf_query_info *query = entry->data;
349 uint64_t config_id;
350
351 if (gen_perf_load_metric_id(perf, query->guid, &config_id)) {
352 DBG("metric set: %s (already loaded)\n", query->guid);
353 register_oa_config(perf, devinfo, query, config_id);
354 continue;
355 }
356
357 int ret = i915_add_config(perf, fd, &query->config, query->guid);
358 if (ret < 0) {
359 DBG("Failed to load \"%s\" (%s) metrics set in kernel: %s\n",
360 query->name, query->guid, strerror(errno));
361 continue;
362 }
363
364 register_oa_config(perf, devinfo, query, ret);
365 DBG("metric set: %s (added)\n", query->guid);
366 }
367 }
368
369 static void
compute_topology_builtins(struct gen_perf_config * perf,const struct gen_device_info * devinfo)370 compute_topology_builtins(struct gen_perf_config *perf,
371 const struct gen_device_info *devinfo)
372 {
373 perf->sys_vars.slice_mask = devinfo->slice_masks;
374 perf->sys_vars.n_eu_slices = devinfo->num_slices;
375
376 for (int i = 0; i < sizeof(devinfo->subslice_masks[i]); i++) {
377 perf->sys_vars.n_eu_sub_slices +=
378 __builtin_popcount(devinfo->subslice_masks[i]);
379 }
380
381 for (int i = 0; i < sizeof(devinfo->eu_masks); i++)
382 perf->sys_vars.n_eus += __builtin_popcount(devinfo->eu_masks[i]);
383
384 perf->sys_vars.eu_threads_count = devinfo->num_thread_per_eu;
385
386 /* The subslice mask builtin contains bits for all slices. Prior to Gen11
387 * it had groups of 3bits for each slice, on Gen11 it's 8bits for each
388 * slice.
389 *
390 * Ideally equations would be updated to have a slice/subslice query
391 * function/operator.
392 */
393 perf->sys_vars.subslice_mask = 0;
394
395 int bits_per_subslice = devinfo->gen == 11 ? 8 : 3;
396
397 for (int s = 0; s < util_last_bit(devinfo->slice_masks); s++) {
398 for (int ss = 0; ss < (devinfo->subslice_slice_stride * 8); ss++) {
399 if (gen_device_info_subslice_available(devinfo, s, ss))
400 perf->sys_vars.subslice_mask |= 1ULL << (s * bits_per_subslice + ss);
401 }
402 }
403 }
404
405 static bool
init_oa_sys_vars(struct gen_perf_config * perf,const struct gen_device_info * devinfo)406 init_oa_sys_vars(struct gen_perf_config *perf, const struct gen_device_info *devinfo)
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 compute_topology_builtins(perf, devinfo);
427
428 return true;
429 }
430
431 typedef void (*perf_register_oa_queries_t)(struct gen_perf_config *);
432
433 static perf_register_oa_queries_t
get_register_queries_function(const struct gen_device_info * devinfo)434 get_register_queries_function(const struct gen_device_info *devinfo)
435 {
436 if (devinfo->is_haswell)
437 return gen_oa_register_queries_hsw;
438 if (devinfo->is_cherryview)
439 return gen_oa_register_queries_chv;
440 if (devinfo->is_broadwell)
441 return gen_oa_register_queries_bdw;
442 if (devinfo->is_broxton)
443 return gen_oa_register_queries_bxt;
444 if (devinfo->is_skylake) {
445 if (devinfo->gt == 2)
446 return gen_oa_register_queries_sklgt2;
447 if (devinfo->gt == 3)
448 return gen_oa_register_queries_sklgt3;
449 if (devinfo->gt == 4)
450 return gen_oa_register_queries_sklgt4;
451 }
452 if (devinfo->is_kabylake) {
453 if (devinfo->gt == 2)
454 return gen_oa_register_queries_kblgt2;
455 if (devinfo->gt == 3)
456 return gen_oa_register_queries_kblgt3;
457 }
458 if (devinfo->is_geminilake)
459 return gen_oa_register_queries_glk;
460 if (devinfo->is_coffeelake) {
461 if (devinfo->gt == 2)
462 return gen_oa_register_queries_cflgt2;
463 if (devinfo->gt == 3)
464 return gen_oa_register_queries_cflgt3;
465 }
466 if (devinfo->gen == 11) {
467 if (devinfo->is_elkhartlake)
468 return gen_oa_register_queries_lkf;
469 return gen_oa_register_queries_icl;
470 }
471 if (devinfo->gen == 12)
472 return gen_oa_register_queries_tgl;
473
474 return NULL;
475 }
476
477 static int
gen_perf_compare_counter_names(const void * v1,const void * v2)478 gen_perf_compare_counter_names(const void *v1, const void *v2)
479 {
480 const struct gen_perf_query_counter *c1 = v1;
481 const struct gen_perf_query_counter *c2 = v2;
482
483 return strcmp(c1->name, c2->name);
484 }
485
486 static void
sort_query(struct gen_perf_query_info * q)487 sort_query(struct gen_perf_query_info *q)
488 {
489 qsort(q->counters, q->n_counters, sizeof(q->counters[0]),
490 gen_perf_compare_counter_names);
491 }
492
493 static void
load_pipeline_statistic_metrics(struct gen_perf_config * perf_cfg,const struct gen_device_info * devinfo)494 load_pipeline_statistic_metrics(struct gen_perf_config *perf_cfg,
495 const struct gen_device_info *devinfo)
496 {
497 struct gen_perf_query_info *query =
498 gen_perf_append_query_info(perf_cfg, MAX_STAT_COUNTERS);
499
500 query->kind = GEN_PERF_QUERY_TYPE_PIPELINE;
501 query->name = "Pipeline Statistics Registers";
502
503 gen_perf_query_add_basic_stat_reg(query, IA_VERTICES_COUNT,
504 "N vertices submitted");
505 gen_perf_query_add_basic_stat_reg(query, IA_PRIMITIVES_COUNT,
506 "N primitives submitted");
507 gen_perf_query_add_basic_stat_reg(query, VS_INVOCATION_COUNT,
508 "N vertex shader invocations");
509
510 if (devinfo->gen == 6) {
511 gen_perf_query_add_stat_reg(query, GEN6_SO_PRIM_STORAGE_NEEDED, 1, 1,
512 "SO_PRIM_STORAGE_NEEDED",
513 "N geometry shader stream-out primitives (total)");
514 gen_perf_query_add_stat_reg(query, GEN6_SO_NUM_PRIMS_WRITTEN, 1, 1,
515 "SO_NUM_PRIMS_WRITTEN",
516 "N geometry shader stream-out primitives (written)");
517 } else {
518 gen_perf_query_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
519 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
520 "N stream-out (stream 0) primitives (total)");
521 gen_perf_query_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
522 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
523 "N stream-out (stream 1) primitives (total)");
524 gen_perf_query_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
525 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
526 "N stream-out (stream 2) primitives (total)");
527 gen_perf_query_add_stat_reg(query, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
528 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
529 "N stream-out (stream 3) primitives (total)");
530 gen_perf_query_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
531 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
532 "N stream-out (stream 0) primitives (written)");
533 gen_perf_query_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
534 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
535 "N stream-out (stream 1) primitives (written)");
536 gen_perf_query_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
537 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
538 "N stream-out (stream 2) primitives (written)");
539 gen_perf_query_add_stat_reg(query, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
540 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
541 "N stream-out (stream 3) primitives (written)");
542 }
543
544 gen_perf_query_add_basic_stat_reg(query, HS_INVOCATION_COUNT,
545 "N TCS shader invocations");
546 gen_perf_query_add_basic_stat_reg(query, DS_INVOCATION_COUNT,
547 "N TES shader invocations");
548
549 gen_perf_query_add_basic_stat_reg(query, GS_INVOCATION_COUNT,
550 "N geometry shader invocations");
551 gen_perf_query_add_basic_stat_reg(query, GS_PRIMITIVES_COUNT,
552 "N geometry shader primitives emitted");
553
554 gen_perf_query_add_basic_stat_reg(query, CL_INVOCATION_COUNT,
555 "N primitives entering clipping");
556 gen_perf_query_add_basic_stat_reg(query, CL_PRIMITIVES_COUNT,
557 "N primitives leaving clipping");
558
559 if (devinfo->is_haswell || devinfo->gen == 8) {
560 gen_perf_query_add_stat_reg(query, PS_INVOCATION_COUNT, 1, 4,
561 "N fragment shader invocations",
562 "N fragment shader invocations");
563 } else {
564 gen_perf_query_add_basic_stat_reg(query, PS_INVOCATION_COUNT,
565 "N fragment shader invocations");
566 }
567
568 gen_perf_query_add_basic_stat_reg(query, PS_DEPTH_COUNT,
569 "N z-pass fragments");
570
571 if (devinfo->gen >= 7) {
572 gen_perf_query_add_basic_stat_reg(query, CS_INVOCATION_COUNT,
573 "N compute shader invocations");
574 }
575
576 query->data_size = sizeof(uint64_t) * query->n_counters;
577
578 sort_query(query);
579 }
580
581 static int
i915_perf_version(int drm_fd)582 i915_perf_version(int drm_fd)
583 {
584 int tmp;
585 drm_i915_getparam_t gp = {
586 .param = I915_PARAM_PERF_REVISION,
587 .value = &tmp,
588 };
589
590 int ret = gen_ioctl(drm_fd, DRM_IOCTL_I915_GETPARAM, &gp);
591
592 /* Return 0 if this getparam is not supported, the first version supported
593 * is 1.
594 */
595 return ret < 0 ? 0 : tmp;
596 }
597
598 static void
i915_get_sseu(int drm_fd,struct drm_i915_gem_context_param_sseu * sseu)599 i915_get_sseu(int drm_fd, struct drm_i915_gem_context_param_sseu *sseu)
600 {
601 struct drm_i915_gem_context_param arg = {
602 .param = I915_CONTEXT_PARAM_SSEU,
603 .size = sizeof(*sseu),
604 .value = to_user_pointer(sseu)
605 };
606
607 gen_ioctl(drm_fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &arg);
608 }
609
610 static inline int
compare_str_or_null(const char * s1,const char * s2)611 compare_str_or_null(const char *s1, const char *s2)
612 {
613 if (s1 == NULL && s2 == NULL)
614 return 0;
615 if (s1 == NULL)
616 return -1;
617 if (s2 == NULL)
618 return 1;
619
620 return strcmp(s1, s2);
621 }
622
623 static int
compare_counter_categories_and_names(const void * _c1,const void * _c2)624 compare_counter_categories_and_names(const void *_c1, const void *_c2)
625 {
626 const struct gen_perf_query_counter_info *c1 = (const struct gen_perf_query_counter_info *)_c1;
627 const struct gen_perf_query_counter_info *c2 = (const struct gen_perf_query_counter_info *)_c2;
628
629 /* pipeline counters don't have an assigned category */
630 int r = compare_str_or_null(c1->counter->category, c2->counter->category);
631 if (r)
632 return r;
633
634 return strcmp(c1->counter->name, c2->counter->name);
635 }
636
637 static void
build_unique_counter_list(struct gen_perf_config * perf)638 build_unique_counter_list(struct gen_perf_config *perf)
639 {
640 assert(perf->n_queries < 64);
641
642 size_t max_counters = 0;
643
644 for (int q = 0; q < perf->n_queries; q++)
645 max_counters += perf->queries[q].n_counters;
646
647 /*
648 * Allocate big enough array to hold maximum possible number of counters.
649 * We can't alloc it small and realloc when needed because the hash table
650 * below contains pointers to this array.
651 */
652 struct gen_perf_query_counter_info *counter_infos =
653 ralloc_array_size(perf, sizeof(counter_infos[0]), max_counters);
654
655 perf->n_counters = 0;
656
657 struct hash_table *counters_table =
658 _mesa_hash_table_create(perf,
659 _mesa_hash_string,
660 _mesa_key_string_equal);
661 struct hash_entry *entry;
662 for (int q = 0; q < perf->n_queries ; q++) {
663 struct gen_perf_query_info *query = &perf->queries[q];
664
665 for (int c = 0; c < query->n_counters; c++) {
666 struct gen_perf_query_counter *counter;
667 struct gen_perf_query_counter_info *counter_info;
668
669 counter = &query->counters[c];
670 entry = _mesa_hash_table_search(counters_table, counter->symbol_name);
671
672 if (entry) {
673 counter_info = entry->data;
674 counter_info->query_mask |= BITFIELD64_BIT(q);
675 continue;
676 }
677 assert(perf->n_counters < max_counters);
678
679 counter_info = &counter_infos[perf->n_counters++];
680 counter_info->counter = counter;
681 counter_info->query_mask = BITFIELD64_BIT(q);
682
683 counter_info->location.group_idx = q;
684 counter_info->location.counter_idx = c;
685
686 _mesa_hash_table_insert(counters_table, counter->symbol_name, counter_info);
687 }
688 }
689
690 _mesa_hash_table_destroy(counters_table, NULL);
691
692 /* Now we can realloc counter_infos array because hash table doesn't exist. */
693 perf->counter_infos = reralloc_array_size(perf, counter_infos,
694 sizeof(counter_infos[0]), perf->n_counters);
695
696 qsort(perf->counter_infos, perf->n_counters, sizeof(perf->counter_infos[0]),
697 compare_counter_categories_and_names);
698 }
699
700 static bool
oa_metrics_available(struct gen_perf_config * perf,int fd,const struct gen_device_info * devinfo)701 oa_metrics_available(struct gen_perf_config *perf, int fd,
702 const struct gen_device_info *devinfo)
703 {
704 perf_register_oa_queries_t oa_register = get_register_queries_function(devinfo);
705 bool i915_perf_oa_available = false;
706 struct stat sb;
707
708 perf->i915_query_supported = i915_query_perf_config_supported(perf, fd);
709 perf->i915_perf_version = i915_perf_version(fd);
710
711 /* Record the default SSEU configuration. */
712 i915_get_sseu(fd, &perf->sseu);
713
714 /* The existence of this sysctl parameter implies the kernel supports
715 * the i915 perf interface.
716 */
717 if (stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb) == 0) {
718
719 /* If _paranoid == 1 then on Gen8+ we won't be able to access OA
720 * metrics unless running as root.
721 */
722 if (devinfo->is_haswell)
723 i915_perf_oa_available = true;
724 else {
725 uint64_t paranoid = 1;
726
727 read_file_uint64("/proc/sys/dev/i915/perf_stream_paranoid", ¶noid);
728
729 if (paranoid == 0 || geteuid() == 0)
730 i915_perf_oa_available = true;
731 }
732
733 perf->platform_supported = oa_register != NULL;
734 }
735
736 return i915_perf_oa_available &&
737 oa_register &&
738 get_sysfs_dev_dir(perf, fd) &&
739 init_oa_sys_vars(perf, devinfo);
740 }
741
742 static void
load_oa_metrics(struct gen_perf_config * perf,int fd,const struct gen_device_info * devinfo)743 load_oa_metrics(struct gen_perf_config *perf, int fd,
744 const struct gen_device_info *devinfo)
745 {
746 int existing_queries = perf->n_queries;
747
748 perf_register_oa_queries_t oa_register = get_register_queries_function(devinfo);
749
750 perf->oa_metrics_table =
751 _mesa_hash_table_create(perf, _mesa_hash_string,
752 _mesa_key_string_equal);
753
754 /* Index all the metric sets mesa knows about before looking to see what
755 * the kernel is advertising.
756 */
757 oa_register(perf);
758
759 if (!(INTEL_DEBUG & DEBUG_NO_OACONFIG)) {
760 if (kernel_has_dynamic_config_support(perf, fd))
761 init_oa_configs(perf, fd, devinfo);
762 else
763 enumerate_sysfs_metrics(perf, devinfo);
764 } else {
765 add_all_metrics(perf, devinfo);
766 }
767
768 /* sort counters in each individual group created by this function by name */
769 for (int i = existing_queries; i < perf->n_queries; ++i)
770 sort_query(&perf->queries[i]);
771
772 /* Select a fallback OA metric. Look for the TestOa metric or use the last
773 * one if no present (on HSW).
774 */
775 for (int i = existing_queries; i < perf->n_queries; i++) {
776 if (perf->queries[i].symbol_name &&
777 strcmp(perf->queries[i].symbol_name, "TestOa") == 0) {
778 perf->fallback_raw_oa_metric = perf->queries[i].oa_metrics_set_id;
779 break;
780 }
781 }
782 if (perf->fallback_raw_oa_metric == 0 && perf->n_queries > 0)
783 perf->fallback_raw_oa_metric = perf->queries[perf->n_queries - 1].oa_metrics_set_id;
784 }
785
786 struct gen_perf_registers *
gen_perf_load_configuration(struct gen_perf_config * perf_cfg,int fd,const char * guid)787 gen_perf_load_configuration(struct gen_perf_config *perf_cfg, int fd, const char *guid)
788 {
789 if (!perf_cfg->i915_query_supported)
790 return NULL;
791
792 struct drm_i915_perf_oa_config i915_config = { 0, };
793 if (!i915_query_perf_config_data(perf_cfg, fd, guid, &i915_config))
794 return NULL;
795
796 struct gen_perf_registers *config = rzalloc(NULL, struct gen_perf_registers);
797 config->n_flex_regs = i915_config.n_flex_regs;
798 config->flex_regs = rzalloc_array(config, struct gen_perf_query_register_prog, config->n_flex_regs);
799 config->n_mux_regs = i915_config.n_mux_regs;
800 config->mux_regs = rzalloc_array(config, struct gen_perf_query_register_prog, config->n_mux_regs);
801 config->n_b_counter_regs = i915_config.n_boolean_regs;
802 config->b_counter_regs = rzalloc_array(config, struct gen_perf_query_register_prog, config->n_b_counter_regs);
803
804 /*
805 * struct gen_perf_query_register_prog maps exactly to the tuple of
806 * (register offset, register value) returned by the i915.
807 */
808 i915_config.flex_regs_ptr = to_const_user_pointer(config->flex_regs);
809 i915_config.mux_regs_ptr = to_const_user_pointer(config->mux_regs);
810 i915_config.boolean_regs_ptr = to_const_user_pointer(config->b_counter_regs);
811 if (!i915_query_perf_config_data(perf_cfg, fd, guid, &i915_config)) {
812 ralloc_free(config);
813 return NULL;
814 }
815
816 return config;
817 }
818
819 uint64_t
gen_perf_store_configuration(struct gen_perf_config * perf_cfg,int fd,const struct gen_perf_registers * config,const char * guid)820 gen_perf_store_configuration(struct gen_perf_config *perf_cfg, int fd,
821 const struct gen_perf_registers *config,
822 const char *guid)
823 {
824 if (guid)
825 return i915_add_config(perf_cfg, fd, config, guid);
826
827 struct mesa_sha1 sha1_ctx;
828 _mesa_sha1_init(&sha1_ctx);
829
830 if (config->flex_regs) {
831 _mesa_sha1_update(&sha1_ctx, config->flex_regs,
832 sizeof(config->flex_regs[0]) *
833 config->n_flex_regs);
834 }
835 if (config->mux_regs) {
836 _mesa_sha1_update(&sha1_ctx, config->mux_regs,
837 sizeof(config->mux_regs[0]) *
838 config->n_mux_regs);
839 }
840 if (config->b_counter_regs) {
841 _mesa_sha1_update(&sha1_ctx, config->b_counter_regs,
842 sizeof(config->b_counter_regs[0]) *
843 config->n_b_counter_regs);
844 }
845
846 uint8_t hash[20];
847 _mesa_sha1_final(&sha1_ctx, hash);
848
849 char formatted_hash[41];
850 _mesa_sha1_format(formatted_hash, hash);
851
852 char generated_guid[37];
853 snprintf(generated_guid, sizeof(generated_guid),
854 "%.8s-%.4s-%.4s-%.4s-%.12s",
855 &formatted_hash[0], &formatted_hash[8],
856 &formatted_hash[8 + 4], &formatted_hash[8 + 4 + 4],
857 &formatted_hash[8 + 4 + 4 + 4]);
858
859 /* Check if already present. */
860 uint64_t id;
861 if (gen_perf_load_metric_id(perf_cfg, generated_guid, &id))
862 return id;
863
864 return i915_add_config(perf_cfg, fd, config, generated_guid);
865 }
866
867 static uint64_t
get_passes_mask(struct gen_perf_config * perf,const uint32_t * counter_indices,uint32_t counter_indices_count)868 get_passes_mask(struct gen_perf_config *perf,
869 const uint32_t *counter_indices,
870 uint32_t counter_indices_count)
871 {
872 uint64_t queries_mask = 0;
873
874 assert(perf->n_queries < 64);
875
876 /* Compute the number of passes by going through all counters N times (with
877 * N the number of queries) to make sure we select the most constraining
878 * counters first and look at the more flexible ones (that could be
879 * obtained from multiple queries) later. That way we minimize the number
880 * of passes required.
881 */
882 for (uint32_t q = 0; q < perf->n_queries; q++) {
883 for (uint32_t i = 0; i < counter_indices_count; i++) {
884 assert(counter_indices[i] < perf->n_counters);
885
886 uint32_t idx = counter_indices[i];
887 if (__builtin_popcount(perf->counter_infos[idx].query_mask) != (q + 1))
888 continue;
889
890 if (queries_mask & perf->counter_infos[idx].query_mask)
891 continue;
892
893 queries_mask |= BITFIELD64_BIT(ffsll(perf->counter_infos[idx].query_mask) - 1);
894 }
895 }
896
897 return queries_mask;
898 }
899
900 uint32_t
gen_perf_get_n_passes(struct gen_perf_config * perf,const uint32_t * counter_indices,uint32_t counter_indices_count,struct gen_perf_query_info ** pass_queries)901 gen_perf_get_n_passes(struct gen_perf_config *perf,
902 const uint32_t *counter_indices,
903 uint32_t counter_indices_count,
904 struct gen_perf_query_info **pass_queries)
905 {
906 uint64_t queries_mask = get_passes_mask(perf, counter_indices, counter_indices_count);
907
908 if (pass_queries) {
909 uint32_t pass = 0;
910 for (uint32_t q = 0; q < perf->n_queries; q++) {
911 if ((1ULL << q) & queries_mask)
912 pass_queries[pass++] = &perf->queries[q];
913 }
914 }
915
916 return __builtin_popcount(queries_mask);
917 }
918
919 void
gen_perf_get_counters_passes(struct gen_perf_config * perf,const uint32_t * counter_indices,uint32_t counter_indices_count,struct gen_perf_counter_pass * counter_pass)920 gen_perf_get_counters_passes(struct gen_perf_config *perf,
921 const uint32_t *counter_indices,
922 uint32_t counter_indices_count,
923 struct gen_perf_counter_pass *counter_pass)
924 {
925 uint64_t queries_mask = get_passes_mask(perf, counter_indices, counter_indices_count);
926 ASSERTED uint32_t n_passes = __builtin_popcount(queries_mask);
927
928 for (uint32_t i = 0; i < counter_indices_count; i++) {
929 assert(counter_indices[i] < perf->n_counters);
930
931 uint32_t idx = counter_indices[i];
932 counter_pass[i].counter = perf->counter_infos[idx].counter;
933
934 uint32_t query_idx = ffsll(perf->counter_infos[idx].query_mask & queries_mask) - 1;
935 counter_pass[i].query = &perf->queries[query_idx];
936
937 uint32_t clear_bits = 63 - query_idx;
938 counter_pass[i].pass = __builtin_popcount((queries_mask << clear_bits) >> clear_bits) - 1;
939 assert(counter_pass[i].pass < n_passes);
940 }
941 }
942
943 /* Accumulate 32bits OA counters */
944 static inline void
accumulate_uint32(const uint32_t * report0,const uint32_t * report1,uint64_t * accumulator)945 accumulate_uint32(const uint32_t *report0,
946 const uint32_t *report1,
947 uint64_t *accumulator)
948 {
949 *accumulator += (uint32_t)(*report1 - *report0);
950 }
951
952 /* Accumulate 40bits OA counters */
953 static inline void
accumulate_uint40(int a_index,const uint32_t * report0,const uint32_t * report1,uint64_t * accumulator)954 accumulate_uint40(int a_index,
955 const uint32_t *report0,
956 const uint32_t *report1,
957 uint64_t *accumulator)
958 {
959 const uint8_t *high_bytes0 = (uint8_t *)(report0 + 40);
960 const uint8_t *high_bytes1 = (uint8_t *)(report1 + 40);
961 uint64_t high0 = (uint64_t)(high_bytes0[a_index]) << 32;
962 uint64_t high1 = (uint64_t)(high_bytes1[a_index]) << 32;
963 uint64_t value0 = report0[a_index + 4] | high0;
964 uint64_t value1 = report1[a_index + 4] | high1;
965 uint64_t delta;
966
967 if (value0 > value1)
968 delta = (1ULL << 40) + value1 - value0;
969 else
970 delta = value1 - value0;
971
972 *accumulator += delta;
973 }
974
975 static void
gen8_read_report_clock_ratios(const uint32_t * report,uint64_t * slice_freq_hz,uint64_t * unslice_freq_hz)976 gen8_read_report_clock_ratios(const uint32_t *report,
977 uint64_t *slice_freq_hz,
978 uint64_t *unslice_freq_hz)
979 {
980 /* The lower 16bits of the RPT_ID field of the OA reports contains a
981 * snapshot of the bits coming from the RP_FREQ_NORMAL register and is
982 * divided this way :
983 *
984 * RPT_ID[31:25]: RP_FREQ_NORMAL[20:14] (low squashed_slice_clock_frequency)
985 * RPT_ID[10:9]: RP_FREQ_NORMAL[22:21] (high squashed_slice_clock_frequency)
986 * RPT_ID[8:0]: RP_FREQ_NORMAL[31:23] (squashed_unslice_clock_frequency)
987 *
988 * RP_FREQ_NORMAL[31:23]: Software Unslice Ratio Request
989 * Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
990 *
991 * RP_FREQ_NORMAL[22:14]: Software Slice Ratio Request
992 * Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
993 */
994
995 uint32_t unslice_freq = report[0] & 0x1ff;
996 uint32_t slice_freq_low = (report[0] >> 25) & 0x7f;
997 uint32_t slice_freq_high = (report[0] >> 9) & 0x3;
998 uint32_t slice_freq = slice_freq_low | (slice_freq_high << 7);
999
1000 *slice_freq_hz = slice_freq * 16666667ULL;
1001 *unslice_freq_hz = unslice_freq * 16666667ULL;
1002 }
1003
1004 void
gen_perf_query_result_read_frequencies(struct gen_perf_query_result * result,const struct gen_device_info * devinfo,const uint32_t * start,const uint32_t * end)1005 gen_perf_query_result_read_frequencies(struct gen_perf_query_result *result,
1006 const struct gen_device_info *devinfo,
1007 const uint32_t *start,
1008 const uint32_t *end)
1009 {
1010 /* Slice/Unslice frequency is only available in the OA reports when the
1011 * "Disable OA reports due to clock ratio change" field in
1012 * OA_DEBUG_REGISTER is set to 1. This is how the kernel programs this
1013 * global register (see drivers/gpu/drm/i915/i915_perf.c)
1014 *
1015 * Documentation says this should be available on Gen9+ but experimentation
1016 * shows that Gen8 reports similar values, so we enable it there too.
1017 */
1018 if (devinfo->gen < 8)
1019 return;
1020
1021 gen8_read_report_clock_ratios(start,
1022 &result->slice_frequency[0],
1023 &result->unslice_frequency[0]);
1024 gen8_read_report_clock_ratios(end,
1025 &result->slice_frequency[1],
1026 &result->unslice_frequency[1]);
1027 }
1028
1029 void
gen_perf_query_result_accumulate(struct gen_perf_query_result * result,const struct gen_perf_query_info * query,const uint32_t * start,const uint32_t * end)1030 gen_perf_query_result_accumulate(struct gen_perf_query_result *result,
1031 const struct gen_perf_query_info *query,
1032 const uint32_t *start,
1033 const uint32_t *end)
1034 {
1035 int i;
1036
1037 if (result->hw_id == OA_REPORT_INVALID_CTX_ID &&
1038 start[2] != OA_REPORT_INVALID_CTX_ID)
1039 result->hw_id = start[2];
1040 if (result->reports_accumulated == 0)
1041 result->begin_timestamp = start[1];
1042 result->reports_accumulated++;
1043
1044 switch (query->oa_format) {
1045 case I915_OA_FORMAT_A32u40_A4u32_B8_C8:
1046 accumulate_uint32(start + 1, end + 1,
1047 result->accumulator + query->gpu_time_offset); /* timestamp */
1048 accumulate_uint32(start + 3, end + 3,
1049 result->accumulator + query->gpu_clock_offset); /* clock */
1050
1051 /* 32x 40bit A counters... */
1052 for (i = 0; i < 32; i++) {
1053 accumulate_uint40(i, start, end,
1054 result->accumulator + query->a_offset + i);
1055 }
1056
1057 /* 4x 32bit A counters... */
1058 for (i = 0; i < 4; i++) {
1059 accumulate_uint32(start + 36 + i, end + 36 + i,
1060 result->accumulator + query->a_offset + 32 + i);
1061 }
1062
1063 /* 8x 32bit B counters */
1064 for (i = 0; i < 8; i++) {
1065 accumulate_uint32(start + 48 + i, end + 48 + i,
1066 result->accumulator + query->b_offset + i);
1067 }
1068
1069 /* 8x 32bit C counters... */
1070 for (i = 0; i < 8; i++) {
1071 accumulate_uint32(start + 56 + i, end + 56 + i,
1072 result->accumulator + query->c_offset + i);
1073 }
1074 break;
1075
1076 case I915_OA_FORMAT_A45_B8_C8:
1077 accumulate_uint32(start + 1, end + 1, result->accumulator); /* timestamp */
1078
1079 for (i = 0; i < 61; i++) {
1080 accumulate_uint32(start + 3 + i, end + 3 + i,
1081 result->accumulator + query->a_offset + i);
1082 }
1083 break;
1084
1085 default:
1086 unreachable("Can't accumulate OA counters in unknown format");
1087 }
1088
1089 }
1090
1091 void
gen_perf_query_result_clear(struct gen_perf_query_result * result)1092 gen_perf_query_result_clear(struct gen_perf_query_result *result)
1093 {
1094 memset(result, 0, sizeof(*result));
1095 result->hw_id = OA_REPORT_INVALID_CTX_ID; /* invalid */
1096 }
1097
1098 static int
gen_perf_compare_query_names(const void * v1,const void * v2)1099 gen_perf_compare_query_names(const void *v1, const void *v2)
1100 {
1101 const struct gen_perf_query_info *q1 = v1;
1102 const struct gen_perf_query_info *q2 = v2;
1103
1104 return strcmp(q1->name, q2->name);
1105 }
1106
1107 void
gen_perf_init_metrics(struct gen_perf_config * perf_cfg,const struct gen_device_info * devinfo,int drm_fd,bool include_pipeline_statistics)1108 gen_perf_init_metrics(struct gen_perf_config *perf_cfg,
1109 const struct gen_device_info *devinfo,
1110 int drm_fd,
1111 bool include_pipeline_statistics)
1112 {
1113 if (include_pipeline_statistics) {
1114 load_pipeline_statistic_metrics(perf_cfg, devinfo);
1115 gen_perf_register_mdapi_statistic_query(perf_cfg, devinfo);
1116 }
1117
1118 bool oa_metrics = oa_metrics_available(perf_cfg, drm_fd, devinfo);
1119 if (oa_metrics)
1120 load_oa_metrics(perf_cfg, drm_fd, devinfo);
1121
1122 /* sort query groups by name */
1123 qsort(perf_cfg->queries, perf_cfg->n_queries,
1124 sizeof(perf_cfg->queries[0]), gen_perf_compare_query_names);
1125
1126 build_unique_counter_list(perf_cfg);
1127
1128 if (oa_metrics)
1129 gen_perf_register_mdapi_oa_query(perf_cfg, devinfo);
1130 }
1131