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 CRC sampling "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.
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().
67 static int crc_control_show(struct seq_file
*m
, void *data
)
69 struct drm_crtc
*crtc
= m
->private;
71 seq_printf(m
, "%s\n", crtc
->crc
.source
);
76 static int crc_control_open(struct inode
*inode
, struct file
*file
)
78 struct drm_crtc
*crtc
= inode
->i_private
;
80 return single_open(file
, crc_control_show
, crtc
);
83 static ssize_t
crc_control_write(struct file
*file
, const char __user
*ubuf
,
84 size_t len
, loff_t
*offp
)
86 struct seq_file
*m
= file
->private_data
;
87 struct drm_crtc
*crtc
= m
->private;
88 struct drm_crtc_crc
*crc
= &crtc
->crc
;
94 if (len
> PAGE_SIZE
- 1) {
95 DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
100 source
= memdup_user_nul(ubuf
, len
);
102 return PTR_ERR(source
);
104 if (source
[len
] == '\n')
107 spin_lock_irq(&crc
->lock
);
110 spin_unlock_irq(&crc
->lock
);
116 crc
->source
= source
;
118 spin_unlock_irq(&crc
->lock
);
124 static const struct file_operations drm_crtc_crc_control_fops
= {
125 .owner
= THIS_MODULE
,
126 .open
= crc_control_open
,
129 .release
= single_release
,
130 .write
= crc_control_write
133 static int crtc_crc_data_count(struct drm_crtc_crc
*crc
)
135 assert_spin_locked(&crc
->lock
);
136 return CIRC_CNT(crc
->head
, crc
->tail
, DRM_CRC_ENTRIES_NR
);
139 static void crtc_crc_cleanup(struct drm_crtc_crc
*crc
)
149 static int crtc_crc_open(struct inode
*inode
, struct file
*filep
)
151 struct drm_crtc
*crtc
= inode
->i_private
;
152 struct drm_crtc_crc
*crc
= &crtc
->crc
;
153 struct drm_crtc_crc_entry
*entries
= NULL
;
157 if (drm_drv_uses_atomic_modeset(crtc
->dev
)) {
158 ret
= drm_modeset_lock_single_interruptible(&crtc
->mutex
);
162 if (!crtc
->state
->active
)
164 drm_modeset_unlock(&crtc
->mutex
);
170 spin_lock_irq(&crc
->lock
);
175 spin_unlock_irq(&crc
->lock
);
180 ret
= crtc
->funcs
->set_crc_source(crtc
, crc
->source
, &values_cnt
);
184 if (WARN_ON(values_cnt
> DRM_MAX_CRC_NR
)) {
189 if (WARN_ON(values_cnt
== 0)) {
194 entries
= kcalloc(DRM_CRC_ENTRIES_NR
, sizeof(*entries
), GFP_KERNEL
);
200 spin_lock_irq(&crc
->lock
);
201 crc
->entries
= entries
;
202 crc
->values_cnt
= values_cnt
;
205 * Only return once we got a first frame, so userspace doesn't have to
206 * guess when this particular piece of HW will be ready to start
209 ret
= wait_event_interruptible_lock_irq(crc
->wq
,
210 crtc_crc_data_count(crc
),
212 spin_unlock_irq(&crc
->lock
);
220 crtc
->funcs
->set_crc_source(crtc
, NULL
, &values_cnt
);
222 spin_lock_irq(&crc
->lock
);
223 crtc_crc_cleanup(crc
);
224 spin_unlock_irq(&crc
->lock
);
228 static int crtc_crc_release(struct inode
*inode
, struct file
*filep
)
230 struct drm_crtc
*crtc
= filep
->f_inode
->i_private
;
231 struct drm_crtc_crc
*crc
= &crtc
->crc
;
234 crtc
->funcs
->set_crc_source(crtc
, NULL
, &values_cnt
);
236 spin_lock_irq(&crc
->lock
);
237 crtc_crc_cleanup(crc
);
238 spin_unlock_irq(&crc
->lock
);
244 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
245 * separated, with a newline at the end and null-terminated.
247 #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
248 #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
250 static ssize_t
crtc_crc_read(struct file
*filep
, char __user
*user_buf
,
251 size_t count
, loff_t
*pos
)
253 struct drm_crtc
*crtc
= filep
->f_inode
->i_private
;
254 struct drm_crtc_crc
*crc
= &crtc
->crc
;
255 struct drm_crtc_crc_entry
*entry
;
256 char buf
[MAX_LINE_LEN
];
259 spin_lock_irq(&crc
->lock
);
262 spin_unlock_irq(&crc
->lock
);
266 /* Nothing to read? */
267 while (crtc_crc_data_count(crc
) == 0) {
268 if (filep
->f_flags
& O_NONBLOCK
) {
269 spin_unlock_irq(&crc
->lock
);
273 ret
= wait_event_interruptible_lock_irq(crc
->wq
,
274 crtc_crc_data_count(crc
),
277 spin_unlock_irq(&crc
->lock
);
282 /* We know we have an entry to be read */
283 entry
= &crc
->entries
[crc
->tail
];
285 if (count
< LINE_LEN(crc
->values_cnt
)) {
286 spin_unlock_irq(&crc
->lock
);
290 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR
);
291 crc
->tail
= (crc
->tail
+ 1) & (DRM_CRC_ENTRIES_NR
- 1);
293 spin_unlock_irq(&crc
->lock
);
295 if (entry
->has_frame_counter
)
296 sprintf(buf
, "0x%08x", entry
->frame
);
298 sprintf(buf
, "XXXXXXXXXX");
300 for (i
= 0; i
< crc
->values_cnt
; i
++)
301 sprintf(buf
+ 10 + i
* 11, " 0x%08x", entry
->crcs
[i
]);
302 sprintf(buf
+ 10 + crc
->values_cnt
* 11, "\n");
304 if (copy_to_user(user_buf
, buf
, LINE_LEN(crc
->values_cnt
)))
307 return LINE_LEN(crc
->values_cnt
);
310 static const struct file_operations drm_crtc_crc_data_fops
= {
311 .owner
= THIS_MODULE
,
312 .open
= crtc_crc_open
,
313 .read
= crtc_crc_read
,
314 .release
= crtc_crc_release
,
317 int drm_debugfs_crtc_crc_add(struct drm_crtc
*crtc
)
319 struct dentry
*crc_ent
, *ent
;
321 if (!crtc
->funcs
->set_crc_source
)
324 crc_ent
= debugfs_create_dir("crc", crtc
->debugfs_entry
);
328 ent
= debugfs_create_file("control", S_IRUGO
, crc_ent
, crtc
,
329 &drm_crtc_crc_control_fops
);
333 ent
= debugfs_create_file("data", S_IRUGO
, crc_ent
, crtc
,
334 &drm_crtc_crc_data_fops
);
341 debugfs_remove_recursive(crc_ent
);
347 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
348 * @crtc: CRTC to which the frame belongs
349 * @has_frame: whether this entry has a frame number to go with
350 * @frame: number of the frame these CRCs are about
351 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
353 * For each frame, the driver polls the source of CRCs for new data and calls
354 * this function to add them to the buffer from where userspace reads.
356 int drm_crtc_add_crc_entry(struct drm_crtc
*crtc
, bool has_frame
,
357 uint32_t frame
, uint32_t *crcs
)
359 struct drm_crtc_crc
*crc
= &crtc
->crc
;
360 struct drm_crtc_crc_entry
*entry
;
363 spin_lock(&crc
->lock
);
365 /* Caller may not have noticed yet that userspace has stopped reading */
367 spin_unlock(&crc
->lock
);
374 if (CIRC_SPACE(head
, tail
, DRM_CRC_ENTRIES_NR
) < 1) {
375 spin_unlock(&crc
->lock
);
376 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
380 entry
= &crc
->entries
[head
];
381 entry
->frame
= frame
;
382 entry
->has_frame_counter
= has_frame
;
383 memcpy(&entry
->crcs
, crcs
, sizeof(*crcs
) * crc
->values_cnt
);
385 head
= (head
+ 1) & (DRM_CRC_ENTRIES_NR
- 1);
388 spin_unlock(&crc
->lock
);
390 wake_up_interruptible(&crc
->wq
);
394 EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry
);