• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
crc_control_show(struct seq_file * m,void * data)67 static 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 
crc_control_open(struct inode * inode,struct file * file)76 static 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 
crc_control_write(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)83 static 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 - 1] == '\n')
105 		source[len - 1] = '\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 
124 static 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 
crtc_crc_data_count(struct drm_crtc_crc * crc)133 static 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 
crtc_crc_cleanup(struct drm_crtc_crc * crc)139 static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
140 {
141 	kfree(crc->entries);
142 	crc->overflow = false;
143 	crc->entries = NULL;
144 	crc->head = 0;
145 	crc->tail = 0;
146 	crc->values_cnt = 0;
147 	crc->opened = false;
148 }
149 
crtc_crc_open(struct inode * inode,struct file * filep)150 static int crtc_crc_open(struct inode *inode, struct file *filep)
151 {
152 	struct drm_crtc *crtc = inode->i_private;
153 	struct drm_crtc_crc *crc = &crtc->crc;
154 	struct drm_crtc_crc_entry *entries = NULL;
155 	size_t values_cnt;
156 	int ret = 0;
157 
158 	if (drm_drv_uses_atomic_modeset(crtc->dev)) {
159 		ret = drm_modeset_lock_interruptible(&crtc->mutex, NULL);
160 		if (ret)
161 			return ret;
162 
163 		if (!crtc->state->active)
164 			ret = -EIO;
165 		drm_modeset_unlock(&crtc->mutex);
166 
167 		if (ret)
168 			return ret;
169 	}
170 
171 	spin_lock_irq(&crc->lock);
172 	if (!crc->opened)
173 		crc->opened = true;
174 	else
175 		ret = -EBUSY;
176 	spin_unlock_irq(&crc->lock);
177 
178 	if (ret)
179 		return ret;
180 
181 	ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
182 	if (ret)
183 		goto err;
184 
185 	if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
186 		ret = -EINVAL;
187 		goto err_disable;
188 	}
189 
190 	if (WARN_ON(values_cnt == 0)) {
191 		ret = -EINVAL;
192 		goto err_disable;
193 	}
194 
195 	entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
196 	if (!entries) {
197 		ret = -ENOMEM;
198 		goto err_disable;
199 	}
200 
201 	spin_lock_irq(&crc->lock);
202 	crc->entries = entries;
203 	crc->values_cnt = values_cnt;
204 
205 	/*
206 	 * Only return once we got a first frame, so userspace doesn't have to
207 	 * guess when this particular piece of HW will be ready to start
208 	 * generating CRCs.
209 	 */
210 	ret = wait_event_interruptible_lock_irq(crc->wq,
211 						crtc_crc_data_count(crc),
212 						crc->lock);
213 	spin_unlock_irq(&crc->lock);
214 
215 	if (ret)
216 		goto err_disable;
217 
218 	return 0;
219 
220 err_disable:
221 	crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
222 err:
223 	spin_lock_irq(&crc->lock);
224 	crtc_crc_cleanup(crc);
225 	spin_unlock_irq(&crc->lock);
226 	return ret;
227 }
228 
crtc_crc_release(struct inode * inode,struct file * filep)229 static int crtc_crc_release(struct inode *inode, struct file *filep)
230 {
231 	struct drm_crtc *crtc = filep->f_inode->i_private;
232 	struct drm_crtc_crc *crc = &crtc->crc;
233 	size_t values_cnt;
234 
235 	crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
236 
237 	spin_lock_irq(&crc->lock);
238 	crtc_crc_cleanup(crc);
239 	spin_unlock_irq(&crc->lock);
240 
241 	return 0;
242 }
243 
244 /*
245  * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
246  * separated, with a newline at the end and null-terminated.
247  */
248 #define LINE_LEN(values_cnt)	(10 + 11 * values_cnt + 1 + 1)
249 #define MAX_LINE_LEN		(LINE_LEN(DRM_MAX_CRC_NR))
250 
crtc_crc_read(struct file * filep,char __user * user_buf,size_t count,loff_t * pos)251 static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
252 			     size_t count, loff_t *pos)
253 {
254 	struct drm_crtc *crtc = filep->f_inode->i_private;
255 	struct drm_crtc_crc *crc = &crtc->crc;
256 	struct drm_crtc_crc_entry *entry;
257 	char buf[MAX_LINE_LEN];
258 	int ret, i;
259 
260 	spin_lock_irq(&crc->lock);
261 
262 	if (!crc->source) {
263 		spin_unlock_irq(&crc->lock);
264 		return 0;
265 	}
266 
267 	/* Nothing to read? */
268 	while (crtc_crc_data_count(crc) == 0) {
269 		if (filep->f_flags & O_NONBLOCK) {
270 			spin_unlock_irq(&crc->lock);
271 			return -EAGAIN;
272 		}
273 
274 		ret = wait_event_interruptible_lock_irq(crc->wq,
275 							crtc_crc_data_count(crc),
276 							crc->lock);
277 		if (ret) {
278 			spin_unlock_irq(&crc->lock);
279 			return ret;
280 		}
281 	}
282 
283 	/* We know we have an entry to be read */
284 	entry = &crc->entries[crc->tail];
285 
286 	if (count < LINE_LEN(crc->values_cnt)) {
287 		spin_unlock_irq(&crc->lock);
288 		return -EINVAL;
289 	}
290 
291 	BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
292 	crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
293 
294 	spin_unlock_irq(&crc->lock);
295 
296 	if (entry->has_frame_counter)
297 		sprintf(buf, "0x%08x", entry->frame);
298 	else
299 		sprintf(buf, "XXXXXXXXXX");
300 
301 	for (i = 0; i < crc->values_cnt; i++)
302 		sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
303 	sprintf(buf + 10 + crc->values_cnt * 11, "\n");
304 
305 	if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
306 		return -EFAULT;
307 
308 	return LINE_LEN(crc->values_cnt);
309 }
310 
311 static const struct file_operations drm_crtc_crc_data_fops = {
312 	.owner = THIS_MODULE,
313 	.open = crtc_crc_open,
314 	.read = crtc_crc_read,
315 	.release = crtc_crc_release,
316 };
317 
drm_debugfs_crtc_crc_add(struct drm_crtc * crtc)318 int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
319 {
320 	struct dentry *crc_ent, *ent;
321 
322 	if (!crtc->funcs->set_crc_source)
323 		return 0;
324 
325 	crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
326 	if (!crc_ent)
327 		return -ENOMEM;
328 
329 	ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
330 				  &drm_crtc_crc_control_fops);
331 	if (!ent)
332 		goto error;
333 
334 	ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
335 				  &drm_crtc_crc_data_fops);
336 	if (!ent)
337 		goto error;
338 
339 	return 0;
340 
341 error:
342 	debugfs_remove_recursive(crc_ent);
343 
344 	return -ENOMEM;
345 }
346 
347 /**
348  * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
349  * @crtc: CRTC to which the frame belongs
350  * @has_frame: whether this entry has a frame number to go with
351  * @frame: number of the frame these CRCs are about
352  * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
353  *
354  * For each frame, the driver polls the source of CRCs for new data and calls
355  * this function to add them to the buffer from where userspace reads.
356  */
drm_crtc_add_crc_entry(struct drm_crtc * crtc,bool has_frame,uint32_t frame,uint32_t * crcs)357 int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
358 			   uint32_t frame, uint32_t *crcs)
359 {
360 	struct drm_crtc_crc *crc = &crtc->crc;
361 	struct drm_crtc_crc_entry *entry;
362 	int head, tail;
363 	unsigned long flags;
364 
365 	spin_lock_irqsave(&crc->lock, flags);
366 
367 	/* Caller may not have noticed yet that userspace has stopped reading */
368 	if (!crc->entries) {
369 		spin_unlock_irqrestore(&crc->lock, flags);
370 		return -EINVAL;
371 	}
372 
373 	head = crc->head;
374 	tail = crc->tail;
375 
376 	if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
377 		bool was_overflow = crc->overflow;
378 
379 		crc->overflow = true;
380 		spin_unlock_irqrestore(&crc->lock, flags);
381 
382 		if (!was_overflow)
383 			DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
384 
385 		return -ENOBUFS;
386 	}
387 
388 	entry = &crc->entries[head];
389 	entry->frame = frame;
390 	entry->has_frame_counter = has_frame;
391 	memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
392 
393 	head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
394 	crc->head = head;
395 
396 	spin_unlock_irqrestore(&crc->lock, flags);
397 
398 	wake_up_interruptible(&crc->wq);
399 
400 	return 0;
401 }
402 EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);
403