xref: /linux/drivers/gpu/drm/i915/display/intel_dp_link_training.c (revision 5c9deba5578db5a3ea05be8a3e4a59ecb08ee76f)
1 /*
2  * Copyright © 2008-2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <linux/debugfs.h>
25 #include <linux/iopoll.h>
26 
27 #include <drm/display/drm_dp_helper.h>
28 #include <drm/drm_print.h>
29 
30 #include "intel_display.h"
31 #include "intel_display_core.h"
32 #include "intel_display_jiffies.h"
33 #include "intel_display_types.h"
34 #include "intel_display_utils.h"
35 #include "intel_dp.h"
36 #include "intel_dp_link_caps.h"
37 #include "intel_dp_link_training.h"
38 #include "intel_dp_mst.h"
39 #include "intel_encoder.h"
40 #include "intel_hdmi.h"
41 #include "intel_hotplug.h"
42 #include "intel_modeset_lock.h"
43 #include "intel_panel.h"
44 #include "intel_psr.h"
45 
46 /**
47  * DOC: DisplayPort link training
48  *
49  * This documents the Intel DisplayPort link training implementation and
50  * its internal interfaces, with a current focus on link recovery.
51  *
52  * Documentation of the full link training procedure is not yet included.
53  *
54  * The Intel DP link recovery logic governs how the driver reacts to
55  * link training failures and to links that degrade asynchronously
56  * after a previously successful training. Recovery is first attempted
57  * via automatic retraining (``autoretrain``) and, when that is no
58  * longer possible, by selecting fallback link configurations and
59  * notifying userspace to recover the link via a modeset.
60  *
61  * Recovery sequence and userspace notification
62  * --------------------------------------------
63  *
64  * After the first link training failure following initialization or a
65  * previously successful training, recovery is first attempted by the
66  * driver via automatic retraining, without userspace involvement.
67  * During this phase, a given link configuration is attempted twice
68  * before being abandoned: after the initial link training failure, an
69  * automatic retraining modeset is performed with the same link
70  * parameters, constituting the second attempt.
71  *
72  * Once automatic retraining is no longer possible, recovery is delegated
73  * to userspace, which must select a new modeset configuration, as the
74  * kernel must not do so. From this point onwards, each link configuration
75  * may be attempted only once as userspace iterates through alternative
76  * configurations. A successful link training restores the automatic
77  * retraining model for subsequent failures.
78  *
79  * The failure of the last automatic retraining attempt is reported to
80  * userspace, and from that point onward the driver notifies userspace of
81  * each subsequent failure. This allows userspace to both initiate
82  * recovery via modesets and observe the outcome of those recovery
83  * attempts, even when no further fallback configurations remain.
84  *
85  * Link training failures are always reported to userspace, even when they
86  * result from a kernel-internal modeset. Such modesets only re-apply the
87  * existing userspace-provided state and must not modify it. A failure
88  * triggered by such a modeset is therefore treated the same as a link
89  * degradation after a previously successful training, and recovery is
90  * handled by userspace in place of the kernel caller.
91  *
92  * Contexts
93  * --------
94  *
95  * The following execution contexts (A/B/C) describe how the different
96  * recovery states are reached but are not themselves implementation
97  * states. The actual state machine is defined by &enum
98  * intel_dp_link_training_recovery_state.
99  *
100  * A. Modeset context:
101  *
102  *   Triggered by:
103  *     - link training during a modeset, or
104  *     - via the "i915_dp_force_link_training_failure" debugfs entry,
105  *       forcing this path by emulating a link training failure.
106  *
107  *   Transitions:
108  *     - A1 Link training succeeds.
109  *
110  *       A link check work to recover any degraded link is scheduled
111  *       (and handled if needed in context B).
112  *
113  *       State -> %INTEL_DP_LINK_RECOVERY_IDLE.
114  *
115  *     - A2 First link training fails after initialization or a previously
116  *       successful link training.
117  *
118  *       An automatic retraining work is scheduled (and handled in
119  *       context B) with the same link parameters with which the link
120  *       training failed.
121  *
122  *       State -> %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING.
123  *
124  *     - A3 Link training fails again after A2 or A3.
125  *
126  *       Through fallback selection, the driver attempts to restrict the
127  *       allowed link configurations for subsequent modesets. This may
128  *       be done either by lowering global limits (rate/lane caps), or by
129  *       disabling only the currently failing configuration while leaving
130  *       all other configurations allowed, even if they use higher rate or
131  *       lane count.
132  *
133  *       (The current implementation may still apply parameter capping as
134  *       a coarse fallback selection mechanism. This is transitional and is
135  *       expected to be replaced by a scheme that disables only the failing
136  *       configuration, rather than removing configurations that have not
137  *       been observed to fail and may still train successfully.)
138  *
139  *       This case may repeat in a loop:
140  *           %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED ->
141  *           %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED
142  *
143  *       via repeated A3a -> A3a transitions until the configuration fallback
144  *       space is exhausted, reaching the A3b terminal case.
145  *
146  *       - A3a Fallback selection succeeds.
147  *
148  *         Userspace is notified to retry the modeset.
149  *
150  *         State -> %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED.
151  *
152  *       - A3b Fallback selection fails.
153  *
154  *         Userspace is notified of the failure and may continue recovery
155  *         by retrying the modeset with the remaining allowed link
156  *         configuration.
157  *
158  *         State -> %INTEL_DP_LINK_RECOVERY_NO_FALLBACK.
159  *
160  * B. Automatic retraining context:
161  *
162  *   Triggered by:
163  *     - after a successful link training in context A1 followed by
164  *       asynchronous link degradation, or
165  *     - after the first failed link training attempt in context A2, or
166  *     - via the "i915_dp_force_link_retrain" debugfs entry, which may
167  *       bypass normal gating and force this path.
168  *
169  *   Transitions:
170  *     - B1 ``Autoretrain`` modeset check and link training succeeds.
171  *
172  *       The case is handled as in A1, scheduling a link check work to
173  *       recover any degraded link.
174  *
175  *       State -> %INTEL_DP_LINK_RECOVERY_IDLE.
176  *
177  *     - B2 ``Autoretrain`` modeset check succeeds but link training fails.
178  *
179  *       - B2a Previously the link degraded asynchronously (current state
180  *         is %INTEL_DP_LINK_RECOVERY_IDLE).
181  *
182  *         This corresponds to a first failure in a new failure
183  *         sequence and is handled as in A2: an automatic retraining
184  *         attempt is scheduled with the same link parameters.
185  *
186  *         State -> %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING.
187  *
188  *       - B2b Previously a link training failed (current state is
189  *         %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING).
190  *
191  *         In non-regular (debug-forced) scenarios this may also be
192  *         reached from
193  *         %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED or
194  *         %INTEL_DP_LINK_RECOVERY_NO_FALLBACK, effectively behaving
195  *         like a userspace-driven recovery attempt.
196  *
197  *         The failure is handled as in A3, performing a fallback selection:
198  *
199  *         State -> %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED (via A3a).
200  *
201  *         or
202  *
203  *         State -> %INTEL_DP_LINK_RECOVERY_NO_FALLBACK (via A3b).
204  *
205  *     - B3 ``Autoretrain`` modeset check fails (and hence the link training
206  *       cannot be started).
207  *
208  *       The modeset check may fail, for example, due to external conditions
209  *       such as changed shared link bandwidth, which can make previously
210  *       valid modeset parameters no longer acceptable.
211  *
212  *       In this case, automatic retraining is disabled without selecting
213  *       a fallback configuration. The driver hands recovery over to
214  *       userspace without modifying the allowed configuration set, so a
215  *       subsequent userspace modeset will retry with the current link
216  *       configuration. Userspace is in a better position to select new
217  *       modeset parameters (e.g. video mode or enabled outputs) that
218  *       satisfy the updated constraints, as the driver is only allowed
219  *       to retry the modeset with the existing userspace-provided modeset
220  *       configuration.
221  *
222  *       This policy preserves the normal retry model, where a given link
223  *       configuration is attempted twice in the automatic retraining
224  *       flow before being abandoned: after a first link training failure,
225  *       an automatic retraining modeset is performed with the same link
226  *       parameters, and if its atomic check passes, the link training
227  *       itself may either succeed or fail, constituting the second
228  *       attempt. In this case, however, the retry modeset's atomic check
229  *       failed, so no second link training attempt with those parameters
230  *       was performed, and selecting a fallback would cause that
231  *       configuration to be tried only once rather than twice.
232  *
233  *       The userspace-driven link recovery continues with subsequent
234  *       userspace modesets handled in A3.
235  *
236  *       State -> %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED.
237  *
238  * C. State reset context:
239  *
240  *   Triggered by:
241  *     - sink capability changes, or
242  *     - sink disconnect/reconnect, or
243  *     - system suspend/resume or power transitions where HPD
244  *       handling may have been suppressed, or
245  *     - successful link training.
246  *
247  *   Transitions:
248  *     - The recovery state is reset from any of the recovery states
249  *
250  *       State -> %INTEL_DP_LINK_RECOVERY_IDLE.
251  *
252  *       After reset, the driver may re-check link status and schedule
253  *       retraining if the link is found to remain degraded.
254  *
255  * State transition summary
256  * ------------------------
257  *
258  * - From %INTEL_DP_LINK_RECOVERY_IDLE
259  *
260  *   - To %INTEL_DP_LINK_RECOVERY_IDLE
261  *
262  *     - | In context: B1
263  *       | Action: no action
264  *
265  *   - To %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING
266  *
267  *     - | In contexts: A2, B2a
268  *       | Action: queue ``autoretrain`` work
269  *
270  *   - To %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED
271  *
272  *     - | In context: B3
273  *       | Action: notify userspace
274  *
275  * - From %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING
276  *
277  *   - To %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED
278  *
279  *     - | In contexts: A3a, B2b
280  *       | Action: select fallback configurations, notify userspace
281  *
282  *     - | In context: B3
283  *       | Action: notify userspace
284  *
285  *   - To %INTEL_DP_LINK_RECOVERY_NO_FALLBACK
286  *
287  *     - | In contexts: A3b, B2b
288  *       | Action: notify userspace
289  *
290  * - From %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED
291  *
292  *   - To %INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED
293  *
294  *     - | In contexts: A3a, B2b
295  *       | Action: select fallback configurations, notify userspace
296  *
297  *   - To %INTEL_DP_LINK_RECOVERY_NO_FALLBACK
298  *
299  *     - | In contexts: A3b, B2b
300  *       | Action: notify userspace
301  *
302  * - From %INTEL_DP_LINK_RECOVERY_NO_FALLBACK
303  *
304  *   - To %INTEL_DP_LINK_RECOVERY_NO_FALLBACK
305  *
306  *     - | In contexts: A3b
307  *       | Action: notify userspace
308  *
309  * - From any state
310  *
311  *   - To %INTEL_DP_LINK_RECOVERY_IDLE
312  *
313  *     - | In contexts: C
314  *       | Action: no action
315  *
316  * Recovery flows
317  * --------------
318  *
319  * Userspace modeset link recovery::
320  *
321  *                       [IDLE]
322  *                          |
323  *                          | userspace modeset link training fails
324  *                          | (autoretrain link recovery work scheduled)
325  *                          v
326  *               [AUTORETRAIN_PENDING]-- autoretrain link recovery succeeds -> [IDLE]
327  *                          |
328  *                          | autoretrain link recovery modeset check or link training fails
329  *                          |
330  *                       +--o--+
331  *  modeset check fails  |     | link training fails
332  *  (userspace notified) |     |
333  *                       |     o-------- no fallback (userspace notified) ---> [NO_FALLBACK]
334  *                       |     |
335  *   +-------------+     |     | fallback selected (userspace notified)
336  *   |             |     |     |
337  *   |             v     v     v
338  *   |        [AUTORETRAIN_DISABLED]--- userspace link recovery succeeds ----> [IDLE]
339  *   |                   |
340  *   |                   | userspace link recovery fails
341  *   |                   |
342  *   +-------------------o------------- no fallback (userspace notified) ----> [NO_FALLBACK]
343  *   fallback selected
344  *   (userspace notified)
345  *
346  * Asynchronous link degradation recovery::
347  *
348  *                       [IDLE]
349  *                          |
350  *                          | link degrades
351  *                          | (autoretrain link recovery performed)
352  *                          |
353  *                          o--- autoretrain link recovery succeeds ---> [IDLE]
354  *                          |
355  *                          | autoretrain link recovery modeset check or link training fails
356  *                          |
357  *                       +--o--+
358  *  modeset check fails  |     | link training fails
359  *  (userspace notified) |     | (autoretrain work scheduled)
360  *                       v     v
361  *  [AUTORETRAIN_DISABLED*]   [AUTORETRAIN_PENDING*]
362  *
363  * ``*`` marks states where the sequence continues from the corresponding state
364  * in the Userspace modeset link recovery flow above.
365  *
366  */
367 
368 #define LT_MSG_PREFIX			"[CONNECTOR:%d:%s][ENCODER:%d:%s][%s] "
369 #define LT_MSG_ARGS(_intel_dp, _dp_phy)	(_intel_dp)->attached_connector->base.base.id, \
370 					(_intel_dp)->attached_connector->base.name, \
371 					dp_to_dig_port(_intel_dp)->base.base.base.id, \
372 					dp_to_dig_port(_intel_dp)->base.base.name, \
373 					drm_dp_phy_name(_dp_phy)
374 
375 #define lt_dbg(_intel_dp, _dp_phy, _format, ...) \
376 	drm_dbg_kms(to_intel_display(_intel_dp)->drm, \
377 		    LT_MSG_PREFIX _format, \
378 		    LT_MSG_ARGS(_intel_dp, _dp_phy), ## __VA_ARGS__)
379 
380 #define lt_err(_intel_dp, _dp_phy, _format, ...) do { \
381 	if (intel_digital_port_connected(&dp_to_dig_port(_intel_dp)->base)) \
382 		drm_err(to_intel_display(_intel_dp)->drm, \
383 			LT_MSG_PREFIX _format, \
384 			LT_MSG_ARGS(_intel_dp, _dp_phy), ## __VA_ARGS__); \
385 	else \
386 		lt_dbg(_intel_dp, _dp_phy, "Sink disconnected: " _format, ## __VA_ARGS__); \
387 } while (0)
388 
389 /*
390  * enum intel_dp_link_recovery_state - LT recovery state
391  * @INTEL_DP_LINK_RECOVERY_IDLE:
392  *   No link training failure is currently tracked and no recovery is
393  *   in progress. This is the initial state after driver initialization,
394  *   power state transitions, sink (re-)connection, or after a successful
395  *   link training.
396  *
397  * @INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING:
398  *   A first link training failure has been observed and an automatic
399  *   retraining attempt with the same link parameters is pending. Exactly
400  *   one such attempt is allowed before switching to userspace-driven
401  *   recovery.
402  *
403  * @INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED:
404  *   Automatic retraining is no longer possible. At this point, a
405  *   fallback selection is made and userspace is notified to take over
406  *   recovery, performing modesets with parameters it determines are
407  *   required. The driver then selects a link configuration from the
408  *   remaining fallback configuration set. Subsequent link training
409  *   failures trigger further fallback selections and userspace
410  *   notifications.
411  *
412  * @INTEL_DP_LINK_RECOVERY_NO_FALLBACK:
413  *   Fallback selection is no longer possible, as no usable fallback link
414  *   configurations remain. Recovery must proceed via userspace modesets
415  *   using the remaining allowed link configuration. Userspace continues
416  *   to be notified of subsequent link training failures.
417  *
418  * Describes the link recovery state used by the Intel DP link recovery
419  * logic.
420  *
421  * See also:
422  *   - DOC: DisplayPort link training
423  *   - link_recovery_autoretrain_pending()
424  *   - link_recovery_autoretrain_allowed()
425  *   - link_recovery_has_no_fallback()
426  *   - link_recovery_mark_train_failure()
427  *   - link_recovery_mark_autoretrain_modeset_failure()
428  *   - link_recovery_mark_no_fallback()
429  *   - link_recovery_reset()
430  */
431 enum intel_dp_link_recovery_state {
432 	/*
433 	 * Keep the enum values ordered from least to most severe
434 	 * recovery state; helper logic relies on that ordering.
435 	 */
436 	INTEL_DP_LINK_RECOVERY_IDLE,
437 	INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING,
438 	INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED,
439 	INTEL_DP_LINK_RECOVERY_NO_FALLBACK,
440 };
441 
442 struct intel_dp_link_training {
443 	struct intel_dp *dp;
444 
445 	enum intel_dp_link_recovery_state recovery_state;
446 
447 	int force_train_failure;
448 	bool force_retrain;
449 };
450 
451 static struct intel_dp_link_training *connector_to_link_training(struct intel_connector *connector)
452 {
453 	return intel_attached_dp(connector)->link.training;
454 }
455 
456 static void intel_dp_reset_lttpr_common_caps(struct intel_dp *intel_dp)
457 {
458 	memset(intel_dp->lttpr_common_caps, 0, sizeof(intel_dp->lttpr_common_caps));
459 }
460 
461 static void intel_dp_reset_lttpr_count(struct intel_dp *intel_dp)
462 {
463 	intel_dp->lttpr_common_caps[DP_PHY_REPEATER_CNT -
464 				    DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV] = 0;
465 }
466 
467 static u8 *intel_dp_lttpr_phy_caps(struct intel_dp *intel_dp,
468 				   enum drm_dp_phy dp_phy)
469 {
470 	return intel_dp->lttpr_phy_caps[dp_phy - DP_PHY_LTTPR1];
471 }
472 
473 static void intel_dp_read_lttpr_phy_caps(struct intel_dp *intel_dp,
474 					 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
475 					 enum drm_dp_phy dp_phy)
476 {
477 	u8 *phy_caps = intel_dp_lttpr_phy_caps(intel_dp, dp_phy);
478 
479 	if (drm_dp_read_lttpr_phy_caps(&intel_dp->aux, dpcd, dp_phy, phy_caps) < 0) {
480 		lt_dbg(intel_dp, dp_phy, "failed to read the PHY caps\n");
481 		return;
482 	}
483 
484 	lt_dbg(intel_dp, dp_phy, "PHY capabilities: %*ph\n",
485 	       (int)sizeof(intel_dp->lttpr_phy_caps[0]),
486 	       phy_caps);
487 }
488 
489 static bool intel_dp_read_lttpr_common_caps(struct intel_dp *intel_dp,
490 					    const u8 dpcd[DP_RECEIVER_CAP_SIZE])
491 {
492 	int ret;
493 
494 	ret = drm_dp_read_lttpr_common_caps(&intel_dp->aux, dpcd,
495 					    intel_dp->lttpr_common_caps);
496 	if (ret < 0)
497 		goto reset_caps;
498 
499 	lt_dbg(intel_dp, DP_PHY_DPRX, "LTTPR common capabilities: %*ph\n",
500 	       (int)sizeof(intel_dp->lttpr_common_caps),
501 	       intel_dp->lttpr_common_caps);
502 
503 	/* The minimum value of LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV is 1.4 */
504 	if (intel_dp->lttpr_common_caps[0] < 0x14)
505 		goto reset_caps;
506 
507 	return true;
508 
509 reset_caps:
510 	intel_dp_reset_lttpr_common_caps(intel_dp);
511 	return false;
512 }
513 
514 static bool
515 intel_dp_set_lttpr_transparent_mode(struct intel_dp *intel_dp, bool enable)
516 {
517 	u8 val = enable ? DP_PHY_REPEATER_MODE_TRANSPARENT :
518 			  DP_PHY_REPEATER_MODE_NON_TRANSPARENT;
519 
520 	intel_dp->lttpr_common_caps[DP_PHY_REPEATER_MODE -
521 				    DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV] = val;
522 
523 	return true;
524 }
525 
526 bool intel_dp_lttpr_transparent_mode_enabled(struct intel_dp *intel_dp)
527 {
528 	return intel_dp->lttpr_common_caps[DP_PHY_REPEATER_MODE -
529 					   DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV] ==
530 		DP_PHY_REPEATER_MODE_TRANSPARENT;
531 }
532 
533 /*
534  * Read the LTTPR common capabilities and switch the LTTPR PHYs to
535  * non-transparent mode if this is supported. Preserve the
536  * transparent/non-transparent mode on an active link.
537  *
538  * Return the number of detected LTTPRs in non-transparent mode or 0 if the
539  * LTTPRs are in transparent mode or the detection failed.
540  */
541 static int intel_dp_init_lttpr_phys(struct intel_dp *intel_dp, const u8 dpcd[DP_RECEIVER_CAP_SIZE])
542 {
543 	int lttpr_count;
544 	int ret;
545 
546 	if (!intel_dp_read_lttpr_common_caps(intel_dp, dpcd))
547 		return 0;
548 
549 	lttpr_count = drm_dp_lttpr_count(intel_dp->lttpr_common_caps);
550 	/*
551 	 * Prevent setting LTTPR transparent mode explicitly if no LTTPRs are
552 	 * detected as this breaks link training at least on the Dell WD19TB
553 	 * dock.
554 	 */
555 	if (lttpr_count == 0)
556 		return 0;
557 
558 	/*
559 	 * Don't change the mode on an active link, to prevent a loss of link
560 	 * synchronization. See DP Standard v2.0 3.6.7. about the LTTPR
561 	 * resetting its internal state when the mode is changed from
562 	 * non-transparent to transparent.
563 	 */
564 	if (intel_dp->link.active) {
565 		if (lttpr_count < 0 || intel_dp_lttpr_transparent_mode_enabled(intel_dp))
566 			goto out_reset_lttpr_count;
567 
568 		return lttpr_count;
569 	}
570 
571 	ret = drm_dp_lttpr_init(&intel_dp->aux, lttpr_count);
572 	if (ret) {
573 		lt_dbg(intel_dp, DP_PHY_DPRX,
574 		       "Switching to LTTPR non-transparent LT mode failed, fall-back to transparent mode\n");
575 
576 		intel_dp_set_lttpr_transparent_mode(intel_dp, true);
577 
578 		goto out_reset_lttpr_count;
579 	}
580 
581 	intel_dp_set_lttpr_transparent_mode(intel_dp, false);
582 
583 	return lttpr_count;
584 
585 out_reset_lttpr_count:
586 	intel_dp_reset_lttpr_count(intel_dp);
587 
588 	return 0;
589 }
590 
591 static int intel_dp_init_lttpr(struct intel_dp *intel_dp, const u8 dpcd[DP_RECEIVER_CAP_SIZE])
592 {
593 	int lttpr_count;
594 	int i;
595 
596 	lttpr_count = intel_dp_init_lttpr_phys(intel_dp, dpcd);
597 
598 	for (i = 0; i < lttpr_count; i++) {
599 		intel_dp_read_lttpr_phy_caps(intel_dp, dpcd, DP_PHY_LTTPR(i));
600 		drm_dp_dump_lttpr_desc(&intel_dp->aux, DP_PHY_LTTPR(i));
601 	}
602 
603 	return lttpr_count;
604 }
605 
606 int intel_dp_read_dprx_caps(struct intel_dp *intel_dp, u8 dpcd[DP_RECEIVER_CAP_SIZE])
607 {
608 	struct intel_display *display = to_intel_display(intel_dp);
609 
610 	if (intel_dp_is_edp(intel_dp))
611 		return 0;
612 
613 	/*
614 	 * Detecting LTTPRs must be avoided on platforms with an AUX timeout
615 	 * period < 3.2ms. (see DP Standard v2.0, 2.11.2, 3.6.6.1).
616 	 */
617 	if (DISPLAY_VER(display) >= 10 && !display->platform.geminilake)
618 		if (drm_dp_dpcd_probe(&intel_dp->aux,
619 				      DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV))
620 			return -EIO;
621 
622 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, dpcd))
623 		return -EIO;
624 
625 	return 0;
626 }
627 
628 /**
629  * intel_dp_init_lttpr_and_dprx_caps - detect LTTPR and DPRX caps, init the LTTPR link training mode
630  * @intel_dp: Intel DP struct
631  *
632  * Read the LTTPR common and DPRX capabilities and switch to non-transparent
633  * link training mode if any is detected and read the PHY capabilities for all
634  * detected LTTPRs. In case of an LTTPR detection error or if the number of
635  * LTTPRs is more than is supported (8), fall back to the no-LTTPR,
636  * transparent mode link training mode.
637  *
638  * Returns:
639  * - >0  if LTTPRs were detected and the non-transparent LT mode was
640  *       set. The DPRX capabilities are read out.
641  * -  0  if no LTTPRs or more than 8 LTTPRs were detected or in case of
642  *       a detection failure and the transparent LT mode was set. The
643  *       DPRX capabilities are read out.
644  * - <0  Reading out the DPRX capabilities failed.
645  */
646 int intel_dp_init_lttpr_and_dprx_caps(struct intel_dp *intel_dp)
647 {
648 	struct intel_display *display = to_intel_display(intel_dp);
649 	int lttpr_count = 0;
650 
651 	/*
652 	 * Detecting LTTPRs must be avoided on platforms with an AUX timeout
653 	 * period < 3.2ms. (see DP Standard v2.0, 2.11.2, 3.6.6.1).
654 	 */
655 	if (!intel_dp_is_edp(intel_dp) &&
656 	    (DISPLAY_VER(display) >= 10 && !display->platform.geminilake)) {
657 		u8 dpcd[DP_RECEIVER_CAP_SIZE];
658 		int err = intel_dp_read_dprx_caps(intel_dp, dpcd);
659 
660 		if (err != 0)
661 			return err;
662 
663 		lttpr_count = intel_dp_init_lttpr(intel_dp, dpcd);
664 	}
665 
666 	/*
667 	 * The DPTX shall read the DPRX caps after LTTPR detection, so re-read
668 	 * it here.
669 	 */
670 	if (drm_dp_read_dpcd_caps(&intel_dp->aux, intel_dp->dpcd)) {
671 		intel_dp_reset_lttpr_common_caps(intel_dp);
672 		return -EIO;
673 	}
674 
675 	return lttpr_count;
676 }
677 
678 static u8 dp_voltage_max(u8 preemph)
679 {
680 	switch (preemph & DP_TRAIN_PRE_EMPHASIS_MASK) {
681 	case DP_TRAIN_PRE_EMPH_LEVEL_0:
682 		return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
683 	case DP_TRAIN_PRE_EMPH_LEVEL_1:
684 		return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
685 	case DP_TRAIN_PRE_EMPH_LEVEL_2:
686 		return DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
687 	case DP_TRAIN_PRE_EMPH_LEVEL_3:
688 	default:
689 		return DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
690 	}
691 }
692 
693 static u8 intel_dp_lttpr_voltage_max(struct intel_dp *intel_dp,
694 				     enum drm_dp_phy dp_phy)
695 {
696 	const u8 *phy_caps = intel_dp_lttpr_phy_caps(intel_dp, dp_phy);
697 
698 	if (drm_dp_lttpr_voltage_swing_level_3_supported(phy_caps))
699 		return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
700 	else
701 		return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
702 }
703 
704 static u8 intel_dp_lttpr_preemph_max(struct intel_dp *intel_dp,
705 				     enum drm_dp_phy dp_phy)
706 {
707 	const u8 *phy_caps = intel_dp_lttpr_phy_caps(intel_dp, dp_phy);
708 
709 	if (drm_dp_lttpr_pre_emphasis_level_3_supported(phy_caps))
710 		return DP_TRAIN_PRE_EMPH_LEVEL_3;
711 	else
712 		return DP_TRAIN_PRE_EMPH_LEVEL_2;
713 }
714 
715 static bool
716 intel_dp_phy_is_downstream_of_source(struct intel_dp *intel_dp,
717 				     enum drm_dp_phy dp_phy)
718 {
719 	struct intel_display *display = to_intel_display(intel_dp);
720 	int lttpr_count = drm_dp_lttpr_count(intel_dp->lttpr_common_caps);
721 
722 	drm_WARN_ON_ONCE(display->drm,
723 			 lttpr_count <= 0 && dp_phy != DP_PHY_DPRX);
724 
725 	return lttpr_count <= 0 || dp_phy == DP_PHY_LTTPR(lttpr_count - 1);
726 }
727 
728 static u8 intel_dp_phy_voltage_max(struct intel_dp *intel_dp,
729 				   const struct intel_crtc_state *crtc_state,
730 				   enum drm_dp_phy dp_phy)
731 {
732 	struct intel_display *display = to_intel_display(intel_dp);
733 	u8 voltage_max;
734 
735 	/*
736 	 * Get voltage_max from the DPTX_PHY (source or LTTPR) upstream from
737 	 * the DPRX_PHY we train.
738 	 */
739 	if (intel_dp_phy_is_downstream_of_source(intel_dp, dp_phy))
740 		voltage_max = intel_dp->voltage_max(intel_dp, crtc_state);
741 	else
742 		voltage_max = intel_dp_lttpr_voltage_max(intel_dp, dp_phy + 1);
743 
744 	drm_WARN_ON_ONCE(display->drm,
745 			 voltage_max != DP_TRAIN_VOLTAGE_SWING_LEVEL_2 &&
746 			 voltage_max != DP_TRAIN_VOLTAGE_SWING_LEVEL_3);
747 
748 	return voltage_max;
749 }
750 
751 static u8 intel_dp_phy_preemph_max(struct intel_dp *intel_dp,
752 				   enum drm_dp_phy dp_phy)
753 {
754 	struct intel_display *display = to_intel_display(intel_dp);
755 	u8 preemph_max;
756 
757 	/*
758 	 * Get preemph_max from the DPTX_PHY (source or LTTPR) upstream from
759 	 * the DPRX_PHY we train.
760 	 */
761 	if (intel_dp_phy_is_downstream_of_source(intel_dp, dp_phy))
762 		preemph_max = intel_dp->preemph_max(intel_dp);
763 	else
764 		preemph_max = intel_dp_lttpr_preemph_max(intel_dp, dp_phy + 1);
765 
766 	drm_WARN_ON_ONCE(display->drm,
767 			 preemph_max != DP_TRAIN_PRE_EMPH_LEVEL_2 &&
768 			 preemph_max != DP_TRAIN_PRE_EMPH_LEVEL_3);
769 
770 	return preemph_max;
771 }
772 
773 static bool has_per_lane_signal_levels(struct intel_dp *intel_dp,
774 				       enum drm_dp_phy dp_phy)
775 {
776 	struct intel_display *display = to_intel_display(intel_dp);
777 
778 	return !intel_dp_phy_is_downstream_of_source(intel_dp, dp_phy) ||
779 		DISPLAY_VER(display) >= 10 || display->platform.broxton;
780 }
781 
782 /* 128b/132b */
783 static u8 intel_dp_get_lane_adjust_tx_ffe_preset(struct intel_dp *intel_dp,
784 						 const struct intel_crtc_state *crtc_state,
785 						 enum drm_dp_phy dp_phy,
786 						 const u8 link_status[DP_LINK_STATUS_SIZE],
787 						 int lane)
788 {
789 	u8 tx_ffe = 0;
790 
791 	if (has_per_lane_signal_levels(intel_dp, dp_phy)) {
792 		lane = min(lane, crtc_state->lane_count - 1);
793 		tx_ffe = drm_dp_get_adjust_tx_ffe_preset(link_status, lane);
794 	} else {
795 		for (lane = 0; lane < crtc_state->lane_count; lane++)
796 			tx_ffe = max(tx_ffe, drm_dp_get_adjust_tx_ffe_preset(link_status, lane));
797 	}
798 
799 	return tx_ffe;
800 }
801 
802 /* 8b/10b */
803 static u8 intel_dp_get_lane_adjust_vswing_preemph(struct intel_dp *intel_dp,
804 						  const struct intel_crtc_state *crtc_state,
805 						  enum drm_dp_phy dp_phy,
806 						  const u8 link_status[DP_LINK_STATUS_SIZE],
807 						  int lane)
808 {
809 	u8 v = 0;
810 	u8 p = 0;
811 	u8 voltage_max;
812 	u8 preemph_max;
813 
814 	if (has_per_lane_signal_levels(intel_dp, dp_phy)) {
815 		lane = min(lane, crtc_state->lane_count - 1);
816 
817 		v = drm_dp_get_adjust_request_voltage(link_status, lane);
818 		p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
819 	} else {
820 		for (lane = 0; lane < crtc_state->lane_count; lane++) {
821 			v = max(v, drm_dp_get_adjust_request_voltage(link_status, lane));
822 			p = max(p, drm_dp_get_adjust_request_pre_emphasis(link_status, lane));
823 		}
824 	}
825 
826 	preemph_max = intel_dp_phy_preemph_max(intel_dp, dp_phy);
827 	if (p >= preemph_max)
828 		p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
829 
830 	v = min(v, dp_voltage_max(p));
831 
832 	voltage_max = intel_dp_phy_voltage_max(intel_dp, crtc_state, dp_phy);
833 	if (v >= voltage_max)
834 		v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
835 
836 	return v | p;
837 }
838 
839 static u8 intel_dp_get_lane_adjust_train(struct intel_dp *intel_dp,
840 					 const struct intel_crtc_state *crtc_state,
841 					 enum drm_dp_phy dp_phy,
842 					 const u8 link_status[DP_LINK_STATUS_SIZE],
843 					 int lane)
844 {
845 	if (intel_dp_is_uhbr(crtc_state))
846 		return intel_dp_get_lane_adjust_tx_ffe_preset(intel_dp, crtc_state,
847 							      dp_phy, link_status, lane);
848 	else
849 		return intel_dp_get_lane_adjust_vswing_preemph(intel_dp, crtc_state,
850 							       dp_phy, link_status, lane);
851 }
852 
853 #define TRAIN_REQ_FMT "%d/%d/%d/%d"
854 #define _TRAIN_REQ_VSWING_ARGS(link_status, lane) \
855 	(drm_dp_get_adjust_request_voltage((link_status), (lane)) >> DP_TRAIN_VOLTAGE_SWING_SHIFT)
856 #define TRAIN_REQ_VSWING_ARGS(link_status) \
857 	_TRAIN_REQ_VSWING_ARGS(link_status, 0), \
858 	_TRAIN_REQ_VSWING_ARGS(link_status, 1), \
859 	_TRAIN_REQ_VSWING_ARGS(link_status, 2), \
860 	_TRAIN_REQ_VSWING_ARGS(link_status, 3)
861 #define _TRAIN_REQ_PREEMPH_ARGS(link_status, lane) \
862 	(drm_dp_get_adjust_request_pre_emphasis((link_status), (lane)) >> DP_TRAIN_PRE_EMPHASIS_SHIFT)
863 #define TRAIN_REQ_PREEMPH_ARGS(link_status) \
864 	_TRAIN_REQ_PREEMPH_ARGS(link_status, 0), \
865 	_TRAIN_REQ_PREEMPH_ARGS(link_status, 1), \
866 	_TRAIN_REQ_PREEMPH_ARGS(link_status, 2), \
867 	_TRAIN_REQ_PREEMPH_ARGS(link_status, 3)
868 #define _TRAIN_REQ_TX_FFE_ARGS(link_status, lane) \
869 	drm_dp_get_adjust_tx_ffe_preset((link_status), (lane))
870 #define TRAIN_REQ_TX_FFE_ARGS(link_status) \
871 	_TRAIN_REQ_TX_FFE_ARGS(link_status, 0), \
872 	_TRAIN_REQ_TX_FFE_ARGS(link_status, 1), \
873 	_TRAIN_REQ_TX_FFE_ARGS(link_status, 2), \
874 	_TRAIN_REQ_TX_FFE_ARGS(link_status, 3)
875 
876 bool
877 intel_dp_get_adjust_train(struct intel_dp *intel_dp,
878 			  const struct intel_crtc_state *crtc_state,
879 			  enum drm_dp_phy dp_phy,
880 			  const u8 link_status[DP_LINK_STATUS_SIZE])
881 {
882 	bool changed = false;
883 	int lane;
884 
885 	if (intel_dp_is_uhbr(crtc_state)) {
886 		lt_dbg(intel_dp, dp_phy,
887 		       "128b/132b, lanes: %d, "
888 		       "TX FFE request: " TRAIN_REQ_FMT "\n",
889 		       crtc_state->lane_count,
890 		       TRAIN_REQ_TX_FFE_ARGS(link_status));
891 	} else {
892 		lt_dbg(intel_dp, dp_phy,
893 		       "8b/10b, lanes: %d, "
894 		       "vswing request: " TRAIN_REQ_FMT ", "
895 		       "pre-emphasis request: " TRAIN_REQ_FMT "\n",
896 		       crtc_state->lane_count,
897 		       TRAIN_REQ_VSWING_ARGS(link_status),
898 		       TRAIN_REQ_PREEMPH_ARGS(link_status));
899 	}
900 
901 	for (lane = 0; lane < 4; lane++) {
902 		u8 new = intel_dp_get_lane_adjust_train(intel_dp, crtc_state,
903 							dp_phy, link_status, lane);
904 		if (intel_dp->train_set[lane] == new)
905 			continue;
906 
907 		intel_dp->train_set[lane] = new;
908 		changed = true;
909 	}
910 
911 	return changed;
912 }
913 
914 static int intel_dp_training_pattern_set_reg(struct intel_dp *intel_dp,
915 					     enum drm_dp_phy dp_phy)
916 {
917 	return dp_phy == DP_PHY_DPRX ?
918 		DP_TRAINING_PATTERN_SET :
919 		DP_TRAINING_PATTERN_SET_PHY_REPEATER(dp_phy);
920 }
921 
922 static bool
923 intel_dp_set_link_train(struct intel_dp *intel_dp,
924 			const struct intel_crtc_state *crtc_state,
925 			enum drm_dp_phy dp_phy,
926 			u8 dp_train_pat)
927 {
928 	int reg = intel_dp_training_pattern_set_reg(intel_dp, dp_phy);
929 	u8 buf[sizeof(intel_dp->train_set) + 1];
930 	int len;
931 
932 	intel_dp_program_link_training_pattern(intel_dp, crtc_state,
933 					       dp_phy, dp_train_pat);
934 
935 	buf[0] = dp_train_pat;
936 	/* DP_TRAINING_LANEx_SET follow DP_TRAINING_PATTERN_SET */
937 	memcpy(buf + 1, intel_dp->train_set, crtc_state->lane_count);
938 	len = crtc_state->lane_count + 1;
939 
940 	return drm_dp_dpcd_write(&intel_dp->aux, reg, buf, len) == len;
941 }
942 
943 static char dp_training_pattern_name(u8 train_pat)
944 {
945 	switch (train_pat) {
946 	case DP_TRAINING_PATTERN_1:
947 	case DP_TRAINING_PATTERN_2:
948 	case DP_TRAINING_PATTERN_3:
949 		return '0' + train_pat;
950 	case DP_TRAINING_PATTERN_4:
951 		return '4';
952 	default:
953 		MISSING_CASE(train_pat);
954 		return '?';
955 	}
956 }
957 
958 void
959 intel_dp_program_link_training_pattern(struct intel_dp *intel_dp,
960 				       const struct intel_crtc_state *crtc_state,
961 				       enum drm_dp_phy dp_phy,
962 				       u8 dp_train_pat)
963 {
964 	u8 train_pat = intel_dp_training_pattern_symbol(dp_train_pat);
965 
966 	if (train_pat != DP_TRAINING_PATTERN_DISABLE)
967 		lt_dbg(intel_dp, dp_phy, "Using DP training pattern TPS%c\n",
968 		       dp_training_pattern_name(train_pat));
969 
970 	intel_dp->set_link_train(intel_dp, crtc_state, dp_train_pat);
971 }
972 
973 #define TRAIN_SET_FMT "%d%s/%d%s/%d%s/%d%s"
974 #define _TRAIN_SET_VSWING_ARGS(train_set) \
975 	((train_set) & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT, \
976 	(train_set) & DP_TRAIN_MAX_SWING_REACHED ? "(max)" : ""
977 #define TRAIN_SET_VSWING_ARGS(train_set) \
978 	_TRAIN_SET_VSWING_ARGS((train_set)[0]), \
979 	_TRAIN_SET_VSWING_ARGS((train_set)[1]), \
980 	_TRAIN_SET_VSWING_ARGS((train_set)[2]), \
981 	_TRAIN_SET_VSWING_ARGS((train_set)[3])
982 #define _TRAIN_SET_PREEMPH_ARGS(train_set) \
983 	((train_set) & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT, \
984 	(train_set) & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED ? "(max)" : ""
985 #define TRAIN_SET_PREEMPH_ARGS(train_set) \
986 	_TRAIN_SET_PREEMPH_ARGS((train_set)[0]), \
987 	_TRAIN_SET_PREEMPH_ARGS((train_set)[1]), \
988 	_TRAIN_SET_PREEMPH_ARGS((train_set)[2]), \
989 	_TRAIN_SET_PREEMPH_ARGS((train_set)[3])
990 #define _TRAIN_SET_TX_FFE_ARGS(train_set) \
991 	((train_set) & DP_TX_FFE_PRESET_VALUE_MASK), ""
992 #define TRAIN_SET_TX_FFE_ARGS(train_set) \
993 	_TRAIN_SET_TX_FFE_ARGS((train_set)[0]), \
994 	_TRAIN_SET_TX_FFE_ARGS((train_set)[1]), \
995 	_TRAIN_SET_TX_FFE_ARGS((train_set)[2]), \
996 	_TRAIN_SET_TX_FFE_ARGS((train_set)[3])
997 
998 void intel_dp_set_signal_levels(struct intel_dp *intel_dp,
999 				const struct intel_crtc_state *crtc_state,
1000 				enum drm_dp_phy dp_phy)
1001 {
1002 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1003 
1004 	if (intel_dp_is_uhbr(crtc_state)) {
1005 		lt_dbg(intel_dp, dp_phy,
1006 		       "128b/132b, lanes: %d, "
1007 		       "TX FFE presets: " TRAIN_SET_FMT "\n",
1008 		       crtc_state->lane_count,
1009 		       TRAIN_SET_TX_FFE_ARGS(intel_dp->train_set));
1010 	} else {
1011 		lt_dbg(intel_dp, dp_phy,
1012 		       "8b/10b, lanes: %d, "
1013 		       "vswing levels: " TRAIN_SET_FMT ", "
1014 		       "pre-emphasis levels: " TRAIN_SET_FMT "\n",
1015 		       crtc_state->lane_count,
1016 		       TRAIN_SET_VSWING_ARGS(intel_dp->train_set),
1017 		       TRAIN_SET_PREEMPH_ARGS(intel_dp->train_set));
1018 	}
1019 
1020 	if (intel_dp_phy_is_downstream_of_source(intel_dp, dp_phy))
1021 		encoder->set_signal_levels(encoder, crtc_state);
1022 }
1023 
1024 static bool
1025 intel_dp_reset_link_train(struct intel_dp *intel_dp,
1026 			  const struct intel_crtc_state *crtc_state,
1027 			  enum drm_dp_phy dp_phy,
1028 			  u8 dp_train_pat)
1029 {
1030 	memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
1031 	intel_dp_set_signal_levels(intel_dp, crtc_state, dp_phy);
1032 	return intel_dp_set_link_train(intel_dp, crtc_state, dp_phy, dp_train_pat);
1033 }
1034 
1035 static bool
1036 intel_dp_update_link_train(struct intel_dp *intel_dp,
1037 			   const struct intel_crtc_state *crtc_state,
1038 			   enum drm_dp_phy dp_phy)
1039 {
1040 	int reg = dp_phy == DP_PHY_DPRX ?
1041 			    DP_TRAINING_LANE0_SET :
1042 			    DP_TRAINING_LANE0_SET_PHY_REPEATER(dp_phy);
1043 	int ret;
1044 
1045 	intel_dp_set_signal_levels(intel_dp, crtc_state, dp_phy);
1046 
1047 	ret = drm_dp_dpcd_write(&intel_dp->aux, reg,
1048 				intel_dp->train_set, crtc_state->lane_count);
1049 
1050 	return ret == crtc_state->lane_count;
1051 }
1052 
1053 /* 128b/132b */
1054 static bool intel_dp_lane_max_tx_ffe_reached(u8 train_set_lane)
1055 {
1056 	return (train_set_lane & DP_TX_FFE_PRESET_VALUE_MASK) ==
1057 		DP_TX_FFE_PRESET_VALUE_MASK;
1058 }
1059 
1060 /*
1061  * 8b/10b
1062  *
1063  * FIXME: The DP spec is very confusing here, also the Link CTS spec seems to
1064  * have self contradicting tests around this area.
1065  *
1066  * In lieu of better ideas let's just stop when we've reached the max supported
1067  * vswing with its max pre-emphasis, which is either 2+1 or 3+0 depending on
1068  * whether vswing level 3 is supported or not.
1069  */
1070 static bool intel_dp_lane_max_vswing_reached(u8 train_set_lane)
1071 {
1072 	u8 v = (train_set_lane & DP_TRAIN_VOLTAGE_SWING_MASK) >>
1073 		DP_TRAIN_VOLTAGE_SWING_SHIFT;
1074 	u8 p = (train_set_lane & DP_TRAIN_PRE_EMPHASIS_MASK) >>
1075 		DP_TRAIN_PRE_EMPHASIS_SHIFT;
1076 
1077 	if ((train_set_lane & DP_TRAIN_MAX_SWING_REACHED) == 0)
1078 		return false;
1079 
1080 	if (v + p != 3)
1081 		return false;
1082 
1083 	return true;
1084 }
1085 
1086 static bool intel_dp_link_max_vswing_reached(struct intel_dp *intel_dp,
1087 					     const struct intel_crtc_state *crtc_state)
1088 {
1089 	int lane;
1090 
1091 	for (lane = 0; lane < crtc_state->lane_count; lane++) {
1092 		u8 train_set_lane = intel_dp->train_set[lane];
1093 
1094 		if (intel_dp_is_uhbr(crtc_state)) {
1095 			if (!intel_dp_lane_max_tx_ffe_reached(train_set_lane))
1096 				return false;
1097 		} else {
1098 			if (!intel_dp_lane_max_vswing_reached(train_set_lane))
1099 				return false;
1100 		}
1101 	}
1102 
1103 	return true;
1104 }
1105 
1106 void intel_dp_link_training_set_mode(struct intel_dp *intel_dp, int link_rate,
1107 				     bool is_vrr,
1108 				     bool pr_with_as_sdp_enable)
1109 {
1110 	u8 link_config[2];
1111 
1112 	link_config[0] = is_vrr ? DP_MSA_TIMING_PAR_IGNORE_EN : 0;
1113 	link_config[0] |= pr_with_as_sdp_enable ? DP_FIXED_VTOTAL_AS_SDP_EN_IN_PR_ACTIVE : 0;
1114 	link_config[1] = drm_dp_is_uhbr_rate(link_rate) ?
1115 			 DP_SET_ANSI_128B132B : DP_SET_ANSI_8B10B;
1116 	drm_dp_dpcd_write(&intel_dp->aux, DP_DOWNSPREAD_CTRL, link_config, 2);
1117 }
1118 
1119 static bool
1120 intel_dp_pr_with_as_sdp_enabled(struct intel_dp *intel_dp,
1121 				const struct intel_crtc_state *crtc_state)
1122 {
1123 	return intel_psr_needs_alpm_aux_less(intel_dp, crtc_state) &&
1124 		(crtc_state->infoframes.enable &
1125 		 intel_hdmi_infoframe_enable(DP_SDP_ADAPTIVE_SYNC));
1126 }
1127 
1128 static void intel_dp_update_downspread_ctrl(struct intel_dp *intel_dp,
1129 					    const struct intel_crtc_state *crtc_state)
1130 {
1131 	 /*
1132 	  * Currently, we set the MSA ignore bit based on vrr.in_range.
1133 	  * We can't really read that out during driver load since we don't have
1134 	  * the connector information read in yet. So if we do end up doing a
1135 	  * modeset during initial_commit() we'll clear the MSA ignore bit.
1136 	  * GOP likely wouldn't have set this bit so after the initial commit,
1137 	  * if there are no modesets and we enable VRR mode seamlessly
1138 	  * (without a full modeset), the MSA ignore bit might never get set.
1139 	  *
1140 	  * #TODO: Implement readout of vrr.in_range.
1141 	  * We need fastset support for setting the MSA ignore bit in DPCD,
1142 	  * especially on the first real commit when clearing the inherited flag.
1143 	  */
1144 	intel_dp_link_training_set_mode(intel_dp,
1145 					crtc_state->port_clock,
1146 					crtc_state->vrr.in_range,
1147 					intel_dp_pr_with_as_sdp_enabled(intel_dp, crtc_state));
1148 }
1149 
1150 void intel_dp_link_training_set_bw(struct intel_dp *intel_dp,
1151 				   int link_bw, int rate_select, int lane_count,
1152 				   bool enhanced_framing, bool post_lt_adj_req)
1153 {
1154 	if (enhanced_framing)
1155 		lane_count |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
1156 
1157 	if (post_lt_adj_req)
1158 		lane_count |= DP_POST_LT_ADJ_REQ_GRANTED;
1159 
1160 	if (link_bw) {
1161 		/* DP and eDP v1.3 and earlier link bw set method. */
1162 		u8 link_config[] = { link_bw, lane_count };
1163 
1164 		drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_BW_SET, link_config,
1165 				  ARRAY_SIZE(link_config));
1166 	} else {
1167 		/*
1168 		 * eDP v1.4 and later link rate set method.
1169 		 *
1170 		 * eDP v1.4x sinks shall ignore DP_LINK_RATE_SET if
1171 		 * DP_LINK_BW_SET is set. Avoid writing DP_LINK_BW_SET.
1172 		 *
1173 		 * eDP v1.5 sinks allow choosing either, and the last choice
1174 		 * shall be active.
1175 		 */
1176 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_LANE_COUNT_SET, lane_count);
1177 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_LINK_RATE_SET, rate_select);
1178 	}
1179 }
1180 
1181 /*
1182  * Pick Training Pattern Sequence (TPS) for channel equalization. 128b/132b TPS2
1183  * for UHBR+, TPS4 for HBR3 or for 1.4 devices that support it, TPS3 for HBR2 or
1184  * 1.2 devices that support it, TPS2 otherwise.
1185  */
1186 static u32 intel_dp_training_pattern(struct intel_dp *intel_dp,
1187 				     const struct intel_crtc_state *crtc_state,
1188 				     enum drm_dp_phy dp_phy)
1189 {
1190 	struct intel_display *display = to_intel_display(intel_dp);
1191 	bool source_tps3, sink_tps3, source_tps4, sink_tps4;
1192 
1193 	/* UHBR+ use separate 128b/132b TPS2 */
1194 	if (intel_dp_is_uhbr(crtc_state))
1195 		return DP_TRAINING_PATTERN_2;
1196 
1197 	/*
1198 	 * TPS4 support is mandatory for all downstream devices that
1199 	 * support HBR3. There are no known eDP panels that support
1200 	 * TPS4 as of Feb 2018 as per VESA eDP_v1.4b_E1 specification.
1201 	 * LTTPRs must support TPS4.
1202 	 */
1203 	source_tps4 = intel_dp_source_supports_tps4(display);
1204 	sink_tps4 = dp_phy != DP_PHY_DPRX ||
1205 		    drm_dp_tps4_supported(intel_dp->dpcd);
1206 	if (source_tps4 && sink_tps4) {
1207 		return DP_TRAINING_PATTERN_4;
1208 	} else if (crtc_state->port_clock == 810000) {
1209 		if (!source_tps4)
1210 			lt_dbg(intel_dp, dp_phy,
1211 			       "8.1 Gbps link rate without source TPS4 support\n");
1212 		if (!sink_tps4)
1213 			lt_dbg(intel_dp, dp_phy,
1214 			       "8.1 Gbps link rate without sink TPS4 support\n");
1215 	}
1216 
1217 	/*
1218 	 * TPS3 support is mandatory for downstream devices that
1219 	 * support HBR2. However, not all sinks follow the spec.
1220 	 */
1221 	source_tps3 = intel_dp_source_supports_tps3(display);
1222 	sink_tps3 = dp_phy != DP_PHY_DPRX ||
1223 		    drm_dp_tps3_supported(intel_dp->dpcd);
1224 	if (source_tps3 && sink_tps3) {
1225 		return  DP_TRAINING_PATTERN_3;
1226 	} else if (crtc_state->port_clock >= 540000) {
1227 		if (!source_tps3)
1228 			lt_dbg(intel_dp, dp_phy,
1229 			       ">=5.4/6.48 Gbps link rate without source TPS3 support\n");
1230 		if (!sink_tps3)
1231 			lt_dbg(intel_dp, dp_phy,
1232 			       ">=5.4/6.48 Gbps link rate without sink TPS3 support\n");
1233 	}
1234 
1235 	return DP_TRAINING_PATTERN_2;
1236 }
1237 
1238 static bool intel_dp_use_post_lt_adj_req(struct intel_dp *intel_dp,
1239 					 const struct intel_crtc_state *crtc_state)
1240 {
1241 	return intel_dp->set_idle_link_train &&
1242 		drm_dp_post_lt_adj_req_supported(intel_dp->dpcd) &&
1243 		intel_dp_training_pattern(intel_dp, crtc_state, DP_PHY_DPRX) != DP_TRAINING_PATTERN_4;
1244 }
1245 
1246 static void intel_dp_update_link_bw_set(struct intel_dp *intel_dp,
1247 					const struct intel_crtc_state *crtc_state,
1248 					u8 link_bw, u8 rate_select)
1249 {
1250 	intel_dp_link_training_set_bw(intel_dp, link_bw, rate_select, crtc_state->lane_count,
1251 				      crtc_state->enhanced_framing,
1252 				      intel_dp_use_post_lt_adj_req(intel_dp, crtc_state));
1253 }
1254 
1255 /*
1256  * Prepare link training by configuring the link parameters. On DDI platforms
1257  * also enable the port here.
1258  */
1259 static bool
1260 intel_dp_prepare_link_train(struct intel_dp *intel_dp,
1261 			    const struct intel_crtc_state *crtc_state)
1262 {
1263 	u8 link_bw, rate_select;
1264 
1265 	if (intel_dp->prepare_link_retrain)
1266 		intel_dp->prepare_link_retrain(intel_dp, crtc_state);
1267 
1268 	intel_dp_compute_rate(intel_dp, crtc_state->port_clock,
1269 			      &link_bw, &rate_select);
1270 
1271 	/*
1272 	 * WaEdpLinkRateDataReload
1273 	 *
1274 	 * Parade PS8461E MUX (used on various TGL+ laptops) needs
1275 	 * to snoop the link rates reported by the sink when we
1276 	 * use LINK_RATE_SET in order to operate in jitter cleaning
1277 	 * mode (as opposed to redriver mode). Unfortunately it
1278 	 * loses track of the snooped link rates when powered down,
1279 	 * so we need to make it re-snoop often. Without this high
1280 	 * link rates are not stable.
1281 	 */
1282 	if (!link_bw) {
1283 		__le16 sink_rates[DP_MAX_SUPPORTED_RATES];
1284 
1285 		lt_dbg(intel_dp, DP_PHY_DPRX, "Reloading eDP link rates\n");
1286 
1287 		drm_dp_dpcd_read(&intel_dp->aux, DP_SUPPORTED_LINK_RATES,
1288 				 sink_rates, sizeof(sink_rates));
1289 	}
1290 
1291 	if (link_bw)
1292 		lt_dbg(intel_dp, DP_PHY_DPRX, "Using LINK_BW_SET value %02x\n",
1293 		       link_bw);
1294 	else
1295 		lt_dbg(intel_dp, DP_PHY_DPRX,
1296 		       "Using LINK_RATE_SET value %02x\n",
1297 		       rate_select);
1298 	/*
1299 	 * Spec DP2.1 Section 3.5.2.16
1300 	 * Prior to LT DPTX should set 128b/132b DP Channel coding and then set link rate
1301 	 */
1302 	intel_dp_update_downspread_ctrl(intel_dp, crtc_state);
1303 	intel_dp_update_link_bw_set(intel_dp, crtc_state, link_bw,
1304 				    rate_select);
1305 
1306 	return true;
1307 }
1308 
1309 static bool intel_dp_adjust_request_changed(const struct intel_crtc_state *crtc_state,
1310 					    const u8 old_link_status[DP_LINK_STATUS_SIZE],
1311 					    const u8 new_link_status[DP_LINK_STATUS_SIZE])
1312 {
1313 	int lane;
1314 
1315 	for (lane = 0; lane < crtc_state->lane_count; lane++) {
1316 		u8 old, new;
1317 
1318 		if (intel_dp_is_uhbr(crtc_state)) {
1319 			old = drm_dp_get_adjust_tx_ffe_preset(old_link_status, lane);
1320 			new = drm_dp_get_adjust_tx_ffe_preset(new_link_status, lane);
1321 		} else {
1322 			old = drm_dp_get_adjust_request_voltage(old_link_status, lane) |
1323 				drm_dp_get_adjust_request_pre_emphasis(old_link_status, lane);
1324 			new = drm_dp_get_adjust_request_voltage(new_link_status, lane) |
1325 				drm_dp_get_adjust_request_pre_emphasis(new_link_status, lane);
1326 		}
1327 
1328 		if (old != new)
1329 			return true;
1330 	}
1331 
1332 	return false;
1333 }
1334 
1335 void
1336 intel_dp_dump_link_status(struct intel_dp *intel_dp, enum drm_dp_phy dp_phy,
1337 			  const u8 link_status[DP_LINK_STATUS_SIZE])
1338 {
1339 	lt_dbg(intel_dp, dp_phy,
1340 	       "ln0_1:0x%x ln2_3:0x%x align:0x%x sink:0x%x adj_req0_1:0x%x adj_req2_3:0x%x\n",
1341 	       link_status[0], link_status[1], link_status[2],
1342 	       link_status[3], link_status[4], link_status[5]);
1343 }
1344 
1345 /*
1346  * Perform the link training clock recovery phase on the given DP PHY using
1347  * training pattern 1.
1348  */
1349 static bool
1350 intel_dp_link_training_clock_recovery(struct intel_dp *intel_dp,
1351 				      const struct intel_crtc_state *crtc_state,
1352 				      enum drm_dp_phy dp_phy)
1353 {
1354 	u8 old_link_status[DP_LINK_STATUS_SIZE] = {};
1355 	int voltage_tries, cr_tries, max_cr_tries;
1356 	u8 link_status[DP_LINK_STATUS_SIZE];
1357 	bool max_vswing_reached = false;
1358 	int delay_us;
1359 
1360 	delay_us = drm_dp_read_clock_recovery_delay(&intel_dp->aux,
1361 						    intel_dp->dpcd, dp_phy,
1362 						    intel_dp_is_uhbr(crtc_state));
1363 
1364 	/* clock recovery */
1365 	if (!intel_dp_reset_link_train(intel_dp, crtc_state, dp_phy,
1366 				       DP_TRAINING_PATTERN_1 |
1367 				       DP_LINK_SCRAMBLING_DISABLE)) {
1368 		lt_err(intel_dp, dp_phy, "Failed to enable link training\n");
1369 		return false;
1370 	}
1371 
1372 	/*
1373 	 * The DP 1.4 spec defines the max clock recovery retries value
1374 	 * as 10 but for pre-DP 1.4 devices we set a very tolerant
1375 	 * retry limit of 80 (4 voltage levels x 4 preemphasis levels x
1376 	 * x 5 identical voltage retries). Since the previous specs didn't
1377 	 * define a limit and created the possibility of an infinite loop
1378 	 * we want to prevent any sync from triggering that corner case.
1379 	 */
1380 	if (intel_dp->dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
1381 		max_cr_tries = 10;
1382 	else
1383 		max_cr_tries = 80;
1384 
1385 	voltage_tries = 1;
1386 	for (cr_tries = 0; cr_tries < max_cr_tries; ++cr_tries) {
1387 		fsleep(delay_us);
1388 
1389 		if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, dp_phy,
1390 						     link_status) < 0) {
1391 			lt_err(intel_dp, dp_phy, "Failed to get link status\n");
1392 			return false;
1393 		}
1394 
1395 		if (drm_dp_clock_recovery_ok(link_status, crtc_state->lane_count)) {
1396 			lt_dbg(intel_dp, dp_phy, "Clock recovery OK\n");
1397 			return true;
1398 		}
1399 
1400 		if (voltage_tries == 5) {
1401 			intel_dp_dump_link_status(intel_dp, dp_phy, link_status);
1402 			lt_dbg(intel_dp, dp_phy, "Same voltage tried 5 times\n");
1403 			return false;
1404 		}
1405 
1406 		if (max_vswing_reached) {
1407 			intel_dp_dump_link_status(intel_dp, dp_phy, link_status);
1408 			lt_dbg(intel_dp, dp_phy, "Max Voltage Swing reached\n");
1409 			return false;
1410 		}
1411 
1412 		/* Update training set as requested by target */
1413 		intel_dp_get_adjust_train(intel_dp, crtc_state, dp_phy,
1414 					  link_status);
1415 		if (!intel_dp_update_link_train(intel_dp, crtc_state, dp_phy)) {
1416 			lt_err(intel_dp, dp_phy, "Failed to update link training\n");
1417 			return false;
1418 		}
1419 
1420 		if (!intel_dp_adjust_request_changed(crtc_state, old_link_status, link_status))
1421 			++voltage_tries;
1422 		else
1423 			voltage_tries = 1;
1424 
1425 		memcpy(old_link_status, link_status, sizeof(link_status));
1426 
1427 		if (intel_dp_link_max_vswing_reached(intel_dp, crtc_state))
1428 			max_vswing_reached = true;
1429 	}
1430 
1431 	intel_dp_dump_link_status(intel_dp, dp_phy, link_status);
1432 	lt_err(intel_dp, dp_phy, "Failed clock recovery %d times, giving up!\n",
1433 	       max_cr_tries);
1434 
1435 	return false;
1436 }
1437 
1438 /*
1439  * Perform the link training channel equalization phase on the given DP PHY
1440  * using one of training pattern 2, 3 or 4 depending on the source and
1441  * sink capabilities.
1442  */
1443 static bool
1444 intel_dp_link_training_channel_equalization(struct intel_dp *intel_dp,
1445 					    const struct intel_crtc_state *crtc_state,
1446 					    enum drm_dp_phy dp_phy)
1447 {
1448 	int tries;
1449 	u32 training_pattern;
1450 	u8 link_status[DP_LINK_STATUS_SIZE];
1451 	bool channel_eq = false;
1452 	int delay_us;
1453 
1454 	delay_us = drm_dp_read_channel_eq_delay(&intel_dp->aux,
1455 						intel_dp->dpcd, dp_phy,
1456 						intel_dp_is_uhbr(crtc_state));
1457 
1458 	training_pattern = intel_dp_training_pattern(intel_dp, crtc_state, dp_phy);
1459 	/* Scrambling is disabled for TPS2/3 and enabled for TPS4 */
1460 	if (training_pattern != DP_TRAINING_PATTERN_4)
1461 		training_pattern |= DP_LINK_SCRAMBLING_DISABLE;
1462 
1463 	/* channel equalization */
1464 	if (!intel_dp_set_link_train(intel_dp, crtc_state, dp_phy,
1465 				     training_pattern)) {
1466 		lt_err(intel_dp, dp_phy, "Failed to start channel equalization\n");
1467 		return false;
1468 	}
1469 
1470 	for (tries = 0; tries < 5; tries++) {
1471 		fsleep(delay_us);
1472 
1473 		if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, dp_phy,
1474 						     link_status) < 0) {
1475 			lt_err(intel_dp, dp_phy, "Failed to get link status\n");
1476 			break;
1477 		}
1478 
1479 		/* Make sure clock is still ok */
1480 		if (!drm_dp_clock_recovery_ok(link_status,
1481 					      crtc_state->lane_count)) {
1482 			intel_dp_dump_link_status(intel_dp, dp_phy, link_status);
1483 			lt_dbg(intel_dp, dp_phy,
1484 			       "Clock recovery check failed, cannot continue channel equalization\n");
1485 			break;
1486 		}
1487 
1488 		if (drm_dp_channel_eq_ok(link_status,
1489 					 crtc_state->lane_count)) {
1490 			channel_eq = true;
1491 			lt_dbg(intel_dp, dp_phy, "Channel EQ done. DP Training successful\n");
1492 			break;
1493 		}
1494 
1495 		/* Update training set as requested by target */
1496 		intel_dp_get_adjust_train(intel_dp, crtc_state, dp_phy,
1497 					  link_status);
1498 		if (!intel_dp_update_link_train(intel_dp, crtc_state, dp_phy)) {
1499 			lt_err(intel_dp, dp_phy, "Failed to update link training\n");
1500 			break;
1501 		}
1502 	}
1503 
1504 	/* Try 5 times, else fail and try at lower BW */
1505 	if (tries == 5) {
1506 		intel_dp_dump_link_status(intel_dp, dp_phy, link_status);
1507 		lt_dbg(intel_dp, dp_phy, "Channel equalization failed 5 times\n");
1508 	}
1509 
1510 	return channel_eq;
1511 }
1512 
1513 static bool
1514 intel_dp_post_lt_adj_req(struct intel_dp *intel_dp,
1515 			 const struct intel_crtc_state *crtc_state)
1516 {
1517 	u8 link_status[DP_LINK_STATUS_SIZE];
1518 	unsigned long deadline;
1519 	bool timeout = false;
1520 	bool success = false;
1521 	int changes = 0;
1522 
1523 	if (!intel_dp_use_post_lt_adj_req(intel_dp, crtc_state))
1524 		return true;
1525 
1526 	if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
1527 					     link_status) < 0) {
1528 		lt_err(intel_dp, DP_PHY_DPRX, "Failed to get link status\n");
1529 		return false;
1530 	}
1531 
1532 	deadline = jiffies + msecs_to_jiffies_timeout(200);
1533 
1534 	for (;;) {
1535 		/* Make sure clock is still ok */
1536 		if (!drm_dp_clock_recovery_ok(link_status,
1537 					      crtc_state->lane_count)) {
1538 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
1539 			lt_dbg(intel_dp, DP_PHY_DPRX,
1540 			       "Clock recovery check failed, cannot continue POST_LT_ADJ_REQ\n");
1541 			break;
1542 		}
1543 
1544 		if (!drm_dp_channel_eq_ok(link_status,
1545 					  crtc_state->lane_count)) {
1546 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
1547 			lt_dbg(intel_dp, DP_PHY_DPRX, "Channel EQ check failed. cannot continue POST_LT_ADJ_REQ\n");
1548 			break;
1549 		}
1550 
1551 		if (!drm_dp_post_lt_adj_req_in_progress(link_status)) {
1552 			success = true;
1553 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
1554 			lt_dbg(intel_dp, DP_PHY_DPRX,
1555 			       "POST_LT_ADJ_REQ done (%d changes). DP Training successful\n", changes);
1556 			break;
1557 		}
1558 
1559 		if (changes == 6) {
1560 			success = true;
1561 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
1562 			lt_dbg(intel_dp, DP_PHY_DPRX,
1563 			       "POST_LT_ADJ_REQ limit reached (%d changes). DP Training successful\n", changes);
1564 			break;
1565 		}
1566 
1567 		if (timeout) {
1568 			success = true;
1569 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
1570 			lt_dbg(intel_dp, DP_PHY_DPRX,
1571 			       "POST_LT_ADJ_REQ timeout reached (%d changes). DP Training successful\n", changes);
1572 			break;
1573 		}
1574 
1575 		fsleep(5000);
1576 
1577 		if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
1578 						     link_status) < 0) {
1579 			lt_err(intel_dp, DP_PHY_DPRX, "Failed to get link status\n");
1580 			break;
1581 		}
1582 
1583 		/* Update training set as requested by target */
1584 		if (intel_dp_get_adjust_train(intel_dp, crtc_state, DP_PHY_DPRX, link_status)) {
1585 			deadline = jiffies + msecs_to_jiffies_timeout(200);
1586 			changes++;
1587 
1588 			if (!intel_dp_update_link_train(intel_dp, crtc_state, DP_PHY_DPRX)) {
1589 				lt_err(intel_dp, DP_PHY_DPRX, "Failed to update link training\n");
1590 				break;
1591 			}
1592 		} else if (time_after(jiffies, deadline)) {
1593 			timeout = true;
1594 		}
1595 	}
1596 
1597 	return success;
1598 }
1599 
1600 static void intel_dp_stop_post_lt_adj_req(struct intel_dp *intel_dp,
1601 					  const struct intel_crtc_state *crtc_state)
1602 {
1603 	u8 lane_count;
1604 
1605 	if (!intel_dp_use_post_lt_adj_req(intel_dp, crtc_state))
1606 		return;
1607 
1608 	/* clear DP_POST_LT_ADJ_REQ_GRANTED */
1609 	lane_count = crtc_state->lane_count;
1610 	if (crtc_state->enhanced_framing)
1611 		lane_count |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
1612 
1613 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_LANE_COUNT_SET, lane_count);
1614 }
1615 
1616 static bool intel_dp_disable_dpcd_training_pattern(struct intel_dp *intel_dp,
1617 						   enum drm_dp_phy dp_phy)
1618 {
1619 	int reg = intel_dp_training_pattern_set_reg(intel_dp, dp_phy);
1620 	u8 val = DP_TRAINING_PATTERN_DISABLE;
1621 
1622 	return drm_dp_dpcd_write(&intel_dp->aux, reg, &val, 1) == 1;
1623 }
1624 
1625 static int
1626 intel_dp_128b132b_intra_hop(struct intel_dp *intel_dp,
1627 			    const struct intel_crtc_state *crtc_state)
1628 {
1629 	u8 sink_status;
1630 	int ret;
1631 
1632 	ret = drm_dp_dpcd_readb(&intel_dp->aux, DP_SINK_STATUS, &sink_status);
1633 	if (ret != 1) {
1634 		lt_dbg(intel_dp, DP_PHY_DPRX, "Failed to read sink status\n");
1635 		return ret < 0 ? ret : -EIO;
1636 	}
1637 
1638 	return sink_status & DP_INTRA_HOP_AUX_REPLY_INDICATION ? 1 : 0;
1639 }
1640 
1641 static bool
1642 link_recovery_autoretrain_pending(struct intel_dp_link_training *link_training)
1643 {
1644 	return link_training->recovery_state == INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING;
1645 }
1646 
1647 /*
1648  * Automatic retraining is a driver-driven link recovery mechanism that
1649  * retrains the link with the current userspace provided modeset
1650  * configuration and link parameters.
1651  *
1652  * Autoretrain is allowed while the link configurations available for
1653  * retraining, i.e. those not disabled yet via fallback selection, still
1654  * make it possible to retrain the link for the current userspace provided
1655  * modeset configuration.
1656  *
1657  * Once automatic retraining is no longer allowed, userspace driven link
1658  * recovery via userspace notifications and userspace modesets takes over.
1659  *
1660  * See also:
1661  *   - DOC: DisplayPort link training
1662  */
1663 static bool
1664 link_recovery_autoretrain_allowed(struct intel_dp_link_training *link_training)
1665 {
1666 	switch (link_training->recovery_state) {
1667 	case INTEL_DP_LINK_RECOVERY_IDLE:
1668 	case INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING:
1669 		return true;
1670 	default:
1671 		return false;
1672 	}
1673 }
1674 
1675 static bool
1676 link_recovery_has_no_fallback(struct intel_dp_link_training *link_training)
1677 {
1678 	return link_training->recovery_state == INTEL_DP_LINK_RECOVERY_NO_FALLBACK;
1679 }
1680 
1681 /*
1682  * Record a link training failure and advance the recovery state to
1683  * indicate the next required recovery step.
1684  *
1685  * The caller must proceed with recovery as instructed by the return
1686  * value, either via automatic retraining or, once automatic retraining
1687  * is no longer possible, via userspace modesets after fallback
1688  * selection.
1689  *
1690  * Note that the error reported via this function is the error seen by
1691  * the link training failure handler proper after an actual link
1692  * training failure indicated by the sink device, and so the error and
1693  * corresponding actions required are distinct from an autoretrain
1694  * modeset failure. See link_recovery_mark_autoretrain_modeset_failure() to
1695  * report a modeset failure.
1696  *
1697  * See also:
1698  *   - DOC: DisplayPort link training
1699  */
1700 static bool
1701 link_recovery_mark_train_failure(struct intel_dp_link_training *link_training)
1702 {
1703 	switch (link_training->recovery_state) {
1704 	case INTEL_DP_LINK_RECOVERY_IDLE:
1705 		link_training->recovery_state = INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING;
1706 		break;
1707 	case INTEL_DP_LINK_RECOVERY_AUTORETRAIN_PENDING:
1708 		link_training->recovery_state = INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED;
1709 		break;
1710 	default:
1711 		break;
1712 	}
1713 
1714 	return link_recovery_autoretrain_allowed(link_training);
1715 }
1716 
1717 /*
1718  * Record a failure of the autoretrain modeset before link training
1719  * itself could run.
1720  *
1721  * Note that the error reported via this function and the corresponding
1722  * expected actions are distinct from an actual link training failure:
1723  * the modeset failed before a link training attempt could be performed.
1724  * See link_recovery_mark_train_failure() to report an actual link
1725  * training failure.
1726  *
1727  * Update the state to indicate that further recovery is to be delegated to
1728  * userspace via a regular modeset.
1729  *
1730  * See also:
1731  *   - DOC: DisplayPort link training
1732  */
1733 static void
1734 link_recovery_mark_autoretrain_modeset_failure(struct intel_dp_link_training *link_training)
1735 {
1736 	if (link_recovery_autoretrain_allowed(link_training))
1737 		link_training->recovery_state = INTEL_DP_LINK_RECOVERY_AUTORETRAIN_DISABLED;
1738 }
1739 
1740 /* Record that no more link fallback configuration is available. */
1741 static void
1742 link_recovery_mark_no_fallback(struct intel_dp_link_training *link_training)
1743 {
1744 	link_training->recovery_state = INTEL_DP_LINK_RECOVERY_NO_FALLBACK;
1745 }
1746 
1747 /**
1748  * link_recovery_reset - reset the link recovery state
1749  * @link_training: link training state
1750  *
1751  * Reset the link recovery state to indicate that no link recovery is
1752  * required.
1753  */
1754 static void link_recovery_reset(struct intel_dp_link_training *link_training)
1755 {
1756 	link_training->recovery_state = INTEL_DP_LINK_RECOVERY_IDLE;
1757 }
1758 
1759 /**
1760  * intel_dp_stop_link_train - stop link training
1761  * @intel_dp: DP struct
1762  * @crtc_state: state for CRTC attached to the encoder
1763  *
1764  * Stop the link training of the @intel_dp port, disabling the training
1765  * pattern in the sink's DPCD, and disabling the test pattern symbol
1766  * generation on the port.
1767  *
1768  * What symbols are output on the port after this point is
1769  * platform specific: On DDI/VLV/CHV platforms it will be the idle pattern
1770  * with the pipe being disabled, on older platforms it's HW specific if/how an
1771  * idle pattern is generated, as the pipe is already enabled here for those.
1772  *
1773  * This function must be called after intel_dp_start_link_train().
1774  */
1775 void intel_dp_stop_link_train(struct intel_dp *intel_dp,
1776 			      const struct intel_crtc_state *crtc_state)
1777 {
1778 	struct intel_dp_link_training *link_training = intel_dp->link.training;
1779 	struct intel_display *display = to_intel_display(intel_dp);
1780 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1781 	int ret;
1782 
1783 	intel_dp->link.active = true;
1784 
1785 	intel_dp_program_link_training_pattern(intel_dp, crtc_state, DP_PHY_DPRX,
1786 					       DP_TRAINING_PATTERN_DISABLE);
1787 
1788 	if (intel_dp_is_uhbr(crtc_state)) {
1789 		ret = poll_timeout_us(ret = intel_dp_128b132b_intra_hop(intel_dp, crtc_state),
1790 				      ret == 0,
1791 				      500, 500 * 1000, false);
1792 		if (ret)
1793 			lt_dbg(intel_dp, DP_PHY_DPRX, "128b/132b intra-hop not clearing\n");
1794 	}
1795 
1796 	intel_hpd_unblock(encoder);
1797 
1798 	if (!display->hotplug.ignore_long_hpd &&
1799 	    link_recovery_autoretrain_allowed(link_training)) {
1800 		int delay_ms = link_recovery_autoretrain_pending(link_training) ? 0 : 2000;
1801 
1802 		intel_encoder_link_check_queue_work(encoder, delay_ms);
1803 	}
1804 }
1805 
1806 static bool
1807 intel_dp_link_train_phy(struct intel_dp *intel_dp,
1808 			const struct intel_crtc_state *crtc_state,
1809 			enum drm_dp_phy dp_phy)
1810 {
1811 	bool ret = false;
1812 
1813 	if (!intel_dp_link_training_clock_recovery(intel_dp, crtc_state, dp_phy))
1814 		goto out;
1815 
1816 	if (!intel_dp_link_training_channel_equalization(intel_dp, crtc_state, dp_phy))
1817 		goto out;
1818 
1819 	ret = true;
1820 
1821 out:
1822 	lt_dbg(intel_dp, dp_phy,
1823 	       "Link Training %s at link rate = %d, lane count = %d\n",
1824 	       ret ? "passed" : "failed",
1825 	       crtc_state->port_clock, crtc_state->lane_count);
1826 
1827 	return ret;
1828 }
1829 
1830 static bool intel_dp_can_link_train_fallback_for_edp(struct intel_dp *intel_dp,
1831 						     int link_rate,
1832 						     u8 lane_count)
1833 {
1834 	/* FIXME figure out what we actually want here */
1835 	const struct drm_display_mode *fixed_mode =
1836 		intel_panel_preferred_fixed_mode(intel_dp->attached_connector);
1837 	int mode_rate, max_rate;
1838 
1839 	mode_rate = intel_dp_link_required(link_rate, lane_count,
1840 					   fixed_mode->clock, fixed_mode->hdisplay,
1841 					   fxp_q4_from_int(18), 0);
1842 	max_rate = intel_dp_max_link_data_rate(intel_dp, link_rate, lane_count);
1843 	if (mode_rate > max_rate)
1844 		return false;
1845 
1846 	return true;
1847 }
1848 
1849 static bool reduce_link_params_in_bw_order(struct intel_dp *intel_dp,
1850 					   const struct intel_crtc_state *crtc_state,
1851 					   int *new_link_rate, int *new_lane_count)
1852 {
1853 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
1854 	struct intel_dp_link_config forced_params;
1855 	int link_rate;
1856 	int lane_count;
1857 	int i;
1858 
1859 	intel_dp_link_caps_get_forced_params(link_caps, &forced_params);
1860 
1861 	i = intel_dp_link_config_index(intel_dp->link.caps,
1862 				       crtc_state->port_clock, crtc_state->lane_count);
1863 	for (i--; i >= 0; i--) {
1864 		intel_dp_link_config_get(intel_dp->link.caps, i, &link_rate, &lane_count);
1865 
1866 		if ((forced_params.rate &&
1867 		     forced_params.rate != link_rate) ||
1868 		    (forced_params.lane_count &&
1869 		     forced_params.lane_count != lane_count))
1870 			continue;
1871 
1872 		break;
1873 	}
1874 
1875 	if (i < 0)
1876 		return false;
1877 
1878 	*new_link_rate = link_rate;
1879 	*new_lane_count = lane_count;
1880 
1881 	return true;
1882 }
1883 
1884 static int reduce_link_rate(struct intel_dp *intel_dp, int current_rate)
1885 {
1886 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
1887 	struct intel_dp_link_config forced_params;
1888 	int rate_index;
1889 	int new_rate;
1890 
1891 	intel_dp_link_caps_get_forced_params(link_caps, &forced_params);
1892 	if (forced_params.rate)
1893 		return -1;
1894 
1895 	rate_index = intel_dp_link_caps_common_rate_idx(link_caps,
1896 							current_rate);
1897 
1898 	if (rate_index <= 0)
1899 		return -1;
1900 
1901 	new_rate = intel_dp_common_rate(link_caps, rate_index - 1);
1902 
1903 	/* TODO: Make switching from UHBR to non-UHBR rates work. */
1904 	if (drm_dp_is_uhbr_rate(current_rate) != drm_dp_is_uhbr_rate(new_rate))
1905 		return -1;
1906 
1907 	return new_rate;
1908 }
1909 
1910 static int reduce_lane_count(struct intel_dp *intel_dp, int current_lane_count)
1911 {
1912 	struct intel_dp_link_config forced_params;
1913 
1914 	intel_dp_link_caps_get_forced_params(intel_dp->link.caps, &forced_params);
1915 	if (forced_params.lane_count)
1916 		return -1;
1917 
1918 	if (current_lane_count == 1)
1919 		return -1;
1920 
1921 	return current_lane_count >> 1;
1922 }
1923 
1924 static bool reduce_link_params_in_rate_lane_order(struct intel_dp *intel_dp,
1925 						  const struct intel_crtc_state *crtc_state,
1926 						  int *new_link_rate, int *new_lane_count)
1927 {
1928 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
1929 	int link_rate;
1930 	int lane_count;
1931 
1932 	lane_count = crtc_state->lane_count;
1933 	link_rate = reduce_link_rate(intel_dp, crtc_state->port_clock);
1934 	if (link_rate < 0) {
1935 		lane_count = reduce_lane_count(intel_dp, crtc_state->lane_count);
1936 		link_rate = intel_dp_max_common_rate(link_caps);
1937 	}
1938 
1939 	if (lane_count < 0)
1940 		return false;
1941 
1942 	*new_link_rate = link_rate;
1943 	*new_lane_count = lane_count;
1944 
1945 	return true;
1946 }
1947 
1948 static bool reduce_link_params(struct intel_dp *intel_dp, const struct intel_crtc_state *crtc_state,
1949 			       int *new_link_rate, int *new_lane_count)
1950 {
1951 	/* TODO: Use the same fallback logic on SST as on MST. */
1952 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
1953 		return reduce_link_params_in_bw_order(intel_dp, crtc_state,
1954 						      new_link_rate, new_lane_count);
1955 	else
1956 		return reduce_link_params_in_rate_lane_order(intel_dp, crtc_state,
1957 							     new_link_rate, new_lane_count);
1958 }
1959 
1960 static int intel_dp_get_link_train_fallback_values(struct intel_dp *intel_dp,
1961 						   const struct intel_crtc_state *crtc_state)
1962 {
1963 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
1964 	struct intel_dp_link_config max_link_limits;
1965 	int new_link_rate;
1966 	int new_lane_count;
1967 
1968 	if (intel_dp_is_edp(intel_dp) && !intel_dp->use_max_params) {
1969 		lt_dbg(intel_dp, DP_PHY_DPRX,
1970 		       "Retrying Link training for eDP with max parameters\n");
1971 		intel_dp->use_max_params = true;
1972 		return 0;
1973 	}
1974 
1975 	if (!reduce_link_params(intel_dp, crtc_state, &new_link_rate, &new_lane_count))
1976 		return -1;
1977 
1978 	if (intel_dp_is_edp(intel_dp) &&
1979 	    !intel_dp_can_link_train_fallback_for_edp(intel_dp, new_link_rate, new_lane_count)) {
1980 		lt_dbg(intel_dp, DP_PHY_DPRX,
1981 		       "Retrying Link training for eDP with same parameters\n");
1982 		return 0;
1983 	}
1984 
1985 	lt_dbg(intel_dp, DP_PHY_DPRX,
1986 	       "Reducing link parameters from %dx%d to %dx%d\n",
1987 	       crtc_state->lane_count, crtc_state->port_clock,
1988 	       new_lane_count, new_link_rate);
1989 
1990 	max_link_limits.rate = new_link_rate;
1991 	max_link_limits.lane_count = new_lane_count;
1992 
1993 	/* TODO: handle an update failure */
1994 	intel_dp_link_caps_set_max_limits(link_caps, &max_link_limits);
1995 
1996 	return 0;
1997 }
1998 
1999 static bool intel_dp_schedule_fallback_link_training(struct intel_atomic_state *state,
2000 						     struct intel_dp *intel_dp,
2001 						     const struct intel_crtc_state *crtc_state)
2002 {
2003 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2004 
2005 	if (!intel_digital_port_connected(&dp_to_dig_port(intel_dp)->base)) {
2006 		lt_dbg(intel_dp, DP_PHY_DPRX, "Link Training failed on disconnected sink.\n");
2007 		return true;
2008 	}
2009 
2010 	if (intel_dp->hobl_active) {
2011 		lt_dbg(intel_dp, DP_PHY_DPRX,
2012 		       "Link Training failed with HOBL active, not enabling it from now on\n");
2013 		intel_dp->hobl_failed = true;
2014 	} else if (intel_dp_get_link_train_fallback_values(intel_dp, crtc_state)) {
2015 		return false;
2016 	}
2017 
2018 	/* Schedule a Hotplug Uevent to userspace to start modeset */
2019 	intel_dp_queue_modeset_retry_for_link(state, encoder, crtc_state);
2020 
2021 	return true;
2022 }
2023 
2024 /* Perform the link training on all LTTPRs and the DPRX on a link. */
2025 static bool
2026 intel_dp_link_train_all_phys(struct intel_dp *intel_dp,
2027 			     const struct intel_crtc_state *crtc_state,
2028 			     int lttpr_count)
2029 {
2030 	bool ret = true;
2031 	int i;
2032 
2033 	for (i = lttpr_count - 1; i >= 0; i--) {
2034 		enum drm_dp_phy dp_phy = DP_PHY_LTTPR(i);
2035 
2036 		ret = intel_dp_link_train_phy(intel_dp, crtc_state, dp_phy);
2037 		intel_dp_disable_dpcd_training_pattern(intel_dp, dp_phy);
2038 
2039 		if (!ret)
2040 			break;
2041 	}
2042 
2043 	if (ret)
2044 		ret = intel_dp_link_train_phy(intel_dp, crtc_state, DP_PHY_DPRX);
2045 
2046 	intel_dp_disable_dpcd_training_pattern(intel_dp, DP_PHY_DPRX);
2047 	intel_dp->set_idle_link_train(intel_dp, crtc_state);
2048 
2049 	if (ret)
2050 		ret = intel_dp_post_lt_adj_req(intel_dp, crtc_state);
2051 
2052 	intel_dp_stop_post_lt_adj_req(intel_dp, crtc_state);
2053 
2054 	return ret;
2055 }
2056 
2057 /*
2058  * 128b/132b DP LANEx_EQ_DONE Sequence (DP 2.0 E11 3.5.2.16.1)
2059  */
2060 static bool
2061 intel_dp_128b132b_lane_eq(struct intel_dp *intel_dp,
2062 			  const struct intel_crtc_state *crtc_state)
2063 {
2064 	u8 link_status[DP_LINK_STATUS_SIZE];
2065 	int delay_us;
2066 	int try, max_tries = 20;
2067 	unsigned long deadline;
2068 	bool timeout = false;
2069 
2070 	/*
2071 	 * Reset signal levels. Start transmitting 128b/132b TPS1.
2072 	 *
2073 	 * Put DPRX and LTTPRs (if any) into intra-hop AUX mode by writing TPS1
2074 	 * in DP_TRAINING_PATTERN_SET.
2075 	 */
2076 	if (!intel_dp_reset_link_train(intel_dp, crtc_state, DP_PHY_DPRX,
2077 				       DP_TRAINING_PATTERN_1)) {
2078 		lt_err(intel_dp, DP_PHY_DPRX, "Failed to start 128b/132b TPS1\n");
2079 		return false;
2080 	}
2081 
2082 	delay_us = drm_dp_128b132b_read_aux_rd_interval(&intel_dp->aux);
2083 
2084 	/* Read the initial TX FFE settings. */
2085 	if (drm_dp_dpcd_read_link_status(&intel_dp->aux, link_status) < 0) {
2086 		lt_err(intel_dp, DP_PHY_DPRX, "Failed to read TX FFE presets\n");
2087 		return false;
2088 	}
2089 
2090 	/* Update signal levels and training set as requested. */
2091 	intel_dp_get_adjust_train(intel_dp, crtc_state, DP_PHY_DPRX, link_status);
2092 	if (!intel_dp_update_link_train(intel_dp, crtc_state, DP_PHY_DPRX)) {
2093 		lt_err(intel_dp, DP_PHY_DPRX, "Failed to set initial TX FFE settings\n");
2094 		return false;
2095 	}
2096 
2097 	/* Start transmitting 128b/132b TPS2. */
2098 	if (!intel_dp_set_link_train(intel_dp, crtc_state, DP_PHY_DPRX,
2099 				     DP_TRAINING_PATTERN_2)) {
2100 		lt_err(intel_dp, DP_PHY_DPRX, "Failed to start 128b/132b TPS2\n");
2101 		return false;
2102 	}
2103 
2104 	/* Time budget for the LANEx_EQ_DONE Sequence */
2105 	deadline = jiffies + msecs_to_jiffies_timeout(450);
2106 
2107 	for (try = 0; try < max_tries; try++) {
2108 		fsleep(delay_us);
2109 
2110 		if (drm_dp_dpcd_read_link_status(&intel_dp->aux, link_status) < 0) {
2111 			lt_err(intel_dp, DP_PHY_DPRX, "Failed to read link status\n");
2112 			return false;
2113 		}
2114 
2115 		if (drm_dp_128b132b_link_training_failed(link_status)) {
2116 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2117 			lt_err(intel_dp, DP_PHY_DPRX,
2118 			       "Downstream link training failure\n");
2119 			return false;
2120 		}
2121 
2122 		if (drm_dp_128b132b_lane_channel_eq_done(link_status, crtc_state->lane_count)) {
2123 			lt_dbg(intel_dp, DP_PHY_DPRX, "Lane channel eq done\n");
2124 			break;
2125 		}
2126 
2127 		if (timeout) {
2128 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2129 			lt_err(intel_dp, DP_PHY_DPRX, "Lane channel eq timeout\n");
2130 			return false;
2131 		}
2132 
2133 		if (time_after(jiffies, deadline))
2134 			timeout = true; /* try one last time after deadline */
2135 
2136 		/*
2137 		 * During LT, Tx shall read AUX_RD_INTERVAL just before writing the new FFE
2138 		 * presets.
2139 		 */
2140 		delay_us = drm_dp_128b132b_read_aux_rd_interval(&intel_dp->aux);
2141 
2142 		intel_dp_get_adjust_train(intel_dp, crtc_state, DP_PHY_DPRX, link_status);
2143 
2144 		/* Update signal levels and training set as requested. */
2145 		if (!intel_dp_update_link_train(intel_dp, crtc_state, DP_PHY_DPRX)) {
2146 			lt_err(intel_dp, DP_PHY_DPRX, "Failed to update TX FFE settings\n");
2147 			return false;
2148 		}
2149 	}
2150 
2151 	if (try == max_tries) {
2152 		intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2153 		lt_err(intel_dp, DP_PHY_DPRX, "Max loop count reached\n");
2154 		return false;
2155 	}
2156 
2157 	for (;;) {
2158 		if (time_after(jiffies, deadline))
2159 			timeout = true; /* try one last time after deadline */
2160 
2161 		if (drm_dp_dpcd_read_link_status(&intel_dp->aux, link_status) < 0) {
2162 			lt_err(intel_dp, DP_PHY_DPRX, "Failed to read link status\n");
2163 			return false;
2164 		}
2165 
2166 		if (drm_dp_128b132b_link_training_failed(link_status)) {
2167 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2168 			lt_err(intel_dp, DP_PHY_DPRX, "Downstream link training failure\n");
2169 			return false;
2170 		}
2171 
2172 		if (drm_dp_128b132b_eq_interlane_align_done(link_status)) {
2173 			lt_dbg(intel_dp, DP_PHY_DPRX, "Interlane align done\n");
2174 			break;
2175 		}
2176 
2177 		if (timeout) {
2178 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2179 			lt_err(intel_dp, DP_PHY_DPRX, "Interlane align timeout\n");
2180 			return false;
2181 		}
2182 
2183 		usleep_range(2000, 3000);
2184 	}
2185 
2186 	return true;
2187 }
2188 
2189 /*
2190  * 128b/132b DP LANEx_CDS_DONE Sequence (DP 2.0 E11 3.5.2.16.2)
2191  */
2192 static bool
2193 intel_dp_128b132b_lane_cds(struct intel_dp *intel_dp,
2194 			   const struct intel_crtc_state *crtc_state,
2195 			   int lttpr_count)
2196 {
2197 	u8 link_status[DP_LINK_STATUS_SIZE];
2198 	unsigned long deadline;
2199 
2200 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TRAINING_PATTERN_SET,
2201 			       DP_TRAINING_PATTERN_2_CDS) != 1) {
2202 		lt_err(intel_dp, DP_PHY_DPRX, "Failed to start 128b/132b TPS2 CDS\n");
2203 		return false;
2204 	}
2205 
2206 	/* Time budget for the LANEx_CDS_DONE Sequence */
2207 	deadline = jiffies + msecs_to_jiffies_timeout((lttpr_count + 1) * 20);
2208 
2209 	for (;;) {
2210 		bool timeout = false;
2211 
2212 		if (time_after(jiffies, deadline))
2213 			timeout = true; /* try one last time after deadline */
2214 
2215 		usleep_range(2000, 3000);
2216 
2217 		if (drm_dp_dpcd_read_link_status(&intel_dp->aux, link_status) < 0) {
2218 			lt_err(intel_dp, DP_PHY_DPRX, "Failed to read link status\n");
2219 			return false;
2220 		}
2221 
2222 		if (drm_dp_128b132b_eq_interlane_align_done(link_status) &&
2223 		    drm_dp_128b132b_cds_interlane_align_done(link_status) &&
2224 		    drm_dp_128b132b_lane_symbol_locked(link_status, crtc_state->lane_count)) {
2225 			lt_dbg(intel_dp, DP_PHY_DPRX, "CDS interlane align done\n");
2226 			break;
2227 		}
2228 
2229 		if (drm_dp_128b132b_link_training_failed(link_status)) {
2230 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2231 			lt_err(intel_dp, DP_PHY_DPRX, "Downstream link training failure\n");
2232 			return false;
2233 		}
2234 
2235 		if (timeout) {
2236 			intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2237 			lt_err(intel_dp, DP_PHY_DPRX, "CDS timeout\n");
2238 			return false;
2239 		}
2240 	}
2241 
2242 	return true;
2243 }
2244 
2245 /*
2246  * 128b/132b link training sequence. (DP 2.0 E11 SCR on link training.)
2247  */
2248 static bool
2249 intel_dp_128b132b_link_train(struct intel_dp *intel_dp,
2250 			     const struct intel_crtc_state *crtc_state,
2251 			     int lttpr_count)
2252 {
2253 	bool passed = false;
2254 	int ret;
2255 
2256 	ret = poll_timeout_us(ret = intel_dp_128b132b_intra_hop(intel_dp, crtc_state),
2257 			      ret == 0,
2258 			      500, 500 * 1000, false);
2259 	if (ret) {
2260 		lt_err(intel_dp, DP_PHY_DPRX, "128b/132b intra-hop not clear\n");
2261 		goto out;
2262 	}
2263 
2264 	if (intel_dp_128b132b_lane_eq(intel_dp, crtc_state) &&
2265 	    intel_dp_128b132b_lane_cds(intel_dp, crtc_state, lttpr_count))
2266 		passed = true;
2267 
2268 	lt_dbg(intel_dp, DP_PHY_DPRX,
2269 	       "128b/132b Link Training %s at link rate = %d, lane count = %d\n",
2270 	       passed ? "passed" : "failed",
2271 	       crtc_state->port_clock, crtc_state->lane_count);
2272 
2273 out:
2274 	/*
2275 	 * Ensure that the training pattern does get set to TPS2 even in case
2276 	 * of a failure, as is the case at the end of a passing link training
2277 	 * and what is expected by the transcoder. Leaving TPS1 set (and
2278 	 * disabling the link train mode in DP_TP_CTL later from TPS1 directly)
2279 	 * would result in a stuck transcoder HW state and flip-done timeouts
2280 	 * later in the modeset sequence.
2281 	 */
2282 	if (!passed)
2283 		intel_dp_program_link_training_pattern(intel_dp, crtc_state,
2284 						       DP_PHY_DPRX, DP_TRAINING_PATTERN_2);
2285 
2286 	intel_dp_disable_dpcd_training_pattern(intel_dp, DP_PHY_DPRX);
2287 
2288 	return passed;
2289 }
2290 
2291 /**
2292  * intel_dp_start_link_train - start link training
2293  * @state: Atomic state
2294  * @intel_dp: DP struct
2295  * @crtc_state: state for CRTC attached to the encoder
2296  *
2297  * Start the link training of the @intel_dp port, scheduling a fallback
2298  * retraining with reduced link rate/lane parameters if the link training
2299  * fails.
2300  * After calling this function intel_dp_stop_link_train() must be called.
2301  */
2302 void intel_dp_start_link_train(struct intel_atomic_state *state,
2303 			       struct intel_dp *intel_dp,
2304 			       const struct intel_crtc_state *crtc_state)
2305 {
2306 	struct intel_display *display = to_intel_display(state);
2307 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2308 	struct intel_encoder *encoder = &dig_port->base;
2309 	struct intel_dp_link_training *link_training =
2310 		intel_dp->link.training;
2311 	bool autoretrain_allowed;
2312 	bool passed;
2313 	/*
2314 	 * Reinit the LTTPRs here to ensure that they are switched to
2315 	 * non-transparent mode. During an earlier LTTPR detection this
2316 	 * could've been prevented by an active link.
2317 	 */
2318 	int lttpr_count;
2319 
2320 	intel_hpd_block(encoder);
2321 
2322 	lttpr_count = intel_dp_init_lttpr_and_dprx_caps(intel_dp);
2323 
2324 	if (lttpr_count < 0)
2325 		/* Still continue with enabling the port and link training. */
2326 		lttpr_count = 0;
2327 
2328 	intel_dp_prepare_link_train(intel_dp, crtc_state);
2329 
2330 	if (intel_dp_is_uhbr(crtc_state))
2331 		passed = intel_dp_128b132b_link_train(intel_dp, crtc_state, lttpr_count);
2332 	else
2333 		passed = intel_dp_link_train_all_phys(intel_dp, crtc_state, lttpr_count);
2334 
2335 	if (link_training->force_train_failure) {
2336 		link_training->force_train_failure--;
2337 		lt_dbg(intel_dp, DP_PHY_DPRX, "Forcing link training failure\n");
2338 	} else if (passed) {
2339 		link_recovery_reset(link_training);
2340 		return;
2341 	}
2342 
2343 	autoretrain_allowed = link_recovery_mark_train_failure(link_training);
2344 
2345 	/*
2346 	 * Ignore the link failure in CI
2347 	 *
2348 	 * In fixed environments like CI, sometimes unexpected long HPDs are
2349 	 * generated by the displays. If ignore_long_hpd flag is set, such long
2350 	 * HPDs are ignored. And probably as a consequence of these ignored
2351 	 * long HPDs, subsequent link trainings are failed resulting into CI
2352 	 * execution failures.
2353 	 *
2354 	 * For test cases which rely on the link training or processing of HPDs
2355 	 * ignore_long_hpd flag can unset from the testcase.
2356 	 */
2357 	if (display->hotplug.ignore_long_hpd) {
2358 		lt_dbg(intel_dp, DP_PHY_DPRX, "Ignore the link failure\n");
2359 		return;
2360 	}
2361 
2362 	if (autoretrain_allowed)
2363 		return;
2364 
2365 	if (intel_dp_schedule_fallback_link_training(state, intel_dp, crtc_state))
2366 		return;
2367 
2368 	link_recovery_mark_no_fallback(link_training);
2369 
2370 	if (!passed)
2371 		lt_err(intel_dp, DP_PHY_DPRX, "Can't reduce link training parameters after failure\n");
2372 	else
2373 		lt_dbg(intel_dp, DP_PHY_DPRX, "Can't reduce link training parameters after forced failure\n");
2374 }
2375 
2376 void intel_dp_128b132b_sdp_crc16(struct intel_dp *intel_dp,
2377 				 const struct intel_crtc_state *crtc_state)
2378 {
2379 	/*
2380 	 * VIDEO_DIP_CTL register bit 31 should be set to '0' to not
2381 	 * disable SDP CRC. This is applicable for Display version 13.
2382 	 * Default value of bit 31 is '0' hence discarding the write
2383 	 * TODO: Corrective actions on SDP corruption yet to be defined
2384 	 */
2385 	if (!intel_dp_is_uhbr(crtc_state))
2386 		return;
2387 
2388 	/* DP v2.0 SCR on SDP CRC16 for 128b/132b Link Layer */
2389 	drm_dp_dpcd_writeb(&intel_dp->aux,
2390 			   DP_SDP_ERROR_DETECTION_CONFIGURATION,
2391 			   DP_SDP_CRC16_128B132B_EN);
2392 
2393 	lt_dbg(intel_dp, DP_PHY_DPRX, "DP2.0 SDP CRC16 for 128b/132b enabled\n");
2394 }
2395 
2396 bool intel_dp_link_params_valid(struct intel_dp *intel_dp, int link_rate,
2397 				u8 lane_count)
2398 {
2399 	struct intel_dp_link_config max_link_limits;
2400 
2401 	/*
2402 	 * FIXME: we need to synchronize the current link parameters with
2403 	 * hardware readout. Currently fast link training doesn't work on
2404 	 * boot-up.
2405 	 *
2406 	 * NOTE:
2407 	 * This may be called from both serialized (locked and synced against
2408 	 * async commit tails) and unserialized (e.g. HPD IRQ) contexts. It
2409 	 * uses the current max link limits as upper bounds to reject
2410 	 * obviously bogus values, even if those bounds may be observed in a
2411 	 * transient or slightly stale state.
2412 	 *
2413 	 * This is not a full validation of the link configuration. Even in
2414 	 * serialized contexts, additional constraints (e.g. source limitations,
2415 	 * bandwidth checks, and other atomic state dependencies) are only
2416 	 * verified during the atomic check of the subsequent commit.
2417 	 *
2418 	 * max_link_limits only provides independent upper bounds for rate and
2419 	 * lane count. Callers must not assume it is itself an allowed link
2420 	 * configuration. Although that happens to be true for now, it will
2421 	 * stop being guaranteed once fallback depends only on disabled configs.
2422 	 */
2423 	intel_dp_link_caps_get_max_limits(intel_dp->link.caps, &max_link_limits);
2424 
2425 	if (link_rate == 0 ||
2426 	    link_rate > max_link_limits.rate)
2427 		return false;
2428 
2429 	if (lane_count == 0 ||
2430 	    lane_count > max_link_limits.lane_count)
2431 		return false;
2432 
2433 	return true;
2434 }
2435 
2436 static bool intel_dp_link_ok(struct intel_dp *intel_dp,
2437 			     u8 link_status[DP_LINK_STATUS_SIZE])
2438 {
2439 	struct intel_display *display = to_intel_display(intel_dp);
2440 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2441 	bool uhbr = intel_dp->link_rate >= 1000000;
2442 	bool ok;
2443 
2444 	if (uhbr)
2445 		ok = drm_dp_128b132b_lane_channel_eq_done(link_status,
2446 							  intel_dp->lane_count);
2447 	else
2448 		ok = drm_dp_channel_eq_ok(link_status, intel_dp->lane_count);
2449 
2450 	if (ok)
2451 		return true;
2452 
2453 	intel_dp_dump_link_status(intel_dp, DP_PHY_DPRX, link_status);
2454 	drm_dbg_kms(display->drm,
2455 		    "[ENCODER:%d:%s] %s link not ok, retraining\n",
2456 		    encoder->base.base.id, encoder->base.name,
2457 		    uhbr ? "128b/132b" : "8b/10b");
2458 
2459 	return false;
2460 }
2461 
2462 static int
2463 intel_dp_read_link_status(struct intel_dp *intel_dp, u8 link_status[DP_LINK_STATUS_SIZE])
2464 {
2465 	int err;
2466 
2467 	memset(link_status, 0, DP_LINK_STATUS_SIZE);
2468 
2469 	if (intel_dp_mst_active_streams(intel_dp) > 0)
2470 		err = drm_dp_dpcd_read_data(&intel_dp->aux, DP_LANE0_1_STATUS_ESI,
2471 					    link_status, DP_LINK_STATUS_SIZE - 2);
2472 	else
2473 		err = drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, DP_PHY_DPRX,
2474 						       link_status);
2475 
2476 	if (err)
2477 		return err;
2478 
2479 	if (link_status[DP_LANE_ALIGN_STATUS_UPDATED - DP_LANE0_1_STATUS] &
2480 	    DP_DOWNSTREAM_PORT_STATUS_CHANGED)
2481 		WRITE_ONCE(intel_dp->downstream_port_changed, true);
2482 
2483 	return 0;
2484 }
2485 
2486 bool intel_dp_link_training_get_force_retrain(struct intel_dp_link_training *link_training)
2487 {
2488 	return link_training->force_retrain;
2489 }
2490 
2491 static void intel_dp_link_training_set_force_retrain(struct intel_dp_link_training *link_training,
2492 						     bool forced)
2493 {
2494 	link_training->force_retrain = forced;
2495 }
2496 
2497 static bool
2498 intel_dp_needs_link_retrain(struct intel_dp *intel_dp)
2499 {
2500 	struct intel_dp_link_training *link_training = intel_dp->link.training;
2501 	u8 link_status[DP_LINK_STATUS_SIZE];
2502 
2503 	if (!intel_dp->link.active)
2504 		return false;
2505 
2506 	/*
2507 	 * While PSR source HW is enabled, it will control main-link sending
2508 	 * frames, enabling and disabling it so trying to do a retrain will fail
2509 	 * as the link would or not be on or it could mix training patterns
2510 	 * and frame data at the same time causing retrain to fail.
2511 	 * Also when exiting PSR, HW will retrain the link anyways fixing
2512 	 * any link status error.
2513 	 */
2514 	if (intel_psr_enabled(intel_dp))
2515 		return false;
2516 
2517 	if (intel_dp_link_training_get_force_retrain(link_training))
2518 		return true;
2519 
2520 	if (intel_dp_read_link_status(intel_dp, link_status) < 0)
2521 		return false;
2522 
2523 	/*
2524 	 * Validate the cached values of intel_dp->link_rate and
2525 	 * intel_dp->lane_count before attempting to retrain.
2526 	 *
2527 	 * FIXME would be nice to user the crtc state here, but since
2528 	 * we need to call this from the short HPD handler that seems
2529 	 * a bit hard.
2530 	 */
2531 	if (!intel_dp_link_params_valid(intel_dp, intel_dp->link_rate,
2532 					intel_dp->lane_count))
2533 		return false;
2534 
2535 	if (!link_recovery_autoretrain_allowed(link_training))
2536 		return false;
2537 
2538 	if (link_recovery_autoretrain_pending(link_training))
2539 		return true;
2540 
2541 	/* Retrain if link not ok */
2542 	return !intel_dp_link_ok(intel_dp, link_status) &&
2543 		!intel_psr_link_ok(intel_dp);
2544 }
2545 
2546 static bool intel_dp_is_connected(struct intel_dp *intel_dp)
2547 {
2548 	struct intel_connector *connector = intel_dp->attached_connector;
2549 
2550 	return connector->base.status == connector_status_connected ||
2551 		intel_dp->is_mst;
2552 }
2553 
2554 static void queue_modeset_retry_for_links_in_state(struct intel_atomic_state *state,
2555 						   struct intel_encoder *encoder,
2556 						   u8 pipe_mask)
2557 {
2558 	const struct intel_crtc_state *crtc_state;
2559 	struct intel_crtc *crtc;
2560 
2561 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state) {
2562 		if (!(BIT(crtc->pipe) & pipe_mask))
2563 			continue;
2564 
2565 		intel_dp_queue_modeset_retry_for_link(state, encoder, crtc_state);
2566 	}
2567 }
2568 
2569 static int intel_dp_retrain_link(struct intel_encoder *encoder,
2570 				 struct drm_modeset_acquire_ctx *ctx)
2571 {
2572 	struct intel_display *display = to_intel_display(encoder);
2573 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2574 	struct intel_dp_link_training *link_training =
2575 		intel_dp->link.training;
2576 	struct intel_atomic_state *state;
2577 	struct drm_atomic_commit *_state;
2578 	u8 pipe_mask;
2579 	int ret;
2580 
2581 	if (!intel_dp_is_connected(intel_dp))
2582 		return 0;
2583 
2584 	ret = drm_modeset_lock(&display->drm->mode_config.connection_mutex,
2585 			       ctx);
2586 	if (ret)
2587 		return ret;
2588 
2589 	if (!intel_dp_needs_link_retrain(intel_dp))
2590 		return 0;
2591 
2592 	ret = intel_dp_get_active_pipes(intel_dp, ctx, &pipe_mask);
2593 	if (ret)
2594 		return ret;
2595 
2596 	if (pipe_mask == 0)
2597 		return 0;
2598 
2599 	if (!intel_dp_needs_link_retrain(intel_dp))
2600 		return 0;
2601 
2602 	drm_dbg_kms(display->drm,
2603 		    "[ENCODER:%d:%s] retraining link (forced %s)\n",
2604 		    encoder->base.base.id, encoder->base.name,
2605 		    str_yes_no(intel_dp_link_training_get_force_retrain(link_training)));
2606 
2607 	_state = drm_atomic_commit_alloc(display->drm);
2608 	if (!_state)
2609 		return -ENOMEM;
2610 
2611 	state = to_intel_atomic_state(_state);
2612 
2613 	ret = intel_modeset_commit_pipes_for_atomic_state(state, pipe_mask, ctx);
2614 	if (ret == -EDEADLK)
2615 		goto out;
2616 
2617 	intel_dp_link_training_set_force_retrain(link_training, false);
2618 
2619 	if (ret) {
2620 		drm_dbg_kms(display->drm,
2621 			    "[ENCODER:%d:%s] link retraining failed: %pe\n",
2622 			    encoder->base.base.id, encoder->base.name,
2623 			    ERR_PTR(ret));
2624 		/*
2625 		 * intel_dp_needs_link_retrain() only performs a coarse check of
2626 		 * retrainability, so the modeset commit may still fail. Disable
2627 		 * further auto-retrain attempts in that case.
2628 		 *
2629 		 * A sink capability change may restore the retrainable state (see
2630 		 * intel_dp_update_sink_caps(), intel_dp_reset_link_params()),
2631 		 * allowing retraining to be attempted again.
2632 		 */
2633 		link_recovery_mark_autoretrain_modeset_failure(link_training);
2634 		queue_modeset_retry_for_links_in_state(state, encoder, pipe_mask);
2635 	}
2636 out:
2637 	drm_atomic_commit_put(&state->base);
2638 
2639 	return ret;
2640 }
2641 
2642 void intel_dp_link_check(struct intel_encoder *encoder)
2643 {
2644 	struct drm_modeset_acquire_ctx ctx;
2645 	int ret;
2646 
2647 	intel_modeset_lock_ctx_retry(&ctx, NULL, 0, ret)
2648 		ret = intel_dp_retrain_link(encoder, &ctx);
2649 }
2650 
2651 void intel_dp_check_link_state(struct intel_dp *intel_dp)
2652 {
2653 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2654 	struct intel_encoder *encoder = &dig_port->base;
2655 
2656 	if (!intel_dp_is_connected(intel_dp))
2657 		return;
2658 
2659 	/*
2660 	 * NOTE:
2661 	 * This may race with an ongoing modeset updating the max link limits
2662 	 * and, with that, the link's retrainability, so
2663 	 * intel_dp_needs_link_retrain() may observe stale state.
2664 	 *
2665 	 * This is harmless: stale params captured as valid may spuriously
2666 	 * allow retraining here, but the decision is rechecked later in a
2667 	 * properly serialized context.
2668 	 *
2669 	 * Conversely, stale params captured as invalid may skip retraining,
2670 	 * but that can only happen before the modeset has completed its own
2671 	 * link training for the new, valid configuration, after which the
2672 	 * link state is rechecked.
2673 	 *
2674 	 * See intel_dp_link_params_valid() for capturing and validating the
2675 	 * params.
2676 	 */
2677 	if (!intel_dp_needs_link_retrain(intel_dp))
2678 		return;
2679 
2680 	intel_encoder_link_check_queue_work(encoder, 0);
2681 }
2682 
2683 static int i915_dp_force_link_training_failure_show(void *data, u64 *val)
2684 {
2685 	struct intel_connector *connector = to_intel_connector(data);
2686 	struct intel_display *display = to_intel_display(connector);
2687 	struct intel_dp_link_training *link_training = connector_to_link_training(connector);
2688 	int err;
2689 
2690 	err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex);
2691 	if (err)
2692 		return err;
2693 
2694 	intel_dp_flush_connector_commits(connector);
2695 
2696 	*val = link_training->force_train_failure;
2697 
2698 	drm_modeset_unlock(&display->drm->mode_config.connection_mutex);
2699 
2700 	return 0;
2701 }
2702 
2703 static int i915_dp_force_link_training_failure_write(void *data, u64 val)
2704 {
2705 	struct intel_connector *connector = to_intel_connector(data);
2706 	struct intel_display *display = to_intel_display(connector);
2707 	struct intel_dp_link_training *link_training = connector_to_link_training(connector);
2708 	int err;
2709 
2710 	if (val > 2)
2711 		return -EINVAL;
2712 
2713 	err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex);
2714 	if (err)
2715 		return err;
2716 
2717 	intel_dp_flush_connector_commits(connector);
2718 
2719 	link_training->force_train_failure = val;
2720 
2721 	drm_modeset_unlock(&display->drm->mode_config.connection_mutex);
2722 
2723 	return 0;
2724 }
2725 DEFINE_DEBUGFS_ATTRIBUTE(i915_dp_force_link_training_failure_fops,
2726 			 i915_dp_force_link_training_failure_show,
2727 			 i915_dp_force_link_training_failure_write, "%llu\n");
2728 
2729 static int i915_dp_force_link_retrain_show(void *data, u64 *val)
2730 {
2731 	struct intel_connector *connector = to_intel_connector(data);
2732 	struct intel_display *display = to_intel_display(connector);
2733 	struct intel_dp_link_training *link_training = connector_to_link_training(connector);
2734 	int err;
2735 
2736 	err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex);
2737 	if (err)
2738 		return err;
2739 
2740 	intel_dp_flush_connector_commits(connector);
2741 
2742 	*val = intel_dp_link_training_get_force_retrain(link_training);
2743 
2744 	drm_modeset_unlock(&display->drm->mode_config.connection_mutex);
2745 
2746 	return 0;
2747 }
2748 
2749 static int i915_dp_force_link_retrain_write(void *data, u64 val)
2750 {
2751 	struct intel_connector *connector = to_intel_connector(data);
2752 	struct intel_display *display = to_intel_display(connector);
2753 	struct intel_dp_link_training *link_training = connector_to_link_training(connector);
2754 	struct intel_dp *intel_dp = link_training->dp;
2755 	int err;
2756 
2757 	err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex);
2758 	if (err)
2759 		return err;
2760 
2761 	intel_dp_flush_connector_commits(connector);
2762 
2763 	intel_dp_link_training_set_force_retrain(link_training, val);
2764 
2765 	drm_modeset_unlock(&display->drm->mode_config.connection_mutex);
2766 
2767 	intel_hpd_trigger_irq(dp_to_dig_port(intel_dp));
2768 
2769 	return 0;
2770 }
2771 DEFINE_DEBUGFS_ATTRIBUTE(i915_dp_force_link_retrain_fops,
2772 			 i915_dp_force_link_retrain_show,
2773 			 i915_dp_force_link_retrain_write, "%llu\n");
2774 
2775 static int i915_dp_link_retrain_disabled_show(struct seq_file *m, void *data)
2776 {
2777 	struct intel_connector *connector = to_intel_connector(m->private);
2778 	struct intel_display *display = to_intel_display(connector);
2779 	struct intel_dp_link_training *link_training = connector_to_link_training(connector);
2780 	int err;
2781 
2782 	err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex);
2783 	if (err)
2784 		return err;
2785 
2786 	intel_dp_flush_connector_commits(connector);
2787 
2788 	/* TODO: Expose this via a debugfs entry reflecting what the state represents. */
2789 	seq_printf(m, "%s\n", str_yes_no(link_recovery_has_no_fallback(link_training)));
2790 
2791 	drm_modeset_unlock(&display->drm->mode_config.connection_mutex);
2792 
2793 	return 0;
2794 }
2795 DEFINE_SHOW_ATTRIBUTE(i915_dp_link_retrain_disabled);
2796 
2797 void intel_dp_link_training_debugfs_add(struct intel_connector *connector)
2798 {
2799 	struct dentry *root = connector->base.debugfs_entry;
2800 
2801 	if (connector->base.connector_type != DRM_MODE_CONNECTOR_DisplayPort &&
2802 	    connector->base.connector_type != DRM_MODE_CONNECTOR_eDP)
2803 		return;
2804 
2805 	debugfs_create_file("i915_dp_force_link_training_failure", 0644, root,
2806 			    connector, &i915_dp_force_link_training_failure_fops);
2807 
2808 	debugfs_create_file("i915_dp_force_link_retrain", 0644, root,
2809 			    connector, &i915_dp_force_link_retrain_fops);
2810 
2811 	debugfs_create_file("i915_dp_link_retrain_disabled", 0444, root,
2812 			    connector, &i915_dp_link_retrain_disabled_fops);
2813 }
2814 
2815 void intel_dp_link_training_reset(struct intel_dp_link_training *link_training)
2816 {
2817 	link_recovery_reset(link_training);
2818 }
2819 
2820 struct intel_dp_link_training *intel_dp_link_training_init(struct intel_dp *intel_dp)
2821 {
2822 	struct intel_dp_link_training *link_training;
2823 
2824 	link_training = kzalloc_obj(*link_training);
2825 	if (!link_training)
2826 		return NULL;
2827 
2828 	link_training->dp = intel_dp;
2829 
2830 	return link_training;
2831 }
2832 
2833 void intel_dp_link_training_cleanup(struct intel_dp_link_training *link_training)
2834 {
2835 	kfree(link_training);
2836 }
2837