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 
67 static int crc_control_show(struct seq_file *m, void *data)
68 {
69 	struct drm_crtc *crtc = m->private;
70 
71 	if (crtc->funcs->get_crc_sources) {
72 		size_t count;
73 		const char *const *sources = crtc->funcs->get_crc_sources(crtc,
74 									&count);
75 		size_t values_cnt;
76 		int i;
77 
78 		if (count == 0 || !sources)
79 			goto out;
80 
81 		for (i = 0; i < count; i++)
82 			if (!crtc->funcs->verify_crc_source(crtc, sources[i],
83 							    &values_cnt)) {
84 				if (strcmp(sources[i], crtc->crc.source))
85 					seq_printf(m, "%s\n", sources[i]);
86 				else
87 					seq_printf(m, "%s*\n", sources[i]);
88 			}
89 	}
90 	return 0;
91 
92 out:
93 	seq_printf(m, "%s*\n", crtc->crc.source);
94 	return 0;
95 }
96 
97 static int crc_control_open(struct inode *inode, struct file *file)
98 {
99 	struct drm_crtc *crtc = inode->i_private;
100 
101 	return single_open(file, crc_control_show, crtc);
102 }
103 
104 static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
105 				 size_t len, loff_t *offp)
106 {
107 	struct seq_file *m = file->private_data;
108 	struct drm_crtc *crtc = m->private;
109 	struct drm_crtc_crc *crc = &crtc->crc;
110 	char *source;
111 	size_t values_cnt;
112 	int ret;
113 
114 	if (len == 0)
115 		return 0;
116 
117 	if (len > PAGE_SIZE - 1) {
118 		DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
119 			      PAGE_SIZE);
120 		return -E2BIG;
121 	}
122 
123 	source = memdup_user_nul(ubuf, len);
124 	if (IS_ERR(source))
125 		return PTR_ERR(source);
126 
127 	if (source[len] == '\n')
128 		source[len] = '\0';
129 
130 	ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
131 	if (ret)
132 		return ret;
133 
134 	spin_lock_irq(&crc->lock);
135 
136 	if (crc->opened) {
137 		spin_unlock_irq(&crc->lock);
138 		kfree(source);
139 		return -EBUSY;
140 	}
141 
142 	kfree(crc->source);
143 	crc->source = source;
144 
145 	spin_unlock_irq(&crc->lock);
146 
147 	*offp += len;
148 	return len;
149 }
150 
151 static const struct file_operations drm_crtc_crc_control_fops = {
152 	.owner = THIS_MODULE,
153 	.open = crc_control_open,
154 	.read = seq_read,
155 	.llseek = seq_lseek,
156 	.release = single_release,
157 	.write = crc_control_write
158 };
159 
160 static int crtc_crc_data_count(struct drm_crtc_crc *crc)
161 {
162 	assert_spin_locked(&crc->lock);
163 	return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
164 }
165 
166 static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
167 {
168 	kfree(crc->entries);
169 	crc->overflow = false;
170 	crc->entries = NULL;
171 	crc->head = 0;
172 	crc->tail = 0;
173 	crc->values_cnt = 0;
174 	crc->opened = false;
175 }
176 
177 static int crtc_crc_open(struct inode *inode, struct file *filep)
178 {
179 	struct drm_crtc *crtc = inode->i_private;
180 	struct drm_crtc_crc *crc = &crtc->crc;
181 	struct drm_crtc_crc_entry *entries = NULL;
182 	size_t values_cnt;
183 	int ret = 0;
184 
185 	if (drm_drv_uses_atomic_modeset(crtc->dev)) {
186 		ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
187 		if (ret)
188 			return ret;
189 
190 		if (!crtc->state->active)
191 			ret = -EIO;
192 		drm_modeset_unlock(&crtc->mutex);
193 
194 		if (ret)
195 			return ret;
196 	}
197 
198 	ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt);
199 	if (ret)
200 		return ret;
201 
202 	if (WARN_ON(values_cnt > DRM_MAX_CRC_NR))
203 		return -EINVAL;
204 
205 	if (WARN_ON(values_cnt == 0))
206 		return -EINVAL;
207 
208 	entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
209 	if (!entries)
210 		return -ENOMEM;
211 
212 	spin_lock_irq(&crc->lock);
213 	if (!crc->opened) {
214 		crc->opened = true;
215 		crc->entries = entries;
216 		crc->values_cnt = values_cnt;
217 	} else {
218 		ret = -EBUSY;
219 	}
220 	spin_unlock_irq(&crc->lock);
221 
222 	if (ret) {
223 		kfree(entries);
224 		return ret;
225 	}
226 
227 	ret = crtc->funcs->set_crc_source(crtc, crc->source);
228 	if (ret)
229 		goto err;
230 
231 	return 0;
232 
233 err:
234 	spin_lock_irq(&crc->lock);
235 	crtc_crc_cleanup(crc);
236 	spin_unlock_irq(&crc->lock);
237 	return ret;
238 }
239 
240 static int crtc_crc_release(struct inode *inode, struct file *filep)
241 {
242 	struct drm_crtc *crtc = filep->f_inode->i_private;
243 	struct drm_crtc_crc *crc = &crtc->crc;
244 
245 	crtc->funcs->set_crc_source(crtc, NULL);
246 
247 	spin_lock_irq(&crc->lock);
248 	crtc_crc_cleanup(crc);
249 	spin_unlock_irq(&crc->lock);
250 
251 	return 0;
252 }
253 
254 /*
255  * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
256  * separated, with a newline at the end and null-terminated.
257  */
258 #define LINE_LEN(values_cnt)	(10 + 11 * values_cnt + 1 + 1)
259 #define MAX_LINE_LEN		(LINE_LEN(DRM_MAX_CRC_NR))
260 
261 static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
262 			     size_t count, loff_t *pos)
263 {
264 	struct drm_crtc *crtc = filep->f_inode->i_private;
265 	struct drm_crtc_crc *crc = &crtc->crc;
266 	struct drm_crtc_crc_entry *entry;
267 	char buf[MAX_LINE_LEN];
268 	int ret, i;
269 
270 	spin_lock_irq(&crc->lock);
271 
272 	if (!crc->source) {
273 		spin_unlock_irq(&crc->lock);
274 		return 0;
275 	}
276 
277 	/* Nothing to read? */
278 	while (crtc_crc_data_count(crc) == 0) {
279 		if (filep->f_flags & O_NONBLOCK) {
280 			spin_unlock_irq(&crc->lock);
281 			return -EAGAIN;
282 		}
283 
284 		ret = wait_event_interruptible_lock_irq(crc->wq,
285 							crtc_crc_data_count(crc),
286 							crc->lock);
287 		if (ret) {
288 			spin_unlock_irq(&crc->lock);
289 			return ret;
290 		}
291 	}
292 
293 	/* We know we have an entry to be read */
294 	entry = &crc->entries[crc->tail];
295 
296 	if (count < LINE_LEN(crc->values_cnt)) {
297 		spin_unlock_irq(&crc->lock);
298 		return -EINVAL;
299 	}
300 
301 	BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
302 	crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
303 
304 	spin_unlock_irq(&crc->lock);
305 
306 	if (entry->has_frame_counter)
307 		sprintf(buf, "0x%08x", entry->frame);
308 	else
309 		sprintf(buf, "XXXXXXXXXX");
310 
311 	for (i = 0; i < crc->values_cnt; i++)
312 		sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
313 	sprintf(buf + 10 + crc->values_cnt * 11, "\n");
314 
315 	if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
316 		return -EFAULT;
317 
318 	return LINE_LEN(crc->values_cnt);
319 }
320 
321 static unsigned int crtc_crc_poll(struct file *file, poll_table *wait)
322 {
323 	struct drm_crtc *crtc = file->f_inode->i_private;
324 	struct drm_crtc_crc *crc = &crtc->crc;
325 	unsigned ret;
326 
327 	poll_wait(file, &crc->wq, wait);
328 
329 	spin_lock_irq(&crc->lock);
330 	if (crc->source && crtc_crc_data_count(crc))
331 		ret = POLLIN | POLLRDNORM;
332 	else
333 		ret = 0;
334 	spin_unlock_irq(&crc->lock);
335 
336 	return ret;
337 }
338 
339 static const struct file_operations drm_crtc_crc_data_fops = {
340 	.owner = THIS_MODULE,
341 	.open = crtc_crc_open,
342 	.read = crtc_crc_read,
343 	.poll = crtc_crc_poll,
344 	.release = crtc_crc_release,
345 };
346 
347 int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
348 {
349 	struct dentry *crc_ent, *ent;
350 
351 	if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source)
352 		return 0;
353 
354 	crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
355 	if (!crc_ent)
356 		return -ENOMEM;
357 
358 	ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
359 				  &drm_crtc_crc_control_fops);
360 	if (!ent)
361 		goto error;
362 
363 	ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
364 				  &drm_crtc_crc_data_fops);
365 	if (!ent)
366 		goto error;
367 
368 	return 0;
369 
370 error:
371 	debugfs_remove_recursive(crc_ent);
372 
373 	return -ENOMEM;
374 }
375 
376 /**
377  * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
378  * @crtc: CRTC to which the frame belongs
379  * @has_frame: whether this entry has a frame number to go with
380  * @frame: number of the frame these CRCs are about
381  * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
382  *
383  * For each frame, the driver polls the source of CRCs for new data and calls
384  * this function to add them to the buffer from where userspace reads.
385  */
386 int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
387 			   uint32_t frame, uint32_t *crcs)
388 {
389 	struct drm_crtc_crc *crc = &crtc->crc;
390 	struct drm_crtc_crc_entry *entry;
391 	int head, tail;
392 
393 	spin_lock(&crc->lock);
394 
395 	/* Caller may not have noticed yet that userspace has stopped reading */
396 	if (!crc->entries) {
397 		spin_unlock(&crc->lock);
398 		return -EINVAL;
399 	}
400 
401 	head = crc->head;
402 	tail = crc->tail;
403 
404 	if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
405 		bool was_overflow = crc->overflow;
406 
407 		crc->overflow = true;
408 		spin_unlock(&crc->lock);
409 
410 		if (!was_overflow)
411 			DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
412 
413 		return -ENOBUFS;
414 	}
415 
416 	entry = &crc->entries[head];
417 	entry->frame = frame;
418 	entry->has_frame_counter = has_frame;
419 	memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
420 
421 	head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
422 	crc->head = head;
423 
424 	spin_unlock(&crc->lock);
425 
426 	wake_up_interruptible(&crc->wq);
427 
428 	return 0;
429 }
430 EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);
431