1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
10 #include "intel_display.h"
11 #include "intel_display_power_map.h"
12 #include "intel_display_types.h"
13 #include "intel_dkl_phy_regs.h"
14 #include "intel_dp_mst.h"
15 #include "intel_mg_phy_regs.h"
27 struct intel_tc_phy_ops {
28 u32 (*hpd_live_status)(struct intel_tc_port *tc);
29 bool (*is_ready)(struct intel_tc_port *tc);
30 bool (*is_owned)(struct intel_tc_port *tc);
31 void (*get_hw_state)(struct intel_tc_port *tc);
32 bool (*connect)(struct intel_tc_port *tc, int required_lanes);
33 void (*disconnect)(struct intel_tc_port *tc);
36 struct intel_tc_port {
37 struct intel_digital_port *dig_port;
39 const struct intel_tc_phy_ops *phy_ops;
41 struct mutex lock; /* protects the TypeC port mode */
42 intel_wakeref_t lock_wakeref;
43 enum intel_display_power_domain lock_power_domain;
44 struct delayed_work disconnect_phy_work;
48 enum tc_port_mode mode;
49 enum tc_port_mode init_mode;
54 static u32 tc_phy_hpd_live_status(struct intel_tc_port *tc);
55 static bool tc_phy_is_ready(struct intel_tc_port *tc);
56 static bool tc_phy_take_ownership(struct intel_tc_port *tc, bool take);
57 static enum tc_port_mode tc_phy_get_current_mode(struct intel_tc_port *tc);
59 static const char *tc_port_mode_name(enum tc_port_mode mode)
61 static const char * const names[] = {
62 [TC_PORT_DISCONNECTED] = "disconnected",
63 [TC_PORT_TBT_ALT] = "tbt-alt",
64 [TC_PORT_DP_ALT] = "dp-alt",
65 [TC_PORT_LEGACY] = "legacy",
68 if (WARN_ON(mode >= ARRAY_SIZE(names)))
69 mode = TC_PORT_DISCONNECTED;
74 static struct intel_tc_port *to_tc_port(struct intel_digital_port *dig_port)
79 static struct drm_i915_private *tc_to_i915(struct intel_tc_port *tc)
81 return to_i915(tc->dig_port->base.base.dev);
84 static bool intel_tc_port_in_mode(struct intel_digital_port *dig_port,
85 enum tc_port_mode mode)
87 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
88 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
89 struct intel_tc_port *tc = to_tc_port(dig_port);
91 return intel_phy_is_tc(i915, phy) && tc->mode == mode;
94 bool intel_tc_port_in_tbt_alt_mode(struct intel_digital_port *dig_port)
96 return intel_tc_port_in_mode(dig_port, TC_PORT_TBT_ALT);
99 bool intel_tc_port_in_dp_alt_mode(struct intel_digital_port *dig_port)
101 return intel_tc_port_in_mode(dig_port, TC_PORT_DP_ALT);
104 bool intel_tc_port_in_legacy_mode(struct intel_digital_port *dig_port)
106 return intel_tc_port_in_mode(dig_port, TC_PORT_LEGACY);
109 bool intel_tc_cold_requires_aux_pw(struct intel_digital_port *dig_port)
111 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
112 struct intel_tc_port *tc = to_tc_port(dig_port);
114 return (DISPLAY_VER(i915) == 11 && tc->legacy_port) ||
115 IS_ALDERLAKE_P(i915);
118 static enum intel_display_power_domain
119 tc_cold_get_power_domain(struct intel_tc_port *tc, enum tc_port_mode mode)
121 struct drm_i915_private *i915 = tc_to_i915(tc);
122 struct intel_digital_port *dig_port = tc->dig_port;
124 if (mode == TC_PORT_TBT_ALT || !intel_tc_cold_requires_aux_pw(dig_port))
125 return POWER_DOMAIN_TC_COLD_OFF;
127 return intel_display_power_legacy_aux_domain(i915, dig_port->aux_ch);
130 static intel_wakeref_t
131 tc_cold_block_in_mode(struct intel_tc_port *tc, enum tc_port_mode mode,
132 enum intel_display_power_domain *domain)
134 struct drm_i915_private *i915 = tc_to_i915(tc);
136 *domain = tc_cold_get_power_domain(tc, mode);
138 return intel_display_power_get(i915, *domain);
141 static intel_wakeref_t
142 tc_cold_block(struct intel_tc_port *tc, enum intel_display_power_domain *domain)
144 return tc_cold_block_in_mode(tc, tc->mode, domain);
148 tc_cold_unblock(struct intel_tc_port *tc, enum intel_display_power_domain domain,
149 intel_wakeref_t wakeref)
151 struct drm_i915_private *i915 = tc_to_i915(tc);
154 * wakeref == -1, means some error happened saving save_depot_stack but
155 * power should still be put down and 0 is a invalid save_depot_stack
156 * id so can be used to skip it for non TC legacy ports.
161 intel_display_power_put(i915, domain, wakeref);
165 assert_tc_cold_blocked(struct intel_tc_port *tc)
167 struct drm_i915_private *i915 = tc_to_i915(tc);
170 enabled = intel_display_power_is_enabled(i915,
171 tc_cold_get_power_domain(tc,
173 drm_WARN_ON(&i915->drm, !enabled);
176 static enum intel_display_power_domain
177 tc_port_power_domain(struct intel_tc_port *tc)
179 struct drm_i915_private *i915 = tc_to_i915(tc);
180 enum tc_port tc_port = intel_port_to_tc(i915, tc->dig_port->base.port);
182 return POWER_DOMAIN_PORT_DDI_LANES_TC1 + tc_port - TC_PORT_1;
186 assert_tc_port_power_enabled(struct intel_tc_port *tc)
188 struct drm_i915_private *i915 = tc_to_i915(tc);
190 drm_WARN_ON(&i915->drm,
191 !intel_display_power_is_enabled(i915, tc_port_power_domain(tc)));
194 u32 intel_tc_port_get_lane_mask(struct intel_digital_port *dig_port)
196 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
197 struct intel_tc_port *tc = to_tc_port(dig_port);
200 lane_mask = intel_de_read(i915, PORT_TX_DFLEXDPSP(tc->phy_fia));
202 drm_WARN_ON(&i915->drm, lane_mask == 0xffffffff);
203 assert_tc_cold_blocked(tc);
205 lane_mask &= DP_LANE_ASSIGNMENT_MASK(tc->phy_fia_idx);
206 return lane_mask >> DP_LANE_ASSIGNMENT_SHIFT(tc->phy_fia_idx);
209 u32 intel_tc_port_get_pin_assignment_mask(struct intel_digital_port *dig_port)
211 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
212 struct intel_tc_port *tc = to_tc_port(dig_port);
215 pin_mask = intel_de_read(i915, PORT_TX_DFLEXPA1(tc->phy_fia));
217 drm_WARN_ON(&i915->drm, pin_mask == 0xffffffff);
218 assert_tc_cold_blocked(tc);
220 return (pin_mask & DP_PIN_ASSIGNMENT_MASK(tc->phy_fia_idx)) >>
221 DP_PIN_ASSIGNMENT_SHIFT(tc->phy_fia_idx);
224 int intel_tc_port_fia_max_lane_count(struct intel_digital_port *dig_port)
226 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
227 struct intel_tc_port *tc = to_tc_port(dig_port);
228 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
229 intel_wakeref_t wakeref;
232 if (!intel_phy_is_tc(i915, phy) || tc->mode != TC_PORT_DP_ALT)
235 assert_tc_cold_blocked(tc);
238 with_intel_display_power(i915, POWER_DOMAIN_DISPLAY_CORE, wakeref)
239 lane_mask = intel_tc_port_get_lane_mask(dig_port);
243 MISSING_CASE(lane_mask);
258 void intel_tc_port_set_fia_lane_count(struct intel_digital_port *dig_port,
261 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
262 struct intel_tc_port *tc = to_tc_port(dig_port);
263 bool lane_reversal = dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
266 drm_WARN_ON(&i915->drm,
267 lane_reversal && tc->mode != TC_PORT_LEGACY);
269 assert_tc_cold_blocked(tc);
271 val = intel_de_read(i915, PORT_TX_DFLEXDPMLE1(tc->phy_fia));
272 val &= ~DFLEXDPMLE1_DPMLETC_MASK(tc->phy_fia_idx);
274 switch (required_lanes) {
276 val |= lane_reversal ?
277 DFLEXDPMLE1_DPMLETC_ML3(tc->phy_fia_idx) :
278 DFLEXDPMLE1_DPMLETC_ML0(tc->phy_fia_idx);
281 val |= lane_reversal ?
282 DFLEXDPMLE1_DPMLETC_ML3_2(tc->phy_fia_idx) :
283 DFLEXDPMLE1_DPMLETC_ML1_0(tc->phy_fia_idx);
286 val |= DFLEXDPMLE1_DPMLETC_ML3_0(tc->phy_fia_idx);
289 MISSING_CASE(required_lanes);
292 intel_de_write(i915, PORT_TX_DFLEXDPMLE1(tc->phy_fia), val);
295 static void tc_port_fixup_legacy_flag(struct intel_tc_port *tc,
296 u32 live_status_mask)
298 struct drm_i915_private *i915 = tc_to_i915(tc);
301 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_DISCONNECTED);
303 if (hweight32(live_status_mask) != 1)
307 valid_hpd_mask = BIT(TC_PORT_LEGACY);
309 valid_hpd_mask = BIT(TC_PORT_DP_ALT) |
310 BIT(TC_PORT_TBT_ALT);
312 if (!(live_status_mask & ~valid_hpd_mask))
315 /* If live status mismatches the VBT flag, trust the live status. */
316 drm_dbg_kms(&i915->drm,
317 "Port %s: live status %08x mismatches the legacy port flag %08x, fixing flag\n",
318 tc->port_name, live_status_mask, valid_hpd_mask);
320 tc->legacy_port = !tc->legacy_port;
324 * ICL TC PHY handlers
325 * -------------------
327 static u32 icl_tc_phy_hpd_live_status(struct intel_tc_port *tc)
329 struct drm_i915_private *i915 = tc_to_i915(tc);
330 struct intel_digital_port *dig_port = tc->dig_port;
331 u32 isr_bit = i915->display.hotplug.pch_hpd[dig_port->base.hpd_pin];
335 val = intel_de_read(i915, PORT_TX_DFLEXDPSP(tc->phy_fia));
337 if (val == 0xffffffff) {
338 drm_dbg_kms(&i915->drm,
339 "Port %s: PHY in TCCOLD, nothing connected\n",
344 if (val & TC_LIVE_STATE_TBT(tc->phy_fia_idx))
345 mask |= BIT(TC_PORT_TBT_ALT);
346 if (val & TC_LIVE_STATE_TC(tc->phy_fia_idx))
347 mask |= BIT(TC_PORT_DP_ALT);
349 if (intel_de_read(i915, SDEISR) & isr_bit)
350 mask |= BIT(TC_PORT_LEGACY);
356 * Return the PHY status complete flag indicating that display can acquire the
357 * PHY ownership. The IOM firmware sets this flag when a DP-alt or legacy sink
358 * is connected and it's ready to switch the ownership to display. The flag
359 * will be left cleared when a TBT-alt sink is connected, where the PHY is
360 * owned by the TBT subsystem and so switching the ownership to display is not
363 static bool icl_tc_phy_is_ready(struct intel_tc_port *tc)
365 struct drm_i915_private *i915 = tc_to_i915(tc);
368 val = intel_de_read(i915, PORT_TX_DFLEXDPPMS(tc->phy_fia));
369 if (val == 0xffffffff) {
370 drm_dbg_kms(&i915->drm,
371 "Port %s: PHY in TCCOLD, assuming not ready\n",
376 return val & DP_PHY_MODE_STATUS_COMPLETED(tc->phy_fia_idx);
379 static bool icl_tc_phy_take_ownership(struct intel_tc_port *tc,
382 struct drm_i915_private *i915 = tc_to_i915(tc);
385 val = intel_de_read(i915, PORT_TX_DFLEXDPCSSS(tc->phy_fia));
386 if (val == 0xffffffff) {
387 drm_dbg_kms(&i915->drm,
388 "Port %s: PHY in TCCOLD, can't %s ownership\n",
389 tc->port_name, take ? "take" : "release");
394 val &= ~DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
396 val |= DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
398 intel_de_write(i915, PORT_TX_DFLEXDPCSSS(tc->phy_fia), val);
403 static bool icl_tc_phy_is_owned(struct intel_tc_port *tc)
405 struct drm_i915_private *i915 = tc_to_i915(tc);
408 val = intel_de_read(i915, PORT_TX_DFLEXDPCSSS(tc->phy_fia));
409 if (val == 0xffffffff) {
410 drm_dbg_kms(&i915->drm,
411 "Port %s: PHY in TCCOLD, assume not owned\n",
416 return val & DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
419 static void icl_tc_phy_get_hw_state(struct intel_tc_port *tc)
421 enum intel_display_power_domain domain;
422 intel_wakeref_t tc_cold_wref;
424 tc_cold_wref = tc_cold_block(tc, &domain);
426 tc->mode = tc_phy_get_current_mode(tc);
427 if (tc->mode != TC_PORT_DISCONNECTED)
428 tc->lock_wakeref = tc_cold_block(tc, &tc->lock_power_domain);
430 tc_cold_unblock(tc, domain, tc_cold_wref);
434 * This function implements the first part of the Connect Flow described by our
435 * specification, Gen11 TypeC Programming chapter. The rest of the flow (reading
436 * lanes, EDID, etc) is done as needed in the typical places.
438 * Unlike the other ports, type-C ports are not available to use as soon as we
439 * get a hotplug. The type-C PHYs can be shared between multiple controllers:
440 * display, USB, etc. As a result, handshaking through FIA is required around
441 * connect and disconnect to cleanly transfer ownership with the controller and
442 * set the type-C power state.
444 static bool tc_phy_verify_legacy_or_dp_alt_mode(struct intel_tc_port *tc,
447 struct drm_i915_private *i915 = tc_to_i915(tc);
448 struct intel_digital_port *dig_port = tc->dig_port;
451 max_lanes = intel_tc_port_fia_max_lane_count(dig_port);
452 if (tc->mode == TC_PORT_LEGACY) {
453 drm_WARN_ON(&i915->drm, max_lanes != 4);
457 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_DP_ALT);
460 * Now we have to re-check the live state, in case the port recently
461 * became disconnected. Not necessary for legacy mode.
463 if (!(tc_phy_hpd_live_status(tc) & BIT(TC_PORT_DP_ALT))) {
464 drm_dbg_kms(&i915->drm, "Port %s: PHY sudden disconnect\n",
469 if (max_lanes < required_lanes) {
470 drm_dbg_kms(&i915->drm,
471 "Port %s: PHY max lanes %d < required lanes %d\n",
473 max_lanes, required_lanes);
480 static bool icl_tc_phy_connect(struct intel_tc_port *tc,
483 struct drm_i915_private *i915 = tc_to_i915(tc);
485 if (tc->mode == TC_PORT_TBT_ALT)
488 if ((!tc_phy_is_ready(tc) ||
489 !tc_phy_take_ownership(tc, true)) &&
490 !drm_WARN_ON(&i915->drm, tc->mode == TC_PORT_LEGACY)) {
491 drm_dbg_kms(&i915->drm, "Port %s: can't take PHY ownership (ready %s)\n",
493 str_yes_no(tc_phy_is_ready(tc)));
498 if (!tc_phy_verify_legacy_or_dp_alt_mode(tc, required_lanes))
499 goto out_release_phy;
504 tc_phy_take_ownership(tc, false);
510 * See the comment at the connect function. This implements the Disconnect
513 static void icl_tc_phy_disconnect(struct intel_tc_port *tc)
518 tc_phy_take_ownership(tc, false);
520 case TC_PORT_TBT_ALT:
523 MISSING_CASE(tc->mode);
527 static const struct intel_tc_phy_ops icl_tc_phy_ops = {
528 .hpd_live_status = icl_tc_phy_hpd_live_status,
529 .is_ready = icl_tc_phy_is_ready,
530 .is_owned = icl_tc_phy_is_owned,
531 .get_hw_state = icl_tc_phy_get_hw_state,
532 .connect = icl_tc_phy_connect,
533 .disconnect = icl_tc_phy_disconnect,
537 * ADLP TC PHY handlers
538 * --------------------
540 static u32 adlp_tc_phy_hpd_live_status(struct intel_tc_port *tc)
542 struct drm_i915_private *i915 = tc_to_i915(tc);
543 struct intel_digital_port *dig_port = tc->dig_port;
544 enum tc_port tc_port = intel_port_to_tc(i915, dig_port->base.port);
545 u32 isr_bit = i915->display.hotplug.pch_hpd[dig_port->base.hpd_pin];
549 * On ADL-P HW/FW will wake from TCCOLD to complete the read access of
550 * registers in IOM. Note that this doesn't apply to PHY and FIA
553 val = intel_de_read(i915, TCSS_DDI_STATUS(tc_port));
554 if (val & TCSS_DDI_STATUS_HPD_LIVE_STATUS_ALT)
555 mask |= BIT(TC_PORT_DP_ALT);
556 if (val & TCSS_DDI_STATUS_HPD_LIVE_STATUS_TBT)
557 mask |= BIT(TC_PORT_TBT_ALT);
559 if (intel_de_read(i915, SDEISR) & isr_bit)
560 mask |= BIT(TC_PORT_LEGACY);
566 * Return the PHY status complete flag indicating that display can acquire the
567 * PHY ownership. The IOM firmware sets this flag when it's ready to switch
568 * the ownership to display, regardless of what sink is connected (TBT-alt,
569 * DP-alt, legacy or nothing). For TBT-alt sinks the PHY is owned by the TBT
570 * subsystem and so switching the ownership to display is not required.
572 static bool adlp_tc_phy_is_ready(struct intel_tc_port *tc)
574 struct drm_i915_private *i915 = tc_to_i915(tc);
575 enum tc_port tc_port = intel_port_to_tc(i915, tc->dig_port->base.port);
578 val = intel_de_read(i915, TCSS_DDI_STATUS(tc_port));
579 if (val == 0xffffffff) {
580 drm_dbg_kms(&i915->drm,
581 "Port %s: PHY in TCCOLD, assuming not ready\n",
586 return val & TCSS_DDI_STATUS_READY;
589 static bool adlp_tc_phy_take_ownership(struct intel_tc_port *tc,
592 struct drm_i915_private *i915 = tc_to_i915(tc);
593 enum port port = tc->dig_port->base.port;
595 intel_de_rmw(i915, DDI_BUF_CTL(port), DDI_BUF_CTL_TC_PHY_OWNERSHIP,
596 take ? DDI_BUF_CTL_TC_PHY_OWNERSHIP : 0);
601 static bool adlp_tc_phy_is_owned(struct intel_tc_port *tc)
603 struct drm_i915_private *i915 = tc_to_i915(tc);
604 enum port port = tc->dig_port->base.port;
607 val = intel_de_read(i915, DDI_BUF_CTL(port));
608 return val & DDI_BUF_CTL_TC_PHY_OWNERSHIP;
611 static const struct intel_tc_phy_ops adlp_tc_phy_ops = {
612 .hpd_live_status = adlp_tc_phy_hpd_live_status,
613 .is_ready = adlp_tc_phy_is_ready,
614 .is_owned = adlp_tc_phy_is_owned,
615 .get_hw_state = icl_tc_phy_get_hw_state,
616 .connect = icl_tc_phy_connect,
617 .disconnect = icl_tc_phy_disconnect,
621 * Generic TC PHY handlers
622 * -----------------------
624 static u32 tc_phy_hpd_live_status(struct intel_tc_port *tc)
626 struct drm_i915_private *i915 = tc_to_i915(tc);
629 mask = tc->phy_ops->hpd_live_status(tc);
631 /* The sink can be connected only in a single mode. */
632 drm_WARN_ON_ONCE(&i915->drm, hweight32(mask) > 1);
637 static bool tc_phy_is_ready(struct intel_tc_port *tc)
639 return tc->phy_ops->is_ready(tc);
642 static bool tc_phy_is_owned(struct intel_tc_port *tc)
644 return tc->phy_ops->is_owned(tc);
647 static void tc_phy_get_hw_state(struct intel_tc_port *tc)
649 tc->phy_ops->get_hw_state(tc);
652 static bool tc_phy_take_ownership(struct intel_tc_port *tc, bool take)
654 struct drm_i915_private *i915 = tc_to_i915(tc);
656 if (IS_ALDERLAKE_P(i915))
657 return adlp_tc_phy_take_ownership(tc, take);
659 return icl_tc_phy_take_ownership(tc, take);
662 static bool tc_phy_is_ready_and_owned(struct intel_tc_port *tc,
663 bool phy_is_ready, bool phy_is_owned)
665 struct drm_i915_private *i915 = tc_to_i915(tc);
667 drm_WARN_ON(&i915->drm, phy_is_owned && !phy_is_ready);
669 return phy_is_ready && phy_is_owned;
672 static bool tc_phy_is_connected(struct intel_tc_port *tc,
673 enum icl_port_dpll_id port_pll_type)
675 struct intel_encoder *encoder = &tc->dig_port->base;
676 struct drm_i915_private *i915 = to_i915(encoder->base.dev);
677 bool phy_is_ready = tc_phy_is_ready(tc);
678 bool phy_is_owned = tc_phy_is_owned(tc);
681 if (tc_phy_is_ready_and_owned(tc, phy_is_ready, phy_is_owned))
682 is_connected = port_pll_type == ICL_PORT_DPLL_MG_PHY;
684 is_connected = port_pll_type == ICL_PORT_DPLL_DEFAULT;
686 drm_dbg_kms(&i915->drm,
687 "Port %s: PHY connected: %s (ready: %s, owned: %s, pll_type: %s)\n",
689 str_yes_no(is_connected),
690 str_yes_no(phy_is_ready),
691 str_yes_no(phy_is_owned),
692 port_pll_type == ICL_PORT_DPLL_DEFAULT ? "tbt" : "non-tbt");
697 static void tc_phy_wait_for_ready(struct intel_tc_port *tc)
699 struct drm_i915_private *i915 = tc_to_i915(tc);
701 if (wait_for(tc_phy_is_ready(tc), 100))
702 drm_err(&i915->drm, "Port %s: timeout waiting for PHY ready\n",
706 static enum tc_port_mode
707 hpd_mask_to_tc_mode(u32 live_status_mask)
709 if (live_status_mask)
710 return fls(live_status_mask) - 1;
712 return TC_PORT_DISCONNECTED;
715 static enum tc_port_mode
716 tc_phy_hpd_live_mode(struct intel_tc_port *tc)
718 u32 live_status_mask = tc_phy_hpd_live_status(tc);
720 return hpd_mask_to_tc_mode(live_status_mask);
723 static enum tc_port_mode
724 get_tc_mode_in_phy_owned_state(struct intel_tc_port *tc,
725 enum tc_port_mode live_mode)
732 MISSING_CASE(live_mode);
734 case TC_PORT_TBT_ALT:
735 case TC_PORT_DISCONNECTED:
737 return TC_PORT_LEGACY;
739 return TC_PORT_DP_ALT;
743 static enum tc_port_mode
744 get_tc_mode_in_phy_not_owned_state(struct intel_tc_port *tc,
745 enum tc_port_mode live_mode)
749 return TC_PORT_DISCONNECTED;
751 case TC_PORT_TBT_ALT:
752 return TC_PORT_TBT_ALT;
754 MISSING_CASE(live_mode);
756 case TC_PORT_DISCONNECTED:
758 return TC_PORT_DISCONNECTED;
760 return TC_PORT_TBT_ALT;
764 static enum tc_port_mode
765 tc_phy_get_current_mode(struct intel_tc_port *tc)
767 struct drm_i915_private *i915 = tc_to_i915(tc);
768 enum tc_port_mode live_mode = tc_phy_hpd_live_mode(tc);
771 enum tc_port_mode mode;
774 * For legacy ports the IOM firmware initializes the PHY during boot-up
775 * and system resume whether or not a sink is connected. Wait here for
776 * the initialization to get ready.
779 tc_phy_wait_for_ready(tc);
781 phy_is_ready = tc_phy_is_ready(tc);
782 phy_is_owned = tc_phy_is_owned(tc);
784 if (!tc_phy_is_ready_and_owned(tc, phy_is_ready, phy_is_owned)) {
785 mode = get_tc_mode_in_phy_not_owned_state(tc, live_mode);
787 drm_WARN_ON(&i915->drm, live_mode == TC_PORT_TBT_ALT);
788 mode = get_tc_mode_in_phy_owned_state(tc, live_mode);
791 drm_dbg_kms(&i915->drm,
792 "Port %s: PHY mode: %s (ready: %s, owned: %s, HPD: %s)\n",
794 tc_port_mode_name(mode),
795 str_yes_no(phy_is_ready),
796 str_yes_no(phy_is_owned),
797 tc_port_mode_name(live_mode));
802 static enum tc_port_mode default_tc_mode(struct intel_tc_port *tc)
805 return TC_PORT_LEGACY;
807 return TC_PORT_TBT_ALT;
810 static enum tc_port_mode
811 hpd_mask_to_target_mode(struct intel_tc_port *tc, u32 live_status_mask)
813 enum tc_port_mode mode = hpd_mask_to_tc_mode(live_status_mask);
815 if (mode != TC_PORT_DISCONNECTED)
818 return default_tc_mode(tc);
821 static enum tc_port_mode
822 tc_phy_get_target_mode(struct intel_tc_port *tc)
824 u32 live_status_mask = tc_phy_hpd_live_status(tc);
826 return hpd_mask_to_target_mode(tc, live_status_mask);
829 static void tc_phy_connect(struct intel_tc_port *tc, int required_lanes)
831 struct drm_i915_private *i915 = tc_to_i915(tc);
832 u32 live_status_mask = tc_phy_hpd_live_status(tc);
835 tc_port_fixup_legacy_flag(tc, live_status_mask);
837 tc->mode = hpd_mask_to_target_mode(tc, live_status_mask);
839 connected = tc->phy_ops->connect(tc, required_lanes);
840 if (!connected && tc->mode != default_tc_mode(tc)) {
841 tc->mode = default_tc_mode(tc);
842 connected = tc->phy_ops->connect(tc, required_lanes);
845 drm_WARN_ON(&i915->drm, !connected);
848 static void tc_phy_disconnect(struct intel_tc_port *tc)
850 if (tc->mode != TC_PORT_DISCONNECTED) {
851 tc->phy_ops->disconnect(tc);
852 tc->mode = TC_PORT_DISCONNECTED;
856 static void intel_tc_port_reset_mode(struct intel_tc_port *tc,
857 int required_lanes, bool force_disconnect)
859 struct drm_i915_private *i915 = tc_to_i915(tc);
860 struct intel_digital_port *dig_port = tc->dig_port;
861 enum tc_port_mode old_tc_mode = tc->mode;
863 intel_display_power_flush_work(i915);
864 if (!intel_tc_cold_requires_aux_pw(dig_port)) {
865 enum intel_display_power_domain aux_domain;
868 aux_domain = intel_aux_power_domain(dig_port);
869 aux_powered = intel_display_power_is_enabled(i915, aux_domain);
870 drm_WARN_ON(&i915->drm, aux_powered);
873 tc_phy_disconnect(tc);
874 if (!force_disconnect)
875 tc_phy_connect(tc, required_lanes);
877 drm_dbg_kms(&i915->drm, "Port %s: TC port mode reset (%s -> %s)\n",
879 tc_port_mode_name(old_tc_mode),
880 tc_port_mode_name(tc->mode));
883 static bool intel_tc_port_needs_reset(struct intel_tc_port *tc)
885 return tc_phy_get_target_mode(tc) != tc->mode;
888 static void intel_tc_port_update_mode(struct intel_tc_port *tc,
889 int required_lanes, bool force_disconnect)
891 enum intel_display_power_domain domain;
892 intel_wakeref_t wref;
893 bool needs_reset = force_disconnect;
896 /* Get power domain required to check the hotplug live status. */
897 wref = tc_cold_block(tc, &domain);
898 needs_reset = intel_tc_port_needs_reset(tc);
899 tc_cold_unblock(tc, domain, wref);
905 /* Get power domain required for resetting the mode. */
906 wref = tc_cold_block_in_mode(tc, TC_PORT_DISCONNECTED, &domain);
908 intel_tc_port_reset_mode(tc, required_lanes, force_disconnect);
910 /* Get power domain matching the new mode after reset. */
911 tc_cold_unblock(tc, tc->lock_power_domain,
912 fetch_and_zero(&tc->lock_wakeref));
913 if (tc->mode != TC_PORT_DISCONNECTED)
914 tc->lock_wakeref = tc_cold_block(tc, &tc->lock_power_domain);
916 tc_cold_unblock(tc, domain, wref);
919 static void __intel_tc_port_get_link(struct intel_tc_port *tc)
924 static void __intel_tc_port_put_link(struct intel_tc_port *tc)
929 static bool tc_port_is_enabled(struct intel_tc_port *tc)
931 struct drm_i915_private *i915 = tc_to_i915(tc);
932 struct intel_digital_port *dig_port = tc->dig_port;
934 assert_tc_port_power_enabled(tc);
936 return intel_de_read(i915, DDI_BUF_CTL(dig_port->base.port)) &
941 * intel_tc_port_init_mode: Read out HW state and init the given port's TypeC mode
942 * @dig_port: digital port
944 * Read out the HW state and initialize the TypeC mode of @dig_port. The mode
945 * will be locked until intel_tc_port_sanitize_mode() is called.
947 void intel_tc_port_init_mode(struct intel_digital_port *dig_port)
949 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
950 struct intel_tc_port *tc = to_tc_port(dig_port);
951 bool update_mode = false;
953 mutex_lock(&tc->lock);
955 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_DISCONNECTED);
956 drm_WARN_ON(&i915->drm, tc->lock_wakeref);
957 drm_WARN_ON(&i915->drm, tc->link_refcount);
959 tc_phy_get_hw_state(tc);
961 * Save the initial mode for the state check in
962 * intel_tc_port_sanitize_mode().
964 tc->init_mode = tc->mode;
967 * The PHY needs to be connected for AUX to work during HW readout and
968 * MST topology resume, but the PHY mode can only be changed if the
971 * An exception is the case where BIOS leaves the PHY incorrectly
972 * disconnected on an enabled legacy port. Work around that by
973 * connecting the PHY even though the port is enabled. This doesn't
974 * cause a problem as the PHY ownership state is ignored by the
975 * IOM/TCSS firmware (only display can own the PHY in that case).
977 if (!tc_port_is_enabled(tc)) {
979 } else if (tc->mode == TC_PORT_DISCONNECTED) {
980 drm_WARN_ON(&i915->drm, !tc->legacy_port);
982 "Port %s: PHY disconnected on enabled port, connecting it\n",
988 intel_tc_port_update_mode(tc, 1, false);
990 /* Prevent changing tc->mode until intel_tc_port_sanitize_mode() is called. */
991 __intel_tc_port_get_link(tc);
993 mutex_unlock(&tc->lock);
996 static bool tc_port_has_active_links(struct intel_tc_port *tc,
997 const struct intel_crtc_state *crtc_state)
999 struct drm_i915_private *i915 = tc_to_i915(tc);
1000 struct intel_digital_port *dig_port = tc->dig_port;
1001 enum icl_port_dpll_id pll_type = ICL_PORT_DPLL_DEFAULT;
1002 int active_links = 0;
1004 if (dig_port->dp.is_mst) {
1005 /* TODO: get the PLL type for MST, once HW readout is done for it. */
1006 active_links = intel_dp_mst_encoder_active_links(dig_port);
1007 } else if (crtc_state && crtc_state->hw.active) {
1008 pll_type = intel_ddi_port_pll_type(&dig_port->base, crtc_state);
1012 if (active_links && !tc_phy_is_connected(tc, pll_type))
1014 "Port %s: PHY disconnected with %d active link(s)\n",
1015 tc->port_name, active_links);
1017 return active_links;
1021 * intel_tc_port_sanitize_mode: Sanitize the given port's TypeC mode
1022 * @dig_port: digital port
1023 * @crtc_state: atomic state of CRTC connected to @dig_port
1025 * Sanitize @dig_port's TypeC mode wrt. the encoder's state right after driver
1026 * loading and system resume:
1027 * If the encoder is enabled keep the TypeC mode/PHY connected state locked until
1028 * the encoder is disabled.
1029 * If the encoder is disabled make sure the PHY is disconnected.
1030 * @crtc_state is valid if @dig_port is enabled, NULL otherwise.
1032 void intel_tc_port_sanitize_mode(struct intel_digital_port *dig_port,
1033 const struct intel_crtc_state *crtc_state)
1035 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1036 struct intel_tc_port *tc = to_tc_port(dig_port);
1038 mutex_lock(&tc->lock);
1040 drm_WARN_ON(&i915->drm, tc->link_refcount != 1);
1041 if (!tc_port_has_active_links(tc, crtc_state)) {
1043 * TBT-alt is the default mode in any case the PHY ownership is not
1044 * held (regardless of the sink's connected live state), so
1045 * we'll just switch to disconnected mode from it here without
1048 if (tc->init_mode != TC_PORT_TBT_ALT &&
1049 tc->init_mode != TC_PORT_DISCONNECTED)
1050 drm_dbg_kms(&i915->drm,
1051 "Port %s: PHY left in %s mode on disabled port, disconnecting it\n",
1053 tc_port_mode_name(tc->init_mode));
1054 tc_phy_disconnect(tc);
1055 __intel_tc_port_put_link(tc);
1057 tc_cold_unblock(tc, tc->lock_power_domain,
1058 fetch_and_zero(&tc->lock_wakeref));
1061 drm_dbg_kms(&i915->drm, "Port %s: sanitize mode (%s)\n",
1063 tc_port_mode_name(tc->mode));
1065 mutex_unlock(&tc->lock);
1069 * The type-C ports are different because even when they are connected, they may
1070 * not be available/usable by the graphics driver: see the comment on
1071 * icl_tc_phy_connect(). So in our driver instead of adding the additional
1072 * concept of "usable" and make everything check for "connected and usable" we
1073 * define a port as "connected" when it is not only connected, but also when it
1074 * is usable by the rest of the driver. That maintains the old assumption that
1075 * connected ports are usable, and avoids exposing to the users objects they
1078 bool intel_tc_port_connected_locked(struct intel_encoder *encoder)
1080 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1081 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1082 struct intel_tc_port *tc = to_tc_port(dig_port);
1084 drm_WARN_ON(&i915->drm, !intel_tc_port_ref_held(dig_port));
1086 return tc_phy_hpd_live_status(tc) & BIT(tc->mode);
1089 bool intel_tc_port_connected(struct intel_encoder *encoder)
1091 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1094 intel_tc_port_lock(dig_port);
1095 is_connected = intel_tc_port_connected_locked(encoder);
1096 intel_tc_port_unlock(dig_port);
1098 return is_connected;
1101 static void __intel_tc_port_lock(struct intel_tc_port *tc,
1104 struct drm_i915_private *i915 = tc_to_i915(tc);
1106 mutex_lock(&tc->lock);
1108 cancel_delayed_work(&tc->disconnect_phy_work);
1110 if (!tc->link_refcount)
1111 intel_tc_port_update_mode(tc, required_lanes,
1114 drm_WARN_ON(&i915->drm, tc->mode == TC_PORT_DISCONNECTED);
1115 drm_WARN_ON(&i915->drm, tc->mode != TC_PORT_TBT_ALT &&
1116 !tc_phy_is_owned(tc));
1119 void intel_tc_port_lock(struct intel_digital_port *dig_port)
1121 __intel_tc_port_lock(to_tc_port(dig_port), 1);
1125 * intel_tc_port_disconnect_phy_work: disconnect TypeC PHY from display port
1126 * @dig_port: digital port
1128 * Disconnect the given digital port from its TypeC PHY (handing back the
1129 * control of the PHY to the TypeC subsystem). This will happen in a delayed
1130 * manner after each aux transactions and modeset disables.
1132 static void intel_tc_port_disconnect_phy_work(struct work_struct *work)
1134 struct intel_tc_port *tc =
1135 container_of(work, struct intel_tc_port, disconnect_phy_work.work);
1137 mutex_lock(&tc->lock);
1139 if (!tc->link_refcount)
1140 intel_tc_port_update_mode(tc, 1, true);
1142 mutex_unlock(&tc->lock);
1146 * intel_tc_port_flush_work: flush the work disconnecting the PHY
1147 * @dig_port: digital port
1149 * Flush the delayed work disconnecting an idle PHY.
1151 void intel_tc_port_flush_work(struct intel_digital_port *dig_port)
1153 flush_delayed_work(&to_tc_port(dig_port)->disconnect_phy_work);
1156 void intel_tc_port_unlock(struct intel_digital_port *dig_port)
1158 struct intel_tc_port *tc = to_tc_port(dig_port);
1160 if (!tc->link_refcount && tc->mode != TC_PORT_DISCONNECTED)
1161 queue_delayed_work(system_unbound_wq, &tc->disconnect_phy_work,
1162 msecs_to_jiffies(1000));
1164 mutex_unlock(&tc->lock);
1167 bool intel_tc_port_ref_held(struct intel_digital_port *dig_port)
1169 struct intel_tc_port *tc = to_tc_port(dig_port);
1171 return mutex_is_locked(&tc->lock) ||
1175 void intel_tc_port_get_link(struct intel_digital_port *dig_port,
1178 struct intel_tc_port *tc = to_tc_port(dig_port);
1180 __intel_tc_port_lock(tc, required_lanes);
1181 __intel_tc_port_get_link(tc);
1182 intel_tc_port_unlock(dig_port);
1185 void intel_tc_port_put_link(struct intel_digital_port *dig_port)
1187 struct intel_tc_port *tc = to_tc_port(dig_port);
1189 intel_tc_port_lock(dig_port);
1190 __intel_tc_port_put_link(tc);
1191 intel_tc_port_unlock(dig_port);
1194 * Disconnecting the PHY after the PHY's PLL gets disabled may
1195 * hang the system on ADL-P, so disconnect the PHY here synchronously.
1196 * TODO: remove this once the root cause of the ordering requirement
1199 intel_tc_port_flush_work(dig_port);
1203 tc_has_modular_fia(struct drm_i915_private *i915, struct intel_tc_port *tc)
1205 enum intel_display_power_domain domain;
1206 intel_wakeref_t wakeref;
1209 if (!INTEL_INFO(i915)->display.has_modular_fia)
1212 mutex_lock(&tc->lock);
1213 wakeref = tc_cold_block(tc, &domain);
1214 val = intel_de_read(i915, PORT_TX_DFLEXDPSP(FIA1));
1215 tc_cold_unblock(tc, domain, wakeref);
1216 mutex_unlock(&tc->lock);
1218 drm_WARN_ON(&i915->drm, val == 0xffffffff);
1220 return val & MODULAR_FIA_MASK;
1224 tc_port_load_fia_params(struct drm_i915_private *i915, struct intel_tc_port *tc)
1226 enum port port = tc->dig_port->base.port;
1227 enum tc_port tc_port = intel_port_to_tc(i915, port);
1230 * Each Modular FIA instance houses 2 TC ports. In SOC that has more
1231 * than two TC ports, there are multiple instances of Modular FIA.
1233 if (tc_has_modular_fia(i915, tc)) {
1234 tc->phy_fia = tc_port / 2;
1235 tc->phy_fia_idx = tc_port % 2;
1238 tc->phy_fia_idx = tc_port;
1242 int intel_tc_port_init(struct intel_digital_port *dig_port, bool is_legacy)
1244 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1245 struct intel_tc_port *tc;
1246 enum port port = dig_port->base.port;
1247 enum tc_port tc_port = intel_port_to_tc(i915, port);
1249 if (drm_WARN_ON(&i915->drm, tc_port == TC_PORT_NONE))
1252 tc = kzalloc(sizeof(*tc), GFP_KERNEL);
1257 tc->dig_port = dig_port;
1259 if (DISPLAY_VER(i915) >= 13)
1260 tc->phy_ops = &adlp_tc_phy_ops;
1262 tc->phy_ops = &icl_tc_phy_ops;
1264 snprintf(tc->port_name, sizeof(tc->port_name),
1265 "%c/TC#%d", port_name(port), tc_port + 1);
1267 mutex_init(&tc->lock);
1268 INIT_DELAYED_WORK(&tc->disconnect_phy_work, intel_tc_port_disconnect_phy_work);
1269 tc->legacy_port = is_legacy;
1270 tc->mode = TC_PORT_DISCONNECTED;
1271 tc->link_refcount = 0;
1272 tc_port_load_fia_params(i915, tc);
1274 intel_tc_port_init_mode(dig_port);
1279 void intel_tc_port_cleanup(struct intel_digital_port *dig_port)
1281 intel_tc_port_flush_work(dig_port);
1283 kfree(dig_port->tc);
1284 dig_port->tc = NULL;