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.
29 #include <linux/circ_buf.h>
30 #include <linux/ctype.h>
31 #include <linux/debugfs.h>
32 #include <linux/poll.h>
33 #include <linux/uaccess.h>
35 #include <drm/drm_crtc.h>
36 #include <drm/drm_debugfs_crc.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_print.h>
40 #include "drm_internal.h"
45 * DRM device drivers can provide to userspace CRC information of each frame as
46 * it reached a given hardware component (a CRC sampling "source").
48 * Userspace can control generation of CRCs in a given CRTC by writing to the
49 * file dri/0/crtc-N/crc/control in debugfs, with N being the index of the CRTC.
50 * Accepted values are source names (which are driver-specific) and the "auto"
51 * keyword, which will let the driver select a default source of frame CRCs
54 * Once frame CRC generation is enabled, userspace can capture them by reading
55 * the dri/0/crtc-N/crc/data file. Each line in that file contains the frame
56 * number in the first field and then a number of unsigned integer fields
57 * containing the CRC data. Fields are separated by a single space and the number
58 * of CRC fields is source-specific.
60 * Note that though in some cases the CRC is computed in a specified way and on
61 * the frame contents as supplied by userspace (eDP 1.3), in general the CRC
62 * computation is performed in an unspecified way and on frame contents that have
63 * been already processed in also an unspecified way and thus userspace cannot
64 * rely on being able to generate matching CRC values for the frame contents that
65 * it submits. In this general case, the maximum userspace can do is to compare
66 * the reported CRCs of frames that should have the same contents.
68 * On the driver side the implementation effort is minimal, drivers only need to
69 * implement &drm_crtc_funcs.set_crc_source. The debugfs files are automatically
70 * set up if that vfunc is set. CRC samples need to be captured in the driver by
71 * calling drm_crtc_add_crc_entry().
74 static int crc_control_show(struct seq_file *m, void *data)
76 struct drm_crtc *crtc = m->private;
78 if (crtc->funcs->get_crc_sources) {
80 const char *const *sources = crtc->funcs->get_crc_sources(crtc,
85 if (count == 0 || !sources)
88 for (i = 0; i < count; i++)
89 if (!crtc->funcs->verify_crc_source(crtc, sources[i],
91 if (strcmp(sources[i], crtc->crc.source))
92 seq_printf(m, "%s\n", sources[i]);
94 seq_printf(m, "%s*\n", sources[i]);
100 seq_printf(m, "%s*\n", crtc->crc.source);
104 static int crc_control_open(struct inode *inode, struct file *file)
106 struct drm_crtc *crtc = inode->i_private;
108 return single_open(file, crc_control_show, crtc);
111 static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
112 size_t len, loff_t *offp)
114 struct seq_file *m = file->private_data;
115 struct drm_crtc *crtc = m->private;
116 struct drm_crtc_crc *crc = &crtc->crc;
124 if (len > PAGE_SIZE - 1) {
125 DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
130 source = memdup_user_nul(ubuf, len);
132 return PTR_ERR(source);
134 if (source[len] == '\n')
137 ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
141 spin_lock_irq(&crc->lock);
144 spin_unlock_irq(&crc->lock);
150 crc->source = source;
152 spin_unlock_irq(&crc->lock);
158 static const struct file_operations drm_crtc_crc_control_fops = {
159 .owner = THIS_MODULE,
160 .open = crc_control_open,
163 .release = single_release,
164 .write = crc_control_write
167 static int crtc_crc_data_count(struct drm_crtc_crc *crc)
169 assert_spin_locked(&crc->lock);
170 return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
173 static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
176 crc->overflow = false;
184 static int crtc_crc_open(struct inode *inode, struct file *filep)
186 struct drm_crtc *crtc = inode->i_private;
187 struct drm_crtc_crc *crc = &crtc->crc;
188 struct drm_crtc_crc_entry *entries = NULL;
192 if (drm_drv_uses_atomic_modeset(crtc->dev)) {
193 ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
197 if (!crtc->state->active)
199 drm_modeset_unlock(&crtc->mutex);
205 ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt);
209 if (WARN_ON(values_cnt > DRM_MAX_CRC_NR))
212 if (WARN_ON(values_cnt == 0))
215 entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
219 spin_lock_irq(&crc->lock);
222 crc->entries = entries;
223 crc->values_cnt = values_cnt;
227 spin_unlock_irq(&crc->lock);
234 ret = crtc->funcs->set_crc_source(crtc, crc->source);
241 spin_lock_irq(&crc->lock);
242 crtc_crc_cleanup(crc);
243 spin_unlock_irq(&crc->lock);
247 static int crtc_crc_release(struct inode *inode, struct file *filep)
249 struct drm_crtc *crtc = filep->f_inode->i_private;
250 struct drm_crtc_crc *crc = &crtc->crc;
252 crtc->funcs->set_crc_source(crtc, NULL);
254 spin_lock_irq(&crc->lock);
255 crtc_crc_cleanup(crc);
256 spin_unlock_irq(&crc->lock);
262 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
263 * separated, with a newline at the end and null-terminated.
265 #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
266 #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
268 static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
269 size_t count, loff_t *pos)
271 struct drm_crtc *crtc = filep->f_inode->i_private;
272 struct drm_crtc_crc *crc = &crtc->crc;
273 struct drm_crtc_crc_entry *entry;
274 char buf[MAX_LINE_LEN];
277 spin_lock_irq(&crc->lock);
280 spin_unlock_irq(&crc->lock);
284 /* Nothing to read? */
285 while (crtc_crc_data_count(crc) == 0) {
286 if (filep->f_flags & O_NONBLOCK) {
287 spin_unlock_irq(&crc->lock);
291 ret = wait_event_interruptible_lock_irq(crc->wq,
292 crtc_crc_data_count(crc),
295 spin_unlock_irq(&crc->lock);
300 /* We know we have an entry to be read */
301 entry = &crc->entries[crc->tail];
303 if (count < LINE_LEN(crc->values_cnt)) {
304 spin_unlock_irq(&crc->lock);
308 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
309 crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
311 spin_unlock_irq(&crc->lock);
313 if (entry->has_frame_counter)
314 sprintf(buf, "0x%08x", entry->frame);
316 sprintf(buf, "XXXXXXXXXX");
318 for (i = 0; i < crc->values_cnt; i++)
319 sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
320 sprintf(buf + 10 + crc->values_cnt * 11, "\n");
322 if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
325 return LINE_LEN(crc->values_cnt);
328 static unsigned int crtc_crc_poll(struct file *file, poll_table *wait)
330 struct drm_crtc *crtc = file->f_inode->i_private;
331 struct drm_crtc_crc *crc = &crtc->crc;
334 poll_wait(file, &crc->wq, wait);
336 spin_lock_irq(&crc->lock);
337 if (crc->source && crtc_crc_data_count(crc))
338 ret = POLLIN | POLLRDNORM;
341 spin_unlock_irq(&crc->lock);
346 static const struct file_operations drm_crtc_crc_data_fops = {
347 .owner = THIS_MODULE,
348 .open = crtc_crc_open,
349 .read = crtc_crc_read,
350 .poll = crtc_crc_poll,
351 .release = crtc_crc_release,
354 void drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
356 struct dentry *crc_ent;
358 if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source)
361 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
363 debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
364 &drm_crtc_crc_control_fops);
365 debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
366 &drm_crtc_crc_data_fops);
370 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
371 * @crtc: CRTC to which the frame belongs
372 * @has_frame: whether this entry has a frame number to go with
373 * @frame: number of the frame these CRCs are about
374 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
376 * For each frame, the driver polls the source of CRCs for new data and calls
377 * this function to add them to the buffer from where userspace reads.
379 int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
380 uint32_t frame, uint32_t *crcs)
382 struct drm_crtc_crc *crc = &crtc->crc;
383 struct drm_crtc_crc_entry *entry;
387 spin_lock_irqsave(&crc->lock, flags);
389 /* Caller may not have noticed yet that userspace has stopped reading */
391 spin_unlock_irqrestore(&crc->lock, flags);
398 if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
399 bool was_overflow = crc->overflow;
401 crc->overflow = true;
402 spin_unlock_irqrestore(&crc->lock, flags);
405 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
410 entry = &crc->entries[head];
411 entry->frame = frame;
412 entry->has_frame_counter = has_frame;
413 memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
415 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
418 spin_unlock_irqrestore(&crc->lock, flags);
420 wake_up_interruptible(&crc->wq);
424 EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);