1 /*
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/ctype.h>
28 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
30 
31 #include "i915_irq.h"
32 #include "i915_reg.h"
33 #include "intel_atomic.h"
34 #include "intel_de.h"
35 #include "intel_display_types.h"
36 #include "intel_pipe_crc.h"
37 
38 static const char * const pipe_crc_sources[] = {
39 	[INTEL_PIPE_CRC_SOURCE_NONE] = "none",
40 	[INTEL_PIPE_CRC_SOURCE_PLANE1] = "plane1",
41 	[INTEL_PIPE_CRC_SOURCE_PLANE2] = "plane2",
42 	[INTEL_PIPE_CRC_SOURCE_PLANE3] = "plane3",
43 	[INTEL_PIPE_CRC_SOURCE_PLANE4] = "plane4",
44 	[INTEL_PIPE_CRC_SOURCE_PLANE5] = "plane5",
45 	[INTEL_PIPE_CRC_SOURCE_PLANE6] = "plane6",
46 	[INTEL_PIPE_CRC_SOURCE_PLANE7] = "plane7",
47 	[INTEL_PIPE_CRC_SOURCE_PIPE] = "pipe",
48 	[INTEL_PIPE_CRC_SOURCE_TV] = "TV",
49 	[INTEL_PIPE_CRC_SOURCE_DP_B] = "DP-B",
50 	[INTEL_PIPE_CRC_SOURCE_DP_C] = "DP-C",
51 	[INTEL_PIPE_CRC_SOURCE_DP_D] = "DP-D",
52 	[INTEL_PIPE_CRC_SOURCE_AUTO] = "auto",
53 };
54 
55 static int i8xx_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
56 				 u32 *val)
57 {
58 	if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
59 		*source = INTEL_PIPE_CRC_SOURCE_PIPE;
60 
61 	switch (*source) {
62 	case INTEL_PIPE_CRC_SOURCE_PIPE:
63 		*val = PIPE_CRC_ENABLE | PIPE_CRC_INCLUDE_BORDER_I8XX;
64 		break;
65 	case INTEL_PIPE_CRC_SOURCE_NONE:
66 		*val = 0;
67 		break;
68 	default:
69 		return -EINVAL;
70 	}
71 
72 	return 0;
73 }
74 
75 static void i9xx_pipe_crc_auto_source(struct drm_i915_private *dev_priv,
76 				      enum pipe pipe,
77 				      enum intel_pipe_crc_source *source)
78 {
79 	struct intel_encoder *encoder;
80 	struct intel_crtc *crtc;
81 	struct intel_digital_port *dig_port;
82 
83 	*source = INTEL_PIPE_CRC_SOURCE_PIPE;
84 
85 	drm_modeset_lock_all(&dev_priv->drm);
86 	for_each_intel_encoder(&dev_priv->drm, encoder) {
87 		if (!encoder->base.crtc)
88 			continue;
89 
90 		crtc = to_intel_crtc(encoder->base.crtc);
91 
92 		if (crtc->pipe != pipe)
93 			continue;
94 
95 		switch (encoder->type) {
96 		case INTEL_OUTPUT_TVOUT:
97 			*source = INTEL_PIPE_CRC_SOURCE_TV;
98 			break;
99 		case INTEL_OUTPUT_DP:
100 		case INTEL_OUTPUT_EDP:
101 			dig_port = enc_to_dig_port(encoder);
102 			switch (dig_port->base.port) {
103 			case PORT_B:
104 				*source = INTEL_PIPE_CRC_SOURCE_DP_B;
105 				break;
106 			case PORT_C:
107 				*source = INTEL_PIPE_CRC_SOURCE_DP_C;
108 				break;
109 			case PORT_D:
110 				*source = INTEL_PIPE_CRC_SOURCE_DP_D;
111 				break;
112 			default:
113 				drm_WARN(&dev_priv->drm, 1, "nonexisting DP port %c\n",
114 					 port_name(dig_port->base.port));
115 				break;
116 			}
117 			break;
118 		default:
119 			break;
120 		}
121 	}
122 	drm_modeset_unlock_all(&dev_priv->drm);
123 }
124 
125 static int vlv_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
126 				enum pipe pipe,
127 				enum intel_pipe_crc_source *source,
128 				u32 *val)
129 {
130 	bool need_stable_symbols = false;
131 
132 	if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
133 		i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
134 
135 	switch (*source) {
136 	case INTEL_PIPE_CRC_SOURCE_PIPE:
137 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_VLV;
138 		break;
139 	case INTEL_PIPE_CRC_SOURCE_DP_B:
140 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_B_VLV;
141 		need_stable_symbols = true;
142 		break;
143 	case INTEL_PIPE_CRC_SOURCE_DP_C:
144 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_C_VLV;
145 		need_stable_symbols = true;
146 		break;
147 	case INTEL_PIPE_CRC_SOURCE_DP_D:
148 		if (!IS_CHERRYVIEW(dev_priv))
149 			return -EINVAL;
150 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_D_VLV;
151 		need_stable_symbols = true;
152 		break;
153 	case INTEL_PIPE_CRC_SOURCE_NONE:
154 		*val = 0;
155 		break;
156 	default:
157 		return -EINVAL;
158 	}
159 
160 	/*
161 	 * When the pipe CRC tap point is after the transcoders we need
162 	 * to tweak symbol-level features to produce a deterministic series of
163 	 * symbols for a given frame. We need to reset those features only once
164 	 * a frame (instead of every nth symbol):
165 	 *   - DC-balance: used to ensure a better clock recovery from the data
166 	 *     link (SDVO)
167 	 *   - DisplayPort scrambling: used for EMI reduction
168 	 */
169 	if (need_stable_symbols) {
170 		u32 tmp = intel_de_read(dev_priv, PORT_DFT2_G4X);
171 
172 		tmp |= DC_BALANCE_RESET_VLV;
173 		switch (pipe) {
174 		case PIPE_A:
175 			tmp |= PIPE_A_SCRAMBLE_RESET;
176 			break;
177 		case PIPE_B:
178 			tmp |= PIPE_B_SCRAMBLE_RESET;
179 			break;
180 		case PIPE_C:
181 			tmp |= PIPE_C_SCRAMBLE_RESET;
182 			break;
183 		default:
184 			return -EINVAL;
185 		}
186 		intel_de_write(dev_priv, PORT_DFT2_G4X, tmp);
187 	}
188 
189 	return 0;
190 }
191 
192 static int i9xx_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
193 				 enum pipe pipe,
194 				 enum intel_pipe_crc_source *source,
195 				 u32 *val)
196 {
197 	if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
198 		i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
199 
200 	switch (*source) {
201 	case INTEL_PIPE_CRC_SOURCE_PIPE:
202 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_I9XX;
203 		break;
204 	case INTEL_PIPE_CRC_SOURCE_TV:
205 		if (!SUPPORTS_TV(dev_priv))
206 			return -EINVAL;
207 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_TV_PRE;
208 		break;
209 	case INTEL_PIPE_CRC_SOURCE_NONE:
210 		*val = 0;
211 		break;
212 	default:
213 		/*
214 		 * The DP CRC source doesn't work on g4x.
215 		 * It can be made to work to some degree by selecting
216 		 * the correct CRC source before the port is enabled,
217 		 * and not touching the CRC source bits again until
218 		 * the port is disabled. But even then the bits
219 		 * eventually get stuck and a reboot is needed to get
220 		 * working CRCs on the pipe again. Let's simply
221 		 * refuse to use DP CRCs on g4x.
222 		 */
223 		return -EINVAL;
224 	}
225 
226 	return 0;
227 }
228 
229 static void vlv_undo_pipe_scramble_reset(struct drm_i915_private *dev_priv,
230 					 enum pipe pipe)
231 {
232 	u32 tmp = intel_de_read(dev_priv, PORT_DFT2_G4X);
233 
234 	switch (pipe) {
235 	case PIPE_A:
236 		tmp &= ~PIPE_A_SCRAMBLE_RESET;
237 		break;
238 	case PIPE_B:
239 		tmp &= ~PIPE_B_SCRAMBLE_RESET;
240 		break;
241 	case PIPE_C:
242 		tmp &= ~PIPE_C_SCRAMBLE_RESET;
243 		break;
244 	default:
245 		return;
246 	}
247 	if (!(tmp & PIPE_SCRAMBLE_RESET_MASK))
248 		tmp &= ~DC_BALANCE_RESET_VLV;
249 	intel_de_write(dev_priv, PORT_DFT2_G4X, tmp);
250 }
251 
252 static int ilk_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
253 				u32 *val)
254 {
255 	if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
256 		*source = INTEL_PIPE_CRC_SOURCE_PIPE;
257 
258 	switch (*source) {
259 	case INTEL_PIPE_CRC_SOURCE_PLANE1:
260 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_ILK;
261 		break;
262 	case INTEL_PIPE_CRC_SOURCE_PLANE2:
263 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_ILK;
264 		break;
265 	case INTEL_PIPE_CRC_SOURCE_PIPE:
266 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_ILK;
267 		break;
268 	case INTEL_PIPE_CRC_SOURCE_NONE:
269 		*val = 0;
270 		break;
271 	default:
272 		return -EINVAL;
273 	}
274 
275 	return 0;
276 }
277 
278 static void
279 intel_crtc_crc_setup_workarounds(struct intel_crtc *crtc, bool enable)
280 {
281 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
282 	struct intel_crtc_state *pipe_config;
283 	struct drm_atomic_state *state;
284 	struct drm_modeset_acquire_ctx ctx;
285 	int ret;
286 
287 	drm_modeset_acquire_init(&ctx, 0);
288 
289 	state = drm_atomic_state_alloc(&dev_priv->drm);
290 	if (!state) {
291 		ret = -ENOMEM;
292 		goto unlock;
293 	}
294 
295 	state->acquire_ctx = &ctx;
296 
297 retry:
298 	pipe_config = intel_atomic_get_crtc_state(state, crtc);
299 	if (IS_ERR(pipe_config)) {
300 		ret = PTR_ERR(pipe_config);
301 		goto put_state;
302 	}
303 
304 	pipe_config->uapi.mode_changed = pipe_config->has_psr;
305 	pipe_config->crc_enabled = enable;
306 
307 	if (IS_HASWELL(dev_priv) &&
308 	    pipe_config->hw.active && crtc->pipe == PIPE_A &&
309 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
310 		pipe_config->uapi.mode_changed = true;
311 
312 	ret = drm_atomic_commit(state);
313 
314 put_state:
315 	if (ret == -EDEADLK) {
316 		drm_atomic_state_clear(state);
317 		drm_modeset_backoff(&ctx);
318 		goto retry;
319 	}
320 
321 	drm_atomic_state_put(state);
322 unlock:
323 	drm_WARN(&dev_priv->drm, ret,
324 		 "Toggling workaround to %i returns %i\n", enable, ret);
325 	drm_modeset_drop_locks(&ctx);
326 	drm_modeset_acquire_fini(&ctx);
327 }
328 
329 static int ivb_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
330 				enum pipe pipe,
331 				enum intel_pipe_crc_source *source,
332 				u32 *val)
333 {
334 	if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
335 		*source = INTEL_PIPE_CRC_SOURCE_PIPE;
336 
337 	switch (*source) {
338 	case INTEL_PIPE_CRC_SOURCE_PLANE1:
339 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_IVB;
340 		break;
341 	case INTEL_PIPE_CRC_SOURCE_PLANE2:
342 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_IVB;
343 		break;
344 	case INTEL_PIPE_CRC_SOURCE_PIPE:
345 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PF_IVB;
346 		break;
347 	case INTEL_PIPE_CRC_SOURCE_NONE:
348 		*val = 0;
349 		break;
350 	default:
351 		return -EINVAL;
352 	}
353 
354 	return 0;
355 }
356 
357 static int skl_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
358 				enum pipe pipe,
359 				enum intel_pipe_crc_source *source,
360 				u32 *val)
361 {
362 	if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
363 		*source = INTEL_PIPE_CRC_SOURCE_PIPE;
364 
365 	switch (*source) {
366 	case INTEL_PIPE_CRC_SOURCE_PLANE1:
367 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_1_SKL;
368 		break;
369 	case INTEL_PIPE_CRC_SOURCE_PLANE2:
370 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_2_SKL;
371 		break;
372 	case INTEL_PIPE_CRC_SOURCE_PLANE3:
373 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_3_SKL;
374 		break;
375 	case INTEL_PIPE_CRC_SOURCE_PLANE4:
376 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_4_SKL;
377 		break;
378 	case INTEL_PIPE_CRC_SOURCE_PLANE5:
379 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_5_SKL;
380 		break;
381 	case INTEL_PIPE_CRC_SOURCE_PLANE6:
382 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_6_SKL;
383 		break;
384 	case INTEL_PIPE_CRC_SOURCE_PLANE7:
385 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PLANE_7_SKL;
386 		break;
387 	case INTEL_PIPE_CRC_SOURCE_PIPE:
388 		*val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DMUX_SKL;
389 		break;
390 	case INTEL_PIPE_CRC_SOURCE_NONE:
391 		*val = 0;
392 		break;
393 	default:
394 		return -EINVAL;
395 	}
396 
397 	return 0;
398 }
399 
400 static int get_new_crc_ctl_reg(struct drm_i915_private *dev_priv,
401 			       enum pipe pipe,
402 			       enum intel_pipe_crc_source *source, u32 *val)
403 {
404 	if (DISPLAY_VER(dev_priv) == 2)
405 		return i8xx_pipe_crc_ctl_reg(source, val);
406 	else if (DISPLAY_VER(dev_priv) < 5)
407 		return i9xx_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
408 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
409 		return vlv_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
410 	else if (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv))
411 		return ilk_pipe_crc_ctl_reg(source, val);
412 	else if (DISPLAY_VER(dev_priv) < 9)
413 		return ivb_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
414 	else
415 		return skl_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
416 }
417 
418 static int
419 display_crc_ctl_parse_source(const char *buf, enum intel_pipe_crc_source *s)
420 {
421 	int i;
422 
423 	if (!buf) {
424 		*s = INTEL_PIPE_CRC_SOURCE_NONE;
425 		return 0;
426 	}
427 
428 	i = match_string(pipe_crc_sources, ARRAY_SIZE(pipe_crc_sources), buf);
429 	if (i < 0)
430 		return i;
431 
432 	*s = i;
433 	return 0;
434 }
435 
436 void intel_crtc_crc_init(struct intel_crtc *crtc)
437 {
438 	struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
439 
440 	spin_lock_init(&pipe_crc->lock);
441 }
442 
443 static int i8xx_crc_source_valid(struct drm_i915_private *dev_priv,
444 				 const enum intel_pipe_crc_source source)
445 {
446 	switch (source) {
447 	case INTEL_PIPE_CRC_SOURCE_PIPE:
448 	case INTEL_PIPE_CRC_SOURCE_NONE:
449 		return 0;
450 	default:
451 		return -EINVAL;
452 	}
453 }
454 
455 static int i9xx_crc_source_valid(struct drm_i915_private *dev_priv,
456 				 const enum intel_pipe_crc_source source)
457 {
458 	switch (source) {
459 	case INTEL_PIPE_CRC_SOURCE_PIPE:
460 	case INTEL_PIPE_CRC_SOURCE_TV:
461 	case INTEL_PIPE_CRC_SOURCE_NONE:
462 		return 0;
463 	default:
464 		return -EINVAL;
465 	}
466 }
467 
468 static int vlv_crc_source_valid(struct drm_i915_private *dev_priv,
469 				const enum intel_pipe_crc_source source)
470 {
471 	switch (source) {
472 	case INTEL_PIPE_CRC_SOURCE_PIPE:
473 	case INTEL_PIPE_CRC_SOURCE_DP_B:
474 	case INTEL_PIPE_CRC_SOURCE_DP_C:
475 	case INTEL_PIPE_CRC_SOURCE_DP_D:
476 	case INTEL_PIPE_CRC_SOURCE_NONE:
477 		return 0;
478 	default:
479 		return -EINVAL;
480 	}
481 }
482 
483 static int ilk_crc_source_valid(struct drm_i915_private *dev_priv,
484 				const enum intel_pipe_crc_source source)
485 {
486 	switch (source) {
487 	case INTEL_PIPE_CRC_SOURCE_PIPE:
488 	case INTEL_PIPE_CRC_SOURCE_PLANE1:
489 	case INTEL_PIPE_CRC_SOURCE_PLANE2:
490 	case INTEL_PIPE_CRC_SOURCE_NONE:
491 		return 0;
492 	default:
493 		return -EINVAL;
494 	}
495 }
496 
497 static int ivb_crc_source_valid(struct drm_i915_private *dev_priv,
498 				const enum intel_pipe_crc_source source)
499 {
500 	switch (source) {
501 	case INTEL_PIPE_CRC_SOURCE_PIPE:
502 	case INTEL_PIPE_CRC_SOURCE_PLANE1:
503 	case INTEL_PIPE_CRC_SOURCE_PLANE2:
504 	case INTEL_PIPE_CRC_SOURCE_NONE:
505 		return 0;
506 	default:
507 		return -EINVAL;
508 	}
509 }
510 
511 static int skl_crc_source_valid(struct drm_i915_private *dev_priv,
512 				const enum intel_pipe_crc_source source)
513 {
514 	switch (source) {
515 	case INTEL_PIPE_CRC_SOURCE_PIPE:
516 	case INTEL_PIPE_CRC_SOURCE_PLANE1:
517 	case INTEL_PIPE_CRC_SOURCE_PLANE2:
518 	case INTEL_PIPE_CRC_SOURCE_PLANE3:
519 	case INTEL_PIPE_CRC_SOURCE_PLANE4:
520 	case INTEL_PIPE_CRC_SOURCE_PLANE5:
521 	case INTEL_PIPE_CRC_SOURCE_PLANE6:
522 	case INTEL_PIPE_CRC_SOURCE_PLANE7:
523 	case INTEL_PIPE_CRC_SOURCE_NONE:
524 		return 0;
525 	default:
526 		return -EINVAL;
527 	}
528 }
529 
530 static int
531 intel_is_valid_crc_source(struct drm_i915_private *dev_priv,
532 			  const enum intel_pipe_crc_source source)
533 {
534 	if (DISPLAY_VER(dev_priv) == 2)
535 		return i8xx_crc_source_valid(dev_priv, source);
536 	else if (DISPLAY_VER(dev_priv) < 5)
537 		return i9xx_crc_source_valid(dev_priv, source);
538 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
539 		return vlv_crc_source_valid(dev_priv, source);
540 	else if (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv))
541 		return ilk_crc_source_valid(dev_priv, source);
542 	else if (DISPLAY_VER(dev_priv) < 9)
543 		return ivb_crc_source_valid(dev_priv, source);
544 	else
545 		return skl_crc_source_valid(dev_priv, source);
546 }
547 
548 const char *const *intel_crtc_get_crc_sources(struct drm_crtc *crtc,
549 					      size_t *count)
550 {
551 	*count = ARRAY_SIZE(pipe_crc_sources);
552 	return pipe_crc_sources;
553 }
554 
555 int intel_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
556 				 size_t *values_cnt)
557 {
558 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
559 	enum intel_pipe_crc_source source;
560 
561 	if (display_crc_ctl_parse_source(source_name, &source) < 0) {
562 		drm_dbg(&dev_priv->drm, "unknown source %s\n", source_name);
563 		return -EINVAL;
564 	}
565 
566 	if (source == INTEL_PIPE_CRC_SOURCE_AUTO ||
567 	    intel_is_valid_crc_source(dev_priv, source) == 0) {
568 		*values_cnt = 5;
569 		return 0;
570 	}
571 
572 	return -EINVAL;
573 }
574 
575 int intel_crtc_set_crc_source(struct drm_crtc *_crtc, const char *source_name)
576 {
577 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
578 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
579 	struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
580 	enum intel_display_power_domain power_domain;
581 	enum intel_pipe_crc_source source;
582 	enum pipe pipe = crtc->pipe;
583 	intel_wakeref_t wakeref;
584 	u32 val = 0; /* shut up gcc */
585 	int ret = 0;
586 	bool enable;
587 
588 	if (display_crc_ctl_parse_source(source_name, &source) < 0) {
589 		drm_dbg(&dev_priv->drm, "unknown source %s\n", source_name);
590 		return -EINVAL;
591 	}
592 
593 	power_domain = POWER_DOMAIN_PIPE(pipe);
594 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
595 	if (!wakeref) {
596 		drm_dbg_kms(&dev_priv->drm,
597 			    "Trying to capture CRC while pipe is off\n");
598 		return -EIO;
599 	}
600 
601 	enable = source != INTEL_PIPE_CRC_SOURCE_NONE;
602 	if (enable)
603 		intel_crtc_crc_setup_workarounds(crtc, true);
604 
605 	ret = get_new_crc_ctl_reg(dev_priv, pipe, &source, &val);
606 	if (ret != 0)
607 		goto out;
608 
609 	pipe_crc->source = source;
610 	intel_de_write(dev_priv, PIPE_CRC_CTL(pipe), val);
611 	intel_de_posting_read(dev_priv, PIPE_CRC_CTL(pipe));
612 
613 	if (!source) {
614 		if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
615 			vlv_undo_pipe_scramble_reset(dev_priv, pipe);
616 	}
617 
618 	pipe_crc->skipped = 0;
619 
620 out:
621 	if (!enable)
622 		intel_crtc_crc_setup_workarounds(crtc, false);
623 
624 	intel_display_power_put(dev_priv, power_domain, wakeref);
625 
626 	return ret;
627 }
628 
629 void intel_crtc_enable_pipe_crc(struct intel_crtc *crtc)
630 {
631 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
632 	struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
633 	enum pipe pipe = crtc->pipe;
634 	u32 val = 0;
635 
636 	if (!crtc->base.crc.opened)
637 		return;
638 
639 	if (get_new_crc_ctl_reg(dev_priv, pipe, &pipe_crc->source, &val) < 0)
640 		return;
641 
642 	/* Don't need pipe_crc->lock here, IRQs are not generated. */
643 	pipe_crc->skipped = 0;
644 
645 	intel_de_write(dev_priv, PIPE_CRC_CTL(pipe), val);
646 	intel_de_posting_read(dev_priv, PIPE_CRC_CTL(pipe));
647 }
648 
649 void intel_crtc_disable_pipe_crc(struct intel_crtc *crtc)
650 {
651 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
652 	struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
653 	enum pipe pipe = crtc->pipe;
654 
655 	/* Swallow crc's until we stop generating them. */
656 	spin_lock_irq(&pipe_crc->lock);
657 	pipe_crc->skipped = INT_MIN;
658 	spin_unlock_irq(&pipe_crc->lock);
659 
660 	intel_de_write(dev_priv, PIPE_CRC_CTL(pipe), 0);
661 	intel_de_posting_read(dev_priv, PIPE_CRC_CTL(pipe));
662 	intel_synchronize_irq(dev_priv);
663 }
664