blob: 72bfd8af3f7a2d0bb373da3ba3358d3eb1c7ba7d [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;
Mahesh Kumard5cc15a2018-07-13 19:29:33 +053090 size_t values_cnt;
91 int ret;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020092
93 if (len == 0)
94 return 0;
95
96 if (len > PAGE_SIZE - 1) {
97 DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
98 PAGE_SIZE);
99 return -E2BIG;
100 }
101
102 source = memdup_user_nul(ubuf, len);
103 if (IS_ERR(source))
104 return PTR_ERR(source);
105
106 if (source[len] == '\n')
107 source[len] = '\0';
108
Mahesh Kumard5cc15a2018-07-13 19:29:33 +0530109 if (crtc->funcs->verify_crc_source) {
110 ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
111 if (ret)
112 return ret;
113 }
114
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200115 spin_lock_irq(&crc->lock);
116
117 if (crc->opened) {
118 spin_unlock_irq(&crc->lock);
119 kfree(source);
120 return -EBUSY;
121 }
122
123 kfree(crc->source);
124 crc->source = source;
125
126 spin_unlock_irq(&crc->lock);
127
128 *offp += len;
129 return len;
130}
131
Jani Nikulae3f56b22016-10-18 14:28:35 +0300132static const struct file_operations drm_crtc_crc_control_fops = {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200133 .owner = THIS_MODULE,
134 .open = crc_control_open,
135 .read = seq_read,
136 .llseek = seq_lseek,
137 .release = single_release,
138 .write = crc_control_write
139};
140
Tomeu Vizosoe8fa5672017-01-02 13:59:10 +0100141static int crtc_crc_data_count(struct drm_crtc_crc *crc)
142{
143 assert_spin_locked(&crc->lock);
144 return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
145}
146
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200147static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
148{
149 kfree(crc->entries);
Maarten Lankhorsta0120242018-04-18 14:51:21 +0200150 crc->overflow = false;
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200151 crc->entries = NULL;
152 crc->head = 0;
153 crc->tail = 0;
154 crc->values_cnt = 0;
155 crc->opened = false;
156}
157
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200158static int crtc_crc_open(struct inode *inode, struct file *filep)
159{
160 struct drm_crtc *crtc = inode->i_private;
161 struct drm_crtc_crc *crc = &crtc->crc;
162 struct drm_crtc_crc_entry *entries = NULL;
163 size_t values_cnt;
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200164 int ret = 0;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200165
Maarten Lankhorst8038e092017-07-06 15:03:15 +0200166 if (drm_drv_uses_atomic_modeset(crtc->dev)) {
Maarten Lankhorst6f8bcc72017-09-12 15:37:44 +0200167 ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
Maarten Lankhorst8038e092017-07-06 15:03:15 +0200168 if (ret)
169 return ret;
170
171 if (!crtc->state->active)
172 ret = -EIO;
173 drm_modeset_unlock(&crtc->mutex);
174
175 if (ret)
176 return ret;
177 }
178
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200179 spin_lock_irq(&crc->lock);
180 if (!crc->opened)
181 crc->opened = true;
182 else
183 ret = -EBUSY;
184 spin_unlock_irq(&crc->lock);
185
186 if (ret)
187 return ret;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200188
189 ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
190 if (ret)
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200191 goto err;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200192
193 if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
194 ret = -EINVAL;
195 goto err_disable;
196 }
197
198 if (WARN_ON(values_cnt == 0)) {
199 ret = -EINVAL;
200 goto err_disable;
201 }
202
203 entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
204 if (!entries) {
205 ret = -ENOMEM;
206 goto err_disable;
207 }
208
209 spin_lock_irq(&crc->lock);
210 crc->entries = entries;
211 crc->values_cnt = values_cnt;
Tomeu Vizosoe8fa5672017-01-02 13:59:10 +0100212
213 /*
214 * Only return once we got a first frame, so userspace doesn't have to
215 * guess when this particular piece of HW will be ready to start
216 * generating CRCs.
217 */
Sean Paulc98cdff2017-04-07 16:15:30 -0400218 ret = wait_event_interruptible_lock_irq(crc->wq,
219 crtc_crc_data_count(crc),
220 crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200221 spin_unlock_irq(&crc->lock);
222
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200223 if (ret)
224 goto err_disable;
Sean Paulc98cdff2017-04-07 16:15:30 -0400225
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200226 return 0;
227
228err_disable:
229 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200230err:
231 spin_lock_irq(&crc->lock);
232 crtc_crc_cleanup(crc);
233 spin_unlock_irq(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200234 return ret;
235}
236
237static int crtc_crc_release(struct inode *inode, struct file *filep)
238{
239 struct drm_crtc *crtc = filep->f_inode->i_private;
240 struct drm_crtc_crc *crc = &crtc->crc;
241 size_t values_cnt;
242
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200243 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
244
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200245 spin_lock_irq(&crc->lock);
246 crtc_crc_cleanup(crc);
247 spin_unlock_irq(&crc->lock);
248
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200249 return 0;
250}
251
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200252/*
253 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
254 * separated, with a newline at the end and null-terminated.
255 */
256#define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
257#define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
258
259static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
260 size_t count, loff_t *pos)
261{
262 struct drm_crtc *crtc = filep->f_inode->i_private;
263 struct drm_crtc_crc *crc = &crtc->crc;
264 struct drm_crtc_crc_entry *entry;
265 char buf[MAX_LINE_LEN];
266 int ret, i;
267
268 spin_lock_irq(&crc->lock);
269
270 if (!crc->source) {
271 spin_unlock_irq(&crc->lock);
272 return 0;
273 }
274
275 /* Nothing to read? */
276 while (crtc_crc_data_count(crc) == 0) {
277 if (filep->f_flags & O_NONBLOCK) {
278 spin_unlock_irq(&crc->lock);
279 return -EAGAIN;
280 }
281
282 ret = wait_event_interruptible_lock_irq(crc->wq,
283 crtc_crc_data_count(crc),
284 crc->lock);
285 if (ret) {
286 spin_unlock_irq(&crc->lock);
287 return ret;
288 }
289 }
290
291 /* We know we have an entry to be read */
292 entry = &crc->entries[crc->tail];
293
294 if (count < LINE_LEN(crc->values_cnt)) {
295 spin_unlock_irq(&crc->lock);
296 return -EINVAL;
297 }
298
299 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
300 crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
301
302 spin_unlock_irq(&crc->lock);
303
304 if (entry->has_frame_counter)
305 sprintf(buf, "0x%08x", entry->frame);
306 else
307 sprintf(buf, "XXXXXXXXXX");
308
309 for (i = 0; i < crc->values_cnt; i++)
310 sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
311 sprintf(buf + 10 + crc->values_cnt * 11, "\n");
312
313 if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
314 return -EFAULT;
315
316 return LINE_LEN(crc->values_cnt);
317}
318
Maarten Lankhorst4beb3b42018-02-02 15:27:43 +0100319static unsigned int crtc_crc_poll(struct file *file, poll_table *wait)
320{
321 struct drm_crtc *crtc = file->f_inode->i_private;
322 struct drm_crtc_crc *crc = &crtc->crc;
323 unsigned ret;
324
325 poll_wait(file, &crc->wq, wait);
326
327 spin_lock_irq(&crc->lock);
328 if (crc->source && crtc_crc_data_count(crc))
329 ret = POLLIN | POLLRDNORM;
330 else
331 ret = 0;
332 spin_unlock_irq(&crc->lock);
333
334 return ret;
335}
336
Jani Nikulae3f56b22016-10-18 14:28:35 +0300337static const struct file_operations drm_crtc_crc_data_fops = {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200338 .owner = THIS_MODULE,
339 .open = crtc_crc_open,
340 .read = crtc_crc_read,
Maarten Lankhorst4beb3b42018-02-02 15:27:43 +0100341 .poll = crtc_crc_poll,
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200342 .release = crtc_crc_release,
343};
344
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200345int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
346{
347 struct dentry *crc_ent, *ent;
348
349 if (!crtc->funcs->set_crc_source)
350 return 0;
351
352 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
353 if (!crc_ent)
354 return -ENOMEM;
355
356 ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
357 &drm_crtc_crc_control_fops);
358 if (!ent)
359 goto error;
360
361 ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
362 &drm_crtc_crc_data_fops);
363 if (!ent)
364 goto error;
365
366 return 0;
367
368error:
369 debugfs_remove_recursive(crc_ent);
370
371 return -ENOMEM;
372}
373
374/**
375 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
376 * @crtc: CRTC to which the frame belongs
377 * @has_frame: whether this entry has a frame number to go with
378 * @frame: number of the frame these CRCs are about
379 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
380 *
381 * For each frame, the driver polls the source of CRCs for new data and calls
382 * this function to add them to the buffer from where userspace reads.
383 */
384int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
385 uint32_t frame, uint32_t *crcs)
386{
387 struct drm_crtc_crc *crc = &crtc->crc;
388 struct drm_crtc_crc_entry *entry;
389 int head, tail;
390
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100391 spin_lock(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200392
393 /* Caller may not have noticed yet that userspace has stopped reading */
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200394 if (!crc->entries) {
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100395 spin_unlock(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200396 return -EINVAL;
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100397 }
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200398
399 head = crc->head;
400 tail = crc->tail;
401
402 if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
Maarten Lankhorsta0120242018-04-18 14:51:21 +0200403 bool was_overflow = crc->overflow;
404
405 crc->overflow = true;
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100406 spin_unlock(&crc->lock);
Maarten Lankhorsta0120242018-04-18 14:51:21 +0200407
408 if (!was_overflow)
409 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
410
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200411 return -ENOBUFS;
412 }
413
414 entry = &crc->entries[head];
415 entry->frame = frame;
416 entry->has_frame_counter = has_frame;
417 memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
418
419 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
420 crc->head = head;
421
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100422 spin_unlock(&crc->lock);
423
Benjamin Gaignard9f547422017-01-06 10:15:03 +0100424 wake_up_interruptible(&crc->wq);
425
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200426 return 0;
427}
428EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);