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