1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2019 Intel Corporation
4 */
5
6 #include <drm/drm_print.h>
7
8 #include "i915_reg.h"
9 #include "i915_utils.h"
10 #include "intel_atomic.h"
11 #include "intel_cx0_phy_regs.h"
12 #include "intel_ddi.h"
13 #include "intel_de.h"
14 #include "intel_display.h"
15 #include "intel_display_driver.h"
16 #include "intel_display_power_map.h"
17 #include "intel_display_regs.h"
18 #include "intel_display_types.h"
19 #include "intel_dkl_phy_regs.h"
20 #include "intel_dp.h"
21 #include "intel_dp_mst.h"
22 #include "intel_mg_phy_regs.h"
23 #include "intel_modeset_lock.h"
24 #include "intel_tc.h"
25
26 #define DP_PIN_ASSIGNMENT_NONE 0x0
27 #define DP_PIN_ASSIGNMENT_C 0x3
28 #define DP_PIN_ASSIGNMENT_D 0x4
29 #define DP_PIN_ASSIGNMENT_E 0x5
30
31 enum tc_port_mode {
32 TC_PORT_DISCONNECTED,
33 TC_PORT_TBT_ALT,
34 TC_PORT_DP_ALT,
35 TC_PORT_LEGACY,
36 };
37
38 struct intel_tc_port;
39
40 struct intel_tc_phy_ops {
41 enum intel_display_power_domain (*cold_off_domain)(struct intel_tc_port *tc);
42 u32 (*hpd_live_status)(struct intel_tc_port *tc);
43 bool (*is_ready)(struct intel_tc_port *tc);
44 bool (*is_owned)(struct intel_tc_port *tc);
45 void (*get_hw_state)(struct intel_tc_port *tc);
46 bool (*connect)(struct intel_tc_port *tc, int required_lanes);
47 void (*disconnect)(struct intel_tc_port *tc);
48 void (*init)(struct intel_tc_port *tc);
49 };
50
51 struct intel_tc_port {
52 struct intel_digital_port *dig_port;
53
54 const struct intel_tc_phy_ops *phy_ops;
55
56 struct mutex lock; /* protects the TypeC port mode */
57 intel_wakeref_t lock_wakeref;
58 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
59 enum intel_display_power_domain lock_power_domain;
60 #endif
61 struct delayed_work disconnect_phy_work;
62 struct delayed_work link_reset_work;
63 int link_refcount;
64 bool legacy_port:1;
65 const char *port_name;
66 enum tc_port_mode mode;
67 enum tc_port_mode init_mode;
68 enum phy_fia phy_fia;
69 u8 phy_fia_idx;
70 u8 max_lane_count;
71 };
72
73 static enum intel_display_power_domain
74 tc_phy_cold_off_domain(struct intel_tc_port *);
75 static u32 tc_phy_hpd_live_status(struct intel_tc_port *tc);
76 static bool tc_phy_is_ready(struct intel_tc_port *tc);
77 static bool tc_phy_wait_for_ready(struct intel_tc_port *tc);
78 static enum tc_port_mode tc_phy_get_current_mode(struct intel_tc_port *tc);
79
tc_port_mode_name(enum tc_port_mode mode)80 static const char *tc_port_mode_name(enum tc_port_mode mode)
81 {
82 static const char * const names[] = {
83 [TC_PORT_DISCONNECTED] = "disconnected",
84 [TC_PORT_TBT_ALT] = "tbt-alt",
85 [TC_PORT_DP_ALT] = "dp-alt",
86 [TC_PORT_LEGACY] = "legacy",
87 };
88
89 if (WARN_ON(mode >= ARRAY_SIZE(names)))
90 mode = TC_PORT_DISCONNECTED;
91
92 return names[mode];
93 }
94
to_tc_port(struct intel_digital_port * dig_port)95 static struct intel_tc_port *to_tc_port(struct intel_digital_port *dig_port)
96 {
97 return dig_port->tc;
98 }
99
intel_tc_port_in_mode(struct intel_digital_port * dig_port,enum tc_port_mode mode)100 static bool intel_tc_port_in_mode(struct intel_digital_port *dig_port,
101 enum tc_port_mode mode)
102 {
103 struct intel_tc_port *tc = to_tc_port(dig_port);
104
105 return intel_encoder_is_tc(&dig_port->base) && tc->mode == mode;
106 }
107
intel_tc_port_in_tbt_alt_mode(struct intel_digital_port * dig_port)108 bool intel_tc_port_in_tbt_alt_mode(struct intel_digital_port *dig_port)
109 {
110 return intel_tc_port_in_mode(dig_port, TC_PORT_TBT_ALT);
111 }
112
intel_tc_port_in_dp_alt_mode(struct intel_digital_port * dig_port)113 bool intel_tc_port_in_dp_alt_mode(struct intel_digital_port *dig_port)
114 {
115 return intel_tc_port_in_mode(dig_port, TC_PORT_DP_ALT);
116 }
117
intel_tc_port_in_legacy_mode(struct intel_digital_port * dig_port)118 bool intel_tc_port_in_legacy_mode(struct intel_digital_port *dig_port)
119 {
120 return intel_tc_port_in_mode(dig_port, TC_PORT_LEGACY);
121 }
122
intel_tc_port_handles_hpd_glitches(struct intel_digital_port * dig_port)123 bool intel_tc_port_handles_hpd_glitches(struct intel_digital_port *dig_port)
124 {
125 struct intel_tc_port *tc = to_tc_port(dig_port);
126
127 return intel_encoder_is_tc(&dig_port->base) && !tc->legacy_port;
128 }
129
130 /*
131 * The display power domains used for TC ports depending on the
132 * platform and TC mode (legacy, DP-alt, TBT):
133 *
134 * POWER_DOMAIN_DISPLAY_CORE:
135 * --------------------------
136 * ADLP/all modes:
137 * - TCSS/IOM access for PHY ready state.
138 * ADLP+/all modes:
139 * - DE/north-,south-HPD ISR access for HPD live state.
140 *
141 * POWER_DOMAIN_PORT_DDI_LANES_<port>:
142 * -----------------------------------
143 * ICL+/all modes:
144 * - DE/DDI_BUF access for port enabled state.
145 * ADLP/all modes:
146 * - DE/DDI_BUF access for PHY owned state.
147 *
148 * POWER_DOMAIN_AUX_USBC<TC port index>:
149 * -------------------------------------
150 * ICL/legacy mode:
151 * - TCSS/IOM,FIA access for PHY ready, owned and HPD live state
152 * - TCSS/PHY: block TC-cold power state for using the PHY AUX and
153 * main lanes.
154 * ADLP/legacy, DP-alt modes:
155 * - TCSS/PHY: block TC-cold power state for using the PHY AUX and
156 * main lanes.
157 *
158 * POWER_DOMAIN_TC_COLD_OFF:
159 * -------------------------
160 * ICL/DP-alt, TBT mode:
161 * - TCSS/TBT: block TC-cold power state for using the (direct or
162 * TBT DP-IN) AUX and main lanes.
163 *
164 * TGL/all modes:
165 * - TCSS/IOM,FIA access for PHY ready, owned and HPD live state
166 * - TCSS/PHY: block TC-cold power state for using the (direct or
167 * TBT DP-IN) AUX and main lanes.
168 *
169 * ADLP/TBT mode:
170 * - TCSS/TBT: block TC-cold power state for using the (TBT DP-IN)
171 * AUX and main lanes.
172 *
173 * XELPDP+/all modes:
174 * - TCSS/IOM,FIA access for PHY ready, owned state
175 * - TCSS/PHY: block TC-cold power state for using the (direct or
176 * TBT DP-IN) AUX and main lanes.
177 */
intel_tc_cold_requires_aux_pw(struct intel_digital_port * dig_port)178 bool intel_tc_cold_requires_aux_pw(struct intel_digital_port *dig_port)
179 {
180 struct intel_display *display = to_intel_display(dig_port);
181 struct intel_tc_port *tc = to_tc_port(dig_port);
182
183 return tc_phy_cold_off_domain(tc) ==
184 intel_display_power_legacy_aux_domain(display, dig_port->aux_ch);
185 }
186
187 static intel_wakeref_t
__tc_cold_block(struct intel_tc_port * tc,enum intel_display_power_domain * domain)188 __tc_cold_block(struct intel_tc_port *tc, enum intel_display_power_domain *domain)
189 {
190 struct intel_display *display = to_intel_display(tc->dig_port);
191
192 *domain = tc_phy_cold_off_domain(tc);
193
194 return intel_display_power_get(display, *domain);
195 }
196
197 static intel_wakeref_t
tc_cold_block(struct intel_tc_port * tc)198 tc_cold_block(struct intel_tc_port *tc)
199 {
200 enum intel_display_power_domain domain;
201 intel_wakeref_t wakeref;
202
203 wakeref = __tc_cold_block(tc, &domain);
204 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
205 tc->lock_power_domain = domain;
206 #endif
207 return wakeref;
208 }
209
210 static void
__tc_cold_unblock(struct intel_tc_port * tc,enum intel_display_power_domain domain,intel_wakeref_t wakeref)211 __tc_cold_unblock(struct intel_tc_port *tc, enum intel_display_power_domain domain,
212 intel_wakeref_t wakeref)
213 {
214 struct intel_display *display = to_intel_display(tc->dig_port);
215
216 intel_display_power_put(display, domain, wakeref);
217 }
218
219 static void
tc_cold_unblock(struct intel_tc_port * tc,intel_wakeref_t wakeref)220 tc_cold_unblock(struct intel_tc_port *tc, intel_wakeref_t wakeref)
221 {
222 struct intel_display __maybe_unused *display = to_intel_display(tc->dig_port);
223 enum intel_display_power_domain domain = tc_phy_cold_off_domain(tc);
224
225 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
226 drm_WARN_ON(display->drm, tc->lock_power_domain != domain);
227 #endif
228 __tc_cold_unblock(tc, domain, wakeref);
229 }
230
231 static void
assert_display_core_power_enabled(struct intel_tc_port * tc)232 assert_display_core_power_enabled(struct intel_tc_port *tc)
233 {
234 struct intel_display *display = to_intel_display(tc->dig_port);
235
236 drm_WARN_ON(display->drm,
237 !intel_display_power_is_enabled(display, POWER_DOMAIN_DISPLAY_CORE));
238 }
239
240 static void
assert_tc_cold_blocked(struct intel_tc_port * tc)241 assert_tc_cold_blocked(struct intel_tc_port *tc)
242 {
243 struct intel_display *display = to_intel_display(tc->dig_port);
244 bool enabled;
245
246 enabled = intel_display_power_is_enabled(display,
247 tc_phy_cold_off_domain(tc));
248 drm_WARN_ON(display->drm, !enabled);
249 }
250
251 static enum intel_display_power_domain
tc_port_power_domain(struct intel_tc_port * tc)252 tc_port_power_domain(struct intel_tc_port *tc)
253 {
254 enum tc_port tc_port = intel_encoder_to_tc(&tc->dig_port->base);
255
256 return POWER_DOMAIN_PORT_DDI_LANES_TC1 + tc_port - TC_PORT_1;
257 }
258
259 static void
assert_tc_port_power_enabled(struct intel_tc_port * tc)260 assert_tc_port_power_enabled(struct intel_tc_port *tc)
261 {
262 struct intel_display *display = to_intel_display(tc->dig_port);
263
264 drm_WARN_ON(display->drm,
265 !intel_display_power_is_enabled(display, tc_port_power_domain(tc)));
266 }
267
intel_tc_port_get_lane_mask(struct intel_digital_port * dig_port)268 static u32 intel_tc_port_get_lane_mask(struct intel_digital_port *dig_port)
269 {
270 struct intel_display *display = to_intel_display(dig_port);
271 struct intel_tc_port *tc = to_tc_port(dig_port);
272 u32 lane_mask;
273
274 lane_mask = intel_de_read(display, PORT_TX_DFLEXDPSP(tc->phy_fia));
275
276 drm_WARN_ON(display->drm, lane_mask == 0xffffffff);
277 assert_tc_cold_blocked(tc);
278
279 lane_mask &= DP_LANE_ASSIGNMENT_MASK(tc->phy_fia_idx);
280 return lane_mask >> DP_LANE_ASSIGNMENT_SHIFT(tc->phy_fia_idx);
281 }
282
intel_tc_port_get_pin_assignment_mask(struct intel_digital_port * dig_port)283 u32 intel_tc_port_get_pin_assignment_mask(struct intel_digital_port *dig_port)
284 {
285 struct intel_display *display = to_intel_display(dig_port);
286 struct intel_tc_port *tc = to_tc_port(dig_port);
287 u32 pin_mask;
288
289 pin_mask = intel_de_read(display, PORT_TX_DFLEXPA1(tc->phy_fia));
290
291 drm_WARN_ON(display->drm, pin_mask == 0xffffffff);
292 assert_tc_cold_blocked(tc);
293
294 return (pin_mask & DP_PIN_ASSIGNMENT_MASK(tc->phy_fia_idx)) >>
295 DP_PIN_ASSIGNMENT_SHIFT(tc->phy_fia_idx);
296 }
297
lnl_tc_port_get_max_lane_count(struct intel_digital_port * dig_port)298 static int lnl_tc_port_get_max_lane_count(struct intel_digital_port *dig_port)
299 {
300 struct intel_display *display = to_intel_display(dig_port);
301 enum tc_port tc_port = intel_encoder_to_tc(&dig_port->base);
302 intel_wakeref_t wakeref;
303 u32 val, pin_assignment;
304
305 with_intel_display_power(display, POWER_DOMAIN_DISPLAY_CORE, wakeref)
306 val = intel_de_read(display, TCSS_DDI_STATUS(tc_port));
307
308 pin_assignment =
309 REG_FIELD_GET(TCSS_DDI_STATUS_PIN_ASSIGNMENT_MASK, val);
310
311 switch (pin_assignment) {
312 case DP_PIN_ASSIGNMENT_NONE:
313 return 0;
314 default:
315 MISSING_CASE(pin_assignment);
316 fallthrough;
317 case DP_PIN_ASSIGNMENT_D:
318 return 2;
319 case DP_PIN_ASSIGNMENT_C:
320 case DP_PIN_ASSIGNMENT_E:
321 return 4;
322 }
323 }
324
mtl_tc_port_get_max_lane_count(struct intel_digital_port * dig_port)325 static int mtl_tc_port_get_max_lane_count(struct intel_digital_port *dig_port)
326 {
327 struct intel_display *display = to_intel_display(dig_port);
328 intel_wakeref_t wakeref;
329 u32 pin_mask;
330
331 with_intel_display_power(display, POWER_DOMAIN_DISPLAY_CORE, wakeref)
332 pin_mask = intel_tc_port_get_pin_assignment_mask(dig_port);
333
334 switch (pin_mask) {
335 default:
336 MISSING_CASE(pin_mask);
337 fallthrough;
338 case DP_PIN_ASSIGNMENT_D:
339 return 2;
340 case DP_PIN_ASSIGNMENT_C:
341 case DP_PIN_ASSIGNMENT_E:
342 return 4;
343 }
344 }
345
intel_tc_port_get_max_lane_count(struct intel_digital_port * dig_port)346 static int intel_tc_port_get_max_lane_count(struct intel_digital_port *dig_port)
347 {
348 struct intel_display *display = to_intel_display(dig_port);
349 intel_wakeref_t wakeref;
350 u32 lane_mask = 0;
351
352 with_intel_display_power(display, POWER_DOMAIN_DISPLAY_CORE, wakeref)
353 lane_mask = intel_tc_port_get_lane_mask(dig_port);
354
355 switch (lane_mask) {
356 default:
357 MISSING_CASE(lane_mask);
358 fallthrough;
359 case 0x1:
360 case 0x2:
361 case 0x4:
362 case 0x8:
363 return 1;
364 case 0x3:
365 case 0xc:
366 return 2;
367 case 0xf:
368 return 4;
369 }
370 }
371
get_max_lane_count(struct intel_tc_port * tc)372 static int get_max_lane_count(struct intel_tc_port *tc)
373 {
374 struct intel_display *display = to_intel_display(tc->dig_port);
375 struct intel_digital_port *dig_port = tc->dig_port;
376
377 if (tc->mode != TC_PORT_DP_ALT)
378 return 4;
379
380 assert_tc_cold_blocked(tc);
381
382 if (DISPLAY_VER(display) >= 20)
383 return lnl_tc_port_get_max_lane_count(dig_port);
384
385 if (DISPLAY_VER(display) >= 14)
386 return mtl_tc_port_get_max_lane_count(dig_port);
387
388 return intel_tc_port_get_max_lane_count(dig_port);
389 }
390
read_pin_configuration(struct intel_tc_port * tc)391 static void read_pin_configuration(struct intel_tc_port *tc)
392 {
393 tc->max_lane_count = get_max_lane_count(tc);
394 }
395
intel_tc_port_max_lane_count(struct intel_digital_port * dig_port)396 int intel_tc_port_max_lane_count(struct intel_digital_port *dig_port)
397 {
398 struct intel_display *display = to_intel_display(dig_port);
399 struct intel_tc_port *tc = to_tc_port(dig_port);
400
401 if (!intel_encoder_is_tc(&dig_port->base))
402 return 4;
403
404 if (DISPLAY_VER(display) < 20)
405 return get_max_lane_count(tc);
406
407 return tc->max_lane_count;
408 }
409
intel_tc_port_set_fia_lane_count(struct intel_digital_port * dig_port,int required_lanes)410 void intel_tc_port_set_fia_lane_count(struct intel_digital_port *dig_port,
411 int required_lanes)
412 {
413 struct intel_display *display = to_intel_display(dig_port);
414 struct intel_tc_port *tc = to_tc_port(dig_port);
415 bool lane_reversal = dig_port->lane_reversal;
416 u32 val;
417
418 if (DISPLAY_VER(display) >= 14)
419 return;
420
421 drm_WARN_ON(display->drm,
422 lane_reversal && tc->mode != TC_PORT_LEGACY);
423
424 assert_tc_cold_blocked(tc);
425
426 val = intel_de_read(display, PORT_TX_DFLEXDPMLE1(tc->phy_fia));
427 val &= ~DFLEXDPMLE1_DPMLETC_MASK(tc->phy_fia_idx);
428
429 switch (required_lanes) {
430 case 1:
431 val |= lane_reversal ?
432 DFLEXDPMLE1_DPMLETC_ML3(tc->phy_fia_idx) :
433 DFLEXDPMLE1_DPMLETC_ML0(tc->phy_fia_idx);
434 break;
435 case 2:
436 val |= lane_reversal ?
437 DFLEXDPMLE1_DPMLETC_ML3_2(tc->phy_fia_idx) :
438 DFLEXDPMLE1_DPMLETC_ML1_0(tc->phy_fia_idx);
439 break;
440 case 4:
441 val |= DFLEXDPMLE1_DPMLETC_ML3_0(tc->phy_fia_idx);
442 break;
443 default:
444 MISSING_CASE(required_lanes);
445 }
446
447 intel_de_write(display, PORT_TX_DFLEXDPMLE1(tc->phy_fia), val);
448 }
449
tc_port_fixup_legacy_flag(struct intel_tc_port * tc,u32 live_status_mask)450 static void tc_port_fixup_legacy_flag(struct intel_tc_port *tc,
451 u32 live_status_mask)
452 {
453 struct intel_display *display = to_intel_display(tc->dig_port);
454 u32 valid_hpd_mask;
455
456 drm_WARN_ON(display->drm, tc->mode != TC_PORT_DISCONNECTED);
457
458 if (hweight32(live_status_mask) != 1)
459 return;
460
461 if (tc->legacy_port)
462 valid_hpd_mask = BIT(TC_PORT_LEGACY);
463 else
464 valid_hpd_mask = BIT(TC_PORT_DP_ALT) |
465 BIT(TC_PORT_TBT_ALT);
466
467 if (!(live_status_mask & ~valid_hpd_mask))
468 return;
469
470 /* If live status mismatches the VBT flag, trust the live status. */
471 drm_dbg_kms(display->drm,
472 "Port %s: live status %08x mismatches the legacy port flag %08x, fixing flag\n",
473 tc->port_name, live_status_mask, valid_hpd_mask);
474
475 tc->legacy_port = !tc->legacy_port;
476 }
477
tc_phy_load_fia_params(struct intel_tc_port * tc,bool modular_fia)478 static void tc_phy_load_fia_params(struct intel_tc_port *tc, bool modular_fia)
479 {
480 enum tc_port tc_port = intel_encoder_to_tc(&tc->dig_port->base);
481
482 /*
483 * Each Modular FIA instance houses 2 TC ports. In SOC that has more
484 * than two TC ports, there are multiple instances of Modular FIA.
485 */
486 if (modular_fia) {
487 tc->phy_fia = tc_port / 2;
488 tc->phy_fia_idx = tc_port % 2;
489 } else {
490 tc->phy_fia = FIA1;
491 tc->phy_fia_idx = tc_port;
492 }
493 }
494
495 /*
496 * ICL TC PHY handlers
497 * -------------------
498 */
499 static enum intel_display_power_domain
icl_tc_phy_cold_off_domain(struct intel_tc_port * tc)500 icl_tc_phy_cold_off_domain(struct intel_tc_port *tc)
501 {
502 struct intel_display *display = to_intel_display(tc->dig_port);
503 struct intel_digital_port *dig_port = tc->dig_port;
504
505 if (tc->legacy_port)
506 return intel_display_power_legacy_aux_domain(display, dig_port->aux_ch);
507
508 return POWER_DOMAIN_TC_COLD_OFF;
509 }
510
icl_tc_phy_hpd_live_status(struct intel_tc_port * tc)511 static u32 icl_tc_phy_hpd_live_status(struct intel_tc_port *tc)
512 {
513 struct intel_display *display = to_intel_display(tc->dig_port);
514 struct intel_digital_port *dig_port = tc->dig_port;
515 u32 isr_bit = display->hotplug.pch_hpd[dig_port->base.hpd_pin];
516 intel_wakeref_t wakeref;
517 u32 fia_isr;
518 u32 pch_isr;
519 u32 mask = 0;
520
521 with_intel_display_power(display, tc_phy_cold_off_domain(tc), wakeref) {
522 fia_isr = intel_de_read(display, PORT_TX_DFLEXDPSP(tc->phy_fia));
523 pch_isr = intel_de_read(display, SDEISR);
524 }
525
526 if (fia_isr == 0xffffffff) {
527 drm_dbg_kms(display->drm,
528 "Port %s: PHY in TCCOLD, nothing connected\n",
529 tc->port_name);
530 return mask;
531 }
532
533 if (fia_isr & TC_LIVE_STATE_TBT(tc->phy_fia_idx))
534 mask |= BIT(TC_PORT_TBT_ALT);
535 if (fia_isr & TC_LIVE_STATE_TC(tc->phy_fia_idx))
536 mask |= BIT(TC_PORT_DP_ALT);
537
538 if (pch_isr & isr_bit)
539 mask |= BIT(TC_PORT_LEGACY);
540
541 return mask;
542 }
543
544 /*
545 * Return the PHY status complete flag indicating that display can acquire the
546 * PHY ownership. The IOM firmware sets this flag when a DP-alt or legacy sink
547 * is connected and it's ready to switch the ownership to display. The flag
548 * will be left cleared when a TBT-alt sink is connected, where the PHY is
549 * owned by the TBT subsystem and so switching the ownership to display is not
550 * required.
551 */
icl_tc_phy_is_ready(struct intel_tc_port * tc)552 static bool icl_tc_phy_is_ready(struct intel_tc_port *tc)
553 {
554 struct intel_display *display = to_intel_display(tc->dig_port);
555 u32 val;
556
557 assert_tc_cold_blocked(tc);
558
559 val = intel_de_read(display, PORT_TX_DFLEXDPPMS(tc->phy_fia));
560 if (val == 0xffffffff) {
561 drm_dbg_kms(display->drm,
562 "Port %s: PHY in TCCOLD, assuming not ready\n",
563 tc->port_name);
564 return false;
565 }
566
567 return val & DP_PHY_MODE_STATUS_COMPLETED(tc->phy_fia_idx);
568 }
569
icl_tc_phy_take_ownership(struct intel_tc_port * tc,bool take)570 static bool icl_tc_phy_take_ownership(struct intel_tc_port *tc,
571 bool take)
572 {
573 struct intel_display *display = to_intel_display(tc->dig_port);
574 u32 val;
575
576 assert_tc_cold_blocked(tc);
577
578 val = intel_de_read(display, PORT_TX_DFLEXDPCSSS(tc->phy_fia));
579 if (val == 0xffffffff) {
580 drm_dbg_kms(display->drm,
581 "Port %s: PHY in TCCOLD, can't %s ownership\n",
582 tc->port_name, take ? "take" : "release");
583
584 return false;
585 }
586
587 val &= ~DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
588 if (take)
589 val |= DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
590
591 intel_de_write(display, PORT_TX_DFLEXDPCSSS(tc->phy_fia), val);
592
593 return true;
594 }
595
icl_tc_phy_is_owned(struct intel_tc_port * tc)596 static bool icl_tc_phy_is_owned(struct intel_tc_port *tc)
597 {
598 struct intel_display *display = to_intel_display(tc->dig_port);
599 u32 val;
600
601 assert_tc_cold_blocked(tc);
602
603 val = intel_de_read(display, PORT_TX_DFLEXDPCSSS(tc->phy_fia));
604 if (val == 0xffffffff) {
605 drm_dbg_kms(display->drm,
606 "Port %s: PHY in TCCOLD, assume not owned\n",
607 tc->port_name);
608 return false;
609 }
610
611 return val & DP_PHY_MODE_STATUS_NOT_SAFE(tc->phy_fia_idx);
612 }
613
icl_tc_phy_get_hw_state(struct intel_tc_port * tc)614 static void icl_tc_phy_get_hw_state(struct intel_tc_port *tc)
615 {
616 enum intel_display_power_domain domain;
617 intel_wakeref_t tc_cold_wref;
618
619 tc_cold_wref = __tc_cold_block(tc, &domain);
620
621 tc->mode = tc_phy_get_current_mode(tc);
622 if (tc->mode != TC_PORT_DISCONNECTED) {
623 tc->lock_wakeref = tc_cold_block(tc);
624
625 read_pin_configuration(tc);
626 }
627
628 __tc_cold_unblock(tc, domain, tc_cold_wref);
629 }
630
631 /*
632 * This function implements the first part of the Connect Flow described by our
633 * specification, Gen11 TypeC Programming chapter. The rest of the flow (reading
634 * lanes, EDID, etc) is done as needed in the typical places.
635 *
636 * Unlike the other ports, type-C ports are not available to use as soon as we
637 * get a hotplug. The type-C PHYs can be shared between multiple controllers:
638 * display, USB, etc. As a result, handshaking through FIA is required around
639 * connect and disconnect to cleanly transfer ownership with the controller and
640 * set the type-C power state.
641 */
tc_phy_verify_legacy_or_dp_alt_mode(struct intel_tc_port * tc,int required_lanes)642 static bool tc_phy_verify_legacy_or_dp_alt_mode(struct intel_tc_port *tc,
643 int required_lanes)
644 {
645 struct intel_display *display = to_intel_display(tc->dig_port);
646 struct intel_digital_port *dig_port = tc->dig_port;
647 int max_lanes;
648
649 max_lanes = intel_tc_port_max_lane_count(dig_port);
650 if (tc->mode == TC_PORT_LEGACY) {
651 drm_WARN_ON(display->drm, max_lanes != 4);
652 return true;
653 }
654
655 drm_WARN_ON(display->drm, tc->mode != TC_PORT_DP_ALT);
656
657 /*
658 * Now we have to re-check the live state, in case the port recently
659 * became disconnected. Not necessary for legacy mode.
660 */
661 if (!(tc_phy_hpd_live_status(tc) & BIT(TC_PORT_DP_ALT))) {
662 drm_dbg_kms(display->drm, "Port %s: PHY sudden disconnect\n",
663 tc->port_name);
664 return false;
665 }
666
667 if (max_lanes < required_lanes) {
668 drm_dbg_kms(display->drm,
669 "Port %s: PHY max lanes %d < required lanes %d\n",
670 tc->port_name,
671 max_lanes, required_lanes);
672 return false;
673 }
674
675 return true;
676 }
677
icl_tc_phy_connect(struct intel_tc_port * tc,int required_lanes)678 static bool icl_tc_phy_connect(struct intel_tc_port *tc,
679 int required_lanes)
680 {
681 struct intel_display *display = to_intel_display(tc->dig_port);
682
683 tc->lock_wakeref = tc_cold_block(tc);
684
685 if (tc->mode == TC_PORT_TBT_ALT) {
686 read_pin_configuration(tc);
687
688 return true;
689 }
690
691 if ((!tc_phy_is_ready(tc) ||
692 !icl_tc_phy_take_ownership(tc, true)) &&
693 !drm_WARN_ON(display->drm, tc->mode == TC_PORT_LEGACY)) {
694 drm_dbg_kms(display->drm, "Port %s: can't take PHY ownership (ready %s)\n",
695 tc->port_name,
696 str_yes_no(tc_phy_is_ready(tc)));
697 goto out_unblock_tc_cold;
698 }
699
700 read_pin_configuration(tc);
701
702 if (!tc_phy_verify_legacy_or_dp_alt_mode(tc, required_lanes))
703 goto out_release_phy;
704
705 return true;
706
707 out_release_phy:
708 icl_tc_phy_take_ownership(tc, false);
709 out_unblock_tc_cold:
710 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
711
712 return false;
713 }
714
715 /*
716 * See the comment at the connect function. This implements the Disconnect
717 * Flow.
718 */
icl_tc_phy_disconnect(struct intel_tc_port * tc)719 static void icl_tc_phy_disconnect(struct intel_tc_port *tc)
720 {
721 switch (tc->mode) {
722 case TC_PORT_LEGACY:
723 case TC_PORT_DP_ALT:
724 icl_tc_phy_take_ownership(tc, false);
725 fallthrough;
726 case TC_PORT_TBT_ALT:
727 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
728 break;
729 default:
730 MISSING_CASE(tc->mode);
731 }
732 }
733
icl_tc_phy_init(struct intel_tc_port * tc)734 static void icl_tc_phy_init(struct intel_tc_port *tc)
735 {
736 tc_phy_load_fia_params(tc, false);
737 }
738
739 static const struct intel_tc_phy_ops icl_tc_phy_ops = {
740 .cold_off_domain = icl_tc_phy_cold_off_domain,
741 .hpd_live_status = icl_tc_phy_hpd_live_status,
742 .is_ready = icl_tc_phy_is_ready,
743 .is_owned = icl_tc_phy_is_owned,
744 .get_hw_state = icl_tc_phy_get_hw_state,
745 .connect = icl_tc_phy_connect,
746 .disconnect = icl_tc_phy_disconnect,
747 .init = icl_tc_phy_init,
748 };
749
750 /*
751 * TGL TC PHY handlers
752 * -------------------
753 */
754 static enum intel_display_power_domain
tgl_tc_phy_cold_off_domain(struct intel_tc_port * tc)755 tgl_tc_phy_cold_off_domain(struct intel_tc_port *tc)
756 {
757 return POWER_DOMAIN_TC_COLD_OFF;
758 }
759
tgl_tc_phy_init(struct intel_tc_port * tc)760 static void tgl_tc_phy_init(struct intel_tc_port *tc)
761 {
762 struct intel_display *display = to_intel_display(tc->dig_port);
763 intel_wakeref_t wakeref;
764 u32 val;
765
766 with_intel_display_power(display, tc_phy_cold_off_domain(tc), wakeref)
767 val = intel_de_read(display, PORT_TX_DFLEXDPSP(FIA1));
768
769 drm_WARN_ON(display->drm, val == 0xffffffff);
770
771 tc_phy_load_fia_params(tc, val & MODULAR_FIA_MASK);
772 }
773
774 static const struct intel_tc_phy_ops tgl_tc_phy_ops = {
775 .cold_off_domain = tgl_tc_phy_cold_off_domain,
776 .hpd_live_status = icl_tc_phy_hpd_live_status,
777 .is_ready = icl_tc_phy_is_ready,
778 .is_owned = icl_tc_phy_is_owned,
779 .get_hw_state = icl_tc_phy_get_hw_state,
780 .connect = icl_tc_phy_connect,
781 .disconnect = icl_tc_phy_disconnect,
782 .init = tgl_tc_phy_init,
783 };
784
785 /*
786 * ADLP TC PHY handlers
787 * --------------------
788 */
789 static enum intel_display_power_domain
adlp_tc_phy_cold_off_domain(struct intel_tc_port * tc)790 adlp_tc_phy_cold_off_domain(struct intel_tc_port *tc)
791 {
792 struct intel_display *display = to_intel_display(tc->dig_port);
793 struct intel_digital_port *dig_port = tc->dig_port;
794
795 if (tc->mode != TC_PORT_TBT_ALT)
796 return intel_display_power_legacy_aux_domain(display, dig_port->aux_ch);
797
798 return POWER_DOMAIN_TC_COLD_OFF;
799 }
800
adlp_tc_phy_hpd_live_status(struct intel_tc_port * tc)801 static u32 adlp_tc_phy_hpd_live_status(struct intel_tc_port *tc)
802 {
803 struct intel_display *display = to_intel_display(tc->dig_port);
804 struct intel_digital_port *dig_port = tc->dig_port;
805 enum hpd_pin hpd_pin = dig_port->base.hpd_pin;
806 u32 cpu_isr_bits = display->hotplug.hpd[hpd_pin];
807 u32 pch_isr_bit = display->hotplug.pch_hpd[hpd_pin];
808 intel_wakeref_t wakeref;
809 u32 cpu_isr;
810 u32 pch_isr;
811 u32 mask = 0;
812
813 with_intel_display_power(display, POWER_DOMAIN_DISPLAY_CORE, wakeref) {
814 cpu_isr = intel_de_read(display, GEN11_DE_HPD_ISR);
815 pch_isr = intel_de_read(display, SDEISR);
816 }
817
818 if (cpu_isr & (cpu_isr_bits & GEN11_DE_TC_HOTPLUG_MASK))
819 mask |= BIT(TC_PORT_DP_ALT);
820 if (cpu_isr & (cpu_isr_bits & GEN11_DE_TBT_HOTPLUG_MASK))
821 mask |= BIT(TC_PORT_TBT_ALT);
822
823 if (pch_isr & pch_isr_bit)
824 mask |= BIT(TC_PORT_LEGACY);
825
826 return mask;
827 }
828
829 /*
830 * Return the PHY status complete flag indicating that display can acquire the
831 * PHY ownership. The IOM firmware sets this flag when it's ready to switch
832 * the ownership to display, regardless of what sink is connected (TBT-alt,
833 * DP-alt, legacy or nothing). For TBT-alt sinks the PHY is owned by the TBT
834 * subsystem and so switching the ownership to display is not required.
835 */
adlp_tc_phy_is_ready(struct intel_tc_port * tc)836 static bool adlp_tc_phy_is_ready(struct intel_tc_port *tc)
837 {
838 struct intel_display *display = to_intel_display(tc->dig_port);
839 enum tc_port tc_port = intel_encoder_to_tc(&tc->dig_port->base);
840 u32 val;
841
842 assert_display_core_power_enabled(tc);
843
844 val = intel_de_read(display, TCSS_DDI_STATUS(tc_port));
845 if (val == 0xffffffff) {
846 drm_dbg_kms(display->drm,
847 "Port %s: PHY in TCCOLD, assuming not ready\n",
848 tc->port_name);
849 return false;
850 }
851
852 return val & TCSS_DDI_STATUS_READY;
853 }
854
adlp_tc_phy_take_ownership(struct intel_tc_port * tc,bool take)855 static bool adlp_tc_phy_take_ownership(struct intel_tc_port *tc,
856 bool take)
857 {
858 struct intel_display *display = to_intel_display(tc->dig_port);
859 enum port port = tc->dig_port->base.port;
860
861 assert_tc_port_power_enabled(tc);
862
863 intel_de_rmw(display, DDI_BUF_CTL(port), DDI_BUF_CTL_TC_PHY_OWNERSHIP,
864 take ? DDI_BUF_CTL_TC_PHY_OWNERSHIP : 0);
865
866 return true;
867 }
868
adlp_tc_phy_is_owned(struct intel_tc_port * tc)869 static bool adlp_tc_phy_is_owned(struct intel_tc_port *tc)
870 {
871 struct intel_display *display = to_intel_display(tc->dig_port);
872 enum port port = tc->dig_port->base.port;
873 u32 val;
874
875 assert_tc_port_power_enabled(tc);
876
877 val = intel_de_read(display, DDI_BUF_CTL(port));
878 return val & DDI_BUF_CTL_TC_PHY_OWNERSHIP;
879 }
880
adlp_tc_phy_get_hw_state(struct intel_tc_port * tc)881 static void adlp_tc_phy_get_hw_state(struct intel_tc_port *tc)
882 {
883 struct intel_display *display = to_intel_display(tc->dig_port);
884 enum intel_display_power_domain port_power_domain =
885 tc_port_power_domain(tc);
886 intel_wakeref_t port_wakeref;
887
888 port_wakeref = intel_display_power_get(display, port_power_domain);
889
890 tc->mode = tc_phy_get_current_mode(tc);
891 if (tc->mode != TC_PORT_DISCONNECTED) {
892 tc->lock_wakeref = tc_cold_block(tc);
893
894 read_pin_configuration(tc);
895 }
896
897 intel_display_power_put(display, port_power_domain, port_wakeref);
898 }
899
adlp_tc_phy_connect(struct intel_tc_port * tc,int required_lanes)900 static bool adlp_tc_phy_connect(struct intel_tc_port *tc, int required_lanes)
901 {
902 struct intel_display *display = to_intel_display(tc->dig_port);
903 enum intel_display_power_domain port_power_domain =
904 tc_port_power_domain(tc);
905 intel_wakeref_t port_wakeref;
906
907 if (tc->mode == TC_PORT_TBT_ALT) {
908 tc->lock_wakeref = tc_cold_block(tc);
909
910 read_pin_configuration(tc);
911
912 return true;
913 }
914
915 port_wakeref = intel_display_power_get(display, port_power_domain);
916
917 if (!adlp_tc_phy_take_ownership(tc, true) &&
918 !drm_WARN_ON(display->drm, tc->mode == TC_PORT_LEGACY)) {
919 drm_dbg_kms(display->drm, "Port %s: can't take PHY ownership\n",
920 tc->port_name);
921 goto out_put_port_power;
922 }
923
924 if (!tc_phy_is_ready(tc) &&
925 !drm_WARN_ON(display->drm, tc->mode == TC_PORT_LEGACY)) {
926 drm_dbg_kms(display->drm, "Port %s: PHY not ready\n",
927 tc->port_name);
928 goto out_release_phy;
929 }
930
931 tc->lock_wakeref = tc_cold_block(tc);
932
933 read_pin_configuration(tc);
934
935 if (!tc_phy_verify_legacy_or_dp_alt_mode(tc, required_lanes))
936 goto out_unblock_tc_cold;
937
938 intel_display_power_put(display, port_power_domain, port_wakeref);
939
940 return true;
941
942 out_unblock_tc_cold:
943 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
944 out_release_phy:
945 adlp_tc_phy_take_ownership(tc, false);
946 out_put_port_power:
947 intel_display_power_put(display, port_power_domain, port_wakeref);
948
949 return false;
950 }
951
adlp_tc_phy_disconnect(struct intel_tc_port * tc)952 static void adlp_tc_phy_disconnect(struct intel_tc_port *tc)
953 {
954 struct intel_display *display = to_intel_display(tc->dig_port);
955 enum intel_display_power_domain port_power_domain =
956 tc_port_power_domain(tc);
957 intel_wakeref_t port_wakeref;
958
959 port_wakeref = intel_display_power_get(display, port_power_domain);
960
961 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
962
963 switch (tc->mode) {
964 case TC_PORT_LEGACY:
965 case TC_PORT_DP_ALT:
966 adlp_tc_phy_take_ownership(tc, false);
967 fallthrough;
968 case TC_PORT_TBT_ALT:
969 break;
970 default:
971 MISSING_CASE(tc->mode);
972 }
973
974 intel_display_power_put(display, port_power_domain, port_wakeref);
975 }
976
adlp_tc_phy_init(struct intel_tc_port * tc)977 static void adlp_tc_phy_init(struct intel_tc_port *tc)
978 {
979 tc_phy_load_fia_params(tc, true);
980 }
981
982 static const struct intel_tc_phy_ops adlp_tc_phy_ops = {
983 .cold_off_domain = adlp_tc_phy_cold_off_domain,
984 .hpd_live_status = adlp_tc_phy_hpd_live_status,
985 .is_ready = adlp_tc_phy_is_ready,
986 .is_owned = adlp_tc_phy_is_owned,
987 .get_hw_state = adlp_tc_phy_get_hw_state,
988 .connect = adlp_tc_phy_connect,
989 .disconnect = adlp_tc_phy_disconnect,
990 .init = adlp_tc_phy_init,
991 };
992
993 /*
994 * XELPDP TC PHY handlers
995 * ----------------------
996 */
xelpdp_tc_phy_hpd_live_status(struct intel_tc_port * tc)997 static u32 xelpdp_tc_phy_hpd_live_status(struct intel_tc_port *tc)
998 {
999 struct intel_display *display = to_intel_display(tc->dig_port);
1000 struct intel_digital_port *dig_port = tc->dig_port;
1001 enum hpd_pin hpd_pin = dig_port->base.hpd_pin;
1002 u32 pica_isr_bits = display->hotplug.hpd[hpd_pin];
1003 u32 pch_isr_bit = display->hotplug.pch_hpd[hpd_pin];
1004 intel_wakeref_t wakeref;
1005 u32 pica_isr;
1006 u32 pch_isr;
1007 u32 mask = 0;
1008
1009 with_intel_display_power(display, POWER_DOMAIN_DISPLAY_CORE, wakeref) {
1010 pica_isr = intel_de_read(display, PICAINTERRUPT_ISR);
1011 pch_isr = intel_de_read(display, SDEISR);
1012 }
1013
1014 if (pica_isr & (pica_isr_bits & XELPDP_DP_ALT_HOTPLUG_MASK))
1015 mask |= BIT(TC_PORT_DP_ALT);
1016 if (pica_isr & (pica_isr_bits & XELPDP_TBT_HOTPLUG_MASK))
1017 mask |= BIT(TC_PORT_TBT_ALT);
1018
1019 if (tc->legacy_port && (pch_isr & pch_isr_bit))
1020 mask |= BIT(TC_PORT_LEGACY);
1021
1022 return mask;
1023 }
1024
1025 static bool
xelpdp_tc_phy_tcss_power_is_enabled(struct intel_tc_port * tc)1026 xelpdp_tc_phy_tcss_power_is_enabled(struct intel_tc_port *tc)
1027 {
1028 struct intel_display *display = to_intel_display(tc->dig_port);
1029 enum port port = tc->dig_port->base.port;
1030 i915_reg_t reg = XELPDP_PORT_BUF_CTL1(display, port);
1031
1032 assert_tc_cold_blocked(tc);
1033
1034 return intel_de_read(display, reg) & XELPDP_TCSS_POWER_STATE;
1035 }
1036
1037 static bool
xelpdp_tc_phy_wait_for_tcss_power(struct intel_tc_port * tc,bool enabled)1038 xelpdp_tc_phy_wait_for_tcss_power(struct intel_tc_port *tc, bool enabled)
1039 {
1040 struct intel_display *display = to_intel_display(tc->dig_port);
1041
1042 if (wait_for(xelpdp_tc_phy_tcss_power_is_enabled(tc) == enabled, 5)) {
1043 drm_dbg_kms(display->drm,
1044 "Port %s: timeout waiting for TCSS power to get %s\n",
1045 str_enabled_disabled(enabled),
1046 tc->port_name);
1047 return false;
1048 }
1049
1050 return true;
1051 }
1052
1053 /*
1054 * Gfx driver WA 14020908590 for PTL tcss_rxdetect_clkswb_req/ack
1055 * handshake violation when pwwreq= 0->1 during TC7/10 entry
1056 */
xelpdp_tc_power_request_wa(struct intel_display * display,bool enable)1057 static void xelpdp_tc_power_request_wa(struct intel_display *display, bool enable)
1058 {
1059 /* check if mailbox is running busy */
1060 if (intel_de_wait_for_clear(display, TCSS_DISP_MAILBOX_IN_CMD,
1061 TCSS_DISP_MAILBOX_IN_CMD_RUN_BUSY, 10)) {
1062 drm_dbg_kms(display->drm,
1063 "Timeout waiting for TCSS mailbox run/busy bit to clear\n");
1064 return;
1065 }
1066
1067 intel_de_write(display, TCSS_DISP_MAILBOX_IN_DATA, enable ? 1 : 0);
1068 intel_de_write(display, TCSS_DISP_MAILBOX_IN_CMD,
1069 TCSS_DISP_MAILBOX_IN_CMD_RUN_BUSY |
1070 TCSS_DISP_MAILBOX_IN_CMD_DATA(0x1));
1071
1072 /* wait to clear mailbox running busy bit before continuing */
1073 if (intel_de_wait_for_clear(display, TCSS_DISP_MAILBOX_IN_CMD,
1074 TCSS_DISP_MAILBOX_IN_CMD_RUN_BUSY, 10)) {
1075 drm_dbg_kms(display->drm,
1076 "Timeout after writing data to mailbox. Mailbox run/busy bit did not clear\n");
1077 return;
1078 }
1079 }
1080
__xelpdp_tc_phy_enable_tcss_power(struct intel_tc_port * tc,bool enable)1081 static void __xelpdp_tc_phy_enable_tcss_power(struct intel_tc_port *tc, bool enable)
1082 {
1083 struct intel_display *display = to_intel_display(tc->dig_port);
1084 enum port port = tc->dig_port->base.port;
1085 i915_reg_t reg = XELPDP_PORT_BUF_CTL1(display, port);
1086 u32 val;
1087
1088 assert_tc_cold_blocked(tc);
1089
1090 if (DISPLAY_VER(display) == 30)
1091 xelpdp_tc_power_request_wa(display, enable);
1092
1093 val = intel_de_read(display, reg);
1094 if (enable)
1095 val |= XELPDP_TCSS_POWER_REQUEST;
1096 else
1097 val &= ~XELPDP_TCSS_POWER_REQUEST;
1098 intel_de_write(display, reg, val);
1099 }
1100
xelpdp_tc_phy_enable_tcss_power(struct intel_tc_port * tc,bool enable)1101 static bool xelpdp_tc_phy_enable_tcss_power(struct intel_tc_port *tc, bool enable)
1102 {
1103 struct intel_display *display = to_intel_display(tc->dig_port);
1104
1105 __xelpdp_tc_phy_enable_tcss_power(tc, enable);
1106
1107 if (enable && !tc_phy_wait_for_ready(tc))
1108 goto out_disable;
1109
1110 if (!xelpdp_tc_phy_wait_for_tcss_power(tc, enable))
1111 goto out_disable;
1112
1113 return true;
1114
1115 out_disable:
1116 if (drm_WARN_ON(display->drm, tc->mode == TC_PORT_LEGACY))
1117 return false;
1118
1119 if (!enable)
1120 return false;
1121
1122 __xelpdp_tc_phy_enable_tcss_power(tc, false);
1123 xelpdp_tc_phy_wait_for_tcss_power(tc, false);
1124
1125 return false;
1126 }
1127
xelpdp_tc_phy_take_ownership(struct intel_tc_port * tc,bool take)1128 static void xelpdp_tc_phy_take_ownership(struct intel_tc_port *tc, bool take)
1129 {
1130 struct intel_display *display = to_intel_display(tc->dig_port);
1131 enum port port = tc->dig_port->base.port;
1132 i915_reg_t reg = XELPDP_PORT_BUF_CTL1(display, port);
1133 u32 val;
1134
1135 assert_tc_cold_blocked(tc);
1136
1137 val = intel_de_read(display, reg);
1138 if (take)
1139 val |= XELPDP_TC_PHY_OWNERSHIP;
1140 else
1141 val &= ~XELPDP_TC_PHY_OWNERSHIP;
1142 intel_de_write(display, reg, val);
1143 }
1144
xelpdp_tc_phy_is_owned(struct intel_tc_port * tc)1145 static bool xelpdp_tc_phy_is_owned(struct intel_tc_port *tc)
1146 {
1147 struct intel_display *display = to_intel_display(tc->dig_port);
1148 enum port port = tc->dig_port->base.port;
1149 i915_reg_t reg = XELPDP_PORT_BUF_CTL1(display, port);
1150
1151 assert_tc_cold_blocked(tc);
1152
1153 return intel_de_read(display, reg) & XELPDP_TC_PHY_OWNERSHIP;
1154 }
1155
xelpdp_tc_phy_get_hw_state(struct intel_tc_port * tc)1156 static void xelpdp_tc_phy_get_hw_state(struct intel_tc_port *tc)
1157 {
1158 struct intel_display *display = to_intel_display(tc->dig_port);
1159 intel_wakeref_t tc_cold_wref;
1160 enum intel_display_power_domain domain;
1161
1162 tc_cold_wref = __tc_cold_block(tc, &domain);
1163
1164 tc->mode = tc_phy_get_current_mode(tc);
1165 if (tc->mode != TC_PORT_DISCONNECTED) {
1166 tc->lock_wakeref = tc_cold_block(tc);
1167
1168 read_pin_configuration(tc);
1169 /*
1170 * Set a valid lane count value for a DP-alt sink which got
1171 * disconnected. The driver can only disable the output on this PHY.
1172 */
1173 if (tc->max_lane_count == 0)
1174 tc->max_lane_count = 4;
1175 }
1176
1177 drm_WARN_ON(display->drm,
1178 (tc->mode == TC_PORT_DP_ALT || tc->mode == TC_PORT_LEGACY) &&
1179 !xelpdp_tc_phy_tcss_power_is_enabled(tc));
1180
1181 __tc_cold_unblock(tc, domain, tc_cold_wref);
1182 }
1183
xelpdp_tc_phy_connect(struct intel_tc_port * tc,int required_lanes)1184 static bool xelpdp_tc_phy_connect(struct intel_tc_port *tc, int required_lanes)
1185 {
1186 tc->lock_wakeref = tc_cold_block(tc);
1187
1188 if (tc->mode == TC_PORT_TBT_ALT) {
1189 read_pin_configuration(tc);
1190
1191 return true;
1192 }
1193
1194 if (!xelpdp_tc_phy_enable_tcss_power(tc, true))
1195 goto out_unblock_tccold;
1196
1197 xelpdp_tc_phy_take_ownership(tc, true);
1198
1199 read_pin_configuration(tc);
1200
1201 if (!tc_phy_verify_legacy_or_dp_alt_mode(tc, required_lanes))
1202 goto out_release_phy;
1203
1204 return true;
1205
1206 out_release_phy:
1207 xelpdp_tc_phy_take_ownership(tc, false);
1208 xelpdp_tc_phy_wait_for_tcss_power(tc, false);
1209
1210 out_unblock_tccold:
1211 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
1212
1213 return false;
1214 }
1215
xelpdp_tc_phy_disconnect(struct intel_tc_port * tc)1216 static void xelpdp_tc_phy_disconnect(struct intel_tc_port *tc)
1217 {
1218 switch (tc->mode) {
1219 case TC_PORT_LEGACY:
1220 case TC_PORT_DP_ALT:
1221 xelpdp_tc_phy_take_ownership(tc, false);
1222 xelpdp_tc_phy_enable_tcss_power(tc, false);
1223 fallthrough;
1224 case TC_PORT_TBT_ALT:
1225 tc_cold_unblock(tc, fetch_and_zero(&tc->lock_wakeref));
1226 break;
1227 default:
1228 MISSING_CASE(tc->mode);
1229 }
1230 }
1231
1232 static const struct intel_tc_phy_ops xelpdp_tc_phy_ops = {
1233 .cold_off_domain = tgl_tc_phy_cold_off_domain,
1234 .hpd_live_status = xelpdp_tc_phy_hpd_live_status,
1235 .is_ready = adlp_tc_phy_is_ready,
1236 .is_owned = xelpdp_tc_phy_is_owned,
1237 .get_hw_state = xelpdp_tc_phy_get_hw_state,
1238 .connect = xelpdp_tc_phy_connect,
1239 .disconnect = xelpdp_tc_phy_disconnect,
1240 .init = adlp_tc_phy_init,
1241 };
1242
1243 /*
1244 * Generic TC PHY handlers
1245 * -----------------------
1246 */
1247 static enum intel_display_power_domain
tc_phy_cold_off_domain(struct intel_tc_port * tc)1248 tc_phy_cold_off_domain(struct intel_tc_port *tc)
1249 {
1250 return tc->phy_ops->cold_off_domain(tc);
1251 }
1252
tc_phy_hpd_live_status(struct intel_tc_port * tc)1253 static u32 tc_phy_hpd_live_status(struct intel_tc_port *tc)
1254 {
1255 struct intel_display *display = to_intel_display(tc->dig_port);
1256 u32 mask;
1257
1258 mask = tc->phy_ops->hpd_live_status(tc);
1259
1260 /* The sink can be connected only in a single mode. */
1261 drm_WARN_ON_ONCE(display->drm, hweight32(mask) > 1);
1262
1263 return mask;
1264 }
1265
tc_phy_is_ready(struct intel_tc_port * tc)1266 static bool tc_phy_is_ready(struct intel_tc_port *tc)
1267 {
1268 return tc->phy_ops->is_ready(tc);
1269 }
1270
tc_phy_is_owned(struct intel_tc_port * tc)1271 static bool tc_phy_is_owned(struct intel_tc_port *tc)
1272 {
1273 return tc->phy_ops->is_owned(tc);
1274 }
1275
tc_phy_get_hw_state(struct intel_tc_port * tc)1276 static void tc_phy_get_hw_state(struct intel_tc_port *tc)
1277 {
1278 tc->phy_ops->get_hw_state(tc);
1279 }
1280
1281 /* Is the PHY owned by display i.e. is it in legacy or DP-alt mode? */
tc_phy_owned_by_display(struct intel_tc_port * tc,bool phy_is_ready,bool phy_is_owned)1282 static bool tc_phy_owned_by_display(struct intel_tc_port *tc,
1283 bool phy_is_ready, bool phy_is_owned)
1284 {
1285 struct intel_display *display = to_intel_display(tc->dig_port);
1286
1287 if (DISPLAY_VER(display) < 20) {
1288 drm_WARN_ON(display->drm, phy_is_owned && !phy_is_ready);
1289
1290 return phy_is_ready && phy_is_owned;
1291 } else {
1292 return phy_is_owned;
1293 }
1294 }
1295
tc_phy_is_connected(struct intel_tc_port * tc,enum icl_port_dpll_id port_pll_type)1296 static bool tc_phy_is_connected(struct intel_tc_port *tc,
1297 enum icl_port_dpll_id port_pll_type)
1298 {
1299 struct intel_display *display = to_intel_display(tc->dig_port);
1300 bool phy_is_ready = tc_phy_is_ready(tc);
1301 bool phy_is_owned = tc_phy_is_owned(tc);
1302 bool is_connected;
1303
1304 if (tc_phy_owned_by_display(tc, phy_is_ready, phy_is_owned))
1305 is_connected = port_pll_type == ICL_PORT_DPLL_MG_PHY;
1306 else
1307 is_connected = port_pll_type == ICL_PORT_DPLL_DEFAULT;
1308
1309 drm_dbg_kms(display->drm,
1310 "Port %s: PHY connected: %s (ready: %s, owned: %s, pll_type: %s)\n",
1311 tc->port_name,
1312 str_yes_no(is_connected),
1313 str_yes_no(phy_is_ready),
1314 str_yes_no(phy_is_owned),
1315 port_pll_type == ICL_PORT_DPLL_DEFAULT ? "tbt" : "non-tbt");
1316
1317 return is_connected;
1318 }
1319
tc_phy_wait_for_ready(struct intel_tc_port * tc)1320 static bool tc_phy_wait_for_ready(struct intel_tc_port *tc)
1321 {
1322 struct intel_display *display = to_intel_display(tc->dig_port);
1323
1324 if (wait_for(tc_phy_is_ready(tc), 500)) {
1325 drm_err(display->drm, "Port %s: timeout waiting for PHY ready\n",
1326 tc->port_name);
1327
1328 return false;
1329 }
1330
1331 return true;
1332 }
1333
1334 static enum tc_port_mode
hpd_mask_to_tc_mode(u32 live_status_mask)1335 hpd_mask_to_tc_mode(u32 live_status_mask)
1336 {
1337 if (live_status_mask)
1338 return fls(live_status_mask) - 1;
1339
1340 return TC_PORT_DISCONNECTED;
1341 }
1342
1343 static enum tc_port_mode
tc_phy_hpd_live_mode(struct intel_tc_port * tc)1344 tc_phy_hpd_live_mode(struct intel_tc_port *tc)
1345 {
1346 u32 live_status_mask = tc_phy_hpd_live_status(tc);
1347
1348 return hpd_mask_to_tc_mode(live_status_mask);
1349 }
1350
1351 static enum tc_port_mode
get_tc_mode_in_phy_owned_state(struct intel_tc_port * tc,enum tc_port_mode live_mode)1352 get_tc_mode_in_phy_owned_state(struct intel_tc_port *tc,
1353 enum tc_port_mode live_mode)
1354 {
1355 switch (live_mode) {
1356 case TC_PORT_LEGACY:
1357 case TC_PORT_DP_ALT:
1358 return live_mode;
1359 default:
1360 MISSING_CASE(live_mode);
1361 fallthrough;
1362 case TC_PORT_TBT_ALT:
1363 case TC_PORT_DISCONNECTED:
1364 if (tc->legacy_port)
1365 return TC_PORT_LEGACY;
1366 else
1367 return TC_PORT_DP_ALT;
1368 }
1369 }
1370
1371 static enum tc_port_mode
get_tc_mode_in_phy_not_owned_state(struct intel_tc_port * tc,enum tc_port_mode live_mode)1372 get_tc_mode_in_phy_not_owned_state(struct intel_tc_port *tc,
1373 enum tc_port_mode live_mode)
1374 {
1375 switch (live_mode) {
1376 case TC_PORT_LEGACY:
1377 return TC_PORT_DISCONNECTED;
1378 case TC_PORT_DP_ALT:
1379 case TC_PORT_TBT_ALT:
1380 return TC_PORT_TBT_ALT;
1381 default:
1382 MISSING_CASE(live_mode);
1383 fallthrough;
1384 case TC_PORT_DISCONNECTED:
1385 if (tc->legacy_port)
1386 return TC_PORT_DISCONNECTED;
1387 else
1388 return TC_PORT_TBT_ALT;
1389 }
1390 }
1391
1392 static enum tc_port_mode
tc_phy_get_current_mode(struct intel_tc_port * tc)1393 tc_phy_get_current_mode(struct intel_tc_port *tc)
1394 {
1395 struct intel_display *display = to_intel_display(tc->dig_port);
1396 enum tc_port_mode live_mode = tc_phy_hpd_live_mode(tc);
1397 bool phy_is_ready;
1398 bool phy_is_owned;
1399 enum tc_port_mode mode;
1400
1401 /*
1402 * For legacy ports the IOM firmware initializes the PHY during boot-up
1403 * and system resume whether or not a sink is connected. Wait here for
1404 * the initialization to get ready.
1405 */
1406 if (tc->legacy_port)
1407 tc_phy_wait_for_ready(tc);
1408
1409 phy_is_ready = tc_phy_is_ready(tc);
1410 phy_is_owned = tc_phy_is_owned(tc);
1411
1412 if (!tc_phy_owned_by_display(tc, phy_is_ready, phy_is_owned)) {
1413 mode = get_tc_mode_in_phy_not_owned_state(tc, live_mode);
1414 } else {
1415 drm_WARN_ON(display->drm, live_mode == TC_PORT_TBT_ALT);
1416 mode = get_tc_mode_in_phy_owned_state(tc, live_mode);
1417 }
1418
1419 drm_dbg_kms(display->drm,
1420 "Port %s: PHY mode: %s (ready: %s, owned: %s, HPD: %s)\n",
1421 tc->port_name,
1422 tc_port_mode_name(mode),
1423 str_yes_no(phy_is_ready),
1424 str_yes_no(phy_is_owned),
1425 tc_port_mode_name(live_mode));
1426
1427 return mode;
1428 }
1429
default_tc_mode(struct intel_tc_port * tc)1430 static enum tc_port_mode default_tc_mode(struct intel_tc_port *tc)
1431 {
1432 if (tc->legacy_port)
1433 return TC_PORT_LEGACY;
1434
1435 return TC_PORT_TBT_ALT;
1436 }
1437
1438 static enum tc_port_mode
hpd_mask_to_target_mode(struct intel_tc_port * tc,u32 live_status_mask)1439 hpd_mask_to_target_mode(struct intel_tc_port *tc, u32 live_status_mask)
1440 {
1441 enum tc_port_mode mode = hpd_mask_to_tc_mode(live_status_mask);
1442
1443 if (mode != TC_PORT_DISCONNECTED)
1444 return mode;
1445
1446 return default_tc_mode(tc);
1447 }
1448
1449 static enum tc_port_mode
tc_phy_get_target_mode(struct intel_tc_port * tc)1450 tc_phy_get_target_mode(struct intel_tc_port *tc)
1451 {
1452 u32 live_status_mask = tc_phy_hpd_live_status(tc);
1453
1454 return hpd_mask_to_target_mode(tc, live_status_mask);
1455 }
1456
tc_phy_connect(struct intel_tc_port * tc,int required_lanes)1457 static void tc_phy_connect(struct intel_tc_port *tc, int required_lanes)
1458 {
1459 struct intel_display *display = to_intel_display(tc->dig_port);
1460 u32 live_status_mask = tc_phy_hpd_live_status(tc);
1461 bool connected;
1462
1463 tc_port_fixup_legacy_flag(tc, live_status_mask);
1464
1465 tc->mode = hpd_mask_to_target_mode(tc, live_status_mask);
1466
1467 connected = tc->phy_ops->connect(tc, required_lanes);
1468 if (!connected && tc->mode != default_tc_mode(tc)) {
1469 tc->mode = default_tc_mode(tc);
1470 connected = tc->phy_ops->connect(tc, required_lanes);
1471 }
1472
1473 drm_WARN_ON(display->drm, !connected);
1474 }
1475
tc_phy_disconnect(struct intel_tc_port * tc)1476 static void tc_phy_disconnect(struct intel_tc_port *tc)
1477 {
1478 if (tc->mode != TC_PORT_DISCONNECTED) {
1479 tc->phy_ops->disconnect(tc);
1480 tc->mode = TC_PORT_DISCONNECTED;
1481 }
1482 }
1483
tc_phy_init(struct intel_tc_port * tc)1484 static void tc_phy_init(struct intel_tc_port *tc)
1485 {
1486 mutex_lock(&tc->lock);
1487 tc->phy_ops->init(tc);
1488 mutex_unlock(&tc->lock);
1489 }
1490
intel_tc_port_reset_mode(struct intel_tc_port * tc,int required_lanes,bool force_disconnect)1491 static void intel_tc_port_reset_mode(struct intel_tc_port *tc,
1492 int required_lanes, bool force_disconnect)
1493 {
1494 struct intel_display *display = to_intel_display(tc->dig_port);
1495 struct intel_digital_port *dig_port = tc->dig_port;
1496 enum tc_port_mode old_tc_mode = tc->mode;
1497
1498 intel_display_power_flush_work(display);
1499 if (!intel_tc_cold_requires_aux_pw(dig_port)) {
1500 enum intel_display_power_domain aux_domain;
1501
1502 aux_domain = intel_aux_power_domain(dig_port);
1503 if (intel_display_power_is_enabled(display, aux_domain))
1504 drm_dbg_kms(display->drm, "Port %s: AUX unexpectedly powered\n",
1505 tc->port_name);
1506 }
1507
1508 tc_phy_disconnect(tc);
1509 if (!force_disconnect)
1510 tc_phy_connect(tc, required_lanes);
1511
1512 drm_dbg_kms(display->drm, "Port %s: TC port mode reset (%s -> %s)\n",
1513 tc->port_name,
1514 tc_port_mode_name(old_tc_mode),
1515 tc_port_mode_name(tc->mode));
1516 }
1517
intel_tc_port_needs_reset(struct intel_tc_port * tc)1518 static bool intel_tc_port_needs_reset(struct intel_tc_port *tc)
1519 {
1520 return tc_phy_get_target_mode(tc) != tc->mode;
1521 }
1522
intel_tc_port_update_mode(struct intel_tc_port * tc,int required_lanes,bool force_disconnect)1523 static void intel_tc_port_update_mode(struct intel_tc_port *tc,
1524 int required_lanes, bool force_disconnect)
1525 {
1526 if (force_disconnect ||
1527 intel_tc_port_needs_reset(tc))
1528 intel_tc_port_reset_mode(tc, required_lanes, force_disconnect);
1529 }
1530
__intel_tc_port_get_link(struct intel_tc_port * tc)1531 static void __intel_tc_port_get_link(struct intel_tc_port *tc)
1532 {
1533 tc->link_refcount++;
1534 }
1535
__intel_tc_port_put_link(struct intel_tc_port * tc)1536 static void __intel_tc_port_put_link(struct intel_tc_port *tc)
1537 {
1538 tc->link_refcount--;
1539 }
1540
tc_port_is_enabled(struct intel_tc_port * tc)1541 static bool tc_port_is_enabled(struct intel_tc_port *tc)
1542 {
1543 struct intel_display *display = to_intel_display(tc->dig_port);
1544 struct intel_digital_port *dig_port = tc->dig_port;
1545
1546 assert_tc_port_power_enabled(tc);
1547
1548 return intel_de_read(display, DDI_BUF_CTL(dig_port->base.port)) &
1549 DDI_BUF_CTL_ENABLE;
1550 }
1551
1552 /**
1553 * intel_tc_port_init_mode: Read out HW state and init the given port's TypeC mode
1554 * @dig_port: digital port
1555 *
1556 * Read out the HW state and initialize the TypeC mode of @dig_port. The mode
1557 * will be locked until intel_tc_port_sanitize_mode() is called.
1558 */
intel_tc_port_init_mode(struct intel_digital_port * dig_port)1559 void intel_tc_port_init_mode(struct intel_digital_port *dig_port)
1560 {
1561 struct intel_display *display = to_intel_display(dig_port);
1562 struct intel_tc_port *tc = to_tc_port(dig_port);
1563 bool update_mode = false;
1564
1565 mutex_lock(&tc->lock);
1566
1567 drm_WARN_ON(display->drm, tc->mode != TC_PORT_DISCONNECTED);
1568 drm_WARN_ON(display->drm, tc->lock_wakeref);
1569 drm_WARN_ON(display->drm, tc->link_refcount);
1570
1571 tc_phy_get_hw_state(tc);
1572 /*
1573 * Save the initial mode for the state check in
1574 * intel_tc_port_sanitize_mode().
1575 */
1576 tc->init_mode = tc->mode;
1577
1578 /*
1579 * The PHY needs to be connected for AUX to work during HW readout and
1580 * MST topology resume, but the PHY mode can only be changed if the
1581 * port is disabled.
1582 *
1583 * An exception is the case where BIOS leaves the PHY incorrectly
1584 * disconnected on an enabled legacy port. Work around that by
1585 * connecting the PHY even though the port is enabled. This doesn't
1586 * cause a problem as the PHY ownership state is ignored by the
1587 * IOM/TCSS firmware (only display can own the PHY in that case).
1588 */
1589 if (!tc_port_is_enabled(tc)) {
1590 update_mode = true;
1591 } else if (tc->mode == TC_PORT_DISCONNECTED) {
1592 drm_WARN_ON(display->drm, !tc->legacy_port);
1593 drm_err(display->drm,
1594 "Port %s: PHY disconnected on enabled port, connecting it\n",
1595 tc->port_name);
1596 update_mode = true;
1597 }
1598
1599 if (update_mode)
1600 intel_tc_port_update_mode(tc, 1, false);
1601
1602 /* Prevent changing tc->mode until intel_tc_port_sanitize_mode() is called. */
1603 __intel_tc_port_get_link(tc);
1604
1605 mutex_unlock(&tc->lock);
1606 }
1607
tc_port_has_active_streams(struct intel_tc_port * tc,const struct intel_crtc_state * crtc_state)1608 static bool tc_port_has_active_streams(struct intel_tc_port *tc,
1609 const struct intel_crtc_state *crtc_state)
1610 {
1611 struct intel_display *display = to_intel_display(tc->dig_port);
1612 struct intel_digital_port *dig_port = tc->dig_port;
1613 enum icl_port_dpll_id pll_type = ICL_PORT_DPLL_DEFAULT;
1614 int active_streams = 0;
1615
1616 if (dig_port->dp.is_mst) {
1617 /* TODO: get the PLL type for MST, once HW readout is done for it. */
1618 active_streams = intel_dp_mst_active_streams(&dig_port->dp);
1619 } else if (crtc_state && crtc_state->hw.active) {
1620 pll_type = intel_ddi_port_pll_type(&dig_port->base, crtc_state);
1621 active_streams = 1;
1622 }
1623
1624 if (active_streams && !tc_phy_is_connected(tc, pll_type))
1625 drm_err(display->drm,
1626 "Port %s: PHY disconnected with %d active stream(s)\n",
1627 tc->port_name, active_streams);
1628
1629 return active_streams;
1630 }
1631
1632 /**
1633 * intel_tc_port_sanitize_mode: Sanitize the given port's TypeC mode
1634 * @dig_port: digital port
1635 * @crtc_state: atomic state of CRTC connected to @dig_port
1636 *
1637 * Sanitize @dig_port's TypeC mode wrt. the encoder's state right after driver
1638 * loading and system resume:
1639 * If the encoder is enabled keep the TypeC mode/PHY connected state locked until
1640 * the encoder is disabled.
1641 * If the encoder is disabled make sure the PHY is disconnected.
1642 * @crtc_state is valid if @dig_port is enabled, NULL otherwise.
1643 */
intel_tc_port_sanitize_mode(struct intel_digital_port * dig_port,const struct intel_crtc_state * crtc_state)1644 void intel_tc_port_sanitize_mode(struct intel_digital_port *dig_port,
1645 const struct intel_crtc_state *crtc_state)
1646 {
1647 struct intel_display *display = to_intel_display(dig_port);
1648 struct intel_tc_port *tc = to_tc_port(dig_port);
1649
1650 mutex_lock(&tc->lock);
1651
1652 drm_WARN_ON(display->drm, tc->link_refcount != 1);
1653 if (!tc_port_has_active_streams(tc, crtc_state)) {
1654 /*
1655 * TBT-alt is the default mode in any case the PHY ownership is not
1656 * held (regardless of the sink's connected live state), so
1657 * we'll just switch to disconnected mode from it here without
1658 * a note.
1659 */
1660 if (tc->init_mode != TC_PORT_TBT_ALT &&
1661 tc->init_mode != TC_PORT_DISCONNECTED)
1662 drm_dbg_kms(display->drm,
1663 "Port %s: PHY left in %s mode on disabled port, disconnecting it\n",
1664 tc->port_name,
1665 tc_port_mode_name(tc->init_mode));
1666 tc_phy_disconnect(tc);
1667 __intel_tc_port_put_link(tc);
1668 }
1669
1670 drm_dbg_kms(display->drm, "Port %s: sanitize mode (%s)\n",
1671 tc->port_name,
1672 tc_port_mode_name(tc->mode));
1673
1674 mutex_unlock(&tc->lock);
1675 }
1676
1677 /*
1678 * The type-C ports are different because even when they are connected, they may
1679 * not be available/usable by the graphics driver: see the comment on
1680 * icl_tc_phy_connect(). So in our driver instead of adding the additional
1681 * concept of "usable" and make everything check for "connected and usable" we
1682 * define a port as "connected" when it is not only connected, but also when it
1683 * is usable by the rest of the driver. That maintains the old assumption that
1684 * connected ports are usable, and avoids exposing to the users objects they
1685 * can't really use.
1686 */
intel_tc_port_connected(struct intel_encoder * encoder)1687 bool intel_tc_port_connected(struct intel_encoder *encoder)
1688 {
1689 struct intel_display *display = to_intel_display(encoder);
1690 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1691 struct intel_tc_port *tc = to_tc_port(dig_port);
1692 u32 mask = ~0;
1693
1694 drm_WARN_ON(display->drm, !intel_tc_port_ref_held(dig_port));
1695
1696 if (tc->mode != TC_PORT_DISCONNECTED)
1697 mask = BIT(tc->mode);
1698
1699 return tc_phy_hpd_live_status(tc) & mask;
1700 }
1701
__intel_tc_port_link_needs_reset(struct intel_tc_port * tc)1702 static bool __intel_tc_port_link_needs_reset(struct intel_tc_port *tc)
1703 {
1704 bool ret;
1705
1706 mutex_lock(&tc->lock);
1707
1708 ret = tc->link_refcount &&
1709 tc->mode == TC_PORT_DP_ALT &&
1710 intel_tc_port_needs_reset(tc);
1711
1712 mutex_unlock(&tc->lock);
1713
1714 return ret;
1715 }
1716
intel_tc_port_link_needs_reset(struct intel_digital_port * dig_port)1717 bool intel_tc_port_link_needs_reset(struct intel_digital_port *dig_port)
1718 {
1719 if (!intel_encoder_is_tc(&dig_port->base))
1720 return false;
1721
1722 return __intel_tc_port_link_needs_reset(to_tc_port(dig_port));
1723 }
1724
reset_link_commit(struct intel_tc_port * tc,struct intel_atomic_state * state,struct drm_modeset_acquire_ctx * ctx)1725 static int reset_link_commit(struct intel_tc_port *tc,
1726 struct intel_atomic_state *state,
1727 struct drm_modeset_acquire_ctx *ctx)
1728 {
1729 struct intel_display *display = to_intel_display(tc->dig_port);
1730 struct intel_digital_port *dig_port = tc->dig_port;
1731 struct intel_dp *intel_dp = enc_to_intel_dp(&dig_port->base);
1732 struct intel_crtc *crtc;
1733 u8 pipe_mask;
1734 int ret;
1735
1736 ret = drm_modeset_lock(&display->drm->mode_config.connection_mutex, ctx);
1737 if (ret)
1738 return ret;
1739
1740 ret = intel_dp_get_active_pipes(intel_dp, ctx, &pipe_mask);
1741 if (ret)
1742 return ret;
1743
1744 if (!pipe_mask)
1745 return 0;
1746
1747 for_each_intel_crtc_in_pipe_mask(display->drm, crtc, pipe_mask) {
1748 struct intel_crtc_state *crtc_state;
1749
1750 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
1751 if (IS_ERR(crtc_state))
1752 return PTR_ERR(crtc_state);
1753
1754 crtc_state->uapi.connectors_changed = true;
1755 }
1756
1757 if (!__intel_tc_port_link_needs_reset(tc))
1758 return 0;
1759
1760 return drm_atomic_commit(&state->base);
1761 }
1762
reset_link(struct intel_tc_port * tc)1763 static int reset_link(struct intel_tc_port *tc)
1764 {
1765 struct intel_display *display = to_intel_display(tc->dig_port);
1766 struct drm_modeset_acquire_ctx ctx;
1767 struct drm_atomic_state *_state;
1768 struct intel_atomic_state *state;
1769 int ret;
1770
1771 _state = drm_atomic_state_alloc(display->drm);
1772 if (!_state)
1773 return -ENOMEM;
1774
1775 state = to_intel_atomic_state(_state);
1776 state->internal = true;
1777
1778 intel_modeset_lock_ctx_retry(&ctx, state, 0, ret)
1779 ret = reset_link_commit(tc, state, &ctx);
1780
1781 drm_atomic_state_put(&state->base);
1782
1783 return ret;
1784 }
1785
intel_tc_port_link_reset_work(struct work_struct * work)1786 static void intel_tc_port_link_reset_work(struct work_struct *work)
1787 {
1788 struct intel_tc_port *tc =
1789 container_of(work, struct intel_tc_port, link_reset_work.work);
1790 struct intel_display *display = to_intel_display(tc->dig_port);
1791 int ret;
1792
1793 if (!__intel_tc_port_link_needs_reset(tc))
1794 return;
1795
1796 mutex_lock(&display->drm->mode_config.mutex);
1797
1798 drm_dbg_kms(display->drm,
1799 "Port %s: TypeC DP-alt sink disconnected, resetting link\n",
1800 tc->port_name);
1801 ret = reset_link(tc);
1802 drm_WARN_ON(display->drm, ret);
1803
1804 mutex_unlock(&display->drm->mode_config.mutex);
1805 }
1806
intel_tc_port_link_reset(struct intel_digital_port * dig_port)1807 bool intel_tc_port_link_reset(struct intel_digital_port *dig_port)
1808 {
1809 if (!intel_tc_port_link_needs_reset(dig_port))
1810 return false;
1811
1812 queue_delayed_work(system_unbound_wq,
1813 &to_tc_port(dig_port)->link_reset_work,
1814 msecs_to_jiffies(2000));
1815
1816 return true;
1817 }
1818
intel_tc_port_link_cancel_reset_work(struct intel_digital_port * dig_port)1819 void intel_tc_port_link_cancel_reset_work(struct intel_digital_port *dig_port)
1820 {
1821 struct intel_tc_port *tc = to_tc_port(dig_port);
1822
1823 if (!intel_encoder_is_tc(&dig_port->base))
1824 return;
1825
1826 cancel_delayed_work(&tc->link_reset_work);
1827 }
1828
__intel_tc_port_lock(struct intel_tc_port * tc,int required_lanes)1829 static void __intel_tc_port_lock(struct intel_tc_port *tc,
1830 int required_lanes)
1831 {
1832 struct intel_display *display = to_intel_display(tc->dig_port);
1833
1834 mutex_lock(&tc->lock);
1835
1836 cancel_delayed_work(&tc->disconnect_phy_work);
1837
1838 if (!tc->link_refcount)
1839 intel_tc_port_update_mode(tc, required_lanes,
1840 false);
1841
1842 drm_WARN_ON(display->drm, tc->mode == TC_PORT_DISCONNECTED);
1843 drm_WARN_ON(display->drm, tc->mode != TC_PORT_TBT_ALT && !tc_phy_is_owned(tc));
1844 }
1845
intel_tc_port_lock(struct intel_digital_port * dig_port)1846 void intel_tc_port_lock(struct intel_digital_port *dig_port)
1847 {
1848 __intel_tc_port_lock(to_tc_port(dig_port), 1);
1849 }
1850
1851 /*
1852 * Disconnect the given digital port from its TypeC PHY (handing back the
1853 * control of the PHY to the TypeC subsystem). This will happen in a delayed
1854 * manner after each aux transactions and modeset disables.
1855 */
intel_tc_port_disconnect_phy_work(struct work_struct * work)1856 static void intel_tc_port_disconnect_phy_work(struct work_struct *work)
1857 {
1858 struct intel_tc_port *tc =
1859 container_of(work, struct intel_tc_port, disconnect_phy_work.work);
1860
1861 mutex_lock(&tc->lock);
1862
1863 if (!tc->link_refcount)
1864 intel_tc_port_update_mode(tc, 1, true);
1865
1866 mutex_unlock(&tc->lock);
1867 }
1868
1869 /**
1870 * intel_tc_port_flush_work: flush the work disconnecting the PHY
1871 * @dig_port: digital port
1872 *
1873 * Flush the delayed work disconnecting an idle PHY.
1874 */
intel_tc_port_flush_work(struct intel_digital_port * dig_port)1875 static void intel_tc_port_flush_work(struct intel_digital_port *dig_port)
1876 {
1877 flush_delayed_work(&to_tc_port(dig_port)->disconnect_phy_work);
1878 }
1879
intel_tc_port_suspend(struct intel_digital_port * dig_port)1880 void intel_tc_port_suspend(struct intel_digital_port *dig_port)
1881 {
1882 struct intel_tc_port *tc = to_tc_port(dig_port);
1883
1884 cancel_delayed_work_sync(&tc->link_reset_work);
1885 intel_tc_port_flush_work(dig_port);
1886 }
1887
intel_tc_port_unlock(struct intel_digital_port * dig_port)1888 void intel_tc_port_unlock(struct intel_digital_port *dig_port)
1889 {
1890 struct intel_tc_port *tc = to_tc_port(dig_port);
1891
1892 if (!tc->link_refcount && tc->mode != TC_PORT_DISCONNECTED)
1893 queue_delayed_work(system_unbound_wq, &tc->disconnect_phy_work,
1894 msecs_to_jiffies(1000));
1895
1896 mutex_unlock(&tc->lock);
1897 }
1898
intel_tc_port_ref_held(struct intel_digital_port * dig_port)1899 bool intel_tc_port_ref_held(struct intel_digital_port *dig_port)
1900 {
1901 struct intel_tc_port *tc = to_tc_port(dig_port);
1902
1903 return mutex_is_locked(&tc->lock) ||
1904 tc->link_refcount;
1905 }
1906
intel_tc_port_get_link(struct intel_digital_port * dig_port,int required_lanes)1907 void intel_tc_port_get_link(struct intel_digital_port *dig_port,
1908 int required_lanes)
1909 {
1910 struct intel_tc_port *tc = to_tc_port(dig_port);
1911
1912 __intel_tc_port_lock(tc, required_lanes);
1913 __intel_tc_port_get_link(tc);
1914 intel_tc_port_unlock(dig_port);
1915 }
1916
intel_tc_port_put_link(struct intel_digital_port * dig_port)1917 void intel_tc_port_put_link(struct intel_digital_port *dig_port)
1918 {
1919 struct intel_tc_port *tc = to_tc_port(dig_port);
1920
1921 intel_tc_port_lock(dig_port);
1922 __intel_tc_port_put_link(tc);
1923 intel_tc_port_unlock(dig_port);
1924
1925 /*
1926 * The firmware will not update the HPD status of other TypeC ports
1927 * that are active in DP-alt mode with their sink disconnected, until
1928 * this port is disabled and its PHY gets disconnected. Make sure this
1929 * happens in a timely manner by disconnecting the PHY synchronously.
1930 */
1931 intel_tc_port_flush_work(dig_port);
1932 }
1933
intel_tc_port_init(struct intel_digital_port * dig_port,bool is_legacy)1934 int intel_tc_port_init(struct intel_digital_port *dig_port, bool is_legacy)
1935 {
1936 struct intel_display *display = to_intel_display(dig_port);
1937 struct intel_tc_port *tc;
1938 enum port port = dig_port->base.port;
1939 enum tc_port tc_port = intel_encoder_to_tc(&dig_port->base);
1940
1941 if (drm_WARN_ON(display->drm, tc_port == TC_PORT_NONE))
1942 return -EINVAL;
1943
1944 tc = kzalloc(sizeof(*tc), GFP_KERNEL);
1945 if (!tc)
1946 return -ENOMEM;
1947
1948 dig_port->tc = tc;
1949 tc->dig_port = dig_port;
1950
1951 if (DISPLAY_VER(display) >= 14)
1952 tc->phy_ops = &xelpdp_tc_phy_ops;
1953 else if (DISPLAY_VER(display) >= 13)
1954 tc->phy_ops = &adlp_tc_phy_ops;
1955 else if (DISPLAY_VER(display) >= 12)
1956 tc->phy_ops = &tgl_tc_phy_ops;
1957 else
1958 tc->phy_ops = &icl_tc_phy_ops;
1959
1960 tc->port_name = kasprintf(GFP_KERNEL, "%c/TC#%d", port_name(port),
1961 tc_port + 1);
1962 if (!tc->port_name) {
1963 kfree(tc);
1964 return -ENOMEM;
1965 }
1966
1967 mutex_init(&tc->lock);
1968 /* TODO: Combine the two works */
1969 INIT_DELAYED_WORK(&tc->disconnect_phy_work, intel_tc_port_disconnect_phy_work);
1970 INIT_DELAYED_WORK(&tc->link_reset_work, intel_tc_port_link_reset_work);
1971 tc->legacy_port = is_legacy;
1972 tc->mode = TC_PORT_DISCONNECTED;
1973 tc->link_refcount = 0;
1974
1975 tc_phy_init(tc);
1976
1977 intel_tc_port_init_mode(dig_port);
1978
1979 return 0;
1980 }
1981
intel_tc_port_cleanup(struct intel_digital_port * dig_port)1982 void intel_tc_port_cleanup(struct intel_digital_port *dig_port)
1983 {
1984 intel_tc_port_suspend(dig_port);
1985
1986 kfree(dig_port->tc->port_name);
1987 kfree(dig_port->tc);
1988 dig_port->tc = NULL;
1989 }
1990