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