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