Linux Audio

Check our new training course

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