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