2 * Copyright 2019 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.
25 #include <drm/drm_dsc.h>
26 #include "dc_hw_types.h"
28 #include <drm/drm_dp_helper.h>
32 /* This module's internal functions */
34 /* default DSC policy target bitrate limit is 16bpp */
35 static uint32_t dsc_policy_max_target_bpp_limit
= 16;
37 /* default DSC policy enables DSC only when needed */
38 static bool dsc_policy_enable_dsc_when_not_needed
;
40 static uint32_t dc_dsc_bandwidth_in_kbps_from_timing(
41 const struct dc_crtc_timing
*timing
)
43 uint32_t bits_per_channel
= 0;
46 if (timing
->flags
.DSC
) {
47 kbps
= (timing
->pix_clk_100hz
* timing
->dsc_cfg
.bits_per_pixel
);
48 kbps
= kbps
/ 160 + ((kbps
% 160) ? 1 : 0);
52 switch (timing
->display_color_depth
) {
59 case COLOR_DEPTH_101010
:
60 bits_per_channel
= 10;
62 case COLOR_DEPTH_121212
:
63 bits_per_channel
= 12;
65 case COLOR_DEPTH_141414
:
66 bits_per_channel
= 14;
68 case COLOR_DEPTH_161616
:
69 bits_per_channel
= 16;
75 ASSERT(bits_per_channel
!= 0);
77 kbps
= timing
->pix_clk_100hz
/ 10;
78 kbps
*= bits_per_channel
;
80 if (timing
->flags
.Y_ONLY
!= 1) {
81 /*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/
83 if (timing
->pixel_encoding
== PIXEL_ENCODING_YCBCR420
)
85 else if (timing
->pixel_encoding
== PIXEL_ENCODING_YCBCR422
)
93 static bool dsc_buff_block_size_from_dpcd(int dpcd_buff_block_size
, int *buff_block_size
)
96 switch (dpcd_buff_block_size
) {
97 case DP_DSC_RC_BUF_BLK_SIZE_1
:
98 *buff_block_size
= 1024;
100 case DP_DSC_RC_BUF_BLK_SIZE_4
:
101 *buff_block_size
= 4 * 1024;
103 case DP_DSC_RC_BUF_BLK_SIZE_16
:
104 *buff_block_size
= 16 * 1024;
106 case DP_DSC_RC_BUF_BLK_SIZE_64
:
107 *buff_block_size
= 64 * 1024;
110 dm_error("%s: DPCD DSC buffer size not recognized.\n", __func__
);
119 static bool dsc_line_buff_depth_from_dpcd(int dpcd_line_buff_bit_depth
, int *line_buff_bit_depth
)
121 if (0 <= dpcd_line_buff_bit_depth
&& dpcd_line_buff_bit_depth
<= 7)
122 *line_buff_bit_depth
= dpcd_line_buff_bit_depth
+ 9;
123 else if (dpcd_line_buff_bit_depth
== 8)
124 *line_buff_bit_depth
= 8;
126 dm_error("%s: DPCD DSC buffer depth not recognized.\n", __func__
);
134 static bool dsc_throughput_from_dpcd(int dpcd_throughput
, int *throughput
)
136 switch (dpcd_throughput
) {
137 case DP_DSC_THROUGHPUT_MODE_0_UNSUPPORTED
:
140 case DP_DSC_THROUGHPUT_MODE_0_170
:
143 case DP_DSC_THROUGHPUT_MODE_0_340
:
146 case DP_DSC_THROUGHPUT_MODE_0_400
:
149 case DP_DSC_THROUGHPUT_MODE_0_450
:
152 case DP_DSC_THROUGHPUT_MODE_0_500
:
155 case DP_DSC_THROUGHPUT_MODE_0_550
:
158 case DP_DSC_THROUGHPUT_MODE_0_600
:
161 case DP_DSC_THROUGHPUT_MODE_0_650
:
164 case DP_DSC_THROUGHPUT_MODE_0_700
:
167 case DP_DSC_THROUGHPUT_MODE_0_750
:
170 case DP_DSC_THROUGHPUT_MODE_0_800
:
173 case DP_DSC_THROUGHPUT_MODE_0_850
:
176 case DP_DSC_THROUGHPUT_MODE_0_900
:
179 case DP_DSC_THROUGHPUT_MODE_0_950
:
182 case DP_DSC_THROUGHPUT_MODE_0_1000
:
186 dm_error("%s: DPCD DSC throughput mode not recognized.\n", __func__
);
195 static bool dsc_bpp_increment_div_from_dpcd(uint8_t bpp_increment_dpcd
, uint32_t *bpp_increment_div
)
197 // Mask bpp increment dpcd field to avoid reading other fields
198 bpp_increment_dpcd
&= 0x7;
200 switch (bpp_increment_dpcd
) {
202 *bpp_increment_div
= 16;
205 *bpp_increment_div
= 8;
208 *bpp_increment_div
= 4;
211 *bpp_increment_div
= 2;
214 *bpp_increment_div
= 1;
217 dm_error("%s: DPCD DSC bits-per-pixel increment not recognized.\n", __func__
);
225 static void get_dsc_enc_caps(
226 const struct display_stream_compressor
*dsc
,
227 struct dsc_enc_caps
*dsc_enc_caps
,
228 int pixel_clock_100Hz
)
230 // This is a static HW query, so we can use any DSC
232 memset(dsc_enc_caps
, 0, sizeof(struct dsc_enc_caps
));
234 if (!dsc
->ctx
->dc
->debug
.disable_dsc
)
235 dsc
->funcs
->dsc_get_enc_caps(dsc_enc_caps
, pixel_clock_100Hz
);
236 if (dsc
->ctx
->dc
->debug
.native422_support
)
237 dsc_enc_caps
->color_formats
.bits
.YCBCR_NATIVE_422
= 1;
241 /* Returns 'false' if no intersection was found for at least one capablity.
242 * It also implicitly validates some sink caps against invalid value of zero.
244 static bool intersect_dsc_caps(
245 const struct dsc_dec_dpcd_caps
*dsc_sink_caps
,
246 const struct dsc_enc_caps
*dsc_enc_caps
,
247 enum dc_pixel_encoding pixel_encoding
,
248 struct dsc_enc_caps
*dsc_common_caps
)
251 int32_t total_sink_throughput
;
253 memset(dsc_common_caps
, 0, sizeof(struct dsc_enc_caps
));
255 dsc_common_caps
->dsc_version
= min(dsc_sink_caps
->dsc_version
, dsc_enc_caps
->dsc_version
);
256 if (!dsc_common_caps
->dsc_version
)
259 dsc_common_caps
->slice_caps
.bits
.NUM_SLICES_1
= dsc_sink_caps
->slice_caps1
.bits
.NUM_SLICES_1
&& dsc_enc_caps
->slice_caps
.bits
.NUM_SLICES_1
;
260 dsc_common_caps
->slice_caps
.bits
.NUM_SLICES_2
= dsc_sink_caps
->slice_caps1
.bits
.NUM_SLICES_2
&& dsc_enc_caps
->slice_caps
.bits
.NUM_SLICES_2
;
261 dsc_common_caps
->slice_caps
.bits
.NUM_SLICES_4
= dsc_sink_caps
->slice_caps1
.bits
.NUM_SLICES_4
&& dsc_enc_caps
->slice_caps
.bits
.NUM_SLICES_4
;
262 dsc_common_caps
->slice_caps
.bits
.NUM_SLICES_8
= dsc_sink_caps
->slice_caps1
.bits
.NUM_SLICES_8
&& dsc_enc_caps
->slice_caps
.bits
.NUM_SLICES_8
;
263 if (!dsc_common_caps
->slice_caps
.raw
)
266 dsc_common_caps
->lb_bit_depth
= min(dsc_sink_caps
->lb_bit_depth
, dsc_enc_caps
->lb_bit_depth
);
267 if (!dsc_common_caps
->lb_bit_depth
)
270 dsc_common_caps
->is_block_pred_supported
= dsc_sink_caps
->is_block_pred_supported
&& dsc_enc_caps
->is_block_pred_supported
;
272 dsc_common_caps
->color_formats
.raw
= dsc_sink_caps
->color_formats
.raw
& dsc_enc_caps
->color_formats
.raw
;
273 if (!dsc_common_caps
->color_formats
.raw
)
276 dsc_common_caps
->color_depth
.raw
= dsc_sink_caps
->color_depth
.raw
& dsc_enc_caps
->color_depth
.raw
;
277 if (!dsc_common_caps
->color_depth
.raw
)
281 if (dsc_common_caps
->slice_caps
.bits
.NUM_SLICES_1
)
284 if (dsc_common_caps
->slice_caps
.bits
.NUM_SLICES_2
)
287 if (dsc_common_caps
->slice_caps
.bits
.NUM_SLICES_4
)
290 total_sink_throughput
= max_slices
* dsc_sink_caps
->throughput_mode_0_mps
;
291 if (pixel_encoding
== PIXEL_ENCODING_YCBCR422
|| pixel_encoding
== PIXEL_ENCODING_YCBCR420
)
292 total_sink_throughput
= max_slices
* dsc_sink_caps
->throughput_mode_1_mps
;
294 dsc_common_caps
->max_total_throughput_mps
= min(total_sink_throughput
, dsc_enc_caps
->max_total_throughput_mps
);
296 dsc_common_caps
->max_slice_width
= min(dsc_sink_caps
->max_slice_width
, dsc_enc_caps
->max_slice_width
);
297 if (!dsc_common_caps
->max_slice_width
)
300 dsc_common_caps
->bpp_increment_div
= min(dsc_sink_caps
->bpp_increment_div
, dsc_enc_caps
->bpp_increment_div
);
302 // TODO DSC: Remove this workaround for N422 and 420 once it's fixed, or move it to get_dsc_encoder_caps()
303 if (pixel_encoding
== PIXEL_ENCODING_YCBCR422
|| pixel_encoding
== PIXEL_ENCODING_YCBCR420
)
304 dsc_common_caps
->bpp_increment_div
= min(dsc_common_caps
->bpp_increment_div
, (uint32_t)8);
309 static inline uint32_t dsc_div_by_10_round_up(uint32_t value
)
311 return (value
+ 9) / 10;
314 /* Get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range, and timing's pixel clock
315 * and uncompressed bandwidth.
317 static void get_dsc_bandwidth_range(
318 const uint32_t min_bpp
,
319 const uint32_t max_bpp
,
320 const struct dsc_enc_caps
*dsc_caps
,
321 const struct dc_crtc_timing
*timing
,
322 struct dc_dsc_bw_range
*range
)
324 /* native stream bandwidth */
325 range
->stream_kbps
= dc_dsc_bandwidth_in_kbps_from_timing(timing
);
327 /* max dsc target bpp */
328 range
->max_kbps
= dsc_div_by_10_round_up(max_bpp
* timing
->pix_clk_100hz
);
329 range
->max_target_bpp_x16
= max_bpp
* 16;
330 if (range
->max_kbps
> range
->stream_kbps
) {
331 /* max dsc target bpp is capped to native bandwidth */
332 range
->max_kbps
= range
->stream_kbps
;
333 range
->max_target_bpp_x16
= calc_dsc_bpp_x16(range
->stream_kbps
, timing
->pix_clk_100hz
, dsc_caps
->bpp_increment_div
);
336 /* min dsc target bpp */
337 range
->min_kbps
= dsc_div_by_10_round_up(min_bpp
* timing
->pix_clk_100hz
);
338 range
->min_target_bpp_x16
= min_bpp
* 16;
339 if (range
->min_kbps
> range
->max_kbps
) {
340 /* min dsc target bpp is capped to max dsc bandwidth*/
341 range
->min_kbps
= range
->max_kbps
;
342 range
->min_target_bpp_x16
= range
->max_target_bpp_x16
;
347 /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy.
350 * - 'true' if DSC was required by policy and was successfully applied
351 * - 'false' if DSC was not necessary (e.g. if uncompressed stream fits 'target_bandwidth_kbps'),
352 * or if it couldn't be applied based on DSC policy.
354 static bool decide_dsc_target_bpp_x16(
355 const struct dc_dsc_policy
*policy
,
356 const struct dsc_enc_caps
*dsc_common_caps
,
357 const int target_bandwidth_kbps
,
358 const struct dc_crtc_timing
*timing
,
361 bool should_use_dsc
= false;
362 struct dc_dsc_bw_range range
;
364 memset(&range
, 0, sizeof(range
));
366 get_dsc_bandwidth_range(policy
->min_target_bpp
, policy
->max_target_bpp
,
367 dsc_common_caps
, timing
, &range
);
368 if (!policy
->enable_dsc_when_not_needed
&& target_bandwidth_kbps
>= range
.stream_kbps
) {
369 /* enough bandwidth without dsc */
371 should_use_dsc
= false;
372 } else if (target_bandwidth_kbps
>= range
.max_kbps
) {
373 /* use max target bpp allowed */
374 *target_bpp_x16
= range
.max_target_bpp_x16
;
375 should_use_dsc
= true;
376 } else if (target_bandwidth_kbps
>= range
.min_kbps
) {
377 /* use target bpp that can take entire target bandwidth */
378 *target_bpp_x16
= calc_dsc_bpp_x16(target_bandwidth_kbps
, timing
->pix_clk_100hz
, dsc_common_caps
->bpp_increment_div
);
379 should_use_dsc
= true;
381 /* not enough bandwidth to fulfill minimum requirement */
383 should_use_dsc
= false;
386 return should_use_dsc
;
389 #define MIN_AVAILABLE_SLICES_SIZE 4
391 static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps
, int *available_slices
)
395 memset(available_slices
, -1, MIN_AVAILABLE_SLICES_SIZE
);
397 if (slice_caps
.bits
.NUM_SLICES_1
)
398 available_slices
[idx
++] = 1;
400 if (slice_caps
.bits
.NUM_SLICES_2
)
401 available_slices
[idx
++] = 2;
403 if (slice_caps
.bits
.NUM_SLICES_4
)
404 available_slices
[idx
++] = 4;
406 if (slice_caps
.bits
.NUM_SLICES_8
)
407 available_slices
[idx
++] = 8;
413 static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps
)
416 int available_slices
[MIN_AVAILABLE_SLICES_SIZE
];
417 int end_idx
= get_available_dsc_slices(slice_caps
, &available_slices
[0]);
420 max_slices
= available_slices
[end_idx
- 1];
426 // Increment sice number in available sice numbers stops if possible, or just increment if not
427 static int inc_num_slices(union dsc_enc_slice_caps slice_caps
, int num_slices
)
429 // Get next bigger num slices available in common caps
430 int available_slices
[MIN_AVAILABLE_SLICES_SIZE
];
433 int new_num_slices
= num_slices
;
435 end_idx
= get_available_dsc_slices(slice_caps
, &available_slices
[0]);
437 // No available slices found
439 return new_num_slices
;
442 // Numbers of slices found - get the next bigger number
443 for (i
= 0; i
< end_idx
; i
++) {
444 if (new_num_slices
< available_slices
[i
]) {
445 new_num_slices
= available_slices
[i
];
450 if (new_num_slices
== num_slices
) // No biger number of slices found
453 return new_num_slices
;
457 // Decrement sice number in available sice numbers stops if possible, or just decrement if not. Stop at zero.
458 static int dec_num_slices(union dsc_enc_slice_caps slice_caps
, int num_slices
)
460 // Get next bigger num slices available in common caps
461 int available_slices
[MIN_AVAILABLE_SLICES_SIZE
];
464 int new_num_slices
= num_slices
;
466 end_idx
= get_available_dsc_slices(slice_caps
, &available_slices
[0]);
467 if (end_idx
== 0 && new_num_slices
> 0) {
468 // No numbers of slices found
470 return new_num_slices
;
473 // Numbers of slices found - get the next smaller number
474 for (i
= end_idx
- 1; i
>= 0; i
--) {
475 if (new_num_slices
> available_slices
[i
]) {
476 new_num_slices
= available_slices
[i
];
481 if (new_num_slices
== num_slices
) {
482 // No smaller number of slices found
484 if (new_num_slices
< 0)
488 return new_num_slices
;
492 // Choose next bigger number of slices if the requested number of slices is not available
493 static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps
, int num_slices
)
495 // Get next bigger num slices available in common caps
496 int available_slices
[MIN_AVAILABLE_SLICES_SIZE
];
499 int new_num_slices
= num_slices
;
501 end_idx
= get_available_dsc_slices(slice_caps
, &available_slices
[0]);
503 // No available slices found
505 return new_num_slices
;
508 // Numbers of slices found - get the equal or next bigger number
509 for (i
= 0; i
< end_idx
; i
++) {
510 if (new_num_slices
<= available_slices
[i
]) {
511 new_num_slices
= available_slices
[i
];
516 return new_num_slices
;
520 /* Attempts to set DSC configuration for the stream, applying DSC policy.
521 * Returns 'true' if successful or 'false' if not.
525 * dsc_sink_caps - DSC sink decoder capabilities (from DPCD)
527 * dsc_enc_caps - DSC encoder capabilities
529 * target_bandwidth_kbps - Target bandwidth to fit the stream into.
530 * If 0, do not calculate target bpp.
532 * timing - The stream timing to fit into 'target_bandwidth_kbps' or apply
533 * maximum compression to, if 'target_badwidth == 0'
535 * dsc_cfg - DSC configuration to use if it was possible to come up with
536 * one for the given inputs.
537 * The target bitrate after DSC can be calculated by multiplying
538 * dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g.
540 * dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0);
542 static bool setup_dsc_config(
543 const struct dsc_dec_dpcd_caps
*dsc_sink_caps
,
544 const struct dsc_enc_caps
*dsc_enc_caps
,
545 int target_bandwidth_kbps
,
546 const struct dc_crtc_timing
*timing
,
547 int min_slice_height_override
,
548 int max_dsc_target_bpp_limit_override
,
549 struct dc_dsc_config
*dsc_cfg
)
551 struct dsc_enc_caps dsc_common_caps
;
558 int sink_per_slice_throughput_mps
;
559 int branch_max_throughput_mps
= 0;
560 bool is_dsc_possible
= false;
563 struct dc_dsc_policy policy
;
565 memset(dsc_cfg
, 0, sizeof(struct dc_dsc_config
));
567 dc_dsc_get_policy_for_timing(timing
, max_dsc_target_bpp_limit_override
, &policy
);
568 pic_width
= timing
->h_addressable
+ timing
->h_border_left
+ timing
->h_border_right
;
569 pic_height
= timing
->v_addressable
+ timing
->v_border_top
+ timing
->v_border_bottom
;
571 if (!dsc_sink_caps
->is_dsc_supported
)
574 if (dsc_sink_caps
->branch_max_line_width
&& dsc_sink_caps
->branch_max_line_width
< pic_width
)
577 // Intersect decoder with encoder DSC caps and validate DSC settings
578 is_dsc_possible
= intersect_dsc_caps(dsc_sink_caps
, dsc_enc_caps
, timing
->pixel_encoding
, &dsc_common_caps
);
579 if (!is_dsc_possible
)
582 if (target_bandwidth_kbps
> 0) {
583 is_dsc_possible
= decide_dsc_target_bpp_x16(
586 target_bandwidth_kbps
,
589 dsc_cfg
->bits_per_pixel
= target_bpp
;
591 if (!is_dsc_possible
)
594 sink_per_slice_throughput_mps
= 0;
596 // Validate available DSC settings against the mode timing
598 // Validate color format (and pick up the throughput values)
599 dsc_cfg
->ycbcr422_simple
= false;
600 switch (timing
->pixel_encoding
) {
601 case PIXEL_ENCODING_RGB
:
602 is_dsc_possible
= (bool)dsc_common_caps
.color_formats
.bits
.RGB
;
603 sink_per_slice_throughput_mps
= dsc_sink_caps
->throughput_mode_0_mps
;
604 branch_max_throughput_mps
= dsc_sink_caps
->branch_overall_throughput_0_mps
;
606 case PIXEL_ENCODING_YCBCR444
:
607 is_dsc_possible
= (bool)dsc_common_caps
.color_formats
.bits
.YCBCR_444
;
608 sink_per_slice_throughput_mps
= dsc_sink_caps
->throughput_mode_0_mps
;
609 branch_max_throughput_mps
= dsc_sink_caps
->branch_overall_throughput_0_mps
;
611 case PIXEL_ENCODING_YCBCR422
:
612 is_dsc_possible
= (bool)dsc_common_caps
.color_formats
.bits
.YCBCR_NATIVE_422
;
613 sink_per_slice_throughput_mps
= dsc_sink_caps
->throughput_mode_1_mps
;
614 branch_max_throughput_mps
= dsc_sink_caps
->branch_overall_throughput_1_mps
;
615 if (!is_dsc_possible
) {
616 is_dsc_possible
= (bool)dsc_common_caps
.color_formats
.bits
.YCBCR_SIMPLE_422
;
617 dsc_cfg
->ycbcr422_simple
= is_dsc_possible
;
618 sink_per_slice_throughput_mps
= dsc_sink_caps
->throughput_mode_0_mps
;
621 case PIXEL_ENCODING_YCBCR420
:
622 is_dsc_possible
= (bool)dsc_common_caps
.color_formats
.bits
.YCBCR_NATIVE_420
;
623 sink_per_slice_throughput_mps
= dsc_sink_caps
->throughput_mode_1_mps
;
624 branch_max_throughput_mps
= dsc_sink_caps
->branch_overall_throughput_1_mps
;
627 is_dsc_possible
= false;
630 // Validate branch's maximum throughput
631 if (branch_max_throughput_mps
&& dsc_div_by_10_round_up(timing
->pix_clk_100hz
) > branch_max_throughput_mps
* 1000)
632 is_dsc_possible
= false;
634 if (!is_dsc_possible
)
638 switch (timing
->display_color_depth
) {
639 case COLOR_DEPTH_888
:
640 is_dsc_possible
= (bool)dsc_common_caps
.color_depth
.bits
.COLOR_DEPTH_8_BPC
;
642 case COLOR_DEPTH_101010
:
643 is_dsc_possible
= (bool)dsc_common_caps
.color_depth
.bits
.COLOR_DEPTH_10_BPC
;
645 case COLOR_DEPTH_121212
:
646 is_dsc_possible
= (bool)dsc_common_caps
.color_depth
.bits
.COLOR_DEPTH_12_BPC
;
649 is_dsc_possible
= false;
652 if (!is_dsc_possible
)
655 // Slice width (i.e. number of slices per line)
656 max_slices_h
= get_max_dsc_slices(dsc_common_caps
.slice_caps
);
658 while (max_slices_h
> 0) {
659 if (pic_width
% max_slices_h
== 0)
662 max_slices_h
= dec_num_slices(dsc_common_caps
.slice_caps
, max_slices_h
);
665 is_dsc_possible
= (dsc_common_caps
.max_slice_width
> 0);
666 if (!is_dsc_possible
)
669 min_slices_h
= pic_width
/ dsc_common_caps
.max_slice_width
;
670 if (pic_width
% dsc_common_caps
.max_slice_width
)
673 min_slices_h
= fit_num_slices_up(dsc_common_caps
.slice_caps
, min_slices_h
);
675 while (min_slices_h
<= max_slices_h
) {
676 int pix_clk_per_slice_khz
= dsc_div_by_10_round_up(timing
->pix_clk_100hz
) / min_slices_h
;
677 if (pix_clk_per_slice_khz
<= sink_per_slice_throughput_mps
* 1000)
680 min_slices_h
= inc_num_slices(dsc_common_caps
.slice_caps
, min_slices_h
);
683 if (pic_width
% min_slices_h
!= 0)
684 min_slices_h
= 0; // DSC TODO: Maybe try increasing the number of slices first?
686 is_dsc_possible
= (min_slices_h
<= max_slices_h
);
687 if (!is_dsc_possible
)
690 if (policy
.use_min_slices_h
) {
691 if (min_slices_h
> 0)
692 num_slices_h
= min_slices_h
;
693 else if (max_slices_h
> 0) { // Fall back to max slices if min slices is not working out
694 if (policy
.max_slices_h
)
695 num_slices_h
= min(policy
.max_slices_h
, max_slices_h
);
697 num_slices_h
= max_slices_h
;
699 is_dsc_possible
= false;
701 if (max_slices_h
> 0) {
702 if (policy
.max_slices_h
)
703 num_slices_h
= min(policy
.max_slices_h
, max_slices_h
);
705 num_slices_h
= max_slices_h
;
706 } else if (min_slices_h
> 0) // Fall back to min slices if max slices is not possible
707 num_slices_h
= min_slices_h
;
709 is_dsc_possible
= false;
712 if (!is_dsc_possible
)
715 dsc_cfg
->num_slices_h
= num_slices_h
;
716 slice_width
= pic_width
/ num_slices_h
;
718 is_dsc_possible
= slice_width
<= dsc_common_caps
.max_slice_width
;
719 if (!is_dsc_possible
)
722 // Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by.
723 // For 4:2:0 make sure the slice height is divisible by 2 as well.
724 if (min_slice_height_override
== 0)
725 slice_height
= min(policy
.min_slice_height
, pic_height
);
727 slice_height
= min(min_slice_height_override
, pic_height
);
729 while (slice_height
< pic_height
&& (pic_height
% slice_height
!= 0 ||
730 (timing
->pixel_encoding
== PIXEL_ENCODING_YCBCR420
&& slice_height
% 2 != 0)))
733 if (timing
->pixel_encoding
== PIXEL_ENCODING_YCBCR420
) // For the case when pic_height < dsc_policy.min_sice_height
734 is_dsc_possible
= (slice_height
% 2 == 0);
736 if (!is_dsc_possible
)
739 dsc_cfg
->num_slices_v
= pic_height
/slice_height
;
741 // Final decission: can we do DSC or not?
742 if (is_dsc_possible
) {
743 // Fill out the rest of DSC settings
744 dsc_cfg
->block_pred_enable
= dsc_common_caps
.is_block_pred_supported
;
745 dsc_cfg
->linebuf_depth
= dsc_common_caps
.lb_bit_depth
;
746 dsc_cfg
->version_minor
= (dsc_common_caps
.dsc_version
& 0xf0) >> 4;
750 if (!is_dsc_possible
)
751 memset(dsc_cfg
, 0, sizeof(struct dc_dsc_config
));
753 return is_dsc_possible
;
756 bool dc_dsc_parse_dsc_dpcd(const struct dc
*dc
, const uint8_t *dpcd_dsc_basic_data
, const uint8_t *dpcd_dsc_branch_decoder_caps
, struct dsc_dec_dpcd_caps
*dsc_sink_caps
)
758 if (!dpcd_dsc_basic_data
)
761 dsc_sink_caps
->is_dsc_supported
= (dpcd_dsc_basic_data
[DP_DSC_SUPPORT
- DP_DSC_SUPPORT
] & DP_DSC_DECOMPRESSION_IS_SUPPORTED
) != 0;
762 if (!dsc_sink_caps
->is_dsc_supported
)
765 dsc_sink_caps
->dsc_version
= dpcd_dsc_basic_data
[DP_DSC_REV
- DP_DSC_SUPPORT
];
771 if (!dsc_buff_block_size_from_dpcd(dpcd_dsc_basic_data
[DP_DSC_RC_BUF_BLK_SIZE
- DP_DSC_SUPPORT
], &buff_block_size
))
774 buff_size
= dpcd_dsc_basic_data
[DP_DSC_RC_BUF_SIZE
- DP_DSC_SUPPORT
] + 1;
775 dsc_sink_caps
->rc_buffer_size
= buff_size
* buff_block_size
;
778 dsc_sink_caps
->slice_caps1
.raw
= dpcd_dsc_basic_data
[DP_DSC_SLICE_CAP_1
- DP_DSC_SUPPORT
];
779 if (!dsc_line_buff_depth_from_dpcd(dpcd_dsc_basic_data
[DP_DSC_LINE_BUF_BIT_DEPTH
- DP_DSC_SUPPORT
], &dsc_sink_caps
->lb_bit_depth
))
782 dsc_sink_caps
->is_block_pred_supported
=
783 (dpcd_dsc_basic_data
[DP_DSC_BLK_PREDICTION_SUPPORT
- DP_DSC_SUPPORT
] & DP_DSC_BLK_PREDICTION_IS_SUPPORTED
) != 0;
785 dsc_sink_caps
->edp_max_bits_per_pixel
=
786 dpcd_dsc_basic_data
[DP_DSC_MAX_BITS_PER_PIXEL_LOW
- DP_DSC_SUPPORT
] |
787 dpcd_dsc_basic_data
[DP_DSC_MAX_BITS_PER_PIXEL_HI
- DP_DSC_SUPPORT
] << 8;
789 dsc_sink_caps
->color_formats
.raw
= dpcd_dsc_basic_data
[DP_DSC_DEC_COLOR_FORMAT_CAP
- DP_DSC_SUPPORT
];
790 dsc_sink_caps
->color_depth
.raw
= dpcd_dsc_basic_data
[DP_DSC_DEC_COLOR_DEPTH_CAP
- DP_DSC_SUPPORT
];
793 int dpcd_throughput
= dpcd_dsc_basic_data
[DP_DSC_PEAK_THROUGHPUT
- DP_DSC_SUPPORT
];
795 if (!dsc_throughput_from_dpcd(dpcd_throughput
& DP_DSC_THROUGHPUT_MODE_0_MASK
, &dsc_sink_caps
->throughput_mode_0_mps
))
798 dpcd_throughput
= (dpcd_throughput
& DP_DSC_THROUGHPUT_MODE_1_MASK
) >> DP_DSC_THROUGHPUT_MODE_1_SHIFT
;
799 if (!dsc_throughput_from_dpcd(dpcd_throughput
, &dsc_sink_caps
->throughput_mode_1_mps
))
803 dsc_sink_caps
->max_slice_width
= dpcd_dsc_basic_data
[DP_DSC_MAX_SLICE_WIDTH
- DP_DSC_SUPPORT
] * 320;
804 dsc_sink_caps
->slice_caps2
.raw
= dpcd_dsc_basic_data
[DP_DSC_SLICE_CAP_2
- DP_DSC_SUPPORT
];
806 if (!dsc_bpp_increment_div_from_dpcd(dpcd_dsc_basic_data
[DP_DSC_BITS_PER_PIXEL_INC
- DP_DSC_SUPPORT
], &dsc_sink_caps
->bpp_increment_div
))
809 if (dc
->debug
.dsc_bpp_increment_div
) {
810 /* dsc_bpp_increment_div should onl be 1, 2, 4, 8 or 16, but rather than rejecting invalid values,
811 * we'll accept all and get it into range. This also makes the above check against 0 redundant,
812 * but that one stresses out the override will be only used if it's not 0.
814 if (dc
->debug
.dsc_bpp_increment_div
>= 1)
815 dsc_sink_caps
->bpp_increment_div
= 1;
816 if (dc
->debug
.dsc_bpp_increment_div
>= 2)
817 dsc_sink_caps
->bpp_increment_div
= 2;
818 if (dc
->debug
.dsc_bpp_increment_div
>= 4)
819 dsc_sink_caps
->bpp_increment_div
= 4;
820 if (dc
->debug
.dsc_bpp_increment_div
>= 8)
821 dsc_sink_caps
->bpp_increment_div
= 8;
822 if (dc
->debug
.dsc_bpp_increment_div
>= 16)
823 dsc_sink_caps
->bpp_increment_div
= 16;
827 if (dpcd_dsc_branch_decoder_caps
== NULL
) { // branch decoder DPCD DSC data can be null for non branch device
828 dsc_sink_caps
->branch_overall_throughput_0_mps
= 0;
829 dsc_sink_caps
->branch_overall_throughput_1_mps
= 0;
830 dsc_sink_caps
->branch_max_line_width
= 0;
834 dsc_sink_caps
->branch_overall_throughput_0_mps
= dpcd_dsc_branch_decoder_caps
[DP_DSC_BRANCH_OVERALL_THROUGHPUT_0
- DP_DSC_BRANCH_OVERALL_THROUGHPUT_0
];
835 if (dsc_sink_caps
->branch_overall_throughput_0_mps
== 0)
836 dsc_sink_caps
->branch_overall_throughput_0_mps
= 0;
837 else if (dsc_sink_caps
->branch_overall_throughput_0_mps
== 1)
838 dsc_sink_caps
->branch_overall_throughput_0_mps
= 680;
840 dsc_sink_caps
->branch_overall_throughput_0_mps
*= 50;
841 dsc_sink_caps
->branch_overall_throughput_0_mps
+= 600;
844 dsc_sink_caps
->branch_overall_throughput_1_mps
= dpcd_dsc_branch_decoder_caps
[DP_DSC_BRANCH_OVERALL_THROUGHPUT_1
- DP_DSC_BRANCH_OVERALL_THROUGHPUT_0
];
845 if (dsc_sink_caps
->branch_overall_throughput_1_mps
== 0)
846 dsc_sink_caps
->branch_overall_throughput_1_mps
= 0;
847 else if (dsc_sink_caps
->branch_overall_throughput_1_mps
== 1)
848 dsc_sink_caps
->branch_overall_throughput_1_mps
= 680;
850 dsc_sink_caps
->branch_overall_throughput_1_mps
*= 50;
851 dsc_sink_caps
->branch_overall_throughput_1_mps
+= 600;
854 dsc_sink_caps
->branch_max_line_width
= dpcd_dsc_branch_decoder_caps
[DP_DSC_BRANCH_MAX_LINE_WIDTH
- DP_DSC_BRANCH_OVERALL_THROUGHPUT_0
] * 320;
855 ASSERT(dsc_sink_caps
->branch_max_line_width
== 0 || dsc_sink_caps
->branch_max_line_width
>= 5120);
861 /* If DSC is possbile, get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range and
862 * timing's pixel clock and uncompressed bandwidth.
863 * If DSC is not possible, leave '*range' untouched.
865 bool dc_dsc_compute_bandwidth_range(
866 const struct display_stream_compressor
*dsc
,
867 uint32_t dsc_min_slice_height_override
,
870 const struct dsc_dec_dpcd_caps
*dsc_sink_caps
,
871 const struct dc_crtc_timing
*timing
,
872 struct dc_dsc_bw_range
*range
)
874 bool is_dsc_possible
= false;
875 struct dsc_enc_caps dsc_enc_caps
;
876 struct dsc_enc_caps dsc_common_caps
;
877 struct dc_dsc_config config
;
879 get_dsc_enc_caps(dsc
, &dsc_enc_caps
, timing
->pix_clk_100hz
);
881 is_dsc_possible
= intersect_dsc_caps(dsc_sink_caps
, &dsc_enc_caps
,
882 timing
->pixel_encoding
, &dsc_common_caps
);
885 is_dsc_possible
= setup_dsc_config(dsc_sink_caps
, &dsc_enc_caps
, 0, timing
,
886 dsc_min_slice_height_override
, max_bpp
, &config
);
889 get_dsc_bandwidth_range(min_bpp
, max_bpp
, &dsc_common_caps
, timing
, range
);
891 return is_dsc_possible
;
894 bool dc_dsc_compute_config(
895 const struct display_stream_compressor
*dsc
,
896 const struct dsc_dec_dpcd_caps
*dsc_sink_caps
,
897 uint32_t dsc_min_slice_height_override
,
898 uint32_t max_target_bpp_limit_override
,
899 uint32_t target_bandwidth_kbps
,
900 const struct dc_crtc_timing
*timing
,
901 struct dc_dsc_config
*dsc_cfg
)
903 bool is_dsc_possible
= false;
904 struct dsc_enc_caps dsc_enc_caps
;
906 get_dsc_enc_caps(dsc
, &dsc_enc_caps
, timing
->pix_clk_100hz
);
907 is_dsc_possible
= setup_dsc_config(dsc_sink_caps
,
909 target_bandwidth_kbps
,
910 timing
, dsc_min_slice_height_override
,
911 max_target_bpp_limit_override
, dsc_cfg
);
912 return is_dsc_possible
;
915 void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing
*timing
, uint32_t max_target_bpp_limit_override
, struct dc_dsc_policy
*policy
)
919 policy
->min_target_bpp
= 0;
920 policy
->max_target_bpp
= 0;
922 /* DSC Policy: Use minimum number of slices that fits the pixel clock */
923 policy
->use_min_slices_h
= true;
925 /* DSC Policy: Use max available slices
926 * (in our case 4 for or 8, depending on the mode)
928 policy
->max_slices_h
= 0;
930 /* DSC Policy: Use slice height recommended
931 * by VESA DSC Spreadsheet user guide
933 policy
->min_slice_height
= 108;
935 /* DSC Policy: follow DP specs with an internal upper limit to 16 bpp
936 * for better interoperability
938 switch (timing
->display_color_depth
) {
939 case COLOR_DEPTH_888
:
942 case COLOR_DEPTH_101010
:
945 case COLOR_DEPTH_121212
:
951 switch (timing
->pixel_encoding
) {
952 case PIXEL_ENCODING_RGB
:
953 case PIXEL_ENCODING_YCBCR444
:
954 case PIXEL_ENCODING_YCBCR422
: /* assume no YCbCr422 native support */
955 /* DP specs limits to 8 */
956 policy
->min_target_bpp
= 8;
957 /* DP specs limits to 3 x bpc */
958 policy
->max_target_bpp
= 3 * bpc
;
960 case PIXEL_ENCODING_YCBCR420
:
961 /* DP specs limits to 6 */
962 policy
->min_target_bpp
= 6;
963 /* DP specs limits to 1.5 x bpc assume bpc is an even number */
964 policy
->max_target_bpp
= bpc
* 3 / 2;
970 /* internal upper limit, default 16 bpp */
971 if (policy
->max_target_bpp
> dsc_policy_max_target_bpp_limit
)
972 policy
->max_target_bpp
= dsc_policy_max_target_bpp_limit
;
975 if (max_target_bpp_limit_override
&& policy
->max_target_bpp
> max_target_bpp_limit_override
)
976 policy
->max_target_bpp
= max_target_bpp_limit_override
;
978 /* enable DSC when not needed, default false */
979 if (dsc_policy_enable_dsc_when_not_needed
)
980 policy
->enable_dsc_when_not_needed
= dsc_policy_enable_dsc_when_not_needed
;
982 policy
->enable_dsc_when_not_needed
= false;
985 void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit
)
987 dsc_policy_max_target_bpp_limit
= limit
;
990 void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable
)
992 dsc_policy_enable_dsc_when_not_needed
= enable
;