1 /*
2  * Copyright 2022 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 /* FILE POLICY AND INTENDED USAGE:
27  * This file implements dp 8b/10b link training software policies and
28  * sequences.
29  */
30 #include "link_dp_training_8b_10b.h"
31 #include "link_dpcd.h"
32 #include "link_dp_phy.h"
33 #include "link_dp_capability.h"
34 
35 #define DC_LOGGER \
36 	link->ctx->logger
37 
get_cr_training_aux_rd_interval(struct dc_link * link,const struct dc_link_settings * link_settings)38 static int32_t get_cr_training_aux_rd_interval(struct dc_link *link,
39 		const struct dc_link_settings *link_settings)
40 {
41 	union training_aux_rd_interval training_rd_interval;
42 	uint32_t wait_in_micro_secs = 100;
43 
44 	memset(&training_rd_interval, 0, sizeof(training_rd_interval));
45 	if (link_dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING &&
46 			link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_12) {
47 		core_link_read_dpcd(
48 				link,
49 				DP_TRAINING_AUX_RD_INTERVAL,
50 				(uint8_t *)&training_rd_interval,
51 				sizeof(training_rd_interval));
52 		if (training_rd_interval.bits.TRAINIG_AUX_RD_INTERVAL)
53 			wait_in_micro_secs = training_rd_interval.bits.TRAINIG_AUX_RD_INTERVAL * 4000;
54 	}
55 	return wait_in_micro_secs;
56 }
57 
get_eq_training_aux_rd_interval(struct dc_link * link,const struct dc_link_settings * link_settings)58 static uint32_t get_eq_training_aux_rd_interval(
59 	struct dc_link *link,
60 	const struct dc_link_settings *link_settings)
61 {
62 	union training_aux_rd_interval training_rd_interval;
63 
64 	memset(&training_rd_interval, 0, sizeof(training_rd_interval));
65 	if (link_dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING) {
66 		core_link_read_dpcd(
67 				link,
68 				DP_128B132B_TRAINING_AUX_RD_INTERVAL,
69 				(uint8_t *)&training_rd_interval,
70 				sizeof(training_rd_interval));
71 	} else if (link_dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING &&
72 			link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_12) {
73 		core_link_read_dpcd(
74 				link,
75 				DP_TRAINING_AUX_RD_INTERVAL,
76 				(uint8_t *)&training_rd_interval,
77 				sizeof(training_rd_interval));
78 	}
79 
80 	switch (training_rd_interval.bits.TRAINIG_AUX_RD_INTERVAL) {
81 	case 0: return 400;
82 	case 1: return 4000;
83 	case 2: return 8000;
84 	case 3: return 12000;
85 	case 4: return 16000;
86 	case 5: return 32000;
87 	case 6: return 64000;
88 	default: return 400;
89 	}
90 }
91 
decide_8b_10b_training_settings(struct dc_link * link,const struct dc_link_settings * link_setting,struct link_training_settings * lt_settings)92 void decide_8b_10b_training_settings(
93 	 struct dc_link *link,
94 	const struct dc_link_settings *link_setting,
95 	struct link_training_settings *lt_settings)
96 {
97 	memset(lt_settings, '\0', sizeof(struct link_training_settings));
98 
99 	/* Initialize link settings */
100 	lt_settings->link_settings.use_link_rate_set = link_setting->use_link_rate_set;
101 	lt_settings->link_settings.link_rate_set = link_setting->link_rate_set;
102 	lt_settings->link_settings.link_rate = link_setting->link_rate;
103 	lt_settings->link_settings.lane_count = link_setting->lane_count;
104 	/* TODO hard coded to SS for now
105 	 * lt_settings.link_settings.link_spread =
106 	 * dal_display_path_is_ss_supported(
107 	 * path_mode->display_path) ?
108 	 * LINK_SPREAD_05_DOWNSPREAD_30KHZ :
109 	 * LINK_SPREAD_DISABLED;
110 	 */
111 	lt_settings->link_settings.link_spread = link->dp_ss_off ?
112 			LINK_SPREAD_DISABLED : LINK_SPREAD_05_DOWNSPREAD_30KHZ;
113 	lt_settings->cr_pattern_time = get_cr_training_aux_rd_interval(link, link_setting);
114 	lt_settings->eq_pattern_time = get_eq_training_aux_rd_interval(link, link_setting);
115 	lt_settings->pattern_for_cr = decide_cr_training_pattern(link_setting);
116 	lt_settings->pattern_for_eq = decide_eq_training_pattern(link, link_setting);
117 	lt_settings->enhanced_framing = 1;
118 	lt_settings->should_set_fec_ready = true;
119 	lt_settings->disallow_per_lane_settings = true;
120 	lt_settings->always_match_dpcd_with_hw_lane_settings = true;
121 	lt_settings->lttpr_mode = dp_decide_8b_10b_lttpr_mode(link);
122 	dp_hw_to_dpcd_lane_settings(lt_settings, lt_settings->hw_lane_settings, lt_settings->dpcd_lane_settings);
123 }
124 
dp_decide_8b_10b_lttpr_mode(struct dc_link * link)125 enum lttpr_mode dp_decide_8b_10b_lttpr_mode(struct dc_link *link)
126 {
127 	bool is_lttpr_present = dp_is_lttpr_present(link);
128 	bool vbios_lttpr_force_non_transparent = link->dc->caps.vbios_lttpr_enable;
129 	bool vbios_lttpr_aware = link->dc->caps.vbios_lttpr_aware;
130 
131 	if (!is_lttpr_present)
132 		return LTTPR_MODE_NON_LTTPR;
133 
134 	if (vbios_lttpr_aware) {
135 		if (vbios_lttpr_force_non_transparent) {
136 			DC_LOG_DC("chose LTTPR_MODE_NON_TRANSPARENT due to VBIOS DCE_INFO_CAPS_LTTPR_SUPPORT_ENABLE set to 1.\n");
137 			return LTTPR_MODE_NON_TRANSPARENT;
138 		} else {
139 			DC_LOG_DC("chose LTTPR_MODE_NON_TRANSPARENT by default due to VBIOS not set DCE_INFO_CAPS_LTTPR_SUPPORT_ENABLE set to 1.\n");
140 			return LTTPR_MODE_TRANSPARENT;
141 		}
142 	}
143 
144 	if (link->dc->config.allow_lttpr_non_transparent_mode.bits.DP1_4A &&
145 			link->dc->caps.extended_aux_timeout_support) {
146 		DC_LOG_DC("chose LTTPR_MODE_NON_TRANSPARENT by default and dc->config.allow_lttpr_non_transparent_mode.bits.DP1_4A set to 1.\n");
147 		return LTTPR_MODE_NON_TRANSPARENT;
148 	}
149 
150 	DC_LOG_DC("chose LTTPR_MODE_NON_LTTPR.\n");
151 	return LTTPR_MODE_NON_LTTPR;
152 }
153 
perform_8b_10b_clock_recovery_sequence(struct dc_link * link,const struct link_resource * link_res,struct link_training_settings * lt_settings,uint32_t offset)154 enum link_training_result perform_8b_10b_clock_recovery_sequence(
155 	struct dc_link *link,
156 	const struct link_resource *link_res,
157 	struct link_training_settings *lt_settings,
158 	uint32_t offset)
159 {
160 	enum dc_status status;
161 	uint32_t retries_cr;
162 	uint32_t retry_count;
163 	uint32_t wait_time_microsec;
164 	enum dc_lane_count lane_count = lt_settings->link_settings.lane_count;
165 	union lane_status dpcd_lane_status[LANE_COUNT_DP_MAX];
166 	union lane_align_status_updated dpcd_lane_status_updated;
167 	union lane_adjust dpcd_lane_adjust[LANE_COUNT_DP_MAX] = {0};
168 
169 	retries_cr = 0;
170 	retry_count = 0;
171 
172 	memset(&dpcd_lane_status, '\0', sizeof(dpcd_lane_status));
173 	memset(&dpcd_lane_status_updated, '\0',
174 	sizeof(dpcd_lane_status_updated));
175 
176 	if (!link->ctx->dc->work_arounds.lt_early_cr_pattern)
177 		dp_set_hw_training_pattern(link, link_res, lt_settings->pattern_for_cr, offset);
178 
179 	/* najeeb - The synaptics MST hub can put the LT in
180 	* infinite loop by switching the VS
181 	*/
182 	/* between level 0 and level 1 continuously, here
183 	* we try for CR lock for LinkTrainingMaxCRRetry count*/
184 	while ((retries_cr < LINK_TRAINING_MAX_RETRY_COUNT) &&
185 		(retry_count < LINK_TRAINING_MAX_CR_RETRY)) {
186 
187 
188 		/* 1. call HWSS to set lane settings*/
189 		dp_set_hw_lane_settings(
190 				link,
191 				link_res,
192 				lt_settings,
193 				offset);
194 
195 		/* 2. update DPCD of the receiver*/
196 		if (!retry_count)
197 			/* EPR #361076 - write as a 5-byte burst,
198 			 * but only for the 1-st iteration.*/
199 			dpcd_set_lt_pattern_and_lane_settings(
200 					link,
201 					lt_settings,
202 					lt_settings->pattern_for_cr,
203 					offset);
204 		else
205 			dpcd_set_lane_settings(
206 					link,
207 					lt_settings,
208 					offset);
209 
210 		/* 3. wait receiver to lock-on*/
211 		wait_time_microsec = lt_settings->cr_pattern_time;
212 
213 		dp_wait_for_training_aux_rd_interval(
214 				link,
215 				wait_time_microsec);
216 
217 		/* 4. Read lane status and requested drive
218 		* settings as set by the sink
219 		*/
220 		status = dp_get_lane_status_and_lane_adjust(
221 				link,
222 				lt_settings,
223 				dpcd_lane_status,
224 				&dpcd_lane_status_updated,
225 				dpcd_lane_adjust,
226 				offset);
227 
228 		if (dp_check_dpcd_reqeust_status(link, status))
229 			return LINK_TRAINING_ABORT;
230 
231 		/* 5. check CR done*/
232 		if (dp_is_cr_done(lane_count, dpcd_lane_status)) {
233 			DC_LOG_HW_LINK_TRAINING("%s: Clock recovery OK\n", __func__);
234 			return LINK_TRAINING_SUCCESS;
235 		}
236 
237 		/* 6. max VS reached*/
238 		if ((link_dp_get_encoding_format(&lt_settings->link_settings) ==
239 				DP_8b_10b_ENCODING) &&
240 				dp_is_max_vs_reached(lt_settings))
241 			break;
242 
243 		/* 7. same lane settings*/
244 		/* Note: settings are the same for all lanes,
245 		 * so comparing first lane is sufficient*/
246 		if ((link_dp_get_encoding_format(&lt_settings->link_settings) == DP_8b_10b_ENCODING) &&
247 				lt_settings->dpcd_lane_settings[0].bits.VOLTAGE_SWING_SET ==
248 						dpcd_lane_adjust[0].bits.VOLTAGE_SWING_LANE)
249 			retries_cr++;
250 		else if ((link_dp_get_encoding_format(&lt_settings->link_settings) == DP_128b_132b_ENCODING) &&
251 				lt_settings->dpcd_lane_settings[0].tx_ffe.PRESET_VALUE ==
252 						dpcd_lane_adjust[0].tx_ffe.PRESET_VALUE)
253 			retries_cr++;
254 		else
255 			retries_cr = 0;
256 
257 		/* 8. update VS/PE/PC2 in lt_settings*/
258 		dp_decide_lane_settings(lt_settings, dpcd_lane_adjust,
259 				lt_settings->hw_lane_settings, lt_settings->dpcd_lane_settings);
260 		retry_count++;
261 	}
262 
263 	if (retry_count >= LINK_TRAINING_MAX_CR_RETRY) {
264 		ASSERT(0);
265 		DC_LOG_ERROR("%s: Link Training Error, could not get CR after %d tries. Possibly voltage swing issue",
266 			__func__,
267 			LINK_TRAINING_MAX_CR_RETRY);
268 
269 	}
270 
271 	return dp_get_cr_failure(lane_count, dpcd_lane_status);
272 }
273 
perform_8b_10b_channel_equalization_sequence(struct dc_link * link,const struct link_resource * link_res,struct link_training_settings * lt_settings,uint32_t offset)274 enum link_training_result perform_8b_10b_channel_equalization_sequence(
275 	struct dc_link *link,
276 	const struct link_resource *link_res,
277 	struct link_training_settings *lt_settings,
278 	uint32_t offset)
279 {
280 	enum dc_status status;
281 	enum dc_dp_training_pattern tr_pattern;
282 	uint32_t retries_ch_eq;
283 	uint32_t wait_time_microsec;
284 	enum dc_lane_count lane_count = lt_settings->link_settings.lane_count;
285 	union lane_align_status_updated dpcd_lane_status_updated = {0};
286 	union lane_status dpcd_lane_status[LANE_COUNT_DP_MAX] = {0};
287 	union lane_adjust dpcd_lane_adjust[LANE_COUNT_DP_MAX] = {0};
288 
289 	/* Note: also check that TPS4 is a supported feature*/
290 	tr_pattern = lt_settings->pattern_for_eq;
291 
292 	if (is_repeater(lt_settings, offset) && link_dp_get_encoding_format(&lt_settings->link_settings) == DP_8b_10b_ENCODING)
293 		tr_pattern = DP_TRAINING_PATTERN_SEQUENCE_4;
294 
295 	dp_set_hw_training_pattern(link, link_res, tr_pattern, offset);
296 
297 	for (retries_ch_eq = 0; retries_ch_eq <= LINK_TRAINING_MAX_RETRY_COUNT;
298 		retries_ch_eq++) {
299 
300 		dp_set_hw_lane_settings(link, link_res, lt_settings, offset);
301 
302 		/* 2. update DPCD*/
303 		if (!retries_ch_eq)
304 			/* EPR #361076 - write as a 5-byte burst,
305 			 * but only for the 1-st iteration
306 			 */
307 
308 			dpcd_set_lt_pattern_and_lane_settings(
309 				link,
310 				lt_settings,
311 				tr_pattern, offset);
312 		else
313 			dpcd_set_lane_settings(link, lt_settings, offset);
314 
315 		/* 3. wait for receiver to lock-on*/
316 		wait_time_microsec = dp_get_eq_aux_rd_interval(link, lt_settings, offset, retries_ch_eq);
317 
318 		dp_wait_for_training_aux_rd_interval(
319 				link,
320 				wait_time_microsec);
321 
322 		/* 4. Read lane status and requested
323 		 * drive settings as set by the sink*/
324 
325 		status = dp_get_lane_status_and_lane_adjust(
326 			link,
327 			lt_settings,
328 			dpcd_lane_status,
329 			&dpcd_lane_status_updated,
330 			dpcd_lane_adjust,
331 			offset);
332 
333 		if (dp_check_dpcd_reqeust_status(link, status))
334 			return LINK_TRAINING_ABORT;
335 
336 		/* 5. check CR done*/
337 		if (!dp_is_cr_done(lane_count, dpcd_lane_status))
338 			return dpcd_lane_status[0].bits.CR_DONE_0 ?
339 					LINK_TRAINING_EQ_FAIL_CR_PARTIAL :
340 					LINK_TRAINING_EQ_FAIL_CR;
341 
342 		/* 6. check CHEQ done*/
343 		if (dp_is_ch_eq_done(lane_count, dpcd_lane_status) &&
344 				dp_is_symbol_locked(lane_count, dpcd_lane_status) &&
345 				dp_check_interlane_aligned(dpcd_lane_status_updated, link, retries_ch_eq))
346 			return LINK_TRAINING_SUCCESS;
347 
348 		/* 7. update VS/PE/PC2 in lt_settings*/
349 		dp_decide_lane_settings(lt_settings, dpcd_lane_adjust,
350 				lt_settings->hw_lane_settings, lt_settings->dpcd_lane_settings);
351 	}
352 
353 	return LINK_TRAINING_EQ_FAIL_EQ;
354 
355 }
356 
dp_perform_8b_10b_link_training(struct dc_link * link,const struct link_resource * link_res,struct link_training_settings * lt_settings)357 enum link_training_result dp_perform_8b_10b_link_training(
358 		struct dc_link *link,
359 		const struct link_resource *link_res,
360 		struct link_training_settings *lt_settings)
361 {
362 	enum link_training_result status = LINK_TRAINING_SUCCESS;
363 
364 	uint8_t repeater_cnt;
365 	uint8_t repeater_id;
366 	uint8_t lane = 0;
367 
368 	if (link->ctx->dc->work_arounds.lt_early_cr_pattern)
369 		start_clock_recovery_pattern_early(link, link_res, lt_settings, DPRX);
370 
371 	/* 1. set link rate, lane count and spread. */
372 	dpcd_set_link_settings(link, lt_settings);
373 
374 	if (lt_settings->lttpr_mode == LTTPR_MODE_NON_TRANSPARENT) {
375 
376 		/* 2. perform link training (set link training done
377 		 *  to false is done as well)
378 		 */
379 		repeater_cnt = dp_parse_lttpr_repeater_count(link->dpcd_caps.lttpr_caps.phy_repeater_cnt);
380 
381 		for (repeater_id = repeater_cnt; (repeater_id > 0 && status == LINK_TRAINING_SUCCESS);
382 				repeater_id--) {
383 			status = perform_8b_10b_clock_recovery_sequence(link, link_res, lt_settings, repeater_id);
384 
385 			if (status != LINK_TRAINING_SUCCESS) {
386 				repeater_training_done(link, repeater_id);
387 				break;
388 			}
389 
390 			status = perform_8b_10b_channel_equalization_sequence(link,
391 					link_res,
392 					lt_settings,
393 					repeater_id);
394 			if (status == LINK_TRAINING_SUCCESS)
395 				DC_LOG_HW_LINK_TRAINING("%s: Channel EQ done.\n", __func__);
396 
397 			repeater_training_done(link, repeater_id);
398 
399 			if (status != LINK_TRAINING_SUCCESS)
400 				break;
401 
402 			for (lane = 0; lane < LANE_COUNT_DP_MAX; lane++) {
403 				lt_settings->dpcd_lane_settings[lane].raw = 0;
404 				lt_settings->hw_lane_settings[lane].VOLTAGE_SWING = 0;
405 				lt_settings->hw_lane_settings[lane].PRE_EMPHASIS = 0;
406 			}
407 		}
408 	}
409 
410 	if (status == LINK_TRAINING_SUCCESS) {
411 		status = perform_8b_10b_clock_recovery_sequence(link, link_res, lt_settings, DPRX);
412 		if (status == LINK_TRAINING_SUCCESS) {
413 			status = perform_8b_10b_channel_equalization_sequence(link,
414 					link_res,
415 					lt_settings,
416 					DPRX);
417 			if (status == LINK_TRAINING_SUCCESS)
418 				DC_LOG_HW_LINK_TRAINING("%s: Channel EQ done.\n", __func__);
419 		}
420 	}
421 
422 	return status;
423 }
424