blob: ca3c55c6b81550270c4953ecac2d3005f7741ad7 [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>
Sam Ravnborg0500c042019-05-26 19:35:35 +020032#include <linux/poll.h>
33#include <linux/uaccess.h>
34
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>
39
Jani Nikulae3f56b22016-10-18 14:28:35 +030040#include "drm_internal.h"
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020041
42/**
43 * DOC: CRC ABI
44 *
45 * DRM device drivers can provide to userspace CRC information of each frame as
Daniel Vetter760f71e2017-03-22 09:36:04 +010046 * it reached a given hardware component (a CRC sampling "source").
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020047 *
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
52 * for this CRTC.
53 *
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.
59 *
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.
Daniel Vetter760f71e2017-03-22 09:36:04 +010067 *
68 * On the driver side the implementation effort is minimal, drivers only need to
Liviu Dudau84a68102019-07-03 15:56:03 +010069 * implement &drm_crtc_funcs.set_crc_source and &drm_crtc_funcs.verify_crc_source.
70 * The debugfs files are automatically set up if those vfuncs are set. CRC samples
71 * need to be captured in the driver by calling drm_crtc_add_crc_entry().
Brian Starkey178e5f32019-08-06 13:46:22 +010072 * Depending on the driver and HW requirements, &drm_crtc_funcs.set_crc_source
73 * may result in a commit (even a full modeset).
74 *
75 * CRC results must be reliable across non-full-modeset atomic commits, so if a
76 * commit via DRM_IOCTL_MODE_ATOMIC would disable or otherwise interfere with
77 * CRC generation, then the driver must mark that commit as a full modeset
78 * (drm_atomic_crtc_needs_modeset() should return true). As a result, to ensure
79 * consistent results, generic userspace must re-setup CRC generation after a
80 * legacy SETCRTC or an atomic commit with DRM_MODE_ATOMIC_ALLOW_MODESET.
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020081 */
82
83static int crc_control_show(struct seq_file *m, void *data)
84{
85 struct drm_crtc *crtc = m->private;
86
Mahesh Kumar43965512018-07-13 19:29:34 +053087 if (crtc->funcs->get_crc_sources) {
88 size_t count;
89 const char *const *sources = crtc->funcs->get_crc_sources(crtc,
90 &count);
91 size_t values_cnt;
92 int i;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020093
Mahesh Kumar43965512018-07-13 19:29:34 +053094 if (count == 0 || !sources)
95 goto out;
96
97 for (i = 0; i < count; i++)
98 if (!crtc->funcs->verify_crc_source(crtc, sources[i],
99 &values_cnt)) {
100 if (strcmp(sources[i], crtc->crc.source))
101 seq_printf(m, "%s\n", sources[i]);
102 else
103 seq_printf(m, "%s*\n", sources[i]);
104 }
105 }
106 return 0;
107
108out:
109 seq_printf(m, "%s*\n", crtc->crc.source);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200110 return 0;
111}
112
113static int crc_control_open(struct inode *inode, struct file *file)
114{
115 struct drm_crtc *crtc = inode->i_private;
116
117 return single_open(file, crc_control_show, crtc);
118}
119
120static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
121 size_t len, loff_t *offp)
122{
123 struct seq_file *m = file->private_data;
124 struct drm_crtc *crtc = m->private;
125 struct drm_crtc_crc *crc = &crtc->crc;
126 char *source;
Mahesh Kumard5cc15a2018-07-13 19:29:33 +0530127 size_t values_cnt;
128 int ret;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200129
130 if (len == 0)
131 return 0;
132
133 if (len > PAGE_SIZE - 1) {
134 DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
135 PAGE_SIZE);
136 return -E2BIG;
137 }
138
139 source = memdup_user_nul(ubuf, len);
140 if (IS_ERR(source))
141 return PTR_ERR(source);
142
143 if (source[len] == '\n')
144 source[len] = '\0';
145
Mahesh Kumarc0811a72018-08-21 14:08:56 +0530146 ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
147 if (ret)
148 return ret;
Mahesh Kumard5cc15a2018-07-13 19:29:33 +0530149
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200150 spin_lock_irq(&crc->lock);
151
152 if (crc->opened) {
153 spin_unlock_irq(&crc->lock);
154 kfree(source);
155 return -EBUSY;
156 }
157
158 kfree(crc->source);
159 crc->source = source;
160
161 spin_unlock_irq(&crc->lock);
162
163 *offp += len;
164 return len;
165}
166
Jani Nikulae3f56b22016-10-18 14:28:35 +0300167static const struct file_operations drm_crtc_crc_control_fops = {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200168 .owner = THIS_MODULE,
169 .open = crc_control_open,
170 .read = seq_read,
171 .llseek = seq_lseek,
172 .release = single_release,
173 .write = crc_control_write
174};
175
Tomeu Vizosoe8fa5672017-01-02 13:59:10 +0100176static int crtc_crc_data_count(struct drm_crtc_crc *crc)
177{
178 assert_spin_locked(&crc->lock);
179 return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
180}
181
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200182static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
183{
184 kfree(crc->entries);
Maarten Lankhorsta0120242018-04-18 14:51:21 +0200185 crc->overflow = false;
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200186 crc->entries = NULL;
187 crc->head = 0;
188 crc->tail = 0;
189 crc->values_cnt = 0;
190 crc->opened = false;
191}
192
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200193static int crtc_crc_open(struct inode *inode, struct file *filep)
194{
195 struct drm_crtc *crtc = inode->i_private;
196 struct drm_crtc_crc *crc = &crtc->crc;
197 struct drm_crtc_crc_entry *entries = NULL;
198 size_t values_cnt;
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200199 int ret = 0;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200200
Maarten Lankhorst8038e092017-07-06 15:03:15 +0200201 if (drm_drv_uses_atomic_modeset(crtc->dev)) {
Maarten Lankhorst6f8bcc72017-09-12 15:37:44 +0200202 ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
Maarten Lankhorst8038e092017-07-06 15:03:15 +0200203 if (ret)
204 return ret;
205
206 if (!crtc->state->active)
207 ret = -EIO;
208 drm_modeset_unlock(&crtc->mutex);
209
210 if (ret)
211 return ret;
212 }
213
Mahesh Kumarc0811a72018-08-21 14:08:56 +0530214 ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt);
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200215 if (ret)
216 return ret;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200217
Mahesh Kumarc0811a72018-08-21 14:08:56 +0530218 if (WARN_ON(values_cnt > DRM_MAX_CRC_NR))
219 return -EINVAL;
220
221 if (WARN_ON(values_cnt == 0))
222 return -EINVAL;
223
224 entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
225 if (!entries)
226 return -ENOMEM;
227
228 spin_lock_irq(&crc->lock);
229 if (!crc->opened) {
230 crc->opened = true;
231 crc->entries = entries;
232 crc->values_cnt = values_cnt;
233 } else {
234 ret = -EBUSY;
235 }
236 spin_unlock_irq(&crc->lock);
237
238 if (ret) {
239 kfree(entries);
240 return ret;
241 }
242
243 ret = crtc->funcs->set_crc_source(crtc, crc->source);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200244 if (ret)
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200245 goto err;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200246
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200247 return 0;
248
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200249err:
250 spin_lock_irq(&crc->lock);
251 crtc_crc_cleanup(crc);
252 spin_unlock_irq(&crc->lock);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200253 return ret;
254}
255
256static int crtc_crc_release(struct inode *inode, struct file *filep)
257{
258 struct drm_crtc *crtc = filep->f_inode->i_private;
259 struct drm_crtc_crc *crc = &crtc->crc;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200260
Mahesh Kumarc0811a72018-08-21 14:08:56 +0530261 crtc->funcs->set_crc_source(crtc, NULL);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200262
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200263 spin_lock_irq(&crc->lock);
264 crtc_crc_cleanup(crc);
265 spin_unlock_irq(&crc->lock);
266
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200267 return 0;
268}
269
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200270/*
271 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
272 * separated, with a newline at the end and null-terminated.
273 */
274#define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
275#define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
276
277static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
278 size_t count, loff_t *pos)
279{
280 struct drm_crtc *crtc = filep->f_inode->i_private;
281 struct drm_crtc_crc *crc = &crtc->crc;
282 struct drm_crtc_crc_entry *entry;
283 char buf[MAX_LINE_LEN];
284 int ret, i;
285
286 spin_lock_irq(&crc->lock);
287
288 if (!crc->source) {
289 spin_unlock_irq(&crc->lock);
290 return 0;
291 }
292
293 /* Nothing to read? */
294 while (crtc_crc_data_count(crc) == 0) {
295 if (filep->f_flags & O_NONBLOCK) {
296 spin_unlock_irq(&crc->lock);
297 return -EAGAIN;
298 }
299
300 ret = wait_event_interruptible_lock_irq(crc->wq,
301 crtc_crc_data_count(crc),
302 crc->lock);
303 if (ret) {
304 spin_unlock_irq(&crc->lock);
305 return ret;
306 }
307 }
308
309 /* We know we have an entry to be read */
310 entry = &crc->entries[crc->tail];
311
312 if (count < LINE_LEN(crc->values_cnt)) {
313 spin_unlock_irq(&crc->lock);
314 return -EINVAL;
315 }
316
317 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
318 crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
319
320 spin_unlock_irq(&crc->lock);
321
322 if (entry->has_frame_counter)
323 sprintf(buf, "0x%08x", entry->frame);
324 else
325 sprintf(buf, "XXXXXXXXXX");
326
327 for (i = 0; i < crc->values_cnt; i++)
328 sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
329 sprintf(buf + 10 + crc->values_cnt * 11, "\n");
330
331 if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
332 return -EFAULT;
333
334 return LINE_LEN(crc->values_cnt);
335}
336
Ville Syrjälä1ab2a992019-07-10 15:51:43 +0300337static __poll_t crtc_crc_poll(struct file *file, poll_table *wait)
Maarten Lankhorst4beb3b42018-02-02 15:27:43 +0100338{
339 struct drm_crtc *crtc = file->f_inode->i_private;
340 struct drm_crtc_crc *crc = &crtc->crc;
Ville Syrjälä1ab2a992019-07-10 15:51:43 +0300341 __poll_t ret = 0;
Maarten Lankhorst4beb3b42018-02-02 15:27:43 +0100342
343 poll_wait(file, &crc->wq, wait);
344
345 spin_lock_irq(&crc->lock);
346 if (crc->source && crtc_crc_data_count(crc))
Ville Syrjälä1ab2a992019-07-10 15:51:43 +0300347 ret |= EPOLLIN | EPOLLRDNORM;
Maarten Lankhorst4beb3b42018-02-02 15:27:43 +0100348 spin_unlock_irq(&crc->lock);
349
350 return ret;
351}
352
Jani Nikulae3f56b22016-10-18 14:28:35 +0300353static const struct file_operations drm_crtc_crc_data_fops = {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200354 .owner = THIS_MODULE,
355 .open = crtc_crc_open,
356 .read = crtc_crc_read,
Maarten Lankhorst4beb3b42018-02-02 15:27:43 +0100357 .poll = crtc_crc_poll,
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200358 .release = crtc_crc_release,
359};
360
Greg Kroah-Hartmanb792e642019-06-13 15:34:39 +0200361void drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200362{
Greg Kroah-Hartmanb792e642019-06-13 15:34:39 +0200363 struct dentry *crc_ent;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200364
Mahesh Kumarc0811a72018-08-21 14:08:56 +0530365 if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source)
Greg Kroah-Hartmanb792e642019-06-13 15:34:39 +0200366 return;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200367
368 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200369
Greg Kroah-Hartmanb792e642019-06-13 15:34:39 +0200370 debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
371 &drm_crtc_crc_control_fops);
372 debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
373 &drm_crtc_crc_data_fops);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200374}
375
376/**
377 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
378 * @crtc: CRTC to which the frame belongs
379 * @has_frame: whether this entry has a frame number to go with
380 * @frame: number of the frame these CRCs are about
381 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
382 *
383 * For each frame, the driver polls the source of CRCs for new data and calls
384 * this function to add them to the buffer from where userspace reads.
385 */
386int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
387 uint32_t frame, uint32_t *crcs)
388{
389 struct drm_crtc_crc *crc = &crtc->crc;
390 struct drm_crtc_crc_entry *entry;
391 int head, tail;
Daniel Vetter18820182019-06-05 21:45:56 +0200392 unsigned long flags;
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200393
Daniel Vetter18820182019-06-05 21:45:56 +0200394 spin_lock_irqsave(&crc->lock, flags);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200395
396 /* Caller may not have noticed yet that userspace has stopped reading */
Maarten Lankhorsteb42ea62017-06-21 13:00:07 +0200397 if (!crc->entries) {
Daniel Vetterd99004d2019-06-06 23:15:44 +0200398 spin_unlock_irqrestore(&crc->lock, flags);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200399 return -EINVAL;
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100400 }
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200401
402 head = crc->head;
403 tail = crc->tail;
404
405 if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
Maarten Lankhorsta0120242018-04-18 14:51:21 +0200406 bool was_overflow = crc->overflow;
407
408 crc->overflow = true;
Daniel Vetterd99004d2019-06-06 23:15:44 +0200409 spin_unlock_irqrestore(&crc->lock, flags);
Maarten Lankhorsta0120242018-04-18 14:51:21 +0200410
411 if (!was_overflow)
412 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
413
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200414 return -ENOBUFS;
415 }
416
417 entry = &crc->entries[head];
418 entry->frame = frame;
419 entry->has_frame_counter = has_frame;
420 memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
421
422 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
423 crc->head = head;
424
Daniel Vetter18820182019-06-05 21:45:56 +0200425 spin_unlock_irqrestore(&crc->lock, flags);
Tomeu Vizoso1aa81be2017-01-02 13:59:09 +0100426
Benjamin Gaignard9f547422017-01-06 10:15:03 +0100427 wake_up_interruptible(&crc->wq);
428
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200429 return 0;
430}
431EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);