blob: d0ea4627a093677dbf1ea157db26a67aaf2822b2 [file] [log] [blame]
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +02001/*
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>
Jani Nikulae3f56b22016-10-18 14:28:35 +030033#include "drm_internal.h"
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020034
35/**
36 * DOC: CRC ABI
37 *
38 * DRM device drivers can provide to userspace CRC information of each frame as
Daniel Vetter760f71e2017-03-22 09:36:04 +010039 * it reached a given hardware component (a CRC sampling "source").
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020040 *
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.
Daniel Vetter760f71e2017-03-22 09:36:04 +010060 *
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().
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020065 */
66
67static int crc_control_show(struct seq_file *m, void *data)
68{
69 struct drm_crtc *crtc = m->private;
70
71 seq_printf(m, "%s\n", crtc->crc.source);
72
73 return 0;
74}
75
76static int crc_control_open(struct inode *inode, struct file *file)
77{
78 struct drm_crtc *crtc = inode->i_private;
79
80 return single_open(file, crc_control_show, crtc);
81}
82
83static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
84 size_t len, loff_t *offp)
85{
86 struct seq_file *m = file->private_data;
87 struct drm_crtc *crtc = m->private;
88 struct drm_crtc_crc *crc = &crtc->crc;
89 char *source;
90
91 if (len == 0)
92 return 0;
93
94 if (len > PAGE_SIZE - 1) {
95 DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
96 PAGE_SIZE);
97 return -E2BIG;
98 }
99
100 source = memdup_user_nul(ubuf, len);
101 if (IS_ERR(source))
102 return PTR_ERR(source);
103
104 if (source[len] == '\n')
105 source[len] = '\0';
106
107 spin_lock_irq(&crc->lock);
108
109 if (crc->opened) {
110 spin_unlock_irq(&crc->lock);
111 kfree(source);
112 return -EBUSY;
113 }
114
115 kfree(crc->source);
116 crc->source = source;
117
118 spin_unlock_irq(&crc->lock);
119
120 *offp += len;
121 return len;
122}
123
Jani Nikulae3f56b22016-10-18 14:28:35 +0300124static const struct file_operations drm_crtc_crc_control_fops = {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200125 .owner = THIS_MODULE,
126 .open = crc_control_open,
127 .read = seq_read,
128 .llseek = seq_lseek,
129 .release = single_release,
130 .write = crc_control_write
131};
132
Tomeu Vizosoe8fa5672017-01-02 13:59:10 +0100133static int crtc_crc_data_count(struct drm_crtc_crc *crc)
134{
135 assert_spin_locked(&crc->lock);
136 return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
137}
138
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200139static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
140{
141 kfree(crc->entries);
142 crc->entries = NULL;
143 crc->head = 0;
144 crc->tail = 0;
145 crc->values_cnt = 0;
146 crc->opened = false;
147}
148
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200149static int crtc_crc_open(struct inode *inode, struct file *filep)
150{
151 struct drm_crtc *crtc = inode->i_private;
152 struct drm_crtc_crc *crc = &crtc->crc;
153 struct drm_crtc_crc_entry *entries = NULL;
154 size_t values_cnt;
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200155 int ret = 0;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200156
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200157 spin_lock_irq(&crc->lock);
158 if (!crc->opened)
159 crc->opened = true;
160 else
161 ret = -EBUSY;
162 spin_unlock_irq(&crc->lock);
163
164 if (ret)
165 return ret;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200166
167 ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
168 if (ret)
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200169 goto err;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200170
171 if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
172 ret = -EINVAL;
173 goto err_disable;
174 }
175
176 if (WARN_ON(values_cnt == 0)) {
177 ret = -EINVAL;
178 goto err_disable;
179 }
180
181 entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
182 if (!entries) {
183 ret = -ENOMEM;
184 goto err_disable;
185 }
186
187 spin_lock_irq(&crc->lock);
188 crc->entries = entries;
189 crc->values_cnt = values_cnt;
Tomeu Vizosoe8fa5672017-01-02 13:59:10 +0100190
191 /*
192 * Only return once we got a first frame, so userspace doesn't have to
193 * guess when this particular piece of HW will be ready to start
194 * generating CRCs.
195 */
Sean Paulc98cdff2017-04-07 16:15:30 -0400196 ret = wait_event_interruptible_lock_irq(crc->wq,
197 crtc_crc_data_count(crc),
198 crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200199 spin_unlock_irq(&crc->lock);
200
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200201 if (ret)
202 goto err_disable;
Sean Paulc98cdff2017-04-07 16:15:30 -0400203
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200204 return 0;
205
206err_disable:
207 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200208err:
209 spin_lock_irq(&crc->lock);
210 crtc_crc_cleanup(crc);
211 spin_unlock_irq(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200212 return ret;
213}
214
215static int crtc_crc_release(struct inode *inode, struct file *filep)
216{
217 struct drm_crtc *crtc = filep->f_inode->i_private;
218 struct drm_crtc_crc *crc = &crtc->crc;
219 size_t values_cnt;
220
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200221 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
222
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200223 spin_lock_irq(&crc->lock);
224 crtc_crc_cleanup(crc);
225 spin_unlock_irq(&crc->lock);
226
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200227 return 0;
228}
229
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200230/*
231 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
232 * separated, with a newline at the end and null-terminated.
233 */
234#define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
235#define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
236
237static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
238 size_t count, loff_t *pos)
239{
240 struct drm_crtc *crtc = filep->f_inode->i_private;
241 struct drm_crtc_crc *crc = &crtc->crc;
242 struct drm_crtc_crc_entry *entry;
243 char buf[MAX_LINE_LEN];
244 int ret, i;
245
246 spin_lock_irq(&crc->lock);
247
248 if (!crc->source) {
249 spin_unlock_irq(&crc->lock);
250 return 0;
251 }
252
253 /* Nothing to read? */
254 while (crtc_crc_data_count(crc) == 0) {
255 if (filep->f_flags & O_NONBLOCK) {
256 spin_unlock_irq(&crc->lock);
257 return -EAGAIN;
258 }
259
260 ret = wait_event_interruptible_lock_irq(crc->wq,
261 crtc_crc_data_count(crc),
262 crc->lock);
263 if (ret) {
264 spin_unlock_irq(&crc->lock);
265 return ret;
266 }
267 }
268
269 /* We know we have an entry to be read */
270 entry = &crc->entries[crc->tail];
271
272 if (count < LINE_LEN(crc->values_cnt)) {
273 spin_unlock_irq(&crc->lock);
274 return -EINVAL;
275 }
276
277 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
278 crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
279
280 spin_unlock_irq(&crc->lock);
281
282 if (entry->has_frame_counter)
283 sprintf(buf, "0x%08x", entry->frame);
284 else
285 sprintf(buf, "XXXXXXXXXX");
286
287 for (i = 0; i < crc->values_cnt; i++)
288 sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
289 sprintf(buf + 10 + crc->values_cnt * 11, "\n");
290
291 if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
292 return -EFAULT;
293
294 return LINE_LEN(crc->values_cnt);
295}
296
Jani Nikulae3f56b22016-10-18 14:28:35 +0300297static const struct file_operations drm_crtc_crc_data_fops = {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200298 .owner = THIS_MODULE,
299 .open = crtc_crc_open,
300 .read = crtc_crc_read,
301 .release = crtc_crc_release,
302};
303
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200304int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
305{
306 struct dentry *crc_ent, *ent;
307
308 if (!crtc->funcs->set_crc_source)
309 return 0;
310
311 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
312 if (!crc_ent)
313 return -ENOMEM;
314
315 ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
316 &drm_crtc_crc_control_fops);
317 if (!ent)
318 goto error;
319
320 ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
321 &drm_crtc_crc_data_fops);
322 if (!ent)
323 goto error;
324
325 return 0;
326
327error:
328 debugfs_remove_recursive(crc_ent);
329
330 return -ENOMEM;
331}
332
333/**
334 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
335 * @crtc: CRTC to which the frame belongs
336 * @has_frame: whether this entry has a frame number to go with
337 * @frame: number of the frame these CRCs are about
338 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
339 *
340 * For each frame, the driver polls the source of CRCs for new data and calls
341 * this function to add them to the buffer from where userspace reads.
342 */
343int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
344 uint32_t frame, uint32_t *crcs)
345{
346 struct drm_crtc_crc *crc = &crtc->crc;
347 struct drm_crtc_crc_entry *entry;
348 int head, tail;
349
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100350 spin_lock(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200351
352 /* Caller may not have noticed yet that userspace has stopped reading */
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200353 if (!crc->entries) {
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100354 spin_unlock(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200355 return -EINVAL;
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100356 }
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200357
358 head = crc->head;
359 tail = crc->tail;
360
361 if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100362 spin_unlock(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200363 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
364 return -ENOBUFS;
365 }
366
367 entry = &crc->entries[head];
368 entry->frame = frame;
369 entry->has_frame_counter = has_frame;
370 memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
371
372 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
373 crc->head = head;
374
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100375 spin_unlock(&crc->lock);
376
Benjamin Gaignard9f547422017-01-06 10:15:03 +0100377 wake_up_interruptible(&crc->wq);
378
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200379 return 0;
380}
381EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);