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