Debian Codegraph @

src/panfrost/perf/pan_perf.c

raw
1 /*
2 * Copyright © 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <assert.h>
7 #include <string.h>
8 #include <xf86drm.h>
9
10 #include "util/macros.h"
11 #include "util/ralloc.h"
12
13 #include "pan_perf.h"
14
15 #include <drm-uapi/panfrost_drm.h>
16 #include <lib/kmod/pan_kmod.h>
17 #include <lib/pan_props.h>
18 #include <pan_perf_metrics.h>
19
20 #define PAN_COUNTERS_PER_CATEGORY 64
21 #define PAN_SHADER_CORE_INDEX 3
22
23 uint32_t
24 pan_perf_counter_read(const struct pan_perf_counter *counter,
25 const struct pan_perf *perf)
26 {
27 unsigned offset = perf->category_offset[counter->category_index];
28 offset += counter->offset;
29 assert(offset < perf->n_counter_values);
30
31 uint32_t ret = perf->counter_values[offset];
32
33 // If counter belongs to shader core, accumulate values for all other cores
34 if (counter->category_index == PAN_SHADER_CORE_INDEX) {
35 for (uint32_t core = 1; core < perf->core_id_range; ++core) {
36 ret += perf->counter_values[offset + PAN_COUNTERS_PER_CATEGORY * core];
37 }
38 }
39
40 return ret;
41 }
42
43 static const struct pan_perf_config *
44 pan_lookup_counters(const char *name)
45 {
46 for (unsigned i = 0; i < ARRAY_SIZE(pan_perf_configs); ++i) {
47 if (strcmp(pan_perf_configs[i]->name, name) == 0)
48 return pan_perf_configs[i];
49 }
50
51 return NULL;
52 }
53
54 void
55 pan_perf_init(struct pan_perf *perf, int fd)
56 {
57 ASSERTED drmVersionPtr version = drmGetVersion(fd);
58
59 /* We only support panfrost at the moment. */
60 assert(version && !strcmp(version->name, "panfrost"));
61
62 drmFreeVersion(version);
63
64 perf->dev = pan_kmod_dev_create(fd, 0, NULL);
65 assert(perf->dev);
66
67 struct pan_kmod_dev_props props = perf->dev->props;
68
69 const struct pan_model *model =
70 pan_get_model(props.gpu_id, props.gpu_variant);
71 if (model == NULL)
72 UNREACHABLE("Invalid GPU ID");
73
74 perf->cfg = pan_lookup_counters(model->performance_counters);
75
76 if (perf->cfg == NULL)
77 UNREACHABLE("Performance counters missing!");
78
79 // Generally counter blocks are laid out in the following order:
80 // Job manager, tiler, one or more L2 caches, and one or more shader cores.
81 unsigned l2_slices = pan_query_l2_slices(&props);
82 pan_query_core_count(&props, &perf->core_id_range);
83
84 uint32_t n_blocks = 2 + l2_slices + perf->core_id_range;
85 perf->n_counter_values = PAN_COUNTERS_PER_CATEGORY * n_blocks;
86 perf->counter_values = ralloc_array(perf, uint32_t, perf->n_counter_values);
87
88 /* Setup the layout */
89 perf->category_offset[0] = PAN_COUNTERS_PER_CATEGORY * 0;
90 perf->category_offset[1] = PAN_COUNTERS_PER_CATEGORY * 1;
91 perf->category_offset[2] = PAN_COUNTERS_PER_CATEGORY * 2;
92 perf->category_offset[3] = PAN_COUNTERS_PER_CATEGORY * (2 + l2_slices);
93 }
94
95 static int
96 pan_perf_query(struct pan_perf *perf, uint32_t enable)
97 {
98 struct drm_panfrost_perfcnt_enable perfcnt_enable = {enable, 0};
99 return pan_kmod_ioctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_ENABLE,
100 &perfcnt_enable);
101 }
102
103 int
104 pan_perf_enable(struct pan_perf *perf)
105 {
106 return pan_perf_query(perf, 1 /* enable */);
107 }
108
109 int
110 pan_perf_disable(struct pan_perf *perf)
111 {
112 return pan_perf_query(perf, 0 /* disable */);
113 }
114
115 int
116 pan_perf_dump(struct pan_perf *perf)
117 {
118 // Dump performance counter values to the memory buffer pointed to by
119 // counter_values
120 struct drm_panfrost_perfcnt_dump perfcnt_dump = {
121 (uint64_t)(uintptr_t)perf->counter_values};
122 return pan_kmod_ioctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_DUMP,
123 &perfcnt_dump);
124 }