2 * Copyright © 2008 Intel Corporation
3 * Copyright © 2016 Collabora Ltd
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:
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
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
24 * Based on code from the i915 driver.
25 * Original author: Damien Lespiau <damien.lespiau@intel.com>
29 #include <linux/circ_buf.h>
30 #include <linux/ctype.h>
31 #include <linux/debugfs.h>
33 #include "drm_internal.h"
38 * DRM device drivers can provide to userspace CRC information of each frame as
39 * it reached a given hardware component (a "source").
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
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.
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.
62 static int crc_control_show(struct seq_file *m, void *data)
64 struct drm_crtc *crtc = m->private;
66 seq_printf(m, "%s\n", crtc->crc.source);
71 static int crc_control_open(struct inode *inode, struct file *file)
73 struct drm_crtc *crtc = inode->i_private;
75 return single_open(file, crc_control_show, crtc);
78 static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
79 size_t len, loff_t *offp)
81 struct seq_file *m = file->private_data;
82 struct drm_crtc *crtc = m->private;
83 struct drm_crtc_crc *crc = &crtc->crc;
89 if (len > PAGE_SIZE - 1) {
90 DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
95 source = memdup_user_nul(ubuf, len);
97 return PTR_ERR(source);
99 if (source[len] == '\n')
102 spin_lock_irq(&crc->lock);
105 spin_unlock_irq(&crc->lock);
111 crc->source = source;
113 spin_unlock_irq(&crc->lock);
119 static const struct file_operations drm_crtc_crc_control_fops = {
120 .owner = THIS_MODULE,
121 .open = crc_control_open,
124 .release = single_release,
125 .write = crc_control_write
128 static int crtc_crc_data_count(struct drm_crtc_crc *crc)
130 assert_spin_locked(&crc->lock);
131 return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
134 static int crtc_crc_open(struct inode *inode, struct file *filep)
136 struct drm_crtc *crtc = inode->i_private;
137 struct drm_crtc_crc *crc = &crtc->crc;
138 struct drm_crtc_crc_entry *entries = NULL;
145 ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
149 if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
154 if (WARN_ON(values_cnt == 0)) {
159 entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
165 spin_lock_irq(&crc->lock);
166 crc->entries = entries;
167 crc->values_cnt = values_cnt;
171 * Only return once we got a first frame, so userspace doesn't have to
172 * guess when this particular piece of HW will be ready to start
175 ret = wait_event_interruptible_lock_irq(crc->wq,
176 crtc_crc_data_count(crc),
178 spin_unlock_irq(&crc->lock);
185 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
189 static int crtc_crc_release(struct inode *inode, struct file *filep)
191 struct drm_crtc *crtc = filep->f_inode->i_private;
192 struct drm_crtc_crc *crc = &crtc->crc;
195 spin_lock_irq(&crc->lock);
202 spin_unlock_irq(&crc->lock);
204 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
210 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
211 * separated, with a newline at the end and null-terminated.
213 #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
214 #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
216 static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
217 size_t count, loff_t *pos)
219 struct drm_crtc *crtc = filep->f_inode->i_private;
220 struct drm_crtc_crc *crc = &crtc->crc;
221 struct drm_crtc_crc_entry *entry;
222 char buf[MAX_LINE_LEN];
225 spin_lock_irq(&crc->lock);
228 spin_unlock_irq(&crc->lock);
232 /* Nothing to read? */
233 while (crtc_crc_data_count(crc) == 0) {
234 if (filep->f_flags & O_NONBLOCK) {
235 spin_unlock_irq(&crc->lock);
239 ret = wait_event_interruptible_lock_irq(crc->wq,
240 crtc_crc_data_count(crc),
243 spin_unlock_irq(&crc->lock);
248 /* We know we have an entry to be read */
249 entry = &crc->entries[crc->tail];
251 if (count < LINE_LEN(crc->values_cnt)) {
252 spin_unlock_irq(&crc->lock);
256 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
257 crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
259 spin_unlock_irq(&crc->lock);
261 if (entry->has_frame_counter)
262 sprintf(buf, "0x%08x", entry->frame);
264 sprintf(buf, "XXXXXXXXXX");
266 for (i = 0; i < crc->values_cnt; i++)
267 sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
268 sprintf(buf + 10 + crc->values_cnt * 11, "\n");
270 if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
273 return LINE_LEN(crc->values_cnt);
276 static const struct file_operations drm_crtc_crc_data_fops = {
277 .owner = THIS_MODULE,
278 .open = crtc_crc_open,
279 .read = crtc_crc_read,
280 .release = crtc_crc_release,
284 * drm_debugfs_crtc_crc_add - Add files to debugfs for capture of frame CRCs
285 * @crtc: CRTC to whom the frames will belong
287 * Adds files to debugfs directory that allows userspace to control the
288 * generation of frame CRCs and to read them.
291 * Zero on success, error code on failure.
293 int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
295 struct dentry *crc_ent, *ent;
297 if (!crtc->funcs->set_crc_source)
300 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
304 ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
305 &drm_crtc_crc_control_fops);
309 ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
310 &drm_crtc_crc_data_fops);
317 debugfs_remove_recursive(crc_ent);
323 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
324 * @crtc: CRTC to which the frame belongs
325 * @has_frame: whether this entry has a frame number to go with
326 * @frame: number of the frame these CRCs are about
327 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
329 * For each frame, the driver polls the source of CRCs for new data and calls
330 * this function to add them to the buffer from where userspace reads.
332 int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
333 uint32_t frame, uint32_t *crcs)
335 struct drm_crtc_crc *crc = &crtc->crc;
336 struct drm_crtc_crc_entry *entry;
339 spin_lock(&crc->lock);
341 /* Caller may not have noticed yet that userspace has stopped reading */
343 spin_unlock(&crc->lock);
350 if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
351 spin_unlock(&crc->lock);
352 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
356 entry = &crc->entries[head];
357 entry->frame = frame;
358 entry->has_frame_counter = has_frame;
359 memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
361 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
364 spin_unlock(&crc->lock);
366 wake_up_interruptible(&crc->wq);
370 EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);