Linux Audio

Check our new training course

Loading...
v6.8
  1/*
  2 * Copyright © 2008 Intel Corporation
  3 * Copyright © 2016 Collabora Ltd
  4 *
  5 * Permission is hereby granted, free of charge, to any person obtaining a
  6 * copy of this software and associated documentation files (the "Software"),
  7 * to deal in the Software without restriction, including without limitation
  8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9 * and/or sell copies of the Software, and to permit persons to whom the
 10 * Software is furnished to do so, subject to the following conditions:
 11 *
 12 * The above copyright notice and this permission notice (including the next
 13 * paragraph) shall be included in all copies or substantial portions of the
 14 * Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 22 * IN THE SOFTWARE.
 23 *
 24 * Based on code from the i915 driver.
 25 * Original author: Damien Lespiau <damien.lespiau@intel.com>
 26 *
 27 */
 28
 29#include <linux/circ_buf.h>
 30#include <linux/ctype.h>
 31#include <linux/debugfs.h>
 32#include <linux/poll.h>
 33#include <linux/uaccess.h>
 34
 35#include <drm/drm_crtc.h>
 36#include <drm/drm_debugfs_crc.h>
 37#include <drm/drm_drv.h>
 38#include <drm/drm_print.h>
 39
 40#include "drm_internal.h"
 41
 42/**
 43 * DOC: CRC ABI
 44 *
 45 * DRM device drivers can provide to userspace CRC information of each frame as
 46 * it reached a given hardware component (a CRC sampling "source").
 47 *
 48 * Userspace can control generation of CRCs in a given CRTC by writing to the
 49 * file dri/0/crtc-N/crc/control in debugfs, with N being the :ref:`index of
 50 * the CRTC<crtc_index>`. Accepted values are source names (which are
 51 * driver-specific) and the "auto" keyword, which will let the driver select a
 52 * default source of frame CRCs for this CRTC.
 53 *
 54 * Once frame CRC generation is enabled, userspace can capture them by reading
 55 * the dri/0/crtc-N/crc/data file. Each line in that file contains the frame
 56 * number in the first field and then a number of unsigned integer fields
 57 * containing the CRC data. Fields are separated by a single space and the number
 58 * of CRC fields is source-specific.
 59 *
 60 * Note that though in some cases the CRC is computed in a specified way and on
 61 * the frame contents as supplied by userspace (eDP 1.3), in general the CRC
 62 * computation is performed in an unspecified way and on frame contents that have
 63 * been already processed in also an unspecified way and thus userspace cannot
 64 * rely on being able to generate matching CRC values for the frame contents that
 65 * it submits. In this general case, the maximum userspace can do is to compare
 66 * the reported CRCs of frames that should have the same contents.
 67 *
 68 * On the driver side the implementation effort is minimal, drivers only need to
 69 * implement &drm_crtc_funcs.set_crc_source and &drm_crtc_funcs.verify_crc_source.
 70 * The debugfs files are automatically set up if those vfuncs are set. CRC samples
 71 * need to be captured in the driver by calling drm_crtc_add_crc_entry().
 72 * Depending on the driver and HW requirements, &drm_crtc_funcs.set_crc_source
 73 * may result in a commit (even a full modeset).
 74 *
 75 * CRC results must be reliable across non-full-modeset atomic commits, so if a
 76 * commit via DRM_IOCTL_MODE_ATOMIC would disable or otherwise interfere with
 77 * CRC generation, then the driver must mark that commit as a full modeset
 78 * (drm_atomic_crtc_needs_modeset() should return true). As a result, to ensure
 79 * consistent results, generic userspace must re-setup CRC generation after a
 80 * legacy SETCRTC or an atomic commit with DRM_MODE_ATOMIC_ALLOW_MODESET.
 81 */
 82
 83static int crc_control_show(struct seq_file *m, void *data)
 84{
 85	struct drm_crtc *crtc = m->private;
 86
 87	if (crtc->funcs->get_crc_sources) {
 88		size_t count;
 89		const char *const *sources = crtc->funcs->get_crc_sources(crtc,
 90									&count);
 91		size_t values_cnt;
 92		int i;
 93
 94		if (count == 0 || !sources)
 95			goto out;
 96
 97		for (i = 0; i < count; i++)
 98			if (!crtc->funcs->verify_crc_source(crtc, sources[i],
 99							    &values_cnt)) {
100				if (strcmp(sources[i], crtc->crc.source))
101					seq_printf(m, "%s\n", sources[i]);
102				else
103					seq_printf(m, "%s*\n", sources[i]);
104			}
105	}
106	return 0;
107
108out:
109	seq_printf(m, "%s*\n", crtc->crc.source);
110	return 0;
111}
112
113static int crc_control_open(struct inode *inode, struct file *file)
114{
115	struct drm_crtc *crtc = inode->i_private;
116
117	return single_open(file, crc_control_show, crtc);
118}
119
120static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
121				 size_t len, loff_t *offp)
122{
123	struct seq_file *m = file->private_data;
124	struct drm_crtc *crtc = m->private;
125	struct drm_crtc_crc *crc = &crtc->crc;
126	char *source;
127	size_t values_cnt;
128	int ret;
129
130	if (len == 0)
131		return 0;
132
133	if (len > PAGE_SIZE - 1) {
134		DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
135			      PAGE_SIZE);
136		return -E2BIG;
137	}
138
139	source = memdup_user_nul(ubuf, len);
140	if (IS_ERR(source))
141		return PTR_ERR(source);
142
143	if (source[len - 1] == '\n')
144		source[len - 1] = '\0';
145
146	ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
147	if (ret) {
148		kfree(source);
149		return ret;
150	}
151
152	spin_lock_irq(&crc->lock);
153
154	if (crc->opened) {
155		spin_unlock_irq(&crc->lock);
156		kfree(source);
157		return -EBUSY;
158	}
159
160	kfree(crc->source);
161	crc->source = source;
162
163	spin_unlock_irq(&crc->lock);
164
165	*offp += len;
166	return len;
167}
168
169static const struct file_operations drm_crtc_crc_control_fops = {
170	.owner = THIS_MODULE,
171	.open = crc_control_open,
172	.read = seq_read,
173	.llseek = seq_lseek,
174	.release = single_release,
175	.write = crc_control_write
176};
177
178static int crtc_crc_data_count(struct drm_crtc_crc *crc)
179{
180	assert_spin_locked(&crc->lock);
181	return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
182}
183
184static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
185{
186	kfree(crc->entries);
187	crc->overflow = false;
188	crc->entries = NULL;
189	crc->head = 0;
190	crc->tail = 0;
191	crc->values_cnt = 0;
192	crc->opened = false;
193}
194
195static int crtc_crc_open(struct inode *inode, struct file *filep)
196{
197	struct drm_crtc *crtc = inode->i_private;
198	struct drm_crtc_crc *crc = &crtc->crc;
199	struct drm_crtc_crc_entry *entries = NULL;
200	size_t values_cnt;
201	int ret = 0;
202
203	if (drm_drv_uses_atomic_modeset(crtc->dev)) {
204		ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
205		if (ret)
206			return ret;
207
208		if (!crtc->state->active)
209			ret = -EIO;
210		drm_modeset_unlock(&crtc->mutex);
211
212		if (ret)
213			return ret;
214	}
215
216	ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt);
 
 
 
 
 
 
217	if (ret)
218		return ret;
219
220	if (WARN_ON(values_cnt > DRM_MAX_CRC_NR))
221		return -EINVAL;
 
222
223	if (WARN_ON(values_cnt == 0))
224		return -EINVAL;
 
 
 
 
 
 
 
225
226	entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
227	if (!entries)
228		return -ENOMEM;
 
 
229
230	spin_lock_irq(&crc->lock);
231	if (!crc->opened) {
232		crc->opened = true;
233		crc->entries = entries;
234		crc->values_cnt = values_cnt;
235	} else {
236		ret = -EBUSY;
237	}
238	spin_unlock_irq(&crc->lock);
239
240	if (ret) {
241		kfree(entries);
242		return ret;
243	}
 
 
 
 
 
244
245	ret = crtc->funcs->set_crc_source(crtc, crc->source);
246	if (ret)
247		goto err;
248
249	return 0;
250
 
 
251err:
252	spin_lock_irq(&crc->lock);
253	crtc_crc_cleanup(crc);
254	spin_unlock_irq(&crc->lock);
255	return ret;
256}
257
258static int crtc_crc_release(struct inode *inode, struct file *filep)
259{
260	struct drm_crtc *crtc = filep->f_inode->i_private;
261	struct drm_crtc_crc *crc = &crtc->crc;
 
262
263	/* terminate the infinite while loop if 'drm_dp_aux_crc_work' running */
264	spin_lock_irq(&crc->lock);
265	crc->opened = false;
266	spin_unlock_irq(&crc->lock);
267
268	crtc->funcs->set_crc_source(crtc, NULL);
269
270	spin_lock_irq(&crc->lock);
271	crtc_crc_cleanup(crc);
272	spin_unlock_irq(&crc->lock);
273
274	return 0;
275}
276
277/*
278 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
279 * separated, with a newline at the end and null-terminated.
280 */
281#define LINE_LEN(values_cnt)	(10 + 11 * values_cnt + 1 + 1)
282#define MAX_LINE_LEN		(LINE_LEN(DRM_MAX_CRC_NR))
283
284static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
285			     size_t count, loff_t *pos)
286{
287	struct drm_crtc *crtc = filep->f_inode->i_private;
288	struct drm_crtc_crc *crc = &crtc->crc;
289	struct drm_crtc_crc_entry *entry;
290	char buf[MAX_LINE_LEN];
291	int ret, i;
292
293	spin_lock_irq(&crc->lock);
294
295	if (!crc->source) {
296		spin_unlock_irq(&crc->lock);
297		return 0;
298	}
299
300	/* Nothing to read? */
301	while (crtc_crc_data_count(crc) == 0) {
302		if (filep->f_flags & O_NONBLOCK) {
303			spin_unlock_irq(&crc->lock);
304			return -EAGAIN;
305		}
306
307		ret = wait_event_interruptible_lock_irq(crc->wq,
308							crtc_crc_data_count(crc),
309							crc->lock);
310		if (ret) {
311			spin_unlock_irq(&crc->lock);
312			return ret;
313		}
314	}
315
316	/* We know we have an entry to be read */
317	entry = &crc->entries[crc->tail];
318
319	if (count < LINE_LEN(crc->values_cnt)) {
320		spin_unlock_irq(&crc->lock);
321		return -EINVAL;
322	}
323
324	BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
325	crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
326
327	spin_unlock_irq(&crc->lock);
328
329	if (entry->has_frame_counter)
330		sprintf(buf, "0x%08x", entry->frame);
331	else
332		sprintf(buf, "XXXXXXXXXX");
333
334	for (i = 0; i < crc->values_cnt; i++)
335		sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
336	sprintf(buf + 10 + crc->values_cnt * 11, "\n");
337
338	if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
339		return -EFAULT;
340
341	return LINE_LEN(crc->values_cnt);
342}
343
344static __poll_t crtc_crc_poll(struct file *file, poll_table *wait)
345{
346	struct drm_crtc *crtc = file->f_inode->i_private;
347	struct drm_crtc_crc *crc = &crtc->crc;
348	__poll_t ret = 0;
349
350	poll_wait(file, &crc->wq, wait);
351
352	spin_lock_irq(&crc->lock);
353	if (crc->source && crtc_crc_data_count(crc))
354		ret |= EPOLLIN | EPOLLRDNORM;
 
 
355	spin_unlock_irq(&crc->lock);
356
357	return ret;
358}
359
360static const struct file_operations drm_crtc_crc_data_fops = {
361	.owner = THIS_MODULE,
362	.open = crtc_crc_open,
363	.read = crtc_crc_read,
364	.poll = crtc_crc_poll,
365	.release = crtc_crc_release,
366};
367
368void drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
369{
370	struct dentry *crc_ent;
371
372	if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source)
373		return;
374
375	crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
 
 
 
 
 
 
 
 
 
 
 
 
376
377	debugfs_create_file("control", S_IRUGO | S_IWUSR, crc_ent, crtc,
378			    &drm_crtc_crc_control_fops);
379	debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
380			    &drm_crtc_crc_data_fops);
 
 
381}
382
383/**
384 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
385 * @crtc: CRTC to which the frame belongs
386 * @has_frame: whether this entry has a frame number to go with
387 * @frame: number of the frame these CRCs are about
388 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
389 *
390 * For each frame, the driver polls the source of CRCs for new data and calls
391 * this function to add them to the buffer from where userspace reads.
392 */
393int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
394			   uint32_t frame, uint32_t *crcs)
395{
396	struct drm_crtc_crc *crc = &crtc->crc;
397	struct drm_crtc_crc_entry *entry;
398	int head, tail;
399	unsigned long flags;
400
401	spin_lock_irqsave(&crc->lock, flags);
402
403	/* Caller may not have noticed yet that userspace has stopped reading */
404	if (!crc->entries) {
405		spin_unlock_irqrestore(&crc->lock, flags);
406		return -EINVAL;
407	}
408
409	head = crc->head;
410	tail = crc->tail;
411
412	if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
413		bool was_overflow = crc->overflow;
414
415		crc->overflow = true;
416		spin_unlock_irqrestore(&crc->lock, flags);
417
418		if (!was_overflow)
419			DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
420
421		return -ENOBUFS;
422	}
423
424	entry = &crc->entries[head];
425	entry->frame = frame;
426	entry->has_frame_counter = has_frame;
427	memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
428
429	head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
430	crc->head = head;
431
432	spin_unlock_irqrestore(&crc->lock, flags);
433
434	wake_up_interruptible(&crc->wq);
435
436	return 0;
437}
438EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);
v4.17
  1/*
  2 * Copyright © 2008 Intel Corporation
  3 * Copyright © 2016 Collabora Ltd
  4 *
  5 * Permission is hereby granted, free of charge, to any person obtaining a
  6 * copy of this software and associated documentation files (the "Software"),
  7 * to deal in the Software without restriction, including without limitation
  8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9 * and/or sell copies of the Software, and to permit persons to whom the
 10 * Software is furnished to do so, subject to the following conditions:
 11 *
 12 * The above copyright notice and this permission notice (including the next
 13 * paragraph) shall be included in all copies or substantial portions of the
 14 * Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 22 * IN THE SOFTWARE.
 23 *
 24 * Based on code from the i915 driver.
 25 * Original author: Damien Lespiau <damien.lespiau@intel.com>
 26 *
 27 */
 28
 29#include <linux/circ_buf.h>
 30#include <linux/ctype.h>
 31#include <linux/debugfs.h>
 32#include <drm/drmP.h>
 
 
 
 
 
 
 
 33#include "drm_internal.h"
 34
 35/**
 36 * DOC: CRC ABI
 37 *
 38 * DRM device drivers can provide to userspace CRC information of each frame as
 39 * it reached a given hardware component (a CRC sampling "source").
 40 *
 41 * Userspace can control generation of CRCs in a given CRTC by writing to the
 42 * file dri/0/crtc-N/crc/control in debugfs, with N being the index of the CRTC.
 43 * Accepted values are source names (which are driver-specific) and the "auto"
 44 * keyword, which will let the driver select a default source of frame CRCs
 45 * for this CRTC.
 46 *
 47 * Once frame CRC generation is enabled, userspace can capture them by reading
 48 * the dri/0/crtc-N/crc/data file. Each line in that file contains the frame
 49 * number in the first field and then a number of unsigned integer fields
 50 * containing the CRC data. Fields are separated by a single space and the number
 51 * of CRC fields is source-specific.
 52 *
 53 * Note that though in some cases the CRC is computed in a specified way and on
 54 * the frame contents as supplied by userspace (eDP 1.3), in general the CRC
 55 * computation is performed in an unspecified way and on frame contents that have
 56 * been already processed in also an unspecified way and thus userspace cannot
 57 * rely on being able to generate matching CRC values for the frame contents that
 58 * it submits. In this general case, the maximum userspace can do is to compare
 59 * the reported CRCs of frames that should have the same contents.
 60 *
 61 * On the driver side the implementation effort is minimal, drivers only need to
 62 * implement &drm_crtc_funcs.set_crc_source. The debugfs files are automatically
 63 * set up if that vfunc is set. CRC samples need to be captured in the driver by
 64 * calling drm_crtc_add_crc_entry().
 
 
 
 
 
 
 
 
 
 65 */
 66
 67static int crc_control_show(struct seq_file *m, void *data)
 68{
 69	struct drm_crtc *crtc = m->private;
 70
 71	seq_printf(m, "%s\n", crtc->crc.source);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 72
 
 
 73	return 0;
 74}
 75
 76static int crc_control_open(struct inode *inode, struct file *file)
 77{
 78	struct drm_crtc *crtc = inode->i_private;
 79
 80	return single_open(file, crc_control_show, crtc);
 81}
 82
 83static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
 84				 size_t len, loff_t *offp)
 85{
 86	struct seq_file *m = file->private_data;
 87	struct drm_crtc *crtc = m->private;
 88	struct drm_crtc_crc *crc = &crtc->crc;
 89	char *source;
 
 
 90
 91	if (len == 0)
 92		return 0;
 93
 94	if (len > PAGE_SIZE - 1) {
 95		DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
 96			      PAGE_SIZE);
 97		return -E2BIG;
 98	}
 99
100	source = memdup_user_nul(ubuf, len);
101	if (IS_ERR(source))
102		return PTR_ERR(source);
103
104	if (source[len] == '\n')
105		source[len] = '\0';
 
 
 
 
 
 
106
107	spin_lock_irq(&crc->lock);
108
109	if (crc->opened) {
110		spin_unlock_irq(&crc->lock);
111		kfree(source);
112		return -EBUSY;
113	}
114
115	kfree(crc->source);
116	crc->source = source;
117
118	spin_unlock_irq(&crc->lock);
119
120	*offp += len;
121	return len;
122}
123
124static const struct file_operations drm_crtc_crc_control_fops = {
125	.owner = THIS_MODULE,
126	.open = crc_control_open,
127	.read = seq_read,
128	.llseek = seq_lseek,
129	.release = single_release,
130	.write = crc_control_write
131};
132
133static int crtc_crc_data_count(struct drm_crtc_crc *crc)
134{
135	assert_spin_locked(&crc->lock);
136	return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
137}
138
139static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
140{
141	kfree(crc->entries);
 
142	crc->entries = NULL;
143	crc->head = 0;
144	crc->tail = 0;
145	crc->values_cnt = 0;
146	crc->opened = false;
147}
148
149static int crtc_crc_open(struct inode *inode, struct file *filep)
150{
151	struct drm_crtc *crtc = inode->i_private;
152	struct drm_crtc_crc *crc = &crtc->crc;
153	struct drm_crtc_crc_entry *entries = NULL;
154	size_t values_cnt;
155	int ret = 0;
156
157	if (drm_drv_uses_atomic_modeset(crtc->dev)) {
158		ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
159		if (ret)
160			return ret;
161
162		if (!crtc->state->active)
163			ret = -EIO;
164		drm_modeset_unlock(&crtc->mutex);
165
166		if (ret)
167			return ret;
168	}
169
170	spin_lock_irq(&crc->lock);
171	if (!crc->opened)
172		crc->opened = true;
173	else
174		ret = -EBUSY;
175	spin_unlock_irq(&crc->lock);
176
177	if (ret)
178		return ret;
179
180	ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
181	if (ret)
182		goto err;
183
184	if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
185		ret = -EINVAL;
186		goto err_disable;
187	}
188
189	if (WARN_ON(values_cnt == 0)) {
190		ret = -EINVAL;
191		goto err_disable;
192	}
193
194	entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
195	if (!entries) {
196		ret = -ENOMEM;
197		goto err_disable;
198	}
199
200	spin_lock_irq(&crc->lock);
201	crc->entries = entries;
202	crc->values_cnt = values_cnt;
 
 
 
 
 
 
203
204	/*
205	 * Only return once we got a first frame, so userspace doesn't have to
206	 * guess when this particular piece of HW will be ready to start
207	 * generating CRCs.
208	 */
209	ret = wait_event_interruptible_lock_irq(crc->wq,
210						crtc_crc_data_count(crc),
211						crc->lock);
212	spin_unlock_irq(&crc->lock);
213
 
214	if (ret)
215		goto err_disable;
216
217	return 0;
218
219err_disable:
220	crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
221err:
222	spin_lock_irq(&crc->lock);
223	crtc_crc_cleanup(crc);
224	spin_unlock_irq(&crc->lock);
225	return ret;
226}
227
228static int crtc_crc_release(struct inode *inode, struct file *filep)
229{
230	struct drm_crtc *crtc = filep->f_inode->i_private;
231	struct drm_crtc_crc *crc = &crtc->crc;
232	size_t values_cnt;
233
234	crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
 
 
 
 
 
235
236	spin_lock_irq(&crc->lock);
237	crtc_crc_cleanup(crc);
238	spin_unlock_irq(&crc->lock);
239
240	return 0;
241}
242
243/*
244 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
245 * separated, with a newline at the end and null-terminated.
246 */
247#define LINE_LEN(values_cnt)	(10 + 11 * values_cnt + 1 + 1)
248#define MAX_LINE_LEN		(LINE_LEN(DRM_MAX_CRC_NR))
249
250static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
251			     size_t count, loff_t *pos)
252{
253	struct drm_crtc *crtc = filep->f_inode->i_private;
254	struct drm_crtc_crc *crc = &crtc->crc;
255	struct drm_crtc_crc_entry *entry;
256	char buf[MAX_LINE_LEN];
257	int ret, i;
258
259	spin_lock_irq(&crc->lock);
260
261	if (!crc->source) {
262		spin_unlock_irq(&crc->lock);
263		return 0;
264	}
265
266	/* Nothing to read? */
267	while (crtc_crc_data_count(crc) == 0) {
268		if (filep->f_flags & O_NONBLOCK) {
269			spin_unlock_irq(&crc->lock);
270			return -EAGAIN;
271		}
272
273		ret = wait_event_interruptible_lock_irq(crc->wq,
274							crtc_crc_data_count(crc),
275							crc->lock);
276		if (ret) {
277			spin_unlock_irq(&crc->lock);
278			return ret;
279		}
280	}
281
282	/* We know we have an entry to be read */
283	entry = &crc->entries[crc->tail];
284
285	if (count < LINE_LEN(crc->values_cnt)) {
286		spin_unlock_irq(&crc->lock);
287		return -EINVAL;
288	}
289
290	BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
291	crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
292
293	spin_unlock_irq(&crc->lock);
294
295	if (entry->has_frame_counter)
296		sprintf(buf, "0x%08x", entry->frame);
297	else
298		sprintf(buf, "XXXXXXXXXX");
299
300	for (i = 0; i < crc->values_cnt; i++)
301		sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
302	sprintf(buf + 10 + crc->values_cnt * 11, "\n");
303
304	if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
305		return -EFAULT;
306
307	return LINE_LEN(crc->values_cnt);
308}
309
310static unsigned int crtc_crc_poll(struct file *file, poll_table *wait)
311{
312	struct drm_crtc *crtc = file->f_inode->i_private;
313	struct drm_crtc_crc *crc = &crtc->crc;
314	unsigned ret;
315
316	poll_wait(file, &crc->wq, wait);
317
318	spin_lock_irq(&crc->lock);
319	if (crc->source && crtc_crc_data_count(crc))
320		ret = POLLIN | POLLRDNORM;
321	else
322		ret = 0;
323	spin_unlock_irq(&crc->lock);
324
325	return ret;
326}
327
328static const struct file_operations drm_crtc_crc_data_fops = {
329	.owner = THIS_MODULE,
330	.open = crtc_crc_open,
331	.read = crtc_crc_read,
332	.poll = crtc_crc_poll,
333	.release = crtc_crc_release,
334};
335
336int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
337{
338	struct dentry *crc_ent, *ent;
339
340	if (!crtc->funcs->set_crc_source)
341		return 0;
342
343	crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
344	if (!crc_ent)
345		return -ENOMEM;
346
347	ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
348				  &drm_crtc_crc_control_fops);
349	if (!ent)
350		goto error;
351
352	ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
353				  &drm_crtc_crc_data_fops);
354	if (!ent)
355		goto error;
356
357	return 0;
358
359error:
360	debugfs_remove_recursive(crc_ent);
361
362	return -ENOMEM;
363}
364
365/**
366 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
367 * @crtc: CRTC to which the frame belongs
368 * @has_frame: whether this entry has a frame number to go with
369 * @frame: number of the frame these CRCs are about
370 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
371 *
372 * For each frame, the driver polls the source of CRCs for new data and calls
373 * this function to add them to the buffer from where userspace reads.
374 */
375int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
376			   uint32_t frame, uint32_t *crcs)
377{
378	struct drm_crtc_crc *crc = &crtc->crc;
379	struct drm_crtc_crc_entry *entry;
380	int head, tail;
 
381
382	spin_lock(&crc->lock);
383
384	/* Caller may not have noticed yet that userspace has stopped reading */
385	if (!crc->entries) {
386		spin_unlock(&crc->lock);
387		return -EINVAL;
388	}
389
390	head = crc->head;
391	tail = crc->tail;
392
393	if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
394		spin_unlock(&crc->lock);
395		DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
 
 
 
 
 
 
396		return -ENOBUFS;
397	}
398
399	entry = &crc->entries[head];
400	entry->frame = frame;
401	entry->has_frame_counter = has_frame;
402	memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
403
404	head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
405	crc->head = head;
406
407	spin_unlock(&crc->lock);
408
409	wake_up_interruptible(&crc->wq);
410
411	return 0;
412}
413EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);