1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
124static 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
131};
132
133static 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
139static int crtc_crc_open(struct inode *inode, struct file *filep)
140{
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;
146
147 if (crc->opened)
148 return -EBUSY;
149
150 ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
151 if (ret)
152 return ret;
153
154 if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
155 ret = -EINVAL;
156 goto err_disable;
157 }
158
159 if (WARN_ON(values_cnt == 0)) {
160 ret = -EINVAL;
161 goto err_disable;
162 }
163
164 entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
165 if (!entries) {
166 ret = -ENOMEM;
167 goto err_disable;
168 }
169
170 spin_lock_irq(&crc->lock);
171 crc->entries = entries;
172 crc->values_cnt = values_cnt;
173 crc->opened = true;
174
175
176
177
178
179
180 ret = wait_event_interruptible_lock_irq(crc->wq,
181 crtc_crc_data_count(crc),
182 crc->lock);
183 spin_unlock_irq(&crc->lock);
184
185 WARN_ON(ret);
186
187 return 0;
188
189err_disable:
190 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
191 return ret;
192}
193
194static int crtc_crc_release(struct inode *inode, struct file *filep)
195{
196 struct drm_crtc *crtc = filep->f_inode->i_private;
197 struct drm_crtc_crc *crc = &crtc->crc;
198 size_t values_cnt;
199
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);
208
209 crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
210
211 return 0;
212}
213
214
215
216
217
218#define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
219#define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
220
221static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
222 size_t count, loff_t *pos)
223{
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;
229
230 spin_lock_irq(&crc->lock);
231
232 if (!crc->source) {
233 spin_unlock_irq(&crc->lock);
234 return 0;
235 }
236
237
238 while (crtc_crc_data_count(crc) == 0) {
239 if (filep->f_flags & O_NONBLOCK) {
240 spin_unlock_irq(&crc->lock);
241 return -EAGAIN;
242 }
243
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;
250 }
251 }
252
253
254 entry = &crc->entries[crc->tail];
255
256 if (count < LINE_LEN(crc->values_cnt)) {
257 spin_unlock_irq(&crc->lock);
258 return -EINVAL;
259 }
260
261 BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
262 crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
263
264 spin_unlock_irq(&crc->lock);
265
266 if (entry->has_frame_counter)
267 sprintf(buf, "0x%08x", entry->frame);
268 else
269 sprintf(buf, "XXXXXXXXXX");
270
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");
274
275 if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
276 return -EFAULT;
277
278 return LINE_LEN(crc->values_cnt);
279}
280
281static 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,
286};
287
288int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
289{
290 struct dentry *crc_ent, *ent;
291
292 if (!crtc->funcs->set_crc_source)
293 return 0;
294
295 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
296 if (!crc_ent)
297 return -ENOMEM;
298
299 ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
300 &drm_crtc_crc_control_fops);
301 if (!ent)
302 goto error;
303
304 ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
305 &drm_crtc_crc_data_fops);
306 if (!ent)
307 goto error;
308
309 return 0;
310
311error:
312 debugfs_remove_recursive(crc_ent);
313
314 return -ENOMEM;
315}
316
317
318
319
320
321
322
323
324
325
326
327int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
328 uint32_t frame, uint32_t *crcs)
329{
330 struct drm_crtc_crc *crc = &crtc->crc;
331 struct drm_crtc_crc_entry *entry;
332 int head, tail;
333
334 spin_lock(&crc->lock);
335
336
337 if (!crc->opened) {
338 spin_unlock(&crc->lock);
339 return -EINVAL;
340 }
341
342 head = crc->head;
343 tail = crc->tail;
344
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;
349 }
350
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);
355
356 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
357 crc->head = head;
358
359 spin_unlock(&crc->lock);
360
361 wake_up_interruptible(&crc->wq);
362
363 return 0;
364}
365EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);
366