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 
45683b5950SMauro Rossi #if defined(CONFIG_DRM_AMD_DC_SI)
46683b5950SMauro Rossi #include "dce60/dce60_resource.h"
47683b5950SMauro Rossi #endif
484562236bSHarry Wentland #include "dce80/dce80_resource.h"
494562236bSHarry Wentland #include "dce100/dce100_resource.h"
504562236bSHarry Wentland #include "dce110/dce110_resource.h"
514562236bSHarry Wentland #include "dce112/dce112_resource.h"
52cf2156e2SAlex Deucher #include "dce120/dce120_resource.h"
53b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
54ff5ef992SAlex Deucher #include "dcn10/dcn10_resource.h"
557ed4e635SHarry Wentland #include "dcn20/dcn20_resource.h"
56e22ece54SBhawanpreet Lakha #include "dcn21/dcn21_resource.h"
5720f2ffe5SAlex Deucher #include "dcn30/dcn30_resource.h"
5820f2ffe5SAlex Deucher #include "dcn301/dcn301_resource.h"
5920f2ffe5SAlex Deucher #include "dcn302/dcn302_resource.h"
60cd6d421eSAurabindo Pillai #include "dcn303/dcn303_resource.h"
6136d26912SBhawanpreet Lakha #endif
625d4b05ddSBhawanpreet Lakha 
635d4b05ddSBhawanpreet Lakha #define DC_LOGGER_INIT(logger)
645d4b05ddSBhawanpreet Lakha 
654562236bSHarry Wentland enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
664562236bSHarry Wentland {
674562236bSHarry Wentland 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
684562236bSHarry Wentland 	switch (asic_id.chip_family) {
694562236bSHarry Wentland 
70683b5950SMauro Rossi #if defined(CONFIG_DRM_AMD_DC_SI)
71683b5950SMauro Rossi 	case FAMILY_SI:
72683b5950SMauro Rossi 		if (ASIC_REV_IS_TAHITI_P(asic_id.hw_internal_rev) ||
73683b5950SMauro Rossi 		    ASIC_REV_IS_PITCAIRN_PM(asic_id.hw_internal_rev) ||
74683b5950SMauro Rossi 		    ASIC_REV_IS_CAPEVERDE_M(asic_id.hw_internal_rev))
75683b5950SMauro Rossi 			dc_version = DCE_VERSION_6_0;
76683b5950SMauro Rossi 		else if (ASIC_REV_IS_OLAND_M(asic_id.hw_internal_rev))
77683b5950SMauro Rossi 			dc_version = DCE_VERSION_6_4;
78683b5950SMauro Rossi 		else
79683b5950SMauro Rossi 			dc_version = DCE_VERSION_6_1;
80683b5950SMauro Rossi 		break;
81683b5950SMauro Rossi #endif
824562236bSHarry Wentland 	case FAMILY_CI:
834562236bSHarry Wentland 		dc_version = DCE_VERSION_8_0;
844562236bSHarry Wentland 		break;
85ebfdf0d0SAlex Deucher 	case FAMILY_KV:
86ebfdf0d0SAlex Deucher 		if (ASIC_REV_IS_KALINDI(asic_id.hw_internal_rev) ||
87ebfdf0d0SAlex Deucher 		    ASIC_REV_IS_BHAVANI(asic_id.hw_internal_rev) ||
88ebfdf0d0SAlex Deucher 		    ASIC_REV_IS_GODAVARI(asic_id.hw_internal_rev))
89ebfdf0d0SAlex Deucher 			dc_version = DCE_VERSION_8_3;
90ebfdf0d0SAlex Deucher 		else
91ebfdf0d0SAlex Deucher 			dc_version = DCE_VERSION_8_1;
92ebfdf0d0SAlex Deucher 		break;
934562236bSHarry Wentland 	case FAMILY_CZ:
944562236bSHarry Wentland 		dc_version = DCE_VERSION_11_0;
954562236bSHarry Wentland 		break;
964562236bSHarry Wentland 
974562236bSHarry Wentland 	case FAMILY_VI:
984562236bSHarry Wentland 		if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
994562236bSHarry Wentland 				ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
1004562236bSHarry Wentland 			dc_version = DCE_VERSION_10_0;
1014562236bSHarry Wentland 			break;
1024562236bSHarry Wentland 		}
1034562236bSHarry Wentland 		if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
104b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
105b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
1064562236bSHarry Wentland 			dc_version = DCE_VERSION_11_2;
1074562236bSHarry Wentland 		}
1080c75d5acSJerry (Fangzhi) Zuo 		if (ASIC_REV_IS_VEGAM(asic_id.hw_internal_rev))
1090c75d5acSJerry (Fangzhi) Zuo 			dc_version = DCE_VERSION_11_22;
1104562236bSHarry Wentland 		break;
1112c8ad2d5SAlex Deucher 	case FAMILY_AI:
112b8b6ce89SLeo Li 		if (ASICREV_IS_VEGA20_P(asic_id.hw_internal_rev))
113b8b6ce89SLeo Li 			dc_version = DCE_VERSION_12_1;
114b8b6ce89SLeo Li 		else
1152c8ad2d5SAlex Deucher 			dc_version = DCE_VERSION_12_0;
1162c8ad2d5SAlex Deucher 		break;
117b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
118ff5ef992SAlex Deucher 	case FAMILY_RV:
119ff5ef992SAlex Deucher 		dc_version = DCN_VERSION_1_0;
1200e3d73f1SBhawanpreet Lakha 		if (ASICREV_IS_RAVEN2(asic_id.hw_internal_rev))
1210e3d73f1SBhawanpreet Lakha 			dc_version = DCN_VERSION_1_01;
122e22ece54SBhawanpreet Lakha 		if (ASICREV_IS_RENOIR(asic_id.hw_internal_rev))
123e22ece54SBhawanpreet Lakha 			dc_version = DCN_VERSION_2_1;
1249ba93114SRoman Li 		if (ASICREV_IS_GREEN_SARDINE(asic_id.hw_internal_rev))
1259ba93114SRoman Li 			dc_version = DCN_VERSION_2_1;
126ff5ef992SAlex Deucher 		break;
1277ed4e635SHarry Wentland 
1287ed4e635SHarry Wentland 	case FAMILY_NV:
1297ed4e635SHarry Wentland 		dc_version = DCN_VERSION_2_0;
1305dba4991SBhawanpreet Lakha 		if (ASICREV_IS_SIENNA_CICHLID_P(asic_id.hw_internal_rev))
1315dba4991SBhawanpreet Lakha 			dc_version = DCN_VERSION_3_0;
13236d26912SBhawanpreet Lakha 		if (ASICREV_IS_DIMGREY_CAVEFISH_P(asic_id.hw_internal_rev))
13336d26912SBhawanpreet Lakha 			dc_version = DCN_VERSION_3_02;
134cd6d421eSAurabindo Pillai 		if (ASICREV_IS_BEIGE_GOBY_P(asic_id.hw_internal_rev))
135cd6d421eSAurabindo Pillai 			dc_version = DCN_VERSION_3_03;
1367ed4e635SHarry Wentland 		break;
1373a83e4e6SRoman Li 
1383a83e4e6SRoman Li 	case FAMILY_VGH:
1393a83e4e6SRoman Li 		dc_version = DCN_VERSION_3_01;
1403a83e4e6SRoman Li 		break;
1413a83e4e6SRoman Li #endif
1424562236bSHarry Wentland 	default:
1434562236bSHarry Wentland 		dc_version = DCE_VERSION_UNKNOWN;
1444562236bSHarry Wentland 		break;
1454562236bSHarry Wentland 	}
1464562236bSHarry Wentland 	return dc_version;
1474562236bSHarry Wentland }
1484562236bSHarry Wentland 
149d9673c92SHarry Wentland struct resource_pool *dc_create_resource_pool(struct dc  *dc,
150d9673c92SHarry Wentland 					      const struct dc_init_data *init_data,
151d9673c92SHarry Wentland 					      enum dce_version dc_version)
1524562236bSHarry Wentland {
1535ac3d3c9SCharlene Liu 	struct resource_pool *res_pool = NULL;
1544562236bSHarry Wentland 
1554562236bSHarry Wentland 	switch (dc_version) {
156683b5950SMauro Rossi #if defined(CONFIG_DRM_AMD_DC_SI)
157683b5950SMauro Rossi 	case DCE_VERSION_6_0:
158683b5950SMauro Rossi 		res_pool = dce60_create_resource_pool(
159683b5950SMauro Rossi 			init_data->num_virtual_links, dc);
160683b5950SMauro Rossi 		break;
161683b5950SMauro Rossi 	case DCE_VERSION_6_1:
162683b5950SMauro Rossi 		res_pool = dce61_create_resource_pool(
163683b5950SMauro Rossi 			init_data->num_virtual_links, dc);
164683b5950SMauro Rossi 		break;
165683b5950SMauro Rossi 	case DCE_VERSION_6_4:
166683b5950SMauro Rossi 		res_pool = dce64_create_resource_pool(
167683b5950SMauro Rossi 			init_data->num_virtual_links, dc);
168683b5950SMauro Rossi 		break;
169683b5950SMauro Rossi #endif
1704562236bSHarry Wentland 	case DCE_VERSION_8_0:
1715ac3d3c9SCharlene Liu 		res_pool = dce80_create_resource_pool(
172d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1735ac3d3c9SCharlene Liu 		break;
1747992a629SAlex Deucher 	case DCE_VERSION_8_1:
1757992a629SAlex Deucher 		res_pool = dce81_create_resource_pool(
176d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1777992a629SAlex Deucher 		break;
1787992a629SAlex Deucher 	case DCE_VERSION_8_3:
1797992a629SAlex Deucher 		res_pool = dce83_create_resource_pool(
180d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1817992a629SAlex Deucher 		break;
1824562236bSHarry Wentland 	case DCE_VERSION_10_0:
1835ac3d3c9SCharlene Liu 		res_pool = dce100_create_resource_pool(
184d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1855ac3d3c9SCharlene Liu 		break;
1864562236bSHarry Wentland 	case DCE_VERSION_11_0:
1875ac3d3c9SCharlene Liu 		res_pool = dce110_create_resource_pool(
188d9673c92SHarry Wentland 				init_data->num_virtual_links, dc,
189d9673c92SHarry Wentland 				init_data->asic_id);
1905ac3d3c9SCharlene Liu 		break;
1914562236bSHarry Wentland 	case DCE_VERSION_11_2:
1920c75d5acSJerry (Fangzhi) Zuo 	case DCE_VERSION_11_22:
1935ac3d3c9SCharlene Liu 		res_pool = dce112_create_resource_pool(
194d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
1955ac3d3c9SCharlene Liu 		break;
1962c8ad2d5SAlex Deucher 	case DCE_VERSION_12_0:
197b8b6ce89SLeo Li 	case DCE_VERSION_12_1:
1982c8ad2d5SAlex Deucher 		res_pool = dce120_create_resource_pool(
199d9673c92SHarry Wentland 				init_data->num_virtual_links, dc);
2002c8ad2d5SAlex Deucher 		break;
201ff5ef992SAlex Deucher 
202b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
203ff5ef992SAlex Deucher 	case DCN_VERSION_1_0:
2040e3d73f1SBhawanpreet Lakha 	case DCN_VERSION_1_01:
205d9673c92SHarry Wentland 		res_pool = dcn10_create_resource_pool(init_data, dc);
206ff5ef992SAlex Deucher 		break;
2077ed4e635SHarry Wentland 	case DCN_VERSION_2_0:
2087ed4e635SHarry Wentland 		res_pool = dcn20_create_resource_pool(init_data, dc);
2097ed4e635SHarry Wentland 		break;
210e22ece54SBhawanpreet Lakha 	case DCN_VERSION_2_1:
211e22ece54SBhawanpreet Lakha 		res_pool = dcn21_create_resource_pool(init_data, dc);
212e22ece54SBhawanpreet Lakha 		break;
2135dba4991SBhawanpreet Lakha 	case DCN_VERSION_3_0:
2145dba4991SBhawanpreet Lakha 		res_pool = dcn30_create_resource_pool(init_data, dc);
2155dba4991SBhawanpreet Lakha 		break;
2163a83e4e6SRoman Li 	case DCN_VERSION_3_01:
2173a83e4e6SRoman Li 		res_pool = dcn301_create_resource_pool(init_data, dc);
2183a83e4e6SRoman Li 		break;
21936d26912SBhawanpreet Lakha 	case DCN_VERSION_3_02:
22036d26912SBhawanpreet Lakha 		res_pool = dcn302_create_resource_pool(init_data, dc);
22136d26912SBhawanpreet Lakha 		break;
222cd6d421eSAurabindo Pillai 	case DCN_VERSION_3_03:
223cd6d421eSAurabindo Pillai 		res_pool = dcn303_create_resource_pool(init_data, dc);
224cd6d421eSAurabindo Pillai 		break;
22536d26912SBhawanpreet Lakha #endif
2264562236bSHarry Wentland 	default:
2274562236bSHarry Wentland 		break;
2284562236bSHarry Wentland 	}
229f49cfa27Shersen wu 
2305ac3d3c9SCharlene Liu 	if (res_pool != NULL) {
2319adc8050SDmytro Laktyushkin 		if (dc->ctx->dc_bios->fw_info_valid) {
23241a5a2a8Shersen wu 			res_pool->ref_clocks.xtalin_clock_inKhz =
2339adc8050SDmytro Laktyushkin 				dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
23441a5a2a8Shersen wu 			/* initialize with firmware data first, no all
23541a5a2a8Shersen wu 			 * ASIC have DCCG SW component. FPGA or
23641a5a2a8Shersen wu 			 * simulation need initialization of
23741a5a2a8Shersen wu 			 * dccg_ref_clock_inKhz, dchub_ref_clock_inKhz
23841a5a2a8Shersen wu 			 * with xtalin_clock_inKhz
23941a5a2a8Shersen wu 			 */
24041a5a2a8Shersen wu 			res_pool->ref_clocks.dccg_ref_clock_inKhz =
24141a5a2a8Shersen wu 				res_pool->ref_clocks.xtalin_clock_inKhz;
24241a5a2a8Shersen wu 			res_pool->ref_clocks.dchub_ref_clock_inKhz =
24341a5a2a8Shersen wu 				res_pool->ref_clocks.xtalin_clock_inKhz;
2445ac3d3c9SCharlene Liu 		} else
2455ac3d3c9SCharlene Liu 			ASSERT_CRITICAL(false);
2465ac3d3c9SCharlene Liu 	}
2475ac3d3c9SCharlene Liu 
2485ac3d3c9SCharlene Liu 	return res_pool;
2494562236bSHarry Wentland }
2504562236bSHarry Wentland 
251fb3466a4SBhawanpreet Lakha void dc_destroy_resource_pool(struct dc  *dc)
2524562236bSHarry Wentland {
2534562236bSHarry Wentland 	if (dc) {
2544562236bSHarry Wentland 		if (dc->res_pool)
2554562236bSHarry Wentland 			dc->res_pool->funcs->destroy(&dc->res_pool);
2564562236bSHarry Wentland 
2572004f45eSHarry Wentland 		kfree(dc->hwseq);
2584562236bSHarry Wentland 	}
2594562236bSHarry Wentland }
2604562236bSHarry Wentland 
2614562236bSHarry Wentland static void update_num_audio(
2624562236bSHarry Wentland 	const struct resource_straps *straps,
2634562236bSHarry Wentland 	unsigned int *num_audio,
2644562236bSHarry Wentland 	struct audio_support *aud_support)
2654562236bSHarry Wentland {
2664562236bSHarry Wentland 	aud_support->dp_audio = true;
267b8e9eb72SCharlene Liu 	aud_support->hdmi_audio_native = false;
268b8e9eb72SCharlene Liu 	aud_support->hdmi_audio_on_dongle = false;
269b8e9eb72SCharlene Liu 
270b8e9eb72SCharlene Liu 	if (straps->hdmi_disable == 0) {
2714562236bSHarry Wentland 		if (straps->dc_pinstraps_audio & 0x2) {
2724562236bSHarry Wentland 			aud_support->hdmi_audio_on_dongle = true;
273b8e9eb72SCharlene Liu 			aud_support->hdmi_audio_native = true;
2744562236bSHarry Wentland 		}
2754562236bSHarry Wentland 	}
2764562236bSHarry Wentland 
2774562236bSHarry Wentland 	switch (straps->audio_stream_number) {
2784562236bSHarry Wentland 	case 0: /* multi streams supported */
2794562236bSHarry Wentland 		break;
2804562236bSHarry Wentland 	case 1: /* multi streams not supported */
2814562236bSHarry Wentland 		*num_audio = 1;
2824562236bSHarry Wentland 		break;
2834562236bSHarry Wentland 	default:
2844562236bSHarry Wentland 		DC_ERR("DC: unexpected audio fuse!\n");
28517a96033SJulia Lawall 	}
2864562236bSHarry Wentland }
2874562236bSHarry Wentland 
2884562236bSHarry Wentland bool resource_construct(
2894562236bSHarry Wentland 	unsigned int num_virtual_links,
290fb3466a4SBhawanpreet Lakha 	struct dc  *dc,
2914562236bSHarry Wentland 	struct resource_pool *pool,
2924562236bSHarry Wentland 	const struct resource_create_funcs *create_funcs)
2934562236bSHarry Wentland {
2944562236bSHarry Wentland 	struct dc_context *ctx = dc->ctx;
2954562236bSHarry Wentland 	const struct resource_caps *caps = pool->res_cap;
2964562236bSHarry Wentland 	int i;
2974562236bSHarry Wentland 	unsigned int num_audio = caps->num_audio;
2984562236bSHarry Wentland 	struct resource_straps straps = {0};
2994562236bSHarry Wentland 
3004562236bSHarry Wentland 	if (create_funcs->read_dce_straps)
3014562236bSHarry Wentland 		create_funcs->read_dce_straps(dc->ctx, &straps);
3024562236bSHarry Wentland 
3034562236bSHarry Wentland 	pool->audio_count = 0;
3044562236bSHarry Wentland 	if (create_funcs->create_audio) {
3054562236bSHarry Wentland 		/* find the total number of streams available via the
3064562236bSHarry Wentland 		 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
3074562236bSHarry Wentland 		 * registers (one for each pin) starting from pin 1
3084562236bSHarry Wentland 		 * up to the max number of audio pins.
3094562236bSHarry Wentland 		 * We stop on the first pin where
3104562236bSHarry Wentland 		 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
3114562236bSHarry Wentland 		 */
3124562236bSHarry Wentland 		update_num_audio(&straps, &num_audio, &pool->audio_support);
3135feb9f07STai Man 		for (i = 0; i < caps->num_audio; i++) {
3144562236bSHarry Wentland 			struct audio *aud = create_funcs->create_audio(ctx, i);
3154562236bSHarry Wentland 
3164562236bSHarry Wentland 			if (aud == NULL) {
3174562236bSHarry Wentland 				DC_ERR("DC: failed to create audio!\n");
3184562236bSHarry Wentland 				return false;
3194562236bSHarry Wentland 			}
3204562236bSHarry Wentland 			if (!aud->funcs->endpoint_valid(aud)) {
3214562236bSHarry Wentland 				aud->funcs->destroy(&aud);
3224562236bSHarry Wentland 				break;
3234562236bSHarry Wentland 			}
3244562236bSHarry Wentland 			pool->audios[i] = aud;
3254562236bSHarry Wentland 			pool->audio_count++;
3264562236bSHarry Wentland 		}
3274562236bSHarry Wentland 	}
3284562236bSHarry Wentland 
3294562236bSHarry Wentland 	pool->stream_enc_count = 0;
3304562236bSHarry Wentland 	if (create_funcs->create_stream_encoder) {
3314562236bSHarry Wentland 		for (i = 0; i < caps->num_stream_encoder; i++) {
3324562236bSHarry Wentland 			pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
3334562236bSHarry Wentland 			if (pool->stream_enc[i] == NULL)
3344562236bSHarry Wentland 				DC_ERR("DC: failed to create stream_encoder!\n");
3354562236bSHarry Wentland 			pool->stream_enc_count++;
3364562236bSHarry Wentland 		}
3374562236bSHarry Wentland 	}
338929c3aaaSEric Bernstein 
33920f2ffe5SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN)
3405dba4991SBhawanpreet Lakha 	for (i = 0; i < caps->num_mpc_3dlut; i++) {
3415dba4991SBhawanpreet Lakha 		pool->mpc_lut[i] = dc_create_3dlut_func();
3425dba4991SBhawanpreet Lakha 		if (pool->mpc_lut[i] == NULL)
3435dba4991SBhawanpreet Lakha 			DC_ERR("DC: failed to create MPC 3dlut!\n");
3445dba4991SBhawanpreet Lakha 		pool->mpc_shaper[i] = dc_create_transfer_func();
3455dba4991SBhawanpreet Lakha 		if (pool->mpc_shaper[i] == NULL)
3465dba4991SBhawanpreet Lakha 			DC_ERR("DC: failed to create MPC shaper!\n");
3475dba4991SBhawanpreet Lakha 	}
3485dba4991SBhawanpreet Lakha #endif
3494176664bSCharlene Liu 	dc->caps.dynamic_audio = false;
3504176664bSCharlene Liu 	if (pool->audio_count < pool->stream_enc_count) {
3514176664bSCharlene Liu 		dc->caps.dynamic_audio = true;
3524176664bSCharlene Liu 	}
3534562236bSHarry Wentland 	for (i = 0; i < num_virtual_links; i++) {
3544562236bSHarry Wentland 		pool->stream_enc[pool->stream_enc_count] =
3554562236bSHarry Wentland 			virtual_stream_encoder_create(
3564562236bSHarry Wentland 					ctx, ctx->dc_bios);
3574562236bSHarry Wentland 		if (pool->stream_enc[pool->stream_enc_count] == NULL) {
3584562236bSHarry Wentland 			DC_ERR("DC: failed to create stream_encoder!\n");
3594562236bSHarry Wentland 			return false;
3604562236bSHarry Wentland 		}
3614562236bSHarry Wentland 		pool->stream_enc_count++;
3624562236bSHarry Wentland 	}
3634562236bSHarry Wentland 
3644562236bSHarry Wentland 	dc->hwseq = create_funcs->create_hwseq(ctx);
3654562236bSHarry Wentland 
3664562236bSHarry Wentland 	return true;
3674562236bSHarry Wentland }
368ad8960a6SMikita Lipski static int find_matching_clock_source(
369ad8960a6SMikita Lipski 		const struct resource_pool *pool,
370ad8960a6SMikita Lipski 		struct clock_source *clock_source)
371ad8960a6SMikita Lipski {
3724562236bSHarry Wentland 
373ad8960a6SMikita Lipski 	int i;
374ad8960a6SMikita Lipski 
375ad8960a6SMikita Lipski 	for (i = 0; i < pool->clk_src_count; i++) {
376ad8960a6SMikita Lipski 		if (pool->clock_sources[i] == clock_source)
377ad8960a6SMikita Lipski 			return i;
378ad8960a6SMikita Lipski 	}
379ad8960a6SMikita Lipski 	return -1;
380ad8960a6SMikita Lipski }
3814562236bSHarry Wentland 
38221e67d4dSHarry Wentland void resource_unreference_clock_source(
3834562236bSHarry Wentland 		struct resource_context *res_ctx,
384a2b8659dSTony Cheng 		const struct resource_pool *pool,
3854a629536SHarry Wentland 		struct clock_source *clock_source)
3864562236bSHarry Wentland {
387ad8960a6SMikita Lipski 	int i = find_matching_clock_source(pool, clock_source);
3884a629536SHarry Wentland 
389ad8960a6SMikita Lipski 	if (i > -1)
3904562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]--;
3914562236bSHarry Wentland 
39221e67d4dSHarry Wentland 	if (pool->dp_clock_source == clock_source)
3934562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count--;
3944562236bSHarry Wentland }
3954562236bSHarry Wentland 
3964562236bSHarry Wentland void resource_reference_clock_source(
3974562236bSHarry Wentland 		struct resource_context *res_ctx,
398a2b8659dSTony Cheng 		const struct resource_pool *pool,
3994562236bSHarry Wentland 		struct clock_source *clock_source)
4004562236bSHarry Wentland {
401ad8960a6SMikita Lipski 	int i = find_matching_clock_source(pool, clock_source);
4024562236bSHarry Wentland 
403ad8960a6SMikita Lipski 	if (i > -1)
4044562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]++;
4054562236bSHarry Wentland 
406a2b8659dSTony Cheng 	if (pool->dp_clock_source == clock_source)
4074562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count++;
4084562236bSHarry Wentland }
4094562236bSHarry Wentland 
410ad8960a6SMikita Lipski int resource_get_clock_source_reference(
411ad8960a6SMikita Lipski 		struct resource_context *res_ctx,
412ad8960a6SMikita Lipski 		const struct resource_pool *pool,
413ad8960a6SMikita Lipski 		struct clock_source *clock_source)
414ad8960a6SMikita Lipski {
415ad8960a6SMikita Lipski 	int i = find_matching_clock_source(pool, clock_source);
416ad8960a6SMikita Lipski 
417ad8960a6SMikita Lipski 	if (i > -1)
418ad8960a6SMikita Lipski 		return res_ctx->clock_source_ref_count[i];
419ad8960a6SMikita Lipski 
420ad8960a6SMikita Lipski 	if (pool->dp_clock_source == clock_source)
421ad8960a6SMikita Lipski 		return res_ctx->dp_clock_source_ref_count;
422ad8960a6SMikita Lipski 
423ad8960a6SMikita Lipski 	return -1;
424ad8960a6SMikita Lipski }
425ad8960a6SMikita Lipski 
42677a2b726SVladimir Stempen bool resource_are_vblanks_synchronizable(
42777a2b726SVladimir Stempen 	struct dc_stream_state *stream1,
42877a2b726SVladimir Stempen 	struct dc_stream_state *stream2)
42977a2b726SVladimir Stempen {
43077a2b726SVladimir Stempen 	uint32_t base60_refresh_rates[] = {10, 20, 5};
43177a2b726SVladimir Stempen 	uint8_t i;
43277a2b726SVladimir Stempen 	uint8_t rr_count = sizeof(base60_refresh_rates)/sizeof(base60_refresh_rates[0]);
433783bf403SVladimir Stempen 	uint64_t frame_time_diff;
43477a2b726SVladimir Stempen 
43577a2b726SVladimir Stempen 	if (stream1->ctx->dc->config.vblank_alignment_dto_params &&
43677a2b726SVladimir Stempen 		stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff > 0 &&
43777a2b726SVladimir Stempen 		dc_is_dp_signal(stream1->signal) &&
43877a2b726SVladimir Stempen 		dc_is_dp_signal(stream2->signal) &&
43977a2b726SVladimir Stempen 		false == stream1->has_non_synchronizable_pclk &&
44077a2b726SVladimir Stempen 		false == stream2->has_non_synchronizable_pclk &&
44177a2b726SVladimir Stempen 		stream1->timing.flags.VBLANK_SYNCHRONIZABLE &&
44277a2b726SVladimir Stempen 		stream2->timing.flags.VBLANK_SYNCHRONIZABLE) {
44377a2b726SVladimir Stempen 		/* disable refresh rates higher than 60Hz for now */
44477a2b726SVladimir Stempen 		if (stream1->timing.pix_clk_100hz*100/stream1->timing.h_total/
44577a2b726SVladimir Stempen 				stream1->timing.v_total > 60)
44677a2b726SVladimir Stempen 			return false;
44777a2b726SVladimir Stempen 		if (stream2->timing.pix_clk_100hz*100/stream2->timing.h_total/
44877a2b726SVladimir Stempen 				stream2->timing.v_total > 60)
44977a2b726SVladimir Stempen 			return false;
450783bf403SVladimir Stempen 		frame_time_diff = (uint64_t)10000 *
45177a2b726SVladimir Stempen 			stream1->timing.h_total *
45277a2b726SVladimir Stempen 			stream1->timing.v_total *
453783bf403SVladimir Stempen 			stream2->timing.pix_clk_100hz;
454783bf403SVladimir Stempen 		frame_time_diff = div_u64(frame_time_diff, stream1->timing.pix_clk_100hz);
455783bf403SVladimir Stempen 		frame_time_diff = div_u64(frame_time_diff, stream2->timing.h_total);
456783bf403SVladimir Stempen 		frame_time_diff = div_u64(frame_time_diff, stream2->timing.v_total);
45777a2b726SVladimir Stempen 		for (i = 0; i < rr_count; i++) {
458783bf403SVladimir Stempen 			int64_t diff = (int64_t)div_u64(frame_time_diff * base60_refresh_rates[i], 10) - 10000;
45977a2b726SVladimir Stempen 
46077a2b726SVladimir Stempen 			if (diff < 0)
46177a2b726SVladimir Stempen 				diff = -diff;
46277a2b726SVladimir Stempen 			if (diff < stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff)
46377a2b726SVladimir Stempen 				return true;
46477a2b726SVladimir Stempen 		}
46577a2b726SVladimir Stempen 	}
46677a2b726SVladimir Stempen 	return false;
46777a2b726SVladimir Stempen }
46877a2b726SVladimir Stempen 
4694562236bSHarry Wentland bool resource_are_streams_timing_synchronizable(
4700971c40eSHarry Wentland 	struct dc_stream_state *stream1,
4710971c40eSHarry Wentland 	struct dc_stream_state *stream2)
4724562236bSHarry Wentland {
4734fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.h_total != stream2->timing.h_total)
4744562236bSHarry Wentland 		return false;
4754562236bSHarry Wentland 
4764fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.v_total != stream2->timing.v_total)
4774562236bSHarry Wentland 		return false;
4784562236bSHarry Wentland 
4794fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.h_addressable
4804fa086b9SLeo (Sunpeng) Li 				!= stream2->timing.h_addressable)
4814562236bSHarry Wentland 		return false;
4824562236bSHarry Wentland 
4834fa086b9SLeo (Sunpeng) Li 	if (stream1->timing.v_addressable
4844fa086b9SLeo (Sunpeng) Li 				!= stream2->timing.v_addressable)
4854562236bSHarry Wentland 		return false;
4864562236bSHarry Wentland 
4878582aea2SDavid Galiffi 	if (stream1->timing.v_front_porch
4888582aea2SDavid Galiffi 				!= stream2->timing.v_front_porch)
4898582aea2SDavid Galiffi 		return false;
4908582aea2SDavid Galiffi 
491380604e2SKen Chalmers 	if (stream1->timing.pix_clk_100hz
492380604e2SKen Chalmers 				!= stream2->timing.pix_clk_100hz)
4934562236bSHarry Wentland 		return false;
4944562236bSHarry Wentland 
4953e27e10eSMikita Lipski 	if (stream1->clamping.c_depth != stream2->clamping.c_depth)
4963e27e10eSMikita Lipski 		return false;
4973e27e10eSMikita Lipski 
4984562236bSHarry Wentland 	if (stream1->phy_pix_clk != stream2->phy_pix_clk
4997e2fe319SCharlene Liu 			&& (!dc_is_dp_signal(stream1->signal)
5007e2fe319SCharlene Liu 			|| !dc_is_dp_signal(stream2->signal)))
5014562236bSHarry Wentland 		return false;
5024562236bSHarry Wentland 
503d77f778eSCharlene Liu 	if (stream1->view_format != stream2->view_format)
504d77f778eSCharlene Liu 		return false;
505d77f778eSCharlene Liu 
5060460f9abSJun Lei 	if (stream1->ignore_msa_timing_param || stream2->ignore_msa_timing_param)
5070460f9abSJun Lei 		return false;
5080460f9abSJun Lei 
5094562236bSHarry Wentland 	return true;
5104562236bSHarry Wentland }
5113e27e10eSMikita Lipski static bool is_dp_and_hdmi_sharable(
5123e27e10eSMikita Lipski 		struct dc_stream_state *stream1,
5133e27e10eSMikita Lipski 		struct dc_stream_state *stream2)
5143e27e10eSMikita Lipski {
5153e27e10eSMikita Lipski 	if (stream1->ctx->dc->caps.disable_dp_clk_share)
5163e27e10eSMikita Lipski 		return false;
5173e27e10eSMikita Lipski 
5183e27e10eSMikita Lipski 	if (stream1->clamping.c_depth != COLOR_DEPTH_888 ||
5193e27e10eSMikita Lipski 		stream2->clamping.c_depth != COLOR_DEPTH_888)
5203e27e10eSMikita Lipski 		return false;
5213e27e10eSMikita Lipski 
5223e27e10eSMikita Lipski 	return true;
5233e27e10eSMikita Lipski 
5243e27e10eSMikita Lipski }
5254562236bSHarry Wentland 
5264562236bSHarry Wentland static bool is_sharable_clk_src(
5274562236bSHarry Wentland 	const struct pipe_ctx *pipe_with_clk_src,
5284562236bSHarry Wentland 	const struct pipe_ctx *pipe)
5294562236bSHarry Wentland {
5304562236bSHarry Wentland 	if (pipe_with_clk_src->clock_source == NULL)
5314562236bSHarry Wentland 		return false;
5324562236bSHarry Wentland 
5334562236bSHarry Wentland 	if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
5344562236bSHarry Wentland 		return false;
5354562236bSHarry Wentland 
5363e27e10eSMikita Lipski 	if (dc_is_dp_signal(pipe_with_clk_src->stream->signal) ||
5373e27e10eSMikita Lipski 		(dc_is_dp_signal(pipe->stream->signal) &&
5383e27e10eSMikita Lipski 		!is_dp_and_hdmi_sharable(pipe_with_clk_src->stream,
5393e27e10eSMikita Lipski 				     pipe->stream)))
5404562236bSHarry Wentland 		return false;
5414562236bSHarry Wentland 
5424562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
543fc69009eSMikita Lipski 			&& dc_is_dual_link_signal(pipe->stream->signal))
5444562236bSHarry Wentland 		return false;
5454562236bSHarry Wentland 
5464562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe->stream->signal)
547fc69009eSMikita Lipski 			&& dc_is_dual_link_signal(pipe_with_clk_src->stream->signal))
5484562236bSHarry Wentland 		return false;
5494562236bSHarry Wentland 
5504562236bSHarry Wentland 	if (!resource_are_streams_timing_synchronizable(
5514562236bSHarry Wentland 			pipe_with_clk_src->stream, pipe->stream))
5524562236bSHarry Wentland 		return false;
5534562236bSHarry Wentland 
5544562236bSHarry Wentland 	return true;
5554562236bSHarry Wentland }
5564562236bSHarry Wentland 
5574562236bSHarry Wentland struct clock_source *resource_find_used_clk_src_for_sharing(
5584562236bSHarry Wentland 					struct resource_context *res_ctx,
5594562236bSHarry Wentland 					struct pipe_ctx *pipe_ctx)
5604562236bSHarry Wentland {
5614562236bSHarry Wentland 	int i;
5624562236bSHarry Wentland 
5634562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
5644562236bSHarry Wentland 		if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
5654562236bSHarry Wentland 			return res_ctx->pipe_ctx[i].clock_source;
5664562236bSHarry Wentland 	}
5674562236bSHarry Wentland 
5684562236bSHarry Wentland 	return NULL;
5694562236bSHarry Wentland }
5704562236bSHarry Wentland 
5714562236bSHarry Wentland static enum pixel_format convert_pixel_format_to_dalsurface(
5724562236bSHarry Wentland 		enum surface_pixel_format surface_pixel_format)
5734562236bSHarry Wentland {
5744562236bSHarry Wentland 	enum pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
5754562236bSHarry Wentland 
5764562236bSHarry Wentland 	switch (surface_pixel_format) {
5774562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
5784562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_INDEX8;
5794562236bSHarry Wentland 		break;
5804562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
5814562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
5824562236bSHarry Wentland 		break;
5834562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
5844562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
5854562236bSHarry Wentland 		break;
5864562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
5874562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
5884562236bSHarry Wentland 		break;
5898693049aSTony Cheng 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
5904562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
5914562236bSHarry Wentland 		break;
5924562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
5934562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
5944562236bSHarry Wentland 		break;
5954562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
5964562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
5974562236bSHarry Wentland 		break;
5984562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
5994562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
6004562236bSHarry Wentland 		break;
6014562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
6024562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
6034562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_FP16;
6044562236bSHarry Wentland 		break;
6054562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
6064562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
60787449a90SAnthony Koo 		dal_pixel_format = PIXEL_FORMAT_420BPP8;
6084562236bSHarry Wentland 		break;
609ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
610ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
61187449a90SAnthony Koo 		dal_pixel_format = PIXEL_FORMAT_420BPP10;
612ffbcd19aSVitaly Prosyak 		break;
6134562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
614050cd3d6SMario Kleiner 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
6154562236bSHarry Wentland 	default:
6164562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
6174562236bSHarry Wentland 		break;
6184562236bSHarry Wentland 	}
6194562236bSHarry Wentland 	return dal_pixel_format;
6204562236bSHarry Wentland }
6214562236bSHarry Wentland 
6229b6067c0SDmytro Laktyushkin static inline void get_vp_scan_direction(
6239b6067c0SDmytro Laktyushkin 	enum dc_rotation_angle rotation,
6249b6067c0SDmytro Laktyushkin 	bool horizontal_mirror,
6259b6067c0SDmytro Laktyushkin 	bool *orthogonal_rotation,
6269b6067c0SDmytro Laktyushkin 	bool *flip_vert_scan_dir,
6279b6067c0SDmytro Laktyushkin 	bool *flip_horz_scan_dir)
6284562236bSHarry Wentland {
6299b6067c0SDmytro Laktyushkin 	*orthogonal_rotation = false;
6309b6067c0SDmytro Laktyushkin 	*flip_vert_scan_dir = false;
6319b6067c0SDmytro Laktyushkin 	*flip_horz_scan_dir = false;
6329b6067c0SDmytro Laktyushkin 	if (rotation == ROTATION_ANGLE_180) {
6339b6067c0SDmytro Laktyushkin 		*flip_vert_scan_dir = true;
6349b6067c0SDmytro Laktyushkin 		*flip_horz_scan_dir = true;
6359b6067c0SDmytro Laktyushkin 	} else if (rotation == ROTATION_ANGLE_90) {
6369b6067c0SDmytro Laktyushkin 		*orthogonal_rotation = true;
6379b6067c0SDmytro Laktyushkin 		*flip_horz_scan_dir = true;
6389b6067c0SDmytro Laktyushkin 	} else if (rotation == ROTATION_ANGLE_270) {
6399b6067c0SDmytro Laktyushkin 		*orthogonal_rotation = true;
6409b6067c0SDmytro Laktyushkin 		*flip_vert_scan_dir = true;
6419b6067c0SDmytro Laktyushkin 	}
6429b6067c0SDmytro Laktyushkin 
6439b6067c0SDmytro Laktyushkin 	if (horizontal_mirror)
6449b6067c0SDmytro Laktyushkin 		*flip_horz_scan_dir = !*flip_horz_scan_dir;
6454562236bSHarry Wentland }
6464562236bSHarry Wentland 
647570bc18cSDmytro Laktyushkin int get_num_mpc_splits(struct pipe_ctx *pipe)
648570bc18cSDmytro Laktyushkin {
649570bc18cSDmytro Laktyushkin 	int mpc_split_count = 0;
650570bc18cSDmytro Laktyushkin 	struct pipe_ctx *other_pipe = pipe->bottom_pipe;
651570bc18cSDmytro Laktyushkin 
652570bc18cSDmytro Laktyushkin 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
653570bc18cSDmytro Laktyushkin 		mpc_split_count++;
654570bc18cSDmytro Laktyushkin 		other_pipe = other_pipe->bottom_pipe;
655570bc18cSDmytro Laktyushkin 	}
656570bc18cSDmytro Laktyushkin 	other_pipe = pipe->top_pipe;
657570bc18cSDmytro Laktyushkin 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
658570bc18cSDmytro Laktyushkin 		mpc_split_count++;
659570bc18cSDmytro Laktyushkin 		other_pipe = other_pipe->top_pipe;
660570bc18cSDmytro Laktyushkin 	}
661570bc18cSDmytro Laktyushkin 
662570bc18cSDmytro Laktyushkin 	return mpc_split_count;
663570bc18cSDmytro Laktyushkin }
664570bc18cSDmytro Laktyushkin 
665228a10d4SAlex Deucher int get_num_odm_splits(struct pipe_ctx *pipe)
666228a10d4SAlex Deucher {
667228a10d4SAlex Deucher 	int odm_split_count = 0;
668228a10d4SAlex Deucher 	struct pipe_ctx *next_pipe = pipe->next_odm_pipe;
669228a10d4SAlex Deucher 	while (next_pipe) {
670228a10d4SAlex Deucher 		odm_split_count++;
671228a10d4SAlex Deucher 		next_pipe = next_pipe->next_odm_pipe;
672228a10d4SAlex Deucher 	}
673228a10d4SAlex Deucher 	pipe = pipe->prev_odm_pipe;
674228a10d4SAlex Deucher 	while (pipe) {
675228a10d4SAlex Deucher 		odm_split_count++;
676228a10d4SAlex Deucher 		pipe = pipe->prev_odm_pipe;
677228a10d4SAlex Deucher 	}
678228a10d4SAlex Deucher 	return odm_split_count;
679228a10d4SAlex Deucher }
680228a10d4SAlex Deucher 
6815bf24270SDmytro Laktyushkin static void calculate_split_count_and_index(struct pipe_ctx *pipe_ctx, int *split_count, int *split_idx)
6825bf24270SDmytro Laktyushkin {
6835bf24270SDmytro Laktyushkin 	*split_count = get_num_odm_splits(pipe_ctx);
6845bf24270SDmytro Laktyushkin 	*split_idx = 0;
6855bf24270SDmytro Laktyushkin 	if (*split_count == 0) {
6865bf24270SDmytro Laktyushkin 		/*Check for mpc split*/
6875bf24270SDmytro Laktyushkin 		struct pipe_ctx *split_pipe = pipe_ctx->top_pipe;
6885bf24270SDmytro Laktyushkin 
689570bc18cSDmytro Laktyushkin 		*split_count = get_num_mpc_splits(pipe_ctx);
6905bf24270SDmytro Laktyushkin 		while (split_pipe && split_pipe->plane_state == pipe_ctx->plane_state) {
6915bf24270SDmytro Laktyushkin 			(*split_idx)++;
6925bf24270SDmytro Laktyushkin 			split_pipe = split_pipe->top_pipe;
6935bf24270SDmytro Laktyushkin 		}
6945bf24270SDmytro Laktyushkin 	} else {
6955bf24270SDmytro Laktyushkin 		/*Get odm split index*/
6965bf24270SDmytro Laktyushkin 		struct pipe_ctx *split_pipe = pipe_ctx->prev_odm_pipe;
6975bf24270SDmytro Laktyushkin 
6985bf24270SDmytro Laktyushkin 		while (split_pipe) {
6995bf24270SDmytro Laktyushkin 			(*split_idx)++;
7005bf24270SDmytro Laktyushkin 			split_pipe = split_pipe->prev_odm_pipe;
7015bf24270SDmytro Laktyushkin 		}
7025bf24270SDmytro Laktyushkin 	}
7035bf24270SDmytro Laktyushkin }
7045bf24270SDmytro Laktyushkin 
7059b6067c0SDmytro Laktyushkin /*
7066566cae7SDmytro Laktyushkin  * This is a preliminary vp size calculation to allow us to check taps support.
7076566cae7SDmytro Laktyushkin  * The result is completely overridden afterwards.
7089b6067c0SDmytro Laktyushkin  */
7096566cae7SDmytro Laktyushkin static void calculate_viewport_size(struct pipe_ctx *pipe_ctx)
7106566cae7SDmytro Laktyushkin {
7116566cae7SDmytro Laktyushkin 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
7129b6067c0SDmytro Laktyushkin 
7136566cae7SDmytro Laktyushkin 	data->viewport.width = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.horz, data->recout.width));
7146566cae7SDmytro Laktyushkin 	data->viewport.height = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.vert, data->recout.height));
7156566cae7SDmytro Laktyushkin 	data->viewport_c.width = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.horz_c, data->recout.width));
7166566cae7SDmytro Laktyushkin 	data->viewport_c.height = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.vert_c, data->recout.height));
7176566cae7SDmytro Laktyushkin 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
7186566cae7SDmytro Laktyushkin 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270) {
7196566cae7SDmytro Laktyushkin 		swap(data->viewport.width, data->viewport.height);
7206566cae7SDmytro Laktyushkin 		swap(data->viewport_c.width, data->viewport_c.height);
7219b6067c0SDmytro Laktyushkin 	}
722b2d0a103SDmytro Laktyushkin }
7231fbd2cfcSDmytro Laktyushkin 
7249b6067c0SDmytro Laktyushkin static void calculate_recout(struct pipe_ctx *pipe_ctx)
7254562236bSHarry Wentland {
7263be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
7270971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
7285bf24270SDmytro Laktyushkin 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
7293be5262eSHarry Wentland 	struct rect surf_clip = plane_state->clip_rect;
7306566cae7SDmytro Laktyushkin 	bool split_tb = stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM;
7316566cae7SDmytro Laktyushkin 	int split_count, split_idx;
7324562236bSHarry Wentland 
7335bf24270SDmytro Laktyushkin 	calculate_split_count_and_index(pipe_ctx, &split_count, &split_idx);
7346566cae7SDmytro Laktyushkin 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
7356566cae7SDmytro Laktyushkin 		split_idx = 0;
7365bf24270SDmytro Laktyushkin 
73705e3d830SWesley Chalmers 	/*
73805e3d830SWesley Chalmers 	 * Only the leftmost ODM pipe should be offset by a nonzero distance
73905e3d830SWesley Chalmers 	 */
7406566cae7SDmytro Laktyushkin 	if (!pipe_ctx->prev_odm_pipe || split_idx == split_count) {
7415bf24270SDmytro Laktyushkin 		data->recout.x = stream->dst.x;
7424fa086b9SLeo (Sunpeng) Li 		if (stream->src.x < surf_clip.x)
7435bf24270SDmytro Laktyushkin 			data->recout.x += (surf_clip.x - stream->src.x) * stream->dst.width
7444fa086b9SLeo (Sunpeng) Li 						/ stream->src.width;
74525b31581SWesley Chalmers 	} else
74625b31581SWesley Chalmers 		data->recout.x = 0;
74725b31581SWesley Chalmers 
74884aef2abSDmytro Laktyushkin 	if (stream->src.x > surf_clip.x)
74984aef2abSDmytro Laktyushkin 		surf_clip.width -= stream->src.x - surf_clip.x;
7505bf24270SDmytro Laktyushkin 	data->recout.width = surf_clip.width * stream->dst.width / stream->src.width;
7515bf24270SDmytro Laktyushkin 	if (data->recout.width + data->recout.x > stream->dst.x + stream->dst.width)
7525bf24270SDmytro Laktyushkin 		data->recout.width = stream->dst.x + stream->dst.width - data->recout.x;
7534562236bSHarry Wentland 
7545bf24270SDmytro Laktyushkin 	data->recout.y = stream->dst.y;
7554fa086b9SLeo (Sunpeng) Li 	if (stream->src.y < surf_clip.y)
7565bf24270SDmytro Laktyushkin 		data->recout.y += (surf_clip.y - stream->src.y) * stream->dst.height
7574fa086b9SLeo (Sunpeng) Li 						/ stream->src.height;
75884aef2abSDmytro Laktyushkin 	else if (stream->src.y > surf_clip.y)
75984aef2abSDmytro Laktyushkin 		surf_clip.height -= stream->src.y - surf_clip.y;
7604562236bSHarry Wentland 
7615bf24270SDmytro Laktyushkin 	data->recout.height = surf_clip.height * stream->dst.height / stream->src.height;
7625bf24270SDmytro Laktyushkin 	if (data->recout.height + data->recout.y > stream->dst.y + stream->dst.height)
7635bf24270SDmytro Laktyushkin 		data->recout.height = stream->dst.y + stream->dst.height - data->recout.y;
764b2d0a103SDmytro Laktyushkin 
7656566cae7SDmytro Laktyushkin 	/* Handle h & v split */
7666566cae7SDmytro Laktyushkin 	if (split_tb) {
7676566cae7SDmytro Laktyushkin 		ASSERT(data->recout.height % 2 == 0);
7685bf24270SDmytro Laktyushkin 		data->recout.height /= 2;
7696566cae7SDmytro Laktyushkin 	} else if (split_count) {
7706566cae7SDmytro Laktyushkin 		if (!pipe_ctx->next_odm_pipe && !pipe_ctx->prev_odm_pipe) {
7715bf24270SDmytro Laktyushkin 			/* extra pixels in the division remainder need to go to pipes after
7725bf24270SDmytro Laktyushkin 			 * the extra pixel index minus one(epimo) defined here as:
7735bf24270SDmytro Laktyushkin 			 */
7745bf24270SDmytro Laktyushkin 			int epimo = split_count - data->recout.width % (split_count + 1);
7755bf24270SDmytro Laktyushkin 
7765bf24270SDmytro Laktyushkin 			data->recout.x += (data->recout.width / (split_count + 1)) * split_idx;
7775bf24270SDmytro Laktyushkin 			if (split_idx > epimo)
7785bf24270SDmytro Laktyushkin 				data->recout.x += split_idx - epimo - 1;
7796566cae7SDmytro Laktyushkin 			ASSERT(stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE || data->recout.width % 2 == 0);
7805bf24270SDmytro Laktyushkin 			data->recout.width = data->recout.width / (split_count + 1) + (split_idx > epimo ? 1 : 0);
7816566cae7SDmytro Laktyushkin 		} else {
7826566cae7SDmytro Laktyushkin 			/* odm */
7836566cae7SDmytro Laktyushkin 			if (split_idx == split_count) {
7846566cae7SDmytro Laktyushkin 				/* rightmost pipe is the remainder recout */
7856566cae7SDmytro Laktyushkin 				data->recout.width -= data->h_active * split_count - data->recout.x;
7866566cae7SDmytro Laktyushkin 				data->recout.x = 0;
7876566cae7SDmytro Laktyushkin 			} else
7886566cae7SDmytro Laktyushkin 				data->recout.width = data->h_active - data->recout.x;
7896566cae7SDmytro Laktyushkin 		}
7905bf24270SDmytro Laktyushkin 	}
7914562236bSHarry Wentland }
792b2d0a103SDmytro Laktyushkin 
793b2d0a103SDmytro Laktyushkin static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
7944562236bSHarry Wentland {
7953be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
7960971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
7973be5262eSHarry Wentland 	struct rect surf_src = plane_state->src_rect;
7984fa086b9SLeo (Sunpeng) Li 	const int in_w = stream->src.width;
7994fa086b9SLeo (Sunpeng) Li 	const int in_h = stream->src.height;
8004fa086b9SLeo (Sunpeng) Li 	const int out_w = stream->dst.width;
8014fa086b9SLeo (Sunpeng) Li 	const int out_h = stream->dst.height;
8024562236bSHarry Wentland 
8039b6067c0SDmytro Laktyushkin 	/*Swap surf_src height and width since scaling ratios are in recout rotation*/
8043be5262eSHarry Wentland 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
8053be5262eSHarry Wentland 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
8069b6067c0SDmytro Laktyushkin 		swap(surf_src.height, surf_src.width);
80786006a7fSDmytro Laktyushkin 
808eb0e5154SDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_from_fraction(
80986006a7fSDmytro Laktyushkin 					surf_src.width,
8103be5262eSHarry Wentland 					plane_state->dst_rect.width);
811eb0e5154SDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_from_fraction(
81286006a7fSDmytro Laktyushkin 					surf_src.height,
8133be5262eSHarry Wentland 					plane_state->dst_rect.height);
8144562236bSHarry Wentland 
8154fa086b9SLeo (Sunpeng) Li 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
8166702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz.value *= 2;
8174fa086b9SLeo (Sunpeng) Li 	else if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
8186702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert.value *= 2;
8194562236bSHarry Wentland 
8206702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.vert.value = div64_s64(
8216702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert.value * in_h, out_h);
8226702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.horz.value = div64_s64(
8236702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz.value * in_w, out_w);
8244562236bSHarry Wentland 
8256702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.horz_c = pipe_ctx->plane_res.scl_data.ratios.horz;
8266702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.ratios.vert_c = pipe_ctx->plane_res.scl_data.ratios.vert;
8274562236bSHarry Wentland 
8286702a9acSHarry Wentland 	if (pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP8
8296702a9acSHarry Wentland 			|| pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP10) {
8306702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.horz_c.value /= 2;
8316702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.ratios.vert_c.value /= 2;
8324562236bSHarry Wentland 	}
8330002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_truncate(
8340002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.horz, 19);
8350002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_truncate(
8360002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.vert, 19);
8370002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.horz_c = dc_fixpt_truncate(
8380002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.horz_c, 19);
8390002d3acSDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.ratios.vert_c = dc_fixpt_truncate(
8400002d3acSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.ratios.vert_c, 19);
8414562236bSHarry Wentland }
8424562236bSHarry Wentland 
8436566cae7SDmytro Laktyushkin 
8446566cae7SDmytro Laktyushkin /*
8456566cae7SDmytro Laktyushkin  * We completely calculate vp offset, size and inits here based entirely on scaling
8466566cae7SDmytro Laktyushkin  * ratios and recout for pixel perfect pipe combine.
8476566cae7SDmytro Laktyushkin  */
8486566cae7SDmytro Laktyushkin static void calculate_init_and_vp(
8499b6067c0SDmytro Laktyushkin 		bool flip_scan_dir,
8506566cae7SDmytro Laktyushkin 		int recout_offset_within_recout_full,
8516566cae7SDmytro Laktyushkin 		int recout_size,
8529b6067c0SDmytro Laktyushkin 		int src_size,
8539b6067c0SDmytro Laktyushkin 		int taps,
8549b6067c0SDmytro Laktyushkin 		struct fixed31_32 ratio,
8559b6067c0SDmytro Laktyushkin 		struct fixed31_32 *init,
8569b6067c0SDmytro Laktyushkin 		int *vp_offset,
8579b6067c0SDmytro Laktyushkin 		int *vp_size)
8584562236bSHarry Wentland {
8596566cae7SDmytro Laktyushkin 	struct fixed31_32 temp;
8609b6067c0SDmytro Laktyushkin 	int int_part;
8619b6067c0SDmytro Laktyushkin 
8626566cae7SDmytro Laktyushkin 	/*
8636566cae7SDmytro Laktyushkin 	 * First of the taps starts sampling pixel number <init_int_part> corresponding to recout
8646566cae7SDmytro Laktyushkin 	 * pixel 1. Next recout pixel samples int part of <init + scaling ratio> and so on.
8656566cae7SDmytro Laktyushkin 	 * All following calculations are based on this logic.
8666566cae7SDmytro Laktyushkin 	 *
8676566cae7SDmytro Laktyushkin 	 * Init calculated according to formula:
8686566cae7SDmytro Laktyushkin 	 * 	init = (scaling_ratio + number_of_taps + 1) / 2
8696566cae7SDmytro Laktyushkin 	 * 	init_bot = init + scaling_ratio
8706566cae7SDmytro Laktyushkin 	 * 	to get pixel perfect combine add the fraction from calculating vp offset
8716566cae7SDmytro Laktyushkin 	 */
8726566cae7SDmytro Laktyushkin 	temp = dc_fixpt_mul_int(ratio, recout_offset_within_recout_full);
8736566cae7SDmytro Laktyushkin 	*vp_offset = dc_fixpt_floor(temp);
8746566cae7SDmytro Laktyushkin 	temp.value &= 0xffffffff;
8756566cae7SDmytro Laktyushkin 	*init = dc_fixpt_truncate(dc_fixpt_add(dc_fixpt_div_int(
8766566cae7SDmytro Laktyushkin 			dc_fixpt_add_int(ratio, taps + 1), 2), temp), 19);
8776566cae7SDmytro Laktyushkin 	/*
8786566cae7SDmytro Laktyushkin 	 * If viewport has non 0 offset and there are more taps than covered by init then
8796566cae7SDmytro Laktyushkin 	 * we should decrease the offset and increase init so we are never sampling
8806566cae7SDmytro Laktyushkin 	 * outside of viewport.
8816566cae7SDmytro Laktyushkin 	 */
8826566cae7SDmytro Laktyushkin 	int_part = dc_fixpt_floor(*init);
8839b6067c0SDmytro Laktyushkin 	if (int_part < taps) {
8846566cae7SDmytro Laktyushkin 		int_part = taps - int_part;
8856566cae7SDmytro Laktyushkin 		if (int_part > *vp_offset)
8866566cae7SDmytro Laktyushkin 			int_part = *vp_offset;
8876566cae7SDmytro Laktyushkin 		*vp_offset -= int_part;
8889b6067c0SDmytro Laktyushkin 		*init = dc_fixpt_add_int(*init, int_part);
8899b6067c0SDmytro Laktyushkin 	}
8909b6067c0SDmytro Laktyushkin 	/*
8916566cae7SDmytro Laktyushkin 	 * If taps are sampling outside of viewport at end of recout and there are more pixels
8926566cae7SDmytro Laktyushkin 	 * available in the surface we should increase the viewport size, regardless set vp to
8936566cae7SDmytro Laktyushkin 	 * only what is used.
8949b6067c0SDmytro Laktyushkin 	 */
8956566cae7SDmytro Laktyushkin 	temp = dc_fixpt_add(*init, dc_fixpt_mul_int(ratio, recout_size - 1));
8966566cae7SDmytro Laktyushkin 	*vp_size = dc_fixpt_floor(temp);
8976566cae7SDmytro Laktyushkin 	if (*vp_size + *vp_offset > src_size)
8986566cae7SDmytro Laktyushkin 		*vp_size = src_size - *vp_offset;
8996566cae7SDmytro Laktyushkin 
9006566cae7SDmytro Laktyushkin 	/* We did all the math assuming we are scanning same direction as display does,
9016566cae7SDmytro Laktyushkin 	 * however mirror/rotation changes how vp scans vs how it is offset. If scan direction
9026566cae7SDmytro Laktyushkin 	 * is flipped we simply need to calculate offset from the other side of plane.
9036566cae7SDmytro Laktyushkin 	 * Note that outside of viewport all scaling hardware works in recout space.
9049b6067c0SDmytro Laktyushkin 	 */
9056566cae7SDmytro Laktyushkin 	if (flip_scan_dir)
9066566cae7SDmytro Laktyushkin 		*vp_offset = src_size - *vp_offset - *vp_size;
9079b6067c0SDmytro Laktyushkin }
9089b6067c0SDmytro Laktyushkin 
9096566cae7SDmytro Laktyushkin static void calculate_inits_and_viewports(struct pipe_ctx *pipe_ctx)
9109b6067c0SDmytro Laktyushkin {
9119b6067c0SDmytro Laktyushkin 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
9129b6067c0SDmytro Laktyushkin 	const struct dc_stream_state *stream = pipe_ctx->stream;
9136702a9acSHarry Wentland 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
9146566cae7SDmytro Laktyushkin 	struct rect src = plane_state->src_rect;
91587449a90SAnthony Koo 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
91687449a90SAnthony Koo 				|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
9176566cae7SDmytro Laktyushkin 	int split_count, split_idx, ro_lb, ro_tb, recout_full_x, recout_full_y;
9189b6067c0SDmytro Laktyushkin 	bool orthogonal_rotation, flip_vert_scan_dir, flip_horz_scan_dir;
9196566cae7SDmytro Laktyushkin 
9206566cae7SDmytro Laktyushkin 	calculate_split_count_and_index(pipe_ctx, &split_count, &split_idx);
9216566cae7SDmytro Laktyushkin 	/*
9226566cae7SDmytro Laktyushkin 	 * recout full is what the recout would have been if we didnt clip
9236566cae7SDmytro Laktyushkin 	 * the source plane at all. We only care about left(ro_lb) and top(ro_tb)
9246566cae7SDmytro Laktyushkin 	 * offsets of recout within recout full because those are the directions
9256566cae7SDmytro Laktyushkin 	 * we scan from and therefore the only ones that affect inits.
9266566cae7SDmytro Laktyushkin 	 */
9276566cae7SDmytro Laktyushkin 	recout_full_x = stream->dst.x + (plane_state->dst_rect.x - stream->src.x)
9286566cae7SDmytro Laktyushkin 			* stream->dst.width / stream->src.width;
9296566cae7SDmytro Laktyushkin 	recout_full_y = stream->dst.y + (plane_state->dst_rect.y - stream->src.y)
9306566cae7SDmytro Laktyushkin 			* stream->dst.height / stream->src.height;
9316566cae7SDmytro Laktyushkin 	if (pipe_ctx->prev_odm_pipe && split_idx)
9326566cae7SDmytro Laktyushkin 		ro_lb = data->h_active * split_idx - recout_full_x;
9336566cae7SDmytro Laktyushkin 	else
9346566cae7SDmytro Laktyushkin 		ro_lb = data->recout.x - recout_full_x;
9356566cae7SDmytro Laktyushkin 	ro_tb = data->recout.y - recout_full_y;
9366566cae7SDmytro Laktyushkin 	ASSERT(ro_lb >= 0 && ro_tb >= 0);
937b2d0a103SDmytro Laktyushkin 
938b0131391SDmytro Laktyushkin 	/*
9396566cae7SDmytro Laktyushkin 	 * Work in recout rotation since that requires less transformations
940b0131391SDmytro Laktyushkin 	 */
9419b6067c0SDmytro Laktyushkin 	get_vp_scan_direction(
9429b6067c0SDmytro Laktyushkin 			plane_state->rotation,
9439b6067c0SDmytro Laktyushkin 			plane_state->horizontal_mirror,
9449b6067c0SDmytro Laktyushkin 			&orthogonal_rotation,
9459b6067c0SDmytro Laktyushkin 			&flip_vert_scan_dir,
9469b6067c0SDmytro Laktyushkin 			&flip_horz_scan_dir);
947b0131391SDmytro Laktyushkin 
9489b6067c0SDmytro Laktyushkin 	if (orthogonal_rotation) {
9499b6067c0SDmytro Laktyushkin 		swap(src.width, src.height);
9506566cae7SDmytro Laktyushkin 		swap(flip_vert_scan_dir, flip_horz_scan_dir);
9519b5349f7SMartin Tsai 	}
9521fbd2cfcSDmytro Laktyushkin 
9536566cae7SDmytro Laktyushkin 	calculate_init_and_vp(
9549b6067c0SDmytro Laktyushkin 			flip_horz_scan_dir,
9556566cae7SDmytro Laktyushkin 			ro_lb,
9566566cae7SDmytro Laktyushkin 			data->recout.width,
9576566cae7SDmytro Laktyushkin 			src.width,
9586566cae7SDmytro Laktyushkin 			data->taps.h_taps,
9596566cae7SDmytro Laktyushkin 			data->ratios.horz,
9606566cae7SDmytro Laktyushkin 			&data->inits.h,
9619b6067c0SDmytro Laktyushkin 			&data->viewport.x,
9629b6067c0SDmytro Laktyushkin 			&data->viewport.width);
9636566cae7SDmytro Laktyushkin 	calculate_init_and_vp(
9649b6067c0SDmytro Laktyushkin 			flip_horz_scan_dir,
9656566cae7SDmytro Laktyushkin 			ro_lb,
9666566cae7SDmytro Laktyushkin 			data->recout.width,
9676566cae7SDmytro Laktyushkin 			src.width / vpc_div,
9686566cae7SDmytro Laktyushkin 			data->taps.h_taps_c,
9696566cae7SDmytro Laktyushkin 			data->ratios.horz_c,
9706566cae7SDmytro Laktyushkin 			&data->inits.h_c,
9719b6067c0SDmytro Laktyushkin 			&data->viewport_c.x,
9729b6067c0SDmytro Laktyushkin 			&data->viewport_c.width);
9736566cae7SDmytro Laktyushkin 	calculate_init_and_vp(
9749b6067c0SDmytro Laktyushkin 			flip_vert_scan_dir,
9756566cae7SDmytro Laktyushkin 			ro_tb,
9766566cae7SDmytro Laktyushkin 			data->recout.height,
9776566cae7SDmytro Laktyushkin 			src.height,
9786566cae7SDmytro Laktyushkin 			data->taps.v_taps,
9796566cae7SDmytro Laktyushkin 			data->ratios.vert,
9806566cae7SDmytro Laktyushkin 			&data->inits.v,
9819b6067c0SDmytro Laktyushkin 			&data->viewport.y,
9829b6067c0SDmytro Laktyushkin 			&data->viewport.height);
9836566cae7SDmytro Laktyushkin 	calculate_init_and_vp(
9849b6067c0SDmytro Laktyushkin 			flip_vert_scan_dir,
9856566cae7SDmytro Laktyushkin 			ro_tb,
9866566cae7SDmytro Laktyushkin 			data->recout.height,
9876566cae7SDmytro Laktyushkin 			src.height / vpc_div,
9886566cae7SDmytro Laktyushkin 			data->taps.v_taps_c,
9896566cae7SDmytro Laktyushkin 			data->ratios.vert_c,
9906566cae7SDmytro Laktyushkin 			&data->inits.v_c,
9919b6067c0SDmytro Laktyushkin 			&data->viewport_c.y,
9929b6067c0SDmytro Laktyushkin 			&data->viewport_c.height);
9936566cae7SDmytro Laktyushkin 	if (orthogonal_rotation) {
9946566cae7SDmytro Laktyushkin 		swap(data->viewport.x, data->viewport.y);
9956566cae7SDmytro Laktyushkin 		swap(data->viewport.width, data->viewport.height);
9966566cae7SDmytro Laktyushkin 		swap(data->viewport_c.x, data->viewport_c.y);
9976566cae7SDmytro Laktyushkin 		swap(data->viewport_c.width, data->viewport_c.height);
998b2d0a103SDmytro Laktyushkin 	}
9996566cae7SDmytro Laktyushkin 	data->viewport.x += src.x;
10006566cae7SDmytro Laktyushkin 	data->viewport.y += src.y;
10016566cae7SDmytro Laktyushkin 	ASSERT(src.x % vpc_div == 0 && src.y % vpc_div == 0);
10026566cae7SDmytro Laktyushkin 	data->viewport_c.x += src.x / vpc_div;
10036566cae7SDmytro Laktyushkin 	data->viewport_c.y += src.y / vpc_div;
100489d07b66SSamson Tam }
100589d07b66SSamson Tam 
1006b2d0a103SDmytro Laktyushkin bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
1007b2d0a103SDmytro Laktyushkin {
10083be5262eSHarry Wentland 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
10094fa086b9SLeo (Sunpeng) Li 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
1010b2d0a103SDmytro Laktyushkin 	bool res = false;
10115d4b05ddSBhawanpreet Lakha 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
10126566cae7SDmytro Laktyushkin 
10136702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
10143be5262eSHarry Wentland 			pipe_ctx->plane_state->format);
10154562236bSHarry Wentland 
10166566cae7SDmytro Laktyushkin 	/* Timing borders are part of vactive that we are also supposed to skip in addition
10176566cae7SDmytro Laktyushkin 	 * to any stream dst offset. Since dm logic assumes dst is in addressable
10186566cae7SDmytro Laktyushkin 	 * space we need to add the the left and top borders to dst offsets temporarily.
10196566cae7SDmytro Laktyushkin 	 * TODO: fix in DM, stream dst is supposed to be in vactive
10206566cae7SDmytro Laktyushkin 	 */
10216566cae7SDmytro Laktyushkin 	pipe_ctx->stream->dst.x += timing->h_border_left;
10226566cae7SDmytro Laktyushkin 	pipe_ctx->stream->dst.y += timing->v_border_top;
1023b2d0a103SDmytro Laktyushkin 
10246566cae7SDmytro Laktyushkin 	/* Calculate H and V active size */
10256566cae7SDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.h_active = timing->h_addressable +
10266566cae7SDmytro Laktyushkin 			timing->h_border_left + timing->h_border_right;
10276566cae7SDmytro Laktyushkin 	pipe_ctx->plane_res.scl_data.v_active = timing->v_addressable +
10286566cae7SDmytro Laktyushkin 		timing->v_border_top + timing->v_border_bottom;
10296566cae7SDmytro Laktyushkin 	if (pipe_ctx->next_odm_pipe || pipe_ctx->prev_odm_pipe)
10306566cae7SDmytro Laktyushkin 		pipe_ctx->plane_res.scl_data.h_active /= get_num_odm_splits(pipe_ctx) + 1;
10314562236bSHarry Wentland 
10326566cae7SDmytro Laktyushkin 	/* depends on h_active */
10339b6067c0SDmytro Laktyushkin 	calculate_recout(pipe_ctx);
10346566cae7SDmytro Laktyushkin 	/* depends on pixel format */
10356566cae7SDmytro Laktyushkin 	calculate_scaling_ratios(pipe_ctx);
10366566cae7SDmytro Laktyushkin 	/* depends on scaling ratios and recout, does not calculate offset yet */
10376566cae7SDmytro Laktyushkin 	calculate_viewport_size(pipe_ctx);
10384562236bSHarry Wentland 
10396566cae7SDmytro Laktyushkin 	/*
10406566cae7SDmytro Laktyushkin 	 * LB calculations depend on vp size, h/v_active and scaling ratios
10414562236bSHarry Wentland 	 * Setting line buffer pixel depth to 24bpp yields banding
1042a316db72SMario Kleiner 	 * on certain displays, such as the Sharp 4k. 36bpp is needed
1043a316db72SMario Kleiner 	 * to support SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616 and
1044a316db72SMario Kleiner 	 * SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616 with actual > 10 bpc
104572a7cf0aSMario Kleiner 	 * precision on at least DCN display engines. However, at least
104672a7cf0aSMario Kleiner 	 * Carrizo with DCE_VERSION_11_0 does not like 36 bpp lb depth,
104772a7cf0aSMario Kleiner 	 * so use only 30 bpp on DCE_VERSION_11_0. Testing with DCE 11.2 and 8.3
104872a7cf0aSMario Kleiner 	 * did not show such problems, so this seems to be the exception.
10494562236bSHarry Wentland 	 */
105072a7cf0aSMario Kleiner 	if (plane_state->ctx->dce_version != DCE_VERSION_11_0)
1051a316db72SMario Kleiner 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_36BPP;
105272a7cf0aSMario Kleiner 	else
105372a7cf0aSMario Kleiner 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
105472a7cf0aSMario Kleiner 
105533eef72fSEric Bernstein 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
10564562236bSHarry Wentland 
1057d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.xfm != NULL)
105886a66c4eSHarry Wentland 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
105986a66c4eSHarry Wentland 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
10604562236bSHarry Wentland 
1061d94585a0SYue Hin Lau 	if (pipe_ctx->plane_res.dpp != NULL)
1062d94585a0SYue Hin Lau 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1063d94585a0SYue Hin Lau 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1064f7938bc0SReza Amini 
1065f7938bc0SReza Amini 
10664562236bSHarry Wentland 	if (!res) {
10674562236bSHarry Wentland 		/* Try 24 bpp linebuffer */
10686702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
10694562236bSHarry Wentland 
10701b6c8067SBhawanpreet Lakha 		if (pipe_ctx->plane_res.xfm != NULL)
107186a66c4eSHarry Wentland 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
10721b6c8067SBhawanpreet Lakha 					pipe_ctx->plane_res.xfm,
10731b6c8067SBhawanpreet Lakha 					&pipe_ctx->plane_res.scl_data,
10741b6c8067SBhawanpreet Lakha 					&plane_state->scaling_quality);
1075d94585a0SYue Hin Lau 
10761b6c8067SBhawanpreet Lakha 		if (pipe_ctx->plane_res.dpp != NULL)
1077d94585a0SYue Hin Lau 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
10781b6c8067SBhawanpreet Lakha 					pipe_ctx->plane_res.dpp,
10791b6c8067SBhawanpreet Lakha 					&pipe_ctx->plane_res.scl_data,
10801b6c8067SBhawanpreet Lakha 					&plane_state->scaling_quality);
10814562236bSHarry Wentland 	}
10824562236bSHarry Wentland 
10836566cae7SDmytro Laktyushkin 	/*
10846566cae7SDmytro Laktyushkin 	 * Depends on recout, scaling ratios, h_active and taps
10856566cae7SDmytro Laktyushkin 	 * May need to re-check lb size after this in some obscure scenario
10866566cae7SDmytro Laktyushkin 	 */
1087b2d0a103SDmytro Laktyushkin 	if (res)
10886566cae7SDmytro Laktyushkin 		calculate_inits_and_viewports(pipe_ctx);
10896566cae7SDmytro Laktyushkin 
10906566cae7SDmytro Laktyushkin 	/*
10916566cae7SDmytro Laktyushkin 	 * Handle side by side and top bottom 3d recout offsets after vp calculation
10926566cae7SDmytro Laktyushkin 	 * since 3d is special and needs to calculate vp as if there is no recout offset
10936566cae7SDmytro Laktyushkin 	 * This may break with rotation, good thing we aren't mixing hw rotation and 3d
10946566cae7SDmytro Laktyushkin 	 */
10956566cae7SDmytro Laktyushkin 	if (pipe_ctx->top_pipe && pipe_ctx->top_pipe->plane_state == plane_state) {
10966566cae7SDmytro Laktyushkin 		ASSERT(plane_state->rotation == ROTATION_ANGLE_0 ||
10976566cae7SDmytro Laktyushkin 			(pipe_ctx->stream->view_format != VIEW_3D_FORMAT_TOP_AND_BOTTOM &&
10986566cae7SDmytro Laktyushkin 				pipe_ctx->stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE));
10996566cae7SDmytro Laktyushkin 		if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
11006566cae7SDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.recout.y += pipe_ctx->plane_res.scl_data.recout.height;
11016566cae7SDmytro Laktyushkin 		else if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
11026566cae7SDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.recout.x += pipe_ctx->plane_res.scl_data.recout.width;
11036566cae7SDmytro Laktyushkin 	}
11046566cae7SDmytro Laktyushkin 
11056566cae7SDmytro Laktyushkin 	if (pipe_ctx->plane_res.scl_data.viewport.height < MIN_VIEWPORT_SIZE ||
11066566cae7SDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.viewport.width < MIN_VIEWPORT_SIZE)
11076566cae7SDmytro Laktyushkin 		res = false;
1108b2d0a103SDmytro Laktyushkin 
11093c0dcf9fSDmytro Laktyushkin 	DC_LOG_SCALER("%s pipe %d:\nViewport: height:%d width:%d x:%d y:%d  Recout: height:%d width:%d x:%d y:%d  HACTIVE:%d VACTIVE:%d\n"
11103c0dcf9fSDmytro Laktyushkin 			"src_rect: height:%d width:%d x:%d y:%d  dst_rect: height:%d width:%d x:%d y:%d  clip_rect: height:%d width:%d x:%d y:%d\n",
11114562236bSHarry Wentland 			__func__,
11123c0dcf9fSDmytro Laktyushkin 			pipe_ctx->pipe_idx,
11136702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.height,
11146702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.width,
11156702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.x,
11166702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.y,
11173c0dcf9fSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.recout.height,
11183c0dcf9fSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.recout.width,
11193c0dcf9fSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.recout.x,
11203c0dcf9fSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.recout.y,
11213c0dcf9fSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.h_active,
11223c0dcf9fSDmytro Laktyushkin 			pipe_ctx->plane_res.scl_data.v_active,
11233c0dcf9fSDmytro Laktyushkin 			plane_state->src_rect.height,
11243c0dcf9fSDmytro Laktyushkin 			plane_state->src_rect.width,
11253c0dcf9fSDmytro Laktyushkin 			plane_state->src_rect.x,
11263c0dcf9fSDmytro Laktyushkin 			plane_state->src_rect.y,
11273be5262eSHarry Wentland 			plane_state->dst_rect.height,
11283be5262eSHarry Wentland 			plane_state->dst_rect.width,
11293be5262eSHarry Wentland 			plane_state->dst_rect.x,
11303c0dcf9fSDmytro Laktyushkin 			plane_state->dst_rect.y,
11313c0dcf9fSDmytro Laktyushkin 			plane_state->clip_rect.height,
11323c0dcf9fSDmytro Laktyushkin 			plane_state->clip_rect.width,
11333c0dcf9fSDmytro Laktyushkin 			plane_state->clip_rect.x,
11343c0dcf9fSDmytro Laktyushkin 			plane_state->clip_rect.y);
11354562236bSHarry Wentland 
11366566cae7SDmytro Laktyushkin 	pipe_ctx->stream->dst.x -= timing->h_border_left;
11376566cae7SDmytro Laktyushkin 	pipe_ctx->stream->dst.y -= timing->v_border_top;
113889d07b66SSamson Tam 
11394562236bSHarry Wentland 	return res;
11404562236bSHarry Wentland }
11414562236bSHarry Wentland 
11424562236bSHarry Wentland 
11434562236bSHarry Wentland enum dc_status resource_build_scaling_params_for_context(
1144fb3466a4SBhawanpreet Lakha 	const struct dc  *dc,
1145608ac7bbSJerry Zuo 	struct dc_state *context)
11464562236bSHarry Wentland {
11474562236bSHarry Wentland 	int i;
11484562236bSHarry Wentland 
11494562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
11503be5262eSHarry Wentland 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
11514562236bSHarry Wentland 				context->res_ctx.pipe_ctx[i].stream != NULL)
1152b2d0a103SDmytro Laktyushkin 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1153f84a8161STony Cheng 				return DC_FAIL_SCALING;
11544562236bSHarry Wentland 	}
11554562236bSHarry Wentland 
11564562236bSHarry Wentland 	return DC_OK;
11574562236bSHarry Wentland }
11584562236bSHarry Wentland 
1159a2b8659dSTony Cheng struct pipe_ctx *find_idle_secondary_pipe(
1160a2b8659dSTony Cheng 		struct resource_context *res_ctx,
11615581192dSJun Lei 		const struct resource_pool *pool,
11625581192dSJun Lei 		const struct pipe_ctx *primary_pipe)
11634562236bSHarry Wentland {
11644562236bSHarry Wentland 	int i;
11654562236bSHarry Wentland 	struct pipe_ctx *secondary_pipe = NULL;
11664562236bSHarry Wentland 
11674562236bSHarry Wentland 	/*
11685581192dSJun Lei 	 * We add a preferred pipe mapping to avoid the chance that
11695581192dSJun Lei 	 * MPCCs already in use will need to be reassigned to other trees.
11705581192dSJun Lei 	 * For example, if we went with the strict, assign backwards logic:
11715581192dSJun Lei 	 *
11725581192dSJun Lei 	 * (State 1)
11735581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
11745581192dSJun Lei 	 * Display B on, no surface, top pipe = 1
11755581192dSJun Lei 	 *
11765581192dSJun Lei 	 * (State 2)
11775581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
11785581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
11795581192dSJun Lei 	 *
11805581192dSJun Lei 	 * (State 3)
11815581192dSJun Lei 	 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
11825581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
11835581192dSJun Lei 	 *
11845581192dSJun Lei 	 * The state 2->3 transition requires remapping MPCC 5 from display B
11855581192dSJun Lei 	 * to display A.
11865581192dSJun Lei 	 *
11875581192dSJun Lei 	 * However, with the preferred pipe logic, state 2 would look like:
11885581192dSJun Lei 	 *
11895581192dSJun Lei 	 * (State 2)
11905581192dSJun Lei 	 * Display A on, no surface, top pipe = 0
11915581192dSJun Lei 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
11925581192dSJun Lei 	 *
11935581192dSJun Lei 	 * This would then cause 2->3 to not require remapping any MPCCs.
11945581192dSJun Lei 	 */
11955581192dSJun Lei 	if (primary_pipe) {
11965581192dSJun Lei 		int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
11975581192dSJun Lei 		if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
11985581192dSJun Lei 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
11995581192dSJun Lei 			secondary_pipe->pipe_idx = preferred_pipe_idx;
12005581192dSJun Lei 		}
12015581192dSJun Lei 	}
12025581192dSJun Lei 
12035581192dSJun Lei 	/*
12044562236bSHarry Wentland 	 * search backwards for the second pipe to keep pipe
12054562236bSHarry Wentland 	 * assignment more consistent
12064562236bSHarry Wentland 	 */
12075581192dSJun Lei 	if (!secondary_pipe)
1208a2b8659dSTony Cheng 		for (i = pool->pipe_count - 1; i >= 0; i--) {
12094562236bSHarry Wentland 			if (res_ctx->pipe_ctx[i].stream == NULL) {
12104562236bSHarry Wentland 				secondary_pipe = &res_ctx->pipe_ctx[i];
12114562236bSHarry Wentland 				secondary_pipe->pipe_idx = i;
12124562236bSHarry Wentland 				break;
12134562236bSHarry Wentland 			}
12144562236bSHarry Wentland 		}
12154562236bSHarry Wentland 
12164562236bSHarry Wentland 	return secondary_pipe;
12174562236bSHarry Wentland }
12184562236bSHarry Wentland 
12194562236bSHarry Wentland struct pipe_ctx *resource_get_head_pipe_for_stream(
12204562236bSHarry Wentland 		struct resource_context *res_ctx,
12210971c40eSHarry Wentland 		struct dc_stream_state *stream)
12224562236bSHarry Wentland {
12234562236bSHarry Wentland 	int i;
122422498036SDmytro Laktyushkin 
1225a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
1226b1f6d01cSDmytro Laktyushkin 		if (res_ctx->pipe_ctx[i].stream == stream
1227b1f6d01cSDmytro Laktyushkin 				&& !res_ctx->pipe_ctx[i].top_pipe
122822498036SDmytro Laktyushkin 				&& !res_ctx->pipe_ctx[i].prev_odm_pipe)
12294562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
12304562236bSHarry Wentland 	}
12314562236bSHarry Wentland 	return NULL;
12324562236bSHarry Wentland }
12334562236bSHarry Wentland 
1234b1f6d01cSDmytro Laktyushkin static struct pipe_ctx *resource_get_tail_pipe(
123519f89e23SAndrey Grodzovsky 		struct resource_context *res_ctx,
1236b1f6d01cSDmytro Laktyushkin 		struct pipe_ctx *head_pipe)
123719f89e23SAndrey Grodzovsky {
1238b1f6d01cSDmytro Laktyushkin 	struct pipe_ctx *tail_pipe;
123919f89e23SAndrey Grodzovsky 
124019f89e23SAndrey Grodzovsky 	tail_pipe = head_pipe->bottom_pipe;
124119f89e23SAndrey Grodzovsky 
124219f89e23SAndrey Grodzovsky 	while (tail_pipe) {
124319f89e23SAndrey Grodzovsky 		head_pipe = tail_pipe;
124419f89e23SAndrey Grodzovsky 		tail_pipe = tail_pipe->bottom_pipe;
124519f89e23SAndrey Grodzovsky 	}
124619f89e23SAndrey Grodzovsky 
124719f89e23SAndrey Grodzovsky 	return head_pipe;
124819f89e23SAndrey Grodzovsky }
124919f89e23SAndrey Grodzovsky 
12504562236bSHarry Wentland /*
1251ab2541b6SAric Cyr  * A free_pipe for a stream is defined here as a pipe
1252ab2541b6SAric Cyr  * that has no surface attached yet
12534562236bSHarry Wentland  */
1254b1f6d01cSDmytro Laktyushkin static struct pipe_ctx *acquire_free_pipe_for_head(
1255608ac7bbSJerry Zuo 		struct dc_state *context,
1256a2b8659dSTony Cheng 		const struct resource_pool *pool,
1257b1f6d01cSDmytro Laktyushkin 		struct pipe_ctx *head_pipe)
12584562236bSHarry Wentland {
12594562236bSHarry Wentland 	int i;
1260745cc746SDmytro Laktyushkin 	struct resource_context *res_ctx = &context->res_ctx;
12614562236bSHarry Wentland 
12623be5262eSHarry Wentland 	if (!head_pipe->plane_state)
12634562236bSHarry Wentland 		return head_pipe;
12644562236bSHarry Wentland 
12654562236bSHarry Wentland 	/* Re-use pipe already acquired for this stream if available*/
1266a2b8659dSTony Cheng 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1267b1f6d01cSDmytro Laktyushkin 		if (res_ctx->pipe_ctx[i].stream == head_pipe->stream &&
12683be5262eSHarry Wentland 				!res_ctx->pipe_ctx[i].plane_state) {
12694562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
12704562236bSHarry Wentland 		}
12714562236bSHarry Wentland 	}
12724562236bSHarry Wentland 
12734562236bSHarry Wentland 	/*
12744562236bSHarry Wentland 	 * At this point we have no re-useable pipe for this stream and we need
12754562236bSHarry Wentland 	 * to acquire an idle one to satisfy the request
12764562236bSHarry Wentland 	 */
12774562236bSHarry Wentland 
1278a2b8659dSTony Cheng 	if (!pool->funcs->acquire_idle_pipe_for_layer)
12794562236bSHarry Wentland 		return NULL;
12804562236bSHarry Wentland 
1281b1f6d01cSDmytro Laktyushkin 	return pool->funcs->acquire_idle_pipe_for_layer(context, pool, head_pipe->stream);
12824562236bSHarry Wentland }
12834562236bSHarry Wentland 
1284b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
12850f9a536fSDmytro Laktyushkin static int acquire_first_split_pipe(
12860f9a536fSDmytro Laktyushkin 		struct resource_context *res_ctx,
12870f9a536fSDmytro Laktyushkin 		const struct resource_pool *pool,
12880971c40eSHarry Wentland 		struct dc_stream_state *stream)
12890f9a536fSDmytro Laktyushkin {
12900f9a536fSDmytro Laktyushkin 	int i;
12910f9a536fSDmytro Laktyushkin 
12920f9a536fSDmytro Laktyushkin 	for (i = 0; i < pool->pipe_count; i++) {
129379592db3SDmytro Laktyushkin 		struct pipe_ctx *split_pipe = &res_ctx->pipe_ctx[i];
12940f9a536fSDmytro Laktyushkin 
1295b1f6d01cSDmytro Laktyushkin 		if (split_pipe->top_pipe &&
129679592db3SDmytro Laktyushkin 				split_pipe->top_pipe->plane_state == split_pipe->plane_state) {
129779592db3SDmytro Laktyushkin 			split_pipe->top_pipe->bottom_pipe = split_pipe->bottom_pipe;
129879592db3SDmytro Laktyushkin 			if (split_pipe->bottom_pipe)
129979592db3SDmytro Laktyushkin 				split_pipe->bottom_pipe->top_pipe = split_pipe->top_pipe;
13000f9a536fSDmytro Laktyushkin 
130179592db3SDmytro Laktyushkin 			if (split_pipe->top_pipe->plane_state)
130279592db3SDmytro Laktyushkin 				resource_build_scaling_params(split_pipe->top_pipe);
13030f9a536fSDmytro Laktyushkin 
130479592db3SDmytro Laktyushkin 			memset(split_pipe, 0, sizeof(*split_pipe));
130579592db3SDmytro Laktyushkin 			split_pipe->stream_res.tg = pool->timing_generators[i];
130679592db3SDmytro Laktyushkin 			split_pipe->plane_res.hubp = pool->hubps[i];
130779592db3SDmytro Laktyushkin 			split_pipe->plane_res.ipp = pool->ipps[i];
130879592db3SDmytro Laktyushkin 			split_pipe->plane_res.dpp = pool->dpps[i];
130979592db3SDmytro Laktyushkin 			split_pipe->stream_res.opp = pool->opps[i];
131079592db3SDmytro Laktyushkin 			split_pipe->plane_res.mpcc_inst = pool->dpps[i]->inst;
131179592db3SDmytro Laktyushkin 			split_pipe->pipe_idx = i;
131279592db3SDmytro Laktyushkin 
131379592db3SDmytro Laktyushkin 			split_pipe->stream = stream;
13140f9a536fSDmytro Laktyushkin 			return i;
13150f9a536fSDmytro Laktyushkin 		}
13160f9a536fSDmytro Laktyushkin 	}
13170f9a536fSDmytro Laktyushkin 	return -1;
13180f9a536fSDmytro Laktyushkin }
13190f9a536fSDmytro Laktyushkin #endif
13200f9a536fSDmytro Laktyushkin 
132119f89e23SAndrey Grodzovsky bool dc_add_plane_to_context(
132219f89e23SAndrey Grodzovsky 		const struct dc *dc,
13230971c40eSHarry Wentland 		struct dc_stream_state *stream,
132419f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1325608ac7bbSJerry Zuo 		struct dc_state *context)
13264562236bSHarry Wentland {
13274562236bSHarry Wentland 	int i;
132819f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
132919f89e23SAndrey Grodzovsky 	struct pipe_ctx *head_pipe, *tail_pipe, *free_pipe;
1330ab2541b6SAric Cyr 	struct dc_stream_status *stream_status = NULL;
13314562236bSHarry Wentland 
1332ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++)
13334fa086b9SLeo (Sunpeng) Li 		if (context->streams[i] == stream) {
1334ab2541b6SAric Cyr 			stream_status = &context->stream_status[i];
13354562236bSHarry Wentland 			break;
13364562236bSHarry Wentland 		}
1337ab2541b6SAric Cyr 	if (stream_status == NULL) {
133819f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to attach surface!\n");
133919f89e23SAndrey Grodzovsky 		return false;
134019f89e23SAndrey Grodzovsky 	}
134119f89e23SAndrey Grodzovsky 
134219f89e23SAndrey Grodzovsky 
134319f89e23SAndrey Grodzovsky 	if (stream_status->plane_count == MAX_SURFACE_NUM) {
134419f89e23SAndrey Grodzovsky 		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
134519f89e23SAndrey Grodzovsky 				plane_state, MAX_SURFACE_NUM);
134619f89e23SAndrey Grodzovsky 		return false;
134719f89e23SAndrey Grodzovsky 	}
134819f89e23SAndrey Grodzovsky 
134919f89e23SAndrey Grodzovsky 	head_pipe = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
135019f89e23SAndrey Grodzovsky 
135119f89e23SAndrey Grodzovsky 	if (!head_pipe) {
135219f89e23SAndrey Grodzovsky 		dm_error("Head pipe not found for stream_state %p !\n", stream);
13534562236bSHarry Wentland 		return false;
13544562236bSHarry Wentland 	}
13554562236bSHarry Wentland 
1356b1f6d01cSDmytro Laktyushkin 	/* retain new surface, but only once per stream */
1357b1f6d01cSDmytro Laktyushkin 	dc_plane_state_retain(plane_state);
1358b1f6d01cSDmytro Laktyushkin 
1359b1f6d01cSDmytro Laktyushkin 	while (head_pipe) {
1360b1f6d01cSDmytro Laktyushkin 		free_pipe = acquire_free_pipe_for_head(context, pool, head_pipe);
13614562236bSHarry Wentland 
1362b86a1aa3SBhawanpreet Lakha 	#if defined(CONFIG_DRM_AMD_DC_DCN)
13630f9a536fSDmytro Laktyushkin 		if (!free_pipe) {
13640f9a536fSDmytro Laktyushkin 			int pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
13650f9a536fSDmytro Laktyushkin 			if (pipe_idx >= 0)
13660f9a536fSDmytro Laktyushkin 				free_pipe = &context->res_ctx.pipe_ctx[pipe_idx];
13670f9a536fSDmytro Laktyushkin 		}
13680f9a536fSDmytro Laktyushkin 	#endif
1369b1f6d01cSDmytro Laktyushkin 		if (!free_pipe) {
1370b1f6d01cSDmytro Laktyushkin 			dc_plane_state_release(plane_state);
13714562236bSHarry Wentland 			return false;
1372b1f6d01cSDmytro Laktyushkin 		}
13734562236bSHarry Wentland 
13743be5262eSHarry Wentland 		free_pipe->plane_state = plane_state;
13754562236bSHarry Wentland 
137619f89e23SAndrey Grodzovsky 		if (head_pipe != free_pipe) {
13775b5c1777SJosip Pavic 			tail_pipe = resource_get_tail_pipe(&context->res_ctx, head_pipe);
13785b5c1777SJosip Pavic 			ASSERT(tail_pipe);
13796b670fa9SHarry Wentland 			free_pipe->stream_res.tg = tail_pipe->stream_res.tg;
13809aef1a31SSivapiriyanKumarasamy 			free_pipe->stream_res.abm = tail_pipe->stream_res.abm;
1381a6a6cb34SHarry Wentland 			free_pipe->stream_res.opp = tail_pipe->stream_res.opp;
13828e9c4c8cSHarry Wentland 			free_pipe->stream_res.stream_enc = tail_pipe->stream_res.stream_enc;
1383afaacef4SHarry Wentland 			free_pipe->stream_res.audio = tail_pipe->stream_res.audio;
1384cfe4645eSDmytro Laktyushkin 			free_pipe->clock_source = tail_pipe->clock_source;
13854562236bSHarry Wentland 			free_pipe->top_pipe = tail_pipe;
13864562236bSHarry Wentland 			tail_pipe->bottom_pipe = free_pipe;
13872e7b43e6SDmytro Laktyushkin 			if (!free_pipe->next_odm_pipe && tail_pipe->next_odm_pipe && tail_pipe->next_odm_pipe->bottom_pipe) {
13882e7b43e6SDmytro Laktyushkin 				free_pipe->next_odm_pipe = tail_pipe->next_odm_pipe->bottom_pipe;
13892e7b43e6SDmytro Laktyushkin 				tail_pipe->next_odm_pipe->bottom_pipe->prev_odm_pipe = free_pipe;
13902e7b43e6SDmytro Laktyushkin 			}
13912e7b43e6SDmytro Laktyushkin 			if (!free_pipe->prev_odm_pipe && tail_pipe->prev_odm_pipe && tail_pipe->prev_odm_pipe->bottom_pipe) {
13922e7b43e6SDmytro Laktyushkin 				free_pipe->prev_odm_pipe = tail_pipe->prev_odm_pipe->bottom_pipe;
13932e7b43e6SDmytro Laktyushkin 				tail_pipe->prev_odm_pipe->bottom_pipe->next_odm_pipe = free_pipe;
13942e7b43e6SDmytro Laktyushkin 			}
13954562236bSHarry Wentland 		}
1396b1f6d01cSDmytro Laktyushkin 		head_pipe = head_pipe->next_odm_pipe;
1397b1f6d01cSDmytro Laktyushkin 	}
13984562236bSHarry Wentland 	/* assign new surfaces*/
139919f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = plane_state;
14004562236bSHarry Wentland 
140119f89e23SAndrey Grodzovsky 	stream_status->plane_count++;
14024562236bSHarry Wentland 
14034562236bSHarry Wentland 	return true;
14044562236bSHarry Wentland }
14054562236bSHarry Wentland 
140619f89e23SAndrey Grodzovsky bool dc_remove_plane_from_context(
140719f89e23SAndrey Grodzovsky 		const struct dc *dc,
140819f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
140919f89e23SAndrey Grodzovsky 		struct dc_plane_state *plane_state,
1410608ac7bbSJerry Zuo 		struct dc_state *context)
141119f89e23SAndrey Grodzovsky {
141219f89e23SAndrey Grodzovsky 	int i;
141319f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
141419f89e23SAndrey Grodzovsky 	struct resource_pool *pool = dc->res_pool;
141519f89e23SAndrey Grodzovsky 
141619f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
141719f89e23SAndrey Grodzovsky 		if (context->streams[i] == stream) {
141819f89e23SAndrey Grodzovsky 			stream_status = &context->stream_status[i];
141919f89e23SAndrey Grodzovsky 			break;
142019f89e23SAndrey Grodzovsky 		}
142119f89e23SAndrey Grodzovsky 
142219f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
142319f89e23SAndrey Grodzovsky 		dm_error("Existing stream not found; failed to remove plane.\n");
142419f89e23SAndrey Grodzovsky 		return false;
142519f89e23SAndrey Grodzovsky 	}
142619f89e23SAndrey Grodzovsky 
142719f89e23SAndrey Grodzovsky 	/* release pipe for plane*/
142819f89e23SAndrey Grodzovsky 	for (i = pool->pipe_count - 1; i >= 0; i--) {
14296ffaa6fcSDmytro Laktyushkin 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
143019f89e23SAndrey Grodzovsky 
14316ffaa6fcSDmytro Laktyushkin 		if (pipe_ctx->plane_state == plane_state) {
143219f89e23SAndrey Grodzovsky 			if (pipe_ctx->top_pipe)
143319f89e23SAndrey Grodzovsky 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
143419f89e23SAndrey Grodzovsky 
143519f89e23SAndrey Grodzovsky 			/* Second condition is to avoid setting NULL to top pipe
143619f89e23SAndrey Grodzovsky 			 * of tail pipe making it look like head pipe in subsequent
143719f89e23SAndrey Grodzovsky 			 * deletes
143819f89e23SAndrey Grodzovsky 			 */
143919f89e23SAndrey Grodzovsky 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
144019f89e23SAndrey Grodzovsky 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
144119f89e23SAndrey Grodzovsky 
144219f89e23SAndrey Grodzovsky 			/*
144319f89e23SAndrey Grodzovsky 			 * For head pipe detach surfaces from pipe for tail
144419f89e23SAndrey Grodzovsky 			 * pipe just zero it out
144519f89e23SAndrey Grodzovsky 			 */
1446b1f6d01cSDmytro Laktyushkin 			if (!pipe_ctx->top_pipe)
144719f89e23SAndrey Grodzovsky 				pipe_ctx->plane_state = NULL;
1448b1f6d01cSDmytro Laktyushkin 			else
144919f89e23SAndrey Grodzovsky 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
145019f89e23SAndrey Grodzovsky 		}
145119f89e23SAndrey Grodzovsky 	}
145219f89e23SAndrey Grodzovsky 
145319f89e23SAndrey Grodzovsky 
145419f89e23SAndrey Grodzovsky 	for (i = 0; i < stream_status->plane_count; i++) {
145519f89e23SAndrey Grodzovsky 		if (stream_status->plane_states[i] == plane_state) {
145619f89e23SAndrey Grodzovsky 
145719f89e23SAndrey Grodzovsky 			dc_plane_state_release(stream_status->plane_states[i]);
145819f89e23SAndrey Grodzovsky 			break;
145919f89e23SAndrey Grodzovsky 		}
146019f89e23SAndrey Grodzovsky 	}
146119f89e23SAndrey Grodzovsky 
146219f89e23SAndrey Grodzovsky 	if (i == stream_status->plane_count) {
146319f89e23SAndrey Grodzovsky 		dm_error("Existing plane_state not found; failed to detach it!\n");
146419f89e23SAndrey Grodzovsky 		return false;
146519f89e23SAndrey Grodzovsky 	}
146619f89e23SAndrey Grodzovsky 
146719f89e23SAndrey Grodzovsky 	stream_status->plane_count--;
146819f89e23SAndrey Grodzovsky 
1469abb4986eSAndrew Jiang 	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
1470abb4986eSAndrew Jiang 	for (; i < stream_status->plane_count; i++)
147119f89e23SAndrey Grodzovsky 		stream_status->plane_states[i] = stream_status->plane_states[i + 1];
147219f89e23SAndrey Grodzovsky 
147319f89e23SAndrey Grodzovsky 	stream_status->plane_states[stream_status->plane_count] = NULL;
147419f89e23SAndrey Grodzovsky 
147519f89e23SAndrey Grodzovsky 	return true;
147619f89e23SAndrey Grodzovsky }
147719f89e23SAndrey Grodzovsky 
147819f89e23SAndrey Grodzovsky bool dc_rem_all_planes_for_stream(
147919f89e23SAndrey Grodzovsky 		const struct dc *dc,
148019f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
1481608ac7bbSJerry Zuo 		struct dc_state *context)
148219f89e23SAndrey Grodzovsky {
148319f89e23SAndrey Grodzovsky 	int i, old_plane_count;
148419f89e23SAndrey Grodzovsky 	struct dc_stream_status *stream_status = NULL;
148519f89e23SAndrey Grodzovsky 	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
148619f89e23SAndrey Grodzovsky 
148719f89e23SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
148819f89e23SAndrey Grodzovsky 			if (context->streams[i] == stream) {
148919f89e23SAndrey Grodzovsky 				stream_status = &context->stream_status[i];
149019f89e23SAndrey Grodzovsky 				break;
149119f89e23SAndrey Grodzovsky 			}
149219f89e23SAndrey Grodzovsky 
149319f89e23SAndrey Grodzovsky 	if (stream_status == NULL) {
149419f89e23SAndrey Grodzovsky 		dm_error("Existing stream %p not found!\n", stream);
149519f89e23SAndrey Grodzovsky 		return false;
149619f89e23SAndrey Grodzovsky 	}
149719f89e23SAndrey Grodzovsky 
149819f89e23SAndrey Grodzovsky 	old_plane_count = stream_status->plane_count;
149919f89e23SAndrey Grodzovsky 
150019f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
150119f89e23SAndrey Grodzovsky 		del_planes[i] = stream_status->plane_states[i];
150219f89e23SAndrey Grodzovsky 
150319f89e23SAndrey Grodzovsky 	for (i = 0; i < old_plane_count; i++)
150419f89e23SAndrey Grodzovsky 		if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
150519f89e23SAndrey Grodzovsky 			return false;
150619f89e23SAndrey Grodzovsky 
150719f89e23SAndrey Grodzovsky 	return true;
150819f89e23SAndrey Grodzovsky }
150919f89e23SAndrey Grodzovsky 
151019f89e23SAndrey Grodzovsky static bool add_all_planes_for_stream(
151119f89e23SAndrey Grodzovsky 		const struct dc *dc,
151219f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
151319f89e23SAndrey Grodzovsky 		const struct dc_validation_set set[],
151419f89e23SAndrey Grodzovsky 		int set_count,
1515608ac7bbSJerry Zuo 		struct dc_state *context)
151619f89e23SAndrey Grodzovsky {
151719f89e23SAndrey Grodzovsky 	int i, j;
151819f89e23SAndrey Grodzovsky 
151919f89e23SAndrey Grodzovsky 	for (i = 0; i < set_count; i++)
152019f89e23SAndrey Grodzovsky 		if (set[i].stream == stream)
152119f89e23SAndrey Grodzovsky 			break;
152219f89e23SAndrey Grodzovsky 
152319f89e23SAndrey Grodzovsky 	if (i == set_count) {
152419f89e23SAndrey Grodzovsky 		dm_error("Stream %p not found in set!\n", stream);
152519f89e23SAndrey Grodzovsky 		return false;
152619f89e23SAndrey Grodzovsky 	}
152719f89e23SAndrey Grodzovsky 
152819f89e23SAndrey Grodzovsky 	for (j = 0; j < set[i].plane_count; j++)
152919f89e23SAndrey Grodzovsky 		if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
153019f89e23SAndrey Grodzovsky 			return false;
153119f89e23SAndrey Grodzovsky 
153219f89e23SAndrey Grodzovsky 	return true;
153319f89e23SAndrey Grodzovsky }
153419f89e23SAndrey Grodzovsky 
153519f89e23SAndrey Grodzovsky bool dc_add_all_planes_for_stream(
153619f89e23SAndrey Grodzovsky 		const struct dc *dc,
153719f89e23SAndrey Grodzovsky 		struct dc_stream_state *stream,
153819f89e23SAndrey Grodzovsky 		struct dc_plane_state * const *plane_states,
153919f89e23SAndrey Grodzovsky 		int plane_count,
1540608ac7bbSJerry Zuo 		struct dc_state *context)
154119f89e23SAndrey Grodzovsky {
154219f89e23SAndrey Grodzovsky 	struct dc_validation_set set;
154319f89e23SAndrey Grodzovsky 	int i;
154419f89e23SAndrey Grodzovsky 
154519f89e23SAndrey Grodzovsky 	set.stream = stream;
154619f89e23SAndrey Grodzovsky 	set.plane_count = plane_count;
154719f89e23SAndrey Grodzovsky 
154819f89e23SAndrey Grodzovsky 	for (i = 0; i < plane_count; i++)
154919f89e23SAndrey Grodzovsky 		set.plane_states[i] = plane_states[i];
155019f89e23SAndrey Grodzovsky 
155119f89e23SAndrey Grodzovsky 	return add_all_planes_for_stream(dc, stream, &set, 1, context);
155219f89e23SAndrey Grodzovsky }
155319f89e23SAndrey Grodzovsky 
15540971c40eSHarry Wentland static bool is_timing_changed(struct dc_stream_state *cur_stream,
15550971c40eSHarry Wentland 		struct dc_stream_state *new_stream)
15564562236bSHarry Wentland {
15574562236bSHarry Wentland 	if (cur_stream == NULL)
15584562236bSHarry Wentland 		return true;
15594562236bSHarry Wentland 
15604562236bSHarry Wentland 	/* If output color space is changed, need to reprogram info frames */
15614fa086b9SLeo (Sunpeng) Li 	if (cur_stream->output_color_space != new_stream->output_color_space)
15624562236bSHarry Wentland 		return true;
15634562236bSHarry Wentland 
15644562236bSHarry Wentland 	return memcmp(
15654fa086b9SLeo (Sunpeng) Li 		&cur_stream->timing,
15664fa086b9SLeo (Sunpeng) Li 		&new_stream->timing,
15674562236bSHarry Wentland 		sizeof(struct dc_crtc_timing)) != 0;
15684562236bSHarry Wentland }
15694562236bSHarry Wentland 
15704562236bSHarry Wentland static bool are_stream_backends_same(
15710971c40eSHarry Wentland 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
15724562236bSHarry Wentland {
15734562236bSHarry Wentland 	if (stream_a == stream_b)
15744562236bSHarry Wentland 		return true;
15754562236bSHarry Wentland 
15764562236bSHarry Wentland 	if (stream_a == NULL || stream_b == NULL)
15774562236bSHarry Wentland 		return false;
15784562236bSHarry Wentland 
15794562236bSHarry Wentland 	if (is_timing_changed(stream_a, stream_b))
15804562236bSHarry Wentland 		return false;
15814562236bSHarry Wentland 
15821e7e86c4SSamson Tam 	if (stream_a->dpms_off != stream_b->dpms_off)
15831e7e86c4SSamson Tam 		return false;
15841e7e86c4SSamson Tam 
15854562236bSHarry Wentland 	return true;
15864562236bSHarry Wentland }
15874562236bSHarry Wentland 
1588625a15bfSLee Jones /*
15892119aa17SDavid Francis  * dc_is_stream_unchanged() - Compare two stream states for equivalence.
15902119aa17SDavid Francis  *
15912119aa17SDavid Francis  * Checks if there a difference between the two states
15922119aa17SDavid Francis  * that would require a mode change.
15932119aa17SDavid Francis  *
15942119aa17SDavid Francis  * Does not compare cursor position or attributes.
15952119aa17SDavid Francis  */
1596d54d29dbSBhawanpreet Lakha bool dc_is_stream_unchanged(
15970971c40eSHarry Wentland 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
15984562236bSHarry Wentland {
15994562236bSHarry Wentland 
16004562236bSHarry Wentland 	if (!are_stream_backends_same(old_stream, stream))
16014562236bSHarry Wentland 		return false;
16024562236bSHarry Wentland 
16030460f9abSJun Lei 	if (old_stream->ignore_msa_timing_param != stream->ignore_msa_timing_param)
16040460f9abSJun Lei 		return false;
16050460f9abSJun Lei 
16064562236bSHarry Wentland 	return true;
16074562236bSHarry Wentland }
16084562236bSHarry Wentland 
1609625a15bfSLee Jones /*
16102119aa17SDavid Francis  * dc_is_stream_scaling_unchanged() - Compare scaling rectangles of two streams.
16112119aa17SDavid Francis  */
16129a5d9c48SLeo (Sunpeng) Li bool dc_is_stream_scaling_unchanged(
16139a5d9c48SLeo (Sunpeng) Li 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
16149a5d9c48SLeo (Sunpeng) Li {
16159a5d9c48SLeo (Sunpeng) Li 	if (old_stream == stream)
16169a5d9c48SLeo (Sunpeng) Li 		return true;
16179a5d9c48SLeo (Sunpeng) Li 
16189a5d9c48SLeo (Sunpeng) Li 	if (old_stream == NULL || stream == NULL)
16199a5d9c48SLeo (Sunpeng) Li 		return false;
16209a5d9c48SLeo (Sunpeng) Li 
16219a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->src,
16229a5d9c48SLeo (Sunpeng) Li 			&stream->src,
16239a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
16249a5d9c48SLeo (Sunpeng) Li 		return false;
16259a5d9c48SLeo (Sunpeng) Li 
16269a5d9c48SLeo (Sunpeng) Li 	if (memcmp(&old_stream->dst,
16279a5d9c48SLeo (Sunpeng) Li 			&stream->dst,
16289a5d9c48SLeo (Sunpeng) Li 			sizeof(struct rect)) != 0)
16299a5d9c48SLeo (Sunpeng) Li 		return false;
16309a5d9c48SLeo (Sunpeng) Li 
16319a5d9c48SLeo (Sunpeng) Li 	return true;
16329a5d9c48SLeo (Sunpeng) Li }
16339a5d9c48SLeo (Sunpeng) Li 
16341dc90497SAndrey Grodzovsky static void update_stream_engine_usage(
16354562236bSHarry Wentland 		struct resource_context *res_ctx,
1636a2b8659dSTony Cheng 		const struct resource_pool *pool,
16371dc90497SAndrey Grodzovsky 		struct stream_encoder *stream_enc,
16381dc90497SAndrey Grodzovsky 		bool acquired)
16394562236bSHarry Wentland {
16404562236bSHarry Wentland 	int i;
16414562236bSHarry Wentland 
1642a2b8659dSTony Cheng 	for (i = 0; i < pool->stream_enc_count; i++) {
1643a2b8659dSTony Cheng 		if (pool->stream_enc[i] == stream_enc)
16441dc90497SAndrey Grodzovsky 			res_ctx->is_stream_enc_acquired[i] = acquired;
16454562236bSHarry Wentland 	}
16464562236bSHarry Wentland }
16474562236bSHarry Wentland 
16484562236bSHarry Wentland /* TODO: release audio object */
16494176664bSCharlene Liu void update_audio_usage(
16504562236bSHarry Wentland 		struct resource_context *res_ctx,
1651a2b8659dSTony Cheng 		const struct resource_pool *pool,
16521dc90497SAndrey Grodzovsky 		struct audio *audio,
16531dc90497SAndrey Grodzovsky 		bool acquired)
16544562236bSHarry Wentland {
16554562236bSHarry Wentland 	int i;
1656a2b8659dSTony Cheng 	for (i = 0; i < pool->audio_count; i++) {
1657a2b8659dSTony Cheng 		if (pool->audios[i] == audio)
16581dc90497SAndrey Grodzovsky 			res_ctx->is_audio_acquired[i] = acquired;
16594562236bSHarry Wentland 	}
16604562236bSHarry Wentland }
16614562236bSHarry Wentland 
16624562236bSHarry Wentland static int acquire_first_free_pipe(
16634562236bSHarry Wentland 		struct resource_context *res_ctx,
1664a2b8659dSTony Cheng 		const struct resource_pool *pool,
16650971c40eSHarry Wentland 		struct dc_stream_state *stream)
16664562236bSHarry Wentland {
16674562236bSHarry Wentland 	int i;
16684562236bSHarry Wentland 
1669a2b8659dSTony Cheng 	for (i = 0; i < pool->pipe_count; i++) {
16704562236bSHarry Wentland 		if (!res_ctx->pipe_ctx[i].stream) {
16714562236bSHarry Wentland 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
16724562236bSHarry Wentland 
16736b670fa9SHarry Wentland 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
167486a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi = pool->mis[i];
16758feabd03SYue Hin Lau 			pipe_ctx->plane_res.hubp = pool->hubps[i];
167686a66c4eSHarry Wentland 			pipe_ctx->plane_res.ipp = pool->ipps[i];
167786a66c4eSHarry Wentland 			pipe_ctx->plane_res.xfm = pool->transforms[i];
1678d94585a0SYue Hin Lau 			pipe_ctx->plane_res.dpp = pool->dpps[i];
1679a6a6cb34SHarry Wentland 			pipe_ctx->stream_res.opp = pool->opps[i];
1680bc373a89SRoman Li 			if (pool->dpps[i])
1681e07f541fSYongqiang Sun 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
16824562236bSHarry Wentland 			pipe_ctx->pipe_idx = i;
16834562236bSHarry Wentland 
1684ff5ef992SAlex Deucher 
16854562236bSHarry Wentland 			pipe_ctx->stream = stream;
16864562236bSHarry Wentland 			return i;
16874562236bSHarry Wentland 		}
16884562236bSHarry Wentland 	}
16894562236bSHarry Wentland 	return -1;
16904562236bSHarry Wentland }
16914562236bSHarry Wentland 
1692a2b8659dSTony Cheng static struct audio *find_first_free_audio(
1693a2b8659dSTony Cheng 		struct resource_context *res_ctx,
1694cfb071f7SCharlene Liu 		const struct resource_pool *pool,
1695f24b0522SPaul Hsieh 		enum engine_id id,
1696f24b0522SPaul Hsieh 		enum dce_version dc_version)
16974562236bSHarry Wentland {
1698b5a41620SCharlene Liu 	int i, available_audio_count;
1699b5a41620SCharlene Liu 
1700b5a41620SCharlene Liu 	available_audio_count = pool->audio_count;
1701b5a41620SCharlene Liu 
1702b5a41620SCharlene Liu 	for (i = 0; i < available_audio_count; i++) {
17034176664bSCharlene Liu 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
1704cfb071f7SCharlene Liu 			/*we have enough audio endpoint, find the matching inst*/
1705cfb071f7SCharlene Liu 			if (id != i)
1706cfb071f7SCharlene Liu 				continue;
1707a2b8659dSTony Cheng 			return pool->audios[i];
17084562236bSHarry Wentland 		}
17094562236bSHarry Wentland 	}
17105feb9f07STai Man 
17115feb9f07STai Man 	/* use engine id to find free audio */
1712b5a41620SCharlene Liu 	if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
17135feb9f07STai Man 		return pool->audios[id];
17145feb9f07STai Man 	}
171566bfd4fdSCharlene Liu 	/*not found the matching one, first come first serve*/
1716b5a41620SCharlene Liu 	for (i = 0; i < available_audio_count; i++) {
17174176664bSCharlene Liu 		if (res_ctx->is_audio_acquired[i] == false) {
17184176664bSCharlene Liu 			return pool->audios[i];
17194176664bSCharlene Liu 		}
17204176664bSCharlene Liu 	}
17214562236bSHarry Wentland 	return 0;
17224562236bSHarry Wentland }
17234562236bSHarry Wentland 
1724625a15bfSLee Jones /*
17252119aa17SDavid Francis  * dc_add_stream_to_ctx() - Add a new dc_stream_state to a dc_state.
17262119aa17SDavid Francis  */
172713ab1b44SYongqiang Sun enum dc_status dc_add_stream_to_ctx(
17281dc90497SAndrey Grodzovsky 		struct dc *dc,
1729608ac7bbSJerry Zuo 		struct dc_state *new_ctx,
17301dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
17311dc90497SAndrey Grodzovsky {
17321dc90497SAndrey Grodzovsky 	enum dc_status res;
1733eb9714a2SWenjing Liu 	DC_LOGGER_INIT(dc->ctx->logger);
17341dc90497SAndrey Grodzovsky 
1735ece4147fSKen Chalmers 	if (new_ctx->stream_count >= dc->res_pool->timing_generator_count) {
1736eb9714a2SWenjing Liu 		DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
17371dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
17381dc90497SAndrey Grodzovsky 	}
17391dc90497SAndrey Grodzovsky 
17401dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = stream;
17411dc90497SAndrey Grodzovsky 	dc_stream_retain(stream);
17421dc90497SAndrey Grodzovsky 	new_ctx->stream_count++;
17431dc90497SAndrey Grodzovsky 
17441dc90497SAndrey Grodzovsky 	res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
17451dc90497SAndrey Grodzovsky 	if (res != DC_OK)
1746eb9714a2SWenjing Liu 		DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);
17471dc90497SAndrey Grodzovsky 
174813ab1b44SYongqiang Sun 	return res;
17491dc90497SAndrey Grodzovsky }
17501dc90497SAndrey Grodzovsky 
1751625a15bfSLee Jones /*
17522119aa17SDavid Francis  * dc_remove_stream_from_ctx() - Remove a stream from a dc_state.
17532119aa17SDavid Francis  */
175462c933f9SYongqiang Sun enum dc_status dc_remove_stream_from_ctx(
17551dc90497SAndrey Grodzovsky 			struct dc *dc,
1756608ac7bbSJerry Zuo 			struct dc_state *new_ctx,
17571dc90497SAndrey Grodzovsky 			struct dc_stream_state *stream)
17581dc90497SAndrey Grodzovsky {
175919f89e23SAndrey Grodzovsky 	int i;
17601dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
176122498036SDmytro Laktyushkin 	struct pipe_ctx *del_pipe = resource_get_head_pipe_for_stream(&new_ctx->res_ctx, stream);
176222498036SDmytro Laktyushkin 	struct pipe_ctx *odm_pipe;
176322498036SDmytro Laktyushkin 
176422498036SDmytro Laktyushkin 	if (!del_pipe) {
176522498036SDmytro Laktyushkin 		DC_ERROR("Pipe not found for stream %p !\n", stream);
176622498036SDmytro Laktyushkin 		return DC_ERROR_UNEXPECTED;
176722498036SDmytro Laktyushkin 	}
176822498036SDmytro Laktyushkin 
176922498036SDmytro Laktyushkin 	odm_pipe = del_pipe->next_odm_pipe;
17701dc90497SAndrey Grodzovsky 
177119f89e23SAndrey Grodzovsky 	/* Release primary pipe */
177219f89e23SAndrey Grodzovsky 	ASSERT(del_pipe->stream_res.stream_enc);
17731dc90497SAndrey Grodzovsky 	update_stream_engine_usage(
17741dc90497SAndrey Grodzovsky 			&new_ctx->res_ctx,
17751dc90497SAndrey Grodzovsky 				dc->res_pool,
17761dc90497SAndrey Grodzovsky 			del_pipe->stream_res.stream_enc,
17771dc90497SAndrey Grodzovsky 			false);
1778f42ef862SJimmy Kizito 	/* Release link encoder from stream in new dc_state. */
1779f42ef862SJimmy Kizito 	if (dc->res_pool->funcs->link_enc_unassign)
1780f42ef862SJimmy Kizito 		dc->res_pool->funcs->link_enc_unassign(new_ctx, del_pipe->stream);
17811dc90497SAndrey Grodzovsky 
17821dc90497SAndrey Grodzovsky 	if (del_pipe->stream_res.audio)
17831dc90497SAndrey Grodzovsky 		update_audio_usage(
17841dc90497SAndrey Grodzovsky 			&new_ctx->res_ctx,
17851dc90497SAndrey Grodzovsky 			dc->res_pool,
17861dc90497SAndrey Grodzovsky 			del_pipe->stream_res.audio,
17871dc90497SAndrey Grodzovsky 			false);
17881dc90497SAndrey Grodzovsky 
17899d0dcecdSHarry Wentland 	resource_unreference_clock_source(&new_ctx->res_ctx,
17909d0dcecdSHarry Wentland 					  dc->res_pool,
17919d0dcecdSHarry Wentland 					  del_pipe->clock_source);
17929d0dcecdSHarry Wentland 
1793e56ae556SNikola Cornij 	if (dc->res_pool->funcs->remove_stream_from_ctx)
1794e56ae556SNikola Cornij 		dc->res_pool->funcs->remove_stream_from_ctx(dc, new_ctx, stream);
1795e56ae556SNikola Cornij 
179622498036SDmytro Laktyushkin 	while (odm_pipe) {
179722498036SDmytro Laktyushkin 		struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
179822498036SDmytro Laktyushkin 
179922498036SDmytro Laktyushkin 		memset(odm_pipe, 0, sizeof(*odm_pipe));
180022498036SDmytro Laktyushkin 		odm_pipe = next_odm_pipe;
180122498036SDmytro Laktyushkin 	}
18021dc90497SAndrey Grodzovsky 	memset(del_pipe, 0, sizeof(*del_pipe));
18036ffaa6fcSDmytro Laktyushkin 
18041dc90497SAndrey Grodzovsky 	for (i = 0; i < new_ctx->stream_count; i++)
18051dc90497SAndrey Grodzovsky 		if (new_ctx->streams[i] == stream)
18061dc90497SAndrey Grodzovsky 			break;
18071dc90497SAndrey Grodzovsky 
18081dc90497SAndrey Grodzovsky 	if (new_ctx->streams[i] != stream) {
18091dc90497SAndrey Grodzovsky 		DC_ERROR("Context doesn't have stream %p !\n", stream);
18101dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
18111dc90497SAndrey Grodzovsky 	}
18121dc90497SAndrey Grodzovsky 
18131dc90497SAndrey Grodzovsky 	dc_stream_release(new_ctx->streams[i]);
18141dc90497SAndrey Grodzovsky 	new_ctx->stream_count--;
18151dc90497SAndrey Grodzovsky 
18161dc90497SAndrey Grodzovsky 	/* Trim back arrays */
18171dc90497SAndrey Grodzovsky 	for (; i < new_ctx->stream_count; i++) {
18181dc90497SAndrey Grodzovsky 		new_ctx->streams[i] = new_ctx->streams[i + 1];
18191dc90497SAndrey Grodzovsky 		new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
18201dc90497SAndrey Grodzovsky 	}
18211dc90497SAndrey Grodzovsky 
18221dc90497SAndrey Grodzovsky 	new_ctx->streams[new_ctx->stream_count] = NULL;
18231dc90497SAndrey Grodzovsky 	memset(
18241dc90497SAndrey Grodzovsky 			&new_ctx->stream_status[new_ctx->stream_count],
18251dc90497SAndrey Grodzovsky 			0,
18261dc90497SAndrey Grodzovsky 			sizeof(new_ctx->stream_status[0]));
18271dc90497SAndrey Grodzovsky 
18281dc90497SAndrey Grodzovsky 	return DC_OK;
18291dc90497SAndrey Grodzovsky }
18301dc90497SAndrey Grodzovsky 
18310971c40eSHarry Wentland static struct dc_stream_state *find_pll_sharable_stream(
18320971c40eSHarry Wentland 		struct dc_stream_state *stream_needs_pll,
1833608ac7bbSJerry Zuo 		struct dc_state *context)
18344562236bSHarry Wentland {
1835ab2541b6SAric Cyr 	int i;
18364562236bSHarry Wentland 
1837ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
18380971c40eSHarry Wentland 		struct dc_stream_state *stream_has_pll = context->streams[i];
18394562236bSHarry Wentland 
18404562236bSHarry Wentland 		/* We are looking for non dp, non virtual stream */
18414562236bSHarry Wentland 		if (resource_are_streams_timing_synchronizable(
18424562236bSHarry Wentland 			stream_needs_pll, stream_has_pll)
18434562236bSHarry Wentland 			&& !dc_is_dp_signal(stream_has_pll->signal)
1844ceb3dbb4SJun Lei 			&& stream_has_pll->link->connector_signal
18454562236bSHarry Wentland 			!= SIGNAL_TYPE_VIRTUAL)
18464562236bSHarry Wentland 			return stream_has_pll;
1847ab2541b6SAric Cyr 
18484562236bSHarry Wentland 	}
18494562236bSHarry Wentland 
18504562236bSHarry Wentland 	return NULL;
18514562236bSHarry Wentland }
18524562236bSHarry Wentland 
18534562236bSHarry Wentland static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
18544562236bSHarry Wentland {
1855380604e2SKen Chalmers 	uint32_t pix_clk = timing->pix_clk_100hz;
18564562236bSHarry Wentland 	uint32_t normalized_pix_clk = pix_clk;
18574562236bSHarry Wentland 
18584562236bSHarry Wentland 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
18594562236bSHarry Wentland 		pix_clk /= 2;
1860cc4d99b8SCharlene Liu 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
18614562236bSHarry Wentland 		switch (timing->display_color_depth) {
18628897810aSJulian Parkin 		case COLOR_DEPTH_666:
18634562236bSHarry Wentland 		case COLOR_DEPTH_888:
18644562236bSHarry Wentland 			normalized_pix_clk = pix_clk;
18654562236bSHarry Wentland 			break;
18664562236bSHarry Wentland 		case COLOR_DEPTH_101010:
18674562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 30) / 24;
18684562236bSHarry Wentland 			break;
18694562236bSHarry Wentland 		case COLOR_DEPTH_121212:
18704562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 36) / 24;
18714562236bSHarry Wentland 		break;
18724562236bSHarry Wentland 		case COLOR_DEPTH_161616:
18734562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 48) / 24;
18744562236bSHarry Wentland 		break;
18754562236bSHarry Wentland 		default:
18764562236bSHarry Wentland 			ASSERT(0);
18774562236bSHarry Wentland 		break;
18784562236bSHarry Wentland 		}
1879cc4d99b8SCharlene Liu 	}
18804562236bSHarry Wentland 	return normalized_pix_clk;
18814562236bSHarry Wentland }
18824562236bSHarry Wentland 
18830971c40eSHarry Wentland static void calculate_phy_pix_clks(struct dc_stream_state *stream)
18844562236bSHarry Wentland {
18854562236bSHarry Wentland 	/* update actual pixel clock on all streams */
18864562236bSHarry Wentland 	if (dc_is_hdmi_signal(stream->signal))
18874562236bSHarry Wentland 		stream->phy_pix_clk = get_norm_pix_clk(
1888380604e2SKen Chalmers 			&stream->timing) / 10;
18894562236bSHarry Wentland 	else
18904562236bSHarry Wentland 		stream->phy_pix_clk =
1891380604e2SKen Chalmers 			stream->timing.pix_clk_100hz / 10;
189239c03e00SCharlene Liu 
189339c03e00SCharlene Liu 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
189439c03e00SCharlene Liu 		stream->phy_pix_clk *= 2;
18954562236bSHarry Wentland }
18964562236bSHarry Wentland 
1897d2d7885fSAnthony Koo static int acquire_resource_from_hw_enabled_state(
1898d2d7885fSAnthony Koo 		struct resource_context *res_ctx,
1899d2d7885fSAnthony Koo 		const struct resource_pool *pool,
1900d2d7885fSAnthony Koo 		struct dc_stream_state *stream)
1901d2d7885fSAnthony Koo {
1902d2d7885fSAnthony Koo 	struct dc_link *link = stream->link;
190308b66279SMartin Leung 	unsigned int i, inst, tg_inst = 0;
1904d2d7885fSAnthony Koo 
1905d2d7885fSAnthony Koo 	/* Check for enabled DIG to identify enabled display */
1906d2d7885fSAnthony Koo 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1907d2d7885fSAnthony Koo 		return -1;
1908d2d7885fSAnthony Koo 
19095ec43edaSMartin Leung 	inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1910d2d7885fSAnthony Koo 
19117f7652eeSMartin Leung 	if (inst == ENGINE_ID_UNKNOWN)
191275441d9dSMikita Lipski 		return -1;
1913d2d7885fSAnthony Koo 
19147f7652eeSMartin Leung 	for (i = 0; i < pool->stream_enc_count; i++) {
19157f7652eeSMartin Leung 		if (pool->stream_enc[i]->id == inst) {
19167f7652eeSMartin Leung 			tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
19177f7652eeSMartin Leung 				pool->stream_enc[i]);
19187f7652eeSMartin Leung 			break;
19197f7652eeSMartin Leung 		}
19207f7652eeSMartin Leung 	}
1921d2d7885fSAnthony Koo 
19227f7652eeSMartin Leung 	// tg_inst not found
19237f7652eeSMartin Leung 	if (i == pool->stream_enc_count)
192475441d9dSMikita Lipski 		return -1;
19255ec43edaSMartin Leung 
19265ec43edaSMartin Leung 	if (tg_inst >= pool->timing_generator_count)
192775441d9dSMikita Lipski 		return -1;
19285ec43edaSMartin Leung 
19295ec43edaSMartin Leung 	if (!res_ctx->pipe_ctx[tg_inst].stream) {
19305ec43edaSMartin Leung 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
19315ec43edaSMartin Leung 
19325ec43edaSMartin Leung 		pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
19335ec43edaSMartin Leung 		pipe_ctx->plane_res.mi = pool->mis[tg_inst];
19345ec43edaSMartin Leung 		pipe_ctx->plane_res.hubp = pool->hubps[tg_inst];
19355ec43edaSMartin Leung 		pipe_ctx->plane_res.ipp = pool->ipps[tg_inst];
19365ec43edaSMartin Leung 		pipe_ctx->plane_res.xfm = pool->transforms[tg_inst];
19375ec43edaSMartin Leung 		pipe_ctx->plane_res.dpp = pool->dpps[tg_inst];
19385ec43edaSMartin Leung 		pipe_ctx->stream_res.opp = pool->opps[tg_inst];
19395ec43edaSMartin Leung 
1940ccce745cSMartin Leung 		if (pool->dpps[tg_inst]) {
19415ec43edaSMartin Leung 			pipe_ctx->plane_res.mpcc_inst = pool->dpps[tg_inst]->inst;
1942ccce745cSMartin Leung 
1943ccce745cSMartin Leung 			// Read DPP->MPCC->OPP Pipe from HW State
1944ccce745cSMartin Leung 			if (pool->mpc->funcs->read_mpcc_state) {
1945ccce745cSMartin Leung 				struct mpcc_state s = {0};
1946ccce745cSMartin Leung 
1947ccce745cSMartin Leung 				pool->mpc->funcs->read_mpcc_state(pool->mpc, pipe_ctx->plane_res.mpcc_inst, &s);
1948ccce745cSMartin Leung 
1949ccce745cSMartin Leung 				if (s.dpp_id < MAX_MPCC)
1950ccce745cSMartin Leung 					pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].dpp_id = s.dpp_id;
1951ccce745cSMartin Leung 
1952ccce745cSMartin Leung 				if (s.bot_mpcc_id < MAX_MPCC)
1953ccce745cSMartin Leung 					pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].mpcc_bot =
1954ccce745cSMartin Leung 							&pool->mpc->mpcc_array[s.bot_mpcc_id];
1955ccce745cSMartin Leung 
1956ccce745cSMartin Leung 				if (s.opp_id < MAX_OPP)
1957ccce745cSMartin Leung 					pipe_ctx->stream_res.opp->mpc_tree_params.opp_id = s.opp_id;
1958ccce745cSMartin Leung 			}
1959ccce745cSMartin Leung 		}
19605ec43edaSMartin Leung 		pipe_ctx->pipe_idx = tg_inst;
1961d2d7885fSAnthony Koo 
1962d2d7885fSAnthony Koo 		pipe_ctx->stream = stream;
19635ec43edaSMartin Leung 		return tg_inst;
1964d2d7885fSAnthony Koo 	}
1965d2d7885fSAnthony Koo 
1966d2d7885fSAnthony Koo 	return -1;
1967d2d7885fSAnthony Koo }
1968d2d7885fSAnthony Koo 
196996b5e3e1SRaymond Yang static void mark_seamless_boot_stream(
197096b5e3e1SRaymond Yang 		const struct dc  *dc,
197196b5e3e1SRaymond Yang 		struct dc_stream_state *stream)
197296b5e3e1SRaymond Yang {
197396b5e3e1SRaymond Yang 	struct dc_bios *dcb = dc->ctx->dc_bios;
197496b5e3e1SRaymond Yang 
197596b5e3e1SRaymond Yang 	/* TODO: Check Linux */
197696b5e3e1SRaymond Yang 	if (dc->config.allow_seamless_boot_optimization &&
197796b5e3e1SRaymond Yang 			!dcb->funcs->is_accelerated_mode(dcb)) {
197896b5e3e1SRaymond Yang 		if (dc_validate_seamless_boot_timing(dc, stream->sink, &stream->timing))
197996b5e3e1SRaymond Yang 			stream->apply_seamless_boot_optimization = true;
198096b5e3e1SRaymond Yang 	}
198196b5e3e1SRaymond Yang }
198296b5e3e1SRaymond Yang 
19834562236bSHarry Wentland enum dc_status resource_map_pool_resources(
1984fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
1985608ac7bbSJerry Zuo 		struct dc_state *context,
19861dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
19874562236bSHarry Wentland {
1988a2b8659dSTony Cheng 	const struct resource_pool *pool = dc->res_pool;
19891dc90497SAndrey Grodzovsky 	int i;
19901dc90497SAndrey Grodzovsky 	struct dc_context *dc_ctx = dc->ctx;
19911dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = NULL;
19921dc90497SAndrey Grodzovsky 	int pipe_idx = -1;
19934562236bSHarry Wentland 
199408e1c28dSYogesh Mohan Marimuthu 	calculate_phy_pix_clks(stream);
199508e1c28dSYogesh Mohan Marimuthu 
199696b5e3e1SRaymond Yang 	mark_seamless_boot_stream(dc, stream);
199746570f09SAnthony Koo 
199896b5e3e1SRaymond Yang 	if (stream->apply_seamless_boot_optimization) {
1999d2d7885fSAnthony Koo 		pipe_idx = acquire_resource_from_hw_enabled_state(
2000d2d7885fSAnthony Koo 				&context->res_ctx,
2001d2d7885fSAnthony Koo 				pool,
2002d2d7885fSAnthony Koo 				stream);
200396b5e3e1SRaymond Yang 		if (pipe_idx < 0)
200496b5e3e1SRaymond Yang 			/* hw resource was assigned to other stream */
200596b5e3e1SRaymond Yang 			stream->apply_seamless_boot_optimization = false;
200696b5e3e1SRaymond Yang 	}
2007d2d7885fSAnthony Koo 
2008d2d7885fSAnthony Koo 	if (pipe_idx < 0)
20094562236bSHarry Wentland 		/* acquire new resources */
20105d11e9fcSDmytro Laktyushkin 		pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
20111dc90497SAndrey Grodzovsky 
2012b86a1aa3SBhawanpreet Lakha #ifdef CONFIG_DRM_AMD_DC_DCN
20135d11e9fcSDmytro Laktyushkin 	if (pipe_idx < 0)
201413ab1b44SYongqiang Sun 		pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
201594c6d735SHarry Wentland #endif
201613ab1b44SYongqiang Sun 
2017c5b38aecSDmytro Laktyushkin 	if (pipe_idx < 0 || context->res_ctx.pipe_ctx[pipe_idx].stream_res.tg == NULL)
20184562236bSHarry Wentland 		return DC_NO_CONTROLLER_RESOURCE;
20194562236bSHarry Wentland 
20204562236bSHarry Wentland 	pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
20214562236bSHarry Wentland 
20228e9c4c8cSHarry Wentland 	pipe_ctx->stream_res.stream_enc =
202378cc70b1SWesley Chalmers 		dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
2024a2b8659dSTony Cheng 			&context->res_ctx, pool, stream);
20254562236bSHarry Wentland 
20268e9c4c8cSHarry Wentland 	if (!pipe_ctx->stream_res.stream_enc)
202738684e46SEric Bernstein 		return DC_NO_STREAM_ENC_RESOURCE;
20284562236bSHarry Wentland 
20291dc90497SAndrey Grodzovsky 	update_stream_engine_usage(
2030a2b8659dSTony Cheng 		&context->res_ctx, pool,
20311dc90497SAndrey Grodzovsky 		pipe_ctx->stream_res.stream_enc,
20321dc90497SAndrey Grodzovsky 		true);
20334562236bSHarry Wentland 
20344562236bSHarry Wentland 	/* TODO: Add check if ASIC support and EDID audio */
2035ceb3dbb4SJun Lei 	if (!stream->converter_disable_audio &&
20364562236bSHarry Wentland 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
2037ce08aad3SAlvin Lee 	    stream->audio_info.mode_count && stream->audio_info.flags.all) {
2038afaacef4SHarry Wentland 		pipe_ctx->stream_res.audio = find_first_free_audio(
2039f24b0522SPaul Hsieh 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
20404562236bSHarry Wentland 
20414562236bSHarry Wentland 		/*
20424562236bSHarry Wentland 		 * Audio assigned in order first come first get.
20434562236bSHarry Wentland 		 * There are asics which has number of audio
20444562236bSHarry Wentland 		 * resources less then number of pipes
20454562236bSHarry Wentland 		 */
2046afaacef4SHarry Wentland 		if (pipe_ctx->stream_res.audio)
20471dc90497SAndrey Grodzovsky 			update_audio_usage(&context->res_ctx, pool,
20481dc90497SAndrey Grodzovsky 					   pipe_ctx->stream_res.audio, true);
20494562236bSHarry Wentland 	}
20504562236bSHarry Wentland 
20519aef1a31SSivapiriyanKumarasamy 	/* Add ABM to the resource if on EDP */
20525dba4991SBhawanpreet Lakha 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal)) {
205320f2ffe5SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN)
20545dba4991SBhawanpreet Lakha 		if (pool->abm)
20559aef1a31SSivapiriyanKumarasamy 			pipe_ctx->stream_res.abm = pool->abm;
20565dba4991SBhawanpreet Lakha 		else
20575dba4991SBhawanpreet Lakha 			pipe_ctx->stream_res.abm = pool->multiple_abms[pipe_ctx->stream_res.tg->inst];
20585dba4991SBhawanpreet Lakha #else
20595dba4991SBhawanpreet Lakha 		pipe_ctx->stream_res.abm = pool->abm;
20605dba4991SBhawanpreet Lakha #endif
20615dba4991SBhawanpreet Lakha 	}
20629aef1a31SSivapiriyanKumarasamy 
20631dc90497SAndrey Grodzovsky 	for (i = 0; i < context->stream_count; i++)
20641dc90497SAndrey Grodzovsky 		if (context->streams[i] == stream) {
20656b670fa9SHarry Wentland 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
20663f0940f8SCharlene Liu 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->stream_enc_inst;
20675fdb7c4cSNicholas Kazlauskas 			context->stream_status[i].audio_inst =
20685fdb7c4cSNicholas Kazlauskas 				pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
20695fdb7c4cSNicholas Kazlauskas 
20701dc90497SAndrey Grodzovsky 			return DC_OK;
20714562236bSHarry Wentland 		}
20724562236bSHarry Wentland 
20731dc90497SAndrey Grodzovsky 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
20741dc90497SAndrey Grodzovsky 	return DC_ERROR_UNEXPECTED;
20754562236bSHarry Wentland }
20764562236bSHarry Wentland 
20772119aa17SDavid Francis /**
20782119aa17SDavid Francis  * dc_resource_state_copy_construct_current() - Creates a new dc_state from existing state
20792119aa17SDavid Francis  * Is a shallow copy.  Increments refcounts on existing streams and planes.
20802119aa17SDavid Francis  * @dc: copy out of dc->current_state
20812119aa17SDavid Francis  * @dst_ctx: copy into this
20822119aa17SDavid Francis  */
2083f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct_current(
20841dc90497SAndrey Grodzovsky 		const struct dc *dc,
2085608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
20861dc90497SAndrey Grodzovsky {
2087f36cc577SBhawanpreet Lakha 	dc_resource_state_copy_construct(dc->current_state, dst_ctx);
20881dc90497SAndrey Grodzovsky }
20891dc90497SAndrey Grodzovsky 
2090ab8db3e1SAndrey Grodzovsky 
2091ab8db3e1SAndrey Grodzovsky void dc_resource_state_construct(
2092ab8db3e1SAndrey Grodzovsky 		const struct dc *dc,
2093ab8db3e1SAndrey Grodzovsky 		struct dc_state *dst_ctx)
2094ab8db3e1SAndrey Grodzovsky {
2095dc88b4a6SEric Yang 	dst_ctx->clk_mgr = dc->clk_mgr;
2096ab8db3e1SAndrey Grodzovsky }
2097ab8db3e1SAndrey Grodzovsky 
20986d822156SNikola Cornij 
20996d822156SNikola Cornij bool dc_resource_is_dsc_encoding_supported(const struct dc *dc)
21006d822156SNikola Cornij {
21016d822156SNikola Cornij 	return dc->res_pool->res_cap->num_dsc > 0;
21026d822156SNikola Cornij }
21036d822156SNikola Cornij 
21046d822156SNikola Cornij 
21052119aa17SDavid Francis /**
21062119aa17SDavid Francis  * dc_validate_global_state() - Determine if HW can support a given state
21072119aa17SDavid Francis  * Checks HW resource availability and bandwidth requirement.
21082119aa17SDavid Francis  * @dc: dc struct for this driver
21092119aa17SDavid Francis  * @new_ctx: state to be validated
2110afcd526bSJoshua Aberback  * @fast_validate: set to true if only yes/no to support matters
21112119aa17SDavid Francis  *
21122119aa17SDavid Francis  * Return: DC_OK if the result can be programmed.  Otherwise, an error code.
21132119aa17SDavid Francis  */
2114e750d56dSYongqiang Sun enum dc_status dc_validate_global_state(
21151dc90497SAndrey Grodzovsky 		struct dc *dc,
2116afcd526bSJoshua Aberback 		struct dc_state *new_ctx,
2117afcd526bSJoshua Aberback 		bool fast_validate)
21181dc90497SAndrey Grodzovsky {
21191dc90497SAndrey Grodzovsky 	enum dc_status result = DC_ERROR_UNEXPECTED;
21201dc90497SAndrey Grodzovsky 	int i, j;
21211dc90497SAndrey Grodzovsky 
2122e41ab030SHarry Wentland 	if (!new_ctx)
2123e41ab030SHarry Wentland 		return DC_ERROR_UNEXPECTED;
2124*cbaf919fSNicholas Kazlauskas #if defined(CONFIG_DRM_AMD_DC_DCN3_1)
2125*cbaf919fSNicholas Kazlauskas 
2126*cbaf919fSNicholas Kazlauskas 	/*
2127*cbaf919fSNicholas Kazlauskas 	 * Update link encoder to stream assignment.
2128*cbaf919fSNicholas Kazlauskas 	 * TODO: Split out reason allocation from validation.
2129*cbaf919fSNicholas Kazlauskas 	 */
2130*cbaf919fSNicholas Kazlauskas 	if (dc->res_pool->funcs->link_encs_assign)
2131*cbaf919fSNicholas Kazlauskas 		dc->res_pool->funcs->link_encs_assign(
2132*cbaf919fSNicholas Kazlauskas 			dc, new_ctx, new_ctx->streams, new_ctx->stream_count);
2133*cbaf919fSNicholas Kazlauskas #endif
2134e41ab030SHarry Wentland 
2135d596e5d0SYongqiang Sun 	if (dc->res_pool->funcs->validate_global) {
2136d596e5d0SYongqiang Sun 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
2137d596e5d0SYongqiang Sun 		if (result != DC_OK)
2138d596e5d0SYongqiang Sun 			return result;
2139d596e5d0SYongqiang Sun 	}
21401dc90497SAndrey Grodzovsky 
2141e41ab030SHarry Wentland 	for (i = 0; i < new_ctx->stream_count; i++) {
21421dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream = new_ctx->streams[i];
21431dc90497SAndrey Grodzovsky 
21441dc90497SAndrey Grodzovsky 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
21451dc90497SAndrey Grodzovsky 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
21461dc90497SAndrey Grodzovsky 
21471dc90497SAndrey Grodzovsky 			if (pipe_ctx->stream != stream)
21481dc90497SAndrey Grodzovsky 				continue;
21491dc90497SAndrey Grodzovsky 
21508d8c82b6SJoseph Gravenor 			if (dc->res_pool->funcs->patch_unknown_plane_state &&
215174eac5f3SSu Sung Chung 					pipe_ctx->plane_state &&
215274eac5f3SSu Sung Chung 					pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
21538d8c82b6SJoseph Gravenor 				result = dc->res_pool->funcs->patch_unknown_plane_state(pipe_ctx->plane_state);
215474eac5f3SSu Sung Chung 				if (result != DC_OK)
215574eac5f3SSu Sung Chung 					return result;
215674eac5f3SSu Sung Chung 			}
215774eac5f3SSu Sung Chung 
21581dc90497SAndrey Grodzovsky 			/* Switch to dp clock source only if there is
21591dc90497SAndrey Grodzovsky 			 * no non dp stream that shares the same timing
21601dc90497SAndrey Grodzovsky 			 * with the dp stream.
21611dc90497SAndrey Grodzovsky 			 */
21621dc90497SAndrey Grodzovsky 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
21631dc90497SAndrey Grodzovsky 				!find_pll_sharable_stream(stream, new_ctx)) {
21641dc90497SAndrey Grodzovsky 
21659d0dcecdSHarry Wentland 				resource_unreference_clock_source(
21661dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
21671dc90497SAndrey Grodzovsky 						dc->res_pool,
21689d0dcecdSHarry Wentland 						pipe_ctx->clock_source);
21694a629536SHarry Wentland 
21701dc90497SAndrey Grodzovsky 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
21711dc90497SAndrey Grodzovsky 				resource_reference_clock_source(
21721dc90497SAndrey Grodzovsky 						&new_ctx->res_ctx,
21731dc90497SAndrey Grodzovsky 						dc->res_pool,
21741dc90497SAndrey Grodzovsky 						 pipe_ctx->clock_source);
21751dc90497SAndrey Grodzovsky 			}
21761dc90497SAndrey Grodzovsky 		}
21771dc90497SAndrey Grodzovsky 	}
21781dc90497SAndrey Grodzovsky 
21791dc90497SAndrey Grodzovsky 	result = resource_build_scaling_params_for_context(dc, new_ctx);
21801dc90497SAndrey Grodzovsky 
21811dc90497SAndrey Grodzovsky 	if (result == DC_OK)
2182afcd526bSJoshua Aberback 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, fast_validate))
21831dc90497SAndrey Grodzovsky 			result = DC_FAIL_BANDWIDTH_VALIDATE;
21841dc90497SAndrey Grodzovsky 
21851dc90497SAndrey Grodzovsky 	return result;
21861dc90497SAndrey Grodzovsky }
21871dc90497SAndrey Grodzovsky 
21886e4d6beeSTony Cheng static void patch_gamut_packet_checksum(
2189e09b6473SAnthony Koo 		struct dc_info_packet *gamut_packet)
21904562236bSHarry Wentland {
21914562236bSHarry Wentland 	/* For gamut we recalc checksum */
21926e4d6beeSTony Cheng 	if (gamut_packet->valid) {
21934562236bSHarry Wentland 		uint8_t chk_sum = 0;
21944562236bSHarry Wentland 		uint8_t *ptr;
21954562236bSHarry Wentland 		uint8_t i;
21964562236bSHarry Wentland 
21974562236bSHarry Wentland 		/*start of the Gamut data. */
21986e4d6beeSTony Cheng 		ptr = &gamut_packet->sb[3];
21994562236bSHarry Wentland 
22006e4d6beeSTony Cheng 		for (i = 0; i <= gamut_packet->sb[1]; i++)
22014562236bSHarry Wentland 			chk_sum += ptr[i];
22024562236bSHarry Wentland 
22036e4d6beeSTony Cheng 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
22041646a6feSAndrew Wong 	}
22054562236bSHarry Wentland }
22064562236bSHarry Wentland 
22074562236bSHarry Wentland static void set_avi_info_frame(
2208e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
22094562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
22104562236bSHarry Wentland {
22110971c40eSHarry Wentland 	struct dc_stream_state *stream = pipe_ctx->stream;
22124562236bSHarry Wentland 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
22134562236bSHarry Wentland 	uint32_t pixel_encoding = 0;
22144562236bSHarry Wentland 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
22154562236bSHarry Wentland 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
22164562236bSHarry Wentland 	bool itc = false;
221750e27654SZeyu Fan 	uint8_t itc_value = 0;
22184562236bSHarry Wentland 	uint8_t cn0_cn1 = 0;
221950e27654SZeyu Fan 	unsigned int cn0_cn1_value = 0;
22204562236bSHarry Wentland 	uint8_t *check_sum = NULL;
22214562236bSHarry Wentland 	uint8_t byte_index = 0;
2222754e3673SAnthony Koo 	union hdmi_info_packet hdmi_info;
222350e27654SZeyu Fan 	union display_content_support support = {0};
22244fa086b9SLeo (Sunpeng) Li 	unsigned int vic = pipe_ctx->stream->timing.vic;
222515e17335SCharlene Liu 	enum dc_timing_3d_format format;
22264562236bSHarry Wentland 
2227754e3673SAnthony Koo 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
2228754e3673SAnthony Koo 
22294fa086b9SLeo (Sunpeng) Li 	color_space = pipe_ctx->stream->output_color_space;
2230e5f2038eSCharlene Liu 	if (color_space == COLOR_SPACE_UNKNOWN)
22314fa086b9SLeo (Sunpeng) Li 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
2232e5f2038eSCharlene Liu 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
22334562236bSHarry Wentland 
22344562236bSHarry Wentland 	/* Initialize header */
2235e09b6473SAnthony Koo 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
22364562236bSHarry Wentland 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
22374562236bSHarry Wentland 	* not be used in HDMI 2.0 (Section 10.1) */
2238e09b6473SAnthony Koo 	hdmi_info.bits.header.version = 2;
2239e09b6473SAnthony Koo 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
22404562236bSHarry Wentland 
22414562236bSHarry Wentland 	/*
22424562236bSHarry Wentland 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
22434562236bSHarry Wentland 	 * according to HDMI 2.0 spec (Section 10.1)
22444562236bSHarry Wentland 	 */
22454562236bSHarry Wentland 
22464fa086b9SLeo (Sunpeng) Li 	switch (stream->timing.pixel_encoding) {
22474562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR422:
22484562236bSHarry Wentland 		pixel_encoding = 1;
22494562236bSHarry Wentland 		break;
22504562236bSHarry Wentland 
22514562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR444:
22524562236bSHarry Wentland 		pixel_encoding = 2;
22534562236bSHarry Wentland 		break;
22544562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR420:
22554562236bSHarry Wentland 		pixel_encoding = 3;
22564562236bSHarry Wentland 		break;
22574562236bSHarry Wentland 
22584562236bSHarry Wentland 	case PIXEL_ENCODING_RGB:
22594562236bSHarry Wentland 	default:
22604562236bSHarry Wentland 		pixel_encoding = 0;
22614562236bSHarry Wentland 	}
22624562236bSHarry Wentland 
22634562236bSHarry Wentland 	/* Y0_Y1_Y2 : The pixel encoding */
22644562236bSHarry Wentland 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
2265e09b6473SAnthony Koo 	hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
22664562236bSHarry Wentland 
22674562236bSHarry Wentland 	/* A0 = 1 Active Format Information valid */
2268e09b6473SAnthony Koo 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
22694562236bSHarry Wentland 
22704562236bSHarry Wentland 	/* B0, B1 = 3; Bar info data is valid */
2271e09b6473SAnthony Koo 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
22724562236bSHarry Wentland 
2273e09b6473SAnthony Koo 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
22744562236bSHarry Wentland 
22754562236bSHarry Wentland 	/* S0, S1 : Underscan / Overscan */
22764562236bSHarry Wentland 	/* TODO: un-hardcode scan type */
22774562236bSHarry Wentland 	scan_type = SCANNING_TYPE_UNDERSCAN;
2278e09b6473SAnthony Koo 	hdmi_info.bits.S0_S1 = scan_type;
22794562236bSHarry Wentland 
22804562236bSHarry Wentland 	/* C0, C1 : Colorimetry */
22818fde5884SCharlene Liu 	if (color_space == COLOR_SPACE_YCBCR709 ||
228215e17335SCharlene Liu 			color_space == COLOR_SPACE_YCBCR709_LIMITED)
2283e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
22848fde5884SCharlene Liu 	else if (color_space == COLOR_SPACE_YCBCR601 ||
22858fde5884SCharlene Liu 			color_space == COLOR_SPACE_YCBCR601_LIMITED)
2286e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
22878fde5884SCharlene Liu 	else {
2288e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
22898fde5884SCharlene Liu 	}
2290534db198SAmy Zhang 	if (color_space == COLOR_SPACE_2020_RGB_FULLRANGE ||
2291534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE ||
2292534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_YCBCR) {
2293e09b6473SAnthony Koo 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
2294e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2295534db198SAmy Zhang 	} else if (color_space == COLOR_SPACE_ADOBERGB) {
2296e09b6473SAnthony Koo 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
2297e09b6473SAnthony Koo 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2298534db198SAmy Zhang 	}
2299534db198SAmy Zhang 
23004562236bSHarry Wentland 	/* TODO: un-hardcode aspect ratio */
23014fa086b9SLeo (Sunpeng) Li 	aspect = stream->timing.aspect_ratio;
23024562236bSHarry Wentland 
23034562236bSHarry Wentland 	switch (aspect) {
23044562236bSHarry Wentland 	case ASPECT_RATIO_4_3:
23054562236bSHarry Wentland 	case ASPECT_RATIO_16_9:
2306e09b6473SAnthony Koo 		hdmi_info.bits.M0_M1 = aspect;
23074562236bSHarry Wentland 		break;
23084562236bSHarry Wentland 
23094562236bSHarry Wentland 	case ASPECT_RATIO_NO_DATA:
23104562236bSHarry Wentland 	case ASPECT_RATIO_64_27:
23114562236bSHarry Wentland 	case ASPECT_RATIO_256_135:
23124562236bSHarry Wentland 	default:
2313e09b6473SAnthony Koo 		hdmi_info.bits.M0_M1 = 0;
23144562236bSHarry Wentland 	}
23154562236bSHarry Wentland 
23164562236bSHarry Wentland 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
2317e09b6473SAnthony Koo 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
23184562236bSHarry Wentland 
23194562236bSHarry Wentland 	/* TODO: un-hardcode cn0_cn1 and itc */
232050e27654SZeyu Fan 
23214562236bSHarry Wentland 	cn0_cn1 = 0;
232250e27654SZeyu Fan 	cn0_cn1_value = 0;
232350e27654SZeyu Fan 
232450e27654SZeyu Fan 	itc = true;
232550e27654SZeyu Fan 	itc_value = 1;
232650e27654SZeyu Fan 
2327ceb3dbb4SJun Lei 	support = stream->content_support;
23284562236bSHarry Wentland 
23294562236bSHarry Wentland 	if (itc) {
233050e27654SZeyu Fan 		if (!support.bits.valid_content_type) {
233150e27654SZeyu Fan 			cn0_cn1_value = 0;
233250e27654SZeyu Fan 		} else {
233350e27654SZeyu Fan 			if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
233450e27654SZeyu Fan 				if (support.bits.graphics_content == 1) {
233550e27654SZeyu Fan 					cn0_cn1_value = 0;
233650e27654SZeyu Fan 				}
233750e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
233850e27654SZeyu Fan 				if (support.bits.photo_content == 1) {
233950e27654SZeyu Fan 					cn0_cn1_value = 1;
234050e27654SZeyu Fan 				} else {
234150e27654SZeyu Fan 					cn0_cn1_value = 0;
234250e27654SZeyu Fan 					itc_value = 0;
234350e27654SZeyu Fan 				}
234450e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
234550e27654SZeyu Fan 				if (support.bits.cinema_content == 1) {
234650e27654SZeyu Fan 					cn0_cn1_value = 2;
234750e27654SZeyu Fan 				} else {
234850e27654SZeyu Fan 					cn0_cn1_value = 0;
234950e27654SZeyu Fan 					itc_value = 0;
235050e27654SZeyu Fan 				}
235150e27654SZeyu Fan 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
235250e27654SZeyu Fan 				if (support.bits.game_content == 1) {
235350e27654SZeyu Fan 					cn0_cn1_value = 3;
235450e27654SZeyu Fan 				} else {
235550e27654SZeyu Fan 					cn0_cn1_value = 0;
235650e27654SZeyu Fan 					itc_value = 0;
235750e27654SZeyu Fan 				}
235850e27654SZeyu Fan 			}
235950e27654SZeyu Fan 		}
2360e09b6473SAnthony Koo 		hdmi_info.bits.CN0_CN1 = cn0_cn1_value;
2361e09b6473SAnthony Koo 		hdmi_info.bits.ITC = itc_value;
23624562236bSHarry Wentland 	}
23634562236bSHarry Wentland 
2364fdf7d4f5SDillon Varone 	if (stream->qs_bit == 1) {
2365fdf7d4f5SDillon Varone 		if (color_space == COLOR_SPACE_SRGB ||
2366fdf7d4f5SDillon Varone 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
2367fdf7d4f5SDillon Varone 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
2368fdf7d4f5SDillon Varone 		else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
2369fdf7d4f5SDillon Varone 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
2370fdf7d4f5SDillon Varone 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
2371fdf7d4f5SDillon Varone 		else
2372fdf7d4f5SDillon Varone 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2373fdf7d4f5SDillon Varone 	} else
2374fdf7d4f5SDillon Varone 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2375fdf7d4f5SDillon Varone 
23764562236bSHarry Wentland 	/* TODO : We should handle YCC quantization */
23774562236bSHarry Wentland 	/* but we do not have matrix calculation */
2378fdf7d4f5SDillon Varone 	if (stream->qy_bit == 1) {
237950e27654SZeyu Fan 		if (color_space == COLOR_SPACE_SRGB ||
2380fdf7d4f5SDillon Varone 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
2381993dca3eSQingqing Zhuo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2382fdf7d4f5SDillon Varone 		else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
2383fdf7d4f5SDillon Varone 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
2384e09b6473SAnthony Koo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2385fdf7d4f5SDillon Varone 		else
2386e09b6473SAnthony Koo 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2387fdf7d4f5SDillon Varone 	} else
2388e09b6473SAnthony Koo 		hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
238950e27654SZeyu Fan 
239015e17335SCharlene Liu 	///VIC
23914fa086b9SLeo (Sunpeng) Li 	format = stream->timing.timing_3d_format;
239215e17335SCharlene Liu 	/*todo, add 3DStereo support*/
239315e17335SCharlene Liu 	if (format != TIMING_3D_FORMAT_NONE) {
239415e17335SCharlene Liu 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
23954fa086b9SLeo (Sunpeng) Li 		switch (pipe_ctx->stream->timing.hdmi_vic) {
239615e17335SCharlene Liu 		case 1:
239715e17335SCharlene Liu 			vic = 95;
239815e17335SCharlene Liu 			break;
239915e17335SCharlene Liu 		case 2:
240015e17335SCharlene Liu 			vic = 94;
240115e17335SCharlene Liu 			break;
240215e17335SCharlene Liu 		case 3:
240315e17335SCharlene Liu 			vic = 93;
240415e17335SCharlene Liu 			break;
240515e17335SCharlene Liu 		case 4:
240615e17335SCharlene Liu 			vic = 98;
240715e17335SCharlene Liu 			break;
240815e17335SCharlene Liu 		default:
240915e17335SCharlene Liu 			break;
241015e17335SCharlene Liu 		}
241115e17335SCharlene Liu 	}
2412efa02336SChris Park 	/* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
2413e09b6473SAnthony Koo 	hdmi_info.bits.VIC0_VIC7 = vic;
2414efa02336SChris Park 	if (vic >= 128)
2415efa02336SChris Park 		hdmi_info.bits.header.version = 3;
2416efa02336SChris Park 	/* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
2417efa02336SChris Park 	 * the Source shall use 20 AVI InfoFrame Version 4
2418efa02336SChris Park 	 */
2419efa02336SChris Park 	if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
2420efa02336SChris Park 			hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
2421efa02336SChris Park 		hdmi_info.bits.header.version = 4;
2422efa02336SChris Park 		hdmi_info.bits.header.length = 14;
2423efa02336SChris Park 	}
24244562236bSHarry Wentland 
24254562236bSHarry Wentland 	/* pixel repetition
24264562236bSHarry Wentland 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
24274562236bSHarry Wentland 	 * repetition start from 1 */
2428e09b6473SAnthony Koo 	hdmi_info.bits.PR0_PR3 = 0;
24294562236bSHarry Wentland 
24304562236bSHarry Wentland 	/* Bar Info
24314562236bSHarry Wentland 	 * barTop:    Line Number of End of Top Bar.
24324562236bSHarry Wentland 	 * barBottom: Line Number of Start of Bottom Bar.
24334562236bSHarry Wentland 	 * barLeft:   Pixel Number of End of Left Bar.
24344562236bSHarry Wentland 	 * barRight:  Pixel Number of Start of Right Bar. */
2435e09b6473SAnthony Koo 	hdmi_info.bits.bar_top = stream->timing.v_border_top;
2436e09b6473SAnthony Koo 	hdmi_info.bits.bar_bottom = (stream->timing.v_total
24374fa086b9SLeo (Sunpeng) Li 			- stream->timing.v_border_bottom + 1);
2438e09b6473SAnthony Koo 	hdmi_info.bits.bar_left  = stream->timing.h_border_left;
2439e09b6473SAnthony Koo 	hdmi_info.bits.bar_right = (stream->timing.h_total
24404fa086b9SLeo (Sunpeng) Li 			- stream->timing.h_border_right + 1);
24414562236bSHarry Wentland 
24422f482c4fSChris Park     /* Additional Colorimetry Extension
24432f482c4fSChris Park      * Used in conduction with C0-C1 and EC0-EC2
24442f482c4fSChris Park      * 0 = DCI-P3 RGB (D65)
24452f482c4fSChris Park      * 1 = DCI-P3 RGB (theater)
24462f482c4fSChris Park      */
24472f482c4fSChris Park 	hdmi_info.bits.ACE0_ACE3 = 0;
24482f482c4fSChris Park 
24494562236bSHarry Wentland 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
2450e09b6473SAnthony Koo 	check_sum = &hdmi_info.packet_raw_data.sb[0];
2451e8d726b7SReza Amini 
2452efa02336SChris Park 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
24534562236bSHarry Wentland 
2454efa02336SChris Park 	for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
2455e09b6473SAnthony Koo 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
24564562236bSHarry Wentland 
24574562236bSHarry Wentland 	/* one byte complement */
24584562236bSHarry Wentland 	*check_sum = (uint8_t) (0x100 - *check_sum);
24594562236bSHarry Wentland 
24604562236bSHarry Wentland 	/* Store in hw_path_mode */
2461e09b6473SAnthony Koo 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
2462e09b6473SAnthony Koo 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
2463e09b6473SAnthony Koo 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
24644562236bSHarry Wentland 
2465e09b6473SAnthony Koo 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
2466e09b6473SAnthony Koo 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
24674562236bSHarry Wentland 
24684562236bSHarry Wentland 	info_packet->valid = true;
24694562236bSHarry Wentland }
24704562236bSHarry Wentland 
24716e4d6beeSTony Cheng static void set_vendor_info_packet(
2472e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
24730971c40eSHarry Wentland 		struct dc_stream_state *stream)
24744562236bSHarry Wentland {
2475ecd0136bSHarmanprit Tatla 	/* SPD info packet for FreeSync */
247615e17335SCharlene Liu 
2477ecd0136bSHarmanprit Tatla 	/* Check if Freesync is supported. Return if false. If true,
2478ecd0136bSHarmanprit Tatla 	 * set the corresponding bit in the info packet
2479ecd0136bSHarmanprit Tatla 	 */
2480ecd0136bSHarmanprit Tatla 	if (!stream->vsp_infopacket.valid)
24814562236bSHarry Wentland 		return;
24824562236bSHarry Wentland 
2483ecd0136bSHarmanprit Tatla 	*info_packet = stream->vsp_infopacket;
24844562236bSHarry Wentland }
24854562236bSHarry Wentland 
24866e4d6beeSTony Cheng static void set_spd_info_packet(
2487e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
24880971c40eSHarry Wentland 		struct dc_stream_state *stream)
24894562236bSHarry Wentland {
24904562236bSHarry Wentland 	/* SPD info packet for FreeSync */
24914562236bSHarry Wentland 
24924562236bSHarry Wentland 	/* Check if Freesync is supported. Return if false. If true,
24934562236bSHarry Wentland 	 * set the corresponding bit in the info packet
24944562236bSHarry Wentland 	 */
249598e6436dSAnthony Koo 	if (!stream->vrr_infopacket.valid)
24964562236bSHarry Wentland 		return;
24974562236bSHarry Wentland 
249898e6436dSAnthony Koo 	*info_packet = stream->vrr_infopacket;
24994562236bSHarry Wentland }
25004562236bSHarry Wentland 
25011646a6feSAndrew Wong static void set_hdr_static_info_packet(
2502e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
25030971c40eSHarry Wentland 		struct dc_stream_state *stream)
25041646a6feSAndrew Wong {
25050eeef690SAnthony Koo 	/* HDR Static Metadata info packet for HDR10 */
25061646a6feSAndrew Wong 
2507a10dc97aSKrunoslav Kovac 	if (!stream->hdr_static_metadata.valid ||
2508a10dc97aSKrunoslav Kovac 			stream->use_dynamic_meta)
250910bff005SYongqiang Sun 		return;
251010bff005SYongqiang Sun 
25110eeef690SAnthony Koo 	*info_packet = stream->hdr_static_metadata;
25121646a6feSAndrew Wong }
25131646a6feSAndrew Wong 
25146e4d6beeSTony Cheng static void set_vsc_info_packet(
2515e09b6473SAnthony Koo 		struct dc_info_packet *info_packet,
25160971c40eSHarry Wentland 		struct dc_stream_state *stream)
25174562236bSHarry Wentland {
25181336926fSAlvin lee 	if (!stream->vsc_infopacket.valid)
25194562236bSHarry Wentland 		return;
25204562236bSHarry Wentland 
25211336926fSAlvin lee 	*info_packet = stream->vsc_infopacket;
25224562236bSHarry Wentland }
25234562236bSHarry Wentland 
2524f36cc577SBhawanpreet Lakha void dc_resource_state_destruct(struct dc_state *context)
25254562236bSHarry Wentland {
25264562236bSHarry Wentland 	int i, j;
25274562236bSHarry Wentland 
2528ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
25293be5262eSHarry Wentland 		for (j = 0; j < context->stream_status[i].plane_count; j++)
25303be5262eSHarry Wentland 			dc_plane_state_release(
25313be5262eSHarry Wentland 				context->stream_status[i].plane_states[j]);
25324562236bSHarry Wentland 
25333be5262eSHarry Wentland 		context->stream_status[i].plane_count = 0;
25344fa086b9SLeo (Sunpeng) Li 		dc_stream_release(context->streams[i]);
2535ab2541b6SAric Cyr 		context->streams[i] = NULL;
25364562236bSHarry Wentland 	}
25375728d5e5SPaul Wu 	context->stream_count = 0;
25384562236bSHarry Wentland }
25394562236bSHarry Wentland 
2540f36cc577SBhawanpreet Lakha void dc_resource_state_copy_construct(
2541608ac7bbSJerry Zuo 		const struct dc_state *src_ctx,
2542608ac7bbSJerry Zuo 		struct dc_state *dst_ctx)
25434562236bSHarry Wentland {
25444562236bSHarry Wentland 	int i, j;
25458ee5702aSDave Airlie 	struct kref refcount = dst_ctx->refcount;
25464562236bSHarry Wentland 
25474562236bSHarry Wentland 	*dst_ctx = *src_ctx;
25484562236bSHarry Wentland 
2549a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
25504562236bSHarry Wentland 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
25514562236bSHarry Wentland 
25524562236bSHarry Wentland 		if (cur_pipe->top_pipe)
25534562236bSHarry Wentland 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
25544562236bSHarry Wentland 
25554562236bSHarry Wentland 		if (cur_pipe->bottom_pipe)
25564562236bSHarry Wentland 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
2557b1f6d01cSDmytro Laktyushkin 
2558b1f6d01cSDmytro Laktyushkin 		if (cur_pipe->next_odm_pipe)
2559b1f6d01cSDmytro Laktyushkin 			cur_pipe->next_odm_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
2560b1f6d01cSDmytro Laktyushkin 
2561b1f6d01cSDmytro Laktyushkin 		if (cur_pipe->prev_odm_pipe)
2562b1f6d01cSDmytro Laktyushkin 			cur_pipe->prev_odm_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
25634562236bSHarry Wentland 	}
25644562236bSHarry Wentland 
2565ab2541b6SAric Cyr 	for (i = 0; i < dst_ctx->stream_count; i++) {
25664fa086b9SLeo (Sunpeng) Li 		dc_stream_retain(dst_ctx->streams[i]);
25673be5262eSHarry Wentland 		for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
25683be5262eSHarry Wentland 			dc_plane_state_retain(
25693be5262eSHarry Wentland 				dst_ctx->stream_status[i].plane_states[j]);
25704562236bSHarry Wentland 	}
25719a3afbb3SAndrey Grodzovsky 
25729a3afbb3SAndrey Grodzovsky 	/* context refcount should not be overridden */
25738ee5702aSDave Airlie 	dst_ctx->refcount = refcount;
25749a3afbb3SAndrey Grodzovsky 
25754562236bSHarry Wentland }
25764562236bSHarry Wentland 
25774562236bSHarry Wentland struct clock_source *dc_resource_find_first_free_pll(
2578a2b8659dSTony Cheng 		struct resource_context *res_ctx,
2579a2b8659dSTony Cheng 		const struct resource_pool *pool)
25804562236bSHarry Wentland {
25814562236bSHarry Wentland 	int i;
25824562236bSHarry Wentland 
2583a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; ++i) {
25844562236bSHarry Wentland 		if (res_ctx->clock_source_ref_count[i] == 0)
2585a2b8659dSTony Cheng 			return pool->clock_sources[i];
25864562236bSHarry Wentland 	}
25874562236bSHarry Wentland 
25884562236bSHarry Wentland 	return NULL;
25894562236bSHarry Wentland }
25904562236bSHarry Wentland 
25914562236bSHarry Wentland void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
25924562236bSHarry Wentland {
25934562236bSHarry Wentland 	enum signal_type signal = SIGNAL_TYPE_NONE;
259496c50c0dSHarry Wentland 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
25954562236bSHarry Wentland 
25964562236bSHarry Wentland 	/* default all packets to invalid */
25976e4d6beeSTony Cheng 	info->avi.valid = false;
25986e4d6beeSTony Cheng 	info->gamut.valid = false;
25996e4d6beeSTony Cheng 	info->vendor.valid = false;
2600630e3573SJeff Smith 	info->spd.valid = false;
26016e4d6beeSTony Cheng 	info->hdrsmd.valid = false;
26026e4d6beeSTony Cheng 	info->vsc.valid = false;
26034562236bSHarry Wentland 
26044562236bSHarry Wentland 	signal = pipe_ctx->stream->signal;
26054562236bSHarry Wentland 
26064562236bSHarry Wentland 	/* HDMi and DP have different info packets*/
26074562236bSHarry Wentland 	if (dc_is_hdmi_signal(signal)) {
26086e4d6beeSTony Cheng 		set_avi_info_frame(&info->avi, pipe_ctx);
26096e4d6beeSTony Cheng 
26106e4d6beeSTony Cheng 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
26116e4d6beeSTony Cheng 
26126e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
26136e4d6beeSTony Cheng 
261456ef6ed9SAnthony Koo 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
26156e4d6beeSTony Cheng 
2616a33fa99dSHarry Wentland 	} else if (dc_is_dp_signal(signal)) {
26176e4d6beeSTony Cheng 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
26186e4d6beeSTony Cheng 
26196e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
26206e4d6beeSTony Cheng 
262156ef6ed9SAnthony Koo 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
2622a33fa99dSHarry Wentland 	}
26234562236bSHarry Wentland 
26246e4d6beeSTony Cheng 	patch_gamut_packet_checksum(&info->gamut);
26254562236bSHarry Wentland }
26264562236bSHarry Wentland 
26274562236bSHarry Wentland enum dc_status resource_map_clock_resources(
2628fb3466a4SBhawanpreet Lakha 		const struct dc  *dc,
2629608ac7bbSJerry Zuo 		struct dc_state *context,
26301dc90497SAndrey Grodzovsky 		struct dc_stream_state *stream)
26314562236bSHarry Wentland {
26324562236bSHarry Wentland 	/* acquire new resources */
26331dc90497SAndrey Grodzovsky 	const struct resource_pool *pool = dc->res_pool;
26341dc90497SAndrey Grodzovsky 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(
26351dc90497SAndrey Grodzovsky 				&context->res_ctx, stream);
26364562236bSHarry Wentland 
26371dc90497SAndrey Grodzovsky 	if (!pipe_ctx)
26381dc90497SAndrey Grodzovsky 		return DC_ERROR_UNEXPECTED;
26394562236bSHarry Wentland 
26404562236bSHarry Wentland 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
26414562236bSHarry Wentland 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
2642a2b8659dSTony Cheng 		pipe_ctx->clock_source = pool->dp_clock_source;
26434562236bSHarry Wentland 	else {
26444562236bSHarry Wentland 		pipe_ctx->clock_source = NULL;
26454562236bSHarry Wentland 
2646fb3466a4SBhawanpreet Lakha 		if (!dc->config.disable_disp_pll_sharing)
26474ed4e51bSMikita Lipski 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
26484562236bSHarry Wentland 				&context->res_ctx,
26494562236bSHarry Wentland 				pipe_ctx);
26504562236bSHarry Wentland 
26514562236bSHarry Wentland 		if (pipe_ctx->clock_source == NULL)
26524562236bSHarry Wentland 			pipe_ctx->clock_source =
2653a2b8659dSTony Cheng 				dc_resource_find_first_free_pll(
2654a2b8659dSTony Cheng 					&context->res_ctx,
2655a2b8659dSTony Cheng 					pool);
26564562236bSHarry Wentland 	}
26574562236bSHarry Wentland 
26584562236bSHarry Wentland 	if (pipe_ctx->clock_source == NULL)
26594562236bSHarry Wentland 		return DC_NO_CLOCK_SOURCE_RESOURCE;
26604562236bSHarry Wentland 
26614562236bSHarry Wentland 	resource_reference_clock_source(
2662a2b8659dSTony Cheng 		&context->res_ctx, pool,
26634562236bSHarry Wentland 		pipe_ctx->clock_source);
26644562236bSHarry Wentland 
26654562236bSHarry Wentland 	return DC_OK;
26664562236bSHarry Wentland }
26674562236bSHarry Wentland 
26684562236bSHarry Wentland /*
26694562236bSHarry Wentland  * Note: We need to disable output if clock sources change,
26704562236bSHarry Wentland  * since bios does optimization and doesn't apply if changing
26714562236bSHarry Wentland  * PHY when not already disabled.
26724562236bSHarry Wentland  */
26734562236bSHarry Wentland bool pipe_need_reprogram(
26744562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx_old,
26754562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
26764562236bSHarry Wentland {
2677cfe4645eSDmytro Laktyushkin 	if (!pipe_ctx_old->stream)
2678cfe4645eSDmytro Laktyushkin 		return false;
2679cfe4645eSDmytro Laktyushkin 
26804562236bSHarry Wentland 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
26814562236bSHarry Wentland 		return true;
26824562236bSHarry Wentland 
26834562236bSHarry Wentland 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
26844562236bSHarry Wentland 		return true;
26854562236bSHarry Wentland 
2686afaacef4SHarry Wentland 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
26874562236bSHarry Wentland 		return true;
26884562236bSHarry Wentland 
26894562236bSHarry Wentland 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
26904562236bSHarry Wentland 			&& pipe_ctx_old->stream != pipe_ctx->stream)
26914562236bSHarry Wentland 		return true;
26924562236bSHarry Wentland 
26938e9c4c8cSHarry Wentland 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
26944562236bSHarry Wentland 		return true;
26954562236bSHarry Wentland 
26964562236bSHarry Wentland 	if (is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
26974562236bSHarry Wentland 		return true;
26984562236bSHarry Wentland 
26991e7e86c4SSamson Tam 	if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
27001e7e86c4SSamson Tam 		return true;
27011e7e86c4SSamson Tam 
2702eed928dcSCharlene Liu 	if (false == pipe_ctx_old->stream->link->link_state_valid &&
2703eed928dcSCharlene Liu 		false == pipe_ctx_old->stream->dpms_off)
2704eed928dcSCharlene Liu 		return true;
2705eed928dcSCharlene Liu 
270614e49bb3SNikola Cornij 	if (pipe_ctx_old->stream_res.dsc != pipe_ctx->stream_res.dsc)
270714e49bb3SNikola Cornij 		return true;
270814e49bb3SNikola Cornij 
2709f42ef862SJimmy Kizito 	/* DIG link encoder resource assignment for stream changed. */
2710f42ef862SJimmy Kizito 	if (pipe_ctx_old->stream->link_enc != pipe_ctx->stream->link_enc)
2711f42ef862SJimmy Kizito 		return true;
2712f42ef862SJimmy Kizito 
27134562236bSHarry Wentland 	return false;
27144562236bSHarry Wentland }
2715529cad0fSDing Wang 
27160971c40eSHarry Wentland void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
2717529cad0fSDing Wang 		struct bit_depth_reduction_params *fmt_bit_depth)
2718529cad0fSDing Wang {
27194fa086b9SLeo (Sunpeng) Li 	enum dc_dither_option option = stream->dither_option;
2720529cad0fSDing Wang 	enum dc_pixel_encoding pixel_encoding =
27214fa086b9SLeo (Sunpeng) Li 			stream->timing.pixel_encoding;
2722529cad0fSDing Wang 
2723529cad0fSDing Wang 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
2724529cad0fSDing Wang 
2725603767f9STony Cheng 	if (option == DITHER_OPTION_DEFAULT) {
2726603767f9STony Cheng 		switch (stream->timing.display_color_depth) {
2727603767f9STony Cheng 		case COLOR_DEPTH_666:
2728603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL6;
2729603767f9STony Cheng 			break;
2730603767f9STony Cheng 		case COLOR_DEPTH_888:
2731603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL8;
2732603767f9STony Cheng 			break;
2733603767f9STony Cheng 		case COLOR_DEPTH_101010:
2734603767f9STony Cheng 			option = DITHER_OPTION_SPATIAL10;
2735603767f9STony Cheng 			break;
2736603767f9STony Cheng 		default:
2737603767f9STony Cheng 			option = DITHER_OPTION_DISABLE;
2738603767f9STony Cheng 		}
2739603767f9STony Cheng 	}
2740603767f9STony Cheng 
2741529cad0fSDing Wang 	if (option == DITHER_OPTION_DISABLE)
2742529cad0fSDing Wang 		return;
2743529cad0fSDing Wang 
2744529cad0fSDing Wang 	if (option == DITHER_OPTION_TRUN6) {
2745529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2746529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
2747529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN8 ||
2748529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
2749529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6) {
2750529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2751529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
2752529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN10        ||
2753529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
2754529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
2755529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8     ||
2756529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2757529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2758529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2759529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2760529cad0fSDing Wang 	}
2761529cad0fSDing Wang 
2762529cad0fSDing Wang 	/* special case - Formatter can only reduce by 4 bits at most.
2763529cad0fSDing Wang 	 * When reducing from 12 to 6 bits,
2764529cad0fSDing Wang 	 * HW recommends we use trunc with round mode
2765529cad0fSDing Wang 	 * (if we did nothing, trunc to 10 bits would be used)
2766529cad0fSDing Wang 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
2767529cad0fSDing Wang 	 * as the input was 10 bits.
2768529cad0fSDing Wang 	 */
2769529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2770529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2771529cad0fSDing Wang 			option == DITHER_OPTION_FM6) {
2772529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2773529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2774529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
2775529cad0fSDing Wang 	}
2776529cad0fSDing Wang 
2777529cad0fSDing Wang 	/* spatial dither
2778529cad0fSDing Wang 	 * note that spatial modes 1-3 are never used
2779529cad0fSDing Wang 	 */
2780529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
2781529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2782529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
2783529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
2784529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2785529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
2786529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2787529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2788529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2789529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
2790529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2791529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6        ||
2792529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
2793529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2794529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2795529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
2796529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2797529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2798529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2799529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
2800529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10 ||
2801529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8 ||
2802529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6) {
2803529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2804529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
2805529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2806529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2807529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2808529cad0fSDing Wang 	}
2809529cad0fSDing Wang 
2810529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6 ||
2811529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2812529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10) {
2813529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
2814529cad0fSDing Wang 	} else {
2815529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
2816529cad0fSDing Wang 	}
2817529cad0fSDing Wang 
2818529cad0fSDing Wang 	//////////////////////
2819529cad0fSDing Wang 	//// temporal dither
2820529cad0fSDing Wang 	//////////////////////
2821529cad0fSDing Wang 	if (option == DITHER_OPTION_FM6           ||
2822529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6     ||
2823529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6     ||
2824529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2825529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6      ||
2826529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2827529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2828529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
2829529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM8        ||
2830529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8  ||
2831529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8) {
2832529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2833529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
2834529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM10) {
2835529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2836529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
2837529cad0fSDing Wang 	}
2838529cad0fSDing Wang 
2839529cad0fSDing Wang 	fmt_bit_depth->pixel_encoding = pixel_encoding;
2840529cad0fSDing Wang }
28419345d987SAndrey Grodzovsky 
284262c933f9SYongqiang Sun enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
28439345d987SAndrey Grodzovsky {
2844ceb3dbb4SJun Lei 	struct dc_link *link = stream->link;
28452b77dcc5SAnthony Koo 	struct timing_generator *tg = dc->res_pool->timing_generators[0];
28469345d987SAndrey Grodzovsky 	enum dc_status res = DC_OK;
28479345d987SAndrey Grodzovsky 
28484fa086b9SLeo (Sunpeng) Li 	calculate_phy_pix_clks(stream);
28499345d987SAndrey Grodzovsky 
28504fa086b9SLeo (Sunpeng) Li 	if (!tg->funcs->validate_timing(tg, &stream->timing))
28519345d987SAndrey Grodzovsky 		res = DC_FAIL_CONTROLLER_VALIDATE;
28529345d987SAndrey Grodzovsky 
2853248cbed6SEric Bernstein 	if (res == DC_OK) {
28549345d987SAndrey Grodzovsky 		if (!link->link_enc->funcs->validate_output_with_stream(
28554fa086b9SLeo (Sunpeng) Li 						link->link_enc, stream))
28569345d987SAndrey Grodzovsky 			res = DC_FAIL_ENC_VALIDATE;
2857248cbed6SEric Bernstein 	}
28589345d987SAndrey Grodzovsky 
28599345d987SAndrey Grodzovsky 	/* TODO: validate audio ASIC caps, encoder */
28609345d987SAndrey Grodzovsky 
28619345d987SAndrey Grodzovsky 	if (res == DC_OK)
28624fa086b9SLeo (Sunpeng) Li 		res = dc_link_validate_mode_timing(stream,
28639345d987SAndrey Grodzovsky 		      link,
28644fa086b9SLeo (Sunpeng) Li 		      &stream->timing);
28659345d987SAndrey Grodzovsky 
286662c933f9SYongqiang Sun 	return res;
28679345d987SAndrey Grodzovsky }
2868792671d7SAndrey Grodzovsky 
286962c933f9SYongqiang Sun enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
2870792671d7SAndrey Grodzovsky {
287162c933f9SYongqiang Sun 	enum dc_status res = DC_OK;
287262c933f9SYongqiang Sun 
2873792671d7SAndrey Grodzovsky 	/* TODO For now validates pixel format only */
28748e7095b9SDmytro Laktyushkin 	if (dc->res_pool->funcs->validate_plane)
287562c933f9SYongqiang Sun 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
2876792671d7SAndrey Grodzovsky 
287762c933f9SYongqiang Sun 	return res;
2878792671d7SAndrey Grodzovsky }
287974eac5f3SSu Sung Chung 
288074eac5f3SSu Sung Chung unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
288174eac5f3SSu Sung Chung {
288274eac5f3SSu Sung Chung 	switch (format) {
288374eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
288474eac5f3SSu Sung Chung 		return 8;
288574eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
288674eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
288774eac5f3SSu Sung Chung 		return 12;
288874eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
288974eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
289074eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
289174eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
289274eac5f3SSu Sung Chung 		return 16;
289374eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
289474eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
289574eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
289674eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
289774eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
289820f2ffe5SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN)
28995dba4991SBhawanpreet Lakha 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE:
29005dba4991SBhawanpreet Lakha 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA:
29015dba4991SBhawanpreet Lakha #endif
290274eac5f3SSu Sung Chung 		return 32;
290374eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
2904050cd3d6SMario Kleiner 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
290574eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
290674eac5f3SSu Sung Chung 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
290774eac5f3SSu Sung Chung 		return 64;
290874eac5f3SSu Sung Chung 	default:
290974eac5f3SSu Sung Chung 		ASSERT_CRITICAL(false);
291074eac5f3SSu Sung Chung 		return -1;
291174eac5f3SSu Sung Chung 	}
291274eac5f3SSu Sung Chung }
29133ab4cc65SCharlene Liu static unsigned int get_max_audio_sample_rate(struct audio_mode *modes)
29143ab4cc65SCharlene Liu {
29153ab4cc65SCharlene Liu 	if (modes) {
29163ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_192)
29173ab4cc65SCharlene Liu 			return 192000;
29183ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_176_4)
29193ab4cc65SCharlene Liu 			return 176400;
29203ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_96)
29213ab4cc65SCharlene Liu 			return 96000;
29223ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_88_2)
29233ab4cc65SCharlene Liu 			return 88200;
29243ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_48)
29253ab4cc65SCharlene Liu 			return 48000;
29263ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_44_1)
29273ab4cc65SCharlene Liu 			return 44100;
29283ab4cc65SCharlene Liu 		if (modes->sample_rates.rate.RATE_32)
29293ab4cc65SCharlene Liu 			return 32000;
29303ab4cc65SCharlene Liu 	}
29313ab4cc65SCharlene Liu 	/*original logic when no audio info*/
29323ab4cc65SCharlene Liu 	return 441000;
29333ab4cc65SCharlene Liu }
29343ab4cc65SCharlene Liu 
29353ab4cc65SCharlene Liu void get_audio_check(struct audio_info *aud_modes,
29363ab4cc65SCharlene Liu 	struct audio_check *audio_chk)
29373ab4cc65SCharlene Liu {
29383ab4cc65SCharlene Liu 	unsigned int i;
29393ab4cc65SCharlene Liu 	unsigned int max_sample_rate = 0;
29403ab4cc65SCharlene Liu 
29413ab4cc65SCharlene Liu 	if (aud_modes) {
29423ab4cc65SCharlene Liu 		audio_chk->audio_packet_type = 0x2;/*audio sample packet AP = .25 for layout0, 1 for layout1*/
29433ab4cc65SCharlene Liu 
29443ab4cc65SCharlene Liu 		audio_chk->max_audiosample_rate = 0;
29453ab4cc65SCharlene Liu 		for (i = 0; i < aud_modes->mode_count; i++) {
29463ab4cc65SCharlene Liu 			max_sample_rate = get_max_audio_sample_rate(&aud_modes->modes[i]);
29473ab4cc65SCharlene Liu 			if (audio_chk->max_audiosample_rate < max_sample_rate)
29483ab4cc65SCharlene Liu 				audio_chk->max_audiosample_rate = max_sample_rate;
29493ab4cc65SCharlene Liu 			/*dts takes the same as type 2: AP = 0.25*/
29503ab4cc65SCharlene Liu 		}
29513ab4cc65SCharlene Liu 		/*check which one take more bandwidth*/
29523ab4cc65SCharlene Liu 		if (audio_chk->max_audiosample_rate > 192000)
29533ab4cc65SCharlene Liu 			audio_chk->audio_packet_type = 0x9;/*AP =1*/
29543ab4cc65SCharlene Liu 		audio_chk->acat = 0;/*not support*/
29553ab4cc65SCharlene Liu 	}
29563ab4cc65SCharlene Liu }
29573ab4cc65SCharlene Liu 
2958