1 /*
2 * Copyright © 2021 Collabora, Ltd.
3 * Author: Antonio Caggiano <[email protected]>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the 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 THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <string.h>
26 #include <xf86drm.h>
27
28 #include "util/macros.h"
29 #include "util/ralloc.h"
30
31 #include "pan_perf.h"
32
33 #include <drm-uapi/panfrost_drm.h>
34 #include <lib/kmod/pan_kmod.h>
35 #include <lib/pan_props.h>
36 #include <pan_perf_metrics.h>
37
38 #define PAN_COUNTERS_PER_CATEGORY 64
39 #define PAN_SHADER_CORE_INDEX 3
40
41 uint32_t
panfrost_perf_counter_read(const struct panfrost_perf_counter * counter,const struct panfrost_perf * perf)42 panfrost_perf_counter_read(const struct panfrost_perf_counter *counter,
43 const struct panfrost_perf *perf)
44 {
45 unsigned offset = perf->category_offset[counter->category_index];
46 offset += counter->offset;
47 assert(offset < perf->n_counter_values);
48
49 uint32_t ret = perf->counter_values[offset];
50
51 // If counter belongs to shader core, accumulate values for all other cores
52 if (counter->category_index == PAN_SHADER_CORE_INDEX) {
53 for (uint32_t core = 1; core < perf->core_id_range; ++core) {
54 ret += perf->counter_values[offset + PAN_COUNTERS_PER_CATEGORY * core];
55 }
56 }
57
58 return ret;
59 }
60
61 static const struct panfrost_perf_config *
panfrost_lookup_counters(const char * name)62 panfrost_lookup_counters(const char *name)
63 {
64 for (unsigned i = 0; i < ARRAY_SIZE(panfrost_perf_configs); ++i) {
65 if (strcmp(panfrost_perf_configs[i]->name, name) == 0)
66 return panfrost_perf_configs[i];
67 }
68
69 return NULL;
70 }
71
72 void
panfrost_perf_init(struct panfrost_perf * perf,int fd)73 panfrost_perf_init(struct panfrost_perf *perf, int fd)
74 {
75 ASSERTED drmVersionPtr version = drmGetVersion(fd);
76
77 /* We only support panfrost at the moment. */
78 assert(version && !strcmp(version->name, "panfrost"));
79
80 drmFreeVersion(version);
81
82 perf->dev = pan_kmod_dev_create(fd, 0, NULL);
83 assert(perf->dev);
84
85 struct pan_kmod_dev_props props = {};
86 pan_kmod_dev_query_props(perf->dev, &props);
87
88 const struct panfrost_model *model =
89 panfrost_get_model(props.gpu_prod_id, props.gpu_variant);
90 if (model == NULL)
91 unreachable("Invalid GPU ID");
92
93 perf->cfg = panfrost_lookup_counters(model->performance_counters);
94
95 if (perf->cfg == NULL)
96 unreachable("Performance counters missing!");
97
98 // Generally counter blocks are laid out in the following order:
99 // Job manager, tiler, one or more L2 caches, and one or more shader cores.
100 unsigned l2_slices = panfrost_query_l2_slices(&props);
101 panfrost_query_core_count(&props, &perf->core_id_range);
102
103 uint32_t n_blocks = 2 + l2_slices + perf->core_id_range;
104 perf->n_counter_values = PAN_COUNTERS_PER_CATEGORY * n_blocks;
105 perf->counter_values = ralloc_array(perf, uint32_t, perf->n_counter_values);
106
107 /* Setup the layout */
108 perf->category_offset[0] = PAN_COUNTERS_PER_CATEGORY * 0;
109 perf->category_offset[1] = PAN_COUNTERS_PER_CATEGORY * 1;
110 perf->category_offset[2] = PAN_COUNTERS_PER_CATEGORY * 2;
111 perf->category_offset[3] = PAN_COUNTERS_PER_CATEGORY * (2 + l2_slices);
112 }
113
114 static int
panfrost_perf_query(struct panfrost_perf * perf,uint32_t enable)115 panfrost_perf_query(struct panfrost_perf *perf, uint32_t enable)
116 {
117 struct drm_panfrost_perfcnt_enable perfcnt_enable = {enable, 0};
118 return drmIoctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_ENABLE,
119 &perfcnt_enable);
120 }
121
122 int
panfrost_perf_enable(struct panfrost_perf * perf)123 panfrost_perf_enable(struct panfrost_perf *perf)
124 {
125 return panfrost_perf_query(perf, 1 /* enable */);
126 }
127
128 int
panfrost_perf_disable(struct panfrost_perf * perf)129 panfrost_perf_disable(struct panfrost_perf *perf)
130 {
131 return panfrost_perf_query(perf, 0 /* disable */);
132 }
133
134 int
panfrost_perf_dump(struct panfrost_perf * perf)135 panfrost_perf_dump(struct panfrost_perf *perf)
136 {
137 // Dump performance counter values to the memory buffer pointed to by
138 // counter_values
139 struct drm_panfrost_perfcnt_dump perfcnt_dump = {
140 (uint64_t)(uintptr_t)perf->counter_values};
141 return drmIoctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_DUMP,
142 &perfcnt_dump);
143 }
144