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  */
254fc4dca8SSam Ravnborg 
264fc4dca8SSam Ravnborg #include <linux/slab.h>
274fc4dca8SSam Ravnborg 
284562236bSHarry Wentland #include "dm_services.h"
294562236bSHarry Wentland 
304562236bSHarry Wentland #include "resource.h"
314562236bSHarry Wentland #include "include/irq_service_interface.h"
324562236bSHarry Wentland #include "link_encoder.h"
334562236bSHarry Wentland #include "stream_encoder.h"
344562236bSHarry Wentland #include "opp.h"
354562236bSHarry Wentland #include "timing_generator.h"
364562236bSHarry Wentland #include "transform.h"
3733d7598dSJun Lei #include "dccg.h"
3833d7598dSJun Lei #include "dchubbub.h"
39d94585a0SYue Hin Lau #include "dpp.h"
405ac3d3c9SCharlene Liu #include "core_types.h"
414562236bSHarry Wentland #include "set_mode_types.h"
424562236bSHarry Wentland #include "virtual/virtual_stream_encoder.h"
433b94a400STao #include "dpcd_defs.h"
444562236bSHarry Wentland 
454562236bSHarry Wentland #include "dce80/dce80_resource.h"
464562236bSHarry Wentland #include "dce100/dce100_resource.h"
474562236bSHarry Wentland #include "dce110/dce110_resource.h"
484562236bSHarry Wentland #include "dce112/dce112_resource.h"
49b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
50ff5ef992SAlex Deucher #include "dcn10/dcn10_resource.h"
51ff5ef992SAlex Deucher #endif
527ed4e635SHarry Wentland #include "dcn20/dcn20_resource.h"
53e22ece54SBhawanpreet Lakha #include "dcn21/dcn21_resource.h"
542c8ad2d5SAlex Deucher #include "dce120/dce120_resource.h"
555d4b05ddSBhawanpreet Lakha 
565d4b05ddSBhawanpreet Lakha #define DC_LOGGER_INIT(logger)
575d4b05ddSBhawanpreet Lakha 
584562236bSHarry Wentland enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
594562236bSHarry Wentland {
604562236bSHarry Wentland 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
614562236bSHarry Wentland 	switch (asic_id.chip_family) {
624562236bSHarry Wentland 
634562236bSHarry Wentland 	case FAMILY_CI:
644562236bSHarry Wentland 		dc_version = DCE_VERSION_8_0;
654562236bSHarry Wentland 		break;
66ebfdf0d0SAlex Deucher 	case FAMILY_KV:
67ebfdf0d0SAlex Deucher 		if (ASIC_REV_IS_KALINDI(asic_id.hw_internal_rev) ||
68ebfdf0d0SAlex Deucher 		    ASIC_REV_IS_BHAVANI(asic_id.hw_internal_rev) ||
69ebfdf0d0SAlex Deucher 		    ASIC_REV_IS_GODAVARI(asic_id.hw_internal_rev))
70ebfdf0d0SAlex Deucher 			dc_version = DCE_VERSION_8_3;
71ebfdf0d0SAlex Deucher 		else
72ebfdf0d0SAlex Deucher 			dc_version = DCE_VERSION_8_1;
73ebfdf0d0SAlex Deucher 		break;
744562236bSHarry Wentland 	case FAMILY_CZ:
754562236bSHarry Wentland 		dc_version = DCE_VERSION_11_0;
764562236bSHarry Wentland 		break;
774562236bSHarry Wentland 
784562236bSHarry Wentland 	case FAMILY_VI:
794562236bSHarry Wentland 		if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
804562236bSHarry Wentland 				ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
814562236bSHarry Wentland 			dc_version = DCE_VERSION_10_0;
824562236bSHarry Wentland 			break;
834562236bSHarry Wentland 		}
844562236bSHarry Wentland 		if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
85b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
86b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
874562236bSHarry Wentland 			dc_version = DCE_VERSION_11_2;
884562236bSHarry Wentland 		}
890c75d5acSJerry (Fangzhi) Zuo 		if (ASIC_REV_IS_VEGAM(asic_id.hw_internal_rev))
900c75d5acSJerry (Fangzhi) Zuo 			dc_version = DCE_VERSION_11_22;
914562236bSHarry Wentland 		break;
922c8ad2d5SAlex Deucher 	case FAMILY_AI:
93b8b6ce89SLeo Li 		if (ASICREV_IS_VEGA20_P(asic_id.hw_internal_rev))
94b8b6ce89SLeo Li 			dc_version = DCE_VERSION_12_1;
95b8b6ce89SLeo Li 		else
962c8ad2d5SAlex Deucher 			dc_version = DCE_VERSION_12_0;
972c8ad2d5SAlex Deucher 		break;
98b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
99ff5ef992SAlex Deucher 	case FAMILY_RV:
100ff5ef992SAlex Deucher 		dc_version = DCN_VERSION_1_0;
1010e3d73f1SBhawanpreet Lakha 		if (ASICREV_IS_RAVEN2(asic_id.hw_internal_rev))
1020e3d73f1SBhawanpreet Lakha 			dc_version = DCN_VERSION_1_01;
103e22ece54SBhawanpreet Lakha 		if (ASICREV_IS_RENOIR(asic_id.hw_internal_rev))
104e22ece54SBhawanpreet Lakha 			dc_version = DCN_VERSION_2_1;
105ff5ef992SAlex Deucher 		break;
106ff5ef992SAlex Deucher #endif
1077ed4e635SHarry Wentland 
1087ed4e635SHarry Wentland 	case FAMILY_NV:
1097ed4e635SHarry Wentland 		dc_version = DCN_VERSION_2_0;
1107ed4e635SHarry Wentland 		break;
1114562236bSHarry Wentland 	default:
1124562236bSHarry Wentland 		dc_version = DCE_VERSION_UNKNOWN;
1134562236bSHarry Wentland 		break;
1144562236bSHarry Wentland 	}
1154562236bSHarry Wentland 	return dc_version;
1164562236bSHarry Wentland }
1174562236bSHarry Wentland 
118d9673c92SHarry Wentland struct resource_pool *dc_create_resource_pool(struct dc  *dc,
119d9673c92SHarry Wentland 					      const struct dc_init_data *init_data,
120d9673c92SHarry Wentland 					      enum dce_version dc_version)
1214562236bSHarry Wentland {
1225ac3d3c9SCharlene Liu 	struct resource_pool *res_pool = NULL;
1234562236bSHarry Wentland 
1244562236bSHarry Wentland 	switch (dc_version) {
1254562236bSHarry Wentland 	case DCE_VERSION_8_0:
1265ac3d3c9SCharlene Liu 		res_pool = dce80_create_resource_pool(
127d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1285ac3d3c9SCharlene Liu 		break;
1297992a629SAlex Deucher 	case DCE_VERSION_8_1:
1307992a629SAlex Deucher 		res_pool = dce81_create_resource_pool(
131d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1327992a629SAlex Deucher 		break;
1337992a629SAlex Deucher 	case DCE_VERSION_8_3:
1347992a629SAlex Deucher 		res_pool = dce83_create_resource_pool(
135d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1367992a629SAlex Deucher 		break;
1374562236bSHarry Wentland 	case DCE_VERSION_10_0:
1385ac3d3c9SCharlene Liu 		res_pool = dce100_create_resource_pool(
139d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1405ac3d3c9SCharlene Liu 		break;
1414562236bSHarry Wentland 	case DCE_VERSION_11_0:
1425ac3d3c9SCharlene Liu 		res_pool = dce110_create_resource_pool(
143d9673c92SHarry Wentland 				init_data->num_virtual_links, dc,
144d9673c92SHarry Wentland 				init_data->asic_id);
1455ac3d3c9SCharlene Liu 		break;
1464562236bSHarry Wentland 	case DCE_VERSION_11_2:
1470c75d5acSJerry (Fangzhi) Zuo 	case DCE_VERSION_11_22:
1485ac3d3c9SCharlene Liu 		res_pool = dce112_create_resource_pool(
149d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1505ac3d3c9SCharlene Liu 		break;
1512c8ad2d5SAlex Deucher 	case DCE_VERSION_12_0:
152b8b6ce89SLeo Li 	case DCE_VERSION_12_1:
1532c8ad2d5SAlex Deucher 		res_pool = dce120_create_resource_pool(
154d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1552c8ad2d5SAlex Deucher 		break;
156ff5ef992SAlex Deucher 
157b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
158ff5ef992SAlex Deucher 	case DCN_VERSION_1_0:
1590e3d73f1SBhawanpreet Lakha 	case DCN_VERSION_1_01:
160d9673c92SHarry Wentland 		res_pool = dcn10_create_resource_pool(init_data, dc);
161ff5ef992SAlex Deucher 		break;
1623639fa68SZeyu Fan 
1633639fa68SZeyu Fan 
1647ed4e635SHarry Wentland 	case DCN_VERSION_2_0:
1657ed4e635SHarry Wentland 		res_pool = dcn20_create_resource_pool(init_data, dc);
1667ed4e635SHarry Wentland 		break;
167e22ece54SBhawanpreet Lakha 	case DCN_VERSION_2_1:
168e22ece54SBhawanpreet Lakha 		res_pool = dcn21_create_resource_pool(init_data, dc);
169e22ece54SBhawanpreet Lakha 		break;
170e22ece54SBhawanpreet Lakha #endif
1717ed4e635SHarry Wentland 
1724562236bSHarry Wentland 	default:
1734562236bSHarry Wentland 		break;
1744562236bSHarry Wentland 	}
175f49cfa27Shersen wu 
1765ac3d3c9SCharlene Liu 	if (res_pool != NULL) {
1779adc8050SDmytro Laktyushkin 		if (dc->ctx->dc_bios->fw_info_valid) {
17841a5a2a8Shersen wu 			res_pool->ref_clocks.xtalin_clock_inKhz =
1799adc8050SDmytro Laktyushkin 				dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
18041a5a2a8Shersen wu 			/* initialize with firmware data first, no all
18141a5a2a8Shersen wu 			 * ASIC have DCCG SW component. FPGA or
18241a5a2a8Shersen wu 			 * simulation need initialization of
18341a5a2a8Shersen wu 			 * dccg_ref_clock_inKhz, dchub_ref_clock_inKhz
18441a5a2a8Shersen wu 			 * with xtalin_clock_inKhz
18541a5a2a8Shersen wu 			 */
18641a5a2a8Shersen wu 			res_pool->ref_clocks.dccg_ref_clock_inKhz =
18741a5a2a8Shersen wu 				res_pool->ref_clocks.xtalin_clock_inKhz;
18841a5a2a8Shersen wu 			res_pool->ref_clocks.dchub_ref_clock_inKhz =
18941a5a2a8Shersen wu 				res_pool->ref_clocks.xtalin_clock_inKhz;
1905ac3d3c9SCharlene Liu 		} else
1915ac3d3c9SCharlene Liu 			ASSERT_CRITICAL(false);
1925ac3d3c9SCharlene Liu 	}
1935ac3d3c9SCharlene Liu 
1945ac3d3c9SCharlene Liu 	return res_pool;
1954562236bSHarry Wentland }
1964562236bSHarry Wentland 
197fb3466a4SBhawanpreet Lakha void dc_destroy_resource_pool(struct dc  *dc)
1984562236bSHarry Wentland {
1994562236bSHarry Wentland 	if (dc) {
2004562236bSHarry Wentland 		if (dc->res_pool)
2014562236bSHarry Wentland 			dc->res_pool->funcs->destroy(&dc->res_pool);
2024562236bSHarry Wentland 
2032004f45eSHarry Wentland 		kfree(dc->hwseq);
2044562236bSHarry Wentland 	}
2054562236bSHarry Wentland }
2064562236bSHarry Wentland 
2074562236bSHarry Wentland static void update_num_audio(
2084562236bSHarry Wentland 	const struct resource_straps *straps,
2094562236bSHarry Wentland 	unsigned int *num_audio,
2104562236bSHarry Wentland 	struct audio_support *aud_support)
2114562236bSHarry Wentland {
2124562236bSHarry Wentland 	aud_support->dp_audio = true;
213b8e9eb72SCharlene Liu 	aud_support->hdmi_audio_native = false;
214b8e9eb72SCharlene Liu 	aud_support->hdmi_audio_on_dongle = false;
215b8e9eb72SCharlene Liu 
216b8e9eb72SCharlene Liu 	if (straps->hdmi_disable == 0) {
2174562236bSHarry Wentland 		if (straps->dc_pinstraps_audio & 0x2) {
2184562236bSHarry Wentland 			aud_support->hdmi_audio_on_dongle = true;
219b8e9eb72SCharlene Liu 			aud_support->hdmi_audio_native = true;
2204562236bSHarry Wentland 		}
2214562236bSHarry Wentland 	}
2224562236bSHarry Wentland 
2234562236bSHarry Wentland 	switch (straps->audio_stream_number) {
2244562236bSHarry Wentland 	case 0: /* multi streams supported */
2254562236bSHarry Wentland 		break;
2264562236bSHarry Wentland 	case 1: /* multi streams not supported */
2274562236bSHarry Wentland 		*num_audio = 1;
2284562236bSHarry Wentland 		break;
2294562236bSHarry Wentland 	default:
2304562236bSHarry Wentland 		DC_ERR("DC: unexpected audio fuse!\n");
23117a96033SJulia Lawall 	}
2324562236bSHarry Wentland }
2334562236bSHarry Wentland 
2344562236bSHarry Wentland bool resource_construct(
2354562236bSHarry Wentland 	unsigned int num_virtual_links,
236fb3466a4SBhawanpreet Lakha 	struct dc  *dc,
2374562236bSHarry Wentland 	struct resource_pool *pool,
2384562236bSHarry Wentland 	const struct resource_create_funcs *create_funcs)
2394562236bSHarry Wentland {
2404562236bSHarry Wentland 	struct dc_context *ctx = dc->ctx;
2414562236bSHarry Wentland 	const struct resource_caps *caps = pool->res_cap;
2424562236bSHarry Wentland 	int i;
2434562236bSHarry Wentland 	unsigned int num_audio = caps->num_audio;
2444562236bSHarry Wentland 	struct resource_straps straps = {0};
2454562236bSHarry Wentland 
2464562236bSHarry Wentland 	if (create_funcs->read_dce_straps)
2474562236bSHarry Wentland 		create_funcs->read_dce_straps(dc->ctx, &straps);
2484562236bSHarry Wentland 
2494562236bSHarry Wentland 	pool->audio_count = 0;
2504562236bSHarry Wentland 	if (create_funcs->create_audio) {
2514562236bSHarry Wentland 		/* find the total number of streams available via the
2524562236bSHarry Wentland 		 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
2534562236bSHarry Wentland 		 * registers (one for each pin) starting from pin 1
2544562236bSHarry Wentland 		 * up to the max number of audio pins.
2554562236bSHarry Wentland 		 * We stop on the first pin where
2564562236bSHarry Wentland 		 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
2574562236bSHarry Wentland 		 */
2584562236bSHarry Wentland 		update_num_audio(&straps, &num_audio, &pool->audio_support);
2595feb9f07STai Man 		for (i = 0; i < caps->num_audio; i++) {
2604562236bSHarry Wentland 			struct audio *aud = create_funcs->create_audio(ctx, i);
2614562236bSHarry Wentland 
2624562236bSHarry Wentland 			if (aud == NULL) {
2634562236bSHarry Wentland 				DC_ERR("DC: failed to create audio!\n");
2644562236bSHarry Wentland 				return false;
2654562236bSHarry Wentland 			}
2664562236bSHarry Wentland 			if (!aud->funcs->endpoint_valid(aud)) {
2674562236bSHarry Wentland 				aud->funcs->destroy(&aud);
2684562236bSHarry Wentland 				break;
2694562236bSHarry Wentland 			}
2704562236bSHarry Wentland 			pool->audios[i] = aud;
2714562236bSHarry Wentland 			pool->audio_count++;
2724562236bSHarry Wentland 		}
2734562236bSHarry Wentland 	}
2744562236bSHarry Wentland 
2754562236bSHarry Wentland 	pool->stream_enc_count = 0;
2764562236bSHarry Wentland 	if (create_funcs->create_stream_encoder) {
2774562236bSHarry Wentland 		for (i = 0; i < caps->num_stream_encoder; i++) {
2784562236bSHarry Wentland 			pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
2794562236bSHarry Wentland 			if (pool->stream_enc[i] == NULL)
2804562236bSHarry Wentland 				DC_ERR("DC: failed to create stream_encoder!\n");
2814562236bSHarry Wentland 			pool->stream_enc_count++;
2824562236bSHarry Wentland 		}
2834562236bSHarry Wentland 	}
284929c3aaaSEric Bernstein 
2854176664bSCharlene Liu 	dc->caps.dynamic_audio = false;
2864176664bSCharlene Liu 	if (pool->audio_count < pool->stream_enc_count) {
2874176664bSCharlene Liu 		dc->caps.dynamic_audio = true;
2884176664bSCharlene Liu 	}
2894562236bSHarry Wentland 	for (i = 0; i < num_virtual_links; i++) {
2904562236bSHarry Wentland 		pool->stream_enc[pool->stream_enc_count] =
2914562236bSHarry Wentland 			virtual_stream_encoder_create(
2924562236bSHarry Wentland 					ctx, ctx->dc_bios);
2934562236bSHarry Wentland 		if (pool->stream_enc[pool->stream_enc_count] == NULL) {
2944562236bSHarry Wentland 			DC_ERR("DC: failed to create stream_encoder!\n");
2954562236bSHarry Wentland 			return false;
2964562236bSHarry Wentland 		}
2974562236bSHarry Wentland 		pool->stream_enc_count++;
2984562236bSHarry Wentland 	}
2994562236bSHarry Wentland 
3004562236bSHarry Wentland 	dc->hwseq = create_funcs->create_hwseq(ctx);
3014562236bSHarry Wentland 
3024562236bSHarry Wentland 	return true;
3034562236bSHarry Wentland }
304ad8960a6SMikita Lipski static int find_matching_clock_source(
305ad8960a6SMikita Lipski 		const struct resource_pool *pool,
306ad8960a6SMikita Lipski 		struct clock_source *clock_source)
307ad8960a6SMikita Lipski {
3084562236bSHarry Wentland 
309ad8960a6SMikita Lipski 	int i;
310ad8960a6SMikita Lipski 
311ad8960a6SMikita Lipski 	for (i = 0; i < pool->clk_src_count; i++) {
312ad8960a6SMikita Lipski 		if (pool->clock_sources[i] == clock_source)
313ad8960a6SMikita Lipski 			return i;
314ad8960a6SMikita Lipski 	}
315ad8960a6SMikita Lipski 	return -1;
316ad8960a6SMikita Lipski }
3174562236bSHarry Wentland 
31821e67d4dSHarry Wentland void resource_unreference_clock_source(
3194562236bSHarry Wentland 		struct resource_context *res_ctx,
320a2b8659dSTony Cheng 		const struct resource_pool *pool,
3214a629536SHarry Wentland 		struct clock_source *clock_source)
3224562236bSHarry Wentland {
323ad8960a6SMikita Lipski 	int i = find_matching_clock_source(pool, clock_source);
3244a629536SHarry Wentland 
325ad8960a6SMikita Lipski 	if (i > -1)
3264562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]--;
3274562236bSHarry Wentland 
32821e67d4dSHarry Wentland 	if (pool->dp_clock_source == clock_source)
3294562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count--;
3304562236bSHarry Wentland }
3314562236bSHarry Wentland 
3324562236bSHarry Wentland void resource_reference_clock_source(
3334562236bSHarry Wentland 		struct resource_context *res_ctx,
334a2b8659dSTony Cheng 		const struct resource_pool *pool,
3354562236bSHarry Wentland 		struct clock_source *clock_source)
3364562236bSHarry Wentland {
337ad8960a6SMikita Lipski 	int i = find_matching_clock_source(pool, clock_source);
3384562236bSHarry Wentland 
339ad8960a6SMikita Lipski 	if (i > -1)
3404562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]++;
3414562236bSHarry Wentland 
342a2b8659dSTony Cheng 	if (pool->dp_clock_source == clock_source)
3434562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count++;
3444562236bSHarry Wentland }
3454562236bSHarry Wentland 
346ad8960a6SMikita Lipski int resource_get_clock_source_reference(
347ad8960a6SMikita Lipski 		struct resource_context *res_ctx,
348ad8960a6SMikita Lipski 		const struct resource_pool *pool,
349ad8960a6SMikita Lipski 		struct clock_source *clock_source)
350ad8960a6SMikita Lipski {
351ad8960a6SMikita Lipski 	int i = find_matching_clock_source(pool, clock_source);
352ad8960a6SMikita Lipski 
353ad8960a6SMikita Lipski 	if (i > -1)
354ad8960a6SMikita Lipski 		return res_ctx->clock_source_ref_count[i];
355ad8960a6SMikita Lipski 
356ad8960a6SMikita Lipski 	if (pool->dp_clock_source == clock_source)
357ad8960a6SMikita Lipski 		return res_ctx->dp_clock_source_ref_count;
358ad8960a6SMikita Lipski 
359ad8960a6SMikita Lipski 	return -1;
360ad8960a6SMikita Lipski }
361ad8960a6SMikita Lipski 
3624562236bSHarry Wentland bool resource_are_streams_timing_synchronizable(
3630971c40eSHarry Wentland 	struct dc_stream_state *stream1,
3640971c40eSHarry Wentland 	struct dc_stream_state *stream2)
3654562236bSHarry Wentland {
3664fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.h_total != stream2->timing.h_total)
3674562236bSHarry Wentland 		return false;
3684562236bSHarry Wentland 
3694fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.v_total != stream2->timing.v_total)
3704562236bSHarry Wentland 		return false;
3714562236bSHarry Wentland 
3724fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.h_addressable
3734fa086b9SLeo (Sunpeng) Li 				!= stream2->timing.h_addressable)
3744562236bSHarry Wentland 		return false;
3754562236bSHarry Wentland 
3764fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.v_addressable
3774fa086b9SLeo (Sunpeng) Li 				!= stream2->timing.v_addressable)
3784562236bSHarry Wentland 		return false;
3794562236bSHarry Wentland 
380380604e2SKen Chalmers 	if (stream1->timing.pix_clk_100hz
381380604e2SKen Chalmers 				!= stream2->timing.pix_clk_100hz)
3824562236bSHarry Wentland 		return false;
3834562236bSHarry Wentland 
3843e27e10eSMikita Lipski 	if (stream1->clamping.c_depth != stream2->clamping.c_depth)
3853e27e10eSMikita Lipski 		return false;
3863e27e10eSMikita Lipski 
3874562236bSHarry Wentland 	if (stream1->phy_pix_clk != stream2->phy_pix_clk
3887e2fe319SCharlene Liu 			&& (!dc_is_dp_signal(stream1->signal)
3897e2fe319SCharlene Liu 			|| !dc_is_dp_signal(stream2->signal)))
3904562236bSHarry Wentland 		return false;
3914562236bSHarry Wentland 
392d77f778eSCharlene Liu 	if (stream1->view_format != stream2->view_format)
393d77f778eSCharlene Liu 		return false;
394d77f778eSCharlene Liu 
3950460f9abSJun Lei 	if (stream1->ignore_msa_timing_param || stream2->ignore_msa_timing_param)
3960460f9abSJun Lei 		return false;
3970460f9abSJun Lei 
3984562236bSHarry Wentland 	return true;
3994562236bSHarry Wentland }
4003e27e10eSMikita Lipski static bool is_dp_and_hdmi_sharable(
4013e27e10eSMikita Lipski 		struct dc_stream_state *stream1,
4023e27e10eSMikita Lipski 		struct dc_stream_state *stream2)
4033e27e10eSMikita Lipski {
4043e27e10eSMikita Lipski 	if (stream1->ctx->dc->caps.disable_dp_clk_share)
4053e27e10eSMikita Lipski 		return false;
4063e27e10eSMikita Lipski 
4073e27e10eSMikita Lipski 	if (stream1->clamping.c_depth != COLOR_DEPTH_888 ||
4083e27e10eSMikita Lipski 		stream2->clamping.c_depth != COLOR_DEPTH_888)
4093e27e10eSMikita Lipski 		return false;
4103e27e10eSMikita Lipski 
4113e27e10eSMikita Lipski 	return true;
4123e27e10eSMikita Lipski 
4133e27e10eSMikita Lipski }
4144562236bSHarry Wentland 
4154562236bSHarry Wentland static bool is_sharable_clk_src(
4164562236bSHarry Wentland 	const struct pipe_ctx *pipe_with_clk_src,
4174562236bSHarry Wentland 	const struct pipe_ctx *pipe)
4184562236bSHarry Wentland {
4194562236bSHarry Wentland 	if (pipe_with_clk_src->clock_source == NULL)
4204562236bSHarry Wentland 		return false;
4214562236bSHarry Wentland 
4224562236bSHarry Wentland 	if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
4234562236bSHarry Wentland 		return false;
4244562236bSHarry Wentland 
4253e27e10eSMikita Lipski 	if (dc_is_dp_signal(pipe_with_clk_src->stream->signal) ||
4263e27e10eSMikita Lipski 		(dc_is_dp_signal(pipe->stream->signal) &&
4273e27e10eSMikita Lipski 		!is_dp_and_hdmi_sharable(pipe_with_clk_src->stream,
4283e27e10eSMikita Lipski 				     pipe->stream)))
4294562236bSHarry Wentland 		return false;
4304562236bSHarry Wentland 
4314562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
432fc69009eSMikita Lipski 			&& dc_is_dual_link_signal(pipe->stream->signal))
4334562236bSHarry Wentland 		return false;
4344562236bSHarry Wentland 
4354562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe->stream->signal)
436fc69009eSMikita Lipski 			&& dc_is_dual_link_signal(pipe_with_clk_src->stream->signal))
4374562236bSHarry Wentland 		return false;
4384562236bSHarry Wentland 
4394562236bSHarry Wentland 	if (!resource_are_streams_timing_synchronizable(
4404562236bSHarry Wentland 			pipe_with_clk_src->stream, pipe->stream))
4414562236bSHarry Wentland 		return false;
4424562236bSHarry Wentland 
4434562236bSHarry Wentland 	return true;
4444562236bSHarry Wentland }
4454562236bSHarry Wentland 
4464562236bSHarry Wentland struct clock_source *resource_find_used_clk_src_for_sharing(
4474562236bSHarry Wentland 					struct resource_context *res_ctx,
4484562236bSHarry Wentland 					struct pipe_ctx *pipe_ctx)
4494562236bSHarry Wentland {
4504562236bSHarry Wentland 	int i;
4514562236bSHarry Wentland 
4524562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
4534562236bSHarry Wentland 		if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
4544562236bSHarry Wentland 			return res_ctx->pipe_ctx[i].clock_source;
4554562236bSHarry Wentland 	}
4564562236bSHarry Wentland 
4574562236bSHarry Wentland 	return NULL;
4584562236bSHarry Wentland }
4594562236bSHarry Wentland 
4604562236bSHarry Wentland static enum pixel_format convert_pixel_format_to_dalsurface(
4614562236bSHarry Wentland 		enum surface_pixel_format surface_pixel_format)
4624562236bSHarry Wentland {
4634562236bSHarry Wentland 	enum pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
4644562236bSHarry Wentland 
4654562236bSHarry Wentland 	switch (surface_pixel_format) {
4664562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
4674562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_INDEX8;
4684562236bSHarry Wentland 		break;
4694562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
4704562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
4714562236bSHarry Wentland 		break;
4724562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
4734562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
4744562236bSHarry Wentland 		break;
4754562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
4764562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
4774562236bSHarry Wentland 		break;
4788693049aSTony Cheng 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
4794562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
4804562236bSHarry Wentland 		break;
4814562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
4824562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
4834562236bSHarry Wentland 		break;
4844562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
4854562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
4864562236bSHarry Wentland 		break;
4874562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
4884562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
4894562236bSHarry Wentland 		break;
4904562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
4914562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
4924562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_FP16;
4934562236bSHarry Wentland 		break;
4944562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
4954562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
49687449a90SAnthony Koo 		dal_pixel_format = PIXEL_FORMAT_420BPP8;
4974562236bSHarry Wentland 		break;
498ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
499ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
50087449a90SAnthony Koo 		dal_pixel_format = PIXEL_FORMAT_420BPP10;
501ffbcd19aSVitaly Prosyak 		break;
5024562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
5034562236bSHarry Wentland 	default:
5044562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
5054562236bSHarry Wentland 		break;
5064562236bSHarry Wentland 	}
5074562236bSHarry Wentland 	return dal_pixel_format;
5084562236bSHarry Wentland }
5094562236bSHarry Wentland 
5109b6067c0SDmytro Laktyushkin static inline void get_vp_scan_direction(
5119b6067c0SDmytro Laktyushkin 	enum dc_rotation_angle rotation,
5129b6067c0SDmytro Laktyushkin 	bool horizontal_mirror,
5139b6067c0SDmytro Laktyushkin 	bool *orthogonal_rotation,
5149b6067c0SDmytro Laktyushkin 	bool *flip_vert_scan_dir,
5159b6067c0SDmytro Laktyushkin 	bool *flip_horz_scan_dir)
5164562236bSHarry Wentland {
5179b6067c0SDmytro Laktyushkin 	*orthogonal_rotation = false;
5189b6067c0SDmytro Laktyushkin 	*flip_vert_scan_dir = false;
5199b6067c0SDmytro Laktyushkin 	*flip_horz_scan_dir = false;
5209b6067c0SDmytro Laktyushkin 	if (rotation == ROTATION_ANGLE_180) {
5219b6067c0SDmytro Laktyushkin 		*flip_vert_scan_dir = true;
5229b6067c0SDmytro Laktyushkin 		*flip_horz_scan_dir = true;
5239b6067c0SDmytro Laktyushkin 	} else if (rotation == ROTATION_ANGLE_90) {
5249b6067c0SDmytro Laktyushkin 		*orthogonal_rotation = true;
5259b6067c0SDmytro Laktyushkin 		*flip_horz_scan_dir = true;
5269b6067c0SDmytro Laktyushkin 	} else if (rotation == ROTATION_ANGLE_270) {
5279b6067c0SDmytro Laktyushkin 		*orthogonal_rotation = true;
5289b6067c0SDmytro Laktyushkin 		*flip_vert_scan_dir = true;
5299b6067c0SDmytro Laktyushkin 	}
5309b6067c0SDmytro Laktyushkin 
5319b6067c0SDmytro Laktyushkin 	if (horizontal_mirror)
5329b6067c0SDmytro Laktyushkin 		*flip_horz_scan_dir = !*flip_horz_scan_dir;
5334562236bSHarry Wentland }
5344562236bSHarry Wentland 
535b2d0a103SDmytro Laktyushkin static void calculate_viewport(struct pipe_ctx *pipe_ctx)
5364562236bSHarry Wentland {
5373be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
5380971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
5396702a9acSHarry Wentland 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
5403be5262eSHarry Wentland 	struct rect surf_src = plane_state->src_rect;
5419b6067c0SDmytro Laktyushkin 	struct rect clip, dest;
54287449a90SAnthony Koo 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
54387449a90SAnthony Koo 			|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
5441fbd2cfcSDmytro Laktyushkin 	bool pri_split = pipe_ctx->bottom_pipe &&
5453be5262eSHarry Wentland 			pipe_ctx->bottom_pipe->plane_state == pipe_ctx->plane_state;
5461fbd2cfcSDmytro Laktyushkin 	bool sec_split = pipe_ctx->top_pipe &&
5473be5262eSHarry Wentland 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
5489b6067c0SDmytro Laktyushkin 	bool orthogonal_rotation, flip_y_start, flip_x_start;
54983d40659SDmytro Laktyushkin 
5507f5c22d1SVitaly Prosyak 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE ||
5517f5c22d1SVitaly Prosyak 		stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM) {
5527b779c99SVitaly Prosyak 		pri_split = false;
5537b779c99SVitaly Prosyak 		sec_split = false;
5547b779c99SVitaly Prosyak 	}
55586006a7fSDmytro Laktyushkin 
5564562236bSHarry Wentland 	/* The actual clip is an intersection between stream
5574562236bSHarry Wentland 	 * source and surface clip
5584562236bSHarry Wentland 	 */
5599b6067c0SDmytro Laktyushkin 	dest = plane_state->dst_rect;
5603be5262eSHarry Wentland 	clip.x = stream->src.x > plane_state->clip_rect.x ?
5613be5262eSHarry Wentland 			stream->src.x : plane_state->clip_rect.x;
5624562236bSHarry Wentland 
5631fbd2cfcSDmytro Laktyushkin 	clip.width = stream->src.x + stream->src.width <
5643be5262eSHarry Wentland 			plane_state->clip_rect.x + plane_state->clip_rect.width ?
5651fbd2cfcSDmytro Laktyushkin 			stream->src.x + stream->src.width - clip.x :
5663be5262eSHarry Wentland 			plane_state->clip_rect.x + plane_state->clip_rect.width - clip.x ;
5674562236bSHarry Wentland 
5683be5262eSHarry Wentland 	clip.y = stream->src.y > plane_state->clip_rect.y ?
5693be5262eSHarry Wentland 			stream->src.y : plane_state->clip_rect.y;
5704562236bSHarry Wentland 
5711fbd2cfcSDmytro Laktyushkin 	clip.height = stream->src.y + stream->src.height <
5723be5262eSHarry Wentland 			plane_state->clip_rect.y + plane_state->clip_rect.height ?
5731fbd2cfcSDmytro Laktyushkin 			stream->src.y + stream->src.height - clip.y :
5743be5262eSHarry Wentland 			plane_state->clip_rect.y + plane_state->clip_rect.height - clip.y ;
5754562236bSHarry Wentland 
5769b6067c0SDmytro Laktyushkin 	/*
5779b6067c0SDmytro Laktyushkin 	 * Need to calculate how scan origin is shifted in vp space
5789b6067c0SDmytro Laktyushkin 	 * to correctly rotate clip and dst
5799b6067c0SDmytro Laktyushkin 	 */
5809b6067c0SDmytro Laktyushkin 	get_vp_scan_direction(
5819b6067c0SDmytro Laktyushkin 			plane_state->rotation,
5829b6067c0SDmytro Laktyushkin 			plane_state->horizontal_mirror,
5839b6067c0SDmytro Laktyushkin 			&orthogonal_rotation,
5849b6067c0SDmytro Laktyushkin 			&flip_y_start,
5859b6067c0SDmytro Laktyushkin 			&flip_x_start);
5869b6067c0SDmytro Laktyushkin 
5879b6067c0SDmytro Laktyushkin 	if (orthogonal_rotation) {
5889b6067c0SDmytro Laktyushkin 		swap(clip.x, clip.y);
5899b6067c0SDmytro Laktyushkin 		swap(clip.width, clip.height);
5909b6067c0SDmytro Laktyushkin 		swap(dest.x, dest.y);
5919b6067c0SDmytro Laktyushkin 		swap(dest.width, dest.height);
5929b6067c0SDmytro Laktyushkin 	}
5939b6067c0SDmytro Laktyushkin 	if (flip_x_start) {
5949b6067c0SDmytro Laktyushkin 		clip.x = dest.x + dest.width - clip.x - clip.width;
5959b6067c0SDmytro Laktyushkin 		dest.x = 0;
5969b6067c0SDmytro Laktyushkin 	}
5979b6067c0SDmytro Laktyushkin 	if (flip_y_start) {
5989b6067c0SDmytro Laktyushkin 		clip.y = dest.y + dest.height - clip.y - clip.height;
5999b6067c0SDmytro Laktyushkin 		dest.y = 0;
6009b6067c0SDmytro Laktyushkin 	}
6019b6067c0SDmytro Laktyushkin 
60286006a7fSDmytro Laktyushkin 	/* offset = surf_src.ofs + (clip.ofs - surface->dst_rect.ofs) * scl_ratio
6034562236bSHarry Wentland 	 * num_pixels = clip.num_pix * scl_ratio
6044562236bSHarry Wentland 	 */
6059b6067c0SDmytro Laktyushkin 	data->viewport.x = surf_src.x + (clip.x - dest.x) * surf_src.width / dest.width;
6069b6067c0SDmytro Laktyushkin 	data->viewport.width = clip.width * surf_src.width / dest.width;
6074562236bSHarry Wentland 
6089b6067c0SDmytro Laktyushkin 	data->viewport.y = surf_src.y + (clip.y - dest.y) * surf_src.height / dest.height;
6099b6067c0SDmytro Laktyushkin 	data->viewport.height = clip.height * surf_src.height / dest.height;
6104562236bSHarry Wentland 
6119b6067c0SDmytro Laktyushkin 	/* Handle split */
6129b6067c0SDmytro Laktyushkin 	if (pri_split || sec_split) {
6139b6067c0SDmytro Laktyushkin 		if (orthogonal_rotation) {
6149b6067c0SDmytro Laktyushkin 			if (flip_y_start != pri_split)
6159b6067c0SDmytro Laktyushkin 				data->viewport.height /= 2;
6169b6067c0SDmytro Laktyushkin 			else {
6179b6067c0SDmytro Laktyushkin 				data->viewport.y +=  data->viewport.height / 2;
6189b6067c0SDmytro Laktyushkin 				/* Ceil offset pipe */
6199b6067c0SDmytro Laktyushkin 				data->viewport.height = (data->viewport.height + 1) / 2;
6209b6067c0SDmytro Laktyushkin 			}
6219b6067c0SDmytro Laktyushkin 		} else {
6229b6067c0SDmytro Laktyushkin 			if (flip_x_start != pri_split)
6239b6067c0SDmytro Laktyushkin 				data->viewport.width /= 2;
6249b6067c0SDmytro Laktyushkin 			else {
6259b6067c0SDmytro Laktyushkin 				data->viewport.x +=  data->viewport.width / 2;
6269b6067c0SDmytro Laktyushkin 				/* Ceil offset pipe */
6279b6067c0SDmytro Laktyushkin 				data->viewport.width = (data->viewport.width + 1) / 2;
6289b6067c0SDmytro Laktyushkin 			}
6299b6067c0SDmytro Laktyushkin 		}
6309b6067c0SDmytro Laktyushkin 	}
6319b5349f7SMartin Tsai 
632b2d0a103SDmytro Laktyushkin 	/* Round down, compensate in init */
633b2d0a103SDmytro Laktyushkin 	data->viewport_c.x = data->viewport.x / vpc_div;
634b2d0a103SDmytro Laktyushkin 	data->viewport_c.y = data->viewport.y / vpc_div;
6359b6067c0SDmytro Laktyushkin 	data->inits.h_c = (data->viewport.x % vpc_div) != 0 ? dc_fixpt_half : dc_fixpt_zero;
6369b6067c0SDmytro Laktyushkin 	data->inits.v_c = (data->viewport.y % vpc_div) != 0 ? dc_fixpt_half : dc_fixpt_zero;
6379b6067c0SDmytro Laktyushkin 
638b2d0a103SDmytro Laktyushkin 	/* Round up, assume original video size always even dimensions */
639b2d0a103SDmytro Laktyushkin 	data->viewport_c.width = (data->viewport.width + vpc_div - 1) / vpc_div;
640b2d0a103SDmytro Laktyushkin 	data->viewport_c.height = (data->viewport.height + vpc_div - 1) / vpc_div;
641b2d0a103SDmytro Laktyushkin }
6421fbd2cfcSDmytro Laktyushkin 
6439b6067c0SDmytro Laktyushkin static void calculate_recout(struct pipe_ctx *pipe_ctx)
6444562236bSHarry Wentland {
6453be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
6460971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
6473be5262eSHarry Wentland 	struct rect surf_clip = plane_state->clip_rect;
6480c31a821SYongqiang Sun 	bool pri_split = pipe_ctx->bottom_pipe &&
6490c31a821SYongqiang Sun 			pipe_ctx->bottom_pipe->plane_state == pipe_ctx->plane_state;
6500c31a821SYongqiang Sun 	bool sec_split = pipe_ctx->top_pipe &&
6510c31a821SYongqiang Sun 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
652b0131391SDmytro Laktyushkin 	bool top_bottom_split = stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM;
6534562236bSHarry Wentland 
6546702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.x = stream->dst.x;
6554fa086b9SLeo (Sunpeng) Li 	if (stream->src.x < surf_clip.x)
6566702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.x += (surf_clip.x
6574fa086b9SLeo (Sunpeng) Li 			- stream->src.x) * stream->dst.width
6584fa086b9SLeo (Sunpeng) Li 						/ stream->src.width;
6594562236bSHarry Wentland 
6606702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.width = surf_clip.width *
6614fa086b9SLeo (Sunpeng) Li 			stream->dst.width / stream->src.width;
6626702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.recout.width + pipe_ctx->plane_res.scl_data.recout.x >
6634fa086b9SLeo (Sunpeng) Li 			stream->dst.x + stream->dst.width)
6646702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.width =
6654fa086b9SLeo (Sunpeng) Li 			stream->dst.x + stream->dst.width
6666702a9acSHarry Wentland 						- pipe_ctx->plane_res.scl_data.recout.x;
6674562236bSHarry Wentland 
6686702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.y = stream->dst.y;
6694fa086b9SLeo (Sunpeng) Li 	if (stream->src.y < surf_clip.y)
6706702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.y += (surf_clip.y
6714fa086b9SLeo (Sunpeng) Li 			- stream->src.y) * stream->dst.height
6724fa086b9SLeo (Sunpeng) Li 						/ stream->src.height;
6734562236bSHarry Wentland 
6746702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.recout.height = surf_clip.height *
6754fa086b9SLeo (Sunpeng) Li 			stream->dst.height / stream->src.height;
6766702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.recout.height + pipe_ctx->plane_res.scl_data.recout.y >
6774fa086b9SLeo (Sunpeng) Li 			stream->dst.y + stream->dst.height)
6786702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.height =
6794fa086b9SLeo (Sunpeng) Li 			stream->dst.y + stream->dst.height
6806702a9acSHarry Wentland 						- pipe_ctx->plane_res.scl_data.recout.y;
681b2d0a103SDmytro Laktyushkin 
6829b6067c0SDmytro Laktyushkin 	/* Handle h & v split, handle rotation using viewport */
683b0131391SDmytro Laktyushkin 	if (sec_split && top_bottom_split) {
684b0131391SDmytro Laktyushkin 		pipe_ctx->plane_res.scl_data.recout.y +=
685b0131391SDmytro Laktyushkin 				pipe_ctx->plane_res.scl_data.recout.height / 2;
6867b779c99SVitaly Prosyak 		/* Floor primary pipe, ceil 2ndary pipe */
687b0131391SDmytro Laktyushkin 		pipe_ctx->plane_res.scl_data.recout.height =
688b0131391SDmytro Laktyushkin 				(pipe_ctx->plane_res.scl_data.recout.height + 1) / 2;
689b0131391SDmytro Laktyushkin 	} else if (pri_split && top_bottom_split)
6906702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.height /= 2;
6919b6067c0SDmytro Laktyushkin 	else if (sec_split) {
6920c31a821SYongqiang Sun 		pipe_ctx->plane_res.scl_data.recout.x +=
6930c31a821SYongqiang Sun 				pipe_ctx->plane_res.scl_data.recout.width / 2;
6940c31a821SYongqiang Sun 		/* Ceil offset pipe */
6950c31a821SYongqiang Sun 		pipe_ctx->plane_res.scl_data.recout.width =
6960c31a821SYongqiang Sun 				(pipe_ctx->plane_res.scl_data.recout.width + 1) / 2;
6979b6067c0SDmytro Laktyushkin 	} else if (pri_split)
6986702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.recout.width /= 2;
6994562236bSHarry Wentland }
700b2d0a103SDmytro Laktyushkin 
701b2d0a103SDmytro Laktyushkin static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
7024562236bSHarry Wentland {
7033be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
7040971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
7053be5262eSHarry Wentland 	struct rect surf_src = plane_state->src_rect;
7064fa086b9SLeo (Sunpeng) Li 	const int in_w = stream->src.width;
7074fa086b9SLeo (Sunpeng) Li 	const int in_h = stream->src.height;
7084fa086b9SLeo (Sunpeng) Li 	const int out_w = stream->dst.width;
7094fa086b9SLeo (Sunpeng) Li 	const int out_h = stream->dst.height;
7104562236bSHarry Wentland 
7119b6067c0SDmytro Laktyushkin 	/*Swap surf_src height and width since scaling ratios are in recout rotation*/
7123be5262eSHarry Wentland 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
7133be5262eSHarry Wentland 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
7149b6067c0SDmytro Laktyushkin 		swap(surf_src.height, surf_src.width);
71586006a7fSDmytro Laktyushkin 
716eb0e5154SDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_from_fraction(
71786006a7fSDmytro Laktyushkin 					surf_src.width,
7183be5262eSHarry Wentland 					plane_state->dst_rect.width);
719eb0e5154SDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_from_fraction(
72086006a7fSDmytro Laktyushkin 					surf_src.height,
7213be5262eSHarry Wentland 					plane_state->dst_rect.height);
7224562236bSHarry Wentland 
7234fa086b9SLeo (Sunpeng) Li 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
7246702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz.value *= 2;
7254fa086b9SLeo (Sunpeng) Li 	else if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
7266702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert.value *= 2;
7274562236bSHarry Wentland 
7286702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.vert.value = div64_s64(
7296702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert.value * in_h, out_h);
7306702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.horz.value = div64_s64(
7316702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz.value * in_w, out_w);
7324562236bSHarry Wentland 
7336702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.horz_c = pipe_ctx->plane_res.scl_data.ratios.horz;
7346702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.vert_c = pipe_ctx->plane_res.scl_data.ratios.vert;
7354562236bSHarry Wentland 
7366702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP8
7376702a9acSHarry Wentland 			|| pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP10) {
7386702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz_c.value /= 2;
7396702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert_c.value /= 2;
7404562236bSHarry Wentland 	}
7410002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_truncate(
7420002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.horz, 19);
7430002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_truncate(
7440002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.vert, 19);
7450002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.horz_c = dc_fixpt_truncate(
7460002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.horz_c, 19);
7470002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.vert_c = dc_fixpt_truncate(
7480002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.vert_c, 19);
7494562236bSHarry Wentland }
7504562236bSHarry Wentland 
7519b6067c0SDmytro Laktyushkin static inline void adjust_vp_and_init_for_seamless_clip(
7529b6067c0SDmytro Laktyushkin 		bool flip_scan_dir,
7539b6067c0SDmytro Laktyushkin 		int recout_skip,
7549b6067c0SDmytro Laktyushkin 		int src_size,
7559b6067c0SDmytro Laktyushkin 		int taps,
7569b6067c0SDmytro Laktyushkin 		struct fixed31_32 ratio,
7579b6067c0SDmytro Laktyushkin 		struct fixed31_32 *init,
7589b6067c0SDmytro Laktyushkin 		int *vp_offset,
7599b6067c0SDmytro Laktyushkin 		int *vp_size)
7604562236bSHarry Wentland {
7619b6067c0SDmytro Laktyushkin 	if (!flip_scan_dir) {
7629b6067c0SDmytro Laktyushkin 		/* Adjust for viewport end clip-off */
7639b6067c0SDmytro Laktyushkin 		if ((*vp_offset + *vp_size) < src_size) {
7649b6067c0SDmytro Laktyushkin 			int vp_clip = src_size - *vp_size - *vp_offset;
7659b6067c0SDmytro Laktyushkin 			int int_part = dc_fixpt_floor(dc_fixpt_sub(*init, ratio));
7669b6067c0SDmytro Laktyushkin 
7679b6067c0SDmytro Laktyushkin 			int_part = int_part > 0 ? int_part : 0;
7689b6067c0SDmytro Laktyushkin 			*vp_size += int_part < vp_clip ? int_part : vp_clip;
7699b6067c0SDmytro Laktyushkin 		}
7709b6067c0SDmytro Laktyushkin 
7719b6067c0SDmytro Laktyushkin 		/* Adjust for non-0 viewport offset */
7729b6067c0SDmytro Laktyushkin 		if (*vp_offset) {
7739b6067c0SDmytro Laktyushkin 			int int_part;
7749b6067c0SDmytro Laktyushkin 
7759b6067c0SDmytro Laktyushkin 			*init = dc_fixpt_add(*init, dc_fixpt_mul_int(ratio, recout_skip));
7769b6067c0SDmytro Laktyushkin 			int_part = dc_fixpt_floor(*init) - *vp_offset;
7779b6067c0SDmytro Laktyushkin 			if (int_part < taps) {
7789b6067c0SDmytro Laktyushkin 				int int_adj = *vp_offset >= (taps - int_part) ?
7799b6067c0SDmytro Laktyushkin 							(taps - int_part) : *vp_offset;
7809b6067c0SDmytro Laktyushkin 				*vp_offset -= int_adj;
7819b6067c0SDmytro Laktyushkin 				*vp_size += int_adj;
7829b6067c0SDmytro Laktyushkin 				int_part += int_adj;
7839b6067c0SDmytro Laktyushkin 			} else if (int_part > taps) {
7849b6067c0SDmytro Laktyushkin 				*vp_offset += int_part - taps;
7859b6067c0SDmytro Laktyushkin 				*vp_size -= int_part - taps;
7869b6067c0SDmytro Laktyushkin 				int_part = taps;
7879b6067c0SDmytro Laktyushkin 			}
7889b6067c0SDmytro Laktyushkin 			init->value &= 0xffffffff;
7899b6067c0SDmytro Laktyushkin 			*init = dc_fixpt_add_int(*init, int_part);
7909b6067c0SDmytro Laktyushkin 		}
7919b6067c0SDmytro Laktyushkin 	} else {
7929b6067c0SDmytro Laktyushkin 		/* Adjust for non-0 viewport offset */
7939b6067c0SDmytro Laktyushkin 		if (*vp_offset) {
7949b6067c0SDmytro Laktyushkin 			int int_part = dc_fixpt_floor(dc_fixpt_sub(*init, ratio));
7959b6067c0SDmytro Laktyushkin 
7969b6067c0SDmytro Laktyushkin 			int_part = int_part > 0 ? int_part : 0;
7979b6067c0SDmytro Laktyushkin 			*vp_size += int_part < *vp_offset ? int_part : *vp_offset;
7989b6067c0SDmytro Laktyushkin 			*vp_offset -= int_part < *vp_offset ? int_part : *vp_offset;
7999b6067c0SDmytro Laktyushkin 		}
8009b6067c0SDmytro Laktyushkin 
8019b6067c0SDmytro Laktyushkin 		/* Adjust for viewport end clip-off */
8029b6067c0SDmytro Laktyushkin 		if ((*vp_offset + *vp_size) < src_size) {
8039b6067c0SDmytro Laktyushkin 			int int_part;
8049b6067c0SDmytro Laktyushkin 			int end_offset = src_size - *vp_offset - *vp_size;
8059b6067c0SDmytro Laktyushkin 
8069b6067c0SDmytro Laktyushkin 			/*
8079b6067c0SDmytro Laktyushkin 			 * this is init if vp had no offset, keep in mind this is from the
8089b6067c0SDmytro Laktyushkin 			 * right side of vp due to scan direction
8099b6067c0SDmytro Laktyushkin 			 */
8109b6067c0SDmytro Laktyushkin 			*init = dc_fixpt_add(*init, dc_fixpt_mul_int(ratio, recout_skip));
8119b6067c0SDmytro Laktyushkin 			/*
8129b6067c0SDmytro Laktyushkin 			 * this is the difference between first pixel of viewport available to read
8139b6067c0SDmytro Laktyushkin 			 * and init position, takning into account scan direction
8149b6067c0SDmytro Laktyushkin 			 */
8159b6067c0SDmytro Laktyushkin 			int_part = dc_fixpt_floor(*init) - end_offset;
8169b6067c0SDmytro Laktyushkin 			if (int_part < taps) {
8179b6067c0SDmytro Laktyushkin 				int int_adj = end_offset >= (taps - int_part) ?
8189b6067c0SDmytro Laktyushkin 							(taps - int_part) : end_offset;
8199b6067c0SDmytro Laktyushkin 				*vp_size += int_adj;
8209b6067c0SDmytro Laktyushkin 				int_part += int_adj;
8219b6067c0SDmytro Laktyushkin 			} else if (int_part > taps) {
8229b6067c0SDmytro Laktyushkin 				*vp_size += int_part - taps;
8239b6067c0SDmytro Laktyushkin 				int_part = taps;
8249b6067c0SDmytro Laktyushkin 			}
8259b6067c0SDmytro Laktyushkin 			init->value &= 0xffffffff;
8269b6067c0SDmytro Laktyushkin 			*init = dc_fixpt_add_int(*init, int_part);
8279b6067c0SDmytro Laktyushkin 		}
8289b6067c0SDmytro Laktyushkin 	}
8299b6067c0SDmytro Laktyushkin }
8309b6067c0SDmytro Laktyushkin 
8319b6067c0SDmytro Laktyushkin static void calculate_inits_and_adj_vp(struct pipe_ctx *pipe_ctx)
8329b6067c0SDmytro Laktyushkin {
8339b6067c0SDmytro Laktyushkin 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
8349b6067c0SDmytro Laktyushkin 	const struct dc_stream_state *stream = pipe_ctx->stream;
8356702a9acSHarry Wentland 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
8363be5262eSHarry Wentland 	struct rect src = pipe_ctx->plane_state->src_rect;
8379b6067c0SDmytro Laktyushkin 	int recout_skip_h, recout_skip_v, surf_size_h, surf_size_v;
83887449a90SAnthony Koo 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
83987449a90SAnthony Koo 			|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
8409b6067c0SDmytro Laktyushkin 	bool orthogonal_rotation, flip_vert_scan_dir, flip_horz_scan_dir;
841b2d0a103SDmytro Laktyushkin 
842b0131391SDmytro Laktyushkin 	/*
843b0131391SDmytro Laktyushkin 	 * Need to calculate the scan direction for viewport to make adjustments
844b0131391SDmytro Laktyushkin 	 */
8459b6067c0SDmytro Laktyushkin 	get_vp_scan_direction(
8469b6067c0SDmytro Laktyushkin 			plane_state->rotation,
8479b6067c0SDmytro Laktyushkin 			plane_state->horizontal_mirror,
8489b6067c0SDmytro Laktyushkin 			&orthogonal_rotation,
8499b6067c0SDmytro Laktyushkin 			&flip_vert_scan_dir,
8509b6067c0SDmytro Laktyushkin 			&flip_horz_scan_dir);
851b0131391SDmytro Laktyushkin 
8529b6067c0SDmytro Laktyushkin 	/* Calculate src rect rotation adjusted to recout space */
8539b6067c0SDmytro Laktyushkin 	surf_size_h = src.x + src.width;
8549b6067c0SDmytro Laktyushkin 	surf_size_v = src.y + src.height;
8559b6067c0SDmytro Laktyushkin 	if (flip_horz_scan_dir)
8569b6067c0SDmytro Laktyushkin 		src.x = 0;
8579b6067c0SDmytro Laktyushkin 	if (flip_vert_scan_dir)
8589b6067c0SDmytro Laktyushkin 		src.y = 0;
8599b6067c0SDmytro Laktyushkin 	if (orthogonal_rotation) {
8609b6067c0SDmytro Laktyushkin 		swap(src.x, src.y);
8619b6067c0SDmytro Laktyushkin 		swap(src.width, src.height);
8629b5349f7SMartin Tsai 	}
8631fbd2cfcSDmytro Laktyushkin 
8649b6067c0SDmytro Laktyushkin 	/* Recout matching initial vp offset = recout_offset - (stream dst offset +
8659b6067c0SDmytro Laktyushkin 	 *			((surf dst offset - stream src offset) * 1/ stream scaling ratio)
8669b6067c0SDmytro Laktyushkin 	 *			- (surf surf_src offset * 1/ full scl ratio))
8679b6067c0SDmytro Laktyushkin 	 */
8689b6067c0SDmytro Laktyushkin 	recout_skip_h = data->recout.x - (stream->dst.x + (plane_state->dst_rect.x - stream->src.x)
8699b6067c0SDmytro Laktyushkin 					* stream->dst.width / stream->src.width -
8709b6067c0SDmytro Laktyushkin 					src.x * plane_state->dst_rect.width / src.width
8719b6067c0SDmytro Laktyushkin 					* stream->dst.width / stream->src.width);
8729b6067c0SDmytro Laktyushkin 	recout_skip_v = data->recout.y - (stream->dst.y + (plane_state->dst_rect.y - stream->src.y)
8739b6067c0SDmytro Laktyushkin 					* stream->dst.height / stream->src.height -
8749b6067c0SDmytro Laktyushkin 					src.y * plane_state->dst_rect.height / src.height
8759b6067c0SDmytro Laktyushkin 					* stream->dst.height / stream->src.height);
8769b6067c0SDmytro Laktyushkin 	if (orthogonal_rotation)
8779b6067c0SDmytro Laktyushkin 		swap(recout_skip_h, recout_skip_v);
878b2d0a103SDmytro Laktyushkin 	/*
879b2d0a103SDmytro Laktyushkin 	 * Init calculated according to formula:
880b2d0a103SDmytro Laktyushkin 	 * 	init = (scaling_ratio + number_of_taps + 1) / 2
881b2d0a103SDmytro Laktyushkin 	 * 	init_bot = init + scaling_ratio
882b2d0a103SDmytro Laktyushkin 	 * 	init_c = init + truncated_vp_c_offset(from calculate viewport)
883b2d0a103SDmytro Laktyushkin 	 */
8840002d3acSDmytro Laktyushkin 	data->inits.h = dc_fixpt_truncate(dc_fixpt_div_int(
8850002d3acSDmytro Laktyushkin 			dc_fixpt_add_int(data->ratios.horz, data->taps.h_taps + 1), 2), 19);
886b2d0a103SDmytro Laktyushkin 
8870002d3acSDmytro Laktyushkin 	data->inits.h_c = dc_fixpt_truncate(dc_fixpt_add(data->inits.h_c, dc_fixpt_div_int(
8880002d3acSDmytro Laktyushkin 			dc_fixpt_add_int(data->ratios.horz_c, data->taps.h_taps_c + 1), 2)), 19);
889b2d0a103SDmytro Laktyushkin 
8900002d3acSDmytro Laktyushkin 	data->inits.v = dc_fixpt_truncate(dc_fixpt_div_int(
8910002d3acSDmytro Laktyushkin 			dc_fixpt_add_int(data->ratios.vert, data->taps.v_taps + 1), 2), 19);
892b2d0a103SDmytro Laktyushkin 
8930002d3acSDmytro Laktyushkin 	data->inits.v_c = dc_fixpt_truncate(dc_fixpt_add(data->inits.v_c, dc_fixpt_div_int(
8940002d3acSDmytro Laktyushkin 			dc_fixpt_add_int(data->ratios.vert_c, data->taps.v_taps_c + 1), 2)), 19);
8950002d3acSDmytro Laktyushkin 
8969a08f51fSDmytro Laktyushkin 	/*
8979b6067c0SDmytro Laktyushkin 	 * Taps, inits and scaling ratios are in recout space need to rotate
8989b6067c0SDmytro Laktyushkin 	 * to viewport rotation before adjustment
8999a08f51fSDmytro Laktyushkin 	 */
9009b6067c0SDmytro Laktyushkin 	adjust_vp_and_init_for_seamless_clip(
9019b6067c0SDmytro Laktyushkin 			flip_horz_scan_dir,
9029b6067c0SDmytro Laktyushkin 			recout_skip_h,
9039b6067c0SDmytro Laktyushkin 			surf_size_h,
9049b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->taps.v_taps : data->taps.h_taps,
9059b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->ratios.vert : data->ratios.horz,
9069b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? &data->inits.v : &data->inits.h,
9079b6067c0SDmytro Laktyushkin 			&data->viewport.x,
9089b6067c0SDmytro Laktyushkin 			&data->viewport.width);
9099b6067c0SDmytro Laktyushkin 	adjust_vp_and_init_for_seamless_clip(
9109b6067c0SDmytro Laktyushkin 			flip_horz_scan_dir,
9119b6067c0SDmytro Laktyushkin 			recout_skip_h,
9129b6067c0SDmytro Laktyushkin 			surf_size_h / vpc_div,
9139b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->taps.v_taps_c : data->taps.h_taps_c,
9149b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->ratios.vert_c : data->ratios.horz_c,
9159b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? &data->inits.v_c : &data->inits.h_c,
9169b6067c0SDmytro Laktyushkin 			&data->viewport_c.x,
9179b6067c0SDmytro Laktyushkin 			&data->viewport_c.width);
9189b6067c0SDmytro Laktyushkin 	adjust_vp_and_init_for_seamless_clip(
9199b6067c0SDmytro Laktyushkin 			flip_vert_scan_dir,
9209b6067c0SDmytro Laktyushkin 			recout_skip_v,
9219b6067c0SDmytro Laktyushkin 			surf_size_v,
9229b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->taps.h_taps : data->taps.v_taps,
9239b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->ratios.horz : data->ratios.vert,
9249b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? &data->inits.h : &data->inits.v,
9259b6067c0SDmytro Laktyushkin 			&data->viewport.y,
9269b6067c0SDmytro Laktyushkin 			&data->viewport.height);
9279b6067c0SDmytro Laktyushkin 	adjust_vp_and_init_for_seamless_clip(
9289b6067c0SDmytro Laktyushkin 			flip_vert_scan_dir,
9299b6067c0SDmytro Laktyushkin 			recout_skip_v,
9309b6067c0SDmytro Laktyushkin 			surf_size_v / vpc_div,
9319b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->taps.h_taps_c : data->taps.v_taps_c,
9329b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? data->ratios.horz_c : data->ratios.vert_c,
9339b6067c0SDmytro Laktyushkin 			orthogonal_rotation ? &data->inits.h_c : &data->inits.v_c,
9349b6067c0SDmytro Laktyushkin 			&data->viewport_c.y,
9359b6067c0SDmytro Laktyushkin 			&data->viewport_c.height);
936b2d0a103SDmytro Laktyushkin 
937b2d0a103SDmytro Laktyushkin 	/* Interlaced inits based on final vert inits */
938eb0e5154SDmytro Laktyushkin 	data->inits.v_bot = dc_fixpt_add(data->inits.v, data->ratios.vert);
939eb0e5154SDmytro Laktyushkin 	data->inits.v_c_bot = dc_fixpt_add(data->inits.v_c, data->ratios.vert_c);
9401fbd2cfcSDmytro Laktyushkin 
941b2d0a103SDmytro Laktyushkin }
9423b733278SReza Amini 
943663bfef0SAidan Yang static bool is_downscaled(const struct rect *src_rect, const struct rect *dst_rect)
944663bfef0SAidan Yang {
945663bfef0SAidan Yang         if (src_rect->width > dst_rect->width || src_rect->height > dst_rect->height)
946663bfef0SAidan Yang 		return true;
947663bfef0SAidan Yang 	return false;
948663bfef0SAidan Yang }
949663bfef0SAidan Yang 
950663bfef0SAidan Yang static bool is_mpo(int layer_index)
951663bfef0SAidan Yang {
952663bfef0SAidan Yang 	if (layer_index > 0)
953663bfef0SAidan Yang 		return true;
954663bfef0SAidan Yang 	return false;
955663bfef0SAidan Yang }
956663bfef0SAidan Yang 
9574ef0b9d0SYueHaibing static void calculate_integer_scaling(struct pipe_ctx *pipe_ctx)
9583b733278SReza Amini {
95900ada9d1SReza Amini 	unsigned int integer_multiple = 1;
9603b733278SReza Amini 
961663bfef0SAidan Yang 	if (pipe_ctx->plane_state->scaling_quality.integer_scaling &&
962663bfef0SAidan Yang 	    !is_downscaled(&pipe_ctx->plane_state->src_rect, &pipe_ctx->plane_state->dst_rect) &&
963663bfef0SAidan Yang 	    !is_mpo(pipe_ctx->plane_state->layer_index)) {
9643b733278SReza Amini 		// calculate maximum # of replication of src onto addressable
96500ada9d1SReza Amini 		integer_multiple = min(
9663b733278SReza Amini 				pipe_ctx->stream->timing.h_addressable / pipe_ctx->stream->src.width,
9673b733278SReza Amini 				pipe_ctx->stream->timing.v_addressable  / pipe_ctx->stream->src.height);
9683b733278SReza Amini 
9693b733278SReza Amini 		//scale dst
9703b733278SReza Amini 		pipe_ctx->stream->dst.width  = integer_multiple * pipe_ctx->stream->src.width;
9713b733278SReza Amini 		pipe_ctx->stream->dst.height = integer_multiple * pipe_ctx->stream->src.height;
9723b733278SReza Amini 
9733b733278SReza Amini 		//center dst onto addressable
9743b733278SReza Amini 		pipe_ctx->stream->dst.x = (pipe_ctx->stream->timing.h_addressable - pipe_ctx->stream->dst.width)/2;
9753b733278SReza Amini 		pipe_ctx->stream->dst.y = (pipe_ctx->stream->timing.v_addressable - pipe_ctx->stream->dst.height)/2;
9763b733278SReza Amini 
97700ada9d1SReza Amini 		//We are guaranteed that we are scaling in integer ratio
9783b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.v_taps = 1;
9793b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.h_taps = 1;
9803b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.v_taps_c = 1;
9813b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.h_taps_c = 1;
9823b733278SReza Amini 	}
9833b733278SReza Amini }
9843b733278SReza Amini 
985b2d0a103SDmytro Laktyushkin bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
986b2d0a103SDmytro Laktyushkin {
9873be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
9884fa086b9SLeo (Sunpeng) Li 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
989b2d0a103SDmytro Laktyushkin 	bool res = false;
9905d4b05ddSBhawanpreet Lakha 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
9914562236bSHarry Wentland 	/* Important: scaling ratio calculation requires pixel format,
9924562236bSHarry Wentland 	 * lb depth calculation requires recout and taps require scaling ratios.
993b2d0a103SDmytro Laktyushkin 	 * Inits require viewport, taps, ratios and recout of split pipe
9944562236bSHarry Wentland 	 */
9956702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
9963be5262eSHarry Wentland 			pipe_ctx->plane_state->format);
9974562236bSHarry Wentland 
9983b733278SReza Amini 	calculate_integer_scaling(pipe_ctx);
9993b733278SReza Amini 
1000b2d0a103SDmytro Laktyushkin 	calculate_scaling_ratios(pipe_ctx);
1001b2d0a103SDmytro Laktyushkin 
1002b2d0a103SDmytro Laktyushkin 	calculate_viewport(pipe_ctx);
10034562236bSHarry Wentland 
10046702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.viewport.height < 16 || pipe_ctx->plane_res.scl_data.viewport.width < 16)
10054562236bSHarry Wentland 		return false;
10064562236bSHarry Wentland 
10079b6067c0SDmytro Laktyushkin 	calculate_recout(pipe_ctx);
10084562236bSHarry Wentland 
10094562236bSHarry Wentland 	/**
10104562236bSHarry Wentland 	 * Setting line buffer pixel depth to 24bpp yields banding
10114562236bSHarry Wentland 	 * on certain displays, such as the Sharp 4k
10124562236bSHarry Wentland 	 */
10136702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
10144562236bSHarry Wentland 
1015199e458aSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.recout.x += timing->h_border_left;
101658bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.recout.y += timing->v_border_top;
1017199e458aSDmytro Laktyushkin 
101858bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.h_active = timing->h_addressable + timing->h_border_left + timing->h_border_right;
101958bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.v_active = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
10201b6c8067SBhawanpreet Lakha 
10214562236bSHarry Wentland 	/* Taps calculations */
1022d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.xfm != NULL)
102386a66c4eSHarry Wentland 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
102486a66c4eSHarry Wentland 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
10254562236bSHarry Wentland 
1026d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.dpp != NULL)
1027d94585a0SYue Hin Lau 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1028d94585a0SYue Hin Lau 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1029f7938bc0SReza Amini 
1030f7938bc0SReza Amini 
10314562236bSHarry Wentland 	if (!res) {
10324562236bSHarry Wentland 		/* Try 24 bpp linebuffer */
10336702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
10344562236bSHarry Wentland 
10351b6c8067SBhawanpreet Lakha 		if (pipe_ctx->plane_res.xfm != NULL)
103686a66c4eSHarry Wentland 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
10371b6c8067SBhawanpreet Lakha 					pipe_ctx->plane_res.xfm,
10381b6c8067SBhawanpreet Lakha 					&pipe_ctx->plane_res.scl_data,
10391b6c8067SBhawanpreet Lakha 					&plane_state->scaling_quality);
1040d94585a0SYue Hin Lau 
10411b6c8067SBhawanpreet Lakha 		if (pipe_ctx->plane_res.dpp != NULL)
1042d94585a0SYue Hin Lau 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
10431b6c8067SBhawanpreet Lakha 					pipe_ctx->plane_res.dpp,
10441b6c8067SBhawanpreet Lakha 					&pipe_ctx->plane_res.scl_data,
10451b6c8067SBhawanpreet Lakha 					&plane_state->scaling_quality);
10464562236bSHarry Wentland 	}
10474562236bSHarry Wentland 
1048b2d0a103SDmytro Laktyushkin 	if (res)
10491fbd2cfcSDmytro Laktyushkin 		/* May need to re-check lb size after this in some obscure scenario */
10509b6067c0SDmytro Laktyushkin 		calculate_inits_and_adj_vp(pipe_ctx);
1051b2d0a103SDmytro Laktyushkin 
10521296423bSBhawanpreet Lakha 	DC_LOG_SCALER(
10534562236bSHarry Wentland 				"%s: Viewport:\nheight:%d width:%d x:%d "
10544562236bSHarry Wentland 				"y:%d\n dst_rect:\nheight:%d width:%d x:%d "
10554562236bSHarry Wentland 				"y:%d\n",
10564562236bSHarry Wentland 				__func__,
10576702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.height,
10586702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.width,
10596702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.x,
10606702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.y,
10613be5262eSHarry Wentland 				plane_state->dst_rect.height,
10623be5262eSHarry Wentland 				plane_state->dst_rect.width,
10633be5262eSHarry Wentland 				plane_state->dst_rect.x,
10643be5262eSHarry Wentland 				plane_state->dst_rect.y);
10654562236bSHarry Wentland 
10664562236bSHarry Wentland 	return res;
10674562236bSHarry Wentland }
10684562236bSHarry Wentland 
10694562236bSHarry Wentland 
10704562236bSHarry Wentland enum dc_status resource_build_scaling_params_for_context(
1071fb3466a4SBhawanpreet Lakha 	const struct dc  *dc,
1072608ac7bbSJerry Zuo 	struct dc_state *context)
10734562236bSHarry Wentland {
10744562236bSHarry Wentland 	int i;
10754562236bSHarry Wentland 
10764562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
10773be5262eSHarry Wentland 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
10784562236bSHarry Wentland 				context->res_ctx.pipe_ctx[i].stream != NULL)
1079b2d0a103SDmytro Laktyushkin 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1080f84a8161STony Cheng 				return DC_FAIL_SCALING;
10814562236bSHarry Wentland 	}
10824562236bSHarry Wentland 
10834562236bSHarry Wentland 	return DC_OK;
10844562236bSHarry Wentland }
10854562236bSHarry Wentland 
1086a2b8659dSTony Cheng struct pipe_ctx *find_idle_secondary_pipe(
1087a2b8659dSTony Cheng 		struct resource_context *res_ctx,
10885581192dSJun Lei 		const struct resource_pool *pool,
10895581192dSJun Lei 		const struct pipe_ctx *primary_pipe)
10904562236bSHarry Wentland {
10914562236bSHarry Wentland 	int i;
10924562236bSHarry Wentland 	struct pipe_ctx *secondary_pipe = NULL;
10934562236bSHarry Wentland 
10944562236bSHarry Wentland 	/*
10955581192dSJun Lei 	 * We add a preferred pipe mapping to avoid the chance that
10965581192dSJun Lei 	 * MPCCs already in use will need to be reassigned to other trees.
10975581192dSJun Lei 	 * For example, if we went with the strict, assign backwards logic:
10985581192dSJun Lei 	 *
10995581192dSJun Lei 	 * (State 1)
11005581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
11015581192dSJun Lei 	 * Display B on, no surface, top pipe = 1
11025581192dSJun Lei 	 *
11035581192dSJun Lei 	 * (State 2)
11045581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
11055581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
11065581192dSJun Lei 	 *
11075581192dSJun Lei 	 * (State 3)
11085581192dSJun Lei 	 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
11095581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
11105581192dSJun Lei 	 *
11115581192dSJun Lei 	 * The state 2->3 transition requires remapping MPCC 5 from display B
11125581192dSJun Lei 	 * to display A.
11135581192dSJun Lei 	 *
11145581192dSJun Lei 	 * However, with the preferred pipe logic, state 2 would look like:
11155581192dSJun Lei 	 *
11165581192dSJun Lei 	 * (State 2)
11175581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
11185581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
11195581192dSJun Lei 	 *
11205581192dSJun Lei 	 * This would then cause 2->3 to not require remapping any MPCCs.
11215581192dSJun Lei 	 */
11225581192dSJun Lei 	if (primary_pipe) {
11235581192dSJun Lei 		int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
11245581192dSJun Lei 		if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
11255581192dSJun Lei 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
11265581192dSJun Lei 			secondary_pipe->pipe_idx = preferred_pipe_idx;
11275581192dSJun Lei 		}
11285581192dSJun Lei 	}
11295581192dSJun Lei 
11305581192dSJun Lei 	/*
11314562236bSHarry Wentland 	 * search backwards for the second pipe to keep pipe
11324562236bSHarry Wentland 	 * assignment more consistent
11334562236bSHarry Wentland 	 */
11345581192dSJun Lei 	if (!secondary_pipe)
1135a2b8659dSTony Cheng 		for (i = pool->pipe_count - 1; i >= 0; i--) {
11364562236bSHarry Wentland 			if (res_ctx->pipe_ctx[i].stream == NULL) {
11374562236bSHarry Wentland 				secondary_pipe = &res_ctx->pipe_ctx[i];
11384562236bSHarry Wentland 				secondary_pipe->pipe_idx = i;
11394562236bSHarry Wentland 				break;
11404562236bSHarry Wentland 			}
11414562236bSHarry Wentland 		}
11424562236bSHarry Wentland 
11434562236bSHarry Wentland 	return secondary_pipe;
11444562236bSHarry Wentland }
11454562236bSHarry Wentland 
11464562236bSHarry Wentland struct pipe_ctx *resource_get_head_pipe_for_stream(
11474562236bSHarry Wentland 		struct resource_context *res_ctx,
11480971c40eSHarry Wentland 		struct dc_stream_state *stream)
11494562236bSHarry Wentland {
11504562236bSHarry Wentland 	int i;
115122498036SDmytro Laktyushkin 
1152a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
1153b1f6d01cSDmytro Laktyushkin 		if (res_ctx->pipe_ctx[i].stream == stream
1154b1f6d01cSDmytro Laktyushkin 				&& !res_ctx->pipe_ctx[i].top_pipe
115522498036SDmytro Laktyushkin 				&& !res_ctx->pipe_ctx[i].prev_odm_pipe)
11564562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
11574562236bSHarry Wentland 	}
11584562236bSHarry Wentland 	return NULL;
11594562236bSHarry Wentland }
11604562236bSHarry Wentland 
1161b1f6d01cSDmytro Laktyushkin static struct pipe_ctx *resource_get_tail_pipe(
116219f89e23SAndrey Grodzovsky 		struct resource_context *res_ctx,
1163b1f6d01cSDmytro Laktyushkin 		struct pipe_ctx *head_pipe)
116419f89e23SAndrey Grodzovsky {
1165b1f6d01cSDmytro Laktyushkin 	struct pipe_ctx *tail_pipe;
116619f89e23SAndrey Grodzovsky 
116719f89e23SAndrey Grodzovsky 	tail_pipe = head_pipe->bottom_pipe;
116819f89e23SAndrey Grodzovsky 
116919f89e23SAndrey Grodzovsky 	while (tail_pipe) {
117019f89e23SAndrey Grodzovsky 		head_pipe = tail_pipe;
117119f89e23SAndrey Grodzovsky 		tail_pipe = tail_pipe->bottom_pipe;
117219f89e23SAndrey Grodzovsky 	}
117319f89e23SAndrey Grodzovsky 
117419f89e23SAndrey Grodzovsky 	return head_pipe;
117519f89e23SAndrey Grodzovsky }
117619f89e23SAndrey Grodzovsky 
11774562236bSHarry Wentland /*
1178ab2541b6SAric Cyr  * A free_pipe for a stream is defined here as a pipe
1179ab2541b6SAric Cyr  * that has no surface attached yet
11804562236bSHarry Wentland  */
1181b1f6d01cSDmytro Laktyushkin static struct pipe_ctx *acquire_free_pipe_for_head(
1182608ac7bbSJerry Zuo 		struct dc_state *context,
1183a2b8659dSTony Cheng 		const struct resource_pool *pool,
1184b1f6d01cSDmytro Laktyushkin 		struct pipe_ctx *head_pipe)
11854562236bSHarry Wentland {
11864562236bSHarry Wentland 	int i;
1187745cc746SDmytro Laktyushkin 	struct resource_context *res_ctx = &context->res_ctx;
11884562236bSHarry Wentland 
11893be5262eSHarry Wentland 	if (!head_pipe->plane_state)
11904562236bSHarry Wentland 		return head_pipe;
11914562236bSHarry Wentland 
11924562236bSHarry Wentland 	/* Re-use pipe already acquired for this stream if available*/
1193a2b8659dSTony Cheng 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1194b1f6d01cSDmytro Laktyushkin 		if (res_ctx->pipe_ctx[i].stream == head_pipe->stream &&
11953be5262eSHarry Wentland 				!res_ctx->pipe_ctx[i].plane_state) {
11964562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
11974562236bSHarry Wentland 		}
11984562236bSHarry Wentland 	}
11994562236bSHarry Wentland 
12004562236bSHarry Wentland 	/*
12014562236bSHarry Wentland 	 * At this point we have no re-useable pipe for this stream and we need
12024562236bSHarry Wentland 	 * to acquire an idle one to satisfy the request
12034562236bSHarry Wentland 	 */
12044562236bSHarry Wentland 
1205a2b8659dSTony Cheng 	if (!pool->funcs->acquire_idle_pipe_for_layer)
12064562236bSHarry Wentland 		return NULL;
12074562236bSHarry Wentland 
1208b1f6d01cSDmytro Laktyushkin 	return pool->funcs->acquire_idle_pipe_for_layer(context, pool, head_pipe->stream);
12094562236bSHarry Wentland }
12104562236bSHarry Wentland 
1211b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
12120f9a536fSDmytro Laktyushkin static int acquire_first_split_pipe(
12130f9a536fSDmytro Laktyushkin 		struct resource_context *res_ctx,
12140f9a536fSDmytro Laktyushkin 		const struct resource_pool *pool,
12150971c40eSHarry Wentland 		struct dc_stream_state *stream)
12160f9a536fSDmytro Laktyushkin {
12170f9a536fSDmytro Laktyushkin 	int i;
12180f9a536fSDmytro Laktyushkin 
12190f9a536fSDmytro Laktyushkin 	for (i = 0; i < pool->pipe_count; i++) {
122079592db3SDmytro Laktyushkin 		struct pipe_ctx *split_pipe = &res_ctx->pipe_ctx[i];
12210f9a536fSDmytro Laktyushkin 
1222b1f6d01cSDmytro Laktyushkin 		if (split_pipe->top_pipe &&
122379592db3SDmytro Laktyushkin 				split_pipe->top_pipe->plane_state == split_pipe->plane_state) {
122479592db3SDmytro Laktyushkin 			split_pipe->top_pipe->bottom_pipe = split_pipe->bottom_pipe;
122579592db3SDmytro Laktyushkin 			if (split_pipe->bottom_pipe)
122679592db3SDmytro Laktyushkin 				split_pipe->bottom_pipe->top_pipe = split_pipe->top_pipe;
12270f9a536fSDmytro Laktyushkin 
122879592db3SDmytro Laktyushkin 			if (split_pipe->top_pipe->plane_state)
122979592db3SDmytro Laktyushkin 				resource_build_scaling_params(split_pipe->top_pipe);
12300f9a536fSDmytro Laktyushkin 
123179592db3SDmytro Laktyushkin 			memset(split_pipe, 0, sizeof(*split_pipe));
123279592db3SDmytro Laktyushkin 			split_pipe->stream_res.tg = pool->timing_generators[i];
123379592db3SDmytro Laktyushkin 			split_pipe->plane_res.hubp = pool->hubps[i];
123479592db3SDmytro Laktyushkin 			split_pipe->plane_res.ipp = pool->ipps[i];
123579592db3SDmytro Laktyushkin 			split_pipe->plane_res.dpp = pool->dpps[i];
123679592db3SDmytro Laktyushkin 			split_pipe->stream_res.opp = pool->opps[i];
123779592db3SDmytro Laktyushkin 			split_pipe->plane_res.mpcc_inst = pool->dpps[i]->inst;
123879592db3SDmytro Laktyushkin 			split_pipe->pipe_idx = i;
123979592db3SDmytro Laktyushkin 
124079592db3SDmytro Laktyushkin 			split_pipe->stream = stream;
12410f9a536fSDmytro Laktyushkin 			return i;
12420f9a536fSDmytro Laktyushkin 		}
12430f9a536fSDmytro Laktyushkin 	}
12440f9a536fSDmytro Laktyushkin 	return -1;
12450f9a536fSDmytro Laktyushkin }
12460f9a536fSDmytro Laktyushkin #endif
12470f9a536fSDmytro Laktyushkin 
124819f89e23SAndrey Grodzovsky bool dc_add_plane_to_context(
124919f89e23SAndrey Grodzovsky 		const struct dc *dc,
12500971c40eSHarry Wentland 		struct dc_stream_state *stream,
125119f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1252608ac7bbSJerry Zuo 		struct dc_state *context)
12534562236bSHarry Wentland {
12544562236bSHarry Wentland 	int i;
125519f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
125619f89e23SAndrey Grodzovsky 	struct pipe_ctx *head_pipe, *tail_pipe, *free_pipe;
1257ab2541b6SAric Cyr 	struct dc_stream_status *stream_status = NULL;
12584562236bSHarry Wentland 
1259ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++)
12604fa086b9SLeo (Sunpeng) Li 		if (context->streams[i] == stream) {
1261ab2541b6SAric Cyr 			stream_status = &context->stream_status[i];
12624562236bSHarry Wentland 			break;
12634562236bSHarry Wentland 		}
1264ab2541b6SAric Cyr 	if (stream_status == NULL) {
126519f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to attach surface!\n");
126619f89e23SAndrey Grodzovsky 		return false;
126719f89e23SAndrey Grodzovsky 	}
126819f89e23SAndrey Grodzovsky 
126919f89e23SAndrey Grodzovsky 
127019f89e23SAndrey Grodzovsky 	if (stream_status->plane_count == MAX_SURFACE_NUM) {
127119f89e23SAndrey Grodzovsky 		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
127219f89e23SAndrey Grodzovsky 				plane_state, MAX_SURFACE_NUM);
127319f89e23SAndrey Grodzovsky 		return false;
127419f89e23SAndrey Grodzovsky 	}
127519f89e23SAndrey Grodzovsky 
127619f89e23SAndrey Grodzovsky 	head_pipe = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
127719f89e23SAndrey Grodzovsky 
127819f89e23SAndrey Grodzovsky 	if (!head_pipe) {
127919f89e23SAndrey Grodzovsky 		dm_error("Head pipe not found for stream_state %p !\n", stream);
12804562236bSHarry Wentland 		return false;
12814562236bSHarry Wentland 	}
12824562236bSHarry Wentland 
1283b1f6d01cSDmytro Laktyushkin 	/* retain new surface, but only once per stream */
1284b1f6d01cSDmytro Laktyushkin 	dc_plane_state_retain(plane_state);
1285b1f6d01cSDmytro Laktyushkin 
1286b1f6d01cSDmytro Laktyushkin 	while (head_pipe) {
1287b1f6d01cSDmytro Laktyushkin 		tail_pipe = resource_get_tail_pipe(&context->res_ctx, head_pipe);
128800737c59SEric Bernstein 		ASSERT(tail_pipe);
128900737c59SEric Bernstein 
1290b1f6d01cSDmytro Laktyushkin 		free_pipe = acquire_free_pipe_for_head(context, pool, head_pipe);
12914562236bSHarry Wentland 
1292b86a1aa3SBhawanpreet Lakha 	#if defined(CONFIG_DRM_AMD_DC_DCN)
12930f9a536fSDmytro Laktyushkin 		if (!free_pipe) {
12940f9a536fSDmytro Laktyushkin 			int pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
12950f9a536fSDmytro Laktyushkin 			if (pipe_idx >= 0)
12960f9a536fSDmytro Laktyushkin 				free_pipe = &context->res_ctx.pipe_ctx[pipe_idx];
12970f9a536fSDmytro Laktyushkin 		}
12980f9a536fSDmytro Laktyushkin 	#endif
1299b1f6d01cSDmytro Laktyushkin 		if (!free_pipe) {
1300b1f6d01cSDmytro Laktyushkin 			dc_plane_state_release(plane_state);
13014562236bSHarry Wentland 			return false;
1302b1f6d01cSDmytro Laktyushkin 		}
13034562236bSHarry Wentland 
13043be5262eSHarry Wentland 		free_pipe->plane_state = plane_state;
13054562236bSHarry Wentland 
130619f89e23SAndrey Grodzovsky 		if (head_pipe != free_pipe) {
13076b670fa9SHarry Wentland 			free_pipe->stream_res.tg = tail_pipe->stream_res.tg;
13089aef1a31SSivapiriyanKumarasamy 			free_pipe->stream_res.abm = tail_pipe->stream_res.abm;
1309a6a6cb34SHarry Wentland 			free_pipe->stream_res.opp = tail_pipe->stream_res.opp;
13108e9c4c8cSHarry Wentland 			free_pipe->stream_res.stream_enc = tail_pipe->stream_res.stream_enc;
1311afaacef4SHarry Wentland 			free_pipe->stream_res.audio = tail_pipe->stream_res.audio;
1312cfe4645eSDmytro Laktyushkin 			free_pipe->clock_source = tail_pipe->clock_source;
13134562236bSHarry Wentland 			free_pipe->top_pipe = tail_pipe;
13144562236bSHarry Wentland 			tail_pipe->bottom_pipe = free_pipe;
13154562236bSHarry Wentland 		}
1316b1f6d01cSDmytro Laktyushkin 		head_pipe = head_pipe->next_odm_pipe;
1317b1f6d01cSDmytro Laktyushkin 	}
13184562236bSHarry Wentland 	/* assign new surfaces*/
131919f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = plane_state;
13204562236bSHarry Wentland 
132119f89e23SAndrey Grodzovsky 	stream_status->plane_count++;
13224562236bSHarry Wentland 
13234562236bSHarry Wentland 	return true;
13244562236bSHarry Wentland }
13254562236bSHarry Wentland 
132619f89e23SAndrey Grodzovsky bool dc_remove_plane_from_context(
132719f89e23SAndrey Grodzovsky 		const struct dc *dc,
132819f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
132919f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1330608ac7bbSJerry Zuo 		struct dc_state *context)
133119f89e23SAndrey Grodzovsky {
133219f89e23SAndrey Grodzovsky 	int i;
133319f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
133419f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
133519f89e23SAndrey Grodzovsky 
133619f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
133719f89e23SAndrey Grodzovsky 		if (context->streams[i] == stream) {
133819f89e23SAndrey Grodzovsky 			stream_status = &context->stream_status[i];
133919f89e23SAndrey Grodzovsky 			break;
134019f89e23SAndrey Grodzovsky 		}
134119f89e23SAndrey Grodzovsky 
134219f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
134319f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to remove plane.\n");
134419f89e23SAndrey Grodzovsky 		return false;
134519f89e23SAndrey Grodzovsky 	}
134619f89e23SAndrey Grodzovsky 
134719f89e23SAndrey Grodzovsky 	/* release pipe for plane*/
134819f89e23SAndrey Grodzovsky 	for (i = pool->pipe_count - 1; i >= 0; i--) {
13496ffaa6fcSDmytro Laktyushkin 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
135019f89e23SAndrey Grodzovsky 
13516ffaa6fcSDmytro Laktyushkin 		if (pipe_ctx->plane_state == plane_state) {
135219f89e23SAndrey Grodzovsky 			if (pipe_ctx->top_pipe)
135319f89e23SAndrey Grodzovsky 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
135419f89e23SAndrey Grodzovsky 
135519f89e23SAndrey Grodzovsky 			/* Second condition is to avoid setting NULL to top pipe
135619f89e23SAndrey Grodzovsky 			 * of tail pipe making it look like head pipe in subsequent
135719f89e23SAndrey Grodzovsky 			 * deletes
135819f89e23SAndrey Grodzovsky 			 */
135919f89e23SAndrey Grodzovsky 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
136019f89e23SAndrey Grodzovsky 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
136119f89e23SAndrey Grodzovsky 
136219f89e23SAndrey Grodzovsky 			/*
136319f89e23SAndrey Grodzovsky 			 * For head pipe detach surfaces from pipe for tail
136419f89e23SAndrey Grodzovsky 			 * pipe just zero it out
136519f89e23SAndrey Grodzovsky 			 */
1366b1f6d01cSDmytro Laktyushkin 			if (!pipe_ctx->top_pipe)
136719f89e23SAndrey Grodzovsky 				pipe_ctx->plane_state = NULL;
1368b1f6d01cSDmytro Laktyushkin 			else
136919f89e23SAndrey Grodzovsky 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
137019f89e23SAndrey Grodzovsky 		}
137119f89e23SAndrey Grodzovsky 	}
137219f89e23SAndrey Grodzovsky 
137319f89e23SAndrey Grodzovsky 
137419f89e23SAndrey Grodzovsky 	for (i = 0; i < stream_status->plane_count; i++) {
137519f89e23SAndrey Grodzovsky 		if (stream_status->plane_states[i] == plane_state) {
137619f89e23SAndrey Grodzovsky 
137719f89e23SAndrey Grodzovsky 			dc_plane_state_release(stream_status->plane_states[i]);
137819f89e23SAndrey Grodzovsky 			break;
137919f89e23SAndrey Grodzovsky 		}
138019f89e23SAndrey Grodzovsky 	}
138119f89e23SAndrey Grodzovsky 
138219f89e23SAndrey Grodzovsky 	if (i == stream_status->plane_count) {
138319f89e23SAndrey Grodzovsky 		dm_error("Existing plane_state not found; failed to detach it!\n");
138419f89e23SAndrey Grodzovsky 		return false;
138519f89e23SAndrey Grodzovsky 	}
138619f89e23SAndrey Grodzovsky 
138719f89e23SAndrey Grodzovsky 	stream_status->plane_count--;
138819f89e23SAndrey Grodzovsky 
1389abb4986eSAndrew Jiang 	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
1390abb4986eSAndrew Jiang 	for (; i < stream_status->plane_count; i++)
139119f89e23SAndrey Grodzovsky 		stream_status->plane_states[i] = stream_status->plane_states[i + 1];
139219f89e23SAndrey Grodzovsky 
139319f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = NULL;
139419f89e23SAndrey Grodzovsky 
139519f89e23SAndrey Grodzovsky 	return true;
139619f89e23SAndrey Grodzovsky }
139719f89e23SAndrey Grodzovsky 
139819f89e23SAndrey Grodzovsky bool dc_rem_all_planes_for_stream(
139919f89e23SAndrey Grodzovsky 		const struct dc *dc,
140019f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
1401608ac7bbSJerry Zuo 		struct dc_state *context)
140219f89e23SAndrey Grodzovsky {
140319f89e23SAndrey Grodzovsky 	int i, old_plane_count;
140419f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
140519f89e23SAndrey Grodzovsky 	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
140619f89e23SAndrey Grodzovsky 
140719f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
140819f89e23SAndrey Grodzovsky 			if (context->streams[i] == stream) {
140919f89e23SAndrey Grodzovsky 				stream_status = &context->stream_status[i];
141019f89e23SAndrey Grodzovsky 				break;
141119f89e23SAndrey Grodzovsky 			}
141219f89e23SAndrey Grodzovsky 
141319f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
141419f89e23SAndrey Grodzovsky 		dm_error("Existing stream %p not found!\n", stream);
141519f89e23SAndrey Grodzovsky 		return false;
141619f89e23SAndrey Grodzovsky 	}
141719f89e23SAndrey Grodzovsky 
141819f89e23SAndrey Grodzovsky 	old_plane_count = stream_status->plane_count;
141919f89e23SAndrey Grodzovsky 
142019f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
142119f89e23SAndrey Grodzovsky 		del_planes[i] = stream_status->plane_states[i];
142219f89e23SAndrey Grodzovsky 
142319f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
142419f89e23SAndrey Grodzovsky 		if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
142519f89e23SAndrey Grodzovsky 			return false;
142619f89e23SAndrey Grodzovsky 
142719f89e23SAndrey Grodzovsky 	return true;
142819f89e23SAndrey Grodzovsky }
142919f89e23SAndrey Grodzovsky 
143019f89e23SAndrey Grodzovsky static bool add_all_planes_for_stream(
143119f89e23SAndrey Grodzovsky 		const struct dc *dc,
143219f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
143319f89e23SAndrey Grodzovsky 		const struct dc_validation_set set[],
143419f89e23SAndrey Grodzovsky 		int set_count,
1435608ac7bbSJerry Zuo 		struct dc_state *context)
143619f89e23SAndrey Grodzovsky {
143719f89e23SAndrey Grodzovsky 	int i, j;
143819f89e23SAndrey Grodzovsky 
143919f89e23SAndrey Grodzovsky 	for (i = 0; i < set_count; i++)
144019f89e23SAndrey Grodzovsky 		if (set[i].stream == stream)
144119f89e23SAndrey Grodzovsky 			break;
144219f89e23SAndrey Grodzovsky 
144319f89e23SAndrey Grodzovsky 	if (i == set_count) {
144419f89e23SAndrey Grodzovsky 		dm_error("Stream %p not found in set!\n", stream);
144519f89e23SAndrey Grodzovsky 		return false;
144619f89e23SAndrey Grodzovsky 	}
144719f89e23SAndrey Grodzovsky 
144819f89e23SAndrey Grodzovsky 	for (j = 0; j < set[i].plane_count; j++)
144919f89e23SAndrey Grodzovsky 		if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
145019f89e23SAndrey Grodzovsky 			return false;
145119f89e23SAndrey Grodzovsky 
145219f89e23SAndrey Grodzovsky 	return true;
145319f89e23SAndrey Grodzovsky }
145419f89e23SAndrey Grodzovsky 
145519f89e23SAndrey Grodzovsky bool dc_add_all_planes_for_stream(
145619f89e23SAndrey Grodzovsky 		const struct dc *dc,
145719f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
145819f89e23SAndrey Grodzovsky 		struct dc_plane_state * const *plane_states,
145919f89e23SAndrey Grodzovsky 		int plane_count,
1460608ac7bbSJerry Zuo 		struct dc_state *context)
146119f89e23SAndrey Grodzovsky {
146219f89e23SAndrey Grodzovsky 	struct dc_validation_set set;
146319f89e23SAndrey Grodzovsky 	int i;
146419f89e23SAndrey Grodzovsky 
146519f89e23SAndrey Grodzovsky 	set.stream = stream;
146619f89e23SAndrey Grodzovsky 	set.plane_count = plane_count;
146719f89e23SAndrey Grodzovsky 
146819f89e23SAndrey Grodzovsky 	for (i = 0; i < plane_count; i++)
146919f89e23SAndrey Grodzovsky 		set.plane_states[i] = plane_states[i];
147019f89e23SAndrey Grodzovsky 
147119f89e23SAndrey Grodzovsky 	return add_all_planes_for_stream(dc, stream, &set, 1, context);
147219f89e23SAndrey Grodzovsky }
147319f89e23SAndrey Grodzovsky 
147419f89e23SAndrey Grodzovsky 
14756b622181SJulian Parkin static bool is_hdr_static_meta_changed(struct dc_stream_state *cur_stream,
14766b622181SJulian Parkin 	struct dc_stream_state *new_stream)
14776b622181SJulian Parkin {
14786b622181SJulian Parkin 	if (cur_stream == NULL)
14796b622181SJulian Parkin 		return true;
14806b622181SJulian Parkin 
14816b622181SJulian Parkin 	if (memcmp(&cur_stream->hdr_static_metadata,
14826b622181SJulian Parkin 			&new_stream->hdr_static_metadata,
14836b622181SJulian Parkin 			sizeof(struct dc_info_packet)) != 0)
14846b622181SJulian Parkin 		return true;
14856b622181SJulian Parkin 
14866b622181SJulian Parkin 	return false;
14876b622181SJulian Parkin }
14884562236bSHarry Wentland 
14891336926fSAlvin lee static bool is_vsc_info_packet_changed(struct dc_stream_state *cur_stream,
14901336926fSAlvin lee 		struct dc_stream_state *new_stream)
14911336926fSAlvin lee {
14921336926fSAlvin lee 	if (cur_stream == NULL)
14931336926fSAlvin lee 		return true;
14941336926fSAlvin lee 
14951336926fSAlvin lee 	if (memcmp(&cur_stream->vsc_infopacket,
14961336926fSAlvin lee 			&new_stream->vsc_infopacket,
14971336926fSAlvin lee 			sizeof(struct dc_info_packet)) != 0)
14981336926fSAlvin lee 		return true;
14991336926fSAlvin lee 
15001336926fSAlvin lee 	return false;
15011336926fSAlvin lee }
15021336926fSAlvin lee 
15030971c40eSHarry Wentland static bool is_timing_changed(struct dc_stream_state *cur_stream,
15040971c40eSHarry Wentland 		struct dc_stream_state *new_stream)
15054562236bSHarry Wentland {
15064562236bSHarry Wentland 	if (cur_stream == NULL)
15074562236bSHarry Wentland 		return true;
15084562236bSHarry Wentland 
15094562236bSHarry Wentland 	/* If sink pointer changed, it means this is a hotplug, we should do
15104562236bSHarry Wentland 	 * full hw setting.
15114562236bSHarry Wentland 	 */
15124562236bSHarry Wentland 	if (cur_stream->sink != new_stream->sink)
15134562236bSHarry Wentland 		return true;
15144562236bSHarry Wentland 
15154562236bSHarry Wentland 	/* If output color space is changed, need to reprogram info frames */
15164fa086b9SLeo (Sunpeng) Li 	if (cur_stream->output_color_space != new_stream->output_color_space)
15174562236bSHarry Wentland 		return true;
15184562236bSHarry Wentland 
15194562236bSHarry Wentland 	return memcmp(
15204fa086b9SLeo (Sunpeng) Li 		&cur_stream->timing,
15214fa086b9SLeo (Sunpeng) Li 		&new_stream->timing,
15224562236bSHarry Wentland 		sizeof(struct dc_crtc_timing)) != 0;
15234562236bSHarry Wentland }
15244562236bSHarry Wentland 
15254562236bSHarry Wentland static bool are_stream_backends_same(
15260971c40eSHarry Wentland 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
15274562236bSHarry Wentland {
15284562236bSHarry Wentland 	if (stream_a == stream_b)
15294562236bSHarry Wentland 		return true;
15304562236bSHarry Wentland 
15314562236bSHarry Wentland 	if (stream_a == NULL || stream_b == NULL)
15324562236bSHarry Wentland 		return false;
15334562236bSHarry Wentland 
15344562236bSHarry Wentland 	if (is_timing_changed(stream_a, stream_b))
15354562236bSHarry Wentland 		return false;
15364562236bSHarry Wentland 
15376b622181SJulian Parkin 	if (is_hdr_static_meta_changed(stream_a, stream_b))
15386b622181SJulian Parkin 		return false;
15396b622181SJulian Parkin 
15401e7e86c4SSamson Tam 	if (stream_a->dpms_off != stream_b->dpms_off)
15411e7e86c4SSamson Tam 		return false;
15421e7e86c4SSamson Tam 
15431336926fSAlvin lee 	if (is_vsc_info_packet_changed(stream_a, stream_b))
15441336926fSAlvin lee 		return false;
15451336926fSAlvin lee 
15464562236bSHarry Wentland 	return true;
15474562236bSHarry Wentland }
15484562236bSHarry Wentland 
15492119aa17SDavid Francis /**
15502119aa17SDavid Francis  * dc_is_stream_unchanged() - Compare two stream states for equivalence.
15512119aa17SDavid Francis  *
15522119aa17SDavid Francis  * Checks if there a difference between the two states
15532119aa17SDavid Francis  * that would require a mode change.
15542119aa17SDavid Francis  *
15552119aa17SDavid Francis  * Does not compare cursor position or attributes.
15562119aa17SDavid Francis  */
1557d54d29dbSBhawanpreet Lakha bool dc_is_stream_unchanged(
15580971c40eSHarry Wentland 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
15594562236bSHarry Wentland {
15604562236bSHarry Wentland 
15614562236bSHarry Wentland 	if (!are_stream_backends_same(old_stream, stream))
15624562236bSHarry Wentland 		return false;
15634562236bSHarry Wentland 
15640460f9abSJun Lei 	if (old_stream->ignore_msa_timing_param != stream->ignore_msa_timing_param)
15650460f9abSJun Lei 		return false;
15660460f9abSJun Lei 
15674562236bSHarry Wentland 	return true;
15684562236bSHarry Wentland }
15694562236bSHarry Wentland 
15702119aa17SDavid Francis /**
15712119aa17SDavid Francis  * dc_is_stream_scaling_unchanged() - Compare scaling rectangles of two streams.
15722119aa17SDavid Francis  */
15739a5d9c48SLeo (Sunpeng) Li bool dc_is_stream_scaling_unchanged(
15749a5d9c48SLeo (Sunpeng) Li 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
15759a5d9c48SLeo (Sunpeng) Li {
15769a5d9c48SLeo (Sunpeng) Li 	if (old_stream == stream)
15779a5d9c48SLeo (Sunpeng) Li 		return true;
15789a5d9c48SLeo (Sunpeng) Li 
15799a5d9c48SLeo (Sunpeng) Li 	if (old_stream == NULL || stream == NULL)
15809a5d9c48SLeo (Sunpeng) Li 		return false;
15819a5d9c48SLeo (Sunpeng) Li 
15829a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->src,
15839a5d9c48SLeo (Sunpeng) Li 			&stream->src,
15849a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
15859a5d9c48SLeo (Sunpeng) Li 		return false;
15869a5d9c48SLeo (Sunpeng) Li 
15879a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->dst,
15889a5d9c48SLeo (Sunpeng) Li 			&stream->dst,
15899a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
15909a5d9c48SLeo (Sunpeng) Li 		return false;
15919a5d9c48SLeo (Sunpeng) Li 
15929a5d9c48SLeo (Sunpeng) Li 	return true;
15939a5d9c48SLeo (Sunpeng) Li }
15949a5d9c48SLeo (Sunpeng) Li 
15951dc90497SAndrey Grodzovsky static void update_stream_engine_usage(
15964562236bSHarry Wentland 		struct resource_context *res_ctx,
1597a2b8659dSTony Cheng 		const struct resource_pool *pool,
15981dc90497SAndrey Grodzovsky 		struct stream_encoder *stream_enc,
15991dc90497SAndrey Grodzovsky 		bool acquired)
16004562236bSHarry Wentland {
16014562236bSHarry Wentland 	int i;
16024562236bSHarry Wentland 
1603a2b8659dSTony Cheng 	for (i = 0; i < pool->stream_enc_count; i++) {
1604a2b8659dSTony Cheng 		if (pool->stream_enc[i] == stream_enc)
16051dc90497SAndrey Grodzovsky 			res_ctx->is_stream_enc_acquired[i] = acquired;
16064562236bSHarry Wentland 	}
16074562236bSHarry Wentland }
16084562236bSHarry Wentland 
16094562236bSHarry Wentland /* TODO: release audio object */
16104176664bSCharlene Liu void update_audio_usage(
16114562236bSHarry Wentland 		struct resource_context *res_ctx,
1612a2b8659dSTony Cheng 		const struct resource_pool *pool,
16131dc90497SAndrey Grodzovsky 		struct audio *audio,
16141dc90497SAndrey Grodzovsky 		bool acquired)
16154562236bSHarry Wentland {
16164562236bSHarry Wentland 	int i;
1617a2b8659dSTony Cheng 	for (i = 0; i < pool->audio_count; i++) {
1618a2b8659dSTony Cheng 		if (pool->audios[i] == audio)
16191dc90497SAndrey Grodzovsky 			res_ctx->is_audio_acquired[i] = acquired;
16204562236bSHarry Wentland 	}
16214562236bSHarry Wentland }
16224562236bSHarry Wentland 
16234562236bSHarry Wentland static int acquire_first_free_pipe(
16244562236bSHarry Wentland 		struct resource_context *res_ctx,
1625a2b8659dSTony Cheng 		const struct resource_pool *pool,
16260971c40eSHarry Wentland 		struct dc_stream_state *stream)
16274562236bSHarry Wentland {
16284562236bSHarry Wentland 	int i;
16294562236bSHarry Wentland 
1630a2b8659dSTony Cheng 	for (i = 0; i < pool->pipe_count; i++) {
16314562236bSHarry Wentland 		if (!res_ctx->pipe_ctx[i].stream) {
16324562236bSHarry Wentland 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
16334562236bSHarry Wentland 
16346b670fa9SHarry Wentland 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
163586a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi = pool->mis[i];
16368feabd03SYue Hin Lau 			pipe_ctx->plane_res.hubp = pool->hubps[i];
163786a66c4eSHarry Wentland 			pipe_ctx->plane_res.ipp = pool->ipps[i];
163886a66c4eSHarry Wentland 			pipe_ctx->plane_res.xfm = pool->transforms[i];
1639d94585a0SYue Hin Lau 			pipe_ctx->plane_res.dpp = pool->dpps[i];
1640a6a6cb34SHarry Wentland 			pipe_ctx->stream_res.opp = pool->opps[i];
1641bc373a89SRoman Li 			if (pool->dpps[i])
1642e07f541fSYongqiang Sun 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
16434562236bSHarry Wentland 			pipe_ctx->pipe_idx = i;
16444562236bSHarry Wentland 
1645ff5ef992SAlex Deucher 
16464562236bSHarry Wentland 			pipe_ctx->stream = stream;
16474562236bSHarry Wentland 			return i;
16484562236bSHarry Wentland 		}
16494562236bSHarry Wentland 	}
16504562236bSHarry Wentland 	return -1;
16514562236bSHarry Wentland }
16524562236bSHarry Wentland 
1653a2b8659dSTony Cheng static struct audio *find_first_free_audio(
1654a2b8659dSTony Cheng 		struct resource_context *res_ctx,
1655cfb071f7SCharlene Liu 		const struct resource_pool *pool,
1656f24b0522SPaul Hsieh 		enum engine_id id,
1657f24b0522SPaul Hsieh 		enum dce_version dc_version)
16584562236bSHarry Wentland {
1659b5a41620SCharlene Liu 	int i, available_audio_count;
1660b5a41620SCharlene Liu 
1661b5a41620SCharlene Liu 	available_audio_count = pool->audio_count;
1662b5a41620SCharlene Liu 
1663b5a41620SCharlene Liu 	for (i = 0; i < available_audio_count; i++) {
16644176664bSCharlene Liu 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
1665cfb071f7SCharlene Liu 			/*we have enough audio endpoint, find the matching inst*/
1666cfb071f7SCharlene Liu 			if (id != i)
1667cfb071f7SCharlene Liu 				continue;
1668a2b8659dSTony Cheng 			return pool->audios[i];
16694562236bSHarry Wentland 		}
16704562236bSHarry Wentland 	}
16715feb9f07STai Man 
16725feb9f07STai Man 	/* use engine id to find free audio */
1673b5a41620SCharlene Liu 	if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
16745feb9f07STai Man 		return pool->audios[id];
16755feb9f07STai Man 	}
167666bfd4fdSCharlene Liu 	/*not found the matching one, first come first serve*/
1677b5a41620SCharlene Liu 	for (i = 0; i < available_audio_count; i++) {
16784176664bSCharlene Liu 		if (res_ctx->is_audio_acquired[i] == false) {
16794176664bSCharlene Liu 			return pool->audios[i];
16804176664bSCharlene Liu 		}
16814176664bSCharlene Liu 	}
16824562236bSHarry Wentland 	return 0;
16834562236bSHarry Wentland }
16844562236bSHarry Wentland 
16854562236bSHarry Wentland bool resource_is_stream_unchanged(
1686608ac7bbSJerry Zuo 	struct dc_state *old_context, struct dc_stream_state *stream)
16874562236bSHarry Wentland {
1688ab2541b6SAric Cyr 	int i;
16894562236bSHarry Wentland 
1690ab2541b6SAric Cyr 	for (i = 0; i < old_context->stream_count; i++) {
16910971c40eSHarry Wentland 		struct dc_stream_state *old_stream = old_context->streams[i];
16924562236bSHarry Wentland 
16934562236bSHarry Wentland 		if (are_stream_backends_same(old_stream, stream))
16944562236bSHarry Wentland 				return true;
16954562236bSHarry Wentland 	}
16964562236bSHarry Wentland 
16974562236bSHarry Wentland 	return false;
16984562236bSHarry Wentland }
16994562236bSHarry Wentland 
17002119aa17SDavid Francis /**
17012119aa17SDavid Francis  * dc_add_stream_to_ctx() - Add a new dc_stream_state to a dc_state.
17022119aa17SDavid Francis  */
170313ab1b44SYongqiang Sun enum dc_status dc_add_stream_to_ctx(
17041dc90497SAndrey Grodzovsky 		struct dc *dc,
1705608ac7bbSJerry Zuo 		struct dc_state *new_ctx,
17061dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
17071dc90497SAndrey Grodzovsky {
17081dc90497SAndrey Grodzovsky 	enum dc_status res;
1709eb9714a2SWenjing Liu 	DC_LOGGER_INIT(dc->ctx->logger);
17101dc90497SAndrey Grodzovsky 
1711ece4147fSKen Chalmers 	if (new_ctx->stream_count >= dc->res_pool->timing_generator_count) {
1712eb9714a2SWenjing Liu 		DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
17131dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
17141dc90497SAndrey Grodzovsky 	}
17151dc90497SAndrey Grodzovsky 
17161dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = stream;
17171dc90497SAndrey Grodzovsky 	dc_stream_retain(stream);
17181dc90497SAndrey Grodzovsky 	new_ctx->stream_count++;
17191dc90497SAndrey Grodzovsky 
17201dc90497SAndrey Grodzovsky 	res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
17211dc90497SAndrey Grodzovsky 	if (res != DC_OK)
1722eb9714a2SWenjing Liu 		DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);
17231dc90497SAndrey Grodzovsky 
172413ab1b44SYongqiang Sun 	return res;
17251dc90497SAndrey Grodzovsky }
17261dc90497SAndrey Grodzovsky 
17272119aa17SDavid Francis /**
17282119aa17SDavid Francis  * dc_remove_stream_from_ctx() - Remove a stream from a dc_state.
17292119aa17SDavid Francis  */
173062c933f9SYongqiang Sun enum dc_status dc_remove_stream_from_ctx(
17311dc90497SAndrey Grodzovsky 			struct dc *dc,
1732608ac7bbSJerry Zuo 			struct dc_state *new_ctx,
17331dc90497SAndrey Grodzovsky 			struct dc_stream_state *stream)
17341dc90497SAndrey Grodzovsky {
173519f89e23SAndrey Grodzovsky 	int i;
17361dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
173722498036SDmytro Laktyushkin 	struct pipe_ctx *del_pipe = resource_get_head_pipe_for_stream(&new_ctx->res_ctx, stream);
173822498036SDmytro Laktyushkin 	struct pipe_ctx *odm_pipe;
173922498036SDmytro Laktyushkin 
174022498036SDmytro Laktyushkin 	if (!del_pipe) {
174122498036SDmytro Laktyushkin 		DC_ERROR("Pipe not found for stream %p !\n", stream);
174222498036SDmytro Laktyushkin 		return DC_ERROR_UNEXPECTED;
174322498036SDmytro Laktyushkin 	}
174422498036SDmytro Laktyushkin 
174522498036SDmytro Laktyushkin 	odm_pipe = del_pipe->next_odm_pipe;
17461dc90497SAndrey Grodzovsky 
174719f89e23SAndrey Grodzovsky 	/* Release primary pipe */
174819f89e23SAndrey Grodzovsky 	ASSERT(del_pipe->stream_res.stream_enc);
17491dc90497SAndrey Grodzovsky 	update_stream_engine_usage(
17501dc90497SAndrey Grodzovsky 			&new_ctx->res_ctx,
17511dc90497SAndrey Grodzovsky 				dc->res_pool,
17521dc90497SAndrey Grodzovsky 			del_pipe->stream_res.stream_enc,
17531dc90497SAndrey Grodzovsky 			false);
17541dc90497SAndrey Grodzovsky 
17551dc90497SAndrey Grodzovsky 	if (del_pipe->stream_res.audio)
17561dc90497SAndrey Grodzovsky 		update_audio_usage(
17571dc90497SAndrey Grodzovsky 			&new_ctx->res_ctx,
17581dc90497SAndrey Grodzovsky 			dc->res_pool,
17591dc90497SAndrey Grodzovsky 			del_pipe->stream_res.audio,
17601dc90497SAndrey Grodzovsky 			false);
17611dc90497SAndrey Grodzovsky 
17629d0dcecdSHarry Wentland 	resource_unreference_clock_source(&new_ctx->res_ctx,
17639d0dcecdSHarry Wentland 					  dc->res_pool,
17649d0dcecdSHarry Wentland 					  del_pipe->clock_source);
17659d0dcecdSHarry Wentland 
1766e56ae556SNikola Cornij 	if (dc->res_pool->funcs->remove_stream_from_ctx)
1767e56ae556SNikola Cornij 		dc->res_pool->funcs->remove_stream_from_ctx(dc, new_ctx, stream);
1768e56ae556SNikola Cornij 
176922498036SDmytro Laktyushkin 	while (odm_pipe) {
177022498036SDmytro Laktyushkin 		struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
177122498036SDmytro Laktyushkin 
177222498036SDmytro Laktyushkin 		memset(odm_pipe, 0, sizeof(*odm_pipe));
177322498036SDmytro Laktyushkin 		odm_pipe = next_odm_pipe;
177422498036SDmytro Laktyushkin 	}
17751dc90497SAndrey Grodzovsky 	memset(del_pipe, 0, sizeof(*del_pipe));
17766ffaa6fcSDmytro Laktyushkin 
17771dc90497SAndrey Grodzovsky 	for (i = 0; i < new_ctx->stream_count; i++)
17781dc90497SAndrey Grodzovsky 		if (new_ctx->streams[i] == stream)
17791dc90497SAndrey Grodzovsky 			break;
17801dc90497SAndrey Grodzovsky 
17811dc90497SAndrey Grodzovsky 	if (new_ctx->streams[i] != stream) {
17821dc90497SAndrey Grodzovsky 		DC_ERROR("Context doesn't have stream %p !\n", stream);
17831dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
17841dc90497SAndrey Grodzovsky 	}
17851dc90497SAndrey Grodzovsky 
17861dc90497SAndrey Grodzovsky 	dc_stream_release(new_ctx->streams[i]);
17871dc90497SAndrey Grodzovsky 	new_ctx->stream_count--;
17881dc90497SAndrey Grodzovsky 
17891dc90497SAndrey Grodzovsky 	/* Trim back arrays */
17901dc90497SAndrey Grodzovsky 	for (; i < new_ctx->stream_count; i++) {
17911dc90497SAndrey Grodzovsky 		new_ctx->streams[i] = new_ctx->streams[i + 1];
17921dc90497SAndrey Grodzovsky 		new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
17931dc90497SAndrey Grodzovsky 	}
17941dc90497SAndrey Grodzovsky 
17951dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = NULL;
17961dc90497SAndrey Grodzovsky 	memset(
17971dc90497SAndrey Grodzovsky 			&new_ctx->stream_status[new_ctx->stream_count],
17981dc90497SAndrey Grodzovsky 			0,
17991dc90497SAndrey Grodzovsky 			sizeof(new_ctx->stream_status[0]));
18001dc90497SAndrey Grodzovsky 
18011dc90497SAndrey Grodzovsky 	return DC_OK;
18021dc90497SAndrey Grodzovsky }
18031dc90497SAndrey Grodzovsky 
18040971c40eSHarry Wentland static struct dc_stream_state *find_pll_sharable_stream(
18050971c40eSHarry Wentland 		struct dc_stream_state *stream_needs_pll,
1806608ac7bbSJerry Zuo 		struct dc_state *context)
18074562236bSHarry Wentland {
1808ab2541b6SAric Cyr 	int i;
18094562236bSHarry Wentland 
1810ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
18110971c40eSHarry Wentland 		struct dc_stream_state *stream_has_pll = context->streams[i];
18124562236bSHarry Wentland 
18134562236bSHarry Wentland 		/* We are looking for non dp, non virtual stream */
18144562236bSHarry Wentland 		if (resource_are_streams_timing_synchronizable(
18154562236bSHarry Wentland 			stream_needs_pll, stream_has_pll)
18164562236bSHarry Wentland 			&& !dc_is_dp_signal(stream_has_pll->signal)
1817ceb3dbb4SJun Lei 			&& stream_has_pll->link->connector_signal
18184562236bSHarry Wentland 			!= SIGNAL_TYPE_VIRTUAL)
18194562236bSHarry Wentland 			return stream_has_pll;
1820ab2541b6SAric Cyr 
18214562236bSHarry Wentland 	}
18224562236bSHarry Wentland 
18234562236bSHarry Wentland 	return NULL;
18244562236bSHarry Wentland }
18254562236bSHarry Wentland 
18264562236bSHarry Wentland static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
18274562236bSHarry Wentland {
1828380604e2SKen Chalmers 	uint32_t pix_clk = timing->pix_clk_100hz;
18294562236bSHarry Wentland 	uint32_t normalized_pix_clk = pix_clk;
18304562236bSHarry Wentland 
18314562236bSHarry Wentland 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
18324562236bSHarry Wentland 		pix_clk /= 2;
1833cc4d99b8SCharlene Liu 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
18344562236bSHarry Wentland 		switch (timing->display_color_depth) {
18358897810aSJulian Parkin 		case COLOR_DEPTH_666:
18364562236bSHarry Wentland 		case COLOR_DEPTH_888:
18374562236bSHarry Wentland 			normalized_pix_clk = pix_clk;
18384562236bSHarry Wentland 			break;
18394562236bSHarry Wentland 		case COLOR_DEPTH_101010:
18404562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 30) / 24;
18414562236bSHarry Wentland 			break;
18424562236bSHarry Wentland 		case COLOR_DEPTH_121212:
18434562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 36) / 24;
18444562236bSHarry Wentland 		break;
18454562236bSHarry Wentland 		case COLOR_DEPTH_161616:
18464562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 48) / 24;
18474562236bSHarry Wentland 		break;
18484562236bSHarry Wentland 		default:
18494562236bSHarry Wentland 			ASSERT(0);
18504562236bSHarry Wentland 		break;
18514562236bSHarry Wentland 		}
1852cc4d99b8SCharlene Liu 	}
18534562236bSHarry Wentland 	return normalized_pix_clk;
18544562236bSHarry Wentland }
18554562236bSHarry Wentland 
18560971c40eSHarry Wentland static void calculate_phy_pix_clks(struct dc_stream_state *stream)
18574562236bSHarry Wentland {
18584562236bSHarry Wentland 	/* update actual pixel clock on all streams */
18594562236bSHarry Wentland 	if (dc_is_hdmi_signal(stream->signal))
18604562236bSHarry Wentland 		stream->phy_pix_clk = get_norm_pix_clk(
1861380604e2SKen Chalmers 			&stream->timing) / 10;
18624562236bSHarry Wentland 	else
18634562236bSHarry Wentland 		stream->phy_pix_clk =
1864380604e2SKen Chalmers 			stream->timing.pix_clk_100hz / 10;
186539c03e00SCharlene Liu 
186639c03e00SCharlene Liu 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
186739c03e00SCharlene Liu 		stream->phy_pix_clk *= 2;
18684562236bSHarry Wentland }
18694562236bSHarry Wentland 
1870d2d7885fSAnthony Koo static int acquire_resource_from_hw_enabled_state(
1871d2d7885fSAnthony Koo 		struct resource_context *res_ctx,
1872d2d7885fSAnthony Koo 		const struct resource_pool *pool,
1873d2d7885fSAnthony Koo 		struct dc_stream_state *stream)
1874d2d7885fSAnthony Koo {
1875d2d7885fSAnthony Koo 	struct dc_link *link = stream->link;
187608b66279SMartin Leung 	unsigned int i, inst, tg_inst = 0;
1877d2d7885fSAnthony Koo 
1878d2d7885fSAnthony Koo 	/* Check for enabled DIG to identify enabled display */
1879d2d7885fSAnthony Koo 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1880d2d7885fSAnthony Koo 		return -1;
1881d2d7885fSAnthony Koo 
18825ec43edaSMartin Leung 	inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1883d2d7885fSAnthony Koo 
18847f7652eeSMartin Leung 	if (inst == ENGINE_ID_UNKNOWN)
188575441d9dSMikita Lipski 		return -1;
1886d2d7885fSAnthony Koo 
18877f7652eeSMartin Leung 	for (i = 0; i < pool->stream_enc_count; i++) {
18887f7652eeSMartin Leung 		if (pool->stream_enc[i]->id == inst) {
18897f7652eeSMartin Leung 			tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
18907f7652eeSMartin Leung 				pool->stream_enc[i]);
18917f7652eeSMartin Leung 			break;
18927f7652eeSMartin Leung 		}
18937f7652eeSMartin Leung 	}
1894d2d7885fSAnthony Koo 
18957f7652eeSMartin Leung 	// tg_inst not found
18967f7652eeSMartin Leung 	if (i == pool->stream_enc_count)
189775441d9dSMikita Lipski 		return -1;
18985ec43edaSMartin Leung 
18995ec43edaSMartin Leung 	if (tg_inst >= pool->timing_generator_count)
190075441d9dSMikita Lipski 		return -1;
19015ec43edaSMartin Leung 
19025ec43edaSMartin Leung 	if (!res_ctx->pipe_ctx[tg_inst].stream) {
19035ec43edaSMartin Leung 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
19045ec43edaSMartin Leung 
19055ec43edaSMartin Leung 		pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
19065ec43edaSMartin Leung 		pipe_ctx->plane_res.mi = pool->mis[tg_inst];
19075ec43edaSMartin Leung 		pipe_ctx->plane_res.hubp = pool->hubps[tg_inst];
19085ec43edaSMartin Leung 		pipe_ctx->plane_res.ipp = pool->ipps[tg_inst];
19095ec43edaSMartin Leung 		pipe_ctx->plane_res.xfm = pool->transforms[tg_inst];
19105ec43edaSMartin Leung 		pipe_ctx->plane_res.dpp = pool->dpps[tg_inst];
19115ec43edaSMartin Leung 		pipe_ctx->stream_res.opp = pool->opps[tg_inst];
19125ec43edaSMartin Leung 
19135ec43edaSMartin Leung 		if (pool->dpps[tg_inst])
19145ec43edaSMartin Leung 			pipe_ctx->plane_res.mpcc_inst = pool->dpps[tg_inst]->inst;
19155ec43edaSMartin Leung 		pipe_ctx->pipe_idx = tg_inst;
1916d2d7885fSAnthony Koo 
1917d2d7885fSAnthony Koo 		pipe_ctx->stream = stream;
19185ec43edaSMartin Leung 		return tg_inst;
1919d2d7885fSAnthony Koo 	}
1920d2d7885fSAnthony Koo 
1921d2d7885fSAnthony Koo 	return -1;
1922d2d7885fSAnthony Koo }
1923d2d7885fSAnthony Koo 
19244562236bSHarry Wentland enum dc_status resource_map_pool_resources(
1925fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
1926608ac7bbSJerry Zuo 		struct dc_state *context,
19271dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
19284562236bSHarry Wentland {
1929a2b8659dSTony Cheng 	const struct resource_pool *pool = dc->res_pool;
19301dc90497SAndrey Grodzovsky 	int i;
19311dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
19321dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = NULL;
19331dc90497SAndrey Grodzovsky 	int pipe_idx = -1;
193446570f09SAnthony Koo 	struct dc_bios *dcb = dc->ctx->dc_bios;
19354562236bSHarry Wentland 
19361dc90497SAndrey Grodzovsky 	/* TODO Check if this is needed */
19371dc90497SAndrey Grodzovsky 	/*if (!resource_is_stream_unchanged(old_context, stream)) {
1938430ef426SDmytro Laktyushkin 			if (stream != NULL && old_context->streams[i] != NULL) {
19394b679bc3SCharlene Liu 				stream->bit_depth_params =
1940430ef426SDmytro Laktyushkin 						old_context->streams[i]->bit_depth_params;
1941430ef426SDmytro Laktyushkin 				stream->clamping = old_context->streams[i]->clamping;
19424562236bSHarry Wentland 				continue;
19434b679bc3SCharlene Liu 			}
19444b679bc3SCharlene Liu 		}
19454562236bSHarry Wentland 	*/
19464562236bSHarry Wentland 
194708e1c28dSYogesh Mohan Marimuthu 	calculate_phy_pix_clks(stream);
194808e1c28dSYogesh Mohan Marimuthu 
194946570f09SAnthony Koo 	/* TODO: Check Linux */
195046570f09SAnthony Koo 	if (dc->config.allow_seamless_boot_optimization &&
195146570f09SAnthony Koo 			!dcb->funcs->is_accelerated_mode(dcb)) {
195246570f09SAnthony Koo 		if (dc_validate_seamless_boot_timing(dc, stream->sink, &stream->timing))
195346570f09SAnthony Koo 			stream->apply_seamless_boot_optimization = true;
195446570f09SAnthony Koo 	}
195546570f09SAnthony Koo 
1956d2d7885fSAnthony Koo 	if (stream->apply_seamless_boot_optimization)
1957d2d7885fSAnthony Koo 		pipe_idx = acquire_resource_from_hw_enabled_state(
1958d2d7885fSAnthony Koo 				&context->res_ctx,
1959d2d7885fSAnthony Koo 				pool,
1960d2d7885fSAnthony Koo 				stream);
1961d2d7885fSAnthony Koo 
1962d2d7885fSAnthony Koo 	if (pipe_idx < 0)
19634562236bSHarry Wentland 		/* acquire new resources */
19645d11e9fcSDmytro Laktyushkin 		pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
19651dc90497SAndrey Grodzovsky 
1966b86a1aa3SBhawanpreet Lakha #ifdef CONFIG_DRM_AMD_DC_DCN
19675d11e9fcSDmytro Laktyushkin 	if (pipe_idx < 0)
196813ab1b44SYongqiang Sun 		pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
196994c6d735SHarry Wentland #endif
197013ab1b44SYongqiang Sun 
1971c5b38aecSDmytro Laktyushkin 	if (pipe_idx < 0 || context->res_ctx.pipe_ctx[pipe_idx].stream_res.tg == NULL)
19724562236bSHarry Wentland 		return DC_NO_CONTROLLER_RESOURCE;
19734562236bSHarry Wentland 
19744562236bSHarry Wentland 	pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
19754562236bSHarry Wentland 
19768e9c4c8cSHarry Wentland 	pipe_ctx->stream_res.stream_enc =
197778cc70b1SWesley Chalmers 		dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
1978a2b8659dSTony Cheng 			&context->res_ctx, pool, stream);
19794562236bSHarry Wentland 
19808e9c4c8cSHarry Wentland 	if (!pipe_ctx->stream_res.stream_enc)
198138684e46SEric Bernstein 		return DC_NO_STREAM_ENC_RESOURCE;
19824562236bSHarry Wentland 
19831dc90497SAndrey Grodzovsky 	update_stream_engine_usage(
1984a2b8659dSTony Cheng 		&context->res_ctx, pool,
19851dc90497SAndrey Grodzovsky 		pipe_ctx->stream_res.stream_enc,
19861dc90497SAndrey Grodzovsky 		true);
19874562236bSHarry Wentland 
19884562236bSHarry Wentland 	/* TODO: Add check if ASIC support and EDID audio */
1989ceb3dbb4SJun Lei 	if (!stream->converter_disable_audio &&
19904562236bSHarry Wentland 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
1991ce08aad3SAlvin Lee 	    stream->audio_info.mode_count && stream->audio_info.flags.all) {
1992afaacef4SHarry Wentland 		pipe_ctx->stream_res.audio = find_first_free_audio(
1993f24b0522SPaul Hsieh 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
19944562236bSHarry Wentland 
19954562236bSHarry Wentland 		/*
19964562236bSHarry Wentland 		 * Audio assigned in order first come first get.
19974562236bSHarry Wentland 		 * There are asics which has number of audio
19984562236bSHarry Wentland 		 * resources less then number of pipes
19994562236bSHarry Wentland 		 */
2000afaacef4SHarry Wentland 		if (pipe_ctx->stream_res.audio)
20011dc90497SAndrey Grodzovsky 			update_audio_usage(&context->res_ctx, pool,
20021dc90497SAndrey Grodzovsky 					   pipe_ctx->stream_res.audio, true);
20034562236bSHarry Wentland 	}
20044562236bSHarry Wentland 
20059aef1a31SSivapiriyanKumarasamy 	/* Add ABM to the resource if on EDP */
20069aef1a31SSivapiriyanKumarasamy 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal))
20079aef1a31SSivapiriyanKumarasamy 		pipe_ctx->stream_res.abm = pool->abm;
20089aef1a31SSivapiriyanKumarasamy 
20091dc90497SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
20101dc90497SAndrey Grodzovsky 		if (context->streams[i] == stream) {
20116b670fa9SHarry Wentland 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
20120f0bdca5SWenjing Liu 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->id;
20135fdb7c4cSNicholas Kazlauskas 			context->stream_status[i].audio_inst =
20145fdb7c4cSNicholas Kazlauskas 				pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
20155fdb7c4cSNicholas Kazlauskas 
20161dc90497SAndrey Grodzovsky 			return DC_OK;
20174562236bSHarry Wentland 		}
20184562236bSHarry Wentland 
20191dc90497SAndrey Grodzovsky 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
20201dc90497SAndrey Grodzovsky 	return DC_ERROR_UNEXPECTED;
20214562236bSHarry Wentland }
20224562236bSHarry Wentland 
20232119aa17SDavid Francis /**
20242119aa17SDavid Francis  * dc_resource_state_copy_construct_current() - Creates a new dc_state from existing state
20252119aa17SDavid Francis  * Is a shallow copy.  Increments refcounts on existing streams and planes.
20262119aa17SDavid Francis  * @dc: copy out of dc->current_state
20272119aa17SDavid Francis  * @dst_ctx: copy into this
20282119aa17SDavid Francis  */
2029f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct_current(
20301dc90497SAndrey Grodzovsky 		const struct dc *dc,
2031608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
20321dc90497SAndrey Grodzovsky {
2033f36cc577SBhawanpreet Lakha 	dc_resource_state_copy_construct(dc->current_state, dst_ctx);
20341dc90497SAndrey Grodzovsky }
20351dc90497SAndrey Grodzovsky 
2036ab8db3e1SAndrey Grodzovsky 
2037ab8db3e1SAndrey Grodzovsky void dc_resource_state_construct(
2038ab8db3e1SAndrey Grodzovsky 		const struct dc *dc,
2039ab8db3e1SAndrey Grodzovsky 		struct dc_state *dst_ctx)
2040ab8db3e1SAndrey Grodzovsky {
2041dc88b4a6SEric Yang 	dst_ctx->clk_mgr = dc->clk_mgr;
2042ab8db3e1SAndrey Grodzovsky }
2043ab8db3e1SAndrey Grodzovsky 
20442119aa17SDavid Francis /**
20452119aa17SDavid Francis  * dc_validate_global_state() - Determine if HW can support a given state
20462119aa17SDavid Francis  * Checks HW resource availability and bandwidth requirement.
20472119aa17SDavid Francis  * @dc: dc struct for this driver
20482119aa17SDavid Francis  * @new_ctx: state to be validated
2049afcd526bSJoshua Aberback  * @fast_validate: set to true if only yes/no to support matters
20502119aa17SDavid Francis  *
20512119aa17SDavid Francis  * Return: DC_OK if the result can be programmed.  Otherwise, an error code.
20522119aa17SDavid Francis  */
2053e750d56dSYongqiang Sun enum dc_status dc_validate_global_state(
20541dc90497SAndrey Grodzovsky 		struct dc *dc,
2055afcd526bSJoshua Aberback 		struct dc_state *new_ctx,
2056afcd526bSJoshua Aberback 		bool fast_validate)
20571dc90497SAndrey Grodzovsky {
20581dc90497SAndrey Grodzovsky 	enum dc_status result = DC_ERROR_UNEXPECTED;
20591dc90497SAndrey Grodzovsky 	int i, j;
20601dc90497SAndrey Grodzovsky 
2061e41ab030SHarry Wentland 	if (!new_ctx)
2062e41ab030SHarry Wentland 		return DC_ERROR_UNEXPECTED;
2063e41ab030SHarry Wentland 
2064d596e5d0SYongqiang Sun 	if (dc->res_pool->funcs->validate_global) {
2065d596e5d0SYongqiang Sun 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
2066d596e5d0SYongqiang Sun 		if (result != DC_OK)
2067d596e5d0SYongqiang Sun 			return result;
2068d596e5d0SYongqiang Sun 	}
20691dc90497SAndrey Grodzovsky 
2070e41ab030SHarry Wentland 	for (i = 0; i < new_ctx->stream_count; i++) {
20711dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream = new_ctx->streams[i];
20721dc90497SAndrey Grodzovsky 
20731dc90497SAndrey Grodzovsky 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
20741dc90497SAndrey Grodzovsky 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
20751dc90497SAndrey Grodzovsky 
20761dc90497SAndrey Grodzovsky 			if (pipe_ctx->stream != stream)
20771dc90497SAndrey Grodzovsky 				continue;
20781dc90497SAndrey Grodzovsky 
207974eac5f3SSu Sung Chung 			if (dc->res_pool->funcs->get_default_swizzle_mode &&
208074eac5f3SSu Sung Chung 					pipe_ctx->plane_state &&
208174eac5f3SSu Sung Chung 					pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
208274eac5f3SSu Sung Chung 				result = dc->res_pool->funcs->get_default_swizzle_mode(pipe_ctx->plane_state);
208374eac5f3SSu Sung Chung 				if (result != DC_OK)
208474eac5f3SSu Sung Chung 					return result;
208574eac5f3SSu Sung Chung 			}
208674eac5f3SSu Sung Chung 
20871dc90497SAndrey Grodzovsky 			/* Switch to dp clock source only if there is
20881dc90497SAndrey Grodzovsky 			 * no non dp stream that shares the same timing
20891dc90497SAndrey Grodzovsky 			 * with the dp stream.
20901dc90497SAndrey Grodzovsky 			 */
20911dc90497SAndrey Grodzovsky 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
20921dc90497SAndrey Grodzovsky 				!find_pll_sharable_stream(stream, new_ctx)) {
20931dc90497SAndrey Grodzovsky 
20949d0dcecdSHarry Wentland 				resource_unreference_clock_source(
20951dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
20961dc90497SAndrey Grodzovsky 						dc->res_pool,
20979d0dcecdSHarry Wentland 						pipe_ctx->clock_source);
20984a629536SHarry Wentland 
20991dc90497SAndrey Grodzovsky 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
21001dc90497SAndrey Grodzovsky 				resource_reference_clock_source(
21011dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
21021dc90497SAndrey Grodzovsky 						dc->res_pool,
21031dc90497SAndrey Grodzovsky 						 pipe_ctx->clock_source);
21041dc90497SAndrey Grodzovsky 			}
21051dc90497SAndrey Grodzovsky 		}
21061dc90497SAndrey Grodzovsky 	}
21071dc90497SAndrey Grodzovsky 
21081dc90497SAndrey Grodzovsky 	result = resource_build_scaling_params_for_context(dc, new_ctx);
21091dc90497SAndrey Grodzovsky 
21101dc90497SAndrey Grodzovsky 	if (result == DC_OK)
2111afcd526bSJoshua Aberback 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, fast_validate))
21121dc90497SAndrey Grodzovsky 			result = DC_FAIL_BANDWIDTH_VALIDATE;
21131dc90497SAndrey Grodzovsky 
21141dc90497SAndrey Grodzovsky 	return result;
21151dc90497SAndrey Grodzovsky }
21161dc90497SAndrey Grodzovsky 
21176e4d6beeSTony Cheng static void patch_gamut_packet_checksum(
2118e09b6473SAnthony Koo 		struct dc_info_packet *gamut_packet)
21194562236bSHarry Wentland {
21204562236bSHarry Wentland 	/* For gamut we recalc checksum */
21216e4d6beeSTony Cheng 	if (gamut_packet->valid) {
21224562236bSHarry Wentland 		uint8_t chk_sum = 0;
21234562236bSHarry Wentland 		uint8_t *ptr;
21244562236bSHarry Wentland 		uint8_t i;
21254562236bSHarry Wentland 
21264562236bSHarry Wentland 		/*start of the Gamut data. */
21276e4d6beeSTony Cheng 		ptr = &gamut_packet->sb[3];
21284562236bSHarry Wentland 
21296e4d6beeSTony Cheng 		for (i = 0; i <= gamut_packet->sb[1]; i++)
21304562236bSHarry Wentland 			chk_sum += ptr[i];
21314562236bSHarry Wentland 
21326e4d6beeSTony Cheng 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
21331646a6feSAndrew Wong 	}
21344562236bSHarry Wentland }
21354562236bSHarry Wentland 
21364562236bSHarry Wentland static void set_avi_info_frame(
2137e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
21384562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
21394562236bSHarry Wentland {
21400971c40eSHarry Wentland 	struct dc_stream_state *stream = pipe_ctx->stream;
21414562236bSHarry Wentland 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
21424562236bSHarry Wentland 	uint32_t pixel_encoding = 0;
21434562236bSHarry Wentland 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
21444562236bSHarry Wentland 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
21454562236bSHarry Wentland 	bool itc = false;
214650e27654SZeyu Fan 	uint8_t itc_value = 0;
21474562236bSHarry Wentland 	uint8_t cn0_cn1 = 0;
214850e27654SZeyu Fan 	unsigned int cn0_cn1_value = 0;
21494562236bSHarry Wentland 	uint8_t *check_sum = NULL;
21504562236bSHarry Wentland 	uint8_t byte_index = 0;
2151754e3673SAnthony Koo 	union hdmi_info_packet hdmi_info;
215250e27654SZeyu Fan 	union display_content_support support = {0};
21534fa086b9SLeo (Sunpeng) Li 	unsigned int vic = pipe_ctx->stream->timing.vic;
215415e17335SCharlene Liu 	enum dc_timing_3d_format format;
21554562236bSHarry Wentland 
2156754e3673SAnthony Koo 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
2157754e3673SAnthony Koo 
21584fa086b9SLeo (Sunpeng) Li 	color_space = pipe_ctx->stream->output_color_space;
2159e5f2038eSCharlene Liu 	if (color_space == COLOR_SPACE_UNKNOWN)
21604fa086b9SLeo (Sunpeng) Li 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
2161e5f2038eSCharlene Liu 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
21624562236bSHarry Wentland 
21634562236bSHarry Wentland 	/* Initialize header */
2164e09b6473SAnthony Koo 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
21654562236bSHarry Wentland 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
21664562236bSHarry Wentland 	* not be used in HDMI 2.0 (Section 10.1) */
2167e09b6473SAnthony Koo 	hdmi_info.bits.header.version = 2;
2168e09b6473SAnthony Koo 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
21694562236bSHarry Wentland 
21704562236bSHarry Wentland 	/*
21714562236bSHarry Wentland 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
21724562236bSHarry Wentland 	 * according to HDMI 2.0 spec (Section 10.1)
21734562236bSHarry Wentland 	 */
21744562236bSHarry Wentland 
21754fa086b9SLeo (Sunpeng) Li 	switch (stream->timing.pixel_encoding) {
21764562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR422:
21774562236bSHarry Wentland 		pixel_encoding = 1;
21784562236bSHarry Wentland 		break;
21794562236bSHarry Wentland 
21804562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR444:
21814562236bSHarry Wentland 		pixel_encoding = 2;
21824562236bSHarry Wentland 		break;
21834562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR420:
21844562236bSHarry Wentland 		pixel_encoding = 3;
21854562236bSHarry Wentland 		break;
21864562236bSHarry Wentland 
21874562236bSHarry Wentland 	case PIXEL_ENCODING_RGB:
21884562236bSHarry Wentland 	default:
21894562236bSHarry Wentland 		pixel_encoding = 0;
21904562236bSHarry Wentland 	}
21914562236bSHarry Wentland 
21924562236bSHarry Wentland 	/* Y0_Y1_Y2 : The pixel encoding */
21934562236bSHarry Wentland 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
2194e09b6473SAnthony Koo 	hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
21954562236bSHarry Wentland 
21964562236bSHarry Wentland 	/* A0 = 1 Active Format Information valid */
2197e09b6473SAnthony Koo 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
21984562236bSHarry Wentland 
21994562236bSHarry Wentland 	/* B0, B1 = 3; Bar info data is valid */
2200e09b6473SAnthony Koo 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
22014562236bSHarry Wentland 
2202e09b6473SAnthony Koo 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
22034562236bSHarry Wentland 
22044562236bSHarry Wentland 	/* S0, S1 : Underscan / Overscan */
22054562236bSHarry Wentland 	/* TODO: un-hardcode scan type */
22064562236bSHarry Wentland 	scan_type = SCANNING_TYPE_UNDERSCAN;
2207e09b6473SAnthony Koo 	hdmi_info.bits.S0_S1 = scan_type;
22084562236bSHarry Wentland 
22094562236bSHarry Wentland 	/* C0, C1 : Colorimetry */
22108fde5884SCharlene Liu 	if (color_space == COLOR_SPACE_YCBCR709 ||
221115e17335SCharlene Liu 			color_space == COLOR_SPACE_YCBCR709_LIMITED)
2212e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
22138fde5884SCharlene Liu 	else if (color_space == COLOR_SPACE_YCBCR601 ||
22148fde5884SCharlene Liu 			color_space == COLOR_SPACE_YCBCR601_LIMITED)
2215e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
22168fde5884SCharlene Liu 	else {
2217e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
22188fde5884SCharlene Liu 	}
2219534db198SAmy Zhang 	if (color_space == COLOR_SPACE_2020_RGB_FULLRANGE ||
2220534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE ||
2221534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_YCBCR) {
2222e09b6473SAnthony Koo 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
2223e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2224534db198SAmy Zhang 	} else if (color_space == COLOR_SPACE_ADOBERGB) {
2225e09b6473SAnthony Koo 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
2226e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2227534db198SAmy Zhang 	}
2228534db198SAmy Zhang 
22294562236bSHarry Wentland 	/* TODO: un-hardcode aspect ratio */
22304fa086b9SLeo (Sunpeng) Li 	aspect = stream->timing.aspect_ratio;
22314562236bSHarry Wentland 
22324562236bSHarry Wentland 	switch (aspect) {
22334562236bSHarry Wentland 	case ASPECT_RATIO_4_3:
22344562236bSHarry Wentland 	case ASPECT_RATIO_16_9:
2235e09b6473SAnthony Koo 		hdmi_info.bits.M0_M1 = aspect;
22364562236bSHarry Wentland 		break;
22374562236bSHarry Wentland 
22384562236bSHarry Wentland 	case ASPECT_RATIO_NO_DATA:
22394562236bSHarry Wentland 	case ASPECT_RATIO_64_27:
22404562236bSHarry Wentland 	case ASPECT_RATIO_256_135:
22414562236bSHarry Wentland 	default:
2242e09b6473SAnthony Koo 		hdmi_info.bits.M0_M1 = 0;
22434562236bSHarry Wentland 	}
22444562236bSHarry Wentland 
22454562236bSHarry Wentland 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
2246e09b6473SAnthony Koo 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
22474562236bSHarry Wentland 
22484562236bSHarry Wentland 	/* TODO: un-hardcode cn0_cn1 and itc */
224950e27654SZeyu Fan 
22504562236bSHarry Wentland 	cn0_cn1 = 0;
225150e27654SZeyu Fan 	cn0_cn1_value = 0;
225250e27654SZeyu Fan 
225350e27654SZeyu Fan 	itc = true;
225450e27654SZeyu Fan 	itc_value = 1;
225550e27654SZeyu Fan 
2256ceb3dbb4SJun Lei 	support = stream->content_support;
22574562236bSHarry Wentland 
22584562236bSHarry Wentland 	if (itc) {
225950e27654SZeyu Fan 		if (!support.bits.valid_content_type) {
226050e27654SZeyu Fan 			cn0_cn1_value = 0;
226150e27654SZeyu Fan 		} else {
226250e27654SZeyu Fan 			if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
226350e27654SZeyu Fan 				if (support.bits.graphics_content == 1) {
226450e27654SZeyu Fan 					cn0_cn1_value = 0;
226550e27654SZeyu Fan 				}
226650e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
226750e27654SZeyu Fan 				if (support.bits.photo_content == 1) {
226850e27654SZeyu Fan 					cn0_cn1_value = 1;
226950e27654SZeyu Fan 				} else {
227050e27654SZeyu Fan 					cn0_cn1_value = 0;
227150e27654SZeyu Fan 					itc_value = 0;
227250e27654SZeyu Fan 				}
227350e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
227450e27654SZeyu Fan 				if (support.bits.cinema_content == 1) {
227550e27654SZeyu Fan 					cn0_cn1_value = 2;
227650e27654SZeyu Fan 				} else {
227750e27654SZeyu Fan 					cn0_cn1_value = 0;
227850e27654SZeyu Fan 					itc_value = 0;
227950e27654SZeyu Fan 				}
228050e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
228150e27654SZeyu Fan 				if (support.bits.game_content == 1) {
228250e27654SZeyu Fan 					cn0_cn1_value = 3;
228350e27654SZeyu Fan 				} else {
228450e27654SZeyu Fan 					cn0_cn1_value = 0;
228550e27654SZeyu Fan 					itc_value = 0;
228650e27654SZeyu Fan 				}
228750e27654SZeyu Fan 			}
228850e27654SZeyu Fan 		}
2289e09b6473SAnthony Koo 		hdmi_info.bits.CN0_CN1 = cn0_cn1_value;
2290e09b6473SAnthony Koo 		hdmi_info.bits.ITC = itc_value;
22914562236bSHarry Wentland 	}
22924562236bSHarry Wentland 
22934562236bSHarry Wentland 	/* TODO : We should handle YCC quantization */
22944562236bSHarry Wentland 	/* but we do not have matrix calculation */
2295ceb3dbb4SJun Lei 	if (stream->qs_bit == 1 &&
2296ceb3dbb4SJun Lei 			stream->qy_bit == 1) {
229750e27654SZeyu Fan 		if (color_space == COLOR_SPACE_SRGB ||
229850e27654SZeyu Fan 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE) {
2299e09b6473SAnthony Koo 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
2300993dca3eSQingqing Zhuo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
230150e27654SZeyu Fan 		} else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
230250e27654SZeyu Fan 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE) {
2303e09b6473SAnthony Koo 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
2304e09b6473SAnthony Koo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
23054562236bSHarry Wentland 		} else {
2306e09b6473SAnthony Koo 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2307e09b6473SAnthony Koo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
23084562236bSHarry Wentland 		}
230950e27654SZeyu Fan 	} else {
2310e09b6473SAnthony Koo 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2311e09b6473SAnthony Koo 		hdmi_info.bits.YQ0_YQ1   = YYC_QUANTIZATION_LIMITED_RANGE;
231250e27654SZeyu Fan 	}
231350e27654SZeyu Fan 
231415e17335SCharlene Liu 	///VIC
23154fa086b9SLeo (Sunpeng) Li 	format = stream->timing.timing_3d_format;
231615e17335SCharlene Liu 	/*todo, add 3DStereo support*/
231715e17335SCharlene Liu 	if (format != TIMING_3D_FORMAT_NONE) {
231815e17335SCharlene Liu 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
23194fa086b9SLeo (Sunpeng) Li 		switch (pipe_ctx->stream->timing.hdmi_vic) {
232015e17335SCharlene Liu 		case 1:
232115e17335SCharlene Liu 			vic = 95;
232215e17335SCharlene Liu 			break;
232315e17335SCharlene Liu 		case 2:
232415e17335SCharlene Liu 			vic = 94;
232515e17335SCharlene Liu 			break;
232615e17335SCharlene Liu 		case 3:
232715e17335SCharlene Liu 			vic = 93;
232815e17335SCharlene Liu 			break;
232915e17335SCharlene Liu 		case 4:
233015e17335SCharlene Liu 			vic = 98;
233115e17335SCharlene Liu 			break;
233215e17335SCharlene Liu 		default:
233315e17335SCharlene Liu 			break;
233415e17335SCharlene Liu 		}
233515e17335SCharlene Liu 	}
2336efa02336SChris Park 	/* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
2337e09b6473SAnthony Koo 	hdmi_info.bits.VIC0_VIC7 = vic;
2338efa02336SChris Park 	if (vic >= 128)
2339efa02336SChris Park 		hdmi_info.bits.header.version = 3;
2340efa02336SChris Park 	/* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
2341efa02336SChris Park 	 * the Source shall use 20 AVI InfoFrame Version 4
2342efa02336SChris Park 	 */
2343efa02336SChris Park 	if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
2344efa02336SChris Park 			hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
2345efa02336SChris Park 		hdmi_info.bits.header.version = 4;
2346efa02336SChris Park 		hdmi_info.bits.header.length = 14;
2347efa02336SChris Park 	}
23484562236bSHarry Wentland 
23494562236bSHarry Wentland 	/* pixel repetition
23504562236bSHarry Wentland 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
23514562236bSHarry Wentland 	 * repetition start from 1 */
2352e09b6473SAnthony Koo 	hdmi_info.bits.PR0_PR3 = 0;
23534562236bSHarry Wentland 
23544562236bSHarry Wentland 	/* Bar Info
23554562236bSHarry Wentland 	 * barTop:    Line Number of End of Top Bar.
23564562236bSHarry Wentland 	 * barBottom: Line Number of Start of Bottom Bar.
23574562236bSHarry Wentland 	 * barLeft:   Pixel Number of End of Left Bar.
23584562236bSHarry Wentland 	 * barRight:  Pixel Number of Start of Right Bar. */
2359e09b6473SAnthony Koo 	hdmi_info.bits.bar_top = stream->timing.v_border_top;
2360e09b6473SAnthony Koo 	hdmi_info.bits.bar_bottom = (stream->timing.v_total
23614fa086b9SLeo (Sunpeng) Li 			- stream->timing.v_border_bottom + 1);
2362e09b6473SAnthony Koo 	hdmi_info.bits.bar_left  = stream->timing.h_border_left;
2363e09b6473SAnthony Koo 	hdmi_info.bits.bar_right = (stream->timing.h_total
23644fa086b9SLeo (Sunpeng) Li 			- stream->timing.h_border_right + 1);
23654562236bSHarry Wentland 
23662f482c4fSChris Park     /* Additional Colorimetry Extension
23672f482c4fSChris Park      * Used in conduction with C0-C1 and EC0-EC2
23682f482c4fSChris Park      * 0 = DCI-P3 RGB (D65)
23692f482c4fSChris Park      * 1 = DCI-P3 RGB (theater)
23702f482c4fSChris Park      */
23712f482c4fSChris Park 	hdmi_info.bits.ACE0_ACE3 = 0;
23722f482c4fSChris Park 
23734562236bSHarry Wentland 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
2374e09b6473SAnthony Koo 	check_sum = &hdmi_info.packet_raw_data.sb[0];
2375e8d726b7SReza Amini 
2376efa02336SChris Park 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
23774562236bSHarry Wentland 
2378efa02336SChris Park 	for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
2379e09b6473SAnthony Koo 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
23804562236bSHarry Wentland 
23814562236bSHarry Wentland 	/* one byte complement */
23824562236bSHarry Wentland 	*check_sum = (uint8_t) (0x100 - *check_sum);
23834562236bSHarry Wentland 
23844562236bSHarry Wentland 	/* Store in hw_path_mode */
2385e09b6473SAnthony Koo 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
2386e09b6473SAnthony Koo 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
2387e09b6473SAnthony Koo 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
23884562236bSHarry Wentland 
2389e09b6473SAnthony Koo 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
2390e09b6473SAnthony Koo 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
23914562236bSHarry Wentland 
23924562236bSHarry Wentland 	info_packet->valid = true;
23934562236bSHarry Wentland }
23944562236bSHarry Wentland 
23956e4d6beeSTony Cheng static void set_vendor_info_packet(
2396e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
23970971c40eSHarry Wentland 		struct dc_stream_state *stream)
23984562236bSHarry Wentland {
2399ecd0136bSHarmanprit Tatla 	/* SPD info packet for FreeSync */
240015e17335SCharlene Liu 
2401ecd0136bSHarmanprit Tatla 	/* Check if Freesync is supported. Return if false. If true,
2402ecd0136bSHarmanprit Tatla 	 * set the corresponding bit in the info packet
2403ecd0136bSHarmanprit Tatla 	 */
2404ecd0136bSHarmanprit Tatla 	if (!stream->vsp_infopacket.valid)
24054562236bSHarry Wentland 		return;
24064562236bSHarry Wentland 
2407ecd0136bSHarmanprit Tatla 	*info_packet = stream->vsp_infopacket;
24084562236bSHarry Wentland }
24094562236bSHarry Wentland 
24106e4d6beeSTony Cheng static void set_spd_info_packet(
2411e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
24120971c40eSHarry Wentland 		struct dc_stream_state *stream)
24134562236bSHarry Wentland {
24144562236bSHarry Wentland 	/* SPD info packet for FreeSync */
24154562236bSHarry Wentland 
24164562236bSHarry Wentland 	/* Check if Freesync is supported. Return if false. If true,
24174562236bSHarry Wentland 	 * set the corresponding bit in the info packet
24184562236bSHarry Wentland 	 */
241998e6436dSAnthony Koo 	if (!stream->vrr_infopacket.valid)
24204562236bSHarry Wentland 		return;
24214562236bSHarry Wentland 
242298e6436dSAnthony Koo 	*info_packet = stream->vrr_infopacket;
24234562236bSHarry Wentland }
24244562236bSHarry Wentland 
24251646a6feSAndrew Wong static void set_hdr_static_info_packet(
2426e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
24270971c40eSHarry Wentland 		struct dc_stream_state *stream)
24281646a6feSAndrew Wong {
24290eeef690SAnthony Koo 	/* HDR Static Metadata info packet for HDR10 */
24301646a6feSAndrew Wong 
2431a10dc97aSKrunoslav Kovac 	if (!stream->hdr_static_metadata.valid ||
2432a10dc97aSKrunoslav Kovac 			stream->use_dynamic_meta)
243310bff005SYongqiang Sun 		return;
243410bff005SYongqiang Sun 
24350eeef690SAnthony Koo 	*info_packet = stream->hdr_static_metadata;
24361646a6feSAndrew Wong }
24371646a6feSAndrew Wong 
24386e4d6beeSTony Cheng static void set_vsc_info_packet(
2439e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
24400971c40eSHarry Wentland 		struct dc_stream_state *stream)
24414562236bSHarry Wentland {
24421336926fSAlvin lee 	if (!stream->vsc_infopacket.valid)
24434562236bSHarry Wentland 		return;
24444562236bSHarry Wentland 
24451336926fSAlvin lee 	*info_packet = stream->vsc_infopacket;
24464562236bSHarry Wentland }
24474562236bSHarry Wentland 
2448f36cc577SBhawanpreet Lakha void dc_resource_state_destruct(struct dc_state *context)
24494562236bSHarry Wentland {
24504562236bSHarry Wentland 	int i, j;
24514562236bSHarry Wentland 
2452ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
24533be5262eSHarry Wentland 		for (j = 0; j < context->stream_status[i].plane_count; j++)
24543be5262eSHarry Wentland 			dc_plane_state_release(
24553be5262eSHarry Wentland 				context->stream_status[i].plane_states[j]);
24564562236bSHarry Wentland 
24573be5262eSHarry Wentland 		context->stream_status[i].plane_count = 0;
24584fa086b9SLeo (Sunpeng) Li 		dc_stream_release(context->streams[i]);
2459ab2541b6SAric Cyr 		context->streams[i] = NULL;
24604562236bSHarry Wentland 	}
24614562236bSHarry Wentland }
24624562236bSHarry Wentland 
2463f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct(
2464608ac7bbSJerry Zuo 		const struct dc_state *src_ctx,
2465608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
24664562236bSHarry Wentland {
24674562236bSHarry Wentland 	int i, j;
24688ee5702aSDave Airlie 	struct kref refcount = dst_ctx->refcount;
24694562236bSHarry Wentland 
24704562236bSHarry Wentland 	*dst_ctx = *src_ctx;
24714562236bSHarry Wentland 
2472a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
24734562236bSHarry Wentland 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
24744562236bSHarry Wentland 
24754562236bSHarry Wentland 		if (cur_pipe->top_pipe)
24764562236bSHarry Wentland 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
24774562236bSHarry Wentland 
24784562236bSHarry Wentland 		if (cur_pipe->bottom_pipe)
24794562236bSHarry Wentland 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
2480b1f6d01cSDmytro Laktyushkin 
2481b1f6d01cSDmytro Laktyushkin 		if (cur_pipe->next_odm_pipe)
2482b1f6d01cSDmytro Laktyushkin 			cur_pipe->next_odm_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
2483b1f6d01cSDmytro Laktyushkin 
2484b1f6d01cSDmytro Laktyushkin 		if (cur_pipe->prev_odm_pipe)
2485b1f6d01cSDmytro Laktyushkin 			cur_pipe->prev_odm_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
24864562236bSHarry Wentland 	}
24874562236bSHarry Wentland 
2488ab2541b6SAric Cyr 	for (i = 0; i < dst_ctx->stream_count; i++) {
24894fa086b9SLeo (Sunpeng) Li 		dc_stream_retain(dst_ctx->streams[i]);
24903be5262eSHarry Wentland 		for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
24913be5262eSHarry Wentland 			dc_plane_state_retain(
24923be5262eSHarry Wentland 				dst_ctx->stream_status[i].plane_states[j]);
24934562236bSHarry Wentland 	}
24949a3afbb3SAndrey Grodzovsky 
24959a3afbb3SAndrey Grodzovsky 	/* context refcount should not be overridden */
24968ee5702aSDave Airlie 	dst_ctx->refcount = refcount;
24979a3afbb3SAndrey Grodzovsky 
24984562236bSHarry Wentland }
24994562236bSHarry Wentland 
25004562236bSHarry Wentland struct clock_source *dc_resource_find_first_free_pll(
2501a2b8659dSTony Cheng 		struct resource_context *res_ctx,
2502a2b8659dSTony Cheng 		const struct resource_pool *pool)
25034562236bSHarry Wentland {
25044562236bSHarry Wentland 	int i;
25054562236bSHarry Wentland 
2506a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; ++i) {
25074562236bSHarry Wentland 		if (res_ctx->clock_source_ref_count[i] == 0)
2508a2b8659dSTony Cheng 			return pool->clock_sources[i];
25094562236bSHarry Wentland 	}
25104562236bSHarry Wentland 
25114562236bSHarry Wentland 	return NULL;
25124562236bSHarry Wentland }
25134562236bSHarry Wentland 
25144562236bSHarry Wentland void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
25154562236bSHarry Wentland {
25164562236bSHarry Wentland 	enum signal_type signal = SIGNAL_TYPE_NONE;
251796c50c0dSHarry Wentland 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
25184562236bSHarry Wentland 
25194562236bSHarry Wentland 	/* default all packets to invalid */
25206e4d6beeSTony Cheng 	info->avi.valid = false;
25216e4d6beeSTony Cheng 	info->gamut.valid = false;
25226e4d6beeSTony Cheng 	info->vendor.valid = false;
2523630e3573SJeff Smith 	info->spd.valid = false;
25246e4d6beeSTony Cheng 	info->hdrsmd.valid = false;
25256e4d6beeSTony Cheng 	info->vsc.valid = false;
25264562236bSHarry Wentland 
25274562236bSHarry Wentland 	signal = pipe_ctx->stream->signal;
25284562236bSHarry Wentland 
25294562236bSHarry Wentland 	/* HDMi and DP have different info packets*/
25304562236bSHarry Wentland 	if (dc_is_hdmi_signal(signal)) {
25316e4d6beeSTony Cheng 		set_avi_info_frame(&info->avi, pipe_ctx);
25326e4d6beeSTony Cheng 
25336e4d6beeSTony Cheng 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
25346e4d6beeSTony Cheng 
25356e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
25366e4d6beeSTony Cheng 
253756ef6ed9SAnthony Koo 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
25386e4d6beeSTony Cheng 
2539a33fa99dSHarry Wentland 	} else if (dc_is_dp_signal(signal)) {
25406e4d6beeSTony Cheng 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
25416e4d6beeSTony Cheng 
25426e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
25436e4d6beeSTony Cheng 
254456ef6ed9SAnthony Koo 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
2545a33fa99dSHarry Wentland 	}
25464562236bSHarry Wentland 
25476e4d6beeSTony Cheng 	patch_gamut_packet_checksum(&info->gamut);
25484562236bSHarry Wentland }
25494562236bSHarry Wentland 
25504562236bSHarry Wentland enum dc_status resource_map_clock_resources(
2551fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
2552608ac7bbSJerry Zuo 		struct dc_state *context,
25531dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
25544562236bSHarry Wentland {
25554562236bSHarry Wentland 	/* acquire new resources */
25561dc90497SAndrey Grodzovsky 	const struct resource_pool *pool = dc->res_pool;
25571dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(
25581dc90497SAndrey Grodzovsky 				&context->res_ctx, stream);
25594562236bSHarry Wentland 
25601dc90497SAndrey Grodzovsky 	if (!pipe_ctx)
25611dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
25624562236bSHarry Wentland 
25634562236bSHarry Wentland 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
25644562236bSHarry Wentland 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
2565a2b8659dSTony Cheng 		pipe_ctx->clock_source = pool->dp_clock_source;
25664562236bSHarry Wentland 	else {
25674562236bSHarry Wentland 		pipe_ctx->clock_source = NULL;
25684562236bSHarry Wentland 
2569fb3466a4SBhawanpreet Lakha 		if (!dc->config.disable_disp_pll_sharing)
25704ed4e51bSMikita Lipski 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
25714562236bSHarry Wentland 				&context->res_ctx,
25724562236bSHarry Wentland 				pipe_ctx);
25734562236bSHarry Wentland 
25744562236bSHarry Wentland 		if (pipe_ctx->clock_source == NULL)
25754562236bSHarry Wentland 			pipe_ctx->clock_source =
2576a2b8659dSTony Cheng 				dc_resource_find_first_free_pll(
2577a2b8659dSTony Cheng 					&context->res_ctx,
2578a2b8659dSTony Cheng 					pool);
25794562236bSHarry Wentland 	}
25804562236bSHarry Wentland 
25814562236bSHarry Wentland 	if (pipe_ctx->clock_source == NULL)
25824562236bSHarry Wentland 		return DC_NO_CLOCK_SOURCE_RESOURCE;
25834562236bSHarry Wentland 
25844562236bSHarry Wentland 	resource_reference_clock_source(
2585a2b8659dSTony Cheng 		&context->res_ctx, pool,
25864562236bSHarry Wentland 		pipe_ctx->clock_source);
25874562236bSHarry Wentland 
25884562236bSHarry Wentland 	return DC_OK;
25894562236bSHarry Wentland }
25904562236bSHarry Wentland 
25914562236bSHarry Wentland /*
25924562236bSHarry Wentland  * Note: We need to disable output if clock sources change,
25934562236bSHarry Wentland  * since bios does optimization and doesn't apply if changing
25944562236bSHarry Wentland  * PHY when not already disabled.
25954562236bSHarry Wentland  */
25964562236bSHarry Wentland bool pipe_need_reprogram(
25974562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx_old,
25984562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
25994562236bSHarry Wentland {
2600cfe4645eSDmytro Laktyushkin 	if (!pipe_ctx_old->stream)
2601cfe4645eSDmytro Laktyushkin 		return false;
2602cfe4645eSDmytro Laktyushkin 
26034562236bSHarry Wentland 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
26044562236bSHarry Wentland 		return true;
26054562236bSHarry Wentland 
26064562236bSHarry Wentland 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
26074562236bSHarry Wentland 		return true;
26084562236bSHarry Wentland 
2609afaacef4SHarry Wentland 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
26104562236bSHarry Wentland 		return true;
26114562236bSHarry Wentland 
26124562236bSHarry Wentland 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
26134562236bSHarry Wentland 			&& pipe_ctx_old->stream != pipe_ctx->stream)
26144562236bSHarry Wentland 		return true;
26154562236bSHarry Wentland 
26168e9c4c8cSHarry Wentland 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
26174562236bSHarry Wentland 		return true;
26184562236bSHarry Wentland 
26194562236bSHarry Wentland 	if (is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26204562236bSHarry Wentland 		return true;
26214562236bSHarry Wentland 
26226b622181SJulian Parkin 	if (is_hdr_static_meta_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26236b622181SJulian Parkin 		return true;
26244562236bSHarry Wentland 
26251e7e86c4SSamson Tam 	if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
26261e7e86c4SSamson Tam 		return true;
26271e7e86c4SSamson Tam 
26281336926fSAlvin lee 	if (is_vsc_info_packet_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26291336926fSAlvin lee 		return true;
26301336926fSAlvin lee 
2631eed928dcSCharlene Liu 	if (false == pipe_ctx_old->stream->link->link_state_valid &&
2632eed928dcSCharlene Liu 		false == pipe_ctx_old->stream->dpms_off)
2633eed928dcSCharlene Liu 		return true;
2634eed928dcSCharlene Liu 
26354562236bSHarry Wentland 	return false;
26364562236bSHarry Wentland }
2637529cad0fSDing Wang 
26380971c40eSHarry Wentland void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
2639529cad0fSDing Wang 		struct bit_depth_reduction_params *fmt_bit_depth)
2640529cad0fSDing Wang {
26414fa086b9SLeo (Sunpeng) Li 	enum dc_dither_option option = stream->dither_option;
2642529cad0fSDing Wang 	enum dc_pixel_encoding pixel_encoding =
26434fa086b9SLeo (Sunpeng) Li 			stream->timing.pixel_encoding;
2644529cad0fSDing Wang 
2645529cad0fSDing Wang 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
2646529cad0fSDing Wang 
2647603767f9STony Cheng 	if (option == DITHER_OPTION_DEFAULT) {
2648603767f9STony Cheng 		switch (stream->timing.display_color_depth) {
2649603767f9STony Cheng 		case COLOR_DEPTH_666:
2650603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL6;
2651603767f9STony Cheng 			break;
2652603767f9STony Cheng 		case COLOR_DEPTH_888:
2653603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL8;
2654603767f9STony Cheng 			break;
2655603767f9STony Cheng 		case COLOR_DEPTH_101010:
2656603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL10;
2657603767f9STony Cheng 			break;
2658603767f9STony Cheng 		default:
2659603767f9STony Cheng 			option = DITHER_OPTION_DISABLE;
2660603767f9STony Cheng 		}
2661603767f9STony Cheng 	}
2662603767f9STony Cheng 
2663529cad0fSDing Wang 	if (option == DITHER_OPTION_DISABLE)
2664529cad0fSDing Wang 		return;
2665529cad0fSDing Wang 
2666529cad0fSDing Wang 	if (option == DITHER_OPTION_TRUN6) {
2667529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2668529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
2669529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN8 ||
2670529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
2671529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6) {
2672529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2673529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
2674529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN10        ||
2675529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
2676529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
2677529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8     ||
2678529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2679529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2680529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2681529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2682529cad0fSDing Wang 	}
2683529cad0fSDing Wang 
2684529cad0fSDing Wang 	/* special case - Formatter can only reduce by 4 bits at most.
2685529cad0fSDing Wang 	 * When reducing from 12 to 6 bits,
2686529cad0fSDing Wang 	 * HW recommends we use trunc with round mode
2687529cad0fSDing Wang 	 * (if we did nothing, trunc to 10 bits would be used)
2688529cad0fSDing Wang 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
2689529cad0fSDing Wang 	 * as the input was 10 bits.
2690529cad0fSDing Wang 	 */
2691529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2692529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2693529cad0fSDing Wang 			option == DITHER_OPTION_FM6) {
2694529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2695529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2696529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
2697529cad0fSDing Wang 	}
2698529cad0fSDing Wang 
2699529cad0fSDing Wang 	/* spatial dither
2700529cad0fSDing Wang 	 * note that spatial modes 1-3 are never used
2701529cad0fSDing Wang 	 */
2702529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
2703529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2704529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
2705529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
2706529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2707529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
2708529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2709529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2710529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2711529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
2712529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2713529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6        ||
2714529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
2715529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2716529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2717529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
2718529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2719529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2720529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2721529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
2722529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10 ||
2723529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8 ||
2724529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6) {
2725529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2726529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
2727529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2728529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2729529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2730529cad0fSDing Wang 	}
2731529cad0fSDing Wang 
2732529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6 ||
2733529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2734529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10) {
2735529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
2736529cad0fSDing Wang 	} else {
2737529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
2738529cad0fSDing Wang 	}
2739529cad0fSDing Wang 
2740529cad0fSDing Wang 	//////////////////////
2741529cad0fSDing Wang 	//// temporal dither
2742529cad0fSDing Wang 	//////////////////////
2743529cad0fSDing Wang 	if (option == DITHER_OPTION_FM6           ||
2744529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6     ||
2745529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6     ||
2746529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2747529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6      ||
2748529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2749529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2750529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
2751529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM8        ||
2752529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8  ||
2753529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8) {
2754529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2755529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
2756529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM10) {
2757529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2758529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
2759529cad0fSDing Wang 	}
2760529cad0fSDing Wang 
2761529cad0fSDing Wang 	fmt_bit_depth->pixel_encoding = pixel_encoding;
2762529cad0fSDing Wang }
27639345d987SAndrey Grodzovsky 
276462c933f9SYongqiang Sun enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
27659345d987SAndrey Grodzovsky {
2766ceb3dbb4SJun Lei 	struct dc_link *link = stream->link;
27672b77dcc5SAnthony Koo 	struct timing_generator *tg = dc->res_pool->timing_generators[0];
27689345d987SAndrey Grodzovsky 	enum dc_status res = DC_OK;
27699345d987SAndrey Grodzovsky 
27704fa086b9SLeo (Sunpeng) Li 	calculate_phy_pix_clks(stream);
27719345d987SAndrey Grodzovsky 
27724fa086b9SLeo (Sunpeng) Li 	if (!tg->funcs->validate_timing(tg, &stream->timing))
27739345d987SAndrey Grodzovsky 		res = DC_FAIL_CONTROLLER_VALIDATE;
27749345d987SAndrey Grodzovsky 
2775248cbed6SEric Bernstein 	if (res == DC_OK) {
27769345d987SAndrey Grodzovsky 		if (!link->link_enc->funcs->validate_output_with_stream(
27774fa086b9SLeo (Sunpeng) Li 						link->link_enc, stream))
27789345d987SAndrey Grodzovsky 			res = DC_FAIL_ENC_VALIDATE;
2779248cbed6SEric Bernstein 	}
27809345d987SAndrey Grodzovsky 
27819345d987SAndrey Grodzovsky 	/* TODO: validate audio ASIC caps, encoder */
27829345d987SAndrey Grodzovsky 
27839345d987SAndrey Grodzovsky 	if (res == DC_OK)
27844fa086b9SLeo (Sunpeng) Li 		res = dc_link_validate_mode_timing(stream,
27859345d987SAndrey Grodzovsky 		      link,
27864fa086b9SLeo (Sunpeng) Li 		      &stream->timing);
27879345d987SAndrey Grodzovsky 
278862c933f9SYongqiang Sun 	return res;
27899345d987SAndrey Grodzovsky }
2790792671d7SAndrey Grodzovsky 
279162c933f9SYongqiang Sun enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
2792792671d7SAndrey Grodzovsky {
279362c933f9SYongqiang Sun 	enum dc_status res = DC_OK;
279462c933f9SYongqiang Sun 
2795792671d7SAndrey Grodzovsky 	/* TODO For now validates pixel format only */
27968e7095b9SDmytro Laktyushkin 	if (dc->res_pool->funcs->validate_plane)
279762c933f9SYongqiang Sun 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
2798792671d7SAndrey Grodzovsky 
279962c933f9SYongqiang Sun 	return res;
2800792671d7SAndrey Grodzovsky }
280174eac5f3SSu Sung Chung 
280274eac5f3SSu Sung Chung unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
280374eac5f3SSu Sung Chung {
280474eac5f3SSu Sung Chung 	switch (format) {
280574eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
280674eac5f3SSu Sung Chung 		return 8;
280774eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
280874eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
280974eac5f3SSu Sung Chung 		return 12;
281074eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
281174eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
281274eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
281374eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
281474eac5f3SSu Sung Chung 		return 16;
281574eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
281674eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
281774eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
281874eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
281974eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
282074eac5f3SSu Sung Chung 		return 32;
282174eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
282274eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
282374eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
282474eac5f3SSu Sung Chung 		return 64;
282574eac5f3SSu Sung Chung 	default:
282674eac5f3SSu Sung Chung 		ASSERT_CRITICAL(false);
282774eac5f3SSu Sung Chung 		return -1;
282874eac5f3SSu Sung Chung 	}
282974eac5f3SSu Sung Chung }
28303ab4cc65SCharlene Liu static unsigned int get_max_audio_sample_rate(struct audio_mode *modes)
28313ab4cc65SCharlene Liu {
28323ab4cc65SCharlene Liu 	if (modes) {
28333ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_192)
28343ab4cc65SCharlene Liu 			return 192000;
28353ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_176_4)
28363ab4cc65SCharlene Liu 			return 176400;
28373ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_96)
28383ab4cc65SCharlene Liu 			return 96000;
28393ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_88_2)
28403ab4cc65SCharlene Liu 			return 88200;
28413ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_48)
28423ab4cc65SCharlene Liu 			return 48000;
28433ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_44_1)
28443ab4cc65SCharlene Liu 			return 44100;
28453ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_32)
28463ab4cc65SCharlene Liu 			return 32000;
28473ab4cc65SCharlene Liu 	}
28483ab4cc65SCharlene Liu 	/*original logic when no audio info*/
28493ab4cc65SCharlene Liu 	return 441000;
28503ab4cc65SCharlene Liu }
28513ab4cc65SCharlene Liu 
28523ab4cc65SCharlene Liu void get_audio_check(struct audio_info *aud_modes,
28533ab4cc65SCharlene Liu 	struct audio_check *audio_chk)
28543ab4cc65SCharlene Liu {
28553ab4cc65SCharlene Liu 	unsigned int i;
28563ab4cc65SCharlene Liu 	unsigned int max_sample_rate = 0;
28573ab4cc65SCharlene Liu 
28583ab4cc65SCharlene Liu 	if (aud_modes) {
28593ab4cc65SCharlene Liu 		audio_chk->audio_packet_type = 0x2;/*audio sample packet AP = .25 for layout0, 1 for layout1*/
28603ab4cc65SCharlene Liu 
28613ab4cc65SCharlene Liu 		audio_chk->max_audiosample_rate = 0;
28623ab4cc65SCharlene Liu 		for (i = 0; i < aud_modes->mode_count; i++) {
28633ab4cc65SCharlene Liu 			max_sample_rate = get_max_audio_sample_rate(&aud_modes->modes[i]);
28643ab4cc65SCharlene Liu 			if (audio_chk->max_audiosample_rate < max_sample_rate)
28653ab4cc65SCharlene Liu 				audio_chk->max_audiosample_rate = max_sample_rate;
28663ab4cc65SCharlene Liu 			/*dts takes the same as type 2: AP = 0.25*/
28673ab4cc65SCharlene Liu 		}
28683ab4cc65SCharlene Liu 		/*check which one take more bandwidth*/
28693ab4cc65SCharlene Liu 		if (audio_chk->max_audiosample_rate > 192000)
28703ab4cc65SCharlene Liu 			audio_chk->audio_packet_type = 0x9;/*AP =1*/
28713ab4cc65SCharlene Liu 		audio_chk->acat = 0;/*not support*/
28723ab4cc65SCharlene Liu 	}
28733ab4cc65SCharlene Liu }
28743ab4cc65SCharlene Liu 
28753ab4cc65SCharlene Liu 
28763ab4cc65SCharlene Liu 
28773ab4cc65SCharlene Liu 
2878