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