Revert "drm/amd/display: Always write repeater mode regardless of LTTPR"
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / display / dc / core / dc_link_dp.c
1 /* Copyright 2015 Advanced Micro Devices, Inc. */
2 #include "dm_services.h"
3 #include "dc.h"
4 #include "dc_link_dp.h"
5 #include "dm_helpers.h"
6 #include "opp.h"
7 #include "dsc.h"
8 #include "resource.h"
9
10 #include "inc/core_types.h"
11 #include "link_hwss.h"
12 #include "dc_link_ddc.h"
13 #include "core_status.h"
14 #include "dpcd_defs.h"
15 #include "dc_dmub_srv.h"
16 #include "dce/dmub_hw_lock_mgr.h"
17 #include "inc/link_enc_cfg.h"
18
19 /*Travis*/
20 static const uint8_t DP_VGA_LVDS_CONVERTER_ID_2[] = "sivarT";
21 /*Nutmeg*/
22 static const uint8_t DP_VGA_LVDS_CONVERTER_ID_3[] = "dnomlA";
23
24 #define DC_LOGGER \
25         link->ctx->logger
26 #define DC_TRACE_LEVEL_MESSAGE(...) /* do nothing */
27
28 #include "link_dpcd.h"
29
30         /* maximum pre emphasis level allowed for each voltage swing level*/
31         static const enum dc_pre_emphasis
32         voltage_swing_to_pre_emphasis[] = { PRE_EMPHASIS_LEVEL3,
33                                             PRE_EMPHASIS_LEVEL2,
34                                             PRE_EMPHASIS_LEVEL1,
35                                             PRE_EMPHASIS_DISABLED };
36
37 enum {
38         POST_LT_ADJ_REQ_LIMIT = 6,
39         POST_LT_ADJ_REQ_TIMEOUT = 200
40 };
41
42 static bool decide_fallback_link_setting(
43                 struct dc_link_settings initial_link_settings,
44                 struct dc_link_settings *current_link_setting,
45                 enum link_training_result training_result);
46 static struct dc_link_settings get_common_supported_link_settings(
47                 struct dc_link_settings link_setting_a,
48                 struct dc_link_settings link_setting_b);
49
50 static uint32_t get_cr_training_aux_rd_interval(struct dc_link *link,
51                 const struct dc_link_settings *link_settings)
52 {
53         union training_aux_rd_interval training_rd_interval;
54         uint32_t wait_in_micro_secs = 100;
55
56         memset(&training_rd_interval, 0, sizeof(training_rd_interval));
57         core_link_read_dpcd(
58                         link,
59                         DP_TRAINING_AUX_RD_INTERVAL,
60                         (uint8_t *)&training_rd_interval,
61                         sizeof(training_rd_interval));
62         if (training_rd_interval.bits.TRAINIG_AUX_RD_INTERVAL)
63                 wait_in_micro_secs = training_rd_interval.bits.TRAINIG_AUX_RD_INTERVAL * 4000;
64         return wait_in_micro_secs;
65 }
66
67 static uint32_t get_eq_training_aux_rd_interval(
68         struct dc_link *link,
69         const struct dc_link_settings *link_settings)
70 {
71         union training_aux_rd_interval training_rd_interval;
72         uint32_t wait_in_micro_secs = 400;
73
74         memset(&training_rd_interval, 0, sizeof(training_rd_interval));
75         /* overwrite the delay if rev > 1.1*/
76         if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_12) {
77                 /* DP 1.2 or later - retrieve delay through
78                  * "DPCD_ADDR_TRAINING_AUX_RD_INTERVAL" register */
79                 core_link_read_dpcd(
80                         link,
81                         DP_TRAINING_AUX_RD_INTERVAL,
82                         (uint8_t *)&training_rd_interval,
83                         sizeof(training_rd_interval));
84
85                 if (training_rd_interval.bits.TRAINIG_AUX_RD_INTERVAL)
86                         wait_in_micro_secs = training_rd_interval.bits.TRAINIG_AUX_RD_INTERVAL * 4000;
87         }
88
89         return wait_in_micro_secs;
90 }
91
92 void dp_wait_for_training_aux_rd_interval(
93         struct dc_link *link,
94         uint32_t wait_in_micro_secs)
95 {
96         udelay(wait_in_micro_secs);
97
98         DC_LOG_HW_LINK_TRAINING("%s:\n wait = %d\n",
99                 __func__,
100                 wait_in_micro_secs);
101 }
102
103 enum dpcd_training_patterns
104         dc_dp_training_pattern_to_dpcd_training_pattern(
105         struct dc_link *link,
106         enum dc_dp_training_pattern pattern)
107 {
108         enum dpcd_training_patterns dpcd_tr_pattern =
109         DPCD_TRAINING_PATTERN_VIDEOIDLE;
110
111         switch (pattern) {
112         case DP_TRAINING_PATTERN_SEQUENCE_1:
113                 dpcd_tr_pattern = DPCD_TRAINING_PATTERN_1;
114                 break;
115         case DP_TRAINING_PATTERN_SEQUENCE_2:
116                 dpcd_tr_pattern = DPCD_TRAINING_PATTERN_2;
117                 break;
118         case DP_TRAINING_PATTERN_SEQUENCE_3:
119                 dpcd_tr_pattern = DPCD_TRAINING_PATTERN_3;
120                 break;
121         case DP_TRAINING_PATTERN_SEQUENCE_4:
122                 dpcd_tr_pattern = DPCD_TRAINING_PATTERN_4;
123                 break;
124         case DP_TRAINING_PATTERN_VIDEOIDLE:
125                 dpcd_tr_pattern = DPCD_TRAINING_PATTERN_VIDEOIDLE;
126                 break;
127         default:
128                 ASSERT(0);
129                 DC_LOG_HW_LINK_TRAINING("%s: Invalid HW Training pattern: %d\n",
130                         __func__, pattern);
131                 break;
132         }
133
134         return dpcd_tr_pattern;
135 }
136
137 static void dpcd_set_training_pattern(
138         struct dc_link *link,
139         enum dc_dp_training_pattern training_pattern)
140 {
141         union dpcd_training_pattern dpcd_pattern = { {0} };
142
143         dpcd_pattern.v1_4.TRAINING_PATTERN_SET =
144                         dc_dp_training_pattern_to_dpcd_training_pattern(
145                                         link, training_pattern);
146
147         core_link_write_dpcd(
148                 link,
149                 DP_TRAINING_PATTERN_SET,
150                 &dpcd_pattern.raw,
151                 1);
152
153         DC_LOG_HW_LINK_TRAINING("%s\n %x pattern = %x\n",
154                 __func__,
155                 DP_TRAINING_PATTERN_SET,
156                 dpcd_pattern.v1_4.TRAINING_PATTERN_SET);
157 }
158
159 static enum dc_dp_training_pattern decide_cr_training_pattern(
160                 const struct dc_link_settings *link_settings)
161 {
162         return DP_TRAINING_PATTERN_SEQUENCE_1;
163 }
164
165 static enum dc_dp_training_pattern decide_eq_training_pattern(struct dc_link *link,
166                 const struct dc_link_settings *link_settings)
167 {
168         struct link_encoder *link_enc;
169         enum dc_dp_training_pattern highest_tp = DP_TRAINING_PATTERN_SEQUENCE_2;
170         struct encoder_feature_support *features;
171         struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
172
173         /* Access link encoder capability based on whether it is statically
174          * or dynamically assigned to a link.
175          */
176         if (link->is_dig_mapping_flexible &&
177                         link->dc->res_pool->funcs->link_encs_assign)
178                 link_enc = link_enc_cfg_get_link_enc_used_by_link(link->dc->current_state, link);
179         else
180                 link_enc = link->link_enc;
181         ASSERT(link_enc);
182         features = &link_enc->features;
183
184         if (features->flags.bits.IS_TPS3_CAPABLE)
185                 highest_tp = DP_TRAINING_PATTERN_SEQUENCE_3;
186
187         if (features->flags.bits.IS_TPS4_CAPABLE)
188                 highest_tp = DP_TRAINING_PATTERN_SEQUENCE_4;
189
190         if (dpcd_caps->max_down_spread.bits.TPS4_SUPPORTED &&
191                 highest_tp >= DP_TRAINING_PATTERN_SEQUENCE_4)
192                 return DP_TRAINING_PATTERN_SEQUENCE_4;
193
194         if (dpcd_caps->max_ln_count.bits.TPS3_SUPPORTED &&
195                 highest_tp >= DP_TRAINING_PATTERN_SEQUENCE_3)
196                 return DP_TRAINING_PATTERN_SEQUENCE_3;
197
198         return DP_TRAINING_PATTERN_SEQUENCE_2;
199 }
200
201 enum dc_status dpcd_set_link_settings(
202         struct dc_link *link,
203         const struct link_training_settings *lt_settings)
204 {
205         uint8_t rate;
206         enum dc_status status;
207
208         union down_spread_ctrl downspread = { {0} };
209         union lane_count_set lane_count_set = { {0} };
210
211         downspread.raw = (uint8_t)
212         (lt_settings->link_settings.link_spread);
213
214         lane_count_set.bits.LANE_COUNT_SET =
215         lt_settings->link_settings.lane_count;
216
217         lane_count_set.bits.ENHANCED_FRAMING = lt_settings->enhanced_framing;
218         lane_count_set.bits.POST_LT_ADJ_REQ_GRANTED = 0;
219
220
221         if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
222                         lt_settings->pattern_for_eq < DP_TRAINING_PATTERN_SEQUENCE_4) {
223                 lane_count_set.bits.POST_LT_ADJ_REQ_GRANTED =
224                                 link->dpcd_caps.max_ln_count.bits.POST_LT_ADJ_REQ_SUPPORTED;
225         }
226
227         status = core_link_write_dpcd(link, DP_DOWNSPREAD_CTRL,
228                 &downspread.raw, sizeof(downspread));
229
230         status = core_link_write_dpcd(link, DP_LANE_COUNT_SET,
231                 &lane_count_set.raw, 1);
232
233         if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_14 &&
234                         lt_settings->link_settings.use_link_rate_set == true) {
235                 rate = 0;
236                 /* WA for some MUX chips that will power down with eDP and lose supported
237                  * link rate set for eDP 1.4. Source reads DPCD 0x010 again to ensure
238                  * MUX chip gets link rate set back before link training.
239                  */
240                 if (link->connector_signal == SIGNAL_TYPE_EDP) {
241                         uint8_t supported_link_rates[16];
242
243                         core_link_read_dpcd(link, DP_SUPPORTED_LINK_RATES,
244                                         supported_link_rates, sizeof(supported_link_rates));
245                 }
246                 status = core_link_write_dpcd(link, DP_LINK_BW_SET, &rate, 1);
247                 status = core_link_write_dpcd(link, DP_LINK_RATE_SET,
248                                 &lt_settings->link_settings.link_rate_set, 1);
249         } else {
250                 rate = (uint8_t) (lt_settings->link_settings.link_rate);
251                 status = core_link_write_dpcd(link, DP_LINK_BW_SET, &rate, 1);
252         }
253
254         if (rate) {
255                 DC_LOG_HW_LINK_TRAINING("%s\n %x rate = %x\n %x lane = %x framing = %x\n %x spread = %x\n",
256                         __func__,
257                         DP_LINK_BW_SET,
258                         lt_settings->link_settings.link_rate,
259                         DP_LANE_COUNT_SET,
260                         lt_settings->link_settings.lane_count,
261                         lt_settings->enhanced_framing,
262                         DP_DOWNSPREAD_CTRL,
263                         lt_settings->link_settings.link_spread);
264         } else {
265                 DC_LOG_HW_LINK_TRAINING("%s\n %x rate set = %x\n %x lane = %x framing = %x\n %x spread = %x\n",
266                         __func__,
267                         DP_LINK_RATE_SET,
268                         lt_settings->link_settings.link_rate_set,
269                         DP_LANE_COUNT_SET,
270                         lt_settings->link_settings.lane_count,
271                         lt_settings->enhanced_framing,
272                         DP_DOWNSPREAD_CTRL,
273                         lt_settings->link_settings.link_spread);
274         }
275
276         return status;
277 }
278
279 uint8_t dc_dp_initialize_scrambling_data_symbols(
280         struct dc_link *link,
281         enum dc_dp_training_pattern pattern)
282 {
283         uint8_t disable_scrabled_data_symbols = 0;
284
285         switch (pattern) {
286         case DP_TRAINING_PATTERN_SEQUENCE_1:
287         case DP_TRAINING_PATTERN_SEQUENCE_2:
288         case DP_TRAINING_PATTERN_SEQUENCE_3:
289                 disable_scrabled_data_symbols = 1;
290                 break;
291         case DP_TRAINING_PATTERN_SEQUENCE_4:
292                 disable_scrabled_data_symbols = 0;
293                 break;
294         default:
295                 ASSERT(0);
296                 DC_LOG_HW_LINK_TRAINING("%s: Invalid HW Training pattern: %d\n",
297                         __func__, pattern);
298                 break;
299         }
300         return disable_scrabled_data_symbols;
301 }
302
303 static inline bool is_repeater(struct dc_link *link, uint32_t offset)
304 {
305         return (link->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT) && (offset != 0);
306 }
307
308 static void dpcd_set_lt_pattern_and_lane_settings(
309         struct dc_link *link,
310         const struct link_training_settings *lt_settings,
311         enum dc_dp_training_pattern pattern,
312         uint32_t offset)
313 {
314         union dpcd_training_lane dpcd_lane[LANE_COUNT_DP_MAX] = { { {0} } };
315
316         uint32_t dpcd_base_lt_offset;
317
318         uint8_t dpcd_lt_buffer[5] = {0};
319         union dpcd_training_pattern dpcd_pattern = { {0} };
320         uint32_t lane;
321         uint32_t size_in_bytes;
322         bool edp_workaround = false; /* TODO link_prop.INTERNAL */
323         dpcd_base_lt_offset = DP_TRAINING_PATTERN_SET;
324
325         if (is_repeater(link, offset))
326                 dpcd_base_lt_offset = DP_TRAINING_PATTERN_SET_PHY_REPEATER1 +
327                         ((DP_REPEATER_CONFIGURATION_AND_STATUS_SIZE) * (offset - 1));
328
329         /*****************************************************************
330         * DpcdAddress_TrainingPatternSet
331         *****************************************************************/
332         dpcd_pattern.v1_4.TRAINING_PATTERN_SET =
333                 dc_dp_training_pattern_to_dpcd_training_pattern(link, pattern);
334
335         dpcd_pattern.v1_4.SCRAMBLING_DISABLE =
336                 dc_dp_initialize_scrambling_data_symbols(link, pattern);
337
338         dpcd_lt_buffer[DP_TRAINING_PATTERN_SET - DP_TRAINING_PATTERN_SET]
339                 = dpcd_pattern.raw;
340
341         if (is_repeater(link, offset)) {
342                 DC_LOG_HW_LINK_TRAINING("%s\n LTTPR Repeater ID: %d\n 0x%X pattern = %x\n",
343                         __func__,
344                         offset,
345                         dpcd_base_lt_offset,
346                         dpcd_pattern.v1_4.TRAINING_PATTERN_SET);
347         } else {
348                 DC_LOG_HW_LINK_TRAINING("%s\n 0x%X pattern = %x\n",
349                         __func__,
350                         dpcd_base_lt_offset,
351                         dpcd_pattern.v1_4.TRAINING_PATTERN_SET);
352         }
353         /*****************************************************************
354         * DpcdAddress_Lane0Set -> DpcdAddress_Lane3Set
355         *****************************************************************/
356         for (lane = 0; lane <
357                 (uint32_t)(lt_settings->link_settings.lane_count); lane++) {
358
359                 dpcd_lane[lane].bits.VOLTAGE_SWING_SET =
360                 (uint8_t)(lt_settings->lane_settings[lane].VOLTAGE_SWING);
361                 dpcd_lane[lane].bits.PRE_EMPHASIS_SET =
362                 (uint8_t)(lt_settings->lane_settings[lane].PRE_EMPHASIS);
363
364                 dpcd_lane[lane].bits.MAX_SWING_REACHED =
365                 (lt_settings->lane_settings[lane].VOLTAGE_SWING ==
366                 VOLTAGE_SWING_MAX_LEVEL ? 1 : 0);
367                 dpcd_lane[lane].bits.MAX_PRE_EMPHASIS_REACHED =
368                 (lt_settings->lane_settings[lane].PRE_EMPHASIS ==
369                 PRE_EMPHASIS_MAX_LEVEL ? 1 : 0);
370         }
371
372         /* concatenate everything into one buffer*/
373
374         size_in_bytes = lt_settings->link_settings.lane_count * sizeof(dpcd_lane[0]);
375
376          // 0x00103 - 0x00102
377         memmove(
378                 &dpcd_lt_buffer[DP_TRAINING_LANE0_SET - DP_TRAINING_PATTERN_SET],
379                 dpcd_lane,
380                 size_in_bytes);
381
382         if (is_repeater(link, offset)) {
383                 DC_LOG_HW_LINK_TRAINING("%s:\n LTTPR Repeater ID: %d\n"
384                                 " 0x%X VS set = %x PE set = %x max VS Reached = %x  max PE Reached = %x\n",
385                         __func__,
386                         offset,
387                         dpcd_base_lt_offset,
388                         dpcd_lane[0].bits.VOLTAGE_SWING_SET,
389                         dpcd_lane[0].bits.PRE_EMPHASIS_SET,
390                         dpcd_lane[0].bits.MAX_SWING_REACHED,
391                         dpcd_lane[0].bits.MAX_PRE_EMPHASIS_REACHED);
392         } else {
393                 DC_LOG_HW_LINK_TRAINING("%s:\n 0x%X VS set = %x  PE set = %x max VS Reached = %x  max PE Reached = %x\n",
394                         __func__,
395                         dpcd_base_lt_offset,
396                         dpcd_lane[0].bits.VOLTAGE_SWING_SET,
397                         dpcd_lane[0].bits.PRE_EMPHASIS_SET,
398                         dpcd_lane[0].bits.MAX_SWING_REACHED,
399                         dpcd_lane[0].bits.MAX_PRE_EMPHASIS_REACHED);
400         }
401         if (edp_workaround) {
402                 /* for eDP write in 2 parts because the 5-byte burst is
403                 * causing issues on some eDP panels (EPR#366724)
404                 */
405                 core_link_write_dpcd(
406                         link,
407                         DP_TRAINING_PATTERN_SET,
408                         &dpcd_pattern.raw,
409                         sizeof(dpcd_pattern.raw));
410
411                 core_link_write_dpcd(
412                         link,
413                         DP_TRAINING_LANE0_SET,
414                         (uint8_t *)(dpcd_lane),
415                         size_in_bytes);
416
417                 } else
418                 /* write it all in (1 + number-of-lanes)-byte burst*/
419                         core_link_write_dpcd(
420                                 link,
421                                 dpcd_base_lt_offset,
422                                 dpcd_lt_buffer,
423                                 size_in_bytes + sizeof(dpcd_pattern.raw));
424
425         link->cur_lane_setting = lt_settings->lane_settings[0];
426 }
427
428 bool dp_is_cr_done(enum dc_lane_count ln_count,
429         union lane_status *dpcd_lane_status)
430 {
431         uint32_t lane;
432         /*LANEx_CR_DONE bits All 1's?*/
433         for (lane = 0; lane < (uint32_t)(ln_count); lane++) {
434                 if (!dpcd_lane_status[lane].bits.CR_DONE_0)
435                         return false;
436         }
437         return true;
438 }
439
440 bool dp_is_ch_eq_done(enum dc_lane_count ln_count,
441                 union lane_status *dpcd_lane_status)
442 {
443         bool done = true;
444         uint32_t lane;
445         for (lane = 0; lane < (uint32_t)(ln_count); lane++)
446                 if (!dpcd_lane_status[lane].bits.CHANNEL_EQ_DONE_0)
447                         done = false;
448         return done;
449 }
450
451 bool dp_is_symbol_locked(enum dc_lane_count ln_count,
452                 union lane_status *dpcd_lane_status)
453 {
454         bool locked = true;
455         uint32_t lane;
456         for (lane = 0; lane < (uint32_t)(ln_count); lane++)
457                 if (!dpcd_lane_status[lane].bits.SYMBOL_LOCKED_0)
458                         locked = false;
459         return locked;
460 }
461
462 bool dp_is_interlane_aligned(union lane_align_status_updated align_status)
463 {
464         return align_status.bits.INTERLANE_ALIGN_DONE == 1;
465 }
466
467 void dp_update_drive_settings(
468                 struct link_training_settings *dest,
469                 struct link_training_settings src)
470 {
471         uint32_t lane;
472         for (lane = 0; lane < src.link_settings.lane_count; lane++) {
473                 if (dest->voltage_swing == NULL)
474                         dest->lane_settings[lane].VOLTAGE_SWING = src.lane_settings[lane].VOLTAGE_SWING;
475                 else
476                         dest->lane_settings[lane].VOLTAGE_SWING = *dest->voltage_swing;
477
478                 if (dest->pre_emphasis == NULL)
479                         dest->lane_settings[lane].PRE_EMPHASIS = src.lane_settings[lane].PRE_EMPHASIS;
480                 else
481                         dest->lane_settings[lane].PRE_EMPHASIS = *dest->pre_emphasis;
482
483                 if (dest->post_cursor2 == NULL)
484                         dest->lane_settings[lane].POST_CURSOR2 = src.lane_settings[lane].POST_CURSOR2;
485                 else
486                         dest->lane_settings[lane].POST_CURSOR2 = *dest->post_cursor2;
487         }
488 }
489
490 static uint8_t get_nibble_at_index(const uint8_t *buf,
491         uint32_t index)
492 {
493         uint8_t nibble;
494         nibble = buf[index / 2];
495
496         if (index % 2)
497                 nibble >>= 4;
498         else
499                 nibble &= 0x0F;
500
501         return nibble;
502 }
503
504 static enum dc_pre_emphasis get_max_pre_emphasis_for_voltage_swing(
505         enum dc_voltage_swing voltage)
506 {
507         enum dc_pre_emphasis pre_emphasis;
508         pre_emphasis = PRE_EMPHASIS_MAX_LEVEL;
509
510         if (voltage <= VOLTAGE_SWING_MAX_LEVEL)
511                 pre_emphasis = voltage_swing_to_pre_emphasis[voltage];
512
513         return pre_emphasis;
514
515 }
516
517 static void find_max_drive_settings(
518         const struct link_training_settings *link_training_setting,
519         struct link_training_settings *max_lt_setting)
520 {
521         uint32_t lane;
522         struct dc_lane_settings max_requested;
523
524         max_requested.VOLTAGE_SWING =
525                 link_training_setting->
526                 lane_settings[0].VOLTAGE_SWING;
527         max_requested.PRE_EMPHASIS =
528                 link_training_setting->
529                 lane_settings[0].PRE_EMPHASIS;
530         /*max_requested.postCursor2 =
531          * link_training_setting->laneSettings[0].postCursor2;*/
532
533         /* Determine what the maximum of the requested settings are*/
534         for (lane = 1; lane < link_training_setting->link_settings.lane_count;
535                         lane++) {
536                 if (link_training_setting->lane_settings[lane].VOLTAGE_SWING >
537                         max_requested.VOLTAGE_SWING)
538
539                         max_requested.VOLTAGE_SWING =
540                         link_training_setting->
541                         lane_settings[lane].VOLTAGE_SWING;
542
543                 if (link_training_setting->lane_settings[lane].PRE_EMPHASIS >
544                                 max_requested.PRE_EMPHASIS)
545                         max_requested.PRE_EMPHASIS =
546                         link_training_setting->
547                         lane_settings[lane].PRE_EMPHASIS;
548
549                 /*
550                 if (link_training_setting->laneSettings[lane].postCursor2 >
551                  max_requested.postCursor2)
552                 {
553                 max_requested.postCursor2 =
554                 link_training_setting->laneSettings[lane].postCursor2;
555                 }
556                 */
557         }
558
559         /* make sure the requested settings are
560          * not higher than maximum settings*/
561         if (max_requested.VOLTAGE_SWING > VOLTAGE_SWING_MAX_LEVEL)
562                 max_requested.VOLTAGE_SWING = VOLTAGE_SWING_MAX_LEVEL;
563
564         if (max_requested.PRE_EMPHASIS > PRE_EMPHASIS_MAX_LEVEL)
565                 max_requested.PRE_EMPHASIS = PRE_EMPHASIS_MAX_LEVEL;
566         /*
567         if (max_requested.postCursor2 > PostCursor2_MaxLevel)
568         max_requested.postCursor2 = PostCursor2_MaxLevel;
569         */
570
571         /* make sure the pre-emphasis matches the voltage swing*/
572         if (max_requested.PRE_EMPHASIS >
573                 get_max_pre_emphasis_for_voltage_swing(
574                         max_requested.VOLTAGE_SWING))
575                 max_requested.PRE_EMPHASIS =
576                 get_max_pre_emphasis_for_voltage_swing(
577                         max_requested.VOLTAGE_SWING);
578
579         /*
580          * Post Cursor2 levels are completely independent from
581          * pre-emphasis (Post Cursor1) levels. But Post Cursor2 levels
582          * can only be applied to each allowable combination of voltage
583          * swing and pre-emphasis levels */
584          /* if ( max_requested.postCursor2 >
585           *  getMaxPostCursor2ForVoltageSwing(max_requested.voltageSwing))
586           *  max_requested.postCursor2 =
587           *  getMaxPostCursor2ForVoltageSwing(max_requested.voltageSwing);
588           */
589
590         max_lt_setting->link_settings.link_rate =
591                 link_training_setting->link_settings.link_rate;
592         max_lt_setting->link_settings.lane_count =
593         link_training_setting->link_settings.lane_count;
594         max_lt_setting->link_settings.link_spread =
595                 link_training_setting->link_settings.link_spread;
596
597         for (lane = 0; lane <
598                 link_training_setting->link_settings.lane_count;
599                 lane++) {
600                 max_lt_setting->lane_settings[lane].VOLTAGE_SWING =
601                         max_requested.VOLTAGE_SWING;
602                 max_lt_setting->lane_settings[lane].PRE_EMPHASIS =
603                         max_requested.PRE_EMPHASIS;
604                 /*max_lt_setting->laneSettings[lane].postCursor2 =
605                  * max_requested.postCursor2;
606                  */
607         }
608
609 }
610
611 enum dc_status dp_get_lane_status_and_drive_settings(
612         struct dc_link *link,
613         const struct link_training_settings *link_training_setting,
614         union lane_status *ln_status,
615         union lane_align_status_updated *ln_status_updated,
616         struct link_training_settings *req_settings,
617         uint32_t offset)
618 {
619         unsigned int lane01_status_address = DP_LANE0_1_STATUS;
620         uint8_t lane_adjust_offset = 4;
621         unsigned int lane01_adjust_address;
622         uint8_t dpcd_buf[6] = {0};
623         union lane_adjust dpcd_lane_adjust[LANE_COUNT_DP_MAX] = { { {0} } };
624         struct link_training_settings request_settings = { {0} };
625         uint32_t lane;
626         enum dc_status status;
627
628         memset(req_settings, '\0', sizeof(struct link_training_settings));
629
630         if (is_repeater(link, offset)) {
631                 lane01_status_address =
632                                 DP_LANE0_1_STATUS_PHY_REPEATER1 +
633                                 ((DP_REPEATER_CONFIGURATION_AND_STATUS_SIZE) * (offset - 1));
634                 lane_adjust_offset = 3;
635         }
636
637         status = core_link_read_dpcd(
638                 link,
639                 lane01_status_address,
640                 (uint8_t *)(dpcd_buf),
641                 sizeof(dpcd_buf));
642
643         for (lane = 0; lane <
644                 (uint32_t)(link_training_setting->link_settings.lane_count);
645                 lane++) {
646
647                 ln_status[lane].raw =
648                         get_nibble_at_index(&dpcd_buf[0], lane);
649                 dpcd_lane_adjust[lane].raw =
650                         get_nibble_at_index(&dpcd_buf[lane_adjust_offset], lane);
651         }
652
653         ln_status_updated->raw = dpcd_buf[2];
654
655         if (is_repeater(link, offset)) {
656                 DC_LOG_HW_LINK_TRAINING("%s:\n LTTPR Repeater ID: %d\n"
657                                 " 0x%X Lane01Status = %x\n 0x%X Lane23Status = %x\n ",
658                         __func__,
659                         offset,
660                         lane01_status_address, dpcd_buf[0],
661                         lane01_status_address + 1, dpcd_buf[1]);
662         } else {
663                 DC_LOG_HW_LINK_TRAINING("%s:\n 0x%X Lane01Status = %x\n 0x%X Lane23Status = %x\n ",
664                         __func__,
665                         lane01_status_address, dpcd_buf[0],
666                         lane01_status_address + 1, dpcd_buf[1]);
667         }
668         lane01_adjust_address = DP_ADJUST_REQUEST_LANE0_1;
669
670         if (is_repeater(link, offset))
671                 lane01_adjust_address = DP_ADJUST_REQUEST_LANE0_1_PHY_REPEATER1 +
672                                 ((DP_REPEATER_CONFIGURATION_AND_STATUS_SIZE) * (offset - 1));
673
674         if (is_repeater(link, offset)) {
675                 DC_LOG_HW_LINK_TRAINING("%s:\n LTTPR Repeater ID: %d\n"
676                                 " 0x%X Lane01AdjustRequest = %x\n 0x%X Lane23AdjustRequest = %x\n",
677                                         __func__,
678                                         offset,
679                                         lane01_adjust_address,
680                                         dpcd_buf[lane_adjust_offset],
681                                         lane01_adjust_address + 1,
682                                         dpcd_buf[lane_adjust_offset + 1]);
683         } else {
684                 DC_LOG_HW_LINK_TRAINING("%s:\n 0x%X Lane01AdjustRequest = %x\n 0x%X Lane23AdjustRequest = %x\n",
685                         __func__,
686                         lane01_adjust_address,
687                         dpcd_buf[lane_adjust_offset],
688                         lane01_adjust_address + 1,
689                         dpcd_buf[lane_adjust_offset + 1]);
690         }
691
692         /*copy to req_settings*/
693         request_settings.link_settings.lane_count =
694                 link_training_setting->link_settings.lane_count;
695         request_settings.link_settings.link_rate =
696                 link_training_setting->link_settings.link_rate;
697         request_settings.link_settings.link_spread =
698                 link_training_setting->link_settings.link_spread;
699
700         for (lane = 0; lane <
701                 (uint32_t)(link_training_setting->link_settings.lane_count);
702                 lane++) {
703
704                 request_settings.lane_settings[lane].VOLTAGE_SWING =
705                         (enum dc_voltage_swing)(dpcd_lane_adjust[lane].bits.
706                                 VOLTAGE_SWING_LANE);
707                 request_settings.lane_settings[lane].PRE_EMPHASIS =
708                         (enum dc_pre_emphasis)(dpcd_lane_adjust[lane].bits.
709                                 PRE_EMPHASIS_LANE);
710         }
711
712         /*Note: for postcursor2, read adjusted
713          * postcursor2 settings from*/
714         /*DpcdAddress_AdjustRequestPostCursor2 =
715          *0x020C (not implemented yet)*/
716
717         /* we find the maximum of the requested settings across all lanes*/
718         /* and set this maximum for all lanes*/
719         find_max_drive_settings(&request_settings, req_settings);
720
721         /* if post cursor 2 is needed in the future,
722          * read DpcdAddress_AdjustRequestPostCursor2 = 0x020C
723          */
724
725         return status;
726 }
727
728 enum dc_status dpcd_set_lane_settings(
729         struct dc_link *link,
730         const struct link_training_settings *link_training_setting,
731         uint32_t offset)
732 {
733         union dpcd_training_lane dpcd_lane[LANE_COUNT_DP_MAX] = {{{0}}};
734         uint32_t lane;
735         unsigned int lane0_set_address;
736         enum dc_status status;
737
738         lane0_set_address = DP_TRAINING_LANE0_SET;
739
740         if (is_repeater(link, offset))
741                 lane0_set_address = DP_TRAINING_LANE0_SET_PHY_REPEATER1 +
742                 ((DP_REPEATER_CONFIGURATION_AND_STATUS_SIZE) * (offset - 1));
743
744         for (lane = 0; lane <
745                 (uint32_t)(link_training_setting->
746                 link_settings.lane_count);
747                 lane++) {
748                 dpcd_lane[lane].bits.VOLTAGE_SWING_SET =
749                         (uint8_t)(link_training_setting->
750                         lane_settings[lane].VOLTAGE_SWING);
751                 dpcd_lane[lane].bits.PRE_EMPHASIS_SET =
752                         (uint8_t)(link_training_setting->
753                         lane_settings[lane].PRE_EMPHASIS);
754                 dpcd_lane[lane].bits.MAX_SWING_REACHED =
755                         (link_training_setting->
756                         lane_settings[lane].VOLTAGE_SWING ==
757                         VOLTAGE_SWING_MAX_LEVEL ? 1 : 0);
758                 dpcd_lane[lane].bits.MAX_PRE_EMPHASIS_REACHED =
759                         (link_training_setting->
760                         lane_settings[lane].PRE_EMPHASIS ==
761                         PRE_EMPHASIS_MAX_LEVEL ? 1 : 0);
762         }
763
764         status = core_link_write_dpcd(link,
765                 lane0_set_address,
766                 (uint8_t *)(dpcd_lane),
767                 link_training_setting->link_settings.lane_count);
768
769         /*
770         if (LTSettings.link.rate == LinkRate_High2)
771         {
772                 DpcdTrainingLaneSet2 dpcd_lane2[lane_count_DPMax] = {0};
773                 for ( uint32_t lane = 0;
774                 lane < lane_count_DPMax; lane++)
775                 {
776                         dpcd_lane2[lane].bits.post_cursor2_set =
777                         static_cast<unsigned char>(
778                         LTSettings.laneSettings[lane].postCursor2);
779                         dpcd_lane2[lane].bits.max_post_cursor2_reached = 0;
780                 }
781                 m_pDpcdAccessSrv->WriteDpcdData(
782                 DpcdAddress_Lane0Set2,
783                 reinterpret_cast<unsigned char*>(dpcd_lane2),
784                 LTSettings.link.lanes);
785         }
786         */
787
788         if (is_repeater(link, offset)) {
789                 DC_LOG_HW_LINK_TRAINING("%s\n LTTPR Repeater ID: %d\n"
790                                 " 0x%X VS set = %x  PE set = %x max VS Reached = %x  max PE Reached = %x\n",
791                         __func__,
792                         offset,
793                         lane0_set_address,
794                         dpcd_lane[0].bits.VOLTAGE_SWING_SET,
795                         dpcd_lane[0].bits.PRE_EMPHASIS_SET,
796                         dpcd_lane[0].bits.MAX_SWING_REACHED,
797                         dpcd_lane[0].bits.MAX_PRE_EMPHASIS_REACHED);
798
799         } else {
800                 DC_LOG_HW_LINK_TRAINING("%s\n 0x%X VS set = %x  PE set = %x max VS Reached = %x  max PE Reached = %x\n",
801                         __func__,
802                         lane0_set_address,
803                         dpcd_lane[0].bits.VOLTAGE_SWING_SET,
804                         dpcd_lane[0].bits.PRE_EMPHASIS_SET,
805                         dpcd_lane[0].bits.MAX_SWING_REACHED,
806                         dpcd_lane[0].bits.MAX_PRE_EMPHASIS_REACHED);
807         }
808         link->cur_lane_setting = link_training_setting->lane_settings[0];
809
810         return status;
811 }
812
813 bool dp_is_max_vs_reached(
814         const struct link_training_settings *lt_settings)
815 {
816         uint32_t lane;
817         for (lane = 0; lane <
818                 (uint32_t)(lt_settings->link_settings.lane_count);
819                 lane++) {
820                 if (lt_settings->lane_settings[lane].VOLTAGE_SWING
821                         == VOLTAGE_SWING_MAX_LEVEL)
822                         return true;
823         }
824         return false;
825
826 }
827
828 static bool perform_post_lt_adj_req_sequence(
829         struct dc_link *link,
830         struct link_training_settings *lt_settings)
831 {
832         enum dc_lane_count lane_count =
833         lt_settings->link_settings.lane_count;
834
835         uint32_t adj_req_count;
836         uint32_t adj_req_timer;
837         bool req_drv_setting_changed;
838         uint32_t lane;
839
840         req_drv_setting_changed = false;
841         for (adj_req_count = 0; adj_req_count < POST_LT_ADJ_REQ_LIMIT;
842         adj_req_count++) {
843
844                 req_drv_setting_changed = false;
845
846                 for (adj_req_timer = 0;
847                         adj_req_timer < POST_LT_ADJ_REQ_TIMEOUT;
848                         adj_req_timer++) {
849
850                         struct link_training_settings req_settings;
851                         union lane_status dpcd_lane_status[LANE_COUNT_DP_MAX];
852                         union lane_align_status_updated
853                                 dpcd_lane_status_updated;
854
855                         dp_get_lane_status_and_drive_settings(
856                                 link,
857                                 lt_settings,
858                                 dpcd_lane_status,
859                                 &dpcd_lane_status_updated,
860                                 &req_settings,
861                                 DPRX);
862
863                         if (dpcd_lane_status_updated.bits.
864                                         POST_LT_ADJ_REQ_IN_PROGRESS == 0)
865                                 return true;
866
867                         if (!dp_is_cr_done(lane_count, dpcd_lane_status))
868                                 return false;
869
870                         if (!dp_is_ch_eq_done(lane_count, dpcd_lane_status) ||
871                                         !dp_is_symbol_locked(lane_count, dpcd_lane_status) ||
872                                         !dp_is_interlane_aligned(dpcd_lane_status_updated))
873                                 return false;
874
875                         for (lane = 0; lane < (uint32_t)(lane_count); lane++) {
876
877                                 if (lt_settings->
878                                 lane_settings[lane].VOLTAGE_SWING !=
879                                 req_settings.lane_settings[lane].
880                                 VOLTAGE_SWING ||
881                                 lt_settings->lane_settings[lane].PRE_EMPHASIS !=
882                                 req_settings.lane_settings[lane].PRE_EMPHASIS) {
883
884                                         req_drv_setting_changed = true;
885                                         break;
886                                 }
887                         }
888
889                         if (req_drv_setting_changed) {
890                                 dp_update_drive_settings(
891                                         lt_settings, req_settings);
892
893                                 dc_link_dp_set_drive_settings(link,
894                                                 lt_settings);
895                                 break;
896                         }
897
898                         msleep(1);
899                 }
900
901                 if (!req_drv_setting_changed) {
902                         DC_LOG_WARNING("%s: Post Link Training Adjust Request Timed out\n",
903                                 __func__);
904
905                         ASSERT(0);
906                         return true;
907                 }
908         }
909         DC_LOG_WARNING("%s: Post Link Training Adjust Request limit reached\n",
910                 __func__);
911
912         ASSERT(0);
913         return true;
914
915 }
916
917 /* Only used for channel equalization */
918 uint32_t dp_translate_training_aux_read_interval(uint32_t dpcd_aux_read_interval)
919 {
920         unsigned int aux_rd_interval_us = 400;
921
922         switch (dpcd_aux_read_interval) {
923         case 0x01:
924                 aux_rd_interval_us = 4000;
925                 break;
926         case 0x02:
927                 aux_rd_interval_us = 8000;
928                 break;
929         case 0x03:
930                 aux_rd_interval_us = 12000;
931                 break;
932         case 0x04:
933                 aux_rd_interval_us = 16000;
934                 break;
935         default:
936                 break;
937         }
938
939         return aux_rd_interval_us;
940 }
941
942 enum link_training_result dp_get_cr_failure(enum dc_lane_count ln_count,
943                                         union lane_status *dpcd_lane_status)
944 {
945         enum link_training_result result = LINK_TRAINING_SUCCESS;
946
947         if (ln_count >= LANE_COUNT_ONE && !dpcd_lane_status[0].bits.CR_DONE_0)
948                 result = LINK_TRAINING_CR_FAIL_LANE0;
949         else if (ln_count >= LANE_COUNT_TWO && !dpcd_lane_status[1].bits.CR_DONE_0)
950                 result = LINK_TRAINING_CR_FAIL_LANE1;
951         else if (ln_count >= LANE_COUNT_FOUR && !dpcd_lane_status[2].bits.CR_DONE_0)
952                 result = LINK_TRAINING_CR_FAIL_LANE23;
953         else if (ln_count >= LANE_COUNT_FOUR && !dpcd_lane_status[3].bits.CR_DONE_0)
954                 result = LINK_TRAINING_CR_FAIL_LANE23;
955         return result;
956 }
957
958 static enum link_training_result perform_channel_equalization_sequence(
959         struct dc_link *link,
960         struct link_training_settings *lt_settings,
961         uint32_t offset)
962 {
963         struct link_training_settings req_settings;
964         enum dc_dp_training_pattern tr_pattern;
965         uint32_t retries_ch_eq;
966         uint32_t wait_time_microsec;
967         enum dc_lane_count lane_count = lt_settings->link_settings.lane_count;
968         union lane_align_status_updated dpcd_lane_status_updated = { {0} };
969         union lane_status dpcd_lane_status[LANE_COUNT_DP_MAX] = { { {0} } };
970
971         /* Note: also check that TPS4 is a supported feature*/
972
973         tr_pattern = lt_settings->pattern_for_eq;
974
975         if (is_repeater(link, offset))
976                 tr_pattern = DP_TRAINING_PATTERN_SEQUENCE_4;
977
978         dp_set_hw_training_pattern(link, tr_pattern, offset);
979
980         for (retries_ch_eq = 0; retries_ch_eq <= LINK_TRAINING_MAX_RETRY_COUNT;
981                 retries_ch_eq++) {
982
983                 dp_set_hw_lane_settings(link, lt_settings, offset);
984
985                 /* 2. update DPCD*/
986                 if (!retries_ch_eq)
987                         /* EPR #361076 - write as a 5-byte burst,
988                          * but only for the 1-st iteration
989                          */
990
991                         dpcd_set_lt_pattern_and_lane_settings(
992                                 link,
993                                 lt_settings,
994                                 tr_pattern, offset);
995                 else
996                         dpcd_set_lane_settings(link, lt_settings, offset);
997
998                 /* 3. wait for receiver to lock-on*/
999                 wait_time_microsec = lt_settings->eq_pattern_time;
1000
1001                 if (is_repeater(link, offset))
1002                         wait_time_microsec =
1003                                         dp_translate_training_aux_read_interval(
1004                                                 link->dpcd_caps.lttpr_caps.aux_rd_interval[offset - 1]);
1005
1006                 dp_wait_for_training_aux_rd_interval(
1007                                 link,
1008                                 wait_time_microsec);
1009
1010                 /* 4. Read lane status and requested
1011                  * drive settings as set by the sink*/
1012
1013                 dp_get_lane_status_and_drive_settings(
1014                         link,
1015                         lt_settings,
1016                         dpcd_lane_status,
1017                         &dpcd_lane_status_updated,
1018                         &req_settings,
1019                         offset);
1020
1021                 /* 5. check CR done*/
1022                 if (!dp_is_cr_done(lane_count, dpcd_lane_status))
1023                         return LINK_TRAINING_EQ_FAIL_CR;
1024
1025                 /* 6. check CHEQ done*/
1026                 if (dp_is_ch_eq_done(lane_count, dpcd_lane_status) &&
1027                                 dp_is_symbol_locked(lane_count, dpcd_lane_status) &&
1028                                 dp_is_interlane_aligned(dpcd_lane_status_updated))
1029                         return LINK_TRAINING_SUCCESS;
1030
1031                 /* 7. update VS/PE/PC2 in lt_settings*/
1032                 dp_update_drive_settings(lt_settings, req_settings);
1033         }
1034
1035         return LINK_TRAINING_EQ_FAIL_EQ;
1036
1037 }
1038
1039 static void start_clock_recovery_pattern_early(struct dc_link *link,
1040                 struct link_training_settings *lt_settings,
1041                 uint32_t offset)
1042 {
1043         DC_LOG_HW_LINK_TRAINING("%s\n GPU sends TPS1. Wait 400us.\n",
1044                         __func__);
1045         dp_set_hw_training_pattern(link, lt_settings->pattern_for_cr, offset);
1046         dp_set_hw_lane_settings(link, lt_settings, offset);
1047         udelay(400);
1048 }
1049
1050 static enum link_training_result perform_clock_recovery_sequence(
1051         struct dc_link *link,
1052         struct link_training_settings *lt_settings,
1053         uint32_t offset)
1054 {
1055         uint32_t retries_cr;
1056         uint32_t retry_count;
1057         uint32_t wait_time_microsec;
1058         struct link_training_settings req_settings;
1059         enum dc_lane_count lane_count = lt_settings->link_settings.lane_count;
1060         union lane_status dpcd_lane_status[LANE_COUNT_DP_MAX];
1061         union lane_align_status_updated dpcd_lane_status_updated;
1062
1063         retries_cr = 0;
1064         retry_count = 0;
1065
1066         if (!link->ctx->dc->work_arounds.lt_early_cr_pattern)
1067                 dp_set_hw_training_pattern(link, lt_settings->pattern_for_cr, offset);
1068
1069         /* najeeb - The synaptics MST hub can put the LT in
1070         * infinite loop by switching the VS
1071         */
1072         /* between level 0 and level 1 continuously, here
1073         * we try for CR lock for LinkTrainingMaxCRRetry count*/
1074         while ((retries_cr < LINK_TRAINING_MAX_RETRY_COUNT) &&
1075                 (retry_count < LINK_TRAINING_MAX_CR_RETRY)) {
1076
1077                 memset(&dpcd_lane_status, '\0', sizeof(dpcd_lane_status));
1078                 memset(&dpcd_lane_status_updated, '\0',
1079                 sizeof(dpcd_lane_status_updated));
1080
1081                 /* 1. call HWSS to set lane settings*/
1082                 dp_set_hw_lane_settings(
1083                                 link,
1084                                 lt_settings,
1085                                 offset);
1086
1087                 /* 2. update DPCD of the receiver*/
1088                 if (!retry_count)
1089                         /* EPR #361076 - write as a 5-byte burst,
1090                          * but only for the 1-st iteration.*/
1091                         dpcd_set_lt_pattern_and_lane_settings(
1092                                         link,
1093                                         lt_settings,
1094                                         lt_settings->pattern_for_cr,
1095                                         offset);
1096                 else
1097                         dpcd_set_lane_settings(
1098                                         link,
1099                                         lt_settings,
1100                                         offset);
1101
1102                 /* 3. wait receiver to lock-on*/
1103                 wait_time_microsec = lt_settings->cr_pattern_time;
1104
1105                 if (link->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT)
1106                         wait_time_microsec = TRAINING_AUX_RD_INTERVAL;
1107
1108                 dp_wait_for_training_aux_rd_interval(
1109                                 link,
1110                                 wait_time_microsec);
1111
1112                 /* 4. Read lane status and requested drive
1113                 * settings as set by the sink
1114                 */
1115                 dp_get_lane_status_and_drive_settings(
1116                                 link,
1117                                 lt_settings,
1118                                 dpcd_lane_status,
1119                                 &dpcd_lane_status_updated,
1120                                 &req_settings,
1121                                 offset);
1122
1123                 /* 5. check CR done*/
1124                 if (dp_is_cr_done(lane_count, dpcd_lane_status))
1125                         return LINK_TRAINING_SUCCESS;
1126
1127                 /* 6. max VS reached*/
1128                 if (dp_is_max_vs_reached(lt_settings))
1129                         break;
1130
1131                 /* 7. same lane settings*/
1132                 /* Note: settings are the same for all lanes,
1133                  * so comparing first lane is sufficient*/
1134                 if ((lt_settings->lane_settings[0].VOLTAGE_SWING ==
1135                         req_settings.lane_settings[0].VOLTAGE_SWING)
1136                         && (lt_settings->lane_settings[0].PRE_EMPHASIS ==
1137                                 req_settings.lane_settings[0].PRE_EMPHASIS))
1138                         retries_cr++;
1139                 else
1140                         retries_cr = 0;
1141
1142                 /* 8. update VS/PE/PC2 in lt_settings*/
1143                 dp_update_drive_settings(lt_settings, req_settings);
1144
1145                 retry_count++;
1146         }
1147
1148         if (retry_count >= LINK_TRAINING_MAX_CR_RETRY) {
1149                 ASSERT(0);
1150                 DC_LOG_ERROR("%s: Link Training Error, could not get CR after %d tries. Possibly voltage swing issue",
1151                         __func__,
1152                         LINK_TRAINING_MAX_CR_RETRY);
1153
1154         }
1155
1156         return dp_get_cr_failure(lane_count, dpcd_lane_status);
1157 }
1158
1159 static inline enum link_training_result dp_transition_to_video_idle(
1160         struct dc_link *link,
1161         struct link_training_settings *lt_settings,
1162         enum link_training_result status)
1163 {
1164         union lane_count_set lane_count_set = { {0} };
1165
1166         /* 4. mainlink output idle pattern*/
1167         dp_set_hw_test_pattern(link, DP_TEST_PATTERN_VIDEO_MODE, NULL, 0);
1168
1169         /*
1170          * 5. post training adjust if required
1171          * If the upstream DPTX and downstream DPRX both support TPS4,
1172          * TPS4 must be used instead of POST_LT_ADJ_REQ.
1173          */
1174         if (link->dpcd_caps.max_ln_count.bits.POST_LT_ADJ_REQ_SUPPORTED != 1 ||
1175                         lt_settings->pattern_for_eq == DP_TRAINING_PATTERN_SEQUENCE_4) {
1176                 /* delay 5ms after Main Link output idle pattern and then check
1177                  * DPCD 0202h.
1178                  */
1179                 if (link->connector_signal != SIGNAL_TYPE_EDP && status == LINK_TRAINING_SUCCESS) {
1180                         msleep(5);
1181                         status = dp_check_link_loss_status(link, lt_settings);
1182                 }
1183                 return status;
1184         }
1185
1186         if (status == LINK_TRAINING_SUCCESS &&
1187                 perform_post_lt_adj_req_sequence(link, lt_settings) == false)
1188                 status = LINK_TRAINING_LQA_FAIL;
1189
1190         lane_count_set.bits.LANE_COUNT_SET = lt_settings->link_settings.lane_count;
1191         lane_count_set.bits.ENHANCED_FRAMING = lt_settings->enhanced_framing;
1192         lane_count_set.bits.POST_LT_ADJ_REQ_GRANTED = 0;
1193
1194         core_link_write_dpcd(
1195                 link,
1196                 DP_LANE_COUNT_SET,
1197                 &lane_count_set.raw,
1198                 sizeof(lane_count_set));
1199
1200         return status;
1201 }
1202
1203 enum link_training_result dp_check_link_loss_status(
1204         struct dc_link *link,
1205         const struct link_training_settings *link_training_setting)
1206 {
1207         enum link_training_result status = LINK_TRAINING_SUCCESS;
1208         union lane_status lane_status;
1209         uint8_t dpcd_buf[6] = {0};
1210         uint32_t lane;
1211
1212         core_link_read_dpcd(
1213                         link,
1214                         DP_SINK_COUNT,
1215                         (uint8_t *)(dpcd_buf),
1216                         sizeof(dpcd_buf));
1217
1218         /*parse lane status*/
1219         for (lane = 0; lane < link->cur_link_settings.lane_count; lane++) {
1220                 /*
1221                  * check lanes status
1222                  */
1223                 lane_status.raw = get_nibble_at_index(&dpcd_buf[2], lane);
1224
1225                 if (!lane_status.bits.CHANNEL_EQ_DONE_0 ||
1226                         !lane_status.bits.CR_DONE_0 ||
1227                         !lane_status.bits.SYMBOL_LOCKED_0) {
1228                         /* if one of the channel equalization, clock
1229                          * recovery or symbol lock is dropped
1230                          * consider it as (link has been
1231                          * dropped) dp sink status has changed
1232                          */
1233                         status = LINK_TRAINING_LINK_LOSS;
1234                         break;
1235                 }
1236         }
1237
1238         return status;
1239 }
1240
1241 static inline void decide_8b_10b_training_settings(
1242          struct dc_link *link,
1243         const struct dc_link_settings *link_setting,
1244         const struct dc_link_training_overrides *overrides,
1245         struct link_training_settings *lt_settings)
1246 {
1247         uint32_t lane;
1248
1249         memset(lt_settings, '\0', sizeof(struct link_training_settings));
1250
1251         /* Initialize link settings */
1252         lt_settings->link_settings.use_link_rate_set = link_setting->use_link_rate_set;
1253         lt_settings->link_settings.link_rate_set = link_setting->link_rate_set;
1254
1255         if (link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN)
1256                 lt_settings->link_settings.link_rate = link->preferred_link_setting.link_rate;
1257         else
1258                 lt_settings->link_settings.link_rate = link_setting->link_rate;
1259
1260         if (link->preferred_link_setting.lane_count != LANE_COUNT_UNKNOWN)
1261                 lt_settings->link_settings.lane_count = link->preferred_link_setting.lane_count;
1262         else
1263                 lt_settings->link_settings.lane_count = link_setting->lane_count;
1264
1265         /*@todo[vdevulap] move SS to LS, should not be handled by displaypath*/
1266
1267         /* TODO hard coded to SS for now
1268          * lt_settings.link_settings.link_spread =
1269          * dal_display_path_is_ss_supported(
1270          * path_mode->display_path) ?
1271          * LINK_SPREAD_05_DOWNSPREAD_30KHZ :
1272          * LINK_SPREAD_DISABLED;
1273          */
1274         /* Initialize link spread */
1275         if (link->dp_ss_off)
1276                 lt_settings->link_settings.link_spread = LINK_SPREAD_DISABLED;
1277         else if (overrides->downspread != NULL)
1278                 lt_settings->link_settings.link_spread
1279                         = *overrides->downspread
1280                         ? LINK_SPREAD_05_DOWNSPREAD_30KHZ
1281                         : LINK_SPREAD_DISABLED;
1282         else
1283                 lt_settings->link_settings.link_spread = LINK_SPREAD_05_DOWNSPREAD_30KHZ;
1284
1285         lt_settings->lttpr_mode = link->lttpr_mode;
1286
1287         /* Initialize lane settings overrides */
1288         if (overrides->voltage_swing != NULL)
1289                 lt_settings->voltage_swing = overrides->voltage_swing;
1290
1291         if (overrides->pre_emphasis != NULL)
1292                 lt_settings->pre_emphasis = overrides->pre_emphasis;
1293
1294         if (overrides->post_cursor2 != NULL)
1295                 lt_settings->post_cursor2 = overrides->post_cursor2;
1296
1297         /* Initialize lane settings (VS/PE/PC2) */
1298         for (lane = 0; lane < LANE_COUNT_DP_MAX; lane++) {
1299                 lt_settings->lane_settings[lane].VOLTAGE_SWING =
1300                         lt_settings->voltage_swing != NULL ?
1301                         *lt_settings->voltage_swing :
1302                         VOLTAGE_SWING_LEVEL0;
1303                 lt_settings->lane_settings[lane].PRE_EMPHASIS =
1304                         lt_settings->pre_emphasis != NULL ?
1305                         *lt_settings->pre_emphasis
1306                         : PRE_EMPHASIS_DISABLED;
1307                 lt_settings->lane_settings[lane].POST_CURSOR2 =
1308                         lt_settings->post_cursor2 != NULL ?
1309                         *lt_settings->post_cursor2
1310                         : POST_CURSOR2_DISABLED;
1311         }
1312
1313         /* Initialize training timings */
1314         if (overrides->cr_pattern_time != NULL)
1315                 lt_settings->cr_pattern_time = *overrides->cr_pattern_time;
1316         else
1317                 lt_settings->cr_pattern_time = get_cr_training_aux_rd_interval(link, link_setting);
1318
1319         if (overrides->eq_pattern_time != NULL)
1320                 lt_settings->eq_pattern_time = *overrides->eq_pattern_time;
1321         else
1322                 lt_settings->eq_pattern_time = get_eq_training_aux_rd_interval(link, link_setting);
1323
1324         if (overrides->pattern_for_cr != NULL)
1325                 lt_settings->pattern_for_cr = *overrides->pattern_for_cr;
1326         else
1327                 lt_settings->pattern_for_cr = decide_cr_training_pattern(link_setting);
1328         if (overrides->pattern_for_eq != NULL)
1329                 lt_settings->pattern_for_eq = *overrides->pattern_for_eq;
1330         else
1331                 lt_settings->pattern_for_eq = decide_eq_training_pattern(link, link_setting);
1332
1333         if (overrides->enhanced_framing != NULL)
1334                 lt_settings->enhanced_framing = *overrides->enhanced_framing;
1335         else
1336                 lt_settings->enhanced_framing = 1;
1337
1338         if (link->preferred_training_settings.fec_enable != NULL)
1339                 lt_settings->should_set_fec_ready = *link->preferred_training_settings.fec_enable;
1340         else
1341                 lt_settings->should_set_fec_ready = true;
1342 }
1343
1344 void dp_decide_training_settings(
1345                 struct dc_link *link,
1346                 const struct dc_link_settings *link_settings,
1347                 const struct dc_link_training_overrides *overrides,
1348                 struct link_training_settings *lt_settings)
1349 {
1350         if (dp_get_link_encoding_format(link_settings) == DP_8b_10b_ENCODING)
1351                 decide_8b_10b_training_settings(link, link_settings, overrides, lt_settings);
1352 }
1353
1354
1355 uint8_t dp_convert_to_count(uint8_t lttpr_repeater_count)
1356 {
1357         switch (lttpr_repeater_count) {
1358         case 0x80: // 1 lttpr repeater
1359                 return 1;
1360         case 0x40: // 2 lttpr repeaters
1361                 return 2;
1362         case 0x20: // 3 lttpr repeaters
1363                 return 3;
1364         case 0x10: // 4 lttpr repeaters
1365                 return 4;
1366         case 0x08: // 5 lttpr repeaters
1367                 return 5;
1368         case 0x04: // 6 lttpr repeaters
1369                 return 6;
1370         case 0x02: // 7 lttpr repeaters
1371                 return 7;
1372         case 0x01: // 8 lttpr repeaters
1373                 return 8;
1374         default:
1375                 break;
1376         }
1377         return 0; // invalid value
1378 }
1379
1380 enum dc_status configure_lttpr_mode_transparent(struct dc_link *link)
1381 {
1382         uint8_t repeater_mode = DP_PHY_REPEATER_MODE_TRANSPARENT;
1383
1384         DC_LOG_HW_LINK_TRAINING("%s\n Set LTTPR to Transparent Mode\n", __func__);
1385         return core_link_write_dpcd(link,
1386                         DP_PHY_REPEATER_MODE,
1387                         (uint8_t *)&repeater_mode,
1388                         sizeof(repeater_mode));
1389 }
1390
1391 enum dc_status configure_lttpr_mode_non_transparent(
1392                 struct dc_link *link,
1393                 const struct link_training_settings *lt_settings)
1394 {
1395         /* aux timeout is already set to extended */
1396         /* RESET/SET lttpr mode to enable non transparent mode */
1397         uint8_t repeater_cnt;
1398         uint32_t aux_interval_address;
1399         uint8_t repeater_id;
1400         enum dc_status result = DC_ERROR_UNEXPECTED;
1401         uint8_t repeater_mode = DP_PHY_REPEATER_MODE_TRANSPARENT;
1402
1403         enum dp_link_encoding encoding = dp_get_link_encoding_format(&lt_settings->link_settings);
1404
1405         if (encoding == DP_8b_10b_ENCODING) {
1406                 DC_LOG_HW_LINK_TRAINING("%s\n Set LTTPR to Transparent Mode\n", __func__);
1407                 result = core_link_write_dpcd(link,
1408                                 DP_PHY_REPEATER_MODE,
1409                                 (uint8_t *)&repeater_mode,
1410                                 sizeof(repeater_mode));
1411
1412         }
1413
1414         if (result == DC_OK) {
1415                 link->dpcd_caps.lttpr_caps.mode = repeater_mode;
1416         }
1417
1418         if (link->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT) {
1419
1420                 DC_LOG_HW_LINK_TRAINING("%s\n Set LTTPR to Non Transparent Mode\n", __func__);
1421
1422                 repeater_mode = DP_PHY_REPEATER_MODE_NON_TRANSPARENT;
1423                 result = core_link_write_dpcd(link,
1424                                 DP_PHY_REPEATER_MODE,
1425                                 (uint8_t *)&repeater_mode,
1426                                 sizeof(repeater_mode));
1427
1428                 if (result == DC_OK) {
1429                         link->dpcd_caps.lttpr_caps.mode = repeater_mode;
1430                 }
1431
1432                 if (encoding == DP_8b_10b_ENCODING) {
1433                         repeater_cnt = dp_convert_to_count(link->dpcd_caps.lttpr_caps.phy_repeater_cnt);
1434                         for (repeater_id = repeater_cnt; repeater_id > 0; repeater_id--) {
1435                                 aux_interval_address = DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1 +
1436                                                         ((DP_REPEATER_CONFIGURATION_AND_STATUS_SIZE) * (repeater_id - 1));
1437                                 core_link_read_dpcd(
1438                                         link,
1439                                         aux_interval_address,
1440                                         (uint8_t *)&link->dpcd_caps.lttpr_caps.aux_rd_interval[repeater_id - 1],
1441                                         sizeof(link->dpcd_caps.lttpr_caps.aux_rd_interval[repeater_id - 1]));
1442                                 link->dpcd_caps.lttpr_caps.aux_rd_interval[repeater_id - 1] &= 0x7F;
1443                         }
1444                 }
1445         }
1446
1447         return result;
1448 }
1449
1450 static void repeater_training_done(struct dc_link *link, uint32_t offset)
1451 {
1452         union dpcd_training_pattern dpcd_pattern = { {0} };
1453
1454         const uint32_t dpcd_base_lt_offset =
1455                         DP_TRAINING_PATTERN_SET_PHY_REPEATER1 +
1456                                 ((DP_REPEATER_CONFIGURATION_AND_STATUS_SIZE) * (offset - 1));
1457         /* Set training not in progress*/
1458         dpcd_pattern.v1_4.TRAINING_PATTERN_SET = DPCD_TRAINING_PATTERN_VIDEOIDLE;
1459
1460         core_link_write_dpcd(
1461                 link,
1462                 dpcd_base_lt_offset,
1463                 &dpcd_pattern.raw,
1464                 1);
1465
1466         DC_LOG_HW_LINK_TRAINING("%s\n LTTPR Id: %d 0x%X pattern = %x\n",
1467                 __func__,
1468                 offset,
1469                 dpcd_base_lt_offset,
1470                 dpcd_pattern.v1_4.TRAINING_PATTERN_SET);
1471 }
1472
1473 static void print_status_message(
1474         struct dc_link *link,
1475         const struct link_training_settings *lt_settings,
1476         enum link_training_result status)
1477 {
1478         char *link_rate = "Unknown";
1479         char *lt_result = "Unknown";
1480         char *lt_spread = "Disabled";
1481
1482         switch (lt_settings->link_settings.link_rate) {
1483         case LINK_RATE_LOW:
1484                 link_rate = "RBR";
1485                 break;
1486         case LINK_RATE_RATE_2:
1487                 link_rate = "R2";
1488                 break;
1489         case LINK_RATE_RATE_3:
1490                 link_rate = "R3";
1491                 break;
1492         case LINK_RATE_HIGH:
1493                 link_rate = "HBR";
1494                 break;
1495         case LINK_RATE_RBR2:
1496                 link_rate = "RBR2";
1497                 break;
1498         case LINK_RATE_RATE_6:
1499                 link_rate = "R6";
1500                 break;
1501         case LINK_RATE_HIGH2:
1502                 link_rate = "HBR2";
1503                 break;
1504         case LINK_RATE_HIGH3:
1505                 link_rate = "HBR3";
1506                 break;
1507         default:
1508                 break;
1509         }
1510
1511         switch (status) {
1512         case LINK_TRAINING_SUCCESS:
1513                 lt_result = "pass";
1514                 break;
1515         case LINK_TRAINING_CR_FAIL_LANE0:
1516                 lt_result = "CR failed lane0";
1517                 break;
1518         case LINK_TRAINING_CR_FAIL_LANE1:
1519                 lt_result = "CR failed lane1";
1520                 break;
1521         case LINK_TRAINING_CR_FAIL_LANE23:
1522                 lt_result = "CR failed lane23";
1523                 break;
1524         case LINK_TRAINING_EQ_FAIL_CR:
1525                 lt_result = "CR failed in EQ";
1526                 break;
1527         case LINK_TRAINING_EQ_FAIL_EQ:
1528                 lt_result = "EQ failed";
1529                 break;
1530         case LINK_TRAINING_LQA_FAIL:
1531                 lt_result = "LQA failed";
1532                 break;
1533         case LINK_TRAINING_LINK_LOSS:
1534                 lt_result = "Link loss";
1535                 break;
1536         default:
1537                 break;
1538         }
1539
1540         switch (lt_settings->link_settings.link_spread) {
1541         case LINK_SPREAD_DISABLED:
1542                 lt_spread = "Disabled";
1543                 break;
1544         case LINK_SPREAD_05_DOWNSPREAD_30KHZ:
1545                 lt_spread = "0.5% 30KHz";
1546                 break;
1547         case LINK_SPREAD_05_DOWNSPREAD_33KHZ:
1548                 lt_spread = "0.5% 33KHz";
1549                 break;
1550         default:
1551                 break;
1552         }
1553
1554         /* Connectivity log: link training */
1555         CONN_MSG_LT(link, "%sx%d %s VS=%d, PE=%d, DS=%s",
1556                                 link_rate,
1557                                 lt_settings->link_settings.lane_count,
1558                                 lt_result,
1559                                 lt_settings->lane_settings[0].VOLTAGE_SWING,
1560                                 lt_settings->lane_settings[0].PRE_EMPHASIS,
1561                                 lt_spread);
1562 }
1563
1564 void dc_link_dp_set_drive_settings(
1565         struct dc_link *link,
1566         struct link_training_settings *lt_settings)
1567 {
1568         /* program ASIC PHY settings*/
1569         dp_set_hw_lane_settings(link, lt_settings, DPRX);
1570
1571         /* Notify DP sink the PHY settings from source */
1572         dpcd_set_lane_settings(link, lt_settings, DPRX);
1573 }
1574
1575 bool dc_link_dp_perform_link_training_skip_aux(
1576         struct dc_link *link,
1577         const struct dc_link_settings *link_setting)
1578 {
1579         struct link_training_settings lt_settings;
1580
1581         dp_decide_training_settings(
1582                         link,
1583                         link_setting,
1584                         &link->preferred_training_settings,
1585                         &lt_settings);
1586
1587         /* 1. Perform_clock_recovery_sequence. */
1588
1589         /* transmit training pattern for clock recovery */
1590         dp_set_hw_training_pattern(link, lt_settings.pattern_for_cr, DPRX);
1591
1592         /* call HWSS to set lane settings*/
1593         dp_set_hw_lane_settings(link, &lt_settings, DPRX);
1594
1595         /* wait receiver to lock-on*/
1596         dp_wait_for_training_aux_rd_interval(link, lt_settings.cr_pattern_time);
1597
1598         /* 2. Perform_channel_equalization_sequence. */
1599
1600         /* transmit training pattern for channel equalization. */
1601         dp_set_hw_training_pattern(link, lt_settings.pattern_for_eq, DPRX);
1602
1603         /* call HWSS to set lane settings*/
1604         dp_set_hw_lane_settings(link, &lt_settings, DPRX);
1605
1606         /* wait receiver to lock-on. */
1607         dp_wait_for_training_aux_rd_interval(link, lt_settings.eq_pattern_time);
1608
1609         /* 3. Perform_link_training_int. */
1610
1611         /* Mainlink output idle pattern. */
1612         dp_set_hw_test_pattern(link, DP_TEST_PATTERN_VIDEO_MODE, NULL, 0);
1613
1614         print_status_message(link, &lt_settings, LINK_TRAINING_SUCCESS);
1615
1616         return true;
1617 }
1618
1619 enum dc_status dpcd_configure_lttpr_mode(struct dc_link *link, struct link_training_settings *lt_settings)
1620 {
1621         enum dc_status status = DC_OK;
1622
1623         if (lt_settings->lttpr_mode == LTTPR_MODE_TRANSPARENT)
1624                 status = configure_lttpr_mode_transparent(link);
1625
1626         else if (lt_settings->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT)
1627                 status = configure_lttpr_mode_non_transparent(link, lt_settings);
1628
1629         return status;
1630 }
1631
1632 static void dpcd_exit_training_mode(struct dc_link *link)
1633 {
1634
1635         /* clear training pattern set */
1636         dpcd_set_training_pattern(link, DP_TRAINING_PATTERN_VIDEOIDLE);
1637 }
1638
1639 enum dc_status dpcd_configure_channel_coding(struct dc_link *link,
1640                 struct link_training_settings *lt_settings)
1641 {
1642         enum dp_link_encoding encoding =
1643                         dp_get_link_encoding_format(
1644                                         &lt_settings->link_settings);
1645         enum dc_status status;
1646
1647         status = core_link_write_dpcd(
1648                         link,
1649                         DP_MAIN_LINK_CHANNEL_CODING_SET,
1650                         (uint8_t *) &encoding,
1651                         1);
1652         DC_LOG_HW_LINK_TRAINING("%s:\n 0x%X MAIN_LINK_CHANNEL_CODING_SET = %x\n",
1653                                         __func__,
1654                                         DP_MAIN_LINK_CHANNEL_CODING_SET,
1655                                         encoding);
1656
1657         return status;
1658 }
1659
1660 static enum link_training_result dp_perform_8b_10b_link_training(
1661                 struct dc_link *link,
1662                 struct link_training_settings *lt_settings)
1663 {
1664         enum link_training_result status = LINK_TRAINING_SUCCESS;
1665
1666         uint8_t repeater_cnt;
1667         uint8_t repeater_id;
1668         uint8_t lane = 0;
1669
1670         if (link->ctx->dc->work_arounds.lt_early_cr_pattern)
1671                 start_clock_recovery_pattern_early(link, lt_settings, DPRX);
1672
1673         /* 1. set link rate, lane count and spread. */
1674         dpcd_set_link_settings(link, lt_settings);
1675
1676         if (link->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT) {
1677
1678                 /* 2. perform link training (set link training done
1679                  *  to false is done as well)
1680                  */
1681                 repeater_cnt = dp_convert_to_count(link->dpcd_caps.lttpr_caps.phy_repeater_cnt);
1682
1683                 for (repeater_id = repeater_cnt; (repeater_id > 0 && status == LINK_TRAINING_SUCCESS);
1684                                 repeater_id--) {
1685                         status = perform_clock_recovery_sequence(link, lt_settings, repeater_id);
1686
1687                         if (status != LINK_TRAINING_SUCCESS)
1688                                 break;
1689
1690                         status = perform_channel_equalization_sequence(link,
1691                                         lt_settings,
1692                                         repeater_id);
1693
1694                         if (status != LINK_TRAINING_SUCCESS)
1695                                 break;
1696
1697                         repeater_training_done(link, repeater_id);
1698                 }
1699
1700                 for (lane = 0; lane < (uint8_t)lt_settings->link_settings.lane_count; lane++)
1701                         lt_settings->lane_settings[lane].VOLTAGE_SWING = VOLTAGE_SWING_LEVEL0;
1702         }
1703
1704         if (status == LINK_TRAINING_SUCCESS) {
1705                 status = perform_clock_recovery_sequence(link, lt_settings, DPRX);
1706         if (status == LINK_TRAINING_SUCCESS) {
1707                 status = perform_channel_equalization_sequence(link,
1708                                         lt_settings,
1709                                         DPRX);
1710                 }
1711         }
1712
1713         return status;
1714 }
1715
1716 enum link_training_result dc_link_dp_perform_link_training(
1717         struct dc_link *link,
1718         const struct dc_link_settings *link_settings,
1719         bool skip_video_pattern)
1720 {
1721         enum link_training_result status = LINK_TRAINING_SUCCESS;
1722         struct link_training_settings lt_settings;
1723         enum dp_link_encoding encoding =
1724                         dp_get_link_encoding_format(link_settings);
1725
1726         /* decide training settings */
1727         dp_decide_training_settings(
1728                         link,
1729                         link_settings,
1730                         &link->preferred_training_settings,
1731                         &lt_settings);
1732
1733         /* reset previous training states */
1734         dpcd_exit_training_mode(link);
1735
1736         /* configure link prior to entering training mode */
1737         dpcd_configure_lttpr_mode(link, &lt_settings);
1738         dp_set_fec_ready(link, lt_settings.should_set_fec_ready);
1739         dpcd_configure_channel_coding(link, &lt_settings);
1740
1741         /* enter training mode:
1742          * Per DP specs starting from here, DPTX device shall not issue
1743          * Non-LT AUX transactions inside training mode.
1744          */
1745         if (encoding == DP_8b_10b_ENCODING)
1746                 status = dp_perform_8b_10b_link_training(link, &lt_settings);
1747         else
1748                 ASSERT(0);
1749
1750         /* exit training mode and switch to video idle */
1751         dpcd_exit_training_mode(link);
1752         if ((status == LINK_TRAINING_SUCCESS) || !skip_video_pattern)
1753                 status = dp_transition_to_video_idle(link,
1754                                 &lt_settings,
1755                                 status);
1756
1757         /* dump debug data */
1758         print_status_message(link, &lt_settings, status);
1759         if (status != LINK_TRAINING_SUCCESS)
1760                 link->ctx->dc->debug_data.ltFailCount++;
1761         return status;
1762 }
1763
1764 bool perform_link_training_with_retries(
1765         const struct dc_link_settings *link_setting,
1766         bool skip_video_pattern,
1767         int attempts,
1768         struct pipe_ctx *pipe_ctx,
1769         enum signal_type signal,
1770         bool do_fallback)
1771 {
1772         int j;
1773         uint8_t delay_between_attempts = LINK_TRAINING_RETRY_DELAY;
1774         struct dc_stream_state *stream = pipe_ctx->stream;
1775         struct dc_link *link = stream->link;
1776         enum dp_panel_mode panel_mode = dp_get_panel_mode(link);
1777         struct link_encoder *link_enc;
1778         enum link_training_result status = LINK_TRAINING_CR_FAIL_LANE0;
1779         struct dc_link_settings current_setting = *link_setting;
1780
1781         /* Dynamically assigned link encoders associated with stream rather than
1782          * link.
1783          */
1784         if (link->dc->res_pool->funcs->link_encs_assign)
1785                 link_enc = stream->link_enc;
1786         else
1787                 link_enc = link->link_enc;
1788         ASSERT(link_enc);
1789
1790         /* We need to do this before the link training to ensure the idle pattern in SST
1791          * mode will be sent right after the link training
1792          */
1793         link_enc->funcs->connect_dig_be_to_fe(link_enc,
1794                                                         pipe_ctx->stream_res.stream_enc->id, true);
1795
1796         for (j = 0; j < attempts; ++j) {
1797
1798                 DC_LOG_HW_LINK_TRAINING("%s: Beginning link training attempt %u of %d\n",
1799                         __func__, (unsigned int)j + 1, attempts);
1800
1801                 dp_enable_link_phy(
1802                         link,
1803                         signal,
1804                         pipe_ctx->clock_source->id,
1805                         &current_setting);
1806
1807                 if (stream->sink_patches.dppowerup_delay > 0) {
1808                         int delay_dp_power_up_in_ms = stream->sink_patches.dppowerup_delay;
1809
1810                         msleep(delay_dp_power_up_in_ms);
1811                 }
1812
1813 #ifdef CONFIG_DRM_AMD_DC_HDCP
1814                 if (panel_mode == DP_PANEL_MODE_EDP) {
1815                         struct cp_psp *cp_psp = &stream->ctx->cp_psp;
1816
1817                         if (cp_psp && cp_psp->funcs.enable_assr) {
1818                                 if (!cp_psp->funcs.enable_assr(cp_psp->handle, link)) {
1819                                         /* since eDP implies ASSR on, change panel
1820                                          * mode to disable ASSR
1821                                          */
1822                                         panel_mode = DP_PANEL_MODE_DEFAULT;
1823                                 }
1824                         } else
1825                                 panel_mode = DP_PANEL_MODE_DEFAULT;
1826                 }
1827 #endif
1828
1829                 dp_set_panel_mode(link, panel_mode);
1830
1831                 if (link->aux_access_disabled) {
1832                         dc_link_dp_perform_link_training_skip_aux(link, &current_setting);
1833                         return true;
1834                 } else {
1835                                 status = dc_link_dp_perform_link_training(
1836                                                                                 link,
1837                                                                                 &current_setting,
1838                                                                                 skip_video_pattern);
1839                         if (status == LINK_TRAINING_SUCCESS)
1840                                 return true;
1841                 }
1842
1843                 /* latest link training still fail, skip delay and keep PHY on
1844                  */
1845                 if (j == (attempts - 1) && link->ep_type == DISPLAY_ENDPOINT_PHY)
1846                         break;
1847
1848                 DC_LOG_WARNING("%s: Link training attempt %u of %d failed\n",
1849                         __func__, (unsigned int)j + 1, attempts);
1850
1851                 dp_disable_link_phy(link, signal);
1852
1853                 /* Abort link training if failure due to sink being unplugged. */
1854                 if (status == LINK_TRAINING_ABORT)
1855                         break;
1856                 else if (do_fallback) {
1857                         decide_fallback_link_setting(*link_setting, &current_setting, status);
1858                         /* Fail link training if reduced link bandwidth no longer meets
1859                          * stream requirements.
1860                          */
1861                         if (dc_bandwidth_in_kbps_from_timing(&stream->timing) <
1862                                         dc_link_bandwidth_kbps(link, &current_setting))
1863                                 break;
1864                 }
1865
1866                 msleep(delay_between_attempts);
1867
1868                 delay_between_attempts += LINK_TRAINING_RETRY_DELAY;
1869         }
1870
1871         return false;
1872 }
1873
1874 static enum clock_source_id get_clock_source_id(struct dc_link *link)
1875 {
1876         enum clock_source_id dp_cs_id = CLOCK_SOURCE_ID_UNDEFINED;
1877         struct clock_source *dp_cs = link->dc->res_pool->dp_clock_source;
1878
1879         if (dp_cs != NULL) {
1880                 dp_cs_id = dp_cs->id;
1881         } else {
1882                 /*
1883                  * dp clock source is not initialized for some reason.
1884                  * Should not happen, CLOCK_SOURCE_ID_EXTERNAL will be used
1885                  */
1886                 ASSERT(dp_cs);
1887         }
1888
1889         return dp_cs_id;
1890 }
1891
1892 static void set_dp_mst_mode(struct dc_link *link, bool mst_enable)
1893 {
1894         if (mst_enable == false &&
1895                 link->type == dc_connection_mst_branch) {
1896                 /* Disable MST on link. Use only local sink. */
1897                 dp_disable_link_phy_mst(link, link->connector_signal);
1898
1899                 link->type = dc_connection_single;
1900                 link->local_sink = link->remote_sinks[0];
1901                 link->local_sink->sink_signal = SIGNAL_TYPE_DISPLAY_PORT;
1902                 dc_sink_retain(link->local_sink);
1903                 dm_helpers_dp_mst_stop_top_mgr(link->ctx, link);
1904         } else if (mst_enable == true &&
1905                         link->type == dc_connection_single &&
1906                         link->remote_sinks[0] != NULL) {
1907                 /* Re-enable MST on link. */
1908                 dp_disable_link_phy(link, link->connector_signal);
1909                 dp_enable_mst_on_sink(link, true);
1910
1911                 link->type = dc_connection_mst_branch;
1912                 link->local_sink->sink_signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
1913         }
1914 }
1915
1916 bool dc_link_dp_sync_lt_begin(struct dc_link *link)
1917 {
1918         /* Begin Sync LT. During this time,
1919          * DPCD:600h must not be powered down.
1920          */
1921         link->sync_lt_in_progress = true;
1922
1923         /*Clear any existing preferred settings.*/
1924         memset(&link->preferred_training_settings, 0,
1925                 sizeof(struct dc_link_training_overrides));
1926         memset(&link->preferred_link_setting, 0,
1927                 sizeof(struct dc_link_settings));
1928
1929         return true;
1930 }
1931
1932 enum link_training_result dc_link_dp_sync_lt_attempt(
1933     struct dc_link *link,
1934     struct dc_link_settings *link_settings,
1935     struct dc_link_training_overrides *lt_overrides)
1936 {
1937         struct link_training_settings lt_settings;
1938         enum link_training_result lt_status = LINK_TRAINING_SUCCESS;
1939         enum dp_panel_mode panel_mode = DP_PANEL_MODE_DEFAULT;
1940         enum clock_source_id dp_cs_id = CLOCK_SOURCE_ID_EXTERNAL;
1941         bool fec_enable = false;
1942
1943         dp_decide_training_settings(
1944                 link,
1945                 link_settings,
1946                 lt_overrides,
1947                 &lt_settings);
1948
1949         /* Setup MST Mode */
1950         if (lt_overrides->mst_enable)
1951                 set_dp_mst_mode(link, *lt_overrides->mst_enable);
1952
1953         /* Disable link */
1954         dp_disable_link_phy(link, link->connector_signal);
1955
1956         /* Enable link */
1957         dp_cs_id = get_clock_source_id(link);
1958         dp_enable_link_phy(link, link->connector_signal,
1959                 dp_cs_id, link_settings);
1960
1961         /* Set FEC enable */
1962         fec_enable = lt_overrides->fec_enable && *lt_overrides->fec_enable;
1963         dp_set_fec_ready(link, fec_enable);
1964
1965         if (lt_overrides->alternate_scrambler_reset) {
1966                 if (*lt_overrides->alternate_scrambler_reset)
1967                         panel_mode = DP_PANEL_MODE_EDP;
1968                 else
1969                         panel_mode = DP_PANEL_MODE_DEFAULT;
1970         } else
1971                 panel_mode = dp_get_panel_mode(link);
1972
1973         dp_set_panel_mode(link, panel_mode);
1974
1975         /* Attempt to train with given link training settings */
1976         if (link->ctx->dc->work_arounds.lt_early_cr_pattern)
1977                 start_clock_recovery_pattern_early(link, &lt_settings, DPRX);
1978
1979         /* Set link rate, lane count and spread. */
1980         dpcd_set_link_settings(link, &lt_settings);
1981
1982         /* 2. perform link training (set link training done
1983          *  to false is done as well)
1984          */
1985         lt_status = perform_clock_recovery_sequence(link, &lt_settings, DPRX);
1986         if (lt_status == LINK_TRAINING_SUCCESS) {
1987                 lt_status = perform_channel_equalization_sequence(link,
1988                                                 &lt_settings,
1989                                                 DPRX);
1990         }
1991
1992         /* 3. Sync LT must skip TRAINING_PATTERN_SET:0 (video pattern)*/
1993         /* 4. print status message*/
1994         print_status_message(link, &lt_settings, lt_status);
1995
1996         return lt_status;
1997 }
1998
1999 bool dc_link_dp_sync_lt_end(struct dc_link *link, bool link_down)
2000 {
2001         /* If input parameter is set, shut down phy.
2002          * Still shouldn't turn off dp_receiver (DPCD:600h)
2003          */
2004         if (link_down == true) {
2005                 dp_disable_link_phy(link, link->connector_signal);
2006                 dp_set_fec_ready(link, false);
2007         }
2008
2009         link->sync_lt_in_progress = false;
2010         return true;
2011 }
2012
2013 bool dc_link_dp_get_max_link_enc_cap(const struct dc_link *link, struct dc_link_settings *max_link_enc_cap)
2014 {
2015         if (!max_link_enc_cap) {
2016                 DC_LOG_ERROR("%s: Could not return max link encoder caps", __func__);
2017                 return false;
2018         }
2019
2020         if (link->link_enc->funcs->get_max_link_cap) {
2021                 link->link_enc->funcs->get_max_link_cap(link->link_enc, max_link_enc_cap);
2022                 return true;
2023         }
2024
2025         DC_LOG_ERROR("%s: Max link encoder caps unknown", __func__);
2026         max_link_enc_cap->lane_count = 1;
2027         max_link_enc_cap->link_rate = 6;
2028         return false;
2029 }
2030
2031 static struct dc_link_settings get_max_link_cap(struct dc_link *link)
2032 {
2033         struct dc_link_settings max_link_cap = {0};
2034
2035         /* get max link encoder capability */
2036         link->link_enc->funcs->get_max_link_cap(link->link_enc, &max_link_cap);
2037
2038         /* Lower link settings based on sink's link cap */
2039         if (link->reported_link_cap.lane_count < max_link_cap.lane_count)
2040                 max_link_cap.lane_count =
2041                                 link->reported_link_cap.lane_count;
2042         if (link->reported_link_cap.link_rate < max_link_cap.link_rate)
2043                 max_link_cap.link_rate =
2044                                 link->reported_link_cap.link_rate;
2045         if (link->reported_link_cap.link_spread <
2046                         max_link_cap.link_spread)
2047                 max_link_cap.link_spread =
2048                                 link->reported_link_cap.link_spread;
2049         /*
2050          * account for lttpr repeaters cap
2051          * notes: repeaters do not snoop in the DPRX Capabilities addresses (3.6.3).
2052          */
2053         if (link->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT) {
2054                 if (link->dpcd_caps.lttpr_caps.max_lane_count < max_link_cap.lane_count)
2055                         max_link_cap.lane_count = link->dpcd_caps.lttpr_caps.max_lane_count;
2056
2057                 if (link->dpcd_caps.lttpr_caps.max_link_rate < max_link_cap.link_rate)
2058                         max_link_cap.link_rate = link->dpcd_caps.lttpr_caps.max_link_rate;
2059
2060                 DC_LOG_HW_LINK_TRAINING("%s\n Training with LTTPR,  max_lane count %d max_link rate %d \n",
2061                                                 __func__,
2062                                                 max_link_cap.lane_count,
2063                                                 max_link_cap.link_rate);
2064         }
2065         return max_link_cap;
2066 }
2067
2068 enum dc_status read_hpd_rx_irq_data(
2069         struct dc_link *link,
2070         union hpd_irq_data *irq_data)
2071 {
2072         static enum dc_status retval;
2073
2074         /* The HW reads 16 bytes from 200h on HPD,
2075          * but if we get an AUX_DEFER, the HW cannot retry
2076          * and this causes the CTS tests 4.3.2.1 - 3.2.4 to
2077          * fail, so we now explicitly read 6 bytes which is
2078          * the req from the above mentioned test cases.
2079          *
2080          * For DP 1.4 we need to read those from 2002h range.
2081          */
2082         if (link->dpcd_caps.dpcd_rev.raw < DPCD_REV_14)
2083                 retval = core_link_read_dpcd(
2084                         link,
2085                         DP_SINK_COUNT,
2086                         irq_data->raw,
2087                         sizeof(union hpd_irq_data));
2088         else {
2089                 /* Read 14 bytes in a single read and then copy only the required fields.
2090                  * This is more efficient than doing it in two separate AUX reads. */
2091
2092                 uint8_t tmp[DP_SINK_STATUS_ESI - DP_SINK_COUNT_ESI + 1];
2093
2094                 retval = core_link_read_dpcd(
2095                         link,
2096                         DP_SINK_COUNT_ESI,
2097                         tmp,
2098                         sizeof(tmp));
2099
2100                 if (retval != DC_OK)
2101                         return retval;
2102
2103                 irq_data->bytes.sink_cnt.raw = tmp[DP_SINK_COUNT_ESI - DP_SINK_COUNT_ESI];
2104                 irq_data->bytes.device_service_irq.raw = tmp[DP_DEVICE_SERVICE_IRQ_VECTOR_ESI0 - DP_SINK_COUNT_ESI];
2105                 irq_data->bytes.lane01_status.raw = tmp[DP_LANE0_1_STATUS_ESI - DP_SINK_COUNT_ESI];
2106                 irq_data->bytes.lane23_status.raw = tmp[DP_LANE2_3_STATUS_ESI - DP_SINK_COUNT_ESI];
2107                 irq_data->bytes.lane_status_updated.raw = tmp[DP_LANE_ALIGN_STATUS_UPDATED_ESI - DP_SINK_COUNT_ESI];
2108                 irq_data->bytes.sink_status.raw = tmp[DP_SINK_STATUS_ESI - DP_SINK_COUNT_ESI];
2109         }
2110
2111         return retval;
2112 }
2113
2114 bool hpd_rx_irq_check_link_loss_status(
2115         struct dc_link *link,
2116         union hpd_irq_data *hpd_irq_dpcd_data)
2117 {
2118         uint8_t irq_reg_rx_power_state = 0;
2119         enum dc_status dpcd_result = DC_ERROR_UNEXPECTED;
2120         union lane_status lane_status;
2121         uint32_t lane;
2122         bool sink_status_changed;
2123         bool return_code;
2124
2125         sink_status_changed = false;
2126         return_code = false;
2127
2128         if (link->cur_link_settings.lane_count == 0)
2129                 return return_code;
2130
2131         /*1. Check that Link Status changed, before re-training.*/
2132
2133         /*parse lane status*/
2134         for (lane = 0; lane < link->cur_link_settings.lane_count; lane++) {
2135                 /* check status of lanes 0,1
2136                  * changed DpcdAddress_Lane01Status (0x202)
2137                  */
2138                 lane_status.raw = get_nibble_at_index(
2139                         &hpd_irq_dpcd_data->bytes.lane01_status.raw,
2140                         lane);
2141
2142                 if (!lane_status.bits.CHANNEL_EQ_DONE_0 ||
2143                         !lane_status.bits.CR_DONE_0 ||
2144                         !lane_status.bits.SYMBOL_LOCKED_0) {
2145                         /* if one of the channel equalization, clock
2146                          * recovery or symbol lock is dropped
2147                          * consider it as (link has been
2148                          * dropped) dp sink status has changed
2149                          */
2150                         sink_status_changed = true;
2151                         break;
2152                 }
2153         }
2154
2155         /* Check interlane align.*/
2156         if (sink_status_changed ||
2157                 !hpd_irq_dpcd_data->bytes.lane_status_updated.bits.INTERLANE_ALIGN_DONE) {
2158
2159                 DC_LOG_HW_HPD_IRQ("%s: Link Status changed.\n", __func__);
2160
2161                 return_code = true;
2162
2163                 /*2. Check that we can handle interrupt: Not in FS DOS,
2164                  *  Not in "Display Timeout" state, Link is trained.
2165                  */
2166                 dpcd_result = core_link_read_dpcd(link,
2167                         DP_SET_POWER,
2168                         &irq_reg_rx_power_state,
2169                         sizeof(irq_reg_rx_power_state));
2170
2171                 if (dpcd_result != DC_OK) {
2172                         DC_LOG_HW_HPD_IRQ("%s: DPCD read failed to obtain power state.\n",
2173                                 __func__);
2174                 } else {
2175                         if (irq_reg_rx_power_state != DP_SET_POWER_D0)
2176                                 return_code = false;
2177                 }
2178         }
2179
2180         return return_code;
2181 }
2182
2183 bool dp_verify_link_cap(
2184         struct dc_link *link,
2185         struct dc_link_settings *known_limit_link_setting,
2186         int *fail_count)
2187 {
2188         struct dc_link_settings max_link_cap = {0};
2189         struct dc_link_settings cur_link_setting = {0};
2190         struct dc_link_settings *cur = &cur_link_setting;
2191         struct dc_link_settings initial_link_settings = {0};
2192         bool success;
2193         bool skip_link_training;
2194         bool skip_video_pattern;
2195         enum clock_source_id dp_cs_id = CLOCK_SOURCE_ID_EXTERNAL;
2196         enum link_training_result status;
2197         union hpd_irq_data irq_data;
2198
2199         if (link->dc->debug.skip_detection_link_training) {
2200                 link->verified_link_cap = *known_limit_link_setting;
2201                 return true;
2202         }
2203
2204         memset(&irq_data, 0, sizeof(irq_data));
2205         success = false;
2206         skip_link_training = false;
2207
2208         max_link_cap = get_max_link_cap(link);
2209
2210         /* Grant extended timeout request */
2211         if ((link->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT) && (link->dpcd_caps.lttpr_caps.max_ext_timeout > 0)) {
2212                 uint8_t grant = link->dpcd_caps.lttpr_caps.max_ext_timeout & 0x80;
2213
2214                 core_link_write_dpcd(link, DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT, &grant, sizeof(grant));
2215         }
2216
2217         /* TODO implement override and monitor patch later */
2218
2219         /* try to train the link from high to low to
2220          * find the physical link capability
2221          */
2222         /* disable PHY done possible by BIOS, will be done by driver itself */
2223         dp_disable_link_phy(link, link->connector_signal);
2224
2225         dp_cs_id = get_clock_source_id(link);
2226
2227         /* link training starts with the maximum common settings
2228          * supported by both sink and ASIC.
2229          */
2230         initial_link_settings = get_common_supported_link_settings(
2231                         *known_limit_link_setting,
2232                         max_link_cap);
2233         cur_link_setting = initial_link_settings;
2234
2235         /* Temporary Renoir-specific workaround for SWDEV-215184;
2236          * PHY will sometimes be in bad state on hotplugging display from certain USB-C dongle,
2237          * so add extra cycle of enabling and disabling the PHY before first link training.
2238          */
2239         if (link->link_enc->features.flags.bits.DP_IS_USB_C &&
2240                         link->dc->debug.usbc_combo_phy_reset_wa) {
2241                 dp_enable_link_phy(link, link->connector_signal, dp_cs_id, cur);
2242                 dp_disable_link_phy(link, link->connector_signal);
2243         }
2244
2245         do {
2246                 skip_video_pattern = true;
2247
2248                 if (cur->link_rate == LINK_RATE_LOW)
2249                         skip_video_pattern = false;
2250
2251                 dp_enable_link_phy(
2252                                 link,
2253                                 link->connector_signal,
2254                                 dp_cs_id,
2255                                 cur);
2256
2257
2258                 if (skip_link_training)
2259                         success = true;
2260                 else {
2261                         status = dc_link_dp_perform_link_training(
2262                                                         link,
2263                                                         cur,
2264                                                         skip_video_pattern);
2265                         if (status == LINK_TRAINING_SUCCESS)
2266                                 success = true;
2267                         else
2268                                 (*fail_count)++;
2269                 }
2270
2271                 if (success) {
2272                         link->verified_link_cap = *cur;
2273                         udelay(1000);
2274                         if (read_hpd_rx_irq_data(link, &irq_data) == DC_OK)
2275                                 if (hpd_rx_irq_check_link_loss_status(
2276                                                 link,
2277                                                 &irq_data))
2278                                         (*fail_count)++;
2279                 }
2280                 /* always disable the link before trying another
2281                  * setting or before returning we'll enable it later
2282                  * based on the actual mode we're driving
2283                  */
2284                 dp_disable_link_phy(link, link->connector_signal);
2285         } while (!success && decide_fallback_link_setting(
2286                         initial_link_settings, cur, status));
2287
2288         /* Link Training failed for all Link Settings
2289          *  (Lane Count is still unknown)
2290          */
2291         if (!success) {
2292                 /* If all LT fails for all settings,
2293                  * set verified = failed safe (1 lane low)
2294                  */
2295                 link->verified_link_cap.lane_count = LANE_COUNT_ONE;
2296                 link->verified_link_cap.link_rate = LINK_RATE_LOW;
2297
2298                 link->verified_link_cap.link_spread =
2299                 LINK_SPREAD_DISABLED;
2300         }
2301
2302
2303         return success;
2304 }
2305
2306 bool dp_verify_link_cap_with_retries(
2307         struct dc_link *link,
2308         struct dc_link_settings *known_limit_link_setting,
2309         int attempts)
2310 {
2311         int i = 0;
2312         bool success = false;
2313
2314         for (i = 0; i < attempts; i++) {
2315                 int fail_count = 0;
2316                 enum dc_connection_type type = dc_connection_none;
2317
2318                 memset(&link->verified_link_cap, 0,
2319                                 sizeof(struct dc_link_settings));
2320                 if (!dc_link_detect_sink(link, &type) || type == dc_connection_none) {
2321                         link->verified_link_cap.lane_count = LANE_COUNT_ONE;
2322                         link->verified_link_cap.link_rate = LINK_RATE_LOW;
2323                         link->verified_link_cap.link_spread = LINK_SPREAD_DISABLED;
2324                         break;
2325                 } else if (dp_verify_link_cap(link,
2326                                 &link->reported_link_cap,
2327                                 &fail_count) && fail_count == 0) {
2328                         success = true;
2329                         break;
2330                 }
2331                 msleep(10);
2332         }
2333         return success;
2334 }
2335
2336 bool dp_verify_mst_link_cap(
2337         struct dc_link *link)
2338 {
2339         struct dc_link_settings max_link_cap = {0};
2340
2341         max_link_cap = get_max_link_cap(link);
2342         link->verified_link_cap = get_common_supported_link_settings(
2343                 link->reported_link_cap,
2344                 max_link_cap);
2345
2346         return true;
2347 }
2348
2349 static struct dc_link_settings get_common_supported_link_settings(
2350                 struct dc_link_settings link_setting_a,
2351                 struct dc_link_settings link_setting_b)
2352 {
2353         struct dc_link_settings link_settings = {0};
2354
2355         link_settings.lane_count =
2356                 (link_setting_a.lane_count <=
2357                         link_setting_b.lane_count) ?
2358                         link_setting_a.lane_count :
2359                         link_setting_b.lane_count;
2360         link_settings.link_rate =
2361                 (link_setting_a.link_rate <=
2362                         link_setting_b.link_rate) ?
2363                         link_setting_a.link_rate :
2364                         link_setting_b.link_rate;
2365         link_settings.link_spread = LINK_SPREAD_DISABLED;
2366
2367         /* in DP compliance test, DPR-120 may have
2368          * a random value in its MAX_LINK_BW dpcd field.
2369          * We map it to the maximum supported link rate that
2370          * is smaller than MAX_LINK_BW in this case.
2371          */
2372         if (link_settings.link_rate > LINK_RATE_HIGH3) {
2373                 link_settings.link_rate = LINK_RATE_HIGH3;
2374         } else if (link_settings.link_rate < LINK_RATE_HIGH3
2375                         && link_settings.link_rate > LINK_RATE_HIGH2) {
2376                 link_settings.link_rate = LINK_RATE_HIGH2;
2377         } else if (link_settings.link_rate < LINK_RATE_HIGH2
2378                         && link_settings.link_rate > LINK_RATE_HIGH) {
2379                 link_settings.link_rate = LINK_RATE_HIGH;
2380         } else if (link_settings.link_rate < LINK_RATE_HIGH
2381                         && link_settings.link_rate > LINK_RATE_LOW) {
2382                 link_settings.link_rate = LINK_RATE_LOW;
2383         } else if (link_settings.link_rate < LINK_RATE_LOW) {
2384                 link_settings.link_rate = LINK_RATE_UNKNOWN;
2385         }
2386
2387         return link_settings;
2388 }
2389
2390 static inline bool reached_minimum_lane_count(enum dc_lane_count lane_count)
2391 {
2392         return lane_count <= LANE_COUNT_ONE;
2393 }
2394
2395 static inline bool reached_minimum_link_rate(enum dc_link_rate link_rate)
2396 {
2397         return link_rate <= LINK_RATE_LOW;
2398 }
2399
2400 static enum dc_lane_count reduce_lane_count(enum dc_lane_count lane_count)
2401 {
2402         switch (lane_count) {
2403         case LANE_COUNT_FOUR:
2404                 return LANE_COUNT_TWO;
2405         case LANE_COUNT_TWO:
2406                 return LANE_COUNT_ONE;
2407         case LANE_COUNT_ONE:
2408                 return LANE_COUNT_UNKNOWN;
2409         default:
2410                 return LANE_COUNT_UNKNOWN;
2411         }
2412 }
2413
2414 static enum dc_link_rate reduce_link_rate(enum dc_link_rate link_rate)
2415 {
2416         switch (link_rate) {
2417         case LINK_RATE_HIGH3:
2418                 return LINK_RATE_HIGH2;
2419         case LINK_RATE_HIGH2:
2420                 return LINK_RATE_HIGH;
2421         case LINK_RATE_HIGH:
2422                 return LINK_RATE_LOW;
2423         case LINK_RATE_LOW:
2424                 return LINK_RATE_UNKNOWN;
2425         default:
2426                 return LINK_RATE_UNKNOWN;
2427         }
2428 }
2429
2430 static enum dc_lane_count increase_lane_count(enum dc_lane_count lane_count)
2431 {
2432         switch (lane_count) {
2433         case LANE_COUNT_ONE:
2434                 return LANE_COUNT_TWO;
2435         case LANE_COUNT_TWO:
2436                 return LANE_COUNT_FOUR;
2437         default:
2438                 return LANE_COUNT_UNKNOWN;
2439         }
2440 }
2441
2442 static enum dc_link_rate increase_link_rate(enum dc_link_rate link_rate)
2443 {
2444         switch (link_rate) {
2445         case LINK_RATE_LOW:
2446                 return LINK_RATE_HIGH;
2447         case LINK_RATE_HIGH:
2448                 return LINK_RATE_HIGH2;
2449         case LINK_RATE_HIGH2:
2450                 return LINK_RATE_HIGH3;
2451         default:
2452                 return LINK_RATE_UNKNOWN;
2453         }
2454 }
2455
2456 /*
2457  * function: set link rate and lane count fallback based
2458  * on current link setting and last link training result
2459  * return value:
2460  *                      true - link setting could be set
2461  *                      false - has reached minimum setting
2462  *                                      and no further fallback could be done
2463  */
2464 static bool decide_fallback_link_setting(
2465                 struct dc_link_settings initial_link_settings,
2466                 struct dc_link_settings *current_link_setting,
2467                 enum link_training_result training_result)
2468 {
2469         if (!current_link_setting)
2470                 return false;
2471
2472         switch (training_result) {
2473         case LINK_TRAINING_CR_FAIL_LANE0:
2474         case LINK_TRAINING_CR_FAIL_LANE1:
2475         case LINK_TRAINING_CR_FAIL_LANE23:
2476         case LINK_TRAINING_LQA_FAIL:
2477         {
2478                 if (!reached_minimum_link_rate
2479                                 (current_link_setting->link_rate)) {
2480                         current_link_setting->link_rate =
2481                                 reduce_link_rate(
2482                                         current_link_setting->link_rate);
2483                 } else if (!reached_minimum_lane_count
2484                                 (current_link_setting->lane_count)) {
2485                         current_link_setting->link_rate =
2486                                 initial_link_settings.link_rate;
2487                         if (training_result == LINK_TRAINING_CR_FAIL_LANE0)
2488                                 return false;
2489                         else if (training_result == LINK_TRAINING_CR_FAIL_LANE1)
2490                                 current_link_setting->lane_count =
2491                                                 LANE_COUNT_ONE;
2492                         else if (training_result ==
2493                                         LINK_TRAINING_CR_FAIL_LANE23)
2494                                 current_link_setting->lane_count =
2495                                                 LANE_COUNT_TWO;
2496                         else
2497                                 current_link_setting->lane_count =
2498                                         reduce_lane_count(
2499                                         current_link_setting->lane_count);
2500                 } else {
2501                         return false;
2502                 }
2503                 break;
2504         }
2505         case LINK_TRAINING_EQ_FAIL_EQ:
2506         {
2507                 if (!reached_minimum_lane_count
2508                                 (current_link_setting->lane_count)) {
2509                         current_link_setting->lane_count =
2510                                 reduce_lane_count(
2511                                         current_link_setting->lane_count);
2512                 } else if (!reached_minimum_link_rate
2513                                 (current_link_setting->link_rate)) {
2514                         current_link_setting->link_rate =
2515                                 reduce_link_rate(
2516                                         current_link_setting->link_rate);
2517                 } else {
2518                         return false;
2519                 }
2520                 break;
2521         }
2522         case LINK_TRAINING_EQ_FAIL_CR:
2523         {
2524                 if (!reached_minimum_link_rate
2525                                 (current_link_setting->link_rate)) {
2526                         current_link_setting->link_rate =
2527                                 reduce_link_rate(
2528                                         current_link_setting->link_rate);
2529                 } else {
2530                         return false;
2531                 }
2532                 break;
2533         }
2534         default:
2535                 return false;
2536         }
2537         return true;
2538 }
2539
2540 bool dp_validate_mode_timing(
2541         struct dc_link *link,
2542         const struct dc_crtc_timing *timing)
2543 {
2544         uint32_t req_bw;
2545         uint32_t max_bw;
2546
2547         const struct dc_link_settings *link_setting;
2548
2549         /* According to spec, VSC SDP should be used if pixel format is YCbCr420 */
2550         if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 &&
2551                         !link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED &&
2552                         dal_graphics_object_id_get_connector_id(link->link_id) != CONNECTOR_ID_VIRTUAL)
2553                 return false;
2554
2555         /*always DP fail safe mode*/
2556         if ((timing->pix_clk_100hz / 10) == (uint32_t) 25175 &&
2557                 timing->h_addressable == (uint32_t) 640 &&
2558                 timing->v_addressable == (uint32_t) 480)
2559                 return true;
2560
2561         link_setting = dc_link_get_link_cap(link);
2562
2563         /* TODO: DYNAMIC_VALIDATION needs to be implemented */
2564         /*if (flags.DYNAMIC_VALIDATION == 1 &&
2565                 link->verified_link_cap.lane_count != LANE_COUNT_UNKNOWN)
2566                 link_setting = &link->verified_link_cap;
2567         */
2568
2569         req_bw = dc_bandwidth_in_kbps_from_timing(timing);
2570         max_bw = dc_link_bandwidth_kbps(link, link_setting);
2571
2572         if (req_bw <= max_bw) {
2573                 /* remember the biggest mode here, during
2574                  * initial link training (to get
2575                  * verified_link_cap), LS sends event about
2576                  * cannot train at reported cap to upper
2577                  * layer and upper layer will re-enumerate modes.
2578                  * this is not necessary if the lower
2579                  * verified_link_cap is enough to drive
2580                  * all the modes */
2581
2582                 /* TODO: DYNAMIC_VALIDATION needs to be implemented */
2583                 /* if (flags.DYNAMIC_VALIDATION == 1)
2584                         dpsst->max_req_bw_for_verified_linkcap = dal_max(
2585                                 dpsst->max_req_bw_for_verified_linkcap, req_bw); */
2586                 return true;
2587         } else
2588                 return false;
2589 }
2590
2591 static bool decide_dp_link_settings(struct dc_link *link, struct dc_link_settings *link_setting, uint32_t req_bw)
2592 {
2593         struct dc_link_settings initial_link_setting = {
2594                 LANE_COUNT_ONE, LINK_RATE_LOW, LINK_SPREAD_DISABLED, false, 0};
2595         struct dc_link_settings current_link_setting =
2596                         initial_link_setting;
2597         uint32_t link_bw;
2598
2599         if (req_bw > dc_link_bandwidth_kbps(link, &link->verified_link_cap))
2600                 return false;
2601
2602         /* search for the minimum link setting that:
2603          * 1. is supported according to the link training result
2604          * 2. could support the b/w requested by the timing
2605          */
2606         while (current_link_setting.link_rate <=
2607                         link->verified_link_cap.link_rate) {
2608                 link_bw = dc_link_bandwidth_kbps(
2609                                 link,
2610                                 &current_link_setting);
2611                 if (req_bw <= link_bw) {
2612                         *link_setting = current_link_setting;
2613                         return true;
2614                 }
2615
2616                 if (current_link_setting.lane_count <
2617                                 link->verified_link_cap.lane_count) {
2618                         current_link_setting.lane_count =
2619                                         increase_lane_count(
2620                                                         current_link_setting.lane_count);
2621                 } else {
2622                         current_link_setting.link_rate =
2623                                         increase_link_rate(
2624                                                         current_link_setting.link_rate);
2625                         current_link_setting.lane_count =
2626                                         initial_link_setting.lane_count;
2627                 }
2628         }
2629
2630         return false;
2631 }
2632
2633 bool decide_edp_link_settings(struct dc_link *link, struct dc_link_settings *link_setting, uint32_t req_bw)
2634 {
2635         struct dc_link_settings initial_link_setting;
2636         struct dc_link_settings current_link_setting;
2637         uint32_t link_bw;
2638
2639         /*
2640          * edp_supported_link_rates_count is only valid for eDP v1.4 or higher.
2641          * Per VESA eDP spec, "The DPCD revision for eDP v1.4 is 13h"
2642          */
2643         if (link->dpcd_caps.dpcd_rev.raw < DPCD_REV_13 ||
2644                         link->dpcd_caps.edp_supported_link_rates_count == 0) {
2645                 *link_setting = link->verified_link_cap;
2646                 return true;
2647         }
2648
2649         memset(&initial_link_setting, 0, sizeof(initial_link_setting));
2650         initial_link_setting.lane_count = LANE_COUNT_ONE;
2651         initial_link_setting.link_rate = link->dpcd_caps.edp_supported_link_rates[0];
2652         initial_link_setting.link_spread = LINK_SPREAD_DISABLED;
2653         initial_link_setting.use_link_rate_set = true;
2654         initial_link_setting.link_rate_set = 0;
2655         current_link_setting = initial_link_setting;
2656
2657         /* search for the minimum link setting that:
2658          * 1. is supported according to the link training result
2659          * 2. could support the b/w requested by the timing
2660          */
2661         while (current_link_setting.link_rate <=
2662                         link->verified_link_cap.link_rate) {
2663                 link_bw = dc_link_bandwidth_kbps(
2664                                 link,
2665                                 &current_link_setting);
2666                 if (req_bw <= link_bw) {
2667                         *link_setting = current_link_setting;
2668                         return true;
2669                 }
2670
2671                 if (current_link_setting.lane_count <
2672                                 link->verified_link_cap.lane_count) {
2673                         current_link_setting.lane_count =
2674                                         increase_lane_count(
2675                                                         current_link_setting.lane_count);
2676                 } else {
2677                         if (current_link_setting.link_rate_set < link->dpcd_caps.edp_supported_link_rates_count) {
2678                                 current_link_setting.link_rate_set++;
2679                                 current_link_setting.link_rate =
2680                                         link->dpcd_caps.edp_supported_link_rates[current_link_setting.link_rate_set];
2681                                 current_link_setting.lane_count =
2682                                                                         initial_link_setting.lane_count;
2683                         } else
2684                                 break;
2685                 }
2686         }
2687         return false;
2688 }
2689
2690 static bool decide_mst_link_settings(const struct dc_link *link, struct dc_link_settings *link_setting)
2691 {
2692         *link_setting = link->verified_link_cap;
2693         return true;
2694 }
2695
2696 void decide_link_settings(struct dc_stream_state *stream,
2697         struct dc_link_settings *link_setting)
2698 {
2699         struct dc_link *link;
2700         uint32_t req_bw;
2701
2702         req_bw = dc_bandwidth_in_kbps_from_timing(&stream->timing);
2703
2704         link = stream->link;
2705
2706         /* if preferred is specified through AMDDP, use it, if it's enough
2707          * to drive the mode
2708          */
2709         if (link->preferred_link_setting.lane_count !=
2710                         LANE_COUNT_UNKNOWN &&
2711                         link->preferred_link_setting.link_rate !=
2712                                         LINK_RATE_UNKNOWN) {
2713                 *link_setting =  link->preferred_link_setting;
2714                 return;
2715         }
2716
2717         /* MST doesn't perform link training for now
2718          * TODO: add MST specific link training routine
2719          */
2720         if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2721                 if (decide_mst_link_settings(link, link_setting))
2722                         return;
2723         } else if (link->connector_signal == SIGNAL_TYPE_EDP) {
2724                 if (decide_edp_link_settings(link, link_setting, req_bw))
2725                         return;
2726         } else if (decide_dp_link_settings(link, link_setting, req_bw))
2727                 return;
2728
2729         BREAK_TO_DEBUGGER();
2730         ASSERT(link->verified_link_cap.lane_count != LANE_COUNT_UNKNOWN);
2731
2732         *link_setting = link->verified_link_cap;
2733 }
2734
2735 /*************************Short Pulse IRQ***************************/
2736 static bool allow_hpd_rx_irq(const struct dc_link *link)
2737 {
2738         /*
2739          * Don't handle RX IRQ unless one of following is met:
2740          * 1) The link is established (cur_link_settings != unknown)
2741          * 2) We know we're dealing with a branch device, SST or MST
2742          */
2743
2744         if ((link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
2745                 is_dp_branch_device(link))
2746                 return true;
2747
2748         return false;
2749 }
2750
2751 static bool handle_hpd_irq_psr_sink(struct dc_link *link)
2752 {
2753         union dpcd_psr_configuration psr_configuration;
2754
2755         if (!link->psr_settings.psr_feature_enabled)
2756                 return false;
2757
2758         dm_helpers_dp_read_dpcd(
2759                 link->ctx,
2760                 link,
2761                 368,/*DpcdAddress_PSR_Enable_Cfg*/
2762                 &psr_configuration.raw,
2763                 sizeof(psr_configuration.raw));
2764
2765
2766         if (psr_configuration.bits.ENABLE) {
2767                 unsigned char dpcdbuf[3] = {0};
2768                 union psr_error_status psr_error_status;
2769                 union psr_sink_psr_status psr_sink_psr_status;
2770
2771                 dm_helpers_dp_read_dpcd(
2772                         link->ctx,
2773                         link,
2774                         0x2006, /*DpcdAddress_PSR_Error_Status*/
2775                         (unsigned char *) dpcdbuf,
2776                         sizeof(dpcdbuf));
2777
2778                 /*DPCD 2006h   ERROR STATUS*/
2779                 psr_error_status.raw = dpcdbuf[0];
2780                 /*DPCD 2008h   SINK PANEL SELF REFRESH STATUS*/
2781                 psr_sink_psr_status.raw = dpcdbuf[2];
2782
2783                 if (psr_error_status.bits.LINK_CRC_ERROR ||
2784                                 psr_error_status.bits.RFB_STORAGE_ERROR ||
2785                                 psr_error_status.bits.VSC_SDP_ERROR) {
2786                         /* Acknowledge and clear error bits */
2787                         dm_helpers_dp_write_dpcd(
2788                                 link->ctx,
2789                                 link,
2790                                 8198,/*DpcdAddress_PSR_Error_Status*/
2791                                 &psr_error_status.raw,
2792                                 sizeof(psr_error_status.raw));
2793
2794                         /* PSR error, disable and re-enable PSR */
2795                         dc_link_set_psr_allow_active(link, false, true, false);
2796                         dc_link_set_psr_allow_active(link, true, true, false);
2797
2798                         return true;
2799                 } else if (psr_sink_psr_status.bits.SINK_SELF_REFRESH_STATUS ==
2800                                 PSR_SINK_STATE_ACTIVE_DISPLAY_FROM_SINK_RFB){
2801                         /* No error is detect, PSR is active.
2802                          * We should return with IRQ_HPD handled without
2803                          * checking for loss of sync since PSR would have
2804                          * powered down main link.
2805                          */
2806                         return true;
2807                 }
2808         }
2809         return false;
2810 }
2811
2812 static void dp_test_send_link_training(struct dc_link *link)
2813 {
2814         struct dc_link_settings link_settings = {0};
2815
2816         core_link_read_dpcd(
2817                         link,
2818                         DP_TEST_LANE_COUNT,
2819                         (unsigned char *)(&link_settings.lane_count),
2820                         1);
2821         core_link_read_dpcd(
2822                         link,
2823                         DP_TEST_LINK_RATE,
2824                         (unsigned char *)(&link_settings.link_rate),
2825                         1);
2826
2827         /* Set preferred link settings */
2828         link->verified_link_cap.lane_count = link_settings.lane_count;
2829         link->verified_link_cap.link_rate = link_settings.link_rate;
2830
2831         dp_retrain_link_dp_test(link, &link_settings, false);
2832 }
2833
2834 /* TODO Raven hbr2 compliance eye output is unstable
2835  * (toggling on and off) with debugger break
2836  * This caueses intermittent PHY automation failure
2837  * Need to look into the root cause */
2838 static void dp_test_send_phy_test_pattern(struct dc_link *link)
2839 {
2840         union phy_test_pattern dpcd_test_pattern;
2841         union lane_adjust dpcd_lane_adjustment[2];
2842         unsigned char dpcd_post_cursor_2_adjustment = 0;
2843         unsigned char test_pattern_buffer[
2844                         (DP_TEST_80BIT_CUSTOM_PATTERN_79_72 -
2845                         DP_TEST_80BIT_CUSTOM_PATTERN_7_0)+1] = {0};
2846         unsigned int test_pattern_size = 0;
2847         enum dp_test_pattern test_pattern;
2848         struct dc_link_training_settings link_settings;
2849         union lane_adjust dpcd_lane_adjust;
2850         unsigned int lane;
2851         struct link_training_settings link_training_settings;
2852         int i = 0;
2853
2854         dpcd_test_pattern.raw = 0;
2855         memset(dpcd_lane_adjustment, 0, sizeof(dpcd_lane_adjustment));
2856         memset(&link_settings, 0, sizeof(link_settings));
2857
2858         /* get phy test pattern and pattern parameters from DP receiver */
2859         core_link_read_dpcd(
2860                         link,
2861                         DP_PHY_TEST_PATTERN,
2862                         &dpcd_test_pattern.raw,
2863                         sizeof(dpcd_test_pattern));
2864         core_link_read_dpcd(
2865                         link,
2866                         DP_ADJUST_REQUEST_LANE0_1,
2867                         &dpcd_lane_adjustment[0].raw,
2868                         sizeof(dpcd_lane_adjustment));
2869
2870         /*get post cursor 2 parameters
2871          * For DP 1.1a or eariler, this DPCD register's value is 0
2872          * For DP 1.2 or later:
2873          * Bits 1:0 = POST_CURSOR2_LANE0; Bits 3:2 = POST_CURSOR2_LANE1
2874          * Bits 5:4 = POST_CURSOR2_LANE2; Bits 7:6 = POST_CURSOR2_LANE3
2875          */
2876         core_link_read_dpcd(
2877                         link,
2878                         DP_ADJUST_REQUEST_POST_CURSOR2,
2879                         &dpcd_post_cursor_2_adjustment,
2880                         sizeof(dpcd_post_cursor_2_adjustment));
2881
2882         /* translate request */
2883         switch (dpcd_test_pattern.bits.PATTERN) {
2884         case PHY_TEST_PATTERN_D10_2:
2885                 test_pattern = DP_TEST_PATTERN_D102;
2886                 break;
2887         case PHY_TEST_PATTERN_SYMBOL_ERROR:
2888                 test_pattern = DP_TEST_PATTERN_SYMBOL_ERROR;
2889                 break;
2890         case PHY_TEST_PATTERN_PRBS7:
2891                 test_pattern = DP_TEST_PATTERN_PRBS7;
2892                 break;
2893         case PHY_TEST_PATTERN_80BIT_CUSTOM:
2894                 test_pattern = DP_TEST_PATTERN_80BIT_CUSTOM;
2895                 break;
2896         case PHY_TEST_PATTERN_CP2520_1:
2897                 /* CP2520 pattern is unstable, temporarily use TPS4 instead */
2898                 test_pattern = (link->dc->caps.force_dp_tps4_for_cp2520 == 1) ?
2899                                 DP_TEST_PATTERN_TRAINING_PATTERN4 :
2900                                 DP_TEST_PATTERN_HBR2_COMPLIANCE_EYE;
2901                 break;
2902         case PHY_TEST_PATTERN_CP2520_2:
2903                 /* CP2520 pattern is unstable, temporarily use TPS4 instead */
2904                 test_pattern = (link->dc->caps.force_dp_tps4_for_cp2520 == 1) ?
2905                                 DP_TEST_PATTERN_TRAINING_PATTERN4 :
2906                                 DP_TEST_PATTERN_HBR2_COMPLIANCE_EYE;
2907                 break;
2908         case PHY_TEST_PATTERN_CP2520_3:
2909                 test_pattern = DP_TEST_PATTERN_TRAINING_PATTERN4;
2910                 break;
2911         default:
2912                 test_pattern = DP_TEST_PATTERN_VIDEO_MODE;
2913         break;
2914         }
2915
2916         if (test_pattern == DP_TEST_PATTERN_80BIT_CUSTOM) {
2917                 test_pattern_size = (DP_TEST_80BIT_CUSTOM_PATTERN_79_72 -
2918                                 DP_TEST_80BIT_CUSTOM_PATTERN_7_0) + 1;
2919                 core_link_read_dpcd(
2920                                 link,
2921                                 DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2922                                 test_pattern_buffer,
2923                                 test_pattern_size);
2924         }
2925
2926         /* prepare link training settings */
2927         link_settings.link = link->cur_link_settings;
2928
2929         for (lane = 0; lane <
2930                 (unsigned int)(link->cur_link_settings.lane_count);
2931                 lane++) {
2932                 dpcd_lane_adjust.raw =
2933                         get_nibble_at_index(&dpcd_lane_adjustment[0].raw, lane);
2934                 link_settings.lane_settings[lane].VOLTAGE_SWING =
2935                         (enum dc_voltage_swing)
2936                         (dpcd_lane_adjust.bits.VOLTAGE_SWING_LANE);
2937                 link_settings.lane_settings[lane].PRE_EMPHASIS =
2938                         (enum dc_pre_emphasis)
2939                         (dpcd_lane_adjust.bits.PRE_EMPHASIS_LANE);
2940                 link_settings.lane_settings[lane].POST_CURSOR2 =
2941                         (enum dc_post_cursor2)
2942                         ((dpcd_post_cursor_2_adjustment >> (lane * 2)) & 0x03);
2943         }
2944
2945         for (i = 0; i < 4; i++)
2946                 link_training_settings.lane_settings[i] =
2947                                 link_settings.lane_settings[i];
2948         link_training_settings.link_settings = link_settings.link;
2949         link_training_settings.allow_invalid_msa_timing_param = false;
2950         /*Usage: Measure DP physical lane signal
2951          * by DP SI test equipment automatically.
2952          * PHY test pattern request is generated by equipment via HPD interrupt.
2953          * HPD needs to be active all the time. HPD should be active
2954          * all the time. Do not touch it.
2955          * forward request to DS
2956          */
2957         dc_link_dp_set_test_pattern(
2958                 link,
2959                 test_pattern,
2960                 DP_TEST_PATTERN_COLOR_SPACE_UNDEFINED,
2961                 &link_training_settings,
2962                 test_pattern_buffer,
2963                 test_pattern_size);
2964 }
2965
2966 static void dp_test_send_link_test_pattern(struct dc_link *link)
2967 {
2968         union link_test_pattern dpcd_test_pattern;
2969         union test_misc dpcd_test_params;
2970         enum dp_test_pattern test_pattern;
2971         enum dp_test_pattern_color_space test_pattern_color_space =
2972                         DP_TEST_PATTERN_COLOR_SPACE_UNDEFINED;
2973         enum dc_color_depth requestColorDepth = COLOR_DEPTH_UNDEFINED;
2974         struct pipe_ctx *pipes = link->dc->current_state->res_ctx.pipe_ctx;
2975         struct pipe_ctx *pipe_ctx = NULL;
2976         int i;
2977
2978         memset(&dpcd_test_pattern, 0, sizeof(dpcd_test_pattern));
2979         memset(&dpcd_test_params, 0, sizeof(dpcd_test_params));
2980
2981         for (i = 0; i < MAX_PIPES; i++) {
2982                 if (pipes[i].stream == NULL)
2983                         continue;
2984
2985                 if (pipes[i].stream->link == link && !pipes[i].top_pipe && !pipes[i].prev_odm_pipe) {
2986                         pipe_ctx = &pipes[i];
2987                         break;
2988                 }
2989         }
2990
2991         if (pipe_ctx == NULL)
2992                 return;
2993
2994         /* get link test pattern and pattern parameters */
2995         core_link_read_dpcd(
2996                         link,
2997                         DP_TEST_PATTERN,
2998                         &dpcd_test_pattern.raw,
2999                         sizeof(dpcd_test_pattern));
3000         core_link_read_dpcd(
3001                         link,
3002                         DP_TEST_MISC0,
3003                         &dpcd_test_params.raw,
3004                         sizeof(dpcd_test_params));
3005
3006         switch (dpcd_test_pattern.bits.PATTERN) {
3007         case LINK_TEST_PATTERN_COLOR_RAMP:
3008                 test_pattern = DP_TEST_PATTERN_COLOR_RAMP;
3009         break;
3010         case LINK_TEST_PATTERN_VERTICAL_BARS:
3011                 test_pattern = DP_TEST_PATTERN_VERTICAL_BARS;
3012         break; /* black and white */
3013         case LINK_TEST_PATTERN_COLOR_SQUARES:
3014                 test_pattern = (dpcd_test_params.bits.DYN_RANGE ==
3015                                 TEST_DYN_RANGE_VESA ?
3016                                 DP_TEST_PATTERN_COLOR_SQUARES :
3017                                 DP_TEST_PATTERN_COLOR_SQUARES_CEA);
3018         break;
3019         default:
3020                 test_pattern = DP_TEST_PATTERN_VIDEO_MODE;
3021         break;
3022         }
3023
3024         if (dpcd_test_params.bits.CLR_FORMAT == 0)
3025                 test_pattern_color_space = DP_TEST_PATTERN_COLOR_SPACE_RGB;
3026         else
3027                 test_pattern_color_space = dpcd_test_params.bits.YCBCR_COEFS ?
3028                                 DP_TEST_PATTERN_COLOR_SPACE_YCBCR709 :
3029                                 DP_TEST_PATTERN_COLOR_SPACE_YCBCR601;
3030
3031         switch (dpcd_test_params.bits.BPC) {
3032         case 0: // 6 bits
3033                 requestColorDepth = COLOR_DEPTH_666;
3034                 break;
3035         case 1: // 8 bits
3036                 requestColorDepth = COLOR_DEPTH_888;
3037                 break;
3038         case 2: // 10 bits
3039                 requestColorDepth = COLOR_DEPTH_101010;
3040                 break;
3041         case 3: // 12 bits
3042                 requestColorDepth = COLOR_DEPTH_121212;
3043                 break;
3044         default:
3045                 break;
3046         }
3047
3048         switch (dpcd_test_params.bits.CLR_FORMAT) {
3049         case 0:
3050                 pipe_ctx->stream->timing.pixel_encoding = PIXEL_ENCODING_RGB;
3051                 break;
3052         case 1:
3053                 pipe_ctx->stream->timing.pixel_encoding = PIXEL_ENCODING_YCBCR422;
3054                 break;
3055         case 2:
3056                 pipe_ctx->stream->timing.pixel_encoding = PIXEL_ENCODING_YCBCR444;
3057                 break;
3058         default:
3059                 pipe_ctx->stream->timing.pixel_encoding = PIXEL_ENCODING_RGB;
3060                 break;
3061         }
3062
3063
3064         if (requestColorDepth != COLOR_DEPTH_UNDEFINED
3065                         && pipe_ctx->stream->timing.display_color_depth != requestColorDepth) {
3066                 DC_LOG_DEBUG("%s: original bpc %d, changing to %d\n",
3067                                 __func__,
3068                                 pipe_ctx->stream->timing.display_color_depth,
3069                                 requestColorDepth);
3070                 pipe_ctx->stream->timing.display_color_depth = requestColorDepth;
3071         }
3072
3073         dp_update_dsc_config(pipe_ctx);
3074
3075         dc_link_dp_set_test_pattern(
3076                         link,
3077                         test_pattern,
3078                         test_pattern_color_space,
3079                         NULL,
3080                         NULL,
3081                         0);
3082 }
3083
3084 static void dp_test_get_audio_test_data(struct dc_link *link, bool disable_video)
3085 {
3086         union audio_test_mode            dpcd_test_mode = {0};
3087         struct audio_test_pattern_type   dpcd_pattern_type = {0};
3088         union audio_test_pattern_period  dpcd_pattern_period[AUDIO_CHANNELS_COUNT] = {0};
3089         enum dp_test_pattern test_pattern = DP_TEST_PATTERN_AUDIO_OPERATOR_DEFINED;
3090
3091         struct pipe_ctx *pipes = link->dc->current_state->res_ctx.pipe_ctx;
3092         struct pipe_ctx *pipe_ctx = &pipes[0];
3093         unsigned int channel_count;
3094         unsigned int channel = 0;
3095         unsigned int modes = 0;
3096         unsigned int sampling_rate_in_hz = 0;
3097
3098         // get audio test mode and test pattern parameters
3099         core_link_read_dpcd(
3100                 link,
3101                 DP_TEST_AUDIO_MODE,
3102                 &dpcd_test_mode.raw,
3103                 sizeof(dpcd_test_mode));
3104
3105         core_link_read_dpcd(
3106                 link,
3107                 DP_TEST_AUDIO_PATTERN_TYPE,
3108                 &dpcd_pattern_type.value,
3109                 sizeof(dpcd_pattern_type));
3110
3111         channel_count = dpcd_test_mode.bits.channel_count + 1;
3112
3113         // read pattern periods for requested channels when sawTooth pattern is requested
3114         if (dpcd_pattern_type.value == AUDIO_TEST_PATTERN_SAWTOOTH ||
3115                         dpcd_pattern_type.value == AUDIO_TEST_PATTERN_OPERATOR_DEFINED) {
3116
3117                 test_pattern = (dpcd_pattern_type.value == AUDIO_TEST_PATTERN_SAWTOOTH) ?
3118                                 DP_TEST_PATTERN_AUDIO_SAWTOOTH : DP_TEST_PATTERN_AUDIO_OPERATOR_DEFINED;
3119                 // read period for each channel
3120                 for (channel = 0; channel < channel_count; channel++) {
3121                         core_link_read_dpcd(
3122                                                         link,
3123                                                         DP_TEST_AUDIO_PERIOD_CH1 + channel,
3124                                                         &dpcd_pattern_period[channel].raw,
3125                                                         sizeof(dpcd_pattern_period[channel]));
3126                 }
3127         }
3128
3129         // translate sampling rate
3130         switch (dpcd_test_mode.bits.sampling_rate) {
3131         case AUDIO_SAMPLING_RATE_32KHZ:
3132                 sampling_rate_in_hz = 32000;
3133                 break;
3134         case AUDIO_SAMPLING_RATE_44_1KHZ:
3135                 sampling_rate_in_hz = 44100;
3136                 break;
3137         case AUDIO_SAMPLING_RATE_48KHZ:
3138                 sampling_rate_in_hz = 48000;
3139                 break;
3140         case AUDIO_SAMPLING_RATE_88_2KHZ:
3141                 sampling_rate_in_hz = 88200;
3142                 break;
3143         case AUDIO_SAMPLING_RATE_96KHZ:
3144                 sampling_rate_in_hz = 96000;
3145                 break;
3146         case AUDIO_SAMPLING_RATE_176_4KHZ:
3147                 sampling_rate_in_hz = 176400;
3148                 break;
3149         case AUDIO_SAMPLING_RATE_192KHZ:
3150                 sampling_rate_in_hz = 192000;
3151                 break;
3152         default:
3153                 sampling_rate_in_hz = 0;
3154                 break;
3155         }
3156
3157         link->audio_test_data.flags.test_requested = 1;
3158         link->audio_test_data.flags.disable_video = disable_video;
3159         link->audio_test_data.sampling_rate = sampling_rate_in_hz;
3160         link->audio_test_data.channel_count = channel_count;
3161         link->audio_test_data.pattern_type = test_pattern;
3162
3163         if (test_pattern == DP_TEST_PATTERN_AUDIO_SAWTOOTH) {
3164                 for (modes = 0; modes < pipe_ctx->stream->audio_info.mode_count; modes++) {
3165                         link->audio_test_data.pattern_period[modes] = dpcd_pattern_period[modes].bits.pattern_period;
3166                 }
3167         }
3168 }
3169
3170 static void handle_automated_test(struct dc_link *link)
3171 {
3172         union test_request test_request;
3173         union test_response test_response;
3174
3175         memset(&test_request, 0, sizeof(test_request));
3176         memset(&test_response, 0, sizeof(test_response));
3177
3178         core_link_read_dpcd(
3179                 link,
3180                 DP_TEST_REQUEST,
3181                 &test_request.raw,
3182                 sizeof(union test_request));
3183         if (test_request.bits.LINK_TRAINING) {
3184                 /* ACK first to let DP RX test box monitor LT sequence */
3185                 test_response.bits.ACK = 1;
3186                 core_link_write_dpcd(
3187                         link,
3188                         DP_TEST_RESPONSE,
3189                         &test_response.raw,
3190                         sizeof(test_response));
3191                 dp_test_send_link_training(link);
3192                 /* no acknowledge request is needed again */
3193                 test_response.bits.ACK = 0;
3194         }
3195         if (test_request.bits.LINK_TEST_PATTRN) {
3196                 dp_test_send_link_test_pattern(link);
3197                 test_response.bits.ACK = 1;
3198         }
3199
3200         if (test_request.bits.AUDIO_TEST_PATTERN) {
3201                 dp_test_get_audio_test_data(link, test_request.bits.TEST_AUDIO_DISABLED_VIDEO);
3202                 test_response.bits.ACK = 1;
3203         }
3204
3205         if (test_request.bits.PHY_TEST_PATTERN) {
3206                 dp_test_send_phy_test_pattern(link);
3207                 test_response.bits.ACK = 1;
3208         }
3209
3210         /* send request acknowledgment */
3211         if (test_response.bits.ACK)
3212                 core_link_write_dpcd(
3213                         link,
3214                         DP_TEST_RESPONSE,
3215                         &test_response.raw,
3216                         sizeof(test_response));
3217 }
3218
3219 bool dc_link_handle_hpd_rx_irq(struct dc_link *link, union hpd_irq_data *out_hpd_irq_dpcd_data, bool *out_link_loss)
3220 {
3221         union hpd_irq_data hpd_irq_dpcd_data = { { { {0} } } };
3222         union device_service_irq device_service_clear = { { 0 } };
3223         enum dc_status result;
3224         bool status = false;
3225         struct pipe_ctx *pipe_ctx;
3226         int i;
3227
3228         if (out_link_loss)
3229                 *out_link_loss = false;
3230         /* For use cases related to down stream connection status change,
3231          * PSR and device auto test, refer to function handle_sst_hpd_irq
3232          * in DAL2.1*/
3233
3234         DC_LOG_HW_HPD_IRQ("%s: Got short pulse HPD on link %d\n",
3235                 __func__, link->link_index);
3236
3237
3238          /* All the "handle_hpd_irq_xxx()" methods
3239                  * should be called only after
3240                  * dal_dpsst_ls_read_hpd_irq_data
3241                  * Order of calls is important too
3242                  */
3243         result = read_hpd_rx_irq_data(link, &hpd_irq_dpcd_data);
3244         if (out_hpd_irq_dpcd_data)
3245                 *out_hpd_irq_dpcd_data = hpd_irq_dpcd_data;
3246
3247         if (result != DC_OK) {
3248                 DC_LOG_HW_HPD_IRQ("%s: DPCD read failed to obtain irq data\n",
3249                         __func__);
3250                 return false;
3251         }
3252
3253         if (hpd_irq_dpcd_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
3254                 device_service_clear.bits.AUTOMATED_TEST = 1;
3255                 core_link_write_dpcd(
3256                         link,
3257                         DP_DEVICE_SERVICE_IRQ_VECTOR,
3258                         &device_service_clear.raw,
3259                         sizeof(device_service_clear.raw));
3260                 device_service_clear.raw = 0;
3261                 handle_automated_test(link);
3262                 return false;
3263         }
3264
3265         if (!allow_hpd_rx_irq(link)) {
3266                 DC_LOG_HW_HPD_IRQ("%s: skipping HPD handling on %d\n",
3267                         __func__, link->link_index);
3268                 return false;
3269         }
3270
3271         if (handle_hpd_irq_psr_sink(link))
3272                 /* PSR-related error was detected and handled */
3273                 return true;
3274
3275         /* If PSR-related error handled, Main link may be off,
3276          * so do not handle as a normal sink status change interrupt.
3277          */
3278
3279         if (hpd_irq_dpcd_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY)
3280                 return true;
3281
3282         /* check if we have MST msg and return since we poll for it */
3283         if (hpd_irq_dpcd_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY)
3284                 return false;
3285
3286         /* For now we only handle 'Downstream port status' case.
3287          * If we got sink count changed it means
3288          * Downstream port status changed,
3289          * then DM should call DC to do the detection.
3290          * NOTE: Do not handle link loss on eDP since it is internal link*/
3291         if ((link->connector_signal != SIGNAL_TYPE_EDP) &&
3292                 hpd_rx_irq_check_link_loss_status(
3293                         link,
3294                         &hpd_irq_dpcd_data)) {
3295                 /* Connectivity log: link loss */
3296                 CONN_DATA_LINK_LOSS(link,
3297                                         hpd_irq_dpcd_data.raw,
3298                                         sizeof(hpd_irq_dpcd_data),
3299                                         "Status: ");
3300
3301                 for (i = 0; i < MAX_PIPES; i++) {
3302                         pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
3303                         if (pipe_ctx && pipe_ctx->stream && pipe_ctx->stream->link == link)
3304                                 break;
3305                 }
3306
3307                 if (pipe_ctx == NULL || pipe_ctx->stream == NULL)
3308                         return false;
3309
3310
3311                 for (i = 0; i < MAX_PIPES; i++) {
3312                         pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
3313                         if (pipe_ctx && pipe_ctx->stream && !pipe_ctx->stream->dpms_off &&
3314                                         pipe_ctx->stream->link == link && !pipe_ctx->prev_odm_pipe)
3315                                 core_link_disable_stream(pipe_ctx);
3316                 }
3317
3318                 for (i = 0; i < MAX_PIPES; i++) {
3319                         pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
3320                         if (pipe_ctx && pipe_ctx->stream && !pipe_ctx->stream->dpms_off &&
3321                                         pipe_ctx->stream->link == link && !pipe_ctx->prev_odm_pipe)
3322                                 core_link_enable_stream(link->dc->current_state, pipe_ctx);
3323                 }
3324
3325                 status = false;
3326                 if (out_link_loss)
3327                         *out_link_loss = true;
3328         }
3329
3330         if (link->type == dc_connection_sst_branch &&
3331                 hpd_irq_dpcd_data.bytes.sink_cnt.bits.SINK_COUNT
3332                         != link->dpcd_sink_count)
3333                 status = true;
3334
3335         /* reasons for HPD RX:
3336          * 1. Link Loss - ie Re-train the Link
3337          * 2. MST sideband message
3338          * 3. Automated Test - ie. Internal Commit
3339          * 4. CP (copy protection) - (not interesting for DM???)
3340          * 5. DRR
3341          * 6. Downstream Port status changed
3342          * -ie. Detect - this the only one
3343          * which is interesting for DM because
3344          * it must call dc_link_detect.
3345          */
3346         return status;
3347 }
3348
3349 /*query dpcd for version and mst cap addresses*/
3350 bool is_mst_supported(struct dc_link *link)
3351 {
3352         bool mst          = false;
3353         enum dc_status st = DC_OK;
3354         union dpcd_rev rev;
3355         union mstm_cap cap;
3356
3357         if (link->preferred_training_settings.mst_enable &&
3358                 *link->preferred_training_settings.mst_enable == false) {
3359                 return false;
3360         }
3361
3362         rev.raw  = 0;
3363         cap.raw  = 0;
3364
3365         st = core_link_read_dpcd(link, DP_DPCD_REV, &rev.raw,
3366                         sizeof(rev));
3367
3368         if (st == DC_OK && rev.raw >= DPCD_REV_12) {
3369
3370                 st = core_link_read_dpcd(link, DP_MSTM_CAP,
3371                                 &cap.raw, sizeof(cap));
3372                 if (st == DC_OK && cap.bits.MST_CAP == 1)
3373                         mst = true;
3374         }
3375         return mst;
3376
3377 }
3378
3379 bool is_dp_active_dongle(const struct dc_link *link)
3380 {
3381         return (link->dpcd_caps.dongle_type >= DISPLAY_DONGLE_DP_VGA_CONVERTER) &&
3382                                 (link->dpcd_caps.dongle_type <= DISPLAY_DONGLE_DP_HDMI_CONVERTER);
3383 }
3384
3385 bool is_dp_branch_device(const struct dc_link *link)
3386 {
3387         return link->dpcd_caps.is_branch_dev;
3388 }
3389
3390 static int translate_dpcd_max_bpc(enum dpcd_downstream_port_max_bpc bpc)
3391 {
3392         switch (bpc) {
3393         case DOWN_STREAM_MAX_8BPC:
3394                 return 8;
3395         case DOWN_STREAM_MAX_10BPC:
3396                 return 10;
3397         case DOWN_STREAM_MAX_12BPC:
3398                 return 12;
3399         case DOWN_STREAM_MAX_16BPC:
3400                 return 16;
3401         default:
3402                 break;
3403         }
3404
3405         return -1;
3406 }
3407
3408 static void read_dp_device_vendor_id(struct dc_link *link)
3409 {
3410         struct dp_device_vendor_id dp_id;
3411
3412         /* read IEEE branch device id */
3413         core_link_read_dpcd(
3414                 link,
3415                 DP_BRANCH_OUI,
3416                 (uint8_t *)&dp_id,
3417                 sizeof(dp_id));
3418
3419         link->dpcd_caps.branch_dev_id =
3420                 (dp_id.ieee_oui[0] << 16) +
3421                 (dp_id.ieee_oui[1] << 8) +
3422                 dp_id.ieee_oui[2];
3423
3424         memmove(
3425                 link->dpcd_caps.branch_dev_name,
3426                 dp_id.ieee_device_id,
3427                 sizeof(dp_id.ieee_device_id));
3428 }
3429
3430
3431
3432 static void get_active_converter_info(
3433         uint8_t data, struct dc_link *link)
3434 {
3435         union dp_downstream_port_present ds_port = { .byte = data };
3436         memset(&link->dpcd_caps.dongle_caps, 0, sizeof(link->dpcd_caps.dongle_caps));
3437
3438         /* decode converter info*/
3439         if (!ds_port.fields.PORT_PRESENT) {
3440                 link->dpcd_caps.dongle_type = DISPLAY_DONGLE_NONE;
3441                 ddc_service_set_dongle_type(link->ddc,
3442                                 link->dpcd_caps.dongle_type);
3443                 link->dpcd_caps.is_branch_dev = false;
3444                 return;
3445         }
3446
3447         /* DPCD 0x5 bit 0 = 1, it indicate it's branch device */
3448         link->dpcd_caps.is_branch_dev = ds_port.fields.PORT_PRESENT;
3449
3450         switch (ds_port.fields.PORT_TYPE) {
3451         case DOWNSTREAM_VGA:
3452                 link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_VGA_CONVERTER;
3453                 break;
3454         case DOWNSTREAM_DVI_HDMI_DP_PLUS_PLUS:
3455                 /* At this point we don't know is it DVI or HDMI or DP++,
3456                  * assume DVI.*/
3457                 link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_DVI_CONVERTER;
3458                 break;
3459         default:
3460                 link->dpcd_caps.dongle_type = DISPLAY_DONGLE_NONE;
3461                 break;
3462         }
3463
3464         if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_11) {
3465                 uint8_t det_caps[16]; /* CTS 4.2.2.7 expects source to read Detailed Capabilities Info : 00080h-0008F.*/
3466                 union dwnstream_port_caps_byte0 *port_caps =
3467                         (union dwnstream_port_caps_byte0 *)det_caps;
3468                 if (core_link_read_dpcd(link, DP_DOWNSTREAM_PORT_0,
3469                                 det_caps, sizeof(det_caps)) == DC_OK) {
3470
3471                         switch (port_caps->bits.DWN_STRM_PORTX_TYPE) {
3472                         /*Handle DP case as DONGLE_NONE*/
3473                         case DOWN_STREAM_DETAILED_DP:
3474                                 link->dpcd_caps.dongle_type = DISPLAY_DONGLE_NONE;
3475                                 break;
3476                         case DOWN_STREAM_DETAILED_VGA:
3477                                 link->dpcd_caps.dongle_type =
3478                                         DISPLAY_DONGLE_DP_VGA_CONVERTER;
3479                                 break;
3480                         case DOWN_STREAM_DETAILED_DVI:
3481                                 link->dpcd_caps.dongle_type =
3482                                         DISPLAY_DONGLE_DP_DVI_CONVERTER;
3483                                 break;
3484                         case DOWN_STREAM_DETAILED_HDMI:
3485                         case DOWN_STREAM_DETAILED_DP_PLUS_PLUS:
3486                                 /*Handle DP++ active converter case, process DP++ case as HDMI case according DP1.4 spec*/
3487                                 link->dpcd_caps.dongle_type =
3488                                         DISPLAY_DONGLE_DP_HDMI_CONVERTER;
3489
3490                                 link->dpcd_caps.dongle_caps.dongle_type = link->dpcd_caps.dongle_type;
3491                                 if (ds_port.fields.DETAILED_CAPS) {
3492
3493                                         union dwnstream_port_caps_byte3_hdmi
3494                                                 hdmi_caps = {.raw = det_caps[3] };
3495                                         union dwnstream_port_caps_byte2
3496                                                 hdmi_color_caps = {.raw = det_caps[2] };
3497                                         link->dpcd_caps.dongle_caps.dp_hdmi_max_pixel_clk_in_khz =
3498                                                 det_caps[1] * 2500;
3499
3500                                         link->dpcd_caps.dongle_caps.is_dp_hdmi_s3d_converter =
3501                                                 hdmi_caps.bits.FRAME_SEQ_TO_FRAME_PACK;
3502                                         /*YCBCR capability only for HDMI case*/
3503                                         if (port_caps->bits.DWN_STRM_PORTX_TYPE
3504                                                         == DOWN_STREAM_DETAILED_HDMI) {
3505                                                 link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr422_pass_through =
3506                                                                 hdmi_caps.bits.YCrCr422_PASS_THROUGH;
3507                                                 link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr420_pass_through =
3508                                                                 hdmi_caps.bits.YCrCr420_PASS_THROUGH;
3509                                                 link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr422_converter =
3510                                                                 hdmi_caps.bits.YCrCr422_CONVERSION;
3511                                                 link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr420_converter =
3512                                                                 hdmi_caps.bits.YCrCr420_CONVERSION;
3513                                         }
3514
3515                                         link->dpcd_caps.dongle_caps.dp_hdmi_max_bpc =
3516                                                 translate_dpcd_max_bpc(
3517                                                         hdmi_color_caps.bits.MAX_BITS_PER_COLOR_COMPONENT);
3518
3519                                         if (link->dpcd_caps.dongle_caps.dp_hdmi_max_pixel_clk_in_khz != 0)
3520                                                 link->dpcd_caps.dongle_caps.extendedCapValid = true;
3521                                 }
3522
3523                                 break;
3524                         }
3525                 }
3526         }
3527
3528         ddc_service_set_dongle_type(link->ddc, link->dpcd_caps.dongle_type);
3529
3530         {
3531                 struct dp_sink_hw_fw_revision dp_hw_fw_revision;
3532
3533                 core_link_read_dpcd(
3534                         link,
3535                         DP_BRANCH_REVISION_START,
3536                         (uint8_t *)&dp_hw_fw_revision,
3537                         sizeof(dp_hw_fw_revision));
3538
3539                 link->dpcd_caps.branch_hw_revision =
3540                         dp_hw_fw_revision.ieee_hw_rev;
3541
3542                 memmove(
3543                         link->dpcd_caps.branch_fw_revision,
3544                         dp_hw_fw_revision.ieee_fw_rev,
3545                         sizeof(dp_hw_fw_revision.ieee_fw_rev));
3546         }
3547 }
3548
3549 static void dp_wa_power_up_0010FA(struct dc_link *link, uint8_t *dpcd_data,
3550                 int length)
3551 {
3552         int retry = 0;
3553
3554         if (!link->dpcd_caps.dpcd_rev.raw) {
3555                 do {
3556                         dp_receiver_power_ctrl(link, true);
3557                         core_link_read_dpcd(link, DP_DPCD_REV,
3558                                                         dpcd_data, length);
3559                         link->dpcd_caps.dpcd_rev.raw = dpcd_data[
3560                                 DP_DPCD_REV -
3561                                 DP_DPCD_REV];
3562                 } while (retry++ < 4 && !link->dpcd_caps.dpcd_rev.raw);
3563         }
3564
3565         if (link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_VGA_CONVERTER) {
3566                 switch (link->dpcd_caps.branch_dev_id) {
3567                 /* 0010FA active dongles (DP-VGA, DP-DLDVI converters) power down
3568                  * all internal circuits including AUX communication preventing
3569                  * reading DPCD table and EDID (spec violation).
3570                  * Encoder will skip DP RX power down on disable_output to
3571                  * keep receiver powered all the time.*/
3572                 case DP_BRANCH_DEVICE_ID_0010FA:
3573                 case DP_BRANCH_DEVICE_ID_0080E1:
3574                 case DP_BRANCH_DEVICE_ID_00E04C:
3575                         link->wa_flags.dp_keep_receiver_powered = true;
3576                         break;
3577
3578                 /* TODO: May need work around for other dongles. */
3579                 default:
3580                         link->wa_flags.dp_keep_receiver_powered = false;
3581                         break;
3582                 }
3583         } else
3584                 link->wa_flags.dp_keep_receiver_powered = false;
3585 }
3586
3587 /* Read additional sink caps defined in source specific DPCD area
3588  * This function currently only reads from SinkCapability address (DP_SOURCE_SINK_CAP)
3589  */
3590 static bool dpcd_read_sink_ext_caps(struct dc_link *link)
3591 {
3592         uint8_t dpcd_data;
3593
3594         if (!link)
3595                 return false;
3596
3597         if (core_link_read_dpcd(link, DP_SOURCE_SINK_CAP, &dpcd_data, 1) != DC_OK)
3598                 return false;
3599
3600         link->dpcd_sink_ext_caps.raw = dpcd_data;
3601         return true;
3602 }
3603
3604 bool dp_retrieve_lttpr_cap(struct dc_link *link)
3605 {
3606         uint8_t lttpr_dpcd_data[6];
3607         bool vbios_lttpr_enable = false;
3608         bool vbios_lttpr_interop = false;
3609         struct dc_bios *bios = link->dc->ctx->dc_bios;
3610         enum dc_status status = DC_ERROR_UNEXPECTED;
3611         bool is_lttpr_present = false;
3612
3613         memset(lttpr_dpcd_data, '\0', sizeof(lttpr_dpcd_data));
3614         /* Query BIOS to determine if LTTPR functionality is forced on by system */
3615         if (bios->funcs->get_lttpr_caps) {
3616                 enum bp_result bp_query_result;
3617                 uint8_t is_vbios_lttpr_enable = 0;
3618
3619                 bp_query_result = bios->funcs->get_lttpr_caps(bios, &is_vbios_lttpr_enable);
3620                 vbios_lttpr_enable = (bp_query_result == BP_RESULT_OK) && !!is_vbios_lttpr_enable;
3621         }
3622
3623         if (bios->funcs->get_lttpr_interop) {
3624                 enum bp_result bp_query_result;
3625                 uint8_t is_vbios_interop_enabled = 0;
3626
3627                 bp_query_result = bios->funcs->get_lttpr_interop(bios, &is_vbios_interop_enabled);
3628                 vbios_lttpr_interop = (bp_query_result == BP_RESULT_OK) && !!is_vbios_interop_enabled;
3629         }
3630
3631         /*
3632          * Logic to determine LTTPR mode
3633          */
3634         link->lttpr_mode = LTTPR_MODE_NON_LTTPR;
3635         if (vbios_lttpr_enable && vbios_lttpr_interop)
3636                 link->lttpr_mode = LTTPR_MODE_NON_TRANSPARENT;
3637         else if (!vbios_lttpr_enable && vbios_lttpr_interop) {
3638                 if (link->dc->config.allow_lttpr_non_transparent_mode)
3639                         link->lttpr_mode = LTTPR_MODE_NON_TRANSPARENT;
3640                 else
3641                         link->lttpr_mode = LTTPR_MODE_TRANSPARENT;
3642         } else if (!vbios_lttpr_enable && !vbios_lttpr_interop) {
3643                 if (!link->dc->config.allow_lttpr_non_transparent_mode
3644                         || !link->dc->caps.extended_aux_timeout_support)
3645                         link->lttpr_mode = LTTPR_MODE_NON_LTTPR;
3646                 else
3647                         link->lttpr_mode = LTTPR_MODE_NON_TRANSPARENT;
3648         }
3649
3650         if (link->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT || link->lttpr_mode == LTTPR_MODE_TRANSPARENT) {
3651                 /* By reading LTTPR capability, RX assumes that we will enable
3652                  * LTTPR extended aux timeout if LTTPR is present.
3653                  */
3654                 status = core_link_read_dpcd(
3655                                 link,
3656                                 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
3657                                 lttpr_dpcd_data,
3658                                 sizeof(lttpr_dpcd_data));
3659                 if (status != DC_OK) {
3660                         dm_error("%s: Read LTTPR caps data failed.\n", __func__);
3661                         return false;
3662                 }
3663
3664                 link->dpcd_caps.lttpr_caps.revision.raw =
3665                                 lttpr_dpcd_data[DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV -
3666                                                                 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
3667
3668                 link->dpcd_caps.lttpr_caps.max_link_rate =
3669                                 lttpr_dpcd_data[DP_MAX_LINK_RATE_PHY_REPEATER -
3670                                                                 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
3671
3672                 link->dpcd_caps.lttpr_caps.phy_repeater_cnt =
3673                                 lttpr_dpcd_data[DP_PHY_REPEATER_CNT -
3674                                                                 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
3675
3676                 link->dpcd_caps.lttpr_caps.max_lane_count =
3677                                 lttpr_dpcd_data[DP_MAX_LANE_COUNT_PHY_REPEATER -
3678                                                                 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
3679
3680                 link->dpcd_caps.lttpr_caps.mode =
3681                                 lttpr_dpcd_data[DP_PHY_REPEATER_MODE -
3682                                                                 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
3683
3684                 link->dpcd_caps.lttpr_caps.max_ext_timeout =
3685                                 lttpr_dpcd_data[DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT -
3686                                                                 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
3687
3688                 /* Attempt to train in LTTPR transparent mode if repeater count exceeds 8. */
3689                 is_lttpr_present = (dp_convert_to_count(link->dpcd_caps.lttpr_caps.phy_repeater_cnt) != 0 &&
3690                                 link->dpcd_caps.lttpr_caps.max_lane_count > 0 &&
3691                                 link->dpcd_caps.lttpr_caps.max_lane_count <= 4 &&
3692                                 link->dpcd_caps.lttpr_caps.revision.raw >= 0x14);
3693                 if (is_lttpr_present) {
3694                         CONN_DATA_DETECT(link, lttpr_dpcd_data, sizeof(lttpr_dpcd_data), "LTTPR Caps: ");
3695                         configure_lttpr_mode_transparent(link);
3696                 } else
3697                         link->lttpr_mode = LTTPR_MODE_NON_LTTPR;
3698         }
3699         return is_lttpr_present;
3700 }
3701
3702 static bool retrieve_link_cap(struct dc_link *link)
3703 {
3704         /* DP_ADAPTER_CAP - DP_DPCD_REV + 1 == 16 and also DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT + 1 == 16,
3705          * which means size 16 will be good for both of those DPCD register block reads
3706          */
3707         uint8_t dpcd_data[16];
3708         /*Only need to read 1 byte starting from DP_DPRX_FEATURE_ENUMERATION_LIST.
3709          */
3710         uint8_t dpcd_dprx_data = '\0';
3711         uint8_t dpcd_power_state = '\0';
3712
3713         struct dp_device_vendor_id sink_id;
3714         union down_stream_port_count down_strm_port_count;
3715         union edp_configuration_cap edp_config_cap;
3716         union dp_downstream_port_present ds_port = { 0 };
3717         enum dc_status status = DC_ERROR_UNEXPECTED;
3718         uint32_t read_dpcd_retry_cnt = 3;
3719         int i;
3720         struct dp_sink_hw_fw_revision dp_hw_fw_revision;
3721         const uint32_t post_oui_delay = 30; // 30ms
3722         bool is_lttpr_present = false;
3723
3724         memset(dpcd_data, '\0', sizeof(dpcd_data));
3725         memset(&down_strm_port_count,
3726                 '\0', sizeof(union down_stream_port_count));
3727         memset(&edp_config_cap, '\0',
3728                 sizeof(union edp_configuration_cap));
3729
3730         /* if extended timeout is supported in hardware,
3731          * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
3732          * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
3733          */
3734         dc_link_aux_try_to_configure_timeout(link->ddc,
3735                         LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
3736
3737         is_lttpr_present = dp_retrieve_lttpr_cap(link);
3738
3739         status = core_link_read_dpcd(link, DP_SET_POWER,
3740                         &dpcd_power_state, sizeof(dpcd_power_state));
3741
3742         /* Delay 1 ms if AUX CH is in power down state. Based on spec
3743          * section 2.3.1.2, if AUX CH may be powered down due to
3744          * write to DPCD 600h = 2. Sink AUX CH is monitoring differential
3745          * signal and may need up to 1 ms before being able to reply.
3746          */
3747         if (status != DC_OK || dpcd_power_state == DP_SET_POWER_D3)
3748                 udelay(1000);
3749
3750         dpcd_set_source_specific_data(link);
3751         /* Sink may need to configure internals based on vendor, so allow some
3752          * time before proceeding with possibly vendor specific transactions
3753          */
3754         msleep(post_oui_delay);
3755
3756         for (i = 0; i < read_dpcd_retry_cnt; i++) {
3757                 status = core_link_read_dpcd(
3758                                 link,
3759                                 DP_DPCD_REV,
3760                                 dpcd_data,
3761                                 sizeof(dpcd_data));
3762                 if (status == DC_OK)
3763                         break;
3764         }
3765
3766         if (status != DC_OK) {
3767                 dm_error("%s: Read receiver caps dpcd data failed.\n", __func__);
3768                 return false;
3769         }
3770
3771         if (!is_lttpr_present)
3772                 dc_link_aux_try_to_configure_timeout(link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
3773
3774         {
3775                 union training_aux_rd_interval aux_rd_interval;
3776
3777                 aux_rd_interval.raw =
3778                         dpcd_data[DP_TRAINING_AUX_RD_INTERVAL];
3779
3780                 link->dpcd_caps.ext_receiver_cap_field_present =
3781                                 aux_rd_interval.bits.EXT_RECEIVER_CAP_FIELD_PRESENT == 1;
3782
3783                 if (aux_rd_interval.bits.EXT_RECEIVER_CAP_FIELD_PRESENT == 1) {
3784                         uint8_t ext_cap_data[16];
3785
3786                         memset(ext_cap_data, '\0', sizeof(ext_cap_data));
3787                         for (i = 0; i < read_dpcd_retry_cnt; i++) {
3788                                 status = core_link_read_dpcd(
3789                                 link,
3790                                 DP_DP13_DPCD_REV,
3791                                 ext_cap_data,
3792                                 sizeof(ext_cap_data));
3793                                 if (status == DC_OK) {
3794                                         memcpy(dpcd_data, ext_cap_data, sizeof(dpcd_data));
3795                                         break;
3796                                 }
3797                         }
3798                         if (status != DC_OK)
3799                                 dm_error("%s: Read extend caps data failed, use cap from dpcd 0.\n", __func__);
3800                 }
3801         }
3802
3803         link->dpcd_caps.dpcd_rev.raw =
3804                         dpcd_data[DP_DPCD_REV - DP_DPCD_REV];
3805
3806         if (link->dpcd_caps.ext_receiver_cap_field_present) {
3807                 for (i = 0; i < read_dpcd_retry_cnt; i++) {
3808                         status = core_link_read_dpcd(
3809                                         link,
3810                                         DP_DPRX_FEATURE_ENUMERATION_LIST,
3811                                         &dpcd_dprx_data,
3812                                         sizeof(dpcd_dprx_data));
3813                         if (status == DC_OK)
3814                                 break;
3815                 }
3816
3817                 link->dpcd_caps.dprx_feature.raw = dpcd_dprx_data;
3818
3819                 if (status != DC_OK)
3820                         dm_error("%s: Read DPRX caps data failed.\n", __func__);
3821         }
3822
3823         else {
3824                 link->dpcd_caps.dprx_feature.raw = 0;
3825         }
3826
3827
3828         /* Error condition checking...
3829          * It is impossible for Sink to report Max Lane Count = 0.
3830          * It is possible for Sink to report Max Link Rate = 0, if it is
3831          * an eDP device that is reporting specialized link rates in the
3832          * SUPPORTED_LINK_RATE table.
3833          */
3834         if (dpcd_data[DP_MAX_LANE_COUNT - DP_DPCD_REV] == 0)
3835                 return false;
3836
3837         ds_port.byte = dpcd_data[DP_DOWNSTREAMPORT_PRESENT -
3838                                  DP_DPCD_REV];
3839
3840         read_dp_device_vendor_id(link);
3841
3842         get_active_converter_info(ds_port.byte, link);
3843
3844         dp_wa_power_up_0010FA(link, dpcd_data, sizeof(dpcd_data));
3845
3846         down_strm_port_count.raw = dpcd_data[DP_DOWN_STREAM_PORT_COUNT -
3847                                  DP_DPCD_REV];
3848
3849         link->dpcd_caps.allow_invalid_MSA_timing_param =
3850                 down_strm_port_count.bits.IGNORE_MSA_TIMING_PARAM;
3851
3852         link->dpcd_caps.max_ln_count.raw = dpcd_data[
3853                 DP_MAX_LANE_COUNT - DP_DPCD_REV];
3854
3855         link->dpcd_caps.max_down_spread.raw = dpcd_data[
3856                 DP_MAX_DOWNSPREAD - DP_DPCD_REV];
3857
3858         link->reported_link_cap.lane_count =
3859                 link->dpcd_caps.max_ln_count.bits.MAX_LANE_COUNT;
3860         link->reported_link_cap.link_rate = dpcd_data[
3861                 DP_MAX_LINK_RATE - DP_DPCD_REV];
3862         link->reported_link_cap.link_spread =
3863                 link->dpcd_caps.max_down_spread.bits.MAX_DOWN_SPREAD ?
3864                 LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
3865
3866         edp_config_cap.raw = dpcd_data[
3867                 DP_EDP_CONFIGURATION_CAP - DP_DPCD_REV];
3868         link->dpcd_caps.panel_mode_edp =
3869                 edp_config_cap.bits.ALT_SCRAMBLER_RESET;
3870         link->dpcd_caps.dpcd_display_control_capable =
3871                 edp_config_cap.bits.DPCD_DISPLAY_CONTROL_CAPABLE;
3872
3873         link->test_pattern_enabled = false;
3874         link->compliance_test_state.raw = 0;
3875
3876         /* read sink count */
3877         core_link_read_dpcd(link,
3878                         DP_SINK_COUNT,
3879                         &link->dpcd_caps.sink_count.raw,
3880                         sizeof(link->dpcd_caps.sink_count.raw));
3881
3882         /* read sink ieee oui */
3883         core_link_read_dpcd(link,
3884                         DP_SINK_OUI,
3885                         (uint8_t *)(&sink_id),
3886                         sizeof(sink_id));
3887
3888         link->dpcd_caps.sink_dev_id =
3889                         (sink_id.ieee_oui[0] << 16) +
3890                         (sink_id.ieee_oui[1] << 8) +
3891                         (sink_id.ieee_oui[2]);
3892
3893         memmove(
3894                 link->dpcd_caps.sink_dev_id_str,
3895                 sink_id.ieee_device_id,
3896                 sizeof(sink_id.ieee_device_id));
3897
3898         /* Quirk Apple MBP 2017 15" Retina panel: Wrong DP_MAX_LINK_RATE */
3899         {
3900                 uint8_t str_mbp_2017[] = { 101, 68, 21, 101, 98, 97 };
3901
3902                 if ((link->dpcd_caps.sink_dev_id == 0x0010fa) &&
3903                     !memcmp(link->dpcd_caps.sink_dev_id_str, str_mbp_2017,
3904                             sizeof(str_mbp_2017))) {
3905                         link->reported_link_cap.link_rate = 0x0c;
3906                 }
3907         }
3908
3909         core_link_read_dpcd(
3910                 link,
3911                 DP_SINK_HW_REVISION_START,
3912                 (uint8_t *)&dp_hw_fw_revision,
3913                 sizeof(dp_hw_fw_revision));
3914
3915         link->dpcd_caps.sink_hw_revision =
3916                 dp_hw_fw_revision.ieee_hw_rev;
3917
3918         memmove(
3919                 link->dpcd_caps.sink_fw_revision,
3920                 dp_hw_fw_revision.ieee_fw_rev,
3921                 sizeof(dp_hw_fw_revision.ieee_fw_rev));
3922
3923         memset(&link->dpcd_caps.dsc_caps, '\0',
3924                         sizeof(link->dpcd_caps.dsc_caps));
3925         memset(&link->dpcd_caps.fec_cap, '\0', sizeof(link->dpcd_caps.fec_cap));
3926         /* Read DSC and FEC sink capabilities if DP revision is 1.4 and up */
3927         if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_14) {
3928                 status = core_link_read_dpcd(
3929                                 link,
3930                                 DP_FEC_CAPABILITY,
3931                                 &link->dpcd_caps.fec_cap.raw,
3932                                 sizeof(link->dpcd_caps.fec_cap.raw));
3933                 status = core_link_read_dpcd(
3934                                 link,
3935                                 DP_DSC_SUPPORT,
3936                                 link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
3937                                 sizeof(link->dpcd_caps.dsc_caps.dsc_basic_caps.raw));
3938                 status = core_link_read_dpcd(
3939                                 link,
3940                                 DP_DSC_BRANCH_OVERALL_THROUGHPUT_0,
3941                                 link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
3942                                 sizeof(link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw));
3943         }
3944
3945         if (!dpcd_read_sink_ext_caps(link))
3946                 link->dpcd_sink_ext_caps.raw = 0;
3947
3948         /* Connectivity log: detection */
3949         CONN_DATA_DETECT(link, dpcd_data, sizeof(dpcd_data), "Rx Caps: ");
3950
3951         return true;
3952 }
3953
3954 bool dp_overwrite_extended_receiver_cap(struct dc_link *link)
3955 {
3956         uint8_t dpcd_data[16];
3957         uint32_t read_dpcd_retry_cnt = 3;
3958         enum dc_status status = DC_ERROR_UNEXPECTED;
3959         union dp_downstream_port_present ds_port = { 0 };
3960         union down_stream_port_count down_strm_port_count;
3961         union edp_configuration_cap edp_config_cap;
3962
3963         int i;
3964
3965         for (i = 0; i < read_dpcd_retry_cnt; i++) {
3966                 status = core_link_read_dpcd(
3967                                 link,
3968                                 DP_DPCD_REV,
3969                                 dpcd_data,
3970                                 sizeof(dpcd_data));
3971                 if (status == DC_OK)
3972                         break;
3973         }
3974
3975         link->dpcd_caps.dpcd_rev.raw =
3976                 dpcd_data[DP_DPCD_REV - DP_DPCD_REV];
3977
3978         if (dpcd_data[DP_MAX_LANE_COUNT - DP_DPCD_REV] == 0)
3979                 return false;
3980
3981         ds_port.byte = dpcd_data[DP_DOWNSTREAMPORT_PRESENT -
3982                         DP_DPCD_REV];
3983
3984         get_active_converter_info(ds_port.byte, link);
3985
3986         down_strm_port_count.raw = dpcd_data[DP_DOWN_STREAM_PORT_COUNT -
3987                         DP_DPCD_REV];
3988
3989         link->dpcd_caps.allow_invalid_MSA_timing_param =
3990                 down_strm_port_count.bits.IGNORE_MSA_TIMING_PARAM;
3991
3992         link->dpcd_caps.max_ln_count.raw = dpcd_data[
3993                 DP_MAX_LANE_COUNT - DP_DPCD_REV];
3994
3995         link->dpcd_caps.max_down_spread.raw = dpcd_data[
3996                 DP_MAX_DOWNSPREAD - DP_DPCD_REV];
3997
3998         link->reported_link_cap.lane_count =
3999                 link->dpcd_caps.max_ln_count.bits.MAX_LANE_COUNT;
4000         link->reported_link_cap.link_rate = dpcd_data[
4001                 DP_MAX_LINK_RATE - DP_DPCD_REV];
4002         link->reported_link_cap.link_spread =
4003                 link->dpcd_caps.max_down_spread.bits.MAX_DOWN_SPREAD ?
4004                 LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
4005
4006         edp_config_cap.raw = dpcd_data[
4007                 DP_EDP_CONFIGURATION_CAP - DP_DPCD_REV];
4008         link->dpcd_caps.panel_mode_edp =
4009                 edp_config_cap.bits.ALT_SCRAMBLER_RESET;
4010         link->dpcd_caps.dpcd_display_control_capable =
4011                 edp_config_cap.bits.DPCD_DISPLAY_CONTROL_CAPABLE;
4012
4013         return true;
4014 }
4015
4016 bool detect_dp_sink_caps(struct dc_link *link)
4017 {
4018         return retrieve_link_cap(link);
4019
4020         /* dc init_hw has power encoder using default
4021          * signal for connector. For native DP, no
4022          * need to power up encoder again. If not native
4023          * DP, hw_init may need check signal or power up
4024          * encoder here.
4025          */
4026         /* TODO save sink caps in link->sink */
4027 }
4028
4029 static enum dc_link_rate linkRateInKHzToLinkRateMultiplier(uint32_t link_rate_in_khz)
4030 {
4031         enum dc_link_rate link_rate;
4032         // LinkRate is normally stored as a multiplier of 0.27 Gbps per lane. Do the translation.
4033         switch (link_rate_in_khz) {
4034         case 1620000:
4035                 link_rate = LINK_RATE_LOW;              // Rate_1 (RBR)         - 1.62 Gbps/Lane
4036                 break;
4037         case 2160000:
4038                 link_rate = LINK_RATE_RATE_2;   // Rate_2                       - 2.16 Gbps/Lane
4039                 break;
4040         case 2430000:
4041                 link_rate = LINK_RATE_RATE_3;   // Rate_3                       - 2.43 Gbps/Lane
4042                 break;
4043         case 2700000:
4044                 link_rate = LINK_RATE_HIGH;             // Rate_4 (HBR)         - 2.70 Gbps/Lane
4045                 break;
4046         case 3240000:
4047                 link_rate = LINK_RATE_RBR2;             // Rate_5 (RBR2)        - 3.24 Gbps/Lane
4048                 break;
4049         case 4320000:
4050                 link_rate = LINK_RATE_RATE_6;   // Rate_6                       - 4.32 Gbps/Lane
4051                 break;
4052         case 5400000:
4053                 link_rate = LINK_RATE_HIGH2;    // Rate_7 (HBR2)        - 5.40 Gbps/Lane
4054                 break;
4055         case 8100000:
4056                 link_rate = LINK_RATE_HIGH3;    // Rate_8 (HBR3)        - 8.10 Gbps/Lane
4057                 break;
4058         default:
4059                 link_rate = LINK_RATE_UNKNOWN;
4060                 break;
4061         }
4062         return link_rate;
4063 }
4064
4065 void detect_edp_sink_caps(struct dc_link *link)
4066 {
4067         uint8_t supported_link_rates[16];
4068         uint32_t entry;
4069         uint32_t link_rate_in_khz;
4070         enum dc_link_rate link_rate = LINK_RATE_UNKNOWN;
4071         uint8_t backlight_adj_cap;
4072
4073         retrieve_link_cap(link);
4074         link->dpcd_caps.edp_supported_link_rates_count = 0;
4075         memset(supported_link_rates, 0, sizeof(supported_link_rates));
4076
4077         /*
4078          * edp_supported_link_rates_count is only valid for eDP v1.4 or higher.
4079          * Per VESA eDP spec, "The DPCD revision for eDP v1.4 is 13h"
4080          */
4081         if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_13 &&
4082                         (link->dc->debug.optimize_edp_link_rate ||
4083                         link->reported_link_cap.link_rate == LINK_RATE_UNKNOWN)) {
4084                 // Read DPCD 00010h - 0001Fh 16 bytes at one shot
4085                 core_link_read_dpcd(link, DP_SUPPORTED_LINK_RATES,
4086                                                         supported_link_rates, sizeof(supported_link_rates));
4087
4088                 for (entry = 0; entry < 16; entry += 2) {
4089                         // DPCD register reports per-lane link rate = 16-bit link rate capability
4090                         // value X 200 kHz. Need multiplier to find link rate in kHz.
4091                         link_rate_in_khz = (supported_link_rates[entry+1] * 0x100 +
4092                                                                                 supported_link_rates[entry]) * 200;
4093
4094                         if (link_rate_in_khz != 0) {
4095                                 link_rate = linkRateInKHzToLinkRateMultiplier(link_rate_in_khz);
4096                                 link->dpcd_caps.edp_supported_link_rates[link->dpcd_caps.edp_supported_link_rates_count] = link_rate;
4097                                 link->dpcd_caps.edp_supported_link_rates_count++;
4098
4099                                 if (link->reported_link_cap.link_rate < link_rate)
4100                                         link->reported_link_cap.link_rate = link_rate;
4101                         }
4102                 }
4103         }
4104         link->verified_link_cap = link->reported_link_cap;
4105
4106         core_link_read_dpcd(link, DP_EDP_BACKLIGHT_ADJUSTMENT_CAP,
4107                                                 &backlight_adj_cap, sizeof(backlight_adj_cap));
4108
4109         link->dpcd_caps.dynamic_backlight_capable_edp =
4110                                 (backlight_adj_cap & DP_EDP_DYNAMIC_BACKLIGHT_CAP) ? true:false;
4111
4112         dc_link_set_default_brightness_aux(link);
4113 }
4114
4115 void dc_link_dp_enable_hpd(const struct dc_link *link)
4116 {
4117         struct link_encoder *encoder = link->link_enc;
4118
4119         if (encoder != NULL && encoder->funcs->enable_hpd != NULL)
4120                 encoder->funcs->enable_hpd(encoder);
4121 }
4122
4123 void dc_link_dp_disable_hpd(const struct dc_link *link)
4124 {
4125         struct link_encoder *encoder = link->link_enc;
4126
4127         if (encoder != NULL && encoder->funcs->enable_hpd != NULL)
4128                 encoder->funcs->disable_hpd(encoder);
4129 }
4130
4131 static bool is_dp_phy_pattern(enum dp_test_pattern test_pattern)
4132 {
4133         if ((DP_TEST_PATTERN_PHY_PATTERN_BEGIN <= test_pattern &&
4134                         test_pattern <= DP_TEST_PATTERN_PHY_PATTERN_END) ||
4135                         test_pattern == DP_TEST_PATTERN_VIDEO_MODE)
4136                 return true;
4137         else
4138                 return false;
4139 }
4140
4141 static void set_crtc_test_pattern(struct dc_link *link,
4142                                 struct pipe_ctx *pipe_ctx,
4143                                 enum dp_test_pattern test_pattern,
4144                                 enum dp_test_pattern_color_space test_pattern_color_space)
4145 {
4146         enum controller_dp_test_pattern controller_test_pattern;
4147         enum dc_color_depth color_depth = pipe_ctx->
4148                 stream->timing.display_color_depth;
4149         struct bit_depth_reduction_params params;
4150         struct output_pixel_processor *opp = pipe_ctx->stream_res.opp;
4151         int width = pipe_ctx->stream->timing.h_addressable +
4152                 pipe_ctx->stream->timing.h_border_left +
4153                 pipe_ctx->stream->timing.h_border_right;
4154         int height = pipe_ctx->stream->timing.v_addressable +
4155                 pipe_ctx->stream->timing.v_border_bottom +
4156                 pipe_ctx->stream->timing.v_border_top;
4157
4158         memset(&params, 0, sizeof(params));
4159
4160         switch (test_pattern) {
4161         case DP_TEST_PATTERN_COLOR_SQUARES:
4162                 controller_test_pattern =
4163                                 CONTROLLER_DP_TEST_PATTERN_COLORSQUARES;
4164         break;
4165         case DP_TEST_PATTERN_COLOR_SQUARES_CEA:
4166                 controller_test_pattern =
4167                                 CONTROLLER_DP_TEST_PATTERN_COLORSQUARES_CEA;
4168         break;
4169         case DP_TEST_PATTERN_VERTICAL_BARS:
4170                 controller_test_pattern =
4171                                 CONTROLLER_DP_TEST_PATTERN_VERTICALBARS;
4172         break;
4173         case DP_TEST_PATTERN_HORIZONTAL_BARS:
4174                 controller_test_pattern =
4175                                 CONTROLLER_DP_TEST_PATTERN_HORIZONTALBARS;
4176         break;
4177         case DP_TEST_PATTERN_COLOR_RAMP:
4178                 controller_test_pattern =
4179                                 CONTROLLER_DP_TEST_PATTERN_COLORRAMP;
4180         break;
4181         default:
4182                 controller_test_pattern =
4183                                 CONTROLLER_DP_TEST_PATTERN_VIDEOMODE;
4184         break;
4185         }
4186
4187         switch (test_pattern) {
4188         case DP_TEST_PATTERN_COLOR_SQUARES:
4189         case DP_TEST_PATTERN_COLOR_SQUARES_CEA:
4190         case DP_TEST_PATTERN_VERTICAL_BARS:
4191         case DP_TEST_PATTERN_HORIZONTAL_BARS:
4192         case DP_TEST_PATTERN_COLOR_RAMP:
4193         {
4194                 /* disable bit depth reduction */
4195                 pipe_ctx->stream->bit_depth_params = params;
4196                 opp->funcs->opp_program_bit_depth_reduction(opp, &params);
4197                 if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
4198                         pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
4199                                 controller_test_pattern, color_depth);
4200                 else if (link->dc->hwss.set_disp_pattern_generator) {
4201                         struct pipe_ctx *odm_pipe;
4202                         enum controller_dp_color_space controller_color_space;
4203                         int opp_cnt = 1;
4204                         int offset = 0;
4205                         int dpg_width = width;
4206
4207                         switch (test_pattern_color_space) {
4208                         case DP_TEST_PATTERN_COLOR_SPACE_RGB:
4209                                 controller_color_space = CONTROLLER_DP_COLOR_SPACE_RGB;
4210                                 break;
4211                         case DP_TEST_PATTERN_COLOR_SPACE_YCBCR601:
4212                                 controller_color_space = CONTROLLER_DP_COLOR_SPACE_YCBCR601;
4213                                 break;
4214                         case DP_TEST_PATTERN_COLOR_SPACE_YCBCR709:
4215                                 controller_color_space = CONTROLLER_DP_COLOR_SPACE_YCBCR709;
4216                                 break;
4217                         case DP_TEST_PATTERN_COLOR_SPACE_UNDEFINED:
4218                         default:
4219                                 controller_color_space = CONTROLLER_DP_COLOR_SPACE_UDEFINED;
4220                                 DC_LOG_ERROR("%s: Color space must be defined for test pattern", __func__);
4221                                 ASSERT(0);
4222                                 break;
4223                         }
4224
4225                         for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
4226                                 opp_cnt++;
4227                         dpg_width = width / opp_cnt;
4228                         offset = dpg_width;
4229
4230                         link->dc->hwss.set_disp_pattern_generator(link->dc,
4231                                         pipe_ctx,
4232                                         controller_test_pattern,
4233                                         controller_color_space,
4234                                         color_depth,
4235                                         NULL,
4236                                         dpg_width,
4237                                         height,
4238                                         0);
4239
4240                         for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
4241                                 struct output_pixel_processor *odm_opp = odm_pipe->stream_res.opp;
4242
4243                                 odm_opp->funcs->opp_program_bit_depth_reduction(odm_opp, &params);
4244                                 link->dc->hwss.set_disp_pattern_generator(link->dc,
4245                                                 odm_pipe,
4246                                                 controller_test_pattern,
4247                                                 controller_color_space,
4248                                                 color_depth,
4249                                                 NULL,
4250                                                 dpg_width,
4251                                                 height,
4252                                                 offset);
4253                                 offset += offset;
4254                         }
4255                 }
4256         }
4257         break;
4258         case DP_TEST_PATTERN_VIDEO_MODE:
4259         {
4260                 /* restore bitdepth reduction */
4261                 resource_build_bit_depth_reduction_params(pipe_ctx->stream, &params);
4262                 pipe_ctx->stream->bit_depth_params = params;
4263                 opp->funcs->opp_program_bit_depth_reduction(opp, &params);
4264                 if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
4265                         pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
4266                                 CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
4267                                 color_depth);
4268                 else if (link->dc->hwss.set_disp_pattern_generator) {
4269                         struct pipe_ctx *odm_pipe;
4270                         int opp_cnt = 1;
4271                         int dpg_width = width;
4272
4273                         for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
4274                                 opp_cnt++;
4275
4276                         dpg_width = width / opp_cnt;
4277                         for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
4278                                 struct output_pixel_processor *odm_opp = odm_pipe->stream_res.opp;
4279
4280                                 odm_opp->funcs->opp_program_bit_depth_reduction(odm_opp, &params);
4281                                 link->dc->hwss.set_disp_pattern_generator(link->dc,
4282                                                 odm_pipe,
4283                                                 CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
4284                                                 CONTROLLER_DP_COLOR_SPACE_UDEFINED,
4285                                                 color_depth,
4286                                                 NULL,
4287                                                 dpg_width,
4288                                                 height,
4289                                                 0);
4290                         }
4291                         link->dc->hwss.set_disp_pattern_generator(link->dc,
4292                                         pipe_ctx,
4293                                         CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
4294                                         CONTROLLER_DP_COLOR_SPACE_UDEFINED,
4295                                         color_depth,
4296                                         NULL,
4297                                         dpg_width,
4298                                         height,
4299                                         0);
4300                 }
4301         }
4302         break;
4303
4304         default:
4305         break;
4306         }
4307 }
4308
4309 bool dc_link_dp_set_test_pattern(
4310         struct dc_link *link,
4311         enum dp_test_pattern test_pattern,
4312         enum dp_test_pattern_color_space test_pattern_color_space,
4313         const struct link_training_settings *p_link_settings,
4314         const unsigned char *p_custom_pattern,
4315         unsigned int cust_pattern_size)
4316 {
4317         struct pipe_ctx *pipes = link->dc->current_state->res_ctx.pipe_ctx;
4318         struct pipe_ctx *pipe_ctx = NULL;
4319         unsigned int lane;
4320         unsigned int i;
4321         unsigned char link_qual_pattern[LANE_COUNT_DP_MAX] = {0};
4322         union dpcd_training_pattern training_pattern;
4323         enum dpcd_phy_test_patterns pattern;
4324
4325         memset(&training_pattern, 0, sizeof(training_pattern));
4326
4327         for (i = 0; i < MAX_PIPES; i++) {
4328                 if (pipes[i].stream == NULL)
4329                         continue;
4330
4331                 if (pipes[i].stream->link == link && !pipes[i].top_pipe && !pipes[i].prev_odm_pipe) {
4332                         pipe_ctx = &pipes[i];
4333                         break;
4334                 }
4335         }
4336
4337         if (pipe_ctx == NULL)
4338                 return false;
4339
4340         /* Reset CRTC Test Pattern if it is currently running and request is VideoMode */
4341         if (link->test_pattern_enabled && test_pattern ==
4342                         DP_TEST_PATTERN_VIDEO_MODE) {
4343                 /* Set CRTC Test Pattern */
4344                 set_crtc_test_pattern(link, pipe_ctx, test_pattern, test_pattern_color_space);
4345                 dp_set_hw_test_pattern(link, test_pattern,
4346                                 (uint8_t *)p_custom_pattern,
4347                                 (uint32_t)cust_pattern_size);
4348
4349                 /* Unblank Stream */
4350                 link->dc->hwss.unblank_stream(
4351                         pipe_ctx,
4352                         &link->verified_link_cap);
4353                 /* TODO:m_pHwss->MuteAudioEndpoint
4354                  * (pPathMode->pDisplayPath, false);
4355                  */
4356
4357                 /* Reset Test Pattern state */
4358                 link->test_pattern_enabled = false;
4359
4360                 return true;
4361         }
4362
4363         /* Check for PHY Test Patterns */
4364         if (is_dp_phy_pattern(test_pattern)) {
4365                 /* Set DPCD Lane Settings before running test pattern */
4366                 if (p_link_settings != NULL) {
4367                         dp_set_hw_lane_settings(link, p_link_settings, DPRX);
4368                         dpcd_set_lane_settings(link, p_link_settings, DPRX);
4369                 }
4370
4371                 /* Blank stream if running test pattern */
4372                 if (test_pattern != DP_TEST_PATTERN_VIDEO_MODE) {
4373                         /*TODO:
4374                          * m_pHwss->
4375                          * MuteAudioEndpoint(pPathMode->pDisplayPath, true);
4376                          */
4377                         /* Blank stream */
4378                         pipes->stream_res.stream_enc->funcs->dp_blank(pipe_ctx->stream_res.stream_enc);
4379                 }
4380
4381                 dp_set_hw_test_pattern(link, test_pattern,
4382                                 (uint8_t *)p_custom_pattern,
4383                                 (uint32_t)cust_pattern_size);
4384
4385                 if (test_pattern != DP_TEST_PATTERN_VIDEO_MODE) {
4386                         /* Set Test Pattern state */
4387                         link->test_pattern_enabled = true;
4388                         if (p_link_settings != NULL)
4389                                 dpcd_set_link_settings(link,
4390                                                 p_link_settings);
4391                 }
4392
4393                 switch (test_pattern) {
4394                 case DP_TEST_PATTERN_VIDEO_MODE:
4395                         pattern = PHY_TEST_PATTERN_NONE;
4396                         break;
4397                 case DP_TEST_PATTERN_D102:
4398                         pattern = PHY_TEST_PATTERN_D10_2;
4399                         break;
4400                 case DP_TEST_PATTERN_SYMBOL_ERROR:
4401                         pattern = PHY_TEST_PATTERN_SYMBOL_ERROR;
4402                         break;
4403                 case DP_TEST_PATTERN_PRBS7:
4404                         pattern = PHY_TEST_PATTERN_PRBS7;
4405                         break;
4406                 case DP_TEST_PATTERN_80BIT_CUSTOM:
4407                         pattern = PHY_TEST_PATTERN_80BIT_CUSTOM;
4408                         break;
4409                 case DP_TEST_PATTERN_CP2520_1:
4410                         pattern = PHY_TEST_PATTERN_CP2520_1;
4411                         break;
4412                 case DP_TEST_PATTERN_CP2520_2:
4413                         pattern = PHY_TEST_PATTERN_CP2520_2;
4414                         break;
4415                 case DP_TEST_PATTERN_CP2520_3:
4416                         pattern = PHY_TEST_PATTERN_CP2520_3;
4417                         break;
4418                 default:
4419                         return false;
4420                 }
4421
4422                 if (test_pattern == DP_TEST_PATTERN_VIDEO_MODE
4423                 /*TODO:&& !pPathMode->pDisplayPath->IsTargetPoweredOn()*/)
4424                         return false;
4425
4426                 if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_12) {
4427                         /* tell receiver that we are sending qualification
4428                          * pattern DP 1.2 or later - DP receiver's link quality
4429                          * pattern is set using DPCD LINK_QUAL_LANEx_SET
4430                          * register (0x10B~0x10E)\
4431                          */
4432                         for (lane = 0; lane < LANE_COUNT_DP_MAX; lane++)
4433                                 link_qual_pattern[lane] =
4434                                                 (unsigned char)(pattern);
4435
4436                         core_link_write_dpcd(link,
4437                                         DP_LINK_QUAL_LANE0_SET,
4438                                         link_qual_pattern,
4439                                         sizeof(link_qual_pattern));
4440                 } else if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_10 ||
4441                            link->dpcd_caps.dpcd_rev.raw == 0) {
4442                         /* tell receiver that we are sending qualification
4443                          * pattern DP 1.1a or earlier - DP receiver's link
4444                          * quality pattern is set using
4445                          * DPCD TRAINING_PATTERN_SET -> LINK_QUAL_PATTERN_SET
4446                          * register (0x102). We will use v_1.3 when we are
4447                          * setting test pattern for DP 1.1.
4448                          */
4449                         core_link_read_dpcd(link, DP_TRAINING_PATTERN_SET,
4450                                             &training_pattern.raw,
4451                                             sizeof(training_pattern));
4452                         training_pattern.v1_3.LINK_QUAL_PATTERN_SET = pattern;
4453                         core_link_write_dpcd(link, DP_TRAINING_PATTERN_SET,
4454                                              &training_pattern.raw,
4455                                              sizeof(training_pattern));
4456                 }
4457         } else {
4458                 enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
4459
4460                 switch (test_pattern_color_space) {
4461                 case DP_TEST_PATTERN_COLOR_SPACE_RGB:
4462                         color_space = COLOR_SPACE_SRGB;
4463                         if (test_pattern == DP_TEST_PATTERN_COLOR_SQUARES_CEA)
4464                                 color_space = COLOR_SPACE_SRGB_LIMITED;
4465                         break;
4466
4467                 case DP_TEST_PATTERN_COLOR_SPACE_YCBCR601:
4468                         color_space = COLOR_SPACE_YCBCR601;
4469                         if (test_pattern == DP_TEST_PATTERN_COLOR_SQUARES_CEA)
4470                                 color_space = COLOR_SPACE_YCBCR601_LIMITED;
4471                         break;
4472                 case DP_TEST_PATTERN_COLOR_SPACE_YCBCR709:
4473                         color_space = COLOR_SPACE_YCBCR709;
4474                         if (test_pattern == DP_TEST_PATTERN_COLOR_SQUARES_CEA)
4475                                 color_space = COLOR_SPACE_YCBCR709_LIMITED;
4476                         break;
4477                 default:
4478                         break;
4479                 }
4480
4481                 if (pipe_ctx->stream_res.tg->funcs->lock_doublebuffer_enable) {
4482                         if (pipe_ctx->stream && should_use_dmub_lock(pipe_ctx->stream->link)) {
4483                                 union dmub_hw_lock_flags hw_locks = { 0 };
4484                                 struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4485
4486                                 hw_locks.bits.lock_dig = 1;
4487                                 inst_flags.dig_inst = pipe_ctx->stream_res.tg->inst;
4488
4489                                 dmub_hw_lock_mgr_cmd(link->ctx->dmub_srv,
4490                                                         true,
4491                                                         &hw_locks,
4492                                                         &inst_flags);
4493                         } else
4494                                 pipe_ctx->stream_res.tg->funcs->lock_doublebuffer_enable(
4495                                                 pipe_ctx->stream_res.tg);
4496                 }
4497
4498                 pipe_ctx->stream_res.tg->funcs->lock(pipe_ctx->stream_res.tg);
4499                 /* update MSA to requested color space */
4500                 pipe_ctx->stream_res.stream_enc->funcs->dp_set_stream_attribute(pipe_ctx->stream_res.stream_enc,
4501                                 &pipe_ctx->stream->timing,
4502                                 color_space,
4503                                 pipe_ctx->stream->use_vsc_sdp_for_colorimetry,
4504                                 link->dpcd_caps.dprx_feature.bits.SST_SPLIT_SDP_CAP);
4505
4506                 if (pipe_ctx->stream->use_vsc_sdp_for_colorimetry) {
4507                         if (test_pattern == DP_TEST_PATTERN_COLOR_SQUARES_CEA)
4508                                 pipe_ctx->stream->vsc_infopacket.sb[17] |= (1 << 7); // sb17 bit 7 Dynamic Range: 0 = VESA range, 1 = CTA range
4509                         else
4510                                 pipe_ctx->stream->vsc_infopacket.sb[17] &= ~(1 << 7);
4511                         resource_build_info_frame(pipe_ctx);
4512                         link->dc->hwss.update_info_frame(pipe_ctx);
4513                 }
4514
4515                 /* CRTC Patterns */
4516                 set_crtc_test_pattern(link, pipe_ctx, test_pattern, test_pattern_color_space);
4517                 pipe_ctx->stream_res.tg->funcs->unlock(pipe_ctx->stream_res.tg);
4518                 pipe_ctx->stream_res.tg->funcs->wait_for_state(pipe_ctx->stream_res.tg,
4519                                 CRTC_STATE_VACTIVE);
4520                 pipe_ctx->stream_res.tg->funcs->wait_for_state(pipe_ctx->stream_res.tg,
4521                                 CRTC_STATE_VBLANK);
4522                 pipe_ctx->stream_res.tg->funcs->wait_for_state(pipe_ctx->stream_res.tg,
4523                                 CRTC_STATE_VACTIVE);
4524
4525                 if (pipe_ctx->stream_res.tg->funcs->lock_doublebuffer_disable) {
4526                         if (pipe_ctx->stream && should_use_dmub_lock(pipe_ctx->stream->link)) {
4527                                 union dmub_hw_lock_flags hw_locks = { 0 };
4528                                 struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4529
4530                                 hw_locks.bits.lock_dig = 1;
4531                                 inst_flags.dig_inst = pipe_ctx->stream_res.tg->inst;
4532
4533                                 dmub_hw_lock_mgr_cmd(link->ctx->dmub_srv,
4534                                                         false,
4535                                                         &hw_locks,
4536                                                         &inst_flags);
4537                         } else
4538                                 pipe_ctx->stream_res.tg->funcs->lock_doublebuffer_disable(
4539                                                 pipe_ctx->stream_res.tg);
4540                 }
4541
4542                 /* Set Test Pattern state */
4543                 link->test_pattern_enabled = true;
4544         }
4545
4546         return true;
4547 }
4548
4549 void dp_enable_mst_on_sink(struct dc_link *link, bool enable)
4550 {
4551         unsigned char mstmCntl;
4552
4553         core_link_read_dpcd(link, DP_MSTM_CTRL, &mstmCntl, 1);
4554         if (enable)
4555                 mstmCntl |= DP_MST_EN;
4556         else
4557                 mstmCntl &= (~DP_MST_EN);
4558
4559         core_link_write_dpcd(link, DP_MSTM_CTRL, &mstmCntl, 1);
4560 }
4561
4562 void dp_set_panel_mode(struct dc_link *link, enum dp_panel_mode panel_mode)
4563 {
4564         union dpcd_edp_config edp_config_set;
4565         bool panel_mode_edp = false;
4566
4567         memset(&edp_config_set, '\0', sizeof(union dpcd_edp_config));
4568
4569         if (panel_mode != DP_PANEL_MODE_DEFAULT) {
4570
4571                 switch (panel_mode) {
4572                 case DP_PANEL_MODE_EDP:
4573                 case DP_PANEL_MODE_SPECIAL:
4574                         panel_mode_edp = true;
4575                         break;
4576
4577                 default:
4578                                 break;
4579                 }
4580
4581                 /*set edp panel mode in receiver*/
4582                 core_link_read_dpcd(
4583                         link,
4584                         DP_EDP_CONFIGURATION_SET,
4585                         &edp_config_set.raw,
4586                         sizeof(edp_config_set.raw));
4587
4588                 if (edp_config_set.bits.PANEL_MODE_EDP
4589                         != panel_mode_edp) {
4590                         enum dc_status result;
4591
4592                         edp_config_set.bits.PANEL_MODE_EDP =
4593                         panel_mode_edp;
4594                         result = core_link_write_dpcd(
4595                                 link,
4596                                 DP_EDP_CONFIGURATION_SET,
4597                                 &edp_config_set.raw,
4598                                 sizeof(edp_config_set.raw));
4599
4600                         ASSERT(result == DC_OK);
4601                 }
4602         }
4603         DC_LOG_DETECTION_DP_CAPS("Link: %d eDP panel mode supported: %d "
4604                  "eDP panel mode enabled: %d \n",
4605                  link->link_index,
4606                  link->dpcd_caps.panel_mode_edp,
4607                  panel_mode_edp);
4608 }
4609
4610 enum dp_panel_mode dp_get_panel_mode(struct dc_link *link)
4611 {
4612         /* We need to explicitly check that connector
4613          * is not DP. Some Travis_VGA get reported
4614          * by video bios as DP.
4615          */
4616         if (link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT) {
4617
4618                 switch (link->dpcd_caps.branch_dev_id) {
4619                 case DP_BRANCH_DEVICE_ID_0022B9:
4620                         /* alternate scrambler reset is required for Travis
4621                          * for the case when external chip does not
4622                          * provide sink device id, alternate scrambler
4623                          * scheme will  be overriden later by querying
4624                          * Encoder features
4625                          */
4626                         if (strncmp(
4627                                 link->dpcd_caps.branch_dev_name,
4628                                 DP_VGA_LVDS_CONVERTER_ID_2,
4629                                 sizeof(
4630                                 link->dpcd_caps.
4631                                 branch_dev_name)) == 0) {
4632                                         return DP_PANEL_MODE_SPECIAL;
4633                         }
4634                         break;
4635                 case DP_BRANCH_DEVICE_ID_00001A:
4636                         /* alternate scrambler reset is required for Travis
4637                          * for the case when external chip does not provide
4638                          * sink device id, alternate scrambler scheme will
4639                          * be overriden later by querying Encoder feature
4640                          */
4641                         if (strncmp(link->dpcd_caps.branch_dev_name,
4642                                 DP_VGA_LVDS_CONVERTER_ID_3,
4643                                 sizeof(
4644                                 link->dpcd_caps.
4645                                 branch_dev_name)) == 0) {
4646                                         return DP_PANEL_MODE_SPECIAL;
4647                         }
4648                         break;
4649                 default:
4650                         break;
4651                 }
4652         }
4653
4654         if (link->dpcd_caps.panel_mode_edp) {
4655                 return DP_PANEL_MODE_EDP;
4656         }
4657
4658         return DP_PANEL_MODE_DEFAULT;
4659 }
4660
4661 enum dc_status dp_set_fec_ready(struct dc_link *link, bool ready)
4662 {
4663         /* FEC has to be "set ready" before the link training.
4664          * The policy is to always train with FEC
4665          * if the sink supports it and leave it enabled on link.
4666          * If FEC is not supported, disable it.
4667          */
4668         struct link_encoder *link_enc = NULL;
4669         enum dc_status status = DC_OK;
4670         uint8_t fec_config = 0;
4671
4672         /* Access link encoder based on whether it is statically
4673          * or dynamically assigned to a link.
4674          */
4675         if (link->is_dig_mapping_flexible &&
4676                         link->dc->res_pool->funcs->link_encs_assign)
4677                 link_enc = link_enc_cfg_get_link_enc_used_by_link(link->dc->current_state, link);
4678         else
4679                 link_enc = link->link_enc;
4680         ASSERT(link_enc);
4681
4682         if (!dc_link_should_enable_fec(link))
4683                 return status;
4684
4685         if (link_enc->funcs->fec_set_ready &&
4686                         link->dpcd_caps.fec_cap.bits.FEC_CAPABLE) {
4687                 if (ready) {
4688                         fec_config = 1;
4689                         status = core_link_write_dpcd(link,
4690                                         DP_FEC_CONFIGURATION,
4691                                         &fec_config,
4692                                         sizeof(fec_config));
4693                         if (status == DC_OK) {
4694                                 link_enc->funcs->fec_set_ready(link_enc, true);
4695                                 link->fec_state = dc_link_fec_ready;
4696                         } else {
4697                                 link_enc->funcs->fec_set_ready(link->link_enc, false);
4698                                 link->fec_state = dc_link_fec_not_ready;
4699                                 dm_error("dpcd write failed to set fec_ready");
4700                         }
4701                 } else if (link->fec_state == dc_link_fec_ready) {
4702                         fec_config = 0;
4703                         status = core_link_write_dpcd(link,
4704                                         DP_FEC_CONFIGURATION,
4705                                         &fec_config,
4706                                         sizeof(fec_config));
4707                         link_enc->funcs->fec_set_ready(link_enc, false);
4708                         link->fec_state = dc_link_fec_not_ready;
4709                 }
4710         }
4711
4712         return status;
4713 }
4714
4715 void dp_set_fec_enable(struct dc_link *link, bool enable)
4716 {
4717         struct link_encoder *link_enc = NULL;
4718
4719         /* Access link encoder based on whether it is statically
4720          * or dynamically assigned to a link.
4721          */
4722         if (link->is_dig_mapping_flexible &&
4723                         link->dc->res_pool->funcs->link_encs_assign)
4724                 link_enc = link_enc_cfg_get_link_enc_used_by_link(
4725                                 link->dc->current_state, link);
4726         else
4727                 link_enc = link->link_enc;
4728         ASSERT(link_enc);
4729
4730         if (!dc_link_should_enable_fec(link))
4731                 return;
4732
4733         if (link_enc->funcs->fec_set_enable &&
4734                         link->dpcd_caps.fec_cap.bits.FEC_CAPABLE) {
4735                 if (link->fec_state == dc_link_fec_ready && enable) {
4736                         /* Accord to DP spec, FEC enable sequence can first
4737                          * be transmitted anytime after 1000 LL codes have
4738                          * been transmitted on the link after link training
4739                          * completion. Using 1 lane RBR should have the maximum
4740                          * time for transmitting 1000 LL codes which is 6.173 us.
4741                          * So use 7 microseconds delay instead.
4742                          */
4743                         udelay(7);
4744                         link_enc->funcs->fec_set_enable(link_enc, true);
4745                         link->fec_state = dc_link_fec_enabled;
4746                 } else if (link->fec_state == dc_link_fec_enabled && !enable) {
4747                         link_enc->funcs->fec_set_enable(link_enc, false);
4748                         link->fec_state = dc_link_fec_ready;
4749                 }
4750         }
4751 }
4752
4753 void dpcd_set_source_specific_data(struct dc_link *link)
4754 {
4755         if (!link->dc->vendor_signature.is_valid) {
4756                 enum dc_status __maybe_unused result_write_min_hblank = DC_NOT_SUPPORTED;
4757                 struct dpcd_amd_signature amd_signature = {0};
4758                 struct dpcd_amd_device_id amd_device_id = {0};
4759
4760                 amd_device_id.device_id_byte1 =
4761                                 (uint8_t)(link->ctx->asic_id.chip_id);
4762                 amd_device_id.device_id_byte2 =
4763                                 (uint8_t)(link->ctx->asic_id.chip_id >> 8);
4764                 amd_device_id.dce_version =
4765                                 (uint8_t)(link->ctx->dce_version);
4766                 amd_device_id.dal_version_byte1 = 0x0; // needed? where to get?
4767                 amd_device_id.dal_version_byte2 = 0x0; // needed? where to get?
4768
4769                 core_link_read_dpcd(link, DP_SOURCE_OUI,
4770                                 (uint8_t *)(&amd_signature),
4771                                 sizeof(amd_signature));
4772
4773                 if (!((amd_signature.AMD_IEEE_TxSignature_byte1 == 0x0) &&
4774                         (amd_signature.AMD_IEEE_TxSignature_byte2 == 0x0) &&
4775                         (amd_signature.AMD_IEEE_TxSignature_byte3 == 0x1A))) {
4776
4777                         amd_signature.AMD_IEEE_TxSignature_byte1 = 0x0;
4778                         amd_signature.AMD_IEEE_TxSignature_byte2 = 0x0;
4779                         amd_signature.AMD_IEEE_TxSignature_byte3 = 0x1A;
4780
4781                         core_link_write_dpcd(link, DP_SOURCE_OUI,
4782                                 (uint8_t *)(&amd_signature),
4783                                 sizeof(amd_signature));
4784                 }
4785
4786                 core_link_write_dpcd(link, DP_SOURCE_OUI+0x03,
4787                                 (uint8_t *)(&amd_device_id),
4788                                 sizeof(amd_device_id));
4789
4790                 if (link->ctx->dce_version >= DCN_VERSION_2_0 &&
4791                         link->dc->caps.min_horizontal_blanking_period != 0) {
4792
4793                         uint8_t hblank_size = (uint8_t)link->dc->caps.min_horizontal_blanking_period;
4794
4795                         result_write_min_hblank = core_link_write_dpcd(link,
4796                                 DP_SOURCE_MINIMUM_HBLANK_SUPPORTED, (uint8_t *)(&hblank_size),
4797                                 sizeof(hblank_size));
4798                 }
4799                 DC_TRACE_LEVEL_MESSAGE(DAL_TRACE_LEVEL_INFORMATION,
4800                                                         WPP_BIT_FLAG_DC_DETECTION_DP_CAPS,
4801                                                         "result=%u link_index=%u enum dce_version=%d DPCD=0x%04X min_hblank=%u branch_dev_id=0x%x branch_dev_name='%c%c%c%c%c%c'",
4802                                                         result_write_min_hblank,
4803                                                         link->link_index,
4804                                                         link->ctx->dce_version,
4805                                                         DP_SOURCE_MINIMUM_HBLANK_SUPPORTED,
4806                                                         link->dc->caps.min_horizontal_blanking_period,
4807                                                         link->dpcd_caps.branch_dev_id,
4808                                                         link->dpcd_caps.branch_dev_name[0],
4809                                                         link->dpcd_caps.branch_dev_name[1],
4810                                                         link->dpcd_caps.branch_dev_name[2],
4811                                                         link->dpcd_caps.branch_dev_name[3],
4812                                                         link->dpcd_caps.branch_dev_name[4],
4813                                                         link->dpcd_caps.branch_dev_name[5]);
4814         } else {
4815                 core_link_write_dpcd(link, DP_SOURCE_OUI,
4816                                 link->dc->vendor_signature.data.raw,
4817                                 sizeof(link->dc->vendor_signature.data.raw));
4818         }
4819 }
4820
4821 bool dc_link_set_backlight_level_nits(struct dc_link *link,
4822                 bool isHDR,
4823                 uint32_t backlight_millinits,
4824                 uint32_t transition_time_in_ms)
4825 {
4826         struct dpcd_source_backlight_set dpcd_backlight_set;
4827         uint8_t backlight_control = isHDR ? 1 : 0;
4828
4829         if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
4830                         link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
4831                 return false;
4832
4833         // OLEDs have no PWM, they can only use AUX
4834         if (link->dpcd_sink_ext_caps.bits.oled == 1)
4835                 backlight_control = 1;
4836
4837         *(uint32_t *)&dpcd_backlight_set.backlight_level_millinits = backlight_millinits;
4838         *(uint16_t *)&dpcd_backlight_set.backlight_transition_time_ms = (uint16_t)transition_time_in_ms;
4839
4840
4841         if (core_link_write_dpcd(link, DP_SOURCE_BACKLIGHT_LEVEL,
4842                         (uint8_t *)(&dpcd_backlight_set),
4843                         sizeof(dpcd_backlight_set)) != DC_OK)
4844                 return false;
4845
4846         if (core_link_write_dpcd(link, DP_SOURCE_BACKLIGHT_CONTROL,
4847                         &backlight_control, 1) != DC_OK)
4848                 return false;
4849
4850         return true;
4851 }
4852
4853 bool dc_link_get_backlight_level_nits(struct dc_link *link,
4854                 uint32_t *backlight_millinits_avg,
4855                 uint32_t *backlight_millinits_peak)
4856 {
4857         union dpcd_source_backlight_get dpcd_backlight_get;
4858
4859         memset(&dpcd_backlight_get, 0, sizeof(union dpcd_source_backlight_get));
4860
4861         if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
4862                         link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
4863                 return false;
4864
4865         if (core_link_read_dpcd(link, DP_SOURCE_BACKLIGHT_CURRENT_PEAK,
4866                         dpcd_backlight_get.raw,
4867                         sizeof(union dpcd_source_backlight_get)) != DC_OK)
4868                 return false;
4869
4870         *backlight_millinits_avg =
4871                 dpcd_backlight_get.bytes.backlight_millinits_avg;
4872         *backlight_millinits_peak =
4873                 dpcd_backlight_get.bytes.backlight_millinits_peak;
4874
4875         /* On non-supported panels dpcd_read usually succeeds with 0 returned */
4876         if (*backlight_millinits_avg == 0 ||
4877                         *backlight_millinits_avg > *backlight_millinits_peak)
4878                 return false;
4879
4880         return true;
4881 }
4882
4883 bool dc_link_backlight_enable_aux(struct dc_link *link, bool enable)
4884 {
4885         uint8_t backlight_enable = enable ? 1 : 0;
4886
4887         if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
4888                 link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
4889                 return false;
4890
4891         if (core_link_write_dpcd(link, DP_SOURCE_BACKLIGHT_ENABLE,
4892                 &backlight_enable, 1) != DC_OK)
4893                 return false;
4894
4895         return true;
4896 }
4897
4898 // we read default from 0x320 because we expect BIOS wrote it there
4899 // regular get_backlight_nit reads from panel set at 0x326
4900 bool dc_link_read_default_bl_aux(struct dc_link *link, uint32_t *backlight_millinits)
4901 {
4902         if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
4903                 link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
4904                 return false;
4905
4906         if (core_link_read_dpcd(link, DP_SOURCE_BACKLIGHT_LEVEL,
4907                 (uint8_t *) backlight_millinits,
4908                 sizeof(uint32_t)) != DC_OK)
4909                 return false;
4910
4911         return true;
4912 }
4913
4914 bool dc_link_set_default_brightness_aux(struct dc_link *link)
4915 {
4916         uint32_t default_backlight;
4917
4918         if (link &&
4919                 (link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1 ||
4920                 link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1)) {
4921                 if (!dc_link_read_default_bl_aux(link, &default_backlight))
4922                         default_backlight = 150000;
4923                 // if < 5 nits or > 5000, it might be wrong readback
4924                 if (default_backlight < 5000 || default_backlight > 5000000)
4925                         default_backlight = 150000; //
4926
4927                 return dc_link_set_backlight_level_nits(link, true,
4928                                 default_backlight, 0);
4929         }
4930         return false;
4931 }
4932
4933 bool is_edp_ilr_optimization_required(struct dc_link *link, struct dc_crtc_timing *crtc_timing)
4934 {
4935         struct dc_link_settings link_setting;
4936         uint8_t link_bw_set;
4937         uint8_t link_rate_set;
4938         uint32_t req_bw;
4939         union lane_count_set lane_count_set = { {0} };
4940
4941         ASSERT(link || crtc_timing); // invalid input
4942
4943         if (link->dpcd_caps.edp_supported_link_rates_count == 0 ||
4944                         !link->dc->debug.optimize_edp_link_rate)
4945                 return false;
4946
4947
4948         // Read DPCD 00100h to find if standard link rates are set
4949         core_link_read_dpcd(link, DP_LINK_BW_SET,
4950                                 &link_bw_set, sizeof(link_bw_set));
4951
4952         if (link_bw_set) {
4953                 DC_LOG_EVENT_LINK_TRAINING("eDP ILR: Optimization required, VBIOS used link_bw_set\n");
4954                 return true;
4955         }
4956
4957         // Read DPCD 00115h to find the edp link rate set used
4958         core_link_read_dpcd(link, DP_LINK_RATE_SET,
4959                             &link_rate_set, sizeof(link_rate_set));
4960
4961         // Read DPCD 00101h to find out the number of lanes currently set
4962         core_link_read_dpcd(link, DP_LANE_COUNT_SET,
4963                                 &lane_count_set.raw, sizeof(lane_count_set));
4964
4965         req_bw = dc_bandwidth_in_kbps_from_timing(crtc_timing);
4966
4967         decide_edp_link_settings(link, &link_setting, req_bw);
4968
4969         if (link->dpcd_caps.edp_supported_link_rates[link_rate_set] != link_setting.link_rate ||
4970                         lane_count_set.bits.LANE_COUNT_SET != link_setting.lane_count) {
4971                 DC_LOG_EVENT_LINK_TRAINING("eDP ILR: Optimization required, VBIOS link_rate_set not optimal\n");
4972                 return true;
4973         }
4974
4975         DC_LOG_EVENT_LINK_TRAINING("eDP ILR: No optimization required, VBIOS set optimal link_rate_set\n");
4976         return false;
4977 }
4978
4979 enum dp_link_encoding dp_get_link_encoding_format(const struct dc_link_settings *link_settings)
4980 {
4981         if ((link_settings->link_rate >= LINK_RATE_LOW) &&
4982                         (link_settings->link_rate <= LINK_RATE_HIGH3))
4983                 return DP_8b_10b_ENCODING;
4984         return DP_UNKNOWN_ENCODING;
4985 }
4986