14562236bSHarry Wentland /*
24562236bSHarry Wentland * Copyright 2012-15 Advanced Micro Devices, Inc.
34562236bSHarry Wentland  *
44562236bSHarry Wentland  * Permission is hereby granted, free of charge, to any person obtaining a
54562236bSHarry Wentland  * copy of this software and associated documentation files (the "Software"),
64562236bSHarry Wentland  * to deal in the Software without restriction, including without limitation
74562236bSHarry Wentland  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84562236bSHarry Wentland  * and/or sell copies of the Software, and to permit persons to whom the
94562236bSHarry Wentland  * Software is furnished to do so, subject to the following conditions:
104562236bSHarry Wentland  *
114562236bSHarry Wentland  * The above copyright notice and this permission notice shall be included in
124562236bSHarry Wentland  * all copies or substantial portions of the Software.
134562236bSHarry Wentland  *
144562236bSHarry Wentland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154562236bSHarry Wentland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164562236bSHarry Wentland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174562236bSHarry Wentland  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184562236bSHarry Wentland  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194562236bSHarry Wentland  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204562236bSHarry Wentland  * OTHER DEALINGS IN THE SOFTWARE.
214562236bSHarry Wentland  *
224562236bSHarry Wentland  * Authors: AMD
234562236bSHarry Wentland  *
244562236bSHarry Wentland  */
254562236bSHarry Wentland #include "dm_services.h"
264562236bSHarry Wentland 
274562236bSHarry Wentland #include "resource.h"
284562236bSHarry Wentland #include "include/irq_service_interface.h"
294562236bSHarry Wentland #include "link_encoder.h"
304562236bSHarry Wentland #include "stream_encoder.h"
314562236bSHarry Wentland #include "opp.h"
324562236bSHarry Wentland #include "timing_generator.h"
334562236bSHarry Wentland #include "transform.h"
34d94585a0SYue Hin Lau #include "dpp.h"
355ac3d3c9SCharlene Liu #include "core_types.h"
364562236bSHarry Wentland #include "set_mode_types.h"
374562236bSHarry Wentland #include "virtual/virtual_stream_encoder.h"
384562236bSHarry Wentland 
394562236bSHarry Wentland #include "dce80/dce80_resource.h"
404562236bSHarry Wentland #include "dce100/dce100_resource.h"
414562236bSHarry Wentland #include "dce110/dce110_resource.h"
424562236bSHarry Wentland #include "dce112/dce112_resource.h"
43ff5ef992SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
44ff5ef992SAlex Deucher #include "dcn10/dcn10_resource.h"
45ff5ef992SAlex Deucher #endif
462c8ad2d5SAlex Deucher #include "dce120/dce120_resource.h"
474562236bSHarry Wentland 
484562236bSHarry Wentland enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
494562236bSHarry Wentland {
504562236bSHarry Wentland 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
514562236bSHarry Wentland 	switch (asic_id.chip_family) {
524562236bSHarry Wentland 
534562236bSHarry Wentland 	case FAMILY_CI:
544562236bSHarry Wentland 		dc_version = DCE_VERSION_8_0;
554562236bSHarry Wentland 		break;
56ebfdf0d0SAlex Deucher 	case FAMILY_KV:
57ebfdf0d0SAlex Deucher 		if (ASIC_REV_IS_KALINDI(asic_id.hw_internal_rev) ||
58ebfdf0d0SAlex Deucher 		    ASIC_REV_IS_BHAVANI(asic_id.hw_internal_rev) ||
59ebfdf0d0SAlex Deucher 		    ASIC_REV_IS_GODAVARI(asic_id.hw_internal_rev))
60ebfdf0d0SAlex Deucher 			dc_version = DCE_VERSION_8_3;
61ebfdf0d0SAlex Deucher 		else
62ebfdf0d0SAlex Deucher 			dc_version = DCE_VERSION_8_1;
63ebfdf0d0SAlex Deucher 		break;
644562236bSHarry Wentland 	case FAMILY_CZ:
654562236bSHarry Wentland 		dc_version = DCE_VERSION_11_0;
664562236bSHarry Wentland 		break;
674562236bSHarry Wentland 
684562236bSHarry Wentland 	case FAMILY_VI:
694562236bSHarry Wentland 		if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
704562236bSHarry Wentland 				ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
714562236bSHarry Wentland 			dc_version = DCE_VERSION_10_0;
724562236bSHarry Wentland 			break;
734562236bSHarry Wentland 		}
744562236bSHarry Wentland 		if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
75b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
76b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
774562236bSHarry Wentland 			dc_version = DCE_VERSION_11_2;
784562236bSHarry Wentland 		}
794562236bSHarry Wentland 		break;
802c8ad2d5SAlex Deucher 	case FAMILY_AI:
812c8ad2d5SAlex Deucher 		dc_version = DCE_VERSION_12_0;
822c8ad2d5SAlex Deucher 		break;
83ff5ef992SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
84ff5ef992SAlex Deucher 	case FAMILY_RV:
85ff5ef992SAlex Deucher 		dc_version = DCN_VERSION_1_0;
86ff5ef992SAlex Deucher 		break;
87ff5ef992SAlex Deucher #endif
884562236bSHarry Wentland 	default:
894562236bSHarry Wentland 		dc_version = DCE_VERSION_UNKNOWN;
904562236bSHarry Wentland 		break;
914562236bSHarry Wentland 	}
924562236bSHarry Wentland 	return dc_version;
934562236bSHarry Wentland }
944562236bSHarry Wentland 
954562236bSHarry Wentland struct resource_pool *dc_create_resource_pool(
96fb3466a4SBhawanpreet Lakha 				struct dc  *dc,
974562236bSHarry Wentland 				int num_virtual_links,
984562236bSHarry Wentland 				enum dce_version dc_version,
994562236bSHarry Wentland 				struct hw_asic_id asic_id)
1004562236bSHarry Wentland {
1015ac3d3c9SCharlene Liu 	struct resource_pool *res_pool = NULL;
1024562236bSHarry Wentland 
1034562236bSHarry Wentland 	switch (dc_version) {
1044562236bSHarry Wentland 	case DCE_VERSION_8_0:
1055ac3d3c9SCharlene Liu 		res_pool = dce80_create_resource_pool(
1064562236bSHarry Wentland 			num_virtual_links, dc);
1075ac3d3c9SCharlene Liu 		break;
1087992a629SAlex Deucher 	case DCE_VERSION_8_1:
1097992a629SAlex Deucher 		res_pool = dce81_create_resource_pool(
1107992a629SAlex Deucher 			num_virtual_links, dc);
1117992a629SAlex Deucher 		break;
1127992a629SAlex Deucher 	case DCE_VERSION_8_3:
1137992a629SAlex Deucher 		res_pool = dce83_create_resource_pool(
1147992a629SAlex Deucher 			num_virtual_links, dc);
1157992a629SAlex Deucher 		break;
1164562236bSHarry Wentland 	case DCE_VERSION_10_0:
1175ac3d3c9SCharlene Liu 		res_pool = dce100_create_resource_pool(
1184562236bSHarry Wentland 				num_virtual_links, dc);
1195ac3d3c9SCharlene Liu 		break;
1204562236bSHarry Wentland 	case DCE_VERSION_11_0:
1215ac3d3c9SCharlene Liu 		res_pool = dce110_create_resource_pool(
1224562236bSHarry Wentland 			num_virtual_links, dc, asic_id);
1235ac3d3c9SCharlene Liu 		break;
1244562236bSHarry Wentland 	case DCE_VERSION_11_2:
1255ac3d3c9SCharlene Liu 		res_pool = dce112_create_resource_pool(
1264562236bSHarry Wentland 			num_virtual_links, dc);
1275ac3d3c9SCharlene Liu 		break;
1282c8ad2d5SAlex Deucher 	case DCE_VERSION_12_0:
1292c8ad2d5SAlex Deucher 		res_pool = dce120_create_resource_pool(
1302c8ad2d5SAlex Deucher 			num_virtual_links, dc);
1312c8ad2d5SAlex Deucher 		break;
132ff5ef992SAlex Deucher 
133ff5ef992SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
134ff5ef992SAlex Deucher 	case DCN_VERSION_1_0:
135ff5ef992SAlex Deucher 		res_pool = dcn10_create_resource_pool(
136ff5ef992SAlex Deucher 				num_virtual_links, dc);
137ff5ef992SAlex Deucher 		break;
138ff5ef992SAlex Deucher #endif
1393639fa68SZeyu Fan 
1403639fa68SZeyu Fan 
1414562236bSHarry Wentland 	default:
1424562236bSHarry Wentland 		break;
1434562236bSHarry Wentland 	}
1445ac3d3c9SCharlene Liu 	if (res_pool != NULL) {
1451515a47bSHarry Wentland 		struct dc_firmware_info fw_info = { { 0 } };
1464562236bSHarry Wentland 
1475ac3d3c9SCharlene Liu 		if (dc->ctx->dc_bios->funcs->get_firmware_info(
1485ac3d3c9SCharlene Liu 				dc->ctx->dc_bios, &fw_info) == BP_RESULT_OK) {
1495ac3d3c9SCharlene Liu 				res_pool->ref_clock_inKhz = fw_info.pll_info.crystal_frequency;
1505ac3d3c9SCharlene Liu 			} else
1515ac3d3c9SCharlene Liu 				ASSERT_CRITICAL(false);
1525ac3d3c9SCharlene Liu 	}
1535ac3d3c9SCharlene Liu 
1545ac3d3c9SCharlene Liu 	return res_pool;
1554562236bSHarry Wentland }
1564562236bSHarry Wentland 
157fb3466a4SBhawanpreet Lakha void dc_destroy_resource_pool(struct dc  *dc)
1584562236bSHarry Wentland {
1594562236bSHarry Wentland 	if (dc) {
1604562236bSHarry Wentland 		if (dc->res_pool)
1614562236bSHarry Wentland 			dc->res_pool->funcs->destroy(&dc->res_pool);
1624562236bSHarry Wentland 
1632004f45eSHarry Wentland 		kfree(dc->hwseq);
1644562236bSHarry Wentland 	}
1654562236bSHarry Wentland }
1664562236bSHarry Wentland 
1674562236bSHarry Wentland static void update_num_audio(
1684562236bSHarry Wentland 	const struct resource_straps *straps,
1694562236bSHarry Wentland 	unsigned int *num_audio,
1704562236bSHarry Wentland 	struct audio_support *aud_support)
1714562236bSHarry Wentland {
1724562236bSHarry Wentland 	aud_support->dp_audio = true;
173b8e9eb72SCharlene Liu 	aud_support->hdmi_audio_native = false;
174b8e9eb72SCharlene Liu 	aud_support->hdmi_audio_on_dongle = false;
175b8e9eb72SCharlene Liu 
176b8e9eb72SCharlene Liu 	if (straps->hdmi_disable == 0) {
1774562236bSHarry Wentland 		if (straps->dc_pinstraps_audio & 0x2) {
1784562236bSHarry Wentland 			aud_support->hdmi_audio_on_dongle = true;
179b8e9eb72SCharlene Liu 			aud_support->hdmi_audio_native = true;
1804562236bSHarry Wentland 		}
1814562236bSHarry Wentland 	}
1824562236bSHarry Wentland 
1834562236bSHarry Wentland 	switch (straps->audio_stream_number) {
1844562236bSHarry Wentland 	case 0: /* multi streams supported */
1854562236bSHarry Wentland 		break;
1864562236bSHarry Wentland 	case 1: /* multi streams not supported */
1874562236bSHarry Wentland 		*num_audio = 1;
1884562236bSHarry Wentland 		break;
1894562236bSHarry Wentland 	default:
1904562236bSHarry Wentland 		DC_ERR("DC: unexpected audio fuse!\n");
19117a96033SJulia Lawall 	}
1924562236bSHarry Wentland }
1934562236bSHarry Wentland 
1944562236bSHarry Wentland bool resource_construct(
1954562236bSHarry Wentland 	unsigned int num_virtual_links,
196fb3466a4SBhawanpreet Lakha 	struct dc  *dc,
1974562236bSHarry Wentland 	struct resource_pool *pool,
1984562236bSHarry Wentland 	const struct resource_create_funcs *create_funcs)
1994562236bSHarry Wentland {
2004562236bSHarry Wentland 	struct dc_context *ctx = dc->ctx;
2014562236bSHarry Wentland 	const struct resource_caps *caps = pool->res_cap;
2024562236bSHarry Wentland 	int i;
2034562236bSHarry Wentland 	unsigned int num_audio = caps->num_audio;
2044562236bSHarry Wentland 	struct resource_straps straps = {0};
2054562236bSHarry Wentland 
2064562236bSHarry Wentland 	if (create_funcs->read_dce_straps)
2074562236bSHarry Wentland 		create_funcs->read_dce_straps(dc->ctx, &straps);
2084562236bSHarry Wentland 
2094562236bSHarry Wentland 	pool->audio_count = 0;
2104562236bSHarry Wentland 	if (create_funcs->create_audio) {
2114562236bSHarry Wentland 		/* find the total number of streams available via the
2124562236bSHarry Wentland 		 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
2134562236bSHarry Wentland 		 * registers (one for each pin) starting from pin 1
2144562236bSHarry Wentland 		 * up to the max number of audio pins.
2154562236bSHarry Wentland 		 * We stop on the first pin where
2164562236bSHarry Wentland 		 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
2174562236bSHarry Wentland 		 */
2184562236bSHarry Wentland 		update_num_audio(&straps, &num_audio, &pool->audio_support);
2194562236bSHarry Wentland 		for (i = 0; i < pool->pipe_count && i < num_audio; i++) {
2204562236bSHarry Wentland 			struct audio *aud = create_funcs->create_audio(ctx, i);
2214562236bSHarry Wentland 
2224562236bSHarry Wentland 			if (aud == NULL) {
2234562236bSHarry Wentland 				DC_ERR("DC: failed to create audio!\n");
2244562236bSHarry Wentland 				return false;
2254562236bSHarry Wentland 			}
2264562236bSHarry Wentland 
2274562236bSHarry Wentland 			if (!aud->funcs->endpoint_valid(aud)) {
2284562236bSHarry Wentland 				aud->funcs->destroy(&aud);
2294562236bSHarry Wentland 				break;
2304562236bSHarry Wentland 			}
2314562236bSHarry Wentland 
2324562236bSHarry Wentland 			pool->audios[i] = aud;
2334562236bSHarry Wentland 			pool->audio_count++;
2344562236bSHarry Wentland 		}
2354562236bSHarry Wentland 	}
2364562236bSHarry Wentland 
2374562236bSHarry Wentland 	pool->stream_enc_count = 0;
2384562236bSHarry Wentland 	if (create_funcs->create_stream_encoder) {
2394562236bSHarry Wentland 		for (i = 0; i < caps->num_stream_encoder; i++) {
2404562236bSHarry Wentland 			pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
2414562236bSHarry Wentland 			if (pool->stream_enc[i] == NULL)
2424562236bSHarry Wentland 				DC_ERR("DC: failed to create stream_encoder!\n");
2434562236bSHarry Wentland 			pool->stream_enc_count++;
2444562236bSHarry Wentland 		}
2454562236bSHarry Wentland 	}
2464176664bSCharlene Liu 	dc->caps.dynamic_audio = false;
2474176664bSCharlene Liu 	if (pool->audio_count < pool->stream_enc_count) {
2484176664bSCharlene Liu 		dc->caps.dynamic_audio = true;
2494176664bSCharlene Liu 	}
2504562236bSHarry Wentland 	for (i = 0; i < num_virtual_links; i++) {
2514562236bSHarry Wentland 		pool->stream_enc[pool->stream_enc_count] =
2524562236bSHarry Wentland 			virtual_stream_encoder_create(
2534562236bSHarry Wentland 					ctx, ctx->dc_bios);
2544562236bSHarry Wentland 		if (pool->stream_enc[pool->stream_enc_count] == NULL) {
2554562236bSHarry Wentland 			DC_ERR("DC: failed to create stream_encoder!\n");
2564562236bSHarry Wentland 			return false;
2574562236bSHarry Wentland 		}
2584562236bSHarry Wentland 		pool->stream_enc_count++;
2594562236bSHarry Wentland 	}
2604562236bSHarry Wentland 
2614562236bSHarry Wentland 	dc->hwseq = create_funcs->create_hwseq(ctx);
2624562236bSHarry Wentland 
2634562236bSHarry Wentland 	return true;
2644562236bSHarry Wentland }
2654562236bSHarry Wentland 
2664562236bSHarry Wentland 
26721e67d4dSHarry Wentland void resource_unreference_clock_source(
2684562236bSHarry Wentland 		struct resource_context *res_ctx,
269a2b8659dSTony Cheng 		const struct resource_pool *pool,
2704a629536SHarry Wentland 		struct clock_source *clock_source)
2714562236bSHarry Wentland {
2724562236bSHarry Wentland 	int i;
2734a629536SHarry Wentland 
274a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; i++) {
2754a629536SHarry Wentland 		if (pool->clock_sources[i] != clock_source)
2764562236bSHarry Wentland 			continue;
2774562236bSHarry Wentland 
2784562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]--;
2794562236bSHarry Wentland 
2804562236bSHarry Wentland 		break;
2814562236bSHarry Wentland 	}
2824562236bSHarry Wentland 
28321e67d4dSHarry Wentland 	if (pool->dp_clock_source == clock_source)
2844562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count--;
2854562236bSHarry Wentland }
2864562236bSHarry Wentland 
2874562236bSHarry Wentland void resource_reference_clock_source(
2884562236bSHarry Wentland 		struct resource_context *res_ctx,
289a2b8659dSTony Cheng 		const struct resource_pool *pool,
2904562236bSHarry Wentland 		struct clock_source *clock_source)
2914562236bSHarry Wentland {
2924562236bSHarry Wentland 	int i;
293a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; i++) {
294a2b8659dSTony Cheng 		if (pool->clock_sources[i] != clock_source)
2954562236bSHarry Wentland 			continue;
2964562236bSHarry Wentland 
2974562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]++;
2984562236bSHarry Wentland 		break;
2994562236bSHarry Wentland 	}
3004562236bSHarry Wentland 
301a2b8659dSTony Cheng 	if (pool->dp_clock_source == clock_source)
3024562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count++;
3034562236bSHarry Wentland }
3044562236bSHarry Wentland 
3054562236bSHarry Wentland bool resource_are_streams_timing_synchronizable(
3060971c40eSHarry Wentland 	struct dc_stream_state *stream1,
3070971c40eSHarry Wentland 	struct dc_stream_state *stream2)
3084562236bSHarry Wentland {
3094fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.h_total != stream2->timing.h_total)
3104562236bSHarry Wentland 		return false;
3114562236bSHarry Wentland 
3124fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.v_total != stream2->timing.v_total)
3134562236bSHarry Wentland 		return false;
3144562236bSHarry Wentland 
3154fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.h_addressable
3164fa086b9SLeo (Sunpeng) Li 				!= stream2->timing.h_addressable)
3174562236bSHarry Wentland 		return false;
3184562236bSHarry Wentland 
3194fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.v_addressable
3204fa086b9SLeo (Sunpeng) Li 				!= stream2->timing.v_addressable)
3214562236bSHarry Wentland 		return false;
3224562236bSHarry Wentland 
3234fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.pix_clk_khz
3244fa086b9SLeo (Sunpeng) Li 				!= stream2->timing.pix_clk_khz)
3254562236bSHarry Wentland 		return false;
3264562236bSHarry Wentland 
3274562236bSHarry Wentland 	if (stream1->phy_pix_clk != stream2->phy_pix_clk
3287e2fe319SCharlene Liu 			&& (!dc_is_dp_signal(stream1->signal)
3297e2fe319SCharlene Liu 			|| !dc_is_dp_signal(stream2->signal)))
3304562236bSHarry Wentland 		return false;
3314562236bSHarry Wentland 
3324562236bSHarry Wentland 	return true;
3334562236bSHarry Wentland }
3344562236bSHarry Wentland 
3354562236bSHarry Wentland static bool is_sharable_clk_src(
3364562236bSHarry Wentland 	const struct pipe_ctx *pipe_with_clk_src,
3374562236bSHarry Wentland 	const struct pipe_ctx *pipe)
3384562236bSHarry Wentland {
3394562236bSHarry Wentland 	if (pipe_with_clk_src->clock_source == NULL)
3404562236bSHarry Wentland 		return false;
3414562236bSHarry Wentland 
3424562236bSHarry Wentland 	if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
3434562236bSHarry Wentland 		return false;
3444562236bSHarry Wentland 
3454562236bSHarry Wentland 	if (dc_is_dp_signal(pipe_with_clk_src->stream->signal))
3464562236bSHarry Wentland 		return false;
3474562236bSHarry Wentland 
3484562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
3494562236bSHarry Wentland 			&& dc_is_dvi_signal(pipe->stream->signal))
3504562236bSHarry Wentland 		return false;
3514562236bSHarry Wentland 
3524562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe->stream->signal)
3534562236bSHarry Wentland 			&& dc_is_dvi_signal(pipe_with_clk_src->stream->signal))
3544562236bSHarry Wentland 		return false;
3554562236bSHarry Wentland 
3564562236bSHarry Wentland 	if (!resource_are_streams_timing_synchronizable(
3574562236bSHarry Wentland 			pipe_with_clk_src->stream, pipe->stream))
3584562236bSHarry Wentland 		return false;
3594562236bSHarry Wentland 
3604562236bSHarry Wentland 	return true;
3614562236bSHarry Wentland }
3624562236bSHarry Wentland 
3634562236bSHarry Wentland struct clock_source *resource_find_used_clk_src_for_sharing(
3644562236bSHarry Wentland 					struct resource_context *res_ctx,
3654562236bSHarry Wentland 					struct pipe_ctx *pipe_ctx)
3664562236bSHarry Wentland {
3674562236bSHarry Wentland 	int i;
3684562236bSHarry Wentland 
3694562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
3704562236bSHarry Wentland 		if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
3714562236bSHarry Wentland 			return res_ctx->pipe_ctx[i].clock_source;
3724562236bSHarry Wentland 	}
3734562236bSHarry Wentland 
3744562236bSHarry Wentland 	return NULL;
3754562236bSHarry Wentland }
3764562236bSHarry Wentland 
3774562236bSHarry Wentland static enum pixel_format convert_pixel_format_to_dalsurface(
3784562236bSHarry Wentland 		enum surface_pixel_format surface_pixel_format)
3794562236bSHarry Wentland {
3804562236bSHarry Wentland 	enum pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
3814562236bSHarry Wentland 
3824562236bSHarry Wentland 	switch (surface_pixel_format) {
3834562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
3844562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_INDEX8;
3854562236bSHarry Wentland 		break;
3864562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
3874562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
3884562236bSHarry Wentland 		break;
3894562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
3904562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
3914562236bSHarry Wentland 		break;
3924562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
3934562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
3944562236bSHarry Wentland 		break;
3958693049aSTony Cheng 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
3964562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
3974562236bSHarry Wentland 		break;
3984562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
3994562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
4004562236bSHarry Wentland 		break;
4014562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
4024562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
4034562236bSHarry Wentland 		break;
4044562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
4054562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
4064562236bSHarry Wentland 		break;
4074562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
4084562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
4094562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_FP16;
4104562236bSHarry Wentland 		break;
4114562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
4124562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
41387449a90SAnthony Koo 		dal_pixel_format = PIXEL_FORMAT_420BPP8;
4144562236bSHarry Wentland 		break;
415ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
416ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
41787449a90SAnthony Koo 		dal_pixel_format = PIXEL_FORMAT_420BPP10;
418ffbcd19aSVitaly Prosyak 		break;
4194562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
4204562236bSHarry Wentland 	default:
4214562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
4224562236bSHarry Wentland 		break;
4234562236bSHarry Wentland 	}
4244562236bSHarry Wentland 	return dal_pixel_format;
4254562236bSHarry Wentland }
4264562236bSHarry Wentland 
4274562236bSHarry Wentland static void rect_swap_helper(struct rect *rect)
4284562236bSHarry Wentland {
4294562236bSHarry Wentland 	uint32_t temp = 0;
4304562236bSHarry Wentland 
4314562236bSHarry Wentland 	temp = rect->height;
4324562236bSHarry Wentland 	rect->height = rect->width;
4334562236bSHarry Wentland 	rect->width = temp;
4344562236bSHarry Wentland 
4354562236bSHarry Wentland 	temp = rect->x;
4364562236bSHarry Wentland 	rect->x = rect->y;
4374562236bSHarry Wentland 	rect->y = temp;
4384562236bSHarry Wentland }
4394562236bSHarry Wentland 
440b2d0a103SDmytro Laktyushkin static void calculate_viewport(struct pipe_ctx *pipe_ctx)
4414562236bSHarry Wentland {
4423be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
4430971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
4446702a9acSHarry Wentland 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
4453be5262eSHarry Wentland 	struct rect surf_src = plane_state->src_rect;
4464562236bSHarry Wentland 	struct rect clip = { 0 };
44787449a90SAnthony Koo 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
44887449a90SAnthony Koo 			|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
4491fbd2cfcSDmytro Laktyushkin 	bool pri_split = pipe_ctx->bottom_pipe &&
4503be5262eSHarry Wentland 			pipe_ctx->bottom_pipe->plane_state == pipe_ctx->plane_state;
4511fbd2cfcSDmytro Laktyushkin 	bool sec_split = pipe_ctx->top_pipe &&
4523be5262eSHarry Wentland 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
4534562236bSHarry Wentland 
4547f5c22d1SVitaly Prosyak 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE ||
4557f5c22d1SVitaly Prosyak 		stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM) {
4567b779c99SVitaly Prosyak 		pri_split = false;
4577b779c99SVitaly Prosyak 		sec_split = false;
4587b779c99SVitaly Prosyak 	}
45986006a7fSDmytro Laktyushkin 
4603be5262eSHarry Wentland 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
4613be5262eSHarry Wentland 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
46286006a7fSDmytro Laktyushkin 		rect_swap_helper(&surf_src);
46386006a7fSDmytro Laktyushkin 
4644562236bSHarry Wentland 	/* The actual clip is an intersection between stream
4654562236bSHarry Wentland 	 * source and surface clip
4664562236bSHarry Wentland 	 */
4673be5262eSHarry Wentland 	clip.x = stream->src.x > plane_state->clip_rect.x ?
4683be5262eSHarry Wentland 			stream->src.x : plane_state->clip_rect.x;
4694562236bSHarry Wentland 
4701fbd2cfcSDmytro Laktyushkin 	clip.width = stream->src.x + stream->src.width <
4713be5262eSHarry Wentland 			plane_state->clip_rect.x + plane_state->clip_rect.width ?
4721fbd2cfcSDmytro Laktyushkin 			stream->src.x + stream->src.width - clip.x :
4733be5262eSHarry Wentland 			plane_state->clip_rect.x + plane_state->clip_rect.width - clip.x ;
4744562236bSHarry Wentland 
4753be5262eSHarry Wentland 	clip.y = stream->src.y > plane_state->clip_rect.y ?
4763be5262eSHarry Wentland 			stream->src.y : plane_state->clip_rect.y;
4774562236bSHarry Wentland 
4781fbd2cfcSDmytro Laktyushkin 	clip.height = stream->src.y + stream->src.height <
4793be5262eSHarry Wentland 			plane_state->clip_rect.y + plane_state->clip_rect.height ?
4801fbd2cfcSDmytro Laktyushkin 			stream->src.y + stream->src.height - clip.y :
4813be5262eSHarry Wentland 			plane_state->clip_rect.y + plane_state->clip_rect.height - clip.y ;
4824562236bSHarry Wentland 
48386006a7fSDmytro Laktyushkin 	/* offset = surf_src.ofs + (clip.ofs - surface->dst_rect.ofs) * scl_ratio
4844562236bSHarry Wentland 	 * num_pixels = clip.num_pix * scl_ratio
4854562236bSHarry Wentland 	 */
4863be5262eSHarry Wentland 	data->viewport.x = surf_src.x + (clip.x - plane_state->dst_rect.x) *
4873be5262eSHarry Wentland 			surf_src.width / plane_state->dst_rect.width;
488b2d0a103SDmytro Laktyushkin 	data->viewport.width = clip.width *
4893be5262eSHarry Wentland 			surf_src.width / plane_state->dst_rect.width;
4904562236bSHarry Wentland 
4913be5262eSHarry Wentland 	data->viewport.y = surf_src.y + (clip.y - plane_state->dst_rect.y) *
4923be5262eSHarry Wentland 			surf_src.height / plane_state->dst_rect.height;
493b2d0a103SDmytro Laktyushkin 	data->viewport.height = clip.height *
4943be5262eSHarry Wentland 			surf_src.height / plane_state->dst_rect.height;
4954562236bSHarry Wentland 
496b2d0a103SDmytro Laktyushkin 	/* Round down, compensate in init */
497b2d0a103SDmytro Laktyushkin 	data->viewport_c.x = data->viewport.x / vpc_div;
498b2d0a103SDmytro Laktyushkin 	data->viewport_c.y = data->viewport.y / vpc_div;
499b2d0a103SDmytro Laktyushkin 	data->inits.h_c = (data->viewport.x % vpc_div) != 0 ?
500b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_half : dal_fixed31_32_zero;
501b2d0a103SDmytro Laktyushkin 	data->inits.v_c = (data->viewport.y % vpc_div) != 0 ?
502b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_half : dal_fixed31_32_zero;
503b2d0a103SDmytro Laktyushkin 	/* Round up, assume original video size always even dimensions */
504b2d0a103SDmytro Laktyushkin 	data->viewport_c.width = (data->viewport.width + vpc_div - 1) / vpc_div;
505b2d0a103SDmytro Laktyushkin 	data->viewport_c.height = (data->viewport.height + vpc_div - 1) / vpc_div;
506b2d0a103SDmytro Laktyushkin 
507b2d0a103SDmytro Laktyushkin 	/* Handle hsplit */
5081fbd2cfcSDmytro Laktyushkin 	if (pri_split || sec_split) {
5091fbd2cfcSDmytro Laktyushkin 		/* HMirror XOR Secondary_pipe XOR Rotation_180 */
5103be5262eSHarry Wentland 		bool right_view = (sec_split != plane_state->horizontal_mirror) !=
5113be5262eSHarry Wentland 					(plane_state->rotation == ROTATION_ANGLE_180);
5129e6c74ceSDmytro Laktyushkin 
5133be5262eSHarry Wentland 		if (plane_state->rotation == ROTATION_ANGLE_90
5143be5262eSHarry Wentland 				|| plane_state->rotation == ROTATION_ANGLE_270)
5151fbd2cfcSDmytro Laktyushkin 			/* Secondary_pipe XOR Rotation_270 */
5163be5262eSHarry Wentland 			right_view = (plane_state->rotation == ROTATION_ANGLE_270) != sec_split;
5179e6c74ceSDmytro Laktyushkin 
5189e6c74ceSDmytro Laktyushkin 		if (right_view) {
519b2d0a103SDmytro Laktyushkin 			data->viewport.width /= 2;
520b2d0a103SDmytro Laktyushkin 			data->viewport_c.width /= 2;
521b2d0a103SDmytro Laktyushkin 			data->viewport.x +=  data->viewport.width;
522b2d0a103SDmytro Laktyushkin 			data->viewport_c.x +=  data->viewport_c.width;
5239e6c74ceSDmytro Laktyushkin 			/* Ceil offset pipe */
524b2d0a103SDmytro Laktyushkin 			data->viewport.width += data->viewport.width % 2;
525b2d0a103SDmytro Laktyushkin 			data->viewport_c.width += data->viewport_c.width % 2;
5269e6c74ceSDmytro Laktyushkin 		} else {
527b2d0a103SDmytro Laktyushkin 			data->viewport.width /= 2;
528b2d0a103SDmytro Laktyushkin 			data->viewport_c.width /= 2;
529b2d0a103SDmytro Laktyushkin 		}
5304562236bSHarry Wentland 	}
5311fbd2cfcSDmytro Laktyushkin 
5323be5262eSHarry Wentland 	if (plane_state->rotation == ROTATION_ANGLE_90 ||
5333be5262eSHarry Wentland 			plane_state->rotation == ROTATION_ANGLE_270) {
5341fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport_c);
5351fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport);
5361fbd2cfcSDmytro Laktyushkin 	}
5379e6c74ceSDmytro Laktyushkin }
5384562236bSHarry Wentland 
539b2d0a103SDmytro Laktyushkin static void calculate_recout(struct pipe_ctx *pipe_ctx, struct view *recout_skip)
5404562236bSHarry Wentland {
5413be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
5420971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
5433be5262eSHarry Wentland 	struct rect surf_src = plane_state->src_rect;
5443be5262eSHarry Wentland 	struct rect surf_clip = plane_state->clip_rect;
545c802570eSDmytro Laktyushkin 	int recout_full_x, recout_full_y;
5464562236bSHarry Wentland 
5473be5262eSHarry Wentland 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
5483be5262eSHarry Wentland 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
54986006a7fSDmytro Laktyushkin 		rect_swap_helper(&surf_src);
55086006a7fSDmytro Laktyushkin 
5516702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.x = stream->dst.x;
5524fa086b9SLeo (Sunpeng) Li 	if (stream->src.x < surf_clip.x)
5536702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.x += (surf_clip.x
5544fa086b9SLeo (Sunpeng) Li 			- stream->src.x) * stream->dst.width
5554fa086b9SLeo (Sunpeng) Li 						/ stream->src.width;
5564562236bSHarry Wentland 
5576702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.width = surf_clip.width *
5584fa086b9SLeo (Sunpeng) Li 			stream->dst.width / stream->src.width;
5596702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.recout.width + pipe_ctx->plane_res.scl_data.recout.x >
5604fa086b9SLeo (Sunpeng) Li 			stream->dst.x + stream->dst.width)
5616702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.width =
5624fa086b9SLeo (Sunpeng) Li 			stream->dst.x + stream->dst.width
5636702a9acSHarry Wentland 						- pipe_ctx->plane_res.scl_data.recout.x;
5644562236bSHarry Wentland 
5656702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.y = stream->dst.y;
5664fa086b9SLeo (Sunpeng) Li 	if (stream->src.y < surf_clip.y)
5676702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.y += (surf_clip.y
5684fa086b9SLeo (Sunpeng) Li 			- stream->src.y) * stream->dst.height
5694fa086b9SLeo (Sunpeng) Li 						/ stream->src.height;
5704562236bSHarry Wentland 
5716702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.height = surf_clip.height *
5724fa086b9SLeo (Sunpeng) Li 			stream->dst.height / stream->src.height;
5736702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.recout.height + pipe_ctx->plane_res.scl_data.recout.y >
5744fa086b9SLeo (Sunpeng) Li 			stream->dst.y + stream->dst.height)
5756702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.height =
5764fa086b9SLeo (Sunpeng) Li 			stream->dst.y + stream->dst.height
5776702a9acSHarry Wentland 						- pipe_ctx->plane_res.scl_data.recout.y;
578b2d0a103SDmytro Laktyushkin 
5797b779c99SVitaly Prosyak 	/* Handle h & vsplit */
5803be5262eSHarry Wentland 	if (pipe_ctx->top_pipe && pipe_ctx->top_pipe->plane_state ==
5813be5262eSHarry Wentland 		pipe_ctx->plane_state) {
5824fa086b9SLeo (Sunpeng) Li 		if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM) {
5836702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.height /= 2;
5846702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.y += pipe_ctx->plane_res.scl_data.recout.height;
5857b779c99SVitaly Prosyak 			/* Floor primary pipe, ceil 2ndary pipe */
5866702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.height += pipe_ctx->plane_res.scl_data.recout.height % 2;
5877b779c99SVitaly Prosyak 		} else {
5886702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.width /= 2;
5896702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.x += pipe_ctx->plane_res.scl_data.recout.width;
5906702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.width += pipe_ctx->plane_res.scl_data.recout.width % 2;
5917b779c99SVitaly Prosyak 		}
5927b779c99SVitaly Prosyak 	} else if (pipe_ctx->bottom_pipe &&
5933be5262eSHarry Wentland 			pipe_ctx->bottom_pipe->plane_state == pipe_ctx->plane_state) {
5944fa086b9SLeo (Sunpeng) Li 		if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
5956702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.height /= 2;
5967b779c99SVitaly Prosyak 		else
5976702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.width /= 2;
5984562236bSHarry Wentland 	}
5994562236bSHarry Wentland 
60086006a7fSDmytro Laktyushkin 	/* Unclipped recout offset = stream dst offset + ((surf dst offset - stream surf_src offset)
60186006a7fSDmytro Laktyushkin 	 * 				* 1/ stream scaling ratio) - (surf surf_src offset * 1/ full scl
602c802570eSDmytro Laktyushkin 	 * 				ratio)
603c802570eSDmytro Laktyushkin 	 */
6043be5262eSHarry Wentland 	recout_full_x = stream->dst.x + (plane_state->dst_rect.x -  stream->src.x)
6054fa086b9SLeo (Sunpeng) Li 					* stream->dst.width / stream->src.width -
6063be5262eSHarry Wentland 			surf_src.x * plane_state->dst_rect.width / surf_src.width
6074fa086b9SLeo (Sunpeng) Li 					* stream->dst.width / stream->src.width;
6083be5262eSHarry Wentland 	recout_full_y = stream->dst.y + (plane_state->dst_rect.y -  stream->src.y)
6094fa086b9SLeo (Sunpeng) Li 					* stream->dst.height / stream->src.height -
6103be5262eSHarry Wentland 			surf_src.y * plane_state->dst_rect.height / surf_src.height
6114fa086b9SLeo (Sunpeng) Li 					* stream->dst.height / stream->src.height;
612c802570eSDmytro Laktyushkin 
6136702a9acSHarry Wentland 	recout_skip->width = pipe_ctx->plane_res.scl_data.recout.x - recout_full_x;
6146702a9acSHarry Wentland 	recout_skip->height = pipe_ctx->plane_res.scl_data.recout.y - recout_full_y;
615b2d0a103SDmytro Laktyushkin }
616b2d0a103SDmytro Laktyushkin 
617b2d0a103SDmytro Laktyushkin static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
6184562236bSHarry Wentland {
6193be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
6200971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
6213be5262eSHarry Wentland 	struct rect surf_src = plane_state->src_rect;
6224fa086b9SLeo (Sunpeng) Li 	const int in_w = stream->src.width;
6234fa086b9SLeo (Sunpeng) Li 	const int in_h = stream->src.height;
6244fa086b9SLeo (Sunpeng) Li 	const int out_w = stream->dst.width;
6254fa086b9SLeo (Sunpeng) Li 	const int out_h = stream->dst.height;
6264562236bSHarry Wentland 
6273be5262eSHarry Wentland 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
6283be5262eSHarry Wentland 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
62986006a7fSDmytro Laktyushkin 		rect_swap_helper(&surf_src);
63086006a7fSDmytro Laktyushkin 
6316702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.horz = dal_fixed31_32_from_fraction(
63286006a7fSDmytro Laktyushkin 					surf_src.width,
6333be5262eSHarry Wentland 					plane_state->dst_rect.width);
6346702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.vert = dal_fixed31_32_from_fraction(
63586006a7fSDmytro Laktyushkin 					surf_src.height,
6363be5262eSHarry Wentland 					plane_state->dst_rect.height);
6374562236bSHarry Wentland 
6384fa086b9SLeo (Sunpeng) Li 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
6396702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz.value *= 2;
6404fa086b9SLeo (Sunpeng) Li 	else if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
6416702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert.value *= 2;
6424562236bSHarry Wentland 
6436702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.vert.value = div64_s64(
6446702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert.value * in_h, out_h);
6456702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.horz.value = div64_s64(
6466702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz.value * in_w, out_w);
6474562236bSHarry Wentland 
6486702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.horz_c = pipe_ctx->plane_res.scl_data.ratios.horz;
6496702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.vert_c = pipe_ctx->plane_res.scl_data.ratios.vert;
6504562236bSHarry Wentland 
6516702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP8
6526702a9acSHarry Wentland 			|| pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP10) {
6536702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz_c.value /= 2;
6546702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert_c.value /= 2;
6554562236bSHarry Wentland 	}
6564562236bSHarry Wentland }
6574562236bSHarry Wentland 
658b2d0a103SDmytro Laktyushkin static void calculate_inits_and_adj_vp(struct pipe_ctx *pipe_ctx, struct view *recout_skip)
6594562236bSHarry Wentland {
6606702a9acSHarry Wentland 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
6613be5262eSHarry Wentland 	struct rect src = pipe_ctx->plane_state->src_rect;
66287449a90SAnthony Koo 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
66387449a90SAnthony Koo 			|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
664b2d0a103SDmytro Laktyushkin 
66586006a7fSDmytro Laktyushkin 
6663be5262eSHarry Wentland 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
6673be5262eSHarry Wentland 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270) {
66886006a7fSDmytro Laktyushkin 		rect_swap_helper(&src);
6691fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport_c);
6701fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport);
6711fbd2cfcSDmytro Laktyushkin 	}
6721fbd2cfcSDmytro Laktyushkin 
673b2d0a103SDmytro Laktyushkin 	/*
674b2d0a103SDmytro Laktyushkin 	 * Init calculated according to formula:
675b2d0a103SDmytro Laktyushkin 	 * 	init = (scaling_ratio + number_of_taps + 1) / 2
676b2d0a103SDmytro Laktyushkin 	 * 	init_bot = init + scaling_ratio
677b2d0a103SDmytro Laktyushkin 	 * 	init_c = init + truncated_vp_c_offset(from calculate viewport)
678b2d0a103SDmytro Laktyushkin 	 */
679b2d0a103SDmytro Laktyushkin 	data->inits.h = dal_fixed31_32_div_int(
680b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.horz, data->taps.h_taps + 1), 2);
681b2d0a103SDmytro Laktyushkin 
682b2d0a103SDmytro Laktyushkin 	data->inits.h_c = dal_fixed31_32_add(data->inits.h_c, dal_fixed31_32_div_int(
683b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.horz_c, data->taps.h_taps_c + 1), 2));
684b2d0a103SDmytro Laktyushkin 
685b2d0a103SDmytro Laktyushkin 	data->inits.v = dal_fixed31_32_div_int(
686b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.vert, data->taps.v_taps + 1), 2);
687b2d0a103SDmytro Laktyushkin 
688b2d0a103SDmytro Laktyushkin 	data->inits.v_c = dal_fixed31_32_add(data->inits.v_c, dal_fixed31_32_div_int(
689b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.vert_c, data->taps.v_taps_c + 1), 2));
690b2d0a103SDmytro Laktyushkin 
691b2d0a103SDmytro Laktyushkin 
692b2d0a103SDmytro Laktyushkin 	/* Adjust for viewport end clip-off */
693b2d0a103SDmytro Laktyushkin 	if ((data->viewport.x + data->viewport.width) < (src.x + src.width)) {
694b2d0a103SDmytro Laktyushkin 		int vp_clip = src.x + src.width - data->viewport.width - data->viewport.x;
6951fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
6961fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.h, data->ratios.horz));
697b2d0a103SDmytro Laktyushkin 
6981fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
699b2d0a103SDmytro Laktyushkin 		data->viewport.width += int_part < vp_clip ? int_part : vp_clip;
700b2d0a103SDmytro Laktyushkin 	}
701b2d0a103SDmytro Laktyushkin 	if ((data->viewport.y + data->viewport.height) < (src.y + src.height)) {
702b2d0a103SDmytro Laktyushkin 		int vp_clip = src.y + src.height - data->viewport.height - data->viewport.y;
7031fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
7041fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.v, data->ratios.vert));
705b2d0a103SDmytro Laktyushkin 
7061fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
707b2d0a103SDmytro Laktyushkin 		data->viewport.height += int_part < vp_clip ? int_part : vp_clip;
708b2d0a103SDmytro Laktyushkin 	}
709b2d0a103SDmytro Laktyushkin 	if ((data->viewport_c.x + data->viewport_c.width) < (src.x + src.width) / vpc_div) {
710b2d0a103SDmytro Laktyushkin 		int vp_clip = (src.x + src.width) / vpc_div -
711b2d0a103SDmytro Laktyushkin 				data->viewport_c.width - data->viewport_c.x;
7121fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
7131fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.h_c, data->ratios.horz_c));
714b2d0a103SDmytro Laktyushkin 
7151fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
716b2d0a103SDmytro Laktyushkin 		data->viewport_c.width += int_part < vp_clip ? int_part : vp_clip;
717b2d0a103SDmytro Laktyushkin 	}
718b2d0a103SDmytro Laktyushkin 	if ((data->viewport_c.y + data->viewport_c.height) < (src.y + src.height) / vpc_div) {
719b2d0a103SDmytro Laktyushkin 		int vp_clip = (src.y + src.height) / vpc_div -
720b2d0a103SDmytro Laktyushkin 				data->viewport_c.height - data->viewport_c.y;
7211fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
7221fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.v_c, data->ratios.vert_c));
723b2d0a103SDmytro Laktyushkin 
7241fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
725b2d0a103SDmytro Laktyushkin 		data->viewport_c.height += int_part < vp_clip ? int_part : vp_clip;
726b2d0a103SDmytro Laktyushkin 	}
727b2d0a103SDmytro Laktyushkin 
728b2d0a103SDmytro Laktyushkin 	/* Adjust for non-0 viewport offset */
729b2d0a103SDmytro Laktyushkin 	if (data->viewport.x) {
730b2d0a103SDmytro Laktyushkin 		int int_part;
731b2d0a103SDmytro Laktyushkin 
732b2d0a103SDmytro Laktyushkin 		data->inits.h = dal_fixed31_32_add(data->inits.h, dal_fixed31_32_mul_int(
733b2d0a103SDmytro Laktyushkin 				data->ratios.horz, recout_skip->width));
734b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.h) - data->viewport.x;
735b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.h_taps) {
736b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport.x >= (data->taps.h_taps - int_part) ?
737b2d0a103SDmytro Laktyushkin 						(data->taps.h_taps - int_part) : data->viewport.x;
738b2d0a103SDmytro Laktyushkin 			data->viewport.x -= int_adj;
739b2d0a103SDmytro Laktyushkin 			data->viewport.width += int_adj;
740b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
741b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.h_taps) {
742b2d0a103SDmytro Laktyushkin 			data->viewport.x += int_part - data->taps.h_taps;
743b2d0a103SDmytro Laktyushkin 			data->viewport.width -= int_part - data->taps.h_taps;
744b2d0a103SDmytro Laktyushkin 			int_part = data->taps.h_taps;
745b2d0a103SDmytro Laktyushkin 		}
746b2d0a103SDmytro Laktyushkin 		data->inits.h.value &= 0xffffffff;
747b2d0a103SDmytro Laktyushkin 		data->inits.h = dal_fixed31_32_add_int(data->inits.h, int_part);
748b2d0a103SDmytro Laktyushkin 	}
749b2d0a103SDmytro Laktyushkin 
750b2d0a103SDmytro Laktyushkin 	if (data->viewport_c.x) {
751b2d0a103SDmytro Laktyushkin 		int int_part;
752b2d0a103SDmytro Laktyushkin 
753b2d0a103SDmytro Laktyushkin 		data->inits.h_c = dal_fixed31_32_add(data->inits.h_c, dal_fixed31_32_mul_int(
754b2d0a103SDmytro Laktyushkin 				data->ratios.horz_c, recout_skip->width));
755b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.h_c) - data->viewport_c.x;
756b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.h_taps_c) {
757b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport_c.x >= (data->taps.h_taps_c - int_part) ?
758b2d0a103SDmytro Laktyushkin 					(data->taps.h_taps_c - int_part) : data->viewport_c.x;
759b2d0a103SDmytro Laktyushkin 			data->viewport_c.x -= int_adj;
760b2d0a103SDmytro Laktyushkin 			data->viewport_c.width += int_adj;
761b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
762b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.h_taps_c) {
763b2d0a103SDmytro Laktyushkin 			data->viewport_c.x += int_part - data->taps.h_taps_c;
764b2d0a103SDmytro Laktyushkin 			data->viewport_c.width -= int_part - data->taps.h_taps_c;
765b2d0a103SDmytro Laktyushkin 			int_part = data->taps.h_taps_c;
766b2d0a103SDmytro Laktyushkin 		}
767b2d0a103SDmytro Laktyushkin 		data->inits.h_c.value &= 0xffffffff;
768b2d0a103SDmytro Laktyushkin 		data->inits.h_c = dal_fixed31_32_add_int(data->inits.h_c, int_part);
769b2d0a103SDmytro Laktyushkin 	}
770b2d0a103SDmytro Laktyushkin 
771b2d0a103SDmytro Laktyushkin 	if (data->viewport.y) {
772b2d0a103SDmytro Laktyushkin 		int int_part;
773b2d0a103SDmytro Laktyushkin 
774b2d0a103SDmytro Laktyushkin 		data->inits.v = dal_fixed31_32_add(data->inits.v, dal_fixed31_32_mul_int(
775b2d0a103SDmytro Laktyushkin 				data->ratios.vert, recout_skip->height));
776b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.v) - data->viewport.y;
777b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.v_taps) {
778b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport.y >= (data->taps.v_taps - int_part) ?
779b2d0a103SDmytro Laktyushkin 						(data->taps.v_taps - int_part) : data->viewport.y;
780b2d0a103SDmytro Laktyushkin 			data->viewport.y -= int_adj;
781b2d0a103SDmytro Laktyushkin 			data->viewport.height += int_adj;
782b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
783b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.v_taps) {
784b2d0a103SDmytro Laktyushkin 			data->viewport.y += int_part - data->taps.v_taps;
785b2d0a103SDmytro Laktyushkin 			data->viewport.height -= int_part - data->taps.v_taps;
786b2d0a103SDmytro Laktyushkin 			int_part = data->taps.v_taps;
787b2d0a103SDmytro Laktyushkin 		}
788b2d0a103SDmytro Laktyushkin 		data->inits.v.value &= 0xffffffff;
789b2d0a103SDmytro Laktyushkin 		data->inits.v = dal_fixed31_32_add_int(data->inits.v, int_part);
790b2d0a103SDmytro Laktyushkin 	}
791b2d0a103SDmytro Laktyushkin 
792b2d0a103SDmytro Laktyushkin 	if (data->viewport_c.y) {
793b2d0a103SDmytro Laktyushkin 		int int_part;
794b2d0a103SDmytro Laktyushkin 
795b2d0a103SDmytro Laktyushkin 		data->inits.v_c = dal_fixed31_32_add(data->inits.v_c, dal_fixed31_32_mul_int(
796b2d0a103SDmytro Laktyushkin 				data->ratios.vert_c, recout_skip->height));
797b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.v_c) - data->viewport_c.y;
798b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.v_taps_c) {
799b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport_c.y >= (data->taps.v_taps_c - int_part) ?
800b2d0a103SDmytro Laktyushkin 					(data->taps.v_taps_c - int_part) : data->viewport_c.y;
801b2d0a103SDmytro Laktyushkin 			data->viewport_c.y -= int_adj;
802b2d0a103SDmytro Laktyushkin 			data->viewport_c.height += int_adj;
803b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
804b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.v_taps_c) {
805b2d0a103SDmytro Laktyushkin 			data->viewport_c.y += int_part - data->taps.v_taps_c;
806b2d0a103SDmytro Laktyushkin 			data->viewport_c.height -= int_part - data->taps.v_taps_c;
807b2d0a103SDmytro Laktyushkin 			int_part = data->taps.v_taps_c;
808b2d0a103SDmytro Laktyushkin 		}
809b2d0a103SDmytro Laktyushkin 		data->inits.v_c.value &= 0xffffffff;
810b2d0a103SDmytro Laktyushkin 		data->inits.v_c = dal_fixed31_32_add_int(data->inits.v_c, int_part);
811b2d0a103SDmytro Laktyushkin 	}
812b2d0a103SDmytro Laktyushkin 
813b2d0a103SDmytro Laktyushkin 	/* Interlaced inits based on final vert inits */
814b2d0a103SDmytro Laktyushkin 	data->inits.v_bot = dal_fixed31_32_add(data->inits.v, data->ratios.vert);
815b2d0a103SDmytro Laktyushkin 	data->inits.v_c_bot = dal_fixed31_32_add(data->inits.v_c, data->ratios.vert_c);
8161fbd2cfcSDmytro Laktyushkin 
8173be5262eSHarry Wentland 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
8183be5262eSHarry Wentland 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270) {
8191fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport_c);
8201fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport);
8211fbd2cfcSDmytro Laktyushkin 	}
822b2d0a103SDmytro Laktyushkin }
823b2d0a103SDmytro Laktyushkin 
824b2d0a103SDmytro Laktyushkin bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
825b2d0a103SDmytro Laktyushkin {
8263be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
8274fa086b9SLeo (Sunpeng) Li 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
828b2d0a103SDmytro Laktyushkin 	struct view recout_skip = { 0 };
829b2d0a103SDmytro Laktyushkin 	bool res = false;
830b2d0a103SDmytro Laktyushkin 
8314562236bSHarry Wentland 	/* Important: scaling ratio calculation requires pixel format,
8324562236bSHarry Wentland 	 * lb depth calculation requires recout and taps require scaling ratios.
833b2d0a103SDmytro Laktyushkin 	 * Inits require viewport, taps, ratios and recout of split pipe
8344562236bSHarry Wentland 	 */
8356702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
8363be5262eSHarry Wentland 			pipe_ctx->plane_state->format);
8374562236bSHarry Wentland 
838b2d0a103SDmytro Laktyushkin 	calculate_scaling_ratios(pipe_ctx);
839b2d0a103SDmytro Laktyushkin 
840b2d0a103SDmytro Laktyushkin 	calculate_viewport(pipe_ctx);
8414562236bSHarry Wentland 
8426702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.viewport.height < 16 || pipe_ctx->plane_res.scl_data.viewport.width < 16)
8434562236bSHarry Wentland 		return false;
8444562236bSHarry Wentland 
845b2d0a103SDmytro Laktyushkin 	calculate_recout(pipe_ctx, &recout_skip);
8464562236bSHarry Wentland 
8474562236bSHarry Wentland 	/**
8484562236bSHarry Wentland 	 * Setting line buffer pixel depth to 24bpp yields banding
8494562236bSHarry Wentland 	 * on certain displays, such as the Sharp 4k
8504562236bSHarry Wentland 	 */
8516702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
8524562236bSHarry Wentland 
853199e458aSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.recout.x += timing->h_border_left;
85458bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.recout.y += timing->v_border_top;
855199e458aSDmytro Laktyushkin 
85658bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.h_active = timing->h_addressable + timing->h_border_left + timing->h_border_right;
85758bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.v_active = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
8584562236bSHarry Wentland 
8594562236bSHarry Wentland 	/* Taps calculations */
860d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.xfm != NULL)
86186a66c4eSHarry Wentland 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
86286a66c4eSHarry Wentland 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
8634562236bSHarry Wentland 
864d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.dpp != NULL)
865d94585a0SYue Hin Lau 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
866d94585a0SYue Hin Lau 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
867d94585a0SYue Hin Lau 
8684562236bSHarry Wentland 	if (!res) {
8694562236bSHarry Wentland 		/* Try 24 bpp linebuffer */
8706702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
8714562236bSHarry Wentland 
87286a66c4eSHarry Wentland 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
87386a66c4eSHarry Wentland 			pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
874d94585a0SYue Hin Lau 
875d94585a0SYue Hin Lau 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
876d94585a0SYue Hin Lau 			pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
8774562236bSHarry Wentland 	}
8784562236bSHarry Wentland 
879b2d0a103SDmytro Laktyushkin 	if (res)
8801fbd2cfcSDmytro Laktyushkin 		/* May need to re-check lb size after this in some obscure scenario */
881b2d0a103SDmytro Laktyushkin 		calculate_inits_and_adj_vp(pipe_ctx, &recout_skip);
882b2d0a103SDmytro Laktyushkin 
8834562236bSHarry Wentland 	dm_logger_write(pipe_ctx->stream->ctx->logger, LOG_SCALER,
8844562236bSHarry Wentland 				"%s: Viewport:\nheight:%d width:%d x:%d "
8854562236bSHarry Wentland 				"y:%d\n dst_rect:\nheight:%d width:%d x:%d "
8864562236bSHarry Wentland 				"y:%d\n",
8874562236bSHarry Wentland 				__func__,
8886702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.height,
8896702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.width,
8906702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.x,
8916702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.y,
8923be5262eSHarry Wentland 				plane_state->dst_rect.height,
8933be5262eSHarry Wentland 				plane_state->dst_rect.width,
8943be5262eSHarry Wentland 				plane_state->dst_rect.x,
8953be5262eSHarry Wentland 				plane_state->dst_rect.y);
8964562236bSHarry Wentland 
8974562236bSHarry Wentland 	return res;
8984562236bSHarry Wentland }
8994562236bSHarry Wentland 
9004562236bSHarry Wentland 
9014562236bSHarry Wentland enum dc_status resource_build_scaling_params_for_context(
902fb3466a4SBhawanpreet Lakha 	const struct dc  *dc,
903608ac7bbSJerry Zuo 	struct dc_state *context)
9044562236bSHarry Wentland {
9054562236bSHarry Wentland 	int i;
9064562236bSHarry Wentland 
9074562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
9083be5262eSHarry Wentland 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
9094562236bSHarry Wentland 				context->res_ctx.pipe_ctx[i].stream != NULL)
910b2d0a103SDmytro Laktyushkin 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
911f84a8161STony Cheng 				return DC_FAIL_SCALING;
9124562236bSHarry Wentland 	}
9134562236bSHarry Wentland 
9144562236bSHarry Wentland 	return DC_OK;
9154562236bSHarry Wentland }
9164562236bSHarry Wentland 
917a2b8659dSTony Cheng struct pipe_ctx *find_idle_secondary_pipe(
918a2b8659dSTony Cheng 		struct resource_context *res_ctx,
919a2b8659dSTony Cheng 		const struct resource_pool *pool)
9204562236bSHarry Wentland {
9214562236bSHarry Wentland 	int i;
9224562236bSHarry Wentland 	struct pipe_ctx *secondary_pipe = NULL;
9234562236bSHarry Wentland 
9244562236bSHarry Wentland 	/*
9254562236bSHarry Wentland 	 * search backwards for the second pipe to keep pipe
9264562236bSHarry Wentland 	 * assignment more consistent
9274562236bSHarry Wentland 	 */
9284562236bSHarry Wentland 
929a2b8659dSTony Cheng 	for (i = pool->pipe_count - 1; i >= 0; i--) {
9304562236bSHarry Wentland 		if (res_ctx->pipe_ctx[i].stream == NULL) {
9314562236bSHarry Wentland 			secondary_pipe = &res_ctx->pipe_ctx[i];
9324562236bSHarry Wentland 			secondary_pipe->pipe_idx = i;
9334562236bSHarry Wentland 			break;
9344562236bSHarry Wentland 		}
9354562236bSHarry Wentland 	}
9364562236bSHarry Wentland 
9374562236bSHarry Wentland 
9384562236bSHarry Wentland 	return secondary_pipe;
9394562236bSHarry Wentland }
9404562236bSHarry Wentland 
9414562236bSHarry Wentland struct pipe_ctx *resource_get_head_pipe_for_stream(
9424562236bSHarry Wentland 		struct resource_context *res_ctx,
9430971c40eSHarry Wentland 		struct dc_stream_state *stream)
9444562236bSHarry Wentland {
9454562236bSHarry Wentland 	int i;
946a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
9474562236bSHarry Wentland 		if (res_ctx->pipe_ctx[i].stream == stream &&
9481dc90497SAndrey Grodzovsky 				!res_ctx->pipe_ctx[i].top_pipe) {
9494562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
9504562236bSHarry Wentland 			break;
9514562236bSHarry Wentland 		}
9524562236bSHarry Wentland 	}
9534562236bSHarry Wentland 	return NULL;
9544562236bSHarry Wentland }
9554562236bSHarry Wentland 
95619f89e23SAndrey Grodzovsky static struct pipe_ctx *resource_get_tail_pipe_for_stream(
95719f89e23SAndrey Grodzovsky 		struct resource_context *res_ctx,
95819f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream)
95919f89e23SAndrey Grodzovsky {
96019f89e23SAndrey Grodzovsky 	struct pipe_ctx *head_pipe, *tail_pipe;
96119f89e23SAndrey Grodzovsky 	head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
96219f89e23SAndrey Grodzovsky 
96319f89e23SAndrey Grodzovsky 	if (!head_pipe)
96419f89e23SAndrey Grodzovsky 		return NULL;
96519f89e23SAndrey Grodzovsky 
96619f89e23SAndrey Grodzovsky 	tail_pipe = head_pipe->bottom_pipe;
96719f89e23SAndrey Grodzovsky 
96819f89e23SAndrey Grodzovsky 	while (tail_pipe) {
96919f89e23SAndrey Grodzovsky 		head_pipe = tail_pipe;
97019f89e23SAndrey Grodzovsky 		tail_pipe = tail_pipe->bottom_pipe;
97119f89e23SAndrey Grodzovsky 	}
97219f89e23SAndrey Grodzovsky 
97319f89e23SAndrey Grodzovsky 	return head_pipe;
97419f89e23SAndrey Grodzovsky }
97519f89e23SAndrey Grodzovsky 
9764562236bSHarry Wentland /*
977ab2541b6SAric Cyr  * A free_pipe for a stream is defined here as a pipe
978ab2541b6SAric Cyr  * that has no surface attached yet
9794562236bSHarry Wentland  */
980ab2541b6SAric Cyr static struct pipe_ctx *acquire_free_pipe_for_stream(
981608ac7bbSJerry Zuo 		struct dc_state *context,
982a2b8659dSTony Cheng 		const struct resource_pool *pool,
9830971c40eSHarry Wentland 		struct dc_stream_state *stream)
9844562236bSHarry Wentland {
9854562236bSHarry Wentland 	int i;
986745cc746SDmytro Laktyushkin 	struct resource_context *res_ctx = &context->res_ctx;
9874562236bSHarry Wentland 
9884562236bSHarry Wentland 	struct pipe_ctx *head_pipe = NULL;
9894562236bSHarry Wentland 
9904562236bSHarry Wentland 	/* Find head pipe, which has the back end set up*/
9914562236bSHarry Wentland 
9924562236bSHarry Wentland 	head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
9934562236bSHarry Wentland 
9944562236bSHarry Wentland 	if (!head_pipe)
9954562236bSHarry Wentland 		ASSERT(0);
9964562236bSHarry Wentland 
9973be5262eSHarry Wentland 	if (!head_pipe->plane_state)
9984562236bSHarry Wentland 		return head_pipe;
9994562236bSHarry Wentland 
10004562236bSHarry Wentland 	/* Re-use pipe already acquired for this stream if available*/
1001a2b8659dSTony Cheng 	for (i = pool->pipe_count - 1; i >= 0; i--) {
10024562236bSHarry Wentland 		if (res_ctx->pipe_ctx[i].stream == stream &&
10033be5262eSHarry Wentland 				!res_ctx->pipe_ctx[i].plane_state) {
10044562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
10054562236bSHarry Wentland 		}
10064562236bSHarry Wentland 	}
10074562236bSHarry Wentland 
10084562236bSHarry Wentland 	/*
10094562236bSHarry Wentland 	 * At this point we have no re-useable pipe for this stream and we need
10104562236bSHarry Wentland 	 * to acquire an idle one to satisfy the request
10114562236bSHarry Wentland 	 */
10124562236bSHarry Wentland 
1013a2b8659dSTony Cheng 	if (!pool->funcs->acquire_idle_pipe_for_layer)
10144562236bSHarry Wentland 		return NULL;
10154562236bSHarry Wentland 
1016a2b8659dSTony Cheng 	return pool->funcs->acquire_idle_pipe_for_layer(context, pool, stream);
10174562236bSHarry Wentland 
10184562236bSHarry Wentland }
10194562236bSHarry Wentland 
10200f9a536fSDmytro Laktyushkin #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
10210f9a536fSDmytro Laktyushkin static int acquire_first_split_pipe(
10220f9a536fSDmytro Laktyushkin 		struct resource_context *res_ctx,
10230f9a536fSDmytro Laktyushkin 		const struct resource_pool *pool,
10240971c40eSHarry Wentland 		struct dc_stream_state *stream)
10250f9a536fSDmytro Laktyushkin {
10260f9a536fSDmytro Laktyushkin 	int i;
10270f9a536fSDmytro Laktyushkin 
10280f9a536fSDmytro Laktyushkin 	for (i = 0; i < pool->pipe_count; i++) {
10290f9a536fSDmytro Laktyushkin 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
10300f9a536fSDmytro Laktyushkin 
10310f9a536fSDmytro Laktyushkin 		if (pipe_ctx->top_pipe &&
10323be5262eSHarry Wentland 				pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state) {
10330f9a536fSDmytro Laktyushkin 			pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
10340f9a536fSDmytro Laktyushkin 			if (pipe_ctx->bottom_pipe)
10350f9a536fSDmytro Laktyushkin 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
10360f9a536fSDmytro Laktyushkin 
10370f9a536fSDmytro Laktyushkin 			memset(pipe_ctx, 0, sizeof(*pipe_ctx));
10386b670fa9SHarry Wentland 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
10398feabd03SYue Hin Lau 			pipe_ctx->plane_res.hubp = pool->hubps[i];
104086a66c4eSHarry Wentland 			pipe_ctx->plane_res.ipp = pool->ipps[i];
1041d94585a0SYue Hin Lau 			pipe_ctx->plane_res.dpp = pool->dpps[i];
1042a6a6cb34SHarry Wentland 			pipe_ctx->stream_res.opp = pool->opps[i];
10430f9a536fSDmytro Laktyushkin 			pipe_ctx->pipe_idx = i;
10440f9a536fSDmytro Laktyushkin 
10450f9a536fSDmytro Laktyushkin 			pipe_ctx->stream = stream;
10460f9a536fSDmytro Laktyushkin 			return i;
10470f9a536fSDmytro Laktyushkin 		}
10480f9a536fSDmytro Laktyushkin 	}
10490f9a536fSDmytro Laktyushkin 	return -1;
10500f9a536fSDmytro Laktyushkin }
10510f9a536fSDmytro Laktyushkin #endif
10520f9a536fSDmytro Laktyushkin 
105319f89e23SAndrey Grodzovsky bool dc_add_plane_to_context(
105419f89e23SAndrey Grodzovsky 		const struct dc *dc,
10550971c40eSHarry Wentland 		struct dc_stream_state *stream,
105619f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1057608ac7bbSJerry Zuo 		struct dc_state *context)
10584562236bSHarry Wentland {
10594562236bSHarry Wentland 	int i;
106019f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
106119f89e23SAndrey Grodzovsky 	struct pipe_ctx *head_pipe, *tail_pipe, *free_pipe;
1062ab2541b6SAric Cyr 	struct dc_stream_status *stream_status = NULL;
10634562236bSHarry Wentland 
1064ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++)
10654fa086b9SLeo (Sunpeng) Li 		if (context->streams[i] == stream) {
1066ab2541b6SAric Cyr 			stream_status = &context->stream_status[i];
10674562236bSHarry Wentland 			break;
10684562236bSHarry Wentland 		}
1069ab2541b6SAric Cyr 	if (stream_status == NULL) {
107019f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to attach surface!\n");
107119f89e23SAndrey Grodzovsky 		return false;
107219f89e23SAndrey Grodzovsky 	}
107319f89e23SAndrey Grodzovsky 
107419f89e23SAndrey Grodzovsky 
107519f89e23SAndrey Grodzovsky 	if (stream_status->plane_count == MAX_SURFACE_NUM) {
107619f89e23SAndrey Grodzovsky 		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
107719f89e23SAndrey Grodzovsky 				plane_state, MAX_SURFACE_NUM);
107819f89e23SAndrey Grodzovsky 		return false;
107919f89e23SAndrey Grodzovsky 	}
108019f89e23SAndrey Grodzovsky 
108119f89e23SAndrey Grodzovsky 	head_pipe = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
108219f89e23SAndrey Grodzovsky 
108319f89e23SAndrey Grodzovsky 	if (!head_pipe) {
108419f89e23SAndrey Grodzovsky 		dm_error("Head pipe not found for stream_state %p !\n", stream);
10854562236bSHarry Wentland 		return false;
10864562236bSHarry Wentland 	}
10874562236bSHarry Wentland 
108819f89e23SAndrey Grodzovsky 	free_pipe = acquire_free_pipe_for_stream(context, pool, stream);
10894562236bSHarry Wentland 
10900f9a536fSDmytro Laktyushkin #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
10910f9a536fSDmytro Laktyushkin 	if (!free_pipe) {
10920f9a536fSDmytro Laktyushkin 		int pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
10930f9a536fSDmytro Laktyushkin 		if (pipe_idx >= 0)
10940f9a536fSDmytro Laktyushkin 			free_pipe = &context->res_ctx.pipe_ctx[pipe_idx];
10950f9a536fSDmytro Laktyushkin 	}
10960f9a536fSDmytro Laktyushkin #endif
1097abb4986eSAndrew Jiang 	if (!free_pipe)
10984562236bSHarry Wentland 		return false;
10994562236bSHarry Wentland 
1100abb4986eSAndrew Jiang 	/* retain new surfaces */
1101abb4986eSAndrew Jiang 	dc_plane_state_retain(plane_state);
11023be5262eSHarry Wentland 	free_pipe->plane_state = plane_state;
11034562236bSHarry Wentland 
110419f89e23SAndrey Grodzovsky 	if (head_pipe != free_pipe) {
110519f89e23SAndrey Grodzovsky 
110619f89e23SAndrey Grodzovsky 		tail_pipe = resource_get_tail_pipe_for_stream(&context->res_ctx, stream);
110719f89e23SAndrey Grodzovsky 		ASSERT(tail_pipe);
110819f89e23SAndrey Grodzovsky 
11096b670fa9SHarry Wentland 		free_pipe->stream_res.tg = tail_pipe->stream_res.tg;
1110a6a6cb34SHarry Wentland 		free_pipe->stream_res.opp = tail_pipe->stream_res.opp;
11118e9c4c8cSHarry Wentland 		free_pipe->stream_res.stream_enc = tail_pipe->stream_res.stream_enc;
1112afaacef4SHarry Wentland 		free_pipe->stream_res.audio = tail_pipe->stream_res.audio;
1113cfe4645eSDmytro Laktyushkin 		free_pipe->clock_source = tail_pipe->clock_source;
11144562236bSHarry Wentland 		free_pipe->top_pipe = tail_pipe;
11154562236bSHarry Wentland 		tail_pipe->bottom_pipe = free_pipe;
11164562236bSHarry Wentland 	}
11174562236bSHarry Wentland 
11184562236bSHarry Wentland 	/* assign new surfaces*/
111919f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = plane_state;
11204562236bSHarry Wentland 
112119f89e23SAndrey Grodzovsky 	stream_status->plane_count++;
11224562236bSHarry Wentland 
11234562236bSHarry Wentland 	return true;
11244562236bSHarry Wentland }
11254562236bSHarry Wentland 
112619f89e23SAndrey Grodzovsky bool dc_remove_plane_from_context(
112719f89e23SAndrey Grodzovsky 		const struct dc *dc,
112819f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
112919f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1130608ac7bbSJerry Zuo 		struct dc_state *context)
113119f89e23SAndrey Grodzovsky {
113219f89e23SAndrey Grodzovsky 	int i;
113319f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
113419f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
113519f89e23SAndrey Grodzovsky 
113619f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
113719f89e23SAndrey Grodzovsky 		if (context->streams[i] == stream) {
113819f89e23SAndrey Grodzovsky 			stream_status = &context->stream_status[i];
113919f89e23SAndrey Grodzovsky 			break;
114019f89e23SAndrey Grodzovsky 		}
114119f89e23SAndrey Grodzovsky 
114219f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
114319f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to remove plane.\n");
114419f89e23SAndrey Grodzovsky 		return false;
114519f89e23SAndrey Grodzovsky 	}
114619f89e23SAndrey Grodzovsky 
114719f89e23SAndrey Grodzovsky 	/* release pipe for plane*/
114819f89e23SAndrey Grodzovsky 	for (i = pool->pipe_count - 1; i >= 0; i--) {
114919f89e23SAndrey Grodzovsky 		struct pipe_ctx *pipe_ctx;
115019f89e23SAndrey Grodzovsky 
115119f89e23SAndrey Grodzovsky 		if (context->res_ctx.pipe_ctx[i].plane_state == plane_state) {
115219f89e23SAndrey Grodzovsky 			pipe_ctx = &context->res_ctx.pipe_ctx[i];
115319f89e23SAndrey Grodzovsky 
115419f89e23SAndrey Grodzovsky 			if (pipe_ctx->top_pipe)
115519f89e23SAndrey Grodzovsky 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
115619f89e23SAndrey Grodzovsky 
115719f89e23SAndrey Grodzovsky 			/* Second condition is to avoid setting NULL to top pipe
115819f89e23SAndrey Grodzovsky 			 * of tail pipe making it look like head pipe in subsequent
115919f89e23SAndrey Grodzovsky 			 * deletes
116019f89e23SAndrey Grodzovsky 			 */
116119f89e23SAndrey Grodzovsky 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
116219f89e23SAndrey Grodzovsky 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
116319f89e23SAndrey Grodzovsky 
116419f89e23SAndrey Grodzovsky 			/*
116519f89e23SAndrey Grodzovsky 			 * For head pipe detach surfaces from pipe for tail
116619f89e23SAndrey Grodzovsky 			 * pipe just zero it out
116719f89e23SAndrey Grodzovsky 			 */
116819f89e23SAndrey Grodzovsky 			if (!pipe_ctx->top_pipe) {
116919f89e23SAndrey Grodzovsky 				pipe_ctx->plane_state = NULL;
117019f89e23SAndrey Grodzovsky 				pipe_ctx->bottom_pipe = NULL;
117119f89e23SAndrey Grodzovsky 			} else  {
117219f89e23SAndrey Grodzovsky 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
117319f89e23SAndrey Grodzovsky 			}
117419f89e23SAndrey Grodzovsky 		}
117519f89e23SAndrey Grodzovsky 	}
117619f89e23SAndrey Grodzovsky 
117719f89e23SAndrey Grodzovsky 
117819f89e23SAndrey Grodzovsky 	for (i = 0; i < stream_status->plane_count; i++) {
117919f89e23SAndrey Grodzovsky 		if (stream_status->plane_states[i] == plane_state) {
118019f89e23SAndrey Grodzovsky 
118119f89e23SAndrey Grodzovsky 			dc_plane_state_release(stream_status->plane_states[i]);
118219f89e23SAndrey Grodzovsky 			break;
118319f89e23SAndrey Grodzovsky 		}
118419f89e23SAndrey Grodzovsky 	}
118519f89e23SAndrey Grodzovsky 
118619f89e23SAndrey Grodzovsky 	if (i == stream_status->plane_count) {
118719f89e23SAndrey Grodzovsky 		dm_error("Existing plane_state not found; failed to detach it!\n");
118819f89e23SAndrey Grodzovsky 		return false;
118919f89e23SAndrey Grodzovsky 	}
119019f89e23SAndrey Grodzovsky 
119119f89e23SAndrey Grodzovsky 	stream_status->plane_count--;
119219f89e23SAndrey Grodzovsky 
1193abb4986eSAndrew Jiang 	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
1194abb4986eSAndrew Jiang 	for (; i < stream_status->plane_count; i++)
119519f89e23SAndrey Grodzovsky 		stream_status->plane_states[i] = stream_status->plane_states[i + 1];
119619f89e23SAndrey Grodzovsky 
119719f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = NULL;
119819f89e23SAndrey Grodzovsky 
119919f89e23SAndrey Grodzovsky 	return true;
120019f89e23SAndrey Grodzovsky }
120119f89e23SAndrey Grodzovsky 
120219f89e23SAndrey Grodzovsky bool dc_rem_all_planes_for_stream(
120319f89e23SAndrey Grodzovsky 		const struct dc *dc,
120419f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
1205608ac7bbSJerry Zuo 		struct dc_state *context)
120619f89e23SAndrey Grodzovsky {
120719f89e23SAndrey Grodzovsky 	int i, old_plane_count;
120819f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
120919f89e23SAndrey Grodzovsky 	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
121019f89e23SAndrey Grodzovsky 
121119f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
121219f89e23SAndrey Grodzovsky 			if (context->streams[i] == stream) {
121319f89e23SAndrey Grodzovsky 				stream_status = &context->stream_status[i];
121419f89e23SAndrey Grodzovsky 				break;
121519f89e23SAndrey Grodzovsky 			}
121619f89e23SAndrey Grodzovsky 
121719f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
121819f89e23SAndrey Grodzovsky 		dm_error("Existing stream %p not found!\n", stream);
121919f89e23SAndrey Grodzovsky 		return false;
122019f89e23SAndrey Grodzovsky 	}
122119f89e23SAndrey Grodzovsky 
122219f89e23SAndrey Grodzovsky 	old_plane_count = stream_status->plane_count;
122319f89e23SAndrey Grodzovsky 
122419f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
122519f89e23SAndrey Grodzovsky 		del_planes[i] = stream_status->plane_states[i];
122619f89e23SAndrey Grodzovsky 
122719f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
122819f89e23SAndrey Grodzovsky 		if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
122919f89e23SAndrey Grodzovsky 			return false;
123019f89e23SAndrey Grodzovsky 
123119f89e23SAndrey Grodzovsky 	return true;
123219f89e23SAndrey Grodzovsky }
123319f89e23SAndrey Grodzovsky 
123419f89e23SAndrey Grodzovsky static bool add_all_planes_for_stream(
123519f89e23SAndrey Grodzovsky 		const struct dc *dc,
123619f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
123719f89e23SAndrey Grodzovsky 		const struct dc_validation_set set[],
123819f89e23SAndrey Grodzovsky 		int set_count,
1239608ac7bbSJerry Zuo 		struct dc_state *context)
124019f89e23SAndrey Grodzovsky {
124119f89e23SAndrey Grodzovsky 	int i, j;
124219f89e23SAndrey Grodzovsky 
124319f89e23SAndrey Grodzovsky 	for (i = 0; i < set_count; i++)
124419f89e23SAndrey Grodzovsky 		if (set[i].stream == stream)
124519f89e23SAndrey Grodzovsky 			break;
124619f89e23SAndrey Grodzovsky 
124719f89e23SAndrey Grodzovsky 	if (i == set_count) {
124819f89e23SAndrey Grodzovsky 		dm_error("Stream %p not found in set!\n", stream);
124919f89e23SAndrey Grodzovsky 		return false;
125019f89e23SAndrey Grodzovsky 	}
125119f89e23SAndrey Grodzovsky 
125219f89e23SAndrey Grodzovsky 	for (j = 0; j < set[i].plane_count; j++)
125319f89e23SAndrey Grodzovsky 		if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
125419f89e23SAndrey Grodzovsky 			return false;
125519f89e23SAndrey Grodzovsky 
125619f89e23SAndrey Grodzovsky 	return true;
125719f89e23SAndrey Grodzovsky }
125819f89e23SAndrey Grodzovsky 
125919f89e23SAndrey Grodzovsky bool dc_add_all_planes_for_stream(
126019f89e23SAndrey Grodzovsky 		const struct dc *dc,
126119f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
126219f89e23SAndrey Grodzovsky 		struct dc_plane_state * const *plane_states,
126319f89e23SAndrey Grodzovsky 		int plane_count,
1264608ac7bbSJerry Zuo 		struct dc_state *context)
126519f89e23SAndrey Grodzovsky {
126619f89e23SAndrey Grodzovsky 	struct dc_validation_set set;
126719f89e23SAndrey Grodzovsky 	int i;
126819f89e23SAndrey Grodzovsky 
126919f89e23SAndrey Grodzovsky 	set.stream = stream;
127019f89e23SAndrey Grodzovsky 	set.plane_count = plane_count;
127119f89e23SAndrey Grodzovsky 
127219f89e23SAndrey Grodzovsky 	for (i = 0; i < plane_count; i++)
127319f89e23SAndrey Grodzovsky 		set.plane_states[i] = plane_states[i];
127419f89e23SAndrey Grodzovsky 
127519f89e23SAndrey Grodzovsky 	return add_all_planes_for_stream(dc, stream, &set, 1, context);
127619f89e23SAndrey Grodzovsky }
127719f89e23SAndrey Grodzovsky 
127819f89e23SAndrey Grodzovsky 
12794562236bSHarry Wentland 
12800971c40eSHarry Wentland static bool is_timing_changed(struct dc_stream_state *cur_stream,
12810971c40eSHarry Wentland 		struct dc_stream_state *new_stream)
12824562236bSHarry Wentland {
12834562236bSHarry Wentland 	if (cur_stream == NULL)
12844562236bSHarry Wentland 		return true;
12854562236bSHarry Wentland 
12864562236bSHarry Wentland 	/* If sink pointer changed, it means this is a hotplug, we should do
12874562236bSHarry Wentland 	 * full hw setting.
12884562236bSHarry Wentland 	 */
12894562236bSHarry Wentland 	if (cur_stream->sink != new_stream->sink)
12904562236bSHarry Wentland 		return true;
12914562236bSHarry Wentland 
12924562236bSHarry Wentland 	/* If output color space is changed, need to reprogram info frames */
12934fa086b9SLeo (Sunpeng) Li 	if (cur_stream->output_color_space != new_stream->output_color_space)
12944562236bSHarry Wentland 		return true;
12954562236bSHarry Wentland 
12964562236bSHarry Wentland 	return memcmp(
12974fa086b9SLeo (Sunpeng) Li 		&cur_stream->timing,
12984fa086b9SLeo (Sunpeng) Li 		&new_stream->timing,
12994562236bSHarry Wentland 		sizeof(struct dc_crtc_timing)) != 0;
13004562236bSHarry Wentland }
13014562236bSHarry Wentland 
13024562236bSHarry Wentland static bool are_stream_backends_same(
13030971c40eSHarry Wentland 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
13044562236bSHarry Wentland {
13054562236bSHarry Wentland 	if (stream_a == stream_b)
13064562236bSHarry Wentland 		return true;
13074562236bSHarry Wentland 
13084562236bSHarry Wentland 	if (stream_a == NULL || stream_b == NULL)
13094562236bSHarry Wentland 		return false;
13104562236bSHarry Wentland 
13114562236bSHarry Wentland 	if (is_timing_changed(stream_a, stream_b))
13124562236bSHarry Wentland 		return false;
13134562236bSHarry Wentland 
13144562236bSHarry Wentland 	return true;
13154562236bSHarry Wentland }
13164562236bSHarry Wentland 
1317d54d29dbSBhawanpreet Lakha bool dc_is_stream_unchanged(
13180971c40eSHarry Wentland 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
13194562236bSHarry Wentland {
13204562236bSHarry Wentland 
13214562236bSHarry Wentland 	if (!are_stream_backends_same(old_stream, stream))
13224562236bSHarry Wentland 		return false;
13234562236bSHarry Wentland 
13244562236bSHarry Wentland 	return true;
13254562236bSHarry Wentland }
13264562236bSHarry Wentland 
13279a5d9c48SLeo (Sunpeng) Li bool dc_is_stream_scaling_unchanged(
13289a5d9c48SLeo (Sunpeng) Li 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
13299a5d9c48SLeo (Sunpeng) Li {
13309a5d9c48SLeo (Sunpeng) Li 	if (old_stream == stream)
13319a5d9c48SLeo (Sunpeng) Li 		return true;
13329a5d9c48SLeo (Sunpeng) Li 
13339a5d9c48SLeo (Sunpeng) Li 	if (old_stream == NULL || stream == NULL)
13349a5d9c48SLeo (Sunpeng) Li 		return false;
13359a5d9c48SLeo (Sunpeng) Li 
13369a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->src,
13379a5d9c48SLeo (Sunpeng) Li 			&stream->src,
13389a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
13399a5d9c48SLeo (Sunpeng) Li 		return false;
13409a5d9c48SLeo (Sunpeng) Li 
13419a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->dst,
13429a5d9c48SLeo (Sunpeng) Li 			&stream->dst,
13439a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
13449a5d9c48SLeo (Sunpeng) Li 		return false;
13459a5d9c48SLeo (Sunpeng) Li 
13469a5d9c48SLeo (Sunpeng) Li 	return true;
13479a5d9c48SLeo (Sunpeng) Li }
13489a5d9c48SLeo (Sunpeng) Li 
13494562236bSHarry Wentland /* Maximum TMDS single link pixel clock 165MHz */
13504562236bSHarry Wentland #define TMDS_MAX_PIXEL_CLOCK_IN_KHZ 165000
13514562236bSHarry Wentland 
13521dc90497SAndrey Grodzovsky static void update_stream_engine_usage(
13534562236bSHarry Wentland 		struct resource_context *res_ctx,
1354a2b8659dSTony Cheng 		const struct resource_pool *pool,
13551dc90497SAndrey Grodzovsky 		struct stream_encoder *stream_enc,
13561dc90497SAndrey Grodzovsky 		bool acquired)
13574562236bSHarry Wentland {
13584562236bSHarry Wentland 	int i;
13594562236bSHarry Wentland 
1360a2b8659dSTony Cheng 	for (i = 0; i < pool->stream_enc_count; i++) {
1361a2b8659dSTony Cheng 		if (pool->stream_enc[i] == stream_enc)
13621dc90497SAndrey Grodzovsky 			res_ctx->is_stream_enc_acquired[i] = acquired;
13634562236bSHarry Wentland 	}
13644562236bSHarry Wentland }
13654562236bSHarry Wentland 
13664562236bSHarry Wentland /* TODO: release audio object */
13674176664bSCharlene Liu void update_audio_usage(
13684562236bSHarry Wentland 		struct resource_context *res_ctx,
1369a2b8659dSTony Cheng 		const struct resource_pool *pool,
13701dc90497SAndrey Grodzovsky 		struct audio *audio,
13711dc90497SAndrey Grodzovsky 		bool acquired)
13724562236bSHarry Wentland {
13734562236bSHarry Wentland 	int i;
1374a2b8659dSTony Cheng 	for (i = 0; i < pool->audio_count; i++) {
1375a2b8659dSTony Cheng 		if (pool->audios[i] == audio)
13761dc90497SAndrey Grodzovsky 			res_ctx->is_audio_acquired[i] = acquired;
13774562236bSHarry Wentland 	}
13784562236bSHarry Wentland }
13794562236bSHarry Wentland 
13804562236bSHarry Wentland static int acquire_first_free_pipe(
13814562236bSHarry Wentland 		struct resource_context *res_ctx,
1382a2b8659dSTony Cheng 		const struct resource_pool *pool,
13830971c40eSHarry Wentland 		struct dc_stream_state *stream)
13844562236bSHarry Wentland {
13854562236bSHarry Wentland 	int i;
13864562236bSHarry Wentland 
1387a2b8659dSTony Cheng 	for (i = 0; i < pool->pipe_count; i++) {
13884562236bSHarry Wentland 		if (!res_ctx->pipe_ctx[i].stream) {
13894562236bSHarry Wentland 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
13904562236bSHarry Wentland 
13916b670fa9SHarry Wentland 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
139286a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi = pool->mis[i];
13938feabd03SYue Hin Lau 			pipe_ctx->plane_res.hubp = pool->hubps[i];
139486a66c4eSHarry Wentland 			pipe_ctx->plane_res.ipp = pool->ipps[i];
139586a66c4eSHarry Wentland 			pipe_ctx->plane_res.xfm = pool->transforms[i];
1396d94585a0SYue Hin Lau 			pipe_ctx->plane_res.dpp = pool->dpps[i];
1397a6a6cb34SHarry Wentland 			pipe_ctx->stream_res.opp = pool->opps[i];
13984562236bSHarry Wentland 			pipe_ctx->pipe_idx = i;
13994562236bSHarry Wentland 
1400ff5ef992SAlex Deucher 
14014562236bSHarry Wentland 			pipe_ctx->stream = stream;
14024562236bSHarry Wentland 			return i;
14034562236bSHarry Wentland 		}
14044562236bSHarry Wentland 	}
14054562236bSHarry Wentland 	return -1;
14064562236bSHarry Wentland }
14074562236bSHarry Wentland 
14084562236bSHarry Wentland static struct stream_encoder *find_first_free_match_stream_enc_for_link(
14094562236bSHarry Wentland 		struct resource_context *res_ctx,
1410a2b8659dSTony Cheng 		const struct resource_pool *pool,
14110971c40eSHarry Wentland 		struct dc_stream_state *stream)
14124562236bSHarry Wentland {
14134562236bSHarry Wentland 	int i;
14144562236bSHarry Wentland 	int j = -1;
1415d0778ebfSHarry Wentland 	struct dc_link *link = stream->sink->link;
14164562236bSHarry Wentland 
1417a2b8659dSTony Cheng 	for (i = 0; i < pool->stream_enc_count; i++) {
14184562236bSHarry Wentland 		if (!res_ctx->is_stream_enc_acquired[i] &&
1419a2b8659dSTony Cheng 				pool->stream_enc[i]) {
14204562236bSHarry Wentland 			/* Store first available for MST second display
14214562236bSHarry Wentland 			 * in daisy chain use case */
14224562236bSHarry Wentland 			j = i;
1423a2b8659dSTony Cheng 			if (pool->stream_enc[i]->id ==
14244562236bSHarry Wentland 					link->link_enc->preferred_engine)
1425a2b8659dSTony Cheng 				return pool->stream_enc[i];
14264562236bSHarry Wentland 		}
14274562236bSHarry Wentland 	}
14284562236bSHarry Wentland 
14294562236bSHarry Wentland 	/*
14304562236bSHarry Wentland 	 * below can happen in cases when stream encoder is acquired:
14314562236bSHarry Wentland 	 * 1) for second MST display in chain, so preferred engine already
14324562236bSHarry Wentland 	 * acquired;
14334562236bSHarry Wentland 	 * 2) for another link, which preferred engine already acquired by any
14344562236bSHarry Wentland 	 * MST configuration.
14354562236bSHarry Wentland 	 *
14364562236bSHarry Wentland 	 * If signal is of DP type and preferred engine not found, return last available
14374562236bSHarry Wentland 	 *
14384562236bSHarry Wentland 	 * TODO - This is just a patch up and a generic solution is
14394562236bSHarry Wentland 	 * required for non DP connectors.
14404562236bSHarry Wentland 	 */
14414562236bSHarry Wentland 
14424562236bSHarry Wentland 	if (j >= 0 && dc_is_dp_signal(stream->signal))
1443a2b8659dSTony Cheng 		return pool->stream_enc[j];
14444562236bSHarry Wentland 
14454562236bSHarry Wentland 	return NULL;
14464562236bSHarry Wentland }
14474562236bSHarry Wentland 
1448a2b8659dSTony Cheng static struct audio *find_first_free_audio(
1449a2b8659dSTony Cheng 		struct resource_context *res_ctx,
1450a2b8659dSTony Cheng 		const struct resource_pool *pool)
14514562236bSHarry Wentland {
14524562236bSHarry Wentland 	int i;
1453a2b8659dSTony Cheng 	for (i = 0; i < pool->audio_count; i++) {
14544176664bSCharlene Liu 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
1455a2b8659dSTony Cheng 			return pool->audios[i];
14564562236bSHarry Wentland 		}
14574562236bSHarry Wentland 	}
145866bfd4fdSCharlene Liu 	/*not found the matching one, first come first serve*/
14594176664bSCharlene Liu 	for (i = 0; i < pool->audio_count; i++) {
14604176664bSCharlene Liu 		if (res_ctx->is_audio_acquired[i] == false) {
14614176664bSCharlene Liu 			return pool->audios[i];
14624176664bSCharlene Liu 		}
14634176664bSCharlene Liu 	}
14644562236bSHarry Wentland 	return 0;
14654562236bSHarry Wentland }
14664562236bSHarry Wentland 
14674562236bSHarry Wentland bool resource_is_stream_unchanged(
1468608ac7bbSJerry Zuo 	struct dc_state *old_context, struct dc_stream_state *stream)
14694562236bSHarry Wentland {
1470ab2541b6SAric Cyr 	int i;
14714562236bSHarry Wentland 
1472ab2541b6SAric Cyr 	for (i = 0; i < old_context->stream_count; i++) {
14730971c40eSHarry Wentland 		struct dc_stream_state *old_stream = old_context->streams[i];
14744562236bSHarry Wentland 
14754562236bSHarry Wentland 		if (are_stream_backends_same(old_stream, stream))
14764562236bSHarry Wentland 				return true;
14774562236bSHarry Wentland 	}
14784562236bSHarry Wentland 
14794562236bSHarry Wentland 	return false;
14804562236bSHarry Wentland }
14814562236bSHarry Wentland 
148213ab1b44SYongqiang Sun enum dc_status dc_add_stream_to_ctx(
14831dc90497SAndrey Grodzovsky 		struct dc *dc,
1484608ac7bbSJerry Zuo 		struct dc_state *new_ctx,
14851dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
14861dc90497SAndrey Grodzovsky {
14871dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
14881dc90497SAndrey Grodzovsky 	enum dc_status res;
14891dc90497SAndrey Grodzovsky 
14901dc90497SAndrey Grodzovsky 	if (new_ctx->stream_count >= dc->res_pool->pipe_count) {
14911dc90497SAndrey Grodzovsky 		DC_ERROR("Max streams reached, can add stream %p !\n", stream);
14921dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
14931dc90497SAndrey Grodzovsky 	}
14941dc90497SAndrey Grodzovsky 
14951dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = stream;
14961dc90497SAndrey Grodzovsky 	dc_stream_retain(stream);
14971dc90497SAndrey Grodzovsky 	new_ctx->stream_count++;
14981dc90497SAndrey Grodzovsky 
14991dc90497SAndrey Grodzovsky 	res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
15001dc90497SAndrey Grodzovsky 	if (res != DC_OK)
15011dc90497SAndrey Grodzovsky 		DC_ERROR("Adding stream %p to context failed with err %d!\n", stream, res);
15021dc90497SAndrey Grodzovsky 
150313ab1b44SYongqiang Sun 	return res;
15041dc90497SAndrey Grodzovsky }
15051dc90497SAndrey Grodzovsky 
15061dc90497SAndrey Grodzovsky bool dc_remove_stream_from_ctx(
15071dc90497SAndrey Grodzovsky 			struct dc *dc,
1508608ac7bbSJerry Zuo 			struct dc_state *new_ctx,
15091dc90497SAndrey Grodzovsky 			struct dc_stream_state *stream)
15101dc90497SAndrey Grodzovsky {
151119f89e23SAndrey Grodzovsky 	int i;
15121dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
15131dc90497SAndrey Grodzovsky 	struct pipe_ctx *del_pipe = NULL;
15141dc90497SAndrey Grodzovsky 
151519f89e23SAndrey Grodzovsky 	/* Release primary pipe */
15161dc90497SAndrey Grodzovsky 	for (i = 0; i < MAX_PIPES; i++) {
151719f89e23SAndrey Grodzovsky 		if (new_ctx->res_ctx.pipe_ctx[i].stream == stream &&
151819f89e23SAndrey Grodzovsky 				!new_ctx->res_ctx.pipe_ctx[i].top_pipe) {
15191dc90497SAndrey Grodzovsky 			del_pipe = &new_ctx->res_ctx.pipe_ctx[i];
15201dc90497SAndrey Grodzovsky 
152119f89e23SAndrey Grodzovsky 			ASSERT(del_pipe->stream_res.stream_enc);
15221dc90497SAndrey Grodzovsky 			update_stream_engine_usage(
15231dc90497SAndrey Grodzovsky 					&new_ctx->res_ctx,
15241dc90497SAndrey Grodzovsky 						dc->res_pool,
15251dc90497SAndrey Grodzovsky 					del_pipe->stream_res.stream_enc,
15261dc90497SAndrey Grodzovsky 					false);
15271dc90497SAndrey Grodzovsky 
15281dc90497SAndrey Grodzovsky 			if (del_pipe->stream_res.audio)
15291dc90497SAndrey Grodzovsky 				update_audio_usage(
15301dc90497SAndrey Grodzovsky 					&new_ctx->res_ctx,
15311dc90497SAndrey Grodzovsky 					dc->res_pool,
15321dc90497SAndrey Grodzovsky 					del_pipe->stream_res.audio,
15331dc90497SAndrey Grodzovsky 					false);
15341dc90497SAndrey Grodzovsky 
15359d0dcecdSHarry Wentland 			resource_unreference_clock_source(&new_ctx->res_ctx,
15369d0dcecdSHarry Wentland 							  dc->res_pool,
15379d0dcecdSHarry Wentland 							  del_pipe->clock_source);
15389d0dcecdSHarry Wentland 
15391dc90497SAndrey Grodzovsky 			memset(del_pipe, 0, sizeof(*del_pipe));
154019f89e23SAndrey Grodzovsky 
154119f89e23SAndrey Grodzovsky 			break;
15421dc90497SAndrey Grodzovsky 		}
15431dc90497SAndrey Grodzovsky 	}
15441dc90497SAndrey Grodzovsky 
15451dc90497SAndrey Grodzovsky 	if (!del_pipe) {
15461dc90497SAndrey Grodzovsky 		DC_ERROR("Pipe not found for stream %p !\n", stream);
15471dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
15481dc90497SAndrey Grodzovsky 	}
15491dc90497SAndrey Grodzovsky 
15501dc90497SAndrey Grodzovsky 	for (i = 0; i < new_ctx->stream_count; i++)
15511dc90497SAndrey Grodzovsky 		if (new_ctx->streams[i] == stream)
15521dc90497SAndrey Grodzovsky 			break;
15531dc90497SAndrey Grodzovsky 
15541dc90497SAndrey Grodzovsky 	if (new_ctx->streams[i] != stream) {
15551dc90497SAndrey Grodzovsky 		DC_ERROR("Context doesn't have stream %p !\n", stream);
15561dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
15571dc90497SAndrey Grodzovsky 	}
15581dc90497SAndrey Grodzovsky 
15591dc90497SAndrey Grodzovsky 	dc_stream_release(new_ctx->streams[i]);
15601dc90497SAndrey Grodzovsky 	new_ctx->stream_count--;
15611dc90497SAndrey Grodzovsky 
15621dc90497SAndrey Grodzovsky 	/* Trim back arrays */
15631dc90497SAndrey Grodzovsky 	for (; i < new_ctx->stream_count; i++) {
15641dc90497SAndrey Grodzovsky 		new_ctx->streams[i] = new_ctx->streams[i + 1];
15651dc90497SAndrey Grodzovsky 		new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
15661dc90497SAndrey Grodzovsky 	}
15671dc90497SAndrey Grodzovsky 
15681dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = NULL;
15691dc90497SAndrey Grodzovsky 	memset(
15701dc90497SAndrey Grodzovsky 			&new_ctx->stream_status[new_ctx->stream_count],
15711dc90497SAndrey Grodzovsky 			0,
15721dc90497SAndrey Grodzovsky 			sizeof(new_ctx->stream_status[0]));
15731dc90497SAndrey Grodzovsky 
15741dc90497SAndrey Grodzovsky 	return DC_OK;
15751dc90497SAndrey Grodzovsky }
15761dc90497SAndrey Grodzovsky 
15774562236bSHarry Wentland static void copy_pipe_ctx(
15784562236bSHarry Wentland 	const struct pipe_ctx *from_pipe_ctx, struct pipe_ctx *to_pipe_ctx)
15794562236bSHarry Wentland {
15803be5262eSHarry Wentland 	struct dc_plane_state *plane_state = to_pipe_ctx->plane_state;
15810971c40eSHarry Wentland 	struct dc_stream_state *stream = to_pipe_ctx->stream;
15824562236bSHarry Wentland 
15834562236bSHarry Wentland 	*to_pipe_ctx = *from_pipe_ctx;
15844562236bSHarry Wentland 	to_pipe_ctx->stream = stream;
15853be5262eSHarry Wentland 	if (plane_state != NULL)
15863be5262eSHarry Wentland 		to_pipe_ctx->plane_state = plane_state;
15874562236bSHarry Wentland }
15884562236bSHarry Wentland 
15890971c40eSHarry Wentland static struct dc_stream_state *find_pll_sharable_stream(
15900971c40eSHarry Wentland 		struct dc_stream_state *stream_needs_pll,
1591608ac7bbSJerry Zuo 		struct dc_state *context)
15924562236bSHarry Wentland {
1593ab2541b6SAric Cyr 	int i;
15944562236bSHarry Wentland 
1595ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
15960971c40eSHarry Wentland 		struct dc_stream_state *stream_has_pll = context->streams[i];
15974562236bSHarry Wentland 
15984562236bSHarry Wentland 		/* We are looking for non dp, non virtual stream */
15994562236bSHarry Wentland 		if (resource_are_streams_timing_synchronizable(
16004562236bSHarry Wentland 			stream_needs_pll, stream_has_pll)
16014562236bSHarry Wentland 			&& !dc_is_dp_signal(stream_has_pll->signal)
1602d0778ebfSHarry Wentland 			&& stream_has_pll->sink->link->connector_signal
16034562236bSHarry Wentland 			!= SIGNAL_TYPE_VIRTUAL)
16044562236bSHarry Wentland 			return stream_has_pll;
1605ab2541b6SAric Cyr 
16064562236bSHarry Wentland 	}
16074562236bSHarry Wentland 
16084562236bSHarry Wentland 	return NULL;
16094562236bSHarry Wentland }
16104562236bSHarry Wentland 
16114562236bSHarry Wentland static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
16124562236bSHarry Wentland {
16134562236bSHarry Wentland 	uint32_t pix_clk = timing->pix_clk_khz;
16144562236bSHarry Wentland 	uint32_t normalized_pix_clk = pix_clk;
16154562236bSHarry Wentland 
16164562236bSHarry Wentland 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
16174562236bSHarry Wentland 		pix_clk /= 2;
1618cc4d99b8SCharlene Liu 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
16194562236bSHarry Wentland 		switch (timing->display_color_depth) {
16204562236bSHarry Wentland 		case COLOR_DEPTH_888:
16214562236bSHarry Wentland 			normalized_pix_clk = pix_clk;
16224562236bSHarry Wentland 			break;
16234562236bSHarry Wentland 		case COLOR_DEPTH_101010:
16244562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 30) / 24;
16254562236bSHarry Wentland 			break;
16264562236bSHarry Wentland 		case COLOR_DEPTH_121212:
16274562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 36) / 24;
16284562236bSHarry Wentland 		break;
16294562236bSHarry Wentland 		case COLOR_DEPTH_161616:
16304562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 48) / 24;
16314562236bSHarry Wentland 		break;
16324562236bSHarry Wentland 		default:
16334562236bSHarry Wentland 			ASSERT(0);
16344562236bSHarry Wentland 		break;
16354562236bSHarry Wentland 		}
1636cc4d99b8SCharlene Liu 	}
16374562236bSHarry Wentland 	return normalized_pix_clk;
16384562236bSHarry Wentland }
16394562236bSHarry Wentland 
16400971c40eSHarry Wentland static void calculate_phy_pix_clks(struct dc_stream_state *stream)
16414562236bSHarry Wentland {
16424562236bSHarry Wentland 	/* update actual pixel clock on all streams */
16434562236bSHarry Wentland 	if (dc_is_hdmi_signal(stream->signal))
16444562236bSHarry Wentland 		stream->phy_pix_clk = get_norm_pix_clk(
16454fa086b9SLeo (Sunpeng) Li 			&stream->timing);
16464562236bSHarry Wentland 	else
16474562236bSHarry Wentland 		stream->phy_pix_clk =
16484fa086b9SLeo (Sunpeng) Li 			stream->timing.pix_clk_khz;
16494562236bSHarry Wentland }
16504562236bSHarry Wentland 
16514562236bSHarry Wentland enum dc_status resource_map_pool_resources(
1652fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
1653608ac7bbSJerry Zuo 		struct dc_state *context,
16541dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
16554562236bSHarry Wentland {
1656a2b8659dSTony Cheng 	const struct resource_pool *pool = dc->res_pool;
16571dc90497SAndrey Grodzovsky 	int i;
16581dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
16591dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = NULL;
16601dc90497SAndrey Grodzovsky 	int pipe_idx = -1;
16614562236bSHarry Wentland 
16621dc90497SAndrey Grodzovsky 	/* TODO Check if this is needed */
16631dc90497SAndrey Grodzovsky 	/*if (!resource_is_stream_unchanged(old_context, stream)) {
1664430ef426SDmytro Laktyushkin 			if (stream != NULL && old_context->streams[i] != NULL) {
16654b679bc3SCharlene Liu 				stream->bit_depth_params =
1666430ef426SDmytro Laktyushkin 						old_context->streams[i]->bit_depth_params;
1667430ef426SDmytro Laktyushkin 				stream->clamping = old_context->streams[i]->clamping;
16684562236bSHarry Wentland 				continue;
16694b679bc3SCharlene Liu 			}
16704b679bc3SCharlene Liu 		}
16714562236bSHarry Wentland 	*/
16724562236bSHarry Wentland 
16734562236bSHarry Wentland 	/* acquire new resources */
16745d11e9fcSDmytro Laktyushkin 	pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
16751dc90497SAndrey Grodzovsky 
167694c6d735SHarry Wentland #ifdef CONFIG_DRM_AMD_DC_DCN1_0
16775d11e9fcSDmytro Laktyushkin 	if (pipe_idx < 0)
167813ab1b44SYongqiang Sun 		pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
167994c6d735SHarry Wentland #endif
168013ab1b44SYongqiang Sun 
16814562236bSHarry Wentland 	if (pipe_idx < 0)
16824562236bSHarry Wentland 		return DC_NO_CONTROLLER_RESOURCE;
16834562236bSHarry Wentland 
16844562236bSHarry Wentland 	pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
16854562236bSHarry Wentland 
16868e9c4c8cSHarry Wentland 	pipe_ctx->stream_res.stream_enc =
16874562236bSHarry Wentland 		find_first_free_match_stream_enc_for_link(
1688a2b8659dSTony Cheng 			&context->res_ctx, pool, stream);
16894562236bSHarry Wentland 
16908e9c4c8cSHarry Wentland 	if (!pipe_ctx->stream_res.stream_enc)
16914562236bSHarry Wentland 		return DC_NO_STREAM_ENG_RESOURCE;
16924562236bSHarry Wentland 
16931dc90497SAndrey Grodzovsky 	update_stream_engine_usage(
1694a2b8659dSTony Cheng 		&context->res_ctx, pool,
16951dc90497SAndrey Grodzovsky 		pipe_ctx->stream_res.stream_enc,
16961dc90497SAndrey Grodzovsky 		true);
16974562236bSHarry Wentland 
16984562236bSHarry Wentland 	/* TODO: Add check if ASIC support and EDID audio */
1699b73a22d3SHarry Wentland 	if (!stream->sink->converter_disable_audio &&
17004562236bSHarry Wentland 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
17014fa086b9SLeo (Sunpeng) Li 	    stream->audio_info.mode_count) {
1702afaacef4SHarry Wentland 		pipe_ctx->stream_res.audio = find_first_free_audio(
1703a2b8659dSTony Cheng 		&context->res_ctx, pool);
17044562236bSHarry Wentland 
17054562236bSHarry Wentland 		/*
17064562236bSHarry Wentland 		 * Audio assigned in order first come first get.
17074562236bSHarry Wentland 		 * There are asics which has number of audio
17084562236bSHarry Wentland 		 * resources less then number of pipes
17094562236bSHarry Wentland 		 */
1710afaacef4SHarry Wentland 		if (pipe_ctx->stream_res.audio)
17111dc90497SAndrey Grodzovsky 			update_audio_usage(&context->res_ctx, pool,
17121dc90497SAndrey Grodzovsky 					   pipe_ctx->stream_res.audio, true);
17134562236bSHarry Wentland 	}
17144562236bSHarry Wentland 
17151dc90497SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
17161dc90497SAndrey Grodzovsky 		if (context->streams[i] == stream) {
17176b670fa9SHarry Wentland 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
17180f0bdca5SWenjing Liu 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->id;
17191dc90497SAndrey Grodzovsky 			return DC_OK;
17204562236bSHarry Wentland 		}
17214562236bSHarry Wentland 
17221dc90497SAndrey Grodzovsky 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
17231dc90497SAndrey Grodzovsky 	return DC_ERROR_UNEXPECTED;
17244562236bSHarry Wentland }
17254562236bSHarry Wentland 
1726ab2541b6SAric Cyr /* first stream in the context is used to populate the rest */
1727ab2541b6SAric Cyr void validate_guaranteed_copy_streams(
1728608ac7bbSJerry Zuo 		struct dc_state *context,
1729ab2541b6SAric Cyr 		int max_streams)
17304562236bSHarry Wentland {
17314562236bSHarry Wentland 	int i;
17324562236bSHarry Wentland 
1733ab2541b6SAric Cyr 	for (i = 1; i < max_streams; i++) {
1734ab2541b6SAric Cyr 		context->streams[i] = context->streams[0];
17354562236bSHarry Wentland 
17364562236bSHarry Wentland 		copy_pipe_ctx(&context->res_ctx.pipe_ctx[0],
17374562236bSHarry Wentland 			      &context->res_ctx.pipe_ctx[i]);
17384562236bSHarry Wentland 		context->res_ctx.pipe_ctx[i].stream =
17394562236bSHarry Wentland 				context->res_ctx.pipe_ctx[0].stream;
17404562236bSHarry Wentland 
17414fa086b9SLeo (Sunpeng) Li 		dc_stream_retain(context->streams[i]);
1742ab2541b6SAric Cyr 		context->stream_count++;
17434562236bSHarry Wentland 	}
17444562236bSHarry Wentland }
17454562236bSHarry Wentland 
1746f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct_current(
17471dc90497SAndrey Grodzovsky 		const struct dc *dc,
1748608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
17491dc90497SAndrey Grodzovsky {
1750f36cc577SBhawanpreet Lakha 	dc_resource_state_copy_construct(dc->current_state, dst_ctx);
17511dc90497SAndrey Grodzovsky }
17521dc90497SAndrey Grodzovsky 
1753ab8db3e1SAndrey Grodzovsky 
1754ab8db3e1SAndrey Grodzovsky void dc_resource_state_construct(
1755ab8db3e1SAndrey Grodzovsky 		const struct dc *dc,
1756ab8db3e1SAndrey Grodzovsky 		struct dc_state *dst_ctx)
1757ab8db3e1SAndrey Grodzovsky {
1758ab8db3e1SAndrey Grodzovsky 	dst_ctx->dis_clk = dc->res_pool->display_clock;
1759ab8db3e1SAndrey Grodzovsky }
1760ab8db3e1SAndrey Grodzovsky 
1761e750d56dSYongqiang Sun enum dc_status dc_validate_global_state(
17621dc90497SAndrey Grodzovsky 		struct dc *dc,
1763608ac7bbSJerry Zuo 		struct dc_state *new_ctx)
17641dc90497SAndrey Grodzovsky {
17651dc90497SAndrey Grodzovsky 	enum dc_status result = DC_ERROR_UNEXPECTED;
17661dc90497SAndrey Grodzovsky 	int i, j;
17671dc90497SAndrey Grodzovsky 
1768d596e5d0SYongqiang Sun 	if (dc->res_pool->funcs->validate_global) {
1769d596e5d0SYongqiang Sun 			result = dc->res_pool->funcs->validate_global(dc, new_ctx);
1770d596e5d0SYongqiang Sun 			if (result != DC_OK)
1771d596e5d0SYongqiang Sun 				return result;
1772d596e5d0SYongqiang Sun 	}
17731dc90497SAndrey Grodzovsky 
17741dc90497SAndrey Grodzovsky 	for (i = 0; new_ctx && i < new_ctx->stream_count; i++) {
17751dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream = new_ctx->streams[i];
17761dc90497SAndrey Grodzovsky 
17771dc90497SAndrey Grodzovsky 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
17781dc90497SAndrey Grodzovsky 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
17791dc90497SAndrey Grodzovsky 
17801dc90497SAndrey Grodzovsky 			if (pipe_ctx->stream != stream)
17811dc90497SAndrey Grodzovsky 				continue;
17821dc90497SAndrey Grodzovsky 
17831dc90497SAndrey Grodzovsky 			/* Switch to dp clock source only if there is
17841dc90497SAndrey Grodzovsky 			 * no non dp stream that shares the same timing
17851dc90497SAndrey Grodzovsky 			 * with the dp stream.
17861dc90497SAndrey Grodzovsky 			 */
17871dc90497SAndrey Grodzovsky 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
17881dc90497SAndrey Grodzovsky 				!find_pll_sharable_stream(stream, new_ctx)) {
17891dc90497SAndrey Grodzovsky 
17909d0dcecdSHarry Wentland 				resource_unreference_clock_source(
17911dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
17921dc90497SAndrey Grodzovsky 						dc->res_pool,
17939d0dcecdSHarry Wentland 						pipe_ctx->clock_source);
17944a629536SHarry Wentland 
17951dc90497SAndrey Grodzovsky 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
17961dc90497SAndrey Grodzovsky 				resource_reference_clock_source(
17971dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
17981dc90497SAndrey Grodzovsky 						dc->res_pool,
17991dc90497SAndrey Grodzovsky 						 pipe_ctx->clock_source);
18001dc90497SAndrey Grodzovsky 			}
18011dc90497SAndrey Grodzovsky 		}
18021dc90497SAndrey Grodzovsky 	}
18031dc90497SAndrey Grodzovsky 
18041dc90497SAndrey Grodzovsky 	result = resource_build_scaling_params_for_context(dc, new_ctx);
18051dc90497SAndrey Grodzovsky 
18061dc90497SAndrey Grodzovsky 	if (result == DC_OK)
18071dc90497SAndrey Grodzovsky 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx))
18081dc90497SAndrey Grodzovsky 			result = DC_FAIL_BANDWIDTH_VALIDATE;
18091dc90497SAndrey Grodzovsky 
18101dc90497SAndrey Grodzovsky 	return result;
18111dc90497SAndrey Grodzovsky }
18121dc90497SAndrey Grodzovsky 
18136e4d6beeSTony Cheng static void patch_gamut_packet_checksum(
18146e4d6beeSTony Cheng 		struct encoder_info_packet *gamut_packet)
18154562236bSHarry Wentland {
18164562236bSHarry Wentland 	/* For gamut we recalc checksum */
18176e4d6beeSTony Cheng 	if (gamut_packet->valid) {
18184562236bSHarry Wentland 		uint8_t chk_sum = 0;
18194562236bSHarry Wentland 		uint8_t *ptr;
18204562236bSHarry Wentland 		uint8_t i;
18214562236bSHarry Wentland 
18224562236bSHarry Wentland 		/*start of the Gamut data. */
18236e4d6beeSTony Cheng 		ptr = &gamut_packet->sb[3];
18244562236bSHarry Wentland 
18256e4d6beeSTony Cheng 		for (i = 0; i <= gamut_packet->sb[1]; i++)
18264562236bSHarry Wentland 			chk_sum += ptr[i];
18274562236bSHarry Wentland 
18286e4d6beeSTony Cheng 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
18291646a6feSAndrew Wong 	}
18304562236bSHarry Wentland }
18314562236bSHarry Wentland 
18324562236bSHarry Wentland static void set_avi_info_frame(
18336e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
18344562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
18354562236bSHarry Wentland {
18360971c40eSHarry Wentland 	struct dc_stream_state *stream = pipe_ctx->stream;
18374562236bSHarry Wentland 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
18384562236bSHarry Wentland 	struct info_frame info_frame = { {0} };
18394562236bSHarry Wentland 	uint32_t pixel_encoding = 0;
18404562236bSHarry Wentland 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
18414562236bSHarry Wentland 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
18424562236bSHarry Wentland 	bool itc = false;
184350e27654SZeyu Fan 	uint8_t itc_value = 0;
18444562236bSHarry Wentland 	uint8_t cn0_cn1 = 0;
184550e27654SZeyu Fan 	unsigned int cn0_cn1_value = 0;
18464562236bSHarry Wentland 	uint8_t *check_sum = NULL;
18474562236bSHarry Wentland 	uint8_t byte_index = 0;
1848e8d726b7SReza Amini 	union hdmi_info_packet *hdmi_info = &info_frame.avi_info_packet.info_packet_hdmi;
184950e27654SZeyu Fan 	union display_content_support support = {0};
18504fa086b9SLeo (Sunpeng) Li 	unsigned int vic = pipe_ctx->stream->timing.vic;
185115e17335SCharlene Liu 	enum dc_timing_3d_format format;
18524562236bSHarry Wentland 
18534fa086b9SLeo (Sunpeng) Li 	color_space = pipe_ctx->stream->output_color_space;
1854e5f2038eSCharlene Liu 	if (color_space == COLOR_SPACE_UNKNOWN)
18554fa086b9SLeo (Sunpeng) Li 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
1856e5f2038eSCharlene Liu 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
18574562236bSHarry Wentland 
18584562236bSHarry Wentland 	/* Initialize header */
1859e8d726b7SReza Amini 	hdmi_info->bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
18604562236bSHarry Wentland 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
18614562236bSHarry Wentland 	* not be used in HDMI 2.0 (Section 10.1) */
1862e8d726b7SReza Amini 	hdmi_info->bits.header.version = 2;
1863e8d726b7SReza Amini 	hdmi_info->bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
18644562236bSHarry Wentland 
18654562236bSHarry Wentland 	/*
18664562236bSHarry Wentland 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
18674562236bSHarry Wentland 	 * according to HDMI 2.0 spec (Section 10.1)
18684562236bSHarry Wentland 	 */
18694562236bSHarry Wentland 
18704fa086b9SLeo (Sunpeng) Li 	switch (stream->timing.pixel_encoding) {
18714562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR422:
18724562236bSHarry Wentland 		pixel_encoding = 1;
18734562236bSHarry Wentland 		break;
18744562236bSHarry Wentland 
18754562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR444:
18764562236bSHarry Wentland 		pixel_encoding = 2;
18774562236bSHarry Wentland 		break;
18784562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR420:
18794562236bSHarry Wentland 		pixel_encoding = 3;
18804562236bSHarry Wentland 		break;
18814562236bSHarry Wentland 
18824562236bSHarry Wentland 	case PIXEL_ENCODING_RGB:
18834562236bSHarry Wentland 	default:
18844562236bSHarry Wentland 		pixel_encoding = 0;
18854562236bSHarry Wentland 	}
18864562236bSHarry Wentland 
18874562236bSHarry Wentland 	/* Y0_Y1_Y2 : The pixel encoding */
18884562236bSHarry Wentland 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
1889e8d726b7SReza Amini 	hdmi_info->bits.Y0_Y1_Y2 = pixel_encoding;
18904562236bSHarry Wentland 
18914562236bSHarry Wentland 	/* A0 = 1 Active Format Information valid */
1892e8d726b7SReza Amini 	hdmi_info->bits.A0 = ACTIVE_FORMAT_VALID;
18934562236bSHarry Wentland 
18944562236bSHarry Wentland 	/* B0, B1 = 3; Bar info data is valid */
1895e8d726b7SReza Amini 	hdmi_info->bits.B0_B1 = BAR_INFO_BOTH_VALID;
18964562236bSHarry Wentland 
1897e8d726b7SReza Amini 	hdmi_info->bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
18984562236bSHarry Wentland 
18994562236bSHarry Wentland 	/* S0, S1 : Underscan / Overscan */
19004562236bSHarry Wentland 	/* TODO: un-hardcode scan type */
19014562236bSHarry Wentland 	scan_type = SCANNING_TYPE_UNDERSCAN;
1902e8d726b7SReza Amini 	hdmi_info->bits.S0_S1 = scan_type;
19034562236bSHarry Wentland 
19044562236bSHarry Wentland 	/* C0, C1 : Colorimetry */
19058fde5884SCharlene Liu 	if (color_space == COLOR_SPACE_YCBCR709 ||
190615e17335SCharlene Liu 			color_space == COLOR_SPACE_YCBCR709_LIMITED)
1907e8d726b7SReza Amini 		hdmi_info->bits.C0_C1 = COLORIMETRY_ITU709;
19088fde5884SCharlene Liu 	else if (color_space == COLOR_SPACE_YCBCR601 ||
19098fde5884SCharlene Liu 			color_space == COLOR_SPACE_YCBCR601_LIMITED)
1910e8d726b7SReza Amini 		hdmi_info->bits.C0_C1 = COLORIMETRY_ITU601;
19118fde5884SCharlene Liu 	else {
1912e8d726b7SReza Amini 		hdmi_info->bits.C0_C1 = COLORIMETRY_NO_DATA;
19138fde5884SCharlene Liu 	}
1914534db198SAmy Zhang 	if (color_space == COLOR_SPACE_2020_RGB_FULLRANGE ||
1915534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE ||
1916534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_YCBCR) {
1917e8d726b7SReza Amini 		hdmi_info->bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
1918e8d726b7SReza Amini 		hdmi_info->bits.C0_C1   = COLORIMETRY_EXTENDED;
1919534db198SAmy Zhang 	} else if (color_space == COLOR_SPACE_ADOBERGB) {
1920e8d726b7SReza Amini 		hdmi_info->bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
1921e8d726b7SReza Amini 		hdmi_info->bits.C0_C1   = COLORIMETRY_EXTENDED;
1922534db198SAmy Zhang 	}
1923534db198SAmy Zhang 
19244562236bSHarry Wentland 	/* TODO: un-hardcode aspect ratio */
19254fa086b9SLeo (Sunpeng) Li 	aspect = stream->timing.aspect_ratio;
19264562236bSHarry Wentland 
19274562236bSHarry Wentland 	switch (aspect) {
19284562236bSHarry Wentland 	case ASPECT_RATIO_4_3:
19294562236bSHarry Wentland 	case ASPECT_RATIO_16_9:
1930e8d726b7SReza Amini 		hdmi_info->bits.M0_M1 = aspect;
19314562236bSHarry Wentland 		break;
19324562236bSHarry Wentland 
19334562236bSHarry Wentland 	case ASPECT_RATIO_NO_DATA:
19344562236bSHarry Wentland 	case ASPECT_RATIO_64_27:
19354562236bSHarry Wentland 	case ASPECT_RATIO_256_135:
19364562236bSHarry Wentland 	default:
1937e8d726b7SReza Amini 		hdmi_info->bits.M0_M1 = 0;
19384562236bSHarry Wentland 	}
19394562236bSHarry Wentland 
19404562236bSHarry Wentland 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
1941e8d726b7SReza Amini 	hdmi_info->bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
19424562236bSHarry Wentland 
19434562236bSHarry Wentland 	/* TODO: un-hardcode cn0_cn1 and itc */
194450e27654SZeyu Fan 
19454562236bSHarry Wentland 	cn0_cn1 = 0;
194650e27654SZeyu Fan 	cn0_cn1_value = 0;
194750e27654SZeyu Fan 
194850e27654SZeyu Fan 	itc = true;
194950e27654SZeyu Fan 	itc_value = 1;
195050e27654SZeyu Fan 
19514fa086b9SLeo (Sunpeng) Li 	support = stream->sink->edid_caps.content_support;
19524562236bSHarry Wentland 
19534562236bSHarry Wentland 	if (itc) {
195450e27654SZeyu Fan 		if (!support.bits.valid_content_type) {
195550e27654SZeyu Fan 			cn0_cn1_value = 0;
195650e27654SZeyu Fan 		} else {
195750e27654SZeyu Fan 			if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
195850e27654SZeyu Fan 				if (support.bits.graphics_content == 1) {
195950e27654SZeyu Fan 					cn0_cn1_value = 0;
196050e27654SZeyu Fan 				}
196150e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
196250e27654SZeyu Fan 				if (support.bits.photo_content == 1) {
196350e27654SZeyu Fan 					cn0_cn1_value = 1;
196450e27654SZeyu Fan 				} else {
196550e27654SZeyu Fan 					cn0_cn1_value = 0;
196650e27654SZeyu Fan 					itc_value = 0;
196750e27654SZeyu Fan 				}
196850e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
196950e27654SZeyu Fan 				if (support.bits.cinema_content == 1) {
197050e27654SZeyu Fan 					cn0_cn1_value = 2;
197150e27654SZeyu Fan 				} else {
197250e27654SZeyu Fan 					cn0_cn1_value = 0;
197350e27654SZeyu Fan 					itc_value = 0;
197450e27654SZeyu Fan 				}
197550e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
197650e27654SZeyu Fan 				if (support.bits.game_content == 1) {
197750e27654SZeyu Fan 					cn0_cn1_value = 3;
197850e27654SZeyu Fan 				} else {
197950e27654SZeyu Fan 					cn0_cn1_value = 0;
198050e27654SZeyu Fan 					itc_value = 0;
198150e27654SZeyu Fan 				}
198250e27654SZeyu Fan 			}
198350e27654SZeyu Fan 		}
198450e27654SZeyu Fan 		hdmi_info->bits.CN0_CN1 = cn0_cn1_value;
198550e27654SZeyu Fan 		hdmi_info->bits.ITC = itc_value;
19864562236bSHarry Wentland 	}
19874562236bSHarry Wentland 
19884562236bSHarry Wentland 	/* TODO : We should handle YCC quantization */
19894562236bSHarry Wentland 	/* but we do not have matrix calculation */
19904fa086b9SLeo (Sunpeng) Li 	if (stream->sink->edid_caps.qs_bit == 1 &&
19914fa086b9SLeo (Sunpeng) Li 			stream->sink->edid_caps.qy_bit == 1) {
199250e27654SZeyu Fan 		if (color_space == COLOR_SPACE_SRGB ||
199350e27654SZeyu Fan 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE) {
1994e8d726b7SReza Amini 			hdmi_info->bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
1995e8d726b7SReza Amini 			hdmi_info->bits.YQ0_YQ1 = YYC_QUANTIZATION_FULL_RANGE;
199650e27654SZeyu Fan 		} else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
199750e27654SZeyu Fan 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE) {
1998e8d726b7SReza Amini 			hdmi_info->bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
1999e8d726b7SReza Amini 			hdmi_info->bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
20004562236bSHarry Wentland 		} else {
2001e8d726b7SReza Amini 			hdmi_info->bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2002e8d726b7SReza Amini 			hdmi_info->bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
20034562236bSHarry Wentland 		}
200450e27654SZeyu Fan 	} else {
200550e27654SZeyu Fan 		hdmi_info->bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
200650e27654SZeyu Fan 		hdmi_info->bits.YQ0_YQ1   = YYC_QUANTIZATION_LIMITED_RANGE;
200750e27654SZeyu Fan 	}
200850e27654SZeyu Fan 
200915e17335SCharlene Liu 	///VIC
20104fa086b9SLeo (Sunpeng) Li 	format = stream->timing.timing_3d_format;
201115e17335SCharlene Liu 	/*todo, add 3DStereo support*/
201215e17335SCharlene Liu 	if (format != TIMING_3D_FORMAT_NONE) {
201315e17335SCharlene Liu 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
20144fa086b9SLeo (Sunpeng) Li 		switch (pipe_ctx->stream->timing.hdmi_vic) {
201515e17335SCharlene Liu 		case 1:
201615e17335SCharlene Liu 			vic = 95;
201715e17335SCharlene Liu 			break;
201815e17335SCharlene Liu 		case 2:
201915e17335SCharlene Liu 			vic = 94;
202015e17335SCharlene Liu 			break;
202115e17335SCharlene Liu 		case 3:
202215e17335SCharlene Liu 			vic = 93;
202315e17335SCharlene Liu 			break;
202415e17335SCharlene Liu 		case 4:
202515e17335SCharlene Liu 			vic = 98;
202615e17335SCharlene Liu 			break;
202715e17335SCharlene Liu 		default:
202815e17335SCharlene Liu 			break;
202915e17335SCharlene Liu 		}
203015e17335SCharlene Liu 	}
203115e17335SCharlene Liu 	hdmi_info->bits.VIC0_VIC7 = vic;
20324562236bSHarry Wentland 
20334562236bSHarry Wentland 	/* pixel repetition
20344562236bSHarry Wentland 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
20354562236bSHarry Wentland 	 * repetition start from 1 */
2036e8d726b7SReza Amini 	hdmi_info->bits.PR0_PR3 = 0;
20374562236bSHarry Wentland 
20384562236bSHarry Wentland 	/* Bar Info
20394562236bSHarry Wentland 	 * barTop:    Line Number of End of Top Bar.
20404562236bSHarry Wentland 	 * barBottom: Line Number of Start of Bottom Bar.
20414562236bSHarry Wentland 	 * barLeft:   Pixel Number of End of Left Bar.
20424562236bSHarry Wentland 	 * barRight:  Pixel Number of Start of Right Bar. */
20434fa086b9SLeo (Sunpeng) Li 	hdmi_info->bits.bar_top = stream->timing.v_border_top;
20444fa086b9SLeo (Sunpeng) Li 	hdmi_info->bits.bar_bottom = (stream->timing.v_total
20454fa086b9SLeo (Sunpeng) Li 			- stream->timing.v_border_bottom + 1);
20464fa086b9SLeo (Sunpeng) Li 	hdmi_info->bits.bar_left  = stream->timing.h_border_left;
20474fa086b9SLeo (Sunpeng) Li 	hdmi_info->bits.bar_right = (stream->timing.h_total
20484fa086b9SLeo (Sunpeng) Li 			- stream->timing.h_border_right + 1);
20494562236bSHarry Wentland 
20504562236bSHarry Wentland 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
2051e8d726b7SReza Amini 	check_sum = &info_frame.avi_info_packet.info_packet_hdmi.packet_raw_data.sb[0];
2052e8d726b7SReza Amini 
20533e183c5fSDave Airlie 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + HDMI_AVI_INFOFRAME_SIZE + 2;
20544562236bSHarry Wentland 
20553e183c5fSDave Airlie 	for (byte_index = 1; byte_index <= HDMI_AVI_INFOFRAME_SIZE; byte_index++)
2056e8d726b7SReza Amini 		*check_sum += hdmi_info->packet_raw_data.sb[byte_index];
20574562236bSHarry Wentland 
20584562236bSHarry Wentland 	/* one byte complement */
20594562236bSHarry Wentland 	*check_sum = (uint8_t) (0x100 - *check_sum);
20604562236bSHarry Wentland 
20614562236bSHarry Wentland 	/* Store in hw_path_mode */
2062e8d726b7SReza Amini 	info_packet->hb0 = hdmi_info->packet_raw_data.hb0;
2063e8d726b7SReza Amini 	info_packet->hb1 = hdmi_info->packet_raw_data.hb1;
2064e8d726b7SReza Amini 	info_packet->hb2 = hdmi_info->packet_raw_data.hb2;
20654562236bSHarry Wentland 
2066e66e4d64SHarry Wentland 	for (byte_index = 0; byte_index < sizeof(info_frame.avi_info_packet.
2067e66e4d64SHarry Wentland 				info_packet_hdmi.packet_raw_data.sb); byte_index++)
20684562236bSHarry Wentland 		info_packet->sb[byte_index] = info_frame.avi_info_packet.
20694562236bSHarry Wentland 				info_packet_hdmi.packet_raw_data.sb[byte_index];
20704562236bSHarry Wentland 
20714562236bSHarry Wentland 	info_packet->valid = true;
20724562236bSHarry Wentland }
20734562236bSHarry Wentland 
20746e4d6beeSTony Cheng static void set_vendor_info_packet(
20756e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
20760971c40eSHarry Wentland 		struct dc_stream_state *stream)
20774562236bSHarry Wentland {
20784562236bSHarry Wentland 	uint32_t length = 0;
20794562236bSHarry Wentland 	bool hdmi_vic_mode = false;
20804562236bSHarry Wentland 	uint8_t checksum = 0;
20814562236bSHarry Wentland 	uint32_t i = 0;
20824562236bSHarry Wentland 	enum dc_timing_3d_format format;
208315e17335SCharlene Liu 	// Can be different depending on packet content /*todo*/
208415e17335SCharlene Liu 	// unsigned int length = pPathMode->dolbyVision ? 24 : 5;
208515e17335SCharlene Liu 
208615e17335SCharlene Liu 	info_packet->valid = false;
20874562236bSHarry Wentland 
20884fa086b9SLeo (Sunpeng) Li 	format = stream->timing.timing_3d_format;
20894fa086b9SLeo (Sunpeng) Li 	if (stream->view_format == VIEW_3D_FORMAT_NONE)
20907f5c22d1SVitaly Prosyak 		format = TIMING_3D_FORMAT_NONE;
20914562236bSHarry Wentland 
20924562236bSHarry Wentland 	/* Can be different depending on packet content */
20934562236bSHarry Wentland 	length = 5;
20944562236bSHarry Wentland 
20954fa086b9SLeo (Sunpeng) Li 	if (stream->timing.hdmi_vic != 0
20964fa086b9SLeo (Sunpeng) Li 			&& stream->timing.h_total >= 3840
20974fa086b9SLeo (Sunpeng) Li 			&& stream->timing.v_total >= 2160)
20984562236bSHarry Wentland 		hdmi_vic_mode = true;
20994562236bSHarry Wentland 
21004562236bSHarry Wentland 	/* According to HDMI 1.4a CTS, VSIF should be sent
21014562236bSHarry Wentland 	 * for both 3D stereo and HDMI VIC modes.
21024562236bSHarry Wentland 	 * For all other modes, there is no VSIF sent.  */
21034562236bSHarry Wentland 
21044562236bSHarry Wentland 	if (format == TIMING_3D_FORMAT_NONE && !hdmi_vic_mode)
21054562236bSHarry Wentland 		return;
21064562236bSHarry Wentland 
21074562236bSHarry Wentland 	/* 24bit IEEE Registration identifier (0x000c03). LSB first. */
21084562236bSHarry Wentland 	info_packet->sb[1] = 0x03;
21094562236bSHarry Wentland 	info_packet->sb[2] = 0x0C;
21104562236bSHarry Wentland 	info_packet->sb[3] = 0x00;
21114562236bSHarry Wentland 
21124562236bSHarry Wentland 	/*PB4: 5 lower bytes = 0 (reserved). 3 higher bits = HDMI_Video_Format.
21134562236bSHarry Wentland 	 * The value for HDMI_Video_Format are:
21144562236bSHarry Wentland 	 * 0x0 (0b000) - No additional HDMI video format is presented in this
21154562236bSHarry Wentland 	 * packet
21164562236bSHarry Wentland 	 * 0x1 (0b001) - Extended resolution format present. 1 byte of HDMI_VIC
21174562236bSHarry Wentland 	 * parameter follows
21184562236bSHarry Wentland 	 * 0x2 (0b010) - 3D format indication present. 3D_Structure and
21194562236bSHarry Wentland 	 * potentially 3D_Ext_Data follows
21204562236bSHarry Wentland 	 * 0x3..0x7 (0b011..0b111) - reserved for future use */
21214562236bSHarry Wentland 	if (format != TIMING_3D_FORMAT_NONE)
21224562236bSHarry Wentland 		info_packet->sb[4] = (2 << 5);
21234562236bSHarry Wentland 	else if (hdmi_vic_mode)
21244562236bSHarry Wentland 		info_packet->sb[4] = (1 << 5);
21254562236bSHarry Wentland 
21264562236bSHarry Wentland 	/* PB5: If PB4 claims 3D timing (HDMI_Video_Format = 0x2):
21274562236bSHarry Wentland 	 * 4 lower bites = 0 (reserved). 4 higher bits = 3D_Structure.
21284562236bSHarry Wentland 	 * The value for 3D_Structure are:
21294562236bSHarry Wentland 	 * 0x0 - Frame Packing
21304562236bSHarry Wentland 	 * 0x1 - Field Alternative
21314562236bSHarry Wentland 	 * 0x2 - Line Alternative
21324562236bSHarry Wentland 	 * 0x3 - Side-by-Side (full)
21334562236bSHarry Wentland 	 * 0x4 - L + depth
21344562236bSHarry Wentland 	 * 0x5 - L + depth + graphics + graphics-depth
21354562236bSHarry Wentland 	 * 0x6 - Top-and-Bottom
21364562236bSHarry Wentland 	 * 0x7 - Reserved for future use
21374562236bSHarry Wentland 	 * 0x8 - Side-by-Side (Half)
21384562236bSHarry Wentland 	 * 0x9..0xE - Reserved for future use
21394562236bSHarry Wentland 	 * 0xF - Not used */
21404562236bSHarry Wentland 	switch (format) {
21414562236bSHarry Wentland 	case TIMING_3D_FORMAT_HW_FRAME_PACKING:
21424562236bSHarry Wentland 	case TIMING_3D_FORMAT_SW_FRAME_PACKING:
21434562236bSHarry Wentland 		info_packet->sb[5] = (0x0 << 4);
21444562236bSHarry Wentland 		break;
21454562236bSHarry Wentland 
21464562236bSHarry Wentland 	case TIMING_3D_FORMAT_SIDE_BY_SIDE:
21474562236bSHarry Wentland 	case TIMING_3D_FORMAT_SBS_SW_PACKED:
21484562236bSHarry Wentland 		info_packet->sb[5] = (0x8 << 4);
21494562236bSHarry Wentland 		length = 6;
21504562236bSHarry Wentland 		break;
21514562236bSHarry Wentland 
21524562236bSHarry Wentland 	case TIMING_3D_FORMAT_TOP_AND_BOTTOM:
21534562236bSHarry Wentland 	case TIMING_3D_FORMAT_TB_SW_PACKED:
21544562236bSHarry Wentland 		info_packet->sb[5] = (0x6 << 4);
21554562236bSHarry Wentland 		break;
21564562236bSHarry Wentland 
21574562236bSHarry Wentland 	default:
21584562236bSHarry Wentland 		break;
21594562236bSHarry Wentland 	}
21604562236bSHarry Wentland 
21614562236bSHarry Wentland 	/*PB5: If PB4 is set to 0x1 (extended resolution format)
21624562236bSHarry Wentland 	 * fill PB5 with the correct HDMI VIC code */
21634562236bSHarry Wentland 	if (hdmi_vic_mode)
21644fa086b9SLeo (Sunpeng) Li 		info_packet->sb[5] = stream->timing.hdmi_vic;
21654562236bSHarry Wentland 
21664562236bSHarry Wentland 	/* Header */
21673e183c5fSDave Airlie 	info_packet->hb0 = HDMI_INFOFRAME_TYPE_VENDOR; /* VSIF packet type. */
21684562236bSHarry Wentland 	info_packet->hb1 = 0x01; /* Version */
21694562236bSHarry Wentland 
21704562236bSHarry Wentland 	/* 4 lower bits = Length, 4 higher bits = 0 (reserved) */
21714562236bSHarry Wentland 	info_packet->hb2 = (uint8_t) (length);
21724562236bSHarry Wentland 
21734562236bSHarry Wentland 	/* Calculate checksum */
21744562236bSHarry Wentland 	checksum = 0;
21754562236bSHarry Wentland 	checksum += info_packet->hb0;
21764562236bSHarry Wentland 	checksum += info_packet->hb1;
21774562236bSHarry Wentland 	checksum += info_packet->hb2;
21784562236bSHarry Wentland 
21794562236bSHarry Wentland 	for (i = 1; i <= length; i++)
21804562236bSHarry Wentland 		checksum += info_packet->sb[i];
21814562236bSHarry Wentland 
21824562236bSHarry Wentland 	info_packet->sb[0] = (uint8_t) (0x100 - checksum);
21834562236bSHarry Wentland 
21844562236bSHarry Wentland 	info_packet->valid = true;
21854562236bSHarry Wentland }
21864562236bSHarry Wentland 
21876e4d6beeSTony Cheng static void set_spd_info_packet(
21886e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
21890971c40eSHarry Wentland 		struct dc_stream_state *stream)
21904562236bSHarry Wentland {
21914562236bSHarry Wentland 	/* SPD info packet for FreeSync */
21924562236bSHarry Wentland 
21934562236bSHarry Wentland 	unsigned char checksum = 0;
21944562236bSHarry Wentland 	unsigned int idx, payload_size = 0;
21954562236bSHarry Wentland 
21964562236bSHarry Wentland 	/* Check if Freesync is supported. Return if false. If true,
21974562236bSHarry Wentland 	 * set the corresponding bit in the info packet
21984562236bSHarry Wentland 	 */
21994fa086b9SLeo (Sunpeng) Li 	if (stream->freesync_ctx.supported == false)
22004562236bSHarry Wentland 		return;
22014562236bSHarry Wentland 
22024562236bSHarry Wentland 	if (dc_is_hdmi_signal(stream->signal)) {
22034562236bSHarry Wentland 
22044562236bSHarry Wentland 		/* HEADER */
22054562236bSHarry Wentland 
22064562236bSHarry Wentland 		/* HB0  = Packet Type = 0x83 (Source Product
22074562236bSHarry Wentland 		 *	  Descriptor InfoFrame)
22084562236bSHarry Wentland 		 */
22093e183c5fSDave Airlie 		info_packet->hb0 = HDMI_INFOFRAME_TYPE_SPD;
22104562236bSHarry Wentland 
22114562236bSHarry Wentland 		/* HB1  = Version = 0x01 */
22124562236bSHarry Wentland 		info_packet->hb1 = 0x01;
22134562236bSHarry Wentland 
22144562236bSHarry Wentland 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x08] */
22154562236bSHarry Wentland 		info_packet->hb2 = 0x08;
22164562236bSHarry Wentland 
22174562236bSHarry Wentland 		payload_size = 0x08;
22184562236bSHarry Wentland 
22194562236bSHarry Wentland 	} else if (dc_is_dp_signal(stream->signal)) {
22204562236bSHarry Wentland 
22214562236bSHarry Wentland 		/* HEADER */
22224562236bSHarry Wentland 
22234562236bSHarry Wentland 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
22244562236bSHarry Wentland 		 *	  when used to associate audio related info packets
22254562236bSHarry Wentland 		 */
22264562236bSHarry Wentland 		info_packet->hb0 = 0x00;
22274562236bSHarry Wentland 
22284562236bSHarry Wentland 		/* HB1  = Packet Type = 0x83 (Source Product
22294562236bSHarry Wentland 		 *	  Descriptor InfoFrame)
22304562236bSHarry Wentland 		 */
22313e183c5fSDave Airlie 		info_packet->hb1 = HDMI_INFOFRAME_TYPE_SPD;
22324562236bSHarry Wentland 
22334562236bSHarry Wentland 		/* HB2  = [Bits 7:0 = Least significant eight bits -
22344562236bSHarry Wentland 		 *	  For INFOFRAME, the value must be 1Bh]
22354562236bSHarry Wentland 		 */
22364562236bSHarry Wentland 		info_packet->hb2 = 0x1B;
22374562236bSHarry Wentland 
22384562236bSHarry Wentland 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x1]
22394562236bSHarry Wentland 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
22404562236bSHarry Wentland 		 */
22414562236bSHarry Wentland 		info_packet->hb3 = 0x04;
22424562236bSHarry Wentland 
22434562236bSHarry Wentland 		payload_size = 0x1B;
22444562236bSHarry Wentland 	}
22454562236bSHarry Wentland 
22464562236bSHarry Wentland 	/* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
22474562236bSHarry Wentland 	info_packet->sb[1] = 0x1A;
22484562236bSHarry Wentland 
22494562236bSHarry Wentland 	/* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
22504562236bSHarry Wentland 	info_packet->sb[2] = 0x00;
22514562236bSHarry Wentland 
22524562236bSHarry Wentland 	/* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
22534562236bSHarry Wentland 	info_packet->sb[3] = 0x00;
22544562236bSHarry Wentland 
22554562236bSHarry Wentland 	/* PB4 = Reserved */
22564562236bSHarry Wentland 	info_packet->sb[4] = 0x00;
22574562236bSHarry Wentland 
22584562236bSHarry Wentland 	/* PB5 = Reserved */
22594562236bSHarry Wentland 	info_packet->sb[5] = 0x00;
22604562236bSHarry Wentland 
22614562236bSHarry Wentland 	/* PB6 = [Bits 7:3 = Reserved] */
22624562236bSHarry Wentland 	info_packet->sb[6] = 0x00;
22634562236bSHarry Wentland 
22644fa086b9SLeo (Sunpeng) Li 	if (stream->freesync_ctx.supported == true)
22654562236bSHarry Wentland 		/* PB6 = [Bit 0 = FreeSync Supported] */
22664562236bSHarry Wentland 		info_packet->sb[6] |= 0x01;
22674562236bSHarry Wentland 
22684fa086b9SLeo (Sunpeng) Li 	if (stream->freesync_ctx.enabled == true)
22694562236bSHarry Wentland 		/* PB6 = [Bit 1 = FreeSync Enabled] */
22704562236bSHarry Wentland 		info_packet->sb[6] |= 0x02;
22714562236bSHarry Wentland 
22724fa086b9SLeo (Sunpeng) Li 	if (stream->freesync_ctx.active == true)
22734562236bSHarry Wentland 		/* PB6 = [Bit 2 = FreeSync Active] */
22744562236bSHarry Wentland 		info_packet->sb[6] |= 0x04;
22754562236bSHarry Wentland 
22764562236bSHarry Wentland 	/* PB7 = FreeSync Minimum refresh rate (Hz) */
22774fa086b9SLeo (Sunpeng) Li 	info_packet->sb[7] = (unsigned char) (stream->freesync_ctx.
22784562236bSHarry Wentland 			min_refresh_in_micro_hz / 1000000);
22794562236bSHarry Wentland 
22804562236bSHarry Wentland 	/* PB8 = FreeSync Maximum refresh rate (Hz)
22814562236bSHarry Wentland 	 *
22824562236bSHarry Wentland 	 * Note: We do not use the maximum capable refresh rate
22834562236bSHarry Wentland 	 * of the panel, because we should never go above the field
22844562236bSHarry Wentland 	 * rate of the mode timing set.
22854562236bSHarry Wentland 	 */
22864fa086b9SLeo (Sunpeng) Li 	info_packet->sb[8] = (unsigned char) (stream->freesync_ctx.
22874562236bSHarry Wentland 			nominal_refresh_in_micro_hz / 1000000);
22884562236bSHarry Wentland 
22894562236bSHarry Wentland 	/* PB9 - PB27  = Reserved */
22904562236bSHarry Wentland 	for (idx = 9; idx <= 27; idx++)
22914562236bSHarry Wentland 		info_packet->sb[idx] = 0x00;
22924562236bSHarry Wentland 
22934562236bSHarry Wentland 	/* Calculate checksum */
22944562236bSHarry Wentland 	checksum += info_packet->hb0;
22954562236bSHarry Wentland 	checksum += info_packet->hb1;
22964562236bSHarry Wentland 	checksum += info_packet->hb2;
22974562236bSHarry Wentland 	checksum += info_packet->hb3;
22984562236bSHarry Wentland 
22994562236bSHarry Wentland 	for (idx = 1; idx <= payload_size; idx++)
23004562236bSHarry Wentland 		checksum += info_packet->sb[idx];
23014562236bSHarry Wentland 
23024562236bSHarry Wentland 	/* PB0 = Checksum (one byte complement) */
23034562236bSHarry Wentland 	info_packet->sb[0] = (unsigned char) (0x100 - checksum);
23044562236bSHarry Wentland 
23054562236bSHarry Wentland 	info_packet->valid = true;
23064562236bSHarry Wentland }
23074562236bSHarry Wentland 
23081646a6feSAndrew Wong static void set_hdr_static_info_packet(
23096e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
23103be5262eSHarry Wentland 		struct dc_plane_state *plane_state,
23110971c40eSHarry Wentland 		struct dc_stream_state *stream)
23121646a6feSAndrew Wong {
2313e5cf325bSHarry Wentland 	uint16_t i = 0;
23141646a6feSAndrew Wong 	enum signal_type signal = stream->signal;
2315e5cf325bSHarry Wentland 	struct dc_hdr_static_metadata hdr_metadata;
2316e5cf325bSHarry Wentland 	uint32_t data;
23171646a6feSAndrew Wong 
23183be5262eSHarry Wentland 	if (!plane_state)
23191646a6feSAndrew Wong 		return;
23201646a6feSAndrew Wong 
23213be5262eSHarry Wentland 	hdr_metadata = plane_state->hdr_static_ctx;
23221646a6feSAndrew Wong 
232370063a59SAmy Zhang 	if (!hdr_metadata.hdr_supported)
232410bff005SYongqiang Sun 		return;
232510bff005SYongqiang Sun 
23261646a6feSAndrew Wong 	if (dc_is_hdmi_signal(signal)) {
23271646a6feSAndrew Wong 		info_packet->valid = true;
23281646a6feSAndrew Wong 
23291646a6feSAndrew Wong 		info_packet->hb0 = 0x87;
23301646a6feSAndrew Wong 		info_packet->hb1 = 0x01;
23311646a6feSAndrew Wong 		info_packet->hb2 = 0x1A;
23321646a6feSAndrew Wong 		i = 1;
23331646a6feSAndrew Wong 	} else if (dc_is_dp_signal(signal)) {
23341646a6feSAndrew Wong 		info_packet->valid = true;
23351646a6feSAndrew Wong 
23361646a6feSAndrew Wong 		info_packet->hb0 = 0x00;
23371646a6feSAndrew Wong 		info_packet->hb1 = 0x87;
23381646a6feSAndrew Wong 		info_packet->hb2 = 0x1D;
23391646a6feSAndrew Wong 		info_packet->hb3 = (0x13 << 2);
23401646a6feSAndrew Wong 		i = 2;
23411646a6feSAndrew Wong 	}
23421646a6feSAndrew Wong 
23431646a6feSAndrew Wong 	data = hdr_metadata.is_hdr;
23441646a6feSAndrew Wong 	info_packet->sb[i++] = data ? 0x02 : 0x00;
23451646a6feSAndrew Wong 	info_packet->sb[i++] = 0x00;
23461646a6feSAndrew Wong 
23471646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_green_x / 2;
23481646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23491646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23501646a6feSAndrew Wong 
23511646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_green_y / 2;
23521646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23531646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23541646a6feSAndrew Wong 
23551646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_blue_x / 2;
23561646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23571646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23581646a6feSAndrew Wong 
23591646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_blue_y / 2;
23601646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23611646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23621646a6feSAndrew Wong 
23631646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_red_x / 2;
23641646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23651646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23661646a6feSAndrew Wong 
23671646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_red_y / 2;
23681646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23691646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23701646a6feSAndrew Wong 
23711646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_white_point_x / 2;
23721646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23731646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23741646a6feSAndrew Wong 
23751646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_white_point_y / 2;
23761646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23771646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23781646a6feSAndrew Wong 
23791646a6feSAndrew Wong 	data = hdr_metadata.max_luminance;
23801646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23811646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23821646a6feSAndrew Wong 
23831646a6feSAndrew Wong 	data = hdr_metadata.min_luminance;
23841646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23851646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23861646a6feSAndrew Wong 
23871646a6feSAndrew Wong 	data = hdr_metadata.maximum_content_light_level;
23881646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23891646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23901646a6feSAndrew Wong 
23911646a6feSAndrew Wong 	data = hdr_metadata.maximum_frame_average_light_level;
23921646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
23931646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
23941646a6feSAndrew Wong 
23951646a6feSAndrew Wong 	if (dc_is_hdmi_signal(signal)) {
23961646a6feSAndrew Wong 		uint32_t checksum = 0;
23971646a6feSAndrew Wong 
23981646a6feSAndrew Wong 		checksum += info_packet->hb0;
23991646a6feSAndrew Wong 		checksum += info_packet->hb1;
24001646a6feSAndrew Wong 		checksum += info_packet->hb2;
24011646a6feSAndrew Wong 
24021646a6feSAndrew Wong 		for (i = 1; i <= info_packet->hb2; i++)
24031646a6feSAndrew Wong 			checksum += info_packet->sb[i];
24041646a6feSAndrew Wong 
24051646a6feSAndrew Wong 		info_packet->sb[0] = 0x100 - checksum;
24061646a6feSAndrew Wong 	} else if (dc_is_dp_signal(signal)) {
24071646a6feSAndrew Wong 		info_packet->sb[0] = 0x01;
24081646a6feSAndrew Wong 		info_packet->sb[1] = 0x1A;
24091646a6feSAndrew Wong 	}
24101646a6feSAndrew Wong }
24111646a6feSAndrew Wong 
24126e4d6beeSTony Cheng static void set_vsc_info_packet(
24136e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
24140971c40eSHarry Wentland 		struct dc_stream_state *stream)
24154562236bSHarry Wentland {
24164562236bSHarry Wentland 	unsigned int vscPacketRevision = 0;
24174562236bSHarry Wentland 	unsigned int i;
24184562236bSHarry Wentland 
241994267b3dSSylvia Tsai 	if (stream->sink->link->psr_enabled) {
24204562236bSHarry Wentland 		vscPacketRevision = 2;
24214562236bSHarry Wentland 	}
24224562236bSHarry Wentland 
24234562236bSHarry Wentland 	/* VSC packet not needed based on the features
24244562236bSHarry Wentland 	 * supported by this DP display
24254562236bSHarry Wentland 	 */
24264562236bSHarry Wentland 	if (vscPacketRevision == 0)
24274562236bSHarry Wentland 		return;
24284562236bSHarry Wentland 
24294562236bSHarry Wentland 	if (vscPacketRevision == 0x2) {
24304562236bSHarry Wentland 		/* Secondary-data Packet ID = 0*/
24314562236bSHarry Wentland 		info_packet->hb0 = 0x00;
24324562236bSHarry Wentland 		/* 07h - Packet Type Value indicating Video
24334562236bSHarry Wentland 		 * Stream Configuration packet
24344562236bSHarry Wentland 		 */
24354562236bSHarry Wentland 		info_packet->hb1 = 0x07;
24364562236bSHarry Wentland 		/* 02h = VSC SDP supporting 3D stereo and PSR
24374562236bSHarry Wentland 		 * (applies to eDP v1.3 or higher).
24384562236bSHarry Wentland 		 */
24394562236bSHarry Wentland 		info_packet->hb2 = 0x02;
24404562236bSHarry Wentland 		/* 08h = VSC packet supporting 3D stereo + PSR
24414562236bSHarry Wentland 		 * (HB2 = 02h).
24424562236bSHarry Wentland 		 */
24434562236bSHarry Wentland 		info_packet->hb3 = 0x08;
24444562236bSHarry Wentland 
24454562236bSHarry Wentland 		for (i = 0; i < 28; i++)
24464562236bSHarry Wentland 			info_packet->sb[i] = 0;
24474562236bSHarry Wentland 
24484562236bSHarry Wentland 		info_packet->valid = true;
24494562236bSHarry Wentland 	}
24504562236bSHarry Wentland 
24514562236bSHarry Wentland 	/*TODO: stereo 3D support and extend pixel encoding colorimetry*/
24524562236bSHarry Wentland }
24534562236bSHarry Wentland 
2454f36cc577SBhawanpreet Lakha void dc_resource_state_destruct(struct dc_state *context)
24554562236bSHarry Wentland {
24564562236bSHarry Wentland 	int i, j;
24574562236bSHarry Wentland 
2458ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
24593be5262eSHarry Wentland 		for (j = 0; j < context->stream_status[i].plane_count; j++)
24603be5262eSHarry Wentland 			dc_plane_state_release(
24613be5262eSHarry Wentland 				context->stream_status[i].plane_states[j]);
24624562236bSHarry Wentland 
24633be5262eSHarry Wentland 		context->stream_status[i].plane_count = 0;
24644fa086b9SLeo (Sunpeng) Li 		dc_stream_release(context->streams[i]);
2465ab2541b6SAric Cyr 		context->streams[i] = NULL;
24664562236bSHarry Wentland 	}
24674562236bSHarry Wentland }
24684562236bSHarry Wentland 
24694562236bSHarry Wentland /*
2470ab2541b6SAric Cyr  * Copy src_ctx into dst_ctx and retain all surfaces and streams referenced
24714562236bSHarry Wentland  * by the src_ctx
24724562236bSHarry Wentland  */
2473f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct(
2474608ac7bbSJerry Zuo 		const struct dc_state *src_ctx,
2475608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
24764562236bSHarry Wentland {
24774562236bSHarry Wentland 	int i, j;
24788ee5702aSDave Airlie 	struct kref refcount = dst_ctx->refcount;
24794562236bSHarry Wentland 
24804562236bSHarry Wentland 	*dst_ctx = *src_ctx;
24814562236bSHarry Wentland 
2482a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
24834562236bSHarry Wentland 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
24844562236bSHarry Wentland 
24854562236bSHarry Wentland 		if (cur_pipe->top_pipe)
24864562236bSHarry Wentland 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
24874562236bSHarry Wentland 
24884562236bSHarry Wentland 		if (cur_pipe->bottom_pipe)
24894562236bSHarry Wentland 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
24904562236bSHarry Wentland 
24914562236bSHarry Wentland 	}
24924562236bSHarry Wentland 
2493ab2541b6SAric Cyr 	for (i = 0; i < dst_ctx->stream_count; i++) {
24944fa086b9SLeo (Sunpeng) Li 		dc_stream_retain(dst_ctx->streams[i]);
24953be5262eSHarry Wentland 		for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
24963be5262eSHarry Wentland 			dc_plane_state_retain(
24973be5262eSHarry Wentland 				dst_ctx->stream_status[i].plane_states[j]);
24984562236bSHarry Wentland 	}
24999a3afbb3SAndrey Grodzovsky 
25009a3afbb3SAndrey Grodzovsky 	/* context refcount should not be overridden */
25018ee5702aSDave Airlie 	dst_ctx->refcount = refcount;
25029a3afbb3SAndrey Grodzovsky 
25034562236bSHarry Wentland }
25044562236bSHarry Wentland 
25054562236bSHarry Wentland struct clock_source *dc_resource_find_first_free_pll(
2506a2b8659dSTony Cheng 		struct resource_context *res_ctx,
2507a2b8659dSTony Cheng 		const struct resource_pool *pool)
25084562236bSHarry Wentland {
25094562236bSHarry Wentland 	int i;
25104562236bSHarry Wentland 
2511a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; ++i) {
25124562236bSHarry Wentland 		if (res_ctx->clock_source_ref_count[i] == 0)
2513a2b8659dSTony Cheng 			return pool->clock_sources[i];
25144562236bSHarry Wentland 	}
25154562236bSHarry Wentland 
25164562236bSHarry Wentland 	return NULL;
25174562236bSHarry Wentland }
25184562236bSHarry Wentland 
25194562236bSHarry Wentland void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
25204562236bSHarry Wentland {
25214562236bSHarry Wentland 	enum signal_type signal = SIGNAL_TYPE_NONE;
252296c50c0dSHarry Wentland 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
25234562236bSHarry Wentland 
25244562236bSHarry Wentland 	/* default all packets to invalid */
25256e4d6beeSTony Cheng 	info->avi.valid = false;
25266e4d6beeSTony Cheng 	info->gamut.valid = false;
25276e4d6beeSTony Cheng 	info->vendor.valid = false;
2528630e3573SJeff Smith 	info->spd.valid = false;
25296e4d6beeSTony Cheng 	info->hdrsmd.valid = false;
25306e4d6beeSTony Cheng 	info->vsc.valid = false;
25314562236bSHarry Wentland 
25324562236bSHarry Wentland 	signal = pipe_ctx->stream->signal;
25334562236bSHarry Wentland 
25344562236bSHarry Wentland 	/* HDMi and DP have different info packets*/
25354562236bSHarry Wentland 	if (dc_is_hdmi_signal(signal)) {
25366e4d6beeSTony Cheng 		set_avi_info_frame(&info->avi, pipe_ctx);
25376e4d6beeSTony Cheng 
25386e4d6beeSTony Cheng 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
25396e4d6beeSTony Cheng 
25406e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
25416e4d6beeSTony Cheng 
25426e4d6beeSTony Cheng 		set_hdr_static_info_packet(&info->hdrsmd,
25433be5262eSHarry Wentland 				pipe_ctx->plane_state, pipe_ctx->stream);
25446e4d6beeSTony Cheng 
2545a33fa99dSHarry Wentland 	} else if (dc_is_dp_signal(signal)) {
25466e4d6beeSTony Cheng 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
25476e4d6beeSTony Cheng 
25486e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
25496e4d6beeSTony Cheng 
25506e4d6beeSTony Cheng 		set_hdr_static_info_packet(&info->hdrsmd,
25513be5262eSHarry Wentland 				pipe_ctx->plane_state, pipe_ctx->stream);
2552a33fa99dSHarry Wentland 	}
25534562236bSHarry Wentland 
25546e4d6beeSTony Cheng 	patch_gamut_packet_checksum(&info->gamut);
25554562236bSHarry Wentland }
25564562236bSHarry Wentland 
25574562236bSHarry Wentland enum dc_status resource_map_clock_resources(
2558fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
2559608ac7bbSJerry Zuo 		struct dc_state *context,
25601dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
25614562236bSHarry Wentland {
25624562236bSHarry Wentland 	/* acquire new resources */
25631dc90497SAndrey Grodzovsky 	const struct resource_pool *pool = dc->res_pool;
25641dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(
25651dc90497SAndrey Grodzovsky 				&context->res_ctx, stream);
25664562236bSHarry Wentland 
25671dc90497SAndrey Grodzovsky 	if (!pipe_ctx)
25681dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
25694562236bSHarry Wentland 
25704562236bSHarry Wentland 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
25714562236bSHarry Wentland 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
2572a2b8659dSTony Cheng 		pipe_ctx->clock_source = pool->dp_clock_source;
25734562236bSHarry Wentland 	else {
25744562236bSHarry Wentland 		pipe_ctx->clock_source = NULL;
25754562236bSHarry Wentland 
2576fb3466a4SBhawanpreet Lakha 		if (!dc->config.disable_disp_pll_sharing)
25774ed4e51bSMikita Lipski 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
25784562236bSHarry Wentland 				&context->res_ctx,
25794562236bSHarry Wentland 				pipe_ctx);
25804562236bSHarry Wentland 
25814562236bSHarry Wentland 		if (pipe_ctx->clock_source == NULL)
25824562236bSHarry Wentland 			pipe_ctx->clock_source =
2583a2b8659dSTony Cheng 				dc_resource_find_first_free_pll(
2584a2b8659dSTony Cheng 					&context->res_ctx,
2585a2b8659dSTony Cheng 					pool);
25864562236bSHarry Wentland 	}
25874562236bSHarry Wentland 
25884562236bSHarry Wentland 	if (pipe_ctx->clock_source == NULL)
25894562236bSHarry Wentland 		return DC_NO_CLOCK_SOURCE_RESOURCE;
25904562236bSHarry Wentland 
25914562236bSHarry Wentland 	resource_reference_clock_source(
2592a2b8659dSTony Cheng 		&context->res_ctx, pool,
25934562236bSHarry Wentland 		pipe_ctx->clock_source);
25944562236bSHarry Wentland 
25954562236bSHarry Wentland 	return DC_OK;
25964562236bSHarry Wentland }
25974562236bSHarry Wentland 
25984562236bSHarry Wentland /*
25994562236bSHarry Wentland  * Note: We need to disable output if clock sources change,
26004562236bSHarry Wentland  * since bios does optimization and doesn't apply if changing
26014562236bSHarry Wentland  * PHY when not already disabled.
26024562236bSHarry Wentland  */
26034562236bSHarry Wentland bool pipe_need_reprogram(
26044562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx_old,
26054562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
26064562236bSHarry Wentland {
2607cfe4645eSDmytro Laktyushkin 	if (!pipe_ctx_old->stream)
2608cfe4645eSDmytro Laktyushkin 		return false;
2609cfe4645eSDmytro Laktyushkin 
26104562236bSHarry Wentland 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
26114562236bSHarry Wentland 		return true;
26124562236bSHarry Wentland 
26134562236bSHarry Wentland 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
26144562236bSHarry Wentland 		return true;
26154562236bSHarry Wentland 
2616afaacef4SHarry Wentland 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
26174562236bSHarry Wentland 		return true;
26184562236bSHarry Wentland 
26194562236bSHarry Wentland 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
26204562236bSHarry Wentland 			&& pipe_ctx_old->stream != pipe_ctx->stream)
26214562236bSHarry Wentland 		return true;
26224562236bSHarry Wentland 
26238e9c4c8cSHarry Wentland 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
26244562236bSHarry Wentland 		return true;
26254562236bSHarry Wentland 
26264562236bSHarry Wentland 	if (is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26274562236bSHarry Wentland 		return true;
26284562236bSHarry Wentland 
26294562236bSHarry Wentland 
26304562236bSHarry Wentland 	return false;
26314562236bSHarry Wentland }
2632529cad0fSDing Wang 
26330971c40eSHarry Wentland void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
2634529cad0fSDing Wang 		struct bit_depth_reduction_params *fmt_bit_depth)
2635529cad0fSDing Wang {
26364fa086b9SLeo (Sunpeng) Li 	enum dc_dither_option option = stream->dither_option;
2637529cad0fSDing Wang 	enum dc_pixel_encoding pixel_encoding =
26384fa086b9SLeo (Sunpeng) Li 			stream->timing.pixel_encoding;
2639529cad0fSDing Wang 
2640529cad0fSDing Wang 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
2641529cad0fSDing Wang 
2642603767f9STony Cheng 	if (option == DITHER_OPTION_DEFAULT) {
2643603767f9STony Cheng 		switch (stream->timing.display_color_depth) {
2644603767f9STony Cheng 		case COLOR_DEPTH_666:
2645603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL6;
2646603767f9STony Cheng 			break;
2647603767f9STony Cheng 		case COLOR_DEPTH_888:
2648603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL8;
2649603767f9STony Cheng 			break;
2650603767f9STony Cheng 		case COLOR_DEPTH_101010:
2651603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL10;
2652603767f9STony Cheng 			break;
2653603767f9STony Cheng 		default:
2654603767f9STony Cheng 			option = DITHER_OPTION_DISABLE;
2655603767f9STony Cheng 		}
2656603767f9STony Cheng 	}
2657603767f9STony Cheng 
2658529cad0fSDing Wang 	if (option == DITHER_OPTION_DISABLE)
2659529cad0fSDing Wang 		return;
2660529cad0fSDing Wang 
2661529cad0fSDing Wang 	if (option == DITHER_OPTION_TRUN6) {
2662529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2663529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
2664529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN8 ||
2665529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
2666529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6) {
2667529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2668529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
2669529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN10        ||
2670529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
2671529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
2672529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8     ||
2673529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2674529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2675529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2676529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2677529cad0fSDing Wang 	}
2678529cad0fSDing Wang 
2679529cad0fSDing Wang 	/* special case - Formatter can only reduce by 4 bits at most.
2680529cad0fSDing Wang 	 * When reducing from 12 to 6 bits,
2681529cad0fSDing Wang 	 * HW recommends we use trunc with round mode
2682529cad0fSDing Wang 	 * (if we did nothing, trunc to 10 bits would be used)
2683529cad0fSDing Wang 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
2684529cad0fSDing Wang 	 * as the input was 10 bits.
2685529cad0fSDing Wang 	 */
2686529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2687529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2688529cad0fSDing Wang 			option == DITHER_OPTION_FM6) {
2689529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2690529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2691529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
2692529cad0fSDing Wang 	}
2693529cad0fSDing Wang 
2694529cad0fSDing Wang 	/* spatial dither
2695529cad0fSDing Wang 	 * note that spatial modes 1-3 are never used
2696529cad0fSDing Wang 	 */
2697529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
2698529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2699529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
2700529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
2701529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2702529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
2703529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2704529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2705529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2706529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
2707529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2708529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6        ||
2709529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
2710529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2711529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2712529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
2713529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2714529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2715529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2716529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
2717529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10 ||
2718529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8 ||
2719529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6) {
2720529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2721529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
2722529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2723529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2724529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2725529cad0fSDing Wang 	}
2726529cad0fSDing Wang 
2727529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6 ||
2728529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2729529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10) {
2730529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
2731529cad0fSDing Wang 	} else {
2732529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
2733529cad0fSDing Wang 	}
2734529cad0fSDing Wang 
2735529cad0fSDing Wang 	//////////////////////
2736529cad0fSDing Wang 	//// temporal dither
2737529cad0fSDing Wang 	//////////////////////
2738529cad0fSDing Wang 	if (option == DITHER_OPTION_FM6           ||
2739529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6     ||
2740529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6     ||
2741529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2742529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6      ||
2743529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2744529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2745529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
2746529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM8        ||
2747529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8  ||
2748529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8) {
2749529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2750529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
2751529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM10) {
2752529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2753529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
2754529cad0fSDing Wang 	}
2755529cad0fSDing Wang 
2756529cad0fSDing Wang 	fmt_bit_depth->pixel_encoding = pixel_encoding;
2757529cad0fSDing Wang }
27589345d987SAndrey Grodzovsky 
2759fb3466a4SBhawanpreet Lakha bool dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
27609345d987SAndrey Grodzovsky {
2761fb3466a4SBhawanpreet Lakha 	struct dc  *core_dc = dc;
27624fa086b9SLeo (Sunpeng) Li 	struct dc_link *link = stream->sink->link;
27639345d987SAndrey Grodzovsky 	struct timing_generator *tg = core_dc->res_pool->timing_generators[0];
27649345d987SAndrey Grodzovsky 	enum dc_status res = DC_OK;
27659345d987SAndrey Grodzovsky 
27664fa086b9SLeo (Sunpeng) Li 	calculate_phy_pix_clks(stream);
27679345d987SAndrey Grodzovsky 
27684fa086b9SLeo (Sunpeng) Li 	if (!tg->funcs->validate_timing(tg, &stream->timing))
27699345d987SAndrey Grodzovsky 		res = DC_FAIL_CONTROLLER_VALIDATE;
27709345d987SAndrey Grodzovsky 
27719345d987SAndrey Grodzovsky 	if (res == DC_OK)
27729345d987SAndrey Grodzovsky 		if (!link->link_enc->funcs->validate_output_with_stream(
27734fa086b9SLeo (Sunpeng) Li 						link->link_enc, stream))
27749345d987SAndrey Grodzovsky 			res = DC_FAIL_ENC_VALIDATE;
27759345d987SAndrey Grodzovsky 
27769345d987SAndrey Grodzovsky 	/* TODO: validate audio ASIC caps, encoder */
27779345d987SAndrey Grodzovsky 
27789345d987SAndrey Grodzovsky 	if (res == DC_OK)
27794fa086b9SLeo (Sunpeng) Li 		res = dc_link_validate_mode_timing(stream,
27809345d987SAndrey Grodzovsky 		      link,
27814fa086b9SLeo (Sunpeng) Li 		      &stream->timing);
27829345d987SAndrey Grodzovsky 
27839345d987SAndrey Grodzovsky 	return res == DC_OK;
27849345d987SAndrey Grodzovsky }
2785792671d7SAndrey Grodzovsky 
2786fb3466a4SBhawanpreet Lakha bool dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
2787792671d7SAndrey Grodzovsky {
2788792671d7SAndrey Grodzovsky 	/* TODO For now validates pixel format only */
27898e7095b9SDmytro Laktyushkin 	if (dc->res_pool->funcs->validate_plane)
27908e7095b9SDmytro Laktyushkin 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps) == DC_OK;
2791792671d7SAndrey Grodzovsky 
2792792671d7SAndrey Grodzovsky 	return true;
2793792671d7SAndrey Grodzovsky }
2794