blob: ef0c0e195164d7f4f756029097498775767af75f [file] [log] [blame]
Tomeu Vizoso731035f2016-12-12 13:29:48 +01001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Author: Damien Lespiau <damien.lespiau@intel.com>
24 *
25 */
26
27#include <linux/seq_file.h>
28#include <linux/circ_buf.h>
29#include <linux/ctype.h>
30#include <linux/debugfs.h>
31#include "intel_drv.h"
32
33struct pipe_crc_info {
34 const char *name;
35 struct drm_i915_private *dev_priv;
36 enum pipe pipe;
37};
38
39/* As the drm_debugfs_init() routines are called before dev->dev_private is
40 * allocated we need to hook into the minor for release.
41 */
42static int drm_add_fake_info_node(struct drm_minor *minor,
43 struct dentry *ent, const void *key)
44{
45 struct drm_info_node *node;
46
47 node = kmalloc(sizeof(*node), GFP_KERNEL);
48 if (node == NULL) {
49 debugfs_remove(ent);
50 return -ENOMEM;
51 }
52
53 node->minor = minor;
54 node->dent = ent;
55 node->info_ent = (void *) key;
56
57 mutex_lock(&minor->debugfs_lock);
58 list_add(&node->list, &minor->debugfs_list);
59 mutex_unlock(&minor->debugfs_lock);
60
61 return 0;
62}
63
64static int i915_pipe_crc_open(struct inode *inode, struct file *filep)
65{
66 struct pipe_crc_info *info = inode->i_private;
67 struct drm_i915_private *dev_priv = info->dev_priv;
68 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[info->pipe];
69
70 if (info->pipe >= INTEL_INFO(dev_priv)->num_pipes)
71 return -ENODEV;
72
73 spin_lock_irq(&pipe_crc->lock);
74
75 if (pipe_crc->opened) {
76 spin_unlock_irq(&pipe_crc->lock);
77 return -EBUSY; /* already open */
78 }
79
80 pipe_crc->opened = true;
81 filep->private_data = inode->i_private;
82
83 spin_unlock_irq(&pipe_crc->lock);
84
85 return 0;
86}
87
88static int i915_pipe_crc_release(struct inode *inode, struct file *filep)
89{
90 struct pipe_crc_info *info = inode->i_private;
91 struct drm_i915_private *dev_priv = info->dev_priv;
92 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[info->pipe];
93
94 spin_lock_irq(&pipe_crc->lock);
95 pipe_crc->opened = false;
96 spin_unlock_irq(&pipe_crc->lock);
97
98 return 0;
99}
100
101/* (6 fields, 8 chars each, space separated (5) + '\n') */
102#define PIPE_CRC_LINE_LEN (6 * 8 + 5 + 1)
103/* account for \'0' */
104#define PIPE_CRC_BUFFER_LEN (PIPE_CRC_LINE_LEN + 1)
105
106static int pipe_crc_data_count(struct intel_pipe_crc *pipe_crc)
107{
108 assert_spin_locked(&pipe_crc->lock);
109 return CIRC_CNT(pipe_crc->head, pipe_crc->tail,
110 INTEL_PIPE_CRC_ENTRIES_NR);
111}
112
113static ssize_t
114i915_pipe_crc_read(struct file *filep, char __user *user_buf, size_t count,
115 loff_t *pos)
116{
117 struct pipe_crc_info *info = filep->private_data;
118 struct drm_i915_private *dev_priv = info->dev_priv;
119 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[info->pipe];
120 char buf[PIPE_CRC_BUFFER_LEN];
121 int n_entries;
122 ssize_t bytes_read;
123
124 /*
125 * Don't allow user space to provide buffers not big enough to hold
126 * a line of data.
127 */
128 if (count < PIPE_CRC_LINE_LEN)
129 return -EINVAL;
130
131 if (pipe_crc->source == INTEL_PIPE_CRC_SOURCE_NONE)
132 return 0;
133
134 /* nothing to read */
135 spin_lock_irq(&pipe_crc->lock);
136 while (pipe_crc_data_count(pipe_crc) == 0) {
137 int ret;
138
139 if (filep->f_flags & O_NONBLOCK) {
140 spin_unlock_irq(&pipe_crc->lock);
141 return -EAGAIN;
142 }
143
144 ret = wait_event_interruptible_lock_irq(pipe_crc->wq,
145 pipe_crc_data_count(pipe_crc), pipe_crc->lock);
146 if (ret) {
147 spin_unlock_irq(&pipe_crc->lock);
148 return ret;
149 }
150 }
151
152 /* We now have one or more entries to read */
153 n_entries = count / PIPE_CRC_LINE_LEN;
154
155 bytes_read = 0;
156 while (n_entries > 0) {
157 struct intel_pipe_crc_entry *entry =
158 &pipe_crc->entries[pipe_crc->tail];
159
160 if (CIRC_CNT(pipe_crc->head, pipe_crc->tail,
161 INTEL_PIPE_CRC_ENTRIES_NR) < 1)
162 break;
163
164 BUILD_BUG_ON_NOT_POWER_OF_2(INTEL_PIPE_CRC_ENTRIES_NR);
165 pipe_crc->tail = (pipe_crc->tail + 1) &
166 (INTEL_PIPE_CRC_ENTRIES_NR - 1);
167
168 bytes_read += snprintf(buf, PIPE_CRC_BUFFER_LEN,
169 "%8u %8x %8x %8x %8x %8x\n",
170 entry->frame, entry->crc[0],
171 entry->crc[1], entry->crc[2],
172 entry->crc[3], entry->crc[4]);
173
174 spin_unlock_irq(&pipe_crc->lock);
175
176 if (copy_to_user(user_buf, buf, PIPE_CRC_LINE_LEN))
177 return -EFAULT;
178
179 user_buf += PIPE_CRC_LINE_LEN;
180 n_entries--;
181
182 spin_lock_irq(&pipe_crc->lock);
183 }
184
185 spin_unlock_irq(&pipe_crc->lock);
186
187 return bytes_read;
188}
189
190static const struct file_operations i915_pipe_crc_fops = {
191 .owner = THIS_MODULE,
192 .open = i915_pipe_crc_open,
193 .read = i915_pipe_crc_read,
194 .release = i915_pipe_crc_release,
195};
196
197static struct pipe_crc_info i915_pipe_crc_data[I915_MAX_PIPES] = {
198 {
199 .name = "i915_pipe_A_crc",
200 .pipe = PIPE_A,
201 },
202 {
203 .name = "i915_pipe_B_crc",
204 .pipe = PIPE_B,
205 },
206 {
207 .name = "i915_pipe_C_crc",
208 .pipe = PIPE_C,
209 },
210};
211
212static int i915_pipe_crc_create(struct dentry *root, struct drm_minor *minor,
213 enum pipe pipe)
214{
215 struct drm_i915_private *dev_priv = to_i915(minor->dev);
216 struct dentry *ent;
217 struct pipe_crc_info *info = &i915_pipe_crc_data[pipe];
218
219 info->dev_priv = dev_priv;
220 ent = debugfs_create_file(info->name, S_IRUGO, root, info,
221 &i915_pipe_crc_fops);
222 if (!ent)
223 return -ENOMEM;
224
225 return drm_add_fake_info_node(minor, ent, info);
226}
227
228static const char * const pipe_crc_sources[] = {
229 "none",
230 "plane1",
231 "plane2",
232 "pf",
233 "pipe",
234 "TV",
235 "DP-B",
236 "DP-C",
237 "DP-D",
238 "auto",
239};
240
241static const char *pipe_crc_source_name(enum intel_pipe_crc_source source)
242{
243 BUILD_BUG_ON(ARRAY_SIZE(pipe_crc_sources) != INTEL_PIPE_CRC_SOURCE_MAX);
244 return pipe_crc_sources[source];
245}
246
247static int display_crc_ctl_show(struct seq_file *m, void *data)
248{
249 struct drm_i915_private *dev_priv = m->private;
250 int i;
251
252 for (i = 0; i < I915_MAX_PIPES; i++)
253 seq_printf(m, "%c %s\n", pipe_name(i),
254 pipe_crc_source_name(dev_priv->pipe_crc[i].source));
255
256 return 0;
257}
258
259static int display_crc_ctl_open(struct inode *inode, struct file *file)
260{
261 return single_open(file, display_crc_ctl_show, inode->i_private);
262}
263
264static int i8xx_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
265 uint32_t *val)
266{
267 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
268 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
269
270 switch (*source) {
271 case INTEL_PIPE_CRC_SOURCE_PIPE:
272 *val = PIPE_CRC_ENABLE | PIPE_CRC_INCLUDE_BORDER_I8XX;
273 break;
274 case INTEL_PIPE_CRC_SOURCE_NONE:
275 *val = 0;
276 break;
277 default:
278 return -EINVAL;
279 }
280
281 return 0;
282}
283
284static int i9xx_pipe_crc_auto_source(struct drm_i915_private *dev_priv,
285 enum pipe pipe,
286 enum intel_pipe_crc_source *source)
287{
288 struct drm_device *dev = &dev_priv->drm;
289 struct intel_encoder *encoder;
290 struct intel_crtc *crtc;
291 struct intel_digital_port *dig_port;
292 int ret = 0;
293
294 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
295
296 drm_modeset_lock_all(dev);
297 for_each_intel_encoder(dev, encoder) {
298 if (!encoder->base.crtc)
299 continue;
300
301 crtc = to_intel_crtc(encoder->base.crtc);
302
303 if (crtc->pipe != pipe)
304 continue;
305
306 switch (encoder->type) {
307 case INTEL_OUTPUT_TVOUT:
308 *source = INTEL_PIPE_CRC_SOURCE_TV;
309 break;
310 case INTEL_OUTPUT_DP:
311 case INTEL_OUTPUT_EDP:
312 dig_port = enc_to_dig_port(&encoder->base);
313 switch (dig_port->port) {
314 case PORT_B:
315 *source = INTEL_PIPE_CRC_SOURCE_DP_B;
316 break;
317 case PORT_C:
318 *source = INTEL_PIPE_CRC_SOURCE_DP_C;
319 break;
320 case PORT_D:
321 *source = INTEL_PIPE_CRC_SOURCE_DP_D;
322 break;
323 default:
324 WARN(1, "nonexisting DP port %c\n",
325 port_name(dig_port->port));
326 break;
327 }
328 break;
329 default:
330 break;
331 }
332 }
333 drm_modeset_unlock_all(dev);
334
335 return ret;
336}
337
338static int vlv_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
339 enum pipe pipe,
340 enum intel_pipe_crc_source *source,
341 uint32_t *val)
342{
343 bool need_stable_symbols = false;
344
345 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
346 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
347 if (ret)
348 return ret;
349 }
350
351 switch (*source) {
352 case INTEL_PIPE_CRC_SOURCE_PIPE:
353 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_VLV;
354 break;
355 case INTEL_PIPE_CRC_SOURCE_DP_B:
356 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_B_VLV;
357 need_stable_symbols = true;
358 break;
359 case INTEL_PIPE_CRC_SOURCE_DP_C:
360 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_C_VLV;
361 need_stable_symbols = true;
362 break;
363 case INTEL_PIPE_CRC_SOURCE_DP_D:
364 if (!IS_CHERRYVIEW(dev_priv))
365 return -EINVAL;
366 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_D_VLV;
367 need_stable_symbols = true;
368 break;
369 case INTEL_PIPE_CRC_SOURCE_NONE:
370 *val = 0;
371 break;
372 default:
373 return -EINVAL;
374 }
375
376 /*
377 * When the pipe CRC tap point is after the transcoders we need
378 * to tweak symbol-level features to produce a deterministic series of
379 * symbols for a given frame. We need to reset those features only once
380 * a frame (instead of every nth symbol):
381 * - DC-balance: used to ensure a better clock recovery from the data
382 * link (SDVO)
383 * - DisplayPort scrambling: used for EMI reduction
384 */
385 if (need_stable_symbols) {
386 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
387
388 tmp |= DC_BALANCE_RESET_VLV;
389 switch (pipe) {
390 case PIPE_A:
391 tmp |= PIPE_A_SCRAMBLE_RESET;
392 break;
393 case PIPE_B:
394 tmp |= PIPE_B_SCRAMBLE_RESET;
395 break;
396 case PIPE_C:
397 tmp |= PIPE_C_SCRAMBLE_RESET;
398 break;
399 default:
400 return -EINVAL;
401 }
402 I915_WRITE(PORT_DFT2_G4X, tmp);
403 }
404
405 return 0;
406}
407
408static int i9xx_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
409 enum pipe pipe,
410 enum intel_pipe_crc_source *source,
411 uint32_t *val)
412{
413 bool need_stable_symbols = false;
414
415 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
416 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
417 if (ret)
418 return ret;
419 }
420
421 switch (*source) {
422 case INTEL_PIPE_CRC_SOURCE_PIPE:
423 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_I9XX;
424 break;
425 case INTEL_PIPE_CRC_SOURCE_TV:
426 if (!SUPPORTS_TV(dev_priv))
427 return -EINVAL;
428 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_TV_PRE;
429 break;
430 case INTEL_PIPE_CRC_SOURCE_DP_B:
431 if (!IS_G4X(dev_priv))
432 return -EINVAL;
433 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_B_G4X;
434 need_stable_symbols = true;
435 break;
436 case INTEL_PIPE_CRC_SOURCE_DP_C:
437 if (!IS_G4X(dev_priv))
438 return -EINVAL;
439 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_C_G4X;
440 need_stable_symbols = true;
441 break;
442 case INTEL_PIPE_CRC_SOURCE_DP_D:
443 if (!IS_G4X(dev_priv))
444 return -EINVAL;
445 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_D_G4X;
446 need_stable_symbols = true;
447 break;
448 case INTEL_PIPE_CRC_SOURCE_NONE:
449 *val = 0;
450 break;
451 default:
452 return -EINVAL;
453 }
454
455 /*
456 * When the pipe CRC tap point is after the transcoders we need
457 * to tweak symbol-level features to produce a deterministic series of
458 * symbols for a given frame. We need to reset those features only once
459 * a frame (instead of every nth symbol):
460 * - DC-balance: used to ensure a better clock recovery from the data
461 * link (SDVO)
462 * - DisplayPort scrambling: used for EMI reduction
463 */
464 if (need_stable_symbols) {
465 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
466
467 WARN_ON(!IS_G4X(dev_priv));
468
469 I915_WRITE(PORT_DFT_I9XX,
470 I915_READ(PORT_DFT_I9XX) | DC_BALANCE_RESET);
471
472 if (pipe == PIPE_A)
473 tmp |= PIPE_A_SCRAMBLE_RESET;
474 else
475 tmp |= PIPE_B_SCRAMBLE_RESET;
476
477 I915_WRITE(PORT_DFT2_G4X, tmp);
478 }
479
480 return 0;
481}
482
483static void vlv_undo_pipe_scramble_reset(struct drm_i915_private *dev_priv,
484 enum pipe pipe)
485{
486 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
487
488 switch (pipe) {
489 case PIPE_A:
490 tmp &= ~PIPE_A_SCRAMBLE_RESET;
491 break;
492 case PIPE_B:
493 tmp &= ~PIPE_B_SCRAMBLE_RESET;
494 break;
495 case PIPE_C:
496 tmp &= ~PIPE_C_SCRAMBLE_RESET;
497 break;
498 default:
499 return;
500 }
501 if (!(tmp & PIPE_SCRAMBLE_RESET_MASK))
502 tmp &= ~DC_BALANCE_RESET_VLV;
503 I915_WRITE(PORT_DFT2_G4X, tmp);
504
505}
506
507static void g4x_undo_pipe_scramble_reset(struct drm_i915_private *dev_priv,
508 enum pipe pipe)
509{
510 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
511
512 if (pipe == PIPE_A)
513 tmp &= ~PIPE_A_SCRAMBLE_RESET;
514 else
515 tmp &= ~PIPE_B_SCRAMBLE_RESET;
516 I915_WRITE(PORT_DFT2_G4X, tmp);
517
518 if (!(tmp & PIPE_SCRAMBLE_RESET_MASK)) {
519 I915_WRITE(PORT_DFT_I9XX,
520 I915_READ(PORT_DFT_I9XX) & ~DC_BALANCE_RESET);
521 }
522}
523
524static int ilk_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
525 uint32_t *val)
526{
527 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
528 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
529
530 switch (*source) {
531 case INTEL_PIPE_CRC_SOURCE_PLANE1:
532 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_ILK;
533 break;
534 case INTEL_PIPE_CRC_SOURCE_PLANE2:
535 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_ILK;
536 break;
537 case INTEL_PIPE_CRC_SOURCE_PIPE:
538 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_ILK;
539 break;
540 case INTEL_PIPE_CRC_SOURCE_NONE:
541 *val = 0;
542 break;
543 default:
544 return -EINVAL;
545 }
546
547 return 0;
548}
549
550static void hsw_trans_edp_pipe_A_crc_wa(struct drm_i915_private *dev_priv,
551 bool enable)
552{
553 struct drm_device *dev = &dev_priv->drm;
554 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
555 struct intel_crtc_state *pipe_config;
556 struct drm_atomic_state *state;
557 int ret = 0;
558
559 drm_modeset_lock_all(dev);
560 state = drm_atomic_state_alloc(dev);
561 if (!state) {
562 ret = -ENOMEM;
563 goto out;
564 }
565
566 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(&crtc->base);
567 pipe_config = intel_atomic_get_crtc_state(state, crtc);
568 if (IS_ERR(pipe_config)) {
569 ret = PTR_ERR(pipe_config);
570 goto out;
571 }
572
573 pipe_config->pch_pfit.force_thru = enable;
574 if (pipe_config->cpu_transcoder == TRANSCODER_EDP &&
575 pipe_config->pch_pfit.enabled != enable)
576 pipe_config->base.connectors_changed = true;
577
578 ret = drm_atomic_commit(state);
579out:
580 WARN(ret, "Toggling workaround to %i returns %i\n", enable, ret);
581 drm_modeset_unlock_all(dev);
582 drm_atomic_state_put(state);
583}
584
585static int ivb_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
586 enum pipe pipe,
587 enum intel_pipe_crc_source *source,
588 uint32_t *val)
589{
590 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
591 *source = INTEL_PIPE_CRC_SOURCE_PF;
592
593 switch (*source) {
594 case INTEL_PIPE_CRC_SOURCE_PLANE1:
595 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_IVB;
596 break;
597 case INTEL_PIPE_CRC_SOURCE_PLANE2:
598 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_IVB;
599 break;
600 case INTEL_PIPE_CRC_SOURCE_PF:
601 if (IS_HASWELL(dev_priv) && pipe == PIPE_A)
602 hsw_trans_edp_pipe_A_crc_wa(dev_priv, true);
603
604 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PF_IVB;
605 break;
606 case INTEL_PIPE_CRC_SOURCE_NONE:
607 *val = 0;
608 break;
609 default:
610 return -EINVAL;
611 }
612
613 return 0;
614}
615
616static int pipe_crc_set_source(struct drm_i915_private *dev_priv,
617 enum pipe pipe,
618 enum intel_pipe_crc_source source)
619{
620 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
621 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
622 enum intel_display_power_domain power_domain;
623 u32 val = 0; /* shut up gcc */
624 int ret;
625
626 if (pipe_crc->source == source)
627 return 0;
628
629 /* forbid changing the source without going back to 'none' */
630 if (pipe_crc->source && source)
631 return -EINVAL;
632
633 power_domain = POWER_DOMAIN_PIPE(pipe);
634 if (!intel_display_power_get_if_enabled(dev_priv, power_domain)) {
635 DRM_DEBUG_KMS("Trying to capture CRC while pipe is off\n");
636 return -EIO;
637 }
638
639 if (IS_GEN2(dev_priv))
640 ret = i8xx_pipe_crc_ctl_reg(&source, &val);
641 else if (INTEL_GEN(dev_priv) < 5)
642 ret = i9xx_pipe_crc_ctl_reg(dev_priv, pipe, &source, &val);
643 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
644 ret = vlv_pipe_crc_ctl_reg(dev_priv, pipe, &source, &val);
645 else if (IS_GEN5(dev_priv) || IS_GEN6(dev_priv))
646 ret = ilk_pipe_crc_ctl_reg(&source, &val);
647 else
648 ret = ivb_pipe_crc_ctl_reg(dev_priv, pipe, &source, &val);
649
650 if (ret != 0)
651 goto out;
652
653 /* none -> real source transition */
654 if (source) {
655 struct intel_pipe_crc_entry *entries;
656
657 DRM_DEBUG_DRIVER("collecting CRCs for pipe %c, %s\n",
658 pipe_name(pipe), pipe_crc_source_name(source));
659
660 entries = kcalloc(INTEL_PIPE_CRC_ENTRIES_NR,
661 sizeof(pipe_crc->entries[0]),
662 GFP_KERNEL);
663 if (!entries) {
664 ret = -ENOMEM;
665 goto out;
666 }
667
668 /*
669 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
670 * enabled and disabled dynamically based on package C states,
671 * user space can't make reliable use of the CRCs, so let's just
672 * completely disable it.
673 */
674 hsw_disable_ips(crtc);
675
676 spin_lock_irq(&pipe_crc->lock);
677 kfree(pipe_crc->entries);
678 pipe_crc->entries = entries;
679 pipe_crc->head = 0;
680 pipe_crc->tail = 0;
681 spin_unlock_irq(&pipe_crc->lock);
682 }
683
684 pipe_crc->source = source;
685
686 I915_WRITE(PIPE_CRC_CTL(pipe), val);
687 POSTING_READ(PIPE_CRC_CTL(pipe));
688
689 /* real source -> none transition */
690 if (source == INTEL_PIPE_CRC_SOURCE_NONE) {
691 struct intel_pipe_crc_entry *entries;
692 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
693 pipe);
694
695 DRM_DEBUG_DRIVER("stopping CRCs for pipe %c\n",
696 pipe_name(pipe));
697
698 drm_modeset_lock(&crtc->base.mutex, NULL);
699 if (crtc->base.state->active)
700 intel_wait_for_vblank(dev_priv, pipe);
701 drm_modeset_unlock(&crtc->base.mutex);
702
703 spin_lock_irq(&pipe_crc->lock);
704 entries = pipe_crc->entries;
705 pipe_crc->entries = NULL;
706 pipe_crc->head = 0;
707 pipe_crc->tail = 0;
708 spin_unlock_irq(&pipe_crc->lock);
709
710 kfree(entries);
711
712 if (IS_G4X(dev_priv))
713 g4x_undo_pipe_scramble_reset(dev_priv, pipe);
714 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
715 vlv_undo_pipe_scramble_reset(dev_priv, pipe);
716 else if (IS_HASWELL(dev_priv) && pipe == PIPE_A)
717 hsw_trans_edp_pipe_A_crc_wa(dev_priv, false);
718
719 hsw_enable_ips(crtc);
720 }
721
722 ret = 0;
723
724out:
725 intel_display_power_put(dev_priv, power_domain);
726
727 return ret;
728}
729
730/*
731 * Parse pipe CRC command strings:
732 * command: wsp* object wsp+ name wsp+ source wsp*
733 * object: 'pipe'
734 * name: (A | B | C)
735 * source: (none | plane1 | plane2 | pf)
736 * wsp: (#0x20 | #0x9 | #0xA)+
737 *
738 * eg.:
739 * "pipe A plane1" -> Start CRC computations on plane1 of pipe A
740 * "pipe A none" -> Stop CRC
741 */
742static int display_crc_ctl_tokenize(char *buf, char *words[], int max_words)
743{
744 int n_words = 0;
745
746 while (*buf) {
747 char *end;
748
749 /* skip leading white space */
750 buf = skip_spaces(buf);
751 if (!*buf)
752 break; /* end of buffer */
753
754 /* find end of word */
755 for (end = buf; *end && !isspace(*end); end++)
756 ;
757
758 if (n_words == max_words) {
759 DRM_DEBUG_DRIVER("too many words, allowed <= %d\n",
760 max_words);
761 return -EINVAL; /* ran out of words[] before bytes */
762 }
763
764 if (*end)
765 *end++ = '\0';
766 words[n_words++] = buf;
767 buf = end;
768 }
769
770 return n_words;
771}
772
773enum intel_pipe_crc_object {
774 PIPE_CRC_OBJECT_PIPE,
775};
776
777static const char * const pipe_crc_objects[] = {
778 "pipe",
779};
780
781static int
782display_crc_ctl_parse_object(const char *buf, enum intel_pipe_crc_object *o)
783{
784 int i;
785
786 for (i = 0; i < ARRAY_SIZE(pipe_crc_objects); i++)
787 if (!strcmp(buf, pipe_crc_objects[i])) {
788 *o = i;
789 return 0;
790 }
791
792 return -EINVAL;
793}
794
795static int display_crc_ctl_parse_pipe(const char *buf, enum pipe *pipe)
796{
797 const char name = buf[0];
798
799 if (name < 'A' || name >= pipe_name(I915_MAX_PIPES))
800 return -EINVAL;
801
802 *pipe = name - 'A';
803
804 return 0;
805}
806
807static int
808display_crc_ctl_parse_source(const char *buf, enum intel_pipe_crc_source *s)
809{
810 int i;
811
812 for (i = 0; i < ARRAY_SIZE(pipe_crc_sources); i++)
813 if (!strcmp(buf, pipe_crc_sources[i])) {
814 *s = i;
815 return 0;
816 }
817
818 return -EINVAL;
819}
820
821static int display_crc_ctl_parse(struct drm_i915_private *dev_priv,
822 char *buf, size_t len)
823{
824#define N_WORDS 3
825 int n_words;
826 char *words[N_WORDS];
827 enum pipe pipe;
828 enum intel_pipe_crc_object object;
829 enum intel_pipe_crc_source source;
830
831 n_words = display_crc_ctl_tokenize(buf, words, N_WORDS);
832 if (n_words != N_WORDS) {
833 DRM_DEBUG_DRIVER("tokenize failed, a command is %d words\n",
834 N_WORDS);
835 return -EINVAL;
836 }
837
838 if (display_crc_ctl_parse_object(words[0], &object) < 0) {
839 DRM_DEBUG_DRIVER("unknown object %s\n", words[0]);
840 return -EINVAL;
841 }
842
843 if (display_crc_ctl_parse_pipe(words[1], &pipe) < 0) {
844 DRM_DEBUG_DRIVER("unknown pipe %s\n", words[1]);
845 return -EINVAL;
846 }
847
848 if (display_crc_ctl_parse_source(words[2], &source) < 0) {
849 DRM_DEBUG_DRIVER("unknown source %s\n", words[2]);
850 return -EINVAL;
851 }
852
853 return pipe_crc_set_source(dev_priv, pipe, source);
854}
855
856static ssize_t display_crc_ctl_write(struct file *file, const char __user *ubuf,
857 size_t len, loff_t *offp)
858{
859 struct seq_file *m = file->private_data;
860 struct drm_i915_private *dev_priv = m->private;
861 char *tmpbuf;
862 int ret;
863
864 if (len == 0)
865 return 0;
866
867 if (len > PAGE_SIZE - 1) {
868 DRM_DEBUG_DRIVER("expected <%lu bytes into pipe crc control\n",
869 PAGE_SIZE);
870 return -E2BIG;
871 }
872
873 tmpbuf = kmalloc(len + 1, GFP_KERNEL);
874 if (!tmpbuf)
875 return -ENOMEM;
876
877 if (copy_from_user(tmpbuf, ubuf, len)) {
878 ret = -EFAULT;
879 goto out;
880 }
881 tmpbuf[len] = '\0';
882
883 ret = display_crc_ctl_parse(dev_priv, tmpbuf, len);
884
885out:
886 kfree(tmpbuf);
887 if (ret < 0)
888 return ret;
889
890 *offp += len;
891 return len;
892}
893
894const struct file_operations i915_display_crc_ctl_fops = {
895 .owner = THIS_MODULE,
896 .open = display_crc_ctl_open,
897 .read = seq_read,
898 .llseek = seq_lseek,
899 .release = single_release,
900 .write = display_crc_ctl_write
901};
902
903void intel_display_crc_init(struct drm_i915_private *dev_priv)
904{
905 enum pipe pipe;
906
907 for_each_pipe(dev_priv, pipe) {
908 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
909
910 pipe_crc->opened = false;
911 spin_lock_init(&pipe_crc->lock);
912 init_waitqueue_head(&pipe_crc->wq);
913 }
914}
915
916int intel_pipe_crc_create(struct drm_minor *minor)
917{
918 int ret, i;
919
920 for (i = 0; i < ARRAY_SIZE(i915_pipe_crc_data); i++) {
921 ret = i915_pipe_crc_create(minor->debugfs_root, minor, i);
922 if (ret)
923 return ret;
924 }
925
926 return 0;
927}
928
929void intel_pipe_crc_cleanup(struct drm_minor *minor)
930{
931 int i;
932
933 for (i = 0; i < ARRAY_SIZE(i915_pipe_crc_data); i++) {
934 struct drm_info_list *info_list =
935 (struct drm_info_list *)&i915_pipe_crc_data[i];
936
937 drm_debugfs_remove_files(info_list, 1, minor);
938 }
939}