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