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