2 * Copyright 2012-15 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <linux/slab.h>
28 #include "dm_services.h"
31 #include "include/irq_service_interface.h"
32 #include "link_encoder.h"
33 #include "stream_encoder.h"
35 #include "timing_generator.h"
36 #include "transform.h"
40 #include "core_types.h"
41 #include "set_mode_types.h"
42 #include "virtual/virtual_stream_encoder.h"
43 #include "dpcd_defs.h"
45 #if defined(CONFIG_DRM_AMD_DC_SI)
46 #include "dce60/dce60_resource.h"
48 #include "dce80/dce80_resource.h"
49 #include "dce100/dce100_resource.h"
50 #include "dce110/dce110_resource.h"
51 #include "dce112/dce112_resource.h"
52 #include "dce120/dce120_resource.h"
53 #if defined(CONFIG_DRM_AMD_DC_DCN)
54 #include "dcn10/dcn10_resource.h"
55 #include "dcn20/dcn20_resource.h"
56 #include "dcn21/dcn21_resource.h"
57 #include "dcn30/dcn30_resource.h"
58 #include "dcn301/dcn301_resource.h"
59 #include "dcn302/dcn302_resource.h"
62 #define DC_LOGGER_INIT(logger)
64 enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
66 enum dce_version dc_version = DCE_VERSION_UNKNOWN;
67 switch (asic_id.chip_family) {
69 #if defined(CONFIG_DRM_AMD_DC_SI)
71 if (ASIC_REV_IS_TAHITI_P(asic_id.hw_internal_rev) ||
72 ASIC_REV_IS_PITCAIRN_PM(asic_id.hw_internal_rev) ||
73 ASIC_REV_IS_CAPEVERDE_M(asic_id.hw_internal_rev))
74 dc_version = DCE_VERSION_6_0;
75 else if (ASIC_REV_IS_OLAND_M(asic_id.hw_internal_rev))
76 dc_version = DCE_VERSION_6_4;
78 dc_version = DCE_VERSION_6_1;
82 dc_version = DCE_VERSION_8_0;
85 if (ASIC_REV_IS_KALINDI(asic_id.hw_internal_rev) ||
86 ASIC_REV_IS_BHAVANI(asic_id.hw_internal_rev) ||
87 ASIC_REV_IS_GODAVARI(asic_id.hw_internal_rev))
88 dc_version = DCE_VERSION_8_3;
90 dc_version = DCE_VERSION_8_1;
93 dc_version = DCE_VERSION_11_0;
97 if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
98 ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
99 dc_version = DCE_VERSION_10_0;
102 if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
103 ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
104 ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
105 dc_version = DCE_VERSION_11_2;
107 if (ASIC_REV_IS_VEGAM(asic_id.hw_internal_rev))
108 dc_version = DCE_VERSION_11_22;
111 if (ASICREV_IS_VEGA20_P(asic_id.hw_internal_rev))
112 dc_version = DCE_VERSION_12_1;
114 dc_version = DCE_VERSION_12_0;
116 #if defined(CONFIG_DRM_AMD_DC_DCN)
118 dc_version = DCN_VERSION_1_0;
119 if (ASICREV_IS_RAVEN2(asic_id.hw_internal_rev))
120 dc_version = DCN_VERSION_1_01;
121 if (ASICREV_IS_RENOIR(asic_id.hw_internal_rev))
122 dc_version = DCN_VERSION_2_1;
123 if (ASICREV_IS_GREEN_SARDINE(asic_id.hw_internal_rev))
124 dc_version = DCN_VERSION_2_1;
128 dc_version = DCN_VERSION_2_0;
129 if (ASICREV_IS_SIENNA_CICHLID_P(asic_id.hw_internal_rev))
130 dc_version = DCN_VERSION_3_0;
131 if (ASICREV_IS_DIMGREY_CAVEFISH_P(asic_id.hw_internal_rev))
132 dc_version = DCN_VERSION_3_02;
136 dc_version = DCN_VERSION_3_01;
140 dc_version = DCE_VERSION_UNKNOWN;
146 struct resource_pool *dc_create_resource_pool(struct dc *dc,
147 const struct dc_init_data *init_data,
148 enum dce_version dc_version)
150 struct resource_pool *res_pool = NULL;
152 switch (dc_version) {
153 #if defined(CONFIG_DRM_AMD_DC_SI)
154 case DCE_VERSION_6_0:
155 res_pool = dce60_create_resource_pool(
156 init_data->num_virtual_links, dc);
158 case DCE_VERSION_6_1:
159 res_pool = dce61_create_resource_pool(
160 init_data->num_virtual_links, dc);
162 case DCE_VERSION_6_4:
163 res_pool = dce64_create_resource_pool(
164 init_data->num_virtual_links, dc);
167 case DCE_VERSION_8_0:
168 res_pool = dce80_create_resource_pool(
169 init_data->num_virtual_links, dc);
171 case DCE_VERSION_8_1:
172 res_pool = dce81_create_resource_pool(
173 init_data->num_virtual_links, dc);
175 case DCE_VERSION_8_3:
176 res_pool = dce83_create_resource_pool(
177 init_data->num_virtual_links, dc);
179 case DCE_VERSION_10_0:
180 res_pool = dce100_create_resource_pool(
181 init_data->num_virtual_links, dc);
183 case DCE_VERSION_11_0:
184 res_pool = dce110_create_resource_pool(
185 init_data->num_virtual_links, dc,
188 case DCE_VERSION_11_2:
189 case DCE_VERSION_11_22:
190 res_pool = dce112_create_resource_pool(
191 init_data->num_virtual_links, dc);
193 case DCE_VERSION_12_0:
194 case DCE_VERSION_12_1:
195 res_pool = dce120_create_resource_pool(
196 init_data->num_virtual_links, dc);
199 #if defined(CONFIG_DRM_AMD_DC_DCN)
200 case DCN_VERSION_1_0:
201 case DCN_VERSION_1_01:
202 res_pool = dcn10_create_resource_pool(init_data, dc);
204 case DCN_VERSION_2_0:
205 res_pool = dcn20_create_resource_pool(init_data, dc);
207 case DCN_VERSION_2_1:
208 res_pool = dcn21_create_resource_pool(init_data, dc);
210 case DCN_VERSION_3_0:
211 res_pool = dcn30_create_resource_pool(init_data, dc);
213 case DCN_VERSION_3_01:
214 res_pool = dcn301_create_resource_pool(init_data, dc);
216 case DCN_VERSION_3_02:
217 res_pool = dcn302_create_resource_pool(init_data, dc);
224 if (res_pool != NULL) {
225 if (dc->ctx->dc_bios->fw_info_valid) {
226 res_pool->ref_clocks.xtalin_clock_inKhz =
227 dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
228 /* initialize with firmware data first, no all
229 * ASIC have DCCG SW component. FPGA or
230 * simulation need initialization of
231 * dccg_ref_clock_inKhz, dchub_ref_clock_inKhz
232 * with xtalin_clock_inKhz
234 res_pool->ref_clocks.dccg_ref_clock_inKhz =
235 res_pool->ref_clocks.xtalin_clock_inKhz;
236 res_pool->ref_clocks.dchub_ref_clock_inKhz =
237 res_pool->ref_clocks.xtalin_clock_inKhz;
239 ASSERT_CRITICAL(false);
245 void dc_destroy_resource_pool(struct dc *dc)
249 dc->res_pool->funcs->destroy(&dc->res_pool);
255 static void update_num_audio(
256 const struct resource_straps *straps,
257 unsigned int *num_audio,
258 struct audio_support *aud_support)
260 aud_support->dp_audio = true;
261 aud_support->hdmi_audio_native = false;
262 aud_support->hdmi_audio_on_dongle = false;
264 if (straps->hdmi_disable == 0) {
265 if (straps->dc_pinstraps_audio & 0x2) {
266 aud_support->hdmi_audio_on_dongle = true;
267 aud_support->hdmi_audio_native = true;
271 switch (straps->audio_stream_number) {
272 case 0: /* multi streams supported */
274 case 1: /* multi streams not supported */
278 DC_ERR("DC: unexpected audio fuse!\n");
282 bool resource_construct(
283 unsigned int num_virtual_links,
285 struct resource_pool *pool,
286 const struct resource_create_funcs *create_funcs)
288 struct dc_context *ctx = dc->ctx;
289 const struct resource_caps *caps = pool->res_cap;
291 unsigned int num_audio = caps->num_audio;
292 struct resource_straps straps = {0};
294 if (create_funcs->read_dce_straps)
295 create_funcs->read_dce_straps(dc->ctx, &straps);
297 pool->audio_count = 0;
298 if (create_funcs->create_audio) {
299 /* find the total number of streams available via the
300 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
301 * registers (one for each pin) starting from pin 1
302 * up to the max number of audio pins.
303 * We stop on the first pin where
304 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
306 update_num_audio(&straps, &num_audio, &pool->audio_support);
307 for (i = 0; i < caps->num_audio; i++) {
308 struct audio *aud = create_funcs->create_audio(ctx, i);
311 DC_ERR("DC: failed to create audio!\n");
314 if (!aud->funcs->endpoint_valid(aud)) {
315 aud->funcs->destroy(&aud);
318 pool->audios[i] = aud;
323 pool->stream_enc_count = 0;
324 if (create_funcs->create_stream_encoder) {
325 for (i = 0; i < caps->num_stream_encoder; i++) {
326 pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
327 if (pool->stream_enc[i] == NULL)
328 DC_ERR("DC: failed to create stream_encoder!\n");
329 pool->stream_enc_count++;
333 #if defined(CONFIG_DRM_AMD_DC_DCN)
334 for (i = 0; i < caps->num_mpc_3dlut; i++) {
335 pool->mpc_lut[i] = dc_create_3dlut_func();
336 if (pool->mpc_lut[i] == NULL)
337 DC_ERR("DC: failed to create MPC 3dlut!\n");
338 pool->mpc_shaper[i] = dc_create_transfer_func();
339 if (pool->mpc_shaper[i] == NULL)
340 DC_ERR("DC: failed to create MPC shaper!\n");
343 dc->caps.dynamic_audio = false;
344 if (pool->audio_count < pool->stream_enc_count) {
345 dc->caps.dynamic_audio = true;
347 for (i = 0; i < num_virtual_links; i++) {
348 pool->stream_enc[pool->stream_enc_count] =
349 virtual_stream_encoder_create(
351 if (pool->stream_enc[pool->stream_enc_count] == NULL) {
352 DC_ERR("DC: failed to create stream_encoder!\n");
355 pool->stream_enc_count++;
358 dc->hwseq = create_funcs->create_hwseq(ctx);
362 static int find_matching_clock_source(
363 const struct resource_pool *pool,
364 struct clock_source *clock_source)
369 for (i = 0; i < pool->clk_src_count; i++) {
370 if (pool->clock_sources[i] == clock_source)
376 void resource_unreference_clock_source(
377 struct resource_context *res_ctx,
378 const struct resource_pool *pool,
379 struct clock_source *clock_source)
381 int i = find_matching_clock_source(pool, clock_source);
384 res_ctx->clock_source_ref_count[i]--;
386 if (pool->dp_clock_source == clock_source)
387 res_ctx->dp_clock_source_ref_count--;
390 void resource_reference_clock_source(
391 struct resource_context *res_ctx,
392 const struct resource_pool *pool,
393 struct clock_source *clock_source)
395 int i = find_matching_clock_source(pool, clock_source);
398 res_ctx->clock_source_ref_count[i]++;
400 if (pool->dp_clock_source == clock_source)
401 res_ctx->dp_clock_source_ref_count++;
404 int resource_get_clock_source_reference(
405 struct resource_context *res_ctx,
406 const struct resource_pool *pool,
407 struct clock_source *clock_source)
409 int i = find_matching_clock_source(pool, clock_source);
412 return res_ctx->clock_source_ref_count[i];
414 if (pool->dp_clock_source == clock_source)
415 return res_ctx->dp_clock_source_ref_count;
420 bool resource_are_vblanks_synchronizable(
421 struct dc_stream_state *stream1,
422 struct dc_stream_state *stream2)
424 uint32_t base60_refresh_rates[] = {10, 20, 5};
426 uint8_t rr_count = sizeof(base60_refresh_rates)/sizeof(base60_refresh_rates[0]);
427 uint64_t frame_time_diff;
429 if (stream1->ctx->dc->config.vblank_alignment_dto_params &&
430 stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff > 0 &&
431 dc_is_dp_signal(stream1->signal) &&
432 dc_is_dp_signal(stream2->signal) &&
433 false == stream1->has_non_synchronizable_pclk &&
434 false == stream2->has_non_synchronizable_pclk &&
435 stream1->timing.flags.VBLANK_SYNCHRONIZABLE &&
436 stream2->timing.flags.VBLANK_SYNCHRONIZABLE) {
437 /* disable refresh rates higher than 60Hz for now */
438 if (stream1->timing.pix_clk_100hz*100/stream1->timing.h_total/
439 stream1->timing.v_total > 60)
441 if (stream2->timing.pix_clk_100hz*100/stream2->timing.h_total/
442 stream2->timing.v_total > 60)
444 frame_time_diff = (uint64_t)10000 *
445 stream1->timing.h_total *
446 stream1->timing.v_total *
447 stream2->timing.pix_clk_100hz;
448 frame_time_diff = div_u64(frame_time_diff, stream1->timing.pix_clk_100hz);
449 frame_time_diff = div_u64(frame_time_diff, stream2->timing.h_total);
450 frame_time_diff = div_u64(frame_time_diff, stream2->timing.v_total);
451 for (i = 0; i < rr_count; i++) {
452 int64_t diff = (int64_t)div_u64(frame_time_diff * base60_refresh_rates[i], 10) - 10000;
456 if (diff < stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff)
463 bool resource_are_streams_timing_synchronizable(
464 struct dc_stream_state *stream1,
465 struct dc_stream_state *stream2)
467 if (stream1->timing.h_total != stream2->timing.h_total)
470 if (stream1->timing.v_total != stream2->timing.v_total)
473 if (stream1->timing.h_addressable
474 != stream2->timing.h_addressable)
477 if (stream1->timing.v_addressable
478 != stream2->timing.v_addressable)
481 if (stream1->timing.v_front_porch
482 != stream2->timing.v_front_porch)
485 if (stream1->timing.pix_clk_100hz
486 != stream2->timing.pix_clk_100hz)
489 if (stream1->clamping.c_depth != stream2->clamping.c_depth)
492 if (stream1->phy_pix_clk != stream2->phy_pix_clk
493 && (!dc_is_dp_signal(stream1->signal)
494 || !dc_is_dp_signal(stream2->signal)))
497 if (stream1->view_format != stream2->view_format)
500 if (stream1->ignore_msa_timing_param || stream2->ignore_msa_timing_param)
505 static bool is_dp_and_hdmi_sharable(
506 struct dc_stream_state *stream1,
507 struct dc_stream_state *stream2)
509 if (stream1->ctx->dc->caps.disable_dp_clk_share)
512 if (stream1->clamping.c_depth != COLOR_DEPTH_888 ||
513 stream2->clamping.c_depth != COLOR_DEPTH_888)
520 static bool is_sharable_clk_src(
521 const struct pipe_ctx *pipe_with_clk_src,
522 const struct pipe_ctx *pipe)
524 if (pipe_with_clk_src->clock_source == NULL)
527 if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
530 if (dc_is_dp_signal(pipe_with_clk_src->stream->signal) ||
531 (dc_is_dp_signal(pipe->stream->signal) &&
532 !is_dp_and_hdmi_sharable(pipe_with_clk_src->stream,
536 if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
537 && dc_is_dual_link_signal(pipe->stream->signal))
540 if (dc_is_hdmi_signal(pipe->stream->signal)
541 && dc_is_dual_link_signal(pipe_with_clk_src->stream->signal))
544 if (!resource_are_streams_timing_synchronizable(
545 pipe_with_clk_src->stream, pipe->stream))
551 struct clock_source *resource_find_used_clk_src_for_sharing(
552 struct resource_context *res_ctx,
553 struct pipe_ctx *pipe_ctx)
557 for (i = 0; i < MAX_PIPES; i++) {
558 if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
559 return res_ctx->pipe_ctx[i].clock_source;
565 static enum pixel_format convert_pixel_format_to_dalsurface(
566 enum surface_pixel_format surface_pixel_format)
568 enum pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
570 switch (surface_pixel_format) {
571 case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
572 dal_pixel_format = PIXEL_FORMAT_INDEX8;
574 case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
575 dal_pixel_format = PIXEL_FORMAT_RGB565;
577 case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
578 dal_pixel_format = PIXEL_FORMAT_RGB565;
580 case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
581 dal_pixel_format = PIXEL_FORMAT_ARGB8888;
583 case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
584 dal_pixel_format = PIXEL_FORMAT_ARGB8888;
586 case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
587 dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
589 case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
590 dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
592 case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
593 dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
595 case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
596 case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
597 dal_pixel_format = PIXEL_FORMAT_FP16;
599 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
600 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
601 dal_pixel_format = PIXEL_FORMAT_420BPP8;
603 case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
604 case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
605 dal_pixel_format = PIXEL_FORMAT_420BPP10;
607 case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
609 dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
612 return dal_pixel_format;
615 static inline void get_vp_scan_direction(
616 enum dc_rotation_angle rotation,
617 bool horizontal_mirror,
618 bool *orthogonal_rotation,
619 bool *flip_vert_scan_dir,
620 bool *flip_horz_scan_dir)
622 *orthogonal_rotation = false;
623 *flip_vert_scan_dir = false;
624 *flip_horz_scan_dir = false;
625 if (rotation == ROTATION_ANGLE_180) {
626 *flip_vert_scan_dir = true;
627 *flip_horz_scan_dir = true;
628 } else if (rotation == ROTATION_ANGLE_90) {
629 *orthogonal_rotation = true;
630 *flip_horz_scan_dir = true;
631 } else if (rotation == ROTATION_ANGLE_270) {
632 *orthogonal_rotation = true;
633 *flip_vert_scan_dir = true;
636 if (horizontal_mirror)
637 *flip_horz_scan_dir = !*flip_horz_scan_dir;
640 int get_num_mpc_splits(struct pipe_ctx *pipe)
642 int mpc_split_count = 0;
643 struct pipe_ctx *other_pipe = pipe->bottom_pipe;
645 while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
647 other_pipe = other_pipe->bottom_pipe;
649 other_pipe = pipe->top_pipe;
650 while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
652 other_pipe = other_pipe->top_pipe;
655 return mpc_split_count;
658 int get_num_odm_splits(struct pipe_ctx *pipe)
660 int odm_split_count = 0;
661 struct pipe_ctx *next_pipe = pipe->next_odm_pipe;
664 next_pipe = next_pipe->next_odm_pipe;
666 pipe = pipe->prev_odm_pipe;
669 pipe = pipe->prev_odm_pipe;
671 return odm_split_count;
674 static void calculate_split_count_and_index(struct pipe_ctx *pipe_ctx, int *split_count, int *split_idx)
676 *split_count = get_num_odm_splits(pipe_ctx);
678 if (*split_count == 0) {
679 /*Check for mpc split*/
680 struct pipe_ctx *split_pipe = pipe_ctx->top_pipe;
682 *split_count = get_num_mpc_splits(pipe_ctx);
683 while (split_pipe && split_pipe->plane_state == pipe_ctx->plane_state) {
685 split_pipe = split_pipe->top_pipe;
688 /*Get odm split index*/
689 struct pipe_ctx *split_pipe = pipe_ctx->prev_odm_pipe;
693 split_pipe = split_pipe->prev_odm_pipe;
698 static void calculate_viewport(struct pipe_ctx *pipe_ctx)
700 const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
701 const struct dc_stream_state *stream = pipe_ctx->stream;
702 struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
703 struct rect surf_src = plane_state->src_rect;
704 struct rect clip, dest;
705 int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
706 || data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
709 bool orthogonal_rotation, flip_y_start, flip_x_start;
711 calculate_split_count_and_index(pipe_ctx, &split_count, &split_idx);
713 if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE ||
714 stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM) {
719 /* The actual clip is an intersection between stream
720 * source and surface clip
722 dest = plane_state->dst_rect;
723 clip.x = stream->src.x > plane_state->clip_rect.x ?
724 stream->src.x : plane_state->clip_rect.x;
726 clip.width = stream->src.x + stream->src.width <
727 plane_state->clip_rect.x + plane_state->clip_rect.width ?
728 stream->src.x + stream->src.width - clip.x :
729 plane_state->clip_rect.x + plane_state->clip_rect.width - clip.x ;
731 clip.y = stream->src.y > plane_state->clip_rect.y ?
732 stream->src.y : plane_state->clip_rect.y;
734 clip.height = stream->src.y + stream->src.height <
735 plane_state->clip_rect.y + plane_state->clip_rect.height ?
736 stream->src.y + stream->src.height - clip.y :
737 plane_state->clip_rect.y + plane_state->clip_rect.height - clip.y ;
740 * Need to calculate how scan origin is shifted in vp space
741 * to correctly rotate clip and dst
743 get_vp_scan_direction(
744 plane_state->rotation,
745 plane_state->horizontal_mirror,
746 &orthogonal_rotation,
750 if (orthogonal_rotation) {
751 swap(clip.x, clip.y);
752 swap(clip.width, clip.height);
753 swap(dest.x, dest.y);
754 swap(dest.width, dest.height);
757 clip.x = dest.x + dest.width - clip.x - clip.width;
761 clip.y = dest.y + dest.height - clip.y - clip.height;
765 /* offset = surf_src.ofs + (clip.ofs - surface->dst_rect.ofs) * scl_ratio
766 * num_pixels = clip.num_pix * scl_ratio
768 data->viewport.x = surf_src.x + (clip.x - dest.x) * surf_src.width / dest.width;
769 data->viewport.width = clip.width * surf_src.width / dest.width;
771 data->viewport.y = surf_src.y + (clip.y - dest.y) * surf_src.height / dest.height;
772 data->viewport.height = clip.height * surf_src.height / dest.height;
776 /* extra pixels in the division remainder need to go to pipes after
777 * the extra pixel index minus one(epimo) defined here as:
781 if (orthogonal_rotation) {
783 split_idx = split_count - split_idx;
785 epimo = split_count - data->viewport.height % (split_count + 1);
787 data->viewport.y += (data->viewport.height / (split_count + 1)) * split_idx;
788 if (split_idx > epimo)
789 data->viewport.y += split_idx - epimo - 1;
790 data->viewport.height = data->viewport.height / (split_count + 1) + (split_idx > epimo ? 1 : 0);
793 split_idx = split_count - split_idx;
795 epimo = split_count - data->viewport.width % (split_count + 1);
797 data->viewport.x += (data->viewport.width / (split_count + 1)) * split_idx;
798 if (split_idx > epimo)
799 data->viewport.x += split_idx - epimo - 1;
800 data->viewport.width = data->viewport.width / (split_count + 1) + (split_idx > epimo ? 1 : 0);
804 /* Round down, compensate in init */
805 data->viewport_c.x = data->viewport.x / vpc_div;
806 data->viewport_c.y = data->viewport.y / vpc_div;
807 data->inits.h_c = (data->viewport.x % vpc_div) != 0 ? dc_fixpt_half : dc_fixpt_zero;
808 data->inits.v_c = (data->viewport.y % vpc_div) != 0 ? dc_fixpt_half : dc_fixpt_zero;
810 /* Round up, assume original video size always even dimensions */
811 data->viewport_c.width = (data->viewport.width + vpc_div - 1) / vpc_div;
812 data->viewport_c.height = (data->viewport.height + vpc_div - 1) / vpc_div;
814 data->viewport_unadjusted = data->viewport;
815 data->viewport_c_unadjusted = data->viewport_c;
818 static void calculate_recout(struct pipe_ctx *pipe_ctx)
820 const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
821 const struct dc_stream_state *stream = pipe_ctx->stream;
822 struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
823 struct rect surf_clip = plane_state->clip_rect;
824 bool pri_split_tb = pipe_ctx->bottom_pipe &&
825 pipe_ctx->bottom_pipe->plane_state == pipe_ctx->plane_state &&
826 stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM;
827 bool sec_split_tb = pipe_ctx->top_pipe &&
828 pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state &&
829 stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM;
833 calculate_split_count_and_index(pipe_ctx, &split_count, &split_idx);
836 * Only the leftmost ODM pipe should be offset by a nonzero distance
838 if (!pipe_ctx->prev_odm_pipe) {
839 data->recout.x = stream->dst.x;
840 if (stream->src.x < surf_clip.x)
841 data->recout.x += (surf_clip.x - stream->src.x) * stream->dst.width
847 if (stream->src.x > surf_clip.x)
848 surf_clip.width -= stream->src.x - surf_clip.x;
849 data->recout.width = surf_clip.width * stream->dst.width / stream->src.width;
850 if (data->recout.width + data->recout.x > stream->dst.x + stream->dst.width)
851 data->recout.width = stream->dst.x + stream->dst.width - data->recout.x;
853 data->recout.y = stream->dst.y;
854 if (stream->src.y < surf_clip.y)
855 data->recout.y += (surf_clip.y - stream->src.y) * stream->dst.height
856 / stream->src.height;
857 else if (stream->src.y > surf_clip.y)
858 surf_clip.height -= stream->src.y - surf_clip.y;
860 data->recout.height = surf_clip.height * stream->dst.height / stream->src.height;
861 if (data->recout.height + data->recout.y > stream->dst.y + stream->dst.height)
862 data->recout.height = stream->dst.y + stream->dst.height - data->recout.y;
864 /* Handle h & v split, handle rotation using viewport */
866 data->recout.y += data->recout.height / 2;
867 /* Floor primary pipe, ceil 2ndary pipe */
868 data->recout.height = (data->recout.height + 1) / 2;
869 } else if (pri_split_tb)
870 data->recout.height /= 2;
871 else if (split_count) {
872 /* extra pixels in the division remainder need to go to pipes after
873 * the extra pixel index minus one(epimo) defined here as:
875 int epimo = split_count - data->recout.width % (split_count + 1);
877 /*no recout offset due to odm */
878 if (!pipe_ctx->next_odm_pipe && !pipe_ctx->prev_odm_pipe) {
879 data->recout.x += (data->recout.width / (split_count + 1)) * split_idx;
880 if (split_idx > epimo)
881 data->recout.x += split_idx - epimo - 1;
883 data->recout.width = data->recout.width / (split_count + 1) + (split_idx > epimo ? 1 : 0);
887 static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
889 const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
890 const struct dc_stream_state *stream = pipe_ctx->stream;
891 struct rect surf_src = plane_state->src_rect;
892 const int in_w = stream->src.width;
893 const int in_h = stream->src.height;
894 const int out_w = stream->dst.width;
895 const int out_h = stream->dst.height;
897 /*Swap surf_src height and width since scaling ratios are in recout rotation*/
898 if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
899 pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
900 swap(surf_src.height, surf_src.width);
902 pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_from_fraction(
904 plane_state->dst_rect.width);
905 pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_from_fraction(
907 plane_state->dst_rect.height);
909 if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
910 pipe_ctx->plane_res.scl_data.ratios.horz.value *= 2;
911 else if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
912 pipe_ctx->plane_res.scl_data.ratios.vert.value *= 2;
914 pipe_ctx->plane_res.scl_data.ratios.vert.value = div64_s64(
915 pipe_ctx->plane_res.scl_data.ratios.vert.value * in_h, out_h);
916 pipe_ctx->plane_res.scl_data.ratios.horz.value = div64_s64(
917 pipe_ctx->plane_res.scl_data.ratios.horz.value * in_w, out_w);
919 pipe_ctx->plane_res.scl_data.ratios.horz_c = pipe_ctx->plane_res.scl_data.ratios.horz;
920 pipe_ctx->plane_res.scl_data.ratios.vert_c = pipe_ctx->plane_res.scl_data.ratios.vert;
922 if (pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP8
923 || pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP10) {
924 pipe_ctx->plane_res.scl_data.ratios.horz_c.value /= 2;
925 pipe_ctx->plane_res.scl_data.ratios.vert_c.value /= 2;
927 pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_truncate(
928 pipe_ctx->plane_res.scl_data.ratios.horz, 19);
929 pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_truncate(
930 pipe_ctx->plane_res.scl_data.ratios.vert, 19);
931 pipe_ctx->plane_res.scl_data.ratios.horz_c = dc_fixpt_truncate(
932 pipe_ctx->plane_res.scl_data.ratios.horz_c, 19);
933 pipe_ctx->plane_res.scl_data.ratios.vert_c = dc_fixpt_truncate(
934 pipe_ctx->plane_res.scl_data.ratios.vert_c, 19);
937 static inline void adjust_vp_and_init_for_seamless_clip(
942 struct fixed31_32 ratio,
943 struct fixed31_32 *init,
947 if (!flip_scan_dir) {
948 /* Adjust for viewport end clip-off */
949 if ((*vp_offset + *vp_size) < src_size) {
950 int vp_clip = src_size - *vp_size - *vp_offset;
951 int int_part = dc_fixpt_floor(dc_fixpt_sub(*init, ratio));
953 int_part = int_part > 0 ? int_part : 0;
954 *vp_size += int_part < vp_clip ? int_part : vp_clip;
957 /* Adjust for non-0 viewport offset */
961 *init = dc_fixpt_add(*init, dc_fixpt_mul_int(ratio, recout_skip));
962 int_part = dc_fixpt_floor(*init) - *vp_offset;
963 if (int_part < taps) {
964 int int_adj = *vp_offset >= (taps - int_part) ?
965 (taps - int_part) : *vp_offset;
966 *vp_offset -= int_adj;
969 } else if (int_part > taps) {
970 *vp_offset += int_part - taps;
971 *vp_size -= int_part - taps;
974 init->value &= 0xffffffff;
975 *init = dc_fixpt_add_int(*init, int_part);
978 /* Adjust for non-0 viewport offset */
980 int int_part = dc_fixpt_floor(dc_fixpt_sub(*init, ratio));
982 int_part = int_part > 0 ? int_part : 0;
983 *vp_size += int_part < *vp_offset ? int_part : *vp_offset;
984 *vp_offset -= int_part < *vp_offset ? int_part : *vp_offset;
987 /* Adjust for viewport end clip-off */
988 if ((*vp_offset + *vp_size) < src_size) {
990 int end_offset = src_size - *vp_offset - *vp_size;
993 * this is init if vp had no offset, keep in mind this is from the
994 * right side of vp due to scan direction
996 *init = dc_fixpt_add(*init, dc_fixpt_mul_int(ratio, recout_skip));
998 * this is the difference between first pixel of viewport available to read
999 * and init position, takning into account scan direction
1001 int_part = dc_fixpt_floor(*init) - end_offset;
1002 if (int_part < taps) {
1003 int int_adj = end_offset >= (taps - int_part) ?
1004 (taps - int_part) : end_offset;
1005 *vp_size += int_adj;
1006 int_part += int_adj;
1007 } else if (int_part > taps) {
1008 *vp_size += int_part - taps;
1011 init->value &= 0xffffffff;
1012 *init = dc_fixpt_add_int(*init, int_part);
1017 static void calculate_inits_and_adj_vp(struct pipe_ctx *pipe_ctx)
1019 const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1020 const struct dc_stream_state *stream = pipe_ctx->stream;
1021 struct pipe_ctx *odm_pipe = pipe_ctx;
1022 struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
1023 struct rect src = pipe_ctx->plane_state->src_rect;
1024 int recout_skip_h, recout_skip_v, surf_size_h, surf_size_v;
1025 int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
1026 || data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
1027 bool orthogonal_rotation, flip_vert_scan_dir, flip_horz_scan_dir;
1031 * Need to calculate the scan direction for viewport to make adjustments
1033 get_vp_scan_direction(
1034 plane_state->rotation,
1035 plane_state->horizontal_mirror,
1036 &orthogonal_rotation,
1037 &flip_vert_scan_dir,
1038 &flip_horz_scan_dir);
1040 /* Calculate src rect rotation adjusted to recout space */
1041 surf_size_h = src.x + src.width;
1042 surf_size_v = src.y + src.height;
1043 if (flip_horz_scan_dir)
1045 if (flip_vert_scan_dir)
1047 if (orthogonal_rotation) {
1049 swap(src.width, src.height);
1052 /*modified recout_skip_h calculation due to odm having no recout offset*/
1053 while (odm_pipe->prev_odm_pipe) {
1055 odm_pipe = odm_pipe->prev_odm_pipe;
1057 /*odm_pipe is the leftmost pipe in the ODM group*/
1058 recout_skip_h = odm_idx * data->recout.width;
1060 /* Recout matching initial vp offset = recout_offset - (stream dst offset +
1061 * ((surf dst offset - stream src offset) * 1/ stream scaling ratio)
1062 * - (surf surf_src offset * 1/ full scl ratio))
1064 recout_skip_h += odm_pipe->plane_res.scl_data.recout.x
1065 - (stream->dst.x + (plane_state->dst_rect.x - stream->src.x)
1066 * stream->dst.width / stream->src.width -
1067 src.x * plane_state->dst_rect.width / src.width
1068 * stream->dst.width / stream->src.width);
1071 recout_skip_v = data->recout.y - (stream->dst.y + (plane_state->dst_rect.y - stream->src.y)
1072 * stream->dst.height / stream->src.height -
1073 src.y * plane_state->dst_rect.height / src.height
1074 * stream->dst.height / stream->src.height);
1075 if (orthogonal_rotation)
1076 swap(recout_skip_h, recout_skip_v);
1078 * Init calculated according to formula:
1079 * init = (scaling_ratio + number_of_taps + 1) / 2
1080 * init_bot = init + scaling_ratio
1081 * init_c = init + truncated_vp_c_offset(from calculate viewport)
1083 data->inits.h = dc_fixpt_truncate(dc_fixpt_div_int(
1084 dc_fixpt_add_int(data->ratios.horz, data->taps.h_taps + 1), 2), 19);
1086 data->inits.h_c = dc_fixpt_truncate(dc_fixpt_add(data->inits.h_c, dc_fixpt_div_int(
1087 dc_fixpt_add_int(data->ratios.horz_c, data->taps.h_taps_c + 1), 2)), 19);
1089 data->inits.v = dc_fixpt_truncate(dc_fixpt_div_int(
1090 dc_fixpt_add_int(data->ratios.vert, data->taps.v_taps + 1), 2), 19);
1092 data->inits.v_c = dc_fixpt_truncate(dc_fixpt_add(data->inits.v_c, dc_fixpt_div_int(
1093 dc_fixpt_add_int(data->ratios.vert_c, data->taps.v_taps_c + 1), 2)), 19);
1096 * Taps, inits and scaling ratios are in recout space need to rotate
1097 * to viewport rotation before adjustment
1099 adjust_vp_and_init_for_seamless_clip(
1103 orthogonal_rotation ? data->taps.v_taps : data->taps.h_taps,
1104 orthogonal_rotation ? data->ratios.vert : data->ratios.horz,
1105 orthogonal_rotation ? &data->inits.v : &data->inits.h,
1107 &data->viewport.width);
1108 adjust_vp_and_init_for_seamless_clip(
1111 surf_size_h / vpc_div,
1112 orthogonal_rotation ? data->taps.v_taps_c : data->taps.h_taps_c,
1113 orthogonal_rotation ? data->ratios.vert_c : data->ratios.horz_c,
1114 orthogonal_rotation ? &data->inits.v_c : &data->inits.h_c,
1115 &data->viewport_c.x,
1116 &data->viewport_c.width);
1117 adjust_vp_and_init_for_seamless_clip(
1121 orthogonal_rotation ? data->taps.h_taps : data->taps.v_taps,
1122 orthogonal_rotation ? data->ratios.horz : data->ratios.vert,
1123 orthogonal_rotation ? &data->inits.h : &data->inits.v,
1125 &data->viewport.height);
1126 adjust_vp_and_init_for_seamless_clip(
1129 surf_size_v / vpc_div,
1130 orthogonal_rotation ? data->taps.h_taps_c : data->taps.v_taps_c,
1131 orthogonal_rotation ? data->ratios.horz_c : data->ratios.vert_c,
1132 orthogonal_rotation ? &data->inits.h_c : &data->inits.v_c,
1133 &data->viewport_c.y,
1134 &data->viewport_c.height);
1136 /* Interlaced inits based on final vert inits */
1137 data->inits.v_bot = dc_fixpt_add(data->inits.v, data->ratios.vert);
1138 data->inits.v_c_bot = dc_fixpt_add(data->inits.v_c, data->ratios.vert_c);
1143 * When handling 270 rotation in mixed SLS mode, we have
1144 * stream->timing.h_border_left that is non zero. If we are doing
1145 * pipe-splitting, this h_border_left value gets added to recout.x and when it
1146 * calls calculate_inits_and_adj_vp() and
1147 * adjust_vp_and_init_for_seamless_clip(), it can cause viewport.height for a
1148 * pipe to be incorrect.
1150 * To fix this, instead of using stream->timing.h_border_left, we can use
1151 * stream->dst.x to represent the border instead. So we will set h_border_left
1152 * to 0 and shift the appropriate amount in stream->dst.x. We will then
1153 * perform all calculations in resource_build_scaling_params() based on this
1154 * and then restore the h_border_left and stream->dst.x to their original
1157 * shift_border_left_to_dst() will shift the amount of h_border_left to
1158 * stream->dst.x and set h_border_left to 0. restore_border_left_from_dst()
1159 * will restore h_border_left and stream->dst.x back to their original values
1160 * We also need to make sure pipe_ctx->plane_res.scl_data.h_active uses the
1161 * original h_border_left value in its calculation.
1163 static int shift_border_left_to_dst(struct pipe_ctx *pipe_ctx)
1165 int store_h_border_left = pipe_ctx->stream->timing.h_border_left;
1167 if (store_h_border_left) {
1168 pipe_ctx->stream->timing.h_border_left = 0;
1169 pipe_ctx->stream->dst.x += store_h_border_left;
1171 return store_h_border_left;
1174 static void restore_border_left_from_dst(struct pipe_ctx *pipe_ctx,
1175 int store_h_border_left)
1177 pipe_ctx->stream->dst.x -= store_h_border_left;
1178 pipe_ctx->stream->timing.h_border_left = store_h_border_left;
1181 bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
1183 const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1184 struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
1186 int store_h_border_left = shift_border_left_to_dst(pipe_ctx);
1187 DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1188 /* Important: scaling ratio calculation requires pixel format,
1189 * lb depth calculation requires recout and taps require scaling ratios.
1190 * Inits require viewport, taps, ratios and recout of split pipe
1192 pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
1193 pipe_ctx->plane_state->format);
1195 calculate_scaling_ratios(pipe_ctx);
1197 calculate_viewport(pipe_ctx);
1199 if (pipe_ctx->plane_res.scl_data.viewport.height < MIN_VIEWPORT_SIZE ||
1200 pipe_ctx->plane_res.scl_data.viewport.width < MIN_VIEWPORT_SIZE) {
1201 if (store_h_border_left) {
1202 restore_border_left_from_dst(pipe_ctx,
1203 store_h_border_left);
1208 calculate_recout(pipe_ctx);
1211 * Setting line buffer pixel depth to 24bpp yields banding
1212 * on certain displays, such as the Sharp 4k
1214 pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1215 pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
1217 pipe_ctx->plane_res.scl_data.recout.x += timing->h_border_left;
1218 pipe_ctx->plane_res.scl_data.recout.y += timing->v_border_top;
1220 pipe_ctx->plane_res.scl_data.h_active = timing->h_addressable +
1221 store_h_border_left + timing->h_border_right;
1222 pipe_ctx->plane_res.scl_data.v_active = timing->v_addressable +
1223 timing->v_border_top + timing->v_border_bottom;
1224 if (pipe_ctx->next_odm_pipe || pipe_ctx->prev_odm_pipe)
1225 pipe_ctx->plane_res.scl_data.h_active /= get_num_odm_splits(pipe_ctx) + 1;
1227 /* Taps calculations */
1228 if (pipe_ctx->plane_res.xfm != NULL)
1229 res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1230 pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1232 if (pipe_ctx->plane_res.dpp != NULL)
1233 res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1234 pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1238 /* Try 24 bpp linebuffer */
1239 pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
1241 if (pipe_ctx->plane_res.xfm != NULL)
1242 res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1243 pipe_ctx->plane_res.xfm,
1244 &pipe_ctx->plane_res.scl_data,
1245 &plane_state->scaling_quality);
1247 if (pipe_ctx->plane_res.dpp != NULL)
1248 res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1249 pipe_ctx->plane_res.dpp,
1250 &pipe_ctx->plane_res.scl_data,
1251 &plane_state->scaling_quality);
1255 /* May need to re-check lb size after this in some obscure scenario */
1256 calculate_inits_and_adj_vp(pipe_ctx);
1258 DC_LOG_SCALER("%s pipe %d:\nViewport: height:%d width:%d x:%d y:%d Recout: height:%d width:%d x:%d y:%d HACTIVE:%d VACTIVE:%d\n"
1259 "src_rect: height:%d width:%d x:%d y:%d dst_rect: height:%d width:%d x:%d y:%d clip_rect: height:%d width:%d x:%d y:%d\n",
1262 pipe_ctx->plane_res.scl_data.viewport.height,
1263 pipe_ctx->plane_res.scl_data.viewport.width,
1264 pipe_ctx->plane_res.scl_data.viewport.x,
1265 pipe_ctx->plane_res.scl_data.viewport.y,
1266 pipe_ctx->plane_res.scl_data.recout.height,
1267 pipe_ctx->plane_res.scl_data.recout.width,
1268 pipe_ctx->plane_res.scl_data.recout.x,
1269 pipe_ctx->plane_res.scl_data.recout.y,
1270 pipe_ctx->plane_res.scl_data.h_active,
1271 pipe_ctx->plane_res.scl_data.v_active,
1272 plane_state->src_rect.height,
1273 plane_state->src_rect.width,
1274 plane_state->src_rect.x,
1275 plane_state->src_rect.y,
1276 plane_state->dst_rect.height,
1277 plane_state->dst_rect.width,
1278 plane_state->dst_rect.x,
1279 plane_state->dst_rect.y,
1280 plane_state->clip_rect.height,
1281 plane_state->clip_rect.width,
1282 plane_state->clip_rect.x,
1283 plane_state->clip_rect.y);
1285 if (store_h_border_left)
1286 restore_border_left_from_dst(pipe_ctx, store_h_border_left);
1292 enum dc_status resource_build_scaling_params_for_context(
1293 const struct dc *dc,
1294 struct dc_state *context)
1298 for (i = 0; i < MAX_PIPES; i++) {
1299 if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
1300 context->res_ctx.pipe_ctx[i].stream != NULL)
1301 if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1302 return DC_FAIL_SCALING;
1308 struct pipe_ctx *find_idle_secondary_pipe(
1309 struct resource_context *res_ctx,
1310 const struct resource_pool *pool,
1311 const struct pipe_ctx *primary_pipe)
1314 struct pipe_ctx *secondary_pipe = NULL;
1317 * We add a preferred pipe mapping to avoid the chance that
1318 * MPCCs already in use will need to be reassigned to other trees.
1319 * For example, if we went with the strict, assign backwards logic:
1322 * Display A on, no surface, top pipe = 0
1323 * Display B on, no surface, top pipe = 1
1326 * Display A on, no surface, top pipe = 0
1327 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
1330 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
1331 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1333 * The state 2->3 transition requires remapping MPCC 5 from display B
1336 * However, with the preferred pipe logic, state 2 would look like:
1339 * Display A on, no surface, top pipe = 0
1340 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1342 * This would then cause 2->3 to not require remapping any MPCCs.
1345 int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
1346 if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1347 secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1348 secondary_pipe->pipe_idx = preferred_pipe_idx;
1353 * search backwards for the second pipe to keep pipe
1354 * assignment more consistent
1356 if (!secondary_pipe)
1357 for (i = pool->pipe_count - 1; i >= 0; i--) {
1358 if (res_ctx->pipe_ctx[i].stream == NULL) {
1359 secondary_pipe = &res_ctx->pipe_ctx[i];
1360 secondary_pipe->pipe_idx = i;
1365 return secondary_pipe;
1368 struct pipe_ctx *resource_get_head_pipe_for_stream(
1369 struct resource_context *res_ctx,
1370 struct dc_stream_state *stream)
1374 for (i = 0; i < MAX_PIPES; i++) {
1375 if (res_ctx->pipe_ctx[i].stream == stream
1376 && !res_ctx->pipe_ctx[i].top_pipe
1377 && !res_ctx->pipe_ctx[i].prev_odm_pipe)
1378 return &res_ctx->pipe_ctx[i];
1383 static struct pipe_ctx *resource_get_tail_pipe(
1384 struct resource_context *res_ctx,
1385 struct pipe_ctx *head_pipe)
1387 struct pipe_ctx *tail_pipe;
1389 tail_pipe = head_pipe->bottom_pipe;
1392 head_pipe = tail_pipe;
1393 tail_pipe = tail_pipe->bottom_pipe;
1400 * A free_pipe for a stream is defined here as a pipe
1401 * that has no surface attached yet
1403 static struct pipe_ctx *acquire_free_pipe_for_head(
1404 struct dc_state *context,
1405 const struct resource_pool *pool,
1406 struct pipe_ctx *head_pipe)
1409 struct resource_context *res_ctx = &context->res_ctx;
1411 if (!head_pipe->plane_state)
1414 /* Re-use pipe already acquired for this stream if available*/
1415 for (i = pool->pipe_count - 1; i >= 0; i--) {
1416 if (res_ctx->pipe_ctx[i].stream == head_pipe->stream &&
1417 !res_ctx->pipe_ctx[i].plane_state) {
1418 return &res_ctx->pipe_ctx[i];
1423 * At this point we have no re-useable pipe for this stream and we need
1424 * to acquire an idle one to satisfy the request
1427 if (!pool->funcs->acquire_idle_pipe_for_layer)
1430 return pool->funcs->acquire_idle_pipe_for_layer(context, pool, head_pipe->stream);
1433 #if defined(CONFIG_DRM_AMD_DC_DCN)
1434 static int acquire_first_split_pipe(
1435 struct resource_context *res_ctx,
1436 const struct resource_pool *pool,
1437 struct dc_stream_state *stream)
1441 for (i = 0; i < pool->pipe_count; i++) {
1442 struct pipe_ctx *split_pipe = &res_ctx->pipe_ctx[i];
1444 if (split_pipe->top_pipe &&
1445 split_pipe->top_pipe->plane_state == split_pipe->plane_state) {
1446 split_pipe->top_pipe->bottom_pipe = split_pipe->bottom_pipe;
1447 if (split_pipe->bottom_pipe)
1448 split_pipe->bottom_pipe->top_pipe = split_pipe->top_pipe;
1450 if (split_pipe->top_pipe->plane_state)
1451 resource_build_scaling_params(split_pipe->top_pipe);
1453 memset(split_pipe, 0, sizeof(*split_pipe));
1454 split_pipe->stream_res.tg = pool->timing_generators[i];
1455 split_pipe->plane_res.hubp = pool->hubps[i];
1456 split_pipe->plane_res.ipp = pool->ipps[i];
1457 split_pipe->plane_res.dpp = pool->dpps[i];
1458 split_pipe->stream_res.opp = pool->opps[i];
1459 split_pipe->plane_res.mpcc_inst = pool->dpps[i]->inst;
1460 split_pipe->pipe_idx = i;
1462 split_pipe->stream = stream;
1470 bool dc_add_plane_to_context(
1471 const struct dc *dc,
1472 struct dc_stream_state *stream,
1473 struct dc_plane_state *plane_state,
1474 struct dc_state *context)
1477 struct resource_pool *pool = dc->res_pool;
1478 struct pipe_ctx *head_pipe, *tail_pipe, *free_pipe;
1479 struct dc_stream_status *stream_status = NULL;
1481 for (i = 0; i < context->stream_count; i++)
1482 if (context->streams[i] == stream) {
1483 stream_status = &context->stream_status[i];
1486 if (stream_status == NULL) {
1487 dm_error("Existing stream not found; failed to attach surface!\n");
1492 if (stream_status->plane_count == MAX_SURFACE_NUM) {
1493 dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
1494 plane_state, MAX_SURFACE_NUM);
1498 head_pipe = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
1501 dm_error("Head pipe not found for stream_state %p !\n", stream);
1505 /* retain new surface, but only once per stream */
1506 dc_plane_state_retain(plane_state);
1509 free_pipe = acquire_free_pipe_for_head(context, pool, head_pipe);
1511 #if defined(CONFIG_DRM_AMD_DC_DCN)
1513 int pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
1515 free_pipe = &context->res_ctx.pipe_ctx[pipe_idx];
1519 dc_plane_state_release(plane_state);
1523 free_pipe->plane_state = plane_state;
1525 if (head_pipe != free_pipe) {
1526 tail_pipe = resource_get_tail_pipe(&context->res_ctx, head_pipe);
1528 free_pipe->stream_res.tg = tail_pipe->stream_res.tg;
1529 free_pipe->stream_res.abm = tail_pipe->stream_res.abm;
1530 free_pipe->stream_res.opp = tail_pipe->stream_res.opp;
1531 free_pipe->stream_res.stream_enc = tail_pipe->stream_res.stream_enc;
1532 free_pipe->stream_res.audio = tail_pipe->stream_res.audio;
1533 free_pipe->clock_source = tail_pipe->clock_source;
1534 free_pipe->top_pipe = tail_pipe;
1535 tail_pipe->bottom_pipe = free_pipe;
1536 if (!free_pipe->next_odm_pipe && tail_pipe->next_odm_pipe && tail_pipe->next_odm_pipe->bottom_pipe) {
1537 free_pipe->next_odm_pipe = tail_pipe->next_odm_pipe->bottom_pipe;
1538 tail_pipe->next_odm_pipe->bottom_pipe->prev_odm_pipe = free_pipe;
1540 if (!free_pipe->prev_odm_pipe && tail_pipe->prev_odm_pipe && tail_pipe->prev_odm_pipe->bottom_pipe) {
1541 free_pipe->prev_odm_pipe = tail_pipe->prev_odm_pipe->bottom_pipe;
1542 tail_pipe->prev_odm_pipe->bottom_pipe->next_odm_pipe = free_pipe;
1545 head_pipe = head_pipe->next_odm_pipe;
1547 /* assign new surfaces*/
1548 stream_status->plane_states[stream_status->plane_count] = plane_state;
1550 stream_status->plane_count++;
1555 bool dc_remove_plane_from_context(
1556 const struct dc *dc,
1557 struct dc_stream_state *stream,
1558 struct dc_plane_state *plane_state,
1559 struct dc_state *context)
1562 struct dc_stream_status *stream_status = NULL;
1563 struct resource_pool *pool = dc->res_pool;
1565 for (i = 0; i < context->stream_count; i++)
1566 if (context->streams[i] == stream) {
1567 stream_status = &context->stream_status[i];
1571 if (stream_status == NULL) {
1572 dm_error("Existing stream not found; failed to remove plane.\n");
1576 /* release pipe for plane*/
1577 for (i = pool->pipe_count - 1; i >= 0; i--) {
1578 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1580 if (pipe_ctx->plane_state == plane_state) {
1581 if (pipe_ctx->top_pipe)
1582 pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
1584 /* Second condition is to avoid setting NULL to top pipe
1585 * of tail pipe making it look like head pipe in subsequent
1588 if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
1589 pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
1592 * For head pipe detach surfaces from pipe for tail
1593 * pipe just zero it out
1595 if (!pipe_ctx->top_pipe)
1596 pipe_ctx->plane_state = NULL;
1598 memset(pipe_ctx, 0, sizeof(*pipe_ctx));
1603 for (i = 0; i < stream_status->plane_count; i++) {
1604 if (stream_status->plane_states[i] == plane_state) {
1606 dc_plane_state_release(stream_status->plane_states[i]);
1611 if (i == stream_status->plane_count) {
1612 dm_error("Existing plane_state not found; failed to detach it!\n");
1616 stream_status->plane_count--;
1618 /* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
1619 for (; i < stream_status->plane_count; i++)
1620 stream_status->plane_states[i] = stream_status->plane_states[i + 1];
1622 stream_status->plane_states[stream_status->plane_count] = NULL;
1627 bool dc_rem_all_planes_for_stream(
1628 const struct dc *dc,
1629 struct dc_stream_state *stream,
1630 struct dc_state *context)
1632 int i, old_plane_count;
1633 struct dc_stream_status *stream_status = NULL;
1634 struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
1636 for (i = 0; i < context->stream_count; i++)
1637 if (context->streams[i] == stream) {
1638 stream_status = &context->stream_status[i];
1642 if (stream_status == NULL) {
1643 dm_error("Existing stream %p not found!\n", stream);
1647 old_plane_count = stream_status->plane_count;
1649 for (i = 0; i < old_plane_count; i++)
1650 del_planes[i] = stream_status->plane_states[i];
1652 for (i = 0; i < old_plane_count; i++)
1653 if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
1659 static bool add_all_planes_for_stream(
1660 const struct dc *dc,
1661 struct dc_stream_state *stream,
1662 const struct dc_validation_set set[],
1664 struct dc_state *context)
1668 for (i = 0; i < set_count; i++)
1669 if (set[i].stream == stream)
1672 if (i == set_count) {
1673 dm_error("Stream %p not found in set!\n", stream);
1677 for (j = 0; j < set[i].plane_count; j++)
1678 if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
1684 bool dc_add_all_planes_for_stream(
1685 const struct dc *dc,
1686 struct dc_stream_state *stream,
1687 struct dc_plane_state * const *plane_states,
1689 struct dc_state *context)
1691 struct dc_validation_set set;
1694 set.stream = stream;
1695 set.plane_count = plane_count;
1697 for (i = 0; i < plane_count; i++)
1698 set.plane_states[i] = plane_states[i];
1700 return add_all_planes_for_stream(dc, stream, &set, 1, context);
1703 static bool is_timing_changed(struct dc_stream_state *cur_stream,
1704 struct dc_stream_state *new_stream)
1706 if (cur_stream == NULL)
1709 /* If output color space is changed, need to reprogram info frames */
1710 if (cur_stream->output_color_space != new_stream->output_color_space)
1714 &cur_stream->timing,
1715 &new_stream->timing,
1716 sizeof(struct dc_crtc_timing)) != 0;
1719 static bool are_stream_backends_same(
1720 struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
1722 if (stream_a == stream_b)
1725 if (stream_a == NULL || stream_b == NULL)
1728 if (is_timing_changed(stream_a, stream_b))
1731 if (stream_a->dpms_off != stream_b->dpms_off)
1738 * dc_is_stream_unchanged() - Compare two stream states for equivalence.
1740 * Checks if there a difference between the two states
1741 * that would require a mode change.
1743 * Does not compare cursor position or attributes.
1745 bool dc_is_stream_unchanged(
1746 struct dc_stream_state *old_stream, struct dc_stream_state *stream)
1749 if (!are_stream_backends_same(old_stream, stream))
1752 if (old_stream->ignore_msa_timing_param != stream->ignore_msa_timing_param)
1759 * dc_is_stream_scaling_unchanged() - Compare scaling rectangles of two streams.
1761 bool dc_is_stream_scaling_unchanged(
1762 struct dc_stream_state *old_stream, struct dc_stream_state *stream)
1764 if (old_stream == stream)
1767 if (old_stream == NULL || stream == NULL)
1770 if (memcmp(&old_stream->src,
1772 sizeof(struct rect)) != 0)
1775 if (memcmp(&old_stream->dst,
1777 sizeof(struct rect)) != 0)
1783 static void update_stream_engine_usage(
1784 struct resource_context *res_ctx,
1785 const struct resource_pool *pool,
1786 struct stream_encoder *stream_enc,
1791 for (i = 0; i < pool->stream_enc_count; i++) {
1792 if (pool->stream_enc[i] == stream_enc)
1793 res_ctx->is_stream_enc_acquired[i] = acquired;
1797 /* TODO: release audio object */
1798 void update_audio_usage(
1799 struct resource_context *res_ctx,
1800 const struct resource_pool *pool,
1801 struct audio *audio,
1805 for (i = 0; i < pool->audio_count; i++) {
1806 if (pool->audios[i] == audio)
1807 res_ctx->is_audio_acquired[i] = acquired;
1811 static int acquire_first_free_pipe(
1812 struct resource_context *res_ctx,
1813 const struct resource_pool *pool,
1814 struct dc_stream_state *stream)
1818 for (i = 0; i < pool->pipe_count; i++) {
1819 if (!res_ctx->pipe_ctx[i].stream) {
1820 struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
1822 pipe_ctx->stream_res.tg = pool->timing_generators[i];
1823 pipe_ctx->plane_res.mi = pool->mis[i];
1824 pipe_ctx->plane_res.hubp = pool->hubps[i];
1825 pipe_ctx->plane_res.ipp = pool->ipps[i];
1826 pipe_ctx->plane_res.xfm = pool->transforms[i];
1827 pipe_ctx->plane_res.dpp = pool->dpps[i];
1828 pipe_ctx->stream_res.opp = pool->opps[i];
1830 pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
1831 pipe_ctx->pipe_idx = i;
1834 pipe_ctx->stream = stream;
1841 static struct audio *find_first_free_audio(
1842 struct resource_context *res_ctx,
1843 const struct resource_pool *pool,
1845 enum dce_version dc_version)
1847 int i, available_audio_count;
1849 available_audio_count = pool->audio_count;
1851 for (i = 0; i < available_audio_count; i++) {
1852 if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
1853 /*we have enough audio endpoint, find the matching inst*/
1856 return pool->audios[i];
1860 /* use engine id to find free audio */
1861 if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
1862 return pool->audios[id];
1864 /*not found the matching one, first come first serve*/
1865 for (i = 0; i < available_audio_count; i++) {
1866 if (res_ctx->is_audio_acquired[i] == false) {
1867 return pool->audios[i];
1874 * dc_add_stream_to_ctx() - Add a new dc_stream_state to a dc_state.
1876 enum dc_status dc_add_stream_to_ctx(
1878 struct dc_state *new_ctx,
1879 struct dc_stream_state *stream)
1882 DC_LOGGER_INIT(dc->ctx->logger);
1884 if (new_ctx->stream_count >= dc->res_pool->timing_generator_count) {
1885 DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
1886 return DC_ERROR_UNEXPECTED;
1889 new_ctx->streams[new_ctx->stream_count] = stream;
1890 dc_stream_retain(stream);
1891 new_ctx->stream_count++;
1893 res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
1895 DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);
1901 * dc_remove_stream_from_ctx() - Remove a stream from a dc_state.
1903 enum dc_status dc_remove_stream_from_ctx(
1905 struct dc_state *new_ctx,
1906 struct dc_stream_state *stream)
1909 struct dc_context *dc_ctx = dc->ctx;
1910 struct pipe_ctx *del_pipe = resource_get_head_pipe_for_stream(&new_ctx->res_ctx, stream);
1911 struct pipe_ctx *odm_pipe;
1914 DC_ERROR("Pipe not found for stream %p !\n", stream);
1915 return DC_ERROR_UNEXPECTED;
1918 odm_pipe = del_pipe->next_odm_pipe;
1920 /* Release primary pipe */
1921 ASSERT(del_pipe->stream_res.stream_enc);
1922 update_stream_engine_usage(
1925 del_pipe->stream_res.stream_enc,
1927 /* Release link encoder from stream in new dc_state. */
1928 if (dc->res_pool->funcs->link_enc_unassign)
1929 dc->res_pool->funcs->link_enc_unassign(new_ctx, del_pipe->stream);
1931 if (del_pipe->stream_res.audio)
1935 del_pipe->stream_res.audio,
1938 resource_unreference_clock_source(&new_ctx->res_ctx,
1940 del_pipe->clock_source);
1942 if (dc->res_pool->funcs->remove_stream_from_ctx)
1943 dc->res_pool->funcs->remove_stream_from_ctx(dc, new_ctx, stream);
1946 struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
1948 memset(odm_pipe, 0, sizeof(*odm_pipe));
1949 odm_pipe = next_odm_pipe;
1951 memset(del_pipe, 0, sizeof(*del_pipe));
1953 for (i = 0; i < new_ctx->stream_count; i++)
1954 if (new_ctx->streams[i] == stream)
1957 if (new_ctx->streams[i] != stream) {
1958 DC_ERROR("Context doesn't have stream %p !\n", stream);
1959 return DC_ERROR_UNEXPECTED;
1962 dc_stream_release(new_ctx->streams[i]);
1963 new_ctx->stream_count--;
1965 /* Trim back arrays */
1966 for (; i < new_ctx->stream_count; i++) {
1967 new_ctx->streams[i] = new_ctx->streams[i + 1];
1968 new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
1971 new_ctx->streams[new_ctx->stream_count] = NULL;
1973 &new_ctx->stream_status[new_ctx->stream_count],
1975 sizeof(new_ctx->stream_status[0]));
1980 static struct dc_stream_state *find_pll_sharable_stream(
1981 struct dc_stream_state *stream_needs_pll,
1982 struct dc_state *context)
1986 for (i = 0; i < context->stream_count; i++) {
1987 struct dc_stream_state *stream_has_pll = context->streams[i];
1989 /* We are looking for non dp, non virtual stream */
1990 if (resource_are_streams_timing_synchronizable(
1991 stream_needs_pll, stream_has_pll)
1992 && !dc_is_dp_signal(stream_has_pll->signal)
1993 && stream_has_pll->link->connector_signal
1994 != SIGNAL_TYPE_VIRTUAL)
1995 return stream_has_pll;
2002 static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
2004 uint32_t pix_clk = timing->pix_clk_100hz;
2005 uint32_t normalized_pix_clk = pix_clk;
2007 if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2009 if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
2010 switch (timing->display_color_depth) {
2011 case COLOR_DEPTH_666:
2012 case COLOR_DEPTH_888:
2013 normalized_pix_clk = pix_clk;
2015 case COLOR_DEPTH_101010:
2016 normalized_pix_clk = (pix_clk * 30) / 24;
2018 case COLOR_DEPTH_121212:
2019 normalized_pix_clk = (pix_clk * 36) / 24;
2021 case COLOR_DEPTH_161616:
2022 normalized_pix_clk = (pix_clk * 48) / 24;
2029 return normalized_pix_clk;
2032 static void calculate_phy_pix_clks(struct dc_stream_state *stream)
2034 /* update actual pixel clock on all streams */
2035 if (dc_is_hdmi_signal(stream->signal))
2036 stream->phy_pix_clk = get_norm_pix_clk(
2037 &stream->timing) / 10;
2039 stream->phy_pix_clk =
2040 stream->timing.pix_clk_100hz / 10;
2042 if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
2043 stream->phy_pix_clk *= 2;
2046 static int acquire_resource_from_hw_enabled_state(
2047 struct resource_context *res_ctx,
2048 const struct resource_pool *pool,
2049 struct dc_stream_state *stream)
2051 struct dc_link *link = stream->link;
2052 unsigned int i, inst, tg_inst = 0;
2054 /* Check for enabled DIG to identify enabled display */
2055 if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
2058 inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
2060 if (inst == ENGINE_ID_UNKNOWN)
2063 for (i = 0; i < pool->stream_enc_count; i++) {
2064 if (pool->stream_enc[i]->id == inst) {
2065 tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
2066 pool->stream_enc[i]);
2071 // tg_inst not found
2072 if (i == pool->stream_enc_count)
2075 if (tg_inst >= pool->timing_generator_count)
2078 if (!res_ctx->pipe_ctx[tg_inst].stream) {
2079 struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
2081 pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
2082 pipe_ctx->plane_res.mi = pool->mis[tg_inst];
2083 pipe_ctx->plane_res.hubp = pool->hubps[tg_inst];
2084 pipe_ctx->plane_res.ipp = pool->ipps[tg_inst];
2085 pipe_ctx->plane_res.xfm = pool->transforms[tg_inst];
2086 pipe_ctx->plane_res.dpp = pool->dpps[tg_inst];
2087 pipe_ctx->stream_res.opp = pool->opps[tg_inst];
2089 if (pool->dpps[tg_inst]) {
2090 pipe_ctx->plane_res.mpcc_inst = pool->dpps[tg_inst]->inst;
2092 // Read DPP->MPCC->OPP Pipe from HW State
2093 if (pool->mpc->funcs->read_mpcc_state) {
2094 struct mpcc_state s = {0};
2096 pool->mpc->funcs->read_mpcc_state(pool->mpc, pipe_ctx->plane_res.mpcc_inst, &s);
2098 if (s.dpp_id < MAX_MPCC)
2099 pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].dpp_id = s.dpp_id;
2101 if (s.bot_mpcc_id < MAX_MPCC)
2102 pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].mpcc_bot =
2103 &pool->mpc->mpcc_array[s.bot_mpcc_id];
2105 if (s.opp_id < MAX_OPP)
2106 pipe_ctx->stream_res.opp->mpc_tree_params.opp_id = s.opp_id;
2109 pipe_ctx->pipe_idx = tg_inst;
2111 pipe_ctx->stream = stream;
2118 static void mark_seamless_boot_stream(
2119 const struct dc *dc,
2120 struct dc_stream_state *stream)
2122 struct dc_bios *dcb = dc->ctx->dc_bios;
2124 /* TODO: Check Linux */
2125 if (dc->config.allow_seamless_boot_optimization &&
2126 !dcb->funcs->is_accelerated_mode(dcb)) {
2127 if (dc_validate_seamless_boot_timing(dc, stream->sink, &stream->timing))
2128 stream->apply_seamless_boot_optimization = true;
2132 enum dc_status resource_map_pool_resources(
2133 const struct dc *dc,
2134 struct dc_state *context,
2135 struct dc_stream_state *stream)
2137 const struct resource_pool *pool = dc->res_pool;
2139 struct dc_context *dc_ctx = dc->ctx;
2140 struct pipe_ctx *pipe_ctx = NULL;
2143 calculate_phy_pix_clks(stream);
2145 mark_seamless_boot_stream(dc, stream);
2147 if (stream->apply_seamless_boot_optimization) {
2148 pipe_idx = acquire_resource_from_hw_enabled_state(
2153 /* hw resource was assigned to other stream */
2154 stream->apply_seamless_boot_optimization = false;
2158 /* acquire new resources */
2159 pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
2161 #ifdef CONFIG_DRM_AMD_DC_DCN
2163 pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
2166 if (pipe_idx < 0 || context->res_ctx.pipe_ctx[pipe_idx].stream_res.tg == NULL)
2167 return DC_NO_CONTROLLER_RESOURCE;
2169 pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
2171 pipe_ctx->stream_res.stream_enc =
2172 dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
2173 &context->res_ctx, pool, stream);
2175 if (!pipe_ctx->stream_res.stream_enc)
2176 return DC_NO_STREAM_ENC_RESOURCE;
2178 update_stream_engine_usage(
2179 &context->res_ctx, pool,
2180 pipe_ctx->stream_res.stream_enc,
2183 /* TODO: Add check if ASIC support and EDID audio */
2184 if (!stream->converter_disable_audio &&
2185 dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
2186 stream->audio_info.mode_count && stream->audio_info.flags.all) {
2187 pipe_ctx->stream_res.audio = find_first_free_audio(
2188 &context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
2191 * Audio assigned in order first come first get.
2192 * There are asics which has number of audio
2193 * resources less then number of pipes
2195 if (pipe_ctx->stream_res.audio)
2196 update_audio_usage(&context->res_ctx, pool,
2197 pipe_ctx->stream_res.audio, true);
2200 /* Add ABM to the resource if on EDP */
2201 if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal)) {
2202 #if defined(CONFIG_DRM_AMD_DC_DCN)
2204 pipe_ctx->stream_res.abm = pool->abm;
2206 pipe_ctx->stream_res.abm = pool->multiple_abms[pipe_ctx->stream_res.tg->inst];
2208 pipe_ctx->stream_res.abm = pool->abm;
2212 for (i = 0; i < context->stream_count; i++)
2213 if (context->streams[i] == stream) {
2214 context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
2215 context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->stream_enc_inst;
2216 context->stream_status[i].audio_inst =
2217 pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
2222 DC_ERROR("Stream %p not found in new ctx!\n", stream);
2223 return DC_ERROR_UNEXPECTED;
2227 * dc_resource_state_copy_construct_current() - Creates a new dc_state from existing state
2228 * Is a shallow copy. Increments refcounts on existing streams and planes.
2229 * @dc: copy out of dc->current_state
2230 * @dst_ctx: copy into this
2232 void dc_resource_state_copy_construct_current(
2233 const struct dc *dc,
2234 struct dc_state *dst_ctx)
2236 dc_resource_state_copy_construct(dc->current_state, dst_ctx);
2240 void dc_resource_state_construct(
2241 const struct dc *dc,
2242 struct dc_state *dst_ctx)
2244 dst_ctx->clk_mgr = dc->clk_mgr;
2248 bool dc_resource_is_dsc_encoding_supported(const struct dc *dc)
2250 return dc->res_pool->res_cap->num_dsc > 0;
2255 * dc_validate_global_state() - Determine if HW can support a given state
2256 * Checks HW resource availability and bandwidth requirement.
2257 * @dc: dc struct for this driver
2258 * @new_ctx: state to be validated
2259 * @fast_validate: set to true if only yes/no to support matters
2261 * Return: DC_OK if the result can be programmed. Otherwise, an error code.
2263 enum dc_status dc_validate_global_state(
2265 struct dc_state *new_ctx,
2268 enum dc_status result = DC_ERROR_UNEXPECTED;
2272 return DC_ERROR_UNEXPECTED;
2274 if (dc->res_pool->funcs->validate_global) {
2275 result = dc->res_pool->funcs->validate_global(dc, new_ctx);
2276 if (result != DC_OK)
2280 for (i = 0; i < new_ctx->stream_count; i++) {
2281 struct dc_stream_state *stream = new_ctx->streams[i];
2283 for (j = 0; j < dc->res_pool->pipe_count; j++) {
2284 struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
2286 if (pipe_ctx->stream != stream)
2289 if (dc->res_pool->funcs->patch_unknown_plane_state &&
2290 pipe_ctx->plane_state &&
2291 pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
2292 result = dc->res_pool->funcs->patch_unknown_plane_state(pipe_ctx->plane_state);
2293 if (result != DC_OK)
2297 /* Switch to dp clock source only if there is
2298 * no non dp stream that shares the same timing
2299 * with the dp stream.
2301 if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
2302 !find_pll_sharable_stream(stream, new_ctx)) {
2304 resource_unreference_clock_source(
2307 pipe_ctx->clock_source);
2309 pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
2310 resource_reference_clock_source(
2313 pipe_ctx->clock_source);
2318 result = resource_build_scaling_params_for_context(dc, new_ctx);
2320 if (result == DC_OK)
2321 if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, fast_validate))
2322 result = DC_FAIL_BANDWIDTH_VALIDATE;
2327 static void patch_gamut_packet_checksum(
2328 struct dc_info_packet *gamut_packet)
2330 /* For gamut we recalc checksum */
2331 if (gamut_packet->valid) {
2332 uint8_t chk_sum = 0;
2336 /*start of the Gamut data. */
2337 ptr = &gamut_packet->sb[3];
2339 for (i = 0; i <= gamut_packet->sb[1]; i++)
2342 gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
2346 static void set_avi_info_frame(
2347 struct dc_info_packet *info_packet,
2348 struct pipe_ctx *pipe_ctx)
2350 struct dc_stream_state *stream = pipe_ctx->stream;
2351 enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
2352 uint32_t pixel_encoding = 0;
2353 enum scanning_type scan_type = SCANNING_TYPE_NODATA;
2354 enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
2356 uint8_t itc_value = 0;
2357 uint8_t cn0_cn1 = 0;
2358 unsigned int cn0_cn1_value = 0;
2359 uint8_t *check_sum = NULL;
2360 uint8_t byte_index = 0;
2361 union hdmi_info_packet hdmi_info;
2362 union display_content_support support = {0};
2363 unsigned int vic = pipe_ctx->stream->timing.vic;
2364 enum dc_timing_3d_format format;
2366 memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
2368 color_space = pipe_ctx->stream->output_color_space;
2369 if (color_space == COLOR_SPACE_UNKNOWN)
2370 color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
2371 COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
2373 /* Initialize header */
2374 hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
2375 /* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
2376 * not be used in HDMI 2.0 (Section 10.1) */
2377 hdmi_info.bits.header.version = 2;
2378 hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
2381 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
2382 * according to HDMI 2.0 spec (Section 10.1)
2385 switch (stream->timing.pixel_encoding) {
2386 case PIXEL_ENCODING_YCBCR422:
2390 case PIXEL_ENCODING_YCBCR444:
2393 case PIXEL_ENCODING_YCBCR420:
2397 case PIXEL_ENCODING_RGB:
2402 /* Y0_Y1_Y2 : The pixel encoding */
2403 /* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
2404 hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
2406 /* A0 = 1 Active Format Information valid */
2407 hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
2409 /* B0, B1 = 3; Bar info data is valid */
2410 hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
2412 hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
2414 /* S0, S1 : Underscan / Overscan */
2415 /* TODO: un-hardcode scan type */
2416 scan_type = SCANNING_TYPE_UNDERSCAN;
2417 hdmi_info.bits.S0_S1 = scan_type;
2419 /* C0, C1 : Colorimetry */
2420 if (color_space == COLOR_SPACE_YCBCR709 ||
2421 color_space == COLOR_SPACE_YCBCR709_LIMITED)
2422 hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
2423 else if (color_space == COLOR_SPACE_YCBCR601 ||
2424 color_space == COLOR_SPACE_YCBCR601_LIMITED)
2425 hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
2427 hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
2429 if (color_space == COLOR_SPACE_2020_RGB_FULLRANGE ||
2430 color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE ||
2431 color_space == COLOR_SPACE_2020_YCBCR) {
2432 hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
2433 hdmi_info.bits.C0_C1 = COLORIMETRY_EXTENDED;
2434 } else if (color_space == COLOR_SPACE_ADOBERGB) {
2435 hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
2436 hdmi_info.bits.C0_C1 = COLORIMETRY_EXTENDED;
2439 /* TODO: un-hardcode aspect ratio */
2440 aspect = stream->timing.aspect_ratio;
2443 case ASPECT_RATIO_4_3:
2444 case ASPECT_RATIO_16_9:
2445 hdmi_info.bits.M0_M1 = aspect;
2448 case ASPECT_RATIO_NO_DATA:
2449 case ASPECT_RATIO_64_27:
2450 case ASPECT_RATIO_256_135:
2452 hdmi_info.bits.M0_M1 = 0;
2455 /* Active Format Aspect ratio - same as Picture Aspect Ratio. */
2456 hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
2458 /* TODO: un-hardcode cn0_cn1 and itc */
2466 support = stream->content_support;
2469 if (!support.bits.valid_content_type) {
2472 if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
2473 if (support.bits.graphics_content == 1) {
2476 } else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
2477 if (support.bits.photo_content == 1) {
2483 } else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
2484 if (support.bits.cinema_content == 1) {
2490 } else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
2491 if (support.bits.game_content == 1) {
2499 hdmi_info.bits.CN0_CN1 = cn0_cn1_value;
2500 hdmi_info.bits.ITC = itc_value;
2503 if (stream->qs_bit == 1) {
2504 if (color_space == COLOR_SPACE_SRGB ||
2505 color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
2506 hdmi_info.bits.Q0_Q1 = RGB_QUANTIZATION_FULL_RANGE;
2507 else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
2508 color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
2509 hdmi_info.bits.Q0_Q1 = RGB_QUANTIZATION_LIMITED_RANGE;
2511 hdmi_info.bits.Q0_Q1 = RGB_QUANTIZATION_DEFAULT_RANGE;
2513 hdmi_info.bits.Q0_Q1 = RGB_QUANTIZATION_DEFAULT_RANGE;
2515 /* TODO : We should handle YCC quantization */
2516 /* but we do not have matrix calculation */
2517 if (stream->qy_bit == 1) {
2518 if (color_space == COLOR_SPACE_SRGB ||
2519 color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
2520 hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2521 else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
2522 color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
2523 hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2525 hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2527 hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
2530 format = stream->timing.timing_3d_format;
2531 /*todo, add 3DStereo support*/
2532 if (format != TIMING_3D_FORMAT_NONE) {
2533 // Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
2534 switch (pipe_ctx->stream->timing.hdmi_vic) {
2551 /* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
2552 hdmi_info.bits.VIC0_VIC7 = vic;
2554 hdmi_info.bits.header.version = 3;
2555 /* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
2556 * the Source shall use 20 AVI InfoFrame Version 4
2558 if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
2559 hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
2560 hdmi_info.bits.header.version = 4;
2561 hdmi_info.bits.header.length = 14;
2565 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
2566 * repetition start from 1 */
2567 hdmi_info.bits.PR0_PR3 = 0;
2570 * barTop: Line Number of End of Top Bar.
2571 * barBottom: Line Number of Start of Bottom Bar.
2572 * barLeft: Pixel Number of End of Left Bar.
2573 * barRight: Pixel Number of Start of Right Bar. */
2574 hdmi_info.bits.bar_top = stream->timing.v_border_top;
2575 hdmi_info.bits.bar_bottom = (stream->timing.v_total
2576 - stream->timing.v_border_bottom + 1);
2577 hdmi_info.bits.bar_left = stream->timing.h_border_left;
2578 hdmi_info.bits.bar_right = (stream->timing.h_total
2579 - stream->timing.h_border_right + 1);
2581 /* Additional Colorimetry Extension
2582 * Used in conduction with C0-C1 and EC0-EC2
2583 * 0 = DCI-P3 RGB (D65)
2584 * 1 = DCI-P3 RGB (theater)
2586 hdmi_info.bits.ACE0_ACE3 = 0;
2588 /* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
2589 check_sum = &hdmi_info.packet_raw_data.sb[0];
2591 *check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
2593 for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
2594 *check_sum += hdmi_info.packet_raw_data.sb[byte_index];
2596 /* one byte complement */
2597 *check_sum = (uint8_t) (0x100 - *check_sum);
2599 /* Store in hw_path_mode */
2600 info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
2601 info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
2602 info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
2604 for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
2605 info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
2607 info_packet->valid = true;
2610 static void set_vendor_info_packet(
2611 struct dc_info_packet *info_packet,
2612 struct dc_stream_state *stream)
2614 /* SPD info packet for FreeSync */
2616 /* Check if Freesync is supported. Return if false. If true,
2617 * set the corresponding bit in the info packet
2619 if (!stream->vsp_infopacket.valid)
2622 *info_packet = stream->vsp_infopacket;
2625 static void set_spd_info_packet(
2626 struct dc_info_packet *info_packet,
2627 struct dc_stream_state *stream)
2629 /* SPD info packet for FreeSync */
2631 /* Check if Freesync is supported. Return if false. If true,
2632 * set the corresponding bit in the info packet
2634 if (!stream->vrr_infopacket.valid)
2637 *info_packet = stream->vrr_infopacket;
2640 static void set_hdr_static_info_packet(
2641 struct dc_info_packet *info_packet,
2642 struct dc_stream_state *stream)
2644 /* HDR Static Metadata info packet for HDR10 */
2646 if (!stream->hdr_static_metadata.valid ||
2647 stream->use_dynamic_meta)
2650 *info_packet = stream->hdr_static_metadata;
2653 static void set_vsc_info_packet(
2654 struct dc_info_packet *info_packet,
2655 struct dc_stream_state *stream)
2657 if (!stream->vsc_infopacket.valid)
2660 *info_packet = stream->vsc_infopacket;
2663 void dc_resource_state_destruct(struct dc_state *context)
2667 for (i = 0; i < context->stream_count; i++) {
2668 for (j = 0; j < context->stream_status[i].plane_count; j++)
2669 dc_plane_state_release(
2670 context->stream_status[i].plane_states[j]);
2672 context->stream_status[i].plane_count = 0;
2673 dc_stream_release(context->streams[i]);
2674 context->streams[i] = NULL;
2678 void dc_resource_state_copy_construct(
2679 const struct dc_state *src_ctx,
2680 struct dc_state *dst_ctx)
2683 struct kref refcount = dst_ctx->refcount;
2685 *dst_ctx = *src_ctx;
2687 for (i = 0; i < MAX_PIPES; i++) {
2688 struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
2690 if (cur_pipe->top_pipe)
2691 cur_pipe->top_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
2693 if (cur_pipe->bottom_pipe)
2694 cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
2696 if (cur_pipe->next_odm_pipe)
2697 cur_pipe->next_odm_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
2699 if (cur_pipe->prev_odm_pipe)
2700 cur_pipe->prev_odm_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
2703 for (i = 0; i < dst_ctx->stream_count; i++) {
2704 dc_stream_retain(dst_ctx->streams[i]);
2705 for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
2706 dc_plane_state_retain(
2707 dst_ctx->stream_status[i].plane_states[j]);
2710 /* context refcount should not be overridden */
2711 dst_ctx->refcount = refcount;
2715 struct clock_source *dc_resource_find_first_free_pll(
2716 struct resource_context *res_ctx,
2717 const struct resource_pool *pool)
2721 for (i = 0; i < pool->clk_src_count; ++i) {
2722 if (res_ctx->clock_source_ref_count[i] == 0)
2723 return pool->clock_sources[i];
2729 void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
2731 enum signal_type signal = SIGNAL_TYPE_NONE;
2732 struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
2734 /* default all packets to invalid */
2735 info->avi.valid = false;
2736 info->gamut.valid = false;
2737 info->vendor.valid = false;
2738 info->spd.valid = false;
2739 info->hdrsmd.valid = false;
2740 info->vsc.valid = false;
2742 signal = pipe_ctx->stream->signal;
2744 /* HDMi and DP have different info packets*/
2745 if (dc_is_hdmi_signal(signal)) {
2746 set_avi_info_frame(&info->avi, pipe_ctx);
2748 set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
2750 set_spd_info_packet(&info->spd, pipe_ctx->stream);
2752 set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
2754 } else if (dc_is_dp_signal(signal)) {
2755 set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
2757 set_spd_info_packet(&info->spd, pipe_ctx->stream);
2759 set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
2762 patch_gamut_packet_checksum(&info->gamut);
2765 enum dc_status resource_map_clock_resources(
2766 const struct dc *dc,
2767 struct dc_state *context,
2768 struct dc_stream_state *stream)
2770 /* acquire new resources */
2771 const struct resource_pool *pool = dc->res_pool;
2772 struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(
2773 &context->res_ctx, stream);
2776 return DC_ERROR_UNEXPECTED;
2778 if (dc_is_dp_signal(pipe_ctx->stream->signal)
2779 || pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
2780 pipe_ctx->clock_source = pool->dp_clock_source;
2782 pipe_ctx->clock_source = NULL;
2784 if (!dc->config.disable_disp_pll_sharing)
2785 pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
2789 if (pipe_ctx->clock_source == NULL)
2790 pipe_ctx->clock_source =
2791 dc_resource_find_first_free_pll(
2796 if (pipe_ctx->clock_source == NULL)
2797 return DC_NO_CLOCK_SOURCE_RESOURCE;
2799 resource_reference_clock_source(
2800 &context->res_ctx, pool,
2801 pipe_ctx->clock_source);
2807 * Note: We need to disable output if clock sources change,
2808 * since bios does optimization and doesn't apply if changing
2809 * PHY when not already disabled.
2811 bool pipe_need_reprogram(
2812 struct pipe_ctx *pipe_ctx_old,
2813 struct pipe_ctx *pipe_ctx)
2815 if (!pipe_ctx_old->stream)
2818 if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
2821 if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
2824 if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
2827 if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
2828 && pipe_ctx_old->stream != pipe_ctx->stream)
2831 if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
2834 if (is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
2837 if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
2840 if (false == pipe_ctx_old->stream->link->link_state_valid &&
2841 false == pipe_ctx_old->stream->dpms_off)
2844 if (pipe_ctx_old->stream_res.dsc != pipe_ctx->stream_res.dsc)
2847 /* DIG link encoder resource assignment for stream changed. */
2848 if (pipe_ctx_old->stream->link_enc != pipe_ctx->stream->link_enc)
2854 void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
2855 struct bit_depth_reduction_params *fmt_bit_depth)
2857 enum dc_dither_option option = stream->dither_option;
2858 enum dc_pixel_encoding pixel_encoding =
2859 stream->timing.pixel_encoding;
2861 memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
2863 if (option == DITHER_OPTION_DEFAULT) {
2864 switch (stream->timing.display_color_depth) {
2865 case COLOR_DEPTH_666:
2866 option = DITHER_OPTION_SPATIAL6;
2868 case COLOR_DEPTH_888:
2869 option = DITHER_OPTION_SPATIAL8;
2871 case COLOR_DEPTH_101010:
2872 option = DITHER_OPTION_SPATIAL10;
2875 option = DITHER_OPTION_DISABLE;
2879 if (option == DITHER_OPTION_DISABLE)
2882 if (option == DITHER_OPTION_TRUN6) {
2883 fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2884 fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
2885 } else if (option == DITHER_OPTION_TRUN8 ||
2886 option == DITHER_OPTION_TRUN8_SPATIAL6 ||
2887 option == DITHER_OPTION_TRUN8_FM6) {
2888 fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2889 fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
2890 } else if (option == DITHER_OPTION_TRUN10 ||
2891 option == DITHER_OPTION_TRUN10_SPATIAL6 ||
2892 option == DITHER_OPTION_TRUN10_SPATIAL8 ||
2893 option == DITHER_OPTION_TRUN10_FM8 ||
2894 option == DITHER_OPTION_TRUN10_FM6 ||
2895 option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2896 fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2897 fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2900 /* special case - Formatter can only reduce by 4 bits at most.
2901 * When reducing from 12 to 6 bits,
2902 * HW recommends we use trunc with round mode
2903 * (if we did nothing, trunc to 10 bits would be used)
2904 * note that any 12->10 bit reduction is ignored prior to DCE8,
2905 * as the input was 10 bits.
2907 if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2908 option == DITHER_OPTION_SPATIAL6 ||
2909 option == DITHER_OPTION_FM6) {
2910 fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
2911 fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
2912 fmt_bit_depth->flags.TRUNCATE_MODE = 1;
2916 * note that spatial modes 1-3 are never used
2918 if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
2919 option == DITHER_OPTION_SPATIAL6 ||
2920 option == DITHER_OPTION_TRUN10_SPATIAL6 ||
2921 option == DITHER_OPTION_TRUN8_SPATIAL6) {
2922 fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2923 fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
2924 fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2925 fmt_bit_depth->flags.RGB_RANDOM =
2926 (pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2927 } else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM ||
2928 option == DITHER_OPTION_SPATIAL8 ||
2929 option == DITHER_OPTION_SPATIAL8_FM6 ||
2930 option == DITHER_OPTION_TRUN10_SPATIAL8 ||
2931 option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2932 fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2933 fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
2934 fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2935 fmt_bit_depth->flags.RGB_RANDOM =
2936 (pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2937 } else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
2938 option == DITHER_OPTION_SPATIAL10 ||
2939 option == DITHER_OPTION_SPATIAL10_FM8 ||
2940 option == DITHER_OPTION_SPATIAL10_FM6) {
2941 fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
2942 fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
2943 fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
2944 fmt_bit_depth->flags.RGB_RANDOM =
2945 (pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
2948 if (option == DITHER_OPTION_SPATIAL6 ||
2949 option == DITHER_OPTION_SPATIAL8 ||
2950 option == DITHER_OPTION_SPATIAL10) {
2951 fmt_bit_depth->flags.FRAME_RANDOM = 0;
2953 fmt_bit_depth->flags.FRAME_RANDOM = 1;
2956 //////////////////////
2957 //// temporal dither
2958 //////////////////////
2959 if (option == DITHER_OPTION_FM6 ||
2960 option == DITHER_OPTION_SPATIAL8_FM6 ||
2961 option == DITHER_OPTION_SPATIAL10_FM6 ||
2962 option == DITHER_OPTION_TRUN10_FM6 ||
2963 option == DITHER_OPTION_TRUN8_FM6 ||
2964 option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
2965 fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2966 fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
2967 } else if (option == DITHER_OPTION_FM8 ||
2968 option == DITHER_OPTION_SPATIAL10_FM8 ||
2969 option == DITHER_OPTION_TRUN10_FM8) {
2970 fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2971 fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
2972 } else if (option == DITHER_OPTION_FM10) {
2973 fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
2974 fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
2977 fmt_bit_depth->pixel_encoding = pixel_encoding;
2980 enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
2982 struct dc_link *link = stream->link;
2983 struct timing_generator *tg = dc->res_pool->timing_generators[0];
2984 enum dc_status res = DC_OK;
2986 calculate_phy_pix_clks(stream);
2988 if (!tg->funcs->validate_timing(tg, &stream->timing))
2989 res = DC_FAIL_CONTROLLER_VALIDATE;
2992 if (!link->link_enc->funcs->validate_output_with_stream(
2993 link->link_enc, stream))
2994 res = DC_FAIL_ENC_VALIDATE;
2997 /* TODO: validate audio ASIC caps, encoder */
3000 res = dc_link_validate_mode_timing(stream,
3007 enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
3009 enum dc_status res = DC_OK;
3011 /* TODO For now validates pixel format only */
3012 if (dc->res_pool->funcs->validate_plane)
3013 return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
3018 unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
3021 case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
3023 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
3024 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
3026 case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
3027 case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
3028 case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
3029 case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
3031 case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
3032 case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
3033 case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
3034 case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
3035 case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
3036 #if defined(CONFIG_DRM_AMD_DC_DCN)
3037 case SURFACE_PIXEL_FORMAT_GRPH_RGBE:
3038 case SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA:
3041 case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
3042 case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
3043 case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
3046 ASSERT_CRITICAL(false);
3050 static unsigned int get_max_audio_sample_rate(struct audio_mode *modes)
3053 if (modes->sample_rates.rate.RATE_192)
3055 if (modes->sample_rates.rate.RATE_176_4)
3057 if (modes->sample_rates.rate.RATE_96)
3059 if (modes->sample_rates.rate.RATE_88_2)
3061 if (modes->sample_rates.rate.RATE_48)
3063 if (modes->sample_rates.rate.RATE_44_1)
3065 if (modes->sample_rates.rate.RATE_32)
3068 /*original logic when no audio info*/
3072 void get_audio_check(struct audio_info *aud_modes,
3073 struct audio_check *audio_chk)
3076 unsigned int max_sample_rate = 0;
3079 audio_chk->audio_packet_type = 0x2;/*audio sample packet AP = .25 for layout0, 1 for layout1*/
3081 audio_chk->max_audiosample_rate = 0;
3082 for (i = 0; i < aud_modes->mode_count; i++) {
3083 max_sample_rate = get_max_audio_sample_rate(&aud_modes->modes[i]);
3084 if (audio_chk->max_audiosample_rate < max_sample_rate)
3085 audio_chk->max_audiosample_rate = max_sample_rate;
3086 /*dts takes the same as type 2: AP = 0.25*/
3088 /*check which one take more bandwidth*/
3089 if (audio_chk->max_audiosample_rate > 192000)
3090 audio_chk->audio_packet_type = 0x9;/*AP =1*/
3091 audio_chk->acat = 0;/*not support*/