1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2008 Intel Corporation
3*4882a593Smuzhiyun * Copyright © 2016 Collabora Ltd
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
6*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
7*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
8*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
10*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
13*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
14*4882a593Smuzhiyun * Software.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22*4882a593Smuzhiyun * IN THE SOFTWARE.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Based on code from the i915 driver.
25*4882a593Smuzhiyun * Original author: Damien Lespiau <damien.lespiau@intel.com>
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/circ_buf.h>
30*4882a593Smuzhiyun #include <linux/ctype.h>
31*4882a593Smuzhiyun #include <linux/debugfs.h>
32*4882a593Smuzhiyun #include <linux/poll.h>
33*4882a593Smuzhiyun #include <linux/uaccess.h>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include <drm/drm_crtc.h>
36*4882a593Smuzhiyun #include <drm/drm_debugfs_crc.h>
37*4882a593Smuzhiyun #include <drm/drm_drv.h>
38*4882a593Smuzhiyun #include <drm/drm_print.h>
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include "drm_internal.h"
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun * DOC: CRC ABI
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * DRM device drivers can provide to userspace CRC information of each frame as
46*4882a593Smuzhiyun * it reached a given hardware component (a CRC sampling "source").
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * Userspace can control generation of CRCs in a given CRTC by writing to the
49*4882a593Smuzhiyun * file dri/0/crtc-N/crc/control in debugfs, with N being the index of the CRTC.
50*4882a593Smuzhiyun * Accepted values are source names (which are driver-specific) and the "auto"
51*4882a593Smuzhiyun * keyword, which will let the driver select a default source of frame CRCs
52*4882a593Smuzhiyun * for this CRTC.
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * Once frame CRC generation is enabled, userspace can capture them by reading
55*4882a593Smuzhiyun * the dri/0/crtc-N/crc/data file. Each line in that file contains the frame
56*4882a593Smuzhiyun * number in the first field and then a number of unsigned integer fields
57*4882a593Smuzhiyun * containing the CRC data. Fields are separated by a single space and the number
58*4882a593Smuzhiyun * of CRC fields is source-specific.
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * Note that though in some cases the CRC is computed in a specified way and on
61*4882a593Smuzhiyun * the frame contents as supplied by userspace (eDP 1.3), in general the CRC
62*4882a593Smuzhiyun * computation is performed in an unspecified way and on frame contents that have
63*4882a593Smuzhiyun * been already processed in also an unspecified way and thus userspace cannot
64*4882a593Smuzhiyun * rely on being able to generate matching CRC values for the frame contents that
65*4882a593Smuzhiyun * it submits. In this general case, the maximum userspace can do is to compare
66*4882a593Smuzhiyun * the reported CRCs of frames that should have the same contents.
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * On the driver side the implementation effort is minimal, drivers only need to
69*4882a593Smuzhiyun * implement &drm_crtc_funcs.set_crc_source and &drm_crtc_funcs.verify_crc_source.
70*4882a593Smuzhiyun * The debugfs files are automatically set up if those vfuncs are set. CRC samples
71*4882a593Smuzhiyun * need to be captured in the driver by calling drm_crtc_add_crc_entry().
72*4882a593Smuzhiyun * Depending on the driver and HW requirements, &drm_crtc_funcs.set_crc_source
73*4882a593Smuzhiyun * may result in a commit (even a full modeset).
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * CRC results must be reliable across non-full-modeset atomic commits, so if a
76*4882a593Smuzhiyun * commit via DRM_IOCTL_MODE_ATOMIC would disable or otherwise interfere with
77*4882a593Smuzhiyun * CRC generation, then the driver must mark that commit as a full modeset
78*4882a593Smuzhiyun * (drm_atomic_crtc_needs_modeset() should return true). As a result, to ensure
79*4882a593Smuzhiyun * consistent results, generic userspace must re-setup CRC generation after a
80*4882a593Smuzhiyun * legacy SETCRTC or an atomic commit with DRM_MODE_ATOMIC_ALLOW_MODESET.
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun
crc_control_show(struct seq_file * m,void * data)83*4882a593Smuzhiyun static int crc_control_show(struct seq_file *m, void *data)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun struct drm_crtc *crtc = m->private;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun if (crtc->funcs->get_crc_sources) {
88*4882a593Smuzhiyun size_t count;
89*4882a593Smuzhiyun const char *const *sources = crtc->funcs->get_crc_sources(crtc,
90*4882a593Smuzhiyun &count);
91*4882a593Smuzhiyun size_t values_cnt;
92*4882a593Smuzhiyun int i;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if (count == 0 || !sources)
95*4882a593Smuzhiyun goto out;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun for (i = 0; i < count; i++)
98*4882a593Smuzhiyun if (!crtc->funcs->verify_crc_source(crtc, sources[i],
99*4882a593Smuzhiyun &values_cnt)) {
100*4882a593Smuzhiyun if (strcmp(sources[i], crtc->crc.source))
101*4882a593Smuzhiyun seq_printf(m, "%s\n", sources[i]);
102*4882a593Smuzhiyun else
103*4882a593Smuzhiyun seq_printf(m, "%s*\n", sources[i]);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun return 0;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun out:
109*4882a593Smuzhiyun seq_printf(m, "%s*\n", crtc->crc.source);
110*4882a593Smuzhiyun return 0;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
crc_control_open(struct inode * inode,struct file * file)113*4882a593Smuzhiyun static int crc_control_open(struct inode *inode, struct file *file)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct drm_crtc *crtc = inode->i_private;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun return single_open(file, crc_control_show, crtc);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
crc_control_write(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)120*4882a593Smuzhiyun static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
121*4882a593Smuzhiyun size_t len, loff_t *offp)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun struct seq_file *m = file->private_data;
124*4882a593Smuzhiyun struct drm_crtc *crtc = m->private;
125*4882a593Smuzhiyun struct drm_crtc_crc *crc = &crtc->crc;
126*4882a593Smuzhiyun char *source;
127*4882a593Smuzhiyun size_t values_cnt;
128*4882a593Smuzhiyun int ret;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (len == 0)
131*4882a593Smuzhiyun return 0;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (len > PAGE_SIZE - 1) {
134*4882a593Smuzhiyun DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
135*4882a593Smuzhiyun PAGE_SIZE);
136*4882a593Smuzhiyun return -E2BIG;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun source = memdup_user_nul(ubuf, len);
140*4882a593Smuzhiyun if (IS_ERR(source))
141*4882a593Smuzhiyun return PTR_ERR(source);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun if (source[len - 1] == '\n')
144*4882a593Smuzhiyun source[len - 1] = '\0';
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
147*4882a593Smuzhiyun if (ret) {
148*4882a593Smuzhiyun kfree(source);
149*4882a593Smuzhiyun return ret;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun spin_lock_irq(&crc->lock);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun if (crc->opened) {
155*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
156*4882a593Smuzhiyun kfree(source);
157*4882a593Smuzhiyun return -EBUSY;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun kfree(crc->source);
161*4882a593Smuzhiyun crc->source = source;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun *offp += len;
166*4882a593Smuzhiyun return len;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun static const struct file_operations drm_crtc_crc_control_fops = {
170*4882a593Smuzhiyun .owner = THIS_MODULE,
171*4882a593Smuzhiyun .open = crc_control_open,
172*4882a593Smuzhiyun .read = seq_read,
173*4882a593Smuzhiyun .llseek = seq_lseek,
174*4882a593Smuzhiyun .release = single_release,
175*4882a593Smuzhiyun .write = crc_control_write
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun
crtc_crc_data_count(struct drm_crtc_crc * crc)178*4882a593Smuzhiyun static int crtc_crc_data_count(struct drm_crtc_crc *crc)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun assert_spin_locked(&crc->lock);
181*4882a593Smuzhiyun return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
crtc_crc_cleanup(struct drm_crtc_crc * crc)184*4882a593Smuzhiyun static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun kfree(crc->entries);
187*4882a593Smuzhiyun crc->overflow = false;
188*4882a593Smuzhiyun crc->entries = NULL;
189*4882a593Smuzhiyun crc->head = 0;
190*4882a593Smuzhiyun crc->tail = 0;
191*4882a593Smuzhiyun crc->values_cnt = 0;
192*4882a593Smuzhiyun crc->opened = false;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
crtc_crc_open(struct inode * inode,struct file * filep)195*4882a593Smuzhiyun static int crtc_crc_open(struct inode *inode, struct file *filep)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun struct drm_crtc *crtc = inode->i_private;
198*4882a593Smuzhiyun struct drm_crtc_crc *crc = &crtc->crc;
199*4882a593Smuzhiyun struct drm_crtc_crc_entry *entries = NULL;
200*4882a593Smuzhiyun size_t values_cnt;
201*4882a593Smuzhiyun int ret = 0;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (drm_drv_uses_atomic_modeset(crtc->dev)) {
204*4882a593Smuzhiyun ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
205*4882a593Smuzhiyun if (ret)
206*4882a593Smuzhiyun return ret;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (!crtc->state->active)
209*4882a593Smuzhiyun ret = -EIO;
210*4882a593Smuzhiyun drm_modeset_unlock(&crtc->mutex);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (ret)
213*4882a593Smuzhiyun return ret;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt);
217*4882a593Smuzhiyun if (ret)
218*4882a593Smuzhiyun return ret;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (WARN_ON(values_cnt > DRM_MAX_CRC_NR))
221*4882a593Smuzhiyun return -EINVAL;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun if (WARN_ON(values_cnt == 0))
224*4882a593Smuzhiyun return -EINVAL;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
227*4882a593Smuzhiyun if (!entries)
228*4882a593Smuzhiyun return -ENOMEM;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun spin_lock_irq(&crc->lock);
231*4882a593Smuzhiyun if (!crc->opened) {
232*4882a593Smuzhiyun crc->opened = true;
233*4882a593Smuzhiyun crc->entries = entries;
234*4882a593Smuzhiyun crc->values_cnt = values_cnt;
235*4882a593Smuzhiyun } else {
236*4882a593Smuzhiyun ret = -EBUSY;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun if (ret) {
241*4882a593Smuzhiyun kfree(entries);
242*4882a593Smuzhiyun return ret;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun ret = crtc->funcs->set_crc_source(crtc, crc->source);
246*4882a593Smuzhiyun if (ret)
247*4882a593Smuzhiyun goto err;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun return 0;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun err:
252*4882a593Smuzhiyun spin_lock_irq(&crc->lock);
253*4882a593Smuzhiyun crtc_crc_cleanup(crc);
254*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
crtc_crc_release(struct inode * inode,struct file * filep)258*4882a593Smuzhiyun static int crtc_crc_release(struct inode *inode, struct file *filep)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun struct drm_crtc *crtc = filep->f_inode->i_private;
261*4882a593Smuzhiyun struct drm_crtc_crc *crc = &crtc->crc;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* terminate the infinite while loop if 'drm_dp_aux_crc_work' running */
264*4882a593Smuzhiyun spin_lock_irq(&crc->lock);
265*4882a593Smuzhiyun crc->opened = false;
266*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun crtc->funcs->set_crc_source(crtc, NULL);
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun spin_lock_irq(&crc->lock);
271*4882a593Smuzhiyun crtc_crc_cleanup(crc);
272*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun return 0;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /*
278*4882a593Smuzhiyun * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
279*4882a593Smuzhiyun * separated, with a newline at the end and null-terminated.
280*4882a593Smuzhiyun */
281*4882a593Smuzhiyun #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
282*4882a593Smuzhiyun #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
283*4882a593Smuzhiyun
crtc_crc_read(struct file * filep,char __user * user_buf,size_t count,loff_t * pos)284*4882a593Smuzhiyun static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
285*4882a593Smuzhiyun size_t count, loff_t *pos)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun struct drm_crtc *crtc = filep->f_inode->i_private;
288*4882a593Smuzhiyun struct drm_crtc_crc *crc = &crtc->crc;
289*4882a593Smuzhiyun struct drm_crtc_crc_entry *entry;
290*4882a593Smuzhiyun char buf[MAX_LINE_LEN];
291*4882a593Smuzhiyun int ret, i;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun spin_lock_irq(&crc->lock);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun if (!crc->source) {
296*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
297*4882a593Smuzhiyun return 0;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /* Nothing to read? */
301*4882a593Smuzhiyun while (crtc_crc_data_count(crc) == 0) {
302*4882a593Smuzhiyun if (filep->f_flags & O_NONBLOCK) {
303*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
304*4882a593Smuzhiyun return -EAGAIN;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun ret = wait_event_interruptible_lock_irq(crc->wq,
308*4882a593Smuzhiyun crtc_crc_data_count(crc),
309*4882a593Smuzhiyun crc->lock);
310*4882a593Smuzhiyun if (ret) {
311*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
312*4882a593Smuzhiyun return ret;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun /* We know we have an entry to be read */
317*4882a593Smuzhiyun entry = &crc->entries[crc->tail];
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun if (count < LINE_LEN(crc->values_cnt)) {
320*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
321*4882a593Smuzhiyun return -EINVAL;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
325*4882a593Smuzhiyun crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (entry->has_frame_counter)
330*4882a593Smuzhiyun sprintf(buf, "0x%08x", entry->frame);
331*4882a593Smuzhiyun else
332*4882a593Smuzhiyun sprintf(buf, "XXXXXXXXXX");
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun for (i = 0; i < crc->values_cnt; i++)
335*4882a593Smuzhiyun sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
336*4882a593Smuzhiyun sprintf(buf + 10 + crc->values_cnt * 11, "\n");
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
339*4882a593Smuzhiyun return -EFAULT;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun return LINE_LEN(crc->values_cnt);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
crtc_crc_poll(struct file * file,poll_table * wait)344*4882a593Smuzhiyun static __poll_t crtc_crc_poll(struct file *file, poll_table *wait)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun struct drm_crtc *crtc = file->f_inode->i_private;
347*4882a593Smuzhiyun struct drm_crtc_crc *crc = &crtc->crc;
348*4882a593Smuzhiyun __poll_t ret = 0;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun poll_wait(file, &crc->wq, wait);
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun spin_lock_irq(&crc->lock);
353*4882a593Smuzhiyun if (crc->source && crtc_crc_data_count(crc))
354*4882a593Smuzhiyun ret |= EPOLLIN | EPOLLRDNORM;
355*4882a593Smuzhiyun spin_unlock_irq(&crc->lock);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun return ret;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun static const struct file_operations drm_crtc_crc_data_fops = {
361*4882a593Smuzhiyun .owner = THIS_MODULE,
362*4882a593Smuzhiyun .open = crtc_crc_open,
363*4882a593Smuzhiyun .read = crtc_crc_read,
364*4882a593Smuzhiyun .poll = crtc_crc_poll,
365*4882a593Smuzhiyun .release = crtc_crc_release,
366*4882a593Smuzhiyun };
367*4882a593Smuzhiyun
drm_debugfs_crtc_crc_add(struct drm_crtc * crtc)368*4882a593Smuzhiyun void drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun struct dentry *crc_ent;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source)
373*4882a593Smuzhiyun return;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun debugfs_create_file("control", S_IRUGO | S_IWUSR, crc_ent, crtc,
378*4882a593Smuzhiyun &drm_crtc_crc_control_fops);
379*4882a593Smuzhiyun debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
380*4882a593Smuzhiyun &drm_crtc_crc_data_fops);
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun /**
384*4882a593Smuzhiyun * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
385*4882a593Smuzhiyun * @crtc: CRTC to which the frame belongs
386*4882a593Smuzhiyun * @has_frame: whether this entry has a frame number to go with
387*4882a593Smuzhiyun * @frame: number of the frame these CRCs are about
388*4882a593Smuzhiyun * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * For each frame, the driver polls the source of CRCs for new data and calls
391*4882a593Smuzhiyun * this function to add them to the buffer from where userspace reads.
392*4882a593Smuzhiyun */
drm_crtc_add_crc_entry(struct drm_crtc * crtc,bool has_frame,uint32_t frame,uint32_t * crcs)393*4882a593Smuzhiyun int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
394*4882a593Smuzhiyun uint32_t frame, uint32_t *crcs)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun struct drm_crtc_crc *crc = &crtc->crc;
397*4882a593Smuzhiyun struct drm_crtc_crc_entry *entry;
398*4882a593Smuzhiyun int head, tail;
399*4882a593Smuzhiyun unsigned long flags;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun spin_lock_irqsave(&crc->lock, flags);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun /* Caller may not have noticed yet that userspace has stopped reading */
404*4882a593Smuzhiyun if (!crc->entries) {
405*4882a593Smuzhiyun spin_unlock_irqrestore(&crc->lock, flags);
406*4882a593Smuzhiyun return -EINVAL;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun head = crc->head;
410*4882a593Smuzhiyun tail = crc->tail;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
413*4882a593Smuzhiyun bool was_overflow = crc->overflow;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun crc->overflow = true;
416*4882a593Smuzhiyun spin_unlock_irqrestore(&crc->lock, flags);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (!was_overflow)
419*4882a593Smuzhiyun DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun return -ENOBUFS;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun entry = &crc->entries[head];
425*4882a593Smuzhiyun entry->frame = frame;
426*4882a593Smuzhiyun entry->has_frame_counter = has_frame;
427*4882a593Smuzhiyun memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
430*4882a593Smuzhiyun crc->head = head;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun spin_unlock_irqrestore(&crc->lock, flags);
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun wake_up_interruptible(&crc->wq);
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun return 0;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);
439