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 
9434ef0b9d0SYueHaibing static void calculate_integer_scaling(struct pipe_ctx *pipe_ctx)
9443b733278SReza Amini {
94500ada9d1SReza Amini 	unsigned int integer_multiple = 1;
9463b733278SReza Amini 
94700ada9d1SReza Amini 	if (pipe_ctx->plane_state->scaling_quality.integer_scaling) {
9483b733278SReza Amini 		// calculate maximum # of replication of src onto addressable
94900ada9d1SReza Amini 		integer_multiple = min(
9503b733278SReza Amini 				pipe_ctx->stream->timing.h_addressable / pipe_ctx->stream->src.width,
9513b733278SReza Amini 				pipe_ctx->stream->timing.v_addressable  / pipe_ctx->stream->src.height);
9523b733278SReza Amini 
9533b733278SReza Amini 		//scale dst
9543b733278SReza Amini 		pipe_ctx->stream->dst.width  = integer_multiple * pipe_ctx->stream->src.width;
9553b733278SReza Amini 		pipe_ctx->stream->dst.height = integer_multiple * pipe_ctx->stream->src.height;
9563b733278SReza Amini 
9573b733278SReza Amini 		//center dst onto addressable
9583b733278SReza Amini 		pipe_ctx->stream->dst.x = (pipe_ctx->stream->timing.h_addressable - pipe_ctx->stream->dst.width)/2;
9593b733278SReza Amini 		pipe_ctx->stream->dst.y = (pipe_ctx->stream->timing.v_addressable - pipe_ctx->stream->dst.height)/2;
9603b733278SReza Amini 
96100ada9d1SReza Amini 		//We are guaranteed that we are scaling in integer ratio
9623b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.v_taps = 1;
9633b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.h_taps = 1;
9643b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.v_taps_c = 1;
9653b733278SReza Amini 		pipe_ctx->plane_state->scaling_quality.h_taps_c = 1;
9663b733278SReza Amini 	}
9673b733278SReza Amini }
9683b733278SReza Amini 
969b2d0a103SDmytro Laktyushkin bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
970b2d0a103SDmytro Laktyushkin {
9713be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
9724fa086b9SLeo (Sunpeng) Li 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
973b2d0a103SDmytro Laktyushkin 	bool res = false;
9745d4b05ddSBhawanpreet Lakha 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
9754562236bSHarry Wentland 	/* Important: scaling ratio calculation requires pixel format,
9764562236bSHarry Wentland 	 * lb depth calculation requires recout and taps require scaling ratios.
977b2d0a103SDmytro Laktyushkin 	 * Inits require viewport, taps, ratios and recout of split pipe
9784562236bSHarry Wentland 	 */
9796702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
9803be5262eSHarry Wentland 			pipe_ctx->plane_state->format);
9814562236bSHarry Wentland 
9823b733278SReza Amini 	calculate_integer_scaling(pipe_ctx);
9833b733278SReza Amini 
984b2d0a103SDmytro Laktyushkin 	calculate_scaling_ratios(pipe_ctx);
985b2d0a103SDmytro Laktyushkin 
986b2d0a103SDmytro Laktyushkin 	calculate_viewport(pipe_ctx);
9874562236bSHarry Wentland 
9886702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.viewport.height < 16 || pipe_ctx->plane_res.scl_data.viewport.width < 16)
9894562236bSHarry Wentland 		return false;
9904562236bSHarry Wentland 
9919b6067c0SDmytro Laktyushkin 	calculate_recout(pipe_ctx);
9924562236bSHarry Wentland 
9934562236bSHarry Wentland 	/**
9944562236bSHarry Wentland 	 * Setting line buffer pixel depth to 24bpp yields banding
9954562236bSHarry Wentland 	 * on certain displays, such as the Sharp 4k
9964562236bSHarry Wentland 	 */
9976702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
9984562236bSHarry Wentland 
999199e458aSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.recout.x += timing->h_border_left;
100058bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.recout.y += timing->v_border_top;
1001199e458aSDmytro Laktyushkin 
100258bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.h_active = timing->h_addressable + timing->h_border_left + timing->h_border_right;
100358bb0e63SAndrew Jiang 	pipe_ctx->plane_res.scl_data.v_active = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
10041b6c8067SBhawanpreet Lakha 
10054562236bSHarry Wentland 	/* Taps calculations */
1006d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.xfm != NULL)
100786a66c4eSHarry Wentland 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
100886a66c4eSHarry Wentland 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
10094562236bSHarry Wentland 
1010d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.dpp != NULL)
1011d94585a0SYue Hin Lau 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1012d94585a0SYue Hin Lau 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1013f7938bc0SReza Amini 
1014f7938bc0SReza Amini 
10154562236bSHarry Wentland 	if (!res) {
10164562236bSHarry Wentland 		/* Try 24 bpp linebuffer */
10176702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
10184562236bSHarry Wentland 
10191b6c8067SBhawanpreet Lakha 		if (pipe_ctx->plane_res.xfm != NULL)
102086a66c4eSHarry Wentland 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
10211b6c8067SBhawanpreet Lakha 					pipe_ctx->plane_res.xfm,
10221b6c8067SBhawanpreet Lakha 					&pipe_ctx->plane_res.scl_data,
10231b6c8067SBhawanpreet Lakha 					&plane_state->scaling_quality);
1024d94585a0SYue Hin Lau 
10251b6c8067SBhawanpreet Lakha 		if (pipe_ctx->plane_res.dpp != NULL)
1026d94585a0SYue Hin Lau 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
10271b6c8067SBhawanpreet Lakha 					pipe_ctx->plane_res.dpp,
10281b6c8067SBhawanpreet Lakha 					&pipe_ctx->plane_res.scl_data,
10291b6c8067SBhawanpreet Lakha 					&plane_state->scaling_quality);
10304562236bSHarry Wentland 	}
10314562236bSHarry Wentland 
1032b2d0a103SDmytro Laktyushkin 	if (res)
10331fbd2cfcSDmytro Laktyushkin 		/* May need to re-check lb size after this in some obscure scenario */
10349b6067c0SDmytro Laktyushkin 		calculate_inits_and_adj_vp(pipe_ctx);
1035b2d0a103SDmytro Laktyushkin 
10361296423bSBhawanpreet Lakha 	DC_LOG_SCALER(
10374562236bSHarry Wentland 				"%s: Viewport:\nheight:%d width:%d x:%d "
10384562236bSHarry Wentland 				"y:%d\n dst_rect:\nheight:%d width:%d x:%d "
10394562236bSHarry Wentland 				"y:%d\n",
10404562236bSHarry Wentland 				__func__,
10416702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.height,
10426702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.width,
10436702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.x,
10446702a9acSHarry Wentland 				pipe_ctx->plane_res.scl_data.viewport.y,
10453be5262eSHarry Wentland 				plane_state->dst_rect.height,
10463be5262eSHarry Wentland 				plane_state->dst_rect.width,
10473be5262eSHarry Wentland 				plane_state->dst_rect.x,
10483be5262eSHarry Wentland 				plane_state->dst_rect.y);
10494562236bSHarry Wentland 
10504562236bSHarry Wentland 	return res;
10514562236bSHarry Wentland }
10524562236bSHarry Wentland 
10534562236bSHarry Wentland 
10544562236bSHarry Wentland enum dc_status resource_build_scaling_params_for_context(
1055fb3466a4SBhawanpreet Lakha 	const struct dc  *dc,
1056608ac7bbSJerry Zuo 	struct dc_state *context)
10574562236bSHarry Wentland {
10584562236bSHarry Wentland 	int i;
10594562236bSHarry Wentland 
10604562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
10613be5262eSHarry Wentland 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
10624562236bSHarry Wentland 				context->res_ctx.pipe_ctx[i].stream != NULL)
1063b2d0a103SDmytro Laktyushkin 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1064f84a8161STony Cheng 				return DC_FAIL_SCALING;
10654562236bSHarry Wentland 	}
10664562236bSHarry Wentland 
10674562236bSHarry Wentland 	return DC_OK;
10684562236bSHarry Wentland }
10694562236bSHarry Wentland 
1070a2b8659dSTony Cheng struct pipe_ctx *find_idle_secondary_pipe(
1071a2b8659dSTony Cheng 		struct resource_context *res_ctx,
10725581192dSJun Lei 		const struct resource_pool *pool,
10735581192dSJun Lei 		const struct pipe_ctx *primary_pipe)
10744562236bSHarry Wentland {
10754562236bSHarry Wentland 	int i;
10764562236bSHarry Wentland 	struct pipe_ctx *secondary_pipe = NULL;
10774562236bSHarry Wentland 
10784562236bSHarry Wentland 	/*
10795581192dSJun Lei 	 * We add a preferred pipe mapping to avoid the chance that
10805581192dSJun Lei 	 * MPCCs already in use will need to be reassigned to other trees.
10815581192dSJun Lei 	 * For example, if we went with the strict, assign backwards logic:
10825581192dSJun Lei 	 *
10835581192dSJun Lei 	 * (State 1)
10845581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
10855581192dSJun Lei 	 * Display B on, no surface, top pipe = 1
10865581192dSJun Lei 	 *
10875581192dSJun Lei 	 * (State 2)
10885581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
10895581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
10905581192dSJun Lei 	 *
10915581192dSJun Lei 	 * (State 3)
10925581192dSJun Lei 	 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
10935581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
10945581192dSJun Lei 	 *
10955581192dSJun Lei 	 * The state 2->3 transition requires remapping MPCC 5 from display B
10965581192dSJun Lei 	 * to display A.
10975581192dSJun Lei 	 *
10985581192dSJun Lei 	 * However, with the preferred pipe logic, state 2 would look like:
10995581192dSJun Lei 	 *
11005581192dSJun Lei 	 * (State 2)
11015581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
11025581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
11035581192dSJun Lei 	 *
11045581192dSJun Lei 	 * This would then cause 2->3 to not require remapping any MPCCs.
11055581192dSJun Lei 	 */
11065581192dSJun Lei 	if (primary_pipe) {
11075581192dSJun Lei 		int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
11085581192dSJun Lei 		if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
11095581192dSJun Lei 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
11105581192dSJun Lei 			secondary_pipe->pipe_idx = preferred_pipe_idx;
11115581192dSJun Lei 		}
11125581192dSJun Lei 	}
11135581192dSJun Lei 
11145581192dSJun Lei 	/*
11154562236bSHarry Wentland 	 * search backwards for the second pipe to keep pipe
11164562236bSHarry Wentland 	 * assignment more consistent
11174562236bSHarry Wentland 	 */
11185581192dSJun Lei 	if (!secondary_pipe)
1119a2b8659dSTony Cheng 		for (i = pool->pipe_count - 1; i >= 0; i--) {
11204562236bSHarry Wentland 			if (res_ctx->pipe_ctx[i].stream == NULL) {
11214562236bSHarry Wentland 				secondary_pipe = &res_ctx->pipe_ctx[i];
11224562236bSHarry Wentland 				secondary_pipe->pipe_idx = i;
11234562236bSHarry Wentland 				break;
11244562236bSHarry Wentland 			}
11254562236bSHarry Wentland 		}
11264562236bSHarry Wentland 
11274562236bSHarry Wentland 	return secondary_pipe;
11284562236bSHarry Wentland }
11294562236bSHarry Wentland 
11304562236bSHarry Wentland struct pipe_ctx *resource_get_head_pipe_for_stream(
11314562236bSHarry Wentland 		struct resource_context *res_ctx,
11320971c40eSHarry Wentland 		struct dc_stream_state *stream)
11334562236bSHarry Wentland {
11344562236bSHarry Wentland 	int i;
113522498036SDmytro Laktyushkin 
1136a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
1137b1f6d01cSDmytro Laktyushkin 		if (res_ctx->pipe_ctx[i].stream == stream
1138b1f6d01cSDmytro Laktyushkin 				&& !res_ctx->pipe_ctx[i].top_pipe
113922498036SDmytro Laktyushkin 				&& !res_ctx->pipe_ctx[i].prev_odm_pipe)
11404562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
11414562236bSHarry Wentland 	}
11424562236bSHarry Wentland 	return NULL;
11434562236bSHarry Wentland }
11444562236bSHarry Wentland 
1145b1f6d01cSDmytro Laktyushkin static struct pipe_ctx *resource_get_tail_pipe(
114619f89e23SAndrey Grodzovsky 		struct resource_context *res_ctx,
1147b1f6d01cSDmytro Laktyushkin 		struct pipe_ctx *head_pipe)
114819f89e23SAndrey Grodzovsky {
1149b1f6d01cSDmytro Laktyushkin 	struct pipe_ctx *tail_pipe;
115019f89e23SAndrey Grodzovsky 
115119f89e23SAndrey Grodzovsky 	tail_pipe = head_pipe->bottom_pipe;
115219f89e23SAndrey Grodzovsky 
115319f89e23SAndrey Grodzovsky 	while (tail_pipe) {
115419f89e23SAndrey Grodzovsky 		head_pipe = tail_pipe;
115519f89e23SAndrey Grodzovsky 		tail_pipe = tail_pipe->bottom_pipe;
115619f89e23SAndrey Grodzovsky 	}
115719f89e23SAndrey Grodzovsky 
115819f89e23SAndrey Grodzovsky 	return head_pipe;
115919f89e23SAndrey Grodzovsky }
116019f89e23SAndrey Grodzovsky 
11614562236bSHarry Wentland /*
1162ab2541b6SAric Cyr  * A free_pipe for a stream is defined here as a pipe
1163ab2541b6SAric Cyr  * that has no surface attached yet
11644562236bSHarry Wentland  */
1165b1f6d01cSDmytro Laktyushkin static struct pipe_ctx *acquire_free_pipe_for_head(
1166608ac7bbSJerry Zuo 		struct dc_state *context,
1167a2b8659dSTony Cheng 		const struct resource_pool *pool,
1168b1f6d01cSDmytro Laktyushkin 		struct pipe_ctx *head_pipe)
11694562236bSHarry Wentland {
11704562236bSHarry Wentland 	int i;
1171745cc746SDmytro Laktyushkin 	struct resource_context *res_ctx = &context->res_ctx;
11724562236bSHarry Wentland 
11733be5262eSHarry Wentland 	if (!head_pipe->plane_state)
11744562236bSHarry Wentland 		return head_pipe;
11754562236bSHarry Wentland 
11764562236bSHarry Wentland 	/* Re-use pipe already acquired for this stream if available*/
1177a2b8659dSTony Cheng 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1178b1f6d01cSDmytro Laktyushkin 		if (res_ctx->pipe_ctx[i].stream == head_pipe->stream &&
11793be5262eSHarry Wentland 				!res_ctx->pipe_ctx[i].plane_state) {
11804562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
11814562236bSHarry Wentland 		}
11824562236bSHarry Wentland 	}
11834562236bSHarry Wentland 
11844562236bSHarry Wentland 	/*
11854562236bSHarry Wentland 	 * At this point we have no re-useable pipe for this stream and we need
11864562236bSHarry Wentland 	 * to acquire an idle one to satisfy the request
11874562236bSHarry Wentland 	 */
11884562236bSHarry Wentland 
1189a2b8659dSTony Cheng 	if (!pool->funcs->acquire_idle_pipe_for_layer)
11904562236bSHarry Wentland 		return NULL;
11914562236bSHarry Wentland 
1192b1f6d01cSDmytro Laktyushkin 	return pool->funcs->acquire_idle_pipe_for_layer(context, pool, head_pipe->stream);
11934562236bSHarry Wentland }
11944562236bSHarry Wentland 
1195b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
11960f9a536fSDmytro Laktyushkin static int acquire_first_split_pipe(
11970f9a536fSDmytro Laktyushkin 		struct resource_context *res_ctx,
11980f9a536fSDmytro Laktyushkin 		const struct resource_pool *pool,
11990971c40eSHarry Wentland 		struct dc_stream_state *stream)
12000f9a536fSDmytro Laktyushkin {
12010f9a536fSDmytro Laktyushkin 	int i;
12020f9a536fSDmytro Laktyushkin 
12030f9a536fSDmytro Laktyushkin 	for (i = 0; i < pool->pipe_count; i++) {
120479592db3SDmytro Laktyushkin 		struct pipe_ctx *split_pipe = &res_ctx->pipe_ctx[i];
12050f9a536fSDmytro Laktyushkin 
1206b1f6d01cSDmytro Laktyushkin 		if (split_pipe->top_pipe &&
120779592db3SDmytro Laktyushkin 				split_pipe->top_pipe->plane_state == split_pipe->plane_state) {
120879592db3SDmytro Laktyushkin 			split_pipe->top_pipe->bottom_pipe = split_pipe->bottom_pipe;
120979592db3SDmytro Laktyushkin 			if (split_pipe->bottom_pipe)
121079592db3SDmytro Laktyushkin 				split_pipe->bottom_pipe->top_pipe = split_pipe->top_pipe;
12110f9a536fSDmytro Laktyushkin 
121279592db3SDmytro Laktyushkin 			if (split_pipe->top_pipe->plane_state)
121379592db3SDmytro Laktyushkin 				resource_build_scaling_params(split_pipe->top_pipe);
12140f9a536fSDmytro Laktyushkin 
121579592db3SDmytro Laktyushkin 			memset(split_pipe, 0, sizeof(*split_pipe));
121679592db3SDmytro Laktyushkin 			split_pipe->stream_res.tg = pool->timing_generators[i];
121779592db3SDmytro Laktyushkin 			split_pipe->plane_res.hubp = pool->hubps[i];
121879592db3SDmytro Laktyushkin 			split_pipe->plane_res.ipp = pool->ipps[i];
121979592db3SDmytro Laktyushkin 			split_pipe->plane_res.dpp = pool->dpps[i];
122079592db3SDmytro Laktyushkin 			split_pipe->stream_res.opp = pool->opps[i];
122179592db3SDmytro Laktyushkin 			split_pipe->plane_res.mpcc_inst = pool->dpps[i]->inst;
122279592db3SDmytro Laktyushkin 			split_pipe->pipe_idx = i;
122379592db3SDmytro Laktyushkin 
122479592db3SDmytro Laktyushkin 			split_pipe->stream = stream;
12250f9a536fSDmytro Laktyushkin 			return i;
12260f9a536fSDmytro Laktyushkin 		}
12270f9a536fSDmytro Laktyushkin 	}
12280f9a536fSDmytro Laktyushkin 	return -1;
12290f9a536fSDmytro Laktyushkin }
12300f9a536fSDmytro Laktyushkin #endif
12310f9a536fSDmytro Laktyushkin 
123219f89e23SAndrey Grodzovsky bool dc_add_plane_to_context(
123319f89e23SAndrey Grodzovsky 		const struct dc *dc,
12340971c40eSHarry Wentland 		struct dc_stream_state *stream,
123519f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1236608ac7bbSJerry Zuo 		struct dc_state *context)
12374562236bSHarry Wentland {
12384562236bSHarry Wentland 	int i;
123919f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
124019f89e23SAndrey Grodzovsky 	struct pipe_ctx *head_pipe, *tail_pipe, *free_pipe;
1241ab2541b6SAric Cyr 	struct dc_stream_status *stream_status = NULL;
12424562236bSHarry Wentland 
1243ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++)
12444fa086b9SLeo (Sunpeng) Li 		if (context->streams[i] == stream) {
1245ab2541b6SAric Cyr 			stream_status = &context->stream_status[i];
12464562236bSHarry Wentland 			break;
12474562236bSHarry Wentland 		}
1248ab2541b6SAric Cyr 	if (stream_status == NULL) {
124919f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to attach surface!\n");
125019f89e23SAndrey Grodzovsky 		return false;
125119f89e23SAndrey Grodzovsky 	}
125219f89e23SAndrey Grodzovsky 
125319f89e23SAndrey Grodzovsky 
125419f89e23SAndrey Grodzovsky 	if (stream_status->plane_count == MAX_SURFACE_NUM) {
125519f89e23SAndrey Grodzovsky 		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
125619f89e23SAndrey Grodzovsky 				plane_state, MAX_SURFACE_NUM);
125719f89e23SAndrey Grodzovsky 		return false;
125819f89e23SAndrey Grodzovsky 	}
125919f89e23SAndrey Grodzovsky 
126019f89e23SAndrey Grodzovsky 	head_pipe = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
126119f89e23SAndrey Grodzovsky 
126219f89e23SAndrey Grodzovsky 	if (!head_pipe) {
126319f89e23SAndrey Grodzovsky 		dm_error("Head pipe not found for stream_state %p !\n", stream);
12644562236bSHarry Wentland 		return false;
12654562236bSHarry Wentland 	}
12664562236bSHarry Wentland 
1267b1f6d01cSDmytro Laktyushkin 	/* retain new surface, but only once per stream */
1268b1f6d01cSDmytro Laktyushkin 	dc_plane_state_retain(plane_state);
1269b1f6d01cSDmytro Laktyushkin 
1270b1f6d01cSDmytro Laktyushkin 	while (head_pipe) {
1271b1f6d01cSDmytro Laktyushkin 		tail_pipe = resource_get_tail_pipe(&context->res_ctx, head_pipe);
127200737c59SEric Bernstein 		ASSERT(tail_pipe);
127300737c59SEric Bernstein 
1274b1f6d01cSDmytro Laktyushkin 		free_pipe = acquire_free_pipe_for_head(context, pool, head_pipe);
12754562236bSHarry Wentland 
1276b86a1aa3SBhawanpreet Lakha 	#if defined(CONFIG_DRM_AMD_DC_DCN)
12770f9a536fSDmytro Laktyushkin 		if (!free_pipe) {
12780f9a536fSDmytro Laktyushkin 			int pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
12790f9a536fSDmytro Laktyushkin 			if (pipe_idx >= 0)
12800f9a536fSDmytro Laktyushkin 				free_pipe = &context->res_ctx.pipe_ctx[pipe_idx];
12810f9a536fSDmytro Laktyushkin 		}
12820f9a536fSDmytro Laktyushkin 	#endif
1283b1f6d01cSDmytro Laktyushkin 		if (!free_pipe) {
1284b1f6d01cSDmytro Laktyushkin 			dc_plane_state_release(plane_state);
12854562236bSHarry Wentland 			return false;
1286b1f6d01cSDmytro Laktyushkin 		}
12874562236bSHarry Wentland 
12883be5262eSHarry Wentland 		free_pipe->plane_state = plane_state;
12894562236bSHarry Wentland 
129019f89e23SAndrey Grodzovsky 		if (head_pipe != free_pipe) {
12916b670fa9SHarry Wentland 			free_pipe->stream_res.tg = tail_pipe->stream_res.tg;
12929aef1a31SSivapiriyanKumarasamy 			free_pipe->stream_res.abm = tail_pipe->stream_res.abm;
1293a6a6cb34SHarry Wentland 			free_pipe->stream_res.opp = tail_pipe->stream_res.opp;
12948e9c4c8cSHarry Wentland 			free_pipe->stream_res.stream_enc = tail_pipe->stream_res.stream_enc;
1295afaacef4SHarry Wentland 			free_pipe->stream_res.audio = tail_pipe->stream_res.audio;
1296cfe4645eSDmytro Laktyushkin 			free_pipe->clock_source = tail_pipe->clock_source;
12974562236bSHarry Wentland 			free_pipe->top_pipe = tail_pipe;
12984562236bSHarry Wentland 			tail_pipe->bottom_pipe = free_pipe;
12994562236bSHarry Wentland 		}
1300b1f6d01cSDmytro Laktyushkin 		head_pipe = head_pipe->next_odm_pipe;
1301b1f6d01cSDmytro Laktyushkin 	}
13024562236bSHarry Wentland 	/* assign new surfaces*/
130319f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = plane_state;
13044562236bSHarry Wentland 
130519f89e23SAndrey Grodzovsky 	stream_status->plane_count++;
13064562236bSHarry Wentland 
13074562236bSHarry Wentland 	return true;
13084562236bSHarry Wentland }
13094562236bSHarry Wentland 
131019f89e23SAndrey Grodzovsky bool dc_remove_plane_from_context(
131119f89e23SAndrey Grodzovsky 		const struct dc *dc,
131219f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
131319f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1314608ac7bbSJerry Zuo 		struct dc_state *context)
131519f89e23SAndrey Grodzovsky {
131619f89e23SAndrey Grodzovsky 	int i;
131719f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
131819f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
131919f89e23SAndrey Grodzovsky 
132019f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
132119f89e23SAndrey Grodzovsky 		if (context->streams[i] == stream) {
132219f89e23SAndrey Grodzovsky 			stream_status = &context->stream_status[i];
132319f89e23SAndrey Grodzovsky 			break;
132419f89e23SAndrey Grodzovsky 		}
132519f89e23SAndrey Grodzovsky 
132619f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
132719f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to remove plane.\n");
132819f89e23SAndrey Grodzovsky 		return false;
132919f89e23SAndrey Grodzovsky 	}
133019f89e23SAndrey Grodzovsky 
133119f89e23SAndrey Grodzovsky 	/* release pipe for plane*/
133219f89e23SAndrey Grodzovsky 	for (i = pool->pipe_count - 1; i >= 0; i--) {
13336ffaa6fcSDmytro Laktyushkin 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
133419f89e23SAndrey Grodzovsky 
13356ffaa6fcSDmytro Laktyushkin 		if (pipe_ctx->plane_state == plane_state) {
133619f89e23SAndrey Grodzovsky 			if (pipe_ctx->top_pipe)
133719f89e23SAndrey Grodzovsky 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
133819f89e23SAndrey Grodzovsky 
133919f89e23SAndrey Grodzovsky 			/* Second condition is to avoid setting NULL to top pipe
134019f89e23SAndrey Grodzovsky 			 * of tail pipe making it look like head pipe in subsequent
134119f89e23SAndrey Grodzovsky 			 * deletes
134219f89e23SAndrey Grodzovsky 			 */
134319f89e23SAndrey Grodzovsky 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
134419f89e23SAndrey Grodzovsky 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
134519f89e23SAndrey Grodzovsky 
134619f89e23SAndrey Grodzovsky 			/*
134719f89e23SAndrey Grodzovsky 			 * For head pipe detach surfaces from pipe for tail
134819f89e23SAndrey Grodzovsky 			 * pipe just zero it out
134919f89e23SAndrey Grodzovsky 			 */
1350b1f6d01cSDmytro Laktyushkin 			if (!pipe_ctx->top_pipe)
135119f89e23SAndrey Grodzovsky 				pipe_ctx->plane_state = NULL;
1352b1f6d01cSDmytro Laktyushkin 			else
135319f89e23SAndrey Grodzovsky 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
135419f89e23SAndrey Grodzovsky 		}
135519f89e23SAndrey Grodzovsky 	}
135619f89e23SAndrey Grodzovsky 
135719f89e23SAndrey Grodzovsky 
135819f89e23SAndrey Grodzovsky 	for (i = 0; i < stream_status->plane_count; i++) {
135919f89e23SAndrey Grodzovsky 		if (stream_status->plane_states[i] == plane_state) {
136019f89e23SAndrey Grodzovsky 
136119f89e23SAndrey Grodzovsky 			dc_plane_state_release(stream_status->plane_states[i]);
136219f89e23SAndrey Grodzovsky 			break;
136319f89e23SAndrey Grodzovsky 		}
136419f89e23SAndrey Grodzovsky 	}
136519f89e23SAndrey Grodzovsky 
136619f89e23SAndrey Grodzovsky 	if (i == stream_status->plane_count) {
136719f89e23SAndrey Grodzovsky 		dm_error("Existing plane_state not found; failed to detach it!\n");
136819f89e23SAndrey Grodzovsky 		return false;
136919f89e23SAndrey Grodzovsky 	}
137019f89e23SAndrey Grodzovsky 
137119f89e23SAndrey Grodzovsky 	stream_status->plane_count--;
137219f89e23SAndrey Grodzovsky 
1373abb4986eSAndrew Jiang 	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
1374abb4986eSAndrew Jiang 	for (; i < stream_status->plane_count; i++)
137519f89e23SAndrey Grodzovsky 		stream_status->plane_states[i] = stream_status->plane_states[i + 1];
137619f89e23SAndrey Grodzovsky 
137719f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = NULL;
137819f89e23SAndrey Grodzovsky 
137919f89e23SAndrey Grodzovsky 	return true;
138019f89e23SAndrey Grodzovsky }
138119f89e23SAndrey Grodzovsky 
138219f89e23SAndrey Grodzovsky bool dc_rem_all_planes_for_stream(
138319f89e23SAndrey Grodzovsky 		const struct dc *dc,
138419f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
1385608ac7bbSJerry Zuo 		struct dc_state *context)
138619f89e23SAndrey Grodzovsky {
138719f89e23SAndrey Grodzovsky 	int i, old_plane_count;
138819f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
138919f89e23SAndrey Grodzovsky 	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
139019f89e23SAndrey Grodzovsky 
139119f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
139219f89e23SAndrey Grodzovsky 			if (context->streams[i] == stream) {
139319f89e23SAndrey Grodzovsky 				stream_status = &context->stream_status[i];
139419f89e23SAndrey Grodzovsky 				break;
139519f89e23SAndrey Grodzovsky 			}
139619f89e23SAndrey Grodzovsky 
139719f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
139819f89e23SAndrey Grodzovsky 		dm_error("Existing stream %p not found!\n", stream);
139919f89e23SAndrey Grodzovsky 		return false;
140019f89e23SAndrey Grodzovsky 	}
140119f89e23SAndrey Grodzovsky 
140219f89e23SAndrey Grodzovsky 	old_plane_count = stream_status->plane_count;
140319f89e23SAndrey Grodzovsky 
140419f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
140519f89e23SAndrey Grodzovsky 		del_planes[i] = stream_status->plane_states[i];
140619f89e23SAndrey Grodzovsky 
140719f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
140819f89e23SAndrey Grodzovsky 		if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
140919f89e23SAndrey Grodzovsky 			return false;
141019f89e23SAndrey Grodzovsky 
141119f89e23SAndrey Grodzovsky 	return true;
141219f89e23SAndrey Grodzovsky }
141319f89e23SAndrey Grodzovsky 
141419f89e23SAndrey Grodzovsky static bool add_all_planes_for_stream(
141519f89e23SAndrey Grodzovsky 		const struct dc *dc,
141619f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
141719f89e23SAndrey Grodzovsky 		const struct dc_validation_set set[],
141819f89e23SAndrey Grodzovsky 		int set_count,
1419608ac7bbSJerry Zuo 		struct dc_state *context)
142019f89e23SAndrey Grodzovsky {
142119f89e23SAndrey Grodzovsky 	int i, j;
142219f89e23SAndrey Grodzovsky 
142319f89e23SAndrey Grodzovsky 	for (i = 0; i < set_count; i++)
142419f89e23SAndrey Grodzovsky 		if (set[i].stream == stream)
142519f89e23SAndrey Grodzovsky 			break;
142619f89e23SAndrey Grodzovsky 
142719f89e23SAndrey Grodzovsky 	if (i == set_count) {
142819f89e23SAndrey Grodzovsky 		dm_error("Stream %p not found in set!\n", stream);
142919f89e23SAndrey Grodzovsky 		return false;
143019f89e23SAndrey Grodzovsky 	}
143119f89e23SAndrey Grodzovsky 
143219f89e23SAndrey Grodzovsky 	for (j = 0; j < set[i].plane_count; j++)
143319f89e23SAndrey Grodzovsky 		if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
143419f89e23SAndrey Grodzovsky 			return false;
143519f89e23SAndrey Grodzovsky 
143619f89e23SAndrey Grodzovsky 	return true;
143719f89e23SAndrey Grodzovsky }
143819f89e23SAndrey Grodzovsky 
143919f89e23SAndrey Grodzovsky bool dc_add_all_planes_for_stream(
144019f89e23SAndrey Grodzovsky 		const struct dc *dc,
144119f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
144219f89e23SAndrey Grodzovsky 		struct dc_plane_state * const *plane_states,
144319f89e23SAndrey Grodzovsky 		int plane_count,
1444608ac7bbSJerry Zuo 		struct dc_state *context)
144519f89e23SAndrey Grodzovsky {
144619f89e23SAndrey Grodzovsky 	struct dc_validation_set set;
144719f89e23SAndrey Grodzovsky 	int i;
144819f89e23SAndrey Grodzovsky 
144919f89e23SAndrey Grodzovsky 	set.stream = stream;
145019f89e23SAndrey Grodzovsky 	set.plane_count = plane_count;
145119f89e23SAndrey Grodzovsky 
145219f89e23SAndrey Grodzovsky 	for (i = 0; i < plane_count; i++)
145319f89e23SAndrey Grodzovsky 		set.plane_states[i] = plane_states[i];
145419f89e23SAndrey Grodzovsky 
145519f89e23SAndrey Grodzovsky 	return add_all_planes_for_stream(dc, stream, &set, 1, context);
145619f89e23SAndrey Grodzovsky }
145719f89e23SAndrey Grodzovsky 
145819f89e23SAndrey Grodzovsky 
14596b622181SJulian Parkin static bool is_hdr_static_meta_changed(struct dc_stream_state *cur_stream,
14606b622181SJulian Parkin 	struct dc_stream_state *new_stream)
14616b622181SJulian Parkin {
14626b622181SJulian Parkin 	if (cur_stream == NULL)
14636b622181SJulian Parkin 		return true;
14646b622181SJulian Parkin 
14656b622181SJulian Parkin 	if (memcmp(&cur_stream->hdr_static_metadata,
14666b622181SJulian Parkin 			&new_stream->hdr_static_metadata,
14676b622181SJulian Parkin 			sizeof(struct dc_info_packet)) != 0)
14686b622181SJulian Parkin 		return true;
14696b622181SJulian Parkin 
14706b622181SJulian Parkin 	return false;
14716b622181SJulian Parkin }
14724562236bSHarry Wentland 
14731336926fSAlvin lee static bool is_vsc_info_packet_changed(struct dc_stream_state *cur_stream,
14741336926fSAlvin lee 		struct dc_stream_state *new_stream)
14751336926fSAlvin lee {
14761336926fSAlvin lee 	if (cur_stream == NULL)
14771336926fSAlvin lee 		return true;
14781336926fSAlvin lee 
14791336926fSAlvin lee 	if (memcmp(&cur_stream->vsc_infopacket,
14801336926fSAlvin lee 			&new_stream->vsc_infopacket,
14811336926fSAlvin lee 			sizeof(struct dc_info_packet)) != 0)
14821336926fSAlvin lee 		return true;
14831336926fSAlvin lee 
14841336926fSAlvin lee 	return false;
14851336926fSAlvin lee }
14861336926fSAlvin lee 
14870971c40eSHarry Wentland static bool is_timing_changed(struct dc_stream_state *cur_stream,
14880971c40eSHarry Wentland 		struct dc_stream_state *new_stream)
14894562236bSHarry Wentland {
14904562236bSHarry Wentland 	if (cur_stream == NULL)
14914562236bSHarry Wentland 		return true;
14924562236bSHarry Wentland 
14934562236bSHarry Wentland 	/* If sink pointer changed, it means this is a hotplug, we should do
14944562236bSHarry Wentland 	 * full hw setting.
14954562236bSHarry Wentland 	 */
14964562236bSHarry Wentland 	if (cur_stream->sink != new_stream->sink)
14974562236bSHarry Wentland 		return true;
14984562236bSHarry Wentland 
14994562236bSHarry Wentland 	/* If output color space is changed, need to reprogram info frames */
15004fa086b9SLeo (Sunpeng) Li 	if (cur_stream->output_color_space != new_stream->output_color_space)
15014562236bSHarry Wentland 		return true;
15024562236bSHarry Wentland 
15034562236bSHarry Wentland 	return memcmp(
15044fa086b9SLeo (Sunpeng) Li 		&cur_stream->timing,
15054fa086b9SLeo (Sunpeng) Li 		&new_stream->timing,
15064562236bSHarry Wentland 		sizeof(struct dc_crtc_timing)) != 0;
15074562236bSHarry Wentland }
15084562236bSHarry Wentland 
15094562236bSHarry Wentland static bool are_stream_backends_same(
15100971c40eSHarry Wentland 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
15114562236bSHarry Wentland {
15124562236bSHarry Wentland 	if (stream_a == stream_b)
15134562236bSHarry Wentland 		return true;
15144562236bSHarry Wentland 
15154562236bSHarry Wentland 	if (stream_a == NULL || stream_b == NULL)
15164562236bSHarry Wentland 		return false;
15174562236bSHarry Wentland 
15184562236bSHarry Wentland 	if (is_timing_changed(stream_a, stream_b))
15194562236bSHarry Wentland 		return false;
15204562236bSHarry Wentland 
15216b622181SJulian Parkin 	if (is_hdr_static_meta_changed(stream_a, stream_b))
15226b622181SJulian Parkin 		return false;
15236b622181SJulian Parkin 
15241e7e86c4SSamson Tam 	if (stream_a->dpms_off != stream_b->dpms_off)
15251e7e86c4SSamson Tam 		return false;
15261e7e86c4SSamson Tam 
15271336926fSAlvin lee 	if (is_vsc_info_packet_changed(stream_a, stream_b))
15281336926fSAlvin lee 		return false;
15291336926fSAlvin lee 
15304562236bSHarry Wentland 	return true;
15314562236bSHarry Wentland }
15324562236bSHarry Wentland 
15332119aa17SDavid Francis /**
15342119aa17SDavid Francis  * dc_is_stream_unchanged() - Compare two stream states for equivalence.
15352119aa17SDavid Francis  *
15362119aa17SDavid Francis  * Checks if there a difference between the two states
15372119aa17SDavid Francis  * that would require a mode change.
15382119aa17SDavid Francis  *
15392119aa17SDavid Francis  * Does not compare cursor position or attributes.
15402119aa17SDavid Francis  */
1541d54d29dbSBhawanpreet Lakha bool dc_is_stream_unchanged(
15420971c40eSHarry Wentland 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
15434562236bSHarry Wentland {
15444562236bSHarry Wentland 
15454562236bSHarry Wentland 	if (!are_stream_backends_same(old_stream, stream))
15464562236bSHarry Wentland 		return false;
15474562236bSHarry Wentland 
15480460f9abSJun Lei 	if (old_stream->ignore_msa_timing_param != stream->ignore_msa_timing_param)
15490460f9abSJun Lei 		return false;
15500460f9abSJun Lei 
15514562236bSHarry Wentland 	return true;
15524562236bSHarry Wentland }
15534562236bSHarry Wentland 
15542119aa17SDavid Francis /**
15552119aa17SDavid Francis  * dc_is_stream_scaling_unchanged() - Compare scaling rectangles of two streams.
15562119aa17SDavid Francis  */
15579a5d9c48SLeo (Sunpeng) Li bool dc_is_stream_scaling_unchanged(
15589a5d9c48SLeo (Sunpeng) Li 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
15599a5d9c48SLeo (Sunpeng) Li {
15609a5d9c48SLeo (Sunpeng) Li 	if (old_stream == stream)
15619a5d9c48SLeo (Sunpeng) Li 		return true;
15629a5d9c48SLeo (Sunpeng) Li 
15639a5d9c48SLeo (Sunpeng) Li 	if (old_stream == NULL || stream == NULL)
15649a5d9c48SLeo (Sunpeng) Li 		return false;
15659a5d9c48SLeo (Sunpeng) Li 
15669a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->src,
15679a5d9c48SLeo (Sunpeng) Li 			&stream->src,
15689a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
15699a5d9c48SLeo (Sunpeng) Li 		return false;
15709a5d9c48SLeo (Sunpeng) Li 
15719a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->dst,
15729a5d9c48SLeo (Sunpeng) Li 			&stream->dst,
15739a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
15749a5d9c48SLeo (Sunpeng) Li 		return false;
15759a5d9c48SLeo (Sunpeng) Li 
15769a5d9c48SLeo (Sunpeng) Li 	return true;
15779a5d9c48SLeo (Sunpeng) Li }
15789a5d9c48SLeo (Sunpeng) Li 
15791dc90497SAndrey Grodzovsky static void update_stream_engine_usage(
15804562236bSHarry Wentland 		struct resource_context *res_ctx,
1581a2b8659dSTony Cheng 		const struct resource_pool *pool,
15821dc90497SAndrey Grodzovsky 		struct stream_encoder *stream_enc,
15831dc90497SAndrey Grodzovsky 		bool acquired)
15844562236bSHarry Wentland {
15854562236bSHarry Wentland 	int i;
15864562236bSHarry Wentland 
1587a2b8659dSTony Cheng 	for (i = 0; i < pool->stream_enc_count; i++) {
1588a2b8659dSTony Cheng 		if (pool->stream_enc[i] == stream_enc)
15891dc90497SAndrey Grodzovsky 			res_ctx->is_stream_enc_acquired[i] = acquired;
15904562236bSHarry Wentland 	}
15914562236bSHarry Wentland }
15924562236bSHarry Wentland 
15934562236bSHarry Wentland /* TODO: release audio object */
15944176664bSCharlene Liu void update_audio_usage(
15954562236bSHarry Wentland 		struct resource_context *res_ctx,
1596a2b8659dSTony Cheng 		const struct resource_pool *pool,
15971dc90497SAndrey Grodzovsky 		struct audio *audio,
15981dc90497SAndrey Grodzovsky 		bool acquired)
15994562236bSHarry Wentland {
16004562236bSHarry Wentland 	int i;
1601a2b8659dSTony Cheng 	for (i = 0; i < pool->audio_count; i++) {
1602a2b8659dSTony Cheng 		if (pool->audios[i] == audio)
16031dc90497SAndrey Grodzovsky 			res_ctx->is_audio_acquired[i] = acquired;
16044562236bSHarry Wentland 	}
16054562236bSHarry Wentland }
16064562236bSHarry Wentland 
16074562236bSHarry Wentland static int acquire_first_free_pipe(
16084562236bSHarry Wentland 		struct resource_context *res_ctx,
1609a2b8659dSTony Cheng 		const struct resource_pool *pool,
16100971c40eSHarry Wentland 		struct dc_stream_state *stream)
16114562236bSHarry Wentland {
16124562236bSHarry Wentland 	int i;
16134562236bSHarry Wentland 
1614a2b8659dSTony Cheng 	for (i = 0; i < pool->pipe_count; i++) {
16154562236bSHarry Wentland 		if (!res_ctx->pipe_ctx[i].stream) {
16164562236bSHarry Wentland 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
16174562236bSHarry Wentland 
16186b670fa9SHarry Wentland 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
161986a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi = pool->mis[i];
16208feabd03SYue Hin Lau 			pipe_ctx->plane_res.hubp = pool->hubps[i];
162186a66c4eSHarry Wentland 			pipe_ctx->plane_res.ipp = pool->ipps[i];
162286a66c4eSHarry Wentland 			pipe_ctx->plane_res.xfm = pool->transforms[i];
1623d94585a0SYue Hin Lau 			pipe_ctx->plane_res.dpp = pool->dpps[i];
1624a6a6cb34SHarry Wentland 			pipe_ctx->stream_res.opp = pool->opps[i];
1625bc373a89SRoman Li 			if (pool->dpps[i])
1626e07f541fSYongqiang Sun 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
16274562236bSHarry Wentland 			pipe_ctx->pipe_idx = i;
16284562236bSHarry Wentland 
1629ff5ef992SAlex Deucher 
16304562236bSHarry Wentland 			pipe_ctx->stream = stream;
16314562236bSHarry Wentland 			return i;
16324562236bSHarry Wentland 		}
16334562236bSHarry Wentland 	}
16344562236bSHarry Wentland 	return -1;
16354562236bSHarry Wentland }
16364562236bSHarry Wentland 
1637a2b8659dSTony Cheng static struct audio *find_first_free_audio(
1638a2b8659dSTony Cheng 		struct resource_context *res_ctx,
1639cfb071f7SCharlene Liu 		const struct resource_pool *pool,
1640f24b0522SPaul Hsieh 		enum engine_id id,
1641f24b0522SPaul Hsieh 		enum dce_version dc_version)
16424562236bSHarry Wentland {
1643b5a41620SCharlene Liu 	int i, available_audio_count;
1644b5a41620SCharlene Liu 
1645b5a41620SCharlene Liu 	available_audio_count = pool->audio_count;
1646b5a41620SCharlene Liu 
1647b5a41620SCharlene Liu 	for (i = 0; i < available_audio_count; i++) {
16484176664bSCharlene Liu 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
1649cfb071f7SCharlene Liu 			/*we have enough audio endpoint, find the matching inst*/
1650cfb071f7SCharlene Liu 			if (id != i)
1651cfb071f7SCharlene Liu 				continue;
1652a2b8659dSTony Cheng 			return pool->audios[i];
16534562236bSHarry Wentland 		}
16544562236bSHarry Wentland 	}
16555feb9f07STai Man 
16565feb9f07STai Man 	/* use engine id to find free audio */
1657b5a41620SCharlene Liu 	if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
16585feb9f07STai Man 		return pool->audios[id];
16595feb9f07STai Man 	}
166066bfd4fdSCharlene Liu 	/*not found the matching one, first come first serve*/
1661b5a41620SCharlene Liu 	for (i = 0; i < available_audio_count; i++) {
16624176664bSCharlene Liu 		if (res_ctx->is_audio_acquired[i] == false) {
16634176664bSCharlene Liu 			return pool->audios[i];
16644176664bSCharlene Liu 		}
16654176664bSCharlene Liu 	}
16664562236bSHarry Wentland 	return 0;
16674562236bSHarry Wentland }
16684562236bSHarry Wentland 
16694562236bSHarry Wentland bool resource_is_stream_unchanged(
1670608ac7bbSJerry Zuo 	struct dc_state *old_context, struct dc_stream_state *stream)
16714562236bSHarry Wentland {
1672ab2541b6SAric Cyr 	int i;
16734562236bSHarry Wentland 
1674ab2541b6SAric Cyr 	for (i = 0; i < old_context->stream_count; i++) {
16750971c40eSHarry Wentland 		struct dc_stream_state *old_stream = old_context->streams[i];
16764562236bSHarry Wentland 
16774562236bSHarry Wentland 		if (are_stream_backends_same(old_stream, stream))
16784562236bSHarry Wentland 				return true;
16794562236bSHarry Wentland 	}
16804562236bSHarry Wentland 
16814562236bSHarry Wentland 	return false;
16824562236bSHarry Wentland }
16834562236bSHarry Wentland 
16842119aa17SDavid Francis /**
16852119aa17SDavid Francis  * dc_add_stream_to_ctx() - Add a new dc_stream_state to a dc_state.
16862119aa17SDavid Francis  */
168713ab1b44SYongqiang Sun enum dc_status dc_add_stream_to_ctx(
16881dc90497SAndrey Grodzovsky 		struct dc *dc,
1689608ac7bbSJerry Zuo 		struct dc_state *new_ctx,
16901dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
16911dc90497SAndrey Grodzovsky {
16921dc90497SAndrey Grodzovsky 	enum dc_status res;
1693eb9714a2SWenjing Liu 	DC_LOGGER_INIT(dc->ctx->logger);
16941dc90497SAndrey Grodzovsky 
1695ece4147fSKen Chalmers 	if (new_ctx->stream_count >= dc->res_pool->timing_generator_count) {
1696eb9714a2SWenjing Liu 		DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
16971dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
16981dc90497SAndrey Grodzovsky 	}
16991dc90497SAndrey Grodzovsky 
17001dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = stream;
17011dc90497SAndrey Grodzovsky 	dc_stream_retain(stream);
17021dc90497SAndrey Grodzovsky 	new_ctx->stream_count++;
17031dc90497SAndrey Grodzovsky 
17041dc90497SAndrey Grodzovsky 	res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
17051dc90497SAndrey Grodzovsky 	if (res != DC_OK)
1706eb9714a2SWenjing Liu 		DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);
17071dc90497SAndrey Grodzovsky 
170813ab1b44SYongqiang Sun 	return res;
17091dc90497SAndrey Grodzovsky }
17101dc90497SAndrey Grodzovsky 
17112119aa17SDavid Francis /**
17122119aa17SDavid Francis  * dc_remove_stream_from_ctx() - Remove a stream from a dc_state.
17132119aa17SDavid Francis  */
171462c933f9SYongqiang Sun enum dc_status dc_remove_stream_from_ctx(
17151dc90497SAndrey Grodzovsky 			struct dc *dc,
1716608ac7bbSJerry Zuo 			struct dc_state *new_ctx,
17171dc90497SAndrey Grodzovsky 			struct dc_stream_state *stream)
17181dc90497SAndrey Grodzovsky {
171919f89e23SAndrey Grodzovsky 	int i;
17201dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
172122498036SDmytro Laktyushkin 	struct pipe_ctx *del_pipe = resource_get_head_pipe_for_stream(&new_ctx->res_ctx, stream);
172222498036SDmytro Laktyushkin 	struct pipe_ctx *odm_pipe;
172322498036SDmytro Laktyushkin 
172422498036SDmytro Laktyushkin 	if (!del_pipe) {
172522498036SDmytro Laktyushkin 		DC_ERROR("Pipe not found for stream %p !\n", stream);
172622498036SDmytro Laktyushkin 		return DC_ERROR_UNEXPECTED;
172722498036SDmytro Laktyushkin 	}
172822498036SDmytro Laktyushkin 
172922498036SDmytro Laktyushkin 	odm_pipe = del_pipe->next_odm_pipe;
17301dc90497SAndrey Grodzovsky 
173119f89e23SAndrey Grodzovsky 	/* Release primary pipe */
173219f89e23SAndrey Grodzovsky 	ASSERT(del_pipe->stream_res.stream_enc);
17331dc90497SAndrey Grodzovsky 	update_stream_engine_usage(
17341dc90497SAndrey Grodzovsky 			&new_ctx->res_ctx,
17351dc90497SAndrey Grodzovsky 				dc->res_pool,
17361dc90497SAndrey Grodzovsky 			del_pipe->stream_res.stream_enc,
17371dc90497SAndrey Grodzovsky 			false);
17381dc90497SAndrey Grodzovsky 
17391dc90497SAndrey Grodzovsky 	if (del_pipe->stream_res.audio)
17401dc90497SAndrey Grodzovsky 		update_audio_usage(
17411dc90497SAndrey Grodzovsky 			&new_ctx->res_ctx,
17421dc90497SAndrey Grodzovsky 			dc->res_pool,
17431dc90497SAndrey Grodzovsky 			del_pipe->stream_res.audio,
17441dc90497SAndrey Grodzovsky 			false);
17451dc90497SAndrey Grodzovsky 
17469d0dcecdSHarry Wentland 	resource_unreference_clock_source(&new_ctx->res_ctx,
17479d0dcecdSHarry Wentland 					  dc->res_pool,
17489d0dcecdSHarry Wentland 					  del_pipe->clock_source);
17499d0dcecdSHarry Wentland 
1750e56ae556SNikola Cornij 	if (dc->res_pool->funcs->remove_stream_from_ctx)
1751e56ae556SNikola Cornij 		dc->res_pool->funcs->remove_stream_from_ctx(dc, new_ctx, stream);
1752e56ae556SNikola Cornij 
175322498036SDmytro Laktyushkin 	while (odm_pipe) {
175422498036SDmytro Laktyushkin 		struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
175522498036SDmytro Laktyushkin 
175622498036SDmytro Laktyushkin 		memset(odm_pipe, 0, sizeof(*odm_pipe));
175722498036SDmytro Laktyushkin 		odm_pipe = next_odm_pipe;
175822498036SDmytro Laktyushkin 	}
17591dc90497SAndrey Grodzovsky 	memset(del_pipe, 0, sizeof(*del_pipe));
17606ffaa6fcSDmytro Laktyushkin 
17611dc90497SAndrey Grodzovsky 	for (i = 0; i < new_ctx->stream_count; i++)
17621dc90497SAndrey Grodzovsky 		if (new_ctx->streams[i] == stream)
17631dc90497SAndrey Grodzovsky 			break;
17641dc90497SAndrey Grodzovsky 
17651dc90497SAndrey Grodzovsky 	if (new_ctx->streams[i] != stream) {
17661dc90497SAndrey Grodzovsky 		DC_ERROR("Context doesn't have stream %p !\n", stream);
17671dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
17681dc90497SAndrey Grodzovsky 	}
17691dc90497SAndrey Grodzovsky 
17701dc90497SAndrey Grodzovsky 	dc_stream_release(new_ctx->streams[i]);
17711dc90497SAndrey Grodzovsky 	new_ctx->stream_count--;
17721dc90497SAndrey Grodzovsky 
17731dc90497SAndrey Grodzovsky 	/* Trim back arrays */
17741dc90497SAndrey Grodzovsky 	for (; i < new_ctx->stream_count; i++) {
17751dc90497SAndrey Grodzovsky 		new_ctx->streams[i] = new_ctx->streams[i + 1];
17761dc90497SAndrey Grodzovsky 		new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
17771dc90497SAndrey Grodzovsky 	}
17781dc90497SAndrey Grodzovsky 
17791dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = NULL;
17801dc90497SAndrey Grodzovsky 	memset(
17811dc90497SAndrey Grodzovsky 			&new_ctx->stream_status[new_ctx->stream_count],
17821dc90497SAndrey Grodzovsky 			0,
17831dc90497SAndrey Grodzovsky 			sizeof(new_ctx->stream_status[0]));
17841dc90497SAndrey Grodzovsky 
17851dc90497SAndrey Grodzovsky 	return DC_OK;
17861dc90497SAndrey Grodzovsky }
17871dc90497SAndrey Grodzovsky 
17880971c40eSHarry Wentland static struct dc_stream_state *find_pll_sharable_stream(
17890971c40eSHarry Wentland 		struct dc_stream_state *stream_needs_pll,
1790608ac7bbSJerry Zuo 		struct dc_state *context)
17914562236bSHarry Wentland {
1792ab2541b6SAric Cyr 	int i;
17934562236bSHarry Wentland 
1794ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
17950971c40eSHarry Wentland 		struct dc_stream_state *stream_has_pll = context->streams[i];
17964562236bSHarry Wentland 
17974562236bSHarry Wentland 		/* We are looking for non dp, non virtual stream */
17984562236bSHarry Wentland 		if (resource_are_streams_timing_synchronizable(
17994562236bSHarry Wentland 			stream_needs_pll, stream_has_pll)
18004562236bSHarry Wentland 			&& !dc_is_dp_signal(stream_has_pll->signal)
1801ceb3dbb4SJun Lei 			&& stream_has_pll->link->connector_signal
18024562236bSHarry Wentland 			!= SIGNAL_TYPE_VIRTUAL)
18034562236bSHarry Wentland 			return stream_has_pll;
1804ab2541b6SAric Cyr 
18054562236bSHarry Wentland 	}
18064562236bSHarry Wentland 
18074562236bSHarry Wentland 	return NULL;
18084562236bSHarry Wentland }
18094562236bSHarry Wentland 
18104562236bSHarry Wentland static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
18114562236bSHarry Wentland {
1812380604e2SKen Chalmers 	uint32_t pix_clk = timing->pix_clk_100hz;
18134562236bSHarry Wentland 	uint32_t normalized_pix_clk = pix_clk;
18144562236bSHarry Wentland 
18154562236bSHarry Wentland 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
18164562236bSHarry Wentland 		pix_clk /= 2;
1817cc4d99b8SCharlene Liu 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
18184562236bSHarry Wentland 		switch (timing->display_color_depth) {
18198897810aSJulian Parkin 		case COLOR_DEPTH_666:
18204562236bSHarry Wentland 		case COLOR_DEPTH_888:
18214562236bSHarry Wentland 			normalized_pix_clk = pix_clk;
18224562236bSHarry Wentland 			break;
18234562236bSHarry Wentland 		case COLOR_DEPTH_101010:
18244562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 30) / 24;
18254562236bSHarry Wentland 			break;
18264562236bSHarry Wentland 		case COLOR_DEPTH_121212:
18274562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 36) / 24;
18284562236bSHarry Wentland 		break;
18294562236bSHarry Wentland 		case COLOR_DEPTH_161616:
18304562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 48) / 24;
18314562236bSHarry Wentland 		break;
18324562236bSHarry Wentland 		default:
18334562236bSHarry Wentland 			ASSERT(0);
18344562236bSHarry Wentland 		break;
18354562236bSHarry Wentland 		}
1836cc4d99b8SCharlene Liu 	}
18374562236bSHarry Wentland 	return normalized_pix_clk;
18384562236bSHarry Wentland }
18394562236bSHarry Wentland 
18400971c40eSHarry Wentland static void calculate_phy_pix_clks(struct dc_stream_state *stream)
18414562236bSHarry Wentland {
18424562236bSHarry Wentland 	/* update actual pixel clock on all streams */
18434562236bSHarry Wentland 	if (dc_is_hdmi_signal(stream->signal))
18444562236bSHarry Wentland 		stream->phy_pix_clk = get_norm_pix_clk(
1845380604e2SKen Chalmers 			&stream->timing) / 10;
18464562236bSHarry Wentland 	else
18474562236bSHarry Wentland 		stream->phy_pix_clk =
1848380604e2SKen Chalmers 			stream->timing.pix_clk_100hz / 10;
184939c03e00SCharlene Liu 
185039c03e00SCharlene Liu 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
185139c03e00SCharlene Liu 		stream->phy_pix_clk *= 2;
18524562236bSHarry Wentland }
18534562236bSHarry Wentland 
1854d2d7885fSAnthony Koo static int acquire_resource_from_hw_enabled_state(
1855d2d7885fSAnthony Koo 		struct resource_context *res_ctx,
1856d2d7885fSAnthony Koo 		const struct resource_pool *pool,
1857d2d7885fSAnthony Koo 		struct dc_stream_state *stream)
1858d2d7885fSAnthony Koo {
1859d2d7885fSAnthony Koo 	struct dc_link *link = stream->link;
186008b66279SMartin Leung 	unsigned int i, inst, tg_inst = 0;
1861d2d7885fSAnthony Koo 
1862d2d7885fSAnthony Koo 	/* Check for enabled DIG to identify enabled display */
1863d2d7885fSAnthony Koo 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1864d2d7885fSAnthony Koo 		return -1;
1865d2d7885fSAnthony Koo 
18665ec43edaSMartin Leung 	inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1867d2d7885fSAnthony Koo 
18687f7652eeSMartin Leung 	if (inst == ENGINE_ID_UNKNOWN)
18697f7652eeSMartin Leung 		return false;
1870d2d7885fSAnthony Koo 
18717f7652eeSMartin Leung 	for (i = 0; i < pool->stream_enc_count; i++) {
18727f7652eeSMartin Leung 		if (pool->stream_enc[i]->id == inst) {
18737f7652eeSMartin Leung 			tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
18747f7652eeSMartin Leung 				pool->stream_enc[i]);
18757f7652eeSMartin Leung 			break;
18767f7652eeSMartin Leung 		}
18777f7652eeSMartin Leung 	}
1878d2d7885fSAnthony Koo 
18797f7652eeSMartin Leung 	// tg_inst not found
18807f7652eeSMartin Leung 	if (i == pool->stream_enc_count)
18817f7652eeSMartin Leung 		return false;
18825ec43edaSMartin Leung 
18835ec43edaSMartin Leung 	if (tg_inst >= pool->timing_generator_count)
18845ec43edaSMartin Leung 		return false;
18855ec43edaSMartin Leung 
18865ec43edaSMartin Leung 	if (!res_ctx->pipe_ctx[tg_inst].stream) {
18875ec43edaSMartin Leung 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
18885ec43edaSMartin Leung 
18895ec43edaSMartin Leung 		pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
18905ec43edaSMartin Leung 		pipe_ctx->plane_res.mi = pool->mis[tg_inst];
18915ec43edaSMartin Leung 		pipe_ctx->plane_res.hubp = pool->hubps[tg_inst];
18925ec43edaSMartin Leung 		pipe_ctx->plane_res.ipp = pool->ipps[tg_inst];
18935ec43edaSMartin Leung 		pipe_ctx->plane_res.xfm = pool->transforms[tg_inst];
18945ec43edaSMartin Leung 		pipe_ctx->plane_res.dpp = pool->dpps[tg_inst];
18955ec43edaSMartin Leung 		pipe_ctx->stream_res.opp = pool->opps[tg_inst];
18965ec43edaSMartin Leung 
18975ec43edaSMartin Leung 		if (pool->dpps[tg_inst])
18985ec43edaSMartin Leung 			pipe_ctx->plane_res.mpcc_inst = pool->dpps[tg_inst]->inst;
18995ec43edaSMartin Leung 		pipe_ctx->pipe_idx = tg_inst;
1900d2d7885fSAnthony Koo 
1901d2d7885fSAnthony Koo 		pipe_ctx->stream = stream;
19025ec43edaSMartin Leung 		return tg_inst;
1903d2d7885fSAnthony Koo 	}
1904d2d7885fSAnthony Koo 
1905d2d7885fSAnthony Koo 	return -1;
1906d2d7885fSAnthony Koo }
1907d2d7885fSAnthony Koo 
19084562236bSHarry Wentland enum dc_status resource_map_pool_resources(
1909fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
1910608ac7bbSJerry Zuo 		struct dc_state *context,
19111dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
19124562236bSHarry Wentland {
1913a2b8659dSTony Cheng 	const struct resource_pool *pool = dc->res_pool;
19141dc90497SAndrey Grodzovsky 	int i;
19151dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
19161dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = NULL;
19171dc90497SAndrey Grodzovsky 	int pipe_idx = -1;
191846570f09SAnthony Koo 	struct dc_bios *dcb = dc->ctx->dc_bios;
19194562236bSHarry Wentland 
19201dc90497SAndrey Grodzovsky 	/* TODO Check if this is needed */
19211dc90497SAndrey Grodzovsky 	/*if (!resource_is_stream_unchanged(old_context, stream)) {
1922430ef426SDmytro Laktyushkin 			if (stream != NULL && old_context->streams[i] != NULL) {
19234b679bc3SCharlene Liu 				stream->bit_depth_params =
1924430ef426SDmytro Laktyushkin 						old_context->streams[i]->bit_depth_params;
1925430ef426SDmytro Laktyushkin 				stream->clamping = old_context->streams[i]->clamping;
19264562236bSHarry Wentland 				continue;
19274b679bc3SCharlene Liu 			}
19284b679bc3SCharlene Liu 		}
19294562236bSHarry Wentland 	*/
19304562236bSHarry Wentland 
193108e1c28dSYogesh Mohan Marimuthu 	calculate_phy_pix_clks(stream);
193208e1c28dSYogesh Mohan Marimuthu 
193346570f09SAnthony Koo 	/* TODO: Check Linux */
193446570f09SAnthony Koo 	if (dc->config.allow_seamless_boot_optimization &&
193546570f09SAnthony Koo 			!dcb->funcs->is_accelerated_mode(dcb)) {
193646570f09SAnthony Koo 		if (dc_validate_seamless_boot_timing(dc, stream->sink, &stream->timing))
193746570f09SAnthony Koo 			stream->apply_seamless_boot_optimization = true;
193846570f09SAnthony Koo 	}
193946570f09SAnthony Koo 
1940d2d7885fSAnthony Koo 	if (stream->apply_seamless_boot_optimization)
1941d2d7885fSAnthony Koo 		pipe_idx = acquire_resource_from_hw_enabled_state(
1942d2d7885fSAnthony Koo 				&context->res_ctx,
1943d2d7885fSAnthony Koo 				pool,
1944d2d7885fSAnthony Koo 				stream);
1945d2d7885fSAnthony Koo 
1946d2d7885fSAnthony Koo 	if (pipe_idx < 0)
19474562236bSHarry Wentland 		/* acquire new resources */
19485d11e9fcSDmytro Laktyushkin 		pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
19491dc90497SAndrey Grodzovsky 
1950b86a1aa3SBhawanpreet Lakha #ifdef CONFIG_DRM_AMD_DC_DCN
19515d11e9fcSDmytro Laktyushkin 	if (pipe_idx < 0)
195213ab1b44SYongqiang Sun 		pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
195394c6d735SHarry Wentland #endif
195413ab1b44SYongqiang Sun 
1955c5b38aecSDmytro Laktyushkin 	if (pipe_idx < 0 || context->res_ctx.pipe_ctx[pipe_idx].stream_res.tg == NULL)
19564562236bSHarry Wentland 		return DC_NO_CONTROLLER_RESOURCE;
19574562236bSHarry Wentland 
19584562236bSHarry Wentland 	pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
19594562236bSHarry Wentland 
19608e9c4c8cSHarry Wentland 	pipe_ctx->stream_res.stream_enc =
196178cc70b1SWesley Chalmers 		dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
1962a2b8659dSTony Cheng 			&context->res_ctx, pool, stream);
19634562236bSHarry Wentland 
19648e9c4c8cSHarry Wentland 	if (!pipe_ctx->stream_res.stream_enc)
196538684e46SEric Bernstein 		return DC_NO_STREAM_ENC_RESOURCE;
19664562236bSHarry Wentland 
19671dc90497SAndrey Grodzovsky 	update_stream_engine_usage(
1968a2b8659dSTony Cheng 		&context->res_ctx, pool,
19691dc90497SAndrey Grodzovsky 		pipe_ctx->stream_res.stream_enc,
19701dc90497SAndrey Grodzovsky 		true);
19714562236bSHarry Wentland 
19724562236bSHarry Wentland 	/* TODO: Add check if ASIC support and EDID audio */
1973ceb3dbb4SJun Lei 	if (!stream->converter_disable_audio &&
19744562236bSHarry Wentland 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
1975ce08aad3SAlvin Lee 	    stream->audio_info.mode_count && stream->audio_info.flags.all) {
1976afaacef4SHarry Wentland 		pipe_ctx->stream_res.audio = find_first_free_audio(
1977f24b0522SPaul Hsieh 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
19784562236bSHarry Wentland 
19794562236bSHarry Wentland 		/*
19804562236bSHarry Wentland 		 * Audio assigned in order first come first get.
19814562236bSHarry Wentland 		 * There are asics which has number of audio
19824562236bSHarry Wentland 		 * resources less then number of pipes
19834562236bSHarry Wentland 		 */
1984afaacef4SHarry Wentland 		if (pipe_ctx->stream_res.audio)
19851dc90497SAndrey Grodzovsky 			update_audio_usage(&context->res_ctx, pool,
19861dc90497SAndrey Grodzovsky 					   pipe_ctx->stream_res.audio, true);
19874562236bSHarry Wentland 	}
19884562236bSHarry Wentland 
19899aef1a31SSivapiriyanKumarasamy 	/* Add ABM to the resource if on EDP */
19909aef1a31SSivapiriyanKumarasamy 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal))
19919aef1a31SSivapiriyanKumarasamy 		pipe_ctx->stream_res.abm = pool->abm;
19929aef1a31SSivapiriyanKumarasamy 
19931dc90497SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
19941dc90497SAndrey Grodzovsky 		if (context->streams[i] == stream) {
19956b670fa9SHarry Wentland 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
19960f0bdca5SWenjing Liu 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->id;
19975fdb7c4cSNicholas Kazlauskas 			context->stream_status[i].audio_inst =
19985fdb7c4cSNicholas Kazlauskas 				pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
19995fdb7c4cSNicholas Kazlauskas 
20001dc90497SAndrey Grodzovsky 			return DC_OK;
20014562236bSHarry Wentland 		}
20024562236bSHarry Wentland 
20031dc90497SAndrey Grodzovsky 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
20041dc90497SAndrey Grodzovsky 	return DC_ERROR_UNEXPECTED;
20054562236bSHarry Wentland }
20064562236bSHarry Wentland 
20072119aa17SDavid Francis /**
20082119aa17SDavid Francis  * dc_resource_state_copy_construct_current() - Creates a new dc_state from existing state
20092119aa17SDavid Francis  * Is a shallow copy.  Increments refcounts on existing streams and planes.
20102119aa17SDavid Francis  * @dc: copy out of dc->current_state
20112119aa17SDavid Francis  * @dst_ctx: copy into this
20122119aa17SDavid Francis  */
2013f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct_current(
20141dc90497SAndrey Grodzovsky 		const struct dc *dc,
2015608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
20161dc90497SAndrey Grodzovsky {
2017f36cc577SBhawanpreet Lakha 	dc_resource_state_copy_construct(dc->current_state, dst_ctx);
20181dc90497SAndrey Grodzovsky }
20191dc90497SAndrey Grodzovsky 
2020ab8db3e1SAndrey Grodzovsky 
2021ab8db3e1SAndrey Grodzovsky void dc_resource_state_construct(
2022ab8db3e1SAndrey Grodzovsky 		const struct dc *dc,
2023ab8db3e1SAndrey Grodzovsky 		struct dc_state *dst_ctx)
2024ab8db3e1SAndrey Grodzovsky {
2025dc88b4a6SEric Yang 	dst_ctx->clk_mgr = dc->clk_mgr;
2026ab8db3e1SAndrey Grodzovsky }
2027ab8db3e1SAndrey Grodzovsky 
20282119aa17SDavid Francis /**
20292119aa17SDavid Francis  * dc_validate_global_state() - Determine if HW can support a given state
20302119aa17SDavid Francis  * Checks HW resource availability and bandwidth requirement.
20312119aa17SDavid Francis  * @dc: dc struct for this driver
20322119aa17SDavid Francis  * @new_ctx: state to be validated
2033afcd526bSJoshua Aberback  * @fast_validate: set to true if only yes/no to support matters
20342119aa17SDavid Francis  *
20352119aa17SDavid Francis  * Return: DC_OK if the result can be programmed.  Otherwise, an error code.
20362119aa17SDavid Francis  */
2037e750d56dSYongqiang Sun enum dc_status dc_validate_global_state(
20381dc90497SAndrey Grodzovsky 		struct dc *dc,
2039afcd526bSJoshua Aberback 		struct dc_state *new_ctx,
2040afcd526bSJoshua Aberback 		bool fast_validate)
20411dc90497SAndrey Grodzovsky {
20421dc90497SAndrey Grodzovsky 	enum dc_status result = DC_ERROR_UNEXPECTED;
20431dc90497SAndrey Grodzovsky 	int i, j;
20441dc90497SAndrey Grodzovsky 
2045e41ab030SHarry Wentland 	if (!new_ctx)
2046e41ab030SHarry Wentland 		return DC_ERROR_UNEXPECTED;
2047e41ab030SHarry Wentland 
2048d596e5d0SYongqiang Sun 	if (dc->res_pool->funcs->validate_global) {
2049d596e5d0SYongqiang Sun 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
2050d596e5d0SYongqiang Sun 		if (result != DC_OK)
2051d596e5d0SYongqiang Sun 			return result;
2052d596e5d0SYongqiang Sun 	}
20531dc90497SAndrey Grodzovsky 
2054e41ab030SHarry Wentland 	for (i = 0; i < new_ctx->stream_count; i++) {
20551dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream = new_ctx->streams[i];
20561dc90497SAndrey Grodzovsky 
20571dc90497SAndrey Grodzovsky 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
20581dc90497SAndrey Grodzovsky 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
20591dc90497SAndrey Grodzovsky 
20601dc90497SAndrey Grodzovsky 			if (pipe_ctx->stream != stream)
20611dc90497SAndrey Grodzovsky 				continue;
20621dc90497SAndrey Grodzovsky 
206374eac5f3SSu Sung Chung 			if (dc->res_pool->funcs->get_default_swizzle_mode &&
206474eac5f3SSu Sung Chung 					pipe_ctx->plane_state &&
206574eac5f3SSu Sung Chung 					pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
206674eac5f3SSu Sung Chung 				result = dc->res_pool->funcs->get_default_swizzle_mode(pipe_ctx->plane_state);
206774eac5f3SSu Sung Chung 				if (result != DC_OK)
206874eac5f3SSu Sung Chung 					return result;
206974eac5f3SSu Sung Chung 			}
207074eac5f3SSu Sung Chung 
20711dc90497SAndrey Grodzovsky 			/* Switch to dp clock source only if there is
20721dc90497SAndrey Grodzovsky 			 * no non dp stream that shares the same timing
20731dc90497SAndrey Grodzovsky 			 * with the dp stream.
20741dc90497SAndrey Grodzovsky 			 */
20751dc90497SAndrey Grodzovsky 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
20761dc90497SAndrey Grodzovsky 				!find_pll_sharable_stream(stream, new_ctx)) {
20771dc90497SAndrey Grodzovsky 
20789d0dcecdSHarry Wentland 				resource_unreference_clock_source(
20791dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
20801dc90497SAndrey Grodzovsky 						dc->res_pool,
20819d0dcecdSHarry Wentland 						pipe_ctx->clock_source);
20824a629536SHarry Wentland 
20831dc90497SAndrey Grodzovsky 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
20841dc90497SAndrey Grodzovsky 				resource_reference_clock_source(
20851dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
20861dc90497SAndrey Grodzovsky 						dc->res_pool,
20871dc90497SAndrey Grodzovsky 						 pipe_ctx->clock_source);
20881dc90497SAndrey Grodzovsky 			}
20891dc90497SAndrey Grodzovsky 		}
20901dc90497SAndrey Grodzovsky 	}
20911dc90497SAndrey Grodzovsky 
20921dc90497SAndrey Grodzovsky 	result = resource_build_scaling_params_for_context(dc, new_ctx);
20931dc90497SAndrey Grodzovsky 
20941dc90497SAndrey Grodzovsky 	if (result == DC_OK)
2095afcd526bSJoshua Aberback 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, fast_validate))
20961dc90497SAndrey Grodzovsky 			result = DC_FAIL_BANDWIDTH_VALIDATE;
20971dc90497SAndrey Grodzovsky 
20981dc90497SAndrey Grodzovsky 	return result;
20991dc90497SAndrey Grodzovsky }
21001dc90497SAndrey Grodzovsky 
21016e4d6beeSTony Cheng static void patch_gamut_packet_checksum(
2102e09b6473SAnthony Koo 		struct dc_info_packet *gamut_packet)
21034562236bSHarry Wentland {
21044562236bSHarry Wentland 	/* For gamut we recalc checksum */
21056e4d6beeSTony Cheng 	if (gamut_packet->valid) {
21064562236bSHarry Wentland 		uint8_t chk_sum = 0;
21074562236bSHarry Wentland 		uint8_t *ptr;
21084562236bSHarry Wentland 		uint8_t i;
21094562236bSHarry Wentland 
21104562236bSHarry Wentland 		/*start of the Gamut data. */
21116e4d6beeSTony Cheng 		ptr = &gamut_packet->sb[3];
21124562236bSHarry Wentland 
21136e4d6beeSTony Cheng 		for (i = 0; i <= gamut_packet->sb[1]; i++)
21144562236bSHarry Wentland 			chk_sum += ptr[i];
21154562236bSHarry Wentland 
21166e4d6beeSTony Cheng 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
21171646a6feSAndrew Wong 	}
21184562236bSHarry Wentland }
21194562236bSHarry Wentland 
21204562236bSHarry Wentland static void set_avi_info_frame(
2121e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
21224562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
21234562236bSHarry Wentland {
21240971c40eSHarry Wentland 	struct dc_stream_state *stream = pipe_ctx->stream;
21254562236bSHarry Wentland 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
21264562236bSHarry Wentland 	uint32_t pixel_encoding = 0;
21274562236bSHarry Wentland 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
21284562236bSHarry Wentland 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
21294562236bSHarry Wentland 	bool itc = false;
213050e27654SZeyu Fan 	uint8_t itc_value = 0;
21314562236bSHarry Wentland 	uint8_t cn0_cn1 = 0;
213250e27654SZeyu Fan 	unsigned int cn0_cn1_value = 0;
21334562236bSHarry Wentland 	uint8_t *check_sum = NULL;
21344562236bSHarry Wentland 	uint8_t byte_index = 0;
2135754e3673SAnthony Koo 	union hdmi_info_packet hdmi_info;
213650e27654SZeyu Fan 	union display_content_support support = {0};
21374fa086b9SLeo (Sunpeng) Li 	unsigned int vic = pipe_ctx->stream->timing.vic;
213815e17335SCharlene Liu 	enum dc_timing_3d_format format;
21394562236bSHarry Wentland 
2140754e3673SAnthony Koo 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
2141754e3673SAnthony Koo 
21424fa086b9SLeo (Sunpeng) Li 	color_space = pipe_ctx->stream->output_color_space;
2143e5f2038eSCharlene Liu 	if (color_space == COLOR_SPACE_UNKNOWN)
21444fa086b9SLeo (Sunpeng) Li 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
2145e5f2038eSCharlene Liu 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
21464562236bSHarry Wentland 
21474562236bSHarry Wentland 	/* Initialize header */
2148e09b6473SAnthony Koo 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
21494562236bSHarry Wentland 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
21504562236bSHarry Wentland 	* not be used in HDMI 2.0 (Section 10.1) */
2151e09b6473SAnthony Koo 	hdmi_info.bits.header.version = 2;
2152e09b6473SAnthony Koo 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
21534562236bSHarry Wentland 
21544562236bSHarry Wentland 	/*
21554562236bSHarry Wentland 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
21564562236bSHarry Wentland 	 * according to HDMI 2.0 spec (Section 10.1)
21574562236bSHarry Wentland 	 */
21584562236bSHarry Wentland 
21594fa086b9SLeo (Sunpeng) Li 	switch (stream->timing.pixel_encoding) {
21604562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR422:
21614562236bSHarry Wentland 		pixel_encoding = 1;
21624562236bSHarry Wentland 		break;
21634562236bSHarry Wentland 
21644562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR444:
21654562236bSHarry Wentland 		pixel_encoding = 2;
21664562236bSHarry Wentland 		break;
21674562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR420:
21684562236bSHarry Wentland 		pixel_encoding = 3;
21694562236bSHarry Wentland 		break;
21704562236bSHarry Wentland 
21714562236bSHarry Wentland 	case PIXEL_ENCODING_RGB:
21724562236bSHarry Wentland 	default:
21734562236bSHarry Wentland 		pixel_encoding = 0;
21744562236bSHarry Wentland 	}
21754562236bSHarry Wentland 
21764562236bSHarry Wentland 	/* Y0_Y1_Y2 : The pixel encoding */
21774562236bSHarry Wentland 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
2178e09b6473SAnthony Koo 	hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
21794562236bSHarry Wentland 
21804562236bSHarry Wentland 	/* A0 = 1 Active Format Information valid */
2181e09b6473SAnthony Koo 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
21824562236bSHarry Wentland 
21834562236bSHarry Wentland 	/* B0, B1 = 3; Bar info data is valid */
2184e09b6473SAnthony Koo 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
21854562236bSHarry Wentland 
2186e09b6473SAnthony Koo 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
21874562236bSHarry Wentland 
21884562236bSHarry Wentland 	/* S0, S1 : Underscan / Overscan */
21894562236bSHarry Wentland 	/* TODO: un-hardcode scan type */
21904562236bSHarry Wentland 	scan_type = SCANNING_TYPE_UNDERSCAN;
2191e09b6473SAnthony Koo 	hdmi_info.bits.S0_S1 = scan_type;
21924562236bSHarry Wentland 
21934562236bSHarry Wentland 	/* C0, C1 : Colorimetry */
21948fde5884SCharlene Liu 	if (color_space == COLOR_SPACE_YCBCR709 ||
219515e17335SCharlene Liu 			color_space == COLOR_SPACE_YCBCR709_LIMITED)
2196e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
21978fde5884SCharlene Liu 	else if (color_space == COLOR_SPACE_YCBCR601 ||
21988fde5884SCharlene Liu 			color_space == COLOR_SPACE_YCBCR601_LIMITED)
2199e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
22008fde5884SCharlene Liu 	else {
2201e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
22028fde5884SCharlene Liu 	}
2203534db198SAmy Zhang 	if (color_space == COLOR_SPACE_2020_RGB_FULLRANGE ||
2204534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE ||
2205534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_YCBCR) {
2206e09b6473SAnthony Koo 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
2207e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2208534db198SAmy Zhang 	} else if (color_space == COLOR_SPACE_ADOBERGB) {
2209e09b6473SAnthony Koo 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
2210e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2211534db198SAmy Zhang 	}
2212534db198SAmy Zhang 
22134562236bSHarry Wentland 	/* TODO: un-hardcode aspect ratio */
22144fa086b9SLeo (Sunpeng) Li 	aspect = stream->timing.aspect_ratio;
22154562236bSHarry Wentland 
22164562236bSHarry Wentland 	switch (aspect) {
22174562236bSHarry Wentland 	case ASPECT_RATIO_4_3:
22184562236bSHarry Wentland 	case ASPECT_RATIO_16_9:
2219e09b6473SAnthony Koo 		hdmi_info.bits.M0_M1 = aspect;
22204562236bSHarry Wentland 		break;
22214562236bSHarry Wentland 
22224562236bSHarry Wentland 	case ASPECT_RATIO_NO_DATA:
22234562236bSHarry Wentland 	case ASPECT_RATIO_64_27:
22244562236bSHarry Wentland 	case ASPECT_RATIO_256_135:
22254562236bSHarry Wentland 	default:
2226e09b6473SAnthony Koo 		hdmi_info.bits.M0_M1 = 0;
22274562236bSHarry Wentland 	}
22284562236bSHarry Wentland 
22294562236bSHarry Wentland 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
2230e09b6473SAnthony Koo 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
22314562236bSHarry Wentland 
22324562236bSHarry Wentland 	/* TODO: un-hardcode cn0_cn1 and itc */
223350e27654SZeyu Fan 
22344562236bSHarry Wentland 	cn0_cn1 = 0;
223550e27654SZeyu Fan 	cn0_cn1_value = 0;
223650e27654SZeyu Fan 
223750e27654SZeyu Fan 	itc = true;
223850e27654SZeyu Fan 	itc_value = 1;
223950e27654SZeyu Fan 
2240ceb3dbb4SJun Lei 	support = stream->content_support;
22414562236bSHarry Wentland 
22424562236bSHarry Wentland 	if (itc) {
224350e27654SZeyu Fan 		if (!support.bits.valid_content_type) {
224450e27654SZeyu Fan 			cn0_cn1_value = 0;
224550e27654SZeyu Fan 		} else {
224650e27654SZeyu Fan 			if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
224750e27654SZeyu Fan 				if (support.bits.graphics_content == 1) {
224850e27654SZeyu Fan 					cn0_cn1_value = 0;
224950e27654SZeyu Fan 				}
225050e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
225150e27654SZeyu Fan 				if (support.bits.photo_content == 1) {
225250e27654SZeyu Fan 					cn0_cn1_value = 1;
225350e27654SZeyu Fan 				} else {
225450e27654SZeyu Fan 					cn0_cn1_value = 0;
225550e27654SZeyu Fan 					itc_value = 0;
225650e27654SZeyu Fan 				}
225750e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
225850e27654SZeyu Fan 				if (support.bits.cinema_content == 1) {
225950e27654SZeyu Fan 					cn0_cn1_value = 2;
226050e27654SZeyu Fan 				} else {
226150e27654SZeyu Fan 					cn0_cn1_value = 0;
226250e27654SZeyu Fan 					itc_value = 0;
226350e27654SZeyu Fan 				}
226450e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
226550e27654SZeyu Fan 				if (support.bits.game_content == 1) {
226650e27654SZeyu Fan 					cn0_cn1_value = 3;
226750e27654SZeyu Fan 				} else {
226850e27654SZeyu Fan 					cn0_cn1_value = 0;
226950e27654SZeyu Fan 					itc_value = 0;
227050e27654SZeyu Fan 				}
227150e27654SZeyu Fan 			}
227250e27654SZeyu Fan 		}
2273e09b6473SAnthony Koo 		hdmi_info.bits.CN0_CN1 = cn0_cn1_value;
2274e09b6473SAnthony Koo 		hdmi_info.bits.ITC = itc_value;
22754562236bSHarry Wentland 	}
22764562236bSHarry Wentland 
22774562236bSHarry Wentland 	/* TODO : We should handle YCC quantization */
22784562236bSHarry Wentland 	/* but we do not have matrix calculation */
2279ceb3dbb4SJun Lei 	if (stream->qs_bit == 1 &&
2280ceb3dbb4SJun Lei 			stream->qy_bit == 1) {
228150e27654SZeyu Fan 		if (color_space == COLOR_SPACE_SRGB ||
228250e27654SZeyu Fan 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE) {
2283e09b6473SAnthony Koo 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
2284e09b6473SAnthony Koo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_FULL_RANGE;
228550e27654SZeyu Fan 		} else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
228650e27654SZeyu Fan 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE) {
2287e09b6473SAnthony Koo 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
2288e09b6473SAnthony Koo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
22894562236bSHarry Wentland 		} else {
2290e09b6473SAnthony Koo 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2291e09b6473SAnthony Koo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
22924562236bSHarry Wentland 		}
229350e27654SZeyu Fan 	} else {
2294e09b6473SAnthony Koo 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2295e09b6473SAnthony Koo 		hdmi_info.bits.YQ0_YQ1   = YYC_QUANTIZATION_LIMITED_RANGE;
229650e27654SZeyu Fan 	}
229750e27654SZeyu Fan 
229815e17335SCharlene Liu 	///VIC
22994fa086b9SLeo (Sunpeng) Li 	format = stream->timing.timing_3d_format;
230015e17335SCharlene Liu 	/*todo, add 3DStereo support*/
230115e17335SCharlene Liu 	if (format != TIMING_3D_FORMAT_NONE) {
230215e17335SCharlene Liu 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
23034fa086b9SLeo (Sunpeng) Li 		switch (pipe_ctx->stream->timing.hdmi_vic) {
230415e17335SCharlene Liu 		case 1:
230515e17335SCharlene Liu 			vic = 95;
230615e17335SCharlene Liu 			break;
230715e17335SCharlene Liu 		case 2:
230815e17335SCharlene Liu 			vic = 94;
230915e17335SCharlene Liu 			break;
231015e17335SCharlene Liu 		case 3:
231115e17335SCharlene Liu 			vic = 93;
231215e17335SCharlene Liu 			break;
231315e17335SCharlene Liu 		case 4:
231415e17335SCharlene Liu 			vic = 98;
231515e17335SCharlene Liu 			break;
231615e17335SCharlene Liu 		default:
231715e17335SCharlene Liu 			break;
231815e17335SCharlene Liu 		}
231915e17335SCharlene Liu 	}
2320efa02336SChris Park 	/* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
2321e09b6473SAnthony Koo 	hdmi_info.bits.VIC0_VIC7 = vic;
2322efa02336SChris Park 	if (vic >= 128)
2323efa02336SChris Park 		hdmi_info.bits.header.version = 3;
2324efa02336SChris Park 	/* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
2325efa02336SChris Park 	 * the Source shall use 20 AVI InfoFrame Version 4
2326efa02336SChris Park 	 */
2327efa02336SChris Park 	if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
2328efa02336SChris Park 			hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
2329efa02336SChris Park 		hdmi_info.bits.header.version = 4;
2330efa02336SChris Park 		hdmi_info.bits.header.length = 14;
2331efa02336SChris Park 	}
23324562236bSHarry Wentland 
23334562236bSHarry Wentland 	/* pixel repetition
23344562236bSHarry Wentland 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
23354562236bSHarry Wentland 	 * repetition start from 1 */
2336e09b6473SAnthony Koo 	hdmi_info.bits.PR0_PR3 = 0;
23374562236bSHarry Wentland 
23384562236bSHarry Wentland 	/* Bar Info
23394562236bSHarry Wentland 	 * barTop:    Line Number of End of Top Bar.
23404562236bSHarry Wentland 	 * barBottom: Line Number of Start of Bottom Bar.
23414562236bSHarry Wentland 	 * barLeft:   Pixel Number of End of Left Bar.
23424562236bSHarry Wentland 	 * barRight:  Pixel Number of Start of Right Bar. */
2343e09b6473SAnthony Koo 	hdmi_info.bits.bar_top = stream->timing.v_border_top;
2344e09b6473SAnthony Koo 	hdmi_info.bits.bar_bottom = (stream->timing.v_total
23454fa086b9SLeo (Sunpeng) Li 			- stream->timing.v_border_bottom + 1);
2346e09b6473SAnthony Koo 	hdmi_info.bits.bar_left  = stream->timing.h_border_left;
2347e09b6473SAnthony Koo 	hdmi_info.bits.bar_right = (stream->timing.h_total
23484fa086b9SLeo (Sunpeng) Li 			- stream->timing.h_border_right + 1);
23494562236bSHarry Wentland 
23502f482c4fSChris Park     /* Additional Colorimetry Extension
23512f482c4fSChris Park      * Used in conduction with C0-C1 and EC0-EC2
23522f482c4fSChris Park      * 0 = DCI-P3 RGB (D65)
23532f482c4fSChris Park      * 1 = DCI-P3 RGB (theater)
23542f482c4fSChris Park      */
23552f482c4fSChris Park 	hdmi_info.bits.ACE0_ACE3 = 0;
23562f482c4fSChris Park 
23574562236bSHarry Wentland 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
2358e09b6473SAnthony Koo 	check_sum = &hdmi_info.packet_raw_data.sb[0];
2359e8d726b7SReza Amini 
2360efa02336SChris Park 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
23614562236bSHarry Wentland 
2362efa02336SChris Park 	for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
2363e09b6473SAnthony Koo 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
23644562236bSHarry Wentland 
23654562236bSHarry Wentland 	/* one byte complement */
23664562236bSHarry Wentland 	*check_sum = (uint8_t) (0x100 - *check_sum);
23674562236bSHarry Wentland 
23684562236bSHarry Wentland 	/* Store in hw_path_mode */
2369e09b6473SAnthony Koo 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
2370e09b6473SAnthony Koo 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
2371e09b6473SAnthony Koo 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
23724562236bSHarry Wentland 
2373e09b6473SAnthony Koo 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
2374e09b6473SAnthony Koo 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
23754562236bSHarry Wentland 
23764562236bSHarry Wentland 	info_packet->valid = true;
23774562236bSHarry Wentland }
23784562236bSHarry Wentland 
23796e4d6beeSTony Cheng static void set_vendor_info_packet(
2380e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
23810971c40eSHarry Wentland 		struct dc_stream_state *stream)
23824562236bSHarry Wentland {
2383ecd0136bSHarmanprit Tatla 	/* SPD info packet for FreeSync */
238415e17335SCharlene Liu 
2385ecd0136bSHarmanprit Tatla 	/* Check if Freesync is supported. Return if false. If true,
2386ecd0136bSHarmanprit Tatla 	 * set the corresponding bit in the info packet
2387ecd0136bSHarmanprit Tatla 	 */
2388ecd0136bSHarmanprit Tatla 	if (!stream->vsp_infopacket.valid)
23894562236bSHarry Wentland 		return;
23904562236bSHarry Wentland 
2391ecd0136bSHarmanprit Tatla 	*info_packet = stream->vsp_infopacket;
23924562236bSHarry Wentland }
23934562236bSHarry Wentland 
23946e4d6beeSTony Cheng static void set_spd_info_packet(
2395e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
23960971c40eSHarry Wentland 		struct dc_stream_state *stream)
23974562236bSHarry Wentland {
23984562236bSHarry Wentland 	/* SPD info packet for FreeSync */
23994562236bSHarry Wentland 
24004562236bSHarry Wentland 	/* Check if Freesync is supported. Return if false. If true,
24014562236bSHarry Wentland 	 * set the corresponding bit in the info packet
24024562236bSHarry Wentland 	 */
240398e6436dSAnthony Koo 	if (!stream->vrr_infopacket.valid)
24044562236bSHarry Wentland 		return;
24054562236bSHarry Wentland 
240698e6436dSAnthony Koo 	*info_packet = stream->vrr_infopacket;
24074562236bSHarry Wentland }
24084562236bSHarry Wentland 
24091646a6feSAndrew Wong static void set_hdr_static_info_packet(
2410e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
24110971c40eSHarry Wentland 		struct dc_stream_state *stream)
24121646a6feSAndrew Wong {
24130eeef690SAnthony Koo 	/* HDR Static Metadata info packet for HDR10 */
24141646a6feSAndrew Wong 
2415a10dc97aSKrunoslav Kovac 	if (!stream->hdr_static_metadata.valid ||
2416a10dc97aSKrunoslav Kovac 			stream->use_dynamic_meta)
241710bff005SYongqiang Sun 		return;
241810bff005SYongqiang Sun 
24190eeef690SAnthony Koo 	*info_packet = stream->hdr_static_metadata;
24201646a6feSAndrew Wong }
24211646a6feSAndrew Wong 
24226e4d6beeSTony Cheng static void set_vsc_info_packet(
2423e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
24240971c40eSHarry Wentland 		struct dc_stream_state *stream)
24254562236bSHarry Wentland {
24261336926fSAlvin lee 	if (!stream->vsc_infopacket.valid)
24274562236bSHarry Wentland 		return;
24284562236bSHarry Wentland 
24291336926fSAlvin lee 	*info_packet = stream->vsc_infopacket;
24304562236bSHarry Wentland }
24314562236bSHarry Wentland 
2432f36cc577SBhawanpreet Lakha void dc_resource_state_destruct(struct dc_state *context)
24334562236bSHarry Wentland {
24344562236bSHarry Wentland 	int i, j;
24354562236bSHarry Wentland 
2436ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
24373be5262eSHarry Wentland 		for (j = 0; j < context->stream_status[i].plane_count; j++)
24383be5262eSHarry Wentland 			dc_plane_state_release(
24393be5262eSHarry Wentland 				context->stream_status[i].plane_states[j]);
24404562236bSHarry Wentland 
24413be5262eSHarry Wentland 		context->stream_status[i].plane_count = 0;
24424fa086b9SLeo (Sunpeng) Li 		dc_stream_release(context->streams[i]);
2443ab2541b6SAric Cyr 		context->streams[i] = NULL;
24444562236bSHarry Wentland 	}
24454562236bSHarry Wentland }
24464562236bSHarry Wentland 
2447f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct(
2448608ac7bbSJerry Zuo 		const struct dc_state *src_ctx,
2449608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
24504562236bSHarry Wentland {
24514562236bSHarry Wentland 	int i, j;
24528ee5702aSDave Airlie 	struct kref refcount = dst_ctx->refcount;
24534562236bSHarry Wentland 
24544562236bSHarry Wentland 	*dst_ctx = *src_ctx;
24554562236bSHarry Wentland 
2456a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
24574562236bSHarry Wentland 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
24584562236bSHarry Wentland 
24594562236bSHarry Wentland 		if (cur_pipe->top_pipe)
24604562236bSHarry Wentland 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
24614562236bSHarry Wentland 
24624562236bSHarry Wentland 		if (cur_pipe->bottom_pipe)
24634562236bSHarry Wentland 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
2464b1f6d01cSDmytro Laktyushkin 
2465b1f6d01cSDmytro Laktyushkin 		if (cur_pipe->next_odm_pipe)
2466b1f6d01cSDmytro Laktyushkin 			cur_pipe->next_odm_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
2467b1f6d01cSDmytro Laktyushkin 
2468b1f6d01cSDmytro Laktyushkin 		if (cur_pipe->prev_odm_pipe)
2469b1f6d01cSDmytro Laktyushkin 			cur_pipe->prev_odm_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
24704562236bSHarry Wentland 	}
24714562236bSHarry Wentland 
2472ab2541b6SAric Cyr 	for (i = 0; i < dst_ctx->stream_count; i++) {
24734fa086b9SLeo (Sunpeng) Li 		dc_stream_retain(dst_ctx->streams[i]);
24743be5262eSHarry Wentland 		for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
24753be5262eSHarry Wentland 			dc_plane_state_retain(
24763be5262eSHarry Wentland 				dst_ctx->stream_status[i].plane_states[j]);
24774562236bSHarry Wentland 	}
24789a3afbb3SAndrey Grodzovsky 
24799a3afbb3SAndrey Grodzovsky 	/* context refcount should not be overridden */
24808ee5702aSDave Airlie 	dst_ctx->refcount = refcount;
24819a3afbb3SAndrey Grodzovsky 
24824562236bSHarry Wentland }
24834562236bSHarry Wentland 
24844562236bSHarry Wentland struct clock_source *dc_resource_find_first_free_pll(
2485a2b8659dSTony Cheng 		struct resource_context *res_ctx,
2486a2b8659dSTony Cheng 		const struct resource_pool *pool)
24874562236bSHarry Wentland {
24884562236bSHarry Wentland 	int i;
24894562236bSHarry Wentland 
2490a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; ++i) {
24914562236bSHarry Wentland 		if (res_ctx->clock_source_ref_count[i] == 0)
2492a2b8659dSTony Cheng 			return pool->clock_sources[i];
24934562236bSHarry Wentland 	}
24944562236bSHarry Wentland 
24954562236bSHarry Wentland 	return NULL;
24964562236bSHarry Wentland }
24974562236bSHarry Wentland 
24984562236bSHarry Wentland void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
24994562236bSHarry Wentland {
25004562236bSHarry Wentland 	enum signal_type signal = SIGNAL_TYPE_NONE;
250196c50c0dSHarry Wentland 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
25024562236bSHarry Wentland 
25034562236bSHarry Wentland 	/* default all packets to invalid */
25046e4d6beeSTony Cheng 	info->avi.valid = false;
25056e4d6beeSTony Cheng 	info->gamut.valid = false;
25066e4d6beeSTony Cheng 	info->vendor.valid = false;
2507630e3573SJeff Smith 	info->spd.valid = false;
25086e4d6beeSTony Cheng 	info->hdrsmd.valid = false;
25096e4d6beeSTony Cheng 	info->vsc.valid = false;
25104562236bSHarry Wentland 
25114562236bSHarry Wentland 	signal = pipe_ctx->stream->signal;
25124562236bSHarry Wentland 
25134562236bSHarry Wentland 	/* HDMi and DP have different info packets*/
25144562236bSHarry Wentland 	if (dc_is_hdmi_signal(signal)) {
25156e4d6beeSTony Cheng 		set_avi_info_frame(&info->avi, pipe_ctx);
25166e4d6beeSTony Cheng 
25176e4d6beeSTony Cheng 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
25186e4d6beeSTony Cheng 
25196e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
25206e4d6beeSTony Cheng 
252156ef6ed9SAnthony Koo 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
25226e4d6beeSTony Cheng 
2523a33fa99dSHarry Wentland 	} else if (dc_is_dp_signal(signal)) {
25246e4d6beeSTony Cheng 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
25256e4d6beeSTony Cheng 
25266e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
25276e4d6beeSTony Cheng 
252856ef6ed9SAnthony Koo 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
2529a33fa99dSHarry Wentland 	}
25304562236bSHarry Wentland 
25316e4d6beeSTony Cheng 	patch_gamut_packet_checksum(&info->gamut);
25324562236bSHarry Wentland }
25334562236bSHarry Wentland 
25344562236bSHarry Wentland enum dc_status resource_map_clock_resources(
2535fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
2536608ac7bbSJerry Zuo 		struct dc_state *context,
25371dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
25384562236bSHarry Wentland {
25394562236bSHarry Wentland 	/* acquire new resources */
25401dc90497SAndrey Grodzovsky 	const struct resource_pool *pool = dc->res_pool;
25411dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(
25421dc90497SAndrey Grodzovsky 				&context->res_ctx, stream);
25434562236bSHarry Wentland 
25441dc90497SAndrey Grodzovsky 	if (!pipe_ctx)
25451dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
25464562236bSHarry Wentland 
25474562236bSHarry Wentland 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
25484562236bSHarry Wentland 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
2549a2b8659dSTony Cheng 		pipe_ctx->clock_source = pool->dp_clock_source;
25504562236bSHarry Wentland 	else {
25514562236bSHarry Wentland 		pipe_ctx->clock_source = NULL;
25524562236bSHarry Wentland 
2553fb3466a4SBhawanpreet Lakha 		if (!dc->config.disable_disp_pll_sharing)
25544ed4e51bSMikita Lipski 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
25554562236bSHarry Wentland 				&context->res_ctx,
25564562236bSHarry Wentland 				pipe_ctx);
25574562236bSHarry Wentland 
25584562236bSHarry Wentland 		if (pipe_ctx->clock_source == NULL)
25594562236bSHarry Wentland 			pipe_ctx->clock_source =
2560a2b8659dSTony Cheng 				dc_resource_find_first_free_pll(
2561a2b8659dSTony Cheng 					&context->res_ctx,
2562a2b8659dSTony Cheng 					pool);
25634562236bSHarry Wentland 	}
25644562236bSHarry Wentland 
25654562236bSHarry Wentland 	if (pipe_ctx->clock_source == NULL)
25664562236bSHarry Wentland 		return DC_NO_CLOCK_SOURCE_RESOURCE;
25674562236bSHarry Wentland 
25684562236bSHarry Wentland 	resource_reference_clock_source(
2569a2b8659dSTony Cheng 		&context->res_ctx, pool,
25704562236bSHarry Wentland 		pipe_ctx->clock_source);
25714562236bSHarry Wentland 
25724562236bSHarry Wentland 	return DC_OK;
25734562236bSHarry Wentland }
25744562236bSHarry Wentland 
25754562236bSHarry Wentland /*
25764562236bSHarry Wentland  * Note: We need to disable output if clock sources change,
25774562236bSHarry Wentland  * since bios does optimization and doesn't apply if changing
25784562236bSHarry Wentland  * PHY when not already disabled.
25794562236bSHarry Wentland  */
25804562236bSHarry Wentland bool pipe_need_reprogram(
25814562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx_old,
25824562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
25834562236bSHarry Wentland {
2584cfe4645eSDmytro Laktyushkin 	if (!pipe_ctx_old->stream)
2585cfe4645eSDmytro Laktyushkin 		return false;
2586cfe4645eSDmytro Laktyushkin 
25874562236bSHarry Wentland 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
25884562236bSHarry Wentland 		return true;
25894562236bSHarry Wentland 
25904562236bSHarry Wentland 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
25914562236bSHarry Wentland 		return true;
25924562236bSHarry Wentland 
2593afaacef4SHarry Wentland 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
25944562236bSHarry Wentland 		return true;
25954562236bSHarry Wentland 
25964562236bSHarry Wentland 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
25974562236bSHarry Wentland 			&& pipe_ctx_old->stream != pipe_ctx->stream)
25984562236bSHarry Wentland 		return true;
25994562236bSHarry Wentland 
26008e9c4c8cSHarry Wentland 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
26014562236bSHarry Wentland 		return true;
26024562236bSHarry Wentland 
26034562236bSHarry Wentland 	if (is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26044562236bSHarry Wentland 		return true;
26054562236bSHarry Wentland 
26066b622181SJulian Parkin 	if (is_hdr_static_meta_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26076b622181SJulian Parkin 		return true;
26084562236bSHarry Wentland 
26091e7e86c4SSamson Tam 	if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
26101e7e86c4SSamson Tam 		return true;
26111e7e86c4SSamson Tam 
26121336926fSAlvin lee 	if (is_vsc_info_packet_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26131336926fSAlvin lee 		return true;
26141336926fSAlvin lee 
2615eed928dcSCharlene Liu 	if (false == pipe_ctx_old->stream->link->link_state_valid &&
2616eed928dcSCharlene Liu 		false == pipe_ctx_old->stream->dpms_off)
2617eed928dcSCharlene Liu 		return true;
2618eed928dcSCharlene Liu 
26194562236bSHarry Wentland 	return false;
26204562236bSHarry Wentland }
2621529cad0fSDing Wang 
26220971c40eSHarry Wentland void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
2623529cad0fSDing Wang 		struct bit_depth_reduction_params *fmt_bit_depth)
2624529cad0fSDing Wang {
26254fa086b9SLeo (Sunpeng) Li 	enum dc_dither_option option = stream->dither_option;
2626529cad0fSDing Wang 	enum dc_pixel_encoding pixel_encoding =
26274fa086b9SLeo (Sunpeng) Li 			stream->timing.pixel_encoding;
2628529cad0fSDing Wang 
2629529cad0fSDing Wang 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
2630529cad0fSDing Wang 
2631603767f9STony Cheng 	if (option == DITHER_OPTION_DEFAULT) {
2632603767f9STony Cheng 		switch (stream->timing.display_color_depth) {
2633603767f9STony Cheng 		case COLOR_DEPTH_666:
2634603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL6;
2635603767f9STony Cheng 			break;
2636603767f9STony Cheng 		case COLOR_DEPTH_888:
2637603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL8;
2638603767f9STony Cheng 			break;
2639603767f9STony Cheng 		case COLOR_DEPTH_101010:
2640603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL10;
2641603767f9STony Cheng 			break;
2642603767f9STony Cheng 		default:
2643603767f9STony Cheng 			option = DITHER_OPTION_DISABLE;
2644603767f9STony Cheng 		}
2645603767f9STony Cheng 	}
2646603767f9STony Cheng 
2647529cad0fSDing Wang 	if (option == DITHER_OPTION_DISABLE)
2648529cad0fSDing Wang 		return;
2649529cad0fSDing Wang 
2650529cad0fSDing Wang 	if (option == DITHER_OPTION_TRUN6) {
2651529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2652529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
2653529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN8 ||
2654529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
2655529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6) {
2656529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2657529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
2658529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN10        ||
2659529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
2660529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
2661529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8     ||
2662529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2663529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2664529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2665529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2666529cad0fSDing Wang 	}
2667529cad0fSDing Wang 
2668529cad0fSDing Wang 	/* special case - Formatter can only reduce by 4 bits at most.
2669529cad0fSDing Wang 	 * When reducing from 12 to 6 bits,
2670529cad0fSDing Wang 	 * HW recommends we use trunc with round mode
2671529cad0fSDing Wang 	 * (if we did nothing, trunc to 10 bits would be used)
2672529cad0fSDing Wang 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
2673529cad0fSDing Wang 	 * as the input was 10 bits.
2674529cad0fSDing Wang 	 */
2675529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2676529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2677529cad0fSDing Wang 			option == DITHER_OPTION_FM6) {
2678529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2679529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2680529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
2681529cad0fSDing Wang 	}
2682529cad0fSDing Wang 
2683529cad0fSDing Wang 	/* spatial dither
2684529cad0fSDing Wang 	 * note that spatial modes 1-3 are never used
2685529cad0fSDing Wang 	 */
2686529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
2687529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2688529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
2689529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
2690529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2691529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
2692529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2693529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2694529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2695529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
2696529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2697529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6        ||
2698529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
2699529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2700529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2701529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
2702529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2703529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2704529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2705529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
2706529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10 ||
2707529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8 ||
2708529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6) {
2709529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2710529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
2711529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2712529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2713529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2714529cad0fSDing Wang 	}
2715529cad0fSDing Wang 
2716529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6 ||
2717529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2718529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10) {
2719529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
2720529cad0fSDing Wang 	} else {
2721529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
2722529cad0fSDing Wang 	}
2723529cad0fSDing Wang 
2724529cad0fSDing Wang 	//////////////////////
2725529cad0fSDing Wang 	//// temporal dither
2726529cad0fSDing Wang 	//////////////////////
2727529cad0fSDing Wang 	if (option == DITHER_OPTION_FM6           ||
2728529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6     ||
2729529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6     ||
2730529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2731529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6      ||
2732529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2733529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2734529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
2735529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM8        ||
2736529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8  ||
2737529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8) {
2738529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2739529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
2740529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM10) {
2741529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2742529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
2743529cad0fSDing Wang 	}
2744529cad0fSDing Wang 
2745529cad0fSDing Wang 	fmt_bit_depth->pixel_encoding = pixel_encoding;
2746529cad0fSDing Wang }
27479345d987SAndrey Grodzovsky 
274862c933f9SYongqiang Sun enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
27499345d987SAndrey Grodzovsky {
2750fb3466a4SBhawanpreet Lakha 	struct dc  *core_dc = dc;
2751ceb3dbb4SJun Lei 	struct dc_link *link = stream->link;
27529345d987SAndrey Grodzovsky 	struct timing_generator *tg = core_dc->res_pool->timing_generators[0];
27539345d987SAndrey Grodzovsky 	enum dc_status res = DC_OK;
27549345d987SAndrey Grodzovsky 
27554fa086b9SLeo (Sunpeng) Li 	calculate_phy_pix_clks(stream);
27569345d987SAndrey Grodzovsky 
27574fa086b9SLeo (Sunpeng) Li 	if (!tg->funcs->validate_timing(tg, &stream->timing))
27589345d987SAndrey Grodzovsky 		res = DC_FAIL_CONTROLLER_VALIDATE;
27599345d987SAndrey Grodzovsky 
2760248cbed6SEric Bernstein 	if (res == DC_OK) {
27619345d987SAndrey Grodzovsky 		if (!link->link_enc->funcs->validate_output_with_stream(
27624fa086b9SLeo (Sunpeng) Li 						link->link_enc, stream))
27639345d987SAndrey Grodzovsky 			res = DC_FAIL_ENC_VALIDATE;
2764248cbed6SEric Bernstein 	}
27659345d987SAndrey Grodzovsky 
27669345d987SAndrey Grodzovsky 	/* TODO: validate audio ASIC caps, encoder */
27679345d987SAndrey Grodzovsky 
27689345d987SAndrey Grodzovsky 	if (res == DC_OK)
27694fa086b9SLeo (Sunpeng) Li 		res = dc_link_validate_mode_timing(stream,
27709345d987SAndrey Grodzovsky 		      link,
27714fa086b9SLeo (Sunpeng) Li 		      &stream->timing);
27729345d987SAndrey Grodzovsky 
277362c933f9SYongqiang Sun 	return res;
27749345d987SAndrey Grodzovsky }
2775792671d7SAndrey Grodzovsky 
277662c933f9SYongqiang Sun enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
2777792671d7SAndrey Grodzovsky {
277862c933f9SYongqiang Sun 	enum dc_status res = DC_OK;
277962c933f9SYongqiang Sun 
2780792671d7SAndrey Grodzovsky 	/* TODO For now validates pixel format only */
27818e7095b9SDmytro Laktyushkin 	if (dc->res_pool->funcs->validate_plane)
278262c933f9SYongqiang Sun 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
2783792671d7SAndrey Grodzovsky 
278462c933f9SYongqiang Sun 	return res;
2785792671d7SAndrey Grodzovsky }
278674eac5f3SSu Sung Chung 
278774eac5f3SSu Sung Chung unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
278874eac5f3SSu Sung Chung {
278974eac5f3SSu Sung Chung 	switch (format) {
279074eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
279174eac5f3SSu Sung Chung 		return 8;
279274eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
279374eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
279474eac5f3SSu Sung Chung 		return 12;
279574eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
279674eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
279774eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
279874eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
279974eac5f3SSu Sung Chung 		return 16;
280074eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
280174eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
280274eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
280374eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
280474eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
280574eac5f3SSu Sung Chung 		return 32;
280674eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
280774eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
280874eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
280974eac5f3SSu Sung Chung 		return 64;
281074eac5f3SSu Sung Chung 	default:
281174eac5f3SSu Sung Chung 		ASSERT_CRITICAL(false);
281274eac5f3SSu Sung Chung 		return -1;
281374eac5f3SSu Sung Chung 	}
281474eac5f3SSu Sung Chung }
2815