1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
8 #include "intel_atomic.h"
9 #include "intel_cx0_phy_regs.h"
10 #include "intel_ddi.h"
12 #include "intel_display.h"
13 #include "intel_display_driver.h"
14 #include "intel_display_power_map.h"
15 #include "intel_display_types.h"
16 #include "intel_dkl_phy_regs.h"
18 #include "intel_dp_mst.h"
19 #include "intel_mg_phy_regs.h"
20 #include "intel_modeset_lock.h"
23 #define DP_PIN_ASSIGNMENT_C 0x3
24 #define DP_PIN_ASSIGNMENT_D 0x4
25 #define DP_PIN_ASSIGNMENT_E 0x5
36 struct intel_tc_phy_ops {
37 enum intel_display_power_domain (*cold_off_domain)(struct intel_tc_port *tc);
38 u32 (*hpd_live_status)(struct intel_tc_port *tc);
39 bool (*is_ready)(struct intel_tc_port *tc);
40 bool (*is_owned)(struct intel_tc_port *tc);
41 void (*get_hw_state)(struct intel_tc_port *tc);
42 bool (*connect)(struct intel_tc_port *tc, int required_lanes);
43 void (*disconnect)(struct intel_tc_port *tc);
44 void (*init)(struct intel_tc_port *tc);
47 struct intel_tc_port {
48 struct intel_digital_port *dig_port;
50 const struct intel_tc_phy_ops *phy_ops;
52 struct mutex lock; /* protects the TypeC port mode */
53 intel_wakeref_t lock_wakeref;
54 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
55 enum intel_display_power_domain lock_power_domain;
57 struct delayed_work disconnect_phy_work;
58 struct delayed_work link_reset_work;
61 const char *port_name;
62 enum tc_port_mode mode;
63 enum tc_port_mode init_mode;
68 static enum intel_display_power_domain
69 tc_phy_cold_off_domain(struct intel_tc_port *);
70 static u32 tc_phy_hpd_live_status(struct intel_tc_port *tc);
71 static bool tc_phy_is_ready(struct intel_tc_port *tc);
72 static bool tc_phy_wait_for_ready(struct intel_tc_port *tc);
73 static enum tc_port_mode tc_phy_get_current_mode(struct intel_tc_port *tc);
75 static const char *tc_port_mode_name(enum tc_port_mode mode)
77 static const char * const names[] = {
78 [TC_PORT_DISCONNECTED] = "disconnected",
79 [TC_PORT_TBT_ALT] = "tbt-alt",
80 [TC_PORT_DP_ALT] = "dp-alt",
81 [TC_PORT_LEGACY] = "legacy",
84 if (WARN_ON(mode >= ARRAY_SIZE(names)))
85 mode = TC_PORT_DISCONNECTED;
90 static struct intel_tc_port *to_tc_port(struct intel_digital_port *dig_port)
95 static struct drm_i915_private *tc_to_i915(struct intel_tc_port *tc)
97 return to_i915(tc->dig_port->base.base.dev);
100 static bool intel_tc_port_in_mode(struct intel_digital_port *dig_port,
101 enum tc_port_mode mode)
103 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
104 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
105 struct intel_tc_port *tc = to_tc_port(dig_port);
107 return intel_phy_is_tc(i915, phy) && tc->mode == mode;
110 bool intel_tc_port_in_tbt_alt_mode(struct intel_digital_port *dig_port)
112 return intel_tc_port_in_mode(dig_port, TC_PORT_TBT_ALT);
115 bool intel_tc_port_in_dp_alt_mode(struct intel_digital_port *dig_port)
117 return intel_tc_port_in_mode(dig_port, TC_PORT_DP_ALT);
120 bool intel_tc_port_in_legacy_mode(struct intel_digital_port *dig_port)
122 return intel_tc_port_in_mode(dig_port, TC_PORT_LEGACY);
126 * The display power domains used for TC ports depending on the
127 * platform and TC mode (legacy, DP-alt, TBT):
129 * POWER_DOMAIN_DISPLAY_CORE:
130 * --------------------------
132 * - TCSS/IOM access for PHY ready state.
134 * - DE/north-,south-HPD ISR access for HPD live state.
136 * POWER_DOMAIN_PORT_DDI_LANES_<port>:
137 * -----------------------------------
139 * - DE/DDI_BUF access for port enabled state.
141 * - DE/DDI_BUF access for PHY owned state.
143 * POWER_DOMAIN_AUX_USBC<TC port index>:
144 * -------------------------------------
146 * - TCSS/IOM,FIA access for PHY ready, owned and HPD live state
147 * - TCSS/PHY: block TC-cold power state for using the PHY AUX and
149 * ADLP/legacy, DP-alt modes:
150 * - TCSS/PHY: block TC-cold power state for using the PHY AUX and
153 * POWER_DOMAIN_TC_COLD_OFF:
154 * -------------------------
155 * ICL/DP-alt, TBT mode:
156 * - TCSS/TBT: block TC-cold power state for using the (direct or
157 * TBT DP-IN) AUX and main lanes.
160 * - TCSS/IOM,FIA access for PHY ready, owned and HPD live state
161 * - TCSS/PHY: block TC-cold power state for using the (direct or
162 * TBT DP-IN) AUX and main lanes.
165 * - TCSS/TBT: block TC-cold power state for using the (TBT DP-IN)
166 * AUX and main lanes.
169 * - TCSS/IOM,FIA access for PHY ready, owned state
170 * - TCSS/PHY: block TC-cold power state for using the (direct or
171 * TBT DP-IN) AUX and main lanes.
173 bool intel_tc_cold_requires_aux_pw(struct intel_digital_port *dig_port)
175 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
176 struct intel_tc_port *tc = to_tc_port(dig_port);
178 return tc_phy_cold_off_domain(tc) ==
179 intel_display_power_legacy_aux_domain(i915, dig_port->aux_ch);
182 static intel_wakeref_t
183 __tc_cold_block(struct intel_tc_port *tc, enum intel_display_power_domain *domain)
185 struct drm_i915_private *i915 = tc_to_i915(tc);
187 *domain = tc_phy_cold_off_domain(tc);
189 return intel_display_power_get(i915, *domain);
192 static intel_wakeref_t
193 tc_cold_block(struct intel_tc_port *tc)
195 enum intel_display_power_domain domain;
196 intel_wakeref_t wakeref;
198 wakeref = __tc_cold_block(tc, &domain);
199 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
200 tc->lock_power_domain = domain;
206 __tc_cold_unblock(struct intel_tc_port *tc, enum intel_display_power_domain domain,
207 intel_wakeref_t wakeref)
209 struct drm_i915_private *i915 = tc_to_i915(tc);
211 intel_display_power_put(i915, domain, wakeref);
215 tc_cold_unblock(struct intel_tc_port *tc, intel_wakeref_t wakeref)
217 enum intel_display_power_domain domain = tc_phy_cold_off_domain(tc);
219 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
220 drm_WARN_ON(&tc_to_i915(tc)->drm, tc->lock_power_domain != domain);
222 __tc_cold_unblock(tc, domain, wakeref);
226 assert_display_core_power_enabled(struct intel_tc_port *tc)
228 struct drm_i915_private *i915 = tc_to_i915(tc);
230 drm_WARN_ON(&i915->drm,
231 !intel_display_power_is_enabled(i915, POWER_DOMAIN_DISPLAY_CORE));
235 assert_tc_cold_blocked(struct intel_tc_port *tc)
237 struct drm_i915_private *i915 = tc_to_i915(tc);
240 enabled = intel_display_power_is_enabled(i915,
241 tc_phy_cold_off_domain(tc));
242 drm_WARN_ON(&i915->drm, !enabled);
245 static enum intel_display_power_domain
246 tc_port_power_domain(struct intel_tc_port *tc)
248 struct drm_i915_private *i915 = tc_to_i915(tc);
249 enum tc_port tc_port = intel_port_to_tc(i915, tc->dig_port->base.port);
251 return POWER_DOMAIN_PORT_DDI_LANES_TC1 + tc_port - TC_PORT_1;
255 assert_tc_port_power_enabled(struct intel_tc_port *tc)
257 struct drm_i915_private *i915 = tc_to_i915(tc);
259 drm_WARN_ON(&i915->drm,
260 !intel_display_power_is_enabled(i915, tc_port_power_domain(tc)));
263 static u32 intel_tc_port_get_lane_mask(struct intel_digital_port *dig_port)
265 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
266 struct intel_tc_port *tc = to_tc_port(dig_port);
269 lane_mask = intel_de_read(i915, PORT_TX_DFLEXDPSP(tc->phy_fia));
271 drm_WARN_ON(&i915->drm, lane_mask == 0xffffffff);
272 assert_tc_cold_blocked(tc);
274 lane_mask &= DP_LANE_ASSIGNMENT_MASK(tc->phy_fia_idx);
275 return lane_mask >> DP_LANE_ASSIGNMENT_SHIFT(tc->phy_fia_idx);
278 u32 intel_tc_port_get_pin_assignment_mask(struct intel_digital_port *dig_port)
280 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
281 struct intel_tc_port *tc = to_tc_port(dig_port);
284 pin_mask = intel_de_read(i915, PORT_TX_DFLEXPA1(tc->phy_fia));
286 drm_WARN_ON(&i915->drm, pin_mask == 0xffffffff);
287 assert_tc_cold_blocked(tc);
289 return (pin_mask & DP_PIN_ASSIGNMENT_MASK(tc->phy_fia_idx)) >>
290 DP_PIN_ASSIGNMENT_SHIFT(tc->phy_fia_idx);
293 static int lnl_tc_port_get_max_lane_count(struct intel_digital_port *dig_port)
295 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
296 enum tc_port tc_port = intel_port_to_tc(i915, dig_port->base.port);
297 intel_wakeref_t wakeref;
298 u32 val, pin_assignment;
300 with_intel_display_power(i915, POWER_DOMAIN_DISPLAY_CORE, wakeref)
301 val = intel_de_read(i915, TCSS_DDI_STATUS(tc_port));
304 REG_FIELD_GET(TCSS_DDI_STATUS_PIN_ASSIGNMENT_MASK, val);
306 switch (pin_assignment) {
308 MISSING_CASE(pin_assignment);
310 case DP_PIN_ASSIGNMENT_D:
312 case DP_PIN_ASSIGNMENT_C:
313 case DP_PIN_ASSIGNMENT_E:
318 static int mtl_tc_port_get_max_lane_count(struct intel_digital_port *dig_port)
320 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
321 intel_wakeref_t wakeref;
324 with_intel_display_power(i915, POWER_DOMAIN_DISPLAY_CORE, wakeref)
325 pin_mask = intel_tc_port_get_pin_assignment_mask(dig_port);
329 MISSING_CASE(pin_mask);
331 case DP_PIN_ASSIGNMENT_D:
333 case DP_PIN_ASSIGNMENT_C:
334 case DP_PIN_ASSIGNMENT_E:
339 static int intel_tc_port_get_max_lane_count(struct intel_digital_port *dig_port)
341 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
342 intel_wakeref_t wakeref;
345 with_intel_display_power(i915, POWER_DOMAIN_DISPLAY_CORE, wakeref)
346 lane_mask = intel_tc_port_get_lane_mask(dig_port);
350 MISSING_CASE(lane_mask);
365 int intel_tc_port_max_lane_count(struct intel_digital_port *dig_port)
367 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
368 struct intel_tc_port *tc = to_tc_port(dig_port);
369 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
371 if (!intel_phy_is_tc(i915, phy) || tc->mode != TC_PORT_DP_ALT)
374 assert_tc_cold_blocked(tc);
376 if (DISPLAY_VER(i915) >= 20)
377 return lnl_tc_port_get_max_lane_count(dig_port);
379 if (DISPLAY_VER(i915) >= 14)
380 return mtl_tc_port_get_max_lane_count(dig_port);
382 return intel_tc_port_get_max_lane_count(dig_port);
385 void intel_tc_port_set_fia_lane_count(struct intel_digital_port *dig_port,
388 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
389 struct intel_tc_port *tc = to_tc_port(dig_port);
390 bool lane_reversal = dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
393 drm_WARN_ON(&i915->drm,
394 lane_reversal && tc->mode != TC_PORT_LEGACY);
396 assert_tc_cold_blocked(tc);
398 val = intel_de_read(i915, PORT_TX_DFLEXDPMLE1(tc->phy_fia));
399 val &= ~DFLEXDPMLE1_DPMLETC_MASK(tc->phy_fia_idx);
401 switch (required_lanes) {
403 val |= lane_reversal ?
404 DFLEXDPMLE1_DPMLETC_ML3(tc->phy_fia_idx) :
405 DFLEXDPMLE1_DPMLETC_ML0(tc->phy_fia_idx);
408 val |= lane_reversal ?
409 DFLEXDPMLE1_DPMLETC_ML3_2(tc->phy_fia_idx) :
410 DFLEXDPMLE1_DPMLETC_ML1_0(tc->phy_fia_idx);
413 val |= DFLEXDPMLE1_DPMLETC_ML3_0(tc->phy_fia_idx);
416 MISSING_CASE(required_lanes);
419 intel_de_write(i915, PORT_TX_DFLEXDPMLE1(tc->phy_fia), val);
422 static void tc_port_fixup_legacy_flag(struct intel_tc_port *tc,
423 u32 live_status_mask)
425 struct drm_i915_private *i915 = tc_to_i915(tc);
428 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_DISCONNECTED);
430 if (hweight32(live_status_mask) != 1)
434 valid_hpd_mask = BIT(TC_PORT_LEGACY);
436 valid_hpd_mask = BIT(TC_PORT_DP_ALT) |
437 BIT(TC_PORT_TBT_ALT);
439 if (!(live_status_mask & ~valid_hpd_mask))
442 /* If live status mismatches the VBT flag, trust the live status. */
443 drm_dbg_kms(&i915->drm,
444 "Port %s: live status %08x mismatches the legacy port flag %08x, fixing flag\n",
445 tc->port_name, live_status_mask, valid_hpd_mask);
447 tc->legacy_port = !tc->legacy_port;
450 static void tc_phy_load_fia_params(struct intel_tc_port *tc, bool modular_fia)
452 struct drm_i915_private *i915 = tc_to_i915(tc);
453 enum port port = tc->dig_port->base.port;
454 enum tc_port tc_port = intel_port_to_tc(i915, port);
457 * Each Modular FIA instance houses 2 TC ports. In SOC that has more
458 * than two TC ports, there are multiple instances of Modular FIA.
461 tc->phy_fia = tc_port / 2;
462 tc->phy_fia_idx = tc_port % 2;
465 tc->phy_fia_idx = tc_port;
470 * ICL TC PHY handlers
471 * -------------------
473 static enum intel_display_power_domain
474 icl_tc_phy_cold_off_domain(struct intel_tc_port *tc)
476 struct drm_i915_private *i915 = tc_to_i915(tc);
477 struct intel_digital_port *dig_port = tc->dig_port;
480 return intel_display_power_legacy_aux_domain(i915, dig_port->aux_ch);
482 return POWER_DOMAIN_TC_COLD_OFF;
485 static u32 icl_tc_phy_hpd_live_status(struct intel_tc_port *tc)
487 struct drm_i915_private *i915 = tc_to_i915(tc);
488 struct intel_digital_port *dig_port = tc->dig_port;
489 u32 isr_bit = i915->display.hotplug.pch_hpd[dig_port->base.hpd_pin];
490 intel_wakeref_t wakeref;
495 with_intel_display_power(i915, tc_phy_cold_off_domain(tc), wakeref) {
496 fia_isr = intel_de_read(i915, PORT_TX_DFLEXDPSP(tc->phy_fia));
497 pch_isr = intel_de_read(i915, SDEISR);
500 if (fia_isr == 0xffffffff) {
501 drm_dbg_kms(&i915->drm,
502 "Port %s: PHY in TCCOLD, nothing connected\n",
507 if (fia_isr & TC_LIVE_STATE_TBT(tc->phy_fia_idx))
508 mask |= BIT(TC_PORT_TBT_ALT);
509 if (fia_isr & TC_LIVE_STATE_TC(tc->phy_fia_idx))
510 mask |= BIT(TC_PORT_DP_ALT);
512 if (pch_isr & isr_bit)
513 mask |= BIT(TC_PORT_LEGACY);
519 * Return the PHY status complete flag indicating that display can acquire the
520 * PHY ownership. The IOM firmware sets this flag when a DP-alt or legacy sink
521 * is connected and it's ready to switch the ownership to display. The flag
522 * will be left cleared when a TBT-alt sink is connected, where the PHY is
523 * owned by the TBT subsystem and so switching the ownership to display is not
526 static bool icl_tc_phy_is_ready(struct intel_tc_port *tc)
528 struct drm_i915_private *i915 = tc_to_i915(tc);
531 assert_tc_cold_blocked(tc);
533 val = intel_de_read(i915, PORT_TX_DFLEXDPPMS(tc->phy_fia));
534 if (val == 0xffffffff) {
535 drm_dbg_kms(&i915->drm,
536 "Port %s: PHY in TCCOLD, assuming not ready\n",
541 return val & DP_PHY_MODE_STATUS_COMPLETED(tc->phy_fia_idx);
544 static bool icl_tc_phy_take_ownership(struct intel_tc_port *tc,
547 struct drm_i915_private *i915 = tc_to_i915(tc);
550 assert_tc_cold_blocked(tc);
552 val = intel_de_read(i915, PORT_TX_DFLEXDPCSSS(tc->phy_fia));
553 if (val == 0xffffffff) {
554 drm_dbg_kms(&i915->drm,
555 "Port %s: PHY in TCCOLD, can't %s ownership\n",
556 tc->port_name, take ? "take" : "release");
561 val &= ~DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
563 val |= DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
565 intel_de_write(i915, PORT_TX_DFLEXDPCSSS(tc->phy_fia), val);
570 static bool icl_tc_phy_is_owned(struct intel_tc_port *tc)
572 struct drm_i915_private *i915 = tc_to_i915(tc);
575 assert_tc_cold_blocked(tc);
577 val = intel_de_read(i915, PORT_TX_DFLEXDPCSSS(tc->phy_fia));
578 if (val == 0xffffffff) {
579 drm_dbg_kms(&i915->drm,
580 "Port %s: PHY in TCCOLD, assume not owned\n",
585 return val & DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
588 static void icl_tc_phy_get_hw_state(struct intel_tc_port *tc)
590 enum intel_display_power_domain domain;
591 intel_wakeref_t tc_cold_wref;
593 tc_cold_wref = __tc_cold_block(tc, &domain);
595 tc->mode = tc_phy_get_current_mode(tc);
596 if (tc->mode != TC_PORT_DISCONNECTED)
597 tc->lock_wakeref = tc_cold_block(tc);
599 __tc_cold_unblock(tc, domain, tc_cold_wref);
603 * This function implements the first part of the Connect Flow described by our
604 * specification, Gen11 TypeC Programming chapter. The rest of the flow (reading
605 * lanes, EDID, etc) is done as needed in the typical places.
607 * Unlike the other ports, type-C ports are not available to use as soon as we
608 * get a hotplug. The type-C PHYs can be shared between multiple controllers:
609 * display, USB, etc. As a result, handshaking through FIA is required around
610 * connect and disconnect to cleanly transfer ownership with the controller and
611 * set the type-C power state.
613 static bool tc_phy_verify_legacy_or_dp_alt_mode(struct intel_tc_port *tc,
616 struct drm_i915_private *i915 = tc_to_i915(tc);
617 struct intel_digital_port *dig_port = tc->dig_port;
620 max_lanes = intel_tc_port_max_lane_count(dig_port);
621 if (tc->mode == TC_PORT_LEGACY) {
622 drm_WARN_ON(&i915->drm, max_lanes != 4);
626 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_DP_ALT);
629 * Now we have to re-check the live state, in case the port recently
630 * became disconnected. Not necessary for legacy mode.
632 if (!(tc_phy_hpd_live_status(tc) & BIT(TC_PORT_DP_ALT))) {
633 drm_dbg_kms(&i915->drm, "Port %s: PHY sudden disconnect\n",
638 if (max_lanes < required_lanes) {
639 drm_dbg_kms(&i915->drm,
640 "Port %s: PHY max lanes %d < required lanes %d\n",
642 max_lanes, required_lanes);
649 static bool icl_tc_phy_connect(struct intel_tc_port *tc,
652 struct drm_i915_private *i915 = tc_to_i915(tc);
654 tc->lock_wakeref = tc_cold_block(tc);
656 if (tc->mode == TC_PORT_TBT_ALT)
659 if ((!tc_phy_is_ready(tc) ||
660 !icl_tc_phy_take_ownership(tc, true)) &&
661 !drm_WARN_ON(&i915->drm, tc->mode == TC_PORT_LEGACY)) {
662 drm_dbg_kms(&i915->drm, "Port %s: can't take PHY ownership (ready %s)\n",
664 str_yes_no(tc_phy_is_ready(tc)));
665 goto out_unblock_tc_cold;
669 if (!tc_phy_verify_legacy_or_dp_alt_mode(tc, required_lanes))
670 goto out_release_phy;
675 icl_tc_phy_take_ownership(tc, false);
677 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
683 * See the comment at the connect function. This implements the Disconnect
686 static void icl_tc_phy_disconnect(struct intel_tc_port *tc)
691 icl_tc_phy_take_ownership(tc, false);
693 case TC_PORT_TBT_ALT:
694 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
697 MISSING_CASE(tc->mode);
701 static void icl_tc_phy_init(struct intel_tc_port *tc)
703 tc_phy_load_fia_params(tc, false);
706 static const struct intel_tc_phy_ops icl_tc_phy_ops = {
707 .cold_off_domain = icl_tc_phy_cold_off_domain,
708 .hpd_live_status = icl_tc_phy_hpd_live_status,
709 .is_ready = icl_tc_phy_is_ready,
710 .is_owned = icl_tc_phy_is_owned,
711 .get_hw_state = icl_tc_phy_get_hw_state,
712 .connect = icl_tc_phy_connect,
713 .disconnect = icl_tc_phy_disconnect,
714 .init = icl_tc_phy_init,
718 * TGL TC PHY handlers
719 * -------------------
721 static enum intel_display_power_domain
722 tgl_tc_phy_cold_off_domain(struct intel_tc_port *tc)
724 return POWER_DOMAIN_TC_COLD_OFF;
727 static void tgl_tc_phy_init(struct intel_tc_port *tc)
729 struct drm_i915_private *i915 = tc_to_i915(tc);
730 intel_wakeref_t wakeref;
733 with_intel_display_power(i915, tc_phy_cold_off_domain(tc), wakeref)
734 val = intel_de_read(i915, PORT_TX_DFLEXDPSP(FIA1));
736 drm_WARN_ON(&i915->drm, val == 0xffffffff);
738 tc_phy_load_fia_params(tc, val & MODULAR_FIA_MASK);
741 static const struct intel_tc_phy_ops tgl_tc_phy_ops = {
742 .cold_off_domain = tgl_tc_phy_cold_off_domain,
743 .hpd_live_status = icl_tc_phy_hpd_live_status,
744 .is_ready = icl_tc_phy_is_ready,
745 .is_owned = icl_tc_phy_is_owned,
746 .get_hw_state = icl_tc_phy_get_hw_state,
747 .connect = icl_tc_phy_connect,
748 .disconnect = icl_tc_phy_disconnect,
749 .init = tgl_tc_phy_init,
753 * ADLP TC PHY handlers
754 * --------------------
756 static enum intel_display_power_domain
757 adlp_tc_phy_cold_off_domain(struct intel_tc_port *tc)
759 struct drm_i915_private *i915 = tc_to_i915(tc);
760 struct intel_digital_port *dig_port = tc->dig_port;
762 if (tc->mode != TC_PORT_TBT_ALT)
763 return intel_display_power_legacy_aux_domain(i915, dig_port->aux_ch);
765 return POWER_DOMAIN_TC_COLD_OFF;
768 static u32 adlp_tc_phy_hpd_live_status(struct intel_tc_port *tc)
770 struct drm_i915_private *i915 = tc_to_i915(tc);
771 struct intel_digital_port *dig_port = tc->dig_port;
772 enum hpd_pin hpd_pin = dig_port->base.hpd_pin;
773 u32 cpu_isr_bits = i915->display.hotplug.hpd[hpd_pin];
774 u32 pch_isr_bit = i915->display.hotplug.pch_hpd[hpd_pin];
775 intel_wakeref_t wakeref;
780 with_intel_display_power(i915, POWER_DOMAIN_DISPLAY_CORE, wakeref) {
781 cpu_isr = intel_de_read(i915, GEN11_DE_HPD_ISR);
782 pch_isr = intel_de_read(i915, SDEISR);
785 if (cpu_isr & (cpu_isr_bits & GEN11_DE_TC_HOTPLUG_MASK))
786 mask |= BIT(TC_PORT_DP_ALT);
787 if (cpu_isr & (cpu_isr_bits & GEN11_DE_TBT_HOTPLUG_MASK))
788 mask |= BIT(TC_PORT_TBT_ALT);
790 if (pch_isr & pch_isr_bit)
791 mask |= BIT(TC_PORT_LEGACY);
797 * Return the PHY status complete flag indicating that display can acquire the
798 * PHY ownership. The IOM firmware sets this flag when it's ready to switch
799 * the ownership to display, regardless of what sink is connected (TBT-alt,
800 * DP-alt, legacy or nothing). For TBT-alt sinks the PHY is owned by the TBT
801 * subsystem and so switching the ownership to display is not required.
803 static bool adlp_tc_phy_is_ready(struct intel_tc_port *tc)
805 struct drm_i915_private *i915 = tc_to_i915(tc);
806 enum tc_port tc_port = intel_port_to_tc(i915, tc->dig_port->base.port);
809 assert_display_core_power_enabled(tc);
811 val = intel_de_read(i915, TCSS_DDI_STATUS(tc_port));
812 if (val == 0xffffffff) {
813 drm_dbg_kms(&i915->drm,
814 "Port %s: PHY in TCCOLD, assuming not ready\n",
819 return val & TCSS_DDI_STATUS_READY;
822 static bool adlp_tc_phy_take_ownership(struct intel_tc_port *tc,
825 struct drm_i915_private *i915 = tc_to_i915(tc);
826 enum port port = tc->dig_port->base.port;
828 assert_tc_port_power_enabled(tc);
830 intel_de_rmw(i915, DDI_BUF_CTL(port), DDI_BUF_CTL_TC_PHY_OWNERSHIP,
831 take ? DDI_BUF_CTL_TC_PHY_OWNERSHIP : 0);
836 static bool adlp_tc_phy_is_owned(struct intel_tc_port *tc)
838 struct drm_i915_private *i915 = tc_to_i915(tc);
839 enum port port = tc->dig_port->base.port;
842 assert_tc_port_power_enabled(tc);
844 val = intel_de_read(i915, DDI_BUF_CTL(port));
845 return val & DDI_BUF_CTL_TC_PHY_OWNERSHIP;
848 static void adlp_tc_phy_get_hw_state(struct intel_tc_port *tc)
850 struct drm_i915_private *i915 = tc_to_i915(tc);
851 enum intel_display_power_domain port_power_domain =
852 tc_port_power_domain(tc);
853 intel_wakeref_t port_wakeref;
855 port_wakeref = intel_display_power_get(i915, port_power_domain);
857 tc->mode = tc_phy_get_current_mode(tc);
858 if (tc->mode != TC_PORT_DISCONNECTED)
859 tc->lock_wakeref = tc_cold_block(tc);
861 intel_display_power_put(i915, port_power_domain, port_wakeref);
864 static bool adlp_tc_phy_connect(struct intel_tc_port *tc, int required_lanes)
866 struct drm_i915_private *i915 = tc_to_i915(tc);
867 enum intel_display_power_domain port_power_domain =
868 tc_port_power_domain(tc);
869 intel_wakeref_t port_wakeref;
871 if (tc->mode == TC_PORT_TBT_ALT) {
872 tc->lock_wakeref = tc_cold_block(tc);
876 port_wakeref = intel_display_power_get(i915, port_power_domain);
878 if (!adlp_tc_phy_take_ownership(tc, true) &&
879 !drm_WARN_ON(&i915->drm, tc->mode == TC_PORT_LEGACY)) {
880 drm_dbg_kms(&i915->drm, "Port %s: can't take PHY ownership\n",
882 goto out_put_port_power;
885 if (!tc_phy_is_ready(tc) &&
886 !drm_WARN_ON(&i915->drm, tc->mode == TC_PORT_LEGACY)) {
887 drm_dbg_kms(&i915->drm, "Port %s: PHY not ready\n",
889 goto out_release_phy;
892 tc->lock_wakeref = tc_cold_block(tc);
894 if (!tc_phy_verify_legacy_or_dp_alt_mode(tc, required_lanes))
895 goto out_unblock_tc_cold;
897 intel_display_power_put(i915, port_power_domain, port_wakeref);
902 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
904 adlp_tc_phy_take_ownership(tc, false);
906 intel_display_power_put(i915, port_power_domain, port_wakeref);
911 static void adlp_tc_phy_disconnect(struct intel_tc_port *tc)
913 struct drm_i915_private *i915 = tc_to_i915(tc);
914 enum intel_display_power_domain port_power_domain =
915 tc_port_power_domain(tc);
916 intel_wakeref_t port_wakeref;
918 port_wakeref = intel_display_power_get(i915, port_power_domain);
920 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
925 adlp_tc_phy_take_ownership(tc, false);
927 case TC_PORT_TBT_ALT:
930 MISSING_CASE(tc->mode);
933 intel_display_power_put(i915, port_power_domain, port_wakeref);
936 static void adlp_tc_phy_init(struct intel_tc_port *tc)
938 tc_phy_load_fia_params(tc, true);
941 static const struct intel_tc_phy_ops adlp_tc_phy_ops = {
942 .cold_off_domain = adlp_tc_phy_cold_off_domain,
943 .hpd_live_status = adlp_tc_phy_hpd_live_status,
944 .is_ready = adlp_tc_phy_is_ready,
945 .is_owned = adlp_tc_phy_is_owned,
946 .get_hw_state = adlp_tc_phy_get_hw_state,
947 .connect = adlp_tc_phy_connect,
948 .disconnect = adlp_tc_phy_disconnect,
949 .init = adlp_tc_phy_init,
953 * XELPDP TC PHY handlers
954 * ----------------------
956 static u32 xelpdp_tc_phy_hpd_live_status(struct intel_tc_port *tc)
958 struct drm_i915_private *i915 = tc_to_i915(tc);
959 struct intel_digital_port *dig_port = tc->dig_port;
960 enum hpd_pin hpd_pin = dig_port->base.hpd_pin;
961 u32 pica_isr_bits = i915->display.hotplug.hpd[hpd_pin];
962 u32 pch_isr_bit = i915->display.hotplug.pch_hpd[hpd_pin];
963 intel_wakeref_t wakeref;
968 with_intel_display_power(i915, POWER_DOMAIN_DISPLAY_CORE, wakeref) {
969 pica_isr = intel_de_read(i915, PICAINTERRUPT_ISR);
970 pch_isr = intel_de_read(i915, SDEISR);
973 if (pica_isr & (pica_isr_bits & XELPDP_DP_ALT_HOTPLUG_MASK))
974 mask |= BIT(TC_PORT_DP_ALT);
975 if (pica_isr & (pica_isr_bits & XELPDP_TBT_HOTPLUG_MASK))
976 mask |= BIT(TC_PORT_TBT_ALT);
978 if (tc->legacy_port && (pch_isr & pch_isr_bit))
979 mask |= BIT(TC_PORT_LEGACY);
985 xelpdp_tc_phy_tcss_power_is_enabled(struct intel_tc_port *tc)
987 struct drm_i915_private *i915 = tc_to_i915(tc);
988 enum port port = tc->dig_port->base.port;
990 assert_tc_cold_blocked(tc);
992 return intel_de_read(i915, XELPDP_PORT_BUF_CTL1(port)) & XELPDP_TCSS_POWER_STATE;
996 xelpdp_tc_phy_wait_for_tcss_power(struct intel_tc_port *tc, bool enabled)
998 struct drm_i915_private *i915 = tc_to_i915(tc);
1000 if (wait_for(xelpdp_tc_phy_tcss_power_is_enabled(tc) == enabled, 5)) {
1001 drm_dbg_kms(&i915->drm,
1002 "Port %s: timeout waiting for TCSS power to get %s\n",
1003 enabled ? "enabled" : "disabled",
1011 static void __xelpdp_tc_phy_enable_tcss_power(struct intel_tc_port *tc, bool enable)
1013 struct drm_i915_private *i915 = tc_to_i915(tc);
1014 enum port port = tc->dig_port->base.port;
1017 assert_tc_cold_blocked(tc);
1019 val = intel_de_read(i915, XELPDP_PORT_BUF_CTL1(port));
1021 val |= XELPDP_TCSS_POWER_REQUEST;
1023 val &= ~XELPDP_TCSS_POWER_REQUEST;
1024 intel_de_write(i915, XELPDP_PORT_BUF_CTL1(port), val);
1027 static bool xelpdp_tc_phy_enable_tcss_power(struct intel_tc_port *tc, bool enable)
1029 struct drm_i915_private *i915 = tc_to_i915(tc);
1031 __xelpdp_tc_phy_enable_tcss_power(tc, enable);
1033 if ((!tc_phy_wait_for_ready(tc) ||
1034 !xelpdp_tc_phy_wait_for_tcss_power(tc, enable)) &&
1035 !drm_WARN_ON(&i915->drm, tc->mode == TC_PORT_LEGACY)) {
1037 __xelpdp_tc_phy_enable_tcss_power(tc, false);
1038 xelpdp_tc_phy_wait_for_tcss_power(tc, false);
1047 static void xelpdp_tc_phy_take_ownership(struct intel_tc_port *tc, bool take)
1049 struct drm_i915_private *i915 = tc_to_i915(tc);
1050 enum port port = tc->dig_port->base.port;
1053 assert_tc_cold_blocked(tc);
1055 val = intel_de_read(i915, XELPDP_PORT_BUF_CTL1(port));
1057 val |= XELPDP_TC_PHY_OWNERSHIP;
1059 val &= ~XELPDP_TC_PHY_OWNERSHIP;
1060 intel_de_write(i915, XELPDP_PORT_BUF_CTL1(port), val);
1063 static bool xelpdp_tc_phy_is_owned(struct intel_tc_port *tc)
1065 struct drm_i915_private *i915 = tc_to_i915(tc);
1066 enum port port = tc->dig_port->base.port;
1068 assert_tc_cold_blocked(tc);
1070 return intel_de_read(i915, XELPDP_PORT_BUF_CTL1(port)) & XELPDP_TC_PHY_OWNERSHIP;
1073 static void xelpdp_tc_phy_get_hw_state(struct intel_tc_port *tc)
1075 struct drm_i915_private *i915 = tc_to_i915(tc);
1076 intel_wakeref_t tc_cold_wref;
1077 enum intel_display_power_domain domain;
1079 tc_cold_wref = __tc_cold_block(tc, &domain);
1081 tc->mode = tc_phy_get_current_mode(tc);
1082 if (tc->mode != TC_PORT_DISCONNECTED)
1083 tc->lock_wakeref = tc_cold_block(tc);
1085 drm_WARN_ON(&i915->drm,
1086 (tc->mode == TC_PORT_DP_ALT || tc->mode == TC_PORT_LEGACY) &&
1087 !xelpdp_tc_phy_tcss_power_is_enabled(tc));
1089 __tc_cold_unblock(tc, domain, tc_cold_wref);
1092 static bool xelpdp_tc_phy_connect(struct intel_tc_port *tc, int required_lanes)
1094 tc->lock_wakeref = tc_cold_block(tc);
1096 if (tc->mode == TC_PORT_TBT_ALT)
1099 if (!xelpdp_tc_phy_enable_tcss_power(tc, true))
1100 goto out_unblock_tccold;
1102 xelpdp_tc_phy_take_ownership(tc, true);
1104 if (!tc_phy_verify_legacy_or_dp_alt_mode(tc, required_lanes))
1105 goto out_release_phy;
1110 xelpdp_tc_phy_take_ownership(tc, false);
1111 xelpdp_tc_phy_wait_for_tcss_power(tc, false);
1114 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
1119 static void xelpdp_tc_phy_disconnect(struct intel_tc_port *tc)
1122 case TC_PORT_LEGACY:
1123 case TC_PORT_DP_ALT:
1124 xelpdp_tc_phy_take_ownership(tc, false);
1125 xelpdp_tc_phy_enable_tcss_power(tc, false);
1127 case TC_PORT_TBT_ALT:
1128 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
1131 MISSING_CASE(tc->mode);
1135 static const struct intel_tc_phy_ops xelpdp_tc_phy_ops = {
1136 .cold_off_domain = tgl_tc_phy_cold_off_domain,
1137 .hpd_live_status = xelpdp_tc_phy_hpd_live_status,
1138 .is_ready = adlp_tc_phy_is_ready,
1139 .is_owned = xelpdp_tc_phy_is_owned,
1140 .get_hw_state = xelpdp_tc_phy_get_hw_state,
1141 .connect = xelpdp_tc_phy_connect,
1142 .disconnect = xelpdp_tc_phy_disconnect,
1143 .init = adlp_tc_phy_init,
1147 * Generic TC PHY handlers
1148 * -----------------------
1150 static enum intel_display_power_domain
1151 tc_phy_cold_off_domain(struct intel_tc_port *tc)
1153 return tc->phy_ops->cold_off_domain(tc);
1156 static u32 tc_phy_hpd_live_status(struct intel_tc_port *tc)
1158 struct drm_i915_private *i915 = tc_to_i915(tc);
1161 mask = tc->phy_ops->hpd_live_status(tc);
1163 /* The sink can be connected only in a single mode. */
1164 drm_WARN_ON_ONCE(&i915->drm, hweight32(mask) > 1);
1169 static bool tc_phy_is_ready(struct intel_tc_port *tc)
1171 return tc->phy_ops->is_ready(tc);
1174 static bool tc_phy_is_owned(struct intel_tc_port *tc)
1176 return tc->phy_ops->is_owned(tc);
1179 static void tc_phy_get_hw_state(struct intel_tc_port *tc)
1181 tc->phy_ops->get_hw_state(tc);
1184 static bool tc_phy_is_ready_and_owned(struct intel_tc_port *tc,
1185 bool phy_is_ready, bool phy_is_owned)
1187 struct drm_i915_private *i915 = tc_to_i915(tc);
1189 drm_WARN_ON(&i915->drm, phy_is_owned && !phy_is_ready);
1191 return phy_is_ready && phy_is_owned;
1194 static bool tc_phy_is_connected(struct intel_tc_port *tc,
1195 enum icl_port_dpll_id port_pll_type)
1197 struct intel_encoder *encoder = &tc->dig_port->base;
1198 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1199 bool phy_is_ready = tc_phy_is_ready(tc);
1200 bool phy_is_owned = tc_phy_is_owned(tc);
1203 if (tc_phy_is_ready_and_owned(tc, phy_is_ready, phy_is_owned))
1204 is_connected = port_pll_type == ICL_PORT_DPLL_MG_PHY;
1206 is_connected = port_pll_type == ICL_PORT_DPLL_DEFAULT;
1208 drm_dbg_kms(&i915->drm,
1209 "Port %s: PHY connected: %s (ready: %s, owned: %s, pll_type: %s)\n",
1211 str_yes_no(is_connected),
1212 str_yes_no(phy_is_ready),
1213 str_yes_no(phy_is_owned),
1214 port_pll_type == ICL_PORT_DPLL_DEFAULT ? "tbt" : "non-tbt");
1216 return is_connected;
1219 static bool tc_phy_wait_for_ready(struct intel_tc_port *tc)
1221 struct drm_i915_private *i915 = tc_to_i915(tc);
1223 if (wait_for(tc_phy_is_ready(tc), 500)) {
1224 drm_err(&i915->drm, "Port %s: timeout waiting for PHY ready\n",
1233 static enum tc_port_mode
1234 hpd_mask_to_tc_mode(u32 live_status_mask)
1236 if (live_status_mask)
1237 return fls(live_status_mask) - 1;
1239 return TC_PORT_DISCONNECTED;
1242 static enum tc_port_mode
1243 tc_phy_hpd_live_mode(struct intel_tc_port *tc)
1245 u32 live_status_mask = tc_phy_hpd_live_status(tc);
1247 return hpd_mask_to_tc_mode(live_status_mask);
1250 static enum tc_port_mode
1251 get_tc_mode_in_phy_owned_state(struct intel_tc_port *tc,
1252 enum tc_port_mode live_mode)
1254 switch (live_mode) {
1255 case TC_PORT_LEGACY:
1256 case TC_PORT_DP_ALT:
1259 MISSING_CASE(live_mode);
1261 case TC_PORT_TBT_ALT:
1262 case TC_PORT_DISCONNECTED:
1263 if (tc->legacy_port)
1264 return TC_PORT_LEGACY;
1266 return TC_PORT_DP_ALT;
1270 static enum tc_port_mode
1271 get_tc_mode_in_phy_not_owned_state(struct intel_tc_port *tc,
1272 enum tc_port_mode live_mode)
1274 switch (live_mode) {
1275 case TC_PORT_LEGACY:
1276 return TC_PORT_DISCONNECTED;
1277 case TC_PORT_DP_ALT:
1278 case TC_PORT_TBT_ALT:
1279 return TC_PORT_TBT_ALT;
1281 MISSING_CASE(live_mode);
1283 case TC_PORT_DISCONNECTED:
1284 if (tc->legacy_port)
1285 return TC_PORT_DISCONNECTED;
1287 return TC_PORT_TBT_ALT;
1291 static enum tc_port_mode
1292 tc_phy_get_current_mode(struct intel_tc_port *tc)
1294 struct drm_i915_private *i915 = tc_to_i915(tc);
1295 enum tc_port_mode live_mode = tc_phy_hpd_live_mode(tc);
1298 enum tc_port_mode mode;
1301 * For legacy ports the IOM firmware initializes the PHY during boot-up
1302 * and system resume whether or not a sink is connected. Wait here for
1303 * the initialization to get ready.
1305 if (tc->legacy_port)
1306 tc_phy_wait_for_ready(tc);
1308 phy_is_ready = tc_phy_is_ready(tc);
1309 phy_is_owned = tc_phy_is_owned(tc);
1311 if (!tc_phy_is_ready_and_owned(tc, phy_is_ready, phy_is_owned)) {
1312 mode = get_tc_mode_in_phy_not_owned_state(tc, live_mode);
1314 drm_WARN_ON(&i915->drm, live_mode == TC_PORT_TBT_ALT);
1315 mode = get_tc_mode_in_phy_owned_state(tc, live_mode);
1318 drm_dbg_kms(&i915->drm,
1319 "Port %s: PHY mode: %s (ready: %s, owned: %s, HPD: %s)\n",
1321 tc_port_mode_name(mode),
1322 str_yes_no(phy_is_ready),
1323 str_yes_no(phy_is_owned),
1324 tc_port_mode_name(live_mode));
1329 static enum tc_port_mode default_tc_mode(struct intel_tc_port *tc)
1331 if (tc->legacy_port)
1332 return TC_PORT_LEGACY;
1334 return TC_PORT_TBT_ALT;
1337 static enum tc_port_mode
1338 hpd_mask_to_target_mode(struct intel_tc_port *tc, u32 live_status_mask)
1340 enum tc_port_mode mode = hpd_mask_to_tc_mode(live_status_mask);
1342 if (mode != TC_PORT_DISCONNECTED)
1345 return default_tc_mode(tc);
1348 static enum tc_port_mode
1349 tc_phy_get_target_mode(struct intel_tc_port *tc)
1351 u32 live_status_mask = tc_phy_hpd_live_status(tc);
1353 return hpd_mask_to_target_mode(tc, live_status_mask);
1356 static void tc_phy_connect(struct intel_tc_port *tc, int required_lanes)
1358 struct drm_i915_private *i915 = tc_to_i915(tc);
1359 u32 live_status_mask = tc_phy_hpd_live_status(tc);
1362 tc_port_fixup_legacy_flag(tc, live_status_mask);
1364 tc->mode = hpd_mask_to_target_mode(tc, live_status_mask);
1366 connected = tc->phy_ops->connect(tc, required_lanes);
1367 if (!connected && tc->mode != default_tc_mode(tc)) {
1368 tc->mode = default_tc_mode(tc);
1369 connected = tc->phy_ops->connect(tc, required_lanes);
1372 drm_WARN_ON(&i915->drm, !connected);
1375 static void tc_phy_disconnect(struct intel_tc_port *tc)
1377 if (tc->mode != TC_PORT_DISCONNECTED) {
1378 tc->phy_ops->disconnect(tc);
1379 tc->mode = TC_PORT_DISCONNECTED;
1383 static void tc_phy_init(struct intel_tc_port *tc)
1385 mutex_lock(&tc->lock);
1386 tc->phy_ops->init(tc);
1387 mutex_unlock(&tc->lock);
1390 static void intel_tc_port_reset_mode(struct intel_tc_port *tc,
1391 int required_lanes, bool force_disconnect)
1393 struct drm_i915_private *i915 = tc_to_i915(tc);
1394 struct intel_digital_port *dig_port = tc->dig_port;
1395 enum tc_port_mode old_tc_mode = tc->mode;
1397 intel_display_power_flush_work(i915);
1398 if (!intel_tc_cold_requires_aux_pw(dig_port)) {
1399 enum intel_display_power_domain aux_domain;
1402 aux_domain = intel_aux_power_domain(dig_port);
1403 aux_powered = intel_display_power_is_enabled(i915, aux_domain);
1404 drm_WARN_ON(&i915->drm, aux_powered);
1407 tc_phy_disconnect(tc);
1408 if (!force_disconnect)
1409 tc_phy_connect(tc, required_lanes);
1411 drm_dbg_kms(&i915->drm, "Port %s: TC port mode reset (%s -> %s)\n",
1413 tc_port_mode_name(old_tc_mode),
1414 tc_port_mode_name(tc->mode));
1417 static bool intel_tc_port_needs_reset(struct intel_tc_port *tc)
1419 return tc_phy_get_target_mode(tc) != tc->mode;
1422 static void intel_tc_port_update_mode(struct intel_tc_port *tc,
1423 int required_lanes, bool force_disconnect)
1425 if (force_disconnect ||
1426 intel_tc_port_needs_reset(tc))
1427 intel_tc_port_reset_mode(tc, required_lanes, force_disconnect);
1430 static void __intel_tc_port_get_link(struct intel_tc_port *tc)
1432 tc->link_refcount++;
1435 static void __intel_tc_port_put_link(struct intel_tc_port *tc)
1437 tc->link_refcount--;
1440 static bool tc_port_is_enabled(struct intel_tc_port *tc)
1442 struct drm_i915_private *i915 = tc_to_i915(tc);
1443 struct intel_digital_port *dig_port = tc->dig_port;
1445 assert_tc_port_power_enabled(tc);
1447 return intel_de_read(i915, DDI_BUF_CTL(dig_port->base.port)) &
1452 * intel_tc_port_init_mode: Read out HW state and init the given port's TypeC mode
1453 * @dig_port: digital port
1455 * Read out the HW state and initialize the TypeC mode of @dig_port. The mode
1456 * will be locked until intel_tc_port_sanitize_mode() is called.
1458 void intel_tc_port_init_mode(struct intel_digital_port *dig_port)
1460 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1461 struct intel_tc_port *tc = to_tc_port(dig_port);
1462 bool update_mode = false;
1464 mutex_lock(&tc->lock);
1466 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_DISCONNECTED);
1467 drm_WARN_ON(&i915->drm, tc->lock_wakeref);
1468 drm_WARN_ON(&i915->drm, tc->link_refcount);
1470 tc_phy_get_hw_state(tc);
1472 * Save the initial mode for the state check in
1473 * intel_tc_port_sanitize_mode().
1475 tc->init_mode = tc->mode;
1478 * The PHY needs to be connected for AUX to work during HW readout and
1479 * MST topology resume, but the PHY mode can only be changed if the
1482 * An exception is the case where BIOS leaves the PHY incorrectly
1483 * disconnected on an enabled legacy port. Work around that by
1484 * connecting the PHY even though the port is enabled. This doesn't
1485 * cause a problem as the PHY ownership state is ignored by the
1486 * IOM/TCSS firmware (only display can own the PHY in that case).
1488 if (!tc_port_is_enabled(tc)) {
1490 } else if (tc->mode == TC_PORT_DISCONNECTED) {
1491 drm_WARN_ON(&i915->drm, !tc->legacy_port);
1493 "Port %s: PHY disconnected on enabled port, connecting it\n",
1499 intel_tc_port_update_mode(tc, 1, false);
1501 /* Prevent changing tc->mode until intel_tc_port_sanitize_mode() is called. */
1502 __intel_tc_port_get_link(tc);
1504 mutex_unlock(&tc->lock);
1507 static bool tc_port_has_active_links(struct intel_tc_port *tc,
1508 const struct intel_crtc_state *crtc_state)
1510 struct drm_i915_private *i915 = tc_to_i915(tc);
1511 struct intel_digital_port *dig_port = tc->dig_port;
1512 enum icl_port_dpll_id pll_type = ICL_PORT_DPLL_DEFAULT;
1513 int active_links = 0;
1515 if (dig_port->dp.is_mst) {
1516 /* TODO: get the PLL type for MST, once HW readout is done for it. */
1517 active_links = intel_dp_mst_encoder_active_links(dig_port);
1518 } else if (crtc_state && crtc_state->hw.active) {
1519 pll_type = intel_ddi_port_pll_type(&dig_port->base, crtc_state);
1523 if (active_links && !tc_phy_is_connected(tc, pll_type))
1525 "Port %s: PHY disconnected with %d active link(s)\n",
1526 tc->port_name, active_links);
1528 return active_links;
1532 * intel_tc_port_sanitize_mode: Sanitize the given port's TypeC mode
1533 * @dig_port: digital port
1534 * @crtc_state: atomic state of CRTC connected to @dig_port
1536 * Sanitize @dig_port's TypeC mode wrt. the encoder's state right after driver
1537 * loading and system resume:
1538 * If the encoder is enabled keep the TypeC mode/PHY connected state locked until
1539 * the encoder is disabled.
1540 * If the encoder is disabled make sure the PHY is disconnected.
1541 * @crtc_state is valid if @dig_port is enabled, NULL otherwise.
1543 void intel_tc_port_sanitize_mode(struct intel_digital_port *dig_port,
1544 const struct intel_crtc_state *crtc_state)
1546 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1547 struct intel_tc_port *tc = to_tc_port(dig_port);
1549 mutex_lock(&tc->lock);
1551 drm_WARN_ON(&i915->drm, tc->link_refcount != 1);
1552 if (!tc_port_has_active_links(tc, crtc_state)) {
1554 * TBT-alt is the default mode in any case the PHY ownership is not
1555 * held (regardless of the sink's connected live state), so
1556 * we'll just switch to disconnected mode from it here without
1559 if (tc->init_mode != TC_PORT_TBT_ALT &&
1560 tc->init_mode != TC_PORT_DISCONNECTED)
1561 drm_dbg_kms(&i915->drm,
1562 "Port %s: PHY left in %s mode on disabled port, disconnecting it\n",
1564 tc_port_mode_name(tc->init_mode));
1565 tc_phy_disconnect(tc);
1566 __intel_tc_port_put_link(tc);
1569 drm_dbg_kms(&i915->drm, "Port %s: sanitize mode (%s)\n",
1571 tc_port_mode_name(tc->mode));
1573 mutex_unlock(&tc->lock);
1577 * The type-C ports are different because even when they are connected, they may
1578 * not be available/usable by the graphics driver: see the comment on
1579 * icl_tc_phy_connect(). So in our driver instead of adding the additional
1580 * concept of "usable" and make everything check for "connected and usable" we
1581 * define a port as "connected" when it is not only connected, but also when it
1582 * is usable by the rest of the driver. That maintains the old assumption that
1583 * connected ports are usable, and avoids exposing to the users objects they
1586 bool intel_tc_port_connected_locked(struct intel_encoder *encoder)
1588 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1589 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1590 struct intel_tc_port *tc = to_tc_port(dig_port);
1593 drm_WARN_ON(&i915->drm, !intel_tc_port_ref_held(dig_port));
1595 if (tc->mode != TC_PORT_DISCONNECTED)
1596 mask = BIT(tc->mode);
1598 return tc_phy_hpd_live_status(tc) & mask;
1601 bool intel_tc_port_connected(struct intel_encoder *encoder)
1603 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1604 struct intel_tc_port *tc = to_tc_port(dig_port);
1607 mutex_lock(&tc->lock);
1608 is_connected = intel_tc_port_connected_locked(encoder);
1609 mutex_unlock(&tc->lock);
1611 return is_connected;
1614 static bool __intel_tc_port_link_needs_reset(struct intel_tc_port *tc)
1618 mutex_lock(&tc->lock);
1620 ret = tc->link_refcount &&
1621 tc->mode == TC_PORT_DP_ALT &&
1622 intel_tc_port_needs_reset(tc);
1624 mutex_unlock(&tc->lock);
1629 bool intel_tc_port_link_needs_reset(struct intel_digital_port *dig_port)
1631 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1632 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
1634 if (!intel_phy_is_tc(i915, phy))
1637 return __intel_tc_port_link_needs_reset(to_tc_port(dig_port));
1640 static int reset_link_commit(struct intel_tc_port *tc,
1641 struct intel_atomic_state *state,
1642 struct drm_modeset_acquire_ctx *ctx)
1644 struct drm_i915_private *i915 = tc_to_i915(tc);
1645 struct intel_digital_port *dig_port = tc->dig_port;
1646 struct intel_dp *intel_dp = enc_to_intel_dp(&dig_port->base);
1647 struct intel_crtc *crtc;
1651 ret = drm_modeset_lock(&i915->drm.mode_config.connection_mutex, ctx);
1655 ret = intel_dp_get_active_pipes(intel_dp, ctx, &pipe_mask);
1662 for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc, pipe_mask) {
1663 struct intel_crtc_state *crtc_state;
1665 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
1666 if (IS_ERR(crtc_state))
1667 return PTR_ERR(crtc_state);
1669 crtc_state->uapi.connectors_changed = true;
1672 if (!__intel_tc_port_link_needs_reset(tc))
1675 return drm_atomic_commit(&state->base);
1678 static int reset_link(struct intel_tc_port *tc)
1680 struct drm_i915_private *i915 = tc_to_i915(tc);
1681 struct drm_modeset_acquire_ctx ctx;
1682 struct drm_atomic_state *_state;
1683 struct intel_atomic_state *state;
1686 _state = drm_atomic_state_alloc(&i915->drm);
1690 state = to_intel_atomic_state(_state);
1691 state->internal = true;
1693 intel_modeset_lock_ctx_retry(&ctx, state, 0, ret)
1694 ret = reset_link_commit(tc, state, &ctx);
1696 drm_atomic_state_put(&state->base);
1701 static void intel_tc_port_link_reset_work(struct work_struct *work)
1703 struct intel_tc_port *tc =
1704 container_of(work, struct intel_tc_port, link_reset_work.work);
1705 struct drm_i915_private *i915 = tc_to_i915(tc);
1708 if (!__intel_tc_port_link_needs_reset(tc))
1711 mutex_lock(&i915->drm.mode_config.mutex);
1713 drm_dbg_kms(&i915->drm,
1714 "Port %s: TypeC DP-alt sink disconnected, resetting link\n",
1716 ret = reset_link(tc);
1717 drm_WARN_ON(&i915->drm, ret);
1719 mutex_unlock(&i915->drm.mode_config.mutex);
1722 bool intel_tc_port_link_reset(struct intel_digital_port *dig_port)
1724 if (!intel_tc_port_link_needs_reset(dig_port))
1727 queue_delayed_work(system_unbound_wq,
1728 &to_tc_port(dig_port)->link_reset_work,
1729 msecs_to_jiffies(2000));
1734 void intel_tc_port_link_cancel_reset_work(struct intel_digital_port *dig_port)
1736 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1737 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
1738 struct intel_tc_port *tc = to_tc_port(dig_port);
1740 if (!intel_phy_is_tc(i915, phy))
1743 cancel_delayed_work(&tc->link_reset_work);
1746 static void __intel_tc_port_lock(struct intel_tc_port *tc,
1749 struct drm_i915_private *i915 = tc_to_i915(tc);
1751 mutex_lock(&tc->lock);
1753 cancel_delayed_work(&tc->disconnect_phy_work);
1755 if (!tc->link_refcount)
1756 intel_tc_port_update_mode(tc, required_lanes,
1759 drm_WARN_ON(&i915->drm, tc->mode == TC_PORT_DISCONNECTED);
1760 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_TBT_ALT &&
1761 !tc_phy_is_owned(tc));
1764 void intel_tc_port_lock(struct intel_digital_port *dig_port)
1766 __intel_tc_port_lock(to_tc_port(dig_port), 1);
1770 * Disconnect the given digital port from its TypeC PHY (handing back the
1771 * control of the PHY to the TypeC subsystem). This will happen in a delayed
1772 * manner after each aux transactions and modeset disables.
1774 static void intel_tc_port_disconnect_phy_work(struct work_struct *work)
1776 struct intel_tc_port *tc =
1777 container_of(work, struct intel_tc_port, disconnect_phy_work.work);
1779 mutex_lock(&tc->lock);
1781 if (!tc->link_refcount)
1782 intel_tc_port_update_mode(tc, 1, true);
1784 mutex_unlock(&tc->lock);
1788 * intel_tc_port_flush_work: flush the work disconnecting the PHY
1789 * @dig_port: digital port
1791 * Flush the delayed work disconnecting an idle PHY.
1793 static void intel_tc_port_flush_work(struct intel_digital_port *dig_port)
1795 flush_delayed_work(&to_tc_port(dig_port)->disconnect_phy_work);
1798 void intel_tc_port_suspend(struct intel_digital_port *dig_port)
1800 struct intel_tc_port *tc = to_tc_port(dig_port);
1802 cancel_delayed_work_sync(&tc->link_reset_work);
1803 intel_tc_port_flush_work(dig_port);
1806 void intel_tc_port_unlock(struct intel_digital_port *dig_port)
1808 struct intel_tc_port *tc = to_tc_port(dig_port);
1810 if (!tc->link_refcount && tc->mode != TC_PORT_DISCONNECTED)
1811 queue_delayed_work(system_unbound_wq, &tc->disconnect_phy_work,
1812 msecs_to_jiffies(1000));
1814 mutex_unlock(&tc->lock);
1817 bool intel_tc_port_ref_held(struct intel_digital_port *dig_port)
1819 struct intel_tc_port *tc = to_tc_port(dig_port);
1821 return mutex_is_locked(&tc->lock) ||
1825 void intel_tc_port_get_link(struct intel_digital_port *dig_port,
1828 struct intel_tc_port *tc = to_tc_port(dig_port);
1830 __intel_tc_port_lock(tc, required_lanes);
1831 __intel_tc_port_get_link(tc);
1832 intel_tc_port_unlock(dig_port);
1835 void intel_tc_port_put_link(struct intel_digital_port *dig_port)
1837 struct intel_tc_port *tc = to_tc_port(dig_port);
1839 intel_tc_port_lock(dig_port);
1840 __intel_tc_port_put_link(tc);
1841 intel_tc_port_unlock(dig_port);
1844 * The firmware will not update the HPD status of other TypeC ports
1845 * that are active in DP-alt mode with their sink disconnected, until
1846 * this port is disabled and its PHY gets disconnected. Make sure this
1847 * happens in a timely manner by disconnecting the PHY synchronously.
1849 intel_tc_port_flush_work(dig_port);
1852 int intel_tc_port_init(struct intel_digital_port *dig_port, bool is_legacy)
1854 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1855 struct intel_tc_port *tc;
1856 enum port port = dig_port->base.port;
1857 enum tc_port tc_port = intel_port_to_tc(i915, port);
1859 if (drm_WARN_ON(&i915->drm, tc_port == TC_PORT_NONE))
1862 tc = kzalloc(sizeof(*tc), GFP_KERNEL);
1867 tc->dig_port = dig_port;
1869 if (DISPLAY_VER(i915) >= 14)
1870 tc->phy_ops = &xelpdp_tc_phy_ops;
1871 else if (DISPLAY_VER(i915) >= 13)
1872 tc->phy_ops = &adlp_tc_phy_ops;
1873 else if (DISPLAY_VER(i915) >= 12)
1874 tc->phy_ops = &tgl_tc_phy_ops;
1876 tc->phy_ops = &icl_tc_phy_ops;
1878 tc->port_name = kasprintf(GFP_KERNEL, "%c/TC#%d", port_name(port),
1880 if (!tc->port_name) {
1885 mutex_init(&tc->lock);
1886 /* TODO: Combine the two works */
1887 INIT_DELAYED_WORK(&tc->disconnect_phy_work, intel_tc_port_disconnect_phy_work);
1888 INIT_DELAYED_WORK(&tc->link_reset_work, intel_tc_port_link_reset_work);
1889 tc->legacy_port = is_legacy;
1890 tc->mode = TC_PORT_DISCONNECTED;
1891 tc->link_refcount = 0;
1895 intel_tc_port_init_mode(dig_port);
1900 void intel_tc_port_cleanup(struct intel_digital_port *dig_port)
1902 intel_tc_port_suspend(dig_port);
1904 kfree(dig_port->tc->port_name);
1905 kfree(dig_port->tc);
1906 dig_port->tc = NULL;