1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018 Broadcom
4 */
5
6 /**
7 * DOC: VC4 V3D performance monitor module
8 *
9 * The V3D block provides 16 hardware counters which can count various events.
10 */
11
12 #include <drm/drm_print.h>
13
14 #include "vc4_drv.h"
15 #include "vc4_regs.h"
16
17 #define VC4_PERFMONID_MIN 1
18 #define VC4_PERFMONID_MAX U32_MAX
19
vc4_perfmon_get(struct vc4_perfmon * perfmon)20 void vc4_perfmon_get(struct vc4_perfmon *perfmon)
21 {
22 struct vc4_dev *vc4;
23
24 if (!perfmon)
25 return;
26
27 vc4 = perfmon->dev;
28 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
29 return;
30
31 refcount_inc(&perfmon->refcnt);
32 }
33
vc4_perfmon_put(struct vc4_perfmon * perfmon)34 void vc4_perfmon_put(struct vc4_perfmon *perfmon)
35 {
36 struct vc4_dev *vc4;
37
38 if (!perfmon)
39 return;
40
41 vc4 = perfmon->dev;
42 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
43 return;
44
45 if (refcount_dec_and_test(&perfmon->refcnt))
46 kfree(perfmon);
47 }
48
vc4_perfmon_start(struct vc4_dev * vc4,struct vc4_perfmon * perfmon)49 void vc4_perfmon_start(struct vc4_dev *vc4, struct vc4_perfmon *perfmon)
50 {
51 unsigned int i;
52 u32 mask;
53
54 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
55 return;
56
57 if (WARN_ON_ONCE(!perfmon || vc4->active_perfmon))
58 return;
59
60 for (i = 0; i < perfmon->ncounters; i++)
61 V3D_WRITE(V3D_PCTRS(i), perfmon->events[i]);
62
63 mask = GENMASK(perfmon->ncounters - 1, 0);
64 V3D_WRITE(V3D_PCTRC, mask);
65 V3D_WRITE(V3D_PCTRE, V3D_PCTRE_EN | mask);
66 vc4->active_perfmon = perfmon;
67 }
68
vc4_perfmon_stop(struct vc4_dev * vc4,struct vc4_perfmon * perfmon,bool capture)69 void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
70 bool capture)
71 {
72 unsigned int i;
73
74 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
75 return;
76
77 if (WARN_ON_ONCE(!vc4->active_perfmon ||
78 perfmon != vc4->active_perfmon))
79 return;
80
81 if (capture) {
82 for (i = 0; i < perfmon->ncounters; i++)
83 perfmon->counters[i] += V3D_READ(V3D_PCTR(i));
84 }
85
86 V3D_WRITE(V3D_PCTRE, 0);
87 vc4->active_perfmon = NULL;
88 }
89
vc4_perfmon_find(struct vc4_file * vc4file,int id)90 struct vc4_perfmon *vc4_perfmon_find(struct vc4_file *vc4file, int id)
91 {
92 struct vc4_dev *vc4 = vc4file->dev;
93 struct vc4_perfmon *perfmon;
94
95 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
96 return NULL;
97
98 mutex_lock(&vc4file->perfmon.lock);
99 perfmon = idr_find(&vc4file->perfmon.idr, id);
100 vc4_perfmon_get(perfmon);
101 mutex_unlock(&vc4file->perfmon.lock);
102
103 return perfmon;
104 }
105
vc4_perfmon_open_file(struct vc4_file * vc4file)106 void vc4_perfmon_open_file(struct vc4_file *vc4file)
107 {
108 struct vc4_dev *vc4 = vc4file->dev;
109
110 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
111 return;
112
113 mutex_init(&vc4file->perfmon.lock);
114 idr_init_base(&vc4file->perfmon.idr, VC4_PERFMONID_MIN);
115 vc4file->dev = vc4;
116 }
117
vc4_perfmon_idr_del(int id,void * elem,void * data)118 static int vc4_perfmon_idr_del(int id, void *elem, void *data)
119 {
120 struct vc4_perfmon *perfmon = elem;
121 struct vc4_dev *vc4 = (struct vc4_dev *)data;
122
123 /* If the active perfmon is being destroyed, stop it first */
124 if (perfmon == vc4->active_perfmon)
125 vc4_perfmon_stop(vc4, perfmon, false);
126
127 vc4_perfmon_put(perfmon);
128
129 return 0;
130 }
131
vc4_perfmon_close_file(struct vc4_file * vc4file)132 void vc4_perfmon_close_file(struct vc4_file *vc4file)
133 {
134 struct vc4_dev *vc4 = vc4file->dev;
135
136 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
137 return;
138
139 mutex_lock(&vc4file->perfmon.lock);
140 idr_for_each(&vc4file->perfmon.idr, vc4_perfmon_idr_del, vc4);
141 idr_destroy(&vc4file->perfmon.idr);
142 mutex_unlock(&vc4file->perfmon.lock);
143 mutex_destroy(&vc4file->perfmon.lock);
144 }
145
vc4_perfmon_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)146 int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,
147 struct drm_file *file_priv)
148 {
149 struct vc4_dev *vc4 = to_vc4_dev(dev);
150 struct vc4_file *vc4file = file_priv->driver_priv;
151 struct drm_vc4_perfmon_create *req = data;
152 struct vc4_perfmon *perfmon;
153 unsigned int i;
154 int ret;
155
156 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
157 return -ENODEV;
158
159 if (!vc4->v3d) {
160 DRM_DEBUG("Creating perfmon no VC4 V3D probed\n");
161 return -ENODEV;
162 }
163
164 /* Number of monitored counters cannot exceed HW limits. */
165 if (req->ncounters > DRM_VC4_MAX_PERF_COUNTERS ||
166 !req->ncounters)
167 return -EINVAL;
168
169 /* Make sure all events are valid. */
170 for (i = 0; i < req->ncounters; i++) {
171 if (req->events[i] >= VC4_PERFCNT_NUM_EVENTS)
172 return -EINVAL;
173 }
174
175 perfmon = kzalloc_flex(*perfmon, counters, req->ncounters);
176 if (!perfmon)
177 return -ENOMEM;
178 perfmon->dev = vc4;
179
180 for (i = 0; i < req->ncounters; i++)
181 perfmon->events[i] = req->events[i];
182
183 perfmon->ncounters = req->ncounters;
184
185 refcount_set(&perfmon->refcnt, 1);
186
187 mutex_lock(&vc4file->perfmon.lock);
188 ret = idr_alloc(&vc4file->perfmon.idr, perfmon, VC4_PERFMONID_MIN,
189 VC4_PERFMONID_MAX, GFP_KERNEL);
190 mutex_unlock(&vc4file->perfmon.lock);
191
192 if (ret < 0) {
193 kfree(perfmon);
194 return ret;
195 }
196
197 req->id = ret;
198 return 0;
199 }
200
vc4_perfmon_destroy_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)201 int vc4_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
202 struct drm_file *file_priv)
203 {
204 struct vc4_dev *vc4 = to_vc4_dev(dev);
205 struct vc4_file *vc4file = file_priv->driver_priv;
206 struct drm_vc4_perfmon_destroy *req = data;
207 struct vc4_perfmon *perfmon;
208
209 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
210 return -ENODEV;
211
212 if (!vc4->v3d) {
213 DRM_DEBUG("Destroying perfmon no VC4 V3D probed\n");
214 return -ENODEV;
215 }
216
217 mutex_lock(&vc4file->perfmon.lock);
218 perfmon = idr_remove(&vc4file->perfmon.idr, req->id);
219 mutex_unlock(&vc4file->perfmon.lock);
220
221 if (!perfmon)
222 return -EINVAL;
223
224 vc4_perfmon_put(perfmon);
225 return 0;
226 }
227
vc4_perfmon_get_values_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)228 int vc4_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
229 struct drm_file *file_priv)
230 {
231 struct vc4_dev *vc4 = to_vc4_dev(dev);
232 struct vc4_file *vc4file = file_priv->driver_priv;
233 struct drm_vc4_perfmon_get_values *req = data;
234 struct vc4_perfmon *perfmon;
235 int ret;
236
237 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
238 return -ENODEV;
239
240 if (!vc4->v3d) {
241 DRM_DEBUG("Getting perfmon no VC4 V3D probed\n");
242 return -ENODEV;
243 }
244
245 perfmon = vc4_perfmon_find(vc4file, req->id);
246 if (!perfmon)
247 return -EINVAL;
248
249 if (copy_to_user(u64_to_user_ptr(req->values_ptr), perfmon->counters,
250 perfmon->ncounters * sizeof(u64)))
251 ret = -EFAULT;
252 else
253 ret = 0;
254
255 vc4_perfmon_put(perfmon);
256 return ret;
257 }
258