Loading...
1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
28#include <linux/iopoll.h>
29#include <linux/string_helpers.h>
30
31#include <drm/display/drm_scdc_helper.h>
32#include <drm/drm_privacy_screen_consumer.h>
33
34#include "i915_drv.h"
35#include "i915_reg.h"
36#include "icl_dsi.h"
37#include "intel_audio.h"
38#include "intel_audio_regs.h"
39#include "intel_backlight.h"
40#include "intel_combo_phy.h"
41#include "intel_combo_phy_regs.h"
42#include "intel_connector.h"
43#include "intel_crtc.h"
44#include "intel_cx0_phy.h"
45#include "intel_cx0_phy_regs.h"
46#include "intel_ddi.h"
47#include "intel_ddi_buf_trans.h"
48#include "intel_de.h"
49#include "intel_display_power.h"
50#include "intel_display_types.h"
51#include "intel_dkl_phy.h"
52#include "intel_dkl_phy_regs.h"
53#include "intel_dp.h"
54#include "intel_dp_aux.h"
55#include "intel_dp_link_training.h"
56#include "intel_dp_mst.h"
57#include "intel_dp_test.h"
58#include "intel_dp_tunnel.h"
59#include "intel_dpio_phy.h"
60#include "intel_dsi.h"
61#include "intel_encoder.h"
62#include "intel_fdi.h"
63#include "intel_fifo_underrun.h"
64#include "intel_gmbus.h"
65#include "intel_hdcp.h"
66#include "intel_hdmi.h"
67#include "intel_hotplug.h"
68#include "intel_hti.h"
69#include "intel_lspcon.h"
70#include "intel_mg_phy_regs.h"
71#include "intel_modeset_lock.h"
72#include "intel_pps.h"
73#include "intel_psr.h"
74#include "intel_quirks.h"
75#include "intel_snps_phy.h"
76#include "intel_tc.h"
77#include "intel_vdsc.h"
78#include "intel_vdsc_regs.h"
79#include "skl_scaler.h"
80#include "skl_universal_plane.h"
81
82static const u8 index_to_dp_signal_levels[] = {
83 [0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
84 [1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
85 [2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
86 [3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
87 [4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
88 [5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
89 [6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
90 [7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
91 [8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
92 [9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
93};
94
95static int intel_ddi_hdmi_level(struct intel_encoder *encoder,
96 const struct intel_ddi_buf_trans *trans)
97{
98 int level;
99
100 level = intel_bios_hdmi_level_shift(encoder->devdata);
101 if (level < 0)
102 level = trans->hdmi_default_entry;
103
104 return level;
105}
106
107static bool has_buf_trans_select(struct drm_i915_private *i915)
108{
109 return DISPLAY_VER(i915) < 10 && !IS_BROXTON(i915);
110}
111
112static bool has_iboost(struct drm_i915_private *i915)
113{
114 return DISPLAY_VER(i915) == 9 && !IS_BROXTON(i915);
115}
116
117/*
118 * Starting with Haswell, DDI port buffers must be programmed with correct
119 * values in advance. This function programs the correct values for
120 * DP/eDP/FDI use cases.
121 */
122void hsw_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
123 const struct intel_crtc_state *crtc_state)
124{
125 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
126 u32 iboost_bit = 0;
127 int i, n_entries;
128 enum port port = encoder->port;
129 const struct intel_ddi_buf_trans *trans;
130
131 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
132 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
133 return;
134
135 /* If we're boosting the current, set bit 31 of trans1 */
136 if (has_iboost(dev_priv) &&
137 intel_bios_dp_boost_level(encoder->devdata))
138 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
139
140 for (i = 0; i < n_entries; i++) {
141 intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, i),
142 trans->entries[i].hsw.trans1 | iboost_bit);
143 intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, i),
144 trans->entries[i].hsw.trans2);
145 }
146}
147
148/*
149 * Starting with Haswell, DDI port buffers must be programmed with correct
150 * values in advance. This function programs the correct values for
151 * HDMI/DVI use cases.
152 */
153static void hsw_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
154 const struct intel_crtc_state *crtc_state)
155{
156 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
157 int level = intel_ddi_level(encoder, crtc_state, 0);
158 u32 iboost_bit = 0;
159 int n_entries;
160 enum port port = encoder->port;
161 const struct intel_ddi_buf_trans *trans;
162
163 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
164 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
165 return;
166
167 /* If we're boosting the current, set bit 31 of trans1 */
168 if (has_iboost(dev_priv) &&
169 intel_bios_hdmi_boost_level(encoder->devdata))
170 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
171
172 /* Entry 9 is for HDMI: */
173 intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, 9),
174 trans->entries[level].hsw.trans1 | iboost_bit);
175 intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, 9),
176 trans->entries[level].hsw.trans2);
177}
178
179static void mtl_wait_ddi_buf_idle(struct drm_i915_private *i915, enum port port)
180{
181 int ret;
182
183 /* FIXME: find out why Bspec's 100us timeout is too short */
184 ret = wait_for_us((intel_de_read(i915, XELPDP_PORT_BUF_CTL1(i915, port)) &
185 XELPDP_PORT_BUF_PHY_IDLE), 10000);
186 if (ret)
187 drm_err(&i915->drm, "Timeout waiting for DDI BUF %c to get idle\n",
188 port_name(port));
189}
190
191void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
192 enum port port)
193{
194 if (IS_BROXTON(dev_priv)) {
195 udelay(16);
196 return;
197 }
198
199 if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
200 DDI_BUF_IS_IDLE), 8))
201 drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get idle\n",
202 port_name(port));
203}
204
205static void intel_wait_ddi_buf_active(struct intel_encoder *encoder)
206{
207 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
208 enum port port = encoder->port;
209 int timeout_us;
210 int ret;
211
212 /* Wait > 518 usecs for DDI_BUF_CTL to be non idle */
213 if (DISPLAY_VER(dev_priv) < 10) {
214 usleep_range(518, 1000);
215 return;
216 }
217
218 if (DISPLAY_VER(dev_priv) >= 14) {
219 timeout_us = 10000;
220 } else if (IS_DG2(dev_priv)) {
221 timeout_us = 1200;
222 } else if (DISPLAY_VER(dev_priv) >= 12) {
223 if (intel_encoder_is_tc(encoder))
224 timeout_us = 3000;
225 else
226 timeout_us = 1000;
227 } else {
228 timeout_us = 500;
229 }
230
231 if (DISPLAY_VER(dev_priv) >= 14)
232 ret = _wait_for(!(intel_de_read(dev_priv,
233 XELPDP_PORT_BUF_CTL1(dev_priv, port)) &
234 XELPDP_PORT_BUF_PHY_IDLE),
235 timeout_us, 10, 10);
236 else
237 ret = _wait_for(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) & DDI_BUF_IS_IDLE),
238 timeout_us, 10, 10);
239
240 if (ret)
241 drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get active\n",
242 port_name(port));
243}
244
245static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
246{
247 switch (pll->info->id) {
248 case DPLL_ID_WRPLL1:
249 return PORT_CLK_SEL_WRPLL1;
250 case DPLL_ID_WRPLL2:
251 return PORT_CLK_SEL_WRPLL2;
252 case DPLL_ID_SPLL:
253 return PORT_CLK_SEL_SPLL;
254 case DPLL_ID_LCPLL_810:
255 return PORT_CLK_SEL_LCPLL_810;
256 case DPLL_ID_LCPLL_1350:
257 return PORT_CLK_SEL_LCPLL_1350;
258 case DPLL_ID_LCPLL_2700:
259 return PORT_CLK_SEL_LCPLL_2700;
260 default:
261 MISSING_CASE(pll->info->id);
262 return PORT_CLK_SEL_NONE;
263 }
264}
265
266static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
267 const struct intel_crtc_state *crtc_state)
268{
269 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
270 int clock = crtc_state->port_clock;
271 const enum intel_dpll_id id = pll->info->id;
272
273 switch (id) {
274 default:
275 /*
276 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
277 * here, so do warn if this get passed in
278 */
279 MISSING_CASE(id);
280 return DDI_CLK_SEL_NONE;
281 case DPLL_ID_ICL_TBTPLL:
282 switch (clock) {
283 case 162000:
284 return DDI_CLK_SEL_TBT_162;
285 case 270000:
286 return DDI_CLK_SEL_TBT_270;
287 case 540000:
288 return DDI_CLK_SEL_TBT_540;
289 case 810000:
290 return DDI_CLK_SEL_TBT_810;
291 default:
292 MISSING_CASE(clock);
293 return DDI_CLK_SEL_NONE;
294 }
295 case DPLL_ID_ICL_MGPLL1:
296 case DPLL_ID_ICL_MGPLL2:
297 case DPLL_ID_ICL_MGPLL3:
298 case DPLL_ID_ICL_MGPLL4:
299 case DPLL_ID_TGL_MGPLL5:
300 case DPLL_ID_TGL_MGPLL6:
301 return DDI_CLK_SEL_MG;
302 }
303}
304
305static u32 ddi_buf_phy_link_rate(int port_clock)
306{
307 switch (port_clock) {
308 case 162000:
309 return DDI_BUF_PHY_LINK_RATE(0);
310 case 216000:
311 return DDI_BUF_PHY_LINK_RATE(4);
312 case 243000:
313 return DDI_BUF_PHY_LINK_RATE(5);
314 case 270000:
315 return DDI_BUF_PHY_LINK_RATE(1);
316 case 324000:
317 return DDI_BUF_PHY_LINK_RATE(6);
318 case 432000:
319 return DDI_BUF_PHY_LINK_RATE(7);
320 case 540000:
321 return DDI_BUF_PHY_LINK_RATE(2);
322 case 810000:
323 return DDI_BUF_PHY_LINK_RATE(3);
324 default:
325 MISSING_CASE(port_clock);
326 return DDI_BUF_PHY_LINK_RATE(0);
327 }
328}
329
330static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder,
331 const struct intel_crtc_state *crtc_state)
332{
333 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
334 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
335 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
336
337 /* DDI_BUF_CTL_ENABLE will be set by intel_ddi_prepare_link_retrain() later */
338 intel_dp->DP = dig_port->saved_port_bits |
339 DDI_PORT_WIDTH(crtc_state->lane_count) |
340 DDI_BUF_TRANS_SELECT(0);
341
342 if (DISPLAY_VER(i915) >= 14) {
343 if (intel_dp_is_uhbr(crtc_state))
344 intel_dp->DP |= DDI_BUF_PORT_DATA_40BIT;
345 else
346 intel_dp->DP |= DDI_BUF_PORT_DATA_10BIT;
347 }
348
349 if (IS_ALDERLAKE_P(i915) && intel_encoder_is_tc(encoder)) {
350 intel_dp->DP |= ddi_buf_phy_link_rate(crtc_state->port_clock);
351 if (!intel_tc_port_in_tbt_alt_mode(dig_port))
352 intel_dp->DP |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
353 }
354}
355
356static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
357 enum port port)
358{
359 u32 val = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
360
361 switch (val) {
362 case DDI_CLK_SEL_NONE:
363 return 0;
364 case DDI_CLK_SEL_TBT_162:
365 return 162000;
366 case DDI_CLK_SEL_TBT_270:
367 return 270000;
368 case DDI_CLK_SEL_TBT_540:
369 return 540000;
370 case DDI_CLK_SEL_TBT_810:
371 return 810000;
372 default:
373 MISSING_CASE(val);
374 return 0;
375 }
376}
377
378static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
379{
380 /* CRT dotclock is determined via other means */
381 if (pipe_config->has_pch_encoder)
382 return;
383
384 pipe_config->hw.adjusted_mode.crtc_clock =
385 intel_crtc_dotclock(pipe_config);
386}
387
388void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
389 const struct drm_connector_state *conn_state)
390{
391 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
392 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
393 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
394 u32 temp;
395
396 if (!intel_crtc_has_dp_encoder(crtc_state))
397 return;
398
399 drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
400
401 temp = DP_MSA_MISC_SYNC_CLOCK;
402
403 switch (crtc_state->pipe_bpp) {
404 case 18:
405 temp |= DP_MSA_MISC_6_BPC;
406 break;
407 case 24:
408 temp |= DP_MSA_MISC_8_BPC;
409 break;
410 case 30:
411 temp |= DP_MSA_MISC_10_BPC;
412 break;
413 case 36:
414 temp |= DP_MSA_MISC_12_BPC;
415 break;
416 default:
417 MISSING_CASE(crtc_state->pipe_bpp);
418 break;
419 }
420
421 /* nonsense combination */
422 drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
423 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
424
425 if (crtc_state->limited_color_range)
426 temp |= DP_MSA_MISC_COLOR_CEA_RGB;
427
428 /*
429 * As per DP 1.2 spec section 2.3.4.3 while sending
430 * YCBCR 444 signals we should program MSA MISC1/0 fields with
431 * colorspace information.
432 */
433 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
434 temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
435
436 /*
437 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
438 * of Color Encoding Format and Content Color Gamut] while sending
439 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
440 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
441 */
442 if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
443 temp |= DP_MSA_MISC_COLOR_VSC_SDP;
444
445 intel_de_write(dev_priv, TRANS_MSA_MISC(dev_priv, cpu_transcoder),
446 temp);
447}
448
449static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
450{
451 if (master_transcoder == TRANSCODER_EDP)
452 return 0;
453 else
454 return master_transcoder + 1;
455}
456
457static void
458intel_ddi_config_transcoder_dp2(struct intel_encoder *encoder,
459 const struct intel_crtc_state *crtc_state)
460{
461 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
462 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
463 u32 val = 0;
464
465 if (intel_dp_is_uhbr(crtc_state))
466 val = TRANS_DP2_128B132B_CHANNEL_CODING;
467
468 intel_de_write(i915, TRANS_DP2_CTL(cpu_transcoder), val);
469}
470
471/*
472 * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
473 *
474 * Only intended to be used by intel_ddi_enable_transcoder_func() and
475 * intel_ddi_config_transcoder_func().
476 */
477static u32
478intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
479 const struct intel_crtc_state *crtc_state)
480{
481 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
482 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
483 enum pipe pipe = crtc->pipe;
484 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
485 enum port port = encoder->port;
486 u32 temp;
487
488 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
489 temp = TRANS_DDI_FUNC_ENABLE;
490 if (DISPLAY_VER(dev_priv) >= 12)
491 temp |= TGL_TRANS_DDI_SELECT_PORT(port);
492 else
493 temp |= TRANS_DDI_SELECT_PORT(port);
494
495 switch (crtc_state->pipe_bpp) {
496 default:
497 MISSING_CASE(crtc_state->pipe_bpp);
498 fallthrough;
499 case 18:
500 temp |= TRANS_DDI_BPC_6;
501 break;
502 case 24:
503 temp |= TRANS_DDI_BPC_8;
504 break;
505 case 30:
506 temp |= TRANS_DDI_BPC_10;
507 break;
508 case 36:
509 temp |= TRANS_DDI_BPC_12;
510 break;
511 }
512
513 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
514 temp |= TRANS_DDI_PVSYNC;
515 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
516 temp |= TRANS_DDI_PHSYNC;
517
518 if (cpu_transcoder == TRANSCODER_EDP) {
519 switch (pipe) {
520 default:
521 MISSING_CASE(pipe);
522 fallthrough;
523 case PIPE_A:
524 /* On Haswell, can only use the always-on power well for
525 * eDP when not using the panel fitter, and when not
526 * using motion blur mitigation (which we don't
527 * support). */
528 if (crtc_state->pch_pfit.force_thru)
529 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
530 else
531 temp |= TRANS_DDI_EDP_INPUT_A_ON;
532 break;
533 case PIPE_B:
534 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
535 break;
536 case PIPE_C:
537 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
538 break;
539 }
540 }
541
542 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
543 if (crtc_state->has_hdmi_sink)
544 temp |= TRANS_DDI_MODE_SELECT_HDMI;
545 else
546 temp |= TRANS_DDI_MODE_SELECT_DVI;
547
548 if (crtc_state->hdmi_scrambling)
549 temp |= TRANS_DDI_HDMI_SCRAMBLING;
550 if (crtc_state->hdmi_high_tmds_clock_ratio)
551 temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
552 if (DISPLAY_VER(dev_priv) >= 14)
553 temp |= TRANS_DDI_PORT_WIDTH(crtc_state->lane_count);
554 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
555 temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
556 temp |= (crtc_state->fdi_lanes - 1) << 1;
557 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
558 if (intel_dp_is_uhbr(crtc_state))
559 temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
560 else
561 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
562 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
563
564 if (DISPLAY_VER(dev_priv) >= 12) {
565 enum transcoder master;
566
567 master = crtc_state->mst_master_transcoder;
568 drm_WARN_ON(&dev_priv->drm,
569 master == INVALID_TRANSCODER);
570 temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
571 }
572 } else {
573 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
574 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
575 }
576
577 if (IS_DISPLAY_VER(dev_priv, 8, 10) &&
578 crtc_state->master_transcoder != INVALID_TRANSCODER) {
579 u8 master_select =
580 bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
581
582 temp |= TRANS_DDI_PORT_SYNC_ENABLE |
583 TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
584 }
585
586 return temp;
587}
588
589void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
590 const struct intel_crtc_state *crtc_state)
591{
592 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
593 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
594 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
595
596 if (DISPLAY_VER(dev_priv) >= 11) {
597 enum transcoder master_transcoder = crtc_state->master_transcoder;
598 u32 ctl2 = 0;
599
600 if (master_transcoder != INVALID_TRANSCODER) {
601 u8 master_select =
602 bdw_trans_port_sync_master_select(master_transcoder);
603
604 ctl2 |= PORT_SYNC_MODE_ENABLE |
605 PORT_SYNC_MODE_MASTER_SELECT(master_select);
606 }
607
608 intel_de_write(dev_priv,
609 TRANS_DDI_FUNC_CTL2(dev_priv, cpu_transcoder),
610 ctl2);
611 }
612
613 intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder),
614 intel_ddi_transcoder_func_reg_val_get(encoder,
615 crtc_state));
616}
617
618/*
619 * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
620 * bit.
621 */
622static void
623intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
624 const struct intel_crtc_state *crtc_state)
625{
626 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
627 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
628 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
629 u32 ctl;
630
631 ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
632 ctl &= ~TRANS_DDI_FUNC_ENABLE;
633 intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder),
634 ctl);
635}
636
637void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
638{
639 struct intel_display *display = to_intel_display(crtc_state);
640 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
641 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
642 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
643 u32 ctl;
644
645 if (DISPLAY_VER(dev_priv) >= 11)
646 intel_de_write(dev_priv,
647 TRANS_DDI_FUNC_CTL2(dev_priv, cpu_transcoder),
648 0);
649
650 ctl = intel_de_read(dev_priv,
651 TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder));
652
653 drm_WARN_ON(crtc->base.dev, ctl & TRANS_DDI_HDCP_SIGNALLING);
654
655 ctl &= ~TRANS_DDI_FUNC_ENABLE;
656
657 if (IS_DISPLAY_VER(dev_priv, 8, 10))
658 ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
659 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
660
661 if (DISPLAY_VER(dev_priv) >= 12) {
662 if (!intel_dp_mst_is_master_trans(crtc_state)) {
663 ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
664 TRANS_DDI_MODE_SELECT_MASK);
665 }
666 } else {
667 ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
668 }
669
670 intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder),
671 ctl);
672
673 if (intel_has_quirk(display, QUIRK_INCREASE_DDI_DISABLED_TIME) &&
674 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
675 drm_dbg_kms(display->drm, "Quirk Increase DDI disabled time\n");
676 /* Quirk time at 100ms for reliable operation */
677 msleep(100);
678 }
679}
680
681int intel_ddi_toggle_hdcp_bits(struct intel_encoder *intel_encoder,
682 enum transcoder cpu_transcoder,
683 bool enable, u32 hdcp_mask)
684{
685 struct drm_device *dev = intel_encoder->base.dev;
686 struct drm_i915_private *dev_priv = to_i915(dev);
687 intel_wakeref_t wakeref;
688 int ret = 0;
689
690 wakeref = intel_display_power_get_if_enabled(dev_priv,
691 intel_encoder->power_domain);
692 if (drm_WARN_ON(dev, !wakeref))
693 return -ENXIO;
694
695 intel_de_rmw(dev_priv, TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder),
696 hdcp_mask, enable ? hdcp_mask : 0);
697 intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
698 return ret;
699}
700
701bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
702{
703 struct drm_device *dev = intel_connector->base.dev;
704 struct drm_i915_private *dev_priv = to_i915(dev);
705 struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
706 int type = intel_connector->base.connector_type;
707 enum port port = encoder->port;
708 enum transcoder cpu_transcoder;
709 intel_wakeref_t wakeref;
710 enum pipe pipe = 0;
711 u32 tmp;
712 bool ret;
713
714 wakeref = intel_display_power_get_if_enabled(dev_priv,
715 encoder->power_domain);
716 if (!wakeref)
717 return false;
718
719 if (!encoder->get_hw_state(encoder, &pipe)) {
720 ret = false;
721 goto out;
722 }
723
724 if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
725 cpu_transcoder = TRANSCODER_EDP;
726 else
727 cpu_transcoder = (enum transcoder) pipe;
728
729 tmp = intel_de_read(dev_priv,
730 TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder));
731
732 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
733 case TRANS_DDI_MODE_SELECT_HDMI:
734 case TRANS_DDI_MODE_SELECT_DVI:
735 ret = type == DRM_MODE_CONNECTOR_HDMIA;
736 break;
737
738 case TRANS_DDI_MODE_SELECT_DP_SST:
739 ret = type == DRM_MODE_CONNECTOR_eDP ||
740 type == DRM_MODE_CONNECTOR_DisplayPort;
741 break;
742
743 case TRANS_DDI_MODE_SELECT_DP_MST:
744 /* if the transcoder is in MST state then
745 * connector isn't connected */
746 ret = false;
747 break;
748
749 case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
750 if (HAS_DP20(dev_priv))
751 /* 128b/132b */
752 ret = false;
753 else
754 /* FDI */
755 ret = type == DRM_MODE_CONNECTOR_VGA;
756 break;
757
758 default:
759 ret = false;
760 break;
761 }
762
763out:
764 intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
765
766 return ret;
767}
768
769static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
770 u8 *pipe_mask, bool *is_dp_mst)
771{
772 struct drm_device *dev = encoder->base.dev;
773 struct drm_i915_private *dev_priv = to_i915(dev);
774 enum port port = encoder->port;
775 intel_wakeref_t wakeref;
776 enum pipe p;
777 u32 tmp;
778 u8 mst_pipe_mask;
779
780 *pipe_mask = 0;
781 *is_dp_mst = false;
782
783 wakeref = intel_display_power_get_if_enabled(dev_priv,
784 encoder->power_domain);
785 if (!wakeref)
786 return;
787
788 tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
789 if (!(tmp & DDI_BUF_CTL_ENABLE))
790 goto out;
791
792 if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
793 tmp = intel_de_read(dev_priv,
794 TRANS_DDI_FUNC_CTL(dev_priv, TRANSCODER_EDP));
795
796 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
797 default:
798 MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
799 fallthrough;
800 case TRANS_DDI_EDP_INPUT_A_ON:
801 case TRANS_DDI_EDP_INPUT_A_ONOFF:
802 *pipe_mask = BIT(PIPE_A);
803 break;
804 case TRANS_DDI_EDP_INPUT_B_ONOFF:
805 *pipe_mask = BIT(PIPE_B);
806 break;
807 case TRANS_DDI_EDP_INPUT_C_ONOFF:
808 *pipe_mask = BIT(PIPE_C);
809 break;
810 }
811
812 goto out;
813 }
814
815 mst_pipe_mask = 0;
816 for_each_pipe(dev_priv, p) {
817 enum transcoder cpu_transcoder = (enum transcoder)p;
818 unsigned int port_mask, ddi_select;
819 intel_wakeref_t trans_wakeref;
820
821 trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
822 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
823 if (!trans_wakeref)
824 continue;
825
826 if (DISPLAY_VER(dev_priv) >= 12) {
827 port_mask = TGL_TRANS_DDI_PORT_MASK;
828 ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
829 } else {
830 port_mask = TRANS_DDI_PORT_MASK;
831 ddi_select = TRANS_DDI_SELECT_PORT(port);
832 }
833
834 tmp = intel_de_read(dev_priv,
835 TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder));
836 intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
837 trans_wakeref);
838
839 if ((tmp & port_mask) != ddi_select)
840 continue;
841
842 if ((tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_DP_MST ||
843 (HAS_DP20(dev_priv) &&
844 (tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_FDI_OR_128B132B))
845 mst_pipe_mask |= BIT(p);
846
847 *pipe_mask |= BIT(p);
848 }
849
850 if (!*pipe_mask)
851 drm_dbg_kms(&dev_priv->drm,
852 "No pipe for [ENCODER:%d:%s] found\n",
853 encoder->base.base.id, encoder->base.name);
854
855 if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
856 drm_dbg_kms(&dev_priv->drm,
857 "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
858 encoder->base.base.id, encoder->base.name,
859 *pipe_mask);
860 *pipe_mask = BIT(ffs(*pipe_mask) - 1);
861 }
862
863 if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
864 drm_dbg_kms(&dev_priv->drm,
865 "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
866 encoder->base.base.id, encoder->base.name,
867 *pipe_mask, mst_pipe_mask);
868 else
869 *is_dp_mst = mst_pipe_mask;
870
871out:
872 if (*pipe_mask && (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))) {
873 tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
874 if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
875 BXT_PHY_LANE_POWERDOWN_ACK |
876 BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
877 drm_err(&dev_priv->drm,
878 "[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
879 encoder->base.base.id, encoder->base.name, tmp);
880 }
881
882 intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
883}
884
885bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
886 enum pipe *pipe)
887{
888 u8 pipe_mask;
889 bool is_mst;
890
891 intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
892
893 if (is_mst || !pipe_mask)
894 return false;
895
896 *pipe = ffs(pipe_mask) - 1;
897
898 return true;
899}
900
901static enum intel_display_power_domain
902intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port,
903 const struct intel_crtc_state *crtc_state)
904{
905 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
906
907 /*
908 * ICL+ HW requires corresponding AUX IOs to be powered up for PSR with
909 * DC states enabled at the same time, while for driver initiated AUX
910 * transfers we need the same AUX IOs to be powered but with DC states
911 * disabled. Accordingly use the AUX_IO_<port> power domain here which
912 * leaves DC states enabled.
913 *
914 * Before MTL TypeC PHYs (in all TypeC modes and both DP/HDMI) also require
915 * AUX IO to be enabled, but all these require DC_OFF to be enabled as
916 * well, so we can acquire a wider AUX_<port> power domain reference
917 * instead of a specific AUX_IO_<port> reference without powering up any
918 * extra wells.
919 */
920 if (intel_psr_needs_aux_io_power(&dig_port->base, crtc_state))
921 return intel_display_power_aux_io_domain(i915, dig_port->aux_ch);
922 else if (DISPLAY_VER(i915) < 14 &&
923 (intel_crtc_has_dp_encoder(crtc_state) ||
924 intel_encoder_is_tc(&dig_port->base)))
925 return intel_aux_power_domain(dig_port);
926 else
927 return POWER_DOMAIN_INVALID;
928}
929
930static void
931main_link_aux_power_domain_get(struct intel_digital_port *dig_port,
932 const struct intel_crtc_state *crtc_state)
933{
934 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
935 enum intel_display_power_domain domain =
936 intel_ddi_main_link_aux_domain(dig_port, crtc_state);
937
938 drm_WARN_ON(&i915->drm, dig_port->aux_wakeref);
939
940 if (domain == POWER_DOMAIN_INVALID)
941 return;
942
943 dig_port->aux_wakeref = intel_display_power_get(i915, domain);
944}
945
946static void
947main_link_aux_power_domain_put(struct intel_digital_port *dig_port,
948 const struct intel_crtc_state *crtc_state)
949{
950 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
951 enum intel_display_power_domain domain =
952 intel_ddi_main_link_aux_domain(dig_port, crtc_state);
953 intel_wakeref_t wf;
954
955 wf = fetch_and_zero(&dig_port->aux_wakeref);
956 if (!wf)
957 return;
958
959 intel_display_power_put(i915, domain, wf);
960}
961
962static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
963 struct intel_crtc_state *crtc_state)
964{
965 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
966 struct intel_digital_port *dig_port;
967
968 /*
969 * TODO: Add support for MST encoders. Atm, the following should never
970 * happen since fake-MST encoders don't set their get_power_domains()
971 * hook.
972 */
973 if (drm_WARN_ON(&dev_priv->drm,
974 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
975 return;
976
977 dig_port = enc_to_dig_port(encoder);
978
979 if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
980 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
981 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
982 dig_port->ddi_io_power_domain);
983 }
984
985 main_link_aux_power_domain_get(dig_port, crtc_state);
986}
987
988void intel_ddi_enable_transcoder_clock(struct intel_encoder *encoder,
989 const struct intel_crtc_state *crtc_state)
990{
991 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
992 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
993 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
994 enum phy phy = intel_encoder_to_phy(encoder);
995 u32 val;
996
997 if (cpu_transcoder == TRANSCODER_EDP)
998 return;
999
1000 if (DISPLAY_VER(dev_priv) >= 13)
1001 val = TGL_TRANS_CLK_SEL_PORT(phy);
1002 else if (DISPLAY_VER(dev_priv) >= 12)
1003 val = TGL_TRANS_CLK_SEL_PORT(encoder->port);
1004 else
1005 val = TRANS_CLK_SEL_PORT(encoder->port);
1006
1007 intel_de_write(dev_priv, TRANS_CLK_SEL(cpu_transcoder), val);
1008}
1009
1010void intel_ddi_disable_transcoder_clock(const struct intel_crtc_state *crtc_state)
1011{
1012 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1013 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1014 u32 val;
1015
1016 if (cpu_transcoder == TRANSCODER_EDP)
1017 return;
1018
1019 if (DISPLAY_VER(dev_priv) >= 12)
1020 val = TGL_TRANS_CLK_SEL_DISABLED;
1021 else
1022 val = TRANS_CLK_SEL_DISABLED;
1023
1024 intel_de_write(dev_priv, TRANS_CLK_SEL(cpu_transcoder), val);
1025}
1026
1027static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
1028 enum port port, u8 iboost)
1029{
1030 u32 tmp;
1031
1032 tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
1033 tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
1034 if (iboost)
1035 tmp |= iboost << BALANCE_LEG_SHIFT(port);
1036 else
1037 tmp |= BALANCE_LEG_DISABLE(port);
1038 intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
1039}
1040
1041static void skl_ddi_set_iboost(struct intel_encoder *encoder,
1042 const struct intel_crtc_state *crtc_state,
1043 int level)
1044{
1045 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1046 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1047 u8 iboost;
1048
1049 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1050 iboost = intel_bios_hdmi_boost_level(encoder->devdata);
1051 else
1052 iboost = intel_bios_dp_boost_level(encoder->devdata);
1053
1054 if (iboost == 0) {
1055 const struct intel_ddi_buf_trans *trans;
1056 int n_entries;
1057
1058 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1059 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1060 return;
1061
1062 iboost = trans->entries[level].hsw.i_boost;
1063 }
1064
1065 /* Make sure that the requested I_boost is valid */
1066 if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
1067 drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
1068 return;
1069 }
1070
1071 _skl_ddi_set_iboost(dev_priv, encoder->port, iboost);
1072
1073 if (encoder->port == PORT_A && dig_port->max_lanes == 4)
1074 _skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
1075}
1076
1077static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp,
1078 const struct intel_crtc_state *crtc_state)
1079{
1080 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1081 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1082 int n_entries;
1083
1084 encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1085
1086 if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
1087 n_entries = 1;
1088 if (drm_WARN_ON(&dev_priv->drm,
1089 n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1090 n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1091
1092 return index_to_dp_signal_levels[n_entries - 1] &
1093 DP_TRAIN_VOLTAGE_SWING_MASK;
1094}
1095
1096/*
1097 * We assume that the full set of pre-emphasis values can be
1098 * used on all DDI platforms. Should that change we need to
1099 * rethink this code.
1100 */
1101static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
1102{
1103 return DP_TRAIN_PRE_EMPH_LEVEL_3;
1104}
1105
1106static u32 icl_combo_phy_loadgen_select(const struct intel_crtc_state *crtc_state,
1107 int lane)
1108{
1109 if (crtc_state->port_clock > 600000)
1110 return 0;
1111
1112 if (crtc_state->lane_count == 4)
1113 return lane >= 1 ? LOADGEN_SELECT : 0;
1114 else
1115 return lane == 1 || lane == 2 ? LOADGEN_SELECT : 0;
1116}
1117
1118static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
1119 const struct intel_crtc_state *crtc_state)
1120{
1121 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1122 const struct intel_ddi_buf_trans *trans;
1123 enum phy phy = intel_encoder_to_phy(encoder);
1124 int n_entries, ln;
1125 u32 val;
1126
1127 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1128 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1129 return;
1130
1131 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP)) {
1132 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1133
1134 val = EDP4K2K_MODE_OVRD_EN | EDP4K2K_MODE_OVRD_OPTIMIZED;
1135 intel_dp->hobl_active = is_hobl_buf_trans(trans);
1136 intel_de_rmw(dev_priv, ICL_PORT_CL_DW10(phy), val,
1137 intel_dp->hobl_active ? val : 0);
1138 }
1139
1140 /* Set PORT_TX_DW5 */
1141 val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1142 val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
1143 TAP2_DISABLE | TAP3_DISABLE);
1144 val |= SCALING_MODE_SEL(0x2);
1145 val |= RTERM_SELECT(0x6);
1146 val |= TAP3_DISABLE;
1147 intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1148
1149 /* Program PORT_TX_DW2 */
1150 for (ln = 0; ln < 4; ln++) {
1151 int level = intel_ddi_level(encoder, crtc_state, ln);
1152
1153 intel_de_rmw(dev_priv, ICL_PORT_TX_DW2_LN(ln, phy),
1154 SWING_SEL_UPPER_MASK | SWING_SEL_LOWER_MASK | RCOMP_SCALAR_MASK,
1155 SWING_SEL_UPPER(trans->entries[level].icl.dw2_swing_sel) |
1156 SWING_SEL_LOWER(trans->entries[level].icl.dw2_swing_sel) |
1157 RCOMP_SCALAR(0x98));
1158 }
1159
1160 /* Program PORT_TX_DW4 */
1161 /* We cannot write to GRP. It would overwrite individual loadgen. */
1162 for (ln = 0; ln < 4; ln++) {
1163 int level = intel_ddi_level(encoder, crtc_state, ln);
1164
1165 intel_de_rmw(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy),
1166 POST_CURSOR_1_MASK | POST_CURSOR_2_MASK | CURSOR_COEFF_MASK,
1167 POST_CURSOR_1(trans->entries[level].icl.dw4_post_cursor_1) |
1168 POST_CURSOR_2(trans->entries[level].icl.dw4_post_cursor_2) |
1169 CURSOR_COEFF(trans->entries[level].icl.dw4_cursor_coeff));
1170 }
1171
1172 /* Program PORT_TX_DW7 */
1173 for (ln = 0; ln < 4; ln++) {
1174 int level = intel_ddi_level(encoder, crtc_state, ln);
1175
1176 intel_de_rmw(dev_priv, ICL_PORT_TX_DW7_LN(ln, phy),
1177 N_SCALAR_MASK,
1178 N_SCALAR(trans->entries[level].icl.dw7_n_scalar));
1179 }
1180}
1181
1182static void icl_combo_phy_set_signal_levels(struct intel_encoder *encoder,
1183 const struct intel_crtc_state *crtc_state)
1184{
1185 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1186 enum phy phy = intel_encoder_to_phy(encoder);
1187 u32 val;
1188 int ln;
1189
1190 /*
1191 * 1. If port type is eDP or DP,
1192 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
1193 * else clear to 0b.
1194 */
1195 val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN(0, phy));
1196 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1197 val &= ~COMMON_KEEPER_EN;
1198 else
1199 val |= COMMON_KEEPER_EN;
1200 intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
1201
1202 /* 2. Program loadgen select */
1203 /*
1204 * Program PORT_TX_DW4 depending on Bit rate and used lanes
1205 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
1206 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
1207 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
1208 */
1209 for (ln = 0; ln < 4; ln++) {
1210 intel_de_rmw(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy),
1211 LOADGEN_SELECT,
1212 icl_combo_phy_loadgen_select(crtc_state, ln));
1213 }
1214
1215 /* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
1216 intel_de_rmw(dev_priv, ICL_PORT_CL_DW5(phy),
1217 0, SUS_CLOCK_CONFIG);
1218
1219 /* 4. Clear training enable to change swing values */
1220 val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1221 val &= ~TX_TRAINING_EN;
1222 intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1223
1224 /* 5. Program swing and de-emphasis */
1225 icl_ddi_combo_vswing_program(encoder, crtc_state);
1226
1227 /* 6. Set training enable to trigger update */
1228 val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1229 val |= TX_TRAINING_EN;
1230 intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1231}
1232
1233static void icl_mg_phy_set_signal_levels(struct intel_encoder *encoder,
1234 const struct intel_crtc_state *crtc_state)
1235{
1236 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1237 enum tc_port tc_port = intel_encoder_to_tc(encoder);
1238 const struct intel_ddi_buf_trans *trans;
1239 int n_entries, ln;
1240
1241 if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1242 return;
1243
1244 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1245 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1246 return;
1247
1248 for (ln = 0; ln < 2; ln++) {
1249 intel_de_rmw(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port),
1250 CRI_USE_FS32, 0);
1251 intel_de_rmw(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port),
1252 CRI_USE_FS32, 0);
1253 }
1254
1255 /* Program MG_TX_SWINGCTRL with values from vswing table */
1256 for (ln = 0; ln < 2; ln++) {
1257 int level;
1258
1259 level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1260
1261 intel_de_rmw(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port),
1262 CRI_TXDEEMPH_OVERRIDE_17_12_MASK,
1263 CRI_TXDEEMPH_OVERRIDE_17_12(trans->entries[level].mg.cri_txdeemph_override_17_12));
1264
1265 level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1266
1267 intel_de_rmw(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port),
1268 CRI_TXDEEMPH_OVERRIDE_17_12_MASK,
1269 CRI_TXDEEMPH_OVERRIDE_17_12(trans->entries[level].mg.cri_txdeemph_override_17_12));
1270 }
1271
1272 /* Program MG_TX_DRVCTRL with values from vswing table */
1273 for (ln = 0; ln < 2; ln++) {
1274 int level;
1275
1276 level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1277
1278 intel_de_rmw(dev_priv, MG_TX1_DRVCTRL(ln, tc_port),
1279 CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1280 CRI_TXDEEMPH_OVERRIDE_5_0_MASK,
1281 CRI_TXDEEMPH_OVERRIDE_11_6(trans->entries[level].mg.cri_txdeemph_override_11_6) |
1282 CRI_TXDEEMPH_OVERRIDE_5_0(trans->entries[level].mg.cri_txdeemph_override_5_0) |
1283 CRI_TXDEEMPH_OVERRIDE_EN);
1284
1285 level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1286
1287 intel_de_rmw(dev_priv, MG_TX2_DRVCTRL(ln, tc_port),
1288 CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1289 CRI_TXDEEMPH_OVERRIDE_5_0_MASK,
1290 CRI_TXDEEMPH_OVERRIDE_11_6(trans->entries[level].mg.cri_txdeemph_override_11_6) |
1291 CRI_TXDEEMPH_OVERRIDE_5_0(trans->entries[level].mg.cri_txdeemph_override_5_0) |
1292 CRI_TXDEEMPH_OVERRIDE_EN);
1293
1294 /* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
1295 }
1296
1297 /*
1298 * Program MG_CLKHUB<LN, port being used> with value from frequency table
1299 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
1300 * values from table for which TX1 and TX2 enabled.
1301 */
1302 for (ln = 0; ln < 2; ln++) {
1303 intel_de_rmw(dev_priv, MG_CLKHUB(ln, tc_port),
1304 CFG_LOW_RATE_LKREN_EN,
1305 crtc_state->port_clock < 300000 ? CFG_LOW_RATE_LKREN_EN : 0);
1306 }
1307
1308 /* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
1309 for (ln = 0; ln < 2; ln++) {
1310 intel_de_rmw(dev_priv, MG_TX1_DCC(ln, tc_port),
1311 CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK |
1312 CFG_AMI_CK_DIV_OVERRIDE_EN,
1313 crtc_state->port_clock > 500000 ?
1314 CFG_AMI_CK_DIV_OVERRIDE_VAL(1) |
1315 CFG_AMI_CK_DIV_OVERRIDE_EN : 0);
1316
1317 intel_de_rmw(dev_priv, MG_TX2_DCC(ln, tc_port),
1318 CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK |
1319 CFG_AMI_CK_DIV_OVERRIDE_EN,
1320 crtc_state->port_clock > 500000 ?
1321 CFG_AMI_CK_DIV_OVERRIDE_VAL(1) |
1322 CFG_AMI_CK_DIV_OVERRIDE_EN : 0);
1323 }
1324
1325 /* Program MG_TX_PISO_READLOAD with values from vswing table */
1326 for (ln = 0; ln < 2; ln++) {
1327 intel_de_rmw(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
1328 0, CRI_CALCINIT);
1329 intel_de_rmw(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
1330 0, CRI_CALCINIT);
1331 }
1332}
1333
1334static void tgl_dkl_phy_set_signal_levels(struct intel_encoder *encoder,
1335 const struct intel_crtc_state *crtc_state)
1336{
1337 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1338 enum tc_port tc_port = intel_encoder_to_tc(encoder);
1339 const struct intel_ddi_buf_trans *trans;
1340 int n_entries, ln;
1341
1342 if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1343 return;
1344
1345 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1346 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1347 return;
1348
1349 for (ln = 0; ln < 2; ln++) {
1350 int level;
1351
1352 intel_dkl_phy_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port, ln), 0);
1353
1354 level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1355
1356 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL0(tc_port, ln),
1357 DKL_TX_PRESHOOT_COEFF_MASK |
1358 DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1359 DKL_TX_VSWING_CONTROL_MASK,
1360 DKL_TX_PRESHOOT_COEFF(trans->entries[level].dkl.preshoot) |
1361 DKL_TX_DE_EMPHASIS_COEFF(trans->entries[level].dkl.de_emphasis) |
1362 DKL_TX_VSWING_CONTROL(trans->entries[level].dkl.vswing));
1363
1364 level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1365
1366 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL1(tc_port, ln),
1367 DKL_TX_PRESHOOT_COEFF_MASK |
1368 DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1369 DKL_TX_VSWING_CONTROL_MASK,
1370 DKL_TX_PRESHOOT_COEFF(trans->entries[level].dkl.preshoot) |
1371 DKL_TX_DE_EMPHASIS_COEFF(trans->entries[level].dkl.de_emphasis) |
1372 DKL_TX_VSWING_CONTROL(trans->entries[level].dkl.vswing));
1373
1374 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL2(tc_port, ln),
1375 DKL_TX_DP20BITMODE, 0);
1376
1377 if (IS_ALDERLAKE_P(dev_priv)) {
1378 u32 val;
1379
1380 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1381 if (ln == 0) {
1382 val = DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1(0);
1383 val |= DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2(2);
1384 } else {
1385 val = DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1(3);
1386 val |= DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2(3);
1387 }
1388 } else {
1389 val = DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1(0);
1390 val |= DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2(0);
1391 }
1392
1393 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL2(tc_port, ln),
1394 DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1_MASK |
1395 DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2_MASK,
1396 val);
1397 }
1398 }
1399}
1400
1401static int translate_signal_level(struct intel_dp *intel_dp,
1402 u8 signal_levels)
1403{
1404 struct intel_display *display = to_intel_display(intel_dp);
1405 int i;
1406
1407 for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
1408 if (index_to_dp_signal_levels[i] == signal_levels)
1409 return i;
1410 }
1411
1412 drm_WARN(display->drm, 1,
1413 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
1414 signal_levels);
1415
1416 return 0;
1417}
1418
1419static int intel_ddi_dp_level(struct intel_dp *intel_dp,
1420 const struct intel_crtc_state *crtc_state,
1421 int lane)
1422{
1423 u8 train_set = intel_dp->train_set[lane];
1424
1425 if (intel_dp_is_uhbr(crtc_state)) {
1426 return train_set & DP_TX_FFE_PRESET_VALUE_MASK;
1427 } else {
1428 u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1429 DP_TRAIN_PRE_EMPHASIS_MASK);
1430
1431 return translate_signal_level(intel_dp, signal_levels);
1432 }
1433}
1434
1435int intel_ddi_level(struct intel_encoder *encoder,
1436 const struct intel_crtc_state *crtc_state,
1437 int lane)
1438{
1439 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1440 const struct intel_ddi_buf_trans *trans;
1441 int level, n_entries;
1442
1443 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1444 if (drm_WARN_ON_ONCE(&i915->drm, !trans))
1445 return 0;
1446
1447 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1448 level = intel_ddi_hdmi_level(encoder, trans);
1449 else
1450 level = intel_ddi_dp_level(enc_to_intel_dp(encoder), crtc_state,
1451 lane);
1452
1453 if (drm_WARN_ON_ONCE(&i915->drm, level >= n_entries))
1454 level = n_entries - 1;
1455
1456 return level;
1457}
1458
1459static void
1460hsw_set_signal_levels(struct intel_encoder *encoder,
1461 const struct intel_crtc_state *crtc_state)
1462{
1463 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1464 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1465 int level = intel_ddi_level(encoder, crtc_state, 0);
1466 enum port port = encoder->port;
1467 u32 signal_levels;
1468
1469 if (has_iboost(dev_priv))
1470 skl_ddi_set_iboost(encoder, crtc_state, level);
1471
1472 /* HDMI ignores the rest */
1473 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1474 return;
1475
1476 signal_levels = DDI_BUF_TRANS_SELECT(level);
1477
1478 drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
1479 signal_levels);
1480
1481 intel_dp->DP &= ~DDI_BUF_EMP_MASK;
1482 intel_dp->DP |= signal_levels;
1483
1484 intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
1485 intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
1486}
1487
1488static void _icl_ddi_enable_clock(struct drm_i915_private *i915, i915_reg_t reg,
1489 u32 clk_sel_mask, u32 clk_sel, u32 clk_off)
1490{
1491 mutex_lock(&i915->display.dpll.lock);
1492
1493 intel_de_rmw(i915, reg, clk_sel_mask, clk_sel);
1494
1495 /*
1496 * "This step and the step before must be
1497 * done with separate register writes."
1498 */
1499 intel_de_rmw(i915, reg, clk_off, 0);
1500
1501 mutex_unlock(&i915->display.dpll.lock);
1502}
1503
1504static void _icl_ddi_disable_clock(struct drm_i915_private *i915, i915_reg_t reg,
1505 u32 clk_off)
1506{
1507 mutex_lock(&i915->display.dpll.lock);
1508
1509 intel_de_rmw(i915, reg, 0, clk_off);
1510
1511 mutex_unlock(&i915->display.dpll.lock);
1512}
1513
1514static bool _icl_ddi_is_clock_enabled(struct drm_i915_private *i915, i915_reg_t reg,
1515 u32 clk_off)
1516{
1517 return !(intel_de_read(i915, reg) & clk_off);
1518}
1519
1520static struct intel_shared_dpll *
1521_icl_ddi_get_pll(struct drm_i915_private *i915, i915_reg_t reg,
1522 u32 clk_sel_mask, u32 clk_sel_shift)
1523{
1524 enum intel_dpll_id id;
1525
1526 id = (intel_de_read(i915, reg) & clk_sel_mask) >> clk_sel_shift;
1527
1528 return intel_get_shared_dpll_by_id(i915, id);
1529}
1530
1531static void adls_ddi_enable_clock(struct intel_encoder *encoder,
1532 const struct intel_crtc_state *crtc_state)
1533{
1534 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1535 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1536 enum phy phy = intel_encoder_to_phy(encoder);
1537
1538 if (drm_WARN_ON(&i915->drm, !pll))
1539 return;
1540
1541 _icl_ddi_enable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
1542 ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1543 pll->info->id << ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy),
1544 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1545}
1546
1547static void adls_ddi_disable_clock(struct intel_encoder *encoder)
1548{
1549 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1550 enum phy phy = intel_encoder_to_phy(encoder);
1551
1552 _icl_ddi_disable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
1553 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1554}
1555
1556static bool adls_ddi_is_clock_enabled(struct intel_encoder *encoder)
1557{
1558 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1559 enum phy phy = intel_encoder_to_phy(encoder);
1560
1561 return _icl_ddi_is_clock_enabled(i915, ADLS_DPCLKA_CFGCR(phy),
1562 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1563}
1564
1565static struct intel_shared_dpll *adls_ddi_get_pll(struct intel_encoder *encoder)
1566{
1567 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1568 enum phy phy = intel_encoder_to_phy(encoder);
1569
1570 return _icl_ddi_get_pll(i915, ADLS_DPCLKA_CFGCR(phy),
1571 ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1572 ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy));
1573}
1574
1575static void rkl_ddi_enable_clock(struct intel_encoder *encoder,
1576 const struct intel_crtc_state *crtc_state)
1577{
1578 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1579 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1580 enum phy phy = intel_encoder_to_phy(encoder);
1581
1582 if (drm_WARN_ON(&i915->drm, !pll))
1583 return;
1584
1585 _icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
1586 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1587 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
1588 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1589}
1590
1591static void rkl_ddi_disable_clock(struct intel_encoder *encoder)
1592{
1593 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1594 enum phy phy = intel_encoder_to_phy(encoder);
1595
1596 _icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
1597 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1598}
1599
1600static bool rkl_ddi_is_clock_enabled(struct intel_encoder *encoder)
1601{
1602 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1603 enum phy phy = intel_encoder_to_phy(encoder);
1604
1605 return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
1606 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1607}
1608
1609static struct intel_shared_dpll *rkl_ddi_get_pll(struct intel_encoder *encoder)
1610{
1611 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1612 enum phy phy = intel_encoder_to_phy(encoder);
1613
1614 return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1615 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1616 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1617}
1618
1619static void dg1_ddi_enable_clock(struct intel_encoder *encoder,
1620 const struct intel_crtc_state *crtc_state)
1621{
1622 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1623 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1624 enum phy phy = intel_encoder_to_phy(encoder);
1625
1626 if (drm_WARN_ON(&i915->drm, !pll))
1627 return;
1628
1629 /*
1630 * If we fail this, something went very wrong: first 2 PLLs should be
1631 * used by first 2 phys and last 2 PLLs by last phys
1632 */
1633 if (drm_WARN_ON(&i915->drm,
1634 (pll->info->id < DPLL_ID_DG1_DPLL2 && phy >= PHY_C) ||
1635 (pll->info->id >= DPLL_ID_DG1_DPLL2 && phy < PHY_C)))
1636 return;
1637
1638 _icl_ddi_enable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
1639 DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1640 DG1_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
1641 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1642}
1643
1644static void dg1_ddi_disable_clock(struct intel_encoder *encoder)
1645{
1646 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1647 enum phy phy = intel_encoder_to_phy(encoder);
1648
1649 _icl_ddi_disable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
1650 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1651}
1652
1653static bool dg1_ddi_is_clock_enabled(struct intel_encoder *encoder)
1654{
1655 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1656 enum phy phy = intel_encoder_to_phy(encoder);
1657
1658 return _icl_ddi_is_clock_enabled(i915, DG1_DPCLKA_CFGCR0(phy),
1659 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1660}
1661
1662static struct intel_shared_dpll *dg1_ddi_get_pll(struct intel_encoder *encoder)
1663{
1664 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1665 enum phy phy = intel_encoder_to_phy(encoder);
1666 enum intel_dpll_id id;
1667 u32 val;
1668
1669 val = intel_de_read(i915, DG1_DPCLKA_CFGCR0(phy));
1670 val &= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
1671 val >>= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
1672 id = val;
1673
1674 /*
1675 * _DG1_DPCLKA0_CFGCR0 maps between DPLL 0 and 1 with one bit for phy A
1676 * and B while _DG1_DPCLKA1_CFGCR0 maps between DPLL 2 and 3 with one
1677 * bit for phy C and D.
1678 */
1679 if (phy >= PHY_C)
1680 id += DPLL_ID_DG1_DPLL2;
1681
1682 return intel_get_shared_dpll_by_id(i915, id);
1683}
1684
1685static void icl_ddi_combo_enable_clock(struct intel_encoder *encoder,
1686 const struct intel_crtc_state *crtc_state)
1687{
1688 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1689 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1690 enum phy phy = intel_encoder_to_phy(encoder);
1691
1692 if (drm_WARN_ON(&i915->drm, !pll))
1693 return;
1694
1695 _icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
1696 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1697 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
1698 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1699}
1700
1701static void icl_ddi_combo_disable_clock(struct intel_encoder *encoder)
1702{
1703 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1704 enum phy phy = intel_encoder_to_phy(encoder);
1705
1706 _icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
1707 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1708}
1709
1710static bool icl_ddi_combo_is_clock_enabled(struct intel_encoder *encoder)
1711{
1712 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1713 enum phy phy = intel_encoder_to_phy(encoder);
1714
1715 return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
1716 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1717}
1718
1719struct intel_shared_dpll *icl_ddi_combo_get_pll(struct intel_encoder *encoder)
1720{
1721 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1722 enum phy phy = intel_encoder_to_phy(encoder);
1723
1724 return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1725 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1726 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1727}
1728
1729static void jsl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1730 const struct intel_crtc_state *crtc_state)
1731{
1732 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1733 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1734 enum port port = encoder->port;
1735
1736 if (drm_WARN_ON(&i915->drm, !pll))
1737 return;
1738
1739 /*
1740 * "For DDIC and DDID, program DDI_CLK_SEL to map the MG clock to the port.
1741 * MG does not exist, but the programming is required to ungate DDIC and DDID."
1742 */
1743 intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_MG);
1744
1745 icl_ddi_combo_enable_clock(encoder, crtc_state);
1746}
1747
1748static void jsl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1749{
1750 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1751 enum port port = encoder->port;
1752
1753 icl_ddi_combo_disable_clock(encoder);
1754
1755 intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1756}
1757
1758static bool jsl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
1759{
1760 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1761 enum port port = encoder->port;
1762 u32 tmp;
1763
1764 tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1765
1766 if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
1767 return false;
1768
1769 return icl_ddi_combo_is_clock_enabled(encoder);
1770}
1771
1772static void icl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1773 const struct intel_crtc_state *crtc_state)
1774{
1775 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1776 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1777 enum tc_port tc_port = intel_encoder_to_tc(encoder);
1778 enum port port = encoder->port;
1779
1780 if (drm_WARN_ON(&i915->drm, !pll))
1781 return;
1782
1783 intel_de_write(i915, DDI_CLK_SEL(port),
1784 icl_pll_to_ddi_clk_sel(encoder, crtc_state));
1785
1786 mutex_lock(&i915->display.dpll.lock);
1787
1788 intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
1789 ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port), 0);
1790
1791 mutex_unlock(&i915->display.dpll.lock);
1792}
1793
1794static void icl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1795{
1796 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1797 enum tc_port tc_port = intel_encoder_to_tc(encoder);
1798 enum port port = encoder->port;
1799
1800 mutex_lock(&i915->display.dpll.lock);
1801
1802 intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
1803 0, ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
1804
1805 mutex_unlock(&i915->display.dpll.lock);
1806
1807 intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1808}
1809
1810static bool icl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
1811{
1812 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1813 enum tc_port tc_port = intel_encoder_to_tc(encoder);
1814 enum port port = encoder->port;
1815 u32 tmp;
1816
1817 tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1818
1819 if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
1820 return false;
1821
1822 tmp = intel_de_read(i915, ICL_DPCLKA_CFGCR0);
1823
1824 return !(tmp & ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
1825}
1826
1827static struct intel_shared_dpll *icl_ddi_tc_get_pll(struct intel_encoder *encoder)
1828{
1829 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1830 enum tc_port tc_port = intel_encoder_to_tc(encoder);
1831 enum port port = encoder->port;
1832 enum intel_dpll_id id;
1833 u32 tmp;
1834
1835 tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1836
1837 switch (tmp & DDI_CLK_SEL_MASK) {
1838 case DDI_CLK_SEL_TBT_162:
1839 case DDI_CLK_SEL_TBT_270:
1840 case DDI_CLK_SEL_TBT_540:
1841 case DDI_CLK_SEL_TBT_810:
1842 id = DPLL_ID_ICL_TBTPLL;
1843 break;
1844 case DDI_CLK_SEL_MG:
1845 id = icl_tc_port_to_pll_id(tc_port);
1846 break;
1847 default:
1848 MISSING_CASE(tmp);
1849 fallthrough;
1850 case DDI_CLK_SEL_NONE:
1851 return NULL;
1852 }
1853
1854 return intel_get_shared_dpll_by_id(i915, id);
1855}
1856
1857static struct intel_shared_dpll *bxt_ddi_get_pll(struct intel_encoder *encoder)
1858{
1859 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1860 enum intel_dpll_id id;
1861
1862 switch (encoder->port) {
1863 case PORT_A:
1864 id = DPLL_ID_SKL_DPLL0;
1865 break;
1866 case PORT_B:
1867 id = DPLL_ID_SKL_DPLL1;
1868 break;
1869 case PORT_C:
1870 id = DPLL_ID_SKL_DPLL2;
1871 break;
1872 default:
1873 MISSING_CASE(encoder->port);
1874 return NULL;
1875 }
1876
1877 return intel_get_shared_dpll_by_id(i915, id);
1878}
1879
1880static void skl_ddi_enable_clock(struct intel_encoder *encoder,
1881 const struct intel_crtc_state *crtc_state)
1882{
1883 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1884 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1885 enum port port = encoder->port;
1886
1887 if (drm_WARN_ON(&i915->drm, !pll))
1888 return;
1889
1890 mutex_lock(&i915->display.dpll.lock);
1891
1892 intel_de_rmw(i915, DPLL_CTRL2,
1893 DPLL_CTRL2_DDI_CLK_OFF(port) |
1894 DPLL_CTRL2_DDI_CLK_SEL_MASK(port),
1895 DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
1896 DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
1897
1898 mutex_unlock(&i915->display.dpll.lock);
1899}
1900
1901static void skl_ddi_disable_clock(struct intel_encoder *encoder)
1902{
1903 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1904 enum port port = encoder->port;
1905
1906 mutex_lock(&i915->display.dpll.lock);
1907
1908 intel_de_rmw(i915, DPLL_CTRL2,
1909 0, DPLL_CTRL2_DDI_CLK_OFF(port));
1910
1911 mutex_unlock(&i915->display.dpll.lock);
1912}
1913
1914static bool skl_ddi_is_clock_enabled(struct intel_encoder *encoder)
1915{
1916 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1917 enum port port = encoder->port;
1918
1919 /*
1920 * FIXME Not sure if the override affects both
1921 * the PLL selection and the CLK_OFF bit.
1922 */
1923 return !(intel_de_read(i915, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_OFF(port));
1924}
1925
1926static struct intel_shared_dpll *skl_ddi_get_pll(struct intel_encoder *encoder)
1927{
1928 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1929 enum port port = encoder->port;
1930 enum intel_dpll_id id;
1931 u32 tmp;
1932
1933 tmp = intel_de_read(i915, DPLL_CTRL2);
1934
1935 /*
1936 * FIXME Not sure if the override affects both
1937 * the PLL selection and the CLK_OFF bit.
1938 */
1939 if ((tmp & DPLL_CTRL2_DDI_SEL_OVERRIDE(port)) == 0)
1940 return NULL;
1941
1942 id = (tmp & DPLL_CTRL2_DDI_CLK_SEL_MASK(port)) >>
1943 DPLL_CTRL2_DDI_CLK_SEL_SHIFT(port);
1944
1945 return intel_get_shared_dpll_by_id(i915, id);
1946}
1947
1948void hsw_ddi_enable_clock(struct intel_encoder *encoder,
1949 const struct intel_crtc_state *crtc_state)
1950{
1951 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1952 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1953 enum port port = encoder->port;
1954
1955 if (drm_WARN_ON(&i915->drm, !pll))
1956 return;
1957
1958 intel_de_write(i915, PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
1959}
1960
1961void hsw_ddi_disable_clock(struct intel_encoder *encoder)
1962{
1963 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1964 enum port port = encoder->port;
1965
1966 intel_de_write(i915, PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1967}
1968
1969bool hsw_ddi_is_clock_enabled(struct intel_encoder *encoder)
1970{
1971 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1972 enum port port = encoder->port;
1973
1974 return intel_de_read(i915, PORT_CLK_SEL(port)) != PORT_CLK_SEL_NONE;
1975}
1976
1977static struct intel_shared_dpll *hsw_ddi_get_pll(struct intel_encoder *encoder)
1978{
1979 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1980 enum port port = encoder->port;
1981 enum intel_dpll_id id;
1982 u32 tmp;
1983
1984 tmp = intel_de_read(i915, PORT_CLK_SEL(port));
1985
1986 switch (tmp & PORT_CLK_SEL_MASK) {
1987 case PORT_CLK_SEL_WRPLL1:
1988 id = DPLL_ID_WRPLL1;
1989 break;
1990 case PORT_CLK_SEL_WRPLL2:
1991 id = DPLL_ID_WRPLL2;
1992 break;
1993 case PORT_CLK_SEL_SPLL:
1994 id = DPLL_ID_SPLL;
1995 break;
1996 case PORT_CLK_SEL_LCPLL_810:
1997 id = DPLL_ID_LCPLL_810;
1998 break;
1999 case PORT_CLK_SEL_LCPLL_1350:
2000 id = DPLL_ID_LCPLL_1350;
2001 break;
2002 case PORT_CLK_SEL_LCPLL_2700:
2003 id = DPLL_ID_LCPLL_2700;
2004 break;
2005 default:
2006 MISSING_CASE(tmp);
2007 fallthrough;
2008 case PORT_CLK_SEL_NONE:
2009 return NULL;
2010 }
2011
2012 return intel_get_shared_dpll_by_id(i915, id);
2013}
2014
2015void intel_ddi_enable_clock(struct intel_encoder *encoder,
2016 const struct intel_crtc_state *crtc_state)
2017{
2018 if (encoder->enable_clock)
2019 encoder->enable_clock(encoder, crtc_state);
2020}
2021
2022void intel_ddi_disable_clock(struct intel_encoder *encoder)
2023{
2024 if (encoder->disable_clock)
2025 encoder->disable_clock(encoder);
2026}
2027
2028void intel_ddi_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
2029{
2030 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2031 u32 port_mask;
2032 bool ddi_clk_needed;
2033
2034 /*
2035 * In case of DP MST, we sanitize the primary encoder only, not the
2036 * virtual ones.
2037 */
2038 if (encoder->type == INTEL_OUTPUT_DP_MST)
2039 return;
2040
2041 if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
2042 u8 pipe_mask;
2043 bool is_mst;
2044
2045 intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2046 /*
2047 * In the unlikely case that BIOS enables DP in MST mode, just
2048 * warn since our MST HW readout is incomplete.
2049 */
2050 if (drm_WARN_ON(&i915->drm, is_mst))
2051 return;
2052 }
2053
2054 port_mask = BIT(encoder->port);
2055 ddi_clk_needed = encoder->base.crtc;
2056
2057 if (encoder->type == INTEL_OUTPUT_DSI) {
2058 struct intel_encoder *other_encoder;
2059
2060 port_mask = intel_dsi_encoder_ports(encoder);
2061 /*
2062 * Sanity check that we haven't incorrectly registered another
2063 * encoder using any of the ports of this DSI encoder.
2064 */
2065 for_each_intel_encoder(&i915->drm, other_encoder) {
2066 if (other_encoder == encoder)
2067 continue;
2068
2069 if (drm_WARN_ON(&i915->drm,
2070 port_mask & BIT(other_encoder->port)))
2071 return;
2072 }
2073 /*
2074 * For DSI we keep the ddi clocks gated
2075 * except during enable/disable sequence.
2076 */
2077 ddi_clk_needed = false;
2078 }
2079
2080 if (ddi_clk_needed || !encoder->is_clock_enabled ||
2081 !encoder->is_clock_enabled(encoder))
2082 return;
2083
2084 drm_dbg_kms(&i915->drm,
2085 "[ENCODER:%d:%s] is disabled/in DSI mode with an ungated DDI clock, gate it\n",
2086 encoder->base.base.id, encoder->base.name);
2087
2088 encoder->disable_clock(encoder);
2089}
2090
2091static void
2092icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
2093 const struct intel_crtc_state *crtc_state)
2094{
2095 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2096 enum tc_port tc_port = intel_encoder_to_tc(&dig_port->base);
2097 u32 ln0, ln1, pin_assignment;
2098 u8 width;
2099
2100 if (DISPLAY_VER(dev_priv) >= 14)
2101 return;
2102
2103 if (!intel_encoder_is_tc(&dig_port->base) ||
2104 intel_tc_port_in_tbt_alt_mode(dig_port))
2105 return;
2106
2107 if (DISPLAY_VER(dev_priv) >= 12) {
2108 ln0 = intel_dkl_phy_read(dev_priv, DKL_DP_MODE(tc_port, 0));
2109 ln1 = intel_dkl_phy_read(dev_priv, DKL_DP_MODE(tc_port, 1));
2110 } else {
2111 ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
2112 ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
2113 }
2114
2115 ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2116 ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2117
2118 /* DPPATC */
2119 pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
2120 width = crtc_state->lane_count;
2121
2122 switch (pin_assignment) {
2123 case 0x0:
2124 drm_WARN_ON(&dev_priv->drm,
2125 !intel_tc_port_in_legacy_mode(dig_port));
2126 if (width == 1) {
2127 ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
2128 } else {
2129 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2130 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2131 }
2132 break;
2133 case 0x1:
2134 if (width == 4) {
2135 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2136 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2137 }
2138 break;
2139 case 0x2:
2140 if (width == 2) {
2141 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2142 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2143 }
2144 break;
2145 case 0x3:
2146 case 0x5:
2147 if (width == 1) {
2148 ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
2149 ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
2150 } else {
2151 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2152 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2153 }
2154 break;
2155 case 0x4:
2156 case 0x6:
2157 if (width == 1) {
2158 ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
2159 ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
2160 } else {
2161 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2162 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2163 }
2164 break;
2165 default:
2166 MISSING_CASE(pin_assignment);
2167 }
2168
2169 if (DISPLAY_VER(dev_priv) >= 12) {
2170 intel_dkl_phy_write(dev_priv, DKL_DP_MODE(tc_port, 0), ln0);
2171 intel_dkl_phy_write(dev_priv, DKL_DP_MODE(tc_port, 1), ln1);
2172 } else {
2173 intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
2174 intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
2175 }
2176}
2177
2178static enum transcoder
2179tgl_dp_tp_transcoder(const struct intel_crtc_state *crtc_state)
2180{
2181 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2182 return crtc_state->mst_master_transcoder;
2183 else
2184 return crtc_state->cpu_transcoder;
2185}
2186
2187i915_reg_t dp_tp_ctl_reg(struct intel_encoder *encoder,
2188 const struct intel_crtc_state *crtc_state)
2189{
2190 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2191
2192 if (DISPLAY_VER(dev_priv) >= 12)
2193 return TGL_DP_TP_CTL(dev_priv,
2194 tgl_dp_tp_transcoder(crtc_state));
2195 else
2196 return DP_TP_CTL(encoder->port);
2197}
2198
2199i915_reg_t dp_tp_status_reg(struct intel_encoder *encoder,
2200 const struct intel_crtc_state *crtc_state)
2201{
2202 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2203
2204 if (DISPLAY_VER(dev_priv) >= 12)
2205 return TGL_DP_TP_STATUS(dev_priv,
2206 tgl_dp_tp_transcoder(crtc_state));
2207 else
2208 return DP_TP_STATUS(encoder->port);
2209}
2210
2211static void intel_dp_sink_set_msa_timing_par_ignore_state(struct intel_dp *intel_dp,
2212 const struct intel_crtc_state *crtc_state,
2213 bool enable)
2214{
2215 struct intel_display *display = to_intel_display(intel_dp);
2216
2217 if (!crtc_state->vrr.enable)
2218 return;
2219
2220 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_DOWNSPREAD_CTRL,
2221 enable ? DP_MSA_TIMING_PAR_IGNORE_EN : 0) <= 0)
2222 drm_dbg_kms(display->drm,
2223 "Failed to %s MSA_TIMING_PAR_IGNORE in the sink\n",
2224 str_enable_disable(enable));
2225}
2226
2227static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
2228 const struct intel_crtc_state *crtc_state,
2229 bool enable)
2230{
2231 struct intel_display *display = to_intel_display(intel_dp);
2232
2233 if (!crtc_state->fec_enable)
2234 return;
2235
2236 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION,
2237 enable ? DP_FEC_READY : 0) <= 0)
2238 drm_dbg_kms(display->drm, "Failed to set FEC_READY to %s in the sink\n",
2239 str_enabled_disabled(enable));
2240
2241 if (enable &&
2242 drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_STATUS,
2243 DP_FEC_DECODE_EN_DETECTED | DP_FEC_DECODE_DIS_DETECTED) <= 0)
2244 drm_dbg_kms(display->drm, "Failed to clear FEC detected flags\n");
2245}
2246
2247static int read_fec_detected_status(struct drm_dp_aux *aux)
2248{
2249 int ret;
2250 u8 status;
2251
2252 ret = drm_dp_dpcd_readb(aux, DP_FEC_STATUS, &status);
2253 if (ret < 0)
2254 return ret;
2255
2256 return status;
2257}
2258
2259static int wait_for_fec_detected(struct drm_dp_aux *aux, bool enabled)
2260{
2261 struct intel_display *display = to_intel_display(aux->drm_dev);
2262 int mask = enabled ? DP_FEC_DECODE_EN_DETECTED : DP_FEC_DECODE_DIS_DETECTED;
2263 int status;
2264 int err;
2265
2266 err = readx_poll_timeout(read_fec_detected_status, aux, status,
2267 status & mask || status < 0,
2268 10000, 200000);
2269
2270 if (err || status < 0) {
2271 drm_dbg_kms(display->drm,
2272 "Failed waiting for FEC %s to get detected: %d (status %d)\n",
2273 str_enabled_disabled(enabled), err, status);
2274 return err ? err : status;
2275 }
2276
2277 return 0;
2278}
2279
2280int intel_ddi_wait_for_fec_status(struct intel_encoder *encoder,
2281 const struct intel_crtc_state *crtc_state,
2282 bool enabled)
2283{
2284 struct intel_display *display = to_intel_display(encoder);
2285 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2286 int ret;
2287
2288 if (!crtc_state->fec_enable)
2289 return 0;
2290
2291 if (enabled)
2292 ret = intel_de_wait_for_set(display, dp_tp_status_reg(encoder, crtc_state),
2293 DP_TP_STATUS_FEC_ENABLE_LIVE, 1);
2294 else
2295 ret = intel_de_wait_for_clear(display, dp_tp_status_reg(encoder, crtc_state),
2296 DP_TP_STATUS_FEC_ENABLE_LIVE, 1);
2297
2298 if (ret) {
2299 drm_err(display->drm,
2300 "Timeout waiting for FEC live state to get %s\n",
2301 str_enabled_disabled(enabled));
2302 return ret;
2303 }
2304 /*
2305 * At least the Synoptics MST hub doesn't set the detected flag for
2306 * FEC decoding disabling so skip waiting for that.
2307 */
2308 if (enabled) {
2309 ret = wait_for_fec_detected(&intel_dp->aux, enabled);
2310 if (ret)
2311 return ret;
2312 }
2313
2314 return 0;
2315}
2316
2317static void intel_ddi_enable_fec(struct intel_encoder *encoder,
2318 const struct intel_crtc_state *crtc_state)
2319{
2320 struct intel_display *display = to_intel_display(encoder);
2321 int i;
2322 int ret;
2323
2324 if (!crtc_state->fec_enable)
2325 return;
2326
2327 intel_de_rmw(display, dp_tp_ctl_reg(encoder, crtc_state),
2328 0, DP_TP_CTL_FEC_ENABLE);
2329
2330 if (DISPLAY_VER(display) < 30)
2331 return;
2332
2333 ret = intel_ddi_wait_for_fec_status(encoder, crtc_state, true);
2334 if (!ret)
2335 return;
2336
2337 for (i = 0; i < 3; i++) {
2338 drm_dbg_kms(display->drm, "Retry FEC enabling\n");
2339
2340 intel_de_rmw(display, dp_tp_ctl_reg(encoder, crtc_state),
2341 DP_TP_CTL_FEC_ENABLE, 0);
2342
2343 ret = intel_ddi_wait_for_fec_status(encoder, crtc_state, false);
2344 if (ret)
2345 continue;
2346
2347 intel_de_rmw(display, dp_tp_ctl_reg(encoder, crtc_state),
2348 0, DP_TP_CTL_FEC_ENABLE);
2349
2350 ret = intel_ddi_wait_for_fec_status(encoder, crtc_state, true);
2351 if (!ret)
2352 return;
2353 }
2354
2355 drm_err(display->drm, "Failed to enable FEC after retries\n");
2356}
2357
2358static void intel_ddi_disable_fec(struct intel_encoder *encoder,
2359 const struct intel_crtc_state *crtc_state)
2360{
2361 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2362
2363 if (!crtc_state->fec_enable)
2364 return;
2365
2366 intel_de_rmw(dev_priv, dp_tp_ctl_reg(encoder, crtc_state),
2367 DP_TP_CTL_FEC_ENABLE, 0);
2368 intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2369}
2370
2371static void intel_ddi_power_up_lanes(struct intel_encoder *encoder,
2372 const struct intel_crtc_state *crtc_state)
2373{
2374 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2375 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2376
2377 if (intel_encoder_is_combo(encoder)) {
2378 enum phy phy = intel_encoder_to_phy(encoder);
2379 bool lane_reversal =
2380 dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
2381
2382 intel_combo_phy_power_up_lanes(i915, phy, false,
2383 crtc_state->lane_count,
2384 lane_reversal);
2385 }
2386}
2387
2388/*
2389 * Splitter enable for eDP MSO is limited to certain pipes, on certain
2390 * platforms.
2391 */
2392static u8 intel_ddi_splitter_pipe_mask(struct drm_i915_private *i915)
2393{
2394 if (DISPLAY_VER(i915) > 20)
2395 return ~0;
2396 else if (IS_ALDERLAKE_P(i915))
2397 return BIT(PIPE_A) | BIT(PIPE_B);
2398 else
2399 return BIT(PIPE_A);
2400}
2401
2402static void intel_ddi_mso_get_config(struct intel_encoder *encoder,
2403 struct intel_crtc_state *pipe_config)
2404{
2405 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
2406 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2407 enum pipe pipe = crtc->pipe;
2408 u32 dss1;
2409
2410 if (!HAS_MSO(i915))
2411 return;
2412
2413 dss1 = intel_de_read(i915, ICL_PIPE_DSS_CTL1(pipe));
2414
2415 pipe_config->splitter.enable = dss1 & SPLITTER_ENABLE;
2416 if (!pipe_config->splitter.enable)
2417 return;
2418
2419 if (drm_WARN_ON(&i915->drm, !(intel_ddi_splitter_pipe_mask(i915) & BIT(pipe)))) {
2420 pipe_config->splitter.enable = false;
2421 return;
2422 }
2423
2424 switch (dss1 & SPLITTER_CONFIGURATION_MASK) {
2425 default:
2426 drm_WARN(&i915->drm, true,
2427 "Invalid splitter configuration, dss1=0x%08x\n", dss1);
2428 fallthrough;
2429 case SPLITTER_CONFIGURATION_2_SEGMENT:
2430 pipe_config->splitter.link_count = 2;
2431 break;
2432 case SPLITTER_CONFIGURATION_4_SEGMENT:
2433 pipe_config->splitter.link_count = 4;
2434 break;
2435 }
2436
2437 pipe_config->splitter.pixel_overlap = REG_FIELD_GET(OVERLAP_PIXELS_MASK, dss1);
2438}
2439
2440static void intel_ddi_mso_configure(const struct intel_crtc_state *crtc_state)
2441{
2442 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2443 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2444 enum pipe pipe = crtc->pipe;
2445 u32 dss1 = 0;
2446
2447 if (!HAS_MSO(i915))
2448 return;
2449
2450 if (crtc_state->splitter.enable) {
2451 dss1 |= SPLITTER_ENABLE;
2452 dss1 |= OVERLAP_PIXELS(crtc_state->splitter.pixel_overlap);
2453 if (crtc_state->splitter.link_count == 2)
2454 dss1 |= SPLITTER_CONFIGURATION_2_SEGMENT;
2455 else
2456 dss1 |= SPLITTER_CONFIGURATION_4_SEGMENT;
2457 }
2458
2459 intel_de_rmw(i915, ICL_PIPE_DSS_CTL1(pipe),
2460 SPLITTER_ENABLE | SPLITTER_CONFIGURATION_MASK |
2461 OVERLAP_PIXELS_MASK, dss1);
2462}
2463
2464static u8 mtl_get_port_width(u8 lane_count)
2465{
2466 switch (lane_count) {
2467 case 1:
2468 return 0;
2469 case 2:
2470 return 1;
2471 case 3:
2472 return 4;
2473 case 4:
2474 return 3;
2475 default:
2476 MISSING_CASE(lane_count);
2477 return 4;
2478 }
2479}
2480
2481static void
2482mtl_ddi_enable_d2d(struct intel_encoder *encoder)
2483{
2484 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2485 enum port port = encoder->port;
2486 i915_reg_t reg;
2487 u32 set_bits, wait_bits;
2488
2489 if (DISPLAY_VER(dev_priv) >= 20) {
2490 reg = DDI_BUF_CTL(port);
2491 set_bits = XE2LPD_DDI_BUF_D2D_LINK_ENABLE;
2492 wait_bits = XE2LPD_DDI_BUF_D2D_LINK_STATE;
2493 } else {
2494 reg = XELPDP_PORT_BUF_CTL1(dev_priv, port);
2495 set_bits = XELPDP_PORT_BUF_D2D_LINK_ENABLE;
2496 wait_bits = XELPDP_PORT_BUF_D2D_LINK_STATE;
2497 }
2498
2499 intel_de_rmw(dev_priv, reg, 0, set_bits);
2500 if (wait_for_us(intel_de_read(dev_priv, reg) & wait_bits, 100)) {
2501 drm_err(&dev_priv->drm, "Timeout waiting for D2D Link enable for DDI/PORT_BUF_CTL %c\n",
2502 port_name(port));
2503 }
2504}
2505
2506static void mtl_port_buf_ctl_program(struct intel_encoder *encoder,
2507 const struct intel_crtc_state *crtc_state)
2508{
2509 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2510 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2511 enum port port = encoder->port;
2512 u32 val;
2513
2514 val = intel_de_read(i915, XELPDP_PORT_BUF_CTL1(i915, port));
2515 val &= ~XELPDP_PORT_WIDTH_MASK;
2516 val |= XELPDP_PORT_WIDTH(mtl_get_port_width(crtc_state->lane_count));
2517
2518 val &= ~XELPDP_PORT_BUF_PORT_DATA_WIDTH_MASK;
2519 if (intel_dp_is_uhbr(crtc_state))
2520 val |= XELPDP_PORT_BUF_PORT_DATA_40BIT;
2521 else
2522 val |= XELPDP_PORT_BUF_PORT_DATA_10BIT;
2523
2524 if (dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL)
2525 val |= XELPDP_PORT_REVERSAL;
2526
2527 intel_de_write(i915, XELPDP_PORT_BUF_CTL1(i915, port), val);
2528}
2529
2530static void mtl_port_buf_ctl_io_selection(struct intel_encoder *encoder)
2531{
2532 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2533 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2534 u32 val;
2535
2536 val = intel_tc_port_in_tbt_alt_mode(dig_port) ?
2537 XELPDP_PORT_BUF_IO_SELECT_TBT : 0;
2538 intel_de_rmw(i915, XELPDP_PORT_BUF_CTL1(i915, encoder->port),
2539 XELPDP_PORT_BUF_IO_SELECT_TBT, val);
2540}
2541
2542static void mtl_ddi_pre_enable_dp(struct intel_atomic_state *state,
2543 struct intel_encoder *encoder,
2544 const struct intel_crtc_state *crtc_state,
2545 const struct drm_connector_state *conn_state)
2546{
2547 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2548 bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2549
2550 intel_dp_set_link_params(intel_dp,
2551 crtc_state->port_clock,
2552 crtc_state->lane_count);
2553
2554 /*
2555 * We only configure what the register value will be here. Actual
2556 * enabling happens during link training farther down.
2557 */
2558 intel_ddi_init_dp_buf_reg(encoder, crtc_state);
2559
2560 /*
2561 * 1. Enable Power Wells
2562 *
2563 * This was handled at the beginning of intel_atomic_commit_tail(),
2564 * before we called down into this function.
2565 */
2566
2567 /* 2. PMdemand was already set */
2568
2569 /* 3. Select Thunderbolt */
2570 mtl_port_buf_ctl_io_selection(encoder);
2571
2572 /* 4. Enable Panel Power if PPS is required */
2573 intel_pps_on(intel_dp);
2574
2575 /* 5. Enable the port PLL */
2576 intel_ddi_enable_clock(encoder, crtc_state);
2577
2578 /*
2579 * 6.a Configure Transcoder Clock Select to direct the Port clock to the
2580 * Transcoder.
2581 */
2582 intel_ddi_enable_transcoder_clock(encoder, crtc_state);
2583
2584 /*
2585 * 6.b If DP v2.0/128b mode - Configure TRANS_DP2_CTL register settings.
2586 */
2587 intel_ddi_config_transcoder_dp2(encoder, crtc_state);
2588
2589 /*
2590 * 6.c Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
2591 * Transport Select
2592 */
2593 intel_ddi_config_transcoder_func(encoder, crtc_state);
2594
2595 /*
2596 * 6.e Program CoG/MSO configuration bits in DSS_CTL1 if selected.
2597 */
2598 intel_ddi_mso_configure(crtc_state);
2599
2600 if (!is_mst)
2601 intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2602
2603 intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2604 if (!is_mst)
2605 intel_dp_sink_enable_decompression(state,
2606 to_intel_connector(conn_state->connector),
2607 crtc_state);
2608
2609 /*
2610 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
2611 * in the FEC_CONFIGURATION register to 1 before initiating link
2612 * training
2613 */
2614 intel_dp_sink_set_fec_ready(intel_dp, crtc_state, true);
2615
2616 intel_dp_check_frl_training(intel_dp);
2617 intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
2618
2619 /*
2620 * 6. The rest of the below are substeps under the bspec's "Enable and
2621 * Train Display Port" step. Note that steps that are specific to
2622 * MST will be handled by intel_mst_pre_enable_dp() before/after it
2623 * calls into this function. Also intel_mst_pre_enable_dp() only calls
2624 * us when active_mst_links==0, so any steps designated for "single
2625 * stream or multi-stream master transcoder" can just be performed
2626 * unconditionally here.
2627 *
2628 * mtl_ddi_prepare_link_retrain() that is called by
2629 * intel_dp_start_link_train() will execute steps: 6.d, 6.f, 6.g, 6.h,
2630 * 6.i and 6.j
2631 *
2632 * 6.k Follow DisplayPort specification training sequence (see notes for
2633 * failure handling)
2634 * 6.m If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
2635 * Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
2636 * (timeout after 800 us)
2637 */
2638 intel_dp_start_link_train(state, intel_dp, crtc_state);
2639
2640 /* 6.n Set DP_TP_CTL link training to Normal */
2641 if (!is_trans_port_sync_mode(crtc_state))
2642 intel_dp_stop_link_train(intel_dp, crtc_state);
2643
2644 /* 6.o Configure and enable FEC if needed */
2645 intel_ddi_enable_fec(encoder, crtc_state);
2646
2647 if (!is_mst)
2648 intel_dsc_dp_pps_write(encoder, crtc_state);
2649}
2650
2651static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
2652 struct intel_encoder *encoder,
2653 const struct intel_crtc_state *crtc_state,
2654 const struct drm_connector_state *conn_state)
2655{
2656 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2657 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2658 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2659 bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2660
2661 intel_dp_set_link_params(intel_dp,
2662 crtc_state->port_clock,
2663 crtc_state->lane_count);
2664
2665 /*
2666 * We only configure what the register value will be here. Actual
2667 * enabling happens during link training farther down.
2668 */
2669 intel_ddi_init_dp_buf_reg(encoder, crtc_state);
2670
2671 /*
2672 * 1. Enable Power Wells
2673 *
2674 * This was handled at the beginning of intel_atomic_commit_tail(),
2675 * before we called down into this function.
2676 */
2677
2678 /* 2. Enable Panel Power if PPS is required */
2679 intel_pps_on(intel_dp);
2680
2681 /*
2682 * 3. For non-TBT Type-C ports, set FIA lane count
2683 * (DFLEXDPSP.DPX4TXLATC)
2684 *
2685 * This was done before tgl_ddi_pre_enable_dp by
2686 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
2687 */
2688
2689 /*
2690 * 4. Enable the port PLL.
2691 *
2692 * The PLL enabling itself was already done before this function by
2693 * hsw_crtc_enable()->intel_enable_shared_dpll(). We need only
2694 * configure the PLL to port mapping here.
2695 */
2696 intel_ddi_enable_clock(encoder, crtc_state);
2697
2698 /* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
2699 if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2700 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2701 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2702 dig_port->ddi_io_power_domain);
2703 }
2704
2705 /* 6. Program DP_MODE */
2706 icl_program_mg_dp_mode(dig_port, crtc_state);
2707
2708 /*
2709 * 7. The rest of the below are substeps under the bspec's "Enable and
2710 * Train Display Port" step. Note that steps that are specific to
2711 * MST will be handled by intel_mst_pre_enable_dp() before/after it
2712 * calls into this function. Also intel_mst_pre_enable_dp() only calls
2713 * us when active_mst_links==0, so any steps designated for "single
2714 * stream or multi-stream master transcoder" can just be performed
2715 * unconditionally here.
2716 */
2717
2718 /*
2719 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
2720 * Transcoder.
2721 */
2722 intel_ddi_enable_transcoder_clock(encoder, crtc_state);
2723
2724 if (HAS_DP20(dev_priv))
2725 intel_ddi_config_transcoder_dp2(encoder, crtc_state);
2726
2727 /*
2728 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
2729 * Transport Select
2730 */
2731 intel_ddi_config_transcoder_func(encoder, crtc_state);
2732
2733 /*
2734 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
2735 * selected
2736 *
2737 * This will be handled by the intel_dp_start_link_train() farther
2738 * down this function.
2739 */
2740
2741 /* 7.e Configure voltage swing and related IO settings */
2742 encoder->set_signal_levels(encoder, crtc_state);
2743
2744 /*
2745 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
2746 * the used lanes of the DDI.
2747 */
2748 intel_ddi_power_up_lanes(encoder, crtc_state);
2749
2750 /*
2751 * 7.g Program CoG/MSO configuration bits in DSS_CTL1 if selected.
2752 */
2753 intel_ddi_mso_configure(crtc_state);
2754
2755 if (!is_mst)
2756 intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2757
2758 intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2759 if (!is_mst)
2760 intel_dp_sink_enable_decompression(state,
2761 to_intel_connector(conn_state->connector),
2762 crtc_state);
2763 /*
2764 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
2765 * in the FEC_CONFIGURATION register to 1 before initiating link
2766 * training
2767 */
2768 intel_dp_sink_set_fec_ready(intel_dp, crtc_state, true);
2769
2770 intel_dp_check_frl_training(intel_dp);
2771 intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
2772
2773 /*
2774 * 7.i Follow DisplayPort specification training sequence (see notes for
2775 * failure handling)
2776 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
2777 * Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
2778 * (timeout after 800 us)
2779 */
2780 intel_dp_start_link_train(state, intel_dp, crtc_state);
2781
2782 /* 7.k Set DP_TP_CTL link training to Normal */
2783 if (!is_trans_port_sync_mode(crtc_state))
2784 intel_dp_stop_link_train(intel_dp, crtc_state);
2785
2786 /* 7.l Configure and enable FEC if needed */
2787 intel_ddi_enable_fec(encoder, crtc_state);
2788
2789 if (!is_mst)
2790 intel_dsc_dp_pps_write(encoder, crtc_state);
2791}
2792
2793static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
2794 struct intel_encoder *encoder,
2795 const struct intel_crtc_state *crtc_state,
2796 const struct drm_connector_state *conn_state)
2797{
2798 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2799 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2800 enum port port = encoder->port;
2801 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2802 bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2803
2804 if (DISPLAY_VER(dev_priv) < 11)
2805 drm_WARN_ON(&dev_priv->drm,
2806 is_mst && (port == PORT_A || port == PORT_E));
2807 else
2808 drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
2809
2810 intel_dp_set_link_params(intel_dp,
2811 crtc_state->port_clock,
2812 crtc_state->lane_count);
2813
2814 /*
2815 * We only configure what the register value will be here. Actual
2816 * enabling happens during link training farther down.
2817 */
2818 intel_ddi_init_dp_buf_reg(encoder, crtc_state);
2819
2820 intel_pps_on(intel_dp);
2821
2822 intel_ddi_enable_clock(encoder, crtc_state);
2823
2824 if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2825 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2826 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2827 dig_port->ddi_io_power_domain);
2828 }
2829
2830 icl_program_mg_dp_mode(dig_port, crtc_state);
2831
2832 if (has_buf_trans_select(dev_priv))
2833 hsw_prepare_dp_ddi_buffers(encoder, crtc_state);
2834
2835 encoder->set_signal_levels(encoder, crtc_state);
2836
2837 intel_ddi_power_up_lanes(encoder, crtc_state);
2838
2839 if (!is_mst)
2840 intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2841 intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2842 if (!is_mst)
2843 intel_dp_sink_enable_decompression(state,
2844 to_intel_connector(conn_state->connector),
2845 crtc_state);
2846 intel_dp_sink_set_fec_ready(intel_dp, crtc_state, true);
2847 intel_dp_start_link_train(state, intel_dp, crtc_state);
2848 if ((port != PORT_A || DISPLAY_VER(dev_priv) >= 9) &&
2849 !is_trans_port_sync_mode(crtc_state))
2850 intel_dp_stop_link_train(intel_dp, crtc_state);
2851
2852 intel_ddi_enable_fec(encoder, crtc_state);
2853
2854 if (!is_mst) {
2855 intel_ddi_enable_transcoder_clock(encoder, crtc_state);
2856 intel_dsc_dp_pps_write(encoder, crtc_state);
2857 }
2858}
2859
2860static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
2861 struct intel_encoder *encoder,
2862 const struct intel_crtc_state *crtc_state,
2863 const struct drm_connector_state *conn_state)
2864{
2865 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2866
2867 if (HAS_DP20(dev_priv))
2868 intel_dp_128b132b_sdp_crc16(enc_to_intel_dp(encoder),
2869 crtc_state);
2870
2871 /* Panel replay has to be enabled in sink dpcd before link training. */
2872 if (crtc_state->has_panel_replay)
2873 intel_psr_enable_sink(enc_to_intel_dp(encoder), crtc_state);
2874
2875 if (DISPLAY_VER(dev_priv) >= 14)
2876 mtl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2877 else if (DISPLAY_VER(dev_priv) >= 12)
2878 tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2879 else
2880 hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2881
2882 /* MST will call a setting of MSA after an allocating of Virtual Channel
2883 * from MST encoder pre_enable callback.
2884 */
2885 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2886 intel_ddi_set_dp_msa(crtc_state, conn_state);
2887}
2888
2889static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
2890 struct intel_encoder *encoder,
2891 const struct intel_crtc_state *crtc_state,
2892 const struct drm_connector_state *conn_state)
2893{
2894 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2895 struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2896 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2897
2898 intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
2899 intel_ddi_enable_clock(encoder, crtc_state);
2900
2901 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2902 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2903 dig_port->ddi_io_power_domain);
2904
2905 icl_program_mg_dp_mode(dig_port, crtc_state);
2906
2907 intel_ddi_enable_transcoder_clock(encoder, crtc_state);
2908
2909 dig_port->set_infoframes(encoder,
2910 crtc_state->has_infoframe,
2911 crtc_state, conn_state);
2912}
2913
2914static void intel_ddi_pre_enable(struct intel_atomic_state *state,
2915 struct intel_encoder *encoder,
2916 const struct intel_crtc_state *crtc_state,
2917 const struct drm_connector_state *conn_state)
2918{
2919 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2920 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2921 enum pipe pipe = crtc->pipe;
2922
2923 /*
2924 * When called from DP MST code:
2925 * - conn_state will be NULL
2926 * - encoder will be the main encoder (ie. mst->primary)
2927 * - the main connector associated with this port
2928 * won't be active or linked to a crtc
2929 * - crtc_state will be the state of the first stream to
2930 * be activated on this port, and it may not be the same
2931 * stream that will be deactivated last, but each stream
2932 * should have a state that is identical when it comes to
2933 * the DP link parameteres
2934 */
2935
2936 drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
2937
2938 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2939
2940 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2941 intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
2942 conn_state);
2943 } else {
2944 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2945
2946 intel_ddi_pre_enable_dp(state, encoder, crtc_state,
2947 conn_state);
2948
2949 /* FIXME precompute everything properly */
2950 /* FIXME how do we turn infoframes off again? */
2951 if (dig_port->lspcon.active && intel_dp_has_hdmi_sink(&dig_port->dp))
2952 dig_port->set_infoframes(encoder,
2953 crtc_state->has_infoframe,
2954 crtc_state, conn_state);
2955 }
2956}
2957
2958static void
2959mtl_ddi_disable_d2d_link(struct intel_encoder *encoder)
2960{
2961 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2962 enum port port = encoder->port;
2963 i915_reg_t reg;
2964 u32 clr_bits, wait_bits;
2965
2966 if (DISPLAY_VER(dev_priv) >= 20) {
2967 reg = DDI_BUF_CTL(port);
2968 clr_bits = XE2LPD_DDI_BUF_D2D_LINK_ENABLE;
2969 wait_bits = XE2LPD_DDI_BUF_D2D_LINK_STATE;
2970 } else {
2971 reg = XELPDP_PORT_BUF_CTL1(dev_priv, port);
2972 clr_bits = XELPDP_PORT_BUF_D2D_LINK_ENABLE;
2973 wait_bits = XELPDP_PORT_BUF_D2D_LINK_STATE;
2974 }
2975
2976 intel_de_rmw(dev_priv, reg, clr_bits, 0);
2977 if (wait_for_us(!(intel_de_read(dev_priv, reg) & wait_bits), 100))
2978 drm_err(&dev_priv->drm, "Timeout waiting for D2D Link disable for DDI/PORT_BUF_CTL %c\n",
2979 port_name(port));
2980}
2981
2982static void mtl_disable_ddi_buf(struct intel_encoder *encoder,
2983 const struct intel_crtc_state *crtc_state)
2984{
2985 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2986 enum port port = encoder->port;
2987 u32 val;
2988
2989 /* 3.b Clear DDI_CTL_DE Enable to 0. */
2990 val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
2991 if (val & DDI_BUF_CTL_ENABLE) {
2992 val &= ~DDI_BUF_CTL_ENABLE;
2993 intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
2994
2995 /* 3.c Poll for PORT_BUF_CTL Idle Status == 1, timeout after 100us */
2996 mtl_wait_ddi_buf_idle(dev_priv, port);
2997 }
2998
2999 /* 3.d Disable D2D Link */
3000 mtl_ddi_disable_d2d_link(encoder);
3001
3002 /* 3.e Disable DP_TP_CTL */
3003 if (intel_crtc_has_dp_encoder(crtc_state)) {
3004 intel_de_rmw(dev_priv, dp_tp_ctl_reg(encoder, crtc_state),
3005 DP_TP_CTL_ENABLE, 0);
3006 }
3007}
3008
3009static void disable_ddi_buf(struct intel_encoder *encoder,
3010 const struct intel_crtc_state *crtc_state)
3011{
3012 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3013 enum port port = encoder->port;
3014 bool wait = false;
3015 u32 val;
3016
3017 val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
3018 if (val & DDI_BUF_CTL_ENABLE) {
3019 val &= ~DDI_BUF_CTL_ENABLE;
3020 intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
3021 wait = true;
3022 }
3023
3024 if (intel_crtc_has_dp_encoder(crtc_state))
3025 intel_de_rmw(dev_priv, dp_tp_ctl_reg(encoder, crtc_state),
3026 DP_TP_CTL_ENABLE, 0);
3027
3028 intel_ddi_disable_fec(encoder, crtc_state);
3029
3030 if (wait)
3031 intel_wait_ddi_buf_idle(dev_priv, port);
3032}
3033
3034static void intel_disable_ddi_buf(struct intel_encoder *encoder,
3035 const struct intel_crtc_state *crtc_state)
3036{
3037 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3038
3039 if (DISPLAY_VER(dev_priv) >= 14) {
3040 mtl_disable_ddi_buf(encoder, crtc_state);
3041
3042 /* 3.f Disable DP_TP_CTL FEC Enable if it is needed */
3043 intel_ddi_disable_fec(encoder, crtc_state);
3044 } else {
3045 disable_ddi_buf(encoder, crtc_state);
3046 }
3047
3048 intel_ddi_wait_for_fec_status(encoder, crtc_state, false);
3049}
3050
3051static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
3052 struct intel_encoder *encoder,
3053 const struct intel_crtc_state *old_crtc_state,
3054 const struct drm_connector_state *old_conn_state)
3055{
3056 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3057 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3058 struct intel_dp *intel_dp = &dig_port->dp;
3059 intel_wakeref_t wakeref;
3060 bool is_mst = intel_crtc_has_type(old_crtc_state,
3061 INTEL_OUTPUT_DP_MST);
3062
3063 if (!is_mst)
3064 intel_dp_set_infoframes(encoder, false,
3065 old_crtc_state, old_conn_state);
3066
3067 /*
3068 * Power down sink before disabling the port, otherwise we end
3069 * up getting interrupts from the sink on detecting link loss.
3070 */
3071 intel_dp_set_power(intel_dp, DP_SET_POWER_D3);
3072
3073 if (DISPLAY_VER(dev_priv) >= 12) {
3074 if (is_mst) {
3075 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
3076
3077 intel_de_rmw(dev_priv,
3078 TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder),
3079 TGL_TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK,
3080 0);
3081 }
3082 } else {
3083 if (!is_mst)
3084 intel_ddi_disable_transcoder_clock(old_crtc_state);
3085 }
3086
3087 intel_disable_ddi_buf(encoder, old_crtc_state);
3088
3089 intel_dp_sink_set_fec_ready(intel_dp, old_crtc_state, false);
3090
3091 /*
3092 * From TGL spec: "If single stream or multi-stream master transcoder:
3093 * Configure Transcoder Clock select to direct no clock to the
3094 * transcoder"
3095 */
3096 if (DISPLAY_VER(dev_priv) >= 12)
3097 intel_ddi_disable_transcoder_clock(old_crtc_state);
3098
3099 intel_pps_vdd_on(intel_dp);
3100 intel_pps_off(intel_dp);
3101
3102 wakeref = fetch_and_zero(&dig_port->ddi_io_wakeref);
3103
3104 if (wakeref)
3105 intel_display_power_put(dev_priv,
3106 dig_port->ddi_io_power_domain,
3107 wakeref);
3108
3109 intel_ddi_disable_clock(encoder);
3110
3111 /* De-select Thunderbolt */
3112 if (DISPLAY_VER(dev_priv) >= 14)
3113 intel_de_rmw(dev_priv, XELPDP_PORT_BUF_CTL1(dev_priv, encoder->port),
3114 XELPDP_PORT_BUF_IO_SELECT_TBT, 0);
3115}
3116
3117static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
3118 struct intel_encoder *encoder,
3119 const struct intel_crtc_state *old_crtc_state,
3120 const struct drm_connector_state *old_conn_state)
3121{
3122 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3123 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3124 struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
3125 intel_wakeref_t wakeref;
3126
3127 dig_port->set_infoframes(encoder, false,
3128 old_crtc_state, old_conn_state);
3129
3130 if (DISPLAY_VER(dev_priv) < 12)
3131 intel_ddi_disable_transcoder_clock(old_crtc_state);
3132
3133 intel_disable_ddi_buf(encoder, old_crtc_state);
3134
3135 if (DISPLAY_VER(dev_priv) >= 12)
3136 intel_ddi_disable_transcoder_clock(old_crtc_state);
3137
3138 wakeref = fetch_and_zero(&dig_port->ddi_io_wakeref);
3139 if (wakeref)
3140 intel_display_power_put(dev_priv,
3141 dig_port->ddi_io_power_domain,
3142 wakeref);
3143
3144 intel_ddi_disable_clock(encoder);
3145
3146 intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
3147}
3148
3149static void intel_ddi_post_disable_hdmi_or_sst(struct intel_atomic_state *state,
3150 struct intel_encoder *encoder,
3151 const struct intel_crtc_state *old_crtc_state,
3152 const struct drm_connector_state *old_conn_state)
3153{
3154 struct intel_display *display = to_intel_display(encoder);
3155 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3156 struct intel_crtc *pipe_crtc;
3157 int i;
3158
3159 for_each_pipe_crtc_modeset_disable(display, pipe_crtc, old_crtc_state, i) {
3160 const struct intel_crtc_state *old_pipe_crtc_state =
3161 intel_atomic_get_old_crtc_state(state, pipe_crtc);
3162
3163 intel_crtc_vblank_off(old_pipe_crtc_state);
3164 }
3165
3166 intel_disable_transcoder(old_crtc_state);
3167
3168 intel_ddi_disable_transcoder_func(old_crtc_state);
3169
3170 for_each_pipe_crtc_modeset_disable(display, pipe_crtc, old_crtc_state, i) {
3171 const struct intel_crtc_state *old_pipe_crtc_state =
3172 intel_atomic_get_old_crtc_state(state, pipe_crtc);
3173
3174 intel_dsc_disable(old_pipe_crtc_state);
3175
3176 if (DISPLAY_VER(dev_priv) >= 9)
3177 skl_scaler_disable(old_pipe_crtc_state);
3178 else
3179 ilk_pfit_disable(old_pipe_crtc_state);
3180 }
3181}
3182
3183static void intel_ddi_post_disable(struct intel_atomic_state *state,
3184 struct intel_encoder *encoder,
3185 const struct intel_crtc_state *old_crtc_state,
3186 const struct drm_connector_state *old_conn_state)
3187{
3188 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
3189 intel_ddi_post_disable_hdmi_or_sst(state, encoder, old_crtc_state,
3190 old_conn_state);
3191
3192 /*
3193 * When called from DP MST code:
3194 * - old_conn_state will be NULL
3195 * - encoder will be the main encoder (ie. mst->primary)
3196 * - the main connector associated with this port
3197 * won't be active or linked to a crtc
3198 * - old_crtc_state will be the state of the last stream to
3199 * be deactivated on this port, and it may not be the same
3200 * stream that was activated last, but each stream
3201 * should have a state that is identical when it comes to
3202 * the DP link parameteres
3203 */
3204
3205 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3206 intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
3207 old_conn_state);
3208 else
3209 intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
3210 old_conn_state);
3211}
3212
3213static void intel_ddi_post_pll_disable(struct intel_atomic_state *state,
3214 struct intel_encoder *encoder,
3215 const struct intel_crtc_state *old_crtc_state,
3216 const struct drm_connector_state *old_conn_state)
3217{
3218 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3219
3220 main_link_aux_power_domain_put(dig_port, old_crtc_state);
3221
3222 if (intel_encoder_is_tc(encoder))
3223 intel_tc_port_put_link(dig_port);
3224}
3225
3226static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
3227 struct intel_encoder *encoder,
3228 const struct intel_crtc_state *crtc_state)
3229{
3230 const struct drm_connector_state *conn_state;
3231 struct drm_connector *conn;
3232 int i;
3233
3234 if (!crtc_state->sync_mode_slaves_mask)
3235 return;
3236
3237 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3238 struct intel_encoder *slave_encoder =
3239 to_intel_encoder(conn_state->best_encoder);
3240 struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
3241 const struct intel_crtc_state *slave_crtc_state;
3242
3243 if (!slave_crtc)
3244 continue;
3245
3246 slave_crtc_state =
3247 intel_atomic_get_new_crtc_state(state, slave_crtc);
3248
3249 if (slave_crtc_state->master_transcoder !=
3250 crtc_state->cpu_transcoder)
3251 continue;
3252
3253 intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder),
3254 slave_crtc_state);
3255 }
3256
3257 usleep_range(200, 400);
3258
3259 intel_dp_stop_link_train(enc_to_intel_dp(encoder),
3260 crtc_state);
3261}
3262
3263static void intel_enable_ddi_dp(struct intel_atomic_state *state,
3264 struct intel_encoder *encoder,
3265 const struct intel_crtc_state *crtc_state,
3266 const struct drm_connector_state *conn_state)
3267{
3268 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3269 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3270 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3271 enum port port = encoder->port;
3272
3273 if (port == PORT_A && DISPLAY_VER(dev_priv) < 9)
3274 intel_dp_stop_link_train(intel_dp, crtc_state);
3275
3276 drm_connector_update_privacy_screen(conn_state);
3277 intel_edp_backlight_on(crtc_state, conn_state);
3278
3279 if (!dig_port->lspcon.active || intel_dp_has_hdmi_sink(&dig_port->dp))
3280 intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3281
3282 trans_port_sync_stop_link_train(state, encoder, crtc_state);
3283}
3284
3285/* FIXME bad home for this function */
3286i915_reg_t hsw_chicken_trans_reg(struct drm_i915_private *i915,
3287 enum transcoder cpu_transcoder)
3288{
3289 return DISPLAY_VER(i915) >= 14 ?
3290 MTL_CHICKEN_TRANS(cpu_transcoder) :
3291 CHICKEN_TRANS(cpu_transcoder);
3292}
3293
3294static i915_reg_t
3295gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
3296 enum port port)
3297{
3298 static const enum transcoder trans[] = {
3299 [PORT_A] = TRANSCODER_EDP,
3300 [PORT_B] = TRANSCODER_A,
3301 [PORT_C] = TRANSCODER_B,
3302 [PORT_D] = TRANSCODER_C,
3303 [PORT_E] = TRANSCODER_A,
3304 };
3305
3306 drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) < 9);
3307
3308 if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
3309 port = PORT_A;
3310
3311 return CHICKEN_TRANS(trans[port]);
3312}
3313
3314static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
3315 struct intel_encoder *encoder,
3316 const struct intel_crtc_state *crtc_state,
3317 const struct drm_connector_state *conn_state)
3318{
3319 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3320 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3321 struct drm_connector *connector = conn_state->connector;
3322 enum port port = encoder->port;
3323 u32 buf_ctl;
3324
3325 if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3326 crtc_state->hdmi_high_tmds_clock_ratio,
3327 crtc_state->hdmi_scrambling))
3328 drm_dbg_kms(&dev_priv->drm,
3329 "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
3330 connector->base.id, connector->name);
3331
3332 if (has_buf_trans_select(dev_priv))
3333 hsw_prepare_hdmi_ddi_buffers(encoder, crtc_state);
3334
3335 /* e. Enable D2D Link for C10/C20 Phy */
3336 if (DISPLAY_VER(dev_priv) >= 14)
3337 mtl_ddi_enable_d2d(encoder);
3338
3339 encoder->set_signal_levels(encoder, crtc_state);
3340
3341 /* Display WA #1143: skl,kbl,cfl */
3342 if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv)) {
3343 /*
3344 * For some reason these chicken bits have been
3345 * stuffed into a transcoder register, event though
3346 * the bits affect a specific DDI port rather than
3347 * a specific transcoder.
3348 */
3349 i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3350 u32 val;
3351
3352 val = intel_de_read(dev_priv, reg);
3353
3354 if (port == PORT_E)
3355 val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3356 DDIE_TRAINING_OVERRIDE_VALUE;
3357 else
3358 val |= DDI_TRAINING_OVERRIDE_ENABLE |
3359 DDI_TRAINING_OVERRIDE_VALUE;
3360
3361 intel_de_write(dev_priv, reg, val);
3362 intel_de_posting_read(dev_priv, reg);
3363
3364 udelay(1);
3365
3366 if (port == PORT_E)
3367 val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3368 DDIE_TRAINING_OVERRIDE_VALUE);
3369 else
3370 val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3371 DDI_TRAINING_OVERRIDE_VALUE);
3372
3373 intel_de_write(dev_priv, reg, val);
3374 }
3375
3376 intel_ddi_power_up_lanes(encoder, crtc_state);
3377
3378 /* In HDMI/DVI mode, the port width, and swing/emphasis values
3379 * are ignored so nothing special needs to be done besides
3380 * enabling the port.
3381 *
3382 * On ADL_P the PHY link rate and lane count must be programmed but
3383 * these are both 0 for HDMI.
3384 *
3385 * But MTL onwards HDMI2.1 is supported and in TMDS mode this
3386 * is filled with lane count, already set in the crtc_state.
3387 * The same is required to be filled in PORT_BUF_CTL for C10/20 Phy.
3388 */
3389 buf_ctl = dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE;
3390 if (DISPLAY_VER(dev_priv) >= 14) {
3391 u8 lane_count = mtl_get_port_width(crtc_state->lane_count);
3392 u32 port_buf = 0;
3393
3394 port_buf |= XELPDP_PORT_WIDTH(lane_count);
3395
3396 if (dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL)
3397 port_buf |= XELPDP_PORT_REVERSAL;
3398
3399 intel_de_rmw(dev_priv, XELPDP_PORT_BUF_CTL1(dev_priv, port),
3400 XELPDP_PORT_WIDTH_MASK | XELPDP_PORT_REVERSAL, port_buf);
3401
3402 buf_ctl |= DDI_PORT_WIDTH(crtc_state->lane_count);
3403
3404 if (DISPLAY_VER(dev_priv) >= 20)
3405 buf_ctl |= XE2LPD_DDI_BUF_D2D_LINK_ENABLE;
3406 } else if (IS_ALDERLAKE_P(dev_priv) && intel_encoder_is_tc(encoder)) {
3407 drm_WARN_ON(&dev_priv->drm, !intel_tc_port_in_legacy_mode(dig_port));
3408 buf_ctl |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
3409 }
3410
3411 intel_de_write(dev_priv, DDI_BUF_CTL(port), buf_ctl);
3412
3413 intel_wait_ddi_buf_active(encoder);
3414}
3415
3416static void intel_enable_ddi(struct intel_atomic_state *state,
3417 struct intel_encoder *encoder,
3418 const struct intel_crtc_state *crtc_state,
3419 const struct drm_connector_state *conn_state)
3420{
3421 struct intel_display *display = to_intel_display(encoder);
3422 struct intel_crtc *pipe_crtc;
3423 int i;
3424
3425 intel_ddi_enable_transcoder_func(encoder, crtc_state);
3426
3427 /* Enable/Disable DP2.0 SDP split config before transcoder */
3428 intel_audio_sdp_split_update(crtc_state);
3429
3430 intel_enable_transcoder(crtc_state);
3431
3432 intel_ddi_wait_for_fec_status(encoder, crtc_state, true);
3433
3434 for_each_pipe_crtc_modeset_enable(display, pipe_crtc, crtc_state, i) {
3435 const struct intel_crtc_state *pipe_crtc_state =
3436 intel_atomic_get_new_crtc_state(state, pipe_crtc);
3437
3438 intel_crtc_vblank_on(pipe_crtc_state);
3439 }
3440
3441 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3442 intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
3443 else
3444 intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
3445
3446 intel_hdcp_enable(state, encoder, crtc_state, conn_state);
3447
3448}
3449
3450static void intel_disable_ddi_dp(struct intel_atomic_state *state,
3451 struct intel_encoder *encoder,
3452 const struct intel_crtc_state *old_crtc_state,
3453 const struct drm_connector_state *old_conn_state)
3454{
3455 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3456 struct intel_connector *connector =
3457 to_intel_connector(old_conn_state->connector);
3458
3459 intel_dp->link_trained = false;
3460
3461 intel_psr_disable(intel_dp, old_crtc_state);
3462 intel_edp_backlight_off(old_conn_state);
3463 /* Disable the decompression in DP Sink */
3464 intel_dp_sink_disable_decompression(state,
3465 connector, old_crtc_state);
3466 /* Disable Ignore_MSA bit in DP Sink */
3467 intel_dp_sink_set_msa_timing_par_ignore_state(intel_dp, old_crtc_state,
3468 false);
3469}
3470
3471static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
3472 struct intel_encoder *encoder,
3473 const struct intel_crtc_state *old_crtc_state,
3474 const struct drm_connector_state *old_conn_state)
3475{
3476 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3477 struct drm_connector *connector = old_conn_state->connector;
3478
3479 if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3480 false, false))
3481 drm_dbg_kms(&i915->drm,
3482 "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3483 connector->base.id, connector->name);
3484}
3485
3486static void intel_disable_ddi(struct intel_atomic_state *state,
3487 struct intel_encoder *encoder,
3488 const struct intel_crtc_state *old_crtc_state,
3489 const struct drm_connector_state *old_conn_state)
3490{
3491 intel_tc_port_link_cancel_reset_work(enc_to_dig_port(encoder));
3492
3493 intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3494
3495 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3496 intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3497 old_conn_state);
3498 else
3499 intel_disable_ddi_dp(state, encoder, old_crtc_state,
3500 old_conn_state);
3501}
3502
3503static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3504 struct intel_encoder *encoder,
3505 const struct intel_crtc_state *crtc_state,
3506 const struct drm_connector_state *conn_state)
3507{
3508 intel_ddi_set_dp_msa(crtc_state, conn_state);
3509
3510 intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3511
3512 intel_backlight_update(state, encoder, crtc_state, conn_state);
3513 drm_connector_update_privacy_screen(conn_state);
3514}
3515
3516static void intel_ddi_update_pipe_hdmi(struct intel_encoder *encoder,
3517 const struct intel_crtc_state *crtc_state,
3518 const struct drm_connector_state *conn_state)
3519{
3520 intel_hdmi_fastset_infoframes(encoder, crtc_state, conn_state);
3521}
3522
3523void intel_ddi_update_pipe(struct intel_atomic_state *state,
3524 struct intel_encoder *encoder,
3525 const struct intel_crtc_state *crtc_state,
3526 const struct drm_connector_state *conn_state)
3527{
3528
3529 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
3530 !intel_encoder_is_mst(encoder))
3531 intel_ddi_update_pipe_dp(state, encoder, crtc_state,
3532 conn_state);
3533
3534 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3535 intel_ddi_update_pipe_hdmi(encoder, crtc_state,
3536 conn_state);
3537
3538 intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
3539}
3540
3541void intel_ddi_update_active_dpll(struct intel_atomic_state *state,
3542 struct intel_encoder *encoder,
3543 struct intel_crtc *crtc)
3544{
3545 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3546 const struct intel_crtc_state *crtc_state =
3547 intel_atomic_get_new_crtc_state(state, crtc);
3548 struct intel_crtc *pipe_crtc;
3549
3550 /* FIXME: Add MTL pll_mgr */
3551 if (DISPLAY_VER(i915) >= 14 || !intel_encoder_is_tc(encoder))
3552 return;
3553
3554 for_each_intel_crtc_in_pipe_mask(&i915->drm, pipe_crtc,
3555 intel_crtc_joined_pipe_mask(crtc_state))
3556 intel_update_active_dpll(state, pipe_crtc, encoder);
3557}
3558
3559static void
3560intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
3561 struct intel_encoder *encoder,
3562 const struct intel_crtc_state *crtc_state,
3563 const struct drm_connector_state *conn_state)
3564{
3565 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3566 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3567 bool is_tc_port = intel_encoder_is_tc(encoder);
3568
3569 if (is_tc_port) {
3570 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3571
3572 intel_tc_port_get_link(dig_port, crtc_state->lane_count);
3573 intel_ddi_update_active_dpll(state, encoder, crtc);
3574 }
3575
3576 main_link_aux_power_domain_get(dig_port, crtc_state);
3577
3578 if (is_tc_port && !intel_tc_port_in_tbt_alt_mode(dig_port))
3579 /*
3580 * Program the lane count for static/dynamic connections on
3581 * Type-C ports. Skip this step for TBT.
3582 */
3583 intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
3584 else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3585 bxt_dpio_phy_set_lane_optim_mask(encoder,
3586 crtc_state->lane_lat_optim_mask);
3587}
3588
3589static void adlp_tbt_to_dp_alt_switch_wa(struct intel_encoder *encoder)
3590{
3591 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3592 enum tc_port tc_port = intel_encoder_to_tc(encoder);
3593 int ln;
3594
3595 for (ln = 0; ln < 2; ln++)
3596 intel_dkl_phy_rmw(i915, DKL_PCS_DW5(tc_port, ln), DKL_PCS_DW5_CORE_SOFTRESET, 0);
3597}
3598
3599static void mtl_ddi_prepare_link_retrain(struct intel_dp *intel_dp,
3600 const struct intel_crtc_state *crtc_state)
3601{
3602 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3603 struct intel_encoder *encoder = &dig_port->base;
3604 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3605 enum port port = encoder->port;
3606 u32 dp_tp_ctl;
3607
3608 /*
3609 * TODO: To train with only a different voltage swing entry is not
3610 * necessary disable and enable port
3611 */
3612 dp_tp_ctl = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3613 if (dp_tp_ctl & DP_TP_CTL_ENABLE)
3614 mtl_disable_ddi_buf(encoder, crtc_state);
3615
3616 /* 6.d Configure and enable DP_TP_CTL with link training pattern 1 selected */
3617 dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
3618 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
3619 dp_tp_ctl |= DP_TP_CTL_MODE_MST;
3620 } else {
3621 dp_tp_ctl |= DP_TP_CTL_MODE_SST;
3622 if (crtc_state->enhanced_framing)
3623 dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3624 }
3625 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3626 intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3627
3628 /* 6.f Enable D2D Link */
3629 mtl_ddi_enable_d2d(encoder);
3630
3631 /* 6.g Configure voltage swing and related IO settings */
3632 encoder->set_signal_levels(encoder, crtc_state);
3633
3634 /* 6.h Configure PORT_BUF_CTL1 */
3635 mtl_port_buf_ctl_program(encoder, crtc_state);
3636
3637 /* 6.i Configure and enable DDI_CTL_DE to start sending valid data to port slice */
3638 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3639 if (DISPLAY_VER(dev_priv) >= 20)
3640 intel_dp->DP |= XE2LPD_DDI_BUF_D2D_LINK_ENABLE;
3641
3642 intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
3643 intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
3644
3645 /* 6.j Poll for PORT_BUF_CTL Idle Status == 0, timeout after 100 us */
3646 intel_wait_ddi_buf_active(encoder);
3647}
3648
3649static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp,
3650 const struct intel_crtc_state *crtc_state)
3651{
3652 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3653 struct intel_encoder *encoder = &dig_port->base;
3654 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3655 enum port port = encoder->port;
3656 u32 dp_tp_ctl, ddi_buf_ctl;
3657 bool wait = false;
3658
3659 dp_tp_ctl = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3660
3661 if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
3662 ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
3663 if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
3664 intel_de_write(dev_priv, DDI_BUF_CTL(port),
3665 ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
3666 wait = true;
3667 }
3668
3669 dp_tp_ctl &= ~DP_TP_CTL_ENABLE;
3670 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3671 intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3672
3673 if (wait)
3674 intel_wait_ddi_buf_idle(dev_priv, port);
3675 }
3676
3677 dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
3678 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
3679 dp_tp_ctl |= DP_TP_CTL_MODE_MST;
3680 } else {
3681 dp_tp_ctl |= DP_TP_CTL_MODE_SST;
3682 if (crtc_state->enhanced_framing)
3683 dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3684 }
3685 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3686 intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3687
3688 if (IS_ALDERLAKE_P(dev_priv) &&
3689 (intel_tc_port_in_dp_alt_mode(dig_port) || intel_tc_port_in_legacy_mode(dig_port)))
3690 adlp_tbt_to_dp_alt_switch_wa(encoder);
3691
3692 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3693 intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
3694 intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
3695
3696 intel_wait_ddi_buf_active(encoder);
3697}
3698
3699static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
3700 const struct intel_crtc_state *crtc_state,
3701 u8 dp_train_pat)
3702{
3703 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3704 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3705 u32 temp;
3706
3707 temp = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3708
3709 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
3710 switch (intel_dp_training_pattern_symbol(dp_train_pat)) {
3711 case DP_TRAINING_PATTERN_DISABLE:
3712 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
3713 break;
3714 case DP_TRAINING_PATTERN_1:
3715 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
3716 break;
3717 case DP_TRAINING_PATTERN_2:
3718 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
3719 break;
3720 case DP_TRAINING_PATTERN_3:
3721 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
3722 break;
3723 case DP_TRAINING_PATTERN_4:
3724 temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
3725 break;
3726 }
3727
3728 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), temp);
3729}
3730
3731static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp,
3732 const struct intel_crtc_state *crtc_state)
3733{
3734 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3735 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3736 enum port port = encoder->port;
3737
3738 intel_de_rmw(dev_priv, dp_tp_ctl_reg(encoder, crtc_state),
3739 DP_TP_CTL_LINK_TRAIN_MASK, DP_TP_CTL_LINK_TRAIN_IDLE);
3740
3741 /*
3742 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
3743 * reason we need to set idle transmission mode is to work around a HW
3744 * issue where we enable the pipe while not in idle link-training mode.
3745 * In this case there is requirement to wait for a minimum number of
3746 * idle patterns to be sent.
3747 */
3748 if (port == PORT_A && DISPLAY_VER(dev_priv) < 12)
3749 return;
3750
3751 if (intel_de_wait_for_set(dev_priv,
3752 dp_tp_status_reg(encoder, crtc_state),
3753 DP_TP_STATUS_IDLE_DONE, 2))
3754 drm_err(&dev_priv->drm,
3755 "Timed out waiting for DP idle patterns\n");
3756}
3757
3758static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3759 enum transcoder cpu_transcoder)
3760{
3761 if (cpu_transcoder == TRANSCODER_EDP)
3762 return false;
3763
3764 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO_MMIO))
3765 return false;
3766
3767 return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
3768 AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3769}
3770
3771static int tgl_ddi_min_voltage_level(const struct intel_crtc_state *crtc_state)
3772{
3773 if (crtc_state->port_clock > 594000)
3774 return 2;
3775 else
3776 return 0;
3777}
3778
3779static int jsl_ddi_min_voltage_level(const struct intel_crtc_state *crtc_state)
3780{
3781 if (crtc_state->port_clock > 594000)
3782 return 3;
3783 else
3784 return 0;
3785}
3786
3787static int icl_ddi_min_voltage_level(const struct intel_crtc_state *crtc_state)
3788{
3789 if (crtc_state->port_clock > 594000)
3790 return 1;
3791 else
3792 return 0;
3793}
3794
3795void intel_ddi_compute_min_voltage_level(struct intel_crtc_state *crtc_state)
3796{
3797 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3798
3799 if (DISPLAY_VER(dev_priv) >= 14)
3800 crtc_state->min_voltage_level = icl_ddi_min_voltage_level(crtc_state);
3801 else if (DISPLAY_VER(dev_priv) >= 12)
3802 crtc_state->min_voltage_level = tgl_ddi_min_voltage_level(crtc_state);
3803 else if (IS_JASPERLAKE(dev_priv) || IS_ELKHARTLAKE(dev_priv))
3804 crtc_state->min_voltage_level = jsl_ddi_min_voltage_level(crtc_state);
3805 else if (DISPLAY_VER(dev_priv) >= 11)
3806 crtc_state->min_voltage_level = icl_ddi_min_voltage_level(crtc_state);
3807}
3808
3809static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
3810 enum transcoder cpu_transcoder)
3811{
3812 u32 master_select;
3813
3814 if (DISPLAY_VER(dev_priv) >= 11) {
3815 u32 ctl2 = intel_de_read(dev_priv,
3816 TRANS_DDI_FUNC_CTL2(dev_priv, cpu_transcoder));
3817
3818 if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
3819 return INVALID_TRANSCODER;
3820
3821 master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
3822 } else {
3823 u32 ctl = intel_de_read(dev_priv,
3824 TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder));
3825
3826 if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
3827 return INVALID_TRANSCODER;
3828
3829 master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
3830 }
3831
3832 if (master_select == 0)
3833 return TRANSCODER_EDP;
3834 else
3835 return master_select - 1;
3836}
3837
3838static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
3839{
3840 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3841 u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
3842 BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
3843 enum transcoder cpu_transcoder;
3844
3845 crtc_state->master_transcoder =
3846 bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
3847
3848 for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
3849 enum intel_display_power_domain power_domain;
3850 intel_wakeref_t trans_wakeref;
3851
3852 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
3853 trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
3854 power_domain);
3855
3856 if (!trans_wakeref)
3857 continue;
3858
3859 if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
3860 crtc_state->cpu_transcoder)
3861 crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
3862
3863 intel_display_power_put(dev_priv, power_domain, trans_wakeref);
3864 }
3865
3866 drm_WARN_ON(&dev_priv->drm,
3867 crtc_state->master_transcoder != INVALID_TRANSCODER &&
3868 crtc_state->sync_mode_slaves_mask);
3869}
3870
3871static void intel_ddi_read_func_ctl(struct intel_encoder *encoder,
3872 struct intel_crtc_state *pipe_config)
3873{
3874 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3875 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3876 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3877 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3878 u32 temp, flags = 0;
3879
3880 temp = intel_de_read(dev_priv,
3881 TRANS_DDI_FUNC_CTL(dev_priv, cpu_transcoder));
3882 if (temp & TRANS_DDI_PHSYNC)
3883 flags |= DRM_MODE_FLAG_PHSYNC;
3884 else
3885 flags |= DRM_MODE_FLAG_NHSYNC;
3886 if (temp & TRANS_DDI_PVSYNC)
3887 flags |= DRM_MODE_FLAG_PVSYNC;
3888 else
3889 flags |= DRM_MODE_FLAG_NVSYNC;
3890
3891 pipe_config->hw.adjusted_mode.flags |= flags;
3892
3893 switch (temp & TRANS_DDI_BPC_MASK) {
3894 case TRANS_DDI_BPC_6:
3895 pipe_config->pipe_bpp = 18;
3896 break;
3897 case TRANS_DDI_BPC_8:
3898 pipe_config->pipe_bpp = 24;
3899 break;
3900 case TRANS_DDI_BPC_10:
3901 pipe_config->pipe_bpp = 30;
3902 break;
3903 case TRANS_DDI_BPC_12:
3904 pipe_config->pipe_bpp = 36;
3905 break;
3906 default:
3907 break;
3908 }
3909
3910 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
3911 case TRANS_DDI_MODE_SELECT_HDMI:
3912 pipe_config->has_hdmi_sink = true;
3913
3914 pipe_config->infoframes.enable |=
3915 intel_hdmi_infoframes_enabled(encoder, pipe_config);
3916
3917 if (pipe_config->infoframes.enable)
3918 pipe_config->has_infoframe = true;
3919
3920 if (temp & TRANS_DDI_HDMI_SCRAMBLING)
3921 pipe_config->hdmi_scrambling = true;
3922 if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
3923 pipe_config->hdmi_high_tmds_clock_ratio = true;
3924 fallthrough;
3925 case TRANS_DDI_MODE_SELECT_DVI:
3926 pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
3927 if (DISPLAY_VER(dev_priv) >= 14)
3928 pipe_config->lane_count =
3929 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3930 else
3931 pipe_config->lane_count = 4;
3932 break;
3933 case TRANS_DDI_MODE_SELECT_DP_SST:
3934 if (encoder->type == INTEL_OUTPUT_EDP)
3935 pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3936 else
3937 pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3938 pipe_config->lane_count =
3939 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3940
3941 intel_cpu_transcoder_get_m1_n1(crtc, cpu_transcoder,
3942 &pipe_config->dp_m_n);
3943 intel_cpu_transcoder_get_m2_n2(crtc, cpu_transcoder,
3944 &pipe_config->dp_m2_n2);
3945
3946 pipe_config->enhanced_framing =
3947 intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, pipe_config)) &
3948 DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3949
3950 if (DISPLAY_VER(dev_priv) >= 11)
3951 pipe_config->fec_enable =
3952 intel_de_read(dev_priv,
3953 dp_tp_ctl_reg(encoder, pipe_config)) & DP_TP_CTL_FEC_ENABLE;
3954
3955 if (dig_port->lspcon.active && intel_dp_has_hdmi_sink(&dig_port->dp))
3956 pipe_config->infoframes.enable |=
3957 intel_lspcon_infoframes_enabled(encoder, pipe_config);
3958 else
3959 pipe_config->infoframes.enable |=
3960 intel_hdmi_infoframes_enabled(encoder, pipe_config);
3961 break;
3962 case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
3963 if (!HAS_DP20(dev_priv)) {
3964 /* FDI */
3965 pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
3966 pipe_config->enhanced_framing =
3967 intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, pipe_config)) &
3968 DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3969 break;
3970 }
3971 fallthrough; /* 128b/132b */
3972 case TRANS_DDI_MODE_SELECT_DP_MST:
3973 pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
3974 pipe_config->lane_count =
3975 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3976
3977 if (DISPLAY_VER(dev_priv) >= 12)
3978 pipe_config->mst_master_transcoder =
3979 REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
3980
3981 intel_cpu_transcoder_get_m1_n1(crtc, cpu_transcoder,
3982 &pipe_config->dp_m_n);
3983
3984 if (DISPLAY_VER(dev_priv) >= 11)
3985 pipe_config->fec_enable =
3986 intel_de_read(dev_priv,
3987 dp_tp_ctl_reg(encoder, pipe_config)) & DP_TP_CTL_FEC_ENABLE;
3988
3989 pipe_config->infoframes.enable |=
3990 intel_hdmi_infoframes_enabled(encoder, pipe_config);
3991 break;
3992 default:
3993 break;
3994 }
3995}
3996
3997static void intel_ddi_get_config(struct intel_encoder *encoder,
3998 struct intel_crtc_state *pipe_config)
3999{
4000 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4001 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4002
4003 /* XXX: DSI transcoder paranoia */
4004 if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
4005 return;
4006
4007 intel_ddi_read_func_ctl(encoder, pipe_config);
4008
4009 intel_ddi_mso_get_config(encoder, pipe_config);
4010
4011 pipe_config->has_audio =
4012 intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
4013
4014 if (encoder->type == INTEL_OUTPUT_EDP)
4015 intel_edp_fixup_vbt_bpp(encoder, pipe_config->pipe_bpp);
4016
4017 ddi_dotclock_get(pipe_config);
4018
4019 if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4020 pipe_config->lane_lat_optim_mask =
4021 bxt_dpio_phy_get_lane_lat_optim_mask(encoder);
4022
4023 intel_ddi_compute_min_voltage_level(pipe_config);
4024
4025 intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
4026
4027 intel_read_infoframe(encoder, pipe_config,
4028 HDMI_INFOFRAME_TYPE_AVI,
4029 &pipe_config->infoframes.avi);
4030 intel_read_infoframe(encoder, pipe_config,
4031 HDMI_INFOFRAME_TYPE_SPD,
4032 &pipe_config->infoframes.spd);
4033 intel_read_infoframe(encoder, pipe_config,
4034 HDMI_INFOFRAME_TYPE_VENDOR,
4035 &pipe_config->infoframes.hdmi);
4036 intel_read_infoframe(encoder, pipe_config,
4037 HDMI_INFOFRAME_TYPE_DRM,
4038 &pipe_config->infoframes.drm);
4039
4040 if (DISPLAY_VER(dev_priv) >= 8)
4041 bdw_get_trans_port_sync_config(pipe_config);
4042
4043 intel_psr_get_config(encoder, pipe_config);
4044
4045 intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
4046 intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
4047 intel_read_dp_sdp(encoder, pipe_config, DP_SDP_ADAPTIVE_SYNC);
4048
4049 intel_audio_codec_get_config(encoder, pipe_config);
4050}
4051
4052void intel_ddi_get_clock(struct intel_encoder *encoder,
4053 struct intel_crtc_state *crtc_state,
4054 struct intel_shared_dpll *pll)
4055{
4056 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4057 enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
4058 struct icl_port_dpll *port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
4059 bool pll_active;
4060
4061 if (drm_WARN_ON(&i915->drm, !pll))
4062 return;
4063
4064 port_dpll->pll = pll;
4065 pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
4066 drm_WARN_ON(&i915->drm, !pll_active);
4067
4068 icl_set_active_port_dpll(crtc_state, port_dpll_id);
4069
4070 crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
4071 &crtc_state->dpll_hw_state);
4072}
4073
4074static void mtl_ddi_get_config(struct intel_encoder *encoder,
4075 struct intel_crtc_state *crtc_state)
4076{
4077 intel_cx0pll_readout_hw_state(encoder, &crtc_state->dpll_hw_state.cx0pll);
4078
4079 if (crtc_state->dpll_hw_state.cx0pll.tbt_mode)
4080 crtc_state->port_clock = intel_mtl_tbt_calc_port_clock(encoder);
4081 else
4082 crtc_state->port_clock = intel_cx0pll_calc_port_clock(encoder, &crtc_state->dpll_hw_state.cx0pll);
4083
4084 intel_ddi_get_config(encoder, crtc_state);
4085}
4086
4087static void dg2_ddi_get_config(struct intel_encoder *encoder,
4088 struct intel_crtc_state *crtc_state)
4089{
4090 intel_mpllb_readout_hw_state(encoder, &crtc_state->dpll_hw_state.mpllb);
4091 crtc_state->port_clock = intel_mpllb_calc_port_clock(encoder, &crtc_state->dpll_hw_state.mpllb);
4092
4093 intel_ddi_get_config(encoder, crtc_state);
4094}
4095
4096static void adls_ddi_get_config(struct intel_encoder *encoder,
4097 struct intel_crtc_state *crtc_state)
4098{
4099 intel_ddi_get_clock(encoder, crtc_state, adls_ddi_get_pll(encoder));
4100 intel_ddi_get_config(encoder, crtc_state);
4101}
4102
4103static void rkl_ddi_get_config(struct intel_encoder *encoder,
4104 struct intel_crtc_state *crtc_state)
4105{
4106 intel_ddi_get_clock(encoder, crtc_state, rkl_ddi_get_pll(encoder));
4107 intel_ddi_get_config(encoder, crtc_state);
4108}
4109
4110static void dg1_ddi_get_config(struct intel_encoder *encoder,
4111 struct intel_crtc_state *crtc_state)
4112{
4113 intel_ddi_get_clock(encoder, crtc_state, dg1_ddi_get_pll(encoder));
4114 intel_ddi_get_config(encoder, crtc_state);
4115}
4116
4117static void icl_ddi_combo_get_config(struct intel_encoder *encoder,
4118 struct intel_crtc_state *crtc_state)
4119{
4120 intel_ddi_get_clock(encoder, crtc_state, icl_ddi_combo_get_pll(encoder));
4121 intel_ddi_get_config(encoder, crtc_state);
4122}
4123
4124static bool icl_ddi_tc_pll_is_tbt(const struct intel_shared_dpll *pll)
4125{
4126 return pll->info->id == DPLL_ID_ICL_TBTPLL;
4127}
4128
4129static enum icl_port_dpll_id
4130icl_ddi_tc_port_pll_type(struct intel_encoder *encoder,
4131 const struct intel_crtc_state *crtc_state)
4132{
4133 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4134 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
4135
4136 if (drm_WARN_ON(&i915->drm, !pll))
4137 return ICL_PORT_DPLL_DEFAULT;
4138
4139 if (icl_ddi_tc_pll_is_tbt(pll))
4140 return ICL_PORT_DPLL_DEFAULT;
4141 else
4142 return ICL_PORT_DPLL_MG_PHY;
4143}
4144
4145enum icl_port_dpll_id
4146intel_ddi_port_pll_type(struct intel_encoder *encoder,
4147 const struct intel_crtc_state *crtc_state)
4148{
4149 if (!encoder->port_pll_type)
4150 return ICL_PORT_DPLL_DEFAULT;
4151
4152 return encoder->port_pll_type(encoder, crtc_state);
4153}
4154
4155static void icl_ddi_tc_get_clock(struct intel_encoder *encoder,
4156 struct intel_crtc_state *crtc_state,
4157 struct intel_shared_dpll *pll)
4158{
4159 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4160 enum icl_port_dpll_id port_dpll_id;
4161 struct icl_port_dpll *port_dpll;
4162 bool pll_active;
4163
4164 if (drm_WARN_ON(&i915->drm, !pll))
4165 return;
4166
4167 if (icl_ddi_tc_pll_is_tbt(pll))
4168 port_dpll_id = ICL_PORT_DPLL_DEFAULT;
4169 else
4170 port_dpll_id = ICL_PORT_DPLL_MG_PHY;
4171
4172 port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
4173
4174 port_dpll->pll = pll;
4175 pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
4176 drm_WARN_ON(&i915->drm, !pll_active);
4177
4178 icl_set_active_port_dpll(crtc_state, port_dpll_id);
4179
4180 if (icl_ddi_tc_pll_is_tbt(crtc_state->shared_dpll))
4181 crtc_state->port_clock = icl_calc_tbt_pll_link(i915, encoder->port);
4182 else
4183 crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
4184 &crtc_state->dpll_hw_state);
4185}
4186
4187static void icl_ddi_tc_get_config(struct intel_encoder *encoder,
4188 struct intel_crtc_state *crtc_state)
4189{
4190 icl_ddi_tc_get_clock(encoder, crtc_state, icl_ddi_tc_get_pll(encoder));
4191 intel_ddi_get_config(encoder, crtc_state);
4192}
4193
4194static void bxt_ddi_get_config(struct intel_encoder *encoder,
4195 struct intel_crtc_state *crtc_state)
4196{
4197 intel_ddi_get_clock(encoder, crtc_state, bxt_ddi_get_pll(encoder));
4198 intel_ddi_get_config(encoder, crtc_state);
4199}
4200
4201static void skl_ddi_get_config(struct intel_encoder *encoder,
4202 struct intel_crtc_state *crtc_state)
4203{
4204 intel_ddi_get_clock(encoder, crtc_state, skl_ddi_get_pll(encoder));
4205 intel_ddi_get_config(encoder, crtc_state);
4206}
4207
4208void hsw_ddi_get_config(struct intel_encoder *encoder,
4209 struct intel_crtc_state *crtc_state)
4210{
4211 intel_ddi_get_clock(encoder, crtc_state, hsw_ddi_get_pll(encoder));
4212 intel_ddi_get_config(encoder, crtc_state);
4213}
4214
4215static void intel_ddi_sync_state(struct intel_encoder *encoder,
4216 const struct intel_crtc_state *crtc_state)
4217{
4218 if (intel_encoder_is_tc(encoder))
4219 intel_tc_port_sanitize_mode(enc_to_dig_port(encoder),
4220 crtc_state);
4221
4222 if ((crtc_state && intel_crtc_has_dp_encoder(crtc_state)) ||
4223 (!crtc_state && intel_encoder_is_dp(encoder)))
4224 intel_dp_sync_state(encoder, crtc_state);
4225}
4226
4227static bool intel_ddi_initial_fastset_check(struct intel_encoder *encoder,
4228 struct intel_crtc_state *crtc_state)
4229{
4230 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4231 bool fastset = true;
4232
4233 if (intel_encoder_is_tc(encoder)) {
4234 drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Forcing full modeset to compute TC port DPLLs\n",
4235 encoder->base.base.id, encoder->base.name);
4236 crtc_state->uapi.mode_changed = true;
4237 fastset = false;
4238 }
4239
4240 if (intel_crtc_has_dp_encoder(crtc_state) &&
4241 !intel_dp_initial_fastset_check(encoder, crtc_state))
4242 fastset = false;
4243
4244 return fastset;
4245}
4246
4247static enum intel_output_type
4248intel_ddi_compute_output_type(struct intel_encoder *encoder,
4249 struct intel_crtc_state *crtc_state,
4250 struct drm_connector_state *conn_state)
4251{
4252 switch (conn_state->connector->connector_type) {
4253 case DRM_MODE_CONNECTOR_HDMIA:
4254 return INTEL_OUTPUT_HDMI;
4255 case DRM_MODE_CONNECTOR_eDP:
4256 return INTEL_OUTPUT_EDP;
4257 case DRM_MODE_CONNECTOR_DisplayPort:
4258 return INTEL_OUTPUT_DP;
4259 default:
4260 MISSING_CASE(conn_state->connector->connector_type);
4261 return INTEL_OUTPUT_UNUSED;
4262 }
4263}
4264
4265static int intel_ddi_compute_config(struct intel_encoder *encoder,
4266 struct intel_crtc_state *pipe_config,
4267 struct drm_connector_state *conn_state)
4268{
4269 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
4270 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4271 enum port port = encoder->port;
4272 int ret;
4273
4274 if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
4275 pipe_config->cpu_transcoder = TRANSCODER_EDP;
4276
4277 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
4278 pipe_config->has_hdmi_sink =
4279 intel_hdmi_compute_has_hdmi_sink(encoder, pipe_config, conn_state);
4280
4281 ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
4282 } else {
4283 ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
4284 }
4285
4286 if (ret)
4287 return ret;
4288
4289 if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
4290 pipe_config->cpu_transcoder == TRANSCODER_EDP)
4291 pipe_config->pch_pfit.force_thru =
4292 pipe_config->pch_pfit.enabled ||
4293 pipe_config->crc_enabled;
4294
4295 if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4296 pipe_config->lane_lat_optim_mask =
4297 bxt_dpio_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
4298
4299 intel_ddi_compute_min_voltage_level(pipe_config);
4300
4301 return 0;
4302}
4303
4304static bool mode_equal(const struct drm_display_mode *mode1,
4305 const struct drm_display_mode *mode2)
4306{
4307 return drm_mode_match(mode1, mode2,
4308 DRM_MODE_MATCH_TIMINGS |
4309 DRM_MODE_MATCH_FLAGS |
4310 DRM_MODE_MATCH_3D_FLAGS) &&
4311 mode1->clock == mode2->clock; /* we want an exact match */
4312}
4313
4314static bool m_n_equal(const struct intel_link_m_n *m_n_1,
4315 const struct intel_link_m_n *m_n_2)
4316{
4317 return m_n_1->tu == m_n_2->tu &&
4318 m_n_1->data_m == m_n_2->data_m &&
4319 m_n_1->data_n == m_n_2->data_n &&
4320 m_n_1->link_m == m_n_2->link_m &&
4321 m_n_1->link_n == m_n_2->link_n;
4322}
4323
4324static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
4325 const struct intel_crtc_state *crtc_state2)
4326{
4327 /*
4328 * FIXME the modeset sequence is currently wrong and
4329 * can't deal with joiner + port sync at the same time.
4330 */
4331 return crtc_state1->hw.active && crtc_state2->hw.active &&
4332 !crtc_state1->joiner_pipes && !crtc_state2->joiner_pipes &&
4333 crtc_state1->output_types == crtc_state2->output_types &&
4334 crtc_state1->output_format == crtc_state2->output_format &&
4335 crtc_state1->lane_count == crtc_state2->lane_count &&
4336 crtc_state1->port_clock == crtc_state2->port_clock &&
4337 mode_equal(&crtc_state1->hw.adjusted_mode,
4338 &crtc_state2->hw.adjusted_mode) &&
4339 m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
4340}
4341
4342static u8
4343intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
4344 int tile_group_id)
4345{
4346 struct drm_connector *connector;
4347 const struct drm_connector_state *conn_state;
4348 struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
4349 struct intel_atomic_state *state =
4350 to_intel_atomic_state(ref_crtc_state->uapi.state);
4351 u8 transcoders = 0;
4352 int i;
4353
4354 /*
4355 * We don't enable port sync on BDW due to missing w/as and
4356 * due to not having adjusted the modeset sequence appropriately.
4357 */
4358 if (DISPLAY_VER(dev_priv) < 9)
4359 return 0;
4360
4361 if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
4362 return 0;
4363
4364 for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
4365 struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
4366 const struct intel_crtc_state *crtc_state;
4367
4368 if (!crtc)
4369 continue;
4370
4371 if (!connector->has_tile ||
4372 connector->tile_group->id !=
4373 tile_group_id)
4374 continue;
4375 crtc_state = intel_atomic_get_new_crtc_state(state,
4376 crtc);
4377 if (!crtcs_port_sync_compatible(ref_crtc_state,
4378 crtc_state))
4379 continue;
4380 transcoders |= BIT(crtc_state->cpu_transcoder);
4381 }
4382
4383 return transcoders;
4384}
4385
4386static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
4387 struct intel_crtc_state *crtc_state,
4388 struct drm_connector_state *conn_state)
4389{
4390 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4391 struct drm_connector *connector = conn_state->connector;
4392 u8 port_sync_transcoders = 0;
4393
4394 drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]\n",
4395 encoder->base.base.id, encoder->base.name,
4396 crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
4397
4398 if (connector->has_tile)
4399 port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
4400 connector->tile_group->id);
4401
4402 /*
4403 * EDP Transcoders cannot be ensalved
4404 * make them a master always when present
4405 */
4406 if (port_sync_transcoders & BIT(TRANSCODER_EDP))
4407 crtc_state->master_transcoder = TRANSCODER_EDP;
4408 else
4409 crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
4410
4411 if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
4412 crtc_state->master_transcoder = INVALID_TRANSCODER;
4413 crtc_state->sync_mode_slaves_mask =
4414 port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
4415 }
4416
4417 return 0;
4418}
4419
4420static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
4421{
4422 struct drm_i915_private *i915 = to_i915(encoder->dev);
4423 struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
4424
4425 intel_dp_encoder_flush_work(encoder);
4426 if (intel_encoder_is_tc(&dig_port->base))
4427 intel_tc_port_cleanup(dig_port);
4428 intel_display_power_flush_work(i915);
4429
4430 drm_encoder_cleanup(encoder);
4431 kfree(dig_port->hdcp_port_data.streams);
4432 kfree(dig_port);
4433}
4434
4435static void intel_ddi_encoder_reset(struct drm_encoder *encoder)
4436{
4437 struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(encoder));
4438 struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
4439
4440 intel_dp->reset_link_params = true;
4441 intel_dp_invalidate_source_oui(intel_dp);
4442
4443 intel_pps_encoder_reset(intel_dp);
4444
4445 if (intel_encoder_is_tc(&dig_port->base))
4446 intel_tc_port_init_mode(dig_port);
4447}
4448
4449static int intel_ddi_encoder_late_register(struct drm_encoder *_encoder)
4450{
4451 struct intel_encoder *encoder = to_intel_encoder(_encoder);
4452
4453 intel_tc_port_link_reset(enc_to_dig_port(encoder));
4454
4455 return 0;
4456}
4457
4458static const struct drm_encoder_funcs intel_ddi_funcs = {
4459 .reset = intel_ddi_encoder_reset,
4460 .destroy = intel_ddi_encoder_destroy,
4461 .late_register = intel_ddi_encoder_late_register,
4462};
4463
4464static struct intel_connector *
4465intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
4466{
4467 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
4468 struct intel_connector *connector;
4469 enum port port = dig_port->base.port;
4470
4471 connector = intel_connector_alloc();
4472 if (!connector)
4473 return NULL;
4474
4475 dig_port->dp.output_reg = DDI_BUF_CTL(port);
4476 if (DISPLAY_VER(i915) >= 14)
4477 dig_port->dp.prepare_link_retrain = mtl_ddi_prepare_link_retrain;
4478 else
4479 dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
4480 dig_port->dp.set_link_train = intel_ddi_set_link_train;
4481 dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
4482
4483 dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
4484 dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
4485
4486 if (!intel_dp_init_connector(dig_port, connector)) {
4487 kfree(connector);
4488 return NULL;
4489 }
4490
4491 if (dig_port->base.type == INTEL_OUTPUT_EDP) {
4492 struct drm_device *dev = dig_port->base.base.dev;
4493 struct drm_privacy_screen *privacy_screen;
4494
4495 privacy_screen = drm_privacy_screen_get(dev->dev, NULL);
4496 if (!IS_ERR(privacy_screen)) {
4497 drm_connector_attach_privacy_screen_provider(&connector->base,
4498 privacy_screen);
4499 } else if (PTR_ERR(privacy_screen) != -ENODEV) {
4500 drm_warn(dev, "Error getting privacy-screen\n");
4501 }
4502 }
4503
4504 return connector;
4505}
4506
4507static int intel_hdmi_reset_link(struct intel_encoder *encoder,
4508 struct drm_modeset_acquire_ctx *ctx)
4509{
4510 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4511 struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
4512 struct intel_connector *connector = hdmi->attached_connector;
4513 struct i2c_adapter *ddc = connector->base.ddc;
4514 struct drm_connector_state *conn_state;
4515 struct intel_crtc_state *crtc_state;
4516 struct intel_crtc *crtc;
4517 u8 config;
4518 int ret;
4519
4520 if (connector->base.status != connector_status_connected)
4521 return 0;
4522
4523 ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4524 ctx);
4525 if (ret)
4526 return ret;
4527
4528 conn_state = connector->base.state;
4529
4530 crtc = to_intel_crtc(conn_state->crtc);
4531 if (!crtc)
4532 return 0;
4533
4534 ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4535 if (ret)
4536 return ret;
4537
4538 crtc_state = to_intel_crtc_state(crtc->base.state);
4539
4540 drm_WARN_ON(&dev_priv->drm,
4541 !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4542
4543 if (!crtc_state->hw.active)
4544 return 0;
4545
4546 if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4547 !crtc_state->hdmi_scrambling)
4548 return 0;
4549
4550 if (conn_state->commit &&
4551 !try_wait_for_completion(&conn_state->commit->hw_done))
4552 return 0;
4553
4554 ret = drm_scdc_readb(ddc, SCDC_TMDS_CONFIG, &config);
4555 if (ret < 0) {
4556 drm_err(&dev_priv->drm, "[CONNECTOR:%d:%s] Failed to read TMDS config: %d\n",
4557 connector->base.base.id, connector->base.name, ret);
4558 return 0;
4559 }
4560
4561 if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4562 crtc_state->hdmi_high_tmds_clock_ratio &&
4563 !!(config & SCDC_SCRAMBLING_ENABLE) ==
4564 crtc_state->hdmi_scrambling)
4565 return 0;
4566
4567 /*
4568 * HDMI 2.0 says that one should not send scrambled data
4569 * prior to configuring the sink scrambling, and that
4570 * TMDS clock/data transmission should be suspended when
4571 * changing the TMDS clock rate in the sink. So let's
4572 * just do a full modeset here, even though some sinks
4573 * would be perfectly happy if were to just reconfigure
4574 * the SCDC settings on the fly.
4575 */
4576 return intel_modeset_commit_pipes(dev_priv, BIT(crtc->pipe), ctx);
4577}
4578
4579static void intel_ddi_link_check(struct intel_encoder *encoder)
4580{
4581 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4582 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4583
4584 /* TODO: Move checking the HDMI link state here as well. */
4585 drm_WARN_ON(&i915->drm, !dig_port->dp.attached_connector);
4586
4587 intel_dp_link_check(encoder);
4588}
4589
4590static enum intel_hotplug_state
4591intel_ddi_hotplug(struct intel_encoder *encoder,
4592 struct intel_connector *connector)
4593{
4594 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4595 struct intel_dp *intel_dp = &dig_port->dp;
4596 bool is_tc = intel_encoder_is_tc(encoder);
4597 struct drm_modeset_acquire_ctx ctx;
4598 enum intel_hotplug_state state;
4599 int ret;
4600
4601 if (intel_dp_test_phy(intel_dp))
4602 return INTEL_HOTPLUG_UNCHANGED;
4603
4604 state = intel_encoder_hotplug(encoder, connector);
4605
4606 if (!intel_tc_port_link_reset(dig_port)) {
4607 if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA) {
4608 intel_modeset_lock_ctx_retry(&ctx, NULL, 0, ret)
4609 ret = intel_hdmi_reset_link(encoder, &ctx);
4610 drm_WARN_ON(encoder->base.dev, ret);
4611 } else {
4612 intel_dp_check_link_state(intel_dp);
4613 }
4614 }
4615
4616 /*
4617 * Unpowered type-c dongles can take some time to boot and be
4618 * responsible, so here giving some time to those dongles to power up
4619 * and then retrying the probe.
4620 *
4621 * On many platforms the HDMI live state signal is known to be
4622 * unreliable, so we can't use it to detect if a sink is connected or
4623 * not. Instead we detect if it's connected based on whether we can
4624 * read the EDID or not. That in turn has a problem during disconnect,
4625 * since the HPD interrupt may be raised before the DDC lines get
4626 * disconnected (due to how the required length of DDC vs. HPD
4627 * connector pins are specified) and so we'll still be able to get a
4628 * valid EDID. To solve this schedule another detection cycle if this
4629 * time around we didn't detect any change in the sink's connection
4630 * status.
4631 *
4632 * Type-c connectors which get their HPD signal deasserted then
4633 * reasserted, without unplugging/replugging the sink from the
4634 * connector, introduce a delay until the AUX channel communication
4635 * becomes functional. Retry the detection for 5 seconds on type-c
4636 * connectors to account for this delay.
4637 */
4638 if (state == INTEL_HOTPLUG_UNCHANGED &&
4639 connector->hotplug_retries < (is_tc ? 5 : 1) &&
4640 !dig_port->dp.is_mst)
4641 state = INTEL_HOTPLUG_RETRY;
4642
4643 return state;
4644}
4645
4646static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4647{
4648 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4649 u32 bit = dev_priv->display.hotplug.pch_hpd[encoder->hpd_pin];
4650
4651 return intel_de_read(dev_priv, SDEISR) & bit;
4652}
4653
4654static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4655{
4656 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4657 u32 bit = dev_priv->display.hotplug.hpd[encoder->hpd_pin];
4658
4659 return intel_de_read(dev_priv, DEISR) & bit;
4660}
4661
4662static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4663{
4664 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4665 u32 bit = dev_priv->display.hotplug.hpd[encoder->hpd_pin];
4666
4667 return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4668}
4669
4670static struct intel_connector *
4671intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4672{
4673 struct intel_connector *connector;
4674 enum port port = dig_port->base.port;
4675
4676 connector = intel_connector_alloc();
4677 if (!connector)
4678 return NULL;
4679
4680 dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
4681 intel_hdmi_init_connector(dig_port, connector);
4682
4683 return connector;
4684}
4685
4686static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4687{
4688 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4689
4690 if (dig_port->base.port != PORT_A)
4691 return false;
4692
4693 if (dig_port->saved_port_bits & DDI_A_4_LANES)
4694 return false;
4695
4696 /* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4697 * supported configuration
4698 */
4699 if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4700 return true;
4701
4702 return false;
4703}
4704
4705static int
4706intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4707{
4708 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4709 enum port port = dig_port->base.port;
4710 int max_lanes = 4;
4711
4712 if (DISPLAY_VER(dev_priv) >= 11)
4713 return max_lanes;
4714
4715 if (port == PORT_A || port == PORT_E) {
4716 if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4717 max_lanes = port == PORT_A ? 4 : 0;
4718 else
4719 /* Both A and E share 2 lanes */
4720 max_lanes = 2;
4721 }
4722
4723 /*
4724 * Some BIOS might fail to set this bit on port A if eDP
4725 * wasn't lit up at boot. Force this bit set when needed
4726 * so we use the proper lane count for our calculations.
4727 */
4728 if (intel_ddi_a_force_4_lanes(dig_port)) {
4729 drm_dbg_kms(&dev_priv->drm,
4730 "Forcing DDI_A_4_LANES for port A\n");
4731 dig_port->saved_port_bits |= DDI_A_4_LANES;
4732 max_lanes = 4;
4733 }
4734
4735 return max_lanes;
4736}
4737
4738static enum hpd_pin xelpd_hpd_pin(struct drm_i915_private *dev_priv,
4739 enum port port)
4740{
4741 if (port >= PORT_D_XELPD)
4742 return HPD_PORT_D + port - PORT_D_XELPD;
4743 else if (port >= PORT_TC1)
4744 return HPD_PORT_TC1 + port - PORT_TC1;
4745 else
4746 return HPD_PORT_A + port - PORT_A;
4747}
4748
4749static enum hpd_pin dg1_hpd_pin(struct drm_i915_private *dev_priv,
4750 enum port port)
4751{
4752 if (port >= PORT_TC1)
4753 return HPD_PORT_C + port - PORT_TC1;
4754 else
4755 return HPD_PORT_A + port - PORT_A;
4756}
4757
4758static enum hpd_pin tgl_hpd_pin(struct drm_i915_private *dev_priv,
4759 enum port port)
4760{
4761 if (port >= PORT_TC1)
4762 return HPD_PORT_TC1 + port - PORT_TC1;
4763 else
4764 return HPD_PORT_A + port - PORT_A;
4765}
4766
4767static enum hpd_pin rkl_hpd_pin(struct drm_i915_private *dev_priv,
4768 enum port port)
4769{
4770 if (HAS_PCH_TGP(dev_priv))
4771 return tgl_hpd_pin(dev_priv, port);
4772
4773 if (port >= PORT_TC1)
4774 return HPD_PORT_C + port - PORT_TC1;
4775 else
4776 return HPD_PORT_A + port - PORT_A;
4777}
4778
4779static enum hpd_pin icl_hpd_pin(struct drm_i915_private *dev_priv,
4780 enum port port)
4781{
4782 if (port >= PORT_C)
4783 return HPD_PORT_TC1 + port - PORT_C;
4784 else
4785 return HPD_PORT_A + port - PORT_A;
4786}
4787
4788static enum hpd_pin ehl_hpd_pin(struct drm_i915_private *dev_priv,
4789 enum port port)
4790{
4791 if (port == PORT_D)
4792 return HPD_PORT_A;
4793
4794 if (HAS_PCH_TGP(dev_priv))
4795 return icl_hpd_pin(dev_priv, port);
4796
4797 return HPD_PORT_A + port - PORT_A;
4798}
4799
4800static enum hpd_pin skl_hpd_pin(struct drm_i915_private *dev_priv, enum port port)
4801{
4802 if (HAS_PCH_TGP(dev_priv))
4803 return icl_hpd_pin(dev_priv, port);
4804
4805 return HPD_PORT_A + port - PORT_A;
4806}
4807
4808static bool intel_ddi_is_tc(struct drm_i915_private *i915, enum port port)
4809{
4810 if (DISPLAY_VER(i915) >= 12)
4811 return port >= PORT_TC1;
4812 else if (DISPLAY_VER(i915) >= 11)
4813 return port >= PORT_C;
4814 else
4815 return false;
4816}
4817
4818static void intel_ddi_encoder_suspend(struct intel_encoder *encoder)
4819{
4820 intel_dp_encoder_suspend(encoder);
4821}
4822
4823static void intel_ddi_tc_encoder_suspend_complete(struct intel_encoder *encoder)
4824{
4825 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4826 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4827
4828 /*
4829 * TODO: Move this to intel_dp_encoder_suspend(),
4830 * once modeset locking around that is removed.
4831 */
4832 intel_encoder_link_check_flush_work(encoder);
4833 intel_tc_port_suspend(dig_port);
4834}
4835
4836static void intel_ddi_encoder_shutdown(struct intel_encoder *encoder)
4837{
4838 intel_dp_encoder_shutdown(encoder);
4839 intel_hdmi_encoder_shutdown(encoder);
4840}
4841
4842static void intel_ddi_tc_encoder_shutdown_complete(struct intel_encoder *encoder)
4843{
4844 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4845 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4846
4847 intel_tc_port_cleanup(dig_port);
4848}
4849
4850#define port_tc_name(port) ((port) - PORT_TC1 + '1')
4851#define tc_port_name(tc_port) ((tc_port) - TC_PORT_1 + '1')
4852
4853static bool port_strap_detected(struct drm_i915_private *i915, enum port port)
4854{
4855 /* straps not used on skl+ */
4856 if (DISPLAY_VER(i915) >= 9)
4857 return true;
4858
4859 switch (port) {
4860 case PORT_A:
4861 return intel_de_read(i915, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
4862 case PORT_B:
4863 return intel_de_read(i915, SFUSE_STRAP) & SFUSE_STRAP_DDIB_DETECTED;
4864 case PORT_C:
4865 return intel_de_read(i915, SFUSE_STRAP) & SFUSE_STRAP_DDIC_DETECTED;
4866 case PORT_D:
4867 return intel_de_read(i915, SFUSE_STRAP) & SFUSE_STRAP_DDID_DETECTED;
4868 case PORT_E:
4869 return true; /* no strap for DDI-E */
4870 default:
4871 MISSING_CASE(port);
4872 return false;
4873 }
4874}
4875
4876static bool need_aux_ch(struct intel_encoder *encoder, bool init_dp)
4877{
4878 return init_dp || intel_encoder_is_tc(encoder);
4879}
4880
4881static bool assert_has_icl_dsi(struct drm_i915_private *i915)
4882{
4883 return !drm_WARN(&i915->drm, !IS_ALDERLAKE_P(i915) &&
4884 !IS_TIGERLAKE(i915) && DISPLAY_VER(i915) != 11,
4885 "Platform does not support DSI\n");
4886}
4887
4888static bool port_in_use(struct drm_i915_private *i915, enum port port)
4889{
4890 struct intel_encoder *encoder;
4891
4892 for_each_intel_encoder(&i915->drm, encoder) {
4893 /* FIXME what about second port for dual link DSI? */
4894 if (encoder->port == port)
4895 return true;
4896 }
4897
4898 return false;
4899}
4900
4901void intel_ddi_init(struct intel_display *display,
4902 const struct intel_bios_encoder_data *devdata)
4903{
4904 struct drm_i915_private *dev_priv = to_i915(display->drm);
4905 struct intel_digital_port *dig_port;
4906 struct intel_encoder *encoder;
4907 bool init_hdmi, init_dp;
4908 enum port port;
4909 enum phy phy;
4910
4911 port = intel_bios_encoder_port(devdata);
4912 if (port == PORT_NONE)
4913 return;
4914
4915 if (!port_strap_detected(dev_priv, port)) {
4916 drm_dbg_kms(&dev_priv->drm,
4917 "Port %c strap not detected\n", port_name(port));
4918 return;
4919 }
4920
4921 if (!assert_port_valid(dev_priv, port))
4922 return;
4923
4924 if (port_in_use(dev_priv, port)) {
4925 drm_dbg_kms(&dev_priv->drm,
4926 "Port %c already claimed\n", port_name(port));
4927 return;
4928 }
4929
4930 if (intel_bios_encoder_supports_dsi(devdata)) {
4931 /* BXT/GLK handled elsewhere, for now at least */
4932 if (!assert_has_icl_dsi(dev_priv))
4933 return;
4934
4935 icl_dsi_init(display, devdata);
4936 return;
4937 }
4938
4939 phy = intel_port_to_phy(dev_priv, port);
4940
4941 /*
4942 * On platforms with HTI (aka HDPORT), if it's enabled at boot it may
4943 * have taken over some of the PHYs and made them unavailable to the
4944 * driver. In that case we should skip initializing the corresponding
4945 * outputs.
4946 */
4947 if (intel_hti_uses_phy(display, phy)) {
4948 drm_dbg_kms(&dev_priv->drm, "PORT %c / PHY %c reserved by HTI\n",
4949 port_name(port), phy_name(phy));
4950 return;
4951 }
4952
4953 init_hdmi = intel_bios_encoder_supports_dvi(devdata) ||
4954 intel_bios_encoder_supports_hdmi(devdata);
4955 init_dp = intel_bios_encoder_supports_dp(devdata);
4956
4957 if (intel_bios_encoder_is_lspcon(devdata)) {
4958 /*
4959 * Lspcon device needs to be driven with DP connector
4960 * with special detection sequence. So make sure DP
4961 * is initialized before lspcon.
4962 */
4963 init_dp = true;
4964 init_hdmi = false;
4965 drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
4966 port_name(port));
4967 }
4968
4969 if (!init_dp && !init_hdmi) {
4970 drm_dbg_kms(&dev_priv->drm,
4971 "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4972 port_name(port));
4973 return;
4974 }
4975
4976 if (intel_phy_is_snps(dev_priv, phy) &&
4977 dev_priv->display.snps.phy_failed_calibration & BIT(phy)) {
4978 drm_dbg_kms(&dev_priv->drm,
4979 "SNPS PHY %c failed to calibrate, proceeding anyway\n",
4980 phy_name(phy));
4981 }
4982
4983 dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
4984 if (!dig_port)
4985 return;
4986
4987 dig_port->aux_ch = AUX_CH_NONE;
4988
4989 encoder = &dig_port->base;
4990 encoder->devdata = devdata;
4991
4992 if (DISPLAY_VER(dev_priv) >= 13 && port >= PORT_D_XELPD) {
4993 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4994 DRM_MODE_ENCODER_TMDS,
4995 "DDI %c/PHY %c",
4996 port_name(port - PORT_D_XELPD + PORT_D),
4997 phy_name(phy));
4998 } else if (DISPLAY_VER(dev_priv) >= 12) {
4999 enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
5000
5001 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
5002 DRM_MODE_ENCODER_TMDS,
5003 "DDI %s%c/PHY %s%c",
5004 port >= PORT_TC1 ? "TC" : "",
5005 port >= PORT_TC1 ? port_tc_name(port) : port_name(port),
5006 tc_port != TC_PORT_NONE ? "TC" : "",
5007 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
5008 } else if (DISPLAY_VER(dev_priv) >= 11) {
5009 enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
5010
5011 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
5012 DRM_MODE_ENCODER_TMDS,
5013 "DDI %c%s/PHY %s%c",
5014 port_name(port),
5015 port >= PORT_C ? " (TC)" : "",
5016 tc_port != TC_PORT_NONE ? "TC" : "",
5017 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
5018 } else {
5019 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
5020 DRM_MODE_ENCODER_TMDS,
5021 "DDI %c/PHY %c", port_name(port), phy_name(phy));
5022 }
5023
5024 intel_encoder_link_check_init(encoder, intel_ddi_link_check);
5025
5026 mutex_init(&dig_port->hdcp_mutex);
5027 dig_port->num_hdcp_streams = 0;
5028
5029 encoder->hotplug = intel_ddi_hotplug;
5030 encoder->compute_output_type = intel_ddi_compute_output_type;
5031 encoder->compute_config = intel_ddi_compute_config;
5032 encoder->compute_config_late = intel_ddi_compute_config_late;
5033 encoder->enable = intel_enable_ddi;
5034 encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
5035 encoder->pre_enable = intel_ddi_pre_enable;
5036 encoder->disable = intel_disable_ddi;
5037 encoder->post_pll_disable = intel_ddi_post_pll_disable;
5038 encoder->post_disable = intel_ddi_post_disable;
5039 encoder->update_pipe = intel_ddi_update_pipe;
5040 encoder->audio_enable = intel_audio_codec_enable;
5041 encoder->audio_disable = intel_audio_codec_disable;
5042 encoder->get_hw_state = intel_ddi_get_hw_state;
5043 encoder->sync_state = intel_ddi_sync_state;
5044 encoder->initial_fastset_check = intel_ddi_initial_fastset_check;
5045 encoder->suspend = intel_ddi_encoder_suspend;
5046 encoder->shutdown = intel_ddi_encoder_shutdown;
5047 encoder->get_power_domains = intel_ddi_get_power_domains;
5048
5049 encoder->type = INTEL_OUTPUT_DDI;
5050 encoder->power_domain = intel_display_power_ddi_lanes_domain(dev_priv, port);
5051 encoder->port = port;
5052 encoder->cloneable = 0;
5053 encoder->pipe_mask = ~0;
5054
5055 if (DISPLAY_VER(dev_priv) >= 14) {
5056 encoder->enable_clock = intel_mtl_pll_enable;
5057 encoder->disable_clock = intel_mtl_pll_disable;
5058 encoder->port_pll_type = intel_mtl_port_pll_type;
5059 encoder->get_config = mtl_ddi_get_config;
5060 } else if (IS_DG2(dev_priv)) {
5061 encoder->enable_clock = intel_mpllb_enable;
5062 encoder->disable_clock = intel_mpllb_disable;
5063 encoder->get_config = dg2_ddi_get_config;
5064 } else if (IS_ALDERLAKE_S(dev_priv)) {
5065 encoder->enable_clock = adls_ddi_enable_clock;
5066 encoder->disable_clock = adls_ddi_disable_clock;
5067 encoder->is_clock_enabled = adls_ddi_is_clock_enabled;
5068 encoder->get_config = adls_ddi_get_config;
5069 } else if (IS_ROCKETLAKE(dev_priv)) {
5070 encoder->enable_clock = rkl_ddi_enable_clock;
5071 encoder->disable_clock = rkl_ddi_disable_clock;
5072 encoder->is_clock_enabled = rkl_ddi_is_clock_enabled;
5073 encoder->get_config = rkl_ddi_get_config;
5074 } else if (IS_DG1(dev_priv)) {
5075 encoder->enable_clock = dg1_ddi_enable_clock;
5076 encoder->disable_clock = dg1_ddi_disable_clock;
5077 encoder->is_clock_enabled = dg1_ddi_is_clock_enabled;
5078 encoder->get_config = dg1_ddi_get_config;
5079 } else if (IS_JASPERLAKE(dev_priv) || IS_ELKHARTLAKE(dev_priv)) {
5080 if (intel_ddi_is_tc(dev_priv, port)) {
5081 encoder->enable_clock = jsl_ddi_tc_enable_clock;
5082 encoder->disable_clock = jsl_ddi_tc_disable_clock;
5083 encoder->is_clock_enabled = jsl_ddi_tc_is_clock_enabled;
5084 encoder->port_pll_type = icl_ddi_tc_port_pll_type;
5085 encoder->get_config = icl_ddi_combo_get_config;
5086 } else {
5087 encoder->enable_clock = icl_ddi_combo_enable_clock;
5088 encoder->disable_clock = icl_ddi_combo_disable_clock;
5089 encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
5090 encoder->get_config = icl_ddi_combo_get_config;
5091 }
5092 } else if (DISPLAY_VER(dev_priv) >= 11) {
5093 if (intel_ddi_is_tc(dev_priv, port)) {
5094 encoder->enable_clock = icl_ddi_tc_enable_clock;
5095 encoder->disable_clock = icl_ddi_tc_disable_clock;
5096 encoder->is_clock_enabled = icl_ddi_tc_is_clock_enabled;
5097 encoder->port_pll_type = icl_ddi_tc_port_pll_type;
5098 encoder->get_config = icl_ddi_tc_get_config;
5099 } else {
5100 encoder->enable_clock = icl_ddi_combo_enable_clock;
5101 encoder->disable_clock = icl_ddi_combo_disable_clock;
5102 encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
5103 encoder->get_config = icl_ddi_combo_get_config;
5104 }
5105 } else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
5106 /* BXT/GLK have fixed PLL->port mapping */
5107 encoder->get_config = bxt_ddi_get_config;
5108 } else if (DISPLAY_VER(dev_priv) == 9) {
5109 encoder->enable_clock = skl_ddi_enable_clock;
5110 encoder->disable_clock = skl_ddi_disable_clock;
5111 encoder->is_clock_enabled = skl_ddi_is_clock_enabled;
5112 encoder->get_config = skl_ddi_get_config;
5113 } else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
5114 encoder->enable_clock = hsw_ddi_enable_clock;
5115 encoder->disable_clock = hsw_ddi_disable_clock;
5116 encoder->is_clock_enabled = hsw_ddi_is_clock_enabled;
5117 encoder->get_config = hsw_ddi_get_config;
5118 }
5119
5120 if (DISPLAY_VER(dev_priv) >= 14) {
5121 encoder->set_signal_levels = intel_cx0_phy_set_signal_levels;
5122 } else if (IS_DG2(dev_priv)) {
5123 encoder->set_signal_levels = intel_snps_phy_set_signal_levels;
5124 } else if (DISPLAY_VER(dev_priv) >= 12) {
5125 if (intel_encoder_is_combo(encoder))
5126 encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
5127 else
5128 encoder->set_signal_levels = tgl_dkl_phy_set_signal_levels;
5129 } else if (DISPLAY_VER(dev_priv) >= 11) {
5130 if (intel_encoder_is_combo(encoder))
5131 encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
5132 else
5133 encoder->set_signal_levels = icl_mg_phy_set_signal_levels;
5134 } else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
5135 encoder->set_signal_levels = bxt_dpio_phy_set_signal_levels;
5136 } else {
5137 encoder->set_signal_levels = hsw_set_signal_levels;
5138 }
5139
5140 intel_ddi_buf_trans_init(encoder);
5141
5142 if (DISPLAY_VER(dev_priv) >= 13)
5143 encoder->hpd_pin = xelpd_hpd_pin(dev_priv, port);
5144 else if (IS_DG1(dev_priv))
5145 encoder->hpd_pin = dg1_hpd_pin(dev_priv, port);
5146 else if (IS_ROCKETLAKE(dev_priv))
5147 encoder->hpd_pin = rkl_hpd_pin(dev_priv, port);
5148 else if (DISPLAY_VER(dev_priv) >= 12)
5149 encoder->hpd_pin = tgl_hpd_pin(dev_priv, port);
5150 else if (IS_JASPERLAKE(dev_priv) || IS_ELKHARTLAKE(dev_priv))
5151 encoder->hpd_pin = ehl_hpd_pin(dev_priv, port);
5152 else if (DISPLAY_VER(dev_priv) == 11)
5153 encoder->hpd_pin = icl_hpd_pin(dev_priv, port);
5154 else if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
5155 encoder->hpd_pin = skl_hpd_pin(dev_priv, port);
5156 else
5157 encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
5158
5159 if (DISPLAY_VER(dev_priv) >= 11)
5160 dig_port->saved_port_bits =
5161 intel_de_read(dev_priv, DDI_BUF_CTL(port))
5162 & DDI_BUF_PORT_REVERSAL;
5163 else
5164 dig_port->saved_port_bits =
5165 intel_de_read(dev_priv, DDI_BUF_CTL(port))
5166 & (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
5167
5168 if (intel_bios_encoder_lane_reversal(devdata))
5169 dig_port->saved_port_bits |= DDI_BUF_PORT_REVERSAL;
5170
5171 dig_port->dp.output_reg = INVALID_MMIO_REG;
5172 dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
5173
5174 if (need_aux_ch(encoder, init_dp)) {
5175 dig_port->aux_ch = intel_dp_aux_ch(encoder);
5176 if (dig_port->aux_ch == AUX_CH_NONE)
5177 goto err;
5178 }
5179
5180 if (intel_encoder_is_tc(encoder)) {
5181 bool is_legacy =
5182 !intel_bios_encoder_supports_typec_usb(devdata) &&
5183 !intel_bios_encoder_supports_tbt(devdata);
5184
5185 if (!is_legacy && init_hdmi) {
5186 is_legacy = !init_dp;
5187
5188 drm_dbg_kms(&dev_priv->drm,
5189 "VBT says port %c is non-legacy TC and has HDMI (with DP: %s), assume it's %s\n",
5190 port_name(port),
5191 str_yes_no(init_dp),
5192 is_legacy ? "legacy" : "non-legacy");
5193 }
5194
5195 encoder->suspend_complete = intel_ddi_tc_encoder_suspend_complete;
5196 encoder->shutdown_complete = intel_ddi_tc_encoder_shutdown_complete;
5197
5198 dig_port->lock = intel_tc_port_lock;
5199 dig_port->unlock = intel_tc_port_unlock;
5200
5201 if (intel_tc_port_init(dig_port, is_legacy) < 0)
5202 goto err;
5203 }
5204
5205 drm_WARN_ON(&dev_priv->drm, port > PORT_I);
5206 dig_port->ddi_io_power_domain = intel_display_power_ddi_io_domain(dev_priv, port);
5207
5208 if (DISPLAY_VER(dev_priv) >= 11) {
5209 if (intel_encoder_is_tc(encoder))
5210 dig_port->connected = intel_tc_port_connected;
5211 else
5212 dig_port->connected = lpt_digital_port_connected;
5213 } else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
5214 dig_port->connected = bdw_digital_port_connected;
5215 } else if (DISPLAY_VER(dev_priv) == 9) {
5216 dig_port->connected = lpt_digital_port_connected;
5217 } else if (IS_BROADWELL(dev_priv)) {
5218 if (port == PORT_A)
5219 dig_port->connected = bdw_digital_port_connected;
5220 else
5221 dig_port->connected = lpt_digital_port_connected;
5222 } else if (IS_HASWELL(dev_priv)) {
5223 if (port == PORT_A)
5224 dig_port->connected = hsw_digital_port_connected;
5225 else
5226 dig_port->connected = lpt_digital_port_connected;
5227 }
5228
5229 intel_infoframe_init(dig_port);
5230
5231 if (init_dp) {
5232 if (!intel_ddi_init_dp_connector(dig_port))
5233 goto err;
5234
5235 dig_port->hpd_pulse = intel_dp_hpd_pulse;
5236
5237 if (dig_port->dp.mso_link_count)
5238 encoder->pipe_mask = intel_ddi_splitter_pipe_mask(dev_priv);
5239 }
5240
5241 /*
5242 * In theory we don't need the encoder->type check,
5243 * but leave it just in case we have some really bad VBTs...
5244 */
5245 if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
5246 if (!intel_ddi_init_hdmi_connector(dig_port))
5247 goto err;
5248 }
5249
5250 return;
5251
5252err:
5253 drm_encoder_cleanup(&encoder->base);
5254 kfree(dig_port);
5255}
1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
28#include <linux/string_helpers.h>
29
30#include <drm/display/drm_scdc_helper.h>
31#include <drm/drm_privacy_screen_consumer.h>
32
33#include "i915_drv.h"
34#include "i915_reg.h"
35#include "intel_audio.h"
36#include "intel_audio_regs.h"
37#include "intel_backlight.h"
38#include "intel_combo_phy.h"
39#include "intel_combo_phy_regs.h"
40#include "intel_connector.h"
41#include "intel_crtc.h"
42#include "intel_ddi.h"
43#include "intel_ddi_buf_trans.h"
44#include "intel_de.h"
45#include "intel_display_power.h"
46#include "intel_display_types.h"
47#include "intel_dkl_phy.h"
48#include "intel_dkl_phy_regs.h"
49#include "intel_dp.h"
50#include "intel_dp_link_training.h"
51#include "intel_dp_mst.h"
52#include "intel_dpio_phy.h"
53#include "intel_dsi.h"
54#include "intel_fdi.h"
55#include "intel_fifo_underrun.h"
56#include "intel_gmbus.h"
57#include "intel_hdcp.h"
58#include "intel_hdmi.h"
59#include "intel_hotplug.h"
60#include "intel_hti.h"
61#include "intel_lspcon.h"
62#include "intel_mg_phy_regs.h"
63#include "intel_pps.h"
64#include "intel_psr.h"
65#include "intel_quirks.h"
66#include "intel_snps_phy.h"
67#include "intel_sprite.h"
68#include "intel_tc.h"
69#include "intel_vdsc.h"
70#include "intel_vrr.h"
71#include "skl_scaler.h"
72#include "skl_universal_plane.h"
73
74static const u8 index_to_dp_signal_levels[] = {
75 [0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
76 [1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
77 [2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
78 [3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
79 [4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
80 [5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
81 [6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
82 [7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
83 [8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
84 [9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
85};
86
87static int intel_ddi_hdmi_level(struct intel_encoder *encoder,
88 const struct intel_ddi_buf_trans *trans)
89{
90 int level;
91
92 level = intel_bios_hdmi_level_shift(encoder);
93 if (level < 0)
94 level = trans->hdmi_default_entry;
95
96 return level;
97}
98
99static bool has_buf_trans_select(struct drm_i915_private *i915)
100{
101 return DISPLAY_VER(i915) < 10 && !IS_BROXTON(i915);
102}
103
104static bool has_iboost(struct drm_i915_private *i915)
105{
106 return DISPLAY_VER(i915) == 9 && !IS_BROXTON(i915);
107}
108
109/*
110 * Starting with Haswell, DDI port buffers must be programmed with correct
111 * values in advance. This function programs the correct values for
112 * DP/eDP/FDI use cases.
113 */
114void hsw_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
115 const struct intel_crtc_state *crtc_state)
116{
117 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
118 u32 iboost_bit = 0;
119 int i, n_entries;
120 enum port port = encoder->port;
121 const struct intel_ddi_buf_trans *trans;
122
123 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
124 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
125 return;
126
127 /* If we're boosting the current, set bit 31 of trans1 */
128 if (has_iboost(dev_priv) &&
129 intel_bios_encoder_dp_boost_level(encoder->devdata))
130 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
131
132 for (i = 0; i < n_entries; i++) {
133 intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, i),
134 trans->entries[i].hsw.trans1 | iboost_bit);
135 intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, i),
136 trans->entries[i].hsw.trans2);
137 }
138}
139
140/*
141 * Starting with Haswell, DDI port buffers must be programmed with correct
142 * values in advance. This function programs the correct values for
143 * HDMI/DVI use cases.
144 */
145static void hsw_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
146 const struct intel_crtc_state *crtc_state)
147{
148 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
149 int level = intel_ddi_level(encoder, crtc_state, 0);
150 u32 iboost_bit = 0;
151 int n_entries;
152 enum port port = encoder->port;
153 const struct intel_ddi_buf_trans *trans;
154
155 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
156 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
157 return;
158
159 /* If we're boosting the current, set bit 31 of trans1 */
160 if (has_iboost(dev_priv) &&
161 intel_bios_encoder_hdmi_boost_level(encoder->devdata))
162 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
163
164 /* Entry 9 is for HDMI: */
165 intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, 9),
166 trans->entries[level].hsw.trans1 | iboost_bit);
167 intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, 9),
168 trans->entries[level].hsw.trans2);
169}
170
171void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
172 enum port port)
173{
174 if (IS_BROXTON(dev_priv)) {
175 udelay(16);
176 return;
177 }
178
179 if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
180 DDI_BUF_IS_IDLE), 8))
181 drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get idle\n",
182 port_name(port));
183}
184
185static void intel_wait_ddi_buf_active(struct drm_i915_private *dev_priv,
186 enum port port)
187{
188 int ret;
189
190 /* Wait > 518 usecs for DDI_BUF_CTL to be non idle */
191 if (DISPLAY_VER(dev_priv) < 10) {
192 usleep_range(518, 1000);
193 return;
194 }
195
196 ret = _wait_for(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
197 DDI_BUF_IS_IDLE), IS_DG2(dev_priv) ? 1200 : 500, 10, 10);
198
199 if (ret)
200 drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get active\n",
201 port_name(port));
202}
203
204static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
205{
206 switch (pll->info->id) {
207 case DPLL_ID_WRPLL1:
208 return PORT_CLK_SEL_WRPLL1;
209 case DPLL_ID_WRPLL2:
210 return PORT_CLK_SEL_WRPLL2;
211 case DPLL_ID_SPLL:
212 return PORT_CLK_SEL_SPLL;
213 case DPLL_ID_LCPLL_810:
214 return PORT_CLK_SEL_LCPLL_810;
215 case DPLL_ID_LCPLL_1350:
216 return PORT_CLK_SEL_LCPLL_1350;
217 case DPLL_ID_LCPLL_2700:
218 return PORT_CLK_SEL_LCPLL_2700;
219 default:
220 MISSING_CASE(pll->info->id);
221 return PORT_CLK_SEL_NONE;
222 }
223}
224
225static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
226 const struct intel_crtc_state *crtc_state)
227{
228 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
229 int clock = crtc_state->port_clock;
230 const enum intel_dpll_id id = pll->info->id;
231
232 switch (id) {
233 default:
234 /*
235 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
236 * here, so do warn if this get passed in
237 */
238 MISSING_CASE(id);
239 return DDI_CLK_SEL_NONE;
240 case DPLL_ID_ICL_TBTPLL:
241 switch (clock) {
242 case 162000:
243 return DDI_CLK_SEL_TBT_162;
244 case 270000:
245 return DDI_CLK_SEL_TBT_270;
246 case 540000:
247 return DDI_CLK_SEL_TBT_540;
248 case 810000:
249 return DDI_CLK_SEL_TBT_810;
250 default:
251 MISSING_CASE(clock);
252 return DDI_CLK_SEL_NONE;
253 }
254 case DPLL_ID_ICL_MGPLL1:
255 case DPLL_ID_ICL_MGPLL2:
256 case DPLL_ID_ICL_MGPLL3:
257 case DPLL_ID_ICL_MGPLL4:
258 case DPLL_ID_TGL_MGPLL5:
259 case DPLL_ID_TGL_MGPLL6:
260 return DDI_CLK_SEL_MG;
261 }
262}
263
264static u32 ddi_buf_phy_link_rate(int port_clock)
265{
266 switch (port_clock) {
267 case 162000:
268 return DDI_BUF_PHY_LINK_RATE(0);
269 case 216000:
270 return DDI_BUF_PHY_LINK_RATE(4);
271 case 243000:
272 return DDI_BUF_PHY_LINK_RATE(5);
273 case 270000:
274 return DDI_BUF_PHY_LINK_RATE(1);
275 case 324000:
276 return DDI_BUF_PHY_LINK_RATE(6);
277 case 432000:
278 return DDI_BUF_PHY_LINK_RATE(7);
279 case 540000:
280 return DDI_BUF_PHY_LINK_RATE(2);
281 case 810000:
282 return DDI_BUF_PHY_LINK_RATE(3);
283 default:
284 MISSING_CASE(port_clock);
285 return DDI_BUF_PHY_LINK_RATE(0);
286 }
287}
288
289static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder,
290 const struct intel_crtc_state *crtc_state)
291{
292 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
293 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
294 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
295 enum phy phy = intel_port_to_phy(i915, encoder->port);
296
297 /* DDI_BUF_CTL_ENABLE will be set by intel_ddi_prepare_link_retrain() later */
298 intel_dp->DP = dig_port->saved_port_bits |
299 DDI_PORT_WIDTH(crtc_state->lane_count) |
300 DDI_BUF_TRANS_SELECT(0);
301
302 if (IS_ALDERLAKE_P(i915) && intel_phy_is_tc(i915, phy)) {
303 intel_dp->DP |= ddi_buf_phy_link_rate(crtc_state->port_clock);
304 if (!intel_tc_port_in_tbt_alt_mode(dig_port))
305 intel_dp->DP |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
306 }
307}
308
309static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
310 enum port port)
311{
312 u32 val = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
313
314 switch (val) {
315 case DDI_CLK_SEL_NONE:
316 return 0;
317 case DDI_CLK_SEL_TBT_162:
318 return 162000;
319 case DDI_CLK_SEL_TBT_270:
320 return 270000;
321 case DDI_CLK_SEL_TBT_540:
322 return 540000;
323 case DDI_CLK_SEL_TBT_810:
324 return 810000;
325 default:
326 MISSING_CASE(val);
327 return 0;
328 }
329}
330
331static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
332{
333 /* CRT dotclock is determined via other means */
334 if (pipe_config->has_pch_encoder)
335 return;
336
337 pipe_config->hw.adjusted_mode.crtc_clock =
338 intel_crtc_dotclock(pipe_config);
339}
340
341void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
342 const struct drm_connector_state *conn_state)
343{
344 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
345 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
346 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
347 u32 temp;
348
349 if (!intel_crtc_has_dp_encoder(crtc_state))
350 return;
351
352 drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
353
354 temp = DP_MSA_MISC_SYNC_CLOCK;
355
356 switch (crtc_state->pipe_bpp) {
357 case 18:
358 temp |= DP_MSA_MISC_6_BPC;
359 break;
360 case 24:
361 temp |= DP_MSA_MISC_8_BPC;
362 break;
363 case 30:
364 temp |= DP_MSA_MISC_10_BPC;
365 break;
366 case 36:
367 temp |= DP_MSA_MISC_12_BPC;
368 break;
369 default:
370 MISSING_CASE(crtc_state->pipe_bpp);
371 break;
372 }
373
374 /* nonsense combination */
375 drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
376 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
377
378 if (crtc_state->limited_color_range)
379 temp |= DP_MSA_MISC_COLOR_CEA_RGB;
380
381 /*
382 * As per DP 1.2 spec section 2.3.4.3 while sending
383 * YCBCR 444 signals we should program MSA MISC1/0 fields with
384 * colorspace information.
385 */
386 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
387 temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
388
389 /*
390 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
391 * of Color Encoding Format and Content Color Gamut] while sending
392 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
393 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
394 */
395 if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
396 temp |= DP_MSA_MISC_COLOR_VSC_SDP;
397
398 intel_de_write(dev_priv, TRANS_MSA_MISC(cpu_transcoder), temp);
399}
400
401static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
402{
403 if (master_transcoder == TRANSCODER_EDP)
404 return 0;
405 else
406 return master_transcoder + 1;
407}
408
409static void
410intel_ddi_config_transcoder_dp2(struct intel_encoder *encoder,
411 const struct intel_crtc_state *crtc_state)
412{
413 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
414 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
415 u32 val = 0;
416
417 if (intel_dp_is_uhbr(crtc_state))
418 val = TRANS_DP2_128B132B_CHANNEL_CODING;
419
420 intel_de_write(i915, TRANS_DP2_CTL(cpu_transcoder), val);
421}
422
423/*
424 * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
425 *
426 * Only intended to be used by intel_ddi_enable_transcoder_func() and
427 * intel_ddi_config_transcoder_func().
428 */
429static u32
430intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
431 const struct intel_crtc_state *crtc_state)
432{
433 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
434 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
435 enum pipe pipe = crtc->pipe;
436 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
437 enum port port = encoder->port;
438 u32 temp;
439
440 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
441 temp = TRANS_DDI_FUNC_ENABLE;
442 if (DISPLAY_VER(dev_priv) >= 12)
443 temp |= TGL_TRANS_DDI_SELECT_PORT(port);
444 else
445 temp |= TRANS_DDI_SELECT_PORT(port);
446
447 switch (crtc_state->pipe_bpp) {
448 default:
449 MISSING_CASE(crtc_state->pipe_bpp);
450 fallthrough;
451 case 18:
452 temp |= TRANS_DDI_BPC_6;
453 break;
454 case 24:
455 temp |= TRANS_DDI_BPC_8;
456 break;
457 case 30:
458 temp |= TRANS_DDI_BPC_10;
459 break;
460 case 36:
461 temp |= TRANS_DDI_BPC_12;
462 break;
463 }
464
465 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
466 temp |= TRANS_DDI_PVSYNC;
467 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
468 temp |= TRANS_DDI_PHSYNC;
469
470 if (cpu_transcoder == TRANSCODER_EDP) {
471 switch (pipe) {
472 default:
473 MISSING_CASE(pipe);
474 fallthrough;
475 case PIPE_A:
476 /* On Haswell, can only use the always-on power well for
477 * eDP when not using the panel fitter, and when not
478 * using motion blur mitigation (which we don't
479 * support). */
480 if (crtc_state->pch_pfit.force_thru)
481 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
482 else
483 temp |= TRANS_DDI_EDP_INPUT_A_ON;
484 break;
485 case PIPE_B:
486 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
487 break;
488 case PIPE_C:
489 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
490 break;
491 }
492 }
493
494 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
495 if (crtc_state->has_hdmi_sink)
496 temp |= TRANS_DDI_MODE_SELECT_HDMI;
497 else
498 temp |= TRANS_DDI_MODE_SELECT_DVI;
499
500 if (crtc_state->hdmi_scrambling)
501 temp |= TRANS_DDI_HDMI_SCRAMBLING;
502 if (crtc_state->hdmi_high_tmds_clock_ratio)
503 temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
504 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
505 temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
506 temp |= (crtc_state->fdi_lanes - 1) << 1;
507 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
508 if (intel_dp_is_uhbr(crtc_state))
509 temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
510 else
511 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
512 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
513
514 if (DISPLAY_VER(dev_priv) >= 12) {
515 enum transcoder master;
516
517 master = crtc_state->mst_master_transcoder;
518 drm_WARN_ON(&dev_priv->drm,
519 master == INVALID_TRANSCODER);
520 temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
521 }
522 } else {
523 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
524 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
525 }
526
527 if (IS_DISPLAY_VER(dev_priv, 8, 10) &&
528 crtc_state->master_transcoder != INVALID_TRANSCODER) {
529 u8 master_select =
530 bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
531
532 temp |= TRANS_DDI_PORT_SYNC_ENABLE |
533 TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
534 }
535
536 return temp;
537}
538
539void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
540 const struct intel_crtc_state *crtc_state)
541{
542 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
543 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
544 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
545
546 if (DISPLAY_VER(dev_priv) >= 11) {
547 enum transcoder master_transcoder = crtc_state->master_transcoder;
548 u32 ctl2 = 0;
549
550 if (master_transcoder != INVALID_TRANSCODER) {
551 u8 master_select =
552 bdw_trans_port_sync_master_select(master_transcoder);
553
554 ctl2 |= PORT_SYNC_MODE_ENABLE |
555 PORT_SYNC_MODE_MASTER_SELECT(master_select);
556 }
557
558 intel_de_write(dev_priv,
559 TRANS_DDI_FUNC_CTL2(cpu_transcoder), ctl2);
560 }
561
562 intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder),
563 intel_ddi_transcoder_func_reg_val_get(encoder,
564 crtc_state));
565}
566
567/*
568 * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
569 * bit.
570 */
571static void
572intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
573 const struct intel_crtc_state *crtc_state)
574{
575 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
576 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
577 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
578 u32 ctl;
579
580 ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
581 ctl &= ~TRANS_DDI_FUNC_ENABLE;
582 intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
583}
584
585void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
586{
587 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
588 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
589 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
590 u32 ctl;
591
592 if (DISPLAY_VER(dev_priv) >= 11)
593 intel_de_write(dev_priv,
594 TRANS_DDI_FUNC_CTL2(cpu_transcoder), 0);
595
596 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
597
598 drm_WARN_ON(crtc->base.dev, ctl & TRANS_DDI_HDCP_SIGNALLING);
599
600 ctl &= ~TRANS_DDI_FUNC_ENABLE;
601
602 if (IS_DISPLAY_VER(dev_priv, 8, 10))
603 ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
604 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
605
606 if (DISPLAY_VER(dev_priv) >= 12) {
607 if (!intel_dp_mst_is_master_trans(crtc_state)) {
608 ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
609 TRANS_DDI_MODE_SELECT_MASK);
610 }
611 } else {
612 ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
613 }
614
615 intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
616
617 if (intel_has_quirk(dev_priv, QUIRK_INCREASE_DDI_DISABLED_TIME) &&
618 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
619 drm_dbg_kms(&dev_priv->drm,
620 "Quirk Increase DDI disabled time\n");
621 /* Quirk time at 100ms for reliable operation */
622 msleep(100);
623 }
624}
625
626int intel_ddi_toggle_hdcp_bits(struct intel_encoder *intel_encoder,
627 enum transcoder cpu_transcoder,
628 bool enable, u32 hdcp_mask)
629{
630 struct drm_device *dev = intel_encoder->base.dev;
631 struct drm_i915_private *dev_priv = to_i915(dev);
632 intel_wakeref_t wakeref;
633 int ret = 0;
634 u32 tmp;
635
636 wakeref = intel_display_power_get_if_enabled(dev_priv,
637 intel_encoder->power_domain);
638 if (drm_WARN_ON(dev, !wakeref))
639 return -ENXIO;
640
641 tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
642 if (enable)
643 tmp |= hdcp_mask;
644 else
645 tmp &= ~hdcp_mask;
646 intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), tmp);
647 intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
648 return ret;
649}
650
651bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
652{
653 struct drm_device *dev = intel_connector->base.dev;
654 struct drm_i915_private *dev_priv = to_i915(dev);
655 struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
656 int type = intel_connector->base.connector_type;
657 enum port port = encoder->port;
658 enum transcoder cpu_transcoder;
659 intel_wakeref_t wakeref;
660 enum pipe pipe = 0;
661 u32 tmp;
662 bool ret;
663
664 wakeref = intel_display_power_get_if_enabled(dev_priv,
665 encoder->power_domain);
666 if (!wakeref)
667 return false;
668
669 if (!encoder->get_hw_state(encoder, &pipe)) {
670 ret = false;
671 goto out;
672 }
673
674 if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
675 cpu_transcoder = TRANSCODER_EDP;
676 else
677 cpu_transcoder = (enum transcoder) pipe;
678
679 tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
680
681 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
682 case TRANS_DDI_MODE_SELECT_HDMI:
683 case TRANS_DDI_MODE_SELECT_DVI:
684 ret = type == DRM_MODE_CONNECTOR_HDMIA;
685 break;
686
687 case TRANS_DDI_MODE_SELECT_DP_SST:
688 ret = type == DRM_MODE_CONNECTOR_eDP ||
689 type == DRM_MODE_CONNECTOR_DisplayPort;
690 break;
691
692 case TRANS_DDI_MODE_SELECT_DP_MST:
693 /* if the transcoder is in MST state then
694 * connector isn't connected */
695 ret = false;
696 break;
697
698 case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
699 if (HAS_DP20(dev_priv))
700 /* 128b/132b */
701 ret = false;
702 else
703 /* FDI */
704 ret = type == DRM_MODE_CONNECTOR_VGA;
705 break;
706
707 default:
708 ret = false;
709 break;
710 }
711
712out:
713 intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
714
715 return ret;
716}
717
718static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
719 u8 *pipe_mask, bool *is_dp_mst)
720{
721 struct drm_device *dev = encoder->base.dev;
722 struct drm_i915_private *dev_priv = to_i915(dev);
723 enum port port = encoder->port;
724 intel_wakeref_t wakeref;
725 enum pipe p;
726 u32 tmp;
727 u8 mst_pipe_mask;
728
729 *pipe_mask = 0;
730 *is_dp_mst = false;
731
732 wakeref = intel_display_power_get_if_enabled(dev_priv,
733 encoder->power_domain);
734 if (!wakeref)
735 return;
736
737 tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
738 if (!(tmp & DDI_BUF_CTL_ENABLE))
739 goto out;
740
741 if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
742 tmp = intel_de_read(dev_priv,
743 TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
744
745 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
746 default:
747 MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
748 fallthrough;
749 case TRANS_DDI_EDP_INPUT_A_ON:
750 case TRANS_DDI_EDP_INPUT_A_ONOFF:
751 *pipe_mask = BIT(PIPE_A);
752 break;
753 case TRANS_DDI_EDP_INPUT_B_ONOFF:
754 *pipe_mask = BIT(PIPE_B);
755 break;
756 case TRANS_DDI_EDP_INPUT_C_ONOFF:
757 *pipe_mask = BIT(PIPE_C);
758 break;
759 }
760
761 goto out;
762 }
763
764 mst_pipe_mask = 0;
765 for_each_pipe(dev_priv, p) {
766 enum transcoder cpu_transcoder = (enum transcoder)p;
767 unsigned int port_mask, ddi_select;
768 intel_wakeref_t trans_wakeref;
769
770 trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
771 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
772 if (!trans_wakeref)
773 continue;
774
775 if (DISPLAY_VER(dev_priv) >= 12) {
776 port_mask = TGL_TRANS_DDI_PORT_MASK;
777 ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
778 } else {
779 port_mask = TRANS_DDI_PORT_MASK;
780 ddi_select = TRANS_DDI_SELECT_PORT(port);
781 }
782
783 tmp = intel_de_read(dev_priv,
784 TRANS_DDI_FUNC_CTL(cpu_transcoder));
785 intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
786 trans_wakeref);
787
788 if ((tmp & port_mask) != ddi_select)
789 continue;
790
791 if ((tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_DP_MST ||
792 (HAS_DP20(dev_priv) &&
793 (tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_FDI_OR_128B132B))
794 mst_pipe_mask |= BIT(p);
795
796 *pipe_mask |= BIT(p);
797 }
798
799 if (!*pipe_mask)
800 drm_dbg_kms(&dev_priv->drm,
801 "No pipe for [ENCODER:%d:%s] found\n",
802 encoder->base.base.id, encoder->base.name);
803
804 if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
805 drm_dbg_kms(&dev_priv->drm,
806 "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
807 encoder->base.base.id, encoder->base.name,
808 *pipe_mask);
809 *pipe_mask = BIT(ffs(*pipe_mask) - 1);
810 }
811
812 if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
813 drm_dbg_kms(&dev_priv->drm,
814 "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
815 encoder->base.base.id, encoder->base.name,
816 *pipe_mask, mst_pipe_mask);
817 else
818 *is_dp_mst = mst_pipe_mask;
819
820out:
821 if (*pipe_mask && (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))) {
822 tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
823 if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
824 BXT_PHY_LANE_POWERDOWN_ACK |
825 BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
826 drm_err(&dev_priv->drm,
827 "[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
828 encoder->base.base.id, encoder->base.name, tmp);
829 }
830
831 intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
832}
833
834bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
835 enum pipe *pipe)
836{
837 u8 pipe_mask;
838 bool is_mst;
839
840 intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
841
842 if (is_mst || !pipe_mask)
843 return false;
844
845 *pipe = ffs(pipe_mask) - 1;
846
847 return true;
848}
849
850static enum intel_display_power_domain
851intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port,
852 const struct intel_crtc_state *crtc_state)
853{
854 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
855 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
856
857 /*
858 * ICL+ HW requires corresponding AUX IOs to be powered up for PSR with
859 * DC states enabled at the same time, while for driver initiated AUX
860 * transfers we need the same AUX IOs to be powered but with DC states
861 * disabled. Accordingly use the AUX_IO_<port> power domain here which
862 * leaves DC states enabled.
863 *
864 * Before MTL TypeC PHYs (in all TypeC modes and both DP/HDMI) also require
865 * AUX IO to be enabled, but all these require DC_OFF to be enabled as
866 * well, so we can acquire a wider AUX_<port> power domain reference
867 * instead of a specific AUX_IO_<port> reference without powering up any
868 * extra wells.
869 */
870 if (intel_encoder_can_psr(&dig_port->base))
871 return intel_display_power_aux_io_domain(i915, dig_port->aux_ch);
872 else if (DISPLAY_VER(i915) < 14 &&
873 (intel_crtc_has_dp_encoder(crtc_state) ||
874 intel_phy_is_tc(i915, phy)))
875 return intel_aux_power_domain(dig_port);
876 else
877 return POWER_DOMAIN_INVALID;
878}
879
880static void
881main_link_aux_power_domain_get(struct intel_digital_port *dig_port,
882 const struct intel_crtc_state *crtc_state)
883{
884 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
885 enum intel_display_power_domain domain =
886 intel_ddi_main_link_aux_domain(dig_port, crtc_state);
887
888 drm_WARN_ON(&i915->drm, dig_port->aux_wakeref);
889
890 if (domain == POWER_DOMAIN_INVALID)
891 return;
892
893 dig_port->aux_wakeref = intel_display_power_get(i915, domain);
894}
895
896static void
897main_link_aux_power_domain_put(struct intel_digital_port *dig_port,
898 const struct intel_crtc_state *crtc_state)
899{
900 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
901 enum intel_display_power_domain domain =
902 intel_ddi_main_link_aux_domain(dig_port, crtc_state);
903 intel_wakeref_t wf;
904
905 wf = fetch_and_zero(&dig_port->aux_wakeref);
906 if (!wf)
907 return;
908
909 intel_display_power_put(i915, domain, wf);
910}
911
912static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
913 struct intel_crtc_state *crtc_state)
914{
915 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
916 struct intel_digital_port *dig_port;
917
918 /*
919 * TODO: Add support for MST encoders. Atm, the following should never
920 * happen since fake-MST encoders don't set their get_power_domains()
921 * hook.
922 */
923 if (drm_WARN_ON(&dev_priv->drm,
924 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
925 return;
926
927 dig_port = enc_to_dig_port(encoder);
928
929 if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
930 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
931 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
932 dig_port->ddi_io_power_domain);
933 }
934
935 main_link_aux_power_domain_get(dig_port, crtc_state);
936}
937
938void intel_ddi_enable_pipe_clock(struct intel_encoder *encoder,
939 const struct intel_crtc_state *crtc_state)
940{
941 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
942 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
943 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
944 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
945 u32 val;
946
947 if (cpu_transcoder != TRANSCODER_EDP) {
948 if (DISPLAY_VER(dev_priv) >= 13)
949 val = TGL_TRANS_CLK_SEL_PORT(phy);
950 else if (DISPLAY_VER(dev_priv) >= 12)
951 val = TGL_TRANS_CLK_SEL_PORT(encoder->port);
952 else
953 val = TRANS_CLK_SEL_PORT(encoder->port);
954
955 intel_de_write(dev_priv, TRANS_CLK_SEL(cpu_transcoder), val);
956 }
957}
958
959void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
960{
961 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
962 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
963
964 if (cpu_transcoder != TRANSCODER_EDP) {
965 if (DISPLAY_VER(dev_priv) >= 12)
966 intel_de_write(dev_priv,
967 TRANS_CLK_SEL(cpu_transcoder),
968 TGL_TRANS_CLK_SEL_DISABLED);
969 else
970 intel_de_write(dev_priv,
971 TRANS_CLK_SEL(cpu_transcoder),
972 TRANS_CLK_SEL_DISABLED);
973 }
974}
975
976static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
977 enum port port, u8 iboost)
978{
979 u32 tmp;
980
981 tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
982 tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
983 if (iboost)
984 tmp |= iboost << BALANCE_LEG_SHIFT(port);
985 else
986 tmp |= BALANCE_LEG_DISABLE(port);
987 intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
988}
989
990static void skl_ddi_set_iboost(struct intel_encoder *encoder,
991 const struct intel_crtc_state *crtc_state,
992 int level)
993{
994 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
995 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
996 u8 iboost;
997
998 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
999 iboost = intel_bios_encoder_hdmi_boost_level(encoder->devdata);
1000 else
1001 iboost = intel_bios_encoder_dp_boost_level(encoder->devdata);
1002
1003 if (iboost == 0) {
1004 const struct intel_ddi_buf_trans *trans;
1005 int n_entries;
1006
1007 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1008 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1009 return;
1010
1011 iboost = trans->entries[level].hsw.i_boost;
1012 }
1013
1014 /* Make sure that the requested I_boost is valid */
1015 if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
1016 drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
1017 return;
1018 }
1019
1020 _skl_ddi_set_iboost(dev_priv, encoder->port, iboost);
1021
1022 if (encoder->port == PORT_A && dig_port->max_lanes == 4)
1023 _skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
1024}
1025
1026static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp,
1027 const struct intel_crtc_state *crtc_state)
1028{
1029 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1030 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1031 int n_entries;
1032
1033 encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1034
1035 if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
1036 n_entries = 1;
1037 if (drm_WARN_ON(&dev_priv->drm,
1038 n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1039 n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1040
1041 return index_to_dp_signal_levels[n_entries - 1] &
1042 DP_TRAIN_VOLTAGE_SWING_MASK;
1043}
1044
1045/*
1046 * We assume that the full set of pre-emphasis values can be
1047 * used on all DDI platforms. Should that change we need to
1048 * rethink this code.
1049 */
1050static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
1051{
1052 return DP_TRAIN_PRE_EMPH_LEVEL_3;
1053}
1054
1055static u32 icl_combo_phy_loadgen_select(const struct intel_crtc_state *crtc_state,
1056 int lane)
1057{
1058 if (crtc_state->port_clock > 600000)
1059 return 0;
1060
1061 if (crtc_state->lane_count == 4)
1062 return lane >= 1 ? LOADGEN_SELECT : 0;
1063 else
1064 return lane == 1 || lane == 2 ? LOADGEN_SELECT : 0;
1065}
1066
1067static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
1068 const struct intel_crtc_state *crtc_state)
1069{
1070 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1071 const struct intel_ddi_buf_trans *trans;
1072 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1073 int n_entries, ln;
1074 u32 val;
1075
1076 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1077 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1078 return;
1079
1080 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP)) {
1081 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1082
1083 val = EDP4K2K_MODE_OVRD_EN | EDP4K2K_MODE_OVRD_OPTIMIZED;
1084 intel_dp->hobl_active = is_hobl_buf_trans(trans);
1085 intel_de_rmw(dev_priv, ICL_PORT_CL_DW10(phy), val,
1086 intel_dp->hobl_active ? val : 0);
1087 }
1088
1089 /* Set PORT_TX_DW5 */
1090 val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1091 val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
1092 TAP2_DISABLE | TAP3_DISABLE);
1093 val |= SCALING_MODE_SEL(0x2);
1094 val |= RTERM_SELECT(0x6);
1095 val |= TAP3_DISABLE;
1096 intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1097
1098 /* Program PORT_TX_DW2 */
1099 for (ln = 0; ln < 4; ln++) {
1100 int level = intel_ddi_level(encoder, crtc_state, ln);
1101
1102 intel_de_rmw(dev_priv, ICL_PORT_TX_DW2_LN(ln, phy),
1103 SWING_SEL_UPPER_MASK | SWING_SEL_LOWER_MASK | RCOMP_SCALAR_MASK,
1104 SWING_SEL_UPPER(trans->entries[level].icl.dw2_swing_sel) |
1105 SWING_SEL_LOWER(trans->entries[level].icl.dw2_swing_sel) |
1106 RCOMP_SCALAR(0x98));
1107 }
1108
1109 /* Program PORT_TX_DW4 */
1110 /* We cannot write to GRP. It would overwrite individual loadgen. */
1111 for (ln = 0; ln < 4; ln++) {
1112 int level = intel_ddi_level(encoder, crtc_state, ln);
1113
1114 intel_de_rmw(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy),
1115 POST_CURSOR_1_MASK | POST_CURSOR_2_MASK | CURSOR_COEFF_MASK,
1116 POST_CURSOR_1(trans->entries[level].icl.dw4_post_cursor_1) |
1117 POST_CURSOR_2(trans->entries[level].icl.dw4_post_cursor_2) |
1118 CURSOR_COEFF(trans->entries[level].icl.dw4_cursor_coeff));
1119 }
1120
1121 /* Program PORT_TX_DW7 */
1122 for (ln = 0; ln < 4; ln++) {
1123 int level = intel_ddi_level(encoder, crtc_state, ln);
1124
1125 intel_de_rmw(dev_priv, ICL_PORT_TX_DW7_LN(ln, phy),
1126 N_SCALAR_MASK,
1127 N_SCALAR(trans->entries[level].icl.dw7_n_scalar));
1128 }
1129}
1130
1131static void icl_combo_phy_set_signal_levels(struct intel_encoder *encoder,
1132 const struct intel_crtc_state *crtc_state)
1133{
1134 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1135 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1136 u32 val;
1137 int ln;
1138
1139 /*
1140 * 1. If port type is eDP or DP,
1141 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
1142 * else clear to 0b.
1143 */
1144 val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN(0, phy));
1145 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1146 val &= ~COMMON_KEEPER_EN;
1147 else
1148 val |= COMMON_KEEPER_EN;
1149 intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
1150
1151 /* 2. Program loadgen select */
1152 /*
1153 * Program PORT_TX_DW4 depending on Bit rate and used lanes
1154 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
1155 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
1156 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
1157 */
1158 for (ln = 0; ln < 4; ln++) {
1159 intel_de_rmw(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy),
1160 LOADGEN_SELECT,
1161 icl_combo_phy_loadgen_select(crtc_state, ln));
1162 }
1163
1164 /* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
1165 intel_de_rmw(dev_priv, ICL_PORT_CL_DW5(phy),
1166 0, SUS_CLOCK_CONFIG);
1167
1168 /* 4. Clear training enable to change swing values */
1169 val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1170 val &= ~TX_TRAINING_EN;
1171 intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1172
1173 /* 5. Program swing and de-emphasis */
1174 icl_ddi_combo_vswing_program(encoder, crtc_state);
1175
1176 /* 6. Set training enable to trigger update */
1177 val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1178 val |= TX_TRAINING_EN;
1179 intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1180}
1181
1182static void icl_mg_phy_set_signal_levels(struct intel_encoder *encoder,
1183 const struct intel_crtc_state *crtc_state)
1184{
1185 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1186 enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1187 const struct intel_ddi_buf_trans *trans;
1188 int n_entries, ln;
1189
1190 if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1191 return;
1192
1193 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1194 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1195 return;
1196
1197 for (ln = 0; ln < 2; ln++) {
1198 intel_de_rmw(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port),
1199 CRI_USE_FS32, 0);
1200 intel_de_rmw(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port),
1201 CRI_USE_FS32, 0);
1202 }
1203
1204 /* Program MG_TX_SWINGCTRL with values from vswing table */
1205 for (ln = 0; ln < 2; ln++) {
1206 int level;
1207
1208 level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1209
1210 intel_de_rmw(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port),
1211 CRI_TXDEEMPH_OVERRIDE_17_12_MASK,
1212 CRI_TXDEEMPH_OVERRIDE_17_12(trans->entries[level].mg.cri_txdeemph_override_17_12));
1213
1214 level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1215
1216 intel_de_rmw(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port),
1217 CRI_TXDEEMPH_OVERRIDE_17_12_MASK,
1218 CRI_TXDEEMPH_OVERRIDE_17_12(trans->entries[level].mg.cri_txdeemph_override_17_12));
1219 }
1220
1221 /* Program MG_TX_DRVCTRL with values from vswing table */
1222 for (ln = 0; ln < 2; ln++) {
1223 int level;
1224
1225 level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1226
1227 intel_de_rmw(dev_priv, MG_TX1_DRVCTRL(ln, tc_port),
1228 CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1229 CRI_TXDEEMPH_OVERRIDE_5_0_MASK,
1230 CRI_TXDEEMPH_OVERRIDE_11_6(trans->entries[level].mg.cri_txdeemph_override_11_6) |
1231 CRI_TXDEEMPH_OVERRIDE_5_0(trans->entries[level].mg.cri_txdeemph_override_5_0) |
1232 CRI_TXDEEMPH_OVERRIDE_EN);
1233
1234 level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1235
1236 intel_de_rmw(dev_priv, MG_TX2_DRVCTRL(ln, tc_port),
1237 CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1238 CRI_TXDEEMPH_OVERRIDE_5_0_MASK,
1239 CRI_TXDEEMPH_OVERRIDE_11_6(trans->entries[level].mg.cri_txdeemph_override_11_6) |
1240 CRI_TXDEEMPH_OVERRIDE_5_0(trans->entries[level].mg.cri_txdeemph_override_5_0) |
1241 CRI_TXDEEMPH_OVERRIDE_EN);
1242
1243 /* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
1244 }
1245
1246 /*
1247 * Program MG_CLKHUB<LN, port being used> with value from frequency table
1248 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
1249 * values from table for which TX1 and TX2 enabled.
1250 */
1251 for (ln = 0; ln < 2; ln++) {
1252 intel_de_rmw(dev_priv, MG_CLKHUB(ln, tc_port),
1253 CFG_LOW_RATE_LKREN_EN,
1254 crtc_state->port_clock < 300000 ? CFG_LOW_RATE_LKREN_EN : 0);
1255 }
1256
1257 /* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
1258 for (ln = 0; ln < 2; ln++) {
1259 intel_de_rmw(dev_priv, MG_TX1_DCC(ln, tc_port),
1260 CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK |
1261 CFG_AMI_CK_DIV_OVERRIDE_EN,
1262 crtc_state->port_clock > 500000 ?
1263 CFG_AMI_CK_DIV_OVERRIDE_VAL(1) |
1264 CFG_AMI_CK_DIV_OVERRIDE_EN : 0);
1265
1266 intel_de_rmw(dev_priv, MG_TX2_DCC(ln, tc_port),
1267 CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK |
1268 CFG_AMI_CK_DIV_OVERRIDE_EN,
1269 crtc_state->port_clock > 500000 ?
1270 CFG_AMI_CK_DIV_OVERRIDE_VAL(1) |
1271 CFG_AMI_CK_DIV_OVERRIDE_EN : 0);
1272 }
1273
1274 /* Program MG_TX_PISO_READLOAD with values from vswing table */
1275 for (ln = 0; ln < 2; ln++) {
1276 intel_de_rmw(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
1277 0, CRI_CALCINIT);
1278 intel_de_rmw(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
1279 0, CRI_CALCINIT);
1280 }
1281}
1282
1283static void tgl_dkl_phy_set_signal_levels(struct intel_encoder *encoder,
1284 const struct intel_crtc_state *crtc_state)
1285{
1286 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1287 enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1288 const struct intel_ddi_buf_trans *trans;
1289 int n_entries, ln;
1290
1291 if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1292 return;
1293
1294 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1295 if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
1296 return;
1297
1298 for (ln = 0; ln < 2; ln++) {
1299 int level;
1300
1301 intel_dkl_phy_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port, ln), 0);
1302
1303 level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1304
1305 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL0(tc_port, ln),
1306 DKL_TX_PRESHOOT_COEFF_MASK |
1307 DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1308 DKL_TX_VSWING_CONTROL_MASK,
1309 DKL_TX_PRESHOOT_COEFF(trans->entries[level].dkl.preshoot) |
1310 DKL_TX_DE_EMPHASIS_COEFF(trans->entries[level].dkl.de_emphasis) |
1311 DKL_TX_VSWING_CONTROL(trans->entries[level].dkl.vswing));
1312
1313 level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1314
1315 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL1(tc_port, ln),
1316 DKL_TX_PRESHOOT_COEFF_MASK |
1317 DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1318 DKL_TX_VSWING_CONTROL_MASK,
1319 DKL_TX_PRESHOOT_COEFF(trans->entries[level].dkl.preshoot) |
1320 DKL_TX_DE_EMPHASIS_COEFF(trans->entries[level].dkl.de_emphasis) |
1321 DKL_TX_VSWING_CONTROL(trans->entries[level].dkl.vswing));
1322
1323 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL2(tc_port, ln),
1324 DKL_TX_DP20BITMODE, 0);
1325
1326 if (IS_ALDERLAKE_P(dev_priv)) {
1327 u32 val;
1328
1329 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1330 if (ln == 0) {
1331 val = DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1(0);
1332 val |= DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2(2);
1333 } else {
1334 val = DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1(3);
1335 val |= DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2(3);
1336 }
1337 } else {
1338 val = DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1(0);
1339 val |= DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2(0);
1340 }
1341
1342 intel_dkl_phy_rmw(dev_priv, DKL_TX_DPCNTL2(tc_port, ln),
1343 DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX1_MASK |
1344 DKL_TX_DPCNTL2_CFG_LOADGENSELECT_TX2_MASK,
1345 val);
1346 }
1347 }
1348}
1349
1350static int translate_signal_level(struct intel_dp *intel_dp,
1351 u8 signal_levels)
1352{
1353 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1354 int i;
1355
1356 for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
1357 if (index_to_dp_signal_levels[i] == signal_levels)
1358 return i;
1359 }
1360
1361 drm_WARN(&i915->drm, 1,
1362 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
1363 signal_levels);
1364
1365 return 0;
1366}
1367
1368static int intel_ddi_dp_level(struct intel_dp *intel_dp,
1369 const struct intel_crtc_state *crtc_state,
1370 int lane)
1371{
1372 u8 train_set = intel_dp->train_set[lane];
1373
1374 if (intel_dp_is_uhbr(crtc_state)) {
1375 return train_set & DP_TX_FFE_PRESET_VALUE_MASK;
1376 } else {
1377 u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1378 DP_TRAIN_PRE_EMPHASIS_MASK);
1379
1380 return translate_signal_level(intel_dp, signal_levels);
1381 }
1382}
1383
1384int intel_ddi_level(struct intel_encoder *encoder,
1385 const struct intel_crtc_state *crtc_state,
1386 int lane)
1387{
1388 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1389 const struct intel_ddi_buf_trans *trans;
1390 int level, n_entries;
1391
1392 trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1393 if (drm_WARN_ON_ONCE(&i915->drm, !trans))
1394 return 0;
1395
1396 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1397 level = intel_ddi_hdmi_level(encoder, trans);
1398 else
1399 level = intel_ddi_dp_level(enc_to_intel_dp(encoder), crtc_state,
1400 lane);
1401
1402 if (drm_WARN_ON_ONCE(&i915->drm, level >= n_entries))
1403 level = n_entries - 1;
1404
1405 return level;
1406}
1407
1408static void
1409hsw_set_signal_levels(struct intel_encoder *encoder,
1410 const struct intel_crtc_state *crtc_state)
1411{
1412 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1413 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1414 int level = intel_ddi_level(encoder, crtc_state, 0);
1415 enum port port = encoder->port;
1416 u32 signal_levels;
1417
1418 if (has_iboost(dev_priv))
1419 skl_ddi_set_iboost(encoder, crtc_state, level);
1420
1421 /* HDMI ignores the rest */
1422 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1423 return;
1424
1425 signal_levels = DDI_BUF_TRANS_SELECT(level);
1426
1427 drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
1428 signal_levels);
1429
1430 intel_dp->DP &= ~DDI_BUF_EMP_MASK;
1431 intel_dp->DP |= signal_levels;
1432
1433 intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
1434 intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
1435}
1436
1437static void _icl_ddi_enable_clock(struct drm_i915_private *i915, i915_reg_t reg,
1438 u32 clk_sel_mask, u32 clk_sel, u32 clk_off)
1439{
1440 mutex_lock(&i915->display.dpll.lock);
1441
1442 intel_de_rmw(i915, reg, clk_sel_mask, clk_sel);
1443
1444 /*
1445 * "This step and the step before must be
1446 * done with separate register writes."
1447 */
1448 intel_de_rmw(i915, reg, clk_off, 0);
1449
1450 mutex_unlock(&i915->display.dpll.lock);
1451}
1452
1453static void _icl_ddi_disable_clock(struct drm_i915_private *i915, i915_reg_t reg,
1454 u32 clk_off)
1455{
1456 mutex_lock(&i915->display.dpll.lock);
1457
1458 intel_de_rmw(i915, reg, 0, clk_off);
1459
1460 mutex_unlock(&i915->display.dpll.lock);
1461}
1462
1463static bool _icl_ddi_is_clock_enabled(struct drm_i915_private *i915, i915_reg_t reg,
1464 u32 clk_off)
1465{
1466 return !(intel_de_read(i915, reg) & clk_off);
1467}
1468
1469static struct intel_shared_dpll *
1470_icl_ddi_get_pll(struct drm_i915_private *i915, i915_reg_t reg,
1471 u32 clk_sel_mask, u32 clk_sel_shift)
1472{
1473 enum intel_dpll_id id;
1474
1475 id = (intel_de_read(i915, reg) & clk_sel_mask) >> clk_sel_shift;
1476
1477 return intel_get_shared_dpll_by_id(i915, id);
1478}
1479
1480static void adls_ddi_enable_clock(struct intel_encoder *encoder,
1481 const struct intel_crtc_state *crtc_state)
1482{
1483 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1484 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1485 enum phy phy = intel_port_to_phy(i915, encoder->port);
1486
1487 if (drm_WARN_ON(&i915->drm, !pll))
1488 return;
1489
1490 _icl_ddi_enable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
1491 ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1492 pll->info->id << ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy),
1493 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1494}
1495
1496static void adls_ddi_disable_clock(struct intel_encoder *encoder)
1497{
1498 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1499 enum phy phy = intel_port_to_phy(i915, encoder->port);
1500
1501 _icl_ddi_disable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
1502 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1503}
1504
1505static bool adls_ddi_is_clock_enabled(struct intel_encoder *encoder)
1506{
1507 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1508 enum phy phy = intel_port_to_phy(i915, encoder->port);
1509
1510 return _icl_ddi_is_clock_enabled(i915, ADLS_DPCLKA_CFGCR(phy),
1511 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1512}
1513
1514static struct intel_shared_dpll *adls_ddi_get_pll(struct intel_encoder *encoder)
1515{
1516 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1517 enum phy phy = intel_port_to_phy(i915, encoder->port);
1518
1519 return _icl_ddi_get_pll(i915, ADLS_DPCLKA_CFGCR(phy),
1520 ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1521 ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy));
1522}
1523
1524static void rkl_ddi_enable_clock(struct intel_encoder *encoder,
1525 const struct intel_crtc_state *crtc_state)
1526{
1527 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1528 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1529 enum phy phy = intel_port_to_phy(i915, encoder->port);
1530
1531 if (drm_WARN_ON(&i915->drm, !pll))
1532 return;
1533
1534 _icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
1535 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1536 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
1537 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1538}
1539
1540static void rkl_ddi_disable_clock(struct intel_encoder *encoder)
1541{
1542 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1543 enum phy phy = intel_port_to_phy(i915, encoder->port);
1544
1545 _icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
1546 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1547}
1548
1549static bool rkl_ddi_is_clock_enabled(struct intel_encoder *encoder)
1550{
1551 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1552 enum phy phy = intel_port_to_phy(i915, encoder->port);
1553
1554 return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
1555 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1556}
1557
1558static struct intel_shared_dpll *rkl_ddi_get_pll(struct intel_encoder *encoder)
1559{
1560 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1561 enum phy phy = intel_port_to_phy(i915, encoder->port);
1562
1563 return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1564 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1565 RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1566}
1567
1568static void dg1_ddi_enable_clock(struct intel_encoder *encoder,
1569 const struct intel_crtc_state *crtc_state)
1570{
1571 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1572 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1573 enum phy phy = intel_port_to_phy(i915, encoder->port);
1574
1575 if (drm_WARN_ON(&i915->drm, !pll))
1576 return;
1577
1578 /*
1579 * If we fail this, something went very wrong: first 2 PLLs should be
1580 * used by first 2 phys and last 2 PLLs by last phys
1581 */
1582 if (drm_WARN_ON(&i915->drm,
1583 (pll->info->id < DPLL_ID_DG1_DPLL2 && phy >= PHY_C) ||
1584 (pll->info->id >= DPLL_ID_DG1_DPLL2 && phy < PHY_C)))
1585 return;
1586
1587 _icl_ddi_enable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
1588 DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1589 DG1_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
1590 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1591}
1592
1593static void dg1_ddi_disable_clock(struct intel_encoder *encoder)
1594{
1595 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1596 enum phy phy = intel_port_to_phy(i915, encoder->port);
1597
1598 _icl_ddi_disable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
1599 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1600}
1601
1602static bool dg1_ddi_is_clock_enabled(struct intel_encoder *encoder)
1603{
1604 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1605 enum phy phy = intel_port_to_phy(i915, encoder->port);
1606
1607 return _icl_ddi_is_clock_enabled(i915, DG1_DPCLKA_CFGCR0(phy),
1608 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1609}
1610
1611static struct intel_shared_dpll *dg1_ddi_get_pll(struct intel_encoder *encoder)
1612{
1613 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1614 enum phy phy = intel_port_to_phy(i915, encoder->port);
1615 enum intel_dpll_id id;
1616 u32 val;
1617
1618 val = intel_de_read(i915, DG1_DPCLKA_CFGCR0(phy));
1619 val &= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
1620 val >>= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
1621 id = val;
1622
1623 /*
1624 * _DG1_DPCLKA0_CFGCR0 maps between DPLL 0 and 1 with one bit for phy A
1625 * and B while _DG1_DPCLKA1_CFGCR0 maps between DPLL 2 and 3 with one
1626 * bit for phy C and D.
1627 */
1628 if (phy >= PHY_C)
1629 id += DPLL_ID_DG1_DPLL2;
1630
1631 return intel_get_shared_dpll_by_id(i915, id);
1632}
1633
1634static void icl_ddi_combo_enable_clock(struct intel_encoder *encoder,
1635 const struct intel_crtc_state *crtc_state)
1636{
1637 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1638 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1639 enum phy phy = intel_port_to_phy(i915, encoder->port);
1640
1641 if (drm_WARN_ON(&i915->drm, !pll))
1642 return;
1643
1644 _icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
1645 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1646 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
1647 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1648}
1649
1650static void icl_ddi_combo_disable_clock(struct intel_encoder *encoder)
1651{
1652 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1653 enum phy phy = intel_port_to_phy(i915, encoder->port);
1654
1655 _icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
1656 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1657}
1658
1659static bool icl_ddi_combo_is_clock_enabled(struct intel_encoder *encoder)
1660{
1661 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1662 enum phy phy = intel_port_to_phy(i915, encoder->port);
1663
1664 return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
1665 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1666}
1667
1668struct intel_shared_dpll *icl_ddi_combo_get_pll(struct intel_encoder *encoder)
1669{
1670 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1671 enum phy phy = intel_port_to_phy(i915, encoder->port);
1672
1673 return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1674 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1675 ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1676}
1677
1678static void jsl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1679 const struct intel_crtc_state *crtc_state)
1680{
1681 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1682 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1683 enum port port = encoder->port;
1684
1685 if (drm_WARN_ON(&i915->drm, !pll))
1686 return;
1687
1688 /*
1689 * "For DDIC and DDID, program DDI_CLK_SEL to map the MG clock to the port.
1690 * MG does not exist, but the programming is required to ungate DDIC and DDID."
1691 */
1692 intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_MG);
1693
1694 icl_ddi_combo_enable_clock(encoder, crtc_state);
1695}
1696
1697static void jsl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1698{
1699 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1700 enum port port = encoder->port;
1701
1702 icl_ddi_combo_disable_clock(encoder);
1703
1704 intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1705}
1706
1707static bool jsl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
1708{
1709 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1710 enum port port = encoder->port;
1711 u32 tmp;
1712
1713 tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1714
1715 if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
1716 return false;
1717
1718 return icl_ddi_combo_is_clock_enabled(encoder);
1719}
1720
1721static void icl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1722 const struct intel_crtc_state *crtc_state)
1723{
1724 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1725 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1726 enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1727 enum port port = encoder->port;
1728
1729 if (drm_WARN_ON(&i915->drm, !pll))
1730 return;
1731
1732 intel_de_write(i915, DDI_CLK_SEL(port),
1733 icl_pll_to_ddi_clk_sel(encoder, crtc_state));
1734
1735 mutex_lock(&i915->display.dpll.lock);
1736
1737 intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
1738 ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port), 0);
1739
1740 mutex_unlock(&i915->display.dpll.lock);
1741}
1742
1743static void icl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1744{
1745 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1746 enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1747 enum port port = encoder->port;
1748
1749 mutex_lock(&i915->display.dpll.lock);
1750
1751 intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
1752 0, ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
1753
1754 mutex_unlock(&i915->display.dpll.lock);
1755
1756 intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1757}
1758
1759static bool icl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
1760{
1761 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1762 enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1763 enum port port = encoder->port;
1764 u32 tmp;
1765
1766 tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1767
1768 if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
1769 return false;
1770
1771 tmp = intel_de_read(i915, ICL_DPCLKA_CFGCR0);
1772
1773 return !(tmp & ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
1774}
1775
1776static struct intel_shared_dpll *icl_ddi_tc_get_pll(struct intel_encoder *encoder)
1777{
1778 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1779 enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1780 enum port port = encoder->port;
1781 enum intel_dpll_id id;
1782 u32 tmp;
1783
1784 tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1785
1786 switch (tmp & DDI_CLK_SEL_MASK) {
1787 case DDI_CLK_SEL_TBT_162:
1788 case DDI_CLK_SEL_TBT_270:
1789 case DDI_CLK_SEL_TBT_540:
1790 case DDI_CLK_SEL_TBT_810:
1791 id = DPLL_ID_ICL_TBTPLL;
1792 break;
1793 case DDI_CLK_SEL_MG:
1794 id = icl_tc_port_to_pll_id(tc_port);
1795 break;
1796 default:
1797 MISSING_CASE(tmp);
1798 fallthrough;
1799 case DDI_CLK_SEL_NONE:
1800 return NULL;
1801 }
1802
1803 return intel_get_shared_dpll_by_id(i915, id);
1804}
1805
1806static struct intel_shared_dpll *bxt_ddi_get_pll(struct intel_encoder *encoder)
1807{
1808 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1809 enum intel_dpll_id id;
1810
1811 switch (encoder->port) {
1812 case PORT_A:
1813 id = DPLL_ID_SKL_DPLL0;
1814 break;
1815 case PORT_B:
1816 id = DPLL_ID_SKL_DPLL1;
1817 break;
1818 case PORT_C:
1819 id = DPLL_ID_SKL_DPLL2;
1820 break;
1821 default:
1822 MISSING_CASE(encoder->port);
1823 return NULL;
1824 }
1825
1826 return intel_get_shared_dpll_by_id(i915, id);
1827}
1828
1829static void skl_ddi_enable_clock(struct intel_encoder *encoder,
1830 const struct intel_crtc_state *crtc_state)
1831{
1832 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1833 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1834 enum port port = encoder->port;
1835
1836 if (drm_WARN_ON(&i915->drm, !pll))
1837 return;
1838
1839 mutex_lock(&i915->display.dpll.lock);
1840
1841 intel_de_rmw(i915, DPLL_CTRL2,
1842 DPLL_CTRL2_DDI_CLK_OFF(port) |
1843 DPLL_CTRL2_DDI_CLK_SEL_MASK(port),
1844 DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
1845 DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
1846
1847 mutex_unlock(&i915->display.dpll.lock);
1848}
1849
1850static void skl_ddi_disable_clock(struct intel_encoder *encoder)
1851{
1852 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1853 enum port port = encoder->port;
1854
1855 mutex_lock(&i915->display.dpll.lock);
1856
1857 intel_de_rmw(i915, DPLL_CTRL2,
1858 0, DPLL_CTRL2_DDI_CLK_OFF(port));
1859
1860 mutex_unlock(&i915->display.dpll.lock);
1861}
1862
1863static bool skl_ddi_is_clock_enabled(struct intel_encoder *encoder)
1864{
1865 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1866 enum port port = encoder->port;
1867
1868 /*
1869 * FIXME Not sure if the override affects both
1870 * the PLL selection and the CLK_OFF bit.
1871 */
1872 return !(intel_de_read(i915, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_OFF(port));
1873}
1874
1875static struct intel_shared_dpll *skl_ddi_get_pll(struct intel_encoder *encoder)
1876{
1877 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1878 enum port port = encoder->port;
1879 enum intel_dpll_id id;
1880 u32 tmp;
1881
1882 tmp = intel_de_read(i915, DPLL_CTRL2);
1883
1884 /*
1885 * FIXME Not sure if the override affects both
1886 * the PLL selection and the CLK_OFF bit.
1887 */
1888 if ((tmp & DPLL_CTRL2_DDI_SEL_OVERRIDE(port)) == 0)
1889 return NULL;
1890
1891 id = (tmp & DPLL_CTRL2_DDI_CLK_SEL_MASK(port)) >>
1892 DPLL_CTRL2_DDI_CLK_SEL_SHIFT(port);
1893
1894 return intel_get_shared_dpll_by_id(i915, id);
1895}
1896
1897void hsw_ddi_enable_clock(struct intel_encoder *encoder,
1898 const struct intel_crtc_state *crtc_state)
1899{
1900 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1901 const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1902 enum port port = encoder->port;
1903
1904 if (drm_WARN_ON(&i915->drm, !pll))
1905 return;
1906
1907 intel_de_write(i915, PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
1908}
1909
1910void hsw_ddi_disable_clock(struct intel_encoder *encoder)
1911{
1912 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1913 enum port port = encoder->port;
1914
1915 intel_de_write(i915, PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1916}
1917
1918bool hsw_ddi_is_clock_enabled(struct intel_encoder *encoder)
1919{
1920 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1921 enum port port = encoder->port;
1922
1923 return intel_de_read(i915, PORT_CLK_SEL(port)) != PORT_CLK_SEL_NONE;
1924}
1925
1926static struct intel_shared_dpll *hsw_ddi_get_pll(struct intel_encoder *encoder)
1927{
1928 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1929 enum port port = encoder->port;
1930 enum intel_dpll_id id;
1931 u32 tmp;
1932
1933 tmp = intel_de_read(i915, PORT_CLK_SEL(port));
1934
1935 switch (tmp & PORT_CLK_SEL_MASK) {
1936 case PORT_CLK_SEL_WRPLL1:
1937 id = DPLL_ID_WRPLL1;
1938 break;
1939 case PORT_CLK_SEL_WRPLL2:
1940 id = DPLL_ID_WRPLL2;
1941 break;
1942 case PORT_CLK_SEL_SPLL:
1943 id = DPLL_ID_SPLL;
1944 break;
1945 case PORT_CLK_SEL_LCPLL_810:
1946 id = DPLL_ID_LCPLL_810;
1947 break;
1948 case PORT_CLK_SEL_LCPLL_1350:
1949 id = DPLL_ID_LCPLL_1350;
1950 break;
1951 case PORT_CLK_SEL_LCPLL_2700:
1952 id = DPLL_ID_LCPLL_2700;
1953 break;
1954 default:
1955 MISSING_CASE(tmp);
1956 fallthrough;
1957 case PORT_CLK_SEL_NONE:
1958 return NULL;
1959 }
1960
1961 return intel_get_shared_dpll_by_id(i915, id);
1962}
1963
1964void intel_ddi_enable_clock(struct intel_encoder *encoder,
1965 const struct intel_crtc_state *crtc_state)
1966{
1967 if (encoder->enable_clock)
1968 encoder->enable_clock(encoder, crtc_state);
1969}
1970
1971void intel_ddi_disable_clock(struct intel_encoder *encoder)
1972{
1973 if (encoder->disable_clock)
1974 encoder->disable_clock(encoder);
1975}
1976
1977void intel_ddi_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
1978{
1979 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1980 u32 port_mask;
1981 bool ddi_clk_needed;
1982
1983 /*
1984 * In case of DP MST, we sanitize the primary encoder only, not the
1985 * virtual ones.
1986 */
1987 if (encoder->type == INTEL_OUTPUT_DP_MST)
1988 return;
1989
1990 if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
1991 u8 pipe_mask;
1992 bool is_mst;
1993
1994 intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
1995 /*
1996 * In the unlikely case that BIOS enables DP in MST mode, just
1997 * warn since our MST HW readout is incomplete.
1998 */
1999 if (drm_WARN_ON(&i915->drm, is_mst))
2000 return;
2001 }
2002
2003 port_mask = BIT(encoder->port);
2004 ddi_clk_needed = encoder->base.crtc;
2005
2006 if (encoder->type == INTEL_OUTPUT_DSI) {
2007 struct intel_encoder *other_encoder;
2008
2009 port_mask = intel_dsi_encoder_ports(encoder);
2010 /*
2011 * Sanity check that we haven't incorrectly registered another
2012 * encoder using any of the ports of this DSI encoder.
2013 */
2014 for_each_intel_encoder(&i915->drm, other_encoder) {
2015 if (other_encoder == encoder)
2016 continue;
2017
2018 if (drm_WARN_ON(&i915->drm,
2019 port_mask & BIT(other_encoder->port)))
2020 return;
2021 }
2022 /*
2023 * For DSI we keep the ddi clocks gated
2024 * except during enable/disable sequence.
2025 */
2026 ddi_clk_needed = false;
2027 }
2028
2029 if (ddi_clk_needed || !encoder->is_clock_enabled ||
2030 !encoder->is_clock_enabled(encoder))
2031 return;
2032
2033 drm_notice(&i915->drm,
2034 "[ENCODER:%d:%s] is disabled/in DSI mode with an ungated DDI clock, gate it\n",
2035 encoder->base.base.id, encoder->base.name);
2036
2037 encoder->disable_clock(encoder);
2038}
2039
2040static void
2041icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
2042 const struct intel_crtc_state *crtc_state)
2043{
2044 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2045 enum tc_port tc_port = intel_port_to_tc(dev_priv, dig_port->base.port);
2046 enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
2047 u32 ln0, ln1, pin_assignment;
2048 u8 width;
2049
2050 if (!intel_phy_is_tc(dev_priv, phy) ||
2051 intel_tc_port_in_tbt_alt_mode(dig_port))
2052 return;
2053
2054 if (DISPLAY_VER(dev_priv) >= 12) {
2055 ln0 = intel_dkl_phy_read(dev_priv, DKL_DP_MODE(tc_port, 0));
2056 ln1 = intel_dkl_phy_read(dev_priv, DKL_DP_MODE(tc_port, 1));
2057 } else {
2058 ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
2059 ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
2060 }
2061
2062 ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2063 ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2064
2065 /* DPPATC */
2066 pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
2067 width = crtc_state->lane_count;
2068
2069 switch (pin_assignment) {
2070 case 0x0:
2071 drm_WARN_ON(&dev_priv->drm,
2072 !intel_tc_port_in_legacy_mode(dig_port));
2073 if (width == 1) {
2074 ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
2075 } else {
2076 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2077 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2078 }
2079 break;
2080 case 0x1:
2081 if (width == 4) {
2082 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2083 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2084 }
2085 break;
2086 case 0x2:
2087 if (width == 2) {
2088 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2089 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2090 }
2091 break;
2092 case 0x3:
2093 case 0x5:
2094 if (width == 1) {
2095 ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
2096 ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
2097 } else {
2098 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2099 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2100 }
2101 break;
2102 case 0x4:
2103 case 0x6:
2104 if (width == 1) {
2105 ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
2106 ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
2107 } else {
2108 ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
2109 ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2110 }
2111 break;
2112 default:
2113 MISSING_CASE(pin_assignment);
2114 }
2115
2116 if (DISPLAY_VER(dev_priv) >= 12) {
2117 intel_dkl_phy_write(dev_priv, DKL_DP_MODE(tc_port, 0), ln0);
2118 intel_dkl_phy_write(dev_priv, DKL_DP_MODE(tc_port, 1), ln1);
2119 } else {
2120 intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
2121 intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
2122 }
2123}
2124
2125static enum transcoder
2126tgl_dp_tp_transcoder(const struct intel_crtc_state *crtc_state)
2127{
2128 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2129 return crtc_state->mst_master_transcoder;
2130 else
2131 return crtc_state->cpu_transcoder;
2132}
2133
2134i915_reg_t dp_tp_ctl_reg(struct intel_encoder *encoder,
2135 const struct intel_crtc_state *crtc_state)
2136{
2137 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2138
2139 if (DISPLAY_VER(dev_priv) >= 12)
2140 return TGL_DP_TP_CTL(tgl_dp_tp_transcoder(crtc_state));
2141 else
2142 return DP_TP_CTL(encoder->port);
2143}
2144
2145i915_reg_t dp_tp_status_reg(struct intel_encoder *encoder,
2146 const struct intel_crtc_state *crtc_state)
2147{
2148 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2149
2150 if (DISPLAY_VER(dev_priv) >= 12)
2151 return TGL_DP_TP_STATUS(tgl_dp_tp_transcoder(crtc_state));
2152 else
2153 return DP_TP_STATUS(encoder->port);
2154}
2155
2156static void intel_dp_sink_set_msa_timing_par_ignore_state(struct intel_dp *intel_dp,
2157 const struct intel_crtc_state *crtc_state,
2158 bool enable)
2159{
2160 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2161
2162 if (!crtc_state->vrr.enable)
2163 return;
2164
2165 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_DOWNSPREAD_CTRL,
2166 enable ? DP_MSA_TIMING_PAR_IGNORE_EN : 0) <= 0)
2167 drm_dbg_kms(&i915->drm,
2168 "Failed to %s MSA_TIMING_PAR_IGNORE in the sink\n",
2169 str_enable_disable(enable));
2170}
2171
2172static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
2173 const struct intel_crtc_state *crtc_state)
2174{
2175 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2176
2177 if (!crtc_state->fec_enable)
2178 return;
2179
2180 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
2181 drm_dbg_kms(&i915->drm,
2182 "Failed to set FEC_READY in the sink\n");
2183}
2184
2185static void intel_ddi_enable_fec(struct intel_encoder *encoder,
2186 const struct intel_crtc_state *crtc_state)
2187{
2188 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2189 struct intel_dp *intel_dp;
2190 u32 val;
2191
2192 if (!crtc_state->fec_enable)
2193 return;
2194
2195 intel_dp = enc_to_intel_dp(encoder);
2196 val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2197 val |= DP_TP_CTL_FEC_ENABLE;
2198 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2199}
2200
2201static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
2202 const struct intel_crtc_state *crtc_state)
2203{
2204 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2205 struct intel_dp *intel_dp;
2206 u32 val;
2207
2208 if (!crtc_state->fec_enable)
2209 return;
2210
2211 intel_dp = enc_to_intel_dp(encoder);
2212 val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2213 val &= ~DP_TP_CTL_FEC_ENABLE;
2214 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2215 intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2216}
2217
2218static void intel_ddi_power_up_lanes(struct intel_encoder *encoder,
2219 const struct intel_crtc_state *crtc_state)
2220{
2221 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2222 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2223 enum phy phy = intel_port_to_phy(i915, encoder->port);
2224
2225 if (intel_phy_is_combo(i915, phy)) {
2226 bool lane_reversal =
2227 dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
2228
2229 intel_combo_phy_power_up_lanes(i915, phy, false,
2230 crtc_state->lane_count,
2231 lane_reversal);
2232 }
2233}
2234
2235/* Splitter enable for eDP MSO is limited to certain pipes. */
2236static u8 intel_ddi_splitter_pipe_mask(struct drm_i915_private *i915)
2237{
2238 if (IS_ALDERLAKE_P(i915))
2239 return BIT(PIPE_A) | BIT(PIPE_B);
2240 else
2241 return BIT(PIPE_A);
2242}
2243
2244static void intel_ddi_mso_get_config(struct intel_encoder *encoder,
2245 struct intel_crtc_state *pipe_config)
2246{
2247 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
2248 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2249 enum pipe pipe = crtc->pipe;
2250 u32 dss1;
2251
2252 if (!HAS_MSO(i915))
2253 return;
2254
2255 dss1 = intel_de_read(i915, ICL_PIPE_DSS_CTL1(pipe));
2256
2257 pipe_config->splitter.enable = dss1 & SPLITTER_ENABLE;
2258 if (!pipe_config->splitter.enable)
2259 return;
2260
2261 if (drm_WARN_ON(&i915->drm, !(intel_ddi_splitter_pipe_mask(i915) & BIT(pipe)))) {
2262 pipe_config->splitter.enable = false;
2263 return;
2264 }
2265
2266 switch (dss1 & SPLITTER_CONFIGURATION_MASK) {
2267 default:
2268 drm_WARN(&i915->drm, true,
2269 "Invalid splitter configuration, dss1=0x%08x\n", dss1);
2270 fallthrough;
2271 case SPLITTER_CONFIGURATION_2_SEGMENT:
2272 pipe_config->splitter.link_count = 2;
2273 break;
2274 case SPLITTER_CONFIGURATION_4_SEGMENT:
2275 pipe_config->splitter.link_count = 4;
2276 break;
2277 }
2278
2279 pipe_config->splitter.pixel_overlap = REG_FIELD_GET(OVERLAP_PIXELS_MASK, dss1);
2280}
2281
2282static void intel_ddi_mso_configure(const struct intel_crtc_state *crtc_state)
2283{
2284 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2285 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2286 enum pipe pipe = crtc->pipe;
2287 u32 dss1 = 0;
2288
2289 if (!HAS_MSO(i915))
2290 return;
2291
2292 if (crtc_state->splitter.enable) {
2293 dss1 |= SPLITTER_ENABLE;
2294 dss1 |= OVERLAP_PIXELS(crtc_state->splitter.pixel_overlap);
2295 if (crtc_state->splitter.link_count == 2)
2296 dss1 |= SPLITTER_CONFIGURATION_2_SEGMENT;
2297 else
2298 dss1 |= SPLITTER_CONFIGURATION_4_SEGMENT;
2299 }
2300
2301 intel_de_rmw(i915, ICL_PIPE_DSS_CTL1(pipe),
2302 SPLITTER_ENABLE | SPLITTER_CONFIGURATION_MASK |
2303 OVERLAP_PIXELS_MASK, dss1);
2304}
2305
2306static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
2307 struct intel_encoder *encoder,
2308 const struct intel_crtc_state *crtc_state,
2309 const struct drm_connector_state *conn_state)
2310{
2311 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2312 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2313 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2314 bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2315
2316 intel_dp_set_link_params(intel_dp,
2317 crtc_state->port_clock,
2318 crtc_state->lane_count);
2319
2320 /*
2321 * We only configure what the register value will be here. Actual
2322 * enabling happens during link training farther down.
2323 */
2324 intel_ddi_init_dp_buf_reg(encoder, crtc_state);
2325
2326 /*
2327 * 1. Enable Power Wells
2328 *
2329 * This was handled at the beginning of intel_atomic_commit_tail(),
2330 * before we called down into this function.
2331 */
2332
2333 /* 2. Enable Panel Power if PPS is required */
2334 intel_pps_on(intel_dp);
2335
2336 /*
2337 * 3. For non-TBT Type-C ports, set FIA lane count
2338 * (DFLEXDPSP.DPX4TXLATC)
2339 *
2340 * This was done before tgl_ddi_pre_enable_dp by
2341 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
2342 */
2343
2344 /*
2345 * 4. Enable the port PLL.
2346 *
2347 * The PLL enabling itself was already done before this function by
2348 * hsw_crtc_enable()->intel_enable_shared_dpll(). We need only
2349 * configure the PLL to port mapping here.
2350 */
2351 intel_ddi_enable_clock(encoder, crtc_state);
2352
2353 /* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
2354 if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2355 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2356 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2357 dig_port->ddi_io_power_domain);
2358 }
2359
2360 /* 6. Program DP_MODE */
2361 icl_program_mg_dp_mode(dig_port, crtc_state);
2362
2363 /*
2364 * 7. The rest of the below are substeps under the bspec's "Enable and
2365 * Train Display Port" step. Note that steps that are specific to
2366 * MST will be handled by intel_mst_pre_enable_dp() before/after it
2367 * calls into this function. Also intel_mst_pre_enable_dp() only calls
2368 * us when active_mst_links==0, so any steps designated for "single
2369 * stream or multi-stream master transcoder" can just be performed
2370 * unconditionally here.
2371 */
2372
2373 /*
2374 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
2375 * Transcoder.
2376 */
2377 intel_ddi_enable_pipe_clock(encoder, crtc_state);
2378
2379 if (HAS_DP20(dev_priv))
2380 intel_ddi_config_transcoder_dp2(encoder, crtc_state);
2381
2382 /*
2383 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
2384 * Transport Select
2385 */
2386 intel_ddi_config_transcoder_func(encoder, crtc_state);
2387
2388 /*
2389 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
2390 * selected
2391 *
2392 * This will be handled by the intel_dp_start_link_train() farther
2393 * down this function.
2394 */
2395
2396 /* 7.e Configure voltage swing and related IO settings */
2397 encoder->set_signal_levels(encoder, crtc_state);
2398
2399 /*
2400 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
2401 * the used lanes of the DDI.
2402 */
2403 intel_ddi_power_up_lanes(encoder, crtc_state);
2404
2405 /*
2406 * 7.g Program CoG/MSO configuration bits in DSS_CTL1 if selected.
2407 */
2408 intel_ddi_mso_configure(crtc_state);
2409
2410 if (!is_mst)
2411 intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2412
2413 intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2414 intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
2415 /*
2416 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
2417 * in the FEC_CONFIGURATION register to 1 before initiating link
2418 * training
2419 */
2420 intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
2421
2422 intel_dp_check_frl_training(intel_dp);
2423 intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
2424
2425 /*
2426 * 7.i Follow DisplayPort specification training sequence (see notes for
2427 * failure handling)
2428 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
2429 * Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
2430 * (timeout after 800 us)
2431 */
2432 intel_dp_start_link_train(intel_dp, crtc_state);
2433
2434 /* 7.k Set DP_TP_CTL link training to Normal */
2435 if (!is_trans_port_sync_mode(crtc_state))
2436 intel_dp_stop_link_train(intel_dp, crtc_state);
2437
2438 /* 7.l Configure and enable FEC if needed */
2439 intel_ddi_enable_fec(encoder, crtc_state);
2440
2441 intel_dsc_dp_pps_write(encoder, crtc_state);
2442}
2443
2444static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
2445 struct intel_encoder *encoder,
2446 const struct intel_crtc_state *crtc_state,
2447 const struct drm_connector_state *conn_state)
2448{
2449 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2450 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2451 enum port port = encoder->port;
2452 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2453 bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2454
2455 if (DISPLAY_VER(dev_priv) < 11)
2456 drm_WARN_ON(&dev_priv->drm,
2457 is_mst && (port == PORT_A || port == PORT_E));
2458 else
2459 drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
2460
2461 intel_dp_set_link_params(intel_dp,
2462 crtc_state->port_clock,
2463 crtc_state->lane_count);
2464
2465 /*
2466 * We only configure what the register value will be here. Actual
2467 * enabling happens during link training farther down.
2468 */
2469 intel_ddi_init_dp_buf_reg(encoder, crtc_state);
2470
2471 intel_pps_on(intel_dp);
2472
2473 intel_ddi_enable_clock(encoder, crtc_state);
2474
2475 if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2476 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2477 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2478 dig_port->ddi_io_power_domain);
2479 }
2480
2481 icl_program_mg_dp_mode(dig_port, crtc_state);
2482
2483 if (has_buf_trans_select(dev_priv))
2484 hsw_prepare_dp_ddi_buffers(encoder, crtc_state);
2485
2486 encoder->set_signal_levels(encoder, crtc_state);
2487
2488 intel_ddi_power_up_lanes(encoder, crtc_state);
2489
2490 if (!is_mst)
2491 intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2492 intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2493 intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
2494 true);
2495 intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
2496 intel_dp_start_link_train(intel_dp, crtc_state);
2497 if ((port != PORT_A || DISPLAY_VER(dev_priv) >= 9) &&
2498 !is_trans_port_sync_mode(crtc_state))
2499 intel_dp_stop_link_train(intel_dp, crtc_state);
2500
2501 intel_ddi_enable_fec(encoder, crtc_state);
2502
2503 if (!is_mst)
2504 intel_ddi_enable_pipe_clock(encoder, crtc_state);
2505
2506 intel_dsc_dp_pps_write(encoder, crtc_state);
2507}
2508
2509static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
2510 struct intel_encoder *encoder,
2511 const struct intel_crtc_state *crtc_state,
2512 const struct drm_connector_state *conn_state)
2513{
2514 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2515
2516 if (DISPLAY_VER(dev_priv) >= 12)
2517 tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2518 else
2519 hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2520
2521 /* MST will call a setting of MSA after an allocating of Virtual Channel
2522 * from MST encoder pre_enable callback.
2523 */
2524 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2525 intel_ddi_set_dp_msa(crtc_state, conn_state);
2526}
2527
2528static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
2529 struct intel_encoder *encoder,
2530 const struct intel_crtc_state *crtc_state,
2531 const struct drm_connector_state *conn_state)
2532{
2533 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2534 struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2535 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2536
2537 intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
2538 intel_ddi_enable_clock(encoder, crtc_state);
2539
2540 drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2541 dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2542 dig_port->ddi_io_power_domain);
2543
2544 icl_program_mg_dp_mode(dig_port, crtc_state);
2545
2546 intel_ddi_enable_pipe_clock(encoder, crtc_state);
2547
2548 dig_port->set_infoframes(encoder,
2549 crtc_state->has_infoframe,
2550 crtc_state, conn_state);
2551}
2552
2553static void intel_ddi_pre_enable(struct intel_atomic_state *state,
2554 struct intel_encoder *encoder,
2555 const struct intel_crtc_state *crtc_state,
2556 const struct drm_connector_state *conn_state)
2557{
2558 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2559 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2560 enum pipe pipe = crtc->pipe;
2561
2562 /*
2563 * When called from DP MST code:
2564 * - conn_state will be NULL
2565 * - encoder will be the main encoder (ie. mst->primary)
2566 * - the main connector associated with this port
2567 * won't be active or linked to a crtc
2568 * - crtc_state will be the state of the first stream to
2569 * be activated on this port, and it may not be the same
2570 * stream that will be deactivated last, but each stream
2571 * should have a state that is identical when it comes to
2572 * the DP link parameteres
2573 */
2574
2575 drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
2576
2577 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2578
2579 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2580 intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
2581 conn_state);
2582 } else {
2583 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2584
2585 intel_ddi_pre_enable_dp(state, encoder, crtc_state,
2586 conn_state);
2587
2588 /* FIXME precompute everything properly */
2589 /* FIXME how do we turn infoframes off again? */
2590 if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
2591 dig_port->set_infoframes(encoder,
2592 crtc_state->has_infoframe,
2593 crtc_state, conn_state);
2594 }
2595}
2596
2597static void intel_disable_ddi_buf(struct intel_encoder *encoder,
2598 const struct intel_crtc_state *crtc_state)
2599{
2600 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2601 enum port port = encoder->port;
2602 bool wait = false;
2603 u32 val;
2604
2605 val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
2606 if (val & DDI_BUF_CTL_ENABLE) {
2607 val &= ~DDI_BUF_CTL_ENABLE;
2608 intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
2609 wait = true;
2610 }
2611
2612 if (intel_crtc_has_dp_encoder(crtc_state)) {
2613 val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2614 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2615 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2616 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2617 }
2618
2619 /* Disable FEC in DP Sink */
2620 intel_ddi_disable_fec_state(encoder, crtc_state);
2621
2622 if (wait)
2623 intel_wait_ddi_buf_idle(dev_priv, port);
2624}
2625
2626static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
2627 struct intel_encoder *encoder,
2628 const struct intel_crtc_state *old_crtc_state,
2629 const struct drm_connector_state *old_conn_state)
2630{
2631 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2632 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2633 struct intel_dp *intel_dp = &dig_port->dp;
2634 bool is_mst = intel_crtc_has_type(old_crtc_state,
2635 INTEL_OUTPUT_DP_MST);
2636
2637 if (!is_mst)
2638 intel_dp_set_infoframes(encoder, false,
2639 old_crtc_state, old_conn_state);
2640
2641 /*
2642 * Power down sink before disabling the port, otherwise we end
2643 * up getting interrupts from the sink on detecting link loss.
2644 */
2645 intel_dp_set_power(intel_dp, DP_SET_POWER_D3);
2646
2647 if (DISPLAY_VER(dev_priv) >= 12) {
2648 if (is_mst) {
2649 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
2650 u32 val;
2651
2652 val = intel_de_read(dev_priv,
2653 TRANS_DDI_FUNC_CTL(cpu_transcoder));
2654 val &= ~(TGL_TRANS_DDI_PORT_MASK |
2655 TRANS_DDI_MODE_SELECT_MASK);
2656 intel_de_write(dev_priv,
2657 TRANS_DDI_FUNC_CTL(cpu_transcoder),
2658 val);
2659 }
2660 } else {
2661 if (!is_mst)
2662 intel_ddi_disable_pipe_clock(old_crtc_state);
2663 }
2664
2665 intel_disable_ddi_buf(encoder, old_crtc_state);
2666
2667 /*
2668 * From TGL spec: "If single stream or multi-stream master transcoder:
2669 * Configure Transcoder Clock select to direct no clock to the
2670 * transcoder"
2671 */
2672 if (DISPLAY_VER(dev_priv) >= 12)
2673 intel_ddi_disable_pipe_clock(old_crtc_state);
2674
2675 intel_pps_vdd_on(intel_dp);
2676 intel_pps_off(intel_dp);
2677
2678 if (!intel_tc_port_in_tbt_alt_mode(dig_port))
2679 intel_display_power_put(dev_priv,
2680 dig_port->ddi_io_power_domain,
2681 fetch_and_zero(&dig_port->ddi_io_wakeref));
2682
2683 intel_ddi_disable_clock(encoder);
2684}
2685
2686static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
2687 struct intel_encoder *encoder,
2688 const struct intel_crtc_state *old_crtc_state,
2689 const struct drm_connector_state *old_conn_state)
2690{
2691 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2692 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2693 struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2694
2695 dig_port->set_infoframes(encoder, false,
2696 old_crtc_state, old_conn_state);
2697
2698 if (DISPLAY_VER(dev_priv) < 12)
2699 intel_ddi_disable_pipe_clock(old_crtc_state);
2700
2701 intel_disable_ddi_buf(encoder, old_crtc_state);
2702
2703 if (DISPLAY_VER(dev_priv) >= 12)
2704 intel_ddi_disable_pipe_clock(old_crtc_state);
2705
2706 intel_display_power_put(dev_priv,
2707 dig_port->ddi_io_power_domain,
2708 fetch_and_zero(&dig_port->ddi_io_wakeref));
2709
2710 intel_ddi_disable_clock(encoder);
2711
2712 intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
2713}
2714
2715static void intel_ddi_post_disable(struct intel_atomic_state *state,
2716 struct intel_encoder *encoder,
2717 const struct intel_crtc_state *old_crtc_state,
2718 const struct drm_connector_state *old_conn_state)
2719{
2720 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2721 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2722 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2723 bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
2724 struct intel_crtc *slave_crtc;
2725
2726 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST)) {
2727 intel_crtc_vblank_off(old_crtc_state);
2728
2729 intel_disable_transcoder(old_crtc_state);
2730
2731 intel_vrr_disable(old_crtc_state);
2732
2733 intel_ddi_disable_transcoder_func(old_crtc_state);
2734
2735 intel_dsc_disable(old_crtc_state);
2736
2737 if (DISPLAY_VER(dev_priv) >= 9)
2738 skl_scaler_disable(old_crtc_state);
2739 else
2740 ilk_pfit_disable(old_crtc_state);
2741 }
2742
2743 for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, slave_crtc,
2744 intel_crtc_bigjoiner_slave_pipes(old_crtc_state)) {
2745 const struct intel_crtc_state *old_slave_crtc_state =
2746 intel_atomic_get_old_crtc_state(state, slave_crtc);
2747
2748 intel_crtc_vblank_off(old_slave_crtc_state);
2749
2750 intel_dsc_disable(old_slave_crtc_state);
2751 skl_scaler_disable(old_slave_crtc_state);
2752 }
2753
2754 /*
2755 * When called from DP MST code:
2756 * - old_conn_state will be NULL
2757 * - encoder will be the main encoder (ie. mst->primary)
2758 * - the main connector associated with this port
2759 * won't be active or linked to a crtc
2760 * - old_crtc_state will be the state of the last stream to
2761 * be deactivated on this port, and it may not be the same
2762 * stream that was activated last, but each stream
2763 * should have a state that is identical when it comes to
2764 * the DP link parameteres
2765 */
2766
2767 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
2768 intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
2769 old_conn_state);
2770 else
2771 intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
2772 old_conn_state);
2773
2774 main_link_aux_power_domain_put(dig_port, old_crtc_state);
2775
2776 if (is_tc_port)
2777 intel_tc_port_put_link(dig_port);
2778}
2779
2780static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
2781 struct intel_encoder *encoder,
2782 const struct intel_crtc_state *crtc_state)
2783{
2784 const struct drm_connector_state *conn_state;
2785 struct drm_connector *conn;
2786 int i;
2787
2788 if (!crtc_state->sync_mode_slaves_mask)
2789 return;
2790
2791 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2792 struct intel_encoder *slave_encoder =
2793 to_intel_encoder(conn_state->best_encoder);
2794 struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
2795 const struct intel_crtc_state *slave_crtc_state;
2796
2797 if (!slave_crtc)
2798 continue;
2799
2800 slave_crtc_state =
2801 intel_atomic_get_new_crtc_state(state, slave_crtc);
2802
2803 if (slave_crtc_state->master_transcoder !=
2804 crtc_state->cpu_transcoder)
2805 continue;
2806
2807 intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder),
2808 slave_crtc_state);
2809 }
2810
2811 usleep_range(200, 400);
2812
2813 intel_dp_stop_link_train(enc_to_intel_dp(encoder),
2814 crtc_state);
2815}
2816
2817static void intel_enable_ddi_dp(struct intel_atomic_state *state,
2818 struct intel_encoder *encoder,
2819 const struct intel_crtc_state *crtc_state,
2820 const struct drm_connector_state *conn_state)
2821{
2822 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2823 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2824 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2825 enum port port = encoder->port;
2826
2827 if (port == PORT_A && DISPLAY_VER(dev_priv) < 9)
2828 intel_dp_stop_link_train(intel_dp, crtc_state);
2829
2830 drm_connector_update_privacy_screen(conn_state);
2831 intel_edp_backlight_on(crtc_state, conn_state);
2832
2833 if (!dig_port->lspcon.active || dig_port->dp.has_hdmi_sink)
2834 intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
2835
2836 intel_audio_codec_enable(encoder, crtc_state, conn_state);
2837
2838 trans_port_sync_stop_link_train(state, encoder, crtc_state);
2839}
2840
2841static i915_reg_t
2842gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
2843 enum port port)
2844{
2845 static const enum transcoder trans[] = {
2846 [PORT_A] = TRANSCODER_EDP,
2847 [PORT_B] = TRANSCODER_A,
2848 [PORT_C] = TRANSCODER_B,
2849 [PORT_D] = TRANSCODER_C,
2850 [PORT_E] = TRANSCODER_A,
2851 };
2852
2853 drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) < 9);
2854
2855 if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
2856 port = PORT_A;
2857
2858 return CHICKEN_TRANS(trans[port]);
2859}
2860
2861static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
2862 struct intel_encoder *encoder,
2863 const struct intel_crtc_state *crtc_state,
2864 const struct drm_connector_state *conn_state)
2865{
2866 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2867 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2868 struct drm_connector *connector = conn_state->connector;
2869 enum port port = encoder->port;
2870 enum phy phy = intel_port_to_phy(dev_priv, port);
2871 u32 buf_ctl;
2872
2873 if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
2874 crtc_state->hdmi_high_tmds_clock_ratio,
2875 crtc_state->hdmi_scrambling))
2876 drm_dbg_kms(&dev_priv->drm,
2877 "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
2878 connector->base.id, connector->name);
2879
2880 if (has_buf_trans_select(dev_priv))
2881 hsw_prepare_hdmi_ddi_buffers(encoder, crtc_state);
2882
2883 encoder->set_signal_levels(encoder, crtc_state);
2884
2885 /* Display WA #1143: skl,kbl,cfl */
2886 if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv)) {
2887 /*
2888 * For some reason these chicken bits have been
2889 * stuffed into a transcoder register, event though
2890 * the bits affect a specific DDI port rather than
2891 * a specific transcoder.
2892 */
2893 i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
2894 u32 val;
2895
2896 val = intel_de_read(dev_priv, reg);
2897
2898 if (port == PORT_E)
2899 val |= DDIE_TRAINING_OVERRIDE_ENABLE |
2900 DDIE_TRAINING_OVERRIDE_VALUE;
2901 else
2902 val |= DDI_TRAINING_OVERRIDE_ENABLE |
2903 DDI_TRAINING_OVERRIDE_VALUE;
2904
2905 intel_de_write(dev_priv, reg, val);
2906 intel_de_posting_read(dev_priv, reg);
2907
2908 udelay(1);
2909
2910 if (port == PORT_E)
2911 val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
2912 DDIE_TRAINING_OVERRIDE_VALUE);
2913 else
2914 val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
2915 DDI_TRAINING_OVERRIDE_VALUE);
2916
2917 intel_de_write(dev_priv, reg, val);
2918 }
2919
2920 intel_ddi_power_up_lanes(encoder, crtc_state);
2921
2922 /* In HDMI/DVI mode, the port width, and swing/emphasis values
2923 * are ignored so nothing special needs to be done besides
2924 * enabling the port.
2925 *
2926 * On ADL_P the PHY link rate and lane count must be programmed but
2927 * these are both 0 for HDMI.
2928 */
2929 buf_ctl = dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE;
2930 if (IS_ALDERLAKE_P(dev_priv) && intel_phy_is_tc(dev_priv, phy)) {
2931 drm_WARN_ON(&dev_priv->drm, !intel_tc_port_in_legacy_mode(dig_port));
2932 buf_ctl |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
2933 }
2934 intel_de_write(dev_priv, DDI_BUF_CTL(port), buf_ctl);
2935
2936 intel_audio_codec_enable(encoder, crtc_state, conn_state);
2937}
2938
2939static void intel_enable_ddi(struct intel_atomic_state *state,
2940 struct intel_encoder *encoder,
2941 const struct intel_crtc_state *crtc_state,
2942 const struct drm_connector_state *conn_state)
2943{
2944 drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
2945
2946 if (!intel_crtc_is_bigjoiner_slave(crtc_state))
2947 intel_ddi_enable_transcoder_func(encoder, crtc_state);
2948
2949 intel_vrr_enable(encoder, crtc_state);
2950
2951 intel_enable_transcoder(crtc_state);
2952
2953 intel_crtc_vblank_on(crtc_state);
2954
2955 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
2956 intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
2957 else
2958 intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
2959
2960 /* Enable hdcp if it's desired */
2961 if (conn_state->content_protection ==
2962 DRM_MODE_CONTENT_PROTECTION_DESIRED)
2963 intel_hdcp_enable(to_intel_connector(conn_state->connector),
2964 crtc_state,
2965 (u8)conn_state->hdcp_content_type);
2966}
2967
2968static void intel_disable_ddi_dp(struct intel_atomic_state *state,
2969 struct intel_encoder *encoder,
2970 const struct intel_crtc_state *old_crtc_state,
2971 const struct drm_connector_state *old_conn_state)
2972{
2973 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2974
2975 intel_dp->link_trained = false;
2976
2977 intel_audio_codec_disable(encoder, old_crtc_state, old_conn_state);
2978
2979 intel_psr_disable(intel_dp, old_crtc_state);
2980 intel_edp_backlight_off(old_conn_state);
2981 /* Disable the decompression in DP Sink */
2982 intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
2983 false);
2984 /* Disable Ignore_MSA bit in DP Sink */
2985 intel_dp_sink_set_msa_timing_par_ignore_state(intel_dp, old_crtc_state,
2986 false);
2987}
2988
2989static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
2990 struct intel_encoder *encoder,
2991 const struct intel_crtc_state *old_crtc_state,
2992 const struct drm_connector_state *old_conn_state)
2993{
2994 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2995 struct drm_connector *connector = old_conn_state->connector;
2996
2997 intel_audio_codec_disable(encoder, old_crtc_state, old_conn_state);
2998
2999 if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3000 false, false))
3001 drm_dbg_kms(&i915->drm,
3002 "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3003 connector->base.id, connector->name);
3004}
3005
3006static void intel_disable_ddi(struct intel_atomic_state *state,
3007 struct intel_encoder *encoder,
3008 const struct intel_crtc_state *old_crtc_state,
3009 const struct drm_connector_state *old_conn_state)
3010{
3011 intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3012
3013 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3014 intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3015 old_conn_state);
3016 else
3017 intel_disable_ddi_dp(state, encoder, old_crtc_state,
3018 old_conn_state);
3019}
3020
3021static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3022 struct intel_encoder *encoder,
3023 const struct intel_crtc_state *crtc_state,
3024 const struct drm_connector_state *conn_state)
3025{
3026 intel_ddi_set_dp_msa(crtc_state, conn_state);
3027
3028 intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3029
3030 intel_backlight_update(state, encoder, crtc_state, conn_state);
3031 drm_connector_update_privacy_screen(conn_state);
3032}
3033
3034void intel_ddi_update_pipe(struct intel_atomic_state *state,
3035 struct intel_encoder *encoder,
3036 const struct intel_crtc_state *crtc_state,
3037 const struct drm_connector_state *conn_state)
3038{
3039
3040 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
3041 !intel_encoder_is_mst(encoder))
3042 intel_ddi_update_pipe_dp(state, encoder, crtc_state,
3043 conn_state);
3044
3045 intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
3046}
3047
3048static void
3049intel_ddi_update_prepare(struct intel_atomic_state *state,
3050 struct intel_encoder *encoder,
3051 struct intel_crtc *crtc)
3052{
3053 struct drm_i915_private *i915 = to_i915(state->base.dev);
3054 struct intel_crtc_state *crtc_state =
3055 crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
3056 int required_lanes = crtc_state ? crtc_state->lane_count : 1;
3057
3058 drm_WARN_ON(state->base.dev, crtc && crtc->active);
3059
3060 intel_tc_port_get_link(enc_to_dig_port(encoder),
3061 required_lanes);
3062 if (crtc_state && crtc_state->hw.active) {
3063 struct intel_crtc *slave_crtc;
3064
3065 intel_update_active_dpll(state, crtc, encoder);
3066
3067 for_each_intel_crtc_in_pipe_mask(&i915->drm, slave_crtc,
3068 intel_crtc_bigjoiner_slave_pipes(crtc_state))
3069 intel_update_active_dpll(state, slave_crtc, encoder);
3070 }
3071}
3072
3073static void
3074intel_ddi_update_complete(struct intel_atomic_state *state,
3075 struct intel_encoder *encoder,
3076 struct intel_crtc *crtc)
3077{
3078 intel_tc_port_put_link(enc_to_dig_port(encoder));
3079}
3080
3081static void
3082intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
3083 struct intel_encoder *encoder,
3084 const struct intel_crtc_state *crtc_state,
3085 const struct drm_connector_state *conn_state)
3086{
3087 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3088 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3089 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3090 bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3091
3092 if (is_tc_port)
3093 intel_tc_port_get_link(dig_port, crtc_state->lane_count);
3094
3095 main_link_aux_power_domain_get(dig_port, crtc_state);
3096
3097 if (is_tc_port && !intel_tc_port_in_tbt_alt_mode(dig_port))
3098 /*
3099 * Program the lane count for static/dynamic connections on
3100 * Type-C ports. Skip this step for TBT.
3101 */
3102 intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
3103 else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3104 bxt_ddi_phy_set_lane_optim_mask(encoder,
3105 crtc_state->lane_lat_optim_mask);
3106}
3107
3108static void adlp_tbt_to_dp_alt_switch_wa(struct intel_encoder *encoder)
3109{
3110 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3111 enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
3112 int ln;
3113
3114 for (ln = 0; ln < 2; ln++)
3115 intel_dkl_phy_rmw(i915, DKL_PCS_DW5(tc_port, ln), DKL_PCS_DW5_CORE_SOFTRESET, 0);
3116}
3117
3118static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp,
3119 const struct intel_crtc_state *crtc_state)
3120{
3121 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3122 struct intel_encoder *encoder = &dig_port->base;
3123 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3124 enum port port = encoder->port;
3125 u32 dp_tp_ctl, ddi_buf_ctl;
3126 bool wait = false;
3127
3128 dp_tp_ctl = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3129
3130 if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
3131 ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
3132 if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
3133 intel_de_write(dev_priv, DDI_BUF_CTL(port),
3134 ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
3135 wait = true;
3136 }
3137
3138 dp_tp_ctl &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
3139 dp_tp_ctl |= DP_TP_CTL_LINK_TRAIN_PAT1;
3140 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3141 intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3142
3143 if (wait)
3144 intel_wait_ddi_buf_idle(dev_priv, port);
3145 }
3146
3147 dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
3148 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
3149 dp_tp_ctl |= DP_TP_CTL_MODE_MST;
3150 } else {
3151 dp_tp_ctl |= DP_TP_CTL_MODE_SST;
3152 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
3153 dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3154 }
3155 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3156 intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3157
3158 if (IS_ALDERLAKE_P(dev_priv) &&
3159 (intel_tc_port_in_dp_alt_mode(dig_port) || intel_tc_port_in_legacy_mode(dig_port)))
3160 adlp_tbt_to_dp_alt_switch_wa(encoder);
3161
3162 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3163 intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
3164 intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
3165
3166 intel_wait_ddi_buf_active(dev_priv, port);
3167}
3168
3169static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
3170 const struct intel_crtc_state *crtc_state,
3171 u8 dp_train_pat)
3172{
3173 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3174 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3175 u32 temp;
3176
3177 temp = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3178
3179 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
3180 switch (intel_dp_training_pattern_symbol(dp_train_pat)) {
3181 case DP_TRAINING_PATTERN_DISABLE:
3182 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
3183 break;
3184 case DP_TRAINING_PATTERN_1:
3185 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
3186 break;
3187 case DP_TRAINING_PATTERN_2:
3188 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
3189 break;
3190 case DP_TRAINING_PATTERN_3:
3191 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
3192 break;
3193 case DP_TRAINING_PATTERN_4:
3194 temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
3195 break;
3196 }
3197
3198 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), temp);
3199}
3200
3201static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp,
3202 const struct intel_crtc_state *crtc_state)
3203{
3204 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3205 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3206 enum port port = encoder->port;
3207 u32 val;
3208
3209 val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3210 val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
3211 val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3212 intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
3213
3214 /*
3215 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
3216 * reason we need to set idle transmission mode is to work around a HW
3217 * issue where we enable the pipe while not in idle link-training mode.
3218 * In this case there is requirement to wait for a minimum number of
3219 * idle patterns to be sent.
3220 */
3221 if (port == PORT_A && DISPLAY_VER(dev_priv) < 12)
3222 return;
3223
3224 if (intel_de_wait_for_set(dev_priv,
3225 dp_tp_status_reg(encoder, crtc_state),
3226 DP_TP_STATUS_IDLE_DONE, 1))
3227 drm_err(&dev_priv->drm,
3228 "Timed out waiting for DP idle patterns\n");
3229}
3230
3231static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3232 enum transcoder cpu_transcoder)
3233{
3234 if (cpu_transcoder == TRANSCODER_EDP)
3235 return false;
3236
3237 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO_MMIO))
3238 return false;
3239
3240 return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
3241 AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3242}
3243
3244void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
3245 struct intel_crtc_state *crtc_state)
3246{
3247 if (DISPLAY_VER(dev_priv) >= 12 && crtc_state->port_clock > 594000)
3248 crtc_state->min_voltage_level = 2;
3249 else if (IS_JSL_EHL(dev_priv) && crtc_state->port_clock > 594000)
3250 crtc_state->min_voltage_level = 3;
3251 else if (DISPLAY_VER(dev_priv) >= 11 && crtc_state->port_clock > 594000)
3252 crtc_state->min_voltage_level = 1;
3253}
3254
3255static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
3256 enum transcoder cpu_transcoder)
3257{
3258 u32 master_select;
3259
3260 if (DISPLAY_VER(dev_priv) >= 11) {
3261 u32 ctl2 = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL2(cpu_transcoder));
3262
3263 if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
3264 return INVALID_TRANSCODER;
3265
3266 master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
3267 } else {
3268 u32 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3269
3270 if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
3271 return INVALID_TRANSCODER;
3272
3273 master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
3274 }
3275
3276 if (master_select == 0)
3277 return TRANSCODER_EDP;
3278 else
3279 return master_select - 1;
3280}
3281
3282static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
3283{
3284 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3285 u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
3286 BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
3287 enum transcoder cpu_transcoder;
3288
3289 crtc_state->master_transcoder =
3290 bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
3291
3292 for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
3293 enum intel_display_power_domain power_domain;
3294 intel_wakeref_t trans_wakeref;
3295
3296 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
3297 trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
3298 power_domain);
3299
3300 if (!trans_wakeref)
3301 continue;
3302
3303 if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
3304 crtc_state->cpu_transcoder)
3305 crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
3306
3307 intel_display_power_put(dev_priv, power_domain, trans_wakeref);
3308 }
3309
3310 drm_WARN_ON(&dev_priv->drm,
3311 crtc_state->master_transcoder != INVALID_TRANSCODER &&
3312 crtc_state->sync_mode_slaves_mask);
3313}
3314
3315static void intel_ddi_read_func_ctl(struct intel_encoder *encoder,
3316 struct intel_crtc_state *pipe_config)
3317{
3318 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3319 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3320 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3321 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3322 u32 temp, flags = 0;
3323
3324 temp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3325 if (temp & TRANS_DDI_PHSYNC)
3326 flags |= DRM_MODE_FLAG_PHSYNC;
3327 else
3328 flags |= DRM_MODE_FLAG_NHSYNC;
3329 if (temp & TRANS_DDI_PVSYNC)
3330 flags |= DRM_MODE_FLAG_PVSYNC;
3331 else
3332 flags |= DRM_MODE_FLAG_NVSYNC;
3333
3334 pipe_config->hw.adjusted_mode.flags |= flags;
3335
3336 switch (temp & TRANS_DDI_BPC_MASK) {
3337 case TRANS_DDI_BPC_6:
3338 pipe_config->pipe_bpp = 18;
3339 break;
3340 case TRANS_DDI_BPC_8:
3341 pipe_config->pipe_bpp = 24;
3342 break;
3343 case TRANS_DDI_BPC_10:
3344 pipe_config->pipe_bpp = 30;
3345 break;
3346 case TRANS_DDI_BPC_12:
3347 pipe_config->pipe_bpp = 36;
3348 break;
3349 default:
3350 break;
3351 }
3352
3353 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
3354 case TRANS_DDI_MODE_SELECT_HDMI:
3355 pipe_config->has_hdmi_sink = true;
3356
3357 pipe_config->infoframes.enable |=
3358 intel_hdmi_infoframes_enabled(encoder, pipe_config);
3359
3360 if (pipe_config->infoframes.enable)
3361 pipe_config->has_infoframe = true;
3362
3363 if (temp & TRANS_DDI_HDMI_SCRAMBLING)
3364 pipe_config->hdmi_scrambling = true;
3365 if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
3366 pipe_config->hdmi_high_tmds_clock_ratio = true;
3367 fallthrough;
3368 case TRANS_DDI_MODE_SELECT_DVI:
3369 pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
3370 pipe_config->lane_count = 4;
3371 break;
3372 case TRANS_DDI_MODE_SELECT_DP_SST:
3373 if (encoder->type == INTEL_OUTPUT_EDP)
3374 pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3375 else
3376 pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3377 pipe_config->lane_count =
3378 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3379
3380 intel_cpu_transcoder_get_m1_n1(crtc, cpu_transcoder,
3381 &pipe_config->dp_m_n);
3382 intel_cpu_transcoder_get_m2_n2(crtc, cpu_transcoder,
3383 &pipe_config->dp_m2_n2);
3384
3385 if (DISPLAY_VER(dev_priv) >= 11) {
3386 i915_reg_t dp_tp_ctl = dp_tp_ctl_reg(encoder, pipe_config);
3387
3388 pipe_config->fec_enable =
3389 intel_de_read(dev_priv, dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
3390
3391 drm_dbg_kms(&dev_priv->drm,
3392 "[ENCODER:%d:%s] Fec status: %u\n",
3393 encoder->base.base.id, encoder->base.name,
3394 pipe_config->fec_enable);
3395 }
3396
3397 if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
3398 pipe_config->infoframes.enable |=
3399 intel_lspcon_infoframes_enabled(encoder, pipe_config);
3400 else
3401 pipe_config->infoframes.enable |=
3402 intel_hdmi_infoframes_enabled(encoder, pipe_config);
3403 break;
3404 case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
3405 if (!HAS_DP20(dev_priv)) {
3406 /* FDI */
3407 pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
3408 break;
3409 }
3410 fallthrough; /* 128b/132b */
3411 case TRANS_DDI_MODE_SELECT_DP_MST:
3412 pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
3413 pipe_config->lane_count =
3414 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3415
3416 if (DISPLAY_VER(dev_priv) >= 12)
3417 pipe_config->mst_master_transcoder =
3418 REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
3419
3420 intel_cpu_transcoder_get_m1_n1(crtc, cpu_transcoder,
3421 &pipe_config->dp_m_n);
3422
3423 pipe_config->infoframes.enable |=
3424 intel_hdmi_infoframes_enabled(encoder, pipe_config);
3425 break;
3426 default:
3427 break;
3428 }
3429}
3430
3431static void intel_ddi_get_config(struct intel_encoder *encoder,
3432 struct intel_crtc_state *pipe_config)
3433{
3434 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3435 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3436
3437 /* XXX: DSI transcoder paranoia */
3438 if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
3439 return;
3440
3441 intel_ddi_read_func_ctl(encoder, pipe_config);
3442
3443 intel_ddi_mso_get_config(encoder, pipe_config);
3444
3445 pipe_config->has_audio =
3446 intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
3447
3448 if (encoder->type == INTEL_OUTPUT_EDP)
3449 intel_edp_fixup_vbt_bpp(encoder, pipe_config->pipe_bpp);
3450
3451 ddi_dotclock_get(pipe_config);
3452
3453 if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3454 pipe_config->lane_lat_optim_mask =
3455 bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
3456
3457 intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3458
3459 intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
3460
3461 intel_read_infoframe(encoder, pipe_config,
3462 HDMI_INFOFRAME_TYPE_AVI,
3463 &pipe_config->infoframes.avi);
3464 intel_read_infoframe(encoder, pipe_config,
3465 HDMI_INFOFRAME_TYPE_SPD,
3466 &pipe_config->infoframes.spd);
3467 intel_read_infoframe(encoder, pipe_config,
3468 HDMI_INFOFRAME_TYPE_VENDOR,
3469 &pipe_config->infoframes.hdmi);
3470 intel_read_infoframe(encoder, pipe_config,
3471 HDMI_INFOFRAME_TYPE_DRM,
3472 &pipe_config->infoframes.drm);
3473
3474 if (DISPLAY_VER(dev_priv) >= 8)
3475 bdw_get_trans_port_sync_config(pipe_config);
3476
3477 intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
3478 intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
3479
3480 intel_psr_get_config(encoder, pipe_config);
3481}
3482
3483void intel_ddi_get_clock(struct intel_encoder *encoder,
3484 struct intel_crtc_state *crtc_state,
3485 struct intel_shared_dpll *pll)
3486{
3487 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3488 enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3489 struct icl_port_dpll *port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3490 bool pll_active;
3491
3492 if (drm_WARN_ON(&i915->drm, !pll))
3493 return;
3494
3495 port_dpll->pll = pll;
3496 pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3497 drm_WARN_ON(&i915->drm, !pll_active);
3498
3499 icl_set_active_port_dpll(crtc_state, port_dpll_id);
3500
3501 crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3502 &crtc_state->dpll_hw_state);
3503}
3504
3505static void dg2_ddi_get_config(struct intel_encoder *encoder,
3506 struct intel_crtc_state *crtc_state)
3507{
3508 intel_mpllb_readout_hw_state(encoder, &crtc_state->mpllb_state);
3509 crtc_state->port_clock = intel_mpllb_calc_port_clock(encoder, &crtc_state->mpllb_state);
3510
3511 intel_ddi_get_config(encoder, crtc_state);
3512}
3513
3514static void adls_ddi_get_config(struct intel_encoder *encoder,
3515 struct intel_crtc_state *crtc_state)
3516{
3517 intel_ddi_get_clock(encoder, crtc_state, adls_ddi_get_pll(encoder));
3518 intel_ddi_get_config(encoder, crtc_state);
3519}
3520
3521static void rkl_ddi_get_config(struct intel_encoder *encoder,
3522 struct intel_crtc_state *crtc_state)
3523{
3524 intel_ddi_get_clock(encoder, crtc_state, rkl_ddi_get_pll(encoder));
3525 intel_ddi_get_config(encoder, crtc_state);
3526}
3527
3528static void dg1_ddi_get_config(struct intel_encoder *encoder,
3529 struct intel_crtc_state *crtc_state)
3530{
3531 intel_ddi_get_clock(encoder, crtc_state, dg1_ddi_get_pll(encoder));
3532 intel_ddi_get_config(encoder, crtc_state);
3533}
3534
3535static void icl_ddi_combo_get_config(struct intel_encoder *encoder,
3536 struct intel_crtc_state *crtc_state)
3537{
3538 intel_ddi_get_clock(encoder, crtc_state, icl_ddi_combo_get_pll(encoder));
3539 intel_ddi_get_config(encoder, crtc_state);
3540}
3541
3542static void icl_ddi_tc_get_clock(struct intel_encoder *encoder,
3543 struct intel_crtc_state *crtc_state,
3544 struct intel_shared_dpll *pll)
3545{
3546 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3547 enum icl_port_dpll_id port_dpll_id;
3548 struct icl_port_dpll *port_dpll;
3549 bool pll_active;
3550
3551 if (drm_WARN_ON(&i915->drm, !pll))
3552 return;
3553
3554 if (pll->info->id == DPLL_ID_ICL_TBTPLL)
3555 port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3556 else
3557 port_dpll_id = ICL_PORT_DPLL_MG_PHY;
3558
3559 port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3560
3561 port_dpll->pll = pll;
3562 pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3563 drm_WARN_ON(&i915->drm, !pll_active);
3564
3565 icl_set_active_port_dpll(crtc_state, port_dpll_id);
3566
3567 if (crtc_state->shared_dpll->info->id == DPLL_ID_ICL_TBTPLL)
3568 crtc_state->port_clock = icl_calc_tbt_pll_link(i915, encoder->port);
3569 else
3570 crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3571 &crtc_state->dpll_hw_state);
3572}
3573
3574static void icl_ddi_tc_get_config(struct intel_encoder *encoder,
3575 struct intel_crtc_state *crtc_state)
3576{
3577 icl_ddi_tc_get_clock(encoder, crtc_state, icl_ddi_tc_get_pll(encoder));
3578 intel_ddi_get_config(encoder, crtc_state);
3579}
3580
3581static void bxt_ddi_get_config(struct intel_encoder *encoder,
3582 struct intel_crtc_state *crtc_state)
3583{
3584 intel_ddi_get_clock(encoder, crtc_state, bxt_ddi_get_pll(encoder));
3585 intel_ddi_get_config(encoder, crtc_state);
3586}
3587
3588static void skl_ddi_get_config(struct intel_encoder *encoder,
3589 struct intel_crtc_state *crtc_state)
3590{
3591 intel_ddi_get_clock(encoder, crtc_state, skl_ddi_get_pll(encoder));
3592 intel_ddi_get_config(encoder, crtc_state);
3593}
3594
3595void hsw_ddi_get_config(struct intel_encoder *encoder,
3596 struct intel_crtc_state *crtc_state)
3597{
3598 intel_ddi_get_clock(encoder, crtc_state, hsw_ddi_get_pll(encoder));
3599 intel_ddi_get_config(encoder, crtc_state);
3600}
3601
3602static void intel_ddi_sync_state(struct intel_encoder *encoder,
3603 const struct intel_crtc_state *crtc_state)
3604{
3605 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3606 enum phy phy = intel_port_to_phy(i915, encoder->port);
3607
3608 if (intel_phy_is_tc(i915, phy))
3609 intel_tc_port_sanitize_mode(enc_to_dig_port(encoder));
3610
3611 if (crtc_state && intel_crtc_has_dp_encoder(crtc_state))
3612 intel_dp_sync_state(encoder, crtc_state);
3613}
3614
3615static bool intel_ddi_initial_fastset_check(struct intel_encoder *encoder,
3616 struct intel_crtc_state *crtc_state)
3617{
3618 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3619 enum phy phy = intel_port_to_phy(i915, encoder->port);
3620 bool fastset = true;
3621
3622 if (intel_phy_is_tc(i915, phy)) {
3623 drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] Forcing full modeset to compute TC port DPLLs\n",
3624 encoder->base.base.id, encoder->base.name);
3625 crtc_state->uapi.mode_changed = true;
3626 fastset = false;
3627 }
3628
3629 if (intel_crtc_has_dp_encoder(crtc_state) &&
3630 !intel_dp_initial_fastset_check(encoder, crtc_state))
3631 fastset = false;
3632
3633 return fastset;
3634}
3635
3636static enum intel_output_type
3637intel_ddi_compute_output_type(struct intel_encoder *encoder,
3638 struct intel_crtc_state *crtc_state,
3639 struct drm_connector_state *conn_state)
3640{
3641 switch (conn_state->connector->connector_type) {
3642 case DRM_MODE_CONNECTOR_HDMIA:
3643 return INTEL_OUTPUT_HDMI;
3644 case DRM_MODE_CONNECTOR_eDP:
3645 return INTEL_OUTPUT_EDP;
3646 case DRM_MODE_CONNECTOR_DisplayPort:
3647 return INTEL_OUTPUT_DP;
3648 default:
3649 MISSING_CASE(conn_state->connector->connector_type);
3650 return INTEL_OUTPUT_UNUSED;
3651 }
3652}
3653
3654static int intel_ddi_compute_config(struct intel_encoder *encoder,
3655 struct intel_crtc_state *pipe_config,
3656 struct drm_connector_state *conn_state)
3657{
3658 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3659 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3660 enum port port = encoder->port;
3661 int ret;
3662
3663 if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
3664 pipe_config->cpu_transcoder = TRANSCODER_EDP;
3665
3666 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
3667 ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
3668 } else {
3669 ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
3670 }
3671
3672 if (ret)
3673 return ret;
3674
3675 if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
3676 pipe_config->cpu_transcoder == TRANSCODER_EDP)
3677 pipe_config->pch_pfit.force_thru =
3678 pipe_config->pch_pfit.enabled ||
3679 pipe_config->crc_enabled;
3680
3681 if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3682 pipe_config->lane_lat_optim_mask =
3683 bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
3684
3685 intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3686
3687 return 0;
3688}
3689
3690static bool mode_equal(const struct drm_display_mode *mode1,
3691 const struct drm_display_mode *mode2)
3692{
3693 return drm_mode_match(mode1, mode2,
3694 DRM_MODE_MATCH_TIMINGS |
3695 DRM_MODE_MATCH_FLAGS |
3696 DRM_MODE_MATCH_3D_FLAGS) &&
3697 mode1->clock == mode2->clock; /* we want an exact match */
3698}
3699
3700static bool m_n_equal(const struct intel_link_m_n *m_n_1,
3701 const struct intel_link_m_n *m_n_2)
3702{
3703 return m_n_1->tu == m_n_2->tu &&
3704 m_n_1->data_m == m_n_2->data_m &&
3705 m_n_1->data_n == m_n_2->data_n &&
3706 m_n_1->link_m == m_n_2->link_m &&
3707 m_n_1->link_n == m_n_2->link_n;
3708}
3709
3710static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
3711 const struct intel_crtc_state *crtc_state2)
3712{
3713 return crtc_state1->hw.active && crtc_state2->hw.active &&
3714 crtc_state1->output_types == crtc_state2->output_types &&
3715 crtc_state1->output_format == crtc_state2->output_format &&
3716 crtc_state1->lane_count == crtc_state2->lane_count &&
3717 crtc_state1->port_clock == crtc_state2->port_clock &&
3718 mode_equal(&crtc_state1->hw.adjusted_mode,
3719 &crtc_state2->hw.adjusted_mode) &&
3720 m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
3721}
3722
3723static u8
3724intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
3725 int tile_group_id)
3726{
3727 struct drm_connector *connector;
3728 const struct drm_connector_state *conn_state;
3729 struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
3730 struct intel_atomic_state *state =
3731 to_intel_atomic_state(ref_crtc_state->uapi.state);
3732 u8 transcoders = 0;
3733 int i;
3734
3735 /*
3736 * We don't enable port sync on BDW due to missing w/as and
3737 * due to not having adjusted the modeset sequence appropriately.
3738 */
3739 if (DISPLAY_VER(dev_priv) < 9)
3740 return 0;
3741
3742 if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
3743 return 0;
3744
3745 for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
3746 struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
3747 const struct intel_crtc_state *crtc_state;
3748
3749 if (!crtc)
3750 continue;
3751
3752 if (!connector->has_tile ||
3753 connector->tile_group->id !=
3754 tile_group_id)
3755 continue;
3756 crtc_state = intel_atomic_get_new_crtc_state(state,
3757 crtc);
3758 if (!crtcs_port_sync_compatible(ref_crtc_state,
3759 crtc_state))
3760 continue;
3761 transcoders |= BIT(crtc_state->cpu_transcoder);
3762 }
3763
3764 return transcoders;
3765}
3766
3767static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
3768 struct intel_crtc_state *crtc_state,
3769 struct drm_connector_state *conn_state)
3770{
3771 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3772 struct drm_connector *connector = conn_state->connector;
3773 u8 port_sync_transcoders = 0;
3774
3775 drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]",
3776 encoder->base.base.id, encoder->base.name,
3777 crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
3778
3779 if (connector->has_tile)
3780 port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
3781 connector->tile_group->id);
3782
3783 /*
3784 * EDP Transcoders cannot be ensalved
3785 * make them a master always when present
3786 */
3787 if (port_sync_transcoders & BIT(TRANSCODER_EDP))
3788 crtc_state->master_transcoder = TRANSCODER_EDP;
3789 else
3790 crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
3791
3792 if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
3793 crtc_state->master_transcoder = INVALID_TRANSCODER;
3794 crtc_state->sync_mode_slaves_mask =
3795 port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
3796 }
3797
3798 return 0;
3799}
3800
3801static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
3802{
3803 struct drm_i915_private *i915 = to_i915(encoder->dev);
3804 struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
3805 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
3806
3807 intel_dp_encoder_flush_work(encoder);
3808 if (intel_phy_is_tc(i915, phy))
3809 intel_tc_port_flush_work(dig_port);
3810 intel_display_power_flush_work(i915);
3811
3812 drm_encoder_cleanup(encoder);
3813 kfree(dig_port->hdcp_port_data.streams);
3814 kfree(dig_port);
3815}
3816
3817static void intel_ddi_encoder_reset(struct drm_encoder *encoder)
3818{
3819 struct drm_i915_private *i915 = to_i915(encoder->dev);
3820 struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(encoder));
3821 struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
3822 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
3823
3824 intel_dp->reset_link_params = true;
3825
3826 intel_pps_encoder_reset(intel_dp);
3827
3828 if (intel_phy_is_tc(i915, phy))
3829 intel_tc_port_init_mode(dig_port);
3830}
3831
3832static const struct drm_encoder_funcs intel_ddi_funcs = {
3833 .reset = intel_ddi_encoder_reset,
3834 .destroy = intel_ddi_encoder_destroy,
3835};
3836
3837static struct intel_connector *
3838intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
3839{
3840 struct intel_connector *connector;
3841 enum port port = dig_port->base.port;
3842
3843 connector = intel_connector_alloc();
3844 if (!connector)
3845 return NULL;
3846
3847 dig_port->dp.output_reg = DDI_BUF_CTL(port);
3848 dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
3849 dig_port->dp.set_link_train = intel_ddi_set_link_train;
3850 dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
3851
3852 dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
3853 dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
3854
3855 if (!intel_dp_init_connector(dig_port, connector)) {
3856 kfree(connector);
3857 return NULL;
3858 }
3859
3860 if (dig_port->base.type == INTEL_OUTPUT_EDP) {
3861 struct drm_device *dev = dig_port->base.base.dev;
3862 struct drm_privacy_screen *privacy_screen;
3863
3864 privacy_screen = drm_privacy_screen_get(dev->dev, NULL);
3865 if (!IS_ERR(privacy_screen)) {
3866 drm_connector_attach_privacy_screen_provider(&connector->base,
3867 privacy_screen);
3868 } else if (PTR_ERR(privacy_screen) != -ENODEV) {
3869 drm_warn(dev, "Error getting privacy-screen\n");
3870 }
3871 }
3872
3873 return connector;
3874}
3875
3876static int modeset_pipe(struct drm_crtc *crtc,
3877 struct drm_modeset_acquire_ctx *ctx)
3878{
3879 struct drm_atomic_state *state;
3880 struct drm_crtc_state *crtc_state;
3881 int ret;
3882
3883 state = drm_atomic_state_alloc(crtc->dev);
3884 if (!state)
3885 return -ENOMEM;
3886
3887 state->acquire_ctx = ctx;
3888
3889 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3890 if (IS_ERR(crtc_state)) {
3891 ret = PTR_ERR(crtc_state);
3892 goto out;
3893 }
3894
3895 crtc_state->connectors_changed = true;
3896
3897 ret = drm_atomic_commit(state);
3898out:
3899 drm_atomic_state_put(state);
3900
3901 return ret;
3902}
3903
3904static int intel_hdmi_reset_link(struct intel_encoder *encoder,
3905 struct drm_modeset_acquire_ctx *ctx)
3906{
3907 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3908 struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
3909 struct intel_connector *connector = hdmi->attached_connector;
3910 struct i2c_adapter *adapter =
3911 intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
3912 struct drm_connector_state *conn_state;
3913 struct intel_crtc_state *crtc_state;
3914 struct intel_crtc *crtc;
3915 u8 config;
3916 int ret;
3917
3918 if (!connector || connector->base.status != connector_status_connected)
3919 return 0;
3920
3921 ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
3922 ctx);
3923 if (ret)
3924 return ret;
3925
3926 conn_state = connector->base.state;
3927
3928 crtc = to_intel_crtc(conn_state->crtc);
3929 if (!crtc)
3930 return 0;
3931
3932 ret = drm_modeset_lock(&crtc->base.mutex, ctx);
3933 if (ret)
3934 return ret;
3935
3936 crtc_state = to_intel_crtc_state(crtc->base.state);
3937
3938 drm_WARN_ON(&dev_priv->drm,
3939 !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
3940
3941 if (!crtc_state->hw.active)
3942 return 0;
3943
3944 if (!crtc_state->hdmi_high_tmds_clock_ratio &&
3945 !crtc_state->hdmi_scrambling)
3946 return 0;
3947
3948 if (conn_state->commit &&
3949 !try_wait_for_completion(&conn_state->commit->hw_done))
3950 return 0;
3951
3952 ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
3953 if (ret < 0) {
3954 drm_err(&dev_priv->drm, "Failed to read TMDS config: %d\n",
3955 ret);
3956 return 0;
3957 }
3958
3959 if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
3960 crtc_state->hdmi_high_tmds_clock_ratio &&
3961 !!(config & SCDC_SCRAMBLING_ENABLE) ==
3962 crtc_state->hdmi_scrambling)
3963 return 0;
3964
3965 /*
3966 * HDMI 2.0 says that one should not send scrambled data
3967 * prior to configuring the sink scrambling, and that
3968 * TMDS clock/data transmission should be suspended when
3969 * changing the TMDS clock rate in the sink. So let's
3970 * just do a full modeset here, even though some sinks
3971 * would be perfectly happy if were to just reconfigure
3972 * the SCDC settings on the fly.
3973 */
3974 return modeset_pipe(&crtc->base, ctx);
3975}
3976
3977static enum intel_hotplug_state
3978intel_ddi_hotplug(struct intel_encoder *encoder,
3979 struct intel_connector *connector)
3980{
3981 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3982 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3983 struct intel_dp *intel_dp = &dig_port->dp;
3984 enum phy phy = intel_port_to_phy(i915, encoder->port);
3985 bool is_tc = intel_phy_is_tc(i915, phy);
3986 struct drm_modeset_acquire_ctx ctx;
3987 enum intel_hotplug_state state;
3988 int ret;
3989
3990 if (intel_dp->compliance.test_active &&
3991 intel_dp->compliance.test_type == DP_TEST_LINK_PHY_TEST_PATTERN) {
3992 intel_dp_phy_test(encoder);
3993 /* just do the PHY test and nothing else */
3994 return INTEL_HOTPLUG_UNCHANGED;
3995 }
3996
3997 state = intel_encoder_hotplug(encoder, connector);
3998
3999 drm_modeset_acquire_init(&ctx, 0);
4000
4001 for (;;) {
4002 if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4003 ret = intel_hdmi_reset_link(encoder, &ctx);
4004 else
4005 ret = intel_dp_retrain_link(encoder, &ctx);
4006
4007 if (ret == -EDEADLK) {
4008 drm_modeset_backoff(&ctx);
4009 continue;
4010 }
4011
4012 break;
4013 }
4014
4015 drm_modeset_drop_locks(&ctx);
4016 drm_modeset_acquire_fini(&ctx);
4017 drm_WARN(encoder->base.dev, ret,
4018 "Acquiring modeset locks failed with %i\n", ret);
4019
4020 /*
4021 * Unpowered type-c dongles can take some time to boot and be
4022 * responsible, so here giving some time to those dongles to power up
4023 * and then retrying the probe.
4024 *
4025 * On many platforms the HDMI live state signal is known to be
4026 * unreliable, so we can't use it to detect if a sink is connected or
4027 * not. Instead we detect if it's connected based on whether we can
4028 * read the EDID or not. That in turn has a problem during disconnect,
4029 * since the HPD interrupt may be raised before the DDC lines get
4030 * disconnected (due to how the required length of DDC vs. HPD
4031 * connector pins are specified) and so we'll still be able to get a
4032 * valid EDID. To solve this schedule another detection cycle if this
4033 * time around we didn't detect any change in the sink's connection
4034 * status.
4035 *
4036 * Type-c connectors which get their HPD signal deasserted then
4037 * reasserted, without unplugging/replugging the sink from the
4038 * connector, introduce a delay until the AUX channel communication
4039 * becomes functional. Retry the detection for 5 seconds on type-c
4040 * connectors to account for this delay.
4041 */
4042 if (state == INTEL_HOTPLUG_UNCHANGED &&
4043 connector->hotplug_retries < (is_tc ? 5 : 1) &&
4044 !dig_port->dp.is_mst)
4045 state = INTEL_HOTPLUG_RETRY;
4046
4047 return state;
4048}
4049
4050static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4051{
4052 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4053 u32 bit = dev_priv->display.hotplug.pch_hpd[encoder->hpd_pin];
4054
4055 return intel_de_read(dev_priv, SDEISR) & bit;
4056}
4057
4058static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4059{
4060 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4061 u32 bit = dev_priv->display.hotplug.hpd[encoder->hpd_pin];
4062
4063 return intel_de_read(dev_priv, DEISR) & bit;
4064}
4065
4066static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4067{
4068 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4069 u32 bit = dev_priv->display.hotplug.hpd[encoder->hpd_pin];
4070
4071 return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4072}
4073
4074static struct intel_connector *
4075intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4076{
4077 struct intel_connector *connector;
4078 enum port port = dig_port->base.port;
4079
4080 connector = intel_connector_alloc();
4081 if (!connector)
4082 return NULL;
4083
4084 dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
4085 intel_hdmi_init_connector(dig_port, connector);
4086
4087 return connector;
4088}
4089
4090static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4091{
4092 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4093
4094 if (dig_port->base.port != PORT_A)
4095 return false;
4096
4097 if (dig_port->saved_port_bits & DDI_A_4_LANES)
4098 return false;
4099
4100 /* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4101 * supported configuration
4102 */
4103 if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4104 return true;
4105
4106 return false;
4107}
4108
4109static int
4110intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4111{
4112 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4113 enum port port = dig_port->base.port;
4114 int max_lanes = 4;
4115
4116 if (DISPLAY_VER(dev_priv) >= 11)
4117 return max_lanes;
4118
4119 if (port == PORT_A || port == PORT_E) {
4120 if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4121 max_lanes = port == PORT_A ? 4 : 0;
4122 else
4123 /* Both A and E share 2 lanes */
4124 max_lanes = 2;
4125 }
4126
4127 /*
4128 * Some BIOS might fail to set this bit on port A if eDP
4129 * wasn't lit up at boot. Force this bit set when needed
4130 * so we use the proper lane count for our calculations.
4131 */
4132 if (intel_ddi_a_force_4_lanes(dig_port)) {
4133 drm_dbg_kms(&dev_priv->drm,
4134 "Forcing DDI_A_4_LANES for port A\n");
4135 dig_port->saved_port_bits |= DDI_A_4_LANES;
4136 max_lanes = 4;
4137 }
4138
4139 return max_lanes;
4140}
4141
4142static enum hpd_pin xelpd_hpd_pin(struct drm_i915_private *dev_priv,
4143 enum port port)
4144{
4145 if (port >= PORT_D_XELPD)
4146 return HPD_PORT_D + port - PORT_D_XELPD;
4147 else if (port >= PORT_TC1)
4148 return HPD_PORT_TC1 + port - PORT_TC1;
4149 else
4150 return HPD_PORT_A + port - PORT_A;
4151}
4152
4153static enum hpd_pin dg1_hpd_pin(struct drm_i915_private *dev_priv,
4154 enum port port)
4155{
4156 if (port >= PORT_TC1)
4157 return HPD_PORT_C + port - PORT_TC1;
4158 else
4159 return HPD_PORT_A + port - PORT_A;
4160}
4161
4162static enum hpd_pin tgl_hpd_pin(struct drm_i915_private *dev_priv,
4163 enum port port)
4164{
4165 if (port >= PORT_TC1)
4166 return HPD_PORT_TC1 + port - PORT_TC1;
4167 else
4168 return HPD_PORT_A + port - PORT_A;
4169}
4170
4171static enum hpd_pin rkl_hpd_pin(struct drm_i915_private *dev_priv,
4172 enum port port)
4173{
4174 if (HAS_PCH_TGP(dev_priv))
4175 return tgl_hpd_pin(dev_priv, port);
4176
4177 if (port >= PORT_TC1)
4178 return HPD_PORT_C + port - PORT_TC1;
4179 else
4180 return HPD_PORT_A + port - PORT_A;
4181}
4182
4183static enum hpd_pin icl_hpd_pin(struct drm_i915_private *dev_priv,
4184 enum port port)
4185{
4186 if (port >= PORT_C)
4187 return HPD_PORT_TC1 + port - PORT_C;
4188 else
4189 return HPD_PORT_A + port - PORT_A;
4190}
4191
4192static enum hpd_pin ehl_hpd_pin(struct drm_i915_private *dev_priv,
4193 enum port port)
4194{
4195 if (port == PORT_D)
4196 return HPD_PORT_A;
4197
4198 if (HAS_PCH_TGP(dev_priv))
4199 return icl_hpd_pin(dev_priv, port);
4200
4201 return HPD_PORT_A + port - PORT_A;
4202}
4203
4204static enum hpd_pin skl_hpd_pin(struct drm_i915_private *dev_priv, enum port port)
4205{
4206 if (HAS_PCH_TGP(dev_priv))
4207 return icl_hpd_pin(dev_priv, port);
4208
4209 return HPD_PORT_A + port - PORT_A;
4210}
4211
4212static bool intel_ddi_is_tc(struct drm_i915_private *i915, enum port port)
4213{
4214 if (DISPLAY_VER(i915) >= 12)
4215 return port >= PORT_TC1;
4216 else if (DISPLAY_VER(i915) >= 11)
4217 return port >= PORT_C;
4218 else
4219 return false;
4220}
4221
4222static void intel_ddi_encoder_suspend(struct intel_encoder *encoder)
4223{
4224 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4225 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4226 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4227 enum phy phy = intel_port_to_phy(i915, encoder->port);
4228
4229 intel_dp_encoder_suspend(encoder);
4230
4231 if (!intel_phy_is_tc(i915, phy))
4232 return;
4233
4234 intel_tc_port_flush_work(dig_port);
4235}
4236
4237static void intel_ddi_encoder_shutdown(struct intel_encoder *encoder)
4238{
4239 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4240 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4241 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4242 enum phy phy = intel_port_to_phy(i915, encoder->port);
4243
4244 intel_dp_encoder_shutdown(encoder);
4245 intel_hdmi_encoder_shutdown(encoder);
4246
4247 if (!intel_phy_is_tc(i915, phy))
4248 return;
4249
4250 intel_tc_port_flush_work(dig_port);
4251}
4252
4253#define port_tc_name(port) ((port) - PORT_TC1 + '1')
4254#define tc_port_name(tc_port) ((tc_port) - TC_PORT_1 + '1')
4255
4256void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4257{
4258 struct intel_digital_port *dig_port;
4259 struct intel_encoder *encoder;
4260 const struct intel_bios_encoder_data *devdata;
4261 bool init_hdmi, init_dp;
4262 enum phy phy = intel_port_to_phy(dev_priv, port);
4263
4264 /*
4265 * On platforms with HTI (aka HDPORT), if it's enabled at boot it may
4266 * have taken over some of the PHYs and made them unavailable to the
4267 * driver. In that case we should skip initializing the corresponding
4268 * outputs.
4269 */
4270 if (intel_hti_uses_phy(dev_priv, phy)) {
4271 drm_dbg_kms(&dev_priv->drm, "PORT %c / PHY %c reserved by HTI\n",
4272 port_name(port), phy_name(phy));
4273 return;
4274 }
4275
4276 devdata = intel_bios_encoder_data_lookup(dev_priv, port);
4277 if (!devdata) {
4278 drm_dbg_kms(&dev_priv->drm,
4279 "VBT says port %c is not present\n",
4280 port_name(port));
4281 return;
4282 }
4283
4284 init_hdmi = intel_bios_encoder_supports_dvi(devdata) ||
4285 intel_bios_encoder_supports_hdmi(devdata);
4286 init_dp = intel_bios_encoder_supports_dp(devdata);
4287
4288 if (intel_bios_is_lspcon_present(dev_priv, port)) {
4289 /*
4290 * Lspcon device needs to be driven with DP connector
4291 * with special detection sequence. So make sure DP
4292 * is initialized before lspcon.
4293 */
4294 init_dp = true;
4295 init_hdmi = false;
4296 drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
4297 port_name(port));
4298 }
4299
4300 if (!init_dp && !init_hdmi) {
4301 drm_dbg_kms(&dev_priv->drm,
4302 "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4303 port_name(port));
4304 return;
4305 }
4306
4307 if (intel_phy_is_snps(dev_priv, phy) &&
4308 dev_priv->snps_phy_failed_calibration & BIT(phy)) {
4309 drm_dbg_kms(&dev_priv->drm,
4310 "SNPS PHY %c failed to calibrate, proceeding anyway\n",
4311 phy_name(phy));
4312 }
4313
4314 dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
4315 if (!dig_port)
4316 return;
4317
4318 encoder = &dig_port->base;
4319 encoder->devdata = devdata;
4320
4321 if (DISPLAY_VER(dev_priv) >= 13 && port >= PORT_D_XELPD) {
4322 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4323 DRM_MODE_ENCODER_TMDS,
4324 "DDI %c/PHY %c",
4325 port_name(port - PORT_D_XELPD + PORT_D),
4326 phy_name(phy));
4327 } else if (DISPLAY_VER(dev_priv) >= 12) {
4328 enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
4329
4330 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4331 DRM_MODE_ENCODER_TMDS,
4332 "DDI %s%c/PHY %s%c",
4333 port >= PORT_TC1 ? "TC" : "",
4334 port >= PORT_TC1 ? port_tc_name(port) : port_name(port),
4335 tc_port != TC_PORT_NONE ? "TC" : "",
4336 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
4337 } else if (DISPLAY_VER(dev_priv) >= 11) {
4338 enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
4339
4340 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4341 DRM_MODE_ENCODER_TMDS,
4342 "DDI %c%s/PHY %s%c",
4343 port_name(port),
4344 port >= PORT_C ? " (TC)" : "",
4345 tc_port != TC_PORT_NONE ? "TC" : "",
4346 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
4347 } else {
4348 drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4349 DRM_MODE_ENCODER_TMDS,
4350 "DDI %c/PHY %c", port_name(port), phy_name(phy));
4351 }
4352
4353 mutex_init(&dig_port->hdcp_mutex);
4354 dig_port->num_hdcp_streams = 0;
4355
4356 encoder->hotplug = intel_ddi_hotplug;
4357 encoder->compute_output_type = intel_ddi_compute_output_type;
4358 encoder->compute_config = intel_ddi_compute_config;
4359 encoder->compute_config_late = intel_ddi_compute_config_late;
4360 encoder->enable = intel_enable_ddi;
4361 encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
4362 encoder->pre_enable = intel_ddi_pre_enable;
4363 encoder->disable = intel_disable_ddi;
4364 encoder->post_disable = intel_ddi_post_disable;
4365 encoder->update_pipe = intel_ddi_update_pipe;
4366 encoder->get_hw_state = intel_ddi_get_hw_state;
4367 encoder->sync_state = intel_ddi_sync_state;
4368 encoder->initial_fastset_check = intel_ddi_initial_fastset_check;
4369 encoder->suspend = intel_ddi_encoder_suspend;
4370 encoder->shutdown = intel_ddi_encoder_shutdown;
4371 encoder->get_power_domains = intel_ddi_get_power_domains;
4372
4373 encoder->type = INTEL_OUTPUT_DDI;
4374 encoder->power_domain = intel_display_power_ddi_lanes_domain(dev_priv, port);
4375 encoder->port = port;
4376 encoder->cloneable = 0;
4377 encoder->pipe_mask = ~0;
4378
4379 if (IS_DG2(dev_priv)) {
4380 encoder->enable_clock = intel_mpllb_enable;
4381 encoder->disable_clock = intel_mpllb_disable;
4382 encoder->get_config = dg2_ddi_get_config;
4383 } else if (IS_ALDERLAKE_S(dev_priv)) {
4384 encoder->enable_clock = adls_ddi_enable_clock;
4385 encoder->disable_clock = adls_ddi_disable_clock;
4386 encoder->is_clock_enabled = adls_ddi_is_clock_enabled;
4387 encoder->get_config = adls_ddi_get_config;
4388 } else if (IS_ROCKETLAKE(dev_priv)) {
4389 encoder->enable_clock = rkl_ddi_enable_clock;
4390 encoder->disable_clock = rkl_ddi_disable_clock;
4391 encoder->is_clock_enabled = rkl_ddi_is_clock_enabled;
4392 encoder->get_config = rkl_ddi_get_config;
4393 } else if (IS_DG1(dev_priv)) {
4394 encoder->enable_clock = dg1_ddi_enable_clock;
4395 encoder->disable_clock = dg1_ddi_disable_clock;
4396 encoder->is_clock_enabled = dg1_ddi_is_clock_enabled;
4397 encoder->get_config = dg1_ddi_get_config;
4398 } else if (IS_JSL_EHL(dev_priv)) {
4399 if (intel_ddi_is_tc(dev_priv, port)) {
4400 encoder->enable_clock = jsl_ddi_tc_enable_clock;
4401 encoder->disable_clock = jsl_ddi_tc_disable_clock;
4402 encoder->is_clock_enabled = jsl_ddi_tc_is_clock_enabled;
4403 encoder->get_config = icl_ddi_combo_get_config;
4404 } else {
4405 encoder->enable_clock = icl_ddi_combo_enable_clock;
4406 encoder->disable_clock = icl_ddi_combo_disable_clock;
4407 encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4408 encoder->get_config = icl_ddi_combo_get_config;
4409 }
4410 } else if (DISPLAY_VER(dev_priv) >= 11) {
4411 if (intel_ddi_is_tc(dev_priv, port)) {
4412 encoder->enable_clock = icl_ddi_tc_enable_clock;
4413 encoder->disable_clock = icl_ddi_tc_disable_clock;
4414 encoder->is_clock_enabled = icl_ddi_tc_is_clock_enabled;
4415 encoder->get_config = icl_ddi_tc_get_config;
4416 } else {
4417 encoder->enable_clock = icl_ddi_combo_enable_clock;
4418 encoder->disable_clock = icl_ddi_combo_disable_clock;
4419 encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4420 encoder->get_config = icl_ddi_combo_get_config;
4421 }
4422 } else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
4423 /* BXT/GLK have fixed PLL->port mapping */
4424 encoder->get_config = bxt_ddi_get_config;
4425 } else if (DISPLAY_VER(dev_priv) == 9) {
4426 encoder->enable_clock = skl_ddi_enable_clock;
4427 encoder->disable_clock = skl_ddi_disable_clock;
4428 encoder->is_clock_enabled = skl_ddi_is_clock_enabled;
4429 encoder->get_config = skl_ddi_get_config;
4430 } else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
4431 encoder->enable_clock = hsw_ddi_enable_clock;
4432 encoder->disable_clock = hsw_ddi_disable_clock;
4433 encoder->is_clock_enabled = hsw_ddi_is_clock_enabled;
4434 encoder->get_config = hsw_ddi_get_config;
4435 }
4436
4437 if (IS_DG2(dev_priv)) {
4438 encoder->set_signal_levels = intel_snps_phy_set_signal_levels;
4439 } else if (DISPLAY_VER(dev_priv) >= 12) {
4440 if (intel_phy_is_combo(dev_priv, phy))
4441 encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4442 else
4443 encoder->set_signal_levels = tgl_dkl_phy_set_signal_levels;
4444 } else if (DISPLAY_VER(dev_priv) >= 11) {
4445 if (intel_phy_is_combo(dev_priv, phy))
4446 encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4447 else
4448 encoder->set_signal_levels = icl_mg_phy_set_signal_levels;
4449 } else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
4450 encoder->set_signal_levels = bxt_ddi_phy_set_signal_levels;
4451 } else {
4452 encoder->set_signal_levels = hsw_set_signal_levels;
4453 }
4454
4455 intel_ddi_buf_trans_init(encoder);
4456
4457 if (DISPLAY_VER(dev_priv) >= 13)
4458 encoder->hpd_pin = xelpd_hpd_pin(dev_priv, port);
4459 else if (IS_DG1(dev_priv))
4460 encoder->hpd_pin = dg1_hpd_pin(dev_priv, port);
4461 else if (IS_ROCKETLAKE(dev_priv))
4462 encoder->hpd_pin = rkl_hpd_pin(dev_priv, port);
4463 else if (DISPLAY_VER(dev_priv) >= 12)
4464 encoder->hpd_pin = tgl_hpd_pin(dev_priv, port);
4465 else if (IS_JSL_EHL(dev_priv))
4466 encoder->hpd_pin = ehl_hpd_pin(dev_priv, port);
4467 else if (DISPLAY_VER(dev_priv) == 11)
4468 encoder->hpd_pin = icl_hpd_pin(dev_priv, port);
4469 else if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
4470 encoder->hpd_pin = skl_hpd_pin(dev_priv, port);
4471 else
4472 encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
4473
4474 if (DISPLAY_VER(dev_priv) >= 11)
4475 dig_port->saved_port_bits =
4476 intel_de_read(dev_priv, DDI_BUF_CTL(port))
4477 & DDI_BUF_PORT_REVERSAL;
4478 else
4479 dig_port->saved_port_bits =
4480 intel_de_read(dev_priv, DDI_BUF_CTL(port))
4481 & (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
4482
4483 if (intel_bios_is_lane_reversal_needed(dev_priv, port))
4484 dig_port->saved_port_bits |= DDI_BUF_PORT_REVERSAL;
4485
4486 dig_port->dp.output_reg = INVALID_MMIO_REG;
4487 dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
4488 dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4489
4490 if (intel_phy_is_tc(dev_priv, phy)) {
4491 bool is_legacy =
4492 !intel_bios_encoder_supports_typec_usb(devdata) &&
4493 !intel_bios_encoder_supports_tbt(devdata);
4494
4495 intel_tc_port_init(dig_port, is_legacy);
4496
4497 encoder->update_prepare = intel_ddi_update_prepare;
4498 encoder->update_complete = intel_ddi_update_complete;
4499 }
4500
4501 drm_WARN_ON(&dev_priv->drm, port > PORT_I);
4502 dig_port->ddi_io_power_domain = intel_display_power_ddi_io_domain(dev_priv, port);
4503
4504 if (init_dp) {
4505 if (!intel_ddi_init_dp_connector(dig_port))
4506 goto err;
4507
4508 dig_port->hpd_pulse = intel_dp_hpd_pulse;
4509
4510 if (dig_port->dp.mso_link_count)
4511 encoder->pipe_mask = intel_ddi_splitter_pipe_mask(dev_priv);
4512 }
4513
4514 /* In theory we don't need the encoder->type check, but leave it just in
4515 * case we have some really bad VBTs... */
4516 if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
4517 if (!intel_ddi_init_hdmi_connector(dig_port))
4518 goto err;
4519 }
4520
4521 if (DISPLAY_VER(dev_priv) >= 11) {
4522 if (intel_phy_is_tc(dev_priv, phy))
4523 dig_port->connected = intel_tc_port_connected;
4524 else
4525 dig_port->connected = lpt_digital_port_connected;
4526 } else if (DISPLAY_VER(dev_priv) >= 8) {
4527 if (port == PORT_A || IS_GEMINILAKE(dev_priv) ||
4528 IS_BROXTON(dev_priv))
4529 dig_port->connected = bdw_digital_port_connected;
4530 else
4531 dig_port->connected = lpt_digital_port_connected;
4532 } else {
4533 if (port == PORT_A)
4534 dig_port->connected = hsw_digital_port_connected;
4535 else
4536 dig_port->connected = lpt_digital_port_connected;
4537 }
4538
4539 intel_infoframe_init(dig_port);
4540
4541 return;
4542
4543err:
4544 drm_encoder_cleanup(&encoder->base);
4545 kfree(dig_port);
4546}