2 * Copyright © 2013 Intel Corporation
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:
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
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
27 #include <linux/circ_buf.h>
28 #include <linux/ctype.h>
29 #include <linux/debugfs.h>
30 #include <linux/seq_file.h>
32 #include "intel_atomic.h"
33 #include "intel_display_types.h"
34 #include "intel_pipe_crc.h"
36 static const char * const pipe_crc_sources[] = {
37 [INTEL_PIPE_CRC_SOURCE_NONE] = "none",
38 [INTEL_PIPE_CRC_SOURCE_PLANE1] = "plane1",
39 [INTEL_PIPE_CRC_SOURCE_PLANE2] = "plane2",
40 [INTEL_PIPE_CRC_SOURCE_PLANE3] = "plane3",
41 [INTEL_PIPE_CRC_SOURCE_PLANE4] = "plane4",
42 [INTEL_PIPE_CRC_SOURCE_PLANE5] = "plane5",
43 [INTEL_PIPE_CRC_SOURCE_PLANE6] = "plane6",
44 [INTEL_PIPE_CRC_SOURCE_PLANE7] = "plane7",
45 [INTEL_PIPE_CRC_SOURCE_PIPE] = "pipe",
46 [INTEL_PIPE_CRC_SOURCE_TV] = "TV",
47 [INTEL_PIPE_CRC_SOURCE_DP_B] = "DP-B",
48 [INTEL_PIPE_CRC_SOURCE_DP_C] = "DP-C",
49 [INTEL_PIPE_CRC_SOURCE_DP_D] = "DP-D",
50 [INTEL_PIPE_CRC_SOURCE_AUTO] = "auto",
53 static int i8xx_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
56 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
57 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
60 case INTEL_PIPE_CRC_SOURCE_PIPE:
61 *val = PIPE_CRC_ENABLE | PIPE_CRC_INCLUDE_BORDER_I8XX;
63 case INTEL_PIPE_CRC_SOURCE_NONE:
73 static int i9xx_pipe_crc_auto_source(struct drm_i915_private *dev_priv,
75 enum intel_pipe_crc_source *source)
77 struct drm_device *dev = &dev_priv->drm;
78 struct intel_encoder *encoder;
79 struct intel_crtc *crtc;
80 struct intel_digital_port *dig_port;
83 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
85 drm_modeset_lock_all(dev);
86 for_each_intel_encoder(dev, encoder) {
87 if (!encoder->base.crtc)
90 crtc = to_intel_crtc(encoder->base.crtc);
92 if (crtc->pipe != pipe)
95 switch (encoder->type) {
96 case INTEL_OUTPUT_TVOUT:
97 *source = INTEL_PIPE_CRC_SOURCE_TV;
100 case INTEL_OUTPUT_EDP:
101 dig_port = enc_to_dig_port(encoder);
102 switch (dig_port->base.port) {
104 *source = INTEL_PIPE_CRC_SOURCE_DP_B;
107 *source = INTEL_PIPE_CRC_SOURCE_DP_C;
110 *source = INTEL_PIPE_CRC_SOURCE_DP_D;
113 drm_WARN(dev, 1, "nonexisting DP port %c\n",
114 port_name(dig_port->base.port));
122 drm_modeset_unlock_all(dev);
127 static int vlv_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
129 enum intel_pipe_crc_source *source,
132 bool need_stable_symbols = false;
134 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
135 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
141 case INTEL_PIPE_CRC_SOURCE_PIPE:
142 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_VLV;
144 case INTEL_PIPE_CRC_SOURCE_DP_B:
145 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_B_VLV;
146 need_stable_symbols = true;
148 case INTEL_PIPE_CRC_SOURCE_DP_C:
149 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_C_VLV;
150 need_stable_symbols = true;
152 case INTEL_PIPE_CRC_SOURCE_DP_D:
153 if (!IS_CHERRYVIEW(dev_priv))
155 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_D_VLV;
156 need_stable_symbols = true;
158 case INTEL_PIPE_CRC_SOURCE_NONE:
166 * When the pipe CRC tap point is after the transcoders we need
167 * to tweak symbol-level features to produce a deterministic series of
168 * symbols for a given frame. We need to reset those features only once
169 * a frame (instead of every nth symbol):
170 * - DC-balance: used to ensure a better clock recovery from the data
172 * - DisplayPort scrambling: used for EMI reduction
174 if (need_stable_symbols) {
175 u32 tmp = intel_de_read(dev_priv, PORT_DFT2_G4X);
177 tmp |= DC_BALANCE_RESET_VLV;
180 tmp |= PIPE_A_SCRAMBLE_RESET;
183 tmp |= PIPE_B_SCRAMBLE_RESET;
186 tmp |= PIPE_C_SCRAMBLE_RESET;
191 intel_de_write(dev_priv, PORT_DFT2_G4X, tmp);
197 static int i9xx_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
199 enum intel_pipe_crc_source *source,
202 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
203 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
209 case INTEL_PIPE_CRC_SOURCE_PIPE:
210 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_I9XX;
212 case INTEL_PIPE_CRC_SOURCE_TV:
213 if (!SUPPORTS_TV(dev_priv))
215 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_TV_PRE;
217 case INTEL_PIPE_CRC_SOURCE_NONE:
222 * The DP CRC source doesn't work on g4x.
223 * It can be made to work to some degree by selecting
224 * the correct CRC source before the port is enabled,
225 * and not touching the CRC source bits again until
226 * the port is disabled. But even then the bits
227 * eventually get stuck and a reboot is needed to get
228 * working CRCs on the pipe again. Let's simply
229 * refuse to use DP CRCs on g4x.
237 static void vlv_undo_pipe_scramble_reset(struct drm_i915_private *dev_priv,
240 u32 tmp = intel_de_read(dev_priv, PORT_DFT2_G4X);
244 tmp &= ~PIPE_A_SCRAMBLE_RESET;
247 tmp &= ~PIPE_B_SCRAMBLE_RESET;
250 tmp &= ~PIPE_C_SCRAMBLE_RESET;
255 if (!(tmp & PIPE_SCRAMBLE_RESET_MASK))
256 tmp &= ~DC_BALANCE_RESET_VLV;
257 intel_de_write(dev_priv, PORT_DFT2_G4X, tmp);
260 static int ilk_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
263 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
264 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
267 case INTEL_PIPE_CRC_SOURCE_PLANE1:
268 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_ILK;
270 case INTEL_PIPE_CRC_SOURCE_PLANE2:
271 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_ILK;
273 case INTEL_PIPE_CRC_SOURCE_PIPE:
274 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_ILK;
276 case INTEL_PIPE_CRC_SOURCE_NONE:
287 intel_crtc_crc_setup_workarounds(struct intel_crtc *crtc, bool enable)
289 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
290 struct intel_crtc_state *pipe_config;
291 struct drm_atomic_state *state;
292 struct drm_modeset_acquire_ctx ctx;
295 drm_modeset_acquire_init(&ctx, 0);
297 state = drm_atomic_state_alloc(&dev_priv->drm);
303 state->acquire_ctx = &ctx;
306 pipe_config = intel_atomic_get_crtc_state(state, crtc);
307 if (IS_ERR(pipe_config)) {
308 ret = PTR_ERR(pipe_config);
312 pipe_config->uapi.mode_changed = pipe_config->has_psr;
313 pipe_config->crc_enabled = enable;
315 if (IS_HASWELL(dev_priv) &&
316 pipe_config->hw.active && crtc->pipe == PIPE_A &&
317 pipe_config->cpu_transcoder == TRANSCODER_EDP)
318 pipe_config->uapi.mode_changed = true;
320 ret = drm_atomic_commit(state);
323 if (ret == -EDEADLK) {
324 drm_atomic_state_clear(state);
325 drm_modeset_backoff(&ctx);
329 drm_atomic_state_put(state);
331 drm_WARN(&dev_priv->drm, ret,
332 "Toggling workaround to %i returns %i\n", enable, ret);
333 drm_modeset_drop_locks(&ctx);
334 drm_modeset_acquire_fini(&ctx);
337 static int ivb_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
339 enum intel_pipe_crc_source *source,
342 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
343 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
346 case INTEL_PIPE_CRC_SOURCE_PLANE1:
347 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_IVB;
349 case INTEL_PIPE_CRC_SOURCE_PLANE2:
350 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_IVB;
352 case INTEL_PIPE_CRC_SOURCE_PIPE:
353 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PF_IVB;
355 case INTEL_PIPE_CRC_SOURCE_NONE:
365 static int skl_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
367 enum intel_pipe_crc_source *source,
370 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
371 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
374 case INTEL_PIPE_CRC_SOURCE_PLANE1:
375 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_1_SKL;
377 case INTEL_PIPE_CRC_SOURCE_PLANE2:
378 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_2_SKL;
380 case INTEL_PIPE_CRC_SOURCE_PLANE3:
381 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_3_SKL;
383 case INTEL_PIPE_CRC_SOURCE_PLANE4:
384 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_4_SKL;
386 case INTEL_PIPE_CRC_SOURCE_PLANE5:
387 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_5_SKL;
389 case INTEL_PIPE_CRC_SOURCE_PLANE6:
390 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_6_SKL;
392 case INTEL_PIPE_CRC_SOURCE_PLANE7:
393 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_7_SKL;
395 case INTEL_PIPE_CRC_SOURCE_PIPE:
396 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DMUX_SKL;
398 case INTEL_PIPE_CRC_SOURCE_NONE:
408 static int get_new_crc_ctl_reg(struct drm_i915_private *dev_priv,
410 enum intel_pipe_crc_source *source, u32 *val)
412 if (IS_GEN(dev_priv, 2))
413 return i8xx_pipe_crc_ctl_reg(source, val);
414 else if (INTEL_GEN(dev_priv) < 5)
415 return i9xx_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
416 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
417 return vlv_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
418 else if (IS_GEN_RANGE(dev_priv, 5, 6))
419 return ilk_pipe_crc_ctl_reg(source, val);
420 else if (INTEL_GEN(dev_priv) < 9)
421 return ivb_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
423 return skl_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
427 display_crc_ctl_parse_source(const char *buf, enum intel_pipe_crc_source *s)
432 *s = INTEL_PIPE_CRC_SOURCE_NONE;
436 i = match_string(pipe_crc_sources, ARRAY_SIZE(pipe_crc_sources), buf);
444 void intel_crtc_crc_init(struct intel_crtc *crtc)
446 struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
448 spin_lock_init(&pipe_crc->lock);
451 static int i8xx_crc_source_valid(struct drm_i915_private *dev_priv,
452 const enum intel_pipe_crc_source source)
455 case INTEL_PIPE_CRC_SOURCE_PIPE:
456 case INTEL_PIPE_CRC_SOURCE_NONE:
463 static int i9xx_crc_source_valid(struct drm_i915_private *dev_priv,
464 const enum intel_pipe_crc_source source)
467 case INTEL_PIPE_CRC_SOURCE_PIPE:
468 case INTEL_PIPE_CRC_SOURCE_TV:
469 case INTEL_PIPE_CRC_SOURCE_NONE:
476 static int vlv_crc_source_valid(struct drm_i915_private *dev_priv,
477 const enum intel_pipe_crc_source source)
480 case INTEL_PIPE_CRC_SOURCE_PIPE:
481 case INTEL_PIPE_CRC_SOURCE_DP_B:
482 case INTEL_PIPE_CRC_SOURCE_DP_C:
483 case INTEL_PIPE_CRC_SOURCE_DP_D:
484 case INTEL_PIPE_CRC_SOURCE_NONE:
491 static int ilk_crc_source_valid(struct drm_i915_private *dev_priv,
492 const enum intel_pipe_crc_source source)
495 case INTEL_PIPE_CRC_SOURCE_PIPE:
496 case INTEL_PIPE_CRC_SOURCE_PLANE1:
497 case INTEL_PIPE_CRC_SOURCE_PLANE2:
498 case INTEL_PIPE_CRC_SOURCE_NONE:
505 static int ivb_crc_source_valid(struct drm_i915_private *dev_priv,
506 const enum intel_pipe_crc_source source)
509 case INTEL_PIPE_CRC_SOURCE_PIPE:
510 case INTEL_PIPE_CRC_SOURCE_PLANE1:
511 case INTEL_PIPE_CRC_SOURCE_PLANE2:
512 case INTEL_PIPE_CRC_SOURCE_NONE:
519 static int skl_crc_source_valid(struct drm_i915_private *dev_priv,
520 const enum intel_pipe_crc_source source)
523 case INTEL_PIPE_CRC_SOURCE_PIPE:
524 case INTEL_PIPE_CRC_SOURCE_PLANE1:
525 case INTEL_PIPE_CRC_SOURCE_PLANE2:
526 case INTEL_PIPE_CRC_SOURCE_PLANE3:
527 case INTEL_PIPE_CRC_SOURCE_PLANE4:
528 case INTEL_PIPE_CRC_SOURCE_PLANE5:
529 case INTEL_PIPE_CRC_SOURCE_PLANE6:
530 case INTEL_PIPE_CRC_SOURCE_PLANE7:
531 case INTEL_PIPE_CRC_SOURCE_NONE:
539 intel_is_valid_crc_source(struct drm_i915_private *dev_priv,
540 const enum intel_pipe_crc_source source)
542 if (IS_GEN(dev_priv, 2))
543 return i8xx_crc_source_valid(dev_priv, source);
544 else if (INTEL_GEN(dev_priv) < 5)
545 return i9xx_crc_source_valid(dev_priv, source);
546 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
547 return vlv_crc_source_valid(dev_priv, source);
548 else if (IS_GEN_RANGE(dev_priv, 5, 6))
549 return ilk_crc_source_valid(dev_priv, source);
550 else if (INTEL_GEN(dev_priv) < 9)
551 return ivb_crc_source_valid(dev_priv, source);
553 return skl_crc_source_valid(dev_priv, source);
556 const char *const *intel_crtc_get_crc_sources(struct drm_crtc *crtc,
559 *count = ARRAY_SIZE(pipe_crc_sources);
560 return pipe_crc_sources;
563 int intel_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
566 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
567 enum intel_pipe_crc_source source;
569 if (display_crc_ctl_parse_source(source_name, &source) < 0) {
570 drm_dbg(&dev_priv->drm, "unknown source %s\n", source_name);
574 if (source == INTEL_PIPE_CRC_SOURCE_AUTO ||
575 intel_is_valid_crc_source(dev_priv, source) == 0) {
583 int intel_crtc_set_crc_source(struct drm_crtc *crtc, const char *source_name)
585 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
587 struct intel_pipe_crc *pipe_crc = &intel_crtc->pipe_crc;
588 enum intel_display_power_domain power_domain;
589 enum intel_pipe_crc_source source;
590 intel_wakeref_t wakeref;
591 u32 val = 0; /* shut up gcc */
595 if (display_crc_ctl_parse_source(source_name, &source) < 0) {
596 drm_dbg(&dev_priv->drm, "unknown source %s\n", source_name);
600 power_domain = POWER_DOMAIN_PIPE(crtc->index);
601 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
603 drm_dbg_kms(&dev_priv->drm,
604 "Trying to capture CRC while pipe is off\n");
608 enable = source != INTEL_PIPE_CRC_SOURCE_NONE;
610 intel_crtc_crc_setup_workarounds(to_intel_crtc(crtc), true);
612 ret = get_new_crc_ctl_reg(dev_priv, crtc->index, &source, &val);
616 pipe_crc->source = source;
617 intel_de_write(dev_priv, PIPE_CRC_CTL(crtc->index), val);
618 intel_de_posting_read(dev_priv, PIPE_CRC_CTL(crtc->index));
621 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
622 vlv_undo_pipe_scramble_reset(dev_priv, crtc->index);
625 pipe_crc->skipped = 0;
629 intel_crtc_crc_setup_workarounds(to_intel_crtc(crtc), false);
631 intel_display_power_put(dev_priv, power_domain, wakeref);
636 void intel_crtc_enable_pipe_crc(struct intel_crtc *intel_crtc)
638 struct drm_crtc *crtc = &intel_crtc->base;
639 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
640 struct intel_pipe_crc *pipe_crc = &intel_crtc->pipe_crc;
643 if (!crtc->crc.opened)
646 if (get_new_crc_ctl_reg(dev_priv, crtc->index, &pipe_crc->source, &val) < 0)
649 /* Don't need pipe_crc->lock here, IRQs are not generated. */
650 pipe_crc->skipped = 0;
652 intel_de_write(dev_priv, PIPE_CRC_CTL(crtc->index), val);
653 intel_de_posting_read(dev_priv, PIPE_CRC_CTL(crtc->index));
656 void intel_crtc_disable_pipe_crc(struct intel_crtc *intel_crtc)
658 struct drm_crtc *crtc = &intel_crtc->base;
659 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
660 struct intel_pipe_crc *pipe_crc = &intel_crtc->pipe_crc;
662 /* Swallow crc's until we stop generating them. */
663 spin_lock_irq(&pipe_crc->lock);
664 pipe_crc->skipped = INT_MIN;
665 spin_unlock_irq(&pipe_crc->lock);
667 intel_de_write(dev_priv, PIPE_CRC_CTL(crtc->index), 0);
668 intel_de_posting_read(dev_priv, PIPE_CRC_CTL(crtc->index));
669 intel_synchronize_irq(dev_priv);