rcutorture: Eliminate unused ts_rem local from rcu_trace_clock_local()
[linux/fpc-iii.git] / drivers / gpu / drm / drm_debugfs_crc.c
blob1722d8f214499794d5304dc7e9597bfcd0dadd6b
1 /*
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
14 * Software.
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.
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>
32 #include <drm/drmP.h>
33 #include "drm_internal.h"
35 /**
36 * DOC: CRC ABI
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
45 * for this CRTC.
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);
73 return 0;
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;
89 char *source;
91 if (len == 0)
92 return 0;
94 if (len > PAGE_SIZE - 1) {
95 DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
96 PAGE_SIZE);
97 return -E2BIG;
100 source = memdup_user_nul(ubuf, len);
101 if (IS_ERR(source))
102 return PTR_ERR(source);
104 if (source[len] == '\n')
105 source[len] = '\0';
107 spin_lock_irq(&crc->lock);
109 if (crc->opened) {
110 spin_unlock_irq(&crc->lock);
111 kfree(source);
112 return -EBUSY;
115 kfree(crc->source);
116 crc->source = source;
118 spin_unlock_irq(&crc->lock);
120 *offp += len;
121 return len;
124 static const struct file_operations drm_crtc_crc_control_fops = {
125 .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
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 int crtc_crc_open(struct inode *inode, struct file *filep)
141 struct drm_crtc *crtc = inode->i_private;
142 struct drm_crtc_crc *crc = &crtc->crc;
143 struct drm_crtc_crc_entry *entries = NULL;
144 size_t values_cnt;
145 int ret;
147 if (crc->opened)
148 return -EBUSY;
150 ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
151 if (ret)
152 return ret;
154 if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
155 ret = -EINVAL;
156 goto err_disable;
159 if (WARN_ON(values_cnt == 0)) {
160 ret = -EINVAL;
161 goto err_disable;
164 entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
165 if (!entries) {
166 ret = -ENOMEM;
167 goto err_disable;
170 spin_lock_irq(&crc->lock);
171 crc->entries = entries;
172 crc->values_cnt = values_cnt;
173 crc->opened = true;
176 * Only return once we got a first frame, so userspace doesn't have to
177 * guess when this particular piece of HW will be ready to start
178 * generating CRCs.
180 ret = wait_event_interruptible_lock_irq(crc->wq,
181 crtc_crc_data_count(crc),
182 crc->lock);
183 spin_unlock_irq(&crc->lock);
185 WARN_ON(ret);
187 return 0;
189 err_disable:
190 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
191 return ret;
194 static int crtc_crc_release(struct inode *inode, struct file *filep)
196 struct drm_crtc *crtc = filep->f_inode->i_private;
197 struct drm_crtc_crc *crc = &crtc->crc;
198 size_t values_cnt;
200 spin_lock_irq(&crc->lock);
201 kfree(crc->entries);
202 crc->entries = NULL;
203 crc->head = 0;
204 crc->tail = 0;
205 crc->values_cnt = 0;
206 crc->opened = false;
207 spin_unlock_irq(&crc->lock);
209 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
211 return 0;
215 * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
216 * separated, with a newline at the end and null-terminated.
218 #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
219 #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
221 static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
222 size_t count, loff_t *pos)
224 struct drm_crtc *crtc = filep->f_inode->i_private;
225 struct drm_crtc_crc *crc = &crtc->crc;
226 struct drm_crtc_crc_entry *entry;
227 char buf[MAX_LINE_LEN];
228 int ret, i;
230 spin_lock_irq(&crc->lock);
232 if (!crc->source) {
233 spin_unlock_irq(&crc->lock);
234 return 0;
237 /* Nothing to read? */
238 while (crtc_crc_data_count(crc) == 0) {
239 if (filep->f_flags & O_NONBLOCK) {
240 spin_unlock_irq(&crc->lock);
241 return -EAGAIN;
244 ret = wait_event_interruptible_lock_irq(crc->wq,
245 crtc_crc_data_count(crc),
246 crc->lock);
247 if (ret) {
248 spin_unlock_irq(&crc->lock);
249 return ret;
253 /* We know we have an entry to be read */
254 entry = &crc->entries[crc->tail];
256 if (count < LINE_LEN(crc->values_cnt)) {
257 spin_unlock_irq(&crc->lock);
258 return -EINVAL;
261 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
262 crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
264 spin_unlock_irq(&crc->lock);
266 if (entry->has_frame_counter)
267 sprintf(buf, "0x%08x", entry->frame);
268 else
269 sprintf(buf, "XXXXXXXXXX");
271 for (i = 0; i < crc->values_cnt; i++)
272 sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
273 sprintf(buf + 10 + crc->values_cnt * 11, "\n");
275 if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
276 return -EFAULT;
278 return LINE_LEN(crc->values_cnt);
281 static const struct file_operations drm_crtc_crc_data_fops = {
282 .owner = THIS_MODULE,
283 .open = crtc_crc_open,
284 .read = crtc_crc_read,
285 .release = crtc_crc_release,
288 int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
290 struct dentry *crc_ent, *ent;
292 if (!crtc->funcs->set_crc_source)
293 return 0;
295 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
296 if (!crc_ent)
297 return -ENOMEM;
299 ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
300 &drm_crtc_crc_control_fops);
301 if (!ent)
302 goto error;
304 ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
305 &drm_crtc_crc_data_fops);
306 if (!ent)
307 goto error;
309 return 0;
311 error:
312 debugfs_remove_recursive(crc_ent);
314 return -ENOMEM;
318 * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
319 * @crtc: CRTC to which the frame belongs
320 * @has_frame: whether this entry has a frame number to go with
321 * @frame: number of the frame these CRCs are about
322 * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
324 * For each frame, the driver polls the source of CRCs for new data and calls
325 * this function to add them to the buffer from where userspace reads.
327 int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
328 uint32_t frame, uint32_t *crcs)
330 struct drm_crtc_crc *crc = &crtc->crc;
331 struct drm_crtc_crc_entry *entry;
332 int head, tail;
334 spin_lock(&crc->lock);
336 /* Caller may not have noticed yet that userspace has stopped reading */
337 if (!crc->opened) {
338 spin_unlock(&crc->lock);
339 return -EINVAL;
342 head = crc->head;
343 tail = crc->tail;
345 if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
346 spin_unlock(&crc->lock);
347 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
348 return -ENOBUFS;
351 entry = &crc->entries[head];
352 entry->frame = frame;
353 entry->has_frame_counter = has_frame;
354 memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
356 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
357 crc->head = head;
359 spin_unlock(&crc->lock);
361 wake_up_interruptible(&crc->wq);
363 return 0;
365 EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);