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 bool dsc_policy_disable_dsc_stream_overhead;
42 static bool dsc_buff_block_size_from_dpcd(int dpcd_buff_block_size, int *buff_block_size)
45 switch (dpcd_buff_block_size) {
46 case DP_DSC_RC_BUF_BLK_SIZE_1:
47 *buff_block_size = 1024;
49 case DP_DSC_RC_BUF_BLK_SIZE_4:
50 *buff_block_size = 4 * 1024;
52 case DP_DSC_RC_BUF_BLK_SIZE_16:
53 *buff_block_size = 16 * 1024;
55 case DP_DSC_RC_BUF_BLK_SIZE_64:
56 *buff_block_size = 64 * 1024;
59 dm_error("%s: DPCD DSC buffer size not recognized.\n", __func__);
68 static bool dsc_line_buff_depth_from_dpcd(int dpcd_line_buff_bit_depth, int *line_buff_bit_depth)
70 if (0 <= dpcd_line_buff_bit_depth && dpcd_line_buff_bit_depth <= 7)
71 *line_buff_bit_depth = dpcd_line_buff_bit_depth + 9;
72 else if (dpcd_line_buff_bit_depth == 8)
73 *line_buff_bit_depth = 8;
75 dm_error("%s: DPCD DSC buffer depth not recognized.\n", __func__);
83 static bool dsc_throughput_from_dpcd(int dpcd_throughput, int *throughput)
85 switch (dpcd_throughput) {
86 case DP_DSC_THROUGHPUT_MODE_0_UNSUPPORTED:
89 case DP_DSC_THROUGHPUT_MODE_0_170:
92 case DP_DSC_THROUGHPUT_MODE_0_340:
95 case DP_DSC_THROUGHPUT_MODE_0_400:
98 case DP_DSC_THROUGHPUT_MODE_0_450:
101 case DP_DSC_THROUGHPUT_MODE_0_500:
104 case DP_DSC_THROUGHPUT_MODE_0_550:
107 case DP_DSC_THROUGHPUT_MODE_0_600:
110 case DP_DSC_THROUGHPUT_MODE_0_650:
113 case DP_DSC_THROUGHPUT_MODE_0_700:
116 case DP_DSC_THROUGHPUT_MODE_0_750:
119 case DP_DSC_THROUGHPUT_MODE_0_800:
122 case DP_DSC_THROUGHPUT_MODE_0_850:
125 case DP_DSC_THROUGHPUT_MODE_0_900:
128 case DP_DSC_THROUGHPUT_MODE_0_950:
131 case DP_DSC_THROUGHPUT_MODE_0_1000:
135 dm_error("%s: DPCD DSC throughput mode not recognized.\n", __func__);
144 static bool dsc_bpp_increment_div_from_dpcd(uint8_t bpp_increment_dpcd, uint32_t *bpp_increment_div)
146 // Mask bpp increment dpcd field to avoid reading other fields
147 bpp_increment_dpcd &= 0x7;
149 switch (bpp_increment_dpcd) {
151 *bpp_increment_div = 16;
154 *bpp_increment_div = 8;
157 *bpp_increment_div = 4;
160 *bpp_increment_div = 2;
163 *bpp_increment_div = 1;
166 dm_error("%s: DPCD DSC bits-per-pixel increment not recognized.\n", __func__);
174 static void get_dsc_enc_caps(
175 const struct display_stream_compressor *dsc,
176 struct dsc_enc_caps *dsc_enc_caps,
177 int pixel_clock_100Hz)
179 // This is a static HW query, so we can use any DSC
181 memset(dsc_enc_caps, 0, sizeof(struct dsc_enc_caps));
183 if (!dsc->ctx->dc->debug.disable_dsc)
184 dsc->funcs->dsc_get_enc_caps(dsc_enc_caps, pixel_clock_100Hz);
185 if (dsc->ctx->dc->debug.native422_support)
186 dsc_enc_caps->color_formats.bits.YCBCR_NATIVE_422 = 1;
190 /* Returns 'false' if no intersection was found for at least one capablity.
191 * It also implicitly validates some sink caps against invalid value of zero.
193 static bool intersect_dsc_caps(
194 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
195 const struct dsc_enc_caps *dsc_enc_caps,
196 enum dc_pixel_encoding pixel_encoding,
197 struct dsc_enc_caps *dsc_common_caps)
200 int32_t total_sink_throughput;
202 memset(dsc_common_caps, 0, sizeof(struct dsc_enc_caps));
204 dsc_common_caps->dsc_version = min(dsc_sink_caps->dsc_version, dsc_enc_caps->dsc_version);
205 if (!dsc_common_caps->dsc_version)
208 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;
209 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;
210 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;
211 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;
212 if (!dsc_common_caps->slice_caps.raw)
215 dsc_common_caps->lb_bit_depth = min(dsc_sink_caps->lb_bit_depth, dsc_enc_caps->lb_bit_depth);
216 if (!dsc_common_caps->lb_bit_depth)
219 dsc_common_caps->is_block_pred_supported = dsc_sink_caps->is_block_pred_supported && dsc_enc_caps->is_block_pred_supported;
221 dsc_common_caps->color_formats.raw = dsc_sink_caps->color_formats.raw & dsc_enc_caps->color_formats.raw;
222 if (!dsc_common_caps->color_formats.raw)
225 dsc_common_caps->color_depth.raw = dsc_sink_caps->color_depth.raw & dsc_enc_caps->color_depth.raw;
226 if (!dsc_common_caps->color_depth.raw)
230 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_1)
233 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_2)
236 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_4)
239 total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_0_mps;
240 if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
241 total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_1_mps;
243 dsc_common_caps->max_total_throughput_mps = min(total_sink_throughput, dsc_enc_caps->max_total_throughput_mps);
245 dsc_common_caps->max_slice_width = min(dsc_sink_caps->max_slice_width, dsc_enc_caps->max_slice_width);
246 if (!dsc_common_caps->max_slice_width)
249 dsc_common_caps->bpp_increment_div = min(dsc_sink_caps->bpp_increment_div, dsc_enc_caps->bpp_increment_div);
251 // TODO DSC: Remove this workaround for N422 and 420 once it's fixed, or move it to get_dsc_encoder_caps()
252 if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
253 dsc_common_caps->bpp_increment_div = min(dsc_common_caps->bpp_increment_div, (uint32_t)8);
255 dsc_common_caps->is_dp = dsc_sink_caps->is_dp;
259 static inline uint32_t dsc_div_by_10_round_up(uint32_t value)
261 return (value + 9) / 10;
264 static struct fixed31_32 compute_dsc_max_bandwidth_overhead(
265 const struct dc_crtc_timing *timing,
266 const int num_slices_h,
269 struct fixed31_32 max_dsc_overhead;
270 struct fixed31_32 refresh_rate;
272 if (dsc_policy_disable_dsc_stream_overhead || !is_dp)
273 return dc_fixpt_from_int(0);
275 /* use target bpp that can take entire target bandwidth */
276 refresh_rate = dc_fixpt_from_int(timing->pix_clk_100hz);
277 refresh_rate = dc_fixpt_div_int(refresh_rate, timing->h_total);
278 refresh_rate = dc_fixpt_div_int(refresh_rate, timing->v_total);
279 refresh_rate = dc_fixpt_mul_int(refresh_rate, 100);
281 max_dsc_overhead = dc_fixpt_from_int(num_slices_h);
282 max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, timing->v_total);
283 max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, 256);
284 max_dsc_overhead = dc_fixpt_div_int(max_dsc_overhead, 1000);
285 max_dsc_overhead = dc_fixpt_mul(max_dsc_overhead, refresh_rate);
287 return max_dsc_overhead;
290 static uint32_t compute_bpp_x16_from_target_bandwidth(
291 const uint32_t bandwidth_in_kbps,
292 const struct dc_crtc_timing *timing,
293 const uint32_t num_slices_h,
294 const uint32_t bpp_increment_div,
297 struct fixed31_32 overhead_in_kbps;
298 struct fixed31_32 effective_bandwidth_in_kbps;
299 struct fixed31_32 bpp_x16;
301 overhead_in_kbps = compute_dsc_max_bandwidth_overhead(
302 timing, num_slices_h, is_dp);
303 effective_bandwidth_in_kbps = dc_fixpt_from_int(bandwidth_in_kbps);
304 effective_bandwidth_in_kbps = dc_fixpt_sub(effective_bandwidth_in_kbps,
306 bpp_x16 = dc_fixpt_mul_int(effective_bandwidth_in_kbps, 10);
307 bpp_x16 = dc_fixpt_div_int(bpp_x16, timing->pix_clk_100hz);
308 bpp_x16 = dc_fixpt_from_int(dc_fixpt_floor(dc_fixpt_mul_int(bpp_x16, bpp_increment_div)));
309 bpp_x16 = dc_fixpt_div_int(bpp_x16, bpp_increment_div);
310 bpp_x16 = dc_fixpt_mul_int(bpp_x16, 16);
311 return dc_fixpt_floor(bpp_x16);
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_x16,
319 const uint32_t max_bpp_x16,
320 const uint32_t num_slices_h,
321 const struct dsc_enc_caps *dsc_caps,
322 const struct dc_crtc_timing *timing,
323 struct dc_dsc_bw_range *range)
325 /* native stream bandwidth */
326 range->stream_kbps = dc_bandwidth_in_kbps_from_timing(timing);
328 /* max dsc target bpp */
329 range->max_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
330 max_bpp_x16, num_slices_h, dsc_caps->is_dp);
331 range->max_target_bpp_x16 = max_bpp_x16;
332 if (range->max_kbps > range->stream_kbps) {
333 /* max dsc target bpp is capped to native bandwidth */
334 range->max_kbps = range->stream_kbps;
335 range->max_target_bpp_x16 = compute_bpp_x16_from_target_bandwidth(
336 range->max_kbps, timing, num_slices_h,
337 dsc_caps->bpp_increment_div,
341 /* min dsc target bpp */
342 range->min_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
343 min_bpp_x16, num_slices_h, dsc_caps->is_dp);
344 range->min_target_bpp_x16 = min_bpp_x16;
345 if (range->min_kbps > range->max_kbps) {
346 /* min dsc target bpp is capped to max dsc bandwidth*/
347 range->min_kbps = range->max_kbps;
348 range->min_target_bpp_x16 = range->max_target_bpp_x16;
352 /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy.
355 * - 'true' if DSC was required by policy and was successfully applied
356 * - 'false' if DSC was not necessary (e.g. if uncompressed stream fits 'target_bandwidth_kbps'),
357 * or if it couldn't be applied based on DSC policy.
359 static bool decide_dsc_target_bpp_x16(
360 const struct dc_dsc_policy *policy,
361 const struct dsc_enc_caps *dsc_common_caps,
362 const int target_bandwidth_kbps,
363 const struct dc_crtc_timing *timing,
364 const int num_slices_h,
367 bool should_use_dsc = false;
368 struct dc_dsc_bw_range range;
370 memset(&range, 0, sizeof(range));
372 get_dsc_bandwidth_range(policy->min_target_bpp * 16, policy->max_target_bpp * 16,
373 num_slices_h, dsc_common_caps, timing, &range);
374 if (!policy->enable_dsc_when_not_needed && target_bandwidth_kbps >= range.stream_kbps) {
375 /* enough bandwidth without dsc */
377 should_use_dsc = false;
378 } else if (policy->preferred_bpp_x16 > 0 &&
379 policy->preferred_bpp_x16 <= range.max_target_bpp_x16 &&
380 policy->preferred_bpp_x16 >= range.min_target_bpp_x16) {
381 *target_bpp_x16 = policy->preferred_bpp_x16;
382 should_use_dsc = true;
383 } else if (target_bandwidth_kbps >= range.max_kbps) {
384 /* use max target bpp allowed */
385 *target_bpp_x16 = range.max_target_bpp_x16;
386 should_use_dsc = true;
387 } else if (target_bandwidth_kbps >= range.min_kbps) {
388 /* use target bpp that can take entire target bandwidth */
389 *target_bpp_x16 = compute_bpp_x16_from_target_bandwidth(
390 target_bandwidth_kbps, timing, num_slices_h,
391 dsc_common_caps->bpp_increment_div,
392 dsc_common_caps->is_dp);
393 should_use_dsc = true;
395 /* not enough bandwidth to fulfill minimum requirement */
397 should_use_dsc = false;
400 return should_use_dsc;
403 #define MIN_AVAILABLE_SLICES_SIZE 4
405 static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps, int *available_slices)
409 memset(available_slices, -1, MIN_AVAILABLE_SLICES_SIZE);
411 if (slice_caps.bits.NUM_SLICES_1)
412 available_slices[idx++] = 1;
414 if (slice_caps.bits.NUM_SLICES_2)
415 available_slices[idx++] = 2;
417 if (slice_caps.bits.NUM_SLICES_4)
418 available_slices[idx++] = 4;
420 if (slice_caps.bits.NUM_SLICES_8)
421 available_slices[idx++] = 8;
427 static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps)
430 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
431 int end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
434 max_slices = available_slices[end_idx - 1];
440 // Increment sice number in available sice numbers stops if possible, or just increment if not
441 static int inc_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
443 // Get next bigger num slices available in common caps
444 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
447 int new_num_slices = num_slices;
449 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
451 // No available slices found
453 return new_num_slices;
456 // Numbers of slices found - get the next bigger number
457 for (i = 0; i < end_idx; i++) {
458 if (new_num_slices < available_slices[i]) {
459 new_num_slices = available_slices[i];
464 if (new_num_slices == num_slices) // No biger number of slices found
467 return new_num_slices;
471 // Decrement sice number in available sice numbers stops if possible, or just decrement if not. Stop at zero.
472 static int dec_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
474 // Get next bigger num slices available in common caps
475 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
478 int new_num_slices = num_slices;
480 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
481 if (end_idx == 0 && new_num_slices > 0) {
482 // No numbers of slices found
484 return new_num_slices;
487 // Numbers of slices found - get the next smaller number
488 for (i = end_idx - 1; i >= 0; i--) {
489 if (new_num_slices > available_slices[i]) {
490 new_num_slices = available_slices[i];
495 if (new_num_slices == num_slices) {
496 // No smaller number of slices found
498 if (new_num_slices < 0)
502 return new_num_slices;
506 // Choose next bigger number of slices if the requested number of slices is not available
507 static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps, int num_slices)
509 // Get next bigger num slices available in common caps
510 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
513 int new_num_slices = num_slices;
515 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
517 // No available slices found
519 return new_num_slices;
522 // Numbers of slices found - get the equal or next bigger number
523 for (i = 0; i < end_idx; i++) {
524 if (new_num_slices <= available_slices[i]) {
525 new_num_slices = available_slices[i];
530 return new_num_slices;
534 /* Attempts to set DSC configuration for the stream, applying DSC policy.
535 * Returns 'true' if successful or 'false' if not.
539 * dsc_sink_caps - DSC sink decoder capabilities (from DPCD)
541 * dsc_enc_caps - DSC encoder capabilities
543 * target_bandwidth_kbps - Target bandwidth to fit the stream into.
544 * If 0, do not calculate target bpp.
546 * timing - The stream timing to fit into 'target_bandwidth_kbps' or apply
547 * maximum compression to, if 'target_badwidth == 0'
549 * dsc_cfg - DSC configuration to use if it was possible to come up with
550 * one for the given inputs.
551 * The target bitrate after DSC can be calculated by multiplying
552 * dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g.
554 * dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0);
556 static bool setup_dsc_config(
557 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
558 const struct dsc_enc_caps *dsc_enc_caps,
559 int target_bandwidth_kbps,
560 const struct dc_crtc_timing *timing,
561 int min_slice_height_override,
562 int max_dsc_target_bpp_limit_override_x16,
563 struct dc_dsc_config *dsc_cfg)
565 struct dsc_enc_caps dsc_common_caps;
572 int sink_per_slice_throughput_mps;
573 int branch_max_throughput_mps = 0;
574 bool is_dsc_possible = false;
577 struct dc_dsc_policy policy;
579 memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
581 dc_dsc_get_policy_for_timing(timing, max_dsc_target_bpp_limit_override_x16, &policy);
582 pic_width = timing->h_addressable + timing->h_border_left + timing->h_border_right;
583 pic_height = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
585 if (!dsc_sink_caps->is_dsc_supported)
588 if (dsc_sink_caps->branch_max_line_width && dsc_sink_caps->branch_max_line_width < pic_width)
591 // Intersect decoder with encoder DSC caps and validate DSC settings
592 is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, dsc_enc_caps, timing->pixel_encoding, &dsc_common_caps);
593 if (!is_dsc_possible)
596 sink_per_slice_throughput_mps = 0;
598 // Validate available DSC settings against the mode timing
600 // Validate color format (and pick up the throughput values)
601 dsc_cfg->ycbcr422_simple = false;
602 switch (timing->pixel_encoding) {
603 case PIXEL_ENCODING_RGB:
604 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.RGB;
605 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
606 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
608 case PIXEL_ENCODING_YCBCR444:
609 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_444;
610 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
611 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
613 case PIXEL_ENCODING_YCBCR422:
614 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_422;
615 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
616 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
617 if (!is_dsc_possible) {
618 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_SIMPLE_422;
619 dsc_cfg->ycbcr422_simple = is_dsc_possible;
620 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
623 case PIXEL_ENCODING_YCBCR420:
624 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_420;
625 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
626 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
629 is_dsc_possible = false;
632 // Validate branch's maximum throughput
633 if (branch_max_throughput_mps && dsc_div_by_10_round_up(timing->pix_clk_100hz) > branch_max_throughput_mps * 1000)
634 is_dsc_possible = false;
636 if (!is_dsc_possible)
640 switch (timing->display_color_depth) {
641 case COLOR_DEPTH_888:
642 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_8_BPC;
644 case COLOR_DEPTH_101010:
645 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_10_BPC;
647 case COLOR_DEPTH_121212:
648 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_12_BPC;
651 is_dsc_possible = false;
654 if (!is_dsc_possible)
657 // Slice width (i.e. number of slices per line)
658 max_slices_h = get_max_dsc_slices(dsc_common_caps.slice_caps);
660 while (max_slices_h > 0) {
661 if (pic_width % max_slices_h == 0)
664 max_slices_h = dec_num_slices(dsc_common_caps.slice_caps, max_slices_h);
667 is_dsc_possible = (dsc_common_caps.max_slice_width > 0);
668 if (!is_dsc_possible)
671 min_slices_h = pic_width / dsc_common_caps.max_slice_width;
672 if (pic_width % dsc_common_caps.max_slice_width)
675 min_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, min_slices_h);
677 while (min_slices_h <= max_slices_h) {
678 int pix_clk_per_slice_khz = dsc_div_by_10_round_up(timing->pix_clk_100hz) / min_slices_h;
679 if (pix_clk_per_slice_khz <= sink_per_slice_throughput_mps * 1000)
682 min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
685 if (pic_width % min_slices_h != 0)
686 min_slices_h = 0; // DSC TODO: Maybe try increasing the number of slices first?
688 is_dsc_possible = (min_slices_h <= max_slices_h);
689 if (!is_dsc_possible)
692 if (policy.use_min_slices_h) {
693 if (min_slices_h > 0)
694 num_slices_h = min_slices_h;
695 else if (max_slices_h > 0) { // Fall back to max slices if min slices is not working out
696 if (policy.max_slices_h)
697 num_slices_h = min(policy.max_slices_h, max_slices_h);
699 num_slices_h = max_slices_h;
701 is_dsc_possible = false;
703 if (max_slices_h > 0) {
704 if (policy.max_slices_h)
705 num_slices_h = min(policy.max_slices_h, max_slices_h);
707 num_slices_h = max_slices_h;
708 } else if (min_slices_h > 0) // Fall back to min slices if max slices is not possible
709 num_slices_h = min_slices_h;
711 is_dsc_possible = false;
714 if (!is_dsc_possible)
717 dsc_cfg->num_slices_h = num_slices_h;
718 slice_width = pic_width / num_slices_h;
720 is_dsc_possible = slice_width <= dsc_common_caps.max_slice_width;
721 if (!is_dsc_possible)
724 // Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by.
725 // For 4:2:0 make sure the slice height is divisible by 2 as well.
726 if (min_slice_height_override == 0)
727 slice_height = min(policy.min_slice_height, pic_height);
729 slice_height = min(min_slice_height_override, pic_height);
731 while (slice_height < pic_height && (pic_height % slice_height != 0 ||
732 (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 && slice_height % 2 != 0)))
735 if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) // For the case when pic_height < dsc_policy.min_sice_height
736 is_dsc_possible = (slice_height % 2 == 0);
738 if (!is_dsc_possible)
741 dsc_cfg->num_slices_v = pic_height/slice_height;
743 if (target_bandwidth_kbps > 0) {
744 is_dsc_possible = decide_dsc_target_bpp_x16(
747 target_bandwidth_kbps,
751 dsc_cfg->bits_per_pixel = target_bpp;
753 if (!is_dsc_possible)
756 // Final decission: can we do DSC or not?
757 if (is_dsc_possible) {
758 // Fill out the rest of DSC settings
759 dsc_cfg->block_pred_enable = dsc_common_caps.is_block_pred_supported;
760 dsc_cfg->linebuf_depth = dsc_common_caps.lb_bit_depth;
761 dsc_cfg->version_minor = (dsc_common_caps.dsc_version & 0xf0) >> 4;
762 dsc_cfg->is_dp = dsc_sink_caps->is_dp;
766 if (!is_dsc_possible)
767 memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
769 return is_dsc_possible;
772 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)
774 if (!dpcd_dsc_basic_data)
777 dsc_sink_caps->is_dsc_supported = (dpcd_dsc_basic_data[DP_DSC_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_DECOMPRESSION_IS_SUPPORTED) != 0;
778 if (!dsc_sink_caps->is_dsc_supported)
781 dsc_sink_caps->dsc_version = dpcd_dsc_basic_data[DP_DSC_REV - DP_DSC_SUPPORT];
787 if (!dsc_buff_block_size_from_dpcd(dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT], &buff_block_size))
790 buff_size = dpcd_dsc_basic_data[DP_DSC_RC_BUF_SIZE - DP_DSC_SUPPORT] + 1;
791 dsc_sink_caps->rc_buffer_size = buff_size * buff_block_size;
794 dsc_sink_caps->slice_caps1.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
795 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))
798 dsc_sink_caps->is_block_pred_supported =
799 (dpcd_dsc_basic_data[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_BLK_PREDICTION_IS_SUPPORTED) != 0;
801 dsc_sink_caps->edp_max_bits_per_pixel =
802 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_LOW - DP_DSC_SUPPORT] |
803 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] << 8;
805 dsc_sink_caps->color_formats.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT];
806 dsc_sink_caps->color_depth.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
809 int dpcd_throughput = dpcd_dsc_basic_data[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];
811 if (!dsc_throughput_from_dpcd(dpcd_throughput & DP_DSC_THROUGHPUT_MODE_0_MASK, &dsc_sink_caps->throughput_mode_0_mps))
814 dpcd_throughput = (dpcd_throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >> DP_DSC_THROUGHPUT_MODE_1_SHIFT;
815 if (!dsc_throughput_from_dpcd(dpcd_throughput, &dsc_sink_caps->throughput_mode_1_mps))
819 dsc_sink_caps->max_slice_width = dpcd_dsc_basic_data[DP_DSC_MAX_SLICE_WIDTH - DP_DSC_SUPPORT] * 320;
820 dsc_sink_caps->slice_caps2.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
822 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))
825 if (dc->debug.dsc_bpp_increment_div) {
826 /* dsc_bpp_increment_div should onl be 1, 2, 4, 8 or 16, but rather than rejecting invalid values,
827 * we'll accept all and get it into range. This also makes the above check against 0 redundant,
828 * but that one stresses out the override will be only used if it's not 0.
830 if (dc->debug.dsc_bpp_increment_div >= 1)
831 dsc_sink_caps->bpp_increment_div = 1;
832 if (dc->debug.dsc_bpp_increment_div >= 2)
833 dsc_sink_caps->bpp_increment_div = 2;
834 if (dc->debug.dsc_bpp_increment_div >= 4)
835 dsc_sink_caps->bpp_increment_div = 4;
836 if (dc->debug.dsc_bpp_increment_div >= 8)
837 dsc_sink_caps->bpp_increment_div = 8;
838 if (dc->debug.dsc_bpp_increment_div >= 16)
839 dsc_sink_caps->bpp_increment_div = 16;
843 if (dpcd_dsc_branch_decoder_caps == NULL) { // branch decoder DPCD DSC data can be null for non branch device
844 dsc_sink_caps->branch_overall_throughput_0_mps = 0;
845 dsc_sink_caps->branch_overall_throughput_1_mps = 0;
846 dsc_sink_caps->branch_max_line_width = 0;
850 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];
851 if (dsc_sink_caps->branch_overall_throughput_0_mps == 0)
852 dsc_sink_caps->branch_overall_throughput_0_mps = 0;
853 else if (dsc_sink_caps->branch_overall_throughput_0_mps == 1)
854 dsc_sink_caps->branch_overall_throughput_0_mps = 680;
856 dsc_sink_caps->branch_overall_throughput_0_mps *= 50;
857 dsc_sink_caps->branch_overall_throughput_0_mps += 600;
860 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];
861 if (dsc_sink_caps->branch_overall_throughput_1_mps == 0)
862 dsc_sink_caps->branch_overall_throughput_1_mps = 0;
863 else if (dsc_sink_caps->branch_overall_throughput_1_mps == 1)
864 dsc_sink_caps->branch_overall_throughput_1_mps = 680;
866 dsc_sink_caps->branch_overall_throughput_1_mps *= 50;
867 dsc_sink_caps->branch_overall_throughput_1_mps += 600;
870 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;
871 ASSERT(dsc_sink_caps->branch_max_line_width == 0 || dsc_sink_caps->branch_max_line_width >= 5120);
873 dsc_sink_caps->is_dp = true;
878 /* If DSC is possbile, get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range and
879 * timing's pixel clock and uncompressed bandwidth.
880 * If DSC is not possible, leave '*range' untouched.
882 bool dc_dsc_compute_bandwidth_range(
883 const struct display_stream_compressor *dsc,
884 uint32_t dsc_min_slice_height_override,
885 uint32_t min_bpp_x16,
886 uint32_t max_bpp_x16,
887 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
888 const struct dc_crtc_timing *timing,
889 struct dc_dsc_bw_range *range)
891 bool is_dsc_possible = false;
892 struct dsc_enc_caps dsc_enc_caps;
893 struct dsc_enc_caps dsc_common_caps;
894 struct dc_dsc_config config;
896 get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
898 is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, &dsc_enc_caps,
899 timing->pixel_encoding, &dsc_common_caps);
902 is_dsc_possible = setup_dsc_config(dsc_sink_caps, &dsc_enc_caps, 0, timing,
903 dsc_min_slice_height_override, max_bpp_x16, &config);
906 get_dsc_bandwidth_range(min_bpp_x16, max_bpp_x16,
907 config.num_slices_h, &dsc_common_caps, timing, range);
909 return is_dsc_possible;
912 bool dc_dsc_compute_config(
913 const struct display_stream_compressor *dsc,
914 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
915 uint32_t dsc_min_slice_height_override,
916 uint32_t max_target_bpp_limit_override,
917 uint32_t target_bandwidth_kbps,
918 const struct dc_crtc_timing *timing,
919 struct dc_dsc_config *dsc_cfg)
921 bool is_dsc_possible = false;
922 struct dsc_enc_caps dsc_enc_caps;
924 get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
925 is_dsc_possible = setup_dsc_config(dsc_sink_caps,
927 target_bandwidth_kbps,
928 timing, dsc_min_slice_height_override,
929 max_target_bpp_limit_override * 16, dsc_cfg);
930 return is_dsc_possible;
933 uint32_t dc_dsc_stream_bandwidth_in_kbps(const struct dc_crtc_timing *timing,
934 uint32_t bpp_x16, uint32_t num_slices_h, bool is_dp)
936 struct fixed31_32 overhead_in_kbps;
937 struct fixed31_32 bpp;
938 struct fixed31_32 actual_bandwidth_in_kbps;
940 overhead_in_kbps = compute_dsc_max_bandwidth_overhead(
941 timing, num_slices_h, is_dp);
942 bpp = dc_fixpt_from_fraction(bpp_x16, 16);
943 actual_bandwidth_in_kbps = dc_fixpt_from_fraction(timing->pix_clk_100hz, 10);
944 actual_bandwidth_in_kbps = dc_fixpt_mul(actual_bandwidth_in_kbps, bpp);
945 actual_bandwidth_in_kbps = dc_fixpt_add(actual_bandwidth_in_kbps, overhead_in_kbps);
946 return dc_fixpt_ceil(actual_bandwidth_in_kbps);
949 void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing *timing, uint32_t max_target_bpp_limit_override_x16, struct dc_dsc_policy *policy)
953 policy->min_target_bpp = 0;
954 policy->max_target_bpp = 0;
956 /* DSC Policy: Use minimum number of slices that fits the pixel clock */
957 policy->use_min_slices_h = true;
959 /* DSC Policy: Use max available slices
960 * (in our case 4 for or 8, depending on the mode)
962 policy->max_slices_h = 0;
964 /* DSC Policy: Use slice height recommended
965 * by VESA DSC Spreadsheet user guide
967 policy->min_slice_height = 108;
969 /* DSC Policy: follow DP specs with an internal upper limit to 16 bpp
970 * for better interoperability
972 switch (timing->display_color_depth) {
973 case COLOR_DEPTH_888:
976 case COLOR_DEPTH_101010:
979 case COLOR_DEPTH_121212:
985 switch (timing->pixel_encoding) {
986 case PIXEL_ENCODING_RGB:
987 case PIXEL_ENCODING_YCBCR444:
988 case PIXEL_ENCODING_YCBCR422: /* assume no YCbCr422 native support */
989 /* DP specs limits to 8 */
990 policy->min_target_bpp = 8;
991 /* DP specs limits to 3 x bpc */
992 policy->max_target_bpp = 3 * bpc;
994 case PIXEL_ENCODING_YCBCR420:
995 /* DP specs limits to 6 */
996 policy->min_target_bpp = 6;
997 /* DP specs limits to 1.5 x bpc assume bpc is an even number */
998 policy->max_target_bpp = bpc * 3 / 2;
1004 policy->preferred_bpp_x16 = timing->dsc_fixed_bits_per_pixel_x16;
1006 /* internal upper limit, default 16 bpp */
1007 if (policy->max_target_bpp > dsc_policy_max_target_bpp_limit)
1008 policy->max_target_bpp = dsc_policy_max_target_bpp_limit;
1010 /* apply override */
1011 if (max_target_bpp_limit_override_x16 && policy->max_target_bpp > max_target_bpp_limit_override_x16 / 16)
1012 policy->max_target_bpp = max_target_bpp_limit_override_x16 / 16;
1014 /* enable DSC when not needed, default false */
1015 if (dsc_policy_enable_dsc_when_not_needed)
1016 policy->enable_dsc_when_not_needed = dsc_policy_enable_dsc_when_not_needed;
1018 policy->enable_dsc_when_not_needed = false;
1021 void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit)
1023 dsc_policy_max_target_bpp_limit = limit;
1026 void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable)
1028 dsc_policy_enable_dsc_when_not_needed = enable;
1031 void dc_dsc_policy_set_disable_dsc_stream_overhead(bool disable)
1033 dsc_policy_disable_dsc_stream_overhead = disable;