1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2012-15 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  * Authors: AMD
23*b843c749SSergey Zigachev  *
24*b843c749SSergey Zigachev  */
25*b843c749SSergey Zigachev #include "dm_services.h"
26*b843c749SSergey Zigachev 
27*b843c749SSergey Zigachev #include "resource.h"
28*b843c749SSergey Zigachev #include "include/irq_service_interface.h"
29*b843c749SSergey Zigachev #include "link_encoder.h"
30*b843c749SSergey Zigachev #include "stream_encoder.h"
31*b843c749SSergey Zigachev #include "opp.h"
32*b843c749SSergey Zigachev #include "timing_generator.h"
33*b843c749SSergey Zigachev #include "transform.h"
34*b843c749SSergey Zigachev #include "dpp.h"
35*b843c749SSergey Zigachev #include "core_types.h"
36*b843c749SSergey Zigachev #include "set_mode_types.h"
37*b843c749SSergey Zigachev #include "virtual/virtual_stream_encoder.h"
38*b843c749SSergey Zigachev #include "dpcd_defs.h"
39*b843c749SSergey Zigachev 
40*b843c749SSergey Zigachev #include "dce80/dce80_resource.h"
41*b843c749SSergey Zigachev #include "dce100/dce100_resource.h"
42*b843c749SSergey Zigachev #include "dce110/dce110_resource.h"
43*b843c749SSergey Zigachev #include "dce112/dce112_resource.h"
44*b843c749SSergey Zigachev #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
45*b843c749SSergey Zigachev #include "dcn10/dcn10_resource.h"
46*b843c749SSergey Zigachev #endif
47*b843c749SSergey Zigachev #include "dce120/dce120_resource.h"
48*b843c749SSergey Zigachev 
49*b843c749SSergey Zigachev #define DC_LOGGER_INIT(logger)
50*b843c749SSergey Zigachev 
resource_parse_asic_id(struct hw_asic_id asic_id)51*b843c749SSergey Zigachev enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
52*b843c749SSergey Zigachev {
53*b843c749SSergey Zigachev 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
54*b843c749SSergey Zigachev 	switch (asic_id.chip_family) {
55*b843c749SSergey Zigachev 
56*b843c749SSergey Zigachev 	case FAMILY_CI:
57*b843c749SSergey Zigachev 		dc_version = DCE_VERSION_8_0;
58*b843c749SSergey Zigachev 		break;
59*b843c749SSergey Zigachev 	case FAMILY_KV:
60*b843c749SSergey Zigachev 		if (ASIC_REV_IS_KALINDI(asic_id.hw_internal_rev) ||
61*b843c749SSergey Zigachev 		    ASIC_REV_IS_BHAVANI(asic_id.hw_internal_rev) ||
62*b843c749SSergey Zigachev 		    ASIC_REV_IS_GODAVARI(asic_id.hw_internal_rev))
63*b843c749SSergey Zigachev 			dc_version = DCE_VERSION_8_3;
64*b843c749SSergey Zigachev 		else
65*b843c749SSergey Zigachev 			dc_version = DCE_VERSION_8_1;
66*b843c749SSergey Zigachev 		break;
67*b843c749SSergey Zigachev 	case FAMILY_CZ:
68*b843c749SSergey Zigachev 		dc_version = DCE_VERSION_11_0;
69*b843c749SSergey Zigachev 		break;
70*b843c749SSergey Zigachev 
71*b843c749SSergey Zigachev 	case FAMILY_VI:
72*b843c749SSergey Zigachev 		if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
73*b843c749SSergey Zigachev 				ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
74*b843c749SSergey Zigachev 			dc_version = DCE_VERSION_10_0;
75*b843c749SSergey Zigachev 			break;
76*b843c749SSergey Zigachev 		}
77*b843c749SSergey Zigachev 		if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
78*b843c749SSergey Zigachev 				ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
79*b843c749SSergey Zigachev 				ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
80*b843c749SSergey Zigachev 			dc_version = DCE_VERSION_11_2;
81*b843c749SSergey Zigachev 		}
82*b843c749SSergey Zigachev 		if (ASIC_REV_IS_VEGAM(asic_id.hw_internal_rev))
83*b843c749SSergey Zigachev 			dc_version = DCE_VERSION_11_22;
84*b843c749SSergey Zigachev 		break;
85*b843c749SSergey Zigachev 	case FAMILY_AI:
86*b843c749SSergey Zigachev 		dc_version = DCE_VERSION_12_0;
87*b843c749SSergey Zigachev 		break;
88*b843c749SSergey Zigachev #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
89*b843c749SSergey Zigachev 	case FAMILY_RV:
90*b843c749SSergey Zigachev 		dc_version = DCN_VERSION_1_0;
91*b843c749SSergey Zigachev 		break;
92*b843c749SSergey Zigachev #endif
93*b843c749SSergey Zigachev 	default:
94*b843c749SSergey Zigachev 		dc_version = DCE_VERSION_UNKNOWN;
95*b843c749SSergey Zigachev 		break;
96*b843c749SSergey Zigachev 	}
97*b843c749SSergey Zigachev 	return dc_version;
98*b843c749SSergey Zigachev }
99*b843c749SSergey Zigachev 
dc_create_resource_pool(struct dc * dc,int num_virtual_links,enum dce_version dc_version,struct hw_asic_id asic_id)100*b843c749SSergey Zigachev struct resource_pool *dc_create_resource_pool(
101*b843c749SSergey Zigachev 				struct dc  *dc,
102*b843c749SSergey Zigachev 				int num_virtual_links,
103*b843c749SSergey Zigachev 				enum dce_version dc_version,
104*b843c749SSergey Zigachev 				struct hw_asic_id asic_id)
105*b843c749SSergey Zigachev {
106*b843c749SSergey Zigachev 	struct resource_pool *res_pool = NULL;
107*b843c749SSergey Zigachev 
108*b843c749SSergey Zigachev 	switch (dc_version) {
109*b843c749SSergey Zigachev 	case DCE_VERSION_8_0:
110*b843c749SSergey Zigachev 		res_pool = dce80_create_resource_pool(
111*b843c749SSergey Zigachev 			num_virtual_links, dc);
112*b843c749SSergey Zigachev 		break;
113*b843c749SSergey Zigachev 	case DCE_VERSION_8_1:
114*b843c749SSergey Zigachev 		res_pool = dce81_create_resource_pool(
115*b843c749SSergey Zigachev 			num_virtual_links, dc);
116*b843c749SSergey Zigachev 		break;
117*b843c749SSergey Zigachev 	case DCE_VERSION_8_3:
118*b843c749SSergey Zigachev 		res_pool = dce83_create_resource_pool(
119*b843c749SSergey Zigachev 			num_virtual_links, dc);
120*b843c749SSergey Zigachev 		break;
121*b843c749SSergey Zigachev 	case DCE_VERSION_10_0:
122*b843c749SSergey Zigachev 		res_pool = dce100_create_resource_pool(
123*b843c749SSergey Zigachev 				num_virtual_links, dc);
124*b843c749SSergey Zigachev 		break;
125*b843c749SSergey Zigachev 	case DCE_VERSION_11_0:
126*b843c749SSergey Zigachev 		res_pool = dce110_create_resource_pool(
127*b843c749SSergey Zigachev 			num_virtual_links, dc, asic_id);
128*b843c749SSergey Zigachev 		break;
129*b843c749SSergey Zigachev 	case DCE_VERSION_11_2:
130*b843c749SSergey Zigachev 	case DCE_VERSION_11_22:
131*b843c749SSergey Zigachev 		res_pool = dce112_create_resource_pool(
132*b843c749SSergey Zigachev 			num_virtual_links, dc);
133*b843c749SSergey Zigachev 		break;
134*b843c749SSergey Zigachev 	case DCE_VERSION_12_0:
135*b843c749SSergey Zigachev 		res_pool = dce120_create_resource_pool(
136*b843c749SSergey Zigachev 			num_virtual_links, dc);
137*b843c749SSergey Zigachev 		break;
138*b843c749SSergey Zigachev 
139*b843c749SSergey Zigachev #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
140*b843c749SSergey Zigachev 	case DCN_VERSION_1_0:
141*b843c749SSergey Zigachev 		res_pool = dcn10_create_resource_pool(
142*b843c749SSergey Zigachev 				num_virtual_links, dc);
143*b843c749SSergey Zigachev 		break;
144*b843c749SSergey Zigachev #endif
145*b843c749SSergey Zigachev 
146*b843c749SSergey Zigachev 
147*b843c749SSergey Zigachev 	default:
148*b843c749SSergey Zigachev 		break;
149*b843c749SSergey Zigachev 	}
150*b843c749SSergey Zigachev 	if (res_pool != NULL) {
151*b843c749SSergey Zigachev 		struct dc_firmware_info fw_info = { { 0 } };
152*b843c749SSergey Zigachev 
153*b843c749SSergey Zigachev 		if (dc->ctx->dc_bios->funcs->get_firmware_info(
154*b843c749SSergey Zigachev 				dc->ctx->dc_bios, &fw_info) == BP_RESULT_OK) {
155*b843c749SSergey Zigachev 				res_pool->ref_clock_inKhz = fw_info.pll_info.crystal_frequency;
156*b843c749SSergey Zigachev 			} else
157*b843c749SSergey Zigachev 				ASSERT_CRITICAL(false);
158*b843c749SSergey Zigachev 	}
159*b843c749SSergey Zigachev 
160*b843c749SSergey Zigachev 	return res_pool;
161*b843c749SSergey Zigachev }
162*b843c749SSergey Zigachev 
dc_destroy_resource_pool(struct dc * dc)163*b843c749SSergey Zigachev void dc_destroy_resource_pool(struct dc  *dc)
164*b843c749SSergey Zigachev {
165*b843c749SSergey Zigachev 	if (dc) {
166*b843c749SSergey Zigachev 		if (dc->res_pool)
167*b843c749SSergey Zigachev 			dc->res_pool->funcs->destroy(&dc->res_pool);
168*b843c749SSergey Zigachev 
169*b843c749SSergey Zigachev 		kfree(dc->hwseq);
170*b843c749SSergey Zigachev 	}
171*b843c749SSergey Zigachev }
172*b843c749SSergey Zigachev 
update_num_audio(const struct resource_straps * straps,unsigned int * num_audio,struct audio_support * aud_support)173*b843c749SSergey Zigachev static void update_num_audio(
174*b843c749SSergey Zigachev 	const struct resource_straps *straps,
175*b843c749SSergey Zigachev 	unsigned int *num_audio,
176*b843c749SSergey Zigachev 	struct audio_support *aud_support)
177*b843c749SSergey Zigachev {
178*b843c749SSergey Zigachev 	aud_support->dp_audio = true;
179*b843c749SSergey Zigachev 	aud_support->hdmi_audio_native = false;
180*b843c749SSergey Zigachev 	aud_support->hdmi_audio_on_dongle = false;
181*b843c749SSergey Zigachev 
182*b843c749SSergey Zigachev 	if (straps->hdmi_disable == 0) {
183*b843c749SSergey Zigachev 		if (straps->dc_pinstraps_audio & 0x2) {
184*b843c749SSergey Zigachev 			aud_support->hdmi_audio_on_dongle = true;
185*b843c749SSergey Zigachev 			aud_support->hdmi_audio_native = true;
186*b843c749SSergey Zigachev 		}
187*b843c749SSergey Zigachev 	}
188*b843c749SSergey Zigachev 
189*b843c749SSergey Zigachev 	switch (straps->audio_stream_number) {
190*b843c749SSergey Zigachev 	case 0: /* multi streams supported */
191*b843c749SSergey Zigachev 		break;
192*b843c749SSergey Zigachev 	case 1: /* multi streams not supported */
193*b843c749SSergey Zigachev 		*num_audio = 1;
194*b843c749SSergey Zigachev 		break;
195*b843c749SSergey Zigachev 	default:
196*b843c749SSergey Zigachev 		DC_ERR("DC: unexpected audio fuse!\n");
197*b843c749SSergey Zigachev 	}
198*b843c749SSergey Zigachev }
199*b843c749SSergey Zigachev 
resource_construct(unsigned int num_virtual_links,struct dc * dc,struct resource_pool * pool,const struct resource_create_funcs * create_funcs)200*b843c749SSergey Zigachev bool resource_construct(
201*b843c749SSergey Zigachev 	unsigned int num_virtual_links,
202*b843c749SSergey Zigachev 	struct dc  *dc,
203*b843c749SSergey Zigachev 	struct resource_pool *pool,
204*b843c749SSergey Zigachev 	const struct resource_create_funcs *create_funcs)
205*b843c749SSergey Zigachev {
206*b843c749SSergey Zigachev 	struct dc_context *ctx = dc->ctx;
207*b843c749SSergey Zigachev 	const struct resource_caps *caps = pool->res_cap;
208*b843c749SSergey Zigachev 	int i;
209*b843c749SSergey Zigachev 	unsigned int num_audio = caps->num_audio;
210*b843c749SSergey Zigachev 	struct resource_straps straps = {0};
211*b843c749SSergey Zigachev 
212*b843c749SSergey Zigachev 	if (create_funcs->read_dce_straps)
213*b843c749SSergey Zigachev 		create_funcs->read_dce_straps(dc->ctx, &straps);
214*b843c749SSergey Zigachev 
215*b843c749SSergey Zigachev 	pool->audio_count = 0;
216*b843c749SSergey Zigachev 	if (create_funcs->create_audio) {
217*b843c749SSergey Zigachev 		/* find the total number of streams available via the
218*b843c749SSergey Zigachev 		 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
219*b843c749SSergey Zigachev 		 * registers (one for each pin) starting from pin 1
220*b843c749SSergey Zigachev 		 * up to the max number of audio pins.
221*b843c749SSergey Zigachev 		 * We stop on the first pin where
222*b843c749SSergey Zigachev 		 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
223*b843c749SSergey Zigachev 		 */
224*b843c749SSergey Zigachev 		update_num_audio(&straps, &num_audio, &pool->audio_support);
225*b843c749SSergey Zigachev 		for (i = 0; i < caps->num_audio; i++) {
226*b843c749SSergey Zigachev 			struct audio *aud = create_funcs->create_audio(ctx, i);
227*b843c749SSergey Zigachev 
228*b843c749SSergey Zigachev 			if (aud == NULL) {
229*b843c749SSergey Zigachev 				DC_ERR("DC: failed to create audio!\n");
230*b843c749SSergey Zigachev 				return false;
231*b843c749SSergey Zigachev 			}
232*b843c749SSergey Zigachev 			if (!aud->funcs->endpoint_valid(aud)) {
233*b843c749SSergey Zigachev 				aud->funcs->destroy(&aud);
234*b843c749SSergey Zigachev 				break;
235*b843c749SSergey Zigachev 			}
236*b843c749SSergey Zigachev 			pool->audios[i] = aud;
237*b843c749SSergey Zigachev 			pool->audio_count++;
238*b843c749SSergey Zigachev 		}
239*b843c749SSergey Zigachev 	}
240*b843c749SSergey Zigachev 
241*b843c749SSergey Zigachev 	pool->stream_enc_count = 0;
242*b843c749SSergey Zigachev 	if (create_funcs->create_stream_encoder) {
243*b843c749SSergey Zigachev 		for (i = 0; i < caps->num_stream_encoder; i++) {
244*b843c749SSergey Zigachev 			pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
245*b843c749SSergey Zigachev 			if (pool->stream_enc[i] == NULL)
246*b843c749SSergey Zigachev 				DC_ERR("DC: failed to create stream_encoder!\n");
247*b843c749SSergey Zigachev 			pool->stream_enc_count++;
248*b843c749SSergey Zigachev 		}
249*b843c749SSergey Zigachev 	}
250*b843c749SSergey Zigachev 	dc->caps.dynamic_audio = false;
251*b843c749SSergey Zigachev 	if (pool->audio_count < pool->stream_enc_count) {
252*b843c749SSergey Zigachev 		dc->caps.dynamic_audio = true;
253*b843c749SSergey Zigachev 	}
254*b843c749SSergey Zigachev 	for (i = 0; i < num_virtual_links; i++) {
255*b843c749SSergey Zigachev 		pool->stream_enc[pool->stream_enc_count] =
256*b843c749SSergey Zigachev 			virtual_stream_encoder_create(
257*b843c749SSergey Zigachev 					ctx, ctx->dc_bios);
258*b843c749SSergey Zigachev 		if (pool->stream_enc[pool->stream_enc_count] == NULL) {
259*b843c749SSergey Zigachev 			DC_ERR("DC: failed to create stream_encoder!\n");
260*b843c749SSergey Zigachev 			return false;
261*b843c749SSergey Zigachev 		}
262*b843c749SSergey Zigachev 		pool->stream_enc_count++;
263*b843c749SSergey Zigachev 	}
264*b843c749SSergey Zigachev 
265*b843c749SSergey Zigachev 	dc->hwseq = create_funcs->create_hwseq(ctx);
266*b843c749SSergey Zigachev 
267*b843c749SSergey Zigachev 	return true;
268*b843c749SSergey Zigachev }
find_matching_clock_source(const struct resource_pool * pool,struct clock_source * clock_source)269*b843c749SSergey Zigachev static int find_matching_clock_source(
270*b843c749SSergey Zigachev 		const struct resource_pool *pool,
271*b843c749SSergey Zigachev 		struct clock_source *clock_source)
272*b843c749SSergey Zigachev {
273*b843c749SSergey Zigachev 
274*b843c749SSergey Zigachev 	int i;
275*b843c749SSergey Zigachev 
276*b843c749SSergey Zigachev 	for (i = 0; i < pool->clk_src_count; i++) {
277*b843c749SSergey Zigachev 		if (pool->clock_sources[i] == clock_source)
278*b843c749SSergey Zigachev 			return i;
279*b843c749SSergey Zigachev 	}
280*b843c749SSergey Zigachev 	return -1;
281*b843c749SSergey Zigachev }
282*b843c749SSergey Zigachev 
resource_unreference_clock_source(struct resource_context * res_ctx,const struct resource_pool * pool,struct clock_source * clock_source)283*b843c749SSergey Zigachev void resource_unreference_clock_source(
284*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
285*b843c749SSergey Zigachev 		const struct resource_pool *pool,
286*b843c749SSergey Zigachev 		struct clock_source *clock_source)
287*b843c749SSergey Zigachev {
288*b843c749SSergey Zigachev 	int i = find_matching_clock_source(pool, clock_source);
289*b843c749SSergey Zigachev 
290*b843c749SSergey Zigachev 	if (i > -1)
291*b843c749SSergey Zigachev 		res_ctx->clock_source_ref_count[i]--;
292*b843c749SSergey Zigachev 
293*b843c749SSergey Zigachev 	if (pool->dp_clock_source == clock_source)
294*b843c749SSergey Zigachev 		res_ctx->dp_clock_source_ref_count--;
295*b843c749SSergey Zigachev }
296*b843c749SSergey Zigachev 
resource_reference_clock_source(struct resource_context * res_ctx,const struct resource_pool * pool,struct clock_source * clock_source)297*b843c749SSergey Zigachev void resource_reference_clock_source(
298*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
299*b843c749SSergey Zigachev 		const struct resource_pool *pool,
300*b843c749SSergey Zigachev 		struct clock_source *clock_source)
301*b843c749SSergey Zigachev {
302*b843c749SSergey Zigachev 	int i = find_matching_clock_source(pool, clock_source);
303*b843c749SSergey Zigachev 
304*b843c749SSergey Zigachev 	if (i > -1)
305*b843c749SSergey Zigachev 		res_ctx->clock_source_ref_count[i]++;
306*b843c749SSergey Zigachev 
307*b843c749SSergey Zigachev 	if (pool->dp_clock_source == clock_source)
308*b843c749SSergey Zigachev 		res_ctx->dp_clock_source_ref_count++;
309*b843c749SSergey Zigachev }
310*b843c749SSergey Zigachev 
resource_get_clock_source_reference(struct resource_context * res_ctx,const struct resource_pool * pool,struct clock_source * clock_source)311*b843c749SSergey Zigachev int resource_get_clock_source_reference(
312*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
313*b843c749SSergey Zigachev 		const struct resource_pool *pool,
314*b843c749SSergey Zigachev 		struct clock_source *clock_source)
315*b843c749SSergey Zigachev {
316*b843c749SSergey Zigachev 	int i = find_matching_clock_source(pool, clock_source);
317*b843c749SSergey Zigachev 
318*b843c749SSergey Zigachev 	if (i > -1)
319*b843c749SSergey Zigachev 		return res_ctx->clock_source_ref_count[i];
320*b843c749SSergey Zigachev 
321*b843c749SSergey Zigachev 	if (pool->dp_clock_source == clock_source)
322*b843c749SSergey Zigachev 		return res_ctx->dp_clock_source_ref_count;
323*b843c749SSergey Zigachev 
324*b843c749SSergey Zigachev 	return -1;
325*b843c749SSergey Zigachev }
326*b843c749SSergey Zigachev 
resource_are_streams_timing_synchronizable(struct dc_stream_state * stream1,struct dc_stream_state * stream2)327*b843c749SSergey Zigachev bool resource_are_streams_timing_synchronizable(
328*b843c749SSergey Zigachev 	struct dc_stream_state *stream1,
329*b843c749SSergey Zigachev 	struct dc_stream_state *stream2)
330*b843c749SSergey Zigachev {
331*b843c749SSergey Zigachev 	if (stream1->timing.h_total != stream2->timing.h_total)
332*b843c749SSergey Zigachev 		return false;
333*b843c749SSergey Zigachev 
334*b843c749SSergey Zigachev 	if (stream1->timing.v_total != stream2->timing.v_total)
335*b843c749SSergey Zigachev 		return false;
336*b843c749SSergey Zigachev 
337*b843c749SSergey Zigachev 	if (stream1->timing.h_addressable
338*b843c749SSergey Zigachev 				!= stream2->timing.h_addressable)
339*b843c749SSergey Zigachev 		return false;
340*b843c749SSergey Zigachev 
341*b843c749SSergey Zigachev 	if (stream1->timing.v_addressable
342*b843c749SSergey Zigachev 				!= stream2->timing.v_addressable)
343*b843c749SSergey Zigachev 		return false;
344*b843c749SSergey Zigachev 
345*b843c749SSergey Zigachev 	if (stream1->timing.pix_clk_khz
346*b843c749SSergey Zigachev 				!= stream2->timing.pix_clk_khz)
347*b843c749SSergey Zigachev 		return false;
348*b843c749SSergey Zigachev 
349*b843c749SSergey Zigachev 	if (stream1->clamping.c_depth != stream2->clamping.c_depth)
350*b843c749SSergey Zigachev 		return false;
351*b843c749SSergey Zigachev 
352*b843c749SSergey Zigachev 	if (stream1->phy_pix_clk != stream2->phy_pix_clk
353*b843c749SSergey Zigachev 			&& (!dc_is_dp_signal(stream1->signal)
354*b843c749SSergey Zigachev 			|| !dc_is_dp_signal(stream2->signal)))
355*b843c749SSergey Zigachev 		return false;
356*b843c749SSergey Zigachev 
357*b843c749SSergey Zigachev 	return true;
358*b843c749SSergey Zigachev }
is_dp_and_hdmi_sharable(struct dc_stream_state * stream1,struct dc_stream_state * stream2)359*b843c749SSergey Zigachev static bool is_dp_and_hdmi_sharable(
360*b843c749SSergey Zigachev 		struct dc_stream_state *stream1,
361*b843c749SSergey Zigachev 		struct dc_stream_state *stream2)
362*b843c749SSergey Zigachev {
363*b843c749SSergey Zigachev 	if (stream1->ctx->dc->caps.disable_dp_clk_share)
364*b843c749SSergey Zigachev 		return false;
365*b843c749SSergey Zigachev 
366*b843c749SSergey Zigachev 	if (stream1->clamping.c_depth != COLOR_DEPTH_888 ||
367*b843c749SSergey Zigachev 	    stream2->clamping.c_depth != COLOR_DEPTH_888)
368*b843c749SSergey Zigachev 	return false;
369*b843c749SSergey Zigachev 
370*b843c749SSergey Zigachev 	return true;
371*b843c749SSergey Zigachev 
372*b843c749SSergey Zigachev }
373*b843c749SSergey Zigachev 
is_sharable_clk_src(const struct pipe_ctx * pipe_with_clk_src,const struct pipe_ctx * pipe)374*b843c749SSergey Zigachev static bool is_sharable_clk_src(
375*b843c749SSergey Zigachev 	const struct pipe_ctx *pipe_with_clk_src,
376*b843c749SSergey Zigachev 	const struct pipe_ctx *pipe)
377*b843c749SSergey Zigachev {
378*b843c749SSergey Zigachev 	if (pipe_with_clk_src->clock_source == NULL)
379*b843c749SSergey Zigachev 		return false;
380*b843c749SSergey Zigachev 
381*b843c749SSergey Zigachev 	if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
382*b843c749SSergey Zigachev 		return false;
383*b843c749SSergey Zigachev 
384*b843c749SSergey Zigachev 	if (dc_is_dp_signal(pipe_with_clk_src->stream->signal) ||
385*b843c749SSergey Zigachev 		(dc_is_dp_signal(pipe->stream->signal) &&
386*b843c749SSergey Zigachev 		!is_dp_and_hdmi_sharable(pipe_with_clk_src->stream,
387*b843c749SSergey Zigachev 				     pipe->stream)))
388*b843c749SSergey Zigachev 		return false;
389*b843c749SSergey Zigachev 
390*b843c749SSergey Zigachev 	if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
391*b843c749SSergey Zigachev 			&& dc_is_dual_link_signal(pipe->stream->signal))
392*b843c749SSergey Zigachev 		return false;
393*b843c749SSergey Zigachev 
394*b843c749SSergey Zigachev 	if (dc_is_hdmi_signal(pipe->stream->signal)
395*b843c749SSergey Zigachev 			&& dc_is_dual_link_signal(pipe_with_clk_src->stream->signal))
396*b843c749SSergey Zigachev 		return false;
397*b843c749SSergey Zigachev 
398*b843c749SSergey Zigachev 	if (!resource_are_streams_timing_synchronizable(
399*b843c749SSergey Zigachev 			pipe_with_clk_src->stream, pipe->stream))
400*b843c749SSergey Zigachev 		return false;
401*b843c749SSergey Zigachev 
402*b843c749SSergey Zigachev 	return true;
403*b843c749SSergey Zigachev }
404*b843c749SSergey Zigachev 
resource_find_used_clk_src_for_sharing(struct resource_context * res_ctx,struct pipe_ctx * pipe_ctx)405*b843c749SSergey Zigachev struct clock_source *resource_find_used_clk_src_for_sharing(
406*b843c749SSergey Zigachev 					struct resource_context *res_ctx,
407*b843c749SSergey Zigachev 					struct pipe_ctx *pipe_ctx)
408*b843c749SSergey Zigachev {
409*b843c749SSergey Zigachev 	int i;
410*b843c749SSergey Zigachev 
411*b843c749SSergey Zigachev 	for (i = 0; i < MAX_PIPES; i++) {
412*b843c749SSergey Zigachev 		if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
413*b843c749SSergey Zigachev 			return res_ctx->pipe_ctx[i].clock_source;
414*b843c749SSergey Zigachev 	}
415*b843c749SSergey Zigachev 
416*b843c749SSergey Zigachev 	return NULL;
417*b843c749SSergey Zigachev }
418*b843c749SSergey Zigachev 
convert_pixel_format_to_dalsurface(enum surface_pixel_format surface_pixel_format)419*b843c749SSergey Zigachev static enum pixel_format convert_pixel_format_to_dalsurface(
420*b843c749SSergey Zigachev 		enum surface_pixel_format surface_pixel_format)
421*b843c749SSergey Zigachev {
422*b843c749SSergey Zigachev 	enum pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
423*b843c749SSergey Zigachev 
424*b843c749SSergey Zigachev 	switch (surface_pixel_format) {
425*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
426*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_INDEX8;
427*b843c749SSergey Zigachev 		break;
428*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
429*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_RGB565;
430*b843c749SSergey Zigachev 		break;
431*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
432*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_RGB565;
433*b843c749SSergey Zigachev 		break;
434*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
435*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
436*b843c749SSergey Zigachev 		break;
437*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
438*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
439*b843c749SSergey Zigachev 		break;
440*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
441*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
442*b843c749SSergey Zigachev 		break;
443*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
444*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
445*b843c749SSergey Zigachev 		break;
446*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
447*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
448*b843c749SSergey Zigachev 		break;
449*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
450*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
451*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_FP16;
452*b843c749SSergey Zigachev 		break;
453*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
454*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
455*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_420BPP8;
456*b843c749SSergey Zigachev 		break;
457*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
458*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
459*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_420BPP10;
460*b843c749SSergey Zigachev 		break;
461*b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
462*b843c749SSergey Zigachev 	default:
463*b843c749SSergey Zigachev 		dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
464*b843c749SSergey Zigachev 		break;
465*b843c749SSergey Zigachev 	}
466*b843c749SSergey Zigachev 	return dal_pixel_format;
467*b843c749SSergey Zigachev }
468*b843c749SSergey Zigachev 
rect_swap_helper(struct rect * rect)469*b843c749SSergey Zigachev static void rect_swap_helper(struct rect *rect)
470*b843c749SSergey Zigachev {
471*b843c749SSergey Zigachev 	swap(rect->height, rect->width);
472*b843c749SSergey Zigachev 	swap(rect->x, rect->y);
473*b843c749SSergey Zigachev }
474*b843c749SSergey Zigachev 
calculate_viewport(struct pipe_ctx * pipe_ctx)475*b843c749SSergey Zigachev static void calculate_viewport(struct pipe_ctx *pipe_ctx)
476*b843c749SSergey Zigachev {
477*b843c749SSergey Zigachev 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
478*b843c749SSergey Zigachev 	const struct dc_stream_state *stream = pipe_ctx->stream;
479*b843c749SSergey Zigachev 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
480*b843c749SSergey Zigachev 	struct rect surf_src = plane_state->src_rect;
481*b843c749SSergey Zigachev 	struct rect clip = { 0 };
482*b843c749SSergey Zigachev 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
483*b843c749SSergey Zigachev 			|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
484*b843c749SSergey Zigachev 	bool pri_split = pipe_ctx->bottom_pipe &&
485*b843c749SSergey Zigachev 			pipe_ctx->bottom_pipe->plane_state == pipe_ctx->plane_state;
486*b843c749SSergey Zigachev 	bool sec_split = pipe_ctx->top_pipe &&
487*b843c749SSergey Zigachev 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
488*b843c749SSergey Zigachev 
489*b843c749SSergey Zigachev 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE ||
490*b843c749SSergey Zigachev 		stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM) {
491*b843c749SSergey Zigachev 		pri_split = false;
492*b843c749SSergey Zigachev 		sec_split = false;
493*b843c749SSergey Zigachev 	}
494*b843c749SSergey Zigachev 
495*b843c749SSergey Zigachev 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
496*b843c749SSergey Zigachev 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
497*b843c749SSergey Zigachev 		rect_swap_helper(&surf_src);
498*b843c749SSergey Zigachev 
499*b843c749SSergey Zigachev 	/* The actual clip is an intersection between stream
500*b843c749SSergey Zigachev 	 * source and surface clip
501*b843c749SSergey Zigachev 	 */
502*b843c749SSergey Zigachev 	clip.x = stream->src.x > plane_state->clip_rect.x ?
503*b843c749SSergey Zigachev 			stream->src.x : plane_state->clip_rect.x;
504*b843c749SSergey Zigachev 
505*b843c749SSergey Zigachev 	clip.width = stream->src.x + stream->src.width <
506*b843c749SSergey Zigachev 			plane_state->clip_rect.x + plane_state->clip_rect.width ?
507*b843c749SSergey Zigachev 			stream->src.x + stream->src.width - clip.x :
508*b843c749SSergey Zigachev 			plane_state->clip_rect.x + plane_state->clip_rect.width - clip.x ;
509*b843c749SSergey Zigachev 
510*b843c749SSergey Zigachev 	clip.y = stream->src.y > plane_state->clip_rect.y ?
511*b843c749SSergey Zigachev 			stream->src.y : plane_state->clip_rect.y;
512*b843c749SSergey Zigachev 
513*b843c749SSergey Zigachev 	clip.height = stream->src.y + stream->src.height <
514*b843c749SSergey Zigachev 			plane_state->clip_rect.y + plane_state->clip_rect.height ?
515*b843c749SSergey Zigachev 			stream->src.y + stream->src.height - clip.y :
516*b843c749SSergey Zigachev 			plane_state->clip_rect.y + plane_state->clip_rect.height - clip.y ;
517*b843c749SSergey Zigachev 
518*b843c749SSergey Zigachev 	/* offset = surf_src.ofs + (clip.ofs - surface->dst_rect.ofs) * scl_ratio
519*b843c749SSergey Zigachev 	 * num_pixels = clip.num_pix * scl_ratio
520*b843c749SSergey Zigachev 	 */
521*b843c749SSergey Zigachev 	data->viewport.x = surf_src.x + (clip.x - plane_state->dst_rect.x) *
522*b843c749SSergey Zigachev 			surf_src.width / plane_state->dst_rect.width;
523*b843c749SSergey Zigachev 	data->viewport.width = clip.width *
524*b843c749SSergey Zigachev 			surf_src.width / plane_state->dst_rect.width;
525*b843c749SSergey Zigachev 
526*b843c749SSergey Zigachev 	data->viewport.y = surf_src.y + (clip.y - plane_state->dst_rect.y) *
527*b843c749SSergey Zigachev 			surf_src.height / plane_state->dst_rect.height;
528*b843c749SSergey Zigachev 	data->viewport.height = clip.height *
529*b843c749SSergey Zigachev 			surf_src.height / plane_state->dst_rect.height;
530*b843c749SSergey Zigachev 
531*b843c749SSergey Zigachev 	/* Round down, compensate in init */
532*b843c749SSergey Zigachev 	data->viewport_c.x = data->viewport.x / vpc_div;
533*b843c749SSergey Zigachev 	data->viewport_c.y = data->viewport.y / vpc_div;
534*b843c749SSergey Zigachev 	data->inits.h_c = (data->viewport.x % vpc_div) != 0 ?
535*b843c749SSergey Zigachev 			dc_fixpt_half : dc_fixpt_zero;
536*b843c749SSergey Zigachev 	data->inits.v_c = (data->viewport.y % vpc_div) != 0 ?
537*b843c749SSergey Zigachev 			dc_fixpt_half : dc_fixpt_zero;
538*b843c749SSergey Zigachev 	/* Round up, assume original video size always even dimensions */
539*b843c749SSergey Zigachev 	data->viewport_c.width = (data->viewport.width + vpc_div - 1) / vpc_div;
540*b843c749SSergey Zigachev 	data->viewport_c.height = (data->viewport.height + vpc_div - 1) / vpc_div;
541*b843c749SSergey Zigachev 
542*b843c749SSergey Zigachev 	/* Handle hsplit */
543*b843c749SSergey Zigachev 	if (sec_split) {
544*b843c749SSergey Zigachev 		data->viewport.x +=  data->viewport.width / 2;
545*b843c749SSergey Zigachev 		data->viewport_c.x +=  data->viewport_c.width / 2;
546*b843c749SSergey Zigachev 		/* Ceil offset pipe */
547*b843c749SSergey Zigachev 		data->viewport.width = (data->viewport.width + 1) / 2;
548*b843c749SSergey Zigachev 		data->viewport_c.width = (data->viewport_c.width + 1) / 2;
549*b843c749SSergey Zigachev 	} else if (pri_split) {
550*b843c749SSergey Zigachev 		data->viewport.width /= 2;
551*b843c749SSergey Zigachev 		data->viewport_c.width /= 2;
552*b843c749SSergey Zigachev 	}
553*b843c749SSergey Zigachev 
554*b843c749SSergey Zigachev 	if (plane_state->rotation == ROTATION_ANGLE_90 ||
555*b843c749SSergey Zigachev 			plane_state->rotation == ROTATION_ANGLE_270) {
556*b843c749SSergey Zigachev 		rect_swap_helper(&data->viewport_c);
557*b843c749SSergey Zigachev 		rect_swap_helper(&data->viewport);
558*b843c749SSergey Zigachev 	}
559*b843c749SSergey Zigachev }
560*b843c749SSergey Zigachev 
calculate_recout(struct pipe_ctx * pipe_ctx,struct rect * recout_full)561*b843c749SSergey Zigachev static void calculate_recout(struct pipe_ctx *pipe_ctx, struct rect *recout_full)
562*b843c749SSergey Zigachev {
563*b843c749SSergey Zigachev 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
564*b843c749SSergey Zigachev 	const struct dc_stream_state *stream = pipe_ctx->stream;
565*b843c749SSergey Zigachev 	struct rect surf_src = plane_state->src_rect;
566*b843c749SSergey Zigachev 	struct rect surf_clip = plane_state->clip_rect;
567*b843c749SSergey Zigachev 	bool pri_split = pipe_ctx->bottom_pipe &&
568*b843c749SSergey Zigachev 			pipe_ctx->bottom_pipe->plane_state == pipe_ctx->plane_state;
569*b843c749SSergey Zigachev 	bool sec_split = pipe_ctx->top_pipe &&
570*b843c749SSergey Zigachev 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
571*b843c749SSergey Zigachev 	bool top_bottom_split = stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM;
572*b843c749SSergey Zigachev 
573*b843c749SSergey Zigachev 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
574*b843c749SSergey Zigachev 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
575*b843c749SSergey Zigachev 		rect_swap_helper(&surf_src);
576*b843c749SSergey Zigachev 
577*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.recout.x = stream->dst.x;
578*b843c749SSergey Zigachev 	if (stream->src.x < surf_clip.x)
579*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.recout.x += (surf_clip.x
580*b843c749SSergey Zigachev 			- stream->src.x) * stream->dst.width
581*b843c749SSergey Zigachev 						/ stream->src.width;
582*b843c749SSergey Zigachev 
583*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.recout.width = surf_clip.width *
584*b843c749SSergey Zigachev 			stream->dst.width / stream->src.width;
585*b843c749SSergey Zigachev 	if (pipe_ctx->plane_res.scl_data.recout.width + pipe_ctx->plane_res.scl_data.recout.x >
586*b843c749SSergey Zigachev 			stream->dst.x + stream->dst.width)
587*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.recout.width =
588*b843c749SSergey Zigachev 			stream->dst.x + stream->dst.width
589*b843c749SSergey Zigachev 						- pipe_ctx->plane_res.scl_data.recout.x;
590*b843c749SSergey Zigachev 
591*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.recout.y = stream->dst.y;
592*b843c749SSergey Zigachev 	if (stream->src.y < surf_clip.y)
593*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.recout.y += (surf_clip.y
594*b843c749SSergey Zigachev 			- stream->src.y) * stream->dst.height
595*b843c749SSergey Zigachev 						/ stream->src.height;
596*b843c749SSergey Zigachev 
597*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.recout.height = surf_clip.height *
598*b843c749SSergey Zigachev 			stream->dst.height / stream->src.height;
599*b843c749SSergey Zigachev 	if (pipe_ctx->plane_res.scl_data.recout.height + pipe_ctx->plane_res.scl_data.recout.y >
600*b843c749SSergey Zigachev 			stream->dst.y + stream->dst.height)
601*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.recout.height =
602*b843c749SSergey Zigachev 			stream->dst.y + stream->dst.height
603*b843c749SSergey Zigachev 						- pipe_ctx->plane_res.scl_data.recout.y;
604*b843c749SSergey Zigachev 
605*b843c749SSergey Zigachev 	/* Handle h & vsplit */
606*b843c749SSergey Zigachev 	if (sec_split && top_bottom_split) {
607*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.recout.y +=
608*b843c749SSergey Zigachev 				pipe_ctx->plane_res.scl_data.recout.height / 2;
609*b843c749SSergey Zigachev 		/* Floor primary pipe, ceil 2ndary pipe */
610*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.recout.height =
611*b843c749SSergey Zigachev 				(pipe_ctx->plane_res.scl_data.recout.height + 1) / 2;
612*b843c749SSergey Zigachev 	} else if (pri_split && top_bottom_split)
613*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.recout.height /= 2;
614*b843c749SSergey Zigachev 	else if (pri_split || sec_split) {
615*b843c749SSergey Zigachev 		/* HMirror XOR Secondary_pipe XOR Rotation_180 */
616*b843c749SSergey Zigachev 		bool right_view = (sec_split != plane_state->horizontal_mirror) !=
617*b843c749SSergey Zigachev 					(plane_state->rotation == ROTATION_ANGLE_180);
618*b843c749SSergey Zigachev 
619*b843c749SSergey Zigachev 		if (plane_state->rotation == ROTATION_ANGLE_90
620*b843c749SSergey Zigachev 				|| plane_state->rotation == ROTATION_ANGLE_270)
621*b843c749SSergey Zigachev 			/* Secondary_pipe XOR Rotation_270 */
622*b843c749SSergey Zigachev 			right_view = (plane_state->rotation == ROTATION_ANGLE_270) != sec_split;
623*b843c749SSergey Zigachev 
624*b843c749SSergey Zigachev 		if (right_view) {
625*b843c749SSergey Zigachev 			pipe_ctx->plane_res.scl_data.recout.x +=
626*b843c749SSergey Zigachev 					pipe_ctx->plane_res.scl_data.recout.width / 2;
627*b843c749SSergey Zigachev 			/* Ceil offset pipe */
628*b843c749SSergey Zigachev 			pipe_ctx->plane_res.scl_data.recout.width =
629*b843c749SSergey Zigachev 					(pipe_ctx->plane_res.scl_data.recout.width + 1) / 2;
630*b843c749SSergey Zigachev 		} else {
631*b843c749SSergey Zigachev 			pipe_ctx->plane_res.scl_data.recout.width /= 2;
632*b843c749SSergey Zigachev 		}
633*b843c749SSergey Zigachev 	}
634*b843c749SSergey Zigachev 	/* Unclipped recout offset = stream dst offset + ((surf dst offset - stream surf_src offset)
635*b843c749SSergey Zigachev 	 *			* 1/ stream scaling ratio) - (surf surf_src offset * 1/ full scl
636*b843c749SSergey Zigachev 	 *			ratio)
637*b843c749SSergey Zigachev 	 */
638*b843c749SSergey Zigachev 	recout_full->x = stream->dst.x + (plane_state->dst_rect.x - stream->src.x)
639*b843c749SSergey Zigachev 					* stream->dst.width / stream->src.width -
640*b843c749SSergey Zigachev 			surf_src.x * plane_state->dst_rect.width / surf_src.width
641*b843c749SSergey Zigachev 					* stream->dst.width / stream->src.width;
642*b843c749SSergey Zigachev 	recout_full->y = stream->dst.y + (plane_state->dst_rect.y - stream->src.y)
643*b843c749SSergey Zigachev 					* stream->dst.height / stream->src.height -
644*b843c749SSergey Zigachev 			surf_src.y * plane_state->dst_rect.height / surf_src.height
645*b843c749SSergey Zigachev 					* stream->dst.height / stream->src.height;
646*b843c749SSergey Zigachev 
647*b843c749SSergey Zigachev 	recout_full->width = plane_state->dst_rect.width
648*b843c749SSergey Zigachev 					* stream->dst.width / stream->src.width;
649*b843c749SSergey Zigachev 	recout_full->height = plane_state->dst_rect.height
650*b843c749SSergey Zigachev 					* stream->dst.height / stream->src.height;
651*b843c749SSergey Zigachev }
652*b843c749SSergey Zigachev 
calculate_scaling_ratios(struct pipe_ctx * pipe_ctx)653*b843c749SSergey Zigachev static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
654*b843c749SSergey Zigachev {
655*b843c749SSergey Zigachev 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
656*b843c749SSergey Zigachev 	const struct dc_stream_state *stream = pipe_ctx->stream;
657*b843c749SSergey Zigachev 	struct rect surf_src = plane_state->src_rect;
658*b843c749SSergey Zigachev 	const int in_w = stream->src.width;
659*b843c749SSergey Zigachev 	const int in_h = stream->src.height;
660*b843c749SSergey Zigachev 	const int out_w = stream->dst.width;
661*b843c749SSergey Zigachev 	const int out_h = stream->dst.height;
662*b843c749SSergey Zigachev 
663*b843c749SSergey Zigachev 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
664*b843c749SSergey Zigachev 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
665*b843c749SSergey Zigachev 		rect_swap_helper(&surf_src);
666*b843c749SSergey Zigachev 
667*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_from_fraction(
668*b843c749SSergey Zigachev 					surf_src.width,
669*b843c749SSergey Zigachev 					plane_state->dst_rect.width);
670*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_from_fraction(
671*b843c749SSergey Zigachev 					surf_src.height,
672*b843c749SSergey Zigachev 					plane_state->dst_rect.height);
673*b843c749SSergey Zigachev 
674*b843c749SSergey Zigachev 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
675*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.ratios.horz.value *= 2;
676*b843c749SSergey Zigachev 	else if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
677*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.ratios.vert.value *= 2;
678*b843c749SSergey Zigachev 
679*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.vert.value = div64_s64(
680*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.ratios.vert.value * in_h, out_h);
681*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.horz.value = div64_s64(
682*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.ratios.horz.value * in_w, out_w);
683*b843c749SSergey Zigachev 
684*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.horz_c = pipe_ctx->plane_res.scl_data.ratios.horz;
685*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.vert_c = pipe_ctx->plane_res.scl_data.ratios.vert;
686*b843c749SSergey Zigachev 
687*b843c749SSergey Zigachev 	if (pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP8
688*b843c749SSergey Zigachev 			|| pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP10) {
689*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.ratios.horz_c.value /= 2;
690*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.ratios.vert_c.value /= 2;
691*b843c749SSergey Zigachev 	}
692*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_truncate(
693*b843c749SSergey Zigachev 			pipe_ctx->plane_res.scl_data.ratios.horz, 19);
694*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_truncate(
695*b843c749SSergey Zigachev 			pipe_ctx->plane_res.scl_data.ratios.vert, 19);
696*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.horz_c = dc_fixpt_truncate(
697*b843c749SSergey Zigachev 			pipe_ctx->plane_res.scl_data.ratios.horz_c, 19);
698*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.ratios.vert_c = dc_fixpt_truncate(
699*b843c749SSergey Zigachev 			pipe_ctx->plane_res.scl_data.ratios.vert_c, 19);
700*b843c749SSergey Zigachev }
701*b843c749SSergey Zigachev 
calculate_inits_and_adj_vp(struct pipe_ctx * pipe_ctx,struct rect * recout_full)702*b843c749SSergey Zigachev static void calculate_inits_and_adj_vp(struct pipe_ctx *pipe_ctx, struct rect *recout_full)
703*b843c749SSergey Zigachev {
704*b843c749SSergey Zigachev 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
705*b843c749SSergey Zigachev 	struct rect src = pipe_ctx->plane_state->src_rect;
706*b843c749SSergey Zigachev 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
707*b843c749SSergey Zigachev 			|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
708*b843c749SSergey Zigachev 	bool flip_vert_scan_dir = false, flip_horz_scan_dir = false;
709*b843c749SSergey Zigachev 
710*b843c749SSergey Zigachev 	/*
711*b843c749SSergey Zigachev 	 * Need to calculate the scan direction for viewport to make adjustments
712*b843c749SSergey Zigachev 	 */
713*b843c749SSergey Zigachev 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_180) {
714*b843c749SSergey Zigachev 		flip_vert_scan_dir = true;
715*b843c749SSergey Zigachev 		flip_horz_scan_dir = true;
716*b843c749SSergey Zigachev 	} else if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90)
717*b843c749SSergey Zigachev 		flip_vert_scan_dir = true;
718*b843c749SSergey Zigachev 	else if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
719*b843c749SSergey Zigachev 		flip_horz_scan_dir = true;
720*b843c749SSergey Zigachev 
721*b843c749SSergey Zigachev 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
722*b843c749SSergey Zigachev 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270) {
723*b843c749SSergey Zigachev 		rect_swap_helper(&src);
724*b843c749SSergey Zigachev 		rect_swap_helper(&data->viewport_c);
725*b843c749SSergey Zigachev 		rect_swap_helper(&data->viewport);
726*b843c749SSergey Zigachev 	} else if (pipe_ctx->plane_state->horizontal_mirror)
727*b843c749SSergey Zigachev 			flip_horz_scan_dir = !flip_horz_scan_dir;
728*b843c749SSergey Zigachev 
729*b843c749SSergey Zigachev 	/*
730*b843c749SSergey Zigachev 	 * Init calculated according to formula:
731*b843c749SSergey Zigachev 	 * 	init = (scaling_ratio + number_of_taps + 1) / 2
732*b843c749SSergey Zigachev 	 * 	init_bot = init + scaling_ratio
733*b843c749SSergey Zigachev 	 * 	init_c = init + truncated_vp_c_offset(from calculate viewport)
734*b843c749SSergey Zigachev 	 */
735*b843c749SSergey Zigachev 	data->inits.h = dc_fixpt_truncate(dc_fixpt_div_int(
736*b843c749SSergey Zigachev 			dc_fixpt_add_int(data->ratios.horz, data->taps.h_taps + 1), 2), 19);
737*b843c749SSergey Zigachev 
738*b843c749SSergey Zigachev 	data->inits.h_c = dc_fixpt_truncate(dc_fixpt_add(data->inits.h_c, dc_fixpt_div_int(
739*b843c749SSergey Zigachev 			dc_fixpt_add_int(data->ratios.horz_c, data->taps.h_taps_c + 1), 2)), 19);
740*b843c749SSergey Zigachev 
741*b843c749SSergey Zigachev 	data->inits.v = dc_fixpt_truncate(dc_fixpt_div_int(
742*b843c749SSergey Zigachev 			dc_fixpt_add_int(data->ratios.vert, data->taps.v_taps + 1), 2), 19);
743*b843c749SSergey Zigachev 
744*b843c749SSergey Zigachev 	data->inits.v_c = dc_fixpt_truncate(dc_fixpt_add(data->inits.v_c, dc_fixpt_div_int(
745*b843c749SSergey Zigachev 			dc_fixpt_add_int(data->ratios.vert_c, data->taps.v_taps_c + 1), 2)), 19);
746*b843c749SSergey Zigachev 
747*b843c749SSergey Zigachev 	if (!flip_horz_scan_dir) {
748*b843c749SSergey Zigachev 		/* Adjust for viewport end clip-off */
749*b843c749SSergey Zigachev 		if ((data->viewport.x + data->viewport.width) < (src.x + src.width)) {
750*b843c749SSergey Zigachev 			int vp_clip = src.x + src.width - data->viewport.width - data->viewport.x;
751*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
752*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.h, data->ratios.horz));
753*b843c749SSergey Zigachev 
754*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
755*b843c749SSergey Zigachev 			data->viewport.width += int_part < vp_clip ? int_part : vp_clip;
756*b843c749SSergey Zigachev 		}
757*b843c749SSergey Zigachev 		if ((data->viewport_c.x + data->viewport_c.width) < (src.x + src.width) / vpc_div) {
758*b843c749SSergey Zigachev 			int vp_clip = (src.x + src.width) / vpc_div -
759*b843c749SSergey Zigachev 					data->viewport_c.width - data->viewport_c.x;
760*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
761*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.h_c, data->ratios.horz_c));
762*b843c749SSergey Zigachev 
763*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
764*b843c749SSergey Zigachev 			data->viewport_c.width += int_part < vp_clip ? int_part : vp_clip;
765*b843c749SSergey Zigachev 		}
766*b843c749SSergey Zigachev 
767*b843c749SSergey Zigachev 		/* Adjust for non-0 viewport offset */
768*b843c749SSergey Zigachev 		if (data->viewport.x) {
769*b843c749SSergey Zigachev 			int int_part;
770*b843c749SSergey Zigachev 
771*b843c749SSergey Zigachev 			data->inits.h = dc_fixpt_add(data->inits.h, dc_fixpt_mul_int(
772*b843c749SSergey Zigachev 					data->ratios.horz, data->recout.x - recout_full->x));
773*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.h) - data->viewport.x;
774*b843c749SSergey Zigachev 			if (int_part < data->taps.h_taps) {
775*b843c749SSergey Zigachev 				int int_adj = data->viewport.x >= (data->taps.h_taps - int_part) ?
776*b843c749SSergey Zigachev 							(data->taps.h_taps - int_part) : data->viewport.x;
777*b843c749SSergey Zigachev 				data->viewport.x -= int_adj;
778*b843c749SSergey Zigachev 				data->viewport.width += int_adj;
779*b843c749SSergey Zigachev 				int_part += int_adj;
780*b843c749SSergey Zigachev 			} else if (int_part > data->taps.h_taps) {
781*b843c749SSergey Zigachev 				data->viewport.x += int_part - data->taps.h_taps;
782*b843c749SSergey Zigachev 				data->viewport.width -= int_part - data->taps.h_taps;
783*b843c749SSergey Zigachev 				int_part = data->taps.h_taps;
784*b843c749SSergey Zigachev 			}
785*b843c749SSergey Zigachev 			data->inits.h.value &= 0xffffffff;
786*b843c749SSergey Zigachev 			data->inits.h = dc_fixpt_add_int(data->inits.h, int_part);
787*b843c749SSergey Zigachev 		}
788*b843c749SSergey Zigachev 
789*b843c749SSergey Zigachev 		if (data->viewport_c.x) {
790*b843c749SSergey Zigachev 			int int_part;
791*b843c749SSergey Zigachev 
792*b843c749SSergey Zigachev 			data->inits.h_c = dc_fixpt_add(data->inits.h_c, dc_fixpt_mul_int(
793*b843c749SSergey Zigachev 					data->ratios.horz_c, data->recout.x - recout_full->x));
794*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.h_c) - data->viewport_c.x;
795*b843c749SSergey Zigachev 			if (int_part < data->taps.h_taps_c) {
796*b843c749SSergey Zigachev 				int int_adj = data->viewport_c.x >= (data->taps.h_taps_c - int_part) ?
797*b843c749SSergey Zigachev 						(data->taps.h_taps_c - int_part) : data->viewport_c.x;
798*b843c749SSergey Zigachev 				data->viewport_c.x -= int_adj;
799*b843c749SSergey Zigachev 				data->viewport_c.width += int_adj;
800*b843c749SSergey Zigachev 				int_part += int_adj;
801*b843c749SSergey Zigachev 			} else if (int_part > data->taps.h_taps_c) {
802*b843c749SSergey Zigachev 				data->viewport_c.x += int_part - data->taps.h_taps_c;
803*b843c749SSergey Zigachev 				data->viewport_c.width -= int_part - data->taps.h_taps_c;
804*b843c749SSergey Zigachev 				int_part = data->taps.h_taps_c;
805*b843c749SSergey Zigachev 			}
806*b843c749SSergey Zigachev 			data->inits.h_c.value &= 0xffffffff;
807*b843c749SSergey Zigachev 			data->inits.h_c = dc_fixpt_add_int(data->inits.h_c, int_part);
808*b843c749SSergey Zigachev 		}
809*b843c749SSergey Zigachev 	} else {
810*b843c749SSergey Zigachev 		/* Adjust for non-0 viewport offset */
811*b843c749SSergey Zigachev 		if (data->viewport.x) {
812*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
813*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.h, data->ratios.horz));
814*b843c749SSergey Zigachev 
815*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
816*b843c749SSergey Zigachev 			data->viewport.width += int_part < data->viewport.x ? int_part : data->viewport.x;
817*b843c749SSergey Zigachev 			data->viewport.x -= int_part < data->viewport.x ? int_part : data->viewport.x;
818*b843c749SSergey Zigachev 		}
819*b843c749SSergey Zigachev 		if (data->viewport_c.x) {
820*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
821*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.h_c, data->ratios.horz_c));
822*b843c749SSergey Zigachev 
823*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
824*b843c749SSergey Zigachev 			data->viewport_c.width += int_part < data->viewport_c.x ? int_part : data->viewport_c.x;
825*b843c749SSergey Zigachev 			data->viewport_c.x -= int_part < data->viewport_c.x ? int_part : data->viewport_c.x;
826*b843c749SSergey Zigachev 		}
827*b843c749SSergey Zigachev 
828*b843c749SSergey Zigachev 		/* Adjust for viewport end clip-off */
829*b843c749SSergey Zigachev 		if ((data->viewport.x + data->viewport.width) < (src.x + src.width)) {
830*b843c749SSergey Zigachev 			int int_part;
831*b843c749SSergey Zigachev 			int end_offset = src.x + src.width
832*b843c749SSergey Zigachev 					- data->viewport.x - data->viewport.width;
833*b843c749SSergey Zigachev 
834*b843c749SSergey Zigachev 			/*
835*b843c749SSergey Zigachev 			 * this is init if vp had no offset, keep in mind this is from the
836*b843c749SSergey Zigachev 			 * right side of vp due to scan direction
837*b843c749SSergey Zigachev 			 */
838*b843c749SSergey Zigachev 			data->inits.h = dc_fixpt_add(data->inits.h, dc_fixpt_mul_int(
839*b843c749SSergey Zigachev 					data->ratios.horz, data->recout.x - recout_full->x));
840*b843c749SSergey Zigachev 			/*
841*b843c749SSergey Zigachev 			 * this is the difference between first pixel of viewport available to read
842*b843c749SSergey Zigachev 			 * and init position, takning into account scan direction
843*b843c749SSergey Zigachev 			 */
844*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.h) - end_offset;
845*b843c749SSergey Zigachev 			if (int_part < data->taps.h_taps) {
846*b843c749SSergey Zigachev 				int int_adj = end_offset >= (data->taps.h_taps - int_part) ?
847*b843c749SSergey Zigachev 							(data->taps.h_taps - int_part) : end_offset;
848*b843c749SSergey Zigachev 				data->viewport.width += int_adj;
849*b843c749SSergey Zigachev 				int_part += int_adj;
850*b843c749SSergey Zigachev 			} else if (int_part > data->taps.h_taps) {
851*b843c749SSergey Zigachev 				data->viewport.width += int_part - data->taps.h_taps;
852*b843c749SSergey Zigachev 				int_part = data->taps.h_taps;
853*b843c749SSergey Zigachev 			}
854*b843c749SSergey Zigachev 			data->inits.h.value &= 0xffffffff;
855*b843c749SSergey Zigachev 			data->inits.h = dc_fixpt_add_int(data->inits.h, int_part);
856*b843c749SSergey Zigachev 		}
857*b843c749SSergey Zigachev 
858*b843c749SSergey Zigachev 		if ((data->viewport_c.x + data->viewport_c.width) < (src.x + src.width) / vpc_div) {
859*b843c749SSergey Zigachev 			int int_part;
860*b843c749SSergey Zigachev 			int end_offset = (src.x + src.width) / vpc_div
861*b843c749SSergey Zigachev 					- data->viewport_c.x - data->viewport_c.width;
862*b843c749SSergey Zigachev 
863*b843c749SSergey Zigachev 			/*
864*b843c749SSergey Zigachev 			 * this is init if vp had no offset, keep in mind this is from the
865*b843c749SSergey Zigachev 			 * right side of vp due to scan direction
866*b843c749SSergey Zigachev 			 */
867*b843c749SSergey Zigachev 			data->inits.h_c = dc_fixpt_add(data->inits.h_c, dc_fixpt_mul_int(
868*b843c749SSergey Zigachev 					data->ratios.horz_c, data->recout.x - recout_full->x));
869*b843c749SSergey Zigachev 			/*
870*b843c749SSergey Zigachev 			 * this is the difference between first pixel of viewport available to read
871*b843c749SSergey Zigachev 			 * and init position, takning into account scan direction
872*b843c749SSergey Zigachev 			 */
873*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.h_c) - end_offset;
874*b843c749SSergey Zigachev 			if (int_part < data->taps.h_taps_c) {
875*b843c749SSergey Zigachev 				int int_adj = end_offset >= (data->taps.h_taps_c - int_part) ?
876*b843c749SSergey Zigachev 							(data->taps.h_taps_c - int_part) : end_offset;
877*b843c749SSergey Zigachev 				data->viewport_c.width += int_adj;
878*b843c749SSergey Zigachev 				int_part += int_adj;
879*b843c749SSergey Zigachev 			} else if (int_part > data->taps.h_taps_c) {
880*b843c749SSergey Zigachev 				data->viewport_c.width += int_part - data->taps.h_taps_c;
881*b843c749SSergey Zigachev 				int_part = data->taps.h_taps_c;
882*b843c749SSergey Zigachev 			}
883*b843c749SSergey Zigachev 			data->inits.h_c.value &= 0xffffffff;
884*b843c749SSergey Zigachev 			data->inits.h_c = dc_fixpt_add_int(data->inits.h_c, int_part);
885*b843c749SSergey Zigachev 		}
886*b843c749SSergey Zigachev 
887*b843c749SSergey Zigachev 	}
888*b843c749SSergey Zigachev 	if (!flip_vert_scan_dir) {
889*b843c749SSergey Zigachev 		/* Adjust for viewport end clip-off */
890*b843c749SSergey Zigachev 		if ((data->viewport.y + data->viewport.height) < (src.y + src.height)) {
891*b843c749SSergey Zigachev 			int vp_clip = src.y + src.height - data->viewport.height - data->viewport.y;
892*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
893*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.v, data->ratios.vert));
894*b843c749SSergey Zigachev 
895*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
896*b843c749SSergey Zigachev 			data->viewport.height += int_part < vp_clip ? int_part : vp_clip;
897*b843c749SSergey Zigachev 		}
898*b843c749SSergey Zigachev 		if ((data->viewport_c.y + data->viewport_c.height) < (src.y + src.height) / vpc_div) {
899*b843c749SSergey Zigachev 			int vp_clip = (src.y + src.height) / vpc_div -
900*b843c749SSergey Zigachev 					data->viewport_c.height - data->viewport_c.y;
901*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
902*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.v_c, data->ratios.vert_c));
903*b843c749SSergey Zigachev 
904*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
905*b843c749SSergey Zigachev 			data->viewport_c.height += int_part < vp_clip ? int_part : vp_clip;
906*b843c749SSergey Zigachev 		}
907*b843c749SSergey Zigachev 
908*b843c749SSergey Zigachev 		/* Adjust for non-0 viewport offset */
909*b843c749SSergey Zigachev 		if (data->viewport.y) {
910*b843c749SSergey Zigachev 			int int_part;
911*b843c749SSergey Zigachev 
912*b843c749SSergey Zigachev 			data->inits.v = dc_fixpt_add(data->inits.v, dc_fixpt_mul_int(
913*b843c749SSergey Zigachev 					data->ratios.vert, data->recout.y - recout_full->y));
914*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.v) - data->viewport.y;
915*b843c749SSergey Zigachev 			if (int_part < data->taps.v_taps) {
916*b843c749SSergey Zigachev 				int int_adj = data->viewport.y >= (data->taps.v_taps - int_part) ?
917*b843c749SSergey Zigachev 							(data->taps.v_taps - int_part) : data->viewport.y;
918*b843c749SSergey Zigachev 				data->viewport.y -= int_adj;
919*b843c749SSergey Zigachev 				data->viewport.height += int_adj;
920*b843c749SSergey Zigachev 				int_part += int_adj;
921*b843c749SSergey Zigachev 			} else if (int_part > data->taps.v_taps) {
922*b843c749SSergey Zigachev 				data->viewport.y += int_part - data->taps.v_taps;
923*b843c749SSergey Zigachev 				data->viewport.height -= int_part - data->taps.v_taps;
924*b843c749SSergey Zigachev 				int_part = data->taps.v_taps;
925*b843c749SSergey Zigachev 			}
926*b843c749SSergey Zigachev 			data->inits.v.value &= 0xffffffff;
927*b843c749SSergey Zigachev 			data->inits.v = dc_fixpt_add_int(data->inits.v, int_part);
928*b843c749SSergey Zigachev 		}
929*b843c749SSergey Zigachev 
930*b843c749SSergey Zigachev 		if (data->viewport_c.y) {
931*b843c749SSergey Zigachev 			int int_part;
932*b843c749SSergey Zigachev 
933*b843c749SSergey Zigachev 			data->inits.v_c = dc_fixpt_add(data->inits.v_c, dc_fixpt_mul_int(
934*b843c749SSergey Zigachev 					data->ratios.vert_c, data->recout.y - recout_full->y));
935*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.v_c) - data->viewport_c.y;
936*b843c749SSergey Zigachev 			if (int_part < data->taps.v_taps_c) {
937*b843c749SSergey Zigachev 				int int_adj = data->viewport_c.y >= (data->taps.v_taps_c - int_part) ?
938*b843c749SSergey Zigachev 						(data->taps.v_taps_c - int_part) : data->viewport_c.y;
939*b843c749SSergey Zigachev 				data->viewport_c.y -= int_adj;
940*b843c749SSergey Zigachev 				data->viewport_c.height += int_adj;
941*b843c749SSergey Zigachev 				int_part += int_adj;
942*b843c749SSergey Zigachev 			} else if (int_part > data->taps.v_taps_c) {
943*b843c749SSergey Zigachev 				data->viewport_c.y += int_part - data->taps.v_taps_c;
944*b843c749SSergey Zigachev 				data->viewport_c.height -= int_part - data->taps.v_taps_c;
945*b843c749SSergey Zigachev 				int_part = data->taps.v_taps_c;
946*b843c749SSergey Zigachev 			}
947*b843c749SSergey Zigachev 			data->inits.v_c.value &= 0xffffffff;
948*b843c749SSergey Zigachev 			data->inits.v_c = dc_fixpt_add_int(data->inits.v_c, int_part);
949*b843c749SSergey Zigachev 		}
950*b843c749SSergey Zigachev 	} else {
951*b843c749SSergey Zigachev 		/* Adjust for non-0 viewport offset */
952*b843c749SSergey Zigachev 		if (data->viewport.y) {
953*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
954*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.v, data->ratios.vert));
955*b843c749SSergey Zigachev 
956*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
957*b843c749SSergey Zigachev 			data->viewport.height += int_part < data->viewport.y ? int_part : data->viewport.y;
958*b843c749SSergey Zigachev 			data->viewport.y -= int_part < data->viewport.y ? int_part : data->viewport.y;
959*b843c749SSergey Zigachev 		}
960*b843c749SSergey Zigachev 		if (data->viewport_c.y) {
961*b843c749SSergey Zigachev 			int int_part = dc_fixpt_floor(
962*b843c749SSergey Zigachev 					dc_fixpt_sub(data->inits.v_c, data->ratios.vert_c));
963*b843c749SSergey Zigachev 
964*b843c749SSergey Zigachev 			int_part = int_part > 0 ? int_part : 0;
965*b843c749SSergey Zigachev 			data->viewport_c.height += int_part < data->viewport_c.y ? int_part : data->viewport_c.y;
966*b843c749SSergey Zigachev 			data->viewport_c.y -= int_part < data->viewport_c.y ? int_part : data->viewport_c.y;
967*b843c749SSergey Zigachev 		}
968*b843c749SSergey Zigachev 
969*b843c749SSergey Zigachev 		/* Adjust for viewport end clip-off */
970*b843c749SSergey Zigachev 		if ((data->viewport.y + data->viewport.height) < (src.y + src.height)) {
971*b843c749SSergey Zigachev 			int int_part;
972*b843c749SSergey Zigachev 			int end_offset = src.y + src.height
973*b843c749SSergey Zigachev 					- data->viewport.y - data->viewport.height;
974*b843c749SSergey Zigachev 
975*b843c749SSergey Zigachev 			/*
976*b843c749SSergey Zigachev 			 * this is init if vp had no offset, keep in mind this is from the
977*b843c749SSergey Zigachev 			 * right side of vp due to scan direction
978*b843c749SSergey Zigachev 			 */
979*b843c749SSergey Zigachev 			data->inits.v = dc_fixpt_add(data->inits.v, dc_fixpt_mul_int(
980*b843c749SSergey Zigachev 					data->ratios.vert, data->recout.y - recout_full->y));
981*b843c749SSergey Zigachev 			/*
982*b843c749SSergey Zigachev 			 * this is the difference between first pixel of viewport available to read
983*b843c749SSergey Zigachev 			 * and init position, taking into account scan direction
984*b843c749SSergey Zigachev 			 */
985*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.v) - end_offset;
986*b843c749SSergey Zigachev 			if (int_part < data->taps.v_taps) {
987*b843c749SSergey Zigachev 				int int_adj = end_offset >= (data->taps.v_taps - int_part) ?
988*b843c749SSergey Zigachev 							(data->taps.v_taps - int_part) : end_offset;
989*b843c749SSergey Zigachev 				data->viewport.height += int_adj;
990*b843c749SSergey Zigachev 				int_part += int_adj;
991*b843c749SSergey Zigachev 			} else if (int_part > data->taps.v_taps) {
992*b843c749SSergey Zigachev 				data->viewport.height += int_part - data->taps.v_taps;
993*b843c749SSergey Zigachev 				int_part = data->taps.v_taps;
994*b843c749SSergey Zigachev 			}
995*b843c749SSergey Zigachev 			data->inits.v.value &= 0xffffffff;
996*b843c749SSergey Zigachev 			data->inits.v = dc_fixpt_add_int(data->inits.v, int_part);
997*b843c749SSergey Zigachev 		}
998*b843c749SSergey Zigachev 
999*b843c749SSergey Zigachev 		if ((data->viewport_c.y + data->viewport_c.height) < (src.y + src.height) / vpc_div) {
1000*b843c749SSergey Zigachev 			int int_part;
1001*b843c749SSergey Zigachev 			int end_offset = (src.y + src.height) / vpc_div
1002*b843c749SSergey Zigachev 					- data->viewport_c.y - data->viewport_c.height;
1003*b843c749SSergey Zigachev 
1004*b843c749SSergey Zigachev 			/*
1005*b843c749SSergey Zigachev 			 * this is init if vp had no offset, keep in mind this is from the
1006*b843c749SSergey Zigachev 			 * right side of vp due to scan direction
1007*b843c749SSergey Zigachev 			 */
1008*b843c749SSergey Zigachev 			data->inits.v_c = dc_fixpt_add(data->inits.v_c, dc_fixpt_mul_int(
1009*b843c749SSergey Zigachev 					data->ratios.vert_c, data->recout.y - recout_full->y));
1010*b843c749SSergey Zigachev 			/*
1011*b843c749SSergey Zigachev 			 * this is the difference between first pixel of viewport available to read
1012*b843c749SSergey Zigachev 			 * and init position, taking into account scan direction
1013*b843c749SSergey Zigachev 			 */
1014*b843c749SSergey Zigachev 			int_part = dc_fixpt_floor(data->inits.v_c) - end_offset;
1015*b843c749SSergey Zigachev 			if (int_part < data->taps.v_taps_c) {
1016*b843c749SSergey Zigachev 				int int_adj = end_offset >= (data->taps.v_taps_c - int_part) ?
1017*b843c749SSergey Zigachev 							(data->taps.v_taps_c - int_part) : end_offset;
1018*b843c749SSergey Zigachev 				data->viewport_c.height += int_adj;
1019*b843c749SSergey Zigachev 				int_part += int_adj;
1020*b843c749SSergey Zigachev 			} else if (int_part > data->taps.v_taps_c) {
1021*b843c749SSergey Zigachev 				data->viewport_c.height += int_part - data->taps.v_taps_c;
1022*b843c749SSergey Zigachev 				int_part = data->taps.v_taps_c;
1023*b843c749SSergey Zigachev 			}
1024*b843c749SSergey Zigachev 			data->inits.v_c.value &= 0xffffffff;
1025*b843c749SSergey Zigachev 			data->inits.v_c = dc_fixpt_add_int(data->inits.v_c, int_part);
1026*b843c749SSergey Zigachev 		}
1027*b843c749SSergey Zigachev 	}
1028*b843c749SSergey Zigachev 
1029*b843c749SSergey Zigachev 	/* Interlaced inits based on final vert inits */
1030*b843c749SSergey Zigachev 	data->inits.v_bot = dc_fixpt_add(data->inits.v, data->ratios.vert);
1031*b843c749SSergey Zigachev 	data->inits.v_c_bot = dc_fixpt_add(data->inits.v_c, data->ratios.vert_c);
1032*b843c749SSergey Zigachev 
1033*b843c749SSergey Zigachev 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
1034*b843c749SSergey Zigachev 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270) {
1035*b843c749SSergey Zigachev 		rect_swap_helper(&data->viewport_c);
1036*b843c749SSergey Zigachev 		rect_swap_helper(&data->viewport);
1037*b843c749SSergey Zigachev 	}
1038*b843c749SSergey Zigachev }
1039*b843c749SSergey Zigachev 
resource_build_scaling_params(struct pipe_ctx * pipe_ctx)1040*b843c749SSergey Zigachev bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
1041*b843c749SSergey Zigachev {
1042*b843c749SSergey Zigachev 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1043*b843c749SSergey Zigachev 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
1044*b843c749SSergey Zigachev 	struct rect recout_full = { 0 };
1045*b843c749SSergey Zigachev 	bool res = false;
1046*b843c749SSergey Zigachev 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1047*b843c749SSergey Zigachev 	/* Important: scaling ratio calculation requires pixel format,
1048*b843c749SSergey Zigachev 	 * lb depth calculation requires recout and taps require scaling ratios.
1049*b843c749SSergey Zigachev 	 * Inits require viewport, taps, ratios and recout of split pipe
1050*b843c749SSergey Zigachev 	 */
1051*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
1052*b843c749SSergey Zigachev 			pipe_ctx->plane_state->format);
1053*b843c749SSergey Zigachev 
1054*b843c749SSergey Zigachev 	if (pipe_ctx->stream->timing.flags.INTERLACE)
1055*b843c749SSergey Zigachev 		pipe_ctx->stream->dst.height *= 2;
1056*b843c749SSergey Zigachev 
1057*b843c749SSergey Zigachev 	calculate_scaling_ratios(pipe_ctx);
1058*b843c749SSergey Zigachev 
1059*b843c749SSergey Zigachev 	calculate_viewport(pipe_ctx);
1060*b843c749SSergey Zigachev 
1061*b843c749SSergey Zigachev 	if (pipe_ctx->plane_res.scl_data.viewport.height < 16 || pipe_ctx->plane_res.scl_data.viewport.width < 16)
1062*b843c749SSergey Zigachev 		return false;
1063*b843c749SSergey Zigachev 
1064*b843c749SSergey Zigachev 	calculate_recout(pipe_ctx, &recout_full);
1065*b843c749SSergey Zigachev 
1066*b843c749SSergey Zigachev 	/**
1067*b843c749SSergey Zigachev 	 * Setting line buffer pixel depth to 24bpp yields banding
1068*b843c749SSergey Zigachev 	 * on certain displays, such as the Sharp 4k
1069*b843c749SSergey Zigachev 	 */
1070*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1071*b843c749SSergey Zigachev 
1072*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.recout.x += timing->h_border_left;
1073*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.recout.y += timing->v_border_top;
1074*b843c749SSergey Zigachev 
1075*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.h_active = timing->h_addressable + timing->h_border_left + timing->h_border_right;
1076*b843c749SSergey Zigachev 	pipe_ctx->plane_res.scl_data.v_active = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
1077*b843c749SSergey Zigachev 	if (pipe_ctx->stream->timing.flags.INTERLACE)
1078*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.v_active *= 2;
1079*b843c749SSergey Zigachev 
1080*b843c749SSergey Zigachev 
1081*b843c749SSergey Zigachev 	/* Taps calculations */
1082*b843c749SSergey Zigachev 	if (pipe_ctx->plane_res.xfm != NULL)
1083*b843c749SSergey Zigachev 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1084*b843c749SSergey Zigachev 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1085*b843c749SSergey Zigachev 
1086*b843c749SSergey Zigachev 	if (pipe_ctx->plane_res.dpp != NULL)
1087*b843c749SSergey Zigachev 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1088*b843c749SSergey Zigachev 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1089*b843c749SSergey Zigachev 	if (!res) {
1090*b843c749SSergey Zigachev 		/* Try 24 bpp linebuffer */
1091*b843c749SSergey Zigachev 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
1092*b843c749SSergey Zigachev 
1093*b843c749SSergey Zigachev 		if (pipe_ctx->plane_res.xfm != NULL)
1094*b843c749SSergey Zigachev 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1095*b843c749SSergey Zigachev 					pipe_ctx->plane_res.xfm,
1096*b843c749SSergey Zigachev 					&pipe_ctx->plane_res.scl_data,
1097*b843c749SSergey Zigachev 					&plane_state->scaling_quality);
1098*b843c749SSergey Zigachev 
1099*b843c749SSergey Zigachev 		if (pipe_ctx->plane_res.dpp != NULL)
1100*b843c749SSergey Zigachev 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1101*b843c749SSergey Zigachev 					pipe_ctx->plane_res.dpp,
1102*b843c749SSergey Zigachev 					&pipe_ctx->plane_res.scl_data,
1103*b843c749SSergey Zigachev 					&plane_state->scaling_quality);
1104*b843c749SSergey Zigachev 	}
1105*b843c749SSergey Zigachev 
1106*b843c749SSergey Zigachev 	if (res)
1107*b843c749SSergey Zigachev 		/* May need to re-check lb size after this in some obscure scenario */
1108*b843c749SSergey Zigachev 		calculate_inits_and_adj_vp(pipe_ctx, &recout_full);
1109*b843c749SSergey Zigachev 
1110*b843c749SSergey Zigachev 	DC_LOG_SCALER(
1111*b843c749SSergey Zigachev 				"%s: Viewport:\nheight:%d width:%d x:%d "
1112*b843c749SSergey Zigachev 				"y:%d\n dst_rect:\nheight:%d width:%d x:%d "
1113*b843c749SSergey Zigachev 				"y:%d\n",
1114*b843c749SSergey Zigachev 				__func__,
1115*b843c749SSergey Zigachev 				pipe_ctx->plane_res.scl_data.viewport.height,
1116*b843c749SSergey Zigachev 				pipe_ctx->plane_res.scl_data.viewport.width,
1117*b843c749SSergey Zigachev 				pipe_ctx->plane_res.scl_data.viewport.x,
1118*b843c749SSergey Zigachev 				pipe_ctx->plane_res.scl_data.viewport.y,
1119*b843c749SSergey Zigachev 				plane_state->dst_rect.height,
1120*b843c749SSergey Zigachev 				plane_state->dst_rect.width,
1121*b843c749SSergey Zigachev 				plane_state->dst_rect.x,
1122*b843c749SSergey Zigachev 				plane_state->dst_rect.y);
1123*b843c749SSergey Zigachev 
1124*b843c749SSergey Zigachev 	if (pipe_ctx->stream->timing.flags.INTERLACE)
1125*b843c749SSergey Zigachev 		pipe_ctx->stream->dst.height /= 2;
1126*b843c749SSergey Zigachev 
1127*b843c749SSergey Zigachev 	return res;
1128*b843c749SSergey Zigachev }
1129*b843c749SSergey Zigachev 
1130*b843c749SSergey Zigachev 
resource_build_scaling_params_for_context(const struct dc * dc,struct dc_state * context)1131*b843c749SSergey Zigachev enum dc_status resource_build_scaling_params_for_context(
1132*b843c749SSergey Zigachev 	const struct dc  *dc,
1133*b843c749SSergey Zigachev 	struct dc_state *context)
1134*b843c749SSergey Zigachev {
1135*b843c749SSergey Zigachev 	int i;
1136*b843c749SSergey Zigachev 
1137*b843c749SSergey Zigachev 	for (i = 0; i < MAX_PIPES; i++) {
1138*b843c749SSergey Zigachev 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
1139*b843c749SSergey Zigachev 				context->res_ctx.pipe_ctx[i].stream != NULL)
1140*b843c749SSergey Zigachev 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1141*b843c749SSergey Zigachev 				return DC_FAIL_SCALING;
1142*b843c749SSergey Zigachev 	}
1143*b843c749SSergey Zigachev 
1144*b843c749SSergey Zigachev 	return DC_OK;
1145*b843c749SSergey Zigachev }
1146*b843c749SSergey Zigachev 
find_idle_secondary_pipe(struct resource_context * res_ctx,const struct resource_pool * pool)1147*b843c749SSergey Zigachev struct pipe_ctx *find_idle_secondary_pipe(
1148*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1149*b843c749SSergey Zigachev 		const struct resource_pool *pool)
1150*b843c749SSergey Zigachev {
1151*b843c749SSergey Zigachev 	int i;
1152*b843c749SSergey Zigachev 	struct pipe_ctx *secondary_pipe = NULL;
1153*b843c749SSergey Zigachev 
1154*b843c749SSergey Zigachev 	/*
1155*b843c749SSergey Zigachev 	 * search backwards for the second pipe to keep pipe
1156*b843c749SSergey Zigachev 	 * assignment more consistent
1157*b843c749SSergey Zigachev 	 */
1158*b843c749SSergey Zigachev 
1159*b843c749SSergey Zigachev 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1160*b843c749SSergey Zigachev 		if (res_ctx->pipe_ctx[i].stream == NULL) {
1161*b843c749SSergey Zigachev 			secondary_pipe = &res_ctx->pipe_ctx[i];
1162*b843c749SSergey Zigachev 			secondary_pipe->pipe_idx = i;
1163*b843c749SSergey Zigachev 			break;
1164*b843c749SSergey Zigachev 		}
1165*b843c749SSergey Zigachev 	}
1166*b843c749SSergey Zigachev 
1167*b843c749SSergey Zigachev 
1168*b843c749SSergey Zigachev 	return secondary_pipe;
1169*b843c749SSergey Zigachev }
1170*b843c749SSergey Zigachev 
resource_get_head_pipe_for_stream(struct resource_context * res_ctx,struct dc_stream_state * stream)1171*b843c749SSergey Zigachev struct pipe_ctx *resource_get_head_pipe_for_stream(
1172*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1173*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1174*b843c749SSergey Zigachev {
1175*b843c749SSergey Zigachev 	int i;
1176*b843c749SSergey Zigachev 	for (i = 0; i < MAX_PIPES; i++) {
1177*b843c749SSergey Zigachev 		if (res_ctx->pipe_ctx[i].stream == stream &&
1178*b843c749SSergey Zigachev 				!res_ctx->pipe_ctx[i].top_pipe) {
1179*b843c749SSergey Zigachev 			return &res_ctx->pipe_ctx[i];
1180*b843c749SSergey Zigachev 			break;
1181*b843c749SSergey Zigachev 		}
1182*b843c749SSergey Zigachev 	}
1183*b843c749SSergey Zigachev 	return NULL;
1184*b843c749SSergey Zigachev }
1185*b843c749SSergey Zigachev 
resource_get_tail_pipe_for_stream(struct resource_context * res_ctx,struct dc_stream_state * stream)1186*b843c749SSergey Zigachev static struct pipe_ctx *resource_get_tail_pipe_for_stream(
1187*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1188*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1189*b843c749SSergey Zigachev {
1190*b843c749SSergey Zigachev 	struct pipe_ctx *head_pipe, *tail_pipe;
1191*b843c749SSergey Zigachev 	head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
1192*b843c749SSergey Zigachev 
1193*b843c749SSergey Zigachev 	if (!head_pipe)
1194*b843c749SSergey Zigachev 		return NULL;
1195*b843c749SSergey Zigachev 
1196*b843c749SSergey Zigachev 	tail_pipe = head_pipe->bottom_pipe;
1197*b843c749SSergey Zigachev 
1198*b843c749SSergey Zigachev 	while (tail_pipe) {
1199*b843c749SSergey Zigachev 		head_pipe = tail_pipe;
1200*b843c749SSergey Zigachev 		tail_pipe = tail_pipe->bottom_pipe;
1201*b843c749SSergey Zigachev 	}
1202*b843c749SSergey Zigachev 
1203*b843c749SSergey Zigachev 	return head_pipe;
1204*b843c749SSergey Zigachev }
1205*b843c749SSergey Zigachev 
1206*b843c749SSergey Zigachev /*
1207*b843c749SSergey Zigachev  * A free_pipe for a stream is defined here as a pipe
1208*b843c749SSergey Zigachev  * that has no surface attached yet
1209*b843c749SSergey Zigachev  */
acquire_free_pipe_for_stream(struct dc_state * context,const struct resource_pool * pool,struct dc_stream_state * stream)1210*b843c749SSergey Zigachev static struct pipe_ctx *acquire_free_pipe_for_stream(
1211*b843c749SSergey Zigachev 		struct dc_state *context,
1212*b843c749SSergey Zigachev 		const struct resource_pool *pool,
1213*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1214*b843c749SSergey Zigachev {
1215*b843c749SSergey Zigachev 	int i;
1216*b843c749SSergey Zigachev 	struct resource_context *res_ctx = &context->res_ctx;
1217*b843c749SSergey Zigachev 
1218*b843c749SSergey Zigachev 	struct pipe_ctx *head_pipe = NULL;
1219*b843c749SSergey Zigachev 
1220*b843c749SSergey Zigachev 	/* Find head pipe, which has the back end set up*/
1221*b843c749SSergey Zigachev 
1222*b843c749SSergey Zigachev 	head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
1223*b843c749SSergey Zigachev 
1224*b843c749SSergey Zigachev 	if (!head_pipe) {
1225*b843c749SSergey Zigachev 		ASSERT(0);
1226*b843c749SSergey Zigachev 		return NULL;
1227*b843c749SSergey Zigachev 	}
1228*b843c749SSergey Zigachev 
1229*b843c749SSergey Zigachev 	if (!head_pipe->plane_state)
1230*b843c749SSergey Zigachev 		return head_pipe;
1231*b843c749SSergey Zigachev 
1232*b843c749SSergey Zigachev 	/* Re-use pipe already acquired for this stream if available*/
1233*b843c749SSergey Zigachev 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1234*b843c749SSergey Zigachev 		if (res_ctx->pipe_ctx[i].stream == stream &&
1235*b843c749SSergey Zigachev 				!res_ctx->pipe_ctx[i].plane_state) {
1236*b843c749SSergey Zigachev 			return &res_ctx->pipe_ctx[i];
1237*b843c749SSergey Zigachev 		}
1238*b843c749SSergey Zigachev 	}
1239*b843c749SSergey Zigachev 
1240*b843c749SSergey Zigachev 	/*
1241*b843c749SSergey Zigachev 	 * At this point we have no re-useable pipe for this stream and we need
1242*b843c749SSergey Zigachev 	 * to acquire an idle one to satisfy the request
1243*b843c749SSergey Zigachev 	 */
1244*b843c749SSergey Zigachev 
1245*b843c749SSergey Zigachev 	if (!pool->funcs->acquire_idle_pipe_for_layer)
1246*b843c749SSergey Zigachev 		return NULL;
1247*b843c749SSergey Zigachev 
1248*b843c749SSergey Zigachev 	return pool->funcs->acquire_idle_pipe_for_layer(context, pool, stream);
1249*b843c749SSergey Zigachev 
1250*b843c749SSergey Zigachev }
1251*b843c749SSergey Zigachev 
1252*b843c749SSergey Zigachev #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
acquire_first_split_pipe(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)1253*b843c749SSergey Zigachev static int acquire_first_split_pipe(
1254*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1255*b843c749SSergey Zigachev 		const struct resource_pool *pool,
1256*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1257*b843c749SSergey Zigachev {
1258*b843c749SSergey Zigachev 	int i;
1259*b843c749SSergey Zigachev 
1260*b843c749SSergey Zigachev 	for (i = 0; i < pool->pipe_count; i++) {
1261*b843c749SSergey Zigachev 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
1262*b843c749SSergey Zigachev 
1263*b843c749SSergey Zigachev 		if (pipe_ctx->top_pipe &&
1264*b843c749SSergey Zigachev 				pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state) {
1265*b843c749SSergey Zigachev 			pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
1266*b843c749SSergey Zigachev 			if (pipe_ctx->bottom_pipe)
1267*b843c749SSergey Zigachev 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
1268*b843c749SSergey Zigachev 
1269*b843c749SSergey Zigachev 			memset(pipe_ctx, 0, sizeof(*pipe_ctx));
1270*b843c749SSergey Zigachev 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
1271*b843c749SSergey Zigachev 			pipe_ctx->plane_res.hubp = pool->hubps[i];
1272*b843c749SSergey Zigachev 			pipe_ctx->plane_res.ipp = pool->ipps[i];
1273*b843c749SSergey Zigachev 			pipe_ctx->plane_res.dpp = pool->dpps[i];
1274*b843c749SSergey Zigachev 			pipe_ctx->stream_res.opp = pool->opps[i];
1275*b843c749SSergey Zigachev 			pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
1276*b843c749SSergey Zigachev 			pipe_ctx->pipe_idx = i;
1277*b843c749SSergey Zigachev 
1278*b843c749SSergey Zigachev 			pipe_ctx->stream = stream;
1279*b843c749SSergey Zigachev 			return i;
1280*b843c749SSergey Zigachev 		}
1281*b843c749SSergey Zigachev 	}
1282*b843c749SSergey Zigachev 	return -1;
1283*b843c749SSergey Zigachev }
1284*b843c749SSergey Zigachev #endif
1285*b843c749SSergey Zigachev 
dc_add_plane_to_context(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * plane_state,struct dc_state * context)1286*b843c749SSergey Zigachev bool dc_add_plane_to_context(
1287*b843c749SSergey Zigachev 		const struct dc *dc,
1288*b843c749SSergey Zigachev 		struct dc_stream_state *stream,
1289*b843c749SSergey Zigachev 		struct dc_plane_state *plane_state,
1290*b843c749SSergey Zigachev 		struct dc_state *context)
1291*b843c749SSergey Zigachev {
1292*b843c749SSergey Zigachev 	int i;
1293*b843c749SSergey Zigachev 	struct resource_pool *pool = dc->res_pool;
1294*b843c749SSergey Zigachev 	struct pipe_ctx *head_pipe, *tail_pipe, *free_pipe;
1295*b843c749SSergey Zigachev 	struct dc_stream_status *stream_status = NULL;
1296*b843c749SSergey Zigachev 
1297*b843c749SSergey Zigachev 	for (i = 0; i < context->stream_count; i++)
1298*b843c749SSergey Zigachev 		if (context->streams[i] == stream) {
1299*b843c749SSergey Zigachev 			stream_status = &context->stream_status[i];
1300*b843c749SSergey Zigachev 			break;
1301*b843c749SSergey Zigachev 		}
1302*b843c749SSergey Zigachev 	if (stream_status == NULL) {
1303*b843c749SSergey Zigachev 		dm_error("Existing stream not found; failed to attach surface!\n");
1304*b843c749SSergey Zigachev 		return false;
1305*b843c749SSergey Zigachev 	}
1306*b843c749SSergey Zigachev 
1307*b843c749SSergey Zigachev 
1308*b843c749SSergey Zigachev 	if (stream_status->plane_count == MAX_SURFACE_NUM) {
1309*b843c749SSergey Zigachev 		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
1310*b843c749SSergey Zigachev 				plane_state, MAX_SURFACE_NUM);
1311*b843c749SSergey Zigachev 		return false;
1312*b843c749SSergey Zigachev 	}
1313*b843c749SSergey Zigachev 
1314*b843c749SSergey Zigachev 	head_pipe = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
1315*b843c749SSergey Zigachev 
1316*b843c749SSergey Zigachev 	if (!head_pipe) {
1317*b843c749SSergey Zigachev 		dm_error("Head pipe not found for stream_state %p !\n", stream);
1318*b843c749SSergey Zigachev 		return false;
1319*b843c749SSergey Zigachev 	}
1320*b843c749SSergey Zigachev 
1321*b843c749SSergey Zigachev 	free_pipe = acquire_free_pipe_for_stream(context, pool, stream);
1322*b843c749SSergey Zigachev 
1323*b843c749SSergey Zigachev #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1324*b843c749SSergey Zigachev 	if (!free_pipe) {
1325*b843c749SSergey Zigachev 		int pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
1326*b843c749SSergey Zigachev 		if (pipe_idx >= 0)
1327*b843c749SSergey Zigachev 			free_pipe = &context->res_ctx.pipe_ctx[pipe_idx];
1328*b843c749SSergey Zigachev 	}
1329*b843c749SSergey Zigachev #endif
1330*b843c749SSergey Zigachev 	if (!free_pipe)
1331*b843c749SSergey Zigachev 		return false;
1332*b843c749SSergey Zigachev 
1333*b843c749SSergey Zigachev 	/* retain new surfaces */
1334*b843c749SSergey Zigachev 	dc_plane_state_retain(plane_state);
1335*b843c749SSergey Zigachev 	free_pipe->plane_state = plane_state;
1336*b843c749SSergey Zigachev 
1337*b843c749SSergey Zigachev 	if (head_pipe != free_pipe) {
1338*b843c749SSergey Zigachev 
1339*b843c749SSergey Zigachev 		tail_pipe = resource_get_tail_pipe_for_stream(&context->res_ctx, stream);
1340*b843c749SSergey Zigachev 		ASSERT(tail_pipe);
1341*b843c749SSergey Zigachev 
1342*b843c749SSergey Zigachev 		free_pipe->stream_res.tg = tail_pipe->stream_res.tg;
1343*b843c749SSergey Zigachev 		free_pipe->stream_res.abm = tail_pipe->stream_res.abm;
1344*b843c749SSergey Zigachev 		free_pipe->stream_res.opp = tail_pipe->stream_res.opp;
1345*b843c749SSergey Zigachev 		free_pipe->stream_res.stream_enc = tail_pipe->stream_res.stream_enc;
1346*b843c749SSergey Zigachev 		free_pipe->stream_res.audio = tail_pipe->stream_res.audio;
1347*b843c749SSergey Zigachev 		free_pipe->clock_source = tail_pipe->clock_source;
1348*b843c749SSergey Zigachev 		free_pipe->top_pipe = tail_pipe;
1349*b843c749SSergey Zigachev 		tail_pipe->bottom_pipe = free_pipe;
1350*b843c749SSergey Zigachev 	}
1351*b843c749SSergey Zigachev 
1352*b843c749SSergey Zigachev 	/* assign new surfaces*/
1353*b843c749SSergey Zigachev 	stream_status->plane_states[stream_status->plane_count] = plane_state;
1354*b843c749SSergey Zigachev 
1355*b843c749SSergey Zigachev 	stream_status->plane_count++;
1356*b843c749SSergey Zigachev 
1357*b843c749SSergey Zigachev 	return true;
1358*b843c749SSergey Zigachev }
1359*b843c749SSergey Zigachev 
dc_remove_plane_from_context(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * plane_state,struct dc_state * context)1360*b843c749SSergey Zigachev bool dc_remove_plane_from_context(
1361*b843c749SSergey Zigachev 		const struct dc *dc,
1362*b843c749SSergey Zigachev 		struct dc_stream_state *stream,
1363*b843c749SSergey Zigachev 		struct dc_plane_state *plane_state,
1364*b843c749SSergey Zigachev 		struct dc_state *context)
1365*b843c749SSergey Zigachev {
1366*b843c749SSergey Zigachev 	int i;
1367*b843c749SSergey Zigachev 	struct dc_stream_status *stream_status = NULL;
1368*b843c749SSergey Zigachev 	struct resource_pool *pool = dc->res_pool;
1369*b843c749SSergey Zigachev 
1370*b843c749SSergey Zigachev 	for (i = 0; i < context->stream_count; i++)
1371*b843c749SSergey Zigachev 		if (context->streams[i] == stream) {
1372*b843c749SSergey Zigachev 			stream_status = &context->stream_status[i];
1373*b843c749SSergey Zigachev 			break;
1374*b843c749SSergey Zigachev 		}
1375*b843c749SSergey Zigachev 
1376*b843c749SSergey Zigachev 	if (stream_status == NULL) {
1377*b843c749SSergey Zigachev 		dm_error("Existing stream not found; failed to remove plane.\n");
1378*b843c749SSergey Zigachev 		return false;
1379*b843c749SSergey Zigachev 	}
1380*b843c749SSergey Zigachev 
1381*b843c749SSergey Zigachev 	/* release pipe for plane*/
1382*b843c749SSergey Zigachev 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1383*b843c749SSergey Zigachev 		struct pipe_ctx *pipe_ctx;
1384*b843c749SSergey Zigachev 
1385*b843c749SSergey Zigachev 		if (context->res_ctx.pipe_ctx[i].plane_state == plane_state) {
1386*b843c749SSergey Zigachev 			pipe_ctx = &context->res_ctx.pipe_ctx[i];
1387*b843c749SSergey Zigachev 
1388*b843c749SSergey Zigachev 			if (pipe_ctx->top_pipe)
1389*b843c749SSergey Zigachev 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
1390*b843c749SSergey Zigachev 
1391*b843c749SSergey Zigachev 			/* Second condition is to avoid setting NULL to top pipe
1392*b843c749SSergey Zigachev 			 * of tail pipe making it look like head pipe in subsequent
1393*b843c749SSergey Zigachev 			 * deletes
1394*b843c749SSergey Zigachev 			 */
1395*b843c749SSergey Zigachev 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
1396*b843c749SSergey Zigachev 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
1397*b843c749SSergey Zigachev 
1398*b843c749SSergey Zigachev 			/*
1399*b843c749SSergey Zigachev 			 * For head pipe detach surfaces from pipe for tail
1400*b843c749SSergey Zigachev 			 * pipe just zero it out
1401*b843c749SSergey Zigachev 			 */
1402*b843c749SSergey Zigachev 			if (!pipe_ctx->top_pipe || (!pipe_ctx->top_pipe->top_pipe &&
1403*b843c749SSergey Zigachev 					pipe_ctx->top_pipe->stream_res.opp != pipe_ctx->stream_res.opp)) {
1404*b843c749SSergey Zigachev 				pipe_ctx->top_pipe = NULL;
1405*b843c749SSergey Zigachev 				pipe_ctx->plane_state = NULL;
1406*b843c749SSergey Zigachev 				pipe_ctx->bottom_pipe = NULL;
1407*b843c749SSergey Zigachev 			} else {
1408*b843c749SSergey Zigachev 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
1409*b843c749SSergey Zigachev 			}
1410*b843c749SSergey Zigachev 		}
1411*b843c749SSergey Zigachev 	}
1412*b843c749SSergey Zigachev 
1413*b843c749SSergey Zigachev 
1414*b843c749SSergey Zigachev 	for (i = 0; i < stream_status->plane_count; i++) {
1415*b843c749SSergey Zigachev 		if (stream_status->plane_states[i] == plane_state) {
1416*b843c749SSergey Zigachev 
1417*b843c749SSergey Zigachev 			dc_plane_state_release(stream_status->plane_states[i]);
1418*b843c749SSergey Zigachev 			break;
1419*b843c749SSergey Zigachev 		}
1420*b843c749SSergey Zigachev 	}
1421*b843c749SSergey Zigachev 
1422*b843c749SSergey Zigachev 	if (i == stream_status->plane_count) {
1423*b843c749SSergey Zigachev 		dm_error("Existing plane_state not found; failed to detach it!\n");
1424*b843c749SSergey Zigachev 		return false;
1425*b843c749SSergey Zigachev 	}
1426*b843c749SSergey Zigachev 
1427*b843c749SSergey Zigachev 	stream_status->plane_count--;
1428*b843c749SSergey Zigachev 
1429*b843c749SSergey Zigachev 	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
1430*b843c749SSergey Zigachev 	for (; i < stream_status->plane_count; i++)
1431*b843c749SSergey Zigachev 		stream_status->plane_states[i] = stream_status->plane_states[i + 1];
1432*b843c749SSergey Zigachev 
1433*b843c749SSergey Zigachev 	stream_status->plane_states[stream_status->plane_count] = NULL;
1434*b843c749SSergey Zigachev 
1435*b843c749SSergey Zigachev 	return true;
1436*b843c749SSergey Zigachev }
1437*b843c749SSergey Zigachev 
dc_rem_all_planes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)1438*b843c749SSergey Zigachev bool dc_rem_all_planes_for_stream(
1439*b843c749SSergey Zigachev 		const struct dc *dc,
1440*b843c749SSergey Zigachev 		struct dc_stream_state *stream,
1441*b843c749SSergey Zigachev 		struct dc_state *context)
1442*b843c749SSergey Zigachev {
1443*b843c749SSergey Zigachev 	int i, old_plane_count;
1444*b843c749SSergey Zigachev 	struct dc_stream_status *stream_status = NULL;
1445*b843c749SSergey Zigachev 	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
1446*b843c749SSergey Zigachev 
1447*b843c749SSergey Zigachev 	for (i = 0; i < context->stream_count; i++)
1448*b843c749SSergey Zigachev 			if (context->streams[i] == stream) {
1449*b843c749SSergey Zigachev 				stream_status = &context->stream_status[i];
1450*b843c749SSergey Zigachev 				break;
1451*b843c749SSergey Zigachev 			}
1452*b843c749SSergey Zigachev 
1453*b843c749SSergey Zigachev 	if (stream_status == NULL) {
1454*b843c749SSergey Zigachev 		dm_error("Existing stream %p not found!\n", stream);
1455*b843c749SSergey Zigachev 		return false;
1456*b843c749SSergey Zigachev 	}
1457*b843c749SSergey Zigachev 
1458*b843c749SSergey Zigachev 	old_plane_count = stream_status->plane_count;
1459*b843c749SSergey Zigachev 
1460*b843c749SSergey Zigachev 	for (i = 0; i < old_plane_count; i++)
1461*b843c749SSergey Zigachev 		del_planes[i] = stream_status->plane_states[i];
1462*b843c749SSergey Zigachev 
1463*b843c749SSergey Zigachev 	for (i = 0; i < old_plane_count; i++)
1464*b843c749SSergey Zigachev 		if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
1465*b843c749SSergey Zigachev 			return false;
1466*b843c749SSergey Zigachev 
1467*b843c749SSergey Zigachev 	return true;
1468*b843c749SSergey Zigachev }
1469*b843c749SSergey Zigachev 
add_all_planes_for_stream(const struct dc * dc,struct dc_stream_state * stream,const struct dc_validation_set set[],int set_count,struct dc_state * context)1470*b843c749SSergey Zigachev static bool add_all_planes_for_stream(
1471*b843c749SSergey Zigachev 		const struct dc *dc,
1472*b843c749SSergey Zigachev 		struct dc_stream_state *stream,
1473*b843c749SSergey Zigachev 		const struct dc_validation_set set[],
1474*b843c749SSergey Zigachev 		int set_count,
1475*b843c749SSergey Zigachev 		struct dc_state *context)
1476*b843c749SSergey Zigachev {
1477*b843c749SSergey Zigachev 	int i, j;
1478*b843c749SSergey Zigachev 
1479*b843c749SSergey Zigachev 	for (i = 0; i < set_count; i++)
1480*b843c749SSergey Zigachev 		if (set[i].stream == stream)
1481*b843c749SSergey Zigachev 			break;
1482*b843c749SSergey Zigachev 
1483*b843c749SSergey Zigachev 	if (i == set_count) {
1484*b843c749SSergey Zigachev 		dm_error("Stream %p not found in set!\n", stream);
1485*b843c749SSergey Zigachev 		return false;
1486*b843c749SSergey Zigachev 	}
1487*b843c749SSergey Zigachev 
1488*b843c749SSergey Zigachev 	for (j = 0; j < set[i].plane_count; j++)
1489*b843c749SSergey Zigachev 		if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
1490*b843c749SSergey Zigachev 			return false;
1491*b843c749SSergey Zigachev 
1492*b843c749SSergey Zigachev 	return true;
1493*b843c749SSergey Zigachev }
1494*b843c749SSergey Zigachev 
dc_add_all_planes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * const * plane_states,int plane_count,struct dc_state * context)1495*b843c749SSergey Zigachev bool dc_add_all_planes_for_stream(
1496*b843c749SSergey Zigachev 		const struct dc *dc,
1497*b843c749SSergey Zigachev 		struct dc_stream_state *stream,
1498*b843c749SSergey Zigachev 		struct dc_plane_state * const *plane_states,
1499*b843c749SSergey Zigachev 		int plane_count,
1500*b843c749SSergey Zigachev 		struct dc_state *context)
1501*b843c749SSergey Zigachev {
1502*b843c749SSergey Zigachev 	struct dc_validation_set set;
1503*b843c749SSergey Zigachev 	int i;
1504*b843c749SSergey Zigachev 
1505*b843c749SSergey Zigachev 	set.stream = stream;
1506*b843c749SSergey Zigachev 	set.plane_count = plane_count;
1507*b843c749SSergey Zigachev 
1508*b843c749SSergey Zigachev 	for (i = 0; i < plane_count; i++)
1509*b843c749SSergey Zigachev 		set.plane_states[i] = plane_states[i];
1510*b843c749SSergey Zigachev 
1511*b843c749SSergey Zigachev 	return add_all_planes_for_stream(dc, stream, &set, 1, context);
1512*b843c749SSergey Zigachev }
1513*b843c749SSergey Zigachev 
1514*b843c749SSergey Zigachev 
is_hdr_static_meta_changed(struct dc_stream_state * cur_stream,struct dc_stream_state * new_stream)1515*b843c749SSergey Zigachev static bool is_hdr_static_meta_changed(struct dc_stream_state *cur_stream,
1516*b843c749SSergey Zigachev 	struct dc_stream_state *new_stream)
1517*b843c749SSergey Zigachev {
1518*b843c749SSergey Zigachev 	if (cur_stream == NULL)
1519*b843c749SSergey Zigachev 		return true;
1520*b843c749SSergey Zigachev 
1521*b843c749SSergey Zigachev 	if (memcmp(&cur_stream->hdr_static_metadata,
1522*b843c749SSergey Zigachev 			&new_stream->hdr_static_metadata,
1523*b843c749SSergey Zigachev 			sizeof(struct dc_info_packet)) != 0)
1524*b843c749SSergey Zigachev 		return true;
1525*b843c749SSergey Zigachev 
1526*b843c749SSergey Zigachev 	return false;
1527*b843c749SSergey Zigachev }
1528*b843c749SSergey Zigachev 
is_timing_changed(struct dc_stream_state * cur_stream,struct dc_stream_state * new_stream)1529*b843c749SSergey Zigachev static bool is_timing_changed(struct dc_stream_state *cur_stream,
1530*b843c749SSergey Zigachev 		struct dc_stream_state *new_stream)
1531*b843c749SSergey Zigachev {
1532*b843c749SSergey Zigachev 	if (cur_stream == NULL)
1533*b843c749SSergey Zigachev 		return true;
1534*b843c749SSergey Zigachev 
1535*b843c749SSergey Zigachev 	/* If sink pointer changed, it means this is a hotplug, we should do
1536*b843c749SSergey Zigachev 	 * full hw setting.
1537*b843c749SSergey Zigachev 	 */
1538*b843c749SSergey Zigachev 	if (cur_stream->sink != new_stream->sink)
1539*b843c749SSergey Zigachev 		return true;
1540*b843c749SSergey Zigachev 
1541*b843c749SSergey Zigachev 	/* If output color space is changed, need to reprogram info frames */
1542*b843c749SSergey Zigachev 	if (cur_stream->output_color_space != new_stream->output_color_space)
1543*b843c749SSergey Zigachev 		return true;
1544*b843c749SSergey Zigachev 
1545*b843c749SSergey Zigachev 	return memcmp(
1546*b843c749SSergey Zigachev 		&cur_stream->timing,
1547*b843c749SSergey Zigachev 		&new_stream->timing,
1548*b843c749SSergey Zigachev 		sizeof(struct dc_crtc_timing)) != 0;
1549*b843c749SSergey Zigachev }
1550*b843c749SSergey Zigachev 
are_stream_backends_same(struct dc_stream_state * stream_a,struct dc_stream_state * stream_b)1551*b843c749SSergey Zigachev static bool are_stream_backends_same(
1552*b843c749SSergey Zigachev 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
1553*b843c749SSergey Zigachev {
1554*b843c749SSergey Zigachev 	if (stream_a == stream_b)
1555*b843c749SSergey Zigachev 		return true;
1556*b843c749SSergey Zigachev 
1557*b843c749SSergey Zigachev 	if (stream_a == NULL || stream_b == NULL)
1558*b843c749SSergey Zigachev 		return false;
1559*b843c749SSergey Zigachev 
1560*b843c749SSergey Zigachev 	if (is_timing_changed(stream_a, stream_b))
1561*b843c749SSergey Zigachev 		return false;
1562*b843c749SSergey Zigachev 
1563*b843c749SSergey Zigachev 	if (is_hdr_static_meta_changed(stream_a, stream_b))
1564*b843c749SSergey Zigachev 		return false;
1565*b843c749SSergey Zigachev 
1566*b843c749SSergey Zigachev 	return true;
1567*b843c749SSergey Zigachev }
1568*b843c749SSergey Zigachev 
dc_is_stream_unchanged(struct dc_stream_state * old_stream,struct dc_stream_state * stream)1569*b843c749SSergey Zigachev bool dc_is_stream_unchanged(
1570*b843c749SSergey Zigachev 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
1571*b843c749SSergey Zigachev {
1572*b843c749SSergey Zigachev 
1573*b843c749SSergey Zigachev 	if (!are_stream_backends_same(old_stream, stream))
1574*b843c749SSergey Zigachev 		return false;
1575*b843c749SSergey Zigachev 
1576*b843c749SSergey Zigachev 	return true;
1577*b843c749SSergey Zigachev }
1578*b843c749SSergey Zigachev 
dc_is_stream_scaling_unchanged(struct dc_stream_state * old_stream,struct dc_stream_state * stream)1579*b843c749SSergey Zigachev bool dc_is_stream_scaling_unchanged(
1580*b843c749SSergey Zigachev 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
1581*b843c749SSergey Zigachev {
1582*b843c749SSergey Zigachev 	if (old_stream == stream)
1583*b843c749SSergey Zigachev 		return true;
1584*b843c749SSergey Zigachev 
1585*b843c749SSergey Zigachev 	if (old_stream == NULL || stream == NULL)
1586*b843c749SSergey Zigachev 		return false;
1587*b843c749SSergey Zigachev 
1588*b843c749SSergey Zigachev 	if (memcmp(&old_stream->src,
1589*b843c749SSergey Zigachev 			&stream->src,
1590*b843c749SSergey Zigachev 			sizeof(struct rect)) != 0)
1591*b843c749SSergey Zigachev 		return false;
1592*b843c749SSergey Zigachev 
1593*b843c749SSergey Zigachev 	if (memcmp(&old_stream->dst,
1594*b843c749SSergey Zigachev 			&stream->dst,
1595*b843c749SSergey Zigachev 			sizeof(struct rect)) != 0)
1596*b843c749SSergey Zigachev 		return false;
1597*b843c749SSergey Zigachev 
1598*b843c749SSergey Zigachev 	return true;
1599*b843c749SSergey Zigachev }
1600*b843c749SSergey Zigachev 
update_stream_engine_usage(struct resource_context * res_ctx,const struct resource_pool * pool,struct stream_encoder * stream_enc,bool acquired)1601*b843c749SSergey Zigachev static void update_stream_engine_usage(
1602*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1603*b843c749SSergey Zigachev 		const struct resource_pool *pool,
1604*b843c749SSergey Zigachev 		struct stream_encoder *stream_enc,
1605*b843c749SSergey Zigachev 		bool acquired)
1606*b843c749SSergey Zigachev {
1607*b843c749SSergey Zigachev 	int i;
1608*b843c749SSergey Zigachev 
1609*b843c749SSergey Zigachev 	for (i = 0; i < pool->stream_enc_count; i++) {
1610*b843c749SSergey Zigachev 		if (pool->stream_enc[i] == stream_enc)
1611*b843c749SSergey Zigachev 			res_ctx->is_stream_enc_acquired[i] = acquired;
1612*b843c749SSergey Zigachev 	}
1613*b843c749SSergey Zigachev }
1614*b843c749SSergey Zigachev 
1615*b843c749SSergey Zigachev /* TODO: release audio object */
update_audio_usage(struct resource_context * res_ctx,const struct resource_pool * pool,struct audio * audio,bool acquired)1616*b843c749SSergey Zigachev void update_audio_usage(
1617*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1618*b843c749SSergey Zigachev 		const struct resource_pool *pool,
1619*b843c749SSergey Zigachev 		struct audio *audio,
1620*b843c749SSergey Zigachev 		bool acquired)
1621*b843c749SSergey Zigachev {
1622*b843c749SSergey Zigachev 	int i;
1623*b843c749SSergey Zigachev 	for (i = 0; i < pool->audio_count; i++) {
1624*b843c749SSergey Zigachev 		if (pool->audios[i] == audio)
1625*b843c749SSergey Zigachev 			res_ctx->is_audio_acquired[i] = acquired;
1626*b843c749SSergey Zigachev 	}
1627*b843c749SSergey Zigachev }
1628*b843c749SSergey Zigachev 
acquire_first_free_pipe(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)1629*b843c749SSergey Zigachev static int acquire_first_free_pipe(
1630*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1631*b843c749SSergey Zigachev 		const struct resource_pool *pool,
1632*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1633*b843c749SSergey Zigachev {
1634*b843c749SSergey Zigachev 	int i;
1635*b843c749SSergey Zigachev 
1636*b843c749SSergey Zigachev 	for (i = 0; i < pool->pipe_count; i++) {
1637*b843c749SSergey Zigachev 		if (!res_ctx->pipe_ctx[i].stream) {
1638*b843c749SSergey Zigachev 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
1639*b843c749SSergey Zigachev 
1640*b843c749SSergey Zigachev 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
1641*b843c749SSergey Zigachev 			pipe_ctx->plane_res.mi = pool->mis[i];
1642*b843c749SSergey Zigachev 			pipe_ctx->plane_res.hubp = pool->hubps[i];
1643*b843c749SSergey Zigachev 			pipe_ctx->plane_res.ipp = pool->ipps[i];
1644*b843c749SSergey Zigachev 			pipe_ctx->plane_res.xfm = pool->transforms[i];
1645*b843c749SSergey Zigachev 			pipe_ctx->plane_res.dpp = pool->dpps[i];
1646*b843c749SSergey Zigachev 			pipe_ctx->stream_res.opp = pool->opps[i];
1647*b843c749SSergey Zigachev 			if (pool->dpps[i])
1648*b843c749SSergey Zigachev 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
1649*b843c749SSergey Zigachev 			pipe_ctx->pipe_idx = i;
1650*b843c749SSergey Zigachev 
1651*b843c749SSergey Zigachev 
1652*b843c749SSergey Zigachev 			pipe_ctx->stream = stream;
1653*b843c749SSergey Zigachev 			return i;
1654*b843c749SSergey Zigachev 		}
1655*b843c749SSergey Zigachev 	}
1656*b843c749SSergey Zigachev 	return -1;
1657*b843c749SSergey Zigachev }
1658*b843c749SSergey Zigachev 
find_first_free_match_stream_enc_for_link(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)1659*b843c749SSergey Zigachev static struct stream_encoder *find_first_free_match_stream_enc_for_link(
1660*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1661*b843c749SSergey Zigachev 		const struct resource_pool *pool,
1662*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1663*b843c749SSergey Zigachev {
1664*b843c749SSergey Zigachev 	int i;
1665*b843c749SSergey Zigachev 	int j = -1;
1666*b843c749SSergey Zigachev 	struct dc_link *link = stream->sink->link;
1667*b843c749SSergey Zigachev 
1668*b843c749SSergey Zigachev 	for (i = 0; i < pool->stream_enc_count; i++) {
1669*b843c749SSergey Zigachev 		if (!res_ctx->is_stream_enc_acquired[i] &&
1670*b843c749SSergey Zigachev 				pool->stream_enc[i]) {
1671*b843c749SSergey Zigachev 			/* Store first available for MST second display
1672*b843c749SSergey Zigachev 			 * in daisy chain use case */
1673*b843c749SSergey Zigachev 			j = i;
1674*b843c749SSergey Zigachev 			if (pool->stream_enc[i]->id ==
1675*b843c749SSergey Zigachev 					link->link_enc->preferred_engine)
1676*b843c749SSergey Zigachev 				return pool->stream_enc[i];
1677*b843c749SSergey Zigachev 		}
1678*b843c749SSergey Zigachev 	}
1679*b843c749SSergey Zigachev 
1680*b843c749SSergey Zigachev 	/*
1681*b843c749SSergey Zigachev 	 * below can happen in cases when stream encoder is acquired:
1682*b843c749SSergey Zigachev 	 * 1) for second MST display in chain, so preferred engine already
1683*b843c749SSergey Zigachev 	 * acquired;
1684*b843c749SSergey Zigachev 	 * 2) for another link, which preferred engine already acquired by any
1685*b843c749SSergey Zigachev 	 * MST configuration.
1686*b843c749SSergey Zigachev 	 *
1687*b843c749SSergey Zigachev 	 * If signal is of DP type and preferred engine not found, return last available
1688*b843c749SSergey Zigachev 	 *
1689*b843c749SSergey Zigachev 	 * TODO - This is just a patch up and a generic solution is
1690*b843c749SSergey Zigachev 	 * required for non DP connectors.
1691*b843c749SSergey Zigachev 	 */
1692*b843c749SSergey Zigachev 
1693*b843c749SSergey Zigachev 	if (j >= 0 && dc_is_dp_signal(stream->signal))
1694*b843c749SSergey Zigachev 		return pool->stream_enc[j];
1695*b843c749SSergey Zigachev 
1696*b843c749SSergey Zigachev 	return NULL;
1697*b843c749SSergey Zigachev }
1698*b843c749SSergey Zigachev 
find_first_free_audio(struct resource_context * res_ctx,const struct resource_pool * pool,enum engine_id id)1699*b843c749SSergey Zigachev static struct audio *find_first_free_audio(
1700*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
1701*b843c749SSergey Zigachev 		const struct resource_pool *pool,
1702*b843c749SSergey Zigachev 		enum engine_id id)
1703*b843c749SSergey Zigachev {
1704*b843c749SSergey Zigachev 	int i, available_audio_count;
1705*b843c749SSergey Zigachev 
1706*b843c749SSergey Zigachev 	available_audio_count = pool->audio_count;
1707*b843c749SSergey Zigachev 
1708*b843c749SSergey Zigachev 	for (i = 0; i < available_audio_count; i++) {
1709*b843c749SSergey Zigachev 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
1710*b843c749SSergey Zigachev 			/*we have enough audio endpoint, find the matching inst*/
1711*b843c749SSergey Zigachev 			if (id != i)
1712*b843c749SSergey Zigachev 				continue;
1713*b843c749SSergey Zigachev 			return pool->audios[i];
1714*b843c749SSergey Zigachev 		}
1715*b843c749SSergey Zigachev 	}
1716*b843c749SSergey Zigachev 
1717*b843c749SSergey Zigachev 	/* use engine id to find free audio */
1718*b843c749SSergey Zigachev 	if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
1719*b843c749SSergey Zigachev 		return pool->audios[id];
1720*b843c749SSergey Zigachev 	}
1721*b843c749SSergey Zigachev 	/*not found the matching one, first come first serve*/
1722*b843c749SSergey Zigachev 	for (i = 0; i < available_audio_count; i++) {
1723*b843c749SSergey Zigachev 		if (res_ctx->is_audio_acquired[i] == false) {
1724*b843c749SSergey Zigachev 			return pool->audios[i];
1725*b843c749SSergey Zigachev 		}
1726*b843c749SSergey Zigachev 	}
1727*b843c749SSergey Zigachev 	return 0;
1728*b843c749SSergey Zigachev }
1729*b843c749SSergey Zigachev 
resource_is_stream_unchanged(struct dc_state * old_context,struct dc_stream_state * stream)1730*b843c749SSergey Zigachev bool resource_is_stream_unchanged(
1731*b843c749SSergey Zigachev 	struct dc_state *old_context, struct dc_stream_state *stream)
1732*b843c749SSergey Zigachev {
1733*b843c749SSergey Zigachev 	int i;
1734*b843c749SSergey Zigachev 
1735*b843c749SSergey Zigachev 	for (i = 0; i < old_context->stream_count; i++) {
1736*b843c749SSergey Zigachev 		struct dc_stream_state *old_stream = old_context->streams[i];
1737*b843c749SSergey Zigachev 
1738*b843c749SSergey Zigachev 		if (are_stream_backends_same(old_stream, stream))
1739*b843c749SSergey Zigachev 				return true;
1740*b843c749SSergey Zigachev 	}
1741*b843c749SSergey Zigachev 
1742*b843c749SSergey Zigachev 	return false;
1743*b843c749SSergey Zigachev }
1744*b843c749SSergey Zigachev 
dc_add_stream_to_ctx(struct dc * dc,struct dc_state * new_ctx,struct dc_stream_state * stream)1745*b843c749SSergey Zigachev enum dc_status dc_add_stream_to_ctx(
1746*b843c749SSergey Zigachev 		struct dc *dc,
1747*b843c749SSergey Zigachev 		struct dc_state *new_ctx,
1748*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1749*b843c749SSergey Zigachev {
1750*b843c749SSergey Zigachev 	struct dc_context *dc_ctx = dc->ctx;
1751*b843c749SSergey Zigachev 	enum dc_status res;
1752*b843c749SSergey Zigachev 
1753*b843c749SSergey Zigachev 	if (new_ctx->stream_count >= dc->res_pool->timing_generator_count) {
1754*b843c749SSergey Zigachev 		DC_ERROR("Max streams reached, can't add stream %p !\n", stream);
1755*b843c749SSergey Zigachev 		return DC_ERROR_UNEXPECTED;
1756*b843c749SSergey Zigachev 	}
1757*b843c749SSergey Zigachev 
1758*b843c749SSergey Zigachev 	new_ctx->streams[new_ctx->stream_count] = stream;
1759*b843c749SSergey Zigachev 	dc_stream_retain(stream);
1760*b843c749SSergey Zigachev 	new_ctx->stream_count++;
1761*b843c749SSergey Zigachev 
1762*b843c749SSergey Zigachev 	res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
1763*b843c749SSergey Zigachev 	if (res != DC_OK)
1764*b843c749SSergey Zigachev 		DC_ERROR("Adding stream %p to context failed with err %d!\n", stream, res);
1765*b843c749SSergey Zigachev 
1766*b843c749SSergey Zigachev 	return res;
1767*b843c749SSergey Zigachev }
1768*b843c749SSergey Zigachev 
dc_remove_stream_from_ctx(struct dc * dc,struct dc_state * new_ctx,struct dc_stream_state * stream)1769*b843c749SSergey Zigachev enum dc_status dc_remove_stream_from_ctx(
1770*b843c749SSergey Zigachev 			struct dc *dc,
1771*b843c749SSergey Zigachev 			struct dc_state *new_ctx,
1772*b843c749SSergey Zigachev 			struct dc_stream_state *stream)
1773*b843c749SSergey Zigachev {
1774*b843c749SSergey Zigachev 	int i;
1775*b843c749SSergey Zigachev 	struct dc_context *dc_ctx = dc->ctx;
1776*b843c749SSergey Zigachev 	struct pipe_ctx *del_pipe = NULL;
1777*b843c749SSergey Zigachev 
1778*b843c749SSergey Zigachev 	/* Release primary pipe */
1779*b843c749SSergey Zigachev 	for (i = 0; i < MAX_PIPES; i++) {
1780*b843c749SSergey Zigachev 		if (new_ctx->res_ctx.pipe_ctx[i].stream == stream &&
1781*b843c749SSergey Zigachev 				!new_ctx->res_ctx.pipe_ctx[i].top_pipe) {
1782*b843c749SSergey Zigachev 			del_pipe = &new_ctx->res_ctx.pipe_ctx[i];
1783*b843c749SSergey Zigachev 
1784*b843c749SSergey Zigachev 			ASSERT(del_pipe->stream_res.stream_enc);
1785*b843c749SSergey Zigachev 			update_stream_engine_usage(
1786*b843c749SSergey Zigachev 					&new_ctx->res_ctx,
1787*b843c749SSergey Zigachev 						dc->res_pool,
1788*b843c749SSergey Zigachev 					del_pipe->stream_res.stream_enc,
1789*b843c749SSergey Zigachev 					false);
1790*b843c749SSergey Zigachev 
1791*b843c749SSergey Zigachev 			if (del_pipe->stream_res.audio)
1792*b843c749SSergey Zigachev 				update_audio_usage(
1793*b843c749SSergey Zigachev 					&new_ctx->res_ctx,
1794*b843c749SSergey Zigachev 					dc->res_pool,
1795*b843c749SSergey Zigachev 					del_pipe->stream_res.audio,
1796*b843c749SSergey Zigachev 					false);
1797*b843c749SSergey Zigachev 
1798*b843c749SSergey Zigachev 			resource_unreference_clock_source(&new_ctx->res_ctx,
1799*b843c749SSergey Zigachev 							  dc->res_pool,
1800*b843c749SSergey Zigachev 							  del_pipe->clock_source);
1801*b843c749SSergey Zigachev 
1802*b843c749SSergey Zigachev 			if (dc->res_pool->funcs->remove_stream_from_ctx)
1803*b843c749SSergey Zigachev 				dc->res_pool->funcs->remove_stream_from_ctx(dc, new_ctx, stream);
1804*b843c749SSergey Zigachev 
1805*b843c749SSergey Zigachev 			memset(del_pipe, 0, sizeof(*del_pipe));
1806*b843c749SSergey Zigachev 		}
1807*b843c749SSergey Zigachev 	}
1808*b843c749SSergey Zigachev 
1809*b843c749SSergey Zigachev 	if (!del_pipe) {
1810*b843c749SSergey Zigachev 		DC_ERROR("Pipe not found for stream %p !\n", stream);
1811*b843c749SSergey Zigachev 		return DC_ERROR_UNEXPECTED;
1812*b843c749SSergey Zigachev 	}
1813*b843c749SSergey Zigachev 
1814*b843c749SSergey Zigachev 	for (i = 0; i < new_ctx->stream_count; i++)
1815*b843c749SSergey Zigachev 		if (new_ctx->streams[i] == stream)
1816*b843c749SSergey Zigachev 			break;
1817*b843c749SSergey Zigachev 
1818*b843c749SSergey Zigachev 	if (new_ctx->streams[i] != stream) {
1819*b843c749SSergey Zigachev 		DC_ERROR("Context doesn't have stream %p !\n", stream);
1820*b843c749SSergey Zigachev 		return DC_ERROR_UNEXPECTED;
1821*b843c749SSergey Zigachev 	}
1822*b843c749SSergey Zigachev 
1823*b843c749SSergey Zigachev 	dc_stream_release(new_ctx->streams[i]);
1824*b843c749SSergey Zigachev 	new_ctx->stream_count--;
1825*b843c749SSergey Zigachev 
1826*b843c749SSergey Zigachev 	/* Trim back arrays */
1827*b843c749SSergey Zigachev 	for (; i < new_ctx->stream_count; i++) {
1828*b843c749SSergey Zigachev 		new_ctx->streams[i] = new_ctx->streams[i + 1];
1829*b843c749SSergey Zigachev 		new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
1830*b843c749SSergey Zigachev 	}
1831*b843c749SSergey Zigachev 
1832*b843c749SSergey Zigachev 	new_ctx->streams[new_ctx->stream_count] = NULL;
1833*b843c749SSergey Zigachev 	memset(
1834*b843c749SSergey Zigachev 			&new_ctx->stream_status[new_ctx->stream_count],
1835*b843c749SSergey Zigachev 			0,
1836*b843c749SSergey Zigachev 			sizeof(new_ctx->stream_status[0]));
1837*b843c749SSergey Zigachev 
1838*b843c749SSergey Zigachev 	return DC_OK;
1839*b843c749SSergey Zigachev }
1840*b843c749SSergey Zigachev 
find_pll_sharable_stream(struct dc_stream_state * stream_needs_pll,struct dc_state * context)1841*b843c749SSergey Zigachev static struct dc_stream_state *find_pll_sharable_stream(
1842*b843c749SSergey Zigachev 		struct dc_stream_state *stream_needs_pll,
1843*b843c749SSergey Zigachev 		struct dc_state *context)
1844*b843c749SSergey Zigachev {
1845*b843c749SSergey Zigachev 	int i;
1846*b843c749SSergey Zigachev 
1847*b843c749SSergey Zigachev 	for (i = 0; i < context->stream_count; i++) {
1848*b843c749SSergey Zigachev 		struct dc_stream_state *stream_has_pll = context->streams[i];
1849*b843c749SSergey Zigachev 
1850*b843c749SSergey Zigachev 		/* We are looking for non dp, non virtual stream */
1851*b843c749SSergey Zigachev 		if (resource_are_streams_timing_synchronizable(
1852*b843c749SSergey Zigachev 			stream_needs_pll, stream_has_pll)
1853*b843c749SSergey Zigachev 			&& !dc_is_dp_signal(stream_has_pll->signal)
1854*b843c749SSergey Zigachev 			&& stream_has_pll->sink->link->connector_signal
1855*b843c749SSergey Zigachev 			!= SIGNAL_TYPE_VIRTUAL)
1856*b843c749SSergey Zigachev 			return stream_has_pll;
1857*b843c749SSergey Zigachev 
1858*b843c749SSergey Zigachev 	}
1859*b843c749SSergey Zigachev 
1860*b843c749SSergey Zigachev 	return NULL;
1861*b843c749SSergey Zigachev }
1862*b843c749SSergey Zigachev 
get_norm_pix_clk(const struct dc_crtc_timing * timing)1863*b843c749SSergey Zigachev static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
1864*b843c749SSergey Zigachev {
1865*b843c749SSergey Zigachev 	uint32_t pix_clk = timing->pix_clk_khz;
1866*b843c749SSergey Zigachev 	uint32_t normalized_pix_clk = pix_clk;
1867*b843c749SSergey Zigachev 
1868*b843c749SSergey Zigachev 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
1869*b843c749SSergey Zigachev 		pix_clk /= 2;
1870*b843c749SSergey Zigachev 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
1871*b843c749SSergey Zigachev 		switch (timing->display_color_depth) {
1872*b843c749SSergey Zigachev 		case COLOR_DEPTH_666:
1873*b843c749SSergey Zigachev 		case COLOR_DEPTH_888:
1874*b843c749SSergey Zigachev 			normalized_pix_clk = pix_clk;
1875*b843c749SSergey Zigachev 			break;
1876*b843c749SSergey Zigachev 		case COLOR_DEPTH_101010:
1877*b843c749SSergey Zigachev 			normalized_pix_clk = (pix_clk * 30) / 24;
1878*b843c749SSergey Zigachev 			break;
1879*b843c749SSergey Zigachev 		case COLOR_DEPTH_121212:
1880*b843c749SSergey Zigachev 			normalized_pix_clk = (pix_clk * 36) / 24;
1881*b843c749SSergey Zigachev 		break;
1882*b843c749SSergey Zigachev 		case COLOR_DEPTH_161616:
1883*b843c749SSergey Zigachev 			normalized_pix_clk = (pix_clk * 48) / 24;
1884*b843c749SSergey Zigachev 		break;
1885*b843c749SSergey Zigachev 		default:
1886*b843c749SSergey Zigachev 			ASSERT(0);
1887*b843c749SSergey Zigachev 		break;
1888*b843c749SSergey Zigachev 		}
1889*b843c749SSergey Zigachev 	}
1890*b843c749SSergey Zigachev 	return normalized_pix_clk;
1891*b843c749SSergey Zigachev }
1892*b843c749SSergey Zigachev 
calculate_phy_pix_clks(struct dc_stream_state * stream)1893*b843c749SSergey Zigachev static void calculate_phy_pix_clks(struct dc_stream_state *stream)
1894*b843c749SSergey Zigachev {
1895*b843c749SSergey Zigachev 	/* update actual pixel clock on all streams */
1896*b843c749SSergey Zigachev 	if (dc_is_hdmi_signal(stream->signal))
1897*b843c749SSergey Zigachev 		stream->phy_pix_clk = get_norm_pix_clk(
1898*b843c749SSergey Zigachev 			&stream->timing);
1899*b843c749SSergey Zigachev 	else
1900*b843c749SSergey Zigachev 		stream->phy_pix_clk =
1901*b843c749SSergey Zigachev 			stream->timing.pix_clk_khz;
1902*b843c749SSergey Zigachev }
1903*b843c749SSergey Zigachev 
resource_map_pool_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)1904*b843c749SSergey Zigachev enum dc_status resource_map_pool_resources(
1905*b843c749SSergey Zigachev 		const struct dc  *dc,
1906*b843c749SSergey Zigachev 		struct dc_state *context,
1907*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
1908*b843c749SSergey Zigachev {
1909*b843c749SSergey Zigachev 	const struct resource_pool *pool = dc->res_pool;
1910*b843c749SSergey Zigachev 	int i;
1911*b843c749SSergey Zigachev 	struct dc_context *dc_ctx = dc->ctx;
1912*b843c749SSergey Zigachev 	struct pipe_ctx *pipe_ctx = NULL;
1913*b843c749SSergey Zigachev 	int pipe_idx = -1;
1914*b843c749SSergey Zigachev 
1915*b843c749SSergey Zigachev 	/* TODO Check if this is needed */
1916*b843c749SSergey Zigachev 	/*if (!resource_is_stream_unchanged(old_context, stream)) {
1917*b843c749SSergey Zigachev 			if (stream != NULL && old_context->streams[i] != NULL) {
1918*b843c749SSergey Zigachev 				stream->bit_depth_params =
1919*b843c749SSergey Zigachev 						old_context->streams[i]->bit_depth_params;
1920*b843c749SSergey Zigachev 				stream->clamping = old_context->streams[i]->clamping;
1921*b843c749SSergey Zigachev 				continue;
1922*b843c749SSergey Zigachev 			}
1923*b843c749SSergey Zigachev 		}
1924*b843c749SSergey Zigachev 	*/
1925*b843c749SSergey Zigachev 
1926*b843c749SSergey Zigachev 	calculate_phy_pix_clks(stream);
1927*b843c749SSergey Zigachev 
1928*b843c749SSergey Zigachev 	/* acquire new resources */
1929*b843c749SSergey Zigachev 	pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
1930*b843c749SSergey Zigachev 
1931*b843c749SSergey Zigachev #ifdef CONFIG_DRM_AMD_DC_DCN1_0
1932*b843c749SSergey Zigachev 	if (pipe_idx < 0)
1933*b843c749SSergey Zigachev 		pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
1934*b843c749SSergey Zigachev #endif
1935*b843c749SSergey Zigachev 
1936*b843c749SSergey Zigachev 	if (pipe_idx < 0 || context->res_ctx.pipe_ctx[pipe_idx].stream_res.tg == NULL)
1937*b843c749SSergey Zigachev 		return DC_NO_CONTROLLER_RESOURCE;
1938*b843c749SSergey Zigachev 
1939*b843c749SSergey Zigachev 	pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
1940*b843c749SSergey Zigachev 
1941*b843c749SSergey Zigachev 	pipe_ctx->stream_res.stream_enc =
1942*b843c749SSergey Zigachev 		find_first_free_match_stream_enc_for_link(
1943*b843c749SSergey Zigachev 			&context->res_ctx, pool, stream);
1944*b843c749SSergey Zigachev 
1945*b843c749SSergey Zigachev 	if (!pipe_ctx->stream_res.stream_enc)
1946*b843c749SSergey Zigachev 		return DC_NO_STREAM_ENG_RESOURCE;
1947*b843c749SSergey Zigachev 
1948*b843c749SSergey Zigachev 	update_stream_engine_usage(
1949*b843c749SSergey Zigachev 		&context->res_ctx, pool,
1950*b843c749SSergey Zigachev 		pipe_ctx->stream_res.stream_enc,
1951*b843c749SSergey Zigachev 		true);
1952*b843c749SSergey Zigachev 
1953*b843c749SSergey Zigachev 	/* TODO: Add check if ASIC support and EDID audio */
1954*b843c749SSergey Zigachev 	if (!stream->sink->converter_disable_audio &&
1955*b843c749SSergey Zigachev 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
1956*b843c749SSergey Zigachev 	    stream->audio_info.mode_count && stream->audio_info.flags.all) {
1957*b843c749SSergey Zigachev 		pipe_ctx->stream_res.audio = find_first_free_audio(
1958*b843c749SSergey Zigachev 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id);
1959*b843c749SSergey Zigachev 
1960*b843c749SSergey Zigachev 		/*
1961*b843c749SSergey Zigachev 		 * Audio assigned in order first come first get.
1962*b843c749SSergey Zigachev 		 * There are asics which has number of audio
1963*b843c749SSergey Zigachev 		 * resources less then number of pipes
1964*b843c749SSergey Zigachev 		 */
1965*b843c749SSergey Zigachev 		if (pipe_ctx->stream_res.audio)
1966*b843c749SSergey Zigachev 			update_audio_usage(&context->res_ctx, pool,
1967*b843c749SSergey Zigachev 					   pipe_ctx->stream_res.audio, true);
1968*b843c749SSergey Zigachev 	}
1969*b843c749SSergey Zigachev 
1970*b843c749SSergey Zigachev 	/* Add ABM to the resource if on EDP */
1971*b843c749SSergey Zigachev 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal))
1972*b843c749SSergey Zigachev 		pipe_ctx->stream_res.abm = pool->abm;
1973*b843c749SSergey Zigachev 
1974*b843c749SSergey Zigachev 	for (i = 0; i < context->stream_count; i++)
1975*b843c749SSergey Zigachev 		if (context->streams[i] == stream) {
1976*b843c749SSergey Zigachev 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
1977*b843c749SSergey Zigachev 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->id;
1978*b843c749SSergey Zigachev 			return DC_OK;
1979*b843c749SSergey Zigachev 		}
1980*b843c749SSergey Zigachev 
1981*b843c749SSergey Zigachev 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
1982*b843c749SSergey Zigachev 	return DC_ERROR_UNEXPECTED;
1983*b843c749SSergey Zigachev }
1984*b843c749SSergey Zigachev 
dc_resource_state_copy_construct_current(const struct dc * dc,struct dc_state * dst_ctx)1985*b843c749SSergey Zigachev void dc_resource_state_copy_construct_current(
1986*b843c749SSergey Zigachev 		const struct dc *dc,
1987*b843c749SSergey Zigachev 		struct dc_state *dst_ctx)
1988*b843c749SSergey Zigachev {
1989*b843c749SSergey Zigachev 	dc_resource_state_copy_construct(dc->current_state, dst_ctx);
1990*b843c749SSergey Zigachev }
1991*b843c749SSergey Zigachev 
1992*b843c749SSergey Zigachev 
dc_resource_state_construct(const struct dc * dc,struct dc_state * dst_ctx)1993*b843c749SSergey Zigachev void dc_resource_state_construct(
1994*b843c749SSergey Zigachev 		const struct dc *dc,
1995*b843c749SSergey Zigachev 		struct dc_state *dst_ctx)
1996*b843c749SSergey Zigachev {
1997*b843c749SSergey Zigachev 	dst_ctx->dis_clk = dc->res_pool->dccg;
1998*b843c749SSergey Zigachev }
1999*b843c749SSergey Zigachev 
dc_validate_global_state(struct dc * dc,struct dc_state * new_ctx)2000*b843c749SSergey Zigachev enum dc_status dc_validate_global_state(
2001*b843c749SSergey Zigachev 		struct dc *dc,
2002*b843c749SSergey Zigachev 		struct dc_state *new_ctx)
2003*b843c749SSergey Zigachev {
2004*b843c749SSergey Zigachev 	enum dc_status result = DC_ERROR_UNEXPECTED;
2005*b843c749SSergey Zigachev 	int i, j;
2006*b843c749SSergey Zigachev 
2007*b843c749SSergey Zigachev 	if (!new_ctx)
2008*b843c749SSergey Zigachev 		return DC_ERROR_UNEXPECTED;
2009*b843c749SSergey Zigachev 
2010*b843c749SSergey Zigachev 	if (dc->res_pool->funcs->validate_global) {
2011*b843c749SSergey Zigachev 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
2012*b843c749SSergey Zigachev 		if (result != DC_OK)
2013*b843c749SSergey Zigachev 			return result;
2014*b843c749SSergey Zigachev 	}
2015*b843c749SSergey Zigachev 
2016*b843c749SSergey Zigachev 	for (i = 0; i < new_ctx->stream_count; i++) {
2017*b843c749SSergey Zigachev 		struct dc_stream_state *stream = new_ctx->streams[i];
2018*b843c749SSergey Zigachev 
2019*b843c749SSergey Zigachev 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
2020*b843c749SSergey Zigachev 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
2021*b843c749SSergey Zigachev 
2022*b843c749SSergey Zigachev 			if (pipe_ctx->stream != stream)
2023*b843c749SSergey Zigachev 				continue;
2024*b843c749SSergey Zigachev 
2025*b843c749SSergey Zigachev 			/* Switch to dp clock source only if there is
2026*b843c749SSergey Zigachev 			 * no non dp stream that shares the same timing
2027*b843c749SSergey Zigachev 			 * with the dp stream.
2028*b843c749SSergey Zigachev 			 */
2029*b843c749SSergey Zigachev 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
2030*b843c749SSergey Zigachev 				!find_pll_sharable_stream(stream, new_ctx)) {
2031*b843c749SSergey Zigachev 
2032*b843c749SSergey Zigachev 				resource_unreference_clock_source(
2033*b843c749SSergey Zigachev 						&new_ctx->res_ctx,
2034*b843c749SSergey Zigachev 						dc->res_pool,
2035*b843c749SSergey Zigachev 						pipe_ctx->clock_source);
2036*b843c749SSergey Zigachev 
2037*b843c749SSergey Zigachev 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
2038*b843c749SSergey Zigachev 				resource_reference_clock_source(
2039*b843c749SSergey Zigachev 						&new_ctx->res_ctx,
2040*b843c749SSergey Zigachev 						dc->res_pool,
2041*b843c749SSergey Zigachev 						 pipe_ctx->clock_source);
2042*b843c749SSergey Zigachev 			}
2043*b843c749SSergey Zigachev 		}
2044*b843c749SSergey Zigachev 	}
2045*b843c749SSergey Zigachev 
2046*b843c749SSergey Zigachev 	result = resource_build_scaling_params_for_context(dc, new_ctx);
2047*b843c749SSergey Zigachev 
2048*b843c749SSergey Zigachev 	if (result == DC_OK)
2049*b843c749SSergey Zigachev 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx))
2050*b843c749SSergey Zigachev 			result = DC_FAIL_BANDWIDTH_VALIDATE;
2051*b843c749SSergey Zigachev 
2052*b843c749SSergey Zigachev 	return result;
2053*b843c749SSergey Zigachev }
2054*b843c749SSergey Zigachev 
patch_gamut_packet_checksum(struct dc_info_packet * gamut_packet)2055*b843c749SSergey Zigachev static void patch_gamut_packet_checksum(
2056*b843c749SSergey Zigachev 		struct dc_info_packet *gamut_packet)
2057*b843c749SSergey Zigachev {
2058*b843c749SSergey Zigachev 	/* For gamut we recalc checksum */
2059*b843c749SSergey Zigachev 	if (gamut_packet->valid) {
2060*b843c749SSergey Zigachev 		uint8_t chk_sum = 0;
2061*b843c749SSergey Zigachev 		uint8_t *ptr;
2062*b843c749SSergey Zigachev 		uint8_t i;
2063*b843c749SSergey Zigachev 
2064*b843c749SSergey Zigachev 		/*start of the Gamut data. */
2065*b843c749SSergey Zigachev 		ptr = &gamut_packet->sb[3];
2066*b843c749SSergey Zigachev 
2067*b843c749SSergey Zigachev 		for (i = 0; i <= gamut_packet->sb[1]; i++)
2068*b843c749SSergey Zigachev 			chk_sum += ptr[i];
2069*b843c749SSergey Zigachev 
2070*b843c749SSergey Zigachev 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
2071*b843c749SSergey Zigachev 	}
2072*b843c749SSergey Zigachev }
2073*b843c749SSergey Zigachev 
set_avi_info_frame(struct dc_info_packet * info_packet,struct pipe_ctx * pipe_ctx)2074*b843c749SSergey Zigachev static void set_avi_info_frame(
2075*b843c749SSergey Zigachev 		struct dc_info_packet *info_packet,
2076*b843c749SSergey Zigachev 		struct pipe_ctx *pipe_ctx)
2077*b843c749SSergey Zigachev {
2078*b843c749SSergey Zigachev 	struct dc_stream_state *stream = pipe_ctx->stream;
2079*b843c749SSergey Zigachev 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
2080*b843c749SSergey Zigachev 	uint32_t pixel_encoding = 0;
2081*b843c749SSergey Zigachev 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
2082*b843c749SSergey Zigachev 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
2083*b843c749SSergey Zigachev 	bool itc = false;
2084*b843c749SSergey Zigachev 	uint8_t itc_value = 0;
2085*b843c749SSergey Zigachev 	uint8_t cn0_cn1 = 0;
2086*b843c749SSergey Zigachev 	unsigned int cn0_cn1_value = 0;
2087*b843c749SSergey Zigachev 	uint8_t *check_sum = NULL;
2088*b843c749SSergey Zigachev 	uint8_t byte_index = 0;
2089*b843c749SSergey Zigachev 	union hdmi_info_packet hdmi_info;
2090*b843c749SSergey Zigachev 	union display_content_support support = {0};
2091*b843c749SSergey Zigachev 	unsigned int vic = pipe_ctx->stream->timing.vic;
2092*b843c749SSergey Zigachev 	enum dc_timing_3d_format format;
2093*b843c749SSergey Zigachev 
2094*b843c749SSergey Zigachev 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
2095*b843c749SSergey Zigachev 
2096*b843c749SSergey Zigachev 	color_space = pipe_ctx->stream->output_color_space;
2097*b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_UNKNOWN)
2098*b843c749SSergey Zigachev 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
2099*b843c749SSergey Zigachev 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
2100*b843c749SSergey Zigachev 
2101*b843c749SSergey Zigachev 	/* Initialize header */
2102*b843c749SSergey Zigachev 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
2103*b843c749SSergey Zigachev 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
2104*b843c749SSergey Zigachev 	* not be used in HDMI 2.0 (Section 10.1) */
2105*b843c749SSergey Zigachev 	hdmi_info.bits.header.version = 2;
2106*b843c749SSergey Zigachev 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
2107*b843c749SSergey Zigachev 
2108*b843c749SSergey Zigachev 	/*
2109*b843c749SSergey Zigachev 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
2110*b843c749SSergey Zigachev 	 * according to HDMI 2.0 spec (Section 10.1)
2111*b843c749SSergey Zigachev 	 */
2112*b843c749SSergey Zigachev 
2113*b843c749SSergey Zigachev 	switch (stream->timing.pixel_encoding) {
2114*b843c749SSergey Zigachev 	case PIXEL_ENCODING_YCBCR422:
2115*b843c749SSergey Zigachev 		pixel_encoding = 1;
2116*b843c749SSergey Zigachev 		break;
2117*b843c749SSergey Zigachev 
2118*b843c749SSergey Zigachev 	case PIXEL_ENCODING_YCBCR444:
2119*b843c749SSergey Zigachev 		pixel_encoding = 2;
2120*b843c749SSergey Zigachev 		break;
2121*b843c749SSergey Zigachev 	case PIXEL_ENCODING_YCBCR420:
2122*b843c749SSergey Zigachev 		pixel_encoding = 3;
2123*b843c749SSergey Zigachev 		break;
2124*b843c749SSergey Zigachev 
2125*b843c749SSergey Zigachev 	case PIXEL_ENCODING_RGB:
2126*b843c749SSergey Zigachev 	default:
2127*b843c749SSergey Zigachev 		pixel_encoding = 0;
2128*b843c749SSergey Zigachev 	}
2129*b843c749SSergey Zigachev 
2130*b843c749SSergey Zigachev 	/* Y0_Y1_Y2 : The pixel encoding */
2131*b843c749SSergey Zigachev 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
2132*b843c749SSergey Zigachev 	hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
2133*b843c749SSergey Zigachev 
2134*b843c749SSergey Zigachev 	/* A0 = 1 Active Format Information valid */
2135*b843c749SSergey Zigachev 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
2136*b843c749SSergey Zigachev 
2137*b843c749SSergey Zigachev 	/* B0, B1 = 3; Bar info data is valid */
2138*b843c749SSergey Zigachev 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
2139*b843c749SSergey Zigachev 
2140*b843c749SSergey Zigachev 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
2141*b843c749SSergey Zigachev 
2142*b843c749SSergey Zigachev 	/* S0, S1 : Underscan / Overscan */
2143*b843c749SSergey Zigachev 	/* TODO: un-hardcode scan type */
2144*b843c749SSergey Zigachev 	scan_type = SCANNING_TYPE_UNDERSCAN;
2145*b843c749SSergey Zigachev 	hdmi_info.bits.S0_S1 = scan_type;
2146*b843c749SSergey Zigachev 
2147*b843c749SSergey Zigachev 	/* C0, C1 : Colorimetry */
2148*b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_YCBCR709 ||
2149*b843c749SSergey Zigachev 			color_space == COLOR_SPACE_YCBCR709_LIMITED)
2150*b843c749SSergey Zigachev 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
2151*b843c749SSergey Zigachev 	else if (color_space == COLOR_SPACE_YCBCR601 ||
2152*b843c749SSergey Zigachev 			color_space == COLOR_SPACE_YCBCR601_LIMITED)
2153*b843c749SSergey Zigachev 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
2154*b843c749SSergey Zigachev 	else {
2155*b843c749SSergey Zigachev 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
2156*b843c749SSergey Zigachev 	}
2157*b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_2020_RGB_FULLRANGE ||
2158*b843c749SSergey Zigachev 			color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE ||
2159*b843c749SSergey Zigachev 			color_space == COLOR_SPACE_2020_YCBCR) {
2160*b843c749SSergey Zigachev 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
2161*b843c749SSergey Zigachev 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2162*b843c749SSergey Zigachev 	} else if (color_space == COLOR_SPACE_ADOBERGB) {
2163*b843c749SSergey Zigachev 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
2164*b843c749SSergey Zigachev 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
2165*b843c749SSergey Zigachev 	}
2166*b843c749SSergey Zigachev 
2167*b843c749SSergey Zigachev 	/* TODO: un-hardcode aspect ratio */
2168*b843c749SSergey Zigachev 	aspect = stream->timing.aspect_ratio;
2169*b843c749SSergey Zigachev 
2170*b843c749SSergey Zigachev 	switch (aspect) {
2171*b843c749SSergey Zigachev 	case ASPECT_RATIO_4_3:
2172*b843c749SSergey Zigachev 	case ASPECT_RATIO_16_9:
2173*b843c749SSergey Zigachev 		hdmi_info.bits.M0_M1 = aspect;
2174*b843c749SSergey Zigachev 		break;
2175*b843c749SSergey Zigachev 
2176*b843c749SSergey Zigachev 	case ASPECT_RATIO_NO_DATA:
2177*b843c749SSergey Zigachev 	case ASPECT_RATIO_64_27:
2178*b843c749SSergey Zigachev 	case ASPECT_RATIO_256_135:
2179*b843c749SSergey Zigachev 	default:
2180*b843c749SSergey Zigachev 		hdmi_info.bits.M0_M1 = 0;
2181*b843c749SSergey Zigachev 	}
2182*b843c749SSergey Zigachev 
2183*b843c749SSergey Zigachev 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
2184*b843c749SSergey Zigachev 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
2185*b843c749SSergey Zigachev 
2186*b843c749SSergey Zigachev 	/* TODO: un-hardcode cn0_cn1 and itc */
2187*b843c749SSergey Zigachev 
2188*b843c749SSergey Zigachev 	cn0_cn1 = 0;
2189*b843c749SSergey Zigachev 	cn0_cn1_value = 0;
2190*b843c749SSergey Zigachev 
2191*b843c749SSergey Zigachev 	itc = true;
2192*b843c749SSergey Zigachev 	itc_value = 1;
2193*b843c749SSergey Zigachev 
2194*b843c749SSergey Zigachev 	support = stream->sink->edid_caps.content_support;
2195*b843c749SSergey Zigachev 
2196*b843c749SSergey Zigachev 	if (itc) {
2197*b843c749SSergey Zigachev 		if (!support.bits.valid_content_type) {
2198*b843c749SSergey Zigachev 			cn0_cn1_value = 0;
2199*b843c749SSergey Zigachev 		} else {
2200*b843c749SSergey Zigachev 			if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
2201*b843c749SSergey Zigachev 				if (support.bits.graphics_content == 1) {
2202*b843c749SSergey Zigachev 					cn0_cn1_value = 0;
2203*b843c749SSergey Zigachev 				}
2204*b843c749SSergey Zigachev 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
2205*b843c749SSergey Zigachev 				if (support.bits.photo_content == 1) {
2206*b843c749SSergey Zigachev 					cn0_cn1_value = 1;
2207*b843c749SSergey Zigachev 				} else {
2208*b843c749SSergey Zigachev 					cn0_cn1_value = 0;
2209*b843c749SSergey Zigachev 					itc_value = 0;
2210*b843c749SSergey Zigachev 				}
2211*b843c749SSergey Zigachev 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
2212*b843c749SSergey Zigachev 				if (support.bits.cinema_content == 1) {
2213*b843c749SSergey Zigachev 					cn0_cn1_value = 2;
2214*b843c749SSergey Zigachev 				} else {
2215*b843c749SSergey Zigachev 					cn0_cn1_value = 0;
2216*b843c749SSergey Zigachev 					itc_value = 0;
2217*b843c749SSergey Zigachev 				}
2218*b843c749SSergey Zigachev 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
2219*b843c749SSergey Zigachev 				if (support.bits.game_content == 1) {
2220*b843c749SSergey Zigachev 					cn0_cn1_value = 3;
2221*b843c749SSergey Zigachev 				} else {
2222*b843c749SSergey Zigachev 					cn0_cn1_value = 0;
2223*b843c749SSergey Zigachev 					itc_value = 0;
2224*b843c749SSergey Zigachev 				}
2225*b843c749SSergey Zigachev 			}
2226*b843c749SSergey Zigachev 		}
2227*b843c749SSergey Zigachev 		hdmi_info.bits.CN0_CN1 = cn0_cn1_value;
2228*b843c749SSergey Zigachev 		hdmi_info.bits.ITC = itc_value;
2229*b843c749SSergey Zigachev 	}
2230*b843c749SSergey Zigachev 
2231*b843c749SSergey Zigachev 	/* TODO : We should handle YCC quantization */
2232*b843c749SSergey Zigachev 	/* but we do not have matrix calculation */
2233*b843c749SSergey Zigachev 	if (stream->sink->edid_caps.qs_bit == 1 &&
2234*b843c749SSergey Zigachev 			stream->sink->edid_caps.qy_bit == 1) {
2235*b843c749SSergey Zigachev 		if (color_space == COLOR_SPACE_SRGB ||
2236*b843c749SSergey Zigachev 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE) {
2237*b843c749SSergey Zigachev 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
2238*b843c749SSergey Zigachev 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_FULL_RANGE;
2239*b843c749SSergey Zigachev 		} else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
2240*b843c749SSergey Zigachev 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE) {
2241*b843c749SSergey Zigachev 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
2242*b843c749SSergey Zigachev 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2243*b843c749SSergey Zigachev 		} else {
2244*b843c749SSergey Zigachev 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2245*b843c749SSergey Zigachev 			hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2246*b843c749SSergey Zigachev 		}
2247*b843c749SSergey Zigachev 	} else {
2248*b843c749SSergey Zigachev 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
2249*b843c749SSergey Zigachev 		hdmi_info.bits.YQ0_YQ1   = YYC_QUANTIZATION_LIMITED_RANGE;
2250*b843c749SSergey Zigachev 	}
2251*b843c749SSergey Zigachev 
2252*b843c749SSergey Zigachev 	///VIC
2253*b843c749SSergey Zigachev 	format = stream->timing.timing_3d_format;
2254*b843c749SSergey Zigachev 	/*todo, add 3DStereo support*/
2255*b843c749SSergey Zigachev 	if (format != TIMING_3D_FORMAT_NONE) {
2256*b843c749SSergey Zigachev 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
2257*b843c749SSergey Zigachev 		switch (pipe_ctx->stream->timing.hdmi_vic) {
2258*b843c749SSergey Zigachev 		case 1:
2259*b843c749SSergey Zigachev 			vic = 95;
2260*b843c749SSergey Zigachev 			break;
2261*b843c749SSergey Zigachev 		case 2:
2262*b843c749SSergey Zigachev 			vic = 94;
2263*b843c749SSergey Zigachev 			break;
2264*b843c749SSergey Zigachev 		case 3:
2265*b843c749SSergey Zigachev 			vic = 93;
2266*b843c749SSergey Zigachev 			break;
2267*b843c749SSergey Zigachev 		case 4:
2268*b843c749SSergey Zigachev 			vic = 98;
2269*b843c749SSergey Zigachev 			break;
2270*b843c749SSergey Zigachev 		default:
2271*b843c749SSergey Zigachev 			break;
2272*b843c749SSergey Zigachev 		}
2273*b843c749SSergey Zigachev 	}
2274*b843c749SSergey Zigachev 	hdmi_info.bits.VIC0_VIC7 = vic;
2275*b843c749SSergey Zigachev 
2276*b843c749SSergey Zigachev 	/* pixel repetition
2277*b843c749SSergey Zigachev 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
2278*b843c749SSergey Zigachev 	 * repetition start from 1 */
2279*b843c749SSergey Zigachev 	hdmi_info.bits.PR0_PR3 = 0;
2280*b843c749SSergey Zigachev 
2281*b843c749SSergey Zigachev 	/* Bar Info
2282*b843c749SSergey Zigachev 	 * barTop:    Line Number of End of Top Bar.
2283*b843c749SSergey Zigachev 	 * barBottom: Line Number of Start of Bottom Bar.
2284*b843c749SSergey Zigachev 	 * barLeft:   Pixel Number of End of Left Bar.
2285*b843c749SSergey Zigachev 	 * barRight:  Pixel Number of Start of Right Bar. */
2286*b843c749SSergey Zigachev 	hdmi_info.bits.bar_top = stream->timing.v_border_top;
2287*b843c749SSergey Zigachev 	hdmi_info.bits.bar_bottom = (stream->timing.v_total
2288*b843c749SSergey Zigachev 			- stream->timing.v_border_bottom + 1);
2289*b843c749SSergey Zigachev 	hdmi_info.bits.bar_left  = stream->timing.h_border_left;
2290*b843c749SSergey Zigachev 	hdmi_info.bits.bar_right = (stream->timing.h_total
2291*b843c749SSergey Zigachev 			- stream->timing.h_border_right + 1);
2292*b843c749SSergey Zigachev 
2293*b843c749SSergey Zigachev 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
2294*b843c749SSergey Zigachev 	check_sum = &hdmi_info.packet_raw_data.sb[0];
2295*b843c749SSergey Zigachev 
2296*b843c749SSergey Zigachev 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + HDMI_AVI_INFOFRAME_SIZE + 2;
2297*b843c749SSergey Zigachev 
2298*b843c749SSergey Zigachev 	for (byte_index = 1; byte_index <= HDMI_AVI_INFOFRAME_SIZE; byte_index++)
2299*b843c749SSergey Zigachev 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
2300*b843c749SSergey Zigachev 
2301*b843c749SSergey Zigachev 	/* one byte complement */
2302*b843c749SSergey Zigachev 	*check_sum = (uint8_t) (0x100 - *check_sum);
2303*b843c749SSergey Zigachev 
2304*b843c749SSergey Zigachev 	/* Store in hw_path_mode */
2305*b843c749SSergey Zigachev 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
2306*b843c749SSergey Zigachev 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
2307*b843c749SSergey Zigachev 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
2308*b843c749SSergey Zigachev 
2309*b843c749SSergey Zigachev 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
2310*b843c749SSergey Zigachev 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
2311*b843c749SSergey Zigachev 
2312*b843c749SSergey Zigachev 	info_packet->valid = true;
2313*b843c749SSergey Zigachev }
2314*b843c749SSergey Zigachev 
set_vendor_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)2315*b843c749SSergey Zigachev static void set_vendor_info_packet(
2316*b843c749SSergey Zigachev 		struct dc_info_packet *info_packet,
2317*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
2318*b843c749SSergey Zigachev {
2319*b843c749SSergey Zigachev 	uint32_t length = 0;
2320*b843c749SSergey Zigachev 	bool hdmi_vic_mode = false;
2321*b843c749SSergey Zigachev 	uint8_t checksum = 0;
2322*b843c749SSergey Zigachev 	uint32_t i = 0;
2323*b843c749SSergey Zigachev 	enum dc_timing_3d_format format;
2324*b843c749SSergey Zigachev 	// Can be different depending on packet content /*todo*/
2325*b843c749SSergey Zigachev 	// unsigned int length = pPathMode->dolbyVision ? 24 : 5;
2326*b843c749SSergey Zigachev 
2327*b843c749SSergey Zigachev 	info_packet->valid = false;
2328*b843c749SSergey Zigachev 
2329*b843c749SSergey Zigachev 	format = stream->timing.timing_3d_format;
2330*b843c749SSergey Zigachev 	if (stream->view_format == VIEW_3D_FORMAT_NONE)
2331*b843c749SSergey Zigachev 		format = TIMING_3D_FORMAT_NONE;
2332*b843c749SSergey Zigachev 
2333*b843c749SSergey Zigachev 	/* Can be different depending on packet content */
2334*b843c749SSergey Zigachev 	length = 5;
2335*b843c749SSergey Zigachev 
2336*b843c749SSergey Zigachev 	if (stream->timing.hdmi_vic != 0
2337*b843c749SSergey Zigachev 			&& stream->timing.h_total >= 3840
2338*b843c749SSergey Zigachev 			&& stream->timing.v_total >= 2160)
2339*b843c749SSergey Zigachev 		hdmi_vic_mode = true;
2340*b843c749SSergey Zigachev 
2341*b843c749SSergey Zigachev 	/* According to HDMI 1.4a CTS, VSIF should be sent
2342*b843c749SSergey Zigachev 	 * for both 3D stereo and HDMI VIC modes.
2343*b843c749SSergey Zigachev 	 * For all other modes, there is no VSIF sent.  */
2344*b843c749SSergey Zigachev 
2345*b843c749SSergey Zigachev 	if (format == TIMING_3D_FORMAT_NONE && !hdmi_vic_mode)
2346*b843c749SSergey Zigachev 		return;
2347*b843c749SSergey Zigachev 
2348*b843c749SSergey Zigachev 	/* 24bit IEEE Registration identifier (0x000c03). LSB first. */
2349*b843c749SSergey Zigachev 	info_packet->sb[1] = 0x03;
2350*b843c749SSergey Zigachev 	info_packet->sb[2] = 0x0C;
2351*b843c749SSergey Zigachev 	info_packet->sb[3] = 0x00;
2352*b843c749SSergey Zigachev 
2353*b843c749SSergey Zigachev 	/*PB4: 5 lower bytes = 0 (reserved). 3 higher bits = HDMI_Video_Format.
2354*b843c749SSergey Zigachev 	 * The value for HDMI_Video_Format are:
2355*b843c749SSergey Zigachev 	 * 0x0 (0b000) - No additional HDMI video format is presented in this
2356*b843c749SSergey Zigachev 	 * packet
2357*b843c749SSergey Zigachev 	 * 0x1 (0b001) - Extended resolution format present. 1 byte of HDMI_VIC
2358*b843c749SSergey Zigachev 	 * parameter follows
2359*b843c749SSergey Zigachev 	 * 0x2 (0b010) - 3D format indication present. 3D_Structure and
2360*b843c749SSergey Zigachev 	 * potentially 3D_Ext_Data follows
2361*b843c749SSergey Zigachev 	 * 0x3..0x7 (0b011..0b111) - reserved for future use */
2362*b843c749SSergey Zigachev 	if (format != TIMING_3D_FORMAT_NONE)
2363*b843c749SSergey Zigachev 		info_packet->sb[4] = (2 << 5);
2364*b843c749SSergey Zigachev 	else if (hdmi_vic_mode)
2365*b843c749SSergey Zigachev 		info_packet->sb[4] = (1 << 5);
2366*b843c749SSergey Zigachev 
2367*b843c749SSergey Zigachev 	/* PB5: If PB4 claims 3D timing (HDMI_Video_Format = 0x2):
2368*b843c749SSergey Zigachev 	 * 4 lower bites = 0 (reserved). 4 higher bits = 3D_Structure.
2369*b843c749SSergey Zigachev 	 * The value for 3D_Structure are:
2370*b843c749SSergey Zigachev 	 * 0x0 - Frame Packing
2371*b843c749SSergey Zigachev 	 * 0x1 - Field Alternative
2372*b843c749SSergey Zigachev 	 * 0x2 - Line Alternative
2373*b843c749SSergey Zigachev 	 * 0x3 - Side-by-Side (full)
2374*b843c749SSergey Zigachev 	 * 0x4 - L + depth
2375*b843c749SSergey Zigachev 	 * 0x5 - L + depth + graphics + graphics-depth
2376*b843c749SSergey Zigachev 	 * 0x6 - Top-and-Bottom
2377*b843c749SSergey Zigachev 	 * 0x7 - Reserved for future use
2378*b843c749SSergey Zigachev 	 * 0x8 - Side-by-Side (Half)
2379*b843c749SSergey Zigachev 	 * 0x9..0xE - Reserved for future use
2380*b843c749SSergey Zigachev 	 * 0xF - Not used */
2381*b843c749SSergey Zigachev 	switch (format) {
2382*b843c749SSergey Zigachev 	case TIMING_3D_FORMAT_HW_FRAME_PACKING:
2383*b843c749SSergey Zigachev 	case TIMING_3D_FORMAT_SW_FRAME_PACKING:
2384*b843c749SSergey Zigachev 		info_packet->sb[5] = (0x0 << 4);
2385*b843c749SSergey Zigachev 		break;
2386*b843c749SSergey Zigachev 
2387*b843c749SSergey Zigachev 	case TIMING_3D_FORMAT_SIDE_BY_SIDE:
2388*b843c749SSergey Zigachev 	case TIMING_3D_FORMAT_SBS_SW_PACKED:
2389*b843c749SSergey Zigachev 		info_packet->sb[5] = (0x8 << 4);
2390*b843c749SSergey Zigachev 		length = 6;
2391*b843c749SSergey Zigachev 		break;
2392*b843c749SSergey Zigachev 
2393*b843c749SSergey Zigachev 	case TIMING_3D_FORMAT_TOP_AND_BOTTOM:
2394*b843c749SSergey Zigachev 	case TIMING_3D_FORMAT_TB_SW_PACKED:
2395*b843c749SSergey Zigachev 		info_packet->sb[5] = (0x6 << 4);
2396*b843c749SSergey Zigachev 		break;
2397*b843c749SSergey Zigachev 
2398*b843c749SSergey Zigachev 	default:
2399*b843c749SSergey Zigachev 		break;
2400*b843c749SSergey Zigachev 	}
2401*b843c749SSergey Zigachev 
2402*b843c749SSergey Zigachev 	/*PB5: If PB4 is set to 0x1 (extended resolution format)
2403*b843c749SSergey Zigachev 	 * fill PB5 with the correct HDMI VIC code */
2404*b843c749SSergey Zigachev 	if (hdmi_vic_mode)
2405*b843c749SSergey Zigachev 		info_packet->sb[5] = stream->timing.hdmi_vic;
2406*b843c749SSergey Zigachev 
2407*b843c749SSergey Zigachev 	/* Header */
2408*b843c749SSergey Zigachev 	info_packet->hb0 = HDMI_INFOFRAME_TYPE_VENDOR; /* VSIF packet type. */
2409*b843c749SSergey Zigachev 	info_packet->hb1 = 0x01; /* Version */
2410*b843c749SSergey Zigachev 
2411*b843c749SSergey Zigachev 	/* 4 lower bits = Length, 4 higher bits = 0 (reserved) */
2412*b843c749SSergey Zigachev 	info_packet->hb2 = (uint8_t) (length);
2413*b843c749SSergey Zigachev 
2414*b843c749SSergey Zigachev 	/* Calculate checksum */
2415*b843c749SSergey Zigachev 	checksum = 0;
2416*b843c749SSergey Zigachev 	checksum += info_packet->hb0;
2417*b843c749SSergey Zigachev 	checksum += info_packet->hb1;
2418*b843c749SSergey Zigachev 	checksum += info_packet->hb2;
2419*b843c749SSergey Zigachev 
2420*b843c749SSergey Zigachev 	for (i = 1; i <= length; i++)
2421*b843c749SSergey Zigachev 		checksum += info_packet->sb[i];
2422*b843c749SSergey Zigachev 
2423*b843c749SSergey Zigachev 	info_packet->sb[0] = (uint8_t) (0x100 - checksum);
2424*b843c749SSergey Zigachev 
2425*b843c749SSergey Zigachev 	info_packet->valid = true;
2426*b843c749SSergey Zigachev }
2427*b843c749SSergey Zigachev 
set_spd_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)2428*b843c749SSergey Zigachev static void set_spd_info_packet(
2429*b843c749SSergey Zigachev 		struct dc_info_packet *info_packet,
2430*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
2431*b843c749SSergey Zigachev {
2432*b843c749SSergey Zigachev 	/* SPD info packet for FreeSync */
2433*b843c749SSergey Zigachev 
2434*b843c749SSergey Zigachev 	unsigned char checksum = 0;
2435*b843c749SSergey Zigachev 	unsigned int idx, payload_size = 0;
2436*b843c749SSergey Zigachev 
2437*b843c749SSergey Zigachev 	/* Check if Freesync is supported. Return if false. If true,
2438*b843c749SSergey Zigachev 	 * set the corresponding bit in the info packet
2439*b843c749SSergey Zigachev 	 */
2440*b843c749SSergey Zigachev 	if (stream->freesync_ctx.supported == false)
2441*b843c749SSergey Zigachev 		return;
2442*b843c749SSergey Zigachev 
2443*b843c749SSergey Zigachev 	if (dc_is_hdmi_signal(stream->signal)) {
2444*b843c749SSergey Zigachev 
2445*b843c749SSergey Zigachev 		/* HEADER */
2446*b843c749SSergey Zigachev 
2447*b843c749SSergey Zigachev 		/* HB0  = Packet Type = 0x83 (Source Product
2448*b843c749SSergey Zigachev 		 *	  Descriptor InfoFrame)
2449*b843c749SSergey Zigachev 		 */
2450*b843c749SSergey Zigachev 		info_packet->hb0 = HDMI_INFOFRAME_TYPE_SPD;
2451*b843c749SSergey Zigachev 
2452*b843c749SSergey Zigachev 		/* HB1  = Version = 0x01 */
2453*b843c749SSergey Zigachev 		info_packet->hb1 = 0x01;
2454*b843c749SSergey Zigachev 
2455*b843c749SSergey Zigachev 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x08] */
2456*b843c749SSergey Zigachev 		info_packet->hb2 = 0x08;
2457*b843c749SSergey Zigachev 
2458*b843c749SSergey Zigachev 		payload_size = 0x08;
2459*b843c749SSergey Zigachev 
2460*b843c749SSergey Zigachev 	} else if (dc_is_dp_signal(stream->signal)) {
2461*b843c749SSergey Zigachev 
2462*b843c749SSergey Zigachev 		/* HEADER */
2463*b843c749SSergey Zigachev 
2464*b843c749SSergey Zigachev 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
2465*b843c749SSergey Zigachev 		 *	  when used to associate audio related info packets
2466*b843c749SSergey Zigachev 		 */
2467*b843c749SSergey Zigachev 		info_packet->hb0 = 0x00;
2468*b843c749SSergey Zigachev 
2469*b843c749SSergey Zigachev 		/* HB1  = Packet Type = 0x83 (Source Product
2470*b843c749SSergey Zigachev 		 *	  Descriptor InfoFrame)
2471*b843c749SSergey Zigachev 		 */
2472*b843c749SSergey Zigachev 		info_packet->hb1 = HDMI_INFOFRAME_TYPE_SPD;
2473*b843c749SSergey Zigachev 
2474*b843c749SSergey Zigachev 		/* HB2  = [Bits 7:0 = Least significant eight bits -
2475*b843c749SSergey Zigachev 		 *	  For INFOFRAME, the value must be 1Bh]
2476*b843c749SSergey Zigachev 		 */
2477*b843c749SSergey Zigachev 		info_packet->hb2 = 0x1B;
2478*b843c749SSergey Zigachev 
2479*b843c749SSergey Zigachev 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x1]
2480*b843c749SSergey Zigachev 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
2481*b843c749SSergey Zigachev 		 */
2482*b843c749SSergey Zigachev 		info_packet->hb3 = 0x04;
2483*b843c749SSergey Zigachev 
2484*b843c749SSergey Zigachev 		payload_size = 0x1B;
2485*b843c749SSergey Zigachev 	}
2486*b843c749SSergey Zigachev 
2487*b843c749SSergey Zigachev 	/* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
2488*b843c749SSergey Zigachev 	info_packet->sb[1] = 0x1A;
2489*b843c749SSergey Zigachev 
2490*b843c749SSergey Zigachev 	/* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
2491*b843c749SSergey Zigachev 	info_packet->sb[2] = 0x00;
2492*b843c749SSergey Zigachev 
2493*b843c749SSergey Zigachev 	/* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
2494*b843c749SSergey Zigachev 	info_packet->sb[3] = 0x00;
2495*b843c749SSergey Zigachev 
2496*b843c749SSergey Zigachev 	/* PB4 = Reserved */
2497*b843c749SSergey Zigachev 	info_packet->sb[4] = 0x00;
2498*b843c749SSergey Zigachev 
2499*b843c749SSergey Zigachev 	/* PB5 = Reserved */
2500*b843c749SSergey Zigachev 	info_packet->sb[5] = 0x00;
2501*b843c749SSergey Zigachev 
2502*b843c749SSergey Zigachev 	/* PB6 = [Bits 7:3 = Reserved] */
2503*b843c749SSergey Zigachev 	info_packet->sb[6] = 0x00;
2504*b843c749SSergey Zigachev 
2505*b843c749SSergey Zigachev 	if (stream->freesync_ctx.supported == true)
2506*b843c749SSergey Zigachev 		/* PB6 = [Bit 0 = FreeSync Supported] */
2507*b843c749SSergey Zigachev 		info_packet->sb[6] |= 0x01;
2508*b843c749SSergey Zigachev 
2509*b843c749SSergey Zigachev 	if (stream->freesync_ctx.enabled == true)
2510*b843c749SSergey Zigachev 		/* PB6 = [Bit 1 = FreeSync Enabled] */
2511*b843c749SSergey Zigachev 		info_packet->sb[6] |= 0x02;
2512*b843c749SSergey Zigachev 
2513*b843c749SSergey Zigachev 	if (stream->freesync_ctx.active == true)
2514*b843c749SSergey Zigachev 		/* PB6 = [Bit 2 = FreeSync Active] */
2515*b843c749SSergey Zigachev 		info_packet->sb[6] |= 0x04;
2516*b843c749SSergey Zigachev 
2517*b843c749SSergey Zigachev 	/* PB7 = FreeSync Minimum refresh rate (Hz) */
2518*b843c749SSergey Zigachev 	info_packet->sb[7] = (unsigned char) (stream->freesync_ctx.
2519*b843c749SSergey Zigachev 			min_refresh_in_micro_hz / 1000000);
2520*b843c749SSergey Zigachev 
2521*b843c749SSergey Zigachev 	/* PB8 = FreeSync Maximum refresh rate (Hz)
2522*b843c749SSergey Zigachev 	 *
2523*b843c749SSergey Zigachev 	 * Note: We do not use the maximum capable refresh rate
2524*b843c749SSergey Zigachev 	 * of the panel, because we should never go above the field
2525*b843c749SSergey Zigachev 	 * rate of the mode timing set.
2526*b843c749SSergey Zigachev 	 */
2527*b843c749SSergey Zigachev 	info_packet->sb[8] = (unsigned char) (stream->freesync_ctx.
2528*b843c749SSergey Zigachev 			nominal_refresh_in_micro_hz / 1000000);
2529*b843c749SSergey Zigachev 
2530*b843c749SSergey Zigachev 	/* PB9 - PB27  = Reserved */
2531*b843c749SSergey Zigachev 	for (idx = 9; idx <= 27; idx++)
2532*b843c749SSergey Zigachev 		info_packet->sb[idx] = 0x00;
2533*b843c749SSergey Zigachev 
2534*b843c749SSergey Zigachev 	/* Calculate checksum */
2535*b843c749SSergey Zigachev 	checksum += info_packet->hb0;
2536*b843c749SSergey Zigachev 	checksum += info_packet->hb1;
2537*b843c749SSergey Zigachev 	checksum += info_packet->hb2;
2538*b843c749SSergey Zigachev 	checksum += info_packet->hb3;
2539*b843c749SSergey Zigachev 
2540*b843c749SSergey Zigachev 	for (idx = 1; idx <= payload_size; idx++)
2541*b843c749SSergey Zigachev 		checksum += info_packet->sb[idx];
2542*b843c749SSergey Zigachev 
2543*b843c749SSergey Zigachev 	/* PB0 = Checksum (one byte complement) */
2544*b843c749SSergey Zigachev 	info_packet->sb[0] = (unsigned char) (0x100 - checksum);
2545*b843c749SSergey Zigachev 
2546*b843c749SSergey Zigachev 	info_packet->valid = true;
2547*b843c749SSergey Zigachev }
2548*b843c749SSergey Zigachev 
set_hdr_static_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)2549*b843c749SSergey Zigachev static void set_hdr_static_info_packet(
2550*b843c749SSergey Zigachev 		struct dc_info_packet *info_packet,
2551*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
2552*b843c749SSergey Zigachev {
2553*b843c749SSergey Zigachev 	/* HDR Static Metadata info packet for HDR10 */
2554*b843c749SSergey Zigachev 
2555*b843c749SSergey Zigachev 	if (!stream->hdr_static_metadata.valid ||
2556*b843c749SSergey Zigachev 			stream->use_dynamic_meta)
2557*b843c749SSergey Zigachev 		return;
2558*b843c749SSergey Zigachev 
2559*b843c749SSergey Zigachev 	*info_packet = stream->hdr_static_metadata;
2560*b843c749SSergey Zigachev }
2561*b843c749SSergey Zigachev 
set_vsc_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)2562*b843c749SSergey Zigachev static void set_vsc_info_packet(
2563*b843c749SSergey Zigachev 		struct dc_info_packet *info_packet,
2564*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
2565*b843c749SSergey Zigachev {
2566*b843c749SSergey Zigachev 	unsigned int vscPacketRevision = 0;
2567*b843c749SSergey Zigachev 	unsigned int i;
2568*b843c749SSergey Zigachev 
2569*b843c749SSergey Zigachev 	/*VSC packet set to 2 when DP revision >= 1.2*/
2570*b843c749SSergey Zigachev 	if (stream->psr_version != 0) {
2571*b843c749SSergey Zigachev 		vscPacketRevision = 2;
2572*b843c749SSergey Zigachev 	}
2573*b843c749SSergey Zigachev 
2574*b843c749SSergey Zigachev 	/* VSC packet not needed based on the features
2575*b843c749SSergey Zigachev 	 * supported by this DP display
2576*b843c749SSergey Zigachev 	 */
2577*b843c749SSergey Zigachev 	if (vscPacketRevision == 0)
2578*b843c749SSergey Zigachev 		return;
2579*b843c749SSergey Zigachev 
2580*b843c749SSergey Zigachev 	if (vscPacketRevision == 0x2) {
2581*b843c749SSergey Zigachev 		/* Secondary-data Packet ID = 0*/
2582*b843c749SSergey Zigachev 		info_packet->hb0 = 0x00;
2583*b843c749SSergey Zigachev 		/* 07h - Packet Type Value indicating Video
2584*b843c749SSergey Zigachev 		 * Stream Configuration packet
2585*b843c749SSergey Zigachev 		 */
2586*b843c749SSergey Zigachev 		info_packet->hb1 = 0x07;
2587*b843c749SSergey Zigachev 		/* 02h = VSC SDP supporting 3D stereo and PSR
2588*b843c749SSergey Zigachev 		 * (applies to eDP v1.3 or higher).
2589*b843c749SSergey Zigachev 		 */
2590*b843c749SSergey Zigachev 		info_packet->hb2 = 0x02;
2591*b843c749SSergey Zigachev 		/* 08h = VSC packet supporting 3D stereo + PSR
2592*b843c749SSergey Zigachev 		 * (HB2 = 02h).
2593*b843c749SSergey Zigachev 		 */
2594*b843c749SSergey Zigachev 		info_packet->hb3 = 0x08;
2595*b843c749SSergey Zigachev 
2596*b843c749SSergey Zigachev 		for (i = 0; i < 28; i++)
2597*b843c749SSergey Zigachev 			info_packet->sb[i] = 0;
2598*b843c749SSergey Zigachev 
2599*b843c749SSergey Zigachev 		info_packet->valid = true;
2600*b843c749SSergey Zigachev 	}
2601*b843c749SSergey Zigachev 
2602*b843c749SSergey Zigachev 	/*TODO: stereo 3D support and extend pixel encoding colorimetry*/
2603*b843c749SSergey Zigachev }
2604*b843c749SSergey Zigachev 
dc_resource_state_destruct(struct dc_state * context)2605*b843c749SSergey Zigachev void dc_resource_state_destruct(struct dc_state *context)
2606*b843c749SSergey Zigachev {
2607*b843c749SSergey Zigachev 	int i, j;
2608*b843c749SSergey Zigachev 
2609*b843c749SSergey Zigachev 	for (i = 0; i < context->stream_count; i++) {
2610*b843c749SSergey Zigachev 		for (j = 0; j < context->stream_status[i].plane_count; j++)
2611*b843c749SSergey Zigachev 			dc_plane_state_release(
2612*b843c749SSergey Zigachev 				context->stream_status[i].plane_states[j]);
2613*b843c749SSergey Zigachev 
2614*b843c749SSergey Zigachev 		context->stream_status[i].plane_count = 0;
2615*b843c749SSergey Zigachev 		dc_stream_release(context->streams[i]);
2616*b843c749SSergey Zigachev 		context->streams[i] = NULL;
2617*b843c749SSergey Zigachev 	}
2618*b843c749SSergey Zigachev }
2619*b843c749SSergey Zigachev 
2620*b843c749SSergey Zigachev /*
2621*b843c749SSergey Zigachev  * Copy src_ctx into dst_ctx and retain all surfaces and streams referenced
2622*b843c749SSergey Zigachev  * by the src_ctx
2623*b843c749SSergey Zigachev  */
dc_resource_state_copy_construct(const struct dc_state * src_ctx,struct dc_state * dst_ctx)2624*b843c749SSergey Zigachev void dc_resource_state_copy_construct(
2625*b843c749SSergey Zigachev 		const struct dc_state *src_ctx,
2626*b843c749SSergey Zigachev 		struct dc_state *dst_ctx)
2627*b843c749SSergey Zigachev {
2628*b843c749SSergey Zigachev 	int i, j;
2629*b843c749SSergey Zigachev 	struct kref refcount = dst_ctx->refcount;
2630*b843c749SSergey Zigachev 
2631*b843c749SSergey Zigachev 	*dst_ctx = *src_ctx;
2632*b843c749SSergey Zigachev 
2633*b843c749SSergey Zigachev 	for (i = 0; i < MAX_PIPES; i++) {
2634*b843c749SSergey Zigachev 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
2635*b843c749SSergey Zigachev 
2636*b843c749SSergey Zigachev 		if (cur_pipe->top_pipe)
2637*b843c749SSergey Zigachev 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
2638*b843c749SSergey Zigachev 
2639*b843c749SSergey Zigachev 		if (cur_pipe->bottom_pipe)
2640*b843c749SSergey Zigachev 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
2641*b843c749SSergey Zigachev 
2642*b843c749SSergey Zigachev 	}
2643*b843c749SSergey Zigachev 
2644*b843c749SSergey Zigachev 	for (i = 0; i < dst_ctx->stream_count; i++) {
2645*b843c749SSergey Zigachev 		dc_stream_retain(dst_ctx->streams[i]);
2646*b843c749SSergey Zigachev 		for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
2647*b843c749SSergey Zigachev 			dc_plane_state_retain(
2648*b843c749SSergey Zigachev 				dst_ctx->stream_status[i].plane_states[j]);
2649*b843c749SSergey Zigachev 	}
2650*b843c749SSergey Zigachev 
2651*b843c749SSergey Zigachev 	/* context refcount should not be overridden */
2652*b843c749SSergey Zigachev 	dst_ctx->refcount = refcount;
2653*b843c749SSergey Zigachev 
2654*b843c749SSergey Zigachev }
2655*b843c749SSergey Zigachev 
dc_resource_find_first_free_pll(struct resource_context * res_ctx,const struct resource_pool * pool)2656*b843c749SSergey Zigachev struct clock_source *dc_resource_find_first_free_pll(
2657*b843c749SSergey Zigachev 		struct resource_context *res_ctx,
2658*b843c749SSergey Zigachev 		const struct resource_pool *pool)
2659*b843c749SSergey Zigachev {
2660*b843c749SSergey Zigachev 	int i;
2661*b843c749SSergey Zigachev 
2662*b843c749SSergey Zigachev 	for (i = 0; i < pool->clk_src_count; ++i) {
2663*b843c749SSergey Zigachev 		if (res_ctx->clock_source_ref_count[i] == 0)
2664*b843c749SSergey Zigachev 			return pool->clock_sources[i];
2665*b843c749SSergey Zigachev 	}
2666*b843c749SSergey Zigachev 
2667*b843c749SSergey Zigachev 	return NULL;
2668*b843c749SSergey Zigachev }
2669*b843c749SSergey Zigachev 
resource_build_info_frame(struct pipe_ctx * pipe_ctx)2670*b843c749SSergey Zigachev void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
2671*b843c749SSergey Zigachev {
2672*b843c749SSergey Zigachev 	enum signal_type signal = SIGNAL_TYPE_NONE;
2673*b843c749SSergey Zigachev 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
2674*b843c749SSergey Zigachev 
2675*b843c749SSergey Zigachev 	/* default all packets to invalid */
2676*b843c749SSergey Zigachev 	info->avi.valid = false;
2677*b843c749SSergey Zigachev 	info->gamut.valid = false;
2678*b843c749SSergey Zigachev 	info->vendor.valid = false;
2679*b843c749SSergey Zigachev 	info->spd.valid = false;
2680*b843c749SSergey Zigachev 	info->hdrsmd.valid = false;
2681*b843c749SSergey Zigachev 	info->vsc.valid = false;
2682*b843c749SSergey Zigachev 
2683*b843c749SSergey Zigachev 	signal = pipe_ctx->stream->signal;
2684*b843c749SSergey Zigachev 
2685*b843c749SSergey Zigachev 	/* HDMi and DP have different info packets*/
2686*b843c749SSergey Zigachev 	if (dc_is_hdmi_signal(signal)) {
2687*b843c749SSergey Zigachev 		set_avi_info_frame(&info->avi, pipe_ctx);
2688*b843c749SSergey Zigachev 
2689*b843c749SSergey Zigachev 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
2690*b843c749SSergey Zigachev 
2691*b843c749SSergey Zigachev 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
2692*b843c749SSergey Zigachev 
2693*b843c749SSergey Zigachev 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
2694*b843c749SSergey Zigachev 
2695*b843c749SSergey Zigachev 	} else if (dc_is_dp_signal(signal)) {
2696*b843c749SSergey Zigachev 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
2697*b843c749SSergey Zigachev 
2698*b843c749SSergey Zigachev 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
2699*b843c749SSergey Zigachev 
2700*b843c749SSergey Zigachev 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
2701*b843c749SSergey Zigachev 	}
2702*b843c749SSergey Zigachev 
2703*b843c749SSergey Zigachev 	patch_gamut_packet_checksum(&info->gamut);
2704*b843c749SSergey Zigachev }
2705*b843c749SSergey Zigachev 
resource_map_clock_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)2706*b843c749SSergey Zigachev enum dc_status resource_map_clock_resources(
2707*b843c749SSergey Zigachev 		const struct dc  *dc,
2708*b843c749SSergey Zigachev 		struct dc_state *context,
2709*b843c749SSergey Zigachev 		struct dc_stream_state *stream)
2710*b843c749SSergey Zigachev {
2711*b843c749SSergey Zigachev 	/* acquire new resources */
2712*b843c749SSergey Zigachev 	const struct resource_pool *pool = dc->res_pool;
2713*b843c749SSergey Zigachev 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(
2714*b843c749SSergey Zigachev 				&context->res_ctx, stream);
2715*b843c749SSergey Zigachev 
2716*b843c749SSergey Zigachev 	if (!pipe_ctx)
2717*b843c749SSergey Zigachev 		return DC_ERROR_UNEXPECTED;
2718*b843c749SSergey Zigachev 
2719*b843c749SSergey Zigachev 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
2720*b843c749SSergey Zigachev 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
2721*b843c749SSergey Zigachev 		pipe_ctx->clock_source = pool->dp_clock_source;
2722*b843c749SSergey Zigachev 	else {
2723*b843c749SSergey Zigachev 		pipe_ctx->clock_source = NULL;
2724*b843c749SSergey Zigachev 
2725*b843c749SSergey Zigachev 		if (!dc->config.disable_disp_pll_sharing)
2726*b843c749SSergey Zigachev 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
2727*b843c749SSergey Zigachev 				&context->res_ctx,
2728*b843c749SSergey Zigachev 				pipe_ctx);
2729*b843c749SSergey Zigachev 
2730*b843c749SSergey Zigachev 		if (pipe_ctx->clock_source == NULL)
2731*b843c749SSergey Zigachev 			pipe_ctx->clock_source =
2732*b843c749SSergey Zigachev 				dc_resource_find_first_free_pll(
2733*b843c749SSergey Zigachev 					&context->res_ctx,
2734*b843c749SSergey Zigachev 					pool);
2735*b843c749SSergey Zigachev 	}
2736*b843c749SSergey Zigachev 
2737*b843c749SSergey Zigachev 	if (pipe_ctx->clock_source == NULL)
2738*b843c749SSergey Zigachev 		return DC_NO_CLOCK_SOURCE_RESOURCE;
2739*b843c749SSergey Zigachev 
2740*b843c749SSergey Zigachev 	resource_reference_clock_source(
2741*b843c749SSergey Zigachev 		&context->res_ctx, pool,
2742*b843c749SSergey Zigachev 		pipe_ctx->clock_source);
2743*b843c749SSergey Zigachev 
2744*b843c749SSergey Zigachev 	return DC_OK;
2745*b843c749SSergey Zigachev }
2746*b843c749SSergey Zigachev 
2747*b843c749SSergey Zigachev /*
2748*b843c749SSergey Zigachev  * Note: We need to disable output if clock sources change,
2749*b843c749SSergey Zigachev  * since bios does optimization and doesn't apply if changing
2750*b843c749SSergey Zigachev  * PHY when not already disabled.
2751*b843c749SSergey Zigachev  */
pipe_need_reprogram(struct pipe_ctx * pipe_ctx_old,struct pipe_ctx * pipe_ctx)2752*b843c749SSergey Zigachev bool pipe_need_reprogram(
2753*b843c749SSergey Zigachev 		struct pipe_ctx *pipe_ctx_old,
2754*b843c749SSergey Zigachev 		struct pipe_ctx *pipe_ctx)
2755*b843c749SSergey Zigachev {
2756*b843c749SSergey Zigachev 	if (!pipe_ctx_old->stream)
2757*b843c749SSergey Zigachev 		return false;
2758*b843c749SSergey Zigachev 
2759*b843c749SSergey Zigachev 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
2760*b843c749SSergey Zigachev 		return true;
2761*b843c749SSergey Zigachev 
2762*b843c749SSergey Zigachev 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
2763*b843c749SSergey Zigachev 		return true;
2764*b843c749SSergey Zigachev 
2765*b843c749SSergey Zigachev 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
2766*b843c749SSergey Zigachev 		return true;
2767*b843c749SSergey Zigachev 
2768*b843c749SSergey Zigachev 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
2769*b843c749SSergey Zigachev 			&& pipe_ctx_old->stream != pipe_ctx->stream)
2770*b843c749SSergey Zigachev 		return true;
2771*b843c749SSergey Zigachev 
2772*b843c749SSergey Zigachev 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
2773*b843c749SSergey Zigachev 		return true;
2774*b843c749SSergey Zigachev 
2775*b843c749SSergey Zigachev 	if (is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
2776*b843c749SSergey Zigachev 		return true;
2777*b843c749SSergey Zigachev 
2778*b843c749SSergey Zigachev 	if (is_hdr_static_meta_changed(pipe_ctx_old->stream, pipe_ctx->stream))
2779*b843c749SSergey Zigachev 		return true;
2780*b843c749SSergey Zigachev 
2781*b843c749SSergey Zigachev 	return false;
2782*b843c749SSergey Zigachev }
2783*b843c749SSergey Zigachev 
resource_build_bit_depth_reduction_params(struct dc_stream_state * stream,struct bit_depth_reduction_params * fmt_bit_depth)2784*b843c749SSergey Zigachev void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
2785*b843c749SSergey Zigachev 		struct bit_depth_reduction_params *fmt_bit_depth)
2786*b843c749SSergey Zigachev {
2787*b843c749SSergey Zigachev 	enum dc_dither_option option = stream->dither_option;
2788*b843c749SSergey Zigachev 	enum dc_pixel_encoding pixel_encoding =
2789*b843c749SSergey Zigachev 			stream->timing.pixel_encoding;
2790*b843c749SSergey Zigachev 
2791*b843c749SSergey Zigachev 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
2792*b843c749SSergey Zigachev 
2793*b843c749SSergey Zigachev 	if (option == DITHER_OPTION_DEFAULT) {
2794*b843c749SSergey Zigachev 		switch (stream->timing.display_color_depth) {
2795*b843c749SSergey Zigachev 		case COLOR_DEPTH_666:
2796*b843c749SSergey Zigachev 			option = DITHER_OPTION_SPATIAL6;
2797*b843c749SSergey Zigachev 			break;
2798*b843c749SSergey Zigachev 		case COLOR_DEPTH_888:
2799*b843c749SSergey Zigachev 			option = DITHER_OPTION_SPATIAL8;
2800*b843c749SSergey Zigachev 			break;
2801*b843c749SSergey Zigachev 		case COLOR_DEPTH_101010:
2802*b843c749SSergey Zigachev 			option = DITHER_OPTION_SPATIAL10;
2803*b843c749SSergey Zigachev 			break;
2804*b843c749SSergey Zigachev 		default:
2805*b843c749SSergey Zigachev 			option = DITHER_OPTION_DISABLE;
2806*b843c749SSergey Zigachev 		}
2807*b843c749SSergey Zigachev 	}
2808*b843c749SSergey Zigachev 
2809*b843c749SSergey Zigachev 	if (option == DITHER_OPTION_DISABLE)
2810*b843c749SSergey Zigachev 		return;
2811*b843c749SSergey Zigachev 
2812*b843c749SSergey Zigachev 	if (option == DITHER_OPTION_TRUN6) {
2813*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2814*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
2815*b843c749SSergey Zigachev 	} else if (option == DITHER_OPTION_TRUN8 ||
2816*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
2817*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN8_FM6) {
2818*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2819*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
2820*b843c749SSergey Zigachev 	} else if (option == DITHER_OPTION_TRUN10        ||
2821*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
2822*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
2823*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_FM8     ||
2824*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_FM6     ||
2825*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2826*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2827*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2828*b843c749SSergey Zigachev 	}
2829*b843c749SSergey Zigachev 
2830*b843c749SSergey Zigachev 	/* special case - Formatter can only reduce by 4 bits at most.
2831*b843c749SSergey Zigachev 	 * When reducing from 12 to 6 bits,
2832*b843c749SSergey Zigachev 	 * HW recommends we use trunc with round mode
2833*b843c749SSergey Zigachev 	 * (if we did nothing, trunc to 10 bits would be used)
2834*b843c749SSergey Zigachev 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
2835*b843c749SSergey Zigachev 	 * as the input was 10 bits.
2836*b843c749SSergey Zigachev 	 */
2837*b843c749SSergey Zigachev 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2838*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL6 ||
2839*b843c749SSergey Zigachev 			option == DITHER_OPTION_FM6) {
2840*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2841*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2842*b843c749SSergey Zigachev 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
2843*b843c749SSergey Zigachev 	}
2844*b843c749SSergey Zigachev 
2845*b843c749SSergey Zigachev 	/* spatial dither
2846*b843c749SSergey Zigachev 	 * note that spatial modes 1-3 are never used
2847*b843c749SSergey Zigachev 	 */
2848*b843c749SSergey Zigachev 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
2849*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL6 ||
2850*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
2851*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
2852*b843c749SSergey Zigachev 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2853*b843c749SSergey Zigachev 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
2854*b843c749SSergey Zigachev 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2855*b843c749SSergey Zigachev 		fmt_bit_depth->flags.RGB_RANDOM =
2856*b843c749SSergey Zigachev 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2857*b843c749SSergey Zigachev 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
2858*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL8 ||
2859*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL8_FM6        ||
2860*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
2861*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2862*b843c749SSergey Zigachev 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2863*b843c749SSergey Zigachev 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
2864*b843c749SSergey Zigachev 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2865*b843c749SSergey Zigachev 		fmt_bit_depth->flags.RGB_RANDOM =
2866*b843c749SSergey Zigachev 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2867*b843c749SSergey Zigachev 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
2868*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL10 ||
2869*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL10_FM8 ||
2870*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL10_FM6) {
2871*b843c749SSergey Zigachev 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2872*b843c749SSergey Zigachev 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
2873*b843c749SSergey Zigachev 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2874*b843c749SSergey Zigachev 		fmt_bit_depth->flags.RGB_RANDOM =
2875*b843c749SSergey Zigachev 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2876*b843c749SSergey Zigachev 	}
2877*b843c749SSergey Zigachev 
2878*b843c749SSergey Zigachev 	if (option == DITHER_OPTION_SPATIAL6 ||
2879*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL8 ||
2880*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL10) {
2881*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
2882*b843c749SSergey Zigachev 	} else {
2883*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
2884*b843c749SSergey Zigachev 	}
2885*b843c749SSergey Zigachev 
2886*b843c749SSergey Zigachev 	//////////////////////
2887*b843c749SSergey Zigachev 	//// temporal dither
2888*b843c749SSergey Zigachev 	//////////////////////
2889*b843c749SSergey Zigachev 	if (option == DITHER_OPTION_FM6           ||
2890*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL8_FM6     ||
2891*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL10_FM6     ||
2892*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_FM6     ||
2893*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN8_FM6      ||
2894*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2895*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2896*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
2897*b843c749SSergey Zigachev 	} else if (option == DITHER_OPTION_FM8        ||
2898*b843c749SSergey Zigachev 			option == DITHER_OPTION_SPATIAL10_FM8  ||
2899*b843c749SSergey Zigachev 			option == DITHER_OPTION_TRUN10_FM8) {
2900*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2901*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
2902*b843c749SSergey Zigachev 	} else if (option == DITHER_OPTION_FM10) {
2903*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2904*b843c749SSergey Zigachev 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
2905*b843c749SSergey Zigachev 	}
2906*b843c749SSergey Zigachev 
2907*b843c749SSergey Zigachev 	fmt_bit_depth->pixel_encoding = pixel_encoding;
2908*b843c749SSergey Zigachev }
2909*b843c749SSergey Zigachev 
dc_validate_stream(struct dc * dc,struct dc_stream_state * stream)2910*b843c749SSergey Zigachev enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
2911*b843c749SSergey Zigachev {
2912*b843c749SSergey Zigachev 	struct dc  *core_dc = dc;
2913*b843c749SSergey Zigachev 	struct dc_link *link = stream->sink->link;
2914*b843c749SSergey Zigachev 	struct timing_generator *tg = core_dc->res_pool->timing_generators[0];
2915*b843c749SSergey Zigachev 	enum dc_status res = DC_OK;
2916*b843c749SSergey Zigachev 
2917*b843c749SSergey Zigachev 	calculate_phy_pix_clks(stream);
2918*b843c749SSergey Zigachev 
2919*b843c749SSergey Zigachev 	if (!tg->funcs->validate_timing(tg, &stream->timing))
2920*b843c749SSergey Zigachev 		res = DC_FAIL_CONTROLLER_VALIDATE;
2921*b843c749SSergey Zigachev 
2922*b843c749SSergey Zigachev 	if (res == DC_OK)
2923*b843c749SSergey Zigachev 		if (!link->link_enc->funcs->validate_output_with_stream(
2924*b843c749SSergey Zigachev 						link->link_enc, stream))
2925*b843c749SSergey Zigachev 			res = DC_FAIL_ENC_VALIDATE;
2926*b843c749SSergey Zigachev 
2927*b843c749SSergey Zigachev 	/* TODO: validate audio ASIC caps, encoder */
2928*b843c749SSergey Zigachev 
2929*b843c749SSergey Zigachev 	if (res == DC_OK)
2930*b843c749SSergey Zigachev 		res = dc_link_validate_mode_timing(stream,
2931*b843c749SSergey Zigachev 		      link,
2932*b843c749SSergey Zigachev 		      &stream->timing);
2933*b843c749SSergey Zigachev 
2934*b843c749SSergey Zigachev 	return res;
2935*b843c749SSergey Zigachev }
2936*b843c749SSergey Zigachev 
dc_validate_plane(struct dc * dc,const struct dc_plane_state * plane_state)2937*b843c749SSergey Zigachev enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
2938*b843c749SSergey Zigachev {
2939*b843c749SSergey Zigachev 	enum dc_status res = DC_OK;
2940*b843c749SSergey Zigachev 
2941*b843c749SSergey Zigachev 	/* TODO For now validates pixel format only */
2942*b843c749SSergey Zigachev 	if (dc->res_pool->funcs->validate_plane)
2943*b843c749SSergey Zigachev 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
2944*b843c749SSergey Zigachev 
2945*b843c749SSergey Zigachev 	return res;
2946*b843c749SSergey Zigachev }
2947