1 /*
2 * Copyright © 2024 Raspberry Pi Ltd
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <assert.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "common/v3d_device_info.h"
12 #include "common/v3d_macros.h"
13 #include "common/v3d_performance_counters.h"
14 #include "common/v3d_util.h"
15 #include "drm-uapi/v3d_drm.h"
16 #include "util/log.h"
17 #include "util/ralloc.h"
18 #include "v3d_perfcntrs.h"
19 #include "v3dx_counter.h"
20
21 unsigned
v3dX(perfcounters_num)22 v3dX(perfcounters_num)(const struct v3d_device_info *devinfo)
23 {
24 return devinfo->max_perfcnt ? devinfo->max_perfcnt
25 : ARRAY_SIZE(v3d_performance_counters);
26 }
27
28 struct v3d_perfcntr_desc *
v3dX(perfcounters_get)29 v3dX(perfcounters_get)(struct v3d_perfcntrs *perfcounters, unsigned index)
30 {
31 const unsigned max_perfcnt = perfcounters->max_perfcnt;
32 struct v3d_perfcntr_desc *counter;
33
34 assert(index < max_perfcnt);
35 assert(perfcounters->perfcnt[index] == NULL);
36
37 counter = ralloc(perfcounters, struct v3d_perfcntr_desc);
38 if (!counter)
39 return NULL;
40
41 if (perfcounters->devinfo->max_perfcnt) {
42 struct drm_v3d_perfmon_get_counter req = {
43 .counter = index,
44 };
45 int ret = v3d_ioctl(perfcounters->fd, DRM_IOCTL_V3D_PERFMON_GET_COUNTER, &req);
46 if (ret != 0) {
47 mesa_loge("Failed to get performance counter %d: %s\n", index, strerror(errno));
48 return NULL;
49 }
50
51 counter->name = ralloc_strdup(perfcounters->perfcnt, (const char *) req.name);
52 counter->category = ralloc_strdup(perfcounters->perfcnt, (const char *) req.category);
53 counter->description = ralloc_strdup(perfcounters->perfcnt, (const char *) req.description);
54 } else {
55 counter->name = v3d_performance_counters[index][V3D_PERFCNT_NAME];
56 counter->category = v3d_performance_counters[index][V3D_PERFCNT_CATEGORY];
57 counter->description = v3d_performance_counters[index][V3D_PERFCNT_DESCRIPTION];
58 }
59
60 counter->index = index;
61 perfcounters->perfcnt[index] = counter;
62
63 return counter;
64 }
65