14562236bSHarry Wentland /*
24562236bSHarry Wentland * Copyright 2012-15 Advanced Micro Devices, Inc.
34562236bSHarry Wentland  *
44562236bSHarry Wentland  * Permission is hereby granted, free of charge, to any person obtaining a
54562236bSHarry Wentland  * copy of this software and associated documentation files (the "Software"),
64562236bSHarry Wentland  * to deal in the Software without restriction, including without limitation
74562236bSHarry Wentland  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84562236bSHarry Wentland  * and/or sell copies of the Software, and to permit persons to whom the
94562236bSHarry Wentland  * Software is furnished to do so, subject to the following conditions:
104562236bSHarry Wentland  *
114562236bSHarry Wentland  * The above copyright notice and this permission notice shall be included in
124562236bSHarry Wentland  * all copies or substantial portions of the Software.
134562236bSHarry Wentland  *
144562236bSHarry Wentland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154562236bSHarry Wentland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164562236bSHarry Wentland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174562236bSHarry Wentland  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184562236bSHarry Wentland  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194562236bSHarry Wentland  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204562236bSHarry Wentland  * OTHER DEALINGS IN THE SOFTWARE.
214562236bSHarry Wentland  *
224562236bSHarry Wentland  * Authors: AMD
234562236bSHarry Wentland  *
244562236bSHarry Wentland  */
254562236bSHarry Wentland #include "dm_services.h"
264562236bSHarry Wentland 
274562236bSHarry Wentland #include "resource.h"
284562236bSHarry Wentland #include "include/irq_service_interface.h"
294562236bSHarry Wentland #include "link_encoder.h"
304562236bSHarry Wentland #include "stream_encoder.h"
314562236bSHarry Wentland #include "opp.h"
324562236bSHarry Wentland #include "timing_generator.h"
334562236bSHarry Wentland #include "transform.h"
345ac3d3c9SCharlene Liu #include "core_types.h"
354562236bSHarry Wentland #include "set_mode_types.h"
364562236bSHarry Wentland #include "virtual/virtual_stream_encoder.h"
374562236bSHarry Wentland 
384562236bSHarry Wentland #include "dce80/dce80_resource.h"
394562236bSHarry Wentland #include "dce100/dce100_resource.h"
404562236bSHarry Wentland #include "dce110/dce110_resource.h"
414562236bSHarry Wentland #include "dce112/dce112_resource.h"
42ff5ef992SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
43ff5ef992SAlex Deucher #include "dcn10/dcn10_resource.h"
44ff5ef992SAlex Deucher #endif
452c8ad2d5SAlex Deucher #include "dce120/dce120_resource.h"
464562236bSHarry Wentland 
474562236bSHarry Wentland enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
484562236bSHarry Wentland {
494562236bSHarry Wentland 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
504562236bSHarry Wentland 	switch (asic_id.chip_family) {
514562236bSHarry Wentland 
524562236bSHarry Wentland 	case FAMILY_CI:
534562236bSHarry Wentland 	case FAMILY_KV:
544562236bSHarry Wentland 		dc_version = DCE_VERSION_8_0;
554562236bSHarry Wentland 		break;
564562236bSHarry Wentland 	case FAMILY_CZ:
574562236bSHarry Wentland 		dc_version = DCE_VERSION_11_0;
584562236bSHarry Wentland 		break;
594562236bSHarry Wentland 
604562236bSHarry Wentland 	case FAMILY_VI:
614562236bSHarry Wentland 		if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
624562236bSHarry Wentland 				ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
634562236bSHarry Wentland 			dc_version = DCE_VERSION_10_0;
644562236bSHarry Wentland 			break;
654562236bSHarry Wentland 		}
664562236bSHarry Wentland 		if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
67b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
68b264d345SJordan Lazare 				ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
694562236bSHarry Wentland 			dc_version = DCE_VERSION_11_2;
704562236bSHarry Wentland 		}
714562236bSHarry Wentland 		break;
722c8ad2d5SAlex Deucher 	case FAMILY_AI:
732c8ad2d5SAlex Deucher 		dc_version = DCE_VERSION_12_0;
742c8ad2d5SAlex Deucher 		break;
75ff5ef992SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
76ff5ef992SAlex Deucher 	case FAMILY_RV:
77ff5ef992SAlex Deucher 		dc_version = DCN_VERSION_1_0;
78ff5ef992SAlex Deucher 		break;
79ff5ef992SAlex Deucher #endif
804562236bSHarry Wentland 	default:
814562236bSHarry Wentland 		dc_version = DCE_VERSION_UNKNOWN;
824562236bSHarry Wentland 		break;
834562236bSHarry Wentland 	}
844562236bSHarry Wentland 	return dc_version;
854562236bSHarry Wentland }
864562236bSHarry Wentland 
874562236bSHarry Wentland struct resource_pool *dc_create_resource_pool(
884562236bSHarry Wentland 				struct core_dc *dc,
894562236bSHarry Wentland 				int num_virtual_links,
904562236bSHarry Wentland 				enum dce_version dc_version,
914562236bSHarry Wentland 				struct hw_asic_id asic_id)
924562236bSHarry Wentland {
935ac3d3c9SCharlene Liu 	struct resource_pool *res_pool = NULL;
944562236bSHarry Wentland 
954562236bSHarry Wentland 	switch (dc_version) {
964562236bSHarry Wentland 	case DCE_VERSION_8_0:
975ac3d3c9SCharlene Liu 		res_pool = dce80_create_resource_pool(
984562236bSHarry Wentland 			num_virtual_links, dc);
995ac3d3c9SCharlene Liu 		break;
1004562236bSHarry Wentland 	case DCE_VERSION_10_0:
1015ac3d3c9SCharlene Liu 		res_pool = dce100_create_resource_pool(
1024562236bSHarry Wentland 				num_virtual_links, dc);
1035ac3d3c9SCharlene Liu 		break;
1044562236bSHarry Wentland 	case DCE_VERSION_11_0:
1055ac3d3c9SCharlene Liu 		res_pool = dce110_create_resource_pool(
1064562236bSHarry Wentland 			num_virtual_links, dc, asic_id);
1075ac3d3c9SCharlene Liu 		break;
1084562236bSHarry Wentland 	case DCE_VERSION_11_2:
1095ac3d3c9SCharlene Liu 		res_pool = dce112_create_resource_pool(
1104562236bSHarry Wentland 			num_virtual_links, dc);
1115ac3d3c9SCharlene Liu 		break;
1122c8ad2d5SAlex Deucher 	case DCE_VERSION_12_0:
1132c8ad2d5SAlex Deucher 		res_pool = dce120_create_resource_pool(
1142c8ad2d5SAlex Deucher 			num_virtual_links, dc);
1152c8ad2d5SAlex Deucher 		break;
116ff5ef992SAlex Deucher 
117ff5ef992SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
118ff5ef992SAlex Deucher 	case DCN_VERSION_1_0:
119ff5ef992SAlex Deucher 		res_pool = dcn10_create_resource_pool(
120ff5ef992SAlex Deucher 			num_virtual_links, dc);
121ff5ef992SAlex Deucher 		break;
122ff5ef992SAlex Deucher #endif
1234562236bSHarry Wentland 	default:
1244562236bSHarry Wentland 		break;
1254562236bSHarry Wentland 	}
1265ac3d3c9SCharlene Liu 	if (res_pool != NULL) {
1275ac3d3c9SCharlene Liu 		struct firmware_info fw_info = { { 0 } };
1284562236bSHarry Wentland 
1295ac3d3c9SCharlene Liu 		if (dc->ctx->dc_bios->funcs->get_firmware_info(
1305ac3d3c9SCharlene Liu 				dc->ctx->dc_bios, &fw_info) == BP_RESULT_OK) {
1315ac3d3c9SCharlene Liu 				res_pool->ref_clock_inKhz = fw_info.pll_info.crystal_frequency;
1325ac3d3c9SCharlene Liu 			} else
1335ac3d3c9SCharlene Liu 				ASSERT_CRITICAL(false);
1345ac3d3c9SCharlene Liu 	}
1355ac3d3c9SCharlene Liu 
1365ac3d3c9SCharlene Liu 	return res_pool;
1374562236bSHarry Wentland }
1384562236bSHarry Wentland 
1394562236bSHarry Wentland void dc_destroy_resource_pool(struct core_dc *dc)
1404562236bSHarry Wentland {
1414562236bSHarry Wentland 	if (dc) {
1424562236bSHarry Wentland 		if (dc->res_pool)
1434562236bSHarry Wentland 			dc->res_pool->funcs->destroy(&dc->res_pool);
1444562236bSHarry Wentland 
1454562236bSHarry Wentland 		if (dc->hwseq)
1464562236bSHarry Wentland 			dm_free(dc->hwseq);
1474562236bSHarry Wentland 	}
1484562236bSHarry Wentland }
1494562236bSHarry Wentland 
1504562236bSHarry Wentland static void update_num_audio(
1514562236bSHarry Wentland 	const struct resource_straps *straps,
1524562236bSHarry Wentland 	unsigned int *num_audio,
1534562236bSHarry Wentland 	struct audio_support *aud_support)
1544562236bSHarry Wentland {
1554562236bSHarry Wentland 	if (straps->hdmi_disable == 0) {
1564562236bSHarry Wentland 		aud_support->hdmi_audio_native = true;
1574562236bSHarry Wentland 		aud_support->hdmi_audio_on_dongle = true;
1584562236bSHarry Wentland 		aud_support->dp_audio = true;
1594562236bSHarry Wentland 	} else {
1604562236bSHarry Wentland 		if (straps->dc_pinstraps_audio & 0x2) {
1614562236bSHarry Wentland 			aud_support->hdmi_audio_on_dongle = true;
1624562236bSHarry Wentland 			aud_support->dp_audio = true;
1634562236bSHarry Wentland 		} else {
1644562236bSHarry Wentland 			aud_support->dp_audio = true;
1654562236bSHarry Wentland 		}
1664562236bSHarry Wentland 	}
1674562236bSHarry Wentland 
1684562236bSHarry Wentland 	switch (straps->audio_stream_number) {
1694562236bSHarry Wentland 	case 0: /* multi streams supported */
1704562236bSHarry Wentland 		break;
1714562236bSHarry Wentland 	case 1: /* multi streams not supported */
1724562236bSHarry Wentland 		*num_audio = 1;
1734562236bSHarry Wentland 		break;
1744562236bSHarry Wentland 	default:
1754562236bSHarry Wentland 		DC_ERR("DC: unexpected audio fuse!\n");
17617a96033SJulia Lawall 	}
1774562236bSHarry Wentland }
1784562236bSHarry Wentland 
1794562236bSHarry Wentland bool resource_construct(
1804562236bSHarry Wentland 	unsigned int num_virtual_links,
1814562236bSHarry Wentland 	struct core_dc *dc,
1824562236bSHarry Wentland 	struct resource_pool *pool,
1834562236bSHarry Wentland 	const struct resource_create_funcs *create_funcs)
1844562236bSHarry Wentland {
1854562236bSHarry Wentland 	struct dc_context *ctx = dc->ctx;
1864562236bSHarry Wentland 	const struct resource_caps *caps = pool->res_cap;
1874562236bSHarry Wentland 	int i;
1884562236bSHarry Wentland 	unsigned int num_audio = caps->num_audio;
1894562236bSHarry Wentland 	struct resource_straps straps = {0};
1904562236bSHarry Wentland 
1914562236bSHarry Wentland 	if (create_funcs->read_dce_straps)
1924562236bSHarry Wentland 		create_funcs->read_dce_straps(dc->ctx, &straps);
1934562236bSHarry Wentland 
1944562236bSHarry Wentland 	pool->audio_count = 0;
1954562236bSHarry Wentland 	if (create_funcs->create_audio) {
1964562236bSHarry Wentland 		/* find the total number of streams available via the
1974562236bSHarry Wentland 		 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
1984562236bSHarry Wentland 		 * registers (one for each pin) starting from pin 1
1994562236bSHarry Wentland 		 * up to the max number of audio pins.
2004562236bSHarry Wentland 		 * We stop on the first pin where
2014562236bSHarry Wentland 		 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
2024562236bSHarry Wentland 		 */
2034562236bSHarry Wentland 		update_num_audio(&straps, &num_audio, &pool->audio_support);
2044562236bSHarry Wentland 		for (i = 0; i < pool->pipe_count && i < num_audio; i++) {
2054562236bSHarry Wentland 			struct audio *aud = create_funcs->create_audio(ctx, i);
2064562236bSHarry Wentland 
2074562236bSHarry Wentland 			if (aud == NULL) {
2084562236bSHarry Wentland 				DC_ERR("DC: failed to create audio!\n");
2094562236bSHarry Wentland 				return false;
2104562236bSHarry Wentland 			}
2114562236bSHarry Wentland 
2124562236bSHarry Wentland 			if (!aud->funcs->endpoint_valid(aud)) {
2134562236bSHarry Wentland 				aud->funcs->destroy(&aud);
2144562236bSHarry Wentland 				break;
2154562236bSHarry Wentland 			}
2164562236bSHarry Wentland 
2174562236bSHarry Wentland 			pool->audios[i] = aud;
2184562236bSHarry Wentland 			pool->audio_count++;
2194562236bSHarry Wentland 		}
2204562236bSHarry Wentland 	}
2214562236bSHarry Wentland 
2224562236bSHarry Wentland 	pool->stream_enc_count = 0;
2234562236bSHarry Wentland 	if (create_funcs->create_stream_encoder) {
2244562236bSHarry Wentland 		for (i = 0; i < caps->num_stream_encoder; i++) {
2254562236bSHarry Wentland 			pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
2264562236bSHarry Wentland 			if (pool->stream_enc[i] == NULL)
2274562236bSHarry Wentland 				DC_ERR("DC: failed to create stream_encoder!\n");
2284562236bSHarry Wentland 			pool->stream_enc_count++;
2294562236bSHarry Wentland 		}
2304562236bSHarry Wentland 	}
2314562236bSHarry Wentland 
2324562236bSHarry Wentland 	for (i = 0; i < num_virtual_links; i++) {
2334562236bSHarry Wentland 		pool->stream_enc[pool->stream_enc_count] =
2344562236bSHarry Wentland 			virtual_stream_encoder_create(
2354562236bSHarry Wentland 					ctx, ctx->dc_bios);
2364562236bSHarry Wentland 		if (pool->stream_enc[pool->stream_enc_count] == NULL) {
2374562236bSHarry Wentland 			DC_ERR("DC: failed to create stream_encoder!\n");
2384562236bSHarry Wentland 			return false;
2394562236bSHarry Wentland 		}
2404562236bSHarry Wentland 		pool->stream_enc_count++;
2414562236bSHarry Wentland 	}
2424562236bSHarry Wentland 
2434562236bSHarry Wentland 	dc->hwseq = create_funcs->create_hwseq(ctx);
2444562236bSHarry Wentland 
2454562236bSHarry Wentland 	return true;
2464562236bSHarry Wentland }
2474562236bSHarry Wentland 
2484562236bSHarry Wentland 
2494562236bSHarry Wentland void resource_unreference_clock_source(
2504562236bSHarry Wentland 		struct resource_context *res_ctx,
251a2b8659dSTony Cheng 		const struct resource_pool *pool,
2528c737fccSYongqiang Sun 		struct clock_source **clock_source)
2534562236bSHarry Wentland {
2544562236bSHarry Wentland 	int i;
255a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; i++) {
256a2b8659dSTony Cheng 		if (pool->clock_sources[i] != *clock_source)
2574562236bSHarry Wentland 			continue;
2584562236bSHarry Wentland 
2594562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]--;
2604562236bSHarry Wentland 
2614562236bSHarry Wentland 		if (res_ctx->clock_source_ref_count[i] == 0)
2628c737fccSYongqiang Sun 			(*clock_source)->funcs->cs_power_down(*clock_source);
2634562236bSHarry Wentland 
2644562236bSHarry Wentland 		break;
2654562236bSHarry Wentland 	}
2664562236bSHarry Wentland 
267a2b8659dSTony Cheng 	if (pool->dp_clock_source == *clock_source) {
2684562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count--;
2694562236bSHarry Wentland 
2704562236bSHarry Wentland 		if (res_ctx->dp_clock_source_ref_count == 0)
2718c737fccSYongqiang Sun 			(*clock_source)->funcs->cs_power_down(*clock_source);
2724562236bSHarry Wentland 	}
2738c737fccSYongqiang Sun 	*clock_source = NULL;
2744562236bSHarry Wentland }
2754562236bSHarry Wentland 
2764562236bSHarry Wentland void resource_reference_clock_source(
2774562236bSHarry Wentland 		struct resource_context *res_ctx,
278a2b8659dSTony Cheng 		const struct resource_pool *pool,
2794562236bSHarry Wentland 		struct clock_source *clock_source)
2804562236bSHarry Wentland {
2814562236bSHarry Wentland 	int i;
282a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; i++) {
283a2b8659dSTony Cheng 		if (pool->clock_sources[i] != clock_source)
2844562236bSHarry Wentland 			continue;
2854562236bSHarry Wentland 
2864562236bSHarry Wentland 		res_ctx->clock_source_ref_count[i]++;
2874562236bSHarry Wentland 		break;
2884562236bSHarry Wentland 	}
2894562236bSHarry Wentland 
290a2b8659dSTony Cheng 	if (pool->dp_clock_source == clock_source)
2914562236bSHarry Wentland 		res_ctx->dp_clock_source_ref_count++;
2924562236bSHarry Wentland }
2934562236bSHarry Wentland 
2944562236bSHarry Wentland bool resource_are_streams_timing_synchronizable(
2954562236bSHarry Wentland 	const struct core_stream *stream1,
2964562236bSHarry Wentland 	const struct core_stream *stream2)
2974562236bSHarry Wentland {
2984562236bSHarry Wentland 	if (stream1->public.timing.h_total != stream2->public.timing.h_total)
2994562236bSHarry Wentland 		return false;
3004562236bSHarry Wentland 
3014562236bSHarry Wentland 	if (stream1->public.timing.v_total != stream2->public.timing.v_total)
3024562236bSHarry Wentland 		return false;
3034562236bSHarry Wentland 
3044562236bSHarry Wentland 	if (stream1->public.timing.h_addressable
3054562236bSHarry Wentland 				!= stream2->public.timing.h_addressable)
3064562236bSHarry Wentland 		return false;
3074562236bSHarry Wentland 
3084562236bSHarry Wentland 	if (stream1->public.timing.v_addressable
3094562236bSHarry Wentland 				!= stream2->public.timing.v_addressable)
3104562236bSHarry Wentland 		return false;
3114562236bSHarry Wentland 
3124562236bSHarry Wentland 	if (stream1->public.timing.pix_clk_khz
3134562236bSHarry Wentland 				!= stream2->public.timing.pix_clk_khz)
3144562236bSHarry Wentland 		return false;
3154562236bSHarry Wentland 
3164562236bSHarry Wentland 	if (stream1->phy_pix_clk != stream2->phy_pix_clk
3177e2fe319SCharlene Liu 			&& (!dc_is_dp_signal(stream1->signal)
3187e2fe319SCharlene Liu 			|| !dc_is_dp_signal(stream2->signal)))
3194562236bSHarry Wentland 		return false;
3204562236bSHarry Wentland 
3214562236bSHarry Wentland 	return true;
3224562236bSHarry Wentland }
3234562236bSHarry Wentland 
3244562236bSHarry Wentland static bool is_sharable_clk_src(
3254562236bSHarry Wentland 	const struct pipe_ctx *pipe_with_clk_src,
3264562236bSHarry Wentland 	const struct pipe_ctx *pipe)
3274562236bSHarry Wentland {
3284562236bSHarry Wentland 	if (pipe_with_clk_src->clock_source == NULL)
3294562236bSHarry Wentland 		return false;
3304562236bSHarry Wentland 
3314562236bSHarry Wentland 	if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
3324562236bSHarry Wentland 		return false;
3334562236bSHarry Wentland 
3344562236bSHarry Wentland 	if (dc_is_dp_signal(pipe_with_clk_src->stream->signal))
3354562236bSHarry Wentland 		return false;
3364562236bSHarry Wentland 
3374562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
3384562236bSHarry Wentland 			&& dc_is_dvi_signal(pipe->stream->signal))
3394562236bSHarry Wentland 		return false;
3404562236bSHarry Wentland 
3414562236bSHarry Wentland 	if (dc_is_hdmi_signal(pipe->stream->signal)
3424562236bSHarry Wentland 			&& dc_is_dvi_signal(pipe_with_clk_src->stream->signal))
3434562236bSHarry Wentland 		return false;
3444562236bSHarry Wentland 
3454562236bSHarry Wentland 	if (!resource_are_streams_timing_synchronizable(
3464562236bSHarry Wentland 			pipe_with_clk_src->stream, pipe->stream))
3474562236bSHarry Wentland 		return false;
3484562236bSHarry Wentland 
3494562236bSHarry Wentland 	return true;
3504562236bSHarry Wentland }
3514562236bSHarry Wentland 
3524562236bSHarry Wentland struct clock_source *resource_find_used_clk_src_for_sharing(
3534562236bSHarry Wentland 					struct resource_context *res_ctx,
3544562236bSHarry Wentland 					struct pipe_ctx *pipe_ctx)
3554562236bSHarry Wentland {
3564562236bSHarry Wentland 	int i;
3574562236bSHarry Wentland 
3584562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
3594562236bSHarry Wentland 		if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
3604562236bSHarry Wentland 			return res_ctx->pipe_ctx[i].clock_source;
3614562236bSHarry Wentland 	}
3624562236bSHarry Wentland 
3634562236bSHarry Wentland 	return NULL;
3644562236bSHarry Wentland }
3654562236bSHarry Wentland 
3664562236bSHarry Wentland static enum pixel_format convert_pixel_format_to_dalsurface(
3674562236bSHarry Wentland 		enum surface_pixel_format surface_pixel_format)
3684562236bSHarry Wentland {
3694562236bSHarry Wentland 	enum pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
3704562236bSHarry Wentland 
3714562236bSHarry Wentland 	switch (surface_pixel_format) {
3724562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
3734562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_INDEX8;
3744562236bSHarry Wentland 		break;
3754562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
3764562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
3774562236bSHarry Wentland 		break;
3784562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
3794562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_RGB565;
3804562236bSHarry Wentland 		break;
3814562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
3824562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
3834562236bSHarry Wentland 		break;
3848693049aSTony Cheng 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
3854562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
3864562236bSHarry Wentland 		break;
3874562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
3884562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
3894562236bSHarry Wentland 		break;
3904562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
3914562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
3924562236bSHarry Wentland 		break;
3934562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
3944562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
3954562236bSHarry Wentland 		break;
3964562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
3974562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
3984562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_FP16;
3994562236bSHarry Wentland 		break;
4004562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
4014562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
4024562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_420BPP12;
4034562236bSHarry Wentland 		break;
404ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
405ffbcd19aSVitaly Prosyak 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
406ffbcd19aSVitaly Prosyak 		dal_pixel_format = PIXEL_FORMAT_420BPP15;
407ffbcd19aSVitaly Prosyak 		break;
4084562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
4094562236bSHarry Wentland 	default:
4104562236bSHarry Wentland 		dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
4114562236bSHarry Wentland 		break;
4124562236bSHarry Wentland 	}
4134562236bSHarry Wentland 	return dal_pixel_format;
4144562236bSHarry Wentland }
4154562236bSHarry Wentland 
4164562236bSHarry Wentland static void rect_swap_helper(struct rect *rect)
4174562236bSHarry Wentland {
4184562236bSHarry Wentland 	uint32_t temp = 0;
4194562236bSHarry Wentland 
4204562236bSHarry Wentland 	temp = rect->height;
4214562236bSHarry Wentland 	rect->height = rect->width;
4224562236bSHarry Wentland 	rect->width = temp;
4234562236bSHarry Wentland 
4244562236bSHarry Wentland 	temp = rect->x;
4254562236bSHarry Wentland 	rect->x = rect->y;
4264562236bSHarry Wentland 	rect->y = temp;
4274562236bSHarry Wentland }
4284562236bSHarry Wentland 
429b2d0a103SDmytro Laktyushkin static void calculate_viewport(struct pipe_ctx *pipe_ctx)
4304562236bSHarry Wentland {
431b2d0a103SDmytro Laktyushkin 	const struct dc_surface *surface = &pipe_ctx->surface->public;
4321fbd2cfcSDmytro Laktyushkin 	const struct dc_stream *stream = &pipe_ctx->stream->public;
433b2d0a103SDmytro Laktyushkin 	struct scaler_data *data = &pipe_ctx->scl_data;
43486006a7fSDmytro Laktyushkin 	struct rect surf_src = surface->src_rect;
4354562236bSHarry Wentland 	struct rect clip = { 0 };
436b2d0a103SDmytro Laktyushkin 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP12
437b2d0a103SDmytro Laktyushkin 			|| data->format == PIXEL_FORMAT_420BPP15) ? 2 : 1;
4381fbd2cfcSDmytro Laktyushkin 	bool pri_split = pipe_ctx->bottom_pipe &&
4391fbd2cfcSDmytro Laktyushkin 			pipe_ctx->bottom_pipe->surface == pipe_ctx->surface;
4401fbd2cfcSDmytro Laktyushkin 	bool sec_split = pipe_ctx->top_pipe &&
4411fbd2cfcSDmytro Laktyushkin 			pipe_ctx->top_pipe->surface == pipe_ctx->surface;
4424562236bSHarry Wentland 
4437b779c99SVitaly Prosyak 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_SIDE_BY_SIDE ||
4447b779c99SVitaly Prosyak 		stream->timing.timing_3d_format == TIMING_3D_FORMAT_TOP_AND_BOTTOM) {
4457b779c99SVitaly Prosyak 		pri_split = false;
4467b779c99SVitaly Prosyak 		sec_split = false;
4477b779c99SVitaly Prosyak 	}
44886006a7fSDmytro Laktyushkin 
44986006a7fSDmytro Laktyushkin 	if (pipe_ctx->surface->public.rotation == ROTATION_ANGLE_90 ||
45086006a7fSDmytro Laktyushkin 			pipe_ctx->surface->public.rotation == ROTATION_ANGLE_270)
45186006a7fSDmytro Laktyushkin 		rect_swap_helper(&surf_src);
45286006a7fSDmytro Laktyushkin 
4534562236bSHarry Wentland 	/* The actual clip is an intersection between stream
4544562236bSHarry Wentland 	 * source and surface clip
4554562236bSHarry Wentland 	 */
4561fbd2cfcSDmytro Laktyushkin 	clip.x = stream->src.x > surface->clip_rect.x ?
4571fbd2cfcSDmytro Laktyushkin 			stream->src.x : surface->clip_rect.x;
4584562236bSHarry Wentland 
4591fbd2cfcSDmytro Laktyushkin 	clip.width = stream->src.x + stream->src.width <
4601fbd2cfcSDmytro Laktyushkin 			surface->clip_rect.x + surface->clip_rect.width ?
4611fbd2cfcSDmytro Laktyushkin 			stream->src.x + stream->src.width - clip.x :
4621fbd2cfcSDmytro Laktyushkin 			surface->clip_rect.x + surface->clip_rect.width - clip.x ;
4634562236bSHarry Wentland 
4641fbd2cfcSDmytro Laktyushkin 	clip.y = stream->src.y > surface->clip_rect.y ?
4651fbd2cfcSDmytro Laktyushkin 			stream->src.y : surface->clip_rect.y;
4664562236bSHarry Wentland 
4671fbd2cfcSDmytro Laktyushkin 	clip.height = stream->src.y + stream->src.height <
4681fbd2cfcSDmytro Laktyushkin 			surface->clip_rect.y + surface->clip_rect.height ?
4691fbd2cfcSDmytro Laktyushkin 			stream->src.y + stream->src.height - clip.y :
4701fbd2cfcSDmytro Laktyushkin 			surface->clip_rect.y + surface->clip_rect.height - clip.y ;
4714562236bSHarry Wentland 
47286006a7fSDmytro Laktyushkin 	/* offset = surf_src.ofs + (clip.ofs - surface->dst_rect.ofs) * scl_ratio
4734562236bSHarry Wentland 	 * num_pixels = clip.num_pix * scl_ratio
4744562236bSHarry Wentland 	 */
47586006a7fSDmytro Laktyushkin 	data->viewport.x = surf_src.x + (clip.x - surface->dst_rect.x) *
47686006a7fSDmytro Laktyushkin 			surf_src.width / surface->dst_rect.width;
477b2d0a103SDmytro Laktyushkin 	data->viewport.width = clip.width *
47886006a7fSDmytro Laktyushkin 			surf_src.width / surface->dst_rect.width;
4794562236bSHarry Wentland 
48086006a7fSDmytro Laktyushkin 	data->viewport.y = surf_src.y + (clip.y - surface->dst_rect.y) *
48186006a7fSDmytro Laktyushkin 			surf_src.height / surface->dst_rect.height;
482b2d0a103SDmytro Laktyushkin 	data->viewport.height = clip.height *
48386006a7fSDmytro Laktyushkin 			surf_src.height / surface->dst_rect.height;
4844562236bSHarry Wentland 
485b2d0a103SDmytro Laktyushkin 	/* Round down, compensate in init */
486b2d0a103SDmytro Laktyushkin 	data->viewport_c.x = data->viewport.x / vpc_div;
487b2d0a103SDmytro Laktyushkin 	data->viewport_c.y = data->viewport.y / vpc_div;
488b2d0a103SDmytro Laktyushkin 	data->inits.h_c = (data->viewport.x % vpc_div) != 0 ?
489b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_half : dal_fixed31_32_zero;
490b2d0a103SDmytro Laktyushkin 	data->inits.v_c = (data->viewport.y % vpc_div) != 0 ?
491b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_half : dal_fixed31_32_zero;
492b2d0a103SDmytro Laktyushkin 	/* Round up, assume original video size always even dimensions */
493b2d0a103SDmytro Laktyushkin 	data->viewport_c.width = (data->viewport.width + vpc_div - 1) / vpc_div;
494b2d0a103SDmytro Laktyushkin 	data->viewport_c.height = (data->viewport.height + vpc_div - 1) / vpc_div;
495b2d0a103SDmytro Laktyushkin 
496b2d0a103SDmytro Laktyushkin 	/* Handle hsplit */
4971fbd2cfcSDmytro Laktyushkin 	if (pri_split || sec_split) {
4981fbd2cfcSDmytro Laktyushkin 		/* HMirror XOR Secondary_pipe XOR Rotation_180 */
4991fbd2cfcSDmytro Laktyushkin 		bool right_view = (sec_split != surface->horizontal_mirror) !=
5001fbd2cfcSDmytro Laktyushkin 					(surface->rotation == ROTATION_ANGLE_180);
5019e6c74ceSDmytro Laktyushkin 
5021fbd2cfcSDmytro Laktyushkin 		if (surface->rotation == ROTATION_ANGLE_90
5031fbd2cfcSDmytro Laktyushkin 				|| surface->rotation == ROTATION_ANGLE_270)
5041fbd2cfcSDmytro Laktyushkin 			/* Secondary_pipe XOR Rotation_270 */
5051fbd2cfcSDmytro Laktyushkin 			right_view = (surface->rotation == ROTATION_ANGLE_270) != sec_split;
5069e6c74ceSDmytro Laktyushkin 
5079e6c74ceSDmytro Laktyushkin 		if (right_view) {
508b2d0a103SDmytro Laktyushkin 			data->viewport.width /= 2;
509b2d0a103SDmytro Laktyushkin 			data->viewport_c.width /= 2;
510b2d0a103SDmytro Laktyushkin 			data->viewport.x +=  data->viewport.width;
511b2d0a103SDmytro Laktyushkin 			data->viewport_c.x +=  data->viewport_c.width;
5129e6c74ceSDmytro Laktyushkin 			/* Ceil offset pipe */
513b2d0a103SDmytro Laktyushkin 			data->viewport.width += data->viewport.width % 2;
514b2d0a103SDmytro Laktyushkin 			data->viewport_c.width += data->viewport_c.width % 2;
5159e6c74ceSDmytro Laktyushkin 		} else {
516b2d0a103SDmytro Laktyushkin 			data->viewport.width /= 2;
517b2d0a103SDmytro Laktyushkin 			data->viewport_c.width /= 2;
518b2d0a103SDmytro Laktyushkin 		}
5194562236bSHarry Wentland 	}
5201fbd2cfcSDmytro Laktyushkin 
5211fbd2cfcSDmytro Laktyushkin 	if (surface->rotation == ROTATION_ANGLE_90 ||
5221fbd2cfcSDmytro Laktyushkin 			surface->rotation == ROTATION_ANGLE_270) {
5231fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport_c);
5241fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport);
5251fbd2cfcSDmytro Laktyushkin 	}
5269e6c74ceSDmytro Laktyushkin }
5274562236bSHarry Wentland 
528b2d0a103SDmytro Laktyushkin static void calculate_recout(struct pipe_ctx *pipe_ctx, struct view *recout_skip)
5294562236bSHarry Wentland {
530b2d0a103SDmytro Laktyushkin 	const struct dc_surface *surface = &pipe_ctx->surface->public;
5314562236bSHarry Wentland 	struct core_stream *stream = pipe_ctx->stream;
53286006a7fSDmytro Laktyushkin 	struct rect surf_src = surface->src_rect;
53386006a7fSDmytro Laktyushkin 	struct rect surf_clip = surface->clip_rect;
534c802570eSDmytro Laktyushkin 	int recout_full_x, recout_full_y;
5354562236bSHarry Wentland 
53686006a7fSDmytro Laktyushkin 	if (pipe_ctx->surface->public.rotation == ROTATION_ANGLE_90 ||
53786006a7fSDmytro Laktyushkin 			pipe_ctx->surface->public.rotation == ROTATION_ANGLE_270)
53886006a7fSDmytro Laktyushkin 		rect_swap_helper(&surf_src);
53986006a7fSDmytro Laktyushkin 
5404562236bSHarry Wentland 	pipe_ctx->scl_data.recout.x = stream->public.dst.x;
54186006a7fSDmytro Laktyushkin 	if (stream->public.src.x < surf_clip.x)
54286006a7fSDmytro Laktyushkin 		pipe_ctx->scl_data.recout.x += (surf_clip.x
5434562236bSHarry Wentland 			- stream->public.src.x) * stream->public.dst.width
5444562236bSHarry Wentland 						/ stream->public.src.width;
5454562236bSHarry Wentland 
54686006a7fSDmytro Laktyushkin 	pipe_ctx->scl_data.recout.width = surf_clip.width *
5474562236bSHarry Wentland 			stream->public.dst.width / stream->public.src.width;
5484562236bSHarry Wentland 	if (pipe_ctx->scl_data.recout.width + pipe_ctx->scl_data.recout.x >
5494562236bSHarry Wentland 			stream->public.dst.x + stream->public.dst.width)
5504562236bSHarry Wentland 		pipe_ctx->scl_data.recout.width =
5514562236bSHarry Wentland 			stream->public.dst.x + stream->public.dst.width
5524562236bSHarry Wentland 						- pipe_ctx->scl_data.recout.x;
5534562236bSHarry Wentland 
5544562236bSHarry Wentland 	pipe_ctx->scl_data.recout.y = stream->public.dst.y;
55586006a7fSDmytro Laktyushkin 	if (stream->public.src.y < surf_clip.y)
55686006a7fSDmytro Laktyushkin 		pipe_ctx->scl_data.recout.y += (surf_clip.y
5574562236bSHarry Wentland 			- stream->public.src.y) * stream->public.dst.height
5584562236bSHarry Wentland 						/ stream->public.src.height;
5594562236bSHarry Wentland 
56086006a7fSDmytro Laktyushkin 	pipe_ctx->scl_data.recout.height = surf_clip.height *
5614562236bSHarry Wentland 			stream->public.dst.height / stream->public.src.height;
5624562236bSHarry Wentland 	if (pipe_ctx->scl_data.recout.height + pipe_ctx->scl_data.recout.y >
5634562236bSHarry Wentland 			stream->public.dst.y + stream->public.dst.height)
5644562236bSHarry Wentland 		pipe_ctx->scl_data.recout.height =
5654562236bSHarry Wentland 			stream->public.dst.y + stream->public.dst.height
5664562236bSHarry Wentland 						- pipe_ctx->scl_data.recout.y;
567b2d0a103SDmytro Laktyushkin 
5687b779c99SVitaly Prosyak 	/* Handle h & vsplit */
5697b779c99SVitaly Prosyak 	if (pipe_ctx->top_pipe && pipe_ctx->top_pipe->surface ==
5707b779c99SVitaly Prosyak 		pipe_ctx->surface) {
5717b779c99SVitaly Prosyak 		if (stream->public.timing.timing_3d_format ==
5727b779c99SVitaly Prosyak 			TIMING_3D_FORMAT_TOP_AND_BOTTOM) {
5737b779c99SVitaly Prosyak 			pipe_ctx->scl_data.recout.height /= 2;
5747b779c99SVitaly Prosyak 			pipe_ctx->scl_data.recout.y += pipe_ctx->scl_data.recout.height;
5757b779c99SVitaly Prosyak 			/* Floor primary pipe, ceil 2ndary pipe */
5767b779c99SVitaly Prosyak 			pipe_ctx->scl_data.recout.height += pipe_ctx->scl_data.recout.height % 2;
5777b779c99SVitaly Prosyak 		} else {
578b2d0a103SDmytro Laktyushkin 			pipe_ctx->scl_data.recout.width /= 2;
579b2d0a103SDmytro Laktyushkin 			pipe_ctx->scl_data.recout.x += pipe_ctx->scl_data.recout.width;
580b2d0a103SDmytro Laktyushkin 			pipe_ctx->scl_data.recout.width += pipe_ctx->scl_data.recout.width % 2;
5817b779c99SVitaly Prosyak 		}
5827b779c99SVitaly Prosyak 	} else if (pipe_ctx->bottom_pipe &&
5837b779c99SVitaly Prosyak 			pipe_ctx->bottom_pipe->surface == pipe_ctx->surface) {
5847b779c99SVitaly Prosyak 		if (stream->public.timing.timing_3d_format ==
5857b779c99SVitaly Prosyak 			TIMING_3D_FORMAT_TOP_AND_BOTTOM)
5867b779c99SVitaly Prosyak 			pipe_ctx->scl_data.recout.height /= 2;
5877b779c99SVitaly Prosyak 		else
588b2d0a103SDmytro Laktyushkin 			pipe_ctx->scl_data.recout.width /= 2;
5894562236bSHarry Wentland 	}
5904562236bSHarry Wentland 
59186006a7fSDmytro Laktyushkin 	/* Unclipped recout offset = stream dst offset + ((surf dst offset - stream surf_src offset)
59286006a7fSDmytro Laktyushkin 	 * 				* 1/ stream scaling ratio) - (surf surf_src offset * 1/ full scl
593c802570eSDmytro Laktyushkin 	 * 				ratio)
594c802570eSDmytro Laktyushkin 	 */
595c802570eSDmytro Laktyushkin 	recout_full_x = stream->public.dst.x + (surface->dst_rect.x -  stream->public.src.x)
596c802570eSDmytro Laktyushkin 					* stream->public.dst.width / stream->public.src.width -
59786006a7fSDmytro Laktyushkin 			surf_src.x * surface->dst_rect.width / surf_src.width
598c802570eSDmytro Laktyushkin 					* stream->public.dst.width / stream->public.src.width;
599c802570eSDmytro Laktyushkin 	recout_full_y = stream->public.dst.y + (surface->dst_rect.y -  stream->public.src.y)
600c802570eSDmytro Laktyushkin 					* stream->public.dst.height / stream->public.src.height -
60186006a7fSDmytro Laktyushkin 			surf_src.y * surface->dst_rect.height / surf_src.height
602c802570eSDmytro Laktyushkin 					* stream->public.dst.height / stream->public.src.height;
603c802570eSDmytro Laktyushkin 
604c802570eSDmytro Laktyushkin 	recout_skip->width = pipe_ctx->scl_data.recout.x - recout_full_x;
605c802570eSDmytro Laktyushkin 	recout_skip->height = pipe_ctx->scl_data.recout.y - recout_full_y;
606b2d0a103SDmytro Laktyushkin }
607b2d0a103SDmytro Laktyushkin 
608b2d0a103SDmytro Laktyushkin static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
6094562236bSHarry Wentland {
610b2d0a103SDmytro Laktyushkin 	const struct dc_surface *surface = &pipe_ctx->surface->public;
6114562236bSHarry Wentland 	struct core_stream *stream = pipe_ctx->stream;
61286006a7fSDmytro Laktyushkin 	struct rect surf_src = surface->src_rect;
6131fbd2cfcSDmytro Laktyushkin 	const int in_w = stream->public.src.width;
6141fbd2cfcSDmytro Laktyushkin 	const int in_h = stream->public.src.height;
6151fbd2cfcSDmytro Laktyushkin 	const int out_w = stream->public.dst.width;
6161fbd2cfcSDmytro Laktyushkin 	const int out_h = stream->public.dst.height;
6174562236bSHarry Wentland 
61886006a7fSDmytro Laktyushkin 	if (pipe_ctx->surface->public.rotation == ROTATION_ANGLE_90 ||
61986006a7fSDmytro Laktyushkin 			pipe_ctx->surface->public.rotation == ROTATION_ANGLE_270)
62086006a7fSDmytro Laktyushkin 		rect_swap_helper(&surf_src);
62186006a7fSDmytro Laktyushkin 
6224562236bSHarry Wentland 	pipe_ctx->scl_data.ratios.horz = dal_fixed31_32_from_fraction(
62386006a7fSDmytro Laktyushkin 					surf_src.width,
6244562236bSHarry Wentland 					surface->dst_rect.width);
6254562236bSHarry Wentland 	pipe_ctx->scl_data.ratios.vert = dal_fixed31_32_from_fraction(
62686006a7fSDmytro Laktyushkin 					surf_src.height,
6274562236bSHarry Wentland 					surface->dst_rect.height);
6284562236bSHarry Wentland 
6294562236bSHarry Wentland 	if (surface->stereo_format == PLANE_STEREO_FORMAT_SIDE_BY_SIDE)
6304562236bSHarry Wentland 		pipe_ctx->scl_data.ratios.horz.value *= 2;
6314562236bSHarry Wentland 	else if (surface->stereo_format == PLANE_STEREO_FORMAT_TOP_AND_BOTTOM)
6324562236bSHarry Wentland 		pipe_ctx->scl_data.ratios.vert.value *= 2;
6334562236bSHarry Wentland 
6344562236bSHarry Wentland 	pipe_ctx->scl_data.ratios.vert.value = div64_s64(
6354562236bSHarry Wentland 		pipe_ctx->scl_data.ratios.vert.value * in_h, out_h);
6364562236bSHarry Wentland 	pipe_ctx->scl_data.ratios.horz.value = div64_s64(
6374562236bSHarry Wentland 		pipe_ctx->scl_data.ratios.horz.value * in_w, out_w);
6384562236bSHarry Wentland 
6394562236bSHarry Wentland 	pipe_ctx->scl_data.ratios.horz_c = pipe_ctx->scl_data.ratios.horz;
6404562236bSHarry Wentland 	pipe_ctx->scl_data.ratios.vert_c = pipe_ctx->scl_data.ratios.vert;
6414562236bSHarry Wentland 
642b2d0a103SDmytro Laktyushkin 	if (pipe_ctx->scl_data.format == PIXEL_FORMAT_420BPP12
643b2d0a103SDmytro Laktyushkin 			|| pipe_ctx->scl_data.format == PIXEL_FORMAT_420BPP15) {
6444562236bSHarry Wentland 		pipe_ctx->scl_data.ratios.horz_c.value /= 2;
6454562236bSHarry Wentland 		pipe_ctx->scl_data.ratios.vert_c.value /= 2;
6464562236bSHarry Wentland 	}
6474562236bSHarry Wentland }
6484562236bSHarry Wentland 
649b2d0a103SDmytro Laktyushkin static void calculate_inits_and_adj_vp(struct pipe_ctx *pipe_ctx, struct view *recout_skip)
6504562236bSHarry Wentland {
651b2d0a103SDmytro Laktyushkin 	struct scaler_data *data = &pipe_ctx->scl_data;
652b2d0a103SDmytro Laktyushkin 	struct rect src = pipe_ctx->surface->public.src_rect;
653b2d0a103SDmytro Laktyushkin 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP12
654b2d0a103SDmytro Laktyushkin 			|| data->format == PIXEL_FORMAT_420BPP15) ? 2 : 1;
655b2d0a103SDmytro Laktyushkin 
65686006a7fSDmytro Laktyushkin 
6571fbd2cfcSDmytro Laktyushkin 	if (pipe_ctx->surface->public.rotation == ROTATION_ANGLE_90 ||
6581fbd2cfcSDmytro Laktyushkin 			pipe_ctx->surface->public.rotation == ROTATION_ANGLE_270) {
65986006a7fSDmytro Laktyushkin 		rect_swap_helper(&src);
6601fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport_c);
6611fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport);
6621fbd2cfcSDmytro Laktyushkin 	}
6631fbd2cfcSDmytro Laktyushkin 
664b2d0a103SDmytro Laktyushkin 	/*
665b2d0a103SDmytro Laktyushkin 	 * Init calculated according to formula:
666b2d0a103SDmytro Laktyushkin 	 * 	init = (scaling_ratio + number_of_taps + 1) / 2
667b2d0a103SDmytro Laktyushkin 	 * 	init_bot = init + scaling_ratio
668b2d0a103SDmytro Laktyushkin 	 * 	init_c = init + truncated_vp_c_offset(from calculate viewport)
669b2d0a103SDmytro Laktyushkin 	 */
670b2d0a103SDmytro Laktyushkin 	data->inits.h = dal_fixed31_32_div_int(
671b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.horz, data->taps.h_taps + 1), 2);
672b2d0a103SDmytro Laktyushkin 
673b2d0a103SDmytro Laktyushkin 	data->inits.h_c = dal_fixed31_32_add(data->inits.h_c, dal_fixed31_32_div_int(
674b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.horz_c, data->taps.h_taps_c + 1), 2));
675b2d0a103SDmytro Laktyushkin 
676b2d0a103SDmytro Laktyushkin 	data->inits.v = dal_fixed31_32_div_int(
677b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.vert, data->taps.v_taps + 1), 2);
678b2d0a103SDmytro Laktyushkin 
679b2d0a103SDmytro Laktyushkin 	data->inits.v_c = dal_fixed31_32_add(data->inits.v_c, dal_fixed31_32_div_int(
680b2d0a103SDmytro Laktyushkin 			dal_fixed31_32_add_int(data->ratios.vert_c, data->taps.v_taps_c + 1), 2));
681b2d0a103SDmytro Laktyushkin 
682b2d0a103SDmytro Laktyushkin 
683b2d0a103SDmytro Laktyushkin 	/* Adjust for viewport end clip-off */
684b2d0a103SDmytro Laktyushkin 	if ((data->viewport.x + data->viewport.width) < (src.x + src.width)) {
685b2d0a103SDmytro Laktyushkin 		int vp_clip = src.x + src.width - data->viewport.width - data->viewport.x;
6861fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
6871fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.h, data->ratios.horz));
688b2d0a103SDmytro Laktyushkin 
6891fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
690b2d0a103SDmytro Laktyushkin 		data->viewport.width += int_part < vp_clip ? int_part : vp_clip;
691b2d0a103SDmytro Laktyushkin 	}
692b2d0a103SDmytro Laktyushkin 	if ((data->viewport.y + data->viewport.height) < (src.y + src.height)) {
693b2d0a103SDmytro Laktyushkin 		int vp_clip = src.y + src.height - data->viewport.height - data->viewport.y;
6941fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
6951fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.v, data->ratios.vert));
696b2d0a103SDmytro Laktyushkin 
6971fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
698b2d0a103SDmytro Laktyushkin 		data->viewport.height += int_part < vp_clip ? int_part : vp_clip;
699b2d0a103SDmytro Laktyushkin 	}
700b2d0a103SDmytro Laktyushkin 	if ((data->viewport_c.x + data->viewport_c.width) < (src.x + src.width) / vpc_div) {
701b2d0a103SDmytro Laktyushkin 		int vp_clip = (src.x + src.width) / vpc_div -
702b2d0a103SDmytro Laktyushkin 				data->viewport_c.width - data->viewport_c.x;
7031fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
7041fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.h_c, data->ratios.horz_c));
705b2d0a103SDmytro Laktyushkin 
7061fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
707b2d0a103SDmytro Laktyushkin 		data->viewport_c.width += int_part < vp_clip ? int_part : vp_clip;
708b2d0a103SDmytro Laktyushkin 	}
709b2d0a103SDmytro Laktyushkin 	if ((data->viewport_c.y + data->viewport_c.height) < (src.y + src.height) / vpc_div) {
710b2d0a103SDmytro Laktyushkin 		int vp_clip = (src.y + src.height) / vpc_div -
711b2d0a103SDmytro Laktyushkin 				data->viewport_c.height - data->viewport_c.y;
7121fbd2cfcSDmytro Laktyushkin 		int int_part = dal_fixed31_32_floor(
7131fbd2cfcSDmytro Laktyushkin 				dal_fixed31_32_sub(data->inits.v_c, data->ratios.vert_c));
714b2d0a103SDmytro Laktyushkin 
7151fbd2cfcSDmytro Laktyushkin 		int_part = int_part > 0 ? int_part : 0;
716b2d0a103SDmytro Laktyushkin 		data->viewport_c.height += int_part < vp_clip ? int_part : vp_clip;
717b2d0a103SDmytro Laktyushkin 	}
718b2d0a103SDmytro Laktyushkin 
719b2d0a103SDmytro Laktyushkin 	/* Adjust for non-0 viewport offset */
720b2d0a103SDmytro Laktyushkin 	if (data->viewport.x) {
721b2d0a103SDmytro Laktyushkin 		int int_part;
722b2d0a103SDmytro Laktyushkin 
723b2d0a103SDmytro Laktyushkin 		data->inits.h = dal_fixed31_32_add(data->inits.h, dal_fixed31_32_mul_int(
724b2d0a103SDmytro Laktyushkin 				data->ratios.horz, recout_skip->width));
725b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.h) - data->viewport.x;
726b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.h_taps) {
727b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport.x >= (data->taps.h_taps - int_part) ?
728b2d0a103SDmytro Laktyushkin 						(data->taps.h_taps - int_part) : data->viewport.x;
729b2d0a103SDmytro Laktyushkin 			data->viewport.x -= int_adj;
730b2d0a103SDmytro Laktyushkin 			data->viewport.width += int_adj;
731b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
732b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.h_taps) {
733b2d0a103SDmytro Laktyushkin 			data->viewport.x += int_part - data->taps.h_taps;
734b2d0a103SDmytro Laktyushkin 			data->viewport.width -= int_part - data->taps.h_taps;
735b2d0a103SDmytro Laktyushkin 			int_part = data->taps.h_taps;
736b2d0a103SDmytro Laktyushkin 		}
737b2d0a103SDmytro Laktyushkin 		data->inits.h.value &= 0xffffffff;
738b2d0a103SDmytro Laktyushkin 		data->inits.h = dal_fixed31_32_add_int(data->inits.h, int_part);
739b2d0a103SDmytro Laktyushkin 	}
740b2d0a103SDmytro Laktyushkin 
741b2d0a103SDmytro Laktyushkin 	if (data->viewport_c.x) {
742b2d0a103SDmytro Laktyushkin 		int int_part;
743b2d0a103SDmytro Laktyushkin 
744b2d0a103SDmytro Laktyushkin 		data->inits.h_c = dal_fixed31_32_add(data->inits.h_c, dal_fixed31_32_mul_int(
745b2d0a103SDmytro Laktyushkin 				data->ratios.horz_c, recout_skip->width));
746b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.h_c) - data->viewport_c.x;
747b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.h_taps_c) {
748b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport_c.x >= (data->taps.h_taps_c - int_part) ?
749b2d0a103SDmytro Laktyushkin 					(data->taps.h_taps_c - int_part) : data->viewport_c.x;
750b2d0a103SDmytro Laktyushkin 			data->viewport_c.x -= int_adj;
751b2d0a103SDmytro Laktyushkin 			data->viewport_c.width += int_adj;
752b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
753b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.h_taps_c) {
754b2d0a103SDmytro Laktyushkin 			data->viewport_c.x += int_part - data->taps.h_taps_c;
755b2d0a103SDmytro Laktyushkin 			data->viewport_c.width -= int_part - data->taps.h_taps_c;
756b2d0a103SDmytro Laktyushkin 			int_part = data->taps.h_taps_c;
757b2d0a103SDmytro Laktyushkin 		}
758b2d0a103SDmytro Laktyushkin 		data->inits.h_c.value &= 0xffffffff;
759b2d0a103SDmytro Laktyushkin 		data->inits.h_c = dal_fixed31_32_add_int(data->inits.h_c, int_part);
760b2d0a103SDmytro Laktyushkin 	}
761b2d0a103SDmytro Laktyushkin 
762b2d0a103SDmytro Laktyushkin 	if (data->viewport.y) {
763b2d0a103SDmytro Laktyushkin 		int int_part;
764b2d0a103SDmytro Laktyushkin 
765b2d0a103SDmytro Laktyushkin 		data->inits.v = dal_fixed31_32_add(data->inits.v, dal_fixed31_32_mul_int(
766b2d0a103SDmytro Laktyushkin 				data->ratios.vert, recout_skip->height));
767b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.v) - data->viewport.y;
768b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.v_taps) {
769b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport.y >= (data->taps.v_taps - int_part) ?
770b2d0a103SDmytro Laktyushkin 						(data->taps.v_taps - int_part) : data->viewport.y;
771b2d0a103SDmytro Laktyushkin 			data->viewport.y -= int_adj;
772b2d0a103SDmytro Laktyushkin 			data->viewport.height += int_adj;
773b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
774b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.v_taps) {
775b2d0a103SDmytro Laktyushkin 			data->viewport.y += int_part - data->taps.v_taps;
776b2d0a103SDmytro Laktyushkin 			data->viewport.height -= int_part - data->taps.v_taps;
777b2d0a103SDmytro Laktyushkin 			int_part = data->taps.v_taps;
778b2d0a103SDmytro Laktyushkin 		}
779b2d0a103SDmytro Laktyushkin 		data->inits.v.value &= 0xffffffff;
780b2d0a103SDmytro Laktyushkin 		data->inits.v = dal_fixed31_32_add_int(data->inits.v, int_part);
781b2d0a103SDmytro Laktyushkin 	}
782b2d0a103SDmytro Laktyushkin 
783b2d0a103SDmytro Laktyushkin 	if (data->viewport_c.y) {
784b2d0a103SDmytro Laktyushkin 		int int_part;
785b2d0a103SDmytro Laktyushkin 
786b2d0a103SDmytro Laktyushkin 		data->inits.v_c = dal_fixed31_32_add(data->inits.v_c, dal_fixed31_32_mul_int(
787b2d0a103SDmytro Laktyushkin 				data->ratios.vert_c, recout_skip->height));
788b2d0a103SDmytro Laktyushkin 		int_part = dal_fixed31_32_floor(data->inits.v_c) - data->viewport_c.y;
789b2d0a103SDmytro Laktyushkin 		if (int_part < data->taps.v_taps_c) {
790b2d0a103SDmytro Laktyushkin 			int int_adj = data->viewport_c.y >= (data->taps.v_taps_c - int_part) ?
791b2d0a103SDmytro Laktyushkin 					(data->taps.v_taps_c - int_part) : data->viewport_c.y;
792b2d0a103SDmytro Laktyushkin 			data->viewport_c.y -= int_adj;
793b2d0a103SDmytro Laktyushkin 			data->viewport_c.height += int_adj;
794b2d0a103SDmytro Laktyushkin 			int_part += int_adj;
795b2d0a103SDmytro Laktyushkin 		} else if (int_part > data->taps.v_taps_c) {
796b2d0a103SDmytro Laktyushkin 			data->viewport_c.y += int_part - data->taps.v_taps_c;
797b2d0a103SDmytro Laktyushkin 			data->viewport_c.height -= int_part - data->taps.v_taps_c;
798b2d0a103SDmytro Laktyushkin 			int_part = data->taps.v_taps_c;
799b2d0a103SDmytro Laktyushkin 		}
800b2d0a103SDmytro Laktyushkin 		data->inits.v_c.value &= 0xffffffff;
801b2d0a103SDmytro Laktyushkin 		data->inits.v_c = dal_fixed31_32_add_int(data->inits.v_c, int_part);
802b2d0a103SDmytro Laktyushkin 	}
803b2d0a103SDmytro Laktyushkin 
804b2d0a103SDmytro Laktyushkin 	/* Interlaced inits based on final vert inits */
805b2d0a103SDmytro Laktyushkin 	data->inits.v_bot = dal_fixed31_32_add(data->inits.v, data->ratios.vert);
806b2d0a103SDmytro Laktyushkin 	data->inits.v_c_bot = dal_fixed31_32_add(data->inits.v_c, data->ratios.vert_c);
8071fbd2cfcSDmytro Laktyushkin 
8081fbd2cfcSDmytro Laktyushkin 	if (pipe_ctx->surface->public.rotation == ROTATION_ANGLE_90 ||
8091fbd2cfcSDmytro Laktyushkin 			pipe_ctx->surface->public.rotation == ROTATION_ANGLE_270) {
8101fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport_c);
8111fbd2cfcSDmytro Laktyushkin 		rect_swap_helper(&data->viewport);
8121fbd2cfcSDmytro Laktyushkin 	}
813b2d0a103SDmytro Laktyushkin }
814b2d0a103SDmytro Laktyushkin 
815b2d0a103SDmytro Laktyushkin bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
816b2d0a103SDmytro Laktyushkin {
817b2d0a103SDmytro Laktyushkin 	const struct dc_surface *surface = &pipe_ctx->surface->public;
8184562236bSHarry Wentland 	struct dc_crtc_timing *timing = &pipe_ctx->stream->public.timing;
819b2d0a103SDmytro Laktyushkin 	struct view recout_skip = { 0 };
820b2d0a103SDmytro Laktyushkin 	bool res = false;
821b2d0a103SDmytro Laktyushkin 
8224562236bSHarry Wentland 	/* Important: scaling ratio calculation requires pixel format,
8234562236bSHarry Wentland 	 * lb depth calculation requires recout and taps require scaling ratios.
824b2d0a103SDmytro Laktyushkin 	 * Inits require viewport, taps, ratios and recout of split pipe
8254562236bSHarry Wentland 	 */
826b2d0a103SDmytro Laktyushkin 	pipe_ctx->scl_data.format = convert_pixel_format_to_dalsurface(
827b2d0a103SDmytro Laktyushkin 			pipe_ctx->surface->public.format);
8284562236bSHarry Wentland 
829b2d0a103SDmytro Laktyushkin 	calculate_scaling_ratios(pipe_ctx);
830b2d0a103SDmytro Laktyushkin 
831b2d0a103SDmytro Laktyushkin 	calculate_viewport(pipe_ctx);
8324562236bSHarry Wentland 
8334562236bSHarry Wentland 	if (pipe_ctx->scl_data.viewport.height < 16 || pipe_ctx->scl_data.viewport.width < 16)
8344562236bSHarry Wentland 		return false;
8354562236bSHarry Wentland 
836b2d0a103SDmytro Laktyushkin 	calculate_recout(pipe_ctx, &recout_skip);
8374562236bSHarry Wentland 
8384562236bSHarry Wentland 	/**
8394562236bSHarry Wentland 	 * Setting line buffer pixel depth to 24bpp yields banding
8404562236bSHarry Wentland 	 * on certain displays, such as the Sharp 4k
8414562236bSHarry Wentland 	 */
8424562236bSHarry Wentland 	pipe_ctx->scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
8434562236bSHarry Wentland 
8444562236bSHarry Wentland 	pipe_ctx->scl_data.h_active = timing->h_addressable;
8454562236bSHarry Wentland 	pipe_ctx->scl_data.v_active = timing->v_addressable;
8464562236bSHarry Wentland 
8474562236bSHarry Wentland 	/* Taps calculations */
8484562236bSHarry Wentland 	res = pipe_ctx->xfm->funcs->transform_get_optimal_number_of_taps(
8494562236bSHarry Wentland 		pipe_ctx->xfm, &pipe_ctx->scl_data, &surface->scaling_quality);
8504562236bSHarry Wentland 
8514562236bSHarry Wentland 	if (!res) {
8524562236bSHarry Wentland 		/* Try 24 bpp linebuffer */
8534562236bSHarry Wentland 		pipe_ctx->scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
8544562236bSHarry Wentland 
8554562236bSHarry Wentland 		res = pipe_ctx->xfm->funcs->transform_get_optimal_number_of_taps(
8564562236bSHarry Wentland 			pipe_ctx->xfm, &pipe_ctx->scl_data, &surface->scaling_quality);
8574562236bSHarry Wentland 	}
8584562236bSHarry Wentland 
859b2d0a103SDmytro Laktyushkin 	if (res)
8601fbd2cfcSDmytro Laktyushkin 		/* May need to re-check lb size after this in some obscure scenario */
861b2d0a103SDmytro Laktyushkin 		calculate_inits_and_adj_vp(pipe_ctx, &recout_skip);
862b2d0a103SDmytro Laktyushkin 
8634562236bSHarry Wentland 	dm_logger_write(pipe_ctx->stream->ctx->logger, LOG_SCALER,
8644562236bSHarry Wentland 				"%s: Viewport:\nheight:%d width:%d x:%d "
8654562236bSHarry Wentland 				"y:%d\n dst_rect:\nheight:%d width:%d x:%d "
8664562236bSHarry Wentland 				"y:%d\n",
8674562236bSHarry Wentland 				__func__,
8684562236bSHarry Wentland 				pipe_ctx->scl_data.viewport.height,
8694562236bSHarry Wentland 				pipe_ctx->scl_data.viewport.width,
8704562236bSHarry Wentland 				pipe_ctx->scl_data.viewport.x,
8714562236bSHarry Wentland 				pipe_ctx->scl_data.viewport.y,
8724562236bSHarry Wentland 				surface->dst_rect.height,
8734562236bSHarry Wentland 				surface->dst_rect.width,
8744562236bSHarry Wentland 				surface->dst_rect.x,
8754562236bSHarry Wentland 				surface->dst_rect.y);
8764562236bSHarry Wentland 
8774562236bSHarry Wentland 	return res;
8784562236bSHarry Wentland }
8794562236bSHarry Wentland 
8804562236bSHarry Wentland 
8814562236bSHarry Wentland enum dc_status resource_build_scaling_params_for_context(
8824562236bSHarry Wentland 	const struct core_dc *dc,
8834562236bSHarry Wentland 	struct validate_context *context)
8844562236bSHarry Wentland {
8854562236bSHarry Wentland 	int i;
8864562236bSHarry Wentland 
8874562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
8884562236bSHarry Wentland 		if (context->res_ctx.pipe_ctx[i].surface != NULL &&
8894562236bSHarry Wentland 				context->res_ctx.pipe_ctx[i].stream != NULL)
890b2d0a103SDmytro Laktyushkin 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
891f84a8161STony Cheng 				return DC_FAIL_SCALING;
8924562236bSHarry Wentland 	}
8934562236bSHarry Wentland 
8944562236bSHarry Wentland 	return DC_OK;
8954562236bSHarry Wentland }
8964562236bSHarry Wentland 
897a2b8659dSTony Cheng struct pipe_ctx *find_idle_secondary_pipe(
898a2b8659dSTony Cheng 		struct resource_context *res_ctx,
899a2b8659dSTony Cheng 		const struct resource_pool *pool)
9004562236bSHarry Wentland {
9014562236bSHarry Wentland 	int i;
9024562236bSHarry Wentland 	struct pipe_ctx *secondary_pipe = NULL;
9034562236bSHarry Wentland 
9044562236bSHarry Wentland 	/*
9054562236bSHarry Wentland 	 * search backwards for the second pipe to keep pipe
9064562236bSHarry Wentland 	 * assignment more consistent
9074562236bSHarry Wentland 	 */
9084562236bSHarry Wentland 
909a2b8659dSTony Cheng 	for (i = pool->pipe_count - 1; i >= 0; i--) {
9104562236bSHarry Wentland 		if (res_ctx->pipe_ctx[i].stream == NULL) {
9114562236bSHarry Wentland 			secondary_pipe = &res_ctx->pipe_ctx[i];
9124562236bSHarry Wentland 			secondary_pipe->pipe_idx = i;
9134562236bSHarry Wentland 			break;
9144562236bSHarry Wentland 		}
9154562236bSHarry Wentland 	}
9164562236bSHarry Wentland 
9174562236bSHarry Wentland 
9184562236bSHarry Wentland 	return secondary_pipe;
9194562236bSHarry Wentland }
9204562236bSHarry Wentland 
9214562236bSHarry Wentland struct pipe_ctx *resource_get_head_pipe_for_stream(
9224562236bSHarry Wentland 		struct resource_context *res_ctx,
9234562236bSHarry Wentland 		const struct core_stream *stream)
9244562236bSHarry Wentland {
9254562236bSHarry Wentland 	int i;
926a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
9274562236bSHarry Wentland 		if (res_ctx->pipe_ctx[i].stream == stream &&
928e73c1efcSYongqiang Sun 				res_ctx->pipe_ctx[i].stream_enc) {
9294562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
9304562236bSHarry Wentland 			break;
9314562236bSHarry Wentland 		}
9324562236bSHarry Wentland 	}
9334562236bSHarry Wentland 	return NULL;
9344562236bSHarry Wentland }
9354562236bSHarry Wentland 
9364562236bSHarry Wentland /*
937ab2541b6SAric Cyr  * A free_pipe for a stream is defined here as a pipe
938ab2541b6SAric Cyr  * that has no surface attached yet
9394562236bSHarry Wentland  */
940ab2541b6SAric Cyr static struct pipe_ctx *acquire_free_pipe_for_stream(
941745cc746SDmytro Laktyushkin 		struct validate_context *context,
942a2b8659dSTony Cheng 		const struct resource_pool *pool,
943ab2541b6SAric Cyr 		const struct dc_stream *dc_stream)
9444562236bSHarry Wentland {
9454562236bSHarry Wentland 	int i;
946745cc746SDmytro Laktyushkin 	struct resource_context *res_ctx = &context->res_ctx;
947ab2541b6SAric Cyr 	struct core_stream *stream = DC_STREAM_TO_CORE(dc_stream);
9484562236bSHarry Wentland 
9494562236bSHarry Wentland 	struct pipe_ctx *head_pipe = NULL;
9504562236bSHarry Wentland 
9514562236bSHarry Wentland 	/* Find head pipe, which has the back end set up*/
9524562236bSHarry Wentland 
9534562236bSHarry Wentland 	head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
9544562236bSHarry Wentland 
9554562236bSHarry Wentland 	if (!head_pipe)
9564562236bSHarry Wentland 		ASSERT(0);
9574562236bSHarry Wentland 
9584562236bSHarry Wentland 	if (!head_pipe->surface)
9594562236bSHarry Wentland 		return head_pipe;
9604562236bSHarry Wentland 
9614562236bSHarry Wentland 	/* Re-use pipe already acquired for this stream if available*/
962a2b8659dSTony Cheng 	for (i = pool->pipe_count - 1; i >= 0; i--) {
9634562236bSHarry Wentland 		if (res_ctx->pipe_ctx[i].stream == stream &&
9644562236bSHarry Wentland 				!res_ctx->pipe_ctx[i].surface) {
9654562236bSHarry Wentland 			return &res_ctx->pipe_ctx[i];
9664562236bSHarry Wentland 		}
9674562236bSHarry Wentland 	}
9684562236bSHarry Wentland 
9694562236bSHarry Wentland 	/*
9704562236bSHarry Wentland 	 * At this point we have no re-useable pipe for this stream and we need
9714562236bSHarry Wentland 	 * to acquire an idle one to satisfy the request
9724562236bSHarry Wentland 	 */
9734562236bSHarry Wentland 
974a2b8659dSTony Cheng 	if (!pool->funcs->acquire_idle_pipe_for_layer)
9754562236bSHarry Wentland 		return NULL;
9764562236bSHarry Wentland 
977a2b8659dSTony Cheng 	return pool->funcs->acquire_idle_pipe_for_layer(context, pool, stream);
9784562236bSHarry Wentland 
9794562236bSHarry Wentland }
9804562236bSHarry Wentland 
981ab2541b6SAric Cyr static void release_free_pipes_for_stream(
9824562236bSHarry Wentland 		struct resource_context *res_ctx,
983ab2541b6SAric Cyr 		const struct dc_stream *dc_stream)
9844562236bSHarry Wentland {
9854562236bSHarry Wentland 	int i;
986ab2541b6SAric Cyr 	struct core_stream *stream = DC_STREAM_TO_CORE(dc_stream);
9874562236bSHarry Wentland 
988a2b8659dSTony Cheng 	for (i = MAX_PIPES - 1; i >= 0; i--) {
98905a19c39SDmytro Laktyushkin 		/* never release the topmost pipe*/
9904562236bSHarry Wentland 		if (res_ctx->pipe_ctx[i].stream == stream &&
99105a19c39SDmytro Laktyushkin 				res_ctx->pipe_ctx[i].top_pipe &&
9924562236bSHarry Wentland 				!res_ctx->pipe_ctx[i].surface) {
99305a19c39SDmytro Laktyushkin 			memset(&res_ctx->pipe_ctx[i], 0, sizeof(struct pipe_ctx));
9944562236bSHarry Wentland 		}
9954562236bSHarry Wentland 	}
9964562236bSHarry Wentland }
9974562236bSHarry Wentland 
9984562236bSHarry Wentland bool resource_attach_surfaces_to_context(
9994562236bSHarry Wentland 		const struct dc_surface * const *surfaces,
10004562236bSHarry Wentland 		int surface_count,
1001ab2541b6SAric Cyr 		const struct dc_stream *dc_stream,
1002a2b8659dSTony Cheng 		struct validate_context *context,
1003a2b8659dSTony Cheng 		const struct resource_pool *pool)
10044562236bSHarry Wentland {
10054562236bSHarry Wentland 	int i;
10064562236bSHarry Wentland 	struct pipe_ctx *tail_pipe;
1007ab2541b6SAric Cyr 	struct dc_stream_status *stream_status = NULL;
100805a19c39SDmytro Laktyushkin 	struct core_stream *stream = DC_STREAM_TO_CORE(dc_stream);
10094562236bSHarry Wentland 
10104562236bSHarry Wentland 
10114562236bSHarry Wentland 	if (surface_count > MAX_SURFACE_NUM) {
10124562236bSHarry Wentland 		dm_error("Surface: can not attach %d surfaces! Maximum is: %d\n",
10134562236bSHarry Wentland 			surface_count, MAX_SURFACE_NUM);
10144562236bSHarry Wentland 		return false;
10154562236bSHarry Wentland 	}
10164562236bSHarry Wentland 
1017ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++)
1018ab2541b6SAric Cyr 		if (&context->streams[i]->public == dc_stream) {
1019ab2541b6SAric Cyr 			stream_status = &context->stream_status[i];
10204562236bSHarry Wentland 			break;
10214562236bSHarry Wentland 		}
1022ab2541b6SAric Cyr 	if (stream_status == NULL) {
1023ab2541b6SAric Cyr 		dm_error("Existing stream not found; failed to attach surfaces\n");
10244562236bSHarry Wentland 		return false;
10254562236bSHarry Wentland 	}
10264562236bSHarry Wentland 
10274562236bSHarry Wentland 	/* retain new surfaces */
10284562236bSHarry Wentland 	for (i = 0; i < surface_count; i++)
10294562236bSHarry Wentland 		dc_surface_retain(surfaces[i]);
10304562236bSHarry Wentland 
103105a19c39SDmytro Laktyushkin 	/* detach surfaces from pipes */
103205a19c39SDmytro Laktyushkin 	for (i = 0; i < pool->pipe_count; i++)
103305a19c39SDmytro Laktyushkin 		if (context->res_ctx.pipe_ctx[i].stream == stream) {
103405a19c39SDmytro Laktyushkin 			context->res_ctx.pipe_ctx[i].surface = NULL;
103505a19c39SDmytro Laktyushkin 			context->res_ctx.pipe_ctx[i].bottom_pipe = NULL;
103605a19c39SDmytro Laktyushkin 		}
10374562236bSHarry Wentland 
10384562236bSHarry Wentland 	/* release existing surfaces*/
1039ab2541b6SAric Cyr 	for (i = 0; i < stream_status->surface_count; i++)
1040ab2541b6SAric Cyr 		dc_surface_release(stream_status->surfaces[i]);
10414562236bSHarry Wentland 
1042ab2541b6SAric Cyr 	for (i = surface_count; i < stream_status->surface_count; i++)
1043ab2541b6SAric Cyr 		stream_status->surfaces[i] = NULL;
10444562236bSHarry Wentland 
10454562236bSHarry Wentland 	tail_pipe = NULL;
10464562236bSHarry Wentland 	for (i = 0; i < surface_count; i++) {
10474562236bSHarry Wentland 		struct core_surface *surface = DC_SURFACE_TO_CORE(surfaces[i]);
1048a2b8659dSTony Cheng 		struct pipe_ctx *free_pipe = acquire_free_pipe_for_stream(
1049a2b8659dSTony Cheng 				context, pool, dc_stream);
10504562236bSHarry Wentland 
10514562236bSHarry Wentland 		if (!free_pipe) {
1052ab2541b6SAric Cyr 			stream_status->surfaces[i] = NULL;
10534562236bSHarry Wentland 			return false;
10544562236bSHarry Wentland 		}
10554562236bSHarry Wentland 
10564562236bSHarry Wentland 		free_pipe->surface = surface;
10574562236bSHarry Wentland 
10584562236bSHarry Wentland 		if (tail_pipe) {
10594562236bSHarry Wentland 			free_pipe->top_pipe = tail_pipe;
10604562236bSHarry Wentland 			tail_pipe->bottom_pipe = free_pipe;
10614562236bSHarry Wentland 		}
10624562236bSHarry Wentland 
10634562236bSHarry Wentland 		tail_pipe = free_pipe;
10644562236bSHarry Wentland 	}
10654562236bSHarry Wentland 
1066ab2541b6SAric Cyr 	release_free_pipes_for_stream(&context->res_ctx, dc_stream);
10674562236bSHarry Wentland 
10684562236bSHarry Wentland 	/* assign new surfaces*/
10694562236bSHarry Wentland 	for (i = 0; i < surface_count; i++)
1070ab2541b6SAric Cyr 		stream_status->surfaces[i] = surfaces[i];
10714562236bSHarry Wentland 
1072ab2541b6SAric Cyr 	stream_status->surface_count = surface_count;
10734562236bSHarry Wentland 
10744562236bSHarry Wentland 	return true;
10754562236bSHarry Wentland }
10764562236bSHarry Wentland 
10774562236bSHarry Wentland 
10784562236bSHarry Wentland static bool is_timing_changed(const struct core_stream *cur_stream,
10794562236bSHarry Wentland 		const struct core_stream *new_stream)
10804562236bSHarry Wentland {
10814562236bSHarry Wentland 	if (cur_stream == NULL)
10824562236bSHarry Wentland 		return true;
10834562236bSHarry Wentland 
10844562236bSHarry Wentland 	/* If sink pointer changed, it means this is a hotplug, we should do
10854562236bSHarry Wentland 	 * full hw setting.
10864562236bSHarry Wentland 	 */
10874562236bSHarry Wentland 	if (cur_stream->sink != new_stream->sink)
10884562236bSHarry Wentland 		return true;
10894562236bSHarry Wentland 
10904562236bSHarry Wentland 	/* If output color space is changed, need to reprogram info frames */
10914562236bSHarry Wentland 	if (cur_stream->public.output_color_space !=
10924562236bSHarry Wentland 			new_stream->public.output_color_space)
10934562236bSHarry Wentland 		return true;
10944562236bSHarry Wentland 
10954562236bSHarry Wentland 	return memcmp(
10964562236bSHarry Wentland 		&cur_stream->public.timing,
10974562236bSHarry Wentland 		&new_stream->public.timing,
10984562236bSHarry Wentland 		sizeof(struct dc_crtc_timing)) != 0;
10994562236bSHarry Wentland }
11004562236bSHarry Wentland 
11014562236bSHarry Wentland static bool are_stream_backends_same(
11024562236bSHarry Wentland 	const struct core_stream *stream_a, const struct core_stream *stream_b)
11034562236bSHarry Wentland {
11044562236bSHarry Wentland 	if (stream_a == stream_b)
11054562236bSHarry Wentland 		return true;
11064562236bSHarry Wentland 
11074562236bSHarry Wentland 	if (stream_a == NULL || stream_b == NULL)
11084562236bSHarry Wentland 		return false;
11094562236bSHarry Wentland 
11104562236bSHarry Wentland 	if (is_timing_changed(stream_a, stream_b))
11114562236bSHarry Wentland 		return false;
11124562236bSHarry Wentland 
11134562236bSHarry Wentland 	return true;
11144562236bSHarry Wentland }
11154562236bSHarry Wentland 
1116ab2541b6SAric Cyr bool is_stream_unchanged(
1117ab2541b6SAric Cyr 	const struct core_stream *old_stream, const struct core_stream *stream)
11184562236bSHarry Wentland {
11194562236bSHarry Wentland 
11204562236bSHarry Wentland 	if (!are_stream_backends_same(old_stream, stream))
11214562236bSHarry Wentland 		return false;
11224562236bSHarry Wentland 
11234562236bSHarry Wentland 	return true;
11244562236bSHarry Wentland }
11254562236bSHarry Wentland 
11264562236bSHarry Wentland bool resource_validate_attach_surfaces(
11274562236bSHarry Wentland 		const struct dc_validation_set set[],
11284562236bSHarry Wentland 		int set_count,
11294562236bSHarry Wentland 		const struct validate_context *old_context,
1130a2b8659dSTony Cheng 		struct validate_context *context,
1131a2b8659dSTony Cheng 		const struct resource_pool *pool)
11324562236bSHarry Wentland {
11334562236bSHarry Wentland 	int i, j;
11344562236bSHarry Wentland 
11354562236bSHarry Wentland 	for (i = 0; i < set_count; i++) {
1136430ef426SDmytro Laktyushkin 		for (j = 0; old_context && j < old_context->stream_count; j++)
1137ab2541b6SAric Cyr 			if (is_stream_unchanged(
1138ab2541b6SAric Cyr 					old_context->streams[j],
1139ab2541b6SAric Cyr 					context->streams[i])) {
11404562236bSHarry Wentland 				if (!resource_attach_surfaces_to_context(
1141ab2541b6SAric Cyr 						old_context->stream_status[j].surfaces,
1142ab2541b6SAric Cyr 						old_context->stream_status[j].surface_count,
1143ab2541b6SAric Cyr 						&context->streams[i]->public,
1144a2b8659dSTony Cheng 						context, pool))
11454562236bSHarry Wentland 					return false;
1146ab2541b6SAric Cyr 				context->stream_status[i] = old_context->stream_status[j];
11474562236bSHarry Wentland 			}
11484562236bSHarry Wentland 		if (set[i].surface_count != 0)
11494562236bSHarry Wentland 			if (!resource_attach_surfaces_to_context(
11504562236bSHarry Wentland 					set[i].surfaces,
11514562236bSHarry Wentland 					set[i].surface_count,
1152ab2541b6SAric Cyr 					&context->streams[i]->public,
1153a2b8659dSTony Cheng 					context, pool))
11544562236bSHarry Wentland 				return false;
11554562236bSHarry Wentland 
11564562236bSHarry Wentland 	}
11574562236bSHarry Wentland 
11584562236bSHarry Wentland 	return true;
11594562236bSHarry Wentland }
11604562236bSHarry Wentland 
11614562236bSHarry Wentland /* Maximum TMDS single link pixel clock 165MHz */
11624562236bSHarry Wentland #define TMDS_MAX_PIXEL_CLOCK_IN_KHZ 165000
11634562236bSHarry Wentland 
11644562236bSHarry Wentland static void set_stream_engine_in_use(
11654562236bSHarry Wentland 		struct resource_context *res_ctx,
1166a2b8659dSTony Cheng 		const struct resource_pool *pool,
11674562236bSHarry Wentland 		struct stream_encoder *stream_enc)
11684562236bSHarry Wentland {
11694562236bSHarry Wentland 	int i;
11704562236bSHarry Wentland 
1171a2b8659dSTony Cheng 	for (i = 0; i < pool->stream_enc_count; i++) {
1172a2b8659dSTony Cheng 		if (pool->stream_enc[i] == stream_enc)
11734562236bSHarry Wentland 			res_ctx->is_stream_enc_acquired[i] = true;
11744562236bSHarry Wentland 	}
11754562236bSHarry Wentland }
11764562236bSHarry Wentland 
11774562236bSHarry Wentland /* TODO: release audio object */
11784562236bSHarry Wentland static void set_audio_in_use(
11794562236bSHarry Wentland 		struct resource_context *res_ctx,
1180a2b8659dSTony Cheng 		const struct resource_pool *pool,
11814562236bSHarry Wentland 		struct audio *audio)
11824562236bSHarry Wentland {
11834562236bSHarry Wentland 	int i;
1184a2b8659dSTony Cheng 	for (i = 0; i < pool->audio_count; i++) {
1185a2b8659dSTony Cheng 		if (pool->audios[i] == audio)
11864562236bSHarry Wentland 			res_ctx->is_audio_acquired[i] = true;
11874562236bSHarry Wentland 	}
11884562236bSHarry Wentland }
11894562236bSHarry Wentland 
11904562236bSHarry Wentland static int acquire_first_free_pipe(
11914562236bSHarry Wentland 		struct resource_context *res_ctx,
1192a2b8659dSTony Cheng 		const struct resource_pool *pool,
11934562236bSHarry Wentland 		struct core_stream *stream)
11944562236bSHarry Wentland {
11954562236bSHarry Wentland 	int i;
11964562236bSHarry Wentland 
1197a2b8659dSTony Cheng 	for (i = 0; i < pool->pipe_count; i++) {
11984562236bSHarry Wentland 		if (!res_ctx->pipe_ctx[i].stream) {
11994562236bSHarry Wentland 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
12004562236bSHarry Wentland 
1201a2b8659dSTony Cheng 			pipe_ctx->tg = pool->timing_generators[i];
1202a2b8659dSTony Cheng 			pipe_ctx->mi = pool->mis[i];
1203a2b8659dSTony Cheng 			pipe_ctx->ipp = pool->ipps[i];
1204a2b8659dSTony Cheng 			pipe_ctx->xfm = pool->transforms[i];
1205a2b8659dSTony Cheng 			pipe_ctx->opp = pool->opps[i];
1206a2b8659dSTony Cheng 			pipe_ctx->dis_clk = pool->display_clock;
12074562236bSHarry Wentland 			pipe_ctx->pipe_idx = i;
12084562236bSHarry Wentland 
1209ff5ef992SAlex Deucher #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1210ff5ef992SAlex Deucher 			pipe_ctx->mpc_idx = -1;
1211ff5ef992SAlex Deucher #endif
1212ff5ef992SAlex Deucher 
12134562236bSHarry Wentland 			pipe_ctx->stream = stream;
12144562236bSHarry Wentland 			return i;
12154562236bSHarry Wentland 		}
12164562236bSHarry Wentland 	}
12174562236bSHarry Wentland 	return -1;
12184562236bSHarry Wentland }
12194562236bSHarry Wentland 
12204562236bSHarry Wentland static struct stream_encoder *find_first_free_match_stream_enc_for_link(
12214562236bSHarry Wentland 		struct resource_context *res_ctx,
1222a2b8659dSTony Cheng 		const struct resource_pool *pool,
12234562236bSHarry Wentland 		struct core_stream *stream)
12244562236bSHarry Wentland {
12254562236bSHarry Wentland 	int i;
12264562236bSHarry Wentland 	int j = -1;
12274562236bSHarry Wentland 	struct core_link *link = stream->sink->link;
12284562236bSHarry Wentland 
1229a2b8659dSTony Cheng 	for (i = 0; i < pool->stream_enc_count; i++) {
12304562236bSHarry Wentland 		if (!res_ctx->is_stream_enc_acquired[i] &&
1231a2b8659dSTony Cheng 				pool->stream_enc[i]) {
12324562236bSHarry Wentland 			/* Store first available for MST second display
12334562236bSHarry Wentland 			 * in daisy chain use case */
12344562236bSHarry Wentland 			j = i;
1235a2b8659dSTony Cheng 			if (pool->stream_enc[i]->id ==
12364562236bSHarry Wentland 					link->link_enc->preferred_engine)
1237a2b8659dSTony Cheng 				return pool->stream_enc[i];
12384562236bSHarry Wentland 		}
12394562236bSHarry Wentland 	}
12404562236bSHarry Wentland 
12414562236bSHarry Wentland 	/*
12424562236bSHarry Wentland 	 * below can happen in cases when stream encoder is acquired:
12434562236bSHarry Wentland 	 * 1) for second MST display in chain, so preferred engine already
12444562236bSHarry Wentland 	 * acquired;
12454562236bSHarry Wentland 	 * 2) for another link, which preferred engine already acquired by any
12464562236bSHarry Wentland 	 * MST configuration.
12474562236bSHarry Wentland 	 *
12484562236bSHarry Wentland 	 * If signal is of DP type and preferred engine not found, return last available
12494562236bSHarry Wentland 	 *
12504562236bSHarry Wentland 	 * TODO - This is just a patch up and a generic solution is
12514562236bSHarry Wentland 	 * required for non DP connectors.
12524562236bSHarry Wentland 	 */
12534562236bSHarry Wentland 
12544562236bSHarry Wentland 	if (j >= 0 && dc_is_dp_signal(stream->signal))
1255a2b8659dSTony Cheng 		return pool->stream_enc[j];
12564562236bSHarry Wentland 
12574562236bSHarry Wentland 	return NULL;
12584562236bSHarry Wentland }
12594562236bSHarry Wentland 
1260a2b8659dSTony Cheng static struct audio *find_first_free_audio(
1261a2b8659dSTony Cheng 		struct resource_context *res_ctx,
1262a2b8659dSTony Cheng 		const struct resource_pool *pool)
12634562236bSHarry Wentland {
12644562236bSHarry Wentland 	int i;
1265a2b8659dSTony Cheng 	for (i = 0; i < pool->audio_count; i++) {
12664562236bSHarry Wentland 		if (res_ctx->is_audio_acquired[i] == false) {
1267a2b8659dSTony Cheng 			return pool->audios[i];
12684562236bSHarry Wentland 		}
12694562236bSHarry Wentland 	}
12704562236bSHarry Wentland 
12714562236bSHarry Wentland 	return 0;
12724562236bSHarry Wentland }
12734562236bSHarry Wentland 
12744562236bSHarry Wentland static void update_stream_signal(struct core_stream *stream)
12754562236bSHarry Wentland {
12768b32076cSSylvia Tsai 	if (stream->public.output_signal == SIGNAL_TYPE_NONE) {
12774562236bSHarry Wentland 		const struct dc_sink *dc_sink = stream->public.sink;
12784562236bSHarry Wentland 
12792796eaeeSJoshua Aberback 		if (dc_sink->sink_signal == SIGNAL_TYPE_NONE)
12808b32076cSSylvia Tsai 			stream->signal =
12818b32076cSSylvia Tsai 					stream->sink->link->
12828b32076cSSylvia Tsai 					public.connector_signal;
12832796eaeeSJoshua Aberback 		else
12842796eaeeSJoshua Aberback 			stream->signal = dc_sink->sink_signal;
12858b32076cSSylvia Tsai 	} else {
12868b32076cSSylvia Tsai 		stream->signal = stream->public.output_signal;
12878b32076cSSylvia Tsai 	}
12888b32076cSSylvia Tsai 
128956dcade3SHarry Wentland 	if (dc_is_dvi_signal(stream->signal)) {
129056dcade3SHarry Wentland 		if (stream->public.timing.pix_clk_khz > TMDS_MAX_PIXEL_CLOCK_IN_KHZ)
12918b32076cSSylvia Tsai 			stream->signal = SIGNAL_TYPE_DVI_DUAL_LINK;
129256dcade3SHarry Wentland 		else
129356dcade3SHarry Wentland 			stream->signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
129456dcade3SHarry Wentland 	}
12954562236bSHarry Wentland }
12964562236bSHarry Wentland 
12974562236bSHarry Wentland bool resource_is_stream_unchanged(
1298ab2541b6SAric Cyr 	const struct validate_context *old_context, const struct core_stream *stream)
12994562236bSHarry Wentland {
1300ab2541b6SAric Cyr 	int i;
13014562236bSHarry Wentland 
1302ab2541b6SAric Cyr 	for (i = 0; i < old_context->stream_count; i++) {
1303ab2541b6SAric Cyr 		const struct core_stream *old_stream = old_context->streams[i];
13044562236bSHarry Wentland 
13054562236bSHarry Wentland 		if (are_stream_backends_same(old_stream, stream))
13064562236bSHarry Wentland 				return true;
13074562236bSHarry Wentland 	}
13084562236bSHarry Wentland 
13094562236bSHarry Wentland 	return false;
13104562236bSHarry Wentland }
13114562236bSHarry Wentland 
13124562236bSHarry Wentland static void copy_pipe_ctx(
13134562236bSHarry Wentland 	const struct pipe_ctx *from_pipe_ctx, struct pipe_ctx *to_pipe_ctx)
13144562236bSHarry Wentland {
13154562236bSHarry Wentland 	struct core_surface *surface = to_pipe_ctx->surface;
13164562236bSHarry Wentland 	struct core_stream *stream = to_pipe_ctx->stream;
13174562236bSHarry Wentland 
13184562236bSHarry Wentland 	*to_pipe_ctx = *from_pipe_ctx;
13194562236bSHarry Wentland 	to_pipe_ctx->stream = stream;
13204562236bSHarry Wentland 	if (surface != NULL)
13214562236bSHarry Wentland 		to_pipe_ctx->surface = surface;
13224562236bSHarry Wentland }
13234562236bSHarry Wentland 
13244562236bSHarry Wentland static struct core_stream *find_pll_sharable_stream(
13254562236bSHarry Wentland 		const struct core_stream *stream_needs_pll,
13264562236bSHarry Wentland 		struct validate_context *context)
13274562236bSHarry Wentland {
1328ab2541b6SAric Cyr 	int i;
13294562236bSHarry Wentland 
1330ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
1331ab2541b6SAric Cyr 		struct core_stream *stream_has_pll = context->streams[i];
13324562236bSHarry Wentland 
13334562236bSHarry Wentland 		/* We are looking for non dp, non virtual stream */
13344562236bSHarry Wentland 		if (resource_are_streams_timing_synchronizable(
13354562236bSHarry Wentland 			stream_needs_pll, stream_has_pll)
13364562236bSHarry Wentland 			&& !dc_is_dp_signal(stream_has_pll->signal)
13374562236bSHarry Wentland 			&& stream_has_pll->sink->link->public.connector_signal
13384562236bSHarry Wentland 			!= SIGNAL_TYPE_VIRTUAL)
13394562236bSHarry Wentland 			return stream_has_pll;
1340ab2541b6SAric Cyr 
13414562236bSHarry Wentland 	}
13424562236bSHarry Wentland 
13434562236bSHarry Wentland 	return NULL;
13444562236bSHarry Wentland }
13454562236bSHarry Wentland 
13464562236bSHarry Wentland static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
13474562236bSHarry Wentland {
13484562236bSHarry Wentland 	uint32_t pix_clk = timing->pix_clk_khz;
13494562236bSHarry Wentland 	uint32_t normalized_pix_clk = pix_clk;
13504562236bSHarry Wentland 
13514562236bSHarry Wentland 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
13524562236bSHarry Wentland 		pix_clk /= 2;
1353cc4d99b8SCharlene Liu 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
13544562236bSHarry Wentland 		switch (timing->display_color_depth) {
13554562236bSHarry Wentland 		case COLOR_DEPTH_888:
13564562236bSHarry Wentland 			normalized_pix_clk = pix_clk;
13574562236bSHarry Wentland 			break;
13584562236bSHarry Wentland 		case COLOR_DEPTH_101010:
13594562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 30) / 24;
13604562236bSHarry Wentland 			break;
13614562236bSHarry Wentland 		case COLOR_DEPTH_121212:
13624562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 36) / 24;
13634562236bSHarry Wentland 		break;
13644562236bSHarry Wentland 		case COLOR_DEPTH_161616:
13654562236bSHarry Wentland 			normalized_pix_clk = (pix_clk * 48) / 24;
13664562236bSHarry Wentland 		break;
13674562236bSHarry Wentland 		default:
13684562236bSHarry Wentland 			ASSERT(0);
13694562236bSHarry Wentland 		break;
13704562236bSHarry Wentland 		}
1371cc4d99b8SCharlene Liu 	}
13724562236bSHarry Wentland 	return normalized_pix_clk;
13734562236bSHarry Wentland }
13744562236bSHarry Wentland 
1375430ef426SDmytro Laktyushkin static void calculate_phy_pix_clks(struct validate_context *context)
13764562236bSHarry Wentland {
1377ab2541b6SAric Cyr 	int i;
13784562236bSHarry Wentland 
1379ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
1380ab2541b6SAric Cyr 		struct core_stream *stream = context->streams[i];
13814562236bSHarry Wentland 
13824562236bSHarry Wentland 		update_stream_signal(stream);
13834562236bSHarry Wentland 
13844562236bSHarry Wentland 		/* update actual pixel clock on all streams */
13854562236bSHarry Wentland 		if (dc_is_hdmi_signal(stream->signal))
13864562236bSHarry Wentland 			stream->phy_pix_clk = get_norm_pix_clk(
13874562236bSHarry Wentland 				&stream->public.timing);
13884562236bSHarry Wentland 		else
13894562236bSHarry Wentland 			stream->phy_pix_clk =
13904562236bSHarry Wentland 				stream->public.timing.pix_clk_khz;
13914562236bSHarry Wentland 	}
13924562236bSHarry Wentland }
13934562236bSHarry Wentland 
13945d11e9fcSDmytro Laktyushkin #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
13955d11e9fcSDmytro Laktyushkin static int acquire_first_split_pipe(
13965d11e9fcSDmytro Laktyushkin 		struct resource_context *res_ctx,
13975d11e9fcSDmytro Laktyushkin 		const struct resource_pool *pool,
13985d11e9fcSDmytro Laktyushkin 		struct core_stream *stream)
13995d11e9fcSDmytro Laktyushkin {
14005d11e9fcSDmytro Laktyushkin 	int i;
14015d11e9fcSDmytro Laktyushkin 
14025d11e9fcSDmytro Laktyushkin 	for (i = 0; i < pool->pipe_count; i++) {
14035d11e9fcSDmytro Laktyushkin 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
14045d11e9fcSDmytro Laktyushkin 
14055d11e9fcSDmytro Laktyushkin 		if (pipe_ctx->top_pipe &&
14065d11e9fcSDmytro Laktyushkin 				pipe_ctx->top_pipe->surface == pipe_ctx->surface) {
14075d11e9fcSDmytro Laktyushkin 			int mpc_idx = pipe_ctx->mpc_idx;
14085d11e9fcSDmytro Laktyushkin 
14095d11e9fcSDmytro Laktyushkin 			pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
14105d11e9fcSDmytro Laktyushkin 			pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
14115d11e9fcSDmytro Laktyushkin 
14125d11e9fcSDmytro Laktyushkin 			memset(pipe_ctx, 0, sizeof(*pipe_ctx));
14135d11e9fcSDmytro Laktyushkin 			pipe_ctx->tg = pool->timing_generators[i];
14145d11e9fcSDmytro Laktyushkin 			pipe_ctx->mi = pool->mis[i];
14155d11e9fcSDmytro Laktyushkin 			pipe_ctx->ipp = pool->ipps[i];
14165d11e9fcSDmytro Laktyushkin 			pipe_ctx->xfm = pool->transforms[i];
14175d11e9fcSDmytro Laktyushkin 			pipe_ctx->opp = pool->opps[i];
14185d11e9fcSDmytro Laktyushkin 			pipe_ctx->dis_clk = pool->display_clock;
14195d11e9fcSDmytro Laktyushkin 			pipe_ctx->pipe_idx = i;
14205d11e9fcSDmytro Laktyushkin 			pipe_ctx->mpc_idx = mpc_idx;
14215d11e9fcSDmytro Laktyushkin 
14225d11e9fcSDmytro Laktyushkin 			pipe_ctx->stream = stream;
14235d11e9fcSDmytro Laktyushkin 			return i;
14245d11e9fcSDmytro Laktyushkin 		}
14255d11e9fcSDmytro Laktyushkin 	}
14265d11e9fcSDmytro Laktyushkin 	return -1;
14275d11e9fcSDmytro Laktyushkin }
14285d11e9fcSDmytro Laktyushkin #endif
14295d11e9fcSDmytro Laktyushkin 
14304562236bSHarry Wentland enum dc_status resource_map_pool_resources(
14314562236bSHarry Wentland 		const struct core_dc *dc,
1432430ef426SDmytro Laktyushkin 		struct validate_context *context,
1433430ef426SDmytro Laktyushkin 		struct validate_context *old_context)
14344562236bSHarry Wentland {
1435a2b8659dSTony Cheng 	const struct resource_pool *pool = dc->res_pool;
1436ab2541b6SAric Cyr 	int i, j;
14374562236bSHarry Wentland 
1438430ef426SDmytro Laktyushkin 	calculate_phy_pix_clks(context);
14394562236bSHarry Wentland 
1440430ef426SDmytro Laktyushkin 	for (i = 0; old_context && i < context->stream_count; i++) {
1441ab2541b6SAric Cyr 		struct core_stream *stream = context->streams[i];
14424562236bSHarry Wentland 
1443430ef426SDmytro Laktyushkin 		if (!resource_is_stream_unchanged(old_context, stream)) {
1444430ef426SDmytro Laktyushkin 			if (stream != NULL && old_context->streams[i] != NULL) {
14454b679bc3SCharlene Liu 				stream->bit_depth_params =
1446430ef426SDmytro Laktyushkin 						old_context->streams[i]->bit_depth_params;
1447430ef426SDmytro Laktyushkin 				stream->clamping = old_context->streams[i]->clamping;
14484562236bSHarry Wentland 				continue;
14494b679bc3SCharlene Liu 			}
14504b679bc3SCharlene Liu 		}
14517e2fe319SCharlene Liu 
14524562236bSHarry Wentland 		/* mark resources used for stream that is already active */
1453a2b8659dSTony Cheng 		for (j = 0; j < pool->pipe_count; j++) {
14544562236bSHarry Wentland 			struct pipe_ctx *pipe_ctx =
1455ab2541b6SAric Cyr 				&context->res_ctx.pipe_ctx[j];
14564562236bSHarry Wentland 			const struct pipe_ctx *old_pipe_ctx =
1457430ef426SDmytro Laktyushkin 					&old_context->res_ctx.pipe_ctx[j];
14584562236bSHarry Wentland 
14594562236bSHarry Wentland 			if (!are_stream_backends_same(old_pipe_ctx->stream, stream))
14604562236bSHarry Wentland 				continue;
14614562236bSHarry Wentland 
1462268cadbdSYongqiang Sun 			if (old_pipe_ctx->top_pipe)
1463268cadbdSYongqiang Sun 				continue;
1464268cadbdSYongqiang Sun 
14654562236bSHarry Wentland 			pipe_ctx->stream = stream;
14664562236bSHarry Wentland 			copy_pipe_ctx(old_pipe_ctx, pipe_ctx);
14674562236bSHarry Wentland 
14688c737fccSYongqiang Sun 			/* Split pipe resource, do not acquire back end */
14698c737fccSYongqiang Sun 			if (!pipe_ctx->stream_enc)
14708c737fccSYongqiang Sun 				continue;
14718c737fccSYongqiang Sun 
14724562236bSHarry Wentland 			set_stream_engine_in_use(
1473a2b8659dSTony Cheng 				&context->res_ctx, pool,
14744562236bSHarry Wentland 				pipe_ctx->stream_enc);
14754562236bSHarry Wentland 
14764562236bSHarry Wentland 			/* Switch to dp clock source only if there is
14774562236bSHarry Wentland 			 * no non dp stream that shares the same timing
14784562236bSHarry Wentland 			 * with the dp stream.
14794562236bSHarry Wentland 			 */
14804562236bSHarry Wentland 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
14814562236bSHarry Wentland 				!find_pll_sharable_stream(stream, context))
1482a2b8659dSTony Cheng 				pipe_ctx->clock_source = pool->dp_clock_source;
14834562236bSHarry Wentland 
14844562236bSHarry Wentland 			resource_reference_clock_source(
1485a2b8659dSTony Cheng 				&context->res_ctx, pool,
14864562236bSHarry Wentland 				pipe_ctx->clock_source);
14874562236bSHarry Wentland 
1488a2b8659dSTony Cheng 			set_audio_in_use(&context->res_ctx, pool,
14894562236bSHarry Wentland 					pipe_ctx->audio);
14904562236bSHarry Wentland 		}
14914562236bSHarry Wentland 	}
14924562236bSHarry Wentland 
1493ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
1494ab2541b6SAric Cyr 		struct core_stream *stream = context->streams[i];
14954562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx = NULL;
14964562236bSHarry Wentland 		int pipe_idx = -1;
14974562236bSHarry Wentland 
1498430ef426SDmytro Laktyushkin 		if (old_context && resource_is_stream_unchanged(old_context, stream))
14994562236bSHarry Wentland 			continue;
15004562236bSHarry Wentland 		/* acquire new resources */
15015d11e9fcSDmytro Laktyushkin 		pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
15025d11e9fcSDmytro Laktyushkin #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
15035d11e9fcSDmytro Laktyushkin 		if (pipe_idx < 0)
15045d11e9fcSDmytro Laktyushkin 			acquire_first_split_pipe(&context->res_ctx, pool, stream);
15055d11e9fcSDmytro Laktyushkin #endif
15064562236bSHarry Wentland 		if (pipe_idx < 0)
15074562236bSHarry Wentland 			return DC_NO_CONTROLLER_RESOURCE;
15084562236bSHarry Wentland 
15094562236bSHarry Wentland 		pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
15104562236bSHarry Wentland 
15114562236bSHarry Wentland 		pipe_ctx->stream_enc =
15124562236bSHarry Wentland 			find_first_free_match_stream_enc_for_link(
1513a2b8659dSTony Cheng 				&context->res_ctx, pool, stream);
15144562236bSHarry Wentland 
15154562236bSHarry Wentland 		if (!pipe_ctx->stream_enc)
15164562236bSHarry Wentland 			return DC_NO_STREAM_ENG_RESOURCE;
15174562236bSHarry Wentland 
15184562236bSHarry Wentland 		set_stream_engine_in_use(
1519a2b8659dSTony Cheng 			&context->res_ctx, pool,
15204562236bSHarry Wentland 			pipe_ctx->stream_enc);
15214562236bSHarry Wentland 
15224562236bSHarry Wentland 		/* TODO: Add check if ASIC support and EDID audio */
15234a9a5d62SZeyu Fan 		if (!stream->sink->public.converter_disable_audio &&
15244562236bSHarry Wentland 			dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
15254562236bSHarry Wentland 			stream->public.audio_info.mode_count) {
15264562236bSHarry Wentland 			pipe_ctx->audio = find_first_free_audio(
1527a2b8659dSTony Cheng 				&context->res_ctx, pool);
15284562236bSHarry Wentland 
15294562236bSHarry Wentland 			/*
15304562236bSHarry Wentland 			 * Audio assigned in order first come first get.
15314562236bSHarry Wentland 			 * There are asics which has number of audio
15324562236bSHarry Wentland 			 * resources less then number of pipes
15334562236bSHarry Wentland 			 */
15344562236bSHarry Wentland 			if (pipe_ctx->audio)
15354562236bSHarry Wentland 				set_audio_in_use(
1536a2b8659dSTony Cheng 					&context->res_ctx, pool,
15374562236bSHarry Wentland 					pipe_ctx->audio);
15384562236bSHarry Wentland 		}
15394562236bSHarry Wentland 
1540ab2541b6SAric Cyr 		context->stream_status[i].primary_otg_inst = pipe_ctx->tg->inst;
15414562236bSHarry Wentland 	}
15424562236bSHarry Wentland 
15434562236bSHarry Wentland 	return DC_OK;
15444562236bSHarry Wentland }
15454562236bSHarry Wentland 
1546ab2541b6SAric Cyr /* first stream in the context is used to populate the rest */
1547ab2541b6SAric Cyr void validate_guaranteed_copy_streams(
15484562236bSHarry Wentland 		struct validate_context *context,
1549ab2541b6SAric Cyr 		int max_streams)
15504562236bSHarry Wentland {
15514562236bSHarry Wentland 	int i;
15524562236bSHarry Wentland 
1553ab2541b6SAric Cyr 	for (i = 1; i < max_streams; i++) {
1554ab2541b6SAric Cyr 		context->streams[i] = context->streams[0];
15554562236bSHarry Wentland 
15564562236bSHarry Wentland 		copy_pipe_ctx(&context->res_ctx.pipe_ctx[0],
15574562236bSHarry Wentland 			      &context->res_ctx.pipe_ctx[i]);
15584562236bSHarry Wentland 		context->res_ctx.pipe_ctx[i].stream =
15594562236bSHarry Wentland 				context->res_ctx.pipe_ctx[0].stream;
15604562236bSHarry Wentland 
1561ab2541b6SAric Cyr 		dc_stream_retain(&context->streams[i]->public);
1562ab2541b6SAric Cyr 		context->stream_count++;
15634562236bSHarry Wentland 	}
15644562236bSHarry Wentland }
15654562236bSHarry Wentland 
15666e4d6beeSTony Cheng static void patch_gamut_packet_checksum(
15676e4d6beeSTony Cheng 		struct encoder_info_packet *gamut_packet)
15684562236bSHarry Wentland {
15694562236bSHarry Wentland 	/* For gamut we recalc checksum */
15706e4d6beeSTony Cheng 	if (gamut_packet->valid) {
15714562236bSHarry Wentland 		uint8_t chk_sum = 0;
15724562236bSHarry Wentland 		uint8_t *ptr;
15734562236bSHarry Wentland 		uint8_t i;
15744562236bSHarry Wentland 
15754562236bSHarry Wentland 		/*start of the Gamut data. */
15766e4d6beeSTony Cheng 		ptr = &gamut_packet->sb[3];
15774562236bSHarry Wentland 
15786e4d6beeSTony Cheng 		for (i = 0; i <= gamut_packet->sb[1]; i++)
15794562236bSHarry Wentland 			chk_sum += ptr[i];
15804562236bSHarry Wentland 
15816e4d6beeSTony Cheng 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
15821646a6feSAndrew Wong 	}
15834562236bSHarry Wentland }
15844562236bSHarry Wentland 
15854562236bSHarry Wentland static void set_avi_info_frame(
15866e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
15874562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
15884562236bSHarry Wentland {
15894562236bSHarry Wentland 	struct core_stream *stream = pipe_ctx->stream;
15904562236bSHarry Wentland 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
15914562236bSHarry Wentland 	struct info_frame info_frame = { {0} };
15924562236bSHarry Wentland 	uint32_t pixel_encoding = 0;
15934562236bSHarry Wentland 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
15944562236bSHarry Wentland 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
15954562236bSHarry Wentland 	bool itc = false;
15964562236bSHarry Wentland 	uint8_t cn0_cn1 = 0;
15974562236bSHarry Wentland 	uint8_t *check_sum = NULL;
15984562236bSHarry Wentland 	uint8_t byte_index = 0;
1599e8d726b7SReza Amini 	union hdmi_info_packet *hdmi_info = &info_frame.avi_info_packet.info_packet_hdmi;
16004562236bSHarry Wentland 
16014562236bSHarry Wentland 	color_space = pipe_ctx->stream->public.output_color_space;
16024562236bSHarry Wentland 
16034562236bSHarry Wentland 	/* Initialize header */
1604e8d726b7SReza Amini 	hdmi_info->bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
16054562236bSHarry Wentland 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
16064562236bSHarry Wentland 	* not be used in HDMI 2.0 (Section 10.1) */
1607e8d726b7SReza Amini 	hdmi_info->bits.header.version = 2;
1608e8d726b7SReza Amini 	hdmi_info->bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
16094562236bSHarry Wentland 
16104562236bSHarry Wentland 	/*
16114562236bSHarry Wentland 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
16124562236bSHarry Wentland 	 * according to HDMI 2.0 spec (Section 10.1)
16134562236bSHarry Wentland 	 */
16144562236bSHarry Wentland 
16154562236bSHarry Wentland 	switch (stream->public.timing.pixel_encoding) {
16164562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR422:
16174562236bSHarry Wentland 		pixel_encoding = 1;
16184562236bSHarry Wentland 		break;
16194562236bSHarry Wentland 
16204562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR444:
16214562236bSHarry Wentland 		pixel_encoding = 2;
16224562236bSHarry Wentland 		break;
16234562236bSHarry Wentland 	case PIXEL_ENCODING_YCBCR420:
16244562236bSHarry Wentland 		pixel_encoding = 3;
16254562236bSHarry Wentland 		break;
16264562236bSHarry Wentland 
16274562236bSHarry Wentland 	case PIXEL_ENCODING_RGB:
16284562236bSHarry Wentland 	default:
16294562236bSHarry Wentland 		pixel_encoding = 0;
16304562236bSHarry Wentland 	}
16314562236bSHarry Wentland 
16324562236bSHarry Wentland 	/* Y0_Y1_Y2 : The pixel encoding */
16334562236bSHarry Wentland 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
1634e8d726b7SReza Amini 	hdmi_info->bits.Y0_Y1_Y2 = pixel_encoding;
16354562236bSHarry Wentland 
16364562236bSHarry Wentland 	/* A0 = 1 Active Format Information valid */
1637e8d726b7SReza Amini 	hdmi_info->bits.A0 = ACTIVE_FORMAT_VALID;
16384562236bSHarry Wentland 
16394562236bSHarry Wentland 	/* B0, B1 = 3; Bar info data is valid */
1640e8d726b7SReza Amini 	hdmi_info->bits.B0_B1 = BAR_INFO_BOTH_VALID;
16414562236bSHarry Wentland 
1642e8d726b7SReza Amini 	hdmi_info->bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
16434562236bSHarry Wentland 
16444562236bSHarry Wentland 	/* S0, S1 : Underscan / Overscan */
16454562236bSHarry Wentland 	/* TODO: un-hardcode scan type */
16464562236bSHarry Wentland 	scan_type = SCANNING_TYPE_UNDERSCAN;
1647e8d726b7SReza Amini 	hdmi_info->bits.S0_S1 = scan_type;
16484562236bSHarry Wentland 
16494562236bSHarry Wentland 	/* C0, C1 : Colorimetry */
16508fde5884SCharlene Liu 	if (color_space == COLOR_SPACE_YCBCR709 ||
16518fde5884SCharlene Liu 			color_space == COLOR_SPACE_YCBCR709_LIMITED)
1652e8d726b7SReza Amini 		hdmi_info->bits.C0_C1 = COLORIMETRY_ITU709;
16538fde5884SCharlene Liu 	else if (color_space == COLOR_SPACE_YCBCR601 ||
16548fde5884SCharlene Liu 			color_space == COLOR_SPACE_YCBCR601_LIMITED)
1655e8d726b7SReza Amini 		hdmi_info->bits.C0_C1 = COLORIMETRY_ITU601;
16568fde5884SCharlene Liu 	else {
16578fde5884SCharlene Liu 		if (stream->public.timing.pixel_encoding != PIXEL_ENCODING_RGB)
16588fde5884SCharlene Liu 			BREAK_TO_DEBUGGER();
1659e8d726b7SReza Amini 		hdmi_info->bits.C0_C1 = COLORIMETRY_NO_DATA;
16608fde5884SCharlene Liu 	}
1661534db198SAmy Zhang 	if (color_space == COLOR_SPACE_2020_RGB_FULLRANGE ||
1662534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE ||
1663534db198SAmy Zhang 			color_space == COLOR_SPACE_2020_YCBCR) {
1664e8d726b7SReza Amini 		hdmi_info->bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
1665e8d726b7SReza Amini 		hdmi_info->bits.C0_C1   = COLORIMETRY_EXTENDED;
1666534db198SAmy Zhang 	} else if (color_space == COLOR_SPACE_ADOBERGB) {
1667e8d726b7SReza Amini 		hdmi_info->bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
1668e8d726b7SReza Amini 		hdmi_info->bits.C0_C1   = COLORIMETRY_EXTENDED;
1669534db198SAmy Zhang 	}
1670534db198SAmy Zhang 
16714562236bSHarry Wentland 	/* TODO: un-hardcode aspect ratio */
16724562236bSHarry Wentland 	aspect = stream->public.timing.aspect_ratio;
16734562236bSHarry Wentland 
16744562236bSHarry Wentland 	switch (aspect) {
16754562236bSHarry Wentland 	case ASPECT_RATIO_4_3:
16764562236bSHarry Wentland 	case ASPECT_RATIO_16_9:
1677e8d726b7SReza Amini 		hdmi_info->bits.M0_M1 = aspect;
16784562236bSHarry Wentland 		break;
16794562236bSHarry Wentland 
16804562236bSHarry Wentland 	case ASPECT_RATIO_NO_DATA:
16814562236bSHarry Wentland 	case ASPECT_RATIO_64_27:
16824562236bSHarry Wentland 	case ASPECT_RATIO_256_135:
16834562236bSHarry Wentland 	default:
1684e8d726b7SReza Amini 		hdmi_info->bits.M0_M1 = 0;
16854562236bSHarry Wentland 	}
16864562236bSHarry Wentland 
16874562236bSHarry Wentland 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
1688e8d726b7SReza Amini 	hdmi_info->bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
16894562236bSHarry Wentland 
16904562236bSHarry Wentland 	/* TODO: un-hardcode cn0_cn1 and itc */
16914562236bSHarry Wentland 	cn0_cn1 = 0;
16924562236bSHarry Wentland 	itc = false;
16934562236bSHarry Wentland 
16944562236bSHarry Wentland 	if (itc) {
1695e8d726b7SReza Amini 		hdmi_info->bits.ITC     = 1;
1696e8d726b7SReza Amini 		hdmi_info->bits.CN0_CN1 = cn0_cn1;
16974562236bSHarry Wentland 	}
16984562236bSHarry Wentland 
16994562236bSHarry Wentland 	/* TODO : We should handle YCC quantization */
17004562236bSHarry Wentland 	/* but we do not have matrix calculation */
17014562236bSHarry Wentland 	if (color_space == COLOR_SPACE_SRGB) {
1702e8d726b7SReza Amini 		hdmi_info->bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
1703e8d726b7SReza Amini 		hdmi_info->bits.YQ0_YQ1 = YYC_QUANTIZATION_FULL_RANGE;
17044562236bSHarry Wentland 	} else if (color_space == COLOR_SPACE_SRGB_LIMITED) {
1705e8d726b7SReza Amini 		hdmi_info->bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
1706e8d726b7SReza Amini 		hdmi_info->bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
17074562236bSHarry Wentland 	} else {
1708e8d726b7SReza Amini 		hdmi_info->bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
1709e8d726b7SReza Amini 		hdmi_info->bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
17104562236bSHarry Wentland 	}
17114562236bSHarry Wentland 
1712e8d726b7SReza Amini 	hdmi_info->bits.VIC0_VIC7 =
17134562236bSHarry Wentland 					stream->public.timing.vic;
17144562236bSHarry Wentland 
17154562236bSHarry Wentland 	/* pixel repetition
17164562236bSHarry Wentland 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
17174562236bSHarry Wentland 	 * repetition start from 1 */
1718e8d726b7SReza Amini 	hdmi_info->bits.PR0_PR3 = 0;
17194562236bSHarry Wentland 
17204562236bSHarry Wentland 	/* Bar Info
17214562236bSHarry Wentland 	 * barTop:    Line Number of End of Top Bar.
17224562236bSHarry Wentland 	 * barBottom: Line Number of Start of Bottom Bar.
17234562236bSHarry Wentland 	 * barLeft:   Pixel Number of End of Left Bar.
17244562236bSHarry Wentland 	 * barRight:  Pixel Number of Start of Right Bar. */
1725e8d726b7SReza Amini 	hdmi_info->bits.bar_top = stream->public.timing.v_border_top;
1726e8d726b7SReza Amini 	hdmi_info->bits.bar_bottom = (stream->public.timing.v_border_top
17274562236bSHarry Wentland 			- stream->public.timing.v_border_bottom + 1);
1728e8d726b7SReza Amini 	hdmi_info->bits.bar_left  = stream->public.timing.h_border_left;
1729e8d726b7SReza Amini 	hdmi_info->bits.bar_right = (stream->public.timing.h_total
17304562236bSHarry Wentland 			- stream->public.timing.h_border_right + 1);
17314562236bSHarry Wentland 
17324562236bSHarry Wentland 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
1733e8d726b7SReza Amini 	check_sum = &info_frame.avi_info_packet.info_packet_hdmi.packet_raw_data.sb[0];
1734e8d726b7SReza Amini 
17353e183c5fSDave Airlie 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + HDMI_AVI_INFOFRAME_SIZE + 2;
17364562236bSHarry Wentland 
17373e183c5fSDave Airlie 	for (byte_index = 1; byte_index <= HDMI_AVI_INFOFRAME_SIZE; byte_index++)
1738e8d726b7SReza Amini 		*check_sum += hdmi_info->packet_raw_data.sb[byte_index];
17394562236bSHarry Wentland 
17404562236bSHarry Wentland 	/* one byte complement */
17414562236bSHarry Wentland 	*check_sum = (uint8_t) (0x100 - *check_sum);
17424562236bSHarry Wentland 
17434562236bSHarry Wentland 	/* Store in hw_path_mode */
1744e8d726b7SReza Amini 	info_packet->hb0 = hdmi_info->packet_raw_data.hb0;
1745e8d726b7SReza Amini 	info_packet->hb1 = hdmi_info->packet_raw_data.hb1;
1746e8d726b7SReza Amini 	info_packet->hb2 = hdmi_info->packet_raw_data.hb2;
17474562236bSHarry Wentland 
1748e66e4d64SHarry Wentland 	for (byte_index = 0; byte_index < sizeof(info_frame.avi_info_packet.
1749e66e4d64SHarry Wentland 				info_packet_hdmi.packet_raw_data.sb); byte_index++)
17504562236bSHarry Wentland 		info_packet->sb[byte_index] = info_frame.avi_info_packet.
17514562236bSHarry Wentland 				info_packet_hdmi.packet_raw_data.sb[byte_index];
17524562236bSHarry Wentland 
17534562236bSHarry Wentland 	info_packet->valid = true;
17544562236bSHarry Wentland }
17554562236bSHarry Wentland 
17566e4d6beeSTony Cheng static void set_vendor_info_packet(
17576e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
17586e4d6beeSTony Cheng 		struct core_stream *stream)
17594562236bSHarry Wentland {
17604562236bSHarry Wentland 	uint32_t length = 0;
17614562236bSHarry Wentland 	bool hdmi_vic_mode = false;
17624562236bSHarry Wentland 	uint8_t checksum = 0;
17634562236bSHarry Wentland 	uint32_t i = 0;
17644562236bSHarry Wentland 	enum dc_timing_3d_format format;
17654562236bSHarry Wentland 
17664562236bSHarry Wentland 	format = stream->public.timing.timing_3d_format;
17674562236bSHarry Wentland 
17684562236bSHarry Wentland 	/* Can be different depending on packet content */
17694562236bSHarry Wentland 	length = 5;
17704562236bSHarry Wentland 
17714562236bSHarry Wentland 	if (stream->public.timing.hdmi_vic != 0
17724562236bSHarry Wentland 			&& stream->public.timing.h_total >= 3840
17734562236bSHarry Wentland 			&& stream->public.timing.v_total >= 2160)
17744562236bSHarry Wentland 		hdmi_vic_mode = true;
17754562236bSHarry Wentland 
17764562236bSHarry Wentland 	/* According to HDMI 1.4a CTS, VSIF should be sent
17774562236bSHarry Wentland 	 * for both 3D stereo and HDMI VIC modes.
17784562236bSHarry Wentland 	 * For all other modes, there is no VSIF sent.  */
17794562236bSHarry Wentland 
17804562236bSHarry Wentland 	if (format == TIMING_3D_FORMAT_NONE && !hdmi_vic_mode)
17814562236bSHarry Wentland 		return;
17824562236bSHarry Wentland 
17834562236bSHarry Wentland 	/* 24bit IEEE Registration identifier (0x000c03). LSB first. */
17844562236bSHarry Wentland 	info_packet->sb[1] = 0x03;
17854562236bSHarry Wentland 	info_packet->sb[2] = 0x0C;
17864562236bSHarry Wentland 	info_packet->sb[3] = 0x00;
17874562236bSHarry Wentland 
17884562236bSHarry Wentland 	/*PB4: 5 lower bytes = 0 (reserved). 3 higher bits = HDMI_Video_Format.
17894562236bSHarry Wentland 	 * The value for HDMI_Video_Format are:
17904562236bSHarry Wentland 	 * 0x0 (0b000) - No additional HDMI video format is presented in this
17914562236bSHarry Wentland 	 * packet
17924562236bSHarry Wentland 	 * 0x1 (0b001) - Extended resolution format present. 1 byte of HDMI_VIC
17934562236bSHarry Wentland 	 * parameter follows
17944562236bSHarry Wentland 	 * 0x2 (0b010) - 3D format indication present. 3D_Structure and
17954562236bSHarry Wentland 	 * potentially 3D_Ext_Data follows
17964562236bSHarry Wentland 	 * 0x3..0x7 (0b011..0b111) - reserved for future use */
17974562236bSHarry Wentland 	if (format != TIMING_3D_FORMAT_NONE)
17984562236bSHarry Wentland 		info_packet->sb[4] = (2 << 5);
17994562236bSHarry Wentland 	else if (hdmi_vic_mode)
18004562236bSHarry Wentland 		info_packet->sb[4] = (1 << 5);
18014562236bSHarry Wentland 
18024562236bSHarry Wentland 	/* PB5: If PB4 claims 3D timing (HDMI_Video_Format = 0x2):
18034562236bSHarry Wentland 	 * 4 lower bites = 0 (reserved). 4 higher bits = 3D_Structure.
18044562236bSHarry Wentland 	 * The value for 3D_Structure are:
18054562236bSHarry Wentland 	 * 0x0 - Frame Packing
18064562236bSHarry Wentland 	 * 0x1 - Field Alternative
18074562236bSHarry Wentland 	 * 0x2 - Line Alternative
18084562236bSHarry Wentland 	 * 0x3 - Side-by-Side (full)
18094562236bSHarry Wentland 	 * 0x4 - L + depth
18104562236bSHarry Wentland 	 * 0x5 - L + depth + graphics + graphics-depth
18114562236bSHarry Wentland 	 * 0x6 - Top-and-Bottom
18124562236bSHarry Wentland 	 * 0x7 - Reserved for future use
18134562236bSHarry Wentland 	 * 0x8 - Side-by-Side (Half)
18144562236bSHarry Wentland 	 * 0x9..0xE - Reserved for future use
18154562236bSHarry Wentland 	 * 0xF - Not used */
18164562236bSHarry Wentland 	switch (format) {
18174562236bSHarry Wentland 	case TIMING_3D_FORMAT_HW_FRAME_PACKING:
18184562236bSHarry Wentland 	case TIMING_3D_FORMAT_SW_FRAME_PACKING:
18194562236bSHarry Wentland 		info_packet->sb[5] = (0x0 << 4);
18204562236bSHarry Wentland 		break;
18214562236bSHarry Wentland 
18224562236bSHarry Wentland 	case TIMING_3D_FORMAT_SIDE_BY_SIDE:
18234562236bSHarry Wentland 	case TIMING_3D_FORMAT_SBS_SW_PACKED:
18244562236bSHarry Wentland 		info_packet->sb[5] = (0x8 << 4);
18254562236bSHarry Wentland 		length = 6;
18264562236bSHarry Wentland 		break;
18274562236bSHarry Wentland 
18284562236bSHarry Wentland 	case TIMING_3D_FORMAT_TOP_AND_BOTTOM:
18294562236bSHarry Wentland 	case TIMING_3D_FORMAT_TB_SW_PACKED:
18304562236bSHarry Wentland 		info_packet->sb[5] = (0x6 << 4);
18314562236bSHarry Wentland 		break;
18324562236bSHarry Wentland 
18334562236bSHarry Wentland 	default:
18344562236bSHarry Wentland 		break;
18354562236bSHarry Wentland 	}
18364562236bSHarry Wentland 
18374562236bSHarry Wentland 	/*PB5: If PB4 is set to 0x1 (extended resolution format)
18384562236bSHarry Wentland 	 * fill PB5 with the correct HDMI VIC code */
18394562236bSHarry Wentland 	if (hdmi_vic_mode)
18404562236bSHarry Wentland 		info_packet->sb[5] = stream->public.timing.hdmi_vic;
18414562236bSHarry Wentland 
18424562236bSHarry Wentland 	/* Header */
18433e183c5fSDave Airlie 	info_packet->hb0 = HDMI_INFOFRAME_TYPE_VENDOR; /* VSIF packet type. */
18444562236bSHarry Wentland 	info_packet->hb1 = 0x01; /* Version */
18454562236bSHarry Wentland 
18464562236bSHarry Wentland 	/* 4 lower bits = Length, 4 higher bits = 0 (reserved) */
18474562236bSHarry Wentland 	info_packet->hb2 = (uint8_t) (length);
18484562236bSHarry Wentland 
18494562236bSHarry Wentland 	/* Calculate checksum */
18504562236bSHarry Wentland 	checksum = 0;
18514562236bSHarry Wentland 	checksum += info_packet->hb0;
18524562236bSHarry Wentland 	checksum += info_packet->hb1;
18534562236bSHarry Wentland 	checksum += info_packet->hb2;
18544562236bSHarry Wentland 
18554562236bSHarry Wentland 	for (i = 1; i <= length; i++)
18564562236bSHarry Wentland 		checksum += info_packet->sb[i];
18574562236bSHarry Wentland 
18584562236bSHarry Wentland 	info_packet->sb[0] = (uint8_t) (0x100 - checksum);
18594562236bSHarry Wentland 
18604562236bSHarry Wentland 	info_packet->valid = true;
18614562236bSHarry Wentland }
18624562236bSHarry Wentland 
18636e4d6beeSTony Cheng static void set_spd_info_packet(
18646e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
18656e4d6beeSTony Cheng 		struct core_stream *stream)
18664562236bSHarry Wentland {
18674562236bSHarry Wentland 	/* SPD info packet for FreeSync */
18684562236bSHarry Wentland 
18694562236bSHarry Wentland 	unsigned char checksum = 0;
18704562236bSHarry Wentland 	unsigned int idx, payload_size = 0;
18714562236bSHarry Wentland 
18724562236bSHarry Wentland 	/* Check if Freesync is supported. Return if false. If true,
18734562236bSHarry Wentland 	 * set the corresponding bit in the info packet
18744562236bSHarry Wentland 	 */
18754562236bSHarry Wentland 	if (stream->public.freesync_ctx.supported == false)
18764562236bSHarry Wentland 		return;
18774562236bSHarry Wentland 
18784562236bSHarry Wentland 	if (dc_is_hdmi_signal(stream->signal)) {
18794562236bSHarry Wentland 
18804562236bSHarry Wentland 		/* HEADER */
18814562236bSHarry Wentland 
18824562236bSHarry Wentland 		/* HB0  = Packet Type = 0x83 (Source Product
18834562236bSHarry Wentland 		 *	  Descriptor InfoFrame)
18844562236bSHarry Wentland 		 */
18853e183c5fSDave Airlie 		info_packet->hb0 = HDMI_INFOFRAME_TYPE_SPD;
18864562236bSHarry Wentland 
18874562236bSHarry Wentland 		/* HB1  = Version = 0x01 */
18884562236bSHarry Wentland 		info_packet->hb1 = 0x01;
18894562236bSHarry Wentland 
18904562236bSHarry Wentland 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x08] */
18914562236bSHarry Wentland 		info_packet->hb2 = 0x08;
18924562236bSHarry Wentland 
18934562236bSHarry Wentland 		payload_size = 0x08;
18944562236bSHarry Wentland 
18954562236bSHarry Wentland 	} else if (dc_is_dp_signal(stream->signal)) {
18964562236bSHarry Wentland 
18974562236bSHarry Wentland 		/* HEADER */
18984562236bSHarry Wentland 
18994562236bSHarry Wentland 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
19004562236bSHarry Wentland 		 *	  when used to associate audio related info packets
19014562236bSHarry Wentland 		 */
19024562236bSHarry Wentland 		info_packet->hb0 = 0x00;
19034562236bSHarry Wentland 
19044562236bSHarry Wentland 		/* HB1  = Packet Type = 0x83 (Source Product
19054562236bSHarry Wentland 		 *	  Descriptor InfoFrame)
19064562236bSHarry Wentland 		 */
19073e183c5fSDave Airlie 		info_packet->hb1 = HDMI_INFOFRAME_TYPE_SPD;
19084562236bSHarry Wentland 
19094562236bSHarry Wentland 		/* HB2  = [Bits 7:0 = Least significant eight bits -
19104562236bSHarry Wentland 		 *	  For INFOFRAME, the value must be 1Bh]
19114562236bSHarry Wentland 		 */
19124562236bSHarry Wentland 		info_packet->hb2 = 0x1B;
19134562236bSHarry Wentland 
19144562236bSHarry Wentland 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x1]
19154562236bSHarry Wentland 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
19164562236bSHarry Wentland 		 */
19174562236bSHarry Wentland 		info_packet->hb3 = 0x04;
19184562236bSHarry Wentland 
19194562236bSHarry Wentland 		payload_size = 0x1B;
19204562236bSHarry Wentland 	}
19214562236bSHarry Wentland 
19224562236bSHarry Wentland 	/* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
19234562236bSHarry Wentland 	info_packet->sb[1] = 0x1A;
19244562236bSHarry Wentland 
19254562236bSHarry Wentland 	/* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
19264562236bSHarry Wentland 	info_packet->sb[2] = 0x00;
19274562236bSHarry Wentland 
19284562236bSHarry Wentland 	/* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
19294562236bSHarry Wentland 	info_packet->sb[3] = 0x00;
19304562236bSHarry Wentland 
19314562236bSHarry Wentland 	/* PB4 = Reserved */
19324562236bSHarry Wentland 	info_packet->sb[4] = 0x00;
19334562236bSHarry Wentland 
19344562236bSHarry Wentland 	/* PB5 = Reserved */
19354562236bSHarry Wentland 	info_packet->sb[5] = 0x00;
19364562236bSHarry Wentland 
19374562236bSHarry Wentland 	/* PB6 = [Bits 7:3 = Reserved] */
19384562236bSHarry Wentland 	info_packet->sb[6] = 0x00;
19394562236bSHarry Wentland 
19404562236bSHarry Wentland 	if (stream->public.freesync_ctx.supported == true)
19414562236bSHarry Wentland 		/* PB6 = [Bit 0 = FreeSync Supported] */
19424562236bSHarry Wentland 		info_packet->sb[6] |= 0x01;
19434562236bSHarry Wentland 
19444562236bSHarry Wentland 	if (stream->public.freesync_ctx.enabled == true)
19454562236bSHarry Wentland 		/* PB6 = [Bit 1 = FreeSync Enabled] */
19464562236bSHarry Wentland 		info_packet->sb[6] |= 0x02;
19474562236bSHarry Wentland 
19484562236bSHarry Wentland 	if (stream->public.freesync_ctx.active == true)
19494562236bSHarry Wentland 		/* PB6 = [Bit 2 = FreeSync Active] */
19504562236bSHarry Wentland 		info_packet->sb[6] |= 0x04;
19514562236bSHarry Wentland 
19524562236bSHarry Wentland 	/* PB7 = FreeSync Minimum refresh rate (Hz) */
19534562236bSHarry Wentland 	info_packet->sb[7] = (unsigned char) (stream->public.freesync_ctx.
19544562236bSHarry Wentland 			min_refresh_in_micro_hz / 1000000);
19554562236bSHarry Wentland 
19564562236bSHarry Wentland 	/* PB8 = FreeSync Maximum refresh rate (Hz)
19574562236bSHarry Wentland 	 *
19584562236bSHarry Wentland 	 * Note: We do not use the maximum capable refresh rate
19594562236bSHarry Wentland 	 * of the panel, because we should never go above the field
19604562236bSHarry Wentland 	 * rate of the mode timing set.
19614562236bSHarry Wentland 	 */
19624562236bSHarry Wentland 	info_packet->sb[8] = (unsigned char) (stream->public.freesync_ctx.
19634562236bSHarry Wentland 			nominal_refresh_in_micro_hz / 1000000);
19644562236bSHarry Wentland 
19654562236bSHarry Wentland 	/* PB9 - PB27  = Reserved */
19664562236bSHarry Wentland 	for (idx = 9; idx <= 27; idx++)
19674562236bSHarry Wentland 		info_packet->sb[idx] = 0x00;
19684562236bSHarry Wentland 
19694562236bSHarry Wentland 	/* Calculate checksum */
19704562236bSHarry Wentland 	checksum += info_packet->hb0;
19714562236bSHarry Wentland 	checksum += info_packet->hb1;
19724562236bSHarry Wentland 	checksum += info_packet->hb2;
19734562236bSHarry Wentland 	checksum += info_packet->hb3;
19744562236bSHarry Wentland 
19754562236bSHarry Wentland 	for (idx = 1; idx <= payload_size; idx++)
19764562236bSHarry Wentland 		checksum += info_packet->sb[idx];
19774562236bSHarry Wentland 
19784562236bSHarry Wentland 	/* PB0 = Checksum (one byte complement) */
19794562236bSHarry Wentland 	info_packet->sb[0] = (unsigned char) (0x100 - checksum);
19804562236bSHarry Wentland 
19814562236bSHarry Wentland 	info_packet->valid = true;
19824562236bSHarry Wentland }
19834562236bSHarry Wentland 
19841646a6feSAndrew Wong static void set_hdr_static_info_packet(
19856e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
19861646a6feSAndrew Wong 		struct core_surface *surface,
19876e4d6beeSTony Cheng 		struct core_stream *stream)
19881646a6feSAndrew Wong {
1989e5cf325bSHarry Wentland 	uint16_t i = 0;
19901646a6feSAndrew Wong 	enum signal_type signal = stream->signal;
1991e5cf325bSHarry Wentland 	struct dc_hdr_static_metadata hdr_metadata;
1992e5cf325bSHarry Wentland 	uint32_t data;
19931646a6feSAndrew Wong 
19941646a6feSAndrew Wong 	if (!surface)
19951646a6feSAndrew Wong 		return;
19961646a6feSAndrew Wong 
1997e5cf325bSHarry Wentland 	hdr_metadata = surface->public.hdr_static_ctx;
19981646a6feSAndrew Wong 
199970063a59SAmy Zhang 	if (!hdr_metadata.hdr_supported)
200010bff005SYongqiang Sun 		return;
200110bff005SYongqiang Sun 
20021646a6feSAndrew Wong 	if (dc_is_hdmi_signal(signal)) {
20031646a6feSAndrew Wong 		info_packet->valid = true;
20041646a6feSAndrew Wong 
20051646a6feSAndrew Wong 		info_packet->hb0 = 0x87;
20061646a6feSAndrew Wong 		info_packet->hb1 = 0x01;
20071646a6feSAndrew Wong 		info_packet->hb2 = 0x1A;
20081646a6feSAndrew Wong 		i = 1;
20091646a6feSAndrew Wong 	} else if (dc_is_dp_signal(signal)) {
20101646a6feSAndrew Wong 		info_packet->valid = true;
20111646a6feSAndrew Wong 
20121646a6feSAndrew Wong 		info_packet->hb0 = 0x00;
20131646a6feSAndrew Wong 		info_packet->hb1 = 0x87;
20141646a6feSAndrew Wong 		info_packet->hb2 = 0x1D;
20151646a6feSAndrew Wong 		info_packet->hb3 = (0x13 << 2);
20161646a6feSAndrew Wong 		i = 2;
20171646a6feSAndrew Wong 	}
20181646a6feSAndrew Wong 
20191646a6feSAndrew Wong 	data = hdr_metadata.is_hdr;
20201646a6feSAndrew Wong 	info_packet->sb[i++] = data ? 0x02 : 0x00;
20211646a6feSAndrew Wong 	info_packet->sb[i++] = 0x00;
20221646a6feSAndrew Wong 
20231646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_green_x / 2;
20241646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20251646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20261646a6feSAndrew Wong 
20271646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_green_y / 2;
20281646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20291646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20301646a6feSAndrew Wong 
20311646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_blue_x / 2;
20321646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20331646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20341646a6feSAndrew Wong 
20351646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_blue_y / 2;
20361646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20371646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20381646a6feSAndrew Wong 
20391646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_red_x / 2;
20401646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20411646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20421646a6feSAndrew Wong 
20431646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_red_y / 2;
20441646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20451646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20461646a6feSAndrew Wong 
20471646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_white_point_x / 2;
20481646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20491646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20501646a6feSAndrew Wong 
20511646a6feSAndrew Wong 	data = hdr_metadata.chromaticity_white_point_y / 2;
20521646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20531646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20541646a6feSAndrew Wong 
20551646a6feSAndrew Wong 	data = hdr_metadata.max_luminance;
20561646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20571646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20581646a6feSAndrew Wong 
20591646a6feSAndrew Wong 	data = hdr_metadata.min_luminance;
20601646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20611646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20621646a6feSAndrew Wong 
20631646a6feSAndrew Wong 	data = hdr_metadata.maximum_content_light_level;
20641646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20651646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20661646a6feSAndrew Wong 
20671646a6feSAndrew Wong 	data = hdr_metadata.maximum_frame_average_light_level;
20681646a6feSAndrew Wong 	info_packet->sb[i++] = data & 0xFF;
20691646a6feSAndrew Wong 	info_packet->sb[i++] = (data & 0xFF00) >> 8;
20701646a6feSAndrew Wong 
20711646a6feSAndrew Wong 	if (dc_is_hdmi_signal(signal)) {
20721646a6feSAndrew Wong 		uint32_t checksum = 0;
20731646a6feSAndrew Wong 
20741646a6feSAndrew Wong 		checksum += info_packet->hb0;
20751646a6feSAndrew Wong 		checksum += info_packet->hb1;
20761646a6feSAndrew Wong 		checksum += info_packet->hb2;
20771646a6feSAndrew Wong 
20781646a6feSAndrew Wong 		for (i = 1; i <= info_packet->hb2; i++)
20791646a6feSAndrew Wong 			checksum += info_packet->sb[i];
20801646a6feSAndrew Wong 
20811646a6feSAndrew Wong 		info_packet->sb[0] = 0x100 - checksum;
20821646a6feSAndrew Wong 	} else if (dc_is_dp_signal(signal)) {
20831646a6feSAndrew Wong 		info_packet->sb[0] = 0x01;
20841646a6feSAndrew Wong 		info_packet->sb[1] = 0x1A;
20851646a6feSAndrew Wong 	}
20861646a6feSAndrew Wong }
20871646a6feSAndrew Wong 
20886e4d6beeSTony Cheng static void set_vsc_info_packet(
20896e4d6beeSTony Cheng 		struct encoder_info_packet *info_packet,
20906e4d6beeSTony Cheng 		struct core_stream *stream)
20914562236bSHarry Wentland {
20924562236bSHarry Wentland 	unsigned int vscPacketRevision = 0;
20934562236bSHarry Wentland 	unsigned int i;
20944562236bSHarry Wentland 
209594267b3dSSylvia Tsai 	if (stream->sink->link->psr_enabled) {
20964562236bSHarry Wentland 		vscPacketRevision = 2;
20974562236bSHarry Wentland 	}
20984562236bSHarry Wentland 
20994562236bSHarry Wentland 	/* VSC packet not needed based on the features
21004562236bSHarry Wentland 	 * supported by this DP display
21014562236bSHarry Wentland 	 */
21024562236bSHarry Wentland 	if (vscPacketRevision == 0)
21034562236bSHarry Wentland 		return;
21044562236bSHarry Wentland 
21054562236bSHarry Wentland 	if (vscPacketRevision == 0x2) {
21064562236bSHarry Wentland 		/* Secondary-data Packet ID = 0*/
21074562236bSHarry Wentland 		info_packet->hb0 = 0x00;
21084562236bSHarry Wentland 		/* 07h - Packet Type Value indicating Video
21094562236bSHarry Wentland 		 * Stream Configuration packet
21104562236bSHarry Wentland 		 */
21114562236bSHarry Wentland 		info_packet->hb1 = 0x07;
21124562236bSHarry Wentland 		/* 02h = VSC SDP supporting 3D stereo and PSR
21134562236bSHarry Wentland 		 * (applies to eDP v1.3 or higher).
21144562236bSHarry Wentland 		 */
21154562236bSHarry Wentland 		info_packet->hb2 = 0x02;
21164562236bSHarry Wentland 		/* 08h = VSC packet supporting 3D stereo + PSR
21174562236bSHarry Wentland 		 * (HB2 = 02h).
21184562236bSHarry Wentland 		 */
21194562236bSHarry Wentland 		info_packet->hb3 = 0x08;
21204562236bSHarry Wentland 
21214562236bSHarry Wentland 		for (i = 0; i < 28; i++)
21224562236bSHarry Wentland 			info_packet->sb[i] = 0;
21234562236bSHarry Wentland 
21244562236bSHarry Wentland 		info_packet->valid = true;
21254562236bSHarry Wentland 	}
21264562236bSHarry Wentland 
21274562236bSHarry Wentland 	/*TODO: stereo 3D support and extend pixel encoding colorimetry*/
21284562236bSHarry Wentland }
21294562236bSHarry Wentland 
21308122a253SHarry Wentland void dc_resource_validate_ctx_destruct(struct validate_context *context)
21314562236bSHarry Wentland {
21324562236bSHarry Wentland 	int i, j;
21334562236bSHarry Wentland 
2134ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
2135ab2541b6SAric Cyr 		for (j = 0; j < context->stream_status[i].surface_count; j++)
21364562236bSHarry Wentland 			dc_surface_release(
2137ab2541b6SAric Cyr 				context->stream_status[i].surfaces[j]);
21384562236bSHarry Wentland 
2139ab2541b6SAric Cyr 		context->stream_status[i].surface_count = 0;
2140ab2541b6SAric Cyr 		dc_stream_release(&context->streams[i]->public);
2141ab2541b6SAric Cyr 		context->streams[i] = NULL;
21424562236bSHarry Wentland 	}
21434562236bSHarry Wentland }
21444562236bSHarry Wentland 
21454562236bSHarry Wentland /*
2146ab2541b6SAric Cyr  * Copy src_ctx into dst_ctx and retain all surfaces and streams referenced
21474562236bSHarry Wentland  * by the src_ctx
21484562236bSHarry Wentland  */
21498122a253SHarry Wentland void dc_resource_validate_ctx_copy_construct(
21504562236bSHarry Wentland 		const struct validate_context *src_ctx,
21514562236bSHarry Wentland 		struct validate_context *dst_ctx)
21524562236bSHarry Wentland {
21534562236bSHarry Wentland 	int i, j;
21544562236bSHarry Wentland 
21554562236bSHarry Wentland 	*dst_ctx = *src_ctx;
21564562236bSHarry Wentland 
2157a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
21584562236bSHarry Wentland 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
21594562236bSHarry Wentland 
21604562236bSHarry Wentland 		if (cur_pipe->top_pipe)
21614562236bSHarry Wentland 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
21624562236bSHarry Wentland 
21634562236bSHarry Wentland 		if (cur_pipe->bottom_pipe)
21644562236bSHarry Wentland 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
21654562236bSHarry Wentland 
21664562236bSHarry Wentland 	}
21674562236bSHarry Wentland 
2168ab2541b6SAric Cyr 	for (i = 0; i < dst_ctx->stream_count; i++) {
2169ab2541b6SAric Cyr 		dc_stream_retain(&dst_ctx->streams[i]->public);
2170ab2541b6SAric Cyr 		for (j = 0; j < dst_ctx->stream_status[i].surface_count; j++)
21714562236bSHarry Wentland 			dc_surface_retain(
2172ab2541b6SAric Cyr 				dst_ctx->stream_status[i].surfaces[j]);
21734562236bSHarry Wentland 	}
21744562236bSHarry Wentland }
21754562236bSHarry Wentland 
21764562236bSHarry Wentland struct clock_source *dc_resource_find_first_free_pll(
2177a2b8659dSTony Cheng 		struct resource_context *res_ctx,
2178a2b8659dSTony Cheng 		const struct resource_pool *pool)
21794562236bSHarry Wentland {
21804562236bSHarry Wentland 	int i;
21814562236bSHarry Wentland 
2182a2b8659dSTony Cheng 	for (i = 0; i < pool->clk_src_count; ++i) {
21834562236bSHarry Wentland 		if (res_ctx->clock_source_ref_count[i] == 0)
2184a2b8659dSTony Cheng 			return pool->clock_sources[i];
21854562236bSHarry Wentland 	}
21864562236bSHarry Wentland 
21874562236bSHarry Wentland 	return NULL;
21884562236bSHarry Wentland }
21894562236bSHarry Wentland 
21904562236bSHarry Wentland void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
21914562236bSHarry Wentland {
21924562236bSHarry Wentland 	enum signal_type signal = SIGNAL_TYPE_NONE;
21936e4d6beeSTony Cheng 	struct encoder_info_frame *info = &pipe_ctx->encoder_info_frame;
21944562236bSHarry Wentland 
21954562236bSHarry Wentland 	/* default all packets to invalid */
21966e4d6beeSTony Cheng 	info->avi.valid = false;
21976e4d6beeSTony Cheng 	info->gamut.valid = false;
21986e4d6beeSTony Cheng 	info->vendor.valid = false;
2199630e3573SJeff Smith 	info->spd.valid = false;
22006e4d6beeSTony Cheng 	info->hdrsmd.valid = false;
22016e4d6beeSTony Cheng 	info->vsc.valid = false;
22024562236bSHarry Wentland 
22034562236bSHarry Wentland 	signal = pipe_ctx->stream->signal;
22044562236bSHarry Wentland 
22054562236bSHarry Wentland 	/* HDMi and DP have different info packets*/
22064562236bSHarry Wentland 	if (dc_is_hdmi_signal(signal)) {
22076e4d6beeSTony Cheng 		set_avi_info_frame(&info->avi, pipe_ctx);
22086e4d6beeSTony Cheng 
22096e4d6beeSTony Cheng 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
22106e4d6beeSTony Cheng 
22116e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
22126e4d6beeSTony Cheng 
22136e4d6beeSTony Cheng 		set_hdr_static_info_packet(&info->hdrsmd,
22146e4d6beeSTony Cheng 				pipe_ctx->surface, pipe_ctx->stream);
22156e4d6beeSTony Cheng 
2216a33fa99dSHarry Wentland 	} else if (dc_is_dp_signal(signal)) {
22176e4d6beeSTony Cheng 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
22186e4d6beeSTony Cheng 
22196e4d6beeSTony Cheng 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
22206e4d6beeSTony Cheng 
22216e4d6beeSTony Cheng 		set_hdr_static_info_packet(&info->hdrsmd,
22226e4d6beeSTony Cheng 				pipe_ctx->surface, pipe_ctx->stream);
2223a33fa99dSHarry Wentland 	}
22244562236bSHarry Wentland 
22256e4d6beeSTony Cheng 	patch_gamut_packet_checksum(&info->gamut);
22264562236bSHarry Wentland }
22274562236bSHarry Wentland 
22284562236bSHarry Wentland enum dc_status resource_map_clock_resources(
22294562236bSHarry Wentland 		const struct core_dc *dc,
2230430ef426SDmytro Laktyushkin 		struct validate_context *context,
2231430ef426SDmytro Laktyushkin 		struct validate_context *old_context)
22324562236bSHarry Wentland {
2233ab2541b6SAric Cyr 	int i, j;
2234a2b8659dSTony Cheng 	const struct resource_pool *pool = dc->res_pool;
22354562236bSHarry Wentland 
22364562236bSHarry Wentland 	/* acquire new resources */
2237ab2541b6SAric Cyr 	for (i = 0; i < context->stream_count; i++) {
2238ab2541b6SAric Cyr 		const struct core_stream *stream = context->streams[i];
22394562236bSHarry Wentland 
2240430ef426SDmytro Laktyushkin 		if (old_context && resource_is_stream_unchanged(old_context, stream))
22414562236bSHarry Wentland 			continue;
22424562236bSHarry Wentland 
2243ab2541b6SAric Cyr 		for (j = 0; j < MAX_PIPES; j++) {
22444562236bSHarry Wentland 			struct pipe_ctx *pipe_ctx =
2245ab2541b6SAric Cyr 				&context->res_ctx.pipe_ctx[j];
22464562236bSHarry Wentland 
2247ab2541b6SAric Cyr 			if (context->res_ctx.pipe_ctx[j].stream != stream)
22484562236bSHarry Wentland 				continue;
22494562236bSHarry Wentland 
22504562236bSHarry Wentland 			if (dc_is_dp_signal(pipe_ctx->stream->signal)
22514562236bSHarry Wentland 				|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
2252a2b8659dSTony Cheng 				pipe_ctx->clock_source = pool->dp_clock_source;
22534562236bSHarry Wentland 			else {
22544562236bSHarry Wentland 				pipe_ctx->clock_source = NULL;
22554562236bSHarry Wentland 
22564562236bSHarry Wentland 				if (!dc->public.config.disable_disp_pll_sharing)
22574562236bSHarry Wentland 					resource_find_used_clk_src_for_sharing(
22584562236bSHarry Wentland 						&context->res_ctx,
22594562236bSHarry Wentland 						pipe_ctx);
22604562236bSHarry Wentland 
22614562236bSHarry Wentland 				if (pipe_ctx->clock_source == NULL)
22624562236bSHarry Wentland 					pipe_ctx->clock_source =
2263a2b8659dSTony Cheng 						dc_resource_find_first_free_pll(
2264a2b8659dSTony Cheng 							&context->res_ctx,
2265a2b8659dSTony Cheng 							pool);
22664562236bSHarry Wentland 			}
22674562236bSHarry Wentland 
22684562236bSHarry Wentland 			if (pipe_ctx->clock_source == NULL)
22694562236bSHarry Wentland 				return DC_NO_CLOCK_SOURCE_RESOURCE;
22704562236bSHarry Wentland 
22714562236bSHarry Wentland 			resource_reference_clock_source(
2272a2b8659dSTony Cheng 				&context->res_ctx, pool,
22734562236bSHarry Wentland 				pipe_ctx->clock_source);
22744562236bSHarry Wentland 
22754562236bSHarry Wentland 			/* only one cs per stream regardless of mpo */
22764562236bSHarry Wentland 			break;
22774562236bSHarry Wentland 		}
22784562236bSHarry Wentland 	}
22794562236bSHarry Wentland 
22804562236bSHarry Wentland 	return DC_OK;
22814562236bSHarry Wentland }
22824562236bSHarry Wentland 
22834562236bSHarry Wentland /*
22844562236bSHarry Wentland  * Note: We need to disable output if clock sources change,
22854562236bSHarry Wentland  * since bios does optimization and doesn't apply if changing
22864562236bSHarry Wentland  * PHY when not already disabled.
22874562236bSHarry Wentland  */
22884562236bSHarry Wentland bool pipe_need_reprogram(
22894562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx_old,
22904562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
22914562236bSHarry Wentland {
22924562236bSHarry Wentland 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
22934562236bSHarry Wentland 		return true;
22944562236bSHarry Wentland 
22954562236bSHarry Wentland 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
22964562236bSHarry Wentland 		return true;
22974562236bSHarry Wentland 
22984562236bSHarry Wentland 	if (pipe_ctx_old->audio != pipe_ctx->audio)
22994562236bSHarry Wentland 		return true;
23004562236bSHarry Wentland 
23014562236bSHarry Wentland 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
23024562236bSHarry Wentland 			&& pipe_ctx_old->stream != pipe_ctx->stream)
23034562236bSHarry Wentland 		return true;
23044562236bSHarry Wentland 
23054562236bSHarry Wentland 	if (pipe_ctx_old->stream_enc != pipe_ctx->stream_enc)
23064562236bSHarry Wentland 		return true;
23074562236bSHarry Wentland 
23084562236bSHarry Wentland 	if (is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
23094562236bSHarry Wentland 		return true;
23104562236bSHarry Wentland 
23114562236bSHarry Wentland 
23124562236bSHarry Wentland 	return false;
23134562236bSHarry Wentland }
2314529cad0fSDing Wang 
2315529cad0fSDing Wang void resource_build_bit_depth_reduction_params(const struct core_stream *stream,
2316529cad0fSDing Wang 		struct bit_depth_reduction_params *fmt_bit_depth)
2317529cad0fSDing Wang {
2318529cad0fSDing Wang 	enum dc_dither_option option = stream->public.dither_option;
2319529cad0fSDing Wang 	enum dc_pixel_encoding pixel_encoding =
2320529cad0fSDing Wang 			stream->public.timing.pixel_encoding;
2321529cad0fSDing Wang 
2322529cad0fSDing Wang 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
2323529cad0fSDing Wang 
2324529cad0fSDing Wang 	if (option == DITHER_OPTION_DISABLE)
2325529cad0fSDing Wang 		return;
2326529cad0fSDing Wang 
2327529cad0fSDing Wang 	if (option == DITHER_OPTION_TRUN6) {
2328529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2329529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
2330529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN8 ||
2331529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
2332529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6) {
2333529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2334529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
2335529cad0fSDing Wang 	} else if (option == DITHER_OPTION_TRUN10        ||
2336529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
2337529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
2338529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8     ||
2339529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2340529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2341529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2342529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2343529cad0fSDing Wang 	}
2344529cad0fSDing Wang 
2345529cad0fSDing Wang 	/* special case - Formatter can only reduce by 4 bits at most.
2346529cad0fSDing Wang 	 * When reducing from 12 to 6 bits,
2347529cad0fSDing Wang 	 * HW recommends we use trunc with round mode
2348529cad0fSDing Wang 	 * (if we did nothing, trunc to 10 bits would be used)
2349529cad0fSDing Wang 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
2350529cad0fSDing Wang 	 * as the input was 10 bits.
2351529cad0fSDing Wang 	 */
2352529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2353529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2354529cad0fSDing Wang 			option == DITHER_OPTION_FM6) {
2355529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2356529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2357529cad0fSDing Wang 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
2358529cad0fSDing Wang 	}
2359529cad0fSDing Wang 
2360529cad0fSDing Wang 	/* spatial dither
2361529cad0fSDing Wang 	 * note that spatial modes 1-3 are never used
2362529cad0fSDing Wang 	 */
2363529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
2364529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL6 ||
2365529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
2366529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
2367529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2368529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
2369529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2370529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2371529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2372529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
2373529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2374529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6        ||
2375529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
2376529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2377529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2378529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
2379529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2380529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2381529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2382529cad0fSDing Wang 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
2383529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10 ||
2384529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8 ||
2385529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6) {
2386529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2387529cad0fSDing Wang 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
2388529cad0fSDing Wang 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2389529cad0fSDing Wang 		fmt_bit_depth->flags.RGB_RANDOM =
2390529cad0fSDing Wang 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2391529cad0fSDing Wang 	}
2392529cad0fSDing Wang 
2393529cad0fSDing Wang 	if (option == DITHER_OPTION_SPATIAL6 ||
2394529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8 ||
2395529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10) {
2396529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
2397529cad0fSDing Wang 	} else {
2398529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
2399529cad0fSDing Wang 	}
2400529cad0fSDing Wang 
2401529cad0fSDing Wang 	//////////////////////
2402529cad0fSDing Wang 	//// temporal dither
2403529cad0fSDing Wang 	//////////////////////
2404529cad0fSDing Wang 	if (option == DITHER_OPTION_FM6           ||
2405529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL8_FM6     ||
2406529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM6     ||
2407529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM6     ||
2408529cad0fSDing Wang 			option == DITHER_OPTION_TRUN8_FM6      ||
2409529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2410529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2411529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
2412529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM8        ||
2413529cad0fSDing Wang 			option == DITHER_OPTION_SPATIAL10_FM8  ||
2414529cad0fSDing Wang 			option == DITHER_OPTION_TRUN10_FM8) {
2415529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2416529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
2417529cad0fSDing Wang 	} else if (option == DITHER_OPTION_FM10) {
2418529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2419529cad0fSDing Wang 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
2420529cad0fSDing Wang 	}
2421529cad0fSDing Wang 
2422529cad0fSDing Wang 	fmt_bit_depth->pixel_encoding = pixel_encoding;
2423529cad0fSDing Wang }
2424