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