This source file includes following definitions.
- crc_control_show
- crc_control_open
- crc_control_write
- crtc_crc_data_count
- crtc_crc_cleanup
- crtc_crc_open
- crtc_crc_release
- crtc_crc_read
- crtc_crc_poll
- drm_debugfs_crtc_crc_add
- drm_crtc_add_crc_entry
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 <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
40 #include "drm_internal.h"
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 static int crc_control_show(struct seq_file *m, void *data)
84 {
85 struct drm_crtc *crtc = m->private;
86
87 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;
93
94 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
108 out:
109 seq_printf(m, "%s*\n", crtc->crc.source);
110 return 0;
111 }
112
113 static 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
120 static 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;
127 size_t values_cnt;
128 int ret;
129
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 - 1] == '\n')
144 source[len - 1] = '\0';
145
146 ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
147 if (ret)
148 return ret;
149
150 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
167 static const struct file_operations drm_crtc_crc_control_fops = {
168 .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
176 static 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
182 static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
183 {
184 kfree(crc->entries);
185 crc->overflow = false;
186 crc->entries = NULL;
187 crc->head = 0;
188 crc->tail = 0;
189 crc->values_cnt = 0;
190 crc->opened = false;
191 }
192
193 static 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;
199 int ret = 0;
200
201 if (drm_drv_uses_atomic_modeset(crtc->dev)) {
202 ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
203 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
214 ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt);
215 if (ret)
216 return ret;
217
218 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);
244 if (ret)
245 goto err;
246
247 return 0;
248
249 err:
250 spin_lock_irq(&crc->lock);
251 crtc_crc_cleanup(crc);
252 spin_unlock_irq(&crc->lock);
253 return ret;
254 }
255
256 static 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;
260
261 crtc->funcs->set_crc_source(crtc, NULL);
262
263 spin_lock_irq(&crc->lock);
264 crtc_crc_cleanup(crc);
265 spin_unlock_irq(&crc->lock);
266
267 return 0;
268 }
269
270
271
272
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
277 static 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
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
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
337 static unsigned int crtc_crc_poll(struct file *file, poll_table *wait)
338 {
339 struct drm_crtc *crtc = file->f_inode->i_private;
340 struct drm_crtc_crc *crc = &crtc->crc;
341 unsigned ret;
342
343 poll_wait(file, &crc->wq, wait);
344
345 spin_lock_irq(&crc->lock);
346 if (crc->source && crtc_crc_data_count(crc))
347 ret = POLLIN | POLLRDNORM;
348 else
349 ret = 0;
350 spin_unlock_irq(&crc->lock);
351
352 return ret;
353 }
354
355 static const struct file_operations drm_crtc_crc_data_fops = {
356 .owner = THIS_MODULE,
357 .open = crtc_crc_open,
358 .read = crtc_crc_read,
359 .poll = crtc_crc_poll,
360 .release = crtc_crc_release,
361 };
362
363 void drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
364 {
365 struct dentry *crc_ent;
366
367 if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source)
368 return;
369
370 crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
371
372 debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
373 &drm_crtc_crc_control_fops);
374 debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
375 &drm_crtc_crc_data_fops);
376 }
377
378
379
380
381
382
383
384
385
386
387
388 int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
389 uint32_t frame, uint32_t *crcs)
390 {
391 struct drm_crtc_crc *crc = &crtc->crc;
392 struct drm_crtc_crc_entry *entry;
393 int head, tail;
394 unsigned long flags;
395
396 spin_lock_irqsave(&crc->lock, flags);
397
398
399 if (!crc->entries) {
400 spin_unlock_irqrestore(&crc->lock, flags);
401 return -EINVAL;
402 }
403
404 head = crc->head;
405 tail = crc->tail;
406
407 if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
408 bool was_overflow = crc->overflow;
409
410 crc->overflow = true;
411 spin_unlock_irqrestore(&crc->lock, flags);
412
413 if (!was_overflow)
414 DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
415
416 return -ENOBUFS;
417 }
418
419 entry = &crc->entries[head];
420 entry->frame = frame;
421 entry->has_frame_counter = has_frame;
422 memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
423
424 head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
425 crc->head = head;
426
427 spin_unlock_irqrestore(&crc->lock, flags);
428
429 wake_up_interruptible(&crc->wq);
430
431 return 0;
432 }
433 EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);