• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012-15 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 #include <linux/slab.h>
27 
28 #include "dm_services.h"
29 #include "atomfirmware.h"
30 #include "dm_helpers.h"
31 #include "dc.h"
32 #include "grph_object_id.h"
33 #include "gpio_service_interface.h"
34 #include "core_status.h"
35 #include "dc_link_dp.h"
36 #include "dc_link_ddc.h"
37 #include "link_hwss.h"
38 #include "opp.h"
39 
40 #include "link_encoder.h"
41 #include "hw_sequencer.h"
42 #include "resource.h"
43 #include "abm.h"
44 #include "fixed31_32.h"
45 #include "dpcd_defs.h"
46 #include "dmcu.h"
47 #include "hw/clk_mgr.h"
48 #include "dce/dmub_psr.h"
49 #include "dmub/dmub_srv.h"
50 #include "inc/hw/panel_cntl.h"
51 #include "inc/link_enc_cfg.h"
52 #include "inc/link_dpcd.h"
53 
54 #define DC_LOGGER_INIT(logger)
55 
56 #define LINK_INFO(...) \
57 	DC_LOG_HW_HOTPLUG(  \
58 		__VA_ARGS__)
59 
60 #define RETIMER_REDRIVER_INFO(...) \
61 	DC_LOG_RETIMER_REDRIVER(  \
62 		__VA_ARGS__)
63 
64 /*******************************************************************************
65  * Private functions
66  ******************************************************************************/
dc_link_destruct(struct dc_link * link)67 static void dc_link_destruct(struct dc_link *link)
68 {
69 	int i;
70 
71 	if (link->hpd_gpio) {
72 		dal_gpio_destroy_irq(&link->hpd_gpio);
73 		link->hpd_gpio = NULL;
74 	}
75 
76 	if (link->ddc)
77 		dal_ddc_service_destroy(&link->ddc);
78 
79 	if (link->panel_cntl)
80 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
81 
82 	if (link->link_enc) {
83 		/* Update link encoder resource tracking variables. These are used for
84 		 * the dynamic assignment of link encoders to streams. Virtual links
85 		 * are not assigned encoder resources on creation.
86 		 */
87 		if (link->link_id.id != CONNECTOR_ID_VIRTUAL) {
88 			link->dc->res_pool->link_encoders[link->eng_id - ENGINE_ID_DIGA] = NULL;
89 			link->dc->res_pool->dig_link_enc_count--;
90 		}
91 		link->link_enc->funcs->destroy(&link->link_enc);
92 	}
93 
94 	if (link->local_sink)
95 		dc_sink_release(link->local_sink);
96 
97 	for (i = 0; i < link->sink_count; ++i)
98 		dc_sink_release(link->remote_sinks[i]);
99 }
100 
get_hpd_gpio(struct dc_bios * dcb,struct graphics_object_id link_id,struct gpio_service * gpio_service)101 struct gpio *get_hpd_gpio(struct dc_bios *dcb,
102 			  struct graphics_object_id link_id,
103 			  struct gpio_service *gpio_service)
104 {
105 	enum bp_result bp_result;
106 	struct graphics_object_hpd_info hpd_info;
107 	struct gpio_pin_info pin_info;
108 
109 	if (dcb->funcs->get_hpd_info(dcb, link_id, &hpd_info) != BP_RESULT_OK)
110 		return NULL;
111 
112 	bp_result = dcb->funcs->get_gpio_pin_info(dcb,
113 		hpd_info.hpd_int_gpio_uid, &pin_info);
114 
115 	if (bp_result != BP_RESULT_OK) {
116 		ASSERT(bp_result == BP_RESULT_NORECORD);
117 		return NULL;
118 	}
119 
120 	return dal_gpio_service_create_irq(gpio_service,
121 					   pin_info.offset,
122 					   pin_info.mask);
123 }
124 
125 /*
126  *  Function: program_hpd_filter
127  *
128  *  @brief
129  *     Programs HPD filter on associated HPD line
130  *
131  *  @param [in] delay_on_connect_in_ms: Connect filter timeout
132  *  @param [in] delay_on_disconnect_in_ms: Disconnect filter timeout
133  *
134  *  @return
135  *     true on success, false otherwise
136  */
program_hpd_filter(const struct dc_link * link)137 static bool program_hpd_filter(const struct dc_link *link)
138 {
139 	bool result = false;
140 	struct gpio *hpd;
141 	int delay_on_connect_in_ms = 0;
142 	int delay_on_disconnect_in_ms = 0;
143 
144 	if (link->is_hpd_filter_disabled)
145 		return false;
146 	/* Verify feature is supported */
147 	switch (link->connector_signal) {
148 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
149 	case SIGNAL_TYPE_DVI_DUAL_LINK:
150 	case SIGNAL_TYPE_HDMI_TYPE_A:
151 		/* Program hpd filter */
152 		delay_on_connect_in_ms = 500;
153 		delay_on_disconnect_in_ms = 100;
154 		break;
155 	case SIGNAL_TYPE_DISPLAY_PORT:
156 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
157 		/* Program hpd filter to allow DP signal to settle */
158 		/* 500:	not able to detect MST <-> SST switch as HPD is low for
159 		 * only 100ms on DELL U2413
160 		 * 0: some passive dongle still show aux mode instead of i2c
161 		 * 20-50: not enough to hide bouncing HPD with passive dongle.
162 		 * also see intermittent i2c read issues.
163 		 */
164 		delay_on_connect_in_ms = 80;
165 		delay_on_disconnect_in_ms = 0;
166 		break;
167 	case SIGNAL_TYPE_LVDS:
168 	case SIGNAL_TYPE_EDP:
169 	default:
170 		/* Don't program hpd filter */
171 		return false;
172 	}
173 
174 	/* Obtain HPD handle */
175 	hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
176 			   link->ctx->gpio_service);
177 
178 	if (!hpd)
179 		return result;
180 
181 	/* Setup HPD filtering */
182 	if (dal_gpio_open(hpd, GPIO_MODE_INTERRUPT) == GPIO_RESULT_OK) {
183 		struct gpio_hpd_config config;
184 
185 		config.delay_on_connect = delay_on_connect_in_ms;
186 		config.delay_on_disconnect = delay_on_disconnect_in_ms;
187 
188 		dal_irq_setup_hpd_filter(hpd, &config);
189 
190 		dal_gpio_close(hpd);
191 
192 		result = true;
193 	} else {
194 		ASSERT_CRITICAL(false);
195 	}
196 
197 	/* Release HPD handle */
198 	dal_gpio_destroy_irq(&hpd);
199 
200 	return result;
201 }
202 
dc_link_wait_for_t12(struct dc_link * link)203 bool dc_link_wait_for_t12(struct dc_link *link)
204 {
205 	if (link->connector_signal == SIGNAL_TYPE_EDP && link->dc->hwss.edp_wait_for_T12) {
206 		link->dc->hwss.edp_wait_for_T12(link);
207 
208 		return true;
209 	}
210 
211 	return false;
212 }
213 
214 /**
215  * dc_link_detect_sink() - Determine if there is a sink connected
216  *
217  * @link: pointer to the dc link
218  * @type: Returned connection type
219  * Does not detect downstream devices, such as MST sinks
220  * or display connected through active dongles
221  */
dc_link_detect_sink(struct dc_link * link,enum dc_connection_type * type)222 bool dc_link_detect_sink(struct dc_link *link, enum dc_connection_type *type)
223 {
224 	uint32_t is_hpd_high = 0;
225 	struct gpio *hpd_pin;
226 
227 	if (link->connector_signal == SIGNAL_TYPE_LVDS) {
228 		*type = dc_connection_single;
229 		return true;
230 	}
231 
232 	if (link->connector_signal == SIGNAL_TYPE_EDP) {
233 		/*in case it is not on*/
234 		link->dc->hwss.edp_power_control(link, true);
235 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
236 	}
237 
238 	/* Link may not have physical HPD pin. */
239 	if (link->ep_type != DISPLAY_ENDPOINT_PHY) {
240 		if (link->hpd_status)
241 			*type = dc_connection_single;
242 		else
243 			*type = dc_connection_none;
244 
245 		return true;
246 	}
247 
248 	/* todo: may need to lock gpio access */
249 	hpd_pin = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
250 			       link->ctx->gpio_service);
251 	if (!hpd_pin)
252 		goto hpd_gpio_failure;
253 
254 	dal_gpio_open(hpd_pin, GPIO_MODE_INTERRUPT);
255 	dal_gpio_get_value(hpd_pin, &is_hpd_high);
256 	dal_gpio_close(hpd_pin);
257 	dal_gpio_destroy_irq(&hpd_pin);
258 
259 	if (is_hpd_high) {
260 		*type = dc_connection_single;
261 		/* TODO: need to do the actual detection */
262 	} else {
263 		*type = dc_connection_none;
264 	}
265 
266 	return true;
267 
268 hpd_gpio_failure:
269 	return false;
270 }
271 
get_ddc_transaction_type(enum signal_type sink_signal)272 static enum ddc_transaction_type get_ddc_transaction_type(enum signal_type sink_signal)
273 {
274 	enum ddc_transaction_type transaction_type = DDC_TRANSACTION_TYPE_NONE;
275 
276 	switch (sink_signal) {
277 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
278 	case SIGNAL_TYPE_DVI_DUAL_LINK:
279 	case SIGNAL_TYPE_HDMI_TYPE_A:
280 	case SIGNAL_TYPE_LVDS:
281 	case SIGNAL_TYPE_RGB:
282 		transaction_type = DDC_TRANSACTION_TYPE_I2C;
283 		break;
284 
285 	case SIGNAL_TYPE_DISPLAY_PORT:
286 	case SIGNAL_TYPE_EDP:
287 		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
288 		break;
289 
290 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
291 		/* MST does not use I2COverAux, but there is the
292 		 * SPECIAL use case for "immediate dwnstrm device
293 		 * access" (EPR#370830).
294 		 */
295 		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
296 		break;
297 
298 	default:
299 		break;
300 	}
301 
302 	return transaction_type;
303 }
304 
get_basic_signal_type(struct graphics_object_id encoder,struct graphics_object_id downstream)305 static enum signal_type get_basic_signal_type(struct graphics_object_id encoder,
306 					      struct graphics_object_id downstream)
307 {
308 	if (downstream.type == OBJECT_TYPE_CONNECTOR) {
309 		switch (downstream.id) {
310 		case CONNECTOR_ID_SINGLE_LINK_DVII:
311 			switch (encoder.id) {
312 			case ENCODER_ID_INTERNAL_DAC1:
313 			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
314 			case ENCODER_ID_INTERNAL_DAC2:
315 			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
316 				return SIGNAL_TYPE_RGB;
317 			default:
318 				return SIGNAL_TYPE_DVI_SINGLE_LINK;
319 			}
320 		break;
321 		case CONNECTOR_ID_DUAL_LINK_DVII:
322 		{
323 			switch (encoder.id) {
324 			case ENCODER_ID_INTERNAL_DAC1:
325 			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
326 			case ENCODER_ID_INTERNAL_DAC2:
327 			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
328 				return SIGNAL_TYPE_RGB;
329 			default:
330 				return SIGNAL_TYPE_DVI_DUAL_LINK;
331 			}
332 		}
333 		break;
334 		case CONNECTOR_ID_SINGLE_LINK_DVID:
335 			return SIGNAL_TYPE_DVI_SINGLE_LINK;
336 		case CONNECTOR_ID_DUAL_LINK_DVID:
337 			return SIGNAL_TYPE_DVI_DUAL_LINK;
338 		case CONNECTOR_ID_VGA:
339 			return SIGNAL_TYPE_RGB;
340 		case CONNECTOR_ID_HDMI_TYPE_A:
341 			return SIGNAL_TYPE_HDMI_TYPE_A;
342 		case CONNECTOR_ID_LVDS:
343 			return SIGNAL_TYPE_LVDS;
344 		case CONNECTOR_ID_DISPLAY_PORT:
345 			return SIGNAL_TYPE_DISPLAY_PORT;
346 		case CONNECTOR_ID_EDP:
347 			return SIGNAL_TYPE_EDP;
348 		default:
349 			return SIGNAL_TYPE_NONE;
350 		}
351 	} else if (downstream.type == OBJECT_TYPE_ENCODER) {
352 		switch (downstream.id) {
353 		case ENCODER_ID_EXTERNAL_NUTMEG:
354 		case ENCODER_ID_EXTERNAL_TRAVIS:
355 			return SIGNAL_TYPE_DISPLAY_PORT;
356 		default:
357 			return SIGNAL_TYPE_NONE;
358 		}
359 	}
360 
361 	return SIGNAL_TYPE_NONE;
362 }
363 
364 /*
365  * dc_link_is_dp_sink_present() - Check if there is a native DP
366  * or passive DP-HDMI dongle connected
367  */
dc_link_is_dp_sink_present(struct dc_link * link)368 bool dc_link_is_dp_sink_present(struct dc_link *link)
369 {
370 	enum gpio_result gpio_result;
371 	uint32_t clock_pin = 0;
372 	uint8_t retry = 0;
373 	struct ddc *ddc;
374 
375 	enum connector_id connector_id =
376 		dal_graphics_object_id_get_connector_id(link->link_id);
377 
378 	bool present =
379 		((connector_id == CONNECTOR_ID_DISPLAY_PORT) ||
380 		(connector_id == CONNECTOR_ID_EDP));
381 
382 	ddc = dal_ddc_service_get_ddc_pin(link->ddc);
383 
384 	if (!ddc) {
385 		BREAK_TO_DEBUGGER();
386 		return present;
387 	}
388 
389 	/* Open GPIO and set it to I2C mode */
390 	/* Note: this GpioMode_Input will be converted
391 	 * to GpioConfigType_I2cAuxDualMode in GPIO component,
392 	 * which indicates we need additional delay
393 	 */
394 
395 	if (dal_ddc_open(ddc, GPIO_MODE_INPUT,
396 			 GPIO_DDC_CONFIG_TYPE_MODE_I2C) != GPIO_RESULT_OK) {
397 		dal_ddc_close(ddc);
398 
399 		return present;
400 	}
401 
402 	/*
403 	 * Read GPIO: DP sink is present if both clock and data pins are zero
404 	 *
405 	 * [W/A] plug-unplug DP cable, sometimes customer board has
406 	 * one short pulse on clk_pin(1V, < 1ms). DP will be config to HDMI/DVI
407 	 * then monitor can't br light up. Add retry 3 times
408 	 * But in real passive dongle, it need additional 3ms to detect
409 	 */
410 	do {
411 		gpio_result = dal_gpio_get_value(ddc->pin_clock, &clock_pin);
412 		ASSERT(gpio_result == GPIO_RESULT_OK);
413 		if (clock_pin)
414 			udelay(1000);
415 		else
416 			break;
417 	} while (retry++ < 3);
418 
419 	present = (gpio_result == GPIO_RESULT_OK) && !clock_pin;
420 
421 	dal_ddc_close(ddc);
422 
423 	return present;
424 }
425 
426 /*
427  * @brief
428  * Detect output sink type
429  */
link_detect_sink(struct dc_link * link,enum dc_detect_reason reason)430 static enum signal_type link_detect_sink(struct dc_link *link,
431 					 enum dc_detect_reason reason)
432 {
433 	enum signal_type result;
434 	struct graphics_object_id enc_id;
435 
436 	if (link->is_dig_mapping_flexible)
437 		enc_id = (struct graphics_object_id){.id = ENCODER_ID_UNKNOWN};
438 	else
439 		enc_id = link->link_enc->id;
440 	result = get_basic_signal_type(enc_id, link->link_id);
441 
442 	/* Use basic signal type for link without physical connector. */
443 	if (link->ep_type != DISPLAY_ENDPOINT_PHY)
444 		return result;
445 
446 	/* Internal digital encoder will detect only dongles
447 	 * that require digital signal
448 	 */
449 
450 	/* Detection mechanism is different
451 	 * for different native connectors.
452 	 * LVDS connector supports only LVDS signal;
453 	 * PCIE is a bus slot, the actual connector needs to be detected first;
454 	 * eDP connector supports only eDP signal;
455 	 * HDMI should check straps for audio
456 	 */
457 
458 	/* PCIE detects the actual connector on add-on board */
459 	if (link->link_id.id == CONNECTOR_ID_PCIE) {
460 		/* ZAZTODO implement PCIE add-on card detection */
461 	}
462 
463 	switch (link->link_id.id) {
464 	case CONNECTOR_ID_HDMI_TYPE_A: {
465 		/* check audio support:
466 		 * if native HDMI is not supported, switch to DVI
467 		 */
468 		struct audio_support *aud_support =
469 					&link->dc->res_pool->audio_support;
470 
471 		if (!aud_support->hdmi_audio_native)
472 			if (link->link_id.id == CONNECTOR_ID_HDMI_TYPE_A)
473 				result = SIGNAL_TYPE_DVI_SINGLE_LINK;
474 	}
475 	break;
476 	case CONNECTOR_ID_DISPLAY_PORT: {
477 		/* DP HPD short pulse. Passive DP dongle will not
478 		 * have short pulse
479 		 */
480 		if (reason != DETECT_REASON_HPDRX) {
481 			/* Check whether DP signal detected: if not -
482 			 * we assume signal is DVI; it could be corrected
483 			 * to HDMI after dongle detection
484 			 */
485 			if (!dm_helpers_is_dp_sink_present(link))
486 				result = SIGNAL_TYPE_DVI_SINGLE_LINK;
487 		}
488 	}
489 	break;
490 	default:
491 	break;
492 	}
493 
494 	return result;
495 }
496 
decide_signal_from_strap_and_dongle_type(enum display_dongle_type dongle_type,struct audio_support * audio_support)497 static enum signal_type decide_signal_from_strap_and_dongle_type(enum display_dongle_type dongle_type,
498 								 struct audio_support *audio_support)
499 {
500 	enum signal_type signal = SIGNAL_TYPE_NONE;
501 
502 	switch (dongle_type) {
503 	case DISPLAY_DONGLE_DP_HDMI_DONGLE:
504 		if (audio_support->hdmi_audio_on_dongle)
505 			signal = SIGNAL_TYPE_HDMI_TYPE_A;
506 		else
507 			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
508 		break;
509 	case DISPLAY_DONGLE_DP_DVI_DONGLE:
510 		signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
511 		break;
512 	case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
513 		if (audio_support->hdmi_audio_native)
514 			signal =  SIGNAL_TYPE_HDMI_TYPE_A;
515 		else
516 			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
517 		break;
518 	default:
519 		signal = SIGNAL_TYPE_NONE;
520 		break;
521 	}
522 
523 	return signal;
524 }
525 
dp_passive_dongle_detection(struct ddc_service * ddc,struct display_sink_capability * sink_cap,struct audio_support * audio_support)526 static enum signal_type dp_passive_dongle_detection(struct ddc_service *ddc,
527 						    struct display_sink_capability *sink_cap,
528 						    struct audio_support *audio_support)
529 {
530 	dal_ddc_service_i2c_query_dp_dual_mode_adaptor(ddc, sink_cap);
531 
532 	return decide_signal_from_strap_and_dongle_type(sink_cap->dongle_type,
533 							audio_support);
534 }
535 
link_disconnect_sink(struct dc_link * link)536 static void link_disconnect_sink(struct dc_link *link)
537 {
538 	if (link->local_sink) {
539 		dc_sink_release(link->local_sink);
540 		link->local_sink = NULL;
541 	}
542 
543 	link->dpcd_sink_count = 0;
544 	//link->dpcd_caps.dpcd_rev.raw = 0;
545 }
546 
link_disconnect_remap(struct dc_sink * prev_sink,struct dc_link * link)547 static void link_disconnect_remap(struct dc_sink *prev_sink, struct dc_link *link)
548 {
549 	dc_sink_release(link->local_sink);
550 	link->local_sink = prev_sink;
551 }
552 
553 #if defined(CONFIG_DRM_AMD_DC_HDCP)
dc_link_is_hdcp14(struct dc_link * link,enum signal_type signal)554 bool dc_link_is_hdcp14(struct dc_link *link, enum signal_type signal)
555 {
556 	bool ret = false;
557 
558 	switch (signal)	{
559 	case SIGNAL_TYPE_DISPLAY_PORT:
560 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
561 		ret = link->hdcp_caps.bcaps.bits.HDCP_CAPABLE;
562 		break;
563 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
564 	case SIGNAL_TYPE_DVI_DUAL_LINK:
565 	case SIGNAL_TYPE_HDMI_TYPE_A:
566 	/* HDMI doesn't tell us its HDCP(1.4) capability, so assume to always be capable,
567 	 * we can poll for bksv but some displays have an issue with this. Since its so rare
568 	 * for a display to not be 1.4 capable, this assumtion is ok
569 	 */
570 		ret = true;
571 		break;
572 	default:
573 		break;
574 	}
575 	return ret;
576 }
577 
dc_link_is_hdcp22(struct dc_link * link,enum signal_type signal)578 bool dc_link_is_hdcp22(struct dc_link *link, enum signal_type signal)
579 {
580 	bool ret = false;
581 
582 	switch (signal)	{
583 	case SIGNAL_TYPE_DISPLAY_PORT:
584 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
585 		ret = (link->hdcp_caps.bcaps.bits.HDCP_CAPABLE &&
586 				link->hdcp_caps.rx_caps.fields.byte0.hdcp_capable &&
587 				(link->hdcp_caps.rx_caps.fields.version == 0x2)) ? 1 : 0;
588 		break;
589 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
590 	case SIGNAL_TYPE_DVI_DUAL_LINK:
591 	case SIGNAL_TYPE_HDMI_TYPE_A:
592 		ret = (link->hdcp_caps.rx_caps.fields.version == 0x4) ? 1:0;
593 		break;
594 	default:
595 		break;
596 	}
597 
598 	return ret;
599 }
600 
query_hdcp_capability(enum signal_type signal,struct dc_link * link)601 static void query_hdcp_capability(enum signal_type signal, struct dc_link *link)
602 {
603 	struct hdcp_protection_message msg22;
604 	struct hdcp_protection_message msg14;
605 
606 	memset(&msg22, 0, sizeof(struct hdcp_protection_message));
607 	memset(&msg14, 0, sizeof(struct hdcp_protection_message));
608 	memset(link->hdcp_caps.rx_caps.raw, 0,
609 		sizeof(link->hdcp_caps.rx_caps.raw));
610 
611 	if ((link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
612 			link->ddc->transaction_type ==
613 			DDC_TRANSACTION_TYPE_I2C_OVER_AUX) ||
614 			link->connector_signal == SIGNAL_TYPE_EDP) {
615 		msg22.data = link->hdcp_caps.rx_caps.raw;
616 		msg22.length = sizeof(link->hdcp_caps.rx_caps.raw);
617 		msg22.msg_id = HDCP_MESSAGE_ID_RX_CAPS;
618 	} else {
619 		msg22.data = &link->hdcp_caps.rx_caps.fields.version;
620 		msg22.length = sizeof(link->hdcp_caps.rx_caps.fields.version);
621 		msg22.msg_id = HDCP_MESSAGE_ID_HDCP2VERSION;
622 	}
623 	msg22.version = HDCP_VERSION_22;
624 	msg22.link = HDCP_LINK_PRIMARY;
625 	msg22.max_retries = 5;
626 	dc_process_hdcp_msg(signal, link, &msg22);
627 
628 	if (signal == SIGNAL_TYPE_DISPLAY_PORT || signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
629 		msg14.data = &link->hdcp_caps.bcaps.raw;
630 		msg14.length = sizeof(link->hdcp_caps.bcaps.raw);
631 		msg14.msg_id = HDCP_MESSAGE_ID_READ_BCAPS;
632 		msg14.version = HDCP_VERSION_14;
633 		msg14.link = HDCP_LINK_PRIMARY;
634 		msg14.max_retries = 5;
635 
636 		dc_process_hdcp_msg(signal, link, &msg14);
637 	}
638 
639 }
640 #endif
641 
read_current_link_settings_on_detect(struct dc_link * link)642 static void read_current_link_settings_on_detect(struct dc_link *link)
643 {
644 	union lane_count_set lane_count_set = { {0} };
645 	uint8_t link_bw_set;
646 	uint8_t link_rate_set;
647 	uint32_t read_dpcd_retry_cnt = 10;
648 	enum dc_status status = DC_ERROR_UNEXPECTED;
649 	int i;
650 	union max_down_spread max_down_spread = { {0} };
651 
652 	// Read DPCD 00101h to find out the number of lanes currently set
653 	for (i = 0; i < read_dpcd_retry_cnt; i++) {
654 		status = core_link_read_dpcd(link,
655 					     DP_LANE_COUNT_SET,
656 					     &lane_count_set.raw,
657 					     sizeof(lane_count_set));
658 		/* First DPCD read after VDD ON can fail if the particular board
659 		 * does not have HPD pin wired correctly. So if DPCD read fails,
660 		 * which it should never happen, retry a few times. Target worst
661 		 * case scenario of 80 ms.
662 		 */
663 		if (status == DC_OK) {
664 			link->cur_link_settings.lane_count =
665 					lane_count_set.bits.LANE_COUNT_SET;
666 			break;
667 		}
668 
669 		msleep(8);
670 	}
671 
672 	// Read DPCD 00100h to find if standard link rates are set
673 	core_link_read_dpcd(link, DP_LINK_BW_SET,
674 			    &link_bw_set, sizeof(link_bw_set));
675 
676 	if (link_bw_set == 0) {
677 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
678 			/* If standard link rates are not being used,
679 			 * Read DPCD 00115h to find the edp link rate set used
680 			 */
681 			core_link_read_dpcd(link, DP_LINK_RATE_SET,
682 					    &link_rate_set, sizeof(link_rate_set));
683 
684 			// edp_supported_link_rates_count = 0 for DP
685 			if (link_rate_set < link->dpcd_caps.edp_supported_link_rates_count) {
686 				link->cur_link_settings.link_rate =
687 					link->dpcd_caps.edp_supported_link_rates[link_rate_set];
688 				link->cur_link_settings.link_rate_set = link_rate_set;
689 				link->cur_link_settings.use_link_rate_set = true;
690 			}
691 		} else {
692 			// Link Rate not found. Seamless boot may not work.
693 			ASSERT(false);
694 		}
695 	} else {
696 		link->cur_link_settings.link_rate = link_bw_set;
697 		link->cur_link_settings.use_link_rate_set = false;
698 	}
699 	// Read DPCD 00003h to find the max down spread.
700 	core_link_read_dpcd(link, DP_MAX_DOWNSPREAD,
701 			    &max_down_spread.raw, sizeof(max_down_spread));
702 	link->cur_link_settings.link_spread =
703 		max_down_spread.bits.MAX_DOWN_SPREAD ?
704 		LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
705 }
706 
detect_dp(struct dc_link * link,struct display_sink_capability * sink_caps,enum dc_detect_reason reason)707 static bool detect_dp(struct dc_link *link,
708 		      struct display_sink_capability *sink_caps,
709 		      enum dc_detect_reason reason)
710 {
711 	struct audio_support *audio_support = &link->dc->res_pool->audio_support;
712 
713 	sink_caps->signal = link_detect_sink(link, reason);
714 	sink_caps->transaction_type =
715 		get_ddc_transaction_type(sink_caps->signal);
716 
717 	if (sink_caps->transaction_type == DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
718 		sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT;
719 		if (!detect_dp_sink_caps(link))
720 			return false;
721 		if (is_mst_supported(link)) {
722 			sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
723 			link->type = dc_connection_mst_branch;
724 
725 			dal_ddc_service_set_transaction_type(link->ddc,
726 							     sink_caps->transaction_type);
727 
728 #if defined(CONFIG_DRM_AMD_DC_HDCP)
729 			/* In case of fallback to SST when topology discovery below fails
730 			 * HDCP caps will be querried again later by the upper layer (caller
731 			 * of this function). */
732 			query_hdcp_capability(SIGNAL_TYPE_DISPLAY_PORT_MST, link);
733 #endif
734 		}
735 
736 		if (link->type != dc_connection_mst_branch &&
737 				is_dp_branch_device(link))
738 			/* DP SST branch */
739 			link->type = dc_connection_sst_branch;
740 	} else {
741 		/* DP passive dongles */
742 		sink_caps->signal = dp_passive_dongle_detection(link->ddc,
743 								sink_caps,
744 								audio_support);
745 		link->dpcd_caps.dongle_type = sink_caps->dongle_type;
746 		link->dpcd_caps.dpcd_rev.raw = 0;
747 	}
748 
749 	return true;
750 }
751 
is_same_edid(struct dc_edid * old_edid,struct dc_edid * new_edid)752 static bool is_same_edid(struct dc_edid *old_edid, struct dc_edid *new_edid)
753 {
754 	if (old_edid->length != new_edid->length)
755 		return false;
756 
757 	if (new_edid->length == 0)
758 		return false;
759 
760 	return (memcmp(old_edid->raw_edid,
761 		       new_edid->raw_edid, new_edid->length) == 0);
762 }
763 
wait_for_entering_dp_alt_mode(struct dc_link * link)764 static bool wait_for_entering_dp_alt_mode(struct dc_link *link)
765 {
766 	/**
767 	 * something is terribly wrong if time out is > 200ms. (5Hz)
768 	 * 500 microseconds * 400 tries us 200 ms
769 	 **/
770 	unsigned int sleep_time_in_microseconds = 500;
771 	unsigned int tries_allowed = 400;
772 	bool is_in_alt_mode;
773 	unsigned long long enter_timestamp;
774 	unsigned long long finish_timestamp;
775 	unsigned long long time_taken_in_ns;
776 	int tries_taken;
777 
778 	DC_LOGGER_INIT(link->ctx->logger);
779 
780 	if (!link->link_enc->funcs->is_in_alt_mode)
781 		return true;
782 
783 	is_in_alt_mode = link->link_enc->funcs->is_in_alt_mode(link->link_enc);
784 	DC_LOG_WARNING("DP Alt mode state on HPD: %d\n", is_in_alt_mode);
785 
786 	if (is_in_alt_mode)
787 		return true;
788 
789 	enter_timestamp = dm_get_timestamp(link->ctx);
790 
791 	for (tries_taken = 0; tries_taken < tries_allowed; tries_taken++) {
792 		udelay(sleep_time_in_microseconds);
793 		/* ask the link if alt mode is enabled, if so return ok */
794 		if (link->link_enc->funcs->is_in_alt_mode(link->link_enc)) {
795 			finish_timestamp = dm_get_timestamp(link->ctx);
796 			time_taken_in_ns =
797 				dm_get_elapse_time_in_ns(link->ctx,
798 							 finish_timestamp,
799 							 enter_timestamp);
800 			DC_LOG_WARNING("Alt mode entered finished after %llu ms\n",
801 				       div_u64(time_taken_in_ns, 1000000));
802 			return true;
803 		}
804 	}
805 	finish_timestamp = dm_get_timestamp(link->ctx);
806 	time_taken_in_ns = dm_get_elapse_time_in_ns(link->ctx, finish_timestamp,
807 						    enter_timestamp);
808 	DC_LOG_WARNING("Alt mode has timed out after %llu ms\n",
809 		       div_u64(time_taken_in_ns, 1000000));
810 	return false;
811 }
812 
813 /*
814  * dc_link_detect() - Detect if a sink is attached to a given link
815  *
816  * link->local_sink is created or destroyed as needed.
817  *
818  * This does not create remote sinks but will trigger DM
819  * to start MST detection if a branch is detected.
820  */
dc_link_detect_helper(struct dc_link * link,enum dc_detect_reason reason)821 static bool dc_link_detect_helper(struct dc_link *link,
822 				  enum dc_detect_reason reason)
823 {
824 	struct dc_sink_init_data sink_init_data = { 0 };
825 	struct display_sink_capability sink_caps = { 0 };
826 	uint32_t i;
827 	bool converter_disable_audio = false;
828 	struct audio_support *aud_support = &link->dc->res_pool->audio_support;
829 	bool same_edid = false;
830 	enum dc_edid_status edid_status;
831 	struct dc_context *dc_ctx = link->ctx;
832 	struct dc_sink *sink = NULL;
833 	struct dc_sink *prev_sink = NULL;
834 	struct dpcd_caps prev_dpcd_caps;
835 	enum dc_connection_type new_connection_type = dc_connection_none;
836 	enum dc_connection_type pre_connection_type = dc_connection_none;
837 	bool perform_dp_seamless_boot = false;
838 	const uint32_t post_oui_delay = 30; // 30ms
839 
840 	DC_LOGGER_INIT(link->ctx->logger);
841 
842 	if (dc_is_virtual_signal(link->connector_signal))
843 		return false;
844 
845 	if (((link->connector_signal == SIGNAL_TYPE_LVDS ||
846 		link->connector_signal == SIGNAL_TYPE_EDP) &&
847 		(!link->dc->config.allow_edp_hotplug_detection)) &&
848 		link->local_sink) {
849 		// need to re-write OUI and brightness in resume case
850 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
851 			dpcd_set_source_specific_data(link);
852 			msleep(post_oui_delay);
853 			dc_link_set_default_brightness_aux(link);
854 			//TODO: use cached
855 		}
856 
857 		return true;
858 	}
859 
860 	if (!dc_link_detect_sink(link, &new_connection_type)) {
861 		BREAK_TO_DEBUGGER();
862 		return false;
863 	}
864 
865 	prev_sink = link->local_sink;
866 	if (prev_sink) {
867 		dc_sink_retain(prev_sink);
868 		memcpy(&prev_dpcd_caps, &link->dpcd_caps, sizeof(struct dpcd_caps));
869 	}
870 
871 	link_disconnect_sink(link);
872 	if (new_connection_type != dc_connection_none) {
873 		pre_connection_type = link->type;
874 		link->type = new_connection_type;
875 		link->link_state_valid = false;
876 
877 		/* From Disconnected-to-Connected. */
878 		switch (link->connector_signal) {
879 		case SIGNAL_TYPE_HDMI_TYPE_A: {
880 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
881 			if (aud_support->hdmi_audio_native)
882 				sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
883 			else
884 				sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
885 			break;
886 		}
887 
888 		case SIGNAL_TYPE_DVI_SINGLE_LINK: {
889 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
890 			sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
891 			break;
892 		}
893 
894 		case SIGNAL_TYPE_DVI_DUAL_LINK: {
895 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
896 			sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
897 			break;
898 		}
899 
900 		case SIGNAL_TYPE_LVDS: {
901 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
902 			sink_caps.signal = SIGNAL_TYPE_LVDS;
903 			break;
904 		}
905 
906 		case SIGNAL_TYPE_EDP: {
907 			read_current_link_settings_on_detect(link);
908 
909 			detect_edp_sink_caps(link);
910 			read_current_link_settings_on_detect(link);
911 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
912 			sink_caps.signal = SIGNAL_TYPE_EDP;
913 			break;
914 		}
915 
916 		case SIGNAL_TYPE_DISPLAY_PORT: {
917 			/* wa HPD high coming too early*/
918 			if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
919 			    link->link_enc->features.flags.bits.DP_IS_USB_C == 1) {
920 				/* if alt mode times out, return false */
921 				if (!wait_for_entering_dp_alt_mode(link))
922 					return false;
923 			}
924 
925 			if (!detect_dp(link, &sink_caps, reason)) {
926 				if (prev_sink)
927 					dc_sink_release(prev_sink);
928 				return false;
929 			}
930 
931 			if (link->type == dc_connection_mst_branch) {
932 				LINK_INFO("link=%d, mst branch is now Connected\n",
933 					  link->link_index);
934 				/* Need to setup mst link_cap struct here
935 				 * otherwise dc_link_detect() will leave mst link_cap
936 				 * empty which leads to allocate_mst_payload() has "0"
937 				 * pbn_per_slot value leading to exception on dc_fixpt_div()
938 				 */
939 				dp_verify_mst_link_cap(link);
940 
941 				/*
942 				 * This call will initiate MST topology discovery. Which
943 				 * will detect MST ports and add new DRM connector DRM
944 				 * framework. Then read EDID via remote i2c over aux. In
945 				 * the end, will notify DRM detect result and save EDID
946 				 * into DRM framework.
947 				 *
948 				 * .detect is called by .fill_modes.
949 				 * .fill_modes is called by user mode ioctl
950 				 * DRM_IOCTL_MODE_GETCONNECTOR.
951 				 *
952 				 * .get_modes is called by .fill_modes.
953 				 *
954 				 * call .get_modes, AMDGPU DM implementation will create
955 				 * new dc_sink and add to dc_link. For long HPD plug
956 				 * in/out, MST has its own handle.
957 				 *
958 				 * Therefore, just after dc_create, link->sink is not
959 				 * created for MST until user mode app calls
960 				 * DRM_IOCTL_MODE_GETCONNECTOR.
961 				 *
962 				 * Need check ->sink usages in case ->sink = NULL
963 				 * TODO: s3 resume check
964 				 */
965 
966 				dm_helpers_dp_update_branch_info(link->ctx, link);
967 				if (dm_helpers_dp_mst_start_top_mgr(link->ctx,
968 						link, reason == DETECT_REASON_BOOT)) {
969 					if (prev_sink)
970 						dc_sink_release(prev_sink);
971 					return false;
972 				} else {
973 					link->type = dc_connection_sst_branch;
974 					sink_caps.signal = SIGNAL_TYPE_DISPLAY_PORT;
975 				}
976 			}
977 
978 			/* Active SST downstream branch device unplug*/
979 			if (link->type == dc_connection_sst_branch &&
980 			    link->dpcd_caps.sink_count.bits.SINK_COUNT == 0) {
981 				if (prev_sink)
982 					/* Downstream unplug */
983 					dc_sink_release(prev_sink);
984 				return true;
985 			}
986 
987 			/* disable audio for non DP to HDMI active sst converter */
988 			if (link->type == dc_connection_sst_branch &&
989 					is_dp_active_dongle(link) &&
990 					(link->dpcd_caps.dongle_type !=
991 							DISPLAY_DONGLE_DP_HDMI_CONVERTER))
992 				converter_disable_audio = true;
993 
994 			// link switch from MST to non-MST stop topology manager
995 			if (pre_connection_type == dc_connection_mst_branch &&
996 					link->type != dc_connection_mst_branch)
997 				dm_helpers_dp_mst_stop_top_mgr(link->ctx, link);
998 
999 
1000 			// For seamless boot, to skip verify link cap, we read UEFI settings and set them as verified.
1001 			if (reason == DETECT_REASON_BOOT &&
1002 					!dc_ctx->dc->config.power_down_display_on_boot &&
1003 					link->link_status.link_active)
1004 				perform_dp_seamless_boot = true;
1005 
1006 			if (perform_dp_seamless_boot) {
1007 				read_current_link_settings_on_detect(link);
1008 				link->verified_link_cap = link->reported_link_cap;
1009 			}
1010 
1011 			break;
1012 		}
1013 
1014 		default:
1015 			DC_ERROR("Invalid connector type! signal:%d\n",
1016 				 link->connector_signal);
1017 			if (prev_sink)
1018 				dc_sink_release(prev_sink);
1019 			return false;
1020 		} /* switch() */
1021 
1022 		if (link->dpcd_caps.sink_count.bits.SINK_COUNT)
1023 			link->dpcd_sink_count =
1024 				link->dpcd_caps.sink_count.bits.SINK_COUNT;
1025 		else
1026 			link->dpcd_sink_count = 1;
1027 
1028 		dal_ddc_service_set_transaction_type(link->ddc,
1029 						     sink_caps.transaction_type);
1030 
1031 		link->aux_mode =
1032 			dal_ddc_service_is_in_aux_transaction_mode(link->ddc);
1033 
1034 		sink_init_data.link = link;
1035 		sink_init_data.sink_signal = sink_caps.signal;
1036 
1037 		sink = dc_sink_create(&sink_init_data);
1038 		if (!sink) {
1039 			DC_ERROR("Failed to create sink!\n");
1040 			if (prev_sink)
1041 				dc_sink_release(prev_sink);
1042 			return false;
1043 		}
1044 
1045 		sink->link->dongle_max_pix_clk = sink_caps.max_hdmi_pixel_clock;
1046 		sink->converter_disable_audio = converter_disable_audio;
1047 
1048 		/* dc_sink_create returns a new reference */
1049 		link->local_sink = sink;
1050 
1051 		edid_status = dm_helpers_read_local_edid(link->ctx,
1052 							 link, sink);
1053 
1054 		switch (edid_status) {
1055 		case EDID_BAD_CHECKSUM:
1056 			DC_LOG_ERROR("EDID checksum invalid.\n");
1057 			break;
1058 		case EDID_NO_RESPONSE:
1059 			DC_LOG_ERROR("No EDID read.\n");
1060 			/*
1061 			 * Abort detection for non-DP connectors if we have
1062 			 * no EDID
1063 			 *
1064 			 * DP needs to report as connected if HDP is high
1065 			 * even if we have no EDID in order to go to
1066 			 * fail-safe mode
1067 			 */
1068 			if (dc_is_hdmi_signal(link->connector_signal) ||
1069 			    dc_is_dvi_signal(link->connector_signal)) {
1070 				if (prev_sink)
1071 					dc_sink_release(prev_sink);
1072 
1073 				return false;
1074 			}
1075 			break;
1076 		default:
1077 			break;
1078 		}
1079 
1080 		// Check if edid is the same
1081 		if ((prev_sink) &&
1082 		    (edid_status == EDID_THE_SAME || edid_status == EDID_OK))
1083 			same_edid = is_same_edid(&prev_sink->dc_edid,
1084 						 &sink->dc_edid);
1085 
1086 		if (sink->edid_caps.panel_patch.skip_scdc_overwrite)
1087 			link->ctx->dc->debug.hdmi20_disable = true;
1088 
1089 		if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
1090 		    sink_caps.transaction_type ==
1091 		    DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
1092 			/*
1093 			 * TODO debug why Dell 2413 doesn't like
1094 			 *  two link trainings
1095 			 */
1096 #if defined(CONFIG_DRM_AMD_DC_HDCP)
1097 			query_hdcp_capability(sink->sink_signal, link);
1098 #endif
1099 
1100 			// verify link cap for SST non-seamless boot
1101 			if (!perform_dp_seamless_boot)
1102 				dp_verify_link_cap_with_retries(link,
1103 								&link->reported_link_cap,
1104 								LINK_TRAINING_MAX_VERIFY_RETRY);
1105 		} else {
1106 			// If edid is the same, then discard new sink and revert back to original sink
1107 			if (same_edid) {
1108 				link_disconnect_remap(prev_sink, link);
1109 				sink = prev_sink;
1110 				prev_sink = NULL;
1111 			}
1112 #if defined(CONFIG_DRM_AMD_DC_HDCP)
1113 			query_hdcp_capability(sink->sink_signal, link);
1114 #endif
1115 		}
1116 
1117 		/* HDMI-DVI Dongle */
1118 		if (sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A &&
1119 		    !sink->edid_caps.edid_hdmi)
1120 			sink->sink_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1121 
1122 		/* Connectivity log: detection */
1123 		for (i = 0; i < sink->dc_edid.length / DC_EDID_BLOCK_SIZE; i++) {
1124 			CONN_DATA_DETECT(link,
1125 					 &sink->dc_edid.raw_edid[i * DC_EDID_BLOCK_SIZE],
1126 					 DC_EDID_BLOCK_SIZE,
1127 					 "%s: [Block %d] ", sink->edid_caps.display_name, i);
1128 		}
1129 
1130 		DC_LOG_DETECTION_EDID_PARSER("%s: "
1131 			"manufacturer_id = %X, "
1132 			"product_id = %X, "
1133 			"serial_number = %X, "
1134 			"manufacture_week = %d, "
1135 			"manufacture_year = %d, "
1136 			"display_name = %s, "
1137 			"speaker_flag = %d, "
1138 			"audio_mode_count = %d\n",
1139 			__func__,
1140 			sink->edid_caps.manufacturer_id,
1141 			sink->edid_caps.product_id,
1142 			sink->edid_caps.serial_number,
1143 			sink->edid_caps.manufacture_week,
1144 			sink->edid_caps.manufacture_year,
1145 			sink->edid_caps.display_name,
1146 			sink->edid_caps.speaker_flags,
1147 			sink->edid_caps.audio_mode_count);
1148 
1149 		for (i = 0; i < sink->edid_caps.audio_mode_count; i++) {
1150 			DC_LOG_DETECTION_EDID_PARSER("%s: mode number = %d, "
1151 				"format_code = %d, "
1152 				"channel_count = %d, "
1153 				"sample_rate = %d, "
1154 				"sample_size = %d\n",
1155 				__func__,
1156 				i,
1157 				sink->edid_caps.audio_modes[i].format_code,
1158 				sink->edid_caps.audio_modes[i].channel_count,
1159 				sink->edid_caps.audio_modes[i].sample_rate,
1160 				sink->edid_caps.audio_modes[i].sample_size);
1161 		}
1162 	} else {
1163 		/* From Connected-to-Disconnected. */
1164 		if (link->type == dc_connection_mst_branch) {
1165 			LINK_INFO("link=%d, mst branch is now Disconnected\n",
1166 				  link->link_index);
1167 
1168 			dm_helpers_dp_mst_stop_top_mgr(link->ctx, link);
1169 
1170 			link->mst_stream_alloc_table.stream_count = 0;
1171 			memset(link->mst_stream_alloc_table.stream_allocations,
1172 			       0,
1173 			       sizeof(link->mst_stream_alloc_table.stream_allocations));
1174 		}
1175 
1176 		link->type = dc_connection_none;
1177 		sink_caps.signal = SIGNAL_TYPE_NONE;
1178 		/* When we unplug a passive DP-HDMI dongle connection, dongle_max_pix_clk
1179 		 *  is not cleared. If we emulate a DP signal on this connection, it thinks
1180 		 *  the dongle is still there and limits the number of modes we can emulate.
1181 		 *  Clear dongle_max_pix_clk on disconnect to fix this
1182 		 */
1183 		link->dongle_max_pix_clk = 0;
1184 	}
1185 
1186 	LINK_INFO("link=%d, dc_sink_in=%p is now %s prev_sink=%p edid same=%d\n",
1187 		  link->link_index, sink,
1188 		  (sink_caps.signal ==
1189 		   SIGNAL_TYPE_NONE ? "Disconnected" : "Connected"),
1190 		  prev_sink, same_edid);
1191 
1192 	if (prev_sink)
1193 		dc_sink_release(prev_sink);
1194 
1195 	return true;
1196 }
1197 
dc_link_detect(struct dc_link * link,enum dc_detect_reason reason)1198 bool dc_link_detect(struct dc_link *link, enum dc_detect_reason reason)
1199 {
1200 	const struct dc *dc = link->dc;
1201 	bool ret;
1202 	bool can_apply_seamless_boot = false;
1203 	int i;
1204 
1205 	for (i = 0; i < dc->current_state->stream_count; i++) {
1206 		if (dc->current_state->streams[i]->apply_seamless_boot_optimization) {
1207 			can_apply_seamless_boot = true;
1208 			break;
1209 		}
1210 	}
1211 
1212 	/* get out of low power state */
1213 	if (!can_apply_seamless_boot && reason != DETECT_REASON_BOOT)
1214 		clk_mgr_exit_optimized_pwr_state(dc, dc->clk_mgr);
1215 
1216 	ret = dc_link_detect_helper(link, reason);
1217 
1218 	/* Go back to power optimized state */
1219 	if (!can_apply_seamless_boot && reason != DETECT_REASON_BOOT)
1220 		clk_mgr_optimize_pwr_state(dc, dc->clk_mgr);
1221 
1222 	return ret;
1223 }
1224 
dc_link_get_hpd_state(struct dc_link * dc_link)1225 bool dc_link_get_hpd_state(struct dc_link *dc_link)
1226 {
1227 	uint32_t state;
1228 
1229 	dal_gpio_lock_pin(dc_link->hpd_gpio);
1230 	dal_gpio_get_value(dc_link->hpd_gpio, &state);
1231 	dal_gpio_unlock_pin(dc_link->hpd_gpio);
1232 
1233 	return state;
1234 }
1235 
get_hpd_line(struct dc_link * link)1236 static enum hpd_source_id get_hpd_line(struct dc_link *link)
1237 {
1238 	struct gpio *hpd;
1239 	enum hpd_source_id hpd_id = HPD_SOURCEID_UNKNOWN;
1240 
1241 	hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
1242 			   link->ctx->gpio_service);
1243 
1244 	if (hpd) {
1245 		switch (dal_irq_get_source(hpd)) {
1246 		case DC_IRQ_SOURCE_HPD1:
1247 			hpd_id = HPD_SOURCEID1;
1248 		break;
1249 		case DC_IRQ_SOURCE_HPD2:
1250 			hpd_id = HPD_SOURCEID2;
1251 		break;
1252 		case DC_IRQ_SOURCE_HPD3:
1253 			hpd_id = HPD_SOURCEID3;
1254 		break;
1255 		case DC_IRQ_SOURCE_HPD4:
1256 			hpd_id = HPD_SOURCEID4;
1257 		break;
1258 		case DC_IRQ_SOURCE_HPD5:
1259 			hpd_id = HPD_SOURCEID5;
1260 		break;
1261 		case DC_IRQ_SOURCE_HPD6:
1262 			hpd_id = HPD_SOURCEID6;
1263 		break;
1264 		default:
1265 			BREAK_TO_DEBUGGER();
1266 		break;
1267 		}
1268 
1269 		dal_gpio_destroy_irq(&hpd);
1270 	}
1271 
1272 	return hpd_id;
1273 }
1274 
get_ddc_line(struct dc_link * link)1275 static enum channel_id get_ddc_line(struct dc_link *link)
1276 {
1277 	struct ddc *ddc;
1278 	enum channel_id channel = CHANNEL_ID_UNKNOWN;
1279 
1280 	ddc = dal_ddc_service_get_ddc_pin(link->ddc);
1281 
1282 	if (ddc) {
1283 		switch (dal_ddc_get_line(ddc)) {
1284 		case GPIO_DDC_LINE_DDC1:
1285 			channel = CHANNEL_ID_DDC1;
1286 			break;
1287 		case GPIO_DDC_LINE_DDC2:
1288 			channel = CHANNEL_ID_DDC2;
1289 			break;
1290 		case GPIO_DDC_LINE_DDC3:
1291 			channel = CHANNEL_ID_DDC3;
1292 			break;
1293 		case GPIO_DDC_LINE_DDC4:
1294 			channel = CHANNEL_ID_DDC4;
1295 			break;
1296 		case GPIO_DDC_LINE_DDC5:
1297 			channel = CHANNEL_ID_DDC5;
1298 			break;
1299 		case GPIO_DDC_LINE_DDC6:
1300 			channel = CHANNEL_ID_DDC6;
1301 			break;
1302 		case GPIO_DDC_LINE_DDC_VGA:
1303 			channel = CHANNEL_ID_DDC_VGA;
1304 			break;
1305 		case GPIO_DDC_LINE_I2C_PAD:
1306 			channel = CHANNEL_ID_I2C_PAD;
1307 			break;
1308 		default:
1309 			BREAK_TO_DEBUGGER();
1310 			break;
1311 		}
1312 	}
1313 
1314 	return channel;
1315 }
1316 
translate_encoder_to_transmitter(struct graphics_object_id encoder)1317 static enum transmitter translate_encoder_to_transmitter(struct graphics_object_id encoder)
1318 {
1319 	switch (encoder.id) {
1320 	case ENCODER_ID_INTERNAL_UNIPHY:
1321 		switch (encoder.enum_id) {
1322 		case ENUM_ID_1:
1323 			return TRANSMITTER_UNIPHY_A;
1324 		case ENUM_ID_2:
1325 			return TRANSMITTER_UNIPHY_B;
1326 		default:
1327 			return TRANSMITTER_UNKNOWN;
1328 		}
1329 	break;
1330 	case ENCODER_ID_INTERNAL_UNIPHY1:
1331 		switch (encoder.enum_id) {
1332 		case ENUM_ID_1:
1333 			return TRANSMITTER_UNIPHY_C;
1334 		case ENUM_ID_2:
1335 			return TRANSMITTER_UNIPHY_D;
1336 		default:
1337 			return TRANSMITTER_UNKNOWN;
1338 		}
1339 	break;
1340 	case ENCODER_ID_INTERNAL_UNIPHY2:
1341 		switch (encoder.enum_id) {
1342 		case ENUM_ID_1:
1343 			return TRANSMITTER_UNIPHY_E;
1344 		case ENUM_ID_2:
1345 			return TRANSMITTER_UNIPHY_F;
1346 		default:
1347 			return TRANSMITTER_UNKNOWN;
1348 		}
1349 	break;
1350 	case ENCODER_ID_INTERNAL_UNIPHY3:
1351 		switch (encoder.enum_id) {
1352 		case ENUM_ID_1:
1353 			return TRANSMITTER_UNIPHY_G;
1354 		default:
1355 			return TRANSMITTER_UNKNOWN;
1356 		}
1357 	break;
1358 	case ENCODER_ID_EXTERNAL_NUTMEG:
1359 		switch (encoder.enum_id) {
1360 		case ENUM_ID_1:
1361 			return TRANSMITTER_NUTMEG_CRT;
1362 		default:
1363 			return TRANSMITTER_UNKNOWN;
1364 		}
1365 	break;
1366 	case ENCODER_ID_EXTERNAL_TRAVIS:
1367 		switch (encoder.enum_id) {
1368 		case ENUM_ID_1:
1369 			return TRANSMITTER_TRAVIS_CRT;
1370 		case ENUM_ID_2:
1371 			return TRANSMITTER_TRAVIS_LCD;
1372 		default:
1373 			return TRANSMITTER_UNKNOWN;
1374 		}
1375 	break;
1376 	default:
1377 		return TRANSMITTER_UNKNOWN;
1378 	}
1379 }
1380 
dc_link_construct(struct dc_link * link,const struct link_init_data * init_params)1381 static bool dc_link_construct(struct dc_link *link,
1382 			      const struct link_init_data *init_params)
1383 {
1384 	uint8_t i;
1385 	struct ddc_service_init_data ddc_service_init_data = { { 0 } };
1386 	struct dc_context *dc_ctx = init_params->ctx;
1387 	struct encoder_init_data enc_init_data = { 0 };
1388 	struct panel_cntl_init_data panel_cntl_init_data = { 0 };
1389 	struct integrated_info *info;
1390 	struct dc_bios *bios = init_params->dc->ctx->dc_bios;
1391 	const struct dc_vbios_funcs *bp_funcs = bios->funcs;
1392 	struct bp_disp_connector_caps_info disp_connect_caps_info = { 0 };
1393 
1394 	DC_LOGGER_INIT(dc_ctx->logger);
1395 
1396 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1397 	if (!info)
1398 		goto create_fail;
1399 
1400 	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
1401 	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
1402 
1403 	link->link_status.dpcd_caps = &link->dpcd_caps;
1404 
1405 	link->dc = init_params->dc;
1406 	link->ctx = dc_ctx;
1407 	link->link_index = init_params->link_index;
1408 
1409 	memset(&link->preferred_training_settings, 0,
1410 	       sizeof(struct dc_link_training_overrides));
1411 	memset(&link->preferred_link_setting, 0,
1412 	       sizeof(struct dc_link_settings));
1413 
1414 	link->link_id =
1415 		bios->funcs->get_connector_id(bios, init_params->connector_index);
1416 
1417 	link->ep_type = DISPLAY_ENDPOINT_PHY;
1418 
1419 	DC_LOG_DC("BIOS object table - link_id: %d", link->link_id.id);
1420 
1421 	if (bios->funcs->get_disp_connector_caps_info) {
1422 		bios->funcs->get_disp_connector_caps_info(bios, link->link_id, &disp_connect_caps_info);
1423 		link->is_internal_display = disp_connect_caps_info.INTERNAL_DISPLAY;
1424 		DC_LOG_DC("BIOS object table - is_internal_display: %d", link->is_internal_display);
1425 	}
1426 
1427 	if (link->link_id.type != OBJECT_TYPE_CONNECTOR) {
1428 		dm_output_to_console("%s: Invalid Connector ObjectID from Adapter Service for connector index:%d! type %d expected %d\n",
1429 				     __func__, init_params->connector_index,
1430 				     link->link_id.type, OBJECT_TYPE_CONNECTOR);
1431 		goto create_fail;
1432 	}
1433 
1434 	if (link->dc->res_pool->funcs->link_init)
1435 		link->dc->res_pool->funcs->link_init(link);
1436 
1437 	link->hpd_gpio = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
1438 				      link->ctx->gpio_service);
1439 
1440 	if (link->hpd_gpio) {
1441 		dal_gpio_open(link->hpd_gpio, GPIO_MODE_INTERRUPT);
1442 		dal_gpio_unlock_pin(link->hpd_gpio);
1443 		link->irq_source_hpd = dal_irq_get_source(link->hpd_gpio);
1444 
1445 		DC_LOG_DC("BIOS object table - hpd_gpio id: %d", link->hpd_gpio->id);
1446 		DC_LOG_DC("BIOS object table - hpd_gpio en: %d", link->hpd_gpio->en);
1447 	}
1448 
1449 	switch (link->link_id.id) {
1450 	case CONNECTOR_ID_HDMI_TYPE_A:
1451 		link->connector_signal = SIGNAL_TYPE_HDMI_TYPE_A;
1452 
1453 		break;
1454 	case CONNECTOR_ID_SINGLE_LINK_DVID:
1455 	case CONNECTOR_ID_SINGLE_LINK_DVII:
1456 		link->connector_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1457 		break;
1458 	case CONNECTOR_ID_DUAL_LINK_DVID:
1459 	case CONNECTOR_ID_DUAL_LINK_DVII:
1460 		link->connector_signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1461 		break;
1462 	case CONNECTOR_ID_DISPLAY_PORT:
1463 		link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
1464 
1465 		if (link->hpd_gpio)
1466 			link->irq_source_hpd_rx =
1467 					dal_irq_get_rx_source(link->hpd_gpio);
1468 
1469 		break;
1470 	case CONNECTOR_ID_EDP:
1471 		link->connector_signal = SIGNAL_TYPE_EDP;
1472 
1473 		if (link->hpd_gpio) {
1474 			if (!link->dc->config.allow_edp_hotplug_detection)
1475 				link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
1476 			link->irq_source_hpd_rx =
1477 					dal_irq_get_rx_source(link->hpd_gpio);
1478 		}
1479 
1480 		break;
1481 	case CONNECTOR_ID_LVDS:
1482 		link->connector_signal = SIGNAL_TYPE_LVDS;
1483 		break;
1484 	default:
1485 		DC_LOG_WARNING("Unsupported Connector type:%d!\n",
1486 			       link->link_id.id);
1487 		goto create_fail;
1488 	}
1489 
1490 	/* TODO: #DAL3 Implement id to str function.*/
1491 	LINK_INFO("Connector[%d] description:"
1492 		  "signal %d\n",
1493 		  init_params->connector_index,
1494 		  link->connector_signal);
1495 
1496 	ddc_service_init_data.ctx = link->ctx;
1497 	ddc_service_init_data.id = link->link_id;
1498 	ddc_service_init_data.link = link;
1499 	link->ddc = dal_ddc_service_create(&ddc_service_init_data);
1500 
1501 	if (!link->ddc) {
1502 		DC_ERROR("Failed to create ddc_service!\n");
1503 		goto ddc_create_fail;
1504 	}
1505 
1506 	if (!link->ddc->ddc_pin) {
1507 		DC_ERROR("Failed to get I2C info for connector!\n");
1508 		goto ddc_create_fail;
1509 	}
1510 
1511 	link->ddc_hw_inst =
1512 		dal_ddc_get_line(dal_ddc_service_get_ddc_pin(link->ddc));
1513 
1514 
1515 	if (link->dc->res_pool->funcs->panel_cntl_create &&
1516 		(link->link_id.id == CONNECTOR_ID_EDP ||
1517 			link->link_id.id == CONNECTOR_ID_LVDS)) {
1518 		panel_cntl_init_data.ctx = dc_ctx;
1519 		panel_cntl_init_data.inst =
1520 			panel_cntl_init_data.ctx->dc_edp_id_count;
1521 		link->panel_cntl =
1522 			link->dc->res_pool->funcs->panel_cntl_create(
1523 								&panel_cntl_init_data);
1524 		panel_cntl_init_data.ctx->dc_edp_id_count++;
1525 
1526 		if (link->panel_cntl == NULL) {
1527 			DC_ERROR("Failed to create link panel_cntl!\n");
1528 			goto panel_cntl_create_fail;
1529 		}
1530 	}
1531 
1532 	enc_init_data.ctx = dc_ctx;
1533 	bp_funcs->get_src_obj(dc_ctx->dc_bios, link->link_id, 0,
1534 			      &enc_init_data.encoder);
1535 	enc_init_data.connector = link->link_id;
1536 	enc_init_data.channel = get_ddc_line(link);
1537 	enc_init_data.hpd_source = get_hpd_line(link);
1538 
1539 	link->hpd_src = enc_init_data.hpd_source;
1540 
1541 	enc_init_data.transmitter =
1542 		translate_encoder_to_transmitter(enc_init_data.encoder);
1543 	link->link_enc =
1544 		link->dc->res_pool->funcs->link_enc_create(&enc_init_data);
1545 
1546 	if (!link->link_enc) {
1547 		DC_ERROR("Failed to create link encoder!\n");
1548 		goto link_enc_create_fail;
1549 	}
1550 
1551 	DC_LOG_DC("BIOS object table - DP_IS_USB_C: %d", link->link_enc->features.flags.bits.DP_IS_USB_C);
1552 
1553 	/* Update link encoder tracking variables. These are used for the dynamic
1554 	 * assignment of link encoders to streams.
1555 	 */
1556 	link->eng_id = link->link_enc->preferred_engine;
1557 	link->dc->res_pool->link_encoders[link->eng_id - ENGINE_ID_DIGA] = link->link_enc;
1558 	link->dc->res_pool->dig_link_enc_count++;
1559 
1560 	link->link_enc_hw_inst = link->link_enc->transmitter;
1561 
1562 	for (i = 0; i < 4; i++) {
1563 		if (bp_funcs->get_device_tag(dc_ctx->dc_bios,
1564 					     link->link_id, i,
1565 					     &link->device_tag) != BP_RESULT_OK) {
1566 			DC_ERROR("Failed to find device tag!\n");
1567 			goto device_tag_fail;
1568 		}
1569 
1570 		/* Look for device tag that matches connector signal,
1571 		 * CRT for rgb, LCD for other supported signal tyes
1572 		 */
1573 		if (!bp_funcs->is_device_id_supported(dc_ctx->dc_bios,
1574 						      link->device_tag.dev_id))
1575 			continue;
1576 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_CRT &&
1577 		    link->connector_signal != SIGNAL_TYPE_RGB)
1578 			continue;
1579 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_LCD &&
1580 		    link->connector_signal == SIGNAL_TYPE_RGB)
1581 			continue;
1582 
1583 		DC_LOG_DC("BIOS object table - device_tag.acpi_device: %d", link->device_tag.acpi_device);
1584 		DC_LOG_DC("BIOS object table - device_tag.dev_id.device_type: %d", link->device_tag.dev_id.device_type);
1585 		DC_LOG_DC("BIOS object table - device_tag.dev_id.enum_id: %d", link->device_tag.dev_id.enum_id);
1586 		break;
1587 	}
1588 
1589 	if (bios->integrated_info)
1590 		memcpy(info, bios->integrated_info, sizeof(*info));
1591 
1592 	/* Look for channel mapping corresponding to connector and device tag */
1593 	for (i = 0; i < MAX_NUMBER_OF_EXT_DISPLAY_PATH; i++) {
1594 		struct external_display_path *path =
1595 			&info->ext_disp_conn_info.path[i];
1596 
1597 		if (path->device_connector_id.enum_id == link->link_id.enum_id &&
1598 		    path->device_connector_id.id == link->link_id.id &&
1599 		    path->device_connector_id.type == link->link_id.type) {
1600 			if (link->device_tag.acpi_device != 0 &&
1601 			    path->device_acpi_enum == link->device_tag.acpi_device) {
1602 				link->ddi_channel_mapping = path->channel_mapping;
1603 				link->chip_caps = path->caps;
1604 				DC_LOG_DC("BIOS object table - ddi_channel_mapping: 0x%04X", link->ddi_channel_mapping.raw);
1605 				DC_LOG_DC("BIOS object table - chip_caps: %d", link->chip_caps);
1606 			} else if (path->device_tag ==
1607 				   link->device_tag.dev_id.raw_device_tag) {
1608 				link->ddi_channel_mapping = path->channel_mapping;
1609 				link->chip_caps = path->caps;
1610 				DC_LOG_DC("BIOS object table - ddi_channel_mapping: 0x%04X", link->ddi_channel_mapping.raw);
1611 				DC_LOG_DC("BIOS object table - chip_caps: %d", link->chip_caps);
1612 			}
1613 			break;
1614 		}
1615 	}
1616 
1617 	if (bios->funcs->get_atom_dc_golden_table)
1618 		bios->funcs->get_atom_dc_golden_table(bios);
1619 
1620 	/*
1621 	 * TODO check if GPIO programmed correctly
1622 	 *
1623 	 * If GPIO isn't programmed correctly HPD might not rise or drain
1624 	 * fast enough, leading to bounces.
1625 	 */
1626 	program_hpd_filter(link);
1627 
1628 	link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
1629 
1630 	DC_LOG_DC("BIOS object table - %s finished successfully.\n", __func__);
1631 	kfree(info);
1632 	return true;
1633 device_tag_fail:
1634 	link->link_enc->funcs->destroy(&link->link_enc);
1635 link_enc_create_fail:
1636 	if (link->panel_cntl != NULL)
1637 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
1638 panel_cntl_create_fail:
1639 	dal_ddc_service_destroy(&link->ddc);
1640 ddc_create_fail:
1641 create_fail:
1642 
1643 	if (link->hpd_gpio) {
1644 		dal_gpio_destroy_irq(&link->hpd_gpio);
1645 		link->hpd_gpio = NULL;
1646 	}
1647 
1648 	DC_LOG_DC("BIOS object table - %s failed.\n", __func__);
1649 	kfree(info);
1650 
1651 	return false;
1652 }
1653 
1654 /*******************************************************************************
1655  * Public functions
1656  ******************************************************************************/
link_create(const struct link_init_data * init_params)1657 struct dc_link *link_create(const struct link_init_data *init_params)
1658 {
1659 	struct dc_link *link =
1660 			kzalloc(sizeof(*link), GFP_KERNEL);
1661 
1662 	if (NULL == link)
1663 		goto alloc_fail;
1664 
1665 	if (false == dc_link_construct(link, init_params))
1666 		goto construct_fail;
1667 
1668 	return link;
1669 
1670 construct_fail:
1671 	kfree(link);
1672 
1673 alloc_fail:
1674 	return NULL;
1675 }
1676 
link_destroy(struct dc_link ** link)1677 void link_destroy(struct dc_link **link)
1678 {
1679 	dc_link_destruct(*link);
1680 	kfree(*link);
1681 	*link = NULL;
1682 }
1683 
enable_stream_features(struct pipe_ctx * pipe_ctx)1684 static void enable_stream_features(struct pipe_ctx *pipe_ctx)
1685 {
1686 	struct dc_stream_state *stream = pipe_ctx->stream;
1687 
1688 	if (pipe_ctx->stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST) {
1689 		struct dc_link *link = stream->link;
1690 		union down_spread_ctrl old_downspread;
1691 		union down_spread_ctrl new_downspread;
1692 
1693 		memset(&old_downspread, 0, sizeof(old_downspread));
1694 
1695 		core_link_read_dpcd(link, DP_DOWNSPREAD_CTRL,
1696 				&old_downspread.raw, sizeof(old_downspread));
1697 
1698 		new_downspread.raw = old_downspread.raw;
1699 
1700 		new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1701 				(stream->ignore_msa_timing_param) ? 1 : 0;
1702 
1703 		if (new_downspread.raw != old_downspread.raw) {
1704 			core_link_write_dpcd(link, DP_DOWNSPREAD_CTRL,
1705 				&new_downspread.raw, sizeof(new_downspread));
1706 		}
1707 
1708 	} else {
1709 		dm_helpers_mst_enable_stream_features(stream);
1710 	}
1711 }
1712 
enable_link_dp(struct dc_state * state,struct pipe_ctx * pipe_ctx)1713 static enum dc_status enable_link_dp(struct dc_state *state,
1714 				     struct pipe_ctx *pipe_ctx)
1715 {
1716 	struct dc_stream_state *stream = pipe_ctx->stream;
1717 	enum dc_status status;
1718 	bool skip_video_pattern;
1719 	struct dc_link *link = stream->link;
1720 	struct dc_link_settings link_settings = {0};
1721 	bool fec_enable;
1722 	int i;
1723 	bool apply_seamless_boot_optimization = false;
1724 	uint32_t bl_oled_enable_delay = 50; // in ms
1725 	const uint32_t post_oui_delay = 30; // 30ms
1726 	/* Reduce link bandwidth between failed link training attempts. */
1727 	bool do_fallback = false;
1728 
1729 	// check for seamless boot
1730 	for (i = 0; i < state->stream_count; i++) {
1731 		if (state->streams[i]->apply_seamless_boot_optimization) {
1732 			apply_seamless_boot_optimization = true;
1733 			break;
1734 		}
1735 	}
1736 
1737 	/* get link settings for video mode timing */
1738 	decide_link_settings(stream, &link_settings);
1739 
1740 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP) {
1741 		/*in case it is not on*/
1742 		link->dc->hwss.edp_power_control(link, true);
1743 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
1744 	}
1745 
1746 	pipe_ctx->stream_res.pix_clk_params.requested_sym_clk =
1747 			link_settings.link_rate * LINK_RATE_REF_FREQ_IN_KHZ;
1748 	if (state->clk_mgr && !apply_seamless_boot_optimization)
1749 		state->clk_mgr->funcs->update_clocks(state->clk_mgr,
1750 						     state, false);
1751 
1752 	// during mode switch we do DP_SET_POWER off then on, and OUI is lost
1753 	dpcd_set_source_specific_data(link);
1754 	if (link->dpcd_sink_ext_caps.raw != 0)
1755 		msleep(post_oui_delay);
1756 
1757 	skip_video_pattern = true;
1758 
1759 	if (link_settings.link_rate == LINK_RATE_LOW)
1760 		skip_video_pattern = false;
1761 
1762 	if (perform_link_training_with_retries(&link_settings,
1763 					       skip_video_pattern,
1764 					       LINK_TRAINING_ATTEMPTS,
1765 					       pipe_ctx,
1766 					       pipe_ctx->stream->signal,
1767 					       do_fallback)) {
1768 		link->cur_link_settings = link_settings;
1769 		status = DC_OK;
1770 	} else {
1771 		status = DC_FAIL_DP_LINK_TRAINING;
1772 	}
1773 
1774 	if (link->preferred_training_settings.fec_enable)
1775 		fec_enable = *link->preferred_training_settings.fec_enable;
1776 	else
1777 		fec_enable = true;
1778 
1779 	dp_set_fec_enable(link, fec_enable);
1780 
1781 	// during mode set we do DP_SET_POWER off then on, aux writes are lost
1782 	if (link->dpcd_sink_ext_caps.bits.oled == 1 ||
1783 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1 ||
1784 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1) {
1785 		dc_link_set_default_brightness_aux(link); // TODO: use cached if known
1786 		if (link->dpcd_sink_ext_caps.bits.oled == 1)
1787 			msleep(bl_oled_enable_delay);
1788 		dc_link_backlight_enable_aux(link, true);
1789 	}
1790 
1791 	return status;
1792 }
1793 
enable_link_edp(struct dc_state * state,struct pipe_ctx * pipe_ctx)1794 static enum dc_status enable_link_edp(
1795 		struct dc_state *state,
1796 		struct pipe_ctx *pipe_ctx)
1797 {
1798 	enum dc_status status;
1799 
1800 	status = enable_link_dp(state, pipe_ctx);
1801 
1802 	return status;
1803 }
1804 
enable_link_dp_mst(struct dc_state * state,struct pipe_ctx * pipe_ctx)1805 static enum dc_status enable_link_dp_mst(
1806 		struct dc_state *state,
1807 		struct pipe_ctx *pipe_ctx)
1808 {
1809 	struct dc_link *link = pipe_ctx->stream->link;
1810 
1811 	/* sink signal type after MST branch is MST. Multiple MST sinks
1812 	 * share one link. Link DP PHY is enable or training only once.
1813 	 */
1814 	if (link->link_status.link_active)
1815 		return DC_OK;
1816 
1817 	/* clear payload table */
1818 	dm_helpers_dp_mst_clear_payload_allocation_table(link->ctx, link);
1819 
1820 	/* to make sure the pending down rep can be processed
1821 	 * before enabling the link
1822 	 */
1823 	dm_helpers_dp_mst_poll_pending_down_reply(link->ctx, link);
1824 
1825 	/* set the sink to MST mode before enabling the link */
1826 	dp_enable_mst_on_sink(link, true);
1827 
1828 	return enable_link_dp(state, pipe_ctx);
1829 }
1830 
get_ext_hdmi_settings(struct pipe_ctx * pipe_ctx,enum engine_id eng_id,struct ext_hdmi_settings * settings)1831 static bool get_ext_hdmi_settings(struct pipe_ctx *pipe_ctx,
1832 		enum engine_id eng_id,
1833 		struct ext_hdmi_settings *settings)
1834 {
1835 	bool result = false;
1836 	int i = 0;
1837 	struct integrated_info *integrated_info =
1838 			pipe_ctx->stream->ctx->dc_bios->integrated_info;
1839 
1840 	if (integrated_info == NULL)
1841 		return false;
1842 
1843 	/*
1844 	 * Get retimer settings from sbios for passing SI eye test for DCE11
1845 	 * The setting values are varied based on board revision and port id
1846 	 * Therefore the setting values of each ports is passed by sbios.
1847 	 */
1848 
1849 	// Check if current bios contains ext Hdmi settings
1850 	if (integrated_info->gpu_cap_info & 0x20) {
1851 		switch (eng_id) {
1852 		case ENGINE_ID_DIGA:
1853 			settings->slv_addr = integrated_info->dp0_ext_hdmi_slv_addr;
1854 			settings->reg_num = integrated_info->dp0_ext_hdmi_6g_reg_num;
1855 			settings->reg_num_6g = integrated_info->dp0_ext_hdmi_6g_reg_num;
1856 			memmove(settings->reg_settings,
1857 					integrated_info->dp0_ext_hdmi_reg_settings,
1858 					sizeof(integrated_info->dp0_ext_hdmi_reg_settings));
1859 			memmove(settings->reg_settings_6g,
1860 					integrated_info->dp0_ext_hdmi_6g_reg_settings,
1861 					sizeof(integrated_info->dp0_ext_hdmi_6g_reg_settings));
1862 			result = true;
1863 			break;
1864 		case ENGINE_ID_DIGB:
1865 			settings->slv_addr = integrated_info->dp1_ext_hdmi_slv_addr;
1866 			settings->reg_num = integrated_info->dp1_ext_hdmi_6g_reg_num;
1867 			settings->reg_num_6g = integrated_info->dp1_ext_hdmi_6g_reg_num;
1868 			memmove(settings->reg_settings,
1869 					integrated_info->dp1_ext_hdmi_reg_settings,
1870 					sizeof(integrated_info->dp1_ext_hdmi_reg_settings));
1871 			memmove(settings->reg_settings_6g,
1872 					integrated_info->dp1_ext_hdmi_6g_reg_settings,
1873 					sizeof(integrated_info->dp1_ext_hdmi_6g_reg_settings));
1874 			result = true;
1875 			break;
1876 		case ENGINE_ID_DIGC:
1877 			settings->slv_addr = integrated_info->dp2_ext_hdmi_slv_addr;
1878 			settings->reg_num = integrated_info->dp2_ext_hdmi_6g_reg_num;
1879 			settings->reg_num_6g = integrated_info->dp2_ext_hdmi_6g_reg_num;
1880 			memmove(settings->reg_settings,
1881 					integrated_info->dp2_ext_hdmi_reg_settings,
1882 					sizeof(integrated_info->dp2_ext_hdmi_reg_settings));
1883 			memmove(settings->reg_settings_6g,
1884 					integrated_info->dp2_ext_hdmi_6g_reg_settings,
1885 					sizeof(integrated_info->dp2_ext_hdmi_6g_reg_settings));
1886 			result = true;
1887 			break;
1888 		case ENGINE_ID_DIGD:
1889 			settings->slv_addr = integrated_info->dp3_ext_hdmi_slv_addr;
1890 			settings->reg_num = integrated_info->dp3_ext_hdmi_6g_reg_num;
1891 			settings->reg_num_6g = integrated_info->dp3_ext_hdmi_6g_reg_num;
1892 			memmove(settings->reg_settings,
1893 					integrated_info->dp3_ext_hdmi_reg_settings,
1894 					sizeof(integrated_info->dp3_ext_hdmi_reg_settings));
1895 			memmove(settings->reg_settings_6g,
1896 					integrated_info->dp3_ext_hdmi_6g_reg_settings,
1897 					sizeof(integrated_info->dp3_ext_hdmi_6g_reg_settings));
1898 			result = true;
1899 			break;
1900 		default:
1901 			break;
1902 		}
1903 
1904 		if (result == true) {
1905 			// Validate settings from bios integrated info table
1906 			if (settings->slv_addr == 0)
1907 				return false;
1908 			if (settings->reg_num > 9)
1909 				return false;
1910 			if (settings->reg_num_6g > 3)
1911 				return false;
1912 
1913 			for (i = 0; i < settings->reg_num; i++) {
1914 				if (settings->reg_settings[i].i2c_reg_index > 0x20)
1915 					return false;
1916 			}
1917 
1918 			for (i = 0; i < settings->reg_num_6g; i++) {
1919 				if (settings->reg_settings_6g[i].i2c_reg_index > 0x20)
1920 					return false;
1921 			}
1922 		}
1923 	}
1924 
1925 	return result;
1926 }
1927 
i2c_write(struct pipe_ctx * pipe_ctx,uint8_t address,uint8_t * buffer,uint32_t length)1928 static bool i2c_write(struct pipe_ctx *pipe_ctx,
1929 		uint8_t address, uint8_t *buffer, uint32_t length)
1930 {
1931 	struct i2c_command cmd = {0};
1932 	struct i2c_payload payload = {0};
1933 
1934 	memset(&payload, 0, sizeof(payload));
1935 	memset(&cmd, 0, sizeof(cmd));
1936 
1937 	cmd.number_of_payloads = 1;
1938 	cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
1939 	cmd.speed = pipe_ctx->stream->ctx->dc->caps.i2c_speed_in_khz;
1940 
1941 	payload.address = address;
1942 	payload.data = buffer;
1943 	payload.length = length;
1944 	payload.write = true;
1945 	cmd.payloads = &payload;
1946 
1947 	if (dm_helpers_submit_i2c(pipe_ctx->stream->ctx,
1948 			pipe_ctx->stream->link, &cmd))
1949 		return true;
1950 
1951 	return false;
1952 }
1953 
write_i2c_retimer_setting(struct pipe_ctx * pipe_ctx,bool is_vga_mode,bool is_over_340mhz,struct ext_hdmi_settings * settings)1954 static void write_i2c_retimer_setting(
1955 		struct pipe_ctx *pipe_ctx,
1956 		bool is_vga_mode,
1957 		bool is_over_340mhz,
1958 		struct ext_hdmi_settings *settings)
1959 {
1960 	uint8_t slave_address = (settings->slv_addr >> 1);
1961 	uint8_t buffer[2];
1962 	const uint8_t apply_rx_tx_change = 0x4;
1963 	uint8_t offset = 0xA;
1964 	uint8_t value = 0;
1965 	int i = 0;
1966 	bool i2c_success = false;
1967 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1968 
1969 	memset(&buffer, 0, sizeof(buffer));
1970 
1971 	/* Start Ext-Hdmi programming*/
1972 
1973 	for (i = 0; i < settings->reg_num; i++) {
1974 		/* Apply 3G settings */
1975 		if (settings->reg_settings[i].i2c_reg_index <= 0x20) {
1976 
1977 			buffer[0] = settings->reg_settings[i].i2c_reg_index;
1978 			buffer[1] = settings->reg_settings[i].i2c_reg_val;
1979 			i2c_success = i2c_write(pipe_ctx, slave_address,
1980 						buffer, sizeof(buffer));
1981 			RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1982 				offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
1983 				slave_address, buffer[0], buffer[1], i2c_success?1:0);
1984 
1985 			if (!i2c_success)
1986 				goto i2c_write_fail;
1987 
1988 			/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
1989 			 * needs to be set to 1 on every 0xA-0xC write.
1990 			 */
1991 			if (settings->reg_settings[i].i2c_reg_index == 0xA ||
1992 				settings->reg_settings[i].i2c_reg_index == 0xB ||
1993 				settings->reg_settings[i].i2c_reg_index == 0xC) {
1994 
1995 				/* Query current value from offset 0xA */
1996 				if (settings->reg_settings[i].i2c_reg_index == 0xA)
1997 					value = settings->reg_settings[i].i2c_reg_val;
1998 				else {
1999 					i2c_success =
2000 						dal_ddc_service_query_ddc_data(
2001 						pipe_ctx->stream->link->ddc,
2002 						slave_address, &offset, 1, &value, 1);
2003 					if (!i2c_success)
2004 						goto i2c_write_fail;
2005 				}
2006 
2007 				buffer[0] = offset;
2008 				/* Set APPLY_RX_TX_CHANGE bit to 1 */
2009 				buffer[1] = value | apply_rx_tx_change;
2010 				i2c_success = i2c_write(pipe_ctx, slave_address,
2011 						buffer, sizeof(buffer));
2012 				RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2013 					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2014 					slave_address, buffer[0], buffer[1], i2c_success?1:0);
2015 				if (!i2c_success)
2016 					goto i2c_write_fail;
2017 			}
2018 		}
2019 	}
2020 
2021 	/* Apply 3G settings */
2022 	if (is_over_340mhz) {
2023 		for (i = 0; i < settings->reg_num_6g; i++) {
2024 			/* Apply 3G settings */
2025 			if (settings->reg_settings[i].i2c_reg_index <= 0x20) {
2026 
2027 				buffer[0] = settings->reg_settings_6g[i].i2c_reg_index;
2028 				buffer[1] = settings->reg_settings_6g[i].i2c_reg_val;
2029 				i2c_success = i2c_write(pipe_ctx, slave_address,
2030 							buffer, sizeof(buffer));
2031 				RETIMER_REDRIVER_INFO("above 340Mhz: retimer write to slave_address = 0x%x,\
2032 					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2033 					slave_address, buffer[0], buffer[1], i2c_success?1:0);
2034 
2035 				if (!i2c_success)
2036 					goto i2c_write_fail;
2037 
2038 				/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
2039 				 * needs to be set to 1 on every 0xA-0xC write.
2040 				 */
2041 				if (settings->reg_settings_6g[i].i2c_reg_index == 0xA ||
2042 					settings->reg_settings_6g[i].i2c_reg_index == 0xB ||
2043 					settings->reg_settings_6g[i].i2c_reg_index == 0xC) {
2044 
2045 					/* Query current value from offset 0xA */
2046 					if (settings->reg_settings_6g[i].i2c_reg_index == 0xA)
2047 						value = settings->reg_settings_6g[i].i2c_reg_val;
2048 					else {
2049 						i2c_success =
2050 								dal_ddc_service_query_ddc_data(
2051 								pipe_ctx->stream->link->ddc,
2052 								slave_address, &offset, 1, &value, 1);
2053 						if (!i2c_success)
2054 							goto i2c_write_fail;
2055 					}
2056 
2057 					buffer[0] = offset;
2058 					/* Set APPLY_RX_TX_CHANGE bit to 1 */
2059 					buffer[1] = value | apply_rx_tx_change;
2060 					i2c_success = i2c_write(pipe_ctx, slave_address,
2061 							buffer, sizeof(buffer));
2062 					RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2063 						offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2064 						slave_address, buffer[0], buffer[1], i2c_success?1:0);
2065 					if (!i2c_success)
2066 						goto i2c_write_fail;
2067 				}
2068 			}
2069 		}
2070 	}
2071 
2072 	if (is_vga_mode) {
2073 		/* Program additional settings if using 640x480 resolution */
2074 
2075 		/* Write offset 0xFF to 0x01 */
2076 		buffer[0] = 0xff;
2077 		buffer[1] = 0x01;
2078 		i2c_success = i2c_write(pipe_ctx, slave_address,
2079 				buffer, sizeof(buffer));
2080 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2081 				offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2082 				slave_address, buffer[0], buffer[1], i2c_success?1:0);
2083 		if (!i2c_success)
2084 			goto i2c_write_fail;
2085 
2086 		/* Write offset 0x00 to 0x23 */
2087 		buffer[0] = 0x00;
2088 		buffer[1] = 0x23;
2089 		i2c_success = i2c_write(pipe_ctx, slave_address,
2090 				buffer, sizeof(buffer));
2091 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2092 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2093 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2094 		if (!i2c_success)
2095 			goto i2c_write_fail;
2096 
2097 		/* Write offset 0xff to 0x00 */
2098 		buffer[0] = 0xff;
2099 		buffer[1] = 0x00;
2100 		i2c_success = i2c_write(pipe_ctx, slave_address,
2101 				buffer, sizeof(buffer));
2102 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2103 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2104 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2105 		if (!i2c_success)
2106 			goto i2c_write_fail;
2107 
2108 	}
2109 
2110 	return;
2111 
2112 i2c_write_fail:
2113 	DC_LOG_DEBUG("Set retimer failed");
2114 }
2115 
write_i2c_default_retimer_setting(struct pipe_ctx * pipe_ctx,bool is_vga_mode,bool is_over_340mhz)2116 static void write_i2c_default_retimer_setting(
2117 		struct pipe_ctx *pipe_ctx,
2118 		bool is_vga_mode,
2119 		bool is_over_340mhz)
2120 {
2121 	uint8_t slave_address = (0xBA >> 1);
2122 	uint8_t buffer[2];
2123 	bool i2c_success = false;
2124 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2125 
2126 	memset(&buffer, 0, sizeof(buffer));
2127 
2128 	/* Program Slave Address for tuning single integrity */
2129 	/* Write offset 0x0A to 0x13 */
2130 	buffer[0] = 0x0A;
2131 	buffer[1] = 0x13;
2132 	i2c_success = i2c_write(pipe_ctx, slave_address,
2133 			buffer, sizeof(buffer));
2134 	RETIMER_REDRIVER_INFO("retimer writes default setting to slave_address = 0x%x,\
2135 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2136 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2137 	if (!i2c_success)
2138 		goto i2c_write_fail;
2139 
2140 	/* Write offset 0x0A to 0x17 */
2141 	buffer[0] = 0x0A;
2142 	buffer[1] = 0x17;
2143 	i2c_success = i2c_write(pipe_ctx, slave_address,
2144 			buffer, sizeof(buffer));
2145 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2146 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2147 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2148 	if (!i2c_success)
2149 		goto i2c_write_fail;
2150 
2151 	/* Write offset 0x0B to 0xDA or 0xD8 */
2152 	buffer[0] = 0x0B;
2153 	buffer[1] = is_over_340mhz ? 0xDA : 0xD8;
2154 	i2c_success = i2c_write(pipe_ctx, slave_address,
2155 			buffer, sizeof(buffer));
2156 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2157 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2158 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2159 	if (!i2c_success)
2160 		goto i2c_write_fail;
2161 
2162 	/* Write offset 0x0A to 0x17 */
2163 	buffer[0] = 0x0A;
2164 	buffer[1] = 0x17;
2165 	i2c_success = i2c_write(pipe_ctx, slave_address,
2166 			buffer, sizeof(buffer));
2167 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2168 		offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
2169 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2170 	if (!i2c_success)
2171 		goto i2c_write_fail;
2172 
2173 	/* Write offset 0x0C to 0x1D or 0x91 */
2174 	buffer[0] = 0x0C;
2175 	buffer[1] = is_over_340mhz ? 0x1D : 0x91;
2176 	i2c_success = i2c_write(pipe_ctx, slave_address,
2177 			buffer, sizeof(buffer));
2178 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2179 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2180 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2181 	if (!i2c_success)
2182 		goto i2c_write_fail;
2183 
2184 	/* Write offset 0x0A to 0x17 */
2185 	buffer[0] = 0x0A;
2186 	buffer[1] = 0x17;
2187 	i2c_success = i2c_write(pipe_ctx, slave_address,
2188 			buffer, sizeof(buffer));
2189 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2190 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2191 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2192 	if (!i2c_success)
2193 		goto i2c_write_fail;
2194 
2195 
2196 	if (is_vga_mode) {
2197 		/* Program additional settings if using 640x480 resolution */
2198 
2199 		/* Write offset 0xFF to 0x01 */
2200 		buffer[0] = 0xff;
2201 		buffer[1] = 0x01;
2202 		i2c_success = i2c_write(pipe_ctx, slave_address,
2203 				buffer, sizeof(buffer));
2204 		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2205 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2206 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2207 		if (!i2c_success)
2208 			goto i2c_write_fail;
2209 
2210 		/* Write offset 0x00 to 0x23 */
2211 		buffer[0] = 0x00;
2212 		buffer[1] = 0x23;
2213 		i2c_success = i2c_write(pipe_ctx, slave_address,
2214 				buffer, sizeof(buffer));
2215 		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2216 			offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
2217 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2218 		if (!i2c_success)
2219 			goto i2c_write_fail;
2220 
2221 		/* Write offset 0xff to 0x00 */
2222 		buffer[0] = 0xff;
2223 		buffer[1] = 0x00;
2224 		i2c_success = i2c_write(pipe_ctx, slave_address,
2225 				buffer, sizeof(buffer));
2226 		RETIMER_REDRIVER_INFO("retimer write default setting to slave_addr = 0x%x,\
2227 			offset = 0x%x, reg_val= 0x%x, i2c_success = %d end here\n",
2228 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2229 		if (!i2c_success)
2230 			goto i2c_write_fail;
2231 	}
2232 
2233 	return;
2234 
2235 i2c_write_fail:
2236 	DC_LOG_DEBUG("Set default retimer failed");
2237 }
2238 
write_i2c_redriver_setting(struct pipe_ctx * pipe_ctx,bool is_over_340mhz)2239 static void write_i2c_redriver_setting(
2240 		struct pipe_ctx *pipe_ctx,
2241 		bool is_over_340mhz)
2242 {
2243 	uint8_t slave_address = (0xF0 >> 1);
2244 	uint8_t buffer[16];
2245 	bool i2c_success = false;
2246 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2247 
2248 	memset(&buffer, 0, sizeof(buffer));
2249 
2250 	// Program Slave Address for tuning single integrity
2251 	buffer[3] = 0x4E;
2252 	buffer[4] = 0x4E;
2253 	buffer[5] = 0x4E;
2254 	buffer[6] = is_over_340mhz ? 0x4E : 0x4A;
2255 
2256 	i2c_success = i2c_write(pipe_ctx, slave_address,
2257 					buffer, sizeof(buffer));
2258 	RETIMER_REDRIVER_INFO("redriver write 0 to all 16 reg offset expect following:\n\
2259 		\t slave_addr = 0x%x, offset[3] = 0x%x, offset[4] = 0x%x,\
2260 		offset[5] = 0x%x,offset[6] is_over_340mhz = 0x%x,\
2261 		i2c_success = %d\n",
2262 		slave_address, buffer[3], buffer[4], buffer[5], buffer[6], i2c_success?1:0);
2263 
2264 	if (!i2c_success)
2265 		DC_LOG_DEBUG("Set redriver failed");
2266 }
2267 
disable_link(struct dc_link * link,enum signal_type signal)2268 static void disable_link(struct dc_link *link, enum signal_type signal)
2269 {
2270 	/*
2271 	 * TODO: implement call for dp_set_hw_test_pattern
2272 	 * it is needed for compliance testing
2273 	 */
2274 
2275 	/* Here we need to specify that encoder output settings
2276 	 * need to be calculated as for the set mode,
2277 	 * it will lead to querying dynamic link capabilities
2278 	 * which should be done before enable output
2279 	 */
2280 
2281 	if (dc_is_dp_signal(signal)) {
2282 		/* SST DP, eDP */
2283 		if (dc_is_dp_sst_signal(signal))
2284 			dp_disable_link_phy(link, signal);
2285 		else
2286 			dp_disable_link_phy_mst(link, signal);
2287 
2288 		if (dc_is_dp_sst_signal(signal) ||
2289 				link->mst_stream_alloc_table.stream_count == 0) {
2290 			dp_set_fec_enable(link, false);
2291 			dp_set_fec_ready(link, false);
2292 		}
2293 	} else {
2294 		if (signal != SIGNAL_TYPE_VIRTUAL)
2295 			link->link_enc->funcs->disable_output(link->link_enc, signal);
2296 	}
2297 
2298 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2299 		/* MST disable link only when no stream use the link */
2300 		if (link->mst_stream_alloc_table.stream_count <= 0)
2301 			link->link_status.link_active = false;
2302 	} else {
2303 		link->link_status.link_active = false;
2304 	}
2305 }
2306 
enable_link_hdmi(struct pipe_ctx * pipe_ctx)2307 static void enable_link_hdmi(struct pipe_ctx *pipe_ctx)
2308 {
2309 	struct dc_stream_state *stream = pipe_ctx->stream;
2310 	struct dc_link *link = stream->link;
2311 	enum dc_color_depth display_color_depth;
2312 	enum engine_id eng_id;
2313 	struct ext_hdmi_settings settings = {0};
2314 	bool is_over_340mhz = false;
2315 	bool is_vga_mode = (stream->timing.h_addressable == 640)
2316 			&& (stream->timing.v_addressable == 480);
2317 
2318 	if (stream->phy_pix_clk == 0)
2319 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2320 	if (stream->phy_pix_clk > 340000)
2321 		is_over_340mhz = true;
2322 
2323 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
2324 		unsigned short masked_chip_caps = pipe_ctx->stream->link->chip_caps &
2325 				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
2326 		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
2327 			/* DP159, Retimer settings */
2328 			eng_id = pipe_ctx->stream_res.stream_enc->id;
2329 
2330 			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings)) {
2331 				write_i2c_retimer_setting(pipe_ctx,
2332 						is_vga_mode, is_over_340mhz, &settings);
2333 			} else {
2334 				write_i2c_default_retimer_setting(pipe_ctx,
2335 						is_vga_mode, is_over_340mhz);
2336 			}
2337 		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
2338 			/* PI3EQX1204, Redriver settings */
2339 			write_i2c_redriver_setting(pipe_ctx, is_over_340mhz);
2340 		}
2341 	}
2342 
2343 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2344 		dal_ddc_service_write_scdc_data(
2345 			stream->link->ddc,
2346 			stream->phy_pix_clk,
2347 			stream->timing.flags.LTE_340MCSC_SCRAMBLE);
2348 
2349 	memset(&stream->link->cur_link_settings, 0,
2350 			sizeof(struct dc_link_settings));
2351 
2352 	display_color_depth = stream->timing.display_color_depth;
2353 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
2354 		display_color_depth = COLOR_DEPTH_888;
2355 
2356 	link->link_enc->funcs->enable_tmds_output(
2357 			link->link_enc,
2358 			pipe_ctx->clock_source->id,
2359 			display_color_depth,
2360 			pipe_ctx->stream->signal,
2361 			stream->phy_pix_clk);
2362 
2363 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2364 		dal_ddc_service_read_scdc_data(link->ddc);
2365 }
2366 
enable_link_lvds(struct pipe_ctx * pipe_ctx)2367 static void enable_link_lvds(struct pipe_ctx *pipe_ctx)
2368 {
2369 	struct dc_stream_state *stream = pipe_ctx->stream;
2370 	struct dc_link *link = stream->link;
2371 
2372 	if (stream->phy_pix_clk == 0)
2373 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2374 
2375 	memset(&stream->link->cur_link_settings, 0,
2376 			sizeof(struct dc_link_settings));
2377 
2378 	link->link_enc->funcs->enable_lvds_output(
2379 			link->link_enc,
2380 			pipe_ctx->clock_source->id,
2381 			stream->phy_pix_clk);
2382 
2383 }
2384 
2385 /****************************enable_link***********************************/
enable_link(struct dc_state * state,struct pipe_ctx * pipe_ctx)2386 static enum dc_status enable_link(
2387 		struct dc_state *state,
2388 		struct pipe_ctx *pipe_ctx)
2389 {
2390 	enum dc_status status = DC_ERROR_UNEXPECTED;
2391 	struct dc_stream_state *stream = pipe_ctx->stream;
2392 	struct dc_link *link = stream->link;
2393 
2394 	/* There's some scenarios where driver is unloaded with display
2395 	 * still enabled. When driver is reloaded, it may cause a display
2396 	 * to not light up if there is a mismatch between old and new
2397 	 * link settings. Need to call disable first before enabling at
2398 	 * new link settings.
2399 	 */
2400 	if (link->link_status.link_active) {
2401 		disable_link(link, pipe_ctx->stream->signal);
2402 	}
2403 
2404 	switch (pipe_ctx->stream->signal) {
2405 	case SIGNAL_TYPE_DISPLAY_PORT:
2406 		status = enable_link_dp(state, pipe_ctx);
2407 		break;
2408 	case SIGNAL_TYPE_EDP:
2409 		status = enable_link_edp(state, pipe_ctx);
2410 		break;
2411 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
2412 		status = enable_link_dp_mst(state, pipe_ctx);
2413 		msleep(200);
2414 		break;
2415 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
2416 	case SIGNAL_TYPE_DVI_DUAL_LINK:
2417 	case SIGNAL_TYPE_HDMI_TYPE_A:
2418 		enable_link_hdmi(pipe_ctx);
2419 		status = DC_OK;
2420 		break;
2421 	case SIGNAL_TYPE_LVDS:
2422 		enable_link_lvds(pipe_ctx);
2423 		status = DC_OK;
2424 		break;
2425 	case SIGNAL_TYPE_VIRTUAL:
2426 		status = DC_OK;
2427 		break;
2428 	default:
2429 		break;
2430 	}
2431 
2432 	if (status == DC_OK)
2433 		pipe_ctx->stream->link->link_status.link_active = true;
2434 
2435 	return status;
2436 }
2437 
get_timing_pixel_clock_100hz(const struct dc_crtc_timing * timing)2438 static uint32_t get_timing_pixel_clock_100hz(const struct dc_crtc_timing *timing)
2439 {
2440 
2441 	uint32_t pxl_clk = timing->pix_clk_100hz;
2442 
2443 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2444 		pxl_clk /= 2;
2445 	else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
2446 		pxl_clk = pxl_clk * 2 / 3;
2447 
2448 	if (timing->display_color_depth == COLOR_DEPTH_101010)
2449 		pxl_clk = pxl_clk * 10 / 8;
2450 	else if (timing->display_color_depth == COLOR_DEPTH_121212)
2451 		pxl_clk = pxl_clk * 12 / 8;
2452 
2453 	return pxl_clk;
2454 }
2455 
dp_active_dongle_validate_timing(const struct dc_crtc_timing * timing,const struct dpcd_caps * dpcd_caps)2456 static bool dp_active_dongle_validate_timing(
2457 		const struct dc_crtc_timing *timing,
2458 		const struct dpcd_caps *dpcd_caps)
2459 {
2460 	const struct dc_dongle_caps *dongle_caps = &dpcd_caps->dongle_caps;
2461 
2462 	switch (dpcd_caps->dongle_type) {
2463 	case DISPLAY_DONGLE_DP_VGA_CONVERTER:
2464 	case DISPLAY_DONGLE_DP_DVI_CONVERTER:
2465 	case DISPLAY_DONGLE_DP_DVI_DONGLE:
2466 		if (timing->pixel_encoding == PIXEL_ENCODING_RGB)
2467 			return true;
2468 		else
2469 			return false;
2470 	default:
2471 		break;
2472 	}
2473 
2474 	if (dpcd_caps->dongle_type != DISPLAY_DONGLE_DP_HDMI_CONVERTER ||
2475 		dongle_caps->extendedCapValid == false)
2476 		return true;
2477 
2478 	/* Check Pixel Encoding */
2479 	switch (timing->pixel_encoding) {
2480 	case PIXEL_ENCODING_RGB:
2481 	case PIXEL_ENCODING_YCBCR444:
2482 		break;
2483 	case PIXEL_ENCODING_YCBCR422:
2484 		if (!dongle_caps->is_dp_hdmi_ycbcr422_pass_through)
2485 			return false;
2486 		break;
2487 	case PIXEL_ENCODING_YCBCR420:
2488 		if (!dongle_caps->is_dp_hdmi_ycbcr420_pass_through)
2489 			return false;
2490 		break;
2491 	default:
2492 		/* Invalid Pixel Encoding*/
2493 		return false;
2494 	}
2495 
2496 	switch (timing->display_color_depth) {
2497 	case COLOR_DEPTH_666:
2498 	case COLOR_DEPTH_888:
2499 		/*888 and 666 should always be supported*/
2500 		break;
2501 	case COLOR_DEPTH_101010:
2502 		if (dongle_caps->dp_hdmi_max_bpc < 10)
2503 			return false;
2504 		break;
2505 	case COLOR_DEPTH_121212:
2506 		if (dongle_caps->dp_hdmi_max_bpc < 12)
2507 			return false;
2508 		break;
2509 	case COLOR_DEPTH_141414:
2510 	case COLOR_DEPTH_161616:
2511 	default:
2512 		/* These color depths are currently not supported */
2513 		return false;
2514 	}
2515 
2516 	if (get_timing_pixel_clock_100hz(timing) > (dongle_caps->dp_hdmi_max_pixel_clk_in_khz * 10))
2517 		return false;
2518 
2519 	return true;
2520 }
2521 
dc_link_validate_mode_timing(const struct dc_stream_state * stream,struct dc_link * link,const struct dc_crtc_timing * timing)2522 enum dc_status dc_link_validate_mode_timing(
2523 		const struct dc_stream_state *stream,
2524 		struct dc_link *link,
2525 		const struct dc_crtc_timing *timing)
2526 {
2527 	uint32_t max_pix_clk = stream->link->dongle_max_pix_clk * 10;
2528 	struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
2529 
2530 	/* A hack to avoid failing any modes for EDID override feature on
2531 	 * topology change such as lower quality cable for DP or different dongle
2532 	 */
2533 	if (link->remote_sinks[0] && link->remote_sinks[0]->sink_signal == SIGNAL_TYPE_VIRTUAL)
2534 		return DC_OK;
2535 
2536 	/* Passive Dongle */
2537 	if (max_pix_clk != 0 && get_timing_pixel_clock_100hz(timing) > max_pix_clk)
2538 		return DC_EXCEED_DONGLE_CAP;
2539 
2540 	/* Active Dongle*/
2541 	if (!dp_active_dongle_validate_timing(timing, dpcd_caps))
2542 		return DC_EXCEED_DONGLE_CAP;
2543 
2544 	switch (stream->signal) {
2545 	case SIGNAL_TYPE_EDP:
2546 	case SIGNAL_TYPE_DISPLAY_PORT:
2547 		if (!dp_validate_mode_timing(
2548 				link,
2549 				timing))
2550 			return DC_NO_DP_LINK_BANDWIDTH;
2551 		break;
2552 
2553 	default:
2554 		break;
2555 	}
2556 
2557 	return DC_OK;
2558 }
2559 
get_abm_from_stream_res(const struct dc_link * link)2560 static struct abm *get_abm_from_stream_res(const struct dc_link *link)
2561 {
2562 	int i;
2563 	struct dc *dc = NULL;
2564 	struct abm *abm = NULL;
2565 
2566 	if (!link || !link->ctx)
2567 		return NULL;
2568 
2569 	dc = link->ctx->dc;
2570 
2571 	for (i = 0; i < MAX_PIPES; i++) {
2572 		struct pipe_ctx pipe_ctx = dc->current_state->res_ctx.pipe_ctx[i];
2573 		struct dc_stream_state *stream = pipe_ctx.stream;
2574 
2575 		if (stream && stream->link == link) {
2576 			abm = pipe_ctx.stream_res.abm;
2577 			break;
2578 		}
2579 	}
2580 	return abm;
2581 }
2582 
dc_link_get_backlight_level(const struct dc_link * link)2583 int dc_link_get_backlight_level(const struct dc_link *link)
2584 {
2585 	struct abm *abm = get_abm_from_stream_res(link);
2586 	struct panel_cntl *panel_cntl = link->panel_cntl;
2587 	struct dc  *dc = link->ctx->dc;
2588 	struct dmcu *dmcu = dc->res_pool->dmcu;
2589 	bool fw_set_brightness = true;
2590 
2591 	if (dmcu)
2592 		fw_set_brightness = dmcu->funcs->is_dmcu_initialized(dmcu);
2593 
2594 	if (!fw_set_brightness && panel_cntl->funcs->get_current_backlight)
2595 		return panel_cntl->funcs->get_current_backlight(panel_cntl);
2596 	else if (abm != NULL && abm->funcs->get_current_backlight != NULL)
2597 		return (int) abm->funcs->get_current_backlight(abm);
2598 	else
2599 		return DC_ERROR_UNEXPECTED;
2600 }
2601 
dc_link_get_target_backlight_pwm(const struct dc_link * link)2602 int dc_link_get_target_backlight_pwm(const struct dc_link *link)
2603 {
2604 	struct abm *abm = get_abm_from_stream_res(link);
2605 
2606 	if (abm == NULL || abm->funcs->get_target_backlight == NULL)
2607 		return DC_ERROR_UNEXPECTED;
2608 
2609 	return (int) abm->funcs->get_target_backlight(abm);
2610 }
2611 
get_pipe_from_link(const struct dc_link * link)2612 static struct pipe_ctx *get_pipe_from_link(const struct dc_link *link)
2613 {
2614 	int i;
2615 	struct dc *dc = link->ctx->dc;
2616 	struct pipe_ctx *pipe_ctx = NULL;
2617 
2618 	for (i = 0; i < MAX_PIPES; i++) {
2619 		if (dc->current_state->res_ctx.pipe_ctx[i].stream) {
2620 			if (dc->current_state->res_ctx.pipe_ctx[i].stream->link == link) {
2621 				pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
2622 				break;
2623 			}
2624 		}
2625 	}
2626 
2627 	return pipe_ctx;
2628 }
2629 
dc_link_set_backlight_level(const struct dc_link * link,uint32_t backlight_pwm_u16_16,uint32_t frame_ramp)2630 bool dc_link_set_backlight_level(const struct dc_link *link,
2631 		uint32_t backlight_pwm_u16_16,
2632 		uint32_t frame_ramp)
2633 {
2634 	struct dc  *dc = link->ctx->dc;
2635 
2636 	DC_LOGGER_INIT(link->ctx->logger);
2637 	DC_LOG_BACKLIGHT("New Backlight level: %d (0x%X)\n",
2638 			backlight_pwm_u16_16, backlight_pwm_u16_16);
2639 
2640 	if (dc_is_embedded_signal(link->connector_signal)) {
2641 		struct pipe_ctx *pipe_ctx = get_pipe_from_link(link);
2642 
2643 		if (pipe_ctx) {
2644 			/* Disable brightness ramping when the display is blanked
2645 			 * as it can hang the DMCU
2646 			 */
2647 			if (pipe_ctx->plane_state == NULL)
2648 				frame_ramp = 0;
2649 		} else {
2650 			return false;
2651 		}
2652 
2653 		dc->hwss.set_backlight_level(
2654 				pipe_ctx,
2655 				backlight_pwm_u16_16,
2656 				frame_ramp);
2657 	}
2658 	return true;
2659 }
2660 
dc_link_set_psr_allow_active(struct dc_link * link,bool allow_active,bool wait,bool force_static)2661 bool dc_link_set_psr_allow_active(struct dc_link *link, bool allow_active,
2662 		bool wait, bool force_static)
2663 {
2664 	struct dc  *dc = link->ctx->dc;
2665 	struct dmcu *dmcu = dc->res_pool->dmcu;
2666 	struct dmub_psr *psr = dc->res_pool->psr;
2667 	unsigned int panel_inst;
2668 
2669 	if (psr == NULL && force_static)
2670 		return false;
2671 
2672 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
2673 		return false;
2674 
2675 	link->psr_settings.psr_allow_active = allow_active;
2676 #if defined(CONFIG_DRM_AMD_DC_DCN)
2677 	if (!allow_active)
2678 		dc_z10_restore(dc);
2679 #endif
2680 
2681 	if (psr != NULL && link->psr_settings.psr_feature_enabled) {
2682 		if (force_static && psr->funcs->psr_force_static)
2683 			psr->funcs->psr_force_static(psr, panel_inst);
2684 		psr->funcs->psr_enable(psr, allow_active, wait, panel_inst);
2685 	} else if ((dmcu != NULL && dmcu->funcs->is_dmcu_initialized(dmcu)) && link->psr_settings.psr_feature_enabled)
2686 		dmcu->funcs->set_psr_enable(dmcu, allow_active, wait);
2687 	else
2688 		return false;
2689 
2690 	return true;
2691 }
2692 
dc_link_get_psr_state(const struct dc_link * link,enum dc_psr_state * state)2693 bool dc_link_get_psr_state(const struct dc_link *link, enum dc_psr_state *state)
2694 {
2695 	struct dc  *dc = link->ctx->dc;
2696 	struct dmcu *dmcu = dc->res_pool->dmcu;
2697 	struct dmub_psr *psr = dc->res_pool->psr;
2698 	unsigned int panel_inst;
2699 
2700 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
2701 		return false;
2702 
2703 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
2704 		psr->funcs->psr_get_state(psr, state, panel_inst);
2705 	else if (dmcu != NULL && link->psr_settings.psr_feature_enabled)
2706 		dmcu->funcs->get_psr_state(dmcu, state);
2707 
2708 	return true;
2709 }
2710 
2711 static inline enum physical_phy_id
transmitter_to_phy_id(enum transmitter transmitter_value)2712 transmitter_to_phy_id(enum transmitter transmitter_value)
2713 {
2714 	switch (transmitter_value) {
2715 	case TRANSMITTER_UNIPHY_A:
2716 		return PHYLD_0;
2717 	case TRANSMITTER_UNIPHY_B:
2718 		return PHYLD_1;
2719 	case TRANSMITTER_UNIPHY_C:
2720 		return PHYLD_2;
2721 	case TRANSMITTER_UNIPHY_D:
2722 		return PHYLD_3;
2723 	case TRANSMITTER_UNIPHY_E:
2724 		return PHYLD_4;
2725 	case TRANSMITTER_UNIPHY_F:
2726 		return PHYLD_5;
2727 	case TRANSMITTER_NUTMEG_CRT:
2728 		return PHYLD_6;
2729 	case TRANSMITTER_TRAVIS_CRT:
2730 		return PHYLD_7;
2731 	case TRANSMITTER_TRAVIS_LCD:
2732 		return PHYLD_8;
2733 	case TRANSMITTER_UNIPHY_G:
2734 		return PHYLD_9;
2735 	case TRANSMITTER_COUNT:
2736 		return PHYLD_COUNT;
2737 	case TRANSMITTER_UNKNOWN:
2738 		return PHYLD_UNKNOWN;
2739 	default:
2740 		WARN_ONCE(1, "Unknown transmitter value %d\n",
2741 			  transmitter_value);
2742 		return PHYLD_UNKNOWN;
2743 	}
2744 }
2745 
dc_link_setup_psr(struct dc_link * link,const struct dc_stream_state * stream,struct psr_config * psr_config,struct psr_context * psr_context)2746 bool dc_link_setup_psr(struct dc_link *link,
2747 		const struct dc_stream_state *stream, struct psr_config *psr_config,
2748 		struct psr_context *psr_context)
2749 {
2750 	struct dc *dc;
2751 	struct dmcu *dmcu;
2752 	struct dmub_psr *psr;
2753 	int i;
2754 	unsigned int panel_inst;
2755 	/* updateSinkPsrDpcdConfig*/
2756 	union dpcd_psr_configuration psr_configuration;
2757 
2758 	psr_context->controllerId = CONTROLLER_ID_UNDEFINED;
2759 
2760 	if (!link)
2761 		return false;
2762 
2763 	dc = link->ctx->dc;
2764 	dmcu = dc->res_pool->dmcu;
2765 	psr = dc->res_pool->psr;
2766 
2767 	if (!dmcu && !psr)
2768 		return false;
2769 
2770 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
2771 		return false;
2772 
2773 
2774 	memset(&psr_configuration, 0, sizeof(psr_configuration));
2775 
2776 	psr_configuration.bits.ENABLE                    = 1;
2777 	psr_configuration.bits.CRC_VERIFICATION          = 1;
2778 	psr_configuration.bits.FRAME_CAPTURE_INDICATION  =
2779 			psr_config->psr_frame_capture_indication_req;
2780 
2781 	/* Check for PSR v2*/
2782 	if (psr_config->psr_version == 0x2) {
2783 		/* For PSR v2 selective update.
2784 		 * Indicates whether sink should start capturing
2785 		 * immediately following active scan line,
2786 		 * or starting with the 2nd active scan line.
2787 		 */
2788 		psr_configuration.bits.LINE_CAPTURE_INDICATION = 0;
2789 		/*For PSR v2, determines whether Sink should generate
2790 		 * IRQ_HPD when CRC mismatch is detected.
2791 		 */
2792 		psr_configuration.bits.IRQ_HPD_WITH_CRC_ERROR    = 1;
2793 	}
2794 
2795 	dm_helpers_dp_write_dpcd(
2796 		link->ctx,
2797 		link,
2798 		368,
2799 		&psr_configuration.raw,
2800 		sizeof(psr_configuration.raw));
2801 
2802 	psr_context->channel = link->ddc->ddc_pin->hw_info.ddc_channel;
2803 	psr_context->transmitterId = link->link_enc->transmitter;
2804 	psr_context->engineId = link->link_enc->preferred_engine;
2805 
2806 	for (i = 0; i < MAX_PIPES; i++) {
2807 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
2808 				== stream) {
2809 			/* dmcu -1 for all controller id values,
2810 			 * therefore +1 here
2811 			 */
2812 			psr_context->controllerId =
2813 				dc->current_state->res_ctx.
2814 				pipe_ctx[i].stream_res.tg->inst + 1;
2815 			break;
2816 		}
2817 	}
2818 
2819 	/* Hardcoded for now.  Can be Pcie or Uniphy (or Unknown)*/
2820 	psr_context->phyType = PHY_TYPE_UNIPHY;
2821 	/*PhyId is associated with the transmitter id*/
2822 	psr_context->smuPhyId =
2823 		transmitter_to_phy_id(link->link_enc->transmitter);
2824 
2825 	psr_context->crtcTimingVerticalTotal = stream->timing.v_total;
2826 	psr_context->vsync_rate_hz = div64_u64(div64_u64((stream->
2827 					timing.pix_clk_100hz * 100),
2828 					stream->timing.v_total),
2829 					stream->timing.h_total);
2830 
2831 	psr_context->psrSupportedDisplayConfig = true;
2832 	psr_context->psrExitLinkTrainingRequired =
2833 		psr_config->psr_exit_link_training_required;
2834 	psr_context->sdpTransmitLineNumDeadline =
2835 		psr_config->psr_sdp_transmit_line_num_deadline;
2836 	psr_context->psrFrameCaptureIndicationReq =
2837 		psr_config->psr_frame_capture_indication_req;
2838 
2839 	psr_context->skipPsrWaitForPllLock = 0; /* only = 1 in KV */
2840 
2841 	psr_context->numberOfControllers =
2842 			link->dc->res_pool->timing_generator_count;
2843 
2844 	psr_context->rfb_update_auto_en = true;
2845 
2846 	/* 2 frames before enter PSR. */
2847 	psr_context->timehyst_frames = 2;
2848 	/* half a frame
2849 	 * (units in 100 lines, i.e. a value of 1 represents 100 lines)
2850 	 */
2851 	psr_context->hyst_lines = stream->timing.v_total / 2 / 100;
2852 	psr_context->aux_repeats = 10;
2853 
2854 	psr_context->psr_level.u32all = 0;
2855 
2856 	/*skip power down the single pipe since it blocks the cstate*/
2857 #if defined(CONFIG_DRM_AMD_DC_DCN)
2858 	if (link->ctx->asic_id.chip_family >= FAMILY_RV) {
2859 		psr_context->psr_level.bits.SKIP_CRTC_DISABLE = true;
2860 		if (link->ctx->asic_id.chip_family == FAMILY_YELLOW_CARP && !dc->debug.disable_z10)
2861 			psr_context->psr_level.bits.SKIP_CRTC_DISABLE = false;
2862 	}
2863 #else
2864 	if (link->ctx->asic_id.chip_family >= FAMILY_RV)
2865 		psr_context->psr_level.bits.SKIP_CRTC_DISABLE = true;
2866 #endif
2867 
2868 	/* SMU will perform additional powerdown sequence.
2869 	 * For unsupported ASICs, set psr_level flag to skip PSR
2870 	 *  static screen notification to SMU.
2871 	 *  (Always set for DAL2, did not check ASIC)
2872 	 */
2873 	psr_context->allow_smu_optimizations = psr_config->allow_smu_optimizations;
2874 	psr_context->allow_multi_disp_optimizations = psr_config->allow_multi_disp_optimizations;
2875 
2876 	/* Complete PSR entry before aborting to prevent intermittent
2877 	 * freezes on certain eDPs
2878 	 */
2879 	psr_context->psr_level.bits.DISABLE_PSR_ENTRY_ABORT = 1;
2880 
2881 	/* Controls additional delay after remote frame capture before
2882 	 * continuing power down, default = 0
2883 	 */
2884 	psr_context->frame_delay = 0;
2885 
2886 	if (psr)
2887 		link->psr_settings.psr_feature_enabled = psr->funcs->psr_copy_settings(psr,
2888 			link, psr_context, panel_inst);
2889 	else
2890 		link->psr_settings.psr_feature_enabled = dmcu->funcs->setup_psr(dmcu, link, psr_context);
2891 
2892 	/* psr_enabled == 0 indicates setup_psr did not succeed, but this
2893 	 * should not happen since firmware should be running at this point
2894 	 */
2895 	if (link->psr_settings.psr_feature_enabled == 0)
2896 		ASSERT(0);
2897 
2898 	return true;
2899 
2900 }
2901 
dc_link_get_psr_residency(const struct dc_link * link,uint32_t * residency)2902 void dc_link_get_psr_residency(const struct dc_link *link, uint32_t *residency)
2903 {
2904 	struct dc  *dc = link->ctx->dc;
2905 	struct dmub_psr *psr = dc->res_pool->psr;
2906 	unsigned int panel_inst;
2907 
2908 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
2909 		return;
2910 
2911 	/* PSR residency measurements only supported on DMCUB */
2912 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
2913 		psr->funcs->psr_get_residency(psr, residency, panel_inst);
2914 	else
2915 		*residency = 0;
2916 }
2917 
dc_link_get_status(const struct dc_link * link)2918 const struct dc_link_status *dc_link_get_status(const struct dc_link *link)
2919 {
2920 	return &link->link_status;
2921 }
2922 
core_link_resume(struct dc_link * link)2923 void core_link_resume(struct dc_link *link)
2924 {
2925 	if (link->connector_signal != SIGNAL_TYPE_VIRTUAL)
2926 		program_hpd_filter(link);
2927 }
2928 
get_pbn_per_slot(struct dc_stream_state * stream)2929 static struct fixed31_32 get_pbn_per_slot(struct dc_stream_state *stream)
2930 {
2931 	struct fixed31_32 mbytes_per_sec;
2932 	uint32_t link_rate_in_mbytes_per_sec = dc_link_bandwidth_kbps(stream->link,
2933 			&stream->link->cur_link_settings);
2934 	link_rate_in_mbytes_per_sec /= 8000; /* Kbits to MBytes */
2935 
2936 	mbytes_per_sec = dc_fixpt_from_int(link_rate_in_mbytes_per_sec);
2937 
2938 	return dc_fixpt_div_int(mbytes_per_sec, 54);
2939 }
2940 
get_pbn_from_bw_in_kbps(uint64_t kbps)2941 static struct fixed31_32 get_pbn_from_bw_in_kbps(uint64_t kbps)
2942 {
2943 	struct fixed31_32 peak_kbps;
2944 	uint32_t numerator = 0;
2945 	uint32_t denominator = 1;
2946 
2947 	/*
2948 	 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2949 	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2950 	 * common multiplier to render an integer PBN for all link rate/lane
2951 	 * counts combinations
2952 	 * calculate
2953 	 * peak_kbps *= (1006/1000)
2954 	 * peak_kbps *= (64/54)
2955 	 * peak_kbps *= 8    convert to bytes
2956 	 */
2957 
2958 	numerator = 64 * PEAK_FACTOR_X1000;
2959 	denominator = 54 * 8 * 1000 * 1000;
2960 	kbps *= numerator;
2961 	peak_kbps = dc_fixpt_from_fraction(kbps, denominator);
2962 
2963 	return peak_kbps;
2964 }
2965 
get_pbn_from_timing(struct pipe_ctx * pipe_ctx)2966 static struct fixed31_32 get_pbn_from_timing(struct pipe_ctx *pipe_ctx)
2967 {
2968 	uint64_t kbps;
2969 
2970 	kbps = dc_bandwidth_in_kbps_from_timing(&pipe_ctx->stream->timing);
2971 	return get_pbn_from_bw_in_kbps(kbps);
2972 }
2973 
update_mst_stream_alloc_table(struct dc_link * link,struct stream_encoder * stream_enc,const struct dp_mst_stream_allocation_table * proposed_table)2974 static void update_mst_stream_alloc_table(
2975 	struct dc_link *link,
2976 	struct stream_encoder *stream_enc,
2977 	const struct dp_mst_stream_allocation_table *proposed_table)
2978 {
2979 	struct link_mst_stream_allocation work_table[MAX_CONTROLLER_NUM] = {
2980 			{ 0 } };
2981 	struct link_mst_stream_allocation *dc_alloc;
2982 
2983 	int i;
2984 	int j;
2985 
2986 	/* if DRM proposed_table has more than one new payload */
2987 	ASSERT(proposed_table->stream_count -
2988 			link->mst_stream_alloc_table.stream_count < 2);
2989 
2990 	/* copy proposed_table to link, add stream encoder */
2991 	for (i = 0; i < proposed_table->stream_count; i++) {
2992 
2993 		for (j = 0; j < link->mst_stream_alloc_table.stream_count; j++) {
2994 			dc_alloc =
2995 			&link->mst_stream_alloc_table.stream_allocations[j];
2996 
2997 			if (dc_alloc->vcp_id ==
2998 				proposed_table->stream_allocations[i].vcp_id) {
2999 
3000 				work_table[i] = *dc_alloc;
3001 				work_table[i].slot_count = proposed_table->stream_allocations[i].slot_count;
3002 				break; /* exit j loop */
3003 			}
3004 		}
3005 
3006 		/* new vcp_id */
3007 		if (j == link->mst_stream_alloc_table.stream_count) {
3008 			work_table[i].vcp_id =
3009 				proposed_table->stream_allocations[i].vcp_id;
3010 			work_table[i].slot_count =
3011 				proposed_table->stream_allocations[i].slot_count;
3012 			work_table[i].stream_enc = stream_enc;
3013 		}
3014 	}
3015 
3016 	/* update link->mst_stream_alloc_table with work_table */
3017 	link->mst_stream_alloc_table.stream_count =
3018 			proposed_table->stream_count;
3019 	for (i = 0; i < MAX_CONTROLLER_NUM; i++)
3020 		link->mst_stream_alloc_table.stream_allocations[i] =
3021 				work_table[i];
3022 }
3023 
3024 /* convert link_mst_stream_alloc_table to dm dp_mst_stream_alloc_table
3025  * because stream_encoder is not exposed to dm
3026  */
dc_link_allocate_mst_payload(struct pipe_ctx * pipe_ctx)3027 enum dc_status dc_link_allocate_mst_payload(struct pipe_ctx *pipe_ctx)
3028 {
3029 	struct dc_stream_state *stream = pipe_ctx->stream;
3030 	struct dc_link *link = stream->link;
3031 	struct link_encoder *link_encoder = link->link_enc;
3032 	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
3033 	struct dp_mst_stream_allocation_table proposed_table = {0};
3034 	struct fixed31_32 avg_time_slots_per_mtp;
3035 	struct fixed31_32 pbn;
3036 	struct fixed31_32 pbn_per_slot;
3037 	uint8_t i;
3038 	enum act_return_status ret;
3039 	DC_LOGGER_INIT(link->ctx->logger);
3040 
3041 	/* enable_link_dp_mst already check link->enabled_stream_count
3042 	 * and stream is in link->stream[]. This is called during set mode,
3043 	 * stream_enc is available.
3044 	 */
3045 
3046 	/* get calculate VC payload for stream: stream_alloc */
3047 	if (dm_helpers_dp_mst_write_payload_allocation_table(
3048 		stream->ctx,
3049 		stream,
3050 		&proposed_table,
3051 		true)) {
3052 		update_mst_stream_alloc_table(
3053 					link, pipe_ctx->stream_res.stream_enc, &proposed_table);
3054 	}
3055 	else
3056 		DC_LOG_WARNING("Failed to update"
3057 				"MST allocation table for"
3058 				"pipe idx:%d\n",
3059 				pipe_ctx->pipe_idx);
3060 
3061 	DC_LOG_MST("%s  "
3062 			"stream_count: %d: \n ",
3063 			__func__,
3064 			link->mst_stream_alloc_table.stream_count);
3065 
3066 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
3067 		DC_LOG_MST("stream_enc[%d]: %p      "
3068 		"stream[%d].vcp_id: %d      "
3069 		"stream[%d].slot_count: %d\n",
3070 		i,
3071 		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
3072 		i,
3073 		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
3074 		i,
3075 		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
3076 	}
3077 
3078 	ASSERT(proposed_table.stream_count > 0);
3079 
3080 	/* program DP source TX for payload */
3081 	link_encoder->funcs->update_mst_stream_allocation_table(
3082 		link_encoder,
3083 		&link->mst_stream_alloc_table);
3084 
3085 	/* send down message */
3086 	ret = dm_helpers_dp_mst_poll_for_allocation_change_trigger(
3087 			stream->ctx,
3088 			stream);
3089 
3090 	if (ret != ACT_LINK_LOST) {
3091 		dm_helpers_dp_mst_send_payload_allocation(
3092 				stream->ctx,
3093 				stream,
3094 				true);
3095 	}
3096 
3097 	/* slot X.Y for only current stream */
3098 	pbn_per_slot = get_pbn_per_slot(stream);
3099 	if (pbn_per_slot.value == 0) {
3100 		DC_LOG_ERROR("Failure: pbn_per_slot==0 not allowed. Cannot continue, returning DC_UNSUPPORTED_VALUE.\n");
3101 		return DC_UNSUPPORTED_VALUE;
3102 	}
3103 	pbn = get_pbn_from_timing(pipe_ctx);
3104 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
3105 
3106 	stream_encoder->funcs->set_throttled_vcp_size(
3107 		stream_encoder,
3108 		avg_time_slots_per_mtp);
3109 
3110 	return DC_OK;
3111 
3112 }
3113 
deallocate_mst_payload(struct pipe_ctx * pipe_ctx)3114 static enum dc_status deallocate_mst_payload(struct pipe_ctx *pipe_ctx)
3115 {
3116 	struct dc_stream_state *stream = pipe_ctx->stream;
3117 	struct dc_link *link = stream->link;
3118 	struct link_encoder *link_encoder = link->link_enc;
3119 	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
3120 	struct dp_mst_stream_allocation_table proposed_table = {0};
3121 	struct fixed31_32 avg_time_slots_per_mtp = dc_fixpt_from_int(0);
3122 	uint8_t i;
3123 	bool mst_mode = (link->type == dc_connection_mst_branch);
3124 	DC_LOGGER_INIT(link->ctx->logger);
3125 
3126 	/* deallocate_mst_payload is called before disable link. When mode or
3127 	 * disable/enable monitor, new stream is created which is not in link
3128 	 * stream[] yet. For this, payload is not allocated yet, so de-alloc
3129 	 * should not done. For new mode set, map_resources will get engine
3130 	 * for new stream, so stream_enc->id should be validated until here.
3131 	 */
3132 
3133 	/* slot X.Y */
3134 	stream_encoder->funcs->set_throttled_vcp_size(
3135 		stream_encoder,
3136 		avg_time_slots_per_mtp);
3137 
3138 	/* TODO: which component is responsible for remove payload table? */
3139 	if (mst_mode) {
3140 		if (dm_helpers_dp_mst_write_payload_allocation_table(
3141 				stream->ctx,
3142 				stream,
3143 				&proposed_table,
3144 				false)) {
3145 
3146 			update_mst_stream_alloc_table(
3147 				link, pipe_ctx->stream_res.stream_enc, &proposed_table);
3148 		}
3149 		else {
3150 				DC_LOG_WARNING("Failed to update"
3151 						"MST allocation table for"
3152 						"pipe idx:%d\n",
3153 						pipe_ctx->pipe_idx);
3154 		}
3155 	}
3156 
3157 	DC_LOG_MST("%s"
3158 			"stream_count: %d: ",
3159 			__func__,
3160 			link->mst_stream_alloc_table.stream_count);
3161 
3162 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
3163 		DC_LOG_MST("stream_enc[%d]: %p      "
3164 		"stream[%d].vcp_id: %d      "
3165 		"stream[%d].slot_count: %d\n",
3166 		i,
3167 		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
3168 		i,
3169 		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
3170 		i,
3171 		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
3172 	}
3173 
3174 	link_encoder->funcs->update_mst_stream_allocation_table(
3175 		link_encoder,
3176 		&link->mst_stream_alloc_table);
3177 
3178 	if (mst_mode) {
3179 		dm_helpers_dp_mst_poll_for_allocation_change_trigger(
3180 			stream->ctx,
3181 			stream);
3182 
3183 		dm_helpers_dp_mst_send_payload_allocation(
3184 			stream->ctx,
3185 			stream,
3186 			false);
3187 	}
3188 
3189 	return DC_OK;
3190 }
3191 
3192 
3193 #if defined(CONFIG_DRM_AMD_DC_HDCP)
update_psp_stream_config(struct pipe_ctx * pipe_ctx,bool dpms_off)3194 static void update_psp_stream_config(struct pipe_ctx *pipe_ctx, bool dpms_off)
3195 {
3196 	struct cp_psp *cp_psp = &pipe_ctx->stream->ctx->cp_psp;
3197 	if (cp_psp && cp_psp->funcs.update_stream_config) {
3198 		struct cp_psp_stream_config config = {0};
3199 		enum dp_panel_mode panel_mode =
3200 				dp_get_panel_mode(pipe_ctx->stream->link);
3201 
3202 		config.otg_inst = (uint8_t) pipe_ctx->stream_res.tg->inst;
3203 		/*stream_enc_inst*/
3204 		config.dig_fe = (uint8_t) pipe_ctx->stream_res.stream_enc->stream_enc_inst;
3205 		config.dig_be = pipe_ctx->stream->link->link_enc_hw_inst;
3206 #if defined(CONFIG_DRM_AMD_DC_DCN)
3207 		config.stream_enc_idx = pipe_ctx->stream_res.stream_enc->id - ENGINE_ID_DIGA;
3208 		config.link_enc_idx = pipe_ctx->stream->link->link_enc->transmitter - TRANSMITTER_UNIPHY_A;
3209 		config.phy_idx = pipe_ctx->stream->link->link_enc->transmitter - TRANSMITTER_UNIPHY_A;
3210 #endif
3211 		config.dpms_off = dpms_off;
3212 		config.dm_stream_ctx = pipe_ctx->stream->dm_stream_context;
3213 		config.assr_enabled = (panel_mode == DP_PANEL_MODE_EDP);
3214 		config.mst_enabled = (pipe_ctx->stream->signal ==
3215 				SIGNAL_TYPE_DISPLAY_PORT_MST);
3216 		cp_psp->funcs.update_stream_config(cp_psp->handle, &config);
3217 	}
3218 }
3219 #endif
3220 
core_link_enable_stream(struct dc_state * state,struct pipe_ctx * pipe_ctx)3221 void core_link_enable_stream(
3222 		struct dc_state *state,
3223 		struct pipe_ctx *pipe_ctx)
3224 {
3225 	struct dc *dc = pipe_ctx->stream->ctx->dc;
3226 	struct dc_stream_state *stream = pipe_ctx->stream;
3227 	enum dc_status status;
3228 #if defined(CONFIG_DRM_AMD_DC_DCN)
3229 	enum otg_out_mux_dest otg_out_dest = OUT_MUX_DIO;
3230 #endif
3231 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
3232 
3233 	if (!IS_DIAG_DC(dc->ctx->dce_environment) &&
3234 			dc_is_virtual_signal(pipe_ctx->stream->signal))
3235 		return;
3236 
3237 	if (!dc_is_virtual_signal(pipe_ctx->stream->signal)) {
3238 		stream->link->link_enc->funcs->setup(
3239 			stream->link->link_enc,
3240 			pipe_ctx->stream->signal);
3241 		pipe_ctx->stream_res.stream_enc->funcs->setup_stereo_sync(
3242 			pipe_ctx->stream_res.stream_enc,
3243 			pipe_ctx->stream_res.tg->inst,
3244 			stream->timing.timing_3d_format != TIMING_3D_FORMAT_NONE);
3245 	}
3246 
3247 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
3248 		pipe_ctx->stream_res.stream_enc->funcs->dp_set_stream_attribute(
3249 			pipe_ctx->stream_res.stream_enc,
3250 			&stream->timing,
3251 			stream->output_color_space,
3252 			stream->use_vsc_sdp_for_colorimetry,
3253 			stream->link->dpcd_caps.dprx_feature.bits.SST_SPLIT_SDP_CAP);
3254 
3255 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal))
3256 		pipe_ctx->stream_res.stream_enc->funcs->hdmi_set_stream_attribute(
3257 			pipe_ctx->stream_res.stream_enc,
3258 			&stream->timing,
3259 			stream->phy_pix_clk,
3260 			pipe_ctx->stream_res.audio != NULL);
3261 
3262 	pipe_ctx->stream->link->link_state_valid = true;
3263 
3264 #if defined(CONFIG_DRM_AMD_DC_DCN)
3265 	if (pipe_ctx->stream_res.tg->funcs->set_out_mux)
3266 		pipe_ctx->stream_res.tg->funcs->set_out_mux(pipe_ctx->stream_res.tg, otg_out_dest);
3267 #endif
3268 
3269 	if (dc_is_dvi_signal(pipe_ctx->stream->signal))
3270 		pipe_ctx->stream_res.stream_enc->funcs->dvi_set_stream_attribute(
3271 			pipe_ctx->stream_res.stream_enc,
3272 			&stream->timing,
3273 			(pipe_ctx->stream->signal == SIGNAL_TYPE_DVI_DUAL_LINK) ?
3274 			true : false);
3275 
3276 	if (dc_is_lvds_signal(pipe_ctx->stream->signal))
3277 		pipe_ctx->stream_res.stream_enc->funcs->lvds_set_stream_attribute(
3278 			pipe_ctx->stream_res.stream_enc,
3279 			&stream->timing);
3280 
3281 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
3282 		bool apply_edp_fast_boot_optimization =
3283 			pipe_ctx->stream->apply_edp_fast_boot_optimization;
3284 
3285 		pipe_ctx->stream->apply_edp_fast_boot_optimization = false;
3286 
3287 		resource_build_info_frame(pipe_ctx);
3288 		dc->hwss.update_info_frame(pipe_ctx);
3289 
3290 		/* Do not touch link on seamless boot optimization. */
3291 		if (pipe_ctx->stream->apply_seamless_boot_optimization) {
3292 			pipe_ctx->stream->dpms_off = false;
3293 
3294 			/* Still enable stream features & audio on seamless boot for DP external displays */
3295 			if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT) {
3296 				enable_stream_features(pipe_ctx);
3297 				if (pipe_ctx->stream_res.audio != NULL) {
3298 					pipe_ctx->stream_res.stream_enc->funcs->dp_audio_enable(pipe_ctx->stream_res.stream_enc);
3299 					dc->hwss.enable_audio_stream(pipe_ctx);
3300 				}
3301 			}
3302 
3303 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3304 			update_psp_stream_config(pipe_ctx, false);
3305 #endif
3306 			return;
3307 		}
3308 
3309 		/* eDP lit up by bios already, no need to enable again. */
3310 		if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
3311 					apply_edp_fast_boot_optimization &&
3312 					!pipe_ctx->stream->timing.flags.DSC) {
3313 			pipe_ctx->stream->dpms_off = false;
3314 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3315 			update_psp_stream_config(pipe_ctx, false);
3316 #endif
3317 			return;
3318 		}
3319 
3320 		if (pipe_ctx->stream->dpms_off)
3321 			return;
3322 
3323 		/* Have to setup DSC before DIG FE and BE are connected (which happens before the
3324 		 * link training). This is to make sure the bandwidth sent to DIG BE won't be
3325 		 * bigger than what the link and/or DIG BE can handle. VBID[6]/CompressedStream_flag
3326 		 * will be automatically set at a later time when the video is enabled
3327 		 * (DP_VID_STREAM_EN = 1).
3328 		 */
3329 		if (pipe_ctx->stream->timing.flags.DSC) {
3330 			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3331 					dc_is_virtual_signal(pipe_ctx->stream->signal))
3332 				dp_set_dsc_enable(pipe_ctx, true);
3333 		}
3334 
3335 		status = enable_link(state, pipe_ctx);
3336 
3337 		if (status != DC_OK) {
3338 			DC_LOG_WARNING("enabling link %u failed: %d\n",
3339 			pipe_ctx->stream->link->link_index,
3340 			status);
3341 
3342 			/* Abort stream enable *unless* the failure was due to
3343 			 * DP link training - some DP monitors will recover and
3344 			 * show the stream anyway. But MST displays can't proceed
3345 			 * without link training.
3346 			 */
3347 			if (status != DC_FAIL_DP_LINK_TRAINING ||
3348 					pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3349 				BREAK_TO_DEBUGGER();
3350 				return;
3351 			}
3352 		}
3353 
3354 		/* turn off otg test pattern if enable */
3355 		if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
3356 			pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
3357 					CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
3358 					COLOR_DEPTH_UNDEFINED);
3359 
3360 		/* This second call is needed to reconfigure the DIG
3361 		 * as a workaround for the incorrect value being applied
3362 		 * from transmitter control.
3363 		 */
3364 		if (!dc_is_virtual_signal(pipe_ctx->stream->signal))
3365 			stream->link->link_enc->funcs->setup(
3366 				stream->link->link_enc,
3367 				pipe_ctx->stream->signal);
3368 
3369 		dc->hwss.enable_stream(pipe_ctx);
3370 
3371 		/* Set DPS PPS SDP (AKA "info frames") */
3372 		if (pipe_ctx->stream->timing.flags.DSC) {
3373 			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3374 					dc_is_virtual_signal(pipe_ctx->stream->signal)) {
3375 				dp_set_dsc_on_rx(pipe_ctx, true);
3376 				dp_set_dsc_pps_sdp(pipe_ctx, true);
3377 			}
3378 		}
3379 
3380 		if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3381 			dc_link_allocate_mst_payload(pipe_ctx);
3382 
3383 		dc->hwss.unblank_stream(pipe_ctx,
3384 			&pipe_ctx->stream->link->cur_link_settings);
3385 
3386 		if (stream->sink_patches.delay_ignore_msa > 0)
3387 			msleep(stream->sink_patches.delay_ignore_msa);
3388 
3389 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
3390 			enable_stream_features(pipe_ctx);
3391 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3392 		update_psp_stream_config(pipe_ctx, false);
3393 #endif
3394 
3395 		dc->hwss.enable_audio_stream(pipe_ctx);
3396 
3397 	} else { // if (IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment))
3398 		if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3399 				dc_is_virtual_signal(pipe_ctx->stream->signal))
3400 			dp_set_dsc_enable(pipe_ctx, true);
3401 
3402 	}
3403 
3404 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
3405 		core_link_set_avmute(pipe_ctx, false);
3406 	}
3407 }
3408 
core_link_disable_stream(struct pipe_ctx * pipe_ctx)3409 void core_link_disable_stream(struct pipe_ctx *pipe_ctx)
3410 {
3411 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
3412 	struct dc_stream_state *stream = pipe_ctx->stream;
3413 	struct dc_link *link = stream->sink->link;
3414 
3415 	if (!IS_DIAG_DC(dc->ctx->dce_environment) &&
3416 			dc_is_virtual_signal(pipe_ctx->stream->signal))
3417 		return;
3418 
3419 	if (!pipe_ctx->stream->sink->edid_caps.panel_patch.skip_avmute) {
3420 		if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
3421 			core_link_set_avmute(pipe_ctx, true);
3422 	}
3423 
3424 	dc->hwss.disable_audio_stream(pipe_ctx);
3425 
3426 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3427 	update_psp_stream_config(pipe_ctx, true);
3428 #endif
3429 	dc->hwss.blank_stream(pipe_ctx);
3430 
3431 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3432 		deallocate_mst_payload(pipe_ctx);
3433 
3434 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
3435 		struct ext_hdmi_settings settings = {0};
3436 		enum engine_id eng_id = pipe_ctx->stream_res.stream_enc->id;
3437 
3438 		unsigned short masked_chip_caps = link->chip_caps &
3439 				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
3440 		//Need to inform that sink is going to use legacy HDMI mode.
3441 		dal_ddc_service_write_scdc_data(
3442 			link->ddc,
3443 			165000,//vbios only handles 165Mhz.
3444 			false);
3445 		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
3446 			/* DP159, Retimer settings */
3447 			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings))
3448 				write_i2c_retimer_setting(pipe_ctx,
3449 						false, false, &settings);
3450 			else
3451 				write_i2c_default_retimer_setting(pipe_ctx,
3452 						false, false);
3453 		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
3454 			/* PI3EQX1204, Redriver settings */
3455 			write_i2c_redriver_setting(pipe_ctx, false);
3456 		}
3457 	}
3458 
3459 	disable_link(pipe_ctx->stream->link, pipe_ctx->stream->signal);
3460 
3461 	dc->hwss.disable_stream(pipe_ctx);
3462 
3463 	if (pipe_ctx->stream->timing.flags.DSC) {
3464 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
3465 			dp_set_dsc_enable(pipe_ctx, false);
3466 	}
3467 }
3468 
core_link_set_avmute(struct pipe_ctx * pipe_ctx,bool enable)3469 void core_link_set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
3470 {
3471 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
3472 
3473 	if (!dc_is_hdmi_signal(pipe_ctx->stream->signal))
3474 		return;
3475 
3476 	dc->hwss.set_avmute(pipe_ctx, enable);
3477 }
3478 
3479 /**
3480  *  dc_link_enable_hpd_filter:
3481  *     If enable is true, programs HPD filter on associated HPD line using
3482  *     delay_on_disconnect/delay_on_connect values dependent on
3483  *     link->connector_signal
3484  *
3485  *     If enable is false, programs HPD filter on associated HPD line with no
3486  *     delays on connect or disconnect
3487  *
3488  *  @link:   pointer to the dc link
3489  *  @enable: boolean specifying whether to enable hbd
3490  */
dc_link_enable_hpd_filter(struct dc_link * link,bool enable)3491 void dc_link_enable_hpd_filter(struct dc_link *link, bool enable)
3492 {
3493 	struct gpio *hpd;
3494 
3495 	if (enable) {
3496 		link->is_hpd_filter_disabled = false;
3497 		program_hpd_filter(link);
3498 	} else {
3499 		link->is_hpd_filter_disabled = true;
3500 		/* Obtain HPD handle */
3501 		hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);
3502 
3503 		if (!hpd)
3504 			return;
3505 
3506 		/* Setup HPD filtering */
3507 		if (dal_gpio_open(hpd, GPIO_MODE_INTERRUPT) == GPIO_RESULT_OK) {
3508 			struct gpio_hpd_config config;
3509 
3510 			config.delay_on_connect = 0;
3511 			config.delay_on_disconnect = 0;
3512 
3513 			dal_irq_setup_hpd_filter(hpd, &config);
3514 
3515 			dal_gpio_close(hpd);
3516 		} else {
3517 			ASSERT_CRITICAL(false);
3518 		}
3519 		/* Release HPD handle */
3520 		dal_gpio_destroy_irq(&hpd);
3521 	}
3522 }
3523 
dc_link_set_drive_settings(struct dc * dc,struct link_training_settings * lt_settings,const struct dc_link * link)3524 void dc_link_set_drive_settings(struct dc *dc,
3525 				struct link_training_settings *lt_settings,
3526 				const struct dc_link *link)
3527 {
3528 
3529 	int i;
3530 
3531 	for (i = 0; i < dc->link_count; i++) {
3532 		if (dc->links[i] == link)
3533 			break;
3534 	}
3535 
3536 	if (i >= dc->link_count)
3537 		ASSERT_CRITICAL(false);
3538 
3539 	dc_link_dp_set_drive_settings(dc->links[i], lt_settings);
3540 }
3541 
dc_link_set_preferred_link_settings(struct dc * dc,struct dc_link_settings * link_setting,struct dc_link * link)3542 void dc_link_set_preferred_link_settings(struct dc *dc,
3543 					 struct dc_link_settings *link_setting,
3544 					 struct dc_link *link)
3545 {
3546 	int i;
3547 	struct pipe_ctx *pipe;
3548 	struct dc_stream_state *link_stream;
3549 	struct dc_link_settings store_settings = *link_setting;
3550 
3551 	link->preferred_link_setting = store_settings;
3552 
3553 	/* Retrain with preferred link settings only relevant for
3554 	 * DP signal type
3555 	 * Check for non-DP signal or if passive dongle present
3556 	 */
3557 	if (!dc_is_dp_signal(link->connector_signal) ||
3558 		link->dongle_max_pix_clk > 0)
3559 		return;
3560 
3561 	for (i = 0; i < MAX_PIPES; i++) {
3562 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3563 		if (pipe->stream && pipe->stream->link) {
3564 			if (pipe->stream->link == link) {
3565 				link_stream = pipe->stream;
3566 				break;
3567 			}
3568 		}
3569 	}
3570 
3571 	/* Stream not found */
3572 	if (i == MAX_PIPES)
3573 		return;
3574 
3575 	/* Cannot retrain link if backend is off */
3576 	if (link_stream->dpms_off)
3577 		return;
3578 
3579 	decide_link_settings(link_stream, &store_settings);
3580 
3581 	if ((store_settings.lane_count != LANE_COUNT_UNKNOWN) &&
3582 		(store_settings.link_rate != LINK_RATE_UNKNOWN))
3583 		dp_retrain_link_dp_test(link, &store_settings, false);
3584 }
3585 
dc_link_set_preferred_training_settings(struct dc * dc,struct dc_link_settings * link_setting,struct dc_link_training_overrides * lt_overrides,struct dc_link * link,bool skip_immediate_retrain)3586 void dc_link_set_preferred_training_settings(struct dc *dc,
3587 						 struct dc_link_settings *link_setting,
3588 						 struct dc_link_training_overrides *lt_overrides,
3589 						 struct dc_link *link,
3590 						 bool skip_immediate_retrain)
3591 {
3592 	if (lt_overrides != NULL)
3593 		link->preferred_training_settings = *lt_overrides;
3594 	else
3595 		memset(&link->preferred_training_settings, 0, sizeof(link->preferred_training_settings));
3596 
3597 	if (link_setting != NULL) {
3598 		link->preferred_link_setting = *link_setting;
3599 	} else {
3600 		link->preferred_link_setting.lane_count = LANE_COUNT_UNKNOWN;
3601 		link->preferred_link_setting.link_rate = LINK_RATE_UNKNOWN;
3602 	}
3603 
3604 	/* Retrain now, or wait until next stream update to apply */
3605 	if (skip_immediate_retrain == false)
3606 		dc_link_set_preferred_link_settings(dc, &link->preferred_link_setting, link);
3607 }
3608 
dc_link_enable_hpd(const struct dc_link * link)3609 void dc_link_enable_hpd(const struct dc_link *link)
3610 {
3611 	dc_link_dp_enable_hpd(link);
3612 }
3613 
dc_link_disable_hpd(const struct dc_link * link)3614 void dc_link_disable_hpd(const struct dc_link *link)
3615 {
3616 	dc_link_dp_disable_hpd(link);
3617 }
3618 
dc_link_set_test_pattern(struct dc_link * link,enum dp_test_pattern test_pattern,enum dp_test_pattern_color_space test_pattern_color_space,const struct link_training_settings * p_link_settings,const unsigned char * p_custom_pattern,unsigned int cust_pattern_size)3619 void dc_link_set_test_pattern(struct dc_link *link,
3620 			      enum dp_test_pattern test_pattern,
3621 			      enum dp_test_pattern_color_space test_pattern_color_space,
3622 			      const struct link_training_settings *p_link_settings,
3623 			      const unsigned char *p_custom_pattern,
3624 			      unsigned int cust_pattern_size)
3625 {
3626 	if (link != NULL)
3627 		dc_link_dp_set_test_pattern(
3628 			link,
3629 			test_pattern,
3630 			test_pattern_color_space,
3631 			p_link_settings,
3632 			p_custom_pattern,
3633 			cust_pattern_size);
3634 }
3635 
dc_link_bandwidth_kbps(const struct dc_link * link,const struct dc_link_settings * link_setting)3636 uint32_t dc_link_bandwidth_kbps(
3637 	const struct dc_link *link,
3638 	const struct dc_link_settings *link_setting)
3639 {
3640 	uint32_t link_bw_kbps =
3641 		link_setting->link_rate * LINK_RATE_REF_FREQ_IN_KHZ; /* bytes per sec */
3642 
3643 	link_bw_kbps *= 8;   /* 8 bits per byte*/
3644 	link_bw_kbps *= link_setting->lane_count;
3645 
3646 	if (dc_link_should_enable_fec(link)) {
3647 		/* Account for FEC overhead.
3648 		 * We have to do it based on caps,
3649 		 * and not based on FEC being set ready,
3650 		 * because FEC is set ready too late in
3651 		 * the process to correctly be picked up
3652 		 * by mode enumeration.
3653 		 *
3654 		 * There's enough zeros at the end of 'kbps'
3655 		 * that make the below operation 100% precise
3656 		 * for our purposes.
3657 		 * 'long long' makes it work even for HDMI 2.1
3658 		 * max bandwidth (and much, much bigger bandwidths
3659 		 * than that, actually).
3660 		 *
3661 		 * NOTE: Reducing link BW by 3% may not be precise
3662 		 * because it may be a stream BT that increases by 3%, and so
3663 		 * 1/1.03 = 0.970873 factor should have been used instead,
3664 		 * but the difference is minimal and is in a safe direction,
3665 		 * which all works well around potential ambiguity of DP 1.4a spec.
3666 		 */
3667 		long long fec_link_bw_kbps = link_bw_kbps * 970LL;
3668 		link_bw_kbps = (uint32_t)(div64_s64(fec_link_bw_kbps, 1000LL));
3669 	}
3670 
3671 	return link_bw_kbps;
3672 
3673 }
3674 
dc_link_get_link_cap(const struct dc_link * link)3675 const struct dc_link_settings *dc_link_get_link_cap(
3676 		const struct dc_link *link)
3677 {
3678 	if (link->preferred_link_setting.lane_count != LANE_COUNT_UNKNOWN &&
3679 			link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN)
3680 		return &link->preferred_link_setting;
3681 	return &link->verified_link_cap;
3682 }
3683 
dc_link_overwrite_extended_receiver_cap(struct dc_link * link)3684 void dc_link_overwrite_extended_receiver_cap(
3685 		struct dc_link *link)
3686 {
3687 	dp_overwrite_extended_receiver_cap(link);
3688 }
3689 
dc_link_is_fec_supported(const struct dc_link * link)3690 bool dc_link_is_fec_supported(const struct dc_link *link)
3691 {
3692 	struct link_encoder *link_enc = NULL;
3693 
3694 	/* Links supporting dynamically assigned link encoder will be assigned next
3695 	 * available encoder if one not already assigned.
3696 	 */
3697 	if (link->is_dig_mapping_flexible &&
3698 			link->dc->res_pool->funcs->link_encs_assign) {
3699 		link_enc = link_enc_cfg_get_link_enc_used_by_link(link->dc->current_state, link);
3700 		if (link_enc == NULL)
3701 			link_enc = link_enc_cfg_get_next_avail_link_enc(link->dc, link->dc->current_state);
3702 	} else
3703 		link_enc = link->link_enc;
3704 	ASSERT(link_enc);
3705 
3706 	return (dc_is_dp_signal(link->connector_signal) &&
3707 			link_enc->features.fec_supported &&
3708 			link->dpcd_caps.fec_cap.bits.FEC_CAPABLE &&
3709 			!IS_FPGA_MAXIMUS_DC(link->ctx->dce_environment));
3710 }
3711 
dc_link_should_enable_fec(const struct dc_link * link)3712 bool dc_link_should_enable_fec(const struct dc_link *link)
3713 {
3714 	bool is_fec_disable = false;
3715 	bool ret = false;
3716 
3717 	if ((link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT_MST &&
3718 			link->local_sink &&
3719 			link->local_sink->edid_caps.panel_patch.disable_fec) ||
3720 			(link->connector_signal == SIGNAL_TYPE_EDP &&
3721 					link->dc->debug.force_enable_edp_fec == false)) // Disable FEC for eDP
3722 		is_fec_disable = true;
3723 
3724 	if (dc_link_is_fec_supported(link) && !link->dc->debug.disable_fec && !is_fec_disable)
3725 		ret = true;
3726 
3727 	return ret;
3728 }
3729 
dc_bandwidth_in_kbps_from_timing(const struct dc_crtc_timing * timing)3730 uint32_t dc_bandwidth_in_kbps_from_timing(
3731 		const struct dc_crtc_timing *timing)
3732 {
3733 	uint32_t bits_per_channel = 0;
3734 	uint32_t kbps;
3735 
3736 #if defined(CONFIG_DRM_AMD_DC_DCN)
3737 	if (timing->flags.DSC)
3738 		return dc_dsc_stream_bandwidth_in_kbps(timing,
3739 				timing->dsc_cfg.bits_per_pixel,
3740 				timing->dsc_cfg.num_slices_h,
3741 				timing->dsc_cfg.is_dp);
3742 #endif
3743 
3744 	switch (timing->display_color_depth) {
3745 	case COLOR_DEPTH_666:
3746 		bits_per_channel = 6;
3747 		break;
3748 	case COLOR_DEPTH_888:
3749 		bits_per_channel = 8;
3750 		break;
3751 	case COLOR_DEPTH_101010:
3752 		bits_per_channel = 10;
3753 		break;
3754 	case COLOR_DEPTH_121212:
3755 		bits_per_channel = 12;
3756 		break;
3757 	case COLOR_DEPTH_141414:
3758 		bits_per_channel = 14;
3759 		break;
3760 	case COLOR_DEPTH_161616:
3761 		bits_per_channel = 16;
3762 		break;
3763 	default:
3764 		ASSERT(bits_per_channel != 0);
3765 		bits_per_channel = 8;
3766 		break;
3767 	}
3768 
3769 	kbps = timing->pix_clk_100hz / 10;
3770 	kbps *= bits_per_channel;
3771 
3772 	if (timing->flags.Y_ONLY != 1) {
3773 		/*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/
3774 		kbps *= 3;
3775 		if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
3776 			kbps /= 2;
3777 		else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
3778 			kbps = kbps * 2 / 3;
3779 	}
3780 
3781 	return kbps;
3782 
3783 }
3784