1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Broadcom
7 * DOC: VC4 V3D performance monitor module
9 * The V3D block provides 16 hardware counters which can count various events.
15 #define VC4_PERFMONID_MIN 1
16 #define VC4_PERFMONID_MAX U32_MAX
18 void vc4_perfmon_get(struct vc4_perfmon *perfmon)
26 if (WARN_ON_ONCE(vc4->is_vc5))
29 refcount_inc(&perfmon->refcnt);
32 void vc4_perfmon_put(struct vc4_perfmon *perfmon)
40 if (WARN_ON_ONCE(vc4->is_vc5))
43 if (refcount_dec_and_test(&perfmon->refcnt))
47 void vc4_perfmon_start(struct vc4_dev *vc4, struct vc4_perfmon *perfmon)
52 if (WARN_ON_ONCE(vc4->is_vc5))
55 if (WARN_ON_ONCE(!perfmon || vc4->active_perfmon))
58 for (i = 0; i < perfmon->ncounters; i++)
59 V3D_WRITE(V3D_PCTRS(i), perfmon->events[i]);
61 mask = GENMASK(perfmon->ncounters - 1, 0);
62 V3D_WRITE(V3D_PCTRC, mask);
63 V3D_WRITE(V3D_PCTRE, V3D_PCTRE_EN | mask);
64 vc4->active_perfmon = perfmon;
67 void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
72 if (WARN_ON_ONCE(vc4->is_vc5))
75 if (WARN_ON_ONCE(!vc4->active_perfmon ||
76 perfmon != vc4->active_perfmon))
80 for (i = 0; i < perfmon->ncounters; i++)
81 perfmon->counters[i] += V3D_READ(V3D_PCTR(i));
84 V3D_WRITE(V3D_PCTRE, 0);
85 vc4->active_perfmon = NULL;
88 struct vc4_perfmon *vc4_perfmon_find(struct vc4_file *vc4file, int id)
90 struct vc4_dev *vc4 = vc4file->dev;
91 struct vc4_perfmon *perfmon;
93 if (WARN_ON_ONCE(vc4->is_vc5))
96 mutex_lock(&vc4file->perfmon.lock);
97 perfmon = idr_find(&vc4file->perfmon.idr, id);
98 vc4_perfmon_get(perfmon);
99 mutex_unlock(&vc4file->perfmon.lock);
104 void vc4_perfmon_open_file(struct vc4_file *vc4file)
106 struct vc4_dev *vc4 = vc4file->dev;
108 if (WARN_ON_ONCE(vc4->is_vc5))
111 mutex_init(&vc4file->perfmon.lock);
112 idr_init_base(&vc4file->perfmon.idr, VC4_PERFMONID_MIN);
116 static int vc4_perfmon_idr_del(int id, void *elem, void *data)
118 struct vc4_perfmon *perfmon = elem;
120 vc4_perfmon_put(perfmon);
125 void vc4_perfmon_close_file(struct vc4_file *vc4file)
127 struct vc4_dev *vc4 = vc4file->dev;
129 if (WARN_ON_ONCE(vc4->is_vc5))
132 mutex_lock(&vc4file->perfmon.lock);
133 idr_for_each(&vc4file->perfmon.idr, vc4_perfmon_idr_del, NULL);
134 idr_destroy(&vc4file->perfmon.idr);
135 mutex_unlock(&vc4file->perfmon.lock);
138 int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,
139 struct drm_file *file_priv)
141 struct vc4_dev *vc4 = to_vc4_dev(dev);
142 struct vc4_file *vc4file = file_priv->driver_priv;
143 struct drm_vc4_perfmon_create *req = data;
144 struct vc4_perfmon *perfmon;
148 if (WARN_ON_ONCE(vc4->is_vc5))
152 DRM_DEBUG("Creating perfmon no VC4 V3D probed\n");
156 /* Number of monitored counters cannot exceed HW limits. */
157 if (req->ncounters > DRM_VC4_MAX_PERF_COUNTERS ||
161 /* Make sure all events are valid. */
162 for (i = 0; i < req->ncounters; i++) {
163 if (req->events[i] >= VC4_PERFCNT_NUM_EVENTS)
167 perfmon = kzalloc(struct_size(perfmon, counters, req->ncounters),
173 for (i = 0; i < req->ncounters; i++)
174 perfmon->events[i] = req->events[i];
176 perfmon->ncounters = req->ncounters;
178 refcount_set(&perfmon->refcnt, 1);
180 mutex_lock(&vc4file->perfmon.lock);
181 ret = idr_alloc(&vc4file->perfmon.idr, perfmon, VC4_PERFMONID_MIN,
182 VC4_PERFMONID_MAX, GFP_KERNEL);
183 mutex_unlock(&vc4file->perfmon.lock);
194 int vc4_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
195 struct drm_file *file_priv)
197 struct vc4_dev *vc4 = to_vc4_dev(dev);
198 struct vc4_file *vc4file = file_priv->driver_priv;
199 struct drm_vc4_perfmon_destroy *req = data;
200 struct vc4_perfmon *perfmon;
202 if (WARN_ON_ONCE(vc4->is_vc5))
206 DRM_DEBUG("Destroying perfmon no VC4 V3D probed\n");
210 mutex_lock(&vc4file->perfmon.lock);
211 perfmon = idr_remove(&vc4file->perfmon.idr, req->id);
212 mutex_unlock(&vc4file->perfmon.lock);
217 vc4_perfmon_put(perfmon);
221 int vc4_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
222 struct drm_file *file_priv)
224 struct vc4_dev *vc4 = to_vc4_dev(dev);
225 struct vc4_file *vc4file = file_priv->driver_priv;
226 struct drm_vc4_perfmon_get_values *req = data;
227 struct vc4_perfmon *perfmon;
230 if (WARN_ON_ONCE(vc4->is_vc5))
234 DRM_DEBUG("Getting perfmon no VC4 V3D probed\n");
238 mutex_lock(&vc4file->perfmon.lock);
239 perfmon = idr_find(&vc4file->perfmon.idr, req->id);
240 vc4_perfmon_get(perfmon);
241 mutex_unlock(&vc4file->perfmon.lock);
246 if (copy_to_user(u64_to_user_ptr(req->values_ptr), perfmon->counters,
247 perfmon->ncounters * sizeof(u64)))
252 vc4_perfmon_put(perfmon);