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
23 * Author: Damien Lespiau <damien.lespiau@intel.com>
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_drv.h"
33 #include "intel_pipe_crc.h"
35 static const char * const pipe_crc_sources[] = {
36 [INTEL_PIPE_CRC_SOURCE_NONE] = "none",
37 [INTEL_PIPE_CRC_SOURCE_PLANE1] = "plane1",
38 [INTEL_PIPE_CRC_SOURCE_PLANE2] = "plane2",
39 [INTEL_PIPE_CRC_SOURCE_PLANE3] = "plane3",
40 [INTEL_PIPE_CRC_SOURCE_PLANE4] = "plane4",
41 [INTEL_PIPE_CRC_SOURCE_PLANE5] = "plane5",
42 [INTEL_PIPE_CRC_SOURCE_PLANE6] = "plane6",
43 [INTEL_PIPE_CRC_SOURCE_PLANE7] = "plane7",
44 [INTEL_PIPE_CRC_SOURCE_PIPE] = "pipe",
45 [INTEL_PIPE_CRC_SOURCE_TV] = "TV",
46 [INTEL_PIPE_CRC_SOURCE_DP_B] = "DP-B",
47 [INTEL_PIPE_CRC_SOURCE_DP_C] = "DP-C",
48 [INTEL_PIPE_CRC_SOURCE_DP_D] = "DP-D",
49 [INTEL_PIPE_CRC_SOURCE_AUTO] = "auto",
52 static int i8xx_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
55 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
56 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
59 case INTEL_PIPE_CRC_SOURCE_PIPE:
60 *val = PIPE_CRC_ENABLE | PIPE_CRC_INCLUDE_BORDER_I8XX;
62 case INTEL_PIPE_CRC_SOURCE_NONE:
72 static int i9xx_pipe_crc_auto_source(struct drm_i915_private *dev_priv,
74 enum intel_pipe_crc_source *source)
76 struct drm_device *dev = &dev_priv->drm;
77 struct intel_encoder *encoder;
78 struct intel_crtc *crtc;
79 struct intel_digital_port *dig_port;
82 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
84 drm_modeset_lock_all(dev);
85 for_each_intel_encoder(dev, encoder) {
86 if (!encoder->base.crtc)
89 crtc = to_intel_crtc(encoder->base.crtc);
91 if (crtc->pipe != pipe)
94 switch (encoder->type) {
95 case INTEL_OUTPUT_TVOUT:
96 *source = INTEL_PIPE_CRC_SOURCE_TV;
99 case INTEL_OUTPUT_EDP:
100 dig_port = enc_to_dig_port(&encoder->base);
101 switch (dig_port->base.port) {
103 *source = INTEL_PIPE_CRC_SOURCE_DP_B;
106 *source = INTEL_PIPE_CRC_SOURCE_DP_C;
109 *source = INTEL_PIPE_CRC_SOURCE_DP_D;
112 WARN(1, "nonexisting DP port %c\n",
113 port_name(dig_port->base.port));
121 drm_modeset_unlock_all(dev);
126 static int vlv_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
128 enum intel_pipe_crc_source *source,
131 bool need_stable_symbols = false;
133 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
134 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
140 case INTEL_PIPE_CRC_SOURCE_PIPE:
141 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_VLV;
143 case INTEL_PIPE_CRC_SOURCE_DP_B:
144 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_B_VLV;
145 need_stable_symbols = true;
147 case INTEL_PIPE_CRC_SOURCE_DP_C:
148 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_C_VLV;
149 need_stable_symbols = true;
151 case INTEL_PIPE_CRC_SOURCE_DP_D:
152 if (!IS_CHERRYVIEW(dev_priv))
154 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_D_VLV;
155 need_stable_symbols = true;
157 case INTEL_PIPE_CRC_SOURCE_NONE:
165 * When the pipe CRC tap point is after the transcoders we need
166 * to tweak symbol-level features to produce a deterministic series of
167 * symbols for a given frame. We need to reset those features only once
168 * a frame (instead of every nth symbol):
169 * - DC-balance: used to ensure a better clock recovery from the data
171 * - DisplayPort scrambling: used for EMI reduction
173 if (need_stable_symbols) {
174 u32 tmp = I915_READ(PORT_DFT2_G4X);
176 tmp |= DC_BALANCE_RESET_VLV;
179 tmp |= PIPE_A_SCRAMBLE_RESET;
182 tmp |= PIPE_B_SCRAMBLE_RESET;
185 tmp |= PIPE_C_SCRAMBLE_RESET;
190 I915_WRITE(PORT_DFT2_G4X, tmp);
196 static int i9xx_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
198 enum intel_pipe_crc_source *source,
201 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
202 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
208 case INTEL_PIPE_CRC_SOURCE_PIPE:
209 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_I9XX;
211 case INTEL_PIPE_CRC_SOURCE_TV:
212 if (!SUPPORTS_TV(dev_priv))
214 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_TV_PRE;
216 case INTEL_PIPE_CRC_SOURCE_NONE:
221 * The DP CRC source doesn't work on g4x.
222 * It can be made to work to some degree by selecting
223 * the correct CRC source before the port is enabled,
224 * and not touching the CRC source bits again until
225 * the port is disabled. But even then the bits
226 * eventually get stuck and a reboot is needed to get
227 * working CRCs on the pipe again. Let's simply
228 * refuse to use DP CRCs on g4x.
236 static void vlv_undo_pipe_scramble_reset(struct drm_i915_private *dev_priv,
239 u32 tmp = I915_READ(PORT_DFT2_G4X);
243 tmp &= ~PIPE_A_SCRAMBLE_RESET;
246 tmp &= ~PIPE_B_SCRAMBLE_RESET;
249 tmp &= ~PIPE_C_SCRAMBLE_RESET;
254 if (!(tmp & PIPE_SCRAMBLE_RESET_MASK))
255 tmp &= ~DC_BALANCE_RESET_VLV;
256 I915_WRITE(PORT_DFT2_G4X, tmp);
259 static int ilk_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
262 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
263 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
266 case INTEL_PIPE_CRC_SOURCE_PLANE1:
267 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_ILK;
269 case INTEL_PIPE_CRC_SOURCE_PLANE2:
270 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_ILK;
272 case INTEL_PIPE_CRC_SOURCE_PIPE:
273 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_ILK;
275 case INTEL_PIPE_CRC_SOURCE_NONE:
286 intel_crtc_crc_setup_workarounds(struct intel_crtc *crtc, bool enable)
288 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
289 struct intel_crtc_state *pipe_config;
290 struct drm_atomic_state *state;
291 struct drm_modeset_acquire_ctx ctx;
294 drm_modeset_acquire_init(&ctx, 0);
296 state = drm_atomic_state_alloc(&dev_priv->drm);
302 state->acquire_ctx = &ctx;
305 pipe_config = intel_atomic_get_crtc_state(state, crtc);
306 if (IS_ERR(pipe_config)) {
307 ret = PTR_ERR(pipe_config);
311 pipe_config->base.mode_changed = pipe_config->has_psr;
312 pipe_config->crc_enabled = enable;
314 if (IS_HASWELL(dev_priv) &&
315 pipe_config->base.active && crtc->pipe == PIPE_A &&
316 pipe_config->cpu_transcoder == TRANSCODER_EDP) {
317 bool old_need_power_well = pipe_config->pch_pfit.enabled ||
318 pipe_config->pch_pfit.force_thru;
319 bool new_need_power_well = pipe_config->pch_pfit.enabled ||
322 pipe_config->pch_pfit.force_thru = enable;
324 if (old_need_power_well != new_need_power_well)
325 pipe_config->base.connectors_changed = true;
328 ret = drm_atomic_commit(state);
331 if (ret == -EDEADLK) {
332 drm_atomic_state_clear(state);
333 drm_modeset_backoff(&ctx);
337 drm_atomic_state_put(state);
339 WARN(ret, "Toggling workaround to %i returns %i\n", enable, ret);
340 drm_modeset_drop_locks(&ctx);
341 drm_modeset_acquire_fini(&ctx);
344 static int ivb_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
346 enum intel_pipe_crc_source *source,
349 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
350 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
353 case INTEL_PIPE_CRC_SOURCE_PLANE1:
354 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_IVB;
356 case INTEL_PIPE_CRC_SOURCE_PLANE2:
357 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_IVB;
359 case INTEL_PIPE_CRC_SOURCE_PIPE:
360 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PF_IVB;
362 case INTEL_PIPE_CRC_SOURCE_NONE:
372 static int skl_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
374 enum intel_pipe_crc_source *source,
377 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
378 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
381 case INTEL_PIPE_CRC_SOURCE_PLANE1:
382 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_1_SKL;
384 case INTEL_PIPE_CRC_SOURCE_PLANE2:
385 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_2_SKL;
387 case INTEL_PIPE_CRC_SOURCE_PLANE3:
388 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_3_SKL;
390 case INTEL_PIPE_CRC_SOURCE_PLANE4:
391 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_4_SKL;
393 case INTEL_PIPE_CRC_SOURCE_PLANE5:
394 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_5_SKL;
396 case INTEL_PIPE_CRC_SOURCE_PLANE6:
397 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_6_SKL;
399 case INTEL_PIPE_CRC_SOURCE_PLANE7:
400 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_7_SKL;
402 case INTEL_PIPE_CRC_SOURCE_PIPE:
403 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DMUX_SKL;
405 case INTEL_PIPE_CRC_SOURCE_NONE:
415 static int get_new_crc_ctl_reg(struct drm_i915_private *dev_priv,
417 enum intel_pipe_crc_source *source, u32 *val)
419 if (IS_GEN(dev_priv, 2))
420 return i8xx_pipe_crc_ctl_reg(source, val);
421 else if (INTEL_GEN(dev_priv) < 5)
422 return i9xx_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
423 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
424 return vlv_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
425 else if (IS_GEN_RANGE(dev_priv, 5, 6))
426 return ilk_pipe_crc_ctl_reg(source, val);
427 else if (INTEL_GEN(dev_priv) < 9)
428 return ivb_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
430 return skl_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
434 display_crc_ctl_parse_source(const char *buf, enum intel_pipe_crc_source *s)
439 *s = INTEL_PIPE_CRC_SOURCE_NONE;
443 i = match_string(pipe_crc_sources, ARRAY_SIZE(pipe_crc_sources), buf);
451 void intel_display_crc_init(struct drm_i915_private *dev_priv)
455 for_each_pipe(dev_priv, pipe) {
456 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
458 spin_lock_init(&pipe_crc->lock);
462 static int i8xx_crc_source_valid(struct drm_i915_private *dev_priv,
463 const enum intel_pipe_crc_source source)
466 case INTEL_PIPE_CRC_SOURCE_PIPE:
467 case INTEL_PIPE_CRC_SOURCE_NONE:
474 static int i9xx_crc_source_valid(struct drm_i915_private *dev_priv,
475 const enum intel_pipe_crc_source source)
478 case INTEL_PIPE_CRC_SOURCE_PIPE:
479 case INTEL_PIPE_CRC_SOURCE_TV:
480 case INTEL_PIPE_CRC_SOURCE_NONE:
487 static int vlv_crc_source_valid(struct drm_i915_private *dev_priv,
488 const enum intel_pipe_crc_source source)
491 case INTEL_PIPE_CRC_SOURCE_PIPE:
492 case INTEL_PIPE_CRC_SOURCE_DP_B:
493 case INTEL_PIPE_CRC_SOURCE_DP_C:
494 case INTEL_PIPE_CRC_SOURCE_DP_D:
495 case INTEL_PIPE_CRC_SOURCE_NONE:
502 static int ilk_crc_source_valid(struct drm_i915_private *dev_priv,
503 const enum intel_pipe_crc_source source)
506 case INTEL_PIPE_CRC_SOURCE_PIPE:
507 case INTEL_PIPE_CRC_SOURCE_PLANE1:
508 case INTEL_PIPE_CRC_SOURCE_PLANE2:
509 case INTEL_PIPE_CRC_SOURCE_NONE:
516 static int ivb_crc_source_valid(struct drm_i915_private *dev_priv,
517 const enum intel_pipe_crc_source source)
520 case INTEL_PIPE_CRC_SOURCE_PIPE:
521 case INTEL_PIPE_CRC_SOURCE_PLANE1:
522 case INTEL_PIPE_CRC_SOURCE_PLANE2:
523 case INTEL_PIPE_CRC_SOURCE_NONE:
530 static int skl_crc_source_valid(struct drm_i915_private *dev_priv,
531 const enum intel_pipe_crc_source source)
534 case INTEL_PIPE_CRC_SOURCE_PIPE:
535 case INTEL_PIPE_CRC_SOURCE_PLANE1:
536 case INTEL_PIPE_CRC_SOURCE_PLANE2:
537 case INTEL_PIPE_CRC_SOURCE_PLANE3:
538 case INTEL_PIPE_CRC_SOURCE_PLANE4:
539 case INTEL_PIPE_CRC_SOURCE_PLANE5:
540 case INTEL_PIPE_CRC_SOURCE_PLANE6:
541 case INTEL_PIPE_CRC_SOURCE_PLANE7:
542 case INTEL_PIPE_CRC_SOURCE_NONE:
550 intel_is_valid_crc_source(struct drm_i915_private *dev_priv,
551 const enum intel_pipe_crc_source source)
553 if (IS_GEN(dev_priv, 2))
554 return i8xx_crc_source_valid(dev_priv, source);
555 else if (INTEL_GEN(dev_priv) < 5)
556 return i9xx_crc_source_valid(dev_priv, source);
557 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
558 return vlv_crc_source_valid(dev_priv, source);
559 else if (IS_GEN_RANGE(dev_priv, 5, 6))
560 return ilk_crc_source_valid(dev_priv, source);
561 else if (INTEL_GEN(dev_priv) < 9)
562 return ivb_crc_source_valid(dev_priv, source);
564 return skl_crc_source_valid(dev_priv, source);
567 const char *const *intel_crtc_get_crc_sources(struct drm_crtc *crtc,
570 *count = ARRAY_SIZE(pipe_crc_sources);
571 return pipe_crc_sources;
574 int intel_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
577 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
578 enum intel_pipe_crc_source source;
580 if (display_crc_ctl_parse_source(source_name, &source) < 0) {
581 DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
585 if (source == INTEL_PIPE_CRC_SOURCE_AUTO ||
586 intel_is_valid_crc_source(dev_priv, source) == 0) {
594 int intel_crtc_set_crc_source(struct drm_crtc *crtc, const char *source_name)
596 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
597 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[crtc->index];
598 enum intel_display_power_domain power_domain;
599 enum intel_pipe_crc_source source;
600 intel_wakeref_t wakeref;
601 u32 val = 0; /* shut up gcc */
605 if (display_crc_ctl_parse_source(source_name, &source) < 0) {
606 DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
610 power_domain = POWER_DOMAIN_PIPE(crtc->index);
611 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
613 DRM_DEBUG_KMS("Trying to capture CRC while pipe is off\n");
617 enable = source != INTEL_PIPE_CRC_SOURCE_NONE;
619 intel_crtc_crc_setup_workarounds(to_intel_crtc(crtc), true);
621 ret = get_new_crc_ctl_reg(dev_priv, crtc->index, &source, &val);
625 pipe_crc->source = source;
626 I915_WRITE(PIPE_CRC_CTL(crtc->index), val);
627 POSTING_READ(PIPE_CRC_CTL(crtc->index));
630 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
631 vlv_undo_pipe_scramble_reset(dev_priv, crtc->index);
634 pipe_crc->skipped = 0;
638 intel_crtc_crc_setup_workarounds(to_intel_crtc(crtc), false);
640 intel_display_power_put(dev_priv, power_domain, wakeref);
645 void intel_crtc_enable_pipe_crc(struct intel_crtc *intel_crtc)
647 struct drm_crtc *crtc = &intel_crtc->base;
648 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
649 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[crtc->index];
652 if (!crtc->crc.opened)
655 if (get_new_crc_ctl_reg(dev_priv, crtc->index, &pipe_crc->source, &val) < 0)
658 /* Don't need pipe_crc->lock here, IRQs are not generated. */
659 pipe_crc->skipped = 0;
661 I915_WRITE(PIPE_CRC_CTL(crtc->index), val);
662 POSTING_READ(PIPE_CRC_CTL(crtc->index));
665 void intel_crtc_disable_pipe_crc(struct intel_crtc *intel_crtc)
667 struct drm_crtc *crtc = &intel_crtc->base;
668 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
669 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[crtc->index];
671 /* Swallow crc's until we stop generating them. */
672 spin_lock_irq(&pipe_crc->lock);
673 pipe_crc->skipped = INT_MIN;
674 spin_unlock_irq(&pipe_crc->lock);
676 I915_WRITE(PIPE_CRC_CTL(crtc->index), 0);
677 POSTING_READ(PIPE_CRC_CTL(crtc->index));
678 synchronize_irq(dev_priv->drm.irq);