1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
8 #include "intel_display.h"
9 #include "intel_display_power_map.h"
10 #include "intel_display_types.h"
11 #include "intel_dp_mst.h"
13 #include "intel_tc_phy_regs.h"
15 static const char *tc_port_mode_name(enum tc_port_mode mode)
17 static const char * const names[] = {
18 [TC_PORT_DISCONNECTED] = "disconnected",
19 [TC_PORT_TBT_ALT] = "tbt-alt",
20 [TC_PORT_DP_ALT] = "dp-alt",
21 [TC_PORT_LEGACY] = "legacy",
24 if (WARN_ON(mode >= ARRAY_SIZE(names)))
25 mode = TC_PORT_DISCONNECTED;
30 static bool intel_tc_port_in_mode(struct intel_digital_port *dig_port,
31 enum tc_port_mode mode)
33 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
34 enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
36 return intel_phy_is_tc(i915, phy) && dig_port->tc_mode == mode;
39 bool intel_tc_port_in_tbt_alt_mode(struct intel_digital_port *dig_port)
41 return intel_tc_port_in_mode(dig_port, TC_PORT_TBT_ALT);
44 bool intel_tc_port_in_dp_alt_mode(struct intel_digital_port *dig_port)
46 return intel_tc_port_in_mode(dig_port, TC_PORT_DP_ALT);
49 bool intel_tc_port_in_legacy_mode(struct intel_digital_port *dig_port)
51 return intel_tc_port_in_mode(dig_port, TC_PORT_LEGACY);
54 bool intel_tc_cold_requires_aux_pw(struct intel_digital_port *dig_port)
56 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
58 return (DISPLAY_VER(i915) == 11 && dig_port->tc_legacy_port) ||
62 static enum intel_display_power_domain
63 tc_cold_get_power_domain(struct intel_digital_port *dig_port, enum tc_port_mode mode)
65 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
67 if (mode == TC_PORT_TBT_ALT || !intel_tc_cold_requires_aux_pw(dig_port))
68 return POWER_DOMAIN_TC_COLD_OFF;
70 return intel_display_power_legacy_aux_domain(i915, dig_port->aux_ch);
73 static intel_wakeref_t
74 tc_cold_block_in_mode(struct intel_digital_port *dig_port, enum tc_port_mode mode,
75 enum intel_display_power_domain *domain)
77 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
79 *domain = tc_cold_get_power_domain(dig_port, mode);
81 return intel_display_power_get(i915, *domain);
84 static intel_wakeref_t
85 tc_cold_block(struct intel_digital_port *dig_port, enum intel_display_power_domain *domain)
87 return tc_cold_block_in_mode(dig_port, dig_port->tc_mode, domain);
91 tc_cold_unblock(struct intel_digital_port *dig_port, enum intel_display_power_domain domain,
92 intel_wakeref_t wakeref)
94 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
97 * wakeref == -1, means some error happened saving save_depot_stack but
98 * power should still be put down and 0 is a invalid save_depot_stack
99 * id so can be used to skip it for non TC legacy ports.
104 intel_display_power_put(i915, domain, wakeref);
108 assert_tc_cold_blocked(struct intel_digital_port *dig_port)
110 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
113 enabled = intel_display_power_is_enabled(i915,
114 tc_cold_get_power_domain(dig_port,
116 drm_WARN_ON(&i915->drm, !enabled);
119 u32 intel_tc_port_get_lane_mask(struct intel_digital_port *dig_port)
121 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
122 struct intel_uncore *uncore = &i915->uncore;
125 lane_mask = intel_uncore_read(uncore,
126 PORT_TX_DFLEXDPSP(dig_port->tc_phy_fia));
128 drm_WARN_ON(&i915->drm, lane_mask == 0xffffffff);
129 assert_tc_cold_blocked(dig_port);
131 lane_mask &= DP_LANE_ASSIGNMENT_MASK(dig_port->tc_phy_fia_idx);
132 return lane_mask >> DP_LANE_ASSIGNMENT_SHIFT(dig_port->tc_phy_fia_idx);
135 u32 intel_tc_port_get_pin_assignment_mask(struct intel_digital_port *dig_port)
137 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
138 struct intel_uncore *uncore = &i915->uncore;
141 pin_mask = intel_uncore_read(uncore,
142 PORT_TX_DFLEXPA1(dig_port->tc_phy_fia));
144 drm_WARN_ON(&i915->drm, pin_mask == 0xffffffff);
145 assert_tc_cold_blocked(dig_port);
147 return (pin_mask & DP_PIN_ASSIGNMENT_MASK(dig_port->tc_phy_fia_idx)) >>
148 DP_PIN_ASSIGNMENT_SHIFT(dig_port->tc_phy_fia_idx);
151 int intel_tc_port_fia_max_lane_count(struct intel_digital_port *dig_port)
153 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
154 intel_wakeref_t wakeref;
157 if (dig_port->tc_mode != TC_PORT_DP_ALT)
160 assert_tc_cold_blocked(dig_port);
163 with_intel_display_power(i915, POWER_DOMAIN_DISPLAY_CORE, wakeref)
164 lane_mask = intel_tc_port_get_lane_mask(dig_port);
168 MISSING_CASE(lane_mask);
183 void intel_tc_port_set_fia_lane_count(struct intel_digital_port *dig_port,
186 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
187 bool lane_reversal = dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
188 struct intel_uncore *uncore = &i915->uncore;
191 drm_WARN_ON(&i915->drm,
192 lane_reversal && dig_port->tc_mode != TC_PORT_LEGACY);
194 assert_tc_cold_blocked(dig_port);
196 val = intel_uncore_read(uncore,
197 PORT_TX_DFLEXDPMLE1(dig_port->tc_phy_fia));
198 val &= ~DFLEXDPMLE1_DPMLETC_MASK(dig_port->tc_phy_fia_idx);
200 switch (required_lanes) {
202 val |= lane_reversal ?
203 DFLEXDPMLE1_DPMLETC_ML3(dig_port->tc_phy_fia_idx) :
204 DFLEXDPMLE1_DPMLETC_ML0(dig_port->tc_phy_fia_idx);
207 val |= lane_reversal ?
208 DFLEXDPMLE1_DPMLETC_ML3_2(dig_port->tc_phy_fia_idx) :
209 DFLEXDPMLE1_DPMLETC_ML1_0(dig_port->tc_phy_fia_idx);
212 val |= DFLEXDPMLE1_DPMLETC_ML3_0(dig_port->tc_phy_fia_idx);
215 MISSING_CASE(required_lanes);
218 intel_uncore_write(uncore,
219 PORT_TX_DFLEXDPMLE1(dig_port->tc_phy_fia), val);
222 static void tc_port_fixup_legacy_flag(struct intel_digital_port *dig_port,
223 u32 live_status_mask)
225 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
228 if (dig_port->tc_legacy_port)
229 valid_hpd_mask = BIT(TC_PORT_LEGACY);
231 valid_hpd_mask = BIT(TC_PORT_DP_ALT) |
232 BIT(TC_PORT_TBT_ALT);
234 if (!(live_status_mask & ~valid_hpd_mask))
237 /* If live status mismatches the VBT flag, trust the live status. */
238 drm_dbg_kms(&i915->drm,
239 "Port %s: live status %08x mismatches the legacy port flag %08x, fixing flag\n",
240 dig_port->tc_port_name, live_status_mask, valid_hpd_mask);
242 dig_port->tc_legacy_port = !dig_port->tc_legacy_port;
245 static u32 icl_tc_port_live_status_mask(struct intel_digital_port *dig_port)
247 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
248 struct intel_uncore *uncore = &i915->uncore;
249 u32 isr_bit = i915->display.hotplug.pch_hpd[dig_port->base.hpd_pin];
253 val = intel_uncore_read(uncore,
254 PORT_TX_DFLEXDPSP(dig_port->tc_phy_fia));
256 if (val == 0xffffffff) {
257 drm_dbg_kms(&i915->drm,
258 "Port %s: PHY in TCCOLD, nothing connected\n",
259 dig_port->tc_port_name);
263 if (val & TC_LIVE_STATE_TBT(dig_port->tc_phy_fia_idx))
264 mask |= BIT(TC_PORT_TBT_ALT);
265 if (val & TC_LIVE_STATE_TC(dig_port->tc_phy_fia_idx))
266 mask |= BIT(TC_PORT_DP_ALT);
268 if (intel_uncore_read(uncore, SDEISR) & isr_bit)
269 mask |= BIT(TC_PORT_LEGACY);
271 /* The sink can be connected only in a single mode. */
272 if (!drm_WARN_ON_ONCE(&i915->drm, hweight32(mask) > 1))
273 tc_port_fixup_legacy_flag(dig_port, mask);
278 static u32 adl_tc_port_live_status_mask(struct intel_digital_port *dig_port)
280 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
281 enum tc_port tc_port = intel_port_to_tc(i915, dig_port->base.port);
282 u32 isr_bit = i915->display.hotplug.pch_hpd[dig_port->base.hpd_pin];
283 struct intel_uncore *uncore = &i915->uncore;
287 * On ADL-P HW/FW will wake from TCCOLD to complete the read access of
288 * registers in IOM. Note that this doesn't apply to PHY and FIA
291 val = intel_uncore_read(uncore, TCSS_DDI_STATUS(tc_port));
292 if (val & TCSS_DDI_STATUS_HPD_LIVE_STATUS_ALT)
293 mask |= BIT(TC_PORT_DP_ALT);
294 if (val & TCSS_DDI_STATUS_HPD_LIVE_STATUS_TBT)
295 mask |= BIT(TC_PORT_TBT_ALT);
297 if (intel_uncore_read(uncore, SDEISR) & isr_bit)
298 mask |= BIT(TC_PORT_LEGACY);
300 /* The sink can be connected only in a single mode. */
301 if (!drm_WARN_ON(&i915->drm, hweight32(mask) > 1))
302 tc_port_fixup_legacy_flag(dig_port, mask);
307 static u32 tc_port_live_status_mask(struct intel_digital_port *dig_port)
309 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
311 if (IS_ALDERLAKE_P(i915))
312 return adl_tc_port_live_status_mask(dig_port);
314 return icl_tc_port_live_status_mask(dig_port);
318 * Return the PHY status complete flag indicating that display can acquire the
319 * PHY ownership. The IOM firmware sets this flag when a DP-alt or legacy sink
320 * is connected and it's ready to switch the ownership to display. The flag
321 * will be left cleared when a TBT-alt sink is connected, where the PHY is
322 * owned by the TBT subsystem and so switching the ownership to display is not
325 static bool icl_tc_phy_status_complete(struct intel_digital_port *dig_port)
327 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
328 struct intel_uncore *uncore = &i915->uncore;
331 val = intel_uncore_read(uncore,
332 PORT_TX_DFLEXDPPMS(dig_port->tc_phy_fia));
333 if (val == 0xffffffff) {
334 drm_dbg_kms(&i915->drm,
335 "Port %s: PHY in TCCOLD, assuming not complete\n",
336 dig_port->tc_port_name);
340 return val & DP_PHY_MODE_STATUS_COMPLETED(dig_port->tc_phy_fia_idx);
344 * Return the PHY status complete flag indicating that display can acquire the
345 * PHY ownership. The IOM firmware sets this flag when it's ready to switch
346 * the ownership to display, regardless of what sink is connected (TBT-alt,
347 * DP-alt, legacy or nothing). For TBT-alt sinks the PHY is owned by the TBT
348 * subsystem and so switching the ownership to display is not required.
350 static bool adl_tc_phy_status_complete(struct intel_digital_port *dig_port)
352 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
353 enum tc_port tc_port = intel_port_to_tc(i915, dig_port->base.port);
354 struct intel_uncore *uncore = &i915->uncore;
357 val = intel_uncore_read(uncore, TCSS_DDI_STATUS(tc_port));
358 if (val == 0xffffffff) {
359 drm_dbg_kms(&i915->drm,
360 "Port %s: PHY in TCCOLD, assuming not complete\n",
361 dig_port->tc_port_name);
365 return val & TCSS_DDI_STATUS_READY;
368 static bool tc_phy_status_complete(struct intel_digital_port *dig_port)
370 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
372 if (IS_ALDERLAKE_P(i915))
373 return adl_tc_phy_status_complete(dig_port);
375 return icl_tc_phy_status_complete(dig_port);
378 static bool icl_tc_phy_take_ownership(struct intel_digital_port *dig_port,
381 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
382 struct intel_uncore *uncore = &i915->uncore;
385 val = intel_uncore_read(uncore,
386 PORT_TX_DFLEXDPCSSS(dig_port->tc_phy_fia));
387 if (val == 0xffffffff) {
388 drm_dbg_kms(&i915->drm,
389 "Port %s: PHY in TCCOLD, can't %s ownership\n",
390 dig_port->tc_port_name, take ? "take" : "release");
395 val &= ~DP_PHY_MODE_STATUS_NOT_SAFE(dig_port->tc_phy_fia_idx);
397 val |= DP_PHY_MODE_STATUS_NOT_SAFE(dig_port->tc_phy_fia_idx);
399 intel_uncore_write(uncore,
400 PORT_TX_DFLEXDPCSSS(dig_port->tc_phy_fia), val);
405 static bool adl_tc_phy_take_ownership(struct intel_digital_port *dig_port,
408 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
409 struct intel_uncore *uncore = &i915->uncore;
410 enum port port = dig_port->base.port;
413 val = intel_uncore_read(uncore, DDI_BUF_CTL(port));
415 val |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
417 val &= ~DDI_BUF_CTL_TC_PHY_OWNERSHIP;
418 intel_uncore_write(uncore, DDI_BUF_CTL(port), val);
423 static bool tc_phy_take_ownership(struct intel_digital_port *dig_port, bool take)
425 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
427 if (IS_ALDERLAKE_P(i915))
428 return adl_tc_phy_take_ownership(dig_port, take);
430 return icl_tc_phy_take_ownership(dig_port, take);
433 static bool icl_tc_phy_is_owned(struct intel_digital_port *dig_port)
435 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
436 struct intel_uncore *uncore = &i915->uncore;
439 val = intel_uncore_read(uncore,
440 PORT_TX_DFLEXDPCSSS(dig_port->tc_phy_fia));
441 if (val == 0xffffffff) {
442 drm_dbg_kms(&i915->drm,
443 "Port %s: PHY in TCCOLD, assume safe mode\n",
444 dig_port->tc_port_name);
448 return val & DP_PHY_MODE_STATUS_NOT_SAFE(dig_port->tc_phy_fia_idx);
451 static bool adl_tc_phy_is_owned(struct intel_digital_port *dig_port)
453 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
454 struct intel_uncore *uncore = &i915->uncore;
455 enum port port = dig_port->base.port;
458 val = intel_uncore_read(uncore, DDI_BUF_CTL(port));
459 return val & DDI_BUF_CTL_TC_PHY_OWNERSHIP;
462 static bool tc_phy_is_owned(struct intel_digital_port *dig_port)
464 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
466 if (IS_ALDERLAKE_P(i915))
467 return adl_tc_phy_is_owned(dig_port);
469 return icl_tc_phy_is_owned(dig_port);
473 * This function implements the first part of the Connect Flow described by our
474 * specification, Gen11 TypeC Programming chapter. The rest of the flow (reading
475 * lanes, EDID, etc) is done as needed in the typical places.
477 * Unlike the other ports, type-C ports are not available to use as soon as we
478 * get a hotplug. The type-C PHYs can be shared between multiple controllers:
479 * display, USB, etc. As a result, handshaking through FIA is required around
480 * connect and disconnect to cleanly transfer ownership with the controller and
481 * set the type-C power state.
483 static void icl_tc_phy_connect(struct intel_digital_port *dig_port,
486 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
487 u32 live_status_mask;
490 if (!tc_phy_status_complete(dig_port)) {
491 drm_dbg_kms(&i915->drm, "Port %s: PHY not ready\n",
492 dig_port->tc_port_name);
493 goto out_set_tbt_alt_mode;
496 live_status_mask = tc_port_live_status_mask(dig_port);
497 if (!(live_status_mask & (BIT(TC_PORT_DP_ALT) | BIT(TC_PORT_LEGACY))) &&
498 !dig_port->tc_legacy_port) {
499 drm_dbg_kms(&i915->drm, "Port %s: PHY ownership not required (live status %02x)\n",
500 dig_port->tc_port_name, live_status_mask);
501 goto out_set_tbt_alt_mode;
504 if (!tc_phy_take_ownership(dig_port, true) &&
505 !drm_WARN_ON(&i915->drm, dig_port->tc_legacy_port))
506 goto out_set_tbt_alt_mode;
508 max_lanes = intel_tc_port_fia_max_lane_count(dig_port);
509 if (dig_port->tc_legacy_port) {
510 drm_WARN_ON(&i915->drm, max_lanes != 4);
511 dig_port->tc_mode = TC_PORT_LEGACY;
517 * Now we have to re-check the live state, in case the port recently
518 * became disconnected. Not necessary for legacy mode.
520 if (!(tc_port_live_status_mask(dig_port) & BIT(TC_PORT_DP_ALT))) {
521 drm_dbg_kms(&i915->drm, "Port %s: PHY sudden disconnect\n",
522 dig_port->tc_port_name);
523 goto out_release_phy;
526 if (max_lanes < required_lanes) {
527 drm_dbg_kms(&i915->drm,
528 "Port %s: PHY max lanes %d < required lanes %d\n",
529 dig_port->tc_port_name,
530 max_lanes, required_lanes);
531 goto out_release_phy;
534 dig_port->tc_mode = TC_PORT_DP_ALT;
539 tc_phy_take_ownership(dig_port, false);
540 out_set_tbt_alt_mode:
541 dig_port->tc_mode = TC_PORT_TBT_ALT;
545 * See the comment at the connect function. This implements the Disconnect
548 static void icl_tc_phy_disconnect(struct intel_digital_port *dig_port)
550 switch (dig_port->tc_mode) {
553 tc_phy_take_ownership(dig_port, false);
555 case TC_PORT_TBT_ALT:
556 dig_port->tc_mode = TC_PORT_DISCONNECTED;
558 case TC_PORT_DISCONNECTED:
561 MISSING_CASE(dig_port->tc_mode);
565 static bool icl_tc_phy_is_connected(struct intel_digital_port *dig_port)
567 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
569 if (!tc_phy_status_complete(dig_port)) {
570 drm_dbg_kms(&i915->drm, "Port %s: PHY status not complete\n",
571 dig_port->tc_port_name);
572 return dig_port->tc_mode == TC_PORT_TBT_ALT;
575 /* On ADL-P the PHY complete flag is set in TBT mode as well. */
576 if (IS_ALDERLAKE_P(i915) && dig_port->tc_mode == TC_PORT_TBT_ALT)
579 if (!tc_phy_is_owned(dig_port)) {
580 drm_dbg_kms(&i915->drm, "Port %s: PHY not owned\n",
581 dig_port->tc_port_name);
586 return dig_port->tc_mode == TC_PORT_DP_ALT ||
587 dig_port->tc_mode == TC_PORT_LEGACY;
590 static enum tc_port_mode
591 intel_tc_port_get_current_mode(struct intel_digital_port *dig_port)
593 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
594 u32 live_status_mask = tc_port_live_status_mask(dig_port);
595 enum tc_port_mode mode;
597 if (!tc_phy_is_owned(dig_port) ||
598 drm_WARN_ON(&i915->drm, !tc_phy_status_complete(dig_port)))
599 return TC_PORT_TBT_ALT;
601 mode = dig_port->tc_legacy_port ? TC_PORT_LEGACY : TC_PORT_DP_ALT;
602 if (live_status_mask) {
603 enum tc_port_mode live_mode = fls(live_status_mask) - 1;
605 if (!drm_WARN_ON(&i915->drm, live_mode == TC_PORT_TBT_ALT))
612 static enum tc_port_mode
613 intel_tc_port_get_target_mode(struct intel_digital_port *dig_port)
615 u32 live_status_mask = tc_port_live_status_mask(dig_port);
617 if (live_status_mask)
618 return fls(live_status_mask) - 1;
620 return TC_PORT_TBT_ALT;
623 static void intel_tc_port_reset_mode(struct intel_digital_port *dig_port,
624 int required_lanes, bool force_disconnect)
626 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
627 enum tc_port_mode old_tc_mode = dig_port->tc_mode;
629 intel_display_power_flush_work(i915);
630 if (!intel_tc_cold_requires_aux_pw(dig_port)) {
631 enum intel_display_power_domain aux_domain;
634 aux_domain = intel_aux_power_domain(dig_port);
635 aux_powered = intel_display_power_is_enabled(i915, aux_domain);
636 drm_WARN_ON(&i915->drm, aux_powered);
639 icl_tc_phy_disconnect(dig_port);
640 if (!force_disconnect)
641 icl_tc_phy_connect(dig_port, required_lanes);
643 drm_dbg_kms(&i915->drm, "Port %s: TC port mode reset (%s -> %s)\n",
644 dig_port->tc_port_name,
645 tc_port_mode_name(old_tc_mode),
646 tc_port_mode_name(dig_port->tc_mode));
649 static bool intel_tc_port_needs_reset(struct intel_digital_port *dig_port)
651 return intel_tc_port_get_target_mode(dig_port) != dig_port->tc_mode;
654 static void intel_tc_port_update_mode(struct intel_digital_port *dig_port,
655 int required_lanes, bool force_disconnect)
657 enum intel_display_power_domain domain;
658 intel_wakeref_t wref;
659 bool needs_reset = force_disconnect;
662 /* Get power domain required to check the hotplug live status. */
663 wref = tc_cold_block(dig_port, &domain);
664 needs_reset = intel_tc_port_needs_reset(dig_port);
665 tc_cold_unblock(dig_port, domain, wref);
671 /* Get power domain required for resetting the mode. */
672 wref = tc_cold_block_in_mode(dig_port, TC_PORT_DISCONNECTED, &domain);
674 intel_tc_port_reset_mode(dig_port, required_lanes, force_disconnect);
676 /* Get power domain matching the new mode after reset. */
677 tc_cold_unblock(dig_port, dig_port->tc_lock_power_domain,
678 fetch_and_zero(&dig_port->tc_lock_wakeref));
679 if (dig_port->tc_mode != TC_PORT_DISCONNECTED)
680 dig_port->tc_lock_wakeref = tc_cold_block(dig_port,
681 &dig_port->tc_lock_power_domain);
683 tc_cold_unblock(dig_port, domain, wref);
687 intel_tc_port_link_init_refcount(struct intel_digital_port *dig_port,
690 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
692 drm_WARN_ON(&i915->drm, dig_port->tc_link_refcount);
693 dig_port->tc_link_refcount = refcount;
696 void intel_tc_port_sanitize(struct intel_digital_port *dig_port)
698 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
699 struct intel_encoder *encoder = &dig_port->base;
700 intel_wakeref_t tc_cold_wref;
701 enum intel_display_power_domain domain;
702 int active_links = 0;
704 mutex_lock(&dig_port->tc_lock);
706 if (dig_port->dp.is_mst)
707 active_links = intel_dp_mst_encoder_active_links(dig_port);
708 else if (encoder->base.crtc)
709 active_links = to_intel_crtc(encoder->base.crtc)->active;
711 drm_WARN_ON(&i915->drm, dig_port->tc_mode != TC_PORT_DISCONNECTED);
712 drm_WARN_ON(&i915->drm, dig_port->tc_lock_wakeref);
714 tc_cold_wref = tc_cold_block(dig_port, &domain);
716 dig_port->tc_mode = intel_tc_port_get_current_mode(dig_port);
718 if (!icl_tc_phy_is_connected(dig_port))
719 drm_dbg_kms(&i915->drm,
720 "Port %s: PHY disconnected with %d active link(s)\n",
721 dig_port->tc_port_name, active_links);
722 intel_tc_port_link_init_refcount(dig_port, active_links);
724 dig_port->tc_lock_wakeref = tc_cold_block(dig_port,
725 &dig_port->tc_lock_power_domain);
728 * TBT-alt is the default mode in any case the PHY ownership is not
729 * held (regardless of the sink's connected live state), so
730 * we'll just switch to disconnected mode from it here without
733 if (dig_port->tc_mode != TC_PORT_TBT_ALT)
734 drm_dbg_kms(&i915->drm,
735 "Port %s: PHY left in %s mode on disabled port, disconnecting it\n",
736 dig_port->tc_port_name,
737 tc_port_mode_name(dig_port->tc_mode));
738 icl_tc_phy_disconnect(dig_port);
741 tc_cold_unblock(dig_port, domain, tc_cold_wref);
743 drm_dbg_kms(&i915->drm, "Port %s: sanitize mode (%s)\n",
744 dig_port->tc_port_name,
745 tc_port_mode_name(dig_port->tc_mode));
747 mutex_unlock(&dig_port->tc_lock);
751 * The type-C ports are different because even when they are connected, they may
752 * not be available/usable by the graphics driver: see the comment on
753 * icl_tc_phy_connect(). So in our driver instead of adding the additional
754 * concept of "usable" and make everything check for "connected and usable" we
755 * define a port as "connected" when it is not only connected, but also when it
756 * is usable by the rest of the driver. That maintains the old assumption that
757 * connected ports are usable, and avoids exposing to the users objects they
760 bool intel_tc_port_connected(struct intel_encoder *encoder)
762 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
765 intel_tc_port_lock(dig_port);
767 is_connected = tc_port_live_status_mask(dig_port) &
768 BIT(dig_port->tc_mode);
770 intel_tc_port_unlock(dig_port);
775 static void __intel_tc_port_lock(struct intel_digital_port *dig_port,
778 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
780 mutex_lock(&dig_port->tc_lock);
782 cancel_delayed_work(&dig_port->tc_disconnect_phy_work);
784 if (!dig_port->tc_link_refcount)
785 intel_tc_port_update_mode(dig_port, required_lanes,
788 drm_WARN_ON(&i915->drm, dig_port->tc_mode == TC_PORT_DISCONNECTED);
789 drm_WARN_ON(&i915->drm, dig_port->tc_mode != TC_PORT_TBT_ALT &&
790 !tc_phy_is_owned(dig_port));
793 void intel_tc_port_lock(struct intel_digital_port *dig_port)
795 __intel_tc_port_lock(dig_port, 1);
799 * intel_tc_port_disconnect_phy_work: disconnect TypeC PHY from display port
800 * @dig_port: digital port
802 * Disconnect the given digital port from its TypeC PHY (handing back the
803 * control of the PHY to the TypeC subsystem). This will happen in a delayed
804 * manner after each aux transactions and modeset disables.
806 static void intel_tc_port_disconnect_phy_work(struct work_struct *work)
808 struct intel_digital_port *dig_port =
809 container_of(work, struct intel_digital_port, tc_disconnect_phy_work.work);
811 mutex_lock(&dig_port->tc_lock);
813 if (!dig_port->tc_link_refcount)
814 intel_tc_port_update_mode(dig_port, 1, true);
816 mutex_unlock(&dig_port->tc_lock);
820 * intel_tc_port_flush_work: flush the work disconnecting the PHY
821 * @dig_port: digital port
823 * Flush the delayed work disconnecting an idle PHY.
825 void intel_tc_port_flush_work(struct intel_digital_port *dig_port)
827 flush_delayed_work(&dig_port->tc_disconnect_phy_work);
830 void intel_tc_port_unlock(struct intel_digital_port *dig_port)
832 if (!dig_port->tc_link_refcount && dig_port->tc_mode != TC_PORT_DISCONNECTED)
833 queue_delayed_work(system_unbound_wq, &dig_port->tc_disconnect_phy_work,
834 msecs_to_jiffies(1000));
836 mutex_unlock(&dig_port->tc_lock);
839 bool intel_tc_port_ref_held(struct intel_digital_port *dig_port)
841 return mutex_is_locked(&dig_port->tc_lock) ||
842 dig_port->tc_link_refcount;
845 void intel_tc_port_get_link(struct intel_digital_port *dig_port,
848 __intel_tc_port_lock(dig_port, required_lanes);
849 dig_port->tc_link_refcount++;
850 intel_tc_port_unlock(dig_port);
853 void intel_tc_port_put_link(struct intel_digital_port *dig_port)
855 intel_tc_port_lock(dig_port);
856 --dig_port->tc_link_refcount;
857 intel_tc_port_unlock(dig_port);
860 * Disconnecting the PHY after the PHY's PLL gets disabled may
861 * hang the system on ADL-P, so disconnect the PHY here synchronously.
862 * TODO: remove this once the root cause of the ordering requirement
865 intel_tc_port_flush_work(dig_port);
869 tc_has_modular_fia(struct drm_i915_private *i915, struct intel_digital_port *dig_port)
871 enum intel_display_power_domain domain;
872 intel_wakeref_t wakeref;
875 if (!INTEL_INFO(i915)->display.has_modular_fia)
878 mutex_lock(&dig_port->tc_lock);
879 wakeref = tc_cold_block(dig_port, &domain);
880 val = intel_uncore_read(&i915->uncore, PORT_TX_DFLEXDPSP(FIA1));
881 tc_cold_unblock(dig_port, domain, wakeref);
882 mutex_unlock(&dig_port->tc_lock);
884 drm_WARN_ON(&i915->drm, val == 0xffffffff);
886 return val & MODULAR_FIA_MASK;
890 tc_port_load_fia_params(struct drm_i915_private *i915, struct intel_digital_port *dig_port)
892 enum port port = dig_port->base.port;
893 enum tc_port tc_port = intel_port_to_tc(i915, port);
896 * Each Modular FIA instance houses 2 TC ports. In SOC that has more
897 * than two TC ports, there are multiple instances of Modular FIA.
899 if (tc_has_modular_fia(i915, dig_port)) {
900 dig_port->tc_phy_fia = tc_port / 2;
901 dig_port->tc_phy_fia_idx = tc_port % 2;
903 dig_port->tc_phy_fia = FIA1;
904 dig_port->tc_phy_fia_idx = tc_port;
908 void intel_tc_port_init(struct intel_digital_port *dig_port, bool is_legacy)
910 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
911 enum port port = dig_port->base.port;
912 enum tc_port tc_port = intel_port_to_tc(i915, port);
914 if (drm_WARN_ON(&i915->drm, tc_port == TC_PORT_NONE))
917 snprintf(dig_port->tc_port_name, sizeof(dig_port->tc_port_name),
918 "%c/TC#%d", port_name(port), tc_port + 1);
920 mutex_init(&dig_port->tc_lock);
921 INIT_DELAYED_WORK(&dig_port->tc_disconnect_phy_work, intel_tc_port_disconnect_phy_work);
922 dig_port->tc_legacy_port = is_legacy;
923 dig_port->tc_mode = TC_PORT_DISCONNECTED;
924 dig_port->tc_link_refcount = 0;
925 tc_port_load_fia_params(i915, dig_port);