1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Collabora, Ltd.
3bf215546Sopenharmony_ci * Author: Antonio Caggiano <antonio.caggiano@collabora.com>
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
6bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
7bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights
8bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
13bf215546Sopenharmony_ci * all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18bf215546Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21bf215546Sopenharmony_ci * THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "pan_perf.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include <pan_perf_metrics.h>
27bf215546Sopenharmony_ci#include <lib/pan_device.h>
28bf215546Sopenharmony_ci#include <drm-uapi/panfrost_drm.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#define PAN_COUNTERS_PER_CATEGORY 64
31bf215546Sopenharmony_ci#define PAN_SHADER_CORE_INDEX 3
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ciuint32_t
34bf215546Sopenharmony_cipanfrost_perf_counter_read(const struct panfrost_perf_counter *counter,
35bf215546Sopenharmony_ci                           const struct panfrost_perf *perf)
36bf215546Sopenharmony_ci{
37bf215546Sopenharmony_ci   unsigned offset = perf->category_offset[counter->category_index];
38bf215546Sopenharmony_ci   offset += counter->offset;
39bf215546Sopenharmony_ci   assert(offset < perf->n_counter_values);
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   uint32_t ret = perf->counter_values[offset];
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   // If counter belongs to shader core, accumulate values for all other cores
44bf215546Sopenharmony_ci   if (counter->category_index == PAN_SHADER_CORE_INDEX) {
45bf215546Sopenharmony_ci      for (uint32_t core = 1; core < perf->dev->core_id_range; ++core) {
46bf215546Sopenharmony_ci         ret += perf->counter_values[offset + PAN_COUNTERS_PER_CATEGORY * core];
47bf215546Sopenharmony_ci      }
48bf215546Sopenharmony_ci   }
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   return ret;
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic const struct panfrost_perf_config *
54bf215546Sopenharmony_cipanfrost_lookup_counters(const char *name)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci        for (unsigned i = 0; i < ARRAY_SIZE(panfrost_perf_configs); ++i) {
57bf215546Sopenharmony_ci                if (strcmp(panfrost_perf_configs[i]->name, name) == 0)
58bf215546Sopenharmony_ci                        return panfrost_perf_configs[i];
59bf215546Sopenharmony_ci        }
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci        return NULL;
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_civoid
65bf215546Sopenharmony_cipanfrost_perf_init(struct panfrost_perf *perf, struct panfrost_device *dev)
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   perf->dev = dev;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   if (dev->model == NULL)
70bf215546Sopenharmony_ci           unreachable("Invalid GPU ID");
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   perf->cfg = panfrost_lookup_counters(dev->model->performance_counters);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   if (perf->cfg == NULL)
75bf215546Sopenharmony_ci           unreachable("Performance counters missing!");
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   // Generally counter blocks are laid out in the following order:
78bf215546Sopenharmony_ci   // Job manager, tiler, one or more L2 caches, and one or more shader cores.
79bf215546Sopenharmony_ci   unsigned l2_slices = panfrost_query_l2_slices(dev);
80bf215546Sopenharmony_ci   uint32_t n_blocks = 2 + l2_slices + dev->core_id_range;
81bf215546Sopenharmony_ci   perf->n_counter_values = PAN_COUNTERS_PER_CATEGORY * n_blocks;
82bf215546Sopenharmony_ci   perf->counter_values = ralloc_array(perf, uint32_t, perf->n_counter_values);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   /* Setup the layout */
85bf215546Sopenharmony_ci   perf->category_offset[0] = PAN_COUNTERS_PER_CATEGORY * 0;
86bf215546Sopenharmony_ci   perf->category_offset[1] = PAN_COUNTERS_PER_CATEGORY * 1;
87bf215546Sopenharmony_ci   perf->category_offset[2] = PAN_COUNTERS_PER_CATEGORY * 2;
88bf215546Sopenharmony_ci   perf->category_offset[3] = PAN_COUNTERS_PER_CATEGORY * (2 + l2_slices);
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_cistatic int
92bf215546Sopenharmony_cipanfrost_perf_query(struct panfrost_perf *perf, uint32_t enable)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   struct drm_panfrost_perfcnt_enable perfcnt_enable = {enable, 0};
95bf215546Sopenharmony_ci   return drmIoctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_ENABLE, &perfcnt_enable);
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ciint
99bf215546Sopenharmony_cipanfrost_perf_enable(struct panfrost_perf *perf)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   return panfrost_perf_query(perf, 1 /* enable */);
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ciint
105bf215546Sopenharmony_cipanfrost_perf_disable(struct panfrost_perf *perf)
106bf215546Sopenharmony_ci{
107bf215546Sopenharmony_ci   return panfrost_perf_query(perf, 0 /* disable */);
108bf215546Sopenharmony_ci}
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ciint
111bf215546Sopenharmony_cipanfrost_perf_dump(struct panfrost_perf *perf)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci   // Dump performance counter values to the memory buffer pointed to by counter_values
114bf215546Sopenharmony_ci   struct drm_panfrost_perfcnt_dump perfcnt_dump = {(uint64_t)(uintptr_t)perf->counter_values};
115bf215546Sopenharmony_ci   return drmIoctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_DUMP, &perfcnt_dump);
116bf215546Sopenharmony_ci}
117