dc_link.c 85.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright 2012-15 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: AMD
 *
 */

26 27
#include <linux/slab.h>

28
#include "dm_services.h"
29
#include "atom.h"
30 31 32 33 34 35 36 37
#include "dm_helpers.h"
#include "dc.h"
#include "grph_object_id.h"
#include "gpio_service_interface.h"
#include "core_status.h"
#include "dc_link_dp.h"
#include "dc_link_ddc.h"
#include "link_hwss.h"
38
#include "opp.h"
39

40 41 42
#include "link_encoder.h"
#include "hw_sequencer.h"
#include "resource.h"
43
#include "abm.h"
44
#include "fixed31_32.h"
45
#include "dpcd_defs.h"
A
Amy Zhang 已提交
46
#include "dmcu.h"
47
#include "hw/clk_mgr.h"
48

49 50
#define DC_LOGGER_INIT(logger)

51 52

#define LINK_INFO(...) \
53
	DC_LOG_HW_HOTPLUG(  \
54 55
		__VA_ARGS__)

56 57 58
#define RETIMER_REDRIVER_INFO(...) \
	DC_LOG_RETIMER_REDRIVER(  \
		__VA_ARGS__)
59 60 61 62 63
/*******************************************************************************
 * Private structures
 ******************************************************************************/

enum {
64 65 66 67 68 69 70 71
	PEAK_FACTOR_X1000 = 1006,
	/*
	* Some receivers fail to train on first try and are good
	* on subsequent tries. 2 retries should be plenty. If we
	* don't have a successful training then we don't expect to
	* ever get one.
	*/
	LINK_TRAINING_MAX_VERIFY_RETRY = 2
72 73 74 75 76
};

/*******************************************************************************
 * Private functions
 ******************************************************************************/
77
static void destruct(struct dc_link *link)
78 79 80
{
	int i;

81 82 83 84 85 86
	if (link->hpd_gpio != NULL) {
		dal_gpio_close(link->hpd_gpio);
		dal_gpio_destroy_irq(&link->hpd_gpio);
		link->hpd_gpio = NULL;
	}

87 88
	if (link->ddc)
		dal_ddc_service_destroy(&link->ddc);
89 90 91 92

	if(link->link_enc)
		link->link_enc->funcs->destroy(&link->link_enc);

93 94
	if (link->local_sink)
		dc_sink_release(link->local_sink);
95

96 97
	for (i = 0; i < link->sink_count; ++i)
		dc_sink_release(link->remote_sinks[i]);
98 99
}

100 101 102
struct gpio *get_hpd_gpio(struct dc_bios *dcb,
		struct graphics_object_id link_id,
		struct gpio_service *gpio_service)
103 104 105 106 107
{
	enum bp_result bp_result;
	struct graphics_object_hpd_info hpd_info;
	struct gpio_pin_info pin_info;

108
	if (dcb->funcs->get_hpd_info(dcb, link_id, &hpd_info) != BP_RESULT_OK)
109 110 111 112 113 114 115 116 117 118 119
		return NULL;

	bp_result = dcb->funcs->get_gpio_pin_info(dcb,
		hpd_info.hpd_int_gpio_uid, &pin_info);

	if (bp_result != BP_RESULT_OK) {
		ASSERT(bp_result == BP_RESULT_NORECORD);
		return NULL;
	}

	return dal_gpio_service_create_irq(
120
		gpio_service,
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		pin_info.offset,
		pin_info.mask);
}

/*
 *  Function: program_hpd_filter
 *
 *  @brief
 *     Programs HPD filter on associated HPD line
 *
 *  @param [in] delay_on_connect_in_ms: Connect filter timeout
 *  @param [in] delay_on_disconnect_in_ms: Disconnect filter timeout
 *
 *  @return
 *     true on success, false otherwise
 */
static bool program_hpd_filter(
138
	const struct dc_link *link)
139 140 141 142 143 144 145 146
{
	bool result = false;

	struct gpio *hpd;

	int delay_on_connect_in_ms = 0;
	int delay_on_disconnect_in_ms = 0;

147 148
	if (link->is_hpd_filter_disabled)
		return false;
149
	/* Verify feature is supported */
150
	switch (link->connector_signal) {
151 152 153 154 155
	case SIGNAL_TYPE_DVI_SINGLE_LINK:
	case SIGNAL_TYPE_DVI_DUAL_LINK:
	case SIGNAL_TYPE_HDMI_TYPE_A:
		/* Program hpd filter */
		delay_on_connect_in_ms = 500;
156
		delay_on_disconnect_in_ms = 100;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		break;
	case SIGNAL_TYPE_DISPLAY_PORT:
	case SIGNAL_TYPE_DISPLAY_PORT_MST:
		/* Program hpd filter to allow DP signal to settle */
		/* 500:	not able to detect MST <-> SST switch as HPD is low for
		 * 	only 100ms on DELL U2413
		 * 0:	some passive dongle still show aux mode instead of i2c
		 * 20-50:not enough to hide bouncing HPD with passive dongle.
		 * 	also see intermittent i2c read issues.
		 */
		delay_on_connect_in_ms = 80;
		delay_on_disconnect_in_ms = 0;
		break;
	case SIGNAL_TYPE_LVDS:
	case SIGNAL_TYPE_EDP:
	default:
		/* Don't program hpd filter */
		return false;
	}

	/* Obtain HPD handle */
178
	hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

	if (!hpd)
		return result;

	/* Setup HPD filtering */
	if (dal_gpio_open(hpd, GPIO_MODE_INTERRUPT) == GPIO_RESULT_OK) {
		struct gpio_hpd_config config;

		config.delay_on_connect = delay_on_connect_in_ms;
		config.delay_on_disconnect = delay_on_disconnect_in_ms;

		dal_irq_setup_hpd_filter(hpd, &config);

		dal_gpio_close(hpd);

		result = true;
	} else {
		ASSERT_CRITICAL(false);
	}

	/* Release HPD handle */
	dal_gpio_destroy_irq(&hpd);

	return result;
}

205 206 207 208 209 210 211
/**
 * dc_link_detect_sink() - Determine if there is a sink connected
 *
 * @type: Returned connection type
 * Does not detect downstream devices, such as MST sinks
 * or display connected through active dongles
 */
212
bool dc_link_detect_sink(struct dc_link *link, enum dc_connection_type *type)
213 214 215 216
{
	uint32_t is_hpd_high = 0;
	struct gpio *hpd_pin;

217 218 219 220 221
	if (link->connector_signal == SIGNAL_TYPE_LVDS) {
		*type = dc_connection_single;
		return true;
	}

222 223 224
	if (link->connector_signal == SIGNAL_TYPE_EDP) {
		/*in case it is not on*/
		link->dc->hwss.edp_power_control(link, true);
225
		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
226
	}
227

228
	/* todo: may need to lock gpio access */
229
	hpd_pin = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	if (hpd_pin == NULL)
		goto hpd_gpio_failure;

	dal_gpio_open(hpd_pin, GPIO_MODE_INTERRUPT);
	dal_gpio_get_value(hpd_pin, &is_hpd_high);
	dal_gpio_close(hpd_pin);
	dal_gpio_destroy_irq(&hpd_pin);

	if (is_hpd_high) {
		*type = dc_connection_single;
		/* TODO: need to do the actual detection */
	} else {
		*type = dc_connection_none;
	}

	return true;

hpd_gpio_failure:
	return false;
}

251
static enum ddc_transaction_type get_ddc_transaction_type(
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		enum signal_type sink_signal)
{
	enum ddc_transaction_type transaction_type = DDC_TRANSACTION_TYPE_NONE;

	switch (sink_signal) {
	case SIGNAL_TYPE_DVI_SINGLE_LINK:
	case SIGNAL_TYPE_DVI_DUAL_LINK:
	case SIGNAL_TYPE_HDMI_TYPE_A:
	case SIGNAL_TYPE_LVDS:
	case SIGNAL_TYPE_RGB:
		transaction_type = DDC_TRANSACTION_TYPE_I2C;
		break;

	case SIGNAL_TYPE_DISPLAY_PORT:
	case SIGNAL_TYPE_EDP:
		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
		break;

	case SIGNAL_TYPE_DISPLAY_PORT_MST:
		/* MST does not use I2COverAux, but there is the
		 * SPECIAL use case for "immediate dwnstrm device
		 * access" (EPR#370830). */
		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
		break;

	default:
		break;
	}

	return transaction_type;
}

static enum signal_type get_basic_signal_type(
	struct graphics_object_id encoder,
	struct graphics_object_id downstream)
{
	if (downstream.type == OBJECT_TYPE_CONNECTOR) {
		switch (downstream.id) {
		case CONNECTOR_ID_SINGLE_LINK_DVII:
			switch (encoder.id) {
			case ENCODER_ID_INTERNAL_DAC1:
			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
			case ENCODER_ID_INTERNAL_DAC2:
			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
				return SIGNAL_TYPE_RGB;
			default:
				return SIGNAL_TYPE_DVI_SINGLE_LINK;
			}
		break;
		case CONNECTOR_ID_DUAL_LINK_DVII:
		{
			switch (encoder.id) {
			case ENCODER_ID_INTERNAL_DAC1:
			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
			case ENCODER_ID_INTERNAL_DAC2:
			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
				return SIGNAL_TYPE_RGB;
			default:
				return SIGNAL_TYPE_DVI_DUAL_LINK;
			}
		}
		break;
		case CONNECTOR_ID_SINGLE_LINK_DVID:
			return SIGNAL_TYPE_DVI_SINGLE_LINK;
		case CONNECTOR_ID_DUAL_LINK_DVID:
			return SIGNAL_TYPE_DVI_DUAL_LINK;
		case CONNECTOR_ID_VGA:
			return SIGNAL_TYPE_RGB;
		case CONNECTOR_ID_HDMI_TYPE_A:
			return SIGNAL_TYPE_HDMI_TYPE_A;
		case CONNECTOR_ID_LVDS:
			return SIGNAL_TYPE_LVDS;
		case CONNECTOR_ID_DISPLAY_PORT:
			return SIGNAL_TYPE_DISPLAY_PORT;
		case CONNECTOR_ID_EDP:
			return SIGNAL_TYPE_EDP;
		default:
			return SIGNAL_TYPE_NONE;
		}
	} else if (downstream.type == OBJECT_TYPE_ENCODER) {
		switch (downstream.id) {
		case ENCODER_ID_EXTERNAL_NUTMEG:
		case ENCODER_ID_EXTERNAL_TRAVIS:
			return SIGNAL_TYPE_DISPLAY_PORT;
		default:
			return SIGNAL_TYPE_NONE;
		}
	}

	return SIGNAL_TYPE_NONE;
}

344 345 346
/**
 * dc_link_is_dp_sink_present() - Check if there is a native DP
 * or passive DP-HDMI dongle connected
347
 */
348
bool dc_link_is_dp_sink_present(struct dc_link *link)
349 350 351
{
	enum gpio_result gpio_result;
	uint32_t clock_pin = 0;
352
	uint8_t retry = 0;
353 354 355 356 357 358 359 360 361
	struct ddc *ddc;

	enum connector_id connector_id =
		dal_graphics_object_id_get_connector_id(link->link_id);

	bool present =
		((connector_id == CONNECTOR_ID_DISPLAY_PORT) ||
		(connector_id == CONNECTOR_ID_EDP));

362
	ddc = dal_ddc_service_get_ddc_pin(link->ddc);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

	if (!ddc) {
		BREAK_TO_DEBUGGER();
		return present;
	}

	/* Open GPIO and set it to I2C mode */
	/* Note: this GpioMode_Input will be converted
	 * to GpioConfigType_I2cAuxDualMode in GPIO component,
	 * which indicates we need additional delay */

	if (GPIO_RESULT_OK != dal_ddc_open(
		ddc, GPIO_MODE_INPUT, GPIO_DDC_CONFIG_TYPE_MODE_I2C)) {
		dal_gpio_destroy_ddc(&ddc);

		return present;
	}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	/*
	 * Read GPIO: DP sink is present if both clock and data pins are zero
	 *
	 * [W/A] plug-unplug DP cable, sometimes customer board has
	 * one short pulse on clk_pin(1V, < 1ms). DP will be config to HDMI/DVI
	 * then monitor can't br light up. Add retry 3 times
	 * But in real passive dongle, it need additional 3ms to detect
	 */
	do {
		gpio_result = dal_gpio_get_value(ddc->pin_clock, &clock_pin);
		ASSERT(gpio_result == GPIO_RESULT_OK);
		if (clock_pin)
			udelay(1000);
		else
			break;
	} while (retry++ < 3);
397

398
	present = (gpio_result == GPIO_RESULT_OK) && !clock_pin;
399 400 401 402 403 404 405 406 407 408

	dal_ddc_close(ddc);

	return present;
}

/*
 * @brief
 * Detect output sink type
 */
409 410 411
static enum signal_type link_detect_sink(
	struct dc_link *link,
	enum dc_detect_reason reason)
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
{
	enum signal_type result = get_basic_signal_type(
		link->link_enc->id, link->link_id);

	/* Internal digital encoder will detect only dongles
	 * that require digital signal */

	/* Detection mechanism is different
	 * for different native connectors.
	 * LVDS connector supports only LVDS signal;
	 * PCIE is a bus slot, the actual connector needs to be detected first;
	 * eDP connector supports only eDP signal;
	 * HDMI should check straps for audio */

	/* PCIE detects the actual connector on add-on board */

	if (link->link_id.id == CONNECTOR_ID_PCIE) {
		/* ZAZTODO implement PCIE add-on card detection */
	}

	switch (link->link_id.id) {
	case CONNECTOR_ID_HDMI_TYPE_A: {
		/* check audio support:
		 * if native HDMI is not supported, switch to DVI */
		struct audio_support *aud_support = &link->dc->res_pool->audio_support;

		if (!aud_support->hdmi_audio_native)
			if (link->link_id.id == CONNECTOR_ID_HDMI_TYPE_A)
				result = SIGNAL_TYPE_DVI_SINGLE_LINK;
	}
	break;
	case CONNECTOR_ID_DISPLAY_PORT: {
444 445 446 447 448 449 450 451
		/* DP HPD short pulse. Passive DP dongle will not
		 * have short pulse
		 */
		if (reason != DETECT_REASON_HPDRX) {
			/* Check whether DP signal detected: if not -
			 * we assume signal is DVI; it could be corrected
			 * to HDMI after dongle detection
			 */
452
			if (!dm_helpers_is_dp_sink_present(link))
453 454
				result = SIGNAL_TYPE_DVI_SINGLE_LINK;
		}
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	}
	break;
	default:
	break;
	}

	return result;
}

static enum signal_type decide_signal_from_strap_and_dongle_type(
		enum display_dongle_type dongle_type,
		struct audio_support *audio_support)
{
	enum signal_type signal = SIGNAL_TYPE_NONE;

	switch (dongle_type) {
	case DISPLAY_DONGLE_DP_HDMI_DONGLE:
		if (audio_support->hdmi_audio_on_dongle)
			signal =  SIGNAL_TYPE_HDMI_TYPE_A;
		else
			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
		break;
	case DISPLAY_DONGLE_DP_DVI_DONGLE:
		signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
		break;
	case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
		if (audio_support->hdmi_audio_native)
			signal =  SIGNAL_TYPE_HDMI_TYPE_A;
		else
			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
		break;
	default:
		signal = SIGNAL_TYPE_NONE;
		break;
	}

	return signal;
}

static enum signal_type dp_passive_dongle_detection(
		struct ddc_service *ddc,
		struct display_sink_capability *sink_cap,
		struct audio_support *audio_support)
{
	dal_ddc_service_i2c_query_dp_dual_mode_adaptor(
						ddc, sink_cap);
	return decide_signal_from_strap_and_dongle_type(
			sink_cap->dongle_type,
			audio_support);
}

506
static void link_disconnect_sink(struct dc_link *link)
507
{
508 509 510
	if (link->local_sink) {
		dc_sink_release(link->local_sink);
		link->local_sink = NULL;
511 512 513 514 515
	}

	link->dpcd_sink_count = 0;
}

516 517 518 519 520 521 522
static void link_disconnect_remap(struct dc_sink *prev_sink, struct dc_link *link)
{
	dc_sink_release(link->local_sink);
	link->local_sink = prev_sink;
}


523 524 525 526 527
static void read_edp_current_link_settings_on_detect(struct dc_link *link)
{
	union lane_count_set lane_count_set = { {0} };
	uint8_t link_bw_set;
	uint8_t link_rate_set;
528 529 530
	uint32_t read_dpcd_retry_cnt = 10;
	enum dc_status status = DC_ERROR_UNEXPECTED;
	int i;
531
	union max_down_spread max_down_spread = { {0} };
532 533

	// Read DPCD 00101h to find out the number of lanes currently set
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	for (i = 0; i < read_dpcd_retry_cnt; i++) {
		status = core_link_read_dpcd(
				link,
				DP_LANE_COUNT_SET,
				&lane_count_set.raw,
				sizeof(lane_count_set));
		/* First DPCD read after VDD ON can fail if the particular board
		 * does not have HPD pin wired correctly. So if DPCD read fails,
		 * which it should never happen, retry a few times. Target worst
		 * case scenario of 80 ms.
		 */
		if (status == DC_OK) {
			link->cur_link_settings.lane_count = lane_count_set.bits.LANE_COUNT_SET;
			break;
		}

550
		msleep(8);
551 552
	}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
	// Read DPCD 00100h to find if standard link rates are set
	core_link_read_dpcd(link, DP_LINK_BW_SET,
			&link_bw_set, sizeof(link_bw_set));

	if (link_bw_set == 0) {
		/* If standard link rates are not being used,
		 * Read DPCD 00115h to find the link rate set used
		 */
		core_link_read_dpcd(link, DP_LINK_RATE_SET,
				&link_rate_set, sizeof(link_rate_set));

		if (link_rate_set < link->dpcd_caps.edp_supported_link_rates_count) {
			link->cur_link_settings.link_rate =
				link->dpcd_caps.edp_supported_link_rates[link_rate_set];
			link->cur_link_settings.link_rate_set = link_rate_set;
			link->cur_link_settings.use_link_rate_set = true;
		}
	} else {
		link->cur_link_settings.link_rate = link_bw_set;
		link->cur_link_settings.use_link_rate_set = false;
	}
574 575 576 577 578 579
	// Read DPCD 00003h to find the max down spread.
	core_link_read_dpcd(link, DP_MAX_DOWNSPREAD,
			&max_down_spread.raw, sizeof(max_down_spread));
	link->cur_link_settings.link_spread =
		max_down_spread.bits.MAX_DOWN_SPREAD ?
		LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
580 581
}

582
static bool detect_dp(
583
	struct dc_link *link,
584 585 586
	struct display_sink_capability *sink_caps,
	bool *converter_disable_audio,
	struct audio_support *audio_support,
587
	enum dc_detect_reason reason)
588
{
589 590
	bool boot = false;
	sink_caps->signal = link_detect_sink(link, reason);
591 592 593 594 595
	sink_caps->transaction_type =
		get_ddc_transaction_type(sink_caps->signal);

	if (sink_caps->transaction_type == DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
		sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT;
596 597
		if (!detect_dp_sink_caps(link))
			return false;
598 599 600

		if (is_mst_supported(link)) {
			sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
601
			link->type = dc_connection_mst_branch;
602

603 604 605 606
			dal_ddc_service_set_transaction_type(
							link->ddc,
							sink_caps->transaction_type);

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
			/*
			 * This call will initiate MST topology discovery. Which
			 * will detect MST ports and add new DRM connector DRM
			 * framework. Then read EDID via remote i2c over aux. In
			 * the end, will notify DRM detect result and save EDID
			 * into DRM framework.
			 *
			 * .detect is called by .fill_modes.
			 * .fill_modes is called by user mode ioctl
			 * DRM_IOCTL_MODE_GETCONNECTOR.
			 *
			 * .get_modes is called by .fill_modes.
			 *
			 * call .get_modes, AMDGPU DM implementation will create
			 * new dc_sink and add to dc_link. For long HPD plug
			 * in/out, MST has its own handle.
			 *
			 * Therefore, just after dc_create, link->sink is not
			 * created for MST until user mode app calls
			 * DRM_IOCTL_MODE_GETCONNECTOR.
			 *
			 * Need check ->sink usages in case ->sink = NULL
			 * TODO: s3 resume check
			 */
631 632
			if (reason == DETECT_REASON_BOOT)
				boot = true;
633

634 635 636 637
			dm_helpers_dp_update_branch_info(
				link->ctx,
				link);

638
			if (!dm_helpers_dp_mst_start_top_mgr(
639
				link->ctx,
640
				link, boot)) {
641
				/* MST not supported */
642
				link->type = dc_connection_single;
643 644 645
				sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT;
			}
		}
646 647 648 649 650 651 652 653 654 655

		if (link->type != dc_connection_mst_branch &&
			is_dp_active_dongle(link)) {
			/* DP active dongles */
			link->type = dc_connection_active_dongle;
			if (!link->dpcd_caps.sink_count.bits.SINK_COUNT) {
				/*
				 * active dongle unplug processing for short irq
				 */
				link_disconnect_sink(link);
656
				return true;
657 658 659 660 661
			}

			if (link->dpcd_caps.dongle_type != DISPLAY_DONGLE_DP_HDMI_CONVERTER)
				*converter_disable_audio = true;
		}
662 663
	} else {
		/* DP passive dongles */
664
		sink_caps->signal = dp_passive_dongle_detection(link->ddc,
665 666 667
				sink_caps,
				audio_support);
	}
668 669

	return true;
670 671
}

672 673 674 675 676 677 678 679 680 681 682
static bool is_same_edid(struct dc_edid *old_edid, struct dc_edid *new_edid)
{
	if (old_edid->length != new_edid->length)
		return false;

	if (new_edid->length == 0)
		return false;

	return (memcmp(old_edid->raw_edid, new_edid->raw_edid, new_edid->length) == 0);
}

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
bool wait_for_alt_mode(struct dc_link *link)
{

	/**
	 * something is terribly wrong if time out is > 200ms. (5Hz)
	 * 500 microseconds * 400 tries us 200 ms
	 **/
	unsigned int sleep_time_in_microseconds = 500;
	unsigned int tries_allowed = 400;
	bool is_in_alt_mode;
	unsigned long long enter_timestamp;
	unsigned long long finish_timestamp;
	unsigned long long time_taken_in_ns;
	int tries_taken;

	DC_LOGGER_INIT(link->ctx->logger);

	if (link->link_enc->funcs->is_in_alt_mode == NULL)
		return true;

	is_in_alt_mode = link->link_enc->funcs->is_in_alt_mode(link->link_enc);
	DC_LOG_WARNING("DP Alt mode state on HPD: %d\n", is_in_alt_mode);

	if (is_in_alt_mode)
		return true;

	enter_timestamp = dm_get_timestamp(link->ctx);

	for (tries_taken = 0; tries_taken < tries_allowed; tries_taken++) {
		udelay(sleep_time_in_microseconds);
		/* ask the link if alt mode is enabled, if so return ok */
		if (link->link_enc->funcs->is_in_alt_mode(link->link_enc)) {

			finish_timestamp = dm_get_timestamp(link->ctx);
			time_taken_in_ns = dm_get_elapse_time_in_ns(
				link->ctx, finish_timestamp, enter_timestamp);
			DC_LOG_WARNING("Alt mode entered finished after %llu ms\n",
720
				       div_u64(time_taken_in_ns, 1000000));
721 722 723 724 725 726 727 728
			return true;
		}

	}
	finish_timestamp = dm_get_timestamp(link->ctx);
	time_taken_in_ns = dm_get_elapse_time_in_ns(link->ctx, finish_timestamp,
						    enter_timestamp);
	DC_LOG_WARNING("Alt mode has timed out after %llu ms\n",
729
			div_u64(time_taken_in_ns, 1000000));
730 731 732
	return false;
}

733 734 735 736 737 738 739 740
/**
 * dc_link_detect() - Detect if a sink is attached to a given link
 *
 * link->local_sink is created or destroyed as needed.
 *
 * This does not create remote sinks but will trigger DM
 * to start MST detection if a branch is detected.
 */
741
bool dc_link_detect(struct dc_link *link, enum dc_detect_reason reason)
742 743 744 745 746 747
{
	struct dc_sink_init_data sink_init_data = { 0 };
	struct display_sink_capability sink_caps = { 0 };
	uint8_t i;
	bool converter_disable_audio = false;
	struct audio_support *aud_support = &link->dc->res_pool->audio_support;
748
	bool same_edid = false;
749 750
	enum dc_edid_status edid_status;
	struct dc_context *dc_ctx = link->ctx;
751
	struct dc_sink *sink = NULL;
752 753 754
	struct dc_sink *prev_sink = NULL;
	struct dpcd_caps prev_dpcd_caps;
	bool same_dpcd = true;
755
	enum dc_connection_type new_connection_type = dc_connection_none;
756
	DC_LOGGER_INIT(link->ctx->logger);
757 758

	if (dc_is_virtual_signal(link->connector_signal))
759 760
		return false;

761 762 763 764 765
	if ((link->connector_signal == SIGNAL_TYPE_LVDS ||
			link->connector_signal == SIGNAL_TYPE_EDP) &&
			link->local_sink)
		return true;

766
	if (false == dc_link_detect_sink(link, &new_connection_type)) {
767 768 769 770
		BREAK_TO_DEBUGGER();
		return false;
	}

771 772 773 774 775
	prev_sink = link->local_sink;
	if (prev_sink != NULL) {
		dc_sink_retain(prev_sink);
		memcpy(&prev_dpcd_caps, &link->dpcd_caps, sizeof(struct dpcd_caps));
	}
776 777
	link_disconnect_sink(link);

778
	if (new_connection_type != dc_connection_none) {
779
		link->type = new_connection_type;
780
		link->link_state_valid = false;
781 782

		/* From Disconnected-to-Connected. */
783
		switch (link->connector_signal) {
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
		case SIGNAL_TYPE_HDMI_TYPE_A: {
			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
			if (aud_support->hdmi_audio_native)
				sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
			else
				sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
			break;
		}

		case SIGNAL_TYPE_DVI_SINGLE_LINK: {
			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
			sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
			break;
		}

		case SIGNAL_TYPE_DVI_DUAL_LINK: {
			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
			sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
			break;
		}

805 806 807 808 809 810
		case SIGNAL_TYPE_LVDS: {
			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
			sink_caps.signal = SIGNAL_TYPE_LVDS;
			break;
		}

811
		case SIGNAL_TYPE_EDP: {
812
			read_edp_current_link_settings_on_detect(link);
813
			detect_edp_sink_caps(link);
814 815 816 817 818 819 820
			sink_caps.transaction_type =
				DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
			sink_caps.signal = SIGNAL_TYPE_EDP;
			break;
		}

		case SIGNAL_TYPE_DISPLAY_PORT: {
821 822 823 824 825 826 827 828 829
			/* wa HPD high coming too early*/
			if (link->link_enc->features.flags.bits.DP_IS_USB_C == 1) {

				/* if alt mode times out, return false */
				if (wait_for_alt_mode(link) == false) {
					return false;
				}
			}

830
			if (!detect_dp(
831 832 833
				link,
				&sink_caps,
				&converter_disable_audio,
834 835 836
				aud_support, reason)) {
				if (prev_sink != NULL)
					dc_sink_release(prev_sink);
837
				return false;
838
			}
839

840 841 842 843 844
			// Check if dpcp block is the same
			if (prev_sink != NULL) {
				if (memcmp(&link->dpcd_caps, &prev_dpcd_caps, sizeof(struct dpcd_caps)))
					same_dpcd = false;
			}
845
			/* Active dongle plug in without display or downstream unplug*/
846 847
			if (link->type == dc_connection_active_dongle &&
				link->dpcd_caps.sink_count.bits.SINK_COUNT == 0) {
848 849
				if (prev_sink != NULL) {
					/* Downstream unplug */
850
					dc_sink_release(prev_sink);
851 852
				} else {
					/* Empty dongle plug in */
853 854 855
					dp_verify_link_cap_with_retries(link,
							&link->reported_link_cap,
							LINK_TRAINING_MAX_VERIFY_RETRY);
856
				}
857
				return true;
858
			}
859

860
			if (link->type == dc_connection_mst_branch) {
861
				LINK_INFO("link=%d, mst branch is now Connected\n",
862
					link->link_index);
863 864 865
				/* Need to setup mst link_cap struct here
				 * otherwise dc_link_detect() will leave mst link_cap
				 * empty which leads to allocate_mst_payload() has "0"
866
				 * pbn_per_slot value leading to exception on dc_fixpt_div()
867 868
				 */
				link->verified_link_cap = link->reported_link_cap;
869 870
				if (prev_sink != NULL)
					dc_sink_release(prev_sink);
871 872 873 874 875 876 877 878
				return false;
			}

			break;
		}

		default:
			DC_ERROR("Invalid connector type! signal:%d\n",
879
				link->connector_signal);
880 881
			if (prev_sink != NULL)
				dc_sink_release(prev_sink);
882 883 884 885 886 887
			return false;
		} /* switch() */

		if (link->dpcd_caps.sink_count.bits.SINK_COUNT)
			link->dpcd_sink_count = link->dpcd_caps.sink_count.
					bits.SINK_COUNT;
888 889
		else
			link->dpcd_sink_count = 1;
890 891

		dal_ddc_service_set_transaction_type(
892
						link->ddc,
893 894
						sink_caps.transaction_type);

895 896
		link->aux_mode = dal_ddc_service_is_in_aux_transaction_mode(
				link->ddc);
897

898
		sink_init_data.link = link;
899 900
		sink_init_data.sink_signal = sink_caps.signal;

901 902 903
		sink = dc_sink_create(&sink_init_data);
		if (!sink) {
			DC_ERROR("Failed to create sink!\n");
904 905
			if (prev_sink != NULL)
				dc_sink_release(prev_sink);
906 907
			return false;
		}
908

909
		sink->link->dongle_max_pix_clk = sink_caps.max_hdmi_pixel_clock;
910
		sink->converter_disable_audio = converter_disable_audio;
911

912
		/* dc_sink_create returns a new reference */
913
		link->local_sink = sink;
914

915
		edid_status = dm_helpers_read_local_edid(
916
				link->ctx,
917
				link,
918
				sink);
919 920 921

		switch (edid_status) {
		case EDID_BAD_CHECKSUM:
922
			DC_LOG_ERROR("EDID checksum invalid.\n");
923 924
			break;
		case EDID_NO_RESPONSE:
925
			DC_LOG_ERROR("No EDID read.\n");
926 927 928 929 930 931 932 933 934

			/*
			 * Abort detection for non-DP connectors if we have
			 * no EDID
			 *
			 * DP needs to report as connected if HDP is high
			 * even if we have no EDID in order to go to
			 * fail-safe mode
			 */
935
			if (dc_is_hdmi_signal(link->connector_signal) ||
936 937 938 939
			    dc_is_dvi_signal(link->connector_signal)) {
				if (prev_sink != NULL)
					dc_sink_release(prev_sink);

940
				return false;
941
			}
942 943 944 945
		default:
			break;
		}

946 947 948 949
		// Check if edid is the same
		if ((prev_sink != NULL) && ((edid_status == EDID_THE_SAME) || (edid_status == EDID_OK)))
			same_edid = is_same_edid(&prev_sink->dc_edid, &sink->dc_edid);

950 951 952 953 954 955 956
		if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
			sink_caps.transaction_type == DDC_TRANSACTION_TYPE_I2C_OVER_AUX &&
			reason != DETECT_REASON_HPDRX) {
			/*
			 * TODO debug why Dell 2413 doesn't like
			 *  two link trainings
			 */
957

958
			/* deal with non-mst cases */
959 960 961
			dp_verify_link_cap_with_retries(link,
					&link->reported_link_cap,
					LINK_TRAINING_MAX_VERIFY_RETRY);
962 963 964 965 966 967 968 969
		} else {
			// If edid is the same, then discard new sink and revert back to original sink
			if (same_edid) {
				link_disconnect_remap(prev_sink, link);
				sink = prev_sink;
				prev_sink = NULL;

			}
970
		}
971

972 973 974 975 976
		/* HDMI-DVI Dongle */
		if (sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A &&
				!sink->edid_caps.edid_hdmi)
			sink->sink_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;

977
		/* Connectivity log: detection */
978
		for (i = 0; i < sink->dc_edid.length / DC_EDID_BLOCK_SIZE; i++) {
979
			CONN_DATA_DETECT(link,
980 981
					&sink->dc_edid.raw_edid[i * DC_EDID_BLOCK_SIZE],
					DC_EDID_BLOCK_SIZE,
982
					"%s: [Block %d] ", sink->edid_caps.display_name, i);
983 984
		}

985
		DC_LOG_DETECTION_EDID_PARSER("%s: "
986 987 988 989 990 991 992 993 994
			"manufacturer_id = %X, "
			"product_id = %X, "
			"serial_number = %X, "
			"manufacture_week = %d, "
			"manufacture_year = %d, "
			"display_name = %s, "
			"speaker_flag = %d, "
			"audio_mode_count = %d\n",
			__func__,
995 996 997 998 999 1000 1001 1002 1003 1004
			sink->edid_caps.manufacturer_id,
			sink->edid_caps.product_id,
			sink->edid_caps.serial_number,
			sink->edid_caps.manufacture_week,
			sink->edid_caps.manufacture_year,
			sink->edid_caps.display_name,
			sink->edid_caps.speaker_flags,
			sink->edid_caps.audio_mode_count);

		for (i = 0; i < sink->edid_caps.audio_mode_count; i++) {
1005
			DC_LOG_DETECTION_EDID_PARSER("%s: mode number = %d, "
1006 1007 1008 1009 1010 1011
				"format_code = %d, "
				"channel_count = %d, "
				"sample_rate = %d, "
				"sample_size = %d\n",
				__func__,
				i,
1012 1013 1014 1015
				sink->edid_caps.audio_modes[i].format_code,
				sink->edid_caps.audio_modes[i].channel_count,
				sink->edid_caps.audio_modes[i].sample_rate,
				sink->edid_caps.audio_modes[i].sample_size);
1016 1017 1018 1019
		}

	} else {
		/* From Connected-to-Disconnected. */
1020
		if (link->type == dc_connection_mst_branch) {
1021
			LINK_INFO("link=%d, mst branch is now Disconnected\n",
1022
				link->link_index);
1023

1024
			dm_helpers_dp_mst_stop_top_mgr(link->ctx, link);
1025 1026 1027 1028 1029

			link->mst_stream_alloc_table.stream_count = 0;
			memset(link->mst_stream_alloc_table.stream_allocations, 0, sizeof(link->mst_stream_alloc_table.stream_allocations));
		}

1030 1031
		link->type = dc_connection_none;
		sink_caps.signal = SIGNAL_TYPE_NONE;
1032 1033 1034 1035 1036 1037
		/* When we unplug a passive DP-HDMI dongle connection, dongle_max_pix_clk
		 *  is not cleared. If we emulate a DP signal on this connection, it thinks
		 *  the dongle is still there and limits the number of modes we can emulate.
		 *  Clear dongle_max_pix_clk on disconnect to fix this
		 */
		link->dongle_max_pix_clk = 0;
1038 1039
	}

1040
	LINK_INFO("link=%d, dc_sink_in=%p is now %s prev_sink=%p dpcd same=%d edid same=%d\n",
1041
		link->link_index, sink,
1042
		(sink_caps.signal == SIGNAL_TYPE_NONE ?
1043 1044 1045 1046 1047
			"Disconnected":"Connected"), prev_sink,
			same_dpcd, same_edid);

	if (prev_sink != NULL)
		dc_sink_release(prev_sink);
1048 1049 1050 1051

	return true;
}

1052 1053 1054 1055
bool dc_link_get_hpd_state(struct dc_link *dc_link)
{
	uint32_t state;

1056 1057 1058
	dal_gpio_lock_pin(dc_link->hpd_gpio);
	dal_gpio_get_value(dc_link->hpd_gpio, &state);
	dal_gpio_unlock_pin(dc_link->hpd_gpio);
1059 1060 1061 1062

	return state;
}

1063
static enum hpd_source_id get_hpd_line(
1064
		struct dc_link *link)
1065 1066 1067 1068
{
	struct gpio *hpd;
	enum hpd_source_id hpd_id = HPD_SOURCEID_UNKNOWN;

1069
	hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

	if (hpd) {
		switch (dal_irq_get_source(hpd)) {
		case DC_IRQ_SOURCE_HPD1:
			hpd_id = HPD_SOURCEID1;
		break;
		case DC_IRQ_SOURCE_HPD2:
			hpd_id = HPD_SOURCEID2;
		break;
		case DC_IRQ_SOURCE_HPD3:
			hpd_id = HPD_SOURCEID3;
		break;
		case DC_IRQ_SOURCE_HPD4:
			hpd_id = HPD_SOURCEID4;
		break;
		case DC_IRQ_SOURCE_HPD5:
			hpd_id = HPD_SOURCEID5;
		break;
		case DC_IRQ_SOURCE_HPD6:
			hpd_id = HPD_SOURCEID6;
		break;
		default:
			BREAK_TO_DEBUGGER();
		break;
		}

		dal_gpio_destroy_irq(&hpd);
	}

	return hpd_id;
}

1102
static enum channel_id get_ddc_line(struct dc_link *link)
1103 1104 1105 1106
{
	struct ddc *ddc;
	enum channel_id channel = CHANNEL_ID_UNKNOWN;

1107
	ddc = dal_ddc_service_get_ddc_pin(link->ddc);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

	if (ddc) {
		switch (dal_ddc_get_line(ddc)) {
		case GPIO_DDC_LINE_DDC1:
			channel = CHANNEL_ID_DDC1;
			break;
		case GPIO_DDC_LINE_DDC2:
			channel = CHANNEL_ID_DDC2;
			break;
		case GPIO_DDC_LINE_DDC3:
			channel = CHANNEL_ID_DDC3;
			break;
		case GPIO_DDC_LINE_DDC4:
			channel = CHANNEL_ID_DDC4;
			break;
		case GPIO_DDC_LINE_DDC5:
			channel = CHANNEL_ID_DDC5;
			break;
		case GPIO_DDC_LINE_DDC6:
			channel = CHANNEL_ID_DDC6;
			break;
		case GPIO_DDC_LINE_DDC_VGA:
			channel = CHANNEL_ID_DDC_VGA;
			break;
		case GPIO_DDC_LINE_I2C_PAD:
			channel = CHANNEL_ID_I2C_PAD;
			break;
		default:
			BREAK_TO_DEBUGGER();
			break;
		}
	}

	return channel;
}

static enum transmitter translate_encoder_to_transmitter(
	struct graphics_object_id encoder)
{
	switch (encoder.id) {
	case ENCODER_ID_INTERNAL_UNIPHY:
		switch (encoder.enum_id) {
		case ENUM_ID_1:
			return TRANSMITTER_UNIPHY_A;
		case ENUM_ID_2:
			return TRANSMITTER_UNIPHY_B;
		default:
			return TRANSMITTER_UNKNOWN;
		}
	break;
	case ENCODER_ID_INTERNAL_UNIPHY1:
		switch (encoder.enum_id) {
		case ENUM_ID_1:
			return TRANSMITTER_UNIPHY_C;
		case ENUM_ID_2:
			return TRANSMITTER_UNIPHY_D;
		default:
			return TRANSMITTER_UNKNOWN;
		}
	break;
	case ENCODER_ID_INTERNAL_UNIPHY2:
		switch (encoder.enum_id) {
		case ENUM_ID_1:
			return TRANSMITTER_UNIPHY_E;
		case ENUM_ID_2:
			return TRANSMITTER_UNIPHY_F;
		default:
			return TRANSMITTER_UNKNOWN;
		}
	break;
	case ENCODER_ID_INTERNAL_UNIPHY3:
		switch (encoder.enum_id) {
		case ENUM_ID_1:
			return TRANSMITTER_UNIPHY_G;
		default:
			return TRANSMITTER_UNKNOWN;
		}
	break;
	case ENCODER_ID_EXTERNAL_NUTMEG:
		switch (encoder.enum_id) {
		case ENUM_ID_1:
			return TRANSMITTER_NUTMEG_CRT;
		default:
			return TRANSMITTER_UNKNOWN;
		}
	break;
	case ENCODER_ID_EXTERNAL_TRAVIS:
		switch (encoder.enum_id) {
		case ENUM_ID_1:
			return TRANSMITTER_TRAVIS_CRT;
		case ENUM_ID_2:
			return TRANSMITTER_TRAVIS_LCD;
		default:
			return TRANSMITTER_UNKNOWN;
		}
	break;
	default:
		return TRANSMITTER_UNKNOWN;
	}
}

static bool construct(
1210
	struct dc_link *link,
1211 1212 1213
	const struct link_init_data *init_params)
{
	uint8_t i;
1214
	struct ddc_service_init_data ddc_service_init_data = { { 0 } };
1215 1216 1217 1218 1219
	struct dc_context *dc_ctx = init_params->ctx;
	struct encoder_init_data enc_init_data = { 0 };
	struct integrated_info info = {{{ 0 }}};
	struct dc_bios *bios = init_params->dc->ctx->dc_bios;
	const struct dc_vbios_funcs *bp_funcs = bios->funcs;
1220
	DC_LOGGER_INIT(dc_ctx->logger);
1221

1222 1223
	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
1224 1225 1226 1227 1228

	link->link_status.dpcd_caps = &link->dpcd_caps;

	link->dc = init_params->dc;
	link->ctx = dc_ctx;
1229
	link->link_index = init_params->link_index;
1230

1231 1232 1233
	memset(&link->preferred_training_settings, 0, sizeof(struct dc_link_training_overrides));
	memset(&link->preferred_link_setting, 0, sizeof(struct dc_link_settings));

1234
	link->link_id = bios->funcs->get_connector_id(bios, init_params->connector_index);
1235

1236
	if (link->link_id.type != OBJECT_TYPE_CONNECTOR) {
1237
		dm_output_to_console("%s: Invalid Connector ObjectID from Adapter Service for connector index:%d! type %d expected %d\n",
1238 1239
			 __func__, init_params->connector_index,
			 link->link_id.type, OBJECT_TYPE_CONNECTOR);
1240 1241 1242
		goto create_fail;
	}

1243 1244 1245
	if (link->dc->res_pool->funcs->link_init)
		link->dc->res_pool->funcs->link_init(link);

1246
	link->hpd_gpio = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);
1247 1248 1249
	if (link->hpd_gpio != NULL) {
		dal_gpio_open(link->hpd_gpio, GPIO_MODE_INTERRUPT);
		dal_gpio_unlock_pin(link->hpd_gpio);
1250
		link->irq_source_hpd = dal_irq_get_source(link->hpd_gpio);
1251
	}
1252 1253 1254

	switch (link->link_id.id) {
	case CONNECTOR_ID_HDMI_TYPE_A:
1255
		link->connector_signal = SIGNAL_TYPE_HDMI_TYPE_A;
1256 1257 1258 1259

		break;
	case CONNECTOR_ID_SINGLE_LINK_DVID:
	case CONNECTOR_ID_SINGLE_LINK_DVII:
1260
		link->connector_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1261 1262 1263
		break;
	case CONNECTOR_ID_DUAL_LINK_DVID:
	case CONNECTOR_ID_DUAL_LINK_DVII:
1264
		link->connector_signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1265 1266
		break;
	case CONNECTOR_ID_DISPLAY_PORT:
1267
		link->connector_signal =	SIGNAL_TYPE_DISPLAY_PORT;
1268

1269
		if (link->hpd_gpio != NULL)
1270
			link->irq_source_hpd_rx =
1271
					dal_irq_get_rx_source(link->hpd_gpio);
1272 1273 1274

		break;
	case CONNECTOR_ID_EDP:
1275
		link->connector_signal = SIGNAL_TYPE_EDP;
1276

1277
		if (link->hpd_gpio != NULL) {
1278 1279
			link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
			link->irq_source_hpd_rx =
1280
					dal_irq_get_rx_source(link->hpd_gpio);
1281 1282
		}
		break;
1283 1284 1285
	case CONNECTOR_ID_LVDS:
		link->connector_signal = SIGNAL_TYPE_LVDS;
		break;
1286
	default:
1287
		DC_LOG_WARNING("Unsupported Connector type:%d!\n", link->link_id.id);
1288 1289 1290 1291 1292 1293 1294
		goto create_fail;
	}

	/* TODO: #DAL3 Implement id to str function.*/
	LINK_INFO("Connector[%d] description:"
			"signal %d\n",
			init_params->connector_index,
1295
			link->connector_signal);
1296 1297 1298 1299

	ddc_service_init_data.ctx = link->ctx;
	ddc_service_init_data.id = link->link_id;
	ddc_service_init_data.link = link;
1300
	link->ddc = dal_ddc_service_create(&ddc_service_init_data);
1301

1302
	if (link->ddc == NULL) {
1303 1304 1305 1306
		DC_ERROR("Failed to create ddc_service!\n");
		goto ddc_create_fail;
	}

1307
	link->ddc_hw_inst =
1308
		dal_ddc_get_line(
1309
			dal_ddc_service_get_ddc_pin(link->ddc));
1310 1311 1312 1313 1314 1315

	enc_init_data.ctx = dc_ctx;
	bp_funcs->get_src_obj(dc_ctx->dc_bios, link->link_id, 0, &enc_init_data.encoder);
	enc_init_data.connector = link->link_id;
	enc_init_data.channel = get_ddc_line(link);
	enc_init_data.hpd_source = get_hpd_line(link);
1316

1317
	link->hpd_src = enc_init_data.hpd_source;
1318

1319 1320 1321 1322 1323
	enc_init_data.transmitter =
			translate_encoder_to_transmitter(enc_init_data.encoder);
	link->link_enc = link->dc->res_pool->funcs->link_enc_create(
								&enc_init_data);

1324
	if (link->link_enc == NULL) {
1325 1326 1327 1328
		DC_ERROR("Failed to create link encoder!\n");
		goto link_enc_create_fail;
	}

1329
	link->link_enc_hw_inst = link->link_enc->transmitter;
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343

	for (i = 0; i < 4; i++) {
		if (BP_RESULT_OK !=
				bp_funcs->get_device_tag(dc_ctx->dc_bios, link->link_id, i, &link->device_tag)) {
			DC_ERROR("Failed to find device tag!\n");
			goto device_tag_fail;
		}

		/* Look for device tag that matches connector signal,
		 * CRT for rgb, LCD for other supported signal tyes
		 */
		if (!bp_funcs->is_device_id_supported(dc_ctx->dc_bios, link->device_tag.dev_id))
			continue;
		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_CRT
1344
			&& link->connector_signal != SIGNAL_TYPE_RGB)
1345 1346
			continue;
		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_LCD
1347
			&& link->connector_signal == SIGNAL_TYPE_RGB)
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
			continue;
		break;
	}

	if (bios->integrated_info)
		info = *bios->integrated_info;

	/* Look for channel mapping corresponding to connector and device tag */
	for (i = 0; i < MAX_NUMBER_OF_EXT_DISPLAY_PATH; i++) {
		struct external_display_path *path =
			&info.ext_disp_conn_info.path[i];
		if (path->device_connector_id.enum_id == link->link_id.enum_id
			&& path->device_connector_id.id == link->link_id.id
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
			&& path->device_connector_id.type == link->link_id.type) {

			if (link->device_tag.acpi_device != 0
				&& path->device_acpi_enum == link->device_tag.acpi_device) {
				link->ddi_channel_mapping = path->channel_mapping;
				link->chip_caps = path->caps;
			} else if (path->device_tag ==
					link->device_tag.dev_id.raw_device_tag) {
				link->ddi_channel_mapping = path->channel_mapping;
				link->chip_caps = path->caps;
			}
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
			break;
		}
	}

	/*
	 * TODO check if GPIO programmed correctly
	 *
	 * If GPIO isn't programmed correctly HPD might not rise or drain
	 * fast enough, leading to bounces.
	 */
1382
	program_hpd_filter(link);
1383 1384 1385 1386 1387

	return true;
device_tag_fail:
	link->link_enc->funcs->destroy(&link->link_enc);
link_enc_create_fail:
1388
	dal_ddc_service_destroy(&link->ddc);
1389 1390 1391
ddc_create_fail:
create_fail:

1392 1393 1394
	if (link->hpd_gpio != NULL) {
		dal_gpio_destroy_irq(&link->hpd_gpio);
		link->hpd_gpio = NULL;
1395 1396 1397 1398 1399 1400 1401 1402
	}

	return false;
}

/*******************************************************************************
 * Public functions
 ******************************************************************************/
1403
struct dc_link *link_create(const struct link_init_data *init_params)
1404
{
1405
	struct dc_link *link =
1406
			kzalloc(sizeof(*link), GFP_KERNEL);
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416

	if (NULL == link)
		goto alloc_fail;

	if (false == construct(link, init_params))
		goto construct_fail;

	return link;

construct_fail:
1417
	kfree(link);
1418 1419 1420 1421 1422

alloc_fail:
	return NULL;
}

1423
void link_destroy(struct dc_link **link)
1424 1425
{
	destruct(*link);
1426
	kfree(*link);
1427 1428 1429 1430 1431
	*link = NULL;
}

static void enable_stream_features(struct pipe_ctx *pipe_ctx)
{
1432
	struct dc_stream_state *stream = pipe_ctx->stream;
1433
	struct dc_link *link = stream->link;
1434 1435
	union down_spread_ctrl old_downspread;
	union down_spread_ctrl new_downspread;
1436

1437
	core_link_read_dpcd(link, DP_DOWNSPREAD_CTRL,
1438
			&old_downspread.raw, sizeof(old_downspread));
1439

1440 1441 1442
	new_downspread.raw = old_downspread.raw;

	new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1443
			(stream->ignore_msa_timing_param) ? 1 : 0;
1444

1445 1446 1447 1448
	if (new_downspread.raw != old_downspread.raw) {
		core_link_write_dpcd(link, DP_DOWNSPREAD_CTRL,
			&new_downspread.raw, sizeof(new_downspread));
	}
1449 1450
}

1451 1452 1453
static enum dc_status enable_link_dp(
		struct dc_state *state,
		struct pipe_ctx *pipe_ctx)
1454
{
1455
	struct dc_stream_state *stream = pipe_ctx->stream;
1456 1457
	enum dc_status status;
	bool skip_video_pattern;
1458
	struct dc_link *link = stream->link;
1459 1460
	struct dc_link_settings link_settings = {0};
	enum dp_panel_mode panel_mode;
1461 1462 1463
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
	bool fec_enable;
#endif
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
	int i;
	bool apply_seamless_boot_optimization = false;

	// check for seamless boot
	for (i = 0; i < state->stream_count; i++) {
		if (state->streams[i]->apply_seamless_boot_optimization) {
			apply_seamless_boot_optimization = true;
			break;
		}
	}
1474 1475 1476 1477

	/* get link settings for video mode timing */
	decide_link_settings(stream, &link_settings);

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP) {
		/* If link settings are different than current and link already enabled
		 * then need to disable before programming to new rate.
		 */
		if (link->link_status.link_active &&
			(link->cur_link_settings.lane_count != link_settings.lane_count ||
			 link->cur_link_settings.link_rate != link_settings.link_rate)) {
			dp_disable_link_phy(link, pipe_ctx->stream->signal);
		}

		/*in case it is not on*/
		link->dc->hwss.edp_power_control(link, true);
		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
1491 1492
	}

1493 1494
	pipe_ctx->stream_res.pix_clk_params.requested_sym_clk =
			link_settings.link_rate * LINK_RATE_REF_FREQ_IN_KHZ;
1495 1496
	if (!apply_seamless_boot_optimization)
		state->clk_mgr->funcs->update_clocks(state->clk_mgr, state, false);
1497 1498 1499 1500 1501 1502 1503

	dp_enable_link_phy(
		link,
		pipe_ctx->stream->signal,
		pipe_ctx->clock_source->id,
		&link_settings);

1504 1505
	if (stream->sink_patches.dppowerup_delay > 0) {
		int delay_dp_power_up_in_ms = stream->sink_patches.dppowerup_delay;
1506 1507 1508 1509

		msleep(delay_dp_power_up_in_ms);
	}

1510
	panel_mode = dp_get_panel_mode(link);
1511
	dp_set_panel_mode(link, panel_mode);
1512 1513 1514 1515 1516 1517

	skip_video_pattern = true;

	if (link_settings.link_rate == LINK_RATE_LOW)
			skip_video_pattern = false;

1518 1519 1520 1521 1522 1523
	if (link->aux_access_disabled) {
		dc_link_dp_perform_link_training_skip_aux(link, &link_settings);

		link->cur_link_settings = link_settings;
		status = DC_OK;
	} else if (perform_link_training_with_retries(
1524 1525 1526 1527
			link,
			&link_settings,
			skip_video_pattern,
			LINK_TRAINING_ATTEMPTS)) {
1528
		link->cur_link_settings = link_settings;
1529 1530 1531
		status = DC_OK;
	}
	else
1532
		status = DC_FAIL_DP_LINK_TRAINING;
1533

1534
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
1535 1536 1537 1538 1539
	if (link->preferred_training_settings.fec_enable != NULL)
		fec_enable = *link->preferred_training_settings.fec_enable;
	else
		fec_enable = true;

1540
	dp_set_fec_enable(link, fec_enable);
1541
#endif
1542 1543 1544
	return status;
}

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
static enum dc_status enable_link_edp(
		struct dc_state *state,
		struct pipe_ctx *pipe_ctx)
{
	enum dc_status status;

	status = enable_link_dp(state, pipe_ctx);

	return status;
}

1556 1557 1558
static enum dc_status enable_link_dp_mst(
		struct dc_state *state,
		struct pipe_ctx *pipe_ctx)
1559
{
1560
	struct dc_link *link = pipe_ctx->stream->link;
1561 1562 1563 1564

	/* sink signal type after MST branch is MST. Multiple MST sinks
	 * share one link. Link DP PHY is enable or training only once.
	 */
1565
	if (link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN)
1566 1567
		return DC_OK;

1568 1569 1570
	/* clear payload table */
	dm_helpers_dp_mst_clear_payload_allocation_table(link->ctx, link);

1571
	/* to make sure the pending down rep can be processed
1572
	 * before enabling the link
1573 1574 1575
	 */
	dm_helpers_dp_mst_poll_pending_down_reply(link->ctx, link);

D
Ding Wang 已提交
1576 1577 1578
	/* set the sink to MST mode before enabling the link */
	dp_enable_mst_on_sink(link, true);

1579
	return enable_link_dp(state, pipe_ctx);
1580 1581
}

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
static bool get_ext_hdmi_settings(struct pipe_ctx *pipe_ctx,
		enum engine_id eng_id,
		struct ext_hdmi_settings *settings)
{
	bool result = false;
	int i = 0;
	struct integrated_info *integrated_info =
			pipe_ctx->stream->ctx->dc_bios->integrated_info;

	if (integrated_info == NULL)
		return false;

	/*
	 * Get retimer settings from sbios for passing SI eye test for DCE11
	 * The setting values are varied based on board revision and port id
	 * Therefore the setting values of each ports is passed by sbios.
	 */

	// Check if current bios contains ext Hdmi settings
	if (integrated_info->gpu_cap_info & 0x20) {
		switch (eng_id) {
		case ENGINE_ID_DIGA:
			settings->slv_addr = integrated_info->dp0_ext_hdmi_slv_addr;
			settings->reg_num = integrated_info->dp0_ext_hdmi_6g_reg_num;
			settings->reg_num_6g = integrated_info->dp0_ext_hdmi_6g_reg_num;
			memmove(settings->reg_settings,
					integrated_info->dp0_ext_hdmi_reg_settings,
					sizeof(integrated_info->dp0_ext_hdmi_reg_settings));
			memmove(settings->reg_settings_6g,
					integrated_info->dp0_ext_hdmi_6g_reg_settings,
					sizeof(integrated_info->dp0_ext_hdmi_6g_reg_settings));
			result = true;
			break;
		case ENGINE_ID_DIGB:
			settings->slv_addr = integrated_info->dp1_ext_hdmi_slv_addr;
			settings->reg_num = integrated_info->dp1_ext_hdmi_6g_reg_num;
			settings->reg_num_6g = integrated_info->dp1_ext_hdmi_6g_reg_num;
			memmove(settings->reg_settings,
					integrated_info->dp1_ext_hdmi_reg_settings,
					sizeof(integrated_info->dp1_ext_hdmi_reg_settings));
			memmove(settings->reg_settings_6g,
					integrated_info->dp1_ext_hdmi_6g_reg_settings,
					sizeof(integrated_info->dp1_ext_hdmi_6g_reg_settings));
			result = true;
			break;
		case ENGINE_ID_DIGC:
			settings->slv_addr = integrated_info->dp2_ext_hdmi_slv_addr;
			settings->reg_num = integrated_info->dp2_ext_hdmi_6g_reg_num;
			settings->reg_num_6g = integrated_info->dp2_ext_hdmi_6g_reg_num;
			memmove(settings->reg_settings,
					integrated_info->dp2_ext_hdmi_reg_settings,
					sizeof(integrated_info->dp2_ext_hdmi_reg_settings));
			memmove(settings->reg_settings_6g,
					integrated_info->dp2_ext_hdmi_6g_reg_settings,
					sizeof(integrated_info->dp2_ext_hdmi_6g_reg_settings));
			result = true;
			break;
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
		case ENGINE_ID_DIGD:
			settings->slv_addr = integrated_info->dp3_ext_hdmi_slv_addr;
			settings->reg_num = integrated_info->dp3_ext_hdmi_6g_reg_num;
			settings->reg_num_6g = integrated_info->dp3_ext_hdmi_6g_reg_num;
			memmove(settings->reg_settings,
					integrated_info->dp3_ext_hdmi_reg_settings,
					sizeof(integrated_info->dp3_ext_hdmi_reg_settings));
			memmove(settings->reg_settings_6g,
					integrated_info->dp3_ext_hdmi_6g_reg_settings,
					sizeof(integrated_info->dp3_ext_hdmi_6g_reg_settings));
			result = true;
			break;
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
		default:
			break;
		}

		if (result == true) {
			// Validate settings from bios integrated info table
			if (settings->slv_addr == 0)
				return false;
			if (settings->reg_num > 9)
				return false;
			if (settings->reg_num_6g > 3)
				return false;

			for (i = 0; i < settings->reg_num; i++) {
				if (settings->reg_settings[i].i2c_reg_index > 0x20)
					return false;
			}

			for (i = 0; i < settings->reg_num_6g; i++) {
				if (settings->reg_settings_6g[i].i2c_reg_index > 0x20)
					return false;
			}
		}
	}

	return result;
}

static bool i2c_write(struct pipe_ctx *pipe_ctx,
		uint8_t address, uint8_t *buffer, uint32_t length)
{
	struct i2c_command cmd = {0};
	struct i2c_payload payload = {0};

	memset(&payload, 0, sizeof(payload));
	memset(&cmd, 0, sizeof(cmd));

	cmd.number_of_payloads = 1;
	cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
	cmd.speed = pipe_ctx->stream->ctx->dc->caps.i2c_speed_in_khz;

	payload.address = address;
	payload.data = buffer;
	payload.length = length;
	payload.write = true;
	cmd.payloads = &payload;

1698
	if (dm_helpers_submit_i2c(pipe_ctx->stream->ctx,
1699
			pipe_ctx->stream->link, &cmd))
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
		return true;

	return false;
}

static void write_i2c_retimer_setting(
		struct pipe_ctx *pipe_ctx,
		bool is_vga_mode,
		bool is_over_340mhz,
		struct ext_hdmi_settings *settings)
{
	uint8_t slave_address = (settings->slv_addr >> 1);
	uint8_t buffer[2];
	const uint8_t apply_rx_tx_change = 0x4;
	uint8_t offset = 0xA;
	uint8_t value = 0;
	int i = 0;
	bool i2c_success = false;
1718
	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731

	memset(&buffer, 0, sizeof(buffer));

	/* Start Ext-Hdmi programming*/

	for (i = 0; i < settings->reg_num; i++) {
		/* Apply 3G settings */
		if (settings->reg_settings[i].i2c_reg_index <= 0x20) {

			buffer[0] = settings->reg_settings[i].i2c_reg_index;
			buffer[1] = settings->reg_settings[i].i2c_reg_val;
			i2c_success = i2c_write(pipe_ctx, slave_address,
						buffer, sizeof(buffer));
1732 1733 1734
			RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
				offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
				slave_address, buffer[0], buffer[1], i2c_success?1:0);
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752

			if (!i2c_success)
				/* Write failure */
				ASSERT(i2c_success);

			/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
			 * needs to be set to 1 on every 0xA-0xC write.
			 */
			if (settings->reg_settings[i].i2c_reg_index == 0xA ||
				settings->reg_settings[i].i2c_reg_index == 0xB ||
				settings->reg_settings[i].i2c_reg_index == 0xC) {

				/* Query current value from offset 0xA */
				if (settings->reg_settings[i].i2c_reg_index == 0xA)
					value = settings->reg_settings[i].i2c_reg_val;
				else {
					i2c_success =
						dal_ddc_service_query_ddc_data(
1753
						pipe_ctx->stream->link->ddc,
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
						slave_address, &offset, 1, &value, 1);
					if (!i2c_success)
						/* Write failure */
						ASSERT(i2c_success);
				}

				buffer[0] = offset;
				/* Set APPLY_RX_TX_CHANGE bit to 1 */
				buffer[1] = value | apply_rx_tx_change;
				i2c_success = i2c_write(pipe_ctx, slave_address,
						buffer, sizeof(buffer));
1765 1766 1767
				RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
					slave_address, buffer[0], buffer[1], i2c_success?1:0);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
				if (!i2c_success)
					/* Write failure */
					ASSERT(i2c_success);
			}
		}
	}

	/* Apply 3G settings */
	if (is_over_340mhz) {
		for (i = 0; i < settings->reg_num_6g; i++) {
			/* Apply 3G settings */
			if (settings->reg_settings[i].i2c_reg_index <= 0x20) {

				buffer[0] = settings->reg_settings_6g[i].i2c_reg_index;
				buffer[1] = settings->reg_settings_6g[i].i2c_reg_val;
				i2c_success = i2c_write(pipe_ctx, slave_address,
							buffer, sizeof(buffer));
1785 1786 1787
				RETIMER_REDRIVER_INFO("above 340Mhz: retimer write to slave_address = 0x%x,\
					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
					slave_address, buffer[0], buffer[1], i2c_success?1:0);
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805

				if (!i2c_success)
					/* Write failure */
					ASSERT(i2c_success);

				/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
				 * needs to be set to 1 on every 0xA-0xC write.
				 */
				if (settings->reg_settings_6g[i].i2c_reg_index == 0xA ||
					settings->reg_settings_6g[i].i2c_reg_index == 0xB ||
					settings->reg_settings_6g[i].i2c_reg_index == 0xC) {

					/* Query current value from offset 0xA */
					if (settings->reg_settings_6g[i].i2c_reg_index == 0xA)
						value = settings->reg_settings_6g[i].i2c_reg_val;
					else {
						i2c_success =
								dal_ddc_service_query_ddc_data(
1806
								pipe_ctx->stream->link->ddc,
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
								slave_address, &offset, 1, &value, 1);
						if (!i2c_success)
							/* Write failure */
							ASSERT(i2c_success);
					}

					buffer[0] = offset;
					/* Set APPLY_RX_TX_CHANGE bit to 1 */
					buffer[1] = value | apply_rx_tx_change;
					i2c_success = i2c_write(pipe_ctx, slave_address,
							buffer, sizeof(buffer));
1818 1819 1820
					RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
						offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
						slave_address, buffer[0], buffer[1], i2c_success?1:0);
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
					if (!i2c_success)
						/* Write failure */
						ASSERT(i2c_success);
				}
			}
		}
	}

	if (is_vga_mode) {
		/* Program additional settings if using 640x480 resolution */

		/* Write offset 0xFF to 0x01 */
		buffer[0] = 0xff;
		buffer[1] = 0x01;
		i2c_success = i2c_write(pipe_ctx, slave_address,
				buffer, sizeof(buffer));
1837 1838 1839
		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
				offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
				slave_address, buffer[0], buffer[1], i2c_success?1:0);
1840 1841 1842 1843 1844 1845 1846 1847 1848
		if (!i2c_success)
			/* Write failure */
			ASSERT(i2c_success);

		/* Write offset 0x00 to 0x23 */
		buffer[0] = 0x00;
		buffer[1] = 0x23;
		i2c_success = i2c_write(pipe_ctx, slave_address,
				buffer, sizeof(buffer));
1849
		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1850
			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1851
			slave_address, buffer[0], buffer[1], i2c_success?1:0);
1852 1853 1854 1855 1856 1857 1858 1859 1860
		if (!i2c_success)
			/* Write failure */
			ASSERT(i2c_success);

		/* Write offset 0xff to 0x00 */
		buffer[0] = 0xff;
		buffer[1] = 0x00;
		i2c_success = i2c_write(pipe_ctx, slave_address,
				buffer, sizeof(buffer));
1861
		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1862
			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1863
			slave_address, buffer[0], buffer[1], i2c_success?1:0);
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
		if (!i2c_success)
			/* Write failure */
			ASSERT(i2c_success);

	}
}

static void write_i2c_default_retimer_setting(
		struct pipe_ctx *pipe_ctx,
		bool is_vga_mode,
		bool is_over_340mhz)
{
	uint8_t slave_address = (0xBA >> 1);
	uint8_t buffer[2];
	bool i2c_success = false;
1879
	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1880 1881 1882 1883 1884 1885 1886 1887 1888

	memset(&buffer, 0, sizeof(buffer));

	/* Program Slave Address for tuning single integrity */
	/* Write offset 0x0A to 0x13 */
	buffer[0] = 0x0A;
	buffer[1] = 0x13;
	i2c_success = i2c_write(pipe_ctx, slave_address,
			buffer, sizeof(buffer));
1889 1890 1891
	RETIMER_REDRIVER_INFO("retimer writes default setting to slave_address = 0x%x,\
		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
		slave_address, buffer[0], buffer[1], i2c_success?1:0);
1892 1893 1894 1895 1896 1897 1898 1899 1900
	if (!i2c_success)
		/* Write failure */
		ASSERT(i2c_success);

	/* Write offset 0x0A to 0x17 */
	buffer[0] = 0x0A;
	buffer[1] = 0x17;
	i2c_success = i2c_write(pipe_ctx, slave_address,
			buffer, sizeof(buffer));
1901 1902 1903
	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
		slave_address, buffer[0], buffer[1], i2c_success?1:0);
1904 1905 1906 1907 1908 1909 1910 1911 1912
	if (!i2c_success)
		/* Write failure */
		ASSERT(i2c_success);

	/* Write offset 0x0B to 0xDA or 0xD8 */
	buffer[0] = 0x0B;
	buffer[1] = is_over_340mhz ? 0xDA : 0xD8;
	i2c_success = i2c_write(pipe_ctx, slave_address,
			buffer, sizeof(buffer));
1913 1914 1915
	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
		slave_address, buffer[0], buffer[1], i2c_success?1:0);
1916 1917 1918 1919 1920 1921 1922 1923 1924
	if (!i2c_success)
		/* Write failure */
		ASSERT(i2c_success);

	/* Write offset 0x0A to 0x17 */
	buffer[0] = 0x0A;
	buffer[1] = 0x17;
	i2c_success = i2c_write(pipe_ctx, slave_address,
			buffer, sizeof(buffer));
1925 1926 1927
	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
		offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
		slave_address, buffer[0], buffer[1], i2c_success?1:0);
1928 1929 1930 1931 1932 1933 1934 1935 1936
	if (!i2c_success)
		/* Write failure */
		ASSERT(i2c_success);

	/* Write offset 0x0C to 0x1D or 0x91 */
	buffer[0] = 0x0C;
	buffer[1] = is_over_340mhz ? 0x1D : 0x91;
	i2c_success = i2c_write(pipe_ctx, slave_address,
			buffer, sizeof(buffer));
1937 1938 1939
	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
		slave_address, buffer[0], buffer[1], i2c_success?1:0);
1940 1941 1942 1943 1944 1945 1946 1947 1948
	if (!i2c_success)
		/* Write failure */
		ASSERT(i2c_success);

	/* Write offset 0x0A to 0x17 */
	buffer[0] = 0x0A;
	buffer[1] = 0x17;
	i2c_success = i2c_write(pipe_ctx, slave_address,
			buffer, sizeof(buffer));
1949 1950 1951
	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
		slave_address, buffer[0], buffer[1], i2c_success?1:0);
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
	if (!i2c_success)
		/* Write failure */
		ASSERT(i2c_success);


	if (is_vga_mode) {
		/* Program additional settings if using 640x480 resolution */

		/* Write offset 0xFF to 0x01 */
		buffer[0] = 0xff;
		buffer[1] = 0x01;
		i2c_success = i2c_write(pipe_ctx, slave_address,
				buffer, sizeof(buffer));
1965 1966 1967
		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
			slave_address, buffer[0], buffer[1], i2c_success?1:0);
1968 1969 1970 1971 1972 1973 1974 1975 1976
		if (!i2c_success)
			/* Write failure */
			ASSERT(i2c_success);

		/* Write offset 0x00 to 0x23 */
		buffer[0] = 0x00;
		buffer[1] = 0x23;
		i2c_success = i2c_write(pipe_ctx, slave_address,
				buffer, sizeof(buffer));
1977 1978 1979
		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
			offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
			slave_address, buffer[0], buffer[1], i2c_success?1:0);
1980 1981 1982 1983 1984 1985 1986 1987 1988
		if (!i2c_success)
			/* Write failure */
			ASSERT(i2c_success);

		/* Write offset 0xff to 0x00 */
		buffer[0] = 0xff;
		buffer[1] = 0x00;
		i2c_success = i2c_write(pipe_ctx, slave_address,
				buffer, sizeof(buffer));
1989 1990 1991
		RETIMER_REDRIVER_INFO("retimer write default setting to slave_addr = 0x%x,\
			offset = 0x%x, reg_val= 0x%x, i2c_success = %d end here\n",
			slave_address, buffer[0], buffer[1], i2c_success?1:0);
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
		if (!i2c_success)
			/* Write failure */
			ASSERT(i2c_success);
	}
}

static void write_i2c_redriver_setting(
		struct pipe_ctx *pipe_ctx,
		bool is_over_340mhz)
{
	uint8_t slave_address = (0xF0 >> 1);
	uint8_t buffer[16];
	bool i2c_success = false;
2005
	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016

	memset(&buffer, 0, sizeof(buffer));

	// Program Slave Address for tuning single integrity
	buffer[3] = 0x4E;
	buffer[4] = 0x4E;
	buffer[5] = 0x4E;
	buffer[6] = is_over_340mhz ? 0x4E : 0x4A;

	i2c_success = i2c_write(pipe_ctx, slave_address,
					buffer, sizeof(buffer));
2017 2018 2019 2020 2021
	RETIMER_REDRIVER_INFO("redriver write 0 to all 16 reg offset expect following:\n\
		\t slave_addr = 0x%x, offset[3] = 0x%x, offset[4] = 0x%x,\
		offset[5] = 0x%x,offset[6] is_over_340mhz = 0x%x,\
		i2c_success = %d\n",
		slave_address, buffer[3], buffer[4], buffer[5], buffer[6], i2c_success?1:0);
2022 2023 2024 2025 2026 2027

	if (!i2c_success)
		/* Write failure */
		ASSERT(i2c_success);
}

2028 2029
static void enable_link_hdmi(struct pipe_ctx *pipe_ctx)
{
2030
	struct dc_stream_state *stream = pipe_ctx->stream;
2031
	struct dc_link *link = stream->link;
2032
	enum dc_color_depth display_color_depth;
2033 2034 2035 2036 2037 2038
	enum engine_id eng_id;
	struct ext_hdmi_settings settings = {0};
	bool is_over_340mhz = false;
	bool is_vga_mode = (stream->timing.h_addressable == 640)
			&& (stream->timing.v_addressable == 480);

2039
	if (stream->phy_pix_clk == 0)
2040
		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2041 2042 2043 2044
	if (stream->phy_pix_clk > 340000)
		is_over_340mhz = true;

	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
2045
		unsigned short masked_chip_caps = pipe_ctx->stream->link->chip_caps &
2046
				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
2047
		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
			/* DP159, Retimer settings */
			eng_id = pipe_ctx->stream_res.stream_enc->id;

			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings)) {
				write_i2c_retimer_setting(pipe_ctx,
						is_vga_mode, is_over_340mhz, &settings);
			} else {
				write_i2c_default_retimer_setting(pipe_ctx,
						is_vga_mode, is_over_340mhz);
			}
2058
		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
2059 2060 2061 2062
			/* PI3EQX1204, Redriver settings */
			write_i2c_redriver_setting(pipe_ctx, is_over_340mhz);
		}
	}
2063 2064 2065

	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
		dal_ddc_service_write_scdc_data(
2066
			stream->link->ddc,
2067
			stream->phy_pix_clk,
2068
			stream->timing.flags.LTE_340MCSC_SCRAMBLE);
2069

2070
	memset(&stream->link->cur_link_settings, 0,
2071 2072
			sizeof(struct dc_link_settings));

2073 2074
	display_color_depth = stream->timing.display_color_depth;
	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
2075 2076
		display_color_depth = COLOR_DEPTH_888;

2077 2078 2079
	link->link_enc->funcs->enable_tmds_output(
			link->link_enc,
			pipe_ctx->clock_source->id,
2080
			display_color_depth,
2081
			pipe_ctx->stream->signal,
2082 2083
			stream->phy_pix_clk);

2084
	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2085
		dal_ddc_service_read_scdc_data(link->ddc);
2086 2087
}

2088 2089 2090
static void enable_link_lvds(struct pipe_ctx *pipe_ctx)
{
	struct dc_stream_state *stream = pipe_ctx->stream;
2091
	struct dc_link *link = stream->link;
2092 2093

	if (stream->phy_pix_clk == 0)
2094
		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2095

2096
	memset(&stream->link->cur_link_settings, 0,
2097 2098 2099 2100 2101 2102 2103 2104 2105
			sizeof(struct dc_link_settings));

	link->link_enc->funcs->enable_lvds_output(
			link->link_enc,
			pipe_ctx->clock_source->id,
			stream->phy_pix_clk);

}

2106
/****************************enable_link***********************************/
2107 2108 2109
static enum dc_status enable_link(
		struct dc_state *state,
		struct pipe_ctx *pipe_ctx)
2110 2111 2112 2113
{
	enum dc_status status = DC_ERROR_UNEXPECTED;
	switch (pipe_ctx->stream->signal) {
	case SIGNAL_TYPE_DISPLAY_PORT:
2114
		status = enable_link_dp(state, pipe_ctx);
2115
		break;
2116 2117 2118
	case SIGNAL_TYPE_EDP:
		status = enable_link_edp(state, pipe_ctx);
		break;
2119
	case SIGNAL_TYPE_DISPLAY_PORT_MST:
2120
		status = enable_link_dp_mst(state, pipe_ctx);
2121 2122 2123 2124 2125 2126 2127 2128
		msleep(200);
		break;
	case SIGNAL_TYPE_DVI_SINGLE_LINK:
	case SIGNAL_TYPE_DVI_DUAL_LINK:
	case SIGNAL_TYPE_HDMI_TYPE_A:
		enable_link_hdmi(pipe_ctx);
		status = DC_OK;
		break;
2129 2130 2131 2132
	case SIGNAL_TYPE_LVDS:
		enable_link_lvds(pipe_ctx);
		status = DC_OK;
		break;
2133 2134 2135 2136 2137 2138 2139
	case SIGNAL_TYPE_VIRTUAL:
		status = DC_OK;
		break;
	default:
		break;
	}

2140 2141 2142
	if (status == DC_OK)
		pipe_ctx->stream->link->link_status.link_active = true;

2143 2144 2145
	return status;
}

2146
static void disable_link(struct dc_link *link, enum signal_type signal)
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
{
	/*
	 * TODO: implement call for dp_set_hw_test_pattern
	 * it is needed for compliance testing
	 */

	/* here we need to specify that encoder output settings
	 * need to be calculated as for the set mode,
	 * it will lead to querying dynamic link capabilities
	 * which should be done before enable output */

	if (dc_is_dp_signal(signal)) {
		/* SST DP, eDP */
		if (dc_is_dp_sst_signal(signal))
			dp_disable_link_phy(link, signal);
		else
			dp_disable_link_phy_mst(link, signal);
2164 2165 2166 2167 2168 2169 2170 2171
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT

		if (dc_is_dp_sst_signal(signal) ||
				link->mst_stream_alloc_table.stream_count == 0) {
			dp_set_fec_enable(link, false);
			dp_set_fec_ready(link, false);
		}
#endif
2172
	} else
2173
		link->link_enc->funcs->disable_output(link->link_enc, signal);
2174 2175 2176 2177 2178 2179 2180 2181

	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
		/* MST disable link only when no stream use the link */
		if (link->mst_stream_alloc_table.stream_count <= 0)
			link->link_status.link_active = false;
	} else {
		link->link_status.link_active = false;
	}
2182 2183
}

2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
static uint32_t get_timing_pixel_clock_100hz(const struct dc_crtc_timing *timing)
{

	uint32_t pxl_clk = timing->pix_clk_100hz;

	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
		pxl_clk /= 2;
	else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
		pxl_clk = pxl_clk * 2 / 3;

	if (timing->display_color_depth == COLOR_DEPTH_101010)
		pxl_clk = pxl_clk * 10 / 8;
	else if (timing->display_color_depth == COLOR_DEPTH_121212)
		pxl_clk = pxl_clk * 12 / 8;

	return pxl_clk;
}

2202
static bool dp_active_dongle_validate_timing(
2203
		const struct dc_crtc_timing *timing,
2204
		const struct dpcd_caps *dpcd_caps)
2205
{
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
	const struct dc_dongle_caps *dongle_caps = &dpcd_caps->dongle_caps;

	switch (dpcd_caps->dongle_type) {
	case DISPLAY_DONGLE_DP_VGA_CONVERTER:
	case DISPLAY_DONGLE_DP_DVI_CONVERTER:
	case DISPLAY_DONGLE_DP_DVI_DONGLE:
		if (timing->pixel_encoding == PIXEL_ENCODING_RGB)
			return true;
		else
			return false;
	default:
		break;
	}
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261

	if (dongle_caps->dongle_type != DISPLAY_DONGLE_DP_HDMI_CONVERTER ||
		dongle_caps->extendedCapValid == false)
		return true;

	/* Check Pixel Encoding */
	switch (timing->pixel_encoding) {
	case PIXEL_ENCODING_RGB:
	case PIXEL_ENCODING_YCBCR444:
		break;
	case PIXEL_ENCODING_YCBCR422:
		if (!dongle_caps->is_dp_hdmi_ycbcr422_pass_through)
			return false;
		break;
	case PIXEL_ENCODING_YCBCR420:
		if (!dongle_caps->is_dp_hdmi_ycbcr420_pass_through)
			return false;
		break;
	default:
		/* Invalid Pixel Encoding*/
		return false;
	}

	switch (timing->display_color_depth) {
	case COLOR_DEPTH_666:
	case COLOR_DEPTH_888:
		/*888 and 666 should always be supported*/
		break;
	case COLOR_DEPTH_101010:
		if (dongle_caps->dp_hdmi_max_bpc < 10)
			return false;
		break;
	case COLOR_DEPTH_121212:
		if (dongle_caps->dp_hdmi_max_bpc < 12)
			return false;
		break;
	case COLOR_DEPTH_141414:
	case COLOR_DEPTH_161616:
	default:
		/* These color depths are currently not supported */
		return false;
	}

2262
	if (get_timing_pixel_clock_100hz(timing) > (dongle_caps->dp_hdmi_max_pixel_clk_in_khz * 10))
2263 2264 2265 2266 2267
		return false;

	return true;
}

2268
enum dc_status dc_link_validate_mode_timing(
2269
		const struct dc_stream_state *stream,
2270
		struct dc_link *link,
2271 2272
		const struct dc_crtc_timing *timing)
{
2273
	uint32_t max_pix_clk = stream->link->dongle_max_pix_clk * 10;
2274
	struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
2275 2276 2277 2278

	/* A hack to avoid failing any modes for EDID override feature on
	 * topology change such as lower quality cable for DP or different dongle
	 */
2279
	if (link->remote_sinks[0])
2280 2281
		return DC_OK;

2282
	/* Passive Dongle */
2283
	if (max_pix_clk != 0 && get_timing_pixel_clock_100hz(timing) > max_pix_clk)
2284 2285 2286
		return DC_EXCEED_DONGLE_CAP;

	/* Active Dongle*/
2287
	if (!dp_active_dongle_validate_timing(timing, dpcd_caps))
2288
		return DC_EXCEED_DONGLE_CAP;
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305

	switch (stream->signal) {
	case SIGNAL_TYPE_EDP:
	case SIGNAL_TYPE_DISPLAY_PORT:
		if (!dp_validate_mode_timing(
				link,
				timing))
			return DC_NO_DP_LINK_BANDWIDTH;
		break;

	default:
		break;
	}

	return DC_OK;
}

2306 2307 2308 2309
int dc_link_get_backlight_level(const struct dc_link *link)
{
	struct abm *abm = link->ctx->dc->res_pool->abm;

2310
	if (abm == NULL || abm->funcs->get_current_backlight == NULL)
2311 2312
		return DC_ERROR_UNEXPECTED;

2313
	return (int) abm->funcs->get_current_backlight(abm);
2314
}
2315

2316 2317
bool dc_link_set_backlight_level(const struct dc_link *link,
		uint32_t backlight_pwm_u16_16,
2318
		uint32_t frame_ramp)
2319
{
2320
	struct dc  *core_dc = link->ctx->dc;
2321
	struct abm *abm = core_dc->res_pool->abm;
2322
	struct dmcu *dmcu = core_dc->res_pool->dmcu;
2323
	unsigned int controller_id = 0;
2324
	bool use_smooth_brightness = true;
2325
	int i;
2326
	DC_LOGGER_INIT(link->ctx->logger);
2327

2328 2329
	if ((dmcu == NULL) ||
		(abm == NULL) ||
2330
		(abm->funcs->set_backlight_level_pwm == NULL))
2331
		return false;
2332

2333 2334
	use_smooth_brightness = dmcu->funcs->is_dmcu_initialized(dmcu);

2335 2336
	DC_LOG_BACKLIGHT("New Backlight level: %d (0x%X)\n",
			backlight_pwm_u16_16, backlight_pwm_u16_16);
2337

2338
	if (dc_is_embedded_signal(link->connector_signal)) {
2339 2340
		for (i = 0; i < MAX_PIPES; i++) {
			if (core_dc->current_state->res_ctx.pipe_ctx[i].stream) {
2341
				if (core_dc->current_state->res_ctx.
2342
						pipe_ctx[i].stream->link
2343
						== link) {
2344 2345 2346 2347
					/* DMCU -1 for all controller id values,
					 * therefore +1 here
					 */
					controller_id =
2348
						core_dc->current_state->
2349
						res_ctx.pipe_ctx[i].stream_res.tg->inst +
2350
						1;
2351 2352 2353 2354 2355 2356 2357

					/* Disable brightness ramping when the display is blanked
					 * as it can hang the DMCU
					 */
					if (core_dc->current_state->res_ctx.pipe_ctx[i].plane_state == NULL)
						frame_ramp = 0;
				}
2358
			}
2359
		}
2360
		abm->funcs->set_backlight_level_pwm(
2361
				abm,
2362
				backlight_pwm_u16_16,
2363
				frame_ramp,
2364 2365
				controller_id,
				use_smooth_brightness);
2366 2367 2368 2369 2370
	}

	return true;
}

2371 2372 2373 2374 2375
bool dc_link_set_abm_disable(const struct dc_link *link)
{
	struct dc  *core_dc = link->ctx->dc;
	struct abm *abm = core_dc->res_pool->abm;

2376
	if ((abm == NULL) || (abm->funcs->set_backlight_level_pwm == NULL))
2377 2378 2379 2380 2381 2382 2383
		return false;

	abm->funcs->set_abm_immediate_disable(abm);

	return true;
}

2384
bool dc_link_set_psr_enable(const struct dc_link *link, bool enable, bool wait)
2385
{
2386
	struct dc  *core_dc = link->ctx->dc;
A
Amy Zhang 已提交
2387 2388
	struct dmcu *dmcu = core_dc->res_pool->dmcu;

2389
	if ((dmcu != NULL && dmcu->funcs->is_dmcu_initialized(dmcu)) && link->psr_enabled)
2390
		dmcu->funcs->set_psr_enable(dmcu, enable, wait);
2391 2392 2393 2394

	return true;
}

2395
const struct dc_link_status *dc_link_get_status(const struct dc_link *link)
2396 2397 2398 2399
{
	return &link->link_status;
}

2400
void core_link_resume(struct dc_link *link)
2401
{
2402
	if (link->connector_signal != SIGNAL_TYPE_VIRTUAL)
2403
		program_hpd_filter(link);
2404 2405
}

2406
static struct fixed31_32 get_pbn_per_slot(struct dc_stream_state *stream)
2407
{
2408
	struct fixed31_32 mbytes_per_sec;
2409 2410
	uint32_t link_rate_in_mbytes_per_sec = dc_link_bandwidth_kbps(stream->link,
			&stream->link->cur_link_settings);
2411 2412 2413 2414 2415
	link_rate_in_mbytes_per_sec /= 8000; /* Kbits to MBytes */

	mbytes_per_sec = dc_fixpt_from_int(link_rate_in_mbytes_per_sec);

	return dc_fixpt_div_int(mbytes_per_sec, 54);
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
}

static int get_color_depth(enum dc_color_depth color_depth)
{
	switch (color_depth) {
	case COLOR_DEPTH_666: return 6;
	case COLOR_DEPTH_888: return 8;
	case COLOR_DEPTH_101010: return 10;
	case COLOR_DEPTH_121212: return 12;
	case COLOR_DEPTH_141414: return 14;
	case COLOR_DEPTH_161616: return 16;
	default: return 0;
	}
}

static struct fixed31_32 get_pbn_from_timing(struct pipe_ctx *pipe_ctx)
{
	uint32_t bpc;
	uint64_t kbps;
	struct fixed31_32 peak_kbps;
	uint32_t numerator;
	uint32_t denominator;

2439
	bpc = get_color_depth(pipe_ctx->stream_res.pix_clk_params.color_depth);
2440
	kbps = dc_bandwidth_in_kbps_from_timing(&pipe_ctx->stream->timing);
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455

	/*
	 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
	 * common multiplier to render an integer PBN for all link rate/lane
	 * counts combinations
	 * calculate
	 * peak_kbps *= (1006/1000)
	 * peak_kbps *= (64/54)
	 * peak_kbps *= 8    convert to bytes
	 */

	numerator = 64 * PEAK_FACTOR_X1000;
	denominator = 54 * 8 * 1000 * 1000;
	kbps *= numerator;
2456
	peak_kbps = dc_fixpt_from_fraction(kbps, denominator);
2457 2458 2459 2460 2461

	return peak_kbps;
}

static void update_mst_stream_alloc_table(
2462
	struct dc_link *link,
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
	struct stream_encoder *stream_enc,
	const struct dp_mst_stream_allocation_table *proposed_table)
{
	struct link_mst_stream_allocation work_table[MAX_CONTROLLER_NUM] = {
			{ 0 } };
	struct link_mst_stream_allocation *dc_alloc;

	int i;
	int j;

	/* if DRM proposed_table has more than one new payload */
	ASSERT(proposed_table->stream_count -
			link->mst_stream_alloc_table.stream_count < 2);

2477
	/* copy proposed_table to link, add stream encoder */
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
	for (i = 0; i < proposed_table->stream_count; i++) {

		for (j = 0; j < link->mst_stream_alloc_table.stream_count; j++) {
			dc_alloc =
			&link->mst_stream_alloc_table.stream_allocations[j];

			if (dc_alloc->vcp_id ==
				proposed_table->stream_allocations[i].vcp_id) {

				work_table[i] = *dc_alloc;
				break; /* exit j loop */
			}
		}

		/* new vcp_id */
		if (j == link->mst_stream_alloc_table.stream_count) {
			work_table[i].vcp_id =
				proposed_table->stream_allocations[i].vcp_id;
			work_table[i].slot_count =
				proposed_table->stream_allocations[i].slot_count;
			work_table[i].stream_enc = stream_enc;
		}
	}

	/* update link->mst_stream_alloc_table with work_table */
	link->mst_stream_alloc_table.stream_count =
			proposed_table->stream_count;
	for (i = 0; i < MAX_CONTROLLER_NUM; i++)
		link->mst_stream_alloc_table.stream_allocations[i] =
				work_table[i];
}

/* convert link_mst_stream_alloc_table to dm dp_mst_stream_alloc_table
 * because stream_encoder is not exposed to dm
 */
static enum dc_status allocate_mst_payload(struct pipe_ctx *pipe_ctx)
{
2515
	struct dc_stream_state *stream = pipe_ctx->stream;
2516
	struct dc_link *link = stream->link;
2517
	struct link_encoder *link_encoder = link->link_enc;
2518
	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
2519 2520 2521 2522 2523
	struct dp_mst_stream_allocation_table proposed_table = {0};
	struct fixed31_32 avg_time_slots_per_mtp;
	struct fixed31_32 pbn;
	struct fixed31_32 pbn_per_slot;
	uint8_t i;
2524
	DC_LOGGER_INIT(link->ctx->logger);
2525 2526 2527 2528 2529 2530 2531 2532 2533

	/* enable_link_dp_mst already check link->enabled_stream_count
	 * and stream is in link->stream[]. This is called during set mode,
	 * stream_enc is available.
	 */

	/* get calculate VC payload for stream: stream_alloc */
	if (dm_helpers_dp_mst_write_payload_allocation_table(
		stream->ctx,
2534
		stream,
2535 2536 2537
		&proposed_table,
		true)) {
		update_mst_stream_alloc_table(
2538
					link, pipe_ctx->stream_res.stream_enc, &proposed_table);
2539 2540
	}
	else
2541
		DC_LOG_WARNING("Failed to update"
2542 2543 2544 2545
				"MST allocation table for"
				"pipe idx:%d\n",
				pipe_ctx->pipe_idx);

2546
	DC_LOG_MST("%s  "
2547 2548 2549 2550 2551
			"stream_count: %d: \n ",
			__func__,
			link->mst_stream_alloc_table.stream_count);

	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
2552
		DC_LOG_MST("stream_enc[%d]: %p      "
2553 2554 2555
		"stream[%d].vcp_id: %d      "
		"stream[%d].slot_count: %d\n",
		i,
2556
		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
		i,
		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
		i,
		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
	}

	ASSERT(proposed_table.stream_count > 0);

	/* program DP source TX for payload */
	link_encoder->funcs->update_mst_stream_allocation_table(
		link_encoder,
		&link->mst_stream_alloc_table);

	/* send down message */
	dm_helpers_dp_mst_poll_for_allocation_change_trigger(
			stream->ctx,
2573
			stream);
2574 2575 2576

	dm_helpers_dp_mst_send_payload_allocation(
			stream->ctx,
2577
			stream,
2578 2579 2580 2581 2582
			true);

	/* slot X.Y for only current stream */
	pbn_per_slot = get_pbn_per_slot(stream);
	pbn = get_pbn_from_timing(pipe_ctx);
2583
	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594

	stream_encoder->funcs->set_mst_bandwidth(
		stream_encoder,
		avg_time_slots_per_mtp);

	return DC_OK;

}

static enum dc_status deallocate_mst_payload(struct pipe_ctx *pipe_ctx)
{
2595
	struct dc_stream_state *stream = pipe_ctx->stream;
2596
	struct dc_link *link = stream->link;
2597
	struct link_encoder *link_encoder = link->link_enc;
2598
	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
2599
	struct dp_mst_stream_allocation_table proposed_table = {0};
2600
	struct fixed31_32 avg_time_slots_per_mtp = dc_fixpt_from_int(0);
2601
	uint8_t i;
2602
	bool mst_mode = (link->type == dc_connection_mst_branch);
2603
	DC_LOGGER_INIT(link->ctx->logger);
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620

	/* deallocate_mst_payload is called before disable link. When mode or
	 * disable/enable monitor, new stream is created which is not in link
	 * stream[] yet. For this, payload is not allocated yet, so de-alloc
	 * should not done. For new mode set, map_resources will get engine
	 * for new stream, so stream_enc->id should be validated until here.
	 */

	/* slot X.Y */
	stream_encoder->funcs->set_mst_bandwidth(
		stream_encoder,
		avg_time_slots_per_mtp);

	/* TODO: which component is responsible for remove payload table? */
	if (mst_mode) {
		if (dm_helpers_dp_mst_write_payload_allocation_table(
				stream->ctx,
2621
				stream,
2622 2623 2624 2625
				&proposed_table,
				false)) {

			update_mst_stream_alloc_table(
2626
				link, pipe_ctx->stream_res.stream_enc, &proposed_table);
2627 2628
		}
		else {
2629
				DC_LOG_WARNING("Failed to update"
2630 2631 2632 2633 2634 2635
						"MST allocation table for"
						"pipe idx:%d\n",
						pipe_ctx->pipe_idx);
		}
	}

2636
	DC_LOG_MST("%s"
2637 2638 2639 2640 2641
			"stream_count: %d: ",
			__func__,
			link->mst_stream_alloc_table.stream_count);

	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
2642
		DC_LOG_MST("stream_enc[%d]: %p      "
2643 2644 2645
		"stream[%d].vcp_id: %d      "
		"stream[%d].slot_count: %d\n",
		i,
2646
		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
		i,
		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
		i,
		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
	}

	link_encoder->funcs->update_mst_stream_allocation_table(
		link_encoder,
		&link->mst_stream_alloc_table);

	if (mst_mode) {
		dm_helpers_dp_mst_poll_for_allocation_change_trigger(
			stream->ctx,
2660
			stream);
2661 2662 2663

		dm_helpers_dp_mst_send_payload_allocation(
			stream->ctx,
2664
			stream,
2665 2666 2667 2668 2669 2670
			false);
	}

	return DC_OK;
}

2671 2672 2673
void core_link_enable_stream(
		struct dc_state *state,
		struct pipe_ctx *pipe_ctx)
2674
{
2675
	struct dc *core_dc = pipe_ctx->stream->ctx->dc;
2676
	struct dc_stream_state *stream = pipe_ctx->stream;
2677
	enum dc_status status;
2678
	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2679

2680
	if (!dc_is_virtual_signal(pipe_ctx->stream->signal)) {
2681 2682
		stream->link->link_enc->funcs->setup(
			stream->link->link_enc,
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
			pipe_ctx->stream->signal);
		pipe_ctx->stream_res.stream_enc->funcs->setup_stereo_sync(
			pipe_ctx->stream_res.stream_enc,
			pipe_ctx->stream_res.tg->inst,
			stream->timing.timing_3d_format != TIMING_3D_FORMAT_NONE);
	}

	if (dc_is_dp_signal(pipe_ctx->stream->signal))
		pipe_ctx->stream_res.stream_enc->funcs->dp_set_stream_attribute(
			pipe_ctx->stream_res.stream_enc,
			&stream->timing,
2694 2695
			stream->output_color_space,
			stream->link->dpcd_caps.dprx_feature.bits.SST_SPLIT_SDP_CAP);
2696

2697
	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal))
2698 2699 2700 2701 2702 2703
		pipe_ctx->stream_res.stream_enc->funcs->hdmi_set_stream_attribute(
			pipe_ctx->stream_res.stream_enc,
			&stream->timing,
			stream->phy_pix_clk,
			pipe_ctx->stream_res.audio != NULL);

2704 2705
	pipe_ctx->stream->link->link_state_valid = true;

2706 2707 2708 2709 2710 2711 2712
	if (dc_is_dvi_signal(pipe_ctx->stream->signal))
		pipe_ctx->stream_res.stream_enc->funcs->dvi_set_stream_attribute(
			pipe_ctx->stream_res.stream_enc,
			&stream->timing,
			(pipe_ctx->stream->signal == SIGNAL_TYPE_DVI_DUAL_LINK) ?
			true : false);

2713 2714 2715 2716 2717
	if (dc_is_lvds_signal(pipe_ctx->stream->signal))
		pipe_ctx->stream_res.stream_enc->funcs->lvds_set_stream_attribute(
			pipe_ctx->stream_res.stream_enc,
			&stream->timing);

2718
	if (!IS_FPGA_MAXIMUS_DC(core_dc->ctx->dce_environment)) {
2719 2720 2721 2722 2723
		bool apply_edp_fast_boot_optimization =
			pipe_ctx->stream->apply_edp_fast_boot_optimization;

		pipe_ctx->stream->apply_edp_fast_boot_optimization = false;

2724 2725 2726
		resource_build_info_frame(pipe_ctx);
		core_dc->hwss.update_info_frame(pipe_ctx);

2727 2728 2729 2730 2731 2732
		/* Do not touch link on seamless boot optimization. */
		if (pipe_ctx->stream->apply_seamless_boot_optimization) {
			pipe_ctx->stream->dpms_off = false;
			return;
		}

2733 2734
		/* eDP lit up by bios already, no need to enable again. */
		if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
2735
					apply_edp_fast_boot_optimization) {
2736 2737 2738
			pipe_ctx->stream->dpms_off = false;
			return;
		}
2739

2740 2741
		if (pipe_ctx->stream->dpms_off)
			return;
2742

2743
		status = enable_link(state, pipe_ctx);
2744

2745
		if (status != DC_OK) {
2746
			DC_LOG_WARNING("enabling link %u failed: %d\n",
2747
			pipe_ctx->stream->link->link_index,
2748 2749 2750 2751
			status);

			/* Abort stream enable *unless* the failure was due to
			 * DP link training - some DP monitors will recover and
2752 2753
			 * show the stream anyway. But MST displays can't proceed
			 * without link training.
2754
			 */
2755 2756
			if (status != DC_FAIL_DP_LINK_TRAINING ||
					pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2757 2758 2759
				BREAK_TO_DEBUGGER();
				return;
			}
2760
		}
2761

2762
		core_dc->hwss.enable_audio_stream(pipe_ctx);
2763

2764 2765 2766 2767 2768
		/* turn off otg test pattern if enable */
		if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
			pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
					CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
					COLOR_DEPTH_UNDEFINED);
2769

2770 2771 2772 2773 2774 2775 2776
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
		if (pipe_ctx->stream->timing.flags.DSC) {
			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
					dc_is_virtual_signal(pipe_ctx->stream->signal))
				dp_set_dsc_enable(pipe_ctx, true);
		}
#endif
2777
		core_dc->hwss.enable_stream(pipe_ctx);
2778

2779
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
2780
		/* Set DPS PPS SDP (AKA "info frames") */
2781 2782 2783
		if (pipe_ctx->stream->timing.flags.DSC) {
			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
					dc_is_virtual_signal(pipe_ctx->stream->signal))
2784
				dp_set_dsc_pps_sdp(pipe_ctx, true);
2785 2786
		}
#endif
2787 2788 2789 2790

		if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
			allocate_mst_payload(pipe_ctx);

2791
		core_dc->hwss.unblank_stream(pipe_ctx,
2792
			&pipe_ctx->stream->link->cur_link_settings);
2793

2794 2795
		if (dc_is_dp_signal(pipe_ctx->stream->signal))
			enable_stream_features(pipe_ctx);
2796
	}
2797 2798 2799 2800 2801 2802 2803 2804
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
	else { // if (IS_FPGA_MAXIMUS_DC(core_dc->ctx->dce_environment))
		if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
				dc_is_virtual_signal(pipe_ctx->stream->signal))
			dp_set_dsc_enable(pipe_ctx, true);

	}
#endif
2805 2806
}

2807
void core_link_disable_stream(struct pipe_ctx *pipe_ctx)
2808
{
2809
	struct dc  *core_dc = pipe_ctx->stream->ctx->dc;
2810
	struct dc_stream_state *stream = pipe_ctx->stream;
2811
	struct dc_link *link = stream->sink->link;
2812

2813 2814
	core_dc->hwss.blank_stream(pipe_ctx);

2815 2816 2817
	if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
		deallocate_mst_payload(pipe_ctx);

2818 2819 2820
	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
		struct ext_hdmi_settings settings = {0};
		enum engine_id eng_id = pipe_ctx->stream_res.stream_enc->id;
2821

2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841
		unsigned short masked_chip_caps = link->chip_caps &
				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
		//Need to inform that sink is going to use legacy HDMI mode.
		dal_ddc_service_write_scdc_data(
			link->ddc,
			165000,//vbios only handles 165Mhz.
			false);
		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
			/* DP159, Retimer settings */
			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings))
				write_i2c_retimer_setting(pipe_ctx,
						false, false, &settings);
			else
				write_i2c_default_retimer_setting(pipe_ctx,
						false, false);
		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
			/* PI3EQX1204, Redriver settings */
			write_i2c_redriver_setting(pipe_ctx, false);
		}
	}
2842
	core_dc->hwss.disable_stream(pipe_ctx);
2843

2844
	disable_link(pipe_ctx->stream->link, pipe_ctx->stream->signal);
2845
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
2846 2847 2848
	if (pipe_ctx->stream->timing.flags.DSC) {
		if (dc_is_dp_signal(pipe_ctx->stream->signal))
			dp_set_dsc_enable(pipe_ctx, false);
2849 2850
	}
#endif
2851 2852
}

2853 2854
void core_link_set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
{
2855
	struct dc  *core_dc = pipe_ctx->stream->ctx->dc;
2856

2857
	if (!dc_is_hdmi_signal(pipe_ctx->stream->signal))
2858 2859 2860 2861 2862
		return;

	core_dc->hwss.set_avmute(pipe_ctx, enable);
}

2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
/**
 *****************************************************************************
 *  Function: dc_link_enable_hpd_filter
 *
 *  @brief
 *     If enable is true, programs HPD filter on associated HPD line using
 *     delay_on_disconnect/delay_on_connect values dependent on
 *     link->connector_signal
 *
 *     If enable is false, programs HPD filter on associated HPD line with no
 *     delays on connect or disconnect
 *
 *  @param [in] link: pointer to the dc link
 *  @param [in] enable: boolean specifying whether to enable hbd
 *****************************************************************************
 */
2879
void dc_link_enable_hpd_filter(struct dc_link *link, bool enable)
2880 2881 2882
{
	struct gpio *hpd;

2883 2884 2885 2886
	if (enable) {
		link->is_hpd_filter_disabled = false;
		program_hpd_filter(link);
	} else {
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
		link->is_hpd_filter_disabled = true;
		/* Obtain HPD handle */
		hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);

		if (!hpd)
			return;

		/* Setup HPD filtering */
		if (dal_gpio_open(hpd, GPIO_MODE_INTERRUPT) == GPIO_RESULT_OK) {
			struct gpio_hpd_config config;

			config.delay_on_connect = 0;
			config.delay_on_disconnect = 0;

			dal_irq_setup_hpd_filter(hpd, &config);

			dal_gpio_close(hpd);
		} else {
			ASSERT_CRITICAL(false);
		}
		/* Release HPD handle */
		dal_gpio_destroy_irq(&hpd);
	}
}

2912 2913 2914 2915 2916 2917
uint32_t dc_bandwidth_in_kbps_from_timing(
	const struct dc_crtc_timing *timing)
{
	uint32_t bits_per_channel = 0;
	uint32_t kbps;

2918 2919 2920 2921 2922 2923 2924 2925
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
	if (timing->flags.DSC) {
		kbps = (timing->pix_clk_100hz * timing->dsc_cfg.bits_per_pixel);
		kbps = kbps / 160 + ((kbps % 160) ? 1 : 0);
		return kbps;
	}
#endif

2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
	switch (timing->display_color_depth) {
	case COLOR_DEPTH_666:
		bits_per_channel = 6;
		break;
	case COLOR_DEPTH_888:
		bits_per_channel = 8;
		break;
	case COLOR_DEPTH_101010:
		bits_per_channel = 10;
		break;
	case COLOR_DEPTH_121212:
		bits_per_channel = 12;
		break;
	case COLOR_DEPTH_141414:
		bits_per_channel = 14;
		break;
	case COLOR_DEPTH_161616:
		bits_per_channel = 16;
		break;
	default:
		break;
	}

	ASSERT(bits_per_channel != 0);

	kbps = timing->pix_clk_100hz / 10;
	kbps *= bits_per_channel;

	if (timing->flags.Y_ONLY != 1) {
		/*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/
		kbps *= 3;
		if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
			kbps /= 2;
		else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
			kbps = kbps * 2 / 3;
	}

	return kbps;

}
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010

void dc_link_set_drive_settings(struct dc *dc,
				struct link_training_settings *lt_settings,
				const struct dc_link *link)
{

	int i;

	for (i = 0; i < dc->link_count; i++) {
		if (dc->links[i] == link)
			break;
	}

	if (i >= dc->link_count)
		ASSERT_CRITICAL(false);

	dc_link_dp_set_drive_settings(dc->links[i], lt_settings);
}

void dc_link_perform_link_training(struct dc *dc,
				   struct dc_link_settings *link_setting,
				   bool skip_video_pattern)
{
	int i;

	for (i = 0; i < dc->link_count; i++)
		dc_link_dp_perform_link_training(
			dc->links[i],
			link_setting,
			skip_video_pattern);
}

void dc_link_set_preferred_link_settings(struct dc *dc,
					 struct dc_link_settings *link_setting,
					 struct dc_link *link)
{
	int i;
	struct pipe_ctx *pipe;
	struct dc_stream_state *link_stream;
	struct dc_link_settings store_settings = *link_setting;

	link->preferred_link_setting = store_settings;

	/* Retrain with preferred link settings only relevant for
	 * DP signal type
3011
	 * Check for non-DP signal or if passive dongle present
3012
	 */
3013 3014
	if (!dc_is_dp_signal(link->connector_signal) ||
		link->dongle_max_pix_clk > 0)
3015 3016 3017 3018 3019
		return;

	for (i = 0; i < MAX_PIPES; i++) {
		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
		if (pipe->stream && pipe->stream->link) {
3020 3021
			if (pipe->stream->link == link) {
				link_stream = pipe->stream;
3022
				break;
3023
			}
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
		}
	}

	/* Stream not found */
	if (i == MAX_PIPES)
		return;

	/* Cannot retrain link if backend is off */
	if (link_stream->dpms_off)
		return;

3035
	decide_link_settings(link_stream, &store_settings);
3036 3037 3038 3039 3040 3041

	if ((store_settings.lane_count != LANE_COUNT_UNKNOWN) &&
		(store_settings.link_rate != LINK_RATE_UNKNOWN))
		dp_retrain_link_dp_test(link, &store_settings, false);
}

3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
void 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)
{
	if (lt_overrides != NULL)
		link->preferred_training_settings = *lt_overrides;
	else
		memset(&link->preferred_training_settings, 0, sizeof(link->preferred_training_settings));

	if (link_setting != NULL) {
		link->preferred_link_setting = *link_setting;
	} else {
		link->preferred_link_setting.lane_count = LANE_COUNT_UNKNOWN;
		link->preferred_link_setting.link_rate = LINK_RATE_UNKNOWN;
	}

	/* Retrain now, or wait until next stream update to apply */
	if (skip_immediate_retrain == false)
		dc_link_set_preferred_link_settings(dc, &link->preferred_link_setting, link);
}

3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
void dc_link_enable_hpd(const struct dc_link *link)
{
	dc_link_dp_enable_hpd(link);
}

void dc_link_disable_hpd(const struct dc_link *link)
{
	dc_link_dp_disable_hpd(link);
}

void dc_link_set_test_pattern(struct dc_link *link,
			      enum dp_test_pattern test_pattern,
			      const struct link_training_settings *p_link_settings,
			      const unsigned char *p_custom_pattern,
			      unsigned int cust_pattern_size)
{
	if (link != NULL)
		dc_link_dp_set_test_pattern(
			link,
			test_pattern,
			p_link_settings,
			p_custom_pattern,
			cust_pattern_size);
}

uint32_t dc_link_bandwidth_kbps(
	const struct dc_link *link,
	const struct dc_link_settings *link_setting)
{
	uint32_t link_bw_kbps =
		link_setting->link_rate * LINK_RATE_REF_FREQ_IN_KHZ; /* bytes per sec */

	link_bw_kbps *= 8;   /* 8 bits per byte*/
	link_bw_kbps *= link_setting->lane_count;

3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
#ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
	if (link->dpcd_caps.fec_cap.bits.FEC_CAPABLE) {
		/* Account for FEC overhead.
		 * We have to do it based on caps,
		 * and not based on FEC being set ready,
		 * because FEC is set ready too late in
		 * the process to correctly be picked up
		 * by mode enumeration.
		 *
		 * There's enough zeros at the end of 'kbps'
		 * that make the below operation 100% precise
		 * for our purposes.
		 * 'long long' makes it work even for HDMI 2.1
		 * max bandwidth (and much, much bigger bandwidths
		 * than that, actually).
		 *
		 * NOTE: Reducing link BW by 3% may not be precise
		 * because it may be a stream BT that increases by 3%, and so
		 * 1/1.03 = 0.970873 factor should have been used instead,
		 * but the difference is minimal and is in a safe direction,
		 * which all works well around potential ambiguity of DP 1.4a spec.
		 */
3122 3123
		link_bw_kbps = mul_u64_u32_shr(BIT_ULL(32) * 970LL / 1000,
					       link_bw_kbps, 32);
3124 3125 3126
	}
#endif

3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138
	return link_bw_kbps;

}

const struct dc_link_settings *dc_link_get_link_cap(
		const struct dc_link *link)
{
	if (link->preferred_link_setting.lane_count != LANE_COUNT_UNKNOWN &&
			link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN)
		return &link->preferred_link_setting;
	return &link->verified_link_cap;
}