1 /*
2  * Copyright 2006 Dave Airlie <airlied@linux.ie>
3  * Copyright © 2006-2009 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *	Eric Anholt <eric@anholt.net>
26  *	Jesse Barnes <jesse.barnes@intel.com>
27  */
28 
29 #include <linux/delay.h>
30 #include <linux/hdmi.h>
31 #include <linux/i2c.h>
32 #include <linux/slab.h>
33 #include <linux/string_helpers.h>
34 
35 #include <drm/display/drm_hdcp_helper.h>
36 #include <drm/display/drm_hdmi_helper.h>
37 #include <drm/display/drm_scdc_helper.h>
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_crtc.h>
40 #include <drm/drm_edid.h>
41 #include <drm/intel_lpe_audio.h>
42 
43 #include "i915_debugfs.h"
44 #include "i915_drv.h"
45 #include "i915_reg.h"
46 #include "intel_atomic.h"
47 #include "intel_audio.h"
48 #include "intel_connector.h"
49 #include "intel_ddi.h"
50 #include "intel_de.h"
51 #include "intel_display_types.h"
52 #include "intel_dp.h"
53 #include "intel_gmbus.h"
54 #include "intel_hdcp.h"
55 #include "intel_hdcp_regs.h"
56 #include "intel_hdmi.h"
57 #include "intel_lspcon.h"
58 #include "intel_panel.h"
59 #include "intel_snps_phy.h"
60 
61 static struct drm_i915_private *intel_hdmi_to_i915(struct intel_hdmi *intel_hdmi)
62 {
63 	return to_i915(hdmi_to_dig_port(intel_hdmi)->base.base.dev);
64 }
65 
66 static void
67 assert_hdmi_port_disabled(struct intel_hdmi *intel_hdmi)
68 {
69 	struct drm_i915_private *dev_priv = intel_hdmi_to_i915(intel_hdmi);
70 	u32 enabled_bits;
71 
72 	enabled_bits = HAS_DDI(dev_priv) ? DDI_BUF_CTL_ENABLE : SDVO_ENABLE;
73 
74 	drm_WARN(&dev_priv->drm,
75 		 intel_de_read(dev_priv, intel_hdmi->hdmi_reg) & enabled_bits,
76 		 "HDMI port enabled, expecting disabled\n");
77 }
78 
79 static void
80 assert_hdmi_transcoder_func_disabled(struct drm_i915_private *dev_priv,
81 				     enum transcoder cpu_transcoder)
82 {
83 	drm_WARN(&dev_priv->drm,
84 		 intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder)) &
85 		 TRANS_DDI_FUNC_ENABLE,
86 		 "HDMI transcoder function enabled, expecting disabled\n");
87 }
88 
89 static u32 g4x_infoframe_index(unsigned int type)
90 {
91 	switch (type) {
92 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
93 		return VIDEO_DIP_SELECT_GAMUT;
94 	case HDMI_INFOFRAME_TYPE_AVI:
95 		return VIDEO_DIP_SELECT_AVI;
96 	case HDMI_INFOFRAME_TYPE_SPD:
97 		return VIDEO_DIP_SELECT_SPD;
98 	case HDMI_INFOFRAME_TYPE_VENDOR:
99 		return VIDEO_DIP_SELECT_VENDOR;
100 	default:
101 		MISSING_CASE(type);
102 		return 0;
103 	}
104 }
105 
106 static u32 g4x_infoframe_enable(unsigned int type)
107 {
108 	switch (type) {
109 	case HDMI_PACKET_TYPE_GENERAL_CONTROL:
110 		return VIDEO_DIP_ENABLE_GCP;
111 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
112 		return VIDEO_DIP_ENABLE_GAMUT;
113 	case DP_SDP_VSC:
114 		return 0;
115 	case HDMI_INFOFRAME_TYPE_AVI:
116 		return VIDEO_DIP_ENABLE_AVI;
117 	case HDMI_INFOFRAME_TYPE_SPD:
118 		return VIDEO_DIP_ENABLE_SPD;
119 	case HDMI_INFOFRAME_TYPE_VENDOR:
120 		return VIDEO_DIP_ENABLE_VENDOR;
121 	case HDMI_INFOFRAME_TYPE_DRM:
122 		return 0;
123 	default:
124 		MISSING_CASE(type);
125 		return 0;
126 	}
127 }
128 
129 static u32 hsw_infoframe_enable(unsigned int type)
130 {
131 	switch (type) {
132 	case HDMI_PACKET_TYPE_GENERAL_CONTROL:
133 		return VIDEO_DIP_ENABLE_GCP_HSW;
134 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
135 		return VIDEO_DIP_ENABLE_GMP_HSW;
136 	case DP_SDP_VSC:
137 		return VIDEO_DIP_ENABLE_VSC_HSW;
138 	case DP_SDP_PPS:
139 		return VDIP_ENABLE_PPS;
140 	case HDMI_INFOFRAME_TYPE_AVI:
141 		return VIDEO_DIP_ENABLE_AVI_HSW;
142 	case HDMI_INFOFRAME_TYPE_SPD:
143 		return VIDEO_DIP_ENABLE_SPD_HSW;
144 	case HDMI_INFOFRAME_TYPE_VENDOR:
145 		return VIDEO_DIP_ENABLE_VS_HSW;
146 	case HDMI_INFOFRAME_TYPE_DRM:
147 		return VIDEO_DIP_ENABLE_DRM_GLK;
148 	default:
149 		MISSING_CASE(type);
150 		return 0;
151 	}
152 }
153 
154 static i915_reg_t
155 hsw_dip_data_reg(struct drm_i915_private *dev_priv,
156 		 enum transcoder cpu_transcoder,
157 		 unsigned int type,
158 		 int i)
159 {
160 	switch (type) {
161 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
162 		return HSW_TVIDEO_DIP_GMP_DATA(cpu_transcoder, i);
163 	case DP_SDP_VSC:
164 		return HSW_TVIDEO_DIP_VSC_DATA(cpu_transcoder, i);
165 	case DP_SDP_PPS:
166 		return ICL_VIDEO_DIP_PPS_DATA(cpu_transcoder, i);
167 	case HDMI_INFOFRAME_TYPE_AVI:
168 		return HSW_TVIDEO_DIP_AVI_DATA(cpu_transcoder, i);
169 	case HDMI_INFOFRAME_TYPE_SPD:
170 		return HSW_TVIDEO_DIP_SPD_DATA(cpu_transcoder, i);
171 	case HDMI_INFOFRAME_TYPE_VENDOR:
172 		return HSW_TVIDEO_DIP_VS_DATA(cpu_transcoder, i);
173 	case HDMI_INFOFRAME_TYPE_DRM:
174 		return GLK_TVIDEO_DIP_DRM_DATA(cpu_transcoder, i);
175 	default:
176 		MISSING_CASE(type);
177 		return INVALID_MMIO_REG;
178 	}
179 }
180 
181 static int hsw_dip_data_size(struct drm_i915_private *dev_priv,
182 			     unsigned int type)
183 {
184 	switch (type) {
185 	case DP_SDP_VSC:
186 		return VIDEO_DIP_VSC_DATA_SIZE;
187 	case DP_SDP_PPS:
188 		return VIDEO_DIP_PPS_DATA_SIZE;
189 	case HDMI_PACKET_TYPE_GAMUT_METADATA:
190 		if (DISPLAY_VER(dev_priv) >= 11)
191 			return VIDEO_DIP_GMP_DATA_SIZE;
192 		else
193 			return VIDEO_DIP_DATA_SIZE;
194 	default:
195 		return VIDEO_DIP_DATA_SIZE;
196 	}
197 }
198 
199 static void g4x_write_infoframe(struct intel_encoder *encoder,
200 				const struct intel_crtc_state *crtc_state,
201 				unsigned int type,
202 				const void *frame, ssize_t len)
203 {
204 	const u32 *data = frame;
205 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
206 	u32 val = intel_de_read(dev_priv, VIDEO_DIP_CTL);
207 	int i;
208 
209 	drm_WARN(&dev_priv->drm, !(val & VIDEO_DIP_ENABLE),
210 		 "Writing DIP with CTL reg disabled\n");
211 
212 	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
213 	val |= g4x_infoframe_index(type);
214 
215 	val &= ~g4x_infoframe_enable(type);
216 
217 	intel_de_write(dev_priv, VIDEO_DIP_CTL, val);
218 
219 	for (i = 0; i < len; i += 4) {
220 		intel_de_write(dev_priv, VIDEO_DIP_DATA, *data);
221 		data++;
222 	}
223 	/* Write every possible data byte to force correct ECC calculation. */
224 	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
225 		intel_de_write(dev_priv, VIDEO_DIP_DATA, 0);
226 
227 	val |= g4x_infoframe_enable(type);
228 	val &= ~VIDEO_DIP_FREQ_MASK;
229 	val |= VIDEO_DIP_FREQ_VSYNC;
230 
231 	intel_de_write(dev_priv, VIDEO_DIP_CTL, val);
232 	intel_de_posting_read(dev_priv, VIDEO_DIP_CTL);
233 }
234 
235 static void g4x_read_infoframe(struct intel_encoder *encoder,
236 			       const struct intel_crtc_state *crtc_state,
237 			       unsigned int type,
238 			       void *frame, ssize_t len)
239 {
240 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
241 	u32 *data = frame;
242 	int i;
243 
244 	intel_de_rmw(dev_priv, VIDEO_DIP_CTL,
245 		     VIDEO_DIP_SELECT_MASK | 0xf, g4x_infoframe_index(type));
246 
247 	for (i = 0; i < len; i += 4)
248 		*data++ = intel_de_read(dev_priv, VIDEO_DIP_DATA);
249 }
250 
251 static u32 g4x_infoframes_enabled(struct intel_encoder *encoder,
252 				  const struct intel_crtc_state *pipe_config)
253 {
254 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
255 	u32 val = intel_de_read(dev_priv, VIDEO_DIP_CTL);
256 
257 	if ((val & VIDEO_DIP_ENABLE) == 0)
258 		return 0;
259 
260 	if ((val & VIDEO_DIP_PORT_MASK) != VIDEO_DIP_PORT(encoder->port))
261 		return 0;
262 
263 	return val & (VIDEO_DIP_ENABLE_AVI |
264 		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_SPD);
265 }
266 
267 static void ibx_write_infoframe(struct intel_encoder *encoder,
268 				const struct intel_crtc_state *crtc_state,
269 				unsigned int type,
270 				const void *frame, ssize_t len)
271 {
272 	const u32 *data = frame;
273 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
274 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
275 	i915_reg_t reg = TVIDEO_DIP_CTL(crtc->pipe);
276 	u32 val = intel_de_read(dev_priv, reg);
277 	int i;
278 
279 	drm_WARN(&dev_priv->drm, !(val & VIDEO_DIP_ENABLE),
280 		 "Writing DIP with CTL reg disabled\n");
281 
282 	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
283 	val |= g4x_infoframe_index(type);
284 
285 	val &= ~g4x_infoframe_enable(type);
286 
287 	intel_de_write(dev_priv, reg, val);
288 
289 	for (i = 0; i < len; i += 4) {
290 		intel_de_write(dev_priv, TVIDEO_DIP_DATA(crtc->pipe),
291 			       *data);
292 		data++;
293 	}
294 	/* Write every possible data byte to force correct ECC calculation. */
295 	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
296 		intel_de_write(dev_priv, TVIDEO_DIP_DATA(crtc->pipe), 0);
297 
298 	val |= g4x_infoframe_enable(type);
299 	val &= ~VIDEO_DIP_FREQ_MASK;
300 	val |= VIDEO_DIP_FREQ_VSYNC;
301 
302 	intel_de_write(dev_priv, reg, val);
303 	intel_de_posting_read(dev_priv, reg);
304 }
305 
306 static void ibx_read_infoframe(struct intel_encoder *encoder,
307 			       const struct intel_crtc_state *crtc_state,
308 			       unsigned int type,
309 			       void *frame, ssize_t len)
310 {
311 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
312 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
313 	u32 *data = frame;
314 	int i;
315 
316 	intel_de_rmw(dev_priv, TVIDEO_DIP_CTL(crtc->pipe),
317 		     VIDEO_DIP_SELECT_MASK | 0xf, g4x_infoframe_index(type));
318 
319 	for (i = 0; i < len; i += 4)
320 		*data++ = intel_de_read(dev_priv, TVIDEO_DIP_DATA(crtc->pipe));
321 }
322 
323 static u32 ibx_infoframes_enabled(struct intel_encoder *encoder,
324 				  const struct intel_crtc_state *pipe_config)
325 {
326 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
327 	enum pipe pipe = to_intel_crtc(pipe_config->uapi.crtc)->pipe;
328 	i915_reg_t reg = TVIDEO_DIP_CTL(pipe);
329 	u32 val = intel_de_read(dev_priv, reg);
330 
331 	if ((val & VIDEO_DIP_ENABLE) == 0)
332 		return 0;
333 
334 	if ((val & VIDEO_DIP_PORT_MASK) != VIDEO_DIP_PORT(encoder->port))
335 		return 0;
336 
337 	return val & (VIDEO_DIP_ENABLE_AVI |
338 		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
339 		      VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
340 }
341 
342 static void cpt_write_infoframe(struct intel_encoder *encoder,
343 				const struct intel_crtc_state *crtc_state,
344 				unsigned int type,
345 				const void *frame, ssize_t len)
346 {
347 	const u32 *data = frame;
348 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
349 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
350 	i915_reg_t reg = TVIDEO_DIP_CTL(crtc->pipe);
351 	u32 val = intel_de_read(dev_priv, reg);
352 	int i;
353 
354 	drm_WARN(&dev_priv->drm, !(val & VIDEO_DIP_ENABLE),
355 		 "Writing DIP with CTL reg disabled\n");
356 
357 	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
358 	val |= g4x_infoframe_index(type);
359 
360 	/* The DIP control register spec says that we need to update the AVI
361 	 * infoframe without clearing its enable bit */
362 	if (type != HDMI_INFOFRAME_TYPE_AVI)
363 		val &= ~g4x_infoframe_enable(type);
364 
365 	intel_de_write(dev_priv, reg, val);
366 
367 	for (i = 0; i < len; i += 4) {
368 		intel_de_write(dev_priv, TVIDEO_DIP_DATA(crtc->pipe),
369 			       *data);
370 		data++;
371 	}
372 	/* Write every possible data byte to force correct ECC calculation. */
373 	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
374 		intel_de_write(dev_priv, TVIDEO_DIP_DATA(crtc->pipe), 0);
375 
376 	val |= g4x_infoframe_enable(type);
377 	val &= ~VIDEO_DIP_FREQ_MASK;
378 	val |= VIDEO_DIP_FREQ_VSYNC;
379 
380 	intel_de_write(dev_priv, reg, val);
381 	intel_de_posting_read(dev_priv, reg);
382 }
383 
384 static void cpt_read_infoframe(struct intel_encoder *encoder,
385 			       const struct intel_crtc_state *crtc_state,
386 			       unsigned int type,
387 			       void *frame, ssize_t len)
388 {
389 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
390 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
391 	u32 *data = frame;
392 	int i;
393 
394 	intel_de_rmw(dev_priv, TVIDEO_DIP_CTL(crtc->pipe),
395 		     VIDEO_DIP_SELECT_MASK | 0xf, g4x_infoframe_index(type));
396 
397 	for (i = 0; i < len; i += 4)
398 		*data++ = intel_de_read(dev_priv, TVIDEO_DIP_DATA(crtc->pipe));
399 }
400 
401 static u32 cpt_infoframes_enabled(struct intel_encoder *encoder,
402 				  const struct intel_crtc_state *pipe_config)
403 {
404 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
405 	enum pipe pipe = to_intel_crtc(pipe_config->uapi.crtc)->pipe;
406 	u32 val = intel_de_read(dev_priv, TVIDEO_DIP_CTL(pipe));
407 
408 	if ((val & VIDEO_DIP_ENABLE) == 0)
409 		return 0;
410 
411 	return val & (VIDEO_DIP_ENABLE_AVI |
412 		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
413 		      VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
414 }
415 
416 static void vlv_write_infoframe(struct intel_encoder *encoder,
417 				const struct intel_crtc_state *crtc_state,
418 				unsigned int type,
419 				const void *frame, ssize_t len)
420 {
421 	const u32 *data = frame;
422 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
423 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
424 	i915_reg_t reg = VLV_TVIDEO_DIP_CTL(crtc->pipe);
425 	u32 val = intel_de_read(dev_priv, reg);
426 	int i;
427 
428 	drm_WARN(&dev_priv->drm, !(val & VIDEO_DIP_ENABLE),
429 		 "Writing DIP with CTL reg disabled\n");
430 
431 	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
432 	val |= g4x_infoframe_index(type);
433 
434 	val &= ~g4x_infoframe_enable(type);
435 
436 	intel_de_write(dev_priv, reg, val);
437 
438 	for (i = 0; i < len; i += 4) {
439 		intel_de_write(dev_priv,
440 			       VLV_TVIDEO_DIP_DATA(crtc->pipe), *data);
441 		data++;
442 	}
443 	/* Write every possible data byte to force correct ECC calculation. */
444 	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
445 		intel_de_write(dev_priv,
446 			       VLV_TVIDEO_DIP_DATA(crtc->pipe), 0);
447 
448 	val |= g4x_infoframe_enable(type);
449 	val &= ~VIDEO_DIP_FREQ_MASK;
450 	val |= VIDEO_DIP_FREQ_VSYNC;
451 
452 	intel_de_write(dev_priv, reg, val);
453 	intel_de_posting_read(dev_priv, reg);
454 }
455 
456 static void vlv_read_infoframe(struct intel_encoder *encoder,
457 			       const struct intel_crtc_state *crtc_state,
458 			       unsigned int type,
459 			       void *frame, ssize_t len)
460 {
461 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
462 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
463 	u32 *data = frame;
464 	int i;
465 
466 	intel_de_rmw(dev_priv, VLV_TVIDEO_DIP_CTL(crtc->pipe),
467 		     VIDEO_DIP_SELECT_MASK | 0xf, g4x_infoframe_index(type));
468 
469 	for (i = 0; i < len; i += 4)
470 		*data++ = intel_de_read(dev_priv,
471 				        VLV_TVIDEO_DIP_DATA(crtc->pipe));
472 }
473 
474 static u32 vlv_infoframes_enabled(struct intel_encoder *encoder,
475 				  const struct intel_crtc_state *pipe_config)
476 {
477 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
478 	enum pipe pipe = to_intel_crtc(pipe_config->uapi.crtc)->pipe;
479 	u32 val = intel_de_read(dev_priv, VLV_TVIDEO_DIP_CTL(pipe));
480 
481 	if ((val & VIDEO_DIP_ENABLE) == 0)
482 		return 0;
483 
484 	if ((val & VIDEO_DIP_PORT_MASK) != VIDEO_DIP_PORT(encoder->port))
485 		return 0;
486 
487 	return val & (VIDEO_DIP_ENABLE_AVI |
488 		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
489 		      VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
490 }
491 
492 void hsw_write_infoframe(struct intel_encoder *encoder,
493 			 const struct intel_crtc_state *crtc_state,
494 			 unsigned int type,
495 			 const void *frame, ssize_t len)
496 {
497 	const u32 *data = frame;
498 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
499 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
500 	i915_reg_t ctl_reg = HSW_TVIDEO_DIP_CTL(cpu_transcoder);
501 	int data_size;
502 	int i;
503 	u32 val = intel_de_read(dev_priv, ctl_reg);
504 
505 	data_size = hsw_dip_data_size(dev_priv, type);
506 
507 	drm_WARN_ON(&dev_priv->drm, len > data_size);
508 
509 	val &= ~hsw_infoframe_enable(type);
510 	intel_de_write(dev_priv, ctl_reg, val);
511 
512 	for (i = 0; i < len; i += 4) {
513 		intel_de_write(dev_priv,
514 			       hsw_dip_data_reg(dev_priv, cpu_transcoder, type, i >> 2),
515 			       *data);
516 		data++;
517 	}
518 	/* Write every possible data byte to force correct ECC calculation. */
519 	for (; i < data_size; i += 4)
520 		intel_de_write(dev_priv,
521 			       hsw_dip_data_reg(dev_priv, cpu_transcoder, type, i >> 2),
522 			       0);
523 
524 	/* Wa_14013475917 */
525 	if (IS_DISPLAY_VER(dev_priv, 13, 14) && crtc_state->has_psr && type == DP_SDP_VSC)
526 		return;
527 
528 	val |= hsw_infoframe_enable(type);
529 	intel_de_write(dev_priv, ctl_reg, val);
530 	intel_de_posting_read(dev_priv, ctl_reg);
531 }
532 
533 void hsw_read_infoframe(struct intel_encoder *encoder,
534 			const struct intel_crtc_state *crtc_state,
535 			unsigned int type, void *frame, ssize_t len)
536 {
537 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
538 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
539 	u32 *data = frame;
540 	int i;
541 
542 	for (i = 0; i < len; i += 4)
543 		*data++ = intel_de_read(dev_priv,
544 				        hsw_dip_data_reg(dev_priv, cpu_transcoder, type, i >> 2));
545 }
546 
547 static u32 hsw_infoframes_enabled(struct intel_encoder *encoder,
548 				  const struct intel_crtc_state *pipe_config)
549 {
550 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
551 	u32 val = intel_de_read(dev_priv,
552 				HSW_TVIDEO_DIP_CTL(pipe_config->cpu_transcoder));
553 	u32 mask;
554 
555 	mask = (VIDEO_DIP_ENABLE_VSC_HSW | VIDEO_DIP_ENABLE_AVI_HSW |
556 		VIDEO_DIP_ENABLE_GCP_HSW | VIDEO_DIP_ENABLE_VS_HSW |
557 		VIDEO_DIP_ENABLE_GMP_HSW | VIDEO_DIP_ENABLE_SPD_HSW);
558 
559 	if (DISPLAY_VER(dev_priv) >= 10)
560 		mask |= VIDEO_DIP_ENABLE_DRM_GLK;
561 
562 	return val & mask;
563 }
564 
565 static const u8 infoframe_type_to_idx[] = {
566 	HDMI_PACKET_TYPE_GENERAL_CONTROL,
567 	HDMI_PACKET_TYPE_GAMUT_METADATA,
568 	DP_SDP_VSC,
569 	HDMI_INFOFRAME_TYPE_AVI,
570 	HDMI_INFOFRAME_TYPE_SPD,
571 	HDMI_INFOFRAME_TYPE_VENDOR,
572 	HDMI_INFOFRAME_TYPE_DRM,
573 };
574 
575 u32 intel_hdmi_infoframe_enable(unsigned int type)
576 {
577 	int i;
578 
579 	for (i = 0; i < ARRAY_SIZE(infoframe_type_to_idx); i++) {
580 		if (infoframe_type_to_idx[i] == type)
581 			return BIT(i);
582 	}
583 
584 	return 0;
585 }
586 
587 u32 intel_hdmi_infoframes_enabled(struct intel_encoder *encoder,
588 				  const struct intel_crtc_state *crtc_state)
589 {
590 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
591 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
592 	u32 val, ret = 0;
593 	int i;
594 
595 	val = dig_port->infoframes_enabled(encoder, crtc_state);
596 
597 	/* map from hardware bits to dip idx */
598 	for (i = 0; i < ARRAY_SIZE(infoframe_type_to_idx); i++) {
599 		unsigned int type = infoframe_type_to_idx[i];
600 
601 		if (HAS_DDI(dev_priv)) {
602 			if (val & hsw_infoframe_enable(type))
603 				ret |= BIT(i);
604 		} else {
605 			if (val & g4x_infoframe_enable(type))
606 				ret |= BIT(i);
607 		}
608 	}
609 
610 	return ret;
611 }
612 
613 /*
614  * The data we write to the DIP data buffer registers is 1 byte bigger than the
615  * HDMI infoframe size because of an ECC/reserved byte at position 3 (starting
616  * at 0). It's also a byte used by DisplayPort so the same DIP registers can be
617  * used for both technologies.
618  *
619  * DW0: Reserved/ECC/DP | HB2 | HB1 | HB0
620  * DW1:       DB3       | DB2 | DB1 | DB0
621  * DW2:       DB7       | DB6 | DB5 | DB4
622  * DW3: ...
623  *
624  * (HB is Header Byte, DB is Data Byte)
625  *
626  * The hdmi pack() functions don't know about that hardware specific hole so we
627  * trick them by giving an offset into the buffer and moving back the header
628  * bytes by one.
629  */
630 static void intel_write_infoframe(struct intel_encoder *encoder,
631 				  const struct intel_crtc_state *crtc_state,
632 				  enum hdmi_infoframe_type type,
633 				  const union hdmi_infoframe *frame)
634 {
635 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
636 	u8 buffer[VIDEO_DIP_DATA_SIZE];
637 	ssize_t len;
638 
639 	if ((crtc_state->infoframes.enable &
640 	     intel_hdmi_infoframe_enable(type)) == 0)
641 		return;
642 
643 	if (drm_WARN_ON(encoder->base.dev, frame->any.type != type))
644 		return;
645 
646 	/* see comment above for the reason for this offset */
647 	len = hdmi_infoframe_pack_only(frame, buffer + 1, sizeof(buffer) - 1);
648 	if (drm_WARN_ON(encoder->base.dev, len < 0))
649 		return;
650 
651 	/* Insert the 'hole' (see big comment above) at position 3 */
652 	memmove(&buffer[0], &buffer[1], 3);
653 	buffer[3] = 0;
654 	len++;
655 
656 	dig_port->write_infoframe(encoder, crtc_state, type, buffer, len);
657 }
658 
659 void intel_read_infoframe(struct intel_encoder *encoder,
660 			  const struct intel_crtc_state *crtc_state,
661 			  enum hdmi_infoframe_type type,
662 			  union hdmi_infoframe *frame)
663 {
664 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
665 	u8 buffer[VIDEO_DIP_DATA_SIZE];
666 	int ret;
667 
668 	if ((crtc_state->infoframes.enable &
669 	     intel_hdmi_infoframe_enable(type)) == 0)
670 		return;
671 
672 	dig_port->read_infoframe(encoder, crtc_state,
673 				       type, buffer, sizeof(buffer));
674 
675 	/* Fill the 'hole' (see big comment above) at position 3 */
676 	memmove(&buffer[1], &buffer[0], 3);
677 
678 	/* see comment above for the reason for this offset */
679 	ret = hdmi_infoframe_unpack(frame, buffer + 1, sizeof(buffer) - 1);
680 	if (ret) {
681 		drm_dbg_kms(encoder->base.dev,
682 			    "Failed to unpack infoframe type 0x%02x\n", type);
683 		return;
684 	}
685 
686 	if (frame->any.type != type)
687 		drm_dbg_kms(encoder->base.dev,
688 			    "Found the wrong infoframe type 0x%x (expected 0x%02x)\n",
689 			    frame->any.type, type);
690 }
691 
692 static bool
693 intel_hdmi_compute_avi_infoframe(struct intel_encoder *encoder,
694 				 struct intel_crtc_state *crtc_state,
695 				 struct drm_connector_state *conn_state)
696 {
697 	struct hdmi_avi_infoframe *frame = &crtc_state->infoframes.avi.avi;
698 	const struct drm_display_mode *adjusted_mode =
699 		&crtc_state->hw.adjusted_mode;
700 	struct drm_connector *connector = conn_state->connector;
701 	int ret;
702 
703 	if (!crtc_state->has_infoframe)
704 		return true;
705 
706 	crtc_state->infoframes.enable |=
707 		intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI);
708 
709 	ret = drm_hdmi_avi_infoframe_from_display_mode(frame, connector,
710 						       adjusted_mode);
711 	if (ret)
712 		return false;
713 
714 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
715 		frame->colorspace = HDMI_COLORSPACE_YUV420;
716 	else if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
717 		frame->colorspace = HDMI_COLORSPACE_YUV444;
718 	else
719 		frame->colorspace = HDMI_COLORSPACE_RGB;
720 
721 	drm_hdmi_avi_infoframe_colorimetry(frame, conn_state);
722 
723 	/* nonsense combination */
724 	drm_WARN_ON(encoder->base.dev, crtc_state->limited_color_range &&
725 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
726 
727 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_RGB) {
728 		drm_hdmi_avi_infoframe_quant_range(frame, connector,
729 						   adjusted_mode,
730 						   crtc_state->limited_color_range ?
731 						   HDMI_QUANTIZATION_RANGE_LIMITED :
732 						   HDMI_QUANTIZATION_RANGE_FULL);
733 	} else {
734 		frame->quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
735 		frame->ycc_quantization_range = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
736 	}
737 
738 	drm_hdmi_avi_infoframe_content_type(frame, conn_state);
739 
740 	/* TODO: handle pixel repetition for YCBCR420 outputs */
741 
742 	ret = hdmi_avi_infoframe_check(frame);
743 	if (drm_WARN_ON(encoder->base.dev, ret))
744 		return false;
745 
746 	return true;
747 }
748 
749 static bool
750 intel_hdmi_compute_spd_infoframe(struct intel_encoder *encoder,
751 				 struct intel_crtc_state *crtc_state,
752 				 struct drm_connector_state *conn_state)
753 {
754 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
755 	struct hdmi_spd_infoframe *frame = &crtc_state->infoframes.spd.spd;
756 	int ret;
757 
758 	if (!crtc_state->has_infoframe)
759 		return true;
760 
761 	crtc_state->infoframes.enable |=
762 		intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD);
763 
764 	if (IS_DGFX(i915))
765 		ret = hdmi_spd_infoframe_init(frame, "Intel", "Discrete gfx");
766 	else
767 		ret = hdmi_spd_infoframe_init(frame, "Intel", "Integrated gfx");
768 
769 	if (drm_WARN_ON(encoder->base.dev, ret))
770 		return false;
771 
772 	frame->sdi = HDMI_SPD_SDI_PC;
773 
774 	ret = hdmi_spd_infoframe_check(frame);
775 	if (drm_WARN_ON(encoder->base.dev, ret))
776 		return false;
777 
778 	return true;
779 }
780 
781 static bool
782 intel_hdmi_compute_hdmi_infoframe(struct intel_encoder *encoder,
783 				  struct intel_crtc_state *crtc_state,
784 				  struct drm_connector_state *conn_state)
785 {
786 	struct hdmi_vendor_infoframe *frame =
787 		&crtc_state->infoframes.hdmi.vendor.hdmi;
788 	const struct drm_display_info *info =
789 		&conn_state->connector->display_info;
790 	int ret;
791 
792 	if (!crtc_state->has_infoframe || !info->has_hdmi_infoframe)
793 		return true;
794 
795 	crtc_state->infoframes.enable |=
796 		intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR);
797 
798 	ret = drm_hdmi_vendor_infoframe_from_display_mode(frame,
799 							  conn_state->connector,
800 							  &crtc_state->hw.adjusted_mode);
801 	if (drm_WARN_ON(encoder->base.dev, ret))
802 		return false;
803 
804 	ret = hdmi_vendor_infoframe_check(frame);
805 	if (drm_WARN_ON(encoder->base.dev, ret))
806 		return false;
807 
808 	return true;
809 }
810 
811 static bool
812 intel_hdmi_compute_drm_infoframe(struct intel_encoder *encoder,
813 				 struct intel_crtc_state *crtc_state,
814 				 struct drm_connector_state *conn_state)
815 {
816 	struct hdmi_drm_infoframe *frame = &crtc_state->infoframes.drm.drm;
817 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
818 	int ret;
819 
820 	if (DISPLAY_VER(dev_priv) < 10)
821 		return true;
822 
823 	if (!crtc_state->has_infoframe)
824 		return true;
825 
826 	if (!conn_state->hdr_output_metadata)
827 		return true;
828 
829 	crtc_state->infoframes.enable |=
830 		intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM);
831 
832 	ret = drm_hdmi_infoframe_set_hdr_metadata(frame, conn_state);
833 	if (ret < 0) {
834 		drm_dbg_kms(&dev_priv->drm,
835 			    "couldn't set HDR metadata in infoframe\n");
836 		return false;
837 	}
838 
839 	ret = hdmi_drm_infoframe_check(frame);
840 	if (drm_WARN_ON(&dev_priv->drm, ret))
841 		return false;
842 
843 	return true;
844 }
845 
846 static void g4x_set_infoframes(struct intel_encoder *encoder,
847 			       bool enable,
848 			       const struct intel_crtc_state *crtc_state,
849 			       const struct drm_connector_state *conn_state)
850 {
851 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
852 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
853 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
854 	i915_reg_t reg = VIDEO_DIP_CTL;
855 	u32 val = intel_de_read(dev_priv, reg);
856 	u32 port = VIDEO_DIP_PORT(encoder->port);
857 
858 	assert_hdmi_port_disabled(intel_hdmi);
859 
860 	/* If the registers were not initialized yet, they might be zeroes,
861 	 * which means we're selecting the AVI DIP and we're setting its
862 	 * frequency to once. This seems to really confuse the HW and make
863 	 * things stop working (the register spec says the AVI always needs to
864 	 * be sent every VSync). So here we avoid writing to the register more
865 	 * than we need and also explicitly select the AVI DIP and explicitly
866 	 * set its frequency to every VSync. Avoiding to write it twice seems to
867 	 * be enough to solve the problem, but being defensive shouldn't hurt us
868 	 * either. */
869 	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
870 
871 	if (!enable) {
872 		if (!(val & VIDEO_DIP_ENABLE))
873 			return;
874 		if (port != (val & VIDEO_DIP_PORT_MASK)) {
875 			drm_dbg_kms(&dev_priv->drm,
876 				    "video DIP still enabled on port %c\n",
877 				    (val & VIDEO_DIP_PORT_MASK) >> 29);
878 			return;
879 		}
880 		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
881 			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_SPD);
882 		intel_de_write(dev_priv, reg, val);
883 		intel_de_posting_read(dev_priv, reg);
884 		return;
885 	}
886 
887 	if (port != (val & VIDEO_DIP_PORT_MASK)) {
888 		if (val & VIDEO_DIP_ENABLE) {
889 			drm_dbg_kms(&dev_priv->drm,
890 				    "video DIP already enabled on port %c\n",
891 				    (val & VIDEO_DIP_PORT_MASK) >> 29);
892 			return;
893 		}
894 		val &= ~VIDEO_DIP_PORT_MASK;
895 		val |= port;
896 	}
897 
898 	val |= VIDEO_DIP_ENABLE;
899 	val &= ~(VIDEO_DIP_ENABLE_AVI |
900 		 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_SPD);
901 
902 	intel_de_write(dev_priv, reg, val);
903 	intel_de_posting_read(dev_priv, reg);
904 
905 	intel_write_infoframe(encoder, crtc_state,
906 			      HDMI_INFOFRAME_TYPE_AVI,
907 			      &crtc_state->infoframes.avi);
908 	intel_write_infoframe(encoder, crtc_state,
909 			      HDMI_INFOFRAME_TYPE_SPD,
910 			      &crtc_state->infoframes.spd);
911 	intel_write_infoframe(encoder, crtc_state,
912 			      HDMI_INFOFRAME_TYPE_VENDOR,
913 			      &crtc_state->infoframes.hdmi);
914 }
915 
916 /*
917  * Determine if default_phase=1 can be indicated in the GCP infoframe.
918  *
919  * From HDMI specification 1.4a:
920  * - The first pixel of each Video Data Period shall always have a pixel packing phase of 0
921  * - The first pixel following each Video Data Period shall have a pixel packing phase of 0
922  * - The PP bits shall be constant for all GCPs and will be equal to the last packing phase
923  * - The first pixel following every transition of HSYNC or VSYNC shall have a pixel packing
924  *   phase of 0
925  */
926 static bool gcp_default_phase_possible(int pipe_bpp,
927 				       const struct drm_display_mode *mode)
928 {
929 	unsigned int pixels_per_group;
930 
931 	switch (pipe_bpp) {
932 	case 30:
933 		/* 4 pixels in 5 clocks */
934 		pixels_per_group = 4;
935 		break;
936 	case 36:
937 		/* 2 pixels in 3 clocks */
938 		pixels_per_group = 2;
939 		break;
940 	case 48:
941 		/* 1 pixel in 2 clocks */
942 		pixels_per_group = 1;
943 		break;
944 	default:
945 		/* phase information not relevant for 8bpc */
946 		return false;
947 	}
948 
949 	return mode->crtc_hdisplay % pixels_per_group == 0 &&
950 		mode->crtc_htotal % pixels_per_group == 0 &&
951 		mode->crtc_hblank_start % pixels_per_group == 0 &&
952 		mode->crtc_hblank_end % pixels_per_group == 0 &&
953 		mode->crtc_hsync_start % pixels_per_group == 0 &&
954 		mode->crtc_hsync_end % pixels_per_group == 0 &&
955 		((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0 ||
956 		 mode->crtc_htotal/2 % pixels_per_group == 0);
957 }
958 
959 static bool intel_hdmi_set_gcp_infoframe(struct intel_encoder *encoder,
960 					 const struct intel_crtc_state *crtc_state,
961 					 const struct drm_connector_state *conn_state)
962 {
963 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
964 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
965 	i915_reg_t reg;
966 
967 	if ((crtc_state->infoframes.enable &
968 	     intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL)) == 0)
969 		return false;
970 
971 	if (HAS_DDI(dev_priv))
972 		reg = HSW_TVIDEO_DIP_GCP(crtc_state->cpu_transcoder);
973 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
974 		reg = VLV_TVIDEO_DIP_GCP(crtc->pipe);
975 	else if (HAS_PCH_SPLIT(dev_priv))
976 		reg = TVIDEO_DIP_GCP(crtc->pipe);
977 	else
978 		return false;
979 
980 	intel_de_write(dev_priv, reg, crtc_state->infoframes.gcp);
981 
982 	return true;
983 }
984 
985 void intel_hdmi_read_gcp_infoframe(struct intel_encoder *encoder,
986 				   struct intel_crtc_state *crtc_state)
987 {
988 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
989 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
990 	i915_reg_t reg;
991 
992 	if ((crtc_state->infoframes.enable &
993 	     intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL)) == 0)
994 		return;
995 
996 	if (HAS_DDI(dev_priv))
997 		reg = HSW_TVIDEO_DIP_GCP(crtc_state->cpu_transcoder);
998 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
999 		reg = VLV_TVIDEO_DIP_GCP(crtc->pipe);
1000 	else if (HAS_PCH_SPLIT(dev_priv))
1001 		reg = TVIDEO_DIP_GCP(crtc->pipe);
1002 	else
1003 		return;
1004 
1005 	crtc_state->infoframes.gcp = intel_de_read(dev_priv, reg);
1006 }
1007 
1008 static void intel_hdmi_compute_gcp_infoframe(struct intel_encoder *encoder,
1009 					     struct intel_crtc_state *crtc_state,
1010 					     struct drm_connector_state *conn_state)
1011 {
1012 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1013 
1014 	if (IS_G4X(dev_priv) || !crtc_state->has_infoframe)
1015 		return;
1016 
1017 	crtc_state->infoframes.enable |=
1018 		intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL);
1019 
1020 	/* Indicate color indication for deep color mode */
1021 	if (crtc_state->pipe_bpp > 24)
1022 		crtc_state->infoframes.gcp |= GCP_COLOR_INDICATION;
1023 
1024 	/* Enable default_phase whenever the display mode is suitably aligned */
1025 	if (gcp_default_phase_possible(crtc_state->pipe_bpp,
1026 				       &crtc_state->hw.adjusted_mode))
1027 		crtc_state->infoframes.gcp |= GCP_DEFAULT_PHASE_ENABLE;
1028 }
1029 
1030 static void ibx_set_infoframes(struct intel_encoder *encoder,
1031 			       bool enable,
1032 			       const struct intel_crtc_state *crtc_state,
1033 			       const struct drm_connector_state *conn_state)
1034 {
1035 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1036 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1037 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1038 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
1039 	i915_reg_t reg = TVIDEO_DIP_CTL(crtc->pipe);
1040 	u32 val = intel_de_read(dev_priv, reg);
1041 	u32 port = VIDEO_DIP_PORT(encoder->port);
1042 
1043 	assert_hdmi_port_disabled(intel_hdmi);
1044 
1045 	/* See the big comment in g4x_set_infoframes() */
1046 	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
1047 
1048 	if (!enable) {
1049 		if (!(val & VIDEO_DIP_ENABLE))
1050 			return;
1051 		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
1052 			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
1053 			 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
1054 		intel_de_write(dev_priv, reg, val);
1055 		intel_de_posting_read(dev_priv, reg);
1056 		return;
1057 	}
1058 
1059 	if (port != (val & VIDEO_DIP_PORT_MASK)) {
1060 		drm_WARN(&dev_priv->drm, val & VIDEO_DIP_ENABLE,
1061 			 "DIP already enabled on port %c\n",
1062 			 (val & VIDEO_DIP_PORT_MASK) >> 29);
1063 		val &= ~VIDEO_DIP_PORT_MASK;
1064 		val |= port;
1065 	}
1066 
1067 	val |= VIDEO_DIP_ENABLE;
1068 	val &= ~(VIDEO_DIP_ENABLE_AVI |
1069 		 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
1070 		 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
1071 
1072 	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
1073 		val |= VIDEO_DIP_ENABLE_GCP;
1074 
1075 	intel_de_write(dev_priv, reg, val);
1076 	intel_de_posting_read(dev_priv, reg);
1077 
1078 	intel_write_infoframe(encoder, crtc_state,
1079 			      HDMI_INFOFRAME_TYPE_AVI,
1080 			      &crtc_state->infoframes.avi);
1081 	intel_write_infoframe(encoder, crtc_state,
1082 			      HDMI_INFOFRAME_TYPE_SPD,
1083 			      &crtc_state->infoframes.spd);
1084 	intel_write_infoframe(encoder, crtc_state,
1085 			      HDMI_INFOFRAME_TYPE_VENDOR,
1086 			      &crtc_state->infoframes.hdmi);
1087 }
1088 
1089 static void cpt_set_infoframes(struct intel_encoder *encoder,
1090 			       bool enable,
1091 			       const struct intel_crtc_state *crtc_state,
1092 			       const struct drm_connector_state *conn_state)
1093 {
1094 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1095 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1096 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
1097 	i915_reg_t reg = TVIDEO_DIP_CTL(crtc->pipe);
1098 	u32 val = intel_de_read(dev_priv, reg);
1099 
1100 	assert_hdmi_port_disabled(intel_hdmi);
1101 
1102 	/* See the big comment in g4x_set_infoframes() */
1103 	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
1104 
1105 	if (!enable) {
1106 		if (!(val & VIDEO_DIP_ENABLE))
1107 			return;
1108 		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
1109 			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
1110 			 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
1111 		intel_de_write(dev_priv, reg, val);
1112 		intel_de_posting_read(dev_priv, reg);
1113 		return;
1114 	}
1115 
1116 	/* Set both together, unset both together: see the spec. */
1117 	val |= VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI;
1118 	val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
1119 		 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
1120 
1121 	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
1122 		val |= VIDEO_DIP_ENABLE_GCP;
1123 
1124 	intel_de_write(dev_priv, reg, val);
1125 	intel_de_posting_read(dev_priv, reg);
1126 
1127 	intel_write_infoframe(encoder, crtc_state,
1128 			      HDMI_INFOFRAME_TYPE_AVI,
1129 			      &crtc_state->infoframes.avi);
1130 	intel_write_infoframe(encoder, crtc_state,
1131 			      HDMI_INFOFRAME_TYPE_SPD,
1132 			      &crtc_state->infoframes.spd);
1133 	intel_write_infoframe(encoder, crtc_state,
1134 			      HDMI_INFOFRAME_TYPE_VENDOR,
1135 			      &crtc_state->infoframes.hdmi);
1136 }
1137 
1138 static void vlv_set_infoframes(struct intel_encoder *encoder,
1139 			       bool enable,
1140 			       const struct intel_crtc_state *crtc_state,
1141 			       const struct drm_connector_state *conn_state)
1142 {
1143 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1144 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1145 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
1146 	i915_reg_t reg = VLV_TVIDEO_DIP_CTL(crtc->pipe);
1147 	u32 val = intel_de_read(dev_priv, reg);
1148 	u32 port = VIDEO_DIP_PORT(encoder->port);
1149 
1150 	assert_hdmi_port_disabled(intel_hdmi);
1151 
1152 	/* See the big comment in g4x_set_infoframes() */
1153 	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
1154 
1155 	if (!enable) {
1156 		if (!(val & VIDEO_DIP_ENABLE))
1157 			return;
1158 		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
1159 			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
1160 			 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
1161 		intel_de_write(dev_priv, reg, val);
1162 		intel_de_posting_read(dev_priv, reg);
1163 		return;
1164 	}
1165 
1166 	if (port != (val & VIDEO_DIP_PORT_MASK)) {
1167 		drm_WARN(&dev_priv->drm, val & VIDEO_DIP_ENABLE,
1168 			 "DIP already enabled on port %c\n",
1169 			 (val & VIDEO_DIP_PORT_MASK) >> 29);
1170 		val &= ~VIDEO_DIP_PORT_MASK;
1171 		val |= port;
1172 	}
1173 
1174 	val |= VIDEO_DIP_ENABLE;
1175 	val &= ~(VIDEO_DIP_ENABLE_AVI |
1176 		 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
1177 		 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
1178 
1179 	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
1180 		val |= VIDEO_DIP_ENABLE_GCP;
1181 
1182 	intel_de_write(dev_priv, reg, val);
1183 	intel_de_posting_read(dev_priv, reg);
1184 
1185 	intel_write_infoframe(encoder, crtc_state,
1186 			      HDMI_INFOFRAME_TYPE_AVI,
1187 			      &crtc_state->infoframes.avi);
1188 	intel_write_infoframe(encoder, crtc_state,
1189 			      HDMI_INFOFRAME_TYPE_SPD,
1190 			      &crtc_state->infoframes.spd);
1191 	intel_write_infoframe(encoder, crtc_state,
1192 			      HDMI_INFOFRAME_TYPE_VENDOR,
1193 			      &crtc_state->infoframes.hdmi);
1194 }
1195 
1196 static void hsw_set_infoframes(struct intel_encoder *encoder,
1197 			       bool enable,
1198 			       const struct intel_crtc_state *crtc_state,
1199 			       const struct drm_connector_state *conn_state)
1200 {
1201 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1202 	i915_reg_t reg = HSW_TVIDEO_DIP_CTL(crtc_state->cpu_transcoder);
1203 	u32 val = intel_de_read(dev_priv, reg);
1204 
1205 	assert_hdmi_transcoder_func_disabled(dev_priv,
1206 					     crtc_state->cpu_transcoder);
1207 
1208 	val &= ~(VIDEO_DIP_ENABLE_VSC_HSW | VIDEO_DIP_ENABLE_AVI_HSW |
1209 		 VIDEO_DIP_ENABLE_GCP_HSW | VIDEO_DIP_ENABLE_VS_HSW |
1210 		 VIDEO_DIP_ENABLE_GMP_HSW | VIDEO_DIP_ENABLE_SPD_HSW |
1211 		 VIDEO_DIP_ENABLE_DRM_GLK);
1212 
1213 	if (!enable) {
1214 		intel_de_write(dev_priv, reg, val);
1215 		intel_de_posting_read(dev_priv, reg);
1216 		return;
1217 	}
1218 
1219 	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
1220 		val |= VIDEO_DIP_ENABLE_GCP_HSW;
1221 
1222 	intel_de_write(dev_priv, reg, val);
1223 	intel_de_posting_read(dev_priv, reg);
1224 
1225 	intel_write_infoframe(encoder, crtc_state,
1226 			      HDMI_INFOFRAME_TYPE_AVI,
1227 			      &crtc_state->infoframes.avi);
1228 	intel_write_infoframe(encoder, crtc_state,
1229 			      HDMI_INFOFRAME_TYPE_SPD,
1230 			      &crtc_state->infoframes.spd);
1231 	intel_write_infoframe(encoder, crtc_state,
1232 			      HDMI_INFOFRAME_TYPE_VENDOR,
1233 			      &crtc_state->infoframes.hdmi);
1234 	intel_write_infoframe(encoder, crtc_state,
1235 			      HDMI_INFOFRAME_TYPE_DRM,
1236 			      &crtc_state->infoframes.drm);
1237 }
1238 
1239 void intel_dp_dual_mode_set_tmds_output(struct intel_hdmi *hdmi, bool enable)
1240 {
1241 	struct drm_i915_private *dev_priv = intel_hdmi_to_i915(hdmi);
1242 	struct i2c_adapter *adapter;
1243 
1244 	if (hdmi->dp_dual_mode.type < DRM_DP_DUAL_MODE_TYPE2_DVI)
1245 		return;
1246 
1247 	adapter = intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
1248 
1249 	drm_dbg_kms(&dev_priv->drm, "%s DP dual mode adaptor TMDS output\n",
1250 		    enable ? "Enabling" : "Disabling");
1251 
1252 	drm_dp_dual_mode_set_tmds_output(&dev_priv->drm, hdmi->dp_dual_mode.type, adapter, enable);
1253 }
1254 
1255 static int intel_hdmi_hdcp_read(struct intel_digital_port *dig_port,
1256 				unsigned int offset, void *buffer, size_t size)
1257 {
1258 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1259 	struct intel_hdmi *hdmi = &dig_port->hdmi;
1260 	struct i2c_adapter *adapter = intel_gmbus_get_adapter(i915,
1261 							      hdmi->ddc_bus);
1262 	int ret;
1263 	u8 start = offset & 0xff;
1264 	struct i2c_msg msgs[] = {
1265 		{
1266 			.addr = DRM_HDCP_DDC_ADDR,
1267 			.flags = 0,
1268 			.len = 1,
1269 			.buf = &start,
1270 		},
1271 		{
1272 			.addr = DRM_HDCP_DDC_ADDR,
1273 			.flags = I2C_M_RD,
1274 			.len = size,
1275 			.buf = buffer
1276 		}
1277 	};
1278 	ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
1279 	if (ret == ARRAY_SIZE(msgs))
1280 		return 0;
1281 	return ret >= 0 ? -EIO : ret;
1282 }
1283 
1284 static int intel_hdmi_hdcp_write(struct intel_digital_port *dig_port,
1285 				 unsigned int offset, void *buffer, size_t size)
1286 {
1287 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1288 	struct intel_hdmi *hdmi = &dig_port->hdmi;
1289 	struct i2c_adapter *adapter = intel_gmbus_get_adapter(i915,
1290 							      hdmi->ddc_bus);
1291 	int ret;
1292 	u8 *write_buf;
1293 	struct i2c_msg msg;
1294 
1295 	write_buf = kzalloc(size + 1, GFP_KERNEL);
1296 	if (!write_buf)
1297 		return -ENOMEM;
1298 
1299 	write_buf[0] = offset & 0xff;
1300 	memcpy(&write_buf[1], buffer, size);
1301 
1302 	msg.addr = DRM_HDCP_DDC_ADDR;
1303 	msg.flags = 0,
1304 	msg.len = size + 1,
1305 	msg.buf = write_buf;
1306 
1307 	ret = i2c_transfer(adapter, &msg, 1);
1308 	if (ret == 1)
1309 		ret = 0;
1310 	else if (ret >= 0)
1311 		ret = -EIO;
1312 
1313 	kfree(write_buf);
1314 	return ret;
1315 }
1316 
1317 static
1318 int intel_hdmi_hdcp_write_an_aksv(struct intel_digital_port *dig_port,
1319 				  u8 *an)
1320 {
1321 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1322 	struct intel_hdmi *hdmi = &dig_port->hdmi;
1323 	struct i2c_adapter *adapter = intel_gmbus_get_adapter(i915,
1324 							      hdmi->ddc_bus);
1325 	int ret;
1326 
1327 	ret = intel_hdmi_hdcp_write(dig_port, DRM_HDCP_DDC_AN, an,
1328 				    DRM_HDCP_AN_LEN);
1329 	if (ret) {
1330 		drm_dbg_kms(&i915->drm, "Write An over DDC failed (%d)\n",
1331 			    ret);
1332 		return ret;
1333 	}
1334 
1335 	ret = intel_gmbus_output_aksv(adapter);
1336 	if (ret < 0) {
1337 		drm_dbg_kms(&i915->drm, "Failed to output aksv (%d)\n", ret);
1338 		return ret;
1339 	}
1340 	return 0;
1341 }
1342 
1343 static int intel_hdmi_hdcp_read_bksv(struct intel_digital_port *dig_port,
1344 				     u8 *bksv)
1345 {
1346 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1347 
1348 	int ret;
1349 	ret = intel_hdmi_hdcp_read(dig_port, DRM_HDCP_DDC_BKSV, bksv,
1350 				   DRM_HDCP_KSV_LEN);
1351 	if (ret)
1352 		drm_dbg_kms(&i915->drm, "Read Bksv over DDC failed (%d)\n",
1353 			    ret);
1354 	return ret;
1355 }
1356 
1357 static
1358 int intel_hdmi_hdcp_read_bstatus(struct intel_digital_port *dig_port,
1359 				 u8 *bstatus)
1360 {
1361 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1362 
1363 	int ret;
1364 	ret = intel_hdmi_hdcp_read(dig_port, DRM_HDCP_DDC_BSTATUS,
1365 				   bstatus, DRM_HDCP_BSTATUS_LEN);
1366 	if (ret)
1367 		drm_dbg_kms(&i915->drm, "Read bstatus over DDC failed (%d)\n",
1368 			    ret);
1369 	return ret;
1370 }
1371 
1372 static
1373 int intel_hdmi_hdcp_repeater_present(struct intel_digital_port *dig_port,
1374 				     bool *repeater_present)
1375 {
1376 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1377 	int ret;
1378 	u8 val;
1379 
1380 	ret = intel_hdmi_hdcp_read(dig_port, DRM_HDCP_DDC_BCAPS, &val, 1);
1381 	if (ret) {
1382 		drm_dbg_kms(&i915->drm, "Read bcaps over DDC failed (%d)\n",
1383 			    ret);
1384 		return ret;
1385 	}
1386 	*repeater_present = val & DRM_HDCP_DDC_BCAPS_REPEATER_PRESENT;
1387 	return 0;
1388 }
1389 
1390 static
1391 int intel_hdmi_hdcp_read_ri_prime(struct intel_digital_port *dig_port,
1392 				  u8 *ri_prime)
1393 {
1394 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1395 
1396 	int ret;
1397 	ret = intel_hdmi_hdcp_read(dig_port, DRM_HDCP_DDC_RI_PRIME,
1398 				   ri_prime, DRM_HDCP_RI_LEN);
1399 	if (ret)
1400 		drm_dbg_kms(&i915->drm, "Read Ri' over DDC failed (%d)\n",
1401 			    ret);
1402 	return ret;
1403 }
1404 
1405 static
1406 int intel_hdmi_hdcp_read_ksv_ready(struct intel_digital_port *dig_port,
1407 				   bool *ksv_ready)
1408 {
1409 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1410 	int ret;
1411 	u8 val;
1412 
1413 	ret = intel_hdmi_hdcp_read(dig_port, DRM_HDCP_DDC_BCAPS, &val, 1);
1414 	if (ret) {
1415 		drm_dbg_kms(&i915->drm, "Read bcaps over DDC failed (%d)\n",
1416 			    ret);
1417 		return ret;
1418 	}
1419 	*ksv_ready = val & DRM_HDCP_DDC_BCAPS_KSV_FIFO_READY;
1420 	return 0;
1421 }
1422 
1423 static
1424 int intel_hdmi_hdcp_read_ksv_fifo(struct intel_digital_port *dig_port,
1425 				  int num_downstream, u8 *ksv_fifo)
1426 {
1427 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1428 	int ret;
1429 	ret = intel_hdmi_hdcp_read(dig_port, DRM_HDCP_DDC_KSV_FIFO,
1430 				   ksv_fifo, num_downstream * DRM_HDCP_KSV_LEN);
1431 	if (ret) {
1432 		drm_dbg_kms(&i915->drm,
1433 			    "Read ksv fifo over DDC failed (%d)\n", ret);
1434 		return ret;
1435 	}
1436 	return 0;
1437 }
1438 
1439 static
1440 int intel_hdmi_hdcp_read_v_prime_part(struct intel_digital_port *dig_port,
1441 				      int i, u32 *part)
1442 {
1443 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1444 	int ret;
1445 
1446 	if (i >= DRM_HDCP_V_PRIME_NUM_PARTS)
1447 		return -EINVAL;
1448 
1449 	ret = intel_hdmi_hdcp_read(dig_port, DRM_HDCP_DDC_V_PRIME(i),
1450 				   part, DRM_HDCP_V_PRIME_PART_LEN);
1451 	if (ret)
1452 		drm_dbg_kms(&i915->drm, "Read V'[%d] over DDC failed (%d)\n",
1453 			    i, ret);
1454 	return ret;
1455 }
1456 
1457 static int kbl_repositioning_enc_en_signal(struct intel_connector *connector,
1458 					   enum transcoder cpu_transcoder)
1459 {
1460 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1461 	struct intel_digital_port *dig_port = intel_attached_dig_port(connector);
1462 	struct intel_crtc *crtc = to_intel_crtc(connector->base.state->crtc);
1463 	u32 scanline;
1464 	int ret;
1465 
1466 	for (;;) {
1467 		scanline = intel_de_read(dev_priv, PIPEDSL(crtc->pipe));
1468 		if (scanline > 100 && scanline < 200)
1469 			break;
1470 		usleep_range(25, 50);
1471 	}
1472 
1473 	ret = intel_ddi_toggle_hdcp_bits(&dig_port->base, cpu_transcoder,
1474 					 false, TRANS_DDI_HDCP_SIGNALLING);
1475 	if (ret) {
1476 		drm_err(&dev_priv->drm,
1477 			"Disable HDCP signalling failed (%d)\n", ret);
1478 		return ret;
1479 	}
1480 
1481 	ret = intel_ddi_toggle_hdcp_bits(&dig_port->base, cpu_transcoder,
1482 					 true, TRANS_DDI_HDCP_SIGNALLING);
1483 	if (ret) {
1484 		drm_err(&dev_priv->drm,
1485 			"Enable HDCP signalling failed (%d)\n", ret);
1486 		return ret;
1487 	}
1488 
1489 	return 0;
1490 }
1491 
1492 static
1493 int intel_hdmi_hdcp_toggle_signalling(struct intel_digital_port *dig_port,
1494 				      enum transcoder cpu_transcoder,
1495 				      bool enable)
1496 {
1497 	struct intel_hdmi *hdmi = &dig_port->hdmi;
1498 	struct intel_connector *connector = hdmi->attached_connector;
1499 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1500 	int ret;
1501 
1502 	if (!enable)
1503 		usleep_range(6, 60); /* Bspec says >= 6us */
1504 
1505 	ret = intel_ddi_toggle_hdcp_bits(&dig_port->base,
1506 					 cpu_transcoder, enable,
1507 					 TRANS_DDI_HDCP_SIGNALLING);
1508 	if (ret) {
1509 		drm_err(&dev_priv->drm, "%s HDCP signalling failed (%d)\n",
1510 			enable ? "Enable" : "Disable", ret);
1511 		return ret;
1512 	}
1513 
1514 	/*
1515 	 * WA: To fix incorrect positioning of the window of
1516 	 * opportunity and enc_en signalling in KABYLAKE.
1517 	 */
1518 	if (IS_KABYLAKE(dev_priv) && enable)
1519 		return kbl_repositioning_enc_en_signal(connector,
1520 						       cpu_transcoder);
1521 
1522 	return 0;
1523 }
1524 
1525 static
1526 bool intel_hdmi_hdcp_check_link_once(struct intel_digital_port *dig_port,
1527 				     struct intel_connector *connector)
1528 {
1529 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1530 	enum port port = dig_port->base.port;
1531 	enum transcoder cpu_transcoder = connector->hdcp.cpu_transcoder;
1532 	int ret;
1533 	union {
1534 		u32 reg;
1535 		u8 shim[DRM_HDCP_RI_LEN];
1536 	} ri;
1537 
1538 	ret = intel_hdmi_hdcp_read_ri_prime(dig_port, ri.shim);
1539 	if (ret)
1540 		return false;
1541 
1542 	intel_de_write(i915, HDCP_RPRIME(i915, cpu_transcoder, port), ri.reg);
1543 
1544 	/* Wait for Ri prime match */
1545 	if (wait_for((intel_de_read(i915, HDCP_STATUS(i915, cpu_transcoder, port)) &
1546 		      (HDCP_STATUS_RI_MATCH | HDCP_STATUS_ENC)) ==
1547 		     (HDCP_STATUS_RI_MATCH | HDCP_STATUS_ENC), 1)) {
1548 		drm_dbg_kms(&i915->drm, "Ri' mismatch detected (%x)\n",
1549 			intel_de_read(i915, HDCP_STATUS(i915, cpu_transcoder,
1550 							port)));
1551 		return false;
1552 	}
1553 	return true;
1554 }
1555 
1556 static
1557 bool intel_hdmi_hdcp_check_link(struct intel_digital_port *dig_port,
1558 				struct intel_connector *connector)
1559 {
1560 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1561 	int retry;
1562 
1563 	for (retry = 0; retry < 3; retry++)
1564 		if (intel_hdmi_hdcp_check_link_once(dig_port, connector))
1565 			return true;
1566 
1567 	drm_err(&i915->drm, "Link check failed\n");
1568 	return false;
1569 }
1570 
1571 struct hdcp2_hdmi_msg_timeout {
1572 	u8 msg_id;
1573 	u16 timeout;
1574 };
1575 
1576 static const struct hdcp2_hdmi_msg_timeout hdcp2_msg_timeout[] = {
1577 	{ HDCP_2_2_AKE_SEND_CERT, HDCP_2_2_CERT_TIMEOUT_MS, },
1578 	{ HDCP_2_2_AKE_SEND_PAIRING_INFO, HDCP_2_2_PAIRING_TIMEOUT_MS, },
1579 	{ HDCP_2_2_LC_SEND_LPRIME, HDCP_2_2_HDMI_LPRIME_TIMEOUT_MS, },
1580 	{ HDCP_2_2_REP_SEND_RECVID_LIST, HDCP_2_2_RECVID_LIST_TIMEOUT_MS, },
1581 	{ HDCP_2_2_REP_STREAM_READY, HDCP_2_2_STREAM_READY_TIMEOUT_MS, },
1582 };
1583 
1584 static
1585 int intel_hdmi_hdcp2_read_rx_status(struct intel_digital_port *dig_port,
1586 				    u8 *rx_status)
1587 {
1588 	return intel_hdmi_hdcp_read(dig_port,
1589 				    HDCP_2_2_HDMI_REG_RXSTATUS_OFFSET,
1590 				    rx_status,
1591 				    HDCP_2_2_HDMI_RXSTATUS_LEN);
1592 }
1593 
1594 static int get_hdcp2_msg_timeout(u8 msg_id, bool is_paired)
1595 {
1596 	int i;
1597 
1598 	if (msg_id == HDCP_2_2_AKE_SEND_HPRIME) {
1599 		if (is_paired)
1600 			return HDCP_2_2_HPRIME_PAIRED_TIMEOUT_MS;
1601 		else
1602 			return HDCP_2_2_HPRIME_NO_PAIRED_TIMEOUT_MS;
1603 	}
1604 
1605 	for (i = 0; i < ARRAY_SIZE(hdcp2_msg_timeout); i++) {
1606 		if (hdcp2_msg_timeout[i].msg_id == msg_id)
1607 			return hdcp2_msg_timeout[i].timeout;
1608 	}
1609 
1610 	return -EINVAL;
1611 }
1612 
1613 static int
1614 hdcp2_detect_msg_availability(struct intel_digital_port *dig_port,
1615 			      u8 msg_id, bool *msg_ready,
1616 			      ssize_t *msg_sz)
1617 {
1618 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1619 	u8 rx_status[HDCP_2_2_HDMI_RXSTATUS_LEN];
1620 	int ret;
1621 
1622 	ret = intel_hdmi_hdcp2_read_rx_status(dig_port, rx_status);
1623 	if (ret < 0) {
1624 		drm_dbg_kms(&i915->drm, "rx_status read failed. Err %d\n",
1625 			    ret);
1626 		return ret;
1627 	}
1628 
1629 	*msg_sz = ((HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(rx_status[1]) << 8) |
1630 		  rx_status[0]);
1631 
1632 	if (msg_id == HDCP_2_2_REP_SEND_RECVID_LIST)
1633 		*msg_ready = (HDCP_2_2_HDMI_RXSTATUS_READY(rx_status[1]) &&
1634 			     *msg_sz);
1635 	else
1636 		*msg_ready = *msg_sz;
1637 
1638 	return 0;
1639 }
1640 
1641 static ssize_t
1642 intel_hdmi_hdcp2_wait_for_msg(struct intel_digital_port *dig_port,
1643 			      u8 msg_id, bool paired)
1644 {
1645 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1646 	bool msg_ready = false;
1647 	int timeout, ret;
1648 	ssize_t msg_sz = 0;
1649 
1650 	timeout = get_hdcp2_msg_timeout(msg_id, paired);
1651 	if (timeout < 0)
1652 		return timeout;
1653 
1654 	ret = __wait_for(ret = hdcp2_detect_msg_availability(dig_port,
1655 							     msg_id, &msg_ready,
1656 							     &msg_sz),
1657 			 !ret && msg_ready && msg_sz, timeout * 1000,
1658 			 1000, 5 * 1000);
1659 	if (ret)
1660 		drm_dbg_kms(&i915->drm, "msg_id: %d, ret: %d, timeout: %d\n",
1661 			    msg_id, ret, timeout);
1662 
1663 	return ret ? ret : msg_sz;
1664 }
1665 
1666 static
1667 int intel_hdmi_hdcp2_write_msg(struct intel_digital_port *dig_port,
1668 			       void *buf, size_t size)
1669 {
1670 	unsigned int offset;
1671 
1672 	offset = HDCP_2_2_HDMI_REG_WR_MSG_OFFSET;
1673 	return intel_hdmi_hdcp_write(dig_port, offset, buf, size);
1674 }
1675 
1676 static
1677 int intel_hdmi_hdcp2_read_msg(struct intel_digital_port *dig_port,
1678 			      u8 msg_id, void *buf, size_t size)
1679 {
1680 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1681 	struct intel_hdmi *hdmi = &dig_port->hdmi;
1682 	struct intel_hdcp *hdcp = &hdmi->attached_connector->hdcp;
1683 	unsigned int offset;
1684 	ssize_t ret;
1685 
1686 	ret = intel_hdmi_hdcp2_wait_for_msg(dig_port, msg_id,
1687 					    hdcp->is_paired);
1688 	if (ret < 0)
1689 		return ret;
1690 
1691 	/*
1692 	 * Available msg size should be equal to or lesser than the
1693 	 * available buffer.
1694 	 */
1695 	if (ret > size) {
1696 		drm_dbg_kms(&i915->drm,
1697 			    "msg_sz(%zd) is more than exp size(%zu)\n",
1698 			    ret, size);
1699 		return -EINVAL;
1700 	}
1701 
1702 	offset = HDCP_2_2_HDMI_REG_RD_MSG_OFFSET;
1703 	ret = intel_hdmi_hdcp_read(dig_port, offset, buf, ret);
1704 	if (ret)
1705 		drm_dbg_kms(&i915->drm, "Failed to read msg_id: %d(%zd)\n",
1706 			    msg_id, ret);
1707 
1708 	return ret;
1709 }
1710 
1711 static
1712 int intel_hdmi_hdcp2_check_link(struct intel_digital_port *dig_port,
1713 				struct intel_connector *connector)
1714 {
1715 	u8 rx_status[HDCP_2_2_HDMI_RXSTATUS_LEN];
1716 	int ret;
1717 
1718 	ret = intel_hdmi_hdcp2_read_rx_status(dig_port, rx_status);
1719 	if (ret)
1720 		return ret;
1721 
1722 	/*
1723 	 * Re-auth request and Link Integrity Failures are represented by
1724 	 * same bit. i.e reauth_req.
1725 	 */
1726 	if (HDCP_2_2_HDMI_RXSTATUS_REAUTH_REQ(rx_status[1]))
1727 		ret = HDCP_REAUTH_REQUEST;
1728 	else if (HDCP_2_2_HDMI_RXSTATUS_READY(rx_status[1]))
1729 		ret = HDCP_TOPOLOGY_CHANGE;
1730 
1731 	return ret;
1732 }
1733 
1734 static
1735 int intel_hdmi_hdcp2_capable(struct intel_digital_port *dig_port,
1736 			     bool *capable)
1737 {
1738 	u8 hdcp2_version;
1739 	int ret;
1740 
1741 	*capable = false;
1742 	ret = intel_hdmi_hdcp_read(dig_port, HDCP_2_2_HDMI_REG_VER_OFFSET,
1743 				   &hdcp2_version, sizeof(hdcp2_version));
1744 	if (!ret && hdcp2_version & HDCP_2_2_HDMI_SUPPORT_MASK)
1745 		*capable = true;
1746 
1747 	return ret;
1748 }
1749 
1750 static const struct intel_hdcp_shim intel_hdmi_hdcp_shim = {
1751 	.write_an_aksv = intel_hdmi_hdcp_write_an_aksv,
1752 	.read_bksv = intel_hdmi_hdcp_read_bksv,
1753 	.read_bstatus = intel_hdmi_hdcp_read_bstatus,
1754 	.repeater_present = intel_hdmi_hdcp_repeater_present,
1755 	.read_ri_prime = intel_hdmi_hdcp_read_ri_prime,
1756 	.read_ksv_ready = intel_hdmi_hdcp_read_ksv_ready,
1757 	.read_ksv_fifo = intel_hdmi_hdcp_read_ksv_fifo,
1758 	.read_v_prime_part = intel_hdmi_hdcp_read_v_prime_part,
1759 	.toggle_signalling = intel_hdmi_hdcp_toggle_signalling,
1760 	.check_link = intel_hdmi_hdcp_check_link,
1761 	.write_2_2_msg = intel_hdmi_hdcp2_write_msg,
1762 	.read_2_2_msg = intel_hdmi_hdcp2_read_msg,
1763 	.check_2_2_link	= intel_hdmi_hdcp2_check_link,
1764 	.hdcp_2_2_capable = intel_hdmi_hdcp2_capable,
1765 	.protocol = HDCP_PROTOCOL_HDMI,
1766 };
1767 
1768 static int intel_hdmi_source_max_tmds_clock(struct intel_encoder *encoder)
1769 {
1770 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1771 	int max_tmds_clock, vbt_max_tmds_clock;
1772 
1773 	if (DISPLAY_VER(dev_priv) >= 10)
1774 		max_tmds_clock = 594000;
1775 	else if (DISPLAY_VER(dev_priv) >= 8 || IS_HASWELL(dev_priv))
1776 		max_tmds_clock = 300000;
1777 	else if (DISPLAY_VER(dev_priv) >= 5)
1778 		max_tmds_clock = 225000;
1779 	else
1780 		max_tmds_clock = 165000;
1781 
1782 	vbt_max_tmds_clock = intel_bios_hdmi_max_tmds_clock(encoder->devdata);
1783 	if (vbt_max_tmds_clock)
1784 		max_tmds_clock = min(max_tmds_clock, vbt_max_tmds_clock);
1785 
1786 	return max_tmds_clock;
1787 }
1788 
1789 static bool intel_has_hdmi_sink(struct intel_hdmi *hdmi,
1790 				const struct drm_connector_state *conn_state)
1791 {
1792 	return hdmi->has_hdmi_sink &&
1793 		READ_ONCE(to_intel_digital_connector_state(conn_state)->force_audio) != HDMI_AUDIO_OFF_DVI;
1794 }
1795 
1796 static bool intel_hdmi_is_ycbcr420(const struct intel_crtc_state *crtc_state)
1797 {
1798 	return crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420;
1799 }
1800 
1801 static int hdmi_port_clock_limit(struct intel_hdmi *hdmi,
1802 				 bool respect_downstream_limits,
1803 				 bool has_hdmi_sink)
1804 {
1805 	struct intel_encoder *encoder = &hdmi_to_dig_port(hdmi)->base;
1806 	int max_tmds_clock = intel_hdmi_source_max_tmds_clock(encoder);
1807 
1808 	if (respect_downstream_limits) {
1809 		struct intel_connector *connector = hdmi->attached_connector;
1810 		const struct drm_display_info *info = &connector->base.display_info;
1811 
1812 		if (hdmi->dp_dual_mode.max_tmds_clock)
1813 			max_tmds_clock = min(max_tmds_clock,
1814 					     hdmi->dp_dual_mode.max_tmds_clock);
1815 
1816 		if (info->max_tmds_clock)
1817 			max_tmds_clock = min(max_tmds_clock,
1818 					     info->max_tmds_clock);
1819 		else if (!has_hdmi_sink)
1820 			max_tmds_clock = min(max_tmds_clock, 165000);
1821 	}
1822 
1823 	return max_tmds_clock;
1824 }
1825 
1826 static enum drm_mode_status
1827 hdmi_port_clock_valid(struct intel_hdmi *hdmi,
1828 		      int clock, bool respect_downstream_limits,
1829 		      bool has_hdmi_sink)
1830 {
1831 	struct drm_i915_private *dev_priv = intel_hdmi_to_i915(hdmi);
1832 	enum phy phy = intel_port_to_phy(dev_priv, hdmi_to_dig_port(hdmi)->base.port);
1833 
1834 	if (clock < 25000)
1835 		return MODE_CLOCK_LOW;
1836 	if (clock > hdmi_port_clock_limit(hdmi, respect_downstream_limits,
1837 					  has_hdmi_sink))
1838 		return MODE_CLOCK_HIGH;
1839 
1840 	/* GLK DPLL can't generate 446-480 MHz */
1841 	if (IS_GEMINILAKE(dev_priv) && clock > 446666 && clock < 480000)
1842 		return MODE_CLOCK_RANGE;
1843 
1844 	/* BXT/GLK DPLL can't generate 223-240 MHz */
1845 	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
1846 	    clock > 223333 && clock < 240000)
1847 		return MODE_CLOCK_RANGE;
1848 
1849 	/* CHV DPLL can't generate 216-240 MHz */
1850 	if (IS_CHERRYVIEW(dev_priv) && clock > 216000 && clock < 240000)
1851 		return MODE_CLOCK_RANGE;
1852 
1853 	/* ICL+ combo PHY PLL can't generate 500-533.2 MHz */
1854 	if (intel_phy_is_combo(dev_priv, phy) && clock > 500000 && clock < 533200)
1855 		return MODE_CLOCK_RANGE;
1856 
1857 	/* ICL+ TC PHY PLL can't generate 500-532.8 MHz */
1858 	if (intel_phy_is_tc(dev_priv, phy) && clock > 500000 && clock < 532800)
1859 		return MODE_CLOCK_RANGE;
1860 
1861 	/*
1862 	 * SNPS PHYs' MPLLB table-based programming can only handle a fixed
1863 	 * set of link rates.
1864 	 *
1865 	 * FIXME: We will hopefully get an algorithmic way of programming
1866 	 * the MPLLB for HDMI in the future.
1867 	 */
1868 	if (IS_DG2(dev_priv))
1869 		return intel_snps_phy_check_hdmi_link_rate(clock);
1870 
1871 	return MODE_OK;
1872 }
1873 
1874 int intel_hdmi_tmds_clock(int clock, int bpc, bool ycbcr420_output)
1875 {
1876 	/* YCBCR420 TMDS rate requirement is half the pixel clock */
1877 	if (ycbcr420_output)
1878 		clock /= 2;
1879 
1880 	/*
1881 	 * Need to adjust the port link by:
1882 	 *  1.5x for 12bpc
1883 	 *  1.25x for 10bpc
1884 	 */
1885 	return DIV_ROUND_CLOSEST(clock * bpc, 8);
1886 }
1887 
1888 static bool intel_hdmi_source_bpc_possible(struct drm_i915_private *i915, int bpc)
1889 {
1890 	switch (bpc) {
1891 	case 12:
1892 		return !HAS_GMCH(i915);
1893 	case 10:
1894 		return DISPLAY_VER(i915) >= 11;
1895 	case 8:
1896 		return true;
1897 	default:
1898 		MISSING_CASE(bpc);
1899 		return false;
1900 	}
1901 }
1902 
1903 static bool intel_hdmi_sink_bpc_possible(struct drm_connector *connector,
1904 					 int bpc, bool has_hdmi_sink, bool ycbcr420_output)
1905 {
1906 	const struct drm_display_info *info = &connector->display_info;
1907 	const struct drm_hdmi_info *hdmi = &info->hdmi;
1908 
1909 	switch (bpc) {
1910 	case 12:
1911 		if (!has_hdmi_sink)
1912 			return false;
1913 
1914 		if (ycbcr420_output)
1915 			return hdmi->y420_dc_modes & DRM_EDID_YCBCR420_DC_36;
1916 		else
1917 			return info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36;
1918 	case 10:
1919 		if (!has_hdmi_sink)
1920 			return false;
1921 
1922 		if (ycbcr420_output)
1923 			return hdmi->y420_dc_modes & DRM_EDID_YCBCR420_DC_30;
1924 		else
1925 			return info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30;
1926 	case 8:
1927 		return true;
1928 	default:
1929 		MISSING_CASE(bpc);
1930 		return false;
1931 	}
1932 }
1933 
1934 static enum drm_mode_status
1935 intel_hdmi_mode_clock_valid(struct drm_connector *connector, int clock,
1936 			    bool has_hdmi_sink, bool ycbcr420_output)
1937 {
1938 	struct drm_i915_private *i915 = to_i915(connector->dev);
1939 	struct intel_hdmi *hdmi = intel_attached_hdmi(to_intel_connector(connector));
1940 	enum drm_mode_status status = MODE_OK;
1941 	int bpc;
1942 
1943 	/*
1944 	 * Try all color depths since valid port clock range
1945 	 * can have holes. Any mode that can be used with at
1946 	 * least one color depth is accepted.
1947 	 */
1948 	for (bpc = 12; bpc >= 8; bpc -= 2) {
1949 		int tmds_clock = intel_hdmi_tmds_clock(clock, bpc, ycbcr420_output);
1950 
1951 		if (!intel_hdmi_source_bpc_possible(i915, bpc))
1952 			continue;
1953 
1954 		if (!intel_hdmi_sink_bpc_possible(connector, bpc, has_hdmi_sink, ycbcr420_output))
1955 			continue;
1956 
1957 		status = hdmi_port_clock_valid(hdmi, tmds_clock, true, has_hdmi_sink);
1958 		if (status == MODE_OK)
1959 			return MODE_OK;
1960 	}
1961 
1962 	/* can never happen */
1963 	drm_WARN_ON(&i915->drm, status == MODE_OK);
1964 
1965 	return status;
1966 }
1967 
1968 static enum drm_mode_status
1969 intel_hdmi_mode_valid(struct drm_connector *connector,
1970 		      struct drm_display_mode *mode)
1971 {
1972 	struct intel_hdmi *hdmi = intel_attached_hdmi(to_intel_connector(connector));
1973 	struct drm_i915_private *dev_priv = intel_hdmi_to_i915(hdmi);
1974 	enum drm_mode_status status;
1975 	int clock = mode->clock;
1976 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
1977 	bool has_hdmi_sink = intel_has_hdmi_sink(hdmi, connector->state);
1978 	bool ycbcr_420_only;
1979 
1980 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1981 		clock *= 2;
1982 
1983 	if (clock > max_dotclk)
1984 		return MODE_CLOCK_HIGH;
1985 
1986 	if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
1987 		if (!has_hdmi_sink)
1988 			return MODE_CLOCK_LOW;
1989 		clock *= 2;
1990 	}
1991 
1992 	/*
1993 	 * HDMI2.1 requires higher resolution modes like 8k60, 4K120 to be
1994 	 * enumerated only if FRL is supported. Current platforms do not support
1995 	 * FRL so prune the higher resolution modes that require doctclock more
1996 	 * than 600MHz.
1997 	 */
1998 	if (clock > 600000)
1999 		return MODE_CLOCK_HIGH;
2000 
2001 	ycbcr_420_only = drm_mode_is_420_only(&connector->display_info, mode);
2002 
2003 	status = intel_hdmi_mode_clock_valid(connector, clock, has_hdmi_sink, ycbcr_420_only);
2004 	if (status != MODE_OK) {
2005 		if (ycbcr_420_only ||
2006 		    !connector->ycbcr_420_allowed ||
2007 		    !drm_mode_is_420_also(&connector->display_info, mode))
2008 			return status;
2009 
2010 		status = intel_hdmi_mode_clock_valid(connector, clock, has_hdmi_sink, true);
2011 		if (status != MODE_OK)
2012 			return status;
2013 	}
2014 
2015 	return intel_mode_valid_max_plane_size(dev_priv, mode, false);
2016 }
2017 
2018 bool intel_hdmi_bpc_possible(const struct intel_crtc_state *crtc_state,
2019 			     int bpc, bool has_hdmi_sink, bool ycbcr420_output)
2020 {
2021 	struct drm_atomic_state *state = crtc_state->uapi.state;
2022 	struct drm_connector_state *connector_state;
2023 	struct drm_connector *connector;
2024 	int i;
2025 
2026 	for_each_new_connector_in_state(state, connector, connector_state, i) {
2027 		if (connector_state->crtc != crtc_state->uapi.crtc)
2028 			continue;
2029 
2030 		if (!intel_hdmi_sink_bpc_possible(connector, bpc, has_hdmi_sink, ycbcr420_output))
2031 			return false;
2032 	}
2033 
2034 	return true;
2035 }
2036 
2037 static bool hdmi_bpc_possible(const struct intel_crtc_state *crtc_state, int bpc)
2038 {
2039 	struct drm_i915_private *dev_priv =
2040 		to_i915(crtc_state->uapi.crtc->dev);
2041 	const struct drm_display_mode *adjusted_mode =
2042 		&crtc_state->hw.adjusted_mode;
2043 
2044 	if (!intel_hdmi_source_bpc_possible(dev_priv, bpc))
2045 		return false;
2046 
2047 	/* Display Wa_1405510057:icl,ehl */
2048 	if (intel_hdmi_is_ycbcr420(crtc_state) &&
2049 	    bpc == 10 && DISPLAY_VER(dev_priv) == 11 &&
2050 	    (adjusted_mode->crtc_hblank_end -
2051 	     adjusted_mode->crtc_hblank_start) % 8 == 2)
2052 		return false;
2053 
2054 	return intel_hdmi_bpc_possible(crtc_state, bpc, crtc_state->has_hdmi_sink,
2055 				       intel_hdmi_is_ycbcr420(crtc_state));
2056 }
2057 
2058 static int intel_hdmi_compute_bpc(struct intel_encoder *encoder,
2059 				  struct intel_crtc_state *crtc_state,
2060 				  int clock, bool respect_downstream_limits)
2061 {
2062 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
2063 	bool ycbcr420_output = intel_hdmi_is_ycbcr420(crtc_state);
2064 	int bpc;
2065 
2066 	/*
2067 	 * pipe_bpp could already be below 8bpc due to FDI
2068 	 * bandwidth constraints. HDMI minimum is 8bpc however.
2069 	 */
2070 	bpc = max(crtc_state->pipe_bpp / 3, 8);
2071 
2072 	/*
2073 	 * We will never exceed downstream TMDS clock limits while
2074 	 * attempting deep color. If the user insists on forcing an
2075 	 * out of spec mode they will have to be satisfied with 8bpc.
2076 	 */
2077 	if (!respect_downstream_limits)
2078 		bpc = 8;
2079 
2080 	for (; bpc >= 8; bpc -= 2) {
2081 		int tmds_clock = intel_hdmi_tmds_clock(clock, bpc, ycbcr420_output);
2082 
2083 		if (hdmi_bpc_possible(crtc_state, bpc) &&
2084 		    hdmi_port_clock_valid(intel_hdmi, tmds_clock,
2085 					  respect_downstream_limits,
2086 					  crtc_state->has_hdmi_sink) == MODE_OK)
2087 			return bpc;
2088 	}
2089 
2090 	return -EINVAL;
2091 }
2092 
2093 static int intel_hdmi_compute_clock(struct intel_encoder *encoder,
2094 				    struct intel_crtc_state *crtc_state,
2095 				    bool respect_downstream_limits)
2096 {
2097 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2098 	const struct drm_display_mode *adjusted_mode =
2099 		&crtc_state->hw.adjusted_mode;
2100 	int bpc, clock = adjusted_mode->crtc_clock;
2101 
2102 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
2103 		clock *= 2;
2104 
2105 	bpc = intel_hdmi_compute_bpc(encoder, crtc_state, clock,
2106 				     respect_downstream_limits);
2107 	if (bpc < 0)
2108 		return bpc;
2109 
2110 	crtc_state->port_clock =
2111 		intel_hdmi_tmds_clock(clock, bpc, intel_hdmi_is_ycbcr420(crtc_state));
2112 
2113 	/*
2114 	 * pipe_bpp could already be below 8bpc due to
2115 	 * FDI bandwidth constraints. We shouldn't bump it
2116 	 * back up to the HDMI minimum 8bpc in that case.
2117 	 */
2118 	crtc_state->pipe_bpp = min(crtc_state->pipe_bpp, bpc * 3);
2119 
2120 	drm_dbg_kms(&i915->drm,
2121 		    "picking %d bpc for HDMI output (pipe bpp: %d)\n",
2122 		    bpc, crtc_state->pipe_bpp);
2123 
2124 	return 0;
2125 }
2126 
2127 bool intel_hdmi_limited_color_range(const struct intel_crtc_state *crtc_state,
2128 				    const struct drm_connector_state *conn_state)
2129 {
2130 	const struct intel_digital_connector_state *intel_conn_state =
2131 		to_intel_digital_connector_state(conn_state);
2132 	const struct drm_display_mode *adjusted_mode =
2133 		&crtc_state->hw.adjusted_mode;
2134 
2135 	/*
2136 	 * Our YCbCr output is always limited range.
2137 	 * crtc_state->limited_color_range only applies to RGB,
2138 	 * and it must never be set for YCbCr or we risk setting
2139 	 * some conflicting bits in TRANSCONF which will mess up
2140 	 * the colors on the monitor.
2141 	 */
2142 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
2143 		return false;
2144 
2145 	if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
2146 		/* See CEA-861-E - 5.1 Default Encoding Parameters */
2147 		return crtc_state->has_hdmi_sink &&
2148 			drm_default_rgb_quant_range(adjusted_mode) ==
2149 			HDMI_QUANTIZATION_RANGE_LIMITED;
2150 	} else {
2151 		return intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_LIMITED;
2152 	}
2153 }
2154 
2155 static bool intel_hdmi_has_audio(struct intel_encoder *encoder,
2156 				 const struct intel_crtc_state *crtc_state,
2157 				 const struct drm_connector_state *conn_state)
2158 {
2159 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
2160 	const struct intel_digital_connector_state *intel_conn_state =
2161 		to_intel_digital_connector_state(conn_state);
2162 
2163 	if (!crtc_state->has_hdmi_sink)
2164 		return false;
2165 
2166 	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
2167 		return intel_hdmi->has_audio;
2168 	else
2169 		return intel_conn_state->force_audio == HDMI_AUDIO_ON;
2170 }
2171 
2172 static enum intel_output_format
2173 intel_hdmi_output_format(const struct intel_crtc_state *crtc_state,
2174 			 struct intel_connector *connector,
2175 			 bool ycbcr_420_output)
2176 {
2177 	if (!crtc_state->has_hdmi_sink)
2178 		return INTEL_OUTPUT_FORMAT_RGB;
2179 
2180 	if (connector->base.ycbcr_420_allowed && ycbcr_420_output)
2181 		return INTEL_OUTPUT_FORMAT_YCBCR420;
2182 	else
2183 		return INTEL_OUTPUT_FORMAT_RGB;
2184 }
2185 
2186 static int intel_hdmi_compute_output_format(struct intel_encoder *encoder,
2187 					    struct intel_crtc_state *crtc_state,
2188 					    const struct drm_connector_state *conn_state,
2189 					    bool respect_downstream_limits)
2190 {
2191 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
2192 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2193 	const struct drm_display_info *info = &connector->base.display_info;
2194 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
2195 	bool ycbcr_420_only = drm_mode_is_420_only(info, adjusted_mode);
2196 	int ret;
2197 
2198 	crtc_state->output_format =
2199 		intel_hdmi_output_format(crtc_state, connector, ycbcr_420_only);
2200 
2201 	if (ycbcr_420_only && !intel_hdmi_is_ycbcr420(crtc_state)) {
2202 		drm_dbg_kms(&i915->drm,
2203 			    "YCbCr 4:2:0 mode but YCbCr 4:2:0 output not possible. Falling back to RGB.\n");
2204 		crtc_state->output_format = INTEL_OUTPUT_FORMAT_RGB;
2205 	}
2206 
2207 	ret = intel_hdmi_compute_clock(encoder, crtc_state, respect_downstream_limits);
2208 	if (ret) {
2209 		if (intel_hdmi_is_ycbcr420(crtc_state) ||
2210 		    !connector->base.ycbcr_420_allowed ||
2211 		    !drm_mode_is_420_also(info, adjusted_mode))
2212 			return ret;
2213 
2214 		crtc_state->output_format = intel_hdmi_output_format(crtc_state, connector, true);
2215 		ret = intel_hdmi_compute_clock(encoder, crtc_state, respect_downstream_limits);
2216 	}
2217 
2218 	return ret;
2219 }
2220 
2221 static bool intel_hdmi_is_cloned(const struct intel_crtc_state *crtc_state)
2222 {
2223 	return crtc_state->uapi.encoder_mask &&
2224 		!is_power_of_2(crtc_state->uapi.encoder_mask);
2225 }
2226 
2227 static bool source_supports_scrambling(struct intel_encoder *encoder)
2228 {
2229 	/*
2230 	 * Gen 10+ support HDMI 2.0 : the max tmds clock is 594MHz, and
2231 	 * scrambling is supported.
2232 	 * But there seem to be cases where certain platforms that support
2233 	 * HDMI 2.0, have an HDMI1.4 retimer chip, and the max tmds clock is
2234 	 * capped by VBT to less than 340MHz.
2235 	 *
2236 	 * In such cases when an HDMI2.0 sink is connected, it creates a
2237 	 * problem : the platform and the sink both support scrambling but the
2238 	 * HDMI 1.4 retimer chip doesn't.
2239 	 *
2240 	 * So go for scrambling, based on the max tmds clock taking into account,
2241 	 * restrictions coming from VBT.
2242 	 */
2243 	return intel_hdmi_source_max_tmds_clock(encoder) > 340000;
2244 }
2245 
2246 int intel_hdmi_compute_config(struct intel_encoder *encoder,
2247 			      struct intel_crtc_state *pipe_config,
2248 			      struct drm_connector_state *conn_state)
2249 {
2250 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
2251 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2252 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
2253 	struct drm_connector *connector = conn_state->connector;
2254 	struct drm_scdc *scdc = &connector->display_info.hdmi.scdc;
2255 	int ret;
2256 
2257 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
2258 		return -EINVAL;
2259 
2260 	if (!connector->interlace_allowed &&
2261 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
2262 		return -EINVAL;
2263 
2264 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
2265 	pipe_config->has_hdmi_sink =
2266 		intel_has_hdmi_sink(intel_hdmi, conn_state) &&
2267 		!intel_hdmi_is_cloned(pipe_config);
2268 
2269 	if (pipe_config->has_hdmi_sink)
2270 		pipe_config->has_infoframe = true;
2271 
2272 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
2273 		pipe_config->pixel_multiplier = 2;
2274 
2275 	pipe_config->has_audio =
2276 		intel_hdmi_has_audio(encoder, pipe_config, conn_state) &&
2277 		intel_audio_compute_config(encoder, pipe_config, conn_state);
2278 
2279 	/*
2280 	 * Try to respect downstream TMDS clock limits first, if
2281 	 * that fails assume the user might know something we don't.
2282 	 */
2283 	ret = intel_hdmi_compute_output_format(encoder, pipe_config, conn_state, true);
2284 	if (ret)
2285 		ret = intel_hdmi_compute_output_format(encoder, pipe_config, conn_state, false);
2286 	if (ret) {
2287 		drm_dbg_kms(&dev_priv->drm,
2288 			    "unsupported HDMI clock (%d kHz), rejecting mode\n",
2289 			    pipe_config->hw.adjusted_mode.crtc_clock);
2290 		return ret;
2291 	}
2292 
2293 	if (intel_hdmi_is_ycbcr420(pipe_config)) {
2294 		ret = intel_panel_fitting(pipe_config, conn_state);
2295 		if (ret)
2296 			return ret;
2297 	}
2298 
2299 	pipe_config->limited_color_range =
2300 		intel_hdmi_limited_color_range(pipe_config, conn_state);
2301 
2302 	if (conn_state->picture_aspect_ratio)
2303 		adjusted_mode->picture_aspect_ratio =
2304 			conn_state->picture_aspect_ratio;
2305 
2306 	pipe_config->lane_count = 4;
2307 
2308 	if (scdc->scrambling.supported && source_supports_scrambling(encoder)) {
2309 		if (scdc->scrambling.low_rates)
2310 			pipe_config->hdmi_scrambling = true;
2311 
2312 		if (pipe_config->port_clock > 340000) {
2313 			pipe_config->hdmi_scrambling = true;
2314 			pipe_config->hdmi_high_tmds_clock_ratio = true;
2315 		}
2316 	}
2317 
2318 	intel_hdmi_compute_gcp_infoframe(encoder, pipe_config,
2319 					 conn_state);
2320 
2321 	if (!intel_hdmi_compute_avi_infoframe(encoder, pipe_config, conn_state)) {
2322 		drm_dbg_kms(&dev_priv->drm, "bad AVI infoframe\n");
2323 		return -EINVAL;
2324 	}
2325 
2326 	if (!intel_hdmi_compute_spd_infoframe(encoder, pipe_config, conn_state)) {
2327 		drm_dbg_kms(&dev_priv->drm, "bad SPD infoframe\n");
2328 		return -EINVAL;
2329 	}
2330 
2331 	if (!intel_hdmi_compute_hdmi_infoframe(encoder, pipe_config, conn_state)) {
2332 		drm_dbg_kms(&dev_priv->drm, "bad HDMI infoframe\n");
2333 		return -EINVAL;
2334 	}
2335 
2336 	if (!intel_hdmi_compute_drm_infoframe(encoder, pipe_config, conn_state)) {
2337 		drm_dbg_kms(&dev_priv->drm, "bad DRM infoframe\n");
2338 		return -EINVAL;
2339 	}
2340 
2341 	return 0;
2342 }
2343 
2344 void intel_hdmi_encoder_shutdown(struct intel_encoder *encoder)
2345 {
2346 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
2347 
2348 	/*
2349 	 * Give a hand to buggy BIOSen which forget to turn
2350 	 * the TMDS output buffers back on after a reboot.
2351 	 */
2352 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
2353 }
2354 
2355 static void
2356 intel_hdmi_unset_edid(struct drm_connector *connector)
2357 {
2358 	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(to_intel_connector(connector));
2359 
2360 	intel_hdmi->has_hdmi_sink = false;
2361 	intel_hdmi->has_audio = false;
2362 
2363 	intel_hdmi->dp_dual_mode.type = DRM_DP_DUAL_MODE_NONE;
2364 	intel_hdmi->dp_dual_mode.max_tmds_clock = 0;
2365 
2366 	drm_edid_free(to_intel_connector(connector)->detect_edid);
2367 	to_intel_connector(connector)->detect_edid = NULL;
2368 }
2369 
2370 static void
2371 intel_hdmi_dp_dual_mode_detect(struct drm_connector *connector)
2372 {
2373 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
2374 	struct intel_hdmi *hdmi = intel_attached_hdmi(to_intel_connector(connector));
2375 	enum port port = hdmi_to_dig_port(hdmi)->base.port;
2376 	struct i2c_adapter *adapter =
2377 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
2378 	enum drm_dp_dual_mode_type type = drm_dp_dual_mode_detect(&dev_priv->drm, adapter);
2379 
2380 	/*
2381 	 * Type 1 DVI adaptors are not required to implement any
2382 	 * registers, so we can't always detect their presence.
2383 	 * Ideally we should be able to check the state of the
2384 	 * CONFIG1 pin, but no such luck on our hardware.
2385 	 *
2386 	 * The only method left to us is to check the VBT to see
2387 	 * if the port is a dual mode capable DP port.
2388 	 */
2389 	if (type == DRM_DP_DUAL_MODE_UNKNOWN) {
2390 		if (!connector->force &&
2391 		    intel_bios_is_port_dp_dual_mode(dev_priv, port)) {
2392 			drm_dbg_kms(&dev_priv->drm,
2393 				    "Assuming DP dual mode adaptor presence based on VBT\n");
2394 			type = DRM_DP_DUAL_MODE_TYPE1_DVI;
2395 		} else {
2396 			type = DRM_DP_DUAL_MODE_NONE;
2397 		}
2398 	}
2399 
2400 	if (type == DRM_DP_DUAL_MODE_NONE)
2401 		return;
2402 
2403 	hdmi->dp_dual_mode.type = type;
2404 	hdmi->dp_dual_mode.max_tmds_clock =
2405 		drm_dp_dual_mode_max_tmds_clock(&dev_priv->drm, type, adapter);
2406 
2407 	drm_dbg_kms(&dev_priv->drm,
2408 		    "DP dual mode adaptor (%s) detected (max TMDS clock: %d kHz)\n",
2409 		    drm_dp_get_dual_mode_type_name(type),
2410 		    hdmi->dp_dual_mode.max_tmds_clock);
2411 
2412 	/* Older VBTs are often buggy and can't be trusted :( Play it safe. */
2413 	if ((DISPLAY_VER(dev_priv) >= 8 || IS_HASWELL(dev_priv)) &&
2414 	    !intel_bios_is_port_dp_dual_mode(dev_priv, port)) {
2415 		drm_dbg_kms(&dev_priv->drm,
2416 			    "Ignoring DP dual mode adaptor max TMDS clock for native HDMI port\n");
2417 		hdmi->dp_dual_mode.max_tmds_clock = 0;
2418 	}
2419 }
2420 
2421 static bool
2422 intel_hdmi_set_edid(struct drm_connector *connector)
2423 {
2424 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
2425 	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(to_intel_connector(connector));
2426 	intel_wakeref_t wakeref;
2427 	const struct drm_edid *drm_edid;
2428 	const struct edid *edid;
2429 	bool connected = false;
2430 	struct i2c_adapter *i2c;
2431 
2432 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
2433 
2434 	i2c = intel_gmbus_get_adapter(dev_priv, intel_hdmi->ddc_bus);
2435 
2436 	drm_edid = drm_edid_read_ddc(connector, i2c);
2437 
2438 	if (!drm_edid && !intel_gmbus_is_forced_bit(i2c)) {
2439 		drm_dbg_kms(&dev_priv->drm,
2440 			    "HDMI GMBUS EDID read failed, retry using GPIO bit-banging\n");
2441 		intel_gmbus_force_bit(i2c, true);
2442 		drm_edid = drm_edid_read_ddc(connector, i2c);
2443 		intel_gmbus_force_bit(i2c, false);
2444 	}
2445 
2446 	/* Below we depend on display info having been updated */
2447 	drm_edid_connector_update(connector, drm_edid);
2448 
2449 	to_intel_connector(connector)->detect_edid = drm_edid;
2450 
2451 	/* FIXME: Get rid of drm_edid_raw() */
2452 	edid = drm_edid_raw(drm_edid);
2453 	if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
2454 		intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
2455 		intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
2456 
2457 		intel_hdmi_dp_dual_mode_detect(connector);
2458 
2459 		connected = true;
2460 	}
2461 
2462 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS, wakeref);
2463 
2464 	cec_notifier_set_phys_addr_from_edid(intel_hdmi->cec_notifier, edid);
2465 
2466 	return connected;
2467 }
2468 
2469 static enum drm_connector_status
2470 intel_hdmi_detect(struct drm_connector *connector, bool force)
2471 {
2472 	enum drm_connector_status status = connector_status_disconnected;
2473 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
2474 	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(to_intel_connector(connector));
2475 	struct intel_encoder *encoder = &hdmi_to_dig_port(intel_hdmi)->base;
2476 	intel_wakeref_t wakeref;
2477 
2478 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s]\n",
2479 		    connector->base.id, connector->name);
2480 
2481 	if (!INTEL_DISPLAY_ENABLED(dev_priv))
2482 		return connector_status_disconnected;
2483 
2484 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
2485 
2486 	if (DISPLAY_VER(dev_priv) >= 11 &&
2487 	    !intel_digital_port_connected(encoder))
2488 		goto out;
2489 
2490 	intel_hdmi_unset_edid(connector);
2491 
2492 	if (intel_hdmi_set_edid(connector))
2493 		status = connector_status_connected;
2494 
2495 out:
2496 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS, wakeref);
2497 
2498 	if (status != connector_status_connected)
2499 		cec_notifier_phys_addr_invalidate(intel_hdmi->cec_notifier);
2500 
2501 	/*
2502 	 * Make sure the refs for power wells enabled during detect are
2503 	 * dropped to avoid a new detect cycle triggered by HPD polling.
2504 	 */
2505 	intel_display_power_flush_work(dev_priv);
2506 
2507 	return status;
2508 }
2509 
2510 static void
2511 intel_hdmi_force(struct drm_connector *connector)
2512 {
2513 	struct drm_i915_private *i915 = to_i915(connector->dev);
2514 
2515 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
2516 		    connector->base.id, connector->name);
2517 
2518 	intel_hdmi_unset_edid(connector);
2519 
2520 	if (connector->status != connector_status_connected)
2521 		return;
2522 
2523 	intel_hdmi_set_edid(connector);
2524 }
2525 
2526 static int intel_hdmi_get_modes(struct drm_connector *connector)
2527 {
2528 	/* drm_edid_connector_update() done in ->detect() or ->force() */
2529 	return drm_edid_connector_add_modes(connector);
2530 }
2531 
2532 static struct i2c_adapter *
2533 intel_hdmi_get_i2c_adapter(struct drm_connector *connector)
2534 {
2535 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
2536 	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(to_intel_connector(connector));
2537 
2538 	return intel_gmbus_get_adapter(dev_priv, intel_hdmi->ddc_bus);
2539 }
2540 
2541 static void intel_hdmi_create_i2c_symlink(struct drm_connector *connector)
2542 {
2543 	struct drm_i915_private *i915 = to_i915(connector->dev);
2544 	struct i2c_adapter *adapter = intel_hdmi_get_i2c_adapter(connector);
2545 	struct kobject *i2c_kobj = &adapter->dev.kobj;
2546 	struct kobject *connector_kobj = &connector->kdev->kobj;
2547 	int ret;
2548 
2549 	ret = sysfs_create_link(connector_kobj, i2c_kobj, i2c_kobj->name);
2550 	if (ret)
2551 		drm_err(&i915->drm, "Failed to create i2c symlink (%d)\n", ret);
2552 }
2553 
2554 static void intel_hdmi_remove_i2c_symlink(struct drm_connector *connector)
2555 {
2556 	struct i2c_adapter *adapter = intel_hdmi_get_i2c_adapter(connector);
2557 	struct kobject *i2c_kobj = &adapter->dev.kobj;
2558 	struct kobject *connector_kobj = &connector->kdev->kobj;
2559 
2560 	sysfs_remove_link(connector_kobj, i2c_kobj->name);
2561 }
2562 
2563 static int
2564 intel_hdmi_connector_register(struct drm_connector *connector)
2565 {
2566 	int ret;
2567 
2568 	ret = intel_connector_register(connector);
2569 	if (ret)
2570 		return ret;
2571 
2572 	intel_hdmi_create_i2c_symlink(connector);
2573 
2574 	return ret;
2575 }
2576 
2577 static void intel_hdmi_connector_unregister(struct drm_connector *connector)
2578 {
2579 	struct cec_notifier *n = intel_attached_hdmi(to_intel_connector(connector))->cec_notifier;
2580 
2581 	cec_notifier_conn_unregister(n);
2582 
2583 	intel_hdmi_remove_i2c_symlink(connector);
2584 	intel_connector_unregister(connector);
2585 }
2586 
2587 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
2588 	.detect = intel_hdmi_detect,
2589 	.force = intel_hdmi_force,
2590 	.fill_modes = drm_helper_probe_single_connector_modes,
2591 	.atomic_get_property = intel_digital_connector_atomic_get_property,
2592 	.atomic_set_property = intel_digital_connector_atomic_set_property,
2593 	.late_register = intel_hdmi_connector_register,
2594 	.early_unregister = intel_hdmi_connector_unregister,
2595 	.destroy = intel_connector_destroy,
2596 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2597 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
2598 };
2599 
2600 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
2601 	.get_modes = intel_hdmi_get_modes,
2602 	.mode_valid = intel_hdmi_mode_valid,
2603 	.atomic_check = intel_digital_connector_atomic_check,
2604 };
2605 
2606 static void
2607 intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
2608 {
2609 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
2610 
2611 	intel_attach_force_audio_property(connector);
2612 	intel_attach_broadcast_rgb_property(connector);
2613 	intel_attach_aspect_ratio_property(connector);
2614 
2615 	intel_attach_hdmi_colorspace_property(connector);
2616 	drm_connector_attach_content_type_property(connector);
2617 
2618 	if (DISPLAY_VER(dev_priv) >= 10)
2619 		drm_connector_attach_hdr_output_metadata_property(connector);
2620 
2621 	if (!HAS_GMCH(dev_priv))
2622 		drm_connector_attach_max_bpc_property(connector, 8, 12);
2623 }
2624 
2625 /*
2626  * intel_hdmi_handle_sink_scrambling: handle sink scrambling/clock ratio setup
2627  * @encoder: intel_encoder
2628  * @connector: drm_connector
2629  * @high_tmds_clock_ratio = bool to indicate if the function needs to set
2630  *  or reset the high tmds clock ratio for scrambling
2631  * @scrambling: bool to Indicate if the function needs to set or reset
2632  *  sink scrambling
2633  *
2634  * This function handles scrambling on HDMI 2.0 capable sinks.
2635  * If required clock rate is > 340 Mhz && scrambling is supported by sink
2636  * it enables scrambling. This should be called before enabling the HDMI
2637  * 2.0 port, as the sink can choose to disable the scrambling if it doesn't
2638  * detect a scrambled clock within 100 ms.
2639  *
2640  * Returns:
2641  * True on success, false on failure.
2642  */
2643 bool intel_hdmi_handle_sink_scrambling(struct intel_encoder *encoder,
2644 				       struct drm_connector *connector,
2645 				       bool high_tmds_clock_ratio,
2646 				       bool scrambling)
2647 {
2648 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2649 	struct drm_scrambling *sink_scrambling =
2650 		&connector->display_info.hdmi.scdc.scrambling;
2651 
2652 	if (!sink_scrambling->supported)
2653 		return true;
2654 
2655 	drm_dbg_kms(&dev_priv->drm,
2656 		    "[CONNECTOR:%d:%s] scrambling=%s, TMDS bit clock ratio=1/%d\n",
2657 		    connector->base.id, connector->name,
2658 		    str_yes_no(scrambling), high_tmds_clock_ratio ? 40 : 10);
2659 
2660 	/* Set TMDS bit clock ratio to 1/40 or 1/10, and enable/disable scrambling */
2661 	return drm_scdc_set_high_tmds_clock_ratio(connector, high_tmds_clock_ratio) &&
2662 		drm_scdc_set_scrambling(connector, scrambling);
2663 }
2664 
2665 static u8 chv_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2666 {
2667 	u8 ddc_pin;
2668 
2669 	switch (port) {
2670 	case PORT_B:
2671 		ddc_pin = GMBUS_PIN_DPB;
2672 		break;
2673 	case PORT_C:
2674 		ddc_pin = GMBUS_PIN_DPC;
2675 		break;
2676 	case PORT_D:
2677 		ddc_pin = GMBUS_PIN_DPD_CHV;
2678 		break;
2679 	default:
2680 		MISSING_CASE(port);
2681 		ddc_pin = GMBUS_PIN_DPB;
2682 		break;
2683 	}
2684 	return ddc_pin;
2685 }
2686 
2687 static u8 bxt_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2688 {
2689 	u8 ddc_pin;
2690 
2691 	switch (port) {
2692 	case PORT_B:
2693 		ddc_pin = GMBUS_PIN_1_BXT;
2694 		break;
2695 	case PORT_C:
2696 		ddc_pin = GMBUS_PIN_2_BXT;
2697 		break;
2698 	default:
2699 		MISSING_CASE(port);
2700 		ddc_pin = GMBUS_PIN_1_BXT;
2701 		break;
2702 	}
2703 	return ddc_pin;
2704 }
2705 
2706 static u8 cnp_port_to_ddc_pin(struct drm_i915_private *dev_priv,
2707 			      enum port port)
2708 {
2709 	u8 ddc_pin;
2710 
2711 	switch (port) {
2712 	case PORT_B:
2713 		ddc_pin = GMBUS_PIN_1_BXT;
2714 		break;
2715 	case PORT_C:
2716 		ddc_pin = GMBUS_PIN_2_BXT;
2717 		break;
2718 	case PORT_D:
2719 		ddc_pin = GMBUS_PIN_4_CNP;
2720 		break;
2721 	case PORT_F:
2722 		ddc_pin = GMBUS_PIN_3_BXT;
2723 		break;
2724 	default:
2725 		MISSING_CASE(port);
2726 		ddc_pin = GMBUS_PIN_1_BXT;
2727 		break;
2728 	}
2729 	return ddc_pin;
2730 }
2731 
2732 static u8 icl_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2733 {
2734 	enum phy phy = intel_port_to_phy(dev_priv, port);
2735 
2736 	if (intel_phy_is_combo(dev_priv, phy))
2737 		return GMBUS_PIN_1_BXT + port;
2738 	else if (intel_phy_is_tc(dev_priv, phy))
2739 		return GMBUS_PIN_9_TC1_ICP + intel_port_to_tc(dev_priv, port);
2740 
2741 	drm_WARN(&dev_priv->drm, 1, "Unknown port:%c\n", port_name(port));
2742 	return GMBUS_PIN_2_BXT;
2743 }
2744 
2745 static u8 mcc_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2746 {
2747 	enum phy phy = intel_port_to_phy(dev_priv, port);
2748 	u8 ddc_pin;
2749 
2750 	switch (phy) {
2751 	case PHY_A:
2752 		ddc_pin = GMBUS_PIN_1_BXT;
2753 		break;
2754 	case PHY_B:
2755 		ddc_pin = GMBUS_PIN_2_BXT;
2756 		break;
2757 	case PHY_C:
2758 		ddc_pin = GMBUS_PIN_9_TC1_ICP;
2759 		break;
2760 	default:
2761 		MISSING_CASE(phy);
2762 		ddc_pin = GMBUS_PIN_1_BXT;
2763 		break;
2764 	}
2765 	return ddc_pin;
2766 }
2767 
2768 static u8 rkl_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2769 {
2770 	enum phy phy = intel_port_to_phy(dev_priv, port);
2771 
2772 	WARN_ON(port == PORT_C);
2773 
2774 	/*
2775 	 * Pin mapping for RKL depends on which PCH is present.  With TGP, the
2776 	 * final two outputs use type-c pins, even though they're actually
2777 	 * combo outputs.  With CMP, the traditional DDI A-D pins are used for
2778 	 * all outputs.
2779 	 */
2780 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_TGP && phy >= PHY_C)
2781 		return GMBUS_PIN_9_TC1_ICP + phy - PHY_C;
2782 
2783 	return GMBUS_PIN_1_BXT + phy;
2784 }
2785 
2786 static u8 gen9bc_tgp_port_to_ddc_pin(struct drm_i915_private *i915, enum port port)
2787 {
2788 	enum phy phy = intel_port_to_phy(i915, port);
2789 
2790 	drm_WARN_ON(&i915->drm, port == PORT_A);
2791 
2792 	/*
2793 	 * Pin mapping for GEN9 BC depends on which PCH is present.  With TGP,
2794 	 * final two outputs use type-c pins, even though they're actually
2795 	 * combo outputs.  With CMP, the traditional DDI A-D pins are used for
2796 	 * all outputs.
2797 	 */
2798 	if (INTEL_PCH_TYPE(i915) >= PCH_TGP && phy >= PHY_C)
2799 		return GMBUS_PIN_9_TC1_ICP + phy - PHY_C;
2800 
2801 	return GMBUS_PIN_1_BXT + phy;
2802 }
2803 
2804 static u8 dg1_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2805 {
2806 	return intel_port_to_phy(dev_priv, port) + 1;
2807 }
2808 
2809 static u8 adls_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2810 {
2811 	enum phy phy = intel_port_to_phy(dev_priv, port);
2812 
2813 	WARN_ON(port == PORT_B || port == PORT_C);
2814 
2815 	/*
2816 	 * Pin mapping for ADL-S requires TC pins for all combo phy outputs
2817 	 * except first combo output.
2818 	 */
2819 	if (phy == PHY_A)
2820 		return GMBUS_PIN_1_BXT;
2821 
2822 	return GMBUS_PIN_9_TC1_ICP + phy - PHY_B;
2823 }
2824 
2825 static u8 g4x_port_to_ddc_pin(struct drm_i915_private *dev_priv,
2826 			      enum port port)
2827 {
2828 	u8 ddc_pin;
2829 
2830 	switch (port) {
2831 	case PORT_B:
2832 		ddc_pin = GMBUS_PIN_DPB;
2833 		break;
2834 	case PORT_C:
2835 		ddc_pin = GMBUS_PIN_DPC;
2836 		break;
2837 	case PORT_D:
2838 		ddc_pin = GMBUS_PIN_DPD;
2839 		break;
2840 	default:
2841 		MISSING_CASE(port);
2842 		ddc_pin = GMBUS_PIN_DPB;
2843 		break;
2844 	}
2845 	return ddc_pin;
2846 }
2847 
2848 static u8 intel_hdmi_ddc_pin(struct intel_encoder *encoder)
2849 {
2850 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2851 	enum port port = encoder->port;
2852 	u8 ddc_pin;
2853 
2854 	ddc_pin = intel_bios_hdmi_ddc_pin(encoder->devdata);
2855 	if (ddc_pin) {
2856 		drm_dbg_kms(&dev_priv->drm,
2857 			    "[ENCODER:%d:%s] Using DDC pin 0x%x (VBT)\n",
2858 			    encoder->base.base.id, encoder->base.name,
2859 			    ddc_pin);
2860 		return ddc_pin;
2861 	}
2862 
2863 	if (IS_ALDERLAKE_S(dev_priv))
2864 		ddc_pin = adls_port_to_ddc_pin(dev_priv, port);
2865 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_DG1)
2866 		ddc_pin = dg1_port_to_ddc_pin(dev_priv, port);
2867 	else if (IS_ROCKETLAKE(dev_priv))
2868 		ddc_pin = rkl_port_to_ddc_pin(dev_priv, port);
2869 	else if (DISPLAY_VER(dev_priv) == 9 && HAS_PCH_TGP(dev_priv))
2870 		ddc_pin = gen9bc_tgp_port_to_ddc_pin(dev_priv, port);
2871 	else if (IS_JSL_EHL(dev_priv) && HAS_PCH_TGP(dev_priv))
2872 		ddc_pin = mcc_port_to_ddc_pin(dev_priv, port);
2873 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2874 		ddc_pin = icl_port_to_ddc_pin(dev_priv, port);
2875 	else if (HAS_PCH_CNP(dev_priv))
2876 		ddc_pin = cnp_port_to_ddc_pin(dev_priv, port);
2877 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
2878 		ddc_pin = bxt_port_to_ddc_pin(dev_priv, port);
2879 	else if (IS_CHERRYVIEW(dev_priv))
2880 		ddc_pin = chv_port_to_ddc_pin(dev_priv, port);
2881 	else
2882 		ddc_pin = g4x_port_to_ddc_pin(dev_priv, port);
2883 
2884 	drm_dbg_kms(&dev_priv->drm,
2885 		    "[ENCODER:%d:%s] Using DDC pin 0x%x (platform default)\n",
2886 		    encoder->base.base.id, encoder->base.name,
2887 		    ddc_pin);
2888 
2889 	return ddc_pin;
2890 }
2891 
2892 void intel_infoframe_init(struct intel_digital_port *dig_port)
2893 {
2894 	struct drm_i915_private *dev_priv =
2895 		to_i915(dig_port->base.base.dev);
2896 
2897 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2898 		dig_port->write_infoframe = vlv_write_infoframe;
2899 		dig_port->read_infoframe = vlv_read_infoframe;
2900 		dig_port->set_infoframes = vlv_set_infoframes;
2901 		dig_port->infoframes_enabled = vlv_infoframes_enabled;
2902 	} else if (IS_G4X(dev_priv)) {
2903 		dig_port->write_infoframe = g4x_write_infoframe;
2904 		dig_port->read_infoframe = g4x_read_infoframe;
2905 		dig_port->set_infoframes = g4x_set_infoframes;
2906 		dig_port->infoframes_enabled = g4x_infoframes_enabled;
2907 	} else if (HAS_DDI(dev_priv)) {
2908 		if (intel_bios_encoder_is_lspcon(dig_port->base.devdata)) {
2909 			dig_port->write_infoframe = lspcon_write_infoframe;
2910 			dig_port->read_infoframe = lspcon_read_infoframe;
2911 			dig_port->set_infoframes = lspcon_set_infoframes;
2912 			dig_port->infoframes_enabled = lspcon_infoframes_enabled;
2913 		} else {
2914 			dig_port->write_infoframe = hsw_write_infoframe;
2915 			dig_port->read_infoframe = hsw_read_infoframe;
2916 			dig_port->set_infoframes = hsw_set_infoframes;
2917 			dig_port->infoframes_enabled = hsw_infoframes_enabled;
2918 		}
2919 	} else if (HAS_PCH_IBX(dev_priv)) {
2920 		dig_port->write_infoframe = ibx_write_infoframe;
2921 		dig_port->read_infoframe = ibx_read_infoframe;
2922 		dig_port->set_infoframes = ibx_set_infoframes;
2923 		dig_port->infoframes_enabled = ibx_infoframes_enabled;
2924 	} else {
2925 		dig_port->write_infoframe = cpt_write_infoframe;
2926 		dig_port->read_infoframe = cpt_read_infoframe;
2927 		dig_port->set_infoframes = cpt_set_infoframes;
2928 		dig_port->infoframes_enabled = cpt_infoframes_enabled;
2929 	}
2930 }
2931 
2932 void intel_hdmi_init_connector(struct intel_digital_port *dig_port,
2933 			       struct intel_connector *intel_connector)
2934 {
2935 	struct drm_connector *connector = &intel_connector->base;
2936 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2937 	struct intel_encoder *intel_encoder = &dig_port->base;
2938 	struct drm_device *dev = intel_encoder->base.dev;
2939 	struct drm_i915_private *dev_priv = to_i915(dev);
2940 	struct i2c_adapter *ddc;
2941 	enum port port = intel_encoder->port;
2942 	struct cec_connector_info conn_info;
2943 
2944 	drm_dbg_kms(&dev_priv->drm,
2945 		    "Adding HDMI connector on [ENCODER:%d:%s]\n",
2946 		    intel_encoder->base.base.id, intel_encoder->base.name);
2947 
2948 	if (DISPLAY_VER(dev_priv) < 12 && drm_WARN_ON(dev, port == PORT_A))
2949 		return;
2950 
2951 	if (drm_WARN(dev, dig_port->max_lanes < 4,
2952 		     "Not enough lanes (%d) for HDMI on [ENCODER:%d:%s]\n",
2953 		     dig_port->max_lanes, intel_encoder->base.base.id,
2954 		     intel_encoder->base.name))
2955 		return;
2956 
2957 	intel_hdmi->ddc_bus = intel_hdmi_ddc_pin(intel_encoder);
2958 	ddc = intel_gmbus_get_adapter(dev_priv, intel_hdmi->ddc_bus);
2959 
2960 	drm_connector_init_with_ddc(dev, connector,
2961 				    &intel_hdmi_connector_funcs,
2962 				    DRM_MODE_CONNECTOR_HDMIA,
2963 				    ddc);
2964 	drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
2965 
2966 	if (DISPLAY_VER(dev_priv) < 12)
2967 		connector->interlace_allowed = true;
2968 
2969 	connector->stereo_allowed = true;
2970 
2971 	if (DISPLAY_VER(dev_priv) >= 10)
2972 		connector->ycbcr_420_allowed = true;
2973 
2974 	intel_connector->polled = DRM_CONNECTOR_POLL_HPD;
2975 
2976 	if (HAS_DDI(dev_priv))
2977 		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
2978 	else
2979 		intel_connector->get_hw_state = intel_connector_get_hw_state;
2980 
2981 	intel_hdmi_add_properties(intel_hdmi, connector);
2982 
2983 	intel_connector_attach_encoder(intel_connector, intel_encoder);
2984 	intel_hdmi->attached_connector = intel_connector;
2985 
2986 	if (is_hdcp_supported(dev_priv, port)) {
2987 		int ret = intel_hdcp_init(intel_connector, dig_port,
2988 					  &intel_hdmi_hdcp_shim);
2989 		if (ret)
2990 			drm_dbg_kms(&dev_priv->drm,
2991 				    "HDCP init failed, skipping.\n");
2992 	}
2993 
2994 	/* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2995 	 * 0xd.  Failure to do so will result in spurious interrupts being
2996 	 * generated on the port when a cable is not attached.
2997 	 */
2998 	if (IS_G45(dev_priv)) {
2999 		u32 temp = intel_de_read(dev_priv, PEG_BAND_GAP_DATA);
3000 		intel_de_write(dev_priv, PEG_BAND_GAP_DATA,
3001 		               (temp & ~0xf) | 0xd);
3002 	}
3003 
3004 	cec_fill_conn_info_from_drm(&conn_info, connector);
3005 
3006 	intel_hdmi->cec_notifier =
3007 		cec_notifier_conn_register(dev->dev, port_identifier(port),
3008 					   &conn_info);
3009 	if (!intel_hdmi->cec_notifier)
3010 		drm_dbg_kms(&dev_priv->drm, "CEC notifier get failed\n");
3011 }
3012 
3013 /*
3014  * intel_hdmi_dsc_get_slice_height - get the dsc slice_height
3015  * @vactive: Vactive of a display mode
3016  *
3017  * @return: appropriate dsc slice height for a given mode.
3018  */
3019 int intel_hdmi_dsc_get_slice_height(int vactive)
3020 {
3021 	int slice_height;
3022 
3023 	/*
3024 	 * Slice Height determination : HDMI2.1 Section 7.7.5.2
3025 	 * Select smallest slice height >=96, that results in a valid PPS and
3026 	 * requires minimum padding lines required for final slice.
3027 	 *
3028 	 * Assumption : Vactive is even.
3029 	 */
3030 	for (slice_height = 96; slice_height <= vactive; slice_height += 2)
3031 		if (vactive % slice_height == 0)
3032 			return slice_height;
3033 
3034 	return 0;
3035 }
3036 
3037 /*
3038  * intel_hdmi_dsc_get_num_slices - get no. of dsc slices based on dsc encoder
3039  * and dsc decoder capabilities
3040  *
3041  * @crtc_state: intel crtc_state
3042  * @src_max_slices: maximum slices supported by the DSC encoder
3043  * @src_max_slice_width: maximum slice width supported by DSC encoder
3044  * @hdmi_max_slices: maximum slices supported by sink DSC decoder
3045  * @hdmi_throughput: maximum clock per slice (MHz) supported by HDMI sink
3046  *
3047  * @return: num of dsc slices that can be supported by the dsc encoder
3048  * and decoder.
3049  */
3050 int
3051 intel_hdmi_dsc_get_num_slices(const struct intel_crtc_state *crtc_state,
3052 			      int src_max_slices, int src_max_slice_width,
3053 			      int hdmi_max_slices, int hdmi_throughput)
3054 {
3055 /* Pixel rates in KPixels/sec */
3056 #define HDMI_DSC_PEAK_PIXEL_RATE		2720000
3057 /*
3058  * Rates at which the source and sink are required to process pixels in each
3059  * slice, can be two levels: either atleast 340000KHz or atleast 40000KHz.
3060  */
3061 #define HDMI_DSC_MAX_ENC_THROUGHPUT_0		340000
3062 #define HDMI_DSC_MAX_ENC_THROUGHPUT_1		400000
3063 
3064 /* Spec limits the slice width to 2720 pixels */
3065 #define MAX_HDMI_SLICE_WIDTH			2720
3066 	int kslice_adjust;
3067 	int adjusted_clk_khz;
3068 	int min_slices;
3069 	int target_slices;
3070 	int max_throughput; /* max clock freq. in khz per slice */
3071 	int max_slice_width;
3072 	int slice_width;
3073 	int pixel_clock = crtc_state->hw.adjusted_mode.crtc_clock;
3074 
3075 	if (!hdmi_throughput)
3076 		return 0;
3077 
3078 	/*
3079 	 * Slice Width determination : HDMI2.1 Section 7.7.5.1
3080 	 * kslice_adjust factor for 4:2:0, and 4:2:2 formats is 0.5, where as
3081 	 * for 4:4:4 is 1.0. Multiplying these factors by 10 and later
3082 	 * dividing adjusted clock value by 10.
3083 	 */
3084 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444 ||
3085 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_RGB)
3086 		kslice_adjust = 10;
3087 	else
3088 		kslice_adjust = 5;
3089 
3090 	/*
3091 	 * As per spec, the rate at which the source and the sink process
3092 	 * the pixels per slice are at two levels: atleast 340Mhz or 400Mhz.
3093 	 * This depends upon the pixel clock rate and output formats
3094 	 * (kslice adjust).
3095 	 * If pixel clock * kslice adjust >= 2720MHz slices can be processed
3096 	 * at max 340MHz, otherwise they can be processed at max 400MHz.
3097 	 */
3098 
3099 	adjusted_clk_khz = DIV_ROUND_UP(kslice_adjust * pixel_clock, 10);
3100 
3101 	if (adjusted_clk_khz <= HDMI_DSC_PEAK_PIXEL_RATE)
3102 		max_throughput = HDMI_DSC_MAX_ENC_THROUGHPUT_0;
3103 	else
3104 		max_throughput = HDMI_DSC_MAX_ENC_THROUGHPUT_1;
3105 
3106 	/*
3107 	 * Taking into account the sink's capability for maximum
3108 	 * clock per slice (in MHz) as read from HF-VSDB.
3109 	 */
3110 	max_throughput = min(max_throughput, hdmi_throughput * 1000);
3111 
3112 	min_slices = DIV_ROUND_UP(adjusted_clk_khz, max_throughput);
3113 	max_slice_width = min(MAX_HDMI_SLICE_WIDTH, src_max_slice_width);
3114 
3115 	/*
3116 	 * Keep on increasing the num of slices/line, starting from min_slices
3117 	 * per line till we get such a number, for which the slice_width is
3118 	 * just less than max_slice_width. The slices/line selected should be
3119 	 * less than or equal to the max horizontal slices that the combination
3120 	 * of PCON encoder and HDMI decoder can support.
3121 	 */
3122 	slice_width = max_slice_width;
3123 
3124 	do {
3125 		if (min_slices <= 1 && src_max_slices >= 1 && hdmi_max_slices >= 1)
3126 			target_slices = 1;
3127 		else if (min_slices <= 2 && src_max_slices >= 2 && hdmi_max_slices >= 2)
3128 			target_slices = 2;
3129 		else if (min_slices <= 4 && src_max_slices >= 4 && hdmi_max_slices >= 4)
3130 			target_slices = 4;
3131 		else if (min_slices <= 8 && src_max_slices >= 8 && hdmi_max_slices >= 8)
3132 			target_slices = 8;
3133 		else if (min_slices <= 12 && src_max_slices >= 12 && hdmi_max_slices >= 12)
3134 			target_slices = 12;
3135 		else if (min_slices <= 16 && src_max_slices >= 16 && hdmi_max_slices >= 16)
3136 			target_slices = 16;
3137 		else
3138 			return 0;
3139 
3140 		slice_width = DIV_ROUND_UP(crtc_state->hw.adjusted_mode.hdisplay, target_slices);
3141 		if (slice_width >= max_slice_width)
3142 			min_slices = target_slices + 1;
3143 	} while (slice_width >= max_slice_width);
3144 
3145 	return target_slices;
3146 }
3147 
3148 /*
3149  * intel_hdmi_dsc_get_bpp - get the appropriate compressed bits_per_pixel based on
3150  * source and sink capabilities.
3151  *
3152  * @src_fraction_bpp: fractional bpp supported by the source
3153  * @slice_width: dsc slice width supported by the source and sink
3154  * @num_slices: num of slices supported by the source and sink
3155  * @output_format: video output format
3156  * @hdmi_all_bpp: sink supports decoding of 1/16th bpp setting
3157  * @hdmi_max_chunk_bytes: max bytes in a line of chunks supported by sink
3158  *
3159  * @return: compressed bits_per_pixel in step of 1/16 of bits_per_pixel
3160  */
3161 int
3162 intel_hdmi_dsc_get_bpp(int src_fractional_bpp, int slice_width, int num_slices,
3163 		       int output_format, bool hdmi_all_bpp,
3164 		       int hdmi_max_chunk_bytes)
3165 {
3166 	int max_dsc_bpp, min_dsc_bpp;
3167 	int target_bytes;
3168 	bool bpp_found = false;
3169 	int bpp_decrement_x16;
3170 	int bpp_target;
3171 	int bpp_target_x16;
3172 
3173 	/*
3174 	 * Get min bpp and max bpp as per Table 7.23, in HDMI2.1 spec
3175 	 * Start with the max bpp and keep on decrementing with
3176 	 * fractional bpp, if supported by PCON DSC encoder
3177 	 *
3178 	 * for each bpp we check if no of bytes can be supported by HDMI sink
3179 	 */
3180 
3181 	/* Assuming: bpc as 8*/
3182 	if (output_format == INTEL_OUTPUT_FORMAT_YCBCR420) {
3183 		min_dsc_bpp = 6;
3184 		max_dsc_bpp = 3 * 4; /* 3*bpc/2 */
3185 	} else if (output_format == INTEL_OUTPUT_FORMAT_YCBCR444 ||
3186 		   output_format == INTEL_OUTPUT_FORMAT_RGB) {
3187 		min_dsc_bpp = 8;
3188 		max_dsc_bpp = 3 * 8; /* 3*bpc */
3189 	} else {
3190 		/* Assuming 4:2:2 encoding */
3191 		min_dsc_bpp = 7;
3192 		max_dsc_bpp = 2 * 8; /* 2*bpc */
3193 	}
3194 
3195 	/*
3196 	 * Taking into account if all dsc_all_bpp supported by HDMI2.1 sink
3197 	 * Section 7.7.34 : Source shall not enable compressed Video
3198 	 * Transport with bpp_target settings above 12 bpp unless
3199 	 * DSC_all_bpp is set to 1.
3200 	 */
3201 	if (!hdmi_all_bpp)
3202 		max_dsc_bpp = min(max_dsc_bpp, 12);
3203 
3204 	/*
3205 	 * The Sink has a limit of compressed data in bytes for a scanline,
3206 	 * as described in max_chunk_bytes field in HFVSDB block of edid.
3207 	 * The no. of bytes depend on the target bits per pixel that the
3208 	 * source configures. So we start with the max_bpp and calculate
3209 	 * the target_chunk_bytes. We keep on decrementing the target_bpp,
3210 	 * till we get the target_chunk_bytes just less than what the sink's
3211 	 * max_chunk_bytes, or else till we reach the min_dsc_bpp.
3212 	 *
3213 	 * The decrement is according to the fractional support from PCON DSC
3214 	 * encoder. For fractional BPP we use bpp_target as a multiple of 16.
3215 	 *
3216 	 * bpp_target_x16 = bpp_target * 16
3217 	 * So we need to decrement by {1, 2, 4, 8, 16} for fractional bpps
3218 	 * {1/16, 1/8, 1/4, 1/2, 1} respectively.
3219 	 */
3220 
3221 	bpp_target = max_dsc_bpp;
3222 
3223 	/* src does not support fractional bpp implies decrement by 16 for bppx16 */
3224 	if (!src_fractional_bpp)
3225 		src_fractional_bpp = 1;
3226 	bpp_decrement_x16 = DIV_ROUND_UP(16, src_fractional_bpp);
3227 	bpp_target_x16 = (bpp_target * 16) - bpp_decrement_x16;
3228 
3229 	while (bpp_target_x16 > (min_dsc_bpp * 16)) {
3230 		int bpp;
3231 
3232 		bpp = DIV_ROUND_UP(bpp_target_x16, 16);
3233 		target_bytes = DIV_ROUND_UP((num_slices * slice_width * bpp), 8);
3234 		if (target_bytes <= hdmi_max_chunk_bytes) {
3235 			bpp_found = true;
3236 			break;
3237 		}
3238 		bpp_target_x16 -= bpp_decrement_x16;
3239 	}
3240 	if (bpp_found)
3241 		return bpp_target_x16;
3242 
3243 	return 0;
3244 }
3245